Skip to content

Commit ad11fc3

Browse files
committed
Avoid an --ftrace crash on Windows
1 parent dcf9596 commit ad11fc3

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

libc/intrin/kprintf.greg.c

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
#include "libc/nt/enum/fileflagandattributes.h"
4141
#include "libc/nt/enum/filesharemode.h"
4242
#include "libc/nt/errors.h"
43+
#include "libc/nt/events.h"
4344
#include "libc/nt/files.h"
4445
#include "libc/nt/process.h"
4546
#include "libc/nt/runtime.h"
47+
#include "libc/nt/struct/overlapped.h"
4648
#include "libc/nt/thunk/msabi.h"
4749
#include "libc/runtime/internal.h"
4850
#include "libc/runtime/memtrack.internal.h"
@@ -113,10 +115,13 @@
113115
}
114116

115117
// clang-format off
118+
__msabi extern typeof(CloseHandle) *const __imp_CloseHandle;
119+
__msabi extern typeof(CreateEvent) *const __imp_CreateEventW;
116120
__msabi extern typeof(CreateFile) *const __imp_CreateFileW;
117121
__msabi extern typeof(DuplicateHandle) *const __imp_DuplicateHandle;
118122
__msabi extern typeof(GetEnvironmentVariable) *const __imp_GetEnvironmentVariableW;
119123
__msabi extern typeof(GetLastError) *const __imp_GetLastError;
124+
__msabi extern typeof(GetOverlappedResult) *const __imp_GetOverlappedResult;
120125
__msabi extern typeof(GetStdHandle) *const __imp_GetStdHandle;
121126
__msabi extern typeof(SetLastError) *const __imp_SetLastError;
122127
__msabi extern typeof(WriteFile) *const __imp_WriteFile;
@@ -283,7 +288,7 @@ privileged long kloghandle(void) {
283288
hand = __imp_CreateFileW(
284289
path, kNtFileAppendData,
285290
kNtFileShareRead | kNtFileShareWrite | kNtFileShareDelete, 0,
286-
kNtOpenAlways, kNtFileAttributeNormal, 0);
291+
kNtOpenAlways, kNtFileAttributeNormal | kNtFileFlagOverlapped, 0);
287292
} else {
288293
hand = -1; // KPRINTF_LOG was empty string or too long
289294
}
@@ -359,19 +364,27 @@ privileged void _klog_serial(const char *b, size_t n) {
359364

360365
privileged void klog(const char *b, size_t n) {
361366
#ifdef __x86_64__
362-
int e;
363367
long h;
364368
uint32_t wrote;
365369
long rax, rdi, rsi, rdx;
366370
if ((h = kloghandle()) == -1) {
367371
return;
368372
}
369373
if (IsWindows()) {
370-
e = __imp_GetLastError();
371-
if (!__imp_WriteFile(h, b, n, &wrote, 0)) {
372-
__imp_SetLastError(e);
373-
__klog_handle = 0;
374+
bool32 ok;
375+
intptr_t ev;
376+
int e = __imp_GetLastError();
377+
if ((ev = __imp_CreateEventW(0, 0, 0, 0))) {
378+
struct NtOverlapped overlap = {.hEvent = ev};
379+
ok = !!__imp_WriteFile(h, b, n, 0, &overlap);
380+
if (!ok && __imp_GetLastError() == kNtErrorIoPending)
381+
ok = true;
382+
ok &= !!__imp_GetOverlappedResult(h, &overlap, &wrote, true);
383+
if (!ok)
384+
__klog_handle = 0;
385+
__imp_CloseHandle(ev);
374386
}
387+
__imp_SetLastError(e);
375388
} else if (IsMetal()) {
376389
if (_weaken(_klog_vga)) {
377390
_weaken(_klog_vga)(b, n);

libc/runtime/clone.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ static long AlignStack(long sp, char *stk, long stksz, int mal) {
106106
////////////////////////////////////////////////////////////////////////////////
107107
// THE NEW TECHNOLOGY
108108

109-
__msabi extern typeof(TlsSetValue) *const __imp_TlsSetValue;
110109
__msabi extern typeof(ExitThread) *const __imp_ExitThread;
110+
__msabi extern typeof(GetCurrentThreadId) *const __imp_GetCurrentThreadId;
111+
__msabi extern typeof(TlsSetValue) *const __imp_TlsSetValue;
111112
__msabi extern typeof(WakeByAddressAll) *const __imp_WakeByAddressAll;
112113

113114
static textwindows dontinstrument wontreturn void //
@@ -118,7 +119,7 @@ WinThreadEntry(int rdi, // rcx
118119
int rc;
119120
if (wt->tls)
120121
__set_tls_win32(wt->tls);
121-
*wt->ctid = GetCurrentThreadId();
122+
*wt->ctid = __imp_GetCurrentThreadId();
122123
rc = __stack_call(wt->arg, wt->tid, 0, 0, wt->func, wt->sp);
123124
// we can now clear ctid directly since we're no longer using our own
124125
// stack memory, which can now be safely free'd by the parent thread.

libc/runtime/stack.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,8 @@ uintptr_t GetStackBottom(void) pureconst;
6969
* will also trigger the stack to grow down safely.
7070
*/
7171
forceinline void CheckLargeStackAllocation(void *p, ssize_t n) {
72-
for (; n > 0; n -= 4096) {
73-
((char *)p)[n - 1] = 0;
74-
}
72+
for (; n > 0; n -= 4096)
73+
((volatile char *)p)[n - 1] = 0;
7574
}
7675

7776
void *NewCosmoStack(void) vallocesque;

0 commit comments

Comments
 (0)