Skip to content

Commit

Permalink
*: switch to %w formatting for all errors
Browse files Browse the repository at this point in the history
Without this, users cannot unwrap the underlying error -- which is
particularly problematic for Raw{At,De}tachProgram. runc for instance
needs to detect the existence of BPF_F_REPLACE, but returning a
stringified error makes it harder to deal with.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
  • Loading branch information
cyphar committed Jun 8, 2021
1 parent f510d07 commit 400c5b6
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 22 deletions.
4 changes: 2 additions & 2 deletions asm/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (ins *Instruction) Unmarshal(r io.Reader, bo binary.ByteOrder) (uint64, err
ins.Constant = int64(bi.Constant)
ins.Dst, ins.Src, err = bi.Registers.Unmarshal(bo)
if err != nil {
return 0, fmt.Errorf("can't unmarshal registers: %s", err)
return 0, fmt.Errorf("can't unmarshal registers: %w", err)
}

if !bi.OpCode.IsDWordLoad() {
Expand Down Expand Up @@ -90,7 +90,7 @@ func (ins Instruction) Marshal(w io.Writer, bo binary.ByteOrder) (uint64, error)

regs, err := newBPFRegisters(ins.Dst, ins.Src, bo)
if err != nil {
return 0, fmt.Errorf("can't marshal registers: %s", err)
return 0, fmt.Errorf("can't marshal registers: %w", err)
}

bpfi := bpfInstruction{
Expand Down
6 changes: 3 additions & 3 deletions cmd/bpf2go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,19 @@ func run(stdout io.Writer, pkg, outputDir string, args []string) (err error) {

deps, err := parseDependencies(cwd, &dep)
if err != nil {
return fmt.Errorf("can't read dependency information: %s", err)
return fmt.Errorf("can't read dependency information: %w", err)
}

// There is always at least a dependency for the main file.
deps[0].file = goFileName
depFile, err := adjustDependencies(makeBase, deps)
if err != nil {
return fmt.Errorf("can't adjust dependency information: %s", err)
return fmt.Errorf("can't adjust dependency information: %w", err)
}

depFileName := goFileName + ".d"
if err := ioutil.WriteFile(depFileName, depFile, 0666); err != nil {
return fmt.Errorf("can't write dependency file: %s", err)
return fmt.Errorf("can't write dependency file: %w", err)
}

fmt.Fprintln(stdout, "Wrote", depFileName)
Expand Down
8 changes: 4 additions & 4 deletions cmd/bpf2go/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,12 @@ type writeArgs struct {
func writeCommon(args writeArgs) error {
obj, err := ioutil.ReadAll(args.obj)
if err != nil {
return fmt.Errorf("read object file contents: %s", err)
return fmt.Errorf("read object file contents: %w", err)
}

spec, err := ebpf.LoadCollectionSpecFromReader(bytes.NewReader(obj))
if err != nil {
return fmt.Errorf("can't load BPF from ELF: %s", err)
return fmt.Errorf("can't load BPF from ELF: %w", err)
}

maps := make(map[string]string)
Expand Down Expand Up @@ -260,7 +260,7 @@ func writeCommon(args writeArgs) error {

var buf bytes.Buffer
if err := commonTemplate.Execute(&buf, &ctx); err != nil {
return fmt.Errorf("can't generate types: %s", err)
return fmt.Errorf("can't generate types: %w", err)
}

return writeFormatted(buf.Bytes(), args.out)
Expand All @@ -278,7 +278,7 @@ func binaryString(buf []byte) string {
func writeFormatted(src []byte, out io.Writer) error {
formatted, err := format.Source(src)
if err != nil {
return fmt.Errorf("can't format source: %s", err)
return fmt.Errorf("can't format source: %w", err)
}

_, err = out.Write(formatted)
Expand Down
2 changes: 1 addition & 1 deletion internal/btf/btf.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func loadSpecFromVmlinux(rd io.ReaderAt) (*Spec, error) {

btfSection, _, _, err := findBtfSections(file)
if err != nil {
return nil, fmt.Errorf(".BTF ELF section: %s", err)
return nil, fmt.Errorf(".BTF ELF section: %w", err)
}
if btfSection == nil {
return nil, fmt.Errorf("unable to find .BTF ELF section")
Expand Down
6 changes: 3 additions & 3 deletions link/cgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type CgroupOptions struct {
func AttachCgroup(opts CgroupOptions) (Link, error) {
cgroup, err := os.Open(opts.Path)
if err != nil {
return nil, fmt.Errorf("can't open cgroup: %s", err)
return nil, fmt.Errorf("can't open cgroup: %w", err)
}

clone, err := opts.Program.Clone()
Expand Down Expand Up @@ -107,7 +107,7 @@ func (cg *progAttachCgroup) Close() error {
Attach: cg.attachType,
})
if err != nil {
return fmt.Errorf("close cgroup: %s", err)
return fmt.Errorf("close cgroup: %w", err)
}
return nil
}
Expand Down Expand Up @@ -135,7 +135,7 @@ func (cg *progAttachCgroup) Update(prog *ebpf.Program) error {

if err := RawAttachProgram(args); err != nil {
new.Close()
return fmt.Errorf("can't update cgroup: %s", err)
return fmt.Errorf("can't update cgroup: %w", err)
}

cg.current.Close()
Expand Down
8 changes: 4 additions & 4 deletions link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func AttachRawLink(opts RawLinkOptions) (*RawLink, error) {
}
fd, err := bpfLinkCreate(&attr)
if err != nil {
return nil, fmt.Errorf("can't create link: %s", err)
return nil, fmt.Errorf("can't create link: %w", err)
}

return &RawLink{fd, ""}, nil
Expand All @@ -113,7 +113,7 @@ func LoadPinnedRawLink(fileName string, linkType Type, opts *ebpf.LoadPinOptions
info, err := link.Info()
if err != nil {
link.Close()
return nil, fmt.Errorf("get pinned link info: %s", err)
return nil, fmt.Errorf("get pinned link info: %w", err)
}

if info.Type != linkType {
Expand Down Expand Up @@ -194,7 +194,7 @@ func (l *RawLink) UpdateArgs(opts RawLinkUpdateOptions) error {

linkFd, err := l.fd.Value()
if err != nil {
return fmt.Errorf("can't update link: %s", err)
return fmt.Errorf("can't update link: %w", err)
}

attr := bpfLinkUpdateAttr{
Expand All @@ -218,7 +218,7 @@ func (l *RawLink) Info() (*RawLinkInfo, error) {
var info bpfLinkInfo
err := internal.BPFObjGetInfoByFD(l.fd, unsafe.Pointer(&info), unsafe.Sizeof(info))
if err != nil {
return nil, fmt.Errorf("link info: %s", err)
return nil, fmt.Errorf("link info: %w", err)
}

return &RawLinkInfo{
Expand Down
2 changes: 1 addition & 1 deletion link/perf_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func (pe *perfEvent) attach(prog *ebpf.Program) error {

// PERF_EVENT_IOC_ENABLE and _DISABLE ignore their given values.
if err := unix.IoctlSetInt(int(kfd), unix.PERF_EVENT_IOC_ENABLE, 0); err != nil {
return fmt.Errorf("enable perf event: %s", err)
return fmt.Errorf("enable perf event: %w", err)
}

// Close the perf event when its reference is lost to avoid leaking system resources.
Expand Down
4 changes: 2 additions & 2 deletions link/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func RawAttachProgram(opts RawAttachProgramOptions) error {
}

if err := internal.BPFProgAttach(&attr); err != nil {
return fmt.Errorf("can't attach program: %s", err)
return fmt.Errorf("can't attach program: %w", err)
}
return nil
}
Expand All @@ -69,7 +69,7 @@ func RawDetachProgram(opts RawDetachProgramOptions) error {
AttachType: uint32(opts.Attach),
}
if err := internal.BPFProgDetach(&attr); err != nil {
return fmt.Errorf("can't detach program: %s", err)
return fmt.Errorf("can't detach program: %w", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func newMapFromFD(fd *internal.FD) (*Map, error) {
info, err := newMapInfoFromFd(fd)
if err != nil {
fd.Close()
return nil, fmt.Errorf("get map info: %s", err)
return nil, fmt.Errorf("get map info: %w", err)
}

return newMap(fd, info.Name, info.Type, info.KeySize, info.ValueSize, info.MaxEntries, info.Flags)
Expand Down Expand Up @@ -262,7 +262,7 @@ func newMapWithOptions(spec *MapSpec, opts MapOptions, handles *handleCache) (_
if spec.Pinning == PinByName {
path := filepath.Join(opts.PinPath, spec.Name)
if err := m.Pin(path); err != nil {
return nil, fmt.Errorf("pin map: %s", err)
return nil, fmt.Errorf("pin map: %w", err)
}
}

Expand Down

0 comments on commit 400c5b6

Please sign in to comment.