Skip to content

Commit d23c355

Browse files
maurernebulark
authored andcommitted
Support for -Z patchable-function-entry
`-Z patchable-function-entry` works like `-fpatchable-function-entry` on clang/gcc. The arguments are total nop count and function offset. See MCP rust-lang/compiler-team#704
1 parent 8768db9 commit d23c355

File tree

7 files changed

+139
-1
lines changed

7 files changed

+139
-1
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+26
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,31 @@ fn inline_attr<'ll>(cx: &CodegenCx<'ll, '_>, inline: InlineAttr) -> Option<&'ll
5353
}
5454
}
5555

56+
#[inline]
57+
fn patchable_function_entry_attrs<'ll>(
58+
cx: &CodegenCx<'ll, '_>,
59+
) -> SmallVec<[&'ll Attribute; 2]> {
60+
let mut attrs = SmallVec::new();
61+
let patchable_spec = cx.tcx.sess.opts.unstable_opts.patchable_function_entry;
62+
let entry = patchable_spec.entry();
63+
let prefix = patchable_spec.prefix();
64+
if entry > 0 {
65+
attrs.push(llvm::CreateAttrStringValue(
66+
cx.llcx,
67+
"patchable-function-entry",
68+
&format!("{}", entry),
69+
));
70+
}
71+
if prefix > 0 {
72+
attrs.push(llvm::CreateAttrStringValue(
73+
cx.llcx,
74+
"patchable-function-prefix",
75+
&format!("{}", prefix),
76+
));
77+
}
78+
attrs
79+
}
80+
5681
/// Get LLVM sanitize attributes.
5782
#[inline]
5883
pub fn sanitize_attrs<'ll>(
@@ -420,6 +445,7 @@ pub fn from_fn_attrs<'ll, 'tcx>(
420445
llvm::set_alignment(llfn, align);
421446
}
422447
to_add.extend(sanitize_attrs(cx, codegen_fn_attrs.no_sanitize));
448+
to_add.extend(patchable_function_entry_attrs(cx));
423449

424450
// Always annotate functions with the target-cpu they are compiled for.
425451
// Without this, ThinLTO won't inline Rust functions into Clang generated

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ fn test_unstable_options_tracking_hash() {
813813
tracked!(packed_bundled_libs, true);
814814
tracked!(panic_abort_tests, true);
815815
tracked!(panic_in_drop, PanicStrategy::Abort);
816+
tracked!(patchable_function_entry, PatchableFunctionEntry::from_nop_count_and_offset(3, 4));
816817
tracked!(plt, Some(true));
817818
tracked!(polonius, Polonius::Legacy);
818819
tracked!(precise_enum_drop_elaboration, false);

compiler/rustc_middle/src/middle/codegen_fn_attrs.rs

+23
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,29 @@ pub struct CodegenFnAttrs {
4747
pub alignment: Option<Align>,
4848
}
4949

50+
#[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, HashStable)]
51+
pub struct PatchableFunctionEntry {
52+
/// Nops to prepend to the function
53+
prefix: u8,
54+
/// Nops after entry, but before body
55+
entry: u8,
56+
}
57+
58+
impl PatchableFunctionEntry {
59+
pub fn from_config(config: rustc_session::config::PatchableFunctionEntry) -> Self {
60+
Self { prefix: config.prefix(), entry: config.entry() }
61+
}
62+
pub fn from_prefix_and_entry(prefix: u8, entry: u8) -> Self {
63+
Self { prefix, entry }
64+
}
65+
pub fn prefix(&self) -> u8 {
66+
self.prefix
67+
}
68+
pub fn entry(&self) -> u8 {
69+
self.entry
70+
}
71+
}
72+
5073
#[derive(Clone, Copy, PartialEq, Eq, TyEncodable, TyDecodable, HashStable)]
5174
pub struct CodegenFnAttrFlags(u32);
5275
bitflags::bitflags! {

compiler/rustc_session/src/config.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -2933,7 +2933,7 @@ pub(crate) mod dep_tracking {
29332933
CrateType, DebugInfo, DebugInfoCompression, ErrorOutputType, FunctionReturn,
29342934
InliningThreshold, InstrumentCoverage, InstrumentXRay, LinkerPluginLto, LocationDetail,
29352935
LtoCli, NextSolverConfig, OomStrategy, OptLevel, OutFileName, OutputType, OutputTypes,
2936-
Polonius, RemapPathScopeComponents, ResolveDocLinks, SourceFileHashAlgorithm,
2936+
PatchableFunctionEntry, Polonius, RemapPathScopeComponents, ResolveDocLinks, SourceFileHashAlgorithm,
29372937
SplitDwarfKind, SwitchWithOptPath, SymbolManglingVersion, WasiExecModel,
29382938
};
29392939
use crate::lint;
@@ -3042,6 +3042,7 @@ pub(crate) mod dep_tracking {
30423042
OomStrategy,
30433043
LanguageIdentifier,
30443044
NextSolverConfig,
3045+
PatchableFunctionEntry,
30453046
Polonius,
30463047
InliningThreshold,
30473048
FunctionReturn,
@@ -3219,6 +3220,32 @@ impl DumpMonoStatsFormat {
32193220
}
32203221
}
32213222

3223+
/// `-Z patchable-function-entry` representation - how many nops to put before and after function
3224+
/// entry.
3225+
#[derive(Clone, Copy, PartialEq, Hash, Debug, Default)]
3226+
pub struct PatchableFunctionEntry {
3227+
/// Nops before the entry
3228+
prefix: u8,
3229+
/// Nops after the entry
3230+
entry: u8,
3231+
}
3232+
3233+
impl PatchableFunctionEntry {
3234+
pub fn from_nop_count_and_offset(nop_count: u8, offset: u8) -> Option<PatchableFunctionEntry> {
3235+
if nop_count < offset {
3236+
None
3237+
} else {
3238+
Some(Self { prefix: offset, entry: nop_count - offset })
3239+
}
3240+
}
3241+
pub fn prefix(&self) -> u8 {
3242+
self.prefix
3243+
}
3244+
pub fn entry(&self) -> u8 {
3245+
self.entry
3246+
}
3247+
}
3248+
32223249
/// `-Zpolonius` values, enabling the borrow checker polonius analysis, and which version: legacy,
32233250
/// or future prototype.
32243251
#[derive(Clone, Copy, PartialEq, Hash, Debug, Default)]

compiler/rustc_session/src/options.rs

+29
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ mod desc {
379379
pub const parse_passes: &str = "a space-separated list of passes, or `all`";
380380
pub const parse_panic_strategy: &str = "either `unwind` or `abort`";
381381
pub const parse_on_broken_pipe: &str = "either `kill`, `error`, or `inherit`";
382+
pub const parse_patchable_function_entry: &str =
383+
"nop_count,entry_offset or nop_count (defaulting entry_offset=0)";
382384
pub const parse_opt_panic_strategy: &str = parse_panic_strategy;
383385
pub const parse_oom_strategy: &str = "either `panic` or `abort`";
384386
pub const parse_relro_level: &str = "one of: `full`, `partial`, or `off`";
@@ -710,6 +712,7 @@ mod parse {
710712
true
711713
}
712714

715+
713716
pub(crate) fn parse_on_broken_pipe(slot: &mut OnBrokenPipe, v: Option<&str>) -> bool {
714717
match v {
715718
// OnBrokenPipe::Default can't be explicitly specified
@@ -721,6 +724,30 @@ mod parse {
721724
true
722725
}
723726

727+
pub(crate) fn parse_patchable_function_entry(
728+
slot: &mut PatchableFunctionEntry,
729+
v: Option<&str>,
730+
) -> bool {
731+
let mut nop_count = 0;
732+
let mut offset = 0;
733+
734+
if !parse_number(&mut nop_count, v) {
735+
let parts = v.and_then(|v| v.split_once(',')).unzip();
736+
if !parse_number(&mut nop_count, parts.0) {
737+
return false;
738+
}
739+
if !parse_number(&mut offset, parts.1) {
740+
return false;
741+
}
742+
}
743+
744+
if let Some(pfe) = PatchableFunctionEntry::from_nop_count_and_offset(nop_count, offset) {
745+
*slot = pfe;
746+
return true;
747+
}
748+
false
749+
}
750+
724751
pub(crate) fn parse_oom_strategy(slot: &mut OomStrategy, v: Option<&str>) -> bool {
725752
match v {
726753
Some("panic") => *slot = OomStrategy::Panic,
@@ -1844,6 +1871,8 @@ options! {
18441871
"panic strategy for panics in drops"),
18451872
parse_only: bool = (false, parse_bool, [UNTRACKED],
18461873
"parse only; do not compile, assemble, or link (default: no)"),
1874+
patchable_function_entry: PatchableFunctionEntry = (PatchableFunctionEntry::default(), parse_patchable_function_entry, [TRACKED],
1875+
"nop padding at function entry"),
18471876
plt: Option<bool> = (None, parse_opt_bool, [TRACKED],
18481877
"whether to use the PLT when calling into shared libraries;
18491878
only has effect for PIC code on systems with ELF binaries
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# `patchable-function-entry`
2+
3+
--------------------
4+
5+
The `-Z patchable-function-entry=M,N` or `-Z patchable-function-entry=M`
6+
compiler flag enables nop padding of function entries with M nops, with
7+
an offset for the entry of the function at N nops. In the second form,
8+
N defaults to 0.
9+
10+
As an illustrative example, `-Z patchable-function-entry=3,2` would produce:
11+
12+
```
13+
nop
14+
nop
15+
function_label:
16+
nop
17+
//Actual function code begins here
18+
```
19+
20+
This flag is used for hotpatching, especially in the Linux kernel. The flag
21+
arguments are modeled after hte `-fpatchable-function-entry` flag as defined
22+
for both [Clang](https://clang.llvm.org/docs/ClangCommandLineReference.html#cmdoption-clang-fpatchable-function-entry)
23+
and [gcc](https://gcc.gnu.org/onlinedocs/gcc/Instrumentation-Options.html#index-fpatchable-function-entry)
24+
and is intended to provide the same effect.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// compile-flags: -Z patchable-function-entry=15,10
2+
3+
#![crate_type = "lib"]
4+
5+
#[no_mangle]
6+
pub fn foo() {}
7+
// CHECK: @foo() unnamed_addr #0
8+
// CHECK: attributes #0 = { {{.*}}"patchable-function-entry"="5"{{.*}}"patchable-function-prefix"="10" {{.*}} }

0 commit comments

Comments
 (0)