From 47bd55451f848f0d98d9e0b9ef2926f149fddf65 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Thu, 9 Mar 2023 20:19:27 -0700 Subject: [PATCH] Detailed information on abort using macro and new __abort function --- include/bits/stdlib.h | 2 ++ src/c/stack_chk.c | 8 +++++--- src/c/stdlib.c | 9 +++++++++ src/header/assert/mod.rs | 10 ++++------ src/header/stdlib/mod.rs | 17 +++++++++++++++-- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/include/bits/stdlib.h b/include/bits/stdlib.h index 8d639cef38..a0e8ee14a2 100644 --- a/include/bits/stdlib.h +++ b/include/bits/stdlib.h @@ -1,6 +1,8 @@ #ifndef _BITS_STDLIB_H #define _BITS_STDLIB_H +# define abort() __abort(__func__, __FILE__, __LINE__) + #ifdef __cplusplus extern "C" { #endif diff --git a/src/c/stack_chk.c b/src/c/stack_chk.c index 8574090818..e760722f77 100644 --- a/src/c/stack_chk.c +++ b/src/c/stack_chk.c @@ -1,10 +1,12 @@ #include -void abort(); - uintptr_t __stack_chk_guard = 0xd048c37519fcadfe; +// manually define detailed abort function +void __abort(const char *func, const char *file, int line); + __attribute__((noreturn)) void __stack_chk_fail(void) { - abort(); + // call detailed abort function + __abort(__func__, __FILE__, __LINE__); } diff --git a/src/c/stdlib.c b/src/c/stdlib.c index 39e4fb0f44..3b58c0ccdc 100644 --- a/src/c/stdlib.c +++ b/src/c/stdlib.c @@ -3,3 +3,12 @@ double strtod(const char *nptr, char **endptr); long double strtold(const char *nptr, char **endptr) { return (long double)strtod(nptr, endptr); } + +// manually define detailed abort function +void __abort(const char *func, const char *file, int line); + +// backup definition of abort for programs that link it directly +void abort() { + // call detailed abort function + __abort(__func__, __FILE__, __LINE__); +} diff --git a/src/header/assert/mod.rs b/src/header/assert/mod.rs index bfd6b01c1c..8a9eb3d4e4 100644 --- a/src/header/assert/mod.rs +++ b/src/header/assert/mod.rs @@ -5,7 +5,6 @@ use crate::{ header::{stdio, stdlib}, platform::types::*, }; -use core::fmt::Write; #[no_mangle] pub unsafe extern "C" fn __assert_fail( @@ -18,14 +17,13 @@ pub unsafe extern "C" fn __assert_fail( let file = CStr::from_ptr(file).to_str().unwrap(); let cond = CStr::from_ptr(cond).to_str().unwrap(); - writeln!( - *stdio::stderr, + eprintln!( "{}: {}:{}: Assertion `{}` failed.", func, file, line, cond - ) - .unwrap(); - stdlib::abort(); + ); + + core::intrinsics::abort(); } diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs index 145fb2719e..535a7d6fe8 100644 --- a/src/header/stdlib/mod.rs +++ b/src/header/stdlib/mod.rs @@ -90,8 +90,21 @@ pub unsafe extern "C" fn a64l(s: *const c_char) -> c_long { } #[no_mangle] -pub unsafe extern "C" fn abort() { - eprintln!("abort() called"); +pub unsafe extern "C" fn __abort( + func: *const c_char, + file: *const c_char, + line: c_int, +) { + let func = CStr::from_ptr(func).to_str().unwrap(); + let file = CStr::from_ptr(file).to_str().unwrap(); + + eprintln!( + "{}: {}:{}: Abort", + func, + file, + line + ); + intrinsics::abort(); }