HewlettPackard / kraal

Enables the use of Kotlin coroutines and GraalVM native-image together
MIT License
84 stars 2 forks source link

Getting Error at instruction 115: Incompatible stack heights when trying to run Kraal #3

Open chintana-zz opened 5 years ago

chintana-zz commented 5 years ago

Thanks for publishing this useful tool. I'm trying to get native image generation for jars generated by Ballerina language. When I'm testing, I could generate a simple jar file and run it with Ballerina. Also I can use GraalVM to generate a native image out of it. When I try to run kraal on the same jar, I'm seeing following exception.

May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt removeIrreducibleLoops
INFO: Processing jar file /Users/chintana/src/ballerina/distribution/zip/jballerina-tools/target/jballerina-tools-0.992.0-m2-SNAPSHOT/test2.jar3705232667410202800.jar
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /test2.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /___init.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /mainFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /___init_Frame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/open.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/___init.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/WritableTextRecordChannel___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/WritableDataChannel___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/WritableCSVChannel_writeFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/WritableCSVChannel_closeFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/WritableCSVChannel___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/WritableCharacterChannel___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/WritableByteChannel___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/StringReader_readXmlFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/StringReader_closeFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/StringReader_readJsonFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/StringReader_readCharFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/StringReader___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/ReadableTextRecordChannel___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/ReadableDataChannel___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/ReadableCSVChannel_hasNextFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/ReadableCSVChannel_closeFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/ReadableCSVChannel_getNextFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/ReadableCSVChannel_skipHeadersFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/ReadableCSVChannel___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/_anonType_10___init_Frame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/ReadableCharacterChannel___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/ReadableByteChannel___initFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/IOError___init_Frame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/openWritableCsvFileFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/openReadableCsvFileFrame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/ballerina_io__init_Frame.class
May 29, 2019 4:53:47 PM com.hpe.kraal.FileHandlerKt processClassFile
INFO: Processing /ballerina/io/WritableTextRecordChannel.class
Exception in thread "main" org.objectweb.asm.tree.analysis.AnalyzerException: Error at instruction 115: Incompatible stack heights
    at org.objectweb.asm.tree.analysis.Analyzer.analyze(Analyzer.java:291)
    at com.hpe.kraal.MethodControlFlowGraphKt.createEdges(MethodControlFlowGraph.kt:92)
    at com.hpe.kraal.MethodControlFlowGraphKt.computeControlFlowGraph(MethodControlFlowGraph.kt:98)
    at com.hpe.kraal.ControlFlowGraphSplitterKt.removeIrreducibleLoopsWithoutVerification(ControlFlowGraphSplitter.kt:41)
    at com.hpe.kraal.ControlFlowGraphSplitterKt.removeIrreducibleLoops(ControlFlowGraphSplitter.kt:29)
    at com.hpe.kraal.FileHandlerKt.removeIrreducibleLoops(FileHandler.kt:38)
    at com.hpe.kraal.FileHandlerKt.processClassFile(FileHandler.kt:78)
    at com.hpe.kraal.FileHandlerKt.removeIrreducibleLoops(FileHandler.kt:65)
    at com.hpe.kraal.MainKt.processFile(Main.kt:71)
    at com.hpe.kraal.MainKt.main(Main.kt:41)
Caused by: org.objectweb.asm.tree.analysis.AnalyzerException: Incompatible stack heights
    at org.objectweb.asm.tree.analysis.Frame.merge(Frame.java:672)
    at org.objectweb.asm.tree.analysis.Analyzer.merge(Analyzer.java:541)
    at org.objectweb.asm.tree.analysis.Analyzer.analyze(Analyzer.java:205)
    ... 9 more

The file in question ballerina/io/WritableTextRecordChannel.class contains standard IO routines in Ballerina language. Byte code generation in Ballerina is done through ASM lib as well.

javap -c -p -v output for the class can be found here. There the instruction 115 seems to be an aastore. May be I'm looking at the wrong place. Appreciate any tips for debugging this. Thanks.

bradnewman commented 5 years ago

From the stack trace, the exception is before Kraal has decided whether there are irreducible loops or not - at this point, it's just using ASM to analyze the control flow graph and hasn't modified the bytecode for this method. So, in theory you should be able to reproduce the exception just with ASM using Analyzer and BasicInterpreter. (And, this is most likely either an ASM issue or a Ballerina issue - just based on the stack trace, it seems unlikely that Kraal is to blame.)

The relevant Kraal code is MethodControlFlowGraph.createEdges(), though the overrides of the Analyzer methods should be irrelevant here. It should really just be the ceremony of getting an ASM MethodNode instance for your __init method and then running an Analyzer:

val analyzer = Analyzer<BasicValue>(BasicInterpreter())
analyzer.analyze("ballerina/io/WritableTextRecordChannel", myMethodNode)

If you're just looking to work around this, and you know there aren't actually irreducible loops in this class, it wouldn't be too hard to add a feature to Kraal to accept include/exclude filters for packages and classes.

chintana-zz commented 5 years ago

Thanks! Yes, you're right. It works if I exclude standard lib classes in Ballerina. Let me clean it up a bit and send a PR.