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
|
#!/usr/bin/env python3
import kconfiglib
import logging
import re
import shutil
import subprocess
from versuchung.experiment import Experiment
from versuchung.types import String, Bool, Integer
from versuchung.files import File, Directory
logger = logging.getLogger(__name__)
class AttributeExperiment(Experiment):
outputs = {
"config": File(".config"),
"attributes": File("attributes.json"),
"build_out": File("build.out"),
"build_err": File("build.err"),
}
def run(self):
build_command = self.build_command.value.split()
attr_command = self.attr_command.value.split()
shutil.copyfile(f"{self.project_root.path}/.config", self.config.path)
subprocess.check_call(
["make", "clean"],
cwd=self.project_root.path,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
try:
with open(self.build_out.path, "w") as out_fd, open(
self.build_err.path, "w"
) as err_fd:
subprocess.check_call(
build_command,
cwd=self.project_root.path,
stdout=out_fd,
stderr=err_fd,
)
except subprocess.CalledProcessError:
logger.info("build error")
return
with open(self.attributes.path, "w") as attr_fd:
subprocess.check_call(
attr_command, cwd=self.project_root.path, stdout=attr_fd
)
class RandomConfig(AttributeExperiment):
inputs = {
"randconfig_seed": String("FIXME"),
"project_root": Directory("/tmp"),
"project_version": String("FIXME"),
"clean_command": String("make clean"),
"build_command": String("make"),
"attr_command": String("make attributes"),
}
class ExploreConfig(AttributeExperiment):
inputs = {
"config_hash": String("FIXME"),
"project_root": Directory("/tmp"),
"project_version": String("FIXME"),
"clean_command": String("make clean"),
"build_command": String("make"),
"attr_command": String("make attributes"),
}
class KConfig:
def __init__(self, working_directory):
self.cwd = working_directory
self.clean_command = "make clean"
self.build_command = "make"
self.attribute_command = "make attributes"
def randconfig(self):
status = subprocess.run(
["make", "randconfig"],
cwd=self.cwd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
# make randconfig occasionally generates illegal configurations, so a project may run randconfig more than once.
# Make sure to return the seed of the latest run (don't short-circuit).
seed = None
for line in status.stderr.split("\n"):
match = re.match("KCONFIG_SEED=(.*)", line)
if match:
seed = match.group(1)
if seed:
return seed
raise RuntimeError("KCONFIG_SEED not found")
def git_commit_id(self):
status = subprocess.run(
["git", "rev-parse", "HEAD"],
cwd=self.cwd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
revision = status.stdout.strip()
return revision
def config_hash(self, config_file):
status = subprocess.run(
["sha256sum", config_file],
cwd=self.cwd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
)
config_hash = status.stdout.split()[0]
return config_hash
def run_randconfig(self):
"""Run a randomconfig experiment in the selected project. Results are written to the current working directory."""
experiment = RandomConfig()
experiment(
[
"--randconfig_seed",
self.randconfig(),
"--project_version",
self.git_commit_id(),
"--project_root",
self.cwd,
"--clean_command",
self.clean_command,
"--build_command",
self.build_command,
"--attr_command",
self.attribute_command,
]
)
def config_is_functional(self, kconf):
for choice in kconf.choices:
if (
not choice.is_optional
and 2 in choice.assignable
and choice.selection is None
):
return False
return True
def run_exploration_from_file(self, config_file):
kconf = kconfiglib.Kconfig(f"{self.cwd}/Kconfig")
kconf.load_config(config_file)
symbols = list(kconf.syms.keys())
experiment = ExploreConfig()
shutil.copyfile(config_file, f"{self.cwd}/.config")
experiment(
[
"--config_hash",
self.config_hash(config_file),
"--project_version",
self.git_commit_id(),
"--project_root",
self.cwd,
"--clean_command",
self.clean_command,
"--build_command",
self.build_command,
"--attr_command",
self.attribute_command,
]
)
for symbol in kconf.syms.values():
if kconfiglib.TYPE_TO_STR[symbol.type] != "bool":
continue
if symbol.tri_value == 0 and 2 in symbol.assignable:
logger.debug(f"Set {symbol.name} to y")
symbol.set_value(2)
elif symbol.tri_value == 2 and 0 in symbol.assignable:
logger.debug(f"Set {symbol.name} to n")
symbol.set_value(0)
else:
continue
if not self.config_is_functional(kconf):
logger.debug("Configuration is non-functional")
kconf.load_config(config_file)
continue
kconf.write_config(f"{self.cwd}/.config")
experiment = ExploreConfig()
experiment(
[
"--config_hash",
self.config_hash(f"{self.cwd}/.config"),
"--project_version",
self.git_commit_id(),
"--project_root",
self.cwd,
"--clean_command",
self.clean_command,
"--build_command",
self.build_command,
"--attr_command",
self.attribute_command,
]
)
kconf.load_config(config_file)
|