diff options
Diffstat (limited to 'scripts/build/v-sanitizer.inc')
-rw-r--r-- | scripts/build/v-sanitizer.inc | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/scripts/build/v-sanitizer.inc b/scripts/build/v-sanitizer.inc new file mode 100644 index 00000000..be9b3b39 --- /dev/null +++ b/scripts/build/v-sanitizer.inc @@ -0,0 +1,88 @@ +#!/bin/bash +# Variables for sanitizer handling + +required_variables_sanitizer=( + "SANITIZER_BUILD" +) + +artifact_dependency_sanitizer=("") + +required_variables_check_sanitizer() { + # Allowe empty sanitizer builds + [[ -z "${SANITIZER_BUILD}" ]] && return 0 + + local sanitizers + IFS=":" read -r -a sanitizers <<< "${SANITIZER_BUILD}" + for sanitizer in "${sanitizers[@]}"; do + [[ -z "${sanitizer}" ]] && continue + [[ "${sanitizer}" == "address" ]] && continue + [[ "${sanitizer}" == "memory" ]] && continue + [[ "${sanitizer}" == "undefined" ]] && continue + + echo "Unknown sanitizer: $sanitizer" + exit 1 + done +} + +setup_variables_sanitizer() { + if [[ -z "${SANITIZER_BUILD}" ]]; then + SANITIZERS=("") + SANITIZER_SUFFIX="" + return 0 + fi + # Remove ":" separator + local sanitizers + local e + local sorted_sanitizers + + IFS=":" read -r -a sanitizers <<< "${SANITIZER_BUILD}" + sorted_sanitizers=( $( for e in "${sanitizers[@]}"; do echo "$e"; done | sort) ) + + + # Set variables used by clients. + ## Build variables + [[ -z ${SANITIZER_CXX_FLAGS+x} ]] && SANITIZER_CXX_FLAGS=("") + [[ -z ${SANITIZER_C_FLAGS+x} ]] && SANITIZER_C_FLAGS=("") + [[ -z ${SANITIZER_LD_FLAGS+x} ]] && SANITIZER_LD_FLAGS=("") + + # Docker_suffix + SANITIZER_SUFFIX="" + + local flags + for sanitizer in "${sorted_sanitizers[@]}"; do + [[ -z "${sanitizer}" ]] && continue + # AddressSanitizer + if [ "${sanitizer}" == "address" ]; then + echo "Using ASan" + flags=( "-fsanitize=address" "-fno-omit-frame-pointer" "-g") + SANITIZER_CXX_FLAGS+=("${flags[@]}") + SANITIZER_C_FLAGS+=("${flags[@]}") + SANITIZER_LD_FLAGS+=("${flags[@]}") + SANITIZER_SUFFIX+="_asan" + continue + fi + + # Undefined Behaviour Sanitizer + if [ "${sanitizer}" == "undefined" ]; then + echo "Using UBSan" + flags=("-fsanitize=undefined" "-fno-omit-frame-pointer" "-g") + SANITIZER_CXX_FLAGS+=("${flags[@]}") + SANITIZER_C_FLAGS+=("${flags[@]}") + SANITIZER_LD_FLAGS+=("${flags[@]}" "-lubsan" "-fuse-ld=gold") + SANITIZER_SUFFIX+="_ubsan" + continue + fi + + # Undefined Behaviour Sanitizer + if [ "${sanitizer}" == "memory" ]; then + echo "Using Memory Sanitizer" + SANITIZER_SUFFIX+="_memsan" + continue + fi + + echo "Unknown sanitizer: $sanitizer" + exit 1 + done + + SANITIZERS=("${sorted_sanitizers[@]}") + } \ No newline at end of file |