authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-01-11 09:43:01+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2026-01-11 09:43:01+01:00
logc4345991340af5ff2e0155a9832f4eed9ec677fc
treef1f8f828fc348857db153db2eb19d8a82d7c767a
parent4e806f25210abd17f7092f40619b248e8b73def5
parente8a6e58f9d02657415ea2bb65364f38c9c6558cc

Merge pull request '`std.process`: add `PermissionDenied` to `ProtectMemoryError` (for OpenBSD)' (#30781) from alexrp/zig:openbsd-mprotect-immutable into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30781 Reviewed-by: Andrew Kelley <andrew@ziglang.org>

1 files changed, 11 insertions(+), 7 deletions(-)

lib/std/process.zig+11-7
......@@ -1004,6 +1004,10 @@ pub fn unlockMemoryAll() UnlockMemoryError!void {
10041004
10051005pub const ProtectMemoryError = error{
10061006 UnsupportedOperation,
1007 /// OpenBSD will refuse to change memory protection if the specified region
1008 /// contains any pages that have previously been marked immutable using the
1009 /// `mimmutable` function.
1010 PermissionDenied,
10071011 /// The memory cannot be given the specified access. This can happen, for
10081012 /// example, if you memory map a file to which you have read-only access,
10091013 /// then use `protectMemory` to mark it writable.
......@@ -1052,6 +1056,7 @@ pub fn protectMemory(
10521056 };
10531057 switch (posix.errno(posix.system.mprotect(memory.ptr, memory.len, flags))) {
10541058 .SUCCESS => return,
1059 .PERM => return error.PermissionDenied,
10551060 .INVAL => |err| return std.Io.Threaded.errnoBug(err),
10561061 .ACCES => return error.AccessDenied,
10571062 .NOMEM => return error.OutOfMemory,
......@@ -1061,10 +1066,11 @@ pub fn protectMemory(
10611066 return error.UnsupportedOperation;
10621067}
10631068
1069var test_page: [std.heap.page_size_max]u8 align(std.heap.page_size_max) = undefined;
1070
10641071test lockMemory {
1065 var page: [std.heap.page_size_min]u8 align(std.heap.page_size_min) = undefined;
1066 lockMemory(&page, .{}) catch return error.SkipZigTest;
1067 unlockMemory(&page) catch return error.SkipZigTest;
1072 lockMemory(&test_page, .{}) catch return error.SkipZigTest;
1073 unlockMemory(&test_page) catch return error.SkipZigTest;
10681074}
10691075
10701076test lockMemoryAll {
......@@ -1073,8 +1079,6 @@ test lockMemoryAll {
10731079}
10741080
10751081test protectMemory {
1076 if (builtin.cpu.arch == .hexagon) return error.SkipZigTest; // TODO
1077 var page: [std.heap.page_size_min]u8 align(std.heap.page_size_min) = undefined;
1078 protectMemory(&page, .{}) catch return error.SkipZigTest;
1079 protectMemory(&page, .{ .read = true, .write = true }) catch return error.SkipZigTest;
1082 protectMemory(&test_page, .{}) catch return error.SkipZigTest;
1083 protectMemory(&test_page, .{ .read = true, .write = true }) catch return error.SkipZigTest;
10801084}