|
| 1 | +# CppUTest and AddressSanitizer Compatibility |
| 2 | + |
| 3 | +## The Issue |
| 4 | + |
| 5 | +CppUTest and AddressSanitizer (ASAN) both instrument memory allocation functions (`malloc`, `free`, `new`, `delete`). When used together without proper configuration, they can conflict: |
| 6 | + |
| 7 | +### CppUTest's Memory Leak Detection |
| 8 | +- Overrides `malloc`/`free` to track allocations |
| 9 | +- Uses memory leak detector macros (`MemoryLeakDetectorNewMacros.h`) |
| 10 | +- Maintains internal hash tables of allocated memory |
| 11 | +- Good for detecting leaks within test scope |
| 12 | + |
| 13 | +### AddressSanitizer's Memory Instrumentation |
| 14 | +- Wraps `malloc`/`free` at compile time |
| 15 | +- Tracks shadow memory for overflow detection |
| 16 | +- Monitors use-after-free, double-free, and leaks |
| 17 | +- More comprehensive than CppUTest's detector |
| 18 | + |
| 19 | +### Potential Conflicts |
| 20 | +1. **Double instrumentation**: Both tools wrap the same functions |
| 21 | +2. **Conflicting metadata**: Different memory tracking approaches interfere |
| 22 | +3. **False positives**: One tool's cleanup can confuse the other |
| 23 | +4. **Performance**: Double overhead from both tools |
| 24 | + |
| 25 | +## Our Solution |
| 26 | + |
| 27 | +We've configured the build system to **disable CppUTest's memory leak detection when ASAN is enabled**: |
| 28 | + |
| 29 | +### Configuration Changes |
| 30 | + |
| 31 | +**1. CMakeLists.txt (tests/)** |
| 32 | +```cmake |
| 33 | +if(ENABLE_ADDRESS_SANITIZER OR ENABLE_UNDEFINED_SANITIZER) |
| 34 | + add_compile_definitions(CPPUTEST_MEMORY_LEAK_DETECTION_DISABLED) |
| 35 | + message(STATUS "CppUTest memory leak detection disabled (using AddressSanitizer instead)") |
| 36 | +endif() |
| 37 | +``` |
| 38 | + |
| 39 | +This tells CppUTest to use standard C/C++ library functions without wrapping them. |
| 40 | + |
| 41 | +**2. security_tests.cpp** |
| 42 | +```cpp |
| 43 | +#ifdef CPPUTEST_MEMORY_LEAK_DETECTION_DISABLED |
| 44 | + #define CPPUTEST_USE_STD_CPP_LIB |
| 45 | + #define CPPUTEST_USE_STD_C_LIB |
| 46 | +#endif |
| 47 | +``` |
| 48 | + |
| 49 | +These macros instruct CppUTest to use the standard library instead of its custom memory wrappers. |
| 50 | + |
| 51 | +## Why This Works |
| 52 | + |
| 53 | +- **When ASAN is OFF**: CppUTest's memory checks work normally |
| 54 | +- **When ASAN is ON**: |
| 55 | + - CppUTest doesn't instrument memory |
| 56 | + - Only ASAN does, avoiding conflicts |
| 57 | + - ASAN provides more comprehensive checking anyway |
| 58 | + |
| 59 | +## Testing Both Ways |
| 60 | + |
| 61 | +### Without ASAN (CppUTest memory checks only) |
| 62 | +```bash |
| 63 | +cd bin/posix |
| 64 | +cmake . |
| 65 | +make -j$(nproc) |
| 66 | +./tests/OpENer_Tests -g NetworkHandlerSecurity |
| 67 | +``` |
| 68 | + |
| 69 | +### With ASAN (full memory safety) |
| 70 | +```bash |
| 71 | +cd bin/posix |
| 72 | +cmake -DENABLE_ADDRESS_SANITIZER=ON . |
| 73 | +make -j$(nproc) |
| 74 | +ASAN_OPTIONS="verbosity=0" ./tests/OpENer_Tests -g NetworkHandlerSecurity |
| 75 | +``` |
| 76 | + |
| 77 | +## Memory Safety Coverage |
| 78 | + |
| 79 | +| Feature | CppUTest | ASAN | Both | |
| 80 | +|---------|----------|------|------| |
| 81 | +| Leak detection | ✓ | ✓ | ASAN only | |
| 82 | +| Buffer overflow | - | ✓ | ✓ | |
| 83 | +| Use-after-free | - | ✓ | ✓ | |
| 84 | +| Double-free | - | ✓ | ✓ | |
| 85 | +| Integer overflow | - | ✓ (UBSAN) | ✓ | |
| 86 | +| Stack issues | - | ✓ | ✓ | |
| 87 | +| Uninitialized reads | - | Limited | Limited | |
| 88 | + |
| 89 | +**Recommendation**: Use ASAN for comprehensive memory safety testing. |
| 90 | + |
| 91 | +## Verification |
| 92 | + |
| 93 | +To verify there are no conflicts: |
| 94 | + |
| 95 | +```bash |
| 96 | +# Build with ASAN |
| 97 | +cd bin/posix && cmake -DENABLE_ADDRESS_SANITIZER=ON . && make -j$(nproc) |
| 98 | + |
| 99 | +# Run tests - should see no conflicts |
| 100 | +ASAN_OPTIONS="verbosity=0" ./tests/OpENer_Tests -g NetworkHandlerSecurity |
| 101 | + |
| 102 | +# Check for ASAN errors (exit code 1 = error found) |
| 103 | +echo "Exit code: $?" |
| 104 | +``` |
| 105 | + |
| 106 | +## References |
| 107 | + |
| 108 | +- **CppUTest Memory Management**: Uses `CHECK_EQUAL_TEXT`, `CHECK_EQUAL_NOCASE_TEXT` for memory checks |
| 109 | +- **ASAN Documentation**: https://github.com/google/sanitizers/wiki/AddressSanitizer |
| 110 | +- **Best Practices**: Disable CppUTest's memory checking when using compiler sanitizers |
| 111 | + |
| 112 | +## Future Improvements |
| 113 | + |
| 114 | +1. Could implement custom ASAN suppressions file if false positives occur |
| 115 | +2. Could run both tools separately for comprehensive coverage |
| 116 | +3. Could add environment variable to control behavior at runtime |
| 117 | + |
| 118 | +--- |
| 119 | + |
| 120 | +**Status**: ✅ CppUTest and ASAN are now compatible and won't conflict |
0 commit comments