test_btf.c 68 KB

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