File Index
Files and directories
Sign in
FILE

core_reloc_raw.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/bpf/prog_tests/core_reloc_raw.c C
  1// SPDX-License-Identifier: GPL-2.0
  2
  3/* Test cases that can't load programs using libbpf and need direct
  4 * BPF syscall access
  5 */
  6
  7#include <sys/syscall.h>
  8#include <bpf/libbpf.h>
  9#include <bpf/btf.h>
 10
 11#include "test_progs.h"
 12#include "test_btf.h"
 13#include "bpf/libbpf_internal.h"
 14
 15static char log[16 * 1024];
 16
 17/* Check that verifier rejects BPF program containing relocation
 18 * pointing to non-existent BTF type.
 19 */
 20static void test_bad_local_id(void)
 21{
 22	struct test_btf {
 23		struct btf_header hdr;
 24		__u32 types[15];
 25		char strings[128];
 26	} raw_btf = {
 27		.hdr = {
 28			.magic = BTF_MAGIC,
 29			.version = BTF_VERSION,
 30			.hdr_len = sizeof(struct btf_header),
 31			.type_off = 0,
 32			.type_len = sizeof(raw_btf.types),
 33			.str_off = offsetof(struct test_btf, strings) -
 34				   offsetof(struct test_btf, types),
 35			.str_len = sizeof(raw_btf.strings),
 36		},
 37		.types = {
 38			BTF_PTR_ENC(0),					/* [1] void*  */
 39			BTF_TYPE_INT_ENC(1, BTF_INT_SIGNED, 0, 32, 4),	/* [2] int    */
 40			BTF_FUNC_PROTO_ENC(2, 1),			/* [3] int (*)(void*) */
 41			BTF_FUNC_PROTO_ARG_ENC(8, 1),
 42			BTF_FUNC_ENC(8, 3)			/* [4] FUNC 'foo' type_id=2   */
 43		},
 44		.strings = "\0int\0 0\0foo\0"
 45	};
 46	__u32 log_level = 1 | 2 | 4;
 47	LIBBPF_OPTS(bpf_btf_load_opts, opts,
 48		    .log_buf = log,
 49		    .log_size = sizeof(log),
 50		    .log_level = log_level,
 51	);
 52	struct bpf_insn insns[] = {
 53		BPF_ALU64_IMM(BPF_MOV, BPF_REG_0, 0),
 54		BPF_EXIT_INSN(),
 55	};
 56	struct bpf_func_info funcs[] = {
 57		{
 58			.insn_off = 0,
 59			.type_id = 4,
 60		}
 61	};
 62	struct bpf_core_relo relos[] = {
 63		{
 64			.insn_off = 0,		/* patch first instruction (r0 = 0) */
 65			.type_id = 100500,	/* !!! this type id does not exist */
 66			.access_str_off = 6,	/* offset of "0" */
 67			.kind = BPF_CORE_TYPE_ID_LOCAL,
 68		}
 69	};
 70	union bpf_attr attr;
 71	int saved_errno;
 72	int prog_fd = -1;
 73	int btf_fd = -1;
 74
 75	btf_fd = bpf_btf_load(&raw_btf, sizeof(raw_btf), &opts);
 76	saved_errno = errno;
 77	if (btf_fd < 0 || env.verbosity > VERBOSE_NORMAL) {
 78		printf("-------- BTF load log start --------\n");
 79		printf("%s", log);
 80		printf("-------- BTF load log end ----------\n");
 81	}
 82	if (btf_fd < 0) {
 83		PRINT_FAIL("bpf_btf_load() failed, errno=%d\n", saved_errno);
 84		return;
 85	}
 86
 87	log[0] = 0;
 88	memset(&attr, 0, sizeof(attr));
 89	attr.prog_btf_fd = btf_fd;
 90	attr.prog_type = BPF_TRACE_RAW_TP;
 91	attr.license = (__u64)"GPL";
 92	attr.insns = (__u64)&insns;
 93	attr.insn_cnt = sizeof(insns) / sizeof(*insns);
 94	attr.log_buf = (__u64)log;
 95	attr.log_size = sizeof(log);
 96	attr.log_level = log_level;
 97	attr.func_info = (__u64)funcs;
 98	attr.func_info_cnt = sizeof(funcs) / sizeof(*funcs);
 99	attr.func_info_rec_size = sizeof(*funcs);
100	attr.core_relos = (__u64)relos;
101	attr.core_relo_cnt = sizeof(relos) / sizeof(*relos);
102	attr.core_relo_rec_size = sizeof(*relos);
103	prog_fd = sys_bpf_prog_load(&attr, sizeof(attr), 1);
104	saved_errno = errno;
105	if (prog_fd < 0 || env.verbosity > VERBOSE_NORMAL) {
106		printf("-------- program load log start --------\n");
107		printf("%s", log);
108		printf("-------- program load log end ----------\n");
109	}
110	if (prog_fd >= 0) {
111		PRINT_FAIL("sys_bpf_prog_load() expected to fail\n");
112		goto out;
113	}
114	ASSERT_HAS_SUBSTR(log, "relo #0: bad type id 100500", "program load log");
115
116out:
117	close(prog_fd);
118	close(btf_fd);
119}
120
121void test_core_reloc_raw(void)
122{
123	if (test__start_subtest("bad_local_id"))
124		test_bad_local_id();
125}
~/ Read-only file preview.