Skip to content

Commit 82112f9

Browse files
committed
Fixes btraceio#237: NPE in logs when trying to run unsafe script without unsafe mode enabled
1 parent da5154a commit 82112f9

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

src/share/classes/com/sun/btrace/VerifierException.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
package com.sun.btrace;
2727

28-
/**
28+
/**
2929
* Instance of this exception type is thrown by BTrace
3030
* Verifier when an input .class is not a valid BTrace program.
3131
*
@@ -35,4 +35,8 @@ public class VerifierException extends RuntimeException {
3535
public VerifierException(String msg) {
3636
super(msg);
3737
}
38+
39+
public VerifierException(Throwable cause) {
40+
super(cause);
41+
}
3842
}

src/share/classes/com/sun/btrace/agent/Client.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,10 @@ protected final Class loadClass(InstrumentCommand instr) throws IOException {
210210
String[] args = instr.getArguments();
211211
this.btraceCode = instr.getCode();
212212
try {
213-
probe = verifyAndLoad(btraceCode);
213+
probe = load(btraceCode);
214+
if (!probe.isVerified()) {
215+
throw probe.getVerifierException();
216+
}
214217
} catch (Throwable th) {
215218
debugPrint(th);
216219
errorExit(th);
@@ -304,7 +307,7 @@ final BTraceRuntime getRuntime() {
304307
}
305308

306309
protected final String getClassName() {
307-
return probe.getClassName();
310+
return probe != null ? probe.getClassName() : "<unknown>";
308311
}
309312

310313
final boolean isCandidate(Class c) {
@@ -340,13 +343,17 @@ final void endRetransformClasses() {
340343
}
341344

342345
// Internals only below this point
343-
private BTraceProbe verifyAndLoad(byte[] buf) {
346+
private BTraceProbe load(byte[] buf) {
344347
BTraceProbeFactory f = new BTraceProbeFactory(settings);
345348
debugPrint("loading BTrace class");
346349
BTraceProbe cn = f.createProbe(buf);
347350

348351
if (isDebug()) {
349-
debugPrint("loaded '" + cn.getClassName() + "' successfully");
352+
if (cn.isVerified()) {
353+
debugPrint("loaded '" + cn.getClassName() + "' successfully");
354+
} else {
355+
debugPrint(cn.getClassName() + " failed verification");
356+
}
350357
}
351358
return cn;
352359
}

src/share/classes/com/sun/btrace/runtime/BTraceProbe.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.sun.btrace.BTraceRuntime;
2828
import com.sun.btrace.DebugSupport;
29+
import com.sun.btrace.VerifierException;
2930
import com.sun.btrace.comm.RetransformClassNotification;
3031
import com.sun.btrace.org.objectweb.asm.AnnotationVisitor;
3132
import com.sun.btrace.org.objectweb.asm.ClassReader;
@@ -74,6 +75,7 @@ public final class BTraceProbe extends ClassNode {
7475
private BTraceTransformer transformer;
7576
private boolean subtypeMatcher = false;
7677
private boolean annotationMatcher = false;
78+
private VerifierException verifierException = null;
7779

7880
private BTraceProbe(BTraceProbeFactory factory) {
7981
super(Opcodes.ASM5);
@@ -289,6 +291,14 @@ public boolean isClassRenamed() {
289291
return classRenamed;
290292
}
291293

294+
public boolean isVerified() {
295+
return verifierException == null;
296+
}
297+
298+
public VerifierException getVerifierException() {
299+
return verifierException;
300+
}
301+
292302
boolean isFieldInjected(String name) {
293303
return injectedFields.contains(name);
294304
}
@@ -404,6 +414,8 @@ private void initialize(ClassReader cr) {
404414
}
405415
mapOnProbes();
406416
this.filter = new ClassFilter(onMethods);
417+
} catch (VerifierException e) {
418+
verifierException = e;
407419
} finally {
408420
if (debug.isDumpClasses()) {
409421
debug.dumpClass(name, getBytecode(false));

0 commit comments

Comments
 (0)