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 4. INSTRUMENTATION 38 findOrCreateFunc : file → string → typ → varinfo. If the function is found, the varinfo for it is returned, otherwise a prototype for the function is added to the le, and the varinfo for it is returned. let initTutFunctions (f : file) : unit = let focf : string → typ → varinfo = findOrCreateFunc f in let bl type = mkFunTyp voidType ["f", charConstPtrType; "l", intType] in let el type = mkFunTyp voidType ["f", charConstPtrType; "l", intType; "c", intType; ] in tutfuns.begin loop ← focf begin loop str bl type; tutfuns.end loop ← focf end loop str el type 4.1.2 Loop instrumentation The function makeInstrStmts creates four statements that we'll add to loops. In the returned tuple, the rst statement calls the loop begin instrumentation function. The second statement initializes the iteration counter to zero. The third statement increments the iteration counter, and the fourth statement calls the loop end instrumentation function. In makeInstrStmts we use some shorthand. mkString turns an OCaml string into a Cil.exp constant expression. The integer function does the same for OCaml ints. The v2e function creates an exp out of a varinfo. The var function creates an lval from a varinfo, and the i2s function creates a stmt from an instr. All these can be found in Tututil or Cil. let makeInstrStmts (counter : varinfo) (loc : location) : stmt × stmt × stmt × stmt = let f, l = mkString loc.file, integer loc.line in i2s (Call(None, v2e tutfuns.begin loop, [f; l], loc)), i2s (Set(var counter, zero, loc)), i2s (Set(var counter, BinOp(PlusA, v2e counter, one, counter.vtype), loc)), i2s (Call(None, v2e tutfuns.end loop, [f; l; v2e counter], loc)) The class loopInstrumenterClass is a visitor that uses makeInstrStmts to instrument loops. In vstmt we update the statement s by writing to the mutable skind eld instead of rebuilding a whole new statement so that we don't have to worry about copying over all the other elds, which we'd like to remain the same. In particular, it is very easy to forget about statement labels. Further, we use ChangeDoChildrenPost because there might be nested loops. Remember: ChangeDoChildrenPost recurses into s's children before handling s. That way, we have no innite loops due to turning s into a statement that contains the original s. class loopInstrumenterClass (fd : fundec) = object(self) inherit nopCilVisitor

CHAPTER 4. INSTRUMENTATION 39 method vstmt (s : stmt) = let action s = match s.skind with | Loop(b, loc, co, bo) → let counter = makeTempVar fd intType in let ss, cis, is, es = makeInstrStmts counter loc in b.bstmts ← is :: b.bstmts; let nb = mkBlock [ss; cis; mkStmt s.skind; es] in s.skind ← Block nb; s | → s in ChangeDoChildrenPost(s, action) end The function processFunction applies the loopInstrumenterClass to a function. let processFunction (fd : fundec) (loc : location) : unit = let vis = new loopInstrumenterClass fd in ignore(visitCilFunction vis fd) The function tut4 is the entry point in the module. It applies processFunction to every function in a file. let tut4 (f : file) : unit = initTutFunctions f; iterGlobals f (onlyFunctions processFunction) Now we have added a bunch of function calls to the code. Several questions remain: • Where are these functions dened? In ciltut-lib/src/tut4.c • How does that le get built? We use cmake to congure and build the library libciltut. The build of the library can be adjusted by modifying the CMakeLists.txt les under the ciltut-lib directory. The Makefile generated by cmake is invoked from the root Makefile when ciltutcc is built. • How does that library get linked in to something that ciltutcc is building? There is a Perl script in lib/Ciltut.pm that wraps up this OCaml program to make it look like gcc (and a couple other compilers). It has a function called processArguments where we add the library to a list in @{$self → {CILTUTLIBS}}. When this Perl script detects that it is being used for the link stage of a build, it adds libciltut.a to the list of object les to link in.

CHAPTER 4. INSTRUMENTATION 39<br />

method vstmt (s : stmt) =<br />

let action s =<br />

match s.skind with<br />

| Loop(b, loc, co, bo) →<br />

let counter = makeTempVar fd intType in<br />

let ss, cis, is, es = makeInstrStmts counter loc in<br />

b.bstmts ← is :: b.bstmts;<br />

let nb = mkBlock [ss; cis; mkStmt s.skind; es] in<br />

s.skind ← Block nb;<br />

s<br />

| → s<br />

in<br />

ChangeDoChildrenPost(s, action)<br />

end<br />

The function processFunction applies the loopInstrumenterClass to a function.<br />

let processFunction (fd : fundec) (loc : location) : unit =<br />

let vis = new loopInstrumenterClass fd in<br />

ignore(visitCilFunction vis fd)<br />

The function tut4 is the entry point in the module. It applies processFunction to every function<br />

in a file.<br />

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

initTutFunctions f;<br />

iterGlobals f (onlyFunctions processFunction)<br />

Now we have added a bunch <strong>of</strong> function calls to the code. Several questions remain:<br />

• Where are these functions dened? In ciltut-lib/src/tut4.c<br />

• How does that le get built? We use cmake to congure and build the library libciltut.<br />

The build <strong>of</strong> the library can be adjusted by modifying the CMakeLists.txt les under the<br />

ciltut-lib directory. The Makefile generated by cmake is invoked from the root Makefile<br />

when ciltutcc is built.<br />

• How does that library get linked in to something that ciltutcc is building? There is a<br />

Perl script in lib/Ciltut.pm that wraps up this OCaml program to make it look like gcc<br />

(and a couple other compilers). It has a function called processArguments where we add the<br />

library to a list in @{$self → {<strong>CIL</strong>TUTLIBS}}. When this Perl script detects that it is being<br />

used for the link stage <strong>of</strong> a build, it adds libciltut.a to the list <strong>of</strong> object les to link in.

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

Saved successfully!

Ooh no, something went wrong!