Skip to content

Commit 784713e

Browse files
committed
rt: Add a canary value to the end of every stack
Check it on upcall entry and exit, and on stack deletion
1 parent 4475ec8 commit 784713e

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/rt/rust_task.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
#endif
5050
#endif
5151

52+
// A value that goes at the end of the stack and must not be touched
53+
const uint8_t stack_canary[] = {0xAB, 0xCD, 0xAB, 0xCD,
54+
0xAB, 0xCD, 0xAB, 0xCD,
55+
0xAB, 0xCD, 0xAB, 0xCD,
56+
0xAB, 0xCD, 0xAB, 0xCD};
57+
5258
// Stack size
5359
size_t g_custom_min_stack_size = 0;
5460

@@ -95,7 +101,8 @@ config_valgrind_stack(stk_seg *stk) {
95101
// old stack segments, since the act of popping the stack previously
96102
// caused valgrind to consider the whole thing inaccessible.
97103
size_t sz = stk->end - (uintptr_t)&stk->data[0];
98-
VALGRIND_MAKE_MEM_UNDEFINED(stk->data, sz);
104+
VALGRIND_MAKE_MEM_UNDEFINED(stk->data + sizeof(stack_canary),
105+
sz - sizeof(stack_canary));
99106
#endif
100107
}
101108

@@ -110,6 +117,18 @@ free_stk(rust_task *task, stk_seg *stk) {
110117
task->free(stk);
111118
}
112119

120+
static void
121+
add_stack_canary(stk_seg *stk) {
122+
memcpy(stk->data, stack_canary, sizeof(stack_canary));
123+
assert(sizeof(stack_canary) == 16 && "Stack canary was not the expected size");
124+
}
125+
126+
static void
127+
check_stack_canary(stk_seg *stk) {
128+
assert(!memcmp(stk->data, stack_canary, sizeof(stack_canary))
129+
&& "Somebody killed the canary");
130+
}
131+
113132
static stk_seg*
114133
new_stk(rust_scheduler *sched, rust_task *task, size_t requested_sz)
115134
{
@@ -151,6 +170,7 @@ new_stk(rust_scheduler *sched, rust_task *task, size_t requested_sz)
151170
stk_seg *stk = (stk_seg *)task->malloc(sz, "stack");
152171
LOGPTR(task->sched, "new stk", (uintptr_t)stk);
153172
memset(stk, 0, sizeof(stk_seg));
173+
add_stack_canary(stk);
154174
stk->prev = NULL;
155175
stk->next = task->stk;
156176
stk->end = (uintptr_t) &stk->data[rust_stk_sz + RED_ZONE_SIZE];
@@ -165,6 +185,7 @@ static void
165185
del_stk(rust_task *task, stk_seg *stk)
166186
{
167187
assert(stk == task->stk && "Freeing stack segments out of order!");
188+
check_stack_canary(stk);
168189

169190
task->stk = stk->next;
170191

@@ -784,6 +805,11 @@ rust_task::on_rust_stack() {
784805
return sp_in_stk_seg(get_sp(), stk);
785806
}
786807

808+
void
809+
rust_task::check_stack_canary() {
810+
::check_stack_canary(stk);
811+
}
812+
787813
//
788814
// Local Variables:
789815
// mode: C++

src/rt/rust_task.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ rust_task : public kernel_owned<rust_task>, rust_cond
203203
void record_stack_limit();
204204
void reset_stack_limit();
205205
bool on_rust_stack();
206+
void check_stack_canary();
206207
};
207208

208209
//

src/rt/rust_upcall.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,21 @@ check_stack_alignment() __attribute__ ((aligned (16)));
2727
static void check_stack_alignment() { }
2828
#endif
2929

30+
static inline void
31+
do_sanity_check(rust_task *task) {
32+
check_stack_alignment();
33+
task->check_stack_canary();
34+
}
35+
3036
#define UPCALL_SWITCH_STACK(A, F) call_upcall_on_c_stack((void*)A, (void*)F)
3137

3238
inline void
3339
call_upcall_on_c_stack(void *args, void *fn_ptr) {
34-
check_stack_alignment();
3540
rust_task *task = rust_scheduler::get_task();
41+
do_sanity_check(task);
3642
rust_scheduler *sched = task->sched;
3743
sched->c_context.call_shim_on_c_stack(args, fn_ptr);
44+
do_sanity_check(task);
3845
}
3946

4047
extern "C" void record_sp(void *limit);
@@ -48,8 +55,8 @@ extern "C" void record_sp(void *limit);
4855
*/
4956
extern "C" CDECL void
5057
upcall_call_shim_on_c_stack(void *args, void *fn_ptr) {
51-
check_stack_alignment();
5258
rust_task *task = rust_scheduler::get_task();
59+
do_sanity_check(task);
5360

5461
// FIXME (1226) - The shim functions generated by rustc contain the
5562
// morestack prologue, so we need to let them know they have enough
@@ -65,6 +72,7 @@ upcall_call_shim_on_c_stack(void *args, void *fn_ptr) {
6572

6673
task = rust_scheduler::get_task();
6774
task->record_stack_limit();
75+
do_sanity_check(task);
6876
}
6977

7078
/**********************************************************************/
@@ -634,9 +642,10 @@ upcall_del_stack() {
634642
// needs to acquire the value of the stack pointer
635643
extern "C" CDECL void
636644
upcall_reset_stack_limit() {
637-
check_stack_alignment();
638645
rust_task *task = rust_scheduler::get_task();
646+
do_sanity_check(task);
639647
task->reset_stack_limit();
648+
do_sanity_check(task);
640649
}
641650

642651
//

0 commit comments

Comments
 (0)