diff --git a/Makefile b/Makefile index 00e88e7..a4922cf 100644 --- a/Makefile +++ b/Makefile @@ -122,6 +122,7 @@ initrd: cp target/$(ARCH)-osdev5/$(PROFILE)/cat $(O)/rootfs/bin cp target/$(ARCH)-osdev5/$(PROFILE)/hexd $(O)/rootfs/bin cp target/$(ARCH)-osdev5/$(PROFILE)/mkdir $(O)/rootfs/bin + cp target/$(ARCH)-osdev5/$(PROFILE)/fuzzy $(O)/rootfs/bin cp target/$(ARCH)-osdev5/$(PROFILE)/login $(O)/rootfs/sbin cd $(O)/rootfs && tar cf ../initrd.img `find -type f -printf "%P\n"` ifeq ($(MACH),orangepi3) diff --git a/libusr/src/arch/x86_64.rs b/libusr/src/arch/x86_64.rs index e8596b7..ca3affc 100644 --- a/libusr/src/arch/x86_64.rs +++ b/libusr/src/arch/x86_64.rs @@ -2,42 +2,42 @@ macro_rules! syscall { ($num:expr) => {{ let mut res: usize = $num.repr(); - asm!("syscall", + core::arch::asm!("syscall", inout("rax") res, options(nostack)); res }}; ($num:expr, $a0:expr) => {{ let mut res: usize = $num.repr(); - asm!("syscall", + core::arch::asm!("syscall", inout("rax") res, in("rdi") $a0, options(nostack)); res }}; ($num:expr, $a0:expr, $a1:expr) => {{ let mut res: usize = $num.repr(); - asm!("syscall", + core::arch::asm!("syscall", inout("rax") res, in("rdi") $a0, in("rsi") $a1, options(nostack)); res }}; ($num:expr, $a0:expr, $a1:expr, $a2:expr) => {{ let mut res: usize = $num.repr(); - asm!("syscall", + core::arch::asm!("syscall", inout("rax") res, in("rdi") $a0, in("rsi") $a1, in("rdx") $a2, options(nostack)); res }}; ($num:expr, $a0:expr, $a1:expr, $a2:expr, $a3:expr) => {{ let mut res: usize = $num.repr(); - asm!("syscall", + core::arch::asm!("syscall", inout("rax") res, in("rdi") $a0, in("rsi") $a1, in("rdx") $a2, in("r10") $a3, options(nostack)); res }}; ($num:expr, $a0:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr) => {{ let mut res: usize = $num.repr(); - asm!("syscall", + core::arch::asm!("syscall", inout("rax") res, in("rdi") $a0, in("rsi") $a1, in("rdx") $a2, in("r10") $a3, in("r8") $a4, options(nostack)); res diff --git a/user/Cargo.toml b/user/Cargo.toml index 52ebd67..e4be2ea 100644 --- a/user/Cargo.toml +++ b/user/Cargo.toml @@ -17,9 +17,9 @@ path = "src/bin/mkdir.rs" name = "shell" path = "src/bin/shell.rs" -# [[bin]] -# name = "fuzzy" -# path = "src/bin/fuzzy.rs" +[[bin]] +name = "fuzzy" +path = "src/bin/fuzzy.rs" [[bin]] name = "ls" diff --git a/user/src/bin/fuzzy.rs b/user/src/bin/fuzzy.rs index 9915108..1a54dba 100644 --- a/user/src/bin/fuzzy.rs +++ b/user/src/bin/fuzzy.rs @@ -7,135 +7,90 @@ // #[macro_use] extern crate libusr; -// -// use libusr::sys::{abi::SystemCall, stat::Stat}; -// -// static mut STATE: u64 = 0; -// -// macro_rules! syscall { -// ($num:expr) => {{ -// let mut res: usize; -// asm!("svc #0", out("x0") res, in("x8") $num, options(nostack)); -// res -// }}; -// ($num:expr, $a0:expr) => {{ -// let mut res: usize = $a0; -// asm!("svc #0", -// inout("x0") res, -// in("x8") $num, options(nostack)); -// res -// }}; -// ($num:expr, $a0:expr, $a1:expr) => {{ -// let mut res: usize = $a0; -// asm!("svc #0", -// inout("x0") res, in("x1") $a1, -// in("x8") $num, options(nostack)); -// res -// }}; -// ($num:expr, $a0:expr, $a1:expr, $a2:expr) => {{ -// let mut res: usize = $a0; -// asm!("svc #0", -// inout("x0") res, in("x1") $a1, in("x2") $a2, -// in("x8") $num, options(nostack)); -// res -// }}; -// ($num:expr, $a0:expr, $a1:expr, $a2:expr, $a3:expr) => {{ -// let mut res: usize = $a0; -// asm!("svc #0", -// inout("x0") res, in("x1") $a1, in("x2") $a2, -// in("x3") $a3, in("x8") $num, options(nostack)); -// res -// }}; -// ($num:expr, $a0:expr, $a1:expr, $a2:expr, $a3:expr, $a4:expr) => {{ -// let mut res: usize = $a0; -// asm!("svc #0", -// inout("x0") res, in("x1") $a1, in("x2") $a2, -// in("x3") $a3, in("x4") $a4, in("x8") $num, options(nostack)); -// res -// }}; -// } -// -// /// Integer/size argument -// macro_rules! argn { -// ($a:expr) => { -// $a as usize -// }; -// } -// /// Pointer/base argument -// macro_rules! argp { -// ($a:expr) => { -// $a as usize -// }; -// } -// -// fn random_set_seed(seed: u64) { -// unsafe { STATE = seed; } -// } -// -// fn random_u64() -> u64 { -// let mut x = unsafe { STATE }; -// x ^= x << 13; -// x ^= x >> 7; -// x ^= x << 17; -// unsafe { -// STATE = x; -// } -// x -// } -// -// fn random_ascii_char() -> u8 { -// ((random_u64() % (0x7F - 0x20)) as u8) + 0x20 -// } -// -// fn random_str_range(buf: &mut [u8], min: usize, max: usize) -> &str { -// let max = core::cmp::min(buf.len(), max); -// assert!(max > min); -// let len = ((random_u64() as usize) % (max - min)) + min; -// for c in buf[..len].iter_mut() { -// *c = random_ascii_char(); -// } -// core::str::from_utf8(&buf[..len]).unwrap() -// } -// -// fn random_str(buf: &mut [u8]) -> &str { -// random_str_range(buf, 0, buf.len()) -// } -// -// fn random_bytes(buf: &mut [u8]) { -// for byte in buf.iter_mut() { -// *byte = (random_u64() & 0xFF) as u8; -// } -// } -// + +use libusr::{syscall, sys::{abi::SystemCall, stat::Stat}}; + +static mut STATE: u64 = 0; + +/// Integer/size argument +macro_rules! argn { + ($a:expr) => { + $a as usize + }; +} +/// Pointer/base argument +macro_rules! argp { + ($a:expr) => { + $a as usize + }; +} + +fn random_set_seed(seed: u64) { + unsafe { STATE = seed; } +} + +fn random_u64() -> u64 { + let mut x = unsafe { STATE }; + x ^= x << 13; + x ^= x >> 7; + x ^= x << 17; + unsafe { + STATE = x; + } + x +} + +fn random_ascii_char() -> u8 { + ((random_u64() % (0x7F - 0x20)) as u8) + 0x20 +} + +fn random_str_range(buf: &mut [u8], min: usize, max: usize) -> &str { + let max = core::cmp::min(buf.len(), max); + assert!(max > min); + let len = ((random_u64() as usize) % (max - min)) + min; + for c in buf[..len].iter_mut() { + *c = random_ascii_char(); + } + core::str::from_utf8(&buf[..len]).unwrap() +} + +fn random_str(buf: &mut [u8]) -> &str { + random_str_range(buf, 0, buf.len()) +} + +fn random_bytes(buf: &mut [u8]) { + for byte in buf.iter_mut() { + *byte = (random_u64() & 0xFF) as u8; + } +} + #[no_mangle] fn main() -> i32 { - loop {} + let seed = libusr::sys::sys_ex_getcputime().unwrap().as_nanos() as u64 / 13; + println!("Using seed: {:#x}", seed); + random_set_seed(seed); + + let mut buf = [0; 256]; + + // Test sys_ex_getcputime() + let mut prev_time = libusr::sys::sys_ex_getcputime().unwrap().as_nanos(); + for _ in 0..1000 { + let t = libusr::sys::sys_ex_getcputime().unwrap().as_nanos(); + assert!(t >= prev_time); + prev_time = t; + } + + // Test non-utf8 input fed into syscalls expecting strings + // let old_signal = signal::set_handler(Signal::InvalidSystemCall, SignalHandler::Ignore); + for _ in 0..10000 { + random_bytes(&mut buf); + let mut stat = Stat::default(); + + unsafe { + syscall!(SystemCall::FileStatus, (-2i32) as usize, buf.as_mut_ptr() as usize, buf.len(), (&mut stat) as *mut _ as usize); + } + } + // signal::set_handler(Signal::InvalidSystemCall, old_signal); + + 0 } -// let seed = libusr::sys::sys_ex_getcputime().unwrap().as_nanos() as u64 / 13; -// println!("Using seed: {:#x}", seed); -// random_set_seed(seed); -// -// let mut buf = [0; 256]; -// -// // Test sys_ex_getcputime() -// let mut prev_time = libusr::sys::sys_ex_getcputime().unwrap().as_nanos(); -// for _ in 0..1000 { -// let t = libusr::sys::sys_ex_getcputime().unwrap().as_nanos(); -// assert!(t >= prev_time); -// prev_time = t; -// } -// -// // Test non-utf8 input fed into syscalls expecting strings -// // let old_signal = signal::set_handler(Signal::InvalidSystemCall, SignalHandler::Ignore); -// for _ in 0..10000 { -// random_bytes(&mut buf); -// let mut stat = Stat::default(); -// -// unsafe { -// syscall!(SystemCall::FileStatus.repr(), (-2i32) as usize, buf.as_mut_ptr() as usize, buf.len(), (&mut stat) as *mut _ as usize); -// } -// } -// // signal::set_handler(Signal::InvalidSystemCall, old_signal); -// -// 0 -// }