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: 67 69 97.1 %
Date: 2019-05-05 22:32:45 Branches: 32 44 72.7 %

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 "tracing/trace_event.h"
32
#include "util-inl.h"
33
#include "uv.h"
34
#include "v8.h"
35
36
#include <cstdint>
37
#include <cstdlib>
38
39
#include <string>
40
#include <vector>
41
42
// Custom constants used by both node_constants.cc and node_zlib.cc
43
#define Z_MIN_WINDOWBITS 8
44
#define Z_MAX_WINDOWBITS 15
45
#define Z_DEFAULT_WINDOWBITS 15
46
47
struct sockaddr;
48
49
namespace node {
50
51
namespace native_module {
52
class NativeModuleLoader;
53
}
54
55
namespace per_process {
56
extern Mutex env_var_mutex;
57
extern uint64_t node_start_time;
58
extern bool v8_is_profiling;
59
}  // namespace per_process
60
61
// Forward declaration
62
class Environment;
63
64
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
65
// Sets address and port properties on the info object and returns it.
66
// If |info| is omitted, a new object is returned.
67
v8::Local<v8::Object> AddressToJS(
68
    Environment* env,
69
    const sockaddr* addr,
70
    v8::Local<v8::Object> info = v8::Local<v8::Object>());
71
72
template <typename T, int (*F)(const typename T::HandleType*, sockaddr*, int*)>
73
3844
void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
74
  T* wrap;
75

7688
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
76
                          args.Holder(),
77
                          args.GetReturnValue().Set(UV_EBADF));
78

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

3844
  if (err == 0)
84
7678
    AddressToJS(wrap->env(), addr, args[0].As<v8::Object>());
85
7688
  args.GetReturnValue().Set(err);
86
}
87
88
void PrintStackTrace(v8::Isolate* isolate, v8::Local<v8::StackTrace> stack);
89
void PrintCaughtException(v8::Isolate* isolate,
90
                          v8::Local<v8::Context> context,
91
                          const v8::TryCatch& try_catch);
92
93
void WaitForInspectorDisconnect(Environment* env);
94
void SignalExit(int signo);
95
#ifdef __POSIX__
96
void RegisterSignalHandler(int signal,
97
                           void (*handler)(int signal),
98
                           bool reset_handler = false);
99
#endif
100
101
std::string GetHumanReadableProcessName();
102
void GetHumanReadableProcessName(char (*name)[1024]);
103
104
namespace task_queue {
105
void PromiseRejectCallback(v8::PromiseRejectMessage message);
106
}  // namespace task_queue
107
108
13202
class NodeArrayBufferAllocator : public ArrayBufferAllocator {
109
 public:
110
4650
  inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
111
112
  void* Allocate(size_t size) override;  // Defined in src/node.cc
113
463044
  void* AllocateUninitialized(size_t size) override
114
463044
    { return node::UncheckedMalloc(size); }
115
1349571
  void Free(void* data, size_t) override { free(data); }
116
327117
  virtual void* Reallocate(void* data, size_t old_size, size_t size) {
117
    return static_cast<void*>(
118
327117
        UncheckedRealloc<char>(static_cast<char*>(data), size));
119
  }
120
358
  virtual void RegisterPointer(void* data, size_t size) {}
121
3
  virtual void UnregisterPointer(void* data, size_t size) {}
122
123
4654
  NodeArrayBufferAllocator* GetImpl() final { return this; }
124
125
 private:
126
  uint32_t zero_fill_field_ = 1;  // Boolean but exposed as uint32 to JS land.
127
};
128
129
class DebuggingArrayBufferAllocator final : public NodeArrayBufferAllocator {
130
 public:
131
  ~DebuggingArrayBufferAllocator() override;
132
  void* Allocate(size_t size) override;
133
  void* AllocateUninitialized(size_t size) override;
134
  void Free(void* data, size_t size) override;
135
  void* Reallocate(void* data, size_t old_size, size_t size) override;
136
  void RegisterPointer(void* data, size_t size) override;
137
  void UnregisterPointer(void* data, size_t size) override;
138
139
 private:
140
  void RegisterPointerInternal(void* data, size_t size);
141
  void UnregisterPointerInternal(void* data, size_t size);
142
  Mutex mutex_;
143
  std::unordered_map<void*, size_t> allocations_;
144
};
145
146
namespace Buffer {
147
v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
148
v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
149
// Takes ownership of |data|.
150
v8::MaybeLocal<v8::Object> New(Environment* env,
151
                               char* data,
152
                               size_t length,
153
                               void (*callback)(char* data, void* hint),
154
                               void* hint);
155
// Takes ownership of |data|.  Must allocate |data| with the current Isolate's
156
// ArrayBuffer::Allocator().
157
v8::MaybeLocal<v8::Object> New(Environment* env,
158
                               char* data,
159
                               size_t length,
160
                               bool uses_malloc);
161
162
// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
163
// Utf8Value and TwoByteValue).
164
// If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
165
// changed.
166
// If |buf| contains actual data, this method takes ownership of |buf|'s
167
// underlying buffer. However, |buf| itself can be reused even after this call,
168
// but its capacity, if increased through AllocateSufficientStorage, is not
169
// guaranteed to stay the same.
170
template <typename T>
171
534
static v8::MaybeLocal<v8::Object> New(Environment* env,
172
                                      MaybeStackBuffer<T>* buf) {
173
  v8::MaybeLocal<v8::Object> ret;
174
534
  char* src = reinterpret_cast<char*>(buf->out());
175
534
  const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
176
177

534
  if (buf->IsAllocated())
178
2
    ret = New(env, src, len_in_bytes, true);
179

532
  else if (!buf->IsInvalidated())
180
532
    ret = Copy(env, src, len_in_bytes);
181
182

534
  if (ret.IsEmpty())
183
    return ret;
184
185

534
  if (buf->IsAllocated())
186
2
    buf->Release();
187
188
534
  return ret;
189
}
190
}  // namespace Buffer
191
192
v8::MaybeLocal<v8::Value> InternalMakeCallback(
193
    Environment* env,
194
    v8::Local<v8::Object> recv,
195
    const v8::Local<v8::Function> callback,
196
    int argc,
197
    v8::Local<v8::Value> argv[],
198
    async_context asyncContext);
199
200
class InternalCallbackScope {
201
 public:
202
  // Tell the constructor whether its `object` parameter may be empty or not.
203
  enum ResourceExpectation { kRequireResource, kAllowEmptyResource };
204
  InternalCallbackScope(Environment* env,
205
                        v8::Local<v8::Object> object,
206
                        const async_context& asyncContext,
207
                        ResourceExpectation expect = kRequireResource);
208
  // Utility that can be used by AsyncWrap classes.
209
  explicit InternalCallbackScope(AsyncWrap* async_wrap);
210
  ~InternalCallbackScope();
211
  void Close();
212
213
1864844
  inline bool Failed() const { return failed_; }
214
584
  inline void MarkAsFailed() { failed_ = true; }
215
216
 private:
217
  Environment* env_;
218
  async_context async_context_;
219
  v8::Local<v8::Object> object_;
220
  AsyncCallbackScope callback_scope_;
221
  bool failed_ = false;
222
  bool pushed_ids_ = false;
223
  bool closed_ = false;
224
};
225
226
class DebugSealHandleScope {
227
 public:
228
973986
  explicit inline DebugSealHandleScope(v8::Isolate* isolate)
229
#ifdef DEBUG
230
    : actual_scope_(isolate)
231
#endif
232
973986
  {}
233
234
 private:
235
#ifdef DEBUG
236
  v8::SealHandleScope actual_scope_;
237
#endif
238
};
239
240
class ThreadPoolWork {
241
 public:
242
2667
  explicit inline ThreadPoolWork(Environment* env) : env_(env) {
243
2667
    CHECK_NOT_NULL(env);
244
2667
  }
245
2558
  inline virtual ~ThreadPoolWork() = default;
246
247
  inline void ScheduleWork();
248
  inline int CancelWork();
249
250
  virtual void DoThreadPoolWork() = 0;
251
  virtual void AfterThreadPoolWork(int status) = 0;
252
253
 private:
254
  Environment* env_;
255
  uv_work_t work_req_;
256
};
257
258
9014
void ThreadPoolWork::ScheduleWork() {
259
9014
  env_->IncreaseWaitingRequestCounter();
260
  int status = uv_queue_work(
261
      env_->event_loop(),
262
      &work_req_,
263
27040
      [](uv_work_t* req) {
264
9013
        ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req);
265
9013
        self->DoThreadPoolWork();
266
27040
      },
267
27042
      [](uv_work_t* req, int status) {
268
9014
        ThreadPoolWork* self = ContainerOf(&ThreadPoolWork::work_req_, req);
269
9014
        self->env_->DecreaseWaitingRequestCounter();
270
9014
        self->AfterThreadPoolWork(status);
271
36055
      });
272
9014
  CHECK_EQ(status, 0);
273
9014
}
274
275
1
int ThreadPoolWork::CancelWork() {
276
1
  return uv_cancel(reinterpret_cast<uv_req_t*>(&work_req_));
277
}
278
279
#define TRACING_CATEGORY_NODE "node"
280
#define TRACING_CATEGORY_NODE1(one)                                           \
281
    TRACING_CATEGORY_NODE ","                                                 \
282
    TRACING_CATEGORY_NODE "." #one
283
#define TRACING_CATEGORY_NODE2(one, two)                                      \
284
    TRACING_CATEGORY_NODE ","                                                 \
285
    TRACING_CATEGORY_NODE "." #one ","                                        \
286
    TRACING_CATEGORY_NODE "." #one "." #two
287
288
// Functions defined in node.cc that are exposed via the bootstrapper object
289
290
#if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
291
#define NODE_IMPLEMENTS_POSIX_CREDENTIALS 1
292
#endif  // __POSIX__ && !defined(__ANDROID__) && !defined(__CloudABI__)
293
294
namespace credentials {
295
bool SafeGetenv(const char* key, std::string* text, Environment* env = nullptr);
296
}  // namespace credentials
297
298
void DefineZlibConstants(v8::Local<v8::Object> target);
299
v8::Isolate* NewIsolate(v8::Isolate::CreateParams* params,
300
                        uv_loop_t* event_loop,
301
                        MultiIsolatePlatform* platform);
302
v8::MaybeLocal<v8::Value> RunBootstrapping(Environment* env);
303
v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
304
                                         const char* main_script_id);
305
v8::MaybeLocal<v8::Object> GetPerContextExports(v8::Local<v8::Context> context);
306
v8::MaybeLocal<v8::Value> ExecuteBootstrapper(
307
    Environment* env,
308
    const char* id,
309
    std::vector<v8::Local<v8::String>>* parameters,
310
    std::vector<v8::Local<v8::Value>>* arguments);
311
void MarkBootstrapComplete(const v8::FunctionCallbackInfo<v8::Value>& args);
312
313
8665
struct InitializationResult {
314
  int exit_code = 0;
315
  std::vector<std::string> args;
316
  std::vector<std::string> exec_args;
317
  bool early_return = false;
318
};
319
InitializationResult InitializeOncePerProcess(int argc, char** argv);
320
void TearDownOncePerProcess();
321
enum class IsolateSettingCategories { kErrorHandlers, kMisc };
322
void SetIsolateUpForNode(v8::Isolate* isolate, IsolateSettingCategories cat);
323
void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
324
325
#if HAVE_INSPECTOR
326
namespace profiler {
327
void StartProfilers(Environment* env);
328
void EndStartedProfilers(Environment* env);
329
}
330
#endif  // HAVE_INSPECTOR
331
332
#ifdef _WIN32
333
typedef SYSTEMTIME TIME_TYPE;
334
#else  // UNIX, OSX
335
typedef struct tm TIME_TYPE;
336
#endif
337
338
double GetCurrentTimeInMicroseconds();
339
int WriteFileSync(const char* path, uv_buf_t buf);
340
int WriteFileSync(v8::Isolate* isolate,
341
                  const char* path,
342
                  v8::Local<v8::String> string);
343
344
16
class DiagnosticFilename {
345
 public:
346
  static void LocalTime(TIME_TYPE* tm_struct);
347
348
11
  DiagnosticFilename(Environment* env,
349
                     const char* prefix,
350
                     const char* ext) :
351
11
      filename_(MakeFilename(env->thread_id(), prefix, ext)) {}
352
353
5
  DiagnosticFilename(uint64_t thread_id,
354
                     const char* prefix,
355
                     const char* ext) :
356
5
      filename_(MakeFilename(thread_id, prefix, ext)) {}
357
358
19
  const char* operator*() const { return filename_.c_str(); }
359
360
 private:
361
  static std::string MakeFilename(
362
      uint64_t thread_id,
363
      const char* prefix,
364
      const char* ext);
365
366
  std::string filename_;
367
};
368
369
class TraceEventScope {
370
 public:
371
472079
  TraceEventScope(const char* category,
372
                  const char* name,
373
472079
                  void* id) : category_(category), name_(name), id_(id) {
374

944164
    TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(category_, name_, id_);
375
472082
  }
376
472049
  ~TraceEventScope() {
377

944094
    TRACE_EVENT_NESTABLE_ASYNC_END0(category_, name_, id_);
378
472047
  }
379
380
 private:
381
  const char* category_;
382
  const char* name_;
383
  void* id_;
384
};
385
386
}  // namespace node
387
388
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
389
390
#endif  // SRC_NODE_INTERNALS_H_