Skip to content

Commit

Permalink
btf: fix Spec.TypeByID for split BTF
Browse files Browse the repository at this point in the history
TypeByID currently assumes that the first ID in s.types is 0, which
is not correct when dealing with split BTF.
  • Loading branch information
lmb committed Nov 20, 2022
1 parent 3736f88 commit 575c5e9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 8 additions & 1 deletion btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,14 @@ func (s *Spec) Add(typ Type) (TypeID, error) {
// Returns an error wrapping ErrNotFound if a Type with the given ID
// does not exist in the Spec.
func (s *Spec) TypeByID(id TypeID) (Type, error) {
return s.types.ByID(id)
firstID := s.firstTypeID()
i := int(id - firstID)

if id < firstID || i >= len(s.types) {
return nil, fmt.Errorf("type ID %d: %w", id, ErrNotFound)
}

return s.types[i], nil
}

// TypeID returns the ID for a given Type.
Expand Down
5 changes: 5 additions & 0 deletions btf/btf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,11 @@ func TestLoadSplitSpecFromReader(t *testing.T) {
if err != nil {
t.Fatal(err)
}

typeByID, err := splitSpec.TypeByID(typeID)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, typeByID, qt.Equals, typ)

fnType := typ.(*Func)
fnProto := fnType.Type.(*FuncProto)

Expand Down

0 comments on commit 575c5e9

Please sign in to comment.