Skip to content

Commit

Permalink
prog: test for linking unimplemented forward function declarations
Browse files Browse the repository at this point in the history
This test makes sure the linker allows calls into missing symbols
so that they can be provided by the caller after the CollectionSpec
has been emitted.
  • Loading branch information
arthurfabre authored and ti-mo committed Jan 20, 2022
1 parent 4af6898 commit fa64336
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ TARGETS := \
testdata/iproute2_map_compat \
testdata/map_spin_lock \
testdata/subprog_reloc \
testdata/fwd_decl \
internal/btf/testdata/relocs

.PHONY: all clean container-all container-shell generate
Expand Down
46 changes: 46 additions & 0 deletions linker_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ebpf

import (
"errors"
"testing"

"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/testutils"
)

Expand Down Expand Up @@ -54,3 +56,47 @@ func TestFindReferences(t *testing.T) {
t.Errorf("Expected return code 1337, got %d", ret)
}
}

func TestForwardFunctionDeclaration(t *testing.T) {
testutils.Files(t, testutils.Glob(t, "testdata/fwd_decl-*.elf"), func(t *testing.T, file string) {
coll, err := LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
}

if coll.ByteOrder != internal.NativeEndian {
t.Skip()
}

spec := coll.Programs["call_fwd"]

// This program calls an unimplemented forward function declaration.
_, err = NewProgram(spec)
if !errors.Is(err, errUnsatisfiedReference) {
t.Fatal("Expected an error wrapping errUnsatisfiedReference, got:", err)
}

// Append the implementation of fwd().
spec.Instructions = append(spec.Instructions,
asm.Mov.Imm32(asm.R0, 23).Sym("fwd"),
asm.Return(),
)

// The body of the subprog we appended does not come with BTF func_infos,
// so the verifier will reject it. Load without BTF.
spec.BTF = nil

prog, err := NewProgram(spec)
if err != nil {
t.Fatal(err)
}

ret, _, err := prog.Test(make([]byte, 14))
if err != nil {
t.Fatal("Running program:", err)
}
if ret != 23 {
t.Fatalf("Expected 23, got %d", ret)
}
})
}
Binary file added testdata/fwd_decl-eb.elf
Binary file not shown.
Binary file added testdata/fwd_decl-el.elf
Binary file not shown.
10 changes: 10 additions & 0 deletions testdata/fwd_decl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* This file excercises the ELF loader. It is not a valid BPF program. */

#include "common.h"

// Forward function declaration, never implemented.
int fwd();

__section("socket") int call_fwd() {
return fwd();
}

0 comments on commit fa64336

Please sign in to comment.