]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: test VDSO symbol hash values
authorMichael Pratt <mpratt@google.com>
Wed, 23 Jul 2025 18:46:22 +0000 (14:46 -0400)
committerGopher Robot <gobot@golang.org>
Tue, 5 Aug 2025 16:52:44 +0000 (09:52 -0700)
In addition to verifying existing values, this makes it easier to add a
new one by adding an invalid entry and running the test.

Change-Id: I6a6a636c9c413add29884e4f6759196f4db34de7
Reviewed-on: https://go-review.googlesource.com/c/go/+/693276
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/runtime/export_vdso_linux_test.go [new file with mode: 0644]
src/runtime/vdso_linux_test.go [new file with mode: 0644]

diff --git a/src/runtime/export_vdso_linux_test.go b/src/runtime/export_vdso_linux_test.go
new file mode 100644 (file)
index 0000000..cd339c6
--- /dev/null
@@ -0,0 +1,29 @@
+// Copyright 2025 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.
+
+//go:build linux && (386 || amd64 || arm || arm64 || loong64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x)
+
+package runtime
+
+type VDSOSymbolKey vdsoSymbolKey
+
+func (v VDSOSymbolKey) Name() string {
+       return v.name
+}
+
+func (v VDSOSymbolKey) SymHash() uint32 {
+       return v.symHash
+}
+
+func (v VDSOSymbolKey) GNUHash() uint32 {
+       return v.gnuHash
+}
+
+func VDSOSymbolKeys() []VDSOSymbolKey {
+       keys := make([]VDSOSymbolKey, 0, len(vdsoSymbolKeys))
+       for _, k := range vdsoSymbolKeys {
+               keys = append(keys, VDSOSymbolKey(k))
+       }
+       return keys
+}
diff --git a/src/runtime/vdso_linux_test.go b/src/runtime/vdso_linux_test.go
new file mode 100644 (file)
index 0000000..313dd6e
--- /dev/null
@@ -0,0 +1,52 @@
+// Copyright 2025 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.
+
+//go:build linux && (386 || amd64 || arm || arm64 || loong64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x)
+
+package runtime_test
+
+import (
+       "runtime"
+       "testing"
+)
+
+// DT_GNU_HASH hash function.
+func gnuHash(s string) uint32 {
+       h := uint32(5381)
+       for _, r := range s {
+               h = (h << 5) + h + uint32(r)
+       }
+       return h
+}
+
+// DT_HASH hash function.
+func symHash(s string) uint32 {
+       var h, g uint32
+       for _, r := range s {
+               h = (h << 4) + uint32(r)
+               g = h & 0xf0000000
+               if g != 0 {
+                       h ^= g >> 24
+               }
+               h &^= g
+       }
+       return h
+}
+
+func TestVDSOHash(t *testing.T) {
+       for _, sym := range runtime.VDSOSymbolKeys() {
+               name := sym.Name()
+               t.Run(name, func(t *testing.T) {
+                       want := symHash(name)
+                       if sym.SymHash() != want {
+                               t.Errorf("SymHash got %#x want %#x", sym.SymHash(), want)
+                       }
+
+                       want = gnuHash(name)
+                       if sym.GNUHash() != want {
+                               t.Errorf("GNUHash got %#x want %#x", sym.GNUHash(), want)
+                       }
+               })
+       }
+}