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: 48 53 90.6 %
Date: 2021-04-21 04:11:54 Branches: 29 38 76.3 %

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.h"
28
#include "node.h"
29
#include "node_binding.h"
30
#include "node_mutex.h"
31
#include "tracing/trace_event.h"
32
#include "util.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
}  // namespace per_process
59
60
// Forward declaration
61
class Environment;
62
63
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
64
// Sets address and port properties on the info object and returns it.
65
// If |info| is omitted, a new object is returned.
66
v8::Local<v8::Object> AddressToJS(
67
    Environment* env,
68
    const sockaddr* addr,
69
    v8::Local<v8::Object> info = v8::Local<v8::Object>());
70
71
template <typename T, int (*F)(const typename T::HandleType*, sockaddr*, int*)>
72
4478
void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
73
  T* wrap;
74

4478
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
75
                          args.Holder(),
76
                          args.GetReturnValue().Set(UV_EBADF));
77

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

4478
  if (err == 0)
83
8950
    AddressToJS(wrap->env(), addr, args[0].As<v8::Object>());
84
8956
  args.GetReturnValue().Set(err);
85
}
86
87
void PrintStackTrace(v8::Isolate* isolate, v8::Local<v8::StackTrace> stack);
88
void PrintCaughtException(v8::Isolate* isolate,
89
                          v8::Local<v8::Context> context,
90
                          const v8::TryCatch& try_catch);
91
92
void ResetStdio();  // Safe to call more than once and from signal handlers.
93
#ifdef __POSIX__
94
void SignalExit(int signal, siginfo_t* info, void* ucontext);
95
#endif
96
97
std::string GetProcessTitle(const char* default_title);
98
std::string GetHumanReadableProcessName();
99
100
void InitializeContextRuntime(v8::Local<v8::Context>);
101
bool InitializePrimordials(v8::Local<v8::Context> context);
102
103
14627
class NodeArrayBufferAllocator : public ArrayBufferAllocator {
104
 public:
105
1309609
  inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
106
107
  void* Allocate(size_t size) override;  // Defined in src/node.cc
108
  void* AllocateUninitialized(size_t size) override;
109
  void Free(void* data, size_t size) override;
110
  void* Reallocate(void* data, size_t old_size, size_t size) override;
111
  virtual void RegisterPointer(void* data, size_t size) {
112
    total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
113
  }
114
  virtual void UnregisterPointer(void* data, size_t size) {
115
    total_mem_usage_.fetch_sub(size, std::memory_order_relaxed);
116
  }
117
118
5126
  NodeArrayBufferAllocator* GetImpl() final { return this; }
119
522
  inline uint64_t total_mem_usage() const {
120
1044
    return total_mem_usage_.load(std::memory_order_relaxed);
121
  }
122
123
 private:
124
  uint32_t zero_fill_field_ = 1;  // Boolean but exposed as uint32 to JS land.
125
  std::atomic<size_t> total_mem_usage_ {0};
126
};
127
128
3
class DebuggingArrayBufferAllocator final : public NodeArrayBufferAllocator {
129
 public:
130
  ~DebuggingArrayBufferAllocator() override;
131
  void* Allocate(size_t size) override;
132
  void* AllocateUninitialized(size_t size) override;
133
  void Free(void* data, size_t size) override;
134
  void* Reallocate(void* data, size_t old_size, size_t size) override;
135
  void RegisterPointer(void* data, size_t size) override;
136
  void UnregisterPointer(void* data, size_t size) override;
137
138
 private:
139
  void RegisterPointerInternal(void* data, size_t size);
140
  void UnregisterPointerInternal(void* data, size_t size);
141
  Mutex mutex_;
142
  std::unordered_map<void*, size_t> allocations_;
143
};
144
145
namespace Buffer {
146
v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
147
v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
148
// Takes ownership of |data|.
149
v8::MaybeLocal<v8::Object> New(Environment* env,
150
                               char* data,
151
                               size_t length,
152
                               void (*callback)(char* data, void* hint),
153
                               void* hint);
154
// Takes ownership of |data|.  Must allocate |data| with the current Isolate's
155
// ArrayBuffer::Allocator().
156
v8::MaybeLocal<v8::Object> New(Environment* env,
157
                               char* data,
158
                               size_t length);
159
// Creates a Buffer instance over an existing ArrayBuffer.
160
v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
161
                                   v8::Local<v8::ArrayBuffer> ab,
162
                                   size_t byte_offset,
163
                                   size_t length);
164
// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
165
// Utf8Value and TwoByteValue).
166
// If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
167
// changed.
168
// If |buf| contains actual data, this method takes ownership of |buf|'s
169
// underlying buffer. However, |buf| itself can be reused even after this call,
170
// but its capacity, if increased through AllocateSufficientStorage, is not
171
// guaranteed to stay the same.
172
template <typename T>
173
1005
static v8::MaybeLocal<v8::Object> New(Environment* env,
174
                                      MaybeStackBuffer<T>* buf) {
175
  v8::MaybeLocal<v8::Object> ret;
176
1005
  char* src = reinterpret_cast<char*>(buf->out());
177
1005
  const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
178
179

1005
  if (buf->IsAllocated())
180
78
    ret = New(env, src, len_in_bytes);
181

927
  else if (!buf->IsInvalidated())
182
927
    ret = Copy(env, src, len_in_bytes);
183
184

1005
  if (ret.IsEmpty())
185
    return ret;
186
187

1005
  if (buf->IsAllocated())
188
78
    buf->Release();
189
190
1005
  return ret;
191
}
192
}  // namespace Buffer
193
194
v8::MaybeLocal<v8::Value> InternalMakeCallback(
195
    Environment* env,
196
    v8::Local<v8::Object> resource,
197
    v8::Local<v8::Object> recv,
198
    const v8::Local<v8::Function> callback,
199
    int argc,
200
    v8::Local<v8::Value> argv[],
201
    async_context asyncContext);
202
203
v8::MaybeLocal<v8::Value> MakeSyncCallback(v8::Isolate* isolate,
204
                                           v8::Local<v8::Object> recv,
205
                                           v8::Local<v8::Function> callback,
206
                                           int argc,
207
                                           v8::Local<v8::Value> argv[]);
208
209
class InternalCallbackScope {
210
 public:
211
  enum Flags {
212
    kNoFlags = 0,
213
    // Indicates whether 'before' and 'after' hooks should be skipped.
214
    kSkipAsyncHooks = 1,
215
    // Indicates whether nextTick and microtask queues should be skipped.
216
    // This should only be used when there is no call into JS in this scope.
217
    // (The HTTP parser also uses it for some weird backwards
218
    // compatibility issues, but it shouldn't.)
219
    kSkipTaskQueues = 2
220
  };
221
  InternalCallbackScope(Environment* env,
222
                        v8::Local<v8::Object> object,
223
                        const async_context& asyncContext,
224
                        int flags = kNoFlags);
225
  // Utility that can be used by AsyncWrap classes.
226
  explicit InternalCallbackScope(AsyncWrap* async_wrap, int flags = 0);
227
  ~InternalCallbackScope();
228
  void Close();
229
230
1416860
  inline bool Failed() const { return failed_; }
231
1062
  inline void MarkAsFailed() { failed_ = true; }
232
233
 private:
234
  Environment* env_;
235
  async_context async_context_;
236
  v8::Local<v8::Object> object_;
237
  bool skip_hooks_;
238
  bool skip_task_queues_;
239
  bool failed_ = false;
240
  bool pushed_ids_ = false;
241
  bool closed_ = false;
242
};
243
244
class DebugSealHandleScope {
245
 public:
246
1746134
  explicit inline DebugSealHandleScope(v8::Isolate* isolate = nullptr)
247
#ifdef DEBUG
248
    : actual_scope_(isolate != nullptr ? isolate : v8::Isolate::GetCurrent())
249
#endif
250
1746134
  {}
251
252
 private:
253
#ifdef DEBUG
254
  v8::SealHandleScope actual_scope_;
255
#endif
256
};
257
258
class ThreadPoolWork {
259
 public:
260
5852
  explicit inline ThreadPoolWork(Environment* env) : env_(env) {
261
5852
    CHECK_NOT_NULL(env);
262
5852
  }
263
5794
  inline virtual ~ThreadPoolWork() = default;
264
265
  inline void ScheduleWork();
266
  inline int CancelWork();
267
268
  virtual void DoThreadPoolWork() = 0;
269
  virtual void AfterThreadPoolWork(int status) = 0;
270
271
  Environment* env() const { return env_; }
272
273
 private:
274
  Environment* env_;
275
  uv_work_t work_req_;
276
};
277
278
#define TRACING_CATEGORY_NODE "node"
279
#define TRACING_CATEGORY_NODE1(one)                                           \
280
    TRACING_CATEGORY_NODE ","                                                 \
281
    TRACING_CATEGORY_NODE "." #one
282
#define TRACING_CATEGORY_NODE2(one, two)                                      \
283
    TRACING_CATEGORY_NODE ","                                                 \
284
    TRACING_CATEGORY_NODE "." #one ","                                        \
285
    TRACING_CATEGORY_NODE "." #one "." #two
286
287
// Functions defined in node.cc that are exposed via the bootstrapper object
288
289
#if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
290
#define NODE_IMPLEMENTS_POSIX_CREDENTIALS 1
291
#endif  // __POSIX__ && !defined(__ANDROID__) && !defined(__CloudABI__)
292
293
namespace credentials {
294
bool SafeGetenv(const char* key, std::string* text, Environment* env = nullptr);
295
}  // namespace credentials
296
297
void DefineZlibConstants(v8::Local<v8::Object> target);
298
v8::Isolate* NewIsolate(v8::Isolate::CreateParams* params,
299
                        uv_loop_t* event_loop,
300
                        MultiIsolatePlatform* platform);
301
// This overload automatically picks the right 'main_script_id' if no callback
302
// was provided by the embedder.
303
v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
304
                                         StartExecutionCallback cb = nullptr);
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
9053
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
void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
322
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
323
void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
324
325
#if HAVE_INSPECTOR
326
namespace profiler {
327
void StartProfilers(Environment* env);
328
}
329
#endif  // HAVE_INSPECTOR
330
331
#ifdef __POSIX__
332
static constexpr unsigned kMaxSignal = 32;
333
#endif
334
335
bool HasSignalJSHandler(int signum);
336
337
#ifdef _WIN32
338
typedef SYSTEMTIME TIME_TYPE;
339
#else  // UNIX, OSX
340
typedef struct tm TIME_TYPE;
341
#endif
342
343
double GetCurrentTimeInMicroseconds();
344
int WriteFileSync(const char* path, uv_buf_t buf);
345
int WriteFileSync(v8::Isolate* isolate,
346
                  const char* path,
347
                  v8::Local<v8::String> string);
348
349
32
class DiagnosticFilename {
350
 public:
351
  static void LocalTime(TIME_TYPE* tm_struct);
352
353
  inline DiagnosticFilename(Environment* env,
354
                            const char* prefix,
355
                            const char* ext);
356
357
  inline DiagnosticFilename(uint64_t thread_id,
358
                            const char* prefix,
359
                            const char* ext);
360
361
  inline const char* operator*() const;
362
363
 private:
364
  static std::string MakeFilename(
365
      uint64_t thread_id,
366
      const char* prefix,
367
      const char* ext);
368
369
  std::string filename_;
370
};
371
372
namespace heap {
373
bool WriteSnapshot(v8::Isolate* isolate, const char* filename);
374
}
375
376
class TraceEventScope {
377
 public:
378
1080473
  TraceEventScope(const char* category,
379
                  const char* name,
380
1080473
                  void* id) : category_(category), name_(name), id_(id) {
381
    TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(category_, name_, id_);
382
2160948
  }
383
2160891
  ~TraceEventScope() {
384
4700
    TRACE_EVENT_NESTABLE_ASYNC_END0(category_, name_, id_);
385

3241364
  }
386
387
4782
 private:
388
1080526
  const char* category_;
389
164
  const char* name_;
390
80
  void* id_;
391
80
};
392
161
393
namespace heap {
394
395
void DeleteHeapSnapshot(const v8::HeapSnapshot* snapshot);
396
using HeapSnapshotPointer =
397
  DeleteFnPtr<const v8::HeapSnapshot, DeleteHeapSnapshot>;
398
399
BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
400
    Environment* env, HeapSnapshotPointer&& snapshot);
401
}  // namespace heap
402
403
namespace fs {
404
std::string Basename(const std::string& str, const std::string& extension);
405
}  // namespace fs
406
407
node_module napi_module_to_node_module(const napi_module* mod);
408
409
}  // namespace node
410
411
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
412
413
#endif  // SRC_NODE_INTERNALS_H_