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 native_module {
47 class NativeModuleLoader;
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::Local<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*)>
GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value> & args)67 void GetSockOrPeerName(const v8::FunctionCallbackInfo<v8::Value>& args) {
68 T* wrap;
69 ASSIGN_OR_RETURN_UNWRAP(&wrap,
70 args.Holder(),
71 args.GetReturnValue().Set(UV_EBADF));
72 CHECK(args[0]->IsObject());
73 sockaddr_storage storage;
74 int addrlen = sizeof(storage);
75 sockaddr* const addr = reinterpret_cast<sockaddr*>(&storage);
76 const int err = F(&wrap->handle_, addr, &addrlen);
77 if (err == 0)
78 AddressToJS(wrap->env(), addr, args[0].As<v8::Object>());
79 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 void InitializeContextRuntime(v8::Local<v8::Context>);
96 bool InitializePrimordials(v8::Local<v8::Context> context);
97
98 class NodeArrayBufferAllocator : public ArrayBufferAllocator {
99 public:
zero_fill_field()100 inline uint32_t* zero_fill_field() { return &zero_fill_field_; }
101
102 void* Allocate(size_t size) override; // Defined in src/node.cc
103 void* AllocateUninitialized(size_t size) override;
104 void Free(void* data, size_t size) override;
105 void* Reallocate(void* data, size_t old_size, size_t size) override;
RegisterPointer(void * data,size_t size)106 virtual void RegisterPointer(void* data, size_t size) {
107 total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
108 }
UnregisterPointer(void * data,size_t size)109 virtual void UnregisterPointer(void* data, size_t size) {
110 total_mem_usage_.fetch_sub(size, std::memory_order_relaxed);
111 }
112
GetImpl()113 NodeArrayBufferAllocator* GetImpl() final { return this; }
total_mem_usage()114 inline uint64_t total_mem_usage() const {
115 return total_mem_usage_.load(std::memory_order_relaxed);
116 }
117
118 private:
119 uint32_t zero_fill_field_ = 1; // Boolean but exposed as uint32 to JS land.
120 std::atomic<size_t> total_mem_usage_ {0};
121 };
122
123 class DebuggingArrayBufferAllocator final : public NodeArrayBufferAllocator {
124 public:
125 ~DebuggingArrayBufferAllocator() override;
126 void* Allocate(size_t size) override;
127 void* AllocateUninitialized(size_t size) override;
128 void Free(void* data, size_t size) override;
129 void* Reallocate(void* data, size_t old_size, size_t size) override;
130 void RegisterPointer(void* data, size_t size) override;
131 void UnregisterPointer(void* data, size_t size) override;
132
133 private:
134 void RegisterPointerInternal(void* data, size_t size);
135 void UnregisterPointerInternal(void* data, size_t size);
136 Mutex mutex_;
137 std::unordered_map<void*, size_t> allocations_;
138 };
139
140 namespace Buffer {
141 v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
142 v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
143 // Takes ownership of |data|.
144 v8::MaybeLocal<v8::Object> New(Environment* env,
145 char* data,
146 size_t length,
147 void (*callback)(char* data, void* hint),
148 void* hint);
149 // Takes ownership of |data|. Must allocate |data| with the current Isolate's
150 // ArrayBuffer::Allocator().
151 v8::MaybeLocal<v8::Object> New(Environment* env,
152 char* data,
153 size_t length);
154 // Creates a Buffer instance over an existing ArrayBuffer.
155 v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
156 v8::Local<v8::ArrayBuffer> ab,
157 size_t byte_offset,
158 size_t length);
159 // Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
160 // Utf8Value and TwoByteValue).
161 // If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
162 // changed.
163 // If |buf| contains actual data, this method takes ownership of |buf|'s
164 // underlying buffer. However, |buf| itself can be reused even after this call,
165 // but its capacity, if increased through AllocateSufficientStorage, is not
166 // guaranteed to stay the same.
167 template <typename T>
New(Environment * env,MaybeStackBuffer<T> * buf)168 static v8::MaybeLocal<v8::Object> New(Environment* env,
169 MaybeStackBuffer<T>* buf) {
170 v8::MaybeLocal<v8::Object> ret;
171 char* src = reinterpret_cast<char*>(buf->out());
172 const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
173
174 if (buf->IsAllocated())
175 ret = New(env, src, len_in_bytes);
176 else if (!buf->IsInvalidated())
177 ret = Copy(env, src, len_in_bytes);
178
179 if (ret.IsEmpty())
180 return ret;
181
182 if (buf->IsAllocated())
183 buf->Release();
184
185 return ret;
186 }
187 } // namespace Buffer
188
189 v8::MaybeLocal<v8::Value> InternalMakeCallback(
190 Environment* env,
191 v8::Local<v8::Object> resource,
192 v8::Local<v8::Object> recv,
193 const v8::Local<v8::Function> callback,
194 int argc,
195 v8::Local<v8::Value> argv[],
196 async_context asyncContext);
197
198 class InternalCallbackScope {
199 public:
200 enum Flags {
201 kNoFlags = 0,
202 // Indicates whether 'before' and 'after' hooks should be skipped.
203 kSkipAsyncHooks = 1,
204 // Indicates whether nextTick and microtask queues should be skipped.
205 // This should only be used when there is no call into JS in this scope.
206 // (The HTTP parser also uses it for some weird backwards
207 // compatibility issues, but it shouldn't.)
208 kSkipTaskQueues = 2
209 };
210 InternalCallbackScope(Environment* env,
211 v8::Local<v8::Object> object,
212 const async_context& asyncContext,
213 int flags = kNoFlags);
214 // Utility that can be used by AsyncWrap classes.
215 explicit InternalCallbackScope(AsyncWrap* async_wrap, int flags = 0);
216 ~InternalCallbackScope();
217 void Close();
218
Failed()219 inline bool Failed() const { return failed_; }
MarkAsFailed()220 inline void MarkAsFailed() { failed_ = true; }
221
222 private:
223 Environment* env_;
224 async_context async_context_;
225 v8::Local<v8::Object> object_;
226 bool skip_hooks_;
227 bool skip_task_queues_;
228 bool failed_ = false;
229 bool pushed_ids_ = false;
230 bool closed_ = false;
231 };
232
233 class DebugSealHandleScope {
234 public:
235 explicit inline DebugSealHandleScope(v8::Isolate* isolate = nullptr)
236 #ifdef DEBUG
237 : actual_scope_(isolate != nullptr ? isolate : v8::Isolate::GetCurrent())
238 #endif
239 {}
240
241 private:
242 #ifdef DEBUG
243 v8::SealHandleScope actual_scope_;
244 #endif
245 };
246
247 class ThreadPoolWork {
248 public:
ThreadPoolWork(Environment * env)249 explicit inline ThreadPoolWork(Environment* env) : env_(env) {
250 CHECK_NOT_NULL(env);
251 }
252 inline virtual ~ThreadPoolWork() = default;
253
254 inline void ScheduleWork();
255 inline int CancelWork();
256
257 virtual void DoThreadPoolWork() = 0;
258 virtual void AfterThreadPoolWork(int status) = 0;
259
env()260 Environment* env() const { return env_; }
261
262 private:
263 Environment* env_;
264 uv_work_t work_req_;
265 };
266
267 #define TRACING_CATEGORY_NODE "node"
268 #define TRACING_CATEGORY_NODE1(one) \
269 TRACING_CATEGORY_NODE "," \
270 TRACING_CATEGORY_NODE "." #one
271 #define TRACING_CATEGORY_NODE2(one, two) \
272 TRACING_CATEGORY_NODE "," \
273 TRACING_CATEGORY_NODE "." #one "," \
274 TRACING_CATEGORY_NODE "." #one "." #two
275
276 // Functions defined in node.cc that are exposed via the bootstrapper object
277
278 #if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
279 #define NODE_IMPLEMENTS_POSIX_CREDENTIALS 1
280 #endif // __POSIX__ && !defined(__ANDROID__) && !defined(__CloudABI__)
281
282 namespace credentials {
283 bool SafeGetenv(const char* key, std::string* text, Environment* env = nullptr);
284 } // namespace credentials
285
286 void DefineZlibConstants(v8::Local<v8::Object> target);
287 v8::Isolate* NewIsolate(v8::Isolate::CreateParams* params,
288 uv_loop_t* event_loop,
289 MultiIsolatePlatform* platform);
290 // This overload automatically picks the right 'main_script_id' if no callback
291 // was provided by the embedder.
292 v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
293 StartExecutionCallback cb = nullptr);
294 v8::MaybeLocal<v8::Object> GetPerContextExports(v8::Local<v8::Context> context);
295 v8::MaybeLocal<v8::Value> ExecuteBootstrapper(
296 Environment* env,
297 const char* id,
298 std::vector<v8::Local<v8::String>>* parameters,
299 std::vector<v8::Local<v8::Value>>* arguments);
300 void MarkBootstrapComplete(const v8::FunctionCallbackInfo<v8::Value>& args);
301
302 struct InitializationResult {
303 int exit_code = 0;
304 std::vector<std::string> args;
305 std::vector<std::string> exec_args;
306 bool early_return = false;
307 };
308 InitializationResult InitializeOncePerProcess(int argc, char** argv);
309 void TearDownOncePerProcess();
310 void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
311 void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
312 void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
313
314 #if HAVE_INSPECTOR
315 namespace profiler {
316 void StartProfilers(Environment* env);
317 }
318 #endif // HAVE_INSPECTOR
319
320 #ifdef __POSIX__
321 static constexpr unsigned kMaxSignal = 32;
322 #endif
323
324 bool HasSignalJSHandler(int signum);
325
326 #ifdef _WIN32
327 typedef SYSTEMTIME TIME_TYPE;
328 #else // UNIX, OSX
329 typedef struct tm TIME_TYPE;
330 #endif
331
332 double GetCurrentTimeInMicroseconds();
333 int WriteFileSync(const char* path, uv_buf_t buf);
334 int WriteFileSync(v8::Isolate* isolate,
335 const char* path,
336 v8::Local<v8::String> string);
337
338 class DiagnosticFilename {
339 public:
340 static void LocalTime(TIME_TYPE* tm_struct);
341
342 inline DiagnosticFilename(Environment* env,
343 const char* prefix,
344 const char* ext);
345
346 inline DiagnosticFilename(uint64_t thread_id,
347 const char* prefix,
348 const char* ext);
349
350 inline const char* operator*() const;
351
352 private:
353 static std::string MakeFilename(
354 uint64_t thread_id,
355 const char* prefix,
356 const char* ext);
357
358 std::string filename_;
359 };
360
361 namespace heap {
362 bool WriteSnapshot(v8::Isolate* isolate, const char* filename);
363 }
364
365 class TraceEventScope {
366 public:
TraceEventScope(const char * category,const char * name,void * id)367 TraceEventScope(const char* category,
368 const char* name,
369 void* id) : category_(category), name_(name), id_(id) {
370 TRACE_EVENT_NESTABLE_ASYNC_BEGIN0(category_, name_, id_);
371 }
~TraceEventScope()372 ~TraceEventScope() {
373 TRACE_EVENT_NESTABLE_ASYNC_END0(category_, name_, id_);
374 }
375
376 private:
377 const char* category_;
378 const char* name_;
379 void* id_;
380 };
381
382 namespace heap {
383
384 void DeleteHeapSnapshot(const v8::HeapSnapshot* snapshot);
385 using HeapSnapshotPointer =
386 DeleteFnPtr<const v8::HeapSnapshot, DeleteHeapSnapshot>;
387
388 BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
389 Environment* env, HeapSnapshotPointer&& snapshot);
390 } // namespace heap
391
392 namespace fs {
393 std::string Basename(const std::string& str, const std::string& extension);
394 } // namespace fs
395
396 node_module napi_module_to_node_module(const napi_module* mod);
397
398 } // namespace node
399
400 #endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
401
402 #endif // SRC_NODE_INTERNALS_H_
403