|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 OR ISC |
| 4 | + |
| 5 | +# # Helper script for running aws-lc-rs tests under Valgrind |
| 6 | +# |
| 7 | +# Usage: |
| 8 | +# ./scripts/run-valgrind.sh [OPTIONS] [TEST_NAME] |
| 9 | +# |
| 10 | +# Examples: |
| 11 | +# ./scripts/run-valgrind.sh # Run all tests |
| 12 | +# ./scripts/run-valgrind.sh pqdsa_test # Run specific test |
| 13 | +# ./scripts/run-valgrind.sh --no-suppress # Run without suppressions |
| 14 | +# ./scripts/run-valgrind.sh --release # Run release build |
| 15 | + |
| 16 | +set -e |
| 17 | + |
| 18 | +# Colors for output |
| 19 | +RED='\033[0;31m' |
| 20 | +GREEN='\033[0;32m' |
| 21 | +YELLOW='\033[1;33m' |
| 22 | +BLUE='\033[0;34m' |
| 23 | +NC='\033[0m' # No Color |
| 24 | + |
| 25 | +# Default configuration |
| 26 | +USE_SUPPRESSIONS=1 |
| 27 | +BUILD_MODE="debug" |
| 28 | +LEAK_CHECK="full" |
| 29 | +SHOW_LEAK_KINDS="all" |
| 30 | +ERROR_EXITCODE=1 |
| 31 | +TEST_THREADS=1 |
| 32 | +FEATURES="unstable" |
| 33 | +PACKAGE="aws-lc-rs" |
| 34 | +VALGRIND_EXTRA_ARGS="" |
| 35 | +GEN_SUPPRESSIONS=0 |
| 36 | +export AWS_LC_RS_DISABLE_SLOW_TESTS=1 |
| 37 | + |
| 38 | +# Parse command line arguments |
| 39 | +while [[ $# -gt 0 ]]; do |
| 40 | + case $1 in |
| 41 | + --no-suppress) |
| 42 | + USE_SUPPRESSIONS=0 |
| 43 | + shift |
| 44 | + ;; |
| 45 | + --gen-suppressions) |
| 46 | + GEN_SUPPRESSIONS=1 |
| 47 | + shift |
| 48 | + ;; |
| 49 | + --release) |
| 50 | + BUILD_MODE="release" |
| 51 | + shift |
| 52 | + ;; |
| 53 | + --debug) |
| 54 | + BUILD_MODE="debug" |
| 55 | + shift |
| 56 | + ;; |
| 57 | + --threads) |
| 58 | + TEST_THREADS="$2" |
| 59 | + shift 2 |
| 60 | + ;; |
| 61 | + --features) |
| 62 | + FEATURES="$2" |
| 63 | + shift 2 |
| 64 | + ;; |
| 65 | + --package|-p) |
| 66 | + PACKAGE="$2" |
| 67 | + shift 2 |
| 68 | + ;; |
| 69 | + --help|-h) |
| 70 | + echo "Usage: $0 [OPTIONS] [TEST_NAME]" |
| 71 | + echo "" |
| 72 | + echo "Options:" |
| 73 | + echo " --no-suppress Disable Valgrind suppressions (show all warnings)" |
| 74 | + echo " --gen-suppressions Generate suppression rules for errors found" |
| 75 | + echo " --release Use release build (faster but less debug info)" |
| 76 | + echo " --debug Use debug build (default)" |
| 77 | + echo " --threads N Number of test threads (default: 1)" |
| 78 | + echo " --features FEATS Cargo features to enable (default: unstable)" |
| 79 | + echo " --package PKG Package to test (default: aws-lc-rs)" |
| 80 | + echo " --help, -h Show this help message" |
| 81 | + echo "" |
| 82 | + echo "Examples:" |
| 83 | + echo " $0 # Run all tests" |
| 84 | + echo " $0 pqdsa_test # Run specific test" |
| 85 | + echo " $0 --no-suppress # Run without suppressions" |
| 86 | + echo " $0 --gen-suppressions # Generate suppression rules" |
| 87 | + echo " $0 --release pqdsa_test # Run specific test in release mode" |
| 88 | + exit 0 |
| 89 | + ;; |
| 90 | + --*) |
| 91 | + echo -e "${RED}Error: Unknown option $1${NC}" |
| 92 | + exit 1 |
| 93 | + ;; |
| 94 | + *) |
| 95 | + # Assume it's a test name |
| 96 | + TEST_NAME="$1" |
| 97 | + shift |
| 98 | + ;; |
| 99 | + esac |
| 100 | +done |
| 101 | + |
| 102 | +# Get the repository root directory |
| 103 | +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" |
| 104 | +cd "$REPO_ROOT/aws-lc-rs" |
| 105 | + |
| 106 | +# Check if Valgrind is installed |
| 107 | +if ! command -v valgrind &> /dev/null; then |
| 108 | + echo -e "${RED}Error: Valgrind is not installed${NC}" |
| 109 | + echo "Install it with:" |
| 110 | + echo " Ubuntu/Debian: sudo apt-get install valgrind" |
| 111 | + echo " macOS: brew install valgrind" |
| 112 | + exit 1 |
| 113 | +fi |
| 114 | + |
| 115 | +# Build Valgrind command |
| 116 | +VALGRIND_CMD="valgrind --error-exitcode=${ERROR_EXITCODE} --leak-check=${LEAK_CHECK} --show-leak-kinds=${SHOW_LEAK_KINDS}" |
| 117 | + |
| 118 | +# Add gen-suppressions if enabled |
| 119 | +if [ $GEN_SUPPRESSIONS -eq 1 ]; then |
| 120 | + VALGRIND_CMD="${VALGRIND_CMD} --gen-suppressions=all" |
| 121 | + echo -e "${BLUE}Generating suppression rules for all errors${NC}" |
| 122 | + # Disable error exit code when generating suppressions to see all issues |
| 123 | + ERROR_EXITCODE=0 |
| 124 | +fi |
| 125 | + |
| 126 | +# Add suppression file if enabled |
| 127 | +if [ $USE_SUPPRESSIONS -eq 1 ]; then |
| 128 | + SUPPRESSION_FILE="${REPO_ROOT}/.valgrind/rust-test.supp" |
| 129 | + if [ -f "$SUPPRESSION_FILE" ]; then |
| 130 | + VALGRIND_CMD="${VALGRIND_CMD} --suppressions=${SUPPRESSION_FILE}" |
| 131 | + echo -e "${BLUE}Using suppressions from: ${SUPPRESSION_FILE}${NC}" |
| 132 | + else |
| 133 | + echo -e "${YELLOW}Warning: Suppression file not found: ${SUPPRESSION_FILE}${NC}" |
| 134 | + fi |
| 135 | +else |
| 136 | + echo -e "${YELLOW}Running WITHOUT suppressions - expect false positives${NC}" |
| 137 | +fi |
| 138 | + |
| 139 | +# Add any extra Valgrind arguments |
| 140 | +if [ -n "$VALGRIND_EXTRA_ARGS" ]; then |
| 141 | + VALGRIND_CMD="${VALGRIND_CMD} ${VALGRIND_EXTRA_ARGS}" |
| 142 | +fi |
| 143 | + |
| 144 | +# Build cargo command |
| 145 | +CARGO_CMD="cargo test -p ${PACKAGE} --features ${FEATURES}" |
| 146 | + |
| 147 | +if [ "$BUILD_MODE" = "release" ]; then |
| 148 | + CARGO_CMD="${CARGO_CMD} --release" |
| 149 | + echo -e "${BLUE}Using release build${NC}" |
| 150 | +else |
| 151 | + echo -e "${BLUE}Using debug build${NC}" |
| 152 | +fi |
| 153 | + |
| 154 | +# Add test name if provided |
| 155 | +if [ -n "$TEST_NAME" ]; then |
| 156 | + CARGO_CMD="${CARGO_CMD} --test ${TEST_NAME}" |
| 157 | + echo -e "${BLUE}Running test: ${TEST_NAME}${NC}" |
| 158 | +else |
| 159 | + echo -e "${BLUE}Running all tests${NC}" |
| 160 | +fi |
| 161 | + |
| 162 | +# Add test arguments |
| 163 | +CARGO_CMD="${CARGO_CMD} -- --test-threads=${TEST_THREADS}" |
| 164 | + |
| 165 | +# Print configuration |
| 166 | +echo -e "${GREEN}=== Valgrind Test Configuration ===${NC}" |
| 167 | +echo "Package: ${PACKAGE}" |
| 168 | +echo "Features: ${FEATURES}" |
| 169 | +echo "Build: ${BUILD_MODE}" |
| 170 | +echo "Test threads: ${TEST_THREADS}" |
| 171 | +echo "Suppressions: $([ $USE_SUPPRESSIONS -eq 1 ] && echo 'enabled' || echo 'disabled')" |
| 172 | +echo "Generate suppressions: $([ $GEN_SUPPRESSIONS -eq 1 ] && echo 'enabled' || echo 'disabled')" |
| 173 | +echo "" |
| 174 | + |
| 175 | +# Export environment variables |
| 176 | +export CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="${VALGRIND_CMD}" |
| 177 | +export AWS_LC_RS_DISABLE_SLOW_TESTS=1 |
| 178 | + |
| 179 | +echo -e "${GREEN}=== Starting Valgrind Test Run ===${NC}" |
| 180 | +echo "Command: ${CARGO_CMD}" |
| 181 | +echo "" |
| 182 | + |
| 183 | +# Run the tests |
| 184 | +if eval ${CARGO_CMD}; then |
| 185 | + echo "" |
| 186 | + echo -e "${GREEN}=== Valgrind tests PASSED ===${NC}" |
| 187 | + exit 0 |
| 188 | +else |
| 189 | + EXIT_CODE=$? |
| 190 | + echo "" |
| 191 | + echo -e "${RED}=== Valgrind tests FAILED ===${NC}" |
| 192 | + echo "" |
| 193 | + echo "Possible causes:" |
| 194 | + echo " 1. Memory leak detected (check output above)" |
| 195 | + echo " 2. Uninitialized memory usage" |
| 196 | + echo " 3. Invalid memory access" |
| 197 | + echo "" |
| 198 | + echo "Next steps:" |
| 199 | + echo " - Review the Valgrind output above" |
| 200 | + echo " - Check .valgrind/KNOWN_ISSUES.md for known issues" |
| 201 | + echo " - Run with --no-suppress to see all warnings" |
| 202 | + echo " - Run with --gen-suppressions to generate suppression rules" |
| 203 | + echo " - For false positives in stdlib, add to .valgrind/rust-test.supp" |
| 204 | + exit $EXIT_CODE |
| 205 | +fi |
0 commit comments