package frama-c

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

Source file MemMemory.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
(**************************************************************************)
(*                                                                        *)
(*  This file is part of WP plug-in of Frama-C.                           *)
(*                                                                        *)
(*  Copyright (C) 2007-2025                                               *)
(*    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).            *)
(*                                                                        *)
(**************************************************************************)

(* -------------------------------------------------------------------------- *)
(* --- Memory Model                                                       --- *)
(* -------------------------------------------------------------------------- *)

open Lang
open Lang.F

module L = Qed.Logic

let library = "memory"

let ty_fst_arg = function
  | Some l :: _ -> l
  | _ -> raise Not_found


let f_eqmem = Lang.extern_fp ~library "eqmem"
let f_memcpy = Lang.extern_f ~library ~typecheck:ty_fst_arg "memcpy"
let p_framed = Lang.extern_fp ~coloring:true ~library "framed" (* ptr-memory -> prop *)
let p_sconst = Lang.extern_fp ~coloring:true ~library "sconst" (* int-memory -> prop *)
let p_scinit = Lang.extern_fp ~coloring:true ~library "scinit" (* init-memory -> prop *)

(* -------------------------------------------------------------------------- *)
(* --- Utilities                                                          --- *)
(* -------------------------------------------------------------------------- *)

let t_malloc = L.Array(L.Int,L.Int)
let t_mem t = L.Array(MemAddr.t_addr,t)
let t_init = L.Array(MemAddr.t_addr,L.Bool)

let sconst memory = p_call p_sconst [ memory ]
let scinit memory = p_call p_scinit [ memory ]
let framed memory = p_call p_framed [ memory ]

(* -------------------------------------------------------------------------- *)
(* --- Simplifier for 'eqmem'                                             --- *)
(* -------------------------------------------------------------------------- *)

let r_eqmem = function
  | [_;_;_;n] when n = e_zero -> e_true
  | [m0;m1;p;n] when n = e_one -> e_eq (e_get m0 p) (e_get m1 p)
  | _ -> raise Not_found

(* -------------------------------------------------------------------------- *)
(* --- Simplifier for 'memcpy'                                            --- *)
(* -------------------------------------------------------------------------- *)

(* memcpy(m,q,m0,q0,n)[p] =
   - m[p] WHEN separated (p,1,q,n)
   - m0[q0 ++ p.offset - q.offset] WHEN not separated (p,1,q,n)
*)
let r_get_memcpy es ks =
  match es, ks with
  | [m;q;m0;q0;n],[p] ->
    begin
      match MemAddr.is_separated [p;e_one;q;n] with
      | L.Yes -> F.e_get m p
      | L.No ->
        if p == q then
          F.e_get m0 q0
        else
        if q == q0 then
          F.e_get m0 p
        else
          let i = MemAddr.offset p in
          let j = MemAddr.offset q in
          let q' = MemAddr.shift q0 (F.e_sub i j) in
          F.e_get m0 q'
      | _ -> raise Not_found
    end
  | _ -> raise Not_found

(* -------------------------------------------------------------------------- *)
(* --- Simplifiers Registration                                           --- *)
(* -------------------------------------------------------------------------- *)

let () = Context.register
    begin fun () ->
      F.set_builtin f_eqmem r_eqmem ;
      F.set_builtin_get f_memcpy r_get_memcpy ;
    end

(* -------------------------------------------------------------------------- *)
(* --- Unsupported Unions                                                 --- *)
(* -------------------------------------------------------------------------- *)

let wkey = Wp_parameters.register_warn_category "union"

let unsupported_union ~model (fd : Cil_types.fieldinfo) =
  if not fd.fcomp.cstruct then
    Wp_parameters.warning ~once:true ~wkey
      "Accessing union fields with %s model might be unsound.@\n\
       Please refer to WP manual." model

(* -------------------------------------------------------------------------- *)
OCaml

Innovation. Community. Security.