Skip to content

Commit 9d6296e

Browse files
committed
[ewasm] Polyfill: calldataload & calldatacopy.
1 parent ed82426 commit 9d6296e

File tree

3 files changed

+143
-35
lines changed

3 files changed

+143
-35
lines changed

libyul/backends/wasm/polyfill/Interface.yul

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function callvalue() -> z1, z2, z3, z4 {
5555
}
5656

5757
function calldataload(x1, x2, x3, x4) -> z1, z2, z3, z4 {
58-
eth.callDataCopy(0:i32, u256_to_i32(x1, x2, x3, x4), 32:i32)
58+
calldatacopy(0, 0, 0, 0, x1, x2, x3, x4, 0, 0, 0, 32)
5959
z1, z2, z3, z4 := mload_internal(0:i32)
6060
}
6161

@@ -64,11 +64,31 @@ function calldatasize() -> z1, z2, z3, z4 {
6464
}
6565

6666
function calldatacopy(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4) {
67-
eth.callDataCopy(
68-
to_internal_i32ptr(x1, x2, x3, x4),
69-
u256_to_i32(y1, y2, y3, y4),
70-
u256_to_i32(z1, z2, z3, z4)
71-
)
67+
let cds:i32 := eth.getCallDataSize()
68+
let destination:i32 := u256_to_i32(x1, x2, x3, x4)
69+
let offset:i32 := u256_to_i32(y1, y2, y3, y4)
70+
let requested_size:i32 := u256_to_i32(z1, z2, z3, z4)
71+
// overflow?
72+
if i32.gt_u(offset, i32.sub(0xffffffff:i32, requested_size)) {
73+
eth.revert(0:i32, 0:i32)
74+
}
75+
76+
let available_size:i32 := i32.sub(cds, offset)
77+
if i32.gt_u(offset, cds) {
78+
available_size := 0:i32
79+
}
80+
81+
if i32.gt_u(available_size, 0:i32) {
82+
eth.callDataCopy(
83+
destination,
84+
offset,
85+
available_size
86+
)
87+
}
88+
89+
if i32.gt_u(requested_size, available_size) {
90+
memset(i32.add(destination, available_size), 0:i32, i32.sub(requested_size, available_size))
91+
}
7292
}
7393

7494
// Needed?

libyul/backends/wasm/polyfill/Memory.yul

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,10 @@ function pop(x1, x2, x3, x4) {
6060
function memoryguard(x:i64) -> y1, y2, y3, y4 {
6161
y4 := x
6262
}
63+
64+
function memset(ptr:i32, value:i32, length:i32) {
65+
for { let i:i32 := 0:i32 } i32.lt_u(i, length) { i := i32.add(i, 1:i32) }
66+
{
67+
i32.store8(i32.add(ptr, i), value)
68+
}
69+
}

test/cmdlineTests/evm_to_wasm_break/output

Lines changed: 110 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ object "object" {
4343
x_7 := x_11
4444
}
4545
{
46-
let _5, _6, _7, _8 := iszero_169_788(_1, _1, _1, lt_171(x_4, x_5, x_6, x_7, _1, _1, _1, 10))
46+
let _5, _6, _7, _8 := iszero_197_919_1528(_1, _1, _1, lt_199(x_4, x_5, x_6, x_7, _1, _1, _1, 10))
4747
if i32.eqz(i64.eqz(i64.or(i64.or(_5, _6), i64.or(_7, _8)))) { break }
4848
if i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, eq(x_4, x_5, x_6, x_7, _1, _1, _1, 2))))) { break }
4949
if i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, eq(x_4, x_5, x_6, x_7, _1, _1, _1, 4))))) { continue }
@@ -67,7 +67,7 @@ object "object" {
6767
let r1_1, carry_2 := add_carry(x1, y1, carry_1)
6868
r1 := r1_1
6969
}
70-
function iszero_169_788(x1, x2, x3, x4) -> r1, r2, r3, r4
70+
function iszero_197_919_1528(x1, x2, x3, x4) -> r1, r2, r3, r4
7171
{
7272
r4 := i64.extend_i32_u(i64.eqz(i64.or(i64.or(x1, x2), i64.or(x3, x4))))
7373
}
@@ -87,7 +87,7 @@ object "object" {
8787
case 1:i32 { r := 0xffffffff:i32 }
8888
default { r := i64.ne(a, b) }
8989
}
90-
function lt_171(x1, x2, x3, x4, y1, y2, y3, y4) -> z4
90+
function lt_199(x1, x2, x3, x4, y1, y2, y3, y4) -> z4
9191
{
9292
let z:i32 := false
9393
switch cmp(x1, y1)
@@ -106,6 +106,12 @@ object "object" {
106106
default { z := 1:i32 }
107107
z4 := i64.extend_i32_u(z)
108108
}
109+
function u256_to_i32(x1, x2, x3, x4) -> v:i32
110+
{
111+
if i64.ne(0, i64.or(i64.or(x1, x2), x3)) { unreachable() }
112+
if i64.ne(0, i64.shr_u(x4, 32)) { unreachable() }
113+
v := i32.wrap_i64(x4)
114+
}
109115
function bswap16(x) -> y
110116
{
111117
y := i64.or(i64.and(i64.shl(x, 8), 0xff00), i64.and(i64.shr_u(x, 8), 0xff))
@@ -122,13 +128,33 @@ object "object" {
122128
}
123129
function calldataload(x1, x2, x3, x4) -> z1, z2, z3, z4
124130
{
125-
if i64.ne(0, i64.or(i64.or(x1, x2), x3)) { unreachable() }
126-
if i64.ne(0, i64.shr_u(x4, 32)) { unreachable() }
127-
eth.callDataCopy(0:i32, i32.wrap_i64(x4), 32:i32)
128-
let z1_1 := bswap64(i64.load(0:i32))
129-
let z2_1 := bswap64(i64.load(i32.add(0:i32, 8:i32)))
130-
let z3_1 := bswap64(i64.load(i32.add(0:i32, 16:i32)))
131-
let z4_1 := bswap64(i64.load(i32.add(0:i32, 24:i32)))
131+
let cds:i32 := eth.getCallDataSize()
132+
let _1 := 0
133+
let destination:i32 := u256_to_i32(_1, _1, _1, _1)
134+
let offset:i32 := u256_to_i32(x1, x2, x3, x4)
135+
let requested_size:i32 := u256_to_i32(_1, _1, _1, 32)
136+
if i32.gt_u(offset, i32.sub(0xffffffff:i32, requested_size)) { eth.revert(0:i32, 0:i32) }
137+
let available_size:i32 := i32.sub(cds, offset)
138+
if i32.gt_u(offset, cds) { available_size := 0:i32 }
139+
let _2:i32 := 0:i32
140+
if i32.gt_u(available_size, _2)
141+
{
142+
eth.callDataCopy(destination, offset, available_size)
143+
}
144+
if i32.gt_u(requested_size, available_size)
145+
{
146+
let _3:i32 := i32.sub(requested_size, available_size)
147+
let _4:i32 := i32.add(destination, available_size)
148+
let i:i32 := _2
149+
for { } i32.lt_u(i, _3) { i := i32.add(i, 1:i32) }
150+
{
151+
i32.store8(i32.add(_4, i), _2)
152+
}
153+
}
154+
let z1_1 := bswap64(i64.load(_2))
155+
let z2_1 := bswap64(i64.load(i32.add(_2, 8:i32)))
156+
let z3_1 := bswap64(i64.load(i32.add(_2, 16:i32)))
157+
let z4_1 := bswap64(i64.load(i32.add(_2, 24:i32)))
132158
z1 := z1_1
133159
z2 := z2_1
134160
z3 := z3_1
@@ -152,11 +178,13 @@ object "object" {
152178

153179

154180
Binary representation:
155-
0061736d0100000001480a60000060017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f0002310208657468657265756d0c73746f7261676553746f7265000808657468657265756d0c63616c6c44617461436f70790009030e0d0003060406020601010104070505030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00020af0070da302030b7e017f087e02404200210002402000200020002000100c21012300210223012103230221040b20012105200221062003210720042108420121092000200084210a200a200020098484504545210b02400340200b45450d01024002402000200020002005200620072008200020002000420a10081005210c2300210d2301210e2302210f0b200c200d84200e200f8484504504400c030b200a20002005200620072008200020002000420210068484504504400c030b200a20002005200620072008200020002000420410068484504504400c010b0b024020052006200720082000200020002009100421102300211123012112230221130b201021052011210620122107201321080c000b0b20002000200020002005200620072008100e0b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1003210d2300210e0b200d210a024020012005200e1003210f230021100b200f2109024020002004201010032111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b2d01017e024020002004510440200120055104402002200651044020032007510440420121080b0b0b0b0b20080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b8a0102017e047f0240410021090240200020041007210a200a41004604400240200120051007210b200b41004604400240200220061007210c200c41004604402003200754210905200c41014604404100210905410121090b0b0b05200b41014604404100210905410121090b0b0b05200a41014604404100210905410121090b0b0b2009ad21080b20080b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100942108621022002200042108810098421010b20010b1e01027e02402000100a422086210220022000422088100a8421010b20010b7601087e024042002000200184200284520440000b42002003422088520440000b41002003a7412010014100290000100b2108410041086a290000100b2109410041106a290000100b210a410041186a290000100b210b2008210420092105200a2106200b21070b20052400200624012007240220040b3200024020002001100b370000200041086a2002100b370000200041106a2003100b370000200041186a2004100b3700000b0b2300024041002000200120022003100d41202004200520062007100d4100412010000b0b
181+
0061736d0100000001540c6000006000017f60017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60047e7e7e7e017f60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f00025e0408657468657265756d0c73746f7261676553746f7265000a08657468657265756d06726576657274000a08657468657265756d0f67657443616c6c4461746153697a65000108657468657265756d0c63616c6c44617461436f7079000b030f0e000408050803080602020205090705030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00040aa1090ea302030b7e017f087e02404200210002402000200020002000100f21012300210223012103230221040b20012105200221062003210720042108420121092000200084210a200a200020098484504545210b02400340200b45450d01024002402000200020002005200620072008200020002000420a100a1007210c2300210d2301210e2302210f0b200c200d84200e200f8484504504400c030b200a20002005200620072008200020002000420210088484504504400c030b200a20002005200620072008200020002000420410088484504504400c010b0b024020052006200720082000200020002009100621102300211123012112230221130b201021052011210620122107201321080c000b0b2000200020002000200520062007200810110b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1005210d2300210e0b200d210a024020012005200e1005210f230021100b200f2109024020002004201010052111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b2d01017e024020002004510440200120055104402002200651044020032007510440420121080b0b0b0b0b20080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b8a0102017e047f0240410021090240200020041009210a200a41004604400240200120051009210b200b41004604400240200220061009210c200c41004604402003200754210905200c41014604404100210905410121090b0b0b05200b41014604404100210905410121090b0b0b05200a41014604404100210905410121090b0b0b2009ad21080b20080b2901017f024042002000200184200284520440000b42002003422088520440000b2003a721040b20040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100c421086210220022000421088100c8421010b20010b1e01027e02402000100d422086210220022000422088100d8421010b20010bfc0105047e017f017e087f047e024010022108420021092009200920092009100b210a2000200120022003100b210b2009200920094220100b210c200b417f200c6b4b04404100410010010b2008200b6b210d200b20084b04404100210d0b4100210e200d200e4b0440200a200b200d10030b200c200d4b0440200c200d6b210f200a200d6a2110200e2111024003402011200f49450d010240201020116a200e3a00000b201141016a21110c000b0b0b200e290000100e2112200e41086a290000100e2113200e41106a290000100e2114200e41186a290000100e2115201221042013210520142106201521070b20052400200624012007240220040b3200024020002001100e370000200041086a2002100e370000200041106a2003100e370000200041186a2004100e3700000b0b230002404100200020012002200310104120200420052006200710104100412010000b0b
156182

157183
Text representation:
158184
(module
159185
(import "ethereum" "storageStore" (func $eth.storageStore (param i32 i32)))
186+
(import "ethereum" "revert" (func $eth.revert (param i32 i32)))
187+
(import "ethereum" "getCallDataSize" (func $eth.getCallDataSize (result i32)))
160188
(import "ethereum" "callDataCopy" (func $eth.callDataCopy (param i32 i32 i32)))
161189
(memory $memory (export "memory") 1)
162190
(export "main" (func $main))
@@ -206,7 +234,7 @@ Text representation:
206234
(br_if $label__3 (i32.eqz (i32.eqz (local.get $_4))))
207235
(block $label__4
208236
(block
209-
(local.set $_5 (call $iszero_169_788 (local.get $_1) (local.get $_1) (local.get $_1) (call $lt_171 (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 10))))
237+
(local.set $_5 (call $iszero_197_919_1528 (local.get $_1) (local.get $_1) (local.get $_1) (call $lt_199 (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 10))))
210238
(local.set $_6 (global.get $global_))
211239
(local.set $_7 (global.get $global__1))
212240
(local.set $_8 (global.get $global__2))
@@ -310,7 +338,7 @@ Text representation:
310338
(local.get $r1)
311339
)
312340

313-
(func $iszero_169_788
341+
(func $iszero_197_919_1528
314342
(param $x1 i64)
315343
(param $x2 i64)
316344
(param $x3 i64)
@@ -377,7 +405,7 @@ Text representation:
377405
(local.get $r)
378406
)
379407

380-
(func $lt_171
408+
(func $lt_199
381409
(param $x1 i64)
382410
(param $x2 i64)
383411
(param $x3 i64)
@@ -437,11 +465,29 @@ Text representation:
437465
(local.get $z4)
438466
)
439467

468+
(func $u256_to_i32
469+
(param $x1 i64)
470+
(param $x2 i64)
471+
(param $x3 i64)
472+
(param $x4 i64)
473+
(result i32)
474+
(local $v i32)
475+
(block $label__15
476+
(if (i64.ne (i64.const 0) (i64.or (i64.or (local.get $x1) (local.get $x2)) (local.get $x3))) (then
477+
(unreachable)))
478+
(if (i64.ne (i64.const 0) (i64.shr_u (local.get $x4) (i64.const 32))) (then
479+
(unreachable)))
480+
(local.set $v (i32.wrap_i64 (local.get $x4)))
481+
482+
)
483+
(local.get $v)
484+
)
485+
440486
(func $bswap16
441487
(param $x i64)
442488
(result i64)
443489
(local $y i64)
444-
(block $label__15
490+
(block $label__16
445491
(local.set $y (i64.or (i64.and (i64.shl (local.get $x) (i64.const 8)) (i64.const 65280)) (i64.and (i64.shr_u (local.get $x) (i64.const 8)) (i64.const 255))))
446492

447493
)
@@ -453,7 +499,7 @@ Text representation:
453499
(result i64)
454500
(local $y i64)
455501
(local $hi i64)
456-
(block $label__16
502+
(block $label__17
457503
(local.set $hi (i64.shl (call $bswap16 (local.get $x)) (i64.const 16)))
458504
(local.set $y (i64.or (local.get $hi) (call $bswap16 (i64.shr_u (local.get $x) (i64.const 16)))))
459505

@@ -466,7 +512,7 @@ Text representation:
466512
(result i64)
467513
(local $y i64)
468514
(local $hi i64)
469-
(block $label__17
515+
(block $label__18
470516
(local.set $hi (i64.shl (call $bswap32 (local.get $x)) (i64.const 32)))
471517
(local.set $y (i64.or (local.get $hi) (call $bswap32 (i64.shr_u (local.get $x) (i64.const 32)))))
472518

@@ -484,20 +530,55 @@ Text representation:
484530
(local $z2 i64)
485531
(local $z3 i64)
486532
(local $z4 i64)
533+
(local $cds i32)
534+
(local $_1 i64)
535+
(local $destination i32)
536+
(local $offset i32)
537+
(local $requested_size i32)
538+
(local $available_size i32)
539+
(local $_2 i32)
540+
(local $_3 i32)
541+
(local $_4 i32)
542+
(local $i i32)
487543
(local $z1_1 i64)
488544
(local $z2_1 i64)
489545
(local $z3_1 i64)
490546
(local $z4_1 i64)
491-
(block $label__18
492-
(if (i64.ne (i64.const 0) (i64.or (i64.or (local.get $x1) (local.get $x2)) (local.get $x3))) (then
493-
(unreachable)))
494-
(if (i64.ne (i64.const 0) (i64.shr_u (local.get $x4) (i64.const 32))) (then
495-
(unreachable)))
496-
(call $eth.callDataCopy (i32.const 0) (i32.wrap_i64 (local.get $x4)) (i32.const 32))
497-
(local.set $z1_1 (call $bswap64 (i64.load (i32.const 0))))
498-
(local.set $z2_1 (call $bswap64 (i64.load (i32.add (i32.const 0) (i32.const 8)))))
499-
(local.set $z3_1 (call $bswap64 (i64.load (i32.add (i32.const 0) (i32.const 16)))))
500-
(local.set $z4_1 (call $bswap64 (i64.load (i32.add (i32.const 0) (i32.const 24)))))
547+
(block $label__19
548+
(local.set $cds (call $eth.getCallDataSize))
549+
(local.set $_1 (i64.const 0))
550+
(local.set $destination (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (local.get $_1)))
551+
(local.set $offset (call $u256_to_i32 (local.get $x1) (local.get $x2) (local.get $x3) (local.get $x4)))
552+
(local.set $requested_size (call $u256_to_i32 (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 32)))
553+
(if (i32.gt_u (local.get $offset) (i32.sub (i32.const 4294967295) (local.get $requested_size))) (then
554+
(call $eth.revert (i32.const 0) (i32.const 0))))
555+
(local.set $available_size (i32.sub (local.get $cds) (local.get $offset)))
556+
(if (i32.gt_u (local.get $offset) (local.get $cds)) (then
557+
(local.set $available_size (i32.const 0))
558+
))
559+
(local.set $_2 (i32.const 0))
560+
(if (i32.gt_u (local.get $available_size) (local.get $_2)) (then
561+
(call $eth.callDataCopy (local.get $destination) (local.get $offset) (local.get $available_size))))
562+
(if (i32.gt_u (local.get $requested_size) (local.get $available_size)) (then
563+
(local.set $_3 (i32.sub (local.get $requested_size) (local.get $available_size)))
564+
(local.set $_4 (i32.add (local.get $destination) (local.get $available_size)))
565+
(local.set $i (local.get $_2))
566+
(block $label__20
567+
(loop $label__22
568+
(br_if $label__20 (i32.eqz (i32.lt_u (local.get $i) (local.get $_3))))
569+
(block $label__21
570+
(i32.store8 (i32.add (local.get $_4) (local.get $i)) (local.get $_2))
571+
)
572+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
573+
(br $label__22)
574+
)
575+
576+
)
577+
))
578+
(local.set $z1_1 (call $bswap64 (i64.load (local.get $_2))))
579+
(local.set $z2_1 (call $bswap64 (i64.load (i32.add (local.get $_2) (i32.const 8)))))
580+
(local.set $z3_1 (call $bswap64 (i64.load (i32.add (local.get $_2) (i32.const 16)))))
581+
(local.set $z4_1 (call $bswap64 (i64.load (i32.add (local.get $_2) (i32.const 24)))))
501582
(local.set $z1 (local.get $z1_1))
502583
(local.set $z2 (local.get $z2_1))
503584
(local.set $z3 (local.get $z3_1))
@@ -516,7 +597,7 @@ Text representation:
516597
(param $y2 i64)
517598
(param $y3 i64)
518599
(param $y4 i64)
519-
(block $label__19
600+
(block $label__23
520601
(i64.store (local.get $pos) (call $bswap64 (local.get $y1)))
521602
(i64.store (i32.add (local.get $pos) (i32.const 8)) (call $bswap64 (local.get $y2)))
522603
(i64.store (i32.add (local.get $pos) (i32.const 16)) (call $bswap64 (local.get $y3)))
@@ -533,7 +614,7 @@ Text representation:
533614
(param $y2 i64)
534615
(param $y3 i64)
535616
(param $y4 i64)
536-
(block $label__20
617+
(block $label__24
537618
(call $mstore_internal (i32.const 0) (local.get $x1) (local.get $x2) (local.get $x3) (local.get $x4))
538619
(call $mstore_internal (i32.const 32) (local.get $y1) (local.get $y2) (local.get $y3) (local.get $y4))
539620
(call $eth.storageStore (i32.const 0) (i32.const 32))

0 commit comments

Comments
 (0)