Index: tools/llvm-cfi-verify/CMakeLists.txt =================================================================== --- tools/llvm-cfi-verify/CMakeLists.txt +++ tools/llvm-cfi-verify/CMakeLists.txt @@ -13,4 +13,5 @@ add_llvm_tool(llvm-cfi-verify llvm-cfi-verify.cpp + FileVerifier.cpp ) Index: tools/llvm-cfi-verify/FileVerifier.h =================================================================== --- /dev/null +++ tools/llvm-cfi-verify/FileVerifier.h @@ -0,0 +1,177 @@ +//===- FileVerifier.h -------------------------------------------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_CFI_VERIFY_FILE_VERIFIER_H +#define LLVM_CFI_VERIFY_FILE_VERIFIER_H + +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCContext.h" +#include "llvm/MC/MCDisassembler/MCDisassembler.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCInstrAnalysis.h" +#include "llvm/MC/MCInstrDesc.h" +#include "llvm/MC/MCInstrInfo.h" +#include "llvm/MC/MCObjectFileInfo.h" +#include "llvm/MC/MCRegisterInfo.h" +#include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/Object/Binary.h" +#include "llvm/Object/COFF.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/raw_ostream.h" + +#include +#include +#include +#include + +namespace llvm { +namespace cfi_verify { + +// This class is used to verify CFI instrumentation of a single file. +class FileVerifier { +public: + // A metadata struct for an instruction. + struct Instr { + uint64_t VMAddress; // Virtual memory address of this instruction. + MCInst Instruction; // Instruction. + uint64_t InstructionSize; // Size of this instruction. + std::string SectionName; // Section this instruction is found in. + bool Bad; // Is this instruction bad (in which case, Instr::Instruction is + // invalid). + }; + + // Construct a FileVerifier from a file path. + static Expected Create(StringRef Filename); + + // Construct and take ownership of the supplied object. Do not use this + // constructor, prefer to use FileVerifier::Create instead. + FileVerifier(object::OwningBinary Binary); + FileVerifier() = delete; + FileVerifier(const FileVerifier &) = delete; + FileVerifier(FileVerifier &&Other) = default; + + Error printIndirectCFInstructions() const; + + // Returns the instruction at the provided address. Returns nullptr if there + // is no instruction at the provided address. + const Instr *getInstruction(uint64_t Address) const; + + // Returns the instruction at the provided adress, dying if the instruction is + // not found. + const Instr &getInstructionOrDie(uint64_t Address) const; + + // Returns a pointer to the previous/next instruction in sequence, + // respectively. Retusn nullptr if the next/prev instruction doesn't exist, or + // if the provided instruction doesn't exist. + const Instr *getPrevInstructionSequential(const Instr &InstrMeta) const; + const Instr *getNextInstructionSequential(const Instr &InstrMeta) const; + + // Returns the list of indirect instructions. + const std::vector &getIndirectInstructions() const; + + // Returns whether this instruction is an undefined instruction (used in + // branch-to-undefined CFI protections). + bool isUndefinedInstruction(const Instr &InstrMeta) const; + + // Returns whether this function can fall through to the next instruction. + // Undefined (and bad) instructions cannot fall through, and instruction that + // modify the control flow can only flow through if they are conditional + // branches or calls. + bool canFallThrough(const Instr &InstrMeta) const; + + // Returns the definitive next instruction. This is different from the next + // instruction sequantially as it will follow unconditional branches (assuming + // they can be resolved at compile time, i.e. not indirect). This instruction + // returns nullptr if the provided instruction does not transfer control flow + // to exactly one instruction that is known deterministically at compile time. + // Also returns nullptr if the deterministic target does not exist in this + // file. + const Instr *getDefiniteNextInstruction(const Instr &InstrMeta) const; + + // Get a list of deterministic control flows that lead to the provided + // instruction. This list includes all static control flow cross-references as + // well as the previous instruction if it can fall through. + std::set getControlFlowXRefs(const Instr &InstrMeta) const; + +protected: + // Construct a blank object with the provided triple and features. Used in + // testing, where a sub class will dependency inject protected methods to + // allow analysis of raw binary, without requiring a fully valid ELF file. + FileVerifier(const Triple &ObjectTriple, const SubtargetFeatures &Features); + + // Add an instruction to this object. + void addInstruction(const Instr &Instruction); + + // Disassemble and parse the provided bytes into this object. Instruction + // address calculation is done relative to the provided SectionAddress, and + // instruction metadata is populated with the given section name. + void parseSectionContents(ArrayRef SectionBytes, + uint64_t SectionAddress, StringRef SectionName); + + // Constructs and initialises members required for disassembly. + Error initialiseDisassemblyMembers(); + + // Parses code sections from the internal object file. Saves them into the + // internal members. Should only be called once by Create(). + Error parseCodeSections(); + +private: + // Members that describe the input file. + object::OwningBinary Binary; + const object::ObjectFile *Object = nullptr; + Triple ObjectTriple; + std::string ArchName; + std::string MCPU; + const Target *ObjectTarget = nullptr; + SubtargetFeatures Features; + + // Members required for disassembly. + std::unique_ptr RegisterInfo; + std::unique_ptr AsmInfo; + std::unique_ptr SubtargetInfo; + std::unique_ptr MII; + MCObjectFileInfo MOFI; + std::unique_ptr Context; + std::unique_ptr Disassembler; + std::unique_ptr MIA; + std::unique_ptr Printer; + + // A mapping between the virtual memory address to the instruction metadata + // struct. + std::map Instructions; + + // Contains a mapping between a specific address, and a list of instructions + // that use this address as a branch target (including call instructions). + std::unordered_map> StaticBranchTargetings; + + // A list of addresses of indirect control flow instructions. + std::set IndirectInstructions; +}; + +class UnsupportedDisassembly : public ErrorInfo { +public: + static char ID; + + void log(raw_ostream &OS) const override; + std::error_code convertToErrorCode() const override; +}; + +} // namespace cfi_verify +} // namespace llvm + +#endif // LLVM_CFI_VERIFY_FILE_VERIFIER_H Index: tools/llvm-cfi-verify/FileVerifier.cpp =================================================================== --- /dev/null +++ tools/llvm-cfi-verify/FileVerifier.cpp @@ -0,0 +1,366 @@ +#include "FileVerifier.h" + +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCContext.h" +#include "llvm/MC/MCDisassembler/MCDisassembler.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCInstrAnalysis.h" +#include "llvm/MC/MCInstrDesc.h" +#include "llvm/MC/MCInstrInfo.h" +#include "llvm/MC/MCObjectFileInfo.h" +#include "llvm/MC/MCRegisterInfo.h" +#include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/Object/Binary.h" +#include "llvm/Object/COFF.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/FormatVariadic.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/raw_ostream.h" + +#include + +using Instr = llvm::cfi_verify::FileVerifier::Instr; + +namespace llvm { +namespace cfi_verify { + +Expected FileVerifier::Create(StringRef Filename) { + // Open the filename provided. + Expected> BinaryOrErr = + object::createBinary(Filename); + if (!BinaryOrErr) + return BinaryOrErr.takeError(); + + // Construct the object and allow it to take ownership of the binary. + object::OwningBinary Binary = std::move(BinaryOrErr.get()); + FileVerifier Verifier(std::move(Binary)); + + Verifier.Object = dyn_cast(Verifier.Binary.getBinary()); + if (!Verifier.Object) + return make_error(); + + Verifier.ObjectTriple = Verifier.Object->makeTriple(); + Verifier.Features = Verifier.Object->getFeatures(); + + // Init the rest of the object. + auto InitResponse = Verifier.initialiseDisassemblyMembers(); + if (InitResponse) + return std::move(InitResponse); + + auto SectionParseResponse = Verifier.parseCodeSections(); + if (SectionParseResponse) + return std::move(SectionParseResponse); + + return std::move(Verifier); +} + +FileVerifier::FileVerifier(object::OwningBinary Binary) + : Binary(std::move(Binary)) {} + +FileVerifier::FileVerifier(const Triple &ObjectTriple, + const SubtargetFeatures &Features) + : ObjectTriple(ObjectTriple), Features(Features) {} + +Error FileVerifier::printIndirectCFInstructions() const { + for (const auto &Address : IndirectInstructions) { + const auto &InstructionKV = Instructions.find(Address); + if (InstructionKV == Instructions.end()) + return make_error( + formatv("No instruction found at address {0:x}", Address), + inconvertibleErrorCode()); + + const auto &InstrMeta = InstructionKV->second; + outs() << format_hex(Address, 2) << " [" << InstrMeta.SectionName + << "] = " << MII->getName(InstrMeta.Instruction.getOpcode()) << " "; + InstrMeta.Instruction.print(outs()); + outs() << "\n"; + } + + return Error::success(); +} + +const Instr * +FileVerifier::getPrevInstructionSequential(const Instr &InstrMeta) const { + std::map::const_iterator KV = + Instructions.find(InstrMeta.VMAddress); + if (KV == Instructions.end() || KV == Instructions.begin()) + return nullptr; + + if ((--KV)->second.Bad) + return nullptr; + + return &KV->second; +} + +const Instr * +FileVerifier::getNextInstructionSequential(const Instr &InstrMeta) const { + std::map::const_iterator KV = + Instructions.find(InstrMeta.VMAddress); + + if (KV++ == Instructions.end() || KV == Instructions.end()) + return nullptr; + + if (KV->second.Bad) + return nullptr; + + return &KV->second; +} + +const Instr *FileVerifier::getInstruction(uint64_t Address) const { + const auto &InstrKV = Instructions.find(Address); + if (InstrKV == Instructions.end()) + return nullptr; + + return &InstrKV->second; +} + +const Instr &FileVerifier::getInstructionOrDie(uint64_t Address) const { + const auto &InstrKV = Instructions.find(Address); + assert(InstrKV != Instructions.end() && "Address doesn't exist."); + return InstrKV->second; +} + +bool FileVerifier::isUndefinedInstruction(const Instr &InstrMeta) const { + return MII->getName(InstrMeta.Instruction.getOpcode()) == "TRAP"; +} + +bool FileVerifier::canFallThrough(const Instr &InstrMeta) const { + if (InstrMeta.Bad) + return false; + + if (isUndefinedInstruction(InstrMeta)) + return false; + + const auto &InstrDesc = MII->get(InstrMeta.Instruction.getOpcode()); + if (InstrDesc.mayAffectControlFlow(InstrMeta.Instruction, *RegisterInfo)) + return InstrDesc.isConditionalBranch(); + + return true; +} + +const Instr * +FileVerifier::getDefiniteNextInstruction(const Instr &InstrMeta) const { + if (InstrMeta.Bad) + return nullptr; + + if (isUndefinedInstruction(InstrMeta)) + return nullptr; + + const auto &InstrDesc = MII->get(InstrMeta.Instruction.getOpcode()); + if (InstrDesc.mayAffectControlFlow(InstrMeta.Instruction, *RegisterInfo)) { + if (InstrDesc.isConditionalBranch()) + return nullptr; + + uint64_t Target; + if (!MIA->evaluateBranch(InstrMeta.Instruction, InstrMeta.VMAddress, + InstrMeta.InstructionSize, Target)) + return nullptr; + + const auto &NextKV = Instructions.find(Target); + if (NextKV == Instructions.end()) + return nullptr; + + return &NextKV->second; + } + + const auto &NextKV = + Instructions.find(InstrMeta.VMAddress + InstrMeta.InstructionSize); + if (NextKV == Instructions.end()) + return nullptr; + + if (NextKV->second.Bad) + return nullptr; + + return &NextKV->second; +} + +std::set +FileVerifier::getControlFlowXRefs(const Instr &InstrMeta) const { + std::set CFCrossReferences; + const Instr *PrevInstruction = getPrevInstructionSequential(InstrMeta); + + if (PrevInstruction && canFallThrough(*PrevInstruction)) + CFCrossReferences.insert(PrevInstruction); + + const auto &TargetRefsKV = StaticBranchTargetings.find(InstrMeta.VMAddress); + if (TargetRefsKV == StaticBranchTargetings.end()) + return CFCrossReferences; + + for (uint64_t SourceInstrAddress : TargetRefsKV->second) { + const auto &SourceInstrKV = Instructions.find(SourceInstrAddress); + if (SourceInstrKV == Instructions.end()) { + errs() << "Failed to find source instruction at address " + << format_hex(SourceInstrAddress, 2) + << " for the cross-reference to instruction at address " + << format_hex(InstrMeta.VMAddress, 2) << ".\n"; + continue; + } + + CFCrossReferences.insert(&SourceInstrKV->second); + } + + return CFCrossReferences; +} + +Error FileVerifier::initialiseDisassemblyMembers() { + std::string TripleName = ObjectTriple.getTriple(); + ArchName = ""; + MCPU = ""; + + std::string ErrorString; + + ObjectTarget = + TargetRegistry::lookupTarget(ArchName, ObjectTriple, ErrorString); + + if (!ObjectTarget) + return make_error(Twine("Couldn't find target \"") + + ObjectTriple.getTriple() + + "\", failed with error: " + ErrorString, + inconvertibleErrorCode()); + + RegisterInfo.reset(ObjectTarget->createMCRegInfo(TripleName)); + if (!RegisterInfo) + return make_error("Failed to initialise RegisterInfo.", + inconvertibleErrorCode()); + + AsmInfo.reset(ObjectTarget->createMCAsmInfo(*RegisterInfo, TripleName)); + if (!AsmInfo) + return make_error("Failed to initialise AsmInfo.", + inconvertibleErrorCode()); + + SubtargetInfo.reset(ObjectTarget->createMCSubtargetInfo( + TripleName, MCPU, Features.getString())); + if (!SubtargetInfo) + return make_error("Failed to initialise SubtargetInfo.", + inconvertibleErrorCode()); + + MII.reset(ObjectTarget->createMCInstrInfo()); + if (!MII) + return make_error("Failed to initialise MII.", + inconvertibleErrorCode()); + + Context.reset(new MCContext(AsmInfo.get(), RegisterInfo.get(), &MOFI)); + + Disassembler.reset( + ObjectTarget->createMCDisassembler(*SubtargetInfo, *Context)); + + if (!Disassembler) + return make_error("No disassembler available for target", + inconvertibleErrorCode()); + + MIA.reset(ObjectTarget->createMCInstrAnalysis(MII.get())); + + Printer.reset(ObjectTarget->createMCInstPrinter( + ObjectTriple, AsmInfo->getAssemblerDialect(), *AsmInfo, *MII, + *RegisterInfo)); + + return Error::success(); +} + +Error FileVerifier::parseCodeSections() { + for (const object::SectionRef &Section : Object->sections()) { + // Ensure only executable sections get analysed. + if (!(object::ELFSectionRef(Section).getFlags() & ELF::SHF_EXECINSTR)) + continue; + + StringRef SectionContents; + if (Section.getContents(SectionContents)) + return make_error("Failed to retrieve section contents", + inconvertibleErrorCode()); + + StringRef SectionName; + if (Section.getName(SectionName)) + SectionName = "UNKNOWN"; + + ArrayRef SectionBytes((const uint8_t *)SectionContents.data(), + Section.getSize()); + parseSectionContents(SectionBytes, Section.getAddress(), SectionName); + } + return Error::success(); +} + +void FileVerifier::parseSectionContents(ArrayRef SectionBytes, + uint64_t SectionAddress, + StringRef SectionName) { + MCInst Instruction; + Instr InstrMeta; + uint64_t InstructionSize; + + for (uint64_t Byte = 0; Byte < SectionBytes.size();) { + bool BadInstruction = false; + + SectionBytes.drop_front(Byte); + + // Disassemble the instruction. + if (Disassembler->getInstruction(Instruction, InstructionSize, + SectionBytes.drop_front(Byte), 0, nulls(), + outs()) != MCDisassembler::Success) + BadInstruction = true; + + Byte += InstructionSize; + + uint64_t VMAddress = SectionAddress + Byte - InstructionSize; + InstrMeta.Instruction = Instruction; + InstrMeta.VMAddress = VMAddress; + InstrMeta.InstructionSize = InstructionSize; + InstrMeta.SectionName = SectionName; + InstrMeta.Bad = false; + + if (BadInstruction) { + InstrMeta.Bad = true; + addInstruction(InstrMeta); + continue; + } + + addInstruction(InstrMeta); + + // Skip additional parsing for instructions that do not affect the control + // flow. + const auto &InstrDesc = MII->get(Instruction.getOpcode()); + if (!InstrDesc.mayAffectControlFlow(Instruction, *RegisterInfo)) + continue; + + uint64_t Target; + if (MIA->evaluateBranch(Instruction, VMAddress, InstructionSize, Target)) { + // If the target can be evaluated, it's not indirect. + StaticBranchTargetings[Target].push_back(VMAddress); + continue; + } + + // Skip instructions that do not operate on register operands. + bool UsesRegisterOperand = false; + for (const auto &Operand : Instruction) { + if (Operand.isReg()) + UsesRegisterOperand = true; + } + + if (!UsesRegisterOperand) + continue; + + IndirectInstructions.insert(VMAddress); + } +} + +void FileVerifier::addInstruction(const Instr &Instruction) { + Instructions[Instruction.VMAddress] = Instruction; +} + +char UnsupportedDisassembly::ID; +void UnsupportedDisassembly::log(raw_ostream &OS) const { + OS << "Dissassembling of non-objects not currently supported.\n"; +} + +std::error_code UnsupportedDisassembly::convertToErrorCode() const { + return std::error_code(); +} + +} // namespace cfi_verify +} // namespace llvm Index: tools/llvm-cfi-verify/llvm-cfi-verify.cpp =================================================================== --- tools/llvm-cfi-verify/llvm-cfi-verify.cpp +++ tools/llvm-cfi-verify/llvm-cfi-verify.cpp @@ -17,67 +17,22 @@ // //===----------------------------------------------------------------------===// -#include "llvm/MC/MCAsmInfo.h" -#include "llvm/MC/MCContext.h" -#include "llvm/MC/MCDisassembler/MCDisassembler.h" -#include "llvm/MC/MCInst.h" -#include "llvm/MC/MCInstPrinter.h" -#include "llvm/MC/MCInstrAnalysis.h" -#include "llvm/MC/MCInstrDesc.h" -#include "llvm/MC/MCInstrInfo.h" -#include "llvm/MC/MCObjectFileInfo.h" -#include "llvm/MC/MCRegisterInfo.h" -#include "llvm/MC/MCSubtargetInfo.h" -#include "llvm/Object/Binary.h" -#include "llvm/Object/COFF.h" -#include "llvm/Object/ObjectFile.h" -#include "llvm/Support/Casting.h" +#include "FileVerifier.h" + +#include "llvm/BinaryFormat/ELF.h" #include "llvm/Support/CommandLine.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/TargetRegistry.h" -#include "llvm/Support/TargetSelect.h" -#include "llvm/Support/raw_ostream.h" +#include "llvm/Support/Error.h" -#include #include using namespace llvm; using namespace llvm::object; +using namespace llvm::cfi_verify; -cl::opt ArgDumpSymbols("sym", cl::desc("Dump the symbol table.")); cl::opt InputFilename(cl::Positional, cl::desc(""), cl::Required); -static void printSymbols(const ObjectFile *Object) { - for (const SymbolRef &Symbol : Object->symbols()) { - outs() << "Symbol [" << format_hex_no_prefix(Symbol.getValue(), 2) - << "] = "; - - auto SymbolName = Symbol.getName(); - if (SymbolName) - outs() << *SymbolName; - else - outs() << "UNKNOWN"; - - if (Symbol.getFlags() & SymbolRef::SF_Hidden) - outs() << " .hidden"; - - outs() << " (Section = "; - - auto SymbolSection = Symbol.getSection(); - if (SymbolSection) { - StringRef SymbolSectionName; - if ((*SymbolSection)->getName(SymbolSectionName)) - outs() << "UNKNOWN)"; - else - outs() << SymbolSectionName << ")"; - } else { - outs() << "N/A)"; - } - - outs() << "\n"; - } -} +ExitOnError ExitOnErr; int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv); @@ -87,155 +42,8 @@ InitializeAllAsmParsers(); InitializeAllDisassemblers(); - Expected> BinaryOrErr = createBinary(InputFilename); - if (!BinaryOrErr) { - errs() << "Failed to open file.\n"; - return EXIT_FAILURE; - } - - Binary &Binary = *BinaryOrErr.get().getBinary(); - ObjectFile *Object = dyn_cast(&Binary); - if (!Object) { - errs() << "Disassembling of non-objects not currently supported.\n"; - return EXIT_FAILURE; - } - - Triple TheTriple = Object->makeTriple(); - std::string TripleName = TheTriple.getTriple(); - std::string ArchName = ""; - std::string ErrorString; - - const Target *TheTarget = - TargetRegistry::lookupTarget(ArchName, TheTriple, ErrorString); - - if (!TheTarget) { - errs() << "Couldn't find target \"" << TheTriple.getTriple() - << "\", failed with error: " << ErrorString << ".\n"; - return EXIT_FAILURE; - } - - SubtargetFeatures Features = Object->getFeatures(); - - std::unique_ptr RegisterInfo( - TheTarget->createMCRegInfo(TripleName)); - if (!RegisterInfo) { - errs() << "Failed to initialise RegisterInfo.\n"; - return EXIT_FAILURE; - } - - std::unique_ptr AsmInfo( - TheTarget->createMCAsmInfo(*RegisterInfo, TripleName)); - if (!AsmInfo) { - errs() << "Failed to initialise AsmInfo.\n"; - return EXIT_FAILURE; - } - - std::string MCPU = ""; - std::unique_ptr SubtargetInfo( - TheTarget->createMCSubtargetInfo(TripleName, MCPU, Features.getString())); - if (!SubtargetInfo) { - errs() << "Failed to initialise SubtargetInfo.\n"; - return EXIT_FAILURE; - } - - std::unique_ptr MII(TheTarget->createMCInstrInfo()); - if (!MII) { - errs() << "Failed to initialise MII.\n"; - return EXIT_FAILURE; - } - - MCObjectFileInfo MOFI; - MCContext Context(AsmInfo.get(), RegisterInfo.get(), &MOFI); - - std::unique_ptr Disassembler( - TheTarget->createMCDisassembler(*SubtargetInfo, Context)); - - if (!Disassembler) { - errs() << "No disassembler available for target."; - return EXIT_FAILURE; - } - - std::unique_ptr MIA( - TheTarget->createMCInstrAnalysis(MII.get())); - - std::unique_ptr Printer( - TheTarget->createMCInstPrinter(TheTriple, AsmInfo->getAssemblerDialect(), - *AsmInfo, *MII, *RegisterInfo)); - - if (ArgDumpSymbols) - printSymbols(Object); - - for (const SectionRef &Section : Object->sections()) { - outs() << "Section [" << format_hex_no_prefix(Section.getAddress(), 2) - << "] = "; - StringRef SectionName; - - if (Section.getName(SectionName)) - outs() << "UNKNOWN.\n"; - else - outs() << SectionName << "\n"; - - StringRef SectionContents; - if (Section.getContents(SectionContents)) { - errs() << "Failed to retrieve section contents.\n"; - return EXIT_FAILURE; - } - - MCInst Instruction; - uint64_t InstructionSize; - - ArrayRef SectionBytes((const uint8_t *)SectionContents.data(), - Section.getSize()); - - for (uint64_t Byte = 0; Byte < Section.getSize();) { - bool BadInstruction = false; - - // Disassemble the instruction. - if (Disassembler->getInstruction( - Instruction, InstructionSize, SectionBytes.drop_front(Byte), 0, - nulls(), outs()) != MCDisassembler::Success) { - BadInstruction = true; - } - - Byte += InstructionSize; - - if (BadInstruction) - continue; - - // Skip instructions that do not affect the control flow. - const auto &InstrDesc = MII->get(Instruction.getOpcode()); - if (!InstrDesc.mayAffectControlFlow(Instruction, *RegisterInfo)) - continue; - - // Skip instructions that do not operate on register operands. - bool UsesRegisterOperand = false; - for (const auto &Operand : Instruction) { - if (Operand.isReg()) - UsesRegisterOperand = true; - } - - if (!UsesRegisterOperand) - continue; - - // Print the instruction address. - outs() << " " - << format_hex(Section.getAddress() + Byte - InstructionSize, 2) - << ": "; - - // Print the instruction bytes. - for (uint64_t i = 0; i < InstructionSize; ++i) { - outs() << format_hex_no_prefix(SectionBytes[Byte - InstructionSize + i], - 2) - << " "; - } - - // Print the instruction. - outs() << " | " << MII->getName(Instruction.getOpcode()) << " "; - Instruction.dump_pretty(outs(), Printer.get()); - - outs() << "\n"; - } - } + FileVerifier Verifier = ExitOnErr(FileVerifier::Create(InputFilename)); + ExitOnErr(Verifier.printIndirectCFInstructions()); return EXIT_SUCCESS; } Index: tools/llvm-cfi-verify/unittests/CMakeLists.txt =================================================================== --- /dev/null +++ tools/llvm-cfi-verify/unittests/CMakeLists.txt @@ -0,0 +1,17 @@ +set(LLVM_LINK_COMPONENTS + AllTargetsAsmPrinters + AllTargetsAsmParsers + AllTargetsDescs + AllTargetsDisassemblers + AllTargetsInfos + MC + Object + MCParser + Object + Support + ) + + + +add_llvm_unittest(CFIVerifyTests + FileVerifier.cpp ../FileVerifier.cpp) Index: tools/llvm-cfi-verify/unittests/FileVerifier.cpp =================================================================== --- /dev/null +++ tools/llvm-cfi-verify/unittests/FileVerifier.cpp @@ -0,0 +1,491 @@ +//===- llvm/tools/llvm-cfi-verify/unittests/FileVerifier.cpp --------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "../FileVerifier.h" +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include "llvm/BinaryFormat/ELF.h" +#include "llvm/MC/MCAsmInfo.h" +#include "llvm/MC/MCContext.h" +#include "llvm/MC/MCDisassembler/MCDisassembler.h" +#include "llvm/MC/MCInst.h" +#include "llvm/MC/MCInstPrinter.h" +#include "llvm/MC/MCInstrAnalysis.h" +#include "llvm/MC/MCInstrDesc.h" +#include "llvm/MC/MCInstrInfo.h" +#include "llvm/MC/MCObjectFileInfo.h" +#include "llvm/MC/MCRegisterInfo.h" +#include "llvm/MC/MCSubtargetInfo.h" +#include "llvm/Object/Binary.h" +#include "llvm/Object/COFF.h" +#include "llvm/Object/ELFObjectFile.h" +#include "llvm/Object/ObjectFile.h" +#include "llvm/Support/Casting.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/TargetRegistry.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Support/raw_ostream.h" + +#include + +using Instr = ::llvm::cfi_verify::FileVerifier::Instr; +using ::testing::Eq; +using ::testing::Field; + +namespace llvm { +namespace cfi_verify { +namespace { +class ELFx86TestFileVerifier : public FileVerifier { +public: + ELFx86TestFileVerifier() + : FileVerifier(Triple("x86_64--"), SubtargetFeatures()) {} + + // Expose this method publicly for testing. + void parseSectionContents(ArrayRef SectionBytes, + uint64_t SectionAddress, StringRef SectionName) { + FileVerifier::parseSectionContents(SectionBytes, SectionAddress, + SectionName); + } + + Error initialiseDisassemblyMembers() { + return FileVerifier::initialiseDisassemblyMembers(); + } +}; + +class BasicFileVerifierTest : public ::testing::Test { +protected: + virtual void SetUp() { + if (Verifier.initialiseDisassemblyMembers()) { + exit(EXIT_FAILURE); + } + } + + ELFx86TestFileVerifier Verifier; +}; + +TEST_F(BasicFileVerifierTest, BasicDisassemblyTraversalTest) { + Verifier.parseSectionContents( + { + 0x90, // 0: nop + 0xb0, 0x00, // 1: mov $0x0, %al + 0x48, 0x89, 0xe5, // 3: mov %rsp, %rbp + 0x48, 0x83, 0xec, 0x18, // 6: sub $0x18, %rsp + 0x48, 0xbe, 0xc4, 0x07, 0x40, + 0x00, 0x00, 0x00, 0x00, 0x00, // 10: movabs $0x4007c4, %rsi + 0x2f, // 20: (bad) + 0x41, 0x0e, // 21: rex.B (bad) + 0x62, 0x72, 0x65, 0x61, 0x6b, // 23: (bad) {%k1} + }, + 0xDEADBEEF, "Test"); + + EXPECT_EQ(nullptr, Verifier.getInstruction(0x0)); + EXPECT_EQ(nullptr, Verifier.getInstruction(0x1000)); + + // 0xDEADBEEF: nop + const auto *InstrMeta = Verifier.getInstruction(0xDEADBEEF); + EXPECT_NE(nullptr, InstrMeta); + EXPECT_EQ(0xDEADBEEF, InstrMeta->VMAddress); + EXPECT_EQ(1, InstrMeta->InstructionSize); + EXPECT_EQ("Test", InstrMeta->SectionName); + EXPECT_FALSE(InstrMeta->Bad); + + const auto *NextInstrMeta = Verifier.getNextInstructionSequential(*InstrMeta); + EXPECT_EQ(nullptr, Verifier.getPrevInstructionSequential(*InstrMeta)); + const auto *PrevInstrMeta = InstrMeta; + + // 0xDEADBEEF + 1: mov $0x0, %al + InstrMeta = Verifier.getInstruction(0xDEADBEEF + 1); + EXPECT_NE(nullptr, InstrMeta); + EXPECT_EQ(NextInstrMeta, InstrMeta); + EXPECT_EQ(0xDEADBEEF + 1, InstrMeta->VMAddress); + EXPECT_EQ(2, InstrMeta->InstructionSize); + EXPECT_EQ("Test", InstrMeta->SectionName); + EXPECT_FALSE(InstrMeta->Bad); + + NextInstrMeta = Verifier.getNextInstructionSequential(*InstrMeta); + EXPECT_EQ(PrevInstrMeta, Verifier.getPrevInstructionSequential(*InstrMeta)); + PrevInstrMeta = InstrMeta; + + // 0xDEADBEEF + 3: mov %rsp, %rbp + InstrMeta = Verifier.getInstruction(0xDEADBEEF + 3); + EXPECT_NE(nullptr, InstrMeta); + EXPECT_EQ(NextInstrMeta, InstrMeta); + EXPECT_EQ(0xDEADBEEF + 3, InstrMeta->VMAddress); + EXPECT_EQ(3, InstrMeta->InstructionSize); + EXPECT_EQ("Test", InstrMeta->SectionName); + EXPECT_FALSE(InstrMeta->Bad); + + NextInstrMeta = Verifier.getNextInstructionSequential(*InstrMeta); + EXPECT_EQ(PrevInstrMeta, Verifier.getPrevInstructionSequential(*InstrMeta)); + PrevInstrMeta = InstrMeta; + + // 0xDEADBEEF + 6: sub $0x18, %rsp + InstrMeta = Verifier.getInstruction(0xDEADBEEF + 6); + EXPECT_NE(nullptr, InstrMeta); + EXPECT_EQ(NextInstrMeta, InstrMeta); + EXPECT_EQ(0xDEADBEEF + 6, InstrMeta->VMAddress); + EXPECT_EQ(4, InstrMeta->InstructionSize); + EXPECT_EQ("Test", InstrMeta->SectionName); + EXPECT_FALSE(InstrMeta->Bad); + + NextInstrMeta = Verifier.getNextInstructionSequential(*InstrMeta); + EXPECT_EQ(PrevInstrMeta, Verifier.getPrevInstructionSequential(*InstrMeta)); + PrevInstrMeta = InstrMeta; + + // 0xDEADBEEF + 10: movabs $0x4007c4, %rsi + InstrMeta = Verifier.getInstruction(0xDEADBEEF + 10); + EXPECT_NE(nullptr, InstrMeta); + EXPECT_EQ(NextInstrMeta, InstrMeta); + EXPECT_EQ(0xDEADBEEF + 10, InstrMeta->VMAddress); + EXPECT_EQ(10, InstrMeta->InstructionSize); + EXPECT_EQ("Test", InstrMeta->SectionName); + EXPECT_FALSE(InstrMeta->Bad); + + EXPECT_EQ(nullptr, Verifier.getNextInstructionSequential(*InstrMeta)); + EXPECT_EQ(PrevInstrMeta, Verifier.getPrevInstructionSequential(*InstrMeta)); + PrevInstrMeta = InstrMeta; + + // 0xDEADBEEF + 20: (bad) + InstrMeta = Verifier.getInstruction(0xDEADBEEF + 20); + EXPECT_NE(nullptr, InstrMeta); + EXPECT_EQ(0xDEADBEEF + 20, InstrMeta->VMAddress); + EXPECT_EQ(1, InstrMeta->InstructionSize); + EXPECT_EQ("Test", InstrMeta->SectionName); + EXPECT_TRUE(InstrMeta->Bad); + + EXPECT_EQ(nullptr, Verifier.getNextInstructionSequential(*InstrMeta)); + EXPECT_EQ(PrevInstrMeta, Verifier.getPrevInstructionSequential(*InstrMeta)); + + // 0xDEADBEEF + 21: rex.B (bad) + InstrMeta = Verifier.getInstruction(0xDEADBEEF + 21); + EXPECT_NE(nullptr, InstrMeta); + EXPECT_EQ(0xDEADBEEF + 21, InstrMeta->VMAddress); + EXPECT_EQ(2, InstrMeta->InstructionSize); + EXPECT_EQ("Test", InstrMeta->SectionName); + EXPECT_TRUE(InstrMeta->Bad); + + EXPECT_EQ(nullptr, Verifier.getNextInstructionSequential(*InstrMeta)); + EXPECT_EQ(nullptr, Verifier.getPrevInstructionSequential(*InstrMeta)); + + // 0xDEADBEEF + 6: (bad) {%k1} + InstrMeta = Verifier.getInstruction(0xDEADBEEF + 23); + EXPECT_NE(nullptr, InstrMeta); + EXPECT_EQ(0xDEADBEEF + 23, InstrMeta->VMAddress); + EXPECT_EQ(5, InstrMeta->InstructionSize); + EXPECT_EQ("Test", InstrMeta->SectionName); + EXPECT_TRUE(InstrMeta->Bad); + + EXPECT_EQ(nullptr, Verifier.getNextInstructionSequential(*InstrMeta)); + EXPECT_EQ(nullptr, Verifier.getPrevInstructionSequential(*InstrMeta)); +} + +TEST_F(BasicFileVerifierTest, PrevAndNextFromBadInst) { + Verifier.parseSectionContents( + { + 0x90, // 0: nop + 0x2f, // 1: (bad) + 0x90 // 2: nop + }, + 0xDEADBEEF, "Test"); + const auto &BadInstrMeta = Verifier.getInstructionOrDie(0xDEADBEEF + 1); + const auto *GoodInstrMeta = + Verifier.getPrevInstructionSequential(BadInstrMeta); + EXPECT_NE(nullptr, GoodInstrMeta); + EXPECT_EQ(0xDEADBEEF, GoodInstrMeta->VMAddress); + EXPECT_EQ(1, GoodInstrMeta->InstructionSize); + + GoodInstrMeta = Verifier.getNextInstructionSequential(BadInstrMeta); + EXPECT_NE(nullptr, GoodInstrMeta); + EXPECT_EQ(0xDEADBEEF + 2, GoodInstrMeta->VMAddress); + EXPECT_EQ(1, GoodInstrMeta->InstructionSize); +} + +TEST_F(BasicFileVerifierTest, UndefinedInstructionTest) { + Verifier.parseSectionContents( + { + 0x90, // 0: nop + 0xb0, 0x00, // 1: mov $0x0, %al + 0x48, 0x89, 0xe5, // 3: mov %rsp, %rbp + 0x48, 0x83, 0xec, 0x18, // 6: sub $0x18, %rsp + 0x48, 0xbe, 0xc4, 0x07, 0x40, + 0x00, 0x00, 0x00, 0x00, 0x00, // 10: movabs $0x4007c4, %rsi + 0x2f, // 20: (bad) + 0x41, 0x0e, // 21: rex.B (bad) + 0x62, 0x72, 0x65, 0x61, 0x6b, // 23: (bad) {%k1} + 0x0f, 0x0b // 28: ud2 + }, + 0xDEADBEEF, "Test"); + + EXPECT_FALSE(Verifier.isUndefinedInstruction( + Verifier.getInstructionOrDie(0xDEADBEEF))); + EXPECT_FALSE(Verifier.isUndefinedInstruction( + Verifier.getInstructionOrDie(0xDEADBEEF + 3))); + EXPECT_FALSE(Verifier.isUndefinedInstruction( + Verifier.getInstructionOrDie(0xDEADBEEF + 6))); + EXPECT_FALSE(Verifier.isUndefinedInstruction( + Verifier.getInstructionOrDie(0xDEADBEEF + 10))); + EXPECT_FALSE(Verifier.isUndefinedInstruction( + Verifier.getInstructionOrDie(0xDEADBEEF + 20))); + EXPECT_FALSE(Verifier.isUndefinedInstruction( + Verifier.getInstructionOrDie(0xDEADBEEF + 21))); + EXPECT_FALSE(Verifier.isUndefinedInstruction( + Verifier.getInstructionOrDie(0xDEADBEEF + 23))); + EXPECT_TRUE(Verifier.isUndefinedInstruction( + Verifier.getInstructionOrDie(0xDEADBEEF + 28))); +} + +TEST_F(BasicFileVerifierTest, FallThroughTest) { + Verifier.parseSectionContents( + { + 0x90, // 0: nop + 0xb0, 0x00, // 1: mov $0x0, %al + 0x2f, // 3: (bad) + 0x0f, 0x0b, // 4: ud2 + 0xff, 0x20, // 6: jmpq *(%rax) + 0xeb, 0x00, // 8: jmp +0 + 0xe8, 0x45, 0xfe, 0xff, 0xff, // 10: callq [some loc] + 0xff, 0x10, // 15: callq *(rax) + 0x75, 0x00, // 17: jne +0 + 0xc3, // 19: retq + }, + 0xDEADBEEF, "Test"); + + EXPECT_TRUE( + Verifier.canFallThrough(Verifier.getInstructionOrDie(0xDEADBEEF))); + EXPECT_TRUE( + Verifier.canFallThrough(Verifier.getInstructionOrDie(0xDEADBEEF + 1))); + EXPECT_FALSE( + Verifier.canFallThrough(Verifier.getInstructionOrDie(0xDEADBEEF + 3))); + EXPECT_FALSE( + Verifier.canFallThrough(Verifier.getInstructionOrDie(0xDEADBEEF + 4))); + EXPECT_FALSE( + Verifier.canFallThrough(Verifier.getInstructionOrDie(0xDEADBEEF + 6))); + EXPECT_FALSE( + Verifier.canFallThrough(Verifier.getInstructionOrDie(0xDEADBEEF + 8))); + EXPECT_FALSE( + Verifier.canFallThrough(Verifier.getInstructionOrDie(0xDEADBEEF + 10))); + EXPECT_FALSE( + Verifier.canFallThrough(Verifier.getInstructionOrDie(0xDEADBEEF + 15))); + EXPECT_TRUE( + Verifier.canFallThrough(Verifier.getInstructionOrDie(0xDEADBEEF + 17))); + EXPECT_FALSE( + Verifier.canFallThrough(Verifier.getInstructionOrDie(0xDEADBEEF + 19))); +} + +TEST_F(BasicFileVerifierTest, DefiniteNextInstructionTest) { + Verifier.parseSectionContents( + { + 0x90, // 0: nop + 0xb0, 0x00, // 1: mov $0x0, %al + 0x2f, // 3: (bad) + 0x0f, 0x0b, // 4: ud2 + 0xff, 0x20, // 6: jmpq *(%rax) + 0xeb, 0x00, // 8: jmp 10 [+0] + 0xeb, 0x05, // 10: jmp 17 [+5] + 0xe8, 0x00, 0x00, 0x00, 0x00, // 12: callq 17 [+0] + 0xe8, 0x78, 0x56, 0x34, 0x12, // 17: callq 0x1234569f [+0x12345678] + 0xe8, 0x04, 0x00, 0x00, 0x00, // 22: callq 31 [+4] + 0xff, 0x10, // 27: callq *(rax) + 0x75, 0x00, // 29: jne 31 [+0] + 0x75, 0xe0, // 31: jne 1 [-32] + 0xc3, // 33: retq + 0xeb, 0xdd, // 34: jmp 1 [-35] + 0xeb, 0xdd, // 36: jmp 3 [-35] + 0xeb, 0xdc, // 38: jmp 4 [-36] + }, + 0xDEADBEEF, "Test"); + + const auto *Current = Verifier.getInstruction(0xDEADBEEF); + const auto *Next = Verifier.getDefiniteNextInstruction(*Current); + EXPECT_NE(nullptr, Next); + EXPECT_EQ(0xDEADBEEF + 1, Next->VMAddress); + + Current = Verifier.getInstruction(0xDEADBEEF + 1); + EXPECT_EQ(nullptr, Verifier.getDefiniteNextInstruction(*Current)); + + Current = Verifier.getInstruction(0xDEADBEEF + 3); + EXPECT_EQ(nullptr, Verifier.getDefiniteNextInstruction(*Current)); + + Current = Verifier.getInstruction(0xDEADBEEF + 4); + EXPECT_EQ(nullptr, Verifier.getDefiniteNextInstruction(*Current)); + + Current = Verifier.getInstruction(0xDEADBEEF + 6); + EXPECT_EQ(nullptr, Verifier.getDefiniteNextInstruction(*Current)); + + Current = Verifier.getInstruction(0xDEADBEEF + 8); + Next = Verifier.getDefiniteNextInstruction(*Current); + EXPECT_NE(nullptr, Next); + EXPECT_EQ(0xDEADBEEF + 10, Next->VMAddress); + + Current = Verifier.getInstruction(0xDEADBEEF + 10); + Next = Verifier.getDefiniteNextInstruction(*Current); + EXPECT_NE(nullptr, Next); + EXPECT_EQ(0xDEADBEEF + 17, Next->VMAddress); + + Current = Verifier.getInstruction(0xDEADBEEF + 12); + Next = Verifier.getDefiniteNextInstruction(*Current); + EXPECT_NE(nullptr, Next); + EXPECT_EQ(0xDEADBEEF + 17, Next->VMAddress); + + Current = Verifier.getInstruction(0xDEADBEEF + 17); + // Note, definite next instruction address is out of range and should fail. + EXPECT_EQ(nullptr, Verifier.getDefiniteNextInstruction(*Current)); + Next = Verifier.getDefiniteNextInstruction(*Current); + + Current = Verifier.getInstruction(0xDEADBEEF + 22); + Next = Verifier.getDefiniteNextInstruction(*Current); + EXPECT_NE(nullptr, Next); + EXPECT_EQ(0xDEADBEEF + 31, Next->VMAddress); + + Current = Verifier.getInstruction(0xDEADBEEF + 27); + EXPECT_EQ(nullptr, Verifier.getDefiniteNextInstruction(*Current)); + Current = Verifier.getInstruction(0xDEADBEEF + 29); + EXPECT_EQ(nullptr, Verifier.getDefiniteNextInstruction(*Current)); + Current = Verifier.getInstruction(0xDEADBEEF + 31); + EXPECT_EQ(nullptr, Verifier.getDefiniteNextInstruction(*Current)); + Current = Verifier.getInstruction(0xDEADBEEF + 33); + EXPECT_EQ(nullptr, Verifier.getDefiniteNextInstruction(*Current)); + + Current = Verifier.getInstruction(0xDEADBEEF + 34); + Next = Verifier.getDefiniteNextInstruction(*Current); + EXPECT_NE(nullptr, Next); + EXPECT_EQ(0xDEADBEEF + 1, Next->VMAddress); + + Current = Verifier.getInstruction(0xDEADBEEF + 36); + Next = Verifier.getDefiniteNextInstruction(*Current); + EXPECT_NE(nullptr, Next); + EXPECT_EQ(0xDEADBEEF + 3, Next->VMAddress); + + Current = Verifier.getInstruction(0xDEADBEEF + 38); + Next = Verifier.getDefiniteNextInstruction(*Current); + EXPECT_NE(nullptr, Next); + EXPECT_EQ(0xDEADBEEF + 4, Next->VMAddress); +} + +TEST_F(BasicFileVerifierTest, ControlFlowXRefsTest) { + Verifier.parseSectionContents( + { + 0x90, // 0: nop + 0xb0, 0x00, // 1: mov $0x0, %al + 0x2f, // 3: (bad) + 0x0f, 0x0b, // 4: ud2 + 0xff, 0x20, // 6: jmpq *(%rax) + 0xeb, 0x00, // 8: jmp 10 [+0] + 0xeb, 0x05, // 10: jmp 17 [+5] + 0xe8, 0x00, 0x00, 0x00, 0x00, // 12: callq 17 [+0] + 0xe8, 0x78, 0x56, 0x34, 0x12, // 17: callq 0x1234569f [+0x12345678] + 0xe8, 0x04, 0x00, 0x00, 0x00, // 22: callq 31 [+4] + 0xff, 0x10, // 27: callq *(rax) + 0x75, 0x00, // 29: jne 31 [+0] + 0x75, 0xe0, // 31: jne 1 [-32] + 0xc3, // 33: retq + 0xeb, 0xdd, // 34: jmp 1 [-35] + 0xeb, 0xdd, // 36: jmp 3 [-35] + 0xeb, 0xdc, // 38: jmp 4 [-36] + }, + 0xDEADBEEF, "Test"); + const auto *InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF); + std::set XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_TRUE(XRefs.empty()); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 1); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_THAT(XRefs, UnorderedElementsAre( + Field(&Instr::VMAddress, Eq(0xDEADBEEF)), + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 31)), + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 34)))); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 3); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_THAT(XRefs, UnorderedElementsAre( + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 1)), + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 36)))); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 4); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_THAT(XRefs, UnorderedElementsAre( + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 38)))); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 6); + EXPECT_TRUE(Verifier.getControlFlowXRefs(*InstrMetaPtr).empty()); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 8); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_TRUE(Verifier.getControlFlowXRefs(*InstrMetaPtr).empty()); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 10); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_THAT(XRefs, UnorderedElementsAre( + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 8)))); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 12); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_TRUE(Verifier.getControlFlowXRefs(*InstrMetaPtr).empty()); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 17); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_THAT(XRefs, UnorderedElementsAre( + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 10)), + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 12)))); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 22); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_TRUE(Verifier.getControlFlowXRefs(*InstrMetaPtr).empty()); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 27); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_TRUE(Verifier.getControlFlowXRefs(*InstrMetaPtr).empty()); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 29); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_TRUE(Verifier.getControlFlowXRefs(*InstrMetaPtr).empty()); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 31); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_THAT(XRefs, UnorderedElementsAre( + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 22)), + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 29)))); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 33); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_THAT(XRefs, UnorderedElementsAre( + Field(&Instr::VMAddress, Eq(0xDEADBEEF + 31)))); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 34); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_TRUE(Verifier.getControlFlowXRefs(*InstrMetaPtr).empty()); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 36); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_TRUE(Verifier.getControlFlowXRefs(*InstrMetaPtr).empty()); + + InstrMetaPtr = &Verifier.getInstructionOrDie(0xDEADBEEF + 38); + XRefs = Verifier.getControlFlowXRefs(*InstrMetaPtr); + EXPECT_TRUE(Verifier.getControlFlowXRefs(*InstrMetaPtr).empty()); +} + + +} // anonymous namespace +} // end namespace cfi_verify +} // end namespace llvm + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + llvm::cl::ParseCommandLineOptions(argc, argv); + + llvm::InitializeAllTargetInfos(); + llvm::InitializeAllTargetMCs(); + llvm::InitializeAllAsmParsers(); + llvm::InitializeAllDisassemblers(); + + return RUN_ALL_TESTS(); +} Index: unittests/CMakeLists.txt =================================================================== --- unittests/CMakeLists.txt +++ unittests/CMakeLists.txt @@ -27,3 +27,4 @@ add_subdirectory(Target) add_subdirectory(Transforms) add_subdirectory(XRay) +add_subdirectory(../tools/llvm-cfi-verify/unittests ../tools/llvm-cfi-verify/unittests)