Skip to content

Commit

Permalink
adding the new generated local to opr.stacklocal
Browse files Browse the repository at this point in the history
  • Loading branch information
swissiety committed Oct 9, 2023
1 parent 484ebeb commit d623b8d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,8 +335,11 @@ void mergeStmts(@Nonnull AbstractInsnNode insn, @Nonnull Stmt stmt) {
}

@Nonnull
Local newStackLocal() {
Local newStackLocal(@Nonnull Type type) {
int idx = nextLocal++;
// TODO: [ms] I added the type as parameter into the method already - check if we can
// incorporate the type here - should be possible if the stacklocals are not reused by another
// assignment..
JavaLocal l =
JavaJimple.newLocal("$stack" + idx, UnknownType.getInstance(), Collections.emptyList());
locals.set(idx, l);
Expand Down Expand Up @@ -387,7 +390,7 @@ private void addReadOperandAssignments_internal(BiFunction<Value, Operand, Boole
continue;
}

Local stackLocal = newStackLocal();
Local stackLocal = newStackLocal(opValue.getType());
operand.stackLocal = stackLocal;
JAssignStmt<Local, ?> asssignStmt =
Jimple.newAssignStmt(stackLocal, opValue, getStmtPositionInfo());
Expand Down Expand Up @@ -1573,6 +1576,7 @@ private void convertVarStoreInsn(@Nonnull VarInsnNode insn) {
if (!insnToStmt.containsKey(insn)) {
AbstractDefinitionStmt<Local, ?> as =
Jimple.newAssignStmt(local, opr.stackOrValue(), getStmtPositionInfo());
opr.stackLocal = local;
frame.setIn(opr);
setStmt(insn, as);
opr.addUsageInStmt(as);
Expand Down Expand Up @@ -1620,7 +1624,7 @@ private void convertLabel(@Nonnull LabelNode ln) {
Operand opr;
if (out == null) {
JCaughtExceptionRef ref = JavaJimple.getInstance().newCaughtExceptionRef();
Local stack = newStackLocal();
Local stack = newStackLocal(ref.getType());
AbstractDefinitionStmt<Local, JCaughtExceptionRef> as =
Jimple.newIdentityStmt(stack, ref, getStmtPositionInfo());
opr = new Operand(ln, ref, this);
Expand Down Expand Up @@ -1701,7 +1705,7 @@ private void convert() {
if (inlineExceptionLabels.contains(handlerNode)) {
// Catch the exception
JCaughtExceptionRef ref = JavaJimple.getInstance().newCaughtExceptionRef();
Local local = newStackLocal();
Local local = newStackLocal(ref.getType());
AbstractDefinitionStmt<Local, JCaughtExceptionRef> as =
Jimple.newIdentityStmt(local, ref, getStmtPositionInfo());

Expand Down Expand Up @@ -2110,10 +2114,11 @@ void replaceStmt(@Nonnull Stmt oldStmt, Stmt newStmt) {
replacedStmt.put(oldStmt, newStmt);

if (oldStmt instanceof BranchingStmt) {
List<LabelNode> branchLabels = stmtsThatBranchToLabel.get((BranchingStmt) oldStmt);
final BranchingStmt oldBranchingStmt = (BranchingStmt) oldStmt;
List<LabelNode> branchLabels = stmtsThatBranchToLabel.get(oldBranchingStmt);
if (branchLabels != null) {
branchLabels.forEach(bl -> stmtsThatBranchToLabel.put((BranchingStmt) newStmt, bl));
stmtsThatBranchToLabel.removeAll(oldStmt);
stmtsThatBranchToLabel.removeAll(oldBranchingStmt);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @author Aaloan Miftah
*/
class Operand {
public class Operand {

@SuppressWarnings("ConstantConditions")
static final Operand DWORD_DUMMY = new Operand(null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public Operand popLocal(@Nonnull Operand o) {
Value v = o.value;
Local l = o.stackLocal;
if (l == null && !(v instanceof Local)) {
l = o.stackLocal = methodSource.newStackLocal();
l = o.stackLocal = methodSource.newStackLocal(v.getType());
methodSource.setStmt(o.insn, Jimple.newAssignStmt(l, v, methodSource.getStmtPositionInfo()));
o.updateUsages();
}
Expand All @@ -123,7 +123,7 @@ public Operand popImmediate(@Nonnull Operand o) {
Value v = o.value;
Local l = o.stackLocal;
if (l == null && !(v instanceof Local) && !(v instanceof Constant)) {
l = o.stackLocal = methodSource.newStackLocal();
l = o.stackLocal = methodSource.newStackLocal(v.getType());
methodSource.setStmt(o.insn, Jimple.newAssignStmt(l, v, methodSource.getStmtPositionInfo()));
o.updateUsages();
}
Expand All @@ -135,7 +135,7 @@ public Operand popStackConst(@Nonnull Operand o) {
Value v = o.value;
Local l = o.stackLocal;
if (l == null && !(v instanceof Constant)) {
l = o.stackLocal = methodSource.newStackLocal();
l = o.stackLocal = methodSource.newStackLocal(v.getType());
methodSource.setStmt(o.insn, Jimple.newAssignStmt(l, v, methodSource.getStmtPositionInfo()));
o.updateUsages();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
import sootup.core.jimple.common.stmt.JAssignStmt;
import sootup.core.jimple.common.stmt.JNopStmt;
import sootup.core.jimple.common.stmt.Stmt;
import sootup.core.types.UnknownType;

/**
* Frame of stack for an instruction. (see <a
* href="https://docs.oracle.com/javase/specs/jvms/se11/html/jvms-2.html#jvms-2.6">...</a> )
*
* @author Aaloan Miftah
*/
final class StackFrame {
public final class StackFrame {

@Nullable private Operand[] out = null;
@Nullable private Local[] inStackLocals = null;
Expand Down Expand Up @@ -118,8 +119,7 @@ void mergeIn(int lineNumber, @Nonnull Operand... oprs) {
if (stack != rvalue) {
JAssignStmt<?, ?> as = Jimple.newAssignStmt(stack, rvalue, positionInfo);
src.mergeStmts(newOp.insn, as);
newOp.addUsageInStmt(
as); // [ms] necessary? added it as an equivalent existed in soot as well..
newOp.addUsageInStmt(as);
}
}
} else {
Expand All @@ -132,7 +132,7 @@ void mergeIn(int lineNumber, @Nonnull Operand... oprs) {
if (stack == null) {
stack = newOp.stackLocal;
if (stack == null) {
stack = src.newStackLocal();
stack = src.newStackLocal(UnknownType.getInstance());
}
}
/* add assign statement for prevOp */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import sootup.core.util.printer.StmtPrinter;

/**
* A psuedo stmt containing different stmts.
* A pseudo stmt containing different stmts.
*
* <p>basically its used to map more than one Stmt to a single AbstractInsNode - used in a Map of
* <p>its used to map more than one Stmt to a single AbstractInsNode - used in a Map of
* AsmMethodSource
*
* @author Aaloan Miftah
Expand Down
2 changes: 1 addition & 1 deletion sootup.tests/src/test/java/bugs/Issue698Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
@Category(Java8Test.class)
public class Issue698Test {
@Test
public void testJar_missing_if_flows() {
public void test_missing_local_assignments_from_virtualinvoke() {

// https://repo1.maven.org/maven2/cn/hutool/hutool-db/5.7.18/hutool-db-5.7.18.jar.
JavaProject applicationProject =
Expand Down

0 comments on commit d623b8d

Please sign in to comment.