diff --git a/btf/marshal.go b/btf/marshal.go index f14cfa6e9..5ed79c1f5 100644 --- a/btf/marshal.go +++ b/btf/marshal.go @@ -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) diff --git a/btf/traversal.go b/btf/traversal.go index c39dc66e4..384ae774f 100644 --- a/btf/traversal.go +++ b/btf/traversal.go @@ -109,7 +109,7 @@ func children(typ Type, yield func(child *Type) bool) bool { if !yield(&v.Type) { return false } - case *typeTag: + case *TypeTag: if !yield(&v.Type) { return false } diff --git a/btf/types.go b/btf/types.go index a3397460b..229d95310 100644 --- a/btf/types.go +++ b/btf/types.go @@ -67,7 +67,7 @@ var ( _ Type = (*Datasec)(nil) _ Type = (*Float)(nil) _ Type = (*declTag)(nil) - _ Type = (*typeTag)(nil) + _ Type = (*TypeTag)(nil) _ Type = (*cycle)(nil) ) @@ -540,19 +540,25 @@ func (dt *declTag) copy() Type { return &cpy } -// typeTag associates metadata with a type. -type typeTag struct { +// TypeTag associates metadata with a pointer type. Tag types act as a custom +// modifier(const, restrict, volatile) for the target type. Unlike declTags, +// TypeTags are ordered so the order in which they are added matters. +// +// One of their uses is to mark pointers as `__kptr` meaning a pointer points +// to kernel memory. Adding a `__kptr` to pointers in map values allows you +// to store pointers to kernel memory in maps. +type TypeTag struct { Type Type Value string } -func (tt *typeTag) Format(fs fmt.State, verb rune) { +func (tt *TypeTag) Format(fs fmt.State, verb rune) { formatType(fs, verb, tt, "type=", tt.Type, "value=", tt.Value) } -func (tt *typeTag) TypeName() string { return "" } -func (tt *typeTag) qualify() Type { return tt.Type } -func (tt *typeTag) copy() Type { +func (tt *TypeTag) TypeName() string { return "" } +func (tt *TypeTag) qualify() Type { return tt.Type } +func (tt *TypeTag) copy() Type { cpy := *tt return &cpy } @@ -591,7 +597,7 @@ var ( _ qualifier = (*Const)(nil) _ qualifier = (*Restrict)(nil) _ qualifier = (*Volatile)(nil) - _ qualifier = (*typeTag)(nil) + _ qualifier = (*TypeTag)(nil) ) var errUnsizedType = errors.New("type is unsized") @@ -1081,7 +1087,7 @@ func readAndInflateTypes(r io.Reader, bo binary.ByteOrder, typeLen uint32, rawSt declTags = append(declTags, dt) case kindTypeTag: - tt := &typeTag{nil, name} + tt := &TypeTag{nil, name} fixup(header.Type(), &tt.Type) typ = tt diff --git a/btf/types_test.go b/btf/types_test.go index 24583b553..0704a9a3b 100644 --- a/btf/types_test.go +++ b/btf/types_test.go @@ -175,7 +175,7 @@ func TestType(t *testing.T) { }, func() Type { return &Float{} }, func() Type { return &declTag{Type: &Void{}} }, - func() Type { return &typeTag{Type: &Void{}} }, + func() Type { return &TypeTag{Type: &Void{}} }, func() Type { return &cycle{&Void{}} }, } @@ -214,7 +214,7 @@ func TestType(t *testing.T) { func TestTagMarshaling(t *testing.T) { for _, typ := range []Type{ &declTag{&Struct{Members: []Member{}}, "foo", -1}, - &typeTag{&Int{}, "foo"}, + &TypeTag{&Int{}, "foo"}, } { t.Run(fmt.Sprint(typ), func(t *testing.T) { s := specFromTypes(t, []Type{typ}) @@ -341,7 +341,7 @@ func TestUnderlyingType(t *testing.T) { {"volatile", func(t Type) Type { return &Volatile{Type: t} }}, {"restrict", func(t Type) Type { return &Restrict{Type: t} }}, {"typedef", func(t Type) Type { return &Typedef{Type: t} }}, - {"type tag", func(t Type) Type { return &typeTag{Type: t} }}, + {"type tag", func(t Type) Type { return &TypeTag{Type: t} }}, } for _, test := range wrappers { diff --git a/btf/workarounds.go b/btf/workarounds.go index 12a89b87e..eb09047fb 100644 --- a/btf/workarounds.go +++ b/btf/workarounds.go @@ -12,7 +12,7 @@ func datasecResolveWorkaround(b *Builder, ds *Datasec) error { } switch v.Type.(type) { - case *Typedef, *Volatile, *Const, *Restrict, *typeTag: + case *Typedef, *Volatile, *Const, *Restrict, *TypeTag: // NB: We must never call Add on a Datasec, otherwise we risk // infinite recursion. _, err := b.Add(v.Type) diff --git a/btf/workarounds_test.go b/btf/workarounds_test.go index b7b5bf4a5..ce260ccf0 100644 --- a/btf/workarounds_test.go +++ b/btf/workarounds_test.go @@ -21,10 +21,10 @@ func TestDatasecResolveWorkaround(t *testing.T) { &Volatile{i}, &Const{i}, &Restrict{i}, - &typeTag{i, "foo"}, + &TypeTag{i, "foo"}, } { t.Run(fmt.Sprint(typ), func(t *testing.T) { - if _, ok := typ.(*typeTag); ok { + if _, ok := typ.(*TypeTag); ok { testutils.SkipOnOldKernel(t, "5.17", "BTF_KIND_TYPE_TAG") }