-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Readdir doesn't correctly handle capacity updates #2618
Comments
Subscribe to Label Actioncc @kubkon
This issue or pull request has been labeled: "wasi"
Thus the following users have been cc'd because of the following labels:
To subscribe or unsubscribe from this label, edit the |
The bug is indeed in Wasmtime's WASI implementation. Our I have a fix. |
Previously, `fd_readdir` was truncating directory entry names based on the calculation of `min(name_len, buf_len - bufused)`, but `bufused` was not being updated after writing in the `dirent` structure to the buffer. This allowed `bufused` to be incremented beyond `buf_len` and returned as the number of bytes written to the buffer, which is invalid. This fix adjusts `bufused` when the buffer is written to for the `dirent` so that name truncation happens as expected. Fixes bytecodealliance#2618.
@RReverser would you mind trying the fix in #2620 and verify it addresses this issue? Thanks! |
Previously, `fd_readdir` was truncating directory entry names based on the calculation of `min(name_len, buf_len - bufused)`, but `bufused` was not being updated after writing in the `dirent` structure to the buffer. This allowed `bufused` to be incremented beyond `buf_len` and returned as the number of bytes written to the buffer, which is invalid. This fix adjusts `bufused` when the buffer is written to for the `dirent` so that name truncation happens as expected. Fixes bytecodealliance#2618.
As long as the sample above works, I trust it's fine. |
I'd suggest to add it as a regression test to the PR though. |
Agreed that there should be a regression test here, but it is a little difficult to mock up currently as it isn't directly related to the number of directory entries, just that the (unstable order of) entries being returned doesn't neatly fit in the buffer given to The WASI test programs use a real file system underneath, so we might be able to create a repro test relying on filesystem-specific behavior, perhaps. The reason I wanted to confirm it addresses what you're seeing is that |
Actually, there's some virtualized filesystem support in the WASI test-programs I'm completely unfamiliar with. I'll see if we can use this to properly test this for regression. |
Hmm, it appears that
It should be asserting that |
The virtualized filesystem support currently only works for files, not directories. I'm nearly ready to land @sunfishcode's and my lengthy rewrite of wasi-common, and will need to backport the fix for this into my branch. The virtualized filesystem support in the rewrite is much better and we should be able to synthesize this test, or even fuzz this interface, since we keep on getting it wrong! |
I might also suggest checking out WebAssembly/WASI#9 (comment) thread where @caspervonb has been working on a shared WASI testsuite. It seems like Wasmtime might benefit from running the same tests, and, OTOH, the testsuite could include examples like this one with large-ish directories (should be just a matter of adding more files to the |
Agreed, we'll be contributing the wasmtime tests to that suite and getting the rest of it run as part of wasmtime's CI soon :) |
Previously, `fd_readdir` was truncating directory entry names based on the calculation of `min(name_len, buf_len - bufused)`, but `bufused` was not being updated after writing in the `dirent` structure to the buffer. This allowed `bufused` to be incremented beyond `buf_len` and returned as the number of bytes written to the buffer, which is invalid. This fix adjusts `bufused` when the buffer is written to for the `dirent` so that name truncation happens as expected. Fixes bytecodealliance#2618.
Previously, `fd_readdir` was truncating directory entry names based on the calculation of `min(name_len, buf_len - bufused)`, but `bufused` was not being updated after writing in the `dirent` structure to the buffer. This allowed `bufused` to be incremented beyond `buf_len` and returned as the number of bytes written to the buffer, which is invalid. This fix adjusts `bufused` when the buffer is written to for the `dirent` so that name truncation happens as expected. Fixes bytecodealliance#2618.
Try to compile this sample code to WASI:
Then, choose a large-ish folder - e.g. I created a temporary directory with 50 files named 0...49.
Run the produced Wasm with Wasmtime with that directory mapped to
/
:Same code against the same folder works fine with Wasmer, suggesting it's an environment issue not Rust stdlib issue:
The relevant code on the Rust side also hasn't been updated in 2 years, also confirming it's likely a runtime environment issue: https://github.com/rust-lang/rust/blob/643a79af3d5a31fa87c8a4c9d7f8bc4ebe2add4b/library/std/src/sys/wasi/fs.rs#L164
The text was updated successfully, but these errors were encountered: