From fc23fe90ce1f3d28841c5a5a93a8bdf7edaefc54 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 31 Aug 2025 20:39:33 -0700 Subject: [PATCH] std.os.windows: avoid dragging in baggage on non-windows particularly noticeable when usize is not 4 or 8 bytes --- lib/std/os/windows.zig | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/std/os/windows.zig b/lib/std/os/windows.zig index 66583d21b2..5f7c900eb7 100644 --- a/lib/std/os/windows.zig +++ b/lib/std/os/windows.zig @@ -4621,25 +4621,28 @@ pub const TEB = extern struct { }; comptime { - // Offsets taken from WinDbg info and Geoff Chappell[1] (RIP) - // [1]: https://www.geoffchappell.com/studies/windows/km/ntoskrnl/inc/api/pebteb/teb/index.htm - assert(@offsetOf(TEB, "NtTib") == 0x00); - if (@sizeOf(usize) == 4) { - assert(@offsetOf(TEB, "EnvironmentPointer") == 0x1C); - assert(@offsetOf(TEB, "ClientId") == 0x20); - assert(@offsetOf(TEB, "ActiveRpcHandle") == 0x28); - assert(@offsetOf(TEB, "ThreadLocalStoragePointer") == 0x2C); - assert(@offsetOf(TEB, "ProcessEnvironmentBlock") == 0x30); - assert(@offsetOf(TEB, "LastErrorValue") == 0x34); - assert(@offsetOf(TEB, "TlsSlots") == 0xe10); - } else if (@sizeOf(usize) == 8) { - assert(@offsetOf(TEB, "EnvironmentPointer") == 0x38); - assert(@offsetOf(TEB, "ClientId") == 0x40); - assert(@offsetOf(TEB, "ActiveRpcHandle") == 0x50); - assert(@offsetOf(TEB, "ThreadLocalStoragePointer") == 0x58); - assert(@offsetOf(TEB, "ProcessEnvironmentBlock") == 0x60); - assert(@offsetOf(TEB, "LastErrorValue") == 0x68); - assert(@offsetOf(TEB, "TlsSlots") == 0x1480); + // This file may be referenced for its error sets, so we must not assume target windows here. + if (builtin.os.tag == .windows) { + // Offsets taken from WinDbg info and Geoff Chappell[1] (RIP) + // [1]: https://www.geoffchappell.com/studies/windows/km/ntoskrnl/inc/api/pebteb/teb/index.htm + assert(@offsetOf(TEB, "NtTib") == 0x00); + if (@sizeOf(usize) == 4) { + assert(@offsetOf(TEB, "EnvironmentPointer") == 0x1C); + assert(@offsetOf(TEB, "ClientId") == 0x20); + assert(@offsetOf(TEB, "ActiveRpcHandle") == 0x28); + assert(@offsetOf(TEB, "ThreadLocalStoragePointer") == 0x2C); + assert(@offsetOf(TEB, "ProcessEnvironmentBlock") == 0x30); + assert(@offsetOf(TEB, "LastErrorValue") == 0x34); + assert(@offsetOf(TEB, "TlsSlots") == 0xe10); + } else if (@sizeOf(usize) == 8) { + assert(@offsetOf(TEB, "EnvironmentPointer") == 0x38); + assert(@offsetOf(TEB, "ClientId") == 0x40); + assert(@offsetOf(TEB, "ActiveRpcHandle") == 0x50); + assert(@offsetOf(TEB, "ThreadLocalStoragePointer") == 0x58); + assert(@offsetOf(TEB, "ProcessEnvironmentBlock") == 0x60); + assert(@offsetOf(TEB, "LastErrorValue") == 0x68); + assert(@offsetOf(TEB, "TlsSlots") == 0x1480); + } } }