Disallow codecopy and codesize in pure functions.

This commit is contained in:
Daniel Kirchner 2021-11-08 18:08:47 +01:00
parent 814e233b67
commit d6d286b39b
6 changed files with 12 additions and 2 deletions

View File

@ -3,6 +3,7 @@
Breaking changes: Breaking changes:
* Disallow ``.pop()`` on arrays containing nested mappings. * Disallow ``.pop()`` on arrays containing nested mappings.
* Disallow ``delete`` on types that contain nested mappings. * Disallow ``delete`` on types that contain nested mappings.
* Disallow ``codecopy`` and ``codesize`` in ``pure`` functions.
* Inline Assembly: Consider functions, function parameters and return variables for shadowing checks. * Inline Assembly: Consider functions, function parameters and return variables for shadowing checks.
* Commandline Interface: Remapping targets are not automatically added to allowed paths. * Commandline Interface: Remapping targets are not automatically added to allowed paths.
* Commandline Interface: Assembler mode no longer enables all outputs by default. * Commandline Interface: Assembler mode no longer enables all outputs by default.

View File

@ -225,6 +225,7 @@ Functions can be declared ``pure`` in which case they promise not to read from o
In particular, it should be possible to evaluate a ``pure`` function at compile-time given In particular, it should be possible to evaluate a ``pure`` function at compile-time given
only its inputs and ``msg.data``, but without any knowledge of the current blockchain state. only its inputs and ``msg.data``, but without any knowledge of the current blockchain state.
This means that reading from ``immutable`` variables can be a non-pure operation. This means that reading from ``immutable`` variables can be a non-pure operation.
Similarly, the inline assembly builtins ``codecopy`` and ``codesize`` are not pure.
.. note:: .. note::
If the compiler's EVM target is Byzantium or newer (default) the opcode ``STATICCALL`` is used, If the compiler's EVM target is Byzantium or newer (default) the opcode ``STATICCALL`` is used,

View File

@ -351,6 +351,8 @@ bool SemanticInformation::invalidInPureFunctions(Instruction _instruction)
case Instruction::ORIGIN: case Instruction::ORIGIN:
case Instruction::CALLER: case Instruction::CALLER:
case Instruction::CALLVALUE: case Instruction::CALLVALUE:
case Instruction::CODESIZE:
case Instruction::CODECOPY:
case Instruction::CHAINID: case Instruction::CHAINID:
case Instruction::BASEFEE: case Instruction::BASEFEE:
case Instruction::GAS: case Instruction::GAS:

View File

@ -1,7 +1,7 @@
contract C { contract C {
function f() public pure { function f() public pure {
assembly { assembly {
switch codesize() switch calldatasize()
case hex"00" {} case hex"00" {}
case hex"1122" {} case hex"1122" {}
} }

View File

@ -1,7 +1,7 @@
contract C { contract C {
function f() public pure { function f() public pure {
assembly { assembly {
switch codesize() switch calldatasize()
case "1" {} case "1" {}
case "2" {} case "2" {}
} }

View File

@ -26,5 +26,11 @@ contract C {
function l() public view { function l() public view {
assembly { pop(extcodesize(0)) } assembly { pop(extcodesize(0)) }
} }
function m() public view {
assembly { codecopy(0,0,0) }
}
function n() public view {
assembly { pop(codesize()) }
}
} }
// ---- // ----