Repository navigation
Android: 16 KB pages and abort messages #876
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0876d60
chore(quest): claim quest/m1/ffi-android.md
kixelated 165d633
ffi: align Android libraries to 16 KB pages and report aborts to logcat
kixelated 2f8a79c
ffi: document Android alignment and abort reports, guard toPath below…
kixelated 3ca6ce1
ffi: format the Android test
kixelated e330218
ffi: fence each lent buffer with its monitor, as API 24 has no reacha…
kixelated 537da65
ffi: say bionic keeps the first abort message, fix the reachabilityFe…
kixelated d31f299
Merge origin/main into quest/m1/ffi-android
kixelated 7845c65
ffi: fence kept objects with __Keep, bind the Android tombstone read …
kixelated e32995c
ffi: format the tombstone read
kixelated 045b7f9
quest: keep a Kotlin callback's lent return alive until Rust reads it
kixelated 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
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,73 @@ | ||
| //! What an abort leaves on Android: stderr goes nowhere there, so a message that only | ||
| //! printed there vanishes. It goes to logcat and to bionic's abort message instead, which | ||
| //! the tombstone, `ApplicationExitInfo`, and Play vitals show. | ||
|
|
||
| use std::cell::RefCell; | ||
| use std::ffi::{CString, c_char, c_int}; | ||
| use std::panic::PanicHookInfo; | ||
|
|
||
| #[link(name = "log")] | ||
| unsafe extern "C" { | ||
| fn __android_log_write(priority: c_int, tag: *const c_char, text: *const c_char) -> c_int; | ||
| } | ||
|
|
||
| unsafe extern "C" { | ||
| fn android_set_abort_message(message: *const c_char); | ||
| } | ||
|
|
||
| /// `ANDROID_LOG_FATAL`. | ||
| const FATAL: c_int = 7; | ||
|
|
||
| /// What a panic that reaches the C ABI prints second, after its own message, as the abort | ||
| /// it becomes. | ||
| const CANNOT_UNWIND: &str = "panic in a function that cannot unwind"; | ||
|
|
||
| /// Write `text` to logcat and set it as the abort message, which the abort that follows | ||
| /// shows in its tombstone. Bionic keeps the first abort message and ignores each later one, | ||
| /// and liblog sets a fatal line as one too, so the first call in a process fixes what its | ||
| /// tombstone shows; logcat shows every call. | ||
| pub fn report(text: &str) { | ||
| // A NUL would end either early. | ||
| let text = CString::new(text.replace('\0', "\u{fffd}")).expect("no NUL is left"); | ||
| // SAFETY: both copy the NUL-terminated strings they are given. | ||
| unsafe { | ||
| __android_log_write(FATAL, c"rust2-ffi".as_ptr(), text.as_ptr()); | ||
| android_set_abort_message(text.as_ptr()); | ||
| } | ||
| } | ||
|
|
||
| thread_local! { | ||
| /// The message of the last panic on this thread. | ||
| static LAST: RefCell<String> = const { RefCell::new(String::new()) }; | ||
| } | ||
|
|
||
| /// Report each panic, as the standard library's panic runtime does only in a build that | ||
| /// aborts on one, which this library is not. A panic that crosses the C ABI aborts after a | ||
| /// second panic whose message does not name its cause, so that one reports both. On a thread | ||
| /// with no Rust frame to unwind to, such as ART's, it aborts at once with no second panic, so | ||
| /// the hook cannot tell a panic that aborts from one a caller catches: it reports each, and | ||
| /// bionic keeps the first, which a caught panic that precedes an abort takes. | ||
| fn hook(info: &PanicHookInfo<'_>) { | ||
| let text = info.to_string(); | ||
| LAST.with_borrow_mut(|last| { | ||
| if text.ends_with(CANNOT_UNWIND) && !last.is_empty() { | ||
| report(&format!("{last}\n{text}")); | ||
| } else { | ||
| report(&text); | ||
|
kixelated marked this conversation as resolved.
Resolved
kixelated marked this conversation as resolved.
Resolved
|
||
| *last = text; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /// Run as the library loads, so a panic before any call reports too. | ||
| extern "C" fn install() { | ||
| let previous = std::panic::take_hook(); | ||
| std::panic::set_hook(Box::new(move |info| { | ||
| hook(info); | ||
| previous(info); | ||
| })); | ||
| } | ||
|
|
||
| #[used] | ||
| #[unsafe(link_section = ".init_array")] | ||
| static INSTALL: extern "C" fn() = install; | ||
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
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 was deleted.
Oops, something went wrong.
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,9 @@ | ||
| # [S] Keep a Kotlin callback's lent return alive until Rust reads it | ||
|
|
||
| ## Goal | ||
|
|
||
| A Kotlin closure or callback that returns Rust's lent bytes (a `Bytes` over a `__Lent`, which Rust shares without a copy) keeps the owner reachable until Rust has read the encoded return, so the GC cannot run `__bytes_free` between the return and Rust's read. A differential test returns a lent `Bytes` over 1 KiB from a callback while forcing collections, and Rust reads it intact. | ||
|
|
||
| ## Plan | ||
|
|
||
| Found by CodeRabbit on PR #876, which did not cause it: `__Writer.use { w -> write; w.finish() }` (ffi/src/kotlin.rs:962) releases the writer, and so fences `kept`, before the expression returns the encoding, and Rust reads the lent bytes after that. The `__Lent` KDoc covers only an argument of a JNI call, where the native frame keeps it alive. Hold the owner past the read, for example by returning it with the encoding and fencing after the Rust call returns, or by copying a lent return at this boundary; settle which with a test that fails first. The older `Reference.reachabilityFence` in `release` had the same window on HotSpot, so the API 24 monitor fallback only extends it. |
Oops, something went wrong.
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.