Avoid an --ftrace crash on Windows

This commit is contained in:
Justine Tunney 2024-10-07 18:39:25 -07:00
parent dcf9596620
commit ad11fc32ad
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
3 changed files with 24 additions and 11 deletions

View file

@ -40,9 +40,11 @@
#include "libc/nt/enum/fileflagandattributes.h" #include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/filesharemode.h" #include "libc/nt/enum/filesharemode.h"
#include "libc/nt/errors.h" #include "libc/nt/errors.h"
#include "libc/nt/events.h"
#include "libc/nt/files.h" #include "libc/nt/files.h"
#include "libc/nt/process.h" #include "libc/nt/process.h"
#include "libc/nt/runtime.h" #include "libc/nt/runtime.h"
#include "libc/nt/struct/overlapped.h"
#include "libc/nt/thunk/msabi.h" #include "libc/nt/thunk/msabi.h"
#include "libc/runtime/internal.h" #include "libc/runtime/internal.h"
#include "libc/runtime/memtrack.internal.h" #include "libc/runtime/memtrack.internal.h"
@ -113,10 +115,13 @@
} }
// clang-format off // clang-format off
__msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
__msabi extern typeof(CreateEvent) *const __imp_CreateEventW;
__msabi extern typeof(CreateFile) *const __imp_CreateFileW; __msabi extern typeof(CreateFile) *const __imp_CreateFileW;
__msabi extern typeof(DuplicateHandle) *const __imp_DuplicateHandle; __msabi extern typeof(DuplicateHandle) *const __imp_DuplicateHandle;
__msabi extern typeof(GetEnvironmentVariable) *const __imp_GetEnvironmentVariableW; __msabi extern typeof(GetEnvironmentVariable) *const __imp_GetEnvironmentVariableW;
__msabi extern typeof(GetLastError) *const __imp_GetLastError; __msabi extern typeof(GetLastError) *const __imp_GetLastError;
__msabi extern typeof(GetOverlappedResult) *const __imp_GetOverlappedResult;
__msabi extern typeof(GetStdHandle) *const __imp_GetStdHandle; __msabi extern typeof(GetStdHandle) *const __imp_GetStdHandle;
__msabi extern typeof(SetLastError) *const __imp_SetLastError; __msabi extern typeof(SetLastError) *const __imp_SetLastError;
__msabi extern typeof(WriteFile) *const __imp_WriteFile; __msabi extern typeof(WriteFile) *const __imp_WriteFile;
@ -283,7 +288,7 @@ privileged long kloghandle(void) {
hand = __imp_CreateFileW( hand = __imp_CreateFileW(
path, kNtFileAppendData, path, kNtFileAppendData,
kNtFileShareRead | kNtFileShareWrite | kNtFileShareDelete, 0, kNtFileShareRead | kNtFileShareWrite | kNtFileShareDelete, 0,
kNtOpenAlways, kNtFileAttributeNormal, 0); kNtOpenAlways, kNtFileAttributeNormal | kNtFileFlagOverlapped, 0);
} else { } else {
hand = -1; // KPRINTF_LOG was empty string or too long hand = -1; // KPRINTF_LOG was empty string or too long
} }
@ -359,7 +364,6 @@ privileged void _klog_serial(const char *b, size_t n) {
privileged void klog(const char *b, size_t n) { privileged void klog(const char *b, size_t n) {
#ifdef __x86_64__ #ifdef __x86_64__
int e;
long h; long h;
uint32_t wrote; uint32_t wrote;
long rax, rdi, rsi, rdx; long rax, rdi, rsi, rdx;
@ -367,11 +371,20 @@ privileged void klog(const char *b, size_t n) {
return; return;
} }
if (IsWindows()) { if (IsWindows()) {
e = __imp_GetLastError(); bool32 ok;
if (!__imp_WriteFile(h, b, n, &wrote, 0)) { intptr_t ev;
__imp_SetLastError(e); int e = __imp_GetLastError();
__klog_handle = 0; if ((ev = __imp_CreateEventW(0, 0, 0, 0))) {
struct NtOverlapped overlap = {.hEvent = ev};
ok = !!__imp_WriteFile(h, b, n, 0, &overlap);
if (!ok && __imp_GetLastError() == kNtErrorIoPending)
ok = true;
ok &= !!__imp_GetOverlappedResult(h, &overlap, &wrote, true);
if (!ok)
__klog_handle = 0;
__imp_CloseHandle(ev);
} }
__imp_SetLastError(e);
} else if (IsMetal()) { } else if (IsMetal()) {
if (_weaken(_klog_vga)) { if (_weaken(_klog_vga)) {
_weaken(_klog_vga)(b, n); _weaken(_klog_vga)(b, n);

View file

@ -106,8 +106,9 @@ static long AlignStack(long sp, char *stk, long stksz, int mal) {
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// THE NEW TECHNOLOGY // THE NEW TECHNOLOGY
__msabi extern typeof(TlsSetValue) *const __imp_TlsSetValue;
__msabi extern typeof(ExitThread) *const __imp_ExitThread; __msabi extern typeof(ExitThread) *const __imp_ExitThread;
__msabi extern typeof(GetCurrentThreadId) *const __imp_GetCurrentThreadId;
__msabi extern typeof(TlsSetValue) *const __imp_TlsSetValue;
__msabi extern typeof(WakeByAddressAll) *const __imp_WakeByAddressAll; __msabi extern typeof(WakeByAddressAll) *const __imp_WakeByAddressAll;
static textwindows dontinstrument wontreturn void // static textwindows dontinstrument wontreturn void //
@ -118,7 +119,7 @@ WinThreadEntry(int rdi, // rcx
int rc; int rc;
if (wt->tls) if (wt->tls)
__set_tls_win32(wt->tls); __set_tls_win32(wt->tls);
*wt->ctid = GetCurrentThreadId(); *wt->ctid = __imp_GetCurrentThreadId();
rc = __stack_call(wt->arg, wt->tid, 0, 0, wt->func, wt->sp); rc = __stack_call(wt->arg, wt->tid, 0, 0, wt->func, wt->sp);
// we can now clear ctid directly since we're no longer using our own // we can now clear ctid directly since we're no longer using our own
// stack memory, which can now be safely free'd by the parent thread. // stack memory, which can now be safely free'd by the parent thread.

View file

@ -69,9 +69,8 @@ uintptr_t GetStackBottom(void) pureconst;
* will also trigger the stack to grow down safely. * will also trigger the stack to grow down safely.
*/ */
forceinline void CheckLargeStackAllocation(void *p, ssize_t n) { forceinline void CheckLargeStackAllocation(void *p, ssize_t n) {
for (; n > 0; n -= 4096) { for (; n > 0; n -= 4096)
((char *)p)[n - 1] = 0; ((volatile char *)p)[n - 1] = 0;
}
} }
void *NewCosmoStack(void) vallocesque; void *NewCosmoStack(void) vallocesque;