-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Weird segfault when trying inline asm on macOS in Rust #63977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Could you try removing the |
Also, you should mark the |
SolutionI think you want to use #![feature(asm)]
fn main() {
let result: u64;
unsafe { asm!("
xor rax, rax
${:private}process_loop:
nop
add rax, 1
cmp rax, 500
jne ${:private}process_loop
": "={rax}"(result)
:
:
:"intel") };
println!("Result: {}", result);
} Alternative solutionIf you switch to AT&T syntax, you can use #![feature(asm)]
fn main() {
let result: u64;
unsafe { asm!("
xorl %eax, %eax
0:
nop
addq $$1, %rax
cmpq $$500, %rax
jne 0b
": "={rax}"(result)
:
:
:) };
println!("Result: {}", result);
} ExplanationWhat seems to be happening is the following. If you ask .p2align 4, 0x90
__ZN1d4main17h3e6e8052bbc66057E:
pushq %rbp
movq %rsp, %rbp
subq $112, %rsp
movq __ZN4core3fmt3num3imp52_$LT$impl$u20$core..fmt..Display$u20$for$u20$u64$GT$3fmt17h37acbe90c85d9165E@GOTPCREL(%rip), %rsi
## InlineAsm Start
xorq %rax, %rax
.process_loop:
nop
addq $1, %rax
cmpq $500, %rax
jne .process_loop
## InlineAsm End If you compile it and look at the disassembly you see all of those nops that you saw from IDA. d::main::h3e6e8052bbc66057:
100000d80: pushq %rbp
100000d81: movq %rsp, %rbp
100000d84: subq $112, %rsp
100000d88: leaq 117905(%rip), %rsi
100000d8f: xorq %rax, %rax
100000d92: nop
100000d93: nop
100000d94: nop
100000d95: nop
100000d96: nop
100000d97: nop
100000d98: nop
100000d99: nop
100000d9a: nop
100000d9b: nop
100000d9c: nop
100000d9d: nop
100000d9e: nop
100000d9f: nop
_main:
100000da0: pushq %rbp
100000da1: movq %rsp, %rbp
100000da4: subq $16, %rsp You can see that the initial portion looks the same, up through the first instruction of the inline assembly, but then there's just the nop padding to put the The culprit here is actually the very last line of the assembly file. .subsections_via_symbols If you check out Apple's documentation, you can see what's happening here. Finally, at run time,
This should probably be a warning or error in rustc. |
You should only use local labels in inline assembly. This is stated in the documentation for the new |
Hello! I tried this piece of code:
On windows it compiles and runs normally. On my macOS it compiles with no error or warning output, but fired an output


Segmentation fault: 11
when I try to execute the output binary file.I tried IDA pro on both operating systems. If I compile this code in windows, the IDA gave me normal disassemble output I expected:
But when I compile on macOS (for as default, a Mach-O binary file), it gave me this with weird
nop
lines and a red mark meaning there was something wrong:What can cause this error and is there what I can do to fix this problem? Thanks!
FYI, my mac is
iMac (Retina 5K, 27-inch, 2017)
.The
uname -a
output:The
rustc -V
output:The
sysctl machdep.cpu
output:Click to expand
The text was updated successfully, but these errors were encountered: