xref: /linux/tools/objtool/tests/x86/fixtures/kcfi.c (revision 5b6dca02b3e2254d2fd5706d76d7ab4610a67296)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * An indirect call, which under kCFI is preceded by a type check and a trap.
4  *
5  * Clang emits a __cfi_<func> prefix symbol carrying the type hash ahead of
6  * every address-taken function, and records the trap site in .kcfi_traps.
7  * Both belong to the function and both have to come with it into a patch.
8  *
9  * Needs -fsanitize=kcfi, which only Clang has.
10  */
11 
12 static const char __modinfo[]
13 	__attribute__((section(".modinfo"), used, aligned(1))) = "\0name=vmlinux";
14 
impl_a(int x)15 static int impl_a(int x)
16 {
17 	return x + 1;
18 }
19 
impl_b(int x)20 static int impl_b(int x)
21 {
22 	return x * 2;
23 }
24 
pick(int x)25 __attribute__((noinline)) int (*pick(int x))(int)
26 {
27 	return (x & 1) ? impl_a : impl_b;
28 }
29 
target(int x)30 int target(int x)
31 {
32 	int (*fn)(int arg) = pick(x);
33 
34 #ifdef PATCHED
35 	return fn(x) + 2;
36 #else
37 	return fn(x) + 1;
38 #endif
39 }
40