package mopsa

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

Source file progress.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
(****************************************************************************)
(*                                                                          *)
(* This file is part of MOPSA, a Modular Open Platform for Static Analysis. *)
(*                                                                          *)
(* Copyright (C) 2017-2019 The MOPSA Project.                               *)
(*                                                                          *)
(* This program is free software: 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, either version 3 of the License, or     *)
(* (at your option) any later version.                                      *)
(*                                                                          *)
(* This program 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.                      *)
(*                                                                          *)
(* You should have received a copy of the GNU Lesser General Public License *)
(* along with this program.  If not, see <http://www.gnu.org/licenses/>.    *)
(*                                                                          *)
(****************************************************************************)

(** Hook for displaying progress of the analysis *)

open Mopsa
open Format
open Ast


module Hook =
struct

  (** {2 Hook header} *)
  (** *************** *)

  let name = "progress"



  (** {2 Entries of the progress table} *)
  (** ********************************* *)

  (** Set of location ranges *)
  module RangeSet = SetExt.Make(struct type t = range let compare = compare_range end)

  (** Entry of the progression table *)
  type entry = {
    name: string;                   (** Name of the function *)
    mutable range: range option;    (** Location of the currently analyzed statement *)
    all_stmt_range_set: RangeSet.t; (** Location of all statements in the function body *)
    mutable analyzed_stmt_range_set: RangeSet.t; (** Locations of analyzed statements *)
    nb_all_stmt: int;               (** Total number of statements *)
    mutable nb_analyzed_stmt: int;  (** Number of analyzed statements *)
  }

  (** Print an entry of the progress table *)
  let pp_entry fmt e =
    match e.range with
    | None -> fprintf fmt "%s %d%%" e.name (100*e.nb_analyzed_stmt/e.nb_all_stmt)
    | Some r ->
      let pos = get_range_start r in
      fprintf fmt "%s:%d %d%%" e.name pos.pos_line (100*e.nb_analyzed_stmt/e.nb_all_stmt)


  (** {2 Utilities for moving the terminam cursor} *)
  (** ******************************************** *)

  (** Clear the current line and move the cursor at its beginning *)
  let clear_line () =
    printf "\027[2K\r@?"

  (** Move the cursor one line above *)
  let up_line () =
    printf "\027[1A@?"


  (** {2 Progression table} *)
  (** ********************* *)

  (** Progression table as a stack of entries *)
  let table : entry Stack.t = Stack.create ()


  (** Return the set of statements in the body of a function *)
  let get_function_statements f =
    Visitor.fold_stmt
      (fun acc e -> VisitParts acc)
      (fun acc s ->
         match skind s with
         | S_block _ -> VisitParts acc
         | _ -> VisitParts (RangeSet.add s.srange acc)
      ) RangeSet.empty f.fun_body


  (** Insert a new entry in the progress table *)
  let before_call f =
    let all_stmt_range_set = get_function_statements f in
    let entry = {
      name = f.fun_orig_name;
      range = None;
      all_stmt_range_set;
      analyzed_stmt_range_set = RangeSet.empty;
      nb_all_stmt = RangeSet.cardinal all_stmt_range_set;
      nb_analyzed_stmt = 0;
    }
    in
    Stack.push entry table;
    printf "@.%a@?" pp_entry entry


  (** Remove the head function from the progress table *)
  let after_call () =
    let _ = Stack.pop table in
    (* Clear the current line (displaying the progress of the removed entry) 
       and move the cursor to the line before.
    *)
    clear_line ();
    if Stack.is_empty table then ()
    else (
      up_line ();
      clear_line ();
      printf "%a@?" pp_entry (Stack.top table)
    )


  (** Update the progress table before a statement is analyzed *)
  let before_stmt range =
    if Stack.is_empty table
    then ()
    else (
      let entry = Stack.top table in
      if not @@ RangeSet.mem range entry.all_stmt_range_set then ()
      else (
        entry.range <- Some range;
        clear_line ();
        printf "%a@?" pp_entry entry
      )
    )

  (** Update the progress table after a statement is analyzed *)
  let after_stmt range =
    if Stack.is_empty table
    then ()
    else (
      let entry = Stack.top table in
      if not @@ RangeSet.mem range entry.all_stmt_range_set then ()
      else if RangeSet.mem range entry.analyzed_stmt_range_set then ()
      else (
        entry.analyzed_stmt_range_set <- RangeSet.add range entry.analyzed_stmt_range_set;
        entry.nb_analyzed_stmt <- entry.nb_analyzed_stmt + 1;
        clear_line ();
        printf "%a@?" pp_entry entry
      )
    )  

  

  (** {2 Initialization} *)
  (** ****************** *)


  let init ctx = ()



  (** {2 Events handlers} *)
  (** ******************* *)

  let on_before_exec route stmt man flow =
    before_stmt stmt.srange


  let on_after_exec route stmt man flow post =
    after_stmt stmt.srange


  let on_before_eval route semantic exp man flow =
    match ekind exp with
    | E_call ({ ekind = E_function (User_defined f) }, args) -> before_call f
    | _ -> ()


  let on_after_eval route semantic exp man flow evl =
    match ekind exp with
    | E_call ({ ekind = E_function (User_defined f) }, args) -> after_call ()
    | _ -> ()

  let on_finish man flow = ()

end

let () =
  Core.Hook.register_stateless_hook (module Hook)
OCaml

Innovation. Community. Security.