This is an archive of the discontinued LLVM Phabricator instance.

[Clang] Ignore BTFTag attr if used as a type attribute
ClosedPublic

Authored by yonghong-song on Sep 20 2021, 5:03 PM.

Details

Summary

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

Diff Detail

Event Timeline

yonghong-song requested review of this revision.Sep 20 2021, 5:03 PM
yonghong-song created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptSep 20 2021, 5:03 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
aaron.ballman added inline comments.Sep 21 2021, 5:43 AM
clang/lib/Sema/SemaType.cpp
8130

Errr, this attribute isn't a type attribute in the first place, so I'd feel more comfortable if this was also modified in Attr.td to be a DeclOrTypeAttr instead of an InheritableAttr.

There should also be a change to AttrDocs.td to explain what's going on.

clang/test/Sema/attr-btf_tag.c
45

I'd appreciate a FIXME comment here about why this is silently accepted but does nothing.

  • change to use DeclOrTypeAttr in Attr.td to indicate the attribute can be used for declarations or type qualifier.
  • add explanation of why the change in AttrDocs.td and the test.

@aaron.ballman Addressed your comments, please take a look. Thanks!

aaron.ballman accepted this revision.Sep 22 2021, 1:02 PM

LGTM aside from a minor nit with documentation grammar.

clang/include/clang/Basic/AttrDocs.td
2024
This revision is now accepted and ready to land.Sep 22 2021, 1:02 PM
  • to adjust some wording in AttrDocs.td based on Aaron's suggestion.
This revision was landed with ongoing or failed builds.Sep 22 2021, 2:11 PM
This revision was automatically updated to reflect the committed changes.