xref: /linux/tools/objtool/tests/generic/test-ubsan-noise.sh (revision 5b6dca02b3e2254d2fd5706d76d7ab4610a67296)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# UBSAN instrumentation in an unchanged function must not make it look changed.
5#
6# Every instrumented operation gets a per-callsite metadata object in an
7# anonymous data section -- .data..Lubsan_data and .data..Lubsan_type from GCC,
8# .data..L__unnamed_ from Clang -- whose names are compiler-generated and mean
9# nothing across a rebuild.  is_uncorrelated_section() exists so klp diff does
10# not try to pair them up.
11#
12# Without that, the metadata belonging to a function nobody touched compares as
13# different and drags the function into the patch.  A livepatch which replaces
14# functions the patch never changed is not a build failure: it is a larger
15# patch than intended, taking its dependencies with it, and every extra
16# function is one more that can fail to correlate or to apply.
17#
18# Covers the same ground as corpus/x86_64-ubsan/{ubsan-shift-noise,
19# ubsan-metadata-data-section,gcc-ubsan-anonymous-data,ubsan-handler-cloning}
20# and corpus/x86_64-llvm-ubsan/{clang-ubsan-bounds-noise,
21# clang-ubsan-handler-cloning} in Joe Lawrence's klp-build unit test corpus.
22
23. "$(dirname "$0")/../lib.sh"
24
25setup
26build_pair ubsan_noise.c -fsanitize=shift
27
28# The premise: this compiler really did instrument, and left its metadata in an
29# anonymous section.  Without that the test is just test-basic again.
30ubsan_sec="$(in_sections orig.o |
31	grep -oE '\.data\.\.L(ubsan_data|__unnamed_)[A-Za-z0-9_.]*' | head -1)"
32[ -n "$ubsan_sec" ] ||
33	probe_skip "compiler emitted no anonymous UBSAN data section"
34assert_input_symbol untouched
35
36run_diff
37
38# The changed function is patched, and the untouched one is left alone despite
39# carrying instrumentation of its own.
40assert_patched touched
41assert_not_patched untouched
42
43# The handler the patched code calls has to come with it, or the clone calls
44# nothing when its check fires.
45out_symbols | grep -q '__ubsan_handle_' ||
46	fail "no __ubsan_handle_* reference in the patched output"
47
48pass "UBSAN metadata in an unchanged function does not drag it into the patch"
49