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

program, btf: improve debuggability when CO-RE or kfunc fixup fails #1402

Merged
merged 4 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
btf: use line info to annotate poisoned CO-RE relocations
Add a line info when poisoning an instruction to make the
action explicit in the verifier log. Given a program like:

    ; return bpf_core_enum_value(enum nonexist_enum, NON_EXIST);
    0: LdImmDW dst: r0 imm: 1
    2: Exit

The verifier output now is:

    0: R1=ctx(off=0,imm=0) R10=fp0
    ; instruction poisoned by CO-RE
    0: (18) r10 = 0xbad2310
    frame pointer is read only

Signed-off-by: Lorenz Bauer <lmb@isovalent.com>
  • Loading branch information
lmb committed Apr 3, 2024
commit 125ee42d52d8699e0b2ab4f1068993ce74816a64
12 changes: 12 additions & 0 deletions btf/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ func (f *COREFixup) Apply(ins *asm.Instruction) error {
// Replace all single size instruction with a invalid call instruction.
*ins = asm.BuiltinFunc(COREBadRelocationSentinel).Call()
}

// Add context to the kernel verifier output.
if source := ins.Source(); source != nil {
*ins = ins.WithSource(&Line{
line: fmt.Sprintf("instruction poisoned by CO-RE: %s", source),
})
} else {
*ins = ins.WithSource(&Line{
line: "instruction poisoned by CO-RE",
})
}

return nil
}

Expand Down
43 changes: 43 additions & 0 deletions btf/core_reloc_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package btf_test

import (
"bytes"
"io"
"os"
"slices"
"strings"
"testing"

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/btf"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"

"github.com/go-quicktest/qt"
)

func TestCORERelocationLoad(t *testing.T) {
Expand Down Expand Up @@ -121,3 +125,42 @@ func TestLD64IMMReloc(t *testing.T) {
}
defer coll.Close()
}

func TestCOREPoisonLineInfo(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.0", "program ext_infos")

spec, err := ebpf.LoadCollectionSpec(testutils.NativeFile(t, "../testdata/errors-%s.elf"))
qt.Assert(t, qt.IsNil(err))

var b btf.Builder
raw, err := b.Marshal(nil, nil)
qt.Assert(t, qt.IsNil(err))
empty, err := btf.LoadSpecFromReader(bytes.NewReader(raw))
qt.Assert(t, qt.IsNil(err))

for _, test := range []struct {
name string
}{
{"poisoned_single"},
{"poisoned_double"},
} {
progSpec := spec.Programs[test.name]
qt.Assert(t, qt.IsNotNil(progSpec))

t.Run(test.name, func(t *testing.T) {
t.Log(progSpec.Instructions)
_, err := ebpf.NewProgramWithOptions(progSpec, ebpf.ProgramOptions{
KernelTypes: empty,
})
testutils.SkipIfNotSupported(t, err)

var ve *ebpf.VerifierError
qt.Assert(t, qt.ErrorAs(err, &ve))
found := slices.ContainsFunc(ve.Log, func(line string) bool {
return strings.HasPrefix(line, "; instruction poisoned by CO-RE")
})
qt.Assert(t, qt.IsTrue(found))
t.Logf("%-5v", ve)
})
}
}