Skip to content
This repository was archived by the owner on Mar 24, 2022. It is now read-only.

Commit 9389a4c

Browse files
committed
[lucet-runtime-internals] terminate the stack correctly
This places the pointers to the parent and child contexts _above_ the two zero words used to terminate the call stack. This is the location where memory arguments are expected, which isn't completely relevant since `lucet_context_backstop` is written in assembly, but it at least prevents gdb and other unwinding tools from mistaking the pointers for return addresses.
1 parent bc8aaef commit 9389a4c

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

lucet-runtime/lucet-runtime-internals/src/context/context_asm.S

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ _lucet_context_bootstrap:
6262
.align 16
6363
lucet_context_backstop:
6464
_lucet_context_backstop:
65-
mov -16(%rbp), %rdi /* parent context to arg 1 */
66-
mov -8(%rbp), %rsi /* own context to arg 2 */
65+
mov 16(%rbp), %rdi /* parent context to arg 1 */
66+
mov 24(%rbp), %rsi /* own context to arg 2 */
6767
mov %rax, (8*8 + 8*16 + 8*0)(%rdi) /* store return values before swapping back -- offset is offsetof(struct lucet_context, retvals) */
6868
mov %rdx, (8*8 + 8*16 + 8*1)(%rdi)
6969
movdqu %xmm0, (8*8 + 8*16 + 8*2)(%rdi) /* floating-point return value */

lucet-runtime/lucet-runtime-internals/src/context/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,22 +348,23 @@ impl Context {
348348
// the guest function returns into lucet_context_backstop.
349349
stack[sp + 2 - stack_start] = lucet_context_backstop as u64;
350350

351+
// Terminate the call chain.
352+
stack[sp - 4] = 0;
353+
stack[sp - 3] = 0;
354+
351355
// if fptr ever returns, it returns to the backstop func. backstop needs two arguments in
352356
// its frame - first the context we are switching *out of* (which is also the one we are
353357
// creating right now) and the ctx we switch back into. Note *parent might not be a valid
354358
// ctx now, but it should be when this ctx is started.
355-
stack[sp - 4] = child as *mut Context as u64;
356-
stack[sp - 3] = parent as *mut Context as u64;
357-
// Terminate the call chain.
358-
stack[sp - 2] = 0;
359-
stack[sp - 1] = 0;
359+
stack[sp - 2] = child as *mut Context as u64;
360+
stack[sp - 1] = parent as *mut Context as u64;
360361

361362
// RSP, RBP, and sigset still remain to be initialized.
362363
// Stack pointer: this has the return address of the first function to be run on the swap.
363364
child.gpr.rsp = &mut stack[sp - stack_start] as *mut u64 as u64;
364365
// Frame pointer: this is only used by the backstop code. It uses it to locate the ctx and
365366
// parent arguments set above.
366-
child.gpr.rbp = &mut stack[sp - 2] as *mut u64 as u64;
367+
child.gpr.rbp = &mut stack[sp - 4] as *mut u64 as u64;
367368

368369
// Read the sigprocmask to be restored if we ever need to jump out of a signal handler. If
369370
// this isn't possible, die.

0 commit comments

Comments
 (0)