Skip to content

Commit

Permalink
fix llvm example
Browse files Browse the repository at this point in the history
  • Loading branch information
yunwei37 committed Aug 10, 2024
1 parent f42fa1a commit 2457c59
Show file tree
Hide file tree
Showing 16 changed files with 1,235 additions and 102 deletions.
531 changes: 531 additions & 0 deletions .clang-format

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ CMakeFiles
_deps
cmake_install.cmake
compile_commands.json
libbpftime_llvm_jit_vm.a
29 changes: 18 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ function(bpftime_add_library target)
endfunction()

bpftime_add_library(bpftime_llvm_jit_vm
src/llvm/llvm_jit_context.cpp
src/llvm/compiler.cpp
src/llvm/compiler_utils.cpp
src/llvm_jit_context.cpp
src/compiler.cpp
src/compiler_utils.cpp
src/vm.cpp
)

set_target_properties(bpftime_llvm_jit_vm PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../")
Expand All @@ -56,11 +57,11 @@ else()
)
endif()

if(NOT DEFINED SPDLOG_INCLUDE)
message(INFO " Adding spdlog seperately..")
# spdlog
include(FetchContent)
include(FetchContent)

if(NOT DEFINED SPDLOG_INCLUDE)
message(INFO " Adding spdlog seperately..")
# spdlog
# Fetch spdlog
FetchContent_Declare(
spdlog
Expand All @@ -78,19 +79,25 @@ if(${BPFTIME_LLVM_JIT})
add_subdirectory(cli)
else()
if(${BPFTIME_ENABLE_UNIT_TESTING})
add_subdirectory(../../third_party/Catch2 ${CMAKE_CURRENT_BINARY_DIR}/Catch2)
FetchContent_Declare(
catch
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.4.0 # Specify the version you want to use
)

# Make the Catch2 target available
FetchContent_MakeAvailable(catch)
endif()
add_subdirectory(../compat ${CMAKE_CURRENT_BINARY_DIR}/compat)
endif()

message(STATUS "LLVM_LIBS=${LLVM_LIBS}")
find_package(Boost REQUIRED)

target_link_libraries(bpftime_llvm_jit_vm PUBLIC ${LLVM_LIBS} bpftime_vm_compat PRIVATE spdlog::spdlog)
target_link_libraries(bpftime_llvm_jit_vm PUBLIC ${LLVM_LIBS} PRIVATE spdlog::spdlog)
target_include_directories(bpftime_llvm_jit_vm
PUBLIC ${LLVM_INCLUDE_DIRS} ${SPDLOG_INCLUDE} ${Boost_INCLUDE} ../include include #LLVM jit also used these headers
)
add_dependencies(bpftime_llvm_jit_vm bpftime_vm_compat spdlog::spdlog)
add_dependencies(bpftime_llvm_jit_vm spdlog::spdlog)

if(BPFTIME_ENABLE_UNIT_TESTING)
message(STATUS "Build unit tests for the project. Tests should always be found in the test folder\n")
Expand Down
12 changes: 8 additions & 4 deletions cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ target_include_directories(bpftime-vm-cli
${CMAKE_CURRENT_SOURCE_DIR}/../include
${LIBBPF_INCLUDE_DIRS}
)
target_link_libraries(bpftime-vm-cli PRIVATE spdlog::spdlog argparse bpftime_llvm_jit_vm ${LIBBPF_LIBRARIES} elf z)
set_property(TARGET bpftime-vm-cli PROPERTY CXX_STANDARD 20)

target_compile_definitions(bpftime-vm-cli PRIVATE _GNU_SOURCE)

add_dependencies(bpftime-vm-cli spdlog::spdlog argparse bpftime_llvm_jit_vm libbpf)
target_link_libraries(bpftime-vm-cli
PRIVATE spdlog::spdlog argparse bpftime_llvm_jit_vm
${LIBBPF_LIBRARIES}
elf
z
)
set_property(TARGET bpftime-vm-cli PROPERTY CXX_STANDARD 20)

target_compile_definitions(bpftime-vm-cli PRIVATE _GNU_SOURCE)
install(TARGETS bpftime-vm-cli CONFIGURATIONS Release Debug RelWithDebInfo DESTINATION ~/.bpftime)
6 changes: 3 additions & 3 deletions cli/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# bpftime-vm
# bpftime-vm cli tool

```console
root@mnfe-pve:~# bpftime-vm
# bpftime-vm
Usage: bpftime-vm [--help] [--version] {build,run}

Optional arguments:
Expand All @@ -13,7 +13,7 @@ Subcommands:
run Run an native eBPF program
```

A CLI program for AOT of llvm-jit.
A CLI compiler for AOT of llvm-jit.

It can build ebpf ELF into native elf, or execute compiled native ELF. **Helpers and relocations are not supported**

Expand Down
182 changes: 182 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.27

# Default target executed when no arguments are given to make.
default_target: all
.PHONY : default_target

# Allow only one "make -f Makefile2" at a time, but pass parallelism.
.NOTPARALLEL:

#=============================================================================
# Special targets provided by cmake.

# Disable implicit rules so canonical targets will work.
.SUFFIXES:

# Disable VCS-based implicit rules.
% : %,v

# Disable VCS-based implicit rules.
% : RCS/%

# Disable VCS-based implicit rules.
% : RCS/%,v

# Disable VCS-based implicit rules.
% : SCCS/s.%

# Disable VCS-based implicit rules.
% : s.%

.SUFFIXES: .hpux_make_needs_suffix_list

# Command-line flag to silence nested $(MAKE).
$(VERBOSE)MAKESILENT = -s

#Suppress display of executed commands.
$(VERBOSE).SILENT:

# A target that is always out of date.
cmake_force:
.PHONY : cmake_force

#=============================================================================
# Set environment variables for the build.

# The shell in which to execute make rules.
SHELL = /bin/sh

# The CMake executable.
CMAKE_COMMAND = /usr/bin/cmake

# The command to remove a file.
RM = /usr/bin/cmake -E rm -f

# Escaping for special characters.
EQUALS = =

# The top-level source directory on which CMake was run.
CMAKE_SOURCE_DIR = /home/yunwei37/llvmbpf

# The top-level build directory on which CMake was run.
CMAKE_BINARY_DIR = /home/yunwei37/llvmbpf

#=============================================================================
# Targets provided globally by CMake.

# Special rule for the target edit_cache
edit_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "No interactive CMake dialog available..."
/usr/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available.
.PHONY : edit_cache

# Special rule for the target edit_cache
edit_cache/fast: edit_cache
.PHONY : edit_cache/fast

# Special rule for the target rebuild_cache
rebuild_cache:
@$(CMAKE_COMMAND) -E cmake_echo_color "--switch=$(COLOR)" --cyan "Running CMake to regenerate build system..."
/usr/bin/cmake --regenerate-during-build -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR)
.PHONY : rebuild_cache

# Special rule for the target rebuild_cache
rebuild_cache/fast: rebuild_cache
.PHONY : rebuild_cache/fast

# The main all target
all: cmake_check_build_system
cd /home/yunwei37/llvmbpf && $(CMAKE_COMMAND) -E cmake_progress_start /home/yunwei37/llvmbpf/CMakeFiles /home/yunwei37/llvmbpf/example//CMakeFiles/progress.marks
cd /home/yunwei37/llvmbpf && $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 example/all
$(CMAKE_COMMAND) -E cmake_progress_start /home/yunwei37/llvmbpf/CMakeFiles 0
.PHONY : all

# The main clean target
clean:
cd /home/yunwei37/llvmbpf && $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 example/clean
.PHONY : clean

# The main clean target
clean/fast: clean
.PHONY : clean/fast

# Prepare targets for installation.
preinstall: all
cd /home/yunwei37/llvmbpf && $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 example/preinstall
.PHONY : preinstall

# Prepare targets for installation.
preinstall/fast:
cd /home/yunwei37/llvmbpf && $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 example/preinstall
.PHONY : preinstall/fast

# clear depends
depend:
cd /home/yunwei37/llvmbpf && $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1
.PHONY : depend

# Convenience name for target.
example/CMakeFiles/vm-llvm-example.dir/rule:
cd /home/yunwei37/llvmbpf && $(MAKE) $(MAKESILENT) -f CMakeFiles/Makefile2 example/CMakeFiles/vm-llvm-example.dir/rule
.PHONY : example/CMakeFiles/vm-llvm-example.dir/rule

# Convenience name for target.
vm-llvm-example: example/CMakeFiles/vm-llvm-example.dir/rule
.PHONY : vm-llvm-example

# fast build rule for target.
vm-llvm-example/fast:
cd /home/yunwei37/llvmbpf && $(MAKE) $(MAKESILENT) -f example/CMakeFiles/vm-llvm-example.dir/build.make example/CMakeFiles/vm-llvm-example.dir/build
.PHONY : vm-llvm-example/fast

main.o: main.cpp.o
.PHONY : main.o

# target to build an object file
main.cpp.o:
cd /home/yunwei37/llvmbpf && $(MAKE) $(MAKESILENT) -f example/CMakeFiles/vm-llvm-example.dir/build.make example/CMakeFiles/vm-llvm-example.dir/main.cpp.o
.PHONY : main.cpp.o

main.i: main.cpp.i
.PHONY : main.i

# target to preprocess a source file
main.cpp.i:
cd /home/yunwei37/llvmbpf && $(MAKE) $(MAKESILENT) -f example/CMakeFiles/vm-llvm-example.dir/build.make example/CMakeFiles/vm-llvm-example.dir/main.cpp.i
.PHONY : main.cpp.i

main.s: main.cpp.s
.PHONY : main.s

# target to generate assembly for a file
main.cpp.s:
cd /home/yunwei37/llvmbpf && $(MAKE) $(MAKESILENT) -f example/CMakeFiles/vm-llvm-example.dir/build.make example/CMakeFiles/vm-llvm-example.dir/main.cpp.s
.PHONY : main.cpp.s

# Help Target
help:
@echo "The following are some of the valid targets for this Makefile:"
@echo "... all (the default if no target is provided)"
@echo "... clean"
@echo "... depend"
@echo "... edit_cache"
@echo "... rebuild_cache"
@echo "... vm-llvm-example"
@echo "... main.o"
@echo "... main.i"
@echo "... main.s"
.PHONY : help



#=============================================================================
# Special targets to cleanup operation of make.

# Special rule to run CMake to check the build system integrity.
# No rule that depends on this can have commands that come from listfiles
# because they might be regenerated.
cmake_check_build_system:
cd /home/yunwei37/llvmbpf && $(CMAKE_COMMAND) -S$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0
.PHONY : cmake_check_build_system

2 changes: 0 additions & 2 deletions example/bpf_progs.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const unsigned char jmp_to_exit[] = "\x05\x00\x01\x00\x00\x00\x00\x00"

// original code from libebpf repo
const unsigned char bpf_add_mem_64_bit_minimal[] =
""
"\x61\x12\x00\x00\x00\x00\x00\x00"
"\x61\x10\x04\x00\x00\x00\x00\x00"
"\x0f\x20\x00\x00\x00\x00\x00\x00"
Expand All @@ -42,7 +41,6 @@ const unsigned char bpf_function_call_add[] =
// return 0;
// }
const unsigned char bpf_function_call_print[] =
""
"\xb7\x01\x00\x00\x6f\x00\x00\x00"
"\x6b\x1a\xfc\xff\x00\x00\x00\x00"
"\xb7\x01\x00\x00\x68\x65\x6c\x6c"
Expand Down
43 changes: 18 additions & 25 deletions example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "llvm_jit_context.hpp"
#include <cstdint>
#include <iostream>
#include <ostream>
Expand All @@ -13,23 +12,19 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/TargetSelect.h"
#include "llvmbpf.hpp"

using namespace llvm;
using namespace bpftime;

struct ebpf_inst;

#define JIT_TEST_UBPF 1

#define TEST_BPF_CODE test_1
#define TEST_BPF_SIZE (sizeof(TEST_BPF_CODE) - 1)
#define sizeof_code(code) (sizeof(code) - 1)

typedef unsigned int (*kernel_fn)(const void *ctx,
const struct ebpf_inst *insn);

char *errmsg;
struct mem {
uint64_t val;
};

uint64_t ffi_print_func(uint64_t a, uint64_t _b, uint64_t _c, uint64_t _d,
uint64_t _e)
Expand All @@ -52,42 +47,40 @@ uint64_t ffi_print_integer(uint64_t a, uint64_t b, uint64_t _c, uint64_t _d,

uint8_t bpf_mem[] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88 };

int main(int argc, char *argv[])
{
// Initialize LLVM.
void run_ebpf_prog(const void *code, size_t code_len) {
uint64_t res = 0;
bpftime_llvm_jit_vm vm;
printf("running ebpf prog, code len: %zd\n", code_len);

InitLLVM X(argc, argv);

InitializeNativeTarget();
InitializeNativeTargetAsmPrinter();

cl::ParseCommandLineOptions(argc, argv, "HowToUseLLJIT");

bpftime::vm::llvm::bpftime_llvm_jit_vm vm;

res = vm.load_code(TEST_BPF_CODE, TEST_BPF_SIZE);
res = vm.load_code(code, code_len);
if (res) {
fprintf(stderr, "Failed to load: %s\n",
vm.get_error_message().c_str());
return 1;
return;
}
vm.register_external_function(2, "print", (void *)ffi_print_func);
vm.register_external_function(3, "add", (void *)ffi_add_func);
vm.register_external_function(4, "print_integer",
(void *)ffi_print_integer);
printf("code len: %zd\n", TEST_BPF_SIZE);
auto func = vm.compile();
if (!func) {
fprintf(stderr, "Failed to compile: %s\n",
vm.get_error_message().c_str());
return 1;
return;
}
int err = vm.exec(&bpf_mem, sizeof(bpf_mem), res);
if (err != 0) {
fprintf(stderr, "Failed to exec: %s\n", errmsg);
return 1;
return;
}
printf("res = %" PRIu64 "\n", res);
}

int main(int argc, char *argv[])
{
run_ebpf_prog(bpf_add_mem_64_bit, sizeof(bpf_add_mem_64_bit));
run_ebpf_prog(bpf_mul_64_bit, sizeof(bpf_mul_64_bit));
// here we use string for the code
run_ebpf_prog(bpf_function_call_print, sizeof(bpf_function_call_print) - 1);
return 0;
}
Loading

0 comments on commit 2457c59

Please sign in to comment.