# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

# bazel configurations for running tests under sanitizers.
# Based on https://github.com/bazelment/trunk/blob/master/tools/bazel.rc

# Enable automatic configs based on platform
common --enable_platform_specific_config

# Make globs that don't match anything fail
common --incompatible_disallow_empty_glob

# Needed by gRPC to build on some platforms.
build --copt -DGRPC_BAZEL_BUILD

# Workaround abseil libraries missing symbols
build:windows --dynamic_mode=off

# Set minimum supported C++ version
build:macos --host_cxxopt=-std=c++17 --cxxopt=-std=c++17
build:linux --host_cxxopt=-std=c++17 --cxxopt=-std=c++17
build:windows --host_cxxopt=/std:c++17 --cxxopt=/std:c++17

# --config=asan : Address Sanitizer.
common:asan --copt -DADDRESS_SANITIZER
common:asan --copt -fsanitize=address
common:asan --copt -fsanitize-address-use-after-scope
common:asan --copt -fno-omit-frame-pointer
common:asan --linkopt -fsanitize=address
common:asan --linkopt -fsanitize-address-use-after-scope
common:asan --cc_output_directory_tag=asan
common:asan --test_env=ASAN_OPTIONS=halt_on_error=1:print_stacktrace=1:detect_stack_use_after_return=1

# --config=tsan : Thread Sanitizer.
common:tsan --copt -fsanitize=thread
common:tsan --copt -DTHREAD_SANITIZER
common:tsan --linkopt -fsanitize=thread
common:tsan --cc_output_directory_tag=tsan
# This is needed to address false positive problem with abseil.The same setting as gRPC
# https://github.com/google/sanitizers/issues/953
common:tsan --test_env=TSAN_OPTIONS=report_atomic_races=0

# --config=ubsan : Undefined Behavior Sanitizer
# Restrict UBSan compile instrumentation to workspace labels only.
#
# Bazel syntax: --per_file_copt=<label_regex>@<compiler_flag>
#   ^//.*       matches workspace labels (for example: //sdk/..., //api/...)
#   @repo//...  are external repositories and intentionally excluded
#
# This avoids sanitizer instrumentation issues in third-party dependencies,
# while still linking UBSan runtime into opentelemetry-cpp test binaries.
common:ubsan --per_file_copt=^//.*@-fsanitize=undefined
common:ubsan --per_file_copt=^//.*@-fno-sanitize-recover=undefined
common:ubsan --per_file_copt=^//.*@-fno-sanitize=vptr
common:ubsan --linkopt -fsanitize=undefined
common:ubsan --linkopt -fno-sanitize=vptr
common:ubsan --cc_output_directory_tag=ubsan
common:ubsan --test_env=UBSAN_OPTIONS=halt_on_error=1:print_stacktrace=1
