Skip to content
Open
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
2 changes: 1 addition & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ func handleMapCreateError(attr sys.MapCreateAttr, spec *MapSpec, err error) erro
return fmt.Errorf("map create: %w (MEMLOCK may be too low, consider rlimit.RemoveMemlock)", err)
}
if errors.Is(err, unix.EINVAL) {
if spec.MaxEntries == 0 {
if spec.MaxEntries == 0 && !spec.Type.requiresZeroMaxEntries() {
return fmt.Errorf("map create: %w (MaxEntries may be incorrectly set to zero)", err)
}
if spec.Type == UnspecifiedMap {
Expand Down
64 changes: 64 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2323,3 +2323,67 @@ func ExampleMap_Iterate_nestedMapsAndProgramArrays() {
panic(fmt.Sprint("Iterator encountered an error:", err))
}
}

func TestMapCreateStorageMisleadingError(t *testing.T) {
tests := []struct {
name string
mapType MapType
version string
}{
{"SkStorage", SkStorage, "5.2"},
{"InodeStorage", InodeStorage, "5.10"},
{"TaskStorage", TaskStorage, "5.11"},
{"CgroupStorage", CgroupStorage, "6.2"},
{"CGroupStorage", CGroupStorage, "4.19"},
{"PerCPUCGroupStorage", PerCPUCGroupStorage, "4.20"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testutils.SkipOnOldKernel(t, tt.version, tt.name+" map")

spec := &MapSpec{
Type: tt.mapType,
KeySize: 4,
ValueSize: 4,
MaxEntries: 0,
Flags: sys.BPF_F_NO_PREALLOC,
}

_, err := NewMap(spec)
qt.Assert(t, qt.IsNotNil(err), qt.Commentf("Expected map creation to fail"))

if errors.Is(err, unix.EINVAL) {
qt.Assert(t, qt.Not(qt.StringContains(err.Error(), "MaxEntries may be incorrectly set to zero")))
}
})
}
}

func TestMapCreateZeroMaxEntriesError(t *testing.T) {
tests := []struct {
name string
mapType MapType
}{
{"Hash", Hash},
{"Array", Array},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
spec := &MapSpec{
Type: tt.mapType,
KeySize: 4,
ValueSize: 4,
MaxEntries: 0, // Invalid zero value
}

_, err := NewMap(spec)
qt.Assert(t, qt.IsNotNil(err), qt.Commentf("Expected map creation to fail"))

if errors.Is(err, unix.EINVAL) {
qt.Assert(t, qt.StringContains(err.Error(), "MaxEntries may be incorrectly set to zero"))
}
})
}
}
10 changes: 10 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,16 @@ func (mt MapType) mustHaveNoPrealloc() bool {
return false
}

// requiresZeroMaxEntries returns true if the map type requires MaxEntries to be zero.
func (mt MapType) requiresZeroMaxEntries() bool {
switch mt {
case CgroupStorage, CGroupStorage, PerCPUCGroupStorage, InodeStorage, TaskStorage, SkStorage:
return true
}

return false
}

// ProgramType of the eBPF program
type ProgramType uint32

Expand Down