forked from cilium/ebpf
/
core_test.go
583 lines (535 loc) · 14.5 KB
/
core_test.go
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
package btf
import (
"errors"
"math/rand"
"os"
"strings"
"testing"
"github.com/cilium/ebpf/internal/testutils"
qt "github.com/frankban/quicktest"
)
func TestCoreAreTypesCompatible(t *testing.T) {
tests := []struct {
a, b Type
compatible bool
}{
{&Void{}, &Void{}, true},
{&Struct{Name: "a"}, &Struct{Name: "b"}, true},
{&Union{Name: "a"}, &Union{Name: "b"}, true},
{&Union{Name: "a"}, &Struct{Name: "b"}, false},
{&Enum{Name: "a"}, &Enum{Name: "b"}, true},
{&Fwd{Name: "a"}, &Fwd{Name: "b"}, true},
{&Int{Name: "a", Size: 2}, &Int{Name: "b", Size: 4}, true},
{&Int{OffsetBits: 1}, &Int{}, false},
{&Pointer{Target: &Void{}}, &Pointer{Target: &Void{}}, true},
{&Pointer{Target: &Void{}}, &Void{}, false},
{&Array{Type: &Void{}}, &Array{Type: &Void{}}, true},
{&Array{Type: &Int{}}, &Array{Type: &Void{}}, false},
{&FuncProto{Return: &Int{}}, &FuncProto{Return: &Void{}}, false},
{
&FuncProto{Return: &Void{}, Params: []FuncParam{{Name: "a", Type: &Void{}}}},
&FuncProto{Return: &Void{}, Params: []FuncParam{{Name: "b", Type: &Void{}}}},
true,
},
{
&FuncProto{Return: &Void{}, Params: []FuncParam{{Type: &Void{}}}},
&FuncProto{Return: &Void{}, Params: []FuncParam{{Type: &Int{}}}},
false,
},
{
&FuncProto{Return: &Void{}, Params: []FuncParam{{Type: &Void{}}, {Type: &Void{}}}},
&FuncProto{Return: &Void{}, Params: []FuncParam{{Type: &Void{}}}},
false,
},
}
for _, test := range tests {
err := coreAreTypesCompatible(test.a, test.b)
if test.compatible {
if err != nil {
t.Errorf("Expected types to be compatible: %s\na = %#v\nb = %#v", err, test.a, test.b)
continue
}
} else {
if !errors.Is(err, errImpossibleRelocation) {
t.Errorf("Expected types to be incompatible: %s\na = %#v\nb = %#v", err, test.a, test.b)
continue
}
}
err = coreAreTypesCompatible(test.b, test.a)
if test.compatible {
if err != nil {
t.Errorf("Expected reversed types to be compatible: %s\na = %#v\nb = %#v", err, test.a, test.b)
}
} else {
if !errors.Is(err, errImpossibleRelocation) {
t.Errorf("Expected reversed types to be incompatible: %s\na = %#v\nb = %#v", err, test.a, test.b)
}
}
}
for _, invalid := range []Type{&Var{}, &Datasec{}} {
err := coreAreTypesCompatible(invalid, invalid)
if errors.Is(err, errImpossibleRelocation) {
t.Errorf("Expected an error for %T, not errImpossibleRelocation", invalid)
} else if err == nil {
t.Errorf("Expected an error for %T", invalid)
}
}
}
func TestCoreAreMembersCompatible(t *testing.T) {
tests := []struct {
a, b Type
compatible bool
}{
{&Struct{Name: "a"}, &Struct{Name: "b"}, true},
{&Union{Name: "a"}, &Union{Name: "b"}, true},
{&Union{Name: "a"}, &Struct{Name: "b"}, true},
{&Enum{Name: "a"}, &Enum{Name: "b"}, false},
{&Enum{Name: "a"}, &Enum{Name: "a___foo"}, true},
{&Enum{Name: "a"}, &Enum{Name: ""}, true},
{&Fwd{Name: "a"}, &Fwd{Name: "b"}, false},
{&Fwd{Name: "a"}, &Fwd{Name: "a___foo"}, true},
{&Fwd{Name: "a"}, &Fwd{Name: ""}, true},
{&Int{Name: "a", Size: 2}, &Int{Name: "b", Size: 4}, true},
{&Int{OffsetBits: 1}, &Int{}, false},
{&Pointer{Target: &Void{}}, &Pointer{Target: &Void{}}, true},
{&Pointer{Target: &Void{}}, &Void{}, false},
{&Array{Type: &Int{Size: 1}}, &Array{Type: &Int{Encoding: Signed}}, true},
{&Float{Size: 2}, &Float{Size: 4}, true},
}
for _, test := range tests {
err := coreAreMembersCompatible(test.a, test.b)
if test.compatible {
if err != nil {
t.Errorf("Expected members to be compatible: %s\na = %#v\nb = %#v", err, test.a, test.b)
continue
}
} else {
if !errors.Is(err, errImpossibleRelocation) {
t.Errorf("Expected members to be incompatible: %s\na = %#v\nb = %#v", err, test.a, test.b)
continue
}
}
err = coreAreMembersCompatible(test.b, test.a)
if test.compatible {
if err != nil {
t.Errorf("Expected reversed members to be compatible: %s\na = %#v\nb = %#v", err, test.a, test.b)
}
} else {
if !errors.Is(err, errImpossibleRelocation) {
t.Errorf("Expected reversed members to be incompatible: %s\na = %#v\nb = %#v", err, test.a, test.b)
}
}
}
for _, invalid := range []Type{&Void{}, &FuncProto{}, &Var{}, &Datasec{}} {
err := coreAreMembersCompatible(invalid, invalid)
if errors.Is(err, errImpossibleRelocation) {
t.Errorf("Expected an error for %T, not errImpossibleRelocation", invalid)
} else if err == nil {
t.Errorf("Expected an error for %T", invalid)
}
}
}
func TestCoreAccessor(t *testing.T) {
for _, valid := range []string{
"0",
"1:0",
"1:0:3:34:10:1",
} {
_, err := parseCoreAccessor(valid)
if err != nil {
t.Errorf("Parse %q: %s", valid, err)
}
}
for _, invalid := range []string{
"",
"-1",
":",
"0:",
":12",
"4294967296",
} {
_, err := parseCoreAccessor(invalid)
if err == nil {
t.Errorf("Accepted invalid accessor %q", invalid)
}
}
}
func TestCoreFindEnumValue(t *testing.T) {
a := &Enum{Values: []EnumValue{{"foo", 23}, {"bar", 42}}}
b := &Enum{Values: []EnumValue{
{"foo___flavour", 0},
{"bar", 123},
{"garbage", 3},
}}
invalid := []struct {
name string
local Type
target Type
acc coreAccessor
err error
}{
{"o-o-b accessor", a, b, coreAccessor{len(a.Values)}, nil},
{"long accessor", a, b, coreAccessor{0, 1}, nil},
{"wrong target", a, &Void{}, coreAccessor{0, 1}, nil},
{
"no matching value",
b, a,
coreAccessor{2},
errImpossibleRelocation,
},
}
for _, test := range invalid {
t.Run(test.name, func(t *testing.T) {
_, _, err := coreFindEnumValue(test.local, test.acc, test.target)
if test.err != nil && !errors.Is(err, test.err) {
t.Fatalf("Expected %s, got %s", test.err, err)
}
if err == nil {
t.Fatal("Accepted invalid case")
}
})
}
valid := []struct {
name string
local, target Type
acc coreAccessor
localValue, targetValue int32
}{
{"a to b", a, b, coreAccessor{0}, 23, 0},
{"b to a", b, a, coreAccessor{1}, 123, 42},
}
for _, test := range valid {
t.Run(test.name, func(t *testing.T) {
local, target, err := coreFindEnumValue(test.local, test.acc, test.target)
qt.Assert(t, err, qt.IsNil)
qt.Check(t, local.Value, qt.Equals, test.localValue)
qt.Check(t, target.Value, qt.Equals, test.targetValue)
})
}
}
func TestCoreFindField(t *testing.T) {
ptr := &Pointer{}
u16 := &Int{Size: 2}
u32 := &Int{Size: 4}
aFields := []Member{
{Name: "foo", Type: ptr, OffsetBits: 1},
{Name: "bar", Type: u16, OffsetBits: 2},
}
bFields := []Member{
{Name: "foo", Type: ptr, OffsetBits: 10},
{Name: "bar", Type: u32, OffsetBits: 20},
{Name: "other", OffsetBits: 4},
}
aStruct := &Struct{Members: aFields, Size: 2}
bStruct := &Struct{Members: bFields, Size: 7}
aArray := &Array{Nelems: 4, Type: u16}
bArray := &Array{Nelems: 3, Type: u32}
invalid := []struct {
name string
local, target Type
acc coreAccessor
err error
}{
{
"unsupported type",
&Void{}, &Void{},
coreAccessor{0, 0},
ErrNotSupported,
},
{
"different types",
&Union{}, &Array{Type: u16},
coreAccessor{0},
errImpossibleRelocation,
},
{
"invalid composite accessor",
aStruct, aStruct,
coreAccessor{0, len(aStruct.Members)},
nil,
},
{
"invalid array accessor",
aArray, aArray,
coreAccessor{0, int(aArray.Nelems)},
nil,
},
{
"o-o-b array accessor",
aArray, bArray,
coreAccessor{0, int(bArray.Nelems)},
errImpossibleRelocation,
},
{
"no match",
bStruct, aStruct,
coreAccessor{0, 2},
errImpossibleRelocation,
},
{
"incompatible match",
&Union{Members: []Member{{Name: "foo", Type: &Pointer{}}}},
&Union{Members: []Member{{Name: "foo", Type: &Int{}}}},
coreAccessor{0, 0},
errImpossibleRelocation,
},
}
for _, test := range invalid {
t.Run(test.name, func(t *testing.T) {
_, _, err := coreFindField(test.local, test.acc, test.target)
if test.err != nil && !errors.Is(err, test.err) {
t.Fatalf("Expected %s, got %s", test.err, err)
}
if err == nil {
t.Fatal("Accepted invalid case")
}
t.Log(err)
})
}
bits := func(typ Type) uint32 {
sz, err := Sizeof(typ)
if err != nil {
t.Fatal(err)
}
return uint32(sz * 8)
}
anon := func(t Type, offset uint32) []Member {
return []Member{{Type: t, OffsetBits: offset}}
}
anonStruct := func(m ...Member) Member {
return Member{Type: &Struct{Members: m}}
}
anonUnion := func(m ...Member) Member {
return Member{Type: &Union{Members: m}}
}
valid := []struct {
name string
local Type
target Type
acc coreAccessor
localField, targetField coreField
}{
{
"array[0]",
aArray,
bArray,
coreAccessor{0, 0},
coreField{u16, 0},
coreField{u32, 0},
},
{
"array[1]",
aArray,
bArray,
coreAccessor{0, 1},
coreField{u16, bits(aArray.Type)},
coreField{u32, bits(bArray.Type)},
},
{
"array[0] with base offset",
aArray,
bArray,
coreAccessor{1, 0},
coreField{u16, bits(aArray)},
coreField{u32, bits(bArray)},
},
{
"array[2] with base offset",
aArray,
bArray,
coreAccessor{1, 2},
coreField{u16, bits(aArray) + 2*bits(aArray.Type)},
coreField{u32, bits(bArray) + 2*bits(bArray.Type)},
},
{
"flex array",
&Struct{Members: []Member{{Name: "foo", Type: &Array{Nelems: 0, Type: u16}}}},
&Struct{Members: []Member{{Name: "foo", Type: &Array{Nelems: 0, Type: u32}}}},
coreAccessor{0, 0, 9000},
coreField{u16, bits(u16) * 9000},
coreField{u32, bits(u32) * 9000},
},
{
"struct.0",
aStruct, bStruct,
coreAccessor{0, 0},
coreField{ptr, 1},
coreField{ptr, 10},
},
{
"struct.0 anon",
aStruct, &Struct{Members: anon(bStruct, 23)},
coreAccessor{0, 0},
coreField{ptr, 1},
coreField{ptr, 23 + 10},
},
{
"struct.0 with base offset",
aStruct, bStruct,
coreAccessor{3, 0},
coreField{ptr, 3*bits(aStruct) + 1},
coreField{ptr, 3*bits(bStruct) + 10},
},
{
"struct.1",
aStruct, bStruct,
coreAccessor{0, 1},
coreField{u16, 2},
coreField{u32, 20},
},
{
"struct.1 anon",
aStruct, &Struct{Members: anon(bStruct, 1)},
coreAccessor{0, 1},
coreField{u16, 2},
coreField{u32, 1 + 20},
},
{
"union.1",
&Union{Members: aFields, Size: 32},
&Union{Members: bFields, Size: 32},
coreAccessor{0, 1},
coreField{u16, 2},
coreField{u32, 20},
},
{
"interchangeable composites",
&Struct{
Members: []Member{
anonStruct(anonUnion(Member{Name: "_1", Type: u16})),
},
},
&Struct{
Members: []Member{
anonUnion(anonStruct(Member{Name: "_1", Type: u16})),
},
},
coreAccessor{0, 0, 0, 0},
coreField{u16, 0},
coreField{u16, 0},
},
}
checkCoreField := func(t *testing.T, got, want coreField) {
t.Helper()
qt.Check(t, got.Type, qt.Equals, want.Type, qt.Commentf("type should match"))
qt.Check(t, got.offset, qt.Equals, want.offset, qt.Commentf("offset should match"))
}
for _, test := range valid {
t.Run(test.name, func(t *testing.T) {
localField, targetField, err := coreFindField(test.local, test.acc, test.target)
qt.Assert(t, err, qt.IsNil)
checkCoreField(t, localField, test.localField)
checkCoreField(t, targetField, test.targetField)
})
}
}
func TestCoreFindFieldCyclical(t *testing.T) {
members := []Member{{Name: "foo", Type: &Pointer{}}}
cyclicStruct := &Struct{}
cyclicStruct.Members = []Member{{Type: cyclicStruct}}
cyclicUnion := &Union{}
cyclicUnion.Members = []Member{{Type: cyclicUnion}}
cyclicArray := &Array{Nelems: 1}
cyclicArray.Type = &Pointer{Target: cyclicArray}
tests := []struct {
name string
local, cyclic Type
}{
{"struct", &Struct{Members: members}, cyclicStruct},
{"union", &Union{Members: members}, cyclicUnion},
{"array", &Array{Nelems: 2, Type: &Int{}}, cyclicArray},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
_, _, err := coreFindField(test.local, coreAccessor{0, 0}, test.cyclic)
if !errors.Is(err, errImpossibleRelocation) {
t.Fatal("Should return errImpossibleRelocation, got", err)
}
})
}
}
func TestCoreRelocation(t *testing.T) {
testutils.Files(t, testutils.Glob(t, "testdata/*.elf"), func(t *testing.T, file string) {
rd, err := os.Open(file)
if err != nil {
t.Fatal(err)
}
defer rd.Close()
spec, err := LoadSpecFromReader(rd)
if err != nil {
t.Fatal(err)
}
errs := map[string]error{
"err_ambiguous": errAmbiguousRelocation,
"err_ambiguous_flavour": errAmbiguousRelocation,
}
for section := range spec.funcInfos {
name := strings.TrimPrefix(section, "socket_filter/")
t.Run(name, func(t *testing.T) {
prog, err := spec.Program(section)
if err != nil {
t.Fatal("Retrieve program:", err)
}
relos, err := CORERelocate(prog.Spec(), spec, prog.CoreRelos)
if want := errs[name]; want != nil {
if !errors.Is(err, want) {
t.Fatal("Expected", want, "got", err)
}
return
}
if err != nil {
t.Fatal("Can't relocate against itself:", err)
}
for offset, relo := range relos {
if relo.Local != relo.Target {
// Since we're relocating against ourselves both values
// should match.
t.Errorf("offset %d: local %v doesn't match target %d", offset, relo.Local, relo.Target)
}
}
})
}
})
}
func TestCoreCopyWithoutQualifiers(t *testing.T) {
qualifiers := []struct {
name string
fn func(Type) Type
}{
{"const", func(t Type) Type { return &Const{Type: 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} }},
}
for _, test := range qualifiers {
t.Run(test.name+" cycle", func(t *testing.T) {
root := &Volatile{}
root.Type = test.fn(root)
_, err := copyType(root, skipQualifiersAndTypedefs)
qt.Assert(t, err, qt.Not(qt.IsNil))
})
}
for _, a := range qualifiers {
for _, b := range qualifiers {
t.Run(a.name+" "+b.name, func(t *testing.T) {
v := a.fn(&Pointer{Target: b.fn(&Int{Name: "z"})})
want := &Pointer{Target: &Int{Name: "z"}}
got, err := copyType(v, skipQualifiersAndTypedefs)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, got, qt.DeepEquals, want)
})
}
}
t.Run("long chain", func(t *testing.T) {
root := &Int{Name: "abc"}
v := Type(root)
for i := 0; i < maxTypeDepth; i++ {
q := qualifiers[rand.Intn(len(qualifiers))]
v = q.fn(v)
t.Log(q.name)
}
got, err := copyType(v, skipQualifiersAndTypedefs)
qt.Assert(t, err, qt.IsNil)
qt.Assert(t, got, qt.DeepEquals, root)
})
}