internal/gocore/test: split run into runCrasher and doRunCrasher

Refactors the run function by splitting it in two, making it easier to
reuse for more involved tests that need to set different parameters
(flags, other environment variables).

This commit also makes the otherwise unrelated change of setting
GOMAXPROCS=2 in the child binary. This can reduce the size of the crash
stack (fewer GC workers).

Change-Id: I4edd5c51fdfed6b8bb57f2e682cb98dc084e50a5
Reviewed-on: https://go-review.googlesource.com/c/debug/+/632296
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Nicolas Hillegeer <aktau@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
diff --git a/internal/gocore/gocore_test.go b/internal/gocore/gocore_test.go
index b40e410..c716e8e 100644
--- a/internal/gocore/gocore_test.go
+++ b/internal/gocore/gocore_test.go
@@ -187,20 +187,23 @@
 	return nil
 }
 
-// run spawns the supplied exe with wd as working directory.
-//
-//   - The parent environment is amended with GOTRACEBACK=crash to provoke a
-//     core dump on (e.g.) segfaults.
-//   - Thread/process state (like resource limits) are propagated.
-//
-// If the binary fails to crash, an error is returned.
-func run(exe, wd string) (pid int, output []byte, err error) {
+// runCrasher spawns exe via [doRunCrasher] with wd as working directory.
+// GOTRACEBACK=crash is set.
+func runCrasher(exe, wd string) (pid int, output []byte, err error) {
 	cmd := exec.Command(exe)
-	cmd.Env = append(os.Environ(), "GOTRACEBACK=crash")
+	cmd.Env = append(os.Environ(), "GOMAXPROCS=2", "GOTRACEBACK=crash")
 	cmd.Dir = wd
+	return doRunCrasher(cmd)
+}
+
+// doRunCrasher spawns the supplied cmd, propagating parent state (see
+// [exec.Cmd.Run]), and returns an error if the process failed to start or did
+// *NOT* crash.
+func doRunCrasher(cmd *exec.Cmd) (pid int, output []byte, err error) {
 	var b bytes.Buffer
 	cmd.Stdout = &b
 	cmd.Stderr = &b
+
 	runtime.LockOSThread() // Propagate parent state, see [exec.Cmd.Run].
 	err = cmd.Run()
 	runtime.UnlockOSThread()
@@ -237,7 +240,7 @@
 		return "", nil, fmt.Errorf("error building crasher: %w\n%s", err, string(b))
 	}
 
-	_, b, err = run("./test.exe", dir)
+	_, b, err = runCrasher("./test.exe", dir)
 	if err != nil {
 		return "", b, err
 	}