Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
signals_tests.cpp
Go to the documentation of this file.
1#include <sysio/vm/signals.hpp>
2#include <chrono>
3#include <csignal>
4#include <thread>
5#include <iostream>
6
7#include <catch2/catch.hpp>
8
9struct test_exception {};
10
11TEST_CASE("Testing signals", "[invoke_with_signal_handler]") {
12 bool okay = false;
13 try {
15 std::raise(SIGSEGV);
16 }, [](int sig) {
17 throw test_exception{};
18 });
19 } catch(test_exception&) {
20 okay = true;
21 }
22 CHECK(okay);
23}
24
25TEST_CASE("Testing throw", "[signal_handler_throw]") {
28 }, [](int){}), sysio::vm::wasm_exit_exception);
29}
30
31static volatile sig_atomic_t sig_handled;
32
33static void handle_signal(int sig) {
34 sig_handled = 42 + sig;
35}
36
37static void handle_signal_sigaction(int sig, siginfo_t* info, void* uap) {
38 sig_handled = 142 + sig;
39}
40
41TEST_CASE("Test signal handler forwarding", "[signal_handler_forward]") {
42 // reset backup signal handlers
43 auto guard = sysio::vm::scope_guard{[]{
44 std::signal(SIGSEGV, SIG_DFL);
45 std::signal(SIGBUS, SIG_DFL);
46 std::signal(SIGFPE, SIG_DFL);
47 sysio::vm::setup_signal_handler_impl(); // This is normally only called once
48 }};
49 {
50 std::signal(SIGSEGV, &handle_signal);
51 std::signal(SIGBUS, &handle_signal);
52 std::signal(SIGFPE, &handle_signal);
54 sig_handled = 0;
55 std::raise(SIGSEGV);
56 CHECK(sig_handled == 42 + SIGSEGV);
57 sig_handled = 0;
58 std::raise(SIGBUS);
59 CHECK(sig_handled == 42 + SIGBUS);
60 sig_handled = 0;
61 std::raise(SIGFPE);
62 CHECK(sig_handled == 42 + SIGFPE);
63 }
64 {
65 struct sigaction sa;
66 sa.sa_sigaction = &handle_signal_sigaction;
67 sigemptyset(&sa.sa_mask);
68 sa.sa_flags = SA_NODEFER | SA_SIGINFO;
69 sigaction(SIGSEGV, &sa, nullptr);
70 sigaction(SIGBUS, &sa, nullptr);
71 sigaction(SIGFPE, &sa, nullptr);
73 sig_handled = 0;
74 std::raise(SIGSEGV);
75 CHECK(sig_handled == 142 + SIGSEGV);
76 sig_handled = 0;
77 std::raise(SIGBUS);
78 CHECK(sig_handled == 142 + SIGBUS);
79 sig_handled = 0;
80 std::raise(SIGFPE);
81 CHECK(sig_handled == 142 + SIGFPE);
82 }
83}
#define CHECK(cond)
Definition util.h:80
#define CHECK_THROWS_AS(expr, exceptionType)
Definition catch.hpp:203
#define TEST_CASE(...)
Definition catch.hpp:222
auto invoke_with_signal_handler(F &&f, E &&e)
Definition signals.hpp:123
void setup_signal_handler_impl()
Definition signals.hpp:94
void throw_(const char *msg)
Definition signals.hpp:84