authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-03-06 00:02:45+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-03-06 09:06:26+01:00
logb9a1d67637cfae6206fffce78fc2e40780ef4fb5
tree9b36f734cb9814ab03114ddecb0b4b0965858a6a
parenteaccfffe56655ab11a382685886492e5b9b0865c

std: Nicer way to access the PEB

Use the NtCurrentTeb API instead of using some inline asm, this is much nicer and also more portable. Closes #4645

3 files changed, 15 insertions(+), 16 deletions(-)

lib/std/os/windows.zig+1-16
......@@ -1084,22 +1084,7 @@ pub fn SetFileTime(
10841084}
10851085
10861086pub fn peb() *PEB {
1087 switch (builtin.arch) {
1088 .i386 => {
1089 return asm (
1090 \\ mov %%fs:0x18, %[ptr]
1091 \\ mov %%ds:0x30(%[ptr]), %[ptr]
1092 : [ptr] "=r" (-> *PEB)
1093 );
1094 },
1095 .x86_64 => {
1096 return asm (
1097 \\ mov %%gs:0x60, %[ptr]
1098 : [ptr] "=r" (-> *PEB)
1099 );
1100 },
1101 else => @compileError("unsupported architecture"),
1102 }
1087 return ntdll.NtCurrentTeb().ProcessEnvironmentBlock;
11031088}
11041089
11051090/// A file time is a 64-bit value that represents the number of 100-nanosecond
lib/std/os/windows/bits.zig+13
......@@ -1154,6 +1154,19 @@ const RTL_BITMAP = @OpaqueType();
11541154pub const PRTL_BITMAP = *RTL_BITMAP;
11551155const KAFFINITY = usize;
11561156
1157pub const TEB = extern struct {
1158 Reserved1: [12]PVOID,
1159 ProcessEnvironmentBlock: *PEB,
1160 Reserved2: [399]PVOID,
1161 Reserved3: [1952]u8,
1162 TlsSlots: [64]PVOID,
1163 Reserved4: [8]u8,
1164 Reserved5: [26]PVOID,
1165 ReservedForOle: PVOID,
1166 Reserved6: [4]PVOID,
1167 TlsExpansionSlots: PVOID,
1168};
1169
11571170/// Process Environment Block
11581171/// Microsoft documentation of this is incomplete, the fields here are taken from various resources including:
11591172/// - https://github.com/wine-mirror/wine/blob/1aff1e6a370ee8c0213a0fd4b220d121da8527aa/include/winternl.h#L269
lib/std/os/windows/ntdll.zig+1
......@@ -16,6 +16,7 @@ pub extern "NtDll" fn NtQueryInformationFile(
1616 Length: ULONG,
1717 FileInformationClass: FILE_INFORMATION_CLASS,
1818) callconv(.Stdcall) NTSTATUS;
19pub extern "NtDll" fn NtCurrentTeb() callconv(.Stdcall) *TEB;
1920
2021pub extern "NtDll" fn NtQueryAttributesFile(
2122 ObjectAttributes: *OBJECT_ATTRIBUTES,