package frama-c

  1. Overview
  2. Docs

doc/src/mthread/mt_cfg_types.ml.html

Source file mt_cfg_types.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
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
(**************************************************************************)
(*                                                                        *)
(*  This file is part of Frama-C.                                         *)
(*                                                                        *)
(*  Copyright (C) 2007-2025                                               *)
(*    CEA (Commissariat à l'énergie atomique et aux énergies              *)
(*         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).            *)
(*                                                                        *)
(**************************************************************************)

open Cil_types
open Mt_types
open Mt_memory.Types
open Mt_shared_vars_types

type thread = Thread.t

(* -------------------------------------------------------------------------- *)
(* --- Live threads/taken mutexes at a given point of execution           --- *)
(* -------------------------------------------------------------------------- *)

type context = {
  started_threads : ThreadPresence.t;
  locked_mutexes : MutexPresence.t;
}

module Context = struct

  type t = context

  let pretty fmt c =
    if not (ThreadPresence.is_empty c.started_threads) then
      Format.fprintf fmt "@[<h>Threads: %a@]"
        ThreadPresence.pretty c.started_threads;
    if not (MutexPresence.is_empty c.locked_mutexes) then
      Format.fprintf fmt "@[<h>Mutexes: %a@]"MutexPresence.pretty c.locked_mutexes;
  ;;

  let empty = {
    started_threads = ThreadPresence.empty;
    locked_mutexes = MutexPresence.empty;
  }

end


(* -------------------------------------------------------------------------- *)
(* --- Accesses to shared memory                                              *)
(* -------------------------------------------------------------------------- *)

type var_access_kind =
  | NotReallySharedVar
  | SharedVarNonConcurrentAccess
  | ConcurrentAccess


type cfg_concur = {
  concur_accesses: SetZoneAccess.t;
  var_access_kind: var_access_kind;
}

module CfgConcur = struct
  type t = cfg_concur


  let combine_access_kind a1 a2 = match a1, a2 with
    | ConcurrentAccess, _ | _, ConcurrentAccess -> ConcurrentAccess
    | SharedVarNonConcurrentAccess, _ | _, SharedVarNonConcurrentAccess ->
      SharedVarNonConcurrentAccess
    | NotReallySharedVar, NotReallySharedVar -> NotReallySharedVar

  let default = {
    concur_accesses = SetZoneAccess.empty;
    var_access_kind = NotReallySharedVar;
  }

  let has_concur_accesses c = not (SetZoneAccess.is_empty c.concur_accesses)

  let must_be_in_cfg ~keep c =
    match keep with
    | NotReallySharedVar -> has_concur_accesses c
    | SharedVarNonConcurrentAccess -> c.var_access_kind <> NotReallySharedVar
    | ConcurrentAccess -> c.var_access_kind = ConcurrentAccess

  let combine c1 c2 = {
    concur_accesses = SetZoneAccess.union c1.concur_accesses c2.concur_accesses;
    var_access_kind = combine_access_kind c1.var_access_kind c2.var_access_kind;
  }

  let add_access (rw, z) c = {
    c with concur_accesses = SetZoneAccess.add (rw, z) c.concur_accesses
  }

end


(* -------------------------------------------------------------------------- *)
(* --- State of a cfg node                                                    *)
(* -------------------------------------------------------------------------- *)

type node_value_state = {
  state_before: state;
  state_after: state;
}

module NodeValueState = struct
  type t = node_value_state

  let dummy = {
    state_before = Cvalue.Model.bottom;
    state_after  = Cvalue.Model.bottom;
  }

  let aux_presence default raw_id f state : _ Mt_lib.conversion_with_warning =
    match Mt_ids.read_id_state_enumerate 4 state raw_id with
    | `Failure mess | `WithWarning (mess, _) ->
      `WithWarning(mess, default)
    | `Success l ->
      match f (List.sort compare l) with
      | `Warn ->
        `WithWarning (
          (fun fmt -> Format.fprintf fmt
              "@[Id %a@ contains@ strange@ state@ {%a}@]"
              Mt_ids.pretty_raw_id raw_id
              (Pretty_utils.pp_list ~sep:"@ " ~pre:"" ~suf:""
                 Format.pp_print_int) l;
          ),
          default)
      | `Ok v -> `Success v

  let mutex_presence m =
    aux_presence NotPresent (Mt_ids.of_mutex m)
      (function
        | [0] |[1] | [0;1] -> `Ok NotPresent
        | [2] -> `Ok Present
        | [0;2] | [1;2] | [0;1;2] -> `Ok MaybePresent
        | _ -> `Warn)

  let threads_presence started th =
    aux_presence MaybePresent (Mt_ids.of_thread th)
      (fun l -> match l, started with
         | [0], (`Prior | `Started) -> `Ok Present
         | [0], `MaybeStarted -> `Ok MaybePresent
         | [0], `NotStarted -> `Ok NotPresent
         | [1], _ -> `Ok Present
         | [2], _ -> `Ok NotPresent
         | [0;2], `NotStarted -> `Ok NotPresent
         | ([0;1] | [0;2] | [1;2] | [0;1;2]), _ -> `Ok MaybePresent
         | _ -> `Warn)

end


(* -------------------------------------------------------------------------- *)
(* --- Concurrent control-flow grapg - Type declarations                      *)
(* -------------------------------------------------------------------------- *)

type node = {
  cfgn_id : int;
  mutable cfgn_stack: Callstack.t;
  mutable cfgn_var_access: cfg_concur;
  mutable cfgn_kind : node_kind;
  mutable cfgn_preds: node list;
  mutable cfgn_value_state: node_value_state;
  mutable cfgn_context: context;
}
and node_kind =
  | NMT of stmt * Mt_types.events_set * node
  | NInstr of stmt * node
  | NCall of stmt * (Kernel_function.t list * node list)
  | NWholeCall of
      Kernel_function.t * stmt list * Mt_types.events_set * node
  | NWhile of stmt * node
  | NIf of stmt * node * node
  | NSwitch of stmt * exp * node list
  | NJump of jump_type * node
  | NStart of Kernel_function.t * node
  | NEOP
  | NDead

and jump_type =
  | JBreak of stmt
  | JContinue of stmt
  | JGoto of stmt
  | JReturn of stmt
  | JExit of stmt
  | JBlock of stmt

and cfg = node

module CfgNode = struct

  let make_aux id stack kind = {
    cfgn_id = id;
    cfgn_stack = stack;
    cfgn_var_access = CfgConcur.default;
    cfgn_kind = kind;
    cfgn_preds = [];
    cfgn_value_state = NodeValueState.dummy;
    cfgn_context = Context.empty;
  }

  let dead =
    let stack = Callstack.init (Kernel_function.dummy ()) in
    make_aux (-1) stack NDead

  include Datatype.Make_with_collections(
    struct
      include Datatype.Undefined
      type t = node

      (* XXX incorrect descr: cfgn_value_state contains value datatypes. *)
      let structural_descr = Structural_descr.t_abstract

      let reprs = [dead]
      let name = "Mt_cfg_types.node"

      let rehash x = x

      let compare t1 t2 = Stdlib.compare t1.cfgn_id t2.cfgn_id
      let equal t1 t2 = t1.cfgn_id = t2.cfgn_id
      let hash t = Hashtbl.hash t.cfgn_id

      let pretty fmt node =
        Format.fprintf fmt "node %d" node.cfgn_id

    end)

  let new_node =
    let x = ref 0 in
    fun stack -> make_aux (incr x; !x) stack NEOP
  ;;


  let node_kind_stmt = function
    | NInstr (s, _) | NIf (s, _,_) | NMT (s, _,_) | NCall (s, _)
    | NWhile (s, _) | NSwitch (s, _, _)
    | NJump ((JBreak s | JContinue s | JGoto s |
              JReturn s | JExit s | JBlock s), _) -> [s]
    | NWholeCall (_, l, _, _) -> l
    | NDead | NEOP | NStart _ -> []
  ;;

  let node_stmt n = node_kind_stmt n.cfgn_kind
  let node_first_loc n = match node_stmt n with
    | [] -> None
    | s :: _ -> Some (fst (Cil_datatype.Stmt.loc s))

  let pretty_stmts fmt node =
    match node_stmt node with
    | [] -> Format.pp_print_string fmt "<no stmt>"
    | stmts ->
      let pp_loc fmt s = Printer.pp_location fmt (Cil_datatype.Stmt.loc s) in
      Pretty_utils.pp_list ~sep:",@ " pp_loc fmt stmts

  let pretty_with_stmts fmt node =
    Format.fprintf fmt "%a@ (%a)" pretty node pretty_stmts node

  let node_kind_succs = function
    | NEOP | NDead -> []
    | NMT (_, _, a) | NInstr (_, a) | NWhile (_, a) | NJump (_, a)
    | NWholeCall (_, _, _, a) | NStart (_, a) -> [a]
    | NIf (_, a1, a2) -> [a1; a2]
    | NCall (_, (_, l)) | NSwitch (_, _, l) -> l
  ;;

  let node_succs n = node_kind_succs n.cfgn_kind

  let pretty_jump_type a fmt j =
    let p x = Format.fprintf fmt x in
    match j with
    | JBreak _ -> p "break"
    | JContinue _ -> p "continue"
    | JGoto _ -> p "goto"
    | JReturn _ -> p "return"
    | JExit _ -> p "exit"
    | JBlock _ -> p "Jump %d" a.cfgn_id
  ;;

  let pretty_kind fmt = function
    | NInstr _ -> Format.pp_print_string fmt "Instr"
    | NIf _ ->  Format.pp_print_string fmt "If"
    | NCall _ -> Format.pp_print_string fmt "Call"
    | NWhile _ -> Format.pp_print_string fmt "Loop"
    | NSwitch _ -> Format.pp_print_string fmt "Switch"
    | NJump (j, a) -> pretty_jump_type a fmt j
    | NEOP -> Format.pp_print_string fmt "EOP"
    | NDead -> Format.pp_print_string fmt "Dead"
    | NMT _ -> Format.pp_print_string fmt "MT Events"
    | NWholeCall _ -> Format.pp_print_string fmt "WholeCall"
    | NStart _ -> Format.pp_print_string fmt "Start"
  ;;

  let pretty_kind_debug fmt nk =
    (* Format.fprintf fmt "[s %a]" (node_kind_stmt nk); *)
    pretty_kind fmt nk;
    match node_kind_succs nk with
    | [] -> ()
    | _ :: _ as l ->
      Format.fprintf fmt " -> ";
      Pretty_utils.pp_list ~sep:"@ "
        (fun fmt a -> Format.fprintf fmt "n%d" a.cfgn_id) fmt l
  ;;

  let pretty_kinds_node_list =
    Pretty_utils.pp_list ~pre:"@[<v>" ~sep:"@ "
      (fun fmt node -> Format.fprintf fmt "@[<hov 2>%a@]"
          pretty_kind_debug node.cfgn_kind)


  let has_concur_accesses n =
    CfgConcur.has_concur_accesses n.cfgn_var_access
  let must_be_in_cfg ~keep n =
    CfgConcur.must_be_in_cfg ~keep n.cfgn_var_access


  let iter_aux
      ~keep_prevs
      ?(f_before=(fun ~prevs:_ _ -> ()))
      ?(f_after=(fun ~prevs:_ _ -> ()))
      a =
    let visited = Hashtbl.create 17 in

    let rec visit a prevs =
      try Hashtbl.find visited a
      with Not_found ->
        Hashtbl.add visited a ();

        let aux a' =
          let prevs = if keep_prevs then a :: prevs else [] in
          visit a' prevs
        in

        (f_before ~prevs a : unit);
        (match a.cfgn_kind with
         | NIf (_, a1, a2) -> aux a1; aux a2

         | NInstr (_, a) | NMT (_, _, a) | NJump (_, a) | NWhile (_, a)
         | NWholeCall (_, _, _, a) | NStart (_, a) -> aux a

         | NCall (_, (_, l)) | NSwitch (_, _, l) -> List.iter aux l

         | NEOP | NDead -> ()
        );
        (f_after ~prevs a : unit)
    in
    visit a []


  let iter ?f_before ?f_after =
    iter_aux ~keep_prevs:false
      ?f_before:(match f_before with
          | None -> None
          | Some f -> Some (fun ~prevs:_ -> f))
      ?f_after:(match f_after with
          | None -> None
          | Some f -> Some (fun ~prevs:_ -> f))


  let iter_with_prevs = iter_aux ~keep_prevs:true

end


module NodeIdAccess = struct

  include Datatype.Triple_with_collections (RW) (CfgNode) (Thread)

  let pretty_aux f fmt ((op, node, th) as v : t) =
    Format.fprintf fmt "@[<hov 3>%a@ by %a@ at %a%a@]"
      RW.pretty op Thread.pretty th CfgNode.pretty_stmts node f v

  let pretty = pretty_aux (fun _fmt _v -> ())

end

module SetNodeIdAccess = struct
  include Abstract_interp.Make_Lattice_Set (NodeIdAccess) (NodeIdAccess.Set)

  let pretty_aux f =
    Pretty_utils.pp_iter ~pre:"@[<v 2>  " ~sep:"@ " iter
      (fun fmt v -> Format.fprintf fmt "@[%a@]" (NodeIdAccess.pretty_aux f) v)

  let pretty = pretty_aux (fun _fmt _v -> ())
end

module AccessesByZoneNode = struct
  include Lmap_bitwise.Make_bitwise(
    struct
      include SetNodeIdAccess
      let default = bottom
      let default_is_bottom = true
    end)

  let pretty_map fmt m =
    Format.fprintf fmt "@[<v 0>";
    fold_fuse_same
      (fun z s () ->
         if not (SetNodeIdAccess.(equal empty s)) then
           Format.fprintf fmt "@[<hov 2>[%a]@ %a@]@ "
             Locations.Zone.pretty z (SetNodeIdAccess.pretty) s
      ) m ();
    Format.fprintf fmt "@]";
  ;;

  let pretty fmt = function
    | Top -> Format.pp_print_string fmt "TOP ACCESSES NODE"
    | Bottom -> Format.pp_print_string fmt "BOTTOM ACCESSES NODE"
    | Map m -> pretty_map fmt m

end
OCaml

Innovation. Community. Security.