| 1 |
package ebpf |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/cilium/ebpf/asm" |
| 8 |
"github.com/cilium/ebpf/btf" |
| 9 |
"github.com/cilium/ebpf/internal/testutils" |
| 10 |
|
| 11 |
"github.com/go-quicktest/qt" |
| 12 |
) |
| 13 |
|
| 14 |
func TestFindReferences(t *testing.T) { |
| 15 |
progs := map[string]*ProgramSpec{ |
| 16 |
"entrypoint": { |
| 17 |
Type: SocketFilter, |
| 18 |
Instructions: asm.Instructions{ |
| 19 |
|
| 20 |
|
| 21 |
asm.Mov.Reg(asm.R0, asm.R1), |
| 22 |
asm.Call.Label("my_func"), |
| 23 |
asm.Return(), |
| 24 |
}, |
| 25 |
License: "MIT", |
| 26 |
}, |
| 27 |
"my_other_func": { |
| 28 |
Instructions: asm.Instructions{ |
| 29 |
asm.LoadImm(asm.R0, 1337, asm.DWord).WithSymbol("my_other_func"), |
| 30 |
asm.Return(), |
| 31 |
}, |
| 32 |
}, |
| 33 |
"my_func": { |
| 34 |
Instructions: asm.Instructions{ |
| 35 |
asm.Call.Label("my_other_func").WithSymbol("my_func"), |
| 36 |
asm.Return(), |
| 37 |
}, |
| 38 |
}, |
| 39 |
} |
| 40 |
|
| 41 |
flattenPrograms(progs, []string{"entrypoint"}) |
| 42 |
|
| 43 |
prog, err := newProgram(t, progs["entrypoint"], nil) |
| 44 |
testutils.SkipIfNotSupported(t, err) |
| 45 |
qt.Assert(t, qt.IsNil(err)) |
| 46 |
|
| 47 |
ret := mustRun(t, prog, nil) |
| 48 |
|
| 49 |
if ret != 1337 { |
| 50 |
t.Errorf("Expected return code 1337, got %d", ret) |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
func TestForwardFunctionDeclaration(t *testing.T) { |
| 55 |
file := testutils.NativeFile(t, "testdata/fwd_decl-%s.elf") |
| 56 |
coll, err := LoadCollectionSpec(file) |
| 57 |
if err != nil { |
| 58 |
t.Fatal(err) |
| 59 |
} |
| 60 |
|
| 61 |
spec := coll.Programs["call_fwd"] |
| 62 |
|
| 63 |
|
| 64 |
_, err = newProgram(t, spec, nil) |
| 65 |
if !errors.Is(err, asm.ErrUnsatisfiedProgramReference) { |
| 66 |
t.Fatal("Expected an error wrapping ErrUnsatisfiedProgramReference, got:", err) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
spec.Instructions = append(spec.Instructions, |
| 71 |
asm.Mov.Imm32(asm.R0, 23).WithSymbol("fwd"), |
| 72 |
asm.Return(), |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
for i, ins := range spec.Instructions { |
| 78 |
if btf.FuncMetadata(&ins) != nil || ins.Source() != nil { |
| 79 |
sym := ins.Symbol() |
| 80 |
ref := ins.Reference() |
| 81 |
ins.Metadata = asm.Metadata{} |
| 82 |
spec.Instructions[i] = ins.WithSymbol(sym).WithReference(ref) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
prog, err := newProgram(t, spec, nil) |
| 87 |
testutils.SkipIfNotSupported(t, err) |
| 88 |
qt.Assert(t, qt.IsNil(err)) |
| 89 |
|
| 90 |
ret := mustRun(t, prog, nil) |
| 91 |
|
| 92 |
if ret != 23 { |
| 93 |
t.Fatalf("Expected 23, got %d", ret) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestFlattenInstructionsAllocations(t *testing.T) { |
| 98 |
name := "entrypoint" |
| 99 |
instructions := asm.Instructions{ |
| 100 |
asm.LoadImm(asm.R0, 0, asm.DWord), |
| 101 |
asm.Return(), |
| 102 |
} |
| 103 |
prog := &ProgramSpec{ |
| 104 |
Name: name, |
| 105 |
Instructions: instructions, |
| 106 |
} |
| 107 |
progs := map[string]*ProgramSpec{name: prog} |
| 108 |
refs := make(map[*ProgramSpec][]string) |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
allocs := testing.AllocsPerRun(5, func() { |
| 113 |
_ = flattenInstructions(name, progs, refs) |
| 114 |
}) |
| 115 |
qt.Assert(t, qt.Equals(allocs, float64(0))) |
| 116 |
} |