-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpp_axiomatic_generator.ml
580 lines (560 loc) · 19.9 KB
/
rpp_axiomatic_generator.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
578
579
580
(**************************************************************************)
(* This file is part of RPP plug-in of Frama-C. *)
(* *)
(* Copyright (C) 2016-2023 *)
(* 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 Cil_types
open Rpp_types
let map_extend f l ll =
let rec aux func list list_long acc =
match list,list_long with
| [], _::_ -> (List.rev acc, list_long)
| [],[] -> (List.rev acc, list_long)
| h1 :: q1 , h2 :: q2 -> aux f q1 q2 (func h1 h2::acc)
| _::_ ,[] -> invalid_arg "map_extend"
in
aux f l ll []
(**
Generation of the axiomatic definition
**)
let generat_axiom l self new_predi new_labels logic_info logic_info_pure =
let name_axiome =
String.concat "_" ["Relational_axiome";string_of_int (Rpp_options.Counting_axiome.next ())]
in
let name_lemma =
String.concat "_" ["Relational_lemma";string_of_int (Rpp_options.Counting_axiome.get ())]
in
let new_labels = match new_labels with
| _ :: _ -> new_labels
| [] -> [FormalLabel("L")]
in
let new_predi = Logic_const.toplevel_predicate new_predi in
let lemma =
Cil_types.Dlemma (name_lemma,new_labels,[], new_predi,[],l)
in
let functions =
Hashtbl.fold (fun _ (logic_information,_) acc ->
Queue.add (fun () ->
Logic_utils.add_logic_function logic_information;
)
self#get_filling_actions;
(Cil_types.Dfun_or_pred (logic_information,l)) :: acc)
logic_info_pure.predicate_info_pure []
in
let pred =
Hashtbl.fold (fun _ (logic_information,_,_,_,_) acc ->
Queue.add (fun () ->
Logic_utils.add_logic_function logic_information;
)
self#get_filling_actions;
(Cil_types.Dfun_or_pred (logic_information,l)) :: acc)
logic_info.predicate_info []
in
let axiome =
Cil_types.Daxiomatic(name_axiome,functions@pred@[lemma],[],l)
in
Queue.add(fun () ->
Annotations.add_global (Rpp_options.emitter) axiome;
)
self#get_filling_actions;
(axiome,name_lemma,new_predi)
let check_type t1 t2 =
match t1, t2 with
| Ctype(t1),t2 when Cil_datatype.Typ.equal t1 t2 -> ()
| _ , _ -> assert false
let param_generat self x y l label =
let x = Visitor_behavior.Get.varinfo self#behavior x in
let term_nodei =
TLval(TVar(Cil.cvar_to_lvar x),TNoOffset)
in
let termi =
{term_node = term_nodei;
term_loc = l;
term_type = Ctype(x.vtype);
term_name = []}
in
let old_term_nodei =
Tat(termi,BuiltinLabel(label))
in
let term = {
term_node = old_term_nodei;
term_loc = l;
term_type = Ctype(x.vtype);
term_name = []}
in
check_type y.lv_type x.vtype;
term
let pointer_param_generat self x y l =
let x = Visitor_behavior.Get.varinfo self#behavior x in
let term_nodei =
TLval(TVar(Cil.cvar_to_lvar x),TNoOffset)
in
let term ={
term_node = term_nodei;
term_loc = l;
term_type = Ctype(x.vtype);
term_name = []}
in
check_type y.lv_type x.vtype;
term
(**
Generation of behaviour clauses for the pure function involved in the relational propertie
*)
let generat_behavior_pure l self logic_info_pure =
let name_behavior =
String.concat "_" ["Relational_behavior";
string_of_int (Rpp_options.Counting_axiome.get ())]
in
Hashtbl.iter (
fun _ (logic_information,kf) ->
Queue.add(fun () ->
let kf = Visitor_behavior.Get.kernel_function self#behavior kf in
let params = List.map2 (fun x y ->
param_generat self x y l Old
) (Globals.Functions.get_params kf) (logic_information.l_profile)
in
let term_node1 =
TLval(TResult(Kernel_function.get_return_type kf),TNoOffset)
in
let term_node2 =
Tapp(logic_information,[],params)
in
let logic_return_type =
match logic_information.l_type with
| Some x -> x
| None -> assert false
in
let t =
Kernel_function.get_return_type kf
in
check_type logic_return_type t;
let term1 = {
term_node = term_node1;
term_loc = l;
term_type = Ctype(t);
term_name = []}
in
let term2 = {
term_node = term_node2;
term_loc = l;
term_type = logic_return_type;
term_name = []}
in
let predi =
Logic_const.prel(Req,term1,term2)
in
let ensures =
(Normal,Logic_const.new_predicate predi)
in
let funbehavior =
Cil.mk_behavior ~name:name_behavior ~post_cond:[ensures] ()
in
Annotations.add_behaviors ~register_children:true (Rpp_options.emitter) kf [funbehavior];
(*Set the status of the clause to true (the behavior is supposed valid)*)
let property =
Property.ip_of_ensures kf (Kglobal) funbehavior ensures
in
Property_status.emit (Rpp_options.emitter) ~hyps:[] property Property_status.True;
)
self#get_filling_actions)
logic_info_pure.predicate_info_pure
let make_result kf sub l =
match Kernel_function.get_return_type kf, sub with
| TVoid(_), [] -> []
| _ , [y]->
begin
let t =
Kernel_function.get_return_type kf
in
let term_node =
TLval(TResult(t),TNoOffset)
in
let term = {
term_node = term_node;
term_loc = l;
term_type = Ctype(t);
term_name = []}
in
check_type y.lv_type t;
[term]
end
| _ , _ -> assert false
let make_labels logic_information =
match logic_information.l_labels with
| FormalLabel("pre") :: FormalLabel("post") :: [] ->
[BuiltinLabel(Pre);BuiltinLabel(Post)]
| FormalLabel("post") :: [] ->
[BuiltinLabel(Post)]
| FormalLabel("pre") :: [] ->
[BuiltinLabel(Pre)]
| [] -> []
| _ -> assert false
(**
Generation of behaviour clauses for the function involved in the relational propertie
*)
let generat_behavior l self logic_info =
let name_behavior =
String.concat "_" ["Relational_behavior";
string_of_int (Rpp_options.Counting_axiome.get ())]
in
Hashtbl.iter (
fun _ (logic_information, kf,assigns,froms,pointers) ->
Queue.add(fun () ->
let (params,sub) =
map_extend (fun x y->
param_generat self x y l Old
) (Globals.Functions.get_params kf) (logic_information.l_profile)
in
let (params_froms,sub) =
map_extend (fun x y ->
param_generat self x y l Pre
) froms sub
in
let (params_assigns_post,sub) =
map_extend (fun x y->
param_generat self x y l Post
) assigns sub
in
let (param_pointers,sub) =
map_extend (fun x y ->
pointer_param_generat self x y l
) pointers sub
in
let param_result = make_result kf sub l in
let labels = make_labels logic_information in
let predicate =
Papp(logic_information,labels,
params@params_froms@params_assigns_post@param_pointers@param_result)
in
let loc = l in
let predicate_name =
{pred_name = [];pred_loc= loc;pred_content= predicate}
in
let ensures =
(Normal,Logic_const.new_predicate predicate_name)
in
let funbehavior =
Cil.mk_behavior ~name:name_behavior ~post_cond:[ensures] ()
in
Annotations.add_behaviors ~register_children:true (Rpp_options.emitter) kf [funbehavior];
(*Set the status of the clause to true (the behavior is supposed valid)*)
let property =
Property.ip_of_ensures kf (Kglobal) funbehavior ensures
in
Property_status.emit (Rpp_options.emitter) ~hyps:[] property Property_status.True;
)
self#get_filling_actions)
logic_info.predicate_info
(**
Generation of behaviour clauses for the target function involved in the relational propertie
*)
let generat_behavior_for_kf l self logic_info (target_kf,replace_target) global_map =
let name_behavior =
"Relational_behavior"
in
let (from_map,assert_map,from_p_map,assert_p_map) =
global_map
in
Hashtbl.iter (
fun _ (logic_information, kf,assigns,froms,pointers) ->
if Cil_datatype.Kf.equal kf replace_target then
begin
let (params,sub) =
map_extend (fun x y->
param_generat self x y l Old
) (Globals.Functions.get_params target_kf) (logic_information.l_profile)
in
let (params_froms,sub) =
map_extend (fun x y ->
match Cil_datatype.Varinfo.Map.find x from_map with
| exception Not_found -> assert false
| {lv_origin = Some x} -> param_generat self x y l Pre
| _ -> assert false
) froms sub
in
let (params_assigns_post,sub) =
map_extend (fun x y->
match Cil_datatype.Varinfo.Map.find x assert_map with
| exception Not_found -> assert false
| {lv_origin = Some x} -> param_generat self x y l Post
| _ -> assert false
) assigns sub
in
let (param_pointers,sub) =
map_extend (fun x y ->
match Cil_datatype.Varinfo.Map.find x from_p_map with
| exception Not_found ->
begin
match Cil_datatype.Varinfo.Map.find x assert_p_map with
| exception Not_found -> assert false
| {lv_origin = Some x} -> pointer_param_generat self x y l
| _ -> assert false
end
| {lv_origin = Some x} -> pointer_param_generat self x y l
| _ -> assert false
) pointers sub
in
let param_result = make_result kf sub l in
let labels = make_labels logic_information in
let predicate =
Papp(logic_information,labels,
params@params_froms@params_assigns_post@param_pointers@param_result)
in
let loc = l in
let predicate_name =
{pred_name = [];pred_loc= loc;pred_content= predicate}
in
(*Since Cil_datatype.Predicate.equal don't support the annotation, we use string equality.
This is a temporare solution and not very nice*)
let test =
Annotations.fold_ensures(fun _ (_,pred) test ->
let s1 =
Format.asprintf "Fold pred %a @."
Printer.pp_predicate pred.ip_content.tp_statement
in
let s2 =
Format.asprintf "Fold pred %a @." Printer.pp_predicate predicate_name
in
String.equal s1 s2 || test
) target_kf name_behavior false
in
match test with
| false ->
let ensures =
(Normal,Logic_const.new_predicate predicate_name)
in
let funbehavior =
Cil.mk_behavior ~name:name_behavior ~post_cond:[ensures] ()
in
Annotations.add_behaviors
~register_children:true (Rpp_options.emitter) target_kf [funbehavior];
(*Set the status of the clause to true (the behavior is supposed valid)*)
let property =
Property.ip_of_ensures target_kf (Kglobal) funbehavior ensures
in
Property_status.emit (Rpp_options.emitter) ~hyps:[] property Property_status.True;
| true -> ()
end
else ()) logic_info.predicate_info
(**
Generation of behaviour clauses for the target pure function involved in the relational propertie
*)
let generat_behavior_pure_for_kf l self logic_info_pure (target_kf,replace_target)=
let name_behavior =
"Relational_behavior"
in
Hashtbl.iter (
fun _ (logic_information,kf) ->
if Cil_datatype.Kf.equal kf replace_target then
begin
let params = List.map2 (fun x y ->
param_generat self x y l Old
) (Globals.Functions.get_params target_kf) (logic_information.l_profile)
in
let term_node1 =
TLval(TResult(Kernel_function.get_return_type target_kf),TNoOffset)
in
let term_node2 =
Tapp(logic_information,[],params)
in
let logic_return_type =
match logic_information.l_type with
| Some x -> x
| None -> assert false
in
let t =
Kernel_function.get_return_type kf
in
check_type logic_return_type t;
let term1 = {
term_node = term_node1;
term_loc = l;
term_type =Ctype(Kernel_function.get_return_type target_kf);
term_name = []}
in
let term2 = {
term_node = term_node2;
term_loc = l;
term_type = logic_return_type;
term_name = []}
in
let predicate_name =
Logic_const.prel(Req,term1,term2)
in
(*Since Cil_datatype.Predicate.equal don't support the annotation, we use string equality.
This is a temporare solution and not very nice*)
let test =
Annotations.fold_ensures(fun _ (_,pred) test ->
let s1 =
Format.asprintf "Fold pred %a @."
Printer.pp_predicate pred.ip_content.tp_statement
in
let s2 =
Format.asprintf "Fold pred %a @." Printer.pp_predicate predicate_name
in
String.equal s1 s2 || test
) target_kf name_behavior false
in
match test with
| false ->
let ensures =
(Normal,Logic_const.new_predicate predicate_name)
in
let funbehavior =
Cil.mk_behavior ~name:name_behavior ~post_cond:[ensures] ()
in
Annotations.add_behaviors
~register_children:true (Rpp_options.emitter) target_kf [funbehavior];
(*Set the status of the clause to true (the behavior is supposed valid)*)
let property =
Property.ip_of_ensures target_kf (Kglobal) funbehavior ensures
in
Property_status.emit (Rpp_options.emitter) ~hyps:[] property Property_status.True;
| true -> ()
end
else ()) logic_info_pure.predicate_info_pure
(**
Generation of behaviour clauses for the target pure function involved in the relational propertie
*)
let generat_help_behavior_pure_for_kf l logic_infos_pure (target_kf,replace_target)=
let name_behavior =
"Relational_behavior_helper"
in
let functions = Hashtbl.create 7 in
List.iter(
fun logic_info_pure ->
Hashtbl.iter (
fun _ (logic_information,kf) ->
if not(Cil_datatype.Kf.equal kf replace_target) then
begin
match Hashtbl.find functions kf with
| exception Not_found ->
Hashtbl.add functions kf (ref ([logic_information]))
| x -> x:= logic_information :: !x
end
)logic_info_pure
)logic_infos_pure;
let rec aux list acc =
match list with
| [] -> ()
| h :: q ->
begin
let param = List.map (fun x ->
Cil_const.make_logic_var_quant
(x.lv_name) (x.lv_type)
) (acc.l_profile)
in
let params = List.map (fun x ->
let term_nodei =
TLval(TVar(x),TNoOffset)
in
{term_node = term_nodei;
term_loc = l;
term_type = x.lv_type;
term_name = []}
) param
in
let term_node1 =
Tapp(acc,[],params)
in
let t =
match acc.l_type with
| None -> assert false
| Some x -> x
in
let term1 = {term_node = term_node1;
term_loc = l;
term_type = t;
term_name = []}
in
let term_node2 =
Tapp(List.hd list,[],params)
in
let term2 = {term_node = term_node2;
term_loc = l;
term_type = t;
term_name = []}
in
let predi =
Logic_const.prel(Req,term1,term2)
in
let new_axiome_predicate_content =
Pforall(param,predi)
in
let predicate_name = {pred_name = [];
pred_loc = l;
pred_content = new_axiome_predicate_content;}
in
(*Since Cil_datatype.Predicate.equal don't support the annotation, we use string equality.
This is a temporare solution and not very nice*)
let test =
Annotations.fold_ensures(fun _ (_,pred) test ->
let s1 =
Format.asprintf "Fold pred %a @." Printer.pp_predicate pred.ip_content.tp_statement
in
let s2 =
Format.asprintf "Fold pred %a @." Printer.pp_predicate predicate_name
in
String.equal s1 s2 || test
) target_kf name_behavior false
in
begin
match test with
| false ->
let ensures =
(Normal,Logic_const.new_predicate predicate_name)
in
let funbehavior =
Cil.mk_behavior ~name:name_behavior ~post_cond:[ensures] ()
in
Annotations.add_behaviors
~register_children:true (Rpp_options.emitter) target_kf [funbehavior];
(*Set the status of the clause to true (the behavior is supposed valid)*)
let property =
Property.ip_of_ensures target_kf (Kglobal) funbehavior ensures
in
Property_status.emit (Rpp_options.emitter) ~hyps:[] property Property_status.True
| true -> ()
end;
aux q h
end
in
Hashtbl.iter(
fun _ l ->
aux (List.tl !l) (List.hd !l)
)functions
(**
Function for generating the axiomatic definition related to the
relational property and the corresponding behaviour for each
function related in the relational property
*)
let relationnel_axiom
il_loc self new_predi new_labels (logic_info) (logic_info_pure)
=
let (axiome,il_name,il_pred) =
generat_axiom il_loc self new_predi new_labels logic_info logic_info_pure
in
generat_behavior_pure il_loc self logic_info_pure;
generat_behavior il_loc self logic_info;
Property.(axiome,
{ il_name;
il_labels = [];
il_args = [];
il_pred;
il_attrs = [];
il_loc;
})