GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_internals.h Lines: 52 54 96.3 %
Date: 2019-02-13 22:28:58 Branches: 26 40 65.0 %

Line Branch Exec Source
1
// Copyright Joyent, Inc. and other Node contributors.
2
//
3
// Permission is hereby granted, free of charge, to any person obtaining a
4
// copy of this software and associated documentation files (the
5
// "Software"), to deal in the Software without restriction, including
6
// without limitation the rights to use, copy, modify, merge, publish,
7
// distribute, sublicense, and/or sell copies of the Software, and to permit
8
// persons to whom the Software is furnished to do so, subject to the
9
// following conditions:
10
//
11
// The above copyright notice and this permission notice shall be included
12
// in all copies or substantial portions of the Software.
13
//
14
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22
#ifndef SRC_NODE_INTERNALS_H_
23
#define SRC_NODE_INTERNALS_H_
24
25
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
26
27
#include "env-inl.h"
28
#include "node.h"
29
#include "node_binding.h"
30
#include "node_mutex.h"
31
#include "node_persistent.h"
32
#include "tracing/trace_event.h"
33
#include "util-inl.h"
34
#include "uv.h"
35
#include "v8.h"
36
37
#include <stdint.h>
38
#include <stdlib.h>
39
40
#include <string>
41
#include <vector>
42
43
// Custom constants used by both node_constants.cc and node_zlib.cc
44
#define Z_MIN_WINDOWBITS 8
45
#define Z_MAX_WINDOWBITS 15
46
#define Z_DEFAULT_WINDOWBITS 15
47
48
struct sockaddr;
49
50
namespace node {
51
52
namespace native_module {
53
class NativeModuleLoader;
54
}
55
56
namespace per_process {
57
extern Mutex env_var_mutex;
58
extern double prog_start_time;
59
extern bool v8_is_profiling;
60
}  // namespace per_process
61
62
// Forward declaration
63
class Environment;
64
65
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
66
// Sets address and port properties on the info object and returns it.
67
// If |info| is omitted, a new object is returned.
68
v8::Local<v8::Object> AddressToJS(
69
    Environment* env,
70
    const sockaddr* addr,
71
    v8::Local<v8::Object> info = v8::Local<v8::Object>());
72
73
template <typename T, int (*F)(const typename T::HandleType*, sockaddr*, int*)>
74
3556
void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
75
  T* wrap;
76

7112
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
77
                          args.Holder(),
78
                          args.GetReturnValue().Set(UV_EBADF));
79

7112
  CHECK(args[0]->IsObject());
80
  sockaddr_storage storage;
81
3556
  int addrlen = sizeof(storage);
82
3556
  sockaddr* const addr = reinterpret_cast<sockaddr*>(&storage);
83
3556
  const int err = F(&wrap->handle_, addr, &addrlen);
84

3556
  if (err == 0)
85
7102
    AddressToJS(wrap->env(), addr, args[0].As<v8::Object>());
86
7112
  args.GetReturnValue().Set(err);
87
}
88
89
void WaitForInspectorDisconnect(Environment* env);
90
void SignalExit(int signo);
91
#ifdef __POSIX__
92
void RegisterSignalHandler(int signal,
93
                           void (*handler)(int signal),
94
                           bool reset_handler = false);
95
#endif
96
97
std::string GetHumanReadableProcessName();
98
void GetHumanReadableProcessName(char (*name)[1024]);
99
100
namespace task_queue {
101
void PromiseRejectCallback(v8::PromiseRejectMessage message);
102
}  // namespace task_queue
103
104
12532
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
105
 public:
106
4421
  inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
107
108
  virtual void* Allocate(size_t size);  // Defined in src/node.cc
109
6086
  virtual void* AllocateUninitialized(size_t size)
110
6086
    { return node::UncheckedMalloc(size); }
111
1340239
  virtual void Free(void* data, size_t) { free(data); }
112
113
 private:
114
  uint32_t zero_fill_field_ = 1;  // Boolean but exposed as uint32 to JS land.
115
};
116
117
namespace Buffer {
118
v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
119
v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
120
// Takes ownership of |data|.
121
v8::MaybeLocal<v8::Object> New(Environment* env,
122
                               char* data,
123
                               size_t length,
124
                               void (*callback)(char* data, void* hint),
125
                               void* hint);
126
// Takes ownership of |data|.  Must allocate |data| with malloc() or realloc()
127
// because ArrayBufferAllocator::Free() deallocates it again with free().
128
// Mixing operator new and free() is undefined behavior so don't do that.
129
v8::MaybeLocal<v8::Object> New(Environment* env, char* data, size_t length);
130
131
inline
132
50287
v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
133
                                   v8::Local<v8::ArrayBuffer> ab,
134
                                   size_t byte_offset,
135
                                   size_t length) {
136
50287
  v8::Local<v8::Uint8Array> ui = v8::Uint8Array::New(ab, byte_offset, length);
137
100574
  CHECK(!env->buffer_prototype_object().IsEmpty());
138
  v8::Maybe<bool> mb =
139
150861
      ui->SetPrototype(env->context(), env->buffer_prototype_object());
140
50287
  if (mb.IsNothing())
141
    return v8::MaybeLocal<v8::Uint8Array>();
142
50287
  return ui;
143
}
144
145
// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
146
// Utf8Value and TwoByteValue).
147
// If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
148
// changed.
149
// If |buf| contains actual data, this method takes ownership of |buf|'s
150
// underlying buffer. However, |buf| itself can be reused even after this call,
151
// but its capacity, if increased through AllocateSufficientStorage, is not
152
// guaranteed to stay the same.
153
template <typename T>
154
534
static v8::MaybeLocal<v8::Object> New(Environment* env,
155
                                      MaybeStackBuffer<T>* buf) {
156
  v8::MaybeLocal<v8::Object> ret;
157
534
  char* src = reinterpret_cast<char*>(buf->out());
158
534
  const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
159
160

534
  if (buf->IsAllocated())
161
2
    ret = New(env, src, len_in_bytes);
162

532
  else if (!buf->IsInvalidated())
163
532
    ret = Copy(env, src, len_in_bytes);
164
165

534
  if (ret.IsEmpty())
166
    return ret;
167
168

534
  if (buf->IsAllocated())
169
2
    buf->Release();
170
171
534
  return ret;
172
}
173
}  // namespace Buffer
174
175
v8::MaybeLocal<v8::Value> InternalMakeCallback(
176
    Environment* env,
177
    v8::Local<v8::Object> recv,
178
    const v8::Local<v8::Function> callback,
179
    int argc,
180
    v8::Local<v8::Value> argv[],
181
    async_context asyncContext);
182
183
class InternalCallbackScope {
184
 public:
185
  // Tell the constructor whether its `object` parameter may be empty or not.
186
  enum ResourceExpectation { kRequireResource, kAllowEmptyResource };
187
  InternalCallbackScope(Environment* env,
188
                        v8::Local<v8::Object> object,
189
                        const async_context& asyncContext,
190
                        ResourceExpectation expect = kRequireResource);
191
  // Utility that can be used by AsyncWrap classes.
192
  explicit InternalCallbackScope(AsyncWrap* async_wrap);
193
  ~InternalCallbackScope();
194
  void Close();
195
196
2377298
  inline bool Failed() const { return failed_; }
197
583
  inline void MarkAsFailed() { failed_ = true; }
198
199
 private:
200
  Environment* env_;
201
  async_context async_context_;
202
  v8::Local<v8::Object> object_;
203
  Environment::AsyncCallbackScope callback_scope_;
204
  bool failed_ = false;
205
  bool pushed_ids_ = false;
206
  bool closed_ = false;
207
};
208
209
class ThreadPoolWork {
210
 public:
211
2439
  explicit inline ThreadPoolWork(Environment* env) : env_(env) {
212
2439
    CHECK_NOT_NULL(env);
213
2439
  }
214
2334
  inline virtual ~ThreadPoolWork() = default;
215
216
  inline void ScheduleWork();
217
  inline int CancelWork();
218
219
  virtual void DoThreadPoolWork() = 0;
220
  virtual void AfterThreadPoolWork(int status) = 0;
221
222
 private:
223
  Environment* env_;
224
  uv_work_t work_req_;
225
};
226
227
9089
void ThreadPoolWork::ScheduleWork() {
228
9089
  env_->IncreaseWaitingRequestCounter();
229
  int status = uv_queue_work(
230
      env_->event_loop(),
231
      &work_req_,
232
27265
      [](uv_work_t* req) {
233
9088
        ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req);
234
9088
        self->DoThreadPoolWork();
235
27264
      },
236
27267
      [](uv_work_t* req, int status) {
237
9089
        ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req);
238
9089
        self->env_->DecreaseWaitingRequestCounter();
239
9089
        self->AfterThreadPoolWork(status);
240
36355
      });
241
9089
  CHECK_EQ(status, 0);
242
9089
}
243
244
1
int ThreadPoolWork::CancelWork() {
245
1
  return uv_cancel(reinterpret_cast<uv_req_t*>(&work_req_));
246
}
247
248
#define TRACING_CATEGORY_NODE "node"
249
#define TRACING_CATEGORY_NODE1(one)                                           \
250
    TRACING_CATEGORY_NODE ","                                                 \
251
    TRACING_CATEGORY_NODE "." #one
252
#define TRACING_CATEGORY_NODE2(one, two)                                      \
253
    TRACING_CATEGORY_NODE ","                                                 \
254
    TRACING_CATEGORY_NODE "." #one ","                                        \
255
    TRACING_CATEGORY_NODE "." #one "." #two
256
257
// Functions defined in node.cc that are exposed via the bootstrapper object
258
259
#if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
260
#define NODE_IMPLEMENTS_POSIX_CREDENTIALS 1
261
#endif  // __POSIX__ && !defined(__ANDROID__) && !defined(__CloudABI__)
262
263
namespace credentials {
264
bool SafeGetenv(const char* key, std::string* text);
265
}  // namespace credentials
266
267
void DefineZlibConstants(v8::Local<v8::Object> target);
268
269
v8::MaybeLocal<v8::Value> RunBootstrapping(Environment* env);
270
v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
271
                                         const char* main_script_id);
272
273
}  // namespace node
274
275
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
276
277
#endif  // SRC_NODE_INTERNALS_H_