summaryrefslogtreecommitdiff
path: root/cil/src/testcil.ml
blob: 0c0ef01846259630b8b61097626799b87bc0914d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
(*
 *
 * Copyright (c) 2001-2002, 
 *  George C. Necula    <necula@cs.berkeley.edu>
 *  Scott McPeak        <smcpeak@cs.berkeley.edu>
 *  Wes Weimer          <weimer@cs.berkeley.edu>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * 3. The names of the contributors may not be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *)

(* A test for CIL *)
open Pretty
open Cil
module E = Errormsg

let lu = locUnknown

(* If you have trouble try to reproduce the problem on a smaller type. Try 
 * limiting the maxNesting and integerKinds *)
let integerKinds = [ IChar; ISChar; IUChar; IInt; IUInt; IShort; IUShort;
                     ILong; IULong; ILongLong; IULongLong ] 
let floatKinds = [ FFloat; FDouble ] 
    
let baseTypes = 
       (List.map (fun ik -> (1, fun _ -> TInt(ik, []))) integerKinds)
     @ (List.map (fun fk -> (1, fun _ -> TFloat(fk, []))) floatKinds)


(* Make a random struct *)
let maxNesting  = ref 3  (* Maximum number of levels for struct nesting *)
let maxFields   = ref 8  (* The maximum number of fields in a struct *)
let useBitfields = ref false
let useZeroBitfields = ref true



(* Collect here the globals *)
let globals: global list ref = ref []
let addGlobal (g:global) = globals := g :: !globals
let getGlobals () = List.rev !globals
 
(* Collect here the statements for main *)
let statements: stmt list ref = ref []
let addStatement (s: stmt) = statements := s :: !statements
let getStatements () = List.rev !statements

(* Keep here the main function *)
let main: fundec ref = ref dummyFunDec
let mainRetVal: varinfo ref = ref dummyFunDec.svar

let assertId = ref 0 
let addAssert (b: exp) (extra: stmt list) : unit = 
  incr assertId;
  addStatement (mkStmt (If(UnOp(LNot, b, intType),
                           mkBlock (extra @ 
                                    [mkStmt (Return (Some (integer !assertId),
                                                     lu))]),
                           mkBlock [], lu)))

let addSetRetVal (b: exp) (extra: stmt list) : unit = 
  addStatement 
    (mkStmt (If(UnOp(LNot, b, intType),
                mkBlock (extra @ 
                         [mkStmtOneInstr (Set(var !mainRetVal, one, lu))]),
                mkBlock [], lu)))


let printfFun: fundec = 
  let fdec = emptyFunction "printf" in
  fdec.svar.vtype <- 
     TFun(intType, Some [ ("format", charPtrType, [])], true, []);
  fdec


let memsetFun: fundec = 
  let fdec = emptyFunction "memset" in
  fdec.svar.vtype <- 
     TFun(voidPtrType, Some [ ("start", voidPtrType, []);
                              ("v", intType, []);
                              ("len", uintType, [])], false, []);
  fdec

let checkOffsetFun: fundec = 
  let fdec = emptyFunction "checkOffset" in
  fdec.svar.vtype <- 
     TFun(voidType, Some [ ("start", voidPtrType, []);
                           ("len", uintType, []);
                           ("expected_start", intType, []);
                           ("expected_width", intType, []);
                           ("name", charPtrType, []) ], false, []);
  fdec

let checkSizeOfFun: fundec = 
  let fdec = emptyFunction "checkSizeOf" in
  fdec.svar.vtype <- 
     TFun(voidType, Some [ ("len", uintType, []);
                           ("expected", intType, []);
                           ("name", charPtrType, []) ], false, []);
  fdec


let doPrintf format args = 
  mkStmtOneInstr (Call(None, Lval(var printfFun.svar),
                       (Const(CStr format)) :: args, lu))


(* Select among the choices, each with a given weight *)
type 'a selection = int * (unit -> 'a)
let select (choices: 'a selection list) : 'a = 
  (* Find the total weight *)
  let total = List.fold_left (fun sum (w, _) -> sum + w) 0 choices in
  if total = 0 then E.s (E.bug "Total for choices = 0\n");
  (* Pick a random number *)
  let thechoice = Random.int total in
  (* Now get the choice *)
  let rec loop thechoice = function
      [] -> E.s (E.bug "Ran out of choices\n")
    | (w, c) :: rest -> 
        if thechoice < w then c () else loop (thechoice - w) rest
  in
  loop thechoice choices


(* Generate a new name *)
let nameId = ref 0
let newName (base: string) = 
  incr nameId;
  base ^ (string_of_int !nameId)


(********** Testing of SIZEOF ***********)

(* The current selection of types *)
let typeChoices : typ selection list ref = ref [] 

let baseTypeChoices : typ selection list ref = ref []


let currentNesting = ref 0
let mkCompType (iss: bool) =  
  if !currentNesting >= !maxNesting then (* Replace it with an int *)
    select !baseTypeChoices
  else begin
    incr currentNesting;
    let ci = 
      mkCompInfo iss (newName "comp") 
        (fun _ -> 
          let nrFields = 1 + (Random.int !maxFields) in
          let rec mkFields (i: int) =
            if i = nrFields then [] else begin
              let ft = select !typeChoices in
              let fname = "f" ^ string_of_int i in
              let fname', width = 
                if not !useBitfields || not (isIntegralType ft) 
                                     || (Random.int 8 >= 6) then 
                  fname, None 
                else begin
                  let tw = bitsSizeOf ft in (* Assume this works for TInt *)
                  let w = (if !useZeroBitfields then 0 else 1) + 
                          Random.int (3 * tw / 4) in
                  (if w = 0 then "___missing_field_name" else fname), Some w
                end
              in
              (fname', ft, width, [], lu) :: mkFields (i + 1) 
            end
          in
          mkFields 0)
        []
    in
    decr currentNesting;
    (* Register it with the file *)
    addGlobal (GCompTag(ci, lu));
    TComp(ci, [])
  end

(* Make a pointer type. They are all equal so make one to void *)
let mkPtrType () = TPtr(TVoid([]), [])

(* Make an array type. *)
let mkArrayType () = 
  if !currentNesting >= !maxNesting then 
    select !baseTypeChoices
  else begin
    incr currentNesting;
    let at = TArray(select !typeChoices, Some (integer (1 + (Random.int 32))),
                    []) in
    decr currentNesting;
    at
  end
  

let testSizeOf () = 
  let doOne (i: int) = 
(*    ignore (E.log "doOne %d\n" i); *)
    (* Make a random type *)
    let t = select !typeChoices in
    (* Create a global with that type *)
    let g = makeGlobalVar (newName "g") t in
    addGlobal (GVar(g, {init=None}, lu));
    addStatement (mkStmtOneInstr(Call(None, Lval(var memsetFun.svar),
                                      [ mkAddrOrStartOf (var g); zero;
                                        SizeOfE(Lval(var g))], lu)));
    try
(*      if i = 0 then ignore (E.log "0: %a\n" d_plaintype t); *)
      let bsz = 
        try bitsSizeOf t  (* This is what we are testing *)
        with e -> begin
          ignore (E.log "Exception %s caught while computing bitsSizeOf(%a)\n"
                    (Printexc.to_string e) d_type t);
          raise (Failure "")
        end
      in
(*      ignore (E.log "1 "); *)
      if bsz mod 8 <> 0 then begin
        ignore (E.log "bitsSizeOf did not return a multiple of 8\n");
        raise (Failure "");
      end;
(*      ignore (E.log "2 "); *)
      (* Check the offset of all fields in there *)
      let rec checkOffsets (lv: lval) (lvt: typ) = 
        match lvt with
          TComp(c, _) -> 
            List.iter 
              (fun f -> 
                if f.fname <> "___missing_field_name" then 
                  checkOffsets (addOffsetLval (Field(f, NoOffset)) lv) f.ftype)
              c.cfields
        | TArray (bt, Some len, _) -> 
            let leni = 
              match isInteger len with
                Some i64 -> Int64.to_int i64
              | None -> E.s (E.bug "Array length is not a constant")
            in
            let i = Random.int leni in
            checkOffsets (addOffsetLval (Index(integer i, NoOffset)) lv) bt

        | _ -> (* Now a base type *)
            let _, off = lv in 
            let start, width = bitsOffset t off in
            let setLv (v: exp) = 
              match lvt with
                TFloat (FFloat, _) -> 
                  Set((Mem (mkCast (AddrOf lv) intPtrType), NoOffset),
                      v, lu)
              | TFloat (FDouble, _) -> 
                  Set((Mem (mkCast (AddrOf lv) 
                              (TPtr(TInt(IULongLong, []), []))), NoOffset),
                      mkCast v (TInt(IULongLong, [])), lu)

              | (TPtr _ | TInt((IULongLong|ILongLong), _)) -> 
                  Set(lv, mkCast v lvt, lu)
              | _ -> Set(lv, v, lu)
            in
            let ucharPtrType = TPtr(TInt(IUChar, []), []) in
            let s = 
              mkStmt (Instr ([ setLv mone;
                               Call(None, Lval(var checkOffsetFun.svar),
                                    [ mkCast (mkAddrOrStartOf (var g))
                                             ucharPtrType;
                                      SizeOfE (Lval(var g));
                                      integer start; 
                                      integer width;
                                      (Const(CStr(sprint 80 
                                                    (d_lval () lv))))],lu);
                               setLv zero])) in
            addStatement s
      in
      checkOffsets (var g) t;
(*      ignore (E.log "3 ");*)
      (* Now check the size of *)
      let s = mkStmtOneInstr (Call(None, Lval(var checkSizeOfFun.svar),
                                   [ SizeOfE (Lval (var g));
                                     integer (bitsSizeOf t);
                                     mkString g.vname ], lu)) in
      addStatement s;
(*      ignore (E.log "10\n"); *)
    with _ -> ()
  in

  (* Make the composite choices more likely *)
  typeChoices := 
     [ (1, mkPtrType); 
       (5, mkArrayType);
       (5, fun _ -> mkCompType true);
       (5, fun _ -> mkCompType false); ]
     @ baseTypes;
  baseTypeChoices := baseTypes;
  useBitfields := false;
  maxFields := 4;
  for i = 0 to 100 do 
    doOne i
  done;
                   
  (* Now test the bitfields. *)
  typeChoices :=  [ (1, fun _ -> mkCompType true) ];
  baseTypeChoices := [(1, fun _ -> TInt(IInt, []))];
  useBitfields := true;

  for i = 0 to 100 do
    doOne i
  done;

  (* Now make it a bit more complicated *)
  baseTypeChoices := 
     List.map (fun ik -> (1, fun _ -> TInt(ik, []))) 
       [IInt; ILong; IUInt; IULong ];
  useBitfields := true;
  for i = 0 to 100 do
    doOne i
  done;

  (* An really complicated now *)
  baseTypeChoices := baseTypes;
  useBitfields := true;
  for i = 0 to 100 do 
    doOne i
  done;

  ()


(* Now the main tester. Pass to it the name of a command "cmd" that when 
 * invoked will compile "testingcil.c" and run the result *)
let createFile () = 

  assertId := 0;
  nameId := 0;

  (* Start a new file *)
  globals := [];
  statements := [];

  (* Now make a main function *)
  main := emptyFunction "main";
  !main.svar.vtype <- TFun(intType, None, false, []);
  mainRetVal := makeGlobalVar "retval" intType;

  addGlobal (GVar(!mainRetVal, {init=None}, lu));
  addGlobal (GText("#include \"testcil.h\"\n"));
  addStatement (mkStmtOneInstr(Set(var !mainRetVal, zero, lu)));

  (* Add prototype for printf *)
  addGlobal (GVar(printfFun.svar, {init=None}, lu));
  addGlobal (GVar(memsetFun.svar, {init=None}, lu));

  (* now fill in the composites and the code of main. For simplicity we add 
   * the statements of main in reverse order *)

  testSizeOf ();


  (* Now add a return 0 at the end *)
  addStatement (mkStmt (Return(Some (Lval(var !mainRetVal)), lu)));
  
  
  (* Add main at the end *)
  addGlobal (GFun(!main, lu));
  !main.sbody.bstmts <- getStatements ();

  (* Now build the CIL.file *)
  let file = 
    { fileName = "testingcil.c";
      globals  = getGlobals ();
      globinit = None;
      globinitcalled = false;
    } 
  in
  (* Print the file *)
  let oc = open_out "testingcil.c" in
  dumpFile defaultCilPrinter oc "testingcil.c" file;
  close_out oc

  



(* initialization code for the tester *)
let randomStateFile = "testcil.random"  (* The name of a file where we store 
                                         * the state of the random number 
                                         * generator last time *)
let doit (command: string) = 
  while true do
    (* Initialize the random no generator *)
    begin
      try
        let randomFile = open_in randomStateFile in
        (* The file exists so restore the Random state *)
        Random.set_state (Marshal.from_channel randomFile);
        ignore (E.log "!! Restoring Random state from %s\n" randomStateFile);
        close_in randomFile;
        (* Leave the file there until we succeed *)
      with _ -> begin
        (* The file does not exist *)
        Random.self_init ();
        (* Save the state of the generator *)
        let randomFile = open_out randomStateFile in
        Marshal.to_channel randomFile (Random.get_state()) [] ;
        close_out randomFile;
      end
    end;
    createFile ();
    (* Now compile and run the file *)
    ignore (E.log "Running %s\n" command);
    let err = Sys.command command in
    if err <> 0 then
      E.s (E.bug "Failed to run the command: %s (errcode=%d)" command err)
    else begin
      ignore (E.log "Successfully ran one more round. Press CTRL-C to stop\n");
      (* Delete the file *)
      Sys.remove randomStateFile
    end
  done