How to get all executable paths through the backward slice obtained by Slicer
when I get all the statments through Slicer#computeBackwardSlice(), can I analyze all possible execution paths to the seed? The result I expect is a collection of linked lists, each linked list represents an execution path. Due to reasons such as if-else, multiple linked lists may be generated。
Looking forward to your reply, thank you very much !!
The Slicer basically returns a set of statements. There is no built-in functionality to split this set up based on different execution paths to the seed. So you would have to build in this logic yourself. Relatedly, WALA does not have functionality for constructing executable slices.
Thank you very much for your reply。I also want to ask, can I implement the above functions based on Wala's existing capabilities, such as through SDG, or is it theoretically not feasible?
It is feasible, though to distinguish control-flow paths you will need control-flow graphs, not just an SDG. I don't know how much work it will be. It will take a while to learn the relevant WALA APIs.
Hi @ligenPro I was working for several months on a slicer built on top of WALA Shirke. I used a Control-Flow-Graph, Data-Dependency-Graph, Argument-Dependency-Graph (instructions that are consumed by a fixed instruction using stack simulation) and Block-Dependency-Graph. For now, it just works for a single specific method but multiple slicing criterion's. The criterion's must be given as instruction indexes in which you are interested in. The output is an executable jar with the sliced method. I am currently working on creating tests for validation and more complex examples for covering the most important jvm bytecode instructions. Documentation is missing as well, so it might take some time until I am satisfied to publish the state of the project.
So what you mean is: based on the given instruction index, you can perform similar Intra-procedural analysis on a single method to get an executable jar package?
I do not know what you mean with in-process analysis. The slicer is performing a static backward slice, keeping just the dependent instructions necessary to execute a certain instruction (or multiple). Here is an example: Assume you have the following method
public class SlicerValidation {
public void simpleMethodCallAndLoopWithParameter(long sleep) throws InterruptedException {
for (int i = 0; i < 5; i++) {
Thread.sleep(sleep);
}
}
}
The method bytecode read by WALA Shrike is as the following:
=== Method LSlicerValidation;.simpleMethodCallAndLoopWithParameter(J)V ===
0: Constant(I,0)
1: LocalStore(I,3)
2: Goto(9)
3: LocalLoad(J,1)
4: Invoke(STATIC,Ljava/lang/Thread;,sleep,(J)V)
5: LocalLoad(I,3)
6: Constant(I,1)
7: BinaryOp(I,add)
8: LocalStore(I,3)
9: LocalLoad(I,3)
10: Constant(I,5)
11: ConditionalBranch(I,lt,3)
12: Return(V)
If you are interested in slicing, let say ... instruction index 5 (the load instruction for the variable i). The executable slice will be:
=== Sliced method ===
0: Constant(I,0)
1: LocalStore(I,3)
2: Invoke(STATIC,LNothing;,doNothing,()V) // Just a placeholder here for the begin of loop (jump target of ConditionalBranch below) since inserting "nop" instructions are not possible in Shrike
3: LocalLoad(I,3)
4: Constant(I,1)
5: BinaryOp(I,add)
6: LocalStore(I,3)
7: LocalLoad(I,3)
8: Constant(I,5)
9: ConditionalBranch(I,lt,3)
10: Return(V)
As you can see, the execution of Thread.sleep() is sliced out by performing slicing on various graphs. There are some graphs attached which are generated from the original method.
By the way: There is an error in my slice currently. The Goto(9)-Instruction should be kept as well, but loops are really tricky to slice efficiently. I am working on this. diagrams.zip
I forgot to add the source of the output above. Here is my current way generating the slice:
@Test
public void testSimpleSlice() throws IOException, InvalidClassFileException, InterruptedException,
IllegalStateException, DecoderException {
slicer.setInputJar(slicerValidationJarPath); // Path to your application.jar
slicer.setMethodSignature(
"LSlicerValidation;.simpleMethodCallAndLoopWithParameter(J)V");
System.out.println(slicer.getMethodSummary());
// Define you criterion's from some instruction indexes
// Index 5 here is: LocalLoad(I,3) in the method
List<Integer> instructionIndexes = Arrays.asList(5);
Set<Integer> criterionSet = new HashSet<>(instructionIndexes);
slicer.setInstructionIndexes(criterionSet);
// Variant 1: Get the instruction slice as a list of instructions
SliceResult sliceResult = slicer.getSliceResult();
List<IInstruction> slice = sliceResult.getSlice();
System.out.println("=== Sliced method ===");
int index = 0;
for (IInstruction instruction : slice) {
System.out.println(index + ": " + instruction.toString());
index++;
}
// Variant 2: Generate a new jar with all dependencies from your original one,
// but with a sliced method specified above.
String slicedOutputJar = slicer.makeSlicedFile();
System.out.println("Sliced output jar path" + slicedOutputJar);
}