-
Notifications
You must be signed in to change notification settings - Fork 8
/
x86-memory.k
319 lines (265 loc) · 9.93 KB
/
x86-memory.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
require "x86-configuration.k"
require "x86-mint-wrapper.k"
require "x86-abstract-syntax.k"
module X86-MEMORY-SORT
syntax MemLoadValue
endmodule
module X86-MEMORY-SYNTAX
imports X86-MEMORY-SORT
imports X86-ASBTRACT-SORTS
imports MINT-WRAPPER-SORT
imports MINT
imports INT
syntax Int ::= "StackSpaceForEnvs"
/*@
Load Return Value Sort
*/
syntax MemLoadValue ::= memLoadValue ( MInt )
syntax Bool ::= isAddressAlignedAt(
/* alignment requirement in bits */ Int,
/* Address */ MInt) [function]
/*@
Allocate memory: Used for allocating stack and envionment memory
If N is the number of stack slots allocated, the upper 64 slots are used for
environment memory.
*/
syntax KItem ::= allocateStackMemory(
/* Number of elements */ Int,
/* Alignment in bits */ Int)
/*@
Allocate Bss memory and save allocated pointer
*/
syntax KItem ::= allocateBssMemory(
/* Number of elements */ Int,
/* Alignment in bits */ Int)
/*@
Allocate Data memory and save allocated pointer
*/
syntax KItem ::= allocateDataMemory(
/* Number of elements */ Int,
/* Alignment in bits */ Int)
/*@
Allocate memory: Used for allocating Read only memory
*/
syntax KItem ::= allocateROMemory(
/* Number of elements */ Int,
/* Alignment in bits */ Int)
/*@
Allocate memory: Used for allocating memory for stdout file IO
*/
syntax KItem ::= allocateSTDOUTMemory(
/* Number of elements */ Int,
/* Alignment in bits */ Int)
/*@
Allocate memory: Used for allocating local memory
*/
syntax KItem ::= allocateLocalMemory(
/* Number of elements */ Int,
/* Alignment in bits */ Int)
/*@
Free memory allocated for stack.
*/
//syntax KItem ::= "freeMemory" [token]
syntax KItem ::= freeMemory(PointerVal)
/*@
Load memory at Address represented by MInt for Int bits
*/
syntax KItem ::= loadFromMemory(
/* Address */ MInt,
/* Load size in bits */ Int)
/*@
Sorts to be stored in memory.
*/
syntax StoreVal ::= MInt | MIntMap
/*@
Store 'Val' in 'Nbits' of memory starting at address 'AddrMI'
*/
syntax KItem ::= storeToMemory(
/* Data */ StoreVal,
/* Address */ MInt,
/* Store size in bits*/ Int)
endmodule
module X86-MEMORY
imports COMMON-MEMORY-SYNTAX
imports X86-CONFIGURATION
rule StackSpaceForEnvs => 8 [macro]
/// Decomposition of a dynamic value into bytes
syntax ByteVal ::= byte(/* position, 0 being the LSB */ Int, MInt)
// AbstractValue sort defintion for x86 semantics
syntax AbstractValue ::= ByteVal
// x86 memory helper API implementation for allocation and store.
/*@
Create a MemValue out of a storeVal of size Nbits
*/
syntax MemValue ::= toMemValue(StoreVal, Int) [function]
rule toMemValue(V:StoreVal, N:Int) => mList(N /Int 8, 8, toMemData(V, N, .MemData))
/*@
Create a MemData out of a storeVal of size Nbits
*/
syntax MemData ::= toMemData(StoreVal, Int, MemData) [function, klabel(toMemDataMInt)]
/*@
MInt -> MemData
*/
rule toMemData(V:MInt, N:Int, Bytes:MemData) => toMemDataFromMInt(V, N, Bytes)
requires bitwidthMInt(V) ==Int N
syntax MemData ::= toMemDataFromMInt(MInt, Int, MemData) [function]
rule toMemDataFromMInt(MI:MInt, N:Int, Bytes:MemData) => appendMemData(Bytes, toBytes(N, MI))
/*@
MIntMap -> MemData
*/
rule toMemData(mimap(M):MIntMap, N:Int, Bytes:MemData) => toMemDataFromMIntMap(M, 0, size(M), Bytes)
requires bitwidthMInt({M[0]}:>MInt) *Int size(M) ==Int N
syntax MemData ::= toMemDataFromMIntMap(Map, Int, Int, MemData) [function]
rule toMemDataFromMIntMap(_:Map, N:Int, N, Bytes:MemData) => Bytes
rule toMemDataFromMIntMap(M:Map, I:Int, N:Int, Bytes:MemData) =>
toMemDataFromMIntMap(M, I +Int 1, N, toMemData({M[I]}:>MInt, bitwidthMInt({M[I]}:>MInt), Bytes))
requires I <Int N
/*@
toBytes(N:Int, MI:MInt)
Convert MI to
List{ byte(0, MI) byte(1, MI) ... byte(M-1, MI) } where M = N / 8
*/
syntax MemData ::= toBytes(Int, MInt) [function]
rule toBytes(N:Int, MI:MInt) => toBytesAux(N /Int 8, MI, .MemData)
syntax MemData ::= toBytesAux(Int, MInt, /* bytes accumulator */ MemData) [function]
rule toBytesAux(0 , _:MInt, Bytes:MemData) => Bytes
rule toBytesAux(N:Int, MI:MInt, Bytes:MemData)
=>
toBytesAux(N -Int 1, MI, byte(N -Int 1, MI) Bytes)
requires N >Int 0
// x86 memory helper API implementation for load.
syntax KItem ::= fromMemValue(K, Int) [strict(1)]
rule fromMemValue(mList(_:Int, 8, Bytes:MemData), N:Int) =>
memLoadValue ( fromBytes(takeMemData(N /Int 8, Bytes)) )
syntax MInt ::= fromBytes(MemData) [function]
rule fromBytes(byte(I:Int, MI:MInt) Rest) => extractMask(MI, 8, I *Int 8)
requires Rest ==K .MemData
rule fromBytes(byte(I:Int, MI:MInt) Rest:MemData) => concatenateMInt(fromBytes(Rest), extractMask(MI, 8, I *Int 8))
requires Rest =/=K .MemData
// x86 memory API implementation
/*@
Allocate stack memory: Used for allocating stack memory and saving the stack base address
N ::= Environment Memory (8 byte fake return address + upper StackSpaceForEnvs slots) +
Stack Memory (Rest)
* *
A: *----* <-- rsp, rbp
* * |
A+8: * * <-- argv |
* * v (High Adress)
*/
rule <k> allocateStackMemory(N:Int, A:Int) =>
saveAllocPointer(allocateLocalMemory(N, A)) ... </k>
//<regstate> RSMap => updateMap(RSMap,
// ("RSP" |-> mi(addrSize, N -Int StackSpaceForEnvs))
// ("RBP" |-> mi(addrSize, N -Int StackSpaceForEnvs)) )
//</regstate>
<argv> _ => mi(addrSize, N -Int StackSpaceForEnvs +Int 8) </argv>
<stackbase> stackBaseInfo(_:K , _:K => mi(addrSize, N -Int StackSpaceForEnvs)) </stackbase>
/*
<regstate> RSMap </regstate>
<argv> _ => mi(addrSize,
uvalueMInt({RSMap["RSP"]}:>MInt) +Int StackSpaceForEnvs // N
-Int StackSpaceForEnvs +Int 8) </argv>
<stackbase> stackBaseInfo(_:K , _:K => mi(addrSize,
uvalueMInt({RSMap["RSP"]}:>MInt) +Int StackSpaceForEnvs // N
-Int StackSpaceForEnvs))
</stackbase>
*/
/*@
Save the allocation id for later reads/writes/free.
*/
syntax KItem ::= saveAllocPointer(K) [strict(1)]
rule <k> saveAllocPointer(L:MemLoc) => .
... </k>
<stackbase> stackBaseInfo(_:K => L, BP:MInt) </stackbase>
<regstate> RSMap => updateMap(RSMap,
("RSP" |-> ptr(L, BP))
("RBP" |-> ptr(L, BP)) )
</regstate>
/*@
Allocate Bss memory and save allocated pointer
*/
rule <k> allocateBssMemory(N:Int, A:Int) =>
saveBssPointer(allocateLocalMemory(N, A)) ... </k>
<bssbase>
bssBaseInfo(_:K, _:K => mi(addrSize, 0), _:K => mi(addrSize, 0))
</bssbase>
syntax KItem ::= saveBssPointer(K) [strict(1)]
rule <k> saveBssPointer(L:MemLoc) => .
... </k>
<bssbase> bssBaseInfo(_:K => L, _:K, _:K) </bssbase>
/*@
Allocate Data memory and save allocated pointer
*/
rule <k> allocateDataMemory(N:Int, A:Int) =>
saveDataPointer(allocateLocalMemory(N, A)) ... </k>
<database>
dataBaseInfo(_:K, _:K => mi(addrSize, 0), _:K => mi(addrSize, 0))
</database>
syntax KItem ::= saveDataPointer(K) [strict(1)]
rule <k> saveDataPointer(L:MemLoc) => .
... </k>
<database> dataBaseInfo(_:K => L, _:K, _:K) </database>
/*@
Allocate Read Only memory and save allocated pointer
*/
rule <k> allocateROMemory(N:Int, A:Int) =>
saveROPointer(allocateLocalMemory(N, A)) ... </k>
<robase>
roBaseInfo(_:K, _:K => mi(addrSize, 0), _:K => mi(addrSize, 0))
</robase>
syntax KItem ::= saveROPointer(K) [strict(1)]
rule <k> saveROPointer(L:MemLoc) => .
... </k>
<robase> roBaseInfo(_:K => L, _:K, _:K) </robase>
/*@
Allocate memory for stdout file pointer
*/
rule <k> allocateSTDOUTMemory(N:Int, A:Int) =>
saveSTDOUTPointer(allocateLocalMemory(N, A)) ... </k>
syntax KItem ::= saveSTDOUTPointer(K) [strict(1)]
rule <k> saveSTDOUTPointer(L:MemLoc) =>
storeToMemory(mi(32, #stdout), ptr(L, mi(64,1)), 32)
... </k>
<functargets> FMap:Map => FMap[stdout <- ptr(L, mi(64,1))] </functargets>
/*@
Allocate memory: Used for allocating local memory
`makeUndefMIntMap(N)` creates N 1 byte undefMInt and return a Map storing them
in byte granularity, which are then store in memory.
*/
rule <k> allocateLocalMemory(N:Int, A:Int) =>
mAlloc(
toMemValue(makeUndefMIntMap(N), N *Int 8),
A)
... </k>
/*
rule <k> allocateLocalMemory(N:Int, A:Int) =>
mAlloc(
toMemValue(
makeUndefMIntMap(N, uvalueMInt({RSMap["RSP"]}:>MInt) +Int StackSpaceForEnvs -Int N),
N *Int 8), A)
... </k>
<regstate> RSMap </regstate>
*/
rule <k> freeMemory(PV:PointerVal) => mClear(getMemLoc(PV)) ... </k>
//<stackbase> stackBaseInfo( Loc:MemLoc, _:K) </stackbase>
/*@
Load memory Nbits:Int bits from Address represented AddrMI:MInt
*/
rule <k>
//loadFromMemory(AddrMI:MInt, Nbits:Int) =>
loadFromMemory(ptr(L:MemLoc, AddrMI:MInt), Nbits:Int) =>
fromMemValue(mRead(L, uvalueMInt(AddrMI) *Int 8, Nbits /Int 8, 8), Nbits)
... </k>
//<stackbase> stackBaseInfo( L:MemLoc, _:K) </stackbase>
requires Nbits %Int 8 ==Int 0
/*@
Store 'Val' in 'Nbits' of memory starting at address 'AddrMI'
*/
rule <k>
//storeToMemory(Val:StoreVal, AddrMI:MInt, Nbits:Int) => mWrite(L, uvalueMInt(AddrMI) *Int 8, toMemValue(Val, Nbits))
storeToMemory(Val:StoreVal, ptr(L:MemLoc, AddrMI:MInt), Nbits:Int) => mWrite(L, uvalueMInt(AddrMI) *Int 8, toMemValue(Val, Nbits))
... </k>
//<stackbase> stackBaseInfo( L:MemLoc, _:K) </stackbase>
requires Nbits %Int 8 ==Int 0
endmodule