Skip to content

Commit a6ecd1a

Browse files
committed
Add "volatile" and "memory" clobber to asm! that doesn't fall through.
Use the "volatile" option and the "memory" clobber on inline asm that does things like return directly, to reduce the chances of compilers rearranging the code.
1 parent 93e3091 commit a6ecd1a

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

src/arm.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub unsafe fn __aeabi_uidivmod() {
1111
bl __udivmodsi4
1212
ldr r1, [sp]
1313
add sp, sp, #4
14-
pop {pc}");
14+
pop {pc}" ::: "memory" : "volatile");
1515
intrinsics::unreachable();
1616
}
1717

@@ -26,7 +26,7 @@ pub unsafe fn __aeabi_uldivmod() {
2626
ldr r2, [sp, #8]
2727
ldr r3, [sp, #12]
2828
add sp, sp, #16
29-
pop {r4, pc}");
29+
pop {r4, pc}" ::: "memory" : "volatile");
3030
intrinsics::unreachable();
3131
}
3232

@@ -38,7 +38,7 @@ pub unsafe fn __aeabi_idivmod() {
3838
pop {r1, r2}
3939
muls r2, r2, r0
4040
subs r1, r1, r2
41-
pop {r4, pc}");
41+
pop {r4, pc}" ::: "memory" : "volatile");
4242
intrinsics::unreachable();
4343
}
4444

@@ -53,7 +53,7 @@ pub unsafe fn __aeabi_ldivmod() {
5353
ldr r2, [sp, #8]
5454
ldr r3, [sp, #12]
5555
add sp, sp, #16
56-
pop {r4, pc}");
56+
pop {r4, pc}" ::: "memory" : "volatile");
5757
intrinsics::unreachable();
5858
}
5959

src/probestack.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub unsafe extern fn __rust_probestack() {
8282
add %rax,%rsp
8383
8484
ret
85-
");
85+
" ::: "memory" : "volatile");
8686
::core::intrinsics::unreachable();
8787
}
8888

@@ -111,6 +111,6 @@ pub unsafe extern fn __rust_probestack() {
111111
add %eax,%esp
112112
pop %ecx
113113
ret
114-
");
114+
" ::: "memory" : "volatile");
115115
::core::intrinsics::unreachable();
116116
}

src/x86.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub unsafe fn ___chkstk_ms() {
2929
test %ecx,(%ecx)
3030
pop %eax
3131
pop %ecx
32-
ret");
32+
ret" ::: "memory" : "volatile");
3333
intrinsics::unreachable();
3434
}
3535

@@ -38,7 +38,8 @@ pub unsafe fn ___chkstk_ms() {
3838
#[naked]
3939
#[no_mangle]
4040
pub unsafe fn __alloca() {
41-
asm!("jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable");
41+
asm!("jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable"
42+
::: "memory" : "volatile");
4243
intrinsics::unreachable();
4344
}
4445

@@ -66,6 +67,6 @@ pub unsafe fn ___chkstk() {
6667
mov -4(%eax),%ecx // restore ecx
6768
push (%eax) // push return address onto the stack
6869
sub %esp,%eax // restore the original value in eax
69-
ret");
70+
ret" ::: "memory" : "volatile");
7071
intrinsics::unreachable();
7172
}

src/x86_64.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub unsafe fn ___chkstk_ms() {
2929
test %rcx,(%rcx)
3030
pop %rax
3131
pop %rcx
32-
ret");
32+
ret" ::: "memory" : "volatile");
3333
intrinsics::unreachable();
3434
}
3535

@@ -38,15 +38,17 @@ pub unsafe fn ___chkstk_ms() {
3838
#[no_mangle]
3939
pub unsafe fn __alloca() {
4040
asm!("mov %rcx,%rax // x64 _alloca is a normal function with parameter in rcx
41-
jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable");
41+
jmp ___chkstk // Jump to ___chkstk since fallthrough may be unreliable"
42+
::: "memory" : "volatile");
4243
intrinsics::unreachable();
4344
}
4445

4546
#[cfg(all(windows, target_env = "gnu", not(feature = "mangled-names")))]
4647
#[naked]
4748
#[no_mangle]
4849
pub unsafe fn ___chkstk() {
49-
asm!("
50+
asm!(
51+
"
5052
push %rcx
5153
cmp $$0x1000,%rax
5254
lea 16(%rsp),%rcx // rsp before calling this routine -> rcx
@@ -66,6 +68,8 @@ pub unsafe fn ___chkstk() {
6668
mov -8(%rax),%rcx // restore rcx
6769
push (%rax) // push return address onto the stack
6870
sub %rsp,%rax // restore the original value in rax
69-
ret");
71+
ret"
72+
::: "memory" : "volatile"
73+
);
7074
intrinsics::unreachable();
7175
}

0 commit comments

Comments
 (0)