From: Timo Beckers Date: Wed, 5 Nov 2025 12:08:54 +0000 (+0100) Subject: collection: remove deprecated CollectionSpec.RewriteConstants X-Git-Url: https://git.feebdaed.xyz/?a=commitdiff_plain;h=589910b6a77d140e70502ab4fe8511a8960c3d24;p=0xmirror%2Febpf.git collection: remove deprecated CollectionSpec.RewriteConstants This has been deprecated since v0.17.0. Use CollectionSpec.Variables instead. Signed-off-by: Timo Beckers --- diff --git a/collection.go b/collection.go index 83f1b40..a4ce39b 100644 --- a/collection.go +++ b/collection.go @@ -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 diff --git a/elf_reader_test.go b/elf_reader_test.go index f37659a..3861b68 100644 --- a/elf_reader_test.go +++ b/elf_reader_test.go @@ -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()