package frama-c

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

Source file TacBittest.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
(**************************************************************************)
(*                                                                        *)
(*  This file is part of WP plug-in of Frama-C.                           *)
(*                                                                        *)
(*  Copyright (C) 2007-2024                                               *)
(*    CEA (Commissariat a l'energie atomique et aux energies              *)
(*         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 Lang

(* -------------------------------------------------------------------------- *)
(* --- Helpers                                                            --- *)
(* -------------------------------------------------------------------------- *)

let positive e = F.p_leq F.e_zero e (* 0 <= n *)
let power k = F.e_bigint (Integer.two_power_of_int k)

let lookup_int e =
  let open Qed.Logic in
  match F.repr e with
  | Kint z -> (try Some (Integer.to_int_exn z) with Z.Overflow -> None)
  | _ -> None

let rec lookup_bittest e =
  match F.repr e with
  | Not e -> lookup_bittest e
  | Fun(f,[n;ek]) when List.memq f Cint.f_bits ->
    begin
      match lookup_int ek with
      | Some k when 0 <= k && k < 128 -> Some (n,k)
      | _ -> None
    end
  | _ -> None

(* -------------------------------------------------------------------------- *)
(* --- Bit-Test Range                                                     --- *)
(* -------------------------------------------------------------------------- *)

class bittestrange =
  object
    inherit Tactical.make
        ~id:"Wp.bittestrange"
        ~title:"Bit-Test Range"
        ~descr:"Compute bounds with respect to bits."
        ~params:[]

    method select _feedback selection =
      let e = Tactical.selected selection in
      match lookup_bittest e with
      | Some (n,k) ->
        let bit = Cint.bit_test n k in
        let bit_set = F.p_bool bit in
        let bit_clear = F.p_not bit_set in
        let pos = positive n in
        let pk = power k in
        let pk1 = power (succ k) in
        let g_inf = F.p_hyps [pos] (F.p_leq pk n) in
        let g_sup = F.p_hyps [pos;F.p_lt n pk1] (F.p_lt n pk) in
        let name_inf = Printf.sprintf "Bit #%d (inf)" k in
        let name_sup = Printf.sprintf "Bit #%d (sup)" k in
        let at = Tactical.at selection in
        Tactical.Applicable (Tactical.insert ?at [
            name_inf , F.p_and bit_set g_inf ;
            name_sup , F.p_and bit_clear g_sup ;
          ])
      | None -> Tactical.Not_applicable

  end

let tactical = Tactical.export (new bittestrange)
let strategy = Strategy.make tactical ~arguments:[]

(* -------------------------------------------------------------------------- *)
(* --- Auto Bitrange                                                      --- *)
(* -------------------------------------------------------------------------- *)

let rec lookup push step e =
  match F.repr e with
  | And es -> List.iter (lookup push step) es
  | Or es -> List.iter (lookup push step) es
  | Imply (hs,p) -> List.iter (lookup push step) (p::hs)
  | _ ->
    begin
      match lookup_bittest e with
      | None -> ()
      | Some _ ->
        push @@ strategy ~priority:0.3 (Tactical.Inside(step,e))
    end

class autobittestrange : Strategy.heuristic =
  object

    method id = "wp:bittestrange"
    method title = "Auto Bit-Test Range"
    method descr = "Apply bitwise tactics on bit-tests expressions."

    method search push (seq : Conditions.sequent) =
      Conditions.iter
        (fun step ->
           let p = Conditions.head step |> F.e_prop in
           lookup push (Tactical.Step step) p
        ) (fst seq) ;
      let p = snd seq in
      lookup push (Tactical.Goal p) (F.e_prop p)

  end

let () = Strategy.register (new autobittestrange)
OCaml

Innovation. Community. Security.