]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
objtool/rust: list `noreturn` Rust functions
authorMiguel Ojeda <ojeda@kernel.org>
Thu, 25 Jul 2024 18:33:22 +0000 (20:33 +0200)
committerMiguel Ojeda <ojeda@kernel.org>
Sun, 18 Aug 2024 21:34:37 +0000 (23:34 +0200)
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
Link: https://doc.rust-lang.org/rustc/symbol-mangling/v0.html#disambiguator
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Tested-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Kees Cook <kees@kernel.org>
Tested-by: Benno Lossin <benno.lossin@proton.me>
Link: https://lore.kernel.org/r/20240725183325.122827-6-ojeda@kernel.org
[ 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 <ojeda@kernel.org>
tools/objtool/check.c
tools/objtool/noreturns.h

index 01237d16722387ed5167803540cde442be63185e..d086f207a3d31ef7511218286b75358a58519467 100644 (file)
@@ -177,6 +177,52 @@ static bool is_sibling_call(struct instruction *insn)
        return (is_static_jump(insn) && insn_call_dest(insn));
 }
 
+/*
+ * Checks if a string ends with another.
+ */
+static bool str_ends_with(const char *s, const char *sub)
+{
+       const int slen = strlen(s);
+       const int sublen = strlen(sub);
+
+       if (sublen > slen)
+               return 0;
+
+       return !memcmp(s + slen - sublen, sub, sublen);
+}
+
+/*
+ * Checks if a function is a Rust "noreturn" one.
+ */
+static bool is_rust_noreturn(const struct symbol *func)
+{
+       /*
+        * If it does not start with "_R", then it is not a Rust symbol.
+        */
+       if (strncmp(func->name, "_R", 2))
+               return false;
+
+       /*
+        * These are just heuristics -- we do not control the precise symbol
+        * name, due to the crate disambiguators (which depend on the compiler)
+        * as well as changes to the source code itself between versions (since
+        * these come from the Rust standard library).
+        */
+       return str_ends_with(func->name, "_4core5sliceSp15copy_from_slice17len_mismatch_fail")          ||
+              str_ends_with(func->name, "_4core6option13unwrap_failed")                                ||
+              str_ends_with(func->name, "_4core6result13unwrap_failed")                                ||
+              str_ends_with(func->name, "_4core9panicking5panic")                                      ||
+              str_ends_with(func->name, "_4core9panicking9panic_fmt")                                  ||
+              str_ends_with(func->name, "_4core9panicking14panic_explicit")                            ||
+              str_ends_with(func->name, "_4core9panicking14panic_nounwind")                            ||
+              str_ends_with(func->name, "_4core9panicking18panic_bounds_check")                        ||
+              str_ends_with(func->name, "_4core9panicking19assert_failed_inner")                       ||
+              str_ends_with(func->name, "_4core9panicking36panic_misaligned_pointer_dereference")      ||
+              strstr(func->name, "_4core9panicking11panic_const24panic_const_")                        ||
+              (strstr(func->name, "_4core5slice5index24slice_") &&
+               str_ends_with(func->name, "_fail"));
+}
+
 /*
  * This checks to see if the given function is a "noreturn" function.
  *
@@ -202,10 +248,14 @@ static bool __dead_end_function(struct objtool_file *file, struct symbol *func,
        if (!func)
                return false;
 
-       if (func->bind == STB_GLOBAL || func->bind == STB_WEAK)
+       if (func->bind == STB_GLOBAL || func->bind == STB_WEAK) {
+               if (is_rust_noreturn(func))
+                       return true;
+
                for (i = 0; i < ARRAY_SIZE(global_noreturns); i++)
                        if (!strcmp(func->name, global_noreturns[i]))
                                return true;
+       }
 
        if (func->bind == STB_WEAK)
                return false;
index 1e8141ef1b15d4e218ec384704b3553d38b31e9a..e7da92489167e9eb2aef27d340b9fbd849d9cf6c 100644 (file)
@@ -39,6 +39,8 @@ NORETURN(panic)
 NORETURN(panic_smp_self_stop)
 NORETURN(rest_init)
 NORETURN(rewind_stack_and_make_dead)
+NORETURN(rust_begin_unwind)
+NORETURN(rust_helper_BUG)
 NORETURN(sev_es_terminate)
 NORETURN(snp_abort)
 NORETURN(start_kernel)