Skip to content

Commit 7ab4578

Browse files
committed
android: set abort message
Android has the ability to supply an abort message [1]. This message is automatically included in the debug trace, which helps debugging [2]. Modify panic_abort to populate this message before calling abort(). [1] https://android.googlesource.com/platform/bionic/+/master/libc/include/android/set_abort_message.h [2] https://source.android.com/devices/tech/debug/native-crash
1 parent 1597416 commit 7ab4578

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

Diff for: panic_abort/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ bench = false
1313
doc = false
1414

1515
[dependencies]
16+
alloc = { path = "../alloc" }
1617
cfg-if = { version = "0.1.8", features = ['rustc-dep-of-std'] }
1718
core = { path = "../core" }
1819
libc = { version = "0.2", default-features = false }

Diff for: panic_abort/src/android.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use alloc::string::String;
2+
use core::mem::transmute;
3+
use core::panic::BoxMeUp;
4+
use core::ptr::copy_nonoverlapping;
5+
6+
const ANDROID_SET_ABORT_MESSAGE: &[u8] = b"android_set_abort_message\0";
7+
type SetAbortMessageType = unsafe extern "C" fn(*const libc::c_char) -> ();
8+
9+
// Forward the abort message to libc's android_set_abort_message. We try our best to populate the
10+
// message but as this function may already be called as part of a failed allocation, it may not be
11+
// possible to do so.
12+
//
13+
// Some methods of core are on purpose avoided (such as try_reserve) as these rely on the correct
14+
// resolution of rust_eh_personality which is loosely defined in panic_abort.
15+
//
16+
// Weakly resolve the symbol for android_set_abort_message. This function is only available
17+
// for API >= 21.
18+
pub(crate) unsafe fn android_set_abort_message(payload: *mut &mut dyn BoxMeUp) {
19+
let func_addr =
20+
libc::dlsym(libc::RTLD_DEFAULT, ANDROID_SET_ABORT_MESSAGE.as_ptr() as *const libc::c_char)
21+
as usize;
22+
if func_addr == 0 {
23+
return;
24+
}
25+
26+
let payload = (*payload).get();
27+
let msg = match payload.downcast_ref::<&'static str>() {
28+
Some(msg) => msg.as_bytes(),
29+
None => match payload.downcast_ref::<String>() {
30+
Some(msg) => msg.as_bytes(),
31+
None => &[],
32+
},
33+
};
34+
if msg.is_empty() {
35+
return;
36+
}
37+
38+
// Allocate a new buffer to append the null byte.
39+
let size = msg.len() + 1usize;
40+
let buf = libc::malloc(size) as *mut libc::c_char;
41+
if buf.is_null() {
42+
return; // allocation failure
43+
}
44+
copy_nonoverlapping(msg.as_ptr(), buf as *mut u8, msg.len());
45+
buf.offset(msg.len() as isize).write(0);
46+
47+
let func = transmute::<usize, SetAbortMessageType>(func_addr);
48+
func(buf);
49+
}

Diff for: panic_abort/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
#![feature(rustc_attrs)]
2020
#![feature(asm)]
2121

22+
#[cfg(target_os = "android")]
23+
mod android;
24+
2225
use core::any::Any;
2326
use core::panic::BoxMeUp;
2427

@@ -31,6 +34,10 @@ pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn Any + Sen
3134
// "Leak" the payload and shim to the relevant abort on the platform in question.
3235
#[rustc_std_internal_symbol]
3336
pub unsafe extern "C" fn __rust_start_panic(_payload: *mut &mut dyn BoxMeUp) -> u32 {
37+
// Android has the ability to attach a message as part of the abort.
38+
#[cfg(target_os = "android")]
39+
android::android_set_abort_message(_payload);
40+
3441
abort();
3542

3643
cfg_if::cfg_if! {

0 commit comments

Comments
 (0)