Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm test failed with "index out of bounds: the len is 0 but the index is 4" #99

Closed
taiki-e opened this issue Dec 13, 2023 · 6 comments
Closed

Comments

@taiki-e
Copy link
Collaborator

taiki-e commented Dec 13, 2023

https://github.com/smol-rs/event-listener/actions/runs/7155060118/job/19483256324

Running tests/notify.rs (target/wasm32-unknown-unknown/debug/deps/notify-0ef5209d8abd9615.wasm)
Set timeout to 20 seconds...
Executing bindgen...                              
                                                  
running 9 tests

panicked at /home/runner/work/event-listener/event-listener/src/no_std.rs:639:48:
index out of bounds: the len is 0 but the index is 4

Stack:

Error
    at /home/runner/work/event-listener/event-listener/target/wasm32-unknown-unknown/wbg-tmp-notify-0ef5209d8abd9615.wasm/wasm-bindgen-test.js:562:17
    at logError (/home/runner/work/event-listener/event-listener/target/wasm32-unknown-unknown/wbg-tmp-notify-0ef5209d8abd9615.wasm/wasm-bindgen-test.js:223:18)
    at module.exports.__wbg_new_abda76e883ba8a5f (/home/runner/work/event-listener/event-listener/target/wasm32-unknown-unknown/wbg-tmp-notify-0ef5209d8abd9615.wasm/wasm-bindgen-test.js:561:65)
    at console_error_panic_hook::Error::new::h7d6b2a2d770f64c0 (wasm://wasm/0039f982:wasm-function[1462]:0x9b9fe)
    at console_error_panic_hook::hook_impl::hd5f26b63473b4819 (wasm://wasm/0039f982:wasm-function[405]:0x68b55)
    at console_error_panic_hook::hook::hedca[37](https://github.com/smol-rs/event-listener/actions/runs/7155060118/job/19483256324#step:18:38)fbd9551172 (wasm://wasm/00[39](https://github.com/smol-rs/event-listener/actions/runs/7155060118/job/19483256324#step:18:40)f982:wasm-function[2033]:0xa4f45)
    at wasm_bindgen_test::__rt::Context::new::{{closure}}::{{closure}}::h4947e30e1bc66a88 (wasm://wasm/0039f982:wasm-function[1124]:0x92beb)
    at std::panicking::rust_panic_with_hook::hbf46ef0245cc9589 (wasm://wasm/0039f982:wasm-function[781]:0x83b7b)
    at std::panicking::begin_panic_handler::{{closure}}::hc07db45[42](https://github.com/smol-rs/event-listener/actions/runs/7155060118/job/19483256324#step:18:43)14d2c87 (wasm://wasm/0039f982:wasm-function[1003]:0x8e[49](https://github.com/smol-rs/event-listener/actions/runs/7155060118/job/19483256324#step:18:50)c)
    at std::sys_common::backtrace::__rust_end_short_backtrace::hf9e2f055fb5ef672 (wasm://wasm/0039f982:wasm-function[2[53](https://github.com/smol-rs/event-listener/actions/runs/7155060118/job/19483256324#step:18:54)3]:0xa90e6)


test notify::notify_all_fair ... FAIL

...

panic seems to be happening on this line:

let entry = &self.listeners[e.get()];

cc @notgull

@notgull
Copy link
Member

notgull commented Dec 14, 2023

I can't emulate the failure on my own system, both with wasm-pack or on bare metal. In addition the error doesn't really make sense; the listeners list is initialized with one element and no elements are ever removed; just replaced in place. So the length of listeners should never be zero. Is memory corruption taking place somehow?

@notgull
Copy link
Member

notgull commented Dec 16, 2023

Actually, this looks like a regression in one of our dependencies.

$ wasm-pack test --node --no-default-features --features portable-atomic
<snip: build succeeds>
$ cargo update
    Updating crates.io index
    Updating concurrent-queue v2.3.0 -> v2.4.0
    Updating crossbeam-utils v0.8.16 -> v0.8.17
    Updating futures-lite v2.0.1 -> v2.1.0
    Updating itoa v1.0.9 -> v1.0.10
    Updating js-sys v0.3.65 -> v0.3.66
    Updating libc v0.2.150 -> v0.2.151
    Updating once_cell v1.18.0 -> v1.19.0
    Updating portable-atomic v1.5.1 -> v1.6.0
    Updating proc-macro2 v1.0.69 -> v1.0.70
    Updating ryu v1.0.15 -> v1.0.16
    Updating serde v1.0.192 -> v1.0.193
    Updating serde_derive v1.0.192 -> v1.0.193
    Updating syn v2.0.39 -> v2.0.41
    Updating wasm-bindgen v0.2.88 -> v0.2.89
    Updating wasm-bindgen-backend v0.2.88 -> v0.2.89
    Updating wasm-bindgen-futures v0.4.38 -> v0.4.39
    Updating wasm-bindgen-macro v0.2.88 -> v0.2.89
    Updating wasm-bindgen-macro-support v0.2.88 -> v0.2.89
    Updating wasm-bindgen-shared v0.2.88 -> v0.2.89
    Updating wasm-bindgen-test v0.3.38 -> v0.3.39
    Updating wasm-bindgen-test-macro v0.3.38 -> v0.3.39
    Updating web-sys v0.3.65 -> v0.3.66
$ wasm-pack test --node --no-default-features --features portable-atomic
[INFO]: 🎯  Checking for the Wasm target...
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
[INFO]: ⬇️  Installing wasm-bindgen...
    Finished test [unoptimized + debuginfo] target(s) in 0.07s
     Running unittests src/lib.rs (/hard_data/cargo_target/wasm32-unknown-unknown/debug/deps/event_listener-06fac476c475f6a4.wasm)
Set timeout to 20 seconds...
running 6 tests                                   

test event_listener::sys::tests::uncontended_inner ... ok
test event_listener::sys::tests::listener_slab_notify_prop ... ok
test event_listener::sys::tests::listener_slab_register ... ok
test event_listener::sys::tests::listener_slab_notify ... ok
test event_listener::sys::tests::smoke_listener_slab ... ok
test event_listener::sys::tests::smoke_mutex ... ok

test result: ok. 6 passed; 0 failed; 0 ignored

     Running tests/notify.rs (/hard_data/cargo_target/wasm32-unknown-unknown/debug/deps/notify-f0426cd404a8d46f.wasm)
Set timeout to 20 seconds...
running 9 tests                                   

panicked at tests/notify.rs:179:5:
assertion `left == right` failed
  left: 0
 right: 3

Stack:

Error
    at /hard_data/cargo_target/wasm32-unknown-unknown/wbg-tmp-notify-f0426cd404a8d46f.wasm/wasm-bindgen-test.js:562:17
    at logError (/hard_data/cargo_target/wasm32-unknown-unknown/wbg-tmp-notify-f0426cd404a8d46f.wasm/wasm-bindgen-test.js:223:18)
    at module.exports.__wbg_new_abda76e883ba8a5f (/hard_data/cargo_target/wasm32-unknown-unknown/wbg-tmp-notify-f0426cd404a8d46f.wasm/wasm-bindgen-test.js:561:65)
    at console_error_panic_hook::Error::new::h9390d33cd085384e (wasm://wasm/0037604e:wasm-function[1585]:0x97759)
    at console_error_panic_hook::hook_impl::h9f8eee9438ee2ed7 (wasm://wasm/0037604e:wasm-function[373]:0x6109d)
    at console_error_panic_hook::hook::h8dd903b592224246 (wasm://wasm/0037604e:wasm-function[1996]:0x9da4b)
    at wasm_bindgen_test::__rt::Context::new::{{closure}}::{{closure}}::h892748fa6e621e43 (wasm://wasm/0037604e:wasm-function[1108]:0x8c130)
    at std::panicking::rust_panic_with_hook::h45ef67ea8e8d85f1 (wasm://wasm/0037604e:wasm-function[751]:0x7c699)
    at std::panicking::begin_panic_handler::{{closure}}::h08ba82a95eeeae24 (wasm://wasm/0037604e:wasm-function[980]:0x87564)
    at std::sys_common::backtrace::__rust_end_short_backtrace::h216a38853c6acf5e (wasm://wasm/0037604e:wasm-function[2437]:0xa1644)

...

@notgull
Copy link
Member

notgull commented Dec 16, 2023

It looks like the guilty party is concurrent-queue v2.4.0?

$ wasm-pack test --node --no-default-features --features portable-atomic
<snip: tests succeed>
$ cargo update concurrent-queue
    Updating crates.io index
    Updating concurrent-queue v2.3.0 -> v2.4.0
$ wasm-pack test --node --no-default-features --features portable-atomic
<snip: tests fail>

The only change between concurrent-queue v2.3.0 and v2.4.0 is smol-rs/concurrent-queue#53, which I fail to see how it causes bugs. Is this a rustc bug?

@notgull
Copy link
Member

notgull commented Dec 16, 2023

It's not a WASM issue. When running cargo miri with portable-atomic, I see:

$ cargo miri test --features portable-atomic --tests                                                                                                                                             1 ↵
Preparing a sysroot for Miri (target: x86_64-unknown-linux-musl)... done
   Compiling concurrent-queue v2.4.0
   Compiling event-listener v4.0.0 (/home/jtnunley/Projects/smol-rs/event-listener)
    Finished test [unoptimized + debuginfo] target(s) in 0.30s
     Running unittests src/lib.rs (/hard_data/cargo_target/miri/x86_64-unknown-linux-musl/debug/deps/event_listener-4ce458e23bbc5c7c)

running 2 tests
test sys::tests::drop_non_notified ... ok
test sys::tests::insert ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running tests/notify.rs (/hard_data/cargo_target/miri/x86_64-unknown-linux-musl/debug/deps/notify-0cd1216e34824160)

running 9 tests
test drop_non_notified ... ok
test drop_notified ... ok
test drop_notified2 ... ok
test drop_notified_additional ... ok
test notify ... ok
test notify_additional ... ok
test notify_all ... ok
test notify_all_fair ... ok
test notify_one ... ok

test result: ok. 9 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

╭─jtnunley@skynet ~/Projects/smol-rs/event-listener  ‹fix-pa*› 
╰─➤  cargo miri test --no-default-features --features portable-atomic --tests
Preparing a sysroot for Miri (target: x86_64-unknown-linux-musl)... done
    Finished test [unoptimized + debuginfo] target(s) in 0.02s
     Running unittests src/lib.rs (/hard_data/cargo_target/miri/x86_64-unknown-linux-musl/debug/deps/event_listener-4aa3fed921231326)

running 6 tests
test sys::tests::listener_slab_notify ... ok
test sys::tests::listener_slab_notify_prop ... ok
test sys::tests::listener_slab_register ... ok
test sys::tests::smoke_listener_slab ... ok
test sys::tests::smoke_mutex ... ok
test sys::tests::uncontended_inner ... ok

test result: ok. 6 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

     Running tests/notify.rs (/hard_data/cargo_target/miri/x86_64-unknown-linux-musl/debug/deps/notify-1f52e7e35b88ed56)

running 9 tests
test drop_non_notified ... error: Undefined Behavior: constructing invalid value: encountered an unaligned reference (required 128 byte alignment but found 16)
   --> /home/jtnunley/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/src/rust/library/core/src/ptr/mut_ptr.rs:367:57
    |
367 |         if self.is_null() { None } else { unsafe { Some(&*self) } }
    |                                                         ^^^^^^ constructing invalid value: encountered an unaligned reference (required 128 byte alignment but found 16)
    |
    = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
    = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
    = note: BACKTRACE:
    = note: inside `std::ptr::mut_ptr::<impl *mut event_listener::Inner<()>>::as_ref::<'_>` at /home/jtnunley/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/src/rust/library/core/src/ptr/mut_ptr.rs:367:57: 367:63
note: inside `event_listener::Event::try_inner`
   --> /home/jtnunley/Projects/smol-rs/event-listener/src/lib.rs:443:18
    |
443 |         unsafe { inner.as_ref() }
    |                  ^^^^^^^^^^^^^^
note: inside `event_listener::Event::notify::<i32>`
   --> /home/jtnunley/Projects/smol-rs/event-listener/src/lib.rs:422:30
    |
422 |         if let Some(inner) = self.try_inner() {
    |                              ^^^^^^^^^^^^^^^^
note: inside `drop_non_notified`
   --> tests/notify.rs:141:16
    |
141 |     assert_eq!(event.notify(1), 1);
    |                ^^^^^^^^^^^^^^^
note: inside closure
   --> tests/notify.rs:134:23
    |
133 | #[test]
    | ------- in this procedural macro expansion
134 | fn drop_non_notified() {
    |                       ^
    = note: this error originates in the attribute macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

error: aborting due to previous error

error: test failed, to rerun pass `--test notify`

Caused by:
  process didn't exit successfully: `/home/jtnunley/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/bin/cargo-miri runner /hard_data/cargo_target/miri/x86_64-unknown-linux-musl/debug/deps/notify-1f52e7e35b88ed56` (exit status: 1)
note: test exited abnormally; to see the full output pass --nocapture to the harness.

I only see this with the combination of --no-default-features and --features portable-atomic.

@notgull
Copy link
Member

notgull commented Dec 16, 2023

The root issue is taiki-e/portable-atomic#137

@taiki-e
Copy link
Collaborator Author

taiki-e commented Dec 16, 2023

Fixed in portable-atomic-util 0.1.4. Thanks @notgull for investigating and fixing this.

@taiki-e taiki-e closed this as completed Dec 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants