Skip to content

Commit 2a44627

Browse files
rmacnak-googleCommit Queue
authored andcommitted
[vm] Add missing save of shadow call stack in DartEntryScope.
Thread::saved_shadow_call_stack_ has the value for the top-most Dart entry, and needs to be saved and restored for nested Dart entries. TEST=fuchsia Change-Id: Id035bec7e798458fdfcf85678a7246d5f7eef850 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/464603 Commit-Queue: Ryan Macnak <[email protected]> Reviewed-by: Alexander Aprelev <[email protected]>
1 parent a27682c commit 2a44627

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed

runtime/vm/dart_entry.cc

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,11 @@ class DartEntryScope : public TransitionToGenerated {
5252
#if defined(USING_SAFE_STACK)
5353
// Remember the safestack pointer at entry so it can be restored in
5454
// Exceptions::JumpToFrame when a Dart exception jumps over C++ frames.
55-
saved_safestack_limit_ = OSThread::GetCurrentSafestackPointer();
56-
thread->set_saved_safestack_limit(saved_safestack_limit_);
55+
saved_safestack_ = thread->saved_safestack();
56+
thread->set_saved_safestack(OSThread::GetCurrentSafestackPointer());
57+
#endif
58+
#if defined(USING_SHADOW_CALL_STACK)
59+
saved_shadow_call_stack_ = thread->saved_shadow_call_stack();
5760
#endif
5861

5962
saved_api_scope_ = thread->api_top_scope();
@@ -67,8 +70,11 @@ class DartEntryScope : public TransitionToGenerated {
6770
thread()->ExitApiScope();
6871
}
6972

73+
#if defined(USING_SHADOW_CALL_STACK)
74+
thread()->set_saved_shadow_call_stack(saved_shadow_call_stack_);
75+
#endif
7076
#if defined(USING_SAFE_STACK)
71-
thread()->set_saved_safestack_limit(saved_safestack_limit_);
77+
thread()->set_saved_safestack(saved_safestack_);
7278
#endif
7379

7480
ASSERT(thread()->long_jump_base() == nullptr);
@@ -78,7 +84,10 @@ class DartEntryScope : public TransitionToGenerated {
7884
private:
7985
LongJumpScope* saved_long_jump_base_;
8086
#if defined(USING_SAFE_STACK)
81-
uword saved_safestack_limit_ = 0;
87+
uword saved_safestack_ = 0;
88+
#endif
89+
#if defined(USING_SHADOW_CALL_STACK)
90+
uword saved_shadow_call_stack_ = 0;
8291
#endif
8392
ApiLocalScope* saved_api_scope_;
8493
};

runtime/vm/exceptions.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,7 @@ NO_SANITIZE_SAFE_STACK // This function manipulates the safestack pointer.
650650
// We are jumping over C++ frames, so we have to set the safestack pointer
651651
// back to what it was when we entered the runtime from Dart code.
652652
#if defined(USING_SAFE_STACK)
653-
const uword saved_ssp = thread->saved_safestack_limit();
653+
const uword saved_ssp = thread->saved_safestack();
654654
OSThread::SetCurrentSafestackPointer(saved_ssp);
655655
#endif
656656

runtime/vm/os_thread_fuchsia.cc

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void OSThread::Join(ThreadJoinId id) {
153153
}
154154

155155
void OSThread::Detach(ThreadJoinId id) {
156-
int result = pthread_detach(id);
156+
int result = pthread_detach(id); // NOLINT
157157
VALIDATE_PTHREAD_RESULT(result);
158158
}
159159

@@ -202,6 +202,10 @@ uword OSThread::GetCurrentSafestackPointer() {
202202
asm volatile("mrs %0, TPIDR_EL0;\n" _loadword(ZX_TLS_UNSAFE_SP_OFFSET)
203203
: "=r"(result) // outputs
204204
);
205+
#elif defined(HOST_ARCH_RISCV64)
206+
asm volatile("ld %0, " STRINGIFY(ZX_TLS_UNSAFE_SP_OFFSET) "(tp)"
207+
: "=r"(result) // outputs
208+
);
205209
#else
206210
#error "Architecture not supported"
207211
#endif
@@ -229,6 +233,12 @@ void OSThread::SetCurrentSafestackPointer(uword ssp) {
229233
: "r"(ssp) // inputs.
230234
: // clobbered.
231235
);
236+
#elif defined(HOST_ARCH_RISCV64)
237+
asm volatile("sd %0, " STRINGIFY(ZX_TLS_UNSAFE_SP_OFFSET) "(tp)"
238+
: // outputs.
239+
: "r"(ssp) // inputs.
240+
: // clobbered.
241+
);
232242
#else
233243
#error "Architecture not supported"
234244
#endif

runtime/vm/thread.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -425,29 +425,37 @@ class Thread : public ThreadState, public IntrusiveDListEntry<Thread> {
425425
void SetStackLimit(uword value);
426426
void ClearStackLimit();
427427

428-
// Access to the current stack limit for generated code. Either the true OS
429-
// thread's stack limit minus some headroom, or a special value to trigger
430-
// interrupts.
428+
// The stack limit used by stack overflow checks in generated code. Either the
429+
// true OS thread's stack limit minus some headroom, or a special value to
430+
// trigger interrupts.
431431
uword stack_limit_address() const {
432432
return reinterpret_cast<uword>(&stack_limit_);
433433
}
434434
static intptr_t stack_limit_offset() {
435435
return OFFSET_OF(Thread, stack_limit_);
436436
}
437437

438-
// The true stack limit for this OS thread.
438+
// The true stack limit for this OS thread minus some headroom. Used on ARM64
439+
// to keep CSP/R31 signal-handler safe while Dart uses R15 as its stack
440+
// pointer.
439441
static intptr_t saved_stack_limit_offset() {
440442
return OFFSET_OF(Thread, saved_stack_limit_);
441443
}
442444
uword saved_stack_limit() const { return saved_stack_limit_; }
443445

444446
#if defined(USING_SAFE_STACK)
445-
uword saved_safestack_limit() const { return saved_safestack_limit_; }
446-
void set_saved_safestack_limit(uword limit) {
447-
saved_safestack_limit_ = limit;
448-
}
447+
// The SafeStack pointer during the top-most DartEntry. Needs to be restored
448+
// on Dart throw like longjmp would.
449+
uword saved_safestack() const { return saved_safestack_; }
450+
void set_saved_safestack(uword ssp) { saved_safestack_ = ssp; }
449451
#endif
452+
453+
// The ShadowCallStack pointer during the top-most entry frame. Needs to be
454+
// restored on Dart throw like longjmp would.
450455
uword saved_shadow_call_stack() const { return saved_shadow_call_stack_; }
456+
void set_saved_shadow_call_stack(uword ssp) {
457+
saved_shadow_call_stack_ = ssp;
458+
}
451459
static uword saved_shadow_call_stack_offset() {
452460
return OFFSET_OF(Thread, saved_shadow_call_stack_);
453461
}
@@ -1634,7 +1642,7 @@ class Thread : public ThreadState, public IntrusiveDListEntry<Thread> {
16341642
}
16351643

16361644
#if defined(USING_SAFE_STACK)
1637-
uword saved_safestack_limit_ = 0;
1645+
uword saved_safestack_ = 0;
16381646
#endif
16391647

16401648
Thread* next_; // Used to chain the thread structures in an isolate.

0 commit comments

Comments
 (0)