From: Michael Pratt Date: Wed, 23 Jul 2025 18:46:22 +0000 (-0400) Subject: runtime: test VDSO symbol hash values X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a2c45f0eb1f281ed39c5111dd0fe4b2728f11cf3;p=gostls13.git 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 Auto-Submit: Michael Pratt Reviewed-by: Cherry Mui --- diff --git a/src/runtime/export_vdso_linux_test.go b/src/runtime/export_vdso_linux_test.go new file mode 100644 index 0000000000..cd339c6038 --- /dev/null +++ b/src/runtime/export_vdso_linux_test.go @@ -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 index 0000000000..313dd6e718 --- /dev/null +++ b/src/runtime/vdso_linux_test.go @@ -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) + } + }) + } +}