Skip to content

Validation error: Trying to access parent frame stack values. #199

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

Closed
DarrenTsung opened this issue May 17, 2018 · 9 comments
Closed

Validation error: Trying to access parent frame stack values. #199

DarrenTsung opened this issue May 17, 2018 · 9 comments

Comments

@DarrenTsung
Copy link

Hi, I'm getting a super strange error with wasm-bindgen that I wasn't seeing in the past few days.

Basically when I'm pushing a boxed trait object to some storage (a Vec<Box<_>>), wasm-bindgen fails with:

error: failed to create wasmi module
	caused by: Validation: Function #165 validation error: Trying to access parent frame stack values.

It looks like it's running the output wasm file through an interpreter to figure out the signature of the exposed functions(?).

Here's the minimal lib.rs I'm managed to compile this down to:

#![feature(proc_macro, wasm_custom_section, wasm_import_module)]

use std::boxed::Box;
use std::cell::RefCell;
use std::rc::Rc;

extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn greet(mut holder: TestTraitHolder) {
    let test_struct = TestStruct::new();
    let boxed = Box::new(test_struct);

    // If you comment this out, no errors from wasm-bindgen.
    holder.hold.push(boxed);
}

#[wasm_bindgen]
pub struct TestTraitHolder {
    hold: Vec<Box<TestTrait>>,
}

#[wasm_bindgen]
pub struct TestStruct {
    a: Rc<RefCell<u32>>,
    f: Rc<RefCell<ButtonState>>,
}

#[wasm_bindgen]
impl TestStruct {
    pub fn new() -> TestStruct {
        TestStruct {
            a: Rc::new(RefCell::new(0)),
            f: Rc::new(RefCell::new(ButtonState { hovered: true, clicked_in: false, clicked: true, })),
        }
    }
}

#[derive(Clone)]
struct ButtonState {
    pub hovered: bool,
    pub clicked_in: bool,
    // If you remove one of these fields, no errors from wasm-bindgen.
    pub clicked: bool,
}

pub trait TestTrait {
    fn hello(&self);
}

impl TestTrait for TestStruct {
    fn hello(&self) {
        // If you comment this out OR change `self.f` to `self.a`, no errors from wasm-bindgen
        let _hi = self.f.borrow().clone();
    }
}

The way I tested this was to create a new hello-world project from the README and sub in this lib.rs and run:
cargo build --target wasm32-unknown-unknown && wasm-bindgen target/wasm32-unknown-unknown/debug/wasm_validation_64.wasm --out-dir .

There's also a lot of weird ways to make this go through wasm-bindgen without complaining, see the comments. Does anyone know why this might be happening?

@alexcrichton
Copy link
Contributor

Thanks for the report! This may be an LLVM bug or a bug somewhere else, but unfortunately I can't seem to reproduce :(

Can you create a standalone repo with a script to reproduce this?

@DarrenTsung
Copy link
Author

Thanks for the reply @alexcrichton! Hmm, wonder why it's not reproducing for you. I'm working on a WASM/Rust project and have been using this pattern for at least a week, but I just started encountering this bug.

Here's the standalone repo with information on how to reproduce:
https://github.com/DarrenTsung/wasm-validation-64

Includes my wasm-bindgen version / rustc version, etc. Let me know if you think of any other useful information!

@DarrenTsung
Copy link
Author

DarrenTsung commented May 18, 2018

This is not that useful, but I went back in the wasm-bindgen commit history to see if I could find any versions that work with the example. Once I got to bump 0.2.2 commit, it worked, but the next commit it didn't (3305621). This is because this is the first commit that added the wasmi project for validation / type inference(?).

Anyways, that wasn't the version of wasm-bindgen I was previously using (as a lot of the flags weren't recognized), so it seems like wasm-bindgen isn't the issue here. The likely culprit is the version of nightly I'm running.

@jsheard
Copy link
Contributor

jsheard commented May 18, 2018

Oh yeah, I had this problem as well but forgot to post an issue about it.

Rolling back to rustc e5f80f2a4 2018-05-09 resolved the problem for me.

@DarrenTsung
Copy link
Author

Thanks @jsheard! That helped make the search a lot faster :).

Installed a previous nightly version with:
rustup toolchain install nightly-2018-05-09

Rolling back to that version with override in my project directory:
rustup override set nightly-2018-05-09

Confirmed that it works with the latest wasm-bindgen (0.2.10) for my project as well as the standalone repo.

@shepmaster
Copy link

Here's a short example that also has the problem, although it does pull in quite a lot of crates.

#![feature(proc_macro, wasm_custom_section, wasm_import_module)]

extern crate wasm_bindgen;
extern crate image;

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn read_img() {
    image::load_from_memory(&[]);
}
$ wasm-bindgen ./imaj.wasm.gc --out-dir=.
error: failed to create wasmi module
	caused by: Validation: Function #739 validation error: Trying to access parent frame stack values.

Here's the generated, garbage-collected WASM:

repro.wasm.zip

  • rustc 1.27.0-nightly (acd3871ba 2018-05-10)
  • wasm-bindgen 0.2.10

If I roll back to nightly-2018-05-09, I do not see the problem. If I compile in release mode, I do not see the problem.

DarrenTsung added a commit to DarrenTsung/wasm-rgame-tools that referenced this issue May 19, 2018
This is because of this issue:
rustwasm/wasm-bindgen#199

Which was introduced in the rustc nightly version sometime
after 2018-05-09.
@alexcrichton
Copy link
Contributor

Hm ok thanks for the file @shepmaster! Attempting to run that file through tools like wasm-dis or wasm2wat produces quite a lot of errors, so I think that may just flat out be a corrupted wasm file. That would point towards probably an LLVM or an LLD bug

@alexcrichton
Copy link
Contributor

Hm I still can't reproduce though and actually generate a corrupt wasm file myself. Would someone be willing to make something like a docker image which reproduces this?

@alexcrichton
Copy link
Contributor

Ah ok I was able to reproduce with the instructions in rust-lang/rust#50869 and get an upstream LLVM bug, so I'm going to close this in favor of the rust-lang/rust issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants