1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2014, STMicroelectronics International N.V.
4  * All rights reserved.
5  */
6 
7 #ifndef TB_ASSERTS_H
8 #define TB_ASSERTS_H
9 
10 #include <trace.h>
11 #include "tb_macros.h"
12 
13 /*
14  * TB_ASSERT_MSG general assert function with a message.
15  */
16 #define TB_ASSERT_MSG(cond, str)                                   \
17 do {                                                               \
18 	if (!(cond)) {                                             \
19 		EMSG("Assertion failed at line %d in file:\n%s",   \
20 			__LINE__, __FILE__);                       \
21 		EMSG("Message: %s", str);                          \
22 		HALT;                                              \
23 	}; \
24 } while (0)
25 
26 /*
27  * TB_ASSERT general assert function.
28  */
29 #define TB_ASSERT(cond)                                            \
30 do {                                                               \
31 	if (!(cond)) {                                             \
32 		EMSG("Assertion failed at line %d in file:\n%s",   \
33 			__LINE__, __FILE__);                       \
34 		HALT;                                              \
35 	};                                                         \
36 } while (0)
37 
38 /*
39  * TB_ASSERT_EQ_SHORT checks that src equals the short value.
40  */
41 #define TB_ASSERT_EQ_SHORT(src, short)                                         \
42 do {                                                                           \
43 	if (((short) == 0) && (__mpanum_size((mpanum)src) != 0)) {             \
44 		EMSG("Assertion failed at line %d in file:\n%s",               \
45 			__LINE__, __FILE__);                                   \
46 		EMSG("short == 0, but size != 0");                             \
47 		HALT;                                                          \
48 	} else if (__mpanum_size((mpanum)src) > 1) {                           \
49 		EMSG("Assertion failed at line %d in file:\n%s",               \
50 			__LINE__, __FILE__);                                   \
51 		EMSG("size > 1, cannot be equal to a short.");                 \
52 		HALT;                                                          \
53 	} else if (                                                            \
54 		(int)(__mpanum_lsw((mpanum)src)*__mpanum_sign((mpanum)src)) != \
55 			(int)(short)) {                                        \
56 		EMSG("Assertion failed at line %d in file:\n%s",               \
57 				__LINE__, __FILE__);                           \
58 		EMSG("short == %d, but src == %d", (short),                    \
59 			(int)(__mpanum_lsw((mpanum)src)                        \
60 			*__mpanum_sign((mpanum)src)));                         \
61 		HALT;                                                          \
62 	};                                                                     \
63 } while (0)
64 
65 /*
66  * TB_ASSERT_STR_EQ checks that the two strings a and b are equal.
67  */
68 #define TB_ASSERT_STR_EQ(a, b)                                  \
69 do {                                                            \
70 	if (my_strcmp((a), (b)) != 0) {                         \
71 		EMSG("Assertion failed %s != %s", (a), (b));    \
72 		HALT;                                           \
73 	};                                                      \
74 } while (0)
75 
76 /*
77  * TB_ASSERT_POINTER_NULL(p) checks that p is null
78  */
79 #define TB_ASSERT_POINTER_NULL(p)                                  \
80 do {                                                               \
81 	if ((p) != 0) {                                            \
82 		EMSG("Assertion failed, pointer was not null.");   \
83 		HALT;                                              \
84 	};                                                         \
85 } while (0)
86 
87 /*
88  * TB_ASSERT_POINTERS_EQ checks that p, q are pointing to the same element
89  */
90 #define TB_ASSERT_POINTERS_EQ(p, q)                                  \
91 do {                                                                 \
92 	if ((p) != (q)) {                                            \
93 		EMSG("Assertion failed, pointers are not equal.");   \
94 		HALT;                                                \
95 	};                                                           \
96 } while (0)
97 
98 /*
99  * TB_ASSERT_POINTERS_NEQ checks that p, q are not pointing to the same element
100  */
101 #define TB_ASSERT_POINTERS_NEQ(p, q)                             \
102 do {                                                             \
103 	if ((p) == (q)) {                                        \
104 		EMSG("Assertion failed, pointers are equal.");   \
105 		HALT;                                            \
106 	};                                                       \
107 } while (0)
108 
109 /*
110  * TB_ASSERT_BIGINT_EQ Checks that a and b are equal
111  */
112 #define TB_ASSERT_BIGINT_EQ(a, b)                                   \
113 do {                                                                \
114 	if (TEE_BigIntCmp((a), (b)) != 0) {                         \
115 		EMSG("Assertion failed, numbers are not equal.");   \
116 		HALT;                                               \
117 	};                                                          \
118 } while (0)
119 
120 /*
121  * TB_ASSERT_BIGINT_NEQ Checks that a and b are different
122  */
123 #define TB_ASSERT_BIGINT_NEQ(a, b)                              \
124 do {                                                            \
125 	if (TEE_BigIntCmp((a), (b)) == 0) {                     \
126 		EMSG("Assertion failed, numbers are equal.");   \
127 		HALT;                                           \
128 	};                                                      \
129 } while (0)
130 
131 /*
132  * TB_ASSERT_BIGINT_LESS Checks that a < b
133  */
134 #define TB_ASSERT_BIGINT_LESS(a, b)                                         \
135 do {                                                                        \
136 	if (TEE_BigIntCmp((a), (b)) >= 0) {                                 \
137 		EMSG("Assertion failed, first is not less than second.");   \
138 		HALT;                                                       \
139 	}; \
140 } while (0)
141 
142 /*
143  * TB_ASSERT_INT_EQ Checks that a and be are equal
144  */
145 #define TB_ASSERT_INT_EQ(a, b)                                      \
146 do {                                                                \
147 	if ((a) != (b)) {                                           \
148 		EMSG("Assertion failed, numbers are not equal.");   \
149 		HALT;                                               \
150 	};                                                          \
151 } while (0)
152 
153 #endif
154