Avoid truncating mmap2 offsets if not multiple of page size

This commit is contained in:
LemonBoy 2019-09-27 18:18:38 +02:00
parent 805f9b309b
commit 5aaa7d0fbb

View File

@ -193,9 +193,29 @@ pub fn umount2(special: [*]const u8, flags: u32) usize {
pub fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: u32, fd: i32, offset: u64) usize {
if (@hasDecl(@This(), "SYS_mmap2")) {
return syscall6(SYS_mmap2, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), @truncate(usize, offset / MMAP2_UNIT));
// Make sure the offset is also specified in multiples of page size
if ((offset & (MMAP2_UNIT - 1)) != 0)
return @bitCast(usize, isize(-EINVAL));
return syscall6(
SYS_mmap2,
@ptrToInt(address),
length,
prot,
flags,
@bitCast(usize, isize(fd)),
@truncate(usize, offset / MMAP2_UNIT),
);
} else {
return syscall6(SYS_mmap, @ptrToInt(address), length, prot, flags, @bitCast(usize, isize(fd)), offset);
return syscall6(
SYS_mmap,
@ptrToInt(address),
length,
prot,
flags,
@bitCast(usize, isize(fd)),
offset,
);
}
}