Skip to content
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

sysenc: special case unmarshaling into []byte #1180

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions internal/sysenc/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"unsafe"

"github.com/cilium/ebpf/internal"

"golang.org/x/exp/slices"
)

// Marshal turns data into a byte slice using the system's native endianness.
Expand Down Expand Up @@ -88,6 +90,11 @@ func Unmarshal(data interface{}, buf []byte) error {
*value = string(buf)
return nil

case *[]byte:
// Backwards compat: unmarshaling into a slice replaces the whole slice.
*value = slices.Clone(buf)
return nil

default:
if dataBuf := unsafeBackingMemory(data); len(dataBuf) == len(buf) {
copy(dataBuf, buf)
Expand Down
6 changes: 1 addition & 5 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -1411,11 +1411,7 @@ func (mi *MapIterator) Next(keyOut, valueOut interface{}) bool {
return false
}

// The user can get access to nextKey since unmarshalBytes
// does not copy when unmarshaling into a []byte.
// Make a copy to prevent accidental corruption of
// iterator state.
copy(mi.curKey, nextKey)
mi.curKey = nextKey

mi.count++
mi.err = mi.target.Lookup(nextKey, valueOut)
Expand Down
12 changes: 5 additions & 7 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func newHash(t *testing.T) *Map {

func TestMap(t *testing.T) {
m := createArray(t)
defer m.Close()

t.Log(m)

Expand Down Expand Up @@ -76,6 +75,10 @@ func TestMap(t *testing.T) {
t.Error("Want value 42, got", v)
}

var slice []byte
qt.Assert(t, m.Lookup(uint32(0), &slice), qt.IsNil)
qt.Assert(t, slice, qt.DeepEquals, internal.NativeEndian.AppendUint32(nil, 42))

var k uint32
if err := m.NextKey(uint32(0), &k); err != nil {
t.Fatal("Can't get:", err)
Expand Down Expand Up @@ -430,7 +433,6 @@ func TestMapCloneNil(t *testing.T) {
func TestMapPin(t *testing.T) {
m := createArray(t)
c := qt.New(t)
defer m.Close()

if err := m.Put(uint32(0), uint32(42)); err != nil {
t.Fatal("Can't put:", err)
Expand Down Expand Up @@ -557,7 +559,6 @@ func TestMapPinMultiple(t *testing.T) {
func TestMapPinWithEmptyPath(t *testing.T) {
m := createArray(t)
c := qt.New(t)
defer m.Close()

err := m.Pin("")

Expand Down Expand Up @@ -694,7 +695,6 @@ func TestMapLoadPinnedWithOptions(t *testing.T) {
testutils.SkipOnOldKernel(t, "4.15", "file_flags in BPF_OBJ_GET")

array := createArray(t)
defer array.Close()

tmp := testutils.TempBPFFS(t)

Expand Down Expand Up @@ -780,6 +780,7 @@ func createArray(t *testing.T) *Map {
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { m.Close() })
return m
}

Expand Down Expand Up @@ -1276,7 +1277,6 @@ func TestIterateMapInMap(t *testing.T) {
defer parent.Close()

a := createArray(t)
defer a.Close()

if err := parent.Put(idx, a); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1529,7 +1529,6 @@ func TestMapName(t *testing.T) {

func TestMapFromFD(t *testing.T) {
m := createArray(t)
defer m.Close()

if err := m.Put(uint32(0), uint32(123)); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -1597,7 +1596,6 @@ func TestMapContents(t *testing.T) {

func TestMapFreeze(t *testing.T) {
arr := createArray(t)
defer arr.Close()

err := arr.Freeze()
testutils.SkipIfNotSupported(t, err)
Expand Down
1 change: 0 additions & 1 deletion prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,6 @@ func TestProgramRejectIncorrectByteOrder(t *testing.T) {

func TestProgramSpecTag(t *testing.T) {
arr := createArray(t)
defer arr.Close()

spec := &ProgramSpec{
Type: SocketFilter,
Expand Down