1*4975cfecSriastradh /* $NetBSD: overflow.h,v 1.2 2021/12/19 12:20:53 riastradh Exp $ */
2cfcba743Sriastradh
3cfcba743Sriastradh /*-
4cfcba743Sriastradh * Copyright (c) 2018 The NetBSD Foundation, Inc.
5cfcba743Sriastradh * All rights reserved.
6cfcba743Sriastradh *
7cfcba743Sriastradh * This code is derived from software contributed to The NetBSD Foundation
8cfcba743Sriastradh * by Taylor R. Campbell.
9cfcba743Sriastradh *
10cfcba743Sriastradh * Redistribution and use in source and binary forms, with or without
11cfcba743Sriastradh * modification, are permitted provided that the following conditions
12cfcba743Sriastradh * are met:
13cfcba743Sriastradh * 1. Redistributions of source code must retain the above copyright
14cfcba743Sriastradh * notice, this list of conditions and the following disclaimer.
15cfcba743Sriastradh * 2. Redistributions in binary form must reproduce the above copyright
16cfcba743Sriastradh * notice, this list of conditions and the following disclaimer in the
17cfcba743Sriastradh * documentation and/or other materials provided with the distribution.
18cfcba743Sriastradh *
19cfcba743Sriastradh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20cfcba743Sriastradh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21cfcba743Sriastradh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22cfcba743Sriastradh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23cfcba743Sriastradh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24cfcba743Sriastradh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25cfcba743Sriastradh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26cfcba743Sriastradh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27cfcba743Sriastradh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28cfcba743Sriastradh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29cfcba743Sriastradh * POSSIBILITY OF SUCH DAMAGE.
30cfcba743Sriastradh */
31cfcba743Sriastradh
32cfcba743Sriastradh #ifndef _LINUX_OVERFLOW_H_
33cfcba743Sriastradh #define _LINUX_OVERFLOW_H_
34cfcba743Sriastradh
35cfcba743Sriastradh #include <sys/types.h>
36cfcba743Sriastradh
37cfcba743Sriastradh #include <lib/libkern/libkern.h> /* offsetof */
38cfcba743Sriastradh
39cfcba743Sriastradh #define check_mul_overflow(a, b, res) __builtin_mul_overflow(a, b, res)
40cfcba743Sriastradh #define check_add_overflow(a, b, res) __builtin_add_overflow(a, b, res)
41cfcba743Sriastradh
42cfcba743Sriastradh /* return x*y saturated at SIZE_MAX */
43cfcba743Sriastradh static inline size_t
array_size(size_t x,size_t y)44cfcba743Sriastradh array_size(size_t x, size_t y)
45cfcba743Sriastradh {
46cfcba743Sriastradh size_t xy;
47cfcba743Sriastradh
48cfcba743Sriastradh if (check_mul_overflow(x, y, &xy))
49cfcba743Sriastradh return SIZE_MAX;
50cfcba743Sriastradh return xy;
51cfcba743Sriastradh }
52cfcba743Sriastradh
53cfcba743Sriastradh /* return x*y*z saturated at SIZE_MAX */
54cfcba743Sriastradh static inline size_t
array3_size(size_t x,size_t y,size_t z)55cfcba743Sriastradh array3_size(size_t x, size_t y, size_t z)
56cfcba743Sriastradh {
57cfcba743Sriastradh size_t xy, xyz;
58cfcba743Sriastradh
59cfcba743Sriastradh if (check_mul_overflow(x, y, &xy))
60cfcba743Sriastradh return SIZE_MAX;
61cfcba743Sriastradh if (check_mul_overflow(xy, z, &xyz))
62cfcba743Sriastradh return SIZE_MAX;
63cfcba743Sriastradh return xyz;
64cfcba743Sriastradh }
65cfcba743Sriastradh
66cfcba743Sriastradh /* return basesize + elemsize*nelem saturated at SIZE_MAX */
67cfcba743Sriastradh static inline size_t
__struct_size(size_t basesize,size_t elemsize,size_t nelem)68cfcba743Sriastradh __struct_size(size_t basesize, size_t elemsize, size_t nelem)
69cfcba743Sriastradh {
70cfcba743Sriastradh size_t arraysize, totalsize;
71cfcba743Sriastradh
72cfcba743Sriastradh KASSERT(elemsize);
73cfcba743Sriastradh if ((arraysize = array_size(elemsize, nelem)) == SIZE_MAX)
74cfcba743Sriastradh return SIZE_MAX;
75cfcba743Sriastradh if (check_add_overflow(basesize, arraysize, &totalsize))
76cfcba743Sriastradh return SIZE_MAX;
77cfcba743Sriastradh return totalsize;
78cfcba743Sriastradh }
79cfcba743Sriastradh
80cfcba743Sriastradh #define struct_size(p, member, n) \
81cfcba743Sriastradh ({ \
82*4975cfecSriastradh CTASSERT(sizeof(*(p)) >= offsetof(__typeof__(*(p)), member)); \
83cfcba743Sriastradh __struct_size(sizeof(*(p)), sizeof((p)->member[0]), (n)); \
84cfcba743Sriastradh })
85cfcba743Sriastradh
86cfcba743Sriastradh #endif /* _LINUX_OVERFLOW_H_ */
87