Skip to content

Commit a2c45f0

Browse files
prattmicgopherbot
authored andcommitted
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 <[email protected]> Auto-Submit: Michael Pratt <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent cd55f86 commit a2c45f0

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

src/runtime/export_vdso_linux_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build linux && (386 || amd64 || arm || arm64 || loong64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x)
6+
7+
package runtime
8+
9+
type VDSOSymbolKey vdsoSymbolKey
10+
11+
func (v VDSOSymbolKey) Name() string {
12+
return v.name
13+
}
14+
15+
func (v VDSOSymbolKey) SymHash() uint32 {
16+
return v.symHash
17+
}
18+
19+
func (v VDSOSymbolKey) GNUHash() uint32 {
20+
return v.gnuHash
21+
}
22+
23+
func VDSOSymbolKeys() []VDSOSymbolKey {
24+
keys := make([]VDSOSymbolKey, 0, len(vdsoSymbolKeys))
25+
for _, k := range vdsoSymbolKeys {
26+
keys = append(keys, VDSOSymbolKey(k))
27+
}
28+
return keys
29+
}

src/runtime/vdso_linux_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2025 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build linux && (386 || amd64 || arm || arm64 || loong64 || mips64 || mips64le || ppc64 || ppc64le || riscv64 || s390x)
6+
7+
package runtime_test
8+
9+
import (
10+
"runtime"
11+
"testing"
12+
)
13+
14+
// DT_GNU_HASH hash function.
15+
func gnuHash(s string) uint32 {
16+
h := uint32(5381)
17+
for _, r := range s {
18+
h = (h << 5) + h + uint32(r)
19+
}
20+
return h
21+
}
22+
23+
// DT_HASH hash function.
24+
func symHash(s string) uint32 {
25+
var h, g uint32
26+
for _, r := range s {
27+
h = (h << 4) + uint32(r)
28+
g = h & 0xf0000000
29+
if g != 0 {
30+
h ^= g >> 24
31+
}
32+
h &^= g
33+
}
34+
return h
35+
}
36+
37+
func TestVDSOHash(t *testing.T) {
38+
for _, sym := range runtime.VDSOSymbolKeys() {
39+
name := sym.Name()
40+
t.Run(name, func(t *testing.T) {
41+
want := symHash(name)
42+
if sym.SymHash() != want {
43+
t.Errorf("SymHash got %#x want %#x", sym.SymHash(), want)
44+
}
45+
46+
want = gnuHash(name)
47+
if sym.GNUHash() != want {
48+
t.Errorf("GNUHash got %#x want %#x", sym.GNUHash(), want)
49+
}
50+
})
51+
}
52+
}

0 commit comments

Comments
 (0)