24#ifndef IMPULSE_ASSERT_H
25#define IMPULSE_ASSERT_H
36#if defined(__GNUC__) || defined(__clang__)
37 #define IMPULSE_LIKELY(x) __builtin_expect(!!(x), 1)
38 #define IMPULSE_UNLIKELY(x) __builtin_expect(!!(x), 0)
40 #define IMPULSE_LIKELY(x) (x)
41 #define IMPULSE_UNLIKELY(x) (x)
45 #define IMPULSE_BREAKPOINT() __debugbreak()
46#elif defined(__has_builtin)
47 #if __has_builtin(__builtin_trap)
48 #define IMPULSE_BREAKPOINT() __builtin_trap()
50 #define IMPULSE_BREAKPOINT() abort()
53 #define IMPULSE_BREAKPOINT() abort()
56#define IMPULSE_ASSERT_FAILED_MSG(type, cond_str, file, line, func, msg) \
59 "[%s FAILED] (%s)\n Location: %s:%d\n Function: %s\n Message: %s\n", \
60 type, cond_str, file, line, func, (msg)); \
62 IMPULSE_BREAKPOINT(); \
65#define IMPULSE_ASSERT_FAILED(type, cond_str, file, line, func) \
68 "[%s FAILED] (%s)\n Location: %s:%d\n Function: %s\n", \
69 type, cond_str, file, line, func); \
71 IMPULSE_BREAKPOINT(); \
75 const char* file,
int line,
const char* func) {
77 "[%s FAILED] (%s)\n Location: %s:%d\n Function: %s\n",
78 type, cond_str, file, line, func);
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
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
111#define IMPULSE_COV_BOTH 0
112#define IMPULSE_COV_ALWAYS_TAKEN 1
113#define IMPULSE_COV_NEVER_TAKEN 2
116#define IMPULSE_COV_KIND_TWO_WAY 0x03u
117#define IMPULSE_COV_KIND_THREE_WAY 0x07u
144 uint8_t kind_mask, uint8_t expectation, uint8_t branch_index);
147 uint32_t* out_fully_covered_sites,
148 uint32_t* out_missing_branches);
155#define IMPULSE_CORRUPT_SNAPSHOT (!impulse_test_is_never_corrupt())
160#if defined(IMPULSE_ENABLE_ASSERTIONS) || (!defined(NDEBUG) && !defined(IMPULSE_DISABLE_ASSERTIONS))
161 #define IMPULSE_ASSERTIONS_ACTIVE 1
163 #define IMPULSE_ASSERTIONS_ACTIVE 0
166#if IMPULSE_ASSERTIONS_ACTIVE
168 #define IMPULSE_ASSERT(cond) \
171 IMPULSE_ASSERT_FAILED("IMPULSE_ASSERT", #cond, __FILE__, __LINE__, __func__); \
175 #define IMPULSE_ASSERT_MSG(cond, msg) \
178 IMPULSE_ASSERT_FAILED_MSG("IMPULSE_ASSERT", #cond, __FILE__, __LINE__, __func__, (msg)); \
182 #define IMPULSE_WELL_FORMED_ASSERT(cond) \
184 if (!((cond) || IMPULSE_CORRUPT_SNAPSHOT)) { \
185 IMPULSE_ASSERT_FAILED("IMPULSE_WELL_FORMED_ASSERT", #cond, __FILE__, __LINE__, __func__); \
189 #define IMPULSE_VVA_ONLY(X) X
190 #define IMPULSE_TESTONLY(X) X
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)
205#if defined(IMPULSE_ENABLE_AUDIT_ASSERTIONS)
207 #define IMPULSE_AUDIT_ASSERT(cond) \
210 IMPULSE_ASSERT_FAILED("IMPULSE_AUDIT_ASSERT", #cond, __FILE__, __LINE__, __func__); \
214 #define IMPULSE_AUDIT_ASSERT_MSG(cond, msg) \
217 IMPULSE_ASSERT_FAILED_MSG("IMPULSE_AUDIT_ASSERT", #cond, __FILE__, __LINE__, __func__, (msg)); \
223 #define IMPULSE_AUDIT_ASSERT(cond) ((void)0)
224 #define IMPULSE_AUDIT_ASSERT_MSG(cond, msg) ((void)0)
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)
240 #define IMPULSE_ALWAYS(X) (X)
241 #define IMPULSE_NEVER(X) (X)
247#if IMPULSE_ASSERTIONS_ACTIVE || defined(IMPULSE_COVERAGE_TEST) || defined(IMPULSE_ENABLE_MCDC_COVERAGE)
248 #define IMPULSE_TESTCASE(X) \
250 if (IMPULSE_UNLIKELY(g_impulse_testcase_enabled)) { \
251 impulse_testcase_record(__FILE__, __LINE__, (X) ? 1 : 0); \
255 #define IMPULSE_TESTCASE(X) ((void)0)
259#define IMPULSE_OK_IF_ALWAYS_TRUE(X) (X)
260#define IMPULSE_OK_IF_ALWAYS_FALSE(X) (X)
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)