Add another argument to setimmutable and the AssignImmutable opcode, allowing to modify code at any memory offset.

This commit is contained in:
Daniel Kirchner
2020-10-15 18:14:41 +02:00
parent 00fb152316
commit 0dca223b45
12 changed files with 58 additions and 32 deletions
+21 -10
View File
@@ -91,7 +91,7 @@ BOOST_AUTO_TEST_CASE(all_assembly_items)
// PushDeployTimeAddress
_assembly.append(PushDeployTimeAddress);
// AssignImmutable.
// Note that since there is no reference to "someOtherImmutable", this will compile to a simple POP in the hex output.
// Note that since there is no reference to "someOtherImmutable", this will just compile to two POPs in the hex output.
_assembly.appendImmutableAssignment("someOtherImmutable");
_assembly.append(u256(2));
_assembly.appendImmutableAssignment("someImmutable");
@@ -104,9 +104,9 @@ BOOST_AUTO_TEST_CASE(all_assembly_items)
BOOST_CHECK_EQUAL(
_assembly.assemble().toHex(),
"5b6001600220606f73__$bf005014d9d0f534b8fcb268bd84c491a2$__"
"60005660676022604573000000000000000000000000000000000000000050"
"60028060015250"
"5b6001600220607373__$bf005014d9d0f534b8fcb268bd84c491a2$__"
"600056606b602260497300000000000000000000000000000000000000005050"
"60028181600101525050"
"00fe"
"7f0000000000000000000000000000000000000000000000000000000000000000"
"fe010203044266eeaa"
@@ -184,8 +184,10 @@ BOOST_AUTO_TEST_CASE(immutable)
shared_ptr<Assembly> _subAsmPtr = make_shared<Assembly>(_subAsm);
_assembly.append(u256(42));
_assembly.append(u256(0));
_assembly.appendImmutableAssignment("someImmutable");
_assembly.append(u256(23));
_assembly.append(u256(0));
_assembly.appendImmutableAssignment("someOtherImmutable");
auto sub = _assembly.appendSubroutine(_subAsmPtr);
@@ -198,21 +200,26 @@ BOOST_AUTO_TEST_CASE(immutable)
// root.asm
// assign "someImmutable"
"602a" // PUSH1 42 - value for someImmutable
"80" // DUP1
"6000" // PUSH1 0 - offset of code into which to insert the immutable
"8181" // DUP2 DUP2
"6001" // PUSH1 1 - offset of first someImmutable in sub_0
"01" // ADD - add offset of immutable to offset of code
"52" // MSTORE
"80" // DUP1
"8181" // DUP2 DUP2
"6043" // PUSH1 67 - offset of second someImmutable in sub_0
"01" // ADD - add offset of immutable to offset of code
"52" // MSTORE
"50" // POP
"5050" // POP POP
// assign "someOtherImmutable"
"6017" // PUSH1 23 - value for someOtherImmutable
"80" // DUP1
"6000" // PUSH1 0 - offset of code into which to insert the immutable
"8181" // DUP2 DUP2
"6022" // PUSH1 34 - offset of someOtherImmutable in sub_0
"01" // ADD - add offset of immutable to offset of code
"52" // MSTORE
"50" // POP
"5050" // POP POP
"6063" // PUSH1 0x63 - dataSize(sub_0)
"6017" // PUSH1 0x17 - dataOffset(sub_0)
"6023" // PUSH1 0x23 - dataOffset(sub_0)
"fe" // INVALID
// end of root.asm
// sub.asm
@@ -224,8 +231,10 @@ BOOST_AUTO_TEST_CASE(immutable)
_assembly.assemblyString(),
" /* \"root.asm\":1:3 */\n"
" 0x2a\n"
" 0x00\n"
" assignImmutable(\"0x26f2c0195e9d408feff3abd77d83f2971f3c9a18d1e8a9437c7835ae4211fc9f\")\n"
" 0x17\n"
" 0x00\n"
" assignImmutable(\"0xc3978657661c4d8e32e3d5f42597c009f0d3859e9f9d0d94325268f9799e2bfb\")\n"
" dataSize(sub_0)\n"
" dataOffset(sub_0)\n"
@@ -242,8 +251,10 @@ BOOST_AUTO_TEST_CASE(immutable)
util::jsonCompactPrint(_assembly.assemblyJSON(indices)),
"{\".code\":["
"{\"begin\":1,\"end\":3,\"name\":\"PUSH\",\"source\":0,\"value\":\"2A\"},"
"{\"begin\":1,\"end\":3,\"name\":\"PUSH\",\"source\":0,\"value\":\"0\"},"
"{\"begin\":1,\"end\":3,\"name\":\"ASSIGNIMMUTABLE\",\"source\":0,\"value\":\"someImmutable\"},"
"{\"begin\":1,\"end\":3,\"name\":\"PUSH\",\"source\":0,\"value\":\"17\"},"
"{\"begin\":1,\"end\":3,\"name\":\"PUSH\",\"source\":0,\"value\":\"0\"},"
"{\"begin\":1,\"end\":3,\"name\":\"ASSIGNIMMUTABLE\",\"source\":0,\"value\":\"someOtherImmutable\"},"
"{\"begin\":1,\"end\":3,\"name\":\"PUSH #[$]\",\"source\":0,\"value\":\"0000000000000000000000000000000000000000000000000000000000000000\"},"
"{\"begin\":1,\"end\":3,\"name\":\"PUSH [$]\",\"source\":0,\"value\":\"0000000000000000000000000000000000000000000000000000000000000000\"}"
@@ -1,6 +1,7 @@
object "a" {
code {
setimmutable(
0,
"long___name___that___definitely___exceeds___the___thirty___two___byte___limit",
0x1234567890123456789012345678901234567890
)
@@ -8,10 +9,12 @@ object "a" {
}
// ----
// Assembly:
// /* "source":152:194 */
// /* "source":167:209 */
// 0x1234567890123456789012345678901234567890
// /* "source":32:204 */
// /* "source":58:59 */
// 0x00
// /* "source":32:219 */
// assignImmutable("0x85a5b1db611c82c46f5fa18e39ae218397536256c451e5de155a86de843a9ad6")
// Bytecode: 73123456789012345678901234567890123456789050
// Opcodes: PUSH20 0x1234567890123456789012345678901234567890 POP
// SourceMappings: 152:42:0:-:0;32:172
// Bytecode: 73123456789012345678901234567890123456789060005050
// Opcodes: PUSH20 0x1234567890123456789012345678901234567890 PUSH1 0x0 POP POP
// SourceMappings: 167:42:0:-:0;58:1;32:187
@@ -1,5 +1,5 @@
{
setimmutable(loadimmutable("abc"), "abc")
setimmutable(0, loadimmutable("abc"), "abc")
}
// ====
// dialect: evm
+1 -1
View File
@@ -1,5 +1,5 @@
{
setimmutable("address", 0x1234567890123456789012345678901234567890)
setimmutable(0, "address", 0x1234567890123456789012345678901234567890)
}
// ====
// dialect: evm
@@ -1,11 +1,11 @@
{
setimmutable(0, 0x1234567890123456789012345678901234567890)
setimmutable(true, 0x1234567890123456789012345678901234567890)
setimmutable(false, 0x1234567890123456789012345678901234567890)
setimmutable(0, 0, 0x1234567890123456789012345678901234567890)
setimmutable(0, true, 0x1234567890123456789012345678901234567890)
setimmutable(0, false, 0x1234567890123456789012345678901234567890)
}
// ====
// dialect: evm
// ----
// TypeError 5859: (19-20): Function expects string literal.
// TypeError 5859: (83-87): Function expects string literal.
// TypeError 5859: (150-155): Function expects string literal.
// TypeError 5859: (22-23): Function expects string literal.
// TypeError 5859: (89-93): Function expects string literal.
// TypeError 5859: (159-164): Function expects string literal.
@@ -1,5 +1,6 @@
{
setimmutable(
0,
"long___name___that___definitely___exceeds___the___thirty___two___byte___limit",
0x1234567890123456789012345678901234567890
)