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 {...@@ -1004,6 +1004,10 @@ pub fn unlockMemoryAll() UnlockMemoryError!void {
10041004
1005pub const ProtectMemoryError = error{1005pub const ProtectMemoryError = error{
1006 UnsupportedOperation,1006 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,
1007 /// The memory cannot be given the specified access. This can happen, for1011 /// The memory cannot be given the specified access. This can happen, for
1008 /// example, if you memory map a file to which you have read-only access,1012 /// example, if you memory map a file to which you have read-only access,
1009 /// then use `protectMemory` to mark it writable.1013 /// then use `protectMemory` to mark it writable.
...@@ -1052,6 +1056,7 @@ pub fn protectMemory(...@@ -1052,6 +1056,7 @@ pub fn protectMemory(
1052 };1056 };
1053 switch (posix.errno(posix.system.mprotect(memory.ptr, memory.len, flags))) {1057 switch (posix.errno(posix.system.mprotect(memory.ptr, memory.len, flags))) {
1054 .SUCCESS => return,1058 .SUCCESS => return,
1059 .PERM => return error.PermissionDenied,
1055 .INVAL => |err| return std.Io.Threaded.errnoBug(err),1060 .INVAL => |err| return std.Io.Threaded.errnoBug(err),
1056 .ACCES => return error.AccessDenied,1061 .ACCES => return error.AccessDenied,
1057 .NOMEM => return error.OutOfMemory,1062 .NOMEM => return error.OutOfMemory,
...@@ -1061,10 +1066,11 @@ pub fn protectMemory(...@@ -1061,10 +1066,11 @@ pub fn protectMemory(
1061 return error.UnsupportedOperation;1066 return error.UnsupportedOperation;
1062}1067}
10631068
1069var test_page: [std.heap.page_size_max]u8 align(std.heap.page_size_max) = undefined;
1070
1064test lockMemory {1071test lockMemory {
1065 var page: [std.heap.page_size_min]u8 align(std.heap.page_size_min) = undefined;1072 lockMemory(&test_page, .{}) catch return error.SkipZigTest;
1066 lockMemory(&page, .{}) catch return error.SkipZigTest;1073 unlockMemory(&test_page) catch return error.SkipZigTest;
1067 unlockMemory(&page) catch return error.SkipZigTest;
1068}1074}
10691075
1070test lockMemoryAll {1076test lockMemoryAll {
...@@ -1073,8 +1079,6 @@ test lockMemoryAll {...@@ -1073,8 +1079,6 @@ test lockMemoryAll {
1073}1079}
10741080
1075test protectMemory {1081test protectMemory {
1076 if (builtin.cpu.arch == .hexagon) return error.SkipZigTest; // TODO1082 protectMemory(&test_page, .{}) catch return error.SkipZigTest;
1077 var page: [std.heap.page_size_min]u8 align(std.heap.page_size_min) = undefined;1083 protectMemory(&test_page, .{ .read = true, .write = true }) catch return error.SkipZigTest;
1078 protectMemory(&page, .{}) catch return error.SkipZigTest;
1079 protectMemory(&page, .{ .read = true, .write = true }) catch return error.SkipZigTest;
1080}1084}