Skip to content

Commit 6eb8f8d

Browse files
authored
Merge pull request #244 from qnighy/dynamic-stack-probing
Fix segfault on probestack with dynamic alloca.
2 parents 28daccd + 1d15be6 commit 6eb8f8d

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/probestack.rs

+12
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,21 @@ pub unsafe extern fn __rust_probestack() {
6464
// bytes pushed on the stack orginally with our return address. Using
6565
// `8(%rsp)` simulates us testing the stack pointer in the caller's
6666
// context.
67+
68+
// It's usually called when %rax >= 0x1000, but that's not always true.
69+
// Dynamic stack allocation, which is needed to implement unsized
70+
// rvalues, triggers stackprobe even if %rax < 0x1000.
71+
// Thus we have to check %r11 first to avoid segfault.
72+
cmp $$0x1000,%r11
73+
jna 3f
6774
2:
6875
sub $$0x1000,%rsp
6976
test %rsp,8(%rsp)
7077
sub $$0x1000,%r11
7178
cmp $$0x1000,%r11
7279
ja 2b
7380
81+
3:
7482
// Finish up the last remaining stack space requested, getting the last
7583
// bits out of r11
7684
sub %r11,%rsp
@@ -98,13 +106,17 @@ pub unsafe extern fn __rust_probestack() {
98106
asm!("
99107
push %ecx
100108
mov %eax,%ecx
109+
110+
cmp $$0x1000,%ecx
111+
jna 3f
101112
2:
102113
sub $$0x1000,%esp
103114
test %esp,8(%esp)
104115
sub $$0x1000,%ecx
105116
cmp $$0x1000,%ecx
106117
ja 2b
107118
119+
3:
108120
sub %ecx,%esp
109121
test %esp,8(%esp)
110122

0 commit comments

Comments
 (0)