forked from kframework/c-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymloc.k
361 lines (292 loc) · 15.3 KB
/
symloc.k
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
module C-SYMLOC-SYNTAX
imports C-TYPING-SYNTAX
syntax SymBase ::= base(SymLoc) [function]
syntax Int ::= offset(SymLoc) [function]
// Allocated duration.
syntax Duration ::= "alloc"
// Static duration.
syntax Duration ::= static(String)
syntax Duration ::= link(String)
syntax SymBase ::= string(String)
syntax SymBase ::= wstring(List)
// Thread local duration.
syntax Duration ::= thread(Int)
// Auto storage duration.
syntax Duration ::= auto(Int)
syntax Duration ::= "argv"
// We strip storage class specifiers when processing declarations, so this
// is how we keep track of register-ness.
syntax Duration ::= register(Int)
syntax Duration ::= "nativeAddress"
syntax Duration ::= nativeSymbol(String)
syntax Bool ::= isAllocatedDuration(SymLoc) [function]
// I.e., static or link.
syntax Bool ::= isStaticDuration(SymLoc) [function]
syntax Bool ::= isThreadDuration(SymLoc) [function]
syntax Bool ::= isAutoDuration(SymLoc) [function]
syntax Bool ::= isStringLoc(SymLoc) [function]
syntax Bool ::= isLinkerLoc(SymLoc) [function]
syntax Bool ::= isLinkerLoc(SymBase) [function, klabel('isLinkerLoc2)]
syntax Bool ::= isArgvLoc(SymLoc) [function]
syntax Bool ::= isRegisterLoc(SymLoc) [function]
syntax Bool ::= isNativeLoc(SymLoc) [function]
syntax SymBase ::= autoToRegister(SymBase) [function]
syntax SymBase ::= Int "@" Duration
// Base, byte offset, bit offset.
syntax SymLoc ::= loc(SymBase, Int)
syntax SymLoc ::= CanonicalSymLoc
syntax CanonicalSymLoc ::= loc(SymBase, Int, Set) [klabel(loc3)]
syntax CanonicalSymLoc ::= "NullPointer"
syntax KResult ::= SymLoc
syntax SymBase ::= linc(SymBase) [function]
// Base, type
syntax SymLoc ::= lnew(SymBase, Type) [function]
syntax SymLoc ::= restrictedNew(SymBase, Type, Scope) [function]
syntax SymLoc ::= SymLoc "+bytes" Int [function]
syntax Bool ::= SymLoc ">bytes" SymLoc [function]
| SymLoc ">=bytes" SymLoc [function]
| SymLoc "<bytes" SymLoc [function]
| SymLoc "<=bytes" SymLoc [function]
| SymLoc "==bytes" SymLoc [function]
syntax Bool ::= sameBase(SymLoc, SymLoc) [function]
syntax String ::= getTU(SymLoc) [function]
syntax ThreadId ::= getThreadId(SymLoc) [function]
syntax ThreadId ::= Int | "no-thread"
syntax Int ::= getAlign(SymLoc) [function]
syntax Int ::= getBitOffset(SymLoc) [function]
syntax Int ::= getNativeAlign(Int) [function]
syntax Set ::= prov(SymLoc) [function]
//TODO: why it is nessary here
syntax Set ::= getBases(SymLoc) [function, klabel(getBases)]
syntax Set ::= getBases(Set) [function]
syntax SymLoc ::= addProv(Provenance, SymLoc) [function]
syntax SymLoc ::= addProv(Set, SymLoc) [function]
// From, to.
syntax KItem ::= transferProv(SymLoc, K) [strict(2)]
syntax KItem ::= transferProv(Set, K) [strict(2)]
// Pointers from an array need to be checked that they don't go out of
// bounds of that array, even if that array happens to be part of a larger
// object (i.e., surrounded by valid addresses).
syntax Provenance ::= fromArray(Int, Int)
// Used for detecting violations of aliasing restictions on
// restrict-qualified pointers.
syntax Provenance ::= basedOn(SymBase, Scope)
// Used to tag union members so the rest of the bytes in the union can be
// marked as undefined upon write.
syntax Provenance ::= fromUnion(SymLoc, CId, FieldInfo)
// Used to tag lvalues with their type so that their alignment can be computed if needed
syntax Provenance ::= fromType(Type)
// used to tag bit fields so that we know how many bits into the current byte they begin
syntax Provenance ::= bitOffset(Int)
syntax Bool ::= hasUnionMarker(SymLoc) [function]
syntax Bool ::= isFromArray(SymLoc) [function]
syntax Bool ::= ifFromArrayInBounds(SymLoc, Type, Int) [function]
syntax SymLoc ::= stripBases(SymLoc) [function]
syntax SymLoc ::= stripFromArray(SymLoc) [function]
syntax SymLoc ::= stripProv(SymLoc) [function]
endmodule
module C-SYMLOC
imports C-BITSIZE-SYNTAX
imports C-DYNAMIC-SYNTAX
imports C-SETTINGS-SYNTAX
imports C-SYMLOC-SYNTAX
imports K-REFLECTION
imports C-CONFIGURATION
imports COMPAT-SYNTAX
rule isAllocatedDuration(loc(_ @ alloc, _)) => true
rule isAllocatedDuration(loc(_ @ alloc, _, _)) => true
rule isAllocatedDuration(_) => false [owise]
rule isStaticDuration(loc(_ @ static(_), _)) => true
rule isStaticDuration(loc(_ @ static(_), _, _)) => true
rule isStaticDuration(loc(_ @ link(_), _)) => true
rule isStaticDuration(loc(_ @ link(_), _, _)) => true
rule isStaticDuration(loc(string(_), _)) => true
rule isStaticDuration(loc(string(_), _, _)) => true
rule isStaticDuration(loc(wstring(_), _)) => true
rule isStaticDuration(loc(wstring(_), _, _)) => true
rule isStaticDuration(_) => false [owise]
rule isThreadDuration(loc(_ @ thread(_), _)) => true
rule isThreadDuration(loc(_ @ thread(_), _, _)) => true
rule isThreadDuration(_) => false [owise]
rule isAutoDuration(loc(_ @ auto(_), _)) => true
rule isAutoDuration(loc(_ @ auto(_), _, _)) => true
rule isAutoDuration(loc(_ @ argv, _)) => true
rule isAutoDuration(loc(_ @ argv, _, _)) => true
rule isAutoDuration(loc(_ @ register(_), _)) => true
rule isAutoDuration(loc(_ @ register(_), _, _)) => true
rule isAutoDuration(_) => false [owise]
rule isStringLoc(loc(string(_), _)) => true
rule isStringLoc(loc(string(_), _, _)) => true
rule isStringLoc(loc(wstring(_), _)) => true
rule isStringLoc(loc(wstring(_), _, _)) => true
rule isStringLoc(_) => false [owise]
rule isLinkerLoc(_ @ link(_)) => true
rule isLinkerLoc(_:SymBase) => false [owise]
rule isLinkerLoc(Loc:SymLoc) => true
requires isLinkerLoc(base(Loc))
rule isLinkerLoc(_:SymLoc) => false [owise]
rule isArgvLoc(loc(_ @ argv, _)) => true
rule isArgvLoc(loc(_ @ argv, _, _)) => true
rule isArgvLoc(_) => false [owise]
rule isRegisterLoc(loc(_ @ register(_), _)) => true
rule isRegisterLoc(loc(_ @ register(_), _, _)) => true
rule isRegisterLoc(_) => false [owise]
rule isNativeLoc(loc(_ @ nativeAddress, _)) => true
rule isNativeLoc(loc(_ @ nativeAddress, _, _)) => true
rule isNativeLoc(loc(_ @ nativeSymbol(_), _)) => true
rule isNativeLoc(loc(_ @ nativeSymbol(_), _, _)) => true
rule isNativeLoc(_) => false [owise]
rule autoToRegister(N:Int @ auto(Th:Int)) => N @ register(Th)
rule base(loc(Base:SymBase, _)) => Base
rule base(loc(Base:SymBase, _, _)) => Base
rule offset(loc(_, Offset:Int)) => Offset
rule offset(loc(_, Offset:Int, _)) => Offset
rule linc(X:Int @ D:Duration) => (X +Int 1 @ D)
rule lnew(Base:SymBase, T:Type)
=> loc(Base, 0, SetItem(fromType(T)))
rule restrictedNew(Base:SymBase, T:Type, Tag:Scope)
=> addProv(basedOn(Base, Tag), lnew(Base, T))
rule loc(Base:SymBase, Offset:Int) +bytes N:Int
=> loc(Base, Offset +Int N)
rule loc(Base:SymBase, Offset:Int, Prov:Set) +bytes N:Int
=> loc(Base, Offset +Int N, Prov)
rule NullPointer +bytes _ => NullPointer
rule loc(_, A:Int) >bytes loc(_, B:Int) => A >Int B
rule loc(_, A:Int) >=bytes loc(_, B:Int) => A >=Int B
rule loc(_, A:Int) <bytes loc(_, B:Int) => A <Int B
rule loc(_, A:Int) <=bytes loc(_, B:Int) => A <=Int B
rule loc(_, A:Int) ==bytes loc(_, B:Int) => A ==Int B
rule loc(_, A:Int, _) >bytes loc(_, B:Int, _) => A >Int B
rule loc(_, A:Int, _) >=bytes loc(_, B:Int, _) => A >=Int B
rule loc(_, A:Int, _) <bytes loc(_, B:Int, _) => A <Int B
rule loc(_, A:Int, _) <=bytes loc(_, B:Int, _) => A <=Int B
rule loc(_, A:Int, _) ==bytes loc(_, B:Int, _) => A ==Int B
rule sameBase(loc(A:SymBase, _), loc(B:SymBase, _)) => A ==K B
rule sameBase(loc(A:SymBase, _, _), loc(B:SymBase, _, _)) => A ==K B
rule sameBase(loc(A:SymBase, _), loc(B:SymBase, _, _)) => A ==K B
rule sameBase(loc(A:SymBase, _, _), loc(B:SymBase, _)) => A ==K B
rule getTU(loc(_ @ static(Tu:String), _)) => Tu
rule getTU(loc(_ @ static(Tu:String), _, _)) => Tu
rule getTU(loc(_ @ link(Tu:String), _)) => Tu
rule getTU(loc(_ @ link(Tu:String), _, _)) => Tu
rule getThreadId(loc(_ @ thread(Th:Int), _)) => Th
rule getThreadId(loc(_ @ thread(Th:Int), _, _)) => Th
rule getThreadId(loc(_ @ auto(Th:Int), _)) => Th
rule getThreadId(loc(_ @ auto(Th:Int), _, _)) => Th
rule getThreadId(loc(_ @ register(Th:Int), _)) => Th
rule getThreadId(loc(_ @ register(Th:Int), _, _)) => Th
rule getThreadId(loc(_ @ argv, _)) => 0
rule getThreadId(loc(_ @ argv, _, _)) => 0
rule getThreadId(_) => no-thread [owise]
rule addProv(P:Provenance, loc(Base:SymBase, Offset:Int, Prov:Set))
=> loc(Base, Offset, Prov SetItem(P))
rule addProv(P:Provenance, loc(Base:SymBase, Offset:Int))
=> loc(Base, Offset, SetItem(P))
rule addProv(_:Provenance, NullPointer) => NullPointer
rule addProv(Prov':Set, loc(Base:SymBase, Offset:Int, Prov:Set))
=> loc(Base, Offset, Prov Prov')
rule addProv(Prov':Set, loc(Base:SymBase, Offset:Int))
=> loc(Base, Offset, Prov')
rule addProv(_:Set, NullPointer) => NullPointer
rule prov(loc(_, _, Prov:Set)) => Prov
rule prov(loc(_, _)) => .Set
rule prov(NullPointer) => .Set
rule getBases(Loc:SymLoc) => getBases(prov(Loc))
rule getBases(Prov:Set) => filterSet(Prov, #klabel(`isBase`))
syntax Bool ::= isBase(Provenance) [function]
rule isBase(basedOn(_, _)) => true
rule isBase(_) => false [owise]
rule transferProv(From:SymLoc, tv(To:SymLoc, T:Type))
=> tv(addProv(prov(From), To), T)
[structural]
rule transferProv(_:SymLoc, tv(V:CValue, T:Type)) => tv(V, T)
requires isSymLoc(V:CValue) =/=K true
[structural]
rule transferProv(Prov:Set, tv(Loc:SymLoc, T:Type))
=> tv(addProv(Prov, Loc), T)
[structural]
rule transferProv(_:Set, tv(V:CValue, T:Type)) => tv(V, T)
requires isSymLoc(V:CValue) =/=K true
[structural]
rule transferProv(From:SymLoc, nclv(To:SymLoc, T:Type))
=> lv(addProv(prov(From), To), T)
[structural]
rule transferProv(Prov:Set, nclv(Loc:SymLoc, T:Type))
=> lv(addProv(Prov, Loc), T)
[structural]
rule hasUnionMarker(loc(Base:SymBase, Offset:Int, SetItem(K:K) S:Set))
=> getKLabel(K) ==K #klabel(`fromUnion`)
orBool hasUnionMarker(loc(Base, Offset, S))
rule hasUnionMarker(_) => false [owise]
rule isFromArray(Loc:SymLoc) => #isFromArray(Loc)
syntax Bool ::= #isFromArray(SymLoc) [function]
rule #isFromArray(loc(Base:SymBase, Offset:Int, SetItem(K:K) S:Set))
=> getKLabel(K) ==K #klabel(`fromArray`) orBool #isFromArray(loc(Base, Offset, S))
rule #isFromArray(_) => false [owise]
rule stripBases(loc(Base:SymBase, Offset:Int)) => loc(Base, Offset)
rule stripBases(loc(Base:SymBase, Offset:Int, Prov:Set))
=> loc(Base, Offset, filterSet(Prov, #klabel(`notBase`)))
rule stripBases(Loc:SymLoc) => Loc [owise]
syntax Bool ::= notBase(Provenance) [function]
rule notBase(basedOn(_, _)) => false
rule notBase(_) => true [owise]
rule stripFromArray(loc(Base:SymBase, Offset:Int)) => loc(Base, Offset)
rule stripFromArray(loc(Base:SymBase, Offset:Int, Prov:Set))
=> loc(Base, Offset, filterSet(Prov, #klabel(`notFromArray`)))
rule stripFromArray(Loc:SymLoc) => Loc [owise]
syntax Bool ::= notFromArray(Provenance) [function]
rule notFromArray(fromArray(_, _)) => false
rule notFromArray(_) => true [owise]
rule stripProv(loc(Base:SymBase, Offset:Int, _)) => loc(Base, Offset)
rule stripProv(loc(Base:SymBase, Offset:Int)) => loc(Base, Offset)
rule stripProv(Loc:SymLoc) => Loc [owise]
rule getAlign(loc(Base:SymBase, Offset:Int, Provs:Set)) => getAlign'(.K, Provs, Base, #configuration)
requires notBool isNativeLoc(loc(Base, Offset, Provs))
rule getAlign(NullPointer) => cfg:alignofMalloc
rule getAlign(loc(0 @ nativeAddress, Offset:Int, _)) => getNativeAlign(Offset)
rule getAlign(loc(0 @ nativeAddress, Offset:Int)) => getNativeAlign(Offset)
rule getAlign(_) => 1 [owise]
syntax Int ::= "getAlign'" "(" K "," Set "," SymBase "," K ")" [function]
rule getAlign'(.K, .Set, _, _) => 1
rule getAlign'(.K, SetItem(K:K) S:Set, B:SymBase, C:K) => getAlign'(K, S, B, C)
rule getAlign'(fromType(T:Type), _, _, _) => getAlignas(T)
requires isCompleteType(T)
rule getAlign'(fromType(T:Type), _, (Base:SymBase => NewBase), <generatedTop>... <linkings>... Base |-> NewBase:SymBase ...</linkings> ...</generatedTop>)
requires notBool isCompleteType(T)
rule getAlign'(fromType(T:Type), _, Base:SymBase, <generatedTop>... <mem>... Base |-> object(DefT:Type, _, _, _) ...</mem> <linkings> Linkings:Map </linkings> ...</generatedTop>)
=> getAlignas(DefT)
requires notBool isCompleteType(T) andBool notBool (Base in_keys(Linkings))
rule getAlign'(K:KItem, S:Set, B:SymBase, C:K) => getAlign'(.K, S, B, C)
requires getKLabel(K) =/=K #klabel(`fromType`)
syntax Int ::= #getBitOffset(K, Set) [function]
rule getBitOffset(loc(_, _, Provs:Set)) => #getBitOffset(.K, Provs)
rule #getBitOffset(.K, SetItem(K:K) S:Set) => #getBitOffset(K, S)
rule #getBitOffset(bitOffset(I:Int), _) => I
rule #getBitOffset(K:KItem, S:Set) => #getBitOffset(.K, S)
requires getKLabel(K) =/=K #klabel(`bitOffset`)
rule ifFromArrayInBounds(
loc(_, _, SetItem(K:K) S:Set),
T:Type, I:Int)
=> #ifFromArrayInBounds(K, S, T, I)
rule ifFromArrayInBounds(Loc:SymLoc, _, _) => true
requires notBool isFromArray(Loc)
syntax Bool ::= #ifFromArrayInBounds(K, Set, Type, Int) [function]
rule #ifFromArrayInBounds(fromArray(Offset:Int, Len:Int), _, T:Type, I:Int)
=> true
requires (Offset +Int (I *Int byteSizeofType(innerType(T))) >=Int 0)
andBool (Offset +Int (I *Int byteSizeofType(innerType(T))) <=Int Len)
rule #ifFromArrayInBounds(fromArray(Offset:Int, Len:Int), _, T:Type, I:Int)
=> false
requires (Offset +Int (I *Int byteSizeofType(innerType(T))) <Int 0)
orBool (Offset +Int (I *Int byteSizeofType(innerType(T))) >Int Len)
rule #ifFromArrayInBounds(K:K, .Set, _, _) => true
requires getKLabel(K) =/=K #klabel(`fromArray`)
rule #ifFromArrayInBounds(K:K, SetItem(K':K) S:Set, T:Type, I:Int)
=> #ifFromArrayInBounds(K', S, T, I)
requires getKLabel(K) =/=K #klabel(`fromArray`)
syntax Int ::= #getNativeAlign(Int, Int, Int) [function]
rule getNativeAlign(Address:Int) => #getNativeAlign(Address, 1, 0)
rule #getNativeAlign(Address:Int, Accum:Int, I:Int) => #getNativeAlign(Address /Int 2, Accum *Int 2, I +Int 1)
requires Address %Int 2 ==Int 0 andBool I <Int cfg:ptrsize *Int cfg:bitsPerByte
rule #getNativeAlign(_, Accum:Int, _) => Accum [owise]
endmodule