rusqlite
rusqlite copied to clipboard
disk I/O error when doing queries with WASI + Wasmer-JS
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
This is where the error returned https://github.com/rusqlite/rusqlite/blob/a01d1c5f7d3ed56406e142eba67120b20f3808f6/libsqlite3-sys/sqlite3/wasm32-wasi-vfs.c#L236
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.
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
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
.
It might be a bug in wasm32-wasi-vfs.c
then.
I also encountered this error, but in the browser environment, what could be wrong with this?
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?
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/ca4ddfefc1be1afbCan 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.