summaryrefslogtreecommitdiffstats
path: root/src/include/assert.h
blob: 93d6bfc41276a5eedf1467f346d1ae298d0cc697 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* SPDX-License-Identifier: GPL-2.0-only */

#ifndef __ASSERT_H__
#define __ASSERT_H__

#include <arch/hlt.h>
#include <console/console.h>
#include <stdint.h>

/* TODO: Fix vendorcode headers to not define macros coreboot uses or to be more
   properly isolated. */
#ifdef ASSERT
#undef ASSERT
#endif

/* Do not use filenames nor line numbers on timeless builds, to preserve reproducibility */
#if ENV_TIMELESS
#define __ASSERT_FILE__ "(filenames not available on timeless builds)"
#define __ASSERT_LINE__ 404
#else
#define __ASSERT_FILE__ __FILE__
#define __ASSERT_LINE__ __LINE__
#endif

#ifndef _PORTING_H_	/* TODO: Isolate AGESA properly. */
#define __build_time_assert(x) \
	(__builtin_constant_p(x) ? ((x) ? 1 : dead_code_t(int)) : 0)
#else
#define __build_time_assert(x) 0
#endif

/* CMocka function redefinition. */
void mock_assert(const int result, const char *const expression,
		const char *const file, const int line);

#if ENV_TEST
#define MOCK_ASSERT(result, expression) \
	mock_assert((result), (expression), __ASSERT_FILE__, __ASSERT_LINE__)
#else
#define MOCK_ASSERT(result, expression)
#endif

/* GCC and CAR versions */
#define ASSERT(x) {							\
	if (!__build_time_assert(x) && !(x)) {				\
		printk(BIOS_EMERG,					\
			"ASSERTION ERROR: file '%s', line %d\n",	\
			__ASSERT_FILE__, __ASSERT_LINE__);		\
		MOCK_ASSERT(!!(x), #x);					\
		if (CONFIG(FATAL_ASSERTS))				\
			hlt();						\
	}								\
}
#define ASSERT_MSG(x, msg) {						\
	if (!__build_time_assert(x) && !(x)) {				\
		printk(BIOS_EMERG,					\
			"ASSERTION ERROR: file '%s', line %d\n",	\
			__ASSERT_FILE__, __ASSERT_LINE__);		\
		printk(BIOS_EMERG, "%s", msg);				\
		MOCK_ASSERT(!!(x), (msg));				\
		if (CONFIG(FATAL_ASSERTS))				\
			hlt();						\
	}								\
}
#define BUG() {								\
	printk(BIOS_EMERG,						\
		"ERROR: BUG ENCOUNTERED at file '%s', line %d\n",	\
		__ASSERT_FILE__, __ASSERT_LINE__);			\
	MOCK_ASSERT(0, "BUG ENCOUNTERED");				\
	if (CONFIG(FATAL_ASSERTS))					\
		hlt();							\
}

#define assert(statement)	ASSERT(statement)

/*
 * These macros can be used to assert that a certain branch of code is dead and
 * will be compile-time eliminated. This differs from _Static_assert(), which
 * will generate a compiler error even if the scope it was called from is dead
 * code. This may be useful to double-check things like constants that are only
 * valid if a certain Kconfig option is set.
 *
 * The error message when this hits will look like this:
 *
 * ramstage/lib/bootmode.o: In function `display_init_required':
 * bootmode.c:42: undefined reference to `_dead_code_assertion_failed'
 */
extern void _dead_code_assertion_failed(void) __attribute__((noreturn));
#define dead_code() _dead_code_assertion_failed()

/* This can be used in the context of an expression of type 'type'. */
#define dead_code_t(type) ({ \
	dead_code(); \
	*(type *)(uintptr_t)0; \
})

#if ENV_X86_64
#define pointer_to_uint32_safe(x) ({ \
	if ((uintptr_t)(x) > 0xffffffffUL) \
		die("Cast from pointer to uint32_t overflows"); \
	(uint32_t)(uintptr_t)(x); \
})
#else
#define pointer_to_uint32_safe(x) ({ \
	(uint32_t)(uintptr_t)(x); \
})
#endif
#endif // __ASSERT_H__