corda/test/Threads.java
Joel Dice 7a4cae0dde load bootstrap classes in findInterfaceMethod
In afbd4ff, I made a low-risk, but very specific fix for a more
general problem: "bootstrap" classes (i.e. classes which the VM has
built-in knowledge of) need to be loaded from the classpath before any
of their methods are called.  Based on recent testing, I found there were
more cases than I previously thought where the VM tries to call methods on
"unloaded" bootstrap classes, so we needed a more general solution to
the problem.

This commit addresses it by closing the last (known) loophole by which
methods might be called on bootstrap classes: invokeinterface, and its
helper method findInterfaceMethod.  The fix is to check for bootstrap
classes in findInterfaceMethod and load the full versions if
necessary.  This process may lead to garbage collection and/or thrown
exceptions, which made me nervous about cases of direct or indirect
calls to findInterfaceMethod not expecting those events, which is why
I hadn't used that approach earlier.  However, it turns out there were
only a few places that made non-GC-safe calls to findInterfaceMethod,
and a bit of code rearrangement fixed that.
2015-02-06 13:51:32 -07:00

95 lines
2.3 KiB
Java

public class Threads implements Runnable {
private static boolean success = false;
private static void expect(boolean v) {
if (! v) throw new RuntimeException();
}
public static void main(String[] args) throws Exception {
((Thread.UncaughtExceptionHandler) Thread.currentThread().getThreadGroup())
.uncaughtException(Thread.currentThread(), new Exception());
{ Threads test = new Threads();
Thread thread = new Thread(test);
synchronized (test) {
thread.start();
test.wait();
}
}
{ Thread thread = new Thread() {
public void run() {
while (true) {
System.out.print(".");
try {
sleep(1000);
} catch (Exception e) {
System.out.println("thread interrupted? " + interrupted());
break;
}
}
}
};
thread.start();
System.out.println("\nAbout to interrupt...");
thread.interrupt();
System.out.println("\nInterrupted!");
}
{ Thread thread = new Thread() {
@Override
public void run() {
// do nothing
}
};
thread.start();
thread.join();
}
System.out.println("finished; success? " + success);
if (! success) {
System.exit(-1);
}
}
public void run() {
int i = 0;
try {
expect(! Thread.holdsLock(this));
synchronized (this) {
expect(Thread.holdsLock(this));
System.out.println("I'm running in a separate thread!");
Thread.yield(); // just to prove Thread.yield exists and is callable
final int arrayCount = 16;
final int arraySize = 4;
System.out.println("Allocating and discarding " + arrayCount +
" arrays of " + arraySize + "MB each");
for (; i < arrayCount; ++i) {
byte[] array = new byte[arraySize * 1024 * 1024];
}
long nap = 500;
System.out.println("sleeping for " + nap + " milliseconds");
Thread.sleep(nap);
notifyAll();
}
success = true;
} catch (Throwable e) {
System.err.println("caught something in second thread after " + i +
" iterations");
e.printStackTrace();
} finally {
synchronized (this) {
notifyAll();
}
}
}
}