test hardening C inline assembly code (cc crate)

This commit is contained in:
Raoul Strackx 2020-03-26 15:05:43 +01:00
parent 0526e750cd
commit bdf81f508d
5 changed files with 33 additions and 1 deletions

View file

@ -0,0 +1,15 @@
CHECK: cc_plus_one_c_asm
CHECK: lfence
CHECK: lfence
CHECK: lfence
CHECK: lfence
CHECK: lfence
CHECK-NEXT: incl
CHECK-NEXT: jmp
CHECK-NEXT: shlq $0, (%rsp)
CHECK-NEXT: lfence
CHECK-NEXT: retq
CHECK: popq
CHECK-NEXT: popq [[REGISTER:%[a-z]+]]
CHECK-NEXT: lfence
CHECK-NEXT: jmpq *[[REGISTER]]

View file

@ -1,5 +1,5 @@
fn main() {
cc::Build::new()
.file("foo.c")
.compile("foo");
.compile("foo_c");
}

View file

@ -2,3 +2,17 @@
int cc_plus_one_c(int *arg) {
return *arg + 1;
}
int cc_plus_one_c_asm(int *arg) {
int value = 0;
asm volatile ( " movl (%1), %0\n"
" inc %0\n"
" jmp 1f\n"
" retq\n" // never executed, but a shortcut to determine how the assembler deals with `ret` instructions
"1:\n"
: "=r"(value)
: "r"(arg) );
return value;
}

View file

@ -1,5 +1,6 @@
extern {
fn cc_plus_one_c(arg : &u32) -> u32;
fn cc_plus_one_c_asm(arg : &u32) -> u32;
}
fn main() {
@ -7,5 +8,6 @@ fn main() {
unsafe{
println!("Answer to the Ultimate Question of Life, the Universe, and Everything: {}!", cc_plus_one_c(&value));
println!("Answer to the Ultimate Question of Life, the Universe, and Everything: {}!", cc_plus_one_c_asm(&value));
}
}

View file

@ -39,3 +39,4 @@ build
check "std::io::stdio::_print::h87f0c238421c45bc" print.checks
check cc_plus_one_c cc_plus_one_c.checks
check cc_plus_one_c_asm cc_plus_one_c_asm.checks