-
Notifications
You must be signed in to change notification settings - Fork 13.4k
android: set abort message #81469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
android: set abort message #81469
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use alloc::string::String; | ||
use core::mem::transmute; | ||
use core::panic::BoxMeUp; | ||
use core::ptr::copy_nonoverlapping; | ||
|
||
const ANDROID_SET_ABORT_MESSAGE: &[u8] = b"android_set_abort_message\0"; | ||
type SetAbortMessageType = unsafe extern "C" fn(*const libc::c_char) -> (); | ||
|
||
// Forward the abort message to libc's android_set_abort_message. We try our best to populate the | ||
// message but as this function may already be called as part of a failed allocation, it may not be | ||
// possible to do so. | ||
// | ||
// Some methods of core are on purpose avoided (such as try_reserve) as these rely on the correct | ||
// resolution of rust_eh_personality which is loosely defined in panic_abort. | ||
// | ||
// Weakly resolve the symbol for android_set_abort_message. This function is only available | ||
// for API >= 21. | ||
pub(crate) unsafe fn android_set_abort_message(payload: *mut &mut dyn BoxMeUp) { | ||
let func_addr = | ||
libc::dlsym(libc::RTLD_DEFAULT, ANDROID_SET_ABORT_MESSAGE.as_ptr() as *const libc::c_char) | ||
as usize; | ||
if func_addr == 0 { | ||
return; | ||
} | ||
|
||
let payload = (*payload).get(); | ||
let msg = match payload.downcast_ref::<&'static str>() { | ||
Some(msg) => msg.as_bytes(), | ||
None => match payload.downcast_ref::<String>() { | ||
Some(msg) => msg.as_bytes(), | ||
None => &[], | ||
}, | ||
}; | ||
if msg.is_empty() { | ||
return; | ||
} | ||
|
||
// Allocate a new buffer to append the null byte. | ||
let size = msg.len() + 1usize; | ||
let buf = libc::malloc(size) as *mut libc::c_char; | ||
if buf.is_null() { | ||
return; // allocation failure | ||
} | ||
copy_nonoverlapping(msg.as_ptr(), buf as *mut u8, msg.len()); | ||
buf.offset(msg.len() as isize).write(0); | ||
|
||
let func = transmute::<usize, SetAbortMessageType>(func_addr); | ||
func(buf); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.