coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
assert.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef __ASSERT_H__
4 #define __ASSERT_H__
5 
6 #include <arch/hlt.h>
7 #include <console/console.h>
8 #include <stdint.h>
9 
10 /* TODO: Fix vendorcode headers to not define macros coreboot uses or to be more
11  properly isolated. */
12 #ifdef ASSERT
13 #undef ASSERT
14 #endif
15 
16 /* Do not use filenames nor line numbers on timeless builds, to preserve reproducibility */
17 #if ENV_TIMELESS
18 #define __ASSERT_FILE__ "(filenames not available on timeless builds)"
19 #define __ASSERT_LINE__ 404
20 #else
21 #define __ASSERT_FILE__ __FILE__
22 #define __ASSERT_LINE__ __LINE__
23 #endif
24 
25 #ifndef _PORTING_H_ /* TODO: Isolate AGESA properly. */
26 #define __build_time_assert(x) \
27  (__builtin_constant_p(x) ? ((x) ? 1 : dead_code_t(int)) : 0)
28 #else
29 #define __build_time_assert(x) 0
30 #endif
31 
32 /* CMocka function redefinition. */
33 void mock_assert(const int result, const char *const expression,
34  const char *const file, const int line);
35 
36 #if ENV_TEST
37 #define MOCK_ASSERT(result, expression) \
38  mock_assert((result), (expression), __ASSERT_FILE__, __ASSERT_LINE__)
39 #else
40 #define MOCK_ASSERT(result, expression)
41 #endif
42 
43 /* GCC and CAR versions */
44 #define ASSERT(x) { \
45  if (!__build_time_assert(x) && !(x)) { \
46  printk(BIOS_EMERG, \
47  "ASSERTION ERROR: file '%s', line %d\n", \
48  __ASSERT_FILE__, __ASSERT_LINE__); \
49  MOCK_ASSERT(!!(x), #x); \
50  if (CONFIG(FATAL_ASSERTS)) \
51  hlt(); \
52  } \
53 }
54 #define ASSERT_MSG(x, msg) { \
55  if (!__build_time_assert(x) && !(x)) { \
56  printk(BIOS_EMERG, \
57  "ASSERTION ERROR: file '%s', line %d\n", \
58  __ASSERT_FILE__, __ASSERT_LINE__); \
59  printk(BIOS_EMERG, "%s", msg); \
60  MOCK_ASSERT(!!(x), (msg)); \
61  if (CONFIG(FATAL_ASSERTS)) \
62  hlt(); \
63  } \
64 }
65 #define BUG() { \
66  printk(BIOS_EMERG, \
67  "ERROR: BUG ENCOUNTERED at file '%s', line %d\n", \
68  __ASSERT_FILE__, __ASSERT_LINE__); \
69  MOCK_ASSERT(0, "BUG ENCOUNTERED"); \
70  if (CONFIG(FATAL_ASSERTS)) \
71  hlt(); \
72 }
73 
74 #define assert(statement) ASSERT(statement)
75 
76 /*
77  * These macros can be used to assert that a certain branch of code is dead and
78  * will be compile-time eliminated. This differs from _Static_assert(), which
79  * will generate a compiler error even if the scope it was called from is dead
80  * code. This may be useful to double-check things like constants that are only
81  * valid if a certain Kconfig option is set.
82  *
83  * The error message when this hits will look like this:
84  *
85  * ramstage/lib/bootmode.o: In function `display_init_required':
86  * bootmode.c:42: undefined reference to `_dead_code_assertion_failed'
87  */
88 extern void _dead_code_assertion_failed(void) __attribute__((noreturn));
89 #define dead_code() _dead_code_assertion_failed()
90 
91 /* This can be used in the context of an expression of type 'type'. */
92 #define dead_code_t(type) ({ \
93  dead_code(); \
94  *(type *)(uintptr_t)0; \
95 })
96 
97 #if ENV_X86_64
98 #define pointer_to_uint32_safe(x) ({ \
99  if ((uintptr_t)(x) > 0xffffffffUL) \
100  die("Cast from pointer to uint32_t overflows"); \
101  (uint32_t)(uintptr_t)(x); \
102 })
103 #else
104 #define pointer_to_uint32_safe(x) ({ \
105  (uint32_t)(uintptr_t)(x); \
106 })
107 #endif
108 #endif // __ASSERT_H__
void mock_assert(const int result, const char *const expression, const char *const file, const int line)
void _dead_code_assertion_failed(void)
result
Definition: mrc_cache.c:35
Definition: gcov-glue.c:7