termux / termux-packages

A package build system for Termux.
https://termux.dev
Other
13.41k stars 3.09k forks source link

[Bug]: kotlin: kotlinc,kotlin etc shows segmentation fault error #10851

Closed PiprTuff closed 2 years ago

PiprTuff commented 2 years ago

Problem description

Trying to run any of the executables provided by the Kotlin package shows up the error:

/data/data/com.termux/files/usr/bin/kotlinc: line 98:  3533 Segmentation fault      "${JAVACMD:=java}" $JAVA_OPTS "${java_args[@]}" -cp "${kotlin_app[@]}" "${kotlin_args[@]}"

What steps will reproduce the bug?

  1. pkg i kotlin -y
  2. kotlinc Main.kt or kotlin or run any other binary.

What is the expected behavior?

The Kotlin compiler, and its dependent executables should operate properly.

System information

termux-info:

Termux Variables:
TERMUX_APK_RELEASE=F_DROID
TERMUX_APP_PACKAGE_MANAGER=apt
TERMUX_APP_PID=18953
TERMUX_IS_DEBUGGABLE_BUILD=0
TERMUX_MAIN_PACKAGE_FORMAT=debian
TERMUX_VERSION=0.118.0
Packages CPU architecture:
arm
Subscribed repositories:
# sources.list
deb https://grimler.se/termux-packages-24 stable main
# x11-repo (sources.list.d/x11.list)
deb https://grimler.se/x11-packages x11 main
Updatable packages:
All packages up to date
termux-tools version:
0.184
Android version:
10
Kernel build information:
Linux localhost 4.9.193-perf-g743cb02 #1 SMP PREEMPT Mon Oct 25 17:15:39 CST 2021 armv7l Android
Device manufacturer:
Xiaomi
Device model:
Redmi 8A
MrAdityaAlok commented 2 years ago

Please provide some logs. You may use strace kotlinc Main.kt.

PiprTuff commented 2 years ago

That's to big. Here you go: https://pastebin.pl/view/raw/26981463

Grimler91 commented 2 years ago

Adding a debug print to $PREFIX/bin/kotlinc shows that it runs:

/data/data/com.termux/files/usr/opt/openjdk/bin/java -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main

Running that command under gdb gives:

$ gdb -ex r --args /data/data/com.termux/files/usr/opt/openjdk/bin/java -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main
GNU gdb (GDB) 10.1
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "aarch64-linux-android".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /data/data/com.termux/files/usr/opt/openjdk/bin/java...
(No debugging symbols found in /data/data/com.termux/files/usr/opt/openjdk/bin/java)
Starting program: /data/data/com.termux/files/usr/opt/openjdk/bin/java -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main
[New LWP 28935]
[New LWP 28936]
[New LWP 28937]
[New LWP 28938]
[New LWP 28939]
[New LWP 28940]
[New LWP 28941]
[New LWP 28942]
[New LWP 28943]
[New LWP 28944]
[New LWP 28945]
[New LWP 28946]
[New LWP 28947]
[New LWP 28948]
[New LWP 28949]
[New LWP 28950]
[New LWP 28951]
[New LWP 28952]

Thread 2 "java" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 28935]
0x0000007d25ead844 in jni_SetStaticBooleanField () from /data/data/com.termux/files/usr/opt/openjdk/lib/server/libjvm.so
(gdb) bt
#0  0x0000007d25ead844 in jni_SetStaticBooleanField () from /data/data/com.termux/files/usr/opt/openjdk/lib/server/libjvm.so
#1  0x0000007cf5eb5814 in Java_org_fusesource_jansi_internal_CLibrary_init () from /data/data/com.termux/files/usr/lib/libjansi.so
#2  0x0000007d0d79daac in ?? ()
#3  0x00000000f18540d0 in ?? ()
Backtrace stopped: not enough registers or memory available to unwind further
PiprTuff commented 2 years ago

For me, after I prefixed the kotlinc script's last line with echo, it gives me this output which is not same to yours:

/data/data/com.termux/files/usr/opt/openjdk/bin/java -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-preloader.jar org.jetbrains.kotlin.preloading.Preloader -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-compiler.jar org.jetbrains.kotlin.cli.jvm.K2JVMCompiler

However I can compile and run simple Kotlin application using Gradle without any error.

Gradle version 7.4.2, Kotlin JVM plugin version 1.6.21.

Grimler91 commented 2 years ago

For me, after I prefixed the kotlinc script's last line with echo, it gives me this output which is not same to yours:

My output was from just running kotlin, I suppose yours is from kotlinc Main.kt or similar since it references org.jetbrains.kotlin.cli.jvm.K2JVMCompiler (instead of org.jetbrains.kotlin.runner.Main in my case)

MrAdityaAlok commented 2 years ago

On my aarch64 device it appears randomly: (but I never encountered with kotlinc Main.kt)

Most of the times it segfaults after repl is launched correctly and then some command is run (say println("hello")), with following output:

(Copyright notice is removed)

❯ gdb -ex r --args /data/data/com.termux/files/usr/opt/openjdk/bin/java -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main

Starting program: /data/data/com.termux/files/usr/opt/openjdk/bin/java -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main
warning: .dynamic section for "/data/data/com.termux/files/usr/opt/openjdk/bin/java" is not at the expected address (wrong library or version mismatch?)
[New LWP 927]
[New LWP 928]
[New LWP 929]
[New LWP 930]
[New LWP 931]
[New LWP 932]
[New LWP 933]
[New LWP 939]
[New LWP 945]
[New LWP 946]
[New LWP 947]
[New LWP 948]
[New LWP 949]
[New LWP 950]
[New LWP 951]
[New LWP 952]
[New LWP 953]
[New LWP 954]
[New LWP 955]
[New LWP 956]
[New LWP 957]
[New LWP 958]
[New LWP 964]
[New LWP 965]
[New LWP 966]
[New LWP 967]
[Detaching after vfork from child process 969]
[New LWP 970]
[Detaching after vfork from child process 971]
[Detaching after vfork from child process 972]
[New LWP 973]
Welcome to Kotlin version 1.6.21 (JRE 17-internal+0-adhoc..src)
Type :help for help, :quit for quit
[Detaching after vfork from child process 974]
[Detaching after vfork from child process 975]
[Detaching after vfork from child process 976]
[Detaching after vfork from child process 977]
>>> [New LWP 978]

[Detaching after vfork from child process 984]
[Detaching after vfork from child process 985]
[LWP 973 exited]
[Detaching after vfork from child process 990]
[Detaching after vfork from child process 991]
[Detaching after vfork from child process 992]
[Detaching after vfork from child process 994]
>>> println("hello")
[Detaching after vfork from child process 1061]
[Detaching after vfork from child process 1062]
--Type <RET> for more, q to quit, c to continue without paging--

Thread 2 "java" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 927]
0x0000007fa6c5c450 in ?? ()
(gdb) bt
#0  0x0000007fa6c5bed0 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb)

Other times it segfaults without even launching repl with following backtrace:

Thread 2 "java" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 1463]
0x0000007f9f957aa0 in ?? ()
(gdb) bt
#0  0x0000007f9f957aa0 in ?? ()
#1  0x00000000f00ce268 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

When run through lldb:

❯ lldb /data/data/com.termux/files/usr/opt/openjdk/bin/java -- -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main

(lldb) target create "/data/data/com.termux/files/usr/opt/openjdk/bin/java"
Current executable set to '/data/data/com.termux/files/usr/opt/openjdk/bin/java' (aarch64).
(lldb) settings set -- target.run-args  "-Xmx256M" "-Xms32M" "--add-opens" "java.base/java.util=ALL-UNNAMED" "-Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin" "-cp" "/data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar" "org.jetbrains.kotlin.runner.Main"
(lldb) run
Process 32033 launched: '/data/data/com.termux/files/usr/opt/openjdk/bin/java' (aarch64)
Process 32033 stopped and restarted: thread 2 received signal: SIGCHLD
Process 32033 stopped and restarted: thread 2 received signal: SIGCHLD
Process 32033 stopped and restarted: thread 2 received signal: SIGCHLD
Welcome to Kotlin version 1.6.21 (JRE 17-internal+0-adhoc..src)
Type :help for help, :quit for quit
Process 32033 stopped and restarted: thread 2 received signal: SIGCHLD
Process 32033 stopped and restarted: thread 2 received signal: SIGCHLD
Process 32033 stopped and restarted: thread 2 received signal: SIGCHLD
Process 32033 stopped and restarted: thread 2 received signal: SIGCHLD
>>> print("hi")Process 32033 stopped and restarted: thread 2 received signal: SIGCHLD
Process 32033 stopped and restarted: thread 2 received signal: SIGCHLD
Process 32033 stopped
* thread #2, name = 'java', stop reason = signal SIGSEGV: invalid address (fault address: 0x8)
    frame #0: 0x0000007fa6c5be50
->  0x7fa6c5be50: ldr    w13, [x2, #0x8]
    0x7fa6c5be54: lsl    x10, x13, #3
    0x7fa6c5be58: ldr    x10, [x10, #0x1e8]
    0x7fa6c5be5c: mov    x15, x11
(lldb) bt
* thread #2, name = 'java', stop reason = signal SIGSEGV: invalid address (fault address: 0x8)
  * frame #0: 0x0000007fa6c5be50
    frame #1: 0x0000007f9f14de68
    frame #2: 0x0000007f9f14dfc8
    frame #3: 0x0000007f9f14dd80
    frame #4: 0x0000007f9f14dd80
    frame #5: 0x0000007f9f14dd80
    frame #6: 0x0000007f9f14dd80
    frame #7: 0x0000007f9f14dd80
    frame #8: 0x0000007f9f14dd80
    frame #9: 0x0000007f9f14dd80
    frame #10: 0x0000007f9f14e2c0
    frame #11: 0x0000007f9f14dd80
    frame #12: 0x0000007f9f14dd80
    frame #13: 0x0000007f9f14dd80
    frame #14: 0x0000007f9f14dd80
    frame #15: 0x0000007f9f14dd80
    frame #16: 0x0000007f9f14dd80
    frame #17: 0x0000007f9f14dd80
    frame #18: 0x0000007f9f14dd80
    frame #19: 0x0000007f9f14dd80
    frame #20: 0x0000007f9f14dd80
    frame #21: 0x0000007f9f14e2c0
    frame #22: 0x0000007f9f14dd80
    frame #23: 0x0000007f9f14e2c0
    frame #24: 0x0000007f9f14dd80
    frame #25: 0x0000007f9f14dd80
    frame #26: 0x0000007f9f14e2c0
    frame #27: 0x0000007f9f14dd80
    frame #28: 0x0000007f9f14dd80
    frame #29: 0x0000007f9f14dd80
    frame #30: 0x0000007f9f14dd80
    frame #31: 0x0000007f9f14e2c0
    frame #32: 0x0000007f9f14dd80
    frame #33: 0x0000007f9f14e2c0
    frame #34: 0x0000007f9f14dd80
    frame #35: 0x0000007f9f14dd80
    frame #36: 0x0000007f9f14e2c0
    frame #37: 0x0000007f9f14dd80
    frame #38: 0x0000007f9f14dd80
    frame #39: 0x0000007f9f14dfc8
    frame #40: 0x0000007f9f14dfc8
    frame #41: 0x0000007f9f14dfc8
    frame #42: 0x0000007f9f14dfc8
    frame #43: 0x0000007f9f14dd80
    frame #44: 0x0000007f9f14dd80
    frame #45: 0x0000007f9f14dd80
    frame #46: 0x0000007f9f14dd80
    frame #47: 0x0000007f9f14dd80
    frame #48: 0x0000007f9f14e2c0
    frame #49: 0x0000007f9f14e2c0
    frame #50: 0x0000007f9f14dd80
    frame #51: 0x0000007f9f14dd80
    frame #52: 0x0000007f9f14dd80
    frame #53: 0x0000007f9f14dfc8
    frame #54: 0x0000007f9f14dfc8
    frame #55: 0x0000007f9f14dfc8
    frame #56: 0x0000007f9f14e2c0
    frame #57: 0x0000007f9f14dd80
    frame #58: 0x0000007f9f14dd80
    frame #59: 0x0000007f9f14dd80
    frame #60: 0x0000007f9f14dd80
    frame #61: 0x0000007f9f14dd80
    frame #62: 0x0000007f9f14dd80
    frame #63: 0x0000007f9f14dd80
    frame #64: 0x0000007f9f14dd80
    frame #65: 0x0000007f9f14dd80
    frame #66: 0x0000007f9f14dfc8
    frame #67: 0x0000007f9f14dfc8
    frame #68: 0x0000007f9f148140
    frame #69: 0x0000007fb6a7efac libjvm.so`JavaCalls::call_helper(JavaValue*, methodHandle const&, JavaCallArguments*, JavaThread*) + 852
    frame #70: 0x0000007fb6e20c1c libjvm.so`invoke(InstanceKlass*, methodHandle const&, Handle, bool, objArrayHandle, BasicType, objArrayHandle, bool, JavaThread*) + 2364
    frame #71: 0x0000007fb6e20260 libjvm.so`Reflection::invoke_method(oopDesc*, Handle, objArrayHandle, JavaThread*) + 336
    frame #72: 0x0000007fb6b28c78 libjvm.so`JVM_InvokeMethod + 832
    frame #73: 0x0000007f9f151aac
    frame #74: 0x0000007f9f14dd80
    frame #75: 0x0000007f9f14dd80
    frame #76: 0x0000007f9f14e2c0
    frame #77: 0x0000007f9f14dd80
    frame #78: 0x0000007f9f14dfc8
    frame #79: 0x0000007f9f14e508
    frame #80: 0x0000007f9f14dfc8
    frame #81: 0x0000007f9f148140
    frame #82: 0x0000007fb6a7efac libjvm.so`JavaCalls::call_helper(JavaValue*, methodHandle const&, JavaCallArguments*, JavaThread*) + 852
    frame #83: 0x0000007fb6adf854 libjvm.so`jni_invoke_static(JNIEnv_*, JavaValue*, _jobject*, JNICallType, _jmethodID*, JNI_ArgumentPusher*, JavaThread*) + 268
    frame #84: 0x0000007fb6ae2a90 libjvm.so`jni_CallStaticVoidMethod + 320
    frame #85: 0x0000007fb7c67338 libjli.so`JavaMain + 3588
    frame #86: 0x0000007fb7c6902c libjli.so`ThreadJavaMain + 12
    frame #87: 0x0000007fb7aedfb0 libc.so`__pthread_start(void*) + 40
    frame #88: 0x0000007fb7a7f96c libc.so`__start_thread + 72
(lldb)
Grimler91 commented 2 years ago

Maybe/hopefully we'll get a better backtrace with a debug build of openjdk-17. I'll see if its possible to build a debug variant

thunder-coding commented 2 years ago

Maybe/hopefully we'll get a better backtrace with a debug build of openjdk-17. I'll see if its possible to build a debug variant

I would also like to suggest running Java with AddressSanitizer

Just do:-

LD_PRELOAD=$PREFIX/lib/clang/14.0.3/lib/linux/libclang_rt.asan-aarch64-android.so <command>

Make sure you replace aarch64 with your architecture

Grimler91 commented 2 years ago

Thank you both @MrAdityaAlok and @thunder-coding, I'm learning how to use both lldb and the address sanitizer from this thread :+1:

with address sanitizer I get:

$ LD_PRELOAD=$PREFIX/lib/clang/14.0.3/lib/linux/libclang_rt.asan-aarch64-android.so /data/data/com.termux/files/usr/opt/openjdk/bin/java -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main
AddressSanitizer:DEADLYSIGNAL
=================================================================
==8129==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x005a5e425844 bp 0x005a5da39400 sp 0x005a5da393d0 T1)
==8129==The signal is caused by a READ memory access.
==8129==Hint: address points to the zero page.
    #0 0x5a5e425844 in jni_SetStaticBooleanField (/data/data/com.termux/files/usr/opt/openjdk/lib/server/libjvm.so+0x9e5844)
    #1 0x5a2e2b0810 in Java_org_fusesource_jansi_internal_CLibrary_init (/data/data/com.termux/files/usr/lib/libjansi.so+0x2810)
    #2 0x5a45cd1aa8  (<unknown module>)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/data/data/com.termux/files/usr/opt/openjdk/lib/server/libjvm.so+0x9e5844) in jni_SetStaticBooleanField
Thread T1 created by T0 here:
    #0 0x7ae03eef44 in pthread_create (/data/data/com.termux/files/usr/lib/clang/14.0.3/lib/linux/libclang_rt.asan-aarch64-android.so+0xadf44)
    #1 0x7ae0e9dfc4 in CallJavaMainInNewThread (/data/data/com.termux/files/usr/opt/openjdk/lib/libjli.so+0xafc4)
    #2 0x7ae0e9d330 in ContinueInNewThread (/data/data/com.termux/files/usr/opt/openjdk/lib/libjli.so+0xa330)
    #3 0x7ae0e9b04c in JLI_Launch (/data/data/com.termux/files/usr/opt/openjdk/lib/libjli.so+0x804c)
    #4 0x589b6e0c5c in main (/data/data/com.termux/files/usr/opt/openjdk/bin/java+0x1c5c)
    #5 0x7ae0de888c in __libc_init (/apex/com.android.runtime/lib64/bionic/libc.so+0x4988c) (BuildId: be0bc8debb3a18cdf3e17af4511f1254)
    #6 0x589b6e0a70 in _start_main (/data/data/com.termux/files/usr/opt/openjdk/bin/java+0x1a70)

==8129==ABORTING
Aborted

Just as with gdb and lldb I'm hoping 0x5a45cd1aa8 (<unknown module>) can get resolved to something with a debug build of openjdk-17 so we better see what is going on

Grimler91 commented 2 years ago

Debug build of openjdk in openjdk-debug branch gets quite far but then fails with:

/usr/bin/objcopy: Unable to recognise the format of the input file `/home/builder/.termux-build/openjdk-17/src/build/linux-aarch64-server-fastdebug/support/modules_libs/java.base/server/libjvm.so'
gmake[3]: *** [lib/CompileJvm.gmk:144: /home/builder/.termux-build/openjdk-17/src/build/linux-aarch64-server-fastdebug/support/modules_libs/java.base/server/libjvm.so] Error 1
gmake[3]: *** Deleting file '/home/builder/.termux-build/openjdk-17/src/build/linux-aarch64-server-fastdebug/support/modules_libs/java.base/server/libjvm.so'
gmake[2]: *** [make/Main.gmk:252: hotspot-server-libs] Error 2

ERROR: Build failed for target 'images' in configuration 'linux-aarch64-server-fastdebug' (exit code 2) 

No indication of failed target found.
Hint: Try searching the build log for '] Error'.
Hint: See doc/building.html#troubleshooting for assistance.

make[1]: *** [/home/builder/.termux-build/openjdk-17/src/make/Init.gmk:315: main] Error 2
make: *** [/home/builder/.termux-build/openjdk-17/src/make/Init.gmk:186: images] Error 2
MrAdityaAlok commented 2 years ago

with address sanitizer I get:


$ LD_PRELOAD=$PREFIX/lib/clang/14.0.3/lib/linux/libclang_rt.asan-aarch64-android.so /data/data/com.termux/files/usr/opt/openjdk/bin/java -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main
AddressSanitizer:DEADLYSIGNAL
=================================================================
==8129==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x005a5e425844 bp 0x005a5da39400 sp 0x005a5da393d0 T1)
==8129==The signal is caused by a READ memory access.
==8129==Hint: address points to the zero page.
    #0 0x5a5e425844 in jni_SetStaticBooleanField (/data/data/com.termux/files/usr/opt/openjdk/lib/server/libjvm.so+0x9e5844)
    #1 0x5a2e2b0810 in Java_org_fusesource_jansi_internal_CLibrary_init (/data/data/com.termux/files/usr/lib/libjansi.so+0x2810)
    #2 0x5a45cd1aa8  (<unknown module>)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/data/data/com.termux/files/usr/opt/openjdk/lib/server/libjvm.so+0x9e5844) in jni_SetStaticBooleanField
Thread T1 created by T0 here:
    #0 0x7ae03eef44 in pthread_create (/data/data/com.termux/files/usr/lib/clang/14.0.3/lib/linux/libclang_rt.asan-aarch64-android.so+0xadf44)
    #1 0x7ae0e9dfc4 in CallJavaMainInNewThread (/data/data/com.termux/files/usr/opt/openjdk/lib/libjli.so+0xafc4)
    #2 0x7ae0e9d330 in ContinueInNewThread (/data/data/com.termux/files/usr/opt/openjdk/lib/libjli.so+0xa330)
    #3 0x7ae0e9b04c in JLI_Launch (/data/data/com.termux/files/usr/opt/openjdk/lib/libjli.so+0x804c)
    #4 0x589b6e0c5c in main (/data/data/com.termux/files/usr/opt/openjdk/bin/java+0x1c5c)
    #5 0x7ae0de888c in __libc_init (/apex/com.android.runtime/lib64/bionic/libc.so+0x4988c) (BuildId: be0bc8debb3a18cdf3e17af4511f1254)
    #6 0x589b6e0a70 in _start_main (/data/data/com.termux/files/usr/opt/openjdk/bin/java+0x1a70)

==8129==ABORTING
Aborted

Strangely no such thing happen on my device:

❯ LD_PRELOAD=$PREFIX/lib/clang/14.0.3/lib/linux/libclang_rt.asan-aarch64-android.so /data/data/com.termux/files/usr/opt/openjdk/bin/java -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main
Welcome to Kotlin version 1.6.21 (JRE 17-internal+0-adhoc..src)
Type :help for help, :quit for quit
>>> println("hello")
println("hello")
hello
>>> >>> ⏎
thunder-coding commented 2 years ago

It seems like the issue exists on only certain Android version. I couldn't reproduce the issue on both my "aarch64 MiA3 running Android 11" and "arm Moto G4 running Android 7"

Grimler91 commented 2 years ago

It seems like the issue exists on only certain Android version

Strangely no such thing happen on my device

Different behaviour on different devices can happen with segfaults, if underlying issue causes undefined behaviour then the program might still work fine, or might crash, or might cause demons to fly out of your nose

PiprTuff commented 2 years ago

Hello @Grimler91, seems like I've solved the mystery. From this trace:

Thank you both @MrAdityaAlok and @thunder-coding, I'm learning how to use both lldb and the address sanitizer from this thread 👍

with address sanitizer I get:

$ LD_PRELOAD=$PREFIX/lib/clang/14.0.3/lib/linux/libclang_rt.asan-aarch64-android.so /data/data/com.termux/files/usr/opt/openjdk/bin/java -Xmx256M -Xms32M --add-opens java.base/java.util=ALL-UNNAMED -Dkotlin.home=/data/data/com.termux/files/usr/opt/kotlin -cp /data/data/com.termux/files/usr/opt/kotlin/lib/kotlin-runner.jar org.jetbrains.kotlin.runner.Main
AddressSanitizer:DEADLYSIGNAL
=================================================================
==8129==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x005a5e425844 bp 0x005a5da39400 sp 0x005a5da393d0 T1)
==8129==The signal is caused by a READ memory access.
==8129==Hint: address points to the zero page.
    #0 0x5a5e425844 in jni_SetStaticBooleanField (/data/data/com.termux/files/usr/opt/openjdk/lib/server/libjvm.so+0x9e5844)
    #1 0x5a2e2b0810 in Java_org_fusesource_jansi_internal_CLibrary_init (/data/data/com.termux/files/usr/lib/libjansi.so+0x2810)
    #2 0x5a45cd1aa8  (<unknown module>)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/data/data/com.termux/files/usr/opt/openjdk/lib/server/libjvm.so+0x9e5844) in jni_SetStaticBooleanField
Thread T1 created by T0 here:
    #0 0x7ae03eef44 in pthread_create (/data/data/com.termux/files/usr/lib/clang/14.0.3/lib/linux/libclang_rt.asan-aarch64-android.so+0xadf44)
    #1 0x7ae0e9dfc4 in CallJavaMainInNewThread (/data/data/com.termux/files/usr/opt/openjdk/lib/libjli.so+0xafc4)
    #2 0x7ae0e9d330 in ContinueInNewThread (/data/data/com.termux/files/usr/opt/openjdk/lib/libjli.so+0xa330)
    #3 0x7ae0e9b04c in JLI_Launch (/data/data/com.termux/files/usr/opt/openjdk/lib/libjli.so+0x804c)
    #4 0x589b6e0c5c in main (/data/data/com.termux/files/usr/opt/openjdk/bin/java+0x1c5c)
    #5 0x7ae0de888c in __libc_init (/apex/com.android.runtime/lib64/bionic/libc.so+0x4988c) (BuildId: be0bc8debb3a18cdf3e17af4511f1254)
    #6 0x589b6e0a70 in _start_main (/data/data/com.termux/files/usr/opt/openjdk/bin/java+0x1a70)

==8129==ABORTING
Aborted

I see a line (8th line) that refers to the libjansi.so JNI shared library, which is a helper for Java's Jansi library that provides getline like utility to REPL like Java applications (here kotlinc), leveraging native access to the terminal.

Although kotlinc bundles its own libjansi.so files in a jar, but those are not compatible with Android architectures, so it tries to load the library from the runtime environment's library path (here $PREFIX/lib) instead.

And you may also know, the package maven also depends on the Jansi library, so someone had slightly patched the libjansi.so and made it a dependency of maven, now it is also installed at the runtime library path whenever someone installs maven.

So, kotlinc finds and dynamically loads that library successfully, but when invokes any of native method which returns something that does not exist in the patched library, it crashes.

Notice that, the previous revisions of the openjdk-17 package had wrong runtime library path set (/usr/lib instead of $PREFIX/lib), so kotlinccouldn't load the native library, and silently ignored it. But that hard-coded bug issue was fixed in the latest revision, since then kotlinc is crashing if you also have maven installed.

stale[bot] commented 2 years ago

This issue/PR has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.