wasmerio / wasmer

🚀 The leading Wasm Runtime supporting WASIX, WASI and Emscripten
https://wasmer.io
MIT License
18.25k stars 772 forks source link

Runtime crashing with `RuntimeError: out of bounds memory access` #4151

Open dynamite-bud opened 11 months ago

dynamite-bud commented 11 months ago

Context

The issue was discovered while making the news-scraper for wasmer edge.

https://github.com/wasix-org/wasix-rust-examples/tree/01d27bd5b2910bb3e4b97561f94c6e25fc183e7d/news-web-scraper

The problem:

I used a blocking reqwest call in tokio's async without std::thread::spawn. This should give an error when running. But it does build and run successfully with the target wasm32-wasmer-wasi.

https://github.com/wasix-org/wasix-rust-examples/blob/01d27bd5b2910bb3e4b97561f94c6e25fc183e7d/news-web-scraper/src/main.rs#L8-L20

https://github.com/wasix-org/wasix-rust-examples/blob/01d27bd5b2910bb3e4b97561f94c6e25fc183e7d/news-web-scraper/src/news_scraper.rs#L52-L55

Steps to reproduce:

Running with native cargo run:

PORT=3000 cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.09s
     Running `target/debug/news-scraper`
Listening on 127.0.0.1:3000

Try to curl it:

curl localhost:3000                                                                                                             
curl: (52) Empty reply from server

while in the project's terminal:

thread 'tokio-runtime-worker' panicked at 'Cannot drop a runtime in a context where blocking is not allowed. This happens when a runtime is dropped from within an asynchronous context.', /Users/xorcist/.cargo/git/checkouts/tokio-2eb71367495b4da5/2be2588/tokio/src/runtime/blocking/shutdown.rs:51:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

But the same behaviour doesn't exist in wasm32-wasmer-wasi / WASIX:

Running with wasmer:

wasmer run target/wasm32-wasmer-wasi/release/news-scraper.wasm --net --env PORT=3000
Listening on 127.0.0.1:3000

And trying to curl it:

curl localhost:3000                                                                                                                          

 Hacker News

1. Things you forgot because of React
    134 points by inner_square 2 hours ago | 90 comments
    https://joshcollinsworth.com/blog/antiquated-react

It shows the news! But if you curl it again:

curl localhost:3000

It won't work!, just keeps on waiting.

The trace for the second curl request in the project's terminal shows:

2023-08-15T11:53:33.875879Z TRACE ThreadId(11) epoll_wait: wasmer_wasix::syscalls::wasix::epoll_wait: triggered fd=17 readiness=EPOLLIN | EPOLLRDHUP | EPOLLET
2023-08-15T11:53:33.875910Z TRACE ThreadId(11) epoll_wait: wasmer_wasix::syscalls::wasix::epoll_wait: 1 events triggered
2023-08-15T11:53:33.875913Z TRACE ThreadId(11) epoll_wait: wasmer_wasix::syscalls::wasix::epoll_wait: return=Errno::success
2023-08-15T11:53:33.875916Z TRACE ThreadId(11) epoll_wait: wasmer_wasix::syscalls::wasix::epoll_wait: close time.busy=54.6µs time.idle=249ns
2023-08-15T11:53:33.875926Z TRACE ThreadId(11) clock_time_get: wasmer_wasix::syscalls::wasi::clock_time_get: enter clock_id=Snapshot0Clockid::Monotonic precision=1
2023-08-15T11:53:33.875930Z TRACE ThreadId(11) clock_time_get: wasmer_wasix::syscalls::wasi::clock_time_get: return=Errno::success clock_id=Snapshot0Clockid::Monotonic precision=1
2023-08-15T11:53:33.875973Z TRACE ThreadId(11) clock_time_get: wasmer_wasix::syscalls::wasi::clock_time_get: close time.busy=46.2µs time.idle=167ns clock_id=Snapshot0Clockid::Monotonic precision=1
2023-08-15T11:53:33.875980Z TRACE ThreadId(11) epoll_wait: wasmer_wasix::syscalls::wasix::epoll_wait: enter
2023-08-15T11:53:33.875983Z TRACE ThreadId(11) epoll_wait: wasmer_wasix::syscalls::wasix::epoll_wait: waiting forever on wakers maxevents=1024 epfd=16
2023-08-15T11:53:33.877854Z TRACE ThreadId(13) wasmer_wasix::runtime::task_manager::tokio: task_wasm started in blocking thread
2023-08-15T11:53:33.877947Z DEBUG ThreadId(13) wasmer_wasix::syscalls::wasix::thread_spawn: failed with runtime error: RuntimeError: out of bounds memory access
2023-08-15T11:53:33.877952Z TRACE ThreadId(13) wasmer_wasix::syscalls::wasix::thread_spawn: callback finished (ret=noexec (error 45))
2023-08-15T11:53:33.877955Z TRACE ThreadId(13) wasmer_wasix::state::func_env: wasi[1:6]::cleanup

The important thing to see here:

2023-08-15T11:53:33.877947Z DEBUG ThreadId(13) wasmer_wasix::syscalls::wasix::thread_spawn: failed with runtime error: RuntimeError: out of bounds memory access

royaljain commented 9 months ago

Facing the same issue.

Hi @Michael-F-Bryan - did you work on this further?

@dynamite-bud were you able to solve this?