Source file src/runtime/decoratemappings_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  package runtime_test
     6  
     7  import (
     8  	"os"
     9  	"regexp"
    10  	"runtime"
    11  	"testing"
    12  )
    13  
    14  func validateMapLabels(t *testing.T, labels []string) {
    15  	// These are the specific region labels that need get added during the
    16  	// runtime phase. Hence they are the ones that need to be confirmed as
    17  	// present at the time the test reads its own region labels, which
    18  	// is sufficient to validate that the default `decoratemappings` value
    19  	// (enabled) was set early enough in the init process.
    20  	regions := map[string]bool{
    21  		"allspans array":    false,
    22  		"gc bits":           false,
    23  		"heap":              false,
    24  		"heap index":        false,
    25  		"heap reservation":  false,
    26  		"immortal metadata": false,
    27  		"page alloc":        false,
    28  		"page alloc index":  false,
    29  		"page summary":      false,
    30  		"scavenge index":    false,
    31  	}
    32  	for _, label := range labels {
    33  		if _, ok := regions[label]; !ok {
    34  			t.Logf("unexpected region label found: \"%s\"", label)
    35  		}
    36  		regions[label] = true
    37  	}
    38  	for label, found := range regions {
    39  		if !found {
    40  			t.Logf("region label missing: \"%s\"", label)
    41  		}
    42  	}
    43  }
    44  
    45  func TestDecorateMappings(t *testing.T) {
    46  	if runtime.GOOS != "linux" {
    47  		t.Skip("decoratemappings is only supported on Linux")
    48  		// /proc/self/maps is also Linux-specific
    49  	}
    50  
    51  	var labels []string
    52  	if rawMaps, err := os.ReadFile("/proc/self/maps"); err != nil {
    53  		t.Fatalf("failed to read /proc/self/maps: %v", err)
    54  	} else {
    55  		t.Logf("maps:%s\n", string(rawMaps))
    56  		matches := regexp.MustCompile("[^[]+ \\[anon: Go: (.+)\\]\n").FindAllSubmatch(rawMaps, -1)
    57  		for _, match_pair := range matches {
    58  			// match_pair consists of the matching substring and the parenthesized group
    59  			labels = append(labels, string(match_pair[1]))
    60  		}
    61  	}
    62  	t.Logf("DebugDecorateMappings: %v", *runtime.DebugDecorateMappings)
    63  	if *runtime.DebugDecorateMappings != 0 && runtime.SetVMANameSupported() {
    64  		validateMapLabels(t, labels)
    65  	} else {
    66  		if len(labels) > 0 {
    67  			t.Errorf("unexpected mapping labels present: %v", labels)
    68  		} else {
    69  			t.Skip("mapping labels absent as expected")
    70  		}
    71  	}
    72  }
    73  

View as plain text