solidity/docs/assembly.rst

238 lines
9.4 KiB
ReStructuredText
Raw Normal View History

2017-06-21 19:46:42 +00:00
.. _inline-assembly:
2020-01-09 17:35:22 +00:00
###############
2016-11-05 15:58:06 +00:00
Inline Assembly
2020-01-09 17:35:22 +00:00
###############
2016-11-05 15:58:06 +00:00
2020-01-09 17:35:22 +00:00
.. index:: ! assembly, ! asm, ! evmasm
2020-01-09 17:35:22 +00:00
You can interleave Solidity statements with inline assembly in a language close
to the one of the Ethereum virtual machine. This gives you more fine-grained control,
which is especially useful when you are enhancing the language by writing libraries.
2019-12-16 15:26:32 +00:00
2020-03-09 08:42:03 +00:00
The language used for inline assembly in Solidity is called :ref:`Yul <yul>`
2020-01-09 17:35:22 +00:00
and it is documented in its own section. This section will only cover
how the inline assembly code can interface with the surrounding Solidity code.
2016-11-05 15:58:06 +00:00
.. warning::
2017-01-31 22:31:25 +00:00
Inline assembly is a way to access the Ethereum Virtual Machine
at a low level. This bypasses several important safety
features and checks of Solidity. You should only use it for
tasks that need it, and only if you are confident with using it.
2016-11-05 15:58:06 +00:00
2020-01-09 17:35:22 +00:00
An inline assembly block is marked by ``assembly { ... }``, where the code inside
2020-03-09 08:42:03 +00:00
the curly braces is code in the :ref:`Yul <yul>` language.
2020-01-09 17:35:22 +00:00
The inline assembly code can access local Solidity variables as explained below.
2020-01-09 17:35:22 +00:00
Different inline assembly blocks share no namespace, i.e. it is not possible
to call a Yul function or access a Yul variable defined in a different inline assembly block.
2017-04-03 17:17:17 +00:00
2016-11-05 15:58:06 +00:00
Example
-------
The following example provides library code to access the code of another contract and
load it into a ``bytes`` variable. This is not possible with "plain Solidity" and the
2019-12-16 15:26:32 +00:00
idea is that reusable assembly libraries can enhance the Solidity language
without a compiler change.
2016-11-05 15:58:06 +00:00
.. code::
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
2017-01-31 22:31:25 +00:00
2016-11-05 15:58:06 +00:00
library GetCode {
function at(address _addr) public view returns (bytes memory o_code) {
2016-11-05 15:58:06 +00:00
assembly {
// retrieve the size of the code, this needs assembly
let size := extcodesize(_addr)
// allocate output byte array - this could also be done without assembly
// by using o_code = new bytes(size)
o_code := mload(0x40)
// new "memory end" including padding
mstore(0x40, add(o_code, and(add(add(size, 0x20), 0x1f), not(0x1f))))
// store length in memory
mstore(o_code, size)
// actually retrieve the code, this needs assembly
extcodecopy(_addr, add(o_code, 0x20), 0, size)
}
}
}
Inline assembly is also beneficial in cases where the optimizer fails to produce
efficient code, for example:
2016-11-05 15:58:06 +00:00
.. code::
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
2017-01-31 22:31:25 +00:00
2016-11-05 15:58:06 +00:00
library VectorSum {
// This function is less efficient because the optimizer currently fails to
// remove the bounds checks in array access.
function sumSolidity(uint[] memory _data) public pure returns (uint sum) {
2016-11-05 15:58:06 +00:00
for (uint i = 0; i < _data.length; ++i)
sum += _data[i];
2016-11-05 15:58:06 +00:00
}
// We know that we only access the array in bounds, so we can avoid the check.
// 0x20 needs to be added to an array because the first slot contains the
// array length.
function sumAsm(uint[] memory _data) public pure returns (uint sum) {
2016-11-05 15:58:06 +00:00
for (uint i = 0; i < _data.length; ++i) {
assembly {
sum := add(sum, mload(add(add(_data, 0x20), mul(i, 0x20))))
2016-11-05 15:58:06 +00:00
}
}
}
2017-07-26 14:01:17 +00:00
// Same as above, but accomplish the entire code within inline assembly.
function sumPureAsm(uint[] memory _data) public pure returns (uint sum) {
2017-07-26 14:01:17 +00:00
assembly {
// Load the length (first 32 bytes)
let len := mload(_data)
// Skip over the length field.
//
// Keep temporary variable so it can be incremented in place.
//
// NOTE: incrementing _data would result in an unusable
// _data variable after this assembly block
let data := add(_data, 0x20)
// Iterate until the bound is not met.
for
{ let end := add(data, mul(len, 0x20)) }
lt(data, end)
{ data := add(data, 0x20) }
{
sum := add(sum, mload(data))
}
2017-07-26 14:01:17 +00:00
}
}
2016-11-05 15:58:06 +00:00
}
Access to External Variables, Functions and Libraries
-----------------------------------------------------
2016-11-05 15:58:06 +00:00
You can access Solidity variables and other identifiers by using their name.
2020-01-09 17:35:22 +00:00
Local variables of value type are directly usable in inline assembly.
Local variables that refer to memory or calldata evaluate to the
address of the variable in memory, resp. calldata, not the value itself.
For local storage variables or state variables, a single Yul identifier
is not sufficient, since they do not necessarily occupy a single full storage slot.
Therefore, their "address" is composed of a slot and a byte-offset
2017-04-26 09:58:36 +00:00
inside that slot. To retrieve the slot pointed to by the variable ``x``, you
2020-07-02 13:25:11 +00:00
use ``x.slot``, and to retrieve the byte-offset you use ``x.offset``.
Using ``x`` itself will result in an error.
2017-04-26 09:58:36 +00:00
Local Solidity variables are available for assignments, for example:
2016-11-05 15:58:06 +00:00
.. code::
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
2017-01-31 22:31:25 +00:00
2016-11-05 15:58:06 +00:00
contract C {
uint b;
2018-08-09 13:36:00 +00:00
function f(uint x) public view returns (uint r) {
2016-11-05 15:58:06 +00:00
assembly {
2020-01-09 17:35:22 +00:00
// We ignore the storage slot offset, we know it is zero
// in this special case.
2020-07-02 13:25:11 +00:00
r := mul(x, sload(b.slot))
2016-11-05 15:58:06 +00:00
}
}
}
.. warning::
If you access variables of a type that spans less than 256 bits
(for example ``uint64``, ``address``, ``bytes16`` or ``byte``),
you cannot make any assumptions about bits not part of the
encoding of the type. Especially, do not assume them to be zero.
To be safe, always clear the data properly before you use it
in a context where this is important:
``uint32 x = f(); assembly { x := and(x, 0xffffffff) /* now use x */ }``
To clean signed types, you can use the ``signextend`` opcode:
2019-06-17 11:39:20 +00:00
``assembly { signextend(<num_bytes_of_x_minus_one>, x) }``
2016-11-05 15:58:06 +00:00
2020-07-02 13:25:11 +00:00
Since Solidity 0.6.0 the name of a inline assembly variable may not
shadow any declaration visible in the scope of the inline assembly block
(including variable, contract and function declarations).
2020-07-02 13:25:11 +00:00
Since Solidity 0.7.0, variables and functions declared inside the
inline assembly block may not contain ``.``, but using ``.`` is
valid to access Solidity variables from outside the inline assembly block.
2016-11-05 15:58:06 +00:00
Assignments are possible to assembly-local variables and to function-local
variables. Take care that when you assign to variables that point to
memory or storage, you will only change the pointer and not the data.
2020-07-02 13:25:11 +00:00
You can assign to the ``.slot`` part of a local storage variable pointer.
For these (structs, arrays or mappings), the ``.offset`` part is always zero.
It is not possible to assign to the ``.slot`` or ``.offset`` part of a state variable,
though.
2016-11-05 15:58:06 +00:00
Things to Avoid
---------------
Inline assembly might have a quite high-level look, but it actually is extremely
low-level. Function calls, loops, ifs and switches are converted by simple
rewriting rules and after that, the only thing the assembler does for you is re-arranging
2018-09-20 20:41:14 +00:00
functional-style opcodes, counting stack height for
2016-11-05 15:58:06 +00:00
variable access and removing stack slots for assembly-local variables when the end
2018-09-20 20:41:14 +00:00
of their block is reached.
2016-11-05 15:58:06 +00:00
Conventions in Solidity
-----------------------
In contrast to EVM assembly, Solidity has types which are narrower than 256 bits,
2020-01-09 17:35:22 +00:00
e.g. ``uint24``. For efficiency, most arithmetic operations ignore the fact that
types can be shorter than 256
bits, and the higher-order bits are cleaned when necessary,
i.e., shortly before they are written to memory or before comparisons are performed.
This means that if you access such a variable
from within inline assembly, you might have to manually clean the higher-order bits
2016-11-05 15:58:06 +00:00
first.
Solidity manages memory in the following way. There is a "free memory pointer"
at position ``0x40`` in memory. If you want to allocate memory, use the memory
starting from where this pointer points at and update it.
2018-09-20 20:41:14 +00:00
There is no guarantee that the memory has not been used before and thus
you cannot assume that its contents are zero bytes.
There is no built-in mechanism to release or free allocated memory.
Here is an assembly snippet you can use for allocating memory that follows the process outlined above::
function allocate(length) -> pos {
pos := mload(0x40)
mstore(0x40, add(pos, length))
}
2016-11-05 15:58:06 +00:00
The first 64 bytes of memory can be used as "scratch space" for short-term
allocation. The 32 bytes after the free memory pointer (i.e., starting at ``0x60``)
are meant to be zero permanently and is used as the initial value for
empty dynamic memory arrays.
This means that the allocatable memory starts at ``0x80``, which is the initial value
of the free memory pointer.
Elements in memory arrays in Solidity always occupy multiples of 32 bytes (this is
2016-11-05 15:58:06 +00:00
even true for ``byte[]``, but not for ``bytes`` and ``string``). Multi-dimensional memory
arrays are pointers to memory arrays. The length of a dynamic array is stored at the
2018-09-20 20:41:14 +00:00
first slot of the array and followed by the array elements.
2016-11-05 15:58:06 +00:00
.. warning::
2018-09-20 20:41:14 +00:00
Statically-sized memory arrays do not have a length field, but it might be added later
2016-11-05 15:58:06 +00:00
to allow better convertibility between statically- and dynamically-sized arrays, so
do not rely on this.