selftests/bpf: Add test for the packed enum member in struct/union
authorYoshiki Komachi <komachi.yoshiki@gmail.com>
Tue, 10 Mar 2020 07:32:30 +0000 (16:32 +0900)
committerAlexei Starovoitov <ast@kernel.org>
Tue, 10 Mar 2020 17:00:41 +0000 (10:00 -0700)
Add a simple test to the existing selftest program in order to make
sure that a packed enum member in struct unexceeds the struct_size.

Signed-off-by: Yoshiki Komachi <komachi.yoshiki@gmail.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/1583825550-18606-3-git-send-email-komachi.yoshiki@gmail.com
tools/testing/selftests/bpf/test_btf.c

index 93040ca..8da77cd 100644 (file)
@@ -1062,6 +1062,48 @@ static struct btf_raw_test raw_tests[] = {
        .err_str = "Member exceeds struct_size",
 },
 
+/* Test member unexceeds the size of struct
+ *
+ * enum E {
+ *     E0,
+ *     E1,
+ * };
+ *
+ * struct A {
+ *     char m;
+ *     enum E __attribute__((packed)) n;
+ * };
+ */
+{
+       .descr = "size check test #5",
+       .raw_types = {
+               /* int */                       /* [1] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
+               /* char */                      /* [2] */
+               BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1),
+               /* enum E { */                  /* [3] */
+               BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), 1),
+               BTF_ENUM_ENC(NAME_TBD, 0),
+               BTF_ENUM_ENC(NAME_TBD, 1),
+               /* } */
+               /* struct A { */                /* [4] */
+               BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 2),
+               BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* char m; */
+               BTF_MEMBER_ENC(NAME_TBD, 3, 8),/* enum E __attribute__((packed)) n; */
+               /* } */
+               BTF_END_RAW,
+       },
+       .str_sec = "\0E\0E0\0E1\0A\0m\0n",
+       .str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
+       .map_type = BPF_MAP_TYPE_ARRAY,
+       .map_name = "size_check5_map",
+       .key_size = sizeof(int),
+       .value_size = 2,
+       .key_type_id = 1,
+       .value_type_id = 4,
+       .max_entries = 4,
+},
+
 /* typedef const void * const_void_ptr;
  * struct A {
  *     const_void_ptr m;