|
| 1 | +#!/usr/bin/env bash |
| 2 | +#![doc = r##"<!-- Absolutely cursed hacks: |
| 3 | +SOURCE="$0" NAME=$(basename "$0" .rs) DIR=$(realpath $(dirname "$0")) |
| 4 | +exec "$(dirname "$0")/../ulib/compile.sh" "$0" <<END_MANIFEST |
| 5 | +[package] |
| 6 | +name = "$NAME" |
| 7 | +version = "0.1.0" |
| 8 | +edition = "2021" |
| 9 | +
|
| 10 | +[[bin]] |
| 11 | +name = "$NAME" |
| 12 | +path = "$DIR/$NAME.rs" |
| 13 | +
|
| 14 | +[dependencies] |
| 15 | +ulib = { path = "$DIR/../ulib" } |
| 16 | +
|
| 17 | +[profile.standalone] |
| 18 | +inherits = "release" |
| 19 | +opt-level = 0 |
| 20 | +panic = "abort" |
| 21 | +strip = "debuginfo" |
| 22 | +
|
| 23 | +END_MANIFEST |
| 24 | +exit # -->"##] |
| 25 | +#![no_std] |
| 26 | +#![no_main] |
| 27 | + |
| 28 | +#[macro_use] |
| 29 | +extern crate ulib; |
| 30 | + |
| 31 | +use ulib::sys::{openat, close, mmap, munmap, wait, exit, MAP_PRIVATE, MAP_SHARED, MAP_FILE}; |
| 32 | + |
| 33 | +#[no_mangle] |
| 34 | +fn main() { |
| 35 | + |
| 36 | + let root_fd = 3; |
| 37 | + |
| 38 | + let test_file_path = b"test.txt"; |
| 39 | + let mut test_text_file = openat(root_fd, test_file_path, 0, 0).unwrap(); |
| 40 | + |
| 41 | + static HELLO_CHARS: [u8; 5] = *b"hello"; |
| 42 | + static WORLD_CHARS: [u8; 5] = *b"world"; |
| 43 | + |
| 44 | + let mut mmap_addr: *mut u8 = |
| 45 | + unsafe { mmap(0, 4096, 0, MAP_PRIVATE | MAP_FILE, test_text_file, 0).unwrap() } as *mut u8; |
| 46 | + println!("Memory range is mmaped!"); |
| 47 | + |
| 48 | + for i in 0..5 { |
| 49 | + let curr_char: u8 = unsafe { *(mmap_addr.wrapping_add(i)) }; |
| 50 | + if curr_char != HELLO_CHARS[i] { |
| 51 | + panic!( |
| 52 | + "mmap filed file has an error at index {}! Expected {} got {}", |
| 53 | + i, HELLO_CHARS[i] as char, curr_char as char |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + println!("mmap of test file succeeded!"); |
| 59 | + unsafe { munmap(mmap_addr as *mut ()).unwrap() }; |
| 60 | + _ = close(test_text_file); |
| 61 | + |
| 62 | + println!("starting mmap with offset test 1"); |
| 63 | + let test_file_path2 = b"test2.txt"; |
| 64 | + test_text_file = openat(root_fd, test_file_path2, 0, 0).unwrap(); |
| 65 | + mmap_addr = |
| 66 | + unsafe { mmap(0, 4096 * 2, 0, MAP_PRIVATE | MAP_FILE, test_text_file, 6).unwrap() } as *mut u8; |
| 67 | + println!("mmap addr: {:x}", mmap_addr as usize); |
| 68 | + |
| 69 | + for i in 0..5 { |
| 70 | + let curr_char: u8 = unsafe { *(mmap_addr.wrapping_add(i)) }; |
| 71 | + if curr_char != WORLD_CHARS[i] { |
| 72 | + panic!( |
| 73 | + "mmap filed file has an error at index {}! Expected {} got {}", |
| 74 | + i, WORLD_CHARS[i] as char, curr_char as char |
| 75 | + ); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + println!("done with mmap with offset test 1"); |
| 80 | + |
| 81 | + println!("starting mmap with offset test 2"); |
| 82 | + for i in 0..5 { |
| 83 | + let curr_char: u8 = unsafe { *(mmap_addr.wrapping_add(i + 4096)) }; |
| 84 | + if curr_char != WORLD_CHARS[i] { |
| 85 | + panic!( |
| 86 | + "mmap filed file has an error at index {}! Expected {} got {}", |
| 87 | + i, WORLD_CHARS[i] as char, curr_char as char |
| 88 | + ); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + println!("done with mmap with offset test 2"); |
| 93 | + unsafe { munmap(mmap_addr as *mut ()).unwrap() }; |
| 94 | + _ = close(test_text_file); |
| 95 | + |
| 96 | + println!("Starting shared memory test"); |
| 97 | + |
| 98 | + let shared_mem_fd = unsafe { ulib::sys::sys_memfd_create() as u32 }; |
| 99 | + let shared_frame = |
| 100 | + unsafe { ulib::sys::mmap(0, 4096, 0, MAP_SHARED, shared_mem_fd, 0) }.unwrap() as *mut u8; |
| 101 | + let end_ptr: *mut u8 = shared_frame.wrapping_add(6); |
| 102 | + unsafe { end_ptr.write_volatile('.' as u8) }; |
| 103 | + assert_eq!(unsafe { end_ptr.read_volatile() }, '.' as u8); |
| 104 | + |
| 105 | + println!("Calling fork"); |
| 106 | + let wait_fd = unsafe { ulib::sys::sys_fork() } as u32; |
| 107 | + |
| 108 | + if wait_fd == 0 { |
| 109 | + for _i in 0..1000000 { |
| 110 | + if unsafe { end_ptr.read_volatile() } == ('!' as u8) { |
| 111 | + break; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + let end_val: u8 = unsafe { end_ptr.read_volatile() }; |
| 116 | + if end_val != ('!' as u8) { |
| 117 | + println!( |
| 118 | + "Error: child expected to see '!' with val {} and instead found {}", |
| 119 | + ('!' as u8), |
| 120 | + end_val |
| 121 | + ); |
| 122 | + ulib::sys::exit(5); |
| 123 | + } |
| 124 | + |
| 125 | + for i in 0..5 { |
| 126 | + let curr_char = unsafe { *shared_frame.wrapping_add(i) } as char; |
| 127 | + if curr_char != (HELLO_CHARS[i] as char) { |
| 128 | + println!( |
| 129 | + "Child found wrong char at index {}. Expected {} found {}", |
| 130 | + i, |
| 131 | + (HELLO_CHARS[i] as char), |
| 132 | + curr_char |
| 133 | + ); |
| 134 | + ulib::sys::exit(5); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + println!("Child is done"); |
| 139 | + ulib::sys::exit(0); |
| 140 | + } else { |
| 141 | + unsafe { |
| 142 | + core::ptr::copy_nonoverlapping( |
| 143 | + &raw const HELLO_CHARS[0], |
| 144 | + shared_frame, |
| 145 | + HELLO_CHARS.len(), |
| 146 | + ); |
| 147 | + shared_frame.wrapping_add(6).write('!' as u8); |
| 148 | + } |
| 149 | + |
| 150 | + let child_exit_val = wait(wait_fd).unwrap(); |
| 151 | + assert_eq!(child_exit_val, 0); |
| 152 | + println!("done with shared memory test, parent received exit code 0 from child"); |
| 153 | + } |
| 154 | + |
| 155 | + _ = unsafe { munmap(shared_frame as *mut ()) }; |
| 156 | + |
| 157 | + exit(15); |
| 158 | +} |
0 commit comments