Commit 0d26450b authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by sanglipeng
Browse files

selftests/bpf: Add few corner cases to test padding handling of btf_dump

stable inclusion
from stable-v5.10.177
commit 7ad30ad8c684362364e629574ab02eec3b9bb1bc
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I88YNP

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/?id=7ad30ad8c684362364e629574ab02eec3b9bb1bc

--------------------------------

[ Upstream commit b148c8b9 ]

Add few hand-crafted cases and few randomized cases found using script
from [0] that tests btf_dump's padding logic.

  [0] https://lore.kernel.org/bpf/85f83c333f5355c8ac026f835b18d15060725fcb.camel@ericsson.com/



Signed-off-by: default avatarAndrii Nakryiko <andrii@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Link: https://lore.kernel.org/bpf/20221212211505.558851-7-andrii@kernel.org


Stable-dep-of: 4fb877aa ("libbpf: Fix btf_dump's packed struct determination")
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
Signed-off-by: default avatarsanglipeng <sanglipeng1@jd.com>
parent aad766a1
Loading
Loading
Loading
Loading
+60 −1
Original line number Diff line number Diff line
@@ -58,8 +58,65 @@ union jump_code_union {
	} __attribute__((packed));
};

/* ----- START-EXPECTED-OUTPUT ----- */
/*
 *struct nested_packed_but_aligned_struct {
 *	int x1;
 *	int x2;
 *};
 *
 *struct outer_implicitly_packed_struct {
 *	char y1;
 *	struct nested_packed_but_aligned_struct y2;
 *} __attribute__((packed));
 *
 */
/* ------ END-EXPECTED-OUTPUT ------ */

struct nested_packed_but_aligned_struct {
	int x1;
	int x2;
} __attribute__((packed));

struct outer_implicitly_packed_struct {
	char y1;
	struct nested_packed_but_aligned_struct y2;
};
/* ----- START-EXPECTED-OUTPUT ----- */
/*
 *struct usb_ss_ep_comp_descriptor {
 *	char: 8;
 *	char bDescriptorType;
 *	char bMaxBurst;
 *	short wBytesPerInterval;
 *};
 *
 *struct usb_host_endpoint {
 *	long: 64;
 *	char: 8;
 *	struct usb_ss_ep_comp_descriptor ss_ep_comp;
 *	long: 0;
 *} __attribute__((packed));
 *
 */
/* ------ END-EXPECTED-OUTPUT ------ */

struct usb_ss_ep_comp_descriptor {
	char: 8;
	char bDescriptorType;
	char bMaxBurst;
	int: 0;
	short wBytesPerInterval;
} __attribute__((packed));

struct usb_host_endpoint {
	long: 64;
	char: 8;
	struct usb_ss_ep_comp_descriptor ss_ep_comp;
	long: 0;
};


int f(struct {
	struct packed_trailing_space _1;
	struct non_packed_trailing_space _2;
@@ -69,6 +126,8 @@ int f(struct {
	union union_is_never_packed _6;
	union union_does_not_need_packing _7;
	union jump_code_union _8;
	struct outer_implicitly_packed_struct _9;
	struct usb_host_endpoint _10;
} *_)
{
	return 0;
+104 −0
Original line number Diff line number Diff line
@@ -128,6 +128,98 @@ struct padding_weird_2 {
	char: 8;
};

/* ----- START-EXPECTED-OUTPUT ----- */
struct exact_1byte {
	char x;
};

struct padded_1byte {
	char: 8;
};

struct exact_2bytes {
	short x;
};

struct padded_2bytes {
	short: 16;
};

struct exact_4bytes {
	int x;
};

struct padded_4bytes {
	int: 32;
};

struct exact_8bytes {
	long x;
};

struct padded_8bytes {
	long: 64;
};

struct ff_periodic_effect {
	int: 32;
	short magnitude;
	long: 0;
	short phase;
	long: 0;
	int: 32;
	int custom_len;
	short *custom_data;
};

struct ib_wc {
	long: 64;
	long: 64;
	int: 32;
	int byte_len;
	void *qp;
	union {} ex;
	long: 64;
	int slid;
	int wc_flags;
	long: 64;
	char smac[6];
	long: 0;
	char network_hdr_type;
};

struct acpi_object_method {
	long: 64;
	char: 8;
	char type;
	short reference_count;
	char flags;
	short: 0;
	char: 8;
	char sync_level;
	long: 64;
	void *node;
	void *aml_start;
	union {} dispatch;
	long: 64;
	int aml_length;
};

struct nested_unpacked {
	int x;
};

struct nested_packed {
	struct nested_unpacked a;
	char c;
} __attribute__((packed));

struct outer_mixed_but_unpacked {
	struct nested_packed b1;
	short a1;
	struct nested_packed b2;
};

/* ------ END-EXPECTED-OUTPUT ------ */

int f(struct {
@@ -139,6 +231,18 @@ int f(struct {
	struct padding_wo_named_members _6;
	struct padding_weird_1 _7;
	struct padding_weird_2 _8;
	struct exact_1byte _100;
	struct padded_1byte _101;
	struct exact_2bytes _102;
	struct padded_2bytes _103;
	struct exact_4bytes _104;
	struct padded_4bytes _105;
	struct exact_8bytes _106;
	struct padded_8bytes _107;
	struct ff_periodic_effect _200;
	struct ib_wc _201;
	struct acpi_object_method _202;
	struct outer_mixed_but_unpacked _203;
} *_)
{
	return 0;