authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-28 20:11:36-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-02-28 20:11:36-05:00
log76b4e49178b72fb9b01e97aa6b46f9d5bdb83ab2
treefde3ca02ffc06eaf192a1c183332848e287669c9
parent9753e875453ceeb6f3683b434a7492e20b639d36
signaturelock-open Commit is signed but in an unrecognized format.

add mprotect syscall


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 {
34063406 else => unreachable,
34073407 }
34083408}
3409
3410pub const MProtectError = error{
3411 AccessDenied,
3412 OutOfMemory,
3413 Unexpected,
3414};
3415
3416/// address and length must be page-aligned
3417pub 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
806806 return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset));
807807}
808808
809pub fn mprotect(address: usize, length: usize, protection: usize) usize {
810 return syscall3(SYS_mprotect, address, length, protection);
811}
812
809813pub fn munmap(address: usize, length: usize) usize {
810814 return syscall2(SYS_munmap, address, length);
811815}