MCPcopy Create free account
hub / github.com/bytecodealliance/rustix / test_mmap

Function test_mmap

tests/mm/mmap.rs:4–57  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2#[cfg(not(target_os = "redox"))]
3#[test]
4fn test_mmap() {
5 use rustix::fs::{openat, Mode, OFlags, CWD};
6 use rustix::io::write;
7 use rustix::mm::{mmap, munmap, MapFlags, ProtFlags};
8 use std::ptr::null_mut;
9 use std::slice;
10
11 let tmp = tempfile::tempdir().unwrap();
12 let dir = openat(CWD, tmp.path(), OFlags::RDONLY, Mode::empty()).unwrap();
13
14 let file = openat(
15 &dir,
16 "file",
17 OFlags::CREATE | OFlags::WRONLY | OFlags::TRUNC,
18 Mode::RUSR,
19 )
20 .unwrap();
21 write(&file, &[b'a'; 8192]).unwrap();
22 drop(file);
23
24 let file = openat(&dir, "file", OFlags::RDONLY, Mode::empty()).unwrap();
25 unsafe {
26 let addr = mmap(
27 null_mut(),
28 8192,
29 ProtFlags::READ,
30 MapFlags::PRIVATE,
31 &file,
32 0,
33 )
34 .unwrap();
35 let slice = slice::from_raw_parts(addr.cast::<u8>(), 8192);
36 assert_eq!(slice, &[b'a'; 8192]);
37
38 munmap(addr, 8192).unwrap();
39 }
40
41 let file = openat(&dir, "file", OFlags::RDONLY, Mode::empty()).unwrap();
42 unsafe {
43 assert_eq!(
44 mmap(
45 null_mut(),
46 8192,
47 ProtFlags::READ,
48 MapFlags::PRIVATE,
49 &file,
50 u64::MAX,
51 )
52 .unwrap_err()
53 .raw_os_error(),
54 libc::EINVAL
55 );
56 }
57}
58
59#[test]
60fn test_mmap_anonymous() {

Callers

nothing calls this directly

Calls 5

openatFunction · 0.50
writeFunction · 0.50
mmapFunction · 0.50
munmapFunction · 0.50
pathMethod · 0.45

Tested by

no test coverage detected