github.com/cilium/ebpf@v0.15.1-0.20240517100537-8079b37aa138/btf/workarounds_test.go (about) 1 package btf 2 3 import ( 4 "errors" 5 "fmt" 6 "testing" 7 8 "github.com/cilium/ebpf/internal" 9 "github.com/cilium/ebpf/internal/testutils" 10 11 "github.com/go-quicktest/qt" 12 ) 13 14 func TestDatasecResolveWorkaround(t *testing.T) { 15 testutils.SkipOnOldKernel(t, "5.2", "BTF_KIND_DATASEC") 16 17 i := &Int{Size: 1} 18 19 for _, typ := range []Type{ 20 &Typedef{"foo", i}, 21 &Volatile{i}, 22 &Const{i}, 23 &Restrict{i}, 24 &typeTag{i, "foo"}, 25 } { 26 t.Run(fmt.Sprint(typ), func(t *testing.T) { 27 if _, ok := typ.(*typeTag); ok { 28 testutils.SkipOnOldKernel(t, "5.17", "BTF_KIND_TYPE_TAG") 29 } 30 31 ds := &Datasec{ 32 Name: "a", 33 Size: 2, 34 Vars: []VarSecinfo{ 35 { 36 Size: 1, 37 Offset: 0, 38 // struct, union, pointer, array will trigger the bug. 39 Type: &Var{Name: "a", Type: &Pointer{i}}, 40 }, 41 { 42 Size: 1, 43 Offset: 1, 44 Type: &Var{ 45 Name: "b", 46 Type: typ, 47 }, 48 }, 49 }, 50 } 51 52 b, err := NewBuilder([]Type{ds}) 53 if err != nil { 54 t.Fatal(err) 55 } 56 57 h, err := NewHandle(b) 58 var ve *internal.VerifierError 59 if errors.As(err, &ve) { 60 t.Fatalf("%+v\n", ve) 61 } 62 if err != nil { 63 t.Fatal(err) 64 } 65 h.Close() 66 }) 67 } 68 } 69 70 func TestEmptyBTFWithStringTableWorkaround(t *testing.T) { 71 var b Builder 72 73 _, err := b.addString("foo") 74 qt.Assert(t, qt.IsNil(err)) 75 76 h, err := NewHandle(&b) 77 testutils.SkipIfNotSupported(t, err) 78 qt.Assert(t, qt.IsNil(err)) 79 qt.Assert(t, qt.IsNil(h.Close())) 80 }