Skip to content

prog: get context out from syscall program #1803

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,16 @@ func (p *Program) run(opts *RunOptions) (uint32, time.Duration, error) {
Cpu: opts.CPU,
}

if p.Type() == Syscall && ctxIn != nil && ctxOut != nil {
// Linux syscall program errors on non-nil ctxOut, uses ctxIn
// for both input and output. Shield the user from this wart.
if len(ctxIn) != len(ctxOut) {
return 0, 0, errors.New("length mismatch: Context and ContextOut")
}
attr.CtxOut, attr.CtxSizeOut = sys.TypedPointer[uint8]{}, 0
ctxOut = ctxIn
}

retry:
for {
err := sys.ProgRun(&attr)
Expand Down
35 changes: 35 additions & 0 deletions prog_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/sys"
"github.com/cilium/ebpf/internal/testutils"
"github.com/cilium/ebpf/internal/unix"
)
Expand Down Expand Up @@ -135,3 +136,37 @@ func TestProgramVerifierLogLinux(t *testing.T) {
})
qt.Assert(t, qt.IsTrue(len(prog.VerifierLog) > minVerifierLogSize))
}

func TestProgramTestRunSyscall(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.14", "BPF_PROG_TYPE_SYSCALL")

prog := mustNewProgram(t, &ProgramSpec{
Type: Syscall,
Flags: sys.BPF_F_SLEEPABLE,
License: "MIT",
Instructions: []asm.Instruction{
// fn (ctx *u64) { *ctx++; return *ctx }
asm.LoadMem(asm.R0, asm.R1, 0, asm.DWord),
asm.Add.Imm(asm.R0, 1),
asm.StoreMem(asm.R1, 0, asm.R0, asm.DWord),
asm.Return(),
},
}, nil)

// only Context
rc, err := prog.Run(&RunOptions{Context: uint64(42)})
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(rc, 43))

// Context and ContextOut
out := uint64(0)
rc, err = prog.Run(&RunOptions{Context: uint64(99), ContextOut: &out})
if err != nil {
t.Fatal(err)
}
qt.Assert(t, qt.Equals(rc, 100))
qt.Assert(t, qt.Equals(out, 100))
}
Loading