Skip to content
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

Add Android support for the i686 and x86-64 targets to the crash-context crate #76

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions crash-context/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@

## Supported targets

- `x86_64-unknown-linux-gnu`
- `x86_64-unknown-linux-musl`
- `i686-unknown-linux-gnu`
- `i686-unknown-linux-musl`
- `aarch64-android-linux`
- `aarch64-apple-darwin`
- `aarch64-unknown-linux-gnu`
- `aarch64-unknown-linux-musl`
- `aarch64-android-linux`
- `arm-linux-androideabi`
- `arm-unknown-linux-gnueabi`
- `arm-unknown-linux-musleabi`
- `x86_64-pc-windows-msvc`
- `i686-linux-android`
- `i686-unknown-linux-gnu`
- `i686-unknown-linux-musl`
- `x86_64-apple-darwin`
- `aarch64-apple-darwin`
- `x86_64-linux-android`
- `x86_64-pc-windows-msvc`
- `x86_64-unknown-linux-gnu`
- `x86_64-unknown-linux-musl`

## Contribution

Expand Down
119 changes: 67 additions & 52 deletions crash-context/src/linux/getcontext/x86.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,68 @@
#[cfg(target_os = "android")]
compile_error!("please file an issue if you care about this target");

std::arch::global_asm! {
".text",
".global crash_context_getcontext",
".hidden crash_context_getcontext",
".align 4",
".type crash_context_getcontext, @function",
"crash_context_getcontext:",
"movl 4(%esp), %eax", // eax = uc

// Save register values
"movl %ecx, 0x3c(%eax)",
"movl %edx, 0x38(%eax)",
"movl %ebx, 0x34(%eax)",
"movl %edi, 0x24(%eax)",
"movl %esi, 0x28(%eax)",
"movl %ebp, 0x2c(%eax)",

"movl (%esp), %edx", /* return address */
"lea 4(%esp), %ecx", /* exclude return address from stack */
"mov %edx, 0x4c(%eax)",
"mov %ecx, 0x30(%eax)",

"xorl %ecx, %ecx",
"movw %fs, %cx",
"mov %ecx, 0x18(%eax)",

"movl $0, 0x40(%eax)",

// Save floating point state to fpregstate, then update
// the fpregs pointer to point to it
"leal 0xec(%eax), %ecx",
"fnstenv (%ecx)",
"fldenv (%ecx)",
"mov %ecx, 0x60(%eax)",

// Save signal mask: sigprocmask(SIGBLOCK, NULL, &uc->uc_sigmask)
"leal 0x6c(%eax), %edx",
"xorl %ecx, %ecx",
"push %edx", /* &uc->uc_sigmask */
"push %ecx", /* NULL */
"push %ecx", /* SIGBLOCK == 0 on i386 */
"call sigprocmask@PLT",
"addl $12, %esp",

"movl $0, %eax",
"ret",

".size crash_context_getcontext, . - crash_context_getcontext",
options(att_syntax)
// Unfortunately, the asm! macro has a few really annoying limitations at the
// moment
//
// 1. const operands are unstable
// 2. cfg attributes can't be used inside the asm macro at all
//
// and the worst part is we need it for literally only 1 thing, using a different
// offset to the fpstate in ucontext depending on whether we are targeting android
// or not :(
macro_rules! asm_func {
($offset:expr) => {
std::arch::global_asm! {
".text",
".global crash_context_getcontext",
".hidden crash_context_getcontext",
".align 4",
".type crash_context_getcontext, @function",
"crash_context_getcontext:",
"movl 4(%esp), %eax", // eax = uc

// Save register values
"movl %ecx, 0x3c(%eax)",
"movl %edx, 0x38(%eax)",
"movl %ebx, 0x34(%eax)",
"movl %edi, 0x24(%eax)",
"movl %esi, 0x28(%eax)",
"movl %ebp, 0x2c(%eax)",

"movl (%esp), %edx", /* return address */
"lea 4(%esp), %ecx", /* exclude return address from stack */
"mov %edx, 0x4c(%eax)",
"mov %ecx, 0x30(%eax)",

"xorl %ecx, %ecx",
"movw %fs, %cx",
"mov %ecx, 0x18(%eax)",

"movl $0, 0x40(%eax)",

// Save floating point state to fpregstate, then update
// the fpregs pointer to point to it
stringify!(leal $offset(%eax),%ecx),
"fnstenv (%ecx)",
"fldenv (%ecx)",
"mov %ecx, 0x60(%eax)",

// Save signal mask: sigprocmask(SIGBLOCK, NULL, &uc->uc_sigmask)
"leal 0x6c(%eax), %edx",
"xorl %ecx, %ecx",
"push %edx", /* &uc->uc_sigmask */
"push %ecx", /* NULL */
"push %ecx", /* SIGBLOCK == 0 on i386 */
"call sigprocmask@PLT",
"addl $12, %esp",

"movl $0, %eax",
"ret",

".size crash_context_getcontext, . - crash_context_getcontext",
options(att_syntax)
}
};
}

#[cfg(target_os = "linux")]
asm_func!(0xec);
#[cfg(target_os = "android")]
asm_func!(0x74);
2 changes: 1 addition & 1 deletion crash-context/src/linux/getcontext/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
// adding certain lines/blocks of asm based using cfg https://github.com/rust-lang/rust/issues/15701
// and they're not really inputs, just literals, so...yah

#[cfg(target_os = "linux")]
#[cfg(any(target_os = "linux", target_os = "android"))]

// Unfortunately, the asm! macro has a few really annoying limitations at the
// moment
Expand Down