Skip to content

Commit

Permalink
Merge pull request #14374 from ethereum/fix-non-deterministic-ir-func…
Browse files Browse the repository at this point in the history
…tion-generation-order

Fix undefined order of functions when generating code for index expressions
  • Loading branch information
cameel authored Jul 13, 2023
2 parents 69c034b + 5b70830 commit 3fc2ead
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Compiler Features:


Bugfixes:
* Code Generator: Fix not entirely deterministic order of functions in unoptimized Yul output. The choice of C++ compiler in some cases would result in different (but equivalent) bytecode (especially from native binaries vs emscripten binaries)
* Commandline Interface: Fix internal error when using ``--stop-after parsing`` and requesting some of the outputs that require full analysis or compilation.
* Commandline Interface: It is no longer possible to specify both ``--optimize-yul`` and ``--no-optimize-yul`` at the same time.
* SMTChecker: Fix encoding of side-effects inside ``if`` and ``ternary conditional``statements in the BMC engine.
Expand Down
36 changes: 18 additions & 18 deletions libsolidity/codegen/ir/IRGeneratorForStatements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2295,13 +2295,13 @@ void IRGeneratorForStatements::endVisit(IndexAccess const& _indexAccess)
}
case DataLocation::Memory:
{
string const memAddress =
m_utils.memoryArrayIndexAccessFunction(arrayType) +
"(" +
IRVariable(_indexAccess.baseExpression()).part("mpos").name() +
", " +
expressionAsType(*_indexAccess.indexExpression(), *TypeProvider::uint256()) +
")";
string const indexAccessFunction = m_utils.memoryArrayIndexAccessFunction(arrayType);
string const baseRef = IRVariable(_indexAccess.baseExpression()).part("mpos").name();
string const indexExpression = expressionAsType(
*_indexAccess.indexExpression(),
*TypeProvider::uint256()
);
string const memAddress = indexAccessFunction + "(" + baseRef + ", " + indexExpression + ")";

setLValue(_indexAccess, IRLValue{
*arrayType.baseType(),
Expand All @@ -2311,28 +2311,28 @@ void IRGeneratorForStatements::endVisit(IndexAccess const& _indexAccess)
}
case DataLocation::CallData:
{
string indexAccessFunction = m_utils.calldataArrayIndexAccessFunction(arrayType);
string const indexAccessFunctionCall =
indexAccessFunction +
"(" +
IRVariable(_indexAccess.baseExpression()).commaSeparatedList() +
", " +
expressionAsType(*_indexAccess.indexExpression(), *TypeProvider::uint256()) +
")";
string const indexAccessFunction = m_utils.calldataArrayIndexAccessFunction(arrayType);
string const baseRef = IRVariable(_indexAccess.baseExpression()).commaSeparatedList();
string const indexExpression = expressionAsType(
*_indexAccess.indexExpression(),
*TypeProvider::uint256()
);
string const calldataAddress = indexAccessFunction + "(" + baseRef + ", " + indexExpression + ")";

if (arrayType.isByteArrayOrString())
define(_indexAccess) <<
m_utils.cleanupFunction(*arrayType.baseType()) <<
"(calldataload(" <<
indexAccessFunctionCall <<
calldataAddress <<
"))\n";
else if (arrayType.baseType()->isValueType())
define(_indexAccess) <<
m_utils.readFromCalldata(*arrayType.baseType()) <<
"(" <<
indexAccessFunctionCall <<
calldataAddress <<
")\n";
else
define(_indexAccess) << indexAccessFunctionCall << "\n";
define(_indexAccess) << calldataAddress << "\n";
break;
}
}
Expand Down

0 comments on commit 3fc2ead

Please sign in to comment.