Skip to content

Commit

Permalink
feat: updated lib mmap with match
Browse files Browse the repository at this point in the history
  • Loading branch information
lind committed Oct 22, 2024
1 parent 70e269a commit 91efc0d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
4 changes: 0 additions & 4 deletions src/safeposix/syscalls/fs_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -773,10 +773,6 @@ impl Cage {
virtual_fd: i32,
off: i64,
) -> i32 {
// returns an EINVAL error if both flags are set simultaneously
if (flags & libc::MAP_PRIVATE != 0) && (flags & libc::MAP_SHARED != 0) {
return syscall_error(Errno::EINVAL, "mmap", "Invalid flags: both MAP_PRIVATE and MAP_SHARED set");
}
if virtual_fd != -1 {
match fdtables::translate_virtual_fd(self.cageid, virtual_fd as u64) {
Ok(kernel_fd) => {
Expand Down
31 changes: 21 additions & 10 deletions src/tests/fs_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,21 +458,32 @@ pub mod fs_tests {
//Writing into that file's first 9 bytes.
assert_eq!(cage.write_syscall(fd, str2cbuf("Test text"), 9), 9);

//Checking if passing both `MAP_PRIVATE`
//and `MAP_SHARED` flags correctly results in `The value
//of flags is invalid (`MAP_PRIVATE` and `MAP_SHARED`
//cannot be both set)` error.
assert!(
cage.mmap_syscall(
0 as *mut u8,
// Trying to mmap with both `MAP_PRIVATE` and `MAP_SHARED` flags.
let mmap_result = unsafe {
libc::mmap(
0 as *mut c_void,
5,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_SHARED,
fd,
0
) < 0
);

)
};

// Check the result of mmap and get the error if it failed.
assert_eq!(mmap_result, libc::MAP_FAILED, "mmap did not fail as expected");
if mmap_result == libc::MAP_FAILED {
let errno_val = get_errno();
match errno_val {
libc::EINVAL => assert_eq!(errno_val, libc::EINVAL, "EINVAL error for invalid mmap flags"),
libc::ENOENT => assert_eq!(errno_val, libc::ENOENT, "No such file or directory"),
libc::EISDIR => assert_eq!(errno_val, libc::EISDIR, "Is a directory"),
libc::ENODEV => assert_eq!(errno_val, libc::ENODEV, "No such device"),
_ => panic!("Unexpected error code: {}", errno_val),
}
}
// Clean up and finalize
assert_eq!(cage.unlink_syscall(filepath), 0);
assert_eq!(cage.exit_syscall(libc::EXIT_SUCCESS), libc::EXIT_SUCCESS);
lindrustfinalize();
}
Expand Down

0 comments on commit 91efc0d

Please sign in to comment.