1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
// Copyright 2017 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"
"strings"
"time"
"github.com/google/syzkaller/pkg/compiler"
"github.com/google/syzkaller/pkg/osutil"
)
type linux struct{}
func (*linux) prepare(sourcedir string, build bool, arches []string) error {
if sourcedir == "" {
return fmt.Errorf("provide path to kernel checkout via -sourcedir flag (or make extract SOURCEDIR)")
}
if build {
// Otherwise out-of-tree build fails.
fmt.Printf("make mrproper\n")
out, err := osutil.RunCmd(time.Hour, sourcedir, "make", "mrproper")
if err != nil {
return fmt.Errorf("make mrproper failed: %v\n%s\n", err, out)
}
} else {
if len(arches) > 1 {
return fmt.Errorf("more than 1 arch is invalid without -build")
}
}
return nil
}
func (*linux) prepareArch(arch *Arch) error {
if !arch.build {
return nil
}
target := arch.target
kernelDir := arch.sourceDir
buildDir := arch.buildDir
makeArgs := []string{
"ARCH=" + target.KernelArch,
"CROSS_COMPILE=" + target.CCompilerPrefix,
"CFLAGS=" + strings.Join(target.CrossCFlags, " "),
"O=" + buildDir,
}
out, err := osutil.RunCmd(time.Hour, kernelDir, "make", append(makeArgs, "defconfig")...)
if err != nil {
return fmt.Errorf("make defconfig failed: %v\n%s\n", err, out)
}
// Without CONFIG_NETFILTER kernel does not build.
out, err = osutil.RunCmd(time.Minute, buildDir, "sed", "-i",
"s@# CONFIG_NETFILTER is not set@CONFIG_NETFILTER=y@g", ".config")
if err != nil {
return fmt.Errorf("sed .config failed: %v\n%s\n", err, out)
}
out, err = osutil.RunCmd(time.Hour, kernelDir, "make", append(makeArgs, "olddefconfig")...)
if err != nil {
return fmt.Errorf("make olddefconfig failed: %v\n%s\n", err, out)
}
out, err = osutil.RunCmd(time.Hour, kernelDir, "make", append(makeArgs, "init/main.o")...)
if err != nil {
return fmt.Errorf("make failed: %v\n%s\n", err, out)
}
return nil
}
func (*linux) processFile(arch *Arch, info *compiler.ConstInfo) (map[string]uint64, map[string]bool, error) {
headerArch := arch.target.KernelHeaderArch
sourceDir := arch.sourceDir
buildDir := arch.buildDir
args := []string{
// This would be useful to ensure that we don't include any host headers,
// but kernel includes at least <stdarg.h>
// "-nostdinc",
"-w", "-fmessage-length=0",
"-O3", // required to get expected values for some __builtin_constant_p
"-I.",
"-D__KERNEL__",
"-DKBUILD_MODNAME=\"-\"",
"-I" + sourceDir + "/arch/" + headerArch + "/include",
"-I" + buildDir + "/arch/" + headerArch + "/include/generated/uapi",
"-I" + buildDir + "/arch/" + headerArch + "/include/generated",
"-I" + buildDir + "/include",
"-I" + sourceDir + "/include",
"-I" + sourceDir + "/arch/" + headerArch + "/include/uapi",
"-I" + buildDir + "/arch/" + headerArch + "/include/generated/uapi",
"-I" + sourceDir + "/include/uapi",
"-I" + buildDir + "/include/generated/uapi",
"-I" + sourceDir,
"-include", sourceDir + "/include/linux/kconfig.h",
}
args = append(args, arch.target.CFlags...)
for _, incdir := range info.Incdirs {
args = append(args, "-I"+sourceDir+"/"+incdir)
}
const addSource = `
#include <asm/unistd.h>
unsigned long phys_base;
#ifndef __phys_addr
unsigned long __phys_addr(unsigned long addr) { return 0; }
#endif
`
res, undeclared, err := extract(info, "gcc", args, addSource, true)
if err != nil {
return nil, nil, err
}
if arch.target.PtrSize == 4 {
// mmap syscall on i386/arm is translated to old_mmap and has different signature.
// As a workaround fix it up to mmap2, which has signature that we expect.
// pkg/csource has the same hack.
const mmap = "__NR_mmap"
const mmap2 = "__NR_mmap2"
if res[mmap] != 0 || undeclared[mmap] {
if res[mmap2] == 0 {
return nil, nil, fmt.Errorf("%v is missing", mmap2)
}
res[mmap] = res[mmap2]
delete(undeclared, mmap)
}
}
return res, undeclared, nil
}
|