Compiling GraalVM on Ubuntu 18.04 from master and using the native API provided by libpolyglot.so causes a NullPointerException during a call to poly_create_context.
Source code of the test program:
~~~~
int main(int argc, char* argv[])
{
//Keep the compiler quiet
(void)argc;
(void)argv;
poly_isolate isolate;
poly_thread thread;
poly_status status;
status = poly_create_isolate(NULL, &isolate, &thread);
if (status != poly_ok) {
printf("Status: %d\n", status);
}
else {
printf("Status is ok. Isolate: %p Thread: %p\n", isolate, thread); //Just to see if we get something other than NULL here
}
const char langJS[] = "js";
const char* langs[] = {langJS};
poly_context context;
// Attempted with langs = NULL & size = 0 here too.
if ((status = poly_create_context(thread, langs, 1, &context)) != poly_ok) {
printf("Failed to create context: %d\n", status);
const poly_extended_error_info* errInfo;
poly_get_last_error_info(thread, &errInfo);
printf("Error message: %s\n", errInfo->error_message);
return 1;
}
return 0;
}
__The way Graal was built:__
~~~
git clone the repo
cd graal/vm
mx --env lp_js sforceimports
mx --env lp_js build
~
Contents of lp_js environment:
~~~
DYNAMIC_IMPORTS=/substratevm,/tools,/graal-js
DISABLE_LIBPOLYGLOT=false
DISABLE_POLYGLOT=true
DEBUG_IMAGES=true
FORCE_BASH_LAUNCHERS=true
~
__Compiling the test code:__
~~~
g++ -L$(POLY_LIB_PATH) -I$(POLY_LIB_PATH) -Wall -o test main.cpp -lpolyglot
~
where $(POLY_LIB_PATH) is the path to the $(mx --env lp_js graalvm-home)/jre/lib/polyglot directory.
__Running the test code:__
export LD_LIBRARY_PATH=$(mx --env lp_js graalvm-home)/jre/lib/polyglot
./test
__Output:__
Error code 9 is poly_generic_failure
~~~
./test
Status is ok. Isolate: 0x7f6afcf7c000 Thread: 0x557c1301e260
Failed to create context: 9
Error message: java.lang.NullPointerException
The full stack trace is:
org.graalvm.polyglot.PolyglotException: java.lang.NullPointerException
at com.oracle.truffle.polyglot.PolyglotReferences$SingleContext.assertDirectContextAccess(PolyglotReferences.java:216)
at com.oracle.truffle.polyglot.PolyglotReferences$SingleContext.get(PolyglotReferences.java:211)
at com.oracle.truffle.polyglot.PolyglotReferences$AssumeSingleContext.get(PolyglotReferences.java:261)
at com.oracle.truffle.js.runtime.JSContext.getRealm(JSContext.java:753)
at com.oracle.truffle.js.runtime.objects.JSObject.create(JSObject.java:131)
at com.oracle.truffle.js.runtime.builtins.JSArray.createImpl(JSArray.java:161)
at com.oracle.truffle.js.runtime.builtins.JSArray.create(JSArray.java:150)
at com.oracle.truffle.js.runtime.builtins.JSArray.create(JSArray.java:146)
at com.oracle.truffle.js.runtime.builtins.JSArray.create(JSArray.java:142)
at com.oracle.truffle.js.runtime.builtins.JSArray.create(JSArray.java:138)
at com.oracle.truffle.js.runtime.builtins.JSArray.createConstant(JSArray.java:97)
at com.oracle.truffle.js.runtime.JSRealm.setArguments(JSRealm.java:1244)
at com.oracle.truffle.js.runtime.JSRealm.patchContext(JSRealm.java:1271)
at com.oracle.truffle.js.lang.JavaScriptLanguage.patchContext(JavaScriptLanguage.java:452)
at com.oracle.truffle.js.lang.JavaScriptLanguage.patchContext(JavaScriptLanguage.java:126)
at com.oracle.truffle.api.TruffleLanguage$LanguageImpl.patchEnvContext(TruffleLanguage.java:2704)
at com.oracle.truffle.polyglot.PolyglotLanguageContext.patch(PolyglotLanguageContext.java:522)
at com.oracle.truffle.polyglot.PolyglotContextImpl.patch(PolyglotContextImpl.java:1162)
at com.oracle.truffle.polyglot.PolyglotEngineImpl.loadPreinitializedContext(PolyglotEngineImpl.java:1177)
at com.oracle.truffle.polyglot.PolyglotEngineImpl.createContext(PolyglotEngineImpl.java:1150)
at org.graalvm.polyglot.Context$Builder.build(Context.java:1305)
at org.graalvm.polyglot.Context.create(Context.java:697)
at org.graalvm.polyglot.nativeapi.PolyglotNativeAPI.lambda$poly_create_context$16(PolyglotNativeAPI.java:422)
at org.graalvm.polyglot.nativeapi.PolyglotNativeAPI.withHandledErrors(PolyglotNativeAPI.java:1627)
at org.graalvm.polyglot.nativeapi.PolyglotNativeAPI.poly_create_context(PolyglotNativeAPI.java:419)
Original Internal Error:
java.lang.NullPointerException
at com.oracle.truffle.polyglot.PolyglotReferences$SingleContext.assertDirectContextAccess(PolyglotReferences.java:216)
at com.oracle.truffle.polyglot.PolyglotReferences$SingleContext.get(PolyglotReferences.java:211)
at com.oracle.truffle.polyglot.PolyglotReferences$AssumeSingleContext.get(PolyglotReferences.java:261)
at com.oracle.truffle.js.runtime.JSContext.getRealm(JSContext.java:753)
at com.oracle.truffle.js.runtime.objects.JSObject.create(JSObject.java:131)
at com.oracle.truffle.js.runtime.builtins.JSArray.createImpl(JSArray.java:161)
at com.oracle.truffle.js.runtime.builtins.JSArray.create(JSArray.java:150)
at com.oracle.truffle.js.runtime.builtins.JSArray.create(JSArray.java:146)
at com.oracle.truffle.js.runtime.builtins.JSArray.create(JSArray.java:142)
at com.oracle.truffle.js.runtime.builtins.JSArray.create(JSArray.java:138)
at com.oracle.truffle.js.runtime.builtins.JSArray.createConstant(JSArray.java:97)
at com.oracle.truffle.js.runtime.JSRealm.setArguments(JSRealm.java:1244)
at com.oracle.truffle.js.runtime.JSRealm.patchContext(JSRealm.java:1271)
at com.oracle.truffle.js.lang.JavaScriptLanguage.patchContext(JavaScriptLanguage.java:452)
at com.oracle.truffle.js.lang.JavaScriptLanguage.patchContext(JavaScriptLanguage.java:126)
at com.oracle.truffle.api.TruffleLanguage$LanguageImpl.patchEnvContext(TruffleLanguage.java:2704)
at com.oracle.truffle.polyglot.PolyglotLanguageContext.patch(PolyglotLanguageContext.java:522)
at com.oracle.truffle.polyglot.PolyglotContextImpl.patch(PolyglotContextImpl.java:1162)
at com.oracle.truffle.polyglot.PolyglotEngineImpl.loadPreinitializedContext(PolyglotEngineImpl.java:1177)
at com.oracle.truffle.polyglot.PolyglotEngineImpl.createContext(PolyglotEngineImpl.java:1150)
at org.graalvm.polyglot.Context$Builder.build(Context.java:1305)
at org.graalvm.polyglot.Context.create(Context.java:697)
at org.graalvm.polyglot.nativeapi.PolyglotNativeAPI.lambda$poly_create_context$16(PolyglotNativeAPI.java:422)
at org.graalvm.polyglot.nativeapi.PolyglotNativeAPI.withHandledErrors(PolyglotNativeAPI.java:1627)
at org.graalvm.polyglot.nativeapi.PolyglotNativeAPI.poly_create_context(PolyglotNativeAPI.java:419)
Caused by: Attached Guest Language Frames (0)
~
Changing DEBUG_IMAGES to false in the lp_js environment makes the test code work.
I could reproduce the issue. Working on a fix.
I can confirm it is fixed. Thank you!