build_with_sanitizers.sh 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/bin/bash
  2. ################################################################################
  3. # AddressSanitizer Build Script for OpENer
  4. #
  5. # This script builds OpENer with AddressSanitizer and UndefinedBehaviorSanitizer
  6. # enabled for comprehensive security vulnerability detection.
  7. #
  8. # Usage:
  9. # ./build_with_sanitizers.sh [asan|ubsan|both]
  10. #
  11. # Environment Variables:
  12. # ASAN_OPTIONS: Configure AddressSanitizer behavior
  13. # UBSAN_OPTIONS: Configure UndefinedBehaviorSanitizer behavior
  14. #
  15. # Examples:
  16. # ./build_with_sanitizers.sh asan # Only AddressSanitizer
  17. # ./build_with_sanitizers.sh ubsan # Only UndefinedBehaviorSanitizer
  18. # ./build_with_sanitizers.sh both # Both sanitizers
  19. #
  20. ################################################################################
  21. set -e
  22. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  23. PROJECT_ROOT="$SCRIPT_DIR"
  24. BUILD_DIR="${PROJECT_ROOT}/build_sanitizer"
  25. # Parse arguments
  26. SANITIZER_TYPE="${1:-both}"
  27. if [[ ! "$SANITIZER_TYPE" =~ ^(asan|ubsan|both)$ ]]; then
  28. echo "Usage: $0 [asan|ubsan|both]"
  29. exit 1
  30. fi
  31. echo "=============================================="
  32. echo "OpENer Security Build with Sanitizers"
  33. echo "=============================================="
  34. echo "Sanitizer Type: $SANITIZER_TYPE"
  35. echo "Build Directory: $BUILD_DIR"
  36. echo ""
  37. # Create build directory
  38. mkdir -p "$BUILD_DIR"
  39. cd "$BUILD_DIR"
  40. # Configure CMake with sanitizer options
  41. CMAKE_ARGS="-DCMAKE_BUILD_TYPE=Debug -DOpENer_PLATFORM=POSIX -DOpENer_TESTS=ON"
  42. if [[ "$SANITIZER_TYPE" == "asan" || "$SANITIZER_TYPE" == "both" ]]; then
  43. CMAKE_ARGS="$CMAKE_ARGS -DENABLE_ADDRESS_SANITIZER=ON"
  44. echo "[+] AddressSanitizer enabled"
  45. fi
  46. if [[ "$SANITIZER_TYPE" == "ubsan" || "$SANITIZER_TYPE" == "both" ]]; then
  47. CMAKE_ARGS="$CMAKE_ARGS -DENABLE_UNDEFINED_SANITIZER=ON"
  48. echo "[+] UndefinedBehaviorSanitizer enabled"
  49. fi
  50. echo ""
  51. echo "Configuring CMake..."
  52. cmake "$CMAKE_ARGS" "$PROJECT_ROOT/source"
  53. echo ""
  54. echo "Building..."
  55. cmake --build . --config Debug --parallel "$(nproc)"
  56. echo ""
  57. echo "=============================================="
  58. echo "Build Complete!"
  59. echo "=============================================="
  60. echo ""
  61. echo "Run tests with:"
  62. echo " cd $BUILD_DIR"
  63. echo " ASAN_OPTIONS='detect_leaks=1:halt_on_error=1' ./tests/OpENer_Tests -v -c"
  64. echo ""