diff options
| author | Dmitry Vyukov <dvyukov@google.com> | 2020-08-06 14:55:39 +0200 |
|---|---|---|
| committer | Dmitry Vyukov <dvyukov@google.com> | 2020-08-06 15:27:41 +0200 |
| commit | cb436c69d9bcb0330518a48559649c9436ed5e7a (patch) | |
| tree | 1b1d474ad40b0748d3565c51938c1ed79b23f83e /executor | |
| parent | 56fe5665461fbbc19704e44c23e42d5f6ecb5361 (diff) | |
executor: add some code style checks
Move the test from pkg/csource to executor/
in order to be able to (1) run it on *.cc files,
(2) run on unprocessed *.h files, (3) produce line numbers.
Add a check for missed space after //.
Diffstat (limited to 'executor')
| -rw-r--r-- | executor/common.h | 2 | ||||
| -rw-r--r-- | executor/common_bsd.h | 2 | ||||
| -rw-r--r-- | executor/common_linux.h | 6 | ||||
| -rw-r--r-- | executor/executor.cc | 2 | ||||
| -rw-r--r-- | executor/style_test.go | 107 | ||||
| -rw-r--r-- | executor/test_linux.h | 12 |
6 files changed, 119 insertions, 12 deletions
diff --git a/executor/common.h b/executor/common.h index a3f442edc..35e3c78da 100644 --- a/executor/common.h +++ b/executor/common.h @@ -9,7 +9,7 @@ // - NORETURN/PRINTF/debug are removed // - exitf/fail are replaced with exit // - uintN types are replaced with uintN_t -// - /*FOO*/ placeholders are replaced by actual values +// - /*{{{FOO}}}*/ placeholders are replaced by actual values #ifndef _GNU_SOURCE #define _GNU_SOURCE diff --git a/executor/common_bsd.h b/executor/common_bsd.h index 17306fb96..2aa651dd3 100644 --- a/executor/common_bsd.h +++ b/executor/common_bsd.h @@ -61,7 +61,7 @@ static int inject_fault(int nth) en.scope = FAULT_SCOPE_LWP; en.mode = 0; // FAULT_MODE_NTH_ONESHOT - en.nth = nth + 2; //FAULT_NTH_MIN + en.nth = nth + 2; // FAULT_NTH_MIN if (ioctl(fd, FAULT_IOC_ENABLE, &en) != 0) fail("FAULT_IOC_ENABLE failed with nth=%d", nth); diff --git a/executor/common_linux.h b/executor/common_linux.h index 8c20101ec..b49e67542 100644 --- a/executor/common_linux.h +++ b/executor/common_linux.h @@ -2313,12 +2313,12 @@ error: #include <string.h> #include <sys/mount.h> -//syz_mount_image(fs ptr[in, string[disk_filesystems]], dir ptr[in, filename], size intptr, nsegs len[segments], segments ptr[in, array[fs_image_segment]], flags flags[mount_flags], opts ptr[in, fs_options[vfat_options]]) -//fs_image_segment { +// syz_mount_image(fs ptr[in, string[disk_filesystems]], dir ptr[in, filename], size intptr, nsegs len[segments], segments ptr[in, array[fs_image_segment]], flags flags[mount_flags], opts ptr[in, fs_options[vfat_options]]) +// fs_image_segment { // data ptr[in, array[int8]] // size len[data, intptr] // offset intptr -//} +// } static long syz_mount_image(volatile long fsarg, volatile long dir, volatile unsigned long size, volatile unsigned long nsegs, volatile long segments, volatile long flags, volatile long optsarg) { char loopname[64], fs[32], opts[256]; diff --git a/executor/executor.cc b/executor/executor.cc index 5b7371b1d..04d0259f6 100644 --- a/executor/executor.cc +++ b/executor/executor.cc @@ -362,7 +362,7 @@ int main(int argc, char** argv) } if (argc >= 2 && strcmp(argv[1], "setup_kcsan_filterlist") == 0) { #if SYZ_HAVE_KCSAN - setup_kcsan_filterlist(argv + 2, argc - 2, /*suppress=*/true); + setup_kcsan_filterlist(argv + 2, argc - 2, true); #else fail("KCSAN is not implemented"); #endif diff --git a/executor/style_test.go b/executor/style_test.go new file mode 100644 index 000000000..c3fde4889 --- /dev/null +++ b/executor/style_test.go @@ -0,0 +1,107 @@ +// Copyright 2020 syzkaller project authors. All rights reserved. +// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. + +package executor + +import ( + "bytes" + "io/ioutil" + "path/filepath" + "regexp" + "sort" + "strings" + "testing" +) + +func TestExecutorMistakes(t *testing.T) { + checks := []*struct { + pattern string + suppression string + message string + tests []string + commonOnly bool + }{ + { + pattern: `\)\n\t*(debug|debug_dump_data)\(`, + message: "debug() calls are stripped from C reproducers, this code will break. Use {} around debug() to fix", + commonOnly: true, + tests: []string{ + ` +if (foo) + debug("foo failed"); +`, ` + if (x + y) + debug_dump_data(data, len); +`, + }, + }, + { + // These are also not properly stripped by pkg/csource. + pattern: `/\*[^{]`, + message: "Don't use /* */ block comments. Use // line comments instead", + tests: []string{ + `/* C++ comment */`, + }, + }, + { + pattern: `//[^\s]`, + suppression: `https?://`, + message: "Add a space after //", + tests: []string{ + `//foo`, + }, + }, + } + for _, check := range checks { + re := regexp.MustCompile(check.pattern) + for _, test := range check.tests { + if !re.MatchString(test) { + t.Fatalf("patter %q does not match test %q", check.pattern, test) + } + } + } + for _, file := range executorFiles(t) { + data, err := ioutil.ReadFile(file) + if err != nil { + t.Fatal(err) + } + for _, check := range checks { + if check.commonOnly && !strings.Contains(file, "common") { + continue + } + re := regexp.MustCompile(check.pattern) + supp := regexp.MustCompile(check.suppression) + for _, match := range re.FindAllIndex(data, -1) { + start, end := match[0], match[1] + for start != 0 && data[start-1] != '\n' { + start-- + } + for end != len(data) && data[end] != '\n' { + end++ + } + if supp.Match(data[start:end]) { + continue + } + line := bytes.Count(data[:start], []byte{'\n'}) + 1 + t.Errorf("\nexecutor/%v:%v: %v\n%s\n", file, line, check.message, data[start:end]) + } + } + } +} + +func executorFiles(t *testing.T) []string { + cc, err := filepath.Glob("*.cc") + if err != nil { + t.Fatal(err) + } + h, err := filepath.Glob("*.h") + if err != nil { + t.Fatal(err) + } + if len(cc) == 0 || len(h) == 0 { + t.Fatal("found no executor files") + } + res := append(cc, h...) + sort.Strings(res) + return res +} diff --git a/executor/test_linux.h b/executor/test_linux.h index cdeb72717..96e185175 100644 --- a/executor/test_linux.h +++ b/executor/test_linux.h @@ -96,13 +96,13 @@ static int test_kvm() printf("host kernel version %u\n", ver); // TODO: test VM mode. - //const char text16_vm[] = "\x48\xc7\xc3\xde\xc0\xad\x0b\x90\x90\x48\xc7\xc0\xef\xcd\xab\x00\xf4"; - //if (res = test_one(64, text16_vm, sizeof(text16_vm) - 1, KVM_SETUP_VM, KVM_EXIT_HLT, true)) + // const char text16_vm[] = "\x48\xc7\xc3\xde\xc0\xad\x0b\x90\x90\x48\xc7\xc0\xef\xcd\xab\x00\xf4"; + // if (res = test_one(64, text16_vm, sizeof(text16_vm) - 1, KVM_SETUP_VM, KVM_EXIT_HLT, true)) // return res; - /// TODO: test code executed in interrupt handlers. - //const char text32_div0[] = "\x31\xc0\xf7\xf0"; - //if (res = test_one(32, text32_div0, sizeof(text32_div0)-1, 0, KVM_EXIT_HLT, true)) + // TODO: test code executed in interrupt handlers. + // const char text32_div0[] = "\x31\xc0\xf7\xf0"; + // if (res = test_one(32, text32_div0, sizeof(text32_div0)-1, 0, KVM_EXIT_HLT, true)) // return res; const char text8[] = "\x66\xb8\xde\xc0\xad\x0b"; @@ -147,7 +147,7 @@ static int test_kvm() if ((res = test_one(8, text8_smm, sizeof(text8_smm) - 1, KVM_SETUP_SMM | KVM_SETUP_PROTECTED, KVM_EXIT_HLT, true))) return res; - //const char text32_smm[] = "\xb8\xde\xc0\xad\x0b"; + // const char text32_smm[] = "\xb8\xde\xc0\xad\x0b"; if ((res = test_one(32, text8_smm, sizeof(text8_smm) - 1, KVM_SETUP_SMM, KVM_EXIT_HLT, true))) return res; |
