Impulse Graph C++ Engine 2.4.0
High-Performance Zero-Copy C-ABI Kernel and C++20 Fluent VM Engine
Loading...
Searching...
No Matches
impulse_assert.h
Go to the documentation of this file.
1
23
24#ifndef IMPULSE_ASSERT_H
25#define IMPULSE_ASSERT_H
26
27#include <stdio.h>
28#include <stdlib.h>
29#include <stdint.h>
30#include <stddef.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36#if defined(__GNUC__) || defined(__clang__)
37 #define IMPULSE_LIKELY(x) __builtin_expect(!!(x), 1)
38 #define IMPULSE_UNLIKELY(x) __builtin_expect(!!(x), 0)
39#else
40 #define IMPULSE_LIKELY(x) (x)
41 #define IMPULSE_UNLIKELY(x) (x)
42#endif
43
44#if defined(_MSC_VER)
45 #define IMPULSE_BREAKPOINT() __debugbreak()
46#elif defined(__has_builtin)
47 #if __has_builtin(__builtin_trap)
48 #define IMPULSE_BREAKPOINT() __builtin_trap()
49 #else
50 #define IMPULSE_BREAKPOINT() abort()
51 #endif
52#else
53 #define IMPULSE_BREAKPOINT() abort()
54#endif
55
56#define IMPULSE_ASSERT_FAILED_MSG(type, cond_str, file, line, func, msg) \
57 do { \
58 fprintf(stderr, \
59 "[%s FAILED] (%s)\n Location: %s:%d\n Function: %s\n Message: %s\n", \
60 type, cond_str, file, line, func, (msg)); \
61 fflush(stderr); \
62 IMPULSE_BREAKPOINT(); \
63 } while (0)
64
65#define IMPULSE_ASSERT_FAILED(type, cond_str, file, line, func) \
66 do { \
67 fprintf(stderr, \
68 "[%s FAILED] (%s)\n Location: %s:%d\n Function: %s\n", \
69 type, cond_str, file, line, func); \
70 fflush(stderr); \
71 IMPULSE_BREAKPOINT(); \
72 } while (0)
73
74static inline int impulse_assert_trap_expr(const char* type, const char* cond_str,
75 const char* file, int line, const char* func) {
76 fprintf(stderr,
77 "[%s FAILED] (%s)\n Location: %s:%d\n Function: %s\n",
78 type, cond_str, file, line, func);
79 fflush(stderr);
81 return 0;
82}
83
84/* ========================================================================= */
85/* Runtime Test Control & State Hooks (SQLite sqlite3_test_control pattern) */
86/* ========================================================================= */
87
89#define IMPULSE_TESTCTRL_ASSERT 1
90#define IMPULSE_TESTCTRL_ALWAYS 2
91#define IMPULSE_TESTCTRL_NEVER_CORRUPT 3
92#define IMPULSE_TESTCTRL_OPTIMIZATIONS 4
93#define IMPULSE_TESTCTRL_FAULT_CONFIG 5
94#define IMPULSE_TESTCTRL_FAULT_STATUS 6
95#define IMPULSE_TESTCTRL_BENIGN_FAULT 7
96#define IMPULSE_TESTCTRL_VM_BRANCH_COVERAGE 8
97#define IMPULSE_TESTCTRL_TESTCASE_STATS 9
98
100#define IMPULSE_OPT_CONSTANT_FOLDING (1u << 0)
101#define IMPULSE_OPT_KERNEL_FUSION_2HOP (1u << 1)
102#define IMPULSE_OPT_DIRECTION_SELECTION (1u << 2)
103#define IMPULSE_OPT_REGISTER_PING_PONG (1u << 3)
104#define IMPULSE_OPT_SEED_INLINING (1u << 4)
105#define IMPULSE_OPT_EARLY_EXIT (1u << 5)
106#define IMPULSE_OPT_ADAPTIVE_WALK_PULL (1u << 6)
107#define IMPULSE_OPT_SIMD_VECTOR (1u << 7)
108#define IMPULSE_OPT_ALL_DISABLED 0x00FFu
109
111#define IMPULSE_COV_BOTH 0
112#define IMPULSE_COV_ALWAYS_TAKEN 1
113#define IMPULSE_COV_NEVER_TAKEN 2
114
116#define IMPULSE_COV_KIND_TWO_WAY 0x03u
117#define IMPULSE_COV_KIND_THREE_WAY 0x07u
118
120int impulse_test_control(int op, ...);
121
127
130
132void impulse_testcase_record(const char* file, int line, int cond_val);
133
138
143void impulse_vm_branch_coverage_record(uint32_t pc, uint8_t opcode, uint32_t site_id,
144 uint8_t kind_mask, uint8_t expectation, uint8_t branch_index);
145uint8_t impulse_vm_branch_coverage_get_mask(uint32_t pc, uint8_t opcode, uint32_t site_id);
146void impulse_vm_branch_coverage_summary(uint32_t* out_total_sites,
147 uint32_t* out_fully_covered_sites,
148 uint32_t* out_missing_branches);
149
155#define IMPULSE_CORRUPT_SNAPSHOT (!impulse_test_is_never_corrupt())
156
157/* ========================================================================= */
158/* Tier 1: Standard Debug Invariant Assertions */
159/* ========================================================================= */
160#if defined(IMPULSE_ENABLE_ASSERTIONS) || (!defined(NDEBUG) && !defined(IMPULSE_DISABLE_ASSERTIONS))
161 #define IMPULSE_ASSERTIONS_ACTIVE 1
162#else
163 #define IMPULSE_ASSERTIONS_ACTIVE 0
164#endif
165
166#if IMPULSE_ASSERTIONS_ACTIVE
167
168 #define IMPULSE_ASSERT(cond) \
169 do { \
170 if (!(cond)) { \
171 IMPULSE_ASSERT_FAILED("IMPULSE_ASSERT", #cond, __FILE__, __LINE__, __func__); \
172 } \
173 } while (0)
174
175 #define IMPULSE_ASSERT_MSG(cond, msg) \
176 do { \
177 if (!(cond)) { \
178 IMPULSE_ASSERT_FAILED_MSG("IMPULSE_ASSERT", #cond, __FILE__, __LINE__, __func__, (msg)); \
179 } \
180 } while (0)
181
182 #define IMPULSE_WELL_FORMED_ASSERT(cond) \
183 do { \
184 if (!((cond) || IMPULSE_CORRUPT_SNAPSHOT)) { \
185 IMPULSE_ASSERT_FAILED("IMPULSE_WELL_FORMED_ASSERT", #cond, __FILE__, __LINE__, __func__); \
186 } \
187 } while (0)
188
189 #define IMPULSE_VVA_ONLY(X) X
190 #define IMPULSE_TESTONLY(X) X
191
192#else
193
194 #define IMPULSE_ASSERT(cond) ((void)0)
195 #define IMPULSE_ASSERT_MSG(cond, msg) ((void)0)
196 #define IMPULSE_WELL_FORMED_ASSERT(cond) ((void)0)
197 #define IMPULSE_VVA_ONLY(X)
198 #define IMPULSE_TESTONLY(X)
199
200#endif
201
202/* ========================================================================= */
203/* Tier 2: Expensive Structural Audit Assertions (O(N) invariants) */
204/* ========================================================================= */
205#if defined(IMPULSE_ENABLE_AUDIT_ASSERTIONS)
206
207 #define IMPULSE_AUDIT_ASSERT(cond) \
208 do { \
209 if (!(cond)) { \
210 IMPULSE_ASSERT_FAILED("IMPULSE_AUDIT_ASSERT", #cond, __FILE__, __LINE__, __func__); \
211 } \
212 } while (0)
213
214 #define IMPULSE_AUDIT_ASSERT_MSG(cond, msg) \
215 do { \
216 if (!(cond)) { \
217 IMPULSE_ASSERT_FAILED_MSG("IMPULSE_AUDIT_ASSERT", #cond, __FILE__, __LINE__, __func__, (msg)); \
218 } \
219 } while (0)
220
221#else
222
223 #define IMPULSE_AUDIT_ASSERT(cond) ((void)0)
224 #define IMPULSE_AUDIT_ASSERT_MSG(cond, msg) ((void)0)
225
226#endif
227
228/* ========================================================================= */
229/* Tier 3: Ductile Defensive Branch Macros (SQLite ALWAYS / NEVER pattern) */
230/* ========================================================================= */
231#if defined(IMPULSE_COVERAGE_TEST)
232 #define IMPULSE_ALWAYS(X) (1)
233 #define IMPULSE_NEVER(X) (0)
234#elif IMPULSE_ASSERTIONS_ACTIVE
235 #define IMPULSE_ALWAYS(X) \
236 ((X) ? 1 : impulse_assert_trap_expr("IMPULSE_ALWAYS", #X, __FILE__, __LINE__, __func__))
237 #define IMPULSE_NEVER(X) \
238 ((X) ? (impulse_assert_trap_expr("IMPULSE_NEVER", #X, __FILE__, __LINE__, __func__), 1) : 0)
239#else
240 #define IMPULSE_ALWAYS(X) (X)
241 #define IMPULSE_NEVER(X) (X)
242#endif
243
244/* ========================================================================= */
245/* Tier 4: Boundary-Value Coverage Probe (SQLite testcase(X) pattern) */
246/* ========================================================================= */
247#if IMPULSE_ASSERTIONS_ACTIVE || defined(IMPULSE_COVERAGE_TEST) || defined(IMPULSE_ENABLE_MCDC_COVERAGE)
248 #define IMPULSE_TESTCASE(X) \
249 do { \
250 if (IMPULSE_UNLIKELY(g_impulse_testcase_enabled)) { \
251 impulse_testcase_record(__FILE__, __LINE__, (X) ? 1 : 0); \
252 } \
253 } while (0)
254#else
255 #define IMPULSE_TESTCASE(X) ((void)0)
256#endif
257
259#define IMPULSE_OK_IF_ALWAYS_TRUE(X) (X)
260#define IMPULSE_OK_IF_ALWAYS_FALSE(X) (X)
261
262#ifdef __cplusplus
263}
264#endif
265
266#endif /* IMPULSE_ASSERT_H */
void impulse_faultsim_end_benign(void)
int impulse_vm_branch_coverage_is_enabled(void)
int impulse_test_control(int op,...)
void impulse_testcase_record(const char *file, int line, int cond_val)
void impulse_test_set_opt_disable_mask(uint32_t mask)
void impulse_faultsim_begin_benign(void)
uint32_t impulse_test_get_opt_disable_mask(void)
uint8_t impulse_vm_branch_coverage_get_mask(uint32_t pc, uint8_t opcode, uint32_t site_id)
static int impulse_assert_trap_expr(const char *type, const char *cond_str, const char *file, int line, const char *func)
Definition impulse_assert.h:74
int impulse_faultsim_step(void)
void impulse_test_set_never_corrupt(int flag)
void impulse_vm_branch_coverage_record(uint32_t pc, uint8_t opcode, uint32_t site_id, uint8_t kind_mask, uint8_t expectation, uint8_t branch_index)
int g_impulse_testcase_enabled
void impulse_vm_branch_coverage_reset(void)
void impulse_vm_branch_coverage_summary(uint32_t *out_total_sites, uint32_t *out_fully_covered_sites, uint32_t *out_missing_branches)
void impulse_vm_branch_coverage_set_enabled(int enabled)
#define IMPULSE_BREAKPOINT()
Definition impulse_assert.h:53
int impulse_test_is_never_corrupt(void)