package frama-c

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file cfgDump.ml

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
(**************************************************************************)
(*                                                                        *)
(*  This file is part of WP plug-in of Frama-C.                           *)
(*                                                                        *)
(*  Copyright (C) 2007-2024                                               *)
(*    CEA (Commissariat a l'energie atomique et aux energies              *)
(*         alternatives)                                                  *)
(*                                                                        *)
(*  you can redistribute it and/or modify it under the terms of the GNU   *)
(*  Lesser General Public License as published by the Free Software       *)
(*  Foundation, version 2.1.                                              *)
(*                                                                        *)
(*  It is distributed in the hope that it will be useful,                 *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *)
(*  GNU Lesser General Public License for more details.                   *)
(*                                                                        *)
(*  See the GNU Lesser General Public License version 2.1                 *)
(*  for more details (enclosed in the file licenses/LGPLv2.1).            *)
(*                                                                        *)
(**************************************************************************)

(**************************************************************************)

let _dkey = "cfgdump" (* debugging key *)

let fc = ref None
let out = ref Format.std_formatter
let knode = ref 0
let node () = incr knode ; !knode

let fopen kf bhv =
  begin
    let name =
      match bhv with
      | None -> Kernel_function.get_name kf
      | Some bname -> Kernel_function.get_name kf ^ "_" ^ bname
    in
    let file = Filename.concat (Wp_parameters.get_output () :> string) name in
    Wp_parameters.feedback "CFG %a -> %s@." Kernel_function.pretty kf name ;
    let fout = open_out (file ^ ".dot") in
    fc := Some (fout,file) ;
    out := Format.formatter_of_out_channel fout ;
    Format.fprintf !out "digraph %a {@\n" Kernel_function.pretty kf ;
    Format.fprintf !out "  rankdir = TB ;@\n" ;
    Format.fprintf !out "  node [ style = filled, shape = box ] ;@\n" ;
    Format.fprintf !out "  N000 [ color = red, shape = circle, label = \"*\" ] ;@\n" ;
  end

let flush () =
  begin
    Format.fprintf !out "}@." ;
    out := Format.std_formatter ;
    match !fc with
    | None -> ()
    | Some (fout,file) ->
      close_out fout ;
      ignore (Sys.command
                (Printf.sprintf "dot -Tpdf %s.dot > %s.pdf" file file))
  end

(* -------------------------------------------------------------------------- *)
(* --- MCFG Interface                                                     --- *)
(* -------------------------------------------------------------------------- *)

type t_prop = int (* current node *)

let pretty fmt k = Format.fprintf fmt "N%03d" k

let link a b =
  if b =0
  then Format.fprintf !out " %a -> %a [ style=dotted ];@." pretty a pretty b
  else Format.fprintf !out " %a -> %a ;@." pretty a pretty b

let merge _env k1 k2 =
  if k1=0 then k2 else
  if k2=0 then k1 else
    let u = node () in
    Format.fprintf !out "  %a [ label=\"\" , shape=circle ] ;@." pretty u ;
    link u k1 ; link u k2 ; u

let empty = 0

let has_init _ = false

type t_env = Kernel_function.t

let new_env ?lvars kf : t_env = ignore lvars ; kf

let add_axiom _p _l = ()

let add_probe _env ?stmt p t k =
  ignore stmt ;
  let u = node () in
  if Wp_parameters.debug_atleast 1 then
    Format.fprintf !out "  %a [ label=\"Probe %a\" ] ;@." pretty u Printer.pp_term t
  else
    Format.fprintf !out "  %a [ label=\"Probe %s\" ] ;@." pretty u p ;
  Format.fprintf !out "  %a -> %a [ style=dotted ] ;@." pretty u pretty k ;
  link u k ; u

let add_hyp ?for_pid _env (pid,pred) k =
  ignore(for_pid);
  let u = node () in
  if Wp_parameters.debug_atleast 1 then
    Format.fprintf !out "  %a [ color=green , label=\"Assume %a%a\"] ;@."
      pretty u Printer.pp_predicate pred
      (Pretty_utils.pp_opt ~pre:" for" WpPropId.pretty) for_pid
  else
    Format.fprintf !out "  %a [ color=green , label=\"Assume %a%a\"] ;@."
      pretty u WpPropId.pp_propid pid
      (Pretty_utils.pp_opt ~pre:" for" WpPropId.pretty) for_pid ;
  link u k ; u

let add_goal env (pid,pred) k =
  let u = node () in
  if Wp_parameters.debug_atleast 1 then
    Format.fprintf !out "  %a [ color=red , label=\"Prove %a\" ] ;@." pretty u Printer.pp_predicate pred
  else
    Format.fprintf !out "  %a [ color=red , label=\"Prove %a\" ] ;@." pretty u WpPropId.pp_propid pid ;
  Format.fprintf !out "  %a -> %a [ style=dotted ] ;@." pretty u pretty k ;
  merge env u k

let add_terminates_subgoal env (pid, _) ?deps prop stmt _source k =
  ignore deps ;
  let u = node () in
  if Wp_parameters.debug_atleast 1 then
    Format.fprintf !out "  %a [ color=red , label=\"Prove %a (%a)\" ] ;@."
      pretty u
      Cil_printer.pp_stmt stmt
      Printer.pp_predicate prop
  else
    Format.fprintf !out "  %a [ color=red , label=\"Prove %a\" ] ;@."
      pretty u WpPropId.pp_propid pid ;
  merge env u k

let pp_assigns fmt = function
  | Cil_types.WritesAny -> Format.pp_print_string fmt " \\everything"
  | Cil_types.Writes [] -> Format.pp_print_string fmt " \\nothing"
  | Cil_types.Writes froms ->
    List.iter
      (fun (t,_) -> Format.fprintf fmt "@ %a" Printer.pp_identified_term t)
      froms

let add_assigns env (pid,_) k =
  let u = node () in
  Format.fprintf !out "  %a [ color=red , label=\"Assigns %a\" ] ;@." pretty u WpPropId.pp_propid pid ;
  Format.fprintf !out "  %a -> %a [ style=dotted ] ;@." pretty u pretty k ;
  merge env u k

let use_assigns _env region d k =
  let u = node () in
  begin match region with
    | None ->
      Format.fprintf !out "  %a [ color=orange , label=\"Havoc All\" ] ;@." pretty u
    | Some pid ->
      Format.fprintf !out "  %a [ color=orange , label=\"Havoc %a:\n@[<hov 2>assigns%a;@]\" ] ;@."
        pretty u WpPropId.pp_propid pid
        pp_assigns d.WpPropId.a_assigns
  end ;
  link u k ; u

let label _env stmt label k =
  if Clabels.is_here label then k else
    let u = node () in
    ( match stmt with
      | None ->
        Format.fprintf !out "  %a [ label=\"Label %a\" ] ;@." pretty u Clabels.pretty label
      | Some s ->
        Format.fprintf !out "  %a [ label=\"Label %a (Stmt s%d)\" ] ;@." pretty u Clabels.pretty label s.Cil_types.sid
    ) ;
    link u k ; u

let assign _env _stmt x e k =
  let u = node () in
  Format.fprintf !out "  %a [ color=orange , label=\"%a := %a\" ] ;@." pretty u
    Printer.pp_lval x Printer.pp_exp e ;
  link u k ; u

let return _env _stmt r k =
  let u = node () in
  begin
    match r with
    | None ->
      Format.fprintf !out "  %a [ color=orange , label=\"Return\" ] ;@." pretty u
    | Some e ->
      Format.fprintf !out "  %a [ color=orange , label=\"Return %a\" ] ;@." pretty u
        Printer.pp_exp e
  end ;
  link u k ; u

let test _env _stmt e k1 k2 =
  let u = node () in
  Format.fprintf !out "  %a [ color=cyan , label=\"If %a\" ] ;@."
    pretty u Printer.pp_exp e ;
  link u k1 ; link u k2 ; u

let switch _env _stmt e cases def =
  let u = node () in
  Format.fprintf !out "  %a [ color=cyan , label=\"Switch %a\" ] ;@."
    pretty u Printer.pp_exp e ;
  List.iter (fun (es,k) ->
      let c = node () in
      Format.fprintf !out "  %a [ color=orange , label=\"Case %a\" ] ;@."
        pretty c (Pretty_utils.pp_list ~sep:"," Printer.pp_exp) es ;
      link u c ;
      link c k ;
    ) cases ;
  let d = node () in
  Format.fprintf !out "  %a [ color=orange , label=\"Default\" ] ;@." pretty d ;
  link u d ;
  link d def ; u

let const _ x k =
  let u = node () in
  Format.fprintf !out "  %a [ color=orange, label=\"const %a\" ] ;@."
    pretty u Printer.pp_lval (Cil.var x) ;
  link u k ; u

let init _ x init k =
  let u = node () in
  let pp_init fmt = function
    | None -> Format.pp_print_string fmt "<default>"
    | Some init -> Printer.pp_init fmt init
  in
  Format.fprintf !out "  %a [ color=orange, label=\"init %a := %a\" ] ;@."
    pretty u Printer.pp_lval (Cil.var x) pp_init init ;
  link u k ; u

let tag s k =
  let u = node () in
  Format.fprintf !out "  %a [ color=cyan , label=\"Tag %s\" ] ;@." pretty u s ;
  link u k ; u

let loop_entry w = tag "BeforeLoop" w
let loop_step w = tag "InLoop" w

let call_dynamic _env _stmt _pid fct calls =
  let u = node () in
  Format.fprintf !out "  %a [ color=red , label=\"CallPtr %a\" ];@." pretty u
    Printer.pp_exp fct ;
  List.iter (fun (_,k) -> link u k) calls ; u

let call_goal_precond env _stmt kf _es ~pre k =
  let u = node () in
  Format.fprintf !out "  %a [ color=red , label=\"Prove PreCond %a%t\" ] ;@."
    pretty u Kernel_function.pretty kf
    begin fun fmt ->
      if Wp_parameters.debug_atleast 1 then
        List.iter
          (fun (_,p) -> Format.fprintf fmt "\n@[<hov 2>Requires %a ;@]"
              Printer.pp_predicate p) pre
    end ;
  Format.fprintf !out "  %a -> %a [ style=dotted ] ;@." pretty u pretty k ;
  merge env u k

let call_terminates env _stmt ~kind ?kf _es (_gpid, prop) ~callee_t k =
  ignore kind ;
  let u = node () in
  let pp_opt_kf = Pretty_utils.pp_opt ~none:"(?)" Kernel_function.pretty in
  Format.fprintf !out "  %a [ color=red , label=\"Prove Terminates %a%t\" ] ;@."
    pretty u pp_opt_kf kf
    begin fun fmt ->
      if Wp_parameters.debug_atleast 1 then
        Format.fprintf fmt "\n@[<hov 2>Terminates if %a[%s] ==> %a[%a];@]"
          Printer.pp_predicate prop "Caller"
          Printer.pp_predicate callee_t
          pp_opt_kf kf
    end ;
  Format.fprintf !out "  %a -> %a [ style=dotted ] ;@." pretty u pretty k ;
  merge env u k

let call_decreases env _s ?kf _es (_id, (caller_d, rel)) ?caller_t ?callee_d k =
  let u = node () in
  let pp_opt_pred = Pretty_utils.pp_opt ~none:"TRUE" Printer.pp_predicate in
  let pp_opt_kf = Pretty_utils.pp_opt ~none:"(?)" Kernel_function.pretty in
  let pp_rel fmt callee_d =
    match rel with
    | None ->
      Format.fprintf fmt "%a ==> %a[%a] < %a[%s] && %a[%a] >= 0"
        pp_opt_pred caller_t
        Printer.pp_term callee_d pp_opt_kf kf
        Printer.pp_term caller_d "Caller"
        Printer.pp_term callee_d pp_opt_kf kf
    | Some rel ->
      Format.fprintf fmt "%a ==> %a(%a[%s], %a[%a])"
        pp_opt_pred caller_t
        Printer.pp_logic_info rel
        Printer.pp_term caller_d "Caller"
        Printer.pp_term callee_d pp_opt_kf kf
  in
  Format.fprintf !out "  %a [ color=red , label=\"Prove Decreases %a%t\" ] ;@."
    pretty u pp_opt_kf kf
    begin fun fmt ->
      match callee_d with
      | None ->
        Format.fprintf fmt "\n@[<hov 2>Decreases if %a ==> FALSE;@]"
          pp_opt_pred caller_t
      | Some (callee_d, _) ->
        if Wp_parameters.debug_atleast 1 then
          Format.fprintf fmt "\n@[<hov 2>Decreases if %a;@]" pp_rel callee_d
    end ;
  Format.fprintf !out "  %a -> %a [ style=dotted ] ;@." pretty u pretty k ;
  merge env u k

let call env stmt _r kf _es ~pre ~post ~pexit ~assigns ~p_post ~p_exit =
  let u_post = List.fold_right (add_hyp env) post p_post in
  let u_exit = List.fold_right (add_hyp env) pexit p_exit in
  let u = node () in
  link u u_post ; link u u_exit ;
  Format.fprintf !out
    "  %a [ color=orange , label=\"Call %a @[<hov 2>(assigns%a)@]\" ] ;@."
    pretty u Kernel_function.pretty kf pp_assigns assigns ;
  ignore stmt ;
  List.fold_right (add_hyp env) pre u

let pp_scope sc fmt xs =
  let title = match sc with
    | Mcfg.SC_Global -> "Global"
    | Mcfg.SC_Frame_in -> "F-in"
    | Mcfg.SC_Frame_out -> "F-out"
    | Mcfg.SC_Block_in -> "B-in"
    | Mcfg.SC_Block_out -> "B-out"
  in begin
    Format.fprintf fmt "%s {" title ;
    List.iter (fun x -> Format.fprintf fmt " %a" Printer.pp_varinfo x) xs ;
    Format.fprintf fmt " }" ;
  end

let scope _kfenv xs scope k =
  let u = node () in
  Format.fprintf !out "  %a [ color=lightblue , label=\"%a\" ] ;@." pretty u
    (pp_scope scope) xs ;
  link u k ; u

let close kfenv k =
  let u = node () in
  Format.fprintf !out "  %a [ color=cyan , label=\"Function %a\" ] ;@." pretty u
    Kernel_function.pretty kfenv ;
  link u k ; u

let build_prop_of_from _env _ps _k = 0

(* -------------------------------------------------------------------------- *)
OCaml

Innovation. Community. Security.