corosensei 0.3.4

A fast and safe implementation of stackful coroutines
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
extern crate alloc;
extern crate std;

use core::sync::atomic::{AtomicBool, Ordering};
use std::cell::Cell;
use std::rc::Rc;
use std::string::ToString;
use std::{println, ptr};

use alloc::sync::Arc;

use crate::{Coroutine, CoroutineResult};

#[test]
fn smoke() {
    let hit = Rc::new(Cell::new(false));
    let hit2 = hit.clone();
    let mut coroutine = Coroutine::<(), (), ()>::new(move |_, _| {
        hit2.set(true);
    });
    assert!(!hit.get());
    assert!(!coroutine.started());
    assert!(!coroutine.done());
    assert_eq!(coroutine.resume(()), CoroutineResult::Return(()));
    assert!(hit.get());
    assert!(coroutine.started());
    assert!(coroutine.done());
}

#[test]
fn suspend_and_resume() {
    let hit = Rc::new(Cell::new(false));
    let hit2 = hit.clone();
    let mut coroutine = Coroutine::<(), (), ()>::new(move |y, _| {
        y.suspend(());
        hit2.set(true);
        y.suspend(());
    });
    assert!(!hit.get());
    assert!(!coroutine.started());
    assert!(!coroutine.done());
    assert_eq!(coroutine.resume(()), CoroutineResult::Yield(()));
    assert!(!hit.get());
    assert!(coroutine.started());
    assert!(!coroutine.done());
    assert_eq!(coroutine.resume(()), CoroutineResult::Yield(()));
    assert!(hit.get());
    assert!(coroutine.started());
    assert!(!coroutine.done());
    assert_eq!(coroutine.resume(()), CoroutineResult::Return(()));
    assert!(hit.get());
    assert!(coroutine.started());
    assert!(coroutine.done());
}

// Linked backtraces are not supported on x86 Windows.
#[cfg_attr(all(windows, target_arch = "x86"), ignore)]
#[test]
fn backtrace_traces_to_host() {
    #[inline(never)] // try to get this to show up in backtraces
    fn look_for_me() {
        run_test();
    }
    fn assert_contains_host() {
        let trace = backtrace::Backtrace::new();
        println!("{:?}", trace);
        assert!(trace
            .frames()
            .iter()
            .flat_map(|f| f.symbols())
            .filter_map(|s| Some(s.name()?.to_string()))
            .any(|s| s.contains("look_for_me")));
    }

    fn run_test() {
        assert_contains_host();
        let mut coroutine = Coroutine::<(), (), ()>::new(move |y, ()| {
            assert_contains_host();
            y.suspend(());
            y.on_parent_stack(|| assert_contains_host());
            y.suspend(());
            assert_contains_host();
        });
        assert_eq!(coroutine.resume(()), CoroutineResult::Yield(()));
        assert_eq!(coroutine.resume(()), CoroutineResult::Yield(()));
        assert_eq!(coroutine.resume(()), CoroutineResult::Return(()));
    }

    look_for_me();
}

#[cfg(feature = "unwind")]
#[test]
#[should_panic(expected = "foobar")]
fn panics_propagated() {
    use std::panic::{self, AssertUnwindSafe};

    let a = Rc::new(Cell::new(false));
    let b = SetOnDrop(a.clone());
    let mut coroutine = Coroutine::<(), (), ()>::new(move |_, ()| {
        drop(b);
        panic!("foobar");
    });
    let result = panic::catch_unwind(AssertUnwindSafe(|| coroutine.resume(())));
    assert!(result.is_err());
    assert!(a.get());
    panic::resume_unwind(result.unwrap_err());

    struct SetOnDrop(Rc<Cell<bool>>);

    impl Drop for SetOnDrop {
        fn drop(&mut self) {
            self.0.set(true);
        }
    }
}

#[cfg(feature = "unwind")]
#[test]
#[should_panic(expected = "foobar")]
fn panics_propagated_via_parent() {
    use std::panic::{self, AssertUnwindSafe};

    let a = Rc::new(Cell::new(false));
    let b = SetOnDrop(a.clone());
    let mut coroutine = Coroutine::<(), (), ()>::new(move |y, ()| {
        drop(b);
        y.on_parent_stack(|| {
            panic!("foobar");
        });
    });
    let result = panic::catch_unwind(AssertUnwindSafe(|| coroutine.resume(())));
    assert!(result.is_err());
    assert!(a.get());
    panic::resume_unwind(result.unwrap_err());

    struct SetOnDrop(Rc<Cell<bool>>);

    impl Drop for SetOnDrop {
        fn drop(&mut self) {
            self.0.set(true);
        }
    }
}

#[test]
fn suspend_and_resume_values() {
    let mut coroutine = Coroutine::new(move |y, first| {
        assert_eq!(first, 2.0);
        assert_eq!(y.suspend(4), 3.0);
        "hello".to_string()
    });
    assert_eq!(coroutine.resume(2.0), CoroutineResult::Yield(4));
    assert_eq!(
        coroutine.resume(3.0),
        CoroutineResult::Return("hello".to_string())
    );
}

#[test]
fn stateful() {
    #[allow(dead_code)]
    #[repr(align(128))]
    #[allow(dead_code)]
    struct Aligned(u8);
    let state = [41, 42, 43, 44, 45];
    let aligned = Aligned(100);
    let mut coroutine = Coroutine::new(move |y, _| {
        assert_eq!(&aligned as *const _ as usize % 128, 0);
        for i in state {
            y.suspend(i);
        }
    });
    for i in state {
        assert_eq!(coroutine.resume(()), CoroutineResult::Yield(i));
    }
    assert_eq!(coroutine.resume(()), CoroutineResult::Return(()));
}

#[test]
fn force_unwind() {
    struct SetOnDrop(Arc<AtomicBool>);
    impl Drop for SetOnDrop {
        fn drop(&mut self) {
            self.0.store(true, Ordering::Relaxed);
        }
    }

    let a = Arc::new(AtomicBool::new(false));
    let b = Arc::new(AtomicBool::new(false));
    let a_drop = SetOnDrop(a.clone());
    let b_drop = SetOnDrop(b.clone());
    let mut coroutine = Coroutine::<(), (), (), _>::new(move |y, ()| {
        drop(a_drop);
        y.suspend(());
        drop(b_drop);
    });
    assert!(!coroutine.started());
    assert!(!coroutine.done());
    coroutine.force_unwind();
    assert!(coroutine.started());
    assert!(coroutine.done());
    drop(coroutine);
    assert!(a.load(Ordering::Relaxed));
    assert!(b.load(Ordering::Relaxed));

    #[cfg(feature = "unwind")]
    {
        let a = Arc::new(AtomicBool::new(false));
        let b = Arc::new(AtomicBool::new(false));
        let a_drop = SetOnDrop(a.clone());
        let b_drop = SetOnDrop(b.clone());
        let mut coroutine = Coroutine::<(), (), (), _>::new(move |y, ()| {
            drop(a_drop);
            y.suspend(());
            drop(b_drop);
        });
        coroutine.resume(());
        assert!(coroutine.started());
        assert!(!coroutine.done());
        coroutine.force_unwind();
        assert!(coroutine.started());
        assert!(coroutine.done());
        drop(coroutine);
        assert!(a.load(Ordering::Relaxed));
        assert!(b.load(Ordering::Relaxed));
    }

    #[cfg(feature = "unwind")]
    {
        use std::panic::{self, AssertUnwindSafe};

        let a = Arc::new(AtomicBool::new(false));
        let b = Arc::new(AtomicBool::new(false));
        let a_drop = SetOnDrop(a.clone());
        let b_drop = SetOnDrop(b.clone());
        let mut coroutine = Coroutine::<(), (), (), _>::new(move |y, ()| {
            drop(a_drop);
            _ = panic::catch_unwind(AssertUnwindSafe(|| y.suspend(())));
            drop(b_drop);
        });
        coroutine.resume(());
        assert!(coroutine.started());
        assert!(!coroutine.done());
        coroutine.force_unwind();
        assert!(coroutine.started());
        assert!(coroutine.done());
        drop(coroutine);
        assert!(a.load(Ordering::Relaxed));
        assert!(b.load(Ordering::Relaxed));
    }

    #[cfg(feature = "unwind")]
    {
        use std::panic::{self, AssertUnwindSafe};

        let a = Arc::new(AtomicBool::new(false));
        let b = Arc::new(AtomicBool::new(false));
        let a_drop = SetOnDrop(a.clone());
        let b_drop = SetOnDrop(b.clone());
        let mut coroutine = Coroutine::<(), (), (), _>::new(move |y, ()| {
            drop(a_drop);
            _ = panic::catch_unwind(AssertUnwindSafe(|| y.suspend(())));
            y.suspend(());
            drop(b_drop);
        });
        coroutine.resume(());
        assert!(coroutine.started());
        assert!(!coroutine.done());
        coroutine.force_unwind();
        assert!(coroutine.started());
        assert!(coroutine.done());
        drop(coroutine);
        assert!(a.load(Ordering::Relaxed));
        assert!(b.load(Ordering::Relaxed));
    }

    let a = Arc::new(AtomicBool::new(false));
    let b = Arc::new(AtomicBool::new(false));
    let a_drop = SetOnDrop(a.clone());
    let b_drop = SetOnDrop(b.clone());
    let mut coroutine = Coroutine::<(), (), (), _>::new(move |y, ()| {
        drop(a_drop);
        y.suspend(());
        drop(b_drop);
    });
    coroutine.resume(());
    coroutine.resume(());
    assert!(coroutine.started());
    assert!(coroutine.done());
    coroutine.force_unwind();
    assert!(coroutine.started());
    assert!(coroutine.done());
    drop(coroutine);
    assert!(a.load(Ordering::Relaxed));
    assert!(b.load(Ordering::Relaxed));
}

// The Windows stack starts out small with only one page committed. Check that it
// gets properly grown by the kernel as needed.
#[test]
fn stack_growth() {
    let mut coroutine = Coroutine::<(), (), ()>::new(|_, ()| {
        fn recurse(i: u32, p: &mut [u8; 10000]) {
            unsafe {
                // Ensure the stack allocation isn't optimized away.
                ptr::read_volatile(&p);
            }
            if i > 0 {
                recurse(i - 1, &mut [0; 10000]);
            }
        }

        // Use ~500KB of stack.
        recurse(50, &mut [0; 10000]);
    });
    assert_eq!(coroutine.resume(()), CoroutineResult::Return(()));
}

#[cfg(feature = "unwind")]
#[test]
#[should_panic = "foobar"]
fn backward_stack_address() {
    use crate::stack::DefaultStack;

    // If we create the thread after the stack then (at least on Linux) we
    // should have the thread stack at a lower address than the coroutine
    // stack.
    let stack = DefaultStack::new(1024 * 1024).unwrap();
    let result = std::thread::spawn(move || {
        let mut coroutine = Coroutine::<(), (), ()>::with_stack(stack, move |_, ()| {
            panic!("foobar");
        });

        coroutine.resume(());
        unreachable!();
    })
    .join();
    std::panic::resume_unwind(result.unwrap_err());
}

#[cfg(feature = "unwind")]
#[test]
#[should_panic = "foobar"]
fn forward_stack_address() {
    use crate::stack::DefaultStack;

    // Just in case, also test allocating the stack after thread creation. This
    // should cover all cases.
    let result = std::thread::spawn(move || {
        let stack = DefaultStack::new(1024 * 1024).unwrap();
        let mut coroutine = Coroutine::<(), (), ()>::with_stack(stack, move |_, ()| {
            panic!("foobar");
        });

        coroutine.resume(());
        unreachable!();
    })
    .join();
    std::panic::resume_unwind(result.unwrap_err());
}

#[test]
fn trap_handler() {
    trap_handler::setup_handler();
    let mut coroutine = Coroutine::new(|y, ()| {
        unsafe {
            println!("Before trap");
            ptr::write_volatile(1 as *mut u8, 0);
            println!("After trap");
        }
        y.suspend(1);
        2
    });
    trap_handler::HANDLER.with(|x| {
        x.set(Some((coroutine.trap_handler(), |h| unsafe {
            h.setup_trap_handler(|| 42)
        })))
    });
    assert_eq!(coroutine.resume(()), CoroutineResult::Return(42));
}

#[cfg(feature = "unwind")]
#[test]
#[should_panic = "foobar"]
fn trap_handler_panic() {
    trap_handler::setup_handler();
    let mut coroutine = Coroutine::new(|y, ()| {
        unsafe {
            println!("Before trap");
            ptr::write_volatile(1 as *mut u8, 0);
            println!("After trap");
        }
        y.suspend(1);
        2
    });
    trap_handler::HANDLER.with(|x| {
        x.set(Some((coroutine.trap_handler(), |h| unsafe {
            h.setup_trap_handler(|| panic!("foobar"))
        })))
    });
    assert_eq!(coroutine.resume(()), CoroutineResult::Return(42));
}

#[test]
fn stack_overflow() {
    trap_handler::setup_handler();
    let mut coroutine = Coroutine::new(|y, ()| {
        #[allow(unconditional_recursion)]
        fn recurse(p: &mut [u8; 100]) {
            unsafe {
                // Ensure the stack allocation isn't optimized away.
                ptr::read_volatile(&p);
            }
            recurse(&mut [0; 100]);
        }

        recurse(&mut [0; 100]);
        y.suspend(1);
        2
    });
    trap_handler::HANDLER.with(|x| {
        x.set(Some((coroutine.trap_handler(), |h| unsafe {
            h.setup_trap_handler(|| 42)
        })))
    });
    assert_eq!(coroutine.resume(()), CoroutineResult::Return(42));

    println!("Recovered from first stack overflow");

    // Now reuse the stack and do it again. Make sure the guard pages are
    // properly reset on Windows.
    let stack = coroutine.into_stack();
    let mut coroutine = Coroutine::with_stack(stack, |y, ()| {
        #[allow(unconditional_recursion)]
        fn recurse(p: &mut [u8; 100]) {
            unsafe {
                // Ensure the stack allocation isn't optimized away.
                ptr::read_volatile(&p);
            }
            recurse(&mut [0; 100]);
        }

        recurse(&mut [0; 100]);
        y.suspend(1);
        2
    });
    trap_handler::HANDLER.with(|x| {
        x.set(Some((coroutine.trap_handler(), |h| unsafe {
            h.setup_trap_handler(|| 42)
        })))
    });
    assert_eq!(coroutine.resume(()), CoroutineResult::Return(42));

    println!("Recovered from second stack overflow");
}

/// Helper module for setting up a trap handler.
mod trap_handler {
    extern crate std;

    use std::cell::Cell;
    use std::{io, thread_local};

    use crate::trap::{CoroutineTrapHandler, TrapHandlerRegs};

    thread_local! {
        pub static HANDLER: Cell<
            Option<(
                CoroutineTrapHandler<i32>,
                fn(&CoroutineTrapHandler<i32>) -> TrapHandlerRegs,
            )>,
        > = Cell::new(None);
    }

    #[cfg(unix)]
    pub fn setup_handler() {
        use std::{mem, ptr};
        unsafe extern "C" fn signal_handler(
            _signum: libc::c_int,
            _siginfo: &libc::siginfo_t,
            context: &mut libc::ucontext_t,
        ) {
            let (handler, f) = HANDLER.with(|x| x.get().unwrap());

            let sp;
            cfg_if::cfg_if! {
                if #[cfg(all(
                    any(target_os = "linux", target_os = "android"),
                    target_arch = "x86_64",
                ))] {
                    sp = context.uc_mcontext.gregs[libc::REG_RSP as usize] as usize;
                } else if #[cfg(all(
                    any(target_os = "linux", target_os = "android"),
                    target_arch = "x86",
                ))] {
                    sp = context.uc_mcontext.gregs[libc::REG_ESP as usize] as usize;
                } else if #[cfg(all(target_vendor = "apple", target_arch = "x86_64"))] {
                    sp = (*context.uc_mcontext).__ss.__rsp as usize;
                } else if #[cfg(all(
                        any(target_os = "linux", target_os = "android"),
                        target_arch = "aarch64",
                    ))] {
                    sp = context.uc_mcontext.sp as usize;
                } else if #[cfg(all(
                    any(target_os = "linux", target_os = "android"),
                    target_arch = "arm",
                ))] {
                    sp = context.uc_mcontext.arm_sp as usize;
                } else if #[cfg(all(
                    any(target_os = "linux", target_os = "android"),
                    any(target_arch = "riscv64", target_arch = "riscv32"),
                ))] {
                    sp = context.uc_mcontext.__gregs[libc::REG_SP] as usize;
                } else if #[cfg(all(target_vendor = "apple", target_arch = "aarch64"))] {
                    sp = (*context.uc_mcontext).__ss.__sp as usize;
                } else if #[cfg(all(target_os = "linux", target_arch = "loongarch64"))] {
                    sp = context.uc_mcontext.__gregs[3]  as usize;
                } else if #[cfg(all(target_os = "linux", target_arch = "powerpc64"))] {
                    sp = (*context.uc_mcontext.regs).gpr[1]  as usize;
                } else {
                    compile_error!("Unsupported platform");
                }
            };

            assert!(handler.stack_ptr_in_bounds(sp));
            let regs = f(&handler);

            cfg_if::cfg_if! {
                if #[cfg(all(
                        any(target_os = "linux", target_os = "android"),
                        target_arch = "x86_64",
                    ))] {
                    let TrapHandlerRegs { rip, rsp, rbp, rdi, rsi } = regs;
                    context.uc_mcontext.gregs[libc::REG_RIP as usize] = rip as i64;
                    context.uc_mcontext.gregs[libc::REG_RSP as usize] = rsp as i64;
                    context.uc_mcontext.gregs[libc::REG_RBP as usize] = rbp as i64;
                    context.uc_mcontext.gregs[libc::REG_RDI as usize] = rdi as i64;
                    context.uc_mcontext.gregs[libc::REG_RSI as usize] = rsi as i64;
                } else if #[cfg(all(
                    any(target_os = "linux", target_os = "android"),
                    target_arch = "x86",
                ))] {
                    let TrapHandlerRegs { eip, esp, ebp, ecx, edx } = regs;
                    context.uc_mcontext.gregs[libc::REG_EIP as usize] = eip as i32;
                    context.uc_mcontext.gregs[libc::REG_ESP as usize] = esp as i32;
                    context.uc_mcontext.gregs[libc::REG_EBP as usize] = ebp as i32;
                    context.uc_mcontext.gregs[libc::REG_ECX as usize] = ecx as i32;
                    context.uc_mcontext.gregs[libc::REG_EDX as usize] = edx as i32;
                } else if #[cfg(all(target_vendor = "apple", target_arch = "x86_64"))] {
                    let TrapHandlerRegs { rip, rsp, rbp, rdi, rsi } = regs;
                    (*context.uc_mcontext).__ss.__rip = rip;
                    (*context.uc_mcontext).__ss.__rsp = rsp;
                    (*context.uc_mcontext).__ss.__rbp = rbp;
                    (*context.uc_mcontext).__ss.__rdi = rdi;
                    (*context.uc_mcontext).__ss.__rsi = rsi;
                } else if #[cfg(all(
                        any(target_os = "linux", target_os = "android"),
                        target_arch = "aarch64",
                    ))] {
                    let TrapHandlerRegs { pc, sp, x0, x1, x29, lr } = regs;
                    context.uc_mcontext.pc = pc;
                    context.uc_mcontext.sp = sp;
                    context.uc_mcontext.regs[0] = x0;
                    context.uc_mcontext.regs[1] = x1;
                    context.uc_mcontext.regs[29] = x29;
                    context.uc_mcontext.regs[30] = lr;
                } else if #[cfg(all(
                        any(target_os = "linux", target_os = "android"),
                        target_arch = "arm",
                    ))] {
                    let TrapHandlerRegs {
                        pc,
                        r0,
                        r1,
                        r7,
                        r11,
                        r13,
                        r14,
                        cpsr_thumb,
                        cpsr_endian,
                    } = regs;
                    context.uc_mcontext.arm_pc = pc;
                    context.uc_mcontext.arm_r0 = r0;
                    context.uc_mcontext.arm_r1 = r1;
                    context.uc_mcontext.arm_r7 = r7;
                    context.uc_mcontext.arm_fp = r11;
                    context.uc_mcontext.arm_sp = r13;
                    context.uc_mcontext.arm_lr = r14;
                    if cpsr_thumb {
                        context.uc_mcontext.arm_cpsr |= 0x20;
                    } else {
                        context.uc_mcontext.arm_cpsr &= !0x20;
                    }
                    if cpsr_endian {
                        context.uc_mcontext.arm_cpsr |= 0x200;
                    } else {
                        context.uc_mcontext.arm_cpsr &= !0x200;
                    }
                } else if #[cfg(all(
                    any(target_os = "linux", target_os = "android"),
                    any(target_arch = "riscv64", target_arch = "riscv32"),
                ))] {
                    let TrapHandlerRegs { pc, ra, sp, a0, a1, s0 } = regs;
                    context.uc_mcontext.__gregs[libc::REG_PC] = pc as libc::c_ulong;
                    context.uc_mcontext.__gregs[libc::REG_RA] = ra as libc::c_ulong;
                    context.uc_mcontext.__gregs[libc::REG_SP] = sp as libc::c_ulong;
                    context.uc_mcontext.__gregs[libc::REG_A0] = a0 as libc::c_ulong;
                    context.uc_mcontext.__gregs[libc::REG_A0 + 1] = a1 as libc::c_ulong;
                    context.uc_mcontext.__gregs[libc::REG_S0] = s0 as libc::c_ulong;
                } else if #[cfg(all(target_vendor = "apple", target_arch = "aarch64"))] {
                    let TrapHandlerRegs { pc, sp, x0, x1, x29, lr } = regs;
                    (*context.uc_mcontext).__ss.__pc = pc;
                    (*context.uc_mcontext).__ss.__sp = sp;
                    (*context.uc_mcontext).__ss.__x[0] = x0;
                    (*context.uc_mcontext).__ss.__x[1] = x1;
                    (*context.uc_mcontext).__ss.__fp = x29;
                    (*context.uc_mcontext).__ss.__lr = lr;
                } else if #[cfg(all(target_os = "linux", target_arch = "loongarch64"))] {
                    let TrapHandlerRegs { pc, sp, a0, a1, fp, ra } = regs;
                    context.uc_mcontext.__pc = pc;
                    context.uc_mcontext.__gregs[1] = ra;
                    context.uc_mcontext.__gregs[3] = sp;
                    context.uc_mcontext.__gregs[4] = a0;
                    context.uc_mcontext.__gregs[5] = a1;
                    context.uc_mcontext.__gregs[22] = fp;
                } else if #[cfg(all(target_os = "linux", target_arch = "powerpc64"))] {
                    let TrapHandlerRegs { pc, sp, r3, r4, r12, lr } = regs;
                    (*context.uc_mcontext.regs).nip = pc;
                    (*context.uc_mcontext.regs).gpr[1] = sp;
                    (*context.uc_mcontext.regs).gpr[3] = r3;
                    (*context.uc_mcontext.regs).gpr[4] = r4;
                    (*context.uc_mcontext.regs).gpr[12] = r12;
                    (*context.uc_mcontext.regs).link = lr;
                } else {
                    compile_error!("Unsupported platform");
                }
            };
        }

        unsafe {
            let mut handler: libc::sigaction = mem::zeroed();
            handler.sa_flags = libc::SA_SIGINFO | libc::SA_ONSTACK;
            handler.sa_sigaction = signal_handler as usize;
            libc::sigfillset(&mut handler.sa_mask);
            if libc::sigaction(libc::SIGSEGV, &handler, ptr::null_mut()) != 0 {
                panic!(
                    "unable to install signal handler: {}",
                    io::Error::last_os_error(),
                );
            }
            if libc::sigaction(libc::SIGBUS, &handler, ptr::null_mut()) != 0 {
                panic!(
                    "unable to install signal handler: {}",
                    io::Error::last_os_error(),
                );
            }
        }
    }

    #[cfg(windows)]
    pub fn setup_handler() {
        use windows_sys::Win32::Foundation::{
            EXCEPTION_ACCESS_VIOLATION, EXCEPTION_STACK_OVERFLOW,
        };
        use windows_sys::Win32::System::Diagnostics::Debug::{
            AddVectoredExceptionHandler, EXCEPTION_POINTERS,
        };

        unsafe extern "system" fn exception_handler(
            exception_info: *mut EXCEPTION_POINTERS,
        ) -> i32 {
            match (*(*exception_info).ExceptionRecord).ExceptionCode {
                EXCEPTION_ACCESS_VIOLATION | EXCEPTION_STACK_OVERFLOW => {}
                _ => return 0, // EXCEPTION_CONTINUE_SEARCH
            }

            let (handler, f) = match HANDLER.with(|x| x.get()) {
                Some(handler) => handler,
                None => return 0, // EXCEPTION_CONTINUE_SEARCH
            };

            let sp;
            cfg_if::cfg_if! {
                if #[cfg(target_arch = "x86_64")] {
                    sp = (*(*exception_info).ContextRecord).Rsp as usize;
                } else if #[cfg(target_arch = "x86")] {
                    sp = (*(*exception_info).ContextRecord).Esp as usize;
                } else {
                    compile_error!("Unsupported platform");
                }
            };

            if !handler.stack_ptr_in_bounds(sp) {
                return 0; // EXCEPTION_CONTINUE_SEARCH
            }
            let regs = f(&handler);

            cfg_if::cfg_if! {
                if #[cfg(target_arch = "x86_64")] {
                    let TrapHandlerRegs { rip, rsp, rbp, rdi, rsi } = regs;
                    (*(*exception_info).ContextRecord).Rip = rip;
                    (*(*exception_info).ContextRecord).Rsp = rsp;
                    (*(*exception_info).ContextRecord).Rbp = rbp;
                    (*(*exception_info).ContextRecord).Rdi = rdi;
                    (*(*exception_info).ContextRecord).Rsi = rsi;
                } else if #[cfg(target_arch = "x86")] {
                    let TrapHandlerRegs { eip, esp, ebp,eax, ebx, ecx, edx } = regs;
                    (*(*exception_info).ContextRecord).Eip = eip;
                    (*(*exception_info).ContextRecord).Esp = esp;
                    (*(*exception_info).ContextRecord).Ebp = ebp;
                    (*(*exception_info).ContextRecord).Eax = eax;
                    (*(*exception_info).ContextRecord).Ebx = ebx;
                    (*(*exception_info).ContextRecord).Ecx = ecx;
                    (*(*exception_info).ContextRecord).Edx = edx;
                } else {
                    compile_error!("Unsupported platform");
                }
            };

            // EXCEPTION_CONTINUE_EXECUTION is -1. Not to be confused with
            // ExceptionContinueExecution which has a value of 0.
            -1
        }

        unsafe {
            if AddVectoredExceptionHandler(1, Some(exception_handler)).is_null() {
                panic!(
                    "failed to add exception handler: {}",
                    io::Error::last_os_error()
                );
            }
        }
    }
}