From 9d1f82ebfcc0925631c2a09273b88e3cadb1b318 Mon Sep 17 00:00:00 2001 From: Tomoaki Kawada Date: Tue, 22 Mar 2022 11:13:56 +0900 Subject: kmc-solid: Use `abort` to abort a program The current implementation uses a `hlt` instruction, which is the most direct way to notify a connected debugger but is not the most flexible way. This commit changes it to a call to the `abort` libc function, making it possible for a system designer to override its behavior as they see fit. --- library/std/src/sys/solid/abi/mod.rs | 26 -------------------------- library/std/src/sys/solid/mod.rs | 15 ++------------- library/std/src/sys/solid/os.rs | 9 +++------ 3 files changed, 5 insertions(+), 45 deletions(-) (limited to 'library/std/src') diff --git a/library/std/src/sys/solid/abi/mod.rs b/library/std/src/sys/solid/abi/mod.rs index 1afc83f766d..8440d572cfb 100644 --- a/library/std/src/sys/solid/abi/mod.rs +++ b/library/std/src/sys/solid/abi/mod.rs @@ -4,32 +4,6 @@ mod fs; pub mod sockets; pub use self::fs::*; -#[inline(always)] -pub fn breakpoint_program_exited(tid: usize) { - unsafe { - match () { - // SOLID_BP_PROGRAM_EXITED = 15 - #[cfg(target_arch = "arm")] - () => core::arch::asm!("bkpt #15", in("r0") tid), - #[cfg(target_arch = "aarch64")] - () => core::arch::asm!("hlt #15", in("x0") tid), - } - } -} - -#[inline(always)] -pub fn breakpoint_abort() { - unsafe { - match () { - // SOLID_BP_CSABORT = 16 - #[cfg(target_arch = "arm")] - () => core::arch::asm!("bkpt #16"), - #[cfg(target_arch = "aarch64")] - () => core::arch::asm!("hlt #16"), - } - } -} - // `solid_types.h` pub use super::itron::abi::{ER, ER_ID, E_TMOUT, ID}; diff --git a/library/std/src/sys/solid/mod.rs b/library/std/src/sys/solid/mod.rs index 492b1a55475..43e3fcfd468 100644 --- a/library/std/src/sys/solid/mod.rs +++ b/library/std/src/sys/solid/mod.rs @@ -74,20 +74,9 @@ pub fn decode_error_kind(code: i32) -> crate::io::ErrorKind { error::decode_error_kind(code) } -#[inline(always)] +#[inline] pub fn abort_internal() -> ! { - loop { - abi::breakpoint_abort(); - } -} - -// This function is needed by the panic runtime. The symbol is named in -// pre-link args for the target specification, so keep that in sync. -#[cfg(not(test))] -#[no_mangle] -// NB. used by both libunwind and libpanic_abort -pub extern "C" fn __rust_abort() { - abort_internal(); + unsafe { libc::abort() } } pub fn hashmap_random_keys() -> (u64, u64) { diff --git a/library/std/src/sys/solid/os.rs b/library/std/src/sys/solid/os.rs index 719d95bbe50..b5649d6e0ff 100644 --- a/library/std/src/sys/solid/os.rs +++ b/library/std/src/sys/solid/os.rs @@ -11,7 +11,7 @@ use crate::path::{self, PathBuf}; use crate::sys_common::rwlock::StaticRwLock; use crate::vec; -use super::{abi, error, itron, memchr}; +use super::{error, itron, memchr}; // `solid` directly maps `errno`s to μITRON error codes. impl itron::error::ItronError { @@ -184,11 +184,8 @@ pub fn home_dir() -> Option { None } -pub fn exit(_code: i32) -> ! { - let tid = itron::task::try_current_task_id().unwrap_or(0); - loop { - abi::breakpoint_program_exited(tid as usize); - } +pub fn exit(code: i32) -> ! { + rtabort!("exit({}) called", code); } pub fn getpid() -> u32 { -- cgit 1.4.1-3-g733a5 From 64ac04567ba397f8600952e3875b9a197f7b2910 Mon Sep 17 00:00:00 2001 From: Frank King Date: Thu, 17 Mar 2022 23:11:49 +0800 Subject: protect `std::io::Take::limit` from overflow in `read` fixs #94981 --- library/std/src/io/mod.rs | 1 + library/std/src/io/tests.rs | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) (limited to 'library/std/src') diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 6005270a75f..004f18bbfcb 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -2559,6 +2559,7 @@ impl Read for Take { let max = cmp::min(buf.len() as u64, self.limit) as usize; let n = self.inner.read(&mut buf[..max])?; + assert!(n as u64 <= self.limit, "number of read bytes exceeds limit"); self.limit -= n as u64; Ok(n) } diff --git a/library/std/src/io/tests.rs b/library/std/src/io/tests.rs index eb626348564..b11292ed82d 100644 --- a/library/std/src/io/tests.rs +++ b/library/std/src/io/tests.rs @@ -583,6 +583,25 @@ fn test_write_all_vectored() { } } +// Issue 94981 +#[test] +#[should_panic = "number of read bytes exceeds limit"] +fn test_take_wrong_length() { + struct LieAboutSize(bool); + + impl Read for LieAboutSize { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + // Lie about the read size at first time of read. + if core::mem::take(&mut self.0) { Ok(buf.len() + 1) } else { Ok(buf.len()) } + } + } + + let mut buffer = vec![0; 4]; + let mut reader = LieAboutSize(true).take(4); + // Primed the `Limit` by lying about the read size. + let _ = reader.read(&mut buffer[..]); +} + #[bench] fn bench_take_read(b: &mut test::Bencher) { b.iter(|| { -- cgit 1.4.1-3-g733a5