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(...@@ -1084,22 +1084,7 @@ pub fn SetFileTime(
1084}1084}
10851085
1086pub fn peb() *PEB {1086pub fn peb() *PEB {
1087 switch (builtin.arch) {1087 return ntdll.NtCurrentTeb().ProcessEnvironmentBlock;
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 }
1103}1088}
11041089
1105/// A file time is a 64-bit value that represents the number of 100-nanosecond1090/// 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();...@@ -1154,6 +1154,19 @@ const RTL_BITMAP = @OpaqueType();
1154pub const PRTL_BITMAP = *RTL_BITMAP;1154pub const PRTL_BITMAP = *RTL_BITMAP;
1155const KAFFINITY = usize;1155const 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
1157/// Process Environment Block1170/// Process Environment Block
1158/// Microsoft documentation of this is incomplete, the fields here are taken from various resources including:1171/// Microsoft documentation of this is incomplete, the fields here are taken from various resources including:
1159/// - https://github.com/wine-mirror/wine/blob/1aff1e6a370ee8c0213a0fd4b220d121da8527aa/include/winternl.h#L2691172/// - 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(...@@ -16,6 +16,7 @@ pub extern "NtDll" fn NtQueryInformationFile(
16 Length: ULONG,16 Length: ULONG,
17 FileInformationClass: FILE_INFORMATION_CLASS,17 FileInformationClass: FILE_INFORMATION_CLASS,
18) callconv(.Stdcall) NTSTATUS;18) callconv(.Stdcall) NTSTATUS;
19pub extern "NtDll" fn NtCurrentTeb() callconv(.Stdcall) *TEB;
1920
20pub extern "NtDll" fn NtQueryAttributesFile(21pub extern "NtDll" fn NtQueryAttributesFile(
21 ObjectAttributes: *OBJECT_ATTRIBUTES,22 ObjectAttributes: *OBJECT_ATTRIBUTES,