mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-05-23 05:42:29 +00:00
Fix close(1) bug on Windows
This commit is contained in:
parent
83341a4269
commit
b0e3258709
6 changed files with 51 additions and 7 deletions
3
Makefile
3
Makefile
|
@ -88,7 +88,8 @@ o/$(MODE): \
|
||||||
|
|
||||||
ifneq ($(LANDLOCKMAKE_VERSION),)
|
ifneq ($(LANDLOCKMAKE_VERSION),)
|
||||||
ifeq ($(wildcard /usr/bin/ape),)
|
ifeq ($(wildcard /usr/bin/ape),)
|
||||||
$(error please run ape/apeinstall.sh if you intend to use landlock make)
|
$(warning please run ape/apeinstall.sh if you intend to use landlock make)
|
||||||
|
$(shell sleep .5)
|
||||||
endif
|
endif
|
||||||
ifeq ($(USE_SYSTEM_TOOLCHAIN),)
|
ifeq ($(USE_SYSTEM_TOOLCHAIN),)
|
||||||
.STRICT = 1
|
.STRICT = 1
|
||||||
|
|
|
@ -247,7 +247,7 @@ o/$(MODE)/%-clang.asm: %.c
|
||||||
@$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.c) -S -g0 $(OUTPUT_OPTION) $<
|
@$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.c) -S -g0 $(OUTPUT_OPTION) $<
|
||||||
|
|
||||||
# TODO(jart): Make intrinsics support Clang.
|
# TODO(jart): Make intrinsics support Clang.
|
||||||
# o/$(MODE)/%-clang.asm: CC = $(CLANG)
|
o/$(MODE)/%-clang.asm: CC = $(CLANG)
|
||||||
o/$(MODE)/%-clang.asm: private .UNSANDBOXED = 1
|
o/$(MODE)/%-clang.asm: private .UNSANDBOXED = 1
|
||||||
o/$(MODE)/%-clang.asm: %.cc
|
o/$(MODE)/%-clang.asm: %.cc
|
||||||
@$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.cxx) -S -g0 $(OUTPUT_OPTION) $<
|
@$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.cxx) -S -g0 $(OUTPUT_OPTION) $<
|
||||||
|
|
|
@ -44,20 +44,20 @@ unsigned __wincrash(struct NtExceptionPointers *ep) {
|
||||||
static bool noreentry;
|
static bool noreentry;
|
||||||
noreentry = true;
|
noreentry = true;
|
||||||
|
|
||||||
|
STRACE("wincrash rip %x bt %s", ep->ContextRecord->Rip,
|
||||||
|
DescribeBacktrace((struct StackFrame *)ep->ContextRecord->Rbp));
|
||||||
|
|
||||||
if ((tib = __tls_enabled ? __get_tls_privileged() : 0)) {
|
if ((tib = __tls_enabled ? __get_tls_privileged() : 0)) {
|
||||||
if (~tib->tib_flags & TIB_FLAG_WINCRASHING) {
|
if (~tib->tib_flags & TIB_FLAG_WINCRASHING) {
|
||||||
tib->tib_flags |= TIB_FLAG_WINCRASHING;
|
tib->tib_flags |= TIB_FLAG_WINCRASHING;
|
||||||
} else {
|
} else {
|
||||||
WincrashPanic:
|
|
||||||
STRACE("panic: wincrash reentry: rip %x bt %s", ep->ContextRecord->Rip,
|
|
||||||
DescribeBacktrace((struct StackFrame *)ep->ContextRecord->Rbp));
|
|
||||||
ExitProcess(89);
|
ExitProcess(89);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!noreentry) {
|
if (!noreentry) {
|
||||||
noreentry = true;
|
noreentry = true;
|
||||||
} else {
|
} else {
|
||||||
goto WincrashPanic;
|
ExitProcess(89);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -104,7 +104,7 @@ static dontasan void Print(const char *prologue) {
|
||||||
*
|
*
|
||||||
* @param prologue needs to be a .rodata kprintf string
|
* @param prologue needs to be a .rodata kprintf string
|
||||||
*/
|
*/
|
||||||
textstartup void __printargs(const char *prologue) {
|
dontasan textstartup void __printargs(const char *prologue) {
|
||||||
|
|
||||||
const struct AuxiliaryValue {
|
const struct AuxiliaryValue {
|
||||||
const char *fmt;
|
const char *fmt;
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#include "libc/calls/syscall_support-nt.internal.h"
|
#include "libc/calls/syscall_support-nt.internal.h"
|
||||||
#include "libc/dce.h"
|
#include "libc/dce.h"
|
||||||
#include "libc/intrin/describeflags.internal.h"
|
#include "libc/intrin/describeflags.internal.h"
|
||||||
|
#include "libc/intrin/kprintf.h"
|
||||||
#include "libc/intrin/strace.internal.h"
|
#include "libc/intrin/strace.internal.h"
|
||||||
#include "libc/intrin/weaken.h"
|
#include "libc/intrin/weaken.h"
|
||||||
#include "libc/log/libfatal.internal.h"
|
#include "libc/log/libfatal.internal.h"
|
||||||
|
@ -29,6 +30,7 @@
|
||||||
#include "libc/nt/enum/consolemodeflags.h"
|
#include "libc/nt/enum/consolemodeflags.h"
|
||||||
#include "libc/nt/enum/filemapflags.h"
|
#include "libc/nt/enum/filemapflags.h"
|
||||||
#include "libc/nt/enum/pageflags.h"
|
#include "libc/nt/enum/pageflags.h"
|
||||||
|
#include "libc/nt/files.h"
|
||||||
#include "libc/nt/memory.h"
|
#include "libc/nt/memory.h"
|
||||||
#include "libc/nt/pedef.internal.h"
|
#include "libc/nt/pedef.internal.h"
|
||||||
#include "libc/nt/process.h"
|
#include "libc/nt/process.h"
|
||||||
|
@ -123,6 +125,18 @@ __msabi static textwindows int OnEarlyWinCrash(struct NtExceptionPointers *ep) {
|
||||||
ExitProcess(200);
|
ExitProcess(200);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__msabi static textwindows void DeduplicateStdioHandles(void) {
|
||||||
|
int64_t proc, outhand, errhand, newhand;
|
||||||
|
outhand = GetStdHandle(kNtStdOutputHandle);
|
||||||
|
errhand = GetStdHandle(kNtStdErrorHandle);
|
||||||
|
if (outhand == errhand) {
|
||||||
|
proc = GetCurrentProcess();
|
||||||
|
DuplicateHandle(proc, errhand, proc, &newhand, 0, true,
|
||||||
|
kNtDuplicateSameAccess);
|
||||||
|
SetStdHandle(kNtStdErrorHandle, newhand);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
__msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) {
|
__msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) {
|
||||||
bool32 rc;
|
bool32 rc;
|
||||||
int64_t h, hand;
|
int64_t h, hand;
|
||||||
|
@ -136,6 +150,7 @@ __msabi static textwindows wontreturn void WinMainNew(const char16_t *cmdline) {
|
||||||
intptr_t stackaddr, allocaddr;
|
intptr_t stackaddr, allocaddr;
|
||||||
version = NtGetPeb()->OSMajorVersion;
|
version = NtGetPeb()->OSMajorVersion;
|
||||||
__oldstack = (intptr_t)__builtin_frame_address(0);
|
__oldstack = (intptr_t)__builtin_frame_address(0);
|
||||||
|
DeduplicateStdioHandles();
|
||||||
if ((intptr_t)v_ntsubsystem == kNtImageSubsystemWindowsCui && version >= 10) {
|
if ((intptr_t)v_ntsubsystem == kNtImageSubsystemWindowsCui && version >= 10) {
|
||||||
rc = SetConsoleCP(kNtCpUtf8);
|
rc = SetConsoleCP(kNtCpUtf8);
|
||||||
NTTRACE("SetConsoleCP(kNtCpUtf8) → %hhhd", rc);
|
NTTRACE("SetConsoleCP(kNtCpUtf8) → %hhhd", rc);
|
||||||
|
|
28
test/libc/calls/close_test.c
Normal file
28
test/libc/calls/close_test.c
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
/*-*- 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"
|
||||||
|
#include "libc/stdio/stdio.h"
|
||||||
|
#include "libc/testlib/testlib.h"
|
||||||
|
|
||||||
|
TEST(close, stdout) {
|
||||||
|
if (!IsWindows()) return;
|
||||||
|
close(1);
|
||||||
|
fprintf(stderr, "hi\n");
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue