Skip to content

Commit

Permalink
btf: account for split BTF in loadRawSpec
Browse files Browse the repository at this point in the history
The calculation for the first type id assumes that base.firstTypeID
is zero. This is only true if base wasn't parsed from split BTF
itself. Refuse split BTF as base but use base.lastTypeID as a
defensive measure.

Signed-off-by: Lorenz Bauer <[email protected]>
  • Loading branch information
lmb committed Apr 5, 2023
1 parent e20263d commit 1ac7e2f
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,12 +233,21 @@ func loadRawSpec(btf io.ReaderAt, bo binary.ByteOrder, base *Spec) (*Spec, error
baseStrings *stringTable
baseTypes []Type
firstTypeID TypeID
err error
)

if base != nil {
if base.firstTypeID != 0 {
return nil, fmt.Errorf("can't use split BTF as base")
}

baseStrings = base.strings
baseTypes = base.types
firstTypeID = TypeID(len(baseTypes))

firstTypeID, err = base.nextTypeID()
if err != nil {
return nil, err
}
}

rawTypes, rawStrings, err := parseBTF(btf, bo, baseStrings)
Expand All @@ -257,7 +266,7 @@ func loadRawSpec(btf io.ReaderAt, bo binary.ByteOrder, base *Spec) (*Spec, error
namedTypes: typesByName,
typeIDs: typeIDs,
types: types,
firstTypeID: TypeID(len(baseTypes)),
firstTypeID: firstTypeID,
strings: rawStrings,
byteOrder: bo,
}, nil
Expand Down Expand Up @@ -598,9 +607,9 @@ func (s *Spec) Add(typ Type) (TypeID, error) {
return id, nil
}

id := s.firstTypeID + TypeID(len(s.types))
if id < s.firstTypeID {
return 0, fmt.Errorf("type ID overflow")
id, err := s.nextTypeID()
if err != nil {
return 0, err
}

s.typeIDs[typ] = id
Expand All @@ -613,6 +622,16 @@ func (s *Spec) Add(typ Type) (TypeID, error) {
return id, nil
}

// nextTypeID returns the next unallocated type ID or an error if there are no
// more type IDs.
func (s *Spec) nextTypeID() (TypeID, error) {
id := s.firstTypeID + TypeID(len(s.types))
if id < s.firstTypeID {
return 0, fmt.Errorf("no more type IDs")
}
return id, nil
}

// TypeByID returns the BTF Type with the given type ID.
//
// Returns an error wrapping ErrNotFound if a Type with the given ID
Expand Down

0 comments on commit 1ac7e2f

Please sign in to comment.