-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathValidationInterop.lean
More file actions
277 lines (263 loc) · 12.1 KB
/
Copy pathValidationInterop.lean
File metadata and controls
277 lines (263 loc) · 12.1 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
/-
Compiler.CompilationModel.ValidationInterop: Interop profile validation
-/
import Compiler.CompilationModel.Types
import Compiler.CompilationModel.AbiHelpers
import Compiler.CompilationModel.IssueRefs
import Compiler.CompilationModel.SelectorInteropHelpers
namespace Compiler.CompilationModel
def lowLevelCallUnsupportedError (context : String) (name : String) : Except String Unit :=
throw s!"Compilation error: {context} uses unsupported low-level call '{name}' ({issue586Ref}). Use a verified linked external function wrapper instead of raw call/staticcall/delegatecall/callcode."
def interopBuiltinCallUnsupportedError (context : String) (name : String) : Except String Unit :=
throw s!"Compilation error: {context} uses unsupported interop builtin call '{name}' ({issue586Ref}). Use a verified linked external wrapper or pass the required value through explicit function parameters."
def unsupportedInteropCallError (context : String) (name : String) : Except String Unit :=
if isLowLevelCallName name then
lowLevelCallUnsupportedError context name
else
interopBuiltinCallUnsupportedError context name
mutual
def validateInteropExpr (context : String) : Expr → Except String Unit
| Expr.msgValue | Expr.selfBalance =>
pure ()
| Expr.call gas target value inOffset inSize outOffset outSize => do
validateInteropExpr context gas
validateInteropExpr context target
validateInteropExpr context value
validateInteropExpr context inOffset
validateInteropExpr context inSize
validateInteropExpr context outOffset
validateInteropExpr context outSize
| Expr.staticcall gas target inOffset inSize outOffset outSize => do
validateInteropExpr context gas
validateInteropExpr context target
validateInteropExpr context inOffset
validateInteropExpr context inSize
validateInteropExpr context outOffset
validateInteropExpr context outSize
| Expr.delegatecall gas target inOffset inSize outOffset outSize => do
validateInteropExpr context gas
validateInteropExpr context target
validateInteropExpr context inOffset
validateInteropExpr context inSize
validateInteropExpr context outOffset
validateInteropExpr context outSize
| Expr.contractAddress | Expr.chainid | Expr.blobbasefee =>
pure ()
| Expr.extcodesize addr =>
validateInteropExpr context addr
| Expr.calldatasize =>
pure ()
| Expr.calldataload offset =>
validateInteropExpr context offset
| Expr.mload offset =>
validateInteropExpr context offset
| Expr.tload offset =>
validateInteropExpr context offset
| Expr.keccak256 offset size => do
validateInteropExpr context offset
validateInteropExpr context size
| Expr.returndataSize =>
pure ()
| Expr.returndataOptionalBoolAt outOffset =>
validateInteropExpr context outOffset
| Expr.externalCall name args => do
if isInteropBuiltinCallName name then
unsupportedInteropCallError context name
validateInteropExprList context args
| Expr.mapping _ key => validateInteropExpr context key
| Expr.mappingWord _ key _ => validateInteropExpr context key
| Expr.mappingPackedWord _ key _ _ => validateInteropExpr context key
| Expr.mappingChain _ keys => validateInteropExprList context keys
| Expr.structMember _ key _ => validateInteropExpr context key
| Expr.mapping2 _ key1 key2 | Expr.mapping2Word _ key1 key2 _
| Expr.structMember2 _ key1 key2 _ => do
validateInteropExpr context key1
validateInteropExpr context key2
| Expr.mappingUint _ key => validateInteropExpr context key
| Expr.internalCall _ args =>
validateInteropExprList context args
| Expr.storageArrayElement _ index =>
validateInteropExpr context index
| Expr.arrayElement _ index | Expr.memoryArrayElement _ index
| Expr.arrayElementWord _ index _ _ | Expr.arrayElementDynamicWord _ index _
| Expr.arrayElementDynamicDataOffset _ index
| Expr.arrayElementDynamicMemberDataOffset _ index _
| Expr.arrayElementDynamicMemberLength _ index _ =>
validateInteropExpr context index
| Expr.arrayElementDynamicMemberElement _ index _ innerIndex => do
validateInteropExpr context index
validateInteropExpr context innerIndex
| Expr.paramDynamicMemberElement _ _ innerIndex =>
validateInteropExpr context innerIndex
| Expr.add a b | Expr.sub a b | Expr.mul a b | Expr.div a b | Expr.sdiv a b | Expr.mod a b | Expr.smod a b |
Expr.bitAnd a b | Expr.bitOr a b | Expr.bitXor a b | Expr.shl a b | Expr.shr a b |
Expr.sar a b | Expr.signextend a b | Expr.byte a b |
Expr.eq a b | Expr.ge a b | Expr.gt a b | Expr.sgt a b | Expr.lt a b | Expr.slt a b | Expr.le a b |
Expr.logicalAnd a b | Expr.logicalOr a b |
Expr.wMulDown a b | Expr.wDivUp a b | Expr.min a b | Expr.max a b |
Expr.ceilDiv a b => do
validateInteropExpr context a
validateInteropExpr context b
| Expr.intrinsic _ _ _ args =>
validateInteropExprList context args
| Expr.forkIfAtLeast _ thenExpr elseExpr => do
validateInteropExpr context thenExpr
validateInteropExpr context elseExpr
| Expr.mulDivDown a b c | Expr.mulDivUp a b c
| Expr.mulDiv512Down a b c | Expr.mulDiv512Up a b c => do
validateInteropExpr context a
validateInteropExpr context b
validateInteropExpr context c
| Expr.bitNot a | Expr.logicalNot a =>
validateInteropExpr context a
| Expr.ite cond thenVal elseVal => do
validateInteropExpr context cond
validateInteropExpr context thenVal
validateInteropExpr context elseVal
| Expr.adtConstruct _ _ args =>
validateInteropExprList context args
-- Pure leaves and constant scalars: nothing to validate. Listed explicitly
-- (rather than via `| _ => pure ()`) so the equation-lemma deriver does
-- not have to enumerate the complement of every pattern above. This avoids
-- the `_mutual.eq_def` 200 000-heartbeat ceiling when new `Expr`
-- constructors land (e.g. verity#1832's `paramDynamicHeadWord`).
| Expr.literal _ | Expr.param _ | Expr.constructorArg _
| Expr.storage _ | Expr.storageAddr _
| Expr.caller | Expr.blockTimestamp | Expr.blockNumber
| Expr.localVar _
| Expr.arrayLength _ | Expr.memoryArrayLength _ | Expr.storageArrayLength _
| Expr.paramDynamicHeadWord _ _
| Expr.paramDynamicStaticComposite _ _
| Expr.paramDynamicMemberLength _ _
| Expr.paramDynamicMemberDataOffset _ _
| Expr.dynamicBytesEq _ _
| Expr.adtTag _ _ | Expr.adtField _ _ _ _ _ =>
pure ()
termination_by e => sizeOf e
decreasing_by all_goals simp_wf; all_goals omega
def validateInteropExprList (context : String) : List Expr → Except String Unit
| [] => pure ()
| e :: es => do
validateInteropExpr context e
validateInteropExprList context es
termination_by es => sizeOf es
decreasing_by all_goals simp_wf; all_goals omega
def validateInteropStmt (context : String) : Stmt → Except String Unit
| Stmt.letVar _ value | Stmt.assignVar _ value | Stmt.setStorage _ value | Stmt.setStorageAddr _ value
| Stmt.setStorageWord _ _ value |
Stmt.storageArrayPush _ value |
Stmt.return value | Stmt.require value _ =>
validateInteropExpr context value
| Stmt.setStorageArrayElement _ index value => do
validateInteropExpr context index
validateInteropExpr context value
| Stmt.storageArrayPop _ =>
pure ()
| Stmt.requireError cond _ args => do
validateInteropExpr context cond
validateInteropExprList context args
| Stmt.revertError _ args =>
validateInteropExprList context args
| Stmt.mstore offset value => do
validateInteropExpr context offset
validateInteropExpr context value
| Stmt.tstore offset value => do
validateInteropExpr context offset
validateInteropExpr context value
| Stmt.calldatacopy destOffset sourceOffset size => do
validateInteropExpr context destOffset
validateInteropExpr context sourceOffset
validateInteropExpr context size
| Stmt.returndataCopy destOffset sourceOffset size => do
validateInteropExpr context destOffset
validateInteropExpr context sourceOffset
validateInteropExpr context size
| Stmt.revertReturndata =>
pure ()
| Stmt.setMapping _ key value | Stmt.setMappingWord _ key _ value | Stmt.setMappingPackedWord _ key _ _ value | Stmt.setMappingUint _ key value
| Stmt.setStructMember _ key _ value => do
validateInteropExpr context key
validateInteropExpr context value
| Stmt.setMappingChain _ keys value => do
validateInteropExprList context keys
validateInteropExpr context value
| Stmt.setMapping2 _ key1 key2 value | Stmt.setMapping2Word _ key1 key2 _ value
| Stmt.setStructMember2 _ key1 key2 _ value => do
validateInteropExpr context key1
validateInteropExpr context key2
validateInteropExpr context value
| Stmt.ite cond thenBranch elseBranch => do
validateInteropExpr context cond
validateInteropStmtList context thenBranch
validateInteropStmtList context elseBranch
| Stmt.forEach _ count body => do
validateInteropExpr context count
validateInteropStmtList context body
| Stmt.unsafeBlock _ body =>
validateInteropStmtList context body
| Stmt.matchAdt _ scrutinee branches => do
validateInteropExpr context scrutinee
validateInteropMatchBranches context branches
| Stmt.emit _ args =>
validateInteropExprList context args
| Stmt.internalCall _ args =>
validateInteropExprList context args
| Stmt.internalCallAssign _ _ args =>
validateInteropExprList context args
| Stmt.externalCallBind _ _ args =>
validateInteropExprList context args
| Stmt.tryExternalCallBind _ _ _ args =>
validateInteropExprList context args
| Stmt.returnValues values =>
validateInteropExprList context values
| Stmt.rawLog topics dataOffset dataSize => do
validateInteropExprList context topics
validateInteropExpr context dataOffset
validateInteropExpr context dataSize
| Stmt.ecm _ args =>
validateInteropExprList context args
| _ =>
pure ()
termination_by s => sizeOf s
decreasing_by all_goals simp_wf; all_goals omega
def validateInteropStmtList (context : String) : List Stmt → Except String Unit
| [] => pure ()
| s :: ss => do
validateInteropStmt context s
validateInteropStmtList context ss
termination_by ss => sizeOf ss
decreasing_by all_goals simp_wf; all_goals omega
def validateInteropMatchBranches (context : String) : List (String × List String × List Stmt) → Except String Unit
| [] => pure ()
| (_, _, body) :: rest => do
validateInteropStmtList context body
validateInteropMatchBranches context rest
termination_by bs => sizeOf bs
decreasing_by all_goals simp_wf; all_goals omega
end
def validateInteropFunctionSpec (spec : FunctionSpec) : Except String Unit := do
spec.body.forM (validateInteropStmt s!"function '{spec.name}'")
def validateInteropExternalSpec (spec : ExternalFunction) : Except String Unit := do
if isInteropBuiltinCallName spec.name then
unsupportedInteropCallError s!"external declaration '{spec.name}'" spec.name
def validateInteropConstructorSpec (ctor : Option ConstructorSpec) : Except String Unit := do
match ctor with
| none => pure ()
| some spec =>
spec.body.forM (validateInteropStmt "constructor")
def validateSpecialEntrypointSpec (spec : FunctionSpec) : Except String Unit := do
if !isInteropEntrypointName spec.name then
pure ()
else
if spec.isInternal then
throw s!"Compilation error: function '{spec.name}' cannot be marked internal ({issue586Ref})"
if !spec.params.isEmpty then
throw s!"Compilation error: function '{spec.name}' must not declare parameters ({issue586Ref})"
let returns ← functionReturns spec
if !returns.isEmpty then
throw s!"Compilation error: function '{spec.name}' must not return values ({issue586Ref})"
if spec.name == "receive" && !spec.isPayable then
throw s!"Compilation error: function 'receive' must be payable ({issue586Ref})"
if spec.isView || spec.isPure then
throw s!"Compilation error: function '{spec.name}' cannot be marked view/pure ({issue586Ref})"
end Compiler.CompilationModel