Skip to content

Commit

Permalink
remove redundant guards and fix potential bugs
Browse files Browse the repository at this point in the history
Signed-off-by: kwakubiney <kebiney@hotmail.com>
  • Loading branch information
kwakubiney committed Jul 3, 2023
1 parent 5ef8223 commit fe0b65a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 38 deletions.
1 change: 1 addition & 0 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func TestLoadCollectionSpec(t *testing.T) {
LogLevel: LogLevelBranch,
},
})

testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
Expand Down
17 changes: 8 additions & 9 deletions link/syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ var haveProgAttach = internal.NewFeatureTest("BPF_PROG_ATTACH", "4.10", func() e
})

var haveProgAttachReplace = internal.NewFeatureTest("BPF_PROG_ATTACH atomic replacement of MULTI progs", "5.5", func() error {
if err := haveProgAttach(); err != nil {
return err
}

prog, err := ebpf.NewProgram(&ebpf.ProgramSpec{
Type: ebpf.CGroupSKB,
AttachType: ebpf.AttachCGroupInetIngress,
Expand All @@ -56,12 +60,11 @@ var haveProgAttachReplace = internal.NewFeatureTest("BPF_PROG_ATTACH atomic repl
asm.Return(),
},
})
if err != nil {
if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}
return err

if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}

defer prog.Close()

// We know that we have BPF_PROG_ATTACH since we can load CGroupSKB programs.
Expand All @@ -85,10 +88,6 @@ var haveProgAttachReplace = internal.NewFeatureTest("BPF_PROG_ATTACH atomic repl
if errors.Is(err, unix.EBADF) {
return nil
}

if haveFeatErr := haveProgAttach(); haveFeatErr != nil {
return haveFeatErr
}
}
return err
})
Expand Down
3 changes: 1 addition & 2 deletions link/uprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"debug/elf"
"errors"
"fmt"
"github.com/cilium/ebpf/internal/unix"
"os"
"sync"

Expand All @@ -20,7 +19,7 @@ var (
haveRefCtrOffsetPMU = internal.NewFeatureTest("RefCtrOffsetPMU", "4.20", func() error {
_, err := os.Stat(uprobeRefCtrOffsetPMUPath)
if err != nil {
if errors.Is(err, unix.EINVAL) {
if errors.Is(err, os.ErrNotExist) {
return internal.ErrNotSupported
}
return err
Expand Down
7 changes: 4 additions & 3 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,17 @@ func (spec *MapSpec) createMap(inner *sys.FD, opts MapOptions) (_ *Map, err erro
if errors.Is(err, unix.EINVAL) && spec.Type == UnspecifiedMap {
return nil, fmt.Errorf("map create: cannot use type %s", UnspecifiedMap)
}
if attr.BtfFd == 0 {
return nil, fmt.Errorf("map create: %w (without BTF k/v)", err)
}

if spec.Flags&(unix.BPF_F_RDONLY_PROG|unix.BPF_F_WRONLY_PROG) > 0 || spec.Freeze {
if haveFeatErr := haveMapMutabilityModifiers(); err != nil {
return nil, fmt.Errorf("map create: %w", haveFeatErr)
}
}

if attr.BtfFd == 0 {
return nil, fmt.Errorf("map create: %w (without BTF k/v)", err)
}

if spec.Flags&unix.BPF_F_MMAPABLE > 0 {
if haveFeatErr := haveMmapableMaps(); err != nil {
return nil, fmt.Errorf("map create: %w", haveFeatErr)
Expand Down
33 changes: 9 additions & 24 deletions syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ var haveMapMutabilityModifiers = internal.NewFeatureTest("read- and write-only m
MaxEntries: 1,
MapFlags: unix.BPF_F_RDONLY_PROG,
})

if err != nil {
if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}
return err
return internal.ErrNotSupported
}

_ = m.Close()
return nil
})
Expand All @@ -107,10 +106,7 @@ var haveMmapableMaps = internal.NewFeatureTest("mmapable maps", "5.5", func() er
MapFlags: unix.BPF_F_MMAPABLE,
})
if err != nil {
if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}
return err
return internal.ErrNotSupported
}
_ = m.Close()
return nil
Expand Down Expand Up @@ -145,11 +141,9 @@ var haveNoPreallocMaps = internal.NewFeatureTest("prealloc maps", "4.6", func()
MaxEntries: 1,
MapFlags: unix.BPF_F_NO_PREALLOC,
})

if err != nil {
if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}
return err
return internal.ErrNotSupported
}
_ = m.Close()
return nil
Expand Down Expand Up @@ -191,10 +185,7 @@ var haveObjName = internal.NewFeatureTest("object names", "4.15", func() error {
fd, err := sys.MapCreate(&attr)

if err != nil {
if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}
return err
return internal.ErrNotSupported
}

_ = fd.Close()
Expand All @@ -216,10 +207,7 @@ var objNameAllowsDot = internal.NewFeatureTest("dot in object names", "5.2", fun

fd, err := sys.MapCreate(&attr)
if err != nil {
if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}
return err
return internal.ErrNotSupported
}

_ = fd.Close()
Expand All @@ -238,10 +226,7 @@ var haveBatchAPI = internal.NewFeatureTest("map batch api", "5.6", func() error
fd, err := sys.MapCreate(&attr)

if err != nil {
if errors.Is(err, unix.EINVAL) {
return internal.ErrNotSupported
}
return err
return internal.ErrNotSupported
}
defer fd.Close()

Expand Down

0 comments on commit fe0b65a

Please sign in to comment.