88# - Windows (MSVC): Uses single build directory, configs specified during build
99# - Linux/Mac: Uses separate build/Debug and build/Release directories
1010#
11+ # Memory Sanitizer (ASAN):
12+ # - Functional tests: ENABLED by default (can be disabled with --no-sanitize)
13+ # - Performance tests: DISABLED by default (can be enabled with --sanitize-all)
14+ # - ASAN helps detect memory errors like buffer overflows, use-after-free, etc.
15+ #
1116# Usage:
12- # ./run_tests.sh # Run all tests
13- # ./run_tests.sh --functional # Run only functional tests
14- # ./run_tests.sh --performance # Run only performance tests
17+ # ./run_tests.sh # Run all tests (functional with ASAN, performance without)
18+ # ./run_tests.sh --functional # Run only functional tests (with ASAN)
19+ # ./run_tests.sh --performance # Run only performance tests (without ASAN)
1520# ./run_tests.sh --avx2 # Run only AVX2 performance tests
1621# ./run_tests.sh --shuffle # Run only tests with names containing 'shuffle' (case-insensitive) in functional tests
22+ # ./run_tests.sh --no-sanitize # Disable ASAN for all tests
23+ # ./run_tests.sh --sanitize-all # Enable ASAN for all tests (including performance)
1724# ./run_tests.sh --help # Show help
1825
1926set -e # Exit on any error
@@ -70,6 +77,9 @@ EXIT_WHEN_FAILED=false
7077GTEST_FAIL_FAST_PARAM=" "
7178FILTER=" "
7279SHUFFLE_ONLY=false
80+ # Memory sanitizer flags: auto-detect based on test type, can be overridden
81+ SANITIZE_FUNCTIONAL=" auto" # Default: enabled for functional tests
82+ SANITIZE_PERFORMANCE=" auto" # Default: disabled for performance tests
7383
7484while [[ $# -gt 0 ]]; do
7585 case $1 in
@@ -105,19 +115,37 @@ while [[ $# -gt 0 ]]; do
105115 GTEST_FAIL_FAST_PARAM=" --gtest_fail_fast"
106116 shift
107117 ;;
118+ --no-sanitize)
119+ SANITIZE_FUNCTIONAL=" no"
120+ SANITIZE_PERFORMANCE=" no"
121+ shift
122+ ;;
123+ --sanitize-all)
124+ SANITIZE_FUNCTIONAL=" yes"
125+ SANITIZE_PERFORMANCE=" yes"
126+ shift
127+ ;;
108128 --help)
109129 echo " CCAP Unit Tests Runner"
110130 echo " "
111131 echo " Usage:"
112- echo " $0 # Run all tests"
113- echo " $0 --functional # Run only functional tests (Debug mode)"
114- echo " $0 --performance # Run only performance tests (Release mode)"
132+ echo " $0 # Run all tests (functional with ASAN, performance without) "
133+ echo " $0 --functional # Run only functional tests (Debug mode, with ASAN )"
134+ echo " $0 --performance # Run only performance tests (Release mode, without ASAN )"
115135 echo " $0 --avx2 # Run only AVX2 performance tests (Release mode)"
116136 echo " $0 --shuffle # Run only tests whose names contain '*shuffle*' or '*Shuffle*' in functional tests"
117137 echo " $0 --skip-build # Skip build step, run tests only"
118138 echo " $0 --exit-when-failed # Stop at first test failure (gtest fail fast mode)"
139+ echo " $0 --no-sanitize # Disable AddressSanitizer (ASAN) for all tests"
140+ echo " $0 --sanitize-all # Enable AddressSanitizer (ASAN) for all tests (including performance)"
119141 echo " $0 --help # Show this help"
120142 echo " "
143+ echo " Memory Sanitizer (ASAN):"
144+ echo " - Functional tests: ENABLED by default (detects memory errors)"
145+ echo " - Performance tests: DISABLED by default (would affect performance measurements)"
146+ echo " - Use --no-sanitize to disable ASAN completely"
147+ echo " - Use --sanitize-all to enable ASAN for performance tests too"
148+ echo " "
121149 echo " Note: Performance tests are automatically run in Release mode for accurate results"
122150 exit 0
123151 ;;
162190TEST_RESULT=0
163191PERF_RESULT=0
164192
193+ # Determine ASAN usage
194+ # Functional tests: default enabled, Performance tests: default disabled
195+ USE_ASAN_FUNCTIONAL=false
196+ USE_ASAN_PERFORMANCE=false
197+
198+ if [ " $SANITIZE_FUNCTIONAL " = " auto" ]; then
199+ USE_ASAN_FUNCTIONAL=true # Default: enable ASAN for functional tests
200+ elif [ " $SANITIZE_FUNCTIONAL " = " yes" ]; then
201+ USE_ASAN_FUNCTIONAL=true
202+ fi
203+
204+ if [ " $SANITIZE_PERFORMANCE " = " auto" ]; then
205+ USE_ASAN_PERFORMANCE=false # Default: disable ASAN for performance tests
206+ elif [ " $SANITIZE_PERFORMANCE " = " yes" ]; then
207+ USE_ASAN_PERFORMANCE=true
208+ fi
209+
210+ # Function to check if ASAN is supported
211+ function checkAsanSupport() {
212+ # ASAN is well supported on Linux and macOS with GCC/Clang
213+ # Windows MSVC support is limited and not used here
214+ if isWindows; then
215+ return 1 # Disable ASAN on Windows for now
216+ fi
217+ return 0
218+ }
219+
220+ # Check ASAN support
221+ ASAN_SUPPORTED=false
222+ if checkAsanSupport; then
223+ ASAN_SUPPORTED=true
224+ fi
225+
226+ # Disable ASAN if not supported
227+ if [ " $ASAN_SUPPORTED " = false ]; then
228+ if [ " $USE_ASAN_FUNCTIONAL " = true ] || [ " $USE_ASAN_PERFORMANCE " = true ]; then
229+ echo -e " ${YELLOW} ⚠ AddressSanitizer not supported on this platform, disabling ASAN${NC} "
230+ USE_ASAN_FUNCTIONAL=false
231+ USE_ASAN_PERFORMANCE=false
232+ fi
233+ fi
234+
165235# Build Debug version for functional tests
166236if [ " $RUN_FUNCTIONAL " = true ]; then
167237 echo " "
@@ -170,15 +240,24 @@ if [ "$RUN_FUNCTIONAL" = true ]; then
170240 echo -e " ${BLUE} Skipping build, using existing Debug binaries${NC} "
171241 else
172242 echo -e " ${BLUE} Building Debug version (for functional tests)${NC} "
243+ if [ " $USE_ASAN_FUNCTIONAL " = true ]; then
244+ echo -e " ${GREEN} 🛡️ AddressSanitizer (ASAN) ENABLED for memory error detection${NC} "
245+ fi
173246 fi
174247 echo -e " ${PURPLE} ===============================================${NC} "
175248
176249 if [ " $SKIP_BUILD " = false ]; then
250+ # Prepare ASAN flags if enabled
251+ ASAN_FLAGS=" "
252+ if [ " $USE_ASAN_FUNCTIONAL " = true ]; then
253+ ASAN_FLAGS=" -DCMAKE_CXX_FLAGS=\" -fsanitize=address -g\" -DCMAKE_C_FLAGS=\" -fsanitize=address -g\" -DCMAKE_EXE_LINKER_FLAGS=\" -fsanitize=address\" -DCMAKE_SHARED_LINKER_FLAGS=\" -fsanitize=address\" "
254+ fi
255+
177256 if isWindows; then
178257 # Windows MSVC: use single build directory, specify config during build
179258 cd build
180259 echo -e " ${BLUE} Configuring CMake (Windows MSVC)...${NC} "
181- cmake .. -DCCAP_BUILD_TESTS=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5
260+ eval cmake .. -DCCAP_BUILD_TESTS=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 $ASAN_FLAGS
182261
183262 echo -e " ${BLUE} Building Debug project...${NC} "
184263 cmake --build . --config Debug --parallel $( detectCores)
@@ -191,7 +270,7 @@ if [ "$RUN_FUNCTIONAL" = true ]; then
191270 # Linux/Mac: use separate Debug directory
192271 cd build/Debug
193272 echo -e " ${BLUE} Configuring CMake (Debug)...${NC} "
194- cmake ../.. -DCMAKE_BUILD_TYPE=Debug -DCCAP_BUILD_TESTS=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5
273+ eval cmake ../.. -DCMAKE_BUILD_TYPE=Debug -DCCAP_BUILD_TESTS=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 $ASAN_FLAGS
195274
196275 echo -e " ${BLUE} Building Debug project...${NC} "
197276 cmake --build . --config Debug --parallel $( detectCores)
@@ -211,17 +290,27 @@ if [ "$RUN_PERFORMANCE" = true ]; then
211290 echo -e " ${BLUE} Skipping build, using existing Release binaries${NC} "
212291 else
213292 echo -e " ${BLUE} Building Release version (for performance tests)${NC} "
293+ if [ " $USE_ASAN_PERFORMANCE " = true ]; then
294+ echo -e " ${GREEN} 🛡️ AddressSanitizer (ASAN) ENABLED${NC} "
295+ echo -e " ${YELLOW} ⚠ Note: ASAN affects performance measurements${NC} "
296+ fi
214297 fi
215298 echo -e " ${PURPLE} ===============================================${NC} "
216299
217300 if [ " $SKIP_BUILD " = false ]; then
301+ # Prepare ASAN flags if enabled
302+ ASAN_FLAGS=" "
303+ if [ " $USE_ASAN_PERFORMANCE " = true ]; then
304+ ASAN_FLAGS=" -DCMAKE_CXX_FLAGS=\" -fsanitize=address -g\" -DCMAKE_C_FLAGS=\" -fsanitize=address -g\" -DCMAKE_EXE_LINKER_FLAGS=\" -fsanitize=address\" -DCMAKE_SHARED_LINKER_FLAGS=\" -fsanitize=address\" "
305+ fi
306+
218307 if isWindows; then
219308 # Windows MSVC: use single build directory, specify config during build
220309 cd build
221310 # Only configure if not already configured
222311 if [ ! -f " CMakeCache.txt" ]; then
223312 echo -e " ${BLUE} Configuring CMake (Windows MSVC)...${NC} "
224- cmake .. -DCCAP_BUILD_TESTS=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5
313+ eval cmake .. -DCCAP_BUILD_TESTS=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 $ASAN_FLAGS
225314 fi
226315
227316 echo -e " ${BLUE} Building Release project...${NC} "
@@ -234,7 +323,7 @@ if [ "$RUN_PERFORMANCE" = true ]; then
234323 # Linux/Mac: use separate Release directory
235324 cd build/Release
236325 echo -e " ${BLUE} Configuring CMake (Release)...${NC} "
237- cmake ../.. -DCMAKE_BUILD_TYPE=Release -DCCAP_BUILD_TESTS=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5
326+ eval cmake ../.. -DCMAKE_BUILD_TYPE=Release -DCCAP_BUILD_TESTS=ON -DCMAKE_POLICY_VERSION_MINIMUM=3.5 $ASAN_FLAGS
238327
239328 echo -e " ${BLUE} Building Release project...${NC} "
240329 cmake --build . --config Release --parallel $( detectCores)
@@ -265,6 +354,14 @@ if [ "$RUN_FUNCTIONAL" = true ]; then
265354 if [ -f " $TEST_EXECUTABLE " ]; then
266355 echo -e " ${YELLOW} Running functional tests in Debug mode...${NC} "
267356
357+ # Set ASAN options if enabled
358+ if [ " $USE_ASAN_FUNCTIONAL " = true ]; then
359+ echo -e " ${GREEN} 🛡️ Running with AddressSanitizer enabled${NC} "
360+ # Disable memory leak detection in ASAN (can cause false positives in tests)
361+ # Enable detailed error reporting
362+ export ASAN_OPTIONS=" detect_leaks=0:halt_on_error=1:allocator_may_return_null=1"
363+ fi
364+
268365 if [ " $SHUFFLE_ONLY " = true ]; then
269366 echo -e " ${BLUE} Filtering tests to names containing '*shuffle*' or '*Shuffle*'...${NC} "
270367 " $TEST_EXECUTABLE " --gtest_filter=' *shuffle*:*Shuffle*:*SHUFFLE*' $GTEST_FAIL_FAST_PARAM --gtest_output=xml:test_results_debug.xml
@@ -326,6 +423,13 @@ if [ "$RUN_PERFORMANCE" = true ]; then
326423 echo -e " ${YELLOW} Running performance benchmarks in Release mode...${NC} "
327424 echo -e " ${BLUE} Note: Release mode provides accurate performance measurements${NC} "
328425
426+ # Set ASAN options if enabled
427+ if [ " $USE_ASAN_PERFORMANCE " = true ]; then
428+ echo -e " ${GREEN} 🛡️ Running with AddressSanitizer enabled${NC} "
429+ echo -e " ${YELLOW} ⚠ Performance results may be affected by ASAN overhead${NC} "
430+ export ASAN_OPTIONS=" detect_leaks=0:halt_on_error=1:allocator_may_return_null=1"
431+ fi
432+
329433 if [ -n " $FILTER " ]; then
330434 echo -e " ${BLUE} Filter: $FILTER ${NC} "
331435 " $PERF_EXECUTABLE " $FILTER $GTEST_FAIL_FAST_PARAM --gtest_output=xml:build/performance_results_release.xml
0 commit comments