Source file src/runtime/vdso_linux_test.go

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

View as plain text