servo / rust-mozjs

DEPRECATED - moved to servo/mozjs instead.
Mozilla Public License 2.0
293 stars 122 forks source link

Segfault when calling JS_GetPendingException #367

Closed Fiveside closed 7 years ago

Fiveside commented 7 years ago

I'm playing around with the mozjs rust bindings, but I'm getting a segmentation fault when I run a script that throws an exception when it runs.

fn main() {
    let rt = Runtime::new().unwrap();
    let cx = rt.cx();

    unsafe {
        rooted!(in(cx) let global =
            JS_NewGlobalObject(cx, &SIMPLE_GLOBAL_CLASS, ptr::null_mut(),
                               OnNewGlobalHookOption::FireOnNewGlobalHook,
                               &CompartmentOptions::default())
        );

        let script = "throw new Error('foobar');";

        rooted!(in(cx) let mut rval = UndefinedValue());
        let result = rt.evaluate_script(global.handle(), script, "test", 1, rval.handle_mut());
        assert!(result.is_err());

        rooted!(in(cx) let mut exval = UndefinedValue());
        println!("Looking for errors");

        // Crash happens here
        let ok = JS_GetPendingException(cx, exval.handle_mut());
        println!("JS_GetPendingException returned {}", ok);
    }
}

The equivalent C++ seems to run fine, but this just crashes. What is the correct way to inspect an error that occurred during initial script execution?

jdm commented 7 years ago

In general, the first step when investigating odd behaviour with rust-mozjs is to enable the debugmozjs cargo feature and use a non-release build. I suspect the error you will see will complain that you are not in a compartment; you can avoid this by adding something like let _ac = AutoCompartment::new(cx, global.get());.

Fiveside commented 7 years ago

Aha! I had misunderstood the difference between compartments and rooted values. Re-reading the documentation cleared that up for me. Yes, switching compartments did fix the problem. Unfortunately, enabling debugmozjs and running without the compartment still caused a segmentation fault. Once I cleared that up however, I have much more descriptive debugging messages.

Thanks for your help!