29.01.2014 Views

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

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 5. INTERPRETED CONSTRUCTORS 43<br />

Show-stopping Errors and Bugs: The function Errormsg.s ignores its argument<br />

and throws the exception Errormsg.Error, which is only caught in<br />

main.ml. Thus, executing E.s(E.bug ...) or E.s(E.error ...) has the eect <strong>of</strong><br />

stopping compilation with the given error message. If compilation could continue<br />

despite the problemand a sensible value with the correct type could be<br />

generatedthen E.bug and E.error could be used without E.s. Then, compilation<br />

will continue, possibly generating further error messages.<br />

let zeroPtr (fd : fundec) (blv : lval) : stmt list =<br />

[i2s (Set(blv, CastE(voidPtrType, zero), locUnknown))]<br />

The function zeroType is the entry point for NULLing out pointers found by traversing the lvalue<br />

blv. If blv is a pointer, we NULL it out. If it is an array, we loop over the array NULLing any pointers<br />

found in traversing the base type. If it is a struct type, we NULL any pointers found in traversing<br />

the elds.<br />

let rec zeroType (fd : fundec) (blv : lval) : stmt list =<br />

match unrollType(typeOfLval blv) with<br />

| TPtr → zeroPtr fd blv<br />

| TArray → zeroArray fd blv<br />

| TComp → zeroComp fd blv<br />

| → [ ]<br />

The function zeroComp NULLs out the pointer elds <strong>of</strong> blv assuming blv has a TComp type. If the<br />

type <strong>of</strong> blv is a union, we just NULL out the rst pointer eld.<br />

and zeroComp (fd : fundec) (blv : lval) : stmt list =<br />

let ci = compinfoOfLval blv in<br />

let sl =<br />

ci.cfields<br />

|> L.map (zeroField fd blv)<br />

|> L.concat<br />

in<br />

if ci.cstruct then sl<br />

else if sl ≠ [ ] then [L.hd sl]<br />

else [ ]<br />

The function zeroField is used by zeroComp when iterating over the elds <strong>of</strong> a compinfo. It tacks<br />

the eld onto blv using the Cil function addOffsetLval and NULLs out any pointers in it by calling<br />

zeroType.

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

Saved successfully!

Ooh no, something went wrong!