Skip to content

[GR-65280] Make sure truffle context is entered. #11251

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,10 @@ public Object allocateInstance(KlassRef klass) {

@Override
public void steppingInProgress(Thread t, boolean value) {
Object previous = null;
try {
previous = controller.enterTruffleContext();
performInContext(() -> {
context.getLanguage().getThreadLocalStateFor(t).setSteppingInProgress(value);
} finally {
controller.leaveTruffleContext(previous);
}
return null;
});
}

@Override
Expand All @@ -302,14 +299,14 @@ public Object[] getAllGuestThreads() {
@Override
public String getStringValue(Object object) {
if (object instanceof StaticObject staticObject) {
return (String) InteropLibrary.getUncached().toDisplayString(staticObject, false);
return performInContext(() -> (String) UNCACHED.toDisplayString(staticObject, false));
}
return object.toString();
}

@Override
public MethodVersionRef getMethodFromRootNode(RootNode root) {
if (root != null && root instanceof EspressoRootNode) {
if (root instanceof EspressoRootNode) {
return ((EspressoRootNode) root).getMethodVersion();
}
return null;
Expand Down Expand Up @@ -414,15 +411,17 @@ public int getArrayLength(Object array) {
StaticObject staticObject = (StaticObject) array;
EspressoLanguage language = context.getLanguage();
if (staticObject.isForeignObject()) {
try {
long arrayLength = UNCACHED.getArraySize(staticObject.rawForeignObject(language));
if (arrayLength > Integer.MAX_VALUE) {
return -1;
long arrayLength = performInContext(() -> {
try {
return UNCACHED.getArraySize(staticObject.rawForeignObject(language));
} catch (UnsupportedMessageException e) {
return (long) -1;
}
return (int) arrayLength;
} catch (UnsupportedMessageException e) {
});
if (arrayLength > Integer.MAX_VALUE) {
return -1;
}
return (int) arrayLength;
}
return staticObject.length(language);
}
Expand Down Expand Up @@ -476,17 +475,19 @@ public Object getArrayValue(Object array, int index) {
Klass componentType = ((ArrayKlass) arrayRef.getKlass()).getComponentType();
Meta meta = componentType.getMeta();
if (arrayRef.isForeignObject()) {
Object value = null;
try {
value = InteropLibrary.getUncached().readArrayElement(arrayRef.rawForeignObject(arrayRef.getKlass().getLanguage()), index);
return ToEspressoNode.getUncachedToEspresso(componentType, meta).execute(value);
} catch (UnsupportedMessageException e) {
throw EspressoError.shouldNotReachHere("readArrayElement on a non-array foreign object", e);
} catch (InvalidArrayIndexException e) {
throw meta.throwExceptionWithMessage(meta.java_lang_ArrayIndexOutOfBoundsException, e.getMessage());
} catch (UnsupportedTypeException e) {
throw meta.throwExceptionWithMessage(meta.java_lang_ClassCastException, "%s cannot be cast to %s", value, componentType.getTypeAsString());
}
return performInContext(() -> {
Object value = null;
try {
value = UNCACHED.readArrayElement(arrayRef.rawForeignObject(arrayRef.getKlass().getLanguage()), index);
return ToEspressoNode.getUncachedToEspresso(componentType, meta).execute(value);
} catch (UnsupportedMessageException e) {
throw EspressoError.shouldNotReachHere("readArrayElement on a non-array foreign object", e);
} catch (InvalidArrayIndexException e) {
throw meta.throwExceptionWithMessage(meta.java_lang_ArrayIndexOutOfBoundsException, e.getMessage());
} catch (UnsupportedTypeException e) {
throw meta.throwExceptionWithMessage(meta.java_lang_ClassCastException, "%s cannot be cast to %s", value, componentType.getTypeAsString());
}
});
} else if (componentType.isPrimitive()) {
// primitive array type needs wrapping
Object boxedArray = getUnboxedArray(array);
Expand All @@ -502,19 +503,24 @@ public void setArrayValue(Object array, int index, Object value) {
Klass componentType = ((ArrayKlass) arrayRef.getKlass()).getComponentType();
Meta meta = componentType.getMeta();
if (arrayRef.isForeignObject()) {
try {
Object unWrappedValue = value;
if (value instanceof StaticObject staticObject) {
unWrappedValue = staticObject.isForeignObject() ? staticObject.rawForeignObject(meta.getLanguage()) : staticObject;
}
InteropLibrary.getUncached().writeArrayElement(arrayRef.rawForeignObject(arrayRef.getKlass().getLanguage()), index, unWrappedValue);
} catch (UnsupportedMessageException e) {
throw EspressoError.shouldNotReachHere("writeArrayElement on a non-array foreign object", e);
} catch (InvalidArrayIndexException e) {
throw meta.throwExceptionWithMessage(meta.java_lang_ArrayIndexOutOfBoundsException, e.getMessage());
} catch (UnsupportedTypeException e) {
throw meta.throwExceptionWithMessage(meta.java_lang_ClassCastException, "%s cannot be cast to %s", value, componentType.getTypeAsString());
Object unWrappedValue;
if (value instanceof StaticObject staticObject) {
unWrappedValue = staticObject.isForeignObject() ? staticObject.rawForeignObject(meta.getLanguage()) : staticObject;
} else {
unWrappedValue = value;
}
performInContext(() -> {
try {
UNCACHED.writeArrayElement(arrayRef.rawForeignObject(arrayRef.getKlass().getLanguage()), index, unWrappedValue);
return null;
} catch (UnsupportedMessageException e) {
throw EspressoError.shouldNotReachHere("writeArrayElement on a non-array foreign object", e);
} catch (UnsupportedTypeException e) {
throw meta.throwExceptionWithMessage(meta.java_lang_ClassCastException, "%s cannot be cast to %s", value, componentType.getTypeAsString());
} catch (InvalidArrayIndexException e) {
throw meta.throwExceptionWithMessage(meta.java_lang_ArrayIndexOutOfBoundsException, e.getMessage());
}
});
} else if (componentType.isPrimitive()) {
// primitive array type needs wrapping
Object boxedArray = getUnboxedArray(array);
Expand Down Expand Up @@ -601,24 +607,18 @@ public boolean isInstanceOf(Object object, KlassRef klass) {

@Override
public void stopThread(Object guestThread, Object guestThrowable) {
Object previous = null;
try {
previous = controller.enterTruffleContext();
performInContext(() -> {
context.getThreadAccess().stop((StaticObject) guestThread, (StaticObject) guestThrowable);
} finally {
controller.leaveTruffleContext(previous);
}
return null;
});
}

@Override
public void interruptThread(Object thread) {
Object previous = null;
try {
previous = controller.enterTruffleContext();
performInContext(() -> {
context.interruptThread((StaticObject) thread);
} finally {
controller.leaveTruffleContext(previous);
}
return null;
});
}

@Override
Expand All @@ -628,13 +628,10 @@ public boolean systemExitImplemented() {

@Override
public void exit(int exitCode) {
Object previous = null;
try {
previous = controller.enterTruffleContext();
performInContext(() -> {
context.truffleExit(null, exitCode);
} finally {
controller.leaveTruffleContext(previous);
}
return null;
});
}

@Override
Expand Down Expand Up @@ -834,4 +831,19 @@ public ModuleRef[] getAllModulesRefs() {
public synchronized int redefineClasses(List<RedefineInfo> redefineInfos) {
return context.getClassRedefinition().redefineClasses(redefineInfos, false, true);
}

private <R> R performInContext(InContextAction<R> action) {
Object previous = null;
try {
previous = controller.enterTruffleContext();
return action.call();
} finally {
controller.leaveTruffleContext(previous);
}
}

@FunctionalInterface
private interface InContextAction<R> {
R call();
}
}