Skip to content

Commit

Permalink
Merge pull request #846 from Timbals/fix/operand-side-effects
Browse files Browse the repository at this point in the history
fix cast error for `Operand`s with side effects
  • Loading branch information
swissiety authored Feb 8, 2024
2 parents 84b98b6 + c4d7208 commit d61e334
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
Binary file not shown.
9 changes: 9 additions & 0 deletions shared-test-resources/bugfixes/NestedMethodCall.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class NestedMethodCall {
public void nestedMethodCall() {
int i = 0;
String s = "abc";
decode(s.charAt(++i), s.charAt(i++));
}

void decode(char first, char second) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ void changeStackLocal(Local newStackLocal) {
return;
}

JAssignStmt assignStmt = methodSource.getStmt(insn);
if (assignStmt == null) {
Stmt stmt = methodSource.getStmt(insn);
if (!(stmt instanceof JAssignStmt)) {
// emit `$newStackLocal = value`
methodSource.setStmt(insn, Jimple.newAssignStmt(newStackLocal, value, positionInfo));
} else {
JAssignStmt assignStmt = (JAssignStmt) stmt;
assert assignStmt.getLeftOp() == oldStackLocal || assignStmt.getLeftOp() == newStackLocal;
// replace `$oldStackLocal = value` with `$newStackLocal = value`
methodSource.replaceStmt(assignStmt, assignStmt.withVariable(newStackLocal));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package sootup.java.bytecode.frontend;

import static junit.framework.Assert.assertTrue;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.fail;

import categories.Java8Test;
import java.util.Arrays;
import java.util.Collections;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import sootup.core.model.SootClass;
import sootup.core.model.SootMethod;
import sootup.core.signatures.MethodSignature;
import sootup.java.bytecode.inputlocation.DefaultRTJarAnalysisInputLocation;
import sootup.java.bytecode.inputlocation.JavaClassPathAnalysisInputLocation;
import sootup.java.core.JavaIdentifierFactory;
import sootup.java.core.JavaSootMethod;
import sootup.java.core.types.JavaClassType;
import sootup.java.core.views.JavaView;

Expand Down Expand Up @@ -47,4 +51,30 @@ public void testFix_StackUnderrun_convertPutFieldInsn_init() {
final SootMethod method = abstractClass.getMethod(mainMethodSignature.getSubSignature()).get();
method.getBody().getStmts();
}

@Test
public void testNestedMethodCalls() {
JavaClassPathAnalysisInputLocation inputLocation =
new JavaClassPathAnalysisInputLocation("../shared-test-resources/bugfixes/");
JavaView view = new JavaView(Collections.singletonList(inputLocation));

JavaSootMethod method =
view.getMethod(
JavaIdentifierFactory.getInstance()
.parseMethodSignature("<NestedMethodCall: void nestedMethodCall()>"))
.get();
assertEquals(
"this := @this: NestedMethodCall;\n"
+ "i = 0;\n"
+ "s = \"abc\";\n"
+ "i = i + 1;\n"
+ "$stack5 = virtualinvoke s.<java.lang.String: char charAt(int)>(i);\n"
+ "$stack3 = i;\n"
+ "i = i + 1;\n"
+ "$stack4 = virtualinvoke s.<java.lang.String: char charAt(int)>($stack3);\n"
+ "virtualinvoke this.<NestedMethodCall: void decode(char,char)>($stack5, $stack4);\n"
+ "\n"
+ "return;",
method.getBody().getStmtGraph().toString().trim());
}
}

0 comments on commit d61e334

Please sign in to comment.