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

NullPointerException while creating GraalVM native image #3815

Closed
retriku opened this issue Sep 19, 2021 · 9 comments
Closed

NullPointerException while creating GraalVM native image #3815

retriku opened this issue Sep 19, 2021 · 9 comments
Assignees

Comments

@retriku
Copy link

retriku commented Sep 19, 2021

Describe the issue
NullPointerException while creating native image

Steps to reproduce the issue
Please include both build steps as well as run steps

  1. git clone https://github.com/retriku/graalvm-scala-mongodb-npe.git
  2. sbt clean compile 'show graalvm-native-image:packageBin'

Describe GraalVM and your environment:

  • OpenJDK 64-Bit Server VM (11.0.12+6-jvmci-21.2-b08) for bsd-amd64 JRE (11.0.12+6-jvmci-21.2-b08), built on Jul 19 2021 17:16:18 by "graal" with clang 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)
  • JDK major version: 11
  • OS: macOS BigSur Version 11.6
  • Architecture: AMD64

More details

Error seems to originate in https://github.com/mongodb/mongo-java-driver/blob/master/driver-core/src/main/com/mongodb/internal/connection/tlschannel/util/DirectBufferDeallocator.java#L54

I wonder why would Java8Deallocator would be created if image is built with java 11. Is this on purpose?

Let me know if any other info is needed.

Stack trace is below:

    [error] Fatal error:com.oracle.graal.pointsto.util.AnalysisError$ParsingError: Error encountered while parsing com.mongodb.internal.connection.tlschannel.util.DirectBufferDeallocator$Java8Deallocator.<init>() 
[error] Parsing context:
[error]    at com.mongodb.internal.connection.tlschannel.util.DirectBufferDeallocator.<init>(DirectBufferDeallocator.java:114)
[error]    at com.mongodb.internal.connection.tlschannel.DirectBufferAllocator.<init>(DirectBufferAllocator.java:37)
[error]    at com.mongodb.internal.connection.tlschannel.TlsChannel.<clinit>(TlsChannel.java:70)
[error]         at com.oracle.graal.pointsto.util.AnalysisError.parsingError(AnalysisError.java:126)
[error]         at com.oracle.graal.pointsto.flow.MethodTypeFlow.createTypeFlow(MethodTypeFlow.java:307)
[error]         at com.oracle.graal.pointsto.flow.MethodTypeFlow.ensureTypeFlowCreated(MethodTypeFlow.java:282)
[error]         at com.oracle.graal.pointsto.flow.MethodTypeFlow.addContext(MethodTypeFlow.java:103)
[error]         at com.oracle.graal.pointsto.DefaultAnalysisPolicy$DefaultSpecialInvokeTypeFlow.onObservedUpdate(DefaultAnalysisPolicy.java:363)
[error]         at com.oracle.graal.pointsto.flow.TypeFlow.notifyObservers(TypeFlow.java:471)
[error]         at com.oracle.graal.pointsto.flow.TypeFlow.update(TypeFlow.java:540)
[error]         at com.oracle.graal.pointsto.BigBang$2.run(BigBang.java:539)
[error]         at com.oracle.graal.pointsto.util.CompletionExecutor.executeCommand(CompletionExecutor.java:188)
[error]         at com.oracle.graal.pointsto.util.CompletionExecutor.lambda$executeService$0(CompletionExecutor.java:172)
[error]         at java.base/java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1426)
[error]         at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
[error]         at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
[error]         at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
[error]         at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
[error]         at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
[error] Caused by: org.graalvm.compiler.java.BytecodeParser$BytecodeParserError: java.lang.NullPointerException
[error]         at parsing com.mongodb.internal.connection.tlschannel.util.DirectBufferDeallocator$Java8Deallocator.<init>(DirectBufferDeallocator.java:54)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.throwParserError(BytecodeParser.java:2578)
[error]         at com.oracle.svm.hosted.phases.SharedGraphBuilderPhase$SharedBytecodeParser.throwParserError(SharedGraphBuilderPhase.java:111)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.iterateBytecodesForBlock(BytecodeParser.java:3439)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.handleBytecodeBlock(BytecodeParser.java:3391)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.processBlock(BytecodeParser.java:3236)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.build(BytecodeParser.java:1122)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.buildRootMethod(BytecodeParser.java:1007)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.GraphBuilderPhase$Instance.run(GraphBuilderPhase.java:84)
[error]         at com.oracle.svm.hosted.phases.SharedGraphBuilderPhase.run(SharedGraphBuilderPhase.java:81)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.phases.Phase.run(Phase.java:49)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.phases.BasePhase.apply(BasePhase.java:212)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.phases.Phase.apply(Phase.java:42)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.phases.Phase.apply(Phase.java:38)
[error]         at com.oracle.graal.pointsto.flow.AnalysisParsedGraph.parseBytecode(AnalysisParsedGraph.java:131)
[error]         at com.oracle.svm.hosted.SVMHost.parseBytecode(SVMHost.java:709)
[error]         at com.oracle.graal.pointsto.meta.AnalysisMethod.ensureGraphParsed(AnalysisMethod.java:605)
[error]         at com.oracle.graal.pointsto.flow.MethodTypeFlowBuilder.parse(MethodTypeFlowBuilder.java:163)
[error]         at com.oracle.graal.pointsto.flow.MethodTypeFlowBuilder.apply(MethodTypeFlowBuilder.java:321)
[error]         at com.oracle.graal.pointsto.flow.MethodTypeFlow.createTypeFlow(MethodTypeFlow.java:293)
[error]         ... 14 more
[error] Caused by: java.lang.NullPointerException
[error]         at com.oracle.svm.hosted.snippets.ReflectionPlugins.lambda$foldInvocationUsingReflection$3(ReflectionPlugins.java:374)
[error]         at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
[error]         at java.base/java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
[error]         at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
[error]         at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
[error]         at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
[error]         at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
[error]         at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:578)
[error]         at com.oracle.svm.hosted.snippets.ReflectionPlugins.lambda$foldInvocationUsingReflection$4(ReflectionPlugins.java:374)
[error]         at com.oracle.svm.hosted.snippets.ReflectionPlugins.traceConstant(ReflectionPlugins.java:565)
[error]         at com.oracle.svm.hosted.snippets.ReflectionPlugins.pushConstant(ReflectionPlugins.java:539)
[error]         at com.oracle.svm.hosted.snippets.ReflectionPlugins.foldInvocationUsingReflection(ReflectionPlugins.java:394)
[error]         at com.oracle.svm.hosted.snippets.ReflectionPlugins.access$200(ReflectionPlugins.java:95)
[error]         at com.oracle.svm.hosted.snippets.ReflectionPlugins$5.defaultHandler(ReflectionPlugins.java:335)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.apply(InvocationPlugin.java:105)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin.execute(InvocationPlugin.java:170)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.applyInvocationPlugin(BytecodeParser.java:2197)
[error]         at com.oracle.svm.hosted.phases.AnalysisGraphBuilderPhase$AnalysisBytecodeParser.applyInvocationPlugin(AnalysisGraphBuilderPhase.java:86)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.tryInvocationPlugin(BytecodeParser.java:2183)
[error]         at com.oracle.svm.hosted.phases.AnalysisGraphBuilderPhase$AnalysisBytecodeParser.tryInvocationPlugin(AnalysisGraphBuilderPhase.java:67)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.appendInvoke(BytecodeParser.java:1889)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.genDynamicInvokeHelper(BytecodeParser.java:1762)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.genInvokeVirtual(BytecodeParser.java:1712)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.genInvokeVirtual(BytecodeParser.java:1697)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.processBytecode(BytecodeParser.java:5371)
[error]         at jdk.internal.vm.compiler/org.graalvm.compiler.java.BytecodeParser.iterateBytecodesForBlock(BytecodeParser.java:3431)
[error]         ... 30 more
@munishchouhan
Copy link
Contributor

@retriku thanks for reporting the error, we will take a look and get back to you

@munishchouhan
Copy link
Contributor

@retriku When I am trying to use jar file of this project, I am getting Error:

$java -jar target/scala-2.13/graalvm-scala-simple_2.13-0.1.0.jar
Error: Unable to initialize main class graalvm.Main
Caused by: java.lang.NoClassDefFoundError: org/mongodb/scala/MongoDatabase

@munishchouhan
Copy link
Contributor

for native-image, you can use native-image-agent to generate config files and add them to the settings in build.sbt

  1. Run agent
    java -agentlib:native-image-agent=config-output-dir=./configs -jar target/scala-2.13/graalvm-scala-simple_2.13-0.1.0.jar
  2. add config files
"-H:ResourceConfigurationFiles=../../configs/resource-config.json",
	"-H:ReflectionConfigurationFiles=../../configs/reflect-config.json",
	"-H:JNIConfigurationFiles=../../configs/jni-config.json",
	"-H:DynamicProxyConfigurationFiles=../../configs/proxy-config.json"

@retriku
Copy link
Author

retriku commented Sep 23, 2021

I added assembly plugin. You can build fat jar with sbt assembly, it will create file graalvm-scala-simple-assembly-0.1.0.jar. I force pushed code to repo.

Failing image build seems to be due to (Class[])null parameter value. Following code also fails to create native image:

import java.lang.reflect.Method;

public class JavaApp {
    public static void main(String[] args) throws Exception {
        Method cleanerAccessor = Class.forName("sun.nio.ch.DirectBuffer").getMethod("cleaner", (Class[])null);
        System.out.println(cleanerAccessor);
        Method cleanMethod = Class.forName("sun.misc.Cleaner").getMethod("clean");
        System.out.println(cleanMethod);
    }
}

But succeeds with code:

import java.lang.reflect.Method;

public class JavaApp {
    public static void main(String[] args) throws Exception {
        Method cleanerAccessor = Class.forName("sun.nio.ch.DirectBuffer").getMethod("cleaner");
        System.out.println(cleanerAccessor);
        Method cleanMethod = Class.forName("sun.misc.Cleaner").getMethod("clean");
        System.out.println(cleanMethod);
    }
}

@munishchouhan
Copy link
Contributor

@retriku ok great, do you still need any other assistance from our side?

@jyemin
Copy link

jyemin commented Sep 23, 2021

Hi @mcraj017, the code that @retriku is referring to is contained in multiple releases of a widely-used library (I'm one of the maintainers), so @retriku can't easily change it. Moreover, the Javadoc for Class.getMethod() explicitly states that it's legal to pass null as the value of parameterTypes:

The parameterTypes parameter is an array of Class objects that identify the method's formal parameter types, in declared order. If parameterTypes is null, it is treated as if it were an empty array.

So it seems like GraalVM should handle this code without error, but if not I'd like to understand why.

@munishchouhan
Copy link
Contributor

@jyemin thanks for the clarification, I have raised this issue with the native-image team

@christianwimmer
Copy link

The error occurs as part of the trace output enabled by -H:+ReflectionPluginTracing. So when you remove that option, the error should go away.
Still something we will fix of course.

@christianwimmer
Copy link

Fixed in 8297f26

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