use of org.objectweb.asm.tree.FrameNode in project bytecode-viewer by Konloch.
the class InstructionSearcher method search.
public boolean search() {
for (AbstractInsnNode ain : insns.toArray()) {
if (ain instanceof LineNumberNode || ain instanceof FrameNode)
continue;
if (pattern.accept(ain)) {
matches.add(pattern.getLastMatch());
pattern.resetMatch();
}
}
return size() != 0;
}
use of org.objectweb.asm.tree.FrameNode in project bytecode-viewer by Konloch.
the class StackFramesRemover method execute.
@Override
public void execute(List<ClassNode> classNodeList) {
AtomicInteger counter = new AtomicInteger();
PluginConsole frame = new PluginConsole("StackFrames Remover");
for (ClassNode cn : classNodeList) {
for (MethodNode mn : cn.methods) {
for (AbstractInsnNode insn : mn.instructions.toArray()) {
if (insn instanceof FrameNode) {
mn.instructions.remove(insn);
counter.incrementAndGet();
}
}
}
}
frame.appendText(String.format("Removed %s stackframes.", counter));
frame.setVisible(true);
}
use of org.objectweb.asm.tree.FrameNode in project evosuite by EvoSuite.
the class Instrumenter method wrapMethod.
/**
* public int myMethod(int i)
* {
* try
* {
* return _sw_prototype_original_myMethod(i)
* }
* finally
* {
* Capturer.enable();
* }
* }
*
* @param classNode
* @param className
* @param methodNode
*/
@SuppressWarnings("unchecked")
private MethodNode wrapMethod(final ClassNode classNode, final String className, final MethodNode methodNode) {
methodNode.maxStack += 4;
// create wrapper for original method
final MethodNode wrappingMethodNode = new MethodNode(methodNode.access, methodNode.name, methodNode.desc, methodNode.signature, (String[]) methodNode.exceptions.toArray(new String[methodNode.exceptions.size()]));
wrappingMethodNode.maxStack = methodNode.maxStack;
// assign annotations to wrapping method
wrappingMethodNode.visibleAnnotations = methodNode.visibleAnnotations;
wrappingMethodNode.visibleParameterAnnotations = methodNode.visibleParameterAnnotations;
// remove annotations from wrapped method to avoid wrong behavior controlled by annotations
methodNode.visibleAnnotations = null;
methodNode.visibleParameterAnnotations = null;
// rename original method
methodNode.access = TransformerUtil.modifyVisibility(methodNode.access, Opcodes.ACC_PRIVATE);
final LabelNode l0 = new LabelNode();
final LabelNode l1 = new LabelNode();
final LabelNode l2 = new LabelNode();
final InsnList wInstructions = wrappingMethodNode.instructions;
if ("<init>".equals(methodNode.name)) {
// wrap a constructor
methodNode.name = WRAP_NAME_PREFIX + "init" + WRAP_NAME_PREFIX;
// move call to other constructors to new method
AbstractInsnNode ins = null;
ListIterator<AbstractInsnNode> iter = methodNode.instructions.iterator();
// number of invokespecial calls before actual constructor call
int numInvokeSpecials = 0;
while (iter.hasNext()) {
ins = iter.next();
iter.remove();
wInstructions.add(ins);
if (ins instanceof MethodInsnNode) {
MethodInsnNode mins = (MethodInsnNode) ins;
if (ins.getOpcode() == Opcodes.INVOKESPECIAL) {
if (mins.name.startsWith("<init>")) {
if (numInvokeSpecials == 0) {
break;
} else {
numInvokeSpecials--;
}
}
}
} else if (ins instanceof TypeInsnNode) {
TypeInsnNode typeIns = (TypeInsnNode) ins;
if (typeIns.getOpcode() == Opcodes.NEW || typeIns.getOpcode() == Opcodes.NEWARRAY) {
numInvokeSpecials++;
}
}
}
} else {
methodNode.name = WRAP_NAME_PREFIX + methodNode.name;
}
int varReturnValue = 0;
final Type returnType = Type.getReturnType(methodNode.desc);
if (returnType.equals(Type.VOID_TYPE)) {
wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, "java/lang/Throwable"));
} else {
wrappingMethodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l2, "java/lang/Throwable"));
if (!TransformerUtil.isStatic(methodNode.access)) {
// load "this"
varReturnValue++;
}
// consider method arguments to find right variable index
final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
for (int i = 0; i < argTypes.length; i++) {
varReturnValue++;
// long/double take two registers
if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
varReturnValue++;
}
}
// push NULL on the stack and initialize variable for return value for it
wInstructions.add(new InsnNode(Opcodes.ACONST_NULL));
wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
}
int var = 0;
// --- L0
wInstructions.add(l0);
wInstructions.add(this.addCaptureCall(TransformerUtil.isStatic(methodNode.access), className, wrappingMethodNode.name, wrappingMethodNode.desc, Type.getArgumentTypes(methodNode.desc)));
if (!TransformerUtil.isStatic(methodNode.access)) {
// load "this" to call method
wInstructions.add(new VarInsnNode(Opcodes.ALOAD, 0));
var++;
}
final Type[] argTypes = Type.getArgumentTypes(methodNode.desc);
for (int i = 0; i < argTypes.length; i++) {
this.addLoadInsn(wInstructions, argTypes[i], var++);
// long/double take two registers
if (argTypes[i].equals(Type.LONG_TYPE) || argTypes[i].equals(Type.DOUBLE_TYPE)) {
var++;
}
}
if (TransformerUtil.isStatic(methodNode.access)) {
wInstructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, classNode.name, methodNode.name, methodNode.desc));
} else {
wInstructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, classNode.name, methodNode.name, methodNode.desc));
}
var++;
if (returnType.equals(Type.VOID_TYPE)) {
wInstructions.add(new JumpInsnNode(Opcodes.GOTO, l2));
// --- L1
wInstructions.add(l1);
wInstructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));
this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);
wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
wInstructions.add(new InsnNode(Opcodes.ATHROW));
// FIXME <--- DUPLICATE CODE
// --- L2
wInstructions.add(l2);
wInstructions.add(new FrameNode(Opcodes.F_SAME, 0, null, 0, null));
this.addCaptureEnableStatement(className, methodNode, wInstructions, -1);
wInstructions.add(new InsnNode(Opcodes.RETURN));
} else {
// construct store of the wrapped method call's result
this.addBoxingStmt(wInstructions, returnType);
wInstructions.add(new VarInsnNode(Opcodes.ASTORE, varReturnValue));
wInstructions.add(new VarInsnNode(Opcodes.ALOAD, varReturnValue));
this.addUnBoxingStmt(wInstructions, returnType);
final int storeOpcode = returnType.getOpcode(Opcodes.ISTORE);
// might be only var
wInstructions.add(new VarInsnNode(storeOpcode, ++var));
// --- L1
wInstructions.add(l1);
this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);
// construct load of the wrapped method call's result
int loadOpcode = returnType.getOpcode(Opcodes.ILOAD);
wInstructions.add(new VarInsnNode(loadOpcode, var));
// construct return of the wrapped method call's result
this.addReturnInsn(wInstructions, returnType);
// ---- L2
wInstructions.add(l2);
wInstructions.add(new FrameNode(Opcodes.F_FULL, 2, new Object[] { className, this.getInternalName(returnType) }, 1, new Object[] { "java/lang/Throwable" }));
wInstructions.add(new VarInsnNode(Opcodes.ASTORE, --var));
this.addCaptureEnableStatement(className, methodNode, wInstructions, varReturnValue);
wInstructions.add(new VarInsnNode(Opcodes.ALOAD, var));
wInstructions.add(new InsnNode(Opcodes.ATHROW));
}
transformWrapperCalls(methodNode);
return wrappingMethodNode;
}
use of org.objectweb.asm.tree.FrameNode in project cassandra by apache.
the class MonitorMethodTransformer method writeTryCatchMonitorEnterExit.
void writeTryCatchMonitorEnterExit() {
access |= Opcodes.ACC_SYNTHETIC;
name = baseName;
Label start = new Label();
Label inmonitor = new Label();
Label normal = new Label();
// normal
Label except = new Label();
// normal return failed
Label normalRetExcept = new Label();
// exceptional return success
Label exceptRetNormal = new Label();
// exceptional return failed
Label exceptRetExcept = new Label();
Label end = new Label();
reset(start, end);
// add a local variable slot to save any exceptions into (at maxLocalParams position)
++maxLocals;
// must load self or class onto stack, and return value (if any)
maxStack = Math.max(maxLocalParams, returnCode == Opcodes.RETURN ? 2 : 3);
tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(inmonitor), getLabelNode(normal), getLabelNode(except), null));
tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(normal), getLabelNode(normalRetExcept), getLabelNode(normalRetExcept), null));
tryCatchBlocks.add(new TryCatchBlockNode(getLabelNode(except), getLabelNode(exceptRetNormal), getLabelNode(exceptRetExcept), null));
// preMonitorEnter
// monitorenter
instructions.add(getLabelNode(start));
invokePreMonitorEnter();
invokeMonitor(Opcodes.MONITORENTER);
{
// try1 { val = original();
instructions.add(getLabelNode(inmonitor));
int invokeCode = loadParamsAndReturnInvokeCode();
instructions.add(new MethodInsnNode(invokeCode, className, baseName + "$unsync", desc));
{
// try2 { preMonitorExit(); monitorexit; return val; }
instructions.add(getLabelNode(normal));
invokePreMonitorExit();
invokeMonitor(Opcodes.MONITOREXIT);
// success
instructions.add(new InsnNode(returnCode()));
// }
// catch2 { monitorexit; throw }
instructions.add(getLabelNode(normalRetExcept));
instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
instructions.add(new IntInsnNode(Opcodes.ASTORE, maxLocalParams));
pushRef();
invokeMonitor(Opcodes.MONITOREXIT);
instructions.add(new IntInsnNode(Opcodes.ALOAD, maxLocalParams));
instructions.add(new InsnNode(Opcodes.ATHROW));
// }
}
// catch1 { try3 { preMonitorExit; monitorexit; throw
instructions.add(getLabelNode(except));
instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
instructions.add(new IntInsnNode(Opcodes.ASTORE, maxLocalParams));
invokePreMonitorExit();
invokeMonitor(Opcodes.MONITOREXIT);
instructions.add(new IntInsnNode(Opcodes.ALOAD, maxLocalParams));
instructions.add(getLabelNode(exceptRetNormal));
instructions.add(new InsnNode(Opcodes.ATHROW));
instructions.add(getLabelNode(exceptRetExcept));
instructions.add(new FrameNode(Opcodes.F_SAME1, 0, null, 1, new Object[] { "java/lang/Throwable" }));
instructions.add(new IntInsnNode(Opcodes.ASTORE, maxLocalParams));
pushRef();
invokeMonitor(Opcodes.MONITOREXIT);
instructions.add(new IntInsnNode(Opcodes.ALOAD, maxLocalParams));
instructions.add(new InsnNode(Opcodes.ATHROW));
}
instructions.add(getLabelNode(end));
methodWriterSink.writeSyntheticMethod(MONITOR, this);
}
use of org.objectweb.asm.tree.FrameNode in project maple-ir by LLVM-but-worse.
the class FrameNodeSerializer method deserialize.
@Override
public FrameNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = (JsonObject) json;
int opcode;
int type;
List<?> local = null, stack = null;
opcode = jsonObject.get("opcode").getAsInt();
type = jsonObject.get("type").getAsInt();
local = context.deserialize(jsonObject.get("local"), List.class);
stack = context.deserialize(jsonObject.get("stack"), List.class);
FrameNode node = new FrameNode(opcode, local.size(), local.toArray(), stack.size(), stack.toArray());
node.type = type;
return node;
}
Aggregations