Cross-crate forward declarations in Rust
Rust has several ad-hoc mechanisms to allow user code to influence how the standard library executes. For example, custom panic handlers and custom allocators both involve registering functions that core standard library code needs to call. With more similar features planned, Jana Dönszelmann spotted the developing pattern and put together a general solution, introducing a safer, checked analogue of C's weak symbol linking. At Kangrejos 2026, she explained how this new language feature works, and proposed using it to clean up the kernel's handling of integer overflows in Rust code.
Dönszelmann has been a full-time contributor to the Rust compiler for the past two years, and has recently been occupied with work on the new trait solver. In between working on the trait solver, she has prototyped a new compiler flag, -Coverflow-checks=report that is likely to be useful to the kernel. Currently, when an integer overflow occurs in Rust code, the developer has a choice between a panic, or silently wrapping the overflowed value. There are library methods to perform checked mathematical operations that can return an error, as well, but many overflows occur in code that uses the normal mathematical operators.
In the kernel, panic-on-overflow in Rust code is enabled in both debug and release builds. By introducing that rule early, the Rust for Linux developers hoped to keep overflows out of the Rust code entirely, but that decision has caused some friction with the rest of the kernel. Linus Torvalds, in particular, is of the opinion that unexpected integer overflows should not crash the entire kernel.
Theoretically, this behavior could be configured using a custom panic handler. The kernel already defines one, since the default user-space implementation does not make sense in the kernel. The problem is that, currently, Rust defines the panic handler to be a function that never returns. That assumption is woven deeply enough into the language that providing a custom panic handler that calls WARN() and continues is simply not feasible.
Forward declarations
Custom panic handlers are also an example of a weird corner in the language, Dönszelmann said, because the core library already needs to know how to panic. Therefore, it forward-declares that there exists some panic handler, and leaves it up to subsequent crates to define the panic handler. That is normal in C, but wildly atypical in Rust, which ordinarily relies on order-independent name resolution within a single crate to address problems that C would solve with forward declarations. As a result, the standard library has only three cases of forward declaration: panic handling, custom allocators, and debugging logging. They're all handled as special cases.
Dönszelmann has been hoping to replace those mechanisms with a proper language feature that lets Rust programs deal with cross-crate forward references safely. That will only be useful for a small handful of libraries, but it will prevent them from relying on custom link-time logic. It would also make it relatively painless to add a separate function that permits reporting and continuing when an error occurs.
The language feature is currently using the name "externally implementable items", but Dönszelmann called this a temporary name that could be replaced when the feature was stabilized. The feature is available in nightly versions of the Rust compiler. One indicates an externally implementable item by adding an #[eii] annotation to the function signature. If the crate defines a function body, it is used as a fallback implementation, should no other crate define the item. If another crate does define the function, the body is ignored. If two crates attempt to define the function, or if there is no fallback implementation and no other definition, it results in a compiler error. That condition is checked by the compiler for safety, but actually implemented using weak linking, so that crates can still have their code generated in parallel.
With that infrastructure present in the language, Dönszelmann's prototype adds a new externally implementable item called report_integer_overflow() which is called automatically when integer overflow occurs, as long as the new compiler flag that she added is enabled. It is a normal function, and can return to the calling code, so the compiler will not treat subsequent code as being dead. The default implementation simply calls into the existing panic handler, so a program will need to both build with -Coverflow-checks=report and define its own handler in order to benefit from the new behavior.
Dönszelmann thought that was pretty much what the kernel needed, but acknowledged that she was a compiler developer, not a kernel developer, and said she wanted to hear what the assembled kernel people thought about the feature before she actually proposed the prototype for inclusion in the language. Externally implementable items are implemented in the compiler today, but with limited target support. Windows makes it difficult to use weak linking correctly, so externally implemented items are currently limited to Unix-like platforms. It will not be difficult to stabilize the new overflow-checking mode if the design is agreed on, she predicted, because the code is quite simple.
Discussion
Miguel Ojeda thought that it was worth adding the new compiler flag for use in the kernel. Gary Guo asked whether externally implementable items could be inlined. They aren't currently inlined, Dönszelmann said, but they could be in principle. Guo was glad to hear that, since he had been concerned about the performance impact of using externally implementable items generally, but agreed when Ojeda pointed out that the kernel probably doesn't care if the call to WARN() is cold.
Alice Ryhl expressed worry over the use of weak linking. She has seen cases where one symbol is overridden by another symbol, and that causes problems. Greg Kroah-Hartman said that weak symbols were common across the kernel, and didn't think that an additional use here would be a problem. Dönszelmann clarified that, while plain weak linking could have conflicting symbol implementations, since the Rust compiler can see the entire graph of crates being compiled, it can ensure that there is at most one definition for a given symbol.
Andreas Hindborg said that he had a benchmark for block-layer Rust code, and
offered to try out Dönszelmann's code on it, to see whether the added return
paths from the overflow-reporting function caused any performance regressions.
Dönszelmann was happy for people to try the feature, but did not think it would
have any performance impact compared to the existing panic-handler
implementation. Nadrieril asked whether externally implementable items supported
dynamic linking. Dönszelmann called that a "trick question
", pointing out
that with dynamic linking, it is no longer clear how to ensure that a single
implementation exists across the whole program. She called that more of a
philosophical problem than a technical one — if people want to define what the
expected behavior is, it's probably possible to implement.
Guo and Ryhl suggested some alternate ways the feature could be implemented without weak linking; Dönszelmann pointed out that most of these approaches would not work for programs that attempt to take the address of a static item. Ojeda thought that the approach Dönszelmann had presented would be preferable to simply accepting overflow silently, which Torvalds might otherwise push for. So he was in favor of trying the change. Josh Triplett suggested that perhaps the correct choice would be to stop the current kernel thread, instead of crashing the whole kernel. At that point the session was out of time, but in discussions afterward Guo pointed out that having a kernel thread die in Rust code is actually unsound, because the thread's stack is freed and not leaked. Triplett thought that sounded fixable, but agreed that it made sense to avoid crashing the thread until that was dealt with.
[ Thanks to the Linux Foundation, LWN's travel sponsor, for assistance in traveling to Kangrejos. ]
| Index entries for this article | |
|---|---|
| Conference | Kangrejos/2026 |
The LWN site is currently under high scraper load, so comment display has been suppressed for anonymous users. If you are a human, you may read the comments by clicking the button below:
Note: you can avoid this step in the future by logging into your LWN account.
