aboutsummaryrefslogtreecommitdiff
path: root/backend/tools/MIRTool/MIRToolOperationsTest.cmake
blob: 097e5710d4c0ac2e37cdef6dce33fc6b98f40060 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# SPDX-License-Identifier: GPL-3.0-or-later

if(NOT DEFINED TOOL OR NOT DEFINED INPUT OR NOT DEFINED WORK_DIRECTORY)
  message(FATAL_ERROR "MIR tool operations test is missing a required path")
endif()

file(REMOVE_RECURSE "${WORK_DIRECTORY}")
file(MAKE_DIRECTORY "${WORK_DIRECTORY}")

set(CacheDirectory "${WORK_DIRECTORY}/cache")
set(CachedAssembly "${WORK_DIRECTORY}/cached.s")
set(ReloadedAssembly "${WORK_DIRECTORY}/reloaded.s")
execute_process(
  COMMAND
    "${TOOL}" --input "${INPUT}" --emit assembly --cache-dir "${CacheDirectory}"
    --cache-policy write --output "${CachedAssembly}"
  RESULT_VARIABLE CacheWriteResult
  ERROR_VARIABLE CacheWriteDiagnostics
)
if(NOT CacheWriteResult EQUAL 0)
  message(FATAL_ERROR "cache write failed: ${CacheWriteDiagnostics}")
endif()
execute_process(
  COMMAND
    "${TOOL}" --input "${INPUT}" --emit assembly --cache-dir "${CacheDirectory}"
    --cache-policy read --output "${ReloadedAssembly}"
  RESULT_VARIABLE CacheReadResult
  ERROR_VARIABLE CacheReadDiagnostics
)
if(NOT CacheReadResult EQUAL 0)
  message(FATAL_ERROR "cache hit reload failed: ${CacheReadDiagnostics}")
endif()
execute_process(
  COMMAND "${CMAKE_COMMAND}" -E compare_files "${CachedAssembly}" "${ReloadedAssembly}"
  RESULT_VARIABLE CacheComparisonResult
)
if(NOT CacheComparisonResult EQUAL 0)
  message(FATAL_ERROR "cache hit changed the artifact bytes")
endif()

execute_process(
  COMMAND
    "${TOOL}" --input "${INPUT}" --emit assembly --cache-dir
    "${WORK_DIRECTORY}/empty-cache" --cache-policy read --output "${WORK_DIRECTORY}/miss.s"
  RESULT_VARIABLE CacheMissResult
  ERROR_VARIABLE CacheMissDiagnostics
)
if(CacheMissResult EQUAL 0 OR NOT CacheMissDiagnostics MATCHES "cache miss")
  message(FATAL_ERROR "strict cache read did not report a cache miss")
endif()

file(GLOB_RECURSE CachedArtifacts "${CacheDirectory}/artifact.s")
list(LENGTH CachedArtifacts CachedArtifactCount)
if(NOT CachedArtifactCount EQUAL 1)
  message(FATAL_ERROR "cache publication did not create exactly one assembly artifact")
endif()
list(GET CachedArtifacts 0 CachedArtifact)
file(WRITE "${CachedArtifact}" "corrupt")
execute_process(
  COMMAND
    "${TOOL}" --input "${INPUT}" --emit assembly --cache-dir "${CacheDirectory}"
    --cache-policy read --output "${WORK_DIRECTORY}/corrupt.s"
  RESULT_VARIABLE CorruptCacheResult
  ERROR_VARIABLE CorruptCacheDiagnostics
)
if(CorruptCacheResult EQUAL 0 OR NOT CorruptCacheDiagnostics MATCHES "cache miss")
  message(FATAL_ERROR "strict cache reload accepted a corrupted artifact")
endif()

set(SuccessReproducer "${WORK_DIRECTORY}/success-reproducer")
set(SuccessAssembly "${WORK_DIRECTORY}/success.s")
set(ReplayedAssembly "${WORK_DIRECTORY}/success-replayed.s")
execute_process(
  COMMAND
    "${TOOL}" --input "${INPUT}" --emit assembly --crash-reproducer "${SuccessReproducer}"
    --output "${SuccessAssembly}"
  RESULT_VARIABLE ReproducerWriteResult
  ERROR_VARIABLE ReproducerWriteDiagnostics
)
if(NOT ReproducerWriteResult EQUAL 0)
  message(FATAL_ERROR "successful reproducer publication failed: ${ReproducerWriteDiagnostics}")
endif()
execute_process(
  COMMAND "${TOOL}" --reproduce "${SuccessReproducer}" --output "${ReplayedAssembly}"
  RESULT_VARIABLE ReproducerReplayResult
  ERROR_VARIABLE ReproducerReplayDiagnostics
)
if(NOT ReproducerReplayResult EQUAL 0)
  message(FATAL_ERROR "successful reproducer replay failed: ${ReproducerReplayDiagnostics}")
endif()
execute_process(
  COMMAND "${CMAKE_COMMAND}" -E compare_files "${SuccessAssembly}" "${ReplayedAssembly}"
  RESULT_VARIABLE ReproducerComparisonResult
)
if(NOT ReproducerComparisonResult EQUAL 0)
  message(FATAL_ERROR "reproducer replay changed the artifact bytes")
endif()

set(RegBankInput "${WORK_DIRECTORY}/regbank.mir")
execute_process(
  COMMAND "${TOOL}" --input "${INPUT}" --emit regbank-mir --output "${RegBankInput}"
  RESULT_VARIABLE RegBankResult
  ERROR_VARIABLE RegBankDiagnostics
)
if(NOT RegBankResult EQUAL 0)
  message(FATAL_ERROR "register-bank seed production failed: ${RegBankDiagnostics}")
endif()

set(FailureReproducer "${WORK_DIRECTORY}/failure-reproducer")
execute_process(
  COMMAND
    "${TOOL}" --input "${RegBankInput}" --run-pass InstructionSelect --emit selected-mir
    --fuel 0 --crash-reproducer "${FailureReproducer}" --output "${WORK_DIRECTORY}/failure.mir"
  RESULT_VARIABLE FailureResult
  ERROR_VARIABLE FailureDiagnostics
)
if(FailureResult EQUAL 0 OR
   NOT FailureDiagnostics MATCHES "required selection action was rejected")
  message(FATAL_ERROR "zero Action fuel did not produce the expected failure")
endif()
file(READ "${FailureReproducer}/manifest.yaml" FailureManifest)
if(NOT FailureManifest MATCHES "InstructionSelect" OR
   NOT FailureManifest MATCHES "required selection action was rejected")
  message(FATAL_ERROR "failure reproducer did not record structured pass diagnostics")
endif()
execute_process(
  COMMAND "${TOOL}" --reproduce "${FailureReproducer}" --output "${WORK_DIRECTORY}/replay.mir"
  RESULT_VARIABLE FailureReplayResult
  ERROR_VARIABLE FailureReplayDiagnostics
)
if(FailureReplayResult EQUAL 0 OR
   NOT FailureReplayDiagnostics MATCHES "required selection action was rejected")
  message(FATAL_ERROR "failure reproducer did not replay the original failure")
endif()

set(ActionOutput "${WORK_DIRECTORY}/selected.mir")
execute_process(
  COMMAND
    "${TOOL}" --input "${RegBankInput}" --run-pass InstructionSelect --emit selected-mir
    --print-actions --output "${ActionOutput}"
  RESULT_VARIABLE ActionResult
  ERROR_VARIABLE ActionDiagnostics
)
if(NOT ActionResult EQUAL 0 OR NOT ActionDiagnostics MATCHES "accepted InstructionSelect")
  message(FATAL_ERROR "successful Action execution did not report stable decisions")
endif()

file(READ "${INPUT}" ReducerSeedText)
string(REPLACE
  "    $x10 = COPY %2\n"
  "    %3:gpr(i64) = G_CONSTANT 99\n    $x10 = COPY %2\n"
  ReducerSeedText
  "${ReducerSeedText}"
)
set(ReducerSeed "${WORK_DIRECTORY}/reducer-seed.mir")
set(CanonicalReducerSeed "${WORK_DIRECTORY}/reducer-seed.canonical.mir")
set(ReducedMIR "${WORK_DIRECTORY}/reduced.mir")
file(WRITE "${ReducerSeed}" "${ReducerSeedText}")
execute_process(
  COMMAND "${TOOL}" --verify --input "${ReducerSeed}" --output "${CanonicalReducerSeed}"
  RESULT_VARIABLE ReducerSeedResult
  ERROR_VARIABLE ReducerSeedDiagnostics
)
if(NOT ReducerSeedResult EQUAL 0)
  message(FATAL_ERROR "reducer seed failed canonicalization: ${ReducerSeedDiagnostics}")
endif()
execute_process(
  COMMAND "${TOOL}" --reduce validity --input "${ReducerSeed}" --output "${ReducedMIR}"
  RESULT_VARIABLE ValidityReducerResult
  ERROR_VARIABLE ValidityReducerDiagnostics
)
if(NOT ValidityReducerResult EQUAL 0)
  message(FATAL_ERROR "validity reduction failed: ${ValidityReducerDiagnostics}")
endif()
file(SIZE "${CanonicalReducerSeed}" CanonicalReducerSeedSize)
file(SIZE "${ReducedMIR}" ReducedMIRSize)
if(NOT ReducedMIRSize LESS CanonicalReducerSeedSize)
  message(FATAL_ERROR "validity reducer did not make the canonical seed smaller")
endif()
execute_process(
  COMMAND "${TOOL}" --verify --input "${ReducedMIR}"
  RESULT_VARIABLE ReducedVerifyResult
  ERROR_VARIABLE ReducedVerifyDiagnostics
)
if(NOT ReducedVerifyResult EQUAL 0)
  message(FATAL_ERROR "validity reducer produced invalid MIR: ${ReducedVerifyDiagnostics}")
endif()

set(FailureReducedMIR "${WORK_DIRECTORY}/failure-reduced.mir")
execute_process(
  COMMAND
    "${TOOL}" --reduce failure --input "${RegBankInput}" --run-pass InstructionSelect
    --emit selected-mir --fuel 0 --output "${FailureReducedMIR}"
  RESULT_VARIABLE FailureReducerResult
  ERROR_VARIABLE FailureReducerDiagnostics
)
if(NOT FailureReducerResult EQUAL 0)
  message(FATAL_ERROR "failure reduction failed: ${FailureReducerDiagnostics}")
endif()
execute_process(
  COMMAND
    "${TOOL}" --input "${FailureReducedMIR}" --run-pass InstructionSelect --emit selected-mir
    --fuel 0 --output "${WORK_DIRECTORY}/failure-reduced-output.mir"
  RESULT_VARIABLE ReducedFailureResult
  ERROR_VARIABLE ReducedFailureDiagnostics
)
if(ReducedFailureResult EQUAL 0 OR
   NOT ReducedFailureDiagnostics MATCHES "required selection action was rejected")
  message(FATAL_ERROR "failure reducer did not preserve its interestingness condition")
endif()

set(FirstFuzz "${WORK_DIRECTORY}/fuzz-1.txt")
set(SecondFuzz "${WORK_DIRECTORY}/fuzz-2.txt")
execute_process(
  COMMAND
    "${TOOL}" --fuzz all --fuzz-iterations 2 --seed 123 --input "${INPUT}" --output
    "${FirstFuzz}"
  RESULT_VARIABLE FirstFuzzResult
  ERROR_VARIABLE FirstFuzzDiagnostics
)
if(NOT FirstFuzzResult EQUAL 0)
  message(FATAL_ERROR "layered fuzz execution failed: ${FirstFuzzDiagnostics}")
endif()
execute_process(
  COMMAND
    "${TOOL}" --fuzz all --fuzz-iterations 2 --seed 123 --input "${INPUT}" --output
    "${SecondFuzz}"
  RESULT_VARIABLE SecondFuzzResult
  ERROR_VARIABLE SecondFuzzDiagnostics
)
if(NOT SecondFuzzResult EQUAL 0)
  message(FATAL_ERROR "repeated layered fuzz execution failed: ${SecondFuzzDiagnostics}")
endif()
execute_process(
  COMMAND "${CMAKE_COMMAND}" -E compare_files "${FirstFuzz}" "${SecondFuzz}"
  RESULT_VARIABLE FuzzComparisonResult
)
if(NOT FuzzComparisonResult EQUAL 0)
  message(FATAL_ERROR "layered fuzz accounting is not deterministic")
endif()
file(READ "${FirstFuzz}" FuzzText)
if(NOT FuzzText MATCHES "seed=123 executed=11" OR
   NOT FuzzText MATCHES "parser: executed=2" OR
   NOT FuzzText MATCHES "builder: executed=2" OR
   NOT FuzzText MATCHES "property-stage: executed=2" OR
   NOT FuzzText MATCHES "mutation: executed=2" OR
   NOT FuzzText MATCHES "pipeline: executed=2" OR
   NOT FuzzText MATCHES "semantics: executed=1")
  message(FATAL_ERROR "layered fuzz output does not account for all six layers")
endif()
execute_process(
  COMMAND "${TOOL}" --fuzz parser,parser --input "${INPUT}"
  RESULT_VARIABLE DuplicateFuzzResult
  ERROR_VARIABLE DuplicateFuzzDiagnostics
)
if(DuplicateFuzzResult EQUAL 0 OR NOT DuplicateFuzzDiagnostics MATCHES "must be unique")
  message(FATAL_ERROR "CLI fuzzing accepted duplicate layers")
endif()

file(REMOVE_RECURSE "${WORK_DIRECTORY}")