Skip to content

Compiler builtin calls imported function when compiling with -Z build-std for wasm #91158

Closed
@athei

Description

@athei

This is a minimal reproducer for a bug where the compiler inserts a call to a non-existent
panic function. The following things need to be true to reproduce the bug:

  • target must be wasm32-unknown-unknown
  • overflow checks must be enabled
  • link time optimization must be enabled
  • core must be recompiled using -Z build-std
  • The compiler needs to insert a call to memcpy

The non existent imported function will be then called by the overflow checks in the
memcpy compiler intrinsic.

I reported this error before and also added a regression test but this error is slightly different and hence the regression test didn't catch it.

Steps to reproduce

Clone the reproducer repo and run the following:

cargo +nightly build --release --target wasm32-unknown-unknown -Z build-std
wasm2wat target/wasm32-unknown-unknown/release/mult_panic.wasm | less

I tested these steps with rustc 1.58.0-nightly (936f2600b 2021-11-22). Version 2021-11-17 and
less seem to be unaffected. Therefore the first version to have this bug is 2021-11-18.

If you inspect the resulting wasm file you see that it imports some none existing panic function:

(import "env" "_ZN4core9panicking5panic17h80a3410ec4f43255E" (func $_ZN4core9panicking5panic17h80a3410ec4f43255E (type 0)))

This function is called from the compiler intrinsic on integer overflow. If either lto or
integer overflow is disabled, the error goes away.

It seems that recompiling libcore with overflow-checks = true is at the root of this problem. I wonder if that is even a good idea but I see no way to have this option to only apply to my own crate and not to -Z build-std.

Metadata

Metadata

Assignees

No one assigned

    Labels

    -Zbuild-stdUnstable Cargo option: Compile the standard library yourself.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions