rusqlite icon indicating copy to clipboard operation
rusqlite copied to clipboard

disk I/O error when doing queries with WASI + Wasmer-JS

Open sefeng211 opened this issue 3 years ago • 8 comments

Hi,

I am trying to use preopens to provide file access to WebAssembly, and it works fine to create a connection with the given database file. However, whenever I try to do a query, for instance a simple select query, I get this database error SqliteFailure(Error { code: SystemIOFailure, extended_code: 266 }, Some("disk I/O error")) .

On the JavaScript side, I specified preopens:{'/': __dirname} to provide access to the current directory, and placed database file under this directory. I've also tested to use std::fs::File to write and read dummy test files which worked fine.

cc @TjeuKayim

sefeng211 avatar Dec 25 '20 21:12 sefeng211

This is where the error returned https://github.com/rusqlite/rusqlite/blob/a01d1c5f7d3ed56406e142eba67120b20f3808f6/libsqlite3-sys/sqlite3/wasm32-wasi-vfs.c#L236

sefeng211 avatar Dec 25 '20 22:12 sefeng211

Here's the rust code https://paste.mozilla.org/ErP53XxY#L13 and here's Wasmer-JS code https://paste.mozilla.org/OhqhvUtn.

Note that the rust code works fine with wasmtime, so this is Wasmer-JS specific.

sefeng211 avatar Dec 26 '20 05:12 sefeng211

What browser are you using? Your Javascript contains: require("@wasmer/wasi/lib/bindings/node") Replace that with the browser bindings as explained here https://docs.wasmer.io/integrations/js/wasi/browser/examples/hello-world

TjeuKayim avatar Dec 26 '20 06:12 TjeuKayim

That is nodejs actually. I've been following this example https://docs.wasmer.io/integrations/js/wasi/server/examples/hello-world. I basically just reused this example.

The way how I compiled the rust code was RUSTFLAGS='-C target-feature=-crt-static' cargo build --target wasm32-wasm.

sefeng211 avatar Dec 26 '20 07:12 sefeng211

It might be a bug in wasm32-wasi-vfs.c then.

TjeuKayim avatar Dec 26 '20 13:12 TjeuKayim

I also encountered this error, but in the browser environment, what could be wrong with this?

passchaos avatar Mar 01 '21 06:03 passchaos

Hm, that function demoRead that returns the error is not exactly the same as in the sqlite sources. I used a version before 2019 when this fix was committed: https://www3.sqlite.org/cgi/src/info/ca4ddfefc1be1afb

Can you add these lines:

   }else if( nRead>=0 ){
+    if( nRead<iAmt ){
+      memset(&((char*)zBuf)[nRead], 0, iAmt-nRead);
+    }

And try to see if it solves your issue?

TjeuKayim avatar Mar 02 '21 11:03 TjeuKayim

Hm, that function demoRead that returns the error is not exactly the same as in the sqlite sources. I used a version before 2019 when this fix was committed: https://www3.sqlite.org/cgi/src/info/ca4ddfefc1be1afb

Can you add these lines:

   }else if( nRead>=0 ){
+    if( nRead<iAmt ){
+      memset(&((char*)zBuf)[nRead], 0, iAmt-nRead);
+    }

And try to see if it solves your issue?

After adding this change, it still can't work with wasmer-js on the browser, but it works normally when I use wasm32-wasi-vfs feature on the pc platform, even without the above repair, I think it may be a problem with wasmer-js.

passchaos avatar Mar 02 '21 11:03 passchaos