Source file dpds_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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
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 scope_start_tag = ("startscope", [`UNDERLINE `DOUBLE])
let zones_used_tag = ("zones", [`BACKGROUND "#FFeeCC"])
let show_def_direct_tag = ("show_def", [`BACKGROUND "#FFca63"])
let show_def_indirect_tag = ("show_def_indirect", [`BACKGROUND "#FFdb74"])
let scope_b_tag = ("b_scope", [`BACKGROUND "#CCFFff"])
let scope_fb_tag = ("fb_scope", [`BACKGROUND "#CCFFee"])
let scope_f_tag = ("f_scope", [`BACKGROUND "#CCFFbb"])
let scope_p_tag = ("p_scope", [`BACKGROUND "#FFFFab"])
let scope_p_warn_tag = ("p_warn_scope", [`BACKGROUND "#D5FFAb"])
let empty_tag = ("", [])
let add_msg (main_ui:Design.main_window_extension_points) txt =
main_ui#pretty_information "%s@." txt
let pretty_zone fmt z =
Format.fprintf fmt "@[<h 1>%a@]" Locations.Zone.pretty z
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
let lval =
Logic_to_c.term_lval_to_lval term_lval
in
Some (txt, lval)
with e ->
main_ui#error "[ask for lval] '%s' invalid expression: %s@."
txt (Printexc.to_string e);
None
let get_annot_opt localizable = match localizable with
| PIP(Property.(IPCodeAnnot {ica_ca})) -> Some ica_ca
| _ -> None
(** [kf_opt] is used if we want to ask the lval to the user in a popup *)
let get_lval_opt main_ui kf_opt localizable =
match localizable with
| PLval (Some _kf, (Kstmt _stmt), lv) ->
let lv_txt = Format.asprintf "%a" Printer.pp_lval lv in
Some (lv_txt, lv)
| PTermLval (Some _kf, Kstmt _stmt, _, tlv) -> begin
try
let lv = Logic_to_c.term_lval_to_lval tlv in
let lv_txt = Format.asprintf "%a" Printer.pp_term_lval tlv in
Some (lv_txt, lv)
with Invalid_argument _ -> None
end
| _ ->
( match kf_opt with
None -> None
| Some kf ->
match (ask_for_lval main_ui kf) with
None -> None
| Some (lv_txt, lv) -> Some (lv_txt, lv))
let eval_lval =
let typ_lval_to_zone_gui = Datatype.func2 Stmt.ty Lval.ty Locations.Zone.ty in
Dynamic.get ~plugin:"Value" "lval_to_zone_gui" typ_lval_to_zone_gui
module Kf_containing_highlighted_stmt =
Kernel_function.Make_Table
(Datatype.String.Set)
(struct
let name = "Dpds_gui.Kf_containing_highlighted_stmt"
let size = 7
let dependencies =
[ ]
end)
let default_icon_name = "gtk-apply"
let default_icon = Datatype.String.Set.singleton default_icon_name
module Make_StmtSetState (Info:sig val name: string end) =
struct include State_builder.Ref
(Stmt.Hptset)
(struct
let name = Info.name
let dependencies = [ Eva.Analysis.self ]
let default () = Stmt.Hptset.empty
end)
let set s =
set s;
Kf_containing_highlighted_stmt.clear ();
Stmt.Hptset.iter
(fun stmt ->
Kf_containing_highlighted_stmt.replace
(Kernel_function.find_englobing_kf stmt) default_icon)
s;
!update_column `Contents
end
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;
Kf_containing_highlighted_stmt.clear ();
Stmt.Map.iter
(fun stmt s ->
let kf = Kernel_function.find_englobing_kf stmt in
let prev =
try Kf_containing_highlighted_stmt.find kf
with Not_found -> D.String.Set.empty
in
let union = D.String.Set.union prev s in
Kf_containing_highlighted_stmt.replace kf union)
s;
!update_column `Contents
end
module type DpdCmdSig = sig
type t_in
val help : string
val _get_info : (Kernel_function.t * Cil_types.stmt) option -> string
val compute : Kernel_function.t -> Cil_types.stmt -> t_in -> string
val tag_stmt : Cil_types.stmt -> (string * GText.tag_property list)
val clear: unit -> unit
end
module DataScope : (DpdCmdSig with type t_in = lval) = struct
type t_in = lval
module Fscope =
Make_StmtSetState
(struct let name = "Dpds_gui.Highlighter.Fscope" end)
module FBscope =
Make_StmtSetState
(struct let name = "Dpds_gui.Highlighter.FBscope" end)
module Bscope =
Make_StmtSetState
(struct let name = "Dpds_gui.Highlighter.Bscope" end)
let clear () = Fscope.clear(); FBscope.clear(); Bscope.clear()
let help = ("[data_scope] "
^"highlight the statements where the value of D is the same "
^"than at its value at L.\n\t"
^"For more information, please look at the Scope plugin documentation.")
let _get_info _kf_stmt_opt =
if Stmt.Hptset.is_empty (Fscope.get ())
&& Stmt.Hptset.is_empty (FBscope.get ())
&& Stmt.Hptset.is_empty (Bscope.get ())
then ""
else "[scope] selected"
let compute kf stmt lval =
let f, (fb, b) = Datascope.get_data_scope_at_stmt kf stmt lval in
Fscope.set f; FBscope.set fb; Bscope.set b;
"[scope] computed"
let tag_stmt stmt =
if Stmt.Hptset.mem stmt (Fscope.get()) then scope_f_tag
else if Stmt.Hptset.mem stmt (FBscope.get()) then scope_fb_tag
else if Stmt.Hptset.mem stmt (Bscope.get()) then scope_b_tag
else empty_tag
end
module Pscope = struct
module Pscope =
Make_StmtSetState
(struct let name = "Dpds_gui.Highlighter.Pscope" end)
module Pscope_warn =
State_builder.List_ref
(Code_annotation)
(struct
let name = "Dpds_gui.Highlighter.Pscope_warn"
let dependencies = [ Eva.Analysis.self ]
end)
let clear () = Pscope.clear(); Pscope_warn.clear()
let help = ("[prop_scope] "
^"highlight the statements where the value of the assertion is also ok\n\t"
^"For more information, please look at the Scope plugin documentation.")
let _get_info _kf_stmt_opt =
if Stmt.Hptset.is_empty (Pscope.get ())
then ""
else "[prop_scope] selected"
let compute kf stmt annot =
let s1, s2 = Datascope.get_prop_scope_at_stmt kf stmt annot in
Pscope.set s1; Pscope_warn.set s2;
"[prop_scope] computed"
let tag_stmt stmt =
if Stmt.Hptset.mem stmt (Pscope.get()) then scope_p_tag
else empty_tag
let tag_annot annot =
let tag =
List.exists (fun a -> a.annot_id = annot.annot_id) (Pscope_warn.get())
in if tag then scope_p_warn_tag else empty_tag
end
module ShowDef : (DpdCmdSig with type t_in = lval) = struct
type t_in = lval
module ShowDefState =
Make_StmtMapState
(struct let name = "Dpds_gui.Highlighter.ShowDef" end)
let clear () = ShowDefState.clear()
let help = ("[show_def] "
^"highlight the statements that define the value of D at L,\n\t"
^"and print a message if a part of D might be undefined.\n\t"
^"Notice that 'undefined' only means here "
^"not defined on some path from the beginning of the function.")
let _get_info _kf_stmt_opt =
if Stmt.Map.is_empty (ShowDefState.get()) then ""
else "[show_def] selected"
let indirect_icon = Datatype.String.Set.singleton "gtk-jump-to"
let conv m =
let aux stmt (direct, indirect) acc =
let empty = Datatype.String.Set.empty in
let direct = if direct then default_icon else empty in
let indirect = if indirect then indirect_icon else empty in
let s = Datatype.String.Set.union direct indirect in
if Datatype.String.Set.is_empty s then acc else Stmt.Map.add stmt s acc
in
Stmt.Map.fold aux m Stmt.Map.empty
let compute kf stmt lv =
let z = eval_lval stmt lv in
let r = Defs.compute_with_def_type_zone kf stmt z in
Datascope.R.feedback "Defs computed";
match r with
| None -> clear ();
"[Show Defs] nothing found. The information about some functions \
may be missing."
| Some (defs, undef) ->
let msg = match undef with
| None -> ""
| Some undef ->
Format.asprintf "[Show Defs] notice that %a %s"
pretty_zone undef
"may not be defined by this function at this point"
in
ShowDefState.set (conv defs); msg
let tag_stmt stmt =
try
let s = Stmt.Map.find stmt (ShowDefState.get()) in
if Datatype.String.Set.mem default_icon_name s
then show_def_direct_tag else show_def_indirect_tag
with Not_found -> empty_tag
end
module Zones : (DpdCmdSig with type t_in = lval) = struct
type t_in = lval
module ZonesState =
struct include State_builder.Option_ref
(Datatype.Pair
(Stmt.Hashtbl.Make(Locations.Zone))
(Stmt.Hptset))
(struct
let name = "Dpds_gui.Highlighter.ZonesState"
let dependencies = [ Eva.Analysis.self ]
end)
let set s =
set s;
Kf_containing_highlighted_stmt.clear ();
Stmt.Hptset.iter
(fun stmt ->
Kf_containing_highlighted_stmt.replace
(Kernel_function.find_englobing_kf stmt) default_icon)
(snd s);
!update_column `Contents
end
let clear () = ZonesState.clear ()
let help =
("[zones] computes, for each point Li of the function, "
^"the data Di needed to know the value of D at L.\n"
^"\tAfter this computation, the result Di will be printed in the "
^" information window each time a statement Li is selected.")
let _get_info kf_stmt_opt =
try
let zones, _ = ZonesState.get () in
match kf_stmt_opt with
| None -> "[zones] no information for this point"
| Some (_kf, stmt) ->
let z = Zones.get_zones zones stmt in
let txt =
Format.asprintf "[zones] needed before stmt %d = %a"
stmt.sid pretty_zone z
in txt
with Not_found -> ""
let compute kf stmt lval =
let used_stmts, zones = Zones.build_zones kf stmt lval in
ZonesState.set (zones, used_stmts);
"[zones] computed"
let tag_stmt stmt =
let is_used =
try
let _zones, used = ZonesState.get () in
Stmt.Hptset.mem stmt used
with Not_found -> false
in
if is_used then zones_used_tag else empty_tag
end
let help (main_ui:Design.main_window_extension_points) =
let add txt = add_msg main_ui txt in
add ("General : "
^"each of these commands starts from a data D at a program point L.\n\t"
^"The program point is the one that is before the selected statement,\n\t"
^"and the data is the one that is selected if any, "
^"or it can be given via a popup.\n"
^"\tIf the text given in the popup is empty, or 'Cancel' is chosen, "
^"the selection of the command is reset.");
add (ShowDef.help);
add (Zones.help);
add (DataScope.help);
add (Pscope.help);
add ("Reset : reset the internal state for all the previous commands.")
module DpdsState =
State_builder.Option_ref
(Stmt)
(struct
let name = "Dpds_gui.Highlighter.DpdsState"
let dependencies = [ Eva.Analysis.self ]
end)
let reset () =
DpdsState.clear ();
ShowDef.clear ();
Zones.clear ();
DataScope.clear ();
Pscope.clear ();
Kf_containing_highlighted_stmt.clear ();
!update_column `Contents
type funct = Defs | Zones | Scope | Pscope
let callbacks funct main_ui (kf, stmt, localizable) =
let compute f arg =
let msg = f kf stmt arg in
if msg <> "" then add_msg main_ui msg
in
let set_txt x =
let txt = Format.asprintf
"[dependencies] for %s before stmt %d in %a"
x stmt.sid Kernel_function.pretty kf
in
DpdsState.set stmt;
add_msg main_ui txt
in
let aux_on_lval funct_compute =
match get_lval_opt main_ui (Some kf) localizable with
| None -> ()
| Some (lval_txt, lval) ->
set_txt lval_txt;
compute funct_compute lval
in
reset ();
begin match funct with
| Pscope -> begin
match get_annot_opt localizable with
| Some ({annot_content = (AAssert _)} as annot) ->
set_txt ("annotation "^(string_of_int annot.annot_id));
compute Pscope.compute annot
| _ -> ()
end
| Defs -> aux_on_lval ShowDef.compute
| Zones -> aux_on_lval Zones.compute
| Scope -> aux_on_lval DataScope.compute
end;
main_ui#rehighlight ()
let highlighter (buffer:Design.reactive_buffer) localizable ~start ~stop =
try
let buffer = buffer#buffer in
let start_s = DpdsState.get () in
let put_tag tag = match tag with ("",[]) -> ()
| _ -> add_tag buffer tag start stop
in
match localizable with
| PStmt (_,stmt) ->
if start_s.sid = stmt.sid then put_tag scope_start_tag;
put_tag (Pscope.tag_stmt stmt);
put_tag (DataScope.tag_stmt stmt);
put_tag (Zones.tag_stmt stmt );
put_tag (ShowDef.tag_stmt stmt)
| PIP (Property.(IPCodeAnnot {ica_ca})) ->
put_tag (Pscope.tag_annot ica_ca)
| PStmtStart _ | PExp _
| PVDecl _ | PTermLval _ | PLval _ | PGlobal _ | PIP _ | PType _ -> ()
with Not_found -> ()
let check_value (main_ui:Design.main_window_extension_points) =
if Eva.Analysis.is_computed () then true
else
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 with its current settings now?")
in
if answer = 1 then
match main_ui#full_protect ~cancelable:true Eva.Analysis.compute with
| Some _ ->
main_ui#redisplay ();
true
| None -> false
else false
(** To add a sensitive/insensitive 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 [~use_values], check if the value analysis has been computed.
*)
let add_item (main_ui:Design.main_window_extension_points)
~use_values (factory:GMenu.menu GMenu.factory) name arg_opt callback =
match arg_opt with
| None ->
let item = factory#add_item name ~callback: (fun () -> ())
in item#misc#set_sensitive false
| Some arg ->
let cb arg =
if use_values then
if check_value main_ui then callback arg else ()
else callback arg
in
ignore (factory#add_item name ~callback: (fun () -> cb arg))
let selector (:GMenu.menu GMenu.factory)
(main_ui:Design.main_window_extension_points)
~button localizable =
if button = 3 then
begin
let = popup_factory#add_submenu "Dependencies" in
let = new GMenu.factory submenu in
let arg = match (Pretty_source.kf_of_localizable localizable,
Pretty_source.ki_of_localizable localizable)
with
| Some kf, Kstmt st -> Some(kf, st, localizable)
| Some _, Kglobal | None, _ -> None
in
let add_zones_item name funct =
add_item main_ui ~use_values:true
submenu_factory name arg
(fun arg ->
main_ui#protect ~cancelable:true
(fun () -> callbacks funct main_ui arg))
in
add_zones_item "Show defs" Defs;
add_zones_item "Zones" Zones;
add_zones_item "DataScope" Scope;
add_zones_item "PropScope" Pscope;
ignore (submenu_factory#add_separator ());
add_item main_ui ~use_values:false submenu_factory "Reset All" (Some())
(fun _ -> reset () ; main_ui#rehighlight ());
ignore (submenu_factory#add_separator ());
add_item main_ui ~use_values:false submenu_factory
"Help" (Some()) (fun _ -> help main_ui) ;
end
let filetree_decorate main_ui =
main_ui#file_tree#append_pixbuf_column
~title:"Scope"
(fun globs ->
let icons = function
| GFun ({svar = v }, _) ->
(try Kf_containing_highlighted_stmt.find (Globals.Functions.get v)
with Not_found -> Datatype.String.Set.empty)
| _ -> Datatype.String.Set.empty
in
let ids =
if Kf_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 _ -> Kf_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