2021-01-21 15:20:22 +00:00
|
|
|
from opcodes import AND, ISZERO, GT, DIV
|
2019-06-14 15:47:54 +00:00
|
|
|
from rule import Rule
|
2021-01-21 15:20:22 +00:00
|
|
|
from util import BVUnsignedUpCast, BVUnsignedMax
|
|
|
|
from z3 import BitVec, Not, BVMulNoOverflow
|
2019-06-14 15:47:54 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Overflow checked unsigned integer multiplication.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# Approximation with 16-bit base types.
|
|
|
|
n_bits = 16
|
|
|
|
type_bits = 8
|
|
|
|
|
|
|
|
while type_bits <= n_bits:
|
|
|
|
|
|
|
|
rule = Rule()
|
|
|
|
|
|
|
|
# Input vars
|
|
|
|
X_short = BitVec('X', type_bits)
|
|
|
|
Y_short = BitVec('Y', type_bits)
|
|
|
|
|
|
|
|
# Z3's overflow condition
|
|
|
|
actual_overflow = Not(BVMulNoOverflow(X_short, Y_short, False))
|
|
|
|
|
|
|
|
# cast to full n_bits values
|
|
|
|
X = BVUnsignedUpCast(X_short, n_bits)
|
|
|
|
Y = BVUnsignedUpCast(Y_short, n_bits)
|
|
|
|
|
|
|
|
# Constants
|
|
|
|
maxValue = BVUnsignedMax(type_bits, n_bits)
|
|
|
|
|
|
|
|
# Overflow check in YulUtilFunction::overflowCheckedIntMulFunction
|
|
|
|
overflow_check = AND(ISZERO(ISZERO(X)), GT(Y, DIV(maxValue, X)))
|
|
|
|
|
|
|
|
rule.check(overflow_check != 0, actual_overflow)
|
|
|
|
|
|
|
|
type_bits *= 2
|