| 1 |
package ebpf |
| 2 |
|
| 3 |
import ( |
| 4 |
"runtime" |
| 5 |
"structs" |
| 6 |
"sync/atomic" |
| 7 |
"testing" |
| 8 |
"unsafe" |
| 9 |
|
| 10 |
"github.com/go-quicktest/qt" |
| 11 |
|
| 12 |
"github.com/cilium/ebpf/internal/sys" |
| 13 |
) |
| 14 |
|
| 15 |
func TestUnsafeMemoryDisabled(t *testing.T) { |
| 16 |
mm, err := mustMmapableArray(t, 0).Memory() |
| 17 |
qt.Assert(t, qt.IsNil(err)) |
| 18 |
|
| 19 |
_, err = memoryPointer[uint32](mm, 0) |
| 20 |
qt.Assert(t, qt.ErrorIs(err, ErrNotSupported)) |
| 21 |
} |
| 22 |
|
| 23 |
func TestUnsafeMemoryUnmap(t *testing.T) { |
| 24 |
mm, err := mustMmapableArray(t, 0).unsafeMemory() |
| 25 |
qt.Assert(t, qt.IsNil(err)) |
| 26 |
|
| 27 |
|
| 28 |
runtime.SetFinalizer(unsafe.SliceData(mm.b), nil) |
| 29 |
|
| 30 |
|
| 31 |
unmap(int(mm.Size()))(unsafe.SliceData(mm.b)) |
| 32 |
} |
| 33 |
|
| 34 |
func TestUnsafeMemoryPointer(t *testing.T) { |
| 35 |
m := mustMmapableArray(t, 0) |
| 36 |
mm, err := m.unsafeMemory() |
| 37 |
qt.Assert(t, qt.IsNil(err)) |
| 38 |
|
| 39 |
|
| 40 |
_, err = memoryPointer[uint32](mm, 7) |
| 41 |
qt.Assert(t, qt.IsNotNil(err)) |
| 42 |
|
| 43 |
u64, err := memoryPointer[uint64](mm, 8) |
| 44 |
qt.Assert(t, qt.IsNil(err)) |
| 45 |
|
| 46 |
want := uint64(0xf00d) |
| 47 |
*u64 = want |
| 48 |
qt.Assert(t, qt.Equals(*u64, want)) |
| 49 |
|
| 50 |
|
| 51 |
var got uint64 |
| 52 |
qt.Assert(t, qt.IsNil(m.Lookup(uint32(1), &got))) |
| 53 |
qt.Assert(t, qt.Equals(got, want)) |
| 54 |
|
| 55 |
_, err = memoryPointer[*uint32](mm, 0) |
| 56 |
qt.Assert(t, qt.ErrorIs(err, ErrInvalidType)) |
| 57 |
} |
| 58 |
|
| 59 |
func TestUnsafeMemoryReadOnly(t *testing.T) { |
| 60 |
rd, err := mustMmapableArray(t, sys.BPF_F_RDONLY_PROG).unsafeMemory() |
| 61 |
qt.Assert(t, qt.IsNil(err)) |
| 62 |
|
| 63 |
|
| 64 |
qt.Assert(t, qt.IsTrue(rd.ReadOnly())) |
| 65 |
|
| 66 |
|
| 67 |
frozen := mustMmapableArray(t, 0) |
| 68 |
qt.Assert(t, qt.IsNil(frozen.Freeze())) |
| 69 |
fz, err := frozen.Memory() |
| 70 |
qt.Assert(t, qt.IsNil(err)) |
| 71 |
qt.Assert(t, qt.IsTrue(fz.ReadOnly())) |
| 72 |
|
| 73 |
_, err = fz.WriteAt([]byte{1}, 0) |
| 74 |
qt.Assert(t, qt.ErrorIs(err, ErrReadOnly)) |
| 75 |
|
| 76 |
_, err = memoryPointer[uint32](fz, 0) |
| 77 |
qt.Assert(t, qt.ErrorIs(err, ErrReadOnly)) |
| 78 |
} |
| 79 |
|
| 80 |
func TestCheckUnsafeMemory(t *testing.T) { |
| 81 |
mm, err := mustMmapableArray(t, 0).unsafeMemory() |
| 82 |
qt.Assert(t, qt.IsNil(err)) |
| 83 |
|
| 84 |
|
| 85 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[bool](mm, 0))) |
| 86 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[uint32](mm, 0))) |
| 87 |
|
| 88 |
|
| 89 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[[4]byte](mm, 0))) |
| 90 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[[2]struct { |
| 91 |
_ structs.HostLayout |
| 92 |
A uint32 |
| 93 |
B uint64 |
| 94 |
}](mm, 0))) |
| 95 |
|
| 96 |
|
| 97 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[struct { |
| 98 |
_ structs.HostLayout |
| 99 |
_ uint32 |
| 100 |
}](mm, 0))) |
| 101 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[struct { |
| 102 |
_ structs.HostLayout |
| 103 |
_ [4]byte |
| 104 |
}](mm, 0))) |
| 105 |
|
| 106 |
|
| 107 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[atomic.Uint32](mm, 0))) |
| 108 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[struct { |
| 109 |
_ structs.HostLayout |
| 110 |
_ atomic.Uint32 |
| 111 |
}](mm, 0))) |
| 112 |
|
| 113 |
|
| 114 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[uintptr](mm, 0))) |
| 115 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[atomic.Uintptr](mm, 0))) |
| 116 |
qt.Assert(t, qt.IsNil(checkUnsafeMemory[struct { |
| 117 |
_ structs.HostLayout |
| 118 |
_ uintptr |
| 119 |
}](mm, 0))) |
| 120 |
|
| 121 |
|
| 122 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[*uint32](mm, 0), ErrInvalidType)) |
| 123 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[**uint32](mm, 0), ErrInvalidType)) |
| 124 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[[1]*uint8](mm, 0), ErrInvalidType)) |
| 125 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[struct { |
| 126 |
_ structs.HostLayout |
| 127 |
_ *uint8 |
| 128 |
}](mm, 0), ErrInvalidType)) |
| 129 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[atomic.Pointer[uint64]](mm, 0), ErrInvalidType)) |
| 130 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[atomic.Value](mm, 0), ErrInvalidType)) |
| 131 |
|
| 132 |
|
| 133 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[int](mm, 0), ErrInvalidType)) |
| 134 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[uint](mm, 0), ErrInvalidType)) |
| 135 |
|
| 136 |
|
| 137 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[any](mm, 0), ErrInvalidType)) |
| 138 |
|
| 139 |
|
| 140 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[struct{ _ structs.HostLayout }](mm, 0), ErrInvalidType)) |
| 141 |
|
| 142 |
|
| 143 |
qt.Assert(t, qt.ErrorIs(checkUnsafeMemory[struct{ _ uint32 }](mm, 0), ErrInvalidType)) |
| 144 |
} |