|
| 1 | +#!/bin/bash |
| 2 | +# Run tests with AddressSanitizer (ASan/LSan) to detect memory leaks and errors |
| 3 | +# in the native N-API addon. |
| 4 | +# |
| 5 | +# Requires: ROS2 sourced, g++ with libasan |
| 6 | +# |
| 7 | +# Usage: |
| 8 | +# bash scripts/run_asan_test.sh # rebuild + run all tests |
| 9 | +# bash scripts/run_asan_test.sh test/test-node.js # rebuild + run specific test(s) |
| 10 | +# bash scripts/run_asan_test.sh --no-build test/test-node.js # skip rebuild |
| 11 | +# bash scripts/run_asan_test.sh --exclude test/test-serialization.js # exclude specific test(s) |
| 12 | + |
| 13 | +set -e |
| 14 | + |
| 15 | +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" |
| 16 | +PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 17 | +cd "$PROJECT_DIR" |
| 18 | + |
| 19 | +# Parse flags |
| 20 | +DO_BUILD=1 |
| 21 | +ARGS=() |
| 22 | +EXCLUDES=() |
| 23 | +NEXT_IS_EXCLUDE=0 |
| 24 | +for arg in "$@"; do |
| 25 | + if [[ $NEXT_IS_EXCLUDE -eq 1 ]]; then |
| 26 | + EXCLUDES+=("$arg") |
| 27 | + NEXT_IS_EXCLUDE=0 |
| 28 | + elif [[ "$arg" == "--no-build" ]]; then |
| 29 | + DO_BUILD=0 |
| 30 | + elif [[ "$arg" == "--exclude" ]]; then |
| 31 | + NEXT_IS_EXCLUDE=1 |
| 32 | + else |
| 33 | + ARGS+=("$arg") |
| 34 | + fi |
| 35 | +done |
| 36 | + |
| 37 | +if [[ $NEXT_IS_EXCLUDE -eq 1 ]]; then |
| 38 | + echo "Error: --exclude requires a test file argument" |
| 39 | + echo "Usage: bash scripts/run_asan_test.sh --exclude test/test-foo.js" |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +# Step 1: Build with ASan |
| 44 | +if [[ $DO_BUILD -eq 1 ]]; then |
| 45 | + echo "=== Building with AddressSanitizer ===" |
| 46 | + CXXFLAGS="-fsanitize=address" npx node-gyp -j 16 rebuild --debug |
| 47 | +fi |
| 48 | + |
| 49 | +# Step 2: Locate libasan |
| 50 | +LIBASAN=$(g++ -print-file-name=libasan.so) |
| 51 | +if [[ "$LIBASAN" == "libasan.so" ]]; then |
| 52 | + # g++ returned just the name, try to find the full path |
| 53 | + LIBASAN=$(find /usr/lib* -name "libasan.so*" -type f 2>/dev/null | head -1) |
| 54 | +fi |
| 55 | + |
| 56 | +if [[ -z "$LIBASAN" || ! -f "$LIBASAN" ]]; then |
| 57 | + echo "Warning: Could not find libasan.so, proceeding without LD_PRELOAD" |
| 58 | + echo "If you see 'ASan not the first DSO' errors, install libasan: sudo apt install libasan6" |
| 59 | + LIBASAN="" |
| 60 | +fi |
| 61 | + |
| 62 | +# Step 3: Set up environment |
| 63 | +export LSAN_OPTIONS="suppressions=$PROJECT_DIR/suppr.txt" |
| 64 | +if [[ -n "$LIBASAN" ]]; then |
| 65 | + export LD_PRELOAD="$LIBASAN" |
| 66 | +fi |
| 67 | + |
| 68 | +# Step 3.5: Temporarily hide prebuilds so the debug build is loaded |
| 69 | +PREBUILDS_DIR="$PROJECT_DIR/prebuilds" |
| 70 | +PREBUILDS_BAK="$PROJECT_DIR/.prebuilds_asan_bak" |
| 71 | +MOVED_PREBUILDS=0 |
| 72 | +if [[ -d "$PREBUILDS_DIR" ]]; then |
| 73 | + if [[ -d "$PREBUILDS_BAK" ]]; then |
| 74 | + echo "Error: $PREBUILDS_BAK already exists (previous interrupted run?)." |
| 75 | + echo "Please remove it manually and retry: rm -rf $PREBUILDS_BAK" |
| 76 | + exit 1 |
| 77 | + fi |
| 78 | + mv "$PREBUILDS_DIR" "$PREBUILDS_BAK" |
| 79 | + MOVED_PREBUILDS=1 |
| 80 | +fi |
| 81 | + |
| 82 | +# Restore prebuilds on exit (even on failure) |
| 83 | +cleanup() { |
| 84 | + if [[ $MOVED_PREBUILDS -eq 1 && -d "$PREBUILDS_BAK" ]]; then |
| 85 | + mv "$PREBUILDS_BAK" "$PREBUILDS_DIR" |
| 86 | + fi |
| 87 | +} |
| 88 | +trap cleanup EXIT |
| 89 | + |
| 90 | +# Step 4: Build mocha exclude args from --exclude flags, blocklist.json, and asan_blocklist.json |
| 91 | +MOCHA_EXCLUDES=() |
| 92 | +for exc in "${EXCLUDES[@]}"; do |
| 93 | + MOCHA_EXCLUDES+=(--ignore "$exc") |
| 94 | +done |
| 95 | + |
| 96 | +# Auto-exclude tests from blocklist.json (Linux entries) and asan_blocklist.json |
| 97 | +for blocklist in "$PROJECT_DIR/test/blocklist.json" "$PROJECT_DIR/test/asan_blocklist.json"; do |
| 98 | + if [[ -f "$blocklist" ]]; then |
| 99 | + while IFS= read -r test_file; do |
| 100 | + MOCHA_EXCLUDES+=(--ignore "test/$test_file") |
| 101 | + done < <(node -e " |
| 102 | + const bl = require('$blocklist'); |
| 103 | + const entries = bl.Linux || bl; |
| 104 | + if (Array.isArray(entries)) entries.forEach(t => console.log(t)); |
| 105 | + " 2>/dev/null) |
| 106 | + fi |
| 107 | +done |
| 108 | + |
| 109 | +# Step 5: Run tests |
| 110 | +if [[ ${#ARGS[@]} -gt 0 ]]; then |
| 111 | + echo "=== Running ASan test: ${ARGS[*]} ===" |
| 112 | + node --expose-gc node_modules/.bin/mocha -r test/gc-on-exit.js "${MOCHA_EXCLUDES[@]}" "${ARGS[@]}" |
| 113 | +else |
| 114 | + echo "=== Running all ASan tests ===" |
| 115 | + node --expose-gc node_modules/.bin/mocha -r test/gc-on-exit.js "${MOCHA_EXCLUDES[@]}" 'test/test-*.js' |
| 116 | +fi |
| 117 | + |
| 118 | +echo "=== ASan test complete ===" |
0 commit comments