GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "env.h" |
||
2 |
#include "async_wrap.h" |
||
3 |
#include "base_object-inl.h" |
||
4 |
#include "debug_utils-inl.h" |
||
5 |
#include "diagnosticfilename-inl.h" |
||
6 |
#include "memory_tracker-inl.h" |
||
7 |
#include "node_buffer.h" |
||
8 |
#include "node_context_data.h" |
||
9 |
#include "node_errors.h" |
||
10 |
#include "node_internals.h" |
||
11 |
#include "node_options-inl.h" |
||
12 |
#include "node_process-inl.h" |
||
13 |
#include "node_v8_platform-inl.h" |
||
14 |
#include "node_worker.h" |
||
15 |
#include "req_wrap-inl.h" |
||
16 |
#include "stream_base.h" |
||
17 |
#include "tracing/agent.h" |
||
18 |
#include "tracing/traced_value.h" |
||
19 |
#include "util-inl.h" |
||
20 |
#include "v8-profiler.h" |
||
21 |
|||
22 |
#include <algorithm> |
||
23 |
#include <atomic> |
||
24 |
#include <cinttypes> |
||
25 |
#include <cstdio> |
||
26 |
#include <iostream> |
||
27 |
#include <limits> |
||
28 |
#include <memory> |
||
29 |
|||
30 |
namespace node { |
||
31 |
|||
32 |
using errors::TryCatchScope; |
||
33 |
using v8::Array; |
||
34 |
using v8::Boolean; |
||
35 |
using v8::Context; |
||
36 |
using v8::EmbedderGraph; |
||
37 |
using v8::Function; |
||
38 |
using v8::FunctionTemplate; |
||
39 |
using v8::HandleScope; |
||
40 |
using v8::HeapSpaceStatistics; |
||
41 |
using v8::Integer; |
||
42 |
using v8::Isolate; |
||
43 |
using v8::Local; |
||
44 |
using v8::MaybeLocal; |
||
45 |
using v8::NewStringType; |
||
46 |
using v8::Number; |
||
47 |
using v8::Object; |
||
48 |
using v8::Private; |
||
49 |
using v8::Script; |
||
50 |
using v8::SnapshotCreator; |
||
51 |
using v8::StackTrace; |
||
52 |
using v8::String; |
||
53 |
using v8::Symbol; |
||
54 |
using v8::TracingController; |
||
55 |
using v8::TryCatch; |
||
56 |
using v8::Undefined; |
||
57 |
using v8::Value; |
||
58 |
using worker::Worker; |
||
59 |
|||
60 |
int const Environment::kNodeContextTag = 0x6e6f64; |
||
61 |
void* const Environment::kNodeContextTagPtr = const_cast<void*>( |
||
62 |
static_cast<const void*>(&Environment::kNodeContextTag)); |
||
63 |
|||
64 |
6 |
std::vector<size_t> IsolateData::Serialize(SnapshotCreator* creator) { |
|
65 |
6 |
Isolate* isolate = creator->GetIsolate(); |
|
66 |
6 |
std::vector<size_t> indexes; |
|
67 |
12 |
HandleScope handle_scope(isolate); |
|
68 |
// XXX(joyeecheung): technically speaking, the indexes here should be |
||
69 |
// consecutive and we could just return a range instead of an array, |
||
70 |
// but that's not part of the V8 API contract so we use an array |
||
71 |
// just to be safe. |
||
72 |
|||
73 |
#define VP(PropertyName, StringValue) V(Private, PropertyName) |
||
74 |
#define VY(PropertyName, StringValue) V(Symbol, PropertyName) |
||
75 |
#define VS(PropertyName, StringValue) V(String, PropertyName) |
||
76 |
#define V(TypeName, PropertyName) \ |
||
77 |
indexes.push_back(creator->AddData(PropertyName##_.Get(isolate))); |
||
78 |
54 |
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP) |
|
79 |
78 |
PER_ISOLATE_SYMBOL_PROPERTIES(VY) |
|
80 |
1656 |
PER_ISOLATE_STRING_PROPERTIES(VS) |
|
81 |
#undef V |
||
82 |
#undef VY |
||
83 |
#undef VS |
||
84 |
#undef VP |
||
85 |
✓✓ | 354 |
for (size_t i = 0; i < AsyncWrap::PROVIDERS_LENGTH; i++) |
86 |
696 |
indexes.push_back(creator->AddData(async_wrap_provider(i))); |
|
87 |
|||
88 |
6 |
return indexes; |
|
89 |
} |
||
90 |
|||
91 |
5199 |
void IsolateData::DeserializeProperties(const std::vector<size_t>* indexes) { |
|
92 |
5199 |
size_t i = 0; |
|
93 |
10398 |
HandleScope handle_scope(isolate_); |
|
94 |
|||
95 |
#define VP(PropertyName, StringValue) V(Private, PropertyName) |
||
96 |
#define VY(PropertyName, StringValue) V(Symbol, PropertyName) |
||
97 |
#define VS(PropertyName, StringValue) V(String, PropertyName) |
||
98 |
#define V(TypeName, PropertyName) \ |
||
99 |
do { \ |
||
100 |
MaybeLocal<TypeName> maybe_field = \ |
||
101 |
isolate_->GetDataFromSnapshotOnce<TypeName>((*indexes)[i++]); \ |
||
102 |
Local<TypeName> field; \ |
||
103 |
if (!maybe_field.ToLocal(&field)) { \ |
||
104 |
fprintf(stderr, "Failed to deserialize " #PropertyName "\n"); \ |
||
105 |
} \ |
||
106 |
PropertyName##_.Set(isolate_, field); \ |
||
107 |
} while (0); |
||
108 |
✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ |
88383 |
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP) |
109 |
✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ |
129975 |
PER_ISOLATE_SYMBOL_PROPERTIES(VY) |
110 |
✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓ |
2864649 |
PER_ISOLATE_STRING_PROPERTIES(VS) |
111 |
#undef V |
||
112 |
#undef VY |
||
113 |
#undef VS |
||
114 |
#undef VP |
||
115 |
|||
116 |
✓✓ | 306741 |
for (size_t j = 0; j < AsyncWrap::PROVIDERS_LENGTH; j++) { |
117 |
MaybeLocal<String> maybe_field = |
||
118 |
603084 |
isolate_->GetDataFromSnapshotOnce<String>((*indexes)[i++]); |
|
119 |
Local<String> field; |
||
120 |
✗✓ | 301542 |
if (!maybe_field.ToLocal(&field)) { |
121 |
fprintf(stderr, "Failed to deserialize AsyncWrap provider %zu\n", j); |
||
122 |
} |
||
123 |
301542 |
async_wrap_providers_[j].Set(isolate_, field); |
|
124 |
} |
||
125 |
5199 |
} |
|
126 |
|||
127 |
862 |
void IsolateData::CreateProperties() { |
|
128 |
// Create string and private symbol properties as internalized one byte |
||
129 |
// strings after the platform is properly initialized. |
||
130 |
// |
||
131 |
// Internalized because it makes property lookups a little faster and |
||
132 |
// because the string is created in the old space straight away. It's going |
||
133 |
// to end up in the old space sooner or later anyway but now it doesn't go |
||
134 |
// through v8::Eternal's new space handling first. |
||
135 |
// |
||
136 |
// One byte because our strings are ASCII and we can safely skip V8's UTF-8 |
||
137 |
// decoding step. |
||
138 |
|||
139 |
862 |
HandleScope handle_scope(isolate_); |
|
140 |
|||
141 |
#define V(PropertyName, StringValue) \ |
||
142 |
PropertyName##_.Set( \ |
||
143 |
isolate_, \ |
||
144 |
Private::New(isolate_, \ |
||
145 |
String::NewFromOneByte( \ |
||
146 |
isolate_, \ |
||
147 |
reinterpret_cast<const uint8_t*>(StringValue), \ |
||
148 |
NewStringType::kInternalized, \ |
||
149 |
sizeof(StringValue) - 1) \ |
||
150 |
.ToLocalChecked())); |
||
151 |
7758 |
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(V) |
|
152 |
#undef V |
||
153 |
#define V(PropertyName, StringValue) \ |
||
154 |
PropertyName##_.Set( \ |
||
155 |
isolate_, \ |
||
156 |
Symbol::New(isolate_, \ |
||
157 |
String::NewFromOneByte( \ |
||
158 |
isolate_, \ |
||
159 |
reinterpret_cast<const uint8_t*>(StringValue), \ |
||
160 |
NewStringType::kInternalized, \ |
||
161 |
sizeof(StringValue) - 1) \ |
||
162 |
.ToLocalChecked())); |
||
163 |
11206 |
PER_ISOLATE_SYMBOL_PROPERTIES(V) |
|
164 |
#undef V |
||
165 |
#define V(PropertyName, StringValue) \ |
||
166 |
PropertyName##_.Set( \ |
||
167 |
isolate_, \ |
||
168 |
String::NewFromOneByte(isolate_, \ |
||
169 |
reinterpret_cast<const uint8_t*>(StringValue), \ |
||
170 |
NewStringType::kInternalized, \ |
||
171 |
sizeof(StringValue) - 1) \ |
||
172 |
.ToLocalChecked()); |
||
173 |
237912 |
PER_ISOLATE_STRING_PROPERTIES(V) |
|
174 |
#undef V |
||
175 |
|||
176 |
// Create all the provider strings that will be passed to JS. Place them in |
||
177 |
// an array so the array index matches the PROVIDER id offset. This way the |
||
178 |
// strings can be retrieved quickly. |
||
179 |
#define V(Provider) \ |
||
180 |
async_wrap_providers_[AsyncWrap::PROVIDER_ ## Provider].Set( \ |
||
181 |
isolate_, \ |
||
182 |
String::NewFromOneByte( \ |
||
183 |
isolate_, \ |
||
184 |
reinterpret_cast<const uint8_t*>(#Provider), \ |
||
185 |
NewStringType::kInternalized, \ |
||
186 |
sizeof(#Provider) - 1).ToLocalChecked()); |
||
187 |
50858 |
NODE_ASYNC_PROVIDER_TYPES(V) |
|
188 |
#undef V |
||
189 |
862 |
} |
|
190 |
|||
191 |
6061 |
IsolateData::IsolateData(Isolate* isolate, |
|
192 |
uv_loop_t* event_loop, |
||
193 |
MultiIsolatePlatform* platform, |
||
194 |
ArrayBufferAllocator* node_allocator, |
||
195 |
6061 |
const std::vector<size_t>* indexes) |
|
196 |
: isolate_(isolate), |
||
197 |
event_loop_(event_loop), |
||
198 |
47 |
node_allocator_(node_allocator == nullptr ? nullptr |
|
199 |
6014 |
: node_allocator->GetImpl()), |
|
200 |
✓✓ | 12122 |
platform_(platform) { |
201 |
6061 |
options_.reset( |
|
202 |
6061 |
new PerIsolateOptions(*(per_process::cli_options->per_isolate))); |
|
203 |
|||
204 |
✓✓ | 6061 |
if (indexes == nullptr) { |
205 |
862 |
CreateProperties(); |
|
206 |
} else { |
||
207 |
5199 |
DeserializeProperties(indexes); |
|
208 |
} |
||
209 |
6061 |
} |
|
210 |
|||
211 |
24 |
void IsolateData::MemoryInfo(MemoryTracker* tracker) const { |
|
212 |
#define V(PropertyName, StringValue) \ |
||
213 |
tracker->TrackField(#PropertyName, PropertyName()); |
||
214 |
24 |
PER_ISOLATE_SYMBOL_PROPERTIES(V) |
|
215 |
|||
216 |
24 |
PER_ISOLATE_STRING_PROPERTIES(V) |
|
217 |
#undef V |
||
218 |
|||
219 |
24 |
tracker->TrackField("async_wrap_providers", async_wrap_providers_); |
|
220 |
|||
221 |
✓✗ | 24 |
if (node_allocator_ != nullptr) { |
222 |
24 |
tracker->TrackFieldWithSize( |
|
223 |
"node_allocator", sizeof(*node_allocator_), "NodeArrayBufferAllocator"); |
||
224 |
} |
||
225 |
24 |
tracker->TrackFieldWithSize( |
|
226 |
"platform", sizeof(*platform_), "MultiIsolatePlatform"); |
||
227 |
// TODO(joyeecheung): implement MemoryRetainer in the option classes. |
||
228 |
24 |
} |
|
229 |
|||
230 |
121 |
void TrackingTraceStateObserver::UpdateTraceCategoryState() { |
|
231 |
✓✓✓✓ ✓✓ |
121 |
if (!env_->owns_process_state() || !env_->can_call_into_js()) { |
232 |
// Ideally, we’d have a consistent story that treats all threads/Environment |
||
233 |
// instances equally here. However, tracing is essentially global, and this |
||
234 |
// callback is called from whichever thread calls `StartTracing()` or |
||
235 |
// `StopTracing()`. The only way to do this in a threadsafe fashion |
||
236 |
// seems to be only tracking this from the main thread, and only allowing |
||
237 |
// these state modifications from the main thread. |
||
238 |
63 |
return; |
|
239 |
} |
||
240 |
|||
241 |
110 |
bool async_hooks_enabled = (*(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( |
|
242 |
110 |
TRACING_CATEGORY_NODE1(async_hooks)))) != 0; |
|
243 |
|||
244 |
110 |
Isolate* isolate = env_->isolate(); |
|
245 |
110 |
HandleScope handle_scope(isolate); |
|
246 |
110 |
Local<Function> cb = env_->trace_category_state_function(); |
|
247 |
✓✓ | 110 |
if (cb.IsEmpty()) |
248 |
52 |
return; |
|
249 |
58 |
TryCatchScope try_catch(env_); |
|
250 |
58 |
try_catch.SetVerbose(true); |
|
251 |
✓✓ | 116 |
Local<Value> args[] = {Boolean::New(isolate, async_hooks_enabled)}; |
252 |
116 |
USE(cb->Call(env_->context(), Undefined(isolate), arraysize(args), args)); |
|
253 |
} |
||
254 |
|||
255 |
853 |
void Environment::CreateProperties() { |
|
256 |
1706 |
HandleScope handle_scope(isolate_); |
|
257 |
853 |
Local<Context> ctx = context(); |
|
258 |
|||
259 |
{ |
||
260 |
853 |
Context::Scope context_scope(ctx); |
|
261 |
853 |
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate()); |
|
262 |
1706 |
templ->InstanceTemplate()->SetInternalFieldCount( |
|
263 |
BaseObject::kInternalFieldCount); |
||
264 |
853 |
templ->Inherit(BaseObject::GetConstructorTemplate(this)); |
|
265 |
|||
266 |
853 |
set_binding_data_ctor_template(templ); |
|
267 |
} |
||
268 |
|||
269 |
// Store primordials setup by the per-context script in the environment. |
||
270 |
Local<Object> per_context_bindings = |
||
271 |
1706 |
GetPerContextExports(ctx).ToLocalChecked(); |
|
272 |
Local<Value> primordials = |
||
273 |
2559 |
per_context_bindings->Get(ctx, primordials_string()).ToLocalChecked(); |
|
274 |
✗✓ | 853 |
CHECK(primordials->IsObject()); |
275 |
853 |
set_primordials(primordials.As<Object>()); |
|
276 |
|||
277 |
Local<String> prototype_string = |
||
278 |
853 |
FIXED_ONE_BYTE_STRING(isolate(), "prototype"); |
|
279 |
|||
280 |
#define V(EnvPropertyName, PrimordialsPropertyName) \ |
||
281 |
{ \ |
||
282 |
Local<Value> ctor = \ |
||
283 |
primordials.As<Object>() \ |
||
284 |
->Get(ctx, \ |
||
285 |
FIXED_ONE_BYTE_STRING(isolate(), PrimordialsPropertyName)) \ |
||
286 |
.ToLocalChecked(); \ |
||
287 |
CHECK(ctor->IsObject()); \ |
||
288 |
Local<Value> prototype = \ |
||
289 |
ctor.As<Object>()->Get(ctx, prototype_string).ToLocalChecked(); \ |
||
290 |
CHECK(prototype->IsObject()); \ |
||
291 |
set_##EnvPropertyName(prototype.As<Object>()); \ |
||
292 |
} |
||
293 |
|||
294 |
✗✓✗✓ |
5118 |
V(primordials_safe_map_prototype_object, "SafeMap"); |
295 |
✗✓✗✓ |
5118 |
V(primordials_safe_set_prototype_object, "SafeSet"); |
296 |
✗✓✗✓ |
5118 |
V(primordials_safe_weak_map_prototype_object, "SafeWeakMap"); |
297 |
✗✓✗✓ |
5118 |
V(primordials_safe_weak_set_prototype_object, "SafeWeakSet"); |
298 |
#undef V |
||
299 |
|||
300 |
Local<Object> process_object = |
||
301 |
853 |
node::CreateProcessObject(this).FromMaybe(Local<Object>()); |
|
302 |
853 |
set_process_object(process_object); |
|
303 |
853 |
} |
|
304 |
|||
305 |
6052 |
std::string GetExecPath(const std::vector<std::string>& argv) { |
|
306 |
char exec_path_buf[2 * PATH_MAX]; |
||
307 |
6052 |
size_t exec_path_len = sizeof(exec_path_buf); |
|
308 |
6052 |
std::string exec_path; |
|
309 |
✓✗ | 6052 |
if (uv_exepath(exec_path_buf, &exec_path_len) == 0) { |
310 |
6052 |
exec_path = std::string(exec_path_buf, exec_path_len); |
|
311 |
} else { |
||
312 |
exec_path = argv[0]; |
||
313 |
} |
||
314 |
|||
315 |
// On OpenBSD process.execPath will be relative unless we |
||
316 |
// get the full path before process.execPath is used. |
||
317 |
#if defined(__OpenBSD__) |
||
318 |
uv_fs_t req; |
||
319 |
req.ptr = nullptr; |
||
320 |
if (0 == |
||
321 |
uv_fs_realpath(nullptr, &req, exec_path.c_str(), nullptr)) { |
||
322 |
CHECK_NOT_NULL(req.ptr); |
||
323 |
exec_path = std::string(static_cast<char*>(req.ptr)); |
||
324 |
} |
||
325 |
uv_fs_req_cleanup(&req); |
||
326 |
#endif |
||
327 |
|||
328 |
6052 |
return exec_path; |
|
329 |
} |
||
330 |
|||
331 |
6052 |
Environment::Environment(IsolateData* isolate_data, |
|
332 |
Isolate* isolate, |
||
333 |
const std::vector<std::string>& args, |
||
334 |
const std::vector<std::string>& exec_args, |
||
335 |
const EnvSerializeInfo* env_info, |
||
336 |
EnvironmentFlags::Flags flags, |
||
337 |
6052 |
ThreadId thread_id) |
|
338 |
: isolate_(isolate), |
||
339 |
isolate_data_(isolate_data), |
||
340 |
async_hooks_(isolate, MAYBE_FIELD_PTR(env_info, async_hooks)), |
||
341 |
immediate_info_(isolate, MAYBE_FIELD_PTR(env_info, immediate_info)), |
||
342 |
tick_info_(isolate, MAYBE_FIELD_PTR(env_info, tick_info)), |
||
343 |
6052 |
timer_base_(uv_now(isolate_data->event_loop())), |
|
344 |
exec_argv_(exec_args), |
||
345 |
argv_(args), |
||
346 |
exec_path_(GetExecPath(args)), |
||
347 |
should_abort_on_uncaught_toggle_( |
||
348 |
6052 |
isolate_, |
|
349 |
1, |
||
350 |
MAYBE_FIELD_PTR(env_info, should_abort_on_uncaught_toggle)), |
||
351 |
6052 |
stream_base_state_(isolate_, |
|
352 |
StreamBase::kNumStreamBaseStateFields, |
||
353 |
MAYBE_FIELD_PTR(env_info, stream_base_state)), |
||
354 |
6052 |
environment_start_time_(PERFORMANCE_NOW()), |
|
355 |
flags_(flags), |
||
356 |
6052 |
thread_id_(thread_id.id == static_cast<uint64_t>(-1) |
|
357 |
6052 |
? AllocateEnvironmentThreadId().id |
|
358 |
✓✓✓✓ ✓✓✓✓ ✓✓✓✓ |
24208 |
: thread_id.id) { |
359 |
// We'll be creating new objects so make sure we've entered the context. |
||
360 |
12104 |
HandleScope handle_scope(isolate); |
|
361 |
|||
362 |
// Set some flags if only kDefaultFlags was passed. This can make API version |
||
363 |
// transitions easier for embedders. |
||
364 |
✓✓ | 6052 |
if (flags_ & EnvironmentFlags::kDefaultFlags) { |
365 |
10504 |
flags_ = flags_ | |
|
366 |
5252 |
EnvironmentFlags::kOwnsProcessState | |
|
367 |
EnvironmentFlags::kOwnsInspector; |
||
368 |
} |
||
369 |
|||
370 |
6052 |
set_env_vars(per_process::system_environment); |
|
371 |
// TODO(joyeecheung): pass Isolate* and env_vars to it instead of the entire |
||
372 |
// env, when the recursive dependency inclusion in "debug-utils.h" is |
||
373 |
// resolved. |
||
374 |
6052 |
enabled_debug_list_.Parse(this); |
|
375 |
|||
376 |
// We create new copies of the per-Environment option sets, so that it is |
||
377 |
// easier to modify them after Environment creation. The defaults are |
||
378 |
// part of the per-Isolate option set, for which in turn the defaults are |
||
379 |
// part of the per-process option set. |
||
380 |
12104 |
options_ = std::make_shared<EnvironmentOptions>( |
|
381 |
18156 |
*isolate_data->options()->per_env); |
|
382 |
6052 |
inspector_host_port_ = std::make_shared<ExclusiveAccess<HostPort>>( |
|
383 |
6052 |
options_->debug_options().host_port); |
|
384 |
|||
385 |
✓✓ | 6052 |
if (!(flags_ & EnvironmentFlags::kOwnsProcessState)) { |
386 |
800 |
set_abort_on_uncaught_exception(false); |
|
387 |
} |
||
388 |
|||
389 |
#if HAVE_INSPECTOR |
||
390 |
// We can only create the inspector agent after having cloned the options. |
||
391 |
6052 |
inspector_agent_ = std::make_unique<inspector::Agent>(this); |
|
392 |
#endif |
||
393 |
|||
394 |
✓✗ | 6052 |
if (tracing::AgentWriterHandle* writer = GetTracingAgentWriter()) { |
395 |
6052 |
trace_state_observer_ = std::make_unique<TrackingTraceStateObserver>(this); |
|
396 |
✓✓ | 6052 |
if (TracingController* tracing_controller = writer->GetTracingController()) |
397 |
6004 |
tracing_controller->AddTraceStateObserver(trace_state_observer_.get()); |
|
398 |
} |
||
399 |
|||
400 |
6052 |
destroy_async_id_list_.reserve(512); |
|
401 |
|||
402 |
6052 |
performance_state_ = std::make_unique<performance::PerformanceState>( |
|
403 |
✓✓ | 6052 |
isolate, MAYBE_FIELD_PTR(env_info, performance_state)); |
404 |
|||
405 |
6052 |
if (*TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED( |
|
406 |
✓✓ | 6052 |
TRACING_CATEGORY_NODE1(environment)) != 0) { |
407 |
16 |
auto traced_value = tracing::TracedValue::Create(); |
|
408 |
8 |
traced_value->BeginArray("args"); |
|
409 |
✓✓ | 18 |
for (const std::string& arg : args) traced_value->AppendString(arg); |
410 |
8 |
traced_value->EndArray(); |
|
411 |
8 |
traced_value->BeginArray("exec_args"); |
|
412 |
✓✓ | 33 |
for (const std::string& arg : exec_args) traced_value->AppendString(arg); |
413 |
8 |
traced_value->EndArray(); |
|
414 |
✓✓✓✗ |
15 |
TRACE_EVENT_NESTABLE_ASYNC_BEGIN1(TRACING_CATEGORY_NODE1(environment), |
415 |
"Environment", |
||
416 |
this, |
||
417 |
"args", |
||
418 |
std::move(traced_value)); |
||
419 |
} |
||
420 |
6052 |
} |
|
421 |
|||
422 |
853 |
Environment::Environment(IsolateData* isolate_data, |
|
423 |
Local<Context> context, |
||
424 |
const std::vector<std::string>& args, |
||
425 |
const std::vector<std::string>& exec_args, |
||
426 |
const EnvSerializeInfo* env_info, |
||
427 |
EnvironmentFlags::Flags flags, |
||
428 |
853 |
ThreadId thread_id) |
|
429 |
: Environment(isolate_data, |
||
430 |
context->GetIsolate(), |
||
431 |
args, |
||
432 |
exec_args, |
||
433 |
env_info, |
||
434 |
flags, |
||
435 |
853 |
thread_id) { |
|
436 |
853 |
InitializeMainContext(context, env_info); |
|
437 |
853 |
} |
|
438 |
|||
439 |
6052 |
void Environment::InitializeMainContext(Local<Context> context, |
|
440 |
const EnvSerializeInfo* env_info) { |
||
441 |
6052 |
context_.Reset(context->GetIsolate(), context); |
|
442 |
6052 |
AssignToContext(context, ContextInfo("")); |
|
443 |
✓✓ | 6052 |
if (env_info != nullptr) { |
444 |
5199 |
DeserializeProperties(env_info); |
|
445 |
} else { |
||
446 |
853 |
CreateProperties(); |
|
447 |
} |
||
448 |
|||
449 |
✓✓ | 6052 |
if (!options_->force_async_hooks_checks) { |
450 |
1 |
async_hooks_.no_force_checks(); |
|
451 |
} |
||
452 |
|||
453 |
// By default, always abort when --abort-on-uncaught-exception was passed. |
||
454 |
6052 |
should_abort_on_uncaught_toggle_[0] = 1; |
|
455 |
|||
456 |
6052 |
performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_ENVIRONMENT, |
|
457 |
environment_start_time_); |
||
458 |
6052 |
performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_NODE_START, |
|
459 |
per_process::node_start_time); |
||
460 |
|||
461 |
✓✓ | 6052 |
if (per_process::v8_initialized) { |
462 |
6004 |
performance_state_->Mark(performance::NODE_PERFORMANCE_MILESTONE_V8_START, |
|
463 |
performance::performance_v8_start); |
||
464 |
} |
||
465 |
6052 |
} |
|
466 |
|||
467 |
✓✓ | 1086624 |
Environment::~Environment() { |
468 |
if (Environment** interrupt_data = interrupt_data_.load()) { |
||
469 |
// There are pending RequestInterrupt() callbacks. Tell them not to run, |
||
470 |
// then force V8 to run interrupts by compiling and running an empty script |
||
471 |
// so as not to leak memory. |
||
472 |
10 |
*interrupt_data = nullptr; |
|
473 |
|||
474 |
20 |
Isolate::AllowJavascriptExecutionScope allow_js_here(isolate()); |
|
475 |
20 |
HandleScope handle_scope(isolate()); |
|
476 |
20 |
TryCatch try_catch(isolate()); |
|
477 |
20 |
Context::Scope context_scope(context()); |
|
478 |
|||
479 |
#ifdef DEBUG |
||
480 |
bool consistency_check = false; |
||
481 |
isolate()->RequestInterrupt([](Isolate*, void* data) { |
||
482 |
*static_cast<bool*>(data) = true; |
||
483 |
}, &consistency_check); |
||
484 |
#endif |
||
485 |
|||
486 |
Local<Script> script; |
||
487 |
✓✗ | 30 |
if (Script::Compile(context(), String::Empty(isolate())).ToLocal(&script)) |
488 |
10 |
USE(script->Run(context())); |
|
489 |
|||
490 |
DCHECK(consistency_check); |
||
491 |
} |
||
492 |
|||
493 |
// FreeEnvironment() should have set this. |
||
494 |
✗✓ | 5544 |
CHECK(is_stopping()); |
495 |
|||
496 |
✗✓ | 5544 |
if (options_->heap_snapshot_near_heap_limit > heap_limit_snapshot_taken_) { |
497 |
isolate_->RemoveNearHeapLimitCallback(Environment::NearHeapLimitCallback, |
||
498 |
0); |
||
499 |
} |
||
500 |
|||
501 |
5544 |
isolate()->GetHeapProfiler()->RemoveBuildEmbedderGraphCallback( |
|
502 |
BuildEmbedderGraph, this); |
||
503 |
|||
504 |
11088 |
HandleScope handle_scope(isolate()); |
|
505 |
|||
506 |
#if HAVE_INSPECTOR |
||
507 |
// Destroy inspector agent before erasing the context. The inspector |
||
508 |
// destructor depends on the context still being accessible. |
||
509 |
5544 |
inspector_agent_.reset(); |
|
510 |
#endif |
||
511 |
|||
512 |
11088 |
context()->SetAlignedPointerInEmbedderData(ContextEmbedderIndex::kEnvironment, |
|
513 |
nullptr); |
||
514 |
|||
515 |
✓✗ | 5544 |
if (trace_state_observer_) { |
516 |
5544 |
tracing::AgentWriterHandle* writer = GetTracingAgentWriter(); |
|
517 |
✗✓ | 5544 |
CHECK_NOT_NULL(writer); |
518 |
✓✓ | 5544 |
if (TracingController* tracing_controller = writer->GetTracingController()) |
519 |
5498 |
tracing_controller->RemoveTraceStateObserver(trace_state_observer_.get()); |
|
520 |
} |
||
521 |
|||
522 |
✓✓✓✓ |
10258 |
TRACE_EVENT_NESTABLE_ASYNC_END0( |
523 |
TRACING_CATEGORY_NODE1(environment), "Environment", this); |
||
524 |
|||
525 |
// Do not unload addons on the main thread. Some addons need to retain memory |
||
526 |
// beyond the Environment's lifetime, and unloading them early would break |
||
527 |
// them; with Worker threads, we have the opportunity to be stricter. |
||
528 |
// Also, since the main thread usually stops just before the process exits, |
||
529 |
// this is far less relevant here. |
||
530 |
✓✓ | 5544 |
if (!is_main_thread()) { |
531 |
// Dereference all addons that were loaded into this environment. |
||
532 |
✓✓ | 811 |
for (binding::DLib& addon : loaded_addons_) { |
533 |
14 |
addon.Close(); |
|
534 |
} |
||
535 |
} |
||
536 |
|||
537 |
✗✓ | 5544 |
CHECK_EQ(base_object_count_, 0); |
538 |
5544 |
} |
|
539 |
|||
540 |
6017 |
void Environment::InitializeLibuv() { |
|
541 |
12034 |
HandleScope handle_scope(isolate()); |
|
542 |
6017 |
Context::Scope context_scope(context()); |
|
543 |
|||
544 |
✗✓ | 6017 |
CHECK_EQ(0, uv_timer_init(event_loop(), timer_handle())); |
545 |
6017 |
uv_unref(reinterpret_cast<uv_handle_t*>(timer_handle())); |
|
546 |
|||
547 |
✗✓ | 6017 |
CHECK_EQ(0, uv_check_init(event_loop(), immediate_check_handle())); |
548 |
6017 |
uv_unref(reinterpret_cast<uv_handle_t*>(immediate_check_handle())); |
|
549 |
|||
550 |
✗✓ | 6017 |
CHECK_EQ(0, uv_idle_init(event_loop(), immediate_idle_handle())); |
551 |
|||
552 |
✗✓ | 6017 |
CHECK_EQ(0, uv_check_start(immediate_check_handle(), CheckImmediate)); |
553 |
|||
554 |
// Inform V8's CPU profiler when we're idle. The profiler is sampling-based |
||
555 |
// but not all samples are created equal; mark the wall clock time spent in |
||
556 |
// epoll_wait() and friends so profiling tools can filter it out. The samples |
||
557 |
// still end up in v8.log but with state=IDLE rather than state=EXTERNAL. |
||
558 |
✗✓ | 6017 |
CHECK_EQ(0, uv_prepare_init(event_loop(), &idle_prepare_handle_)); |
559 |
✗✓ | 6017 |
CHECK_EQ(0, uv_check_init(event_loop(), &idle_check_handle_)); |
560 |
|||
561 |
✗✓ | 24851 |
CHECK_EQ(0, uv_async_init( |
562 |
event_loop(), |
||
563 |
&task_queues_async_, |
||
564 |
[](uv_async_t* async) { |
||
565 |
Environment* env = ContainerOf( |
||
566 |
&Environment::task_queues_async_, async); |
||
567 |
HandleScope handle_scope(env->isolate()); |
||
568 |
Context::Scope context_scope(env->context()); |
||
569 |
env->RunAndClearNativeImmediates(); |
||
570 |
})); |
||
571 |
6017 |
uv_unref(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_)); |
|
572 |
6017 |
uv_unref(reinterpret_cast<uv_handle_t*>(&idle_check_handle_)); |
|
573 |
6017 |
uv_unref(reinterpret_cast<uv_handle_t*>(&task_queues_async_)); |
|
574 |
|||
575 |
{ |
||
576 |
12034 |
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_); |
|
577 |
6017 |
task_queues_async_initialized_ = true; |
|
578 |
✓✗✓✓ ✓✓ |
12034 |
if (native_immediates_threadsafe_.size() > 0 || |
579 |
6017 |
native_immediates_interrupts_.size() > 0) { |
|
580 |
5194 |
uv_async_send(&task_queues_async_); |
|
581 |
} |
||
582 |
} |
||
583 |
|||
584 |
// Register clean-up cb to be called to clean up the handles |
||
585 |
// when the environment is freed, note that they are not cleaned in |
||
586 |
// the one environment per process setup, but will be called in |
||
587 |
// FreeEnvironment. |
||
588 |
6017 |
RegisterHandleCleanups(); |
|
589 |
|||
590 |
6017 |
StartProfilerIdleNotifier(); |
|
591 |
6017 |
} |
|
592 |
|||
593 |
395 |
void Environment::ExitEnv() { |
|
594 |
395 |
set_can_call_into_js(false); |
|
595 |
395 |
set_stopping(true); |
|
596 |
395 |
isolate_->TerminateExecution(); |
|
597 |
790 |
SetImmediateThreadsafe([](Environment* env) { uv_stop(env->event_loop()); }); |
|
598 |
395 |
} |
|
599 |
|||
600 |
6017 |
void Environment::RegisterHandleCleanups() { |
|
601 |
6017 |
HandleCleanupCb close_and_finish = [](Environment* env, uv_handle_t* handle, |
|
602 |
33054 |
void* arg) { |
|
603 |
33054 |
handle->data = env; |
|
604 |
|||
605 |
33054 |
env->CloseHandle(handle, [](uv_handle_t* handle) { |
|
606 |
#ifdef DEBUG |
||
607 |
memset(handle, 0xab, uv_handle_size(handle->type)); |
||
608 |
#endif |
||
609 |
33054 |
}); |
|
610 |
33054 |
}; |
|
611 |
|||
612 |
36102 |
auto register_handle = [&](uv_handle_t* handle) { |
|
613 |
36102 |
RegisterHandleCleanup(handle, close_and_finish, nullptr); |
|
614 |
42119 |
}; |
|
615 |
6017 |
register_handle(reinterpret_cast<uv_handle_t*>(timer_handle())); |
|
616 |
6017 |
register_handle(reinterpret_cast<uv_handle_t*>(immediate_check_handle())); |
|
617 |
6017 |
register_handle(reinterpret_cast<uv_handle_t*>(immediate_idle_handle())); |
|
618 |
6017 |
register_handle(reinterpret_cast<uv_handle_t*>(&idle_prepare_handle_)); |
|
619 |
6017 |
register_handle(reinterpret_cast<uv_handle_t*>(&idle_check_handle_)); |
|
620 |
6017 |
register_handle(reinterpret_cast<uv_handle_t*>(&task_queues_async_)); |
|
621 |
6017 |
} |
|
622 |
|||
623 |
11080 |
void Environment::CleanupHandles() { |
|
624 |
{ |
||
625 |
11080 |
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_); |
|
626 |
11080 |
task_queues_async_initialized_ = false; |
|
627 |
} |
||
628 |
|||
629 |
Isolate::DisallowJavascriptExecutionScope disallow_js(isolate(), |
||
630 |
22160 |
Isolate::DisallowJavascriptExecutionScope::THROW_ON_FAILURE); |
|
631 |
|||
632 |
11080 |
RunAndClearNativeImmediates(true /* skip unrefed SetImmediate()s */); |
|
633 |
|||
634 |
✓✓ | 11099 |
for (ReqWrapBase* request : req_wrap_queue_) |
635 |
19 |
request->Cancel(); |
|
636 |
|||
637 |
✓✓ | 14585 |
for (HandleWrap* handle : handle_wrap_queue_) |
638 |
7010 |
handle->Close(); |
|
639 |
|||
640 |
✓✓ | 44134 |
for (HandleCleanup& hc : handle_cleanup_queue_) |
641 |
33054 |
hc.cb_(this, hc.handle_, hc.arg_); |
|
642 |
11080 |
handle_cleanup_queue_.clear(); |
|
643 |
|||
644 |
10294 |
while (handle_cleanup_waiting_ != 0 || |
|
645 |
✓✓✓✓ ✓✓ |
32457 |
request_waiting_ != 0 || |
646 |
✓✓ | 11083 |
!handle_wrap_queue_.IsEmpty()) { |
647 |
10294 |
uv_run(event_loop(), UV_RUN_ONCE); |
|
648 |
} |
||
649 |
11080 |
} |
|
650 |
|||
651 |
6017 |
void Environment::StartProfilerIdleNotifier() { |
|
652 |
6017 |
uv_prepare_start(&idle_prepare_handle_, [](uv_prepare_t* handle) { |
|
653 |
184116 |
Environment* env = ContainerOf(&Environment::idle_prepare_handle_, handle); |
|
654 |
184116 |
env->isolate()->SetIdle(true); |
|
655 |
184116 |
}); |
|
656 |
6017 |
uv_check_start(&idle_check_handle_, [](uv_check_t* handle) { |
|
657 |
183943 |
Environment* env = ContainerOf(&Environment::idle_check_handle_, handle); |
|
658 |
183943 |
env->isolate()->SetIdle(false); |
|
659 |
183943 |
}); |
|
660 |
6017 |
} |
|
661 |
|||
662 |
691954 |
void Environment::PrintSyncTrace() const { |
|
663 |
✓✓ | 691954 |
if (!trace_sync_io_) return; |
664 |
|||
665 |
2 |
HandleScope handle_scope(isolate()); |
|
666 |
|||
667 |
1 |
fprintf( |
|
668 |
stderr, "(node:%d) WARNING: Detected use of sync API\n", uv_os_getpid()); |
||
669 |
1 |
PrintStackTrace(isolate(), |
|
670 |
StackTrace::CurrentStackTrace( |
||
671 |
isolate(), stack_trace_limit(), StackTrace::kDetailed)); |
||
672 |
} |
||
673 |
|||
674 |
5544 |
void Environment::RunCleanup() { |
|
675 |
5544 |
started_cleanup_ = true; |
|
676 |
✓✓✓✓ |
15802 |
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunCleanup"); |
677 |
5544 |
bindings_.clear(); |
|
678 |
5544 |
CleanupHandles(); |
|
679 |
|||
680 |
✓✓ | 16626 |
while (!cleanup_hooks_.empty() || |
681 |
✓✗ | 11090 |
native_immediates_.size() > 0 || |
682 |
✓✓✗✓ ✓✓ |
22170 |
native_immediates_threadsafe_.size() > 0 || |
683 |
5544 |
native_immediates_interrupts_.size() > 0) { |
|
684 |
// Copy into a vector, since we can't sort an unordered_set in-place. |
||
685 |
std::vector<CleanupHookCallback> callbacks( |
||
686 |
11072 |
cleanup_hooks_.begin(), cleanup_hooks_.end()); |
|
687 |
// We can't erase the copied elements from `cleanup_hooks_` yet, because we |
||
688 |
// need to be able to check whether they were un-scheduled by another hook. |
||
689 |
|||
690 |
5536 |
std::sort(callbacks.begin(), callbacks.end(), |
|
691 |
955261 |
[](const CleanupHookCallback& a, const CleanupHookCallback& b) { |
|
692 |
// Sort in descending order so that the most recently inserted callbacks |
||
693 |
// are run first. |
||
694 |
955261 |
return a.insertion_order_counter_ > b.insertion_order_counter_; |
|
695 |
}); |
||
696 |
|||
697 |
✓✓ | 153030 |
for (const CleanupHookCallback& cb : callbacks) { |
698 |
✓✓ | 147494 |
if (cleanup_hooks_.count(cb) == 0) { |
699 |
// This hook was removed from the `cleanup_hooks_` set during another |
||
700 |
// hook that was run earlier. Nothing to do here. |
||
701 |
1137 |
continue; |
|
702 |
} |
||
703 |
|||
704 |
146357 |
cb.fn_(cb.arg_); |
|
705 |
146357 |
cleanup_hooks_.erase(cb); |
|
706 |
} |
||
707 |
5536 |
CleanupHandles(); |
|
708 |
} |
||
709 |
|||
710 |
✓✓ | 5547 |
for (const int fd : unmanaged_fds_) { |
711 |
uv_fs_t close_req; |
||
712 |
3 |
uv_fs_close(nullptr, &close_req, fd, nullptr); |
|
713 |
3 |
uv_fs_req_cleanup(&close_req); |
|
714 |
} |
||
715 |
5544 |
} |
|
716 |
|||
717 |
6139 |
void Environment::RunAtExitCallbacks() { |
|
718 |
✓✓✓✓ |
17481 |
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "AtExit"); |
719 |
✓✓ | 18209 |
for (ExitCallback at_exit : at_exit_functions_) { |
720 |
12070 |
at_exit.cb_(at_exit.arg_); |
|
721 |
} |
||
722 |
6139 |
at_exit_functions_.clear(); |
|
723 |
6139 |
} |
|
724 |
|||
725 |
12094 |
void Environment::AtExit(void (*cb)(void* arg), void* arg) { |
|
726 |
12094 |
at_exit_functions_.push_front(ExitCallback{cb, arg}); |
|
727 |
12094 |
} |
|
728 |
|||
729 |
222065 |
void Environment::RunAndClearInterrupts() { |
|
730 |
✓✓ | 222065 |
while (native_immediates_interrupts_.size() > 0) { |
731 |
10566 |
NativeImmediateQueue queue; |
|
732 |
{ |
||
733 |
21132 |
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_); |
|
734 |
10566 |
queue.ConcatMove(std::move(native_immediates_interrupts_)); |
|
735 |
} |
||
736 |
10566 |
DebugSealHandleScope seal_handle_scope(isolate()); |
|
737 |
|||
738 |
✓✓ | 21138 |
while (auto head = queue.Shift()) |
739 |
21144 |
head->Call(this); |
|
740 |
} |
||
741 |
211499 |
} |
|
742 |
|||
743 |
201303 |
void Environment::RunAndClearNativeImmediates(bool only_refed) { |
|
744 |
✓✓✓✓ |
407511 |
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), |
745 |
"RunAndClearNativeImmediates"); |
||
746 |
402600 |
HandleScope handle_scope(isolate_); |
|
747 |
402600 |
InternalCallbackScope cb_scope(this, Object::New(isolate_), { 0, 0 }); |
|
748 |
|||
749 |
201303 |
size_t ref_count = 0; |
|
750 |
|||
751 |
// Handle interrupts first. These functions are not allowed to throw |
||
752 |
// exceptions, so we do not need to handle that. |
||
753 |
201303 |
RunAndClearInterrupts(); |
|
754 |
|||
755 |
402604 |
auto drain_list = [&](NativeImmediateQueue* queue) { |
|
756 |
805202 |
TryCatchScope try_catch(this); |
|
757 |
402604 |
DebugSealHandleScope seal_handle_scope(isolate()); |
|
758 |
✓✓ | 463060 |
while (auto head = queue->Shift()) { |
759 |
60463 |
bool is_refed = head->flags() & CallbackFlags::kRefed; |
|
760 |
✓✓ | 60463 |
if (is_refed) |
761 |
36220 |
ref_count++; |
|
762 |
|||
763 |
✓✓✓✓ |
60463 |
if (is_refed || !only_refed) |
764 |
60267 |
head->Call(this); |
|
765 |
|||
766 |
60458 |
head.reset(); // Destroy now so that this is also observed by try_catch. |
|
767 |
|||
768 |
✓✓ | 60458 |
if (UNLIKELY(try_catch.HasCaught())) { |
769 |
✓✗✓✗ ✓✗ |
2 |
if (!try_catch.HasTerminated() && can_call_into_js()) |
770 |
2 |
errors::TriggerUncaughtException(isolate(), try_catch); |
|
771 |
|||
772 |
1 |
return true; |
|
773 |
} |
||
774 |
60456 |
} |
|
775 |
402597 |
return false; |
|
776 |
201303 |
}; |
|
777 |
✗✓ | 201303 |
while (drain_list(&native_immediates_)) {} |
778 |
|||
779 |
201300 |
immediate_info()->ref_count_dec(ref_count); |
|
780 |
|||
781 |
✓✓ | 201300 |
if (immediate_info()->ref_count() == 0) |
782 |
157482 |
ToggleImmediateRef(false); |
|
783 |
|||
784 |
// It is safe to check .size() first, because there is a causal relationship |
||
785 |
// between pushes to the threadsafe immediate list and this function being |
||
786 |
// called. For the common case, it's worth checking the size first before |
||
787 |
// establishing a mutex lock. |
||
788 |
// This is intentionally placed after the `ref_count` handling, because when |
||
789 |
// refed threadsafe immediates are created, they are not counted towards the |
||
790 |
// count in immediate_info() either. |
||
791 |
201297 |
NativeImmediateQueue threadsafe_immediates; |
|
792 |
✓✓ | 201300 |
if (native_immediates_threadsafe_.size() > 0) { |
793 |
2364 |
Mutex::ScopedLock lock(native_immediates_threadsafe_mutex_); |
|
794 |
1182 |
threadsafe_immediates.ConcatMove(std::move(native_immediates_threadsafe_)); |
|
795 |
} |
||
796 |
✓✓ | 201301 |
while (drain_list(&threadsafe_immediates)) {} |
797 |
201297 |
} |
|
798 |
|||
799 |
10576 |
void Environment::RequestInterruptFromV8() { |
|
800 |
// The Isolate may outlive the Environment, so some logic to handle the |
||
801 |
// situation in which the Environment is destroyed before the handler runs |
||
802 |
// is required. |
||
803 |
|||
804 |
// We allocate a new pointer to a pointer to this Environment instance, and |
||
805 |
// try to set it as interrupt_data_. If interrupt_data_ was already set, then |
||
806 |
// callbacks are already scheduled to run and we can delete our own pointer |
||
807 |
// and just return. If it was nullptr previously, the Environment** is stored; |
||
808 |
// ~Environment sets the Environment* contained in it to nullptr, so that |
||
809 |
// the callback can check whether ~Environment has already run and it is thus |
||
810 |
// not safe to access the Environment instance itself. |
||
811 |
10576 |
Environment** interrupt_data = new Environment*(this); |
|
812 |
10576 |
Environment** dummy = nullptr; |
|
813 |
✓✓ | 10576 |
if (!interrupt_data_.compare_exchange_strong(dummy, interrupt_data)) { |
814 |
480 |
delete interrupt_data; |
|
815 |
480 |
return; // Already scheduled. |
|
816 |
} |
||
817 |
|||
818 |
10096 |
isolate()->RequestInterrupt([](Isolate* isolate, void* data) { |
|
819 |
10088 |
std::unique_ptr<Environment*> env_ptr { static_cast<Environment**>(data) }; |
|
820 |
10088 |
Environment* env = *env_ptr; |
|
821 |
✓✓ | 10088 |
if (env == nullptr) { |
822 |
// The Environment has already been destroyed. That should be okay; any |
||
823 |
// callback added before the Environment shuts down would have been |
||
824 |
// handled during cleanup. |
||
825 |
10 |
return; |
|
826 |
} |
||
827 |
10078 |
env->interrupt_data_.store(nullptr); |
|
828 |
10078 |
env->RunAndClearInterrupts(); |
|
829 |
}, interrupt_data); |
||
830 |
} |
||
831 |
|||
832 |
9399 |
void Environment::ScheduleTimer(int64_t duration_ms) { |
|
833 |
✗✓ | 9399 |
if (started_cleanup_) return; |
834 |
9399 |
uv_timer_start(timer_handle(), RunTimers, duration_ms, 0); |
|
835 |
} |
||
836 |
|||
837 |
3609 |
void Environment::ToggleTimerRef(bool ref) { |
|
838 |
✗✓ | 3609 |
if (started_cleanup_) return; |
839 |
|||
840 |
✓✓ | 3609 |
if (ref) { |
841 |
2368 |
uv_ref(reinterpret_cast<uv_handle_t*>(timer_handle())); |
|
842 |
} else { |
||
843 |
1241 |
uv_unref(reinterpret_cast<uv_handle_t*>(timer_handle())); |
|
844 |
} |
||
845 |
} |
||
846 |
|||
847 |
7551 |
void Environment::RunTimers(uv_timer_t* handle) { |
|
848 |
7551 |
Environment* env = Environment::from_timer_handle(handle); |
|
849 |
✓✓✓✓ |
8000 |
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "RunTimers"); |
850 |
|||
851 |
✗✓ | 7551 |
if (!env->can_call_into_js()) |
852 |
return; |
||
853 |
|||
854 |
7551 |
HandleScope handle_scope(env->isolate()); |
|
855 |
7551 |
Context::Scope context_scope(env->context()); |
|
856 |
|||
857 |
7551 |
Local<Object> process = env->process_object(); |
|
858 |
7551 |
InternalCallbackScope scope(env, process, {0, 0}); |
|
859 |
|||
860 |
7551 |
Local<Function> cb = env->timers_callback_function(); |
|
861 |
MaybeLocal<Value> ret; |
||
862 |
7551 |
Local<Value> arg = env->GetNow(); |
|
863 |
// This code will loop until all currently due timers will process. It is |
||
864 |
// impossible for us to end up in an infinite loop due to how the JS-side |
||
865 |
// is structured. |
||
866 |
33 |
do { |
|
867 |
7584 |
TryCatchScope try_catch(env); |
|
868 |
7584 |
try_catch.SetVerbose(true); |
|
869 |
7584 |
ret = cb->Call(env->context(), process, 1, &arg); |
|
870 |
✓✓✓✓ ✓✓ |
7574 |
} while (ret.IsEmpty() && env->can_call_into_js()); |
871 |
|||
872 |
// NOTE(apapirovski): If it ever becomes possible that `call_into_js` above |
||
873 |
// is reset back to `true` after being previously set to `false` then this |
||
874 |
// code becomes invalid and needs to be rewritten. Otherwise catastrophic |
||
875 |
// timers corruption will occur and all timers behaviour will become |
||
876 |
// entirely unpredictable. |
||
877 |
✓✓ | 7541 |
if (ret.IsEmpty()) |
878 |
10 |
return; |
|
879 |
|||
880 |
// To allow for less JS-C++ boundary crossing, the value returned from JS |
||
881 |
// serves a few purposes: |
||
882 |
// 1. If it's 0, no more timers exist and the handle should be unrefed |
||
883 |
// 2. If it's > 0, the value represents the next timer's expiry and there |
||
884 |
// is at least one timer remaining that is refed. |
||
885 |
// 3. If it's < 0, the absolute value represents the next timer's expiry |
||
886 |
// and there are no timers that are refed. |
||
887 |
int64_t expiry_ms = |
||
888 |
7531 |
ret.ToLocalChecked()->IntegerValue(env->context()).FromJust(); |
|
889 |
|||
890 |
7531 |
uv_handle_t* h = reinterpret_cast<uv_handle_t*>(handle); |
|
891 |
|||
892 |
✓✓ | 7531 |
if (expiry_ms != 0) { |
893 |
int64_t duration_ms = |
||
894 |
6569 |
llabs(expiry_ms) - (uv_now(env->event_loop()) - env->timer_base()); |
|
895 |
|||
896 |
6569 |
env->ScheduleTimer(duration_ms > 0 ? duration_ms : 1); |
|
897 |
|||
898 |
✓✓ | 6569 |
if (expiry_ms > 0) |
899 |
5957 |
uv_ref(h); |
|
900 |
else |
||
901 |
612 |
uv_unref(h); |
|
902 |
} else { |
||
903 |
962 |
uv_unref(h); |
|
904 |
} |
||
905 |
} |
||
906 |
|||
907 |
|||
908 |
183943 |
void Environment::CheckImmediate(uv_check_t* handle) { |
|
909 |
183943 |
Environment* env = Environment::from_immediate_check_handle(handle); |
|
910 |
✓✓✓✓ |
186882 |
TRACE_EVENT0(TRACING_CATEGORY_NODE1(environment), "CheckImmediate"); |
911 |
|||
912 |
183943 |
HandleScope scope(env->isolate()); |
|
913 |
183943 |
Context::Scope context_scope(env->context()); |
|
914 |
|||
915 |
183943 |
env->RunAndClearNativeImmediates(); |
|
916 |
|||
917 |
✓✓✓✓ ✓✓ |
183943 |
if (env->immediate_info()->count() == 0 || !env->can_call_into_js()) |
918 |
141379 |
return; |
|
919 |
|||
920 |
946 |
do { |
|
921 |
43503 |
MakeCallback(env->isolate(), |
|
922 |
env->process_object(), |
||
923 |
env->immediate_callback_function(), |
||
924 |
0, |
||
925 |
nullptr, |
||
926 |
43510 |
{0, 0}).ToLocalChecked(); |
|
927 |
✓✓✓✓ ✓✓ |
43503 |
} while (env->immediate_info()->has_outstanding() && env->can_call_into_js()); |
928 |
|||
929 |
✓✓ | 42557 |
if (env->immediate_info()->ref_count() == 0) |
930 |
4689 |
env->ToggleImmediateRef(false); |
|
931 |
} |
||
932 |
|||
933 |
227995 |
void Environment::ToggleImmediateRef(bool ref) { |
|
934 |
✓✓ | 227995 |
if (started_cleanup_) return; |
935 |
|||
936 |
✓✓ | 217338 |
if (ref) { |
937 |
// Idle handle is needed only to stop the event loop from blocking in poll. |
||
938 |
65800 |
uv_idle_start(immediate_idle_handle(), [](uv_idle_t*){ }); |
|
939 |
} else { |
||
940 |
151538 |
uv_idle_stop(immediate_idle_handle()); |
|
941 |
} |
||
942 |
} |
||
943 |
|||
944 |
|||
945 |
29017 |
Local<Value> Environment::GetNow() { |
|
946 |
29017 |
uv_update_time(event_loop()); |
|
947 |
29017 |
uint64_t now = uv_now(event_loop()); |
|
948 |
✗✓ | 29017 |
CHECK_GE(now, timer_base()); |
949 |
29017 |
now -= timer_base(); |
|
950 |
✓✗ | 29017 |
if (now <= 0xffffffff) |
951 |
58034 |
return Integer::NewFromUnsigned(isolate(), static_cast<uint32_t>(now)); |
|
952 |
else |
||
953 |
return Number::New(isolate(), static_cast<double>(now)); |
||
954 |
} |
||
955 |
|||
956 |
28 |
void CollectExceptionInfo(Environment* env, |
|
957 |
Local<Object> obj, |
||
958 |
int errorno, |
||
959 |
const char* err_string, |
||
960 |
const char* syscall, |
||
961 |
const char* message, |
||
962 |
const char* path, |
||
963 |
const char* dest) { |
||
964 |
28 |
obj->Set(env->context(), |
|
965 |
env->errno_string(), |
||
966 |
112 |
Integer::New(env->isolate(), errorno)).Check(); |
|
967 |
|||
968 |
28 |
obj->Set(env->context(), env->code_string(), |
|
969 |
84 |
OneByteString(env->isolate(), err_string)).Check(); |
|
970 |
|||
971 |
✓✗ | 28 |
if (message != nullptr) { |
972 |
28 |
obj->Set(env->context(), env->message_string(), |
|
973 |
112 |
OneByteString(env->isolate(), message)).Check(); |
|
974 |
} |
||
975 |
|||
976 |
Local<Value> path_buffer; |
||
977 |
✗✓ | 28 |
if (path != nullptr) { |
978 |
path_buffer = |
||
979 |
Buffer::Copy(env->isolate(), path, strlen(path)).ToLocalChecked(); |
||
980 |
obj->Set(env->context(), env->path_string(), path_buffer).Check(); |
||
981 |
} |
||
982 |
|||
983 |
Local<Value> dest_buffer; |
||
984 |
✗✓ | 28 |
if (dest != nullptr) { |
985 |
dest_buffer = |
||
986 |
Buffer::Copy(env->isolate(), dest, strlen(dest)).ToLocalChecked(); |
||
987 |
obj->Set(env->context(), env->dest_string(), dest_buffer).Check(); |
||
988 |
} |
||
989 |
|||
990 |
✓✗ | 28 |
if (syscall != nullptr) { |
991 |
28 |
obj->Set(env->context(), env->syscall_string(), |
|
992 |
112 |
OneByteString(env->isolate(), syscall)).Check(); |
|
993 |
} |
||
994 |
28 |
} |
|
995 |
|||
996 |
28 |
void Environment::CollectUVExceptionInfo(Local<Value> object, |
|
997 |
int errorno, |
||
998 |
const char* syscall, |
||
999 |
const char* message, |
||
1000 |
const char* path, |
||
1001 |
const char* dest) { |
||
1002 |
✓✗✗✓ ✗✓ |
28 |
if (!object->IsObject() || errorno == 0) |
1003 |
return; |
||
1004 |
|||
1005 |
28 |
Local<Object> obj = object.As<Object>(); |
|
1006 |
28 |
const char* err_string = uv_err_name(errorno); |
|
1007 |
|||
1008 |
✗✓✗✗ |
28 |
if (message == nullptr || message[0] == '\0') { |
1009 |
28 |
message = uv_strerror(errorno); |
|
1010 |
} |
||
1011 |
|||
1012 |
28 |
node::CollectExceptionInfo(this, obj, errorno, err_string, |
|
1013 |
syscall, message, path, dest); |
||
1014 |
} |
||
1015 |
|||
1016 |
6052 |
ImmediateInfo::ImmediateInfo(Isolate* isolate, const SerializeInfo* info) |
|
1017 |
✓✓ | 6052 |
: fields_(isolate, kFieldsCount, MAYBE_FIELD_PTR(info, fields)) {} |
1018 |
|||
1019 |
6 |
ImmediateInfo::SerializeInfo ImmediateInfo::Serialize( |
|
1020 |
Local<Context> context, SnapshotCreator* creator) { |
||
1021 |
6 |
return {fields_.Serialize(context, creator)}; |
|
1022 |
} |
||
1023 |
|||
1024 |
5199 |
void ImmediateInfo::Deserialize(Local<Context> context) { |
|
1025 |
5199 |
fields_.Deserialize(context); |
|
1026 |
5199 |
} |
|
1027 |
|||
1028 |
6 |
std::ostream& operator<<(std::ostream& output, |
|
1029 |
const ImmediateInfo::SerializeInfo& i) { |
||
1030 |
6 |
output << "{ " << i.fields << " }"; |
|
1031 |
6 |
return output; |
|
1032 |
} |
||
1033 |
|||
1034 |
24 |
void ImmediateInfo::MemoryInfo(MemoryTracker* tracker) const { |
|
1035 |
24 |
tracker->TrackField("fields", fields_); |
|
1036 |
24 |
} |
|
1037 |
|||
1038 |
6 |
TickInfo::SerializeInfo TickInfo::Serialize(Local<Context> context, |
|
1039 |
SnapshotCreator* creator) { |
||
1040 |
6 |
return {fields_.Serialize(context, creator)}; |
|
1041 |
} |
||
1042 |
|||
1043 |
5199 |
void TickInfo::Deserialize(Local<Context> context) { |
|
1044 |
5199 |
fields_.Deserialize(context); |
|
1045 |
5199 |
} |
|
1046 |
|||
1047 |
6 |
std::ostream& operator<<(std::ostream& output, |
|
1048 |
const TickInfo::SerializeInfo& i) { |
||
1049 |
6 |
output << "{ " << i.fields << " }"; |
|
1050 |
6 |
return output; |
|
1051 |
} |
||
1052 |
|||
1053 |
24 |
void TickInfo::MemoryInfo(MemoryTracker* tracker) const { |
|
1054 |
24 |
tracker->TrackField("fields", fields_); |
|
1055 |
24 |
} |
|
1056 |
|||
1057 |
6052 |
TickInfo::TickInfo(Isolate* isolate, const SerializeInfo* info) |
|
1058 |
: fields_( |
||
1059 |
✓✓ | 6052 |
isolate, kFieldsCount, info == nullptr ? nullptr : &(info->fields)) {} |
1060 |
|||
1061 |
6052 |
AsyncHooks::AsyncHooks(Isolate* isolate, const SerializeInfo* info) |
|
1062 |
: async_ids_stack_(isolate, 16 * 2, MAYBE_FIELD_PTR(info, async_ids_stack)), |
||
1063 |
fields_(isolate, kFieldsCount, MAYBE_FIELD_PTR(info, fields)), |
||
1064 |
async_id_fields_( |
||
1065 |
isolate, kUidFieldsCount, MAYBE_FIELD_PTR(info, async_id_fields)), |
||
1066 |
✓✓✓✓ ✓✓ |
6052 |
info_(info) { |
1067 |
12104 |
HandleScope handle_scope(isolate); |
|
1068 |
✓✓ | 6052 |
if (info == nullptr) { |
1069 |
853 |
clear_async_id_stack(); |
|
1070 |
|||
1071 |
// Always perform async_hooks checks, not just when async_hooks is enabled. |
||
1072 |
// TODO(AndreasMadsen): Consider removing this for LTS releases. |
||
1073 |
// See discussion in https://github.com/nodejs/node/pull/15454 |
||
1074 |
// When removing this, do it by reverting the commit. Otherwise the test |
||
1075 |
// and flag changes won't be included. |
||
1076 |
853 |
fields_[kCheck] = 1; |
|
1077 |
|||
1078 |
// kDefaultTriggerAsyncId should be -1, this indicates that there is no |
||
1079 |
// specified default value and it should fallback to the executionAsyncId. |
||
1080 |
// 0 is not used as the magic value, because that indicates a missing |
||
1081 |
// context which is different from a default context. |
||
1082 |
853 |
async_id_fields_[AsyncHooks::kDefaultTriggerAsyncId] = -1; |
|
1083 |
|||
1084 |
// kAsyncIdCounter should start at 1 because that'll be the id the execution |
||
1085 |
// context during bootstrap (code that runs before entering uv_run()). |
||
1086 |
853 |
async_id_fields_[AsyncHooks::kAsyncIdCounter] = 1; |
|
1087 |
} |
||
1088 |
6052 |
} |
|
1089 |
|||
1090 |
5199 |
void AsyncHooks::Deserialize(Local<Context> context) { |
|
1091 |
5199 |
async_ids_stack_.Deserialize(context); |
|
1092 |
5199 |
fields_.Deserialize(context); |
|
1093 |
5199 |
async_id_fields_.Deserialize(context); |
|
1094 |
|||
1095 |
Local<Array> js_execution_async_resources; |
||
1096 |
✓✗ | 5199 |
if (info_->js_execution_async_resources != 0) { |
1097 |
js_execution_async_resources = |
||
1098 |
5199 |
context->GetDataFromSnapshotOnce<Array>( |
|
1099 |
✗✓ | 15597 |
info_->js_execution_async_resources).ToLocalChecked(); |
1100 |
} else { |
||
1101 |
js_execution_async_resources = Array::New(context->GetIsolate()); |
||
1102 |
} |
||
1103 |
5199 |
js_execution_async_resources_.Reset( |
|
1104 |
context->GetIsolate(), js_execution_async_resources); |
||
1105 |
|||
1106 |
// The native_execution_async_resources_ field requires v8::Local<> instances |
||
1107 |
// for async calls whose resources were on the stack as JS objects when they |
||
1108 |
// were entered. We cannot recreate this here; however, storing these values |
||
1109 |
// on the JS equivalent gives the same result, so we do that instead. |
||
1110 |
✗✓ | 5199 |
for (size_t i = 0; i < info_->native_execution_async_resources.size(); ++i) { |
1111 |
if (info_->native_execution_async_resources[i] == SIZE_MAX) |
||
1112 |
continue; |
||
1113 |
Local<Object> obj = context->GetDataFromSnapshotOnce<Object>( |
||
1114 |
info_->native_execution_async_resources[i]) |
||
1115 |
.ToLocalChecked(); |
||
1116 |
js_execution_async_resources->Set(context, i, obj).Check(); |
||
1117 |
} |
||
1118 |
5199 |
info_ = nullptr; |
|
1119 |
5199 |
} |
|
1120 |
|||
1121 |
6 |
std::ostream& operator<<(std::ostream& output, |
|
1122 |
const std::vector<SnapshotIndex>& v) { |
||
1123 |
6 |
output << "{ "; |
|
1124 |
✗✓ | 6 |
for (const SnapshotIndex i : v) { |
1125 |
output << i << ", "; |
||
1126 |
} |
||
1127 |
6 |
output << " }"; |
|
1128 |
6 |
return output; |
|
1129 |
} |
||
1130 |
|||
1131 |
6 |
std::ostream& operator<<(std::ostream& output, |
|
1132 |
const AsyncHooks::SerializeInfo& i) { |
||
1133 |
output << "{\n" |
||
1134 |
6 |
<< " " << i.async_ids_stack << ", // async_ids_stack\n" |
|
1135 |
6 |
<< " " << i.fields << ", // fields\n" |
|
1136 |
6 |
<< " " << i.async_id_fields << ", // async_id_fields\n" |
|
1137 |
6 |
<< " " << i.js_execution_async_resources |
|
1138 |
<< ", // js_execution_async_resources\n" |
||
1139 |
6 |
<< " " << i.native_execution_async_resources |
|
1140 |
<< ", // native_execution_async_resources\n" |
||
1141 |
6 |
<< "}"; |
|
1142 |
6 |
return output; |
|
1143 |
} |
||
1144 |
|||
1145 |
6 |
AsyncHooks::SerializeInfo AsyncHooks::Serialize(Local<Context> context, |
|
1146 |
SnapshotCreator* creator) { |
||
1147 |
6 |
SerializeInfo info; |
|
1148 |
6 |
info.async_ids_stack = async_ids_stack_.Serialize(context, creator); |
|
1149 |
6 |
info.fields = fields_.Serialize(context, creator); |
|
1150 |
6 |
info.async_id_fields = async_id_fields_.Serialize(context, creator); |
|
1151 |
✓✗ | 6 |
if (!js_execution_async_resources_.IsEmpty()) { |
1152 |
6 |
info.js_execution_async_resources = creator->AddData( |
|
1153 |
context, js_execution_async_resources_.Get(context->GetIsolate())); |
||
1154 |
✗✓ | 6 |
CHECK_NE(info.js_execution_async_resources, 0); |
1155 |
} else { |
||
1156 |
info.js_execution_async_resources = 0; |
||
1157 |
} |
||
1158 |
|||
1159 |
6 |
info.native_execution_async_resources.resize( |
|
1160 |
native_execution_async_resources_.size()); |
||
1161 |
✗✓ | 6 |
for (size_t i = 0; i < native_execution_async_resources_.size(); i++) { |
1162 |
info.native_execution_async_resources[i] = |
||
1163 |
native_execution_async_resources_[i].IsEmpty() ? SIZE_MAX : |
||
1164 |
creator->AddData( |
||
1165 |
context, |
||
1166 |
native_execution_async_resources_[i]); |
||
1167 |
} |
||
1168 |
✗✓ | 6 |
CHECK_EQ(contexts_.size(), 1); |
1169 |
✗✓✗✓ |
12 |
CHECK_EQ(contexts_[0], env()->context()); |
1170 |
✗✓ | 6 |
CHECK(js_promise_hooks_[0].IsEmpty()); |
1171 |
✗✓ | 6 |
CHECK(js_promise_hooks_[1].IsEmpty()); |
1172 |
✗✓ | 6 |
CHECK(js_promise_hooks_[2].IsEmpty()); |
1173 |
✗✓ | 6 |
CHECK(js_promise_hooks_[3].IsEmpty()); |
1174 |
|||
1175 |
6 |
return info; |
|
1176 |
} |
||
1177 |
|||
1178 |
24 |
void AsyncHooks::MemoryInfo(MemoryTracker* tracker) const { |
|
1179 |
24 |
tracker->TrackField("async_ids_stack", async_ids_stack_); |
|
1180 |
24 |
tracker->TrackField("fields", fields_); |
|
1181 |
24 |
tracker->TrackField("async_id_fields", async_id_fields_); |
|
1182 |
24 |
tracker->TrackField("js_promise_hooks", js_promise_hooks_); |
|
1183 |
24 |
} |
|
1184 |
|||
1185 |
4 |
void AsyncHooks::grow_async_ids_stack() { |
|
1186 |
4 |
async_ids_stack_.reserve(async_ids_stack_.Length() * 3); |
|
1187 |
|||
1188 |
4 |
env()->async_hooks_binding()->Set( |
|
1189 |
env()->context(), |
||
1190 |
env()->async_ids_stack_string(), |
||
1191 |
12 |
async_ids_stack_.GetJSArray()).Check(); |
|
1192 |
4 |
} |
|
1193 |
|||
1194 |
4 |
void AsyncHooks::FailWithCorruptedAsyncStack(double expected_async_id) { |
|
1195 |
4 |
fprintf(stderr, |
|
1196 |
"Error: async hook stack has become corrupted (" |
||
1197 |
"actual: %.f, expected: %.f)\n", |
||
1198 |
async_id_fields_.GetValue(kExecutionAsyncId), |
||
1199 |
expected_async_id); |
||
1200 |
4 |
DumpBacktrace(stderr); |
|
1201 |
4 |
fflush(stderr); |
|
1202 |
✓✗ | 4 |
if (!env()->abort_on_uncaught_exception()) |
1203 |
4 |
exit(1); |
|
1204 |
fprintf(stderr, "\n"); |
||
1205 |
fflush(stderr); |
||
1206 |
ABORT_NO_BACKTRACE(); |
||
1207 |
} |
||
1208 |
|||
1209 |
578 |
void Environment::Exit(int exit_code) { |
|
1210 |
✓✓ | 578 |
if (options()->trace_exit) { |
1211 |
4 |
HandleScope handle_scope(isolate()); |
|
1212 |
Isolate::DisallowJavascriptExecutionScope disallow_js( |
||
1213 |
4 |
isolate(), Isolate::DisallowJavascriptExecutionScope::CRASH_ON_FAILURE); |
|
1214 |
|||
1215 |
✓✓ | 2 |
if (is_main_thread()) { |
1216 |
1 |
fprintf(stderr, "(node:%d) ", uv_os_getpid()); |
|
1217 |
} else { |
||
1218 |
1 |
fprintf(stderr, "(node:%d, thread:%" PRIu64 ") ", |
|
1219 |
uv_os_getpid(), thread_id()); |
||
1220 |
} |
||
1221 |
|||
1222 |
2 |
fprintf( |
|
1223 |
stderr, "WARNING: Exited the environment with code %d\n", exit_code); |
||
1224 |
2 |
PrintStackTrace(isolate(), |
|
1225 |
StackTrace::CurrentStackTrace( |
||
1226 |
isolate(), stack_trace_limit(), StackTrace::kDetailed)); |
||
1227 |
} |
||
1228 |
578 |
process_exit_handler_(this, exit_code); |
|
1229 |
75 |
} |
|
1230 |
|||
1231 |
6073 |
void Environment::stop_sub_worker_contexts() { |
|
1232 |
DCHECK_EQ(Isolate::GetCurrent(), isolate()); |
||
1233 |
|||
1234 |
✓✓ | 6073 |
while (!sub_worker_contexts_.empty()) { |
1235 |
26 |
Worker* w = *sub_worker_contexts_.begin(); |
|
1236 |
26 |
remove_sub_worker_context(w); |
|
1237 |
26 |
w->Exit(1); |
|
1238 |
26 |
w->JoinThread(); |
|
1239 |
} |
||
1240 |
6047 |
} |
|
1241 |
|||
1242 |
10 |
Environment* Environment::worker_parent_env() const { |
|
1243 |
✓✗ | 10 |
if (worker_context() == nullptr) return nullptr; |
1244 |
return worker_context()->env(); |
||
1245 |
} |
||
1246 |
|||
1247 |
65266 |
void Environment::AddUnmanagedFd(int fd) { |
|
1248 |
✓✓ | 65266 |
if (!tracks_unmanaged_fds()) return; |
1249 |
3213 |
auto result = unmanaged_fds_.insert(fd); |
|
1250 |
✓✓ | 3213 |
if (!result.second) { |
1251 |
ProcessEmitWarning( |
||
1252 |
1 |
this, "File descriptor %d opened in unmanaged mode twice", fd); |
|
1253 |
} |
||
1254 |
} |
||
1255 |
|||
1256 |
64887 |
void Environment::RemoveUnmanagedFd(int fd) { |
|
1257 |
✓✓ | 64887 |
if (!tracks_unmanaged_fds()) return; |
1258 |
3210 |
size_t removed_count = unmanaged_fds_.erase(fd); |
|
1259 |
✓✓ | 3210 |
if (removed_count == 0) { |
1260 |
ProcessEmitWarning( |
||
1261 |
1 |
this, "File descriptor %d closed but not opened in unmanaged mode", fd); |
|
1262 |
} |
||
1263 |
} |
||
1264 |
|||
1265 |
5123 |
void Environment::PrintInfoForSnapshotIfDebug() { |
|
1266 |
✗✓ | 5123 |
if (enabled_debug_list()->enabled(DebugCategory::MKSNAPSHOT)) { |
1267 |
fprintf(stderr, "BaseObjects at the exit of the Environment:\n"); |
||
1268 |
PrintAllBaseObjects(); |
||
1269 |
fprintf(stderr, "\nNative modules without cache:\n"); |
||
1270 |
for (const auto& s : native_modules_without_cache) { |
||
1271 |
fprintf(stderr, "%s\n", s.c_str()); |
||
1272 |
} |
||
1273 |
fprintf(stderr, "\nNative modules with cache:\n"); |
||
1274 |
for (const auto& s : native_modules_with_cache) { |
||
1275 |
fprintf(stderr, "%s\n", s.c_str()); |
||
1276 |
} |
||
1277 |
fprintf(stderr, "\nStatic bindings (need to be registered):\n"); |
||
1278 |
for (const auto mod : internal_bindings) { |
||
1279 |
fprintf(stderr, "%s:%s\n", mod->nm_filename, mod->nm_modname); |
||
1280 |
} |
||
1281 |
} |
||
1282 |
5123 |
} |
|
1283 |
|||
1284 |
void Environment::PrintAllBaseObjects() { |
||
1285 |
size_t i = 0; |
||
1286 |
std::cout << "BaseObjects\n"; |
||
1287 |
ForEachBaseObject([&](BaseObject* obj) { |
||
1288 |
std::cout << "#" << i++ << " " << obj << ": " << |
||
1289 |
obj->MemoryInfoName() << "\n"; |
||
1290 |
}); |
||
1291 |
} |
||
1292 |
|||
1293 |
5123 |
void Environment::VerifyNoStrongBaseObjects() { |
|
1294 |
// When a process exits cleanly, i.e. because the event loop ends up without |
||
1295 |
// things to wait for, the Node.js objects that are left on the heap should |
||
1296 |
// be: |
||
1297 |
// |
||
1298 |
// 1. weak, i.e. ready for garbage collection once no longer referenced, or |
||
1299 |
// 2. detached, i.e. scheduled for destruction once no longer referenced, or |
||
1300 |
// 3. an unrefed libuv handle, i.e. does not keep the event loop alive, or |
||
1301 |
// 4. an inactive libuv handle (essentially the same here) |
||
1302 |
// |
||
1303 |
// There are a few exceptions to this rule, but generally, if there are |
||
1304 |
// C++-backed Node.js objects on the heap that do not fall into the above |
||
1305 |
// categories, we may be looking at a potential memory leak. Most likely, |
||
1306 |
// the cause is a missing MakeWeak() call on the corresponding object. |
||
1307 |
// |
||
1308 |
// In order to avoid this kind of problem, we check the list of BaseObjects |
||
1309 |
// for these criteria. Currently, we only do so when explicitly instructed to |
||
1310 |
// or when in debug mode (where --verify-base-objects is always-on). |
||
1311 |
|||
1312 |
✓✗ | 5123 |
if (!options()->verify_base_objects) return; |
1313 |
|||
1314 |
ForEachBaseObject([](BaseObject* obj) { |
||
1315 |
if (obj->IsNotIndicativeOfMemoryLeakAtExit()) return; |
||
1316 |
fprintf(stderr, "Found bad BaseObject during clean exit: %s\n", |
||
1317 |
obj->MemoryInfoName().c_str()); |
||
1318 |
fflush(stderr); |
||
1319 |
ABORT(); |
||
1320 |
}); |
||
1321 |
} |
||
1322 |
|||
1323 |
6 |
EnvSerializeInfo Environment::Serialize(SnapshotCreator* creator) { |
|
1324 |
6 |
EnvSerializeInfo info; |
|
1325 |
6 |
Local<Context> ctx = context(); |
|
1326 |
|||
1327 |
6 |
SerializeBindingData(this, creator, &info); |
|
1328 |
// Currently all modules are compiled without cache in builtin snapshot |
||
1329 |
// builder. |
||
1330 |
12 |
info.native_modules = std::vector<std::string>( |
|
1331 |
6 |
native_modules_without_cache.begin(), native_modules_without_cache.end()); |
|
1332 |
|||
1333 |
6 |
info.async_hooks = async_hooks_.Serialize(ctx, creator); |
|
1334 |
6 |
info.immediate_info = immediate_info_.Serialize(ctx, creator); |
|
1335 |
6 |
info.tick_info = tick_info_.Serialize(ctx, creator); |
|
1336 |
6 |
info.performance_state = performance_state_->Serialize(ctx, creator); |
|
1337 |
6 |
info.stream_base_state = stream_base_state_.Serialize(ctx, creator); |
|
1338 |
6 |
info.should_abort_on_uncaught_toggle = |
|
1339 |
6 |
should_abort_on_uncaught_toggle_.Serialize(ctx, creator); |
|
1340 |
|||
1341 |
6 |
size_t id = 0; |
|
1342 |
#define V(PropertyName, TypeName) \ |
||
1343 |
do { \ |
||
1344 |
Local<TypeName> field = PropertyName(); \ |
||
1345 |
if (!field.IsEmpty()) { \ |
||
1346 |
size_t index = creator->AddData(field); \ |
||
1347 |
info.persistent_templates.push_back({#PropertyName, id, index}); \ |
||
1348 |
} \ |
||
1349 |
id++; \ |
||
1350 |
} while (0); |
||
1351 |
✓✗✗✓ ✓✗✓✗ ✗✓✗✓ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✗✓✗✓ ✗✓✓✗ ✗✓✗✓ ✓✗✓✗ ✗✓✗✓ ✗✓✓✗ ✓✗✓✗ ✗✓✗✓ ✗✓✗✓ ✗✓✓✗ ✓✗✗✓ |
336 |
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V) |
1352 |
#undef V |
||
1353 |
|||
1354 |
6 |
id = 0; |
|
1355 |
#define V(PropertyName, TypeName) \ |
||
1356 |
do { \ |
||
1357 |
Local<TypeName> field = PropertyName(); \ |
||
1358 |
if (!field.IsEmpty()) { \ |
||
1359 |
size_t index = creator->AddData(ctx, field); \ |
||
1360 |
info.persistent_values.push_back({#PropertyName, id, index}); \ |
||
1361 |
} \ |
||
1362 |
id++; \ |
||
1363 |
} while (0); |
||
1364 |
✓✗✓✗ ✗✓✓✗ ✓✗✓✗ ✓✗✓✗ ✗✓✓✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✗✓✗✓ ✓✗✓✗ ✓✗✗✓ ✗✓✓✗ ✗✓✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✗✓✓✗ ✓✗✗✓ ✓✗✗✓ ✓✗✗✓ ✗✓ |
564 |
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V) |
1365 |
#undef V |
||
1366 |
|||
1367 |
6 |
info.context = creator->AddData(ctx, context()); |
|
1368 |
6 |
return info; |
|
1369 |
} |
||
1370 |
|||
1371 |
18 |
std::ostream& operator<<(std::ostream& output, |
|
1372 |
const std::vector<PropInfo>& vec) { |
||
1373 |
18 |
output << "{\n"; |
|
1374 |
✓✓ | 360 |
for (const auto& info : vec) { |
1375 |
684 |
output << " { \"" << info.name << "\", " << std::to_string(info.id) << ", " |
|
1376 |
684 |
<< std::to_string(info.index) << " },\n"; |
|
1377 |
} |
||
1378 |
18 |
output << "}"; |
|
1379 |
18 |
return output; |
|
1380 |
} |
||
1381 |
|||
1382 |
6 |
std::ostream& operator<<(std::ostream& output, |
|
1383 |
const std::vector<std::string>& vec) { |
||
1384 |
6 |
output << "{\n"; |
|
1385 |
✓✓ | 696 |
for (const auto& info : vec) { |
1386 |
690 |
output << " \"" << info << "\",\n"; |
|
1387 |
} |
||
1388 |
6 |
output << "}"; |
|
1389 |
6 |
return output; |
|
1390 |
} |
||
1391 |
|||
1392 |
6 |
std::ostream& operator<<(std::ostream& output, const EnvSerializeInfo& i) { |
|
1393 |
output << "{\n" |
||
1394 |
6 |
<< "// -- bindings begins --\n" |
|
1395 |
6 |
<< i.bindings << ",\n" |
|
1396 |
<< "// -- bindings ends --\n" |
||
1397 |
6 |
<< "// -- native_modules begins --\n" |
|
1398 |
6 |
<< i.native_modules << ",\n" |
|
1399 |
<< "// -- native_modules ends --\n" |
||
1400 |
6 |
<< "// -- async_hooks begins --\n" |
|
1401 |
6 |
<< i.async_hooks << ",\n" |
|
1402 |
6 |
<< "// -- async_hooks ends --\n" |
|
1403 |
6 |
<< i.tick_info << ", // tick_info\n" |
|
1404 |
6 |
<< i.immediate_info << ", // immediate_info\n" |
|
1405 |
6 |
<< "// -- performance_state begins --\n" |
|
1406 |
6 |
<< i.performance_state << ",\n" |
|
1407 |
6 |
<< "// -- performance_state ends --\n" |
|
1408 |
6 |
<< i.stream_base_state << ", // stream_base_state\n" |
|
1409 |
6 |
<< i.should_abort_on_uncaught_toggle |
|
1410 |
<< ", // should_abort_on_uncaught_toggle\n" |
||
1411 |
6 |
<< "// -- persistent_templates begins --\n" |
|
1412 |
6 |
<< i.persistent_templates << ",\n" |
|
1413 |
<< "// persistent_templates ends --\n" |
||
1414 |
6 |
<< "// -- persistent_values begins --\n" |
|
1415 |
6 |
<< i.persistent_values << ",\n" |
|
1416 |
6 |
<< "// -- persistent_values ends --\n" |
|
1417 |
6 |
<< i.context << ", // context\n" |
|
1418 |
6 |
<< "}"; |
|
1419 |
6 |
return output; |
|
1420 |
} |
||
1421 |
|||
1422 |
20796 |
void Environment::EnqueueDeserializeRequest(DeserializeRequestCallback cb, |
|
1423 |
Local<Object> holder, |
||
1424 |
int index, |
||
1425 |
InternalFieldInfo* info) { |
||
1426 |
41592 |
DeserializeRequest request{cb, {isolate(), holder}, index, info}; |
|
1427 |
20796 |
deserialize_requests_.push_back(std::move(request)); |
|
1428 |
20796 |
} |
|
1429 |
|||
1430 |
5199 |
void Environment::RunDeserializeRequests() { |
|
1431 |
10398 |
HandleScope scope(isolate()); |
|
1432 |
5199 |
Local<Context> ctx = context(); |
|
1433 |
5199 |
Isolate* is = isolate(); |
|
1434 |
✓✓ | 25995 |
while (!deserialize_requests_.empty()) { |
1435 |
41592 |
DeserializeRequest request(std::move(deserialize_requests_.front())); |
|
1436 |
20796 |
deserialize_requests_.pop_front(); |
|
1437 |
20796 |
Local<Object> holder = request.holder.Get(is); |
|
1438 |
20796 |
request.cb(ctx, holder, request.index, request.info); |
|
1439 |
request.holder.Reset(); |
||
1440 |
20796 |
request.info->Delete(); |
|
1441 |
} |
||
1442 |
5199 |
} |
|
1443 |
|||
1444 |
5199 |
void Environment::DeserializeProperties(const EnvSerializeInfo* info) { |
|
1445 |
5199 |
Local<Context> ctx = context(); |
|
1446 |
|||
1447 |
5199 |
RunDeserializeRequests(); |
|
1448 |
|||
1449 |
5199 |
native_modules_in_snapshot = info->native_modules; |
|
1450 |
5199 |
async_hooks_.Deserialize(ctx); |
|
1451 |
5199 |
immediate_info_.Deserialize(ctx); |
|
1452 |
5199 |
tick_info_.Deserialize(ctx); |
|
1453 |
5199 |
performance_state_->Deserialize(ctx); |
|
1454 |
5199 |
stream_base_state_.Deserialize(ctx); |
|
1455 |
5199 |
should_abort_on_uncaught_toggle_.Deserialize(ctx); |
|
1456 |
|||
1457 |
✗✓ | 5199 |
if (enabled_debug_list_.enabled(DebugCategory::MKSNAPSHOT)) { |
1458 |
fprintf(stderr, "deserializing...\n"); |
||
1459 |
std::cerr << *info << "\n"; |
||
1460 |
} |
||
1461 |
|||
1462 |
5199 |
const std::vector<PropInfo>& templates = info->persistent_templates; |
|
1463 |
5199 |
size_t i = 0; // index to the array |
|
1464 |
5199 |
size_t id = 0; |
|
1465 |
#define SetProperty(PropertyName, TypeName, vector, type, from) \ |
||
1466 |
do { \ |
||
1467 |
if (vector.size() > i && id == vector[i].id) { \ |
||
1468 |
const PropInfo& d = vector[i]; \ |
||
1469 |
DCHECK_EQ(d.name, #PropertyName); \ |
||
1470 |
MaybeLocal<TypeName> maybe_field = \ |
||
1471 |
from->GetDataFromSnapshotOnce<TypeName>(d.index); \ |
||
1472 |
Local<TypeName> field; \ |
||
1473 |
if (!maybe_field.ToLocal(&field)) { \ |
||
1474 |
fprintf(stderr, \ |
||
1475 |
"Failed to deserialize environment " #type " " #PropertyName \ |
||
1476 |
"\n"); \ |
||
1477 |
} \ |
||
1478 |
set_##PropertyName(field); \ |
||
1479 |
i++; \ |
||
1480 |
} \ |
||
1481 |
} while (0); \ |
||
1482 |
id++; |
||
1483 |
#define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \ |
||
1484 |
templates, template, isolate_) |
||
1485 |
✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✓✗✗ |
202761 |
ENVIRONMENT_STRONG_PERSISTENT_TEMPLATES(V); |
1486 |
#undef V |
||
1487 |
|||
1488 |
5199 |
i = 0; // index to the array |
|
1489 |
5199 |
id = 0; |
|
1490 |
5199 |
const std::vector<PropInfo>& values = info->persistent_values; |
|
1491 |
#define V(PropertyName, TypeName) SetProperty(PropertyName, TypeName, \ |
||
1492 |
values, value, ctx) |
||
1493 |
✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✓✗✗✓ ✗✓✗✗ ✓✗✓✗ ✓✗✗✓ ✗✓✗✗ ✗✓✗✗ ✗✓✗✗ ✗✓✗✗ |
535497 |
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V); |
1494 |
#undef V |
||
1495 |
#undef SetProperty |
||
1496 |
|||
1497 |
MaybeLocal<Context> maybe_ctx_from_snapshot = |
||
1498 |
10398 |
ctx->GetDataFromSnapshotOnce<Context>(info->context); |
|
1499 |
Local<Context> ctx_from_snapshot; |
||
1500 |
✗✓ | 5199 |
if (!maybe_ctx_from_snapshot.ToLocal(&ctx_from_snapshot)) { |
1501 |
fprintf(stderr, |
||
1502 |
"Failed to deserialize context back reference from the snapshot\n"); |
||
1503 |
} |
||
1504 |
✗✓ | 5199 |
CHECK_EQ(ctx_from_snapshot, ctx); |
1505 |
5199 |
} |
|
1506 |
|||
1507 |
1 |
uint64_t GuessMemoryAvailableToTheProcess() { |
|
1508 |
1 |
uint64_t free_in_system = uv_get_free_memory(); |
|
1509 |
1 |
size_t allowed = uv_get_constrained_memory(); |
|
1510 |
✗✓ | 1 |
if (allowed == 0) { |
1511 |
return free_in_system; |
||
1512 |
} |
||
1513 |
size_t rss; |
||
1514 |
1 |
int err = uv_resident_set_memory(&rss); |
|
1515 |
✗✓ | 1 |
if (err) { |
1516 |
return free_in_system; |
||
1517 |
} |
||
1518 |
✗✓ | 1 |
if (allowed < rss) { |
1519 |
// Something is probably wrong. Fallback to the free memory. |
||
1520 |
return free_in_system; |
||
1521 |
} |
||
1522 |
// There may still be room for swap, but we will just leave it here. |
||
1523 |
1 |
return allowed - rss; |
|
1524 |
} |
||
1525 |
|||
1526 |
24 |
void Environment::BuildEmbedderGraph(Isolate* isolate, |
|
1527 |
EmbedderGraph* graph, |
||
1528 |
void* data) { |
||
1529 |
24 |
MemoryTracker tracker(isolate, graph); |
|
1530 |
24 |
Environment* env = static_cast<Environment*>(data); |
|
1531 |
24 |
tracker.Track(env); |
|
1532 |
24 |
env->ForEachBaseObject([&](BaseObject* obj) { |
|
1533 |
✓✓ | 513 |
if (obj->IsDoneInitializing()) |
1534 |
512 |
tracker.Track(obj); |
|
1535 |
513 |
}); |
|
1536 |
24 |
} |
|
1537 |
|||
1538 |
1 |
size_t Environment::NearHeapLimitCallback(void* data, |
|
1539 |
size_t current_heap_limit, |
||
1540 |
size_t initial_heap_limit) { |
||
1541 |
1 |
Environment* env = static_cast<Environment*>(data); |
|
1542 |
|||
1543 |
Debug(env, |
||
1544 |
DebugCategory::DIAGNOSTICS, |
||
1545 |
"Invoked NearHeapLimitCallback, processing=%d, " |
||
1546 |
"current_limit=%" PRIu64 ", " |
||
1547 |
"initial_limit=%" PRIu64 "\n", |
||
1548 |
1 |
env->is_processing_heap_limit_callback_, |
|
1549 |
2 |
static_cast<uint64_t>(current_heap_limit), |
|
1550 |
1 |
static_cast<uint64_t>(initial_heap_limit)); |
|
1551 |
|||
1552 |
1 |
size_t max_young_gen_size = env->isolate_data()->max_young_gen_size; |
|
1553 |
1 |
size_t young_gen_size = 0; |
|
1554 |
1 |
size_t old_gen_size = 0; |
|
1555 |
|||
1556 |
1 |
HeapSpaceStatistics stats; |
|
1557 |
1 |
size_t num_heap_spaces = env->isolate()->NumberOfHeapSpaces(); |
|
1558 |
✓✓ | 9 |
for (size_t i = 0; i < num_heap_spaces; ++i) { |
1559 |
8 |
env->isolate()->GetHeapSpaceStatistics(&stats, i); |
|
1560 |
✓✓✓✓ |
15 |
if (strcmp(stats.space_name(), "new_space") == 0 || |
1561 |
✓✓ | 7 |
strcmp(stats.space_name(), "new_large_object_space") == 0) { |
1562 |
2 |
young_gen_size += stats.space_used_size(); |
|
1563 |
} else { |
||
1564 |
6 |
old_gen_size += stats.space_used_size(); |
|
1565 |
} |
||
1566 |
} |
||
1567 |
|||
1568 |
Debug(env, |
||
1569 |
DebugCategory::DIAGNOSTICS, |
||
1570 |
"max_young_gen_size=%" PRIu64 ", " |
||
1571 |
"young_gen_size=%" PRIu64 ", " |
||
1572 |
"old_gen_size=%" PRIu64 ", " |
||
1573 |
"total_size=%" PRIu64 "\n", |
||
1574 |
2 |
static_cast<uint64_t>(max_young_gen_size), |
|
1575 |
2 |
static_cast<uint64_t>(young_gen_size), |
|
1576 |
2 |
static_cast<uint64_t>(old_gen_size), |
|
1577 |
1 |
static_cast<uint64_t>(young_gen_size + old_gen_size)); |
|
1578 |
|||
1579 |
1 |
uint64_t available = GuessMemoryAvailableToTheProcess(); |
|
1580 |
// TODO(joyeecheung): get a better estimate about the native memory |
||
1581 |
// usage into the overhead, e.g. based on the count of objects. |
||
1582 |
1 |
uint64_t estimated_overhead = max_young_gen_size; |
|
1583 |
Debug(env, |
||
1584 |
DebugCategory::DIAGNOSTICS, |
||
1585 |
"Estimated available memory=%" PRIu64 ", " |
||
1586 |
"estimated overhead=%" PRIu64 "\n", |
||
1587 |
2 |
static_cast<uint64_t>(available), |
|
1588 |
1 |
static_cast<uint64_t>(estimated_overhead)); |
|
1589 |
|||
1590 |
// This might be hit when the snapshot is being taken in another |
||
1591 |
// NearHeapLimitCallback invocation. |
||
1592 |
// When taking the snapshot, objects in the young generation may be |
||
1593 |
// promoted to the old generation, result in increased heap usage, |
||
1594 |
// but it should be no more than the young generation size. |
||
1595 |
// Ideally, this should be as small as possible - the heap limit |
||
1596 |
// can only be restored when the heap usage falls down below the |
||
1597 |
// new limit, so in a heap with unbounded growth the isolate |
||
1598 |
// may eventually crash with this new limit - effectively raising |
||
1599 |
// the heap limit to the new one. |
||
1600 |
✗✓ | 1 |
if (env->is_processing_heap_limit_callback_) { |
1601 |
size_t new_limit = current_heap_limit + max_young_gen_size; |
||
1602 |
Debug(env, |
||
1603 |
DebugCategory::DIAGNOSTICS, |
||
1604 |
"Not generating snapshots in nested callback. " |
||
1605 |
"new_limit=%" PRIu64 "\n", |
||
1606 |
static_cast<uint64_t>(new_limit)); |
||
1607 |
return new_limit; |
||
1608 |
} |
||
1609 |
|||
1610 |
// Estimate whether the snapshot is going to use up all the memory |
||
1611 |
// available to the process. If so, just give up to prevent the system |
||
1612 |
// from killing the process for a system OOM. |
||
1613 |
✗✓ | 1 |
if (estimated_overhead > available) { |
1614 |
Debug(env, |
||
1615 |
DebugCategory::DIAGNOSTICS, |
||
1616 |
"Not generating snapshots because it's too risky.\n"); |
||
1617 |
env->isolate()->RemoveNearHeapLimitCallback(NearHeapLimitCallback, |
||
1618 |
initial_heap_limit); |
||
1619 |
// The new limit must be higher than current_heap_limit or V8 might |
||
1620 |
// crash. |
||
1621 |
return current_heap_limit + 1; |
||
1622 |
} |
||
1623 |
|||
1624 |
// Take the snapshot synchronously. |
||
1625 |
1 |
env->is_processing_heap_limit_callback_ = true; |
|
1626 |
|||
1627 |
2 |
std::string dir = env->options()->diagnostic_dir; |
|
1628 |
✓✗ | 1 |
if (dir.empty()) { |
1629 |
1 |
dir = env->GetCwd(); |
|
1630 |
} |
||
1631 |
2 |
DiagnosticFilename name(env, "Heap", "heapsnapshot"); |
|
1632 |
1 |
std::string filename = dir + kPathSeparator + (*name); |
|
1633 |
|||
1634 |
1 |
Debug(env, DebugCategory::DIAGNOSTICS, "Start generating %s...\n", *name); |
|
1635 |
|||
1636 |
// Remove the callback first in case it's triggered when generating |
||
1637 |
// the snapshot. |
||
1638 |
1 |
env->isolate()->RemoveNearHeapLimitCallback(NearHeapLimitCallback, |
|
1639 |
initial_heap_limit); |
||
1640 |
|||
1641 |
1 |
heap::WriteSnapshot(env, filename.c_str()); |
|
1642 |
1 |
env->heap_limit_snapshot_taken_ += 1; |
|
1643 |
|||
1644 |
// Don't take more snapshots than the number specified by |
||
1645 |
// --heapsnapshot-near-heap-limit. |
||
1646 |
2 |
if (env->heap_limit_snapshot_taken_ < |
|
1647 |
✗✓ | 1 |
env->options_->heap_snapshot_near_heap_limit) { |
1648 |
env->isolate()->AddNearHeapLimitCallback(NearHeapLimitCallback, env); |
||
1649 |
} |
||
1650 |
|||
1651 |
1 |
FPrintF(stderr, "Wrote snapshot to %s\n", filename.c_str()); |
|
1652 |
// Tell V8 to reset the heap limit once the heap usage falls down to |
||
1653 |
// 95% of the initial limit. |
||
1654 |
1 |
env->isolate()->AutomaticallyRestoreInitialHeapLimit(0.95); |
|
1655 |
|||
1656 |
1 |
env->is_processing_heap_limit_callback_ = false; |
|
1657 |
|||
1658 |
// The new limit must be higher than current_heap_limit or V8 might |
||
1659 |
// crash. |
||
1660 |
1 |
return current_heap_limit + 1; |
|
1661 |
} |
||
1662 |
|||
1663 |
24 |
inline size_t Environment::SelfSize() const { |
|
1664 |
24 |
size_t size = sizeof(*this); |
|
1665 |
// Remove non pointer fields that will be tracked in MemoryInfo() |
||
1666 |
// TODO(joyeecheung): refactor the MemoryTracker interface so |
||
1667 |
// this can be done for common types within the Track* calls automatically |
||
1668 |
// if a certain scope is entered. |
||
1669 |
24 |
size -= sizeof(async_hooks_); |
|
1670 |
24 |
size -= sizeof(tick_info_); |
|
1671 |
24 |
size -= sizeof(immediate_info_); |
|
1672 |
24 |
return size; |
|
1673 |
} |
||
1674 |
|||
1675 |
24 |
void Environment::MemoryInfo(MemoryTracker* tracker) const { |
|
1676 |
// Iteratable STLs have their own sizes subtracted from the parent |
||
1677 |
// by default. |
||
1678 |
24 |
tracker->TrackField("isolate_data", isolate_data_); |
|
1679 |
24 |
tracker->TrackField("native_modules_with_cache", native_modules_with_cache); |
|
1680 |
24 |
tracker->TrackField("native_modules_without_cache", |
|
1681 |
24 |
native_modules_without_cache); |
|
1682 |
24 |
tracker->TrackField("destroy_async_id_list", destroy_async_id_list_); |
|
1683 |
24 |
tracker->TrackField("exec_argv", exec_argv_); |
|
1684 |
24 |
tracker->TrackField("should_abort_on_uncaught_toggle", |
|
1685 |
24 |
should_abort_on_uncaught_toggle_); |
|
1686 |
24 |
tracker->TrackField("stream_base_state", stream_base_state_); |
|
1687 |
24 |
tracker->TrackFieldWithSize( |
|
1688 |
24 |
"cleanup_hooks", cleanup_hooks_.size() * sizeof(CleanupHookCallback)); |
|
1689 |
24 |
tracker->TrackField("async_hooks", async_hooks_); |
|
1690 |
24 |
tracker->TrackField("immediate_info", immediate_info_); |
|
1691 |
24 |
tracker->TrackField("tick_info", tick_info_); |
|
1692 |
|||
1693 |
#define V(PropertyName, TypeName) \ |
||
1694 |
tracker->TrackField(#PropertyName, PropertyName()); |
||
1695 |
24 |
ENVIRONMENT_STRONG_PERSISTENT_VALUES(V) |
|
1696 |
#undef V |
||
1697 |
|||
1698 |
// FIXME(joyeecheung): track other fields in Environment. |
||
1699 |
// Currently MemoryTracker is unable to track these |
||
1700 |
// correctly: |
||
1701 |
// - Internal types that do not implement MemoryRetainer yet |
||
1702 |
// - STL containers with MemoryRetainer* inside |
||
1703 |
// - STL containers with numeric types inside that should not have their |
||
1704 |
// nodes elided e.g. numeric keys in maps. |
||
1705 |
// We also need to make sure that when we add a non-pointer field as its own |
||
1706 |
// node, we shift its sizeof() size out of the Environment node. |
||
1707 |
24 |
} |
|
1708 |
|||
1709 |
708897 |
void Environment::RunWeakRefCleanup() { |
|
1710 |
708897 |
isolate()->ClearKeptObjects(); |
|
1711 |
708897 |
} |
|
1712 |
|||
1713 |
// Not really any better place than env.cc at this moment. |
||
1714 |
140519 |
void BaseObject::DeleteMe(void* data) { |
|
1715 |
140519 |
BaseObject* self = static_cast<BaseObject*>(data); |
|
1716 |
✓✓✓✓ |
148445 |
if (self->has_pointer_data() && |
1717 |
✓✓ | 7926 |
self->pointer_data()->strong_ptr_count > 0) { |
1718 |
3443 |
return self->Detach(); |
|
1719 |
} |
||
1720 |
✓✗ | 137076 |
delete self; |
1721 |
} |
||
1722 |
|||
1723 |
456 |
bool BaseObject::IsDoneInitializing() const { return true; } |
|
1724 |
|||
1725 |
512 |
Local<Object> BaseObject::WrappedObject() const { |
|
1726 |
512 |
return object(); |
|
1727 |
} |
||
1728 |
|||
1729 |
1024 |
bool BaseObject::IsRootNode() const { |
|
1730 |
2048 |
return !persistent_handle_.IsWeak(); |
|
1731 |
} |
||
1732 |
|||
1733 |
56957 |
Local<FunctionTemplate> BaseObject::GetConstructorTemplate(Environment* env) { |
|
1734 |
56957 |
Local<FunctionTemplate> tmpl = env->base_object_ctor_template(); |
|
1735 |
✓✓ | 56957 |
if (tmpl.IsEmpty()) { |
1736 |
853 |
tmpl = env->NewFunctionTemplate(nullptr); |
|
1737 |
853 |
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "BaseObject")); |
|
1738 |
853 |
env->set_base_object_ctor_template(tmpl); |
|
1739 |
} |
||
1740 |
56957 |
return tmpl; |
|
1741 |
} |
||
1742 |
|||
1743 |
bool BaseObject::IsNotIndicativeOfMemoryLeakAtExit() const { |
||
1744 |
return IsWeakOrDetached(); |
||
1745 |
} |
||
1746 |
|||
1747 |
} // namespace node |
Generated by: GCOVR (Version 4.2) |