Skip to content

Commit 0f022f5

Browse files
committed
fix: remove incorrect comments
1 parent 2ba8b3a commit 0f022f5

18 files changed

+268
-81
lines changed

.github/workflows/c-cpp.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ jobs:
2828
- name: examples with cpp runtime
2929
run: IMPL=cpp make -C examples
3030
- name: make test
31-
run: make -C runtime/cpp test
31+
run: |
32+
make clean
33+
make -C runtime/cpp test
3234
- name: Upload build result
3335
uses: actions/[email protected]
3436
with:

runtime/cpp/cmake/SourcesAndHeaders.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ set(skel_includes
2424

2525
set(test_sources
2626
src/bpf_api_test.cpp
27+
src/memory_check_test_driver.cpp
2728
)

runtime/cpp/include/bpf-api.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ class wasm_bpf_program {
7575
};
7676

7777
/// @brief A user data structure whose instance will be shared in a wasm
78-
/// runtime. It will store a map containing id->bpf_program and map fds opened
79-
/// by a bpf program
80-
/// Note that we need to remove fds opened by a bpf program when it's closed
78+
/// runtime.
8179
struct bpf_program_manager {
8280
std::unordered_map<uint64_t, std::unique_ptr<wasm_bpf_program>> programs;
8381
};

runtime/cpp/test/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ verbose_message("Adding tests under ${CMAKE_PROJECT_NAME}Tests...")
1414
foreach(file ${test_sources})
1515
string(REGEX REPLACE "(.*/)([a-zA-Z0-9_ ]+)(\.cpp|\.c)" "\\2" test_name ${file})
1616
add_executable(${test_name}_Tests ${file})
17-
17+
add_dependencies(${test_name}_Tests libbpf-build)
18+
target_link_libraries(${test_name}_Tests PUBLIC ${LIBBPF_LIBRARIES} -lelf -lz)
19+
target_include_directories(${test_name}_Tests SYSTEM INTERFACE ${LIBBPF_INCLUDE_DIRS})
1820

1921
#
2022
# Set the compiler standard

runtime/cpp/test/Makefile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
all:
2+
cd asserts && make
3+
cd wasm-apps && make
4+
5+
DEL = rm -rf
6+
7+
clean:
8+
cd asserts && make clean
9+
cd wasm-apps && make clean

runtime/cpp/test/asserts/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.data.h

runtime/cpp/test/asserts/Makefile

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
DEL = rm -rf
3+
FILES = $(shell ls *.bpf.o | awk '{split($$0,a,".");print a[1]}')
4+
5+
all: $(FILES)
6+
7+
$(FILES) : % : %.bpf.o
8+
$(DEL) $@.data.h
9+
touch $@.data.h
10+
echo "#ifndef _$@_H" >> $@.data.h
11+
echo "#define _$@_H" >> $@.data.h
12+
echo "const char* $@_data = \"$$(cat $< | base64 -w 0)\";" >> $@.data.h
13+
echo "#endif" >> $@.data.h
14+
15+
%.data.h : %
16+
17+
clean:
18+
$(DEL) *.data.h

runtime/cpp/test/src/bpf_api_test.cpp

+18-15
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
#include <vector>
1515

1616
#include "bpf-api.h"
17-
#include "maps_test.h"
18-
// #include <bpf/bpf.h>
17+
extern "C" {
18+
#include <bpf/libbpf.h>
19+
}
1920

2021
#define TASK_COMM_LEN 16
2122
#define MAX_SLOTS 26
@@ -25,14 +26,14 @@ struct hist {
2526
char comm[TASK_COMM_LEN];
2627
} __attribute__((packed));
2728

28-
static int print_log2_hists(int fd) {
29+
static int print_log2_hists(bpf_map* map) {
2930
int err;
3031
uint32_t lookup_key = -2, next_key;
3132
struct hist hist;
32-
33-
while (!(err = bpf_map_get_next_key(fd, &lookup_key, &next_key))) {
34-
35-
err = bpf_map_lookup_elem(fd, &next_key, &hist);
33+
while (!(err = bpf_map__get_next_key(map, &lookup_key, &next_key,
34+
sizeof(next_key)))) {
35+
err = bpf_map__lookup_elem(map, &next_key, sizeof(next_key), &hist,
36+
sizeof(hist), 0);
3637
if (err < 0) {
3738
fprintf(stderr, "failed to lookup hist: %d\n", err);
3839
return -1;
@@ -46,8 +47,9 @@ static int print_log2_hists(int fd) {
4647
printf("err %d\n", err);
4748

4849
lookup_key = -2;
49-
while (!(err = bpf_map_get_next_key(fd, &lookup_key, &next_key))) {
50-
err = bpf_map_delete_elem(fd, &next_key);
50+
while (!(err = bpf_map__get_next_key(map, &lookup_key, &next_key,
51+
sizeof(next_key)))) {
52+
err = bpf_map__delete_elem(map, &next_key, sizeof(next_key), 0);
5153
if (err < 0) {
5254
fprintf(stderr, "failed to cleanup hist : %d\n", err);
5355
return -1;
@@ -57,9 +59,9 @@ static int print_log2_hists(int fd) {
5759
return 0;
5860
}
5961

60-
int main(int argc, char **argv) {
62+
int main(int argc, char** argv) {
6163
init_libbpf();
62-
wasm_bpf_program *program = new wasm_bpf_program();
64+
wasm_bpf_program* program = new wasm_bpf_program();
6365
std::ifstream runqlat("../../test/asserts/runqlat.bpf.o");
6466
std::vector<char> runqlat_str((std::istreambuf_iterator<char>(runqlat)),
6567
std::istreambuf_iterator<char>());
@@ -87,7 +89,7 @@ int main(int argc, char **argv) {
8789
delete program;
8890
return -1;
8991
}
90-
struct tm *tm;
92+
struct tm* tm;
9193
char ts[32];
9294
time_t t;
9395
int fd = program->bpf_map_fd_by_name("hists");
@@ -101,9 +103,10 @@ int main(int argc, char **argv) {
101103
tm = localtime(&t);
102104
strftime(ts, sizeof(ts), "%H:%M:%S", tm);
103105
printf("%-8s\n", ts);
104-
105-
int err = print_log2_hists(fd);
106-
if (err) break;
106+
bpf_map* map = program->map_ptr_by_fd(fd);
107+
int err = print_log2_hists(map);
108+
if (err)
109+
break;
107110
count++;
108111
}
109112
delete program;

runtime/cpp/test/src/maps_test.h

-40
Original file line numberDiff line numberDiff line change
@@ -178,46 +178,6 @@ enum bpf_attach_type {
178178
__MAX_BPF_ATTACH_TYPE
179179
};
180180

181-
static int
182-
bpf_map_update_elem(int fd, const void *key, const void *value, uint64_t flags)
183-
{
184-
return bpf_map_operate(fd, (int)BPF_MAP_UPDATE_ELEM, (void *)key,
185-
(void *)value, NULL, flags);
186-
}
187181

188-
static int
189-
bpf_map_lookup_elem(int fd, const void *key, void *value)
190-
{
191-
return bpf_map_operate(fd, (int)BPF_MAP_LOOKUP_ELEM, (void *)key, value,
192-
NULL, 0);
193-
}
194-
195-
static int
196-
bpf_map_lookup_elem_flags(int fd, const void *key, void *value, uint64_t flags)
197-
{
198-
return bpf_map_operate(fd, BPF_MAP_LOOKUP_ELEM, (void *)key, value,
199-
NULL, flags);
200-
}
201-
202-
static int
203-
bpf_map_delete_elem(int fd, const void *key)
204-
{
205-
return bpf_map_operate(fd, BPF_MAP_DELETE_ELEM, (void *)key, NULL,
206-
NULL, 0);
207-
}
208-
209-
static int
210-
bpf_map_delete_elem_flags(int fd, const void *key, uint64_t flags)
211-
{
212-
return bpf_map_operate(fd, BPF_MAP_DELETE_ELEM, (void *)key, NULL,
213-
NULL, flags);
214-
}
215-
216-
static int
217-
bpf_map_get_next_key(int fd, const void *key, void *next_key)
218-
{
219-
return bpf_map_operate(fd, BPF_MAP_GET_NEXT_KEY, (void *)key, NULL,
220-
next_key, 0);
221-
}
222182

223183
#endif /* __BPF_H__ */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "bpf-api.h"
2+
3+
#include <assert.h>
4+
#include <fstream>
5+
#include <iostream>
6+
#include <string>
7+
struct TestModule {
8+
const char* file_name;
9+
const char* desc;
10+
};
11+
12+
static TestModule TEST_MODULES[] = {
13+
{.file_name = "memory_test_1.wasm",
14+
.desc = "Test that if wasm-bpf an handle a buffer of invalid length when "
15+
"loading bpf program"},
16+
{.file_name = "memory_test_2.wasm",
17+
.desc = "Test that if wasm-bpf can handle an invalid string"}};
18+
19+
int main() {
20+
for (const auto& item : TEST_MODULES) {
21+
using namespace std;
22+
cout << "Performing test for `" << item.file_name << "`" << endl;
23+
cout << "Desc: " << item.desc << endl;
24+
ifstream module_file(string("../../test/wasm-apps/") + item.file_name);
25+
assert((bool)module_file);
26+
vector<uint8_t> wasm_module((istreambuf_iterator<char>(module_file)),
27+
istreambuf_iterator<char>());
28+
const char* args[] = {"wasm-bpf", item.file_name};
29+
int ret = wasm_main(wasm_module.data(),
30+
(unsigned int)wasm_module.size(), 2, (char**)args);
31+
cout << "exit code = " << ret << endl;
32+
assert(ret == 0);
33+
cout << "Test for " << item.file_name << " done" << endl;
34+
cout << endl;
35+
cout << endl;
36+
}
37+
return 0;
38+
}

runtime/cpp/test/wasm-apps/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.wasm

runtime/cpp/test/wasm-apps/Makefile

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
DEL = rm -rf
3+
4+
# all: binding.wasm memory_test_1.wasm memory_test_2.wasm
5+
6+
MODULES = binding memory_test_1 memory_test_2
7+
8+
MODULE_WASM_FILES = $(foreach n, $(MODULES), $(n).wasm)
9+
10+
all: $(MODULE_WASM_FILES)
11+
12+
$(MODULE_WASM_FILES) : %.wasm : %.cpp /opt/wasi-sdk/bin/clang
13+
/opt/wasi-sdk/bin/clang \
14+
--target=wasm32-wasi \
15+
-O0 -z stack-size=4096 -Wl,--initial-memory=1048576 \
16+
--sysroot=/opt/wasi-sdk/share/wasi-sysroot \
17+
-Wl,--export=all \
18+
-Wl,--export=bpf_main \
19+
-Wl,--export=process_event \
20+
-Wl,--export-table \
21+
-Wl,--strip-all,--no-entry \
22+
-Wl,--allow-undefined \
23+
-o $@ $<
24+
25+
clean:
26+
$(DEL) *.wasm
27+

runtime/cpp/test/wasm-apps/api.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#ifndef _API_H
2+
#define _API_H
3+
#include <inttypes.h>
4+
#define IMPORT_MODULE "wasm_bpf"
5+
#define ATTR(name) \
6+
__attribute__((import_module(IMPORT_MODULE), import_name(name)))
7+
/// should be externref type for bpf_object_skel.
8+
typedef uint64_t bpf_object_skel;
9+
/// lookup a bpf map fd by name.
10+
ATTR("wasm_bpf_map_fd_by_name")
11+
int wasm_bpf_map_fd_by_name(bpf_object_skel obj, const char* name);
12+
/// detach and close a bpf program.
13+
ATTR("wasm_close_bpf_object")
14+
int wasm_close_bpf_object(bpf_object_skel obj);
15+
/// CO-RE load a bpf object into the kernel.
16+
ATTR("wasm_load_bpf_object")
17+
bpf_object_skel wasm_load_bpf_object(const void* obj_buf, int obj_buf_sz);
18+
/// attach a bpf program to a kernel hook.
19+
ATTR("wasm_attach_bpf_program")
20+
int wasm_attach_bpf_program(bpf_object_skel obj,
21+
const char* name,
22+
const char* attach_target);
23+
/// poll a bpf buffer, and call a wasm callback indicated by sample_func.
24+
/// the first time to call this function will open and create a bpf buffer.
25+
ATTR("wasm_bpf_buffer_poll")
26+
int wasm_bpf_buffer_poll(bpf_object_skel program,
27+
int fd,
28+
int32_t sample_func,
29+
uint32_t ctx,
30+
char* data,
31+
int max_size,
32+
int timeout_ms);
33+
/// lookup, update, delete, and get_next_key operations on a bpf map.
34+
ATTR("wasm_bpf_map_operate")
35+
int wasm_bpf_map_operate(int fd,
36+
int cmd,
37+
void* key,
38+
void* value,
39+
void* next_key,
40+
uint64_t flags);
41+
42+
#endif
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#ifndef _BASE64DECODE_H
2+
#define _BASE64DECODE_H
3+
4+
// Copy from
5+
// https://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c
6+
7+
#include <stdint.h>
8+
#include <stdlib.h>
9+
10+
static char encoding_table[] = {
11+
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
12+
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
13+
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
14+
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
15+
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
16+
static char decoding_table[256] = {
17+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
18+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
19+
0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60,
20+
61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
21+
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0,
22+
0, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
23+
43, 44, 45, 46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30+
0, 0, 0, 0, 0, 0, 0, 0, 0};
31+
static int mod_table[] = {0, 2, 1};
32+
33+
unsigned char* base64_decode(const char* data,
34+
size_t input_length,
35+
size_t* output_length) {
36+
if (input_length % 4 != 0)
37+
return NULL;
38+
39+
*output_length = input_length / 4 * 3;
40+
if (data[input_length - 1] == '=')
41+
(*output_length)--;
42+
if (data[input_length - 2] == '=')
43+
(*output_length)--;
44+
45+
unsigned char* decoded_data = (unsigned char*)malloc(*output_length);
46+
if (decoded_data == NULL)
47+
return NULL;
48+
49+
for (int i = 0, j = 0; i < input_length;) {
50+
uint32_t sextet_a =
51+
data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
52+
uint32_t sextet_b =
53+
data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
54+
uint32_t sextet_c =
55+
data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
56+
uint32_t sextet_d =
57+
data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
58+
59+
uint32_t triple = (sextet_a << 3 * 6) + (sextet_b << 2 * 6) +
60+
(sextet_c << 1 * 6) + (sextet_d << 0 * 6);
61+
62+
if (j < *output_length)
63+
decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
64+
if (j < *output_length)
65+
decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
66+
if (j < *output_length)
67+
decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
68+
}
69+
70+
return decoded_data;
71+
}
72+
73+
#endif
File renamed without changes.

0 commit comments

Comments
 (0)