From b875343873a584965daf507d73ff1fe71eab1953 Mon Sep 17 00:00:00 2001 From: Yonghong Song Date: Mon, 20 Sep 2021 16:23:46 -0700 Subject: [PATCH] [Clang] Ignore BTFTag attr if used as a type attribute Currently, linux kernel has a __user attribute ([1]) defined as __attribute__((noderef, address_space(__user))) which is used by sparse tool ([2]) to do some type checking of pointers to user space memory. During normal compilation, __user will be defined to nothing so it won't have an impact on compilation. The btf_tag attribute, which is motivated by carrying linux kernel annotations into dwarf/BTF, is introduced in [3]. We intended to define __user as __attribute__((btf_tag("user"))) so such information will be encoded in dwarf/BTF and can be used later by bpf verification or other tracing tools. But linux kernel __user attribute is also used during type conversion which btf_tag doesn't support ([4]) since such type conversion is only used for compiler analysis and not encoded in dwarf/btf. Theoretically, it is possible for clang to understand these tags and do a sparse-like type checking work. But I would like to leave that to future work and for now suggest simply ignore these btf_tag attributes if they are used as type attributes. [1] https://github.com/torvalds/linux/blob/master/include/linux/compiler_types.h#L10 [2] https://sparse.docs.kernel.org/en/latest/ [3] https://reviews.llvm.org/D106614 [4] https://github.com/torvalds/linux/blob/master/fs/binfmt_flat.c#L135 Differential Revision: https://reviews.llvm.org/D110116 --- clang/include/clang/Basic/Attr.td | 2 +- clang/include/clang/Basic/AttrDocs.td | 4 ++++ clang/lib/AST/TypePrinter.cpp | 1 + clang/lib/Sema/SemaType.cpp | 6 ++++++ clang/test/Sema/attr-btf_tag.c | 8 ++++++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index a540395..00e847b 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -1835,7 +1835,7 @@ def BPFPreserveAccessIndex : InheritableAttr, let LangOpts = [COnly]; } -def BTFTag : InheritableAttr { +def BTFTag : DeclOrTypeAttr { let Spellings = [Clang<"btf_tag">]; let Args = [StringArgument<"BTFTag">]; let Subjects = SubjectList<[Var, Function, Record, Field], ErrorDiag>; diff --git a/clang/include/clang/Basic/AttrDocs.td b/clang/include/clang/Basic/AttrDocs.td index b2b41c3..d3b0de0 100644 --- a/clang/include/clang/Basic/AttrDocs.td +++ b/clang/include/clang/Basic/AttrDocs.td @@ -2019,6 +2019,10 @@ targets. This attribute may be attached to a struct/union, struct/union field, function, function parameter or variable declaration. If -g is specified, the ``ARGUMENT`` info will be preserved in IR and be emitted to dwarf. For BPF targets, the ``ARGUMENT`` info will be emitted to .BTF ELF section too. + +The attribute can also be used as a type qualifier. Right now it is accepted +and silently ignored in order to permit the linux kernel to build with the +attribute. }]; } diff --git a/clang/lib/AST/TypePrinter.cpp b/clang/lib/AST/TypePrinter.cpp index 3c7a6b8..2805916 100644 --- a/clang/lib/AST/TypePrinter.cpp +++ b/clang/lib/AST/TypePrinter.cpp @@ -1704,6 +1704,7 @@ void TypePrinter::printAttributedAfter(const AttributedType *T, case attr::UPtr: case attr::AddressSpace: case attr::CmseNSCall: + case attr::BTFTag: llvm_unreachable("This attribute should have been handled already"); case attr::NSReturnsRetained: diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index d2d5428..11a6d3f 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -8127,6 +8127,12 @@ static void processTypeAttrs(TypeProcessingState &state, QualType &type, case ParsedAttr::IgnoredAttribute: break; + case ParsedAttr::AT_BTFTag: + // FIXME: Linux kernel may also use this attribute for type casting check, + // which clang doesn's support for now. Let us ignore them so linux kernel + // build won't break. + attr.setUsedAsTypeAttr(); + break; case ParsedAttr::AT_MayAlias: // FIXME: This attribute needs to actually be handled, but if we ignore // it it breaks large amounts of Linux software. diff --git a/clang/test/Sema/attr-btf_tag.c b/clang/test/Sema/attr-btf_tag.c index 88452fa..2ba2515 100644 --- a/clang/test/Sema/attr-btf_tag.c +++ b/clang/test/Sema/attr-btf_tag.c @@ -40,3 +40,11 @@ int __tag2 __tag3 foo(struct t1 *arg, struct t2 *arg2); int __tag1 foo(struct t1 *arg __tag1, struct t2 *arg2) { return arg->a + arg2->a; } + +void * convert(long arg) { + /* FIXME: the attribute __tag1 is accepted but didn't really do type conversion + * or enforce type checking. This is to permit linux kernel build with btf_tag + * attribute. + */ + return (void __tag1 *)arg; +} -- 2.7.4