/
end_to_end.cpp
660 lines (555 loc) · 21.1 KB
/
end_to_end.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
/*
* Copyright (c) Microsoft Corporation
* SPDX-License-Identifier: MIT
*/
#define CATCH_CONFIG_MAIN
#include <chrono>
#include <mutex>
#include <thread>
#include <WinSock2.h>
#include "catch2\catch.hpp"
#include "ebpf_api.h"
#include "ebpf_bind_program_data.h"
#include "ebpf_core.h"
#include "ebpf_xdp_program_data.h"
#include "helpers.h"
#include "mock.h"
#include "tlv.h"
namespace ebpf {
#pragma warning(push)
#pragma warning(disable : 4201) // nonstandard extension used : nameless struct/union
#include "../sample/ebpf.h"
#pragma warning(pop)
}; // namespace ebpf
ebpf_handle_t
GlueCreateFileW(
PCWSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
PSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
ebpf_handle_t hTemplateFile)
{
UNREFERENCED_PARAMETER(lpFileName);
UNREFERENCED_PARAMETER(dwDesiredAccess);
UNREFERENCED_PARAMETER(dwShareMode);
UNREFERENCED_PARAMETER(lpSecurityAttributes);
UNREFERENCED_PARAMETER(dwCreationDisposition);
UNREFERENCED_PARAMETER(dwFlagsAndAttributes);
UNREFERENCED_PARAMETER(hTemplateFile);
return (ebpf_handle_t)0x12345678;
}
BOOL
GlueCloseHandle(ebpf_handle_t hObject)
{
UNREFERENCED_PARAMETER(hObject);
return TRUE;
}
BOOL
GlueDeviceIoControl(
ebpf_handle_t hDevice,
DWORD dwIoControlCode,
PVOID lpInBuffer,
DWORD nInBufferSize,
LPVOID lpOutBuffer,
DWORD nOutBufferSize,
PDWORD lpBytesReturned,
OVERLAPPED* lpOverlapped)
{
UNREFERENCED_PARAMETER(hDevice);
UNREFERENCED_PARAMETER(nInBufferSize);
UNREFERENCED_PARAMETER(dwIoControlCode);
UNREFERENCED_PARAMETER(lpOverlapped);
ebpf_result_t result;
const ebpf_operation_header_t* user_request = reinterpret_cast<decltype(user_request)>(lpInBuffer);
ebpf_operation_header_t* user_reply = nullptr;
*lpBytesReturned = 0;
auto request_id = user_request->id;
size_t minimum_request_size = 0;
size_t minimum_reply_size = 0;
result = ebpf_core_get_protocol_handler_properties(request_id, &minimum_request_size, &minimum_reply_size);
if (result != EBPF_SUCCESS)
goto Fail;
if (user_request->length < minimum_request_size) {
result = EBPF_INVALID_ARGUMENT;
goto Fail;
}
if (minimum_reply_size > 0) {
user_reply = reinterpret_cast<decltype(user_reply)>(lpOutBuffer);
if (!user_reply) {
result = EBPF_INVALID_ARGUMENT;
goto Fail;
}
if (nOutBufferSize < minimum_reply_size) {
result = EBPF_INVALID_ARGUMENT;
goto Fail;
}
user_reply->length = static_cast<uint16_t>(nOutBufferSize);
user_reply->id = user_request->id;
*lpBytesReturned = user_reply->length;
}
result =
ebpf_core_invoke_protocol_handler(request_id, user_request, user_reply, static_cast<uint16_t>(nOutBufferSize));
if (result != EBPF_SUCCESS)
goto Fail;
return TRUE;
Fail:
if (result != EBPF_SUCCESS) {
switch (result) {
case EBPF_NO_MEMORY:
SetLastError(ERROR_OUTOFMEMORY);
break;
case EBPF_ERROR_NOT_FOUND:
SetLastError(ERROR_NOT_FOUND);
break;
case EBPF_INVALID_ARGUMENT:
SetLastError(ERROR_INVALID_PARAMETER);
break;
case EBPF_ERROR_NO_MORE_KEYS:
SetLastError(ERROR_NO_MORE_ITEMS);
break;
default:
SetLastError(ERROR_INVALID_PARAMETER);
break;
}
}
return FALSE;
}
std::vector<uint8_t>
prepare_udp_packet(uint16_t udp_length)
{
std::vector<uint8_t> packet(sizeof(ebpf::IPV4_HEADER) + sizeof(ebpf::UDP_HEADER));
auto ipv4 = reinterpret_cast<ebpf::IPV4_HEADER*>(packet.data());
auto udp = reinterpret_cast<ebpf::UDP_HEADER*>(ipv4 + 1);
ipv4->Protocol = 17;
udp->length = udp_length;
return packet;
}
class _test_helper
{
public:
_test_helper()
{
device_io_control_handler = GlueDeviceIoControl;
create_file_handler = GlueCreateFileW;
close_handle_handler = GlueCloseHandle;
REQUIRE(ebpf_core_initiate() == EBPF_SUCCESS);
ec_initialized = true;
REQUIRE(ebpf_api_initiate() == EBPF_SUCCESS);
api_initialized = true;
}
~_test_helper()
{
if (api_initialized)
ebpf_api_terminate();
if (ec_initialized)
ebpf_core_terminate();
device_io_control_handler = nullptr;
create_file_handler = nullptr;
close_handle_handler = nullptr;
}
private:
bool ec_initialized = false;
bool api_initialized = false;
};
#define SAMPLE_PATH ""
TEST_CASE("droppacket-jit", "[droppacket_jit]")
{
_test_helper test_helper;
ebpf_handle_t program_handle;
ebpf_handle_t map_handle;
uint32_t count_of_map_handle = 1;
uint32_t result = 0;
const char* error_message = NULL;
single_instance_hook_t hook;
program_information_provider_t xdp_program_information(EBPF_PROGRAM_TYPE_XDP);
REQUIRE(
(result = ebpf_api_load_program(
SAMPLE_PATH "droppacket.o",
"xdp",
EBPF_EXECUTION_JIT,
&program_handle,
&count_of_map_handle,
&map_handle,
&error_message),
error_message ? printf("ebpf_api_load_program failed with %s\n", error_message) : 0,
ebpf_api_free_string(error_message),
error_message = nullptr,
result == EBPF_SUCCESS));
REQUIRE(hook.attach(program_handle) == EBPF_SUCCESS);
auto packet = prepare_udp_packet(0);
uint32_t key = 0;
uint64_t value = 1000;
REQUIRE(
ebpf_api_map_update_element(map_handle, sizeof(key), (uint8_t*)&key, sizeof(value), (uint8_t*)&value) ==
EBPF_SUCCESS);
// Test that we drop the packet and increment the map
xdp_md_t ctx{packet.data(), packet.data() + packet.size()};
REQUIRE(hook.fire(&ctx, &result) == EBPF_SUCCESS);
REQUIRE(result == 2);
REQUIRE(
ebpf_api_map_find_element(map_handle, sizeof(key), (uint8_t*)&key, sizeof(value), (uint8_t*)&value) ==
EBPF_SUCCESS);
REQUIRE(value == 1001);
REQUIRE(ebpf_api_map_delete_element(map_handle, sizeof(key), (uint8_t*)&key) == EBPF_SUCCESS);
REQUIRE(
ebpf_api_map_find_element(map_handle, sizeof(key), (uint8_t*)&key, sizeof(value), (uint8_t*)&value) ==
EBPF_SUCCESS);
REQUIRE(value == 0);
packet = prepare_udp_packet(10);
xdp_md_t ctx2{packet.data(), packet.data() + packet.size()};
REQUIRE(hook.fire(&ctx2, &result) == EBPF_SUCCESS);
REQUIRE(result == 1);
REQUIRE(
ebpf_api_map_find_element(map_handle, sizeof(key), (uint8_t*)&key, sizeof(value), (uint8_t*)&value) ==
EBPF_SUCCESS);
REQUIRE(value == 0);
hook.detach();
}
TEST_CASE("droppacket-interpret", "[droppacket_interpret]")
{
_test_helper test_helper;
ebpf_handle_t program_handle;
const char* error_message = NULL;
ebpf_handle_t map_handle;
uint32_t count_of_map_handle = 1;
uint32_t result = 0;
program_information_provider_t xdp_program_information(EBPF_PROGRAM_TYPE_XDP);
single_instance_hook_t hook;
REQUIRE(
(result = ebpf_api_load_program(
SAMPLE_PATH "droppacket.o",
"xdp",
EBPF_EXECUTION_INTERPRET,
&program_handle,
&count_of_map_handle,
&map_handle,
&error_message),
error_message ? printf("ebpf_api_load_program failed with %s\n", error_message) : 0,
ebpf_api_free_string(error_message),
error_message = NULL,
result == EBPF_SUCCESS));
REQUIRE(hook.attach(program_handle) == EBPF_SUCCESS);
auto packet = prepare_udp_packet(0);
uint32_t key = 0;
uint64_t value = 1000;
REQUIRE(
ebpf_api_map_update_element((ebpf_handle_t)1, sizeof(key), (uint8_t*)&key, sizeof(value), (uint8_t*)&value) ==
EBPF_SUCCESS);
// Test that we drop the packet and increment the map
xdp_md_t ctx{packet.data(), packet.data() + packet.size()};
REQUIRE(hook.fire(&ctx, &result) == EBPF_SUCCESS);
REQUIRE(result == 2);
REQUIRE(
ebpf_api_map_find_element((ebpf_handle_t)1, sizeof(key), (uint8_t*)&key, sizeof(value), (uint8_t*)&value) ==
EBPF_SUCCESS);
REQUIRE(value == 1001);
REQUIRE(ebpf_api_map_delete_element((ebpf_handle_t)1, sizeof(key), (uint8_t*)&key) == EBPF_SUCCESS);
REQUIRE(
ebpf_api_map_find_element((ebpf_handle_t)1, sizeof(key), (uint8_t*)&key, sizeof(value), (uint8_t*)&value) ==
EBPF_SUCCESS);
REQUIRE(value == 0);
packet = prepare_udp_packet(10);
xdp_md_t ctx2{packet.data(), packet.data() + packet.size()};
REQUIRE(hook.fire(&ctx2, &result) == EBPF_SUCCESS);
REQUIRE(result == 1);
REQUIRE(
ebpf_api_map_find_element((ebpf_handle_t)1, sizeof(key), (uint8_t*)&key, sizeof(value), (uint8_t*)&value) ==
EBPF_SUCCESS);
REQUIRE(value == 0);
}
TEST_CASE("divide_by_zero_jit", "[divide_by_zero_jit]")
{
_test_helper test_helper;
ebpf_handle_t program_handle;
ebpf_handle_t map_handle;
uint32_t count_of_map_handle = 1;
uint32_t result = 0;
const char* error_message = NULL;
single_instance_hook_t hook;
program_information_provider_t xdp_program_information(EBPF_PROGRAM_TYPE_XDP);
REQUIRE(
(result = ebpf_api_load_program(
SAMPLE_PATH "divide_by_zero.o",
"xdp",
EBPF_EXECUTION_JIT,
&program_handle,
&count_of_map_handle,
&map_handle,
&error_message),
error_message ? printf("ebpf_api_load_program failed with %s\n", error_message) : 0,
ebpf_api_free_string(error_message),
error_message = nullptr,
result == EBPF_SUCCESS));
REQUIRE(hook.attach(program_handle) == EBPF_SUCCESS);
auto packet = prepare_udp_packet(0);
// Test that we drop the packet and increment the map
xdp_md_t ctx{packet.data(), packet.data() + packet.size()};
REQUIRE(hook.fire(&ctx, &result) == EBPF_SUCCESS);
REQUIRE(result == -1);
hook.detach();
}
TEST_CASE("enum section", "[enum sections]")
{
_test_helper test_helper;
const char* error_message = nullptr;
const tlv_type_length_value_t* section_data = nullptr;
uint32_t result;
REQUIRE(
(result =
ebpf_api_elf_enumerate_sections(SAMPLE_PATH "droppacket.o", nullptr, true, §ion_data, &error_message),
ebpf_api_free_string(error_message),
error_message = nullptr,
result == 0));
for (auto current_section = tlv_child(section_data); current_section != tlv_next(section_data);
current_section = tlv_next(current_section)) {
auto section_name = tlv_child(current_section);
auto type = tlv_next(section_name);
auto map_count = tlv_next(type);
auto program_bytes = tlv_next(map_count);
auto stats_secton = tlv_next(program_bytes);
REQUIRE(static_cast<tlv_type_t>(section_name->type) == tlv_type_t::STRING);
REQUIRE(static_cast<tlv_type_t>(type->type) == tlv_type_t::UINT);
REQUIRE(static_cast<tlv_type_t>(map_count->type) == tlv_type_t::UINT);
REQUIRE(static_cast<tlv_type_t>(program_bytes->type) == tlv_type_t::BLOB);
REQUIRE(static_cast<tlv_type_t>(stats_secton->type) == tlv_type_t::SEQUENCE);
for (auto current_stat = tlv_child(stats_secton); current_stat != tlv_next(stats_secton);
current_stat = tlv_next(current_stat)) {
auto name = tlv_child(current_stat);
auto value = tlv_next(name);
REQUIRE(static_cast<tlv_type_t>(name->type) == tlv_type_t::STRING);
REQUIRE(static_cast<tlv_type_t>(value->type) == tlv_type_t::UINT);
}
}
}
TEST_CASE("verify section", "[verify section]")
{
_test_helper test_helper;
const char* error_message = nullptr;
const char* report = nullptr;
uint32_t result;
REQUIRE(
(result = ebpf_api_elf_verify_section(SAMPLE_PATH "droppacket.o", "xdp", false, &report, &error_message),
ebpf_api_free_string(error_message),
error_message = nullptr,
result == 0));
REQUIRE(report != nullptr);
ebpf_api_free_string(report);
}
typedef struct _process_entry
{
uint32_t count;
uint8_t name[64];
uint64_t appid_length;
} process_entry_t;
uint32_t
get_bind_count_for_pid(ebpf_handle_t handle, uint64_t pid)
{
process_entry_t entry{};
ebpf_api_map_find_element(handle, sizeof(pid), (uint8_t*)&pid, sizeof(entry), (uint8_t*)&entry);
return entry.count;
}
bind_action_t
emulate_bind(single_instance_hook_t& hook, uint64_t pid, const char* appid)
{
uint32_t result;
std::string app_id = appid;
bind_md_t ctx{0};
ctx.app_id_start = (uint8_t*)app_id.c_str();
ctx.app_id_end = (uint8_t*)(app_id.c_str()) + app_id.size();
ctx.process_id = pid;
ctx.operation = BIND_OPERATION_BIND;
REQUIRE(hook.fire(&ctx, &result) == EBPF_SUCCESS);
return static_cast<bind_action_t>(result);
}
void
emulate_unbind(single_instance_hook_t& hook, uint64_t pid, const char* appid)
{
uint32_t result;
std::string app_id = appid;
bind_md_t ctx{0};
ctx.process_id = pid;
ctx.operation = BIND_OPERATION_UNBIND;
REQUIRE(hook.fire(&ctx, &result) == EBPF_SUCCESS);
}
void
set_bind_limit(ebpf_handle_t handle, uint32_t limit)
{
uint32_t limit_key = 0;
REQUIRE(
ebpf_api_map_update_element(handle, sizeof(limit_key), (uint8_t*)&limit_key, sizeof(limit), (uint8_t*)&limit) ==
EBPF_SUCCESS);
}
TEST_CASE("bindmonitor-interpret", "[bindmonitor_interpret]")
{
_test_helper test_helper;
ebpf_handle_t program_handle;
const char* error_message = NULL;
ebpf_handle_t map_handles[4];
uint32_t count_of_map_handles = 2;
uint64_t fake_pid = 12345;
uint32_t result;
program_information_provider_t bind_program_information(EBPF_PROGRAM_TYPE_BIND);
REQUIRE(
(result = ebpf_api_load_program(
SAMPLE_PATH "bindmonitor.o",
"bind",
EBPF_EXECUTION_INTERPRET,
&program_handle,
&count_of_map_handles,
map_handles,
&error_message),
error_message ? printf("ebpf_api_load_program failed with %s\n", error_message) : 0,
ebpf_api_free_string(error_message),
error_message = nullptr,
result == EBPF_SUCCESS));
single_instance_hook_t hook;
std::string process_maps_name = "bindmonitor::process_maps";
std::string limit_maps_name = "bindmonitor::limits_map";
REQUIRE(
ebpf_api_pin_object(
map_handles[0],
reinterpret_cast<const uint8_t*>(process_maps_name.c_str()),
static_cast<uint32_t>(process_maps_name.size())) == EBPF_SUCCESS);
REQUIRE(
ebpf_api_pin_object(
map_handles[1],
reinterpret_cast<const uint8_t*>(limit_maps_name.c_str()),
static_cast<uint32_t>(limit_maps_name.size())) == EBPF_SUCCESS);
ebpf_handle_t test_handle;
REQUIRE(
ebpf_api_get_pinned_map(
reinterpret_cast<const uint8_t*>(process_maps_name.c_str()),
static_cast<uint32_t>(process_maps_name.size()),
&map_handles[2]) == EBPF_SUCCESS);
REQUIRE(
ebpf_api_get_pinned_map(
reinterpret_cast<const uint8_t*>(limit_maps_name.c_str()),
static_cast<uint32_t>(limit_maps_name.size()),
&map_handles[3]) == EBPF_SUCCESS);
REQUIRE(
ebpf_api_unpin_object(
reinterpret_cast<const uint8_t*>(process_maps_name.c_str()),
static_cast<uint32_t>(process_maps_name.size())) == EBPF_SUCCESS);
REQUIRE(
ebpf_api_unpin_object(
reinterpret_cast<const uint8_t*>(limit_maps_name.c_str()), static_cast<uint32_t>(limit_maps_name.size())) ==
EBPF_SUCCESS);
REQUIRE(
ebpf_api_get_pinned_map(
reinterpret_cast<const uint8_t*>(process_maps_name.c_str()),
static_cast<uint32_t>(process_maps_name.size()),
&test_handle) == ERROR_NOT_FOUND);
REQUIRE(
ebpf_api_get_pinned_map(
reinterpret_cast<const uint8_t*>(limit_maps_name.c_str()),
static_cast<uint32_t>(limit_maps_name.size()),
&test_handle) == ERROR_NOT_FOUND);
ebpf_handle_t handle_iterator = INVALID_HANDLE_VALUE;
REQUIRE(ebpf_api_get_next_map(handle_iterator, &handle_iterator) == EBPF_SUCCESS);
REQUIRE(ebpf_api_get_next_map(handle_iterator, &handle_iterator) == EBPF_SUCCESS);
REQUIRE(ebpf_api_get_next_map(handle_iterator, &handle_iterator) == EBPF_SUCCESS);
REQUIRE(handle_iterator == INVALID_HANDLE_VALUE);
hook.attach(program_handle);
// Apply policy of maximum 2 binds per process
set_bind_limit(map_handles[1], 2);
// Bind first port - success
REQUIRE(emulate_bind(hook, fake_pid, "fake_app_1") == BIND_PERMIT);
REQUIRE(get_bind_count_for_pid(map_handles[0], fake_pid) == 1);
// Bind second port - success
REQUIRE(emulate_bind(hook, fake_pid, "fake_app_1") == BIND_PERMIT);
REQUIRE(get_bind_count_for_pid(map_handles[0], fake_pid) == 2);
// Bind third port - blocked
REQUIRE(emulate_bind(hook, fake_pid, "fake_app_1") == BIND_DENY);
REQUIRE(get_bind_count_for_pid(map_handles[0], fake_pid) == 2);
// Unbind second port
emulate_unbind(hook, fake_pid, "fake_app_1");
REQUIRE(get_bind_count_for_pid(map_handles[0], fake_pid) == 1);
// Unbind first port
emulate_unbind(hook, fake_pid, "fake_app_1");
REQUIRE(get_bind_count_for_pid(map_handles[0], fake_pid) == 0);
// Bind from two apps to test enumeration
REQUIRE(emulate_bind(hook, fake_pid, "fake_app_1") == BIND_PERMIT);
REQUIRE(get_bind_count_for_pid(map_handles[0], fake_pid) == 1);
fake_pid = 54321;
REQUIRE(emulate_bind(hook, fake_pid, "fake_app_2") == BIND_PERMIT);
REQUIRE(get_bind_count_for_pid(map_handles[0], fake_pid) == 1);
uint64_t pid;
REQUIRE(
ebpf_api_get_next_map_key(map_handles[0], sizeof(uint64_t), NULL, reinterpret_cast<uint8_t*>(&pid)) ==
EBPF_SUCCESS);
REQUIRE(pid != 0);
REQUIRE(
ebpf_api_get_next_map_key(
map_handles[0], sizeof(uint64_t), reinterpret_cast<uint8_t*>(&pid), reinterpret_cast<uint8_t*>(&pid)) ==
EBPF_SUCCESS);
REQUIRE(pid != 0);
REQUIRE(
ebpf_api_get_next_map_key(
map_handles[0], sizeof(uint64_t), reinterpret_cast<uint8_t*>(&pid), reinterpret_cast<uint8_t*>(&pid)) ==
ERROR_NO_MORE_ITEMS);
hook.detach();
}
TEST_CASE("enumerate_and_query_programs", "[enumerate_and_query_programs]")
{
_test_helper test_helper;
ebpf_handle_t program_handle;
ebpf_handle_t map_handles[3];
uint32_t count_of_map_handle = 1;
const char* error_message = NULL;
uint32_t result;
const char* file_name = nullptr;
const char* section_name = nullptr;
program_information_provider_t xdp_program_information(EBPF_PROGRAM_TYPE_XDP);
REQUIRE(
(result = ebpf_api_load_program(
SAMPLE_PATH "droppacket.o",
"xdp",
EBPF_EXECUTION_JIT,
&program_handle,
&count_of_map_handle,
map_handles,
&error_message),
ebpf_api_free_string(error_message),
error_message ? printf("ebpf_api_load_program failed with %s\n", error_message) : 0,
error_message = nullptr,
result == EBPF_SUCCESS));
REQUIRE(
(result = ebpf_api_load_program(
SAMPLE_PATH "droppacket.o",
"xdp",
EBPF_EXECUTION_INTERPRET,
&program_handle,
&count_of_map_handle,
map_handles,
&error_message),
ebpf_api_free_string(error_message),
error_message ? printf("ebpf_api_load_program failed with %s\n", error_message) : 0,
error_message = nullptr,
result == EBPF_SUCCESS));
ebpf_execution_type_t type;
program_handle = INVALID_HANDLE_VALUE;
REQUIRE(ebpf_api_get_next_program(program_handle, &program_handle) == EBPF_SUCCESS);
REQUIRE(ebpf_api_program_query_information(program_handle, &type, &file_name, §ion_name) == EBPF_SUCCESS);
REQUIRE(type == EBPF_EXECUTION_JIT);
REQUIRE(strcmp(file_name, SAMPLE_PATH "droppacket.o") == 0);
ebpf_api_free_string(file_name);
file_name = nullptr;
REQUIRE(strcmp(section_name, "xdp") == 0);
REQUIRE(program_handle != INVALID_HANDLE_VALUE);
ebpf_api_free_string(section_name);
section_name = nullptr;
REQUIRE(ebpf_api_get_next_program(program_handle, &program_handle) == EBPF_SUCCESS);
REQUIRE(program_handle != INVALID_HANDLE_VALUE);
REQUIRE(ebpf_api_program_query_information(program_handle, &type, &file_name, §ion_name) == EBPF_SUCCESS);
REQUIRE(type == EBPF_EXECUTION_INTERPRET);
REQUIRE(strcmp(file_name, SAMPLE_PATH "droppacket.o") == 0);
REQUIRE(strcmp(section_name, "xdp") == 0);
ebpf_api_free_string(file_name);
ebpf_api_free_string(section_name);
file_name = nullptr;
section_name = nullptr;
REQUIRE(ebpf_api_get_next_program(program_handle, &program_handle) == EBPF_SUCCESS);
REQUIRE(program_handle == INVALID_HANDLE_VALUE);
}