summaryrefslogtreecommitdiff
path: root/tools/objtool/tests/x86/fixtures/kcfi.c
blob: b62fc60634cf22efe6efca681f54cce93063e198 (plain) (blame)
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
// SPDX-License-Identifier: GPL-2.0
/*
 * An indirect call, which under kCFI is preceded by a type check and a trap.
 *
 * Clang emits a __cfi_<func> prefix symbol carrying the type hash ahead of
 * every address-taken function, and records the trap site in .kcfi_traps.
 * Both belong to the function and both have to come with it into a patch.
 *
 * Needs -fsanitize=kcfi, which only Clang has.
 */

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

static int impl_a(int x)
{
	return x + 1;
}

static int impl_b(int x)
{
	return x * 2;
}

__attribute__((noinline)) int (*pick(int x))(int)
{
	return (x & 1) ? impl_a : impl_b;
}

int target(int x)
{
	int (*fn)(int arg) = pick(x);

#ifdef PATCHED
	return fn(x) + 2;
#else
	return fn(x) + 1;
#endif
}