Skip to content

Commit

Permalink
Fix btf_type_tag issue with llvm 15
Browse files Browse the repository at this point in the history
With llvm15, the tool 'execsnoop.py' failed the compilation with
the following error messages:

  ...
  /virtual/main.c:103:157: error: expected ')'
      data.ppid = ({ typeof(pid_t) _val; __builtin_memset(&_val, 0, sizeof(_val));
         bpf_probe_read(&_val, sizeof(_val), (void *)&({ typeof(struct task_struct  btf_type_tag(rcu)*) _val;
          __builtin_memset(&_val, 0, sizeof(_val));
          bpf_probe_read(&_val, sizeof(_val), (void *)&task->real_parent); _val; })->tgid); _val; });
  ...

The main reason is incorrect type string
  btf_type_tag(rcu)
btf_type_tag is introduced in llvm15 is an attribute. The correct type representation
should be
  __attribute__((btf_type_tag("rcu")))

The bug is fixed in llvm16 with [1]. Unfortunately the patch cannot be backported to llvm15 since
at that time llvm15 has been freezed.

This patch manually fixed this issue for bcc.

  [1] llvm/llvm-project@82b4446

Signed-off-by: Yonghong Song <[email protected]>
  • Loading branch information
Yonghong Song authored and yonghong-song committed Mar 17, 2024
1 parent c00d69c commit 57ca51b
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/cc/frontends/clang/b_frontend_action.cc
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,27 @@ bool ProbeVisitor::VisitBinaryOperator(BinaryOperator *E) {
}
return true;
}

static std::string FixBTFTypeTag(std::string TypeStr)
{
#if LLVM_VERSION_MAJOR == 15
std::map<std::string, std::string> TypePair =
{{"btf_type_tag(user)", "__attribute__((btf_type_tag(\"user\")))"},
{"btf_type_tag(rcu)", "__attribute__((btf_type_tag(\"rcu\")))"},
{"btf_type_tag(percpu)", "__attribute__((btf_type_tag(\"percpu\")))"}};

for (auto T: TypePair) {
size_t index;
index = TypeStr.find(T.first, 0);
if (index != std::string::npos) {
TypeStr.replace(index, T.first.size(), T.second);
return TypeStr;
}
}
#endif
return TypeStr;
}

bool ProbeVisitor::VisitUnaryOperator(UnaryOperator *E) {
if (E->getOpcode() == UO_AddrOf) {
addrof_stmt_ = E;
Expand Down Expand Up @@ -598,7 +619,7 @@ bool ProbeVisitor::VisitMemberExpr(MemberExpr *E) {
string rhs = rewriter_.getRewrittenText(expansionRange(SourceRange(rhs_start, GET_ENDLOC(E))));
string base_type = base->getType()->getPointeeType().getAsString();
string pre, post;
pre = "({ typeof(" + E->getType().getAsString() + ") _val; __builtin_memset(&_val, 0, sizeof(_val));";
pre = "({ typeof(" + FixBTFTypeTag(E->getType().getAsString()) + ") _val; __builtin_memset(&_val, 0, sizeof(_val));";
if (cannot_fall_back_safely)
pre += " bpf_probe_read_kernel(&_val, sizeof(_val), (void *)&";
else
Expand Down Expand Up @@ -652,7 +673,7 @@ bool ProbeVisitor::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
if (rewriter_.getRewrittenText(lbracket_range).size() == 0)
return true;

pre = "({ typeof(" + E->getType().getAsString() + ") _val; __builtin_memset(&_val, 0, sizeof(_val));";
pre = "({ typeof(" + FixBTFTypeTag(E->getType().getAsString()) + ") _val; __builtin_memset(&_val, 0, sizeof(_val));";
if (cannot_fall_back_safely)
pre += " bpf_probe_read_kernel(&_val, sizeof(_val), (void *)((";
else
Expand Down

0 comments on commit 57ca51b

Please sign in to comment.