Skip to content

Commit 73a51bf

Browse files
committed
Update abort message patch
This is the version merged upstream. This should be available in 1.53.0. Change-Id: I4fbf240b7134227df5f1ba9217056b5385c49fad Bug: 179455420
1 parent 8df7a80 commit 73a51bf

File tree

1 file changed

+71
-45
lines changed

1 file changed

+71
-45
lines changed

patches/rustc-0011-Populate-abort-message.patch

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
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
54

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
1313

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",
1426
diff --git a/library/panic_abort/Cargo.toml b/library/panic_abort/Cargo.toml
15-
index b15919fad7..94bd77b4d8 100644
27+
index caa89aa30d0..bdab664cd64 100644
1628
--- a/library/panic_abort/Cargo.toml
1729
+++ b/library/panic_abort/Cargo.toml
18-
@@ -10,6 +10,7 @@ bench = false
30+
@@ -13,6 +13,7 @@ bench = false
1931
doc = false
2032

2133
[dependencies]
@@ -25,39 +37,61 @@ index b15919fad7..94bd77b4d8 100644
2537
libc = { version = "0.2", default-features = false }
2638
diff --git a/library/panic_abort/src/android.rs b/library/panic_abort/src/android.rs
2739
new file mode 100644
28-
index 0000000000..f68bfda408
40+
index 00000000000..34d77502eab
2941
--- /dev/null
3042
+++ b/library/panic_abort/src/android.rs
31-
@@ -0,0 +1,27 @@
32-
+#![feature(std_internals)]
33-
+
43+
@@ -0,0 +1,49 @@
3444
+use alloc::string::String;
35-
+use alloc::boxed::Box;
36-
+use core::any::Any;
45+
+use core::mem::transmute;
3746
+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) -> ();
3851
+
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();
4270
+ let msg = match payload.downcast_ref::<&'static str>() {
43-
+ Some(s) => String::from(*s),
71+
+ Some(msg) => msg.as_bytes(),
4472
+ 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 => &[],
4775
+ },
4876
+ };
49-
+ return msg
50-
+}
77+
+ if msg.is_empty() {
78+
+ return;
79+
+ }
5180
+
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);
5892
+}
5993
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
6195
--- a/library/panic_abort/src/lib.rs
6296
+++ b/library/panic_abort/src/lib.rs
6397
@@ -19,6 +19,9 @@
@@ -70,22 +104,14 @@ index eb2277d8ba..4b700cb86e 100644
70104
use core::any::Any;
71105
use core::panic::BoxMeUp;
72106

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.
79109
#[rustc_std_internal_symbol]
80110
pub unsafe extern "C" fn __rust_start_panic(_payload: *mut &mut dyn BoxMeUp) -> u32 {
81-
+
82111
+ // Android has the ability to attach a message as part of the abort.
83112
+ #[cfg(target_os = "android")]
84-
+ android::android_set_abort_message(_payload as *mut &mut dyn BoxMeUp);
113+
+ android::android_set_abort_message(_payload);
85114
+
86115
abort();
87116

88117
cfg_if::cfg_if! {
89-
--
90-
2.31.0.291.g576ba9dcdaf-goog
91-

0 commit comments

Comments
 (0)