So I was playing around and noticed this:
pub fn isWriteableMemory(address: usize, size_in_bytes: usize) bool {
return w32.IsBadWritePtr(@ptrFromInt(address), size_in_bytes) == 0;
}
test "isWriteableMemory should return true when memory range is writable" {
var memory = [_]u8{ 0, 1, 2, 3, 4 };
const address = @intFromPtr(&memory);
const size_in_bytes = @sizeOf(@TypeOf(memory));
const is_writeable = isWriteableMemory(address, size_in_bytes);
try testing.expectEqual(true, is_writeable); // Passes :)
}
test "isWriteableMemory should return false when memory range is readable but not writeable" {
const memory = [_]u8{ 0, 1, 2, 3, 4 };
const address = @intFromPtr(&memory);
const size_in_bytes = @sizeOf(@TypeOf(memory));
std.debug.print("address = 0x{X}, size_in_bytes = {}", .{ address, size_in_bytes }); // address = 0x4D370B, size_in_bytes = 5
const is_writeable = isWriteableMemory(address, size_in_bytes); // Segmentation fault at address 0x4d370b
// wine: Unhandled exception 0x80000003 in thread 24 at address 0000000000443EFA (thread 0024), starting debugger...
try testing.expectEqual(false, is_writeable);
}
test "isWriteableMemory should return false when memory range is not writable entirely" {
const is_writeable = isWriteableMemory(0, std.math.maxInt(usize));
try testing.expectEqual(false, is_writeable); // Passes :)
}
This test in the middle seg-faults.
Now I'm wondering if ZIG has some memory security feature in the debug build that makes sure I don't cast a const to a non-const pointer.
Or is it Wine that has a bug in it's implementation of IsBadWritePtr WinAPI function.
I bet its Wine that's doing this but asking just in case.