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
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
}
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()