From c8aa33e0e27e13ef5300abf400b3607d43e84af6 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sun, 30 Jul 2023 14:26:21 -0700 Subject: [PATCH] Improve wait statuses This change has the insight that dwExitCode isn't an exit code but rather should be used to pass the wait status. This lets us report killing as a termination status, similar to UNIX. This change also fixes the fact that exit(259) on Windows will break the parent due way WIN32 is designed. We now work around that. It turns out that NetBSD and OpenBSD, will let you have exit codes beyond 255. This change will let you use them when it's possible. --- libc/calls/__sig2.c | 6 ++- libc/calls/calls.h | 22 ++++++----- libc/calls/execve-nt.greg.c | 2 +- libc/calls/kill-nt.c | 6 +-- libc/calls/raise.c | 4 -- libc/calls/wait4-nt.c | 7 +++- libc/calls/wifcontinued.c | 36 ++++++++++++++++++ libc/calls/wifsignaled.c | 35 ++++++++++++++++++ libc/calls/wifstopped.c | 34 +++++++++++++++++ libc/calls/wincrash.c | 4 +- libc/calls/write-nt.c | 2 +- libc/intrin/exit.c | 24 +++++++++++- libc/intrin/kprintf.greg.c | 3 ++ libc/intrin/terminateprocess.c | 6 +-- libc/runtime/fork-nt.c | 6 +-- libc/runtime/winmain.greg.c | 2 +- libc/sock/kntwsadata.c | 2 +- libc/str/strverscmp.c | 67 ++++++++++++++++------------------ libc/str/wcscmp.c | 2 +- libc/str/wmemcmp.c | 4 +- test/libc/runtime/exit_test.c | 54 +++++++++++++++++++++++++++ third_party/python/python.mk | 6 +-- 22 files changed, 259 insertions(+), 75 deletions(-) create mode 100644 libc/calls/wifcontinued.c create mode 100644 libc/calls/wifsignaled.c create mode 100644 libc/calls/wifstopped.c diff --git a/libc/calls/__sig2.c b/libc/calls/__sig2.c index b0af84096..a82910732 100644 --- a/libc/calls/__sig2.c +++ b/libc/calls/__sig2.c @@ -27,6 +27,7 @@ #include "libc/calls/struct/sigset.h" #include "libc/intrin/strace.internal.h" #include "libc/macros.internal.h" +#include "libc/nt/runtime.h" #include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/str/str.h" @@ -172,13 +173,14 @@ static textwindows bool __sig_is_fatal(int sig) { * Handles signal. * @return true if signal was delivered */ -bool __sig_handle(int sigops, int sig, int si_code, ucontext_t *ctx) { +textwindows bool __sig_handle(int sigops, int sig, int si_code, + ucontext_t *ctx) { bool delivered; switch (__sighandrvas[sig]) { case (intptr_t)SIG_DFL: if (__sig_is_fatal(sig)) { STRACE("terminating on %G", sig); - _Exitr(128 + sig); + ExitProcess(sig); } // fallthrough case (intptr_t)SIG_IGN: diff --git a/libc/calls/calls.h b/libc/calls/calls.h index 6483f340d..8b60471a0 100644 --- a/libc/calls/calls.h +++ b/libc/calls/calls.h @@ -44,15 +44,15 @@ #define MAP_FAILED ((void *)-1) -#define WCOREDUMP(s) (128 & (s)) -#define WEXITSTATUS(s) ((0xff00 & (s)) >> 8) -#define WIFCONTINUED(s) ((s) == 0xffff) -#define WIFEXITED(s) (!WTERMSIG(s)) -#define WIFSIGNALED(s) (((signed char)((127 & (s)) + 1) >> 1) > 0) -#define WIFSTOPPED(s) ((255 & (s)) == 127) -#define WSTOPSIG(s) WEXITSTATUS(s) -#define WTERMSIG(s) (127 & (s)) -#define W_STOPCODE(s) ((s) << 8 | 0177) +#define WTERMSIG(x) (127 & (x)) +#define WCOREDUMP(x) (128 & (x)) +#define WIFEXITED(x) (!WTERMSIG(x)) +#define WEXITSTATUS(x) ((x) >> 8) +#define WSTOPSIG(x) ((0xff00 & (x)) >> 8) +#define WIFSTOPPED(x) __wifstopped(x) +#define WIFSIGNALED(x) __wifsignaled(x) +#define WIFCONTINUED(x) __wifcontinued(x) +#define W_STOPCODE(x) ((x) << 8 | 0177) #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ @@ -234,6 +234,10 @@ ssize_t readansi(int, char *, size_t); ssize_t tinyprint(int, const char *, ...) nullterminated(); #endif +int __wifstopped(int) pureconst; +int __wifcontinued(int) pureconst; +int __wifsignaled(int) pureconst; + COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ #endif /* COSMOPOLITAN_LIBC_CALLS_SYSCALLS_H_ */ diff --git a/libc/calls/execve-nt.greg.c b/libc/calls/execve-nt.greg.c index 90632b925..c66631765 100644 --- a/libc/calls/execve-nt.greg.c +++ b/libc/calls/execve-nt.greg.c @@ -119,7 +119,7 @@ textwindows int sys_execve_nt(const char *program, char *const argv[], rc = ntspawn(program, argv, envp, v, 0, 0, true, 0, 0, &startinfo, &procinfo); if (rc == -1) { STRACE("panic: unrecoverable ntspawn(%#s) error: %m", program); - __imp_ExitProcess(6543); + __imp_ExitProcess(11); } ////////////////////////////////////////////////////////////////////////////// diff --git a/libc/calls/kill-nt.c b/libc/calls/kill-nt.c index b60ef1522..afbc4c2cc 100644 --- a/libc/calls/kill-nt.c +++ b/libc/calls/kill-nt.c @@ -95,14 +95,14 @@ textwindows int sys_kill_nt(int pid, int sig) { do { if (pe.th32ParentProcessID == ntpid) { if ((h = OpenProcess(kNtProcessTerminate, false, pe.th32ProcessID))) { - TerminateProcess(h, 128 + sig); + TerminateProcess(h, sig); CloseHandle(h); } } } while (Process32Next(hSnap, &pe)); } CloseHandle(hSnap); - ok = TerminateProcess(g_fds.p[pid].handle, 128 + sig); + ok = TerminateProcess(g_fds.p[pid].handle, sig); if (!ok && GetLastError() == kNtErrorAccessDenied) ok = true; return 0; } @@ -110,7 +110,7 @@ textwindows int sys_kill_nt(int pid, int sig) { // XXX: Is this a raw new technology pid? Because that's messy. if ((h = OpenProcess(kNtProcessTerminate, false, pid))) { if (sig) { - ok = TerminateProcess(h, 128 + sig); + ok = TerminateProcess(h, sig); if (!ok && GetLastError() == kNtErrorAccessDenied) { ok = true; // cargo culting other codebases here } diff --git a/libc/calls/raise.c b/libc/calls/raise.c index d0777c3f2..f9e38fef3 100644 --- a/libc/calls/raise.c +++ b/libc/calls/raise.c @@ -50,10 +50,6 @@ static dontubsan void RaiseSigFpe(void) { * helps us support Windows. So if the raised signal has a signal * handler, then the reported `si_code` might not be `SI_TKILL`. * - * On Windows, if a signal results in the termination of the process - * then we use the convention `_Exit(128 + sig)` to notify the parent of - * the signal number. - * * @param sig can be SIGALRM, SIGINT, SIGTERM, SIGKILL, etc. * @return 0 if signal was delivered and returned, or -1 w/ errno * @asyncsignalsafe diff --git a/libc/calls/wait4-nt.c b/libc/calls/wait4-nt.c index 9f8c557e2..6e66cfef2 100644 --- a/libc/calls/wait4-nt.c +++ b/libc/calls/wait4-nt.c @@ -134,8 +134,11 @@ static textwindows int sys_wait4_nt_impl(int *pid, int *opt_out_wstatus, if (dwExitCode == kNtStillActive) { return -2; } - if (opt_out_wstatus) { // @see WEXITSTATUS() - *opt_out_wstatus = (dwExitCode & 0xff) << 8; + if (dwExitCode == 0xc9af3d51u) { + dwExitCode = kNtStillActive; + } + if (opt_out_wstatus) { + *opt_out_wstatus = dwExitCode; } if (opt_out_rusage) { bzero(opt_out_rusage, sizeof(*opt_out_rusage)); diff --git a/libc/calls/wifcontinued.c b/libc/calls/wifcontinued.c new file mode 100644 index 000000000..6ad0124c4 --- /dev/null +++ b/libc/calls/wifcontinued.c @@ -0,0 +1,36 @@ +/*-*- 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 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/dce.h" + +int __wifcontinued(int x) { + if (IsLinux() || IsMetal() || IsWindows()) { + return x == 0xffff; + } else if (IsXnu()) { + return (x & 0177) == 0177 && WSTOPSIG(x) == 0x13; // SIGCONT + } else if (IsFreebsd()) { + return x == 0x13; // SIGCONT + } else if (IsOpenbsd()) { + return (x & 0177777) == 0177777; + } else if (IsNetbsd()) { + return x == 0177777; + } else { + __builtin_unreachable(); + } +} diff --git a/libc/calls/wifsignaled.c b/libc/calls/wifsignaled.c new file mode 100644 index 000000000..1c1aacd75 --- /dev/null +++ b/libc/calls/wifsignaled.c @@ -0,0 +1,35 @@ +/*-*- 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 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/dce.h" + +int __wifsignaled(int x) { + if (IsLinux() || IsMetal() || IsWindows()) { + return (x & 0xffff) - 1U < 0xffu; + } else if (IsXnu() || IsOpenbsd()) { + return (x & 0177) != 0177 && (x & 0177) != 0; + } else if (IsFreebsd()) { + return (x & 0177) != 0177 && (x & 0177) != 0 && x != 0x13; // SIGCONT + } else if (IsNetbsd()) { + return !((x & 0177) == 0177 && !(x == 0177777)) && !(x == 0177777) && + !!(127 & x); + } else { + __builtin_unreachable(); + } +} diff --git a/libc/calls/wifstopped.c b/libc/calls/wifstopped.c new file mode 100644 index 000000000..ea8cb8e79 --- /dev/null +++ b/libc/calls/wifstopped.c @@ -0,0 +1,34 @@ +/*-*- 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 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/dce.h" + +int __wifstopped(int x) { + if (IsLinux() || IsMetal() || IsWindows()) { + return (short)(((x & 0xffff) * 0x10001U) >> 8) > 0x7f00; + } else if (IsXnu()) { + return (x & 0177) == 0177 && WSTOPSIG(x) != 0x13; // SIGCONT + } else if (IsOpenbsd() || IsFreebsd()) { + return (x & 0xff) == 0177; + } else if (IsNetbsd()) { + return (x & 0177) == 0177 && !(x == 0177777); + } else { + __builtin_unreachable(); + } +} diff --git a/libc/calls/wincrash.c b/libc/calls/wincrash.c index 49f9edb7b..16b604560 100644 --- a/libc/calls/wincrash.c +++ b/libc/calls/wincrash.c @@ -51,13 +51,13 @@ unsigned __wincrash(struct NtExceptionPointers *ep) { if (~tib->tib_flags & TIB_FLAG_WINCRASHING) { tib->tib_flags |= TIB_FLAG_WINCRASHING; } else { - ExitProcess(89); + ExitProcess(SIGSEGV); } } else { if (!noreentry) { noreentry = true; } else { - ExitProcess(89); + ExitProcess(SIGSEGV); } } diff --git a/libc/calls/write-nt.c b/libc/calls/write-nt.c index 7986db3f3..c5f4c8ad2 100644 --- a/libc/calls/write-nt.c +++ b/libc/calls/write-nt.c @@ -78,7 +78,7 @@ static textwindows ssize_t sys_write_nt_impl(int fd, void *data, size_t size, return epipe(); } else { STRACE("broken pipe"); - _Exitr(128 + EPIPE); + ExitProcess(EPIPE); } case kNtErrorAccessDenied: // write doesn't return EACCESS return ebadf(); diff --git a/libc/intrin/exit.c b/libc/intrin/exit.c index ddada85db..2d5e780e9 100644 --- a/libc/intrin/exit.c +++ b/libc/intrin/exit.c @@ -20,6 +20,7 @@ #include "libc/intrin/promises.internal.h" #include "libc/intrin/strace.internal.h" #include "libc/nexgen32e/vendor.internal.h" +#include "libc/nt/enum/status.h" #include "libc/nt/runtime.h" #include "libc/runtime/runtime.h" #include "libc/sysv/consts/nr.h" @@ -30,7 +31,10 @@ * When running on bare metal, this function will reboot your computer * by hosing the interrupt descriptors and triple faulting the system. * - * @param exitcode is masked with 255 on unix (but not windows) + * Exit codes are narrowed to an unsigned char on most platforms. The + * exceptions would be Windows, NetBSD, and OpenBSD, which should let + * you have larger exit codes. + * * @asyncsignalsafe * @threadsafe * @vforksafe @@ -79,7 +83,23 @@ wontreturn void _Exit(int exitcode) { : "x8", "memory"); #endif } else if (IsWindows()) { - ExitProcess(exitcode); + uint32_t waitstatus; + waitstatus = exitcode; + waitstatus <<= 8; + if (waitstatus == kNtStillActive) { + // The GetExitCodeProcess function returns a valid error code + // defined by the application only after the thread terminates. + // Therefore, an application should not use STILL_ACTIVE (259) as + // an error code (STILL_ACTIVE is a macro for STATUS_PENDING + // (minwinbase.h)). If a thread returns STILL_ACTIVE (259) as an + // error code, then applications that test for that value could + // interpret it to mean that the thread is still running, and + // continue to test for the completion of the thread after the + // thread has terminated, which could put the application into an + // infinite loop. -Quoth MSDN (see also libc/calls/wait4-nt.c) + waitstatus = 0xc9af3d51u; + } + ExitProcess(waitstatus); } #ifdef __x86_64__ asm("push\t$0\n\t" diff --git a/libc/intrin/kprintf.greg.c b/libc/intrin/kprintf.greg.c index d7e71356e..852cd360d 100644 --- a/libc/intrin/kprintf.greg.c +++ b/libc/intrin/kprintf.greg.c @@ -389,6 +389,9 @@ privileged static size_t kformat(char *b, size_t n, const char *fmt, if (!(tib && (tib->tib_flags & TIB_FLAG_VFORKED))) { if (tib) { x = atomic_load_explicit(&tib->tib_tid, memory_order_relaxed); + if (IsNetbsd() && x == 1) { + x = __pid; + } } else { x = __pid; } diff --git a/libc/intrin/terminateprocess.c b/libc/intrin/terminateprocess.c index 88da9bf3e..7fce68c8e 100644 --- a/libc/intrin/terminateprocess.c +++ b/libc/intrin/terminateprocess.c @@ -28,10 +28,10 @@ __msabi extern typeof(TerminateProcess) *const __imp_TerminateProcess; * Terminates the specified process and all of its threads. * @note this wrapper takes care of ABI, STRACE(), and __winerr() */ -textwindows bool32 TerminateProcess(int64_t hProcess, uint32_t uExitCode) { +textwindows bool32 TerminateProcess(int64_t hProcess, uint32_t uWaitStatus) { bool32 ok; - ok = __imp_TerminateProcess(hProcess, uExitCode); + ok = __imp_TerminateProcess(hProcess, uWaitStatus); if (!ok) __winerr(); - NTTRACE("TerminateProcess(%ld, %u) → %hhhd% m", hProcess, uExitCode, ok); + NTTRACE("TerminateProcess(%ld, %u) → %hhhd% m", hProcess, uWaitStatus, ok); return ok; } diff --git a/libc/runtime/fork-nt.c b/libc/runtime/fork-nt.c index 4e3f2b7ab..5d2c1cab2 100644 --- a/libc/runtime/fork-nt.c +++ b/libc/runtime/fork-nt.c @@ -74,7 +74,7 @@ static textwindows wontreturn void AbortFork(const char *func) { #ifdef SYSDEBUG kprintf("fork() %s() failed with win32 error %d\n", func, GetLastError()); #endif - ExitProcess(177); + ExitProcess(11); } static textwindows char16_t *ParseInt(char16_t *p, int64_t *x) { @@ -166,7 +166,7 @@ static textwindows int OnForkCrash(struct NtExceptionPointers *ep) { "\tRip = %x%n", ep->ExceptionRecord->ExceptionCode, ep->ContextRecord ? ep->ContextRecord->Rip : -1); - ExitProcess(73); + ExitProcess(11); } textwindows void WinMainForked(void) { @@ -370,7 +370,7 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) { untrackpid = -1; rc = pid; } else { - TerminateProcess(procinfo.hProcess, 127); + TerminateProcess(procinfo.hProcess, 9); CloseHandle(procinfo.hProcess); } } diff --git a/libc/runtime/winmain.greg.c b/libc/runtime/winmain.greg.c index 4247236e3..973bae99d 100644 --- a/libc/runtime/winmain.greg.c +++ b/libc/runtime/winmain.greg.c @@ -122,7 +122,7 @@ __msabi static textwindows int OnEarlyWinCrash(struct NtExceptionPointers *ep) { *p++ = '\r'; *p++ = '\n'; WriteFile(GetStdHandle(kNtStdErrorHandle), buf, p - buf, &wrote, 0); - ExitProcess(200); + ExitProcess(11); } __msabi static textwindows void DeduplicateStdioHandles(void) { diff --git a/libc/sock/kntwsadata.c b/libc/sock/kntwsadata.c index aca43955d..b9983a519 100644 --- a/libc/sock/kntwsadata.c +++ b/libc/sock/kntwsadata.c @@ -49,6 +49,6 @@ textwindows dontasan void WinSockInit(void) { NTTRACE("WSAStartup()"); if ((rc = WSAStartup(VERSION, &kNtWsaData)) != 0 || kNtWsaData.wVersion != VERSION) { - ExitProcess(123); + ExitProcess(1 << 8); } } diff --git a/libc/str/strverscmp.c b/libc/str/strverscmp.c index 153eeca83..684a925c5 100644 --- a/libc/str/strverscmp.c +++ b/libc/str/strverscmp.c @@ -1,5 +1,5 @@ -/*-*- 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│ +/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│ +│vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi│ ╚──────────────────────────────────────────────────────────────────────────────╝ │ │ │ Musl Libc │ @@ -31,41 +31,38 @@ asm(".ident\t\"\\n\\n\ Musl libc (MIT License)\\n\ Copyright 2005-2014 Rich Felker, et. al.\""); asm(".include \"libc/disclaimer.inc\""); +// clang-format off /** * Compares two version strings. */ -int strverscmp(const char *l0, const char *r0) { - const unsigned char *l = (const void *)l0; - const unsigned char *r = (const void *)r0; - size_t i, dp, j; - int z = 1; - /* Find maximal matching prefix and track its maximal digit - * suffix and whether those digits are all zeros. */ - for (dp = i = 0; l[i] == r[i]; i++) { - int c = l[i]; - if (!c) return 0; - if (!isdigit(c)) { - dp = i + 1, z = 1; - } else if (c != '0') { - z = 0; - } - } - if (l[dp] != '0' && r[dp] != '0') { - /* If we're not looking at a digit sequence that began - * with a zero, longest digit string is greater. */ - for (j = i; isdigit(l[j]); j++) { - if (!isdigit(r[j])) { - return 1; - } - } - if (isdigit(r[j])) { - return -1; - } - } else if (z && dp < i && (isdigit(l[i]) || isdigit(r[i]))) { - /* Otherwise, if common prefix of digit sequence is - * all zeros, digits order less than non-digits. */ - return (unsigned char)(l[i] - '0') - (unsigned char)(r[i] - '0'); - } - return l[i] - r[i]; +int strverscmp(const char *l0, const char *r0) +{ + const unsigned char *l = (const void *)l0; + const unsigned char *r = (const void *)r0; + size_t i, dp, j; + int z = 1; + + /* Find maximal matching prefix and track its maximal digit + * suffix and whether those digits are all zeros. */ + for (dp=i=0; l[i]==r[i]; i++) { + int c = l[i]; + if (!c) return 0; + if (!isdigit(c)) dp=i+1, z=1; + else if (c!='0') z=0; + } + + if (l[dp]-'1'<9U && r[dp]-'1'<9U) { + /* If we're looking at non-degenerate digit sequences starting + * with nonzero digits, longest digit string is greater. */ + for (j=i; isdigit(l[j]); j++) + if (!isdigit(r[j])) return 1; + if (isdigit(r[j])) return -1; + } else if (z && dp (int)b[i]; + return a[i] < b[i] ? -1 : a[i] > b[i]; } __weak_reference(wcscmp, wcscoll); diff --git a/libc/str/wmemcmp.c b/libc/str/wmemcmp.c index 1c993548b..9d51d1a03 100644 --- a/libc/str/wmemcmp.c +++ b/libc/str/wmemcmp.c @@ -31,8 +31,8 @@ int wmemcmp(const wchar_t *a, const wchar_t *b, size_t n) { size_t i; for (i = 0; i < n; ++i) { if (a[i] != b[i]) { - return a[i] - b[i]; + break; } } - return 0; + return i < n ? (a[i] > b[i]) - (a[i] < b[i]) : 0; } diff --git a/test/libc/runtime/exit_test.c b/test/libc/runtime/exit_test.c index 085783a91..2cfe1ec16 100644 --- a/test/libc/runtime/exit_test.c +++ b/test/libc/runtime/exit_test.c @@ -16,7 +16,12 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/atomic.h" +#include "libc/calls/calls.h" +#include "libc/calls/struct/sigaction.h" +#include "libc/dce.h" #include "libc/runtime/runtime.h" +#include "libc/sysv/consts/sig.h" #include "libc/testlib/subprocess.h" #include "libc/testlib/testlib.h" @@ -60,3 +65,52 @@ TEST(exit, test) { ASSERT_EQ(3, p[2]); ASSERT_EQ(3, p[3]); } + +TEST(exit, narrowing) { + SPAWN(vfork); + _Exit(31337); + EXITS(IsWindows() || IsNetbsd() || IsOpenbsd() ? 31337 : 31337 & 255); +} + +TEST(exit, exitCode259_wontCauseParentProcessToHangForever) { + if (!IsWindows()) return; + SPAWN(vfork); + _Exit(259); + EXITS(259); +} + +TEST(exit, sigkill) { + int ws, pid; + atomic_int *ready = _mapshared(4); + ASSERT_NE(-1, (pid = fork())); + if (!pid) { + for (*ready = 1;;) { + pause(); + } + } + while (!*ready) donothing; + ASSERT_EQ(0, kill(pid, SIGKILL)); + ASSERT_SYS(0, pid, wait(&ws)); + ASSERT_EQ(SIGKILL, ws); + munmap(ready, 4); +} + +// NetBSD is the only operating system that ignores SIGPWR by default + +TEST(exit, sigalrm) { + int ws, pid; + sighandler_t oldint = signal(SIGALRM, SIG_DFL); + atomic_int *ready = _mapshared(4); + ASSERT_NE(-1, (pid = fork())); + if (!pid) { + for (*ready = 1;;) { + pause(); + } + } + while (!*ready) donothing; + ASSERT_EQ(0, kill(pid, SIGALRM)); + ASSERT_SYS(0, pid, wait(&ws)); + ASSERT_EQ(SIGALRM, ws); + munmap(ready, 4); + signal(SIGALRM, oldint); +} diff --git a/third_party/python/python.mk b/third_party/python/python.mk index ffc9497f3..1c77ad899 100644 --- a/third_party/python/python.mk +++ b/third_party/python/python.mk @@ -1847,7 +1847,6 @@ THIRD_PARTY_PYTHON_PYTEST_PYMAINS = \ third_party/python/Lib/test/test_exception_variations.py \ third_party/python/Lib/test/test_exceptions.py \ third_party/python/Lib/test/test_extcall.py \ - third_party/python/Lib/test/test_faulthandler.py \ third_party/python/Lib/test/test_fcntl.py \ third_party/python/Lib/test/test_file.py \ third_party/python/Lib/test/test_file_eintr.py \ @@ -2900,8 +2899,9 @@ o/$(MODE)/third_party/python/Lib/test/test_docxmlrpc.py.runs: $(PYTHONTESTER) o/$(MODE)/third_party/python/Lib/test/test_extcall.py.runs: $(PYTHONTESTER) @$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_extcall $(PYTESTARGS) -o/$(MODE)/third_party/python/Lib/test/test_faulthandler.py.runs: $(PYTHONTESTER) - @$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_faulthandler $(PYTESTARGS) +# too slow +#o/$(MODE)/third_party/python/Lib/test/test_faulthandler.py.runs: $(PYTHONTESTER) +# @$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_faulthandler $(PYTESTARGS) o/$(MODE)/third_party/python/Lib/test/test_fcntl.py.runs: $(PYTHONTESTER) @$(COMPILE) -ACHECK -wtT$@ $(PYHARNESSARGS) $(PYTHONTESTER) -m test.test_fcntl $(PYTESTARGS)