From 42ba9901e47be5b6e7cb145dcaf47ec151b64587 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Sun, 9 Jul 2023 09:51:59 -0700 Subject: [PATCH] Fix some behavioral issues on Windows --- libc/calls/calls.mk | 3 ++- libc/nexgen32e/nexgen32e.mk | 1 + libc/nexgen32e/nt2sysv.S | 2 -- libc/nexgen32e/runlevel.c | 20 ++++++++++++++++++++ libc/runtime/clone.c | 3 +++ libc/runtime/inflate.c | 6 +++++- libc/runtime/internal.h | 3 +++ libc/runtime/runtime.mk | 6 ++++++ test/libc/calls/fexecve_test.c | 5 ++--- third_party/dlmalloc/dlmalloc.c | 1 + third_party/dlmalloc/init.inc | 1 + 11 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 libc/nexgen32e/runlevel.c diff --git a/libc/calls/calls.mk b/libc/calls/calls.mk index 655261f8c..d640c0be9 100644 --- a/libc/calls/calls.mk +++ b/libc/calls/calls.mk @@ -100,7 +100,8 @@ o/$(MODE)/libc/calls/mkntenvblock.o: private \ o/$(MODE)/libc/calls/wincrash.o \ o/$(MODE)/libc/calls/ntcontext2linux.o: private \ COPTS += \ - -fno-sanitize=all + -fno-sanitize=all \ + -fpatchable-function-entry=0,0 ifneq ($(ARCH), aarch64) # we always want -O3 because: diff --git a/libc/nexgen32e/nexgen32e.mk b/libc/nexgen32e/nexgen32e.mk index b37bfb6f0..0187923cc 100644 --- a/libc/nexgen32e/nexgen32e.mk +++ b/libc/nexgen32e/nexgen32e.mk @@ -50,6 +50,7 @@ o/$(MODE)/libc/nexgen32e/kbase36.o \ o/$(MODE)/libc/nexgen32e/ktens.o \ o/$(MODE)/libc/nexgen32e/ktolower.o \ o/$(MODE)/libc/nexgen32e/ktoupper.o \ +o/$(MODE)/libc/nexgen32e/runlevel.o \ o/$(MODE)/libc/nexgen32e/pid.o \ o/$(MODE)/libc/nexgen32e/program_invocation_name2.o \ o/$(MODE)/libc/nexgen32e/threaded.o: private \ diff --git a/libc/nexgen32e/nt2sysv.S b/libc/nexgen32e/nt2sysv.S index dd0931d35..0530d086c 100644 --- a/libc/nexgen32e/nt2sysv.S +++ b/libc/nexgen32e/nt2sysv.S @@ -29,9 +29,7 @@ // @return %rax,%xmm0 // @note slower than __sysv2nt // @see NT2SYSV() macro - .ftrace1 __nt2sysv: - .ftrace2 push %rbp mov %rsp,%rbp // TODO(jart): We should probably find some way to use our own diff --git a/libc/nexgen32e/runlevel.c b/libc/nexgen32e/runlevel.c new file mode 100644 index 000000000..f9081ceeb --- /dev/null +++ b/libc/nexgen32e/runlevel.c @@ -0,0 +1,20 @@ +/*-*- 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. │ +╚─────────────────────────────────────────────────────────────────────────────*/ + +char __runlevel; diff --git a/libc/runtime/clone.c b/libc/runtime/clone.c index f5305f192..05663adc4 100644 --- a/libc/runtime/clone.c +++ b/libc/runtime/clone.c @@ -22,15 +22,18 @@ #include "libc/calls/calls.h" #include "libc/calls/struct/ucontext-netbsd.internal.h" #include "libc/calls/syscall-sysv.internal.h" +#include "libc/calls/wincrash.internal.h" #include "libc/dce.h" #include "libc/errno.h" #include "libc/intrin/asan.internal.h" #include "libc/intrin/atomic.h" #include "libc/intrin/describeflags.internal.h" #include "libc/intrin/strace.internal.h" +#include "libc/intrin/weaken.h" #include "libc/limits.h" #include "libc/macros.internal.h" #include "libc/nt/runtime.h" +#include "libc/nt/signals.h" #include "libc/nt/synchronization.h" #include "libc/nt/thread.h" #include "libc/nt/thunk/msabi.h" diff --git a/libc/runtime/inflate.c b/libc/runtime/inflate.c index 5f33a0f2d..7f887fcee 100644 --- a/libc/runtime/inflate.c +++ b/libc/runtime/inflate.c @@ -19,6 +19,7 @@ #include "libc/intrin/strace.internal.h" #include "libc/intrin/weaken.h" #include "libc/macros.internal.h" +#include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "third_party/puff/puff.h" #include "third_party/zlib/zlib.h" @@ -35,7 +36,10 @@ int __inflate(void *out, size_t outsize, const void *in, size_t insize) { int rc; z_stream zs; - if (_weaken(inflateInit2) && _weaken(inflate) && _weaken(inflateEnd)) { + if (_weaken(inflateInit2) && // + _weaken(inflate) && // + _weaken(inflateEnd) && // + __runlevel >= RUNLEVEL_MALLOC) { zs.next_in = in; zs.avail_in = insize; zs.total_in = insize; diff --git a/libc/runtime/internal.h b/libc/runtime/internal.h index 0690f0c73..afcb789b9 100644 --- a/libc/runtime/internal.h +++ b/libc/runtime/internal.h @@ -8,10 +8,13 @@ #define STACK_CEIL 0x700000000000ul #define STACK_SIZE FRAMESIZE +#define RUNLEVEL_MALLOC 1 + #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ extern int __pid; +extern char __runlevel; extern int ftrace_stackdigs; extern uint32_t __ntconsolemode[3]; extern const char v_ntsubsystem[] __attribute__((__weak__)); diff --git a/libc/runtime/runtime.mk b/libc/runtime/runtime.mk index eab72da13..1de311028 100644 --- a/libc/runtime/runtime.mk +++ b/libc/runtime/runtime.mk @@ -130,6 +130,12 @@ o/$(MODE)/libc/runtime/morph_tls.o: private \ -ffreestanding \ -fno-sanitize=all +# TODO(jart): We need a way to avoid WinThreadEntry() being hooked. +o/$(MODE)/libc/runtime/clone.o: private \ + COPTS += \ + -fno-sanitize=all \ + -fpatchable-function-entry=0,0 + # these assembly files are safe to build on aarch64 o/$(MODE)/libc/runtime/init.o: libc/runtime/init.S @$(COMPILE) -AOBJECTIFY.S $(OBJECTIFY.S) $(OUTPUT_OPTION) -c $< diff --git a/test/libc/calls/fexecve_test.c b/test/libc/calls/fexecve_test.c index 83a95b7a9..065c38fad 100644 --- a/test/libc/calls/fexecve_test.c +++ b/test/libc/calls/fexecve_test.c @@ -16,6 +16,7 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#if 0 // TODO(G4Vi): improve reliability of fexecve() implementation #include "libc/calls/calls.h" #include "libc/calls/syscall_support-sysv.internal.h" #include "libc/dce.h" @@ -28,8 +29,6 @@ #include "libc/testlib/testlib.h" // clang-format off -#ifdef __x86_64__ - STATIC_YOINK("zipos"); int fds[2]; @@ -151,4 +150,4 @@ TEST(fexecve, ziposAPEHasZipos) { close(fd); } -#endif /* __x86_64__ */ +#endif diff --git a/third_party/dlmalloc/dlmalloc.c b/third_party/dlmalloc/dlmalloc.c index 4a22347f3..c0ec8ec55 100644 --- a/third_party/dlmalloc/dlmalloc.c +++ b/third_party/dlmalloc/dlmalloc.c @@ -9,6 +9,7 @@ #include "libc/macros.internal.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/rdtsc.h" +#include "libc/runtime/internal.h" #include "libc/runtime/runtime.h" #include "libc/runtime/sysconf.h" #include "libc/stdckdint.h" diff --git a/third_party/dlmalloc/init.inc b/third_party/dlmalloc/init.inc index 810412d13..6c2c26dd6 100644 --- a/third_party/dlmalloc/init.inc +++ b/third_party/dlmalloc/init.inc @@ -96,6 +96,7 @@ __attribute__((__constructor__)) int init_mparams(void) { // RELEASE_MALLOC_GLOBAL_LOCK(); + __runlevel = RUNLEVEL_MALLOC; return 1; }