oracle / graal

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

[GR-45625] polyglot+LLVM "hello world" throws UnsupportedOperationException #6372

Open GajuszZOrod opened 1 year ago

GajuszZOrod commented 1 year ago

Describe GraalVM and your environment :

Have you verified this issue still happens when using the latest snapshot? Yes (GraalVM CE 23.0.0-dev-20230331_1201)

Describe the issue In Java, I'm trying to embed LLVM bitcode generated from simplest C++ code. The eval() function throws exception as below.

Steps to reproduce the issue

hello1.cpp

int main() {
    return 0;
}

HelloPolyglot.java

import org.graalvm.polyglot.*;
import org.graalvm.polyglot.proxy.*;
import org.graalvm.polyglot.io.ByteSequence;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.File;
import java.io.IOException;

public class HelloPolyglot {

    public static void main(String[] args) throws IOException {

        String bcFilePath = "hello1.bc";

        File file = new File(".", bcFilePath);
        String language = Source.findLanguage(file);

        System.out.println("Language detected:\t" + language);

        byte[] llvmBitcode = Files.readAllBytes(Paths.get(bcFilePath));

        System.out.println("LLVM bytecode length:\t" + llvmBitcode.length);

        Source source = Source.newBuilder("llvm",
                    ByteSequence.create(llvmBitcode),
                    "<literal>").buildLiteral();

        Context context = Context.newBuilder().allowAllAccess(true).build();    // allowNativeAccess

        context.eval(source);

        context.close();

    }
}

The command I use to generate LLVM from C++ (results in creation of hello1.bc file): /c/PROGRAMY/graalvm-ce-java19-22.3.1/lib/llvm/bin/clang++ -c -emit-llvm hello1.cpp

Then I compile and run the Java code: javac HelloPolyglot.java java HelloPolyglot

The Java output:

LLVM bytecode length:   2264
Exception in thread "main" org.graalvm.polyglot.PolyglotException: java.lang.UnsupportedOperationException: null not supported as key!
        at org.graalvm.sdk/org.graalvm.collections.EconomicMapImpl.checkKeyNonNull(EconomicMapImpl.java:640)
        at org.graalvm.sdk/org.graalvm.collections.EconomicMapImpl.get(EconomicMapImpl.java:242)
        at com.oracle.truffle.llvm.runtime.LLVMLanguage.getCachedLibrary(LLVMLanguage.java:762)
        at com.oracle.truffle.llvm.runtime.LLVMLanguage.parse(LLVMLanguage.java:726)
        at org.graalvm.truffle/com.oracle.truffle.api.TruffleLanguage$ParsingRequest.parse(TruffleLanguage.java:1000)
        at org.graalvm.truffle/com.oracle.truffle.api.TruffleLanguage.parse(TruffleLanguage.java:1357)
        at org.graalvm.truffle/com.oracle.truffle.api.LanguageAccessor$LanguageImpl.parse(LanguageAccessor.java:298)
        at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotSourceCache.parseImpl(PolyglotSourceCache.java:94)
        at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotSourceCache$WeakCache.lookup(PolyglotSourceCache.java:222)
        at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotSourceCache.parseCached(PolyglotSourceCache.java:80)
        at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotLanguageContext.parseCached(PolyglotLanguageContext.java:372)
        at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotContextImpl.eval(PolyglotContextImpl.java:1480)
        at org.graalvm.truffle/com.oracle.truffle.polyglot.PolyglotContextDispatch.eval(PolyglotContextDispatch.java:63)
        at org.graalvm.sdk/org.graalvm.polyglot.Context.eval(Context.java:402)
        at HelloPolyglot.main(HelloPolyglot.java:32)
        Suppressed: Attached Guest Language Frames (0)
Internal GraalVM error, please report at https://github.com/oracle/graal/issues/.

Expected behavior I expected the C++ code to be executed within Java application, without throwing the exception.

oubidar-Abderrahim commented 1 year ago

Hi, Thank you for reporting this, we'll take a look into it shortly

oubidar-Abderrahim commented 1 year ago

Tracked internally on GR 45625