forked from google/fuzzbench
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_makefile.py
executable file
·145 lines (121 loc) · 4.97 KB
/
generate_makefile.py
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
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Simple generator for local Makefile rules."""
import os
from common import yaml_utils
from common import benchmark_utils
from common import fuzzer_utils
from experiment.build import docker_images
BASE_TAG = "gcr.io/fuzzbench"
BENCHMARK_DIR = benchmark_utils.BENCHMARKS_DIR
def _print_benchmark_fuzz_target(benchmarks):
"""Prints benchmark variables from benchmark.yaml files."""
for benchmark in benchmarks:
benchmark_vars = yaml_utils.read(
os.path.join(BENCHMARK_DIR, benchmark, 'benchmark.yaml'))
print(benchmark + '-fuzz-target=' + benchmark_vars['fuzz_target'])
print()
def _print_makefile_run_template(image):
fuzzer, benchmark = image['tag'].split('/')[1:]
for run_type in ('run', 'debug', 'test-run'):
print(('{run_type}-{fuzzer}-{benchmark}: ' +
'.{fuzzer}-{benchmark}-runner').format(run_type=run_type,
benchmark=benchmark,
fuzzer=fuzzer))
print('\
\tdocker run \\\n\
\t--cpus=1 \\\n\
\t--cap-add SYS_NICE \\\n\
\t--cap-add SYS_PTRACE \\\n\
\t-e FUZZ_OUTSIDE_EXPERIMENT=1 \\\n\
\t-e FORCE_LOCAL=1 \\\n\
\t-e TRIAL_ID=1 \\\n\
\t-e FUZZER={fuzzer} \\\n\
\t-e BENCHMARK={benchmark} \\\n\
\t-e FUZZ_TARGET=$({benchmark}-fuzz-target) \\\
'.format(fuzzer=fuzzer, benchmark=benchmark))
if run_type == 'test-run':
print('\t-e MAX_TOTAL_TIME=20 \\\n\t-e SNAPSHOT_PERIOD=10 \\')
if run_type == 'debug':
print('\t--entrypoint "/bin/bash" \\\n\t-it ', end='')
elif run_type == 'run':
print('\t-it ', end='')
else:
print('\t', end='')
print(os.path.join(BASE_TAG, image['tag']))
print()
# TODO(tanq16): Add unit test.
def _print_rules_for_image(name, image):
"""Print makefile section for given image to stdout."""
if not ('base' in name or 'dispatcher' in name):
print('.', end='')
print(name + ':', end='')
if 'depends_on' in image:
for dep in image['depends_on']:
if 'base' in dep:
print(' ' + dep, end='')
else:
print(' .' + dep, end='')
print()
print('\tdocker build \\')
print('\t--tag ' + os.path.join(BASE_TAG, image['tag']) + ' \\')
print('\t--build-arg BUILDKIT_INLINE_CACHE=1 \\')
print('\t--cache-from ' + os.path.join(BASE_TAG, image['tag']) + ' \\')
if 'build_arg' in image:
for arg in image['build_arg']:
print('\t--build-arg ' + arg + ' \\')
if 'dockerfile' in image:
print('\t--file ' + image['dockerfile'] + ' \\')
print('\t' + image['context'])
print()
# Print run, debug, test-run rules if image is a runner.
if 'runner' in name and not ('intermediate' in name or 'base' in name):
_print_makefile_run_template(image)
def main():
"""Generates Makefile with docker image build rules."""
fuzzers = fuzzer_utils.get_fuzzer_names()
benchmarks = benchmark_utils.get_all_benchmarks()
buildable_images = docker_images.get_images_to_build(fuzzers, benchmarks)
print('export DOCKER_BUILDKIT := 1')
# Print oss-fuzz benchmarks property variables.
_print_benchmark_fuzz_target(benchmarks)
for name, image in buildable_images.items():
_print_rules_for_image(name, image)
# Print build targets for all fuzzer-benchmark pairs (including coverage).
fuzzers.append('coverage')
for fuzzer in fuzzers:
image_type = "runner"
if 'coverage' in fuzzer:
image_type = "builder"
for benchmark in benchmarks:
print(('build-{fuzzer}-{benchmark}: ' +
'.{fuzzer}-{benchmark}-{image_type}\n').format(
fuzzer=fuzzer,
benchmark=benchmark,
image_type=image_type))
print()
# Print fuzzer-all benchmarks build targets.
for fuzzer in fuzzers:
all_build_targets = ' '.join([
'build-{0}-{1}'.format(fuzzer, benchmark)
for benchmark in benchmarks
])
print('build-{fuzzer}-all: {all_targets}'.format(
fuzzer=fuzzer, all_targets=all_build_targets))
# Print all targets build target.
all_build_targets = ' '.join(
['build-{0}-all'.format(name) for name in fuzzers])
print('build-all: {all_targets}'.format(all_targets=all_build_targets))
if __name__ == '__main__':
main()