Skip to content

Commit

Permalink
Updated JITHelpers class to avoid false positives
Browse files Browse the repository at this point in the history
Created methods to allow an expected exception to be set from test framework.
Verifies if the thrown exception is unexpected before running
the debugging agent. More details found in #5.

The JITHelpers class now supports:
- Setting an expected exception
- Getting an expected exception
- Weeds out false positives from triggering debugging agent.

Closes: #5
Signed-off-by: Qasim Khawaja <[email protected]>
  • Loading branch information
qasimy123 authored and r30shah committed Jun 28, 2024
1 parent f7396f1 commit cccddae
Showing 1 changed file with 77 additions and 6 deletions.
83 changes: 77 additions & 6 deletions jcl/src/java.base/share/classes/com/ibm/jit/JITHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public final class JITHelpers {

private static final JITHelpers helpers;
private static final Unsafe unsafe;
private static java.util.concurrent.ConcurrentHashMap<String, Class<? extends Throwable>> exceptionMap;

private JITHelpers() {
}
Expand All @@ -68,6 +69,50 @@ private static JITHelpers jitHelpers() {
return helpers;
}

/**
* Set the expected exception for the current thread.
* Initializes the exceptionMap if necessary.
*
* @param expectedException The class of the expected exception.
*/

public static void setExpectedException(Class<? extends Throwable> expectedException) {
Thread currentThread = Thread.currentThread();
String vmThreadName = currentThread.getName();
initExceptionMap();

if (expectedException == null) {
exceptionMap.remove(vmThreadName);
}
else {
exceptionMap.put(vmThreadName, expectedException);
}
}

/**
* Get the expected exception for the current thread.
*
* @return The class of the expected exception.
*/

public static Class<? extends Throwable> getExpectedException() {
Thread currentThread = Thread.currentThread();
if(exceptionMap == null) {
return null;
}
return exceptionMap.get(currentThread.getName());
}

/**
* Synchronized method to instantiate an exceptionMap if it does not exist
*/

public synchronized static void initExceptionMap() {
if (exceptionMap == null) {
exceptionMap = new java.util.concurrent.ConcurrentHashMap<String, Class<? extends Throwable>>();
}
}

public native int transformedEncodeUTF16Big(long src, long dest, int num);

public native int transformedEncodeUTF16Little(long src, long dest, int num);
Expand Down Expand Up @@ -1192,18 +1237,44 @@ public int getInitialLockword(int flags, int reservedCounter, int cancelCounter)

private native static final void debugAgentRun(MethodAccessor ma, Object obj, Object[] args);

/**
* Invokes the method on the object with the given MethodAccessor and arguments.
* If the method throws an exception, it is caught and if the exception is unexpected,
* the debug agent triggered.
*
* @param ma the method to run the debug agent on.
* @param obj the underlying object.
* @param args the arguments for the method
* @return the return value of the method
*/
public static Object invoke(MethodAccessor ma, Object obj, Object[] args) throws InvocationTargetException {
try {
return ma.invoke(obj, args);
} catch (InvocationTargetException e) {
if (e.getCause() != null && e.getCause().getClass().getName().equals("java.lang.NullPointerException")) {
// TODO: Need synchronization to prevent many threads entering here
System.err.println("Caught java.lang.NullPointerException inside JITHelpers");

debugAgentRun(ma, obj, args);
Class < ? extends Throwable > expected = com.ibm.jit.JITHelpers.getExpectedException();
boolean runDebugAgent = true;
if (expected != null) {
Throwable targetException = e.getTargetException();
if (!expected.isAssignableFrom(targetException.getClass())) {
String message = "From JITHelpers.invoke - Unexpected exception, expected<" +
expected.getName() + "> but was<" +
targetException.getClass().getName() + ">";
System.out.println(message);
} else {
runDebugAgent = false;
}
}
if (runDebugAgent) {
synchronized (JITHelpers.class) {
System.err.println("Caught java.lang.NullPointerException inside JITHelpers, thread "+Thread.currentThread().getName());
e.getCause().printStackTrace();
debugAgentRun(ma, obj, args);

System.err.println("Aborting JVM");
System.exit(1);
System.err.println("Aborting JVM");
System.exit(1);
}
}
}

throw e;
Expand Down

0 comments on commit cccddae

Please sign in to comment.