Skip to content

Commit

Permalink
Correctly track parameter register when collapsing
Browse files Browse the repository at this point in the history
Reference #76
  • Loading branch information
CalebFenton committed Oct 17, 2018
1 parent b076217 commit ef56e5b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
11 changes: 4 additions & 7 deletions smalivm/src/main/java/org/cf/smalivm/VirtualMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,11 @@ private void collapseMultiverse(VirtualMethod calledMethod, ExecutionGraph graph
int parameterRegister = graph.getNodePile(0).get(0).getContext().getMethodState().getParameterStart();
for (int parameterIndex = 0; parameterIndex < parameterTypes.size(); parameterIndex++) {
String type = parameterTypes.get(parameterIndex);
if (configuration.isImmutable(type)) {
continue;
if (configuration.isMutable(type)) {
HeapItem item = getMutableParameterConsensus(terminatingAddresses, graph, parameterRegister);
int register = parameterRegisters[parameterIndex];
callerMethodState.assignRegisterAndUpdateIdentities(register, item);
}

HeapItem item = getMutableParameterConsensus(terminatingAddresses, graph, parameterRegister);
int register = parameterRegisters[parameterIndex];
callerMethodState.assignRegisterAndUpdateIdentities(register, item);

parameterRegister += Utils.getRegisterSize(type);
}
}
Expand Down
8 changes: 8 additions & 0 deletions smalivm/src/test/java/org/cf/smalivm/opcode/InvokeOpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ public void invokeMutateStringBuilderDoesMutateParameter() {
VMTester.test(CLASS_NAME, "invokeMutateStringBuilder()V", initial, expected);
}

@Test
public void invokeMutateStringBuilderAsThirdParameterAndFirstMutableParameterDoesMutateParameter() {
initial.setRegisters(0, 0x10, "I", 1, 0x20, "I", 2, new StringBuilder("i have been"), "Ljava/lang/StringBuilder;");
expected.setRegisters(0, 0x10, "I", 1, 0x20, "I", 2, new StringBuilder("i have been mutated"), "Ljava/lang/StringBuilder;");

VMTester.test(CLASS_NAME, "invokeMutateStringBuilderAsThirdParameterAndFirstMutableParameter()V", initial, expected);
}

@Test
public void invokeMutateStringDoesNotMutateParameter() {
initial.setRegisters(0, "not mutated", "Ljava/lang/String;");
Expand Down
19 changes: 19 additions & 0 deletions smalivm/src/test/resources/smali/ops/invoke_static_test.smali
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@
return-void
.end method

.method public static invokeMutateStringBuilderAsThirdParameterAndFirstMutableParameter()V
.locals 3

invoke-static {v0, v1, v2}, Linvoke_static_test;->mutateStringBuilderAsThirdParameterAndFirstMutableParameter(IILjava/lang/StringBuilder;)V

return-void
.end method

.method public static invokeNonExistentMethod()V
.locals 0

Expand Down Expand Up @@ -250,6 +258,17 @@
return-void
.end method

.method private static mutateStringBuilderAsThirdParameterAndFirstMutableParameter(IILjava/lang/StringBuilder;)V
.locals 1

const-string v0, " mutated"
invoke-virtual {p2, v0}, Ljava/lang/StringBuilder;->append(Ljava/lang/String;)Ljava/lang/StringBuilder;

const/4 p2, 0x1

return-void
.end method

# test assume maximum unknown
.method private static set0thElementOfFirstParameterTo0IfSecondParameterIs0([II)V
.locals 1
Expand Down

0 comments on commit ef56e5b

Please sign in to comment.