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