A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich A CIL Tutorial - Department of Computer Science - ETH Zürich

29.01.2014 Views

CHAPTER 3. DATAFLOW ANALYSIS 32 3.2 test/tut3.c The result of the analysis in tut3.ml will be to print a message to the console for each variable of integral type wherever it is used indicating whether it is even or odd at that program point. We consider the results of the analysis on the code below: ../test/tut3.c # include int main() { int a,b,c,d; a = 1; b = 2; c = 3; d = 4; a += b + c; c *= d - b; b -= d + a; if (a % 2) a++; printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d); return 0; } We build this source with the dataow analysis enabled by doing: $ ciltutcc --enable-tut3 -o tut3 test/tut3.c test/tut3.c:14: (a, Bottom) test/tut3.c:14: (b, Bottom) test/tut3.c:14: (c, Bottom) test/tut3.c:14: (d, Bottom) test/tut3.c:15: (a, Odd) test/tut3.c:15: (a, Odd) test/tut3.c:15: (b, Even) test/tut3.c:15: (c, Odd) test/tut3.c:16: (c, Odd) test/tut3.c:16: (c, Odd) test/tut3.c:16: (d, Even) test/tut3.c:16: (b, Even) test/tut3.c:17: (b, Even) test/tut3.c:17: (b, Even) test/tut3.c:17: (d, Even) test/tut3.c:17: (a, Even) test/tut3.c:18: (a, Even) test/tut3.c:18: (a, Even) test/tut3.c:18: (a, Even) test/tut3.c:19: (a, Top) test/tut3.c:19: (b, Even)

CHAPTER 3. DATAFLOW ANALYSIS 33 test/tut3.c:19: test/tut3.c:19: (c, Even) (d, Even) Since the program is small, we can check by inspection that the results of the analysis are correct. Note, however, that for the variable a at line 19, the result is Top. This occurs because the analysis made no attempt to interpret the guard of the if-statement on that line. Thus, at the join point after the if-statement, as far as this analysis knows, a could be either Even or Odd. 3.3 Exercises 1. Create an option in src/ciltutoptions.ml that controls whether debugging information for the analysis is printed. 2. We could represent the mapping from variables to oekinds many other ways. If the analysis had to be super-fast, we could use bitsets. Then, the abstract state would be four bitsets, one for each oekind, with each local variable of integral type belonging to exactly one of the bitsets. Note the Bitmap module in cil/ocamlutil/bitmap.ml. 3. In oekind of unop for the LNot case, there are two cases e = 0 and e ≠ 0. If we know that e will be zero, then !e will be 1 and therefore Odd. If we know that e will be non-zero, then !e will be zero and therefore Even. If we don't know anything about e, then !e is Top as we have here. Modify this analysis to also track whether variables are zero or non-zero. (Such an analysis can have wider applications. Think about pointers.) 4. There are a number of other binary operations. Figure out how to handle them in oekind of binop. item Modify the analysis to handle not only local variables, but also global variables. What should their initial state be? How would the analysis change in the presence of multiple threads? 5. Implement the doGuard function of OddEvenDF so that the analysis may conclude that a is Even at line 19. Project: Add another layer of generalization on top of CIL's Dataflow functors so that one must only dene the functions operating on the abstract state (and possibly also a function for giving the initial state for a function.) One should then get a function for invoking the analysis and a visitor class for visiting the AST with the results of the analysis. (Essentially everything from the module OddEvenDF and down to the end of vmlVisitorClass, except for collectVars, should be automatic).

CHAPTER 3. DATAFLOW ANALYSIS 32<br />

3.2 test/tut3.c<br />

The result <strong>of</strong> the analysis in tut3.ml will be to print a message to the console for each variable <strong>of</strong><br />

integral type wherever it is used indicating whether it is even or odd at that program point. We<br />

consider the results <strong>of</strong> the analysis on the code below:<br />

../test/tut3.c<br />

# include <br />

int main()<br />

{<br />

int a,b,c,d;<br />

a = 1; b = 2; c = 3; d = 4;<br />

a += b + c;<br />

c *= d - b;<br />

b -= d + a;<br />

if (a % 2) a++;<br />

printf("a = %d, b = %d, c = %d, d = %d\n", a, b, c, d);<br />

return 0;<br />

}<br />

We build this source with the dataow analysis enabled by doing:<br />

$ ciltutcc --enable-tut3 -o tut3 test/tut3.c<br />

test/tut3.c:14: (a, Bottom)<br />

test/tut3.c:14: (b, Bottom)<br />

test/tut3.c:14: (c, Bottom)<br />

test/tut3.c:14: (d, Bottom)<br />

test/tut3.c:15: (a, Odd)<br />

test/tut3.c:15: (a, Odd)<br />

test/tut3.c:15: (b, Even)<br />

test/tut3.c:15: (c, Odd)<br />

test/tut3.c:16: (c, Odd)<br />

test/tut3.c:16: (c, Odd)<br />

test/tut3.c:16: (d, Even)<br />

test/tut3.c:16: (b, Even)<br />

test/tut3.c:17: (b, Even)<br />

test/tut3.c:17: (b, Even)<br />

test/tut3.c:17: (d, Even)<br />

test/tut3.c:17: (a, Even)<br />

test/tut3.c:18: (a, Even)<br />

test/tut3.c:18: (a, Even)<br />

test/tut3.c:18: (a, Even)<br />

test/tut3.c:19: (a, Top)<br />

test/tut3.c:19: (b, Even)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!