forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_c_api.cc
416 lines (334 loc) · 11 KB
/
test_c_api.cc
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
/*
* Copyright (c) 2016 GitHub, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fcntl.h>
#include <dlfcn.h>
#include <stdint.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "bcc_elf.h"
#include "bcc_perf_map.h"
#include "bcc_proc.h"
#include "bcc_syms.h"
#include "common.h"
#include "vendor/tinyformat.hpp"
#include "catch.hpp"
using namespace std;
static pid_t spawn_child(void *, bool, bool, int (*)(void *));
TEST_CASE("language detection", "[c_api]") {
const char *c = bcc_procutils_language(getpid());
REQUIRE(c);
REQUIRE(string(c).compare("c") == 0);
}
TEST_CASE("shared object resolution", "[c_api]") {
char *libm = bcc_procutils_which_so("m", 0);
REQUIRE(libm);
REQUIRE(libm[0] == '/');
REQUIRE(string(libm).find("libm.so") != string::npos);
free(libm);
}
TEST_CASE("shared object resolution using loaded libraries", "[c_api]") {
char *libelf = bcc_procutils_which_so("elf", getpid());
REQUIRE(libelf);
REQUIRE(libelf[0] == '/');
REQUIRE(string(libelf).find("libelf") != string::npos);
free(libelf);
}
TEST_CASE("binary resolution with `which`", "[c_api]") {
char *ld = bcc_procutils_which("ld");
REQUIRE(ld);
REQUIRE(ld[0] == '/');
free(ld);
}
static void _test_ksym(const char *sym, uint64_t addr, void *_) {
if (!strcmp(sym, "startup_64"))
REQUIRE(addr != 0x0ull);
}
TEST_CASE("list all kernel symbols", "[c_api]") {
if (geteuid() != 0)
return;
bcc_procutils_each_ksym(_test_ksym, NULL);
}
TEST_CASE("file-backed mapping identification") {
CHECK(bcc_mapping_is_file_backed("/bin/ls") == 1);
CHECK(bcc_mapping_is_file_backed("") == 0);
CHECK(bcc_mapping_is_file_backed("//anon") == 0);
CHECK(bcc_mapping_is_file_backed("/dev/zero") == 0);
CHECK(bcc_mapping_is_file_backed("/anon_hugepage") == 0);
CHECK(bcc_mapping_is_file_backed("/anon_hugepage (deleted)") == 0);
CHECK(bcc_mapping_is_file_backed("[stack") == 0);
CHECK(bcc_mapping_is_file_backed("/SYSV") == 0);
CHECK(bcc_mapping_is_file_backed("[heap]") == 0);
}
TEST_CASE("resolve symbol name in external library", "[c_api]") {
struct bcc_symbol sym;
REQUIRE(bcc_resolve_symname("c", "malloc", 0x0, 0, &sym) == 0);
REQUIRE(string(sym.module).find("libc.so") != string::npos);
REQUIRE(sym.module[0] == '/');
REQUIRE(sym.offset != 0);
bcc_procutils_free(sym.module);
}
TEST_CASE("resolve symbol name in external library using loaded libraries", "[c_api]") {
struct bcc_symbol sym;
REQUIRE(bcc_resolve_symname("bcc", "bcc_procutils_which", 0x0, getpid(), &sym) == 0);
REQUIRE(string(sym.module).find("libbcc.so") != string::npos);
REQUIRE(sym.module[0] == '/');
REQUIRE(sym.offset != 0);
bcc_procutils_free(sym.module);
}
extern "C" int _a_test_function(const char *a_string) {
int i;
for (i = 0; a_string[i]; ++i)
;
return i;
}
static int setup_tmp_mnts(void) {
// Disconnect this mount namespace from its parent
if (mount(NULL, "/", NULL, MS_REC|MS_PRIVATE, NULL) < 0) {
fprintf(stderr, "unable to mark / PRIVATE: %s\n", strerror(errno));
return -1;
}
// create a new tmpfs mounted on /tmp
if (mount("tmpfs", "/tmp", "tmpfs", 0, NULL) < 0) {
fprintf(stderr, "unable to mount /tmp in mntns: %s\n", strerror(errno));
return -1;
}
return 0;
}
static int mntns_func(void *arg) {
int in_fd, out_fd;
char buf[4096];
char libpath[1024];
ssize_t rb;
void *dlhdl;
if (setup_tmp_mnts() < 0) {
return -1;
}
// Find libz.so.1, if it's installed
dlhdl = dlopen("libz.so.1", RTLD_LAZY);
if (dlhdl == NULL) {
fprintf(stderr, "Unable to dlopen libz.so.1: %s\n", dlerror());
return -1;
}
if (dlinfo(dlhdl, RTLD_DI_ORIGIN, &libpath) < 0) {
fprintf(stderr, "Unable to find origin of libz.so.1: %s\n", dlerror());
return -1;
}
dlclose(dlhdl);
dlhdl = NULL;
// Copy a shared library from shared mntns to private /tmp
snprintf(buf, 4096, "%s/libz.so.1", libpath);
in_fd = open(buf, O_RDONLY);
if (in_fd < 0) {
fprintf(stderr, "Unable to open %s: %s\n", buf, strerror(errno));
return -1;
}
out_fd = open("/tmp/libz.so.1", O_RDWR|O_CREAT|O_EXCL,
S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
if (out_fd < 0) {
fprintf(stderr, "Unable to open /tmp/libz.so.1: %s\n", strerror(errno));
return -1;
}
memset(buf, 0, sizeof (buf));
while ((rb = read(in_fd, buf, sizeof (buf))) > 0) {
if (write(out_fd, buf, rb) < 0) {
fprintf(stderr, "Write error: %s\n", strerror(errno));
return -1;
}
}
close(in_fd);
close(out_fd);
dlhdl = dlopen("/tmp/libz.so.1", RTLD_NOW);
if (dlhdl == NULL) {
fprintf(stderr, "dlopen error: %s\n", dlerror());
return -1;
}
sleep(5);
dlclose(dlhdl);
return 0;
}
TEST_CASE("resolve symbol addresses for a given PID", "[c_api]") {
struct bcc_symbol sym;
void *resolver = bcc_symcache_new(getpid());
REQUIRE(resolver);
SECTION("resolve in our own binary memory space") {
REQUIRE(bcc_symcache_resolve(resolver, (uint64_t)&_a_test_function, &sym) ==
0);
char *this_exe = realpath("/proc/self/exe", NULL);
REQUIRE(string(this_exe) == sym.module);
free(this_exe);
REQUIRE(string("_a_test_function") == sym.name);
}
SECTION("resolve in libbcc.so") {
void *libbcc = dlopen("libbcc.so", RTLD_LAZY | RTLD_NOLOAD);
REQUIRE(libbcc);
void *libbcc_fptr = dlsym(libbcc, "bcc_resolve_symname");
REQUIRE(libbcc_fptr);
REQUIRE(bcc_symcache_resolve(resolver, (uint64_t)libbcc_fptr, &sym) == 0);
REQUIRE(string(sym.module).find("libbcc.so") != string::npos);
REQUIRE(string("bcc_resolve_symname") == sym.name);
}
SECTION("resolve in libc") {
void *libc_fptr = dlsym(NULL, "strtok");
REQUIRE(libc_fptr);
REQUIRE(bcc_symcache_resolve(resolver, (uint64_t)libc_fptr, &sym) == 0);
REQUIRE(sym.module);
REQUIRE(sym.module[0] == '/');
REQUIRE(string(sym.module).find("libc") != string::npos);
REQUIRE(string("strtok") == sym.name);
}
SECTION("resolve in separate mount namespace") {
pid_t child;
uint64_t addr = 0;
child = spawn_child(0, true, true, mntns_func);
REQUIRE(child > 0);
void *resolver = bcc_symcache_new(child);
REQUIRE(resolver);
REQUIRE(bcc_symcache_resolve_name(resolver, "/tmp/libz.so.1", "zlibVersion",
&addr) == 0);
REQUIRE(addr != 0);
}
}
#define STACK_SIZE (1024 * 1024)
static char child_stack[STACK_SIZE];
static string perf_map_path(pid_t pid) {
return tfm::format("/tmp/perf-%d.map", pid);
}
static int make_perf_map_file(string &path, unsigned long long map_addr) {
FILE *file = fopen(path.c_str(), "w");
if (file == NULL) {
return -1;
}
fprintf(file, "%llx 10 dummy_fn\n", map_addr);
fprintf(file, "%llx 10 right_next_door_fn\n", map_addr + 0x10);
fclose(file);
return 0;
}
static int perf_map_func(void *arg) {
string path = perf_map_path(getpid());
if (make_perf_map_file(path, (unsigned long long)arg) < 0)
return -1;
sleep(5);
unlink(path.c_str());
return 0;
}
static int perf_map_func_mntns(void *arg) {
string path = perf_map_path(getpid());
if (setup_tmp_mnts() < 0) {
return -1;
}
if (make_perf_map_file(path, (unsigned long long)arg) < 0)
return -1;
sleep(5);
unlink(path.c_str());
return 0;
}
static int perf_map_func_noop(void *arg) {
if (setup_tmp_mnts() < 0) {
return -1;
}
sleep(5);
return 0;
}
static pid_t spawn_child(void *map_addr, bool own_pidns, bool own_mntns,
int (*child_func)(void *)) {
int flags = 0;
if (own_pidns)
flags |= CLONE_NEWPID;
if (own_mntns)
flags |= CLONE_NEWNS;
pid_t child = clone(child_func,
/* stack grows down */ child_stack + STACK_SIZE, flags, (void*)map_addr);
if (child < 0)
return -1;
sleep(1); // let the child get set up
return child;
}
TEST_CASE("resolve symbols using /tmp/perf-pid.map", "[c_api]") {
const int map_sz = 4096;
void *map_addr = mmap(NULL, map_sz, PROT_READ | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
REQUIRE(map_addr != MAP_FAILED);
struct bcc_symbol sym;
pid_t child = -1;
SECTION("same namespace") {
child = spawn_child(map_addr, /* own_pidns */ false, false, perf_map_func);
REQUIRE(child > 0);
void *resolver = bcc_symcache_new(child);
REQUIRE(resolver);
REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr,
&sym) == 0);
REQUIRE(sym.module);
REQUIRE(string(sym.module) == perf_map_path(child));
REQUIRE(string("dummy_fn") == sym.name);
REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr + 0x10,
&sym) == 0);
REQUIRE(sym.module);
REQUIRE(string(sym.module) == perf_map_path(child));
REQUIRE(string("right_next_door_fn") == sym.name);
}
SECTION("separate namespace") {
child = spawn_child(map_addr, /* own_pidns */ true, false, perf_map_func);
REQUIRE(child > 0);
void *resolver = bcc_symcache_new(child);
REQUIRE(resolver);
REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr,
&sym) == 0);
REQUIRE(sym.module);
// child is PID 1 in its namespace
REQUIRE(string(sym.module) == perf_map_path(1));
REQUIRE(string("dummy_fn") == sym.name);
unlink("/tmp/perf-1.map");
}
SECTION("separate pid and mount namespace") {
child = spawn_child(map_addr, /* own_pidns */ true, true,
perf_map_func_mntns);
REQUIRE(child > 0);
void *resolver = bcc_symcache_new(child);
REQUIRE(resolver);
REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr,
&sym) == 0);
REQUIRE(sym.module);
// child is PID 1 in its namespace
REQUIRE(string(sym.module) == perf_map_path(1));
REQUIRE(string("dummy_fn") == sym.name);
}
SECTION("separate pid and mount namespace, perf-map in host") {
child = spawn_child(map_addr, /* own_pidns */ true, true,
perf_map_func_noop);
REQUIRE(child > 0);
string path = perf_map_path(child);
REQUIRE(make_perf_map_file(path, (unsigned long long)map_addr) == 0);
void *resolver = bcc_symcache_new(child);
REQUIRE(resolver);
REQUIRE(bcc_symcache_resolve(resolver, (unsigned long long)map_addr,
&sym) == 0);
REQUIRE(sym.module);
// child is PID 1 in its namespace
REQUIRE(string(sym.module) == perf_map_path(child));
REQUIRE(string("dummy_fn") == sym.name);
unlink(path.c_str());
}
munmap(map_addr, map_sz);
}
TEST_CASE("get online CPUs", "[c_api]") {
std::vector<int> cpus = ebpf::get_online_cpus();
int num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
REQUIRE(cpus.size() == num_cpus);
}