Skip to content

Commit eac79cd

Browse files
committed
Avoid illegal reflective access to Thread.isInterrupted(boolean).
This is a private method, only used from the current host Thread; the reflective call is replaced with Thread.interrupted().
1 parent 0b18954 commit eac79cd

File tree

1 file changed

+12
-29
lines changed

1 file changed

+12
-29
lines changed

espresso/src/com.oracle.truffle.espresso/src/com/oracle/truffle/espresso/substitutions/Target_java_lang_Thread.java

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
package com.oracle.truffle.espresso.substitutions;
2525

26-
import java.lang.reflect.InvocationTargetException;
2726
import java.util.Arrays;
27+
import java.util.concurrent.atomic.AtomicLong;
2828

2929
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3030
import com.oracle.truffle.api.nodes.DirectCallNode;
@@ -61,33 +61,23 @@
6161
// @formatter:on
6262
@EspressoSubstitutions
6363
public final class Target_java_lang_Thread {
64-
private static final java.lang.reflect.Method isInterrupted;
65-
static {
66-
try {
67-
isInterrupted = Thread.class.getDeclaredMethod("isInterrupted", boolean.class);
68-
isInterrupted.setAccessible(true);
69-
} catch (Throwable e) {
70-
throw EspressoError.shouldNotReachHere();
71-
}
72-
}
7364

7465
public static void incrementThreadCounter(StaticObject thread, Field hiddenField) {
7566
assert hiddenField.isHidden();
76-
Long counter = (Long) hiddenField.getHiddenObject(thread);
77-
if (counter == null) {
78-
counter = 0L;
67+
AtomicLong atomicCounter = (AtomicLong) hiddenField.getHiddenObject(thread);
68+
if (atomicCounter == null) {
69+
hiddenField.setHiddenObject(thread, atomicCounter = new AtomicLong());
7970
}
80-
++counter;
81-
hiddenField.setHiddenObject(thread, counter);
71+
atomicCounter.incrementAndGet();
8272
}
8373

8474
public static long getThreadCounter(StaticObject thread, Field hiddenField) {
8575
assert hiddenField.isHidden();
86-
Long counter = (Long) hiddenField.getHiddenObject(thread);
87-
if (counter == null) {
88-
counter = 0L;
76+
AtomicLong atomicCounter = (AtomicLong) hiddenField.getHiddenObject(thread);
77+
if (atomicCounter == null) {
78+
return 0L;
8979
}
90-
return counter;
80+
return atomicCounter.get();
9181
}
9282

9383
public enum State {
@@ -405,28 +395,21 @@ public static void interrupt0(@Host(Object.class) StaticObject self) {
405395
hostThread.interrupt();
406396
}
407397

398+
@TruffleBoundary
408399
@Substitution(hasReceiver = true)
409400
public static boolean isInterrupted(@Host(Thread.class) StaticObject self, boolean clear) {
410401
boolean result = checkInterrupt(self);
411402
if (clear) {
412403
Thread hostThread = getHostFromGuestThread(self);
404+
EspressoError.guarantee(hostThread == Thread.currentThread(), "Thread#isInterrupted(true) is only supported for the current thread.");
413405
if (hostThread != null && hostThread.isInterrupted()) {
414-
try {
415-
callHostThreadIsInterrupted(hostThread);
416-
} catch (Throwable e) {
417-
throw EspressoError.shouldNotReachHere(e);
418-
}
406+
Thread.interrupted();
419407
}
420408
setInterrupt(self, false);
421409
}
422410
return result;
423411
}
424412

425-
@TruffleBoundary
426-
private static void callHostThreadIsInterrupted(Thread hostThread) throws IllegalAccessException, InvocationTargetException {
427-
isInterrupted.invoke(hostThread, true);
428-
}
429-
430413
@TruffleBoundary
431414
@SuppressWarnings({"unused"})
432415
@Substitution(hasReceiver = true)

0 commit comments

Comments
 (0)