-
Notifications
You must be signed in to change notification settings - Fork 12.1k
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
patchable-function-entry: Add unstable compiler flag and attribute #124741
base: master
Are you sure you want to change the base?
Changes from all commits
d23c355
6b2a26b
1498771
875a429
175c9c2
bd412f6
c68580a
d0b12f3
e700e31
be54f6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -5,7 +5,9 @@ use rustc_hir as hir; | |||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_hir::def::DefKind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_hir::{lang_items, weak_lang_items::WEAK_LANG_ITEMS, LangItem}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_middle::middle::codegen_fn_attrs::{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_middle::mir::mono::Linkage; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_middle::query::Providers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_middle::ty::{self as ty, TyCtxt}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -465,6 +467,66 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs { | |||||||||||||||||||||||||||||||||||||||||||||||||||||
None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
sym::patchable_function_entry => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
codegen_fn_attrs.patchable_function_entry = attr.meta_item_list().and_then(|l| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mut prefix = None; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
let mut entry = None; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
for item in l { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
let Some(meta_item) = item.meta_item() else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tcx.dcx().span_err(item.span(), "Expected name value pair."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
let Some(name_value_lit) = meta_item.name_value_literal() else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tcx.dcx().span_err(item.span(), "Expected name value pair."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
let attrib_to_write = match meta_item.name_or_empty() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
sym::prefix_nops => &mut prefix, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
sym::entry_nops => &mut entry, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
_ => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tcx.dcx().span_err( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
item.span(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
format!( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
"Unexpected name. Allowed names: {}, {}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
sym::prefix_nops, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
sym::entry_nops | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
let rustc_ast::LitKind::Int(val, _) = name_value_lit.kind else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tcx.dcx().span_err( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
name_value_lit.span, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
"Expected integer value between 0 and 255.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
let Ok(val) = val.get().try_into() else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tcx.dcx().span_err( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
name_value_lit.span, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
"Integer value outside range between 0 and 255.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
*attrib_to_write = Some(val); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+501
to
+517
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: this should probably just be
Suggested change
except... maybe an import would be appropriate for one of those. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
if let (None, None) = (prefix, entry) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tcx.dcx().span_err(attr.span, "Must specify at least one parameter."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+476
to
+522
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I missed this in my initial review pass because I was focused on the codegen parts, but yes there should probably be a UI test for these. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
Some(PatchableFunctionEntry::from_prefix_and_entry( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
prefix.unwrap_or(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
entry.unwrap_or(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
_ => {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# `patchable-function-entry` | ||
|
||
-------------------- | ||
|
||
The `-Z patchable-function-entry=total_nops,prefix_nops` or `-Z patchable-function-entry=total_nops` | ||
compiler flag enables nop padding of function entries with 'total_nops' nops, with | ||
an offset for the entry of the function at 'prefix_nops' nops. In the second form, | ||
'prefix_nops' defaults to 0. | ||
|
||
As an illustrative example, `-Z patchable-function-entry=3,2` would produce: | ||
|
||
```text | ||
nop | ||
nop | ||
function_label: | ||
nop | ||
//Actual function code begins here | ||
``` | ||
|
||
This flag is used for hotpatching, especially in the Linux kernel. The flag | ||
arguments are modeled after the `-fpatchable-function-entry` flag as defined | ||
for both [Clang](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fpatchable-function-entry) | ||
and [gcc](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#index-fpatchable-function-entry) | ||
and is intended to provide the same effect. |
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.
Since this is only ever two, instead of "two but sometimes it needs to be more", why not this?
At most it would add a
.filter_map(|x| x)
at the callsite. What do you think?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.
Personally I don't think it is worth it. Code would become a more cluttered, while only saving a bit of space on the stack.
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 was thinking more in the "embed our expectations in the types" rather than as an optimization, but yeah, it does make things a bit messier I guess. It was mostly a vibes-based question, and I'm fine with that not being the vibe~