blob: 235db05d3b2095c7aae36a09d45fc8dc1281f3e6 [file] [log] [blame]
David Benjamin491b9212015-02-11 14:18:45 -05001/* Copyright (c) 2015, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15package main
16
17import (
David Benjamin3d14a152017-06-07 14:02:03 -040018 "bufio"
David Benjamin491b9212015-02-11 14:18:45 -050019 "bytes"
David Benjamin5f237bc2015-02-11 17:14:15 -050020 "encoding/json"
David Benjamin491b9212015-02-11 14:18:45 -050021 "flag"
22 "fmt"
David Benjamin3d14a152017-06-07 14:02:03 -040023 "math/rand"
David Benjamin491b9212015-02-11 14:18:45 -050024 "os"
25 "os/exec"
26 "path"
Adam Langley31fa5a42017-04-11 17:07:27 -070027 "runtime"
David Benjamin0b635c52015-05-15 19:08:49 -040028 "strconv"
David Benjamin491b9212015-02-11 14:18:45 -050029 "strings"
Steven Valdez32223942016-03-02 11:53:07 -050030 "sync"
David Benjamin0b635c52015-05-15 19:08:49 -040031 "syscall"
David Benjamin5f237bc2015-02-11 17:14:15 -050032 "time"
David Benjamin491b9212015-02-11 14:18:45 -050033)
34
35// TODO(davidben): Link tests with the malloc shim and port -malloc-test to this runner.
36
37var (
David Benjamin0b635c52015-05-15 19:08:49 -040038 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
Steven Valdezab14a4a2016-02-29 16:58:26 -050039 useCallgrind = flag.Bool("callgrind", false, "If true, run code under valgrind to generate callgrind traces.")
David Benjamin0b635c52015-05-15 19:08:49 -040040 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
Adam Langleye212f272017-02-03 08:52:21 -080041 useSDE = flag.Bool("sde", false, "If true, run BoringSSL code under Intel's SDE for each supported chip")
David Benjamin799676c2017-05-10 16:56:02 -040042 sdePath = flag.String("sde-path", "sde", "The path to find the sde binary.")
David Benjamin0b635c52015-05-15 19:08:49 -040043 buildDir = flag.String("build-dir", "build", "The build directory to run the tests from.")
Adam Langley31fa5a42017-04-11 17:07:27 -070044 numWorkers = flag.Int("num-workers", runtime.NumCPU(), "Runs the given number of workers when testing.")
David Benjamin0b635c52015-05-15 19:08:49 -040045 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
46 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
47 mallocTestDebug = flag.Bool("malloc-test-debug", false, "If true, ask each test to abort rather than fail a malloc. This can be used with a specific value for --malloc-test to identity the malloc failing that is causing problems.")
David Benjamin491b9212015-02-11 14:18:45 -050048)
49
Adam Langleye212f272017-02-03 08:52:21 -080050type test struct {
51 args []string
52 // cpu, if not empty, contains an Intel CPU code to simulate. Run
53 // `sde64 -help` to get a list of these codes.
54 cpu string
55}
David Benjamin491b9212015-02-11 14:18:45 -050056
Steven Valdez32223942016-03-02 11:53:07 -050057type result struct {
58 Test test
59 Passed bool
60 Error error
61}
62
David Benjamin5f237bc2015-02-11 17:14:15 -050063// testOutput is a representation of Chromium's JSON test result format. See
64// https://www.chromium.org/developers/the-json-test-results-format
65type testOutput struct {
66 Version int `json:"version"`
67 Interrupted bool `json:"interrupted"`
68 PathDelimiter string `json:"path_delimiter"`
69 SecondsSinceEpoch float64 `json:"seconds_since_epoch"`
70 NumFailuresByType map[string]int `json:"num_failures_by_type"`
71 Tests map[string]testResult `json:"tests"`
72}
73
74type testResult struct {
David Benjamin7ead6052015-04-04 03:57:26 -040075 Actual string `json:"actual"`
76 Expected string `json:"expected"`
77 IsUnexpected bool `json:"is_unexpected"`
David Benjamin5f237bc2015-02-11 17:14:15 -050078}
79
Adam Langleye212f272017-02-03 08:52:21 -080080// sdeCPUs contains a list of CPU code that we run all tests under when *useSDE
81// is true.
82var sdeCPUs = []string{
83 "p4p", // Pentium4 Prescott
84 "mrm", // Merom
85 "pnr", // Penryn
86 "nhm", // Nehalem
87 "wsm", // Westmere
88 "snb", // Sandy Bridge
89 "ivb", // Ivy Bridge
90 "hsw", // Haswell
91 "bdw", // Broadwell
92 "skx", // Skylake Server
93 "skl", // Skylake Client
94 "cnl", // Cannonlake
95 "knl", // Knights Landing
96 "slt", // Saltwell
97 "slm", // Silvermont
98 "glm", // Goldmont
99}
100
David Benjamin5f237bc2015-02-11 17:14:15 -0500101func newTestOutput() *testOutput {
102 return &testOutput{
103 Version: 3,
104 PathDelimiter: ".",
105 SecondsSinceEpoch: float64(time.Now().UnixNano()) / float64(time.Second/time.Nanosecond),
106 NumFailuresByType: make(map[string]int),
107 Tests: make(map[string]testResult),
108 }
109}
110
111func (t *testOutput) addResult(name, result string) {
112 if _, found := t.Tests[name]; found {
113 panic(name)
114 }
David Benjamin7ead6052015-04-04 03:57:26 -0400115 t.Tests[name] = testResult{
116 Actual: result,
117 Expected: "PASS",
118 IsUnexpected: result != "PASS",
119 }
David Benjamin5f237bc2015-02-11 17:14:15 -0500120 t.NumFailuresByType[result]++
121}
122
123func (t *testOutput) writeTo(name string) error {
124 file, err := os.Create(name)
125 if err != nil {
126 return err
127 }
128 defer file.Close()
129 out, err := json.MarshalIndent(t, "", " ")
130 if err != nil {
131 return err
132 }
133 _, err = file.Write(out)
134 return err
135}
136
David Benjamin491b9212015-02-11 14:18:45 -0500137func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
David Benjamind2ba8892016-09-20 19:41:04 -0400138 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full", "--quiet"}
David Benjamin491b9212015-02-11 14:18:45 -0500139 if dbAttach {
140 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
141 }
142 valgrindArgs = append(valgrindArgs, path)
143 valgrindArgs = append(valgrindArgs, args...)
144
145 return exec.Command("valgrind", valgrindArgs...)
146}
147
Steven Valdezab14a4a2016-02-29 16:58:26 -0500148func callgrindOf(path string, args ...string) *exec.Cmd {
149 valgrindArgs := []string{"-q", "--tool=callgrind", "--dump-instr=yes", "--collect-jumps=yes", "--callgrind-out-file=" + *buildDir + "/callgrind/callgrind.out.%p"}
150 valgrindArgs = append(valgrindArgs, path)
151 valgrindArgs = append(valgrindArgs, args...)
152
153 return exec.Command("valgrind", valgrindArgs...)
154}
155
David Benjamin0b635c52015-05-15 19:08:49 -0400156func gdbOf(path string, args ...string) *exec.Cmd {
157 xtermArgs := []string{"-e", "gdb", "--args"}
158 xtermArgs = append(xtermArgs, path)
159 xtermArgs = append(xtermArgs, args...)
160
161 return exec.Command("xterm", xtermArgs...)
162}
163
Adam Langleye212f272017-02-03 08:52:21 -0800164func sdeOf(cpu, path string, args ...string) *exec.Cmd {
165 sdeArgs := []string{"-" + cpu, "--", path}
166 sdeArgs = append(sdeArgs, args...)
David Benjamin799676c2017-05-10 16:56:02 -0400167 return exec.Command(*sdePath, sdeArgs...)
Adam Langleye212f272017-02-03 08:52:21 -0800168}
169
David Benjamin0b635c52015-05-15 19:08:49 -0400170type moreMallocsError struct{}
171
172func (moreMallocsError) Error() string {
173 return "child process did not exhaust all allocation calls"
174}
175
176var errMoreMallocs = moreMallocsError{}
177
178func runTestOnce(test test, mallocNumToFail int64) (passed bool, err error) {
Adam Langleye212f272017-02-03 08:52:21 -0800179 prog := path.Join(*buildDir, test.args[0])
180 args := test.args[1:]
David Benjamin491b9212015-02-11 14:18:45 -0500181 var cmd *exec.Cmd
182 if *useValgrind {
183 cmd = valgrindOf(false, prog, args...)
Steven Valdezab14a4a2016-02-29 16:58:26 -0500184 } else if *useCallgrind {
185 cmd = callgrindOf(prog, args...)
David Benjamin0b635c52015-05-15 19:08:49 -0400186 } else if *useGDB {
187 cmd = gdbOf(prog, args...)
Adam Langleye212f272017-02-03 08:52:21 -0800188 } else if *useSDE {
189 cmd = sdeOf(test.cpu, prog, args...)
David Benjamin491b9212015-02-11 14:18:45 -0500190 } else {
191 cmd = exec.Command(prog, args...)
192 }
David Benjamin634b0e32017-02-04 11:14:57 -0500193 var outBuf bytes.Buffer
194 cmd.Stdout = &outBuf
195 cmd.Stderr = &outBuf
David Benjamin0b635c52015-05-15 19:08:49 -0400196 if mallocNumToFail >= 0 {
197 cmd.Env = os.Environ()
198 cmd.Env = append(cmd.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
199 if *mallocTestDebug {
200 cmd.Env = append(cmd.Env, "MALLOC_ABORT_ON_FAIL=1")
201 }
202 cmd.Env = append(cmd.Env, "_MALLOC_CHECK=1")
203 }
David Benjamin491b9212015-02-11 14:18:45 -0500204
205 if err := cmd.Start(); err != nil {
206 return false, err
207 }
208 if err := cmd.Wait(); err != nil {
David Benjamin0b635c52015-05-15 19:08:49 -0400209 if exitError, ok := err.(*exec.ExitError); ok {
210 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
211 return false, errMoreMallocs
212 }
213 }
David Benjamin634b0e32017-02-04 11:14:57 -0500214 fmt.Print(string(outBuf.Bytes()))
David Benjamin491b9212015-02-11 14:18:45 -0500215 return false, err
216 }
217
218 // Account for Windows line-endings.
David Benjamin634b0e32017-02-04 11:14:57 -0500219 stdout := bytes.Replace(outBuf.Bytes(), []byte("\r\n"), []byte("\n"), -1)
David Benjamin491b9212015-02-11 14:18:45 -0500220
221 if bytes.HasSuffix(stdout, []byte("PASS\n")) &&
222 (len(stdout) == 5 || stdout[len(stdout)-6] == '\n') {
223 return true, nil
224 }
David Benjamin96628432017-01-19 19:05:47 -0500225
226 // Also accept a googletest-style pass line. This is left here in
227 // transition until the tests are all converted and this script made
228 // unnecessary.
229 if bytes.Contains(stdout, []byte("\n[ PASSED ]")) {
230 return true, nil
231 }
232
David Benjamin634b0e32017-02-04 11:14:57 -0500233 fmt.Print(string(outBuf.Bytes()))
David Benjamin491b9212015-02-11 14:18:45 -0500234 return false, nil
235}
236
David Benjamin0b635c52015-05-15 19:08:49 -0400237func runTest(test test) (bool, error) {
238 if *mallocTest < 0 {
239 return runTestOnce(test, -1)
240 }
241
242 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
243 if passed, err := runTestOnce(test, mallocNumToFail); err != errMoreMallocs {
244 if err != nil {
245 err = fmt.Errorf("at malloc %d: %s", mallocNumToFail, err)
246 }
247 return passed, err
248 }
249 }
250}
251
Martin Kreichgauer23aff6b2017-04-11 08:53:08 -0700252// shortTestName returns the short name of a test. Except for evp_test and
253// cipher_test, it assumes that any argument which ends in .txt is a path to a
254// data file and not relevant to the test's uniqueness.
David Benjamin5f237bc2015-02-11 17:14:15 -0500255func shortTestName(test test) string {
256 var args []string
Adam Langleye212f272017-02-03 08:52:21 -0800257 for _, arg := range test.args {
David Benjamin3d14a152017-06-07 14:02:03 -0400258 if test.args[0] == "crypto/evp/evp_test" || test.args[0] == "crypto/cipher_extra/cipher_test" || test.args[0] == "crypto/cipher_extra/aead_test" || !strings.HasSuffix(arg, ".txt") || strings.HasPrefix(arg, "--gtest_filter=") {
David Benjamin5f237bc2015-02-11 17:14:15 -0500259 args = append(args, arg)
260 }
261 }
Adam Langleye212f272017-02-03 08:52:21 -0800262 return strings.Join(args, " ") + test.cpuMsg()
David Benjamin5f237bc2015-02-11 17:14:15 -0500263}
264
Adam Langley117da412015-06-10 17:32:25 -0700265// setWorkingDirectory walks up directories as needed until the current working
266// directory is the top of a BoringSSL checkout.
267func setWorkingDirectory() {
268 for i := 0; i < 64; i++ {
David Benjamin95aaf4a2015-09-03 12:09:36 -0400269 if _, err := os.Stat("BUILDING.md"); err == nil {
Adam Langley117da412015-06-10 17:32:25 -0700270 return
271 }
272 os.Chdir("..")
273 }
274
David Benjamin95aaf4a2015-09-03 12:09:36 -0400275 panic("Couldn't find BUILDING.md in a parent directory!")
Adam Langley117da412015-06-10 17:32:25 -0700276}
277
278func parseTestConfig(filename string) ([]test, error) {
279 in, err := os.Open(filename)
280 if err != nil {
281 return nil, err
282 }
283 defer in.Close()
284
285 decoder := json.NewDecoder(in)
Adam Langleye212f272017-02-03 08:52:21 -0800286 var testArgs [][]string
287 if err := decoder.Decode(&testArgs); err != nil {
Adam Langley117da412015-06-10 17:32:25 -0700288 return nil, err
289 }
Adam Langleye212f272017-02-03 08:52:21 -0800290
291 var result []test
292 for _, args := range testArgs {
293 result = append(result, test{args: args})
294 }
Adam Langley117da412015-06-10 17:32:25 -0700295 return result, nil
296}
297
Steven Valdez32223942016-03-02 11:53:07 -0500298func worker(tests <-chan test, results chan<- result, done *sync.WaitGroup) {
299 defer done.Done()
300 for test := range tests {
301 passed, err := runTest(test)
302 results <- result{test, passed, err}
303 }
304}
305
Adam Langleye212f272017-02-03 08:52:21 -0800306func (t test) cpuMsg() string {
307 if len(t.cpu) == 0 {
308 return ""
309 }
310
311 return fmt.Sprintf(" (for CPU %q)", t.cpu)
312}
313
David Benjamin3d14a152017-06-07 14:02:03 -0400314func (t test) getGTestShards() ([]test, error) {
315 if *numWorkers == 1 || len(t.args) != 1 {
316 return []test{t}, nil
317 }
318
319 // Only shard the three GTest-based tests.
320 if t.args[0] != "crypto/crypto_test" && t.args[0] != "ssl/ssl_test" && t.args[0] != "decrepit/decrepit_test" {
321 return []test{t}, nil
322 }
323
324 prog := path.Join(*buildDir, t.args[0])
325 cmd := exec.Command(prog, "--gtest_list_tests")
326 var stdout bytes.Buffer
327 cmd.Stdout = &stdout
328 if err := cmd.Start(); err != nil {
329 return nil, err
330 }
331 if err := cmd.Wait(); err != nil {
332 return nil, err
333 }
334
335 var group string
336 var tests []string
337 scanner := bufio.NewScanner(&stdout)
338 for scanner.Scan() {
339 line := scanner.Text()
340
341 // Remove the parameter comment and trailing space.
342 if idx := strings.Index(line, "#"); idx >= 0 {
343 line = line[:idx]
344 }
345 line = strings.TrimSpace(line)
346 if len(line) == 0 {
347 continue
348 }
349
350 if line[len(line)-1] == '.' {
351 group = line
352 continue
353 }
354
355 if len(group) == 0 {
356 return nil, fmt.Errorf("found test case %q without group", line)
357 }
358 tests = append(tests, group+line)
359 }
360
361 const testsPerShard = 20
362 if len(tests) <= testsPerShard {
363 return []test{t}, nil
364 }
365
366 // Slow tests which process large test vector files tend to be grouped
367 // together, so shuffle the order.
368 shuffled := make([]string, len(tests))
369 perm := rand.Perm(len(tests))
370 for i, j := range perm {
371 shuffled[i] = tests[j]
372 }
373
374 var shards []test
375 for i := 0; i < len(shuffled); i += testsPerShard {
376 n := len(shuffled) - i
377 if n > testsPerShard {
378 n = testsPerShard
379 }
380 shard := t
381 shard.args = []string{shard.args[0], "--gtest_filter=" + strings.Join(shuffled[i:i+n], ":")}
382 shards = append(shards, shard)
383 }
384
385 return shards, nil
386}
387
David Benjamin491b9212015-02-11 14:18:45 -0500388func main() {
389 flag.Parse()
Adam Langley117da412015-06-10 17:32:25 -0700390 setWorkingDirectory()
391
Steven Valdez32223942016-03-02 11:53:07 -0500392 testCases, err := parseTestConfig("util/all_tests.json")
Adam Langley117da412015-06-10 17:32:25 -0700393 if err != nil {
394 fmt.Printf("Failed to parse input: %s\n", err)
395 os.Exit(1)
396 }
David Benjamin491b9212015-02-11 14:18:45 -0500397
Steven Valdez32223942016-03-02 11:53:07 -0500398 var wg sync.WaitGroup
399 tests := make(chan test, *numWorkers)
David Benjamin8b9e7802016-03-02 18:23:21 -0500400 results := make(chan result, *numWorkers)
Steven Valdez32223942016-03-02 11:53:07 -0500401
402 for i := 0; i < *numWorkers; i++ {
David Benjamin8b9e7802016-03-02 18:23:21 -0500403 wg.Add(1)
Steven Valdez32223942016-03-02 11:53:07 -0500404 go worker(tests, results, &wg)
405 }
406
Steven Valdez32223942016-03-02 11:53:07 -0500407 go func() {
David Benjamin8b9e7802016-03-02 18:23:21 -0500408 for _, test := range testCases {
Adam Langleye212f272017-02-03 08:52:21 -0800409 if *useSDE {
David Benjamin3d14a152017-06-07 14:02:03 -0400410 // SDE generates plenty of tasks and gets slower
411 // with additional sharding.
Adam Langleye212f272017-02-03 08:52:21 -0800412 for _, cpu := range sdeCPUs {
413 testForCPU := test
414 testForCPU.cpu = cpu
415 tests <- testForCPU
416 }
417 } else {
David Benjamin3d14a152017-06-07 14:02:03 -0400418 shards, err := test.getGTestShards()
419 if err != nil {
420 fmt.Printf("Error listing tests: %s\n", err)
421 os.Exit(1)
422 }
423 for _, shard := range shards {
424 tests <- shard
425 }
Adam Langleye212f272017-02-03 08:52:21 -0800426 }
David Benjamin8b9e7802016-03-02 18:23:21 -0500427 }
428 close(tests)
429
Steven Valdez32223942016-03-02 11:53:07 -0500430 wg.Wait()
431 close(results)
432 }()
433
David Benjamin5f237bc2015-02-11 17:14:15 -0500434 testOutput := newTestOutput()
David Benjamin491b9212015-02-11 14:18:45 -0500435 var failed []test
Steven Valdez32223942016-03-02 11:53:07 -0500436 for testResult := range results {
437 test := testResult.Test
Adam Langleye212f272017-02-03 08:52:21 -0800438 args := test.args
David Benjamin5f237bc2015-02-11 17:14:15 -0500439
Adam Langleye212f272017-02-03 08:52:21 -0800440 fmt.Printf("%s%s\n", strings.Join(args, " "), test.cpuMsg())
David Benjamin5f237bc2015-02-11 17:14:15 -0500441 name := shortTestName(test)
Steven Valdez32223942016-03-02 11:53:07 -0500442 if testResult.Error != nil {
Adam Langleye212f272017-02-03 08:52:21 -0800443 fmt.Printf("%s failed to complete: %s\n", args[0], testResult.Error)
David Benjamin491b9212015-02-11 14:18:45 -0500444 failed = append(failed, test)
David Benjamin5f237bc2015-02-11 17:14:15 -0500445 testOutput.addResult(name, "CRASHED")
Steven Valdez32223942016-03-02 11:53:07 -0500446 } else if !testResult.Passed {
Adam Langleye212f272017-02-03 08:52:21 -0800447 fmt.Printf("%s failed to print PASS on the last line.\n", args[0])
David Benjamin491b9212015-02-11 14:18:45 -0500448 failed = append(failed, test)
David Benjamin5f237bc2015-02-11 17:14:15 -0500449 testOutput.addResult(name, "FAIL")
450 } else {
451 testOutput.addResult(name, "PASS")
David Benjamin491b9212015-02-11 14:18:45 -0500452 }
453 }
454
David Benjamin5f237bc2015-02-11 17:14:15 -0500455 if *jsonOutput != "" {
456 if err := testOutput.writeTo(*jsonOutput); err != nil {
457 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
458 }
459 }
David Benjamin2ab7a862015-04-04 17:02:18 -0400460
461 if len(failed) > 0 {
David Benjamin8b9e7802016-03-02 18:23:21 -0500462 fmt.Printf("\n%d of %d tests failed:\n", len(failed), len(testCases))
David Benjamin2ab7a862015-04-04 17:02:18 -0400463 for _, test := range failed {
Adam Langleye5adaef2017-05-02 14:48:47 -0700464 fmt.Printf("\t%s%s\n", strings.Join(test.args, " "), test.cpuMsg())
David Benjamin2ab7a862015-04-04 17:02:18 -0400465 }
466 os.Exit(1)
467 }
468
469 fmt.Printf("\nAll tests passed!\n")
David Benjamin491b9212015-02-11 14:18:45 -0500470}