package frama-c

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

Source file studia_gui.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
(**************************************************************************)
(*                                                                        *)
(*  This file is part of Frama-C.                                         *)
(*                                                                        *)
(*  Copyright (C) 2007-2024                                               *)
(*    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 Printer_tag
open Cil_types
open Cil_datatype

let update_column = ref (fun _ -> ())

let add_tag buffer (name, tag_prop) start stop =
  let tag = Gtk_helper.make_tag buffer ~name tag_prop in
  Gtk_helper.apply_tag buffer tag start stop

let studia_start_tag = ("startstudia", [`UNDERLINE `DOUBLE])
let show_direct_tag = ("show_direct", [`BACKGROUND "#FFca63"])
let show_indirect_tag = ("show_indirect", [`BACKGROUND "#FFdb74"])
let empty_tag = ("", [])

let ask_for_lval (main_ui:Design.main_window_extension_points) kf =
  let txt = Gtk_helper.input_string ~parent:main_ui#main_window
      ~title:"Input lvalue expression" ""
  in
  match txt with None | Some "" -> None
               | Some txt ->
                 try
                   let term_lval = Logic_parse_string.term_lval kf txt in
                   Some (txt, term_lval)
                 with e ->
                   main_ui#error "[ask for lval] '%s' invalid expression: %s@."
                     txt (Printexc.to_string e);
                   None

(** [kf_stmt_opt] is used if we want to ask the lval to the user in a popup *)
let get_lval_opt main_ui kf localizable =
  match localizable with
  | PLval (Some _kf, (Kstmt _stmt), lv) ->
    let lv_txt = Pretty_utils.to_string Printer.pp_lval lv in
    let tlv = Logic_utils.lval_to_term_lval lv in
    Some (lv_txt, tlv)
  | PTermLval (Some _kf, (Kstmt _stmt), _, tlv) ->
    let tlv_txt = Pretty_utils.to_string Printer.pp_term_lval tlv in
    Some (tlv_txt, tlv)
  | _ ->
    match ask_for_lval main_ui kf with
    | None -> None
    | Some (lv_txt, lv) -> Some (lv_txt, lv)

let eval_tlval =
  let typ_lval_to_zone_gui = Datatype.func2 Stmt.ty Term.ty Locations.Zone.ty in
  Dynamic.get ~plugin:"Value" "tlval_to_zone_gui" typ_lval_to_zone_gui


module Kfs_containing_highlighted_stmt =
  Kernel_function.Make_Table
    (Datatype.String.Set)
    (struct
      let name = "Studia.Kf_containing_highlighted_stmt"
      let size = 7
      let dependencies =
        [ (*Dependencies are managed manually by Make_StmtSetState*) ]
    end)

let default_icon_name = "gtk-apply"


module Make_StmtMapState (Info:sig val name: string end) =
struct
  module D = Datatype
  include State_builder.Ref
      (Stmt.Map.Make(Datatype.String.Set))
      (struct
        let name = Info.name
        let dependencies = [ Eva.Analysis.self ]
        let default () = Stmt.Map.empty
      end)

  let set s =
    set s;
    Kfs_containing_highlighted_stmt.clear ();
    Stmt.Map.iter
      (fun stmt s ->
         let kf = Kernel_function.find_englobing_kf stmt in
         let prev =
           try Kfs_containing_highlighted_stmt.find kf
           with Not_found -> D.String.Set.empty
         in
         let union = D.String.Set.union prev s in
         Kfs_containing_highlighted_stmt.replace kf union)
      s;
    !update_column `Contents

end

(*
module type StudiaCmdSig = sig
  type input
  val help : string
  val compute : Kernel_function.t -> Cil_types.stmt -> input -> string
  (** Returns a text to be displayed in the GUI  *)
  val tag_stmt : Cil_types.stmt -> (string * GText.tag_property list)
  val clear: unit -> unit
end
 *)

module WritesOrReads =
struct

  (* type input = term_lval *)

  module State =
    Make_StmtMapState
      (struct let name = "Studia.Highlighter.WritesOrRead" end)

  let clear () = State.clear()

  let help_writes = ("[writes] "
                     ^"highlight the statements that writes to the location pointed to \
                       by D at L")
  let help_reads = ("[reads] "
                    ^"highlight the statements that reads the location pointed to \
                      by D at L")

  let indirect_icon_name = "gtk-jump-to"

  let add_icon map stmt icon =
    let update_icons pre =
      Some(Datatype.String.Set.(add icon (Option.value ~default:empty pre)))
    in
    Stmt.Map.update stmt update_icons map

  let add_read map = function
    | Reads.Direct stmt -> add_icon map stmt default_icon_name
    | Indirect stmt -> add_icon map stmt indirect_icon_name

  let add_write map = function
    | Writes.Assign stmt
    | CallDirect stmt -> add_icon map stmt default_icon_name
    | CallIndirect stmt -> add_icon map stmt indirect_icon_name
    | GlobalInit _ -> map
    | FormalInit (_, callsites) ->
      let calls = List.flatten (List.map snd callsites) in
      List.fold_left
        (fun map stmt -> add_icon map stmt default_icon_name)
        map
        calls

  let compute op _kf stmt tlv =
    let t = Logic_const.term (TLval tlv) (Cil.typeOfTermLval tlv) in
    let z = eval_tlval stmt t in
    let r, s = match op with
      | `Reads ->
        List.fold_left add_read Stmt.Map.empty (Reads.compute z), "Reads"
      | `Writes ->
        List.fold_left add_write Stmt.Map.empty (Writes.compute z), "Writes"
    in
    State.set r;
    Options.feedback "%s computed" s;
    if Stmt.Map.is_empty r
    then s ^ " computed; no statement found."
    else s ^ " computed"

  let tag_stmt stmt =
    try
      let s = Stmt.Map.find stmt (State.get()) in
      if Datatype.String.Set.mem default_icon_name s
      then show_direct_tag else show_indirect_tag
    with Not_found -> empty_tag

end

let help (main_ui:Design.main_window_extension_points) =
  main_ui#pretty_information "%s@." WritesOrReads.help_writes;
  main_ui#pretty_information "%s@." WritesOrReads.help_reads;
  main_ui#pretty_information "%s@."
    "Reset : reset the internal state for all the previous commands."

module StudiaState =
  State_builder.Option_ref
    (Stmt)
    (struct
      let name = "Studia.Highlighter.StudiaState"
      let dependencies = [ Eva.Analysis.self ]
    end)

let reset () =
  StudiaState.clear ();
  WritesOrReads.clear ();
  Kfs_containing_highlighted_stmt.clear ();
  !update_column `Contents

let callback op (main_ui:Design.main_window_extension_points) (kf, stmt, localizable) =
  let compute f arg =
    let msg = f kf stmt arg in
    if msg <> "" then main_ui#pretty_information "%s@." msg
  in
  begin
    match get_lval_opt main_ui kf localizable with
    | None -> reset ()
    | Some (lval_txt, lval) ->
      begin
        let txt = Format.asprintf "[studia] query %s" lval_txt in
        StudiaState.set stmt;
        main_ui#pretty_information "%s@." txt;
        compute (WritesOrReads.compute op) lval
      end;
  end;
  main_ui#rehighlight ()

let highlighter (buffer:Design.reactive_buffer) localizable ~start ~stop =
  try
    let start_s = StudiaState.get () in
    let put_tag tag = match tag with
      | ("",[]) -> ()
      | _ -> add_tag buffer#buffer tag start stop
    in
    match localizable with
    | PStmt (_,stmt) ->
      if start_s.sid = stmt.sid then put_tag studia_start_tag;
      put_tag (WritesOrReads.tag_stmt stmt);
    | _ -> ()
  with Not_found -> ()

let check_value (main_ui:Design.main_window_extension_points) =
  Eva.Analysis.is_computed () ||
  let answer = GToolbox.question_box
      ~title:("Eva Needed")
      ~buttons:[ "Run"; "Cancel" ]
      ("Eva has to be run first.\nThis can take some time.\n"
       ^"Do you want to run Eva now ?")
  in
  answer = 1 &&
  match main_ui#full_protect ~cancelable:true Eva.Analysis.compute with
  | Some _ -> true
  | None -> false


(** To add a sensitive/unsensitive menu item to a [factory].
    The menu item is insensitive when [arg_opt = None],
    else, when the item is selected, the callback is called with the argument.
    If [uses_value] is true, check if the value analysis has been computed.
*)
let add_item (main_ui:Design.main_window_extension_points) ~uses_value menu name arg_opt callback =
  (* add the menu item *)
  let item = GMenu.menu_item ~label:name () in
  menu#add item;
  match arg_opt with
  | None -> (* item must not be sensitive *)
    item#misc#set_sensitive false
  | Some arg -> (* add callback to the menu item *)
    let callback () =
      if not uses_value || check_value main_ui then callback arg else ()
    in
    (* The following code circumvents a bug where submenu items are not properly
       activated, by also binding the callback to the button_press event.
       See http://stackoverflow.com/questions/5221326/submenu-item-does-not-call-function-with-working-solution/15309826
       For mysterious reasons, this bug only happens in the contextual menu of
       the Information panel, but not in the source panel. So we need to avoid
       creating two input windows for the source panel menu. *)
    let only_once = ref true in
    let callback () =
      if !only_once
      then begin only_once := false; callback (); only_once := true end
    in
    (* Needed anyway for keyboard selection. *)
    ignore (item#connect#activate ~callback);
    (* Needed for the menu in the Information panel. *)
    ignore (item#event#connect#button_press
              ~callback:(fun evt ->
                  if GdkEvent.Button.button evt = 1
                  then (callback (); true)
                  else false))

let selector (popup_factory:GMenu.menu GMenu.factory)
    (main_ui:Design.main_window_extension_points)
    ~button localizable =
  if button = 3 then begin
    let submenu = popup_factory#add_submenu "Studia" in
    let submenu_factory = new GMenu.factory submenu in
    let arg = match (Pretty_source.kf_of_localizable localizable,
                     Pretty_source.ki_of_localizable localizable)
      with
      | Some kf, Kstmt stmt -> Some (kf, stmt, localizable)
      | Some _, Kglobal | None, _ -> None
    in
    let add_menu_item name callback =
      add_item main_ui ~uses_value:true submenu name arg
        (fun arg ->
           main_ui#protect ~cancelable:true (fun () -> callback main_ui arg))
    in
    add_menu_item "Writes" (callback `Writes);
    add_menu_item "Reads" (callback `Reads);
    ignore (submenu_factory#add_separator ());
    add_item main_ui ~uses_value:false submenu "Reset All" (Some())
      (fun _ -> reset () ; main_ui#rehighlight ());
    ignore (submenu_factory#add_separator ());
    add_item main_ui ~uses_value:false submenu
      "Help" (Some()) (fun _ -> help main_ui) ;
  end

let filetree_decorate main_ui =
  main_ui#file_tree#append_pixbuf_column
    ~title:"Studia"
    (fun globs ->
       let icons = function
         | GFun ({svar = v }, _) ->
           (try Kfs_containing_highlighted_stmt.find  (Globals.Functions.get v)
            with Not_found -> Datatype.String.Set.empty)
         |  _ -> Datatype.String.Set.empty
       in
       let ids =
         if Kfs_containing_highlighted_stmt.length () <> 0 then
           let icons = List.fold_left
               (fun acc glob -> Datatype.String.Set.union (icons glob) acc)
               Datatype.String.Set.empty globs
           in
           if Datatype.String.Set.is_empty icons
           then Datatype.String.Set.singleton ""
           else icons
         else
           Datatype.String.Set.singleton ""
       in
       let icons =
         if Datatype.String.Set.mem default_icon_name ids then
           [default_icon_name]
         else
           Datatype.String.Set.elements
             (Datatype.String.Set.remove default_icon_name ids)
       in
       List.map (fun icon -> `STOCK_ID icon) icons
    )
    (fun _ -> Kfs_containing_highlighted_stmt.length () <> 0)

let main main_ui =
  main_ui#register_source_selector selector;
  main_ui#register_source_highlighter highlighter;
  update_column := (filetree_decorate main_ui)

let () = Design.register_extension main
OCaml

Innovation. Community. Security.