diff options
author | Yonghong Song <yhs@fb.com> | 2023-05-05 14:41:13 -0700 |
---|---|---|
committer | Tom Stellard <tstellar@redhat.com> | 2023-05-16 20:05:03 -0700 |
commit | 82b4446f25d4b4a0b22f5d4c067c6a920045338a (patch) | |
tree | 3f3226ea3f3b08969131dbe48124f9b1927d6728 | |
parent | e24a8596a81b904db37d114f91200d7b5cf89683 (diff) | |
download | llvm-82b4446f25d4b4a0b22f5d4c067c6a920045338a.tar.gz |
[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 3060304906f08f933672f0a30cc57d5f09766444)
-rw-r--r-- | clang/lib/AST/TypePrinter.cpp | 2 | ||||
-rw-r--r-- | clang/test/AST/ast-dump-bpf-attr.c | 5 | ||||
-rw-r--r-- | clang/test/Sema/attr-btf_type_tag.c | 4 | ||||
-rw-r--r-- | clang/test/SemaCXX/sugar-common-types.cpp | 2 |
4 files changed, 9 insertions, 4 deletions
diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp index 5c2464904485..2d06faeca182 100644 --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -1812,7 +1812,7 @@ void TypePrinter::printAttributedAfter(const AttributedType *T, void TypePrinter::printBTFTagAttributedBefore(const BTFTagAttributedType *T, raw_ostream &OS) { printBefore(T->getWrappedType(), OS); - OS << " btf_type_tag(" << T->getAttr()->getBTFTypeTag() << ")"; + OS << " __attribute__((btf_type_tag(\"" << T->getAttr()->getBTFTypeTag() << "\")))"; } void TypePrinter::printBTFTagAttributedAfter(const BTFTagAttributedType *T, diff --git a/clang/test/AST/ast-dump-bpf-attr.c b/clang/test/AST/ast-dump-bpf-attr.c new file mode 100644 index 000000000000..8899cecbb6e9 --- /dev/null +++ b/clang/test/AST/ast-dump-bpf-attr.c @@ -0,0 +1,5 @@ +// RUN: %clang_cc1 -triple bpf-pc-linux-gnu -ast-dump %s \ +// RUN: | FileCheck --strict-whitespace %s + +int __attribute__((btf_type_tag("rcu"))) * g; +// CHECK: VarDecl{{.*}}g 'int __attribute__((btf_type_tag("rcu")))*' diff --git a/clang/test/Sema/attr-btf_type_tag.c b/clang/test/Sema/attr-btf_type_tag.c index f7ba29b0aa65..a3ef404f3823 100644 --- a/clang/test/Sema/attr-btf_type_tag.c +++ b/clang/test/Sema/attr-btf_type_tag.c @@ -28,8 +28,8 @@ int __tag4 * __tag5 * __tag6 *foo1(struct t __tag1 * __tag2 * __tag3 *a1) { int g1 = _Generic((int *)0, int __tag1 *: 0); int g2 = _Generic((int __tag1 *)0, int *: 0); int g3 = _Generic(0, - int __tag1 * : 0, // expected-note {{compatible type 'int btf_type_tag(tag1)*' (aka 'int *') specified here}} - int * : 0, // expected-error {{type 'int *' in generic association compatible with previously specified type 'int btf_type_tag(tag1)*' (aka 'int *')}} + int __tag1 * : 0, // expected-note {{compatible type 'int __attribute__((btf_type_tag("tag1")))*' (aka 'int *') specified here}} + int * : 0, // expected-error {{type 'int *' in generic association compatible with previously specified type 'int __attribute__((btf_type_tag("tag1")))*' (aka 'int *')}} default : 0); // The btf_type_tag attribute will be ignored during overloadable type matching diff --git a/clang/test/SemaCXX/sugar-common-types.cpp b/clang/test/SemaCXX/sugar-common-types.cpp index 1932ccd9c725..cd9b3dff6982 100644 --- a/clang/test/SemaCXX/sugar-common-types.cpp +++ b/clang/test/SemaCXX/sugar-common-types.cpp @@ -95,7 +95,7 @@ using SBTF2 = ::SS1 [[clang::btf_type_tag("1")]]; using SBTF3 = ::SS1 [[clang::btf_type_tag("2")]]; N t21 = 0 ? (SBTF1){} : (SBTF3){}; // expected-error {{from 'SS1'}} -N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 btf_type_tag(1)' (aka 'SS1')}} +N t22 = 0 ? (SBTF1){} : (SBTF2){}; // expected-error {{from 'SS1 __attribute__((btf_type_tag("1")))' (aka 'SS1')}} using QX = const SB1 *; using QY = const ::SB1 *; |