I am developing Android application to work with a network printers. Atol printers have their own native library (*.so) for printing text. Under certain circumstances (for example, when specifying an incorrect ip-address), this library throws native exceptions and my app crashes even when my code is wrapped in try { } catch (t: Throwable) { }
!
The following information is displayed in the logcat:
Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x630000005d in tid 29523 (RxCachedThreadS), pid 27187 (my.app.package)
How can I handle this native exception in my Kotlin code?
This is my full logcat message:
#00 pc 0000000000654930 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/lib/arm64/libfptr10.so (BuildId: 796f36babce130ad8c1d171dab3c2c061ec463bb)
#01 pc 00000000003f8fb4 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/lib/arm64/libfptr10.so (Fptr10::Ports::TcpPort::tag() const+20) (BuildId: 796f36babce130ad8c1d171dab3c2c061ec463bb)
#02 pc 0000000000453ad0 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/lib/arm64/libfptr10.so (Fptr10::Ports::PosixFastTcpPort::open()+4280) (BuildId: 796f36babce130ad8c1d171dab3c2c061ec463bb)
#03 pc 00000000002d7958 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/lib/arm64/libfptr10.so (Fptr10::FiscalPrinter::Atol::AtolProtocolDetector::open()+1600) (BuildId: 796f36babce130ad8c1d171dab3c2c061ec463bb)
#04 pc 000000000026ae60 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/lib/arm64/libfptr10.so (Fptr10::FiscalPrinter::FiscalPrinterHandle::open()+84) (BuildId: 796f36babce130ad8c1d171dab3c2c061ec463bb)
#05 pc 000000000021cb9c /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/lib/arm64/libfptr10.so (doOpen(void*)+36) (BuildId: 796f36babce130ad8c1d171dab3c2c061ec463bb)
#06 pc 00000000002208b4 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/lib/arm64/libfptr10.so (libfptr_open+200) (BuildId: 796f36babce130ad8c1d171dab3c2c061ec463bb)
#07 pc 0000000000351230 /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+144) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#08 pc 000000000033a7a4 /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+612) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#09 pc 0000000000510a3c /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+1976) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#10 pc 00000000004904b8 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+1012) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#11 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#12 pc 0000000001b23090 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#13 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#14 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#15 pc 00000000004904b8 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+1012) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#16 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#17 pc 0000000000a87dfc /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#18 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#19 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#20 pc 0000000000491368 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+4772) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#21 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#22 pc 00000000004a6534 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#23 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#24 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#25 pc 0000000000490828 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+1892) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#26 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#27 pc 0000000000a62bb0 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#28 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#29 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#30 pc 0000000000490828 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+1892) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#31 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#32 pc 0000000000a62b94 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#33 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#34 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#35 pc 0000000000490828 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+1892) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#36 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#37 pc 0000000000a62b58 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#38 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#39 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#40 pc 0000000000491368 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+4772) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#41 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#42 pc 000000000181908c /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#43 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#44 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#45 pc 00000000004904b8 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+1012) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#46 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#47 pc 0000000000488de8 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#48 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#49 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#50 pc 0000000000491368 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+4772) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#51 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#52 pc 0000000001825c2c /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#53 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#54 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#55 pc 00000000004904b8 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+1012) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#56 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#57 pc 0000000000488de8 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#58 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#59 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#60 pc 0000000000491368 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+4772) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#61 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#62 pc 0000000001823288 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#63 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#64 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#65 pc 0000000000491368 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+4772) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#66 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#67 pc 0000000000488ef8 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#68 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#69 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#70 pc 0000000000491368 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+4772) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#71 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#72 pc 0000000001835688 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#73 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#74 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#75 pc 00000000004904b8 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+1012) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#76 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#77 pc 0000000001835468 /data/app/~~M6zM9JK5PqEGK-hMsQq8qw==/my.app.package-4mOPtsiz1G0iT3vRcop3Vw==/oat/arm64/base.vdex
#78 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#79 pc 0000000000511708 /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>(art::ArtMethod*, art::Thread*, art::ShadowFrame&, art::Instruction const*, unsigned short, bool, art::JValue*)+5252) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#80 pc 0000000000491368 /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp<false>(art::interpreter::SwitchImplContext*)+4772) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#81 pc 00000000003539d8 /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+8) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#82 pc 000000000023a008 /apex/com.android.art/javalib/core-oj.jar
#83 pc 000000000036daec /apex/com.android.art/lib64/libart.so (art::interpreter::Execute(art::Thread*, art::CodeItemDataAccessor const&, art::ShadowFrame&, art::JValue, bool, bool) (.__uniq.112435418011751916792819755956732575238.llvm.18012138979834229341)+232) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#84 pc 000000000036d3e4 /apex/com.android.art/lib64/libart.so (artQuickToInterpreterBridge+964) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#85 pc 0000000000351368 /apex/com.android.art/lib64/libart.so (art_quick_to_interpreter_bridge+88) (BuildId: 2452917c4ff69cbb6e75e5512260946b)
#86 pc 000000000201789c /memfd:jit-cache (deleted)
You can't. Native crashes like that aren't exceptions, and don't generate or pass back an exception to Java. In particular SIGSEGV is unrecoverable. It means the application attempted to access a memory address it doesn't have access to. At that point, the assumption has to be that memory has been corrupted and that the application needs to shut down for safety. There is no way to override that behavior.