Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

variable: copy VariableSpec.Type during initial assignment and during copy #1616

Merged
merged 1 commit into from
Nov 21, 2024
Merged
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
4 changes: 3 additions & 1 deletion elf_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -1233,7 +1233,9 @@ func (ec *elfCode) loadDataSections() error {
return fmt.Errorf("data section %s: variable %s size in datasec (%d) doesn't match ELF symbol size (%d)", sec.Name, name, v.Size, ev.size)
}

ev.t = vt
// Decouple the Var in the VariableSpec from the underlying DataSec in
// the MapSpec to avoid modifications from affecting map loads later on.
ev.t = btf.Copy(vt).(*btf.Var)
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ func (s *VariableSpec) copy(cpy *CollectionSpec) *VariableSpec {
name: s.name,
offset: s.offset,
size: s.size,
t: s.t,
}
if s.t != nil {
out.t = btf.Copy(s.t).(*btf.Var)
}

// Attempt to find a MapSpec with the same name in the copied CollectionSpec.
Expand Down
8 changes: 8 additions & 0 deletions variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ func TestVariableSpecCopy(t *testing.T) {
// Verify that the original underlying MapSpec was not modified.
zero := make([]byte, 4)
qt.Assert(t, qt.DeepEquals(spec.Maps[".rodata"].Contents[0].Value.([]byte), zero))

// Check that modifications to the VariableSpec's Type don't affect the
// underlying MapSpec's type information on either the original or the copy.
cpy.Variables["var_rodata"].Type().Name = "modified"
spec.Variables["var_rodata"].Type().Name = "modified"

qt.Assert(t, qt.Equals(cpy.Maps[".rodata"].Value.(*btf.Datasec).Vars[0].Type.(*btf.Var).Name, "var_rodata"))
qt.Assert(t, qt.Equals(spec.Maps[".rodata"].Value.(*btf.Datasec).Vars[0].Type.(*btf.Var).Name, "var_rodata"))
}

func mustReturn(tb testing.TB, prog *Program, value uint32) {
Expand Down