简体   繁体   中英

Segmentation fault in JNI native code (Android)

I have a library in Haskell that I have generated wrappers for via FFI and Swig. I have also managed to cross-compile a shared library that I can load from Java. The wrapped library contains some functions and input/output types for these.

On my Arch machine I am able to use my library from Java. However, when I now tried it out on Android, it segfaults. After some investigation, It seems that if the input object has no fields, it sometimes work. I think it's a problem with data marshalling. However it's hard to debug. In the crash log I find this

W/linker  ( 4783): libAPIsJava.so has text relocations. This is wasting memory and prevents security hardening. Please fix.

and this

--------- beginning of crash
F/libc    ( 4783): Fatal signal 11 (SIGSEGV), code 1, fault addr 0x200019 in tid 4783 (api.flic.io.api)
I/Icing   ( 1689): Indexing 17F83E8EEF17AFFA030207AF16B79084CE236092 from com.google.android.googlequicksearchbox
I/DEBUG   (  357): *** *** *** *** *** *** *** *** *** *** *** *** ***     *** *** ***
I/DEBUG   (  357): Build fingerprint:     'google/shamu/shamu:5.1/LMY47D/1743759:user/release-keys'
I/DEBUG   (  357): Revision: '33696'
I/DEBUG   (  357): ABI: 'arm'
I/DEBUG   (  357): pid: 4783, tid: 4783, name: api.flic.io.api  >>> app.api.flic.io.api <<<
I/DEBUG   (  357): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x200019
I/DEBUG   (  357):     r0 af2033d4  r1 af2033d0  r2 00000000  r3 00200018
I/DEBUG   (  357):     r4 a2bbf950  r5 af2033dc  r6 af2029a0  r7 af202fff
I/DEBUG   (  357):     r8 a2af06b0  r9 00200019  sl af2033c8  fp af2030a4
I/DEBUG   (  357):     ip b6e627dc  sp bec8eee0  lr af2029b0  pc a0b4d248  cpsr 800d0010
I/DEBUG   (  357): 
I/DEBUG   (  357): backtrace:
I/DEBUG   (  357):     #00 pc 01be4248  /data/app/app.api.flic.io.api-2/lib/arm/libAPIsJava.so

However, Issuing arm-linux-androideabi-addr2line -f -e libAPIsJava.so 01be4248, the output is

$a
/tmp/ghc27404_0/ghc27404_3.bc:? 

Which is not of much help. What I would really like is to run the app with valgrind, is something like that possible?

Update: I have now installed valgrind and tried to run the app. However I get

valgrind: mmap(0x108000, 42770432) failed in UME with error 22 (Invalid argument).
valgrind: this can be caused by executables with very large text, data or bss segments.

Which seems to be a no-go.

It turned out the problem was that my cross compile toolchain ( https://github.com/neurocyte/ghc-android ) has a bug in its hsc2hs script. It's implemented as a wrapper that calls /usr/bin/hsc2hs with some flags. However, it wasn't using the --cc and --ld flag, so the resulting .hs file targeted my build host. By adding these flags, everything works great!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM