Skip to content

Commit

Permalink
btf: fix marshaling of typeTag and declTag
Browse files Browse the repository at this point in the history
Marshaling of typeTag and declTag is currently broken. This is because
we rely on marshaling vmlinux BTF for test coverage, which doesn't
contain tags since we don't compile with clang.

Signed-off-by: Lorenz Bauer <[email protected]>
  • Loading branch information
lmb authored and ti-mo committed Mar 3, 2023
1 parent b673899 commit 13c9b3b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions btf/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,10 +319,13 @@ func (e *encoder) deflateType(typ Type) (err error) {

case *declTag:
raw.SetKind(kindDeclTag)
raw.SetType(e.id(v.Type))
raw.data = &btfDeclTag{uint32(v.Index)}
raw.NameOff, err = e.strings.Add(v.Value)

case *typeTag:
raw.SetKind(kindTypeTag)
raw.SetType(e.id(v.Type))
raw.NameOff, err = e.strings.Add(v.Value)

default:
Expand Down
24 changes: 24 additions & 0 deletions btf/types_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package btf

import (
"bytes"
"fmt"
"reflect"
"testing"

"github.com/cilium/ebpf/internal"

qt "github.com/frankban/quicktest"
"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -196,6 +199,27 @@ func TestType(t *testing.T) {
}
}

func TestTagMarshaling(t *testing.T) {
for _, typ := range []Type{
&declTag{&Struct{Members: []Member{}}, "foo", -1},
&typeTag{&Int{}, "foo"},
} {
t.Run(fmt.Sprint(typ), func(t *testing.T) {
var buf bytes.Buffer
err := marshalTypes(&buf, []Type{&Void{}, typ}, nil, nil)
qt.Assert(t, err, qt.IsNil)

s, err := loadRawSpec(bytes.NewReader(buf.Bytes()), internal.NativeEndian, nil, nil)
qt.Assert(t, err, qt.IsNil)

have, err := s.TypeByID(1)
qt.Assert(t, err, qt.IsNil)

qt.Assert(t, have, qt.DeepEquals, typ)
})
}
}

func countChildren(t *testing.T, typ reflect.Type) int {
if typ.Kind() != reflect.Pointer {
t.Fatal("Expected pointer, got", typ.Kind())
Expand Down

0 comments on commit 13c9b3b

Please sign in to comment.