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
Now that the component model supports multiple tasks and cooperative threads
running concurrently in the same instance, the old model of using a per-instance
flag to track whether a component may be entered no longer works. Instead of
keeping track of whether an instance has been entered at all, we must now keep
track of whether an instance has been entered for each in-progress task.
This commit removes `FLAG_MAY_ENTER` from `InstanceFlags`, relying instead on a
per-task call stack maintained at runtime. When the `component-model-async`
feature is enabled, we reuse the `GuestTask` stack for this purpose. When the
`component-model-async` feature is disabled, there's just a single stack used
for the entire store. Note that these stacks are only updated when crossing
component boundaries -- not at every internal call within an instance.
Each time we're about to enter a instance from either the host or the guest, we
check the call stack, and if the instance is already present, we trap.
Otherwise, we push a new element onto the stack and later pop it back off once
the callee returns a value.
In addition to trapping on recursive reentrance, we do a couple of additional
checks for host-to-guest calls, for which we previously relied on
`FLAG_MAY_ENTER`:
- If _any_ instance owned by the store has previously trapped, we disallow entering that or any other instance owned by the store.
- If a post-return call is needed for an instance, we disallow entering it until that call has been made.
Note that, for sync-to-sync, guest-to-guest calls, the above process entails
significantly more overhead than the prior code, which only involved checking
and setting a flag and did not require calling into the host at all. I intend
to follow this PR up with one or more optimizations to reduce that overhead.
See the discussion of `trap_if_on_the_stack` in
https://github.com/WebAssembly/component-model/blob/main/design/mvp/CanonicalABI.md
for examples of such optimizations.
In addition to the above refactoring, this commit does a couple of related
things:
- When a host function recursively calls back into guest code, we now chain the old call stack to the new one. This allows us to catch recursive reentrance which may span multiple top-level instances.
- Previously, we did not push a new task on the stack for sync-to-sync, guest-to-guest calls, which meant we missed catching some violations of the sync-task-must-not-block-before-returning rule. Now that we are pushing a new task in that case, we catch all such violations, which means some of the existing WAST tests needed updating.
Signed-off-by: Joel Dice <[email protected]>
0 commit comments