Skip to content

Commit 3d4ca80

Browse files
lmbti-mo
authored andcommitted
map: don't require BTF to rewrite data section from variable
MapSpec.dataSection() currently enforces the presence of BTF, even though the downstream code doesn't explicitly rely on it. Remove this restriction. Signed-off-by: Lorenz Bauer <[email protected]>
1 parent b2c8981 commit 3d4ca80

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

collection.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,9 @@ func (cl *collectionLoader) populateStructOps(m *Map, mapSpec *MapSpec) error {
615615
return fmt.Errorf("value should be a *Struct")
616616
}
617617

618-
userData, ok := mapSpec.Contents[0].Value.([]byte)
619-
if !ok {
620-
return fmt.Errorf("value should be an array of byte")
618+
userData, err := mapSpec.dataSection()
619+
if err != nil {
620+
return fmt.Errorf("getting data section: %w", err)
621621
}
622622
if len(userData) < int(userType.Size) {
623623
return fmt.Errorf("user data too short: have %d, need at least %d", len(userData), userType.Size)

map.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ var (
2929
ErrKeyExist = errors.New("key already exists")
3030
ErrIterationAborted = errors.New("iteration aborted")
3131
ErrMapIncompatible = errors.New("map spec is incompatible with existing map")
32-
errMapNoBTFValue = errors.New("map spec does not contain a BTF Value")
3332

3433
// pre-allocating these errors here since they may get called in hot code paths
3534
// and cause unnecessary memory allocations
@@ -187,32 +186,23 @@ func (spec *MapSpec) fixupMagicFields() (*MapSpec, error) {
187186
return spec, nil
188187
}
189188

190-
// dataSection returns the contents and BTF Datasec descriptor of the spec.
191-
func (ms *MapSpec) dataSection() ([]byte, *btf.Datasec, error) {
192-
if ms.Value == nil {
193-
return nil, nil, errMapNoBTFValue
194-
}
195-
196-
ds, ok := ms.Value.(*btf.Datasec)
197-
if !ok {
198-
return nil, nil, fmt.Errorf("map value BTF is a %T, not a *btf.Datasec", ms.Value)
199-
}
200-
189+
// dataSection returns the contents of a datasec if the MapSpec represents one.
190+
func (ms *MapSpec) dataSection() ([]byte, error) {
201191
if n := len(ms.Contents); n != 1 {
202-
return nil, nil, fmt.Errorf("expected one key, found %d", n)
192+
return nil, fmt.Errorf("expected one key, found %d", n)
203193
}
204194

205195
kv := ms.Contents[0]
206196
if key, ok := ms.Contents[0].Key.(uint32); !ok || key != 0 {
207-
return nil, nil, fmt.Errorf("expected contents to have key 0")
197+
return nil, fmt.Errorf("expected contents to have key 0")
208198
}
209199

210200
value, ok := kv.Value.([]byte)
211201
if !ok {
212-
return nil, nil, fmt.Errorf("value at first map key is %T, not []byte", kv.Value)
202+
return nil, fmt.Errorf("value at first map key is %T, not []byte", kv.Value)
213203
}
214204

215-
return value, ds, nil
205+
return value, nil
216206
}
217207

218208
// updateDataSection copies the values of variables into MapSpec.Contents[0].Value.
@@ -232,7 +222,7 @@ func (ms *MapSpec) updateDataSection(vars map[string]*VariableSpec, sectionName
232222
return nil
233223
}
234224

235-
data, _, err := ms.dataSection()
225+
data, err := ms.dataSection()
236226
if err != nil {
237227
return err
238228
}

0 commit comments

Comments
 (0)