nodejs / uvwasi

WASI syscall API built atop libuv
MIT License
226 stars 50 forks source link

O_LARGEFILE compiled with wasi-sdk being treated as UVWASI_O_TRUNC in uvwasi #169

Closed mean-ui-thread closed 2 years ago

mean-ui-thread commented 2 years ago

Whenever O_LARGEFILE flag is being used when opening a file stream for readonly, the file gets truncated: data of the file is wiped out because this gets hit:

https://github.com/nodejs/uvwasi/blob/44542cccbdccb2d322d0a2759c2ec6d158bc2c56/src/uvwasi.c#L1919-L1920

It is caused by this code here in wasi-libc:

https://github.com/WebAssembly/wasi-libc/blob/cab0ec601e0d3b1b6eb1be9d76f36aad5507998b/libc-bottom-half/cloudlibc/src/libc/fcntl/openat.c#L79

which makes O_LARGEFILE (0x8000) become 0x0008 after the bit shift, which is the value of UVWASI_O_TRUNC. If I understand wasi-lib's openat code correctly, the real O_TRUNC flag (0x200 I believe) would appear in fs_flags, not in o_flags... I am not sure if the bug is on wasi-libc's side or on uvwasi's side. If the mistake is on wasi-libc's side, please let me know and I will re-create this issue on their side instead.

Thanks!

mean-ui-thread commented 2 years ago

Ok, I made a mistake. I accidentally used the emscripten sysroot with the wasi-sdk compiler, and the values are completely different, which is how the O_TRUNC == O_LARGEFILE confusion came from.

wasi-libc has:

wasi-sysroot/include/__header_fcntl.h:#define O_TRUNC (__WASI_O_TRUNC << 12)

emscripten has:

upstream/emscripten/cache/sysroot/include/bits/fcntl.h:#define O_LARGEFILE 0100000

Sorry about that. I will close the issue right away.