test_btf.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /* Copyright (c) 2018 Facebook */
  3. #include <linux/bpf.h>
  4. #include <linux/btf.h>
  5. #include <linux/err.h>
  6. #include <bpf/bpf.h>
  7. #include <sys/resource.h>
  8. #include <libelf.h>
  9. #include <gelf.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <stdarg.h>
  14. #include <unistd.h>
  15. #include <fcntl.h>
  16. #include <errno.h>
  17. #include <bpf/libbpf.h>
  18. #include <bpf/btf.h>
  19. #include "bpf_rlimit.h"
  20. #include "bpf_util.h"
  21. static uint32_t pass_cnt;
  22. static uint32_t error_cnt;
  23. static uint32_t skip_cnt;
  24. #define CHECK(condition, format...) ({ \
  25. int __ret = !!(condition); \
  26. if (__ret) { \
  27. fprintf(stderr, "%s:%d:FAIL ", __func__, __LINE__); \
  28. fprintf(stderr, format); \
  29. } \
  30. __ret; \
  31. })
  32. static int count_result(int err)
  33. {
  34. if (err)
  35. error_cnt++;
  36. else
  37. pass_cnt++;
  38. fprintf(stderr, "\n");
  39. return err;
  40. }
  41. #define min(a, b) ((a) < (b) ? (a) : (b))
  42. #define __printf(a, b) __attribute__((format(printf, a, b)))
  43. __printf(1, 2)
  44. static int __base_pr(const char *format, ...)
  45. {
  46. va_list args;
  47. int err;
  48. va_start(args, format);
  49. err = vfprintf(stderr, format, args);
  50. va_end(args);
  51. return err;
  52. }
  53. #define BTF_INFO_ENC(kind, root, vlen) \
  54. ((!!(root) << 31) | ((kind) << 24) | ((vlen) & BTF_MAX_VLEN))
  55. #define BTF_TYPE_ENC(name, info, size_or_type) \
  56. (name), (info), (size_or_type)
  57. #define BTF_INT_ENC(encoding, bits_offset, nr_bits) \
  58. ((encoding) << 24 | (bits_offset) << 16 | (nr_bits))
  59. #define BTF_TYPE_INT_ENC(name, encoding, bits_offset, bits, sz) \
  60. BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), sz), \
  61. BTF_INT_ENC(encoding, bits_offset, bits)
  62. #define BTF_ARRAY_ENC(type, index_type, nr_elems) \
  63. (type), (index_type), (nr_elems)
  64. #define BTF_TYPE_ARRAY_ENC(type, index_type, nr_elems) \
  65. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 0), \
  66. BTF_ARRAY_ENC(type, index_type, nr_elems)
  67. #define BTF_MEMBER_ENC(name, type, bits_offset) \
  68. (name), (type), (bits_offset)
  69. #define BTF_ENUM_ENC(name, val) (name), (val)
  70. #define BTF_TYPEDEF_ENC(name, type) \
  71. BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), type)
  72. #define BTF_PTR_ENC(name, type) \
  73. BTF_TYPE_ENC(name, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), type)
  74. #define BTF_END_RAW 0xdeadbeef
  75. #define NAME_TBD 0xdeadb33f
  76. #define MAX_NR_RAW_TYPES 1024
  77. #define BTF_LOG_BUF_SIZE 65535
  78. static struct args {
  79. unsigned int raw_test_num;
  80. unsigned int file_test_num;
  81. unsigned int get_info_test_num;
  82. bool raw_test;
  83. bool file_test;
  84. bool get_info_test;
  85. bool pprint_test;
  86. bool always_log;
  87. } args;
  88. static char btf_log_buf[BTF_LOG_BUF_SIZE];
  89. static struct btf_header hdr_tmpl = {
  90. .magic = BTF_MAGIC,
  91. .version = BTF_VERSION,
  92. .hdr_len = sizeof(struct btf_header),
  93. };
  94. struct btf_raw_test {
  95. const char *descr;
  96. const char *str_sec;
  97. const char *map_name;
  98. const char *err_str;
  99. __u32 raw_types[MAX_NR_RAW_TYPES];
  100. __u32 str_sec_size;
  101. enum bpf_map_type map_type;
  102. __u32 key_size;
  103. __u32 value_size;
  104. __u32 key_type_id;
  105. __u32 value_type_id;
  106. __u32 max_entries;
  107. bool btf_load_err;
  108. bool map_create_err;
  109. bool ordered_map;
  110. bool lossless_map;
  111. int hdr_len_delta;
  112. int type_off_delta;
  113. int str_off_delta;
  114. int str_len_delta;
  115. };
  116. static struct btf_raw_test raw_tests[] = {
  117. /* enum E {
  118. * E0,
  119. * E1,
  120. * };
  121. *
  122. * struct A {
  123. * unsigned long long m;
  124. * int n;
  125. * char o;
  126. * [3 bytes hole]
  127. * int p[8];
  128. * int q[4][8];
  129. * enum E r;
  130. * };
  131. */
  132. {
  133. .descr = "struct test #1",
  134. .raw_types = {
  135. /* int */
  136. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  137. /* unsigned long long */
  138. BTF_TYPE_INT_ENC(0, 0, 0, 64, 8), /* [2] */
  139. /* char */
  140. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 8, 1), /* [3] */
  141. /* int[8] */
  142. BTF_TYPE_ARRAY_ENC(1, 1, 8), /* [4] */
  143. /* struct A { */ /* [5] */
  144. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 6), 180),
  145. BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* unsigned long long m;*/
  146. BTF_MEMBER_ENC(NAME_TBD, 1, 64),/* int n; */
  147. BTF_MEMBER_ENC(NAME_TBD, 3, 96),/* char o; */
  148. BTF_MEMBER_ENC(NAME_TBD, 4, 128),/* int p[8] */
  149. BTF_MEMBER_ENC(NAME_TBD, 6, 384),/* int q[4][8] */
  150. BTF_MEMBER_ENC(NAME_TBD, 7, 1408), /* enum E r */
  151. /* } */
  152. /* int[4][8] */
  153. BTF_TYPE_ARRAY_ENC(4, 1, 4), /* [6] */
  154. /* enum E */ /* [7] */
  155. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
  156. BTF_ENUM_ENC(NAME_TBD, 0),
  157. BTF_ENUM_ENC(NAME_TBD, 1),
  158. BTF_END_RAW,
  159. },
  160. .str_sec = "\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1",
  161. .str_sec_size = sizeof("\0A\0m\0n\0o\0p\0q\0r\0E\0E0\0E1"),
  162. .map_type = BPF_MAP_TYPE_ARRAY,
  163. .map_name = "struct_test1_map",
  164. .key_size = sizeof(int),
  165. .value_size = 180,
  166. .key_type_id = 1,
  167. .value_type_id = 5,
  168. .max_entries = 4,
  169. },
  170. /* typedef struct b Struct_B;
  171. *
  172. * struct A {
  173. * int m;
  174. * struct b n[4];
  175. * const Struct_B o[4];
  176. * };
  177. *
  178. * struct B {
  179. * int m;
  180. * int n;
  181. * };
  182. */
  183. {
  184. .descr = "struct test #2",
  185. .raw_types = {
  186. /* int */ /* [1] */
  187. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  188. /* struct b [4] */ /* [2] */
  189. BTF_TYPE_ARRAY_ENC(4, 1, 4),
  190. /* struct A { */ /* [3] */
  191. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 3), 68),
  192. BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
  193. BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct B n[4] */
  194. BTF_MEMBER_ENC(NAME_TBD, 8, 288),/* const Struct_B o[4];*/
  195. /* } */
  196. /* struct B { */ /* [4] */
  197. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
  198. BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
  199. BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
  200. /* } */
  201. /* const int */ /* [5] */
  202. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
  203. /* typedef struct b Struct_B */ /* [6] */
  204. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_TYPEDEF, 0, 0), 4),
  205. /* const Struct_B */ /* [7] */
  206. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 6),
  207. /* const Struct_B [4] */ /* [8] */
  208. BTF_TYPE_ARRAY_ENC(7, 1, 4),
  209. BTF_END_RAW,
  210. },
  211. .str_sec = "\0A\0m\0n\0o\0B\0m\0n\0Struct_B",
  212. .str_sec_size = sizeof("\0A\0m\0n\0o\0B\0m\0n\0Struct_B"),
  213. .map_type = BPF_MAP_TYPE_ARRAY,
  214. .map_name = "struct_test2_map",
  215. .key_size = sizeof(int),
  216. .value_size = 68,
  217. .key_type_id = 1,
  218. .value_type_id = 3,
  219. .max_entries = 4,
  220. },
  221. {
  222. .descr = "struct test #3 Invalid member offset",
  223. .raw_types = {
  224. /* int */ /* [1] */
  225. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  226. /* int64 */ /* [2] */
  227. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 64, 8),
  228. /* struct A { */ /* [3] */
  229. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 16),
  230. BTF_MEMBER_ENC(NAME_TBD, 1, 64), /* int m; */
  231. BTF_MEMBER_ENC(NAME_TBD, 2, 0), /* int64 n; */
  232. /* } */
  233. BTF_END_RAW,
  234. },
  235. .str_sec = "\0A\0m\0n\0",
  236. .str_sec_size = sizeof("\0A\0m\0n\0"),
  237. .map_type = BPF_MAP_TYPE_ARRAY,
  238. .map_name = "struct_test3_map",
  239. .key_size = sizeof(int),
  240. .value_size = 16,
  241. .key_type_id = 1,
  242. .value_type_id = 3,
  243. .max_entries = 4,
  244. .btf_load_err = true,
  245. .err_str = "Invalid member bits_offset",
  246. },
  247. /* Test member exceeds the size of struct.
  248. *
  249. * struct A {
  250. * int m;
  251. * int n;
  252. * };
  253. */
  254. {
  255. .descr = "size check test #1",
  256. .raw_types = {
  257. /* int */ /* [1] */
  258. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  259. /* struct A { */ /* [2] */
  260. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 - 1),
  261. BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
  262. BTF_MEMBER_ENC(NAME_TBD, 1, 32),/* int n; */
  263. /* } */
  264. BTF_END_RAW,
  265. },
  266. .str_sec = "\0A\0m\0n",
  267. .str_sec_size = sizeof("\0A\0m\0n"),
  268. .map_type = BPF_MAP_TYPE_ARRAY,
  269. .map_name = "size_check1_map",
  270. .key_size = sizeof(int),
  271. .value_size = 1,
  272. .key_type_id = 1,
  273. .value_type_id = 2,
  274. .max_entries = 4,
  275. .btf_load_err = true,
  276. .err_str = "Member exceeds struct_size",
  277. },
  278. /* Test member exeeds the size of struct
  279. *
  280. * struct A {
  281. * int m;
  282. * int n[2];
  283. * };
  284. */
  285. {
  286. .descr = "size check test #2",
  287. .raw_types = {
  288. /* int */ /* [1] */
  289. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
  290. /* int[2] */ /* [2] */
  291. BTF_TYPE_ARRAY_ENC(1, 1, 2),
  292. /* struct A { */ /* [3] */
  293. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 3 - 1),
  294. BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
  295. BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* int n[2]; */
  296. /* } */
  297. BTF_END_RAW,
  298. },
  299. .str_sec = "\0A\0m\0n",
  300. .str_sec_size = sizeof("\0A\0m\0n"),
  301. .map_type = BPF_MAP_TYPE_ARRAY,
  302. .map_name = "size_check2_map",
  303. .key_size = sizeof(int),
  304. .value_size = 1,
  305. .key_type_id = 1,
  306. .value_type_id = 3,
  307. .max_entries = 4,
  308. .btf_load_err = true,
  309. .err_str = "Member exceeds struct_size",
  310. },
  311. /* Test member exeeds the size of struct
  312. *
  313. * struct A {
  314. * int m;
  315. * void *n;
  316. * };
  317. */
  318. {
  319. .descr = "size check test #3",
  320. .raw_types = {
  321. /* int */ /* [1] */
  322. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
  323. /* void* */ /* [2] */
  324. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
  325. /* struct A { */ /* [3] */
  326. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) + sizeof(void *) - 1),
  327. BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
  328. BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* void *n; */
  329. /* } */
  330. BTF_END_RAW,
  331. },
  332. .str_sec = "\0A\0m\0n",
  333. .str_sec_size = sizeof("\0A\0m\0n"),
  334. .map_type = BPF_MAP_TYPE_ARRAY,
  335. .map_name = "size_check3_map",
  336. .key_size = sizeof(int),
  337. .value_size = 1,
  338. .key_type_id = 1,
  339. .value_type_id = 3,
  340. .max_entries = 4,
  341. .btf_load_err = true,
  342. .err_str = "Member exceeds struct_size",
  343. },
  344. /* Test member exceeds the size of struct
  345. *
  346. * enum E {
  347. * E0,
  348. * E1,
  349. * };
  350. *
  351. * struct A {
  352. * int m;
  353. * enum E n;
  354. * };
  355. */
  356. {
  357. .descr = "size check test #4",
  358. .raw_types = {
  359. /* int */ /* [1] */
  360. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, sizeof(int)),
  361. /* enum E { */ /* [2] */
  362. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 2), sizeof(int)),
  363. BTF_ENUM_ENC(NAME_TBD, 0),
  364. BTF_ENUM_ENC(NAME_TBD, 1),
  365. /* } */
  366. /* struct A { */ /* [3] */
  367. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), sizeof(int) * 2 - 1),
  368. BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int m; */
  369. BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* enum E n; */
  370. /* } */
  371. BTF_END_RAW,
  372. },
  373. .str_sec = "\0E\0E0\0E1\0A\0m\0n",
  374. .str_sec_size = sizeof("\0E\0E0\0E1\0A\0m\0n"),
  375. .map_type = BPF_MAP_TYPE_ARRAY,
  376. .map_name = "size_check4_map",
  377. .key_size = sizeof(int),
  378. .value_size = 1,
  379. .key_type_id = 1,
  380. .value_type_id = 3,
  381. .max_entries = 4,
  382. .btf_load_err = true,
  383. .err_str = "Member exceeds struct_size",
  384. },
  385. /* typedef const void * const_void_ptr;
  386. * struct A {
  387. * const_void_ptr m;
  388. * };
  389. */
  390. {
  391. .descr = "void test #1",
  392. .raw_types = {
  393. /* int */ /* [1] */
  394. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  395. /* const void */ /* [2] */
  396. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
  397. /* const void* */ /* [3] */
  398. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
  399. /* typedef const void * const_void_ptr */
  400. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
  401. /* struct A { */ /* [4] */
  402. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
  403. /* const_void_ptr m; */
  404. BTF_MEMBER_ENC(NAME_TBD, 3, 0),
  405. /* } */
  406. BTF_END_RAW,
  407. },
  408. .str_sec = "\0const_void_ptr\0A\0m",
  409. .str_sec_size = sizeof("\0const_void_ptr\0A\0m"),
  410. .map_type = BPF_MAP_TYPE_ARRAY,
  411. .map_name = "void_test1_map",
  412. .key_size = sizeof(int),
  413. .value_size = sizeof(void *),
  414. .key_type_id = 1,
  415. .value_type_id = 4,
  416. .max_entries = 4,
  417. },
  418. /* struct A {
  419. * const void m;
  420. * };
  421. */
  422. {
  423. .descr = "void test #2",
  424. .raw_types = {
  425. /* int */ /* [1] */
  426. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  427. /* const void */ /* [2] */
  428. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
  429. /* struct A { */ /* [3] */
  430. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), 8),
  431. /* const void m; */
  432. BTF_MEMBER_ENC(NAME_TBD, 2, 0),
  433. /* } */
  434. BTF_END_RAW,
  435. },
  436. .str_sec = "\0A\0m",
  437. .str_sec_size = sizeof("\0A\0m"),
  438. .map_type = BPF_MAP_TYPE_ARRAY,
  439. .map_name = "void_test2_map",
  440. .key_size = sizeof(int),
  441. .value_size = sizeof(void *),
  442. .key_type_id = 1,
  443. .value_type_id = 3,
  444. .max_entries = 4,
  445. .btf_load_err = true,
  446. .err_str = "Invalid member",
  447. },
  448. /* typedef const void * const_void_ptr;
  449. * const_void_ptr[4]
  450. */
  451. {
  452. .descr = "void test #3",
  453. .raw_types = {
  454. /* int */ /* [1] */
  455. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  456. /* const void */ /* [2] */
  457. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
  458. /* const void* */ /* [3] */
  459. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 2),
  460. /* typedef const void * const_void_ptr */ /* [4] */
  461. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
  462. /* const_void_ptr[4] */ /* [5] */
  463. BTF_TYPE_ARRAY_ENC(3, 1, 4),
  464. BTF_END_RAW,
  465. },
  466. .str_sec = "\0const_void_ptr",
  467. .str_sec_size = sizeof("\0const_void_ptr"),
  468. .map_type = BPF_MAP_TYPE_ARRAY,
  469. .map_name = "void_test3_map",
  470. .key_size = sizeof(int),
  471. .value_size = sizeof(void *) * 4,
  472. .key_type_id = 1,
  473. .value_type_id = 5,
  474. .max_entries = 4,
  475. },
  476. /* const void[4] */
  477. {
  478. .descr = "void test #4",
  479. .raw_types = {
  480. /* int */ /* [1] */
  481. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  482. /* const void */ /* [2] */
  483. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
  484. /* const void[4] */ /* [3] */
  485. BTF_TYPE_ARRAY_ENC(2, 1, 4),
  486. BTF_END_RAW,
  487. },
  488. .str_sec = "\0A\0m",
  489. .str_sec_size = sizeof("\0A\0m"),
  490. .map_type = BPF_MAP_TYPE_ARRAY,
  491. .map_name = "void_test4_map",
  492. .key_size = sizeof(int),
  493. .value_size = sizeof(void *) * 4,
  494. .key_type_id = 1,
  495. .value_type_id = 3,
  496. .max_entries = 4,
  497. .btf_load_err = true,
  498. .err_str = "Invalid elem",
  499. },
  500. /* Array_A <------------------+
  501. * elem_type == Array_B |
  502. * | |
  503. * | |
  504. * Array_B <-------- + |
  505. * elem_type == Array A --+
  506. */
  507. {
  508. .descr = "loop test #1",
  509. .raw_types = {
  510. /* int */ /* [1] */
  511. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  512. /* Array_A */ /* [2] */
  513. BTF_TYPE_ARRAY_ENC(3, 1, 8),
  514. /* Array_B */ /* [3] */
  515. BTF_TYPE_ARRAY_ENC(2, 1, 8),
  516. BTF_END_RAW,
  517. },
  518. .str_sec = "",
  519. .str_sec_size = sizeof(""),
  520. .map_type = BPF_MAP_TYPE_ARRAY,
  521. .map_name = "loop_test1_map",
  522. .key_size = sizeof(int),
  523. .value_size = sizeof(sizeof(int) * 8),
  524. .key_type_id = 1,
  525. .value_type_id = 2,
  526. .max_entries = 4,
  527. .btf_load_err = true,
  528. .err_str = "Loop detected",
  529. },
  530. /* typedef is _before_ the BTF type of Array_A and Array_B
  531. *
  532. * typedef Array_B int_array;
  533. *
  534. * Array_A <------------------+
  535. * elem_type == int_array |
  536. * | |
  537. * | |
  538. * Array_B <-------- + |
  539. * elem_type == Array_A --+
  540. */
  541. {
  542. .descr = "loop test #2",
  543. .raw_types = {
  544. /* int */
  545. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  546. /* typedef Array_B int_array */
  547. BTF_TYPEDEF_ENC(1, 4), /* [2] */
  548. /* Array_A */
  549. BTF_TYPE_ARRAY_ENC(2, 1, 8), /* [3] */
  550. /* Array_B */
  551. BTF_TYPE_ARRAY_ENC(3, 1, 8), /* [4] */
  552. BTF_END_RAW,
  553. },
  554. .str_sec = "\0int_array\0",
  555. .str_sec_size = sizeof("\0int_array"),
  556. .map_type = BPF_MAP_TYPE_ARRAY,
  557. .map_name = "loop_test2_map",
  558. .key_size = sizeof(int),
  559. .value_size = sizeof(sizeof(int) * 8),
  560. .key_type_id = 1,
  561. .value_type_id = 2,
  562. .max_entries = 4,
  563. .btf_load_err = true,
  564. .err_str = "Loop detected",
  565. },
  566. /* Array_A <------------------+
  567. * elem_type == Array_B |
  568. * | |
  569. * | |
  570. * Array_B <-------- + |
  571. * elem_type == Array_A --+
  572. */
  573. {
  574. .descr = "loop test #3",
  575. .raw_types = {
  576. /* int */ /* [1] */
  577. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  578. /* Array_A */ /* [2] */
  579. BTF_TYPE_ARRAY_ENC(3, 1, 8),
  580. /* Array_B */ /* [3] */
  581. BTF_TYPE_ARRAY_ENC(2, 1, 8),
  582. BTF_END_RAW,
  583. },
  584. .str_sec = "",
  585. .str_sec_size = sizeof(""),
  586. .map_type = BPF_MAP_TYPE_ARRAY,
  587. .map_name = "loop_test3_map",
  588. .key_size = sizeof(int),
  589. .value_size = sizeof(sizeof(int) * 8),
  590. .key_type_id = 1,
  591. .value_type_id = 2,
  592. .max_entries = 4,
  593. .btf_load_err = true,
  594. .err_str = "Loop detected",
  595. },
  596. /* typedef is _between_ the BTF type of Array_A and Array_B
  597. *
  598. * typedef Array_B int_array;
  599. *
  600. * Array_A <------------------+
  601. * elem_type == int_array |
  602. * | |
  603. * | |
  604. * Array_B <-------- + |
  605. * elem_type == Array_A --+
  606. */
  607. {
  608. .descr = "loop test #4",
  609. .raw_types = {
  610. /* int */ /* [1] */
  611. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  612. /* Array_A */ /* [2] */
  613. BTF_TYPE_ARRAY_ENC(3, 1, 8),
  614. /* typedef Array_B int_array */ /* [3] */
  615. BTF_TYPEDEF_ENC(NAME_TBD, 4),
  616. /* Array_B */ /* [4] */
  617. BTF_TYPE_ARRAY_ENC(2, 1, 8),
  618. BTF_END_RAW,
  619. },
  620. .str_sec = "\0int_array\0",
  621. .str_sec_size = sizeof("\0int_array"),
  622. .map_type = BPF_MAP_TYPE_ARRAY,
  623. .map_name = "loop_test4_map",
  624. .key_size = sizeof(int),
  625. .value_size = sizeof(sizeof(int) * 8),
  626. .key_type_id = 1,
  627. .value_type_id = 2,
  628. .max_entries = 4,
  629. .btf_load_err = true,
  630. .err_str = "Loop detected",
  631. },
  632. /* typedef struct B Struct_B
  633. *
  634. * struct A {
  635. * int x;
  636. * Struct_B y;
  637. * };
  638. *
  639. * struct B {
  640. * int x;
  641. * struct A y;
  642. * };
  643. */
  644. {
  645. .descr = "loop test #5",
  646. .raw_types = {
  647. /* int */
  648. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  649. /* struct A */ /* [2] */
  650. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
  651. BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x; */
  652. BTF_MEMBER_ENC(NAME_TBD, 3, 32),/* Struct_B y; */
  653. /* typedef struct B Struct_B */
  654. BTF_TYPEDEF_ENC(NAME_TBD, 4), /* [3] */
  655. /* struct B */ /* [4] */
  656. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
  657. BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x; */
  658. BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A y; */
  659. BTF_END_RAW,
  660. },
  661. .str_sec = "\0A\0x\0y\0Struct_B\0B\0x\0y",
  662. .str_sec_size = sizeof("\0A\0x\0y\0Struct_B\0B\0x\0y"),
  663. .map_type = BPF_MAP_TYPE_ARRAY,
  664. .map_name = "loop_test5_map",
  665. .key_size = sizeof(int),
  666. .value_size = 8,
  667. .key_type_id = 1,
  668. .value_type_id = 2,
  669. .max_entries = 4,
  670. .btf_load_err = true,
  671. .err_str = "Loop detected",
  672. },
  673. /* struct A {
  674. * int x;
  675. * struct A array_a[4];
  676. * };
  677. */
  678. {
  679. .descr = "loop test #6",
  680. .raw_types = {
  681. /* int */
  682. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4), /* [1] */
  683. BTF_TYPE_ARRAY_ENC(3, 1, 4), /* [2] */
  684. /* struct A */ /* [3] */
  685. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 2), 8),
  686. BTF_MEMBER_ENC(NAME_TBD, 1, 0), /* int x; */
  687. BTF_MEMBER_ENC(NAME_TBD, 2, 32),/* struct A array_a[4]; */
  688. BTF_END_RAW,
  689. },
  690. .str_sec = "\0A\0x\0y",
  691. .str_sec_size = sizeof("\0A\0x\0y"),
  692. .map_type = BPF_MAP_TYPE_ARRAY,
  693. .map_name = "loop_test6_map",
  694. .key_size = sizeof(int),
  695. .value_size = 8,
  696. .key_type_id = 1,
  697. .value_type_id = 2,
  698. .max_entries = 4,
  699. .btf_load_err = true,
  700. .err_str = "Loop detected",
  701. },
  702. {
  703. .descr = "loop test #7",
  704. .raw_types = {
  705. /* int */ /* [1] */
  706. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  707. /* struct A { */ /* [2] */
  708. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
  709. /* const void *m; */
  710. BTF_MEMBER_ENC(NAME_TBD, 3, 0),
  711. /* CONST type_id=3 */ /* [3] */
  712. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
  713. /* PTR type_id=2 */ /* [4] */
  714. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 3),
  715. BTF_END_RAW,
  716. },
  717. .str_sec = "\0A\0m",
  718. .str_sec_size = sizeof("\0A\0m"),
  719. .map_type = BPF_MAP_TYPE_ARRAY,
  720. .map_name = "loop_test7_map",
  721. .key_size = sizeof(int),
  722. .value_size = sizeof(void *),
  723. .key_type_id = 1,
  724. .value_type_id = 2,
  725. .max_entries = 4,
  726. .btf_load_err = true,
  727. .err_str = "Loop detected",
  728. },
  729. {
  730. .descr = "loop test #8",
  731. .raw_types = {
  732. /* int */ /* [1] */
  733. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  734. /* struct A { */ /* [2] */
  735. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
  736. /* const void *m; */
  737. BTF_MEMBER_ENC(NAME_TBD, 4, 0),
  738. /* struct B { */ /* [3] */
  739. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 1), sizeof(void *)),
  740. /* const void *n; */
  741. BTF_MEMBER_ENC(NAME_TBD, 6, 0),
  742. /* CONST type_id=5 */ /* [4] */
  743. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 5),
  744. /* PTR type_id=6 */ /* [5] */
  745. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 6),
  746. /* CONST type_id=7 */ /* [6] */
  747. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 7),
  748. /* PTR type_id=4 */ /* [7] */
  749. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 4),
  750. BTF_END_RAW,
  751. },
  752. .str_sec = "\0A\0m\0B\0n",
  753. .str_sec_size = sizeof("\0A\0m\0B\0n"),
  754. .map_type = BPF_MAP_TYPE_ARRAY,
  755. .map_name = "loop_test8_map",
  756. .key_size = sizeof(int),
  757. .value_size = sizeof(void *),
  758. .key_type_id = 1,
  759. .value_type_id = 2,
  760. .max_entries = 4,
  761. .btf_load_err = true,
  762. .err_str = "Loop detected",
  763. },
  764. {
  765. .descr = "string section does not end with null",
  766. .raw_types = {
  767. /* int */ /* [1] */
  768. BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
  769. BTF_END_RAW,
  770. },
  771. .str_sec = "\0int",
  772. .str_sec_size = sizeof("\0int") - 1,
  773. .map_type = BPF_MAP_TYPE_ARRAY,
  774. .map_name = "hdr_test_map",
  775. .key_size = sizeof(int),
  776. .value_size = sizeof(int),
  777. .key_type_id = 1,
  778. .value_type_id = 1,
  779. .max_entries = 4,
  780. .btf_load_err = true,
  781. .err_str = "Invalid string section",
  782. },
  783. {
  784. .descr = "empty string section",
  785. .raw_types = {
  786. /* int */ /* [1] */
  787. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  788. BTF_END_RAW,
  789. },
  790. .str_sec = "",
  791. .str_sec_size = 0,
  792. .map_type = BPF_MAP_TYPE_ARRAY,
  793. .map_name = "hdr_test_map",
  794. .key_size = sizeof(int),
  795. .value_size = sizeof(int),
  796. .key_type_id = 1,
  797. .value_type_id = 1,
  798. .max_entries = 4,
  799. .btf_load_err = true,
  800. .err_str = "Invalid string section",
  801. },
  802. {
  803. .descr = "empty type section",
  804. .raw_types = {
  805. BTF_END_RAW,
  806. },
  807. .str_sec = "\0int",
  808. .str_sec_size = sizeof("\0int"),
  809. .map_type = BPF_MAP_TYPE_ARRAY,
  810. .map_name = "hdr_test_map",
  811. .key_size = sizeof(int),
  812. .value_size = sizeof(int),
  813. .key_type_id = 1,
  814. .value_type_id = 1,
  815. .max_entries = 4,
  816. .btf_load_err = true,
  817. .err_str = "No type found",
  818. },
  819. {
  820. .descr = "btf_header test. Longer hdr_len",
  821. .raw_types = {
  822. /* int */ /* [1] */
  823. BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
  824. BTF_END_RAW,
  825. },
  826. .str_sec = "\0int",
  827. .str_sec_size = sizeof("\0int"),
  828. .map_type = BPF_MAP_TYPE_ARRAY,
  829. .map_name = "hdr_test_map",
  830. .key_size = sizeof(int),
  831. .value_size = sizeof(int),
  832. .key_type_id = 1,
  833. .value_type_id = 1,
  834. .max_entries = 4,
  835. .btf_load_err = true,
  836. .hdr_len_delta = 4,
  837. .err_str = "Unsupported btf_header",
  838. },
  839. {
  840. .descr = "btf_header test. Gap between hdr and type",
  841. .raw_types = {
  842. /* int */ /* [1] */
  843. BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
  844. BTF_END_RAW,
  845. },
  846. .str_sec = "\0int",
  847. .str_sec_size = sizeof("\0int"),
  848. .map_type = BPF_MAP_TYPE_ARRAY,
  849. .map_name = "hdr_test_map",
  850. .key_size = sizeof(int),
  851. .value_size = sizeof(int),
  852. .key_type_id = 1,
  853. .value_type_id = 1,
  854. .max_entries = 4,
  855. .btf_load_err = true,
  856. .type_off_delta = 4,
  857. .err_str = "Unsupported section found",
  858. },
  859. {
  860. .descr = "btf_header test. Gap between type and str",
  861. .raw_types = {
  862. /* int */ /* [1] */
  863. BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
  864. BTF_END_RAW,
  865. },
  866. .str_sec = "\0int",
  867. .str_sec_size = sizeof("\0int"),
  868. .map_type = BPF_MAP_TYPE_ARRAY,
  869. .map_name = "hdr_test_map",
  870. .key_size = sizeof(int),
  871. .value_size = sizeof(int),
  872. .key_type_id = 1,
  873. .value_type_id = 1,
  874. .max_entries = 4,
  875. .btf_load_err = true,
  876. .str_off_delta = 4,
  877. .err_str = "Unsupported section found",
  878. },
  879. {
  880. .descr = "btf_header test. Overlap between type and str",
  881. .raw_types = {
  882. /* int */ /* [1] */
  883. BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
  884. BTF_END_RAW,
  885. },
  886. .str_sec = "\0int",
  887. .str_sec_size = sizeof("\0int"),
  888. .map_type = BPF_MAP_TYPE_ARRAY,
  889. .map_name = "hdr_test_map",
  890. .key_size = sizeof(int),
  891. .value_size = sizeof(int),
  892. .key_type_id = 1,
  893. .value_type_id = 1,
  894. .max_entries = 4,
  895. .btf_load_err = true,
  896. .str_off_delta = -4,
  897. .err_str = "Section overlap found",
  898. },
  899. {
  900. .descr = "btf_header test. Larger BTF size",
  901. .raw_types = {
  902. /* int */ /* [1] */
  903. BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
  904. BTF_END_RAW,
  905. },
  906. .str_sec = "\0int",
  907. .str_sec_size = sizeof("\0int"),
  908. .map_type = BPF_MAP_TYPE_ARRAY,
  909. .map_name = "hdr_test_map",
  910. .key_size = sizeof(int),
  911. .value_size = sizeof(int),
  912. .key_type_id = 1,
  913. .value_type_id = 1,
  914. .max_entries = 4,
  915. .btf_load_err = true,
  916. .str_len_delta = -4,
  917. .err_str = "Unsupported section found",
  918. },
  919. {
  920. .descr = "btf_header test. Smaller BTF size",
  921. .raw_types = {
  922. /* int */ /* [1] */
  923. BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
  924. BTF_END_RAW,
  925. },
  926. .str_sec = "\0int",
  927. .str_sec_size = sizeof("\0int"),
  928. .map_type = BPF_MAP_TYPE_ARRAY,
  929. .map_name = "hdr_test_map",
  930. .key_size = sizeof(int),
  931. .value_size = sizeof(int),
  932. .key_type_id = 1,
  933. .value_type_id = 1,
  934. .max_entries = 4,
  935. .btf_load_err = true,
  936. .str_len_delta = 4,
  937. .err_str = "Total section length too long",
  938. },
  939. {
  940. .descr = "array test. index_type/elem_type \"int\"",
  941. .raw_types = {
  942. /* int */ /* [1] */
  943. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  944. /* int[16] */ /* [2] */
  945. BTF_TYPE_ARRAY_ENC(1, 1, 16),
  946. BTF_END_RAW,
  947. },
  948. .str_sec = "",
  949. .str_sec_size = sizeof(""),
  950. .map_type = BPF_MAP_TYPE_ARRAY,
  951. .map_name = "array_test_map",
  952. .key_size = sizeof(int),
  953. .value_size = sizeof(int),
  954. .key_type_id = 1,
  955. .value_type_id = 1,
  956. .max_entries = 4,
  957. },
  958. {
  959. .descr = "array test. index_type/elem_type \"const int\"",
  960. .raw_types = {
  961. /* int */ /* [1] */
  962. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  963. /* int[16] */ /* [2] */
  964. BTF_TYPE_ARRAY_ENC(3, 3, 16),
  965. /* CONST type_id=1 */ /* [3] */
  966. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 1),
  967. BTF_END_RAW,
  968. },
  969. .str_sec = "",
  970. .str_sec_size = sizeof(""),
  971. .map_type = BPF_MAP_TYPE_ARRAY,
  972. .map_name = "array_test_map",
  973. .key_size = sizeof(int),
  974. .value_size = sizeof(int),
  975. .key_type_id = 1,
  976. .value_type_id = 1,
  977. .max_entries = 4,
  978. },
  979. {
  980. .descr = "array test. index_type \"const int:31\"",
  981. .raw_types = {
  982. /* int */ /* [1] */
  983. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  984. /* int:31 */ /* [2] */
  985. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
  986. /* int[16] */ /* [3] */
  987. BTF_TYPE_ARRAY_ENC(1, 4, 16),
  988. /* CONST type_id=2 */ /* [4] */
  989. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
  990. BTF_END_RAW,
  991. },
  992. .str_sec = "",
  993. .str_sec_size = sizeof(""),
  994. .map_type = BPF_MAP_TYPE_ARRAY,
  995. .map_name = "array_test_map",
  996. .key_size = sizeof(int),
  997. .value_size = sizeof(int),
  998. .key_type_id = 1,
  999. .value_type_id = 1,
  1000. .max_entries = 4,
  1001. .btf_load_err = true,
  1002. .err_str = "Invalid index",
  1003. },
  1004. {
  1005. .descr = "array test. elem_type \"const int:31\"",
  1006. .raw_types = {
  1007. /* int */ /* [1] */
  1008. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1009. /* int:31 */ /* [2] */
  1010. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 31, 4),
  1011. /* int[16] */ /* [3] */
  1012. BTF_TYPE_ARRAY_ENC(4, 1, 16),
  1013. /* CONST type_id=2 */ /* [4] */
  1014. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 2),
  1015. BTF_END_RAW,
  1016. },
  1017. .str_sec = "",
  1018. .str_sec_size = sizeof(""),
  1019. .map_type = BPF_MAP_TYPE_ARRAY,
  1020. .map_name = "array_test_map",
  1021. .key_size = sizeof(int),
  1022. .value_size = sizeof(int),
  1023. .key_type_id = 1,
  1024. .value_type_id = 1,
  1025. .max_entries = 4,
  1026. .btf_load_err = true,
  1027. .err_str = "Invalid array of int",
  1028. },
  1029. {
  1030. .descr = "array test. index_type \"void\"",
  1031. .raw_types = {
  1032. /* int */ /* [1] */
  1033. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1034. /* int[16] */ /* [2] */
  1035. BTF_TYPE_ARRAY_ENC(1, 0, 16),
  1036. BTF_END_RAW,
  1037. },
  1038. .str_sec = "",
  1039. .str_sec_size = sizeof(""),
  1040. .map_type = BPF_MAP_TYPE_ARRAY,
  1041. .map_name = "array_test_map",
  1042. .key_size = sizeof(int),
  1043. .value_size = sizeof(int),
  1044. .key_type_id = 1,
  1045. .value_type_id = 1,
  1046. .max_entries = 4,
  1047. .btf_load_err = true,
  1048. .err_str = "Invalid index",
  1049. },
  1050. {
  1051. .descr = "array test. index_type \"const void\"",
  1052. .raw_types = {
  1053. /* int */ /* [1] */
  1054. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1055. /* int[16] */ /* [2] */
  1056. BTF_TYPE_ARRAY_ENC(1, 3, 16),
  1057. /* CONST type_id=0 (void) */ /* [3] */
  1058. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
  1059. BTF_END_RAW,
  1060. },
  1061. .str_sec = "",
  1062. .str_sec_size = sizeof(""),
  1063. .map_type = BPF_MAP_TYPE_ARRAY,
  1064. .map_name = "array_test_map",
  1065. .key_size = sizeof(int),
  1066. .value_size = sizeof(int),
  1067. .key_type_id = 1,
  1068. .value_type_id = 1,
  1069. .max_entries = 4,
  1070. .btf_load_err = true,
  1071. .err_str = "Invalid index",
  1072. },
  1073. {
  1074. .descr = "array test. elem_type \"const void\"",
  1075. .raw_types = {
  1076. /* int */ /* [1] */
  1077. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1078. /* int[16] */ /* [2] */
  1079. BTF_TYPE_ARRAY_ENC(3, 1, 16),
  1080. /* CONST type_id=0 (void) */ /* [3] */
  1081. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 0),
  1082. BTF_END_RAW,
  1083. },
  1084. .str_sec = "",
  1085. .str_sec_size = sizeof(""),
  1086. .map_type = BPF_MAP_TYPE_ARRAY,
  1087. .map_name = "array_test_map",
  1088. .key_size = sizeof(int),
  1089. .value_size = sizeof(int),
  1090. .key_type_id = 1,
  1091. .value_type_id = 1,
  1092. .max_entries = 4,
  1093. .btf_load_err = true,
  1094. .err_str = "Invalid elem",
  1095. },
  1096. {
  1097. .descr = "array test. elem_type \"const void *\"",
  1098. .raw_types = {
  1099. /* int */ /* [1] */
  1100. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1101. /* const void *[16] */ /* [2] */
  1102. BTF_TYPE_ARRAY_ENC(3, 1, 16),
  1103. /* CONST type_id=4 */ /* [3] */
  1104. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
  1105. /* void* */ /* [4] */
  1106. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
  1107. BTF_END_RAW,
  1108. },
  1109. .str_sec = "",
  1110. .str_sec_size = sizeof(""),
  1111. .map_type = BPF_MAP_TYPE_ARRAY,
  1112. .map_name = "array_test_map",
  1113. .key_size = sizeof(int),
  1114. .value_size = sizeof(int),
  1115. .key_type_id = 1,
  1116. .value_type_id = 1,
  1117. .max_entries = 4,
  1118. },
  1119. {
  1120. .descr = "array test. index_type \"const void *\"",
  1121. .raw_types = {
  1122. /* int */ /* [1] */
  1123. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1124. /* const void *[16] */ /* [2] */
  1125. BTF_TYPE_ARRAY_ENC(3, 3, 16),
  1126. /* CONST type_id=4 */ /* [3] */
  1127. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_CONST, 0, 0), 4),
  1128. /* void* */ /* [4] */
  1129. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_PTR, 0, 0), 0),
  1130. BTF_END_RAW,
  1131. },
  1132. .str_sec = "",
  1133. .str_sec_size = sizeof(""),
  1134. .map_type = BPF_MAP_TYPE_ARRAY,
  1135. .map_name = "array_test_map",
  1136. .key_size = sizeof(int),
  1137. .value_size = sizeof(int),
  1138. .key_type_id = 1,
  1139. .value_type_id = 1,
  1140. .max_entries = 4,
  1141. .btf_load_err = true,
  1142. .err_str = "Invalid index",
  1143. },
  1144. {
  1145. .descr = "array test. t->size != 0\"",
  1146. .raw_types = {
  1147. /* int */ /* [1] */
  1148. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1149. /* int[16] */ /* [2] */
  1150. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ARRAY, 0, 0), 1),
  1151. BTF_ARRAY_ENC(1, 1, 16),
  1152. BTF_END_RAW,
  1153. },
  1154. .str_sec = "",
  1155. .str_sec_size = sizeof(""),
  1156. .map_type = BPF_MAP_TYPE_ARRAY,
  1157. .map_name = "array_test_map",
  1158. .key_size = sizeof(int),
  1159. .value_size = sizeof(int),
  1160. .key_type_id = 1,
  1161. .value_type_id = 1,
  1162. .max_entries = 4,
  1163. .btf_load_err = true,
  1164. .err_str = "size != 0",
  1165. },
  1166. {
  1167. .descr = "int test. invalid int_data",
  1168. .raw_types = {
  1169. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_INT, 0, 0), 4),
  1170. 0x10000000,
  1171. BTF_END_RAW,
  1172. },
  1173. .str_sec = "",
  1174. .str_sec_size = sizeof(""),
  1175. .map_type = BPF_MAP_TYPE_ARRAY,
  1176. .map_name = "array_test_map",
  1177. .key_size = sizeof(int),
  1178. .value_size = sizeof(int),
  1179. .key_type_id = 1,
  1180. .value_type_id = 1,
  1181. .max_entries = 4,
  1182. .btf_load_err = true,
  1183. .err_str = "Invalid int_data",
  1184. },
  1185. {
  1186. .descr = "invalid BTF_INFO",
  1187. .raw_types = {
  1188. /* int */ /* [1] */
  1189. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1190. BTF_TYPE_ENC(0, 0x10000000, 4),
  1191. BTF_END_RAW,
  1192. },
  1193. .str_sec = "",
  1194. .str_sec_size = sizeof(""),
  1195. .map_type = BPF_MAP_TYPE_ARRAY,
  1196. .map_name = "array_test_map",
  1197. .key_size = sizeof(int),
  1198. .value_size = sizeof(int),
  1199. .key_type_id = 1,
  1200. .value_type_id = 1,
  1201. .max_entries = 4,
  1202. .btf_load_err = true,
  1203. .err_str = "Invalid btf_info",
  1204. },
  1205. {
  1206. .descr = "fwd test. t->type != 0\"",
  1207. .raw_types = {
  1208. /* int */ /* [1] */
  1209. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1210. /* fwd type */ /* [2] */
  1211. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_FWD, 0, 0), 1),
  1212. BTF_END_RAW,
  1213. },
  1214. .str_sec = "",
  1215. .str_sec_size = sizeof(""),
  1216. .map_type = BPF_MAP_TYPE_ARRAY,
  1217. .map_name = "fwd_test_map",
  1218. .key_size = sizeof(int),
  1219. .value_size = sizeof(int),
  1220. .key_type_id = 1,
  1221. .value_type_id = 1,
  1222. .max_entries = 4,
  1223. .btf_load_err = true,
  1224. .err_str = "type != 0",
  1225. },
  1226. {
  1227. .descr = "arraymap invalid btf key (a bit field)",
  1228. .raw_types = {
  1229. /* int */ /* [1] */
  1230. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1231. /* 32 bit int with 32 bit offset */ /* [2] */
  1232. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 32, 32, 8),
  1233. BTF_END_RAW,
  1234. },
  1235. .str_sec = "",
  1236. .str_sec_size = sizeof(""),
  1237. .map_type = BPF_MAP_TYPE_ARRAY,
  1238. .map_name = "array_map_check_btf",
  1239. .key_size = sizeof(int),
  1240. .value_size = sizeof(int),
  1241. .key_type_id = 2,
  1242. .value_type_id = 1,
  1243. .max_entries = 4,
  1244. .map_create_err = true,
  1245. },
  1246. {
  1247. .descr = "arraymap invalid btf key (!= 32 bits)",
  1248. .raw_types = {
  1249. /* int */ /* [1] */
  1250. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1251. /* 16 bit int with 0 bit offset */ /* [2] */
  1252. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 16, 2),
  1253. BTF_END_RAW,
  1254. },
  1255. .str_sec = "",
  1256. .str_sec_size = sizeof(""),
  1257. .map_type = BPF_MAP_TYPE_ARRAY,
  1258. .map_name = "array_map_check_btf",
  1259. .key_size = sizeof(int),
  1260. .value_size = sizeof(int),
  1261. .key_type_id = 2,
  1262. .value_type_id = 1,
  1263. .max_entries = 4,
  1264. .map_create_err = true,
  1265. },
  1266. {
  1267. .descr = "arraymap invalid btf value (too small)",
  1268. .raw_types = {
  1269. /* int */ /* [1] */
  1270. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1271. BTF_END_RAW,
  1272. },
  1273. .str_sec = "",
  1274. .str_sec_size = sizeof(""),
  1275. .map_type = BPF_MAP_TYPE_ARRAY,
  1276. .map_name = "array_map_check_btf",
  1277. .key_size = sizeof(int),
  1278. /* btf_value_size < map->value_size */
  1279. .value_size = sizeof(__u64),
  1280. .key_type_id = 1,
  1281. .value_type_id = 1,
  1282. .max_entries = 4,
  1283. .map_create_err = true,
  1284. },
  1285. {
  1286. .descr = "arraymap invalid btf value (too big)",
  1287. .raw_types = {
  1288. /* int */ /* [1] */
  1289. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1290. BTF_END_RAW,
  1291. },
  1292. .str_sec = "",
  1293. .str_sec_size = sizeof(""),
  1294. .map_type = BPF_MAP_TYPE_ARRAY,
  1295. .map_name = "array_map_check_btf",
  1296. .key_size = sizeof(int),
  1297. /* btf_value_size > map->value_size */
  1298. .value_size = sizeof(__u16),
  1299. .key_type_id = 1,
  1300. .value_type_id = 1,
  1301. .max_entries = 4,
  1302. .map_create_err = true,
  1303. },
  1304. }; /* struct btf_raw_test raw_tests[] */
  1305. static const char *get_next_str(const char *start, const char *end)
  1306. {
  1307. return start < end - 1 ? start + 1 : NULL;
  1308. }
  1309. static int get_type_sec_size(const __u32 *raw_types)
  1310. {
  1311. int i;
  1312. for (i = MAX_NR_RAW_TYPES - 1;
  1313. i >= 0 && raw_types[i] != BTF_END_RAW;
  1314. i--)
  1315. ;
  1316. return i < 0 ? i : i * sizeof(raw_types[0]);
  1317. }
  1318. static void *btf_raw_create(const struct btf_header *hdr,
  1319. const __u32 *raw_types,
  1320. const char *str,
  1321. unsigned int str_sec_size,
  1322. unsigned int *btf_size)
  1323. {
  1324. const char *next_str = str, *end_str = str + str_sec_size;
  1325. unsigned int size_needed, offset;
  1326. struct btf_header *ret_hdr;
  1327. int i, type_sec_size;
  1328. uint32_t *ret_types;
  1329. void *raw_btf;
  1330. type_sec_size = get_type_sec_size(raw_types);
  1331. if (CHECK(type_sec_size < 0, "Cannot get nr_raw_types"))
  1332. return NULL;
  1333. size_needed = sizeof(*hdr) + type_sec_size + str_sec_size;
  1334. raw_btf = malloc(size_needed);
  1335. if (CHECK(!raw_btf, "Cannot allocate memory for raw_btf"))
  1336. return NULL;
  1337. /* Copy header */
  1338. memcpy(raw_btf, hdr, sizeof(*hdr));
  1339. offset = sizeof(*hdr);
  1340. /* Copy type section */
  1341. ret_types = raw_btf + offset;
  1342. for (i = 0; i < type_sec_size / sizeof(raw_types[0]); i++) {
  1343. if (raw_types[i] == NAME_TBD) {
  1344. next_str = get_next_str(next_str, end_str);
  1345. if (CHECK(!next_str, "Error in getting next_str")) {
  1346. free(raw_btf);
  1347. return NULL;
  1348. }
  1349. ret_types[i] = next_str - str;
  1350. next_str += strlen(next_str);
  1351. } else {
  1352. ret_types[i] = raw_types[i];
  1353. }
  1354. }
  1355. offset += type_sec_size;
  1356. /* Copy string section */
  1357. memcpy(raw_btf + offset, str, str_sec_size);
  1358. ret_hdr = (struct btf_header *)raw_btf;
  1359. ret_hdr->type_len = type_sec_size;
  1360. ret_hdr->str_off = type_sec_size;
  1361. ret_hdr->str_len = str_sec_size;
  1362. *btf_size = size_needed;
  1363. return raw_btf;
  1364. }
  1365. static int do_test_raw(unsigned int test_num)
  1366. {
  1367. struct btf_raw_test *test = &raw_tests[test_num - 1];
  1368. struct bpf_create_map_attr create_attr = {};
  1369. int map_fd = -1, btf_fd = -1;
  1370. unsigned int raw_btf_size;
  1371. struct btf_header *hdr;
  1372. void *raw_btf;
  1373. int err;
  1374. fprintf(stderr, "BTF raw test[%u] (%s): ", test_num, test->descr);
  1375. raw_btf = btf_raw_create(&hdr_tmpl,
  1376. test->raw_types,
  1377. test->str_sec,
  1378. test->str_sec_size,
  1379. &raw_btf_size);
  1380. if (!raw_btf)
  1381. return -1;
  1382. hdr = raw_btf;
  1383. hdr->hdr_len = (int)hdr->hdr_len + test->hdr_len_delta;
  1384. hdr->type_off = (int)hdr->type_off + test->type_off_delta;
  1385. hdr->str_off = (int)hdr->str_off + test->str_off_delta;
  1386. hdr->str_len = (int)hdr->str_len + test->str_len_delta;
  1387. *btf_log_buf = '\0';
  1388. btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
  1389. btf_log_buf, BTF_LOG_BUF_SIZE,
  1390. args.always_log);
  1391. free(raw_btf);
  1392. err = ((btf_fd == -1) != test->btf_load_err);
  1393. if (CHECK(err, "btf_fd:%d test->btf_load_err:%u",
  1394. btf_fd, test->btf_load_err) ||
  1395. CHECK(test->err_str && !strstr(btf_log_buf, test->err_str),
  1396. "expected err_str:%s", test->err_str)) {
  1397. err = -1;
  1398. goto done;
  1399. }
  1400. if (err || btf_fd == -1)
  1401. goto done;
  1402. create_attr.name = test->map_name;
  1403. create_attr.map_type = test->map_type;
  1404. create_attr.key_size = test->key_size;
  1405. create_attr.value_size = test->value_size;
  1406. create_attr.max_entries = test->max_entries;
  1407. create_attr.btf_fd = btf_fd;
  1408. create_attr.btf_key_type_id = test->key_type_id;
  1409. create_attr.btf_value_type_id = test->value_type_id;
  1410. map_fd = bpf_create_map_xattr(&create_attr);
  1411. err = ((map_fd == -1) != test->map_create_err);
  1412. CHECK(err, "map_fd:%d test->map_create_err:%u",
  1413. map_fd, test->map_create_err);
  1414. done:
  1415. if (!err)
  1416. fprintf(stderr, "OK");
  1417. if (*btf_log_buf && (err || args.always_log))
  1418. fprintf(stderr, "\n%s", btf_log_buf);
  1419. if (btf_fd != -1)
  1420. close(btf_fd);
  1421. if (map_fd != -1)
  1422. close(map_fd);
  1423. return err;
  1424. }
  1425. static int test_raw(void)
  1426. {
  1427. unsigned int i;
  1428. int err = 0;
  1429. if (args.raw_test_num)
  1430. return count_result(do_test_raw(args.raw_test_num));
  1431. for (i = 1; i <= ARRAY_SIZE(raw_tests); i++)
  1432. err |= count_result(do_test_raw(i));
  1433. return err;
  1434. }
  1435. struct btf_get_info_test {
  1436. const char *descr;
  1437. const char *str_sec;
  1438. __u32 raw_types[MAX_NR_RAW_TYPES];
  1439. __u32 str_sec_size;
  1440. int btf_size_delta;
  1441. int (*special_test)(unsigned int test_num);
  1442. };
  1443. static int test_big_btf_info(unsigned int test_num);
  1444. static int test_btf_id(unsigned int test_num);
  1445. const struct btf_get_info_test get_info_tests[] = {
  1446. {
  1447. .descr = "== raw_btf_size+1",
  1448. .raw_types = {
  1449. /* int */ /* [1] */
  1450. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1451. BTF_END_RAW,
  1452. },
  1453. .str_sec = "",
  1454. .str_sec_size = sizeof(""),
  1455. .btf_size_delta = 1,
  1456. },
  1457. {
  1458. .descr = "== raw_btf_size-3",
  1459. .raw_types = {
  1460. /* int */ /* [1] */
  1461. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1462. BTF_END_RAW,
  1463. },
  1464. .str_sec = "",
  1465. .str_sec_size = sizeof(""),
  1466. .btf_size_delta = -3,
  1467. },
  1468. {
  1469. .descr = "Large bpf_btf_info",
  1470. .raw_types = {
  1471. /* int */ /* [1] */
  1472. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1473. BTF_END_RAW,
  1474. },
  1475. .str_sec = "",
  1476. .str_sec_size = sizeof(""),
  1477. .special_test = test_big_btf_info,
  1478. },
  1479. {
  1480. .descr = "BTF ID",
  1481. .raw_types = {
  1482. /* int */ /* [1] */
  1483. BTF_TYPE_INT_ENC(0, BTF_INT_SIGNED, 0, 32, 4),
  1484. /* unsigned int */ /* [2] */
  1485. BTF_TYPE_INT_ENC(0, 0, 0, 32, 4),
  1486. BTF_END_RAW,
  1487. },
  1488. .str_sec = "",
  1489. .str_sec_size = sizeof(""),
  1490. .special_test = test_btf_id,
  1491. },
  1492. };
  1493. static inline __u64 ptr_to_u64(const void *ptr)
  1494. {
  1495. return (__u64)(unsigned long)ptr;
  1496. }
  1497. static int test_big_btf_info(unsigned int test_num)
  1498. {
  1499. const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
  1500. uint8_t *raw_btf = NULL, *user_btf = NULL;
  1501. unsigned int raw_btf_size;
  1502. struct {
  1503. struct bpf_btf_info info;
  1504. uint64_t garbage;
  1505. } info_garbage;
  1506. struct bpf_btf_info *info;
  1507. int btf_fd = -1, err;
  1508. uint32_t info_len;
  1509. raw_btf = btf_raw_create(&hdr_tmpl,
  1510. test->raw_types,
  1511. test->str_sec,
  1512. test->str_sec_size,
  1513. &raw_btf_size);
  1514. if (!raw_btf)
  1515. return -1;
  1516. *btf_log_buf = '\0';
  1517. user_btf = malloc(raw_btf_size);
  1518. if (CHECK(!user_btf, "!user_btf")) {
  1519. err = -1;
  1520. goto done;
  1521. }
  1522. btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
  1523. btf_log_buf, BTF_LOG_BUF_SIZE,
  1524. args.always_log);
  1525. if (CHECK(btf_fd == -1, "errno:%d", errno)) {
  1526. err = -1;
  1527. goto done;
  1528. }
  1529. /*
  1530. * GET_INFO should error out if the userspace info
  1531. * has non zero tailing bytes.
  1532. */
  1533. info = &info_garbage.info;
  1534. memset(info, 0, sizeof(*info));
  1535. info_garbage.garbage = 0xdeadbeef;
  1536. info_len = sizeof(info_garbage);
  1537. info->btf = ptr_to_u64(user_btf);
  1538. info->btf_size = raw_btf_size;
  1539. err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
  1540. if (CHECK(!err, "!err")) {
  1541. err = -1;
  1542. goto done;
  1543. }
  1544. /*
  1545. * GET_INFO should succeed even info_len is larger than
  1546. * the kernel supported as long as tailing bytes are zero.
  1547. * The kernel supported info len should also be returned
  1548. * to userspace.
  1549. */
  1550. info_garbage.garbage = 0;
  1551. err = bpf_obj_get_info_by_fd(btf_fd, info, &info_len);
  1552. if (CHECK(err || info_len != sizeof(*info),
  1553. "err:%d errno:%d info_len:%u sizeof(*info):%lu",
  1554. err, errno, info_len, sizeof(*info))) {
  1555. err = -1;
  1556. goto done;
  1557. }
  1558. fprintf(stderr, "OK");
  1559. done:
  1560. if (*btf_log_buf && (err || args.always_log))
  1561. fprintf(stderr, "\n%s", btf_log_buf);
  1562. free(raw_btf);
  1563. free(user_btf);
  1564. if (btf_fd != -1)
  1565. close(btf_fd);
  1566. return err;
  1567. }
  1568. static int test_btf_id(unsigned int test_num)
  1569. {
  1570. const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
  1571. struct bpf_create_map_attr create_attr = {};
  1572. uint8_t *raw_btf = NULL, *user_btf[2] = {};
  1573. int btf_fd[2] = {-1, -1}, map_fd = -1;
  1574. struct bpf_map_info map_info = {};
  1575. struct bpf_btf_info info[2] = {};
  1576. unsigned int raw_btf_size;
  1577. uint32_t info_len;
  1578. int err, i, ret;
  1579. raw_btf = btf_raw_create(&hdr_tmpl,
  1580. test->raw_types,
  1581. test->str_sec,
  1582. test->str_sec_size,
  1583. &raw_btf_size);
  1584. if (!raw_btf)
  1585. return -1;
  1586. *btf_log_buf = '\0';
  1587. for (i = 0; i < 2; i++) {
  1588. user_btf[i] = malloc(raw_btf_size);
  1589. if (CHECK(!user_btf[i], "!user_btf[%d]", i)) {
  1590. err = -1;
  1591. goto done;
  1592. }
  1593. info[i].btf = ptr_to_u64(user_btf[i]);
  1594. info[i].btf_size = raw_btf_size;
  1595. }
  1596. btf_fd[0] = bpf_load_btf(raw_btf, raw_btf_size,
  1597. btf_log_buf, BTF_LOG_BUF_SIZE,
  1598. args.always_log);
  1599. if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
  1600. err = -1;
  1601. goto done;
  1602. }
  1603. /* Test BPF_OBJ_GET_INFO_BY_ID on btf_id */
  1604. info_len = sizeof(info[0]);
  1605. err = bpf_obj_get_info_by_fd(btf_fd[0], &info[0], &info_len);
  1606. if (CHECK(err, "errno:%d", errno)) {
  1607. err = -1;
  1608. goto done;
  1609. }
  1610. btf_fd[1] = bpf_btf_get_fd_by_id(info[0].id);
  1611. if (CHECK(btf_fd[1] == -1, "errno:%d", errno)) {
  1612. err = -1;
  1613. goto done;
  1614. }
  1615. ret = 0;
  1616. err = bpf_obj_get_info_by_fd(btf_fd[1], &info[1], &info_len);
  1617. if (CHECK(err || info[0].id != info[1].id ||
  1618. info[0].btf_size != info[1].btf_size ||
  1619. (ret = memcmp(user_btf[0], user_btf[1], info[0].btf_size)),
  1620. "err:%d errno:%d id0:%u id1:%u btf_size0:%u btf_size1:%u memcmp:%d",
  1621. err, errno, info[0].id, info[1].id,
  1622. info[0].btf_size, info[1].btf_size, ret)) {
  1623. err = -1;
  1624. goto done;
  1625. }
  1626. /* Test btf members in struct bpf_map_info */
  1627. create_attr.name = "test_btf_id";
  1628. create_attr.map_type = BPF_MAP_TYPE_ARRAY;
  1629. create_attr.key_size = sizeof(int);
  1630. create_attr.value_size = sizeof(unsigned int);
  1631. create_attr.max_entries = 4;
  1632. create_attr.btf_fd = btf_fd[0];
  1633. create_attr.btf_key_type_id = 1;
  1634. create_attr.btf_value_type_id = 2;
  1635. map_fd = bpf_create_map_xattr(&create_attr);
  1636. if (CHECK(map_fd == -1, "errno:%d", errno)) {
  1637. err = -1;
  1638. goto done;
  1639. }
  1640. info_len = sizeof(map_info);
  1641. err = bpf_obj_get_info_by_fd(map_fd, &map_info, &info_len);
  1642. if (CHECK(err || map_info.btf_id != info[0].id ||
  1643. map_info.btf_key_type_id != 1 || map_info.btf_value_type_id != 2,
  1644. "err:%d errno:%d info.id:%u btf_id:%u btf_key_type_id:%u btf_value_type_id:%u",
  1645. err, errno, info[0].id, map_info.btf_id, map_info.btf_key_type_id,
  1646. map_info.btf_value_type_id)) {
  1647. err = -1;
  1648. goto done;
  1649. }
  1650. for (i = 0; i < 2; i++) {
  1651. close(btf_fd[i]);
  1652. btf_fd[i] = -1;
  1653. }
  1654. /* Test BTF ID is removed from the kernel */
  1655. btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
  1656. if (CHECK(btf_fd[0] == -1, "errno:%d", errno)) {
  1657. err = -1;
  1658. goto done;
  1659. }
  1660. close(btf_fd[0]);
  1661. btf_fd[0] = -1;
  1662. /* The map holds the last ref to BTF and its btf_id */
  1663. close(map_fd);
  1664. map_fd = -1;
  1665. btf_fd[0] = bpf_btf_get_fd_by_id(map_info.btf_id);
  1666. if (CHECK(btf_fd[0] != -1, "BTF lingers")) {
  1667. err = -1;
  1668. goto done;
  1669. }
  1670. fprintf(stderr, "OK");
  1671. done:
  1672. if (*btf_log_buf && (err || args.always_log))
  1673. fprintf(stderr, "\n%s", btf_log_buf);
  1674. free(raw_btf);
  1675. if (map_fd != -1)
  1676. close(map_fd);
  1677. for (i = 0; i < 2; i++) {
  1678. free(user_btf[i]);
  1679. if (btf_fd[i] != -1)
  1680. close(btf_fd[i]);
  1681. }
  1682. return err;
  1683. }
  1684. static int do_test_get_info(unsigned int test_num)
  1685. {
  1686. const struct btf_get_info_test *test = &get_info_tests[test_num - 1];
  1687. unsigned int raw_btf_size, user_btf_size, expected_nbytes;
  1688. uint8_t *raw_btf = NULL, *user_btf = NULL;
  1689. struct bpf_btf_info info = {};
  1690. int btf_fd = -1, err, ret;
  1691. uint32_t info_len;
  1692. fprintf(stderr, "BTF GET_INFO test[%u] (%s): ",
  1693. test_num, test->descr);
  1694. if (test->special_test)
  1695. return test->special_test(test_num);
  1696. raw_btf = btf_raw_create(&hdr_tmpl,
  1697. test->raw_types,
  1698. test->str_sec,
  1699. test->str_sec_size,
  1700. &raw_btf_size);
  1701. if (!raw_btf)
  1702. return -1;
  1703. *btf_log_buf = '\0';
  1704. user_btf = malloc(raw_btf_size);
  1705. if (CHECK(!user_btf, "!user_btf")) {
  1706. err = -1;
  1707. goto done;
  1708. }
  1709. btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
  1710. btf_log_buf, BTF_LOG_BUF_SIZE,
  1711. args.always_log);
  1712. if (CHECK(btf_fd == -1, "errno:%d", errno)) {
  1713. err = -1;
  1714. goto done;
  1715. }
  1716. user_btf_size = (int)raw_btf_size + test->btf_size_delta;
  1717. expected_nbytes = min(raw_btf_size, user_btf_size);
  1718. if (raw_btf_size > expected_nbytes)
  1719. memset(user_btf + expected_nbytes, 0xff,
  1720. raw_btf_size - expected_nbytes);
  1721. info_len = sizeof(info);
  1722. info.btf = ptr_to_u64(user_btf);
  1723. info.btf_size = user_btf_size;
  1724. ret = 0;
  1725. err = bpf_obj_get_info_by_fd(btf_fd, &info, &info_len);
  1726. if (CHECK(err || !info.id || info_len != sizeof(info) ||
  1727. info.btf_size != raw_btf_size ||
  1728. (ret = memcmp(raw_btf, user_btf, expected_nbytes)),
  1729. "err:%d errno:%d info.id:%u info_len:%u sizeof(info):%lu raw_btf_size:%u info.btf_size:%u expected_nbytes:%u memcmp:%d",
  1730. err, errno, info.id, info_len, sizeof(info),
  1731. raw_btf_size, info.btf_size, expected_nbytes, ret)) {
  1732. err = -1;
  1733. goto done;
  1734. }
  1735. while (expected_nbytes < raw_btf_size) {
  1736. fprintf(stderr, "%u...", expected_nbytes);
  1737. if (CHECK(user_btf[expected_nbytes++] != 0xff,
  1738. "user_btf[%u]:%x != 0xff", expected_nbytes - 1,
  1739. user_btf[expected_nbytes - 1])) {
  1740. err = -1;
  1741. goto done;
  1742. }
  1743. }
  1744. fprintf(stderr, "OK");
  1745. done:
  1746. if (*btf_log_buf && (err || args.always_log))
  1747. fprintf(stderr, "\n%s", btf_log_buf);
  1748. free(raw_btf);
  1749. free(user_btf);
  1750. if (btf_fd != -1)
  1751. close(btf_fd);
  1752. return err;
  1753. }
  1754. static int test_get_info(void)
  1755. {
  1756. unsigned int i;
  1757. int err = 0;
  1758. if (args.get_info_test_num)
  1759. return count_result(do_test_get_info(args.get_info_test_num));
  1760. for (i = 1; i <= ARRAY_SIZE(get_info_tests); i++)
  1761. err |= count_result(do_test_get_info(i));
  1762. return err;
  1763. }
  1764. struct btf_file_test {
  1765. const char *file;
  1766. bool btf_kv_notfound;
  1767. };
  1768. static struct btf_file_test file_tests[] = {
  1769. {
  1770. .file = "test_btf_haskv.o",
  1771. },
  1772. {
  1773. .file = "test_btf_nokv.o",
  1774. .btf_kv_notfound = true,
  1775. },
  1776. };
  1777. static int file_has_btf_elf(const char *fn)
  1778. {
  1779. Elf_Scn *scn = NULL;
  1780. GElf_Ehdr ehdr;
  1781. int elf_fd;
  1782. Elf *elf;
  1783. int ret;
  1784. if (CHECK(elf_version(EV_CURRENT) == EV_NONE,
  1785. "elf_version(EV_CURRENT) == EV_NONE"))
  1786. return -1;
  1787. elf_fd = open(fn, O_RDONLY);
  1788. if (CHECK(elf_fd == -1, "open(%s): errno:%d", fn, errno))
  1789. return -1;
  1790. elf = elf_begin(elf_fd, ELF_C_READ, NULL);
  1791. if (CHECK(!elf, "elf_begin(%s): %s", fn, elf_errmsg(elf_errno()))) {
  1792. ret = -1;
  1793. goto done;
  1794. }
  1795. if (CHECK(!gelf_getehdr(elf, &ehdr), "!gelf_getehdr(%s)", fn)) {
  1796. ret = -1;
  1797. goto done;
  1798. }
  1799. while ((scn = elf_nextscn(elf, scn))) {
  1800. const char *sh_name;
  1801. GElf_Shdr sh;
  1802. if (CHECK(gelf_getshdr(scn, &sh) != &sh,
  1803. "file:%s gelf_getshdr != &sh", fn)) {
  1804. ret = -1;
  1805. goto done;
  1806. }
  1807. sh_name = elf_strptr(elf, ehdr.e_shstrndx, sh.sh_name);
  1808. if (!strcmp(sh_name, BTF_ELF_SEC)) {
  1809. ret = 1;
  1810. goto done;
  1811. }
  1812. }
  1813. ret = 0;
  1814. done:
  1815. close(elf_fd);
  1816. elf_end(elf);
  1817. return ret;
  1818. }
  1819. static int do_test_file(unsigned int test_num)
  1820. {
  1821. const struct btf_file_test *test = &file_tests[test_num - 1];
  1822. struct bpf_object *obj = NULL;
  1823. struct bpf_program *prog;
  1824. struct bpf_map *map;
  1825. int err;
  1826. fprintf(stderr, "BTF libbpf test[%u] (%s): ", test_num,
  1827. test->file);
  1828. err = file_has_btf_elf(test->file);
  1829. if (err == -1)
  1830. return err;
  1831. if (err == 0) {
  1832. fprintf(stderr, "SKIP. No ELF %s found", BTF_ELF_SEC);
  1833. skip_cnt++;
  1834. return 0;
  1835. }
  1836. obj = bpf_object__open(test->file);
  1837. if (CHECK(IS_ERR(obj), "obj: %ld", PTR_ERR(obj)))
  1838. return PTR_ERR(obj);
  1839. err = bpf_object__btf_fd(obj);
  1840. if (CHECK(err == -1, "bpf_object__btf_fd: -1"))
  1841. goto done;
  1842. prog = bpf_program__next(NULL, obj);
  1843. if (CHECK(!prog, "Cannot find bpf_prog")) {
  1844. err = -1;
  1845. goto done;
  1846. }
  1847. bpf_program__set_type(prog, BPF_PROG_TYPE_TRACEPOINT);
  1848. err = bpf_object__load(obj);
  1849. if (CHECK(err < 0, "bpf_object__load: %d", err))
  1850. goto done;
  1851. map = bpf_object__find_map_by_name(obj, "btf_map");
  1852. if (CHECK(!map, "btf_map not found")) {
  1853. err = -1;
  1854. goto done;
  1855. }
  1856. err = (bpf_map__btf_key_type_id(map) == 0 || bpf_map__btf_value_type_id(map) == 0)
  1857. != test->btf_kv_notfound;
  1858. if (CHECK(err, "btf_key_type_id:%u btf_value_type_id:%u test->btf_kv_notfound:%u",
  1859. bpf_map__btf_key_type_id(map), bpf_map__btf_value_type_id(map),
  1860. test->btf_kv_notfound))
  1861. goto done;
  1862. fprintf(stderr, "OK");
  1863. done:
  1864. bpf_object__close(obj);
  1865. return err;
  1866. }
  1867. static int test_file(void)
  1868. {
  1869. unsigned int i;
  1870. int err = 0;
  1871. if (args.file_test_num)
  1872. return count_result(do_test_file(args.file_test_num));
  1873. for (i = 1; i <= ARRAY_SIZE(file_tests); i++)
  1874. err |= count_result(do_test_file(i));
  1875. return err;
  1876. }
  1877. const char *pprint_enum_str[] = {
  1878. "ENUM_ZERO",
  1879. "ENUM_ONE",
  1880. "ENUM_TWO",
  1881. "ENUM_THREE",
  1882. };
  1883. struct pprint_mapv {
  1884. uint32_t ui32;
  1885. uint16_t ui16;
  1886. /* 2 bytes hole */
  1887. int32_t si32;
  1888. uint32_t unused_bits2a:2,
  1889. bits28:28,
  1890. unused_bits2b:2;
  1891. union {
  1892. uint64_t ui64;
  1893. uint8_t ui8a[8];
  1894. };
  1895. enum {
  1896. ENUM_ZERO,
  1897. ENUM_ONE,
  1898. ENUM_TWO,
  1899. ENUM_THREE,
  1900. } aenum;
  1901. };
  1902. static struct btf_raw_test pprint_test_template = {
  1903. .raw_types = {
  1904. /* unsighed char */ /* [1] */
  1905. BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 8, 1),
  1906. /* unsigned short */ /* [2] */
  1907. BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 16, 2),
  1908. /* unsigned int */ /* [3] */
  1909. BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 32, 4),
  1910. /* int */ /* [4] */
  1911. BTF_TYPE_INT_ENC(NAME_TBD, BTF_INT_SIGNED, 0, 32, 4),
  1912. /* unsigned long long */ /* [5] */
  1913. BTF_TYPE_INT_ENC(NAME_TBD, 0, 0, 64, 8),
  1914. /* 2 bits */ /* [6] */
  1915. BTF_TYPE_INT_ENC(0, 0, 0, 2, 2),
  1916. /* 28 bits */ /* [7] */
  1917. BTF_TYPE_INT_ENC(0, 0, 0, 28, 4),
  1918. /* uint8_t[8] */ /* [8] */
  1919. BTF_TYPE_ARRAY_ENC(9, 1, 8),
  1920. /* typedef unsigned char uint8_t */ /* [9] */
  1921. BTF_TYPEDEF_ENC(NAME_TBD, 1),
  1922. /* typedef unsigned short uint16_t */ /* [10] */
  1923. BTF_TYPEDEF_ENC(NAME_TBD, 2),
  1924. /* typedef unsigned int uint32_t */ /* [11] */
  1925. BTF_TYPEDEF_ENC(NAME_TBD, 3),
  1926. /* typedef int int32_t */ /* [12] */
  1927. BTF_TYPEDEF_ENC(NAME_TBD, 4),
  1928. /* typedef unsigned long long uint64_t *//* [13] */
  1929. BTF_TYPEDEF_ENC(NAME_TBD, 5),
  1930. /* union (anon) */ /* [14] */
  1931. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_UNION, 0, 2), 8),
  1932. BTF_MEMBER_ENC(NAME_TBD, 13, 0),/* uint64_t ui64; */
  1933. BTF_MEMBER_ENC(NAME_TBD, 8, 0), /* uint8_t ui8a[8]; */
  1934. /* enum (anon) */ /* [15] */
  1935. BTF_TYPE_ENC(0, BTF_INFO_ENC(BTF_KIND_ENUM, 0, 4), 4),
  1936. BTF_ENUM_ENC(NAME_TBD, 0),
  1937. BTF_ENUM_ENC(NAME_TBD, 1),
  1938. BTF_ENUM_ENC(NAME_TBD, 2),
  1939. BTF_ENUM_ENC(NAME_TBD, 3),
  1940. /* struct pprint_mapv */ /* [16] */
  1941. BTF_TYPE_ENC(NAME_TBD, BTF_INFO_ENC(BTF_KIND_STRUCT, 0, 8), 32),
  1942. BTF_MEMBER_ENC(NAME_TBD, 11, 0), /* uint32_t ui32 */
  1943. BTF_MEMBER_ENC(NAME_TBD, 10, 32), /* uint16_t ui16 */
  1944. BTF_MEMBER_ENC(NAME_TBD, 12, 64), /* int32_t si32 */
  1945. BTF_MEMBER_ENC(NAME_TBD, 6, 96), /* unused_bits2a */
  1946. BTF_MEMBER_ENC(NAME_TBD, 7, 98), /* bits28 */
  1947. BTF_MEMBER_ENC(NAME_TBD, 6, 126), /* unused_bits2b */
  1948. BTF_MEMBER_ENC(0, 14, 128), /* union (anon) */
  1949. BTF_MEMBER_ENC(NAME_TBD, 15, 192), /* aenum */
  1950. BTF_END_RAW,
  1951. },
  1952. .str_sec = "\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum",
  1953. .str_sec_size = sizeof("\0unsigned char\0unsigned short\0unsigned int\0int\0unsigned long long\0uint8_t\0uint16_t\0uint32_t\0int32_t\0uint64_t\0ui64\0ui8a\0ENUM_ZERO\0ENUM_ONE\0ENUM_TWO\0ENUM_THREE\0pprint_mapv\0ui32\0ui16\0si32\0unused_bits2a\0bits28\0unused_bits2b\0aenum"),
  1954. .key_size = sizeof(unsigned int),
  1955. .value_size = sizeof(struct pprint_mapv),
  1956. .key_type_id = 3, /* unsigned int */
  1957. .value_type_id = 16, /* struct pprint_mapv */
  1958. .max_entries = 128 * 1024,
  1959. };
  1960. static struct btf_pprint_test_meta {
  1961. const char *descr;
  1962. enum bpf_map_type map_type;
  1963. const char *map_name;
  1964. bool ordered_map;
  1965. bool lossless_map;
  1966. } pprint_tests_meta[] = {
  1967. {
  1968. .descr = "BTF pretty print array",
  1969. .map_type = BPF_MAP_TYPE_ARRAY,
  1970. .map_name = "pprint_test_array",
  1971. .ordered_map = true,
  1972. .lossless_map = true,
  1973. },
  1974. {
  1975. .descr = "BTF pretty print hash",
  1976. .map_type = BPF_MAP_TYPE_HASH,
  1977. .map_name = "pprint_test_hash",
  1978. .ordered_map = false,
  1979. .lossless_map = true,
  1980. },
  1981. {
  1982. .descr = "BTF pretty print lru hash",
  1983. .map_type = BPF_MAP_TYPE_LRU_HASH,
  1984. .map_name = "pprint_test_lru_hash",
  1985. .ordered_map = false,
  1986. .lossless_map = false,
  1987. },
  1988. };
  1989. static void set_pprint_mapv(struct pprint_mapv *v, uint32_t i)
  1990. {
  1991. v->ui32 = i;
  1992. v->si32 = -i;
  1993. v->unused_bits2a = 3;
  1994. v->bits28 = i;
  1995. v->unused_bits2b = 3;
  1996. v->ui64 = i;
  1997. v->aenum = i & 0x03;
  1998. }
  1999. static int do_test_pprint(void)
  2000. {
  2001. const struct btf_raw_test *test = &pprint_test_template;
  2002. struct bpf_create_map_attr create_attr = {};
  2003. unsigned int key, nr_read_elems;
  2004. bool ordered_map, lossless_map;
  2005. int map_fd = -1, btf_fd = -1;
  2006. struct pprint_mapv mapv = {};
  2007. unsigned int raw_btf_size;
  2008. char expected_line[255];
  2009. FILE *pin_file = NULL;
  2010. char pin_path[255];
  2011. size_t line_len = 0;
  2012. char *line = NULL;
  2013. uint8_t *raw_btf;
  2014. ssize_t nread;
  2015. int err, ret;
  2016. fprintf(stderr, "%s......", test->descr);
  2017. raw_btf = btf_raw_create(&hdr_tmpl, test->raw_types,
  2018. test->str_sec, test->str_sec_size,
  2019. &raw_btf_size);
  2020. if (!raw_btf)
  2021. return -1;
  2022. *btf_log_buf = '\0';
  2023. btf_fd = bpf_load_btf(raw_btf, raw_btf_size,
  2024. btf_log_buf, BTF_LOG_BUF_SIZE,
  2025. args.always_log);
  2026. free(raw_btf);
  2027. if (CHECK(btf_fd == -1, "errno:%d", errno)) {
  2028. err = -1;
  2029. goto done;
  2030. }
  2031. create_attr.name = test->map_name;
  2032. create_attr.map_type = test->map_type;
  2033. create_attr.key_size = test->key_size;
  2034. create_attr.value_size = test->value_size;
  2035. create_attr.max_entries = test->max_entries;
  2036. create_attr.btf_fd = btf_fd;
  2037. create_attr.btf_key_type_id = test->key_type_id;
  2038. create_attr.btf_value_type_id = test->value_type_id;
  2039. map_fd = bpf_create_map_xattr(&create_attr);
  2040. if (CHECK(map_fd == -1, "errno:%d", errno)) {
  2041. err = -1;
  2042. goto done;
  2043. }
  2044. ret = snprintf(pin_path, sizeof(pin_path), "%s/%s",
  2045. "/sys/fs/bpf", test->map_name);
  2046. if (CHECK(ret == sizeof(pin_path), "pin_path %s/%s is too long",
  2047. "/sys/fs/bpf", test->map_name)) {
  2048. err = -1;
  2049. goto done;
  2050. }
  2051. err = bpf_obj_pin(map_fd, pin_path);
  2052. if (CHECK(err, "bpf_obj_pin(%s): errno:%d.", pin_path, errno))
  2053. goto done;
  2054. for (key = 0; key < test->max_entries; key++) {
  2055. set_pprint_mapv(&mapv, key);
  2056. bpf_map_update_elem(map_fd, &key, &mapv, 0);
  2057. }
  2058. pin_file = fopen(pin_path, "r");
  2059. if (CHECK(!pin_file, "fopen(%s): errno:%d", pin_path, errno)) {
  2060. err = -1;
  2061. goto done;
  2062. }
  2063. /* Skip lines start with '#' */
  2064. while ((nread = getline(&line, &line_len, pin_file)) > 0 &&
  2065. *line == '#')
  2066. ;
  2067. if (CHECK(nread <= 0, "Unexpected EOF")) {
  2068. err = -1;
  2069. goto done;
  2070. }
  2071. nr_read_elems = 0;
  2072. ordered_map = test->ordered_map;
  2073. lossless_map = test->lossless_map;
  2074. do {
  2075. ssize_t nexpected_line;
  2076. unsigned int next_key;
  2077. next_key = ordered_map ? nr_read_elems : atoi(line);
  2078. set_pprint_mapv(&mapv, next_key);
  2079. nexpected_line = snprintf(expected_line, sizeof(expected_line),
  2080. "%u: {%u,0,%d,0x%x,0x%x,0x%x,{%lu|[%u,%u,%u,%u,%u,%u,%u,%u]},%s}\n",
  2081. next_key,
  2082. mapv.ui32, mapv.si32,
  2083. mapv.unused_bits2a, mapv.bits28, mapv.unused_bits2b,
  2084. mapv.ui64,
  2085. mapv.ui8a[0], mapv.ui8a[1], mapv.ui8a[2], mapv.ui8a[3],
  2086. mapv.ui8a[4], mapv.ui8a[5], mapv.ui8a[6], mapv.ui8a[7],
  2087. pprint_enum_str[mapv.aenum]);
  2088. if (CHECK(nexpected_line == sizeof(expected_line),
  2089. "expected_line is too long")) {
  2090. err = -1;
  2091. goto done;
  2092. }
  2093. if (strcmp(expected_line, line)) {
  2094. err = -1;
  2095. fprintf(stderr, "unexpected pprint output\n");
  2096. fprintf(stderr, "expected: %s", expected_line);
  2097. fprintf(stderr, " read: %s", line);
  2098. goto done;
  2099. }
  2100. nread = getline(&line, &line_len, pin_file);
  2101. } while (++nr_read_elems < test->max_entries && nread > 0);
  2102. if (lossless_map &&
  2103. CHECK(nr_read_elems < test->max_entries,
  2104. "Unexpected EOF. nr_read_elems:%u test->max_entries:%u",
  2105. nr_read_elems, test->max_entries)) {
  2106. err = -1;
  2107. goto done;
  2108. }
  2109. if (CHECK(nread > 0, "Unexpected extra pprint output: %s", line)) {
  2110. err = -1;
  2111. goto done;
  2112. }
  2113. err = 0;
  2114. done:
  2115. if (!err)
  2116. fprintf(stderr, "OK");
  2117. if (*btf_log_buf && (err || args.always_log))
  2118. fprintf(stderr, "\n%s", btf_log_buf);
  2119. if (btf_fd != -1)
  2120. close(btf_fd);
  2121. if (map_fd != -1)
  2122. close(map_fd);
  2123. if (pin_file)
  2124. fclose(pin_file);
  2125. unlink(pin_path);
  2126. free(line);
  2127. return err;
  2128. }
  2129. static int test_pprint(void)
  2130. {
  2131. unsigned int i;
  2132. int err = 0;
  2133. for (i = 0; i < ARRAY_SIZE(pprint_tests_meta); i++) {
  2134. pprint_test_template.descr = pprint_tests_meta[i].descr;
  2135. pprint_test_template.map_type = pprint_tests_meta[i].map_type;
  2136. pprint_test_template.map_name = pprint_tests_meta[i].map_name;
  2137. pprint_test_template.ordered_map = pprint_tests_meta[i].ordered_map;
  2138. pprint_test_template.lossless_map = pprint_tests_meta[i].lossless_map;
  2139. err |= count_result(do_test_pprint());
  2140. }
  2141. return err;
  2142. }
  2143. static void usage(const char *cmd)
  2144. {
  2145. fprintf(stderr, "Usage: %s [-l] [[-r test_num (1 - %zu)] | [-g test_num (1 - %zu)] | [-f test_num (1 - %zu)] | [-p]]\n",
  2146. cmd, ARRAY_SIZE(raw_tests), ARRAY_SIZE(get_info_tests),
  2147. ARRAY_SIZE(file_tests));
  2148. }
  2149. static int parse_args(int argc, char **argv)
  2150. {
  2151. const char *optstr = "lpf:r:g:";
  2152. int opt;
  2153. while ((opt = getopt(argc, argv, optstr)) != -1) {
  2154. switch (opt) {
  2155. case 'l':
  2156. args.always_log = true;
  2157. break;
  2158. case 'f':
  2159. args.file_test_num = atoi(optarg);
  2160. args.file_test = true;
  2161. break;
  2162. case 'r':
  2163. args.raw_test_num = atoi(optarg);
  2164. args.raw_test = true;
  2165. break;
  2166. case 'g':
  2167. args.get_info_test_num = atoi(optarg);
  2168. args.get_info_test = true;
  2169. break;
  2170. case 'p':
  2171. args.pprint_test = true;
  2172. break;
  2173. case 'h':
  2174. usage(argv[0]);
  2175. exit(0);
  2176. default:
  2177. usage(argv[0]);
  2178. return -1;
  2179. }
  2180. }
  2181. if (args.raw_test_num &&
  2182. (args.raw_test_num < 1 ||
  2183. args.raw_test_num > ARRAY_SIZE(raw_tests))) {
  2184. fprintf(stderr, "BTF raw test number must be [1 - %zu]\n",
  2185. ARRAY_SIZE(raw_tests));
  2186. return -1;
  2187. }
  2188. if (args.file_test_num &&
  2189. (args.file_test_num < 1 ||
  2190. args.file_test_num > ARRAY_SIZE(file_tests))) {
  2191. fprintf(stderr, "BTF file test number must be [1 - %zu]\n",
  2192. ARRAY_SIZE(file_tests));
  2193. return -1;
  2194. }
  2195. if (args.get_info_test_num &&
  2196. (args.get_info_test_num < 1 ||
  2197. args.get_info_test_num > ARRAY_SIZE(get_info_tests))) {
  2198. fprintf(stderr, "BTF get info test number must be [1 - %zu]\n",
  2199. ARRAY_SIZE(get_info_tests));
  2200. return -1;
  2201. }
  2202. return 0;
  2203. }
  2204. static void print_summary(void)
  2205. {
  2206. fprintf(stderr, "PASS:%u SKIP:%u FAIL:%u\n",
  2207. pass_cnt - skip_cnt, skip_cnt, error_cnt);
  2208. }
  2209. int main(int argc, char **argv)
  2210. {
  2211. int err = 0;
  2212. err = parse_args(argc, argv);
  2213. if (err)
  2214. return err;
  2215. if (args.always_log)
  2216. libbpf_set_print(__base_pr, __base_pr, __base_pr);
  2217. if (args.raw_test)
  2218. err |= test_raw();
  2219. if (args.get_info_test)
  2220. err |= test_get_info();
  2221. if (args.file_test)
  2222. err |= test_file();
  2223. if (args.pprint_test)
  2224. err |= test_pprint();
  2225. if (args.raw_test || args.get_info_test || args.file_test ||
  2226. args.pprint_test)
  2227. goto done;
  2228. err |= test_raw();
  2229. err |= test_get_info();
  2230. err |= test_file();
  2231. done:
  2232. print_summary();
  2233. return err;
  2234. }