Skip to content

Commit

Permalink
*: use internal.EmptyBPFContext() for empty PROG_TEST_RUN input buffers
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed Sep 14, 2022
1 parent 1275781 commit 6624eb1
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 32 deletions.
4 changes: 2 additions & 2 deletions btf/core_reloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestCORERelocationLoad(t *testing.T) {
}
defer prog.Close()

ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Error when running:", err)
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestCORERelocationRead(t *testing.T) {
}
defer prog.Close()

ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Error when running:", err)
Expand Down
4 changes: 2 additions & 2 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestCollectionSpecRewriteMaps(t *testing.T) {
}
defer coll.Close()

ret, _, err := coll.Programs["test-prog"].Test(make([]byte, 14))
ret, _, err := coll.Programs["test-prog"].Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestCollectionSpecMapReplacements(t *testing.T) {
}
defer coll.Close()

ret, _, err := coll.Programs["test-prog"].Test(make([]byte, 14))
ret, _, err := coll.Programs["test-prog"].Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
Expand Down
10 changes: 5 additions & 5 deletions elf_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func TestLoadCollectionSpec(t *testing.T) {
}
defer coll.Close()

ret, _, err := coll.Programs["xdp_prog"].Test(make([]byte, 14))
ret, _, err := coll.Programs["xdp_prog"].Test(internal.EmptyBPFContext)
if err != nil {
t.Fatal("Can't run program:", err)
}
Expand Down Expand Up @@ -255,7 +255,7 @@ func TestDataSections(t *testing.T) {
}
defer obj.Program.Close()

ret, _, err := obj.Program.Test(make([]byte, 14))
ret, _, err := obj.Program.Test(internal.EmptyBPFContext)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -462,7 +462,7 @@ func TestStringSection(t *testing.T) {
}

_, err = prog.Run(&RunOptions{
Data: make([]byte, 14), // Min size for XDP programs
Data: internal.EmptyBPFContext, // Min size for XDP programs
})
if err != nil {
t.Fatalf("prog run: %s", err)
Expand Down Expand Up @@ -530,7 +530,7 @@ func TestTailCall(t *testing.T) {
defer obj.TailMain.Close()
defer obj.ProgArray.Close()

ret, _, err := obj.TailMain.Test(make([]byte, 14))
ret, _, err := obj.TailMain.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -569,7 +569,7 @@ func TestSubprogRelocation(t *testing.T) {
defer obj.Main.Close()
defer obj.HashMap.Close()

ret, _, err := obj.Main.Test(make([]byte, 14))
ret, _, err := obj.Main.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ func BenchmarkStats(b *testing.B) {
// be of a specific value after a call to Test() is therefore not possible.
// See https://golang.org/doc/go1.14#runtime for more details.
func testStats(prog *Program) error {
in := make([]byte, 14)
in := internal.EmptyBPFContext

stats, err := EnableStats(uint32(unix.BPF_STATS_RUN_TIME))
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestFindReferences(t *testing.T) {
}
defer prog.Close()

ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestForwardFunctionDeclaration(t *testing.T) {
}
defer prog.Close()

ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
if err != nil {
t.Fatal("Running program:", err)
}
Expand Down
18 changes: 9 additions & 9 deletions perf/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestPerfReader(t *testing.T) {
}
defer rd.Close()

ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -236,7 +236,7 @@ func TestPerfReaderLostSample(t *testing.T) {
}
defer rd.Close()

ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -338,7 +338,7 @@ func TestPause(t *testing.T) {
}

// Write a sample. The reader should read it.
ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil || ret != 0 {
t.Fatal("Can't write sample")
Expand All @@ -357,7 +357,7 @@ func TestPause(t *testing.T) {
_, err := rd.Read()
errChan <- err
}()
ret, _, err = prog.Test(make([]byte, 14))
ret, _, err = prog.Test(internal.EmptyBPFContext)
if err == nil && ret == 0 {
t.Fatal("Unexpectedly wrote sample while paused")
} // else Success
Expand All @@ -378,7 +378,7 @@ func TestPause(t *testing.T) {
if err = rd.Resume(); err != nil {
t.Fatal(err)
}
ret, _, err = prog.Test(make([]byte, 14))
ret, _, err = prog.Test(internal.EmptyBPFContext)
if err != nil || ret != 0 {
t.Fatal("Can't write sample")
}
Expand Down Expand Up @@ -414,7 +414,7 @@ func BenchmarkReader(b *testing.B) {
}
defer rd.Close()

buf := make([]byte, 14)
buf := internal.EmptyBPFContext

b.ResetTimer()
b.ReportAllocs()
Expand All @@ -441,7 +441,7 @@ func BenchmarkReadInto(b *testing.B) {
}
defer rd.Close()

buf := make([]byte, 14)
buf := internal.EmptyBPFContext

b.ResetTimer()
b.ReportAllocs()
Expand Down Expand Up @@ -503,7 +503,7 @@ func ExampleReader() {
defer rd.Close()

// Writes out a sample with content 1,2,3,4,4
ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
if err != nil || ret != 0 {
panic("Can't write sample")
}
Expand Down Expand Up @@ -531,7 +531,7 @@ func ExampleReader_ReadInto() {

for i := 0; i < 2; i++ {
// Write out two samples
ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
if err != nil || ret != 0 {
panic("Can't write sample")
}
Expand Down
4 changes: 2 additions & 2 deletions prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func (p *Program) Run(opts *RunOptions) (uint32, error) {
// run or an error. reset is called whenever the benchmark syscall is
// interrupted, and should be set to testing.B.ResetTimer or similar.
//
// Note: profiling a call to this function will skew it's results, see
// Note: profiling a call to this function will skew its results, see
// https://github.com/cilium/ebpf/issues/24
//
// This function requires at least Linux 4.12.
Expand Down Expand Up @@ -624,7 +624,7 @@ var haveProgTestRun = internal.FeatureTest("BPF_PROG_TEST_RUN", "4.12", func() e
}
defer prog.Close()

in := make([]byte, 15)
in := internal.EmptyBPFContext
attr := sys.ProgRunAttr{
ProgFd: uint32(prog.FD()),
DataSizeIn: uint32(len(in)),
Expand Down
10 changes: 5 additions & 5 deletions prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestProgramRun(t *testing.T) {
testutils.SkipOnOldKernel(t, "4.8", "XDP program")

pat := []byte{0xDE, 0xAD, 0xBE, 0xEF}
buf := make([]byte, 14)
buf := internal.EmptyBPFContext

// r1 : ctx_start
// r1+4: ctx_end
Expand Down Expand Up @@ -110,10 +110,10 @@ func TestProgramRunWithOptions(t *testing.T) {
}
defer prog.Close()

buf := make([]byte, 14)
buf := internal.EmptyBPFContext
xdp := sys.XdpMd{
Data: 0,
DataEnd: 14,
DataEnd: uint32(len(buf)),
}
xdpOut := sys.XdpMd{}
opts := RunOptions{
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestProgramRunEmptyData(t *testing.T) {
func TestProgramBenchmark(t *testing.T) {
prog := mustSocketFilter(t)

ret, duration, err := prog.Benchmark(make([]byte, 14), 1, nil)
ret, duration, err := prog.Benchmark(internal.EmptyBPFContext, 1, nil)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Error from Benchmark:", err)
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestProgramTestRunInterrupt(t *testing.T) {
// Block this thread in the BPF syscall, so that we can
// trigger EINTR by sending a signal.
opts := RunOptions{
Data: make([]byte, 14),
Data: internal.EmptyBPFContext,
Repeat: math.MaxInt32,
Reset: func() {
// We don't know how long finishing the
Expand Down
9 changes: 5 additions & 4 deletions ringbuf/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -47,7 +48,7 @@ func TestRingbufReader(t *testing.T) {
}
defer rd.Close()

ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -172,7 +173,7 @@ func TestReaderBlocking(t *testing.T) {
testutils.SkipOnOldKernel(t, "5.8", "BPF ring buffer")

prog, events := mustOutputSamplesProg(t, 0, 5)
ret, _, err := prog.Test(make([]byte, 14))
ret, _, err := prog.Test(internal.EmptyBPFContext)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -269,7 +270,7 @@ func BenchmarkReader(b *testing.B) {
}
defer rd.Close()

buf := make([]byte, 14)
buf := internal.EmptyBPFContext

b.ResetTimer()
b.ReportAllocs()
Expand Down Expand Up @@ -301,7 +302,7 @@ func BenchmarkReadInto(b *testing.B) {
}
defer rd.Close()

buf := make([]byte, 14)
buf := internal.EmptyBPFContext

b.ResetTimer()
b.ReportAllocs()
Expand Down

0 comments on commit 6624eb1

Please sign in to comment.