Skip to content

Commit cafd9fb

Browse files
Rollup merge of #144395 - folkertdev:fortanix-run-make-test, r=jieyouxu
update fortanix tests Firstly, as far as I can tell, no CI job actually runs any of the fortanix tests? Maybe I'm missing the job that runs these tests though? In any case, the `assembly` tests now use `minicore`, meaning that they will run regardless of the host architecture (specifically, they will run during a standard PR CI build). The run-make test is actually broken, and I'd propose to make it just `cargo build` rather than `cargo run`. We can have a separate test for actually running the program, if desired. Also this test is subject to #128733, so I'd like to re-evaluate what parts of the C/C++ compilation are actually required or useful. cc [``@jethrogb](https://github.com/jethrogb)`` [``@raoulstrackx](https://github.com/raoulstrackx)`` [``@aditijannu](https://github.com/aditijannu)`` r? ``@jieyouxu``
2 parents ce1961b + 8b90847 commit cafd9fb

File tree

4 files changed

+74
-42
lines changed

4 files changed

+74
-42
lines changed

tests/assembly-llvm/x86_64-fortanix-unknown-sgx-lvi-generic-load.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
1-
// Test LVI load hardening on SGX enclave code
1+
// Test LVI load hardening on SGX enclave code, specifically that `ret` is rewritten.
22

3+
//@ add-core-stubs
34
//@ assembly-output: emit-asm
4-
//@ compile-flags: --crate-type staticlib
5-
//@ only-x86_64-fortanix-unknown-sgx
5+
//@ compile-flags: --target x86_64-fortanix-unknown-sgx -Copt-level=0
6+
//@ needs-llvm-components: x86
7+
8+
#![feature(no_core, lang_items, f16)]
9+
#![crate_type = "lib"]
10+
#![no_core]
11+
12+
extern crate minicore;
13+
use minicore::*;
614

715
#[no_mangle]
8-
pub extern "C" fn plus_one(r: &mut u64) {
9-
*r = *r + 1;
16+
pub extern "C" fn dereference(a: &mut u64) -> u64 {
17+
// CHECK-LABEL: dereference
18+
// CHECK: lfence
19+
// CHECK: mov
20+
// CHECK: popq [[REGISTER:%[a-z]+]]
21+
// CHECK-NEXT: lfence
22+
// CHECK-NEXT: jmpq *[[REGISTER]]
23+
*a
1024
}
11-
12-
// CHECK: plus_one
13-
// CHECK: lfence
14-
// CHECK-NEXT: incq
15-
// CHECK: popq [[REGISTER:%[a-z]+]]
16-
// CHECK-NEXT: lfence
17-
// CHECK-NEXT: jmpq *[[REGISTER]]
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
// Test LVI ret hardening on generic rust code
22

3+
//@ add-core-stubs
34
//@ assembly-output: emit-asm
4-
//@ compile-flags: --crate-type staticlib
5-
//@ only-x86_64-fortanix-unknown-sgx
5+
//@ compile-flags: --target x86_64-fortanix-unknown-sgx
6+
//@ needs-llvm-components: x86
7+
8+
#![feature(no_core, lang_items, f16)]
9+
#![crate_type = "lib"]
10+
#![no_core]
11+
12+
extern crate minicore;
13+
use minicore::*;
614

715
#[no_mangle]
816
pub extern "C" fn myret() {}
9-
// CHECK: myret:
17+
// CHECK-LABEL: myret:
1018
// CHECK: popq [[REGISTER:%[a-z]+]]
1119
// CHECK-NEXT: lfence
1220
// CHECK-NEXT: jmpq *[[REGISTER]]

tests/assembly-llvm/x86_64-fortanix-unknown-sgx-lvi-inline-assembly.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
// Test LVI load hardening on SGX inline assembly code
22

3+
//@ add-core-stubs
34
//@ assembly-output: emit-asm
4-
//@ compile-flags: --crate-type staticlib
5-
//@ only-x86_64-fortanix-unknown-sgx
5+
//@ compile-flags: --target x86_64-fortanix-unknown-sgx
6+
//@ needs-llvm-components: x86
67

7-
use std::arch::asm;
8+
#![feature(no_core, lang_items, f16)]
9+
#![crate_type = "lib"]
10+
#![no_core]
11+
12+
extern crate minicore;
13+
use minicore::*;
814

915
#[no_mangle]
1016
pub extern "C" fn get(ptr: *const u64) -> u64 {
17+
// CHECK-LABEL: get
18+
// CHECK: movq
19+
// CHECK-NEXT: lfence
1120
let value: u64;
1221
unsafe {
1322
asm!("mov {}, [{}]",
@@ -17,18 +26,13 @@ pub extern "C" fn get(ptr: *const u64) -> u64 {
1726
value
1827
}
1928

20-
// CHECK: get
21-
// CHECK: movq
22-
// CHECK-NEXT: lfence
23-
2429
#[no_mangle]
2530
pub extern "C" fn myret() {
31+
// CHECK-LABEL: myret
32+
// CHECK: shlq $0, (%rsp)
33+
// CHECK-NEXT: lfence
34+
// CHECK-NEXT: retq
2635
unsafe {
2736
asm!("ret");
2837
}
2938
}
30-
31-
// CHECK: myret
32-
// CHECK: shlq $0, (%rsp)
33-
// CHECK-NEXT: lfence
34-
// CHECK-NEXT: retq

tests/run-make/x86_64-fortanix-unknown-sgx-lvi/rmake.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,56 @@
1313

1414
//@ only-x86_64-fortanix-unknown-sgx
1515

16-
use run_make_support::{cmd, cwd, llvm_filecheck, llvm_objdump, regex, set_current_dir, target};
16+
use run_make_support::{
17+
cargo, cwd, llvm_filecheck, llvm_objdump, regex, run, set_current_dir, target,
18+
};
1719

1820
fn main() {
19-
let main_dir = cwd();
20-
set_current_dir("enclave");
21-
// HACK(eddyb) sets `RUSTC_BOOTSTRAP=1` so Cargo can accept nightly features.
22-
// These come from the top-level Rust workspace, that this crate is not a
23-
// member of, but Cargo tries to load the workspace `Cargo.toml` anyway.
24-
cmd("cargo")
25-
.env("RUSTC_BOOTSTRAP", "1")
21+
cargo()
2622
.arg("-v")
27-
.arg("run")
23+
.arg("build")
2824
.arg("--target")
2925
.arg(target())
26+
.current_dir("enclave")
27+
.env("CC_x86_64_fortanix_unknown_sgx", "clang")
28+
.env(
29+
"CFLAGS_x86_64_fortanix_unknown_sgx",
30+
"-D__ELF__ -isystem/usr/include/x86_64-linux-gnu -mlvi-hardening -mllvm -x86-experimental-lvi-inline-asm-hardening",
31+
)
32+
.env("CXX_x86_64_fortanix_unknown_sgx", "clang++")
33+
.env(
34+
"CXXFLAGS_x86_64_fortanix_unknown_sgx",
35+
"-D__ELF__ -isystem/usr/include/x86_64-linux-gnu -mlvi-hardening -mllvm -x86-experimental-lvi-inline-asm-hardening",
36+
)
3037
.run();
31-
set_current_dir(&main_dir);
32-
// Rust has various ways of adding code to a binary:
38+
39+
// Rust has several ways of including machine code into a binary:
40+
//
3341
// - Rust code
3442
// - Inline assembly
3543
// - Global assembly
3644
// - C/C++ code compiled as part of Rust crates
37-
// For those different kinds, we do have very small code examples that should be
38-
// mitigated in some way. Mostly we check that ret instructions should no longer be present.
45+
//
46+
// For each of those, check that the mitigations are applied. Mostly we check
47+
// that ret instructions are no longer present.
48+
49+
// Check that normal rust code has the right mitigations.
3950
check("unw_getcontext", "unw_getcontext.checks");
4051
check("__libunwind_Registers_x86_64_jumpto", "jumpto.checks");
4152

4253
check("std::io::stdio::_print::[[:alnum:]]+", "print.with_frame_pointers.checks");
4354

55+
// Check that rust global assembly has the right mitigations.
4456
check("rust_plus_one_global_asm", "rust_plus_one_global_asm.checks");
4557

58+
// Check that C code compiled using the `cc` crate has the right mitigations.
4659
check("cc_plus_one_c", "cc_plus_one_c.checks");
4760
check("cc_plus_one_c_asm", "cc_plus_one_c_asm.checks");
4861
check("cc_plus_one_cxx", "cc_plus_one_cxx.checks");
4962
check("cc_plus_one_cxx_asm", "cc_plus_one_cxx_asm.checks");
5063
check("cc_plus_one_asm", "cc_plus_one_asm.checks");
5164

65+
// Check that C++ code compiled using the `cc` crate has the right mitigations.
5266
check("cmake_plus_one_c", "cmake_plus_one_c.checks");
5367
check("cmake_plus_one_c_asm", "cmake_plus_one_c_asm.checks");
5468
check("cmake_plus_one_c_global_asm", "cmake_plus_one_c_global_asm.checks");
@@ -71,8 +85,7 @@ fn check(func_re: &str, mut checks: &str) {
7185
.input("enclave/target/x86_64-fortanix-unknown-sgx/debug/enclave")
7286
.args(&["--demangle", &format!("--disassemble-symbols={func}")])
7387
.run()
74-
.stdout_utf8();
75-
let dump = dump.as_bytes();
88+
.stdout();
7689

7790
// Unique case, must succeed at one of two possible tests.
7891
// This is because frame pointers are optional, and them being enabled requires

0 commit comments

Comments
 (0)