-
Notifications
You must be signed in to change notification settings - Fork 13.5k
fix -Zsanitizer=kcfi
on #[naked]
functions
#143293
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
base: master
Are you sure you want to change the base?
Conversation
Some changes occurred in compiler/rustc_codegen_ssa Some changes occurred in tests/codegen/sanitizer cc @rcvalle |
let is_reify_shim = matches!(instance.def, InstanceKind::ReifyShim(..)); | ||
|
||
let flags = cx.tcx().codegen_fn_attrs(instance.def_id()).flags; | ||
if flags.contains(CodegenFnAttrFlags::NAKED) && !is_reify_shim { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reify shims are not the only way to trigger this ICE. For example, vtable shims will trigger this too:
use std::arch::naked_asm;
trait MyTrait {
#[unsafe(naked)]
extern "C" fn foo(&self) {
naked_asm!("ret")
}
}
impl MyTrait for i32 {}
fn main() {
let x: extern "C" fn(&_) = <dyn MyTrait as MyTrait>::foo;
x(&1);
}
I think this check should be limited to InstanceKind::Item
, like:
if let InstanceKind::Item(def_id) = instance.kind
&& cx.tcx().codegen_fn_attrs(instance.def_id()).flags.contains(NAKED)
{}
Rather than filtering out shims explicitly.
r? compiler-errors |
Please also grep for other instances of |
This also feels like it deserves a larger refactor, though I wouldn't block this fix on that: I think the |
(edit: the code I shared above actually is just a non-CFI instance of a reify shim, not a vtable shim, but I think that the point still remains that we may add new shim |
Came here to post that I think we should have a If we made a Other attributes that might make sense to have an effective flag strip for some instance kinds include |
Yeah, I agree. I think it's worth to just migrate all And it should probably take |
fixes #143266
With
-Zsanitizer=kcfi
, indirect calls happen via generated intermediate shim that forwards the call. The generated shim preserves the attributes of the original, including#[unsafe(naked)]
. The shim is not a naked function though, and violates its invariants (like having a body that consists of a singlenaked_asm!
call).My fix here is to match on the
InstanceKind
, and only usecodegen_naked_asm
when the instance is not aReifyShim
. That does beg the question whether there are otherInstanceKind
s that could come up. As far as I can tell the answer is no: calling viadyn
seems to work find, and#[track_caller]
is disallowed in combination with#[naked]
.r? codegen
@rustbot label +A-naked
cc @maurer @rcvalle