Skip to content

Commit

Permalink
Only warn on stack pointers if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
john-sharratt committed May 26, 2024
1 parent 1480210 commit 30514ee
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
15 changes: 15 additions & 0 deletions lib/wasix/src/state/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ pub struct WasiInstanceHandles {
/// when a CTRL-C is pressed.
pub(crate) signal_set: bool,

/// Flag that indicates if the stack capture exports are being used by
/// this WASM process which means that it will be using asyncify
pub(crate) has_stack_checkpoint: bool,

/// asyncify_start_unwind(data : i32): call this to start unwinding the
/// stack from the current location. "data" must point to a data
/// structure as described above (with fields containing valid data).
Expand Down Expand Up @@ -143,6 +147,10 @@ pub struct WasiInstanceHandles {

impl WasiInstanceHandles {
pub fn new(memory: Memory, store: &impl AsStoreRef, instance: Instance) -> Self {
let has_stack_checkpoint = instance
.module()
.imports()
.any(|f| f.name() == "stack_checkpoint");
WasiInstanceHandles {
memory,
stack_pointer: instance
Expand Down Expand Up @@ -178,6 +186,7 @@ impl WasiInstanceHandles {
.exports
.get_typed_function(&store, "__wasm_signal")
.ok(),
has_stack_checkpoint,
signal_set: false,
asyncify_start_unwind: instance
.exports
Expand Down Expand Up @@ -439,6 +448,12 @@ impl WasiEnv {
self.thread.tid()
}

/// Returns true if this WASM process will need and try to use
/// asyncify while its running which normally means.
pub fn will_use_asyncify(&self) -> bool {
self.enable_deep_sleep || unsafe { self.inner().has_stack_checkpoint }
}

/// Re-initializes this environment so that it can be executed again
pub fn reinit(&mut self) -> Result<(), WasiStateCreationError> {
// If the cleanup logic is enabled then we need to rebuild the
Expand Down
15 changes: 10 additions & 5 deletions lib/wasix/src/state/func_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl WasiFunctionEnv {
)?;

let new_inner = WasiInstanceHandles::new(memory, store, instance);

let stack_pointer = new_inner.stack_pointer.clone();
let data_end = new_inner.data_end.clone();
let stack_low = new_inner.stack_low.clone();
Expand Down Expand Up @@ -199,15 +200,19 @@ impl WasiFunctionEnv {
// clang-16 and higher generate the `__stack_low` global, and it can be exported with
// `-Wl,--export=__stack_low`. clang-15 generates `__data_end`, which should be identical
// and can be exported if `__stack_low` is not available.
tracing::warn!("Missing both __stack_low and __data_end exports, unwinding may cause memory corruption");
if self.data(store).will_use_asyncify() {
tracing::warn!("Missing both __stack_low and __data_end exports, unwinding may cause memory corruption");
}
0
};

if stack_lower >= stack_base {
tracing::warn!(
"Detected lower end of stack to be above higher end, ignoring stack_lower; \
unwinding may cause memory corruption"
);
if self.data(store).will_use_asyncify() {
tracing::warn!(
"Detected lower end of stack to be above higher end, ignoring stack_lower; \
unwinding may cause memory corruption"
);
}
stack_lower = 0;
}

Expand Down

0 comments on commit 30514ee

Please sign in to comment.