use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.
the class VM method JVM_MonitorWait.
@VmImpl(isJni = true)
@TruffleBoundary
@SuppressFBWarnings(value = { "IMSE" }, justification = "Not dubious, .wait is just forwarded from the guest.")
@SuppressWarnings("try")
public void JVM_MonitorWait(@JavaType(Object.class) StaticObject self, long timeout, @Inject Meta meta, @Inject SubstitutionProfiler profiler) {
EspressoContext context = getContext();
StaticObject currentThread = context.getCurrentThread();
State state = timeout > 0 ? State.TIMED_WAITING : State.WAITING;
try (Transition transition = Transition.transition(context, state)) {
if (context.EnableManagement) {
// Locks bookkeeping.
meta.HIDDEN_THREAD_BLOCKED_OBJECT.setHiddenObject(currentThread, self);
Target_java_lang_Thread.incrementThreadCounter(currentThread, meta.HIDDEN_THREAD_WAITED_COUNT);
}
final boolean report = context.shouldReportVMEvents();
if (report) {
context.reportMonitorWait(self, timeout);
}
boolean timedOut = !InterpreterToVM.monitorWait(self.getLock(getContext()), timeout);
if (report) {
context.reportMonitorWaited(self, timedOut);
}
} catch (GuestInterruptedException e) {
profiler.profile(0);
if (getThreadAccess().isInterrupted(currentThread, true)) {
throw meta.throwExceptionWithMessage(meta.java_lang_InterruptedException, e.getMessage());
}
getThreadAccess().fullSafePoint(currentThread);
} catch (IllegalMonitorStateException e) {
profiler.profile(1);
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalMonitorStateException, e.getMessage());
} catch (IllegalArgumentException e) {
profiler.profile(2);
throw meta.throwExceptionWithMessage(meta.java_lang_IllegalArgumentException, e.getMessage());
} finally {
if (context.EnableManagement) {
meta.HIDDEN_THREAD_BLOCKED_OBJECT.setHiddenObject(currentThread, null);
}
}
}
use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.
the class VM method JVM_DoPrivileged.
@VmImpl(isJni = true)
@SuppressWarnings("unused")
@JavaType(Object.class)
public StaticObject JVM_DoPrivileged(@JavaType(Class.class) StaticObject cls, /* PrivilegedAction or PrivilegedActionException */
@JavaType(Object.class) StaticObject action, @JavaType(AccessControlContext.class) StaticObject context, boolean wrapException, @Inject Meta meta, @Inject SubstitutionProfiler profiler) {
if (StaticObject.isNull(action)) {
profiler.profile(0);
throw meta.throwNullPointerException();
}
FrameInstance callerFrame = getCallerFrame(1, false, meta);
assert callerFrame != null : "No caller ?";
Klass caller = getMethodFromFrame(callerFrame).getDeclaringKlass();
StaticObject acc = context;
if (!StaticObject.isNull(context)) {
if (!isAuthorized(context, caller)) {
acc = createDummyACC();
}
}
Method run = action.getKlass().lookupMethod(Name.run, Signature.Object);
if (run == null || !run.isPublic() || run.isStatic()) {
profiler.profile(1);
throw meta.throwException(meta.java_lang_InternalError);
}
// Prepare the privileged stack
PrivilegedStack stack = getPrivilegedStack();
stack.push(callerFrame, acc, caller);
// Execute the action.
StaticObject result;
try {
result = (StaticObject) run.invokeDirect(action);
} catch (EspressoException e) {
profiler.profile(2);
if (meta.java_lang_Exception.isAssignableFrom(e.getGuestException().getKlass()) && !meta.java_lang_RuntimeException.isAssignableFrom(e.getGuestException().getKlass())) {
profiler.profile(3);
StaticObject wrapper = meta.java_security_PrivilegedActionException.allocateInstance();
getMeta().java_security_PrivilegedActionException_init_Exception.invokeDirect(wrapper, e.getGuestException());
throw meta.throwException(wrapper);
}
profiler.profile(4);
throw e;
} finally {
stack.pop();
}
return result;
}
use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.
the class VM method JVM_AssertionStatusDirectives.
// endregion array
// region assertion
/**
* Espresso only supports basic -ea and -esa options. Complex per-class/package filters are
* unsupported.
*/
@VmImpl(isJni = true)
@TruffleBoundary
@JavaType(internalName = "Ljava/lang/AssertionStatusDirectives;")
public StaticObject JVM_AssertionStatusDirectives(@SuppressWarnings("unused") @JavaType(Class.class) StaticObject unused) {
Meta meta = getMeta();
StaticObject instance = meta.java_lang_AssertionStatusDirectives.allocateInstance();
meta.java_lang_AssertionStatusDirectives.lookupMethod(Name._init_, Signature._void).invokeDirect(instance);
meta.java_lang_AssertionStatusDirectives_classes.set(instance, meta.java_lang_String.allocateReferenceArray(0));
meta.java_lang_AssertionStatusDirectives_classEnabled.set(instance, meta._boolean.allocatePrimitiveArray(0));
meta.java_lang_AssertionStatusDirectives_packages.set(instance, meta.java_lang_String.allocateReferenceArray(0));
meta.java_lang_AssertionStatusDirectives_packageEnabled.set(instance, meta._boolean.allocatePrimitiveArray(0));
boolean ea = getContext().getEnv().getOptions().get(EspressoOptions.EnableAssertions);
meta.java_lang_AssertionStatusDirectives_deflt.set(instance, ea);
return instance;
}
use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.
the class StackWalk method fetchFirstBatch.
/**
* initializes the stack walking, and anchors the Frame Walker instance to a particular frame
* and fetches the first batch of frames requested by guest.
*
* Upon return, unanchors the Frame Walker, and it is then not possible to continue walking for
* this walker anymore.
*
* @return The result of invoking guest
* {@code java.lang.StackStreamFactory.AbstractStackWalker#doStackWalk(long, int, int, int,
* int)} .
*/
public StaticObject fetchFirstBatch(@JavaType(internalName = "Ljava/lang/StackStreamFactory;") StaticObject stackStream, long mode, int skipframes, int batchSize, int startIndex, @JavaType(Object[].class) StaticObject frames, Meta meta) {
assert synchronizedConstants(meta);
FrameWalker fw = new FrameWalker(meta, mode);
fw.init(skipframes, batchSize, startIndex);
Integer decodedOrNull = fw.doStackWalk(frames);
int decoded = decodedOrNull == null ? fw.decoded() : decodedOrNull;
if (decoded < 1) {
throw meta.throwException(meta.java_lang_InternalError);
}
register(fw);
Object result = meta.java_lang_AbstractStackWalker_doStackWalk.invokeDirect(stackStream, fw.anchor, skipframes, batchSize, startIndex, startIndex + decoded);
unAnchor(fw);
return (StaticObject) result;
}
use of com.oracle.truffle.espresso.runtime.StaticObject in project graal by oracle.
the class VM method DetachCurrentThread.
@VmImpl
@TruffleBoundary
public int DetachCurrentThread(@Inject EspressoContext context) {
StaticObject currentThread = context.getCurrentThread();
if (currentThread == null) {
return JNI_OK;
}
getLogger().fine(() -> {
String guestName = getThreadAccess().getThreadName(currentThread);
return "DetachCurrentThread: " + guestName;
});
// HotSpot will wait forever if the current VM this thread was attached to has exited
// Should we reproduce this behaviour?
Method lastJavaMethod = Truffle.getRuntime().iterateFrames(new FrameInstanceVisitor<Method>() {
@Override
public Method visitFrame(FrameInstance frameInstance) {
Method method = getMethodFromFrame(frameInstance);
if (method != null && method.getContext() == context) {
return method;
}
return null;
}
});
if (lastJavaMethod != null) {
// this thread is executing
getLogger().warning(() -> {
String guestName = getThreadAccess().getThreadName(currentThread);
return "DetachCurrentThread called while thread is still executing Java code (" + guestName + ")";
});
return JNI_ERR;
}
StaticObject pendingException = jniEnv.getPendingException();
jniEnv.clearPendingException();
Meta meta = context.getMeta();
try {
if (pendingException != null) {
meta.java_lang_Thread_dispatchUncaughtException.invokeDirect(currentThread, pendingException);
}
getThreadAccess().terminate(currentThread);
} catch (EspressoException e) {
try {
StaticObject ex = e.getGuestException();
String exception = ex.getKlass().getExternalName();
String threadName = getThreadAccess().getThreadName(currentThread);
context.getLogger().warning(String.format("Exception: %s thrown while terminating thread \"%s\"", exception, threadName));
Method printStackTrace = ex.getKlass().lookupMethod(Name.printStackTrace, Signature._void);
printStackTrace.invokeDirect(ex);
} catch (EspressoException ee) {
String exception = ee.getGuestException().getKlass().getExternalName();
context.getLogger().warning(String.format("Exception: %s thrown while trying to print stack trace", exception));
} catch (EspressoExitException ee) {
// ignore
}
} catch (EspressoExitException e) {
// ignore
} catch (Throwable t) {
context.getLogger().severe("Host exception thrown while trying to terminate thread");
t.printStackTrace();
}
return JNI_OK;
}
Aggregations