Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[native-image] Fatal error with grpc-java #917

Closed
schneidersteve opened this issue Jan 15, 2019 · 5 comments
Closed

[native-image] Fatal error with grpc-java #917

schneidersteve opened this issue Jan 15, 2019 · 5 comments

Comments

@schneidersteve
Copy link

I get "a fatal error has been detected by the Java Runtime Environment" while building a native image with GraalVM 1.0.0-rc11 (rc10 too)
. It is a grpc java server (netty based) embedded into a Micronaut 1.0.3 based app. If i add the following content to the reflection.json

{
  "name" : "io.grpc.netty.shaded.io.netty.channel.socket.nio.NioServerSocketChannel",
  "allDeclaredConstructors" : true
}

i get the following error

[test-micronaut-grpc-graalvm:2378]    classlist:   3,953.07 ms
[test-micronaut-grpc-graalvm:2378]        (cap):     780.70 ms
[test-micronaut-grpc-graalvm:2378]        setup:   1,839.72 ms
Warning: RecomputeFieldValue.ArrayIndexScale automatic substitution failed. The automatic substitution registration was attempted because a call to sun.misc.Unsafe.arrayIndexScale(Class) was detected in the static initializer of io.micronaut.caffeine.cache.UnsafeRefArrayAccess. Detailed failure reason(s): Could not determine the field where the value produced by the call to sun.misc.Unsafe.arrayIndexScale(Class) for the array index scale computation is stored. The call is not directly followed by a field store or by a sign extend node followed directly by a field store. 
Warning: RecomputeFieldValue.ArrayIndexScale automatic substitution failed. The automatic substitution registration was attempted because a call to sun.misc.Unsafe.arrayIndexScale(Class) was detected in the static initializer of io.grpc.netty.shaded.io.netty.util.internal.PlatformDependent0. Detailed failure reason(s): The field java.lang.Long.value, where the value produced by the array index scale computation is stored, is not static.
Warning: RecomputeFieldValue.FieldOffset automatic substitution failed. The automatic substitution registration was attempted because a call to sun.misc.Unsafe.objectFieldOffset(Field) was detected in the static initializer of io.grpc.netty.shaded.io.netty.util.internal.PlatformDependent0. Add a RecomputeFieldValue.FieldOffset manual substitution for io.grpc.netty.shaded.io.netty.util.internal.PlatformDependent0.ADDRESS_FIELD_OFFSET. Detailed failure reason(s): The argument of Unsafe.objectFieldOffset(Field) is not a constant field.
Warning: RecomputeFieldValue.ArrayIndexScale automatic substitution failed. The automatic substitution registration was attempted because a call to sun.misc.Unsafe.arrayIndexScale(Class) was detected in the static initializer of io.grpc.netty.shaded.io.netty.util.internal.shaded.org.jctools.util.UnsafeRefArrayAccess. Detailed failure reason(s): Could not determine the field where the value produced by the call to sun.misc.Unsafe.arrayIndexScale(Class) for the array index scale computation is stored. The call is not directly followed by a field store or by a sign extend node followed directly by a field store. 
Warning: RecomputeFieldValue.FieldOffset automatic substitution failed. The automatic substitution registration was attempted because a call to sun.misc.Unsafe.objectFieldOffset(Field) was detected in the static initializer of com.google.protobuf.UnsafeUtil. Detailed failure reason(s): The argument of Unsafe.objectFieldOffset(Field) is not a constant field., Could not determine the field where the value produced by the call to sun.misc.Unsafe.objectFieldOffset(Field) for the field offset computation is stored. The call is not directly followed by a field store or by a sign extend node followed directly by a field store. 
Warning: class initialization of class io.grpc.netty.shaded.io.netty.handler.ssl.ReferenceCountedOpenSslEngine failed with exception java.lang.UnsatisfiedLinkError: io.grpc.netty.shaded.io.netty.internal.tcnative.NativeStaticallyReferencedJniMethods.sslOpCipherServerPreference()I. This class will be initialized at run time because either option --report-unsupported-elements-at-runtime or option --allow-incomplete-classpath is used for image building. Use the option --delay-class-initialization-to-runtime=io.grpc.netty.shaded.io.netty.handler.ssl.ReferenceCountedOpenSslEngine to explicitly request delayed initialization of this class.
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007fca42810a70, pid=2378, tid=0x00007fc9997fb700
#
# JRE version: OpenJDK Runtime Environment (8.0_192-b12) (build 1.8.0_192-20181024121959.buildslave.jdk8u-src-tar--b12)
# Java VM: GraalVM 1.0.0-rc11 (25.192-b12-jvmci-0.53 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C  0x00007fca42810a70
#
# Failed to write core dump. Core dumps have been disabled. To enable core dumping, try "ulimit -c unlimited" before starting Java again
#
# An error report file with more information is saved as:
# /home/steve/workspaces/idea/micronaut-grpc-example/hs_err_pid2378.log
#
# If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp
#
Error: Image building with exit status 134

hs_err_pid2378.log

@cstancu
Copy link
Member

cstancu commented Jan 16, 2019

It looks like the initialization of io.grpc.netty.shaded.io.netty.handler.ssl.OpenSsl crashes the JVM. We run the <clinit> of that class at image build time. This is the relevant part of the trace:

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j  io.grpc.netty.shaded.io.netty.internal.tcnative.Library.aprMajorVersion()I+0
j  io.grpc.netty.shaded.io.netty.internal.tcnative.Library.initialize(Ljava/lang/String;Ljava/lang/String;)Z+31
j  io.grpc.netty.shaded.io.netty.handler.ssl.OpenSsl.initializeTcNative(Ljava/lang/String;)Z+3
j  io.grpc.netty.shaded.io.netty.handler.ssl.OpenSsl.<clinit>()V+240
v  ~StubRoutines::call_stub
J 2201  sun.misc.Unsafe.ensureClassInitialized(Ljava/lang/Class;)V (0 bytes) @ 0x00007fca65727d6c [0x00007fca65727d20+0x4c]

You can try delaying the initialization of that class by adding --delay-class-initialization-to-runtime=io.grpc.netty.shaded.io.netty.handler.ssl.OpenSsl to the native-image command. You can read more about class initialization in the context of native-image in this blog article.

Building native images of netty based apps usually requires some preparation. Take a look at this article for details.

@schneidersteve
Copy link
Author

Thanks for the help. I was able to fix this issue.

@dzou
Copy link

dzou commented Jun 5, 2020

Thanks for the help. I was able to fix this issue.

@schneidersteve - Sorry to bother, I am actually encountering the same problem and wanted to view the solution you posted, but it looks like the links are dead - any chance you remember what you did?

@huntc
Copy link

huntc commented Apr 9, 2021

gRPC Java shades Netty and so the lookup for io.netty.channel.socket.nio.NioServerSocketChannel can never succeed (I'm guessing that the original issue here was because of native-image not being able to resolve NioServerSocketChannel). To make native-image happy, I declared my own io.netty.channel.socket.nio.NioServerSocketChannel as follows:

package io.netty.channel.socket.nio;

public class NioServerSocketChannel {}

@dzou
Copy link

dzou commented Apr 9, 2021

Thanks for the help @huntc. Just an fyi we started a project compiling the Google Cloud GraalVM configurations (for reflection, native calls, etc.) here: https://github.com/GoogleCloudPlatform/google-cloud-graalvm-support

The project aims to get everything working out of the box; hope this helps! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants