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 //.
This commit is contained in:
Dmitry Vyukov 2020-08-06 14:55:39 +02:00
parent 56fe566546
commit cb436c69d9
7 changed files with 119 additions and 60 deletions

View File

@ -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

View File

@ -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);

View File

@ -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];

View File

@ -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

107
executor/style_test.go Normal file
View File

@ -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
}

View File

@ -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;

View File

@ -184,51 +184,3 @@ func TestExecutorMacros(t *testing.T) {
}
}
}
func TestExecutorMistakes(t *testing.T) {
mistakes := []struct {
pattern string
message string
tests []string
}{
{
pattern: `\)\n\t*(debug|debug_dump_data)\(`,
message: "debug() calls are stripped from C reproducers, this code will break. Use {} around debug() to fix",
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 */`,
},
},
}
for _, mistake := range mistakes {
re := regexp.MustCompile(mistake.pattern)
for _, test := range mistake.tests {
if !re.MatchString(test) {
t.Errorf("patter %q does not match test %q", mistake.pattern, test)
}
}
for _, match := range re.FindAllStringIndex(commonHeader, -1) {
start, end := match[0], match[1]
for start != 0 && commonHeader[start] != '\n' {
start--
}
for end != len(commonHeader) && commonHeader[end] != '\n' {
end++
}
t.Errorf("%v:%v", mistake.message, commonHeader[start:end])
}
}
}