| ... | ... | @@ -3406,3 +3406,27 @@ pub fn linuxINotifyRmWatch(inotify_fd: i32, wd: i32) !void { |
| 3406 | 3406 | else => unreachable, |
| 3407 | 3407 | } |
| 3408 | 3408 | } |
| 3409 | |
| 3410 | pub const MProtectError = error{ |
| 3411 | AccessDenied, |
| 3412 | OutOfMemory, |
| 3413 | Unexpected, |
| 3414 | }; |
| 3415 | |
| 3416 | /// address and length must be page-aligned |
| 3417 | pub fn posixMProtect(address: usize, length: usize, protection: u32) MProtectError!void { |
| 3418 | const negative_page_size = @bitCast(usize, -isize(page_size)); |
| 3419 | const aligned_address = address & negative_page_size; |
| 3420 | const aligned_end = (address + length + page_size - 1) & negative_page_size; |
| 3421 | assert(address == aligned_address); |
| 3422 | assert(length == aligned_end - aligned_address); |
| 3423 | const rc = posix.mprotect(address, length, protection); |
| 3424 | const err = posix.getErrno(rc); |
| 3425 | switch (err) { |
| 3426 | 0 => return, |
| 3427 | posix.EINVAL => unreachable, |
| 3428 | posix.EACCES => return error.AccessDenied, |
| 3429 | posix.ENOMEM => return error.OutOfMemory, |
| 3430 | else => return unexpectedErrorPosix(err), |
| 3431 | } |
| 3432 | } |