Files
carbon-lang/bazel/cc_toolchains/defs.bzl
T
Jon Ross-Perkins 7b9ec95118 Print ubsan stack traces (#5429)
Ran into this trying to debug #5428 

Before:

```
lex.cpp:793:21: runtime error: null pointer passed as argument 1, which is declared to never be null
string.h:90:51: note: nonnull attribute specified here
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior lex.cpp:793:21
```

After:

```
lex.cpp:793:21: runtime error: null pointer passed as argument 1, which is declared to never be null
string.h:90:51: note: nonnull attribute specified here
    #0 0x5572db22a680 in Carbon::Lex::Lexer::MakeLines(llvm::StringRef) /proc/self/cwd/toolchain/lex/lex.cpp:793:14
    #1 0x5572db227ee4 in Lex /proc/self/cwd/toolchain/lex/lex.cpp:738:3
(etc)

SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior lex.cpp:793:21
```

Interestingly, even though this is labelled as UB, it's using the
ASAN_SYMBOLIZER_PATH (specifically not LLVM_SYMBOLIZER_PATH). But
canonically UBSAN_SYMBOLIZER_PATH may also be used per
https://github.com/llvm/llvm-project/blob/main/compiler-rt/lib/ubsan/ubsan_flags.cpp#L53,
so I'm adding it to the list out of an excess of caution.

Also doing some small related cleanup:

- Removing `ASAN_SYMBOLIZER_PATH` from `--test_env` because it should
now be getting overridden by these settings (also was a little
inconsistent in that `LLVM_SYMBOLIZER_PATH` was not included).
- Improving the environment construction and documentation.
2025-05-06 17:39:42 +00:00

47 lines
1.5 KiB
Python

# Part of the Carbon Language project, under the Apache License v2.0 with LLVM
# Exceptions. See /LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""Provides helpers for cc rules. Intended for general consumption."""
load("@bazel_cc_toolchain//:clang_detected_variables.bzl", "llvm_symbolizer")
def cc_env():
"""Returns standard environment settings for a cc_binary.
In use, this looks like:
```
load("//bazel/cc_toolchains:defs.bzl", "cc_env")
cc_binary(
...
env = cc_env(),
)
```
We're currently setting this on a target-by-target basis, mainly because
it's difficult to modify default behaviors.
"""
# Settings which apply cross-platform.
# buildifier: disable=unsorted-dict-items
common_env = {
"LLVM_SYMBOLIZER_PATH": llvm_symbolizer,
# Sanitizers don't use LLVM as fallback, but sometimes ASAN may be used
# for UBSAN errors; we still set UBSAN in case it's directly used.
"ASAN_SYMBOLIZER_PATH": llvm_symbolizer,
"UBSAN_SYMBOLIZER_PATH": llvm_symbolizer,
# Default to printing traces for UBSAN.
"UBSAN_OPTIONS": "print_stacktrace=1",
}
# On macOS, there's a nano zone allocation warning when asan is enabled.
# This suppresses the warning in `bazel run`.
macos_env = {"MallocNanoZone": "0"}
return common_env | select({
"//bazel/cc_toolchains:macos_asan": macos_env,
"//conditions:default": {},
})