GraphBuilder.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. //===- llvm/unittests/llvm-cfi-verify/GraphBuilder.cpp --------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "../tools/llvm-cfi-verify/lib/GraphBuilder.h"
  9. #include "../tools/llvm-cfi-verify/lib/FileAnalysis.h"
  10. #include "gmock/gmock.h"
  11. #include "gtest/gtest.h"
  12. #include "llvm/BinaryFormat/ELF.h"
  13. #include "llvm/MC/MCAsmInfo.h"
  14. #include "llvm/MC/MCContext.h"
  15. #include "llvm/MC/MCDisassembler/MCDisassembler.h"
  16. #include "llvm/MC/MCInst.h"
  17. #include "llvm/MC/MCInstPrinter.h"
  18. #include "llvm/MC/MCInstrAnalysis.h"
  19. #include "llvm/MC/MCInstrDesc.h"
  20. #include "llvm/MC/MCInstrInfo.h"
  21. #include "llvm/MC/MCObjectFileInfo.h"
  22. #include "llvm/MC/MCRegisterInfo.h"
  23. #include "llvm/MC/MCSubtargetInfo.h"
  24. #include "llvm/Object/Binary.h"
  25. #include "llvm/Object/COFF.h"
  26. #include "llvm/Object/ELFObjectFile.h"
  27. #include "llvm/Object/ObjectFile.h"
  28. #include "llvm/Support/Casting.h"
  29. #include "llvm/Support/CommandLine.h"
  30. #include "llvm/Support/Error.h"
  31. #include "llvm/Support/MemoryBuffer.h"
  32. #include "llvm/Support/TargetRegistry.h"
  33. #include "llvm/Support/TargetSelect.h"
  34. #include "llvm/Support/raw_ostream.h"
  35. #include <cstdlib>
  36. #include <sstream>
  37. using Instr = ::llvm::cfi_verify::FileAnalysis::Instr;
  38. using ::testing::AllOf;
  39. using ::testing::Each;
  40. using ::testing::ElementsAre;
  41. using ::testing::Eq;
  42. using ::testing::Field;
  43. using ::testing::IsEmpty;
  44. using ::testing::Matches;
  45. using ::testing::Pair;
  46. using ::testing::PrintToString;
  47. using ::testing::Property;
  48. using ::testing::SizeIs;
  49. using ::testing::UnorderedElementsAre;
  50. using ::testing::Value;
  51. namespace llvm {
  52. namespace cfi_verify {
  53. // Printing helpers for gtest.
  54. std::string HexStringifyContainer(const std::vector<uint64_t> &C) {
  55. std::stringstream Stream;
  56. if (C.empty()) {
  57. return "{ }";
  58. }
  59. Stream << "{ ";
  60. const auto &LastElemIt = std::end(C) - 1;
  61. for (auto It = std::begin(C); It != LastElemIt; ++It) {
  62. Stream << "0x" << std::hex << *It << ", ";
  63. }
  64. Stream << "0x" << std::hex << *LastElemIt << " }";
  65. return Stream.str();
  66. }
  67. void PrintTo(const ConditionalBranchNode &BranchNode, ::std::ostream *os) {
  68. *os << "ConditionalBranchNode<Address: 0x" << std::hex << BranchNode.Address
  69. << ", Target: 0x" << BranchNode.Target << ", Fallthrough: 0x"
  70. << BranchNode.Fallthrough
  71. << ", CFIProtection: " << BranchNode.CFIProtection << ">";
  72. }
  73. void PrintTo(const GraphResult &Result, ::std::ostream *os) {
  74. *os << "Result BaseAddress: 0x" << std::hex << Result.BaseAddress << "\n";
  75. if (Result.ConditionalBranchNodes.empty())
  76. *os << " (No conditional branch nodes)\n";
  77. for (const auto &Node : Result.ConditionalBranchNodes) {
  78. *os << " ";
  79. PrintTo(Node, os);
  80. *os << "\n Fallthrough Path: " << std::hex
  81. << HexStringifyContainer(Result.flattenAddress(Node.Fallthrough))
  82. << "\n";
  83. *os << " Target Path: " << std::hex
  84. << HexStringifyContainer(Result.flattenAddress(Node.Target)) << "\n";
  85. }
  86. if (Result.OrphanedNodes.empty())
  87. *os << " (No orphaned nodes)";
  88. for (const auto &Orphan : Result.OrphanedNodes) {
  89. *os << " Orphan (0x" << std::hex << Orphan
  90. << ") Path: " << HexStringifyContainer(Result.flattenAddress(Orphan))
  91. << "\n";
  92. }
  93. }
  94. namespace {
  95. class ELFx86TestFileAnalysis : public FileAnalysis {
  96. public:
  97. ELFx86TestFileAnalysis()
  98. : FileAnalysis(Triple("x86_64--"), SubtargetFeatures()) {}
  99. // Expose this method publicly for testing.
  100. void parseSectionContents(ArrayRef<uint8_t> SectionBytes,
  101. object::SectionedAddress Address) {
  102. FileAnalysis::parseSectionContents(SectionBytes, Address);
  103. }
  104. Error initialiseDisassemblyMembers() {
  105. return FileAnalysis::initialiseDisassemblyMembers();
  106. }
  107. };
  108. class BasicGraphBuilderTest : public ::testing::Test {
  109. protected:
  110. virtual void SetUp() {
  111. IgnoreDWARFFlag = true;
  112. SuccessfullyInitialised = true;
  113. if (auto Err = Analysis.initialiseDisassemblyMembers()) {
  114. handleAllErrors(std::move(Err), [&](const UnsupportedDisassembly &E) {
  115. SuccessfullyInitialised = false;
  116. outs()
  117. << "Note: CFIVerifyTests are disabled due to lack of x86 support "
  118. "on this build.\n";
  119. });
  120. }
  121. }
  122. bool SuccessfullyInitialised;
  123. ELFx86TestFileAnalysis Analysis;
  124. };
  125. MATCHER_P2(HasPath, Result, Matcher, "has path " + PrintToString(Matcher)) {
  126. const auto &Path = Result.flattenAddress(arg);
  127. *result_listener << "the path is " << PrintToString(Path);
  128. return Matches(Matcher)(Path);
  129. }
  130. TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestSinglePathFallthroughUd2) {
  131. if (!SuccessfullyInitialised)
  132. return;
  133. Analysis.parseSectionContents(
  134. {
  135. 0x75, 0x02, // 0: jne 4 [+2]
  136. 0x0f, 0x0b, // 2: ud2
  137. 0xff, 0x10, // 4: callq *(%rax)
  138. },
  139. {0xDEADBEEF, 0x0});
  140. const auto Result =
  141. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 4, 0x0});
  142. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  143. EXPECT_THAT(Result.ConditionalBranchNodes, SizeIs(1));
  144. EXPECT_THAT(Result.ConditionalBranchNodes,
  145. Each(Field(&ConditionalBranchNode::CFIProtection, Eq(true))));
  146. EXPECT_THAT(
  147. Result.ConditionalBranchNodes,
  148. Contains(AllOf(Field(&ConditionalBranchNode::Address, Eq(0xDEADBEEF)),
  149. Field(&ConditionalBranchNode::Target,
  150. HasPath(Result, ElementsAre(0xDEADBEEF + 4))),
  151. Field(&ConditionalBranchNode::Fallthrough,
  152. HasPath(Result, ElementsAre(0xDEADBEEF + 2))))))
  153. << PrintToString(Result);
  154. }
  155. TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestSinglePathJumpUd2) {
  156. if (!SuccessfullyInitialised)
  157. return;
  158. Analysis.parseSectionContents(
  159. {
  160. 0x75, 0x02, // 0: jne 4 [+2]
  161. 0xff, 0x10, // 2: callq *(%rax)
  162. 0x0f, 0x0b, // 4: ud2
  163. },
  164. {0xDEADBEEF, 0x0});
  165. const auto Result =
  166. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 2, 0x0});
  167. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  168. EXPECT_THAT(Result.ConditionalBranchNodes, SizeIs(1));
  169. EXPECT_THAT(Result.ConditionalBranchNodes,
  170. Each(Field(&ConditionalBranchNode::CFIProtection, Eq(true))));
  171. EXPECT_THAT(
  172. Result.ConditionalBranchNodes,
  173. Contains(AllOf(Field(&ConditionalBranchNode::Address, Eq(0xDEADBEEF)),
  174. Field(&ConditionalBranchNode::Target,
  175. HasPath(Result, ElementsAre(0xDEADBEEF + 4))),
  176. Field(&ConditionalBranchNode::Fallthrough,
  177. HasPath(Result, ElementsAre(0xDEADBEEF + 2))))))
  178. << PrintToString(Result);
  179. }
  180. TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestDualPathDualUd2) {
  181. if (!SuccessfullyInitialised)
  182. return;
  183. Analysis.parseSectionContents(
  184. {
  185. 0x75, 0x03, // 0: jne 5 [+3]
  186. 0x90, // 2: nop
  187. 0xff, 0x10, // 3: callq *(%rax)
  188. 0x0f, 0x0b, // 5: ud2
  189. 0x75, 0xf9, // 7: jne 2 [-7]
  190. 0x0f, 0x0b, // 9: ud2
  191. },
  192. {0xDEADBEEF, 0x0});
  193. const auto Result =
  194. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 3, 0x0});
  195. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  196. EXPECT_THAT(Result.ConditionalBranchNodes, SizeIs(2));
  197. EXPECT_THAT(Result.ConditionalBranchNodes,
  198. Each(Field(&ConditionalBranchNode::CFIProtection, Eq(true))));
  199. EXPECT_THAT(
  200. Result.ConditionalBranchNodes,
  201. Contains(AllOf(
  202. Field(&ConditionalBranchNode::Address, Eq(0xDEADBEEF)),
  203. Field(&ConditionalBranchNode::Fallthrough,
  204. HasPath(Result, ElementsAre(0xDEADBEEF + 2, 0xDEADBEEF + 3))),
  205. Field(&ConditionalBranchNode::Target,
  206. HasPath(Result, ElementsAre(0xDEADBEEF + 5))))))
  207. << PrintToString(Result);
  208. EXPECT_THAT(
  209. Result.ConditionalBranchNodes,
  210. Contains(AllOf(
  211. Field(&ConditionalBranchNode::Address, Eq(0xDEADBEEF + 7)),
  212. Field(&ConditionalBranchNode::Fallthrough,
  213. HasPath(Result, ElementsAre(0xDEADBEEF + 9))),
  214. Field(&ConditionalBranchNode::Target,
  215. HasPath(Result, ElementsAre(0xDEADBEEF + 2, 0xDEADBEEF + 3))))))
  216. << PrintToString(Result);
  217. }
  218. TEST_F(BasicGraphBuilderTest, BuildFlowGraphTestDualPathSingleUd2) {
  219. if (!SuccessfullyInitialised)
  220. return;
  221. Analysis.parseSectionContents(
  222. {
  223. 0x75, 0x05, // 0: jne 7 [+5]
  224. 0x90, // 2: nop
  225. 0xff, 0x10, // 3: callq *(%rax)
  226. 0x75, 0xfb, // 5: jne 2 [-5]
  227. 0x0f, 0x0b, // 7: ud2
  228. },
  229. {0xDEADBEEF, 0x0});
  230. GraphResult Result =
  231. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 3, 0x0});
  232. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  233. EXPECT_THAT(Result.ConditionalBranchNodes, SizeIs(2));
  234. EXPECT_THAT(Result.ConditionalBranchNodes,
  235. Each(Field(&ConditionalBranchNode::CFIProtection, Eq(true))));
  236. EXPECT_THAT(
  237. Result.ConditionalBranchNodes,
  238. Contains(AllOf(
  239. Field(&ConditionalBranchNode::Address, Eq(0xDEADBEEF)),
  240. Field(&ConditionalBranchNode::Fallthrough,
  241. HasPath(Result, ElementsAre(0xDEADBEEF + 2, 0xDEADBEEF + 3))),
  242. Field(&ConditionalBranchNode::Target,
  243. HasPath(Result, ElementsAre(0xDEADBEEF + 7))))))
  244. << PrintToString(Result);
  245. EXPECT_THAT(
  246. Result.ConditionalBranchNodes,
  247. Contains(AllOf(
  248. Field(&ConditionalBranchNode::Address, Eq(0xDEADBEEF + 5)),
  249. Field(&ConditionalBranchNode::Fallthrough,
  250. HasPath(Result, ElementsAre(0xDEADBEEF + 7))),
  251. Field(&ConditionalBranchNode::Target,
  252. HasPath(Result, ElementsAre(0xDEADBEEF + 2, 0xDEADBEEF + 3))))))
  253. << PrintToString(Result);
  254. }
  255. TEST_F(BasicGraphBuilderTest, BuildFlowGraphFailures) {
  256. if (!SuccessfullyInitialised)
  257. return;
  258. Analysis.parseSectionContents(
  259. {
  260. 0x90, // 0: nop
  261. 0x75, 0xfe, // 1: jne 1 [-2]
  262. },
  263. {0xDEADBEEF, 0x0});
  264. GraphResult Result =
  265. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF, 0x0});
  266. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  267. EXPECT_THAT(Result.ConditionalBranchNodes, IsEmpty());
  268. Result = GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 1, 0x0});
  269. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  270. EXPECT_THAT(Result.ConditionalBranchNodes, IsEmpty());
  271. Result = GraphBuilder::buildFlowGraph(Analysis, {0xDEADC0DE, 0x0});
  272. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  273. EXPECT_THAT(Result.ConditionalBranchNodes, IsEmpty());
  274. }
  275. TEST_F(BasicGraphBuilderTest, BuildFlowGraphNoXrefs) {
  276. if (!SuccessfullyInitialised)
  277. return;
  278. Analysis.parseSectionContents(
  279. {
  280. 0xeb, 0xfe, // 0: jmp 0 [-2]
  281. 0xff, 0x10, // 2: callq *(%rax)
  282. },
  283. {0xDEADBEEF, 0x0});
  284. GraphResult Result =
  285. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 2, 0x0});
  286. EXPECT_THAT(Result.ConditionalBranchNodes, IsEmpty());
  287. EXPECT_THAT(Result.OrphanedNodes, ElementsAre(0xDEADBEEF + 2));
  288. EXPECT_THAT(Result.IntermediateNodes, IsEmpty());
  289. }
  290. TEST_F(BasicGraphBuilderTest, BuildFlowGraphConditionalInfiniteLoop) {
  291. if (!SuccessfullyInitialised)
  292. return;
  293. Analysis.parseSectionContents(
  294. {
  295. 0x75, 0xfe, // 0: jne 0 [-2]
  296. 0xff, 0x10, // 2: callq *(%rax)
  297. },
  298. {0xDEADBEEF, 0x0});
  299. GraphResult Result =
  300. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 2, 0x0});
  301. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  302. EXPECT_THAT(Result.ConditionalBranchNodes, SizeIs(1));
  303. EXPECT_THAT(
  304. Result.ConditionalBranchNodes,
  305. Each(AllOf(Field(&ConditionalBranchNode::CFIProtection, Eq(false)),
  306. Field(&ConditionalBranchNode::Target,
  307. HasPath(Result, ElementsAre(0xDEADBEEF))),
  308. Field(&ConditionalBranchNode::Fallthrough,
  309. HasPath(Result, ElementsAre(0xDEADBEEF + 2))))))
  310. << PrintToString(Result);
  311. }
  312. TEST_F(BasicGraphBuilderTest, BuildFlowGraphUnconditionalInfiniteLoop) {
  313. if (!SuccessfullyInitialised)
  314. return;
  315. Analysis.parseSectionContents(
  316. {
  317. 0x75, 0x02, // 0: jne 4 [+2]
  318. 0xeb, 0xfc, // 2: jmp 0 [-4]
  319. 0xff, 0x10, // 4: callq *(%rax)
  320. },
  321. {0xDEADBEEF, 0x0});
  322. GraphResult Result =
  323. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 4, 0x0});
  324. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  325. EXPECT_THAT(Result.ConditionalBranchNodes, SizeIs(1));
  326. EXPECT_THAT(
  327. Result.ConditionalBranchNodes,
  328. Contains(
  329. AllOf(Field(&ConditionalBranchNode::Address, Eq(0xDEADBEEF)),
  330. Field(&ConditionalBranchNode::Fallthrough,
  331. HasPath(Result, ElementsAre(0xDEADBEEF + 2, 0xDEADBEEF))),
  332. Field(&ConditionalBranchNode::Target,
  333. HasPath(Result, ElementsAre(0xDEADBEEF + 4))))))
  334. << PrintToString(Result);
  335. }
  336. TEST_F(BasicGraphBuilderTest, BuildFlowGraphNoFlowsToIndirection) {
  337. if (!SuccessfullyInitialised)
  338. return;
  339. Analysis.parseSectionContents(
  340. {
  341. 0x75, 0x00, // 0: jne 2 [+0]
  342. 0xeb, 0xfc, // 2: jmp 0 [-4]
  343. 0xff, 0x10, // 4: callq *(%rax)
  344. },
  345. {0xDEADBEEF, 0x0});
  346. GraphResult Result =
  347. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 4, 0x0});
  348. EXPECT_THAT(Result.OrphanedNodes, ElementsAre(0xDEADBEEF + 4));
  349. EXPECT_THAT(Result.ConditionalBranchNodes, IsEmpty());
  350. }
  351. TEST_F(BasicGraphBuilderTest, BuildFlowGraphLengthExceededUpwards) {
  352. if (!SuccessfullyInitialised)
  353. return;
  354. Analysis.parseSectionContents(
  355. {
  356. 0x75, 0x06, // 0: jne 8 [+6]
  357. 0x90, // 2: nop
  358. 0x90, // 3: nop
  359. 0x90, // 4: nop
  360. 0x90, // 5: nop
  361. 0xff, 0x10, // 6: callq *(%rax)
  362. 0x0f, 0x0b, // 8: ud2
  363. },
  364. {0xDEADBEEF, 0x0});
  365. uint64_t PrevSearchLengthForConditionalBranch =
  366. SearchLengthForConditionalBranch;
  367. SearchLengthForConditionalBranch = 2;
  368. GraphResult Result =
  369. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 6, 0x0});
  370. EXPECT_THAT(Result.OrphanedNodes, SizeIs(1));
  371. EXPECT_THAT(Result.OrphanedNodes,
  372. Each(HasPath(Result, ElementsAre(0xDEADBEEF + 4, 0xDEADBEEF + 5,
  373. 0xDEADBEEF + 6))))
  374. << PrintToString(Result);
  375. EXPECT_THAT(Result.ConditionalBranchNodes, IsEmpty());
  376. SearchLengthForConditionalBranch = PrevSearchLengthForConditionalBranch;
  377. }
  378. TEST_F(BasicGraphBuilderTest, BuildFlowGraphLengthExceededDownwards) {
  379. if (!SuccessfullyInitialised)
  380. return;
  381. Analysis.parseSectionContents(
  382. {
  383. 0x75, 0x02, // 0: jne 4 [+2]
  384. 0xff, 0x10, // 2: callq *(%rax)
  385. 0x90, // 4: nop
  386. 0x90, // 5: nop
  387. 0x90, // 6: nop
  388. 0x90, // 7: nop
  389. 0x0f, 0x0b, // 8: ud2
  390. },
  391. {0xDEADBEEF, 0x0});
  392. uint64_t PrevSearchLengthForUndef = SearchLengthForUndef;
  393. SearchLengthForUndef = 2;
  394. GraphResult Result =
  395. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 2, 0x0});
  396. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  397. EXPECT_THAT(
  398. Result.ConditionalBranchNodes,
  399. Each(AllOf(
  400. Field(&ConditionalBranchNode::CFIProtection, Eq(false)),
  401. Field(&ConditionalBranchNode::Address, Eq(0xDEADBEEF)),
  402. Field(&ConditionalBranchNode::Target,
  403. HasPath(Result, ElementsAre(0xDEADBEEF + 4, 0xDEADBEEF + 5))),
  404. Field(&ConditionalBranchNode::Fallthrough,
  405. HasPath(Result, ElementsAre(0xDEADBEEF + 2))))))
  406. << PrintToString(Result);
  407. SearchLengthForUndef = PrevSearchLengthForUndef;
  408. }
  409. // This test ensures when avoiding doing repeated work we still generate the
  410. // paths correctly. We don't need to recalculate the flow from 0x2 -> 0x3 as it
  411. // should only need to be generated once.
  412. TEST_F(BasicGraphBuilderTest, BuildFlowGraphWithRepeatedWork) {
  413. if (!SuccessfullyInitialised)
  414. return;
  415. Analysis.parseSectionContents(
  416. {
  417. 0x75, 0x05, // 0: jne 7 [+5]
  418. 0x90, // 2: nop
  419. 0xff, 0x10, // 3: callq *(%rax)
  420. 0x75, 0xfb, // 5: jne 2 [-5]
  421. 0x0f, 0x0b, // 7: ud2
  422. },
  423. {0xDEADBEEF, 0x0});
  424. GraphResult Result =
  425. GraphBuilder::buildFlowGraph(Analysis, {0xDEADBEEF + 3, 0x0});
  426. EXPECT_THAT(Result.OrphanedNodes, IsEmpty());
  427. EXPECT_THAT(Result.ConditionalBranchNodes, SizeIs(2));
  428. EXPECT_THAT(
  429. Result.ConditionalBranchNodes,
  430. Contains(AllOf(
  431. Field(&ConditionalBranchNode::CFIProtection, Eq(true)),
  432. Field(&ConditionalBranchNode::Address, Eq(0xDEADBEEF)),
  433. Field(&ConditionalBranchNode::Target,
  434. HasPath(Result, ElementsAre(0xDEADBEEF + 7))),
  435. Field(&ConditionalBranchNode::Fallthrough,
  436. HasPath(Result, ElementsAre(0xDEADBEEF + 2, 0xDEADBEEF + 3))))))
  437. << PrintToString(Result);
  438. EXPECT_THAT(
  439. Result.ConditionalBranchNodes,
  440. Contains(AllOf(
  441. Field(&ConditionalBranchNode::CFIProtection, Eq(true)),
  442. Field(&ConditionalBranchNode::Address, Eq(0xDEADBEEF + 5)),
  443. Field(&ConditionalBranchNode::Target,
  444. HasPath(Result, ElementsAre(0xDEADBEEF + 2, 0xDEADBEEF + 3))),
  445. Field(&ConditionalBranchNode::Fallthrough,
  446. HasPath(Result, ElementsAre(0xDEADBEEF + 7))))))
  447. << PrintToString(Result);
  448. EXPECT_THAT(Result.IntermediateNodes, SizeIs(1));
  449. EXPECT_THAT(Result.IntermediateNodes,
  450. UnorderedElementsAre(Pair(0xDEADBEEF + 2, 0xDEADBEEF + 3)));
  451. }
  452. TEST_F(BasicGraphBuilderTest, BuildFlowGraphComplexExample) {
  453. if (!SuccessfullyInitialised)
  454. return;
  455. // The following code has this graph:
  456. // +----------+ +--------------+
  457. // | 20 | <--- | 0 |
  458. // +----------+ +--------------+
  459. // | |
  460. // v v
  461. // +----------+ +--------------+
  462. // | 21 | | 2 |
  463. // +----------+ +--------------+
  464. // | |
  465. // v v
  466. // +----------+ +--------------+
  467. // | 22 (ud2) | +-> | 7 |
  468. // +----------+ | +--------------+
  469. // ^ | |
  470. // | | v
  471. // +----------+ | +--------------+
  472. // | 4 | | | 8 |
  473. // +----------+ | +--------------+
  474. // | | |
  475. // v | v
  476. // +----------+ | +--------------+ +------------+
  477. // | 6 | -+ | 9 (indirect) | <- | 13 |
  478. // +----------+ +--------------+ +------------+
  479. // ^ |
  480. // | v
  481. // +--------------+ +------------+
  482. // | 11 | | 15 (error) |
  483. // +--------------+ +------------+
  484. // Or, in image format: https://i.imgur.com/aX5fCoi.png
  485. Analysis.parseSectionContents(
  486. {
  487. 0x75, 0x12, // 0: jne 20 [+18]
  488. 0xeb, 0x03, // 2: jmp 7 [+3]
  489. 0x75, 0x10, // 4: jne 22 [+16]
  490. 0x90, // 6: nop
  491. 0x90, // 7: nop
  492. 0x90, // 8: nop
  493. 0xff, 0x10, // 9: callq *(%rax)
  494. 0xeb, 0xfc, // 11: jmp 9 [-4]
  495. 0x75, 0xfa, // 13: jne 9 [-6]
  496. 0xe8, 0x78, 0x56, 0x34, 0x12, // 15: callq OUTOFBOUNDS [+0x12345678]
  497. 0x90, // 20: nop
  498. 0x90, // 21: nop
  499. 0x0f, 0x0b, // 22: ud2
  500. },
  501. {0x1000, 0x0});
  502. uint64_t PrevSearchLengthForUndef = SearchLengthForUndef;
  503. SearchLengthForUndef = 5;
  504. GraphResult Result =
  505. GraphBuilder::buildFlowGraph(Analysis, {0x1000 + 9, 0x0});
  506. EXPECT_THAT(Result.OrphanedNodes, SizeIs(1));
  507. EXPECT_THAT(Result.ConditionalBranchNodes, SizeIs(3));
  508. EXPECT_THAT(
  509. Result.OrphanedNodes,
  510. Each(AllOf(Eq(0x1000u + 11),
  511. HasPath(Result, ElementsAre(0x1000 + 11, 0x1000 + 9)))))
  512. << PrintToString(Result);
  513. EXPECT_THAT(Result.ConditionalBranchNodes,
  514. Contains(AllOf(
  515. Field(&ConditionalBranchNode::CFIProtection, Eq(true)),
  516. Field(&ConditionalBranchNode::Address, Eq(0x1000u)),
  517. Field(&ConditionalBranchNode::Target,
  518. HasPath(Result, ElementsAre(0x1000 + 20, 0x1000 + 21,
  519. 0x1000 + 22))),
  520. Field(&ConditionalBranchNode::Fallthrough,
  521. HasPath(Result, ElementsAre(0x1000 + 2, 0x1000 + 7,
  522. 0x1000 + 8, 0x1000 + 9))))))
  523. << PrintToString(Result);
  524. EXPECT_THAT(Result.ConditionalBranchNodes,
  525. Contains(AllOf(
  526. Field(&ConditionalBranchNode::CFIProtection, Eq(true)),
  527. Field(&ConditionalBranchNode::Address, Eq(0x1000u + 4)),
  528. Field(&ConditionalBranchNode::Target,
  529. HasPath(Result, ElementsAre(0x1000 + 22))),
  530. Field(&ConditionalBranchNode::Fallthrough,
  531. HasPath(Result, ElementsAre(0x1000 + 6, 0x1000 + 7,
  532. 0x1000 + 8, 0x1000 + 9))))))
  533. << PrintToString(Result);
  534. EXPECT_THAT(
  535. Result.ConditionalBranchNodes,
  536. Contains(AllOf(Field(&ConditionalBranchNode::CFIProtection, Eq(false)),
  537. Field(&ConditionalBranchNode::Address, Eq(0x1000u + 13)),
  538. Field(&ConditionalBranchNode::Target,
  539. HasPath(Result, ElementsAre(0x1000 + 9))),
  540. Field(&ConditionalBranchNode::Fallthrough,
  541. HasPath(Result, ElementsAre(0x1000 + 15))))))
  542. << PrintToString(Result);
  543. SearchLengthForUndef = PrevSearchLengthForUndef;
  544. }
  545. } // anonymous namespace
  546. } // end namespace cfi_verify
  547. } // end namespace llvm