-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
is bcc object initialization thread-safe #4500
Labels
Comments
|
Could you try the following diff ? diff --git a/src/cc/frontends/clang/b_frontend_action.cc b/src/cc/frontends/clang/b_frontend_action.cc
index e9c83a96..0e27753f 100644
--- a/src/cc/frontends/clang/b_frontend_action.cc
+++ b/src/cc/frontends/clang/b_frontend_action.cc
@@ -123,17 +123,8 @@ const char *pt_regs_syscall_regs(void) {
return calling_conv_regs[0];
}
-/* Use resolver only once per translation */
-static void *kresolver = NULL;
-static void *get_symbol_resolver(void) {
- if (!kresolver)
- kresolver = bcc_symcache_new(-1, nullptr);
- return kresolver;
-}
-
-static std::string check_bpf_probe_read_kernel(void) {
+static std::string check_bpf_probe_read_kernel(void *resolver) {
bool is_probe_read_kernel;
- void *resolver = get_symbol_resolver();
uint64_t addr = 0;
is_probe_read_kernel = bcc_symcache_resolve_name(resolver, nullptr,
"bpf_probe_read_kernel", &addr) >= 0 ? true: false;
@@ -147,11 +138,10 @@ static std::string check_bpf_probe_read_kernel(void) {
}
static std::string check_bpf_probe_read_user(llvm::StringRef probe,
- bool& overlap_addr) {
+ bool& overlap_addr, void *resolver) {
if (probe.str() == "bpf_probe_read_user" ||
probe.str() == "bpf_probe_read_user_str") {
// Check for probe_user symbols in backported kernel before fallback
- void *resolver = get_symbol_resolver();
uint64_t addr = 0;
bool found = bcc_symcache_resolve_name(resolver, nullptr,
"bpf_probe_read_user", &addr) >= 0 ? true: false;
@@ -1176,7 +1166,7 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) {
// space arch.
bool overlap_addr = false;
std::string probe = check_bpf_probe_read_user(Decl->getName(),
- overlap_addr);
+ overlap_addr, fe_.get_resolver());
if (overlap_addr) {
error(GET_BEGINLOC(Call), "bpf_probe_read_user not found. Use latest kernel");
return false;
@@ -1232,7 +1222,7 @@ bool BTypeVisitor::VisitCallExpr(CallExpr *Call) {
bool overlap_addr = false;
text += check_bpf_probe_read_user(StringRef("bpf_probe_read_user"),
- overlap_addr);
+ overlap_addr, fe_.get_resolver());
if (overlap_addr) {
error(GET_BEGINLOC(Call), "bpf_probe_read_user not found. Use latest kernel");
return false;
@@ -1751,7 +1741,20 @@ BFrontendAction::BFrontendAction(
mod_src_(mod_src),
next_fake_fd_(-1),
fake_fd_map_(fake_fd_map),
- perf_events_(perf_events) {}
+ perf_events_(perf_events),
+ resolver(nullptr) {}
+
+void *BFrontendAction::get_resolver() {
+ if (!resolver)
+ resolver = bcc_symcache_new(-1, nullptr);
+ return resolver;
+}
+
+BFrontendAction::~BFrontendAction() {
+ if (resolver) {
+ bcc_free_symcache(resolver, -1);
+ }
+}
bool BFrontendAction::is_rewritable_ext_func(FunctionDecl *D) {
StringRef file_name = rewriter_->getSourceMgr().getFilename(GET_BEGINLOC(D));
@@ -1766,11 +1769,7 @@ void BFrontendAction::DoMiscWorkAround() {
// to guard certain fields. The workaround here intends to define
// CONFIG_CC_STACKPROTECTOR properly based on other configs, so it relieved any bpf
// program (using task_struct, etc.) of patching the below code.
- std::string probefunc = check_bpf_probe_read_kernel();
- if (kresolver) {
- bcc_free_symcache(kresolver, -1);
- kresolver = NULL;
- }
+ std::string probefunc = check_bpf_probe_read_kernel(resolver);
if (probefunc == "bpf_probe_read") {
probefunc = "#define bpf_probe_read_kernel bpf_probe_read\n"
"#define bpf_probe_read_kernel_str bpf_probe_read_str\n"
diff --git a/src/cc/frontends/clang/b_frontend_action.h b/src/cc/frontends/clang/b_frontend_action.h
index c2881d60..48fa583a 100644
--- a/src/cc/frontends/clang/b_frontend_action.h
+++ b/src/cc/frontends/clang/b_frontend_action.h
@@ -159,6 +159,7 @@ class BFrontendAction : public clang::ASTFrontendAction {
ProgFuncInfo &prog_func_info, std::string &mod_src,
const std::string &maps_ns, fake_fd_map_def &fake_fd_map,
std::map<std::string, std::vector<std::string>> &perf_events);
+ ~BFrontendAction();
// Called by clang when the AST has been completed, here the output stream
// will be flushed.
@@ -180,6 +181,7 @@ class BFrontendAction : public clang::ASTFrontendAction {
std::string> map_def) {
fake_fd_map_[fd] = move(map_def);
}
+ void *get_resolver();
private:
llvm::raw_ostream &os_;
@@ -197,6 +199,7 @@ class BFrontendAction : public clang::ASTFrontendAction {
int next_fake_fd_;
fake_fd_map_def &fake_fd_map_;
std::map<std::string, std::vector<std::string>> &perf_events_;
+ void *resolver;
};
} // namespace visitor |
I imported the diff file into the local code, recompiled and installed bcc, and tested it again without any core dump. However, there is still a problem with header file not found when multiple
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Different errors occur when a process compiles ebpf concurrently using bcc。
e.g.
Sometimes a coredump occurs.
demo.cpp
The text was updated successfully, but these errors were encountered: