eigerco / lumina

Wasm friendly Celestia light node implementation in Rust
Apache License 2.0
114 stars 32 forks source link

Eliminate JS API footgun by making sure we don't invalidate the variables passed across js-wasm bound #262

Open fl0rek opened 5 months ago

fl0rek commented 5 months ago

Currently, WasmNode expects to take ownership over the config when it's passed from JS. Nothing stops JS from still holding the config reference it has though, which causes an exception on access.

fl0rek commented 5 months ago

Another observation: &mut borrows can also be problematic, as they can allow concurrent rw borrows from js, and these can only be checked at runtime.

Consider the example

struct Foo;
impl Foo {
  async fn wait(&mut self) {
    // do some computation
  }
}
let foo = new Foo();
foo.wait();
foo.wait();

Since second wait call will usually happen before the first promise is resolved, Foo is still mutably borrowed there. This results in runtime error from bindgen:

Uncaught Error: recursive use of an object detected which would lead to unsafe aliasing in rust

This case is most easily tiggered with async fn which borrows &mut (as in example), but generally can happen anywhere where looser js rules violate Rust's single mutable borrow rule.