mirror of
https://github.com/fluencelabs/wasm-bindgen
synced 2025-03-16 10:10:52 +00:00
This commit fixes an erroneous use-after-free which can happen in erroneous situations in JS. It's intended that if you invoke a closure after its environment has been destroyed that you'll immediately get an error from Rust saying so. The JS binding generation for mutable closures, however, accidentally did not protect against this. Each closure has an internal reference count which is incremented while being invoked and decremented when the invocation finishes and also when the `Closure` in Rust is dropped. That means there's two branches where the reference count reaches zero and the internal pointer stored in JS needs to be set to zero. Only one, however, actually set the pointer to zero! This means that if a closure was destroyed while it was being invoked it would not correctly set its internal pointer to zero. A further invocation of the closure would then pass as seemingly valid pointer into Rust, causing a use-after-free. A test isn't included here specifically for this because our CI has started failing left-and-right over this test, so this commit will hopefully just make our CI green!