From 8de432b726c8441b9516df3c156f42cdc39fa1bf Mon Sep 17 00:00:00 2001 From: Mark Poliakov Date: Fri, 5 Nov 2021 13:50:00 +0200 Subject: [PATCH] refactor: add clippy to init/libusr --- Makefile | 3 +++ libusr/src/io.rs | 5 ++++- libusr/src/mem.rs | 38 ++++++++++++++++++++++++++++++++++++++ syscall/src/calls.rs | 32 +++++++++++++++++++++++++++++++- 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3309c5b..b124ef1 100644 --- a/Makefile +++ b/Makefile @@ -121,6 +121,9 @@ doc-open: clippy: cd kernel && cargo clippy $(CARGO_BUILD_OPTS) + cd init && cargo clippy \ + --target=../etc/$(ARCH)-osdev5.json \ + -Zbuild-std=core,alloc,compiler_builtins $(CARGO_COMMON_OPTS) qemu: all $(QEMU_PREFIX)qemu-system-$(ARCH) $(QEMU_OPTS) diff --git a/libusr/src/io.rs b/libusr/src/io.rs index 58bbe14..ce90b74 100644 --- a/libusr/src/io.rs +++ b/libusr/src/io.rs @@ -2,7 +2,10 @@ use crate::sys::{self, Stat}; use syscall::stat::AT_FDCWD; use core::fmt; -pub fn stat(pathname: &str) -> Result { +// TODO populate this type +pub struct Error; + +pub fn stat(pathname: &str) -> Result { let mut buf = Stat::default(); let res = unsafe { sys::sys_fstatat(AT_FDCWD, pathname, &mut buf, 0) diff --git a/libusr/src/mem.rs b/libusr/src/mem.rs index 6d667be..1b9f19a 100644 --- a/libusr/src/mem.rs +++ b/libusr/src/mem.rs @@ -1,3 +1,9 @@ +/// See memcpy(3p). +/// +/// # Safety +/// +/// Unsafe: writes to arbitrary memory locations, performs no pointer +/// validation. #[no_mangle] pub unsafe extern "C" fn memcpy(dst: *mut u8, src: *mut u8, mut len: usize) -> *mut u8 { while len != 0 { @@ -7,6 +13,12 @@ pub unsafe extern "C" fn memcpy(dst: *mut u8, src: *mut u8, mut len: usize) -> * dst } +/// See memcmp(3p). +/// +/// # Safety +/// +/// Unsafe: performs reads from arbitrary memory locations, performs no +/// pointer validation. #[no_mangle] pub unsafe extern "C" fn memcmp(a: *mut u8, b: *mut u8, mut len: usize) -> isize { while len != 0 { @@ -21,6 +33,32 @@ pub unsafe extern "C" fn memcmp(a: *mut u8, b: *mut u8, mut len: usize) -> isize 0 } +/// See memmove(3p) +/// +/// # Safety +/// +/// Unsafe: writes to arbitrary memory locations, performs no pointer +/// validation. +#[no_mangle] +pub unsafe extern "C" fn memmove(dst: *mut u8, src: *mut u8, len: usize) -> *mut u8 { + if dst < src { + for i in 0..len { + *dst.add(i) = *src.add(i); + } + } else { + for i in 0..len { + *dst.add(len - (i + 1)) = *src.add(len - (i + 1)); + } + } + dst +} + +/// See memset(3p) +/// +/// # Safety +/// +/// Unsafe: writes to arbitrary memory locations, performs no pointer +/// validation. #[no_mangle] pub unsafe extern "C" fn memset(buf: *mut u8, val: u8, mut len: usize) -> *mut u8 { while len != 0 { diff --git a/syscall/src/calls.rs b/syscall/src/calls.rs index bd30e0f..7494c3e 100644 --- a/syscall/src/calls.rs +++ b/syscall/src/calls.rs @@ -63,22 +63,34 @@ macro_rules! argp { // ($a:expr) => ($a as *const core::ffi::c_void as usize) // } +/// # Safety +/// +/// System call #[inline(always)] pub unsafe fn sys_exit(status: i32) -> ! { syscall!(abi::SYS_EXIT, argn!(status)); - loop {} + unreachable!(); } +/// # Safety +/// +/// System call #[inline(always)] pub unsafe fn sys_close(fd: i32) -> i32 { syscall!(abi::SYS_CLOSE, argn!(fd)) as i32 } +/// # Safety +/// +/// System call #[inline(always)] pub unsafe fn sys_ex_nanosleep(ns: u64, rem: &mut [u64; 2]) -> i32 { syscall!(abi::SYS_EX_NANOSLEEP, argn!(ns), argp!(rem.as_mut_ptr())) as i32 } +/// # Safety +/// +/// System call #[inline(always)] pub unsafe fn sys_ex_debug_trace(msg: &[u8]) -> usize { syscall!( @@ -88,6 +100,9 @@ pub unsafe fn sys_ex_debug_trace(msg: &[u8]) -> usize { ) } +/// # Safety +/// +/// System call #[inline(always)] pub unsafe fn sys_openat(at: i32, pathname: &str, mode: FileMode, flags: OpenFlags) -> i32 { syscall!( @@ -100,6 +115,9 @@ pub unsafe fn sys_openat(at: i32, pathname: &str, mode: FileMode, flags: OpenFla ) as i32 } +/// # Safety +/// +/// System call #[inline(always)] pub unsafe fn sys_read(fd: i32, data: &mut [u8]) -> isize { syscall!( @@ -110,6 +128,9 @@ pub unsafe fn sys_read(fd: i32, data: &mut [u8]) -> isize { ) as isize } +/// # Safety +/// +/// System call #[inline(always)] pub unsafe fn sys_write(fd: i32, data: &[u8]) -> isize { syscall!( @@ -120,6 +141,9 @@ pub unsafe fn sys_write(fd: i32, data: &[u8]) -> isize { ) as isize } +/// # Safety +/// +/// System call #[inline(always)] pub unsafe fn sys_fstatat(at: i32, pathname: &str, statbuf: &mut Stat, flags: u32) -> i32 { syscall!( @@ -132,11 +156,17 @@ pub unsafe fn sys_fstatat(at: i32, pathname: &str, statbuf: &mut Stat, flags: u3 ) as i32 } +/// # Safety +/// +/// System call #[inline(always)] pub unsafe fn sys_fork() -> i32 { syscall!(abi::SYS_FORK) as i32 } +/// # Safety +/// +/// System call #[inline(always)] pub unsafe fn sys_execve(pathname: &str) -> i32 { syscall!(