| 1 |
// Copyright 2023 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 load |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"go/build" |
| 11 |
"internal/godebugs" |
| 12 |
"maps" |
| •13 |
"os" |
| 14 |
"sort" |
| 15 |
"strconv" |
| 16 |
"strings" |
| 17 |
|
| 18 |
"cmd/go/internal/fips140" |
| 19 |
"cmd/go/internal/gover" |
| 20 |
"cmd/go/internal/modload" |
| 21 |
) |
| 22 |
|
| 23 |
var ErrNotGoDebug = errors.New("not //go:debug line") |
| 24 |
|
| 25 |
func ParseGoDebug(text string) (key, value string, err error) { |
| 26 |
if !strings.HasPrefix(text, "//go:debug") { |
| 27 |
return "", "", ErrNotGoDebug |
| 28 |
} |
| 29 |
i := strings.IndexAny(text, " \t") |
| 30 |
if i < 0 { |
| 31 |
if strings.TrimSpace(text) == "//go:debug" { |
| 32 |
return "", "", fmt.Errorf("missing key=value") |
| 33 |
} |
| 34 |
return "", "", ErrNotGoDebug |
| 35 |
} |
| 36 |
k, v, ok := strings.Cut(strings.TrimSpace(text[i:]), "=") |
| 37 |
if !ok { |
| 38 |
return "", "", fmt.Errorf("missing key=value") |
| 39 |
} |
| 40 |
if err := modload.CheckGodebug("//go:debug setting", k, v); err != nil { |
| 41 |
return "", "", err |
| 42 |
} |
| 43 |
return k, v, nil |
| 44 |
} |
| 45 |
|
| 46 |
// defaultGODEBUG returns the default GODEBUG setting for the main package p. |
| 47 |
// When building a test binary, directives, testDirectives, and xtestDirectives |
| 48 |
// list additional directives from the package under test. |
| 49 |
func defaultGODEBUG(loaderstate *modload.State, p *Package, directives, testDirectives, xtestDirectives []build.Directive) string { |
| 50 |
if p.Name != "main" { |
| 51 |
return "" |
| 52 |
} |
| 53 |
goVersion := loaderstate.MainModules.GoVersion(loaderstate) |
| 54 |
if loaderstate.RootMode == modload.NoRoot && p.Module != nil { |
| 55 |
// This is go install pkg@version or go run pkg@version. |
| 56 |
// Use the Go version from the package. |
| 57 |
// If there isn't one, then assume Go 1.20, |
| 58 |
// the last version before GODEBUGs were introduced. |
| 59 |
goVersion = p.Module.GoVersion |
| 60 |
if goVersion == "" { |
| 61 |
goVersion = "1.20" |
| 62 |
} |
| 63 |
} |
| •64 |
debGoMinVer := os.Getenv("DEB_GOMINCOMPAT") |
| 65 |
if debGoMinVer != "" && gover.Compare(goVersion, debGoMinVer) < 0 { |
| 66 |
goVersion = debGoMinVer |
| 67 |
} |
| 68 |
|
| 69 |
var m map[string]string |
| 70 |
|
| 71 |
// If GOFIPS140 is set to anything but "off", |
| 72 |
// default to GODEBUG=fips140=on. |
| 73 |
if fips140.Enabled() { |
| 74 |
if m == nil { |
| 75 |
m = make(map[string]string) |
| 76 |
} |
| 77 |
m["fips140"] = "on" |
| 78 |
} |
| 79 |
|
| 80 |
// Add directives from main module go.mod. |
| 81 |
for _, g := range loaderstate.MainModules.Godebugs(loaderstate) { |
| 82 |
if m == nil { |
| 83 |
m = make(map[string]string) |
| 84 |
} |
| 85 |
m[g.Key] = g.Value |
| 86 |
} |
| 87 |
|
| 88 |
// Add directives from packages. |
| 89 |
for _, list := range [][]build.Directive{p.Internal.Build.Directives, directives, testDirectives, xtestDirectives} { |
| 90 |
for _, d := range list { |
| 91 |
k, v, err := ParseGoDebug(d.Text) |
| 92 |
if err != nil { |
| 93 |
continue |
| 94 |
} |
| 95 |
if m == nil { |
| 96 |
m = make(map[string]string) |
| 97 |
} |
| 98 |
m[k] = v |
| 99 |
} |
| 100 |
} |
| 101 |
if v, ok := m["default"]; ok { |
| 102 |
delete(m, "default") |
| 103 |
v = strings.TrimPrefix(v, "go") |
| 104 |
if gover.IsValid(v) { |
| 105 |
goVersion = v |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
defaults := godebugForGoVersion(goVersion) |
| 110 |
if defaults != nil { |
| 111 |
// Apply m on top of defaults. |
| 112 |
maps.Copy(defaults, m) |
| 113 |
m = defaults |
| 114 |
} |
| 115 |
|
| 116 |
keys := make([]string, 0, len(m)) |
| 117 |
for k := range m { |
| 118 |
keys = append(keys, k) |
| 119 |
} |
| 120 |
sort.Strings(keys) |
| 121 |
var b strings.Builder |
| 122 |
for _, k := range keys { |
| 123 |
if b.Len() > 0 { |
| 124 |
b.WriteString(",") |
| 125 |
} |
| 126 |
b.WriteString(k) |
| 127 |
b.WriteString("=") |
| 128 |
b.WriteString(m[k]) |
| 129 |
} |
| 130 |
return b.String() |
| 131 |
} |
| 132 |
|
| 133 |
func godebugForGoVersion(v string) map[string]string { |
| 134 |
if strings.Count(v, ".") >= 2 { |
| 135 |
i := strings.Index(v, ".") |
| 136 |
j := i + 1 + strings.Index(v[i+1:], ".") |
| 137 |
v = v[:j] |
| 138 |
} |
| 139 |
|
| 140 |
if !strings.HasPrefix(v, "1.") { |
| 141 |
return nil |
| 142 |
} |
| 143 |
n, err := strconv.Atoi(v[len("1."):]) |
| 144 |
if err != nil { |
| 145 |
return nil |
| 146 |
} |
| 147 |
|
| 148 |
def := make(map[string]string) |
| 149 |
for _, info := range godebugs.All { |
| 150 |
if n < info.Changed { |
| 151 |
def[info.Name] = info.Old |
| 152 |
} |
| 153 |
} |
| 154 |
return def |
| 155 |
} |