File: test_global_map_resize.c

package info (click to toggle)
linux 6.16.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,724,468 kB
  • sloc: ansic: 26,560,391; asm: 271,356; sh: 143,999; python: 72,469; makefile: 57,129; perl: 36,821; xml: 19,553; cpp: 5,820; yacc: 4,915; lex: 2,955; awk: 1,667; sed: 28; ruby: 25
file content (94 lines) | stat: -rw-r--r-- 2,301 bytes parent folder | download | duplicates (10)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */

#include "vmlinux.h"
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>

char _license[] SEC("license") = "GPL";

/* rodata section */
const volatile pid_t pid;
const volatile size_t bss_array_len;
const volatile size_t data_array_len;

/* bss section */
int sum = 0;
int array[1];

/* custom data section */
int my_array[1] SEC(".data.custom");

/* custom data section which should NOT be resizable,
 * since it contains a single var which is not an array
 */
int my_int SEC(".data.non_array");

/* custom data section which should NOT be resizable,
 * since its last var is not an array
 */
int my_array_first[1] SEC(".data.array_not_last");
int my_int_last SEC(".data.array_not_last");

int percpu_arr[1] SEC(".data.percpu_arr");

/* at least one extern is included, to ensure that a specific
 * regression is tested whereby resizing resulted in a free-after-use
 * bug after type information is invalidated by the resize operation.
 *
 * There isn't a particularly good API to test for this specific condition,
 * but by having externs for the resizing tests it will cover this path.
 */
extern int LINUX_KERNEL_VERSION __kconfig;
long version_sink;

SEC("tp/syscalls/sys_enter_getpid")
int bss_array_sum(void *ctx)
{
	if (pid != (bpf_get_current_pid_tgid() >> 32))
		return 0;

	/* this will be zero, we just rely on verifier not rejecting this */
	sum = percpu_arr[bpf_get_smp_processor_id()];

	for (size_t i = 0; i < bss_array_len; ++i)
		sum += array[i];

	/* see above; ensure this is not optimized out */
	version_sink = LINUX_KERNEL_VERSION;

	return 0;
}

SEC("tp/syscalls/sys_enter_getuid")
int data_array_sum(void *ctx)
{
	if (pid != (bpf_get_current_pid_tgid() >> 32))
		return 0;

	/* this will be zero, we just rely on verifier not rejecting this */
	sum = percpu_arr[bpf_get_smp_processor_id()];

	for (size_t i = 0; i < data_array_len; ++i)
		sum += my_array[i];

	/* see above; ensure this is not optimized out */
	version_sink = LINUX_KERNEL_VERSION;

	return 0;
}

SEC("struct_ops/test_1")
int BPF_PROG(test_1)
{
	return 0;
}

struct bpf_testmod_ops {
	int (*test_1)(void);
};

SEC(".struct_ops.link")
struct bpf_testmod_ops st_ops_resize = {
	.test_1 = (void *)test_1
};