From 003a85e41857d35523b0664e0b47ba593e85c280 Mon Sep 17 00:00:00 2001 From: Markus Schmidt Date: Mon, 9 Oct 2023 16:54:40 +0200 Subject: [PATCH] revert hacky fix.. it crashed somewhere else --- .../bytecode/frontend/AsmMethodSource.java | 135 +++++++----------- .../java/bytecode/frontend/Operand.java | 4 +- .../java/bytecode/frontend/StackFrame.java | 29 ++-- 3 files changed, 71 insertions(+), 97 deletions(-) diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java index 51f4e1ea5ec..10a3fb76e7f 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/AsmMethodSource.java @@ -21,25 +21,12 @@ * #L% */ -import static org.objectweb.asm.tree.AbstractInsnNode.FIELD_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.FRAME; -import static org.objectweb.asm.tree.AbstractInsnNode.IINC_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.INT_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.INVOKE_DYNAMIC_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.JUMP_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.LABEL; -import static org.objectweb.asm.tree.AbstractInsnNode.LDC_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.LINE; -import static org.objectweb.asm.tree.AbstractInsnNode.LOOKUPSWITCH_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.METHOD_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.MULTIANEWARRAY_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.TABLESWITCH_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.TYPE_INSN; -import static org.objectweb.asm.tree.AbstractInsnNode.VAR_INSN; +import static org.objectweb.asm.tree.AbstractInsnNode.*; import com.google.common.base.Suppliers; -import com.google.common.collect.*; +import com.google.common.collect.HashBasedTable; +import com.google.common.collect.LinkedListMultimap; +import com.google.common.collect.Table; import java.util.*; import java.util.Map.Entry; import java.util.function.BiFunction; @@ -55,38 +42,19 @@ import sootup.core.graph.MutableBlockStmtGraph; import sootup.core.jimple.Jimple; import sootup.core.jimple.basic.*; -import sootup.core.jimple.common.constant.DoubleConstant; -import sootup.core.jimple.common.constant.FloatConstant; -import sootup.core.jimple.common.constant.IntConstant; -import sootup.core.jimple.common.constant.LongConstant; -import sootup.core.jimple.common.constant.MethodHandle; -import sootup.core.jimple.common.constant.NullConstant; -import sootup.core.jimple.common.expr.AbstractBinopExpr; -import sootup.core.jimple.common.expr.AbstractConditionExpr; -import sootup.core.jimple.common.expr.AbstractInstanceInvokeExpr; -import sootup.core.jimple.common.expr.AbstractInvokeExpr; -import sootup.core.jimple.common.expr.AbstractUnopExpr; -import sootup.core.jimple.common.expr.Expr; -import sootup.core.jimple.common.expr.JAddExpr; -import sootup.core.jimple.common.expr.JCastExpr; -import sootup.core.jimple.common.expr.JDynamicInvokeExpr; -import sootup.core.jimple.common.expr.JInstanceOfExpr; -import sootup.core.jimple.common.expr.JNewArrayExpr; -import sootup.core.jimple.common.expr.JNewMultiArrayExpr; -import sootup.core.jimple.common.expr.JStaticInvokeExpr; +import sootup.core.jimple.common.constant.*; +import sootup.core.jimple.common.expr.*; import sootup.core.jimple.common.ref.*; import sootup.core.jimple.common.stmt.*; import sootup.core.jimple.javabytecode.stmt.JSwitchStmt; -import sootup.core.model.*; +import sootup.core.model.Body; +import sootup.core.model.FullPosition; +import sootup.core.model.MethodModifier; +import sootup.core.model.Position; import sootup.core.signatures.FieldSignature; import sootup.core.signatures.MethodSignature; import sootup.core.transform.BodyInterceptor; -import sootup.core.types.ArrayType; -import sootup.core.types.ClassType; -import sootup.core.types.PrimitiveType; -import sootup.core.types.Type; -import sootup.core.types.UnknownType; -import sootup.core.types.VoidType; +import sootup.core.types.*; import sootup.core.views.View; import sootup.java.core.JavaIdentifierFactory; import sootup.java.core.jimple.basic.JavaLocal; @@ -297,23 +265,28 @@ private JavaLocal getOrCreateLocal(int idx) { return local; } + String[] localVarNames = null; + @Nonnull private String determineLocalName(int idx) { - String name; - if (localVariables != null) { - name = null; + if (localVariables == null || localVariables.size() <= idx) { + /* normally for try-catch blocks there is no name found */ + return "l" + idx; + } + + // is the indexed array already created? + if (localVarNames == null) { + // localVariables is not always ordered by local.index! + localVarNames = new String[localVariables.size()]; for (LocalVariableNode lvn : localVariables) { - if (lvn.index == idx) { - name = lvn.name; - break; - } - } - /* normally for try-catch blocks */ - if (name == null) { - name = "l" + idx; + localVarNames[lvn.index] = lvn.name; } - } else { - name = "l" + idx; + } + + // get name from indexed array 'map' + String name = localVarNames[idx]; + if (name == null) { + return "l" + idx; } return name; } @@ -322,7 +295,7 @@ void setStmt(@Nonnull AbstractInsnNode insn, @Nonnull Stmt stmt) { Stmt overwrittenStmt = insnToStmt.put(insn, stmt); if (overwrittenStmt != null) { throw new IllegalArgumentException( - insn.getOpcode() + " already has an associated Stmt: " + overwrittenStmt); + insn + " " + insn.getOpcode() + " already has an associated Stmt: " + overwrittenStmt); } } @@ -417,7 +390,7 @@ private void convertGetFieldInsn(@Nonnull FieldInsnNode insn) { Operand base = operandStack.popLocal(); ref = identifierFactory.getFieldSignature(insn.name, declClass, type); val = Jimple.newInstanceFieldRef((Local) base.stackOrValue(), ref); - frame.setIn(base); + frame.setIncomingOperands(base); } opr = new Operand(insn, val, this); frame.setOut(opr); @@ -447,12 +420,12 @@ private void convertPutFieldInsn(@Nonnull FieldInsnNode insn) { if (notInstance) { ref = identifierFactory.getFieldSignature(insn.name, declClass, type); val = Jimple.newStaticFieldRef(ref); - frame.setIn(rvalue); + frame.setIncomingOperands(rvalue); } else { Operand base = operandStack.popLocal(); ref = identifierFactory.getFieldSignature(insn.name, declClass, type); val = Jimple.newInstanceFieldRef((Local) base.stackOrValue(), ref); - frame.setIn(rvalue, base); + frame.setIncomingOperands(rvalue, base); } opr = new Operand(insn, val, this); frame.setOut(opr); @@ -540,7 +513,7 @@ private void convertArrayLoadInsn(@Nonnull InsnNode insn) { JavaJimple.getInstance() .newArrayRef((Local) base.stackOrValue(), (Immediate) indx.stackOrValue()); opr = new Operand(insn, ar, this); - frame.setIn(indx, base); + frame.setIncomingOperands(indx, base); frame.setOut(opr); } else { opr = out[0]; @@ -567,7 +540,7 @@ private void convertArrayStoreInsn(@Nonnull InsnNode insn) { .newArrayRef((Local) baseOp.stackOrValue(), (Immediate) indexOp.stackOrValue()); JAssignStmt as = Jimple.newAssignStmt(ar, valueOp.stackOrValue(), getStmtPositionInfo()); - frame.setIn(valueOp, indexOp, baseOp); + frame.setIncomingOperands(valueOp, indexOp, baseOp); setStmt(insn, as); valueOp.addUsageInStmt(as); @@ -733,7 +706,7 @@ private void convertBinopInsn(@Nonnull InsnNode insn) { op1.addUsageInExpr(binop); op2.addUsageInExpr(binop); - frame.setIn(op2, op1); + frame.setIncomingOperands(op2, op1); frame.setOut(opr); } else { opr = out[0]; @@ -774,7 +747,7 @@ private void convertUnopInsn(@Nonnull InsnNode insn) { } op1.addUsageInExpr(unop); opr = new Operand(insn, unop, this); - frame.setIn(op1); + frame.setIncomingOperands(op1); frame.setOut(opr); } else { opr = out[0]; @@ -833,7 +806,7 @@ private void convertPrimCastInsn(@Nonnull InsnNode insn) { JCastExpr cast = Jimple.newCastExpr((Immediate) val.stackOrValue(), totype); opr = new Operand(insn, cast, this); val.addUsageInExpr(cast); - frame.setIn(val); + frame.setIncomingOperands(val); frame.setOut(opr); } else { opr = out[0]; @@ -853,7 +826,7 @@ private void convertReturnInsn(@Nonnull InsnNode insn) { if (!insnToStmt.containsKey(insn)) { Operand val = dword ? operandStack.popImmediateDual() : operandStack.popImmediate(); JReturnStmt ret = Jimple.newReturnStmt((Immediate) val.stackOrValue(), getStmtPositionInfo()); - frame.setIn(val); + frame.setIncomingOperands(val); setStmt(insn, ret); val.addUsageInStmt(ret); } else { @@ -913,7 +886,7 @@ private void convertInsn(@Nonnull InsnNode insn) { if (!insnToStmt.containsKey(insn)) { opr = operandStack.popImmediate(); JThrowStmt ts = Jimple.newThrowStmt((Immediate) opr.stackOrValue(), getStmtPositionInfo()); - frame.setIn(opr); + frame.setIncomingOperands(opr); frame.setOut(opr); setStmt(insn, ts); opr.addUsageInStmt(ts); @@ -930,7 +903,7 @@ private void convertInsn(@Nonnull InsnNode insn) { op == MONITORENTER ? Jimple.newEnterMonitorStmt((Immediate) opr.stackOrValue(), getStmtPositionInfo()) : Jimple.newExitMonitorStmt((Immediate) opr.stackOrValue(), getStmtPositionInfo()); - frame.setIn(opr); + frame.setIncomingOperands(opr); setStmt(insn, ts); opr.addUsageInStmt(ts); } else { @@ -985,7 +958,7 @@ private void convertIntInsn(@Nonnull IntInsnNode insn) { JNewArrayExpr anew = JavaJimple.getInstance().newNewArrayExpr(type, (Immediate) size.stackOrValue()); size.addUsageInExpr(anew); - frame.setIn(size); + frame.setIncomingOperands(size); v = anew; } opr = new Operand(insn, v, this); @@ -1048,7 +1021,7 @@ private void convertJumpInsn(@Nonnull JumpInsnNode insn) { } val1.addUsageInExpr(cond); val.addUsageInExpr(cond); - frame.setIn(val, val1); + frame.setIncomingOperands(val, val1); } else { switch (op) { case IFEQ: @@ -1079,7 +1052,7 @@ private void convertJumpInsn(@Nonnull JumpInsnNode insn) { throw new UnsupportedOperationException("Unknown if op: " + op); } val.addUsageInExpr(cond); - frame.setIn(val); + frame.setIncomingOperands(val); } BranchingStmt ifStmt = Jimple.newIfStmt(cond, getStmtPositionInfo()); stmtsThatBranchToLabel.put(ifStmt, insn.label); @@ -1203,7 +1176,7 @@ private void convertLookupSwitchInsn(@Nonnull LookupSwitchInsnNode insn) { stmtsThatBranchToLabel.putAll(lookupSwitchStmt, insn.labels); stmtsThatBranchToLabel.put(lookupSwitchStmt, insn.dflt); - frame.setIn(key); + frame.setIncomingOperands(key); setStmt(insn, lookupSwitchStmt); key.addUsageInStmt(lookupSwitchStmt); } @@ -1279,7 +1252,7 @@ private void convertMethodInsn(@Nonnull MethodInsnNode insn) { for (int i = 0; i < sigTypes.size(); i++) { args[i].addUsageInExpr(invoke); } - frame.setIn(args); + frame.setIncomingOperands(args); } opr = new Operand(insn, invoke, this); frame.setOut(opr); @@ -1373,7 +1346,7 @@ private void convertInvokeDynamicInsn(@Nonnull InvokeDynamicInsnNode insn) { args[i].addUsageInExpr(indy); } - frame.setIn(args); + frame.setIncomingOperands(args); opr = new Operand(insn, indy, this); frame.setOut(opr); } else { @@ -1458,7 +1431,7 @@ private void convertMultiANewArrayInsn(@Nonnull MultiANewArrayInsnNode insn) { for (int i = 0; i < dims; i++) { sizes[i].addUsageInExpr(nm); } - frame.setIn(sizes); + frame.setIncomingOperands(sizes); opr = new Operand(insn, nm, this); frame.setOut(opr); } else { @@ -1488,7 +1461,7 @@ private void convertTableSwitchInsn(@Nonnull TableSwitchInsnNode insn) { stmtsThatBranchToLabel.putAll(tableSwitchStmt, insn.labels); stmtsThatBranchToLabel.put(tableSwitchStmt, insn.dflt); - frame.setIn(key); + frame.setIncomingOperands(key); setStmt(insn, tableSwitchStmt); key.addUsageInStmt(tableSwitchStmt); } @@ -1535,7 +1508,7 @@ private void convertTypeInsn(@Nonnull TypeInsnNode insn) { throw new UnsupportedOperationException("Unknown type op: " + op); } op1.addUsageInExpr(val); - frame.setIn(op1); + frame.setIncomingOperands(op1); } opr = new Operand(insn, val, this); frame.setOut(opr); @@ -1569,15 +1542,14 @@ private void convertVarLoadInsn(@Nonnull VarInsnNode insn) { private void convertVarStoreInsn(@Nonnull VarInsnNode insn) { int op = insn.getOpcode(); - boolean dword = op == LSTORE || op == DSTORE; StackFrame frame = operandStack.getOrCreateStackframe(insn); + boolean dword = op == LSTORE || op == DSTORE; Operand opr = dword ? operandStack.popDual() : operandStack.pop(); Local local = getOrCreateLocal(insn.var); if (!insnToStmt.containsKey(insn)) { AbstractDefinitionStmt as = Jimple.newAssignStmt(local, opr.stackOrValue(), getStmtPositionInfo()); - opr.stackLocal = local; - frame.setIn(opr); + frame.setIncomingOperands(opr); setStmt(insn, as); opr.addUsageInStmt(as); } else { @@ -2096,6 +2068,7 @@ void replaceStmt(@Nonnull Stmt oldStmt, Stmt newStmt) { for (Entry entry : insnToStmt.entrySet()) { if (Objects.equals(oldStmt, entry.getValue())) { key = entry.getKey(); + break; } } @@ -2128,7 +2101,7 @@ void replaceStmt(@Nonnull Stmt oldStmt, Stmt newStmt) { * * @param expr which is used to filter associated Stmts */ - public Stream getStmtsThatUse(@Nonnull Expr expr) { + protected Stream getStmtsThatUse(@Nonnull Expr expr) { Stream currentUses = insnToStmt.values().stream() .flatMap( diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/Operand.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/Operand.java index 11d490a4418..80969b333dc 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/Operand.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/Operand.java @@ -91,7 +91,9 @@ void updateUsages() { .forEach(usedByStmts::add); } - if (value == stackOrValue()) return; + if (value == stackOrValue()) { + return; + } ReplaceUseStmtVisitor replaceStmtVisitor = new ReplaceUseStmtVisitor(value, stackOrValue()); diff --git a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/StackFrame.java b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/StackFrame.java index 1121b9a7c26..6c9f91cc190 100644 --- a/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/StackFrame.java +++ b/sootup.java.bytecode/src/main/java/sootup/java/bytecode/frontend/StackFrame.java @@ -35,16 +35,15 @@ import sootup.core.types.UnknownType; /** - * Frame of stack for an instruction. (see ... ) + * Frame of stack for an instruction. * * @author Aaloan Miftah */ public final class StackFrame { @Nullable private Operand[] out = null; - @Nullable private Local[] inStackLocals = null; - @Nonnull private final ArrayList in = new ArrayList<>(1); + @Nullable private Local[] incomingStackLocals = null; + @Nonnull private final ArrayList incomingOperands = new ArrayList<>(1); @Nonnull private final AsmMethodSource src; /** @@ -61,10 +60,10 @@ public final class StackFrame { * * @param oprs the operands. */ - void setIn(@Nonnull Operand... oprs) { - in.clear(); - in.add(oprs); - inStackLocals = new Local[oprs.length]; + void setIncomingOperands(@Nonnull Operand... oprs) { + incomingOperands.clear(); + incomingOperands.add(oprs); + incomingStackLocals = new Local[oprs.length]; } /** @@ -90,7 +89,7 @@ Operand[] getOut() { * old operands. */ void mergeIn(int lineNumber, @Nonnull Operand... oprs) { - if (in.get(0).length != oprs.length) { + if (incomingOperands.get(0).length != oprs.length) { throw new IllegalArgumentException("Invalid Operand length!"); } @@ -101,12 +100,12 @@ void mergeIn(int lineNumber, @Nonnull Operand... oprs) { positionInfo = StmtPositionInfo.createNoStmtPositionInfo(); } - final int nrIn = in.size(); + final int nrIn = incomingOperands.size(); for (int i = 0; i < oprs.length; i++) { Operand newOp = oprs[i]; /* merge, since prevOp != newOp */ - Local stack = inStackLocals[i]; + Local stack = incomingStackLocals[i]; if (stack != null) { if (newOp.stackLocal == null) { newOp.stackLocal = stack; @@ -124,7 +123,7 @@ void mergeIn(int lineNumber, @Nonnull Operand... oprs) { } } else { for (int j = 0; j < nrIn; j++) { - stack = in.get(j)[i].stackLocal; + stack = incomingOperands.get(j)[i].stackLocal; if (stack != null) { break; } @@ -137,7 +136,7 @@ void mergeIn(int lineNumber, @Nonnull Operand... oprs) { } /* add assign statement for prevOp */ for (int j = 0; j != nrIn; j++) { - Operand prevOp = in.get(j)[i]; + Operand prevOp = incomingOperands.get(j)[i]; if (prevOp.stackLocal == stack) { continue; } @@ -174,7 +173,7 @@ void mergeIn(int lineNumber, @Nonnull Operand... oprs) { } newOp.updateUsages(); } - inStackLocals[i] = stack; + incomingStackLocals[i] = stack; } /* @@ -194,7 +193,7 @@ void mergeIn(int lineNumber, @Nonnull Operand... oprs) { } // add if there is a difference if (0 < oprs.length) { - in.add(oprs); + incomingOperands.add(oprs); } } }