2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/calls.h"
|
2021-10-14 00:27:13 +00:00
|
|
|
#include "libc/calls/struct/sigaction.h"
|
2023-07-10 11:29:46 +00:00
|
|
|
#include "libc/calls/ucontext.h"
|
2023-05-19 02:05:08 +00:00
|
|
|
#include "libc/dce.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/log/check.h"
|
|
|
|
#include "libc/log/log.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
2022-08-05 22:01:13 +00:00
|
|
|
#include "libc/sysv/consts/sa.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2022-08-05 22:01:13 +00:00
|
|
|
#include "libc/testlib/ezbench.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/testlib/testlib.h"
|
|
|
|
|
2022-09-05 15:41:43 +00:00
|
|
|
void OnUsr1(int sig) {
|
2020-11-25 16:19:00 +00:00
|
|
|
_exit(0);
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2022-07-25 05:34:13 +00:00
|
|
|
void SetUpOnce(void) {
|
2022-08-11 07:15:29 +00:00
|
|
|
ASSERT_SYS(0, 0, pledge("stdio proc", 0));
|
2022-06-27 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
TEST(signal, test) {
|
2022-04-12 12:20:17 +00:00
|
|
|
ASSERT_NE(SIG_ERR, signal(SIGUSR1, OnUsr1));
|
|
|
|
ASSERT_NE(-1, raise(SIGUSR1));
|
2020-11-25 16:19:00 +00:00
|
|
|
__die();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-08-05 22:01:13 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// signal round-trip delivery takes about 1µs
|
|
|
|
|
2022-09-02 12:08:35 +00:00
|
|
|
void OnSigTrap(int sig, struct siginfo *si, void *ctx) {
|
2022-08-05 22:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrapBench(int n) {
|
|
|
|
for (int i = 0; i < n; ++i) {
|
2023-05-13 05:42:57 +00:00
|
|
|
__builtin_trap();
|
2022-08-05 22:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BENCH(signal, trapBench) {
|
|
|
|
struct sigaction old;
|
|
|
|
struct sigaction sabus = {.sa_sigaction = OnSigTrap};
|
|
|
|
ASSERT_SYS(0, 0, sigaction(SIGTRAP, &sabus, &old));
|
|
|
|
EZBENCH_N("signal trap", 16, TrapBench(16));
|
|
|
|
EZBENCH_N("signal trap", 256, TrapBench(256));
|
|
|
|
EZBENCH_N("signal trap", 1024, TrapBench(1024));
|
|
|
|
sigaction(SIGTRAP, &old, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BENCH(signal, trapBenchSiginfo) {
|
|
|
|
struct sigaction old;
|
|
|
|
struct sigaction sabus = {.sa_sigaction = OnSigTrap, .sa_flags = SA_SIGINFO};
|
|
|
|
ASSERT_SYS(0, 0, sigaction(SIGTRAP, &sabus, &old));
|
|
|
|
EZBENCH_N("siginfo trap", 16, TrapBench(16));
|
|
|
|
EZBENCH_N("siginfo trap", 256, TrapBench(256));
|
|
|
|
EZBENCH_N("siginfo trap", 1024, TrapBench(1024));
|
|
|
|
sigaction(SIGTRAP, &old, 0);
|
|
|
|
}
|
|
|
|
|
2023-05-13 05:42:57 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2022-09-02 12:08:35 +00:00
|
|
|
void OnSigHlt(int sig, struct siginfo *si, void *vctx) {
|
|
|
|
struct ucontext *ctx = vctx;
|
2022-08-05 22:01:13 +00:00
|
|
|
ctx->uc_mcontext.rip += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HltBench(int n) {
|
|
|
|
for (int i = 0; i < n; ++i) {
|
|
|
|
asm("hlt");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BENCH(signal, hltBenchSiginfo) {
|
|
|
|
struct sigaction old[2];
|
|
|
|
struct sigaction sabus = {.sa_sigaction = OnSigHlt, .sa_flags = SA_SIGINFO};
|
|
|
|
ASSERT_SYS(0, 0, sigaction(SIGSEGV, &sabus, old + 0));
|
|
|
|
ASSERT_SYS(0, 0, sigaction(SIGBUS, &sabus, old + 1));
|
|
|
|
EZBENCH_N("siginfo hlt", 16, HltBench(16));
|
|
|
|
EZBENCH_N("siginfo hlt", 256, HltBench(256));
|
|
|
|
EZBENCH_N("siginfo hlt", 1024, HltBench(1024));
|
|
|
|
sigaction(SIGSEGV, old + 0, 0);
|
|
|
|
sigaction(SIGBUS, old + 1, 0);
|
|
|
|
}
|
2023-05-13 05:42:57 +00:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|