forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.rs
More file actions
395 lines (342 loc) · 13.8 KB
/
Copy pathruntime.rs
File metadata and controls
395 lines (342 loc) · 13.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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//! Tests for the Fe runtime
#![cfg(feature = "solc-backend")]
use fe_compiler::yul::runtime::functions;
use rstest::rstest;
use yultsur::*;
use fe_analyzer::namespace::types::{Base, FixedSize, Integer, Struct};
use fe_common::utils::keccak;
use fe_compiler_test_utils::*;
macro_rules! assert_eq {
($a:tt, $b:tt) => {
statement! {
(if (iszero((eq($a, $b)))) {
(revert(0, 0))
})
}
};
}
#[rstest(
reason,
case("foo"),
case("A very looooooooooooooong reason that consumes multiple words")
)]
fn test_revert_with_reason_string(reason: &str) {
let reason_id = format!(r#""{}""#, keccak::full(reason.as_bytes()));
with_executor(&|mut executor| {
Runtime::default()
.with_data(
vec![yul::Data { name: keccak::full(reason.as_bytes()), value: reason.to_owned() }]
)
.with_test_statements(
statements! {
(let reason := load_data_string((dataoffset([literal_expression! { (reason_id) }])), (datasize([literal_expression! { (reason_id) }]))))
(revert_with_reason_string(reason))
})
.execute(&mut executor)
.expect_revert()
.expect_revert_reason(reason);
})
}
#[test]
fn test_runtime_alloc_and_avail() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
(let a := avail())
(let b := alloc(5))
(let c := alloc(10))
(let d := avail())
[assert_eq!(b, a)]
[assert_eq!(c, (add(b, 5)))]
[assert_eq!(d, (add(c, 10)))]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_mcopys() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
(let a := 0x0111114211111111011111112342311101111112221151110111111111111111)
(let b := 0x0111111234111111011123411111111101112431111111110111111234411111)
(let c := 0x0111341111111111011111111123411101111123411111110111111234111111)
(let d := 0x0111112344111111011111145111111101111111111111110111111111111111)
(let e := 0x0111112344111111011111145111111101111111110000000000000000000000)
(mstore(100, a))
(mstore(132, b))
(mstore(164, c))
(mstore(196, d))
(mcopys(100, 42, 117))
[assert_eq!(a, (sload(42)))]
[assert_eq!(b, (sload(43)))]
[assert_eq!(c, (sload(44)))]
[assert_eq!(e, (sload(45)))]
(mcopys(100, 46, 128))
[assert_eq!(a, (sload(46)))]
[assert_eq!(b, (sload(47)))]
[assert_eq!(c, (sload(48)))]
[assert_eq!(d, (sload(49)))]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_scopym() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
(let a := 0x0111114211111111011111112342311101111112221151110111111111111111)
(let b := 0x0111111234111111011123411111111101112431111111110111111234411111)
(let c := 0x0111341111111111011111111123411101111123411111110111111234111111)
(let d := 0x0111112344111111011111145111111101111111111111110111111111111111)
(let e := 0x0111112344111111011111145111111101111111110000000000000000000000)
(sstore(42, a))
(sstore(43, b))
(sstore(44, c))
(sstore(45, d))
(let ptr1 := scopym(42, 117))
(let ptr2 := add(ptr1, 32))
(let ptr3 := add(ptr2, 32))
(let ptr4 := add(ptr3, 32))
[assert_eq!(a, (mload(ptr1)))]
[assert_eq!(b, (mload(ptr2)))]
[assert_eq!(c, (mload(ptr3)))]
[assert_eq!(e, (mload(ptr4)))]
(let ptr5 := scopym(42, 128))
(let ptr6 := add(ptr5, 32))
(let ptr7 := add(ptr6, 32))
(let ptr8 := add(ptr7, 32))
[assert_eq!(a, (mload(ptr5)))]
[assert_eq!(b, (mload(ptr6)))]
[assert_eq!(c, (mload(ptr7)))]
[assert_eq!(d, (mload(ptr8)))]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_mloadn() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
(let a := 0x4200000000000000000000000000000000000000000000000000000000420026)
(mstore(100, a))
[assert_eq!(0x42, (mloadn(100, 1)))]
[assert_eq!(0x420026, (mloadn(129, 3)))]
[assert_eq!(0x26, (mloadn(130, 2)))]
[assert_eq!(0x26, (mloadn(131, 1)))]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_storage_sanity() {
with_executor(&|mut executor| {
Runtime::new()
.with_test_statements(statements! {
(let a := 0x4200000000000000000000000000000000000000000000000000000000000026)
(let b := 0x9900000000000000000000000000000000000000000000000000000000000077)
(sstore(0, a))
(sstore(1, b))
[assert_eq!(a, (sload(0)))]
[assert_eq!(b, (sload(1)))]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_sloadn() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
(let a := 0x4200000000000000000000000000000000000000000000000000000000000026)
(let b := 0x9900530000003900000000000000000000000000000000000000000000000077)
(sstore(1000, a))
(sstore(1001, b))
[assert_eq!(a, (sloadn(1000, 0, 32)))]
[assert_eq!(b, (sloadn(1001, 0, 32)))]
[assert_eq!(0x42, (sloadn(1000, 0, 1)))]
[assert_eq!(0x26, (sloadn(1000, 31, 1)))]
[assert_eq!(0x4200, (sloadn(1000, 0, 2)))]
[assert_eq!(0x99, (sloadn(1001, 0, 1)))]
[assert_eq!(0x77, (sloadn(1001, 31, 1)))]
[assert_eq!(0x990053, (sloadn(1001, 0, 3)))]
[assert_eq!(0x5300000039, (sloadn(1001, 2, 5)))]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_sstoren() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
(let a := 0x0111111111111111011111111111111101111111111111110111111111111111)
// dashes indicate which bytes are to be replaced in this test
// 0----2 8----10 15----------------23 31--32
(let b := 0x4201111111111111123411111111119999999998998999110111111111111126)
(sstore(1000, a))
(sstoren(1000, 0, 2, 0x4201))
(sstoren(1000, 8, 2, 0x1234))
(sstoren(1000, 15, 8, 0x9999999998998999))
(sstoren(1000, 31, 1, 0x26))
[assert_eq!(b, (sload(1000)))]
(let c := 0x4242424242424242424242424242424242424242424242424242424242424242)
(sstoren(1000, 0, 32, c))
[assert_eq!(c, (sload(1000)))]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_ceil32() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
[assert_eq!(32, (ceil32(29)))]
[assert_eq!(256, (ceil32(225)))]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_ternary() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
(let a := ternary(0, 42, 26))
(let b := ternary(1, 42, 26))
[assert_eq!(a, 26)]
[assert_eq!(b, 42)]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_abi_unpack() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
(let a := 0x0042002600530000000000000000000000000000000000000000000000000000)
(let packed := alloc_mstoren(a, 32))
(let unpacked := avail())
(abi_unpack(packed, 3, 2))
(let elem0 := mload(unpacked))
(let elem1 := mload((add(unpacked, 32))))
(let elem2 := mload((add(unpacked, 64))))
[assert_eq!(elem0, 0x0042)]
[assert_eq!(elem1, 0x0026)]
[assert_eq!(elem2, 0x0053)]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_keccak256() {
with_executor(&|mut executor| {
Runtime::default().with_test_statements(
statements! {
(let num := 63806209331542711802848847270949280092855778197726125910674179583545433573378)
(let result :=109966633016701122630199943745061001312678661825260870342362413625737614346915)
// Can be validated with solidity
// uint(keccak256(abi.encodePacked(uint(63806209331542711802848847270949280092855778197726125910674179583545433573378))));
// which returns 109966633016701122630199943745061001312678661825260870342362413625737614346915
(mstore(0, num))
[assert_eq!(result, (keccak256(0, 32)))]
},
)
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_set_zero() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
(let a := 0x1111111111111111111111111111111111111111111111111111111111111111)
(let b := 0x1111110000000000000000000000000000000000000000000000001111111111)
(let c := 0x1111100000000000000000000000000000000000000000000000000000000000)
[assert_eq!(0, (set_zero(0, 256, a)))]
[assert_eq!(0x11, (set_zero(0, 248, a)))]
[assert_eq!(b, (set_zero(24, 216, a)))]
[assert_eq!(c, (set_zero(20, 256, a)))]
})
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn test_runtime_house_struct() {
let mut house = Struct::new("House");
house
.add_field("price", &FixedSize::Base(Base::Numeric(Integer::U256)))
.unwrap();
house
.add_field("size", &FixedSize::Base(Base::Numeric(Integer::U256)))
.unwrap();
house
.add_field("rooms", &FixedSize::Base(Base::Numeric(Integer::U8)))
.unwrap();
house.add_field("vacant", &FixedSize::bool()).unwrap();
let house_api = functions::structs::struct_apis(house);
with_executor(&|mut executor| {
Runtime::new().with_functions([functions::std(), house_api.clone()].concat()).with_test_statements(
statements! {
(let price := 42)
(let size := 26)
(let rooms := 5)
(let vacant := true)
(let house := struct_House_new(price, size, rooms, vacant))
// For now, all values in a struct occupy a full word. Here we test
// that the word at the given word offset contains the correct 32
// byte value.
//
// This test confirms that each value is being stored right-aligned
// and in the correct word.
[assert_eq!(price, (mload(house)))]
[assert_eq!(size, (mload((add(house, 32)))))]
[assert_eq!(rooms, (mload((add(house, 64)))))]
[assert_eq!(vacant, (mload((add(house, 96)))))]
// To retrieve an individual struct value, we need a pointer that
// references the start of the value.
[assert_eq!(price, (mloadn((struct_House_get_price_ptr(house)), 32)))]
[assert_eq!(size, (mloadn((struct_House_get_size_ptr(house)), 32)))]
[assert_eq!(rooms, (mloadn((struct_House_get_rooms_ptr(house)), 1)))]
[assert_eq!(vacant, (mloadn((struct_House_get_vacant_ptr(house)), 1)))]
// We test the same thing in storage.
// Note that the storage pointer for `house` is a multiple of 32.
(let house_storage := 2048)
(bytes_mcopys(house, house_storage, 128))
[assert_eq!(price, (bytes_sloadn((struct_House_get_price_ptr(house_storage)), 32)))]
[assert_eq!(size, (bytes_sloadn((struct_House_get_size_ptr(house_storage)), 32)))]
[assert_eq!(rooms, (bytes_sloadn((struct_House_get_rooms_ptr(house_storage)), 1)))]
[assert_eq!(vacant, (bytes_sloadn((struct_House_get_vacant_ptr(house_storage)), 1)))]
},
)
.execute(&mut executor)
.expect_success();
})
}
#[test]
fn checked_exp_signed() {
with_executor(&|mut executor| {
Runtime::default()
.with_test_statements(statements! {
[assert_eq!(4, (checked_exp_signed(2, 2, 0, 100)))]
})
.execute(&mut executor)
.expect_success();
})
}