Skip to content

Commit

Permalink
collection: don't copy btf.Spec in CollectionSpec.Copy()
Browse files Browse the repository at this point in the history
Copying btf.Spec requires updating Map/Program.BTF.(S,s)pec, which gets
complicated due to btf.Map/Program being pointers.

Instead of implementing a full deep-copy, avoid copying btf.Spec for now,
since it's considered read-only. Since we're planning to get rid of
btf.Program/Map anyway, this would not be worth the investment.

Add a test that loads both a copy and an original CollectionSpec.

Signed-off-by: Timo Beckers <timo@isovalent.com>
  • Loading branch information
ti-mo committed Feb 23, 2022
1 parent ba2072b commit 1c32cfe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion collection.go
Expand Up @@ -28,6 +28,7 @@ type CollectionSpec struct {
Programs map[string]*ProgramSpec

// Types holds type information about Maps and Programs.
// Modifications to Types are currently undefined behaviour.
Types *btf.Spec

// ByteOrder specifies whether the ELF was compiled for
Expand All @@ -45,7 +46,7 @@ func (cs *CollectionSpec) Copy() *CollectionSpec {
Maps: make(map[string]*MapSpec, len(cs.Maps)),
Programs: make(map[string]*ProgramSpec, len(cs.Programs)),
ByteOrder: cs.ByteOrder,
Types: cs.Types.Copy(),
Types: cs.Types,
}

for name, spec := range cs.Maps {
Expand Down
29 changes: 27 additions & 2 deletions collection_test.go
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/internal"
"github.com/cilium/ebpf/internal/btf"
"github.com/cilium/ebpf/internal/testutils"
)
Expand Down Expand Up @@ -83,8 +84,32 @@ func TestCollectionSpecCopy(t *testing.T) {
t.Error("Copy returned same Programs")
}

if cpy.Types == cs.Types {
t.Error("Copy returned same Types")
if cpy.Types != cs.Types {
t.Error("Copy returned different Types")
}
}

func TestCollectionSpecLoadCopy(t *testing.T) {
file := fmt.Sprintf("testdata/loader-%s.elf", internal.ClangEndian)
spec, err := LoadCollectionSpec(file)
if err != nil {
t.Fatal(err)
}

spec2 := spec.Copy()

var objs struct {
Prog *Program `ebpf:"xdp_prog"`
}

err = spec.LoadAndAssign(&objs, nil)
testutils.SkipIfNotSupported(t, err)
if err != nil {
t.Fatal("Loading original spec:", err)
}

if err := spec2.LoadAndAssign(&objs, nil); err != nil {
t.Fatal("Loading copied spec:", err)
}
}

Expand Down

0 comments on commit 1c32cfe

Please sign in to comment.