blob: 3cf23206b5c1e21160f1ebfa6bdb3dcd95168f70 [file] [log] [blame]
Adam Langleye9ada862015-05-11 17:20:37 -07001/* 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 (
18 "bytes"
19 "encoding/json"
20 "flag"
21 "fmt"
22 "os"
23 "os/exec"
24 "path"
Adam Langleyf4e42722015-06-04 17:45:09 -070025 "strconv"
Adam Langleye9ada862015-05-11 17:20:37 -070026 "strings"
David Benjamin4969cc92016-04-22 15:02:23 -040027 "sync"
Adam Langleyf4e42722015-06-04 17:45:09 -070028 "syscall"
Adam Langleye9ada862015-05-11 17:20:37 -070029 "time"
30)
31
32// TODO(davidben): Link tests with the malloc shim and port -malloc-test to this runner.
33
34var (
Adam Langleyf4e42722015-06-04 17:45:09 -070035 useValgrind = flag.Bool("valgrind", false, "If true, run code under valgrind")
David Benjamin4969cc92016-04-22 15:02:23 -040036 useCallgrind = flag.Bool("callgrind", false, "If true, run code under valgrind to generate callgrind traces.")
Adam Langleyf4e42722015-06-04 17:45:09 -070037 useGDB = flag.Bool("gdb", false, "If true, run BoringSSL code under gdb")
Robert Sloan4d1ac502017-02-06 08:36:14 -080038 useSDE = flag.Bool("sde", false, "If true, run BoringSSL code under Intel's SDE for each supported chip")
Adam Langleyf4e42722015-06-04 17:45:09 -070039 buildDir = flag.String("build-dir", "build", "The build directory to run the tests from.")
David Benjamin4969cc92016-04-22 15:02:23 -040040 numWorkers = flag.Int("num-workers", 1, "Runs the given number of workers when testing.")
Adam Langleyf4e42722015-06-04 17:45:09 -070041 jsonOutput = flag.String("json-output", "", "The file to output JSON results to.")
42 mallocTest = flag.Int64("malloc-test", -1, "If non-negative, run each test with each malloc in turn failing from the given number onwards.")
43 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.")
Adam Langleye9ada862015-05-11 17:20:37 -070044)
45
Robert Sloan4d1ac502017-02-06 08:36:14 -080046type test struct {
47 args []string
48 // cpu, if not empty, contains an Intel CPU code to simulate. Run
49 // `sde64 -help` to get a list of these codes.
50 cpu string
51}
Adam Langleye9ada862015-05-11 17:20:37 -070052
David Benjamin4969cc92016-04-22 15:02:23 -040053type result struct {
54 Test test
55 Passed bool
56 Error error
57}
58
Adam Langleye9ada862015-05-11 17:20:37 -070059// testOutput is a representation of Chromium's JSON test result format. See
60// https://www.chromium.org/developers/the-json-test-results-format
61type testOutput struct {
62 Version int `json:"version"`
63 Interrupted bool `json:"interrupted"`
64 PathDelimiter string `json:"path_delimiter"`
65 SecondsSinceEpoch float64 `json:"seconds_since_epoch"`
66 NumFailuresByType map[string]int `json:"num_failures_by_type"`
67 Tests map[string]testResult `json:"tests"`
68}
69
70type testResult struct {
71 Actual string `json:"actual"`
72 Expected string `json:"expected"`
73 IsUnexpected bool `json:"is_unexpected"`
74}
75
Robert Sloan4d1ac502017-02-06 08:36:14 -080076// sdeCPUs contains a list of CPU code that we run all tests under when *useSDE
77// is true.
78var sdeCPUs = []string{
79 "p4p", // Pentium4 Prescott
80 "mrm", // Merom
81 "pnr", // Penryn
82 "nhm", // Nehalem
83 "wsm", // Westmere
84 "snb", // Sandy Bridge
85 "ivb", // Ivy Bridge
86 "hsw", // Haswell
87 "bdw", // Broadwell
88 "skx", // Skylake Server
89 "skl", // Skylake Client
90 "cnl", // Cannonlake
91 "knl", // Knights Landing
92 "slt", // Saltwell
93 "slm", // Silvermont
94 "glm", // Goldmont
95}
96
Adam Langleye9ada862015-05-11 17:20:37 -070097func newTestOutput() *testOutput {
98 return &testOutput{
99 Version: 3,
100 PathDelimiter: ".",
101 SecondsSinceEpoch: float64(time.Now().UnixNano()) / float64(time.Second/time.Nanosecond),
102 NumFailuresByType: make(map[string]int),
103 Tests: make(map[string]testResult),
104 }
105}
106
107func (t *testOutput) addResult(name, result string) {
108 if _, found := t.Tests[name]; found {
109 panic(name)
110 }
111 t.Tests[name] = testResult{
112 Actual: result,
113 Expected: "PASS",
114 IsUnexpected: result != "PASS",
115 }
116 t.NumFailuresByType[result]++
117}
118
119func (t *testOutput) writeTo(name string) error {
120 file, err := os.Create(name)
121 if err != nil {
122 return err
123 }
124 defer file.Close()
125 out, err := json.MarshalIndent(t, "", " ")
126 if err != nil {
127 return err
128 }
129 _, err = file.Write(out)
130 return err
131}
132
133func valgrindOf(dbAttach bool, path string, args ...string) *exec.Cmd {
David Benjamin7c0d06c2016-08-11 13:26:41 -0400134 valgrindArgs := []string{"--error-exitcode=99", "--track-origins=yes", "--leak-check=full", "--quiet"}
Adam Langleye9ada862015-05-11 17:20:37 -0700135 if dbAttach {
136 valgrindArgs = append(valgrindArgs, "--db-attach=yes", "--db-command=xterm -e gdb -nw %f %p")
137 }
138 valgrindArgs = append(valgrindArgs, path)
139 valgrindArgs = append(valgrindArgs, args...)
140
141 return exec.Command("valgrind", valgrindArgs...)
142}
143
David Benjamin4969cc92016-04-22 15:02:23 -0400144func callgrindOf(path string, args ...string) *exec.Cmd {
145 valgrindArgs := []string{"-q", "--tool=callgrind", "--dump-instr=yes", "--collect-jumps=yes", "--callgrind-out-file=" + *buildDir + "/callgrind/callgrind.out.%p"}
146 valgrindArgs = append(valgrindArgs, path)
147 valgrindArgs = append(valgrindArgs, args...)
148
149 return exec.Command("valgrind", valgrindArgs...)
150}
151
Adam Langleyf4e42722015-06-04 17:45:09 -0700152func gdbOf(path string, args ...string) *exec.Cmd {
153 xtermArgs := []string{"-e", "gdb", "--args"}
154 xtermArgs = append(xtermArgs, path)
155 xtermArgs = append(xtermArgs, args...)
156
157 return exec.Command("xterm", xtermArgs...)
158}
159
Robert Sloan4d1ac502017-02-06 08:36:14 -0800160func sdeOf(cpu, path string, args ...string) *exec.Cmd {
161 sdeArgs := []string{"-" + cpu, "--", path}
162 sdeArgs = append(sdeArgs, args...)
163 return exec.Command("sde", sdeArgs...)
164}
165
Adam Langleyf4e42722015-06-04 17:45:09 -0700166type moreMallocsError struct{}
167
168func (moreMallocsError) Error() string {
169 return "child process did not exhaust all allocation calls"
170}
171
172var errMoreMallocs = moreMallocsError{}
173
174func runTestOnce(test test, mallocNumToFail int64) (passed bool, err error) {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800175 prog := path.Join(*buildDir, test.args[0])
176 args := test.args[1:]
Adam Langleye9ada862015-05-11 17:20:37 -0700177 var cmd *exec.Cmd
178 if *useValgrind {
179 cmd = valgrindOf(false, prog, args...)
David Benjamin4969cc92016-04-22 15:02:23 -0400180 } else if *useCallgrind {
181 cmd = callgrindOf(prog, args...)
Adam Langleyf4e42722015-06-04 17:45:09 -0700182 } else if *useGDB {
183 cmd = gdbOf(prog, args...)
Robert Sloan4d1ac502017-02-06 08:36:14 -0800184 } else if *useSDE {
185 cmd = sdeOf(test.cpu, prog, args...)
Adam Langleye9ada862015-05-11 17:20:37 -0700186 } else {
187 cmd = exec.Command(prog, args...)
188 }
Robert Sloan5d625782017-02-13 09:55:39 -0800189 var outBuf bytes.Buffer
190 cmd.Stdout = &outBuf
191 cmd.Stderr = &outBuf
Adam Langleyf4e42722015-06-04 17:45:09 -0700192 if mallocNumToFail >= 0 {
193 cmd.Env = os.Environ()
194 cmd.Env = append(cmd.Env, "MALLOC_NUMBER_TO_FAIL="+strconv.FormatInt(mallocNumToFail, 10))
195 if *mallocTestDebug {
196 cmd.Env = append(cmd.Env, "MALLOC_ABORT_ON_FAIL=1")
197 }
198 cmd.Env = append(cmd.Env, "_MALLOC_CHECK=1")
199 }
Adam Langleye9ada862015-05-11 17:20:37 -0700200
201 if err := cmd.Start(); err != nil {
202 return false, err
203 }
204 if err := cmd.Wait(); err != nil {
Adam Langleyf4e42722015-06-04 17:45:09 -0700205 if exitError, ok := err.(*exec.ExitError); ok {
206 if exitError.Sys().(syscall.WaitStatus).ExitStatus() == 88 {
207 return false, errMoreMallocs
208 }
209 }
Robert Sloan5d625782017-02-13 09:55:39 -0800210 fmt.Print(string(outBuf.Bytes()))
Adam Langleye9ada862015-05-11 17:20:37 -0700211 return false, err
212 }
213
214 // Account for Windows line-endings.
Robert Sloan5d625782017-02-13 09:55:39 -0800215 stdout := bytes.Replace(outBuf.Bytes(), []byte("\r\n"), []byte("\n"), -1)
Adam Langleye9ada862015-05-11 17:20:37 -0700216
217 if bytes.HasSuffix(stdout, []byte("PASS\n")) &&
218 (len(stdout) == 5 || stdout[len(stdout)-6] == '\n') {
219 return true, nil
220 }
David Benjaminf31229b2017-01-25 14:08:15 -0500221
222 // Also accept a googletest-style pass line. This is left here in
223 // transition until the tests are all converted and this script made
224 // unnecessary.
225 if bytes.Contains(stdout, []byte("\n[ PASSED ]")) {
226 return true, nil
227 }
228
Robert Sloan5d625782017-02-13 09:55:39 -0800229 fmt.Print(string(outBuf.Bytes()))
Adam Langleye9ada862015-05-11 17:20:37 -0700230 return false, nil
231}
232
Adam Langleyf4e42722015-06-04 17:45:09 -0700233func runTest(test test) (bool, error) {
234 if *mallocTest < 0 {
235 return runTestOnce(test, -1)
236 }
237
238 for mallocNumToFail := int64(*mallocTest); ; mallocNumToFail++ {
239 if passed, err := runTestOnce(test, mallocNumToFail); err != errMoreMallocs {
240 if err != nil {
241 err = fmt.Errorf("at malloc %d: %s", mallocNumToFail, err)
242 }
243 return passed, err
244 }
245 }
246}
247
Adam Langleye9ada862015-05-11 17:20:37 -0700248// shortTestName returns the short name of a test. Except for evp_test, it
249// assumes that any argument which ends in .txt is a path to a data file and not
250// relevant to the test's uniqueness.
251func shortTestName(test test) string {
252 var args []string
Robert Sloan4d1ac502017-02-06 08:36:14 -0800253 for _, arg := range test.args {
254 if test.args[0] == "crypto/evp/evp_test" || !strings.HasSuffix(arg, ".txt") {
Adam Langleye9ada862015-05-11 17:20:37 -0700255 args = append(args, arg)
256 }
257 }
Robert Sloan4d1ac502017-02-06 08:36:14 -0800258 return strings.Join(args, " ") + test.cpuMsg()
Adam Langleye9ada862015-05-11 17:20:37 -0700259}
260
Kenny Rootb8494592015-09-25 02:29:14 +0000261// setWorkingDirectory walks up directories as needed until the current working
262// directory is the top of a BoringSSL checkout.
263func setWorkingDirectory() {
264 for i := 0; i < 64; i++ {
265 if _, err := os.Stat("BUILDING.md"); err == nil {
266 return
267 }
268 os.Chdir("..")
269 }
270
271 panic("Couldn't find BUILDING.md in a parent directory!")
272}
273
274func parseTestConfig(filename string) ([]test, error) {
275 in, err := os.Open(filename)
276 if err != nil {
277 return nil, err
278 }
279 defer in.Close()
280
281 decoder := json.NewDecoder(in)
Robert Sloan4d1ac502017-02-06 08:36:14 -0800282 var testArgs [][]string
283 if err := decoder.Decode(&testArgs); err != nil {
Kenny Rootb8494592015-09-25 02:29:14 +0000284 return nil, err
285 }
Robert Sloan4d1ac502017-02-06 08:36:14 -0800286
287 var result []test
288 for _, args := range testArgs {
289 result = append(result, test{args: args})
290 }
Kenny Rootb8494592015-09-25 02:29:14 +0000291 return result, nil
292}
293
David Benjamin4969cc92016-04-22 15:02:23 -0400294func worker(tests <-chan test, results chan<- result, done *sync.WaitGroup) {
295 defer done.Done()
296 for test := range tests {
297 passed, err := runTest(test)
298 results <- result{test, passed, err}
299 }
300}
301
Robert Sloan4d1ac502017-02-06 08:36:14 -0800302func (t test) cpuMsg() string {
303 if len(t.cpu) == 0 {
304 return ""
305 }
306
307 return fmt.Sprintf(" (for CPU %q)", t.cpu)
308}
309
Adam Langleye9ada862015-05-11 17:20:37 -0700310func main() {
311 flag.Parse()
Kenny Rootb8494592015-09-25 02:29:14 +0000312 setWorkingDirectory()
313
David Benjamin4969cc92016-04-22 15:02:23 -0400314 testCases, err := parseTestConfig("util/all_tests.json")
Kenny Rootb8494592015-09-25 02:29:14 +0000315 if err != nil {
316 fmt.Printf("Failed to parse input: %s\n", err)
317 os.Exit(1)
318 }
Adam Langleye9ada862015-05-11 17:20:37 -0700319
David Benjamin4969cc92016-04-22 15:02:23 -0400320 var wg sync.WaitGroup
321 tests := make(chan test, *numWorkers)
322 results := make(chan result, *numWorkers)
323
324 for i := 0; i < *numWorkers; i++ {
325 wg.Add(1)
326 go worker(tests, results, &wg)
327 }
328
329 go func() {
330 for _, test := range testCases {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800331 if *useSDE {
332 for _, cpu := range sdeCPUs {
333 testForCPU := test
334 testForCPU.cpu = cpu
335 tests <- testForCPU
336 }
337 } else {
338 tests <- test
339 }
David Benjamin4969cc92016-04-22 15:02:23 -0400340 }
341 close(tests)
342
343 wg.Wait()
344 close(results)
345 }()
346
Adam Langleye9ada862015-05-11 17:20:37 -0700347 testOutput := newTestOutput()
348 var failed []test
David Benjamin4969cc92016-04-22 15:02:23 -0400349 for testResult := range results {
350 test := testResult.Test
Robert Sloan4d1ac502017-02-06 08:36:14 -0800351 args := test.args
Adam Langleye9ada862015-05-11 17:20:37 -0700352
Robert Sloan4d1ac502017-02-06 08:36:14 -0800353 fmt.Printf("%s%s\n", strings.Join(args, " "), test.cpuMsg())
Adam Langleye9ada862015-05-11 17:20:37 -0700354 name := shortTestName(test)
David Benjamin4969cc92016-04-22 15:02:23 -0400355 if testResult.Error != nil {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800356 fmt.Printf("%s failed to complete: %s\n", args[0], testResult.Error)
Adam Langleye9ada862015-05-11 17:20:37 -0700357 failed = append(failed, test)
358 testOutput.addResult(name, "CRASHED")
David Benjamin4969cc92016-04-22 15:02:23 -0400359 } else if !testResult.Passed {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800360 fmt.Printf("%s failed to print PASS on the last line.\n", args[0])
Adam Langleye9ada862015-05-11 17:20:37 -0700361 failed = append(failed, test)
362 testOutput.addResult(name, "FAIL")
363 } else {
364 testOutput.addResult(name, "PASS")
365 }
366 }
367
368 if *jsonOutput != "" {
369 if err := testOutput.writeTo(*jsonOutput); err != nil {
370 fmt.Fprintf(os.Stderr, "Error: %s\n", err)
371 }
372 }
373
374 if len(failed) > 0 {
David Benjamin4969cc92016-04-22 15:02:23 -0400375 fmt.Printf("\n%d of %d tests failed:\n", len(failed), len(testCases))
Adam Langleye9ada862015-05-11 17:20:37 -0700376 for _, test := range failed {
Robert Sloan4d1ac502017-02-06 08:36:14 -0800377 fmt.Printf("\t%s%s\n", strings.Join(test.args, ""), test.cpuMsg())
Adam Langleye9ada862015-05-11 17:20:37 -0700378 }
379 os.Exit(1)
380 }
381
382 fmt.Printf("\nAll tests passed!\n")
383}