]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: align covctrs symbol
authorKeith Randall <khr@golang.org>
Fri, 2 Jan 2026 21:42:00 +0000 (13:42 -0800)
committerKeith Randall <khr@golang.org>
Fri, 6 Feb 2026 23:22:38 +0000 (15:22 -0800)
If we start the covctrs blob at an odd alignment, then covctrs will
not be correctly aligned. Each individual entry is aligned properly,
but the start marker may be before any padding inserted to enforce
that alignment.

Fixes #58936

Change-Id: I802fbe40eacfa5a3c8c4864e078b0e078da956d5
Reviewed-on: https://go-review.googlesource.com/c/go/+/733740
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@google.com>
src/cmd/cover/cover_test.go
src/cmd/cover/testdata/align.go [new file with mode: 0644]
src/cmd/cover/testdata/align_test.go [new file with mode: 0644]
src/cmd/link/internal/ld/data.go

index a1149e9e6e6b5d0638e9b01112dd49a01ad6732e..a19e0d09249cd594fc6421c7d58cac74b35bfba5 100644 (file)
@@ -638,3 +638,12 @@ func main() {
                t.Errorf("unexpected success; want failure due to newline in file path")
        }
 }
+
+func TestAlignment(t *testing.T) {
+       // Test that cover data structures are aligned appropriately. See issue 58936.
+       testenv.MustHaveGoRun(t)
+       t.Parallel()
+
+       cmd := testenv.Command(t, testenv.GoToolPath(t), "test", "-cover", filepath.Join(testdata, "align.go"), filepath.Join(testdata, "align_test.go"))
+       run(cmd, t)
+}
diff --git a/src/cmd/cover/testdata/align.go b/src/cmd/cover/testdata/align.go
new file mode 100644 (file)
index 0000000..2e8a453
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+var badSlice [8265]byte
+
+func init() {
+       badSlice[0] = 4
+}
diff --git a/src/cmd/cover/testdata/align_test.go b/src/cmd/cover/testdata/align_test.go
new file mode 100644 (file)
index 0000000..e908c5a
--- /dev/null
@@ -0,0 +1,10 @@
+// Copyright 2026 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+import "testing"
+
+func TestFoo(t *testing.T) {
+}
index 683df3bb67f89149f095e2181c0d5373054d046e..a045ff5eeacf7b61cdcfb7afc4cdc9fb63497bf6 100644 (file)
@@ -2022,6 +2022,13 @@ func (state *dodataState) allocateDataSections(ctxt *Link) {
        ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.covctrs", 0), sect)
        ldr.SetSymSect(ldr.LookupOrCreateSym("runtime.ecovctrs", 0), sect)
 
+       // If we started this blob at an odd alignment, then covctrs will
+       // not be correctly aligned. Each individual entry is aligned properly,
+       // but the start marker may be before any padding inserted to enforce
+       // that alignment. Fix that here. See issue 58936.
+       covCounterDataStartOff += covCounterDataLen % 4
+       covCounterDataLen -= covCounterDataLen % 4
+
        // Coverage instrumentation counters for libfuzzer.
        if len(state.data[sym.SLIBFUZZER_8BIT_COUNTER]) > 0 {
                sect := state.allocateNamedSectionAndAssignSyms(&Segdata, ".go.fuzzcntrs", sym.SLIBFUZZER_8BIT_COUNTER, sym.Sxxx, 06)