diff --git a/build.rs b/build.rs index 2a81bd32133606fff52e021efbcc02e15528a296..6aaa801efd8b23d5394ae2c61627a43c5207c09f 100644 --- a/build.rs +++ b/build.rs @@ -3,7 +3,6 @@ extern crate cc; use std::{env, fs, fs::DirEntry, path::Path}; - // include src/header directories that don't start with '_' fn include_dir(d: &DirEntry) -> bool { d.metadata().map(|m| m.is_dir()).unwrap_or(false) @@ -15,10 +14,7 @@ fn include_dir(d: &DirEntry) -> bool { fn get_target() -> String { env::var("TARGET").unwrap_or( - option_env!("TARGET").map_or( - "x86_64-unknown-redox".to_string(), - |x| x.to_string() - ) + option_env!("TARGET").map_or("x86_64-unknown-redox".to_string(), |x| x.to_string()), ) } @@ -68,7 +64,8 @@ fn main() { let mut cc_builder = &mut cc::Build::new(); - cc_builder = cc_builder.flag("-nostdinc") + cc_builder = cc_builder + .flag("-nostdinc") .flag("-nostdlib") .include(&format!("{}/include", crate_dir)) .include(&format!("{}/target/include", crate_dir)); @@ -77,7 +74,8 @@ fn main() { cc_builder = cc_builder.flag("-mno-outline-atomics") } - cc_builder.flag("-fno-stack-protector") + cc_builder + .flag("-fno-stack-protector") .flag("-Wno-expansion-to-defined") .files( fs::read_dir("src/c") diff --git a/posix-regex b/posix-regex index 0d996efe5cfe7ce181af35d8817ac4deae644d4a..df45954609e219b11f108086b5861023a04126a7 160000 --- a/posix-regex +++ b/posix-regex @@ -1 +1 @@ -Subproject commit 0d996efe5cfe7ce181af35d8817ac4deae644d4a +Subproject commit df45954609e219b11f108086b5861023a04126a7 diff --git a/src/header/grp/mod.rs b/src/header/grp/mod.rs index 814bbaeb5bd2b85a8e691f9ccfa96d6e33d75221..e30a1d1557014db30b7c5f162be68a5c63cef47d 100644 --- a/src/header/grp/mod.rs +++ b/src/header/grp/mod.rs @@ -3,15 +3,20 @@ use core::{ convert::{TryFrom, TryInto}, mem, + num::ParseIntError, ops::{Deref, DerefMut}, pin::Pin, primitive::str, - ptr, slice, num::ParseIntError, str::Matches, + ptr, slice, + str::Matches, }; use lazy_static::lazy_static; -use alloc::{borrow::ToOwned, string::{String, FromUtf8Error}}; +use alloc::{ + borrow::ToOwned, + string::{FromUtf8Error, String}, +}; use crate::{ c_str::CStr, @@ -19,9 +24,9 @@ use crate::{ header::{errno, fcntl, string::strlen}, io, io::{prelude::*, BufReader, Lines}, - platform::types::*, platform, - sync::Mutex + platform::types::*, + sync::Mutex, }; use super::{errno::*, string::strncmp}; @@ -69,7 +74,9 @@ static mut GROUP: group = group { gr_mem: ptr::null_mut(), }; -lazy_static! { static ref LINE_READER: Mutex<Option<Lines<BufReader<File>>>> = Mutex::new(None); } +lazy_static! { + static ref LINE_READER: Mutex<Option<Lines<BufReader<File>>>> = Mutex::new(None); +} #[repr(C)] #[derive(Debug)] @@ -88,7 +95,7 @@ enum Error { Misc(io::Error), FromUtf8Error(FromUtf8Error), ParseIntError(ParseIntError), - Other + Other, } #[derive(Debug)] @@ -110,18 +117,17 @@ impl OwnedGrp { fn split(buf: &mut [u8]) -> Option<group> { let gid = match buf[0..mem::size_of::<gid_t>()].try_into() { Ok(buf) => gid_t::from_ne_bytes(buf), - Err(err) => return None + Err(err) => return None, }; - + // We moved the gid to the beginning of the byte buffer so we can do this. let mut parts = buf[mem::size_of::<gid_t>()..].split_mut(|&c| c == b'\0'); - + Some(group { gr_name: parts.next()?.as_mut_ptr() as *mut i8, gr_passwd: parts.next()?.as_mut_ptr() as *mut i8, gr_gid: gid, - gr_mem: parts.next()?.as_mut_ptr() as *mut *mut c_char - // this will work because this points to the first string, which also happens to be the start of the array. The two are equivalent, just need to by typecast. + gr_mem: parts.next()?.as_mut_ptr() as *mut *mut c_char, // this will work because this points to the first string, which also happens to be the start of the array. The two are equivalent, just need to by typecast. }) } @@ -133,61 +139,61 @@ fn parse_grp(line: String, destbuf: Option<DestBuffer>) -> Result<OwnedGrp, Erro .map(|i| if i == b':' { b'\0' } else { i }) .chain([b'\0']) .collect::<Vec<_>>(); - let mut buffer = buffer - .split_mut(|i| *i == b'\0'); + let mut buffer = buffer.split_mut(|i| *i == b'\0'); let mut gr_gid: gid_t = 0; let strings = { let mut vec: Vec<u8> = Vec::new(); - + let gr_name = buffer.next().ok_or(Error::EOF)?.to_vec(); let gr_passwd = buffer.next().ok_or(Error::EOF)?.to_vec(); gr_gid = String::from_utf8(buffer.next().ok_or(Error::EOF)?.to_vec()) .map_err(|err| Error::FromUtf8Error(err))? .parse::<gid_t>() .map_err(|err| Error::ParseIntError(err))?; - + // Place the gid at the beginning of the byte buffer to make getting it back out again later, much faster. - + vec.extend(gr_gid.to_ne_bytes()); vec.extend(gr_name); vec.push(0); vec.extend(gr_passwd); vec.push(0); - - for i in buffer.next().ok_or(Error::EOF)? + + for i in buffer + .next() + .ok_or(Error::EOF)? .split(|b| *b == b',') - .filter(|i| i.len() > 0) { - + .filter(|i| i.len() > 0) + { vec.extend(i.to_vec()); vec.push(0); } - + vec.extend(0usize.to_ne_bytes()); - + vec }; - + let mut buffer = match destbuf { None => MaybeAllocated::Owned(Box::into_pin(strings.into_boxed_slice())), Some(buf) => { let mut buf = MaybeAllocated::Borrowed(buf); - + if buf.len() < buf.len() { - unsafe { platform::errno = errno::ERANGE; } + unsafe { + platform::errno = errno::ERANGE; + } return Err(Error::BufTooSmall); } - + buf[..strings.len()].copy_from_slice(&strings); buf } }; let reference = split(&mut buffer).ok_or(Error::Other)?; - - Ok(OwnedGrp { - buffer, - reference - }) + + Ok(OwnedGrp { buffer, reference }) } // MT-Unsafe race:grgid locale @@ -198,7 +204,7 @@ pub unsafe extern "C" fn getgrgid(gid: gid_t) -> *mut group { for line in BufReader::new(db).lines() { let Ok(line) = line else { return ptr::null_mut() }; let Ok(grp) = parse_grp(line, None) else { return ptr::null_mut() }; - + if grp.reference.gr_gid == gid { return grp.into_global(); } @@ -214,11 +220,16 @@ pub unsafe extern "C" fn getgrnam(name: *const c_char) -> *mut group { for line in BufReader::new(db).lines() { let Ok(line) = line else { return ptr::null_mut() }; - + let Ok(grp) = parse_grp(line, None) else { return ptr::null_mut() }; - + // Attempt to prevent BO vulnerabilities - if strncmp(grp.reference.gr_name, name, strlen(grp.reference.gr_name).min(strlen(name))) > 0 { + if strncmp( + grp.reference.gr_name, + name, + strlen(grp.reference.gr_name).min(strlen(name)), + ) > 0 + { return grp.into_global(); } } @@ -228,7 +239,13 @@ pub unsafe extern "C" fn getgrnam(name: *const c_char) -> *mut group { // MT-Safe locale #[no_mangle] -pub unsafe extern "C" fn getgrgid_r(gid: gid_t, result_buf: *mut group, buffer: *mut c_char, buflen: usize, result: *mut *mut group) -> c_int { +pub unsafe extern "C" fn getgrgid_r( + gid: gid_t, + result_buf: *mut group, + buffer: *mut c_char, + buflen: usize, + result: *mut *mut group, +) -> c_int { let Ok(db) = File::open(c_str!("/etc/group"), fcntl::O_RDONLY) else { return ENOENT }; for line in BufReader::new(db).lines() { @@ -238,31 +255,42 @@ pub unsafe extern "C" fn getgrgid_r(gid: gid_t, result_buf: *mut group, buffer: if grp.reference.gr_gid == gid { *result_buf = grp.reference; *result = result_buf; - + return 0; } } - + return ENOENT; } // MT-Safe locale #[no_mangle] -pub unsafe extern "C" fn getgrnam_r(name: *const c_char, result_buf: *mut group, buffer: *mut c_char, buflen: usize, result: *mut *mut group) -> c_int { +pub unsafe extern "C" fn getgrnam_r( + name: *const c_char, + result_buf: *mut group, + buffer: *mut c_char, + buflen: usize, + result: *mut *mut group, +) -> c_int { let Ok(db) = File::open(c_str!("/etc/group"), fcntl::O_RDONLY) else { return ENOENT }; for line in BufReader::new(db).lines() { let Ok(line) = line else { return EINVAL }; let Ok(mut grp) = parse_grp(line, Some(DestBuffer { ptr: buffer as *mut u8, len: buflen })) else { return EINVAL }; - if strncmp(grp.reference.gr_name, name, strlen(grp.reference.gr_name).min(strlen(name))) > 0 { + if strncmp( + grp.reference.gr_name, + name, + strlen(grp.reference.gr_name).min(strlen(name)), + ) > 0 + { *result_buf = grp.reference; *result = result_buf; - + return 0; } } - + return ENOENT; } @@ -270,20 +298,21 @@ pub unsafe extern "C" fn getgrnam_r(name: *const c_char, result_buf: *mut group, #[no_mangle] pub unsafe extern "C" fn getgrent() -> *mut group { let mut line_reader = LINE_READER.lock(); - + if line_reader.is_none() { let Ok(db) = File::open(c_str!("/etc/group"), fcntl::O_RDONLY) else { return ptr::null_mut() }; *line_reader = Some(BufReader::new(db).lines()); } - - if let Some(lines) = line_reader.deref_mut() { + + if let Some(lines) = line_reader.deref_mut() { let Some(line) = lines.next() else { return ptr::null_mut() }; let Ok(line) = line else { return ptr::null_mut() }; - + if let Ok(grp) = parse_grp(line, None) { return grp.into_global(); - } else { return ptr::null_mut(); } - + } else { + return ptr::null_mut(); + } } else { return ptr::null_mut(); } @@ -299,13 +328,18 @@ pub unsafe extern "C" fn endgrent() { // MT-Unsafe race:grent locale #[no_mangle] pub unsafe extern "C" fn setgrent() { - let mut line_reader = LINE_READER.lock(); + let mut line_reader = LINE_READER.lock(); let Ok(db) = File::open(c_str!("/etc/group"), fcntl::O_RDONLY) else { return }; *line_reader = Some(BufReader::new(db).lines()); } #[no_mangle] -pub unsafe extern "C" fn getgrouplist(user: *const c_char, group: gid_t, groups: *mut gid_t, ngroups: i32) -> i32 { +pub unsafe extern "C" fn getgrouplist( + user: *const c_char, + group: gid_t, + groups: *mut gid_t, + ngroups: i32, +) -> i32 { let mut grps = Vec::<gid_t>::from_raw_parts(groups, 0, ngroups as usize); let Ok(usr) = (crate::c_str::CStr::from_ptr(user).to_str()) else { return 0 }; diff --git a/src/header/wchar/mod.rs b/src/header/wchar/mod.rs index a6bf6b3e7375ab4091e49591c24e870eb6b1ab94..c1da8768a6971888af154579a756106d692c0c72 100644 --- a/src/header/wchar/mod.rs +++ b/src/header/wchar/mod.rs @@ -4,8 +4,13 @@ use core::{char, ffi::VaList as va_list, mem, ptr, slice, usize}; use crate::{ header::{ - ctype::isspace, errno::{ERANGE, EILSEQ, ENOMEM}, stdio::*, - stdlib::{MB_CUR_MAX, MB_LEN_MAX, malloc}, string, time::*, wctype::*, + ctype::isspace, + errno::{EILSEQ, ENOMEM, ERANGE}, + stdio::*, + stdlib::{malloc, MB_CUR_MAX, MB_LEN_MAX}, + string, + time::*, + wctype::*, }, platform::{self, errno, types::*}, }; @@ -46,8 +51,13 @@ pub unsafe extern "C" fn fgetwc(stream: *mut FILE) -> wint_t { let mut wc: wchar_t = 0; loop { - let nread = fread(buf[bytes_read..bytes_read+1].as_mut_ptr() as *mut c_void, 1, 1, stream); - + let nread = fread( + buf[bytes_read..bytes_read + 1].as_mut_ptr() as *mut c_void, + 1, + 1, + stream, + ); + if nread != 1 { errno = EILSEQ; return WEOF; @@ -75,7 +85,12 @@ pub unsafe extern "C" fn fgetwc(stream: *mut FILE) -> wint_t { } } - mbrtowc(&mut wc, buf.as_ptr() as *const c_char, encoded_length, ptr::null_mut()); + mbrtowc( + &mut wc, + buf.as_ptr() as *const c_char, + encoded_length, + ptr::null_mut(), + ); wc as wint_t } @@ -281,8 +296,8 @@ pub unsafe extern "C" fn ungetwc(wc: wint_t, stream: &mut FILE) -> wint_t { return WEOF; } - /* - We might have unget multiple bytes for a single wchar, eg, `ç` is [195, 167]. + /* + We might have unget multiple bytes for a single wchar, eg, `ç` is [195, 167]. We need to unget them in reversed, so they are pused as [..., 167, 195, ...] When we do fgetwc, we pop from the Vec, getting the write order of bytes [195, 167]. If we called ungetc in the non-reversed order, we would get [167, 195] @@ -327,19 +342,12 @@ pub unsafe extern "C" fn vswprintf( } #[no_mangle] -pub unsafe extern "C" fn wcpcpy( - d: *mut wchar_t, - s: *const wchar_t, -) -> *mut wchar_t { +pub unsafe extern "C" fn wcpcpy(d: *mut wchar_t, s: *const wchar_t) -> *mut wchar_t { return (wcscpy(d, s)).offset(wcslen(s) as isize); } #[no_mangle] -pub unsafe extern "C" fn wcpncpy( - d: *mut wchar_t, - s: *const wchar_t, - n: size_t, -) -> *mut wchar_t { +pub unsafe extern "C" fn wcpncpy(d: *mut wchar_t, s: *const wchar_t, n: size_t) -> *mut wchar_t { return (wcsncpy(d, s, n)).offset(wcsnlen(s, n) as isize); } @@ -359,8 +367,8 @@ pub unsafe extern "C" fn wcrtomb(s: *mut c_char, wc: wchar_t, ps: *mut mbstate_t #[no_mangle] pub unsafe extern "C" fn wcsdup(s: *const wchar_t) -> *mut wchar_t { let l = wcslen(s); - - let d = malloc((l + 1)*mem::size_of::<wchar_t>()) as *mut wchar_t; + + let d = malloc((l + 1) * mem::size_of::<wchar_t>()) as *mut wchar_t; if d.is_null() { errno = ENOMEM; @@ -521,7 +529,7 @@ pub unsafe extern "C" fn wcsncpy( #[no_mangle] pub unsafe extern "C" fn wcsnlen(mut s: *const wchar_t, maxlen: size_t) -> size_t { let mut len = 0; - + while len < maxlen { if *s == 0 { break; @@ -550,10 +558,10 @@ pub unsafe extern "C" fn wcsnrtombs( if ps.is_null() { ps = &mut mbs; } - + while read < nwc { buf.fill(0); - + let ret = wcrtomb(buf.as_mut_ptr(), **src, ps); if ret == size_t::MAX { @@ -574,7 +582,7 @@ pub unsafe extern "C" fn wcsnrtombs( *src = ptr::null(); return written; } - + *src = (*src).add(1); read += 1; written += ret; @@ -582,7 +590,6 @@ pub unsafe extern "C" fn wcsnrtombs( written } - #[no_mangle] pub unsafe extern "C" fn wcspbrk(mut wcs: *const wchar_t, set: *const wchar_t) -> *mut wchar_t { wcs = wcs.add(wcscspn(wcs, set)); diff --git a/src/platform/redox/exec.rs b/src/platform/redox/exec.rs index b327f7193a7a52c0547c17396ce294a5c1c90f1f..f0375437300a5891905893efe17d55d9b36a0963 100644 --- a/src/platform/redox/exec.rs +++ b/src/platform/redox/exec.rs @@ -93,7 +93,10 @@ pub fn execve( // NOTE: We must omit O_CLOEXEC and close manually, otherwise it will be closed before we // have even read it! let (mut image_file, arg0) = match exec { - Executable::AtPath(path) => (File::open(path, O_RDONLY as c_int).map_err(|_| Error::new(ENOENT))?, path.to_bytes()), + Executable::AtPath(path) => ( + File::open(path, O_RDONLY as c_int).map_err(|_| Error::new(ENOENT))?, + path.to_bytes(), + ), Executable::InFd { file, arg0 } => (file, arg0), }; diff --git a/src/platform/redox/libredox.rs b/src/platform/redox/libredox.rs index 3a70fdd7cf3ef3efca9ea4da96bdf32743d8131c..337d9329791f44a852b4de9d70f52c694b43bb5e 100644 --- a/src/platform/redox/libredox.rs +++ b/src/platform/redox/libredox.rs @@ -1,9 +1,11 @@ use core::{slice, str}; -use syscall::{Error, Result, EMFILE, WaitFlags}; +use syscall::{Error, Result, WaitFlags, EMFILE}; -use crate::header::signal::sigaction; -use crate::platform::types::{c_int, mode_t}; +use crate::{ + header::signal::sigaction, + platform::types::{c_int, mode_t}, +}; pub type RawResult = usize; @@ -13,15 +15,26 @@ pub fn open(path: &str, oflag: c_int, mode: mode_t) -> Result<usize> { ((oflag as usize) & 0xFFFF_0000) | ((mode as usize) & 0xFFFF), )?; - c_int::try_from(usize_fd).map_err(|_| { - let _ = syscall::close(usize_fd); - Error::new(EMFILE) - }).map(|f| f as usize) + c_int::try_from(usize_fd) + .map_err(|_| { + let _ = syscall::close(usize_fd); + Error::new(EMFILE) + }) + .map(|f| f as usize) } #[no_mangle] -pub unsafe extern "C" fn redox_open_v1(path_base: *const u8, path_len: usize, flags: u32, mode: u16) -> RawResult { - Error::mux(open(str::from_utf8_unchecked(slice::from_raw_parts(path_base, path_len)), flags as c_int, mode as mode_t)) +pub unsafe extern "C" fn redox_open_v1( + path_base: *const u8, + path_len: usize, + flags: u32, + mode: u16, +) -> RawResult { + Error::mux(open( + str::from_utf8_unchecked(slice::from_raw_parts(path_base, path_len)), + flags as c_int, + mode as mode_t, + )) } #[no_mangle] @@ -29,15 +42,31 @@ pub unsafe extern "C" fn redox_dup_v1(fd: usize, buf: *const u8, len: usize) -> Error::mux(syscall::dup(fd, core::slice::from_raw_parts(buf, len))) } #[no_mangle] -pub unsafe extern "C" fn redox_dup2_v1(old_fd: usize, new_fd: usize, buf: *const u8, len: usize) -> RawResult { - Error::mux(syscall::dup2(old_fd, new_fd, core::slice::from_raw_parts(buf, len))) +pub unsafe extern "C" fn redox_dup2_v1( + old_fd: usize, + new_fd: usize, + buf: *const u8, + len: usize, +) -> RawResult { + Error::mux(syscall::dup2( + old_fd, + new_fd, + core::slice::from_raw_parts(buf, len), + )) } #[no_mangle] pub unsafe extern "C" fn redox_read_v1(fd: usize, dst_base: *mut u8, dst_len: usize) -> RawResult { - Error::mux(syscall::read(fd, slice::from_raw_parts_mut(dst_base, dst_len))) + Error::mux(syscall::read( + fd, + slice::from_raw_parts_mut(dst_base, dst_len), + )) } #[no_mangle] -pub unsafe extern "C" fn redox_write_v1(fd: usize, src_base: *const u8, src_len: usize) -> RawResult { +pub unsafe extern "C" fn redox_write_v1( + fd: usize, + src_base: *const u8, + src_len: usize, +) -> RawResult { Error::mux(syscall::write(fd, slice::from_raw_parts(src_base, src_len))) } #[no_mangle] @@ -59,7 +88,10 @@ pub unsafe extern "C" fn redox_fchown_v1(fd: usize, new_uid: u32, new_gid: u32) } #[no_mangle] pub unsafe extern "C" fn redox_fpath_v1(fd: usize, dst_base: *mut u8, dst_len: usize) -> RawResult { - Error::mux(syscall::fpath(fd, core::slice::from_raw_parts_mut(dst_base, dst_len))) + Error::mux(syscall::fpath( + fd, + core::slice::from_raw_parts_mut(dst_base, dst_len), + )) } #[no_mangle] pub unsafe extern "C" fn redox_close_v1(fd: usize) -> RawResult { @@ -94,7 +126,11 @@ pub unsafe extern "C" fn redox_setrens_v1(rns: usize, ens: usize) -> RawResult { #[no_mangle] pub unsafe extern "C" fn redox_waitpid_v1(pid: usize, status: *mut i32, options: u32) -> RawResult { let mut sts = 0_usize; - let res = Error::mux(syscall::waitpid(pid, &mut sts, WaitFlags::from_bits_truncate(options as usize))); + let res = Error::mux(syscall::waitpid( + pid, + &mut sts, + WaitFlags::from_bits_truncate(options as usize), + )); status.write(sts as i32); res } @@ -105,11 +141,19 @@ pub unsafe extern "C" fn redox_kill_v1(pid: usize, signal: u32) -> RawResult { } #[no_mangle] -pub unsafe extern "C" fn redox_sigaction_v1(signal: u32, new: *const sigaction, old: *mut sigaction) -> RawResult { +pub unsafe extern "C" fn redox_sigaction_v1( + signal: u32, + new: *const sigaction, + old: *mut sigaction, +) -> RawResult { Error::mux(super::signal::sigaction_impl(signal as i32, new.as_ref(), old.as_mut()).map(|()| 0)) } #[no_mangle] -pub unsafe extern "C" fn redox_sigprocmask_v1(how: u32, new: *const u64, old: *mut u64) -> RawResult { +pub unsafe extern "C" fn redox_sigprocmask_v1( + how: u32, + new: *const u64, + old: *mut u64, +) -> RawResult { Error::mux(super::signal::sigprocmask_impl(how as i32, new, old).map(|()| 0)) } diff --git a/src/platform/redox/mod.rs b/src/platform/redox/mod.rs index 14e93760a2c2f8f541d84467e638a3ca76fa442d..a532de7fbbbc7f5b44038e1975d390311660e93d 100644 --- a/src/platform/redox/mod.rs +++ b/src/platform/redox/mod.rs @@ -63,7 +63,7 @@ macro_rules! path_from_c_str { }}; } -use self::{path::canonicalize, exec::Executable}; +use self::{exec::Executable, path::canonicalize}; pub fn e(sys: Result<usize>) -> usize { match sys { @@ -207,7 +207,10 @@ impl Pal for Sys { fn clock_settime(clk_id: clockid_t, tp: *const timespec) -> c_int { // TODO - eprintln!("relibc clock_settime({}, {:p}): not implemented", clk_id, tp); + eprintln!( + "relibc clock_settime({}, {:p}): not implemented", + clk_id, tp + ); unsafe { errno = ENOSYS }; -1 } @@ -238,7 +241,10 @@ impl Pal for Sys { } unsafe fn fexecve(fildes: c_int, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int { e(self::exec::execve( - Executable::InFd { file: File::new(fildes), arg0: CStr::from_ptr(argv.read()).to_bytes() }, + Executable::InFd { + file: File::new(fildes), + arg0: CStr::from_ptr(argv.read()).to_bytes(), + }, self::exec::ArgEnv::C { argv, envp }, None, )) as c_int @@ -573,7 +579,10 @@ impl Pal for Sys { unsafe fn getrlimit(resource: c_int, rlim: *mut rlimit) -> c_int { //TODO - eprintln!("relibc getrlimit({}, {:p}): not implemented", resource, rlim); + eprintln!( + "relibc getrlimit({}, {:p}): not implemented", + resource, rlim + ); if !rlim.is_null() { (*rlim).rlim_cur = RLIM_INFINITY; (*rlim).rlim_max = RLIM_INFINITY; @@ -583,7 +592,10 @@ impl Pal for Sys { unsafe fn setrlimit(resource: c_int, rlim: *const rlimit) -> c_int { //TOOD - eprintln!("relibc setrlimit({}, {:p}): not implemented", resource, rlim); + eprintln!( + "relibc setrlimit({}, {:p}): not implemented", + resource, rlim + ); unsafe { errno = EPERM }; -1 } @@ -715,7 +727,10 @@ impl Pal for Sys { } unsafe fn msync(addr: *mut c_void, len: usize, flags: c_int) -> c_int { - eprintln!("relibc msync({:p}, 0x{:x}, 0x{:x}): not implemented", addr, len, flags); + eprintln!( + "relibc msync({:p}, 0x{:x}, 0x{:x}): not implemented", + addr, len, flags + ); e(Err(syscall::Error::new(syscall::ENOSYS))) as c_int /* TODO e(syscall::msync( @@ -744,7 +759,10 @@ impl Pal for Sys { } unsafe fn madvise(addr: *mut c_void, len: usize, flags: c_int) -> c_int { - eprintln!("relibc madvise({:p}, 0x{:x}, 0x{:x}): not implemented", addr, len, flags); + eprintln!( + "relibc madvise({:p}, 0x{:x}, 0x{:x}): not implemented", + addr, len, flags + ); e(Err(syscall::Error::new(syscall::ENOSYS))) as c_int } @@ -850,7 +868,10 @@ impl Pal for Sys { fn setpriority(which: c_int, who: id_t, prio: c_int) -> c_int { // TODO - eprintln!("relibc setpriority({}, {}, {}): not implemented", which, who, prio); + eprintln!( + "relibc setpriority({}, {}, {}): not implemented", + which, who, prio + ); unsafe { errno = ENOSYS }; -1 } diff --git a/src/platform/redox/redox-exec/src/lib.rs b/src/platform/redox/redox-exec/src/lib.rs index 7420c22b5f7408531bb1d193889e0540d7e4c9bd..a5c049baf1d64353c9c5d24d4c5cd0ba75d6ed9a 100644 --- a/src/platform/redox/redox-exec/src/lib.rs +++ b/src/platform/redox/redox-exec/src/lib.rs @@ -23,8 +23,8 @@ use goblin::elf64::{ use syscall::{ error::*, flag::{MapFlags, SEEK_SET}, - PAGE_SIZE, Map, PROT_WRITE, O_CLOEXEC, - GrantDesc, GrantFlags, PROT_READ, PROT_EXEC, MAP_SHARED, MAP_FIXED_NOREPLACE, + GrantDesc, GrantFlags, Map, MAP_FIXED_NOREPLACE, MAP_SHARED, O_CLOEXEC, PAGE_SIZE, PROT_EXEC, + PROT_READ, PROT_WRITE, }; pub use self::arch::*; @@ -174,13 +174,8 @@ where let (first_aligned_page, remaining_filesz) = if voff > 0 { let bytes_to_next_page = PAGE_SIZE - voff; - let (_guard, dst_page) = unsafe { - MmapGuard::map_mut_anywhere( - *grants_fd, - vaddr, - PAGE_SIZE, - )? - }; + let (_guard, dst_page) = + unsafe { MmapGuard::map_mut_anywhere(*grants_fd, vaddr, PAGE_SIZE)? }; let length = core::cmp::min(bytes_to_next_page, filesz); @@ -209,16 +204,19 @@ where // Use commented out lines to trigger kernel bug (FIXME). //let pages_in_this_group = core::cmp::min(PAGES_PER_ITER, file_page_count - page_idx * PAGES_PER_ITER); - let pages_in_this_group = core::cmp::min(PAGES_PER_ITER, remaining_page_count - page_idx); + let pages_in_this_group = + core::cmp::min(PAGES_PER_ITER, remaining_page_count - page_idx); - if pages_in_this_group == 0 { break } + if pages_in_this_group == 0 { + break; + } // TODO: MAP_FIXED to optimize away funmap? let (_guard, dst_memory) = unsafe { MmapGuard::map_mut_anywhere( *grants_fd, first_aligned_page + page_idx * PAGE_SIZE, // offset - pages_in_this_group * PAGE_SIZE, // size + pages_in_this_group * PAGE_SIZE, // size )? }; @@ -290,7 +288,11 @@ where }; unsafe { - page.as_mut_ptr_slice().as_mut_ptr().add(new_page_off).cast::<usize>().write(word); + page.as_mut_ptr_slice() + .as_mut_ptr() + .add(new_page_off) + .cast::<usize>() + .write(word); } Ok(()) @@ -312,11 +314,8 @@ where MapFlags::PROT_READ | MapFlags::PROT_WRITE, )?; unsafe { - let (_guard, memory) = MmapGuard::map_mut_anywhere( - *grants_fd, - pheaders, - pheaders_size_aligned, - )?; + let (_guard, memory) = + MmapGuard::map_mut_anywhere(*grants_fd, pheaders, pheaders_size_aligned)?; memory[..pheaders_to_convey.len()].copy_from_slice(pheaders_to_convey); } @@ -380,11 +379,7 @@ where let aligned_size = size.next_multiple_of(PAGE_SIZE); let (_guard, memory) = unsafe { - MmapGuard::map_mut_anywhere( - *grants_fd, - containing_page, - aligned_size, - )? + MmapGuard::map_mut_anywhere(*grants_fd, containing_page, aligned_size)? }; memory[displacement..][..source_slice.len()].copy_from_slice(source_slice); } @@ -606,22 +601,34 @@ impl MmapGuard { flags.remove(MapFlags::MAP_FIXED_NOREPLACE); flags.insert(MapFlags::MAP_FIXED); - let _new_base = unsafe { syscall::fmap(self.fd, &Map { - offset, - size: self.size, - flags, - address: self.base, - })? }; + let _new_base = unsafe { + syscall::fmap( + self.fd, + &Map { + offset, + size: self.size, + flags, + address: self.base, + }, + )? + }; Ok(()) } - pub unsafe fn map_mut_anywhere<'a>(fd: usize, offset: usize, size: usize) -> Result<(Self, &'a mut [u8])> { - let mut this = Self::map(fd, &Map { - size, - offset, - address: 0, - flags: PROT_WRITE, - })?; + pub unsafe fn map_mut_anywhere<'a>( + fd: usize, + offset: usize, + size: usize, + ) -> Result<(Self, &'a mut [u8])> { + let mut this = Self::map( + fd, + &Map { + size, + offset, + address: 0, + flags: PROT_WRITE, + }, + )?; let slice = &mut *this.as_mut_ptr_slice(); Ok((this, slice)) @@ -704,10 +711,7 @@ fn fork_inner(initial_rsp: *mut usize) -> Result<usize> { let (cur_filetable_fd, new_pid_fd, new_pid); { - let cur_pid_fd = FdGuard::new(syscall::open( - "thisproc:current/open_via_dup", - O_CLOEXEC, - )?); + let cur_pid_fd = FdGuard::new(syscall::open("thisproc:current/open_via_dup", O_CLOEXEC)?); (new_pid_fd, new_pid) = new_context()?; // Do not allocate new signal stack, but copy existing address (all memory will be re-mapped @@ -759,7 +763,12 @@ fn fork_inner(initial_rsp: *mut usize) -> Result<usize> { let mut grant_desc_buf = [GrantDesc::default(); 16]; loop { let bytes_read = { - let buf = unsafe { core::slice::from_raw_parts_mut(grant_desc_buf.as_mut_ptr().cast(), grant_desc_buf.len() * size_of::<GrantDesc>()) }; + let buf = unsafe { + core::slice::from_raw_parts_mut( + grant_desc_buf.as_mut_ptr().cast(), + grant_desc_buf.len() * size_of::<GrantDesc>(), + ) + }; syscall::read(*cur_addr_space_fd, buf)? }; if bytes_read == 0 { @@ -769,7 +778,9 @@ fn fork_inner(initial_rsp: *mut usize) -> Result<usize> { let grants = &grant_desc_buf[..bytes_read / size_of::<GrantDesc>()]; for grant in grants { - if !grant.flags.contains(GrantFlags::GRANT_SCHEME) || !grant.flags.contains(GrantFlags::GRANT_SHARED) { + if !grant.flags.contains(GrantFlags::GRANT_SCHEME) + || !grant.flags.contains(GrantFlags::GRANT_SHARED) + { continue; } @@ -846,10 +857,7 @@ fn fork_inner(initial_rsp: *mut usize) -> Result<usize> { pub fn new_context() -> Result<(FdGuard, usize)> { // Create a new context (fields such as uid/gid will be inherited from the current context). - let fd = FdGuard::new(syscall::open( - "thisproc:new/open_via_dup", - O_CLOEXEC, - )?); + let fd = FdGuard::new(syscall::open("thisproc:new/open_via_dup", O_CLOEXEC)?); // Extract pid. let mut buffer = [0_u8; 64]; diff --git a/src/platform/redox/signal.rs b/src/platform/redox/signal.rs index 0b36869b736c4cb3736db68fdc12c8e0ff220aa4..da53f619d23f207bcbf73fa11ee478b080d97c52 100644 --- a/src/platform/redox/signal.rs +++ b/src/platform/redox/signal.rs @@ -140,7 +140,11 @@ impl PalSignal for Sys { } } -pub(crate) fn sigaction_impl(sig: i32, act: Option<&sigaction>, oact: Option<&mut sigaction>) -> Result<()> { +pub(crate) fn sigaction_impl( + sig: i32, + act: Option<&sigaction>, + oact: Option<&mut sigaction>, +) -> Result<()> { let new_opt = act.map(|act| { let m = act.sa_mask; let sa_handler = unsafe { mem::transmute(act.sa_handler) }; @@ -152,11 +156,7 @@ pub(crate) fn sigaction_impl(sig: i32, act: Option<&sigaction>, oact: Option<&mu } }); let mut old_opt = oact.as_ref().map(|_| syscall::SigAction::default()); - syscall::sigaction( - sig as usize, - new_opt.as_ref(), - old_opt.as_mut(), - )?; + syscall::sigaction(sig as usize, new_opt.as_ref(), old_opt.as_mut())?; if let (Some(old), Some(oact)) = (old_opt, oact) { oact.sa_handler = unsafe { mem::transmute(old.sa_handler) }; let m = old.sa_mask; @@ -172,11 +172,7 @@ pub(crate) fn sigprocmask_impl(how: i32, set: *const sigset_t, oset: *mut sigset Some([unsafe { *set as u64 }, 0]) }; let mut old_opt = if oset.is_null() { None } else { Some([0, 0]) }; - syscall::sigprocmask( - how as usize, - new_opt.as_ref(), - old_opt.as_mut(), - )?; + syscall::sigprocmask(how as usize, new_opt.as_ref(), old_opt.as_mut())?; if let Some(old) = old_opt { unsafe { *oset = old[0] as sigset_t }; }