GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: node_internals.h Lines: 37 43 86.0 %
Date: 2022-08-31 04:20:31 Branches: 1 2 50.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.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
struct sockaddr;
43
44
namespace node {
45
46
namespace builtins {
47
class BuiltinLoader;
48
}
49
50
namespace per_process {
51
extern Mutex env_var_mutex;
52
extern uint64_t node_start_time;
53
}  // namespace per_process
54
55
// Forward declaration
56
class Environment;
57
58
// Convert a struct sockaddr to a { address: '1.2.3.4', port: 1234 } JS object.
59
// Sets address and port properties on the info object and returns it.
60
// If |info| is omitted, a new object is returned.
61
v8::MaybeLocal<v8::Object> AddressToJS(
62
    Environment* env,
63
    const sockaddr* addr,
64
    v8::Local<v8::Object> info = v8::Local<v8::Object>());
65
66
template <typename T, int (*F)(const typename T::HandleType*, sockaddr*, int*)>
67
4544
void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
68
  T* wrap;
69
4544
  ASSIGN_OR_RETURN_UNWRAP(&wrap,
70
                          args.Holder(),
71
                          args.GetReturnValue().Set(UV_EBADF));
72
4544
  CHECK(args[0]->IsObject());
73
  sockaddr_storage storage;
74
4544
  int addrlen = sizeof(storage);
75
4544
  sockaddr* const addr = reinterpret_cast<sockaddr*>(&storage);
76
4544
  const int err = F(&wrap->handle_, addr, &addrlen);
77
4544
  if (err == 0)
78
9084
    AddressToJS(wrap->env(), addr, args[0].As<v8::Object>());
79
9088
  args.GetReturnValue().Set(err);
80
}
81
82
void PrintStackTrace(v8::Isolate* isolate, v8::Local<v8::StackTrace> stack);
83
void PrintCaughtException(v8::Isolate* isolate,
84
                          v8::Local<v8::Context> context,
85
                          const v8::TryCatch& try_catch);
86
87
void ResetStdio();  // Safe to call more than once and from signal handlers.
88
#ifdef __POSIX__
89
void SignalExit(int signal, siginfo_t* info, void* ucontext);
90
#endif
91
92
std::string GetProcessTitle(const char* default_title);
93
std::string GetHumanReadableProcessName();
94
95
v8::Maybe<bool> InitializeBaseContextForSnapshot(
96
    v8::Local<v8::Context> context);
97
v8::Maybe<bool> InitializeContextRuntime(v8::Local<v8::Context> context);
98
v8::Maybe<bool> InitializePrimordials(v8::Local<v8::Context> context);
99
100
class NodeArrayBufferAllocator : public ArrayBufferAllocator {
101
 public:
102
237353
  inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
103
104
  void* Allocate(size_t size) override;  // Defined in src/node.cc
105
  void* AllocateUninitialized(size_t size) override;
106
  void Free(void* data, size_t size) override;
107
  void* Reallocate(void* data, size_t old_size, size_t size) override;
108
  virtual void RegisterPointer(void* data, size_t size) {
109
    total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
110
  }
111
  virtual void UnregisterPointer(void* data, size_t size) {
112
    total_mem_usage_.fetch_sub(size, std::memory_order_relaxed);
113
  }
114
115
6155
  NodeArrayBufferAllocator* GetImpl() final { return this; }
116
522
  inline uint64_t total_mem_usage() const {
117
1044
    return total_mem_usage_.load(std::memory_order_relaxed);
118
  }
119
120
 private:
121
  uint32_t zero_fill_field_ = 1;  // Boolean but exposed as uint32 to JS land.
122
  std::atomic<size_t> total_mem_usage_ {0};
123
124
  // Delegate to V8's allocator for compatibility with the V8 memory cage.
125
  std::unique_ptr<v8::ArrayBuffer::Allocator> allocator_{
126
      v8::ArrayBuffer::Allocator::NewDefaultAllocator()};
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
// Creates a Buffer instance over an existing ArrayBuffer.
161
v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
162
                                   v8::Local<v8::ArrayBuffer> ab,
163
                                   size_t byte_offset,
164
                                   size_t length);
165
// Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
166
// Utf8Value and TwoByteValue).
167
// If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
168
// changed.
169
// If |buf| contains actual data, this method takes ownership of |buf|'s
170
// underlying buffer. However, |buf| itself can be reused even after this call,
171
// but its capacity, if increased through AllocateSufficientStorage, is not
172
// guaranteed to stay the same.
173
template <typename T>
174
2242
static v8::MaybeLocal<v8::Object> New(Environment* env,
175
                                      MaybeStackBuffer<T>* buf) {
176
  v8::MaybeLocal<v8::Object> ret;
177
2242
  char* src = reinterpret_cast<char*>(buf->out());
178
2242
  const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
179
180
2242
  if (buf->IsAllocated())
181
352
    ret = New(env, src, len_in_bytes);
182
1890
  else if (!buf->IsInvalidated())
183
1890
    ret = Copy(env, src, len_in_bytes);
184
185
2242
  if (ret.IsEmpty())
186
    return ret;
187
188
2242
  if (buf->IsAllocated())
189
352
    buf->Release();
190
191
2242
  return ret;
192
}
193
}  // namespace Buffer
194
195
v8::MaybeLocal<v8::Value> InternalMakeCallback(
196
    Environment* env,
197
    v8::Local<v8::Object> resource,
198
    v8::Local<v8::Object> recv,
199
    const v8::Local<v8::Function> callback,
200
    int argc,
201
    v8::Local<v8::Value> argv[],
202
    async_context asyncContext);
203
204
v8::MaybeLocal<v8::Value> MakeSyncCallback(v8::Isolate* isolate,
205
                                           v8::Local<v8::Object> recv,
206
                                           v8::Local<v8::Function> callback,
207
                                           int argc,
208
                                           v8::Local<v8::Value> argv[]);
209
210
class InternalCallbackScope {
211
 public:
212
  enum Flags {
213
    kNoFlags = 0,
214
    // Indicates whether 'before' and 'after' hooks should be skipped.
215
    kSkipAsyncHooks = 1,
216
    // Indicates whether nextTick and microtask queues should be skipped.
217
    // This should only be used when there is no call into JS in this scope.
218
    // (The HTTP parser also uses it for some weird backwards
219
    // compatibility issues, but it shouldn't.)
220
    kSkipTaskQueues = 2
221
  };
222
  InternalCallbackScope(Environment* env,
223
                        v8::Local<v8::Object> object,
224
                        const async_context& asyncContext,
225
                        int flags = kNoFlags);
226
  // Utility that can be used by AsyncWrap classes.
227
  explicit InternalCallbackScope(AsyncWrap* async_wrap, int flags = 0);
228
  ~InternalCallbackScope();
229
  void Close();
230
231
853975
  inline bool Failed() const { return failed_; }
232
1291
  inline void MarkAsFailed() { failed_ = true; }
233
234
 private:
235
  Environment* env_;
236
  async_context async_context_;
237
  v8::Local<v8::Object> object_;
238
  bool skip_hooks_;
239
  bool skip_task_queues_;
240
  bool failed_ = false;
241
  bool pushed_ids_ = false;
242
  bool closed_ = false;
243
};
244
245
class DebugSealHandleScope {
246
 public:
247
668170
  explicit inline DebugSealHandleScope(v8::Isolate* isolate = nullptr)
248
#ifdef DEBUG
249
    : actual_scope_(isolate != nullptr ? isolate : v8::Isolate::GetCurrent())
250
#endif
251
668170
  {}
252
253
 private:
254
#ifdef DEBUG
255
  v8::SealHandleScope actual_scope_;
256
#endif
257
};
258
259
class ThreadPoolWork {
260
 public:
261
11417
  explicit inline ThreadPoolWork(Environment* env) : env_(env) {
262
11417
    CHECK_NOT_NULL(env);
263
11417
  }
264
22584
  inline virtual ~ThreadPoolWork() = default;
265
266
  inline void ScheduleWork();
267
  inline int CancelWork();
268
269
  virtual void DoThreadPoolWork() = 0;
270
  virtual void AfterThreadPoolWork(int status) = 0;
271
272
  Environment* env() const { return env_; }
273
274
 private:
275
  Environment* env_;
276
  uv_work_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  // defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
293
294
namespace credentials {
295
bool SafeGetenv(const char* key,
296
                std::string* text,
297
                std::shared_ptr<KVStore> env_vars = nullptr,
298
                v8::Isolate* isolate = nullptr);
299
}  // namespace credentials
300
301
void DefineZlibConstants(v8::Local<v8::Object> target);
302
v8::Isolate* NewIsolate(v8::Isolate::CreateParams* params,
303
                        uv_loop_t* event_loop,
304
                        MultiIsolatePlatform* platform);
305
// This overload automatically picks the right 'main_script_id' if no callback
306
// was provided by the embedder.
307
v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
308
                                         StartExecutionCallback cb = nullptr);
309
v8::MaybeLocal<v8::Object> GetPerContextExports(v8::Local<v8::Context> context);
310
v8::MaybeLocal<v8::Value> ExecuteBootstrapper(
311
    Environment* env,
312
    const char* id,
313
    std::vector<v8::Local<v8::Value>>* arguments);
314
void MarkBootstrapComplete(const v8::FunctionCallbackInfo<v8::Value>& args);
315
316
class InitializationResultImpl final : public InitializationResult {
317
 public:
318
  ~InitializationResultImpl();
319
10978
  int exit_code() const { return exit_code_; }
320
5493
  bool early_return() const { return early_return_; }
321
5499
  const std::vector<std::string>& args() const { return args_; }
322
5431
  const std::vector<std::string>& exec_args() const { return exec_args_; }
323
5487
  const std::vector<std::string>& errors() const { return errors_; }
324
  MultiIsolatePlatform* platform() const { return platform_; }
325
326
  int exit_code_ = 0;
327
  std::vector<std::string> args_;
328
  std::vector<std::string> exec_args_;
329
  std::vector<std::string> errors_;
330
  bool early_return_ = false;
331
  MultiIsolatePlatform* platform_ = nullptr;
332
};
333
334
void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
335
void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
336
void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
337
338
#if HAVE_INSPECTOR
339
namespace profiler {
340
void StartProfilers(Environment* env);
341
}
342
#endif  // HAVE_INSPECTOR
343
344
#ifdef __POSIX__
345
static constexpr unsigned kMaxSignal = 32;
346
#endif
347
348
bool HasSignalJSHandler(int signum);
349
350
#ifdef _WIN32
351
typedef SYSTEMTIME TIME_TYPE;
352
#else  // UNIX, OSX
353
typedef struct tm TIME_TYPE;
354
#endif
355
356
double GetCurrentTimeInMicroseconds();
357
int WriteFileSync(const char* path, uv_buf_t buf);
358
int WriteFileSync(v8::Isolate* isolate,
359
                  const char* path,
360
                  v8::Local<v8::String> string);
361
362
class DiagnosticFilename {
363
 public:
364
  static void LocalTime(TIME_TYPE* tm_struct);
365
366
  inline DiagnosticFilename(Environment* env,
367
                            const char* prefix,
368
                            const char* ext);
369
370
  inline DiagnosticFilename(uint64_t thread_id,
371
                            const char* prefix,
372
                            const char* ext);
373
374
  inline const char* operator*() const;
375
376
 private:
377
  static std::string MakeFilename(
378
      uint64_t thread_id,
379
      const char* prefix,
380
      const char* ext);
381
382
  std::string filename_;
383
};
384
385
namespace heap {
386
v8::Maybe<void> WriteSnapshot(Environment* env, const char* filename);
387
}
388
389
namespace heap {
390
391
void DeleteHeapSnapshot(const v8::HeapSnapshot* snapshot);
392
using HeapSnapshotPointer =
393
  DeleteFnPtr<const v8::HeapSnapshot, DeleteHeapSnapshot>;
394
395
BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
396
    Environment* env, HeapSnapshotPointer&& snapshot);
397
}  // namespace heap
398
399
namespace fs {
400
std::string Basename(const std::string& str, const std::string& extension);
401
}  // namespace fs
402
403
node_module napi_module_to_node_module(const napi_module* mod);
404
405
std::ostream& operator<<(std::ostream& output,
406
                         const std::vector<SnapshotIndex>& v);
407
std::ostream& operator<<(std::ostream& output,
408
                         const std::vector<std::string>& vec);
409
std::ostream& operator<<(std::ostream& output,
410
                         const std::vector<PropInfo>& vec);
411
std::ostream& operator<<(std::ostream& output, const PropInfo& d);
412
std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& d);
413
std::ostream& operator<<(std::ostream& output,
414
                         const ImmediateInfo::SerializeInfo& d);
415
std::ostream& operator<<(std::ostream& output,
416
                         const TickInfo::SerializeInfo& d);
417
std::ostream& operator<<(std::ostream& output,
418
                         const AsyncHooks::SerializeInfo& d);
419
std::ostream& operator<<(std::ostream& output, const SnapshotMetadata& d);
420
421
namespace performance {
422
std::ostream& operator<<(std::ostream& output,
423
                         const PerformanceState::SerializeInfo& d);
424
}
425
426
bool linux_at_secure();
427
}  // namespace node
428
429
#endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
430
431
#endif  // SRC_NODE_INTERNALS_H_