| 1 |
// Copyright 2020 Marc-Antoine Ruel. All rights reserved. |
| 2 |
// Use of this source code is governed under the Apache License, Version 2.0 |
| 3 |
// that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package internaltest |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"os" |
| 11 |
"os/exec" |
| 12 |
"runtime" |
| 13 |
"strconv" |
| 14 |
"strings" |
| 15 |
"sync" |
| 16 |
) |
| 17 |
|
| 18 |
// PanicwebOutput returns the output of panicweb with inlining disabled. |
| 19 |
// |
| 20 |
// The function panics if any internal error occurs. |
| 21 |
func PanicwebOutput() []byte { |
| 22 |
panicwebOnce.Do(func() { |
| 23 |
p := build("panicweb", false) |
| 24 |
if p == "" { |
| 25 |
panic("building panicweb failed") |
| 26 |
} |
| 27 |
defer func() { |
| 28 |
if err := os.Remove(p); err != nil { |
| 29 |
panic(err) |
| 30 |
} |
| 31 |
}() |
| 32 |
panicwebOutput = execRun(p) |
| 33 |
}) |
| 34 |
out := make([]byte, len(panicwebOutput)) |
| 35 |
copy(out, panicwebOutput) |
| 36 |
return out |
| 37 |
} |
| 38 |
|
| 39 |
// PanicOutputs returns a map of the output of every subcommands. |
| 40 |
// |
| 41 |
// panic is built with inlining disabled. |
| 42 |
// |
| 43 |
// The subcommand "race" is built with the race detector. Others are built |
| 44 |
// without. In particular "asleep" doesn't work with the race detector. |
| 45 |
// |
| 46 |
// The function panics if any internal error occurs. |
| 47 |
func PanicOutputs() map[string][]byte { |
| 48 |
panicOutputsOnce.Do(func() { |
| 49 |
// Extracts the subcommands, then run each of them individually. |
| 50 |
pplain := build("panic", false) |
| 51 |
if pplain == "" { |
| 52 |
// The odd of this failing is close to nil. |
| 53 |
panic("building panic failed") |
| 54 |
} |
| 55 |
defer func() { |
| 56 |
if err := os.Remove(pplain); err != nil { |
| 57 |
panic(err) |
| 58 |
} |
| 59 |
}() |
| 60 |
|
| 61 |
prace := build("panic", true) |
| 62 |
if prace == "" { |
| 63 |
// Race detector is not supported on this platform. |
| 64 |
} else { |
| 65 |
defer func() { |
| 66 |
if err := os.Remove(prace); err != nil { |
| 67 |
panic(err) |
| 68 |
} |
| 69 |
}() |
| 70 |
} |
| 71 |
|
| 72 |
// Collect the subcommands. |
| 73 |
cmds := strings.Split(strings.TrimSpace(string(execRun(pplain, "dump_commands"))), "\n") |
| 74 |
if len(cmds) == 0 { |
| 75 |
panic("no command retrieved") |
| 76 |
} |
| 77 |
|
| 78 |
// Collect the output of each subcommand. |
| 79 |
panicOutputs = map[string][]byte{} |
| 80 |
for _, cmd := range cmds { |
| 81 |
cmd = strings.TrimSpace(cmd) |
| 82 |
p := pplain |
| 83 |
if cmd == "race" { |
| 84 |
if prace == "" { |
| 85 |
// Race detector is not supported. |
| 86 |
continue |
| 87 |
} |
| 88 |
p = prace |
| 89 |
} |
| 90 |
if panicOutputs[cmd] = execRun(p, cmd); len(panicOutputs[cmd]) == 0 { |
| 91 |
panic(fmt.Sprintf("no output for %s", cmd)) |
| 92 |
} |
| 93 |
} |
| 94 |
}) |
| 95 |
out := make(map[string][]byte, len(panicOutputs)) |
| 96 |
for k, v := range panicOutputs { |
| 97 |
w := make([]byte, len(v)) |
| 98 |
copy(w, v) |
| 99 |
out[k] = w |
| 100 |
} |
| 101 |
return out |
| 102 |
} |
| 103 |
|
| 104 |
// StaticPanicwebOutput returns a constant version of panicweb output for use |
| 105 |
// in benchmarks. |
| 106 |
func StaticPanicwebOutput() []byte { |
| 107 |
return []byte(staticPanicweb) |
| 108 |
} |
| 109 |
|
| 110 |
// StaticPanicRaceOutput returns a constant version of 'panic race' output. |
| 111 |
func StaticPanicRaceOutput() []byte { |
| 112 |
return []byte(staticPanicRace) |
| 113 |
} |
| 114 |
|
| 115 |
// IsUsingModules returns if go modules are enabled. |
| 116 |
// |
| 117 |
// It reads the current value of GO111MODULES. |
| 118 |
func IsUsingModules() bool { |
| 119 |
s := os.Getenv("GO111MODULE") |
| 120 |
return s != "off" |
| 121 |
} |
| 122 |
|
| 123 |
// |
| 124 |
|
| 125 |
var ( |
| 126 |
panicwebOnce sync.Once |
| 127 |
panicwebOutput []byte |
| 128 |
panicOutputsOnce sync.Once |
| 129 |
panicOutputs map[string][]byte |
| 130 |
) |
| 131 |
|
| 132 |
// GetGoMinorVersion returns the Go1 minor version. |
| 133 |
// |
| 134 |
// Returns 0 for a developer build, panics if can't parse the version. |
| 135 |
// |
| 136 |
// Ignores the revision (go1.<minor>.<revision>). |
| 137 |
func GetGoMinorVersion() int { |
| 138 |
ver := runtime.Version() |
| 139 |
if strings.HasPrefix(ver, "devel ") { |
| 140 |
return 0 |
| 141 |
} |
| 142 |
if !strings.HasPrefix(ver, "go1.") { |
| 143 |
// This will break on go2. Please submit a PR to fix this once Go2 is |
| 144 |
// released. |
| 145 |
panic(fmt.Sprintf("unexpected go version %q", ver)) |
| 146 |
} |
| 147 |
v := ver[4:] |
| 148 |
if i := strings.IndexByte(v, '.'); i != -1 { |
| 149 |
v = v[:i] |
| 150 |
} else if i := strings.Index(v, "beta"); i != -1 { |
| 151 |
v = v[:i] |
| 152 |
} else if i := strings.Index(v, "rc"); i != -1 { |
| 153 |
v = v[:i] |
| 154 |
} |
| 155 |
|
| 156 |
m, err := strconv.Atoi(v) |
| 157 |
if err != nil { |
| 158 |
panic(fmt.Sprintf("failed to parse %q: %v", ver, err)) |
| 159 |
} |
| 160 |
return m |
| 161 |
} |
| 162 |
|
| 163 |
// build builds to a temporary file and returns the path to it. |
| 164 |
func build(tool string, race bool) string { |
| 165 |
s := "panicparse*" |
| 166 |
if runtime.GOOS == "windows" { |
| 167 |
s = "panicparse*.exe" |
| 168 |
} |
| 169 |
f, err := os.CreateTemp("", s) |
| 170 |
if err != nil { |
| 171 |
_, _ = os.Stderr.WriteString(err.Error()) |
| 172 |
return "" |
| 173 |
} |
| 174 |
p := f.Name() |
| 175 |
_ = f.Close() |
| 176 |
path := "github.com/maruel/panicparse/cmd/" |
| 177 |
if IsUsingModules() { |
| 178 |
path = "github.com/maruel/panicparse/v2/cmd/" |
| 179 |
} |
| 180 |
if err := Compile(path+tool, p, "", true, race); err != nil { |
| 181 |
_ = os.Remove(p) |
| 182 |
_, _ = os.Stderr.WriteString(err.Error()) |
| 183 |
return "" |
| 184 |
} |
| 185 |
return p |
| 186 |
} |
| 187 |
|
| 188 |
var errNoRace = errors.New("platform does not support -race") |
| 189 |
|
| 190 |
// Compile compiles sources into an executable. |
| 191 |
func Compile(in, exe, cwd string, disableInlining, race bool) error { |
| 192 |
// Disable optimization (-N) and inlining (-l) otherwise the inlining varies |
| 193 |
// between local execution and remote execution. This can be observed as |
| 194 |
// Elided being true without any argument. |
| 195 |
args := []string{"build", "-o", exe} |
| 196 |
if disableInlining { |
| 197 |
args = append(args, "-gcflags", "-N -l") |
| 198 |
} |
| 199 |
if race { |
| 200 |
args = append(args, "-race") |
| 201 |
} |
| 202 |
/* #nosec G204 */ |
| 203 |
c := exec.Command("go", append(args, in)...) |
| 204 |
c.Dir = cwd |
| 205 |
if out, err := c.CombinedOutput(); err != nil { |
| 206 |
if race { |
| 207 |
s := string(out) |
| 208 |
const e1 = "go test: -race is only supported on " |
| 209 |
const e2 = "go build: -race is only supported on " |
| 210 |
if strings.HasPrefix(s, e1) || strings.HasPrefix(s, e2) { |
| 211 |
return errNoRace |
| 212 |
} |
| 213 |
} |
| 214 |
return fmt.Errorf("compile failure: %w\n%s", err, out) |
| 215 |
} |
| 216 |
return nil |
| 217 |
} |
| 218 |
|
| 219 |
// execRun runs a command and returns the combined output. |
| 220 |
// |
| 221 |
// It ignores the exit code, since it's meant to run panic, which crashes by |
| 222 |
// design. |
| 223 |
func execRun(cmd ...string) []byte { |
| 224 |
/* #nosec G204 */ |
| 225 |
c := exec.Command(cmd[0], cmd[1:]...) |
| 226 |
c.Env = append(os.Environ(), "GOTRACEBACK=all") |
| 227 |
out, _ := c.CombinedOutput() |
| 228 |
return out |
| 229 |
} |