Skip to main content

wasmer_vm/trap/
traphandlers.rs

1// This file contains code from external sources.
2// Attributions: https://github.com/wasmerio/wasmer/blob/main/docs/ATTRIBUTIONS.md
3
4#![allow(static_mut_refs)]
5
6//! WebAssembly trap handling, which is built on top of the lower-level
7//! signalhandling mechanisms.
8
9use super::trap::UnwindReason;
10use crate::Trap;
11#[cfg(all(unix, feature = "experimental-host-interrupt"))]
12use crate::interrupt_registry;
13use backtrace::Backtrace;
14use bytesize::ByteSize;
15use core::ptr::{read, read_unaligned};
16use corosensei::stack::{DefaultStack, Stack};
17use corosensei::trap::{CoroutineTrapHandler, TrapHandlerRegs};
18use corosensei::{CoroutineResult, ScopedCoroutine, Yielder};
19use scopeguard::defer;
20use std::any::Any;
21use std::cell::Cell;
22use std::error::Error;
23use std::io;
24use std::mem;
25#[cfg(unix)]
26use std::mem::MaybeUninit;
27use std::ptr::{self, NonNull};
28use std::sync::atomic::{AtomicPtr, AtomicUsize, Ordering, compiler_fence};
29use std::sync::{LazyLock, Once};
30use wasmer_types::TrapCode;
31
32/// Convenience extension for [`Stack`] that exposes the total mapped size.
33trait StackExt: Stack {
34    /// Returns the total size of the stack mapping (including guard page).
35    fn size(&self) -> usize {
36        self.base().get() - self.limit().get()
37    }
38}
39impl<T: Stack> StackExt for T {}
40
41/// Configuration for the runtime VM
42/// Currently only the stack size is configurable
43pub struct VMConfig {
44    /// Optional stack size (in byte) of the VM. Value lower than 8K will be rounded to 8K.
45    pub wasm_stack_size: Option<usize>,
46}
47
48// TrapInformation can be stored in the "Undefined Instruction" itself.
49// On x86_64, 0xC? select a "Register" for the Mod R/M part of "ud1" (so with no other bytes after)
50// On Arm64, the udf allows for a 16bits values, so we'll use the same 0xC? to store the trapinfo
51static MAGIC: u8 = 0xc0;
52
53static DEFAULT_STACK_SIZE: AtomicUsize = AtomicUsize::new(ByteSize::mib(1).as_u64() as usize);
54
55/// Maximum allowed default stack size (100 MiB) for the process-wide
56/// configuration set via `set_stack_size`.
57pub const MAX_STACK_SIZE: usize = ByteSize::mib(100).as_u64() as usize;
58
59// Current definition of `ucontext_t` in the `libc` crate is incorrect
60// on aarch64-apple-drawing so it's defined here with a more accurate definition.
61#[repr(C)]
62#[cfg(all(target_arch = "aarch64", target_os = "macos"))]
63#[allow(non_camel_case_types)]
64struct ucontext_t {
65    uc_onstack: libc::c_int,
66    uc_sigmask: libc::sigset_t,
67    uc_stack: libc::stack_t,
68    uc_link: *mut libc::ucontext_t,
69    uc_mcsize: usize,
70    uc_mcontext: libc::mcontext_t,
71}
72
73#[cfg(all(unix, not(all(target_arch = "aarch64", target_os = "macos"))))]
74use libc::ucontext_t;
75
76/// Sets the process-wide default stack size for new Wasmer coroutines.
77/// The value is clamped to [8 KiB, MAX_STACK_SIZE].
78pub fn set_stack_size(size: usize) {
79    DEFAULT_STACK_SIZE.store(
80        size.clamp(ByteSize::kib(8).as_u64() as usize, MAX_STACK_SIZE),
81        Ordering::Relaxed,
82    );
83}
84
85/// Returns the process-wide default stack size in bytes.
86pub fn get_stack_size() -> usize {
87    DEFAULT_STACK_SIZE.load(Ordering::Relaxed)
88}
89
90/// Pool of pre-allocated coroutine stacks to avoid repeated mmap syscalls.
91/// Acts as the cross-thread overflow store; per-thread reuse is served by
92/// `TLS_STACK` to keep the hot path atomic-free.
93static STACK_POOL: LazyLock<crossbeam_queue::SegQueue<DefaultStack>> =
94    LazyLock::new(crossbeam_queue::SegQueue::new);
95
96/// Per-thread cache holding a single ready-to-use coroutine stack. The hot
97/// path of `on_wasm_stack` pops from here without touching the global
98/// `STACK_POOL`'s atomics; only the first call on a thread or re-entrant
99/// nested calls fall back to the pool.
100///
101/// On thread exit the held stack (if any) is pushed to `STACK_POOL` so memory
102/// cycles correctly across thread lifetimes (no mmap leaks).
103struct StackCache(Cell<Option<DefaultStack>>);
104
105impl Drop for StackCache {
106    fn drop(&mut self) {
107        if let Some(stack) = self.0.take() {
108            STACK_POOL.push(stack);
109        }
110    }
111}
112
113thread_local! {
114    static TLS_STACK: StackCache = const { StackCache(Cell::new(None)) };
115}
116
117/// Acquire a coroutine stack large enough for `min_size`. Prefers the
118/// thread-local cache (no atomics), falls back to the global `STACK_POOL`,
119/// then allocates a fresh stack.
120fn acquire_stack(min_size: usize) -> DefaultStack {
121    // Fast path: thread-local cache. Steady-state per-thread reuse never
122    // touches the SegQueue.
123    if let Some(stack) = TLS_STACK.with(|cache| cache.0.take()) {
124        if stack.size() >= min_size {
125            return stack;
126        }
127        // Undersized — discard (mirrors the existing `STACK_POOL.pop().filter(...)`
128        // behavior of not holding undersized stacks in rotation).
129        drop(stack);
130    }
131    // Cross-thread overflow pool. Single pop, single filter — same semantics
132    // as the pre-TLS implementation.
133    STACK_POOL
134        .pop()
135        .filter(|s| s.size() >= min_size)
136        .unwrap_or_else(|| DefaultStack::new(min_size).unwrap())
137}
138
139/// Release a coroutine stack. Prefers the thread-local slot if empty (no
140/// atomics); otherwise pushes to the global `STACK_POOL` so the stack is
141/// still reusable by other threads.
142fn release_stack(stack: DefaultStack) {
143    let displaced = TLS_STACK.with(|cache| cache.0.replace(Some(stack)));
144    if let Some(displaced) = displaced {
145        STACK_POOL.push(displaced);
146    }
147}
148
149/// Drains the coroutine stack pool at the moment it runs.
150///
151/// This is intended to be called before retrying with a larger stack size so
152/// that the pool does not keep serving cached undersized stacks.
153///
154/// Note that `STACK_POOL` is a global, concurrently used queue and that each
155/// thread also keeps a private cached stack in TLS. Other threads may push
156/// stacks back into the pool (for example, when their Wasm execution
157/// finishes) while or after this function is running, and TLS-cached stacks
158/// on other threads are not touched. As a result, this function provides
159/// only a best-effort drain: there is no guarantee that no undersized stacks
160/// exist immediately after it returns unless the caller ensures, via external
161/// synchronization, that no other Wasm executions can return stacks to the
162/// pool while this function runs. The current thread's TLS-cached stack is
163/// drained as part of this call.
164pub fn drain_stack_pool() {
165    // Drain the calling thread's TLS slot first (best-effort across threads
166    // still applies — other threads' caches aren't touched).
167    if let Some(stack) = TLS_STACK.with(|cache| cache.0.take()) {
168        drop(stack);
169    }
170    while STACK_POOL.pop().is_some() {}
171}
172
173cfg_select! {
174    unix => {
175        /// Function which may handle custom signals while processing traps.
176        pub type TrapHandlerFn<'a> = dyn Fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) -> bool + Send + Sync + 'a;
177    }
178    target_os = "windows" => {
179        /// Function which may handle custom signals while processing traps.
180        pub type TrapHandlerFn<'a> = dyn Fn(*mut windows_sys::Win32::System::Diagnostics::Debug::EXCEPTION_POINTERS) -> bool + Send + Sync + 'a;
181    }
182}
183
184// Process an IllegalOpcode to see if it has a TrapCode payload
185unsafe fn process_illegal_op(addr: usize) -> Option<TrapCode> {
186    let mut val: Option<u8> = None;
187    unsafe {
188        if cfg!(target_arch = "x86_64") {
189            val = if read(addr as *mut u8) & 0xf0 == 0x40
190                && read((addr + 1) as *mut u8) == 0x0f
191                && read((addr + 2) as *mut u8) == 0xb9
192            {
193                Some(read((addr + 3) as *mut u8))
194            } else if read(addr as *mut u8) == 0x0f && read((addr + 1) as *mut u8) == 0xb9 {
195                Some(read((addr + 2) as *mut u8))
196            } else {
197                None
198            }
199        }
200        if cfg!(target_arch = "aarch64") {
201            val = if read_unaligned(addr as *mut u32) & 0xffff0000 == 0 {
202                Some(read(addr as *mut u8))
203            } else {
204                None
205            }
206        }
207        if cfg!(target_arch = "riscv64") {
208            let addr = addr as *mut u32;
209            // Check if 'unimp' instruction
210            val = if read(addr) == 0xc0001073 {
211                // Read from the instruction we emitted: 'addi a0, xzero, $payload'
212                // and take the encoded immediate value (upper 12-bits).
213                let prev_insn = read(addr.sub(1));
214                if (prev_insn & 0xffff) == 0x0513 {
215                    Some((prev_insn >> 20) as u8)
216                } else {
217                    None
218                }
219            } else {
220                None
221            };
222        }
223    }
224
225    // The direct encoding of a trap into the instruction is unused on RISC-V:
226    if cfg!(target_arch = "x86_64") || cfg!(target_arch = "aarch64") {
227        val = val.and_then(|val| {
228            if val & MAGIC == MAGIC {
229                Some(val & 0xf)
230            } else {
231                None
232            }
233        });
234    }
235
236    match val {
237        None => None,
238        Some(val) => match val {
239            0 => Some(TrapCode::StackOverflow),
240            1 => Some(TrapCode::HeapAccessOutOfBounds),
241            2 => Some(TrapCode::HeapMisaligned),
242            3 => Some(TrapCode::TableAccessOutOfBounds),
243            4 => Some(TrapCode::IndirectCallToNull),
244            5 => Some(TrapCode::BadSignature),
245            6 => Some(TrapCode::IntegerOverflow),
246            7 => Some(TrapCode::IntegerDivisionByZero),
247            8 => Some(TrapCode::BadConversionToInteger),
248            9 => Some(TrapCode::UnreachableCodeReached),
249            10 => Some(TrapCode::UnalignedAtomic),
250            _ => None,
251        },
252    }
253}
254
255cfg_select! {
256    unix => {
257        static mut PREV_SIGSEGV: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
258        static mut PREV_SIGBUS: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
259        static mut PREV_SIGILL: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
260        static mut PREV_SIGFPE: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
261
262        #[cfg(feature = "experimental-host-interrupt")]
263        static mut PREV_SIGUSR1: MaybeUninit<libc::sigaction> = MaybeUninit::uninit();
264
265        unsafe fn platform_init() { unsafe {
266            let register = |slot: &mut MaybeUninit<libc::sigaction>, signal: i32, nodefer: bool| {
267                let mut handler: libc::sigaction = mem::zeroed();
268                // The flags here are relatively careful, and they are...
269                //
270                // SA_SIGINFO gives us access to information like the program
271                // counter from where the fault happened.
272                //
273                // SA_ONSTACK allows us to handle signals on an alternate stack,
274                // so that the handler can run in response to running out of
275                // stack space on the main stack. Rust installs an alternate
276                // stack with sigaltstack, so we rely on that.
277                //
278                // SA_NODEFER allows us to reenter the signal handler if we
279                // crash while handling the signal, and fall through to the
280                // Breakpad handler by testing handlingSegFault.
281                handler.sa_flags = libc::SA_SIGINFO | libc::SA_ONSTACK;
282                if nodefer {
283                    handler.sa_flags |= libc::SA_NODEFER;
284                }
285                handler.sa_sigaction = trap_handler as *const () as usize;
286                libc::sigemptyset(&mut handler.sa_mask);
287                if libc::sigaction(signal, &handler, slot.as_mut_ptr()) != 0 {
288                    panic!(
289                        "unable to install signal handler: {}",
290                        io::Error::last_os_error(),
291                    );
292                }
293            };
294
295            // Allow handling OOB with signals on all architectures
296            register(&mut PREV_SIGSEGV, libc::SIGSEGV, true);
297
298            // Handle `unreachable` instructions which execute `ud2` right now
299            register(&mut PREV_SIGILL, libc::SIGILL, true);
300
301            // SIGUSR1 is used to interrupt long-running WASM code.
302            // It doesn't use NODEFER since, if a second interruption
303            // request comes in while one is already being processed,
304            // there's nothing meaningful we can do.
305            #[cfg(feature = "experimental-host-interrupt")]
306            register(&mut PREV_SIGUSR1, libc::SIGUSR1, false);
307
308            // x86 uses SIGFPE to report division by zero
309            if cfg!(target_arch = "x86") || cfg!(target_arch = "x86_64") {
310                register(&mut PREV_SIGFPE, libc::SIGFPE, true);
311            }
312
313            // On ARM, handle Unaligned Accesses.
314            // On Darwin, guard page accesses are raised as SIGBUS.
315            if cfg!(target_arch = "arm") || cfg!(target_vendor = "apple") {
316                register(&mut PREV_SIGBUS, libc::SIGBUS, true);
317            }
318
319            // This is necessary to support debugging under LLDB on Darwin.
320            // For more details see https://github.com/mono/mono/commit/8e75f5a28e6537e56ad70bf870b86e22539c2fb7
321            #[cfg(target_vendor = "apple")]
322            {
323                use mach2::exception_types::*;
324                use mach2::kern_return::*;
325                use mach2::port::*;
326                use mach2::thread_status::*;
327                use mach2::traps::*;
328                use mach2::mach_types::*;
329
330                unsafe extern "C" {
331                    fn task_set_exception_ports(
332                        task: task_t,
333                        exception_mask: exception_mask_t,
334                        new_port: mach_port_t,
335                        behavior: exception_behavior_t,
336                        new_flavor: thread_state_flavor_t,
337                    ) -> kern_return_t;
338                }
339
340                #[allow(non_snake_case)]
341                #[cfg(target_arch = "x86_64")]
342                let MACHINE_THREAD_STATE = x86_THREAD_STATE64;
343                #[allow(non_snake_case)]
344                #[cfg(target_arch = "aarch64")]
345                let MACHINE_THREAD_STATE = 6;
346
347                task_set_exception_ports(
348                    mach_task_self(),
349                    EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC | EXC_MASK_BAD_INSTRUCTION,
350                    MACH_PORT_NULL,
351                    EXCEPTION_STATE_IDENTITY as exception_behavior_t,
352                    MACHINE_THREAD_STATE,
353                );
354            }
355        }}
356
357        unsafe extern "C" fn trap_handler(
358            signum: libc::c_int,
359            siginfo: *mut libc::siginfo_t,
360            context: *mut libc::c_void,
361        ) { unsafe {
362            let previous = match signum {
363                libc::SIGSEGV => &PREV_SIGSEGV,
364                libc::SIGBUS => &PREV_SIGBUS,
365                libc::SIGFPE => &PREV_SIGFPE,
366                libc::SIGILL => &PREV_SIGILL,
367                #[cfg(feature = "experimental-host-interrupt")]
368                libc::SIGUSR1 => &PREV_SIGUSR1,
369                _ => panic!("unknown signal: {signum}"),
370            };
371            // We try to get the fault address associated to this signal
372            let maybe_fault_address = match signum {
373                libc::SIGSEGV | libc::SIGBUS => {
374                    Some((*siginfo).si_addr() as usize)
375                }
376                _ => None,
377            };
378            let trap_code = match signum {
379                // check if it was cased by a UD and if the Trap info is a payload to it
380                libc::SIGILL => {
381                    let addr = (*siginfo).si_addr() as usize;
382                    process_illegal_op(addr)
383                }
384                #[cfg(feature = "experimental-host-interrupt")]
385                libc::SIGUSR1 => {
386                    // If we're not running WASM code from the specific store for which
387                    // an interrupt was requested, there's nothing to do.
388                    if !interrupt_registry::on_interrupted() {
389                        return;
390                    }
391                    Some(TrapCode::HostInterrupt)
392                }
393                _ => None,
394            };
395            let ucontext = &mut *(context as *mut ucontext_t);
396            let (pc, sp) = get_pc_sp(ucontext);
397            let handled = TrapHandlerContext::handle_trap(
398                pc,
399                sp,
400                maybe_fault_address,
401                trap_code,
402                |regs| update_context(ucontext, regs),
403                |handler| handler(signum, siginfo, context),
404            );
405
406            if handled {
407                return;
408            }
409
410            // If we're not running WASM code at all, there's nothing to
411            // do for an interrupt.
412            #[cfg(feature = "experimental-host-interrupt")]
413            if signum == libc::SIGUSR1 {
414                return;
415            }
416
417            // This signal is not for any compiled wasm code we expect, so we
418            // need to forward the signal to the next handler. If there is no
419            // next handler (SIG_IGN or SIG_DFL), then it's time to crash. To do
420            // this, we set the signal back to its original disposition and
421            // return. This will cause the faulting op to be re-executed which
422            // will crash in the normal way. If there is a next handler, call
423            // it. It will either crash synchronously, fix up the instruction
424            // so that execution can continue and return, or trigger a crash by
425            // returning the signal to it's original disposition and returning.
426            let previous = &*previous.as_ptr();
427            if previous.sa_flags & libc::SA_SIGINFO != 0 {
428                mem::transmute::<
429                    usize,
430                    extern "C" fn(libc::c_int, *mut libc::siginfo_t, *mut libc::c_void),
431                >(previous.sa_sigaction)(signum, siginfo, context)
432            } else if previous.sa_sigaction == libc::SIG_DFL
433            {
434                libc::sigaction(signum, previous, ptr::null_mut());
435            } else if previous.sa_sigaction != libc::SIG_IGN {
436                mem::transmute::<usize, extern "C" fn(libc::c_int)>(
437                    previous.sa_sigaction
438                )(signum)
439            }
440        }}
441
442        unsafe fn get_pc_sp(context: &ucontext_t) -> (usize, usize) {
443            let (pc, sp);
444            cfg_select! {
445                all(
446                    any(target_os = "linux", target_os = "android"),
447                    target_arch = "x86_64",
448                ) => {
449                    pc = context.uc_mcontext.gregs[libc::REG_RIP as usize] as usize;
450                    sp = context.uc_mcontext.gregs[libc::REG_RSP as usize] as usize;
451                }
452                all(
453                    any(target_os = "linux", target_os = "android"),
454                    target_arch = "x86",
455                ) => {
456                    pc = context.uc_mcontext.gregs[libc::REG_EIP as usize] as usize;
457                    sp = context.uc_mcontext.gregs[libc::REG_ESP as usize] as usize;
458                }
459                all(target_os = "freebsd", target_arch = "x86") => {
460                    pc = context.uc_mcontext.mc_eip as usize;
461                    sp = context.uc_mcontext.mc_esp as usize;
462                }
463                all(target_os = "freebsd", target_arch = "x86_64") => {
464                    pc = context.uc_mcontext.mc_rip as usize;
465                    sp = context.uc_mcontext.mc_rsp as usize;
466                }
467                all(target_vendor = "apple", target_arch = "x86_64") => {
468                    let mcontext = unsafe { &*context.uc_mcontext };
469                    pc = mcontext.__ss.__rip as usize;
470                    sp = mcontext.__ss.__rsp as usize;
471                }
472                all(
473                        any(target_os = "linux", target_os = "android"),
474                        target_arch = "aarch64",
475                    ) => {
476                    pc = context.uc_mcontext.pc as usize;
477                    sp = context.uc_mcontext.sp as usize;
478                }
479                all(
480                    any(target_os = "linux", target_os = "android"),
481                    target_arch = "arm",
482                ) => {
483                    pc = context.uc_mcontext.arm_pc as usize;
484                    sp = context.uc_mcontext.arm_sp as usize;
485                }
486                all(
487                    any(target_os = "linux", target_os = "android"),
488                    any(target_arch = "riscv64", target_arch = "riscv32"),
489                ) => {
490                    pc = context.uc_mcontext.__gregs[libc::REG_PC] as usize;
491                    sp = context.uc_mcontext.__gregs[libc::REG_SP] as usize;
492                }
493                all(target_vendor = "apple", target_arch = "aarch64") => {
494                    let mcontext = unsafe { &*context.uc_mcontext };
495                    pc = mcontext.__ss.__pc as usize;
496                    sp = mcontext.__ss.__sp as usize;
497                }
498                all(target_os = "freebsd", target_arch = "aarch64") => {
499                    pc = context.uc_mcontext.mc_gpregs.gp_elr as usize;
500                    sp = context.uc_mcontext.mc_gpregs.gp_sp as usize;
501                }
502                all(target_os = "linux", target_arch = "loongarch64") => {
503                    pc = context.uc_mcontext.__gregs[1] as usize;
504                    sp = context.uc_mcontext.__gregs[3] as usize;
505                }
506                all(target_os = "linux", target_arch = "powerpc64") => {
507                    pc = (*context.uc_mcontext.regs).nip as usize;
508                    sp = (*context.uc_mcontext.regs).gpr[1] as usize;
509                }
510                _ => {
511                    compile_error!("Unsupported platform");
512                }
513            };
514            (pc, sp)
515        }
516
517        unsafe fn update_context(context: &mut ucontext_t, regs: TrapHandlerRegs) {
518            cfg_select! {
519                all(
520                        any(target_os = "linux", target_os = "android"),
521                        target_arch = "x86_64",
522                    ) => {
523                    let TrapHandlerRegs { rip, rsp, rbp, rdi, rsi } = regs;
524                    context.uc_mcontext.gregs[libc::REG_RIP as usize] = rip as i64;
525                    context.uc_mcontext.gregs[libc::REG_RSP as usize] = rsp as i64;
526                    context.uc_mcontext.gregs[libc::REG_RBP as usize] = rbp as i64;
527                    context.uc_mcontext.gregs[libc::REG_RDI as usize] = rdi as i64;
528                    context.uc_mcontext.gregs[libc::REG_RSI as usize] = rsi as i64;
529                }
530                all(
531                    any(target_os = "linux", target_os = "android"),
532                    target_arch = "x86",
533                ) => {
534                    let TrapHandlerRegs { eip, esp, ebp, ecx, edx } = regs;
535                    context.uc_mcontext.gregs[libc::REG_EIP as usize] = eip as i32;
536                    context.uc_mcontext.gregs[libc::REG_ESP as usize] = esp as i32;
537                    context.uc_mcontext.gregs[libc::REG_EBP as usize] = ebp as i32;
538                    context.uc_mcontext.gregs[libc::REG_ECX as usize] = ecx as i32;
539                    context.uc_mcontext.gregs[libc::REG_EDX as usize] = edx as i32;
540                }
541                all(target_vendor = "apple", target_arch = "x86_64") => {
542                    let TrapHandlerRegs { rip, rsp, rbp, rdi, rsi } = regs;
543                    let mcontext = unsafe { &mut *context.uc_mcontext };
544                    mcontext.__ss.__rip = rip;
545                    mcontext.__ss.__rsp = rsp;
546                    mcontext.__ss.__rbp = rbp;
547                    mcontext.__ss.__rdi = rdi;
548                    mcontext.__ss.__rsi = rsi;
549                }
550                all(target_os = "freebsd", target_arch = "x86") => {
551                    let TrapHandlerRegs { eip, esp, ebp, ecx, edx } = regs;
552                    context.uc_mcontext.mc_eip = eip as libc::register_t;
553                    context.uc_mcontext.mc_esp = esp as libc::register_t;
554                    context.uc_mcontext.mc_ebp = ebp as libc::register_t;
555                    context.uc_mcontext.mc_ecx = ecx as libc::register_t;
556                    context.uc_mcontext.mc_edx = edx as libc::register_t;
557                }
558                all(target_os = "freebsd", target_arch = "x86_64") => {
559                    let TrapHandlerRegs { rip, rsp, rbp, rdi, rsi } = regs;
560                    context.uc_mcontext.mc_rip = rip as libc::register_t;
561                    context.uc_mcontext.mc_rsp = rsp as libc::register_t;
562                    context.uc_mcontext.mc_rbp = rbp as libc::register_t;
563                    context.uc_mcontext.mc_rdi = rdi as libc::register_t;
564                    context.uc_mcontext.mc_rsi = rsi as libc::register_t;
565                }
566                all(
567                        any(target_os = "linux", target_os = "android"),
568                        target_arch = "aarch64",
569                    ) => {
570                    let TrapHandlerRegs { pc, sp, x0, x1, x29, lr } = regs;
571                    context.uc_mcontext.pc = pc;
572                    context.uc_mcontext.sp = sp;
573                    context.uc_mcontext.regs[0] = x0;
574                    context.uc_mcontext.regs[1] = x1;
575                    context.uc_mcontext.regs[29] = x29;
576                    context.uc_mcontext.regs[30] = lr;
577                }
578                all(
579                        any(target_os = "linux", target_os = "android"),
580                        target_arch = "arm",
581                    ) => {
582                    let TrapHandlerRegs {
583                        pc,
584                        r0,
585                        r1,
586                        r7,
587                        r11,
588                        r13,
589                        r14,
590                        cpsr_thumb,
591                        cpsr_endian,
592                    } = regs;
593                    context.uc_mcontext.arm_pc = pc;
594                    context.uc_mcontext.arm_r0 = r0;
595                    context.uc_mcontext.arm_r1 = r1;
596                    context.uc_mcontext.arm_r7 = r7;
597                    context.uc_mcontext.arm_fp = r11;
598                    context.uc_mcontext.arm_sp = r13;
599                    context.uc_mcontext.arm_lr = r14;
600                    if cpsr_thumb {
601                        context.uc_mcontext.arm_cpsr |= 0x20;
602                    } else {
603                        context.uc_mcontext.arm_cpsr &= !0x20;
604                    }
605                    if cpsr_endian {
606                        context.uc_mcontext.arm_cpsr |= 0x200;
607                    } else {
608                        context.uc_mcontext.arm_cpsr &= !0x200;
609                    }
610                }
611                all(
612                    any(target_os = "linux", target_os = "android"),
613                    any(target_arch = "riscv64", target_arch = "riscv32"),
614                ) => {
615                    let TrapHandlerRegs { pc, ra, sp, a0, a1, s0 } = regs;
616                    context.uc_mcontext.__gregs[libc::REG_PC] = pc as libc::c_ulong;
617                    context.uc_mcontext.__gregs[libc::REG_RA] = ra as libc::c_ulong;
618                    context.uc_mcontext.__gregs[libc::REG_SP] = sp as libc::c_ulong;
619                    context.uc_mcontext.__gregs[libc::REG_A0] = a0 as libc::c_ulong;
620                    context.uc_mcontext.__gregs[libc::REG_A0 + 1] = a1 as libc::c_ulong;
621                    context.uc_mcontext.__gregs[libc::REG_S0] = s0 as libc::c_ulong;
622                }
623                all(target_vendor = "apple", target_arch = "aarch64") => {
624                    let TrapHandlerRegs { pc, sp, x0, x1, x29, lr } = regs;
625                    let mcontext = unsafe { &mut *context.uc_mcontext };
626                    mcontext.__ss.__pc = pc;
627                    mcontext.__ss.__sp = sp;
628                    mcontext.__ss.__x[0] = x0;
629                    mcontext.__ss.__x[1] = x1;
630                    mcontext.__ss.__fp = x29;
631                    mcontext.__ss.__lr = lr;
632                }
633                all(target_os = "freebsd", target_arch = "aarch64") => {
634                    let TrapHandlerRegs { pc, sp, x0, x1, x29, lr } = regs;
635                    context.uc_mcontext.mc_gpregs.gp_elr = pc as libc::register_t;
636                    context.uc_mcontext.mc_gpregs.gp_sp = sp as libc::register_t;
637                    context.uc_mcontext.mc_gpregs.gp_x[0] = x0 as libc::register_t;
638                    context.uc_mcontext.mc_gpregs.gp_x[1] = x1 as libc::register_t;
639                    context.uc_mcontext.mc_gpregs.gp_x[29] = x29 as libc::register_t;
640                    context.uc_mcontext.mc_gpregs.gp_lr = lr as libc::register_t;
641                }
642                all(target_os = "linux", target_arch = "loongarch64") => {
643                    let TrapHandlerRegs { pc, sp, a0, a1, fp, ra } = regs;
644                    context.uc_mcontext.__pc = pc;
645                    context.uc_mcontext.__gregs[1] = ra;
646                    context.uc_mcontext.__gregs[3] = sp;
647                    context.uc_mcontext.__gregs[4] = a0;
648                    context.uc_mcontext.__gregs[5] = a1;
649                    context.uc_mcontext.__gregs[22] = fp;
650                }
651                all(target_os = "linux", target_arch = "powerpc64") => {
652                    let TrapHandlerRegs { pc, sp, r3, r4, r31, lr } = regs;
653                    (*context.uc_mcontext.regs).nip = pc;
654                    (*context.uc_mcontext.regs).gpr[1] = sp;
655                    (*context.uc_mcontext.regs).gpr[3] = r3;
656                    (*context.uc_mcontext.regs).gpr[4] = r4;
657                    (*context.uc_mcontext.regs).gpr[31] = r31;
658                    (*context.uc_mcontext.regs).link = lr;
659                }
660                _ => {
661                    compile_error!("Unsupported platform");
662                }
663            };
664        }
665    }
666    target_os = "windows" => {
667        use windows_sys::Win32::System::Diagnostics::Debug::{
668            AddVectoredExceptionHandler,
669            CONTEXT,
670            EXCEPTION_CONTINUE_EXECUTION,
671            EXCEPTION_CONTINUE_SEARCH,
672            EXCEPTION_POINTERS,
673        };
674        use windows_sys::Win32::Foundation::{
675            EXCEPTION_ACCESS_VIOLATION,
676            EXCEPTION_ILLEGAL_INSTRUCTION,
677            EXCEPTION_INT_DIVIDE_BY_ZERO,
678            EXCEPTION_INT_OVERFLOW,
679            EXCEPTION_STACK_OVERFLOW,
680        };
681
682        unsafe fn platform_init() {
683            unsafe {
684                // our trap handler needs to go first, so that we can recover from
685                // wasm faults and continue execution, so pass `1` as a true value
686                // here.
687                let handler = AddVectoredExceptionHandler(1, Some(exception_handler));
688                if handler.is_null() {
689                    panic!("failed to add exception handler: {}", io::Error::last_os_error());
690                }
691            }
692        }
693
694        unsafe extern "system" fn exception_handler(
695            exception_info: *mut EXCEPTION_POINTERS
696        ) -> i32 {
697            unsafe {
698                // Check the kind of exception, since we only handle a subset within
699                // wasm code. If anything else happens we want to defer to whatever
700                // the rest of the system wants to do for this exception.
701                let record = &*(*exception_info).ExceptionRecord;
702                if record.ExceptionCode != EXCEPTION_ACCESS_VIOLATION &&
703                    record.ExceptionCode != EXCEPTION_ILLEGAL_INSTRUCTION &&
704                    record.ExceptionCode != EXCEPTION_STACK_OVERFLOW &&
705                    record.ExceptionCode != EXCEPTION_INT_DIVIDE_BY_ZERO &&
706                    record.ExceptionCode != EXCEPTION_INT_OVERFLOW
707                {
708                    return EXCEPTION_CONTINUE_SEARCH;
709                }
710
711                // FIXME: this is what the previous C++ did to make sure that TLS
712                // works by the time we execute this trap handling code. This isn't
713                // exactly super easy to call from Rust though and it's not clear we
714                // necessarily need to do so. Leaving this here in case we need this
715                // in the future, but for now we can probably wait until we see a
716                // strange fault before figuring out how to reimplement this in
717                // Rust.
718                //
719                // if (!NtCurrentTeb()->Reserved1[sThreadLocalArrayPointerIndex]) {
720                //     return EXCEPTION_CONTINUE_SEARCH;
721                // }
722
723                let context = &mut *(*exception_info).ContextRecord;
724                let (pc, sp) = get_pc_sp(context);
725
726                // We try to get the fault address associated to this exception.
727                let maybe_fault_address = match record.ExceptionCode {
728                    EXCEPTION_ACCESS_VIOLATION => Some(record.ExceptionInformation[1]),
729                    EXCEPTION_STACK_OVERFLOW => Some(sp),
730                    _ => None,
731                };
732                let trap_code = match record.ExceptionCode {
733                    // check if it was cased by a UD and if the Trap info is a payload to it
734                    EXCEPTION_ILLEGAL_INSTRUCTION => {
735                        process_illegal_op(pc)
736                    }
737                    _ => None,
738                };
739                // This is basically the same as the unix version above, only with a
740                // few parameters tweaked here and there.
741                let handled = TrapHandlerContext::handle_trap(
742                    pc,
743                    sp,
744                    maybe_fault_address,
745                    trap_code,
746                    |regs| update_context(context, regs),
747                    |handler| handler(exception_info),
748                );
749
750                if handled {
751                    EXCEPTION_CONTINUE_EXECUTION
752                } else {
753                    EXCEPTION_CONTINUE_SEARCH
754                }
755            }
756        }
757
758        unsafe fn get_pc_sp(context: &CONTEXT) -> (usize, usize) {
759            let (pc, sp);
760            cfg_select! {
761                target_arch = "x86_64" => {
762                    pc = context.Rip as usize;
763                    sp = context.Rsp as usize;
764                }
765                target_arch = "x86" => {
766                    pc = context.Eip as usize;
767                    sp = context.Esp as usize;
768                }
769                _ => {
770                    compile_error!("Unsupported platform");
771                }
772            };
773            (pc, sp)
774        }
775
776        unsafe fn update_context(context: &mut CONTEXT, regs: TrapHandlerRegs) {
777            cfg_select! {
778                target_arch = "x86_64" => {
779                    let TrapHandlerRegs { rip, rsp, rbp, rdi, rsi } = regs;
780                    context.Rip = rip;
781                    context.Rsp = rsp;
782                    context.Rbp = rbp;
783                    context.Rdi = rdi;
784                    context.Rsi = rsi;
785                }
786                target_arch = "x86" => {
787                    let TrapHandlerRegs { eip, esp, ebp, ecx, edx } = regs;
788                    context.Eip = eip;
789                    context.Esp = esp;
790                    context.Ebp = ebp;
791                    context.Ecx = ecx;
792                    context.Edx = edx;
793                }
794                _ => {
795                    compile_error!("Unsupported platform");
796                }
797            };
798        }
799    }
800}
801
802/// This function is required to be called before any WebAssembly is entered.
803/// This will configure global state such as signal handlers to prepare the
804/// process to receive wasm traps.
805///
806/// This function must not only be called globally once before entering
807/// WebAssembly but it must also be called once-per-thread that enters
808/// WebAssembly. Currently in wasmer's integration this function is called on
809/// creation of a `Store`.
810pub fn init_traps() {
811    static INIT: Once = Once::new();
812    INIT.call_once(|| unsafe {
813        platform_init();
814    });
815}
816
817/// Raises a user-defined trap immediately.
818///
819/// This function performs as-if a wasm trap was just executed, only the trap
820/// has a dynamic payload associated with it which is user-provided. This trap
821/// payload is then returned from `catch_traps` below.
822///
823/// # Safety
824///
825/// Only safe to call when wasm code is on the stack, aka `catch_traps` must
826/// have been previous called and not yet returned.
827/// Additionally no Rust destructors may be on the stack.
828/// They will be skipped and not executed.
829pub unsafe fn raise_user_trap(data: Box<dyn Error + Send + Sync>) -> ! {
830    unsafe { unwind_with(UnwindReason::UserTrap(data)) }
831}
832
833/// Raises a trap from inside library code immediately.
834///
835/// This function performs as-if a wasm trap was just executed. This trap
836/// payload is then returned from `catch_traps` below.
837///
838/// # Safety
839///
840/// Only safe to call when wasm code is on the stack, aka `catch_traps` must
841/// have been previous called and not yet returned.
842/// Additionally no Rust destructors may be on the stack.
843/// They will be skipped and not executed.
844pub unsafe fn raise_lib_trap(trap: Trap) -> ! {
845    unsafe { unwind_with(UnwindReason::LibTrap(trap)) }
846}
847
848/// Carries a Rust panic across wasm code and resumes the panic on the other
849/// side.
850///
851/// # Safety
852///
853/// Only safe to call when wasm code is on the stack, aka `catch_traps` must
854/// have been previously called and not returned. Additionally no Rust destructors may be on the
855/// stack. They will be skipped and not executed.
856pub unsafe fn resume_panic(payload: Box<dyn Any + Send>) -> ! {
857    unsafe { unwind_with(UnwindReason::Panic(payload)) }
858}
859
860/// Catches any wasm traps that happen within the execution of `closure`,
861/// returning them as a `Result`.
862///
863/// # Safety
864///
865/// Highly unsafe since `closure` won't have any dtors run.
866pub unsafe fn catch_traps<F, R: 'static>(
867    trap_handler: Option<*const TrapHandlerFn<'static>>,
868    config: &VMConfig,
869    closure: F,
870) -> Result<R, Trap>
871where
872    F: FnOnce() -> R + 'static,
873{
874    // Ensure that per-thread initialization is done.
875    lazy_per_thread_init()?;
876    let stack_size = config
877        .wasm_stack_size
878        .unwrap_or_else(|| DEFAULT_STACK_SIZE.load(Ordering::Relaxed));
879    on_wasm_stack(stack_size, trap_handler, closure).map_err(UnwindReason::into_trap)
880}
881
882// We need two separate thread-local variables here:
883// - YIELDER is set within the new stack and is used to unwind back to the root
884//   of the stack from inside it.
885// - TRAP_HANDLER is set from outside the new stack and is solely used from
886//   signal handlers. It must be atomic since it is used by signal handlers.
887//
888// We also do per-thread signal stack initialization on the first time
889// TRAP_HANDLER is accessed.
890thread_local! {
891    static YIELDER: Cell<Option<NonNull<Yielder<(), UnwindReason>>>> = const { Cell::new(None) };
892    static TRAP_HANDLER: AtomicPtr<TrapHandlerContext> = const { AtomicPtr::new(ptr::null_mut()) };
893}
894
895/// Read-only information that is used by signal handlers to handle and recover
896/// from traps.
897#[allow(clippy::type_complexity)]
898struct TrapHandlerContext {
899    inner: *const u8,
900    handle_trap: fn(
901        *const u8,
902        usize,
903        usize,
904        Option<usize>,
905        Option<TrapCode>,
906        &mut dyn FnMut(TrapHandlerRegs),
907    ) -> bool,
908    custom_trap: Option<*const TrapHandlerFn<'static>>,
909}
910struct TrapHandlerContextInner<T> {
911    /// Information about the currently running coroutine. This is used to
912    /// reset execution to the root of the coroutine when a trap is handled.
913    coro_trap_handler: CoroutineTrapHandler<Result<T, UnwindReason>>,
914}
915
916impl TrapHandlerContext {
917    /// Runs the given function with a trap handler context. The previous
918    /// trap handler context is preserved and restored afterwards.
919    fn install<T, R>(
920        custom_trap: Option<*const TrapHandlerFn<'static>>,
921        coro_trap_handler: CoroutineTrapHandler<Result<T, UnwindReason>>,
922        f: impl FnOnce() -> R,
923    ) -> R {
924        // Type-erase the trap handler function so that it can be placed in TLS.
925        fn func<T>(
926            ptr: *const u8,
927            pc: usize,
928            sp: usize,
929            maybe_fault_address: Option<usize>,
930            trap_code: Option<TrapCode>,
931            update_regs: &mut dyn FnMut(TrapHandlerRegs),
932        ) -> bool {
933            unsafe {
934                (*(ptr as *const TrapHandlerContextInner<T>)).handle_trap(
935                    pc,
936                    sp,
937                    maybe_fault_address,
938                    trap_code,
939                    update_regs,
940                )
941            }
942        }
943        let inner = TrapHandlerContextInner { coro_trap_handler };
944        let ctx = Self {
945            inner: &inner as *const _ as *const u8,
946            handle_trap: func::<T>,
947            custom_trap,
948        };
949
950        compiler_fence(Ordering::Release);
951        let prev = TRAP_HANDLER.with(|ptr| {
952            let prev = ptr.load(Ordering::Relaxed);
953            ptr.store(&ctx as *const Self as *mut Self, Ordering::Relaxed);
954            prev
955        });
956
957        defer! {
958            TRAP_HANDLER.with(|ptr| ptr.store(prev, Ordering::Relaxed));
959            compiler_fence(Ordering::Acquire);
960        }
961
962        f()
963    }
964
965    /// Attempts to handle the trap if it's a wasm trap.
966    unsafe fn handle_trap(
967        pc: usize,
968        sp: usize,
969        maybe_fault_address: Option<usize>,
970        trap_code: Option<TrapCode>,
971        mut update_regs: impl FnMut(TrapHandlerRegs),
972        call_handler: impl Fn(&TrapHandlerFn<'static>) -> bool,
973    ) -> bool {
974        unsafe {
975            let ptr = TRAP_HANDLER.with(|ptr| ptr.load(Ordering::Relaxed));
976            if ptr.is_null() {
977                return false;
978            }
979
980            let ctx = &*ptr;
981
982            // Check if this trap is handled by a custom trap handler.
983            if let Some(trap_handler) = ctx.custom_trap
984                && call_handler(&*trap_handler)
985            {
986                return true;
987            }
988
989            (ctx.handle_trap)(
990                ctx.inner,
991                pc,
992                sp,
993                maybe_fault_address,
994                trap_code,
995                &mut update_regs,
996            )
997        }
998    }
999}
1000
1001impl<T> TrapHandlerContextInner<T> {
1002    unsafe fn handle_trap(
1003        &self,
1004        pc: usize,
1005        sp: usize,
1006        maybe_fault_address: Option<usize>,
1007        trap_code: Option<TrapCode>,
1008        update_regs: &mut dyn FnMut(TrapHandlerRegs),
1009    ) -> bool {
1010        unsafe {
1011            // Check if this trap occurred while executing on the Wasm stack. We can
1012            // only recover from traps if that is the case.
1013            if !self.coro_trap_handler.stack_ptr_in_bounds(sp) {
1014                return false;
1015            }
1016
1017            let signal_trap = trap_code.or_else(|| {
1018                maybe_fault_address.map(|addr| {
1019                    if self.coro_trap_handler.stack_ptr_in_bounds(addr) {
1020                        TrapCode::StackOverflow
1021                    } else {
1022                        TrapCode::HeapAccessOutOfBounds
1023                    }
1024                })
1025            });
1026
1027            // Don't try to generate a backtrace for stack overflows: unwinding
1028            // information is often not precise enough to properly describe what is
1029            // happening during a function prologue, which can lead the unwinder to
1030            // read invalid memory addresses.
1031            //
1032            // See: https://github.com/rust-lang/backtrace-rs/pull/357
1033            let backtrace = if signal_trap == Some(TrapCode::StackOverflow) {
1034                Backtrace::from(vec![])
1035            } else {
1036                Backtrace::new_unresolved()
1037            };
1038
1039            // Set up the register state for exception return to force the
1040            // coroutine to return to its caller with UnwindReason::WasmTrap.
1041            let unwind = UnwindReason::WasmTrap {
1042                backtrace,
1043                signal_trap,
1044                pc,
1045            };
1046            let regs = self
1047                .coro_trap_handler
1048                .setup_trap_handler(move || Err(unwind));
1049            update_regs(regs);
1050            true
1051        }
1052    }
1053}
1054
1055unsafe fn unwind_with(reason: UnwindReason) -> ! {
1056    unsafe {
1057        let yielder = YIELDER
1058            .with(|cell| cell.replace(None))
1059            .expect("not running on Wasm stack");
1060
1061        yielder.as_ref().suspend(reason);
1062
1063        // on_wasm_stack will forcibly reset the coroutine stack after yielding.
1064        unreachable!();
1065    }
1066}
1067
1068/// Runs the given function on a separate stack so that its stack usage can be
1069/// bounded. Stack overflows and other traps can be caught and execution
1070/// returned to the root of the stack.
1071fn on_wasm_stack<F: FnOnce() -> T + 'static, T: 'static>(
1072    stack_size: usize,
1073    trap_handler: Option<*const TrapHandlerFn<'static>>,
1074    f: F,
1075) -> Result<T, UnwindReason> {
1076    // Reuse a cached stack — TLS first (atomic-free hot path), then the
1077    // cross-thread overflow pool, then allocate fresh. Size mismatches
1078    // (e.g. after `drain_stack_pool()` + a stack-size change) are filtered
1079    // inside `acquire_stack`. `base() - limit()` is the full mmap region
1080    // (including guard page), which is always >= the requested size for
1081    // stacks allocated with that size.
1082    let stack = acquire_stack(stack_size);
1083    let mut stack = scopeguard::guard(stack, release_stack);
1084
1085    // Create a coroutine with a new stack to run the function on.
1086    let coro = ScopedCoroutine::with_stack(&mut *stack, move |yielder, ()| {
1087        // Save the yielder to TLS so that it can be used later.
1088        YIELDER.with(|cell| cell.set(Some(yielder.into())));
1089
1090        Ok(f())
1091    });
1092
1093    // Ensure that YIELDER is reset on exit even if the coroutine panics,
1094    defer! {
1095        YIELDER.with(|cell| cell.set(None));
1096    }
1097
1098    coro.scope(|mut coro_ref| {
1099        // Set up metadata for the trap handler for the duration of the coroutine
1100        // execution. This is restored to its previous value afterwards.
1101        TrapHandlerContext::install(trap_handler, coro_ref.trap_handler(), || {
1102            match coro_ref.resume(()) {
1103                CoroutineResult::Yield(trap) => {
1104                    // This came from unwind_with which requires that there be only
1105                    // Wasm code on the stack.
1106                    unsafe {
1107                        coro_ref.force_reset();
1108                    }
1109                    Err(trap)
1110                }
1111                CoroutineResult::Return(result) => result,
1112            }
1113        })
1114    })
1115}
1116
1117/// When executing on the Wasm stack, temporarily switch back to the host stack
1118/// to perform an operation that should not be constrained by the Wasm stack
1119/// limits.
1120///
1121/// This is particularly important since the usage of the Wasm stack is under
1122/// the control of untrusted code. Malicious code could artificially induce a
1123/// stack overflow in the middle of a sensitive host operations (e.g. growing
1124/// a memory) which would be hard to recover from.
1125pub fn on_host_stack<F: FnOnce() -> T, T>(f: F) -> T {
1126    // Reset YIEDER to None for the duration of this call to indicate that we
1127    // are no longer on the Wasm stack.
1128    let yielder_ptr = YIELDER.with(|cell| cell.replace(None));
1129
1130    // If we are already on the host stack, execute the function directly. This
1131    // happens if a host function is called directly from the API.
1132    let yielder = match yielder_ptr {
1133        Some(ptr) => unsafe { ptr.as_ref() },
1134        None => return f(),
1135    };
1136
1137    // Restore YIELDER upon exiting normally or unwinding.
1138    defer! {
1139        YIELDER.with(|cell| cell.set(yielder_ptr));
1140    }
1141
1142    // on_parent_stack requires the closure to be Send so that the Yielder
1143    // cannot be called from the parent stack. This is not a problem for us
1144    // since we don't expose the Yielder.
1145    struct SendWrapper<T>(T);
1146    unsafe impl<T> Send for SendWrapper<T> {}
1147    let wrapped = SendWrapper(f);
1148    yielder.on_parent_stack(move || {
1149        let wrapped = wrapped;
1150        (wrapped.0)()
1151    })
1152}
1153
1154#[cfg(windows)]
1155pub fn lazy_per_thread_init() -> Result<(), Trap> {
1156    // We need additional space on the stack to handle stack overflow
1157    // exceptions. Rust's initialization code sets this to 0x5000 but this
1158    // seems to be insufficient in practice.
1159    use windows_sys::Win32::System::Threading::SetThreadStackGuarantee;
1160    if unsafe { SetThreadStackGuarantee(&mut 0x10000) } == 0 {
1161        panic!("failed to set thread stack guarantee");
1162    }
1163
1164    Ok(())
1165}
1166
1167/// A module for registering a custom alternate signal stack (sigaltstack).
1168///
1169/// Rust's libstd installs an alternate stack with size `SIGSTKSZ`, which is not
1170/// always large enough for our signal handling code. Override it by creating
1171/// and registering our own alternate stack that is large enough and has a guard
1172/// page.
1173#[cfg(unix)]
1174pub fn lazy_per_thread_init() -> Result<(), Trap> {
1175    use std::ptr::null_mut;
1176
1177    thread_local! {
1178        /// Thread-local state is lazy-initialized on the first time it's used,
1179        /// and dropped when the thread exits.
1180        static TLS: Tls = unsafe { init_sigstack() };
1181    }
1182
1183    /// The size of the sigaltstack (not including the guard, which will be
1184    /// added). Make this large enough to run our signal handlers.
1185    const MIN_STACK_SIZE: usize = ByteSize::kib(64).as_u64() as usize;
1186
1187    enum Tls {
1188        OutOfMemory,
1189        Allocated {
1190            mmap_ptr: *mut libc::c_void,
1191            mmap_size: usize,
1192        },
1193        BigEnough,
1194    }
1195
1196    unsafe fn init_sigstack() -> Tls {
1197        unsafe {
1198            // Check to see if the existing sigaltstack, if it exists, is big
1199            // enough. If so we don't need to allocate our own.
1200            let mut old_stack = mem::zeroed();
1201            let r = libc::sigaltstack(ptr::null(), &mut old_stack);
1202            assert_eq!(r, 0, "learning about sigaltstack failed");
1203            if old_stack.ss_flags & libc::SS_DISABLE == 0 && old_stack.ss_size >= MIN_STACK_SIZE {
1204                return Tls::BigEnough;
1205            }
1206
1207            // ... but failing that we need to allocate our own, so do all that
1208            // here.
1209            let page_size: usize = region::page::size();
1210            let guard_size = page_size;
1211            let alloc_size = guard_size + MIN_STACK_SIZE;
1212
1213            let ptr = libc::mmap(
1214                null_mut(),
1215                alloc_size,
1216                libc::PROT_NONE,
1217                libc::MAP_PRIVATE | libc::MAP_ANON,
1218                -1,
1219                0,
1220            );
1221            if ptr == libc::MAP_FAILED {
1222                return Tls::OutOfMemory;
1223            }
1224
1225            // Prepare the stack with readable/writable memory and then register it
1226            // with `sigaltstack`.
1227            let stack_ptr = (ptr as usize + guard_size) as *mut libc::c_void;
1228            let r = libc::mprotect(
1229                stack_ptr,
1230                MIN_STACK_SIZE,
1231                libc::PROT_READ | libc::PROT_WRITE,
1232            );
1233            assert_eq!(r, 0, "mprotect to configure memory for sigaltstack failed");
1234            let new_stack = libc::stack_t {
1235                ss_sp: stack_ptr,
1236                ss_flags: 0,
1237                ss_size: MIN_STACK_SIZE,
1238            };
1239            let r = libc::sigaltstack(&new_stack, ptr::null_mut());
1240            assert_eq!(r, 0, "registering new sigaltstack failed");
1241
1242            Tls::Allocated {
1243                mmap_ptr: ptr,
1244                mmap_size: alloc_size,
1245            }
1246        }
1247    }
1248
1249    // Ensure TLS runs its initializer and return an error if it failed to
1250    // set up a separate stack for signal handlers.
1251    return TLS.with(|tls| {
1252        if let Tls::OutOfMemory = tls {
1253            Err(Trap::oom())
1254        } else {
1255            Ok(())
1256        }
1257    });
1258
1259    impl Drop for Tls {
1260        fn drop(&mut self) {
1261            let (ptr, size) = match self {
1262                Self::Allocated {
1263                    mmap_ptr,
1264                    mmap_size,
1265                } => (*mmap_ptr, *mmap_size),
1266                _ => return,
1267            };
1268            unsafe {
1269                // Deallocate the stack memory.
1270                let r = libc::munmap(ptr, size);
1271                debug_assert_eq!(r, 0, "munmap failed during thread shutdown");
1272            }
1273        }
1274    }
1275}
1276
1277#[cfg(test)]
1278mod tests {
1279    use super::*;
1280    use std::sync::Mutex;
1281
1282    // Guards tests that mutate global state (DEFAULT_STACK_SIZE, STACK_POOL).
1283    // Rust runs tests in parallel by default; this mutex serializes them so
1284    // they don't step on each other.
1285    static GLOBAL_STATE: Mutex<()> = Mutex::new(());
1286
1287    /// Saves the current stack size and restores it on drop (even on panic).
1288    struct RestoreStackSize(usize);
1289    impl Drop for RestoreStackSize {
1290        fn drop(&mut self) {
1291            set_stack_size(self.0);
1292        }
1293    }
1294
1295    #[test]
1296    fn max_stack_size_is_100mb() {
1297        assert_eq!(MAX_STACK_SIZE, ByteSize::mib(100).as_u64() as usize);
1298    }
1299
1300    #[test]
1301    fn get_set_stack_size_roundtrip() {
1302        let _lock = GLOBAL_STATE.lock().unwrap();
1303        let _restore = RestoreStackSize(get_stack_size());
1304        let new_size = ByteSize::mib(4).as_u64() as usize;
1305        set_stack_size(new_size);
1306        assert_eq!(get_stack_size(), new_size);
1307    }
1308
1309    #[test]
1310    fn set_stack_size_clamps_to_min() {
1311        let _lock = GLOBAL_STATE.lock().unwrap();
1312        let _restore = RestoreStackSize(get_stack_size());
1313        set_stack_size(1); // way below 8 KiB minimum
1314        assert_eq!(get_stack_size(), ByteSize::kib(8).as_u64() as usize);
1315    }
1316
1317    #[test]
1318    fn set_stack_size_clamps_to_max() {
1319        let _lock = GLOBAL_STATE.lock().unwrap();
1320        let _restore = RestoreStackSize(get_stack_size());
1321        set_stack_size(usize::MAX);
1322        assert_eq!(get_stack_size(), MAX_STACK_SIZE);
1323    }
1324
1325    #[test]
1326    fn drain_stack_pool_empties_pool() {
1327        let _lock = GLOBAL_STATE.lock().unwrap();
1328        let stack = DefaultStack::new(ByteSize::mib(1).as_u64() as usize).unwrap();
1329        STACK_POOL.push(stack);
1330        assert!(!STACK_POOL.is_empty());
1331        drain_stack_pool();
1332        assert!(STACK_POOL.is_empty());
1333    }
1334
1335    #[test]
1336    fn drain_stack_pool_is_idempotent() {
1337        let _lock = GLOBAL_STATE.lock().unwrap();
1338        drain_stack_pool();
1339        drain_stack_pool(); // second call on empty pool should not panic
1340        assert!(STACK_POOL.is_empty());
1341    }
1342
1343    /// The stack pool is not size-aware, so after a stack size increase it keeps
1344    /// serving cached undersized stacks. `drain_stack_pool()` breaks the cycle.
1345    ///
1346    /// 1. A call fills the pool with 500 KiB stacks (simulating normal execution).
1347    /// 2. The caller doubles the default to 1 MiB (simulating overflow retry).
1348    /// 3. WITHOUT draining, the pool still hands back a 500 KiB stack — the
1349    ///    retry would overflow again, creating an infinite loop.
1350    /// 4. After `drain_stack_pool()`, the pool is empty and the next allocation
1351    ///    must use the new, larger size.
1352    #[test]
1353    fn pool_returns_stale_stack_without_drain() {
1354        let _lock = GLOBAL_STATE.lock().unwrap();
1355        let _restore = RestoreStackSize(get_stack_size());
1356        drain_stack_pool();
1357
1358        // --- Phase 1: simulate normal execution that returns a 500 KiB stack ---
1359        let small_size = ByteSize::kib(500).as_u64() as usize;
1360        let small_stack = DefaultStack::new(small_size).unwrap();
1361        STACK_POOL.push(small_stack);
1362
1363        // --- Phase 2: "overflow detected" — caller doubles the default ---
1364        let big_size = ByteSize::mib(1).as_u64() as usize;
1365        set_stack_size(big_size);
1366        assert_eq!(get_stack_size(), big_size);
1367
1368        // --- Phase 3: WITHOUT drain, pool still returns the old small stack ---
1369        // This is the bug: the caller asked for a bigger stack but the pool
1370        // serves a cached undersized one, causing the retry to overflow again.
1371        let stale = STACK_POOL.pop();
1372        assert!(
1373            stale.is_some(),
1374            "pool should still contain the old stack (the bug scenario)"
1375        );
1376
1377        // --- Phase 4: with drain, pool is empty — next alloc uses new size ---
1378        STACK_POOL.push(stale.unwrap());
1379        drain_stack_pool();
1380        assert!(
1381            STACK_POOL.pop().is_none(),
1382            "after drain, pool must be empty so a fresh stack is allocated at the new size"
1383        );
1384    }
1385
1386    /// `on_wasm_stack` discards undersized stacks from the pool and allocates
1387    /// a fresh one instead of blindly reusing whatever the pool returns.
1388    #[test]
1389    fn on_wasm_stack_discards_undersized_stack() {
1390        let _lock = GLOBAL_STATE.lock().unwrap();
1391        let _restore = RestoreStackSize(get_stack_size());
1392        drain_stack_pool();
1393        clear_tls_stack();
1394
1395        // Push an undersized stack into the pool.
1396        let small_size = ByteSize::kib(500).as_u64() as usize;
1397        let small_stack = DefaultStack::new(small_size).unwrap();
1398        STACK_POOL.push(small_stack);
1399
1400        // Request a larger stack via on_wasm_stack.
1401        let big_size = ByteSize::mib(1).as_u64() as usize;
1402        let result = on_wasm_stack(big_size, None, || 42);
1403
1404        assert_eq!(result.expect("on_wasm_stack should succeed"), 42);
1405        // The undersized stack was discarded; the correctly-sized stack
1406        // allocated for the call now lives in the TLS cache (the hot path).
1407        // It will end up in the global pool only on thread exit or eviction.
1408        let returned = TLS_STACK
1409            .with(|cache| cache.0.take())
1410            .or_else(|| STACK_POOL.pop())
1411            .expect("stack should have been returned to TLS cache or pool");
1412        assert!(
1413            returned.size() >= big_size,
1414            "returned stack must be at least as large as the requested size"
1415        );
1416
1417        // Ensure no residual TLS state leaks into other tests sharing the
1418        // runner thread. `take()` above already cleared the slot, but be
1419        // explicit so future edits cannot drop this guarantee silently.
1420        clear_tls_stack();
1421    }
1422
1423    /// After a wasm call, the freshly-used stack stays in the thread-local
1424    /// cache so subsequent calls on the same thread reuse it without touching
1425    /// the global SegQueue.
1426    #[test]
1427    fn tls_stack_caches_after_first_call() {
1428        let _lock = GLOBAL_STATE.lock().unwrap();
1429        let _restore = RestoreStackSize(get_stack_size());
1430        drain_stack_pool();
1431        clear_tls_stack();
1432
1433        let size = get_stack_size();
1434
1435        // First call: TLS + pool both empty → allocate fresh; stack ends in TLS.
1436        assert!(on_wasm_stack(size, None, || ()).is_ok());
1437        assert!(
1438            STACK_POOL.is_empty(),
1439            "pool should still be empty after a TLS-served call"
1440        );
1441
1442        // Verify TLS holds a stack, then put it back.
1443        let cached_present = TLS_STACK.with(|cache| {
1444            let taken = cache.0.take();
1445            let present = taken.is_some();
1446            cache.0.set(taken);
1447            present
1448        });
1449        assert!(cached_present, "TLS slot should hold the post-call stack");
1450
1451        // Second call should consume from TLS; pool stays empty.
1452        assert!(on_wasm_stack(size, None, || ()).is_ok());
1453        assert!(
1454            STACK_POOL.is_empty(),
1455            "second call must not push to the global pool"
1456        );
1457        let still_cached = TLS_STACK.with(|cache| {
1458            let taken = cache.0.take();
1459            let present = taken.is_some();
1460            cache.0.set(taken);
1461            present
1462        });
1463        assert!(
1464            still_cached,
1465            "TLS slot should still hold a stack after the second call"
1466        );
1467
1468        // Cleanup: clear TLS so we don't leak into other tests. The cached
1469        // stack here is dropped rather than returned to the pool; the next
1470        // test starts with `drain_stack_pool()` anyway, so there is no
1471        // observable difference.
1472        clear_tls_stack();
1473    }
1474
1475    /// On thread exit, the TLS cache's `Drop` impl returns the held stack to
1476    /// the global pool so memory cycles correctly across thread lifetimes.
1477    #[test]
1478    fn tls_stack_returns_to_pool_on_thread_exit() {
1479        // GLOBAL_STATE is the test-suite mutex used to serialize tests that
1480        // touch shared global state (STACK_POOL, the configured stack size).
1481        // The spawned worker thread does NOT touch GLOBAL_STATE — it only
1482        // calls `on_wasm_stack`, which takes neither this mutex nor any
1483        // other lock that could contend with us.
1484        //
1485        // Even so, holding the guard across `handle.join()` is unnecessary:
1486        // the only thing that needs to be serialized against other tests is
1487        // the assertion on `STACK_POOL.pop()` AFTER the join. We release the
1488        // guard before joining so future edits to `on_wasm_stack` that
1489        // happen to touch this lock can't introduce a hard-to-debug
1490        // deadlock here.
1491        let lock = GLOBAL_STATE.lock().unwrap();
1492        let _restore = RestoreStackSize(get_stack_size());
1493        drain_stack_pool();
1494        clear_tls_stack();
1495
1496        let size = get_stack_size();
1497        drop(lock);
1498
1499        let handle = std::thread::spawn(move || {
1500            assert!(on_wasm_stack(size, None, || ()).is_ok());
1501        });
1502        handle.join().unwrap();
1503
1504        let _lock = GLOBAL_STATE.lock().unwrap();
1505        // The spawned thread's TLS cache was dropped on join; the stack must
1506        // have made it back to the global pool.
1507        let returned = STACK_POOL
1508            .pop()
1509            .expect("thread exit should return TLS-cached stack to the global pool");
1510        assert!(returned.size() >= size);
1511    }
1512
1513    // -----------------------------------------------------------------
1514    // Test helpers
1515    // -----------------------------------------------------------------
1516
1517    /// Clears the current thread's TLS slot so tests don't see state from
1518    /// previous tests (they share the same thread under cargo test's
1519    /// per-test serialization via `GLOBAL_STATE`).
1520    fn clear_tls_stack() {
1521        TLS_STACK.with(|cache| cache.0.set(None));
1522    }
1523
1524    /// `base().get() - limit().get()` (i.e. `Stack::size`) is constant per
1525    /// `DefaultStack` instance, but `base().get()` itself uniquely identifies
1526    /// the mmap allocation. We use it as a cheap identity check to see which
1527    /// stack was returned by acquire/release.
1528    fn stack_id(stack: &DefaultStack) -> usize {
1529        stack.base().get()
1530    }
1531
1532    // -----------------------------------------------------------------
1533    // acquire_stack mechanics
1534    // -----------------------------------------------------------------
1535
1536    #[test]
1537    fn acquire_allocates_fresh_when_tls_and_pool_empty() {
1538        let _lock = GLOBAL_STATE.lock().unwrap();
1539        let _restore = RestoreStackSize(get_stack_size());
1540        drain_stack_pool();
1541        clear_tls_stack();
1542
1543        let size = get_stack_size();
1544        let stack = acquire_stack(size);
1545        assert!(
1546            stack.size() >= size,
1547            "freshly allocated stack must satisfy min_size"
1548        );
1549
1550        drop(stack);
1551        clear_tls_stack();
1552        drain_stack_pool();
1553    }
1554
1555    #[test]
1556    fn acquire_prefers_tls_over_pool() {
1557        let _lock = GLOBAL_STATE.lock().unwrap();
1558        let _restore = RestoreStackSize(get_stack_size());
1559        drain_stack_pool();
1560        clear_tls_stack();
1561
1562        let size = get_stack_size();
1563        let tls_stack = DefaultStack::new(size).unwrap();
1564        let tls_id = stack_id(&tls_stack);
1565        TLS_STACK.with(|cache| cache.0.set(Some(tls_stack)));
1566
1567        let pool_stack = DefaultStack::new(size).unwrap();
1568        let pool_id = stack_id(&pool_stack);
1569        STACK_POOL.push(pool_stack);
1570
1571        let got = acquire_stack(size);
1572        assert_eq!(stack_id(&got), tls_id, "acquire must prefer TLS over pool");
1573        assert_ne!(stack_id(&got), pool_id);
1574
1575        drop(got);
1576        clear_tls_stack();
1577        drain_stack_pool();
1578    }
1579
1580    #[test]
1581    fn acquire_uses_pool_when_tls_empty() {
1582        let _lock = GLOBAL_STATE.lock().unwrap();
1583        let _restore = RestoreStackSize(get_stack_size());
1584        drain_stack_pool();
1585        clear_tls_stack();
1586
1587        let size = get_stack_size();
1588        let pool_stack = DefaultStack::new(size).unwrap();
1589        let pool_id = stack_id(&pool_stack);
1590        STACK_POOL.push(pool_stack);
1591
1592        let got = acquire_stack(size);
1593        assert_eq!(
1594            stack_id(&got),
1595            pool_id,
1596            "acquire must consume from pool when TLS is empty"
1597        );
1598        assert!(
1599            STACK_POOL.is_empty(),
1600            "pool stack must be removed when used"
1601        );
1602
1603        drop(got);
1604        clear_tls_stack();
1605        drain_stack_pool();
1606    }
1607
1608    #[test]
1609    fn acquire_discards_undersized_tls_then_allocates() {
1610        let _lock = GLOBAL_STATE.lock().unwrap();
1611        let _restore = RestoreStackSize(get_stack_size());
1612        drain_stack_pool();
1613        clear_tls_stack();
1614
1615        let small_size = ByteSize::kib(512).as_u64() as usize;
1616        let undersized = DefaultStack::new(small_size).unwrap();
1617        TLS_STACK.with(|cache| cache.0.set(Some(undersized)));
1618
1619        let big_size = ByteSize::mib(2).as_u64() as usize;
1620        let got = acquire_stack(big_size);
1621
1622        // The acquired stack must be at least the requested size. We do NOT
1623        // compare base addresses: the OS can reuse a freshly munmap'd
1624        // virtual address for the next mmap, so pointer identity is not a
1625        // reliable "is this a different stack" check across a drop+alloc.
1626        // The meaningful semantic is that the undersized stack was taken
1627        // out of rotation (TLS empty, not silently pushed to the pool) and
1628        // the returned stack is sized correctly.
1629        assert!(
1630            got.size() >= big_size,
1631            "acquired stack must satisfy big_size"
1632        );
1633        let tls_empty = TLS_STACK.with(|cache| {
1634            let s = cache.0.take();
1635            let empty = s.is_none();
1636            cache.0.set(s);
1637            empty
1638        });
1639        assert!(
1640            tls_empty,
1641            "undersized TLS stack must have been taken and discarded"
1642        );
1643        assert!(
1644            STACK_POOL.is_empty(),
1645            "undersized TLS stack must be discarded, not pushed to the pool",
1646        );
1647
1648        drop(got);
1649        clear_tls_stack();
1650        drain_stack_pool();
1651    }
1652
1653    #[test]
1654    fn acquire_discards_undersized_pool_then_allocates() {
1655        let _lock = GLOBAL_STATE.lock().unwrap();
1656        let _restore = RestoreStackSize(get_stack_size());
1657        drain_stack_pool();
1658        clear_tls_stack();
1659
1660        let small_size = ByteSize::kib(512).as_u64() as usize;
1661        let undersized = DefaultStack::new(small_size).unwrap();
1662        STACK_POOL.push(undersized);
1663
1664        let big_size = ByteSize::mib(2).as_u64() as usize;
1665        let got = acquire_stack(big_size);
1666
1667        // Same caveat as the TLS variant: mmap may reuse the virtual
1668        // address of the dropped undersized stack for the new big stack,
1669        // so we verify the semantic outcome — the pool was drained of the
1670        // undersized entry and the returned stack is sized correctly.
1671        assert!(
1672            got.size() >= big_size,
1673            "acquired stack must satisfy big_size"
1674        );
1675        assert!(
1676            STACK_POOL.is_empty(),
1677            "undersized pool stack must have been popped, filtered out and dropped",
1678        );
1679
1680        drop(got);
1681        clear_tls_stack();
1682        drain_stack_pool();
1683    }
1684
1685    // -----------------------------------------------------------------
1686    // release_stack mechanics
1687    // -----------------------------------------------------------------
1688
1689    #[test]
1690    fn release_into_empty_tls_caches_there() {
1691        let _lock = GLOBAL_STATE.lock().unwrap();
1692        drain_stack_pool();
1693        clear_tls_stack();
1694
1695        let size = get_stack_size();
1696        let stack = DefaultStack::new(size).unwrap();
1697        let id = stack_id(&stack);
1698        release_stack(stack);
1699
1700        let in_tls = TLS_STACK
1701            .with(|cache| cache.0.take())
1702            .expect("release into empty TLS should leave the stack in TLS");
1703        assert_eq!(stack_id(&in_tls), id);
1704        assert!(
1705            STACK_POOL.is_empty(),
1706            "pool must not be touched when TLS is empty"
1707        );
1708
1709        drain_stack_pool();
1710    }
1711
1712    #[test]
1713    fn release_into_occupied_tls_displaces_older_to_pool() {
1714        let _lock = GLOBAL_STATE.lock().unwrap();
1715        drain_stack_pool();
1716        clear_tls_stack();
1717
1718        let size = get_stack_size();
1719        let older = DefaultStack::new(size).unwrap();
1720        let older_id = stack_id(&older);
1721        TLS_STACK.with(|cache| cache.0.set(Some(older)));
1722
1723        let newer = DefaultStack::new(size).unwrap();
1724        let newer_id = stack_id(&newer);
1725        release_stack(newer);
1726
1727        let in_tls = TLS_STACK
1728            .with(|cache| cache.0.take())
1729            .expect("TLS should hold the newly-released stack");
1730        assert_eq!(
1731            stack_id(&in_tls),
1732            newer_id,
1733            "newer stack must displace into TLS"
1734        );
1735
1736        let displaced = STACK_POOL
1737            .pop()
1738            .expect("older stack should have been pushed to global pool");
1739        assert_eq!(
1740            stack_id(&displaced),
1741            older_id,
1742            "displaced stack must be the older one"
1743        );
1744
1745        drain_stack_pool();
1746    }
1747
1748    // -----------------------------------------------------------------
1749    // drain_stack_pool extended semantics
1750    // -----------------------------------------------------------------
1751
1752    #[test]
1753    fn drain_stack_pool_clears_calling_thread_tls_slot() {
1754        let _lock = GLOBAL_STATE.lock().unwrap();
1755        drain_stack_pool();
1756        clear_tls_stack();
1757
1758        let stack = DefaultStack::new(get_stack_size()).unwrap();
1759        TLS_STACK.with(|cache| cache.0.set(Some(stack)));
1760
1761        drain_stack_pool();
1762
1763        let tls_empty = TLS_STACK.with(|cache| cache.0.take().is_none());
1764        assert!(
1765            tls_empty,
1766            "drain_stack_pool must also clear current thread's TLS slot"
1767        );
1768        assert!(STACK_POOL.is_empty());
1769    }
1770
1771    // -----------------------------------------------------------------
1772    // on_wasm_stack functional behavior
1773    // -----------------------------------------------------------------
1774
1775    #[test]
1776    fn on_wasm_stack_passes_closure_value_back() {
1777        let _lock = GLOBAL_STATE.lock().unwrap();
1778        let _restore = RestoreStackSize(get_stack_size());
1779        drain_stack_pool();
1780        clear_tls_stack();
1781
1782        let r = on_wasm_stack(get_stack_size(), None, || 12345u32);
1783        assert_eq!(r.ok(), Some(12345));
1784
1785        clear_tls_stack();
1786        drain_stack_pool();
1787    }
1788
1789    #[test]
1790    fn on_wasm_stack_passes_owning_result_back() {
1791        let _lock = GLOBAL_STATE.lock().unwrap();
1792        let _restore = RestoreStackSize(get_stack_size());
1793        drain_stack_pool();
1794        clear_tls_stack();
1795
1796        // Use a heap-allocated value to verify the move-out path: the
1797        // closure produces a `Vec<u8>` that must travel from the coroutine
1798        // stack back to the host.
1799        let r = on_wasm_stack(get_stack_size(), None, || vec![0u8, 1, 2, 3, 4]);
1800        assert_eq!(r.ok(), Some(vec![0u8, 1, 2, 3, 4]));
1801
1802        clear_tls_stack();
1803        drain_stack_pool();
1804    }
1805
1806    #[test]
1807    fn many_calls_do_not_grow_global_pool() {
1808        let _lock = GLOBAL_STATE.lock().unwrap();
1809        let _restore = RestoreStackSize(get_stack_size());
1810        drain_stack_pool();
1811        clear_tls_stack();
1812
1813        // With TLS caching, repeated single-threaded calls must keep
1814        // reusing the same TLS stack and never push to the global pool.
1815        for _ in 0..1000 {
1816            assert!(on_wasm_stack(get_stack_size(), None, || ()).is_ok());
1817        }
1818        assert!(
1819            STACK_POOL.is_empty(),
1820            "1000 sequential calls should not grow the global pool (TLS handles reuse)"
1821        );
1822
1823        clear_tls_stack();
1824        drain_stack_pool();
1825    }
1826
1827    // -----------------------------------------------------------------
1828    // Trap and unwind paths
1829    // -----------------------------------------------------------------
1830
1831    #[test]
1832    fn raise_user_trap_yields_err() {
1833        let _lock = GLOBAL_STATE.lock().unwrap();
1834        let _restore = RestoreStackSize(get_stack_size());
1835        drain_stack_pool();
1836        clear_tls_stack();
1837
1838        let r: Result<(), UnwindReason> = on_wasm_stack(get_stack_size(), None, || unsafe {
1839            raise_user_trap(Box::new(io::Error::other("user trap from test")));
1840        });
1841        assert!(r.is_err(), "raise_user_trap must produce Err");
1842
1843        clear_tls_stack();
1844        drain_stack_pool();
1845    }
1846
1847    #[test]
1848    fn raise_lib_trap_yields_err() {
1849        let _lock = GLOBAL_STATE.lock().unwrap();
1850        let _restore = RestoreStackSize(get_stack_size());
1851        drain_stack_pool();
1852        clear_tls_stack();
1853
1854        let r: Result<(), UnwindReason> = on_wasm_stack(get_stack_size(), None, || unsafe {
1855            raise_lib_trap(Trap::lib(TrapCode::IntegerDivisionByZero));
1856        });
1857        assert!(r.is_err(), "raise_lib_trap must produce Err");
1858
1859        clear_tls_stack();
1860        drain_stack_pool();
1861    }
1862
1863    #[test]
1864    fn resume_panic_yields_err_without_unwinding() {
1865        // `resume_panic` packages the payload as `UnwindReason::Panic`. The
1866        // host-side panic resumption lives in `UnwindReason::into_trap`,
1867        // which we do NOT call here — `on_wasm_stack` itself just returns
1868        // the Err, so the test does not actually panic.
1869        let _lock = GLOBAL_STATE.lock().unwrap();
1870        let _restore = RestoreStackSize(get_stack_size());
1871        drain_stack_pool();
1872        clear_tls_stack();
1873
1874        let r: Result<(), UnwindReason> = on_wasm_stack(get_stack_size(), None, || unsafe {
1875            resume_panic(Box::new("panic payload from test"));
1876        });
1877        assert!(
1878            r.is_err(),
1879            "resume_panic must surface as Err to on_wasm_stack"
1880        );
1881
1882        clear_tls_stack();
1883        drain_stack_pool();
1884    }
1885
1886    #[test]
1887    fn trap_does_not_corrupt_subsequent_calls() {
1888        // After a trap, the per-call coroutine is force-reset and dropped.
1889        // The TLS stack cache and global pool must remain in a usable state
1890        // so that subsequent calls succeed.
1891        let _lock = GLOBAL_STATE.lock().unwrap();
1892        let _restore = RestoreStackSize(get_stack_size());
1893        drain_stack_pool();
1894        clear_tls_stack();
1895
1896        let trapped: Result<(), UnwindReason> = on_wasm_stack(get_stack_size(), None, || unsafe {
1897            raise_user_trap(Box::new(io::Error::other("first call traps")));
1898        });
1899        assert!(trapped.is_err());
1900
1901        // Subsequent normal call must still succeed.
1902        let ok = on_wasm_stack(get_stack_size(), None, || 7u32);
1903        assert_eq!(ok.ok(), Some(7), "calls after a trap must still work");
1904
1905        clear_tls_stack();
1906        drain_stack_pool();
1907    }
1908
1909    // -----------------------------------------------------------------
1910    // on_host_stack
1911    // -----------------------------------------------------------------
1912
1913    #[test]
1914    fn on_host_stack_outside_coroutine_runs_inline() {
1915        // `on_host_stack` outside any wasm coroutine just runs `f()` directly
1916        // (no stack switch); this asserts the no-yielder branch still works.
1917        let _lock = GLOBAL_STATE.lock().unwrap();
1918        let n = on_host_stack(|| 99i32);
1919        assert_eq!(n, 99);
1920    }
1921
1922    #[test]
1923    fn on_host_stack_inside_wasm_switches_and_returns() {
1924        let _lock = GLOBAL_STATE.lock().unwrap();
1925        let _restore = RestoreStackSize(get_stack_size());
1926        drain_stack_pool();
1927        clear_tls_stack();
1928
1929        let r = on_wasm_stack(get_stack_size(), None, || on_host_stack(|| 88i32));
1930        assert_eq!(r.ok(), Some(88));
1931
1932        clear_tls_stack();
1933        drain_stack_pool();
1934    }
1935
1936    // -----------------------------------------------------------------
1937    // Re-entrancy
1938    // -----------------------------------------------------------------
1939
1940    #[test]
1941    fn reentrant_call_returns_value() {
1942        let _lock = GLOBAL_STATE.lock().unwrap();
1943        let _restore = RestoreStackSize(get_stack_size());
1944        drain_stack_pool();
1945        clear_tls_stack();
1946
1947        let outer = on_wasm_stack(get_stack_size(), None, || {
1948            on_wasm_stack(get_stack_size(), None, || 42i32).expect("inner must succeed")
1949        });
1950        assert_eq!(outer.ok(), Some(42));
1951
1952        clear_tls_stack();
1953        drain_stack_pool();
1954    }
1955
1956    #[test]
1957    fn reentrant_calls_run_to_completion_under_pool_pressure() {
1958        // The outer call's stack is held by its scopeguard for the duration
1959        // of the call; the inner call must therefore pop a separate stack
1960        // from the pool (or allocate one). With a pre-populated pool the
1961        // inner call should consume that stack; either way the nested call
1962        // chain must complete without deadlocking or panicking from
1963        // corosensei.
1964        use std::sync::Arc;
1965        use std::sync::atomic::{AtomicUsize, Ordering as O};
1966
1967        let _lock = GLOBAL_STATE.lock().unwrap();
1968        let _restore = RestoreStackSize(get_stack_size());
1969        drain_stack_pool();
1970        clear_tls_stack();
1971
1972        // Pre-populate the pool with one stack so the inner call can grab
1973        // it instead of allocating.
1974        let pre = DefaultStack::new(get_stack_size()).unwrap();
1975        STACK_POOL.push(pre);
1976
1977        let inner_completed = Arc::new(AtomicUsize::new(0));
1978        let inner_completed_outer = inner_completed.clone();
1979        let _ = on_wasm_stack(get_stack_size(), None, move || {
1980            let inner_completed = inner_completed_outer.clone();
1981            let inner = on_wasm_stack(get_stack_size(), None, move || {
1982                inner_completed.fetch_add(1, O::Relaxed);
1983            });
1984            assert!(inner.is_ok(), "inner re-entrant call must succeed");
1985        });
1986        assert_eq!(
1987            inner_completed.load(O::Relaxed),
1988            1,
1989            "inner closure must have executed exactly once",
1990        );
1991
1992        clear_tls_stack();
1993        drain_stack_pool();
1994    }
1995
1996    #[test]
1997    fn reentrant_inner_trap_does_not_kill_outer() {
1998        let _lock = GLOBAL_STATE.lock().unwrap();
1999        let _restore = RestoreStackSize(get_stack_size());
2000        drain_stack_pool();
2001        clear_tls_stack();
2002
2003        let outer = on_wasm_stack(get_stack_size(), None, || {
2004            let inner: Result<i32, UnwindReason> =
2005                on_wasm_stack(get_stack_size(), None, || unsafe {
2006                    raise_user_trap(Box::new(io::Error::other("inner trap")));
2007                });
2008            // Outer observes inner's Err and recovers.
2009            match inner {
2010                Err(_) => 1234i32,
2011                Ok(_) => panic!("inner should have trapped"),
2012            }
2013        });
2014        assert_eq!(
2015            outer.ok(),
2016            Some(1234),
2017            "outer must recover after inner trap and run to completion"
2018        );
2019
2020        clear_tls_stack();
2021        drain_stack_pool();
2022    }
2023
2024    #[test]
2025    fn reentrant_with_on_host_stack_in_between() {
2026        // Outer wasm → on_host_stack → inner wasm. This exercises the
2027        // YIELDER save/restore in `unwind_with` / `on_host_stack` against
2028        // a re-entrant boundary.
2029        let _lock = GLOBAL_STATE.lock().unwrap();
2030        let _restore = RestoreStackSize(get_stack_size());
2031        drain_stack_pool();
2032        clear_tls_stack();
2033
2034        let r = on_wasm_stack(get_stack_size(), None, || {
2035            on_host_stack(|| {
2036                on_wasm_stack(get_stack_size(), None, || 5i32).expect("nested inner must succeed")
2037            })
2038        });
2039        assert_eq!(r.ok(), Some(5));
2040
2041        clear_tls_stack();
2042        drain_stack_pool();
2043    }
2044
2045    // -----------------------------------------------------------------
2046    // Concurrency
2047    // -----------------------------------------------------------------
2048
2049    #[test]
2050    fn many_threads_in_parallel_all_succeed() {
2051        let _lock = GLOBAL_STATE.lock().unwrap();
2052        let _restore = RestoreStackSize(get_stack_size());
2053        drain_stack_pool();
2054
2055        use std::sync::Arc;
2056        use std::sync::atomic::{AtomicUsize, Ordering as O};
2057
2058        let counter = Arc::new(AtomicUsize::new(0));
2059        const THREADS: usize = 8;
2060        const CALLS_PER_THREAD: usize = 200;
2061
2062        let handles: Vec<_> = (0..THREADS)
2063            .map(|_| {
2064                let counter = counter.clone();
2065                std::thread::spawn(move || {
2066                    let size = get_stack_size();
2067                    for _ in 0..CALLS_PER_THREAD {
2068                        if on_wasm_stack(size, None, || 1u32).ok() == Some(1) {
2069                            counter.fetch_add(1, O::Relaxed);
2070                        }
2071                    }
2072                })
2073            })
2074            .collect();
2075        for h in handles {
2076            h.join().unwrap();
2077        }
2078
2079        assert_eq!(counter.load(O::Relaxed), THREADS * CALLS_PER_THREAD);
2080        // Pool should now hold at most `THREADS` stacks (one per thread that
2081        // exited). Each thread also drops its TLS slot on exit, which pushes
2082        // the stack to the pool.
2083        let mut pooled = 0usize;
2084        while STACK_POOL.pop().is_some() {
2085            pooled += 1;
2086        }
2087        assert!(
2088            pooled <= THREADS,
2089            "pool should hold at most one stack per terminated thread (got {pooled} for {THREADS} threads)"
2090        );
2091
2092        clear_tls_stack();
2093        drain_stack_pool();
2094    }
2095
2096    // -----------------------------------------------------------------
2097    // Stack size dynamics
2098    // -----------------------------------------------------------------
2099
2100    #[test]
2101    fn growing_request_discards_smaller_tls_stack() {
2102        let _lock = GLOBAL_STATE.lock().unwrap();
2103        let _restore = RestoreStackSize(get_stack_size());
2104        drain_stack_pool();
2105        clear_tls_stack();
2106
2107        // First call at a small size populates TLS with a small stack.
2108        let small = ByteSize::mib(1).as_u64() as usize;
2109        set_stack_size(small);
2110        assert!(on_wasm_stack(small, None, || ()).is_ok());
2111
2112        let cached_size = TLS_STACK.with(|cache| {
2113            let s = cache.0.take();
2114            let sz = s.as_ref().map_or(0, |s| s.size());
2115            cache.0.set(s);
2116            sz
2117        });
2118        assert!(
2119            cached_size >= small,
2120            "TLS should hold the small-sized stack"
2121        );
2122
2123        // Now request a larger stack. acquire_stack should discard the TLS
2124        // entry and either pop a big-enough one from pool or allocate.
2125        let big = ByteSize::mib(4).as_u64() as usize;
2126        set_stack_size(big);
2127        assert!(on_wasm_stack(big, None, || ()).is_ok());
2128
2129        // The TLS slot should now hold a stack that's big enough.
2130        let cached_size = TLS_STACK.with(|cache| {
2131            let s = cache.0.take();
2132            let sz = s.as_ref().map_or(0, |s| s.size());
2133            cache.0.set(s);
2134            sz
2135        });
2136        assert!(
2137            cached_size >= big,
2138            "TLS should hold the bigger stack after size bump"
2139        );
2140
2141        clear_tls_stack();
2142        drain_stack_pool();
2143    }
2144}