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.38k stars 1.64k forks source link

Mimetype issue #297

Closed himanshumps closed 6 years ago

himanshumps commented 6 years ago

While executing a sample code using graalvm-0.31, we are seeing mimetype issues.

Code:

import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.vm.PolyglotEngine;
import java.io.ByteArrayOutputStream;
public class HelloPolyglotWorld {
 public static void main(String[] args) throws Exception {
   ByteArrayOutputStream yourOutput = new ByteArrayOutputStream();
   PolyglotEngine engine = PolyglotEngine.newBuilder()
           .setOut(yourOutput)
           .setErr(yourOutput)
           .build();
   String js="[1,2,0b11].forEach(v => print(`line ${v}` + v))";
   engine.eval(Source.newBuilder(js).mimeType("text/javascript").name("").build()).get();
   String s = yourOutput.toString("UTF-8");
   System.out.println("Output: " + s);
   yourOutput.reset();
   String ruby="puts 'hello'";
   engine.eval(Source.newBuilder(ruby).mimeType("application/x-ruby").name("").build()).get();
   s = yourOutput.toString("UTF-8");
   System.out.println("Output: " + s);
  }
}

Run:

/opt/graalvm-0.31/bin/java -cp ../jre/lib/boot/graal-sdk.jar:.:../jre/lib/truffle/truffle-api.jar:../jre/languages/R/fastr.jar:../jre/languages/js/graaljs.jar:../jre/languages/python/graalpython.jar:../jre/languages/python/graalpython-env.jar:../jre/languages/ruby/truffleruby.jar:../jre/languages/ruby/truffleruby-launcher.jar HelloPolyglotWorld

Exception:

[ruby] PERFORMANCE this JVM does not have the Graal compiler - performance will be limited - see doc/user/using-graalvm.md
[ruby] WARNING TruffleRuby's home was not explicitly set.
* Launcher not set, home path could not be derived.
* GraalVM home not found.
* Try to set home using -Xhome=PATH option.
Exception in thread "main" java.lang.IllegalStateException: java.lang.IllegalStateException: No language for MIME type application/x-native found. Supported types: [application/x-r, text/x-r, application/javascript, text/javascript, application/x-python, application/x-ruby, application/js-regex]
 at com.oracle.truffle.api.vm.ComputeInExecutor.exceptionCheck(ComputeInExecutor.java:65)
 at com.oracle.truffle.api.vm.ComputeInExecutor.perform(ComputeInExecutor.java:84)
 at com.oracle.truffle.api.vm.PolyglotEngine.evalImpl(PolyglotEngine.java:662)
 at com.oracle.truffle.api.vm.PolyglotEngine.eval(PolyglotEngine.java:646)
 at HelloPolyglotWorld.main(HelloPolyglotWorld.java:19)
timfel commented 6 years ago

Looks like Ruby is trying to load native extensions, and sulong is not on the classpath. Looking at the cmdline used to run it, ../jre/languages/llvm/sulong.jar is missing from that.

chrisseaton commented 6 years ago

application/x-native is the NFI - the Truffle Native Function Interface - rather than Sulong. You want jre/lib/truffle/truffle-nfi.jar. Ruby can run without the NFI if you really need it, but you probably don't.

himanshumps commented 6 years ago

After adding truffle-nfi.jar

/opt/graalvm-0.31/bin/java -cp .:../jre/lib/truffle/truffle-api.jar:../jre/lib/boot/graal-sdk.jar:../jre/languages/js/graaljs.jar:../jre/languages/R/fastr.jar:../jre/languages/python/graalpython.jar:../jre/languages/python/graalpython-env.jar:../jre/languages/ruby/truffleruby.jar:../jre/languages/ruby/truffleruby-launcher.jar:../jre/lib/truffle/truffle-nfi.jar HelloPolyglotWorld
Output: line 11
line 22
line 33

[ruby] PERFORMANCE this JVM does not have the Graal compiler - performance will be limited - see doc/user/using-graalvm.md
[ruby] WARNING TruffleRuby's home was not explicitly set.
* Launcher not set, home path could not be derived.
* GraalVM home not found.
* Try to set home using -Xhome=PATH option.
Exception in thread "main" java.lang.UnsatisfiedLinkError: no trufflenfi in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1867)
        at java.lang.Runtime.loadLibrary0(Runtime.java:870)
        at java.lang.System.loadLibrary(System.java:1122)
        at com.oracle.truffle.nfi.impl.NFIContext.loadNFILib(NFIContext.java:216)
        at com.oracle.truffle.nfi.impl.NFIContext.initialize(NFIContext.java:97)
        at com.oracle.truffle.nfi.impl.NFILanguageImpl.initializeContext(NFILanguageImpl.java:49)
        at com.oracle.truffle.nfi.impl.NFILanguageImpl.initializeContext(NFILanguageImpl.java:37)
        at com.oracle.truffle.api.TruffleLanguage$Env.postInit(TruffleLanguage.java:1639)
        at com.oracle.truffle.api.TruffleLanguage$LanguageImpl.postInitEnv(TruffleLanguage.java:1820)
        at com.oracle.truffle.api.vm.PolyglotEngine$Language.getEnv(PolyglotEngine.java:1466)
        at com.oracle.truffle.api.vm.PolyglotEngine$LegacyEngineImpl.getEnvForLanguage(PolyglotEngine.java:1513)
        at com.oracle.truffle.api.TruffleLanguage$Env.parse(TruffleLanguage.java:1409)
        at com.oracle.truffle.nfi.NFILanguage.parse(NFILanguage.java:74)
        at com.oracle.truffle.api.TruffleLanguage$ParsingRequest.parse(TruffleLanguage.java:596)
        at com.oracle.truffle.api.TruffleLanguage.parse(TruffleLanguage.java:1040)
        at com.oracle.truffle.api.TruffleLanguage$Env.parse(TruffleLanguage.java:1409)
        at org.truffleruby.platform.TruffleNFIPlatform.<init>(TruffleNFIPlatform.java:40)
        at org.truffleruby.RubyContext.<init>(RubyContext.java:208)
        at org.truffleruby.RubyLanguage.createContext(RubyLanguage.java:113)
        at org.truffleruby.RubyLanguage.createContext(RubyLanguage.java:47)
        at com.oracle.truffle.api.TruffleLanguage$LanguageImpl.createEnvContext(TruffleLanguage.java:1805)
        at com.oracle.truffle.api.vm.PolyglotEngine$Language.getEnv(PolyglotEngine.java:1465)
        at com.oracle.truffle.api.vm.PolyglotRootNode$EvalRootNode.initialize(PolyglotRootNode.java:268)
        at com.oracle.truffle.api.vm.PolyglotRootNode$EvalRootNode.executeImpl(PolyglotRootNode.java:235)
        at com.oracle.truffle.api.vm.PolyglotRootNode.execute(PolyglotRootNode.java:73)
        at com.oracle.truffle.api.impl.DefaultCallTarget.call(DefaultCallTarget.java:81)
        at com.oracle.truffle.api.vm.PolyglotEngine$1.compute(PolyglotEngine.java:659)
        at com.oracle.truffle.api.vm.ComputeInExecutor.run(ComputeInExecutor.java:96)
        at com.oracle.truffle.api.vm.ComputeInExecutor.perform(ComputeInExecutor.java:83)
        at com.oracle.truffle.api.vm.PolyglotEngine.evalImpl(PolyglotEngine.java:662)
        at com.oracle.truffle.api.vm.PolyglotEngine.eval(PolyglotEngine.java:646)
        at HelloPolyglotWorld.main(HelloPolyglotWorld.java:19)
Caused by: com.oracle.truffle.api.TruffleStackTrace$LazyStackTrace
Caused by: com.oracle.truffle.api.TruffleStackTrace
        at com.oracle.truffle.api.TruffleStackTrace.fillIn(TruffleStackTrace.java:170)
        at com.oracle.truffle.api.TruffleStackTrace.addStackFrameInfo(TruffleStackTrace.java:233)
        at com.oracle.truffle.api.TruffleLanguage$LanguageImpl.onThrowable(TruffleLanguage.java:1845)
        at com.oracle.truffle.api.impl.TVMCI.onThrowable(TVMCI.java:177)
        at com.oracle.truffle.api.impl.DefaultCallTarget.call(DefaultCallTarget.java:83)
        at com.oracle.truffle.api.vm.PolyglotEngine$1.compute(PolyglotEngine.java:659)
        at com.oracle.truffle.api.vm.ComputeInExecutor.run(ComputeInExecutor.java:96)
        at com.oracle.truffle.api.vm.ComputeInExecutor.perform(ComputeInExecutor.java:83)
        at com.oracle.truffle.api.vm.PolyglotEngine.evalImpl(PolyglotEngine.java:662)
        at com.oracle.truffle.api.vm.PolyglotEngine.eval(PolyglotEngine.java:646)
        at HelloPolyglotWorld.main(HelloPolyglotWorld.java:19)
himanshumps commented 6 years ago

After adding sulong.jar

/opt/graalvm-0.31/bin/java -cp .:../jre/lib/truffle/truffle-api.jar:../jre/lib/boot/graal-sdk.jar:../jre/languages/js/graaljs.jar:../jre/languages/R/fastr.jar:../jre/languages/python/graalpython.jar:../jre/languages/python/graalpython-env.jar:../jre/languages/ruby/truffleruby.jar:../jre/languages/ruby/truffleruby-launcher.jar:../jre/languages/llvm/sulong.jar HelloPolyglotWorld
Output: line 11
line 22
line 33

[ruby] PERFORMANCE this JVM does not have the Graal compiler - performance will be limited - see doc/user/using-graalvm.md
[ruby] WARNING TruffleRuby's home was not explicitly set.
* Launcher not set, home path could not be derived.
* GraalVM home not found.
* Try to set home using -Xhome=PATH option.
Exception in thread "main" java.lang.IllegalStateException: java.lang.IllegalStateException: No language for MIME type application/x-native found. Supported types: [application/x-r, text/x-r, application/x-llvm-ir-bitcode-base64, application/x-sharedlib, application/x-python, application/x-sulong, application/x-sulong-library, application/javascript, text/javascript, application/x-executable, application/x-ruby, application/x-llvm-ir-bitcode, application/js-regex]
        at com.oracle.truffle.api.vm.ComputeInExecutor.exceptionCheck(ComputeInExecutor.java:65)
        at com.oracle.truffle.api.vm.ComputeInExecutor.perform(ComputeInExecutor.java:84)
        at com.oracle.truffle.api.vm.PolyglotEngine.evalImpl(PolyglotEngine.java:662)
        at com.oracle.truffle.api.vm.PolyglotEngine.eval(PolyglotEngine.java:646)
        at HelloPolyglotWorld.main(HelloPolyglotWorld.java:19)
Caused by: java.lang.IllegalStateException: No language for MIME type application/x-native found. Supported types: [application/x-r, text/x-r, application/x-llvm-ir-bitcode-base64, application/x-sharedlib, application/x-python, application/x-sulong, application/x-sulong-library, application/javascript, text/javascript, application/x-executable, application/x-ruby, application/x-llvm-ir-bitcode, application/js-regex]
        at com.oracle.truffle.api.vm.PolyglotEngine.findLanguage(PolyglotEngine.java:905)
        at com.oracle.truffle.api.vm.PolyglotEngine.access$1200(PolyglotEngine.java:203)
        at com.oracle.truffle.api.vm.PolyglotEngine$LegacyEngineImpl.getEnvForLanguage(PolyglotEngine.java:1513)
        at com.oracle.truffle.api.TruffleLanguage$Env.parse(TruffleLanguage.java:1409)
        at org.truffleruby.platform.TruffleNFIPlatform.<init>(TruffleNFIPlatform.java:40)
        at org.truffleruby.RubyContext.<init>(RubyContext.java:208)
        at org.truffleruby.RubyLanguage.createContext(RubyLanguage.java:113)
        at org.truffleruby.RubyLanguage.createContext(RubyLanguage.java:47)
        at com.oracle.truffle.api.TruffleLanguage$LanguageImpl.createEnvContext(TruffleLanguage.java:1805)
        at com.oracle.truffle.api.vm.PolyglotEngine$Language.getEnv(PolyglotEngine.java:1465)
        at com.oracle.truffle.api.vm.PolyglotRootNode$EvalRootNode.initialize(PolyglotRootNode.java:268)
        at com.oracle.truffle.api.vm.PolyglotRootNode$EvalRootNode.executeImpl(PolyglotRootNode.java:235)
        at com.oracle.truffle.api.vm.PolyglotRootNode.execute(PolyglotRootNode.java:73)
        at com.oracle.truffle.api.impl.DefaultCallTarget.call(DefaultCallTarget.java:81)
        at com.oracle.truffle.api.vm.PolyglotEngine$1.compute(PolyglotEngine.java:659)
        at com.oracle.truffle.api.vm.ComputeInExecutor.run(ComputeInExecutor.java:96)
        at com.oracle.truffle.api.vm.ComputeInExecutor.perform(ComputeInExecutor.java:83)
        ... 3 more
Caused by: com.oracle.truffle.api.TruffleStackTrace$LazyStackTrace
Caused by: com.oracle.truffle.api.TruffleStackTrace
        at com.oracle.truffle.api.TruffleStackTrace.fillIn(TruffleStackTrace.java:170)
        at com.oracle.truffle.api.TruffleStackTrace.addStackFrameInfo(TruffleStackTrace.java:233)
        at com.oracle.truffle.api.TruffleLanguage$LanguageImpl.onThrowable(TruffleLanguage.java:1845)
        at com.oracle.truffle.api.impl.TVMCI.onThrowable(TVMCI.java:177)
        at com.oracle.truffle.api.impl.DefaultCallTarget.call(DefaultCallTarget.java:83)
        at com.oracle.truffle.api.vm.PolyglotEngine$1.compute(PolyglotEngine.java:659)
        at com.oracle.truffle.api.vm.ComputeInExecutor.run(ComputeInExecutor.java:96)
        at com.oracle.truffle.api.vm.ComputeInExecutor.perform(ComputeInExecutor.java:83)
        at com.oracle.truffle.api.vm.PolyglotEngine.evalImpl(PolyglotEngine.java:662)
        at com.oracle.truffle.api.vm.PolyglotEngine.eval(PolyglotEngine.java:646)
        at HelloPolyglotWorld.main(HelloPolyglotWorld.java:19)
chrisseaton commented 6 years ago

Ok well that's progress 😀 I'll have to ask someone else to help me work out why the native library part of NFI isn't being found.

rschatz commented 6 years ago

It doesn't find the libtrufflenfi.so. That's weird, because this library is installed in jre/lib/amd64, which should always be on the default library path of the VM. Also, the performance warning about Graal not being found is weird. Looks like there is something messed up with the search paths in general.

As a workaround, it is possible to specify the absolute path to the library using -Dtruffle.nfi.library=/opt/graalvm-0.31/jre/lib/amd64/libtrufflenfi.so. But that will solve only the problem with the NFI, not the problem that Graal is not found.

himanshumps commented 6 years ago

I took the graalvm tar from http://www.oracle.com/technetwork/oracle-labs/program-languages/downloads/index.html and unzipped it in a folder and using that to run this. Are there any environment variables that I should set in order to make it work ?

chumer commented 6 years ago

We are in the progress of deprecating the API you are using for 0.32. Please use the polyglot API instead. Here are some examples: https://github.com/oracle/graal/blob/master/sdk/docs/PolyglotEmbedding.md

The Polyglot API is ready to be used in GraalVM. No need to put Truffle on the classpath. If you do we get a classpath conflict. See the examples in graalvm-0.31/examples/embed

All languages in graalvm-0.31/jre/languages are loaded automatically. No need to put them on the classpath.

chumer commented 6 years ago

Please close if this fixes your problem.

chumer commented 6 years ago

I've translated the example for your convenience:

import org.graalvm.polyglot.*;
import java.io.ByteArrayOutputStream;
public class HelloPolyglotWorld {
 public static void main(String[] args) throws Exception {
   ByteArrayOutputStream yourOutput = new ByteArrayOutputStream();
   Context context = Context.newBuilder()
           .out(yourOutput)
           .err(yourOutput)
           .build();
   context.eval("js", "[1,2,0b11].forEach(v => print(`line ${v}` + v))");
   String s = yourOutput.toString("UTF-8");
   System.out.println("Output: " + s);
   yourOutput.reset();

   context.eval("ruby", "puts 'hello'");
   s = yourOutput.toString("UTF-8");
   System.out.println("Output: " + s);
   context.close();
  }
} 

I've successfully tested this on GraalVM 0.31.

himanshumps commented 6 years ago

Can you please share the java command that you used to run this along with any environment variables that need to be set.

I am ok with this ticket being closed.

chumer commented 6 years ago

The total number of commands I run to test this:

graalvm-0.31/bin/javac HelloPolyglotWorld.java
graalvm-0.31/bin/java HelloPolyglotWorld

No environment variables set.