• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Alan-Jowett / libbtf / 20179964478

28 Oct 2025 07:58PM UTC coverage: 95.996% (+0.7%) from 95.331%
20179964478

push

github

web-flow
Add support for correctly handling BTF data with cycles (#173)

* Add support for handling loops in BTF data

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* Additional tests

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* Fix infinite loop in dependency_order

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* Refactoring for code reuse

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* Remove dead code

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* Remove warning

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* Add more test cases

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* Remove warnings

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* PR feedback

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* Debugging CI/CD failure

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* Fix ubsan failure

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

* PR feedback

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>

---------

Signed-off-by: Alan Jowett <alan.jowett@microsoft.com>
Co-authored-by: Alan Jowett <alan.jowett@microsoft.com>

890 of 935 new or added lines in 6 files covered. (95.19%)

1 existing line in 1 file now uncovered.

2182 of 2273 relevant lines covered (96.0%)

1576.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.65
/libbtf/btf.h
1
// Copyright (c) Prevail Verifier contributors.
2
// SPDX-License-Identifier: MIT
3

4
#pragma once
5

6
#include <cstdint>
7
#include <map>
8
#include <optional>
9
#include <string>
10
#include <variant>
11
#include <vector>
12

13
namespace libbtf {
14
typedef uint32_t btf_type_id;
15

16
enum btf_kind_index {
17
  BTF_KIND_VOID,
18
  BTF_KIND_INT,
19
  BTF_KIND_PTR,
20
  BTF_KIND_ARRAY,
21
  BTF_KIND_STRUCT,
22
  BTF_KIND_UNION,
23
  BTF_KIND_ENUM,
24
  BTF_KIND_FWD,
25
  BTF_KIND_TYPEDEF,
26
  BTF_KIND_VOLATILE,
27
  BTF_KIND_CONST,
28
  BTF_KIND_RESTRICT,
29
  BTF_KIND_FUNCTION,
30
  BTF_KIND_FUNCTION_PROTOTYPE,
31
  BTF_KIND_VAR,
32
  BTF_KIND_DATA_SECTION,
33
  BTF_KIND_FLOAT,
34
  BTF_KIND_DECL_TAG,
35
  BTF_KIND_TYPE_TAG,
36
  BTF_KIND_ENUM64,
37
};
38

39
enum btf_kind_linkage {
40
  BTF_LINKAGE_STATIC,
41
  BTF_LINKAGE_GLOBAL,
42
  BTF_LINKAGE_EXTERN,
43
};
44

45
#define BTF_ENUM_TO_STRING_HELPER(X)                                           \
46
  case X:                                                                      \
47
    return #X;
48

49
static inline const char *BTF_KIND_INDEX_TO_STRING(btf_kind_index index) {
1,158✔
50
  switch (index) {
1,158✔
51
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_VOID);
12✔
52
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_INT);
409✔
53
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_PTR);
188✔
54
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_ARRAY);
84✔
55
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_STRUCT);
52✔
56
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_UNION);
4✔
57
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_ENUM);
3✔
58
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_FWD);
20✔
59
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_TYPEDEF);
222✔
60
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_VOLATILE);
4✔
61
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_CONST);
4✔
62
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_RESTRICT);
4✔
63
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_FUNCTION);
44✔
64
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_FUNCTION_PROTOTYPE);
44✔
65
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_VAR);
30✔
66
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_DATA_SECTION);
26✔
67
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_FLOAT);
2✔
68
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_DECL_TAG);
2✔
69
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_TYPE_TAG);
2✔
70
    BTF_ENUM_TO_STRING_HELPER(BTF_KIND_ENUM64);
2✔
71
  default:
×
72
    return "UNKNOWN";
×
73
  }
74
}
75

76
static inline const char *BTF_KIND_LINKAGE_TO_STRING(btf_kind_linkage linkage) {
74✔
77
  switch (linkage) {
74✔
UNCOV
78
    BTF_ENUM_TO_STRING_HELPER(BTF_LINKAGE_STATIC);
×
79
    BTF_ENUM_TO_STRING_HELPER(BTF_LINKAGE_GLOBAL);
74✔
80
    BTF_ENUM_TO_STRING_HELPER(BTF_LINKAGE_EXTERN);
×
81
  default:
×
82
    return "UNKNOWN";
×
83
  }
84
}
85

86
struct btf_kind_int {
87
  std::string name;
88
  uint32_t size_in_bytes; // The size of the integer in bytes. This value
89
                          // multiplied by 8 must be >= field_width_in_bits
90
  uint16_t offset_from_start_in_bits; // The start of the integer relative to
91
                                      // the start of the member.
92
  uint8_t field_width_in_bits;        // The size of the integer in bits.
93
  bool is_signed;
94
  bool is_char;
95
  bool is_bool;
96
};
97

98
struct btf_kind_ptr {
99
  btf_type_id type;
100
};
101

102
struct btf_kind_array {
103
  btf_type_id element_type;
104
  btf_type_id index_type;
105
  uint32_t count_of_elements;
106
};
107

108
struct btf_kind_struct_member {
109
  std::optional<std::string> name;
110
  btf_type_id type;
111
  uint32_t offset_from_start_in_bits;
112
};
113

114
using btf_kind_union_member = btf_kind_struct_member;
115

116
struct btf_kind_struct {
117
  std::optional<std::string> name;
118
  std::vector<btf_kind_struct_member> members;
119
  uint32_t size_in_bytes;
120
};
121

122
struct btf_kind_union {
123
  std::optional<std::string> name;
124
  std::vector<btf_kind_union_member> members;
125
  uint32_t size_in_bytes;
126
};
127

128
struct btf_kind_enum_member {
129
  std::string name;
130
  uint32_t value;
131
};
132

133
struct btf_kind_enum {
134
  std::optional<std::string> name;
135
  bool is_signed;
136
  std::vector<btf_kind_enum_member> members;
137
  uint32_t size_in_bytes;
138
};
139

140
struct btf_kind_fwd {
141
  std::string name;
142
  bool is_struct;
143
};
144

145
struct btf_kind_typedef {
146
  std::string name;
147
  btf_type_id type;
148
};
149

150
struct btf_kind_volatile {
151
  btf_type_id type;
152
};
153

154
struct btf_kind_const {
155
  btf_type_id type;
156
};
157

158
struct btf_kind_restrict {
159
  btf_type_id type;
160
};
161

162
struct btf_kind_function {
163
  std::string name;
164
  btf_kind_linkage linkage;
165
  btf_type_id type;
166
};
167

168
struct btf_kind_function_parameter {
169
  std::string name;
170
  btf_type_id type;
171
};
172

173
struct btf_kind_function_prototype {
174
  std::vector<btf_kind_function_parameter> parameters;
175
  btf_type_id return_type;
176
};
177

178
struct btf_kind_var {
179
  std::string name;
180
  btf_type_id type;
181
  btf_kind_linkage linkage;
182
};
183

184
struct btf_kind_data_member {
185
  btf_type_id type;
186
  uint32_t offset;
187
  uint32_t size;
188
};
189

190
struct btf_kind_data_section {
191
  std::string name;
192
  std::vector<btf_kind_data_member> members;
193
  uint32_t size;
194
};
195

196
struct btf_kind_float {
197
  std::string name;
198
  uint32_t size_in_bytes;
199
};
200

201
struct btf_kind_decl_tag {
202
  std::string name;
203
  btf_type_id type;
204
  uint32_t component_index;
205
};
206

207
struct btf_kind_type_tag {
208
  std::string name;
209
  btf_type_id type;
210
};
211

212
struct btf_kind_enum64_member {
213
  std::string name;
214
  uint64_t value;
215
};
216

217
struct btf_kind_enum64 {
218
  std::optional<std::string> name;
219
  bool is_signed;
220
  std::vector<btf_kind_enum64_member> members;
221
  uint32_t size_in_bytes;
222
};
223

224
struct btf_kind_void {};
225

226
template <typename T> struct btf_kind_traits {
227
  constexpr static bool has_optional_name = requires(const T &value) {
228
    value.name.has_value();
229
  };
230
  constexpr static bool has_name = requires(const T &value) { value.name; };
231
  constexpr static bool has_members = requires(const T &value) {
232
    value.members;
233
  };
234
  constexpr static bool has_parameters = requires(const T &value) {
235
    value.parameters;
236
  };
237
  constexpr static bool has_return_type = requires(const T &value) {
238
    value.return_type != 0;
239
  };
240
  constexpr static bool has_type = requires(const T &value) {
241
    value.type != 0;
242
  };
243
  constexpr static bool has_size_in_bytes = requires(const T &value) {
244
    value.size_in_bytes;
245
  };
246
  constexpr static bool has_linkage = requires(const T &value) {
247
    value.linkage;
248
  };
249
  constexpr static bool has_count_of_elements = requires(const T &value) {
250
    value.count_of_elements;
251
  };
252
  constexpr static bool has_element_type = requires(const T &value) {
253
    value.element_type;
254
  };
255
  constexpr static bool has_index_type = requires(const T &value) {
256
    value.index_type;
257
  };
258
  constexpr static bool has_offset_from_start_in_bits =
259
      requires(const T &value) {
260
    value.offset_from_start_in_bits;
261
  };
262
  constexpr static bool has_offset = requires(const T &value) { value.offset; };
263
  constexpr static bool has_size = requires(const T &value) { value.size; };
264
  constexpr static bool has_value = requires(const T &value) { value.value; };
265
  constexpr static bool has_is_struct = requires(const T &value) {
266
    value.is_struct;
267
  };
268
  constexpr static bool has_field_width_in_bits = requires(const T &value) {
269
    value.field_width_in_bits;
270
  };
271
  constexpr static bool has_is_signed = requires(const T &value) {
272
    value.is_signed;
273
  };
274
  constexpr static bool has_is_char = requires(const T &value) {
275
    value.is_char;
276
  };
277
  constexpr static bool has_is_bool = requires(const T &value) {
278
    value.is_bool;
279
  };
280
};
281

282
// Note: The order of the variant types must match the order in the enum above.
283
using btf_kind = std::variant<
284
    btf_kind_void, btf_kind_int, btf_kind_ptr, btf_kind_array, btf_kind_struct,
285
    btf_kind_union, btf_kind_enum, btf_kind_fwd, btf_kind_typedef,
286
    btf_kind_volatile, btf_kind_const, btf_kind_restrict, btf_kind_function,
287
    btf_kind_function_prototype, btf_kind_var, btf_kind_data_section,
288
    btf_kind_float, btf_kind_decl_tag, btf_kind_type_tag, btf_kind_enum64>;
289
} // namespace libbtf
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc