1
- From 67e59c05eb2d907d5a8a1ccd5785939af78532f4 Mon Sep 17 00:00:00 2001
2
- From: Jeff Vander Stoep <
[email protected] >
3
- Date: Mon, 29 Mar 2021 12:23:47 +0200
4
- Subject: [PATCH] Populate abort message
1
+ commit 52ee9fbc027f7c371ed2e34ad191633a42c11c95
2
+ Author: Thiébaud Weksteen <
[email protected] >
3
+ Date: Mon Mar 15 11:21:39 2021 +0100
5
4
6
- Change-Id: I36d84d95eededb2670fb9923ad9c848e8519d8e5
7
- ---
8
- library/panic_abort/Cargo.toml | 1 +
9
- library/panic_abort/src/android.rs | 27 +++++++++++++++++++++++++++
10
- library/panic_abort/src/lib.rs | 10 +++++++++-
11
- 3 files changed, 37 insertions(+), 1 deletion(-)
12
- create mode 100644 library/panic_abort/src/android.rs
5
+ android: set abort message
6
+
7
+ Android has the ability to supply an abort message [1]. This message is
8
+ automatically included in the debug trace, which helps debugging [2].
9
+ Modify panic_abort to populate this message before calling abort().
10
+
11
+ [1] https://android.googlesource.com/platform/bionic/+/master/libc/include/android/set_abort_message.h
12
+ [2] https://source.android.com/devices/tech/debug/native-crash
13
13
14
+ diff --git a/Cargo.lock b/Cargo.lock
15
+ index 4bb32f842c2..5b559ae17cf 100644
16
+ --- a/Cargo.lock
17
+ +++ b/Cargo.lock
18
+ @@ -2482,6 +2482,7 @@ dependencies = [
19
+ name = "panic_abort"
20
+ version = "0.0.0"
21
+ dependencies = [
22
+ + "alloc",
23
+ "cfg-if 0.1.10",
24
+ "compiler_builtins",
25
+ "core",
14
26
diff --git a/library/panic_abort/Cargo.toml b/library/panic_abort/Cargo.toml
15
- index b15919fad7..94bd77b4d8 100644
27
+ index caa89aa30d0..bdab664cd64 100644
16
28
--- a/library/panic_abort/Cargo.toml
17
29
+++ b/library/panic_abort/Cargo.toml
18
- @@ -10 ,6 +10 ,7 @@ bench = false
30
+ @@ -13 ,6 +13 ,7 @@ bench = false
19
31
doc = false
20
32
21
33
[dependencies]
@@ -25,39 +37,61 @@ index b15919fad7..94bd77b4d8 100644
25
37
libc = { version = "0.2", default-features = false }
26
38
diff --git a/library/panic_abort/src/android.rs b/library/panic_abort/src/android.rs
27
39
new file mode 100644
28
- index 0000000000..f68bfda408
40
+ index 00000000000..34d77502eab
29
41
--- /dev/null
30
42
+++ b/library/panic_abort/src/android.rs
31
- @@ -0,0 +1,27 @@
32
- + #![feature(std_internals)]
33
- +
43
+ @@ -0,0 +1,49 @@
34
44
+ use alloc::string::String;
35
- + use alloc::boxed::Box;
36
- + use core::any::Any;
45
+ + use core::mem::transmute;
37
46
+ use core::panic::BoxMeUp;
47
+ + use core::ptr::copy_nonoverlapping;
48
+ +
49
+ + const ANDROID_SET_ABORT_MESSAGE: &[u8] = b"android_set_abort_message\0";
50
+ + type SetAbortMessageType = unsafe extern "C" fn(*const libc::c_char) -> ();
38
51
+
39
- + #[rustc_std_internal_symbol]
40
- + unsafe fn format_payload(payload: *mut &mut dyn BoxMeUp) -> String {
41
- + let payload = Box::from_raw((*payload).take_box());
52
+ + // Forward the abort message to libc's android_set_abort_message. We try our best to populate the
53
+ + // message but as this function may already be called as part of a failed allocation, it may not be
54
+ + // possible to do so.
55
+ + //
56
+ + // Some methods of core are on purpose avoided (such as try_reserve) as these rely on the correct
57
+ + // resolution of rust_eh_personality which is loosely defined in panic_abort.
58
+ + //
59
+ + // Weakly resolve the symbol for android_set_abort_message. This function is only available
60
+ + // for API >= 21.
61
+ + pub(crate) unsafe fn android_set_abort_message(payload: *mut &mut dyn BoxMeUp) {
62
+ + let func_addr =
63
+ + libc::dlsym(libc::RTLD_DEFAULT, ANDROID_SET_ABORT_MESSAGE.as_ptr() as *const libc::c_char)
64
+ + as usize;
65
+ + if func_addr == 0 {
66
+ + return;
67
+ + }
68
+ +
69
+ + let payload = (*payload).get();
42
70
+ let msg = match payload.downcast_ref::<&'static str>() {
43
- + Some(s ) => String::from(*s ),
71
+ + Some(msg ) => msg.as_bytes( ),
44
72
+ None => match payload.downcast_ref::<String>() {
45
- + Some(s ) => String::from(s ),
46
- + None => String::from("<unsupported panic payload type>") ,
73
+ + Some(msg ) => msg.as_bytes( ),
74
+ + None => &[] ,
47
75
+ },
48
76
+ };
49
- + return msg
50
- + }
77
+ + if msg.is_empty() {
78
+ + return;
79
+ + }
51
80
+
52
- + pub(crate) unsafe fn android_set_abort_message(payload: *mut &mut dyn BoxMeUp) {
53
- + // std::ffi::CString is not available here. Use alloc::vec to create the char* argument.
54
- + // Manually append the final null byte.
55
- + let mut msg = format_payload(payload).into_bytes();
56
- + msg.push(0);
57
- + libc::android_set_abort_message(msg.as_ptr() as *const libc::c_char);
81
+ + // Allocate a new buffer to append the null byte.
82
+ + let size = msg.len() + 1usize;
83
+ + let buf = libc::malloc(size) as *mut libc::c_char;
84
+ + if buf.is_null() {
85
+ + return; // allocation failure
86
+ + }
87
+ + copy_nonoverlapping(msg.as_ptr(), buf as *mut u8, msg.len());
88
+ + buf.offset(msg.len() as isize).write(0);
89
+ +
90
+ + let func = transmute::<usize, SetAbortMessageType>(func_addr);
91
+ + func(buf);
58
92
+ }
59
93
diff --git a/library/panic_abort/src/lib.rs b/library/panic_abort/src/lib.rs
60
- index eb2277d8ba..4b700cb86e 100644
94
+ index eb2277d8baa..5dcd1e6af36 100644
61
95
--- a/library/panic_abort/src/lib.rs
62
96
+++ b/library/panic_abort/src/lib.rs
63
97
@@ -19,6 +19,9 @@
@@ -70,22 +104,14 @@ index eb2277d8ba..4b700cb86e 100644
70
104
use core::any::Any;
71
105
use core::panic::BoxMeUp;
72
106
73
- @@ -28,9 +31,14 @@ pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn Any + Sen
74
- unreachable!()
75
- }
76
-
77
- - // "Leak" the payload and shim to the relevant abort on the platform in question.
78
- + // Use the relevant abort on the platform in question.
107
+ @@ -31,6 +34,10 @@ pub unsafe extern "C" fn __rust_panic_cleanup(_: *mut u8) -> *mut (dyn Any + Sen
108
+ // "Leak" the payload and shim to the relevant abort on the platform in question.
79
109
#[rustc_std_internal_symbol]
80
110
pub unsafe extern "C" fn __rust_start_panic(_payload: *mut &mut dyn BoxMeUp) -> u32 {
81
- +
82
111
+ // Android has the ability to attach a message as part of the abort.
83
112
+ #[cfg(target_os = "android")]
84
- + android::android_set_abort_message(_payload as *mut &mut dyn BoxMeUp );
113
+ + android::android_set_abort_message(_payload);
85
114
+
86
115
abort();
87
116
88
117
cfg_if::cfg_if! {
89
- - -
90
- 2.31.0.291.g576ba9dcdaf-goog
91
-
0 commit comments