summaryrefslogtreecommitdiff
path: root/tools/objtool/tests/generic/fixtures/ubsan_noise.c
blob: bf59992541630e26caf5ec9abb550830e41d7a1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// SPDX-License-Identifier: GPL-2.0
/*
 * A translation unit built with UBSAN, where only one of two functions is
 * patched.
 *
 * Every instrumented operation gets a per-callsite metadata object in an
 * anonymous data section -- .data..Lubsan_data and .data..Lubsan_type from
 * GCC, .data..L__unnamed_ from Clang -- and a call to a __ubsan_handle_*
 * routine.  The names are compiler-generated and carry no meaning across a
 * rebuild, so klp diff has to treat those sections as uncorrelated rather than
 * pairing them up by name.
 *
 * untouched() is byte-identical in both builds and exists to catch the false
 * positive: if the metadata were correlated by name, its shifts would look
 * changed and it would be dragged into the patch.
 *
 * The shifts are what draw the instrumentation.  A bounds check would do as
 * well but neither compiler emits one for an index it can prove in range.
 */

static const char __modinfo[]
	__attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";

int shift_by(int v, int n);

int untouched(int v, int n)
{
	int s = 0;

	s += v << (n & 31);
	s += v << ((n + 1) & 31);
	s += shift_by(v, n);

	return s;
}

int touched(int v, int n)
{
	int s = 0;

	s += v << (n & 31);
#ifdef PATCHED
	s += v << ((n + 3) & 31);
#else
	s += v << ((n + 2) & 31);
#endif

	return s;
}