Skip to content

Commit

Permalink
Refactor commands to better support context signaling
Browse files Browse the repository at this point in the history
  • Loading branch information
skmcgrail committed Jul 22, 2024
1 parent 6f7190c commit 30db0f9
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions util/all_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,14 @@ var sdeCPUs = []string{

func targetArchMatchesRuntime(target string) bool {
if (target == "") ||
(target == "x86" && runtime.GOARCH == "amd64") ||
(target == "arm" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")) {
(target == "x86" && runtime.GOARCH == "amd64") ||
(target == "arm" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")) {
return true
}
return false
}

func valgrindOf(dbAttach bool, supps []string, path string, args ...string) *exec.Cmd {
func valgrindOf(ctx context.Context, dbAttach bool, supps []string, path string, args ...string) (context.Context, *exec.Cmd) {
valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full", "--trace-children=yes", "--quiet"}
for _, supp := range supps {
valgrindArgs = append(valgrindArgs, "--suppressions="+*valgrindSuppDir+"/"+supp)
Expand All @@ -118,26 +118,26 @@ func valgrindOf(dbAttach bool, supps []string, path string, args ...string) *exe
valgrindArgs = append(valgrindArgs, path)
valgrindArgs = append(valgrindArgs, args...)

return exec.Command("valgrind", valgrindArgs...)
return ctx, exec.CommandContext(ctx, "valgrind", valgrindArgs...)
}

func callgrindOf(path string, args ...string) *exec.Cmd {
func callgrindOf(ctx context.Context, path string, args ...string) (context.Context, *exec.Cmd) {
valgrindArgs := []string{"-q", "--tool=callgrind", "--dump-instr=yes", "--collect-jumps=yes", "--callgrind-out-file=" + *buildDir + "/callgrind/callgrind.out.%p"}
valgrindArgs = append(valgrindArgs, path)
valgrindArgs = append(valgrindArgs, args...)

return exec.Command("valgrind", valgrindArgs...)
return ctx, exec.CommandContext(ctx, "valgrind", valgrindArgs...)
}

func gdbOf(path string, args ...string) *exec.Cmd {
func gdbOf(ctx context.Context, path string, args ...string) (context.Context, *exec.Cmd) {
xtermArgs := []string{"-e", "gdb", "--args"}
xtermArgs = append(xtermArgs, path)
xtermArgs = append(xtermArgs, args...)

return exec.Command("xterm", xtermArgs...)
return ctx, exec.CommandContext(ctx, "xterm", xtermArgs...)
}

func sdeOf(cpu, path string, args ...string) (*exec.Cmd, context.CancelFunc) {
func sdeOf(ctx context.Context, cpu, path string, args ...string) (context.Context, context.CancelFunc, *exec.Cmd) {
sdeArgs := []string{"-" + cpu}
// The kernel's vdso code for gettimeofday sometimes uses the RDTSCP
// instruction. Although SDE has a -chip_check_vsyscall flag that
Expand All @@ -152,9 +152,9 @@ func sdeOf(cpu, path string, args ...string) (*exec.Cmd, context.CancelFunc) {

// TODO(CryptoAlg-2154):SDE+ASAN tests will hang without exiting if tests pass for an unknown reason.
// Current workaround is to manually cancel the run after 20 minutes and check the output.
ctx, cancel := context.WithTimeout(context.Background(), 1200*time.Second)
ctx, cancel := context.WithTimeout(ctx, 1200*time.Second)

return exec.CommandContext(ctx, *sdePath, sdeArgs...), cancel
return ctx, cancel, exec.CommandContext(ctx, *sdePath, sdeArgs...)
}

var (
Expand All @@ -173,21 +173,18 @@ func runTestOnce(test test, mallocNumToFail int64) (passed bool, err error) {
}
var cmd *exec.Cmd
var cancel context.CancelFunc
cancelled := false

ctx := context.Background()

if *useValgrind {
cmd = valgrindOf(false, test.ValgrindSupp, prog, args...)
ctx, cmd = valgrindOf(ctx, false, test.ValgrindSupp, prog, args...)
} else if *useCallgrind {
cmd = callgrindOf(prog, args...)
ctx, cmd = callgrindOf(ctx, prog, args...)
} else if *useGDB {
cmd = gdbOf(prog, args...)
ctx, cmd = gdbOf(ctx, prog, args...)
} else if *useSDE {
cmd, cancel = sdeOf(test.cpu, prog, args...)
ctx, cancel, cmd = sdeOf(ctx, test.cpu, prog, args...)
defer cancel()

cmd.Cancel = func() error {
cancelled = true
return cmd.Process.Kill()
}
} else {
cmd = exec.Command(prog, args...)
}
Expand Down Expand Up @@ -227,15 +224,19 @@ func runTestOnce(test test, mallocNumToFail int64) (passed bool, err error) {
fmt.Print(string(outBuf.Bytes()))
return false, errTestSkipped
}
if cancelled {
return testPass(outBuf), errTestHanging
select {
case <-ctx.Done():
if ctx.Err() == context.Canceled {
return testPass(outBuf), errTestHanging
}
default:
// Nothing
}
}
fmt.Print(string(outBuf.Bytes()))
return false, err
}


return testPass(outBuf), nil
}

Expand Down

0 comments on commit 30db0f9

Please sign in to comment.