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 9. TYPE QUALIFIER INFERENCE 78 let tut9 (f : file) : unit = let g = fileBuildGraph f in let q = enqueueNodes g in findColoredNodes f g; processQueue g q; addInferredColors f g; eraseNodeMarks f 9.2 test/tut9.c This test demonstrates type qualifer inference. In particular, the types of the parameters r and g of function foo will be inferred. ../test/tut9.c # include struct bar { int red * red r; int green g; int red green blue c; }; void foo(int blue c, int r, int g) { return; } int main() { struct bar B; int red r = 50; B.r = &r; B.g = 50; B.c = AddColor(blue, 50); foo(B.c, *B.r, B.g); foo(B.g, r, r); } return 0; When we build this test by doing:

CHAPTER 9. TYPE QUALIFIER INFERENCE 79 $ ciltutcc --enable-tut9 --save-temps -o tut9 test/tut9.c We recieve a number of warnings because we have not erased our custom type qualiers from the source. We did this so that we could examine the results of type qualier inference. In particular, since we passed --save-temps to ciltutcc, we can look at the results in tut9.cil.c. When we do so, we can note the inferred types of the parameters r and g of foo: void foo(int blue c, int red r, int red green g) ... Since the type of parameter c was declared explicitly, its type qualier is not inferred to be anything else. Since only red ints were passed as parameter r, it is inered to be a red int. Finally, since both red and green ints were passed as parameter g, it is inferred to be both red and green. 9.3 Exercises 1. Modify typesAddEdge to recursively descend into types, adding edges as appropriate. Since this is just the inference stage, it is okay to ignore typing errors unless the problem is somehow catastrophic. 9.4 Further Reading As mentioned in earlier chapters, Deputy and CCured use type-qualier inference to infer how pointers are used, and thus what sort of safety checks their use requires. Additionally, SharC [1] is an extension to C's type system and language runtime that enforces programmer written type-qualiers describing how data may be shared among threads. As an optimization it uses a ow-insensitive type-qualier inference algorithm as a thread-escape analysis. In particular, if the compiler can determine that an object is thread-private, then there is no need to check accesses to it at runtime for data-races. The inference algorithm is seeded by the types of locations that are obviously thread-escaping, like global variables accessed in thread functions, or arguments to pthread create(). Then, the relevant type-qualiers ow across assignments as in the above tutorial. At the end of the inference algorithm, those locations without the type qualier indicating thread-escape must be thread-private.

CHAPTER 9. TYPE QUALIFIER INFERENCE 78<br />

let tut9 (f : file) : unit =<br />

let g = fileBuildGraph f in<br />

let q = enqueueNodes g in<br />

findColoredNodes f g;<br />

processQueue g q;<br />

addInferredColors f g;<br />

eraseNodeMarks f<br />

9.2 test/tut9.c<br />

This test demonstrates type qualifer inference. In particular, the types <strong>of</strong> the parameters r and g<br />

<strong>of</strong> function foo will be inferred.<br />

../test/tut9.c<br />

# include <br />

struct bar {<br />

int red * red r;<br />

int green g;<br />

int red green blue c;<br />

};<br />

void foo(int blue c, int r, int g)<br />

{<br />

return;<br />

}<br />

int main()<br />

{<br />

struct bar B;<br />

int red r = 50;<br />

B.r = &r;<br />

B.g = 50;<br />

B.c = AddColor(blue, 50);<br />

foo(B.c, *B.r, B.g);<br />

foo(B.g, r, r);<br />

}<br />

return 0;<br />

When we build this test by doing:

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

Saved successfully!

Ooh no, something went wrong!