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\n", \
20 			__LINE__, __FILE__);                       \
21 		EMSG("Message: %s\n", 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\n", \
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\n",             \
45 			__LINE__, __FILE__);                                   \
46 		EMSG("short == 0, but size != 0\n");                           \
47 		HALT;                                                          \
48 	} else if (__mpanum_size((mpanum)src) > 1) {                           \
49 		EMSG("Assertion failed at line %d in file:\n%s\n",             \
50 			__LINE__, __FILE__);                                   \
51 		EMSG("size > 1, cannot be equal to a short.\n");               \
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\n",             \
57 				__LINE__, __FILE__);                           \
58 		EMSG("short == %d, but src == %d\n", (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\n", (a), (b));  \
72 		HALT;                                           \
73 	};                                                      \
74 } while (0)
75 
76 /*
77  * TB_ASSERT_HEX_VALUE checks that a prints to the string v in hex.
78  */
79 #define TB_ASSERT_HEX_PRINT_VALUE(a, v)                \
80 do {                                                   \
81 	char *_str_;                                   \
82 	_str_ = TEE_BigIntConvertToString(NULL,        \
83 		TEE_STRING_MODE_HEX_UC, (a));          \
84 	TB_ASSERT_STR_EQ(_str_, (v));                  \
85 	TEE_Free(_str_);                               \
86 } while (0)
87 
88 /*
89  * TB_ASSERT_POINTER_NULL(p) checks that p is null
90  */
91 #define TB_ASSERT_POINTER_NULL(p)                                  \
92 do {                                                               \
93 	if ((p) != 0) {                                            \
94 		EMSG("Assertion failed, pointer was not null.\n"); \
95 		HALT;                                              \
96 	};                                                         \
97 } while (0)
98 
99 /*
100  * TB_ASSERT_POINTERS_EQ checks that p, q are pointing to the same element
101  */
102 #define TB_ASSERT_POINTERS_EQ(p, q)                                  \
103 do {                                                                 \
104 	if ((p) != (q)) {                                            \
105 		EMSG("Assertion failed, pointers are not equal.\n"); \
106 		HALT;                                                \
107 	};                                                           \
108 } while (0)
109 
110 /*
111  * TB_ASSERT_POINTERS_NEQ checks that p, q are not pointing to the same element
112  */
113 #define TB_ASSERT_POINTERS_NEQ(p, q)                             \
114 do {                                                             \
115 	if ((p) == (q)) {                                        \
116 		EMSG("Assertion failed, pointers are equal.\n"); \
117 		HALT;                                            \
118 	};                                                       \
119 } while (0)
120 
121 /*
122  * TB_ASSERT_BIGINT_EQ Checks that a and b are equal
123  */
124 #define TB_ASSERT_BIGINT_EQ(a, b)                                   \
125 do {                                                                \
126 	if (TEE_BigIntCmp((a), (b)) != 0) {                         \
127 		EMSG("Assertion failed, numbers are not equal.\n"); \
128 		HALT;                                               \
129 	};                                                          \
130 } while (0)
131 
132 /*
133  * TB_ASSERT_BIGINT_NEQ Checks that a and b are different
134  */
135 #define TB_ASSERT_BIGINT_NEQ(a, b)                              \
136 do {                                                            \
137 	if (TEE_BigIntCmp((a), (b)) == 0) {                     \
138 		EMSG("Assertion failed, numbers are equal.\n"); \
139 		HALT;                                           \
140 	};                                                      \
141 } while (0)
142 
143 /*
144  * TB_ASSERT_BIGINT_LESS Checks that a < b
145  */
146 #define TB_ASSERT_BIGINT_LESS(a, b)                                         \
147 do {                                                                        \
148 	if (TEE_BigIntCmp((a), (b)) >= 0) {                                 \
149 		EMSG("Assertion failed, first is not less than second.\n"); \
150 		HALT;                                                       \
151 	}; \
152 } while (0)
153 
154 /*
155  * TB_ASSERT_INT_EQ Checks that a and be are equal
156  */
157 #define TB_ASSERT_INT_EQ(a, b)                                      \
158 do {                                                                \
159 	if ((a) != (b)) {                                           \
160 		EMSG("Assertion failed, numbers are not equal.\n"); \
161 		HALT;                                               \
162 	};                                                          \
163 } while (0)
164 
165 #endif
166