Skip to content

update fortanix tests #144395

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions tests/assembly-llvm/x86_64-fortanix-unknown-sgx-lvi-generic-load.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
// Test LVI load hardening on SGX enclave code
// Test LVI load hardening on SGX enclave code, specifically that `ret` is rewritten.

//@ add-core-stubs
//@ assembly-output: emit-asm
//@ compile-flags: --crate-type staticlib
//@ only-x86_64-fortanix-unknown-sgx
//@ compile-flags: --target x86_64-fortanix-unknown-sgx -Copt-level=0
//@ needs-llvm-components: x86

#![feature(no_core, lang_items, f16)]
#![crate_type = "lib"]
#![no_core]

extern crate minicore;
use minicore::*;

#[no_mangle]
pub extern "C" fn plus_one(r: &mut u64) {
*r = *r + 1;
pub extern "C" fn dereference(a: &mut u64) -> u64 {
// CHECK-LABEL: dereference
// CHECK: lfence
// CHECK: mov
// CHECK: popq [[REGISTER:%[a-z]+]]
// CHECK-NEXT: lfence
// CHECK-NEXT: jmpq *[[REGISTER]]
*a
}

// CHECK: plus_one
// CHECK: lfence
// CHECK-NEXT: incq
// CHECK: popq [[REGISTER:%[a-z]+]]
// CHECK-NEXT: lfence
// CHECK-NEXT: jmpq *[[REGISTER]]
14 changes: 11 additions & 3 deletions tests/assembly-llvm/x86_64-fortanix-unknown-sgx-lvi-generic-ret.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
// Test LVI ret hardening on generic rust code

//@ add-core-stubs
//@ assembly-output: emit-asm
//@ compile-flags: --crate-type staticlib
//@ only-x86_64-fortanix-unknown-sgx
//@ compile-flags: --target x86_64-fortanix-unknown-sgx
//@ needs-llvm-components: x86

#![feature(no_core, lang_items, f16)]
#![crate_type = "lib"]
#![no_core]

extern crate minicore;
use minicore::*;

#[no_mangle]
pub extern "C" fn myret() {}
// CHECK: myret:
// CHECK-LABEL: myret:
// CHECK: popq [[REGISTER:%[a-z]+]]
// CHECK-NEXT: lfence
// CHECK-NEXT: jmpq *[[REGISTER]]
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// Test LVI load hardening on SGX inline assembly code

//@ add-core-stubs
//@ assembly-output: emit-asm
//@ compile-flags: --crate-type staticlib
//@ only-x86_64-fortanix-unknown-sgx
//@ compile-flags: --target x86_64-fortanix-unknown-sgx
//@ needs-llvm-components: x86

use std::arch::asm;
#![feature(no_core, lang_items, f16)]
#![crate_type = "lib"]
#![no_core]

extern crate minicore;
use minicore::*;

#[no_mangle]
pub extern "C" fn get(ptr: *const u64) -> u64 {
// CHECK-LABEL: get
// CHECK: movq
// CHECK-NEXT: lfence
let value: u64;
unsafe {
asm!("mov {}, [{}]",
Expand All @@ -17,18 +26,13 @@ pub extern "C" fn get(ptr: *const u64) -> u64 {
value
}

// CHECK: get
// CHECK: movq
// CHECK-NEXT: lfence

#[no_mangle]
pub extern "C" fn myret() {
// CHECK-LABEL: myret
// CHECK: shlq $0, (%rsp)
// CHECK-NEXT: lfence
// CHECK-NEXT: retq
unsafe {
asm!("ret");
}
}

// CHECK: myret
// CHECK: shlq $0, (%rsp)
// CHECK-NEXT: lfence
// CHECK-NEXT: retq
43 changes: 28 additions & 15 deletions tests/run-make/x86_64-fortanix-unknown-sgx-lvi/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,56 @@

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

use run_make_support::{cmd, cwd, llvm_filecheck, llvm_objdump, regex, set_current_dir, target};
use run_make_support::{
cargo, cwd, llvm_filecheck, llvm_objdump, regex, run, set_current_dir, target,
};

fn main() {
let main_dir = cwd();
set_current_dir("enclave");
// HACK(eddyb) sets `RUSTC_BOOTSTRAP=1` so Cargo can accept nightly features.
// These come from the top-level Rust workspace, that this crate is not a
// member of, but Cargo tries to load the workspace `Cargo.toml` anyway.
cmd("cargo")
.env("RUSTC_BOOTSTRAP", "1")
cargo()
.arg("-v")
.arg("run")
.arg("build")
.arg("--target")
.arg(target())
.current_dir("enclave")
.env("CC_x86_64_fortanix_unknown_sgx", "clang-11")
.env(
"CFLAGS_x86_64_fortanix_unknown_sgx",
"-D__ELF__ -isystem/usr/include/x86_64-linux-gnu -mlvi-hardening -mllvm -x86-experimental-lvi-inline-asm-hardening",
)
.env("CXX_x86_64_fortanix_unknown_sgx", "clang++-11")
.env(
"CXXFLAGS_x86_64_fortanix_unknown_sgx",
"-D__ELF__ -isystem/usr/include/x86_64-linux-gnu -mlvi-hardening -mllvm -x86-experimental-lvi-inline-asm-hardening",
)
.run();
set_current_dir(&main_dir);
// Rust has various ways of adding code to a binary:

// Rust has several ways of including machine code into a binary:
//
// - Rust code
// - Inline assembly
// - Global assembly
// - C/C++ code compiled as part of Rust crates
// For those different kinds, we do have very small code examples that should be
// mitigated in some way. Mostly we check that ret instructions should no longer be present.
//
// For each of those, check that the mitigations are applied. Mostly we check
// that ret instructions are no longer present.

// Check that normal rust code has the right mitigations.
check("unw_getcontext", "unw_getcontext.checks");
check("__libunwind_Registers_x86_64_jumpto", "jumpto.checks");

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

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

// Check that C code compiled using the `cc` crate has the right mitigations.
check("cc_plus_one_c", "cc_plus_one_c.checks");
check("cc_plus_one_c_asm", "cc_plus_one_c_asm.checks");
check("cc_plus_one_cxx", "cc_plus_one_cxx.checks");
check("cc_plus_one_cxx_asm", "cc_plus_one_cxx_asm.checks");
check("cc_plus_one_asm", "cc_plus_one_asm.checks");

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

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