Compare commits
No commits in common. 'c9' and 'i8c' have entirely different histories.
@ -1,2 +1,2 @@
|
|||||||
397cba881b0b7787869c1e51adeb22c169be9168 SOURCES/bpftrace-0.17.0.tar.gz
|
90dc7733b42f9eca714d2d87a4577c9b0a232293 SOURCES/bpftrace-0.16.0.tar.gz
|
||||||
974ee680e1eb103c415832d69742e194b661da5c SOURCES/cereal-1.3.2.tar.gz
|
974ee680e1eb103c415832d69742e194b661da5c SOURCES/cereal-1.3.2.tar.gz
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
SOURCES/bpftrace-0.17.0.tar.gz
|
SOURCES/bpftrace-0.16.0.tar.gz
|
||||||
SOURCES/cereal-1.3.2.tar.gz
|
SOURCES/cereal-1.3.2.tar.gz
|
||||||
|
@ -0,0 +1,90 @@
|
|||||||
|
From 45f0302773923accd7cc324d839b733c27c92f38 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Khem Raj <raj.khem@gmail.com>
|
||||||
|
Date: Thu, 6 Jul 2023 08:59:41 -0700
|
||||||
|
Subject: [PATCH] Adjust to build with llvm 17
|
||||||
|
|
||||||
|
- PassManagerBuilder has been removed
|
||||||
|
- itaniumDemangle() API signature has changed
|
||||||
|
- update MAX_LLVM_MAJOR in CMakeLists.txt
|
||||||
|
- update bcc and libbpf submodules to their latest versions to allow
|
||||||
|
building bcc with llvm 17
|
||||||
|
- replaced JITEvaluatedSymbol by ExecutorSymbolDef in ORC
|
||||||
|
|
||||||
|
Signed-off-by: Khem Raj <raj.khem@gmail.com>
|
||||||
|
Signed-off-by: Viktor Malik <viktor.malik@gmail.com>
|
||||||
|
---
|
||||||
|
CMakeLists.txt | 2 +-
|
||||||
|
src/ast/bpforc/bpforc.h | 2 +-
|
||||||
|
src/ast/passes/codegen_llvm.cpp | 4 ++++
|
||||||
|
src/cxxdemangler/cxxdemangler_llvm.cpp | 4 ++++
|
||||||
|
4 files changed, 10 insertions(+), 2 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/CMakeLists.txt b/CMakeLists.txt
|
||||||
|
index 433153be..a11a5c36 100644
|
||||||
|
--- a/CMakeLists.txt
|
||||||
|
+++ b/CMakeLists.txt
|
||||||
|
@@ -169,7 +169,7 @@ else()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(MIN_LLVM_MAJOR 6)
|
||||||
|
- set(MAX_LLVM_MAJOR 16)
|
||||||
|
+ set(MAX_LLVM_MAJOR 17)
|
||||||
|
|
||||||
|
if((${LLVM_VERSION_MAJOR} VERSION_LESS ${MIN_LLVM_MAJOR}) OR (${LLVM_VERSION_MAJOR} VERSION_GREATER ${MAX_LLVM_MAJOR}))
|
||||||
|
message(SEND_ERROR "Unsupported LLVM version found via ${LLVM_INCLUDE_DIRS}: ${LLVM_VERSION_MAJOR}")
|
||||||
|
diff --git a/src/ast/bpforc/bpforc.h b/src/ast/bpforc/bpforc.h
|
||||||
|
index 58914419..8d2f1e63 100644
|
||||||
|
--- a/src/ast/bpforc/bpforc.h
|
||||||
|
+++ b/src/ast/bpforc/bpforc.h
|
||||||
|
@@ -157,7 +157,7 @@ public:
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef LLVM_ORC_V2
|
||||||
|
- Expected<JITEvaluatedSymbol> lookup(StringRef Name)
|
||||||
|
+ Expected<ExecutorSymbolDef> lookup(StringRef Name)
|
||||||
|
{
|
||||||
|
return ES->lookup({ &MainJD }, Mangle(Name.str()));
|
||||||
|
}
|
||||||
|
diff --git a/src/ast/passes/codegen_llvm.cpp b/src/ast/passes/codegen_llvm.cpp
|
||||||
|
index 2b888087..a75a0fd1 100644
|
||||||
|
--- a/src/ast/passes/codegen_llvm.cpp
|
||||||
|
+++ b/src/ast/passes/codegen_llvm.cpp
|
||||||
|
@@ -7,7 +7,9 @@
|
||||||
|
#include <ctime>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
|
+#if LLVM_VERSION_MAJOR <= 16
|
||||||
|
#include <llvm-c/Transforms/IPO.h>
|
||||||
|
+#endif
|
||||||
|
#include <llvm/IR/Constants.h>
|
||||||
|
#include <llvm/IR/LLVMContext.h>
|
||||||
|
#include <llvm/IR/LegacyPassManager.h>
|
||||||
|
@@ -17,7 +19,9 @@
|
||||||
|
#include <llvm/Passes/PassBuilder.h>
|
||||||
|
#endif
|
||||||
|
#include <llvm/Transforms/IPO.h>
|
||||||
|
+#if LLVM_VERSION_MAJOR <= 16
|
||||||
|
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
|
||||||
|
+#endif
|
||||||
|
|
||||||
|
#include "arch/arch.h"
|
||||||
|
#include "ast.h"
|
||||||
|
diff --git a/src/cxxdemangler/cxxdemangler_llvm.cpp b/src/cxxdemangler/cxxdemangler_llvm.cpp
|
||||||
|
index e9a9db24..1b0bf7ea 100644
|
||||||
|
--- a/src/cxxdemangler/cxxdemangler_llvm.cpp
|
||||||
|
+++ b/src/cxxdemangler/cxxdemangler_llvm.cpp
|
||||||
|
@@ -6,7 +6,11 @@ namespace bpftrace {
|
||||||
|
|
||||||
|
char* cxxdemangle(const char* mangled)
|
||||||
|
{
|
||||||
|
+#if LLVM_VERSION_MAJOR <= 16
|
||||||
|
return llvm::itaniumDemangle(mangled, nullptr, nullptr, nullptr);
|
||||||
|
+#else
|
||||||
|
+ return llvm::itaniumDemangle(mangled);
|
||||||
|
+#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace bpftrace
|
||||||
|
--
|
||||||
|
2.41.0
|
||||||
|
|
@ -0,0 +1,236 @@
|
|||||||
|
From 84cdf2a78199910642c6f8d78d906eb41e865529 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Jordan Rome <jordalgo@meta.com>
|
||||||
|
Date: Wed, 15 May 2024 10:21:30 -0600
|
||||||
|
Subject: [PATCH] Don't unpack kernel headers or look in tmp (#3156)
|
||||||
|
|
||||||
|
Looking in shared writeable locations for kernel
|
||||||
|
headers is inherently risky even bpftrace does
|
||||||
|
the unpacking. Remove this functionality and let
|
||||||
|
the user specify the path to these headers if
|
||||||
|
we can't find them in known locations.
|
||||||
|
|
||||||
|
References:
|
||||||
|
https://github.com/bpftrace/bpftrace/pull/3033
|
||||||
|
https://github.com/bpftrace/bpftrace/pull/3154
|
||||||
|
|
||||||
|
Co-authored-by: Jordan Rome <jordalgo@fedoraproject.org>
|
||||||
|
---
|
||||||
|
src/fuzz_main.cpp | 2 +-
|
||||||
|
src/main.cpp | 2 +-
|
||||||
|
src/utils.cpp | 105 +---------------------------------------------
|
||||||
|
src/utils.h | 3 +-
|
||||||
|
tests/utils.cpp | 21 ----------
|
||||||
|
5 files changed, 4 insertions(+), 129 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/src/fuzz_main.cpp b/src/fuzz_main.cpp
|
||||||
|
index e168b8d9..a538ece1 100644
|
||||||
|
--- a/src/fuzz_main.cpp
|
||||||
|
+++ b/src/fuzz_main.cpp
|
||||||
|
@@ -135,7 +135,7 @@ int fuzz_main(const char* data, size_t sz)
|
||||||
|
struct utsname utsname;
|
||||||
|
uname(&utsname);
|
||||||
|
std::string ksrc, kobj;
|
||||||
|
- auto kdirs = get_kernel_dirs(utsname, !bpftrace.feature_->has_btf());
|
||||||
|
+ auto kdirs = get_kernel_dirs(utsname);
|
||||||
|
ksrc = std::get<0>(kdirs);
|
||||||
|
kobj = std::get<1>(kdirs);
|
||||||
|
|
||||||
|
diff --git a/src/main.cpp b/src/main.cpp
|
||||||
|
index 8f543038..61ecc2d4 100644
|
||||||
|
--- a/src/main.cpp
|
||||||
|
+++ b/src/main.cpp
|
||||||
|
@@ -362,7 +362,7 @@ static std::optional<struct timespec> get_boottime()
|
||||||
|
struct utsname utsname;
|
||||||
|
uname(&utsname);
|
||||||
|
std::string ksrc, kobj;
|
||||||
|
- auto kdirs = get_kernel_dirs(utsname, !bpftrace.feature_->has_btf());
|
||||||
|
+ auto kdirs = get_kernel_dirs(utsname);
|
||||||
|
ksrc = std::get<0>(kdirs);
|
||||||
|
kobj = std::get<1>(kdirs);
|
||||||
|
|
||||||
|
diff --git a/src/utils.cpp b/src/utils.cpp
|
||||||
|
index 872014ad..2bf489a6 100644
|
||||||
|
--- a/src/utils.cpp
|
||||||
|
+++ b/src/utils.cpp
|
||||||
|
@@ -111,8 +111,6 @@ const struct vmlinux_location vmlinux_locs[] = {
|
||||||
|
{ nullptr, false },
|
||||||
|
};
|
||||||
|
|
||||||
|
-constexpr std::string_view PROC_KHEADERS_PATH = "/sys/kernel/kheaders.tar.xz";
|
||||||
|
-
|
||||||
|
static bool pid_in_different_mountns(int pid);
|
||||||
|
static std::vector<std::string>
|
||||||
|
resolve_binary_path(const std::string &cmd, const char *env_paths, int pid);
|
||||||
|
@@ -505,100 +503,6 @@ bool is_dir(const std::string& path)
|
||||||
|
return std_filesystem::is_directory(buf, ec);
|
||||||
|
}
|
||||||
|
|
||||||
|
-bool file_exists_and_ownedby_root(const char *f)
|
||||||
|
-{
|
||||||
|
- struct stat st;
|
||||||
|
- if (stat(f, &st) == 0) {
|
||||||
|
- if (st.st_uid != 0) {
|
||||||
|
- LOG(ERROR) << "header file ownership expected to be root: "
|
||||||
|
- << std::string(f);
|
||||||
|
- return false;
|
||||||
|
- }
|
||||||
|
- return true;
|
||||||
|
- }
|
||||||
|
- return false;
|
||||||
|
-}
|
||||||
|
-
|
||||||
|
-namespace {
|
||||||
|
- struct KernelHeaderTmpDir {
|
||||||
|
- KernelHeaderTmpDir(const std::string& prefix) : path{prefix + "XXXXXX"}
|
||||||
|
- {
|
||||||
|
- if (::mkdtemp(&path[0]) == nullptr) {
|
||||||
|
- throw std::runtime_error("creating temporary path for kheaders.tar.xz failed");
|
||||||
|
- }
|
||||||
|
- }
|
||||||
|
-
|
||||||
|
- ~KernelHeaderTmpDir()
|
||||||
|
- {
|
||||||
|
- if (path.size() > 0) {
|
||||||
|
- // move_to either did not succeed or did not run, so clean up after ourselves
|
||||||
|
- exec_system(("rm -rf " + path).c_str());
|
||||||
|
- }
|
||||||
|
- }
|
||||||
|
-
|
||||||
|
- void move_to(const std::string& new_path)
|
||||||
|
- {
|
||||||
|
- int err = ::rename(path.c_str(), new_path.c_str());
|
||||||
|
- if (err == 0) {
|
||||||
|
- path = "";
|
||||||
|
- }
|
||||||
|
- }
|
||||||
|
-
|
||||||
|
- std::string path;
|
||||||
|
- };
|
||||||
|
-
|
||||||
|
- std::string unpack_kheaders_tar_xz(const struct utsname& utsname)
|
||||||
|
- {
|
||||||
|
- std::error_code ec;
|
||||||
|
- std_filesystem::path path_prefix{ "/tmp" };
|
||||||
|
- std_filesystem::path path_kheaders{ PROC_KHEADERS_PATH };
|
||||||
|
- if (const char* tmpdir = ::getenv("TMPDIR")) {
|
||||||
|
- path_prefix = tmpdir;
|
||||||
|
- }
|
||||||
|
- path_prefix /= "kheaders-";
|
||||||
|
- std_filesystem::path shared_path{ path_prefix.string() + utsname.release };
|
||||||
|
-
|
||||||
|
- if (file_exists_and_ownedby_root(shared_path.c_str()))
|
||||||
|
- {
|
||||||
|
- // already unpacked
|
||||||
|
- return shared_path.string();
|
||||||
|
- }
|
||||||
|
-
|
||||||
|
- if (!std_filesystem::exists(path_kheaders, ec))
|
||||||
|
- {
|
||||||
|
- StderrSilencer silencer;
|
||||||
|
- silencer.silence();
|
||||||
|
-
|
||||||
|
- FILE* modprobe = ::popen("modprobe kheaders", "w");
|
||||||
|
- if (modprobe == nullptr || pclose(modprobe) != 0) {
|
||||||
|
- return "";
|
||||||
|
- }
|
||||||
|
-
|
||||||
|
- if (!std_filesystem::exists(path_kheaders, ec))
|
||||||
|
- {
|
||||||
|
- return "";
|
||||||
|
- }
|
||||||
|
- }
|
||||||
|
-
|
||||||
|
- KernelHeaderTmpDir tmpdir{path_prefix};
|
||||||
|
- FILE *tar = ::popen(("tar xf " + std::string(PROC_KHEADERS_PATH) + " -C " +
|
||||||
|
- tmpdir.path)
|
||||||
|
- .c_str(),
|
||||||
|
- "w");
|
||||||
|
- if (!tar) {
|
||||||
|
- return "";
|
||||||
|
- }
|
||||||
|
-
|
||||||
|
- int rc = ::pclose(tar);
|
||||||
|
- if (rc == 0) {
|
||||||
|
- tmpdir.move_to(shared_path);
|
||||||
|
- return shared_path;
|
||||||
|
- }
|
||||||
|
-
|
||||||
|
- return "";
|
||||||
|
- }
|
||||||
|
-} // namespace
|
||||||
|
-
|
||||||
|
// get_kernel_dirs returns {ksrc, kobj} - directories for pristine and
|
||||||
|
// generated kernel sources.
|
||||||
|
//
|
||||||
|
@@ -616,8 +520,7 @@ namespace {
|
||||||
|
// {"", ""} is returned if no trace of kernel headers was found at all.
|
||||||
|
// Both ksrc and kobj are guaranteed to be != "", if at least some trace of kernel sources was found.
|
||||||
|
std::tuple<std::string, std::string> get_kernel_dirs(
|
||||||
|
- const struct utsname &utsname,
|
||||||
|
- bool unpack_kheaders)
|
||||||
|
+ const struct utsname &utsname)
|
||||||
|
{
|
||||||
|
#ifdef KERNEL_HEADERS_DIR
|
||||||
|
return {KERNEL_HEADERS_DIR, KERNEL_HEADERS_DIR};
|
||||||
|
@@ -647,12 +550,6 @@ std::tuple<std::string, std::string> get_kernel_dirs(
|
||||||
|
}
|
||||||
|
if (ksrc.empty() && kobj.empty())
|
||||||
|
{
|
||||||
|
- if (unpack_kheaders)
|
||||||
|
- {
|
||||||
|
- const auto kheaders_tar_xz_path = unpack_kheaders_tar_xz(utsname);
|
||||||
|
- if (kheaders_tar_xz_path.size() > 0)
|
||||||
|
- return std::make_tuple(kheaders_tar_xz_path, kheaders_tar_xz_path);
|
||||||
|
- }
|
||||||
|
return std::make_tuple("", "");
|
||||||
|
}
|
||||||
|
if (ksrc.empty())
|
||||||
|
diff --git a/src/utils.h b/src/utils.h
|
||||||
|
index 103af0d3..c3503676 100644
|
||||||
|
--- a/src/utils.h
|
||||||
|
+++ b/src/utils.h
|
||||||
|
@@ -158,8 +158,7 @@ std::vector<int> get_possible_cpus();
|
||||||
|
bool is_dir(const std::string &path);
|
||||||
|
bool file_exists_and_ownedby_root(const char *f);
|
||||||
|
std::tuple<std::string, std::string> get_kernel_dirs(
|
||||||
|
- const struct utsname &utsname,
|
||||||
|
- bool unpack_kheaders);
|
||||||
|
+ const struct utsname &utsname);
|
||||||
|
std::vector<std::string> get_kernel_cflags(const char *uname_machine,
|
||||||
|
const std::string &ksrc,
|
||||||
|
const std::string &kobj);
|
||||||
|
diff --git a/tests/utils.cpp b/tests/utils.cpp
|
||||||
|
index 5244874f..9ca4ace5 100644
|
||||||
|
--- a/tests/utils.cpp
|
||||||
|
+++ b/tests/utils.cpp
|
||||||
|
@@ -222,27 +222,6 @@ TEST(utils, get_cgroup_path_in_hierarchy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
-TEST(utils, file_exists_and_ownedby_root)
|
||||||
|
-{
|
||||||
|
- std::string tmpdir = "/tmp/bpftrace-test-utils-XXXXXX";
|
||||||
|
- std::string file1 = "/ownedby-user";
|
||||||
|
- std::string file2 = "/no-exists";
|
||||||
|
- if (::mkdtemp(tmpdir.data()) == nullptr) {
|
||||||
|
- throw std::runtime_error("creating temporary path for tests failed");
|
||||||
|
- }
|
||||||
|
-
|
||||||
|
- int fd;
|
||||||
|
- fd = open((tmpdir + file1).c_str(), O_CREAT, S_IRUSR);
|
||||||
|
- close(fd);
|
||||||
|
- ASSERT_GE(fd, 0);
|
||||||
|
-
|
||||||
|
- EXPECT_FALSE(file_exists_and_ownedby_root((tmpdir + file1).c_str()));
|
||||||
|
- EXPECT_FALSE(file_exists_and_ownedby_root((tmpdir + file2).c_str()));
|
||||||
|
- EXPECT_TRUE(file_exists_and_ownedby_root("/proc/1/maps"));
|
||||||
|
-
|
||||||
|
- EXPECT_GT(std_filesystem::remove_all(tmpdir), 0);
|
||||||
|
-}
|
||||||
|
-
|
||||||
|
} // namespace utils
|
||||||
|
} // namespace test
|
||||||
|
} // namespace bpftrace
|
||||||
|
--
|
||||||
|
2.45.0
|
||||||
|
|
@ -0,0 +1,127 @@
|
|||||||
|
From 6d659a5283da67837e0b0ea81991d71ae068ac1c Mon Sep 17 00:00:00 2001
|
||||||
|
From: Jordan Rome <jordalgo@meta.com>
|
||||||
|
Date: Wed, 6 Mar 2024 13:59:05 -0500
|
||||||
|
Subject: [PATCH] Fix security hole checking unpacked kernel headers (#3033)
|
||||||
|
|
||||||
|
Make sure to check that the unpacked kheaders tar
|
||||||
|
is owned by root to prevent bpftrace from loading
|
||||||
|
compromised linux headers.
|
||||||
|
|
||||||
|
Co-authored-by: Jordan Rome <jordalgo@fedoraproject.org>
|
||||||
|
---
|
||||||
|
src/utils.cpp | 26 ++++++++++++++++++++++----
|
||||||
|
src/utils.h | 1 +
|
||||||
|
tests/utils.cpp | 21 +++++++++++++++++++++
|
||||||
|
3 files changed, 44 insertions(+), 4 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/src/utils.cpp b/src/utils.cpp
|
||||||
|
index 426644e8..872014ad 100644
|
||||||
|
--- a/src/utils.cpp
|
||||||
|
+++ b/src/utils.cpp
|
||||||
|
@@ -111,6 +111,8 @@ const struct vmlinux_location vmlinux_locs[] = {
|
||||||
|
{ nullptr, false },
|
||||||
|
};
|
||||||
|
|
||||||
|
+constexpr std::string_view PROC_KHEADERS_PATH = "/sys/kernel/kheaders.tar.xz";
|
||||||
|
+
|
||||||
|
static bool pid_in_different_mountns(int pid);
|
||||||
|
static std::vector<std::string>
|
||||||
|
resolve_binary_path(const std::string &cmd, const char *env_paths, int pid);
|
||||||
|
@@ -503,6 +505,20 @@ bool is_dir(const std::string& path)
|
||||||
|
return std_filesystem::is_directory(buf, ec);
|
||||||
|
}
|
||||||
|
|
||||||
|
+bool file_exists_and_ownedby_root(const char *f)
|
||||||
|
+{
|
||||||
|
+ struct stat st;
|
||||||
|
+ if (stat(f, &st) == 0) {
|
||||||
|
+ if (st.st_uid != 0) {
|
||||||
|
+ LOG(ERROR) << "header file ownership expected to be root: "
|
||||||
|
+ << std::string(f);
|
||||||
|
+ return false;
|
||||||
|
+ }
|
||||||
|
+ return true;
|
||||||
|
+ }
|
||||||
|
+ return false;
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
namespace {
|
||||||
|
struct KernelHeaderTmpDir {
|
||||||
|
KernelHeaderTmpDir(const std::string& prefix) : path{prefix + "XXXXXX"}
|
||||||
|
@@ -535,14 +551,14 @@ namespace {
|
||||||
|
{
|
||||||
|
std::error_code ec;
|
||||||
|
std_filesystem::path path_prefix{ "/tmp" };
|
||||||
|
- std_filesystem::path path_kheaders{ "/sys/kernel/kheaders.tar.xz" };
|
||||||
|
+ std_filesystem::path path_kheaders{ PROC_KHEADERS_PATH };
|
||||||
|
if (const char* tmpdir = ::getenv("TMPDIR")) {
|
||||||
|
path_prefix = tmpdir;
|
||||||
|
}
|
||||||
|
path_prefix /= "kheaders-";
|
||||||
|
std_filesystem::path shared_path{ path_prefix.string() + utsname.release };
|
||||||
|
|
||||||
|
- if (std_filesystem::exists(shared_path, ec))
|
||||||
|
+ if (file_exists_and_ownedby_root(shared_path.c_str()))
|
||||||
|
{
|
||||||
|
// already unpacked
|
||||||
|
return shared_path.string();
|
||||||
|
@@ -565,8 +581,10 @@ namespace {
|
||||||
|
}
|
||||||
|
|
||||||
|
KernelHeaderTmpDir tmpdir{path_prefix};
|
||||||
|
-
|
||||||
|
- FILE* tar = ::popen(("tar xf /sys/kernel/kheaders.tar.xz -C " + tmpdir.path).c_str(), "w");
|
||||||
|
+ FILE *tar = ::popen(("tar xf " + std::string(PROC_KHEADERS_PATH) + " -C " +
|
||||||
|
+ tmpdir.path)
|
||||||
|
+ .c_str(),
|
||||||
|
+ "w");
|
||||||
|
if (!tar) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
diff --git a/src/utils.h b/src/utils.h
|
||||||
|
index 9b96be9f..103af0d3 100644
|
||||||
|
--- a/src/utils.h
|
||||||
|
+++ b/src/utils.h
|
||||||
|
@@ -156,6 +156,7 @@ std::vector<std::string> get_wildcard_tokens(const std::string &input,
|
||||||
|
std::vector<int> get_online_cpus();
|
||||||
|
std::vector<int> get_possible_cpus();
|
||||||
|
bool is_dir(const std::string &path);
|
||||||
|
+bool file_exists_and_ownedby_root(const char *f);
|
||||||
|
std::tuple<std::string, std::string> get_kernel_dirs(
|
||||||
|
const struct utsname &utsname,
|
||||||
|
bool unpack_kheaders);
|
||||||
|
diff --git a/tests/utils.cpp b/tests/utils.cpp
|
||||||
|
index 9ca4ace5..5244874f 100644
|
||||||
|
--- a/tests/utils.cpp
|
||||||
|
+++ b/tests/utils.cpp
|
||||||
|
@@ -222,6 +222,27 @@ TEST(utils, get_cgroup_path_in_hierarchy)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
+TEST(utils, file_exists_and_ownedby_root)
|
||||||
|
+{
|
||||||
|
+ std::string tmpdir = "/tmp/bpftrace-test-utils-XXXXXX";
|
||||||
|
+ std::string file1 = "/ownedby-user";
|
||||||
|
+ std::string file2 = "/no-exists";
|
||||||
|
+ if (::mkdtemp(tmpdir.data()) == nullptr) {
|
||||||
|
+ throw std::runtime_error("creating temporary path for tests failed");
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ int fd;
|
||||||
|
+ fd = open((tmpdir + file1).c_str(), O_CREAT, S_IRUSR);
|
||||||
|
+ close(fd);
|
||||||
|
+ ASSERT_GE(fd, 0);
|
||||||
|
+
|
||||||
|
+ EXPECT_FALSE(file_exists_and_ownedby_root((tmpdir + file1).c_str()));
|
||||||
|
+ EXPECT_FALSE(file_exists_and_ownedby_root((tmpdir + file2).c_str()));
|
||||||
|
+ EXPECT_TRUE(file_exists_and_ownedby_root("/proc/1/maps"));
|
||||||
|
+
|
||||||
|
+ EXPECT_GT(std_filesystem::remove_all(tmpdir), 0);
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
} // namespace utils
|
||||||
|
} // namespace test
|
||||||
|
} // namespace bpftrace
|
||||||
|
--
|
||||||
|
2.44.0
|
||||||
|
|
@ -0,0 +1,457 @@
|
|||||||
|
From dfc1f92653707c8d11bdb3be98e68f8297b9bc71 Mon Sep 17 00:00:00 2001
|
||||||
|
From: Viktor Malik <viktor.malik@gmail.com>
|
||||||
|
Date: Mon, 10 Oct 2022 14:26:38 +0200
|
||||||
|
Subject: [PATCH] IR builder: get rid of getPointerElementType calls
|
||||||
|
|
||||||
|
Usage of Value::getPointerElementType is deprecated and will be dropped
|
||||||
|
in LLVM 16 [1].
|
||||||
|
|
||||||
|
There are several places where we use this method:
|
||||||
|
- function (value) calls - the called function type is usually
|
||||||
|
available, so just pass it to createCall, the only exception is
|
||||||
|
CreateProbeReadStr which must have been refactored
|
||||||
|
- getting the type of alloca instruction - there is a dedicated
|
||||||
|
AllocaInst::getAllocatedType method that can be used instead
|
||||||
|
- strncmp - pass sizes of the strings to CreateStrncmp to be able to get
|
||||||
|
the correct string type (which is array of uint8)
|
||||||
|
|
||||||
|
[1] https://llvm.org/docs/OpaquePointers.html
|
||||||
|
---
|
||||||
|
src/ast/irbuilderbpf.cpp | 143 ++++++++++++--------------------
|
||||||
|
src/ast/irbuilderbpf.h | 23 +++--
|
||||||
|
src/ast/passes/codegen_llvm.cpp | 30 +++++--
|
||||||
|
3 files changed, 86 insertions(+), 110 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/src/ast/irbuilderbpf.cpp b/src/ast/irbuilderbpf.cpp
|
||||||
|
index d49883f7..4036b2df 100644
|
||||||
|
--- a/src/ast/irbuilderbpf.cpp
|
||||||
|
+++ b/src/ast/irbuilderbpf.cpp
|
||||||
|
@@ -288,17 +288,16 @@ CallInst *IRBuilderBPF::CreateHelperCall(libbpf::bpf_func_id func_id,
|
||||||
|
Constant *helper_func = ConstantExpr::getCast(Instruction::IntToPtr,
|
||||||
|
getInt64(func_id),
|
||||||
|
helper_ptr_type);
|
||||||
|
- return createCall(helper_func, args, Name);
|
||||||
|
+ return createCall(helper_type, helper_func, args, Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
-CallInst *IRBuilderBPF::createCall(Value *callee,
|
||||||
|
+CallInst *IRBuilderBPF::createCall(FunctionType *callee_type,
|
||||||
|
+ Value *callee,
|
||||||
|
ArrayRef<Value *> args,
|
||||||
|
const Twine &Name)
|
||||||
|
{
|
||||||
|
#if LLVM_VERSION_MAJOR >= 11
|
||||||
|
- auto *calleePtrType = cast<PointerType>(callee->getType());
|
||||||
|
- auto *calleeType = cast<FunctionType>(calleePtrType->getPointerElementType());
|
||||||
|
- return CreateCall(calleeType, callee, args, Name);
|
||||||
|
+ return CreateCall(callee_type, callee, args, Name);
|
||||||
|
#else
|
||||||
|
return CreateCall(callee, args, Name);
|
||||||
|
#endif
|
||||||
|
@@ -307,7 +306,7 @@ CallInst *IRBuilderBPF::createCall(Value *callee,
|
||||||
|
CallInst *IRBuilderBPF::CreateBpfPseudoCallId(int mapid)
|
||||||
|
{
|
||||||
|
Function *pseudo_func = module_.getFunction("llvm.bpf.pseudo");
|
||||||
|
- return createCall(pseudo_func,
|
||||||
|
+ return CreateCall(pseudo_func,
|
||||||
|
{ getInt64(BPF_PSEUDO_MAP_FD), getInt64(mapid) },
|
||||||
|
"pseudo");
|
||||||
|
}
|
||||||
|
@@ -346,7 +345,8 @@ CallInst *IRBuilderBPF::createMapLookup(int mapid, Value *key)
|
||||||
|
Instruction::IntToPtr,
|
||||||
|
getInt64(libbpf::BPF_FUNC_map_lookup_elem),
|
||||||
|
lookup_func_ptr_type);
|
||||||
|
- return createCall(lookup_func, { map_ptr, key }, "lookup_elem");
|
||||||
|
+ return createCall(
|
||||||
|
+ lookup_func_type, lookup_func, { map_ptr, key }, "lookup_elem");
|
||||||
|
}
|
||||||
|
|
||||||
|
CallInst *IRBuilderBPF::CreateGetJoinMap(Value *ctx, const location &loc)
|
||||||
|
@@ -397,8 +397,7 @@ Value *IRBuilderBPF::CreateMapLookupElem(Value *ctx,
|
||||||
|
CREATE_MEMCPY(value, call, type.GetSize(), 1);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
- assert(value->getType()->isPointerTy() &&
|
||||||
|
- (value->getType()->getPointerElementType() == getInt64Ty()));
|
||||||
|
+ assert(value->getAllocatedType() == getInt64Ty());
|
||||||
|
// createMapLookup returns an u8*
|
||||||
|
auto *cast = CreatePointerCast(call, value->getType(), "cast");
|
||||||
|
CreateStore(CreateLoad(getInt64Ty(), cast), value);
|
||||||
|
@@ -448,7 +447,8 @@ void IRBuilderBPF::CreateMapUpdateElem(Value *ctx,
|
||||||
|
Instruction::IntToPtr,
|
||||||
|
getInt64(libbpf::BPF_FUNC_map_update_elem),
|
||||||
|
update_func_ptr_type);
|
||||||
|
- CallInst *call = createCall(update_func,
|
||||||
|
+ CallInst *call = createCall(update_func_type,
|
||||||
|
+ update_func,
|
||||||
|
{ map_ptr, key, val, flags },
|
||||||
|
"update_elem");
|
||||||
|
CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_map_update_elem, loc);
|
||||||
|
@@ -472,7 +472,8 @@ void IRBuilderBPF::CreateMapDeleteElem(Value *ctx,
|
||||||
|
Instruction::IntToPtr,
|
||||||
|
getInt64(libbpf::BPF_FUNC_map_delete_elem),
|
||||||
|
delete_func_ptr_type);
|
||||||
|
- CallInst *call = createCall(delete_func, { map_ptr, key }, "delete_elem");
|
||||||
|
+ CallInst *call = createCall(
|
||||||
|
+ delete_func_type, delete_func, { map_ptr, key }, "delete_elem");
|
||||||
|
CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_map_delete_elem, loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -508,72 +509,53 @@ void IRBuilderBPF::CreateProbeRead(Value *ctx,
|
||||||
|
Constant *proberead_func = ConstantExpr::getCast(Instruction::IntToPtr,
|
||||||
|
getInt64(read_fn),
|
||||||
|
proberead_func_ptr_type);
|
||||||
|
- CallInst *call = createCall(proberead_func,
|
||||||
|
+ CallInst *call = createCall(proberead_func_type,
|
||||||
|
+ proberead_func,
|
||||||
|
{ dst, size, src },
|
||||||
|
probeReadHelperName(read_fn));
|
||||||
|
CreateHelperErrorCond(ctx, call, read_fn, loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
-Constant *IRBuilderBPF::createProbeReadStrFn(llvm::Type *dst,
|
||||||
|
- llvm::Type *src,
|
||||||
|
- AddrSpace as)
|
||||||
|
-{
|
||||||
|
- assert(src && (src->isIntegerTy() || src->isPointerTy()));
|
||||||
|
- // int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
|
||||||
|
- FunctionType *probereadstr_func_type = FunctionType::get(
|
||||||
|
- getInt64Ty(), { dst, getInt32Ty(), src }, false);
|
||||||
|
- PointerType *probereadstr_func_ptr_type = PointerType::get(
|
||||||
|
- probereadstr_func_type, 0);
|
||||||
|
- return ConstantExpr::getCast(Instruction::IntToPtr,
|
||||||
|
- getInt64(selectProbeReadHelper(as, true)),
|
||||||
|
- probereadstr_func_ptr_type);
|
||||||
|
-}
|
||||||
|
-
|
||||||
|
CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
|
||||||
|
- AllocaInst *dst,
|
||||||
|
+ Value *dst,
|
||||||
|
size_t size,
|
||||||
|
Value *src,
|
||||||
|
AddrSpace as,
|
||||||
|
const location &loc)
|
||||||
|
{
|
||||||
|
- assert(ctx && ctx->getType() == getInt8PtrTy());
|
||||||
|
return CreateProbeReadStr(ctx, dst, getInt32(size), src, as, loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
|
||||||
|
Value *dst,
|
||||||
|
- size_t size,
|
||||||
|
- Value *src,
|
||||||
|
- AddrSpace as,
|
||||||
|
- const location &loc)
|
||||||
|
-{
|
||||||
|
- assert(ctx && ctx->getType() == getInt8PtrTy());
|
||||||
|
- Constant *fn = createProbeReadStrFn(dst->getType(), src->getType(), as);
|
||||||
|
- auto read_fn = selectProbeReadHelper(as, true);
|
||||||
|
- CallInst *call = createCall(fn,
|
||||||
|
- { dst, getInt32(size), src },
|
||||||
|
- probeReadHelperName(read_fn));
|
||||||
|
- CreateHelperErrorCond(ctx, call, read_fn, loc);
|
||||||
|
- return call;
|
||||||
|
-}
|
||||||
|
-
|
||||||
|
-CallInst *IRBuilderBPF::CreateProbeReadStr(Value *ctx,
|
||||||
|
- AllocaInst *dst,
|
||||||
|
llvm::Value *size,
|
||||||
|
Value *src,
|
||||||
|
AddrSpace as,
|
||||||
|
const location &loc)
|
||||||
|
{
|
||||||
|
assert(ctx && ctx->getType() == getInt8PtrTy());
|
||||||
|
- assert(dst && dst->getAllocatedType()->isArrayTy() &&
|
||||||
|
- dst->getAllocatedType()->getArrayElementType() == getInt8Ty());
|
||||||
|
assert(size && size->getType()->isIntegerTy());
|
||||||
|
+ if (auto *dst_alloca = dyn_cast<AllocaInst>(dst))
|
||||||
|
+ {
|
||||||
|
+ assert(dst_alloca->getAllocatedType()->isArrayTy() &&
|
||||||
|
+ dst_alloca->getAllocatedType()->getArrayElementType() ==
|
||||||
|
+ getInt8Ty());
|
||||||
|
+ }
|
||||||
|
|
||||||
|
- auto *size_i32 = CreateIntCast(size, getInt32Ty(), false);
|
||||||
|
+ auto *size_i32 = size;
|
||||||
|
+ if (size_i32->getType()->getScalarSizeInBits() != 32)
|
||||||
|
+ size_i32 = CreateIntCast(size_i32, getInt32Ty(), false);
|
||||||
|
|
||||||
|
- Constant *fn = createProbeReadStrFn(dst->getType(), src->getType(), as);
|
||||||
|
auto read_fn = selectProbeReadHelper(as, true);
|
||||||
|
- CallInst *call = createCall(fn,
|
||||||
|
+ // int bpf_probe_read_str(void *dst, int size, const void *unsafe_ptr)
|
||||||
|
+ FunctionType *probereadstr_func_type = FunctionType::get(
|
||||||
|
+ getInt64Ty(), { dst->getType(), getInt32Ty(), src->getType() }, false);
|
||||||
|
+ PointerType *probereadstr_func_ptr_type = PointerType::get(
|
||||||
|
+ probereadstr_func_type, 0);
|
||||||
|
+ Constant *probereadstr_callee = ConstantExpr::getCast(
|
||||||
|
+ Instruction::IntToPtr, getInt64(read_fn), probereadstr_func_ptr_type);
|
||||||
|
+ CallInst *call = createCall(probereadstr_func_type,
|
||||||
|
+ probereadstr_callee,
|
||||||
|
{ dst, size_i32, src },
|
||||||
|
probeReadHelperName(read_fn));
|
||||||
|
CreateHelperErrorCond(ctx, call, read_fn, loc);
|
||||||
|
@@ -732,8 +714,10 @@ Value *IRBuilderBPF::CreateUSDTReadArgument(Value *ctx,
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
-Value *IRBuilderBPF::CreateStrncmp(Value *val1,
|
||||||
|
- Value *val2,
|
||||||
|
+Value *IRBuilderBPF::CreateStrncmp(Value *str1,
|
||||||
|
+ uint64_t str1_size,
|
||||||
|
+ Value *str2,
|
||||||
|
+ uint64_t str2_size,
|
||||||
|
uint64_t n,
|
||||||
|
bool inverse)
|
||||||
|
{
|
||||||
|
@@ -762,40 +746,21 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
|
||||||
|
// Check if the compared strings are literals.
|
||||||
|
// If so, we can avoid storing the literal in memory.
|
||||||
|
std::optional<std::string> literal1;
|
||||||
|
- if (auto constString1 = dyn_cast<ConstantDataArray>(val1))
|
||||||
|
+ if (auto constString1 = dyn_cast<ConstantDataArray>(str1))
|
||||||
|
literal1 = constString1->getAsString();
|
||||||
|
- else if (isa<ConstantAggregateZero>(val1))
|
||||||
|
+ else if (isa<ConstantAggregateZero>(str1))
|
||||||
|
literal1 = "";
|
||||||
|
else
|
||||||
|
literal1 = std::nullopt;
|
||||||
|
|
||||||
|
std::optional<std::string> literal2;
|
||||||
|
- if (auto constString2 = dyn_cast<ConstantDataArray>(val2))
|
||||||
|
+ if (auto constString2 = dyn_cast<ConstantDataArray>(str2))
|
||||||
|
literal2 = constString2->getAsString();
|
||||||
|
- else if (isa<ConstantAggregateZero>(val2))
|
||||||
|
+ else if (isa<ConstantAggregateZero>(str2))
|
||||||
|
literal2 = "";
|
||||||
|
else
|
||||||
|
literal2 = std::nullopt;
|
||||||
|
|
||||||
|
- auto *val1p = dyn_cast<PointerType>(val1->getType());
|
||||||
|
- auto *val2p = dyn_cast<PointerType>(val2->getType());
|
||||||
|
-#ifndef NDEBUG
|
||||||
|
- if (!literal1)
|
||||||
|
- {
|
||||||
|
- assert(val1p);
|
||||||
|
- assert(val1p->getPointerElementType()->isArrayTy() &&
|
||||||
|
- val1p->getPointerElementType()->getArrayElementType() ==
|
||||||
|
- getInt8Ty());
|
||||||
|
- }
|
||||||
|
- if (!literal2)
|
||||||
|
- {
|
||||||
|
- assert(val2p);
|
||||||
|
- assert(val2p->getPointerElementType()->isArrayTy() &&
|
||||||
|
- val2p->getPointerElementType()->getArrayElementType() ==
|
||||||
|
- getInt8Ty());
|
||||||
|
- }
|
||||||
|
-#endif
|
||||||
|
-
|
||||||
|
Function *parent = GetInsertBlock()->getParent();
|
||||||
|
AllocaInst *store = CreateAllocaBPF(getInt1Ty(), "strcmp.result");
|
||||||
|
BasicBlock *str_ne = BasicBlock::Create(module_.getContext(),
|
||||||
|
@@ -822,8 +787,8 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
|
||||||
|
l = getInt8(literal1->c_str()[i]);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
- auto *ptr_l = CreateGEP(val1p->getPointerElementType(),
|
||||||
|
- val1,
|
||||||
|
+ auto *ptr_l = CreateGEP(ArrayType::get(getInt8Ty(), str1_size),
|
||||||
|
+ str1,
|
||||||
|
{ getInt32(0), getInt32(i) });
|
||||||
|
l = CreateLoad(getInt8Ty(), ptr_l);
|
||||||
|
}
|
||||||
|
@@ -833,8 +798,8 @@ Value *IRBuilderBPF::CreateStrncmp(Value *val1,
|
||||||
|
r = getInt8(literal2->c_str()[i]);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
- auto *ptr_r = CreateGEP(val2p->getPointerElementType(),
|
||||||
|
- val2,
|
||||||
|
+ auto *ptr_r = CreateGEP(ArrayType::get(getInt8Ty(), str2_size),
|
||||||
|
+ str2,
|
||||||
|
{ getInt32(0), getInt32(i) });
|
||||||
|
r = CreateLoad(getInt8Ty(), ptr_r);
|
||||||
|
}
|
||||||
|
@@ -994,11 +959,9 @@ void IRBuilderBPF::CreateGetCurrentComm(Value *ctx,
|
||||||
|
size_t size,
|
||||||
|
const location &loc)
|
||||||
|
{
|
||||||
|
- assert(buf->getType()->getPointerElementType()->isArrayTy() &&
|
||||||
|
- buf->getType()->getPointerElementType()->getArrayNumElements() >=
|
||||||
|
- size &&
|
||||||
|
- buf->getType()->getPointerElementType()->getArrayElementType() ==
|
||||||
|
- getInt8Ty());
|
||||||
|
+ assert(buf->getAllocatedType()->isArrayTy() &&
|
||||||
|
+ buf->getAllocatedType()->getArrayNumElements() >= size &&
|
||||||
|
+ buf->getAllocatedType()->getArrayElementType() == getInt8Ty());
|
||||||
|
|
||||||
|
// long bpf_get_current_comm(char *buf, int size_of_buf)
|
||||||
|
// Return: 0 on success or negative error
|
||||||
|
@@ -1077,7 +1040,7 @@ void IRBuilderBPF::CreateSignal(Value *ctx, Value *sig, const location &loc)
|
||||||
|
Instruction::IntToPtr,
|
||||||
|
getInt64(libbpf::BPF_FUNC_send_signal),
|
||||||
|
signal_func_ptr_type);
|
||||||
|
- CallInst *call = createCall(signal_func, { sig }, "signal");
|
||||||
|
+ CallInst *call = createCall(signal_func_type, signal_func, { sig }, "signal");
|
||||||
|
CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_send_signal, loc);
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -1091,7 +1054,7 @@ void IRBuilderBPF::CreateOverrideReturn(Value *ctx, Value *rc)
|
||||||
|
Constant *override_func = ConstantExpr::getCast(Instruction::IntToPtr,
|
||||||
|
getInt64(libbpf::BPF_FUNC_override_return),
|
||||||
|
override_func_ptr_type);
|
||||||
|
- createCall(override_func, { ctx, rc }, "override");
|
||||||
|
+ createCall(override_func_type, override_func, { ctx, rc }, "override");
|
||||||
|
}
|
||||||
|
|
||||||
|
CallInst *IRBuilderBPF::CreateSkbOutput(Value *skb,
|
||||||
|
@@ -1126,7 +1089,8 @@ CallInst *IRBuilderBPF::CreateSkbOutput(Value *skb,
|
||||||
|
Instruction::IntToPtr,
|
||||||
|
getInt64(libbpf::BPF_FUNC_skb_output),
|
||||||
|
skb_output_func_ptr_type);
|
||||||
|
- CallInst *call = createCall(skb_output_func,
|
||||||
|
+ CallInst *call = createCall(skb_output_func_type,
|
||||||
|
+ skb_output_func,
|
||||||
|
{ skb, map_ptr, flags, data, size_val },
|
||||||
|
"skb_output");
|
||||||
|
return call;
|
||||||
|
@@ -1320,7 +1284,8 @@ void IRBuilderBPF::CreateSeqPrintf(Value *ctx,
|
||||||
|
CreateGEP(getInt64Ty(), meta, getInt64(0)),
|
||||||
|
"seq");
|
||||||
|
|
||||||
|
- CallInst *call = createCall(seq_printf_func,
|
||||||
|
+ CallInst *call = createCall(seq_printf_func_type,
|
||||||
|
+ seq_printf_func,
|
||||||
|
{ seq, fmt, fmt_size, data, data_len },
|
||||||
|
"seq_printf");
|
||||||
|
CreateHelperErrorCond(ctx, call, libbpf::BPF_FUNC_seq_printf, loc);
|
||||||
|
diff --git a/src/ast/irbuilderbpf.h b/src/ast/irbuilderbpf.h
|
||||||
|
index e124911b..c9ffb545 100644
|
||||||
|
--- a/src/ast/irbuilderbpf.h
|
||||||
|
+++ b/src/ast/irbuilderbpf.h
|
||||||
|
@@ -90,17 +90,11 @@ public:
|
||||||
|
AddrSpace as,
|
||||||
|
const location &loc);
|
||||||
|
CallInst *CreateProbeReadStr(Value *ctx,
|
||||||
|
- AllocaInst *dst,
|
||||||
|
+ Value *dst,
|
||||||
|
llvm::Value *size,
|
||||||
|
Value *src,
|
||||||
|
AddrSpace as,
|
||||||
|
const location &loc);
|
||||||
|
- CallInst *CreateProbeReadStr(Value *ctx,
|
||||||
|
- AllocaInst *dst,
|
||||||
|
- size_t size,
|
||||||
|
- Value *src,
|
||||||
|
- AddrSpace as,
|
||||||
|
- const location &loc);
|
||||||
|
CallInst *CreateProbeReadStr(Value *ctx,
|
||||||
|
Value *dst,
|
||||||
|
size_t size,
|
||||||
|
@@ -115,7 +109,12 @@ public:
|
||||||
|
pid_t pid,
|
||||||
|
AddrSpace as,
|
||||||
|
const location &loc);
|
||||||
|
- Value *CreateStrncmp(Value *val1, Value *val2, uint64_t n, bool inverse);
|
||||||
|
+ Value *CreateStrncmp(Value *str1,
|
||||||
|
+ uint64_t str1_size,
|
||||||
|
+ Value *str2,
|
||||||
|
+ uint64_t str2_size,
|
||||||
|
+ uint64_t n,
|
||||||
|
+ bool inverse);
|
||||||
|
CallInst *CreateGetNs(bool boot_time, const location &loc);
|
||||||
|
CallInst *CreateGetPidTgid(const location &loc);
|
||||||
|
CallInst *CreateGetCurrentCgroupId(const location &loc);
|
||||||
|
@@ -131,7 +130,10 @@ public:
|
||||||
|
ArrayRef<Value *> args,
|
||||||
|
const Twine &Name,
|
||||||
|
const location *loc = nullptr);
|
||||||
|
- CallInst *createCall(Value *callee, ArrayRef<Value *> args, const Twine &Name);
|
||||||
|
+ CallInst *createCall(FunctionType *callee_type,
|
||||||
|
+ Value *callee,
|
||||||
|
+ ArrayRef<Value *> args,
|
||||||
|
+ const Twine &Name);
|
||||||
|
void CreateGetCurrentComm(Value *ctx, AllocaInst *buf, size_t size, const location& loc);
|
||||||
|
void CreatePerfEventOutput(Value *ctx,
|
||||||
|
Value *data,
|
||||||
|
@@ -185,9 +187,6 @@ private:
|
||||||
|
AddrSpace as,
|
||||||
|
const location &loc);
|
||||||
|
CallInst *createMapLookup(int mapid, Value *key);
|
||||||
|
- Constant *createProbeReadStrFn(llvm::Type *dst,
|
||||||
|
- llvm::Type *src,
|
||||||
|
- AddrSpace as);
|
||||||
|
libbpf::bpf_func_id selectProbeReadHelper(AddrSpace as, bool str);
|
||||||
|
|
||||||
|
std::map<std::string, StructType *> structs_;
|
||||||
|
diff --git a/src/ast/passes/codegen_llvm.cpp b/src/ast/passes/codegen_llvm.cpp
|
||||||
|
index a818ca0b..2b888087 100644
|
||||||
|
--- a/src/ast/passes/codegen_llvm.cpp
|
||||||
|
+++ b/src/ast/passes/codegen_llvm.cpp
|
||||||
|
@@ -1133,8 +1133,12 @@ void CodegenLLVM::visit(Call &call)
|
||||||
|
auto left_string = getString(left_arg);
|
||||||
|
auto right_string = getString(right_arg);
|
||||||
|
|
||||||
|
- expr_ = b_.CreateStrncmp(
|
||||||
|
- left_string.first, right_string.first, size, false);
|
||||||
|
+ expr_ = b_.CreateStrncmp(left_string.first,
|
||||||
|
+ left_string.second,
|
||||||
|
+ right_string.first,
|
||||||
|
+ right_string.second,
|
||||||
|
+ size,
|
||||||
|
+ false);
|
||||||
|
}
|
||||||
|
else if (call.func == "override")
|
||||||
|
{
|
||||||
|
@@ -1269,8 +1273,7 @@ void CodegenLLVM::visit(Variable &var)
|
||||||
|
else
|
||||||
|
{
|
||||||
|
auto *var_alloca = variables_[var.ident];
|
||||||
|
- expr_ = b_.CreateLoad(var_alloca->getType()->getPointerElementType(),
|
||||||
|
- var_alloca);
|
||||||
|
+ expr_ = b_.CreateLoad(var_alloca->getAllocatedType(), var_alloca);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -1310,7 +1313,12 @@ void CodegenLLVM::binop_string(Binop &binop)
|
||||||
|
auto right_string = getString(binop.right);
|
||||||
|
|
||||||
|
size_t len = std::min(left_string.second, right_string.second);
|
||||||
|
- expr_ = b_.CreateStrncmp(left_string.first, right_string.first, len, inverse);
|
||||||
|
+ expr_ = b_.CreateStrncmp(left_string.first,
|
||||||
|
+ left_string.second,
|
||||||
|
+ right_string.first,
|
||||||
|
+ right_string.second,
|
||||||
|
+ len,
|
||||||
|
+ inverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CodegenLLVM::binop_buf(Binop &binop)
|
||||||
|
@@ -1334,7 +1342,12 @@ void CodegenLLVM::binop_buf(Binop &binop)
|
||||||
|
|
||||||
|
size_t len = std::min(binop.left->type.GetSize(),
|
||||||
|
binop.right->type.GetSize());
|
||||||
|
- expr_ = b_.CreateStrncmp(left_string, right_string, len, inverse);
|
||||||
|
+ expr_ = b_.CreateStrncmp(left_string,
|
||||||
|
+ binop.left->type.GetSize(),
|
||||||
|
+ right_string,
|
||||||
|
+ binop.right->type.GetSize(),
|
||||||
|
+ len,
|
||||||
|
+ inverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CodegenLLVM::binop_int(Binop &binop)
|
||||||
|
@@ -3528,9 +3541,8 @@ void CodegenLLVM::createIncDec(Unop &unop)
|
||||||
|
else if (unop.expr->is_variable)
|
||||||
|
{
|
||||||
|
Variable &var = static_cast<Variable &>(*unop.expr);
|
||||||
|
- Value *oldval = b_.CreateLoad(
|
||||||
|
- variables_[var.ident]->getType()->getPointerElementType(),
|
||||||
|
- variables_[var.ident]);
|
||||||
|
+ Value *oldval = b_.CreateLoad(variables_[var.ident]->getAllocatedType(),
|
||||||
|
+ variables_[var.ident]);
|
||||||
|
Value *newval;
|
||||||
|
if (is_increment)
|
||||||
|
newval = b_.CreateAdd(oldval, b_.GetIntSameSize(step, oldval));
|
||||||
|
--
|
||||||
|
2.38.1
|
||||||
|
|
@ -0,0 +1,82 @@
|
|||||||
|
From 7e813d0e3048f52781199384a120f5e5cbad22ae Mon Sep 17 00:00:00 2001
|
||||||
|
From: Viktor Malik <viktor.malik@gmail.com>
|
||||||
|
Date: Mon, 5 Dec 2022 13:31:25 +0100
|
||||||
|
Subject: [PATCH] RHEL8: remove not existing attachpoints from tools
|
||||||
|
|
||||||
|
tools/bio* attempt to attach each probe to multiple kprobes to cover all
|
||||||
|
possible systems. Remove probes which do not exist in RHEL8 to remove
|
||||||
|
unnecessary warnings.
|
||||||
|
---
|
||||||
|
tools/biolatency.bt | 6 ++----
|
||||||
|
tools/biostacks.bt | 4 +---
|
||||||
|
tools/old/biosnoop.bt | 6 ++----
|
||||||
|
3 files changed, 5 insertions(+), 11 deletions(-)
|
||||||
|
|
||||||
|
diff --git a/tools/biolatency.bt b/tools/biolatency.bt
|
||||||
|
index d5af1f29..4ea910b4 100755
|
||||||
|
--- a/tools/biolatency.bt
|
||||||
|
+++ b/tools/biolatency.bt
|
||||||
|
@@ -16,14 +16,12 @@ BEGIN
|
||||||
|
printf("Tracing block device I/O... Hit Ctrl-C to end.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
-kprobe:blk_account_io_start,
|
||||||
|
-kprobe:__blk_account_io_start
|
||||||
|
+kprobe:blk_account_io_start
|
||||||
|
{
|
||||||
|
@start[arg0] = nsecs;
|
||||||
|
}
|
||||||
|
|
||||||
|
-kprobe:blk_account_io_done,
|
||||||
|
-kprobe:__blk_account_io_done
|
||||||
|
+kprobe:blk_account_io_done
|
||||||
|
/@start[arg0]/
|
||||||
|
{
|
||||||
|
@usecs = hist((nsecs - @start[arg0]) / 1000);
|
||||||
|
diff --git a/tools/biostacks.bt b/tools/biostacks.bt
|
||||||
|
index 1bc9f819..80d8cb9e 100755
|
||||||
|
--- a/tools/biostacks.bt
|
||||||
|
+++ b/tools/biostacks.bt
|
||||||
|
@@ -18,14 +18,12 @@ BEGIN
|
||||||
|
printf("Tracing block I/O with init stacks. Hit Ctrl-C to end.\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
-kprobe:blk_account_io_start,
|
||||||
|
-kprobe:__blk_account_io_start
|
||||||
|
+kprobe:blk_account_io_start
|
||||||
|
{
|
||||||
|
@reqstack[arg0] = kstack;
|
||||||
|
@reqts[arg0] = nsecs;
|
||||||
|
}
|
||||||
|
|
||||||
|
-kprobe:blk_start_request,
|
||||||
|
kprobe:blk_mq_start_request
|
||||||
|
/@reqts[arg0]/
|
||||||
|
{
|
||||||
|
diff --git a/tools/old/biosnoop.bt b/tools/old/biosnoop.bt
|
||||||
|
index 1a99643a..327251e3 100755
|
||||||
|
--- a/tools/old/biosnoop.bt
|
||||||
|
+++ b/tools/old/biosnoop.bt
|
||||||
|
@@ -22,8 +22,7 @@ BEGIN
|
||||||
|
printf("%-12s %-7s %-16s %-6s %7s\n", "TIME(ms)", "DISK", "COMM", "PID", "LAT(ms)");
|
||||||
|
}
|
||||||
|
|
||||||
|
-kprobe:blk_account_io_start,
|
||||||
|
-kprobe:__blk_account_io_start
|
||||||
|
+kprobe:blk_account_io_start
|
||||||
|
{
|
||||||
|
@start[arg0] = nsecs;
|
||||||
|
@iopid[arg0] = pid;
|
||||||
|
@@ -31,8 +30,7 @@ kprobe:__blk_account_io_start
|
||||||
|
@disk[arg0] = ((struct request *)arg0)->rq_disk->disk_name;
|
||||||
|
}
|
||||||
|
|
||||||
|
-kprobe:blk_account_io_done,
|
||||||
|
-kprobe:__blk_account_io_done
|
||||||
|
+kprobe:blk_account_io_done
|
||||||
|
/@start[arg0] != 0 && @iopid[arg0] != 0 && @iocomm[arg0] != ""/
|
||||||
|
|
||||||
|
{
|
||||||
|
--
|
||||||
|
2.38.1
|
||||||
|
|
@ -0,0 +1,173 @@
|
|||||||
|
From e661f2a043f8b6548e0bb3e0cc5992d7c0ff3b0f Mon Sep 17 00:00:00 2001
|
||||||
|
From: Rong Tao <rongtao@cestc.cn>
|
||||||
|
Date: Sat, 1 Oct 2022 16:15:27 +0800
|
||||||
|
Subject: [PATCH] tcpdrop: Fix: ERROR: Error attaching probe: 'kprobe:tcp_drop'
|
||||||
|
|
||||||
|
kernel commit 8fbf195798b5('tcp_drop() is no longer needed.') remove
|
||||||
|
the kprobe:tcp_drop, bcc commit 16eab39171eb('Add
|
||||||
|
tracepoint:skb:kfree_skb if no tcp_drop() kprobe.') already fix this
|
||||||
|
problem.
|
||||||
|
|
||||||
|
CI old kernel is too old and not support the 'reason' field, move the
|
||||||
|
old tools/tcpdrop.bt into tools/old/tcpdrop.bt and set the CI to use
|
||||||
|
it.
|
||||||
|
|
||||||
|
Since 5.17 support trace_kfree_skb(skb, ..., reason) 'reason' field.
|
||||||
|
Since 5.19 remove tcp_drop() function.
|
||||||
|
|
||||||
|
ERROR log:
|
||||||
|
|
||||||
|
$ sudo ./tcpdrop.bt
|
||||||
|
./tcpdrop.bt:49-51: WARNING: tcp_drop is not traceable (either non-existing, inlined, or marked as "notrace"); attaching to it will likely fail
|
||||||
|
Attaching 3 probes...
|
||||||
|
cannot attach kprobe, probe entry may not exist
|
||||||
|
ERROR: Error attaching probe: 'kprobe:tcp_drop'
|
||||||
|
|
||||||
|
Link: https://github.com/iovisor/bpftrace/pull/2379
|
||||||
|
Signed-off-by: Rong Tao <rongtao@cestc.cn>
|
||||||
|
---
|
||||||
|
tools/old/tcpdrop.bt | 85 ++++++++++++++++++++++++++++++++++++++++++++
|
||||||
|
tools/tcpdrop.bt | 22 ++++++------
|
||||||
|
2 files changed, 97 insertions(+), 10 deletions(-)
|
||||||
|
create mode 100755 tools/old/tcpdrop.bt
|
||||||
|
|
||||||
|
diff --git a/tools/old/tcpdrop.bt b/tools/old/tcpdrop.bt
|
||||||
|
new file mode 100755
|
||||||
|
index 00000000..685a5f6a
|
||||||
|
--- /dev/null
|
||||||
|
+++ b/tools/old/tcpdrop.bt
|
||||||
|
@@ -0,0 +1,85 @@
|
||||||
|
+#!/usr/bin/env bpftrace
|
||||||
|
+/*
|
||||||
|
+ * tcpdrop.bt Trace TCP kernel-dropped packets/segments.
|
||||||
|
+ * For Linux, uses bpftrace and eBPF.
|
||||||
|
+ *
|
||||||
|
+ * USAGE: tcpdrop.bt
|
||||||
|
+ *
|
||||||
|
+ * This is a bpftrace version of the bcc tool of the same name.
|
||||||
|
+ * It is limited to ipv4 addresses, and cannot show tcp flags.
|
||||||
|
+ *
|
||||||
|
+ * This provides information such as packet details, socket state, and kernel
|
||||||
|
+ * stack trace for packets/segments that were dropped via tcp_drop().
|
||||||
|
+
|
||||||
|
+ * WARNING: this script attaches to the tcp_drop kprobe which is likely inlined
|
||||||
|
+ * on newer kernels and not replaced by anything else, therefore
|
||||||
|
+ * the script will stop working
|
||||||
|
+ *
|
||||||
|
+ * For Linux <= 5.18.
|
||||||
|
+ *
|
||||||
|
+ * Copyright (c) 2018 Dale Hamel.
|
||||||
|
+ * Licensed under the Apache License, Version 2.0 (the "License")
|
||||||
|
+ *
|
||||||
|
+ * 23-Nov-2018 Dale Hamel created this.
|
||||||
|
+ */
|
||||||
|
+
|
||||||
|
+#ifndef BPFTRACE_HAVE_BTF
|
||||||
|
+#include <linux/socket.h>
|
||||||
|
+#include <net/sock.h>
|
||||||
|
+#else
|
||||||
|
+#include <sys/socket.h>
|
||||||
|
+#endif
|
||||||
|
+
|
||||||
|
+BEGIN
|
||||||
|
+{
|
||||||
|
+ printf("Tracing tcp drops. Hit Ctrl-C to end.\n");
|
||||||
|
+ printf("%-8s %-8s %-16s %-21s %-21s %-8s\n", "TIME", "PID", "COMM", "SADDR:SPORT", "DADDR:DPORT", "STATE");
|
||||||
|
+
|
||||||
|
+ // See https://github.com/torvalds/linux/blob/master/include/net/tcp_states.h
|
||||||
|
+ @tcp_states[1] = "ESTABLISHED";
|
||||||
|
+ @tcp_states[2] = "SYN_SENT";
|
||||||
|
+ @tcp_states[3] = "SYN_RECV";
|
||||||
|
+ @tcp_states[4] = "FIN_WAIT1";
|
||||||
|
+ @tcp_states[5] = "FIN_WAIT2";
|
||||||
|
+ @tcp_states[6] = "TIME_WAIT";
|
||||||
|
+ @tcp_states[7] = "CLOSE";
|
||||||
|
+ @tcp_states[8] = "CLOSE_WAIT";
|
||||||
|
+ @tcp_states[9] = "LAST_ACK";
|
||||||
|
+ @tcp_states[10] = "LISTEN";
|
||||||
|
+ @tcp_states[11] = "CLOSING";
|
||||||
|
+ @tcp_states[12] = "NEW_SYN_RECV";
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+kprobe:tcp_drop
|
||||||
|
+{
|
||||||
|
+ $sk = ((struct sock *) arg0);
|
||||||
|
+ $inet_family = $sk->__sk_common.skc_family;
|
||||||
|
+
|
||||||
|
+ if ($inet_family == AF_INET || $inet_family == AF_INET6) {
|
||||||
|
+ if ($inet_family == AF_INET) {
|
||||||
|
+ $daddr = ntop($sk->__sk_common.skc_daddr);
|
||||||
|
+ $saddr = ntop($sk->__sk_common.skc_rcv_saddr);
|
||||||
|
+ } else {
|
||||||
|
+ $daddr = ntop($sk->__sk_common.skc_v6_daddr.in6_u.u6_addr8);
|
||||||
|
+ $saddr = ntop($sk->__sk_common.skc_v6_rcv_saddr.in6_u.u6_addr8);
|
||||||
|
+ }
|
||||||
|
+ $lport = $sk->__sk_common.skc_num;
|
||||||
|
+ $dport = $sk->__sk_common.skc_dport;
|
||||||
|
+
|
||||||
|
+ // Destination port is big endian, it must be flipped
|
||||||
|
+ $dport = bswap($dport);
|
||||||
|
+
|
||||||
|
+ $state = $sk->__sk_common.skc_state;
|
||||||
|
+ $statestr = @tcp_states[$state];
|
||||||
|
+
|
||||||
|
+ time("%H:%M:%S ");
|
||||||
|
+ printf("%-8d %-16s ", pid, comm);
|
||||||
|
+ printf("%39s:%-6d %39s:%-6d %-10s\n", $saddr, $lport, $daddr, $dport, $statestr);
|
||||||
|
+ printf("%s\n", kstack);
|
||||||
|
+ }
|
||||||
|
+}
|
||||||
|
+
|
||||||
|
+END
|
||||||
|
+{
|
||||||
|
+ clear(@tcp_states);
|
||||||
|
+}
|
||||||
|
diff --git a/tools/tcpdrop.bt b/tools/tcpdrop.bt
|
||||||
|
index 3450a533..bb31107f 100755
|
||||||
|
--- a/tools/tcpdrop.bt
|
||||||
|
+++ b/tools/tcpdrop.bt
|
||||||
|
@@ -9,16 +9,15 @@
|
||||||
|
* It is limited to ipv4 addresses, and cannot show tcp flags.
|
||||||
|
*
|
||||||
|
* This provides information such as packet details, socket state, and kernel
|
||||||
|
- * stack trace for packets/segments that were dropped via tcp_drop().
|
||||||
|
-
|
||||||
|
- * WARNING: this script attaches to the tcp_drop kprobe which is likely inlined
|
||||||
|
- * on newer kernels and not replaced by anything else, therefore
|
||||||
|
- * the script will stop working
|
||||||
|
-
|
||||||
|
+ * stack trace for packets/segments that were dropped via kfree_skb.
|
||||||
|
+ *
|
||||||
|
+ * For Linux 5.17+ (see tools/old for script for lower versions).
|
||||||
|
+ *
|
||||||
|
* Copyright (c) 2018 Dale Hamel.
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License")
|
||||||
|
-
|
||||||
|
+ *
|
||||||
|
* 23-Nov-2018 Dale Hamel created this.
|
||||||
|
+ * 01-Oct-2022 Rong Tao use tracepoint:skb:kfree_skb
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BPFTRACE_HAVE_BTF
|
||||||
|
@@ -48,12 +47,15 @@ BEGIN
|
||||||
|
@tcp_states[12] = "NEW_SYN_RECV";
|
||||||
|
}
|
||||||
|
|
||||||
|
-kprobe:tcp_drop
|
||||||
|
+tracepoint:skb:kfree_skb
|
||||||
|
{
|
||||||
|
- $sk = ((struct sock *) arg0);
|
||||||
|
+ $reason = args->reason;
|
||||||
|
+ $skb = (struct sk_buff *)args->skbaddr;
|
||||||
|
+ $sk = ((struct sock *) $skb->sk);
|
||||||
|
$inet_family = $sk->__sk_common.skc_family;
|
||||||
|
|
||||||
|
- if ($inet_family == AF_INET || $inet_family == AF_INET6) {
|
||||||
|
+ if ($reason > SKB_DROP_REASON_NOT_SPECIFIED &&
|
||||||
|
+ ($inet_family == AF_INET || $inet_family == AF_INET6)) {
|
||||||
|
if ($inet_family == AF_INET) {
|
||||||
|
$daddr = ntop($sk->__sk_common.skc_daddr);
|
||||||
|
$saddr = ntop($sk->__sk_common.skc_rcv_saddr);
|
||||||
|
--
|
||||||
|
2.38.1
|
||||||
|
|
@ -0,0 +1,27 @@
|
|||||||
|
From 31a42a47b90f97a2a8c2446101c0007cf09288bc Mon Sep 17 00:00:00 2001
|
||||||
|
From: Viktor Malik <viktor.malik@gmail.com>
|
||||||
|
Date: Mon, 5 Dec 2022 11:57:24 +0100
|
||||||
|
Subject: [PATCH] tools/old/mdflush.bt: fix BPFTRACE_HAVE_BTF macro
|
||||||
|
|
||||||
|
The correct macro to use is called BPFTRACE_HAVE_BTF, not
|
||||||
|
__BPFTRACE_HAVE_BTF.
|
||||||
|
---
|
||||||
|
tools/old/mdflush.bt | 2 +-
|
||||||
|
1 file changed, 1 insertion(+), 1 deletion(-)
|
||||||
|
|
||||||
|
diff --git a/tools/old/mdflush.bt b/tools/old/mdflush.bt
|
||||||
|
index 921c8f1b..23c7dd51 100755
|
||||||
|
--- a/tools/old/mdflush.bt
|
||||||
|
+++ b/tools/old/mdflush.bt
|
||||||
|
@@ -15,7 +15,7 @@
|
||||||
|
* 08-Sep-2018 Brendan Gregg Created this.
|
||||||
|
*/
|
||||||
|
|
||||||
|
-#ifndef __BPFTRACE_HAVE_BTF
|
||||||
|
+#ifndef BPFTRACE_HAVE_BTF
|
||||||
|
#include <linux/genhd.h>
|
||||||
|
#include <linux/bio.h>
|
||||||
|
#endif
|
||||||
|
--
|
||||||
|
2.38.1
|
||||||
|
|
@ -1,160 +0,0 @@
|
|||||||
From 79d849a3a0462ab0a33cbf208e27e28d05eab213 Mon Sep 17 00:00:00 2001
|
|
||||||
From: Viktor Malik <viktor.malik@gmail.com>
|
|
||||||
Date: Fri, 3 Mar 2023 08:28:54 +0100
|
|
||||||
Subject: [PATCH 1/2] Parse kernel configuration
|
|
||||||
|
|
||||||
In future, it may (and will) be useful to have access to the running
|
|
||||||
kernel configuration, e.g. to add config-specific compilation options to
|
|
||||||
ClangParser.
|
|
||||||
|
|
||||||
This adds and fills a new map BPFtrace::kconfig that maps config options
|
|
||||||
to their values. Both the option name and the value are strings. The
|
|
||||||
configuration is parsed from one of two sources:
|
|
||||||
- /boot/config-$(uname -r)
|
|
||||||
- /proc/config.gz
|
|
||||||
|
|
||||||
For testing purposes, the config filename may be passed through the
|
|
||||||
BPFTRACE_KCONFIG_TEST env variable.
|
|
||||||
---
|
|
||||||
src/bpftrace.h | 1 +
|
|
||||||
src/utils.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++++
|
|
||||||
src/utils.h | 12 ++++++++++++
|
|
||||||
tests/utils.cpp | 19 +++++++++++++++++++
|
|
||||||
4 files changed, 82 insertions(+)
|
|
||||||
|
|
||||||
diff --git a/src/bpftrace.h b/src/bpftrace.h
|
|
||||||
index a6a8c00b..94587bff 100644
|
|
||||||
--- a/src/bpftrace.h
|
|
||||||
+++ b/src/bpftrace.h
|
|
||||||
@@ -168,6 +168,7 @@ public:
|
|
||||||
std::map<libbpf::bpf_func_id, location> helper_use_loc_;
|
|
||||||
// mapping traceable functions to modules (or "vmlinux") that they appear in
|
|
||||||
FuncsModulesMap traceable_funcs_;
|
|
||||||
+ KConfig kconfig;
|
|
||||||
std::vector<std::unique_ptr<AttachedProbe>> attached_probes_;
|
|
||||||
|
|
||||||
std::map<std::string, std::unique_ptr<PCAPwriter>> pcap_writers;
|
|
||||||
diff --git a/src/utils.cpp b/src/utils.cpp
|
|
||||||
index 2d9c6695..54c8f054 100644
|
|
||||||
--- a/src/utils.cpp
|
|
||||||
+++ b/src/utils.cpp
|
|
||||||
@@ -28,6 +28,7 @@
|
|
||||||
#include <bcc/bcc_syms.h>
|
|
||||||
#include <bcc/bcc_usdt.h>
|
|
||||||
#include <elf.h>
|
|
||||||
+#include <zlib.h>
|
|
||||||
|
|
||||||
#include <linux/version.h>
|
|
||||||
|
|
||||||
@@ -178,6 +179,55 @@ StdioSilencer::~StdioSilencer()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
+KConfig::KConfig()
|
|
||||||
+{
|
|
||||||
+ std::vector<std::string> config_locs;
|
|
||||||
+
|
|
||||||
+ // Try to get the config from BPFTRACE_KCONFIG_TEST env
|
|
||||||
+ // If not set, use the set of default locations
|
|
||||||
+ const char *path_env = std::getenv("BPFTRACE_KCONFIG_TEST");
|
|
||||||
+ if (path_env)
|
|
||||||
+ config_locs = { std::string(path_env) };
|
|
||||||
+ else
|
|
||||||
+ {
|
|
||||||
+ struct utsname utsname;
|
|
||||||
+ if (uname(&utsname) < 0)
|
|
||||||
+ return;
|
|
||||||
+ config_locs = {
|
|
||||||
+ "/boot/config-" + std::string(utsname.release),
|
|
||||||
+ "/proc/config.gz",
|
|
||||||
+ };
|
|
||||||
+ }
|
|
||||||
+
|
|
||||||
+ for (auto &path : config_locs)
|
|
||||||
+ {
|
|
||||||
+ // gzopen/gzgets handle both uncompressed and compressed files
|
|
||||||
+ gzFile file = gzopen(path.c_str(), "r");
|
|
||||||
+ if (!file)
|
|
||||||
+ continue;
|
|
||||||
+
|
|
||||||
+ char buf[4096];
|
|
||||||
+ while (gzgets(file, buf, sizeof(buf)))
|
|
||||||
+ {
|
|
||||||
+ std::string option(buf);
|
|
||||||
+ if (option.find("CONFIG_") == 0)
|
|
||||||
+ {
|
|
||||||
+ // trim trailing '\n'
|
|
||||||
+ if (option[option.length() - 1] == '\n')
|
|
||||||
+ option = option.substr(0, option.length() - 1);
|
|
||||||
+
|
|
||||||
+ auto split = option.find("=");
|
|
||||||
+ if (split == std::string::npos)
|
|
||||||
+ continue;
|
|
||||||
+
|
|
||||||
+ config.emplace(option.substr(0, split), option.substr(split + 1));
|
|
||||||
+ }
|
|
||||||
+ }
|
|
||||||
+ gzclose(file);
|
|
||||||
+ }
|
|
||||||
+}
|
|
||||||
+
|
|
||||||
+
|
|
||||||
bool get_uint64_env_var(const std::string &str, uint64_t &dest)
|
|
||||||
{
|
|
||||||
if (const char* env_p = std::getenv(str.c_str()))
|
|
||||||
diff --git a/src/utils.h b/src/utils.h
|
|
||||||
index dccc4504..a76aa161 100644
|
|
||||||
--- a/src/utils.h
|
|
||||||
+++ b/src/utils.h
|
|
||||||
@@ -130,6 +130,18 @@ struct DeprecatedName
|
|
||||||
typedef std::unordered_map<std::string, std::unordered_set<std::string>>
|
|
||||||
FuncsModulesMap;
|
|
||||||
|
|
||||||
+struct KConfig
|
|
||||||
+{
|
|
||||||
+ KConfig();
|
|
||||||
+ bool has_value(const std::string &name, const std::string &value) const
|
|
||||||
+ {
|
|
||||||
+ auto c = config.find(name);
|
|
||||||
+ return c != config.end() && c->second == value;
|
|
||||||
+ }
|
|
||||||
+
|
|
||||||
+ std::unordered_map<std::string, std::string> config;
|
|
||||||
+};
|
|
||||||
+
|
|
||||||
static std::vector<DeprecatedName> DEPRECATED_LIST =
|
|
||||||
{
|
|
||||||
};
|
|
||||||
diff --git a/tests/utils.cpp b/tests/utils.cpp
|
|
||||||
index 9ca4ace5..8470745b 100644
|
|
||||||
--- a/tests/utils.cpp
|
|
||||||
+++ b/tests/utils.cpp
|
|
||||||
@@ -222,6 +222,25 @@ TEST(utils, get_cgroup_path_in_hierarchy)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
+TEST(utils, parse_kconfig)
|
|
||||||
+{
|
|
||||||
+ char path[] = "/tmp/configXXXXXX";
|
|
||||||
+ int fd = mkstemp(path);
|
|
||||||
+ const std::string config = "# Intro comment\n"
|
|
||||||
+ "CONFIG_YES=y\n"
|
|
||||||
+ "CONFIG_MOD=m\n"
|
|
||||||
+ "CONFIG_VAL=42\n"
|
|
||||||
+ "# CONFIG_NO is not set";
|
|
||||||
+ EXPECT_EQ(write(fd, config.c_str(), config.length()), config.length());
|
|
||||||
+ setenv("BPFTRACE_KCONFIG_TEST", path, true);
|
|
||||||
+
|
|
||||||
+ KConfig kconfig;
|
|
||||||
+ ASSERT_TRUE(kconfig.has_value("CONFIG_YES", "y"));
|
|
||||||
+ ASSERT_TRUE(kconfig.has_value("CONFIG_MOD", "m"));
|
|
||||||
+ ASSERT_TRUE(kconfig.has_value("CONFIG_VAL", "42"));
|
|
||||||
+ ASSERT_EQ(kconfig.config.find("CONFIG_NO"), kconfig.config.end());
|
|
||||||
+}
|
|
||||||
+
|
|
||||||
} // namespace utils
|
|
||||||
} // namespace test
|
|
||||||
} // namespace bpftrace
|
|
||||||
--
|
|
||||||
2.39.2
|
|
||||||
|
|
@ -1,103 +0,0 @@
|
|||||||
From 29c5d381cd4d36b5e3ce140193729a5a4b97c31e Mon Sep 17 00:00:00 2001
|
|
||||||
From: Viktor Malik <viktor.malik@gmail.com>
|
|
||||||
Date: Mon, 6 Mar 2023 11:41:27 +0100
|
|
||||||
Subject: [PATCH 2/2] arm64: define the KASAN_SHADOW_SCALE_SHIFT macro
|
|
||||||
|
|
||||||
arm64 defines this macro from Makefile instead of defining it in a
|
|
||||||
header file as is the case for other architectures. Since we're not
|
|
||||||
running make, we need to define the macro manually via CFLAGS.
|
|
||||||
|
|
||||||
The value definition is taken from kernel's arch/arm64/Makefile and it
|
|
||||||
depends on the running kernel configuration.
|
|
||||||
|
|
||||||
This fixes the runqlat.bt tcpdrop.bt, and undump.bt tools on
|
|
||||||
aarch64+debug kernel which previously failed with:
|
|
||||||
|
|
||||||
# /usr/share/bpftrace/tools/runqlat.bt
|
|
||||||
[...]/source/arch/arm64/include/asm/memory.h:300:9: error: use of undeclared identifier 'KASAN_SHADOW_SCALE_SHIFT'
|
|
||||||
[...]
|
|
||||||
---
|
|
||||||
src/main.cpp | 3 ++-
|
|
||||||
src/utils.cpp | 23 ++++++++++++++++++-----
|
|
||||||
src/utils.h | 3 ++-
|
|
||||||
3 files changed, 22 insertions(+), 7 deletions(-)
|
|
||||||
|
|
||||||
diff --git a/src/main.cpp b/src/main.cpp
|
|
||||||
index b76d1bb3..593c71be 100644
|
|
||||||
--- a/src/main.cpp
|
|
||||||
+++ b/src/main.cpp
|
|
||||||
@@ -370,7 +370,8 @@ static std::optional<struct timespec> get_boottime()
|
|
||||||
kobj = std::get<1>(kdirs);
|
|
||||||
|
|
||||||
if (ksrc != "")
|
|
||||||
- extra_flags = get_kernel_cflags(utsname.machine, ksrc, kobj);
|
|
||||||
+ extra_flags = get_kernel_cflags(
|
|
||||||
+ utsname.machine, ksrc, kobj, bpftrace.kconfig);
|
|
||||||
}
|
|
||||||
extra_flags.push_back("-include");
|
|
||||||
extra_flags.push_back(CLANG_WORKAROUNDS_H);
|
|
||||||
diff --git a/src/utils.cpp b/src/utils.cpp
|
|
||||||
index 54c8f054..c8fd7da1 100644
|
|
||||||
--- a/src/utils.cpp
|
|
||||||
+++ b/src/utils.cpp
|
|
||||||
@@ -227,7 +227,6 @@ KConfig::KConfig()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
-
|
|
||||||
bool get_uint64_env_var(const std::string &str, uint64_t &dest)
|
|
||||||
{
|
|
||||||
if (const char* env_p = std::getenv(str.c_str()))
|
|
||||||
@@ -364,10 +363,10 @@ std::vector<int> get_possible_cpus()
|
|
||||||
return read_cpu_range("/sys/devices/system/cpu/possible");
|
|
||||||
}
|
|
||||||
|
|
||||||
-std::vector<std::string> get_kernel_cflags(
|
|
||||||
- const char* uname_machine,
|
|
||||||
- const std::string& ksrc,
|
|
||||||
- const std::string& kobj)
|
|
||||||
+std::vector<std::string> get_kernel_cflags(const char *uname_machine,
|
|
||||||
+ const std::string &ksrc,
|
|
||||||
+ const std::string &kobj,
|
|
||||||
+ const KConfig &kconfig)
|
|
||||||
{
|
|
||||||
std::vector<std::string> cflags;
|
|
||||||
std::string arch = uname_machine;
|
|
||||||
@@ -433,6 +432,20 @@ std::vector<std::string> get_kernel_cflags(
|
|
||||||
cflags.push_back("-D__LINUX_ARM_ARCH__=7");
|
|
||||||
}
|
|
||||||
|
|
||||||
+ if (arch == "arm64")
|
|
||||||
+ {
|
|
||||||
+ // arm64 defines KASAN_SHADOW_SCALE_SHIFT in a Makefile instead of defining
|
|
||||||
+ // it in a header file. Since we're not executing make, we need to set the
|
|
||||||
+ // value manually (values are taken from arch/arm64/Makefile).
|
|
||||||
+ if (kconfig.has_value("CONFIG_KASAN", "y"))
|
|
||||||
+ {
|
|
||||||
+ if (kconfig.has_value("CONFIG_KASAN_SW_TAGS", "y"))
|
|
||||||
+ cflags.push_back("-DKASAN_SHADOW_SCALE_SHIFT=4");
|
|
||||||
+ else
|
|
||||||
+ cflags.push_back("-DKASAN_SHADOW_SCALE_SHIFT=3");
|
|
||||||
+ }
|
|
||||||
+ }
|
|
||||||
+
|
|
||||||
return cflags;
|
|
||||||
}
|
|
||||||
|
|
||||||
diff --git a/src/utils.h b/src/utils.h
|
|
||||||
index a76aa161..d4eeac3d 100644
|
|
||||||
--- a/src/utils.h
|
|
||||||
+++ b/src/utils.h
|
|
||||||
@@ -178,7 +178,8 @@ std::tuple<std::string, std::string> get_kernel_dirs(
|
|
||||||
bool unpack_kheaders);
|
|
||||||
std::vector<std::string> get_kernel_cflags(const char *uname_machine,
|
|
||||||
const std::string &ksrc,
|
|
||||||
- const std::string &kobj);
|
|
||||||
+ const std::string &kobj,
|
|
||||||
+ const KConfig &kconfig);
|
|
||||||
std::string get_cgroup_path_in_hierarchy(uint64_t cgroupid,
|
|
||||||
std::string base_path);
|
|
||||||
std::vector<std::pair<std::string, std::string>> get_cgroup_hierarchy_roots();
|
|
||||||
--
|
|
||||||
2.39.2
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
|||||||
From 1632c1fb41d03730a5106696c4fed64cd4a86785 Mon Sep 17 00:00:00 2001
|
|
||||||
From: Khem Raj <raj.khem@gmail.com>
|
|
||||||
Date: Mon, 13 Mar 2023 21:30:27 -0700
|
|
||||||
Subject: [PATCH 1/3] ast: Use std::optional in CodegenLLVM::CodegenLLVM call
|
|
||||||
|
|
||||||
Fixes build with clang-16
|
|
||||||
|
|
||||||
src/ast/passes/codegen_llvm.cpp:63:53: error: use of undeclared identifier 'Optional'; did you mean 'std::optional'?
|
|
||||||
|
|
||||||
Signed-off-by: Khem Raj <raj.khem@gmail.com>
|
|
||||||
---
|
|
||||||
src/ast/passes/codegen_llvm.cpp | 16 +++++++++++-----
|
|
||||||
1 file changed, 11 insertions(+), 5 deletions(-)
|
|
||||||
|
|
||||||
diff --git a/src/ast/passes/codegen_llvm.cpp b/src/ast/passes/codegen_llvm.cpp
|
|
||||||
index 4e2a18e1..efeb4160 100644
|
|
||||||
--- a/src/ast/passes/codegen_llvm.cpp
|
|
||||||
+++ b/src/ast/passes/codegen_llvm.cpp
|
|
||||||
@@ -56,11 +56,17 @@ CodegenLLVM::CodegenLLVM(Node *root, BPFtrace &bpftrace)
|
|
||||||
throw std::runtime_error(
|
|
||||||
"Could not find bpf llvm target, does your llvm support it?");
|
|
||||||
|
|
||||||
- target_machine_.reset(target->createTargetMachine(LLVMTargetTriple,
|
|
||||||
- "generic",
|
|
||||||
- "",
|
|
||||||
- TargetOptions(),
|
|
||||||
- Optional<Reloc::Model>()));
|
|
||||||
+ target_machine_.reset(
|
|
||||||
+ target->createTargetMachine(LLVMTargetTriple,
|
|
||||||
+ "generic",
|
|
||||||
+ "",
|
|
||||||
+ TargetOptions(),
|
|
||||||
+#if LLVM_VERSION_MAJOR >= 16
|
|
||||||
+ std::optional<Reloc::Model>()
|
|
||||||
+#else
|
|
||||||
+ Optional<Reloc::Model>()
|
|
||||||
+#endif
|
|
||||||
+ ));
|
|
||||||
target_machine_->setOptLevel(llvm::CodeGenOpt::Aggressive);
|
|
||||||
|
|
||||||
module_->setTargetTriple(LLVMTargetTriple);
|
|
||||||
--
|
|
||||||
2.40.1
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
|||||||
From 6a1eb84833fd40e9aa49b610089c6ce11f38ba43 Mon Sep 17 00:00:00 2001
|
|
||||||
From: Viktor Malik <viktor.malik@gmail.com>
|
|
||||||
Date: Wed, 10 May 2023 13:30:59 +0200
|
|
||||||
Subject: [PATCH 1/2] Set cmake policy for CMP0057
|
|
||||||
|
|
||||||
Building with LLVM16 fails with the error:
|
|
||||||
CMake Warning (dev) at /usr/lib/llvm-16/lib/cmake/llvm/LLVM-Config.cmake:230 (if):
|
|
||||||
Policy CMP0057 is not set: Support new IN_LIST if() operator. Run "cmake
|
|
||||||
--help-policy CMP0057" for policy details. Use the cmake_policy command to
|
|
||||||
set the policy and suppress this warning.
|
|
||||||
|
|
||||||
IN_LIST will be interpreted as an operator when the policy is set to NEW.
|
|
||||||
Since the policy is not set the OLD behavior will be used.
|
|
||||||
Call Stack (most recent call first):
|
|
||||||
cmake/clang_libs.cmake:32 (llvm_map_components_to_libnames)
|
|
||||||
src/cc/CMakeLists.txt:126 (include)
|
|
||||||
This warning is for project developers. Use -Wno-dev to suppress it.
|
|
||||||
|
|
||||||
CMake Error at /usr/lib/llvm-16/lib/cmake/llvm/LLVM-Config.cmake:230 (if):
|
|
||||||
if given arguments:
|
|
||||||
|
|
||||||
"engine" "IN_LIST" "link_components"
|
|
||||||
|
|
||||||
Unknown arguments specified
|
|
||||||
|
|
||||||
Set cmake policy for CMP0057 explicitly.
|
|
||||||
---
|
|
||||||
CMakeLists.txt | 2 ++
|
|
||||||
1 file changed, 2 insertions(+)
|
|
||||||
|
|
||||||
diff --git a/CMakeLists.txt b/CMakeLists.txt
|
|
||||||
index 0a7914f5..045e9d97 100644
|
|
||||||
--- a/CMakeLists.txt
|
|
||||||
+++ b/CMakeLists.txt
|
|
||||||
@@ -1,6 +1,8 @@
|
|
||||||
cmake_minimum_required(VERSION 3.13.0)
|
|
||||||
project(bpftrace)
|
|
||||||
|
|
||||||
+cmake_policy(SET CMP0057 NEW)
|
|
||||||
+
|
|
||||||
# bpftrace version number components.
|
|
||||||
set(bpftrace_VERSION_MAJOR 0)
|
|
||||||
set(bpftrace_VERSION_MINOR 17)
|
|
||||||
--
|
|
||||||
2.40.1
|
|
||||||
|
|
Loading…
Reference in new issue