From 0d0fbbe73f5b02bfeac0aedd0b6b9e8417ab0b0f Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 26 Aug 2016 07:09:25 +0200 Subject: overhaul syscall description generation process This splits generation process into two phases: 1. Extract values of constants from linux kernel sources. 2. Generate Go code. Constant values are checked in. The advantage is that the second phase is now completely independent from linux source files, kernel version, presence of headers for particular drivers, etc. This allows to change what Go code we generate any time without access to all kernel headers (which in future won't be limited to only upstream headers). Constant extraction process does require proper kernel sources, but this can be done only once by the person who added the driver and has access to the required sources. Then the constant values are checked in for others to use. Consant extraction process is per-file/per-arch. That is, if I am adding a driver that is not present upstream and that works only on a single arch, I will check in constants only for that driver and for that arch. --- sysgen/fetch.go | 106 -------------------------------------------------------- 1 file changed, 106 deletions(-) delete mode 100644 sysgen/fetch.go (limited to 'sysgen/fetch.go') diff --git a/sysgen/fetch.go b/sysgen/fetch.go deleted file mode 100644 index d739541f0..000000000 --- a/sysgen/fetch.go +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright 2015 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 main - -import ( - "fmt" - "io/ioutil" - "os" - "os/exec" - "strconv" - "strings" -) - -// fetchValues converts literal constants (e.g. O_APPEND) or any other C expressions -// into their respective numeric values. It does so by builting and executing a C program -// that prints values of the provided expressions. -func fetchValues(arch string, vals []string, includes []string, defines map[string]string, cflags []string) []string { - logf(1, "Use C compiler to fetch constant values for arch=%v", arch) - includeText := "" - for _, inc := range includes { - includeText += fmt.Sprintf("#include <%v>\n", inc) - } - definesText := "" - for k, v := range defines { - definesText += fmt.Sprintf("#ifndef %v\n#define %v %v\n#endif\n", k, k, v) - } - src := strings.Replace(fetchSrc, "[[INCLUDES]]", includeText, 1) - src = strings.Replace(src, "[[DEFAULTS]]", definesText, 1) - src = strings.Replace(src, "[[VALS]]", strings.Join(vals, ","), 1) - bin, err := ioutil.TempFile("", "") - if err != nil { - failf("failed to create temp file: %v", err) - } - bin.Close() - defer os.Remove(bin.Name()) - logf(2, " Build C program into temp file %v", bin.Name()) - - args := []string{"-x", "c", "-", "-o", bin.Name()} - args = append(args, cflags...) - args = append(args, []string{ - // This would be useful to ensure that we don't include any host headers, - // but kernel includes at least - // "-nostdinc", - "-I.", - "-D__KERNEL__", - "-DKBUILD_MODNAME=\"-\"", - "-I" + *flagLinux + "/arch/" + arch + "/include", - "-I" + *flagLinuxBld + "/arch/" + arch + "/include/generated/uapi", - "-I" + *flagLinuxBld + "/arch/" + arch + "/include/generated", - "-I" + *flagLinuxBld + "/include", - "-I" + *flagLinux + "/include", - "-I" + *flagLinux + "/arch/" + arch + "/include/uapi", - "-I" + *flagLinuxBld + "/arch/" + arch + "/include/generated/uapi", - "-I" + *flagLinux + "/include/uapi", - "-I" + *flagLinuxBld + "/include/generated/uapi", - "-I" + *flagLinux, - "-include", *flagLinux + "/include/linux/kconfig.h", - }...) - - logf(4, " Source code:\n%v", src) - logf(2, " Execute gcc with: %v", args) - cmd := exec.Command("gcc", args...) - cmd.Stdin = strings.NewReader(src) - out, err := cmd.CombinedOutput() - if err != nil { - failf("failed to run gcc: %v\n%v", err, string(out)) - } - - out, err = exec.Command(bin.Name()).CombinedOutput() - if err != nil { - failf("failed to flags binary: %v\n%v", err, string(out)) - } - - flagVals := strings.Split(string(out), " ") - if len(flagVals) != len(vals) { - failf("fetched wrong number of values") - } - for _, v := range flagVals { - _, err := strconv.ParseUint(v, 10, 64) - if err != nil { - failf("failed to parse value: %v (%v)", err, v) - } - } - return flagVals -} - -var fetchSrc = ` -[[INCLUDES]] -[[DEFAULTS]] -int printf(const char *format, ...); -unsigned long phys_base; -#ifndef __phys_addr -unsigned long __phys_addr(unsigned long addr) { return 0; } -#endif -int main() { - int i; - unsigned long vals[] = {[[VALS]]}; - for (i = 0; i < sizeof(vals)/sizeof(vals[0]); i++) { - if (i != 0) - printf(" "); - printf("%lu", vals[i]); - } - return 0; -} -` -- cgit mrf-deployment