test_btf.c 71 KB

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