package mopsa

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

Source file body.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
(****************************************************************************)
(*                                                                          *)
(* 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/>.    *)
(*                                                                          *)
(****************************************************************************)

(** Inter-procedural iterator of stubs by inlining. *)

(** FIXME: remove calls to post_to_flow to preserve logs *)

open Mopsa
open Sig.Abstraction.Stateless
open Universal.Ast
open Ast
open Alarms


module Domain =
struct

  include GenStatelessDomainId(struct
      let name = "stubs.iterators.body"
    end)

  let checks = [CHK_STUB_CONDITION]


  (** Initialization of environments *)
  (** ============================== *)

  let init prog man flow = flow


  (** {2 Command-line options} *)
  (** ************************ *)

  let opt_stub_ignored_cases : string list ref = ref []
  (** List of ignored stub cases *)

  let () = register_builtin_option {
      key      = "-stub-ignore-case";
      doc      = " list of stub cases to ignore";
      category = "Stubs";
      spec     = ArgExt.Set_string_list opt_stub_ignored_cases;
      default  = "";
    }

  (** Check whether a case is ignored *)
  let is_case_ignored stub case : bool =
    match stub with
    | None -> false
    | Some s -> List.mem (s.stub_func_name ^ "." ^ case.case_label) !opt_stub_ignored_cases


  (** Evaluation of expressions *)
  (** ========================= *)

  (** Negate a formula *)
  let rec negate_formula (f:formula with_range) : formula with_range =
    match f.content with
    | F_expr e ->
      with_range (F_expr (mk_not e e.erange)) f.range

    | F_binop (IMPLIES, f1, f2) ->
      with_range (
        F_binop (
          AND,
          f1,
          negate_formula f2
        )) f.range

    | F_binop (op, f1, f2) ->
     with_range (F_binop (
         negate_log_binop op,
         negate_formula f1,
         negate_formula f2
       )) f.range

    | F_not f -> f

    | F_forall (var, set, ff) ->
      with_range (F_exists (
          var,
          set,
          negate_formula ff
        )) f.range

    | F_exists (var, set, ff) ->
      with_range (F_forall (
          var,
          set,
          negate_formula ff
        )) f.range

    | F_in (e, S_interval(l,u)) ->
      with_range (
        F_expr (
          mk_binop
            (mk_binop e O_lt l ~etyp:T_bool f.range)
            O_log_or
            (mk_binop e O_gt u ~etyp:T_bool f.range)
            ~etyp:T_bool
            f.range
        )
      ) f.range

    | F_in (e, S_resource res) ->
      with_range (
        F_expr (
          mk_not (mk_stub_resource_mem e res f.range) f.range
        )
      ) f.range

    | F_otherwise _ -> panic_at f.range "negation of 'otherwise' formulas not possible"

    | F_if(c,f1,f2) ->
      with_range (F_if (c, negate_formula f1, negate_formula f2)) f.range

  (** Translate a formula into prenex normal form *)
  let rec formula_to_prenex f =
    match f.content with
    | F_expr cond ->
      [], cond

    | F_binop (AND, f1, f2) ->
      let quants1,cond1 = formula_to_prenex f1 in
      let quants2,cond2 = formula_to_prenex f2 in
      quants1@quants2, mk_log_and cond1 cond2 f.range

    | F_binop (OR, f1, f2) ->
      let quants1,cond1 = formula_to_prenex f1 in
      let quants2,cond2 = formula_to_prenex f2 in
      quants1@quants2, mk_log_or cond1 cond2 f.range

    | F_binop (IMPLIES, f1, f2) ->
      let quants1,cond1 = formula_to_prenex (negate_formula f1) in
      let quants2,cond2 = formula_to_prenex f2 in
      quants1@quants2, mk_log_or cond1 cond2 f.range

    | F_not ff ->
      formula_to_prenex (negate_formula ff)

    | F_in (e, S_interval (l, u)) ->
      [], mk_in e l u f.range

    | F_in (e, S_resource res) ->
      [], mk_stub_resource_mem e res f.range

    | F_forall(v,s,ff) ->
      let quants,cond = formula_to_prenex ff in
      (FORALL,v,s)::quants, cond

    | F_exists(v,s,ff) ->
      let quants,cond = formula_to_prenex ff in
      (EXISTS,v,s)::quants, cond

    | F_otherwise(ff, e) ->
      let quants,cond = formula_to_prenex ff in
      quants, mk_stub_otherwise cond (Some e) f.range

    | F_if(c,f1,f2) ->
      let quants,cond = formula_to_prenex c in
      let quants1,cond1 = formula_to_prenex f1 in
      let quants2,cond2 = formula_to_prenex f2 in
      quants@quants1@quants2, mk_stub_if cond cond1 cond2 f.range


  (* Function to get variables in a condition (and avoid the alarm expression in `otherwise` *)
  let rec vars_of_condition cond =
    fold_expr
      (fun acc e ->
         match ekind e with
         | E_var (v,_) -> Keep (VarSet.add v acc)
         | E_stub_otherwise(ee, _) -> Keep (VarSet.union (vars_of_condition ee) acc)
         | _ -> VisitParts acc
      )
      (fun acc s -> assert false)
      VarSet.empty cond

  (* Check if a variable is used in an expression *)
  let var_in_expr v e =
    exists_expr
      (fun ee ->
         match ekind ee with
         | E_var (vv,_) -> compare_var v vv = 0
         | _ -> false
      )
      (fun s -> false)
      e

  (* Function to remove unnecessary quantifiers not used in an expression *)
  let remove_unnecessary_quantifiers quants e =
    let vars = vars_of_condition e in
    (* A quantified var is necessary if (i) it used in the condition, or (ii)
       used in the bounds of an other necessary quantifier *)
    let quants =
      List.map
        (fun ((_,v,_) as q) -> (q, VarSet.mem v vars))
        quants
    in
    let rec iter = function
      | [] -> []
      | (_,true) as hd::tl ->
        (* Already used quantifier => keep it *)
        hd::iter tl
      | ((_,v,_) as q,false) as hd::tl ->
        (* Check if this unused quantifier is present in the bounds of an
           already used quantifier *)
        if List.exists (function
            | ((_,_,S_interval(lo,hi)),true) ->
              var_in_expr v lo || var_in_expr v hi
            | _ -> false
          ) tl
        then (q,true)::iter tl
        else hd::iter tl
    in
    (* Iterate [iter] until no new used quantifier is found *)
    let rec fp quants =
      let quants' = iter quants in
      if List.exists (fun ((_,b1),(_,b2)) -> b1 != b2) (List.combine quants quants') then
        fp quants'
      else
        quants
    in
    fp quants |>
    (* Keep only used quantifiers *)
    List.filter (fun (_,b) -> b) |>
    List.map fst


  (** Translate a prenex encoding (i.e. quantifiers and a condition) into an expression *)
  let rec prenex_to_expr quants cond range =
    if quants = [] then cond
    else
      match ekind cond with
      | E_binop(O_log_and, e1, e2) ->
        let quants1 = remove_unnecessary_quantifiers quants e1 in
        let quants2 = remove_unnecessary_quantifiers quants e2 in
        let e1' = prenex_to_expr quants1 e1 e1.erange in
        let e2' = prenex_to_expr quants2 e2 e2.erange in
        mk_log_and e1' e2' range

      | E_binop(O_log_or, e1, e2) ->
        let quants1 = remove_unnecessary_quantifiers quants e1 in
        let quants2 = remove_unnecessary_quantifiers quants e2 in
        let e1' = prenex_to_expr quants1 e1 e1.erange in
        let e2' = prenex_to_expr quants2 e2 e2.erange in
        mk_log_or e1' e2' range

      | E_stub_if(c,e1,e2) ->
        let quants' = remove_unnecessary_quantifiers quants c in
        if quants' = [] then
          let e1' = prenex_to_expr quants e1 e1.erange in
          let e2' = prenex_to_expr quants e2 e2.erange in
          { cond with ekind = E_stub_if(c, e1', e2') }
        else
          mk_stub_quantified_formula quants cond range

      | E_stub_otherwise(e,a) ->
        mk_stub_otherwise (prenex_to_expr quants e e.erange) a range

      | _ ->
        let quants' = remove_unnecessary_quantifiers quants cond in
        if quants' = [] then
          cond
        else
          mk_stub_quantified_formula quants' cond range

  (** Evaluate a formula *)
  let eval_formula
      (cond_to_stmt: expr -> range -> stmt)
      (f: formula with_range)
      man flow =
    (* Write formula in prenex normal form *)
    let quants,cond = formula_to_prenex f in
    (* Translate the prenex encoding into an expression *)
    let cond' = prenex_to_expr quants cond f.range in
    (* Constrain the environment with the obtained condition *)
    man.exec (cond_to_stmt cond' f.range) flow


  (** Initialize the parameters of the stubbed function *)
  let rec init_params args params range man flow =
    match params, args with
    | [], _ -> flow
    | param::tl_params, arg::tl_args ->
      let post = man.exec (mk_add_var param range) flow >>%
                 man.exec (mk_assign (mk_var param range) arg range)
      in
      post_to_flow man post |>
      init_params tl_args tl_params range man
    | _, [] ->
      panic "stubs: insufficent number of arguments"


  (** Remove parameters from the returned flow *)
  let remove_params params range man flow =
    params |> List.fold_left (fun flow param ->
        man.exec (mk_remove_var param range) flow |> post_to_flow man
      ) flow


  (** Evaluate the formula of the `assumes` section *)
  let exec_assumes assumes man flow =
    eval_formula mk_assume assumes.content man flow


  (** Evaluate the formula of the `requires` section *)
  let exec_requires req man flow =
    eval_formula mk_stub_requires req.content man flow


  (** Execute an allocation of a new resource *)
  let exec_local_new v res range man flow : 'a post =
    (* Evaluation the allocation request *)
    man.eval (mk_stub_alloc_resource res range) flow >>$ fun addr flow ->
    (* Assign the address to the variable *)
      man.exec (mk_assign (mk_var v range) addr range) flow


  (** Execute a function call *)
  (* FIXME: check the purity of f *)
  let exec_local_call v f args range man flow =
    man.exec (mk_assign
                (mk_var v range)
                (mk_expr (E_call(f, args)) ~etyp:v.vtyp range)
                range
             ) flow


  (** Execute the `local` section *)
  let exec_local l man flow =
    match l.content.lval with
    | L_new  res -> exec_local_new l.content.lvar res l.range man flow
    | L_call (f, args) -> exec_local_call l.content.lvar f args l.range man flow


  let exec_ensures e return man flow =
    (* Replace E_stub_return expression with the fresh return variable *)
    let f =
      match return with
      | None -> e.content
      | Some v ->
        visit_expr_in_formula
          (fun e ->
             match ekind e with
             | E_stub_return -> Keep { e with ekind = E_var (v, None) }
             | _ -> VisitParts e
          )
          e.content
    in
    (* Evaluate ensure body and return flows that verify it *)
    eval_formula mk_assume f man flow


  let exec_assigns assigns man flow =
    let stmt = mk_stub_assigns assigns.content.assign_target assigns.content.assign_offset assigns.range in
    match assigns.content.assign_offset with
    | [] ->
      man.exec stmt flow

    | (l,u)::tl ->
      (* Check that offsets intervals are not empty *)
      let range = tag_range assigns.range "condition" in
      let cond = List.fold_left
          (fun acc (l,u) -> log_and acc (le l u range) assigns.range)
          (le l u range) tl
      in
      assume cond
        ~fthen:(fun flow -> man.exec stmt flow)
        ~felse:(fun flow -> Post.return flow)
        man flow



  (** Remove locals *)
  let clean_post locals range man flow =
    let block =
      List.fold_left (fun block l ->
          mk_remove_var l.content.lvar range :: block
        ) [] locals
    in
    man.exec (mk_block block range) flow |> post_to_flow man


  let exec_free free man flow =
    let e = free.content in
    let stmt = mk_stub_free e free.range in
    man.exec stmt flow


  let exec_message msg man flow =
    if Flow.get T_cur man.lattice flow |> man.lattice.is_bottom
    then Post.return flow
    else match msg.content.message_kind with
      | WARN ->
        Exceptions.warn_at msg.range "%s" msg.content.message_body;
        Post.return flow

      | UNSOUND ->
        Post.return @@ Flow.add_local_assumption (Soundness.A_stub_soundness_message msg.content.message_body) msg.range flow


  (** Execute a leaf section *)
  let exec_leaf leaf return man flow : 'a post =
    match leaf with
    | S_local local -> exec_local local man flow 
    | S_assumes assumes -> exec_assumes assumes man flow
    | S_requires requires -> exec_requires requires man flow
    | S_assigns assigns -> exec_assigns assigns man flow
    | S_ensures ensures -> exec_ensures ensures return man flow
    | S_free free -> exec_free free man flow
    | S_message msg -> exec_message msg man flow

  (** Execute the body of a case section *)
  let exec_case case return man flow : 'a flow =
    List.fold_left (fun acc leaf ->
        acc >>% fun flow -> exec_leaf leaf return man flow
      ) (Post.return flow) case.case_body |>
    post_to_flow man |>
    (* Clean case post state *)
    clean_post case.case_locals case.case_range man


  (** Execute the body of a stub *)
  let exec_body ?(stub=None) body return range man (flow : 'a flow) =
    (* Execute leaf sections *)
    let post = List.fold_left (fun post section ->
        match section with
        | S_leaf leaf -> post >>% fun flow -> exec_leaf leaf return man flow 
        | _ -> post
      ) (Post.return flow) body
    in
    let flow = post_to_flow man post in 
    (* Execute case sections separately *)
    let flows, ctx = List.fold_left (fun (acc,ctx) section ->
        match section with
        | S_case case when not (is_case_ignored stub case) ->
          let flow = Flow.set_ctx ctx flow in
          let flow' = exec_case case return man flow in
          flow':: acc, Flow.get_ctx flow'
        | _ -> acc, ctx
      ) ([], Flow.get_ctx flow) body
    in
    let flows = List.map (Flow.set_ctx ctx) flows in
    (* Join flows *)
    (* FIXME: when the cases do not define a partitioning, we need
         to do something else *)
    Flow.join_list man.lattice flows ~empty:(fun () -> flow)


  let prepare_all_assigns assigns range man flow =
    (* Check if there are assigned variables *)
    if assigns = []
    then flow
    else man.exec (mk_stub_prepare_all_assigns assigns range) flow |> post_to_flow man

  let clean_all_assigns assigns range man flow =
    (* Check if there are assigned variables *)
    if assigns = []
    then flow
    else man.exec (mk_stub_clean_all_assigns assigns range) flow |> post_to_flow man

  (** Evaluate a call to a stub *)
  let eval_stub_call stub args return range man flow =
      (* Update the callstack *)
      let cs = Flow.get_callstack flow in
      let flow = Flow.push_callstack stub.stub_func_name range flow in
      (* Initialize parameters *)
      let flow = init_params args stub.stub_func_params range man flow in
      (* Prepare assignments *)
      let flow = prepare_all_assigns stub.stub_func_assigns (tag_range stub.stub_func_range "prepare") man flow in
      (* Create the return variable *)
      let flow =
        match return with
        | None -> flow
        | Some v -> man.exec (mk_add_var v range) flow |> post_to_flow man
      in
      (* Evaluate the body of the stb *)
      let flow = exec_body ~stub:(Some stub) stub.stub_func_body return range man flow in
      (* Clean locals *)
      let flow = clean_post stub.stub_func_locals (tag_range stub.stub_func_range "clean") man flow in
      (* Clean assignments *)
      let flow = clean_all_assigns stub.stub_func_assigns (tag_range stub.stub_func_range "clean") man flow in
      (* Restore the callstack *)
      let flow = Flow.set_callstack cs flow in
      let clean_range = tag_range range "clean" in
      let cleaners = List.map (fun param -> mk_remove_var param clean_range) stub.stub_func_params in

      match return with
      | None ->
        Eval.singleton (mk_unit range) flow ~cleaners

      | Some v ->
        man.eval (mk_var v range) flow |>
        Cases.add_cleaners (mk_remove_var v range :: cleaners)

  (** Evaluate an otherwise expression *)
  let eval_otherwise cond alarm range man flow =
    assume cond man flow
      ~fthen:(fun flow -> safe_stub_condition cond.erange man flow |>
                          Eval.singleton (mk_true range))
      ~felse:(fun flow ->
          match alarm with
          | Some e -> man.eval e flow
          | None   -> raise_stub_invalid_requirement ~bottom:false cond range man flow |>
                      Eval.singleton (mk_false range)
        )

  (* Remove flows where a quantification interval is empty *)
  let discard_empty_quantification_intervals quants cond range man flow =
    let remove_quant_vars vars evl =
      if vars = [] then evl
      else
        evl >>$ fun e flow ->
        List.fold_left (fun acc v ->
            acc >>% man.exec (mk_remove_var v range)
          ) (Post.return flow) vars
        >>% fun flow ->
        Eval.singleton e flow
    in
    let rec iter added l flow =
      match l with
      | [] ->
        man.eval ~route:(Below name) (mk_stub_quantified_formula quants cond range) flow |>
        remove_quant_vars added

      | (_,_,S_resource _)::tl ->
        iter added tl flow

      | (FORALL,v,S_interval(lo,hi))::tl ->
        assume
          (mk_le lo hi range) man flow
          ~fthen:(fun flow ->
              man.exec (mk_add_var v range) flow >>%
              man.exec (mk_assume (mk_in (mk_var v range) lo hi range) range) >>%
              iter (v::added) tl
            )
          ~felse:(fun flow ->
              Eval.singleton (mk_true range) flow |>
              remove_quant_vars added
            )

      | (EXISTS,v,S_interval(lo,hi))::tl ->
        assume
          (mk_le lo hi range) man flow
          ~fthen:(fun flow ->
              man.exec (mk_add_var v range) flow >>%
              man.exec (mk_assume (mk_in (mk_var v range) lo hi range) range) >>%
              iter (v::added) tl
            )
          ~felse:(fun flow ->
              Eval.singleton (mk_false range) flow |>
              remove_quant_vars added
            )
    in
    iter [] quants flow


  (** Check if a condition contains an otherwise expression *)
  let rec otherwise_in_condition cond =
    match ekind cond with
    | E_stub_otherwise _ -> true
    | E_binop((O_log_and | O_log_or), cond1, cond2) ->
      otherwise_in_condition cond1 || otherwise_in_condition cond2
    | E_stub_quantified_formula(_, qcond) ->
      otherwise_in_condition qcond
    | E_stub_if(_,fthen,felse) ->
      otherwise_in_condition fthen || otherwise_in_condition felse
    | _ -> false

  (** Remove newly introduced checks *)
  let remove_new_checks old flow =
    let report =
      fold2zo_report
        (fun diag1 acc -> acc)
        (fun diag2 acc -> remove_diagnostic diag2 acc )
        (fun diag1 diag2 acc -> acc)
        (Flow.get_report old) (Flow.get_report flow)
        (Flow.get_report flow)
    in
    Flow.set_report report flow

  (** Move newly introduced checks to a new range *)
  let move_new_checks range old flow =
    let report =
      fold2zo_report
        (fun diag1 acc -> acc)
        (fun diag2 acc -> remove_diagnostic diag2 acc |>
                          add_diagnostic {diag2 with diag_range = range} )
        (fun diag1 diag2 acc -> acc)
        (Flow.get_report old) (Flow.get_report flow)
        (Flow.get_report flow)
    in
    Flow.set_report report flow

  (** Entry point of expression evaluations *)
  let eval exp man flow =
    match ekind exp with
    | E_stub_call (stub, args) ->
      if man.lattice.is_bottom (Flow.get T_cur man.lattice flow) then Cases.empty flow |> OptionExt.return 
      else 
      (* Create the return variable *)
      let return =
        match stub.stub_func_return_type with
        | None   -> None
        | Some t -> Some (Universal.Iterators.Interproc.Common.mk_return exp None)
      in
      eval_stub_call stub args return exp.erange man flow |>
      OptionExt.return

    | E_stub_otherwise(cond, alarm) ->
      eval_otherwise cond alarm exp.erange man flow |>
      OptionExt.return

    | E_stub_raise msg ->
      raise_stub_alarm ~bottom:false msg exp.erange man flow |>
      Eval.singleton (mk_false exp.erange) |>
      OptionExt.return

    | E_binop(O_log_and, e1, e2) when otherwise_in_condition e1 && otherwise_in_condition e2 ->
      (* To evaluate a requirement e1 ∧ e2, we evaluate e1 and e2 and then we
         lift checks on e1 and e2 to e1 ∧ e2:
         - If both e1 and e2 are valid, then we mark e1 ∧ e2 as safe.
           We also remove checks on e1 and e2, since they are redundant with
           the check on e1 ∧ e2.
         - If e1 or e2 is invalid, we move the alarms to the range of e1 ∧ e2, but we keep the same alarm message.
      *)
      let flow0 = flow in
      assume e1 man flow
        ~fthen:(fun flow ->
            assume e2 man flow
              ~fthen:(fun flow ->
                  remove_new_checks flow0 flow |>
                  safe_stub_condition exp.erange man |>
                  Eval.singleton (mk_true exp.erange)
                )
              ~felse:(fun flow ->
                  move_new_checks exp.erange flow0 flow |>
                  Eval.singleton (mk_false exp.erange)
                )
          )
        ~felse:(fun flow ->
            move_new_checks exp.erange flow0 flow |>
            Eval.singleton (mk_false exp.erange)
          ) |>
      OptionExt.return

    | E_binop(O_log_or, e1, e2) when otherwise_in_condition e1 && otherwise_in_condition e2 ->
      (* To evaluate a requirement e1 ∨ e2, we evaluate e1 and e2 and then we
         lift checks on e1 and e2 to e1 ∨ e2:
         - If e1 or e2 is valid, then we mark e1 ∨ e2 as safe.
           We also remove checks on e1 and e2, since they are redundant with
           the check on e1 ∨ e2.
         - If e1 and e2 are invalid, we move the alarms to the range of e1 ∨ e2, but we keep the same alarm message.
      *)
      let flow0 = flow in
      assume e1 man flow
        ~fthen:(fun flow  ->
            remove_new_checks flow0 flow |>
            safe_stub_condition exp.erange man |>
            Eval.singleton (mk_true exp.erange)
          )
        ~felse:(fun flow ->
            assume e2 man flow
              ~fthen:(fun flow ->
                  remove_new_checks flow0 flow |>
                  safe_stub_condition exp.erange man |>
                  Eval.singleton (mk_true exp.erange)
                )
              ~felse:(fun flow ->
                  move_new_checks exp.erange flow0 flow |>
                  Eval.singleton (mk_false exp.erange)
                )
          ) |>
      OptionExt.return

    | E_stub_quantified_formula(quants, cond)
      when List.exists (function (_,_,S_interval _) -> true | _ -> false) quants ->
      discard_empty_quantification_intervals quants cond exp.erange man flow |>
      OptionExt.return

    | E_stub_if(c,f1,f2) ->
      assume c man flow
        ~fthen:(man.eval f1)
        ~felse:(man.eval f2) |>
      OptionExt.return

    | _ -> None


  (** Computation of post-conditions *)
  (** ============================== *)

  (** Execute a global stub directive *)
  let exec_directive stub range man flow =
      (* Prepare assignments *)
      let flow = prepare_all_assigns stub.stub_directive_assigns stub.stub_directive_range man flow in
      (* Evaluate the body of the stub *)
      let flow = exec_body stub.stub_directive_body None range man flow in
      (* Clean locals *)
      let flow = clean_post stub.stub_directive_locals stub.stub_directive_range man flow in
      (* Clean assignments *)
      let flow = clean_all_assigns stub.stub_directive_assigns stub.stub_directive_range man flow in
      Post.return flow


  (** Normalize a requirement condition by adding missing otherwise decorations *)
  let rec normalize_requirement_condition cond =
    match ekind cond with
    | E_stub_otherwise _ ->
      cond

    | E_binop(O_log_and, e1, e2) ->
      let e1' = normalize_requirement_condition e1 in
      let e2' = normalize_requirement_condition e2 in
      mk_log_and e1' e2' cond.erange

    | E_binop(O_log_or, e1, e2) ->
      let e1' = normalize_requirement_condition e1 in
      let e2' = normalize_requirement_condition e2 in
      mk_log_or e1' e2' cond.erange


    | E_stub_quantified_formula(quants, {ekind = E_stub_otherwise(qcond, alarm)}) ->
      mk_stub_otherwise (mk_stub_quantified_formula quants qcond cond.erange) alarm cond.erange

    | E_stub_if(c,e1,e2) ->
      let e1' = normalize_requirement_condition e1 in
      let e2' = normalize_requirement_condition e2 in
      { cond with ekind = E_stub_if(c,e1',e2') }

    | _ ->
      mk_stub_otherwise cond None cond.erange


  (** Check a stub requirement *)
  let exec_requires cond range man flow =
    (* Normalize the condition so that all sub-conditions are decorated with an
       adequate `otherwise` expression *)
    let cond' = normalize_requirement_condition cond in
    (* Evaluate the condition. Note that the evaluation of otherwise expression
       is responsible for raising the alarm if a condition is not satisified. *)
    man.eval cond' flow >>$ fun r flow ->
    match ekind r with
    | E_constant (C_bool true)  -> Post.return flow
    | E_constant (C_bool false) -> Flow.remove T_cur flow |>
                                   Post.return
    | _ -> assert false


  let exec stmt man flow =
    match skind stmt with
    | S_stub_directive (stub) ->
      exec_directive stub stmt.srange man flow |>
      OptionExt.return

    | S_stub_requires cond ->
      exec_requires cond stmt.srange man flow |>
      OptionExt.return

    | _ -> None


  (** Handler of queries *)
  (** ================== *)

  let ask query man flow = None


  (** Pretty printer *)
  (** ============== *)

  let print_expr man flow printer exp = ()

end

let () =
  register_stateless_domain (module Domain)
OCaml

Innovation. Community. Security.