linux/tools/testing/selftests/bpf/prog_tests/btf_write.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/* Copyright (c) 2020 Facebook */
   3#include <test_progs.h>
   4#include <bpf/btf.h>
   5
   6#define ASSERT_EQ(actual, expected, name) ({                            \
   7        typeof(actual) ___act = (actual);                               \
   8        typeof(expected) ___exp = (expected);                           \
   9        bool ___ok = ___act == ___exp;                                  \
  10        CHECK(!___ok, (name),                                           \
  11              "unexpected %s: actual %lld != expected %lld\n",          \
  12              (name), (long long)(___act), (long long)(___exp));        \
  13        ___ok;                                                          \
  14})
  15
  16#define ASSERT_STREQ(actual, expected, name) ({                         \
  17        const char *___act = actual;                                    \
  18        const char *___exp = expected;                                  \
  19        bool ___ok = strcmp(___act, ___exp) == 0;                       \
  20        CHECK(!___ok, (name),                                           \
  21              "unexpected %s: actual '%s' != expected '%s'\n",          \
  22              (name), ___act, ___exp);                                  \
  23        ___ok;                                                          \
  24})
  25
  26#define ASSERT_OK(res, name) ({                                         \
  27        long long ___res = (res);                                       \
  28        bool ___ok = ___res == 0;                                       \
  29        CHECK(!___ok, (name), "unexpected error: %lld\n", ___res);      \
  30        ___ok;                                                          \
  31})
  32
  33#define ASSERT_ERR(res, name) ({                                        \
  34        long long ___res = (res);                                       \
  35        bool ___ok = ___res < 0;                                        \
  36        CHECK(!___ok, (name), "unexpected success: %lld\n", ___res);    \
  37        ___ok;                                                          \
  38})
  39
  40static int duration = 0;
  41
  42void test_btf_write() {
  43        const struct btf_var_secinfo *vi;
  44        const struct btf_type *t;
  45        const struct btf_member *m;
  46        const struct btf_enum *v;
  47        const struct btf_param *p;
  48        struct btf *btf;
  49        int id, err, str_off;
  50
  51        btf = btf__new_empty();
  52        if (CHECK(IS_ERR(btf), "new_empty", "failed: %ld\n", PTR_ERR(btf)))
  53                return;
  54
  55        str_off = btf__find_str(btf, "int");
  56        ASSERT_EQ(str_off, -ENOENT, "int_str_missing_off");
  57
  58        str_off = btf__add_str(btf, "int");
  59        ASSERT_EQ(str_off, 1, "int_str_off");
  60
  61        str_off = btf__find_str(btf, "int");
  62        ASSERT_EQ(str_off, 1, "int_str_found_off");
  63
  64        /* BTF_KIND_INT */
  65        id = btf__add_int(btf, "int", 4,  BTF_INT_SIGNED);
  66        ASSERT_EQ(id, 1, "int_id");
  67
  68        t = btf__type_by_id(btf, 1);
  69        /* should re-use previously added "int" string */
  70        ASSERT_EQ(t->name_off, str_off, "int_name_off");
  71        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "int", "int_name");
  72        ASSERT_EQ(btf_kind(t), BTF_KIND_INT, "int_kind");
  73        ASSERT_EQ(t->size, 4, "int_sz");
  74        ASSERT_EQ(btf_int_encoding(t), BTF_INT_SIGNED, "int_enc");
  75        ASSERT_EQ(btf_int_bits(t), 32, "int_bits");
  76
  77        /* invalid int size */
  78        id = btf__add_int(btf, "bad sz int", 7, 0);
  79        ASSERT_ERR(id, "int_bad_sz");
  80        /* invalid encoding */
  81        id = btf__add_int(btf, "bad enc int", 4, 123);
  82        ASSERT_ERR(id, "int_bad_enc");
  83        /* NULL name */
  84        id = btf__add_int(btf, NULL, 4, 0);
  85        ASSERT_ERR(id, "int_bad_null_name");
  86        /* empty name */
  87        id = btf__add_int(btf, "", 4, 0);
  88        ASSERT_ERR(id, "int_bad_empty_name");
  89
  90        /* PTR/CONST/VOLATILE/RESTRICT */
  91        id = btf__add_ptr(btf, 1);
  92        ASSERT_EQ(id, 2, "ptr_id");
  93        t = btf__type_by_id(btf, 2);
  94        ASSERT_EQ(btf_kind(t), BTF_KIND_PTR, "ptr_kind");
  95        ASSERT_EQ(t->type, 1, "ptr_type");
  96
  97        id = btf__add_const(btf, 5); /* points forward to restrict */
  98        ASSERT_EQ(id, 3, "const_id");
  99        t = btf__type_by_id(btf, 3);
 100        ASSERT_EQ(btf_kind(t), BTF_KIND_CONST, "const_kind");
 101        ASSERT_EQ(t->type, 5, "const_type");
 102
 103        id = btf__add_volatile(btf, 3);
 104        ASSERT_EQ(id, 4, "volatile_id");
 105        t = btf__type_by_id(btf, 4);
 106        ASSERT_EQ(btf_kind(t), BTF_KIND_VOLATILE, "volatile_kind");
 107        ASSERT_EQ(t->type, 3, "volatile_type");
 108
 109        id = btf__add_restrict(btf, 4);
 110        ASSERT_EQ(id, 5, "restrict_id");
 111        t = btf__type_by_id(btf, 5);
 112        ASSERT_EQ(btf_kind(t), BTF_KIND_RESTRICT, "restrict_kind");
 113        ASSERT_EQ(t->type, 4, "restrict_type");
 114
 115        /* ARRAY */
 116        id = btf__add_array(btf, 1, 2, 10); /* int *[10] */
 117        ASSERT_EQ(id, 6, "array_id");
 118        t = btf__type_by_id(btf, 6);
 119        ASSERT_EQ(btf_kind(t), BTF_KIND_ARRAY, "array_kind");
 120        ASSERT_EQ(btf_array(t)->index_type, 1, "array_index_type");
 121        ASSERT_EQ(btf_array(t)->type, 2, "array_elem_type");
 122        ASSERT_EQ(btf_array(t)->nelems, 10, "array_nelems");
 123
 124        /* STRUCT */
 125        err = btf__add_field(btf, "field", 1, 0, 0);
 126        ASSERT_ERR(err, "no_struct_field");
 127        id = btf__add_struct(btf, "s1", 8);
 128        ASSERT_EQ(id, 7, "struct_id");
 129        err = btf__add_field(btf, "f1", 1, 0, 0);
 130        ASSERT_OK(err, "f1_res");
 131        err = btf__add_field(btf, "f2", 1, 32, 16);
 132        ASSERT_OK(err, "f2_res");
 133
 134        t = btf__type_by_id(btf, 7);
 135        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "s1", "struct_name");
 136        ASSERT_EQ(btf_kind(t), BTF_KIND_STRUCT, "struct_kind");
 137        ASSERT_EQ(btf_vlen(t), 2, "struct_vlen");
 138        ASSERT_EQ(btf_kflag(t), true, "struct_kflag");
 139        ASSERT_EQ(t->size, 8, "struct_sz");
 140        m = btf_members(t) + 0;
 141        ASSERT_STREQ(btf__str_by_offset(btf, m->name_off), "f1", "f1_name");
 142        ASSERT_EQ(m->type, 1, "f1_type");
 143        ASSERT_EQ(btf_member_bit_offset(t, 0), 0, "f1_bit_off");
 144        ASSERT_EQ(btf_member_bitfield_size(t, 0), 0, "f1_bit_sz");
 145        m = btf_members(t) + 1;
 146        ASSERT_STREQ(btf__str_by_offset(btf, m->name_off), "f2", "f2_name");
 147        ASSERT_EQ(m->type, 1, "f2_type");
 148        ASSERT_EQ(btf_member_bit_offset(t, 1), 32, "f2_bit_off");
 149        ASSERT_EQ(btf_member_bitfield_size(t, 1), 16, "f2_bit_sz");
 150
 151        /* UNION */
 152        id = btf__add_union(btf, "u1", 8);
 153        ASSERT_EQ(id, 8, "union_id");
 154
 155        /* invalid, non-zero offset */
 156        err = btf__add_field(btf, "field", 1, 1, 0);
 157        ASSERT_ERR(err, "no_struct_field");
 158
 159        err = btf__add_field(btf, "f1", 1, 0, 16);
 160        ASSERT_OK(err, "f1_res");
 161
 162        t = btf__type_by_id(btf, 8);
 163        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "u1", "union_name");
 164        ASSERT_EQ(btf_kind(t), BTF_KIND_UNION, "union_kind");
 165        ASSERT_EQ(btf_vlen(t), 1, "union_vlen");
 166        ASSERT_EQ(btf_kflag(t), true, "union_kflag");
 167        ASSERT_EQ(t->size, 8, "union_sz");
 168        m = btf_members(t) + 0;
 169        ASSERT_STREQ(btf__str_by_offset(btf, m->name_off), "f1", "f1_name");
 170        ASSERT_EQ(m->type, 1, "f1_type");
 171        ASSERT_EQ(btf_member_bit_offset(t, 0), 0, "f1_bit_off");
 172        ASSERT_EQ(btf_member_bitfield_size(t, 0), 16, "f1_bit_sz");
 173
 174        /* ENUM */
 175        id = btf__add_enum(btf, "e1", 4);
 176        ASSERT_EQ(id, 9, "enum_id");
 177        err = btf__add_enum_value(btf, "v1", 1);
 178        ASSERT_OK(err, "v1_res");
 179        err = btf__add_enum_value(btf, "v2", 2);
 180        ASSERT_OK(err, "v2_res");
 181
 182        t = btf__type_by_id(btf, 9);
 183        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "e1", "enum_name");
 184        ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM, "enum_kind");
 185        ASSERT_EQ(btf_vlen(t), 2, "enum_vlen");
 186        ASSERT_EQ(t->size, 4, "enum_sz");
 187        v = btf_enum(t) + 0;
 188        ASSERT_STREQ(btf__str_by_offset(btf, v->name_off), "v1", "v1_name");
 189        ASSERT_EQ(v->val, 1, "v1_val");
 190        v = btf_enum(t) + 1;
 191        ASSERT_STREQ(btf__str_by_offset(btf, v->name_off), "v2", "v2_name");
 192        ASSERT_EQ(v->val, 2, "v2_val");
 193
 194        /* FWDs */
 195        id = btf__add_fwd(btf, "struct_fwd", BTF_FWD_STRUCT);
 196        ASSERT_EQ(id, 10, "struct_fwd_id");
 197        t = btf__type_by_id(btf, 10);
 198        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "struct_fwd", "fwd_name");
 199        ASSERT_EQ(btf_kind(t), BTF_KIND_FWD, "fwd_kind");
 200        ASSERT_EQ(btf_kflag(t), 0, "fwd_kflag");
 201
 202        id = btf__add_fwd(btf, "union_fwd", BTF_FWD_UNION);
 203        ASSERT_EQ(id, 11, "union_fwd_id");
 204        t = btf__type_by_id(btf, 11);
 205        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "union_fwd", "fwd_name");
 206        ASSERT_EQ(btf_kind(t), BTF_KIND_FWD, "fwd_kind");
 207        ASSERT_EQ(btf_kflag(t), 1, "fwd_kflag");
 208
 209        id = btf__add_fwd(btf, "enum_fwd", BTF_FWD_ENUM);
 210        ASSERT_EQ(id, 12, "enum_fwd_id");
 211        t = btf__type_by_id(btf, 12);
 212        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "enum_fwd", "fwd_name");
 213        ASSERT_EQ(btf_kind(t), BTF_KIND_ENUM, "enum_fwd_kind");
 214        ASSERT_EQ(btf_vlen(t), 0, "enum_fwd_kind");
 215        ASSERT_EQ(t->size, 4, "enum_fwd_sz");
 216
 217        /* TYPEDEF */
 218        id = btf__add_typedef(btf, "typedef1", 1);
 219        ASSERT_EQ(id, 13, "typedef_fwd_id");
 220        t = btf__type_by_id(btf, 13);
 221        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "typedef1", "typedef_name");
 222        ASSERT_EQ(btf_kind(t), BTF_KIND_TYPEDEF, "typedef_kind");
 223        ASSERT_EQ(t->type, 1, "typedef_type");
 224
 225        /* FUNC & FUNC_PROTO */
 226        id = btf__add_func(btf, "func1", BTF_FUNC_GLOBAL, 15);
 227        ASSERT_EQ(id, 14, "func_id");
 228        t = btf__type_by_id(btf, 14);
 229        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "func1", "func_name");
 230        ASSERT_EQ(t->type, 15, "func_type");
 231        ASSERT_EQ(btf_kind(t), BTF_KIND_FUNC, "func_kind");
 232        ASSERT_EQ(btf_vlen(t), BTF_FUNC_GLOBAL, "func_vlen");
 233
 234        id = btf__add_func_proto(btf, 1);
 235        ASSERT_EQ(id, 15, "func_proto_id");
 236        err = btf__add_func_param(btf, "p1", 1);
 237        ASSERT_OK(err, "p1_res");
 238        err = btf__add_func_param(btf, "p2", 2);
 239        ASSERT_OK(err, "p2_res");
 240
 241        t = btf__type_by_id(btf, 15);
 242        ASSERT_EQ(btf_kind(t), BTF_KIND_FUNC_PROTO, "func_proto_kind");
 243        ASSERT_EQ(btf_vlen(t), 2, "func_proto_vlen");
 244        ASSERT_EQ(t->type, 1, "func_proto_ret_type");
 245        p = btf_params(t) + 0;
 246        ASSERT_STREQ(btf__str_by_offset(btf, p->name_off), "p1", "p1_name");
 247        ASSERT_EQ(p->type, 1, "p1_type");
 248        p = btf_params(t) + 1;
 249        ASSERT_STREQ(btf__str_by_offset(btf, p->name_off), "p2", "p2_name");
 250        ASSERT_EQ(p->type, 2, "p2_type");
 251
 252        /* VAR */
 253        id = btf__add_var(btf, "var1", BTF_VAR_GLOBAL_ALLOCATED, 1);
 254        ASSERT_EQ(id, 16, "var_id");
 255        t = btf__type_by_id(btf, 16);
 256        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "var1", "var_name");
 257        ASSERT_EQ(btf_kind(t), BTF_KIND_VAR, "var_kind");
 258        ASSERT_EQ(t->type, 1, "var_type");
 259        ASSERT_EQ(btf_var(t)->linkage, BTF_VAR_GLOBAL_ALLOCATED, "var_type");
 260
 261        /* DATASECT */
 262        id = btf__add_datasec(btf, "datasec1", 12);
 263        ASSERT_EQ(id, 17, "datasec_id");
 264        err = btf__add_datasec_var_info(btf, 1, 4, 8);
 265        ASSERT_OK(err, "v1_res");
 266
 267        t = btf__type_by_id(btf, 17);
 268        ASSERT_STREQ(btf__str_by_offset(btf, t->name_off), "datasec1", "datasec_name");
 269        ASSERT_EQ(t->size, 12, "datasec_sz");
 270        ASSERT_EQ(btf_kind(t), BTF_KIND_DATASEC, "datasec_kind");
 271        ASSERT_EQ(btf_vlen(t), 1, "datasec_vlen");
 272        vi = btf_var_secinfos(t) + 0;
 273        ASSERT_EQ(vi->type, 1, "v1_type");
 274        ASSERT_EQ(vi->offset, 4, "v1_off");
 275        ASSERT_EQ(vi->size, 8, "v1_sz");
 276
 277        btf__free(btf);
 278}
 279