Skip to content

Fix allocator compatibility with Rust 1.87 and newer - #79

Open
lkshminarayanan wants to merge 2 commits into
MPI-SWS:masterfrom
lkshminarayanan:fix-rust-allocator-compatability-issues
Open

lkshminarayanan wants to merge 2 commits into
MPI-SWS:masterfrom
lkshminarayanan:fix-rust-allocator-compatability-issues

Conversation

@lkshminarayanan

Copy link
Copy Markdown

Rust programs that allocate memory can fail verification with “Tried to execute an unknown external function” errors on recent rustc versions. This PR updates RustPrepPass to handle two changes in Rust’s allocator interface:

  1. Rust 1.87: mangled allocator symbols. Compiler-internal allocator symbols are now v0-mangled under the __rustc namespace. For example, __rust_alloc becomes _RNvCs<hash>_7___rustc12___rust_alloc, so the existing name matching no longer redirects calls to genmc__rust_alloc and its counterparts.
    Fix: Demangle these symbols before matching allocator names.
    Refs Mangle rustc_std_internal_symbols functions rust-lang/rust#127173

  2. Rust 1.89: function-based allocation shim check. The __rust_no_alloc_shim_is_unstable global was replaced by a mangled __rust_no_alloc_shim_is_unstable_v2 function called before allocation. Its definition is an empty function generated as part of the allocator shim when linking the final binary, so it is unavailable during verification.
    Fix: Remove calls to this function when the legacy global is absent. The calls only enforce the presence of the allocator shim at link time and are unnecessary because GenMC redirects the allocator calls.
    Refs Change __rust_no_alloc_shim_is_unstable to be a function rust-lang/rust#141061

The changes are split into two commits, one for each compiler change. Existing handling of unmangled allocator symbols and the legacy global is preserved, including Rust 1.87 and 1.88, which deliberately leave the global unmangled.

Refs #71

Verifying Rust programs compiled with rustc 1.87 or newer fails with
"Tried to execute an unknown external function" errors, as the
RustPrepPass no longer recognizes the allocator calls it reroutes.

Since Rust 1.87, the compiler-internal allocator symbols are v0-mangled
under the "__rustc" namespace, e.g., __rust_alloc is now emitted as
_RNvCs<hash>_7___rustc12___rust_alloc. The calls to __rust_alloc & co.
were thus no longer rerouted to genmc__rust_alloc & co.
Ref: rust-lang/rust#127173

Demangle __rustc-internal symbols before matching them against the
allocator function names. Unmangled symbols emitted by older rustc
versions are still handled as before. This includes the
__rust_no_alloc_shim_is_unstable global, which rustc 1.87 and 1.88
deliberately keep unmangled, so removeAllocShim() still finds it.

Signed-off-by: Lakshmi Narayanan Sreethar <slnarayanan.tvl@gmail.com>
Since Rust 1.89, the __rust_no_alloc_shim_is_unstable global variable
has been replaced by a (mangled) __rust_no_alloc_shim_is_unstable_v2
function that is called before allocating. The function is defined in
the allocator shim that rustc only generates when linking the final
binary, so verifying Rust programs fails with "Tried to execute an
unknown external function" errors.
Ref: rust-lang/rust#141061

The function is empty and only exists to cause a link error if the
allocator shim is missing. As we reroute the allocator calls to
genmc__rust_alloc & co. anyway, remove its calls if the global
variable is not present, similar to how removeAllocShim() handles the
global variable in older versions.

Signed-off-by: Lakshmi Narayanan Sreethar <slnarayanan.tvl@gmail.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant