Repository navigation
Expand file tree
/
Copy pathStaticMemory.lean
More file actions
123 lines (115 loc) · 6.8 KB
/
Copy pathStaticMemory.lean
File metadata and controls
123 lines (115 loc) · 6.8 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
import CIL.Safety.InstructionMemory
/-!
# Readonly statics in the checked interpreter
`staticReference` resolves an extracted readonly static field to immutable storage, creating
it on first use and revalidating a cached binding on every later use. An opaque field's value
is not extracted, so it must already be bound by the caller and is never created.
`staticInstruction` runs the static and span instructions on top of it and hands every other
memory instruction to `memoryInstruction`. `staticSpanAligned` is the element alignment a typed
static span needs.
-/
namespace CIL.Safety
/-- Decode a readonly field using its storage width and narrow signedness. -/
def staticLoadedValue (descriptor : CIL.StaticDescriptor) : Option CIL.Value :=
match descriptor.scalar with
| none => some (.aggregate descriptor.bytes)
| some signed =>
let number := byteNumber descriptor.bytes
match descriptor.bytes.length with
| 1 => some (.i32 (if signed then (BitVec.ofNat 8 number).signExtend 32
else BitVec.ofNat 32 number))
| 2 => some (.i32 (if signed then (BitVec.ofNat 16 number).signExtend 32
else BitVec.ofNat 32 number))
| 4 => some (.i32 (BitVec.ofNat 32 number))
| 8 => some (.i64 (BitVec.ofNat 64 number))
| _ => none
/-- Static initialization occurs before exposing the readonly view. Existing
bindings are checked against the freshly extracted field bytes, lifetime
and layout; cached contents are never trusted just because names match.
An opaque binding may hold any bytes of the field's width, and without one the
lookup faults: nothing supplies an opaque value except the caller's memory. -/
def staticReference (descriptor : CIL.StaticDescriptor) (m : Memory) :
ExecutionResult (Memory × Reference) := do
match m.staticBindings.find? (fun binding => binding.1 == descriptor.identity) with
| some (_, reference) =>
let allocation ← checkedAt (.address reference) descriptor.bytes.length
(liveAllocation m reference.allocation)
if allocation.kind == .immutableStatic && reference.offset == 0 &&
allocation.layout.size == descriptor.bytes.length &&
allocation.layout.alignment == descriptor.alignment then pure () else .error .invalidState
let bytes ← checkedAt (.address reference) descriptor.bytes.length
(read m reference descriptor.bytes.length 1)
if descriptor.opaqueValue || bytes == descriptor.bytes then return (m, reference)
else .error .invalidState
| none =>
if descriptor.opaqueValue then
.error (.unsupported "Opaque static field value is not bound by the caller")
let (id, memory) ← checkedAt .null descriptor.bytes.length (allocate m {
kind := .immutableStatic
layout := ⟨descriptor.bytes.length, descriptor.alignment, []⟩
sentinels := [descriptor.bytes.length] })
let reference : Reference := ⟨id, 0⟩
return ({ memory with
cells := fun other offset => if other == id then
match descriptor.bytes[offset]? with
| some byte => ⟨byte, true⟩
| none => ⟨0, false⟩
else memory.cells other offset
views := ⟨id, 0, descriptor.bytes.length, true, false⟩ :: memory.views
staticBindings := (descriptor.identity, reference) :: memory.staticBindings }, reference)
/-- CoreCLR's `CreateSpan<T>` throws unless the field data is aligned to `sizeof(T)`, so a typed
span needs `width` to divide the allocation's guaranteed alignment. -/
def staticSpanAligned (allocation : Allocation) (width : Nat) : Bool :=
0 < width && allocation.layout.alignment % width == 0
/-- Runs a memory instruction at `pc`. Static loads use the field identity extracted for that
pc; span constructors over raw pointers are accepted only inside extracted static data. -/
def staticInstruction (body : CIL.Method) (pc : Nat) (operation : CIL.MemoryOp)
(stack : List Value) (m : Memory) : ExecutionResult (Memory × List Value) := do
match operation, stack with
| .staticAddress bytes, _ =>
let some (_, descriptor) := body.staticSites.find? (fun site => site.1 == pc) |
.error (.unsupported "Missing extracted static field identity")
if descriptor.bytes == bytes then pure () else .error .invalidState
let (memory, reference) ← staticReference descriptor m
return (memory, .reference (.address reference) :: stack)
| .staticValue bytes, _ =>
let some (_, descriptor) := body.staticSites.find? (fun site => site.1 == pc) |
.error (.unsupported "Missing extracted static field identity")
-- An opaque value is whatever the caller's binding holds; `bytes` fixes only its width.
if descriptor.opaqueValue then
if descriptor.bytes == bytes then
let (memory, reference) ← staticReference descriptor m
let loaded ← checkedAt (.address reference) bytes.length
(read memory reference bytes.length 1)
let some value := staticLoadedValue { descriptor with bytes := loaded } |
.error .invalidState
return (memory, .scalar value :: stack)
else .error .invalidState
-- The translation certificate checks each initializer's result against these bytes.
else if descriptor.bytes == bytes && descriptor.initializer.isSome then
let some value := staticLoadedValue descriptor | .error .invalidState
return (m, .scalar value :: stack)
else .error .invalidState
| .spanCreate, .scalar (.i32 length) :: .reference (.address reference) :: rest =>
if length.toInt < 0 then .error .invalidState else pure ()
let allocation ← checkedAt (.address reference) length.toNat
(liveAllocation m reference.allocation)
if allocation.kind == .immutableStatic then pure () else
.error (.unsupported "Span pointer constructor outside extracted static data")
let _ ← formValue m (.address reference)
if reference.offset + length.toNat ≤ allocation.layout.size then pure () else
.error (.memory .outsideAllocation (.address reference) length.toNat)
return (m, .span (.address reference) length.toNat :: rest)
| .createSpan width, .reference (.address reference) :: rest =>
let allocation ← checkedAt (.address reference) 0 (liveAllocation m reference.allocation)
if allocation.kind == .immutableStatic && reference.offset == 0 && 0 < width &&
allocation.layout.size % width == 0 then
if staticSpanAligned allocation width then
return (m, .span (.address reference) (allocation.layout.size / width) :: rest)
else .error (.unsupported "Typed span element width does not divide the field alignment")
else .error (.unsupported "Typed span outside a whole extracted static field")
| .spanCreate, .scalar (.i32 length) :: .reference .null :: rest =>
if length == 0 then return (m, .span .null 0 :: rest)
else .error (.memory .nullDereference .null length.toNat)
| _, _ => memoryInstruction operation stack m
end CIL.Safety