Pad code to multiple of 32 bytes.

This commit is contained in:
chriseth 2019-01-28 11:59:18 +01:00
parent d6b8521ed5
commit 35483422f3
3 changed files with 21 additions and 1 deletions

View File

@ -1,6 +1,7 @@
### 0.5.4 (unreleased) ### 0.5.4 (unreleased)
Bugfixes: Bugfixes:
* Code generator: Defensively pad allocation of creationCode and runtimeCode to multiples of 32 bytes.
* Parser: Disallow empty import statements. * Parser: Disallow empty import statements.
* Type system: Properly report packed encoded size for arrays and structs (mostly unused until now). * Type system: Properly report packed encoded size for arrays and structs (mostly unused until now).

View File

@ -1348,7 +1348,7 @@ bool ExpressionCompiler::visit(MemberAccess const& _memberAccess)
m_context.appendInlineAssembly( m_context.appendInlineAssembly(
Whiskers(R"({ Whiskers(R"({
mstore(start, sub(end, add(start, 0x20))) mstore(start, sub(end, add(start, 0x20)))
mstore(<free>, end) mstore(<free>, and(add(end, 31), not(31)))
})")("free", to_string(CompilerUtils::freeMemoryPointer)).render(), })")("free", to_string(CompilerUtils::freeMemoryPointer)).render(),
{"start", "end"} {"start", "end"}
); );

View File

@ -14275,6 +14275,25 @@ BOOST_AUTO_TEST_CASE(code_access)
ABI_CHECK(codeRuntime1, codeRuntime2); ABI_CHECK(codeRuntime1, codeRuntime2);
} }
BOOST_AUTO_TEST_CASE(code_access_padding)
{
char const* sourceCode = R"(
contract C {
function diff() public pure returns (uint remainder) {
bytes memory a = type(D).creationCode;
bytes memory b = type(D).runtimeCode;
assembly { remainder := mod(sub(b, a), 0x20) }
}
}
contract D {
function f() public pure returns (uint) { return 7; }
}
)";
compileAndRun(sourceCode, 0, "C");
// This checks that the allocation function pads to multiples of 32 bytes
ABI_CHECK(callContractFunction("diff()"), encodeArgs(0));
}
BOOST_AUTO_TEST_CASE(code_access_create) BOOST_AUTO_TEST_CASE(code_access_create)
{ {
char const* sourceCode = R"( char const* sourceCode = R"(