Skip to content

Commit 1c8f5db

Browse files
committed
Do not request sanitizers for naked functions
Naked functions can only contain inline asm, so any instrumentation inserted by sanitizers is illegal. Don't request it. Fixes #129224.
1 parent 9b82580 commit 1c8f5db

File tree

2 files changed

+47
-21
lines changed

2 files changed

+47
-21
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -411,26 +411,31 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
411411
// the string "false". Now it is disabled by absence of the attribute.
412412
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "branch-target-enforcement", "false"));
413413
}
414-
} else if llvm_util::get_version() >= (19, 0, 0) {
415-
// For non-naked functions, set branch protection attributes on aarch64.
416-
if let Some(BranchProtection { bti, pac_ret }) =
417-
cx.sess().opts.unstable_opts.branch_protection
418-
{
419-
assert!(cx.sess().target.arch == "aarch64");
420-
if bti {
421-
to_add.push(llvm::CreateAttrString(cx.llcx, "branch-target-enforcement"));
422-
}
423-
if let Some(PacRet { leaf, key }) = pac_ret {
424-
to_add.push(llvm::CreateAttrStringValue(
425-
cx.llcx,
426-
"sign-return-address",
427-
if leaf { "all" } else { "non-leaf" },
428-
));
429-
to_add.push(llvm::CreateAttrStringValue(
430-
cx.llcx,
431-
"sign-return-address-key",
432-
if key == PAuthKey::A { "a_key" } else { "b_key" },
433-
));
414+
} else {
415+
// Do not set sanitizer attributes for naked functions.
416+
to_add.extend(sanitize_attrs(cx, codegen_fn_attrs.no_sanitize));
417+
418+
if llvm_util::get_version() >= (19, 0, 0) {
419+
// For non-naked functions, set branch protection attributes on aarch64.
420+
if let Some(BranchProtection { bti, pac_ret }) =
421+
cx.sess().opts.unstable_opts.branch_protection
422+
{
423+
assert!(cx.sess().target.arch == "aarch64");
424+
if bti {
425+
to_add.push(llvm::CreateAttrString(cx.llcx, "branch-target-enforcement"));
426+
}
427+
if let Some(PacRet { leaf, key }) = pac_ret {
428+
to_add.push(llvm::CreateAttrStringValue(
429+
cx.llcx,
430+
"sign-return-address",
431+
if leaf { "all" } else { "non-leaf" },
432+
));
433+
to_add.push(llvm::CreateAttrStringValue(
434+
cx.llcx,
435+
"sign-return-address-key",
436+
if key == PAuthKey::A { "a_key" } else { "b_key" },
437+
));
438+
}
434439
}
435440
}
436441
}
@@ -485,7 +490,6 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
485490
if let Some(backchain) = backchain_attr(cx) {
486491
to_add.push(backchain);
487492
}
488-
to_add.extend(sanitize_attrs(cx, codegen_fn_attrs.no_sanitize));
489493
to_add.extend(patchable_function_entry_attrs(cx, codegen_fn_attrs.patchable_function_entry));
490494

491495
// Always annotate functions with the target-cpu they are compiled for.

tests/codegen/naked-asan.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Make sure we do not request sanitizers for naked functions.
2+
3+
//@ only-x86_64
4+
//@ needs-sanitizer-address
5+
//@ compile-flags: -Zsanitizer=address
6+
7+
#![crate_type = "lib"]
8+
#![no_std]
9+
#![feature(abi_x86_interrupt, naked_functions)]
10+
11+
// CHECK: define x86_intrcc void @page_fault_handler(ptr {{.*}}%0, i64 {{.*}}%1){{.*}}#[[ATTRS:[0-9]+]] {
12+
// CHECK-NOT: memcpy
13+
#[naked]
14+
#[no_mangle]
15+
pub extern "x86-interrupt" fn page_fault_handler(_: u64, _: u64) {
16+
unsafe {
17+
core::arch::asm!("ud2", options(noreturn));
18+
}
19+
}
20+
21+
// CHECK: #[[ATTRS]] =
22+
// CHECK-NOT: sanitize_address

0 commit comments

Comments
 (0)