Skip to content

Commit b848acf

Browse files
[mlir] Add tests for Support struct types in abi.encode/abi.decode lowering
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
1 parent fd009f0 commit b848acf

File tree

5 files changed

+3731
-0
lines changed

5 files changed

+3731
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
contract C {
2+
struct Big {
3+
uint256 id;
4+
uint256 code;
5+
string note;
6+
uint256[2] fixedArr;
7+
uint256[] dynArr;
8+
}
9+
10+
struct Nested {
11+
uint256 salt;
12+
Big payload;
13+
uint256 extra;
14+
}
15+
16+
function mem_big(Big memory s) public pure returns (uint256, uint256, string memory, uint256[2] memory, uint256[] memory) {
17+
return (s.id, s.code, s.note, s.fixedArr, s.dynArr);
18+
}
19+
20+
function mem_nested(Nested memory s) public pure returns (uint256, uint256) {
21+
return (s.salt, s.extra);
22+
}
23+
24+
function cd_big(Big calldata s) public pure returns (uint256, uint256) {
25+
return (s.id, s.code);
26+
}
27+
28+
function cd_nested(Nested calldata s) public pure returns (uint256, uint256) {
29+
return (s.salt, s.extra);
30+
}
31+
}
32+
33+
// ====
34+
// compileViaMlir: true
35+
// ----
36+
// mem_big((uint256,uint256,string,uint256[2],uint256[])): 0x20, 7, 8, 0xc0, 41, 42, 0x100, 3, "abc", 3, 11, 12, 13 -> 7, 8, 0xc0, 41, 42, 0x100, 3, "abc", 3, 11, 12, 13
37+
// cd_big((uint256,uint256,string,uint256[2],uint256[])): 0x20, 7, 8, 0xc0, 41, 42, 0x100, 3, "abc", 3, 11, 12, 13 -> 7, 8
38+
// mem_nested((uint256,(uint256,uint256,string,uint256[2],uint256[]),uint256)): 0x20, 100, 0x60, 200, 5, 6, 0xc0, 21, 22, 0x100, 2, "xy", 2, 31, 32 -> 100, 200
39+
// cd_nested((uint256,(uint256,uint256,string,uint256[2],uint256[]),uint256)): 0x20, 100, 0x60, 200, 5, 6, 0xc0, 21, 22, 0x100, 2, "xy", 2, 31, 32 -> 100, 200
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
contract C {
2+
struct Big {
3+
uint256 id;
4+
uint256 code;
5+
string note;
6+
uint256[2] fixedArr;
7+
uint256[] dynArr;
8+
}
9+
10+
struct Nested {
11+
uint256 salt;
12+
Big payload;
13+
uint256 extra;
14+
}
15+
16+
struct StorageFlat {
17+
uint256 a;
18+
uint128 b;
19+
string note;
20+
}
21+
22+
struct StorageNested {
23+
uint256 left;
24+
StorageFlat inner;
25+
uint256 right;
26+
}
27+
28+
struct StoragePacked {
29+
uint128 a;
30+
uint64 b;
31+
bool c;
32+
uint256 d;
33+
}
34+
35+
// FIXME: Enable storage encoding tests with Big/Nested once storage-array
36+
// encoding is fixed.
37+
StorageFlat storageFlat;
38+
StorageNested storageNested;
39+
StoragePacked storagePacked;
40+
41+
constructor() {
42+
storageFlat.a = 9;
43+
storageFlat.b = 10;
44+
storageFlat.note = "stor";
45+
46+
storageNested.left = 100;
47+
storageNested.inner.a = 201;
48+
storageNested.inner.b = 202;
49+
storageNested.inner.note = "in";
50+
storageNested.right = 300;
51+
52+
storagePacked.a = 1;
53+
storagePacked.b = 2;
54+
storagePacked.c = true;
55+
storagePacked.d = 3;
56+
}
57+
58+
function mem_big(Big memory s) public pure returns (bytes memory) {
59+
return abi.encode(s);
60+
}
61+
62+
function mem_nested(Nested memory s) public pure returns (bytes memory) {
63+
return abi.encode(s);
64+
}
65+
66+
function cd_big(Big calldata s) public pure returns (bytes memory) {
67+
return abi.encode(s);
68+
}
69+
70+
function cd_nested(Nested calldata s) public pure returns (bytes memory) {
71+
return abi.encode(s);
72+
}
73+
74+
function storage_flat() public view returns (bytes memory) {
75+
return abi.encode(storageFlat);
76+
}
77+
78+
function storage_nested() public view returns (bytes memory) {
79+
return abi.encode(storageNested);
80+
}
81+
82+
function storage_packed() public view returns (bytes memory) {
83+
return abi.encode(storagePacked);
84+
}
85+
}
86+
87+
// ====
88+
// compileViaMlir: true
89+
// ----
90+
// constructor() ->
91+
// mem_big((uint256,uint256,string,uint256[2],uint256[])): 0x20, 7, 8, 0xc0, 41, 42, 0x100, 3, "abc", 3, 11, 12, 13 -> 0x20, 0x1a0, 0x20, 7, 8, 0xc0, 41, 42, 0x100, 3, "abc", 3, 11, 12, 13
92+
// cd_big((uint256,uint256,string,uint256[2],uint256[])): 0x20, 7, 8, 0xc0, 41, 42, 0x100, 3, "abc", 3, 11, 12, 13 -> 0x20, 0x1a0, 0x20, 7, 8, 0xc0, 41, 42, 0x100, 3, "abc", 3, 11, 12, 13
93+
// mem_nested((uint256,(uint256,uint256,string,uint256[2],uint256[]),uint256)): 0x20, 100, 0x60, 200, 5, 6, 0xc0, 21, 22, 0x100, 2, "xy", 2, 31, 32 -> 0x20, 0x1e0, 0x20, 100, 0x60, 200, 5, 6, 0xc0, 21, 22, 0x100, 2, "xy", 2, 31, 32
94+
// cd_nested((uint256,(uint256,uint256,string,uint256[2],uint256[]),uint256)): 0x20, 100, 0x60, 200, 5, 6, 0xc0, 21, 22, 0x100, 2, "xy", 2, 31, 32 -> 0x20, 0x1e0, 0x20, 100, 0x60, 200, 5, 6, 0xc0, 21, 22, 0x100, 2, "xy", 2, 31, 32
95+
// storage_flat() -> 0x20, 0xc0, 0x20, 9, 10, 0x60, 4, "stor"
96+
// storage_nested() -> 0x20, 288, 0x20, 0x64, 0x60, 300, 0xc9, 0xca, 0x60, 2, 47687202278368593055453199545051370742183790376672955679702151372887807754240
97+
// storage_packed() -> 0x20, 0x80, 1, 2, 1, 3

test/libsolidity/semanticTests/mlir/invalid-abi.sol

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
contract C {
2+
struct S {
3+
uint256 a;
4+
string b;
5+
}
6+
27
function f(uint[] memory a) public returns (uint[] memory) {
38
return a;
49
}
10+
511
function g(string memory a) public returns (string memory) {
612
return a;
713
}
14+
815
function h(uint[][] memory a) public returns (uint[][] memory) {
916
return a;
1017
}
18+
1119
function e() public returns (uint256) {
1220
bytes memory data = hex"00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000";
1321
abi.decode(data, (string[2]));
1422
return 1;
1523
}
24+
25+
function sc(S calldata s) external returns (uint256) {
26+
return s.a;
27+
}
28+
29+
function ec(S calldata s) external returns (bytes memory) {
30+
return abi.encode(s);
31+
}
32+
33+
function sm(S memory s) external returns (uint256) {
34+
return s.a;
35+
}
1636
}
1737

1838
// ====
@@ -25,3 +45,9 @@ contract C {
2545
// h(uint256[][]): 32, 1, 0x10000000000000000 -> FAILURE, hex"08c379a0", 0x20, 0x2b, "ABI decoding: invalid calldata a", "rray offset"
2646
// h(uint256[][]): 32, 1, 32, 3618502788666131106986593281521497120414687020801267626233049500247285301248 -> FAILURE, hex"4e487b71", 0x41
2747
// e() -> FAILURE, hex"08c379a0", 0x20, 0x2b, "ABI decoding: invalid calldata a", "rray stride"
48+
// sc((uint256,string)): 0x20, 1 -> FAILURE, hex"08c379a0", 0x20, 0x27, "ABI decoding: struct calldata to", "o short"
49+
// ec((uint256,string)): 0x20, 1, 0x100, 0x80 -> FAILURE, hex"08c379a0", 0x20, 0x1e, "Invalid calldata access offset"
50+
// ec((uint256,string)): 0x20, 1, 0x40, 0x10000000000000000 -> FAILURE, hex"08c379a0", 0x20, 0x1e, "Invalid calldata access length"
51+
// ec((uint256,string)): 0x20, 1, 0x40, 2 -> FAILURE, hex"08c379a0", 0x20, 0x1e, "Invalid calldata access stride"
52+
// sm((uint256,string)): 0x20, 1 -> FAILURE, hex"08c379a0", 0x20, 0x23, "ABI decoding: struct data too sh", "ort"
53+
// sm((uint256,string)): 0x20, 1, 0x10000000000000000 -> FAILURE, hex"08c379a0", 0x20, 0x23, "ABI decoding: invalid struct off", "set"

0 commit comments

Comments
 (0)