xref: /aosp_15_r20/external/libvpx/tools/set_analyzer_env.sh (revision fb1b10ab9aebc7c7068eedab379b749d7e3900be)
1##  Copyright (c) 2018 The WebM project authors. All Rights Reserved.
2##
3##  Use of this source code is governed by a BSD-style license
4##  that can be found in the LICENSE file in the root of the source
5##  tree. An additional intellectual property rights grant can be found
6##  in the file PATENTS.  All contributing project authors may
7##  be found in the AUTHORS file in the root of the source tree.
8##
9##  Sourcing this file sets environment variables to simplify setting up
10##  sanitizer builds and testing.
11
12sanitizer="${1}"
13
14case "${sanitizer}" in
15  address) ;;
16  cfi) ;;
17  integer) ;;
18  memory) ;;
19  thread) ;;
20  undefined) ;;
21  clear)
22    echo "Clearing environment:"
23    set -x
24    unset CC CXX LD AR
25    unset CFLAGS CXXFLAGS LDFLAGS
26    unset ASAN_OPTIONS MSAN_OPTIONS TSAN_OPTIONS UBSAN_OPTIONS
27    set +x
28    return
29    ;;
30  *)
31    echo "Usage: source set_analyzer_env.sh [<sanitizer>|clear]"
32    echo "  Supported sanitizers:"
33    echo "    address cfi integer memory thread undefined"
34    return 1
35    ;;
36esac
37
38if [ ! $(which clang) ]; then
39  # TODO(johannkoenig): Support gcc analyzers.
40  echo "ERROR: 'clang' must be in your PATH"
41  return 1
42fi
43
44# Warnings.
45if [ "${sanitizer}" = "undefined" -o "${sanitizer}" = "integer" ]; then
46  echo "WARNING: When building the ${sanitizer} sanitizer for 32 bit targets"
47  echo "you must run:"
48  echo "export LDFLAGS=\"\${LDFLAGS} --rtlib=compiler-rt -lgcc_s\""
49  echo "See http://llvm.org/bugs/show_bug.cgi?id=17693 for details."
50fi
51
52if [ "${sanitizer}" = "undefined" ]; then
53  major_version=$(clang --version | head -n 1 \
54    | grep -o -E "[[:digit:]]\.[[:digit:]]\.[[:digit:]]" | cut -f1 -d.)
55  if [ ${major_version} -eq 5 ]; then
56    echo "WARNING: clang v5 has a problem with vp9 x86_64 high bit depth"
57    echo "configurations. It can take ~40 minutes to compile"
58    echo "vpx_dsp/x86/fwd_txfm_sse2.c"
59    echo "clang v4 did not have this issue."
60  fi
61fi
62
63echo "It is recommended to configure with '--enable-debug' to improve stack"
64echo "traces. On mac builds, run 'dysmutil' on the output binaries (vpxenc,"
65echo "test_libvpx, etc) to link the stack traces to source code lines."
66
67# Build configuration.
68cflags="-fsanitize=${sanitizer}"
69ldflags="-fsanitize=${sanitizer}"
70
71# Useful backtraces.
72cflags="${cflags} -fno-omit-frame-pointer"
73# Exact backtraces.
74cflags="${cflags} -fno-optimize-sibling-calls"
75
76if [ "${sanitizer}" = "cfi" ]; then
77  # https://clang.llvm.org/docs/ControlFlowIntegrity.html
78  cflags="${cflags} -fno-sanitize-trap=cfi -flto -fvisibility=hidden"
79  ldflags="${ldflags} -fno-sanitize-trap=cfi -flto -fuse-ld=gold"
80  export AR="llvm-ar"
81fi
82
83set -x
84export CC="clang"
85export CXX="clang++"
86export LD="clang++"
87
88export CFLAGS="${cflags}"
89export CXXFLAGS="${cflags}"
90export LDFLAGS="${ldflags}"
91set +x
92
93# Execution configuration.
94sanitizer_options=""
95sanitizer_options="${sanitizer_options}:handle_segv=1"
96sanitizer_options="${sanitizer_options}:handle_abort=1"
97sanitizer_options="${sanitizer_options}:handle_sigfpe=1"
98sanitizer_options="${sanitizer_options}:fast_unwind_on_fatal=1"
99sanitizer_options="${sanitizer_options}:allocator_may_return_null=1"
100
101case "${sanitizer}" in
102  address)
103    sanitizer_options="${sanitizer_options}:detect_stack_use_after_return=1"
104    sanitizer_options="${sanitizer_options}:max_uar_stack_size_log=17"
105    set -x
106    export ASAN_OPTIONS="${sanitizer_options}"
107    set +x
108    ;;
109  cfi)
110    # No environment settings
111    ;;
112  memory)
113    set -x
114    export MSAN_OPTIONS="${sanitizer_options}"
115    set +x
116    ;;
117  thread)
118    # The thread sanitizer uses an entirely independent set of options.
119    set -x
120    export TSAN_OPTIONS="halt_on_error=1"
121    set +x
122    ;;
123  undefined|integer)
124    sanitizer_options="${sanitizer_options}:print_stacktrace=1"
125    set -x
126    export UBSAN_OPTIONS="${sanitizer_options}"
127    set +x
128    ;;
129esac
130