Netdev Archive on lore.kernel.org
help / color / mirror / Atom feed
* [PATCH bpf-next] selftests/bpf: add multidimensional array BTF-based data dump test
@ 2021-07-22 22:05 Alan Maguire
  2021-07-23  0:06 ` Andrii Nakryiko
  0 siblings, 1 reply; 2+ messages in thread
From: Alan Maguire @ 2021-07-22 22:05 UTC (permalink / raw)
  To: ast, daniel, andrii
  Cc: kafai, songliubraving, yhs, john.fastabend, kpsingh, morbo, bpf,
	netdev, Alan Maguire

There are currently no multidimensional array tests for
BTF-based data dumping.  In BTF, a multidimensional array is
represented as an array with an element type of array.  However,
pahole and llvm collapse multidimensional arrays into a
one-dimensional array [1].  Accordingly, the test uses the BTF
add interfaces to create a multidimensional char [2][4] array,
and tests type-based display matches expectations.

[1] See Documentation/bpf/btf.rst Section 2.2.3

Suggested-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
---
 tools/testing/selftests/bpf/prog_tests/btf_dump.c | 39 +++++++++++++++++++++++
 1 file changed, 39 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
index 52ccf0c..70d26cf 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
@@ -681,6 +681,45 @@ static void test_btf_dump_struct_data(struct btf *btf, struct btf_dump *d,
 "}",
 			   { .cb = { 0, 0, 1, 0, 0},});
 
+	/* multidimensional array; because pahole and llvm collapse arrays
+	 * with multiple dimensions into a single dimension, we add a
+	 * multidimensional array explicitly.
+	 */
+	type_id = btf__find_by_name(btf, "char");
+	if (ASSERT_GT(type_id, 0, "find char")) {
+		__s32 index_id, array1_id, array2_id;
+		char strs[2][4] = { "one", "two" };
+
+		index_id = btf__find_by_name(btf, "int");
+		ASSERT_GT(index_id, 0, "find int");
+
+		array2_id = btf__add_array(btf, index_id, type_id, 4);
+		ASSERT_GT(array2_id, 0, "add multidim 2");
+
+		array1_id = btf__add_array(btf, index_id, array2_id, 2);
+		ASSERT_GT(array1_id, 0, "add multidim 1");
+
+		str[0] = '\0';
+		ret = btf_dump__dump_type_data(d, array1_id, &strs, sizeof(strs), &opts);
+		ASSERT_GT(ret, 0, "dump multidimensional array");
+
+		ASSERT_STREQ(str,
+"(char[2][4])[\n"
+"	[\n"
+"		'o',\n"
+"		'n',\n"
+"		'e',\n"
+"	],\n"
+"	[\n"
+"		't',\n"
+"		'w',\n"
+"		'o',\n"
+"	],\n"
+"]",
+			     "multidimensional char array matches");
+	}
+
+
 	/* struct with bitfields */
 	TEST_BTF_DUMP_DATA_C(btf, d, "struct", str, struct bpf_insn, BTF_F_COMPACT,
 		{.code = (__u8)1,.dst_reg = (__u8)0x2,.src_reg = (__u8)0x3,.off = (__s16)4,.imm = (__s32)5,});
-- 
1.8.3.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH bpf-next] selftests/bpf: add multidimensional array BTF-based data dump test
  2021-07-22 22:05 [PATCH bpf-next] selftests/bpf: add multidimensional array BTF-based data dump test Alan Maguire
@ 2021-07-23  0:06 ` Andrii Nakryiko
  0 siblings, 0 replies; 2+ messages in thread
From: Andrii Nakryiko @ 2021-07-23  0:06 UTC (permalink / raw)
  To: Alan Maguire
  Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin Lau,
	Song Liu, Yonghong Song, john fastabend, KP Singh, Bill Wendling,
	bpf, Networking

On Thu, Jul 22, 2021 at 3:06 PM Alan Maguire <alan.maguire@oracle.com> wrote:
>
> There are currently no multidimensional array tests for
> BTF-based data dumping.  In BTF, a multidimensional array is
> represented as an array with an element type of array.  However,
> pahole and llvm collapse multidimensional arrays into a
> one-dimensional array [1].  Accordingly, the test uses the BTF
> add interfaces to create a multidimensional char [2][4] array,
> and tests type-based display matches expectations.
>
> [1] See Documentation/bpf/btf.rst Section 2.2.3
>
> Suggested-by: Andrii Nakryiko <andrii@kernel.org>
> Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
> ---
>  tools/testing/selftests/bpf/prog_tests/btf_dump.c | 39 +++++++++++++++++++++++
>  1 file changed, 39 insertions(+)
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/btf_dump.c b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
> index 52ccf0c..70d26cf 100644
> --- a/tools/testing/selftests/bpf/prog_tests/btf_dump.c
> +++ b/tools/testing/selftests/bpf/prog_tests/btf_dump.c
> @@ -681,6 +681,45 @@ static void test_btf_dump_struct_data(struct btf *btf, struct btf_dump *d,
>  "}",
>                            { .cb = { 0, 0, 1, 0, 0},});
>
> +       /* multidimensional array; because pahole and llvm collapse arrays

Are you sure about llvm collapsing multi-dimensional arrays?... There
is entire progs/btf_dump_test_case_multidim.c testing
multi-dimensional array ;)

> +        * with multiple dimensions into a single dimension, we add a
> +        * multidimensional array explicitly.
> +        */
> +       type_id = btf__find_by_name(btf, "char");
> +       if (ASSERT_GT(type_id, 0, "find char")) {
> +               __s32 index_id, array1_id, array2_id;
> +               char strs[2][4] = { "one", "two" };

oh, it's a way too happy case :) can you try { "a", "", "abc", "ab" } as well?

> +
> +               index_id = btf__find_by_name(btf, "int");
> +               ASSERT_GT(index_id, 0, "find int");
> +
> +               array2_id = btf__add_array(btf, index_id, type_id, 4);
> +               ASSERT_GT(array2_id, 0, "add multidim 2");
> +
> +               array1_id = btf__add_array(btf, index_id, array2_id, 2);
> +               ASSERT_GT(array1_id, 0, "add multidim 1");
> +
> +               str[0] = '\0';
> +               ret = btf_dump__dump_type_data(d, array1_id, &strs, sizeof(strs), &opts);
> +               ASSERT_GT(ret, 0, "dump multidimensional array");
> +
> +               ASSERT_STREQ(str,
> +"(char[2][4])[\n"
> +"      [\n"
> +"              'o',\n"
> +"              'n',\n"
> +"              'e',\n"
> +"      ],\n"
> +"      [\n"
> +"              't',\n"
> +"              'w',\n"
> +"              'o',\n"
> +"      ],\n"
> +"]",

maybe use compact output for such tests, it won't lose much in terms
of readability and will be way more compact


> +                            "multidimensional char array matches");
> +       }
> +
> +
>         /* struct with bitfields */
>         TEST_BTF_DUMP_DATA_C(btf, d, "struct", str, struct bpf_insn, BTF_F_COMPACT,
>                 {.code = (__u8)1,.dst_reg = (__u8)0x2,.src_reg = (__u8)0x3,.off = (__s16)4,.imm = (__s32)5,});
> --
> 1.8.3.1
>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-07-23  0:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-22 22:05 [PATCH bpf-next] selftests/bpf: add multidimensional array BTF-based data dump test Alan Maguire
2021-07-23  0:06 ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).