1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
|
package btf
import (
"bytes"
"encoding/binary"
"math"
"testing"
"github.com/go-quicktest/qt"
"github.com/google/go-cmp/cmp"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
)
func TestBuilderMarshal(t *testing.T) {
typ := &Int{
Name: "foo",
Size: 2,
Encoding: Signed | Char,
}
want := []Type{
(*Void)(nil),
typ,
&Pointer{typ},
&Typedef{"baz", typ, nil},
}
b, err := NewBuilder(want)
qt.Assert(t, qt.IsNil(err))
cpy := *b
buf, err := b.Marshal(nil, &MarshalOptions{Order: internal.NativeEndian})
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.CmpEquals(b, &cpy, cmp.AllowUnexported(*b)), qt.Commentf("Marshaling should not change Builder state"))
have, err := loadRawSpec(bytes.NewReader(buf), internal.NativeEndian, nil)
qt.Assert(t, qt.IsNil(err), qt.Commentf("Couldn't parse BTF"))
qt.Assert(t, qt.DeepEquals(have.imm.types, want))
}
func TestBuilderAdd(t *testing.T) {
i := &Int{
Name: "foo",
Size: 2,
Encoding: Signed | Char,
}
pi := &Pointer{i}
var b Builder
id, err := b.Add(pi)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(id, TypeID(1)), qt.Commentf("First non-void type doesn't get id 1"))
id, err = b.Add(pi)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(id, TypeID(1)))
id, err = b.Add(i)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(id, TypeID(2)), qt.Commentf("Second type doesn't get id 2"))
id, err = b.Add(i)
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(id, TypeID(2)), qt.Commentf("Adding a type twice returns different ids"))
id, err = b.Add(&Typedef{"baz", i, nil})
qt.Assert(t, qt.IsNil(err))
qt.Assert(t, qt.Equals(id, TypeID(3)))
}
func TestRoundtripVMlinux(t *testing.T) {
types := typesFromSpec(vmlinuxSpec(t))
// Randomize the order to force different permutations of walking the type
// graph. Keep Void at index 0.
testutils.Rand(t).Shuffle(len(types[1:]), func(i, j int) {
types[i+1], types[j+1] = types[j+1], types[i+1]
})
visited := make(map[Type]struct{})
limitTypes:
for i, typ := range types {
visitInPostorder(typ, visited, func(_ Type) bool { return true })
if len(visited) >= math.MaxInt16 {
// IDs exceeding math.MaxUint16 can trigger a bug when loading BTF.
// This can be removed once the patch lands.
// See https://lore.kernel.org/bpf/20220909092107.3035-1-oss@lmb.io/
types = types[:i]
break limitTypes
}
}
b, err := NewBuilder(types)
qt.Assert(t, qt.IsNil(err))
buf, err := b.Marshal(nil, KernelMarshalOptions())
qt.Assert(t, qt.IsNil(err))
rebuilt, err := loadRawSpec(bytes.NewReader(buf), binary.LittleEndian, nil)
qt.Assert(t, qt.IsNil(err), qt.Commentf("round tripping BTF failed"))
if n := len(rebuilt.imm.types); n > math.MaxUint16 {
t.Logf("Rebuilt BTF contains %d types which exceeds uint16, test may fail on older kernels", n)
}
h, err := NewHandleFromRawBTF(buf)
testutils.SkipIfNotSupported(t, err)
qt.Assert(t, qt.IsNil(err), qt.Commentf("loading rebuilt BTF failed"))
h.Close()
}
func TestMarshalEnum64(t *testing.T) {
enum := &Enum{
Name: "enum64",
Size: 8,
Signed: true,
Values: []EnumValue{
{"A", 0},
{"B", 1},
},
}
b, err := NewBuilder([]Type{enum})
qt.Assert(t, qt.IsNil(err))
buf, err := b.Marshal(nil, &MarshalOptions{
Order: internal.NativeEndian,
ReplaceEnum64: true,
})
qt.Assert(t, qt.IsNil(err))
spec, err := loadRawSpec(bytes.NewReader(buf), internal.NativeEndian, nil)
qt.Assert(t, qt.IsNil(err))
var have *Union
err = spec.TypeByName("enum64", &have)
qt.Assert(t, qt.IsNil(err))
placeholder := &Int{Name: "enum64_placeholder", Size: 8, Encoding: Signed}
qt.Assert(t, qt.DeepEquals(have, &Union{
Name: "enum64",
Size: 8,
Members: []Member{
{Name: "A", Type: placeholder},
{Name: "B", Type: placeholder},
},
}))
}
func TestMarshalDeclTags(t *testing.T) {
types := []Type{
// Instead of an adjacent declTag, this will receive a placeholder Int.
&Typedef{
Name: "decl tag typedef",
Tags: []string{"decl tag"},
Type: &Int{Name: "decl tag target"},
},
}
b, err := NewBuilder(types)
qt.Assert(t, qt.IsNil(err))
buf, err := b.Marshal(nil, &MarshalOptions{
Order: internal.NativeEndian,
ReplaceDeclTags: true,
})
qt.Assert(t, qt.IsNil(err))
spec, err := loadRawSpec(bytes.NewReader(buf), internal.NativeEndian, nil)
qt.Assert(t, qt.IsNil(err))
var td *Typedef
qt.Assert(t, qt.IsNil(spec.TypeByName("decl tag typedef", &td)))
var ti *Int
qt.Assert(t, qt.IsNil(spec.TypeByName("decl_tag_placeholder", &ti)))
}
func TestMarshalTypeTags(t *testing.T) {
types := []Type{
// Instead of pointing to a TypeTag, this will point to an intermediary Const.
&Typedef{
Name: "type tag typedef",
Type: &TypeTag{
Value: "type tag",
Type: &Pointer{
Target: &Int{Name: "type tag target"},
},
},
},
}
b, err := NewBuilder(types)
qt.Assert(t, qt.IsNil(err))
buf, err := b.Marshal(nil, &MarshalOptions{
Order: internal.NativeEndian,
ReplaceTypeTags: true,
})
qt.Assert(t, qt.IsNil(err))
spec, err := loadRawSpec(bytes.NewReader(buf), internal.NativeEndian, nil)
qt.Assert(t, qt.IsNil(err))
var td *Typedef
qt.Assert(t, qt.IsNil(spec.TypeByName("type tag typedef", &td)))
qt.Assert(t, qt.Satisfies(td.Type, func(typ Type) bool {
_, ok := typ.(*Const)
return ok
}))
}
func BenchmarkMarshaler(b *testing.B) {
types := typesFromSpec(vmlinuxTestdataSpec(b))[:100]
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var b Builder
for _, typ := range types {
_, _ = b.Add(typ)
}
_, _ = b.Marshal(nil, nil)
}
}
func BenchmarkBuildVmlinux(b *testing.B) {
types := typesFromSpec(vmlinuxTestdataSpec(b))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
var b Builder
for _, typ := range types {
_, _ = b.Add(typ)
}
_, _ = b.Marshal(nil, nil)
}
}
func marshalNativeEndian(tb testing.TB, types []Type) []byte {
tb.Helper()
b, err := NewBuilder(types)
qt.Assert(tb, qt.IsNil(err))
buf, err := b.Marshal(nil, nil)
qt.Assert(tb, qt.IsNil(err))
return buf
}
func specFromTypes(tb testing.TB, types []Type) *Spec {
tb.Helper()
btf := marshalNativeEndian(tb, types)
spec, err := loadRawSpec(bytes.NewReader(btf), internal.NativeEndian, nil)
qt.Assert(tb, qt.IsNil(err))
return spec
}
func typesFromSpec(spec *Spec) []Type {
var types []Type
iter := spec.Iterate()
for iter.Next() {
types = append(types, iter.Type)
}
return types
}
|