Skip to content

Commit 121c420

Browse files
committed
rt: Insert stack alignment checks into upcalls
1 parent 4f826b3 commit 121c420

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/rt/arch/i386/record_sp.S

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
#if defined(__APPLE__) || defined(_WIN32)
44
#define RECORD_SP _record_sp
55
#define GET_SP _get_sp
6+
#define CHECK_STACK _check_stack_alignment
67
#else
78
#define RECORD_SP record_sp
89
#define GET_SP get_sp
10+
#define CHECK_STACK check_stack_alignment
911
#endif
1012

1113
.globl RECORD_SP
1214
.globl GET_SP
15+
.globl CHECK_STACK
1316

1417
#if defined(__linux__)
1518
RECORD_SP:
@@ -35,4 +38,11 @@ RECORD_SP:
3538

3639
GET_SP:
3740
movl %esp, %eax
38-
ret
41+
ret
42+
43+
// This will segfault if not called on a 16-byte boundary
44+
CHECK_STACK:
45+
subl $28, %esp
46+
movaps %xmm0, (%esp)
47+
addl $28, %esp
48+
ret

src/rt/arch/x86_64/record_sp.S

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
#if defined(__APPLE__) || defined(_WIN32)
44
#define RECORD_SP _record_sp
55
#define GET_SP _get_sp
6+
#define CHECK_STACK _check_stack_alignment
67
#else
78
#define RECORD_SP record_sp
89
#define GET_SP get_sp
10+
#define CHECK_STACK check_stack_alignment
911
#endif
1012

1113
.globl RECORD_SP
1214
.globl GET_SP
15+
.globl CHECK_STACK
1316

1417
#if defined(__linux__)
1518
RECORD_SP:
@@ -30,3 +33,10 @@ RECORD_SP:
3033
GET_SP:
3134
movq %rsp, %rax
3235
ret
36+
37+
// This will segfault if not called on a 16-byte boundary
38+
CHECK_STACK:
39+
subq $24, %rsp
40+
movaps %xmm0, (%rsp)
41+
addq $24, %rsp
42+
ret

src/rt/rust_upcall.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@
1414
#include "rust_upcall.h"
1515
#include <stdint.h>
1616

17+
18+
// This is called to ensure we've set up our rust stacks
19+
// correctly. Strategically placed at entry to upcalls because they begin on
20+
// the rust stack and happen frequently enough to catch most stack changes,
21+
// including at the beginning of all landing pads.
22+
extern "C" void
23+
check_stack_alignment() __attribute__ ((aligned (16)));
24+
1725
#define SWITCH_STACK(A, F) upcall_call_shim_on_c_stack((void*)A, (void*)F)
1826

1927
extern "C" void record_sp(void *limit);
@@ -26,6 +34,7 @@ extern "C" void record_sp(void *limit);
2634
*/
2735
extern "C" CDECL void
2836
upcall_call_shim_on_c_stack(void *args, void *fn_ptr) {
37+
check_stack_alignment();
2938
rust_task *task = rust_scheduler::get_task();
3039

3140
// FIXME (1226) - The shim functions generated by rustc contain the
@@ -594,6 +603,7 @@ upcall_del_stack() {
594603
// needs to acquire the value of the stack pointer
595604
extern "C" CDECL void
596605
upcall_reset_stack_limit() {
606+
check_stack_alignment();
597607
rust_task *task = rust_scheduler::get_task();
598608
task->reset_stack_limit();
599609
}

0 commit comments

Comments
 (0)