| author | |
| committer | |
| log | 76b4e49178b72fb9b01e97aa6b46f9d5bdb83ab2 |
| tree | fde3ca02ffc06eaf192a1c183332848e287669c9 |
| parent | 9753e875453ceeb6f3683b434a7492e20b639d36 |
| signature |
2 files changed, 28 insertions(+), 0 deletions(-)
std/os/index.zig+24| ... | @@ -3406,3 +3406,27 @@ pub fn linuxINotifyRmWatch(inotify_fd: i32, wd: i32) !void { | ... | @@ -3406,3 +3406,27 @@ pub fn linuxINotifyRmWatch(inotify_fd: i32, wd: i32) !void { |
| 3406 | else => unreachable, | 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 | } |
std/os/linux/index.zig+4| ... | @@ -806,6 +806,10 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, of | ... | @@ -806,6 +806,10 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, of |
| 806 | return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); | 806 | return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset)); |
| 807 | } | 807 | } |
| 808 | 808 | ||
| 809 | pub fn mprotect(address: usize, length: usize, protection: usize) usize { | ||
| 810 | return syscall3(SYS_mprotect, address, length, protection); | ||
| 811 | } | ||
| 812 | |||
| 809 | pub fn munmap(address: usize, length: usize) usize { | 813 | pub fn munmap(address: usize, length: usize) usize { |
| 810 | return syscall2(SYS_munmap, address, length); | 814 | return syscall2(SYS_munmap, address, length); |
| 811 | } | 815 | } |