Skip to content

Commit

Permalink
mtest: set ASAN_OPTIONS and UBSAN_OPTIONS to abort by default
Browse files Browse the repository at this point in the history
Do as we do for MALLOC_PERTURB and set a sensible value for both ASAN_OPTIONS
and UBSAN_OPTIONS to abort on failure and give more helpful output at the
same time. We do not set these options if the user has exported a value
themselves to allow override.

In the last week alone, I've observed two cases where people were expecting
sanitizers to abort on failure and were surprised when it didn't:
1) git/git@252d693
2) https://gitlab.freedesktop.org/pipewire/pipewire/-/commit/c47df433f7bc9936fc59b323ca5e53ea4a04f40e

Correct this - which is in-line with meson's DWIM/DTRT philosophy.

Signed-off-by: Sam James <sam@gentoo.org>
  • Loading branch information
thesamesam authored and eli-schwartz committed Oct 19, 2023
1 parent e2a87af commit 7b7d2e0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/markdown/Unit-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ set to a random value between 1..255. This can help find memory leaks on
configurations using glibc, including with non-GCC compilers. This feature
can be disabled as discussed in [[test]].

### ASAN_OPTIONS and UBSAN_OPTIONS

By default, the environment variables `ASAN_OPTIONS` and `UBSAN_OPTIONS` are
set to enable aborting on detected violations and to give a backtrace. This
feature can be disabled as discussed in [[test]].

## Coverage

If you enable coverage measurements by giving Meson the command line
Expand Down
5 changes: 5 additions & 0 deletions docs/markdown/snippets/sanitizers_test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Tests now abort on errors by default under sanitizers

Sanitizers like AddressSanitizer and UndefinedBehaviorSanitizer do not abort
by default on detected violations. Meson now exports `ASAN_OPTIONS` and `UBSAN_OPTIONS`
when unset in the environment to provide sensible abort-by-default behavior.
4 changes: 4 additions & 0 deletions docs/yaml/functions/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ description: |
test(..., env: nomalloc, ...)
```
By default, the environment variables `ASAN_OPTIONS` and `UBSAN_OPTIONS` are
set to enable aborting on detected violations and to give a backtrace. To suppress
this, `ASAN_OPTIONS` and `UBSAN_OPTIONS` can be set in the environment.
In addition to running individual executables as test cases, `test()`
can also be used to invoke an external test harness. In this case,
it is best to use `verbose: true` *(since 0.62.0)* and, if supported
Expand Down
9 changes: 9 additions & 0 deletions mesonbuild/mtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1418,6 +1418,15 @@ def __init__(self, test: TestSerialisation, env: T.Dict[str, str], name: str,
if ('MALLOC_PERTURB_' not in env or not env['MALLOC_PERTURB_']) and not options.benchmark:
env['MALLOC_PERTURB_'] = str(random.randint(1, 255))

# Sanitizers do not default to aborting on error. This is counter to
# expectations when using -Db_sanitize and has led to confusion in the wild
# in CI. Set our own values of {ASAN,UBSAN}_OPTOINS to rectify this, but
# only if the user has not defined them.
if ('ASAN_OPTIONS' not in env or not env['ASAN_OPTIONS']):
env['ASAN_OPTIONS'] = 'halt_on_error=1:abort_on_error=1:print_summary=1'
if ('UBSAN_OPTIONS' not in env or not env['UBSAN_OPTIONS']):
env['UBSAN_OPTIONS'] = 'halt_on_error=1:abort_on_error=1:print_summary=1:print_stacktrace=1'

if self.options.gdb or self.test.timeout is None or self.test.timeout <= 0:
timeout = None
elif self.options.timeout_multiplier is None:
Expand Down

0 comments on commit 7b7d2e0

Please sign in to comment.