Skip to content

Commit fec86c8

Browse files
committed
codegen_llvm: check inline assembly constraints with LLVM
LLVM provides a way of checking whether the constraints and the actual inline assembly make sense. This commit introduces a check before emitting code for the inline assembly. If LLVM rejects the inline assembly (or its constraints), then the compiler emits an error E0668 ("malformed inline assembly"). Signed-off-by: Levente Kurusa <lkurusa@acm.org>
1 parent e5c6575 commit fec86c8

File tree

6 files changed

+53
-6
lines changed

6 files changed

+53
-6
lines changed

src/librustc_codegen_llvm/asm.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub fn codegen_inline_asm(
3030
ia: &hir::InlineAsm,
3131
outputs: Vec<PlaceRef<'ll, 'tcx>>,
3232
mut inputs: Vec<&'ll Value>
33-
) {
33+
) -> bool {
3434
let mut ext_constraints = vec![];
3535
let mut output_types = vec![];
3636

@@ -97,6 +97,10 @@ pub fn codegen_inline_asm(
9797
ia.alignstack,
9898
dialect
9999
);
100+
if r.is_none() {
101+
return false;
102+
}
103+
let r = r.unwrap();
100104

101105
// Again, based on how many outputs we have
102106
let outputs = ia.outputs.iter().zip(&outputs).filter(|&(ref o, _)| !o.is_indirect);
@@ -117,6 +121,8 @@ pub fn codegen_inline_asm(
117121
llvm::LLVMSetMetadata(r, kind,
118122
llvm::LLVMMDNodeInContext(bx.cx.llcx, &val, 1));
119123
}
124+
125+
return true;
120126
}
121127

122128
pub fn codegen_global_asm<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,

src/librustc_codegen_llvm/builder.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ impl Builder<'a, 'll, 'tcx> {
737737
pub fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char,
738738
inputs: &[&'ll Value], output: &'ll Type,
739739
volatile: bool, alignstack: bool,
740-
dia: AsmDialect) -> &'ll Value {
740+
dia: AsmDialect) -> Option<&'ll Value> {
741741
self.count_insn("inlineasm");
742742

743743
let volatile = if volatile { llvm::True }
@@ -753,9 +753,17 @@ impl Builder<'a, 'll, 'tcx> {
753753
debug!("Asm Output Type: {:?}", output);
754754
let fty = Type::func(&argtys[..], output);
755755
unsafe {
756-
let v = llvm::LLVMRustInlineAsm(
757-
fty, asm, cons, volatile, alignstack, dia);
758-
self.call(v, inputs, None)
756+
// Ask LLVM to verify that the constraints are well-formed.
757+
let constraints_ok = llvm::LLVMRustInlineAsmVerify(fty, cons);
758+
debug!("Constraint verification result: {:?}", constraints_ok);
759+
if constraints_ok == 1 {
760+
let v = llvm::LLVMRustInlineAsm(
761+
fty, asm, cons, volatile, alignstack, dia);
762+
Some(self.call(v, inputs, None))
763+
} else {
764+
// LLVM has detected an issue with our constaints, bail out
765+
None
766+
}
759767
}
760768
}
761769

src/librustc_codegen_llvm/diagnostics.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,26 @@ unsafe { simd_add(i32x2(0, 0), i32x2(1, 2)); } // ok!
4747
```
4848
"##,
4949

50+
E0668: r##"
51+
Malformed inline assembly rejected by LLVM.
52+
53+
LLVM checks the validity of the constraints and the assembly string passed to
54+
it. This error implies that LLVM seems something wrong with the inline
55+
assembly call.
56+
57+
In particular, it can happen if you forgot the closing bracket of a register
58+
constraint (see issue #51430):
59+
```
60+
#![feature(asm)]
61+
62+
fn main() {
63+
let rax: u64;
64+
unsafe {
65+
asm!("" :"={rax"(rax));
66+
println!("Accumulator is: {}", rax);
67+
}
68+
}
69+
```
70+
"##,
71+
5072
}

src/librustc_codegen_llvm/llvm/ffi.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,9 @@ extern "C" {
12081208
AlignStack: Bool,
12091209
Dialect: AsmDialect)
12101210
-> &Value;
1211+
pub fn LLVMRustInlineAsmVerify(Ty: &Type,
1212+
Constraints: *const c_char)
1213+
-> Bool;
12111214

12121215
pub fn LLVMRustDebugMetadataVersion() -> u32;
12131216
pub fn LLVMRustVersionMajor() -> u32;

src/librustc_codegen_llvm/mir/statement.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ impl FunctionCx<'a, 'll, 'tcx> {
8686
self.codegen_operand(&bx, input).immediate()
8787
}).collect();
8888

89-
asm::codegen_inline_asm(&bx, asm, outputs, input_vals);
89+
let res = asm::codegen_inline_asm(&bx, asm, outputs, input_vals);
90+
if !res {
91+
span_err!(bx.sess(), statement.source_info.span, E0668, "malformed inline assembly");
92+
}
9093
bx
9194
}
9295
mir::StatementKind::FakeRead(..) |

src/rustllvm/RustWrapper.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ extern "C" LLVMValueRef LLVMRustInlineAsm(LLVMTypeRef Ty, char *AsmString,
426426
HasSideEffects, IsAlignStack, fromRust(Dialect)));
427427
}
428428

429+
extern "C" bool LLVMRustInlineAsmVerify(LLVMTypeRef Ty,
430+
char *Constraints) {
431+
return InlineAsm::Verify(unwrap<FunctionType>(Ty), Constraints);
432+
}
433+
429434
extern "C" void LLVMRustAppendModuleInlineAsm(LLVMModuleRef M, const char *Asm) {
430435
unwrap(M)->appendModuleInlineAsm(StringRef(Asm));
431436
}

0 commit comments

Comments
 (0)