From 2696cff8da228924b7ece37986b124b67c52acb1 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Tue, 2 Jul 2024 18:28:09 +0200 Subject: [PATCH] Relax memory safety. Co-authored-by: Moritz Hoffmann --- docs/assembly.rst | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/assembly.rst b/docs/assembly.rst index cf2ce1bf9487..04742c504c9a 100644 --- a/docs/assembly.rst +++ b/docs/assembly.rst @@ -377,3 +377,18 @@ of Solidity, you can use a special comment to annotate an assembly block as memo Note that we will disallow the annotation via comment in a future breaking release; so, if you are not concerned with backward-compatibility with older compiler versions, prefer using the dialect string. + +Advanced Safe Use of Memory +--------------------------- + +Beyond the strict definition of memory-safety given above, there are cases in which you may want to use more than 64 bytes +of scratch space starting at memory offset ``0``. If you are careful, it can be admissible to use memory up to (and not +including) offset ``0x80`` and still safely declare the assembly block as ``memory-safe``. +This is admissible under either of the following conditions: + +- By the end of the assembly block, the free memory pointer at offset ``0x40`` is restored to a sane value (i.e. it is either + restored to its original value or an increment of it due to a manual memory allocation), and the memory word at offset ``0x60`` + is restored to a value of zero. + +- The assembly block terminates, i.e. execution can never return to high-level Solidity code. This is the case, for example, + if your assembly block unconditionally ends in a ``revert`` statement.