Skip to content

Commit

Permalink
Do not drop CHECKCAST instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
jbachorik committed Aug 6, 2023
1 parent 792166d commit dd84c5d
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public void visitVarInsn(int opcode, int var) {
case Opcodes.DLOAD:
{
simulatedStack.push(Boolean.FALSE);
// long and double occoupy 2 stack slots; fall through
// long and double occupy 2 stack slots; fall through
}
case Opcodes.ILOAD:
case Opcodes.FLOAD:
Expand All @@ -283,7 +283,7 @@ public void visitVarInsn(int opcode, int var) {
case Opcodes.DSTORE:
{
simulatedStack.poll();
// long and double occoupy 2 stack slots; fall through
// long and double occupy 2 stack slots; fall through
}
case Opcodes.ASTORE:
case Opcodes.ISTORE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
* @author A. Sundararajan
*/
@SupportedAnnotationTypes("*")
@SupportedSourceVersion(SourceVersion.RELEASE_7)
@SupportedSourceVersion(SourceVersion.RELEASE_8)
public class Verifier extends AbstractProcessor implements TaskListener {
private final List<String> classNames = new ArrayList<>();
private final List<CompilationUnitTree> compUnits = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.util.Map;
import org.junit.jupiter.api.Test;
Expand All @@ -11,6 +12,8 @@
import org.openjdk.btrace.instr.BTraceProbe;
import org.openjdk.btrace.instr.BTraceProbeFactory;

import static org.junit.jupiter.api.Assertions.fail;

public class JfrEventsTest {
@Test
public void testCompile() throws Exception {
Expand All @@ -26,9 +29,18 @@ public void testCompile() throws Exception {
BTraceProbeFactory factory = new BTraceProbeFactory(SharedSettings.GLOBAL);
for (byte[] bytes : data.values()) {
BTraceProbe probe = factory.createProbe(bytes);
byte[] code = probe.getDataHolderBytecode();
CheckClassAdapter.verify(new ClassReader(code), true, new PrintWriter(System.err));
verifyCode(probe.getFullBytecode());
verifyCode(probe.getDataHolderBytecode());
}
}

private void verifyCode(byte[] code) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
CheckClassAdapter.verify(new ClassReader(code), true, pw);
if (sw.toString().contains("AnalyzerException")) {
System.err.println(sw);
fail();
}
System.out.println(data);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.openjdk.btrace.compiler;

import org.junit.jupiter.api.Test;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.util.CheckClassAdapter;
import org.openjdk.btrace.core.SharedSettings;
import org.openjdk.btrace.instr.BTraceProbe;
import org.openjdk.btrace.instr.BTraceProbeFactory;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.fail;

public class TypeErasureTest {
@Test
void testTypeErasure() throws Exception {
URL input = TypeErasureTest.class.getResource("/HistoProbe.java");
File inputFile = new File(input.toURI());
Map<String, byte[]> data =
new Compiler(true)
.compile(
inputFile,
new PrintWriter(System.err),
null,
System.getProperty("java.class.path"));
BTraceProbeFactory factory = new BTraceProbeFactory(SharedSettings.GLOBAL);
for (byte[] bytes : data.values()) {
BTraceProbe probe = factory.createProbe(bytes);
verifyCode(probe.getFullBytecode());
verifyCode(probe.getDataHolderBytecode());
}
}

private void verifyCode(byte[] code) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
CheckClassAdapter.verify(new ClassReader(code), true, pw);
if (sw.toString().contains("AnalyzerException")) {
System.err.println(sw);
fail();
}
}
}
27 changes: 27 additions & 0 deletions btrace-compiler/src/test/resources/HistoProbe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test;

import static org.openjdk.btrace.core.BTraceUtils.*;
import org.openjdk.btrace.core.annotations.*;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

@BTrace public class HistoProbe {
private static Map<String, AtomicInteger> histo = newHashMap();
@OnMethod(clazz = "javax.swing.JComponent", method = "<init>")
public static void onMethod(@Self Object obj) {
String cn = name(classOf(obj));
AtomicInteger ai = get((Map<String, AtomicInteger>)histo, cn);
if (ai == null) {
ai = newAtomicInteger(1);
put(histo, cn, ai);
} else {
incrementAndGet(ai); // WORKS if commented out
}
}

@OnTimer(1000)
public static void print() {
printNumberMap("Component Histogram", histo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ String translateOwner(String owner) {
}

boolean isTransforming() {
return onMethods.size() > 0;
return !onMethods.isEmpty();
}

Collection<OnMethod> getApplicableHandlers(BTraceClassReader cr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ public void visitTypeInsn(int opcode, String desc) {
}
Verifier.reportError("no.new.object", desc);
}
super.visitTypeInsn(opcode, desc);
}

@Override
Expand Down

0 comments on commit dd84c5d

Please sign in to comment.