xref: /linux/tools/objtool/tests/generic/fixtures/ubsan_noise.c (revision 5b6dca02b3e2254d2fd5706d76d7ab4610a67296)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * A translation unit built with UBSAN, where only one of two functions is
4  * patched.
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
8  * GCC, .data..L__unnamed_ from Clang -- and a call to a __ubsan_handle_*
9  * routine.  The names are compiler-generated and carry no meaning across a
10  * rebuild, so klp diff has to treat those sections as uncorrelated rather than
11  * pairing them up by name.
12  *
13  * untouched() is byte-identical in both builds and exists to catch the false
14  * positive: if the metadata were correlated by name, its shifts would look
15  * changed and it would be dragged into the patch.
16  *
17  * The shifts are what draw the instrumentation.  A bounds check would do as
18  * well but neither compiler emits one for an index it can prove in range.
19  */
20 
21 static const char __modinfo[]
22 	__attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";
23 
24 int shift_by(int v, int n);
25 
untouched(int v,int n)26 int untouched(int v, int n)
27 {
28 	int s = 0;
29 
30 	s += v << (n & 31);
31 	s += v << ((n + 1) & 31);
32 	s += shift_by(v, n);
33 
34 	return s;
35 }
36 
touched(int v,int n)37 int touched(int v, int n)
38 {
39 	int s = 0;
40 
41 	s += v << (n & 31);
42 #ifdef PATCHED
43 	s += v << ((n + 3) & 31);
44 #else
45 	s += v << ((n + 2) & 31);
46 #endif
47 
48 	return s;
49 }
50