btf.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: LGPL-2.1
  2. /* Copyright (c) 2018 Facebook */
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <linux/err.h>
  8. #include <linux/btf.h>
  9. #include "btf.h"
  10. #include "bpf.h"
  11. #define elog(fmt, ...) { if (err_log) err_log(fmt, ##__VA_ARGS__); }
  12. #define max(a, b) ((a) > (b) ? (a) : (b))
  13. #define min(a, b) ((a) < (b) ? (a) : (b))
  14. #define BTF_MAX_NR_TYPES 65535
  15. #define IS_MODIFIER(k) (((k) == BTF_KIND_TYPEDEF) || \
  16. ((k) == BTF_KIND_VOLATILE) || \
  17. ((k) == BTF_KIND_CONST) || \
  18. ((k) == BTF_KIND_RESTRICT))
  19. static struct btf_type btf_void;
  20. struct btf {
  21. union {
  22. struct btf_header *hdr;
  23. void *data;
  24. };
  25. struct btf_type **types;
  26. const char *strings;
  27. void *nohdr_data;
  28. __u32 nr_types;
  29. __u32 types_size;
  30. __u32 data_size;
  31. int fd;
  32. };
  33. static int btf_add_type(struct btf *btf, struct btf_type *t)
  34. {
  35. if (btf->types_size - btf->nr_types < 2) {
  36. struct btf_type **new_types;
  37. __u32 expand_by, new_size;
  38. if (btf->types_size == BTF_MAX_NR_TYPES)
  39. return -E2BIG;
  40. expand_by = max(btf->types_size >> 2, 16);
  41. new_size = min(BTF_MAX_NR_TYPES, btf->types_size + expand_by);
  42. new_types = realloc(btf->types, sizeof(*new_types) * new_size);
  43. if (!new_types)
  44. return -ENOMEM;
  45. if (btf->nr_types == 0)
  46. new_types[0] = &btf_void;
  47. btf->types = new_types;
  48. btf->types_size = new_size;
  49. }
  50. btf->types[++(btf->nr_types)] = t;
  51. return 0;
  52. }
  53. static int btf_parse_hdr(struct btf *btf, btf_print_fn_t err_log)
  54. {
  55. const struct btf_header *hdr = btf->hdr;
  56. __u32 meta_left;
  57. if (btf->data_size < sizeof(struct btf_header)) {
  58. elog("BTF header not found\n");
  59. return -EINVAL;
  60. }
  61. if (hdr->magic != BTF_MAGIC) {
  62. elog("Invalid BTF magic:%x\n", hdr->magic);
  63. return -EINVAL;
  64. }
  65. if (hdr->version != BTF_VERSION) {
  66. elog("Unsupported BTF version:%u\n", hdr->version);
  67. return -ENOTSUP;
  68. }
  69. if (hdr->flags) {
  70. elog("Unsupported BTF flags:%x\n", hdr->flags);
  71. return -ENOTSUP;
  72. }
  73. meta_left = btf->data_size - sizeof(*hdr);
  74. if (!meta_left) {
  75. elog("BTF has no data\n");
  76. return -EINVAL;
  77. }
  78. if (meta_left < hdr->type_off) {
  79. elog("Invalid BTF type section offset:%u\n", hdr->type_off);
  80. return -EINVAL;
  81. }
  82. if (meta_left < hdr->str_off) {
  83. elog("Invalid BTF string section offset:%u\n", hdr->str_off);
  84. return -EINVAL;
  85. }
  86. if (hdr->type_off >= hdr->str_off) {
  87. elog("BTF type section offset >= string section offset. No type?\n");
  88. return -EINVAL;
  89. }
  90. if (hdr->type_off & 0x02) {
  91. elog("BTF type section is not aligned to 4 bytes\n");
  92. return -EINVAL;
  93. }
  94. btf->nohdr_data = btf->hdr + 1;
  95. return 0;
  96. }
  97. static int btf_parse_str_sec(struct btf *btf, btf_print_fn_t err_log)
  98. {
  99. const struct btf_header *hdr = btf->hdr;
  100. const char *start = btf->nohdr_data + hdr->str_off;
  101. const char *end = start + btf->hdr->str_len;
  102. if (!hdr->str_len || hdr->str_len - 1 > BTF_MAX_NAME_OFFSET ||
  103. start[0] || end[-1]) {
  104. elog("Invalid BTF string section\n");
  105. return -EINVAL;
  106. }
  107. btf->strings = start;
  108. return 0;
  109. }
  110. static int btf_parse_type_sec(struct btf *btf, btf_print_fn_t err_log)
  111. {
  112. struct btf_header *hdr = btf->hdr;
  113. void *nohdr_data = btf->nohdr_data;
  114. void *next_type = nohdr_data + hdr->type_off;
  115. void *end_type = nohdr_data + hdr->str_off;
  116. while (next_type < end_type) {
  117. struct btf_type *t = next_type;
  118. __u16 vlen = BTF_INFO_VLEN(t->info);
  119. int err;
  120. next_type += sizeof(*t);
  121. switch (BTF_INFO_KIND(t->info)) {
  122. case BTF_KIND_INT:
  123. next_type += sizeof(int);
  124. break;
  125. case BTF_KIND_ARRAY:
  126. next_type += sizeof(struct btf_array);
  127. break;
  128. case BTF_KIND_STRUCT:
  129. case BTF_KIND_UNION:
  130. next_type += vlen * sizeof(struct btf_member);
  131. break;
  132. case BTF_KIND_ENUM:
  133. next_type += vlen * sizeof(struct btf_enum);
  134. break;
  135. case BTF_KIND_TYPEDEF:
  136. case BTF_KIND_PTR:
  137. case BTF_KIND_FWD:
  138. case BTF_KIND_VOLATILE:
  139. case BTF_KIND_CONST:
  140. case BTF_KIND_RESTRICT:
  141. break;
  142. default:
  143. elog("Unsupported BTF_KIND:%u\n",
  144. BTF_INFO_KIND(t->info));
  145. return -EINVAL;
  146. }
  147. err = btf_add_type(btf, t);
  148. if (err)
  149. return err;
  150. }
  151. return 0;
  152. }
  153. const struct btf_type *btf__type_by_id(const struct btf *btf, __u32 type_id)
  154. {
  155. if (type_id > btf->nr_types)
  156. return NULL;
  157. return btf->types[type_id];
  158. }
  159. static bool btf_type_is_void(const struct btf_type *t)
  160. {
  161. return t == &btf_void || BTF_INFO_KIND(t->info) == BTF_KIND_FWD;
  162. }
  163. static bool btf_type_is_void_or_null(const struct btf_type *t)
  164. {
  165. return !t || btf_type_is_void(t);
  166. }
  167. static __s64 btf_type_size(const struct btf_type *t)
  168. {
  169. switch (BTF_INFO_KIND(t->info)) {
  170. case BTF_KIND_INT:
  171. case BTF_KIND_STRUCT:
  172. case BTF_KIND_UNION:
  173. case BTF_KIND_ENUM:
  174. return t->size;
  175. case BTF_KIND_PTR:
  176. return sizeof(void *);
  177. default:
  178. return -EINVAL;
  179. }
  180. }
  181. #define MAX_RESOLVE_DEPTH 32
  182. __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
  183. {
  184. const struct btf_array *array;
  185. const struct btf_type *t;
  186. __u32 nelems = 1;
  187. __s64 size = -1;
  188. int i;
  189. t = btf__type_by_id(btf, type_id);
  190. for (i = 0; i < MAX_RESOLVE_DEPTH && !btf_type_is_void_or_null(t);
  191. i++) {
  192. size = btf_type_size(t);
  193. if (size >= 0)
  194. break;
  195. switch (BTF_INFO_KIND(t->info)) {
  196. case BTF_KIND_TYPEDEF:
  197. case BTF_KIND_VOLATILE:
  198. case BTF_KIND_CONST:
  199. case BTF_KIND_RESTRICT:
  200. type_id = t->type;
  201. break;
  202. case BTF_KIND_ARRAY:
  203. array = (const struct btf_array *)(t + 1);
  204. if (nelems && array->nelems > UINT32_MAX / nelems)
  205. return -E2BIG;
  206. nelems *= array->nelems;
  207. type_id = array->type;
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. t = btf__type_by_id(btf, type_id);
  213. }
  214. if (size < 0)
  215. return -EINVAL;
  216. if (nelems && size > UINT32_MAX / nelems)
  217. return -E2BIG;
  218. return nelems * size;
  219. }
  220. int btf__resolve_type(const struct btf *btf, __u32 type_id)
  221. {
  222. const struct btf_type *t;
  223. int depth = 0;
  224. t = btf__type_by_id(btf, type_id);
  225. while (depth < MAX_RESOLVE_DEPTH &&
  226. !btf_type_is_void_or_null(t) &&
  227. IS_MODIFIER(BTF_INFO_KIND(t->info))) {
  228. type_id = t->type;
  229. t = btf__type_by_id(btf, type_id);
  230. depth++;
  231. }
  232. if (depth == MAX_RESOLVE_DEPTH || btf_type_is_void_or_null(t))
  233. return -EINVAL;
  234. return type_id;
  235. }
  236. __s32 btf__find_by_name(const struct btf *btf, const char *type_name)
  237. {
  238. __u32 i;
  239. if (!strcmp(type_name, "void"))
  240. return 0;
  241. for (i = 1; i <= btf->nr_types; i++) {
  242. const struct btf_type *t = btf->types[i];
  243. const char *name = btf__name_by_offset(btf, t->name_off);
  244. if (name && !strcmp(type_name, name))
  245. return i;
  246. }
  247. return -ENOENT;
  248. }
  249. void btf__free(struct btf *btf)
  250. {
  251. if (!btf)
  252. return;
  253. if (btf->fd != -1)
  254. close(btf->fd);
  255. free(btf->data);
  256. free(btf->types);
  257. free(btf);
  258. }
  259. struct btf *btf__new(__u8 *data, __u32 size, btf_print_fn_t err_log)
  260. {
  261. __u32 log_buf_size = 0;
  262. char *log_buf = NULL;
  263. struct btf *btf;
  264. int err;
  265. btf = calloc(1, sizeof(struct btf));
  266. if (!btf)
  267. return ERR_PTR(-ENOMEM);
  268. btf->fd = -1;
  269. if (err_log) {
  270. log_buf = malloc(BPF_LOG_BUF_SIZE);
  271. if (!log_buf) {
  272. err = -ENOMEM;
  273. goto done;
  274. }
  275. *log_buf = 0;
  276. log_buf_size = BPF_LOG_BUF_SIZE;
  277. }
  278. btf->data = malloc(size);
  279. if (!btf->data) {
  280. err = -ENOMEM;
  281. goto done;
  282. }
  283. memcpy(btf->data, data, size);
  284. btf->data_size = size;
  285. btf->fd = bpf_load_btf(btf->data, btf->data_size,
  286. log_buf, log_buf_size, false);
  287. if (btf->fd == -1) {
  288. err = -errno;
  289. elog("Error loading BTF: %s(%d)\n", strerror(errno), errno);
  290. if (log_buf && *log_buf)
  291. elog("%s\n", log_buf);
  292. goto done;
  293. }
  294. err = btf_parse_hdr(btf, err_log);
  295. if (err)
  296. goto done;
  297. err = btf_parse_str_sec(btf, err_log);
  298. if (err)
  299. goto done;
  300. err = btf_parse_type_sec(btf, err_log);
  301. done:
  302. free(log_buf);
  303. if (err) {
  304. btf__free(btf);
  305. return ERR_PTR(err);
  306. }
  307. return btf;
  308. }
  309. int btf__fd(const struct btf *btf)
  310. {
  311. return btf->fd;
  312. }
  313. const char *btf__name_by_offset(const struct btf *btf, __u32 offset)
  314. {
  315. if (offset < btf->hdr->str_len)
  316. return &btf->strings[offset];
  317. else
  318. return NULL;
  319. }