Skip to content

Commit e0d5b92

Browse files
committed
rt: Begin moving stack-building functions to rust_stack.cpp
1 parent 2983e77 commit e0d5b92

File tree

5 files changed

+68
-53
lines changed

5 files changed

+68
-53
lines changed

mk/rt.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ RUNTIME_CS_$(1) := \
4545
rt/rust_task_thread.cpp \
4646
rt/rust_scheduler.cpp \
4747
rt/rust_task.cpp \
48+
rt/rust_stack.cpp \
4849
rt/rust_task_list.cpp \
4950
rt/rust_port.cpp \
5051
rt/rust_upcall.cpp \
@@ -84,6 +85,7 @@ RUNTIME_HDR_$(1) := rt/globals.h \
8485
rt/rust_scheduler.h \
8586
rt/rust_shape.h \
8687
rt/rust_task.h \
88+
rt/rust_stack.h \
8789
rt/rust_task_list.h \
8890
rt/rust_log.h \
8991
rt/circular_buffer.h \

src/rt/rust_stack.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "rust_internal.h"
2+
3+
#include "vg/valgrind.h"
4+
#include "vg/memcheck.h"
5+
6+
// A value that goes at the end of the stack and must not be touched
7+
const uint8_t stack_canary[] = {0xAB, 0xCD, 0xAB, 0xCD,
8+
0xAB, 0xCD, 0xAB, 0xCD,
9+
0xAB, 0xCD, 0xAB, 0xCD,
10+
0xAB, 0xCD, 0xAB, 0xCD};
11+
12+
void
13+
config_valgrind_stack(stk_seg *stk) {
14+
stk->valgrind_id =
15+
VALGRIND_STACK_REGISTER(&stk->data[0],
16+
stk->end);
17+
#ifndef NVALGRIND
18+
// Establish that the stack is accessible. This must be done when reusing
19+
// old stack segments, since the act of popping the stack previously
20+
// caused valgrind to consider the whole thing inaccessible.
21+
size_t sz = stk->end - (uintptr_t)&stk->data[0];
22+
VALGRIND_MAKE_MEM_UNDEFINED(stk->data + sizeof(stack_canary),
23+
sz - sizeof(stack_canary));
24+
#endif
25+
}
26+
27+
void
28+
unconfig_valgrind_stack(stk_seg *stk) {
29+
VALGRIND_STACK_DEREGISTER(stk->valgrind_id);
30+
}
31+
32+
void
33+
add_stack_canary(stk_seg *stk) {
34+
memcpy(stk->data, stack_canary, sizeof(stack_canary));
35+
assert(sizeof(stack_canary) == 16 && "Stack canary was not the expected size");
36+
}
37+
38+
void
39+
check_stack_canary(stk_seg *stk) {
40+
assert(!memcmp(stk->data, stack_canary, sizeof(stack_canary))
41+
&& "Somebody killed the canary");
42+
}

src/rt/rust_stack.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
struct stk_seg {
2+
stk_seg *prev;
3+
stk_seg *next;
4+
uintptr_t end;
5+
unsigned int valgrind_id;
6+
#ifndef _LP64
7+
uint32_t pad;
8+
#endif
9+
10+
uint8_t data[];
11+
};
12+
13+
void
14+
config_valgrind_stack(stk_seg *stk);
15+
16+
void
17+
unconfig_valgrind_stack(stk_seg *stk);
18+
19+
void
20+
add_stack_canary(stk_seg *stk);
21+
22+
void
23+
check_stack_canary(stk_seg *stk);

src/rt/rust_task.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
#include "rust_internal.h"
33
#include "rust_cc.h"
44

5-
#include "vg/valgrind.h"
6-
#include "vg/memcheck.h"
7-
85
#ifndef __WIN32__
96
#include <execinfo.h>
107
#endif
@@ -60,12 +57,6 @@
6057
#endif
6158
#endif
6259

63-
// A value that goes at the end of the stack and must not be touched
64-
const uint8_t stack_canary[] = {0xAB, 0xCD, 0xAB, 0xCD,
65-
0xAB, 0xCD, 0xAB, 0xCD,
66-
0xAB, 0xCD, 0xAB, 0xCD,
67-
0xAB, 0xCD, 0xAB, 0xCD};
68-
6960
static size_t
7061
get_next_stk_size(rust_task_thread *thread, rust_task *task,
7162
size_t min, size_t current, size_t requested) {
@@ -90,38 +81,6 @@ get_next_stk_size(rust_task_thread *thread, rust_task *task,
9081

9182
// Task stack segments. Heap allocated and chained together.
9283

93-
static void
94-
config_valgrind_stack(stk_seg *stk) {
95-
stk->valgrind_id =
96-
VALGRIND_STACK_REGISTER(&stk->data[0],
97-
stk->end);
98-
#ifndef NVALGRIND
99-
// Establish that the stack is accessible. This must be done when reusing
100-
// old stack segments, since the act of popping the stack previously
101-
// caused valgrind to consider the whole thing inaccessible.
102-
size_t sz = stk->end - (uintptr_t)&stk->data[0];
103-
VALGRIND_MAKE_MEM_UNDEFINED(stk->data + sizeof(stack_canary),
104-
sz - sizeof(stack_canary));
105-
#endif
106-
}
107-
108-
static void
109-
unconfig_valgrind_stack(stk_seg *stk) {
110-
VALGRIND_STACK_DEREGISTER(stk->valgrind_id);
111-
}
112-
113-
static void
114-
add_stack_canary(stk_seg *stk) {
115-
memcpy(stk->data, stack_canary, sizeof(stack_canary));
116-
assert(sizeof(stack_canary) == 16 && "Stack canary was not the expected size");
117-
}
118-
119-
static void
120-
check_stack_canary(stk_seg *stk) {
121-
assert(!memcmp(stk->data, stack_canary, sizeof(stack_canary))
122-
&& "Somebody killed the canary");
123-
}
124-
12584
// The amount of stack in a segment available to Rust code
12685
static size_t
12786
user_stack_size(stk_seg *stk) {

src/rt/rust_task.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "rust_kernel.h"
1616
#include "rust_obstack.h"
1717
#include "boxed_region.h"
18+
#include "rust_stack.h"
1819

1920
// Corresponds to the rust chan (currently _chan) type.
2021
struct chan_handle {
@@ -24,18 +25,6 @@ struct chan_handle {
2425

2526
struct rust_box;
2627

27-
struct stk_seg {
28-
stk_seg *prev;
29-
stk_seg *next;
30-
uintptr_t end;
31-
unsigned int valgrind_id;
32-
#ifndef _LP64
33-
uint32_t pad;
34-
#endif
35-
36-
uint8_t data[];
37-
};
38-
3928
struct frame_glue_fns {
4029
uintptr_t mark_glue_off;
4130
uintptr_t drop_glue_off;

0 commit comments

Comments
 (0)