From 30e015950d75a1ad3dae2d2389bf7a7c3748d9bd Mon Sep 17 00:00:00 2001 From: Mitch Phillips Date: Mon, 23 Oct 2017 20:54:01 +0000 Subject: [PATCH] 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 --- tools/llvm-cfi-verify/lib/FileAnalysis.cpp | 31 ++++++++-------- tools/llvm-cfi-verify/lib/FileAnalysis.h | 3 ++ .../tools/llvm-cfi-verify/CMakeLists.txt | 9 ++--- .../tools/llvm-cfi-verify/FileAnalysis.cpp | 23 +++++++++++- .../tools/llvm-cfi-verify/GraphBuilder.cpp | 37 ++++++++++++++++++- 5 files changed, 77 insertions(+), 26 deletions(-) diff --git a/tools/llvm-cfi-verify/lib/FileAnalysis.cpp b/tools/llvm-cfi-verify/lib/FileAnalysis.cpp index 8439a06a24d..761b2ab1037 100644 --- a/tools/llvm-cfi-verify/lib/FileAnalysis.cpp +++ b/tools/llvm-cfi-verify/lib/FileAnalysis.cpp @@ -54,7 +54,7 @@ Expected FileAnalysis::Create(StringRef Filename) { Analysis.Object = dyn_cast(Analysis.Binary.getBinary()); if (!Analysis.Object) - return make_error(); + return make_error("Failed to cast object"); Analysis.ObjectTriple = Analysis.Object->makeTriple(); Analysis.Features = Analysis.Object->getFeatures(); @@ -224,31 +224,28 @@ Error FileAnalysis::initialiseDisassemblyMembers() { ObjectTarget = TargetRegistry::lookupTarget(ArchName, ObjectTriple, ErrorString); if (!ObjectTarget) - return make_error(Twine("Couldn't find target \"") + - ObjectTriple.getTriple() + - "\", failed with error: " + ErrorString, - inconvertibleErrorCode()); + return make_error( + (Twine("Couldn't find target \"") + ObjectTriple.getTriple() + + "\", failed with error: " + ErrorString).str()); RegisterInfo.reset(ObjectTarget->createMCRegInfo(TripleName)); if (!RegisterInfo) - return make_error("Failed to initialise RegisterInfo.", - inconvertibleErrorCode()); + return make_error( + "Failed to initialise RegisterInfo."); AsmInfo.reset(ObjectTarget->createMCAsmInfo(*RegisterInfo, TripleName)); if (!AsmInfo) - return make_error("Failed to initialise AsmInfo.", - inconvertibleErrorCode()); + return make_error("Failed to initialise AsmInfo."); SubtargetInfo.reset(ObjectTarget->createMCSubtargetInfo( TripleName, MCPU, Features.getString())); if (!SubtargetInfo) - return make_error("Failed to initialise SubtargetInfo.", - inconvertibleErrorCode()); + return make_error( + "Failed to initialise SubtargetInfo."); MII.reset(ObjectTarget->createMCInstrInfo()); if (!MII) - return make_error("Failed to initialise MII.", - inconvertibleErrorCode()); + return make_error("Failed to initialise MII."); Context.reset(new MCContext(AsmInfo.get(), RegisterInfo.get(), &MOFI)); @@ -256,8 +253,8 @@ Error FileAnalysis::initialiseDisassemblyMembers() { ObjectTarget->createMCDisassembler(*SubtargetInfo, *Context)); if (!Disassembler) - return make_error("No disassembler available for target", - inconvertibleErrorCode()); + return make_error( + "No disassembler available for target"); MIA.reset(ObjectTarget->createMCInstrAnalysis(MII.get())); @@ -341,9 +338,11 @@ void FileAnalysis::addInstruction(const Instr &Instruction) { } } +UnsupportedDisassembly::UnsupportedDisassembly(StringRef Text) : Text(Text) {} + char UnsupportedDisassembly::ID; void UnsupportedDisassembly::log(raw_ostream &OS) const { - OS << "Dissassembling of non-objects not currently supported.\n"; + OS << "Could not initialise disassembler: " << Text; } std::error_code UnsupportedDisassembly::convertToErrorCode() const { diff --git a/tools/llvm-cfi-verify/lib/FileAnalysis.h b/tools/llvm-cfi-verify/lib/FileAnalysis.h index 3c24bcda75c..803eeee97e0 100644 --- a/tools/llvm-cfi-verify/lib/FileAnalysis.h +++ b/tools/llvm-cfi-verify/lib/FileAnalysis.h @@ -171,6 +171,9 @@ private: class UnsupportedDisassembly : public ErrorInfo { public: static char ID; + std::string Text; + + UnsupportedDisassembly(StringRef Text); void log(raw_ostream &OS) const override; std::error_code convertToErrorCode() const override; diff --git a/unittests/tools/llvm-cfi-verify/CMakeLists.txt b/unittests/tools/llvm-cfi-verify/CMakeLists.txt index 938b90eb83b..08830a2eaa5 100644 --- a/unittests/tools/llvm-cfi-verify/CMakeLists.txt +++ b/unittests/tools/llvm-cfi-verify/CMakeLists.txt @@ -11,9 +11,6 @@ set(LLVM_LINK_COMPONENTS Support ) -list(FIND LLVM_TARGETS_TO_BUILD "X86" x86_idx) -if (NOT x86_idx LESS 0) - add_llvm_unittest(CFIVerifyTests - FileAnalysis.cpp - GraphBuilder.cpp) -endif() +add_llvm_unittest(CFIVerifyTests + FileAnalysis.cpp + GraphBuilder.cpp) diff --git a/unittests/tools/llvm-cfi-verify/FileAnalysis.cpp b/unittests/tools/llvm-cfi-verify/FileAnalysis.cpp index 66fdc42ccfc..c94a0fc75d9 100644 --- a/unittests/tools/llvm-cfi-verify/FileAnalysis.cpp +++ b/unittests/tools/llvm-cfi-verify/FileAnalysis.cpp @@ -63,15 +63,24 @@ public: class BasicFileAnalysisTest : public ::testing::Test { protected: virtual void SetUp() { - if (Analysis.initialiseDisassemblyMembers()) { - FAIL() << "Failed to initialise FileAnalysis."; + SuccessfullyInitialised = true; + if (auto Err = Analysis.initialiseDisassemblyMembers()) { + handleAllErrors(std::move(Err), [&](const UnsupportedDisassembly &E) { + SuccessfullyInitialised = false; + outs() + << "Note: CFIVerifyTests are disabled due to lack of x86 support " + "on this build.\n"; + }); } } + bool SuccessfullyInitialised; ELFx86TestFileAnalysis Analysis; }; TEST_F(BasicFileAnalysisTest, BasicDisassemblyTraversalTest) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x90, // 0: nop @@ -180,6 +189,8 @@ TEST_F(BasicFileAnalysisTest, BasicDisassemblyTraversalTest) { } TEST_F(BasicFileAnalysisTest, PrevAndNextFromBadInst) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x90, // 0: nop @@ -201,6 +212,8 @@ TEST_F(BasicFileAnalysisTest, PrevAndNextFromBadInst) { } TEST_F(BasicFileAnalysisTest, CFITrapTest) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x90, // 0: nop @@ -234,6 +247,8 @@ TEST_F(BasicFileAnalysisTest, CFITrapTest) { } TEST_F(BasicFileAnalysisTest, FallThroughTest) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x90, // 0: nop @@ -272,6 +287,8 @@ TEST_F(BasicFileAnalysisTest, FallThroughTest) { } TEST_F(BasicFileAnalysisTest, DefiniteNextInstructionTest) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x90, // 0: nop @@ -360,6 +377,8 @@ TEST_F(BasicFileAnalysisTest, DefiniteNextInstructionTest) { } TEST_F(BasicFileAnalysisTest, ControlFlowXRefsTest) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x90, // 0: nop diff --git a/unittests/tools/llvm-cfi-verify/GraphBuilder.cpp b/unittests/tools/llvm-cfi-verify/GraphBuilder.cpp index 5cff6577700..b200677dd09 100644 --- a/unittests/tools/llvm-cfi-verify/GraphBuilder.cpp +++ b/unittests/tools/llvm-cfi-verify/GraphBuilder.cpp @@ -126,11 +126,18 @@ public: class BasicGraphBuilderTest : public ::testing::Test { protected: virtual void SetUp() { - if (Analysis.initialiseDisassemblyMembers()) { - FAIL() << "Failed to initialise FileAnalysis."; + SuccessfullyInitialised = true; + if (auto Err = Analysis.initialiseDisassemblyMembers()) { + handleAllErrors(std::move(Err), [&](const UnsupportedDisassembly &E) { + SuccessfullyInitialised = false; + outs() + << "Note: CFIVerifyTests are disabled due to lack of x86 support " + "on this build.\n"; + }); } } + bool SuccessfullyInitialised; ELFx86TestFileAnalysis Analysis; }; @@ -141,6 +148,8 @@ MATCHER_P2(HasPath, Result, Matcher, "has path " + PrintToString(Matcher)) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestSinglePathFallthroughUd2) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x75, 0x02, // 0: jne 4 [+2] @@ -165,6 +174,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestSinglePathFallthroughUd2) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestSinglePathJumpUd2) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x75, 0x02, // 0: jne 4 [+2] @@ -189,6 +200,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestSinglePathJumpUd2) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestDualPathDualUd2) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x75, 0x03, // 0: jne 5 [+3] @@ -226,6 +239,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestDualPathDualUd2) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestDualPathSingleUd2) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x75, 0x05, // 0: jne 7 [+5] @@ -262,6 +277,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestDualPathSingleUd2) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphFailures) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x90, // 0: nop @@ -282,6 +299,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphFailures) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphNoXrefs) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0xeb, 0xfe, // 0: jmp 0 [-2] @@ -295,6 +314,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphNoXrefs) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphConditionalInfiniteLoop) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x75, 0xfe, // 0: jne 0 [-2] @@ -315,6 +336,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphConditionalInfiniteLoop) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphUnconditionalInfiniteLoop) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x75, 0x02, // 0: jne 4 [+2] @@ -337,6 +360,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphUnconditionalInfiniteLoop) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphNoFlowsToIndirection) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x75, 0x00, // 0: jne 2 [+0] @@ -350,6 +375,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphNoFlowsToIndirection) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphLengthExceededUpwards) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x75, 0x06, // 0: jne 8 [+6] @@ -377,6 +404,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphLengthExceededUpwards) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphLengthExceededDownwards) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x75, 0x02, // 0: jne 4 [+2] @@ -411,6 +440,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphLengthExceededDownwards) { // paths correctly. We don't need to recalculate the flow from 0x2 -> 0x3 as it // should only need to be generated once. TEST_F(BasicGraphBuilderTest, BuildFlowGraphWithRepeatedWork) { + if (!SuccessfullyInitialised) + return; Analysis.parseSectionContents( { 0x75, 0x05, // 0: jne 7 [+5] @@ -449,6 +480,8 @@ TEST_F(BasicGraphBuilderTest, BuildFlowGraphWithRepeatedWork) { } TEST_F(BasicGraphBuilderTest, BuildFlowGraphComplexExample) { + if (!SuccessfullyInitialised) + return; // The following code has this graph: // +----------+ +--------------+ // | 20 | <--- | 0 | -- 2.40.0