aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/bpf/progs/test_core_reloc_module.c
blob: f59f175c7bafb6b2456b12a2c6b4e967149755a5 (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
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
95
96
97
98
99
100
101
102
103
104
// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2020 Facebook */

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

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

struct bpf_testmod_test_read_ctx {
	/* field order is mixed up */
	size_t len;
	char *buf;
	loff_t off;
} __attribute__((preserve_access_index));

struct {
	char in[256];
	char out[256];
	bool skip;
	uint64_t my_pid_tgid;
} data = {};

struct core_reloc_module_output {
	long long len;
	long long off;
	int read_ctx_sz;
	bool read_ctx_exists;
	bool buf_exists;
	bool len_exists;
	bool off_exists;
	/* we have test_progs[-flavor], so cut flavor part */
	char comm[sizeof("test_progs")];
	int comm_len;
};

SEC("raw_tp/bpf_testmod_test_read")
int BPF_PROG(test_core_module_probed,
	     struct task_struct *task,
	     struct bpf_testmod_test_read_ctx *read_ctx)
{
#if __has_builtin(__builtin_preserve_enum_value)
	struct core_reloc_module_output *out = (void *)&data.out;
	__u64 pid_tgid = bpf_get_current_pid_tgid();
	__u32 real_tgid = (__u32)(pid_tgid >> 32);
	__u32 real_pid = (__u32)pid_tgid;

	if (data.my_pid_tgid != pid_tgid)
		return 0;

	if (BPF_CORE_READ(task, pid) != real_pid || BPF_CORE_READ(task, tgid) != real_tgid)
		return 0;

	out->len = BPF_CORE_READ(read_ctx, len);
	out->off = BPF_CORE_READ(read_ctx, off);

	out->read_ctx_sz = bpf_core_type_size(struct bpf_testmod_test_read_ctx);
	out->read_ctx_exists = bpf_core_type_exists(struct bpf_testmod_test_read_ctx);
	out->buf_exists = bpf_core_field_exists(read_ctx->buf);
	out->off_exists = bpf_core_field_exists(read_ctx->off);
	out->len_exists = bpf_core_field_exists(read_ctx->len);

	out->comm_len = BPF_CORE_READ_STR_INTO(&out->comm, task, comm);
#else
	data.skip = true;
#endif

	return 0;
}

SEC("tp_btf/bpf_testmod_test_read")
int BPF_PROG(test_core_module_direct,
	     struct task_struct *task,
	     struct bpf_testmod_test_read_ctx *read_ctx)
{
#if __has_builtin(__builtin_preserve_enum_value)
	struct core_reloc_module_output *out = (void *)&data.out;
	__u64 pid_tgid = bpf_get_current_pid_tgid();
	__u32 real_tgid = (__u32)(pid_tgid >> 32);
	__u32 real_pid = (__u32)pid_tgid;

	if (data.my_pid_tgid != pid_tgid)
		return 0;

	if (task->pid != real_pid || task->tgid != real_tgid)
		return 0;

	out->len = read_ctx->len;
	out->off = read_ctx->off;

	out->read_ctx_sz = bpf_core_type_size(struct bpf_testmod_test_read_ctx);
	out->read_ctx_exists = bpf_core_type_exists(struct bpf_testmod_test_read_ctx);
	out->buf_exists = bpf_core_field_exists(read_ctx->buf);
	out->off_exists = bpf_core_field_exists(read_ctx->off);
	out->len_exists = bpf_core_field_exists(read_ctx->len);

	out->comm_len = BPF_CORE_READ_STR_INTO(&out->comm, task, comm);
#else
	data.skip = true;
#endif

	return 0;
}