• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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*)>
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 std::string FormatCaughtException(v8::Isolate* isolate,
87                                   v8::Local<v8::Context> context,
88                                   const v8::TryCatch& try_catch);
89 
90 void ResetStdio();  // Safe to call more than once and from signal handlers.
91 #ifdef __POSIX__
92 void SignalExit(int signal, siginfo_t* info, void* ucontext);
93 #endif
94 
95 std::string GetProcessTitle(const char* default_title);
96 std::string GetHumanReadableProcessName();
97 
98 v8::Maybe<bool> InitializeBaseContextForSnapshot(
99     v8::Local<v8::Context> context);
100 v8::Maybe<bool> InitializeContextRuntime(v8::Local<v8::Context> context);
101 v8::Maybe<bool> InitializePrimordials(v8::Local<v8::Context> context);
102 
103 class NodeArrayBufferAllocator : public ArrayBufferAllocator {
104  public:
zero_fill_field()105   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;
RegisterPointer(void * data,size_t size)111   virtual void RegisterPointer(void* data, size_t size) {
112     total_mem_usage_.fetch_add(size, std::memory_order_relaxed);
113   }
UnregisterPointer(void * data,size_t size)114   virtual void UnregisterPointer(void* data, size_t size) {
115     total_mem_usage_.fetch_sub(size, std::memory_order_relaxed);
116   }
117 
GetImpl()118   NodeArrayBufferAllocator* GetImpl() final { return this; }
total_mem_usage()119   inline uint64_t total_mem_usage() const {
120     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   // Delegate to V8's allocator for compatibility with the V8 memory cage.
128   std::unique_ptr<v8::ArrayBuffer::Allocator> allocator_{
129       v8::ArrayBuffer::Allocator::NewDefaultAllocator()};
130 };
131 
132 class DebuggingArrayBufferAllocator final : public NodeArrayBufferAllocator {
133  public:
134   ~DebuggingArrayBufferAllocator() override;
135   void* Allocate(size_t size) override;
136   void* AllocateUninitialized(size_t size) override;
137   void Free(void* data, size_t size) override;
138   void* Reallocate(void* data, size_t old_size, size_t size) override;
139   void RegisterPointer(void* data, size_t size) override;
140   void UnregisterPointer(void* data, size_t size) override;
141 
142  private:
143   void RegisterPointerInternal(void* data, size_t size);
144   void UnregisterPointerInternal(void* data, size_t size);
145   Mutex mutex_;
146   std::unordered_map<void*, size_t> allocations_;
147 };
148 
149 namespace Buffer {
150 v8::MaybeLocal<v8::Object> Copy(Environment* env, const char* data, size_t len);
151 v8::MaybeLocal<v8::Object> New(Environment* env, size_t size);
152 // Takes ownership of |data|.
153 v8::MaybeLocal<v8::Object> New(Environment* env,
154                                char* data,
155                                size_t length,
156                                void (*callback)(char* data, void* hint),
157                                void* hint);
158 // Takes ownership of |data|.  Must allocate |data| with the current Isolate's
159 // ArrayBuffer::Allocator().
160 v8::MaybeLocal<v8::Object> New(Environment* env,
161                                char* data,
162                                size_t length);
163 // Creates a Buffer instance over an existing ArrayBuffer.
164 v8::MaybeLocal<v8::Uint8Array> New(Environment* env,
165                                    v8::Local<v8::ArrayBuffer> ab,
166                                    size_t byte_offset,
167                                    size_t length);
168 // Construct a Buffer from a MaybeStackBuffer (and also its subclasses like
169 // Utf8Value and TwoByteValue).
170 // If |buf| is invalidated, an empty MaybeLocal is returned, and nothing is
171 // changed.
172 // If |buf| contains actual data, this method takes ownership of |buf|'s
173 // underlying buffer. However, |buf| itself can be reused even after this call,
174 // but its capacity, if increased through AllocateSufficientStorage, is not
175 // guaranteed to stay the same.
176 template <typename T>
New(Environment * env,MaybeStackBuffer<T> * buf)177 static v8::MaybeLocal<v8::Object> New(Environment* env,
178                                       MaybeStackBuffer<T>* buf) {
179   v8::MaybeLocal<v8::Object> ret;
180   char* src = reinterpret_cast<char*>(buf->out());
181   const size_t len_in_bytes = buf->length() * sizeof(buf->out()[0]);
182 
183   if (buf->IsAllocated())
184     ret = New(env, src, len_in_bytes);
185   else if (!buf->IsInvalidated())
186     ret = Copy(env, src, len_in_bytes);
187 
188   if (ret.IsEmpty())
189     return ret;
190 
191   if (buf->IsAllocated())
192     buf->Release();
193 
194   return ret;
195 }
196 }  // namespace Buffer
197 
198 v8::MaybeLocal<v8::Value> InternalMakeCallback(
199     Environment* env,
200     v8::Local<v8::Object> resource,
201     v8::Local<v8::Object> recv,
202     const v8::Local<v8::Function> callback,
203     int argc,
204     v8::Local<v8::Value> argv[],
205     async_context asyncContext);
206 
207 v8::MaybeLocal<v8::Value> MakeSyncCallback(v8::Isolate* isolate,
208                                            v8::Local<v8::Object> recv,
209                                            v8::Local<v8::Function> callback,
210                                            int argc,
211                                            v8::Local<v8::Value> argv[]);
212 
213 class InternalCallbackScope {
214  public:
215   enum Flags {
216     kNoFlags = 0,
217     // Indicates whether 'before' and 'after' hooks should be skipped.
218     kSkipAsyncHooks = 1,
219     // Indicates whether nextTick and microtask queues should be skipped.
220     // This should only be used when there is no call into JS in this scope.
221     // (The HTTP parser also uses it for some weird backwards
222     // compatibility issues, but it shouldn't.)
223     kSkipTaskQueues = 2
224   };
225   InternalCallbackScope(Environment* env,
226                         v8::Local<v8::Object> object,
227                         const async_context& asyncContext,
228                         int flags = kNoFlags);
229   // Utility that can be used by AsyncWrap classes.
230   explicit InternalCallbackScope(AsyncWrap* async_wrap, int flags = 0);
231   ~InternalCallbackScope();
232   void Close();
233 
Failed()234   inline bool Failed() const { return failed_; }
MarkAsFailed()235   inline void MarkAsFailed() { failed_ = true; }
236 
237  private:
238   Environment* env_;
239   async_context async_context_;
240   v8::Local<v8::Object> object_;
241   bool skip_hooks_;
242   bool skip_task_queues_;
243   bool failed_ = false;
244   bool pushed_ids_ = false;
245   bool closed_ = false;
246 };
247 
248 class DebugSealHandleScope {
249  public:
250   explicit inline DebugSealHandleScope(v8::Isolate* isolate = nullptr)
251 #ifdef DEBUG
252     : actual_scope_(isolate != nullptr ? isolate : v8::Isolate::GetCurrent())
253 #endif
254   {}
255 
256  private:
257 #ifdef DEBUG
258   v8::SealHandleScope actual_scope_;
259 #endif
260 };
261 
262 class ThreadPoolWork {
263  public:
ThreadPoolWork(Environment * env,const char * type)264   explicit inline ThreadPoolWork(Environment* env, const char* type)
265       : env_(env), type_(type) {
266     CHECK_NOT_NULL(env);
267   }
268   inline virtual ~ThreadPoolWork() = default;
269 
270   inline void ScheduleWork();
271   inline int CancelWork();
272 
273   virtual void DoThreadPoolWork() = 0;
274   virtual void AfterThreadPoolWork(int status) = 0;
275 
env()276   Environment* env() const { return env_; }
277 
278  private:
279   Environment* env_;
280   uv_work_t work_req_;
281   const char* type_;
282 };
283 
284 #define TRACING_CATEGORY_NODE "node"
285 #define TRACING_CATEGORY_NODE1(one)                                           \
286     TRACING_CATEGORY_NODE ","                                                 \
287     TRACING_CATEGORY_NODE "." #one
288 #define TRACING_CATEGORY_NODE2(one, two)                                      \
289     TRACING_CATEGORY_NODE ","                                                 \
290     TRACING_CATEGORY_NODE "." #one ","                                        \
291     TRACING_CATEGORY_NODE "." #one "." #two
292 
293 // Functions defined in node.cc that are exposed via the bootstrapper object
294 
295 #if defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
296 #define NODE_IMPLEMENTS_POSIX_CREDENTIALS 1
297 #endif  // defined(__POSIX__) && !defined(__ANDROID__) && !defined(__CloudABI__)
298 
299 namespace credentials {
300 bool SafeGetenv(const char* key,
301                 std::string* text,
302                 std::shared_ptr<KVStore> env_vars = nullptr,
303                 v8::Isolate* isolate = nullptr);
304 }  // namespace credentials
305 
306 void DefineZlibConstants(v8::Local<v8::Object> target);
307 v8::Isolate* NewIsolate(v8::Isolate::CreateParams* params,
308                         uv_loop_t* event_loop,
309                         MultiIsolatePlatform* platform,
310                         bool has_snapshot_data = false);
311 // This overload automatically picks the right 'main_script_id' if no callback
312 // was provided by the embedder.
313 v8::MaybeLocal<v8::Value> StartExecution(Environment* env,
314                                          StartExecutionCallback cb = nullptr);
315 v8::MaybeLocal<v8::Object> GetPerContextExports(v8::Local<v8::Context> context);
316 void MarkBootstrapComplete(const v8::FunctionCallbackInfo<v8::Value>& args);
317 
318 class InitializationResultImpl final : public InitializationResult {
319  public:
320   ~InitializationResultImpl();
exit_code()321   int exit_code() const { return exit_code_; }
early_return()322   bool early_return() const { return early_return_; }
args()323   const std::vector<std::string>& args() const { return args_; }
exec_args()324   const std::vector<std::string>& exec_args() const { return exec_args_; }
errors()325   const std::vector<std::string>& errors() const { return errors_; }
platform()326   MultiIsolatePlatform* platform() const { return platform_; }
327 
328   int exit_code_ = 0;
329   std::vector<std::string> args_;
330   std::vector<std::string> exec_args_;
331   std::vector<std::string> errors_;
332   bool early_return_ = false;
333   MultiIsolatePlatform* platform_ = nullptr;
334 };
335 
336 void SetIsolateErrorHandlers(v8::Isolate* isolate, const IsolateSettings& s);
337 void SetIsolateMiscHandlers(v8::Isolate* isolate, const IsolateSettings& s);
338 void SetIsolateCreateParamsForNode(v8::Isolate::CreateParams* params);
339 
340 #if HAVE_INSPECTOR
341 namespace profiler {
342 void StartProfilers(Environment* env);
343 }
344 #endif  // HAVE_INSPECTOR
345 
346 #ifdef __POSIX__
347 static constexpr unsigned kMaxSignal = 32;
348 #endif
349 
350 bool HasSignalJSHandler(int signum);
351 
352 #ifdef _WIN32
353 typedef SYSTEMTIME TIME_TYPE;
354 #else  // UNIX, OSX
355 typedef struct tm TIME_TYPE;
356 #endif
357 
358 double GetCurrentTimeInMicroseconds();
359 int WriteFileSync(const char* path, uv_buf_t buf);
360 int WriteFileSync(v8::Isolate* isolate,
361                   const char* path,
362                   v8::Local<v8::String> string);
363 
364 class DiagnosticFilename {
365  public:
366   static void LocalTime(TIME_TYPE* tm_struct);
367 
368   inline DiagnosticFilename(Environment* env,
369                             const char* prefix,
370                             const char* ext);
371 
372   inline DiagnosticFilename(uint64_t thread_id,
373                             const char* prefix,
374                             const char* ext);
375 
376   inline const char* operator*() const;
377 
378  private:
379   static std::string MakeFilename(
380       uint64_t thread_id,
381       const char* prefix,
382       const char* ext);
383 
384   std::string filename_;
385 };
386 
387 namespace heap {
388 v8::Maybe<void> WriteSnapshot(Environment* env, const char* filename);
389 }
390 
391 namespace heap {
392 
393 void DeleteHeapSnapshot(const v8::HeapSnapshot* snapshot);
394 using HeapSnapshotPointer =
395   DeleteFnPtr<const v8::HeapSnapshot, DeleteHeapSnapshot>;
396 
397 BaseObjectPtr<AsyncWrap> CreateHeapSnapshotStream(
398     Environment* env, HeapSnapshotPointer&& snapshot);
399 }  // namespace heap
400 
401 namespace fs {
402 std::string Basename(const std::string& str, const std::string& extension);
403 }  // namespace fs
404 
405 node_module napi_module_to_node_module(const napi_module* mod);
406 
407 std::ostream& operator<<(std::ostream& output,
408                          const std::vector<SnapshotIndex>& v);
409 std::ostream& operator<<(std::ostream& output,
410                          const std::vector<std::string>& vec);
411 std::ostream& operator<<(std::ostream& output,
412                          const std::vector<PropInfo>& vec);
413 std::ostream& operator<<(std::ostream& output, const PropInfo& d);
414 std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& d);
415 std::ostream& operator<<(std::ostream& output,
416                          const ImmediateInfo::SerializeInfo& d);
417 std::ostream& operator<<(std::ostream& output,
418                          const TickInfo::SerializeInfo& d);
419 std::ostream& operator<<(std::ostream& output,
420                          const AsyncHooks::SerializeInfo& d);
421 std::ostream& operator<<(std::ostream& output, const SnapshotMetadata& d);
422 
423 namespace performance {
424 std::ostream& operator<<(std::ostream& output,
425                          const PerformanceState::SerializeInfo& d);
426 }
427 
428 bool linux_at_secure();
429 }  // namespace node
430 
431 #endif  // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
432 
433 #endif  // SRC_NODE_INTERNALS_H_
434