Wiki

Clone wiki

e0210-project / Project-1

Project-1: Counting dynamic branch instructions


1.1 Background


Static branch instruction is the branch instruction found in the source code of the program. Dynamic branch instruction is the instruction which will be executed when the program is executed. This means that the same static instruction may correspond to multiple dynamic branch instructions at run-time.

1.2 Requirements


In this project you will count the number of dynamic branch instructions for:

  1. Each method in the program
  2. For the entire program

1.2.1 Each method in the program

Whenever a method starts its execution, there should be a counter which records the number of branch instructions that are being executed in this method. This counter should be method-specific, which means that each method will have its own counter. Whenever a branch instruction is executed, the counter should be incremented by 1. When the method exits, the value of this counter should be printed on to the standard output stream. To achieve this, you need to instrument the program with statements that handles the counter operations. These things include counter initialization, counter increment and printing the value of the counter. You will be using the SOOT Local class to create a method-specific local variable. Jimple Add Expression to insert add statements into the program and finally some code to print this local to the output stream. To print the local variable onto the output stream you need to mimic the "System.out.println(local);" method call in the Jimple byte-code. There are multiple ways of doing this:

  1. You can add various instructions involved in making the method call into the program. This would involve creating another Jimple Local, creating an assignment statement and finally a virtual invoke statement which does the method call. (You can write a small program to print something in Java and see the Jimple code corresponding to that to know the various statements that needs to be inserted.) This approach will expose you to a various types of statements present in Jimple and ways of instrumenting these statements.
  2. The other method is to add Hooks. Hooks are custom Java classes and methods which contain code needed to carry out a specific task. Using Hooks, the only instrumentation required to do in the program is to add the method-calls to the Hooks at the required places. Hooks will mask the amount of statements that are actually instrumented into the program using SOOT. To do this, you create a Java class along with a few methods which performs the counter operations. You need to add this class to the Java classpath and also to the SOOT classpath for it to be recognized. After that you need to insert virtual invoke statements to the particular methods in the Hook to the program. This method will expose you to adding Hooks to the program which will reduce the amount of instrumentation done to the bytecode.

The source program will NOT have any print instructions. The only output should be the count of Dynamic branch instructions. The counter value should be emitted just before the method exits from the program.

1.2.2 For the entire program

Number of dynamic branch instructions for the entire program is equal to the sum of the dynamic branch instructions of all the methods in the program. Specifically, just before the main method exits, you need to print the total number of all the dynamic branch instructions executed in the program. The counter operations for this problem requires similar operations as discussed before. Since there a need for a global number, a global variable is required to do this task. You need to use Hooks to mimic the global counter operations.

NOTE:

  1. You also need to consider the constructors while adding instrumentation to the program.

  2. You do not need to consider Switch statements as branch statements.

1.3 Example

Program:

#!java

public static void main(String[] args) {

        int a = args[0].length();

        if (a > 5)
            a++;
        else
            a--;
        System.err.println(a);

        return;
    }

Expected output:

#!java


1

1

Explanation:

#!java

1 - The number of dynamic branch instructions in the main method

1 - The number of dynamic branch instructions in the entire program

Updated