From 56293ad7c7006862a8e7a90fe3d847ea88671929 Mon Sep 17 00:00:00 2001 From: Masaki Hara Date: Sun, 27 May 2018 23:28:17 +0900 Subject: [PATCH 1/2] Fix segfault on probestack with dynamic alloca. --- src/probestack.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/probestack.rs b/src/probestack.rs index 34f466e..c8883e2 100644 --- a/src/probestack.rs +++ b/src/probestack.rs @@ -64,6 +64,8 @@ pub unsafe extern fn __rust_probestack() { // bytes pushed on the stack orginally with our return address. Using // `8(%rsp)` simulates us testing the stack pointer in the caller's // context. + cmp $$0x1000,%r11 + jna 3f 2: sub $$0x1000,%rsp test %rsp,8(%rsp) @@ -71,6 +73,7 @@ pub unsafe extern fn __rust_probestack() { cmp $$0x1000,%r11 ja 2b + 3: // Finish up the last remaining stack space requested, getting the last // bits out of r11 sub %r11,%rsp @@ -98,6 +101,9 @@ pub unsafe extern fn __rust_probestack() { asm!(" push %ecx mov %eax,%ecx + + cmp $$0x1000,%ecx + jna 3f 2: sub $$0x1000,%esp test %esp,8(%esp) @@ -105,6 +111,7 @@ pub unsafe extern fn __rust_probestack() { cmp $$0x1000,%ecx ja 2b + 3: sub %ecx,%esp test %esp,8(%esp) From 1d15be6a868594036edde16f84c39b6bb64014e1 Mon Sep 17 00:00:00 2001 From: Masaki Hara Date: Mon, 28 May 2018 13:29:35 +0900 Subject: [PATCH 2/2] Explain why we need extra check in probestack. --- src/probestack.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/probestack.rs b/src/probestack.rs index c8883e2..52a0414 100644 --- a/src/probestack.rs +++ b/src/probestack.rs @@ -64,6 +64,11 @@ pub unsafe extern fn __rust_probestack() { // bytes pushed on the stack orginally with our return address. Using // `8(%rsp)` simulates us testing the stack pointer in the caller's // context. + + // It's usually called when %rax >= 0x1000, but that's not always true. + // Dynamic stack allocation, which is needed to implement unsized + // rvalues, triggers stackprobe even if %rax < 0x1000. + // Thus we have to check %r11 first to avoid segfault. cmp $$0x1000,%r11 jna 3f 2: