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 7. TYPE QUALIFIERS 56 class colorEraserVisitor = object(self) inherit nopCilVisitor method vattr (a : attribute) = match a with | Attr(s, ) when L.mem s color strings → ChangeTo [ ] | → DoChildren end The function eraseColors invokes the visitor colorEraserVisitor on a le. let eraseColors (f : file) : unit = let vis = new colorEraserVisitor in visitCilFile vis f The tut7 function is the entry point for this module. It checks the color types in all functions. let tut7 (f : file) : unit = iterGlobals f (onlyFunctions checkColorTypes); eraseColors f 7.2 test/tut7.c In this test, we declare a global blue integer b, and a local green integer g. We initialize g using the AddColor macro, which is dened in ciltut.h. It simply casts the constant in the second argument to the given color. Then, we attempt to assign g to b, which should elicit a warning from the compiler. # include int blue b; int main() { int green g = AddColor(green, 5); b = g; return 0; } ../test/tut7.c Now, when we attempt to compile this test, we get a warning:

CHAPTER 7. TYPE QUALIFIERS 57 $ ciltutcc enable-tut7 -o tut7 test/tut7.c Warning: test/tut7.c:16: color mismatch: int attribute (( blue )) int attribute (( green )) Which is what we expected. 7.3 Exercises 1. Modify typecheck result and colorTypesCompat to give more information when types do not match. For example, instead of returning TypesMismatch, colorTypesCompat might return PtrIntMismatch when t1 is a TPtr and t2 is a TInt (but the colors still match). 2. Corret the rule for function types in colorTypesCompat. Possibly add (a) new constructor(s) to typecheck result for the case when function types do not match. 3. In combination with the above two exercises. Generalize the type checking code in colorTypesCompat. Instead of checking the color qualiers, however, colorTypesCompat would accept a function argument for deciding whether the set of type attributes on t1 and t2 are compatible. 4. Write the Call case in colorCheckVisitor#vinst. Extract the type of the function from fe. Check the return type of the function against the destination of the return value (rlvo). Check the types of the actual arguments (args) against the types of the formal parameters. Note that there might be more actuals than formals if it is a variable argument function! 5. The attribute syntax in the warning message above could be cleaned up by inheriting from Cil's defaultCilPrinterClass and overriding the methods for printing attributes. 7.4 Further Reading Researchers have added ow-sensitive [3], and insensitive type-qualiers [2], type-qualier inference, and type-qualier polymorphism [1] to languages such as C and Java [4]. In particular, the CCured [5] tool used ow-insensitive type-qualier inference to determine the kind of fat pointer needed to check the correctness of pointer arithmetic in C, among other purposes.

CHAPTER 7. TYPE QUALIFIERS 56<br />

class colorEraserVisitor = object(self)<br />

inherit nopCilVisitor<br />

method vattr (a : attribute) =<br />

match a with<br />

| Attr(s, ) when L.mem s color strings → ChangeTo [ ]<br />

| → DoChildren<br />

end<br />

The function eraseColors invokes the visitor colorEraserVisitor on a le.<br />

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

let vis = new colorEraserVisitor in<br />

visitCilFile vis f<br />

The tut7 function is the entry point for this module. It checks the color types in all functions.<br />

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

iterGlobals f (onlyFunctions checkColorTypes);<br />

eraseColors f<br />

7.2 test/tut7.c<br />

In this test, we declare a global blue integer b, and a local green integer g. We initialize g using<br />

the AddColor macro, which is dened in ciltut.h. It simply casts the constant in the second<br />

argument to the given color. Then, we attempt to assign g to b, which should elicit a warning from<br />

the compiler.<br />

# include <br />

int blue b;<br />

int main()<br />

{<br />

int green g = AddColor(green, 5);<br />

b = g;<br />

return 0;<br />

}<br />

../test/tut7.c<br />

Now, when we attempt to compile this test, we get a warning:

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

Saved successfully!

Ooh no, something went wrong!