From 9e6faa52566575b448b5c29b322e05becf893f4b Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Mon, 1 Jan 2024 00:00:23 -0800 Subject: [PATCH] Fix --ftrace on Windows --- libc/calls/finddebugbinary.c | 7 ++-- test/posix/sa_resethand2_test.c | 74 +++++++++++++++++++++++++++++++++ third_party/readline/signals.c | 1 - 3 files changed, 78 insertions(+), 4 deletions(-) create mode 100644 test/posix/sa_resethand2_test.c diff --git a/libc/calls/finddebugbinary.c b/libc/calls/finddebugbinary.c index f19658556..b4bae854e 100644 --- a/libc/calls/finddebugbinary.c +++ b/libc/calls/finddebugbinary.c @@ -26,11 +26,11 @@ #include "libc/elf/def.h" #include "libc/elf/tinyelf.internal.h" #include "libc/errno.h" -#include "libc/serialize.h" #include "libc/intrin/directmap.internal.h" #include "libc/nt/memory.h" #include "libc/nt/runtime.h" #include "libc/runtime/runtime.h" +#include "libc/serialize.h" #include "libc/str/str.h" #include "libc/sysv/consts/map.h" #include "libc/sysv/consts/o.h" @@ -80,8 +80,9 @@ static bool IsMyDebugBinary(const char *path) { // contains the same number of bytes of code as our .com executable // which is currently running in memory. if ((size = lseek(fd, 0, SEEK_END)) != -1 && - (dm = sys_mmap(0, size, PROT_READ, MAP_SHARED, fd, 0)).addr != - MAP_FAILED) { + (dm = sys_mmap((void *)0x12345000000, size, PROT_READ, MAP_SHARED, fd, + 0)) + .addr != MAP_FAILED) { if (READ32LE((char *)dm.addr) == READ32LE("\177ELF") && ((Elf64_Ehdr *)dm.addr)->e_machine == GetElfMachine() && GetElfSymbolValue(dm.addr, "_etext", &value)) { diff --git a/test/posix/sa_resethand2_test.c b/test/posix/sa_resethand2_test.c new file mode 100644 index 000000000..c66f8cb8d --- /dev/null +++ b/test/posix/sa_resethand2_test.c @@ -0,0 +1,74 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2023 Justine Alexandra Roberts Tunney │ +│ │ +│ 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. │ +│ │ +│ 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. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/calls/calls.h" +#include "libc/calls/struct/sigaction.h" +#include "libc/calls/struct/sigset.h" +#include "libc/dce.h" +#include "libc/sysv/consts/sa.h" +#include "libc/sysv/consts/sig.h" + +volatile int handler_invoked; + +void signal_handler(int signum) { + handler_invoked = 1; +} + +int main() { + sigset_t mask, oldmask; + struct sigaction sa, current_sa; + + if (IsWindows()) { + // TODO(jart): support non-fatal signals between processes + return 0; + } + + sa.sa_handler = signal_handler; + sa.sa_flags = SA_RESETHAND; + sigemptyset(&sa.sa_mask); + + if (sigaction(SIGINT, &sa, 0) == -1) { + return 1; + } + + sigemptyset(&mask); + sigaddset(&mask, SIGINT); + if (sigprocmask(SIG_BLOCK, &mask, &oldmask) == -1) { + return 2; + } + + int pid = fork(); + if (pid == -1) { + return 3; + } else if (pid == 0) { + kill(getppid(), SIGINT); + return 0; + } else { + sigsuspend(&oldmask); + if (!handler_invoked) { + return 4; + } + if (sigaction(SIGINT, 0, ¤t_sa) == -1) { + return 5; + } + if (current_sa.sa_handler != SIG_DFL) { + return 6; + } + return 0; + } +} diff --git a/third_party/readline/signals.c b/third_party/readline/signals.c index 3fcbdda39..3822c26f8 100644 --- a/third_party/readline/signals.c +++ b/third_party/readline/signals.c @@ -59,7 +59,6 @@ typedef struct sigaction sighandler_cxt; # define rl_sigaction(s, nh, oh) sigaction(s, nh, oh) #else typedef struct { SigHandler *sa_handler; int sa_mask, sa_flags; } sighandler_cxt; -# define sigemptyset(m) #endif /* !HAVE_POSIX_SIGNALS */ #ifndef SA_RESTART