Skip to content

Commit 868fd7f

Browse files
committed
btf: disable handle API on windows
Windows currently has no / limited BTF support. Disable the API for now. Signed-off-by: Lorenz Bauer <[email protected]>
1 parent 061b77f commit 868fd7f

File tree

10 files changed

+48
-3
lines changed

10 files changed

+48
-3
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,7 @@ jobs:
348348
gotestsum --raw-command --ignore-non-json-output-lines --junitfile junit.xml --
349349
go test -short -count 1 -json
350350
./asm
351+
./btf
351352
./internal
352353
./internal/efw
353354
./internal/kallsyms

btf/core.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strings"
1212

1313
"github.com/cilium/ebpf/asm"
14+
"github.com/cilium/ebpf/internal"
15+
"github.com/cilium/ebpf/internal/platform"
1416
)
1517

1618
// Code in this file is derived from libbpf, which is available under a BSD
@@ -46,6 +48,10 @@ func (f *COREFixup) String() string {
4648
}
4749

4850
func (f *COREFixup) Apply(ins *asm.Instruction) error {
51+
if !platform.IsLinux {
52+
return fmt.Errorf("CO-RE fixup: %w", internal.ErrNotSupportedOnOS)
53+
}
54+
4955
if f.poison {
5056
// Relocation is poisoned, replace the instruction with an invalid one.
5157
if ins.OpCode.IsDWordLoad() {

btf/core_reloc_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,14 @@ func TestLD64IMMReloc(t *testing.T) {
120120
}
121121

122122
coll, err := ebpf.NewCollection(spec)
123+
testutils.SkipIfNotSupportedOnOS(t, err)
123124
if err != nil {
124125
t.Fatal(err)
125126
}
126127
defer coll.Close()
127128
}
128129

129130
func TestCOREPoisonLineInfo(t *testing.T) {
130-
testutils.SkipOnOldKernel(t, "5.0", "program ext_infos")
131-
132131
spec, err := ebpf.LoadCollectionSpec(testutils.NativeFile(t, "../testdata/errors-%s.elf"))
133132
qt.Assert(t, qt.IsNil(err))
134133

btf/handle.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"os"
99

1010
"github.com/cilium/ebpf/internal"
11+
"github.com/cilium/ebpf/internal/platform"
1112
"github.com/cilium/ebpf/internal/sys"
1213
"github.com/cilium/ebpf/internal/unix"
1314
)
@@ -43,6 +44,10 @@ func NewHandle(b *Builder) (*Handle, error) {
4344
func NewHandleFromRawBTF(btf []byte) (*Handle, error) {
4445
const minLogSize = 64 * 1024
4546

47+
if platform.IsWindows {
48+
return nil, fmt.Errorf("btf: handle: %w", internal.ErrNotSupportedOnOS)
49+
}
50+
4651
if uint64(len(btf)) > math.MaxUint32 {
4752
return nil, errors.New("BTF exceeds the maximum size")
4853
}
@@ -110,6 +115,10 @@ func NewHandleFromRawBTF(btf []byte) (*Handle, error) {
110115
//
111116
// Requires CAP_SYS_ADMIN.
112117
func NewHandleFromID(id ID) (*Handle, error) {
118+
if platform.IsWindows {
119+
return nil, fmt.Errorf("btf: handle: %w", internal.ErrNotSupportedOnOS)
120+
}
121+
113122
fd, err := sys.BtfGetFdById(&sys.BtfGetFdByIdAttr{
114123
Id: uint32(id),
115124
})
@@ -242,6 +251,11 @@ type HandleIterator struct {
242251
// Returns true if another BTF object was found. Call [HandleIterator.Err] after
243252
// the function returns false.
244253
func (it *HandleIterator) Next() bool {
254+
if platform.IsWindows {
255+
it.err = fmt.Errorf("btf: %w", internal.ErrNotSupportedOnOS)
256+
return false
257+
}
258+
245259
id := it.ID
246260
for {
247261
attr := &sys.BtfGetNextIdAttr{Id: id}

btf/handle_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestHandleIterator(t *testing.T) {
1818
defer it.Handle.Close()
1919

2020
if !it.Next() {
21+
testutils.SkipIfNotSupportedOnOS(t, it.Err())
2122
t.Fatalf("No BTF loaded")
2223
}
2324
if it.Handle == nil {
@@ -57,6 +58,7 @@ func TestParseModuleSplitSpec(t *testing.T) {
5758
}
5859
return false
5960
})
61+
testutils.SkipIfNotSupportedOnOS(t, err)
6062
if err != nil {
6163
t.Fatal(err)
6264
}

btf/kernel.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/cilium/ebpf/internal"
1111
"github.com/cilium/ebpf/internal/linux"
12+
"github.com/cilium/ebpf/internal/platform"
1213
)
1314

1415
var kernelBTF = struct {
@@ -85,14 +86,18 @@ func LoadKernelModuleSpec(module string) (*Spec, error) {
8586

8687
spec, err = loadKernelModuleSpec(module, base)
8788
if err != nil {
88-
return nil, err
89+
return nil, fmt.Errorf("load kernel module: %w", err)
8990
}
9091

9192
kernelBTF.modules[module] = spec
9293
return spec.Copy(), nil
9394
}
9495

9596
func loadKernelSpec() (_ *Spec, fallback bool, _ error) {
97+
if platform.IsWindows {
98+
return nil, false, internal.ErrNotSupportedOnOS
99+
}
100+
96101
fh, err := os.Open("/sys/kernel/btf/vmlinux")
97102
if err == nil {
98103
defer fh.Close()
@@ -112,6 +117,10 @@ func loadKernelSpec() (_ *Spec, fallback bool, _ error) {
112117
}
113118

114119
func loadKernelModuleSpec(module string, base *Spec) (*Spec, error) {
120+
if platform.IsWindows {
121+
return nil, internal.ErrNotSupportedOnOS
122+
}
123+
115124
dir, file := filepath.Split(module)
116125
if dir != "" || filepath.Ext(file) != "" {
117126
return nil, fmt.Errorf("invalid module name %q", module)
@@ -128,6 +137,10 @@ func loadKernelModuleSpec(module string, base *Spec) (*Spec, error) {
128137

129138
// findVMLinux scans multiple well-known paths for vmlinux kernel images.
130139
func findVMLinux() (*os.File, error) {
140+
if platform.IsWindows {
141+
return nil, fmt.Errorf("find vmlinux: %w", internal.ErrNotSupportedOnOS)
142+
}
143+
131144
release, err := linux.KernelRelease()
132145
if err != nil {
133146
return nil, err

btf/workarounds_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func TestDatasecResolveWorkaround(t *testing.T) {
5555
}
5656

5757
h, err := NewHandle(b)
58+
testutils.SkipIfNotSupportedOnOS(t, err)
5859
var ve *internal.VerifierError
5960
if errors.As(err, &ve) {
6061
t.Fatalf("%+v\n", ve)

testdata/errors-eb.elf

320 Bytes
Binary file not shown.

testdata/errors-el.elf

320 Bytes
Binary file not shown.

testdata/errors.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@ struct nonexist {
77

88
enum nonexist_enum { NON_EXIST = 1 };
99

10+
// Force loading program with BTF by including a relocation for a local type.
11+
#define FORCE_BTF \
12+
do { \
13+
if (bpf_core_type_id_local(int) == 0) \
14+
return __LINE__; \
15+
} while (0)
16+
1017
__section("socket") int poisoned_single() {
18+
FORCE_BTF;
1119
struct nonexist ne;
1220
return core_access(ne.non_exist);
1321
}
1422

1523
__section("socket") int poisoned_double() {
24+
FORCE_BTF;
1625
return bpf_core_enum_value(enum nonexist_enum, NON_EXIST);
1726
}
1827

0 commit comments

Comments
 (0)