Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve error wrapping #336

Merged
merged 3 commits into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion internal/syscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"path/filepath"
"runtime"
"syscall"
"unsafe"

"github.com/cilium/ebpf/internal/unix"
Expand Down Expand Up @@ -61,7 +62,7 @@ func BPF(cmd BPFCmd, attr unsafe.Pointer, size uintptr) (uintptr, error) {

var err error
if errNo != 0 {
err = errNo
err = wrappedErrno{errNo}
}

return r1, err
Expand Down Expand Up @@ -213,3 +214,32 @@ func BPFMapCreate(attr *BPFMapCreateAttr) (*FD, error) {

return NewFD(uint32(fd)), nil
}

// wrappedErrno wraps syscall.Errno to prevent direct comparisons with
// syscall.E* or unix.E* constants.
//
// You should never export an error of this type.
type wrappedErrno struct {
syscall.Errno
}

func (we wrappedErrno) Unwrap() error {
return we.Errno
}

type syscallError struct {
error
errno syscall.Errno
}

func SyscallError(err error, errno syscall.Errno) error {
return &syscallError{err, errno}
}
Comment on lines +235 to +237
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not export the type and use it directly? This is what golang stdlib does with types such as os.PathError.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, instead of e.g return syscall.SyscallError(err, errno) you can do return &syscall.SyscallError{err, errno} and avoid an unnecessary function call (not sure if golang will inline it). Since this is an internal type, you're not exporting it.


func (se *syscallError) Is(target error) bool {
return target == se.error
}

func (se *syscallError) Unwrap() error {
return se.errno
}
39 changes: 39 additions & 0 deletions internal/syscall_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"errors"
"testing"

"github.com/cilium/ebpf/internal/unix"
Expand All @@ -15,3 +16,41 @@ func TestObjName(t *testing.T) {
t.Errorf("Name is %d instead of %d bytes long", len(name), unix.BPF_OBJ_NAME_LEN)
}
}

func TestWrappedErrno(t *testing.T) {
a := error(wrappedErrno{unix.EINVAL})
b := error(unix.EINVAL)

if a == b {
t.Error("wrappedErrno is comparable to plain errno")
}

if !errors.Is(a, b) {
t.Error("errors.Is(wrappedErrno, errno) returns false")
}

if errors.Is(a, unix.EAGAIN) {
t.Error("errors.Is(wrappedErrno, EAGAIN) returns true")
}
}

func TestSyscallError(t *testing.T) {
err := errors.New("foo")
foo := SyscallError(err, unix.EINVAL)

if !errors.Is(foo, unix.EINVAL) {
t.Error("SyscallError is not the wrapped errno")
}

if !errors.Is(foo, err) {
t.Error("SyscallError is not the wrapped error")
}

if errors.Is(unix.EINVAL, foo) {
t.Error("Errno is the SyscallError")
}

if errors.Is(err, foo) {
t.Error("Error is the SyscallError")
}
}
30 changes: 11 additions & 19 deletions syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package ebpf
import (
"errors"
"fmt"
"os"
"unsafe"

"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/internal/unix"
)

// Generic errors returned by BPF syscalls.
var ErrNotExist = errors.New("requested object does not exist")
// ErrNotExist is returned when loading a non-existing map or program.
//
// Deprecated: use os.ErrNotExist instead.
var ErrNotExist = os.ErrNotExist

// invalidBPFObjNameChar returns true if char may not appear in
// a BPF object name.
Expand Down Expand Up @@ -162,7 +165,7 @@ func bpfProgLoad(attr *bpfProgLoadAttr) (*internal.FD, error) {
fd, err := internal.BPF(internal.BPF_PROG_LOAD, unsafe.Pointer(attr), unsafe.Sizeof(*attr))
// As of ~4.20 the verifier can be interrupted by a signal,
// and returns EAGAIN in that case.
if err == unix.EAGAIN {
if errors.Is(err, unix.EAGAIN) {
continue
}

Expand Down Expand Up @@ -326,7 +329,7 @@ func objGetNextID(cmd internal.BPFCmd, start uint32) (uint32, error) {
startID: start,
}
_, err := internal.BPF(cmd, unsafe.Pointer(&attr), unsafe.Sizeof(attr))
return attr.nextID, wrapObjError(err)
return attr.nextID, err
}

func bpfMapBatch(cmd internal.BPFCmd, m *internal.FD, inBatch, outBatch, keys, values internal.Pointer, count uint32, opts *BatchOptions) (uint32, error) {
Expand All @@ -352,32 +355,21 @@ func bpfMapBatch(cmd internal.BPFCmd, m *internal.FD, inBatch, outBatch, keys, v
return attr.count, wrapMapError(err)
}

func wrapObjError(err error) error {
if err == nil {
return nil
}
if errors.Is(err, unix.ENOENT) {
return fmt.Errorf("%w", ErrNotExist)
}

return errors.New(err.Error())
}

func wrapMapError(err error) error {
if err == nil {
return nil
}

if errors.Is(err, unix.ENOENT) {
return ErrKeyNotExist
return internal.SyscallError(ErrKeyNotExist, unix.ENOENT)
}

if errors.Is(err, unix.EEXIST) {
return ErrKeyExist
return internal.SyscallError(ErrKeyExist, unix.EEXIST)
}

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

return err
Expand Down Expand Up @@ -484,5 +476,5 @@ func bpfObjGetFDByID(cmd internal.BPFCmd, id uint32) (*internal.FD, error) {
id: id,
}
ptr, err := internal.BPF(cmd, unsafe.Pointer(&attr), unsafe.Sizeof(attr))
return internal.NewFD(uint32(ptr)), wrapObjError(err)
return internal.NewFD(uint32(ptr)), err
}