Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Compiler Features:
* General: Improve performance throughout the compiler using Boost's flat versions of unordered set and map.
* General: Remove support for the experimental Generic Solidity prototype (`pragma experimental solidity`).
* SMTChecker: Emit a deprecation warning for the BMC engine.
* Yul: Warn when the assembled bytecode of a compiled Yul object exceeds the EIP-170 (runtime code) or EIP-3860 (initcode) size limit, matching the existing warnings for Solidity input.

Bugfixes:
* Code Generator: Fix ICE on parenthesized custom error construction in require statement.
Expand Down
2 changes: 2 additions & 0 deletions libevmasm/CMakeLists.txt

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changelog entry is missing. This is a visible Yul change so it should be mentioned. Though not sure if I'd classify it as a bugfix or a new feature. Probably the latter, but it should have been included from the beginning so it's both in a way :)

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set(sources
Assembly.h
AssemblyItem.cpp
AssemblyItem.h
CodeSizeLimits.cpp
CodeSizeLimits.h
Ethdebug.cpp
Ethdebug.h
EthdebugSchema.cpp
Expand Down
77 changes: 77 additions & 0 deletions libevmasm/CodeSizeLimits.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
This file is part of solidity.

solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0

#include <libevmasm/CodeSizeLimits.h>

#include <liblangutil/ErrorReporter.h>

using namespace std::string_literals;
using namespace solidity::langutil;

namespace solidity::evmasm
{

namespace
{

/// Runtime code size limit (in bytes) introduced by EIP-170 in the Spurious Dragon release.
constexpr size_t RUNTIME_CODE_SIZE_LIMIT = 24576;

/// Initcode size limit (in bytes) introduced by EIP-3860 in the Shanghai release.
constexpr size_t CREATION_CODE_SIZE_LIMIT = 49152;

}

void checkCodeSizeLimits(
ErrorReporter& _errorReporter,
EVMVersion _evmVersion,
SourceLocation const& _location,
size_t _creationCodeSize,
size_t _runtimeCodeSize
)
{
// Warn if EIP-170 limits are exceeded.
if (_evmVersion >= EVMVersion::spuriousDragon() && _runtimeCodeSize > RUNTIME_CODE_SIZE_LIMIT)
_errorReporter.warning(
5574_error,
_location,
"Contract code size is "s +
std::to_string(_runtimeCodeSize) +
" bytes and exceeds " + std::to_string(RUNTIME_CODE_SIZE_LIMIT) +
" bytes (a limit introduced in Spurious Dragon). "
"This contract may not be deployable on Mainnet. "
"Consider enabling the optimizer (with a low \"runs\" value!), "
"turning off revert strings, or using libraries."
);

// Warn if EIP-3860 limits are exceeded.
if (_evmVersion >= EVMVersion::shanghai() && _creationCodeSize > CREATION_CODE_SIZE_LIMIT)
_errorReporter.warning(
3860_error,
_location,
"Contract initcode size is "s +
std::to_string(_creationCodeSize) +
" bytes and exceeds " + std::to_string(CREATION_CODE_SIZE_LIMIT) +
" bytes (a limit introduced in Shanghai). "
"This contract may not be deployable on Mainnet. "
"Consider enabling the optimizer (with a low \"runs\" value!), "
"turning off revert strings, or using libraries."
);
}

}
45 changes: 45 additions & 0 deletions libevmasm/CodeSizeLimits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
This file is part of solidity.

solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0

#pragma once

#include <liblangutil/EVMVersion.h>
#include <liblangutil/SourceLocation.h>

#include <cstddef>

namespace solidity::langutil
{
class ErrorReporter;
}

namespace solidity::evmasm
{

/// Reports warnings, via @a _errorReporter, if the given bytecode sizes exceed the limits.
/// @param _creationCodeSize size in bytes of the assembled creation (init) code.
/// @param _runtimeCodeSize size in bytes of the assembled runtime code.
void checkCodeSizeLimits(
langutil::ErrorReporter& _errorReporter,
langutil::EVMVersion _evmVersion,
langutil::SourceLocation const& _location,
size_t _creationCodeSize,
size_t _runtimeCodeSize
);

}
43 changes: 8 additions & 35 deletions libsolidity/interface/CompilerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
#include <libsolutil/Algorithms.h>
#include <libsolutil/FunctionSelector.h>

#include <libevmasm/CodeSizeLimits.h>
#include <libevmasm/Ethdebug.h>

#include <boost/algorithm/string/replace.hpp>
Expand Down Expand Up @@ -1442,41 +1443,13 @@ void CompilerStack::assembleYul(
solAssert(false, "Assembly exception for deployed bytecode"s + error.what());
}

// Throw a warning if EIP-170 limits are exceeded:
// If contract creation returns data with length greater than 0x6000 (2^14 + 2^13) bytes,
// contract creation fails with an out of gas error.
if (
m_evmVersion >= langutil::EVMVersion::spuriousDragon() &&
compiledContract.runtimeObject.bytecode.size() > 0x6000
)
m_errorReporter.warning(
5574_error,
_contract.location(),
"Contract code size is "s +
std::to_string(compiledContract.runtimeObject.bytecode.size()) +
" bytes and exceeds 24576 bytes (a limit introduced in Spurious Dragon). "
"This contract may not be deployable on Mainnet. "
"Consider enabling the optimizer (with a low \"runs\" value!), "
"turning off revert strings, or using libraries."
);

// Throw a warning if EIP-3860 limits are exceeded:
// If initcode is larger than 0xC000 bytes (twice the runtime code limit),
// then contract creation fails with an out of gas error.
if (
m_evmVersion >= langutil::EVMVersion::shanghai() &&
compiledContract.object.bytecode.size() > 0xC000
)
m_errorReporter.warning(
3860_error,
_contract.location(),
"Contract initcode size is "s +
std::to_string(compiledContract.object.bytecode.size()) +
" bytes and exceeds 49152 bytes (a limit introduced in Shanghai). "
"This contract may not be deployable on Mainnet. "
"Consider enabling the optimizer (with a low \"runs\" value!), "
"turning off revert strings, or using libraries."
);
evmasm::checkCodeSizeLimits(
m_errorReporter,
m_evmVersion,
_contract.location(),
compiledContract.object.bytecode.size(),
compiledContract.runtimeObject.bytecode.size()
);
}

void CompilerStack::compileContract(
Expand Down
10 changes: 10 additions & 0 deletions libyul/YulStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <libyul/optimiser/Semantics.h>
#include <libyul/optimiser/Suite.h>
#include <libevmasm/Assembly.h>
#include <libevmasm/CodeSizeLimits.h>
#include <libevmasm/Ethdebug.h>
#include <liblangutil/Scanner.h>
#include <liblangutil/SourceReferenceFormatter.h>
Expand Down Expand Up @@ -297,6 +298,15 @@ YulStack::assembleWithDeployed(std::optional<std::string_view> _deployName, bool
return {MachineAssemblyObject{}, MachineAssemblyObject{}};
}

yulAssert(creationObject.bytecode);
evmasm::checkCodeSizeLimits(
m_errorReporter,
m_evmVersion,
m_parserResult->code()->root().debugData->nativeLocation,
creationObject.bytecode->bytecode.size(),
deployedObject.bytecode ? deployedObject.bytecode->bytecode.size() : 0
);

return {std::move(creationObject), std::move(deployedObject)};
}

Expand Down
Loading