runtime: test VDSO symbol hash values

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>
This commit is contained in:
Michael Pratt 2025-07-23 14:46:22 -04:00 committed by Gopher Robot
parent cd55f86b8d
commit a2c45f0eb1
2 changed files with 81 additions and 0 deletions

View file

@ -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
}

View file

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