overflow: Introduce wrapping_add(), wrapping_sub(), and wrapping_mul()
authorKees Cook <keescook@chromium.org>
Fri, 19 Jan 2024 00:05:52 +0000 (16:05 -0800)
committerKees Cook <keescook@chromium.org>
Thu, 29 Feb 2024 21:38:01 +0000 (13:38 -0800)
commitd70de8054c58d7bd9a4654c9f4797d29fa46d545
treebcc66c9026a9407845dca2827111bf619c092626
parent3e19086fb5a9079611de426e8cb2f4503e28757e
overflow: Introduce wrapping_add(), wrapping_sub(), and wrapping_mul()

Provide helpers that will perform wrapping addition, subtraction, or
multiplication without tripping the arithmetic wrap-around sanitizers. The
first argument is the type under which the wrap-around should happen
with. In other words, these two calls will get very different results:

wrapping_mul(int, 50, 50) == 2500
wrapping_mul(u8,  50, 50) ==  196

Add to the selftests to validate behavior and lack of side-effects.

Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Reviewed-by: Marco Elver <elver@google.com>
Acked-by: Mark Rutland <mark.rutland@arm.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
include/linux/overflow.h
lib/overflow_kunit.c