summaryrefslogtreecommitdiff
path: root/infra/run_unit_tests.sh
blob: 96ce5abb521b0cde2ffc91b3ed6a25721159b410 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash
# Copyright (c) 2021, Google Inc. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#   * Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#
#   * Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in
#     the documentation and/or other materials provided with the
#     distribution.
#
#   * Neither the name of Google nor the names of its contributors may
#     be used to endorse or promote products derived from this software
#     without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set -xeo pipefail
readonly GOOGLETEST_REPO="https://github.com/google/googletest.git"
readonly LIBWEBM_ROOT="$(realpath "$(dirname "$0")/..")"
readonly WORKSPACE=${WORKSPACE:-"$(mktemp -d -t webm.XXX)"}

# shellcheck source=infra/common.sh
source "${LIBWEBM_ROOT}/infra/common.sh"

usage() {
  cat << EOF
Usage: $(basename "$0") TARGET
Options:
TARGET supported targets: (x86-asan, x86-ubsan, x86_64-asan, x86_64-ubsan,
       x86_64-valgrind)
Global variables:
WORKSPACE directory where the build is done
EOF
}

#######################################
# Run valgrind
#######################################
run_valgrind() {
  valgrind \
    --leak-check=full \
    --show-reachable=yes \
    --track-origins=yes \
    --error-exitcode=1 \
    "$@"
}

#######################################
# Ensure GoogleTest repository is setup
#
# Globals:
#  GOOGLETEST_REPO googletest repository url
#  WORKSPACE directory where the build is done
#######################################
ensure_googletest() {
  local googletest_dir
  googletest_dir="${WORKSPACE}/googletest"

  if [[ ! -d "${googletest_dir}" ]] || ! git -C "${googletest_dir}" pull; then
    rm -rf "${googletest_dir}"
    git clone --depth 1 "${GOOGLETEST_REPO}" "${googletest_dir}"
  fi

  opts+=("-DGTEST_SRC_DIR=${googletest_dir}")
}

#######################################
# Symbolizes and dumps warnings in the address sanitizer log
# Globals:
#  BUILD_DIR directory where the build is done
#  SANITIZER_LOG path to sanitizer log file
#######################################
dump_sanitizer_log() {
  if [[ "$#" -ne 1 ]]; then
    return 1
  fi

  local asan_symbolize_tool
  if [[ -x "$(command -v asan_symbolize)" ]]; then
    asan_symbolize_tool="asan_symbolize"
  else
    asan_symbolize_tool="asan_symbolize.py"
  fi

  local target
  target="$1"
  case "${target}" in
    *-asan)
      asanlog_symbolized="${BUILD_DIR}/asan_log.asanlog_symbolized"
      grep -v 'Invalid VP9' "${SANITIZER_LOG}" > "${SANITIZER_LOG}.2" || true
      "${asan_symbolize_tool}" "${BUILD_DIR}" c++filt < "${SANITIZER_LOG}.2" \
        > "${asanlog_symbolized}"
      if [[ -s "${asanlog_symbolized}" ]]; then
        cat "${asanlog_symbolized}"
        return 1
      fi
      ;;
    *) ;;  # No other sanitizer options are required
    # TODO(b/185520494): Handle ubsan warning output inspection
  esac
}

################################################################################
echo "Unit testing libwebm in ${WORKSPACE}"

if [[ ! -d "${WORKSPACE}" ]]; then
  log_err "${WORKSPACE} directory does not exist"
  exit 1
fi

TARGET=${1:? "$(
  echo
  usage
)"}
readonly BUILD_DIR="${WORKSPACE}/tests-${TARGET}"
readonly SANITIZER_LOG="${BUILD_DIR}/sanitizer_log"

# Create a fresh build directory.
trap 'dump_sanitizer_log ${TARGET}; cleanup' EXIT
make_build_dir "${BUILD_DIR}"

case "${TARGET}" in
  x86-*) CXX='clang++ -m32' ;;
  x86_64-*) CXX=clang++ ;;
  *)
    log_err "${TARGET} should have x86 or x86_64 prefix."
    usage
    exit 1
    ;;
esac
# cmake (3.4.3) will only accept the -m32 variants when used via the CXX env
# var.
export CXX
opts+=("-DCMAKE_BUILD_TYPE=Debug" "-DENABLE_TESTS=ON")
case "${TARGET}" in
  *-asan) opts+=("-DCMAKE_CXX_FLAGS=-fsanitize=address") ;;
  *-ubsan)
    opts+=("-DCMAKE_CXX_FLAGS=-fsanitize=integer")
    if [[ "${TARGET}" == "x86-ubsan" ]]; then
      # clang fails to find symbols when -fsanitize=integer is set in x86 arch.
      # https://bugs.llvm.org/show_bug.cgi?id=17693
      opts+=("-DCMAKE_CXX_FLAGS=-rtlib=compiler-rt")
      opts+=("-DCMAKE_EXE_LINKER_FLAGS=-lgcc_s")
    fi
    ;;
  *) ;;  # No additional flags needed.
esac

ensure_googletest
# Using pushd instead of -S/-B for backward compatibility with CMake < 3.13.x
pushd "${BUILD_DIR}"
cmake "${LIBWEBM_ROOT}" "${opts[@]}"
make -j 4
popd

UNIT_TESTS=()
while IFS='' read -r line; do
  UNIT_TESTS+=("${line}")
done < <(find "${BUILD_DIR}" -name '*_tests')

export LIBWEBM_TEST_DATA_PATH="${LIBWEBM_ROOT}/testing/testdata"
case "${TARGET}" in
  *-asan | *-ubsan)
    rm -f "${SANITIZER_LOG}"
    for test in "${UNIT_TESTS[@]}"; do
      "${test}" \
        --gtest_output="xml:${BUILD_DIR}/$(basename "${test}")_detail.xml" \
        3<&1 1>&2 2>&3 | tee -a "${SANITIZER_LOG}"
    done
    ;;
  *-valgrind)
    for test in "${UNIT_TESTS[@]}"; do
      run_valgrind --error-exitcode=1 "${test}" \
        --gtest_output="xml:${BUILD_DIR}/$(basename "${test}")_detail.xml"
    done
    ;;
  *)
    log_err "Unrecognized TARGET:${TARGET}."
    usage
    exit 1
    ;;
esac