Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add embedded clang/llvm for runtime compilation of eBPF programs #6978

Merged
merged 78 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
f04485c
Add basic clang 11 compiler
brycekahle Oct 23, 2020
ec0c56f
Fix compiler to match local build
brycekahle Nov 11, 2020
d5d21d9
Add quotes around KBUILD_MODNAME
brycekahle Nov 11, 2020
a8e1257
Whitespace fixes to compiler C++
brycekahle Nov 18, 2020
6002415
Working bundled and non-bundled tests pass
brycekahle Nov 18, 2020
afa5afb
Embedded stdarg.h with compiler
brycekahle Nov 19, 2020
45d1ee9
Working network tracer with runtime compiler
brycekahle Dec 4, 2020
2d9d2b6
Add config value for runtime compiler output directory
brycekahle Nov 23, 2020
70de38e
Rename runtime compiler enable setting
brycekahle Nov 23, 2020
78ce2cf
Add env var for setting kernel header dirs
brycekahle Nov 23, 2020
104ae07
Add preprocessor that replaces #include statements with header contents
brycekahle Nov 24, 2020
e1e88f2
Fix default BPFDir value to be absolute
brycekahle Nov 24, 2020
83b48fb
Make sure to close non-bundled files that are opened
brycekahle Nov 24, 2020
21bb603
Add basic content integrity hash generator
brycekahle Nov 24, 2020
2959477
Extract compilation logic into runtime asset
brycekahle Nov 25, 2020
f8bfa41
Add runtime-compilation of runtime-security probe
brycekahle Nov 25, 2020
c6d722d
Improve output of go generate tools
brycekahle Nov 25, 2020
dce38d3
Improve TestEbpfBytesCorrect
brycekahle Dec 4, 2020
e24d382
Fix import cycle
brycekahle Nov 25, 2020
989dc5b
Include content integrity generated files to prevent build problems
brycekahle Nov 30, 2020
a76847d
Update security probe C path
brycekahle Dec 4, 2020
8113565
Add stub for runtime-compiled security probe on unsupported builds
brycekahle Dec 4, 2020
ab34c9c
Improve default config BPFDir logic
brycekahle Dec 7, 2020
5d0d146
Move dst_port to prevent unused variable warning
brycekahle Dec 7, 2020
a4e5122
security-agent doesn't need linux_bpf tag
brycekahle Dec 8, 2020
924884a
Use updated sysprobe build images
brycekahle Dec 8, 2020
4c47b61
Fix compiler build on arm64
brycekahle Dec 8, 2020
380461d
We don't need terminfo for our use of clang/llvm
brycekahle Dec 9, 2020
4c47633
Add clang build output tarball to artifacts so we can download it
brycekahle Dec 9, 2020
0a81a72
Use $CI_PROJECT_DIR/.tmp as temp dir
brycekahle Dec 9, 2020
bf827d7
static link libstdc++
brycekahle Dec 9, 2020
b806a3b
wrap select libm (math.h) functions to target older glibc
brycekahle Dec 10, 2020
6320366
Move build flags to common function
brycekahle Dec 10, 2020
800da0d
Install specific version of kernel-headers on CentOS
brycekahle Dec 10, 2020
41c4d1a
Fix network tracer build on old kernels
brycekahle Dec 10, 2020
a42c445
Fix kernel-devel install on older centos versions
brycekahle Dec 11, 2020
aaecea0
Switch to centos 7.6 for system-probe testing
brycekahle Dec 11, 2020
b1867e3
Update buildimage
brycekahle Dec 11, 2020
b645029
Extract common functions into headers to be shared between prebuilt/r…
brycekahle Dec 11, 2020
a26549a
Remove unused variable in system_probe.py script
brycekahle Dec 11, 2020
c7c7e6b
Limit compiler stub for security to just linux
brycekahle Dec 14, 2020
d15b833
Add argument validation to compiler c++
brycekahle Dec 14, 2020
607dafa
Fix CGO_LDFLAGS_ALLOW to not clobber on win32
brycekahle Dec 14, 2020
1bdee9e
Add missing check for socat in test setup script
brycekahle Dec 14, 2020
9d4d381
Merge branch 'master' into bryce.kahle/runtime-compilation
brycekahle Dec 14, 2020
f4713e8
improve compiler c++ further
brycekahle Dec 14, 2020
1d2c0bd
Make LLVM init thread-safe using std::call_once
brycekahle Dec 14, 2020
6cf8e73
Add comment clarifying behavior of include_headers
brycekahle Dec 14, 2020
d8dd972
Remove remapped_files into ClangCompiler
brycekahle Dec 15, 2020
99326b4
Use std::make_unique where possible
brycekahle Dec 15, 2020
ccba0b6
Remove more C-style casts
brycekahle Dec 15, 2020
dc73d4a
Fix compiler output to flow through diagnostics engine
brycekahle Dec 15, 2020
c57c903
Move default cflags to package-level variable
brycekahle Dec 15, 2020
6292876
Add comment about curDir function
brycekahle Dec 15, 2020
e863580
No need for cast in compiler delete
brycekahle Dec 15, 2020
508d733
Merge branch 'master' into bryce.kahle/runtime-compilation
brycekahle Dec 15, 2020
bbdd4e6
Close compiler when done with it
brycekahle Dec 15, 2020
3403fa1
Fix defaultflag usage
brycekahle Dec 15, 2020
cec32e9
use llvm::IntrusiveRefCntPtr for diagnosticsEngine to prevent segfault
brycekahle Dec 15, 2020
505a4c3
Add release note for runtime compilation
brycekahle Dec 16, 2020
7347b87
Prevent config warning on startup if kernel_header_dirs not set
brycekahle Dec 17, 2020
a029c5f
Add missing config.SetKnown calls
brycekahle Dec 18, 2020
3a559e2
Merge branch 'master' into bryce.kahle/runtime-compilation
brycekahle Jan 4, 2021
5621deb
Fix comment typo
brycekahle Jan 4, 2021
0cfd109
Update runtime asset hashes
brycekahle Jan 4, 2021
e5d48a6
Change to use in-memory buffer for compiler input
brycekahle Jan 4, 2021
e90cd3b
Fix potential deadlock in http monitor
brycekahle Jan 5, 2021
62295ee
Allow compiler to compile files OR buffers
brycekahle Jan 5, 2021
e721c25
Merge branch 'master' into bryce.kahle/runtime-compilation
brycekahle Jan 6, 2021
7fe6084
Include hash of cflags in output filename
brycekahle Jan 7, 2021
4e83d96
Merge branch 'master' into bryce.kahle/runtime-compilation
brycekahle Jan 7, 2021
5709e34
Merge branch 'master' into bryce.kahle/runtime-compilation
brycekahle Jan 8, 2021
13339b9
Merge remote-tracking branch 'origin/master' into bryce.kahle/runtime…
brycekahle Jan 20, 2021
1640dac
Update runtime assets
brycekahle Jan 20, 2021
57fdc94
Run kitchen tests another time using the runtime compiled probes
brycekahle Jan 20, 2021
a9d0730
Merge remote-tracking branch 'origin/master' into bryce.kahle/runtime…
brycekahle Jan 20, 2021
b3f8dfb
Passthrough wait_thr to check_output
brycekahle Jan 20, 2021
4cbf304
Make it clear which version of tests are running
brycekahle Jan 21, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix compiler to match local build
  • Loading branch information
brycekahle committed Dec 11, 2020
commit ec0c56f6ccd9553fa4a4a21355e9e509f400b2d6
2 changes: 1 addition & 1 deletion pkg/collector/corechecks/ebpf/c/bcc/oom-kill-kern.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#define KBUILD_MODNAME "foo"
#define KBUILD_MODNAME "ddsysprobe"
#include <linux/oom.h>

#include "bpf-common.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <linux/kconfig.h>
#define KBUILD_MODNAME "foo"
#define KBUILD_MODNAME "ddsysprobe"
brycekahle marked this conversation as resolved.
Show resolved Hide resolved
#include <linux/bpf.h>
#include <linux/ptrace.h>
#include <linux/tcp.h>
Expand Down
13 changes: 7 additions & 6 deletions pkg/ebpf/compiler/compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ ClangCompiler::ClangCompiler(const char *name) :
textDiagnosticPrinter(new clang::TextDiagnosticPrinter(errStream, diagOpts.get())),
diagnosticsEngine(new clang::DiagnosticsEngine(diagID, diagOpts, textDiagnosticPrinter.get(), false)),
defaultCflags({
brycekahle marked this conversation as resolved.
Show resolved Hide resolved
"-O0", "-O2",
"clang", // DO NOT REMOVE, first flag is ignored
"-O2",
"-D__KERNEL__",
"-fno-color-diagnostics",
"-fno-unwind-tables",
"-fno-asynchronous-unwind-tables",
"-fno-stack-protector",
"-x", "c"
}),
theTriple("bpf")
Expand All @@ -34,7 +36,6 @@ ClangCompiler::ClangCompiler(const char *name) :
LLVMInitializeBPFTargetInfo();
LLVMInitializeBPFAsmPrinter();
LLVMInitializeBPFAsmParser();
llvm::InitializeAllTargetMCs();

ClangCompiler::llvmInitialized = true;
}
Expand Down Expand Up @@ -142,7 +143,7 @@ std::unique_ptr<llvm::Module> ClangCompiler::compileToBytecode(
if (outputFile)
invocation->getFrontendOpts().OutputFile = std::string(llvm::StringRef(outputFile));

invocation->getFrontendOpts().ProgramAction = clang::frontend::EmitBC;
invocation->getFrontendOpts().ProgramAction = clang::frontend::EmitLLVM;

clang::CompilerInstance compiler;
compiler.setInvocation(std::move(invocation));
Expand All @@ -152,12 +153,12 @@ std::unique_ptr<llvm::Module> ClangCompiler::compileToBytecode(
return nullptr;
}

std::unique_ptr<clang::CodeGenAction> emitBCAction(new clang::EmitBCAction(llvmContext.get()));
if (!compiler.ExecuteAction(*emitBCAction)) {
std::unique_ptr<clang::CodeGenAction> emitLLVMAction(new clang::EmitLLVMAction(llvmContext.get()));
if (!compiler.ExecuteAction(*emitLLVMAction)) {
return nullptr;
}

return emitBCAction->takeModule();
return emitLLVMAction->takeModule();
}

llvm::StringRef ClangCompiler::getDataLayout()
Expand Down
3 changes: 2 additions & 1 deletion pkg/ebpf/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package compiler
#cgo LDFLAGS: -lclangCodeGen -lclangFrontend -lclangSerialization -lclangDriver -lclangParse -lclangSema -lclangAnalysis -lclangASTMatchers -lclangRewrite -lclangEdit -lclangAST -lclangLex -lclangBasic
#cgo LDFLAGS: -L/opt/datadog-agent/embedded/lib
#cgo LDFLAGS: -lLLVMXRay -lLLVMWindowsManifest -lLLVMTableGen -lLLVMSymbolize -lLLVMDebugInfoPDB -lLLVMOrcJIT -lLLVMOrcError -lLLVMJITLink -lLLVMObjectYAML -lLLVMMIRParser -lLLVMMCJIT -lLLVMMCA -lLLVMLTO -lLLVMPasses -lLLVMCoroutines -lLLVMObjCARCOpts -lLLVMipo -lLLVMInstrumentation -lLLVMVectorize -lLLVMLinker -lLLVMIRReader -lLLVMAsmParser -lLLVMFrontendOpenMP -lLLVMExtensions -lLLVMLineEditor -lLLVMLibDriver -lLLVMGlobalISel -lLLVMFuzzMutate -lLLVMInterpreter -lLLVMExecutionEngine -lLLVMRuntimeDyld -lLLVMDWARFLinker -lLLVMDlltoolDriver -lLLVMOption -lLLVMDebugInfoGSYM -lLLVMCoverage -lLLVMCFGuard -lLLVMBPFDisassembler -lLLVMMCDisassembler -lLLVMBPFCodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMDebugInfoDWARF -lLLVMCodeGen -lLLVMTarget -lLLVMScalarOpts -lLLVMInstCombine -lLLVMAggressiveInstCombine -lLLVMTransformUtils -lLLVMBitWriter -lLLVMAnalysis -lLLVMProfileData -lLLVMObject -lLLVMTextAPI -lLLVMBitReader -lLLVMCore -lLLVMRemarks -lLLVMBitstreamReader -lLLVMBPFAsmParser -lLLVMMCParser -lLLVMBPFDesc -lLLVMMC -lLLVMDebugInfoCodeView -lLLVMDebugInfoMSF -lLLVMBinaryFormat -lLLVMBPFInfo -lLLVMSupport -lLLVMDemangle
#cgo LDFLAGS: -ldl
#cgo LDFLAGS: -lz -ldl -ltinfo
#cgo CPPFLAGS: -I/usr/include -I/opt/datadog-agent/embedded/include -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -DLLVM_MAJOR_VERSION=11

#include <stdlib.h>
Expand Down Expand Up @@ -73,6 +73,7 @@ func (e *EBPFCompiler) Close() {
func NewEBPFCompiler(verbose bool) *EBPFCompiler {
ebpfCompiler := &EBPFCompiler{
compiler: C.new_bpf_compiler(),
verbose: verbose,
}

runtime.SetFinalizer(ebpfCompiler, func(e *EBPFCompiler) {
Expand Down
7 changes: 5 additions & 2 deletions pkg/network/ebpf/c/prebuilt/offset-guess.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ struct bpf_map_def SEC("maps/tracer_status") tracer_status = {
};

static __always_inline bool proc_t_comm_equals(proc_t a, proc_t b) {
int i;
for (i = 0; i < TASK_COMM_LEN; i++) {
for (int i = 0; i < TASK_COMM_LEN; i++) {
if (a.comm[i] != b.comm[i]) {
return false;
}
// if chars equal but a NUL terminator, both strings equal
if (!a.comm[i]) {
break;
}
}
return true;
}
Expand Down
8 changes: 6 additions & 2 deletions tasks/system_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ def build_object_files(ctx, bundle_ebpf=False):
print("checking for clang executable...")
ctx.run("which clang")
print("found clang")
ctx.run("clang -v")

centos_headers_dir = "/usr/src/kernels"
debian_headers_dir = "/usr/src"
Expand All @@ -369,7 +370,7 @@ def build_object_files(ctx, bundle_ebpf=False):
'-D__KERNEL__',
'-DCONFIG_64BIT',
'-D__BPF_TRACING__',
'-DKBUILD_MODNAME="\\"foo\\""',
'-DKBUILD_MODNAME=ddsysprobe',
'-Wno-unused-value',
'-Wno-pointer-sign',
'-Wno-compare-distinct-pointer-types',
Expand All @@ -381,7 +382,10 @@ def build_object_files(ctx, bundle_ebpf=False):
'-emit-llvm',
# Some linux distributions enable stack protector by default which is not available on eBPF
'-fno-stack-protector',
"-I{}".format(c_dir),
'-fno-color-diagnostics',
'-fno-unwind-tables',
'-fno-asynchronous-unwind-tables',
"-I{}".format(c_dir),
]

# Mapping used by the kernel, from https://elixir.bootlin.com/linux/latest/source/scripts/subarch.include
Expand Down