Re: [PATCH bpf-next v2 08/11] selftests/bpf: add BTF_KIND_TAG unit tests

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





On 9/13/21 10:31 PM, Andrii Nakryiko wrote:
On Mon, Sep 13, 2021 at 8:52 AM Yonghong Song <yhs@xxxxxx> wrote:

Test good and bad variants of BTF_KIND_TAG encoding.

Signed-off-by: Yonghong Song <yhs@xxxxxx>
---
  tools/testing/selftests/bpf/prog_tests/btf.c | 223 +++++++++++++++++++
  tools/testing/selftests/bpf/test_btf.h       |   3 +
  2 files changed, 226 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index ad39f4d588d0..21b122f72a55 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -3661,6 +3661,227 @@ static struct btf_raw_test raw_tests[] = {
         .err_str = "Invalid type_size",
  },

+{
+       .descr = "tag test #1, struct/member, well-formed",
+       .raw_types = {
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+               BTF_STRUCT_ENC(0, 2, 8),                        /* [2] */
+               BTF_MEMBER_ENC(NAME_TBD, 1, 0),
+               BTF_MEMBER_ENC(NAME_TBD, 1, 32),
+               BTF_TAG_ENC(NAME_TBD, 2, -1),
+               BTF_TAG_ENC(NAME_TBD, 2, 0),
+               BTF_TAG_ENC(NAME_TBD, 2, 1),
+               BTF_END_RAW,
+       },
+       BTF_STR_SEC("\0m1\0m2\0tag1\0tag2\0tag3"),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "tag_type_check_btf",
+       .key_size = sizeof(int),
+       .value_size = 8,
+       .key_type_id = 1,
+       .value_type_id = 2,
+       .max_entries = 1,
+},
+{
+       .descr = "tag test #2, union/member, well-formed",
+       .raw_types = {
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+               BTF_STRUCT_ENC(NAME_TBD, 2, 4),                 /* [2] */

this is not a union. Other tests open-code union, but it's probably a
good idea to add BTF_UNION_ENC

This intended to be a union. So will fix.


+               BTF_MEMBER_ENC(NAME_TBD, 1, 0),
+               BTF_MEMBER_ENC(NAME_TBD, 1, 0),
+               BTF_TAG_ENC(NAME_TBD, 2, -1),
+               BTF_TAG_ENC(NAME_TBD, 2, 0),
+               BTF_TAG_ENC(NAME_TBD, 2, 1),
+               BTF_END_RAW,
+       },
+       BTF_STR_SEC("\0t\0m1\0m2\0tag1\0tag2\0tag3"),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "tag_type_check_btf",
+       .key_size = sizeof(int),
+       .value_size = 4,
+       .key_type_id = 1,
+       .value_type_id = 2,
+       .max_entries = 1,
+},

[...]

+{
+       .descr = "tag test #7, invalid vlen",
+       .raw_types = {
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+               BTF_VAR_ENC(NAME_TBD, 1, 0),                    /* [2] */
+               BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 1, 1), 2), (0),

here you have both kflag and vlen specified, so it's not clear which
one is rejected. Please keep kflag at 0 for this one.

Ack.


+               BTF_END_RAW,
+       },
+       BTF_STR_SEC("\0local\0tag1"),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "tag_type_check_btf",
+       .key_size = sizeof(int),
+       .value_size = 4,
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 1,
+       .btf_load_err = true,
+       .err_str = "vlen != 0",
+},
+{
+       .descr = "tag test #8, invalid kflag",
+       .raw_types = {
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+               BTF_VAR_ENC(NAME_TBD, 1, 0),                    /* [2] */
+               BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 1, 0), 2), (-1),
+               BTF_END_RAW,
+       },
+       BTF_STR_SEC("\0local\0tag1"),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "tag_type_check_btf",
+       .key_size = sizeof(int),
+       .value_size = 4,
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 1,
+       .btf_load_err = true,
+       .err_str = "Invalid btf_info kind_flag",
+},
+{
+       .descr = "tag test #9, var, invalid component_idx",
+       .raw_types = {
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+               BTF_VAR_ENC(NAME_TBD, 1, 0),                    /* [2] */
+               BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), 2), (0),

nit: could have used BTF_TAG_ENC?

Ack.


+               BTF_END_RAW,
+       },
+       BTF_STR_SEC("\0local\0tag"),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "tag_type_check_btf",
+       .key_size = sizeof(int),
+       .value_size = 4,
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 1,
+       .btf_load_err = true,
+       .err_str = "Invalid component_idx",
+},
+{
+       .descr = "tag test #10, struct member, invalid component_idx",
+       .raw_types = {
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+               BTF_STRUCT_ENC(0, 2, 8),                        /* [2] */
+               BTF_MEMBER_ENC(NAME_TBD, 1, 0),
+               BTF_MEMBER_ENC(NAME_TBD, 1, 32),
+               BTF_TAG_ENC(NAME_TBD, 2, 2),
+               BTF_END_RAW,
+       },
+       BTF_STR_SEC("\0m1\0m2\0tag"),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "tag_type_check_btf",
+       .key_size = sizeof(int),
+       .value_size = 8,
+       .key_type_id = 1,
+       .value_type_id = 2,
+       .max_entries = 1,
+       .btf_load_err = true,
+       .err_str = "Invalid component_idx",
+},
+{
+       .descr = "tag test #11, func parameter, invalid component_idx",
+       .raw_types = {
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),  /* [1] */
+               BTF_FUNC_PROTO_ENC(0, 2),                       /* [2] */
+                       BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
+                       BTF_FUNC_PROTO_ARG_ENC(NAME_TBD, 1),
+               BTF_FUNC_ENC(NAME_TBD, 2),                      /* [3] */
+               BTF_TAG_ENC(NAME_TBD, 3, 2),
+               BTF_END_RAW,
+       },
+       BTF_STR_SEC("\0arg1\0arg2\0f\0tag"),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "tag_type_check_btf",
+       .key_size = sizeof(int),
+       .value_size = 4,
+       .key_type_id = 1,
+       .value_type_id = 1,
+       .max_entries = 1,
+       .btf_load_err = true,
+       .err_str = "Invalid component_idx",
+},
+

please also add invalid negative component_idx test (e.g., -2)

Ack.


  }; /* struct btf_raw_test raw_tests[] */

  static const char *get_next_str(const char *start, const char *end)
@@ -6801,6 +7022,8 @@ static int btf_type_size(const struct btf_type *t)
                 return base_size + sizeof(struct btf_var);
         case BTF_KIND_DATASEC:
                 return base_size + vlen * sizeof(struct btf_var_secinfo);
+       case BTF_KIND_TAG:
+               return base_size + sizeof(struct btf_tag);
         default:
                 fprintf(stderr, "Unsupported BTF_KIND:%u\n", kind);
                 return -EINVAL;
diff --git a/tools/testing/selftests/bpf/test_btf.h b/tools/testing/selftests/bpf/test_btf.h
index e2394eea4b7f..0619e06d745e 100644
--- a/tools/testing/selftests/bpf/test_btf.h
+++ b/tools/testing/selftests/bpf/test_btf.h
@@ -69,4 +69,7 @@
  #define BTF_TYPE_FLOAT_ENC(name, sz) \
         BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_FLOAT, 0, 0), sz)

+#define BTF_TAG_ENC(value, type, component_idx)        \
+       BTF_TYPE_ENC(value, BTF_INFO_ENC(BTF_KIND_TAG, 0, 0), type), (component_idx)
+
  #endif /* _TEST_BTF_H */
--
2.30.2




[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux