1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2026 Xiaomi */ 3 4 #include <test_progs.h> 5 #include <bpf/btf.h> 6 #include "btf_helpers.h" 7 8 static void permute_base_check(struct btf *btf) 9 { 10 VALIDATE_RAW_BTF( 11 btf, 12 "[1] STRUCT 's2' size=4 vlen=1\n" 13 "\t'm' type_id=4 bits_offset=0", 14 "[2] FUNC 'f' type_id=6 linkage=static", 15 "[3] PTR '(anon)' type_id=4", 16 "[4] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", 17 "[5] STRUCT 's1' size=4 vlen=1\n" 18 "\t'm' type_id=4 bits_offset=0", 19 "[6] FUNC_PROTO '(anon)' ret_type_id=4 vlen=1\n" 20 "\t'p' type_id=3"); 21 } 22 23 /* Ensure btf__permute works as expected in the base-BTF scenario */ 24 static void test_permute_base(void) 25 { 26 struct btf *btf; 27 __u32 permute_ids[7]; 28 int err; 29 30 btf = btf__new_empty(); 31 if (!ASSERT_OK_PTR(btf, "empty_main_btf")) 32 return; 33 34 btf__add_int(btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ 35 btf__add_ptr(btf, 1); /* [2] ptr to int */ 36 btf__add_struct(btf, "s1", 4); /* [3] struct s1 { */ 37 btf__add_field(btf, "m", 1, 0, 0); /* int m; */ 38 /* } */ 39 btf__add_struct(btf, "s2", 4); /* [4] struct s2 { */ 40 btf__add_field(btf, "m", 1, 0, 0); /* int m; */ 41 /* } */ 42 btf__add_func_proto(btf, 1); /* [5] int (*)(int *p); */ 43 btf__add_func_param(btf, "p", 2); 44 btf__add_func(btf, "f", BTF_FUNC_STATIC, 5); /* [6] int f(int *p); */ 45 46 VALIDATE_RAW_BTF( 47 btf, 48 "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", 49 "[2] PTR '(anon)' type_id=1", 50 "[3] STRUCT 's1' size=4 vlen=1\n" 51 "\t'm' type_id=1 bits_offset=0", 52 "[4] STRUCT 's2' size=4 vlen=1\n" 53 "\t'm' type_id=1 bits_offset=0", 54 "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" 55 "\t'p' type_id=2", 56 "[6] FUNC 'f' type_id=5 linkage=static"); 57 58 permute_ids[0] = 0; /* [0] -> [0] */ 59 permute_ids[1] = 4; /* [1] -> [4] */ 60 permute_ids[2] = 3; /* [2] -> [3] */ 61 permute_ids[3] = 5; /* [3] -> [5] */ 62 permute_ids[4] = 1; /* [4] -> [1] */ 63 permute_ids[5] = 6; /* [5] -> [6] */ 64 permute_ids[6] = 2; /* [6] -> [2] */ 65 err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); 66 if (!ASSERT_OK(err, "btf__permute_base")) 67 goto done; 68 permute_base_check(btf); 69 70 /* ids[0] must be 0 for base BTF */ 71 permute_ids[0] = 4; /* [0] -> [0] */ 72 permute_ids[1] = 0; /* [1] -> [4] */ 73 permute_ids[2] = 3; /* [2] -> [3] */ 74 permute_ids[3] = 5; /* [3] -> [5] */ 75 permute_ids[4] = 1; /* [4] -> [1] */ 76 permute_ids[5] = 6; /* [5] -> [6] */ 77 permute_ids[6] = 2; /* [6] -> [2] */ 78 err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); 79 if (!ASSERT_ERR(err, "btf__permute_base")) 80 goto done; 81 /* BTF is not modified */ 82 permute_base_check(btf); 83 84 /* id_map_cnt is invalid */ 85 permute_ids[0] = 0; /* [0] -> [0] */ 86 permute_ids[1] = 4; /* [1] -> [4] */ 87 permute_ids[2] = 3; /* [2] -> [3] */ 88 permute_ids[3] = 5; /* [3] -> [5] */ 89 permute_ids[4] = 1; /* [4] -> [1] */ 90 permute_ids[5] = 6; /* [5] -> [6] */ 91 permute_ids[6] = 2; /* [6] -> [2] */ 92 err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids) - 1, NULL); 93 if (!ASSERT_ERR(err, "btf__permute_base")) 94 goto done; 95 /* BTF is not modified */ 96 permute_base_check(btf); 97 98 /* Multiple types can not be mapped to the same ID */ 99 permute_ids[0] = 0; 100 permute_ids[1] = 4; 101 permute_ids[2] = 4; 102 permute_ids[3] = 5; 103 permute_ids[4] = 1; 104 permute_ids[5] = 6; 105 permute_ids[6] = 2; 106 err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); 107 if (!ASSERT_ERR(err, "btf__permute_base")) 108 goto done; 109 /* BTF is not modified */ 110 permute_base_check(btf); 111 112 /* Type ID must be valid */ 113 permute_ids[0] = 0; 114 permute_ids[1] = 4; 115 permute_ids[2] = 3; 116 permute_ids[3] = 5; 117 permute_ids[4] = 1; 118 permute_ids[5] = 7; 119 permute_ids[6] = 2; 120 err = btf__permute(btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); 121 if (!ASSERT_ERR(err, "btf__permute_base")) 122 goto done; 123 /* BTF is not modified */ 124 permute_base_check(btf); 125 126 done: 127 btf__free(btf); 128 } 129 130 static void permute_split_check(struct btf *btf) 131 { 132 VALIDATE_RAW_BTF( 133 btf, 134 "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", 135 "[2] PTR '(anon)' type_id=1", 136 "[3] STRUCT 's2' size=4 vlen=1\n" 137 "\t'm' type_id=1 bits_offset=0", 138 "[4] FUNC 'f' type_id=5 linkage=static", 139 "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" 140 "\t'p' type_id=2", 141 "[6] STRUCT 's1' size=4 vlen=1\n" 142 "\t'm' type_id=1 bits_offset=0"); 143 } 144 145 /* Ensure btf__permute works as expected in the split-BTF scenario */ 146 static void test_permute_split(void) 147 { 148 struct btf *split_btf = NULL, *base_btf = NULL; 149 __u32 permute_ids[4]; 150 int err, start_id; 151 152 base_btf = btf__new_empty(); 153 if (!ASSERT_OK_PTR(base_btf, "empty_main_btf")) 154 return; 155 156 btf__add_int(base_btf, "int", 4, BTF_INT_SIGNED); /* [1] int */ 157 btf__add_ptr(base_btf, 1); /* [2] ptr to int */ 158 VALIDATE_RAW_BTF( 159 base_btf, 160 "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", 161 "[2] PTR '(anon)' type_id=1"); 162 split_btf = btf__new_empty_split(base_btf); 163 if (!ASSERT_OK_PTR(split_btf, "empty_split_btf")) 164 goto cleanup; 165 btf__add_struct(split_btf, "s1", 4); /* [3] struct s1 { */ 166 btf__add_field(split_btf, "m", 1, 0, 0); /* int m; */ 167 /* } */ 168 btf__add_struct(split_btf, "s2", 4); /* [4] struct s2 { */ 169 btf__add_field(split_btf, "m", 1, 0, 0); /* int m; */ 170 /* } */ 171 btf__add_func_proto(split_btf, 1); /* [5] int (*)(int p); */ 172 btf__add_func_param(split_btf, "p", 2); 173 btf__add_func(split_btf, "f", BTF_FUNC_STATIC, 5); /* [6] int f(int *p); */ 174 175 VALIDATE_RAW_BTF( 176 split_btf, 177 "[1] INT 'int' size=4 bits_offset=0 nr_bits=32 encoding=SIGNED", 178 "[2] PTR '(anon)' type_id=1", 179 "[3] STRUCT 's1' size=4 vlen=1\n" 180 "\t'm' type_id=1 bits_offset=0", 181 "[4] STRUCT 's2' size=4 vlen=1\n" 182 "\t'm' type_id=1 bits_offset=0", 183 "[5] FUNC_PROTO '(anon)' ret_type_id=1 vlen=1\n" 184 "\t'p' type_id=2", 185 "[6] FUNC 'f' type_id=5 linkage=static"); 186 187 start_id = btf__type_cnt(base_btf); 188 permute_ids[3 - start_id] = 6; /* [3] -> [6] */ 189 permute_ids[4 - start_id] = 3; /* [4] -> [3] */ 190 permute_ids[5 - start_id] = 5; /* [5] -> [5] */ 191 permute_ids[6 - start_id] = 4; /* [6] -> [4] */ 192 err = btf__permute(split_btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); 193 if (!ASSERT_OK(err, "btf__permute_split")) 194 goto cleanup; 195 permute_split_check(split_btf); 196 197 /* 198 * For split BTF, id_map_cnt must equal to the number of types 199 * added on top of base BTF 200 */ 201 permute_ids[3 - start_id] = 4; 202 permute_ids[4 - start_id] = 3; 203 permute_ids[5 - start_id] = 5; 204 permute_ids[6 - start_id] = 6; 205 err = btf__permute(split_btf, permute_ids, ARRAY_SIZE(permute_ids) - 1, NULL); 206 if (!ASSERT_ERR(err, "btf__permute_split")) 207 goto cleanup; 208 /* BTF is not modified */ 209 permute_split_check(split_btf); 210 211 /* Multiple types can not be mapped to the same ID */ 212 permute_ids[3 - start_id] = 4; 213 permute_ids[4 - start_id] = 3; 214 permute_ids[5 - start_id] = 3; 215 permute_ids[6 - start_id] = 6; 216 err = btf__permute(split_btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); 217 if (!ASSERT_ERR(err, "btf__permute_split")) 218 goto cleanup; 219 /* BTF is not modified */ 220 permute_split_check(split_btf); 221 222 /* Can not map to base ID */ 223 permute_ids[3 - start_id] = 4; 224 permute_ids[4 - start_id] = 2; 225 permute_ids[5 - start_id] = 5; 226 permute_ids[6 - start_id] = 6; 227 err = btf__permute(split_btf, permute_ids, ARRAY_SIZE(permute_ids), NULL); 228 if (!ASSERT_ERR(err, "btf__permute_split")) 229 goto cleanup; 230 /* BTF is not modified */ 231 permute_split_check(split_btf); 232 233 cleanup: 234 btf__free(split_btf); 235 btf__free(base_btf); 236 } 237 238 void test_btf_permute(void) 239 { 240 if (test__start_subtest("permute_base")) 241 test_permute_base(); 242 if (test__start_subtest("permute_split")) 243 test_permute_split(); 244 } 245