Skip to content

Commit 3b6c99f

Browse files
authored
Merge pull request #12572 from ethereum/fixStackShuffling
Improved stack shuffling in corner cases.
2 parents 88f624c + de28f31 commit 3b6c99f

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

Changelog.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Bugfixes:
2525
* IR Generator: Fix IR syntax error when copying storage arrays of structs containing functions.
2626
* Natspec: Fix ICE when overriding a struct getter with a Natspec-documented return value and the name in the struct is different.
2727
* TypeChecker: Fix ICE when a constant variable declaration forward references a struct.
28-
28+
* Yul EVM Code Transform: Improved stack shuffling in corner cases.
2929

3030
Solc-Js:
3131
* The wrapper now requires at least nodejs v10.

libyul/backends/evm/StackHelpers.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,11 @@ class Shuffler
262262
if (ops.sourceMultiplicity(ops.sourceSize() - 1 - swapDepth) < 0)
263263
{
264264
ops.swap(swapDepth);
265+
if (ops.targetIsArbitrary(sourceTop))
266+
// Usually we keep a slot that is to-be-removed, if the current top is arbitrary.
267+
// However, since we are in a stack-too-deep situation, pop it immediately
268+
// to compress the stack (we can always push back junk in the end).
269+
ops.pop();
265270
return true;
266271
}
267272
// Otherwise we rely on stack compression or stack-to-memory.
@@ -321,14 +326,44 @@ class Shuffler
321326
yulAssert(ops.sourceMultiplicity(i) == 0 && (ops.targetIsArbitrary(i) || ops.targetMultiplicity(i) == 0), "");
322327
yulAssert(ops.isCompatible(sourceTop, sourceTop), "");
323328

329+
auto swappableOffsets = ranges::views::iota(size > 17 ? size - 17 : 0u, size);
330+
324331
// If we find a lower slot that is out of position, but also compatible with the top, swap that up.
332+
for (size_t offset: swappableOffsets)
333+
if (!ops.isCompatible(offset, offset) && ops.isCompatible(sourceTop, offset))
334+
{
335+
ops.swap(size - offset - 1);
336+
return true;
337+
}
338+
// Swap up any reachable slot that is still out of position.
339+
for (size_t offset: swappableOffsets)
340+
if (!ops.isCompatible(offset, offset) && !ops.sourceIsSame(offset, sourceTop))
341+
{
342+
ops.swap(size - offset - 1);
343+
return true;
344+
}
345+
// We are in a stack-too-deep situation and try to reduce the stack size.
346+
// If the current top is merely kept since the target slot is arbitrary, pop it.
347+
if (ops.targetIsArbitrary(sourceTop) && ops.sourceMultiplicity(sourceTop) <= 0)
348+
{
349+
ops.pop();
350+
return true;
351+
}
352+
// If any reachable slot is merely kept, since the target slot is arbitrary, swap it up and pop it.
353+
for (size_t offset: swappableOffsets)
354+
if (ops.targetIsArbitrary(offset) && ops.sourceMultiplicity(offset) <= 0)
355+
{
356+
ops.swap(size - offset - 1);
357+
ops.pop();
358+
return true;
359+
}
360+
// We cannot avoid a stack-too-deep error. Repeat the above without restricting to reachable slots.
325361
for (size_t offset: ranges::views::iota(0u, size))
326362
if (!ops.isCompatible(offset, offset) && ops.isCompatible(sourceTop, offset))
327363
{
328364
ops.swap(size - offset - 1);
329365
return true;
330366
}
331-
// Swap up any slot that is still out of position.
332367
for (size_t offset: ranges::views::iota(0u, size))
333368
if (!ops.isCompatible(offset, offset) && !ops.sourceIsSame(offset, sourceTop))
334369
{

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ set(libyul_sources
147147
libyul/Parser.cpp
148148
libyul/StackLayoutGeneratorTest.cpp
149149
libyul/StackLayoutGeneratorTest.h
150+
libyul/StackShufflingTest.cpp
150151
libyul/SyntaxTest.h
151152
libyul/SyntaxTest.cpp
152153
libyul/YulInterpreterTest.cpp

test/libyul/StackShufflingTest.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
This file is part of solidity.
3+
4+
solidity is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
solidity is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with solidity. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
/**
18+
* Unit tests for stack shuffling.
19+
*/
20+
#include <libyul/backends/evm/StackHelpers.h>
21+
#include <boost/test/unit_test.hpp>
22+
23+
using namespace std;
24+
using namespace solidity::langutil;
25+
26+
namespace solidity::yul::test
27+
{
28+
29+
BOOST_AUTO_TEST_SUITE(YulStackShuffling)
30+
31+
BOOST_AUTO_TEST_CASE(swap_cycle)
32+
{
33+
std::vector<Scope::Variable> scopeVariables;
34+
Scope::Function function;
35+
std::vector<VariableSlot> v;
36+
for (size_t i = 0; i < 17; ++i)
37+
scopeVariables.emplace_back(Scope::Variable{""_yulstring, YulString{"v" + to_string(i)}});
38+
for (size_t i = 0; i < 17; ++i)
39+
v.emplace_back(VariableSlot{scopeVariables[i]});
40+
41+
Stack sourceStack{
42+
v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[9], v[10], v[11], v[12], v[13], v[14], v[15], v[16],
43+
FunctionReturnLabelSlot{function}, FunctionReturnLabelSlot{function}, v[5]};
44+
Stack targetStack{
45+
v[1], v[0], v[2], v[3], v[4], v[5], v[6], v[7], v[9], v[10], v[11], v[12], v[13], v[14], v[15], v[16],
46+
FunctionReturnLabelSlot{function}, JunkSlot{}, JunkSlot{}
47+
};
48+
// Used to hit a swapping cycle.
49+
createStackLayout(sourceStack, targetStack, [](auto){}, [](auto){}, [](){});
50+
}
51+
52+
BOOST_AUTO_TEST_SUITE_END()
53+
54+
}

0 commit comments

Comments
 (0)