]> git.feebdaed.xyz Git - 0xmirror/ebpf.git/commitdiff
collection: remove deprecated CollectionSpec.RewriteConstants
authorTimo Beckers <timo@isovalent.com>
Wed, 5 Nov 2025 12:08:54 +0000 (13:08 +0100)
committerTimo Beckers <ti-mo@users.noreply.github.com>
Thu, 27 Nov 2025 10:39:30 +0000 (11:39 +0100)
This has been deprecated since v0.17.0. Use CollectionSpec.Variables instead.

Signed-off-by: Timo Beckers <timo@isovalent.com>
collection.go
elf_reader_test.go

index 83f1b407a017b3da1af9573f77160cb48f717de6..a4ce39bdf807a6b3e7143ddc56df3e2efcb2514b 100644 (file)
@@ -94,58 +94,6 @@ func copyMapOfSpecs[T interface{ Copy() T }](m map[string]T) map[string]T {
        return cpy
 }
 
-// MissingConstantsError is returned by [CollectionSpec.RewriteConstants].
-type MissingConstantsError struct {
-       // The constants missing from .rodata.
-       Constants []string
-}
-
-func (m *MissingConstantsError) Error() string {
-       return fmt.Sprintf("some constants are missing from .rodata: %s", strings.Join(m.Constants, ", "))
-}
-
-// RewriteConstants replaces the value of multiple constants.
-//
-// The constant must be defined like so in the C program:
-//
-//     volatile const type foobar;
-//     volatile const type foobar = default;
-//
-// Replacement values must be of the same length as the C sizeof(type).
-// If necessary, they are marshalled according to the same rules as
-// map values.
-//
-// From Linux 5.5 the verifier will use constants to eliminate dead code.
-//
-// Returns an error wrapping [MissingConstantsError] if a constant doesn't exist.
-//
-// Deprecated: Use [CollectionSpec.Variables] to interact with constants instead.
-// RewriteConstants is now a wrapper around the VariableSpec API.
-func (cs *CollectionSpec) RewriteConstants(consts map[string]interface{}) error {
-       var missing []string
-       for n, c := range consts {
-               v, ok := cs.Variables[n]
-               if !ok {
-                       missing = append(missing, n)
-                       continue
-               }
-
-               if !v.Constant() {
-                       return fmt.Errorf("variable %s is not a constant", n)
-               }
-
-               if err := v.Set(c); err != nil {
-                       return fmt.Errorf("rewriting constant %s: %w", n, err)
-               }
-       }
-
-       if len(missing) != 0 {
-               return fmt.Errorf("rewrite constants: %w", &MissingConstantsError{Constants: missing})
-       }
-
-       return nil
-}
-
 // Assign the contents of a CollectionSpec to a struct.
 //
 // This function is a shortcut to manually checking the presence
index f37659a8a7aa8eca3270f6835139a1ec43fead1b..3861b68218c9070db027387a180e7723afc1c30d 100644 (file)
@@ -242,33 +242,14 @@ func TestLoadCollectionSpec(t *testing.T) {
                        t.Fatal("Can't parse ELF:", err)
                }
 
-               err = have.RewriteConstants(map[string]interface{}{
-                       "arg":  uint32(1),
-                       "arg2": uint32(2),
-               })
-               if err != nil {
-                       t.Fatal("Can't rewrite constant:", err)
-               }
-
-               err = have.RewriteConstants(map[string]interface{}{
-                       "totallyBogus":  uint32(1),
-                       "totallyBogus2": uint32(2),
-               })
-               if err == nil {
-                       t.Error("Rewriting a bogus constant doesn't fail")
-               }
-
-               var mErr *MissingConstantsError
-               if !errors.As(err, &mErr) {
-                       t.Fatal("Error doesn't wrap MissingConstantsError:", err)
-               }
-               qt.Assert(t, qt.ContentEquals(mErr.Constants, []string{"totallyBogus", "totallyBogus2"}))
-
                qt.Assert(t, qt.Equals(have.Maps["perf_event_array"].ValueSize, 0))
                qt.Assert(t, qt.IsNotNil(have.Maps["perf_event_array"].Value))
 
                qt.Assert(t, qt.CmpEquals(have, coll, csCmpOpts))
 
+               qt.Assert(t, qt.IsNil(have.Variables["arg"].Set(uint32(1))))
+               qt.Assert(t, qt.IsNil(have.Variables["arg2"].Set(uint32(2))))
+
                if have.ByteOrder != internal.NativeEndian {
                        return
                }
@@ -361,11 +342,7 @@ func TestFreezeRodata(t *testing.T) {
                Program *Program `ebpf:"freeze_rodata"`
        }
 
-       if err := spec.RewriteConstants(map[string]interface{}{
-               "ret": uint32(1),
-       }); err != nil {
-               t.Fatal(err)
-       }
+       qt.Assert(t, qt.IsNil(spec.Variables["ret"].Set(uint32(1))))
 
        mustLoadAndAssign(t, spec, &obj, nil)
        obj.Program.Close()