Skip to content
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

x86 asm! accepts "lock" as a standalone instruction #82314

Closed
jonas-schievink opened this issue Feb 20, 2021 · 1 comment · Fixed by #90533
Closed

x86 asm! accepts "lock" as a standalone instruction #82314

jonas-schievink opened this issue Feb 20, 2021 · 1 comment · Fixed by #90533
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-bug Category: This is a bug. F-asm `#![feature(asm)]` (not `llvm_asm`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@jonas-schievink
Copy link
Contributor

#![feature(asm)]

#[no_mangle]
pub fn aaaaaaaaaaaaaaaaaa(x: &mut u32) {
    unsafe { asm!("lock"); }
    *x += 1;
}

This is not rejected, and, according to objdump, results in the following assembly (when optimizations are on):

0000000000000000 <aaaaaaaaaaaaaaaaaa>:
   0:	48 83 ec 08          	sub    $0x8,%rsp
   4:	f0 83 07 01          	lock addl $0x1,(%rdi)
   8:	58                   	pop    %rax
   9:	c3                   	ret    

I'd expect this to result in an error, since lock by itself isn't a valid instruction (it doesn't really seem desirable to let asm!() affect how rustc-produced instructions are decoded).

@jonas-schievink jonas-schievink added A-inline-assembly Area: Inline assembly (`asm!(…)`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-bug Category: This is a bug. F-asm `#![feature(asm)]` (not `llvm_asm`) labels Feb 20, 2021
@thomcc
Copy link
Member

thomcc commented Feb 21, 2021

It seems like a pretty esoteric case, and also hard to prevent in the general case.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 4, 2021
Add note about x86 instruction prefixes in asm! to unstable book

Since rustc doesn't do the assembly parsing itself, it is unable to detect when inline assembly ends with an instruction prefix, which doesn't make sense since it would apply to instructions from the compiler. This fixes rust-lang#82314 by mentioning that x86 instruction prefixes must not be used in inline assembly. AFAICT x86 is the only instruction set with instruction prefixes.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 4, 2021
Add note about x86 instruction prefixes in asm! to unstable book

Since rustc doesn't do the assembly parsing itself, it is unable to detect when inline assembly ends with an instruction prefix, which doesn't make sense since it would apply to instructions from the compiler. This fixes rust-lang#82314 by mentioning that x86 instruction prefixes must not be used in inline assembly. AFAICT x86 is the only instruction set with instruction prefixes.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 4, 2021
Add note about x86 instruction prefixes in asm! to unstable book

Since rustc doesn't do the assembly parsing itself, it is unable to detect when inline assembly ends with an instruction prefix, which doesn't make sense since it would apply to instructions from the compiler. This fixes rust-lang#82314 by mentioning that x86 instruction prefixes must not be used in inline assembly. AFAICT x86 is the only instruction set with instruction prefixes.
JohnTitor added a commit to JohnTitor/rust that referenced this issue Nov 5, 2021
Add note about x86 instruction prefixes in asm! to unstable book

Since rustc doesn't do the assembly parsing itself, it is unable to detect when inline assembly ends with an instruction prefix, which doesn't make sense since it would apply to instructions from the compiler. This fixes rust-lang#82314 by mentioning that x86 instruction prefixes must not be used in inline assembly. AFAICT x86 is the only instruction set with instruction prefixes.
@bors bors closed this as completed in fdb6bda Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-inline-assembly Area: Inline assembly (`asm!(…)`) C-bug Category: This is a bug. F-asm `#![feature(asm)]` (not `llvm_asm`) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants