Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

btf: export DeclTag and TypeTag #1548

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ TARGETS := \
btf/testdata/relocs_read \
btf/testdata/relocs_read_tgt \
btf/testdata/relocs_enum \
btf/testdata/tags \
cmd/bpf2go/testdata/minimal

.PHONY: all clean container-all container-shell generate
Expand Down
8 changes: 8 additions & 0 deletions btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,5 +695,13 @@ func (iter *TypesIterator) Next() bool {
iter.Type, ok = iter.spec.typeByID(iter.id)
iter.id++
iter.done = !ok
if !iter.done {
// Skip declTags, during unmarshaling declTags become `Tags` fields of other types.
// We keep them in the spec to avoid holes in the ID space, but for the purposes of
// iteration, they are not useful to the user.
if _, ok := iter.Type.(*declTag); ok {
return iter.Next()
}
}
return !iter.done
}
4 changes: 2 additions & 2 deletions btf/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func (e *encoder) deflateType(typ Type) (err error) {
raw.data = &btfDeclTag{uint32(v.Index)}
raw.NameOff, err = e.strings.Add(v.Value)

case *typeTag:
case *TypeTag:
raw.SetKind(kindTypeTag)
raw.SetType(e.id(v.Type))
raw.NameOff, err = e.strings.Add(v.Value)
Expand Down Expand Up @@ -521,7 +521,7 @@ func (e *encoder) deflateEnum64(raw *rawType, enum *Enum) (err error) {
})
}

return e.deflateUnion(raw, &Union{enum.Name, enum.Size, members})
return e.deflateUnion(raw, &Union{enum.Name, enum.Size, members, nil})
}

raw.SetKind(kindEnum64)
Expand Down
4 changes: 2 additions & 2 deletions btf/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestBuilderMarshal(t *testing.T) {
(*Void)(nil),
typ,
&Pointer{typ},
&Typedef{"baz", typ},
&Typedef{"baz", typ, nil},
}

b, err := NewBuilder(want)
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestBuilderAdd(t *testing.T) {
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(id, TypeID(2)), qt.Commentf("Adding a type twice returns different ids"))

id, err = b.Add(&Typedef{"baz", i})
id, err = b.Add(&Typedef{"baz", i, nil})
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(id, TypeID(3)))
}
Expand Down
Binary file added btf/testdata/tags-eb.elf
Binary file not shown.
Binary file added btf/testdata/tags-el.elf
Binary file not shown.
37 changes: 37 additions & 0 deletions btf/testdata/tags.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "../../testdata/common.h"

#define tagA __attribute__((btf_decl_tag("a")))
#define tagB __attribute__((btf_decl_tag("b")))
#define tagC __attribute__((btf_decl_tag("c")))
#define tagD __attribute__((btf_decl_tag("d")))
#define tagE __attribute__((btf_decl_tag("e")))

struct s {
char tagA foo;
char tagB bar;
} tagC;

union u {
char tagA foo;
char tagB bar;
} tagC;

typedef tagB char td;

struct s tagD s1;
union u tagE u1;
td tagA t1;

int tagA tagB fwdDecl(char tagC x, char tagD y);

int tagE normalDecl1(char tagB x, char tagC y) {
return fwdDecl(x, y);
}

int tagE normalDecl2(char tagB x, char tagC y) {
return fwdDecl(x, y);
}

dylandreimerink marked this conversation as resolved.
Show resolved Hide resolved
__section("syscall") int prog(char *ctx) {
return normalDecl1(ctx[0], ctx[1]) + normalDecl2(ctx[2], ctx[3]);
}
48 changes: 42 additions & 6 deletions btf/traversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ func children(typ Type, yield func(child *Type) bool) bool {
// Explicitly type switch on the most common types to allow the inliner to
// do its work. This avoids allocating intermediate slices from walk() on
// the heap.
var tags []string
switch v := typ.(type) {
case *Void, *Int, *Enum, *Fwd, *Float:
case *Void, *Int, *Enum, *Fwd, *Float, *declTag:
ti-mo marked this conversation as resolved.
Show resolved Hide resolved
// No children to traverse.
dylandreimerink marked this conversation as resolved.
Show resolved Hide resolved
// declTags is declared as a leaf type since it's parsed into .Tags fields of other types
// during unmarshaling.
case *Pointer:
if !yield(&v.Target) {
return false
Expand All @@ -59,17 +62,32 @@ func children(typ Type, yield func(child *Type) bool) bool {
if !yield(&v.Members[i].Type) {
return false
}
for _, t := range v.Members[i].Tags {
var tag Type = &declTag{v, t, i}
if !yield(&tag) {
return false
}
}
}
tags = v.Tags
case *Union:
for i := range v.Members {
if !yield(&v.Members[i].Type) {
return false
}
for _, t := range v.Members[i].Tags {
var tag Type = &declTag{v, t, i}
if !yield(&tag) {
return false
}
}
}
tags = v.Tags
case *Typedef:
if !yield(&v.Type) {
return false
}
tags = v.Tags
case *Volatile:
if !yield(&v.Type) {
return false
Expand All @@ -86,6 +104,20 @@ func children(typ Type, yield func(child *Type) bool) bool {
if !yield(&v.Type) {
return false
}
if fp, ok := v.Type.(*FuncProto); ok {
for i := range fp.Params {
if len(v.ParamTags) <= i {
continue
}
for _, t := range v.ParamTags[i] {
var tag Type = &declTag{v, t, i}
if !yield(&tag) {
return false
}
}
}
}
tags = v.Tags
case *FuncProto:
if !yield(&v.Return) {
return false
Expand All @@ -99,17 +131,14 @@ func children(typ Type, yield func(child *Type) bool) bool {
if !yield(&v.Type) {
return false
}
tags = v.Tags
case *Datasec:
for i := range v.Vars {
if !yield(&v.Vars[i].Type) {
return false
}
}
case *declTag:
if !yield(&v.Type) {
return false
}
case *typeTag:
case *TypeTag:
if !yield(&v.Type) {
return false
}
Expand All @@ -119,5 +148,12 @@ func children(typ Type, yield func(child *Type) bool) bool {
panic(fmt.Sprintf("don't know how to walk Type %T", v))
}

for _, t := range tags {
var tag Type = &declTag{typ, t, -1}
if !yield(&tag) {
return false
}
}

return true
}
Loading
Loading