]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/commitdiff
libbpf: Handle GCC noreturn-turned-volatile quirk
authorAndrii Nakryiko <andriin@fb.com>
Wed, 10 Jun 2020 05:23:35 +0000 (22:23 -0700)
committerKhalid Elmously <khalid.elmously@canonical.com>
Sat, 8 Aug 2020 05:53:12 +0000 (01:53 -0400)
BugLink: https://bugs.launchpad.net/bugs/1885322
[ Upstream commit 32022fd97ed34f6812802bf1288db27c313576f4 ]

Handle a GCC quirk of emitting extra volatile modifier in DWARF (and
subsequently preserved in BTF by pahole) for function pointers marked as
__attribute__((noreturn)). This was the way to mark such functions before GCC
2.5 added noreturn attribute. Drop such func_proto modifiers, similarly to how
it's done for array (also to handle GCC quirk/bug).

Such volatile attribute is emitted by GCC only, so existing selftests can't
express such test. Simple repro is like this (compiled with GCC + BTF
generated by pahole):

  struct my_struct {
      void __attribute__((noreturn)) (*fn)(int);
  };
  struct my_struct a;

Without this fix, output will be:

struct my_struct {
    voidvolatile  (*fn)(int);
};

With the fix:

struct my_struct {
    void (*fn)(int);
};

Fixes: 351131b51c7a ("libbpf: add btf_dump API for BTF-to-C conversion")
Reported-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Tested-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Link: https://lore.kernel.org/bpf/20200610052335.2862559-1-andriin@fb.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Kelsey Skunberg <kelsey.skunberg@canonical.com>
tools/lib/bpf/btf_dump.c

index 87f27e2664c5defcbd9bf5182ac9ef183c6ed469..d9e386b8f47ed04e7d41db0d0e24336661426603 100644 (file)
@@ -1141,6 +1141,20 @@ static void btf_dump_emit_mods(struct btf_dump *d, struct id_stack *decl_stack)
        }
 }
 
+static void btf_dump_drop_mods(struct btf_dump *d, struct id_stack *decl_stack)
+{
+       const struct btf_type *t;
+       __u32 id;
+
+       while (decl_stack->cnt) {
+               id = decl_stack->ids[decl_stack->cnt - 1];
+               t = btf__type_by_id(d->btf, id);
+               if (!btf_is_mod(t))
+                       return;
+               decl_stack->cnt--;
+       }
+}
+
 static void btf_dump_emit_name(const struct btf_dump *d,
                               const char *name, bool last_was_ptr)
 {
@@ -1239,14 +1253,7 @@ static void btf_dump_emit_type_chain(struct btf_dump *d,
                         * a const/volatile modifier for array, so we are
                         * going to silently skip them here.
                         */
-                       while (decls->cnt) {
-                               next_id = decls->ids[decls->cnt - 1];
-                               next_t = btf__type_by_id(d->btf, next_id);
-                               if (btf_is_mod(next_t))
-                                       decls->cnt--;
-                               else
-                                       break;
-                       }
+                       btf_dump_drop_mods(d, decls);
 
                        if (decls->cnt == 0) {
                                btf_dump_emit_name(d, fname, last_was_ptr);
@@ -1274,7 +1281,15 @@ static void btf_dump_emit_type_chain(struct btf_dump *d,
                        __u16 vlen = btf_vlen(t);
                        int i;
 
-                       btf_dump_emit_mods(d, decls);
+                       /*
+                        * GCC emits extra volatile qualifier for
+                        * __attribute__((noreturn)) function pointers. Clang
+                        * doesn't do it. It's a GCC quirk for backwards
+                        * compatibility with code written for GCC <2.5. So,
+                        * similarly to extra qualifiers for array, just drop
+                        * them, instead of handling them.
+                        */
+                       btf_dump_drop_mods(d, decls);
                        if (decls->cnt) {
                                btf_dump_printf(d, " (");
                                btf_dump_emit_type_chain(d, decls, fname, lvl);