Repository navigation
Expand file tree
/
Copy pathMemory.lean
More file actions
338 lines (293 loc) · 15.9 KB
/
Copy pathMemory.lean
File metadata and controls
338 lines (293 loc) · 15.9 KB
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
import CIL.TypeExpr
/-!
# Allocation-aware memory
The storage model of the checked interpreter (`CIL/Safety/Execution.lean`). Memory is a set
of allocations, each with a kind, a layout and a liveness flag, plus byte cells and the
views that grant read or write authority. A reference is an allocation identity and an
offset; it must first *form* (live allocation, in-bounds or sentinel offset) and then pass
an *access* check (bounds, alignment, authority, no GC slots) before bytes move. Every
failure is a `Fault`, never undefined behaviour.
This layer is independent of application types and of the extracted program. Its checked
operations are not yet a production certificate.
-/
namespace CIL.Safety
/-- The identity of an allocation; identities are never reused. -/
abbrev AllocationId := Nat
/-- The runtime type an object allocation records: its exact class identity and the admitted
classes it derives from, nearest first, up to but excluding `System.Object`. An array of
class references records its array type `E[]` and no bases. -/
structure RuntimeType where
identity : CIL.Extraction.TypeExpr
bases : List CIL.Extraction.TypeExpr
deriving DecidableEq, Repr
/-- The classes an instance of `type` is an instance of: its own class, then its bases. -/
def RuntimeType.lineage (type : RuntimeType) : List CIL.Extraction.TypeExpr :=
type.identity :: type.bases
/-- An instance is an instance of its own class. -/
@[simp] theorem RuntimeType.identity_mem_lineage (type : RuntimeType) :
type.identity ∈ type.lineage := List.mem_cons_self
/-- Where an allocation lives; only an immutable static may never be written. An object
allocation is a class instance of its recorded runtime type. -/
inductive StorageKind where
| managedHeap | callerStack | frame (activation : Nat) | immutableStatic
| object (type : RuntimeType)
deriving DecidableEq, Repr
/-- The byte size, alignment and GC reference slots of an allocation. -/
structure Layout where
size : Nat
alignment : Nat
/-- GC-sensitive fields can occur elsewhere in a larger containing object.
Only accesses intersecting these byte intervals are unsupported. -/
referenceSlots : List (Nat × Nat) := []
deriving DecidableEq, Repr
/-- One allocation: its storage kind, layout, whether it is still live, and the end
positions that may be formed as sentinels. -/
structure Allocation where
kind : StorageKind
layout : Layout
live : Bool := true
/-- Only specified field/array end positions may be used as sentinels. -/
sentinels : List Nat := []
deriving DecidableEq, Repr
/-- A byte position inside an allocation. -/
structure Reference where
allocation : AllocationId
offset : Nat
deriving DecidableEq, Repr
/-- A managed reference: `null` or the address of a byte in an allocation. -/
inductive ManagedReference where
| null | address (reference : Reference)
deriving DecidableEq, Repr
/-- One byte of memory and whether it has been initialized. -/
structure Cell where
bits : BitVec 8
initialized : Bool
deriving DecidableEq, Repr
/-- Read and/or write authority over `length` bytes of an allocation from `start`. -/
structure View where
allocation : AllocationId
start : Nat
length : Nat
readable : Bool
writable : Bool
deriving DecidableEq, Repr
/-- The whole checked heap: allocation table, byte cells, granted views, the next fresh
identity, and the references bound to loaded static fields and to string literals. -/
structure Memory where
allocations : AllocationId → Option Allocation
cells : AllocationId → Nat → Cell
views : List View
/-- Never decreases; allocation identities are not recycled when frames return. -/
nextIdentity : Nat
/-- Loaded assembly fields retain identity independently of their contents. -/
staticBindings : List (Nat × Reference) := []
/-- Admitted dynamic-stack bytes per activation; deployment must justify this quota.
Zero is the default, so existing memory grants no dynamic stack storage. -/
stackLimit : Nat := 0
/-- Bytes still available for object allocations; deployment must justify it, and each
allocation consumes its size. Zero is the default, so existing memory grants none. -/
heapBudget : Nat := 0
/-- The object reference each reference-typed field of a class instance holds, by allocation
and field byte offset. No field's bytes encode a reference; zeroed storage holds null. -/
slots : AllocationId → Nat → ManagedReference := fun _ _ => .null
/-- The interned string object `ldstr` loads for each literal text, bound by the caller and
never allocated here. One text loads one object (ECMA-335 III.4.16); nothing assumes that
distinct texts load distinct objects, or that a literal differs from a caller's string. -/
literals : List (String × Reference) := []
/-- Caller memory with no allocations, views or static bindings. It describes a caller that
passes only scalars, so it grants no storage a method could read or write. -/
def Memory.empty : Memory where
allocations := fun _ => none
cells := fun _ _ => ⟨0, false⟩
views := []
nextIdentity := 0
/-- Why a memory operation was rejected. -/
inductive Fault where
| nullDereference | missingAllocation | expiredLifetime | invalidReference
| outsideAllocation | unreadable | unwritable | uninitialized
| unsupportedLayout | alignment | identityReuse
| malformedAllocation
deriving DecidableEq, Repr
/-- A memory operation that either succeeds or reports a `Fault`. -/
abbrev Checked := Except Fault
/-- One past the largest native (64-bit) offset or size. -/
def nativeLimit : Nat := 2^64
/-- An allocation fits the native address space, has a positive alignment, and its
sentinels and reference slots lie within its size. -/
def Allocation.WellFormed (a : Allocation) : Prop :=
a.layout.size < nativeLimit ∧ 0 < a.layout.alignment ∧
(∀ offset ∈ a.sentinels, offset ≤ a.layout.size) ∧
∀ slot ∈ a.layout.referenceSlots, slot.1 + slot.2 ≤ a.layout.size
/-- Decidable form of `Allocation.WellFormed`, checked by `allocate`. -/
def Allocation.valid (a : Allocation) : Bool :=
decide (a.layout.size < nativeLimit) && decide (0 < a.layout.alignment) &&
a.sentinels.all (fun offset => decide (offset ≤ a.layout.size)) &&
a.layout.referenceSlots.all (fun slot => decide (slot.1 + slot.2 ≤ a.layout.size))
/-- The invariants every reachable memory keeps: allocations, views and field references name
existing, well-formed storage. -/
structure Memory.WellFormed (m : Memory) : Prop where
/-- Every allocation has an identity below `nextIdentity` and is well formed. -/
allocations : ∀ id a, m.allocations id = some a → id < m.nextIdentity ∧ a.WellFormed
/-- Every reference a field holds names an identity already issued, so no later allocation
can make a dangling reference live. -/
slots : ∀ id offset reference, m.slots id offset = .address reference →
reference.allocation < m.nextIdentity
/-- Every view lies inside an existing allocation. -/
views : ∀ view ∈ m.views, ∃ a, m.allocations view.allocation = some a ∧
view.start + view.length ≤ a.layout.size
/-- Memory with no allocations or views is well formed. -/
theorem Memory.empty_wellFormed : Memory.empty.WellFormed where
allocations _ _ absent := nomatch absent
views _ member := nomatch member
slots _ _ _ held := nomatch held
#print axioms Memory.empty_wellFormed
/-- The allocation `id`, failing if it is missing or has expired. -/
def liveAllocation (m : Memory) (id : AllocationId) : Checked Allocation := do
let some a := m.allocations id | .error .missingAllocation
if a.live then return a else .error .expiredLifetime
/-- An offset may be formed if it is strictly inside the allocation, or is a declared
sentinel at most its size. -/
def validPosition (a : Allocation) (offset : Nat) : Bool :=
decide (offset < nativeLimit) &&
(decide (offset < a.layout.size) ||
(decide (offset ≤ a.layout.size) && a.sentinels.contains offset))
/-- Formation checks do not require access permission or initialized bytes. -/
def form (m : Memory) (r : Reference) : Checked Reference := do
let a ← liveAllocation m r.allocation
if validPosition a r.offset then return r else .error .invalidReference
/-- `null` or an address that forms. -/
def validManagedReference (m : Memory) : ManagedReference → Bool
| .null => true
| .address r => match form m r with | .ok _ => true | .error _ => false
/-- Offset multiplication and addition wrap at native width. A wrapped result
retains its allocation identity and must pass formation validity anew. -/
def add (m : Memory) (r : Reference) (elementSize : Nat) (offset : BitVec 64) :
Checked Reference := do
let _ ← form m r
form m { r with offset :=
(BitVec.ofNat 64 r.offset + BitVec.ofNat 64 elementSize * offset).toNat }
/-- The byte at `r` lies inside `view`. -/
def viewContains (view : View) (r : Reference) : Bool :=
decide (view.allocation = r.allocation ∧ view.start ≤ r.offset ∧
r.offset < view.start + view.length)
/-- Some view covering the byte at `r` grants the requested read or write authority. -/
def permitted (m : Memory) (write : Bool) (r : Reference) : Bool :=
m.views.any fun view => viewContains view r && (if write then view.writable else view.readable)
/-- Access bounds and API authority are distinct from formation validity. The
required alignment is supplied by the particular instruction, not its width. -/
def access (m : Memory) (r : Reference) (width alignment : Nat) (write : Bool) :
Checked Unit := do
let _ ← form m r
let a ← liveAllocation m r.allocation
if a.layout.referenceSlots.any (fun slot =>
decide (r.offset < slot.1 + slot.2 ∧ slot.1 < r.offset + width))
then .error .unsupportedLayout else pure ()
if r.offset + width ≤ a.layout.size then pure () else .error .outsideAllocation
if alignment > 0 && a.layout.alignment % alignment == 0 && r.offset % alignment == 0
then pure () else .error .alignment
if write && a.kind == .immutableStatic then .error .unwritable else pure ()
if (List.range width).all (fun i => permitted m write { r with offset := r.offset + i })
then pure () else .error (if write then .unwritable else .unreadable)
/-- Reads `width` bytes at `r`; every byte must be readable and initialized. -/
def read (m : Memory) (r : Reference) (width alignment : Nat) : Checked (List (BitVec 8)) := do
access m r width alignment false
let cells := (List.range width).map fun i => m.cells r.allocation (r.offset + i)
if cells.all (·.initialized) then return cells.map (·.bits) else .error .uninitialized
/-- Reads through a managed reference; `null` faults with `nullDereference`. -/
def dereference (m : Memory) (reference : ManagedReference) (width alignment : Nat) :
Checked (List (BitVec 8)) :=
match reference with
| .null => .error .nullDereference
| .address r => read m r width alignment
/-- A covering readable view is the common fast path; fragmented authority may
also cover every byte. Empty writes need no additional authority. -/
def readableRange (m : Memory) (r : Reference) (width : Nat) : Bool :=
width == 0 ||
m.views.any (fun view => view.readable && decide
(view.allocation = r.allocation ∧ view.start ≤ r.offset ∧
r.offset + width ≤ view.start + view.length)) ||
(List.range width).all (fun i => permitted m false { r with offset := r.offset + i })
/-- The views after writing `width` bytes at `r`: a read-only view of the written bytes
is added unless they are already readable. -/
def writeViews (m : Memory) (r : Reference) (width : Nat) : List View :=
if readableRange m r width then m.views
else ⟨r.allocation, r.offset, width, true, false⟩ :: m.views
/-- Reference implementation retained to state and prove optimization equivalence. -/
def writeAlwaysPrepend (m : Memory) (r : Reference) (bytes : List (BitVec 8)) (alignment : Nat) :
Checked Memory := do
access m r bytes.length alignment true
-- A method may read its own initialized output. This grants no write authority
-- and exposes exactly the interval whose original write authority was checked.
return { m with
views := ⟨r.allocation, r.offset, bytes.length, true, false⟩ :: m.views
cells := (fun id offset =>
if id = r.allocation ∧ r.offset ≤ offset then
(match bytes[offset - r.offset]? with
| some bits => { bits, initialized := true }
| none => m.cells id offset)
else m.cells id offset) }
/-- Writes `bytes` at `r` after a write access check, marking them initialized and
making them readable. -/
def write (m : Memory) (r : Reference) (bytes : List (BitVec 8)) (alignment : Nat) :
Checked Memory := do
access m r bytes.length alignment true
-- A method may read its own initialized output. This grants no write authority
-- and exposes exactly the interval whose original write authority was checked.
return { m with
views := writeViews m r bytes.length
cells := (fun id offset =>
if id = r.allocation ∧ r.offset ≤ offset then
(match bytes[offset - r.offset]? with
| some bits => { bits, initialized := true }
| none => m.cells id offset)
else m.cells id offset) }
/-- `m` with the reference field at `field` holding `value`; bytes, authority and allocations
are unchanged. `storeSlot` checks the field and the value before calling it. -/
def setSlot (m : Memory) (field : Reference) (value : ManagedReference) : Memory :=
{ m with slots := fun id offset =>
if id = field.allocation then if offset = field.offset then value else m.slots id offset
else m.slots id offset }
/-- A checked byte write with constant-time lookup in its retained snapshot. -/
def writeIndexed (memory : Memory) (reference : Reference) (bytes : List (BitVec 8))
(alignment : Nat) : Checked Memory := do
access memory reference bytes.length alignment true
let snapshot := bytes.toArray
return { memory with
views := writeViews memory reference bytes.length
cells := fun id offset =>
if id = reference.allocation ∧ reference.offset ≤ offset then
match snapshot[offset - reference.offset]? with
| some bits => { bits, initialized := true }
| none => memory.cells id offset
else memory.cells id offset }
/-- Indexed snapshots retain exactly the checked write semantics. -/
theorem writeIndexed_eq (memory : Memory) (reference : Reference)
(bytes : List (BitVec 8)) (alignment : Nat) :
writeIndexed memory reference bytes alignment = write memory reference bytes alignment := by
simp only [writeIndexed, write, List.getElem?_toArray]
#print axioms writeIndexed_eq
/-- A snapshot is obtained before any destination change, preserving overlap. -/
def copy (m : Memory) (source target : Reference) (width : Nat) : Checked Memory := do
let bytes ← read m source width 1
write m target bytes 1
/-- SkipInit does not initialize or erase anything. -/
def skipInit (m : Memory) (r : Reference) : Checked Memory := do
let _ ← form m r
return m
/-- Marks allocation `id` as no longer live; its identity stays taken. -/
def expire (m : Memory) (id : AllocationId) : Memory :=
{ m with allocations := fun other =>
if other = id then (m.allocations other).map fun a => { a with live := false }
else m.allocations other }
/-- Fresh activation storage has unknown bytes, even if a physical stack slot
held initialized data in an earlier activation. -/
def allocate (m : Memory) (a : Allocation) : Checked (AllocationId × Memory) :=
if !a.valid then .error .malformedAllocation else
if (m.allocations m.nextIdentity).isSome then .error .identityReuse else
.ok (m.nextIdentity, { m with
allocations := fun id => if id = m.nextIdentity then some a else m.allocations id
cells := fun id offset => if id = m.nextIdentity then { bits := 0, initialized := false }
else m.cells id offset
slots := fun id offset => if id = m.nextIdentity then .null else m.slots id offset
nextIdentity := m.nextIdentity + 1 })
end CIL.Safety