tools/testing/selftests/bpf/prog_tests/btf_kind.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/btf_kind.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/btf_kind.c
Extension
.c
Size
6499 bytes
Lines
227
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0
/* Copyright (c) 2026, Oracle and/or its affiliates. */

#include <test_progs.h>
#include <bpf/btf.h>
#include <bpf/libbpf.h>

/* Verify kind encoding exists for each kind */
static void test_btf_kind_encoding(void)
{
	LIBBPF_OPTS(btf_new_opts, opts);
	const struct btf_header *hdr;
	const void *raw_btf;
	struct btf *btf;
	__u32 raw_size;

	opts.add_layout = true;
	btf = btf__new_empty_opts(&opts);
	if (!ASSERT_OK_PTR(btf, "btf_new"))
		return;

	raw_btf = btf__raw_data(btf, &raw_size);
	if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
		return;

	hdr = raw_btf;

	ASSERT_EQ(hdr->layout_off % 4, 0, "layout_aligned");
	ASSERT_EQ(hdr->layout_len, sizeof(struct btf_layout) * NR_BTF_KINDS,
		  "layout_len");
	ASSERT_EQ(hdr->str_off, hdr->layout_off + hdr->layout_len, "str_after_layout");
	btf__free(btf);

	opts.add_layout = false;
	btf = btf__new_empty_opts(&opts);
	if (!ASSERT_OK_PTR(btf, "btf_new"))
		return;

	raw_btf = btf__raw_data(btf, &raw_size);
	if (!ASSERT_OK_PTR(raw_btf, "btf__raw_data"))
		return;

	hdr = raw_btf;

	ASSERT_EQ(hdr->layout_off, 0, "no_layout_off");
	ASSERT_EQ(hdr->layout_len, 0, "no_layout_len");
	ASSERT_EQ(hdr->str_off, hdr->type_off + hdr->type_len, "strs_after_types");
	btf__free(btf);
}

static int write_raw_btf(void *raw_btf, size_t raw_size, char *file)
{
	int fd = mkstemp(file);
	ssize_t n;

	if (!ASSERT_OK_FD(fd, "open_raw_btf"))
		return -1;
	n = write(fd, raw_btf, raw_size);
	close(fd);
	if (!ASSERT_EQ(n, (ssize_t)raw_size, "write_raw_btf"))
		return -1;
	return 0;
}

/*
 * Fabricate an unrecognized kind at BTF_KIND_MAX + 1, and after adding
 * the appropriate struct/typedefs to the BTF such that it recognizes
 * this kind, ensure that parsing of BTF containing the unrecognized kind
 * can succeed.
 */
void test_btf_kind_decoding(void)
{
	char btf_kind_file1[] = "/tmp/test_btf_kind.XXXXXX";
	char btf_kind_file2[] = "/tmp/test_btf_kind.XXXXXX";
	char btf_kind_file3[] = "/tmp/test_btf_kind.XXXXXX";
	struct btf *btf = NULL, *new_btf = NULL;
	__s32 int_id, unrec_id, id, id2;
	LIBBPF_OPTS(btf_new_opts, opts);
	struct btf_layout *l;
	struct btf_header *hdr;
	const void *raw_btf;
	struct btf_type *t;
	void *new_raw_btf;
	void *str_data;
	__u32 raw_size;

	opts.add_layout = true;
	btf = btf__new_empty_opts(&opts);
	if (!ASSERT_OK_PTR(btf, "btf_new"))
		return;

Annotation

Implementation Notes