Repository navigation
Expand file tree
/
Copy pathHeapObjects.lean
More file actions
145 lines (127 loc) · 7.11 KB
/
Copy pathHeapObjects.lean
File metadata and controls
145 lines (127 loc) · 7.11 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
import CIL.Safety.ArrayAllocation
/-!
# Heap objects
Class instances on the checked heap. An object reference is null or offset 0 of a live
allocation whose kind records the object's runtime type (`formObject`). It carries no byte
position, so code can neither observe nor forge one, and since allocation identities are never
reused, reference equality compares identities alone. `CIL/Safety/Execution.lean` uses these
observations for `ceq`, `beq`, `bne`, `brtrue`, `brfalse` and `cgt.un` against null, and
`allocateObject` for `newobj`, which charges each instance to `Memory.heapBudget`.
-/
namespace CIL.Safety
/-- Whether a formed object reference is non-null, as `brtrue` and `x != null` observe it. -/
def objectNonNull (memory : Memory) (reference : ManagedReference) : ExecutionResult Bool := do
let _ ← formObject memory reference
return reference != .null
/-- Reference equality of two formed object references: both null, or the same allocation. -/
def objectEqual (memory : Memory) (left right : ManagedReference) : ExecutionResult Bool := do
let _ ← formObject memory left
let _ ← formObject memory right
return left == right
/-- `cgt.un` on object references: defined only against null, where it tests presence. -/
def objectAboveNull (memory : Memory) (left right : ManagedReference) :
ExecutionResult Bool := do
match right with
| .null => objectNonNull memory left
| .address _ => .error .invalidState
/-- The runtime type the allocation of the live object `receiver` records. A reference to
anything but an object is ill-typed. -/
def runtimeTypeOf (memory : Memory) (receiver : Reference) : ExecutionResult RuntimeType := do
let allocation ← checkedAt (.address receiver) 0 (liveAllocation memory receiver.allocation)
match allocation.kind with
| .object type => .ok type
| _ => .error .invalidState
/-- The model fault of a virtual call on an instance of a class its dispatch table omits: the
class is outside the extraction's closed set, so the model selects no body for it. -/
def outsideDispatchTable : ExecutionFault :=
.unsupported "Virtual call on an instance of a class outside its dispatch table"
/-- The body a virtual call enters for the live object `receiver`: the entry of `targets` for
its exact runtime class, or `outsideDispatchTable`. -/
def virtualTarget (memory : Memory) (targets : List (CIL.Extraction.TypeExpr × Nat))
(receiver : Reference) : ExecutionResult Nat := do
let type ← runtimeTypeOf memory receiver
match targets.lookup type.identity with
| some callee => .ok callee
| none => .error outsideDispatchTable
/-- A selected body is one the table lists. -/
theorem virtualTarget_mem {memory : Memory} {targets : List (CIL.Extraction.TypeExpr × Nat)}
{receiver : Reference} {callee : Nat}
(selected : virtualTarget memory targets receiver = .ok callee) :
callee ∈ targets.map Prod.snd := by
unfold virtualTarget at selected
cases typed : runtimeTypeOf memory receiver with
| error fault => simp [typed, Bind.bind, Except.bind] at selected
| ok type =>
simp only [typed, Bind.bind, Except.bind] at selected
cases found : targets.lookup type.identity with
| none => simp [found] at selected
| some entry =>
simp only [found, Except.ok.injEq] at selected
subst selected
induction targets with
| nil => simp at found
| cons head tail ih =>
obtain ⟨identity, index⟩ := head
simp only [List.lookup_cons] at found
split at found
· cases found; exact List.mem_cons_self
· exact List.mem_cons_of_mem _ (ih found)
#print axioms virtualTarget_mem
/-- `castclass` (`throwing`) or `isinst` of class `type` on a formed object reference: null and
an object whose runtime lineage contains `type` pass unchanged; any other object becomes
null, or throws `System.InvalidCastException` for `castclass` (ECMA-335 III.4.3, III.4.6). -/
def objectTest (memory : Memory) (type : CIL.Extraction.TypeExpr) (throwing : Bool)
(reference : ManagedReference) : ExecutionResult ManagedReference := do
let _ ← formObject memory reference
match reference with
| .null => pure .null
| .address object =>
let runtime ← runtimeTypeOf memory object
if type ∈ runtime.lineage then pure reference
else if throwing then .error (.thrown "System.InvalidCastException")
else pure .null
/-- Object storage is aligned to the 8-byte field slots of the admitted 64-bit targets. -/
def objectAlignment : Nat := 8
/-- `memory` with a `size`-byte instance of `type` at its next identity: live, zeroed, fully
readable and writable, and charged to the heap budget. -/
def objectMemory (memory : Memory) (type : RuntimeType) (size : Nat) : Memory :=
{ memory with
allocations := fun identity => if identity = memory.nextIdentity then
some ⟨.object type, ⟨size, objectAlignment, []⟩, true, [size]⟩
else memory.allocations identity
cells := fun identity offset =>
if identity = memory.nextIdentity then ⟨0, true⟩ else memory.cells identity offset
slots := fun identity offset =>
if identity = memory.nextIdentity then .null else memory.slots identity offset
views := ⟨memory.nextIdentity, 0, size, true, true⟩ :: memory.views
nextIdentity := memory.nextIdentity + 1
heapBudget := memory.heapBudget - size }
/-- A fresh instance of `type` with `size` zeroed bytes and authority over all of them, or
`allocationFailure` when `size` exceeds the heap budget, which it otherwise consumes. No
frame owns the instance, so it lives as long as some reference to it can be observed. -/
def allocateObject (memory : Memory) (type : RuntimeType) (size : Nat) :
ExecutionResult (Reference × Memory) := do
if size > memory.heapBudget then
.error (.allocationFailure size memory.heapBudget) else pure ()
let (identity, allocated) ← checkedAt .null size
(allocate memory ⟨.object type, ⟨size, objectAlignment, []⟩, true, [size]⟩)
return (⟨identity, 0⟩,
{ arrayMemory allocated identity size with heapBudget := memory.heapBudget - size })
/-- The runtime type of an array of `element` references. System.Array and System.Object are
left out of its bases, as System.Object is left out of a class's. -/
def referenceArrayType (element : CIL.Extraction.TypeExpr) : RuntimeType :=
⟨.array element, []⟩
/-- `newarr` of a class: a fresh array of `length` null references, one 8-byte slot each, charged
to the heap budget like an instance of that size. A negative CLI length throws
`OverflowException`; one above the CoreCLR ceiling is outside the model, as for `newArray`. -/
def newReferenceArray (memory : Memory) (element : CIL.Extraction.TypeExpr)
(value : CIL.Value) : ExecutionResult (Reference × Memory) := do
let length ← match value with
| .i32 bits => pure bits.toInt
| .i64 bits => pure bits.toInt
| _ => .error .invalidState
if length < 0 then .error (.thrown "System.OverflowException") else
if length.toNat > maxArrayLength then
.error (.unsupported "Array length outside supported extent") else
allocateObject memory (referenceArrayType element) (objectFieldSlot * length.toNat)
end CIL.Safety