add mprotect syscall

master
Andrew Kelley 2019-02-28 20:11:36 -05:00
parent 9753e87545
commit 76b4e49178
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 28 additions and 0 deletions

View File

@ -3406,3 +3406,27 @@ pub fn linuxINotifyRmWatch(inotify_fd: i32, wd: i32) !void {
else => unreachable,
}
}
pub const MProtectError = error{
AccessDenied,
OutOfMemory,
Unexpected,
};
/// address and length must be page-aligned
pub fn posixMProtect(address: usize, length: usize, protection: u32) MProtectError!void {
const negative_page_size = @bitCast(usize, -isize(page_size));
const aligned_address = address & negative_page_size;
const aligned_end = (address + length + page_size - 1) & negative_page_size;
assert(address == aligned_address);
assert(length == aligned_end - aligned_address);
const rc = posix.mprotect(address, length, protection);
const err = posix.getErrno(rc);
switch (err) {
0 => return,
posix.EINVAL => unreachable,
posix.EACCES => return error.AccessDenied,
posix.ENOMEM => return error.OutOfMemory,
else => return unexpectedErrorPosix(err),
}
}

View File

@ -806,6 +806,10 @@ pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, of
return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @bitCast(usize, offset));
}
pub fn mprotect(address: usize, length: usize, protection: usize) usize {
return syscall3(SYS_mprotect, address, length, protection);
}
pub fn munmap(address: usize, length: usize) usize {
return syscall2(SYS_munmap, address, length);
}