linux/tools/testing/selftests/bpf/prog_tests/core_reloc.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2#include <test_progs.h>
   3#include "progs/core_reloc_types.h"
   4
   5#define STRUCT_TO_CHAR_PTR(struct_name) (const char *)&(struct struct_name)
   6
   7#define FLAVORS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {     \
   8        .a = 42,                                                        \
   9        .b = 0xc001,                                                    \
  10        .c = 0xbeef,                                                    \
  11}
  12
  13#define FLAVORS_CASE_COMMON(name)                                       \
  14        .case_name = #name,                                             \
  15        .bpf_obj_file = "test_core_reloc_flavors.o",                    \
  16        .btf_src_file = "btf__core_reloc_" #name ".o"                   \
  17
  18#define FLAVORS_CASE(name) {                                            \
  19        FLAVORS_CASE_COMMON(name),                                      \
  20        .input = FLAVORS_DATA(core_reloc_##name),                       \
  21        .input_len = sizeof(struct core_reloc_##name),                  \
  22        .output = FLAVORS_DATA(core_reloc_flavors),                     \
  23        .output_len = sizeof(struct core_reloc_flavors),                \
  24}
  25
  26#define FLAVORS_ERR_CASE(name) {                                        \
  27        FLAVORS_CASE_COMMON(name),                                      \
  28        .fails = true,                                                  \
  29}
  30
  31#define NESTING_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {     \
  32        .a = { .a = { .a = 42 } },                                      \
  33        .b = { .b = { .b = 0xc001 } },                                  \
  34}
  35
  36#define NESTING_CASE_COMMON(name)                                       \
  37        .case_name = #name,                                             \
  38        .bpf_obj_file = "test_core_reloc_nesting.o",                    \
  39        .btf_src_file = "btf__core_reloc_" #name ".o"
  40
  41#define NESTING_CASE(name) {                                            \
  42        NESTING_CASE_COMMON(name),                                      \
  43        .input = NESTING_DATA(core_reloc_##name),                       \
  44        .input_len = sizeof(struct core_reloc_##name),                  \
  45        .output = NESTING_DATA(core_reloc_nesting),                     \
  46        .output_len = sizeof(struct core_reloc_nesting)                 \
  47}
  48
  49#define NESTING_ERR_CASE(name) {                                        \
  50        NESTING_CASE_COMMON(name),                                      \
  51        .fails = true,                                                  \
  52}
  53
  54#define ARRAYS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {      \
  55        .a = { [2] = 1 },                                               \
  56        .b = { [1] = { [2] = { [3] = 2 } } },                           \
  57        .c = { [1] = { .c =  3 } },                                     \
  58        .d = { [0] = { [0] = { .d = 4 } } },                            \
  59}
  60
  61#define ARRAYS_CASE_COMMON(name)                                        \
  62        .case_name = #name,                                             \
  63        .bpf_obj_file = "test_core_reloc_arrays.o",                     \
  64        .btf_src_file = "btf__core_reloc_" #name ".o"
  65
  66#define ARRAYS_CASE(name) {                                             \
  67        ARRAYS_CASE_COMMON(name),                                       \
  68        .input = ARRAYS_DATA(core_reloc_##name),                        \
  69        .input_len = sizeof(struct core_reloc_##name),                  \
  70        .output = STRUCT_TO_CHAR_PTR(core_reloc_arrays_output) {        \
  71                .a2   = 1,                                              \
  72                .b123 = 2,                                              \
  73                .c1c  = 3,                                              \
  74                .d00d = 4,                                              \
  75        },                                                              \
  76        .output_len = sizeof(struct core_reloc_arrays_output)           \
  77}
  78
  79#define ARRAYS_ERR_CASE(name) {                                         \
  80        ARRAYS_CASE_COMMON(name),                                       \
  81        .fails = true,                                                  \
  82}
  83
  84#define PRIMITIVES_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {  \
  85        .a = 1,                                                         \
  86        .b = 2,                                                         \
  87        .c = 3,                                                         \
  88        .d = (void *)4,                                                 \
  89        .f = (void *)5,                                                 \
  90}
  91
  92#define PRIMITIVES_CASE_COMMON(name)                                    \
  93        .case_name = #name,                                             \
  94        .bpf_obj_file = "test_core_reloc_primitives.o",                 \
  95        .btf_src_file = "btf__core_reloc_" #name ".o"
  96
  97#define PRIMITIVES_CASE(name) {                                         \
  98        PRIMITIVES_CASE_COMMON(name),                                   \
  99        .input = PRIMITIVES_DATA(core_reloc_##name),                    \
 100        .input_len = sizeof(struct core_reloc_##name),                  \
 101        .output = PRIMITIVES_DATA(core_reloc_primitives),               \
 102        .output_len = sizeof(struct core_reloc_primitives),             \
 103}
 104
 105#define PRIMITIVES_ERR_CASE(name) {                                     \
 106        PRIMITIVES_CASE_COMMON(name),                                   \
 107        .fails = true,                                                  \
 108}
 109
 110#define MODS_CASE(name) {                                               \
 111        .case_name = #name,                                             \
 112        .bpf_obj_file = "test_core_reloc_mods.o",                       \
 113        .btf_src_file = "btf__core_reloc_" #name ".o",                  \
 114        .input = STRUCT_TO_CHAR_PTR(core_reloc_##name) {                \
 115                .a = 1,                                                 \
 116                .b = 2,                                                 \
 117                .c = (void *)3,                                         \
 118                .d = (void *)4,                                         \
 119                .e = { [2] = 5 },                                       \
 120                .f = { [1] = 6 },                                       \
 121                .g = { .x = 7 },                                        \
 122                .h = { .y = 8 },                                        \
 123        },                                                              \
 124        .input_len = sizeof(struct core_reloc_##name),                  \
 125        .output = STRUCT_TO_CHAR_PTR(core_reloc_mods_output) {          \
 126                .a = 1, .b = 2, .c = 3, .d = 4,                         \
 127                .e = 5, .f = 6, .g = 7, .h = 8,                         \
 128        },                                                              \
 129        .output_len = sizeof(struct core_reloc_mods_output),            \
 130}
 131
 132#define PTR_AS_ARR_CASE(name) {                                         \
 133        .case_name = #name,                                             \
 134        .bpf_obj_file = "test_core_reloc_ptr_as_arr.o",                 \
 135        .btf_src_file = "btf__core_reloc_" #name ".o",                  \
 136        .input = (const char *)&(struct core_reloc_##name []){          \
 137                { .a = 1 },                                             \
 138                { .a = 2 },                                             \
 139                { .a = 3 },                                             \
 140        },                                                              \
 141        .input_len = 3 * sizeof(struct core_reloc_##name),              \
 142        .output = STRUCT_TO_CHAR_PTR(core_reloc_ptr_as_arr) {           \
 143                .a = 3,                                                 \
 144        },                                                              \
 145        .output_len = sizeof(struct core_reloc_ptr_as_arr),             \
 146}
 147
 148#define INTS_DATA(struct_name) STRUCT_TO_CHAR_PTR(struct_name) {        \
 149        .u8_field = 1,                                                  \
 150        .s8_field = 2,                                                  \
 151        .u16_field = 3,                                                 \
 152        .s16_field = 4,                                                 \
 153        .u32_field = 5,                                                 \
 154        .s32_field = 6,                                                 \
 155        .u64_field = 7,                                                 \
 156        .s64_field = 8,                                                 \
 157}
 158
 159#define INTS_CASE_COMMON(name)                                          \
 160        .case_name = #name,                                             \
 161        .bpf_obj_file = "test_core_reloc_ints.o",                       \
 162        .btf_src_file = "btf__core_reloc_" #name ".o"
 163
 164#define INTS_CASE(name) {                                               \
 165        INTS_CASE_COMMON(name),                                         \
 166        .input = INTS_DATA(core_reloc_##name),                          \
 167        .input_len = sizeof(struct core_reloc_##name),                  \
 168        .output = INTS_DATA(core_reloc_ints),                           \
 169        .output_len = sizeof(struct core_reloc_ints),                   \
 170}
 171
 172#define INTS_ERR_CASE(name) {                                           \
 173        INTS_CASE_COMMON(name),                                         \
 174        .fails = true,                                                  \
 175}
 176
 177struct core_reloc_test_case {
 178        const char *case_name;
 179        const char *bpf_obj_file;
 180        const char *btf_src_file;
 181        const char *input;
 182        int input_len;
 183        const char *output;
 184        int output_len;
 185        bool fails;
 186};
 187
 188static struct core_reloc_test_case test_cases[] = {
 189        /* validate we can find kernel image and use its BTF for relocs */
 190        {
 191                .case_name = "kernel",
 192                .bpf_obj_file = "test_core_reloc_kernel.o",
 193                .btf_src_file = NULL, /* load from /lib/modules/$(uname -r) */
 194                .input = "",
 195                .input_len = 0,
 196                .output = "\1", /* true */
 197                .output_len = 1,
 198        },
 199
 200        /* validate BPF program can use multiple flavors to match against
 201         * single target BTF type
 202         */
 203        FLAVORS_CASE(flavors),
 204
 205        FLAVORS_ERR_CASE(flavors__err_wrong_name),
 206
 207        /* various struct/enum nesting and resolution scenarios */
 208        NESTING_CASE(nesting),
 209        NESTING_CASE(nesting___anon_embed),
 210        NESTING_CASE(nesting___struct_union_mixup),
 211        NESTING_CASE(nesting___extra_nesting),
 212        NESTING_CASE(nesting___dup_compat_types),
 213
 214        NESTING_ERR_CASE(nesting___err_missing_field),
 215        NESTING_ERR_CASE(nesting___err_array_field),
 216        NESTING_ERR_CASE(nesting___err_missing_container),
 217        NESTING_ERR_CASE(nesting___err_nonstruct_container),
 218        NESTING_ERR_CASE(nesting___err_array_container),
 219        NESTING_ERR_CASE(nesting___err_dup_incompat_types),
 220        NESTING_ERR_CASE(nesting___err_partial_match_dups),
 221        NESTING_ERR_CASE(nesting___err_too_deep),
 222
 223        /* various array access relocation scenarios */
 224        ARRAYS_CASE(arrays),
 225        ARRAYS_CASE(arrays___diff_arr_dim),
 226        ARRAYS_CASE(arrays___diff_arr_val_sz),
 227
 228        ARRAYS_ERR_CASE(arrays___err_too_small),
 229        ARRAYS_ERR_CASE(arrays___err_too_shallow),
 230        ARRAYS_ERR_CASE(arrays___err_non_array),
 231        ARRAYS_ERR_CASE(arrays___err_wrong_val_type1),
 232        ARRAYS_ERR_CASE(arrays___err_wrong_val_type2),
 233
 234        /* enum/ptr/int handling scenarios */
 235        PRIMITIVES_CASE(primitives),
 236        PRIMITIVES_CASE(primitives___diff_enum_def),
 237        PRIMITIVES_CASE(primitives___diff_func_proto),
 238        PRIMITIVES_CASE(primitives___diff_ptr_type),
 239
 240        PRIMITIVES_ERR_CASE(primitives___err_non_enum),
 241        PRIMITIVES_ERR_CASE(primitives___err_non_int),
 242        PRIMITIVES_ERR_CASE(primitives___err_non_ptr),
 243
 244        /* const/volatile/restrict and typedefs scenarios */
 245        MODS_CASE(mods),
 246        MODS_CASE(mods___mod_swap),
 247        MODS_CASE(mods___typedefs),
 248
 249        /* handling "ptr is an array" semantics */
 250        PTR_AS_ARR_CASE(ptr_as_arr),
 251        PTR_AS_ARR_CASE(ptr_as_arr___diff_sz),
 252
 253        /* int signedness/sizing/bitfield handling */
 254        INTS_CASE(ints),
 255        INTS_CASE(ints___bool),
 256        INTS_CASE(ints___reverse_sign),
 257
 258        INTS_ERR_CASE(ints___err_bitfield),
 259        INTS_ERR_CASE(ints___err_wrong_sz_8),
 260        INTS_ERR_CASE(ints___err_wrong_sz_16),
 261        INTS_ERR_CASE(ints___err_wrong_sz_32),
 262        INTS_ERR_CASE(ints___err_wrong_sz_64),
 263        
 264        /* validate edge cases of capturing relocations */
 265        {
 266                .case_name = "misc",
 267                .bpf_obj_file = "test_core_reloc_misc.o",
 268                .btf_src_file = "btf__core_reloc_misc.o",
 269                .input = (const char *)&(struct core_reloc_misc_extensible[]){
 270                        { .a = 1 },
 271                        { .a = 2 }, /* not read */
 272                        { .a = 3 },
 273                },
 274                .input_len = 4 * sizeof(int),
 275                .output = STRUCT_TO_CHAR_PTR(core_reloc_misc_output) {
 276                        .a = 1,
 277                        .b = 1,
 278                        .c = 0, /* BUG in clang, should be 3 */
 279                },
 280                .output_len = sizeof(struct core_reloc_misc_output),
 281        },
 282};
 283
 284struct data {
 285        char in[256];
 286        char out[256];
 287};
 288
 289void test_core_reloc(void)
 290{
 291        const char *probe_name = "raw_tracepoint/sys_enter";
 292        struct bpf_object_load_attr load_attr = {};
 293        struct core_reloc_test_case *test_case;
 294        int err, duration = 0, i, equal;
 295        struct bpf_link *link = NULL;
 296        struct bpf_map *data_map;
 297        struct bpf_program *prog;
 298        struct bpf_object *obj;
 299        const int zero = 0;
 300        struct data data;
 301
 302        for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
 303                test_case = &test_cases[i];
 304
 305                if (!test__start_subtest(test_case->case_name))
 306                        continue;
 307
 308                obj = bpf_object__open(test_case->bpf_obj_file);
 309                if (CHECK(IS_ERR_OR_NULL(obj), "obj_open",
 310                          "failed to open '%s': %ld\n",
 311                          test_case->bpf_obj_file, PTR_ERR(obj)))
 312                        continue;
 313
 314                prog = bpf_object__find_program_by_title(obj, probe_name);
 315                if (CHECK(!prog, "find_probe",
 316                          "prog '%s' not found\n", probe_name))
 317                        goto cleanup;
 318                bpf_program__set_type(prog, BPF_PROG_TYPE_RAW_TRACEPOINT);
 319
 320                load_attr.obj = obj;
 321                load_attr.log_level = 0;
 322                load_attr.target_btf_path = test_case->btf_src_file;
 323                err = bpf_object__load_xattr(&load_attr);
 324                if (test_case->fails) {
 325                        CHECK(!err, "obj_load_fail",
 326                              "should fail to load prog '%s'\n", probe_name);
 327                        goto cleanup;
 328                } else {
 329                        if (CHECK(err, "obj_load",
 330                                  "failed to load prog '%s': %d\n",
 331                                  probe_name, err))
 332                                goto cleanup;
 333                }
 334
 335                link = bpf_program__attach_raw_tracepoint(prog, "sys_enter");
 336                if (CHECK(IS_ERR(link), "attach_raw_tp", "err %ld\n",
 337                          PTR_ERR(link)))
 338                        goto cleanup;
 339
 340                data_map = bpf_object__find_map_by_name(obj, "test_cor.bss");
 341                if (CHECK(!data_map, "find_data_map", "data map not found\n"))
 342                        goto cleanup;
 343
 344                memset(&data, 0, sizeof(data));
 345                memcpy(data.in, test_case->input, test_case->input_len);
 346
 347                err = bpf_map_update_elem(bpf_map__fd(data_map),
 348                                          &zero, &data, 0);
 349                if (CHECK(err, "update_data_map",
 350                          "failed to update .data map: %d\n", err))
 351                        goto cleanup;
 352
 353                /* trigger test run */
 354                usleep(1);
 355
 356                err = bpf_map_lookup_elem(bpf_map__fd(data_map), &zero, &data);
 357                if (CHECK(err, "get_result",
 358                          "failed to get output data: %d\n", err))
 359                        goto cleanup;
 360
 361                equal = memcmp(data.out, test_case->output,
 362                               test_case->output_len) == 0;
 363                if (CHECK(!equal, "check_result",
 364                          "input/output data don't match\n")) {
 365                        int j;
 366
 367                        for (j = 0; j < test_case->input_len; j++) {
 368                                printf("input byte #%d: 0x%02hhx\n",
 369                                       j, test_case->input[j]);
 370                        }
 371                        for (j = 0; j < test_case->output_len; j++) {
 372                                printf("output byte #%d: EXP 0x%02hhx GOT 0x%02hhx\n",
 373                                       j, test_case->output[j], data.out[j]);
 374                        }
 375                        goto cleanup;
 376                }
 377
 378cleanup:
 379                if (!IS_ERR_OR_NULL(link)) {
 380                        bpf_link__destroy(link);
 381                        link = NULL;
 382                }
 383                bpf_object__close(obj);
 384        }
 385}
 386