~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/bpf/prog_tests/btf_endian.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0
  2 /* Copyright (c) 2020 Facebook */
  3 #define _GNU_SOURCE
  4 #include <string.h>
  5 #include <byteswap.h>
  6 #include <test_progs.h>
  7 #include <bpf/btf.h>
  8 
  9 void test_btf_endian() {
 10 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
 11         enum btf_endianness endian = BTF_LITTLE_ENDIAN;
 12 #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
 13         enum btf_endianness endian = BTF_BIG_ENDIAN;
 14 #else
 15 #error "Unrecognized __BYTE_ORDER__"
 16 #endif
 17         enum btf_endianness swap_endian = 1 - endian;
 18         struct btf *btf = NULL, *swap_btf = NULL;
 19         const void *raw_data, *swap_raw_data;
 20         const struct btf_type *t;
 21         const struct btf_header *hdr;
 22         __u32 raw_sz, swap_raw_sz;
 23         int var_id;
 24 
 25         /* Load BTF in native endianness */
 26         btf = btf__parse_elf("btf_dump_test_case_syntax.bpf.o", NULL);
 27         if (!ASSERT_OK_PTR(btf, "parse_native_btf"))
 28                 goto err_out;
 29 
 30         ASSERT_EQ(btf__endianness(btf), endian, "endian");
 31         btf__set_endianness(btf, swap_endian);
 32         ASSERT_EQ(btf__endianness(btf), swap_endian, "endian");
 33 
 34         /* Get raw BTF data in non-native endianness... */
 35         raw_data = btf__raw_data(btf, &raw_sz);
 36         if (!ASSERT_OK_PTR(raw_data, "raw_data_inverted"))
 37                 goto err_out;
 38 
 39         /* ...and open it as a new BTF instance */
 40         swap_btf = btf__new(raw_data, raw_sz);
 41         if (!ASSERT_OK_PTR(swap_btf, "parse_swap_btf"))
 42                 goto err_out;
 43 
 44         ASSERT_EQ(btf__endianness(swap_btf), swap_endian, "endian");
 45         ASSERT_EQ(btf__type_cnt(swap_btf), btf__type_cnt(btf), "nr_types");
 46 
 47         swap_raw_data = btf__raw_data(swap_btf, &swap_raw_sz);
 48         if (!ASSERT_OK_PTR(swap_raw_data, "swap_raw_data"))
 49                 goto err_out;
 50 
 51         /* both raw data should be identical (with non-native endianness) */
 52         ASSERT_OK(memcmp(raw_data, swap_raw_data, raw_sz), "mem_identical");
 53 
 54         /* make sure that at least BTF header data is really swapped */
 55         hdr = swap_raw_data;
 56         ASSERT_EQ(bswap_16(hdr->magic), BTF_MAGIC, "btf_magic_swapped");
 57         ASSERT_EQ(raw_sz, swap_raw_sz, "raw_sizes");
 58 
 59         /* swap it back to native endianness */
 60         btf__set_endianness(swap_btf, endian);
 61         swap_raw_data = btf__raw_data(swap_btf, &swap_raw_sz);
 62         if (!ASSERT_OK_PTR(swap_raw_data, "swap_raw_data"))
 63                 goto err_out;
 64 
 65         /* now header should have native BTF_MAGIC */
 66         hdr = swap_raw_data;
 67         ASSERT_EQ(hdr->magic, BTF_MAGIC, "btf_magic_native");
 68         ASSERT_EQ(raw_sz, swap_raw_sz, "raw_sizes");
 69 
 70         /* now modify original BTF */
 71         var_id = btf__add_var(btf, "some_var", BTF_VAR_GLOBAL_ALLOCATED, 1);
 72         ASSERT_GT(var_id, 0, "var_id");
 73 
 74         btf__free(swap_btf);
 75         swap_btf = NULL;
 76 
 77         btf__set_endianness(btf, swap_endian);
 78         raw_data = btf__raw_data(btf, &raw_sz);
 79         if (!ASSERT_OK_PTR(raw_data, "raw_data_inverted"))
 80                 goto err_out;
 81 
 82         /* and re-open swapped raw data again */
 83         swap_btf = btf__new(raw_data, raw_sz);
 84         if (!ASSERT_OK_PTR(swap_btf, "parse_swap_btf"))
 85                 goto err_out;
 86 
 87         ASSERT_EQ(btf__endianness(swap_btf), swap_endian, "endian");
 88         ASSERT_EQ(btf__type_cnt(swap_btf), btf__type_cnt(btf), "nr_types");
 89 
 90         /* the type should appear as if it was stored in native endianness */
 91         t = btf__type_by_id(swap_btf, var_id);
 92         ASSERT_STREQ(btf__str_by_offset(swap_btf, t->name_off), "some_var", "var_name");
 93         ASSERT_EQ(btf_var(t)->linkage, BTF_VAR_GLOBAL_ALLOCATED, "var_linkage");
 94         ASSERT_EQ(t->type, 1, "var_type");
 95 
 96 err_out:
 97         btf__free(btf);
 98         btf__free(swap_btf);
 99 }
100 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php