Fix signed integer overflow in MakeKernelParams8bit Compute `prod_zp_depth` via `uint32_t` casts so the intended wraparound is well-defined rather than UB. Overflow checks on the surrounding offset/pointer computations are left untouched. See chromium issue: https://issues.chromium.org/issues/526978330 and https://issues.chromium.org/issues/520870343 Closes https://github.com/google/ruy/pull/373 COPYBARA_INTEGRATE_REVIEW=https://github.com/google/ruy/pull/373 from wangw-1991:fix_integer_overflow 781542ce3bf209d415e19d73a65e89bf073f3617 PiperOrigin-RevId: 952143874
diff --git a/ruy/kernel_common.h b/ruy/kernel_common.h index 69e819b..d4cf293 100644 --- a/ruy/kernel_common.h +++ b/ruy/kernel_common.h
@@ -177,7 +177,13 @@ params->rhs_zero_point = rhs.zero_point; params->dst_zero_point = dst->zero_point; params->depth = depth; - params->prod_zp_depth = lhs.zero_point * rhs.zero_point * depth; + // prod_zp_depth intentionally relies on two's-complement wraparound to match + // the wrapping arithmetic performed by the asm kernels. Compute it in unsigned + // so the overflow is well-defined. + params->prod_zp_depth = static_cast<std::int32_t>( + static_cast<std::uint32_t>(lhs.zero_point) * + static_cast<std::uint32_t>(rhs.zero_point) * + static_cast<std::uint32_t>(depth)); params->flags |= RUY_ASM_FLAG_NEEDS_LEFT_SHIFT; if (mul_params.multiplier_fixedpoint_perchannel()) { // Temporary release-assert to debug some crashes in an application.