diff --git a/libsolidity/codegen/YulUtilFunctions.cpp b/libsolidity/codegen/YulUtilFunctions.cpp index 4c8b20636..a791cae35 100644 --- a/libsolidity/codegen/YulUtilFunctions.cpp +++ b/libsolidity/codegen/YulUtilFunctions.cpp @@ -379,27 +379,32 @@ string YulUtilFunctions::roundUpFunction() }); } -string YulUtilFunctions::overflowCheckedUIntAddFunction(size_t _bits) +string YulUtilFunctions::overflowCheckedIntAddFunction(IntegerType const& _type) { - solAssert(0 < _bits && _bits <= 256 && _bits % 8 == 0, ""); - string functionName = "checked_add_uint_" + to_string(_bits); + string functionName = "checked_add_" + _type.identifier(); + // TODO: Consider to add a special case for unsigned 256-bit integers + // and use the following instead: + // sum := add(x, y) if lt(sum, x) { revert(0, 0) } return m_functionCollector->createFunction(functionName, [&]() { return Whiskers(R"( function (x, y) -> sum { - - let mask := - sum := add(and(x, mask), and(y, mask)) - if and(sum, not(mask)) { revert(0, 0) } - - sum := add(x, y) - if lt(sum, x) { revert(0, 0) } - + + // overflow, if x >= 0 and y > (maxValue - x) + if and(iszero(slt(x, 0)), sgt(y, sub(, x))) { revert(0, 0) } + // underflow, if x < 0 and y < (minValue - x) + if and(slt(x, 0), slt(y, sub(, x))) { revert(0, 0) } + + // overflow, if x > (maxValue - y) + if gt(x, sub(, y)) { revert(0, 0) } + + sum := add(x, y) } )") - ("shortType", _bits < 256) ("functionName", functionName) - ("mask", toCompactHexWithPrefix((u256(1) << _bits) - 1)) + ("signed", _type.isSigned()) + ("maxValue", toCompactHexWithPrefix(u256(_type.maxValue()))) + ("minValue", toCompactHexWithPrefix(u256(_type.minValue()))) .render(); }); } diff --git a/libsolidity/codegen/YulUtilFunctions.h b/libsolidity/codegen/YulUtilFunctions.h index ec828ce4f..fcc168626 100644 --- a/libsolidity/codegen/YulUtilFunctions.h +++ b/libsolidity/codegen/YulUtilFunctions.h @@ -93,7 +93,7 @@ public: std::string roundUpFunction(); /// signature: (x, y) -> sum - std::string overflowCheckedUIntAddFunction(size_t _bits); + std::string overflowCheckedIntAddFunction(IntegerType const& _type); /// signature: (x, y) -> product std::string overflowCheckedUIntMulFunction(size_t _bits); diff --git a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp index 80a09ccbd..1c64c2fdc 100644 --- a/libsolidity/codegen/ir/IRGeneratorForStatements.cpp +++ b/libsolidity/codegen/ir/IRGeneratorForStatements.cpp @@ -1126,18 +1126,27 @@ string IRGeneratorForStatements::binaryOperation( if (IntegerType const* type = dynamic_cast(&_type)) { string fun; - // TODO: Only division is implemented for signed integers for now. - if (!type->isSigned()) + // TODO: Implement all operations for signed and unsigned types. + switch (_operator) { - if (_operator == Token::Add) - fun = m_utils.overflowCheckedUIntAddFunction(type->numBits()); - else if (_operator == Token::Sub) - fun = m_utils.overflowCheckedUIntSubFunction(); - else if (_operator == Token::Mul) - fun = m_utils.overflowCheckedUIntMulFunction(type->numBits()); + case Token::Add: + fun = m_utils.overflowCheckedIntAddFunction(*type); + break; + case Token::Sub: + if (!type->isSigned()) + fun = m_utils.overflowCheckedUIntSubFunction(); + break; + case Token::Mul: + if (!type->isSigned()) + fun = m_utils.overflowCheckedUIntMulFunction(type->numBits()); + break; + case Token::Div: + fun = m_utils.overflowCheckedIntDivFunction(*type); + break; + default: + break; } - if (_operator == Token::Div) - fun = m_utils.overflowCheckedIntDivFunction(*type); + solUnimplementedAssert(!fun.empty(), ""); return fun + "(" + _left + ", " + _right + ")\n"; } diff --git a/test/libsolidity/semanticTests/viaYul/detect_add_overflow.yul b/test/libsolidity/semanticTests/viaYul/detect_add_overflow.sol similarity index 100% rename from test/libsolidity/semanticTests/viaYul/detect_add_overflow.yul rename to test/libsolidity/semanticTests/viaYul/detect_add_overflow.sol diff --git a/test/libsolidity/semanticTests/viaYul/detect_add_overflow_signed.sol b/test/libsolidity/semanticTests/viaYul/detect_add_overflow_signed.sol new file mode 100644 index 000000000..6e44c44ba --- /dev/null +++ b/test/libsolidity/semanticTests/viaYul/detect_add_overflow_signed.sol @@ -0,0 +1,37 @@ +contract C { + function f(int a, int b) public pure returns (int x) { + x = a + b; + } + function g(int8 a, int8 b) public pure returns (int8 x) { + x = a + b; + } +} +// ==== +// compileViaYul: true +// ---- +// f(int256,int256): 5, 6 -> 11 +// f(int256,int256): -2, 1 -> -1 +// f(int256,int256): -2, 2 -> 0 +// f(int256,int256): 2, -2 -> 0 +// f(int256,int256): -5, -6 -> -11 +// f(int256,int256): 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0, 0x0F -> 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF +// f(int256,int256): 0x0F, 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF0 -> 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF +// f(int256,int256): 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF, 1 -> FAILURE +// f(int256,int256): 1, 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF -> FAILURE +// f(int256,int256): 0x8000000000000000000000000000000000000000000000000000000000000001, -1 -> 0x8000000000000000000000000000000000000000000000000000000000000000 +// f(int256,int256): -1, 0x8000000000000000000000000000000000000000000000000000000000000001 -> 0x8000000000000000000000000000000000000000000000000000000000000000 +// f(int256,int256): 0x8000000000000000000000000000000000000000000000000000000000000000, -1 -> FAILURE +// f(int256,int256): -1, 0x8000000000000000000000000000000000000000000000000000000000000000 -> FAILURE +// g(int8,int8): 5, 6 -> 11 +// g(int8,int8): -2, 1 -> -1 +// g(int8,int8): -2, 2 -> 0 +// g(int8,int8): 2, -2 -> 0 +// g(int8,int8): -5, -6 -> -11 +// g(int8,int8): 126, 1 -> 127 +// g(int8,int8): 1, 126 -> 127 +// g(int8,int8): 127, 1 -> FAILURE +// g(int8,int8): 1, 127 -> FAILURE +// g(int8,int8): -127, -1 -> -128 +// g(int8,int8): -1, -127 -> -128 +// g(int8,int8): -127, -2 -> FAILURE +// g(int8,int8): -2, -127 -> FAILURE