Skip to content

Commit

Permalink
Merge pull request #1126 from soot-oss/fix/switchComparison
Browse files Browse the repository at this point in the history
Fix switch comparison
  • Loading branch information
stschott authored Nov 5, 2024
2 parents 33cbf35 + 1308362 commit 1cde415
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
import sootup.core.jimple.basic.StmtPositionInfo;
import sootup.core.jimple.basic.Trap;
import sootup.core.jimple.common.ref.JCaughtExceptionRef;
import sootup.core.jimple.common.stmt.*;
import sootup.core.jimple.common.stmt.BranchingStmt;
import sootup.core.jimple.common.stmt.FallsThroughStmt;
import sootup.core.jimple.common.stmt.JIdentityStmt;
import sootup.core.jimple.common.stmt.Stmt;
import sootup.core.signatures.MethodSignature;
import sootup.core.types.ClassType;
import sootup.core.types.Type;
Expand All @@ -53,7 +56,7 @@ public class MutableBlockStmtGraph extends MutableStmtGraph {
@Nonnull
private final Map<Stmt, Pair<Integer, MutableBasicBlock>> stmtToBlock = new IdentityHashMap<>();

@Nonnull private final Set<MutableBasicBlock> blocks = new HashSet<>();
@Nonnull private final Set<MutableBasicBlock> blocks = new LinkedHashSet<>();

public MutableBlockStmtGraph() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,7 @@
import sootup.core.jimple.common.constant.Constant;
import sootup.core.jimple.common.constant.IntConstant;
import sootup.core.jimple.common.expr.*;
import sootup.core.jimple.common.ref.JArrayRef;
import sootup.core.jimple.common.ref.JCaughtExceptionRef;
import sootup.core.jimple.common.ref.JInstanceFieldRef;
import sootup.core.jimple.common.ref.JParameterRef;
import sootup.core.jimple.common.ref.JStaticFieldRef;
import sootup.core.jimple.common.ref.JThisRef;
import sootup.core.jimple.common.ref.*;
import sootup.core.jimple.common.stmt.*;
import sootup.core.jimple.javabytecode.stmt.*;

Expand Down Expand Up @@ -200,7 +195,7 @@ public boolean caseSwitchStmt(JSwitchStmt stmt, Object o) {
}
JSwitchStmt otherSwitchStmt = (JSwitchStmt) o;

if (stmt.getKey() != otherSwitchStmt.getKey()) {
if (!stmt.getKey().equivTo(otherSwitchStmt.getKey())) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package sootup.core.jimple.basic;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import sootup.core.jimple.Jimple;
import sootup.core.jimple.common.constant.IntConstant;
import sootup.core.jimple.javabytecode.stmt.JSwitchStmt;
import sootup.core.types.PrimitiveType;

@Tag("Java8")
public class JimpleComparatorTest {

@Test
public void test() {
// l1 and l2 are equal, l1 and l3 are not
Local l1 = Jimple.newLocal("l", PrimitiveType.getInt());
Local l2 = Jimple.newLocal("l", PrimitiveType.getInt());
Local l3 = Jimple.newLocal("l", PrimitiveType.getBoolean());

List<IntConstant> lookup1 = new ArrayList<>();
lookup1.add(IntConstant.getInstance(3));
lookup1.add(IntConstant.getInstance(5));
lookup1.add(IntConstant.getInstance(999));

List<IntConstant> lookup2 = new ArrayList<>();
lookup2.add(IntConstant.getInstance(3));
lookup2.add(IntConstant.getInstance(5));
lookup2.add(IntConstant.getInstance(999));

JSwitchStmt switch1 = Jimple.newLookupSwitchStmt(l1, lookup1, StmtPositionInfo.NOPOSITION);
JSwitchStmt switch2 = Jimple.newLookupSwitchStmt(l2, lookup2, StmtPositionInfo.NOPOSITION);
JSwitchStmt switch3 = Jimple.newLookupSwitchStmt(l3, lookup2, StmtPositionInfo.NOPOSITION);

assertTrue(switch1.equivTo(switch2));
assertFalse(switch1.equivTo(switch3));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

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

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import sootup.core.jimple.basic.StmtPositionInfo;
Expand All @@ -24,7 +27,6 @@ public void testLookupSwitchStmt() {

Stmt stmt = new JSwitchStmt(IntConstant.getInstance(42), lookupValues, nop);
Stmt stmtDifferentKey = new JSwitchStmt(IntConstant.getInstance(123), lookupValues, nop);
Stmt stmtDifferentDefault = new JSwitchStmt(IntConstant.getInstance(42), lookupValues, nop);

// toString
assertEquals("switch(42) { default: }", stmt.toString());
Expand All @@ -43,7 +45,6 @@ public void testLookupSwitchStmt() {
assertFalse(stmt.equivTo(this));
assertTrue(stmt.equivTo(stmt));
assertFalse(stmt.equivTo(switchWithDefault));
assertFalse(stmt.equivTo(stmtDifferentDefault));
assertFalse(stmt.equivTo(stmtDifferentKey));
}

Expand All @@ -57,7 +58,6 @@ public void testTableSwitchStmt() {
targets.add(new JNopStmt(nop));
JSwitchStmt stmt = new JSwitchStmt(IntConstant.getInstance(123), 1, 4, nop);

ArrayList<Stmt> targets2 = new ArrayList<>();
targets.add(new JReturnStmt(IntConstant.getInstance(1), nop));
targets.add(new JReturnStmt(IntConstant.getInstance(2), nop));
targets.add(new JNopStmt(nop));
Expand All @@ -66,7 +66,6 @@ public void testTableSwitchStmt() {
Stmt stmt3 = new JSwitchStmt(IntConstant.getInstance(456), 1, 4, nop);
Stmt stmt4 = new JSwitchStmt(IntConstant.getInstance(123), 2, 4, nop);
Stmt stmt5 = new JSwitchStmt(IntConstant.getInstance(123), 1, 5, nop);
Stmt stmt6 = new JSwitchStmt(IntConstant.getInstance(123), 1, 4, nop);

// toString
assertEquals(
Expand All @@ -76,13 +75,11 @@ public void testTableSwitchStmt() {
// equivTo
assertFalse(stmt.equivTo(666));
assertTrue(stmt.equivTo(stmt));
assertFalse(stmt.equivTo(stmt2));
assertTrue(stmt.equivTo(stmt2));

assertFalse(stmt.equivTo(stmt2));
assertFalse(stmt.equivTo(stmt3));
assertFalse(stmt.equivTo(stmt4));
assertFalse(stmt.equivTo(stmt5));
assertFalse(stmt.equivTo(stmt6));
}

@Test
Expand Down Expand Up @@ -220,7 +217,6 @@ public void testLookupSwitch() {

Stmt switchStmt = new JSwitchStmt(IntConstant.getInstance(42), lookupValues, nop);
Stmt stmtDifferentKey = new JSwitchStmt(IntConstant.getInstance(123), lookupValues, nop);
Stmt stmtDifferentDefault = new JSwitchStmt(IntConstant.getInstance(42), lookupValues, nop);

// toString
assertEquals("switch(42) { default: }", switchStmt.toString());
Expand All @@ -237,7 +233,6 @@ public void testLookupSwitch() {
assertFalse(switchStmt.equivTo(this));
assertTrue(switchStmt.equivTo(switchStmt));
assertFalse(switchStmt.equivTo(differentSwitchStmt));
assertFalse(switchStmt.equivTo(stmtDifferentDefault));
assertFalse(switchStmt.equivTo(stmtDifferentKey));
}
}

0 comments on commit 1cde415

Please sign in to comment.