Repository: rust
Issue: StackOverflow error when assert fails or panic when compiling to Android
Labels: O-android,A-FFI,O-x86_64,A-cross,T-compiler,C-bug,O-unix,A-panic

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.
-->

When I compile Rust Library to Android (using [this](https://mozilla.github.io/firefox-browser-architecture/experiments/2017-09-21-rust-on-android.html) or [this](https://fernandocejas.com/blog/engineering/2023-07-27-rust-cross-platform-android) tutorial), all works fine, except if my code `panic!`, or an `assert` fails: in this case, the app crashes.

Here is example code (based on [this](https://fernandocejas.com/blog/engineering/2023-07-27-rust-cross-platform-android) tutorial, so the example function name is `Cryptor_decrypt` in `com.fernandocejas.sample` package)

```rust
#[cfg(target_os = "android")]
#[allow(non_snake_case)]
pub mod android {
    extern crate jni;

    // This is the interface to the JVM 
    // that we'll call the majority of our
    // methods on.
    // @See https://docs.rs/jni/latest/jni/
    use self::jni::JNIEnv;

    // These objects are what you should use as arguments to your 
    // native function. They carry extra lifetime information to 
    // prevent them escaping this context and getting used after 
    // being GC'd.
    use self::jni::objects::{JClass, JString};

    // This is just a pointer. We'll be returning it from our function. 
    // We can't return one of the objects with lifetime information 
    // because the lifetime checker won't let us.
    use self::jni::sys::jstring;
    #[no_mangle] // This keeps Rust from "mangling" the name so it is unique (crate).
    pub extern "system" fn Java_com_fernandocejas_rust_Cryptor_decrypt<'local>(
        mut _env: JNIEnv<'local>,
        // This is the class that owns our static method. It's not going to be used,
        // but still must be present to match the expected signature of a static
        // native method.        
        _class: JClass<'local>,
        _input: JString<'local>,
    ) -> jstring { panic!("Normal panic");}
}   
```

I expected to see this happen: the rust library would show the `panic!` output on the logcat

Instead, this happened: the panic caused assertion failure inside the panic handler, which causes the code to panic again, which triggers the assertion again, and so on until the stack overflows. you can see it in the android logcat

### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```yml
rustc 1.72.1 (d5c2e9c34 2023-09-13)
binary: rustc
commit-hash: d5c2e9c342b358556da91d61ed4133f6f50fc0c3
commit-date: 2023-09-13
host: x86_64-unknown-linux-gnu
release: 1.72.1
LLVM version: 16.0.5
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary>Android logcat coredump</summary>
<p>

```
09-29 12:21:42.042 12824 12824 F DEBUG   : Build fingerprint: 'google/sdk_gphone64_x86_64/emu64:./UPB./.:userdebug/dev-keys'
09-29 12:21:42.042 12824 12824 F DEBUG   : Revision: '0'
09-29 12:21:42.042 12824 12824 F DEBUG   : ABI: 'x86_64'
09-29 12:21:42.042 12824 12824 F DEBUG   : Timestamp: 2023-09-29 12:21:24.093060911+0300
09-29 12:21:42.042 12824 12824 F DEBUG   : Process uptime: 75s
09-29 12:21:42.042 12824 12824 F DEBUG   : Cmdline: com.fernandocejas.sample
09-29 12:21:42.042 12824 12824 F DEBUG   : pid: 12686, tid: 12686, name: ndocejas.sample  >>> com.fernandocejas.sample <<<
09-29 12:21:42.042 12824 12824 F DEBUG   : uid: 10184
09-29 12:21:42.042 12824 12824 F DEBUG   : signal 11 (SIGSEGV), code 2 (SEGV_ACCERR), fault addr 0x00007ffd6fb4efe0
09-29 12:21:42.042 12824 12824 F DEBUG   : Cause: stack pointer is not in a rw map; likely due to stack overflow.
09-29 12:21:42.042 12824 12824 F DEBUG   :     rax 000000000000000b  rbx 00007250d1cc6610  rcx 00007ffd6fb4f0c8  rdx 00007250d1cbb8a0
09-29 12:21:42.042 12824 12824 F DEBUG   :     r8  00007250d1cbb8a0  r9  00007ffd6fb4f0d0  r10 00007ffd70334570  r11 43ceafaeb08abafe
09-29 12:21:42.042 12824 12824 F DEBUG   :     r12 00007250d1cbc810  r13 00007ffd6fb4f350  r14 00007250d1cbc428  r15 00007ffd6fb4f230
09-29 12:21:42.042 12824 12824 F DEBUG   :     rdi 0000000000000000  rsi 00007ffd6fb4f0c0
09-29 12:21:42.042 12824 12824 F DEBUG   :     rbp 00007ffd70336680  rsp 00007ffd6fb4efd0  rip 00007250d1c621c7
09-29 12:21:42.042 12824 12824 F DEBUG   : 512 total frames
09-29 12:21:42.042 12824 12824 F DEBUG   : backtrace:
09-29 12:21:42.042 12824 12824 F DEBUG   :       #00 pc 00000000009a71c7  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (core::panicking::assert_failed_inner::h9e15dd8f2a77db4c+7)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #01 pc 000000000096471a  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (core::panicking::assert_failed::h054aa0fe6cc26174+74)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #02 pc 0000000000985f2a  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (std::sys_common::thread_local_key::StaticKey::lazy_init::h5ff3cb4fbb7c6e28+170)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #03 pc 000000000099249d  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (std::panicking::panic_count::LOCAL_PANIC_COUNT::__getit::hc6f49f0edbdb42f3+45)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #04 pc 0000000000981d50  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (std::panicking::rust_panic_with_hook::hf31c0feb2c7da6b5+48)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #05 pc 0000000000981d06  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (std::panicking::begin_panic_handler::_$u7b$$u7b$closure$u7d$$u7d$::h185fdd4a7f50eafe+198)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #06 pc 0000000000980eb5  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (std::sys_common::backtrace::__rust_end_short_backtrace::h026667ccf492240b+5)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #07 pc 0000000000981a51  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (rust_begin_unwind+113)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #08 pc 00000000009a6f72  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (core::panicking::panic_fmt::h495b84bde03156a8+50)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #09 pc 00000000009a729a  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (core::panicking::assert_failed_inner::h9e15dd8f2a77db4c+218)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #10 pc 000000000096471a  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (core::panicking::assert_failed::h054aa0fe6cc26174+74)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #11 pc 0000000000985f2a  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (std::sys_common::thread_local_key::StaticKey::lazy_init::h5ff3cb4fbb7c6e28+170)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #12 pc 000000000099249d  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (std::panicking::panic_count::LOCAL_PANIC_COUNT::__getit::hc6f49f0edbdb42f3+45)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #13 pc 0000000000981d50  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (std::panicking::rust_panic_with_hook::hf31c0feb2c7da6b5+48)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #14 pc 0000000000981d06  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (std::panicking::begin_panic_handler::_$u7b$$u7b$closure$u7d$$u7d$::h185fdd4a7f50eafe+198)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #15 pc 0000000000980eb5  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (std::sys_common::backtrace::__rust_end_short_backtrace::h026667ccf492240b+5)
09-29 12:21:42.042 12824 12824 F DEBUG   :       #16 pc 0000000000981a51  /data/app/~~SJbaSy7U4-vmYK0dwRI07w==/com.fernandocejas.sample-c_MH_u093OUltZ57ZV7H6g==/base.apk (offset 0x5284000) (rust_begin_unwind+113)
...
```

</p>
</details>