From dcded84feb79c38a69ff63c65e86862194323f51 Mon Sep 17 00:00:00 2001 From: Johnathan Sharratt Date: Sun, 26 May 2024 11:09:23 +1000 Subject: [PATCH] Only warn on stack pointers if needed --- lib/wasix/src/state/env.rs | 15 +++++++++++++++ lib/wasix/src/state/func_env.rs | 15 ++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/wasix/src/state/env.rs b/lib/wasix/src/state/env.rs index a626aa09236..e69df00ece6 100644 --- a/lib/wasix/src/state/env.rs +++ b/lib/wasix/src/state/env.rs @@ -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). @@ -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 @@ -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 @@ -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 diff --git a/lib/wasix/src/state/func_env.rs b/lib/wasix/src/state/func_env.rs index 0934c95ec41..29f1c6d5464 100644 --- a/lib/wasix/src/state/func_env.rs +++ b/lib/wasix/src/state/func_env.rs @@ -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(); @@ -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; }