Skip to content

Wasm decoding failed: table index X out of bounds #753

Closed

Description

@19h

Hello,

I'm having an issue when trying to call a declared C extern (setInterval) with a closure and building the library in release (-C lto) mode.

Cargo.toml:

[dependencies]
wasm-bindgen = "0.2.17"
wee_alloc = "0.4.2"

[lib]
crate-type = ["cdylib"]

[profile]

[profile.release]
lto = true
opt-level = "s"
panic = "abort"

lib.rs:

#![no_std]

#![feature(
    alloc_error_handler,
    alloc,
    core_intrinsics,
    custom_attribute,
    lang_items,
    panic_implementation,
)]

extern crate alloc;
extern crate wee_alloc;

#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

extern crate js_sys;
extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen]
    pub fn setInterval(cb: &Closure<FnMut()>, millis: i32) -> i32;
}

#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str);
}

#[wasm_bindgen]
pub struct Julia {
    width: i32,
    height: i32
}

#[wasm_bindgen]
impl Julia {
    #[wasm_bindgen(constructor)]
    pub fn new (width: i32, height: i32) -> Julia {
        Julia {
            width,
            height
        }
    }

    pub fn print(&self) {
        fn handler () {
            log("test");
        }

        setInterval(&Closure::new(handler), 1_000);
    }
}

makefile:

all:
	cargo build --target=wasm32-unknown-unknown --release \
	&& wasm-bindgen target/wasm32-unknown-unknown/release/lib.wasm --out-dir out/ \
	&& wasm-opt -O3 out/lib_bg.wasm -o out/main.wasm

The build doesn't produce a corrupted wasm binary when the handler definition and setInterval invocation from print(self: &Julia) is removed.

Any idea why this can happen?

I didn't find any information on “table index” and wasm2wat (all binaryen tools, actually) refused to produce a wat output: 0000241: error: invalid export table index: 99.

Activity

  1. 19h commented on Aug 26, 2018

    @19h
    Author

    Addendum: I'm not exactly sure, but it seems to be related to the webpack build process (I expected the wasm file not to change).

    File produced through cargo -> wasm-bindgen -> wasm-opt: ares_bg.wasm.zip

    File produced through webpack from the wasm-opt artifact: db7d1aa3f3ce8a64a0a3.wasm.zip

    What kind of aggressive optimization is the webpack build doing? 😕

  2. 19h commented on Aug 26, 2018

    @19h
    Author

    Here's a binary diff of what the webpack build optimization strips from the original wasm-opt artifact:

    screenshot 2018-08-26 at 17 15 23

  3. xtuc commented on Aug 26, 2018

    @xtuc
    Contributor

    What kind of aggressive optimization is the webpack build doing?

    Quite a few - https://github.com/webpack/webpack/blob/master/lib/wasm/WebAssemblyGenerator.js.


    After a quick look at the binary, it seems that some strings are not correctly encoded and the table error you get is a side-effect.

    webpack/webpack#7732 could fix it.

  4. 19h commented on Aug 26, 2018

    @19h
    Author

    Hang on, I'll merge your webpack tree and report back.

  5. 19h commented on Aug 26, 2018

    @19h
    Author

    Removing the preprocess invocation and updating all webassembly-dependencies of webpack to ^1.7.5 did not fix this issue. I will dig deeper and report.

  6. 19h commented on Aug 26, 2018

    @19h
    Author

    Revising my previous statement: cleaning the workspace and the re-running the build with your tree does indeed fix the issue.

  7. alexcrichton commented on Aug 26, 2018

    @alexcrichton
    Contributor

    Ok great! Sounds like this was an upstream webpack issue? If that's the case I'm gonna close, but if it's still an issue here I can certainly reopen!

  8. xtuc commented on Aug 27, 2018

    @xtuc
    Contributor

    Cool! I'll put more pressure to get that merged/released, but need to also update it. If you subscribe to the PR, you'll follow that.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions