package frama-c-luncov

  1. Overview
  2. Docs

Source file data_hyperlabels.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
(**************************************************************************)
(*                                                                        *)
(*  This file is part of the Frama-C's Luncov plug-in.                    *)
(*                                                                        *)
(*  Copyright (C) 2012-2022                                               *)
(*    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 LICENSE)                       *)
(*                                                                        *)
(**************************************************************************)

open Hhelpers

exception UnknownException

type id = int

type t = (id, node) Hashtbl.t

let create ?(size=100) () = Hashtbl.create size

(* One label at least is uncoverable *)
let exists_uncoverable_label env labs =
  List.exists (fun l -> Data_labels.is_uncoverable env l) labs

(* In this version all sequences are implemented using labels *)
let exists_uncoverable_sequence env seqs =
  exists_uncoverable_label env seqs

let exists_uncoverable_bindings env labs =
  List.exists (fun l -> List.exists (fun bl -> l = bl) env) labs

(* INCOMPLET : ne traite pas les bindings *)
(* Unknown iff there is 0 label uncoverable in the conjunction *)
let compute_conjunction_uncoverable (lbls,binds) conj =
  if not (exists_uncoverable_label lbls conj.labels)
  && not (exists_uncoverable_sequence lbls conj.sequences)
  && not (exists_uncoverable_bindings binds conj.labels) then
    raise UnknownException

(* a dnf is uncoverable if all its clauses are uncoverables, unknown otherwise *)
let compute_uncoverable_dnf env dnf =
  try
    List.iter (compute_conjunction_uncoverable env) dnf;
    Uncoverable ""
  with
    UnknownException -> Unknown ""

(* Depending on status (old is not unknown iff force option is used),
   decide if we keep the old or new one *)
let merge_status old_status new_status =
  match old_status,new_status with
  | Covered _, Uncoverable _->
    Options.warning "discrepency detected (covered detected as uncoverable)";
    new_status
  | Covered _, Unknown _ ->
    old_status
  | Uncoverable _, Unknown _->
    Options.warning "loss of precision detected";
    new_status
  | _, _ ->
    new_status

(* For each hyperlabel, get its new status *)
let compute_uncoverable ?(force=false) env hlab =
  let old_status = hlab.status in
  if force || Hhelpers.is_unknown hlab then begin
    let new_status = compute_uncoverable_dnf env hlab.ls in
    hlab.status <- merge_status old_status new_status
  end
  else
    Options.feedback "skip hyperlabel #%d" hlab.id

(* Compute coverage (i.e. if uncoverable or not) for all hyperlabels  *)
let compute_coverage ?(force=false) env hdata =
  Hashtbl.iter (fun _ h -> compute_uncoverable ~force env h) hdata

(* Using parser.mly & lexer.mll, load hyperlabels data from .hyperlabels file*)
let load hdata hfile =
  try
    let input = open_in hfile in
    let lexbuf = Lexing.from_channel (open_in hfile) in
    let result = Parser.hyplist_root Lexer.read lexbuf in
    List.iter (fun h -> Hashtbl.add hdata h.id h) result;
    close_in input
  with
    Hhelpers.SyntaxError m -> Format.eprintf "[LUncov] %s@." m

(* Store hdata into hyperlabels file *)
let store hdata hfile =
  if Options.Backup.get () then Commons.backup hfile;
  Format.printf "[LUncov] updating hyperlabel file with coverage data @.";
  let output = open_out hfile in
  let f id node acc =
    let hl = Hhelpers.string_of_node node in
    (id,hl^"\n") :: acc
  in
  let l = Hashtbl.fold f hdata [] in
  let l = List.sort compare l in
  List.iter (fun (_,line)-> output_string output line) l;
  close_out output

type stats = {
  total: int;
  unknown : int;
  covered : int;
  uncoverable: int;
}

let get_stats hdata =
  let total = ref 0 in
  let unknown = ref 0 in
  let covered = ref 0 in
  let uncoverable = ref 0 in
  let incr_counter = function
    | Unknown _ -> incr unknown
    | Covered _ -> incr covered
    | Uncoverable _ -> incr uncoverable
  in
  Hashtbl.iter (fun _ hli -> incr_counter hli.status; incr total) hdata;
  let total = !total in
  let unknown = !unknown in
  let covered = !covered in
  let uncoverable = !uncoverable in
  { total; unknown; covered; uncoverable }

let coverage_ratio stats =
  let maybe_coverable = stats.total - stats.uncoverable in
  if maybe_coverable > 0 then
    Some (float_of_int stats.covered /. float_of_int maybe_coverable)
  else
    None

let pp_stats f stats =
  Format.fprintf f "total number of hyperlabels %8d@." stats.total;
  Format.fprintf f "number of covered           %8d@." stats.covered;
  Format.fprintf f "number of uncoverable       %8d@." stats.uncoverable;
  Format.fprintf f "number of unknown           %8d@." stats.unknown;
  let pcov rat =
    Format.fprintf f "coverage ratio              %8.2f%% \
                      (covered over maybe coverable)@."
      (100.0 *. rat)
  in
  Option.iter pcov (coverage_ratio stats)

let pp_diff_stats f (before,after) =
  let pp_diff f n = if n <> 0 then Format.fprintf f " (%+d)" n in
  Format.fprintf f "total number of hyperlabels %8d%a@." after.total
    pp_diff (after.total-before.total);
  Format.fprintf f "number of covered           %8d%a@." after.covered
    pp_diff (after.covered-before.covered);
  Format.fprintf f "number of uncoverable       %8d%a@." after.uncoverable
    pp_diff (after.uncoverable-before.uncoverable);
  Format.fprintf f "number of unknown           %8d%a@." after.unknown
    pp_diff (after.unknown-before.unknown);
  match coverage_ratio before, coverage_ratio after with
  | Some before_rat, Some after_rat ->
    Format.fprintf f "coverage ratio              %8.2f%% \
                      (was %.2f%%)@."
      (100.0 *. after_rat) (100.0 *. before_rat)
  | None, Some after_rat ->
    Format.fprintf f "coverage ratio              %8.2f%% \
                      (covered over maybe coverable)@."
      (100.0 *. after_rat)
  | _ -> ()
OCaml

Innovation. Community. Security.