Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct the logic to truncate Stack map in CodeContext.restoreLocalVariables #148

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -1253,6 +1254,59 @@ class ReportedBugsTest extends CommonsCompilerTestSuite {
));
}

@Test public void
testPullRequest148() throws Exception {
ISimpleCompiler s = this.compilerFactory.newSimpleCompiler();
s.cook((""
+ "class MyClass {\n"
+ " private boolean b1;\n"
+ "\n"
+ " public MyClass(boolean b1) {\n"
+ " this.b1 = b1;\n"
+ " }\n"
+ "\n"
+ " public Object func(Object i) {\n"
+ " final int value_0;\n"
+ " // The bug still exist if the if condition is 'true'.\n"
+ " // Here we use a variable to make the test more robust,\n"
+ " // in case the compiler can eliminate the else branch.\n"
+ " if (b1) {\n"
+ " } else {\n"
+ " int field_0 = 1;\n"
+ " }\n"
+ " // The second if-else is necessary to trigger the bug.\n"
+ " if (b1) {\n"
+ " } else {\n"
+ " // The bug disappear if it's an int variable.\n"
+ " long field_1 = 2;\n"
+ " }\n"
+ " value_0 = 1;\n"
+ "\n"
+ " // The second final variable is necessary to trigger the bug.\n"
+ " final int value_2;\n"
+ " if (b1) {\n"
+ " } else {\n"
+ " int field_2 = 3;\n"
+ " }\n"
+ " value_2 = 2;\n"
+ "\n"
+ " return null;\n"
+ " }\n"
+ "}\n"
+ "\n"
+ "public class JaninoTest {\n"
+ " public static Object main() {\n"
+ " MyClass c = new MyClass(true);\n"
+ " return c.func(null);\n"
+ " }\n"
+ "}\n"
));

Class clazz = s.getClassLoader().loadClass("JaninoTest");
Method m = clazz.getDeclaredMethod("main");
Assert.assertNull(m.invoke(null));
}

public ClassLoader
compile(ClassLoader parentClassLoader, CompileUnit... compileUnits) {

Expand Down
25 changes: 13 additions & 12 deletions janino/src/main/java/org/codehaus/janino/CodeContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,25 +228,26 @@ class LocalScope {

this.currentLocalScope = scopeToPop.parent;

// Truncate the stack map.
// reuse local variable slots of the popped scope
this.nextLocalVariableSlot = scopeToPop.startingLocalVariableSlot;

// To truncate the stack map, it removes local variables
// indicated by the popped scope.
if (this.currentLocalScope != null) {
StackMap sm = this.currentInserter.getStackMap();
if (sm != null) {
int numLocalsInStackMap = 0;
if (sm != null && sm.locals().length > 0) {
int numActiveSlots = 0;
int nextLvIndex = 0;
for (VerificationTypeInfo slot : sm.locals()) {
if (slot != StackMapTableAttribute.TOP_VARIABLE_INFO) {
numLocalsInStackMap++;
}
}
while (numLocalsInStackMap-- > this.allLocalVars.size()) {
sm = sm.popLocal();
if (nextLvIndex >= this.nextLocalVariableSlot) break;
nextLvIndex += slot.category();
numActiveSlots += 1;
}
int numRemovedSlots = sm.locals().length - numActiveSlots;
while (numRemovedSlots-- > 0) sm = sm.popLocal();
this.currentInserter.setStackMap(sm);
}
}

// reuse local variable slots of the popped scope
this.nextLocalVariableSlot = scopeToPop.startingLocalVariableSlot;
}

/**
Expand Down