GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "base_object-inl.h" |
||
2 |
#include "debug_utils-inl.h" |
||
3 |
#include "env-inl.h" |
||
4 |
#include "memory_tracker-inl.h" |
||
5 |
#include "node.h" |
||
6 |
#include "node_errors.h" |
||
7 |
#include "node_external_reference.h" |
||
8 |
#include "node_internals.h" |
||
9 |
#include "node_process.h" |
||
10 |
#include "util-inl.h" |
||
11 |
#include "uv.h" |
||
12 |
#include "v8-fast-api-calls.h" |
||
13 |
#include "v8.h" |
||
14 |
|||
15 |
#include <vector> |
||
16 |
|||
17 |
#if HAVE_INSPECTOR |
||
18 |
#include "inspector_io.h" |
||
19 |
#endif |
||
20 |
|||
21 |
#include <climits> // PATH_MAX |
||
22 |
#include <cstdio> |
||
23 |
|||
24 |
#if defined(_MSC_VER) |
||
25 |
#include <direct.h> |
||
26 |
#include <io.h> |
||
27 |
#define umask _umask |
||
28 |
typedef int mode_t; |
||
29 |
#else |
||
30 |
#include <pthread.h> |
||
31 |
#include <sys/resource.h> // getrlimit, setrlimit |
||
32 |
#include <termios.h> // tcgetattr, tcsetattr |
||
33 |
#endif |
||
34 |
|||
35 |
namespace node { |
||
36 |
|||
37 |
using v8::Array; |
||
38 |
using v8::ArrayBuffer; |
||
39 |
using v8::BackingStore; |
||
40 |
using v8::Context; |
||
41 |
using v8::Float64Array; |
||
42 |
using v8::FunctionCallbackInfo; |
||
43 |
using v8::HeapStatistics; |
||
44 |
using v8::Integer; |
||
45 |
using v8::Isolate; |
||
46 |
using v8::Local; |
||
47 |
using v8::NewStringType; |
||
48 |
using v8::Number; |
||
49 |
using v8::Object; |
||
50 |
using v8::String; |
||
51 |
using v8::Uint32; |
||
52 |
using v8::Value; |
||
53 |
|||
54 |
namespace per_process { |
||
55 |
4678 |
Mutex umask_mutex; |
|
56 |
} // namespace per_process |
||
57 |
|||
58 |
// Microseconds in a second, as a float, used in CPUUsage() below |
||
59 |
#define MICROS_PER_SEC 1e6 |
||
60 |
// used in Hrtime() and Uptime() below |
||
61 |
#define NANOS_PER_SEC 1000000000 |
||
62 |
|||
63 |
static void Abort(const FunctionCallbackInfo<Value>& args) { |
||
64 |
Abort(); |
||
65 |
} |
||
66 |
|||
67 |
// For internal testing only, not exposed to userland. |
||
68 |
static void CauseSegfault(const FunctionCallbackInfo<Value>& args) { |
||
69 |
// This should crash hard all platforms. |
||
70 |
volatile void** d = static_cast<volatile void**>(nullptr); |
||
71 |
*d = nullptr; |
||
72 |
} |
||
73 |
|||
74 |
427 |
static void Chdir(const FunctionCallbackInfo<Value>& args) { |
|
75 |
427 |
Environment* env = Environment::GetCurrent(args); |
|
76 |
✗✓ | 427 |
CHECK(env->owns_process_state()); |
77 |
|||
78 |
✗✓ | 427 |
CHECK_EQ(args.Length(), 1); |
79 |
✗✓ | 1281 |
CHECK(args[0]->IsString()); |
80 |
853 |
Utf8Value path(env->isolate(), args[0]); |
|
81 |
427 |
int err = uv_chdir(*path); |
|
82 |
✓✓ | 427 |
if (err) { |
83 |
// Also include the original working directory, since that will usually |
||
84 |
// be helpful information when debugging a `chdir()` failure. |
||
85 |
char buf[PATH_MAX_BYTES]; |
||
86 |
1 |
size_t cwd_len = sizeof(buf); |
|
87 |
1 |
uv_cwd(buf, &cwd_len); |
|
88 |
✓✓ | 1 |
return env->ThrowUVException(err, "chdir", nullptr, buf, *path); |
89 |
} |
||
90 |
} |
||
91 |
|||
92 |
561 |
inline Local<ArrayBuffer> get_fields_array_buffer( |
|
93 |
const FunctionCallbackInfo<Value>& args, |
||
94 |
size_t index, |
||
95 |
size_t array_length) { |
||
96 |
✗✓ | 1683 |
CHECK(args[index]->IsFloat64Array()); |
97 |
1683 |
Local<Float64Array> arr = args[index].As<Float64Array>(); |
|
98 |
✗✓ | 561 |
CHECK_EQ(arr->Length(), array_length); |
99 |
561 |
return arr->Buffer(); |
|
100 |
} |
||
101 |
|||
102 |
// CPUUsage use libuv's uv_getrusage() this-process resource usage accessor, |
||
103 |
// to access ru_utime (user CPU time used) and ru_stime (system CPU time used), |
||
104 |
// which are uv_timeval_t structs (long tv_sec, long tv_usec). |
||
105 |
// Returns those values as Float64 microseconds in the elements of the array |
||
106 |
// passed to the function. |
||
107 |
33 |
static void CPUUsage(const FunctionCallbackInfo<Value>& args) { |
|
108 |
33 |
Environment* env = Environment::GetCurrent(args); |
|
109 |
uv_rusage_t rusage; |
||
110 |
|||
111 |
// Call libuv to get the values we'll return. |
||
112 |
33 |
int err = uv_getrusage(&rusage); |
|
113 |
✗✓ | 33 |
if (err) |
114 |
return env->ThrowUVException(err, "uv_getrusage"); |
||
115 |
|||
116 |
// Get the double array pointer from the Float64Array argument. |
||
117 |
33 |
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 2); |
|
118 |
33 |
double* fields = static_cast<double*>(ab->GetBackingStore()->Data()); |
|
119 |
|||
120 |
// Set the Float64Array elements to be user / system values in microseconds. |
||
121 |
33 |
fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec; |
|
122 |
33 |
fields[1] = MICROS_PER_SEC * rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec; |
|
123 |
} |
||
124 |
|||
125 |
5544 |
static void Cwd(const FunctionCallbackInfo<Value>& args) { |
|
126 |
5544 |
Environment* env = Environment::GetCurrent(args); |
|
127 |
✗✓ | 5544 |
CHECK(env->has_run_bootstrapping_code()); |
128 |
char buf[PATH_MAX_BYTES]; |
||
129 |
5544 |
size_t cwd_len = sizeof(buf); |
|
130 |
5544 |
int err = uv_cwd(buf, &cwd_len); |
|
131 |
✓✓ | 5544 |
if (err) |
132 |
13 |
return env->ThrowUVException(err, "uv_cwd"); |
|
133 |
|||
134 |
11062 |
Local<String> cwd = String::NewFromUtf8(env->isolate(), |
|
135 |
buf, |
||
136 |
NewStringType::kNormal, |
||
137 |
11062 |
cwd_len).ToLocalChecked(); |
|
138 |
11062 |
args.GetReturnValue().Set(cwd); |
|
139 |
} |
||
140 |
|||
141 |
62 |
static void Kill(const FunctionCallbackInfo<Value>& args) { |
|
142 |
62 |
Environment* env = Environment::GetCurrent(args); |
|
143 |
62 |
Local<Context> context = env->context(); |
|
144 |
|||
145 |
✗✓ | 62 |
if (args.Length() < 2) { |
146 |
THROW_ERR_MISSING_ARGS(env, "Bad argument."); |
||
147 |
} |
||
148 |
|||
149 |
int pid; |
||
150 |
✗✓ | 186 |
if (!args[0]->Int32Value(context).To(&pid)) return; |
151 |
int sig; |
||
152 |
✗✓ | 186 |
if (!args[1]->Int32Value(context).To(&sig)) return; |
153 |
|||
154 |
62 |
uv_pid_t own_pid = uv_os_getpid(); |
|
155 |
✓✓✓✓ |
184 |
if (sig > 0 && |
156 |
✓✓✓✗ ✓✓✗✓ ✓✓ |
148 |
(pid == 0 || pid == -1 || pid == own_pid || pid == -own_pid) && |
157 |
27 |
!HasSignalJSHandler(sig)) { |
|
158 |
// This is most likely going to terminate this process. |
||
159 |
// It's not an exact method but it might be close enough. |
||
160 |
18 |
RunAtExit(env); |
|
161 |
} |
||
162 |
|||
163 |
62 |
int err = uv_kill(pid, sig); |
|
164 |
124 |
args.GetReturnValue().Set(err); |
|
165 |
} |
||
166 |
|||
167 |
527 |
static void MemoryUsage(const FunctionCallbackInfo<Value>& args) { |
|
168 |
527 |
Environment* env = Environment::GetCurrent(args); |
|
169 |
|||
170 |
size_t rss; |
||
171 |
527 |
int err = uv_resident_set_memory(&rss); |
|
172 |
✗✓ | 527 |
if (err) |
173 |
return env->ThrowUVException(err, "uv_resident_set_memory"); |
||
174 |
|||
175 |
527 |
Isolate* isolate = env->isolate(); |
|
176 |
// V8 memory usage |
||
177 |
527 |
HeapStatistics v8_heap_stats; |
|
178 |
527 |
isolate->GetHeapStatistics(&v8_heap_stats); |
|
179 |
|||
180 |
NodeArrayBufferAllocator* array_buffer_allocator = |
||
181 |
527 |
env->isolate_data()->node_allocator(); |
|
182 |
|||
183 |
// Get the double array pointer from the Float64Array argument. |
||
184 |
527 |
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 5); |
|
185 |
527 |
double* fields = static_cast<double*>(ab->GetBackingStore()->Data()); |
|
186 |
|||
187 |
527 |
fields[0] = rss; |
|
188 |
527 |
fields[1] = v8_heap_stats.total_heap_size(); |
|
189 |
527 |
fields[2] = v8_heap_stats.used_heap_size(); |
|
190 |
527 |
fields[3] = v8_heap_stats.external_memory(); |
|
191 |
1054 |
fields[4] = array_buffer_allocator == nullptr ? |
|
192 |
✓✗ | 527 |
0 : array_buffer_allocator->total_mem_usage(); |
193 |
} |
||
194 |
|||
195 |
12 |
void RawDebug(const FunctionCallbackInfo<Value>& args) { |
|
196 |
✓✗✗✓ ✗✓✗✓ |
48 |
CHECK(args.Length() == 1 && args[0]->IsString() && |
197 |
"must be called with a single string"); |
||
198 |
24 |
Utf8Value message(args.GetIsolate(), args[0]); |
|
199 |
12 |
FPrintF(stderr, "%s\n", message); |
|
200 |
12 |
fflush(stderr); |
|
201 |
12 |
} |
|
202 |
|||
203 |
4052 |
static void Umask(const FunctionCallbackInfo<Value>& args) { |
|
204 |
4052 |
Environment* env = Environment::GetCurrent(args); |
|
205 |
✗✓ | 4052 |
CHECK(env->has_run_bootstrapping_code()); |
206 |
✗✓ | 4052 |
CHECK_EQ(args.Length(), 1); |
207 |
✓✓✗✓ ✗✓ |
19920 |
CHECK(args[0]->IsUndefined() || args[0]->IsUint32()); |
208 |
8104 |
Mutex::ScopedLock scoped_lock(per_process::umask_mutex); |
|
209 |
|||
210 |
uint32_t old; |
||
211 |
✓✓ | 12156 |
if (args[0]->IsUndefined()) { |
212 |
170 |
old = umask(0); |
|
213 |
170 |
umask(static_cast<mode_t>(old)); |
|
214 |
} else { |
||
215 |
11646 |
int oct = args[0].As<Uint32>()->Value(); |
|
216 |
3882 |
old = umask(static_cast<mode_t>(oct)); |
|
217 |
} |
||
218 |
|||
219 |
8104 |
args.GetReturnValue().Set(old); |
|
220 |
4052 |
} |
|
221 |
|||
222 |
4 |
static void Uptime(const FunctionCallbackInfo<Value>& args) { |
|
223 |
4 |
Environment* env = Environment::GetCurrent(args); |
|
224 |
|||
225 |
4 |
uv_update_time(env->event_loop()); |
|
226 |
double uptime = |
||
227 |
4 |
static_cast<double>(uv_hrtime() - per_process::node_start_time); |
|
228 |
4 |
Local<Number> result = Number::New(env->isolate(), uptime / NANOS_PER_SEC); |
|
229 |
8 |
args.GetReturnValue().Set(result); |
|
230 |
4 |
} |
|
231 |
|||
232 |
1 |
static void GetActiveRequests(const FunctionCallbackInfo<Value>& args) { |
|
233 |
1 |
Environment* env = Environment::GetCurrent(args); |
|
234 |
|||
235 |
2 |
std::vector<Local<Value>> request_v; |
|
236 |
✓✓ | 13 |
for (ReqWrapBase* req_wrap : *env->req_wrap_queue()) { |
237 |
12 |
AsyncWrap* w = req_wrap->GetAsyncWrap(); |
|
238 |
✗✓ | 24 |
if (w->persistent().IsEmpty()) |
239 |
continue; |
||
240 |
12 |
request_v.emplace_back(w->GetOwner()); |
|
241 |
} |
||
242 |
|||
243 |
3 |
args.GetReturnValue().Set( |
|
244 |
Array::New(env->isolate(), request_v.data(), request_v.size())); |
||
245 |
1 |
} |
|
246 |
|||
247 |
// Non-static, friend of HandleWrap. Could have been a HandleWrap method but |
||
248 |
// implemented here for consistency with GetActiveRequests(). |
||
249 |
4 |
void GetActiveHandles(const FunctionCallbackInfo<Value>& args) { |
|
250 |
4 |
Environment* env = Environment::GetCurrent(args); |
|
251 |
|||
252 |
8 |
std::vector<Local<Value>> handle_v; |
|
253 |
✓✓ | 24 |
for (auto w : *env->handle_wrap_queue()) { |
254 |
✓✓ | 20 |
if (!HandleWrap::HasRef(w)) |
255 |
1 |
continue; |
|
256 |
19 |
handle_v.emplace_back(w->GetOwner()); |
|
257 |
} |
||
258 |
12 |
args.GetReturnValue().Set( |
|
259 |
Array::New(env->isolate(), handle_v.data(), handle_v.size())); |
||
260 |
4 |
} |
|
261 |
|||
262 |
1 |
static void ResourceUsage(const FunctionCallbackInfo<Value>& args) { |
|
263 |
1 |
Environment* env = Environment::GetCurrent(args); |
|
264 |
|||
265 |
uv_rusage_t rusage; |
||
266 |
1 |
int err = uv_getrusage(&rusage); |
|
267 |
✗✓ | 1 |
if (err) |
268 |
return env->ThrowUVException(err, "uv_getrusage"); |
||
269 |
|||
270 |
1 |
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 16); |
|
271 |
1 |
double* fields = static_cast<double*>(ab->GetBackingStore()->Data()); |
|
272 |
|||
273 |
1 |
fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec; |
|
274 |
1 |
fields[1] = MICROS_PER_SEC * rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec; |
|
275 |
1 |
fields[2] = rusage.ru_maxrss; |
|
276 |
1 |
fields[3] = rusage.ru_ixrss; |
|
277 |
1 |
fields[4] = rusage.ru_idrss; |
|
278 |
1 |
fields[5] = rusage.ru_isrss; |
|
279 |
1 |
fields[6] = rusage.ru_minflt; |
|
280 |
1 |
fields[7] = rusage.ru_majflt; |
|
281 |
1 |
fields[8] = rusage.ru_nswap; |
|
282 |
1 |
fields[9] = rusage.ru_inblock; |
|
283 |
1 |
fields[10] = rusage.ru_oublock; |
|
284 |
1 |
fields[11] = rusage.ru_msgsnd; |
|
285 |
1 |
fields[12] = rusage.ru_msgrcv; |
|
286 |
1 |
fields[13] = rusage.ru_nsignals; |
|
287 |
1 |
fields[14] = rusage.ru_nvcsw; |
|
288 |
1 |
fields[15] = rusage.ru_nivcsw; |
|
289 |
} |
||
290 |
|||
291 |
#ifdef __POSIX__ |
||
292 |
3 |
static void DebugProcess(const FunctionCallbackInfo<Value>& args) { |
|
293 |
3 |
Environment* env = Environment::GetCurrent(args); |
|
294 |
|||
295 |
✗✓ | 3 |
if (args.Length() < 1) { |
296 |
return THROW_ERR_MISSING_ARGS(env, "Invalid number of arguments."); |
||
297 |
} |
||
298 |
|||
299 |
✗✓ | 6 |
CHECK(args[0]->IsNumber()); |
300 |
9 |
pid_t pid = args[0].As<Integer>()->Value(); |
|
301 |
3 |
int r = kill(pid, SIGUSR1); |
|
302 |
|||
303 |
✓✓ | 3 |
if (r != 0) { |
304 |
1 |
return env->ThrowErrnoException(errno, "kill"); |
|
305 |
} |
||
306 |
} |
||
307 |
#endif // __POSIX__ |
||
308 |
|||
309 |
#ifdef _WIN32 |
||
310 |
static int GetDebugSignalHandlerMappingName(DWORD pid, |
||
311 |
wchar_t* buf, |
||
312 |
size_t buf_len) { |
||
313 |
return _snwprintf(buf, buf_len, L"node-debug-handler-%u", pid); |
||
314 |
} |
||
315 |
|||
316 |
static void DebugProcess(const FunctionCallbackInfo<Value>& args) { |
||
317 |
Environment* env = Environment::GetCurrent(args); |
||
318 |
Isolate* isolate = args.GetIsolate(); |
||
319 |
|||
320 |
if (args.Length() < 1) { |
||
321 |
return THROW_ERR_MISSING_ARGS(env, "Invalid number of arguments."); |
||
322 |
} |
||
323 |
|||
324 |
HANDLE process = nullptr; |
||
325 |
HANDLE thread = nullptr; |
||
326 |
HANDLE mapping = nullptr; |
||
327 |
wchar_t mapping_name[32]; |
||
328 |
LPTHREAD_START_ROUTINE* handler = nullptr; |
||
329 |
DWORD pid = 0; |
||
330 |
|||
331 |
auto cleanup = OnScopeLeave([&]() { |
||
332 |
if (process != nullptr) CloseHandle(process); |
||
333 |
if (thread != nullptr) CloseHandle(thread); |
||
334 |
if (handler != nullptr) UnmapViewOfFile(handler); |
||
335 |
if (mapping != nullptr) CloseHandle(mapping); |
||
336 |
}); |
||
337 |
|||
338 |
CHECK(args[0]->IsNumber()); |
||
339 |
pid = args[0].As<Integer>()->Value(); |
||
340 |
|||
341 |
process = |
||
342 |
OpenProcess(PROCESS_CREATE_THREAD | PROCESS_QUERY_INFORMATION | |
||
343 |
PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_VM_READ, |
||
344 |
FALSE, |
||
345 |
pid); |
||
346 |
if (process == nullptr) { |
||
347 |
isolate->ThrowException( |
||
348 |
WinapiErrnoException(isolate, GetLastError(), "OpenProcess")); |
||
349 |
return; |
||
350 |
} |
||
351 |
|||
352 |
if (GetDebugSignalHandlerMappingName( |
||
353 |
pid, mapping_name, arraysize(mapping_name)) < 0) { |
||
354 |
env->ThrowErrnoException(errno, "sprintf"); |
||
355 |
return; |
||
356 |
} |
||
357 |
|||
358 |
mapping = OpenFileMappingW(FILE_MAP_READ, FALSE, mapping_name); |
||
359 |
if (mapping == nullptr) { |
||
360 |
isolate->ThrowException( |
||
361 |
WinapiErrnoException(isolate, GetLastError(), "OpenFileMappingW")); |
||
362 |
return; |
||
363 |
} |
||
364 |
|||
365 |
handler = reinterpret_cast<LPTHREAD_START_ROUTINE*>( |
||
366 |
MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, sizeof *handler)); |
||
367 |
if (handler == nullptr || *handler == nullptr) { |
||
368 |
isolate->ThrowException( |
||
369 |
WinapiErrnoException(isolate, GetLastError(), "MapViewOfFile")); |
||
370 |
return; |
||
371 |
} |
||
372 |
|||
373 |
thread = |
||
374 |
CreateRemoteThread(process, nullptr, 0, *handler, nullptr, 0, nullptr); |
||
375 |
if (thread == nullptr) { |
||
376 |
isolate->ThrowException( |
||
377 |
WinapiErrnoException(isolate, GetLastError(), "CreateRemoteThread")); |
||
378 |
return; |
||
379 |
} |
||
380 |
|||
381 |
// Wait for the thread to terminate |
||
382 |
if (WaitForSingleObject(thread, INFINITE) != WAIT_OBJECT_0) { |
||
383 |
isolate->ThrowException( |
||
384 |
WinapiErrnoException(isolate, GetLastError(), "WaitForSingleObject")); |
||
385 |
return; |
||
386 |
} |
||
387 |
} |
||
388 |
#endif // _WIN32 |
||
389 |
|||
390 |
5 |
static void DebugEnd(const FunctionCallbackInfo<Value>& args) { |
|
391 |
#if HAVE_INSPECTOR |
||
392 |
5 |
Environment* env = Environment::GetCurrent(args); |
|
393 |
✓✓ | 5 |
if (env->inspector_agent()->IsListening()) { |
394 |
4 |
env->inspector_agent()->Stop(); |
|
395 |
} |
||
396 |
#endif |
||
397 |
5 |
} |
|
398 |
|||
399 |
314 |
static void ReallyExit(const FunctionCallbackInfo<Value>& args) { |
|
400 |
314 |
Environment* env = Environment::GetCurrent(args); |
|
401 |
314 |
RunAtExit(env); |
|
402 |
1256 |
int code = args[0]->Int32Value(env->context()).FromMaybe(0); |
|
403 |
314 |
env->Exit(code); |
|
404 |
54 |
} |
|
405 |
|||
406 |
13601 |
class FastHrtime : public BaseObject { |
|
407 |
public: |
||
408 |
5005 |
static Local<Object> New(Environment* env) { |
|
409 |
Local<v8::FunctionTemplate> ctor = |
||
410 |
5005 |
v8::FunctionTemplate::New(env->isolate()); |
|
411 |
10010 |
ctor->Inherit(BaseObject::GetConstructorTemplate(env)); |
|
412 |
5005 |
Local<v8::ObjectTemplate> otmpl = ctor->InstanceTemplate(); |
|
413 |
5005 |
otmpl->SetInternalFieldCount(FastHrtime::kInternalFieldCount); |
|
414 |
|||
415 |
20019 |
auto create_func = [env](auto fast_func, auto slow_func) { |
|
416 |
10009 |
auto cfunc = v8::CFunction::Make(fast_func); |
|
417 |
20020 |
return v8::FunctionTemplate::New(env->isolate(), |
|
418 |
slow_func, |
||
419 |
Local<Value>(), |
||
420 |
Local<v8::Signature>(), |
||
421 |
0, |
||
422 |
v8::ConstructorBehavior::kThrow, |
||
423 |
v8::SideEffectType::kHasNoSideEffect, |
||
424 |
20020 |
&cfunc); |
|
425 |
5005 |
}; |
|
426 |
|||
427 |
20019 |
otmpl->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "hrtime"), |
|
428 |
5004 |
create_func(FastNumber, SlowNumber)); |
|
429 |
20020 |
otmpl->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "hrtimeBigInt"), |
|
430 |
5005 |
create_func(FastBigInt, SlowBigInt)); |
|
431 |
|||
432 |
15015 |
Local<Object> obj = otmpl->NewInstance(env->context()).ToLocalChecked(); |
|
433 |
|||
434 |
Local<ArrayBuffer> ab = |
||
435 |
ArrayBuffer::New(env->isolate(), |
||
436 |
5005 |
std::max(sizeof(uint64_t), sizeof(uint32_t) * 3)); |
|
437 |
5005 |
new FastHrtime(env, obj, ab); |
|
438 |
10010 |
obj->Set( |
|
439 |
20020 |
env->context(), FIXED_ONE_BYTE_STRING(env->isolate(), "buffer"), ab) |
|
440 |
.ToChecked(); |
||
441 |
|||
442 |
5005 |
return obj; |
|
443 |
} |
||
444 |
|||
445 |
private: |
||
446 |
5004 |
FastHrtime(Environment* env, |
|
447 |
Local<Object> object, |
||
448 |
Local<ArrayBuffer> ab) |
||
449 |
5004 |
: BaseObject(env, object), |
|
450 |
array_buffer_(env->isolate(), ab), |
||
451 |
10009 |
backing_store_(ab->GetBackingStore()) { |
|
452 |
5005 |
MakeWeak(); |
|
453 |
5005 |
} |
|
454 |
|||
455 |
22 |
void MemoryInfo(MemoryTracker* tracker) const override { |
|
456 |
22 |
tracker->TrackField("array_buffer", array_buffer_); |
|
457 |
22 |
} |
|
458 |
22 |
SET_MEMORY_INFO_NAME(FastHrtime) |
|
459 |
22 |
SET_SELF_SIZE(FastHrtime) |
|
460 |
|||
461 |
static FastHrtime* FromV8ApiObject(v8::ApiObject api_object) { |
||
462 |
v8::Object* v8_object = reinterpret_cast<v8::Object*>(&api_object); |
||
463 |
return static_cast<FastHrtime*>( |
||
464 |
v8_object->GetAlignedPointerFromInternalField(BaseObject::kSlot)); |
||
465 |
} |
||
466 |
|||
467 |
// This is the legacy version of hrtime before BigInt was introduced in |
||
468 |
// JavaScript. |
||
469 |
// The value returned by uv_hrtime() is a 64-bit int representing nanoseconds, |
||
470 |
// so this function instead fills in an Uint32Array with 3 entries, |
||
471 |
// to avoid any integer overflow possibility. |
||
472 |
// The first two entries contain the second part of the value |
||
473 |
// broken into the upper/lower 32 bits to be converted back in JS, |
||
474 |
// because there is no Uint64Array in JS. |
||
475 |
// The third entry contains the remaining nanosecond part of the value. |
||
476 |
257334 |
static void NumberImpl(FastHrtime* receiver) { |
|
477 |
257334 |
uint64_t t = uv_hrtime(); |
|
478 |
257334 |
uint32_t* fields = static_cast<uint32_t*>(receiver->backing_store_->Data()); |
|
479 |
257333 |
fields[0] = (t / NANOS_PER_SEC) >> 32; |
|
480 |
257333 |
fields[1] = (t / NANOS_PER_SEC) & 0xffffffff; |
|
481 |
257333 |
fields[2] = t % NANOS_PER_SEC; |
|
482 |
257333 |
} |
|
483 |
|||
484 |
static void FastNumber(v8::ApiObject receiver) { |
||
485 |
NumberImpl(FromV8ApiObject(receiver)); |
||
486 |
} |
||
487 |
|||
488 |
257333 |
static void SlowNumber(const FunctionCallbackInfo<Value>& args) { |
|
489 |
257333 |
NumberImpl(FromJSObject<FastHrtime>(args.Holder())); |
|
490 |
257332 |
} |
|
491 |
|||
492 |
392695 |
static void BigIntImpl(FastHrtime* receiver) { |
|
493 |
392695 |
uint64_t t = uv_hrtime(); |
|
494 |
392695 |
uint64_t* fields = static_cast<uint64_t*>(receiver->backing_store_->Data()); |
|
495 |
392695 |
fields[0] = t; |
|
496 |
392695 |
} |
|
497 |
|||
498 |
static void FastBigInt(v8::ApiObject receiver) { |
||
499 |
BigIntImpl(FromV8ApiObject(receiver)); |
||
500 |
} |
||
501 |
|||
502 |
392695 |
static void SlowBigInt(const FunctionCallbackInfo<Value>& args) { |
|
503 |
392695 |
BigIntImpl(FromJSObject<FastHrtime>(args.Holder())); |
|
504 |
392695 |
} |
|
505 |
|||
506 |
v8::Global<ArrayBuffer> array_buffer_; |
||
507 |
std::shared_ptr<BackingStore> backing_store_; |
||
508 |
}; |
||
509 |
|||
510 |
5004 |
static void GetFastAPIs(const FunctionCallbackInfo<Value>& args) { |
|
511 |
5004 |
Environment* env = Environment::GetCurrent(args); |
|
512 |
5005 |
Local<Object> ret = Object::New(env->isolate()); |
|
513 |
10010 |
ret->Set(env->context(), |
|
514 |
FIXED_ONE_BYTE_STRING(env->isolate(), "hrtime"), |
||
515 |
25025 |
FastHrtime::New(env)) |
|
516 |
.ToChecked(); |
||
517 |
10010 |
args.GetReturnValue().Set(ret); |
|
518 |
5005 |
} |
|
519 |
|||
520 |
445 |
static void InitializeProcessMethods(Local<Object> target, |
|
521 |
Local<Value> unused, |
||
522 |
Local<Context> context, |
||
523 |
void* priv) { |
||
524 |
445 |
Environment* env = Environment::GetCurrent(context); |
|
525 |
|||
526 |
// define various internal methods |
||
527 |
✓✓ | 445 |
if (env->owns_process_state()) { |
528 |
49 |
env->SetMethod(target, "_debugProcess", DebugProcess); |
|
529 |
49 |
env->SetMethod(target, "_debugEnd", DebugEnd); |
|
530 |
49 |
env->SetMethod(target, "abort", Abort); |
|
531 |
49 |
env->SetMethod(target, "causeSegfault", CauseSegfault); |
|
532 |
49 |
env->SetMethod(target, "chdir", Chdir); |
|
533 |
} |
||
534 |
|||
535 |
445 |
env->SetMethod(target, "umask", Umask); |
|
536 |
445 |
env->SetMethod(target, "_rawDebug", RawDebug); |
|
537 |
445 |
env->SetMethod(target, "memoryUsage", MemoryUsage); |
|
538 |
445 |
env->SetMethod(target, "cpuUsage", CPUUsage); |
|
539 |
445 |
env->SetMethod(target, "resourceUsage", ResourceUsage); |
|
540 |
|||
541 |
445 |
env->SetMethod(target, "_getActiveRequests", GetActiveRequests); |
|
542 |
445 |
env->SetMethod(target, "_getActiveHandles", GetActiveHandles); |
|
543 |
445 |
env->SetMethod(target, "_kill", Kill); |
|
544 |
|||
545 |
445 |
env->SetMethodNoSideEffect(target, "cwd", Cwd); |
|
546 |
445 |
env->SetMethod(target, "dlopen", binding::DLOpen); |
|
547 |
445 |
env->SetMethod(target, "reallyExit", ReallyExit); |
|
548 |
445 |
env->SetMethodNoSideEffect(target, "uptime", Uptime); |
|
549 |
445 |
env->SetMethod(target, "patchProcessObject", PatchProcessObject); |
|
550 |
445 |
env->SetMethod(target, "getFastAPIs", GetFastAPIs); |
|
551 |
445 |
} |
|
552 |
|||
553 |
4601 |
void RegisterProcessMethodsExternalReferences( |
|
554 |
ExternalReferenceRegistry* registry) { |
||
555 |
4601 |
registry->Register(DebugProcess); |
|
556 |
4601 |
registry->Register(DebugEnd); |
|
557 |
4601 |
registry->Register(Abort); |
|
558 |
4601 |
registry->Register(CauseSegfault); |
|
559 |
4601 |
registry->Register(Chdir); |
|
560 |
|||
561 |
4601 |
registry->Register(Umask); |
|
562 |
4601 |
registry->Register(RawDebug); |
|
563 |
4601 |
registry->Register(MemoryUsage); |
|
564 |
4601 |
registry->Register(CPUUsage); |
|
565 |
4601 |
registry->Register(ResourceUsage); |
|
566 |
|||
567 |
4601 |
registry->Register(GetActiveRequests); |
|
568 |
4601 |
registry->Register(GetActiveHandles); |
|
569 |
4601 |
registry->Register(Kill); |
|
570 |
|||
571 |
4601 |
registry->Register(Cwd); |
|
572 |
4601 |
registry->Register(binding::DLOpen); |
|
573 |
4601 |
registry->Register(ReallyExit); |
|
574 |
4601 |
registry->Register(Uptime); |
|
575 |
4601 |
registry->Register(PatchProcessObject); |
|
576 |
4601 |
registry->Register(GetFastAPIs); |
|
577 |
4601 |
} |
|
578 |
|||
579 |
} // namespace node |
||
580 |
|||
581 |
4670 |
NODE_MODULE_CONTEXT_AWARE_INTERNAL(process_methods, |
|
582 |
node::InitializeProcessMethods) |
||
583 |
✓✗✓✗ |
18635 |
NODE_MODULE_EXTERNAL_REFERENCE(process_methods, |
584 |
node::RegisterProcessMethodsExternalReferences) |
Generated by: GCOVR (Version 3.4) |