aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/align_runtime_test.go
diff options
context:
space:
mode:
authorKeith Randall <khr@golang.org>2022-05-17 15:52:20 -0700
committerKeith Randall <khr@golang.org>2022-05-19 20:10:40 +0000
commit6b6813fdb79d426c3693eef2cc423263185cb3a2 (patch)
tree7923dcc44746c49751f7a243eb486691e105841f /src/runtime/align_runtime_test.go
parent9b89c380208ea2e85985ee6bf2b1d684274dfa1d (diff)
downloadgo-6b6813fdb79d426c3693eef2cc423263185cb3a2.tar.xz
runtime: test alignment of fields targeted by 64-bit atomics
Make sure that all the targets of 64-bit atomic operations are actually aligned to 8 bytes. This has been a source of bugs on 32-bit systems. (e.g. CL 399754) The strategy is to have a simple test that just checks the alignment of some explicitly listed fields and global variables. Then there's a more complicated test that makes sure the list used in the simple test is exhaustive. That test has some limitations, but it should catch most cases, particularly new uses of atomic operations on new or existing fields. Unlike a runtime assert, this check is free and will catch accesses that occur even in very unlikely code paths. Change-Id: I25ac78df471ac33b57cb91375bd8453d6ce2814f Reviewed-on: https://go-review.googlesource.com/c/go/+/407034 Run-TryBot: Keith Randall <khr@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: David Chase <drchase@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
Diffstat (limited to 'src/runtime/align_runtime_test.go')
-rw-r--r--src/runtime/align_runtime_test.go71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/runtime/align_runtime_test.go b/src/runtime/align_runtime_test.go
new file mode 100644
index 0000000000..ec7956d1bb
--- /dev/null
+++ b/src/runtime/align_runtime_test.go
@@ -0,0 +1,71 @@
+// Copyright 2022 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.
+
+// This file lives in the runtime package
+// so we can get access to the runtime guts.
+// The rest of the implementation of this test is in align_test.go.
+
+package runtime
+
+import "unsafe"
+
+// AtomicFields is the set of fields on which we perform 64-bit atomic
+// operations (all the *64 operations in runtime/internal/atomic).
+var AtomicFields = []uintptr{
+ unsafe.Offsetof(m{}.procid),
+ unsafe.Offsetof(p{}.timer0When),
+ unsafe.Offsetof(p{}.timerModifiedEarliest),
+ unsafe.Offsetof(p{}.gcFractionalMarkTime),
+ unsafe.Offsetof(schedt{}.goidgen),
+ unsafe.Offsetof(schedt{}.lastpoll),
+ unsafe.Offsetof(schedt{}.pollUntil),
+ unsafe.Offsetof(schedt{}.timeToRun),
+ unsafe.Offsetof(gcControllerState{}.bgScanCredit),
+ unsafe.Offsetof(gcControllerState{}.maxStackScan),
+ unsafe.Offsetof(gcControllerState{}.heapLive),
+ unsafe.Offsetof(gcControllerState{}.heapScan),
+ unsafe.Offsetof(gcControllerState{}.dedicatedMarkTime),
+ unsafe.Offsetof(gcControllerState{}.dedicatedMarkWorkersNeeded),
+ unsafe.Offsetof(gcControllerState{}.fractionalMarkTime),
+ unsafe.Offsetof(gcControllerState{}.idleMarkTime),
+ unsafe.Offsetof(gcControllerState{}.globalsScan),
+ unsafe.Offsetof(gcControllerState{}.lastStackScan),
+ unsafe.Offsetof(timeHistogram{}.underflow),
+ unsafe.Offsetof(profBuf{}.overflow),
+ unsafe.Offsetof(profBuf{}.overflowTime),
+ unsafe.Offsetof(heapStatsDelta{}.tinyAllocCount),
+ unsafe.Offsetof(heapStatsDelta{}.smallAllocCount),
+ unsafe.Offsetof(heapStatsDelta{}.smallFreeCount),
+ unsafe.Offsetof(heapStatsDelta{}.largeAlloc),
+ unsafe.Offsetof(heapStatsDelta{}.largeAllocCount),
+ unsafe.Offsetof(heapStatsDelta{}.largeFree),
+ unsafe.Offsetof(heapStatsDelta{}.largeFreeCount),
+ unsafe.Offsetof(heapStatsDelta{}.committed),
+ unsafe.Offsetof(heapStatsDelta{}.released),
+ unsafe.Offsetof(heapStatsDelta{}.inHeap),
+ unsafe.Offsetof(heapStatsDelta{}.inStacks),
+ unsafe.Offsetof(heapStatsDelta{}.inPtrScalarBits),
+ unsafe.Offsetof(heapStatsDelta{}.inWorkBufs),
+ unsafe.Offsetof(lfnode{}.next),
+ unsafe.Offsetof(mstats{}.last_gc_nanotime),
+ unsafe.Offsetof(mstats{}.last_gc_unix),
+ unsafe.Offsetof(mstats{}.gcPauseDist),
+ unsafe.Offsetof(ticksType{}.val),
+ unsafe.Offsetof(workType{}.bytesMarked),
+ unsafe.Offsetof(timeHistogram{}.counts),
+}
+
+// AtomicVariables is the set of global variables on which we perform
+// 64-bit atomic operations.
+var AtomicVariables = []unsafe.Pointer{
+ unsafe.Pointer(&ncgocall),
+ unsafe.Pointer(&test_z64),
+ unsafe.Pointer(&blockprofilerate),
+ unsafe.Pointer(&mutexprofilerate),
+ unsafe.Pointer(&gcController),
+ unsafe.Pointer(&memstats),
+ unsafe.Pointer(&sched),
+ unsafe.Pointer(&ticks),
+ unsafe.Pointer(&work),
+}