mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Implement checked mod for sol->yul code generation.
This commit is contained in:
parent
a5b9f634ef
commit
fcd3410f26
@ -466,6 +466,23 @@ string YulUtilFunctions::overflowCheckedIntDivFunction(IntegerType const& _type)
|
||||
});
|
||||
}
|
||||
|
||||
string YulUtilFunctions::checkedIntModFunction(IntegerType const& _type)
|
||||
{
|
||||
string functionName = "checked_mod_" + _type.identifier();
|
||||
return m_functionCollector->createFunction(functionName, [&]() {
|
||||
return
|
||||
Whiskers(R"(
|
||||
function <functionName>(x, y) -> r {
|
||||
if iszero(y) { revert(0, 0) }
|
||||
r := <?signed>s</signed>mod(x, y)
|
||||
}
|
||||
)")
|
||||
("functionName", functionName)
|
||||
("signed", _type.isSigned())
|
||||
.render();
|
||||
});
|
||||
}
|
||||
|
||||
string YulUtilFunctions::overflowCheckedIntSubFunction(IntegerType const& _type)
|
||||
{
|
||||
string functionName = "checked_sub_" + _type.identifier();
|
||||
|
@ -103,6 +103,10 @@ public:
|
||||
/// signed division of the smallest number by -1.
|
||||
std::string overflowCheckedIntDivFunction(IntegerType const& _type);
|
||||
|
||||
/// @returns name of function to perform modulo on integers.
|
||||
/// Reverts for modulo by zero.
|
||||
std::string checkedIntModFunction(IntegerType const& _type);
|
||||
|
||||
/// @returns computes the difference between two values.
|
||||
/// Assumes the input to be in range for the type.
|
||||
/// signature: (x, y) -> diff
|
||||
|
@ -1141,6 +1141,9 @@ string IRGeneratorForStatements::binaryOperation(
|
||||
case Token::Div:
|
||||
fun = m_utils.overflowCheckedIntDivFunction(*type);
|
||||
break;
|
||||
case Token::Mod:
|
||||
fun = m_utils.checkedIntModFunction(*type);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
25
test/libsolidity/semanticTests/viaYul/detect_mod_zero.sol
Normal file
25
test/libsolidity/semanticTests/viaYul/detect_mod_zero.sol
Normal file
@ -0,0 +1,25 @@
|
||||
contract C {
|
||||
function f(uint a, uint b) public pure returns (uint x) {
|
||||
x = a % b;
|
||||
}
|
||||
function g(uint8 a, uint8 b) public pure returns (uint8 x) {
|
||||
x = a % b;
|
||||
}
|
||||
}
|
||||
// ====
|
||||
// compileViaYul: only
|
||||
// ----
|
||||
// f(uint256,uint256): 10, 3 -> 1
|
||||
// f(uint256,uint256): 10, 2 -> 0
|
||||
// f(uint256,uint256): 11, 2 -> 1
|
||||
// f(uint256,uint256): 2, 2 -> 0
|
||||
// f(uint256,uint256): 1, 0 -> FAILURE
|
||||
// f(uint256,uint256): 0, 0 -> FAILURE
|
||||
// f(uint256,uint256): 0, 1 -> 0
|
||||
// g(uint8,uint8): 10, 3 -> 1
|
||||
// g(uint8,uint8): 10, 2 -> 0
|
||||
// g(uint8,uint8): 11, 2 -> 1
|
||||
// g(uint8,uint8): 2, 2 -> 0
|
||||
// g(uint8,uint8): 1, 0 -> FAILURE
|
||||
// g(uint8,uint8): 0, 0 -> FAILURE
|
||||
// g(uint8,uint8): 0, 1 -> 0
|
@ -0,0 +1,35 @@
|
||||
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: only
|
||||
// ----
|
||||
// f(int256,int256): 10, 3 -> 1
|
||||
// f(int256,int256): 10, 2 -> 0
|
||||
// f(int256,int256): 11, 2 -> 1
|
||||
// f(int256,int256): -10, 3 -> -1
|
||||
// f(int256,int256): 10, -3 -> 1
|
||||
// f(int256,int256): -10, -3 -> -1
|
||||
// f(int256,int256): 2, 2 -> 0
|
||||
// f(int256,int256): 1, 0 -> FAILURE
|
||||
// f(int256,int256): -1, 0 -> FAILURE
|
||||
// f(int256,int256): 0, 0 -> FAILURE
|
||||
// f(int256,int256): 0, 1 -> 0
|
||||
// f(int256,int256): 0, -1 -> 0
|
||||
// g(int8,int8): 10, 3 -> 1
|
||||
// g(int8,int8): 10, 2 -> 0
|
||||
// g(int8,int8): 11, 2 -> 1
|
||||
// g(int8,int8): -10, 3 -> -1
|
||||
// g(int8,int8): 10, -3 -> 1
|
||||
// g(int8,int8): -10, -3 -> -1
|
||||
// g(int8,int8): 2, 2 -> 0
|
||||
// g(int8,int8): 1, 0 -> FAILURE
|
||||
// g(int8,int8): -1, 0 -> FAILURE
|
||||
// g(int8,int8): 0, 0 -> FAILURE
|
||||
// g(int8,int8): 0, 1 -> 0
|
||||
// g(int8,int8): 0, -1 -> 0
|
Loading…
Reference in New Issue
Block a user