Skip to content

Commit 56d680d

Browse files
committed
objtool/rust: list noreturn Rust functions
Rust functions may be `noreturn` (i.e. diverging) by returning the "never" type, `!`, e.g. fn f() -> ! { loop {} } Thus list the known `noreturn` functions to avoid such warnings. Without this, `objtool` would complain if enabled for Rust, e.g.: rust/core.o: warning: objtool: _R...9panic_fmt() falls through to next function _R...18panic_nounwind_fmt() rust/alloc.o: warning: objtool: .text: unexpected end of section In order to do so, we cannot match symbols' names exactly, for two reasons: - Rust mangling scheme [1] contains disambiguators [2] which we cannot predict (e.g. they may vary depending on the compiler version). One possibility to solve this would be to parse v0 and ignore/zero those before comparison. - Some of the diverging functions come from `core`, i.e. the Rust standard library, which may change with each compiler version since they are implementation details (e.g. `panic_internals`). Thus, to workaround both issues, only part of the symbols are matched, instead of using the `NORETURN` macro in `noreturns.h`. Ideally, just like for the C side, we should have a better solution. For instance, the compiler could give us the list via something like: $ rustc --emit=noreturns ... [ Kees agrees this should be automated and Peter says: So it would be fairly simple to make objtool consume a magic section emitted by the compiler.. I think we've asked the compiler folks for that at some point even, but I don't have clear recollections. We will ask upstream Rust about it. And if they agree, then perhaps we can get Clang/GCC to implement something similar too -- for this sort of thing we can take advantage of the shorter cycles of `rustc` as well as their unstable features concept to experiment. Gary proposed using DWARF (though it would need to be available), and wrote a proof of concept script using the `object` and `gimli` crates: https://gist.github.com/nbdd0121/449692570622c2f46a29ad9f47c3379a - Miguel ] Link: https://rust-lang.github.io/rfcs/2603-rust-symbol-name-mangling-v0.html [1] Link: https://doc.rust-lang.org/rustc/symbol-mangling/v0.html#disambiguator [2] Acked-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: Alice Ryhl <[email protected]> Reviewed-by: Kees Cook <[email protected]> Tested-by: Benno Lossin <[email protected]> Link: https://lore.kernel.org/r/[email protected] [ Added `len_mismatch_fail` symbol for new `kernel` crate code merged since then as well as 3 more `core::panicking` symbols that appear in `RUST_DEBUG_ASSERTIONS=y` builds. - Miguel ] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent fc582df commit 56d680d

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

tools/objtool/check.c

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,52 @@ static bool is_sibling_call(struct instruction *insn)
177177
return (is_static_jump(insn) && insn_call_dest(insn));
178178
}
179179

180+
/*
181+
* Checks if a string ends with another.
182+
*/
183+
static bool str_ends_with(const char *s, const char *sub)
184+
{
185+
const int slen = strlen(s);
186+
const int sublen = strlen(sub);
187+
188+
if (sublen > slen)
189+
return 0;
190+
191+
return !memcmp(s + slen - sublen, sub, sublen);
192+
}
193+
194+
/*
195+
* Checks if a function is a Rust "noreturn" one.
196+
*/
197+
static bool is_rust_noreturn(const struct symbol *func)
198+
{
199+
/*
200+
* If it does not start with "_R", then it is not a Rust symbol.
201+
*/
202+
if (strncmp(func->name, "_R", 2))
203+
return false;
204+
205+
/*
206+
* These are just heuristics -- we do not control the precise symbol
207+
* name, due to the crate disambiguators (which depend on the compiler)
208+
* as well as changes to the source code itself between versions (since
209+
* these come from the Rust standard library).
210+
*/
211+
return str_ends_with(func->name, "_4core5sliceSp15copy_from_slice17len_mismatch_fail") ||
212+
str_ends_with(func->name, "_4core6option13unwrap_failed") ||
213+
str_ends_with(func->name, "_4core6result13unwrap_failed") ||
214+
str_ends_with(func->name, "_4core9panicking5panic") ||
215+
str_ends_with(func->name, "_4core9panicking9panic_fmt") ||
216+
str_ends_with(func->name, "_4core9panicking14panic_explicit") ||
217+
str_ends_with(func->name, "_4core9panicking14panic_nounwind") ||
218+
str_ends_with(func->name, "_4core9panicking18panic_bounds_check") ||
219+
str_ends_with(func->name, "_4core9panicking19assert_failed_inner") ||
220+
str_ends_with(func->name, "_4core9panicking36panic_misaligned_pointer_dereference") ||
221+
strstr(func->name, "_4core9panicking11panic_const24panic_const_") ||
222+
(strstr(func->name, "_4core5slice5index24slice_") &&
223+
str_ends_with(func->name, "_fail"));
224+
}
225+
180226
/*
181227
* This checks to see if the given function is a "noreturn" function.
182228
*
@@ -202,10 +248,14 @@ static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
202248
if (!func)
203249
return false;
204250

205-
if (func->bind == STB_GLOBAL || func->bind == STB_WEAK)
251+
if (func->bind == STB_GLOBAL || func->bind == STB_WEAK) {
252+
if (is_rust_noreturn(func))
253+
return true;
254+
206255
for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
207256
if (!strcmp(func->name, global_noreturns[i]))
208257
return true;
258+
}
209259

210260
if (func->bind == STB_WEAK)
211261
return false;

tools/objtool/noreturns.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ NORETURN(panic)
3939
NORETURN(panic_smp_self_stop)
4040
NORETURN(rest_init)
4141
NORETURN(rewind_stack_and_make_dead)
42+
NORETURN(rust_begin_unwind)
43+
NORETURN(rust_helper_BUG)
4244
NORETURN(sev_es_terminate)
4345
NORETURN(snp_abort)
4446
NORETURN(start_kernel)

0 commit comments

Comments
 (0)