orlp / slotmap

Slotmap data structure for Rust
zlib License
1.16k stars 70 forks source link

deserializing a slotmap compiled to .wasm causes error. #33

Closed rebo closed 5 years ago

rebo commented 5 years ago

Slotmap works just fine in web assembly with just one exception. It appears that deserialization with serde does not work. (Serialization works just fine).

I really have no idea if this is a problem with Slotmap, Serde, or wasm_bindgen it really is above my head.

Anyway. this code ::

        let mut sm: SlotMap<PlayerKey, u32> = SlotMap::with_key();

        sm.insert(3);

        let j = serde_json::to_string(&sm).unwrap(); // serialization works fine

        print!("{}",j );

        let sm2 : SlotMap<PlayerKey, u32> = serde_json::from_str(&j).unwrap();

causes this error only in a wasm environment ::

`panicked at 'attempt to shift left with overflow', /Users/rebo/.cargo/registry/src/github.com-1ecc6299db9ec823/slotmap-0.3.0/src/normal.rs:1068:31

Stack:

Error at Module.wbg_new_59cb74e423758ede (webpack-internal:///./crate/pkg/index.js:547:21) at wbg_new_59cb74e423758ede (http://localhost:8000/app.js?56a04bc9da773126182b:801:101) at console_error_panic_hook::Error::new::hf9155aeb30e0df7c (wasm-function[11765]:22) at console_error_panic_hook::hook_impl::h19b17601d076c52f (wasm-function[2120]:100) at console_error_panic_hook::hook::hd8298b7121f5bbfe (wasm-function[12932]:38) at core::ops::function::Fn::call::hced78bec1b5a708c (wasm-function[11784]:45) at std::panicking::rust_panic_with_hook::hefb6d16d56fedd94 (wasm-function[3093]:265) at std::panicking::continue_panic_fmt::ha605a3bfcbd9f700 (wasm-function[6701]:116) at rust_begin_unwind (wasm-function[14788]:3) at core::panicking::panic_fmt::hc16a7652262fbed2 (wasm-function[12041]:70)`

orlp commented 5 years ago

Does WebAssembly have a 32-bit usize? Now there's definitely an issue in my code that I compute 2^32 - 1 with (1 << 32) - 1 as this won't work on 32-bit systems.

However... how did this compile? This should give a compile-time error: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=f233a1f5f93c82a748d1f0d8da58e34c .

orlp commented 5 years ago

This issue should be fixed in the latest commit. It's going to take a bit (probably at least a couple days to a week) for me to publish 0.4, I have some other changes I'd like to make for that.

rebo commented 5 years ago

Thank you very much for looking at this, unfortunately the example above still panics for me on deserialize when using the latest commit on master.

I have created a small sample app that demonstrates this phenomena.

https://github.com/rebo/sm_issue

orlp commented 5 years ago

What is the panic traceback?

rebo commented 5 years ago

It's a little bit different:


RuntimeError: unreachable
    at __rust_start_panic (wasm-function[1114]:1)
    at rust_panic (wasm-function[1004]:31)
    at std::panicking::rust_panic_with_hook::hefb6d16d56fedd94 (wasm-function[246]:304)
    at std::panicking::continue_panic_fmt::ha605a3bfcbd9f700 (wasm-function[533]:116)
    at rust_begin_unwind (wasm-function[1103]:3)
    at core::panicking::panic_fmt::hc16a7652262fbed2 (wasm-function[843]:70)
    at core::panicking::panic::h1f84f7eb49aeaec9 (wasm-function[627]:100)
    at slotmap::normal::serialize::<impl serde::de::Deserialize for slotmap::normal::SlotMap<K,V>>::deserialize::hd239fa69a2c00838 (wasm-function[30]:1967)
    at serde_json::de::from_trait::hc4dda8f146982609 (wasm-function[54]:121)
    at serde_json::de::from_str::h2a6f0f9869081e1d (wasm-function[485]:93)
orlp commented 5 years ago

Alright I tracked it down, it was the exact same issue, just in two other places. I wasn't using a local repository when doing my first fix, so I used Github search in the repo to see if there were any other instances. Unfortunately these instances never showed up with that search. Could you try again with the latest commit?

rebo commented 5 years ago

Again thanks for getting to this so quickly. Looks like we are not quite there yet, here is the latest trace from a new panic than the last one (but similar to the first one lol).


index.js?91f7:408 panicked at 'attempt to shift left with overflow', /Users/rebo/Coding/Projects/Rust/stiff/slotmap/src/normal.rs:1090:31

Stack:

Error
    at Module.__wbg_new_59cb74e423758ede (webpack-internal:///./crate/pkg/index.js:547:21)
    at __wbg_new_59cb74e423758ede (http://localhost:8000/app.js?d0084421dcdfb4bedfd4:801:101)
    at console_error_panic_hook::Error::new::hf9155aeb30e0df7c (wasm-function[11764]:22)
    at console_error_panic_hook::hook_impl::h19b17601d076c52f (wasm-function[2120]:100)
    at console_error_panic_hook::hook::hd8298b7121f5bbfe (wasm-function[12931]:38)
    at core::ops::function::Fn::call::hced78bec1b5a708c (wasm-function[11783]:45)
    at std::panicking::rust_panic_with_hook::hefb6d16d56fedd94 (wasm-function[3092]:265)
    at std::panicking::continue_panic_fmt::ha605a3bfcbd9f700 (wasm-function[6701]:116)
    at rust_begin_unwind (wasm-function[14785]:3)
    at core::panicking::panic_fmt::hc16a7652262fbed2 (wasm-function[12040]:70)
nabijaczleweli commented 5 years ago

(are you sure you're on the latest commit?)

orlp commented 5 years ago

@rebo I believe you do not have the latest version as the line number in that panic message is a line I fixed.

rebo commented 5 years ago

Hi Orlp and nabijaczleweli ,

I am happy to report this works fine now. Tested under slotmap 0.4 and latest nightly.

Many thanks for fixing it so quickly.