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

Unsafe variable pointer #1691

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (mm *Memory) ReadOnly() bool {

// bounds returns true if an access at off of the given size is within bounds.
func (mm *Memory) bounds(off uint64, size uint64) bool {
return off+size < uint64(len(mm.b))
return off < uint64(len(mm.b)) && off+size <= uint64(len(mm.b))
}

// ReadAt implements [io.ReaderAt]. Useful for creating a new [io.OffsetWriter].
Expand Down Expand Up @@ -143,3 +143,16 @@ func (mm *Memory) WriteAt(p []byte, off int64) (int, error) {

return n, nil
}

func (mm *Memory) sliceAt(off uint64, size uint64) ([]byte, error) {
if mm.b == nil {
return nil, fmt.Errorf("memory-mapped region closed")
}
if mm.ro {
return nil, fmt.Errorf("memory-mapped region not writable: %w", ErrReadOnly)
}
if !mm.bounds(off, size) {
return nil, fmt.Errorf("memory-mapped region access out of bounds: %w", io.EOF)
}
return mm.b[off : off+size : off+size], nil
}
6 changes: 3 additions & 3 deletions memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ func TestMemoryBounds(t *testing.T) {
qt.Assert(t, qt.IsNil(err))

size := uint64(mm.Size())
end := size - 1
end := size

qt.Assert(t, qt.IsTrue(mm.bounds(0, 0)))
qt.Assert(t, qt.IsTrue(mm.bounds(end, 0)))
qt.Assert(t, qt.IsFalse(mm.bounds(end, 0)))
qt.Assert(t, qt.IsTrue(mm.bounds(end-8, 8)))
qt.Assert(t, qt.IsTrue(mm.bounds(0, end)))

qt.Assert(t, qt.IsFalse(mm.bounds(end-8, 9)))
qt.Assert(t, qt.IsFalse(mm.bounds(end, 1)))
qt.Assert(t, qt.IsFalse(mm.bounds(0, size)))
qt.Assert(t, qt.IsTrue(mm.bounds(0, size)))
Comment on lines -63 to +72
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I fail to understand something? Especially qt.IsFalse(mm.bounds(0, size))).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix posted in #1694.

}

func TestMemoryReadOnly(t *testing.T) {
Expand Down
Empty file added unsafe/enable_golinkname.s
Empty file.
16 changes: 16 additions & 0 deletions unsafe/variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package unsafe

import (
"unsafe"

"github.com/cilium/ebpf"
)

// VariablePointer enables direct access to the variable, bypassing .Get()
// and .Set() API.
//
// The pointer WILL become dangling if the Variable is collected in
// meantime.
//
//go:linkname VariablePointer github.com/cilium/ebpf.unsafeVariablePointer
func VariablePointer(v *ebpf.Variable) (unsafe.Pointer, error)
12 changes: 12 additions & 0 deletions unsafe/variable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package unsafe

import (
"testing"

"github.com/cilium/ebpf"
)

func TestVariablePointer(*testing.T) {
// ensure that linkname is correct; underlying routine tested elsewhere
VariablePointer(&ebpf.Variable{})
}
19 changes: 19 additions & 0 deletions unsafe_variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ebpf

import (
"unsafe"
)

//go:linkname unsafeVariablePointer
func unsafeVariablePointer(v *Variable) (unsafe.Pointer, error) {
if v.mm == nil {
return nil, errNoDirectVariableAccess(v)
}

b, err := v.mm.sliceAt(v.offset, v.size)
if err != nil {
return nil, err
}

return unsafe.Pointer(&b[0]), nil
}
8 changes: 6 additions & 2 deletions variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (v *Variable) String() string {
// to the same length as the size of the Variable.
func (v *Variable) Set(in any) error {
if v.mm == nil {
return fmt.Errorf("variable %s: direct access requires Linux 5.5 or later: %w", v.name, ErrNotSupported)
return errNoDirectVariableAccess(v)
}

if v.ReadOnly() {
Expand All @@ -215,7 +215,7 @@ func (v *Variable) Set(in any) error {
// be a pointer to a value whose size matches the Variable.
func (v *Variable) Get(out any) error {
if v.mm == nil {
return fmt.Errorf("variable %s: direct access requires Linux 5.5 or later: %w", v.name, ErrNotSupported)
return errNoDirectVariableAccess(v)
}

if !v.mm.bounds(v.offset, v.size) {
Expand All @@ -228,3 +228,7 @@ func (v *Variable) Get(out any) error {

return nil
}

func errNoDirectVariableAccess(v *Variable) error {
return fmt.Errorf("variable %s: direct access requires Linux 5.5 or later: %w", v.name, ErrNotSupported)
}
8 changes: 8 additions & 0 deletions variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ func TestVariable(t *testing.T) {

qt.Assert(t, qt.IsNotNil(obj.Data.Type()))
qt.Assert(t, qt.IsNotNil(obj.Struct.Type()))

bssPtr, err := unsafeVariablePointer(obj.BSS)
qt.Assert(t, qt.IsNil(err))
*(*uint32)(bssPtr) = 42
mustReturn(t, obj.GetBSS, uint32(42))
}

func TestVariableConst(t *testing.T) {
Expand Down Expand Up @@ -153,6 +158,9 @@ func TestVariableConst(t *testing.T) {

qt.Assert(t, qt.IsTrue(obj.Rodata.ReadOnly()))
qt.Assert(t, qt.ErrorIs(obj.Rodata.Set(want), ErrReadOnly))

_, err = unsafeVariablePointer(obj.Rodata)
qt.Assert(t, qt.ErrorIs(err, ErrReadOnly))
}

func TestVariableFallback(t *testing.T) {
Expand Down