Skip to content

Commit 6e91bba

Browse files
committed
Make assertion hit warnings optional in release builds.
1 parent 280b749 commit 6e91bba

4 files changed

Lines changed: 17 additions & 5 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ option(CAPSTONE_USE_DEFAULT_ALLOC "Use default memory allocation functions" ON)
7979
option(CAPSTONE_USE_ARCH_REGISTRATION "Use explicit architecture registration" OFF)
8080
option(CAPSTONE_ARCHITECTURE_DEFAULT "Whether architectures are enabled by default" ON)
8181
option(CAPSTONE_DEBUG "Whether to enable extra debug assertions (enabled with CMAKE_BUILD_TYPE=Debug)" OFF)
82+
option(CAPSTONE_ASSERTION_WARNINGS "Warns about hit assertions in release builds." OFF)
8283
option(CAPSTONE_INSTALL "Generate install target" ${PROJECT_IS_TOP_LEVEL})
8384
option(ENABLE_ASAN "Enable address sanitizer" OFF)
8485
option(ENABLE_COVERAGE "Enable test coverage" OFF)

arch/BPF/BPFDisassembler.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,7 @@ static bpf_insn op2insn_st(unsigned opcode, const uint32_t imm)
539539
CASE(DW);
540540
}
541541

542-
CS_ASSERT_RET_VAL(
543-
false && "Malformed atomic BPF instruction",
544-
BPF_INS_INVALID);
542+
return BPF_INS_INVALID;
545543
}
546544
#undef CASE
547545

cs_priv.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ extern cs_vsnprintf_t cs_vsnprintf;
106106
/// For the release build the @expr is not included.
107107
#ifdef CAPSTONE_DEBUG
108108
#define CS_ASSERT(expr) assert(expr)
109+
#elif CAPSTONE_ASSERTION_WARNINGS
110+
#define CS_ASSERT(expr) \
111+
do { \
112+
if (!(expr)) { \
113+
fprintf(stderr, "Hit assert: " #expr "\n"); \
114+
} \
115+
} while(0)
109116
#else
110117
#define CS_ASSERT(expr)
111118
#endif
@@ -114,28 +121,32 @@ extern cs_vsnprintf_t cs_vsnprintf;
114121
/// In the release build it will check the @expr and return @val if false.
115122
#ifdef CAPSTONE_DEBUG
116123
#define CS_ASSERT_RET_VAL(expr, val) assert(expr)
117-
#else
124+
#elif CAPSTONE_ASSERTION_WARNINGS
118125
#define CS_ASSERT_RET_VAL(expr, val) \
119126
do { \
120127
if (!(expr)) { \
121128
fprintf(stderr, "Hit assert: " #expr "\n"); \
122129
return val; \
123130
} \
124131
} while(0)
132+
#else
133+
#define CS_ASSERT_RET_VAL(expr, val)
125134
#endif
126135

127136
/// If compiled in debug mode it will assert(@expr).
128137
/// In the release build it will check the @expr and return if false.
129138
#ifdef CAPSTONE_DEBUG
130139
#define CS_ASSERT_RET(expr) assert(expr)
131-
#else
140+
#elif CAPSTONE_ASSERTION_WARNINGS
132141
#define CS_ASSERT_RET(expr) \
133142
do { \
134143
if (!(expr)) { \
135144
fprintf(stderr, "Hit assert: " #expr "\n"); \
136145
return; \
137146
} \
138147
} while(0)
148+
#else
149+
#define CS_ASSERT_RET(expr)
139150
#endif
140151

141152
#endif

docs/cs_v6_release_guide.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ For `v7` we can then focus on other big features, like [SAIL](https://github.com
9090

9191
- `clang-tidy` is now run on all files changed by a PR.
9292
- ASAN: All tests are now run with the address sanitizer enabled. This includes checking for leaks.
93+
- Many more asserts were added. They are only enabled for debug builds (with a few exceptions).
94+
Enabling the build option `CAPSTONE_ASSERTION_WARNINGS` will print warnings in release builds if an assert is hit.
9395

9496
**Instruction formats for PPC, SystemZ, LoongArch**
9597

0 commit comments

Comments
 (0)