llvm.org GIT mirror llvm / 30e0159
Made llvm-cfi-verify not execute unit tests on non-x86 builds. Patched out from D38427. Reviewers: vlad.tsyrklevich Reviewed By: vlad.tsyrklevich Subscribers: llvm-commits, kcc, pcc, mgorny Differential Revision: https://reviews.llvm.org/D39197 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@316375 91177308-0d34-0410-b5e6-96231b3b80d8 Mitch Phillips 1 year, 9 months ago
5 changed file(s) with 77 addition(s) and 26 deletion(s). Raw diff Collapse all Expand all
5353
5454 Analysis.Object = dyn_cast(Analysis.Binary.getBinary());
5555 if (!Analysis.Object)
56 return make_error();
56 return make_error("Failed to cast object");
5757
5858 Analysis.ObjectTriple = Analysis.Object->makeTriple();
5959 Analysis.Features = Analysis.Object->getFeatures();
223223 ObjectTarget =
224224 TargetRegistry::lookupTarget(ArchName, ObjectTriple, ErrorString);
225225 if (!ObjectTarget)
226 return make_error(Twine("Couldn't find target \"") +
227 ObjectTriple.getTriple() +
228 "\", failed with error: " + ErrorString,
229 inconvertibleErrorCode());
226 return make_error(
227 (Twine("Couldn't find target \"") + ObjectTriple.getTriple() +
228 "\", failed with error: " + ErrorString).str());
230229
231230 RegisterInfo.reset(ObjectTarget->createMCRegInfo(TripleName));
232231 if (!RegisterInfo)
233 return make_error("Failed to initialise RegisterInfo.",
234 inconvertibleErrorCode());
232 return make_error(
233 "Failed to initialise RegisterInfo.");
235234
236235 AsmInfo.reset(ObjectTarget->createMCAsmInfo(*RegisterInfo, TripleName));
237236 if (!AsmInfo)
238 return make_error("Failed to initialise AsmInfo.",
239 inconvertibleErrorCode());
237 return make_error("Failed to initialise AsmInfo.");
240238
241239 SubtargetInfo.reset(ObjectTarget->createMCSubtargetInfo(
242240 TripleName, MCPU, Features.getString()));
243241 if (!SubtargetInfo)
244 return make_error("Failed to initialise SubtargetInfo.",
245 inconvertibleErrorCode());
242 return make_error(
243 "Failed to initialise SubtargetInfo.");
246244
247245 MII.reset(ObjectTarget->createMCInstrInfo());
248246 if (!MII)
249 return make_error("Failed to initialise MII.",
250 inconvertibleErrorCode());
247 return make_error("Failed to initialise MII.");
251248
252249 Context.reset(new MCContext(AsmInfo.get(), RegisterInfo.get(), &MOFI));
253250
255252 ObjectTarget->createMCDisassembler(*SubtargetInfo, *Context));
256253
257254 if (!Disassembler)
258 return make_error("No disassembler available for target",
259 inconvertibleErrorCode());
255 return make_error(
256 "No disassembler available for target");
260257
261258 MIA.reset(ObjectTarget->createMCInstrAnalysis(MII.get()));
262259
340337 }
341338 }
342339
340 UnsupportedDisassembly::UnsupportedDisassembly(StringRef Text) : Text(Text) {}
341
343342 char UnsupportedDisassembly::ID;
344343 void UnsupportedDisassembly::log(raw_ostream &OS) const {
345 OS << "Dissassembling of non-objects not currently supported.\n";
344 OS << "Could not initialise disassembler: " << Text;
346345 }
347346
348347 std::error_code UnsupportedDisassembly::convertToErrorCode() const {
170170 class UnsupportedDisassembly : public ErrorInfo {
171171 public:
172172 static char ID;
173 std::string Text;
174
175 UnsupportedDisassembly(StringRef Text);
173176
174177 void log(raw_ostream &OS) const override;
175178 std::error_code convertToErrorCode() const override;
1010 Support
1111 )
1212
13 list(FIND LLVM_TARGETS_TO_BUILD "X86" x86_idx)
14 if (NOT x86_idx LESS 0)
15 add_llvm_unittest(CFIVerifyTests
16 FileAnalysis.cpp
17 GraphBuilder.cpp)
18 endif()
13 add_llvm_unittest(CFIVerifyTests
14 FileAnalysis.cpp
15 GraphBuilder.cpp)
6262 class BasicFileAnalysisTest : public ::testing::Test {
6363 protected:
6464 virtual void SetUp() {
65 if (Analysis.initialiseDisassemblyMembers()) {
66 FAIL() << "Failed to initialise FileAnalysis.";
65 SuccessfullyInitialised = true;
66 if (auto Err = Analysis.initialiseDisassemblyMembers()) {
67 handleAllErrors(std::move(Err), [&](const UnsupportedDisassembly &E) {
68 SuccessfullyInitialised = false;
69 outs()
70 << "Note: CFIVerifyTests are disabled due to lack of x86 support "
71 "on this build.\n";
72 });
6773 }
6874 }
6975
76 bool SuccessfullyInitialised;
7077 ELFx86TestFileAnalysis Analysis;
7178 };
7279
7380 TEST_F(BasicFileAnalysisTest, BasicDisassemblyTraversalTest) {
81 if (!SuccessfullyInitialised)
82 return;
7483 Analysis.parseSectionContents(
7584 {
7685 0x90, // 0: nop
179188 }
180189
181190 TEST_F(BasicFileAnalysisTest, PrevAndNextFromBadInst) {
191 if (!SuccessfullyInitialised)
192 return;
182193 Analysis.parseSectionContents(
183194 {
184195 0x90, // 0: nop
200211 }
201212
202213 TEST_F(BasicFileAnalysisTest, CFITrapTest) {
214 if (!SuccessfullyInitialised)
215 return;
203216 Analysis.parseSectionContents(
204217 {
205218 0x90, // 0: nop
233246 }
234247
235248 TEST_F(BasicFileAnalysisTest, FallThroughTest) {
249 if (!SuccessfullyInitialised)
250 return;
236251 Analysis.parseSectionContents(
237252 {
238253 0x90, // 0: nop
271286 }
272287
273288 TEST_F(BasicFileAnalysisTest, DefiniteNextInstructionTest) {
289 if (!SuccessfullyInitialised)
290 return;
274291 Analysis.parseSectionContents(
275292 {
276293 0x90, // 0: nop
359376 }
360377
361378 TEST_F(BasicFileAnalysisTest, ControlFlowXRefsTest) {
379 if (!SuccessfullyInitialised)
380 return;
362381 Analysis.parseSectionContents(
363382 {
364383 0x90, // 0: nop
125125 class BasicGraphBuilderTest : public ::testing::Test {
126126 protected:
127127 virtual void SetUp() {
128 if (Analysis.initialiseDisassemblyMembers()) {
129 FAIL() << "Failed to initialise FileAnalysis.";
128 SuccessfullyInitialised = true;
129 if (auto Err = Analysis.initialiseDisassemblyMembers()) {
130 handleAllErrors(std::move(Err), [&](const UnsupportedDisassembly &E) {
131 SuccessfullyInitialised = false;
132 outs()
133 << "Note: CFIVerifyTests are disabled due to lack of x86 support "
134 "on this build.\n";
135 });
130136 }
131137 }
132138
139 bool SuccessfullyInitialised;
133140 ELFx86TestFileAnalysis Analysis;
134141 };
135142
140147 }
141148
142149 TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestSinglePathFallthroughUd2) {
150 if (!SuccessfullyInitialised)
151 return;
143152 Analysis.parseSectionContents(
144153 {
145154 0x75, 0x02, // 0: jne 4 [+2]
164173 }
165174
166175 TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestSinglePathJumpUd2) {
176 if (!SuccessfullyInitialised)
177 return;
167178 Analysis.parseSectionContents(
168179 {
169180 0x75, 0x02, // 0: jne 4 [+2]
188199 }
189200
190201 TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestDualPathDualUd2) {
202 if (!SuccessfullyInitialised)
203 return;
191204 Analysis.parseSectionContents(
192205 {
193206 0x75, 0x03, // 0: jne 5 [+3]
225238 }
226239
227240 TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestDualPathSingleUd2) {
241 if (!SuccessfullyInitialised)
242 return;
228243 Analysis.parseSectionContents(
229244 {
230245 0x75, 0x05, // 0: jne 7 [+5]
261276 }
262277
263278 TEST_F(BasicGraphBuilderTest, BuildFlowGraphFailures) {
279 if (!SuccessfullyInitialised)
280 return;
264281 Analysis.parseSectionContents(
265282 {
266283 0x90, // 0: nop
281298 }
282299
283300 TEST_F(BasicGraphBuilderTest, BuildFlowGraphNoXrefs) {
301 if (!SuccessfullyInitialised)
302 return;
284303 Analysis.parseSectionContents(
285304 {
286305 0xeb, 0xfe, // 0: jmp 0 [-2]
294313 }
295314
296315 TEST_F(BasicGraphBuilderTest, BuildFlowGraphConditionalInfiniteLoop) {
316 if (!SuccessfullyInitialised)
317 return;
297318 Analysis.parseSectionContents(
298319 {
299320 0x75, 0xfe, // 0: jne 0 [-2]
314335 }
315336
316337 TEST_F(BasicGraphBuilderTest, BuildFlowGraphUnconditionalInfiniteLoop) {
338 if (!SuccessfullyInitialised)
339 return;
317340 Analysis.parseSectionContents(
318341 {
319342 0x75, 0x02, // 0: jne 4 [+2]
336359 }
337360
338361 TEST_F(BasicGraphBuilderTest, BuildFlowGraphNoFlowsToIndirection) {
362 if (!SuccessfullyInitialised)
363 return;
339364 Analysis.parseSectionContents(
340365 {
341366 0x75, 0x00, // 0: jne 2 [+0]
349374 }
350375
351376 TEST_F(BasicGraphBuilderTest, BuildFlowGraphLengthExceededUpwards) {
377 if (!SuccessfullyInitialised)
378 return;
352379 Analysis.parseSectionContents(
353380 {
354381 0x75, 0x06, // 0: jne 8 [+6]
376403 }
377404
378405 TEST_F(BasicGraphBuilderTest, BuildFlowGraphLengthExceededDownwards) {
406 if (!SuccessfullyInitialised)
407 return;
379408 Analysis.parseSectionContents(
380409 {
381410 0x75, 0x02, // 0: jne 4 [+2]
410439 // paths correctly. We don't need to recalculate the flow from 0x2 -> 0x3 as it
411440 // should only need to be generated once.
412441 TEST_F(BasicGraphBuilderTest, BuildFlowGraphWithRepeatedWork) {
442 if (!SuccessfullyInitialised)
443 return;
413444 Analysis.parseSectionContents(
414445 {
415446 0x75, 0x05, // 0: jne 7 [+5]
448479 }
449480
450481 TEST_F(BasicGraphBuilderTest, BuildFlowGraphComplexExample) {
482 if (!SuccessfullyInitialised)
483 return;
451484 // The following code has this graph:
452485 // +----------+ +--------------+
453486 // | 20 | <--- | 0 |