I have identified a bug in my application (which processes bytecode using soot) that only arises on specific bytecode instructions.
I want to create a test for that specific case. However, I'm not able to reliably write test code, that will compile to the expected bytecode, which would then trigger the bug.
This is my attempt to trigger the bug:
public void updateRhsOnIfEq() {
int x = 15;
int y = AircraftControl.readSensor(0);
// FIXME != in bytecode instead of ==
if (x == y) {
AircraftControl.readSensor(y);
}
else {
AircraftControl.readSensor(x);
}
}
The problem is, that the compiler changes the branch logic by inverting the comparison and switching the two branches. As you can see in the bytecode below, it does a !=
comparison instead of ==
. However, the bug I'm testing for is only triggered by a ==
.
public void updateRhsOnIfEq();
0 bipush 15
2 istore_1 [x]
3 iconst_0
4 invokestatic AircraftControl.readSensor(int) : int [17]
7 istore_2 [y]
8 iload_1 [x]
9 iload_2 [y]
10 if_icmpne 21 <============================== Should be if_icmpeq
13 iload_2 [y]
14 invokestatic AircraftControl.readSensor(int) : int [17]
17 pop
18 goto 26
21 iload_1 [x]
22 invokestatic AircraftControl.readSensor(int) : int [17]
25 pop
26 return
Is there a way to write test cases that need to result in predictable bytecode easily? Is this possible at all given that there are different Java compilers, versions thereof etc?