oracle / graal

GraalVM compiles Java applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀
https://www.graalvm.org
Other
20.33k stars 1.63k forks source link

Cannot convert a simple SCTP example to a native-image #1569

Open mbaudier opened 5 years ago

mbaudier commented 5 years ago

Using GraalVM v19.1.1 on CentOS 7 Linux, I am trying to convert to native images this example for a simple SCTP client/server: https://www.oracle.com/technetwork/articles/javase/index-139946.html

It works with GraalVM used as a plain JVM.

The native image generation works, without warnings: /opt/graalvm/bin/native-image -cp bin test.sctp.DaytimeServer

But it fails to run, with what looks like a JNI error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: sun.nio.ch.sctp.SctpNet.socket0(Z)I [symbol: Java_sun_nio_ch_sctp_SctpNet_socket0 or Java_sun_nio_ch_sctp_SctpNet_socket0__Z]
    at com.oracle.svm.jni.access.JNINativeLinkage.getOrFindEntryPoint(JNINativeLinkage.java:145)
    at com.oracle.svm.jni.JNIGeneratedMethodSupport.nativeCallAddress(JNIGeneratedMethodSupport.java:57)
    at sun.nio.ch.sctp.SctpNet.socket0(SctpNet.java)
    at sun.nio.ch.sctp.SctpNet.socket(SctpNet.java:85)
    at sun.nio.ch.sctp.SctpServerChannelImpl.<init>(SctpServerChannelImpl.java:98)
    at com.sun.nio.sctp.SctpServerChannel.open(SctpServerChannel.java:102)
    at test.sctp.DaytimeServer.main(DaytimeServer.java:26)

I have tried various tricks, to no avail:

Since SCTP is supposed to be part of the JVM, I am not sure how I should try or analyse further. Thanks in advance for ideas.

peter-hofer commented 5 years ago

native-image currently does not have support for SCTP. I was able to get a bit further with the following steps:

  1. System.loadLibrary("sctp"); at the beginning of the main methods. The library seems to be loaded during the native image build, likely from a static initializer that is/must be executed eagerly, so it is not loaded at runtime.
  2. -H:+ReportUnsupportedElementsAtRuntime as an option to native-image so that it doesn't generate a fallback image despite no configuration.
  3. -Djava.library.path=/path/to/graalvm/jre/lib/amd64/ as a runtime argument to the native image executable so that libsctp.so can be found.
  4. env LD_LIBRARY_PATH=/path/to/graalvm/jre/lib/amd64/server/ ./daytimeserver -Djava.library.path=... so that libjvm.so can be loaded as a dependency of libsctp.so.

At that point, libsctp.so can be loaded and execution reaches SctpServerChannelImpl.bind. Then however, I get the following stack trace with DaytimeServer:

Exception in thread "main" java.net.SocketException: Invalid argument
        at com.oracle.svm.core.posix.PosixJavaNIOSubstitutions$Util_sun_nio_ch_Net.handleSocketError(PosixJavaNIOSubstitutions.java:1336)
        at sun.nio.ch.Net.bind0(Net.java:947)
        at sun.nio.ch.Net.bind(Net.java:433)
        at sun.nio.ch.Net.bind(Net.java:425)
        at sun.nio.ch.sctp.SctpServerChannelImpl.bind(SctpServerChannelImpl.java:118)
        at com.sun.nio.sctp.SctpServerChannel.bind(SctpServerChannel.java:185)
        at DaytimeServer.main(DaytimeServer.java:24)

With DaytimeClient, I get a segmentation fault in sun.nio.ch.sctp.SctpNet.connect0. From here on, we likely need extra changes in native-image to make this work. Note that this is suboptimal to begin with because libsctp.so is not a stand-alone library, but depends on HotSpot's libjvm.so.

mbaudier commented 4 years ago

I just gave another try on using SCTP with native image, using the recently released GraalVM v19.3.0 for Linux AMD64. I was hoping that the new feature of loading native libraries may now solve this issue.

But now that the native image generation which is failing, with the following error: Fatal error: java.lang.RuntimeException: java.lang.RuntimeException: host C compiler or linker does not seem to work: java.lang.RuntimeException: returned 1

I am testing on an up-to-date CentOS v7.7 x86_64. Is it because CentOS 7 C compiler is too old? Should I try on Fedora?

More log:


Running command: cc -v -o /home/mbaudier/dev/workspaces/vertikal-vje/sctp-test/test.sctp.daytimeserver -z noexecstack -Wl,--gc-sections -Wl,--dynamic-list -Wl,/tmp/SVM-13891162283796859173/exported_symbols.list -Wl,-x -L/tmp/SVM-13891162283796859173 -L/opt/graalvm-ce-java11-19.3/lib -L/opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64 /tmp/SVM-13891162283796859173/test.sctp.daytimeserver.o /opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64/libffi.a /opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64/libjvm.a /opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64/libstrictmath.a /opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64/liblibchelper.a /opt/graalvm-ce-java11-19.3/lib/libnet.a /opt/graalvm-ce-java11-19.3/lib/libjava.a /opt/graalvm-ce-java11-19.3/lib/libzip.a /opt/graalvm-ce-java11-19.3/lib/libnio.a /opt/graalvm-ce-java11-19.3/lib/libextnet.a -lm -lpthread -lpthread -ldl -lz -lrt

Using built-in specs.
COLLECT_GCC=cc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
COMPILER_PATH=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/:/usr/libexec/gcc/x86_64-redhat-linux/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/
LIBRARY_PATH=/usr/lib/gcc/x86_64-redhat-linux/4.8.5/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/:/lib/../lib64/:/usr/lib/../lib64/:/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' '/home/mbaudier/dev/workspaces/vertikal-vje/sctp-test/test.sctp.daytimeserver' '-z' 'noexecstack' '-L/tmp/SVM-13891162283796859173' '-L/opt/graalvm-ce-java11-19.3/lib' '-L/opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64' '-mtune=generic' '-march=x86-64'
 /usr/libexec/gcc/x86_64-redhat-linux/4.8.5/collect2 --build-id --no-add-needed --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o /home/mbaudier/dev/workspaces/vertikal-vje/sctp-test/test.sctp.daytimeserver -z noexecstack /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crt1.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crti.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtbegin.o -L/tmp/SVM-13891162283796859173 -L/opt/graalvm-ce-java11-19.3/lib -L/opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64 -L/lib/../lib64 -L/usr/lib/../lib64 -L/usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../.. --gc-sections --dynamic-list /tmp/SVM-13891162283796859173/exported_symbols.list -x /tmp/SVM-13891162283796859173/test.sctp.daytimeserver.o /opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64/libffi.a /opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64/libjvm.a /opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64/libstrictmath.a /opt/graalvm-ce-java11-19.3/lib/svm/clibraries/linux-amd64/liblibchelper.a /opt/graalvm-ce-java11-19.3/lib/libnet.a /opt/graalvm-ce-java11-19.3/lib/libjava.a /opt/graalvm-ce-java11-19.3/lib/libzip.a /opt/graalvm-ce-java11-19.3/lib/libnio.a /opt/graalvm-ce-java11-19.3/lib/libextnet.a -lm -lpthread -lpthread -ldl -lz -lrt -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-redhat-linux/4.8.5/crtend.o /usr/lib/gcc/x86_64-redhat-linux/4.8.5/../../../../lib64/crtn.o
/tmp/SVM-13891162283796859173/test.sctp.daytimeserver.o:(.data+0xd8): undefined reference to `Java_sun_nio_ch_sctp_SctpServerChannelImpl_accept0'
/tmp/SVM-13891162283796859173/test.sctp.daytimeserver.o:(.data+0x198): undefined reference to `Java_sun_nio_ch_sctp_SctpNet_close0'
/tmp/SVM-13891162283796859173/test.sctp.daytimeserver.o:(.data+0x248): undefined reference to `Java_sun_nio_ch_sctp_SctpChannelImpl_receive0'
/tmp/SVM-13891162283796859173/test.sctp.daytimeserver.o:(.data+0x578): undefined reference to `Java_sun_nio_ch_sctp_SctpNet_socket0'
/tmp/SVM-13891162283796859173/test.sctp.daytimeserver.o:(.data+0x5d8): undefined reference to `Java_sun_nio_ch_sctp_SctpChannelImpl_initIDs'
/tmp/SVM-13891162283796859173/test.sctp.daytimeserver.o:(.data+0x780): undefined reference to `Java_sun_nio_ch_sctp_SctpChannelImpl_send0'
/tmp/SVM-13891162283796859173/test.sctp.daytimeserver.o:(.data+0xa88): undefined reference to `Java_sun_nio_ch_sctp_SctpNet_preClose0'
/tmp/SVM-13891162283796859173/test.sctp.daytimeserver.o:(.data+0xb00): undefined reference to `Java_sun_nio_ch_sctp_SctpNet_listen0'
collect2: error: ld returned 1 exit status
peter-hofer commented 4 years ago

Indeed we started using static linking of JDK libraries in 19.3. Unfortunately however, we do not have a static variant of the sctp library yet, which is why linking fails. A different compiler or toolchain won't help. We hope to have this fixed in an upcoming release.

mbaudier commented 4 years ago

Ok, I understand! Nice to see that this is moving forward. Thanks for the great work.

akibsayyed commented 1 year ago

Hi Did anyone get any luck with SCTP programs so far with the latest CE version?