Skip to content

Commit

Permalink
btf: remove fixupAndAssert
Browse files Browse the repository at this point in the history
fixupAndAssert in inflateRawTypes is used to assert that a Type
value has a certain concrete type. The only use is validating
that Func.Type is a FuncProto so that a later check of Func.Type
can type assert without error checking.
This is a bad idea since understanding why the assertion is safe
now requires understanding how types are inflated.

Remove fixupAndAssert and instead check for errors during the
type assertion.

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb committed Oct 18, 2023
1 parent 2952c23 commit d28b2be
Showing 1 changed file with 10 additions and 35 deletions.
45 changes: 10 additions & 35 deletions btf/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"io"
"math"
"reflect"
"strings"

"github.com/cilium/ebpf/asm"
Expand Down Expand Up @@ -758,43 +757,22 @@ func inflateRawTypes(rawTypes []rawType, rawStrings *stringTable, base *Spec) ([
}

var fixups []fixupDef
fixup := func(id TypeID, typ *Type) bool {
fixup := func(id TypeID, typ *Type) {
if id < firstTypeID {
if baseType, err := base.TypeByID(id); err == nil {
*typ = baseType
return true
return
}
}

idx := int(id - firstTypeID)
if idx < len(types) {
// We've already inflated this type, fix it up immediately.
*typ = types[idx]
return true
return
}

fixups = append(fixups, fixupDef{id, typ})
return false
}

type assertion struct {
id TypeID
typ *Type
want reflect.Type
}

var assertions []assertion
fixupAndAssert := func(id TypeID, typ *Type, want reflect.Type) error {
if !fixup(id, typ) {
assertions = append(assertions, assertion{id, typ, want})
return nil
}

// The type has already been fixed up, check the type immediately.
if reflect.TypeOf(*typ) != want {
return fmt.Errorf("type ID %d: expected %s, got %T", id, want, *typ)
}
return nil
}

type bitfieldFixupDef struct {
Expand Down Expand Up @@ -955,9 +933,7 @@ func inflateRawTypes(rawTypes []rawType, rawStrings *stringTable, base *Spec) ([

case kindFunc:
fn := &Func{name, nil, raw.Linkage()}
if err := fixupAndAssert(raw.Type(), &fn.Type, reflect.TypeOf((*FuncProto)(nil))); err != nil {
return nil, err
}
fixup(raw.Type(), &fn.Type)
typ = fn

case kindFuncProto:
Expand Down Expand Up @@ -1066,12 +1042,6 @@ func inflateRawTypes(rawTypes []rawType, rawStrings *stringTable, base *Spec) ([
}
}

for _, assertion := range assertions {
if reflect.TypeOf(*assertion.typ) != assertion.want {
return nil, fmt.Errorf("type ID %d: expected %s, got %T", assertion.id, assertion.want, *assertion.typ)
}
}

for _, dt := range declTags {
switch t := dt.Type.(type) {
case *Var, *Typedef:
Expand All @@ -1085,7 +1055,12 @@ func inflateRawTypes(rawTypes []rawType, rawStrings *stringTable, base *Spec) ([
}

case *Func:
if dt.Index >= len(t.Type.(*FuncProto).Params) {
fp, ok := t.Type.(*FuncProto)
if !ok {
return nil, fmt.Errorf("type %s: %s is not a FuncProto", dt, t.Type)
}

if dt.Index >= len(fp.Params) {
return nil, fmt.Errorf("type %s: index %d exceeds params of %s", dt, dt.Index, t)
}

Expand Down

0 comments on commit d28b2be

Please sign in to comment.