2022-06-03 20:04:16 +00:00
|
|
|
from opcodes import GT, ADD
|
2019-06-14 15:47:54 +00:00
|
|
|
from rule import Rule
|
2021-01-21 15:20:22 +00:00
|
|
|
from util import BVUnsignedMax, BVUnsignedUpCast
|
|
|
|
from z3 import BitVec, BVAddNoOverflow, Not
|
2019-06-14 15:47:54 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
Overflow checked unsigned integer addition.
|
|
|
|
"""
|
|
|
|
|
|
|
|
n_bits = 256
|
|
|
|
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(BVAddNoOverflow(X_short, Y_short, False))
|
|
|
|
|
|
|
|
# cast to full n_bits values
|
|
|
|
X = BVUnsignedUpCast(X_short, n_bits)
|
|
|
|
Y = BVUnsignedUpCast(Y_short, n_bits)
|
2022-06-03 20:04:16 +00:00
|
|
|
sum_ = ADD(X, Y)
|
2019-06-14 15:47:54 +00:00
|
|
|
|
|
|
|
# Constants
|
|
|
|
maxValue = BVUnsignedMax(type_bits, n_bits)
|
|
|
|
|
|
|
|
# Overflow check in YulUtilFunction::overflowCheckedIntAddFunction
|
2022-06-03 20:04:16 +00:00
|
|
|
if type_bits == 256:
|
|
|
|
overflow_check = GT(X, sum_)
|
|
|
|
else:
|
|
|
|
overflow_check = GT(sum_, maxValue)
|
2019-06-14 15:47:54 +00:00
|
|
|
|
2022-06-03 20:04:16 +00:00
|
|
|
type_bits += 8
|
2019-06-14 15:47:54 +00:00
|
|
|
|
2022-06-03 20:04:16 +00:00
|
|
|
rule.check(overflow_check != 0, actual_overflow)
|