Vlad Tsyrklevich | 31b4531 | 2017-09-20 20:38:14 +0000 | [diff] [blame] | 1 | //===-- llvm-cfi-verify.cpp - CFI Verification tool for LLVM --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This tool verifies Control Flow Integrity (CFI) instrumentation by static |
| 11 | // binary anaylsis. See the design document in /docs/CFIVerify.rst for more |
| 12 | // information. |
| 13 | // |
| 14 | // This tool is currently incomplete. It currently only does disassembly for |
| 15 | // object files, and searches through the code for indirect control flow |
| 16 | // instructions, printing them once found. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame^] | 20 | #include "lib/FileAnalysis.h" |
Vlad Tsyrklevich | b5488a2 | 2017-10-10 20:59:08 +0000 | [diff] [blame] | 21 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame^] | 22 | #include "llvm/BinaryFormat/ELF.h" |
| 23 | #include "llvm/Support/CommandLine.h" |
| 24 | #include "llvm/Support/Error.h" |
| 25 | |
Vlad Tsyrklevich | 31b4531 | 2017-09-20 20:38:14 +0000 | [diff] [blame] | 26 | #include <cstdlib> |
| 27 | |
| 28 | using namespace llvm; |
| 29 | using namespace llvm::object; |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame^] | 30 | using namespace llvm::cfi_verify; |
Vlad Tsyrklevich | 31b4531 | 2017-09-20 20:38:14 +0000 | [diff] [blame] | 31 | |
Vlad Tsyrklevich | 31b4531 | 2017-09-20 20:38:14 +0000 | [diff] [blame] | 32 | cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input file>"), |
| 33 | cl::Required); |
| 34 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame^] | 35 | ExitOnError ExitOnErr; |
Vlad Tsyrklevich | 31b4531 | 2017-09-20 20:38:14 +0000 | [diff] [blame] | 36 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame^] | 37 | void printIndirectCFInstructions(const FileAnalysis &Verifier) { |
| 38 | for (uint64_t Address : Verifier.getIndirectInstructions()) { |
| 39 | const auto &InstrMeta = Verifier.getInstructionOrDie(Address); |
| 40 | outs() << format_hex(Address, 2) << " |" |
| 41 | << Verifier.getMCInstrInfo()->getName( |
| 42 | InstrMeta.Instruction.getOpcode()) |
| 43 | << " "; |
| 44 | InstrMeta.Instruction.print(outs()); |
Vlad Tsyrklevich | 31b4531 | 2017-09-20 20:38:14 +0000 | [diff] [blame] | 45 | outs() << "\n"; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | int main(int argc, char **argv) { |
| 50 | cl::ParseCommandLineOptions(argc, argv); |
| 51 | |
| 52 | InitializeAllTargetInfos(); |
| 53 | InitializeAllTargetMCs(); |
| 54 | InitializeAllAsmParsers(); |
| 55 | InitializeAllDisassemblers(); |
| 56 | |
Vlad Tsyrklevich | 89c3c8c | 2017-10-11 20:35:01 +0000 | [diff] [blame^] | 57 | FileAnalysis Verifier = ExitOnErr(FileAnalysis::Create(InputFilename)); |
| 58 | printIndirectCFInstructions(Verifier); |
Vlad Tsyrklevich | 31b4531 | 2017-09-20 20:38:14 +0000 | [diff] [blame] | 59 | |
| 60 | return EXIT_SUCCESS; |
| 61 | } |