forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack-guard.cc
More file actions
404 lines (349 loc) Β· 13.7 KB
/
stack-guard.cc
File metadata and controls
404 lines (349 loc) Β· 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/execution/stack-guard.h"
#include "src/base/atomicops.h"
#include "src/compiler-dispatcher/optimizing-compile-dispatcher.h"
#include "src/execution/interrupts-scope.h"
#include "src/execution/isolate.h"
#include "src/execution/protectors-inl.h"
#include "src/execution/simulator.h"
#include "src/logging/counters.h"
#include "src/objects/backing-store.h"
#include "src/roots/roots-inl.h"
#include "src/tracing/trace-event.h"
#include "src/utils/memcopy.h"
#ifdef V8_ENABLE_SPARKPLUG
#include "src/baseline/baseline-batch-compiler.h"
#endif
#ifdef V8_ENABLE_MAGLEV
#include "src/maglev/maglev-concurrent-dispatcher.h"
#endif // V8_ENABLE_MAGLEV
#if V8_ENABLE_WEBASSEMBLY
#include "src/wasm/wasm-engine.h"
#endif // V8_ENABLE_WEBASSEMBLY
namespace v8 {
namespace internal {
void StackGuard::update_interrupt_requests_and_stack_limits(
const ExecutionAccess& lock) {
DCHECK_NOT_NULL(isolate_);
if (has_pending_interrupts(lock)) {
thread_local_.set_jslimit(kInterruptLimit);
#ifdef USE_SIMULATOR
thread_local_.set_climit(kInterruptLimit);
#endif
} else {
thread_local_.set_jslimit(thread_local_.real_jslimit_);
#ifdef USE_SIMULATOR
thread_local_.set_climit(thread_local_.real_climit_);
#endif
}
for (InterruptLevel level :
std::array{InterruptLevel::kNoGC, InterruptLevel::kNoHeapWrites,
InterruptLevel::kAnyEffect}) {
thread_local_.set_interrupt_requested(
level, InterruptLevelMask(level) & thread_local_.interrupt_flags_);
}
}
void StackGuard::SetStackLimit(uintptr_t limit) {
ExecutionAccess access(isolate_);
SetStackLimitInternal(access, limit,
SimulatorStack::JsLimitFromCLimit(isolate_, limit));
}
void StackGuard::SetStackLimitInternal(const ExecutionAccess& lock,
uintptr_t limit, uintptr_t jslimit) {
// If the current limits are special (e.g. due to a pending interrupt) then
// leave them alone.
if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
thread_local_.set_jslimit(jslimit);
}
thread_local_.real_jslimit_ = jslimit;
#ifdef USE_SIMULATOR
if (thread_local_.climit() == thread_local_.real_climit_) {
thread_local_.set_climit(limit);
}
thread_local_.real_climit_ = limit;
#endif
}
void StackGuard::SetStackLimitForStackSwitching(uintptr_t limit) {
// Try to compare and swap the new jslimit without the ExecutionAccess lock.
uintptr_t old_jslimit = base::Relaxed_CompareAndSwap(
&thread_local_.jslimit_, thread_local_.real_jslimit_, limit);
USE(old_jslimit);
DCHECK_IMPLIES(old_jslimit != thread_local_.real_jslimit_,
old_jslimit == kInterruptLimit);
// Either way, set the real limit. This does not require synchronization.
thread_local_.real_jslimit_ = limit;
}
#ifdef USE_SIMULATOR
void StackGuard::AdjustStackLimitForSimulator() {
ExecutionAccess access(isolate_);
uintptr_t climit = thread_local_.real_climit_;
// If the current limits are special (e.g. due to a pending interrupt) then
// leave them alone.
uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, climit);
if (thread_local_.jslimit() == thread_local_.real_jslimit_) {
thread_local_.set_jslimit(jslimit);
}
}
void StackGuard::ResetStackLimitForSimulator() {
ExecutionAccess access(isolate_);
// If the current limits are special due to a pending interrupt then
// leave them alone.
if (thread_local_.jslimit() != kInterruptLimit) {
thread_local_.set_jslimit(thread_local_.real_jslimit_);
}
}
#endif
void StackGuard::PushInterruptsScope(InterruptsScope* scope) {
ExecutionAccess access(isolate_);
DCHECK_NE(scope->mode_, InterruptsScope::kNoop);
if (scope->mode_ == InterruptsScope::kPostponeInterrupts) {
// Intercept already requested interrupts.
uint32_t intercepted =
thread_local_.interrupt_flags_ & scope->intercept_mask_;
scope->intercepted_flags_ = intercepted;
thread_local_.interrupt_flags_ &= ~intercepted;
} else {
DCHECK_EQ(scope->mode_, InterruptsScope::kRunInterrupts);
// Restore postponed interrupts.
uint32_t restored_flags = 0;
for (InterruptsScope* current = thread_local_.interrupt_scopes_;
current != nullptr; current = current->prev_) {
restored_flags |= (current->intercepted_flags_ & scope->intercept_mask_);
current->intercepted_flags_ &= ~scope->intercept_mask_;
}
thread_local_.interrupt_flags_ |= restored_flags;
}
update_interrupt_requests_and_stack_limits(access);
// Add scope to the chain.
scope->prev_ = thread_local_.interrupt_scopes_;
thread_local_.interrupt_scopes_ = scope;
}
void StackGuard::PopInterruptsScope() {
ExecutionAccess access(isolate_);
InterruptsScope* top = thread_local_.interrupt_scopes_;
DCHECK_NE(top->mode_, InterruptsScope::kNoop);
if (top->mode_ == InterruptsScope::kPostponeInterrupts) {
// Make intercepted interrupts active.
DCHECK_EQ(thread_local_.interrupt_flags_ & top->intercept_mask_, 0);
thread_local_.interrupt_flags_ |= top->intercepted_flags_;
} else {
DCHECK_EQ(top->mode_, InterruptsScope::kRunInterrupts);
// Postpone existing interupts if needed.
if (top->prev_) {
for (uint32_t interrupt = 1; interrupt < ALL_INTERRUPTS;
interrupt = interrupt << 1) {
InterruptFlag flag = static_cast<InterruptFlag>(interrupt);
if ((thread_local_.interrupt_flags_ & flag) &&
top->prev_->Intercept(flag)) {
thread_local_.interrupt_flags_ &= ~flag;
}
}
}
}
update_interrupt_requests_and_stack_limits(access);
// Remove scope from chain.
thread_local_.interrupt_scopes_ = top->prev_;
}
bool StackGuard::CheckInterrupt(InterruptFlag flag) {
ExecutionAccess access(isolate_);
return (thread_local_.interrupt_flags_ & flag) != 0;
}
void StackGuard::RequestInterrupt(InterruptFlag flag) {
ExecutionAccess access(isolate_);
// Check the chain of InterruptsScope for interception.
if (thread_local_.interrupt_scopes_ &&
thread_local_.interrupt_scopes_->Intercept(flag)) {
return;
}
// Not intercepted. Set as active interrupt flag.
thread_local_.interrupt_flags_ |= flag;
update_interrupt_requests_and_stack_limits(access);
// If this isolate is waiting in a futex, notify it to wake up.
isolate_->futex_wait_list_node()->NotifyWake();
}
void StackGuard::ClearInterrupt(InterruptFlag flag) {
ExecutionAccess access(isolate_);
// Clear the interrupt flag from the chain of InterruptsScope.
for (InterruptsScope* current = thread_local_.interrupt_scopes_;
current != nullptr; current = current->prev_) {
current->intercepted_flags_ &= ~flag;
}
// Clear the interrupt flag from the active interrupt flags.
thread_local_.interrupt_flags_ &= ~flag;
update_interrupt_requests_and_stack_limits(access);
}
bool StackGuard::HasTerminationRequest() {
if (!thread_local_.has_interrupt_requested(InterruptLevel::kNoGC)) {
return false;
}
ExecutionAccess access(isolate_);
if ((thread_local_.interrupt_flags_ & TERMINATE_EXECUTION) != 0) {
thread_local_.interrupt_flags_ &= ~TERMINATE_EXECUTION;
update_interrupt_requests_and_stack_limits(access);
return true;
}
return false;
}
int StackGuard::FetchAndClearInterrupts(InterruptLevel level) {
ExecutionAccess access(isolate_);
InterruptFlag mask = InterruptLevelMask(level);
if ((thread_local_.interrupt_flags_ & TERMINATE_EXECUTION) != 0) {
// The TERMINATE_EXECUTION interrupt is special, since it terminates
// execution but should leave V8 in a resumable state. If it exists, we only
// fetch and clear that bit. On resume, V8 can continue processing other
// interrupts.
mask = TERMINATE_EXECUTION;
}
int result = static_cast<int>(thread_local_.interrupt_flags_ & mask);
thread_local_.interrupt_flags_ &= ~mask;
update_interrupt_requests_and_stack_limits(access);
return result;
}
char* StackGuard::ArchiveStackGuard(char* to) {
ExecutionAccess access(isolate_);
MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal));
thread_local_ = {};
return to + sizeof(ThreadLocal);
}
char* StackGuard::RestoreStackGuard(char* from) {
ExecutionAccess access(isolate_);
MemCopy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal));
return from + sizeof(ThreadLocal);
}
void StackGuard::FreeThreadResources() {
Isolate::PerIsolateThreadData* per_thread =
isolate_->FindOrAllocatePerThreadDataForThisThread();
per_thread->set_stack_limit(real_climit());
}
void StackGuard::ThreadLocal::Initialize(Isolate* isolate,
const ExecutionAccess& lock) {
const uintptr_t kLimitSize = v8_flags.stack_size * KB;
DCHECK_GT(base::Stack::GetStackStart(), kLimitSize);
uintptr_t limit = base::Stack::GetStackStart() - kLimitSize;
real_jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit);
set_jslimit(SimulatorStack::JsLimitFromCLimit(isolate, limit));
#ifdef USE_SIMULATOR
real_climit_ = limit;
set_climit(limit);
#endif
interrupt_scopes_ = nullptr;
interrupt_flags_ = 0;
}
void StackGuard::InitThread(const ExecutionAccess& lock) {
thread_local_.Initialize(isolate_, lock);
Isolate::PerIsolateThreadData* per_thread =
isolate_->FindOrAllocatePerThreadDataForThisThread();
uintptr_t stored_limit = per_thread->stack_limit();
// You should hold the ExecutionAccess lock when you call this.
if (stored_limit != 0) {
SetStackLimit(stored_limit);
}
}
// --- C a l l s t o n a t i v e s ---
namespace {
bool TestAndClear(int* bitfield, int mask) {
bool result = (*bitfield & mask);
*bitfield &= ~mask;
return result;
}
class V8_NODISCARD ShouldBeZeroOnReturnScope final {
public:
#ifndef DEBUG
explicit ShouldBeZeroOnReturnScope(int*) {}
#else // DEBUG
explicit ShouldBeZeroOnReturnScope(int* v) : v_(v) {}
~ShouldBeZeroOnReturnScope() { DCHECK_EQ(*v_, 0); }
private:
int* v_;
#endif // DEBUG
};
} // namespace
Tagged<Object> StackGuard::HandleInterrupts(InterruptLevel level) {
TRACE_EVENT0("v8.execute", "V8.HandleInterrupts");
#if DEBUG
isolate_->heap()->VerifyNewSpaceTop();
#endif
if (v8_flags.verify_predictable) {
// Advance synthetic time by making a time request.
isolate_->heap()->MonotonicallyIncreasingTimeInMs();
}
// Fetch and clear interrupt bits in one go. See comments inside the method
// for special handling of TERMINATE_EXECUTION.
int interrupt_flags = FetchAndClearInterrupts(level);
// All interrupts should be fully processed when returning from this method.
ShouldBeZeroOnReturnScope should_be_zero_on_return(&interrupt_flags);
if (TestAndClear(&interrupt_flags, TERMINATE_EXECUTION)) {
TRACE_EVENT0("v8.execute", "V8.TerminateExecution");
return isolate_->TerminateExecution();
}
if (TestAndClear(&interrupt_flags, GC_REQUEST)) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.gc"), "V8.GCHandleGCRequest");
isolate_->heap()->HandleGCRequest();
}
if (TestAndClear(&interrupt_flags, START_INCREMENTAL_MARKING)) {
isolate_->heap()->StartIncrementalMarkingOnInterrupt();
}
if (TestAndClear(&interrupt_flags, GLOBAL_SAFEPOINT)) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.gc"), "V8.GlobalSafepoint");
isolate_->main_thread_local_heap()->Safepoint();
}
#if V8_ENABLE_WEBASSEMBLY
if (TestAndClear(&interrupt_flags, GROW_SHARED_MEMORY)) {
TRACE_EVENT0("v8.wasm", "V8.WasmGrowSharedMemory");
BackingStore::UpdateSharedWasmMemoryObjects(isolate_);
}
if (TestAndClear(&interrupt_flags, LOG_WASM_CODE)) {
TRACE_EVENT0("v8.wasm", "V8.LogCode");
wasm::GetWasmEngine()->LogOutstandingCodesForIsolate(isolate_);
}
if (TestAndClear(&interrupt_flags, WASM_CODE_GC)) {
TRACE_EVENT0("v8.wasm", "V8.WasmCodeGC");
wasm::GetWasmEngine()->ReportLiveCodeFromStackForGC(isolate_);
}
#endif // V8_ENABLE_WEBASSEMBLY
if (TestAndClear(&interrupt_flags, DEOPT_MARKED_ALLOCATION_SITES)) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.gc"),
"V8.GCDeoptMarkedAllocationSites");
isolate_->heap()->DeoptMarkedAllocationSites();
}
if (TestAndClear(&interrupt_flags, INSTALL_CODE)) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),
"V8.InstallOptimizedFunctions");
DCHECK(isolate_->concurrent_recompilation_enabled());
isolate_->optimizing_compile_dispatcher()->InstallOptimizedFunctions();
}
#ifdef V8_ENABLE_SPARKPLUG
if (TestAndClear(&interrupt_flags, INSTALL_BASELINE_CODE)) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),
"V8.FinalizeBaselineConcurrentCompilation");
isolate_->baseline_batch_compiler()->InstallBatch();
}
#endif // V8_ENABLE_SPARKPLUG
#ifdef V8_ENABLE_MAGLEV
if (TestAndClear(&interrupt_flags, INSTALL_MAGLEV_CODE)) {
TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("v8.compile"),
"V8.FinalizeMaglevConcurrentCompilation");
isolate_->maglev_concurrent_dispatcher()->FinalizeFinishedJobs();
}
#endif // V8_ENABLE_MAGLEV
if (TestAndClear(&interrupt_flags, API_INTERRUPT)) {
TRACE_EVENT0("v8.execute", "V8.InvokeApiInterruptCallbacks");
// Callbacks must be invoked outside of ExecutionAccess lock.
isolate_->InvokeApiInterruptCallbacks();
}
#ifdef V8_RUNTIME_CALL_STATS
// Runtime call stats can be enabled at any via Chrome tracing and since
// there's no global list of active Isolates this seems to be the only
// simple way to invalidate the protector.
if (TracingFlags::is_runtime_stats_enabled() &&
Protectors::IsNoProfilingIntact(isolate_)) {
Protectors::InvalidateNoProfiling(isolate_);
}
#endif
isolate_->counters()->stack_interrupts()->Increment();
return ReadOnlyRoots(isolate_).undefined_value();
}
} // namespace internal
} // namespace v8