Warn on JUMP/JUMPI in inline assembly

This commit is contained in:
Alex Beregszaszi 2017-06-13 19:10:26 +01:00
parent 95f7902955
commit d4fecc7b11
4 changed files with 20 additions and 4 deletions

View File

@ -12,6 +12,7 @@ Features:
* Inline Assembly: introduce ``keccak256`` as an opcode. ``sha3`` is still a valid alias. * Inline Assembly: introduce ``keccak256`` as an opcode. ``sha3`` is still a valid alias.
* Inline Assembly: ``for`` and ``switch`` statements. * Inline Assembly: ``for`` and ``switch`` statements.
* Inline Assembly: function definitions and function calls. * Inline Assembly: function definitions and function calls.
* Inline Assembly: Warn when using ``jump``s.
* Type Checker: Warn about copies in storage that might overwrite unexpectedly. * Type Checker: Warn about copies in storage that might overwrite unexpectedly.
* Code Generator: Added the Whiskers template system. * Code Generator: Added the Whiskers template system.
* Remove obsolete Why3 output. * Remove obsolete Why3 output.

View File

@ -65,7 +65,7 @@ bool AsmAnalyzer::operator()(assembly::Instruction const& _instruction)
auto const& info = instructionInfo(_instruction.instruction); auto const& info = instructionInfo(_instruction.instruction);
m_stackHeight += info.ret - info.args; m_stackHeight += info.ret - info.args;
m_info.stackHeightInfo[&_instruction] = m_stackHeight; m_info.stackHeightInfo[&_instruction] = m_stackHeight;
warnOnFutureInstruction(_instruction.instruction, _instruction.location); warnOnInstructions(_instruction.instruction, _instruction.location);
return true; return true;
} }
@ -150,7 +150,6 @@ bool AsmAnalyzer::operator()(FunctionalInstruction const& _instr)
if (!(*this)(_instr.instruction)) if (!(*this)(_instr.instruction))
success = false; success = false;
m_info.stackHeightInfo[&_instr] = m_stackHeight; m_info.stackHeightInfo[&_instr] = m_stackHeight;
warnOnFutureInstruction(_instr.instruction.instruction, _instr.location);
return success; return success;
} }
@ -470,7 +469,7 @@ void AsmAnalyzer::expectValidType(string const& type, SourceLocation const& _loc
); );
} }
void AsmAnalyzer::warnOnFutureInstruction(solidity::Instruction _instr, SourceLocation const& _location) void AsmAnalyzer::warnOnInstructions(solidity::Instruction _instr, SourceLocation const& _location)
{ {
static set<solidity::Instruction> futureInstructions{ static set<solidity::Instruction> futureInstructions{
solidity::Instruction::CREATE2, solidity::Instruction::CREATE2,
@ -486,4 +485,12 @@ void AsmAnalyzer::warnOnFutureInstruction(solidity::Instruction _instr, SourceLo
+ "\" instruction is only available after " + + "\" instruction is only available after " +
"the Metropolis hard fork. Before that it acts as an invalid instruction." "the Metropolis hard fork. Before that it acts as an invalid instruction."
); );
if (_instr == solidity::Instruction::JUMP || _instr == solidity::Instruction::JUMPI)
m_errorReporter.warning(
_location,
"Jump instructions are low-level EVM features that can lead to "
"incorrect stack access. Because of that they are discouraged. "
"Please consider using \"switch\" or \"for\" statements instead."
);
} }

View File

@ -85,7 +85,7 @@ private:
Scope& scope(assembly::Block const* _block); Scope& scope(assembly::Block const* _block);
void expectValidType(std::string const& type, SourceLocation const& _location); void expectValidType(std::string const& type, SourceLocation const& _location);
void warnOnFutureInstruction(solidity::Instruction _instr, SourceLocation const& _location); void warnOnInstructions(solidity::Instruction _instr, SourceLocation const& _location);
int m_stackHeight = 0; int m_stackHeight = 0;
julia::ExternalIdentifierAccess::Resolver m_resolver; julia::ExternalIdentifierAccess::Resolver m_resolver;

View File

@ -630,6 +630,14 @@ BOOST_AUTO_TEST_CASE(create2)
BOOST_CHECK(successAssemble("{ pop(create2(10, 0x123, 32, 64)) }")); BOOST_CHECK(successAssemble("{ pop(create2(10, 0x123, 32, 64)) }"));
} }
BOOST_AUTO_TEST_CASE(jump_warning)
{
CHECK_PARSE_ERROR("{ 1 jump }", Warning, "Jump instructions");
CHECK_PARSE_ERROR("{ 1 2 jumpi }", Warning, "Jump instructions");
CHECK_PARSE_ERROR("{ a: jump(a) }", Warning, "Jump instructions");
CHECK_PARSE_ERROR("{ a: jumpi(a, 2) }", Warning, "Jump instructions");
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()