XenevaOS
Loading...
Searching...
No Matches
Macros
overflow.h File Reference
#include <linux/compiler.h>
#include <linux/limits.h>
#include <linux/const.h>
Include dependency graph for overflow.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define __type_half_max(type)   ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))
 
#define __type_max(T)   ((T)((__type_half_max(T) - 1) + __type_half_max(T)))
 
#define type_max(t)   __type_max(typeof(t))
 
#define __type_min(T)   ((T)((T)-type_max(T)-(T)1))
 
#define type_min(t)   __type_min(typeof(t))
 
#define check_add_overflow(a, b, d)    __must_check_overflow(__builtin_add_overflow(a, b, d))
 
#define wrapping_add(type, a, b)
 
#define wrapping_assign_add(var, offset)
 
#define check_sub_overflow(a, b, d)    __must_check_overflow(__builtin_sub_overflow(a, b, d))
 
#define wrapping_sub(type, a, b)
 
#define wrapping_assign_sub(var, offset)
 
#define check_mul_overflow(a, b, d)    __must_check_overflow(__builtin_mul_overflow(a, b, d))
 
#define wrapping_mul(type, a, b)
 
#define check_shl_overflow(a, s, d)
 
#define __overflows_type_constexpr(x, T)
 
#define __overflows_type(x, T)
 
#define overflows_type(n, T)
 
#define range_overflows(start, size, max)
 
#define range_overflows_t(type, start, size, max)    range_overflows((type)(start), (type)(size), (type)(max))
 
#define range_end_overflows(start, size, max)
 
#define range_end_overflows_t(type, start, size, max)    range_end_overflows((type)(start), (type)(size), (type)(max))
 
#define castable_to_type(n, T)
 
#define array_size(a, b)   size_mul(a, b)
 
#define array3_size(a, b, c)   size_mul(size_mul(a, b), c)
 
#define flex_array_size(p, member, count)
 
#define struct_size(p, member, count)
 
#define struct_size_t(type, member, count)    struct_size((type *)NULL, member, count)
 
#define struct_offset(p, member)   (offsetof(typeof(*(p)), member))
 
: Name for a variable to define.

DEFINE_RAW_FLEX() - Define an on-stack instance of structure with a trailing flexible array member, when it does not have a __counted_by annotation.

@type: structure type name, including "struct" keyword.

@member: Name of the array member. @count: Number of elements in the array; must be compile-time const.

Define a zeroed, on-stack, instance of @type structure with a trailing flexible array member. Use __struct_size(

#define __DEFINE_FLEX(type, name, member, count, trailer...)
 
#define _DEFINE_FLEX(type, name, member, count, initializer...)    __DEFINE_FLEX(type, name, member, count, = { .obj initializer })
 
, @member) to get compile-time number of

DEFINE_FLEX() - Define an on-stack instance of structure with a trailing flexible array member.

@TYPE: structure type name, including "struct" keyword. @NAME: Name for a variable to define. @MEMBER: Name of the array member. @COUNTER: Name of the __counted_by member. @COUNT: Number of elements in the array; must be compile-time const.

Define a zeroed, on-stack, instance of @TYPE structure with a trailing flexible array member. Use __struct_size(@NAME) to get compile-time size of it afterwards. Use __member_size(@NAME->member) to get compile-time size of @NAME members. Use STACK_FLEX_ARRAY_SIZE( elements in array @member.

#define DEFINE_RAW_FLEX(type, name, member, count)    __DEFINE_FLEX(type, name, member, count, = { })
 
#define DEFINE_FLEX(TYPE, NAME, MEMBER, COUNTER, COUNT)    _DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .COUNTER = COUNT, })
 
: Name for a variable defined in DEFINE_RAW_FLEX()/DEFINE_FLEX().

STACK_FLEX_ARRAY_SIZE() - helper macro for DEFINE_FLEX() family. Returns the number of elements in @array.

@array: Name of the array member.

#define STACK_FLEX_ARRAY_SIZE(name, array)
 
#define typeof_flex_counter(FAM)
 
#define overflows_flex_counter_type(TYPE, FAM, COUNT)    (overflows_type(COUNT, typeof_flex_counter(((TYPE *)NULL)->FAM)))
 
#define __set_flex_counter(FAM, COUNT)
 

Macro Definition Documentation

◆ __DEFINE_FLEX

#define __DEFINE_FLEX (   type,
  name,
  member,
  count,
  trailer... 
)
Value:
_Static_assert(__builtin_constant_p(count), \
"onstack flex array members require compile-time const count"); \
union { \
u8 bytes[struct_size_t(type, member, count)]; \
type obj; \
} name##_u trailer; \
type *name = (type *)&name##_u
struct drm_crtc struct drm_plane struct drm_plane const struct drm_crtc_funcs const char * name
Definition drm_crtc.h:1242
uint8_t u8
Definition kernel.h:14
#define struct_size_t(type, member, count)
Definition overflow.h:452

◆ __overflows_type

#define __overflows_type (   x,
  T 
)
Value:
({ \
typeof(T) v = 0; \
check_add_overflow((x), v, &v); \
})

◆ __overflows_type_constexpr

#define __overflows_type_constexpr (   x,
  T 
)
Value:
( \
is_unsigned_type(typeof(x)) ? \
(x) > type_max(T) : \
is_unsigned_type(typeof(T)) ? \
(x) < 0 || (x) > type_max(T) : \
(x) < type_min(T) || (x) > type_max(T))
int x
Definition layout.c:118
#define type_max(t)
Definition overflow.h:35
#define type_min(t)
Definition overflow.h:37

◆ __set_flex_counter

#define __set_flex_counter (   FAM,
  COUNT 
)
Value:
({ \
*_Generic(__flex_counter(FAM), \
void *: &(size_t){ 0 }, \
default: __flex_counter(FAM)) = (COUNT); \
})
uint64_t size_t
Definition stdint.h:104
#define __flex_counter(FAM)
Definition compiler_types.h:571

__set_flex_counter() - Set the counter associated with the given flexible array member that has been annoated by __counted_by(). @FAM: Instance of flexible array member within a given struct. @COUNT: Value to store to the __counted_by annotated @FAM_PTR's counter.

This is a no-op if no annotation exists. Count needs to be checked with overflows_flex_counter_type() before using this function.

◆ __type_half_max

#define __type_half_max (   type)    ((type)1 << (8*sizeof(type) - 1 - is_signed_type(type)))

◆ __type_max

#define __type_max (   T)    ((T)((__type_half_max(T) - 1) + __type_half_max(T)))

◆ __type_min

#define __type_min (   T)    ((T)((T)-type_max(T)-(T)1))

◆ _DEFINE_FLEX

#define _DEFINE_FLEX (   type,
  name,
  member,
  count,
  initializer... 
)     __DEFINE_FLEX(type, name, member, count, = { .obj initializer })

◆ array3_size

#define array3_size (   a,
  b,
  c 
)    size_mul(size_mul(a, b), c)

array3_size() - Calculate size of 3-dimensional array. : dimension one : dimension two : dimension three

Calculates size of 3-dimensional array: * * .

Returns: number of bytes needed to represent the array or SIZE_MAX on overflow.

◆ array_size

#define array_size (   a,
  b 
)    size_mul(a, b)

array_size() - Calculate size of 2-dimensional array. : dimension one : dimension two

Calculates size of 2-dimensional array: * .

Returns: number of bytes needed to represent the array or SIZE_MAX on overflow.

◆ castable_to_type

#define castable_to_type (   n,
  T 
)
Value:
__builtin_choose_expr(__is_constexpr(n), \
__same_type(n, T))
#define __same_type(a, b)
Definition compiler.h:154
#define __is_constexpr(x)
Definition compiler.h:231
#define __overflows_type_constexpr(x, T)
Definition overflow.h:204

castable_to_type - like __same_type(), but also allows for casted literals


: variable or constant value @T: variable or data type

Unlike the __same_type() macro, this allows a constant value as the first argument. If this value would not overflow into an assignment of the second argument's type, it returns true. Otherwise, this falls back to __same_type().

◆ check_add_overflow

#define check_add_overflow (   a,
  b,
  d 
)     __must_check_overflow(__builtin_add_overflow(a, b, d))

check_add_overflow() - Calculate addition with overflow checking : first addend : second addend @d: pointer to store sum

Returns true on wrap-around, false otherwise.

*@d holds the results of the attempted addition, regardless of whether wrap-around occurred.

◆ check_mul_overflow

#define check_mul_overflow (   a,
  b,
  d 
)     __must_check_overflow(__builtin_mul_overflow(a, b, d))

check_mul_overflow() - Calculate multiplication with overflow checking : first factor : second factor @d: pointer to store product

Returns true on wrap-around, false otherwise.

*@d holds the results of the attempted multiplication, regardless of whether wrap-around occurred.

◆ check_shl_overflow

#define check_shl_overflow (   a,
  s,
  d 
)
Value:
__must_check_overflow(({ \
typeof(a) _a = a; \
typeof(s) _s = s; \
typeof(d) _d = d; \
unsigned long long _a_full = _a; \
unsigned int _to_shift = \
_s >= 0 && _s < 8 * sizeof(*d) ? _s : 0; \
*_d = (_a_full << _to_shift); \
(_to_shift != _s || *_d < 0 || _a < 0 || \
(*_d >> _to_shift) != _a); \
}))
size_t a
Definition main.cpp:156
#define d
Definition libunicode.c:1833

check_shl_overflow() - Calculate a left-shifted value and check overflow : Value to be shifted @s: How many bits left to shift @d: Pointer to where to store the result

Computes *@d = (<< @s)

Returns true if '*@d' cannot hold the result or when '<< @s' doesn't make sense. Example conditions:

  • '<< @s' causes bits to be lost when stored in *@d.
  • '@s' is garbage (e.g. negative) or so large that the result of '<< @s' is guaranteed to be 0.
  • '' is negative.
  • '<< @s' sets the sign bit, if any, in '*@d'.

'*@d' will hold the results of the attempted shift, but is not considered "safe for use" if true is returned.

◆ check_sub_overflow

#define check_sub_overflow (   a,
  b,
  d 
)     __must_check_overflow(__builtin_sub_overflow(a, b, d))

check_sub_overflow() - Calculate subtraction with overflow checking : minuend; value to subtract from : subtrahend; value to subtract from @d : pointer to store difference

Returns true on wrap-around, false otherwise.

*@d holds the results of the attempted subtraction, regardless of whether wrap-around occurred.

◆ DEFINE_FLEX

#define DEFINE_FLEX (   TYPE,
  NAME,
  MEMBER,
  COUNTER,
  COUNT 
)     _DEFINE_FLEX(TYPE, NAME, MEMBER, COUNT, = { .COUNTER = COUNT, })

◆ DEFINE_RAW_FLEX

#define DEFINE_RAW_FLEX (   type,
  name,
  member,
  count 
)     __DEFINE_FLEX(type, name, member, count, = { })

◆ flex_array_size

#define flex_array_size (   p,
  member,
  count 
)
Value:
__builtin_choose_expr(__is_constexpr(count), \
(count) * sizeof(*(p)->member) + __must_be_array((p)->member), \
size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member)))
#define __must_be_array(a)
Definition compiler.h:226

flex_array_size() - Calculate size of a flexible array member within an enclosing structure. : Pointer to the structure. @member: Name of the flexible array member. @count: Number of elements in the array.

Calculates size of a flexible array of @count number of @member elements, at the end of structure .

Return: number of bytes needed or SIZE_MAX on overflow.

◆ overflows_flex_counter_type

#define overflows_flex_counter_type (   TYPE,
  FAM,
  COUNT 
)     (overflows_type(COUNT, typeof_flex_counter(((TYPE *)NULL)->FAM)))

overflows_flex_counter_type() - Check if the counter associated with the given flexible array member can represent a value. @TYPE: Type of the struct that contains the @FAM. @FAM: Member name of the FAM within @TYPE. @COUNT: Value to check against the __counted_by annotated @FAM's counter.

Returns: true if @COUNT can be represented in the @FAM's counter. When @FAM is not annotated with __counted_by(), always returns true.

◆ overflows_type

#define overflows_type (   n,
  T 
)
Value:
__builtin_choose_expr(__is_constexpr(n), \
#define __overflows_type(x, T)
Definition overflow.h:211

overflows_type - helper for checking the overflows between value, variables, or data type


: source constant value or variable to be checked @T: destination variable or data type proposed to store @x

Compares the @x expression for whether or not it can safely fit in the storage of the type in @T. @x and @T can have different types. If @x is a constant expression, this will also resolve to a constant expression.

Returns: true if overflow can occur, false otherwise.

◆ range_end_overflows

#define range_end_overflows (   start,
  size,
  max 
)
Value:
({ \
typeof(start) start__ = (start); \
typeof(size) size__ = (size); \
typeof(max) max__ = (max); \
(void)(&start__ == &size__); \
(void)(&start__ == &max__); \
start__ > max__ || size__ > max__ - start__; \
})
size_t size
Definition drm_bridge.h:1299
#define max(a, b)
Definition minmax.h:28

range_end_overflows() - Check if a range's endpoint is out of bounds @start: Start of the range. @size: Size of the range. @max: Exclusive upper boundary.

Checks only if the endpoint of a range (@start + @size) exceeds @max. Unlike range_overflows(), a zero-sized range at the boundary (@start == @max) is not considered an overflow. Useful for iterator-style checks.

Returns: true if the endpoint exceeds the boundary.

◆ range_end_overflows_t

#define range_end_overflows_t (   type,
  start,
  size,
  max 
)     range_end_overflows((type)(start), (type)(size), (type)(max))

range_end_overflows_t() - Check if a range's endpoint is out of bounds @type: Data type to use. @start: Start of the range. @size: Size of the range. @max: Exclusive upper boundary.

Same as range_end_overflows() but forcing the parameters to @type.

Returns: true if the endpoint exceeds the boundary.

◆ range_overflows

#define range_overflows (   start,
  size,
  max 
)
Value:
({ \
typeof(start) start__ = (start); \
typeof(size) size__ = (size); \
typeof(max) max__ = (max); \
(void)(&start__ == &size__); \
(void)(&start__ == &max__); \
start__ >= max__ || size__ > max__ - start__; \
})

range_overflows() - Check if a range is out of bounds @start: Start of the range. @size: Size of the range. @max: Exclusive upper boundary.

A strict check to determine if the range [@start, @start + @size) is invalid with respect to the allowable range [0, @max). Any range starting at or beyond @max is considered an overflow, even if @size is 0.

Returns: true if the range is out of bounds.

◆ range_overflows_t

#define range_overflows_t (   type,
  start,
  size,
  max 
)     range_overflows((type)(start), (type)(size), (type)(max))

range_overflows_t() - Check if a range is out of bounds @type: Data type to use. @start: Start of the range. @size: Size of the range. @max: Exclusive upper boundary.

Same as range_overflows() but forcing the parameters to @type.

Returns: true if the range is out of bounds.

◆ STACK_FLEX_ARRAY_SIZE

#define STACK_FLEX_ARRAY_SIZE (   name,
  array 
)
Value:
(__member_size((name)->array) / sizeof(*(name)->array) + \
__must_be_array((name)->array))
#define __member_size(p)
Definition compiler_types.h:529

◆ struct_offset

#define struct_offset (   p,
  member 
)    (offsetof(typeof(*(p)), member))

struct_offset() - Calculate the offset of a member within a struct : Pointer to the struct @member: Name of the member to get the offset of

Calculates the offset of a particular @member of the structure pointed to by .

Return: number of bytes to the location of @member.

◆ struct_size

#define struct_size (   p,
  member,
  count 
)
Value:
__builtin_choose_expr(__is_constexpr(count), \
sizeof(*(p)) + flex_array_size(p, member, count), \
size_add(sizeof(*(p)), flex_array_size(p, member, count)))
#define flex_array_size(p, member, count)
Definition overflow.h:418

struct_size() - Calculate size of structure with trailing flexible array. : Pointer to the structure. @member: Name of the array member. @count: Number of elements in the array.

Calculates size of memory needed for structure of followed by an array of @count number of @member elements.

Return: number of bytes needed or SIZE_MAX on overflow.

◆ struct_size_t

#define struct_size_t (   type,
  member,
  count 
)     struct_size((type *)NULL, member, count)

struct_size_t() - Calculate size of structure with trailing flexible array @type: structure type name. @member: Name of the array member. @count: Number of elements in the array.

Calculates size of memory needed for structure @type followed by an array of @count number of @member elements. Prefer using struct_size() when possible instead, to keep calculations associated with a specific instance variable of type @type.

Return: number of bytes needed or SIZE_MAX on overflow.

◆ type_max

#define type_max (   t)    __type_max(typeof(t))

◆ type_min

#define type_min (   t)    __type_min(typeof(t))

◆ typeof_flex_counter

#define typeof_flex_counter (   FAM)
Value:
typeof(_Generic(__flex_counter(FAM), \
void *: (size_t)0, \
default: *__flex_counter(FAM)))

typeof_flex_counter() - Return the type of the counter variable of a given flexible array member annotated by __counted_by(). @FAM: Instance of flexible array member within a given struct.

Returns: "size_t" if no annotation exists.

◆ wrapping_add

#define wrapping_add (   type,
  a,
  b 
)
Value:
({ \
type __val; \
__builtin_add_overflow(a, b, &__val); \
__val; \
})
size_t b
Definition main.cpp:156

wrapping_add() - Intentionally perform a wrapping addition @type: type for result of calculation : first addend : second addend

Return the potentially wrapped-around addition without tripping any wrap-around sanitizers that may be enabled.

◆ wrapping_assign_add

#define wrapping_assign_add (   var,
  offset 
)
Value:
({ \
typeof(var) *__ptr = &(var); \
*__ptr = wrapping_add(typeof(var), *__ptr, offset); \
})
size_t size_t offset
Definition drm_bridge.h:1299
#define wrapping_add(type, a, b)
Definition overflow.h:73

◆ wrapping_assign_sub

#define wrapping_assign_sub (   var,
  offset 
)
Value:
({ \
typeof(var) *__ptr = &(var); \
*__ptr = wrapping_sub(typeof(var), *__ptr, offset); \
})
#define wrapping_sub(type, a, b)
Definition overflow.h:119

◆ wrapping_mul

#define wrapping_mul (   type,
  a,
  b 
)
Value:
({ \
type __val; \
__builtin_mul_overflow(a, b, &__val); \
__val; \
})

wrapping_mul() - Intentionally perform a wrapping multiplication @type: type for result of calculation : first factor : second factor

Return the potentially wrapped-around multiplication without tripping any wrap-around sanitizers that may be enabled.

◆ wrapping_sub

#define wrapping_sub (   type,
  a,
  b 
)
Value:
({ \
type __val; \
__builtin_sub_overflow(a, b, &__val); \
__val; \
})

wrapping_sub() - Intentionally perform a wrapping subtraction @type: type for result of calculation : minuend; value to subtract from : subtrahend; value to subtract from

Return the potentially wrapped-around subtraction without tripping any wrap-around sanitizers that may be enabled.