Skip to content

Commit 82b4446

Browse files
yonghong-songtstellar
authored andcommitted
[Clang][BPF] Type print btf_type_tag properly
When running bcc tool execsnoop ([1]) which is built with latest llvm, I hit the following error: $ sudo ./execsnoop.py /virtual/main.c:99: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 failure reason is due to that the bcc rewriter printed type like struct task_struct btf_type_tag(rcu)* where the compiler cannot recognize what 'btf_type_tag(rcu)' is. The above type is printed in [2] by UnaryOperator->getType().getAsString() (from clang) in function ProbeVisitor::VisitUnaryOperator. The original source type looks like ([3]) struct task_struct { ... struct task_struct __rcu *real_parent; ... } where '__rcu' is a macro expanding to '__attribute__((btf_type_tag("rcu")))'. Let us print btf_type_tag properly in clang so bcc tools and broader type printing will work properly. With this patch, the above rewrited source code looks like data.ppid = ({ typeof(pid_t) _val; __builtin_memset(&_val, 0, sizeof(_val)); bpf_probe_read(&_val, sizeof(_val), (void *)&({ typeof(struct task_struct __attribute__((btf_type_tag("rcu")))*) _val; __builtin_memset(&_val, 0, sizeof(_val)); bpf_probe_read(&_val, sizeof(_val), (void *)&task->real_parent); _val; })->tgid); _val; }); and execsnoop.py tool can run properly. [1] https://github.com/iovisor/bcc/blob/master/tools/exitsnoop.py [2] https://github.com/iovisor/bcc/blob/master/src/cc/frontends/clang/b_frontend_action.cc [3] https://github.com/torvalds/linux/blob/master/include/linux/sched.h Differential Revision: https://reviews.llvm.org/D150017 (cherry picked from commit 3060304)
1 parent e24a859 commit 82b4446

File tree

4 files changed

+9
-4
lines changed

4 files changed

+9
-4
lines changed

clang/lib/AST/TypePrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ void TypePrinter::printAttributedAfter(const AttributedType *T,
18121812
void TypePrinter::printBTFTagAttributedBefore(const BTFTagAttributedType *T,
18131813
raw_ostream &OS) {
18141814
printBefore(T->getWrappedType(), OS);
1815-
OS << " btf_type_tag(" << T->getAttr()->getBTFTypeTag() << ")";
1815+
OS << " __attribute__((btf_type_tag(\"" << T->getAttr()->getBTFTypeTag() << "\")))";
18161816
}
18171817

18181818
void TypePrinter::printBTFTagAttributedAfter(const BTFTagAttributedType *T,

clang/test/AST/ast-dump-bpf-attr.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %clang_cc1 -triple bpf-pc-linux-gnu -ast-dump %s \
2+
// RUN: | FileCheck --strict-whitespace %s
3+
4+
int __attribute__((btf_type_tag("rcu"))) * g;
5+
// CHECK: VarDecl{{.*}}g 'int __attribute__((btf_type_tag("rcu")))*'

clang/test/Sema/attr-btf_type_tag.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ int __tag4 * __tag5 * __tag6 *foo1(struct t __tag1 * __tag2 * __tag3 *a1) {
2828
int g1 = _Generic((int *)0, int __tag1 *: 0);
2929
int g2 = _Generic((int __tag1 *)0, int *: 0);
3030
int g3 = _Generic(0,
31-
int __tag1 * : 0, // expected-note {{compatible type 'int btf_type_tag(tag1)*' (aka 'int *') specified here}}
32-
int * : 0, // expected-error {{type 'int *' in generic association compatible with previously specified type 'int btf_type_tag(tag1)*' (aka 'int *')}}
31+
int __tag1 * : 0, // expected-note {{compatible type 'int __attribute__((btf_type_tag("tag1")))*' (aka 'int *') specified here}}
32+
int * : 0, // expected-error {{type 'int *' in generic association compatible with previously specified type 'int __attribute__((btf_type_tag("tag1")))*' (aka 'int *')}}
3333
default : 0);
3434

3535
// The btf_type_tag attribute will be ignored during overloadable type matching

clang/test/SemaCXX/sugar-common-types.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ using SBTF2 = ::SS1 [[clang::btf_type_tag("1")]];
9595
using SBTF3 = ::SS1 [[clang::btf_type_tag("2")]];
9696

9797
N t21 = 0 ? (SBTF1){} : (SBTF3){}; // expected-error {{from 'SS1'}}
98-
N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 btf_type_tag(1)' (aka 'SS1')}}
98+
N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 __attribute__((btf_type_tag("1")))' (aka 'SS1')}}
9999

100100
using QX = const SB1 *;
101101
using QY = const ::SB1 *;

0 commit comments

Comments
 (0)