-
Notifications
You must be signed in to change notification settings - Fork 13.4k
patchable-function-entry: Add unstable compiler flag and attribute #124741
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
Changes from 3 commits
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,11 +1,14 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_ast::{ast, attr, MetaItemKind, NestedMetaItem}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_data_structures::packed::Pu128; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
use rustc_errors::{codes::*, struct_span_code_err}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -463,6 +466,59 @@ 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."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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. For future reference, the message format doesn't use title-case nor periods. Maybe we should, but let's remain consistent with what we already have:
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let Some(name_value_lit) = meta_item.name_value_literal() else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tcx.dcx().span_err(item.span(), "Expected name value pair."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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 parameter name. Allowed names: {}, {}", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sym::prefix_nops, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
sym::entry_nops | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
let rustc_ast::LitKind::Int(Pu128(val @ 0..=255), _) = name_value_lit.kind | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tcx.dcx().span_err( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
name_value_lit.span, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
"Expected integer value between 0 and 255.", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
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. This error message would also be triggered when passing in a string, so the suggested wording might be a bit weird in that case. Do you think it makes sense to make the check for the string seperate again or is this just a minor issue? 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. I think it is worth it to be more accurate whenever possible. you can make this be
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*attrib_to_write = Some(val.try_into().unwrap()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if let (None, None) = (prefix, entry) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
tcx.dcx().span_err(attr.span, "Must specify at least one parameter."); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Some(PatchableFunctionEntry::from_prefix_and_entry( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
prefix.unwrap_or(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
entry.unwrap_or(0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
_ => {} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,8 +8,8 @@ use rustc_session::config::{ | |||||||||
ErrorOutputType, ExternEntry, ExternLocation, Externs, FunctionReturn, InliningThreshold, | ||||||||||
Input, InstrumentCoverage, InstrumentXRay, LinkSelfContained, LinkerPluginLto, LocationDetail, | ||||||||||
LtoCli, NextSolverConfig, OomStrategy, Options, OutFileName, OutputType, OutputTypes, PAuthKey, | ||||||||||
PacRet, Passes, Polonius, ProcMacroExecutionStrategy, Strip, SwitchWithOptPath, | ||||||||||
SymbolManglingVersion, WasiExecModel, | ||||||||||
PacRet, Passes, PatchableFunctionEntry, Polonius, ProcMacroExecutionStrategy, Strip, | ||||||||||
SwitchWithOptPath, SymbolManglingVersion, WasiExecModel, | ||||||||||
}; | ||||||||||
use rustc_session::lint::Level; | ||||||||||
use rustc_session::search_paths::SearchPath; | ||||||||||
|
@@ -813,6 +813,10 @@ fn test_unstable_options_tracking_hash() { | |||||||||
tracked!(packed_bundled_libs, true); | ||||||||||
tracked!(panic_abort_tests, true); | ||||||||||
tracked!(panic_in_drop, PanicStrategy::Abort); | ||||||||||
tracked!( | ||||||||||
patchable_function_entry, | ||||||||||
PatchableFunctionEntry::from_total_and_prefix_nops(10, 5).expect("total >= prefix") | ||||||||||
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. The message in
Suggested change
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. Don't know if I understood you correctly, but to get a value the condition 'total >=prefix' must be true. The panic would happen if that condition is false. So I'd think it would be correct as written? 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. What will happen if
Instead we should provide additional context beyond that. Having said that, this is a nitpick to make it easier for people to understand that happened in this test, it's not a deal breaker. 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.
Suggested change
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. maybe? 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. or just unwrap, this is just a test, really, and the condition is trivially obviously true. |
||||||||||
); | ||||||||||
tracked!(plt, Some(true)); | ||||||||||
tracked!(polonius, Polonius::Legacy); | ||||||||||
tracked!(precise_enum_drop_elaboration, false); | ||||||||||
|
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. |
Uh oh!
There was an error while loading. Please reload this page.