Refactor __wasi_fd_write in wasmfs.cpp - #15147
Conversation
|
This error occurs in tests when |
| extern "C" { | ||
|
|
||
| int emscripten_wasmfs_printbuffer(__wasi_fd_t fd, const uint8_t* ptr, __wasi_size_t len); | ||
| std::string buffer; |
There was a problem hiding this comment.
Could this be a std::vector<char>? That would avoid including code for string support.
There was a problem hiding this comment.
Would this also not bring in libc++ as well if vector is part of stdlib?
There was a problem hiding this comment.
wasm-ld: error: emscripten/cache/sysroot/lib/wasm32-emscripten/libwasmfs.a(wasmfs.o): undefined symbol: typeinfo for std::length_error
wasm-ld: error: emscripten/cache/sysroot/lib/wasm32-emscripten/libwasmfs.a(wasmfs.o): undefined symbol: vtable for std::length_error
There was a problem hiding this comment.
error: undefined symbol: _ZNKSt3__220__vector_base_commonILb1EE20__throw_length_errorEv (referenced by top-level compiled C/C++ code)
There was a problem hiding this comment.
wasm-ld: error: emscripten/cache/sysroot/lib/wasm32-emscripten/libwasmfs.a(wasmfs.o): undefined symbol: operator delete(void*)
wasm-ld: error: emscripten/cache/sysroot/lib/wasm32-emscripten/libwasmfs.a(wasmfs.o): undefined symbol: std::__2::__vector_base_common<true>::__throw_length_error() const
wasm-ld: error: emscripten/cache/sysroot/lib/wasm32-emscripten/libwasmfs.a(wasmfs.o): undefined symbol: operator new(unsigned long)
There was a problem hiding this comment.
Doesn't simply building without -nostdlib++ fix that?
There was a problem hiding this comment.
Yes, I didn't use both. I was just trying out Alon's suggestion to use -fno-exceptions to see if the throw symbol warning would be resolved
There was a problem hiding this comment.
I'd be ok with only doing this when WASMFS was not in settings.
Would you be ok, though, if in the long term we needed to keep passing some C++ stdlibs to the linker, always? When WASMFS becomes the default, that will be required, if we allow C++ in WASMFS itself.
There was a problem hiding this comment.
Yes, that is a little unfortunate. I was hoping we could so something like:
if settings.WASMFS:
settings.DEFAULT_TO_CXX=1
But if that means pretty much all users will get DEFAULT_TO_CXX.. that is a little unfortunate. Removing libc++/libstdc++ from C programs is more of a nice-to-have.. its really not that important. If we can write WASMFS in C that would be good but it seems like that would slow us down quite lot, right? Probably not worth it.
There was a problem hiding this comment.
Yes, there are tradeoffs here... I think writing WASMFS using modern C++ would be good (unique_ptr, etc. would avoid a lot of bugs). But it does mean that turning WASMFS on by default would mean DEFAULT_TO_CXX everywhere.
For completeness, we've also thrown around the idea of writing WASMFS in Rust or another language that would be better than C. But the build system integration work for that would be pretty large, sadly.
If you're ok with this then I think we can proceed here. But we should also look to see if we can avoid DEFAULT_TO_CXX somehow. I wonder if we can bundle the C++ requirements of WASMFS inside libwasmfs - those should be very few and very trivial, things like stubs for new/delete that just call malloc/free, etc. We'd avoid practically all C++ that uses stuff that is not in headers.
|
Hopefully we can avoid that error by not using a Using header-only stuff like |
tlively
left a comment
There was a problem hiding this comment.
Code LGTM besides comments. I am also ok with linking in C++ library stuff as necessary. The benefits of using C++ are worth the costs (although we don't have a great measurement of the costs).
|
@ethanalee do you have an idea of how much extra code |
IIUC, almost of STL is implemented purely in headers.. its only a few things like new and delete that come in from libraries. Note that adding |
For For For |
| if compiler[0] == EMCC: | ||
| if compiler[0] == EMCC and not self.get_setting('WASMFS'): | ||
| # TODO change test behaviour in the future when WASMFS becomes default file system | ||
| # WASMFS is excluded here since it currently requires stdlib++ functions |
There was a problem hiding this comment.
This kind of worries me, to be honest. It seems like we might end up forgetting it, and then users would hit problems that don't occur in the test suite. That's a risk when working around issues in the test suite like this.
One possible idea is to move this to emcc.py, using the flag @sbc100 mentioned to link as cxx. We'll need that anyhow I think. However, I'm also ok with landing this if we put that on a TODO somewhere we are sure we won't forget.
There was a problem hiding this comment.
You mean using -s DEFAULT_TO_CXX=0 here instead of -nostdlib++? Sure we could try that. I'll open a PR to do that.
There was a problem hiding this comment.
(Maybe what you meant, but to be sure I meant not here and instead in emcc.py - then it will work outside of the test suite too.)
There was a problem hiding this comment.
Are you suggesting we move forward with changing the default to DEFAULT_TO_CXX=0 for all users? This is the plan as layed out in #11121 but it could be a breaking change for many users and I'd like to start by issuing a warning.
The point of this line here is to opt into this new stricter behaviour within the test suite before we apply it to all users.
There was a problem hiding this comment.
No, sorry, I mean to add something like this in emcc.py:
if WASMFS:
DEFAULT_TO_CXX = 1That would affect no one but a few tests atm.
There was a problem hiding this comment.
Yes that sounds like the correct solution.
There was a problem hiding this comment.
We can land this as is for now. I will keep the todo and add this setting in the following PR
| if compiler[0] == EMCC: | ||
| if compiler[0] == EMCC and not self.get_setting('WASMFS'): | ||
| # TODO change test behaviour in the future when WASMFS becomes default file system | ||
| # WASMFS is excluded here since it currently requires stdlib++ functions |
This way WasmFS can be used outside the test suite as well. Followup to #15147
__wasi_fd_writeto only use C++emscripten_console_loginstead of calling out to JSAddresses Jukka's comment in #15104