Skip to content

Commit 92174f9

Browse files
Stop emitting CET prologues for naked functions
We can apply nocf_check as a hack for now.
1 parent 8824d13 commit 92174f9

File tree

6 files changed

+33
-1
lines changed

6 files changed

+33
-1
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+4
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,10 @@ pub fn from_fn_attrs<'ll, 'tcx>(
299299
}
300300
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
301301
to_add.push(AttributeKind::Naked.create_attr(cx.llcx));
302+
// HACK(jubilee): "indirect branch tracking" works by attaching prologues to functions.
303+
// And it is a module-level attribute, so the alternative is pulling naked functions into new LLVM modules.
304+
// Otherwise LLVM's "naked" functions come with endbr prefixes per https://github.com/rust-lang/rust/issues/98768
305+
to_add.push(AttributeKind::NoCfCheck.create_attr(cx.llcx));
302306
}
303307
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::ALLOCATOR) {
304308
// apply to return place instead of function (unlike all other attributes applied in this function)

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ pub enum AttributeKind {
191191
StackProtect = 32,
192192
NoUndef = 33,
193193
SanitizeMemTag = 34,
194+
NoCfCheck = 35,
194195
}
195196

196197
/// LLVMIntPredicate

compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ enum LLVMRustAttribute {
8484
StackProtect = 32,
8585
NoUndef = 33,
8686
SanitizeMemTag = 34,
87+
NoCfCheck = 35,
8788
};
8889

8990
typedef struct OpaqueRustString *RustStringRef;

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ static Attribute::AttrKind fromRust(LLVMRustAttribute Kind) {
176176
return Attribute::NoAlias;
177177
case NoCapture:
178178
return Attribute::NoCapture;
179+
case NoCfCheck:
180+
return Attribute::NoCfCheck;
179181
case NoInline:
180182
return Attribute::NoInline;
181183
case NonNull:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// compile-flags: -C no-prepopulate-passes -Zcf-protection=full
2+
// assembly-output: emit-asm
3+
// needs-asm-support
4+
// only-x86_64
5+
6+
#![crate_type = "lib"]
7+
#![feature(naked_functions)]
8+
use std::arch::asm;
9+
10+
// The problem at hand: Rust has adopted a fairly strict meaning for "naked functions",
11+
// meaning "no prologue whatsoever, no, really, not one instruction."
12+
// Unfortunately, x86's control-flow enforcement, specifically indirect branch protection,
13+
// works by using an instruction for each possible landing site,
14+
// and LLVM implements this via making sure of that.
15+
#[no_mangle]
16+
#[naked]
17+
pub unsafe extern "sysv64" fn will_halt() -> ! {
18+
// CHECK-NOT: endbr{{32|64}}
19+
// CHECK: hlt
20+
asm!("hlt", options(noreturn))
21+
}
22+
23+
// what about aarch64?
24+
// "branch-protection"=false

src/test/codegen/naked-noinline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ pub unsafe fn g() {
2828
f();
2929
}
3030

31-
// CHECK: attributes [[ATTR]] = { naked noinline{{.*}} }
31+
// CHECK: attributes [[ATTR]] = { naked{{.*}}noinline{{.*}} }

0 commit comments

Comments
 (0)