-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
fetch.go
242 lines (225 loc) · 5.6 KB
/
fetch.go
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// 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 (
"bytes"
"debug/elf"
"encoding/binary"
"fmt"
"io"
"os"
"regexp"
"strconv"
"strings"
"text/template"
"github.com/google/syzkaller/pkg/compiler"
"github.com/google/syzkaller/pkg/osutil"
)
type extractParams struct {
AddSource string
DeclarePrintf bool
DefineGlibcUse bool // workaround for incorrect flags to clang for fuchsia.
ExtractFromELF bool
TargetEndian binary.ByteOrder
}
func extract(info *compiler.ConstInfo, cc string, args []string, params *extractParams) (
map[string]uint64, map[string]bool, error) {
data := &CompileData{
extractParams: params,
Defines: info.Defines,
Includes: info.Includes,
}
for _, def := range info.Consts {
data.Values = append(data.Values, def.Name)
}
bin := ""
missingIncludes := make(map[string]bool)
undeclared := make(map[string]bool)
valMap := make(map[string]bool)
for _, def := range info.Consts {
valMap[def.Name] = true
}
for {
bin1, out, err := compile(cc, args, data)
if err == nil {
bin = bin1
break
}
// Some consts and syscall numbers are not defined on some archs.
// Figure out from compiler output undefined consts,
// and try to compile again without them.
// May need to try multiple times because some severe errors terminate compilation.
tryAgain := false
for _, errMsg := range []string{
`error: [‘']([a-zA-Z0-9_]+)[’'] undeclared`,
`note: in expansion of macro [‘']([a-zA-Z0-9_]+)[’']`,
`note: expanded from macro [‘']([a-zA-Z0-9_]+)[’']`,
`error: use of undeclared identifier [‘']([a-zA-Z0-9_]+)[’']`,
} {
re := regexp.MustCompile(errMsg)
matches := re.FindAllSubmatch(out, -1)
for _, match := range matches {
val := string(match[1])
if valMap[val] && !undeclared[val] {
undeclared[val] = true
tryAgain = true
}
}
}
if !tryAgain {
return nil, nil, fmt.Errorf("failed to run compiler: %v %v\n%w\n%s",
cc, args, err, out)
}
data.Values = nil
for _, def := range info.Consts {
if undeclared[def.Name] {
continue
}
data.Values = append(data.Values, def.Name)
}
data.Includes = nil
for _, v := range info.Includes {
if missingIncludes[v] {
continue
}
data.Includes = append(data.Includes, v)
}
}
defer os.Remove(bin)
var flagVals []uint64
var err error
if data.ExtractFromELF {
flagVals, err = extractFromELF(bin, params.TargetEndian)
} else {
flagVals, err = extractFromExecutable(bin)
}
if err != nil {
return nil, nil, err
}
if len(flagVals) != len(data.Values) {
return nil, nil, fmt.Errorf("fetched wrong number of values %v, want != %v",
len(flagVals), len(data.Values))
}
res := make(map[string]uint64)
for i, val := range data.Values {
res[val] = flagVals[i]
}
return res, undeclared, nil
}
type CompileData struct {
*extractParams
Defines map[string]string
Includes []string
Values []string
}
func compile(cc string, args []string, data *CompileData) (string, []byte, error) {
src := new(bytes.Buffer)
if err := srcTemplate.Execute(src, data); err != nil {
return "", nil, fmt.Errorf("failed to generate source: %w", err)
}
binFile, err := osutil.TempFile("syz-extract-bin")
if err != nil {
return "", nil, err
}
args = append(args, []string{
"-x", "c", "-",
"-o", binFile,
"-w",
}...)
if data.ExtractFromELF {
args = append(args, "-c")
}
cmd := osutil.Command(cc, args...)
cmd.Stdin = src
if out, err := cmd.CombinedOutput(); err != nil {
os.Remove(binFile)
return "", out, err
}
return binFile, nil, nil
}
func extractFromExecutable(binFile string) ([]uint64, error) {
out, err := osutil.Command(binFile).CombinedOutput()
if err != nil {
return nil, fmt.Errorf("failed to run flags binary: %w\n%s", err, out)
}
if len(out) == 0 {
return nil, nil
}
var vals []uint64
for _, val := range strings.Split(string(out), " ") {
n, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse value: %w (%v)", err, val)
}
vals = append(vals, n)
}
return vals, nil
}
func extractFromELF(binFile string, targetEndian binary.ByteOrder) ([]uint64, error) {
f, err := os.Open(binFile)
if err != nil {
return nil, err
}
ef, err := elf.NewFile(f)
if err != nil {
return nil, err
}
for _, sec := range ef.Sections {
if sec.Name != "syz_extract_data" {
continue
}
data, err := io.ReadAll(sec.Open())
if err != nil {
return nil, err
}
vals := make([]uint64, len(data)/8)
if err := binary.Read(bytes.NewReader(data), targetEndian, &vals); err != nil {
return nil, err
}
return vals, nil
}
return nil, fmt.Errorf("did not find syz_extract_data section")
}
var srcTemplate = template.Must(template.New("").Parse(`
{{if not .ExtractFromELF}}
#define __asm__(...)
{{end}}
{{if .DefineGlibcUse}}
#ifndef __GLIBC_USE
# define __GLIBC_USE(X) 0
#endif
{{end}}
{{range $incl := $.Includes}}
#include <{{$incl}}>
{{end}}
{{range $name, $val := $.Defines}}
#ifndef {{$name}}
# define {{$name}} {{$val}}
#endif
{{end}}
{{.AddSource}}
{{if .DeclarePrintf}}
int printf(const char *format, ...);
{{end}}
{{if .ExtractFromELF}}
__attribute__((section("syz_extract_data")))
unsigned long long vals[] = {
{{range $val := $.Values}}(unsigned long long){{$val}},
{{end}}
};
{{else}}
int main() {
int i;
unsigned long long vals[] = {
{{range $val := $.Values}}(unsigned long long){{$val}},
{{end}}
};
for (i = 0; i < sizeof(vals)/sizeof(vals[0]); i++) {
if (i != 0)
printf(" ");
printf("%llu", vals[i]);
}
return 0;
}
{{end}}
`))