Skip to content

memory fault in emmalloc prev_region #421

@cuviper

Description

@cuviper

In Fedora, we are building wasi-libc with MALLOC_IMPL=emmalloc, because dlmalloc's CC0 is problematic (#319). However, in further testing I have run into a null pointer crash in this emmalloc port. My reproducer is:

  1. Build current wasi-libc with MALLOC_IMPL=emmalloc
  2. Point to that build in the rust toolchain config [target.wasm32-wasi] wasi-root = "path/to/sysroot", and ./x build library --target wasm32-wasi.
  3. Start a new project, cargo new --lib foo; cd foo.
  4. Build the test, cargo +stage1 test --no-run --target wasm32-wasi.
  5. Run wasmtime -- ./target/wasm32-wasi/debug/deps/foo-*.wasm --help.
Error: failed to run main module `./target/wasm32-wasi/debug/deps/foo-80180d1f382acf95.wasm`

Caused by:
    0: failed to invoke command default
    1: error while executing at wasm backtrace:
           0: 0x455cd - prev_region
                           at ~/src/wasi-libc/emmalloc/emmalloc.c:309:27
                      - attempt_allocate
                           at ~/src/wasi-libc/emmalloc/emmalloc.c:681:26
[...]
    2: memory fault at wasm address 0xfffffffc in linear memory of size 0x120000
    3: wasm trap: out of bounds memory access

I get similar crash running with wasmer too. I only see this when wasi-libc is built with -O2 -DNDEBUG (and I added -g for backtraces), but it doesn't crash or hit any assertions without -DNDEBUG. I also tried with (rebased) #378 and it was no better.

In that line of code, it seems clear that 0xfffffffc must be a null pointer wrapping around by [-1].

size_t prevRegionSize = ((size_t*)region)[-1];

clang-analyzer also finds a null pointer error on that line: report-3472e3.html.gz. That error path includes some of the initialization in claim_more_memory that's different than the original emscripten code, but it's not the same path as what I actually hit at runtime.

Full wasmtime backtrace:
Error: failed to run main module `./target/wasm32-wasi/debug/deps/foo-80180d1f382acf95.wasm`

Caused by:
    0: failed to invoke command default
    1: error while executing at wasm backtrace:
           0: 0x455cd - prev_region
                           at ~/src/wasi-libc/emmalloc/emmalloc.c:309:27
                      - attempt_allocate
                           at ~/src/wasi-libc/emmalloc/emmalloc.c:681:26
           1: 0x45156 - allocate_memory
                           at ~/src/wasi-libc/emmalloc/emmalloc.c:800:19
                      - emmalloc_memalign
                           at ~/src/wasi-libc/emmalloc/emmalloc.c:893:15
           2: 0x4573c - emmalloc_malloc
                           at ~/src/wasi-libc/emmalloc/emmalloc.c:915:10
                      - malloc
                           at ~/src/wasi-libc/emmalloc/emmalloc.c:920:10
           3: 0x435fc - std::sys::wasi::alloc::<impl core::alloc::global::GlobalAlloc for std::alloc::System>::alloc::h30bf2b5c8e81bf65
                           at ~/rust/library/std/src/sys/wasi/../unix/alloc.rs:14:13
                      - __rdl_alloc
                           at ~/rust/library/std/src/alloc.rs:381:13
           4: 0x24bb - <unknown>!__rust_alloc
           5: 0x38755 - alloc::alloc::alloc::h7d5c328179f6fa67
                           at ~/rust/library/alloc/src/alloc.rs:100:9
                      - alloc::alloc::Global::alloc_impl::h1bd1691f491977af
                           at ~/rust/library/alloc/src/alloc.rs:183:73
                      - <alloc::alloc::Global as core::alloc::Allocator>::allocate::h12ef56f8ce7e59a1
                           at ~/rust/library/alloc/src/alloc.rs:243:9
                      - alloc::alloc::exchange_malloc::h609177d2ac860183
                           at ~/rust/library/alloc/src/alloc.rs:332:18
                      - alloc::boxed::Box<T>::new::hb82ce197ac45e458
                           at ~/rust/library/alloc/src/boxed.rs:217:9
                      - getopts::Options::usage_items::hc5b79851b290aebc
                           at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/getopts-0.2.21/src/lib.rs:592:9
           6: 0x38333 - getopts::Options::usage_with_format::hdda5b0b199d95f00
                           at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/getopts-0.2.21/src/lib.rs:513:24
                      - getopts::Options::usage::h1649511b98ad5ea4
                           at ~/.cargo/registry/src/index.crates.io-6f17d22bba15001f/getopts-0.2.21/src/lib.rs:498:9
           7: 0x10d5c - test::cli::usage::h13bd525bb03cc06d
                           at ~/rust/library/test/src/cli.rs:192:17
                      - test::cli::parse_opts::h4ef2547817c55788
                           at ~/rust/library/test/src/cli.rs:213:9
           8: 0x33a31 - test::test_main::h9b57329eed8a804d
                           at ~/rust/library/test/src/lib.rs:99:26
           9: 0x3475a - test::test_main_static::h8c233200492b5e32
                           at ~/rust/library/test/src/lib.rs:158:5
          10: 0x21c2 - foo::main::h27f72609c412c5ae
                           at /tmp/tmp.6bCstl/tmp/foo/src/lib.rs:1:1
          11: 0x1bf2 - core::ops::function::FnOnce::call_once::hfe53c2cf666d96fa
                           at ~/rust/library/core/src/ops/function.rs:250:5
          12: 0x1d84 - std::sys_common::backtrace::__rust_begin_short_backtrace::h994502d568a74c3c
                           at ~/rust/library/std/src/sys_common/backtrace.rs:135:18
          13:  0xaf4 - std::rt::lang_start::{{closure}}::hcd76d0dca9e3dad5
                           at ~/rust/library/std/src/rt.rs:166:18
          14: 0x3cbc2 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::hffbdde799ca12b5f
                           at ~/rust/library/core/src/ops/function.rs:284:13
                      - std::panicking::try::do_call::h4b5d5542f1c86349
                           at ~/rust/library/std/src/panicking.rs:500:40
                      - std::panicking::try::h51bfb7e0e56693e6
                           at ~/rust/library/std/src/panicking.rs:464:19
                      - std::panic::catch_unwind::h11d480fe44ac6ca4
                           at ~/rust/library/std/src/panic.rs:142:14
                      - std::rt::lang_start_internal::{{closure}}::h6e0ffd5421ca9da0
                           at ~/rust/library/std/src/rt.rs:148:48
                      - std::panicking::try::do_call::hfa71db6696b40a2e
                           at ~/rust/library/std/src/panicking.rs:500:40
                      - std::panicking::try::h63d1edb0b6dbf2fd
                           at ~/rust/library/std/src/panicking.rs:464:19
                      - std::panic::catch_unwind::h9e006dc595210a8f
                           at ~/rust/library/std/src/panic.rs:142:14
                      - std::rt::lang_start_internal::h2c53e8a6f6be7467
                           at ~/rust/library/std/src/rt.rs:148:20
          15:  0xa91 - std::rt::lang_start::h8dc3ccc2d1a087a6
                           at ~/rust/library/std/src/rt.rs:165:17
          16: 0x21e6 - <unknown>!__main_void
          17:  0xa0c - _start
                           at ~/src/wasi-libc/libc-bottom-half/crt/crt1-command.c:43:13
    2: memory fault at wasm address 0xfffffffc in linear memory of size 0x120000
    3: wasm trap: out of bounds memory access

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions