Source file code.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
open Cil_types
open Cil_datatype
open Memory
type scalar = { from : node option ; ptr : node option }
let integral = { from = None ; ptr = None }
let pointer m v =
match v.ptr with
| Some p -> v, p
| None ->
let p = new_chunk m () in
Option.iter (fun s -> Memory.add_points_to m s p) v.from ;
{ v with ptr = Some p }, p
let rec add_lval (m:map) (s:stmt) (lv:lval) : node =
let h = fst lv in
add_loffset m s (add_lhost m s h) (Cil.typeOfLhost h) (snd lv)
and add_lhost (m:map) (s:stmt) = function
| Var x -> Memory.add_root m x
| Mem e -> snd @@ pointer m @@ add_exp m s e
and add_loffset (m:map) (s:stmt) (r:node) (ty:typ)= function
| NoOffset -> r
| Field(fd,ofs) ->
add_loffset m s (add_field m r fd) fd.ftype ofs
| Index(e,ofs) ->
let elt = Ast_types.direct_element_type ty in
ignore @@ add_exp m s e ;
add_loffset m s (add_index m r elt) elt ofs
and add_value m s e = ignore (add_exp m s e)
and add_exp (m: map) (s:stmt) (e:exp) : scalar =
match e.enode with
| AddrOf lv | StartOf lv ->
let rv = Some (add_lval m s lv) in
{ from = rv ; ptr = rv }
| Lval lv ->
let rv = add_lval m s lv in
Memory.add_read m rv (Lval(s,lv)) ;
let ptr = Memory.add_value m rv @@ Cil.typeOfLval lv in
{ from = Some rv ; ptr }
| BinOp((PlusPI|MinusPI),p,k,_) ->
add_value m s k ;
let v = add_exp m s p in
Option.iter (fun r -> Memory.add_shift m r (Exp(s,e))) v.from ; v
| UnOp(_,e,_) ->
add_value m s e ; integral
| BinOp(_,a,b,_) ->
add_value m s a ; add_value m s b ; integral
| CastE(ty,p) ->
let v = add_exp m s p in
if Ast_types.is_ptr ty then
fst @@ pointer m @@ v
else
integral
| Const _
| SizeOf _ | SizeOfE _ | SizeOfStr _
| AlignOf _ | AlignOfE _
-> integral
let is_comp lv =
Ast_types.is_struct_or_union @@ Cil.typeOfLval lv
let rec add_init (m:map) (s:stmt) (acs:Access.acs) (lv:lval) (iv:init) =
match iv with
| SingleInit { enode = Lval le } when is_comp le ->
let r = add_lval m s lv in
let v = add_lval m s le in
Memory.merge m r v
| SingleInit e ->
let r = add_lval m s lv in
Memory.add_write m r acs ;
Option.iter (Memory.add_points_to m r) (add_exp m s e).ptr
| CompoundInit(_,fvs) ->
List.iter
(fun (ofs,iv) ->
let lv = Cil.addOffsetLval ofs lv in
add_init m s acs lv iv
) fvs
let add_instr (m:map) (s:stmt) (instr:instr) =
match instr with
| Skip _ | Code_annot _ -> ()
| Set(lv, { enode = Lval le }, _) when is_comp le ->
let r = add_lval m s lv in
let v = add_lval m s le in
Memory.merge m r v
| Set(lv,e,_) ->
let r = add_lval m s lv in
let v = add_exp m s e in
Memory.add_write m r (Lval(s,lv)) ;
Option.iter (Memory.add_points_to m r) v.ptr
| Local_init(x,AssignInit iv,_) ->
let acs = Access.Init(s,x) in
add_init m s acs (Var x,NoOffset) iv
| Call(lr,e,es,_) ->
add_value m s e ;
List.iter (add_value m s) es ;
Option.iter
(fun lv ->
let r = add_lval m s lv in
Memory.add_write m r (Lval(s,lv))
) lr ;
Options.warning ~source:(fst @@ Stmt.loc s) "Incomplete call analysis"
| Local_init(_,ConsInit _,_) ->
Options.warning ~source:(fst @@ Stmt.loc s)
"Constructor init not yet implemented"
| Asm _ ->
Options.warning ~source:(fst @@ Stmt.loc s)
"Inline assembly not supported (ignored)"
type rmap = Memory.map Stmt.Map.t ref
let store rmap m s =
rmap := Stmt.Map.add s (Memory.copy ~locked:true m) !rmap
let rec add_stmt (r:rmap) (m:map) (s:stmt) =
let annots = Annotations.code_annot s in
if annots <> [] then
Options.warning ~source:(fst @@ Stmt.loc s)
"Annotations not analyzed" ;
match s.skind with
| Instr ki -> add_instr m s ki ; store r m s
| Return(Some e,_) -> add_value m s e ; store r m s
| Goto _ | Break _ | Continue _ | Return(None,_) -> store r m s
| If(e,st,se,_) ->
add_value m s e ;
store r m s ;
add_block r m st ;
add_block r m se ;
| Switch(e,b,_,_) ->
add_value m s e ;
store r m s ;
add_block r m b ;
| Block b | Loop(_,b,_,_,_) -> add_block r m b
| UnspecifiedSequence s -> add_block r m @@ Cil.block_from_unspecified_sequence s
| Throw(exn,_) -> Option.iter (fun (e,_) -> add_value m s e) exn
| TryCatch(b,hs,_) ->
add_block r m b ;
List.iter (fun (c,b) -> add_catch r m c ; add_block r m b) hs ;
| TryExcept(a,(ks,e),b,_) ->
add_block r m a ;
List.iter (add_instr m s) ks ;
add_value m s e ;
add_block r m b ;
| TryFinally(a,b,_) ->
add_block r m a ;
add_block r m b ;
and add_catch (r:rmap) (m:map) (c:catch_binder) =
match c with
| Catch_all -> ()
| Catch_exn(_,xbs) -> List.iter (fun (_,b) -> add_block r m b) xbs
and add_block (r:rmap) (m:map) (b:block) =
List.iter (add_stmt r m) b.bstmts
type imap = Memory.map Property.Map.t ref
let istore imap m ip =
imap := Property.Map.add ip (Memory.copy ~locked:true m) !imap
let add_bhv ~kf (s:imap) (m:map) (bhv:behavior) =
List.iter
(fun e ->
let rs = Annot.of_extension e in
if rs <> [] then
begin
List.iter (Logic.add_region m) rs ;
let ip = Property.ip_of_extended (ELContract kf) e in
istore s m ip ;
end
) bhv.b_extended
type domain = {
map : map ;
body : map Stmt.Map.t ;
spec : map Property.Map.t ;
}
let domain ?global kf =
let m = match global with Some g -> g | None -> Memory.create () in
let r = ref Stmt.Map.empty in
let s = ref Property.Map.empty in
begin
try
let funspec = Annotations.funspec kf in
List.iter (add_bhv ~kf s m) funspec.spec_behavior ;
with Annotations.No_funspec _ -> ()
end ;
begin
try
let fundec = Kernel_function.get_definition kf in
add_block r m fundec.sbody ;
with Kernel_function.No_Definition -> ()
end ;
{
map = Memory.copy ~locked:true m ;
body = !r ;
spec = !s ;
}