authorgravatar for m@pop-os.localdomainm <m@pop-os.localdomain> 2022-02-11 20:11:23+01:00
committergravatar for m@pop-os.localdomainm <m@pop-os.localdomain> 2022-02-11 22:15:46+01:00
log65299c37d1b4b4395616d6f86b5f064000951cf6
treecf763fc14a86fa29d548936555f196a8c99cfaa5
parentbd8d6a8342914974ca163fa75db0562d181f6d27

validate in Windows using VirtualQuery


10 files changed, 112 insertions(+), 18 deletions(-)

lib/std/c/darwin.zig+6
...@@ -636,6 +636,12 @@ pub const MAP = struct {...@@ -636,6 +636,12 @@ pub const MAP = struct {
636 pub const FAILED = @intToPtr(*anyopaque, maxInt(usize));636 pub const FAILED = @intToPtr(*anyopaque, maxInt(usize));
637};637};
638638
639pub const MSF = struct {
640 pub const ASYNC = 1;
641 pub const INVALIDATE = 2;
642 pub const SYNC = 4;
643};
644
639pub const SA = struct {645pub const SA = struct {
640 /// take signal on signal stack646 /// take signal on signal stack
641 pub const ONSTACK = 0x0001;647 pub const ONSTACK = 0x0001;
lib/std/c/dragonfly.zig+6
...@@ -185,6 +185,12 @@ pub const MAP = struct {...@@ -185,6 +185,12 @@ pub const MAP = struct {
185 pub const SIZEALIGN = 262144;185 pub const SIZEALIGN = 262144;
186};186};
187187
188pub const MSF = struct {
189 pub const ASYNC = 1;
190 pub const INVALIDATE = 2;
191 pub const SYNC = 4;
192};
193
188pub const W = struct {194pub const W = struct {
189 pub const NOHANG = 0x0001;195 pub const NOHANG = 0x0001;
190 pub const UNTRACED = 0x0002;196 pub const UNTRACED = 0x0002;
lib/std/c/freebsd.zig+6
...@@ -410,6 +410,12 @@ pub const MAP = struct {...@@ -410,6 +410,12 @@ pub const MAP = struct {
410 pub const @"32BIT" = 0x00080000;410 pub const @"32BIT" = 0x00080000;
411};411};
412412
413pub const MSF = struct {
414 pub const ASYNC = 1;
415 pub const INVALIDATE = 2;
416 pub const SYNC = 4;
417};
418
413pub const W = struct {419pub const W = struct {
414 pub const NOHANG = 1;420 pub const NOHANG = 1;
415 pub const UNTRACED = 2;421 pub const UNTRACED = 2;
lib/std/c/netbsd.zig+6
...@@ -575,6 +575,12 @@ pub const MAP = struct {...@@ -575,6 +575,12 @@ pub const MAP = struct {
575 pub const STACK = 0x2000;575 pub const STACK = 0x2000;
576};576};
577577
578pub const MSF = struct {
579 pub const ASYNC = 1;
580 pub const INVALIDATE = 2;
581 pub const SYNC = 4;
582};
583
578pub const W = struct {584pub const W = struct {
579 pub const NOHANG = 0x00000001;585 pub const NOHANG = 0x00000001;
580 pub const UNTRACED = 0x00000002;586 pub const UNTRACED = 0x00000002;
lib/std/c/openbsd.zig+6
...@@ -363,6 +363,12 @@ pub const MAP = struct {...@@ -363,6 +363,12 @@ pub const MAP = struct {
363 pub const CONCEAL = 0x8000;363 pub const CONCEAL = 0x8000;
364};364};
365365
366pub const MSF = struct {
367 pub const ASYNC = 1;
368 pub const INVALIDATE = 2;
369 pub const SYNC = 4;
370};
371
366pub const W = struct {372pub const W = struct {
367 pub const NOHANG = 1;373 pub const NOHANG = 1;
368 pub const UNTRACED = 2;374 pub const UNTRACED = 2;
lib/std/c/solaris.zig+6
...@@ -534,6 +534,12 @@ pub const MAP = struct {...@@ -534,6 +534,12 @@ pub const MAP = struct {
534 pub const INITDATA = 0x0800;534 pub const INITDATA = 0x0800;
535};535};
536536
537pub const MSF = struct {
538 pub const ASYNC = 1;
539 pub const INVALIDATE = 2;
540 pub const SYNC = 4;
541};
542
537pub const MADV = struct {543pub const MADV = struct {
538 /// no further special treatment544 /// no further special treatment
539 pub const NORMAL = 0;545 pub const NORMAL = 0;
lib/std/debug.zig+40-16
...@@ -424,24 +424,48 @@ pub const StackIterator = struct {...@@ -424,24 +424,48 @@ pub const StackIterator = struct {
424 return address;424 return address;
425 }425 }
426426
427 fn isValidMemory(address: u64) bool {427 fn isValidMemory(address: usize) bool {
428 const aligned_address = address & ~@intCast(usize, (mem.page_size - 1));
429
430 // If the address does not span 2 pages, query only the first one
431 const length: usize = if (aligned_address == address) mem.page_size else 2 * mem.page_size;
432
433 const aligned_memory = @intToPtr([*]align(mem.page_size) u8, aligned_address)[0..length];
434
428 if (native_os != .windows) {435 if (native_os != .windows) {
429 var res = true;436 if (native_os != .wasi) {
430 const length = 2 * mem.page_size;437 os.msync(aligned_memory, os.MSF.ASYNC) catch |err| {
431 const aligned_address = address & ~@intCast(u64, (mem.page_size - 1));438 switch (err) {
432 const aligned_memory = @intToPtr([*]align(mem.page_size) u8, aligned_address)[0..length];439 os.MSyncError.UnmappedMemory => {
433440 return false;
434 os.msync(aligned_memory, os.MSF.ASYNC) catch |err| {441 },
435 switch (err) {442 else => unreachable,
436 os.MSyncError.UnmappedMemory => {443 }
437 res = false;444 };
438 },445 }
439 else => unreachable,446
440 }447 return true;
441 };
442 return res;
443 } else {448 } else {
444 // TODO: Using windows memory API check if a page is mapped449 const w = os.windows;
450 var memory_info: w.MEMORY_BASIC_INFORMATION = undefined;
451 //const memory_info_ptr = @ptrCast(w.PMEMORY_BASIC_INFORMATION, buffer);
452
453 // The only error this function can throw is ERROR_INVALID_PARAMETER.
454 // supply an address that invalid i'll be thrown.
455 const rc = w.VirtualQuery(aligned_memory.ptr, &memory_info, aligned_memory.len) catch {
456 return false;
457 };
458
459 // Result code has to be bigger than zero (number of bytes written)
460 if (rc == 0) {
461 return false;
462 }
463
464 // Free pages cannot be read, they are unmapped
465 if (memory_info.State == w.MEM_FREE) {
466 return false;
467 }
468
445 return true;469 return true;
446 }470 }
447 }471 }
lib/std/os/linux.zig+2-2
...@@ -412,8 +412,8 @@ pub const MSF = struct {...@@ -412,8 +412,8 @@ pub const MSF = struct {
412 pub const SYNC = 4;412 pub const SYNC = 4;
413};413};
414414
415pub fn msync(address: [*]const u8, length: usize, flags: u32) usize {415pub fn msync(address: [*]const u8, length: usize, flags: i32) usize {
416 return syscall3(.msync, @ptrToInt(address), length, flags);416 return syscall3(.msync, @ptrToInt(address), length, @bitCast(u32, flags));
417}417}
418418
419pub fn munmap(address: [*]const u8, length: usize) usize {419pub fn munmap(address: [*]const u8, length: usize) usize {
lib/std/os/windows.zig+32
...@@ -1495,6 +1495,19 @@ pub fn VirtualFree(lpAddress: ?LPVOID, dwSize: usize, dwFreeType: DWORD) void {...@@ -1495,6 +1495,19 @@ pub fn VirtualFree(lpAddress: ?LPVOID, dwSize: usize, dwFreeType: DWORD) void {
1495 assert(kernel32.VirtualFree(lpAddress, dwSize, dwFreeType) != 0);1495 assert(kernel32.VirtualFree(lpAddress, dwSize, dwFreeType) != 0);
1496}1496}
14971497
1498pub const VirtualQuerryError = error{Unexpected};
1499
1500pub fn VirtualQuery(lpAddress: ?LPVOID, lpBuffer: PMEMORY_BASIC_INFORMATION, dwLength: SIZE_T) VirtualQuerryError!SIZE_T {
1501 const rc = kernel32.VirtualQuery(lpAddress, lpBuffer, dwLength);
1502 if (rc == 0) {
1503 switch (kernel32.GetLastError()) {
1504 else => |err| return unexpectedError(err),
1505 }
1506 }
1507
1508 return rc;
1509}
1510
1498pub const SetConsoleTextAttributeError = error{Unexpected};1511pub const SetConsoleTextAttributeError = error{Unexpected};
14991512
1500pub fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) SetConsoleTextAttributeError!void {1513pub fn SetConsoleTextAttribute(hConsoleOutput: HANDLE, wAttributes: WORD) SetConsoleTextAttributeError!void {
...@@ -2586,6 +2599,11 @@ pub const CREATE_EVENT_MANUAL_RESET = 0x00000001;...@@ -2586,6 +2599,11 @@ pub const CREATE_EVENT_MANUAL_RESET = 0x00000001;
2586pub const EVENT_ALL_ACCESS = 0x1F0003;2599pub const EVENT_ALL_ACCESS = 0x1F0003;
2587pub const EVENT_MODIFY_STATE = 0x0002;2600pub const EVENT_MODIFY_STATE = 0x0002;
25882601
2602// MEMORY_BASIC_INFORMATION.Type flags for VirtualQuery
2603pub const MEM_IMAGE = 0x1000000;
2604pub const MEM_MAPPED = 0x40000;
2605pub const MEM_PRIVATE = 0x20000;
2606
2589pub const PROCESS_INFORMATION = extern struct {2607pub const PROCESS_INFORMATION = extern struct {
2590 hProcess: HANDLE,2608 hProcess: HANDLE,
2591 hThread: HANDLE,2609 hThread: HANDLE,
...@@ -2661,6 +2679,7 @@ pub const HEAP_NO_SERIALIZE = 0x00000001;...@@ -2661,6 +2679,7 @@ pub const HEAP_NO_SERIALIZE = 0x00000001;
2661// AllocationType values2679// AllocationType values
2662pub const MEM_COMMIT = 0x1000;2680pub const MEM_COMMIT = 0x1000;
2663pub const MEM_RESERVE = 0x2000;2681pub const MEM_RESERVE = 0x2000;
2682pub const MEM_FREE = 0x10000;
2664pub const MEM_RESET = 0x80000;2683pub const MEM_RESET = 0x80000;
2665pub const MEM_RESET_UNDO = 0x1000000;2684pub const MEM_RESET_UNDO = 0x1000000;
2666pub const MEM_LARGE_PAGES = 0x20000000;2685pub const MEM_LARGE_PAGES = 0x20000000;
...@@ -2960,6 +2979,19 @@ pub const COINIT = enum(c_int) {...@@ -2960,6 +2979,19 @@ pub const COINIT = enum(c_int) {
2960 COINIT_SPEED_OVER_MEMORY = 8,2979 COINIT_SPEED_OVER_MEMORY = 8,
2961};2980};
29622981
2982pub const MEMORY_BASIC_INFORMATION = extern struct {
2983 BaseAddress: PVOID,
2984 AllocationBase: PVOID,
2985 AllocationProtect: DWORD,
2986 PartitionId: WORD,
2987 RegionSize: SIZE_T,
2988 State: DWORD,
2989 Protect: DWORD,
2990 Type: DWORD,
2991};
2992
2993pub const PMEMORY_BASIC_INFORMATION = *MEMORY_BASIC_INFORMATION;
2994
2963/// > The maximum path of 32,767 characters is approximate, because the "\\?\"2995/// > The maximum path of 32,767 characters is approximate, because the "\\?\"
2964/// > prefix may be expanded to a longer string by the system at run time, and2996/// > prefix may be expanded to a longer string by the system at run time, and
2965/// > this expansion applies to the total length.2997/// > this expansion applies to the total length.
lib/std/os/windows/kernel32.zig+2
...@@ -56,6 +56,7 @@ const LPOVERLAPPED_COMPLETION_ROUTINE = windows.LPOVERLAPPED_COMPLETION_ROUTINE;...@@ -56,6 +56,7 @@ const LPOVERLAPPED_COMPLETION_ROUTINE = windows.LPOVERLAPPED_COMPLETION_ROUTINE;
56const UCHAR = windows.UCHAR;56const UCHAR = windows.UCHAR;
57const FARPROC = windows.FARPROC;57const FARPROC = windows.FARPROC;
58const INIT_ONCE_FN = windows.INIT_ONCE_FN;58const INIT_ONCE_FN = windows.INIT_ONCE_FN;
59const PMEMORY_BASIC_INFORMATION = windows.PMEMORY_BASIC_INFORMATION;
5960
60pub extern "kernel32" fn AddVectoredExceptionHandler(First: c_ulong, Handler: ?VECTORED_EXCEPTION_HANDLER) callconv(WINAPI) ?*anyopaque;61pub extern "kernel32" fn AddVectoredExceptionHandler(First: c_ulong, Handler: ?VECTORED_EXCEPTION_HANDLER) callconv(WINAPI) ?*anyopaque;
61pub extern "kernel32" fn RemoveVectoredExceptionHandler(Handle: HANDLE) callconv(WINAPI) c_ulong;62pub extern "kernel32" fn RemoveVectoredExceptionHandler(Handle: HANDLE) callconv(WINAPI) c_ulong;
...@@ -245,6 +246,7 @@ pub extern "kernel32" fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: ?*co...@@ -245,6 +246,7 @@ pub extern "kernel32" fn HeapValidate(hHeap: HANDLE, dwFlags: DWORD, lpMem: ?*co
245246
246pub extern "kernel32" fn VirtualAlloc(lpAddress: ?LPVOID, dwSize: SIZE_T, flAllocationType: DWORD, flProtect: DWORD) callconv(WINAPI) ?LPVOID;247pub extern "kernel32" fn VirtualAlloc(lpAddress: ?LPVOID, dwSize: SIZE_T, flAllocationType: DWORD, flProtect: DWORD) callconv(WINAPI) ?LPVOID;
247pub extern "kernel32" fn VirtualFree(lpAddress: ?LPVOID, dwSize: SIZE_T, dwFreeType: DWORD) callconv(WINAPI) BOOL;248pub extern "kernel32" fn VirtualFree(lpAddress: ?LPVOID, dwSize: SIZE_T, dwFreeType: DWORD) callconv(WINAPI) BOOL;
249pub extern "kernel32" fn VirtualQuery(lpAddress: ?LPVOID, lpBuffer: PMEMORY_BASIC_INFORMATION, dwLength: SIZE_T) callconv(WINAPI) SIZE_T;
248250
249pub extern "kernel32" fn LocalFree(hMem: HLOCAL) callconv(WINAPI) ?HLOCAL;251pub extern "kernel32" fn LocalFree(hMem: HLOCAL) callconv(WINAPI) ?HLOCAL;
250252