Skip to content

Commit dfc894c

Browse files
thibaudmichaudV8 LUCI CQ
authored andcommitted
[wasm][sim][jspi] Fix inconsistent stack limit margin
The arm simulators have their own stack limit margin, Simulator::kAdditionalStackMargin, which differs from the one used by JSPI, StackMemory::kJSLimitOffsetKB. These two values where sometimes used inconsistently. Always use the former for simulator builds and the latter for native builds. [email protected] Fixed: 405522049 Change-Id: I5496f0d0cd6629dba6bfb9165635939915168f40 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/6388625 Commit-Queue: Thibaud Michaud <[email protected]> Reviewed-by: Jakob Kummerow <[email protected]> Cr-Commit-Position: refs/heads/main@{#99465}
1 parent 72e6a77 commit dfc894c

6 files changed

Lines changed: 22 additions & 10 deletions

File tree

src/execution/arm/simulator-arm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ class Simulator : public SimulatorBase {
246246
// Return central stack view, without additional safety margins.
247247
// Users, for example wasm::StackMemory, can add their own.
248248
base::Vector<uint8_t> GetCentralStackView() const;
249+
static constexpr int JSStackLimitMargin() { return kAdditionalStackMargin; }
249250

250251
void IterateRegistersAndStack(::heap::base::StackVisitor* visitor);
251252

src/execution/arm64/simulator-arm64.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,7 @@ class Simulator : public DecoderVisitor, public SimulatorBase {
766766
// Return central stack view, without additional safety margins.
767767
// Users, for example wasm::StackMemory, can add their own.
768768
base::Vector<uint8_t> GetCentralStackView() const;
769+
static constexpr int JSStackLimitMargin() { return kAdditionalStackMargin; }
769770

770771
void IterateRegistersAndStack(::heap::base::StackVisitor* visitor);
771772

src/execution/isolate.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,7 +2341,7 @@ Tagged<Object> Isolate::UnwindAndFindHandler() {
23412341
RetireWasmStack(old_continuation);
23422342
#if USE_SIMULATOR_BOOL && V8_TARGET_ARCH_ARM64
23432343
Simulator::current(this)->SetStackLimit(
2344-
reinterpret_cast<uintptr_t>(parent->jslimit()));
2344+
reinterpret_cast<uintptr_t>(parent->jmpbuf()->stack_limit));
23452345
#endif
23462346
continue;
23472347
}
@@ -3848,7 +3848,7 @@ void Isolate::SwitchStacks(Tagged<WasmContinuationObject> old_continuation) {
38483848
reinterpret_cast<wasm::StackMemory*>(old_continuation->stack());
38493849
if (v8_flags.trace_wasm_stack_switching) {
38503850
if (stack->jmpbuf()->state == wasm::JumpBuffer::Suspended) {
3851-
PrintF("Switch from stack %d to #%d (resume/start)\n", old_stack->id(),
3851+
PrintF("Switch from stack %d to %d (resume/start)\n", old_stack->id(),
38523852
stack->id());
38533853
} else if (stack->jmpbuf()->state == wasm::JumpBuffer::Inactive) {
38543854
PrintF("Switch from stack %d to %d (suspend/return)\n", old_stack->id(),

src/execution/simulator.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ class SimulatorStack : public v8::internal::AllStatic {
5656
}
5757

5858
#if V8_ENABLE_WEBASSEMBLY
59+
// Includes the safety stack limit gap.
5960
static inline base::Vector<uint8_t> GetCentralStackView(
6061
v8::internal::Isolate* isolate) {
6162
return Simulator::current(isolate)->GetCentralStackView();
6263
}
64+
65+
// Size of the safety stack limit gap.
66+
static int JSStackLimitMargin() { return Simulator::JSStackLimitMargin(); }
6367
#endif
6468

6569
// Iterates the simulator registers and stack for conservative stack scanning.
@@ -107,11 +111,14 @@ class SimulatorStack : public v8::internal::AllStatic {
107111
static inline base::Vector<uint8_t> GetCentralStackView(
108112
v8::internal::Isolate* isolate) {
109113
uintptr_t upper_bound = base::Stack::GetStackStart();
110-
size_t size =
111-
isolate->stack_size() + wasm::StackMemory::kJSLimitOffsetKB * KB;
114+
size_t size = isolate->stack_size() + JSStackLimitMargin();
112115
uintptr_t lower_bound = upper_bound - size;
113116
return base::VectorOf(reinterpret_cast<uint8_t*>(lower_bound), size);
114117
}
118+
119+
static constexpr int JSStackLimitMargin() {
120+
return wasm::StackMemory::kJSLimitOffsetKB * KB;
121+
}
115122
#endif
116123

117124
static void IterateRegistersAndStack(Isolate* isolate,

src/wasm/stacks.cc

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ StackMemory::~StackMemory() {
2828
}
2929
}
3030

31+
void* StackMemory::jslimit() const {
32+
return (active_segment_ ? active_segment_->limit_ : limit_) +
33+
SimulatorStack::JSStackLimitMargin();
34+
}
35+
3136
StackMemory::StackMemory() : owned_(true) {
3237
static std::atomic<int> next_id(1);
3338
id_ = next_id.fetch_add(1);
@@ -37,8 +42,9 @@ StackMemory::StackMemory() : owned_(true) {
3742
const size_t size_limit = v8_flags.stack_size;
3843
PageAllocator* allocator = GetPlatformPageAllocator();
3944
auto page_size = allocator->AllocatePageSize();
40-
size_t initial_size =
41-
std::min<size_t>(size_limit, kJsStackSizeKB + kJSLimitOffsetKB) * KB;
45+
size_t initial_size = std::min<size_t>(
46+
size_limit * KB,
47+
kJsStackSizeKB * KB + SimulatorStack::JSStackLimitMargin());
4248
first_segment_ =
4349
new StackSegment(RoundUp(initial_size, page_size) / page_size);
4450
active_segment_ = first_segment_;

src/wasm/stacks.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ class StackMemory {
7171
static StackMemory* GetCentralStackView(Isolate* isolate);
7272

7373
~StackMemory();
74-
void* jslimit() const {
75-
return (active_segment_ ? active_segment_->limit_ : limit_) +
76-
kJSLimitOffsetKB * KB;
77-
}
74+
void* jslimit() const;
7875
Address base() const {
7976
Address memory_limit = active_segment_
8077
? active_segment_->base()

0 commit comments

Comments
 (0)