diff mbox series

[bpf-next] selftests/bpf: add multidimensional array BTF-based data dump test

Message ID 1626991540-21097-1-git-send-email-alan.maguire@oracle.com
State New
Headers show
Series [bpf-next] selftests/bpf: add multidimensional array BTF-based data dump test | expand

Commit Message

Alan Maguire July 22, 2021, 10:05 p.m. UTC
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(+)

Comments

Andrii Nakryiko July 23, 2021, 12:06 a.m. UTC | #1
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
>
diff mbox series

Patch

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,});