Store all stack errors before they are thrown.

This commit is contained in:
chriseth 2019-01-31 15:06:20 +01:00
parent 77baf6caf7
commit 22c8d74a8a
3 changed files with 37 additions and 5 deletions

View File

@ -769,21 +769,24 @@ void CodeTransform::generateAssignment(Identifier const& _variableName)
} }
} }
int CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulString _varName, bool _forSwap) const int CodeTransform::variableHeightDiff(Scope::Variable const& _var, YulString _varName, bool _forSwap)
{ {
solAssert(m_context->variableStackHeights.count(&_var), ""); solAssert(m_context->variableStackHeights.count(&_var), "");
int heightDiff = m_assembly.stackHeight() - m_context->variableStackHeights[&_var]; int heightDiff = m_assembly.stackHeight() - m_context->variableStackHeights[&_var];
solAssert(heightDiff > (_forSwap ? 1 : 0), "Negative stack difference for variable."); solAssert(heightDiff > (_forSwap ? 1 : 0), "Negative stack difference for variable.");
int limit = _forSwap ? 17 : 16; int limit = _forSwap ? 17 : 16;
if (heightDiff > limit) if (heightDiff > limit)
// throw exception with variable name and height diff {
BOOST_THROW_EXCEPTION(StackTooDeepError(_varName, heightDiff - limit) << errinfo_comment( m_stackErrors.emplace_back(_varName, heightDiff - limit);
m_stackErrors.back() << errinfo_comment(
"Variable " + "Variable " +
_varName.str() + _varName.str() +
" is " + " is " +
to_string(heightDiff - limit) + to_string(heightDiff - limit) +
" slot(s) too deep inside the stack." " slot(s) too deep inside the stack."
)); );
BOOST_THROW_EXCEPTION(m_stackErrors.back());
}
return heightDiff; return heightDiff;
} }

View File

@ -189,7 +189,7 @@ private:
/// Determines the stack height difference to the given variables. Throws /// Determines the stack height difference to the given variables. Throws
/// if it is not yet in scope or the height difference is too large. Returns /// if it is not yet in scope or the height difference is too large. Returns
/// the (positive) stack height difference otherwise. /// the (positive) stack height difference otherwise.
int variableHeightDiff(Scope::Variable const& _var, YulString _name, bool _forSwap) const; int variableHeightDiff(Scope::Variable const& _var, YulString _name, bool _forSwap);
void expectDeposit(int _deposit, int _oldHeight) const; void expectDeposit(int _deposit, int _oldHeight) const;

View File

@ -190,6 +190,35 @@ BOOST_AUTO_TEST_CASE(nested)
BOOST_CHECK_EQUAL(out, "h: 9 "); BOOST_CHECK_EQUAL(out, "h: 9 ");
} }
BOOST_AUTO_TEST_CASE(also_in_outer_block)
{
string out = check(R"({
let x := 0
let r1 := 0
let r2 := 0
let r3 := 0
let r4 := 0
let r5 := 0
let r6 := 0
let r7 := 0
let r8 := 0
let r9 := 0
let r10 := 0
let r11 := 0
let r12 := 0
let r13 := 0
let r14 := 0
let r15 := 0
let r16 := 0
let r17 := 0
let r18 := 0
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
function g(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19) -> w, v {
}
})");
BOOST_CHECK_EQUAL(out, ": 9 ");
}
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()
} }