-
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 |
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 |
And more broadly only codegen `InstanceKind::Item` using the naked function codegen code. Other instance kinds should follow the normal path.
2511c26
to
41dfac0
Compare
Some changes occurred in compiler/rustc_codegen_cranelift cc @bjorn3 |
Some changes occurred in compiler/rustc_codegen_ssa/src/codegen_attrs.rs The Miri subtree was changed cc @rust-lang/miri Some changes occurred to the CTFE machinery Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri Some changes occurred in compiler/rustc_codegen_gcc |
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.
I've made an attempt at implementing the codegen_instance_attrs
query. It does work (locally), but I think it's not quite right yet.
I've also updated the locations where an instance is easily available. That is generally the case in the backend, but in earlier stages it won't be. On the other hand, having codegen_fn_attrs
available means it's likely to be used by accident. So either it needs a scarier name, or we do translate all occurences to use InstanceKind::Item
, but that'll be quite verbose.
Anyway, my thinking is that resolving that can be its own PR, and here we can focus just on naked functions and some of the low-hanging fruit.
The only one that's still missing is here: rust/compiler/rustc_mir_transform/src/coverage/query.rs Lines 45 to 48 in f51c987
Where I could make up a |
codegen_instance_attrs(tcx, InstanceKind::Item(did.to_def_id())) | ||
} | ||
|
||
fn codegen_instance_attrs(tcx: TyCtxt<'_>, instance_kind: InstanceKind<'_>) -> CodegenFnAttrs { |
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.
Maybe codegen_instance_attrs
could be a regular function that calls the codegen_fn_attrs
query and drops the naked flag (and any other flags like no_mangle) as necessary depending on the instance kind?
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.
For this PR that is probably a nicer solution actually yeah.
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.
A downside of that approach is that function has to clone the CodegenFnAttrs
in order to modify it (the query method returns an immutable reference). I'm not sure how big of a deal that is though?
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.
A Cow<'tcx, CodegenFnAttrs>
could be returned I guess. So only when we actually need to modify the arguments is it necessary to clone it.
This comment has been minimized.
This comment has been minimized.
and use it for naked functions
59d859f
to
4e9b107
Compare
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