-
Notifications
You must be signed in to change notification settings - Fork 3
/
vcos_assert.lua
300 lines (255 loc) · 8.45 KB
/
vcos_assert.lua
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
--[[
=============================================================================
VideoCore OS Abstraction Layer - Assertion and error-handling macros.
=============================================================================
--]]
--[==[
/*
* Macro:
* vcos_assert(cond)
* vcos_assert_msg(cond, fmt, ...)
* Use:
* Detecting programming errors by ensuring that assumptions are correct.
* On failure:
* Performs a platform-dependent "breakpoint", usually with an assert-style
* message. The '_msg' variant expects a printf-style format string and
* parameters.
* If a failure is detected, the code should be fixed and rebuilt.
* In release builds:
* Generates no code, i.e. does not evaluate 'cond'.
* Returns:
* Nothing.
*
* Macro:
* vcos_demand(cond)
* vcos_demand_msg(cond, fmt, ...)
* Use:
* Detecting fatal system errors that require a reboot.
* On failure:
* Performs a platform-dependent "breakpoint", usually with an assert-style
* message, then calls vcos_abort (see below).
* In release builds:
* Calls vcos_abort() if 'cond' is false.
* Returns:
* Nothing (never, on failure).
*
* Macro:
* vcos_verify(cond)
* vcos_verify_msg(cond, fmt, ...)
* Use:
* Detecting run-time errors and interesting conditions, normally within an
* 'if' statement to catch the failures, i.e.
* if (!vcos_verify(cond)) handle_error();
* On failure:
* Generates a message and optionally stops at a platform-dependent
* "breakpoint" (usually disabled). See vcos_verify_bkpts_enable below.
* In release builds:
* Just evaluates and returns 'cond'.
* Returns:
* Non-zero if 'cond' is true, otherwise zero.
*
* Macro:
* vcos_static_assert(cond)
* Use:
* Detecting compile-time errors.
* On failure:
* Generates a compiler error.
* In release builds:
* Generates a compiler error.
*
* Function:
* void vcos_abort(void)
* Use:
* Invokes the fatal error handling mechanism, alerting the host where
* applicable.
* Returns:
* Never.
*
* Macro:
* VCOS_VERIFY_BKPTS
* Use:
* Define in a module (before including vcos.h) to specify an alternative
* flag to control breakpoints on vcos_verify() failures.
* Returns:
* Non-zero values enable breakpoints.
*
* Function:
* int vcos_verify_bkpts_enable(int enable);
* Use:
* Sets the global flag controlling breakpoints on vcos_verify failures,
* enabling the breakpoints iff 'enable' is non-zero.
* Returns:
* The previous state of the flag.
*
* Function:
* int vcos_verify_bkpts_enabled(void);
* Use:
* Queries the state of the global flag enabling breakpoints on vcos_verify
* failures.
* Returns:
* The current state of the flag.
*
* Examples:
*
* int my_breakpoint_enable_flag = 1;
*
* #define VCOS_VERIFY_BKPTS my_breakpoint_enable_flag
*
* #include "interface/vcos/vcos.h"
*
* vcos_static_assert((sizeof(object) % 32) == 0);
*
* // ...
*
* vcos_assert_msg(postcondition_is_true, "Coding error");
*
* if (!vcos_verify_msg(buf, "Buffer allocation failed (%d bytes)", size))
* {
* // Tidy up
* // ...
* return OUT_OF_MEMORY;
* }
*
* vcos_demand(*p++==GUARDWORDHEAP);
*/
#include "interface/vcos/vcos_types.h"
#ifdef __COVERITY__
/*
* This tells coverity not to expand the assert macro, so it still sees the
* asserts in the code, even in release builds (we currently run coverity on
* our release builds). Unfortunately MetaWare won't compile it, even though
* __COVERITY__ isn't defined.
*/
// #nodef assert
/*
* So we need to declare the function. It's already built into coverity that
* assert is a "killpath"
*/
extern void assert(int cond);
extern void __coverity_panic__(void);
#undef VCOS_ASSERT_BKPT
#define VCOS_ASSERT_BKPT __coverity_panic__()
#endif
#ifndef VCOS_VERIFY_BKPTS
#define VCOS_VERIFY_BKPTS vcos_verify_bkpts_enabled()
#endif
#ifndef VCOS_BKPT
#if defined(__VIDEOCORE__) && !defined(VCOS_ASSERT_NO_BKPTS)
#define VCOS_BKPT _bkpt()
#else
#define VCOS_BKPT (void )0
#endif
#endif
#ifndef VCOS_ASSERT_BKPT
#define VCOS_ASSERT_BKPT VCOS_BKPT
#endif
#ifndef VCOS_VERIFY_BKPT
#define VCOS_VERIFY_BKPT (VCOS_VERIFY_BKPTS ? VCOS_BKPT : (void)0)
#endif
VCOSPRE_ int VCOSPOST_ vcos_verify_bkpts_enabled(void);
VCOSPRE_ int VCOSPOST_ vcos_verify_bkpts_enable(int enable);
VCOSPRE_ void VCOSPOST_ vcos_abort(void);
#ifndef VCOS_ASSERT_MSG
#ifdef LOGGING
extern void logging_assert(const char *file, const char *func, int line, const char *format, ...);
extern void logging_assert_dump(void);
#define VCOS_ASSERT_MSG(...) ((VCOS_ASSERT_LOGGING && !VCOS_ASSERT_LOGGING_DISABLE) ? logging_assert_dump(), logging_assert(__FILE__, __func__, __LINE__, __VA_ARGS__) : (void)0)
#else
#define VCOS_ASSERT_MSG(...) ((void)0)
#endif
#endif
#ifndef VCOS_VERIFY_MSG
#define VCOS_VERIFY_MSG(...) VCOS_ASSERT_MSG(__VA_ARGS__)
#endif
VCOS_ASSERT_LOGGING = false
VCOS_ASSERT_LOGGING_DISABLE = false
#if !defined(NDEBUG) || defined(VCOS_RELEASE_ASSERTS)
#ifndef vcos_assert
#define vcos_assert(cond) \
( (cond) ? (void)0 : (VCOS_ASSERT_MSG("%s", #cond), VCOS_ASSERT_BKPT) )
#endif
#ifndef vcos_assert_msg
#define vcos_assert_msg(cond, ...) \
( (cond) ? (void)0 : (VCOS_ASSERT_MSG(__VA_ARGS__), VCOS_ASSERT_BKPT) )
#endif
#else /* !defined(NDEBUG) || defined(VCOS_RELEASE_ASSERTS) */
#ifndef vcos_assert
#define vcos_assert(cond) (void)0
#endif
#ifndef vcos_assert_msg
#define vcos_assert_msg(cond, ...) (void)0
#endif
#endif /* !defined(NDEBUG) || defined(VCOS_RELEASE_ASSERTS) */
#if !defined(NDEBUG)
#ifndef vcos_demand
#define vcos_demand(cond) \
( (cond) ? (void)0 : (VCOS_ASSERT_MSG("%s", #cond), VCOS_ASSERT_BKPT, vcos_abort()) )
#endif
#ifndef vcos_demand_msg
#define vcos_demand_msg(cond, ...) \
( (cond) ? (void)0 : (VCOS_ASSERT_MSG(__VA_ARGS__), VCOS_ASSERT_BKPT, vcos_abort()) )
#endif
#ifndef vcos_verify
#define vcos_verify(cond) \
( (cond) ? 1 : (VCOS_VERIFY_MSG("%s", #cond), VCOS_VERIFY_BKPT, 0) )
#endif
#ifndef vcos_verify_msg
#define vcos_verify_msg(cond, ...) \
( (cond) ? 1 : (VCOS_VERIFY_MSG(__VA_ARGS__), VCOS_VERIFY_BKPT, 0) )
#endif
#else /* !defined(NDEBUG) */
#ifndef vcos_demand
#define vcos_demand(cond) \
( (cond) ? (void)0 : vcos_abort() )
#endif
#ifndef vcos_demand_msg
#define vcos_demand_msg(cond, ...) \
( (cond) ? (void)0 : vcos_abort() )
#endif
#ifndef vcos_verify
#define vcos_verify(cond) (cond)
#endif
#ifndef vcos_verify_msg
#define vcos_verify_msg(cond, ...) (cond)
#endif
#endif /* !defined(NDEBUG) */
#ifndef vcos_static_assert
#if defined(__GNUC__)
#define vcos_static_assert(cond) __attribute__((unused)) extern int vcos_static_assert[(cond)?1:-1]
#else
#define vcos_static_assert(cond) extern int vcos_static_assert[(cond)?1:-1]
#endif
#endif
#ifndef vc_assert
#define vc_assert(cond) vcos_assert(cond)
#endif
#define vcos_unreachable() vcos_assert(0)
#define vcos_not_impl() vcos_assert(0)
/** Print out a backtrace, on supported platforms.
*/
extern void vcos_backtrace_self(void);
--]==]
--[[
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
--]]