You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 13, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Oct 13, 2023. It is now read-only.
I am using the 'release-6.0.0' branch of wasmtime and the latest commits of this repo and wit-bindgen, wasm-tools for creating a component out of my Rust application.
My host is building a Wasi Context using wasi_cap_std_sync crate from this repo as well.
Getting Around
I was able to get around this error by compiling the application to wasm32-unknown-unknown target and remove --adapt flag from wasm-tools component command. This indicates that there might be something going on in wasi and thus I want to raise this issue here.
I don't know how this hasn't cropped up sooner, and unfortunately I'm not sure how best to solve this. The error here is coming from Wasmtime itself and is reflective of the reentrance rules of the component model. The realloc function, when called, isn't allowed to "leave" the component which it's doing so here via ctors acquiring environment variables. I'm not sure what the best solution to that is.
@alexcrichton We could generalize the logic I added in #83 and bytecodealliance/wasm-tools#919 such that wit-component wraps the main module's cabi_realloc function in some code that sets the allocation_state global (and unsets it on exit) to a value that the Preview 2 adapter recognizes as meaning "short circuit any host calls". Currently we do that only when allocating the stack and adapter State, but no reason we can't do that for all cabi_realloc calls.
Does that sound reasonable? If so, I'll be happy to implement it.
This will be an issue with post-return, yeah, but #83 and related support was about updates to the adapter module but this is a case where cabi_realloc starts in the main module so technically, if we look a bit further in the future where wasi-libc is bounds to preview2 directly, the adapter can't solve this. I think it may be able to paper over things today with some really invasive surgery on the main module or the adapter (e.g. importing all reallocs/post-returns into the adapter module, doing some adaptation, and then using those as the lifting options rather than the original ones) as you mention though.
This may be a deeper issue worth talking about with @sunfishcode and @lukewagner though to see if there's a better solution here. I'm not sure how flexible this reentrancy constraint is or how flexible the env var/ctor code can be made.
I discussed this with @sunfishcode and @lukewagner today, and I think the plan going forward is to move away from wrapping each export in ctor/dtor calls and instead rely on initialization happening in an _init (for reactors) or _start (for commands) function. @sunfishcode is going to work to get that standardized and official in the appropriate forums.
So I think we can handle the ctor/dtor wrappers with the preview2 adapter and then assume there will be no such wrappers by the time wasi-libc uses preview2 directly. Does that sound reasonable?
Re relying on initialization happening in an _init for reactors - is this something the component model will support natively, i.e. this is run as part of instantiation of a component, versus being a component export function which needs to be executed before any other functions (and some external mechanism enforces this rule)?
Can you describe more what "handle the ctor/dtor wrappers with the preview2 adapter" means?
From my view (and I apologize that I came to this separately and didnt join the discussion you just had) this difficulty is driven by the adapter needing to rely on wasi-libc in order to allocate memory. This requirement is driven by needing to use "legacy" wasi-libcs that have the allocator behavior from before components or adapters were created.
If we can throw away that requirement, and require preview 1 apps be linked against a wasi-libc which won't stomp on the adapter's allocation, we can resolve this problem in a way that eliminates a lot of the complexity in the adapter. (The fixed wasi-libc allocator is available in rustc nightly, right @sunfishcode?) By "moving away from wrapping each export in ctor/dtor wrappers" we mean that some change to lld is required in order to produce modules that work with the adapter, then have we already made that leap?
Alex corrected my understanding of this issue - even if we can separate allocation from the adapter and libc, we would still run into this issue because libc is calling import functions during the call to cabi_realloc (which is being called from outside the component in preparation for passing in some lists to an export function)
How plausible is it to change wasi-libc just not call any import functions in its constructors? (Edit: I read all of Dan's comments throughout wasi-libc on environment initialization and read man 7 environ and now I am sad but resigned, there is no way out of calling import functions while initializing a working libc)
From my view (and I apologize that I came to this separately and didnt join the discussion you just had) but the lens I am looking at this is, this difficulty is driven by the adapter needing to rely on wasi-libc in order to allocate memory. This requirement is driven by needing to use "legacy" wasi-libcs that have the allocator behavior from before components or adapters were created. If we can throw away that requirement, and require preview 1 apps be linked against a wasi-libc which won't stomp on the adapter's allocation, we can resolve this problem in a way that eliminates a lot of the complexity in the adapter. (The fixed wasi-libc allocator is available in rustc nightly, right @sunfishcode?)
At Fermyon, we would prefer to have a way to convert customers' existing modules to components without rebuilding from source. If that is prohibitively difficult then we can explore other options, but I don't think that's a given at this point. Also, some toolchains, such as TeaVM, don't use wasi-libc at all, and will require modifications to accommodate a "reserved" portion of linear memory for the adapter. That could get awkward as more non-wasi-libc tools come on the scene. And as Alex pointed out, requiring the latest wasi-libc wouldn't help with this particular issue anyway.
I don't understand how this approach is viable, because short-circuiting the response to wasi-libc gives it incorrect values. How do those values later get corrected when calling imports is allowed?
I don't understand how this approach is viable, because short-circuiting the response to wasi-libc gives it incorrect values. How do those values later get corrected when calling imports is allowed?
The incorrect values will only live persist as long as the cabi_realloc call is happening. Later calls to exports will run all the constructors again, and this time they'll get the correct values. Currently, wasi-libc's constructors always assume they're being called for the first time, so there's no boolean check to see if they've already run. That happens to be to our advantage in this case.
So I think we can handle the ctor/dtor wrappers with the preview2 adapter and then assume there will be no such wrappers by the time wasi-libc uses preview2 directly. Does that sound reasonable.
That sounds reasonable to me as a stopgap, yeah. (albeit this continues to pile quite a few hacks in the adapter construction but our hand is sort of forced)
Longer-term I still feel like the picture is a bit unclear, however:
For the command-style _start this heavily depends on the signature of whatever the command entrypoint is. If it needs a post-return or a realloc it seems like the same problem all over again because how, at the component model level, does initialization happn before the realloc/etc are called?
For the reactor-style _init it's not clear to me who or what calls this _init function. I'm presuming there's an idea for a hypothetical component model feature to do this, though, hence the "unclear to me" feeling.
I know there's been a lot of historical discussion about command/reactor patterns in WASI but very little of that I feel has ended up getting surfaced into the component model itself. In that sense I find it difficult to think about commands/reactors since that's in theory a higher-level abstraction than the component model itself and the problem here in this issue is the component model's semantics. So "well it should be a reactor" or "let's fix commands" I find unclear since that's dealing with higher level semantics than the issue being run into here.
Activity
alexcrichton commentedon Feb 28, 2023
I don't know how this hasn't cropped up sooner, and unfortunately I'm not sure how best to solve this. The error here is coming from Wasmtime itself and is reflective of the reentrance rules of the component model. The
reallocfunction, when called, isn't allowed to "leave" the component which it's doing so here via ctors acquiring environment variables. I'm not sure what the best solution to that is.dicej commentedon Feb 28, 2023
@alexcrichton We could generalize the logic I added in #83 and bytecodealliance/wasm-tools#919 such that wit-component wraps the main module's cabi_realloc function in some code that sets the
allocation_stateglobal (and unsets it on exit) to a value that the Preview 2 adapter recognizes as meaning "short circuit any host calls". Currently we do that only when allocating the stack and adapterState, but no reason we can't do that for allcabi_realloccalls.Does that sound reasonable? If so, I'll be happy to implement it.
dicej commentedon Feb 28, 2023
BTW, will this be an issue for post-return functions also?
alexcrichton commentedon Feb 28, 2023
This will be an issue with post-return, yeah, but #83 and related support was about updates to the adapter module but this is a case where
cabi_reallocstarts in the main module so technically, if we look a bit further in the future where wasi-libc is bounds to preview2 directly, the adapter can't solve this. I think it may be able to paper over things today with some really invasive surgery on the main module or the adapter (e.g. importing all reallocs/post-returns into the adapter module, doing some adaptation, and then using those as the lifting options rather than the original ones) as you mention though.This may be a deeper issue worth talking about with @sunfishcode and @lukewagner though to see if there's a better solution here. I'm not sure how flexible this reentrancy constraint is or how flexible the env var/ctor code can be made.
dicej commentedon Feb 28, 2023
I discussed this with @sunfishcode and @lukewagner today, and I think the plan going forward is to move away from wrapping each export in ctor/dtor calls and instead rely on initialization happening in an
_init(for reactors) or_start(for commands) function. @sunfishcode is going to work to get that standardized and official in the appropriate forums.So I think we can handle the ctor/dtor wrappers with the preview2 adapter and then assume there will be no such wrappers by the time wasi-libc uses preview2 directly. Does that sound reasonable?
pchickey commentedon Feb 28, 2023
Re relying on initialization happening in an
_initfor reactors - is this something the component model will support natively, i.e. this is run as part of instantiation of a component, versus being a component export function which needs to be executed before any other functions (and some external mechanism enforces this rule)?Can you describe more what "handle the ctor/dtor wrappers with the preview2 adapter" means?
From my view (and I apologize that I came to this separately and didnt join the discussion you just had) this difficulty is driven by the adapter needing to rely on wasi-libc in order to allocate memory. This requirement is driven by needing to use "legacy" wasi-libcs that have the allocator behavior from before components or adapters were created.
If we can throw away that requirement, and require preview 1 apps be linked against a wasi-libc which won't stomp on the adapter's allocation, we can resolve this problem in a way that eliminates a lot of the complexity in the adapter. (The fixed wasi-libc allocator is available in rustc nightly, right @sunfishcode?) By "moving away from wrapping each export in ctor/dtor wrappers" we mean that some change to lld is required in order to produce modules that work with the adapter, then have we already made that leap?
pchickey commentedon Feb 28, 2023
Alex corrected my understanding of this issue - even if we can separate allocation from the adapter and libc, we would still run into this issue because libc is calling import functions during the call to cabi_realloc (which is being called from outside the component in preparation for passing in some lists to an export function)
pchickey commentedon Feb 28, 2023
How plausible is it to change wasi-libc just not call any import functions in its constructors? (Edit: I read all of Dan's comments throughout wasi-libc on environment initialization and read
man 7 environand now I am sad but resigned, there is no way out of calling import functions while initializing a working libc)dicej commentedon Feb 28, 2023
(I'll let Luke or Dan answer your first question, since they floated a few ideas, and I'm not totally sure where they landed).
I outlined it in my earlier comment: #97 (comment)
At Fermyon, we would prefer to have a way to convert customers' existing modules to components without rebuilding from source. If that is prohibitively difficult then we can explore other options, but I don't think that's a given at this point. Also, some toolchains, such as TeaVM, don't use wasi-libc at all, and will require modifications to accommodate a "reserved" portion of linear memory for the adapter. That could get awkward as more non-wasi-libc tools come on the scene. And as Alex pointed out, requiring the latest wasi-libc wouldn't help with this particular issue anyway.
dicej commentedon Feb 28, 2023
Probably doable for environment variables, but not sure about preopens.
pchickey commentedon Feb 28, 2023
I don't understand how this approach is viable, because short-circuiting the response to wasi-libc gives it incorrect values. How do those values later get corrected when calling imports is allowed?
dicej commentedon Feb 28, 2023
The incorrect values will only live persist as long as the
cabi_realloccall is happening. Later calls to exports will run all the constructors again, and this time they'll get the correct values. Currently,wasi-libc's constructors always assume they're being called for the first time, so there's no boolean check to see if they've already run. That happens to be to our advantage in this case.alexcrichton commentedon Feb 28, 2023
That sounds reasonable to me as a stopgap, yeah. (albeit this continues to pile quite a few hacks in the adapter construction but our hand is sort of forced)
Longer-term I still feel like the picture is a bit unclear, however:
_startthis heavily depends on the signature of whatever the command entrypoint is. If it needs a post-return or areallocit seems like the same problem all over again because how, at the component model level, does initialization happn before the realloc/etc are called?_initit's not clear to me who or what calls this_initfunction. I'm presuming there's an idea for a hypothetical component model feature to do this, though, hence the "unclear to me" feeling.I know there's been a lot of historical discussion about command/reactor patterns in WASI but very little of that I feel has ended up getting surfaced into the component model itself. In that sense I find it difficult to think about commands/reactors since that's in theory a higher-level abstraction than the component model itself and the problem here in this issue is the component model's semantics. So "well it should be a reactor" or "let's fix commands" I find unclear since that's dealing with higher level semantics than the issue being run into here.
alexcrichton commentedon Mar 2, 2023
To link it @pchickey wrote up a great summary of how to tackle this at #99