[llvm] [JITLink][AArch32] Unittest for error paths of readAddend functionality (PR #69636)

Eymen Ünay via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 25 08:35:30 PDT 2023


https://github.com/eymay updated https://github.com/llvm/llvm-project/pull/69636

>From 26e6744ee7d34a6e2df72056e65ebc2a94f2609a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Eymen=20=C3=9Cnay?= <eymenunay at outlook.com>
Date: Thu, 19 Oct 2023 22:38:05 +0300
Subject: [PATCH 1/4] [JITLink][AArch32] Unittest for error paths of readAddend
 functionality

This commit is part of an effort to increase coverage of AArch32 backend.
---
 .../ExecutionEngine/JITLink/AArch32Tests.cpp  | 94 +++++++++++++++++++
 1 file changed, 94 insertions(+)

diff --git a/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp b/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp
index dcc8d3b237ff318..1df59edcd769407 100644
--- a/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp
+++ b/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp
@@ -9,6 +9,7 @@
 #include <llvm/BinaryFormat/ELF.h>
 #include <llvm/ExecutionEngine/JITLink/aarch32.h>
 
+#include "llvm/Testing/Support/Error.h"
 #include "gtest/gtest.h"
 
 using namespace llvm;
@@ -67,6 +68,99 @@ TEST(AArch32_ELF, EdgeKinds) {
   }
 }
 
+struct ThumbRelocation {
+  /// Create a read-only reference to a Thumb32 fixup.
+  ThumbRelocation(const char *FixupPtr)
+      : Hi{*reinterpret_cast<const support::ulittle16_t *>(FixupPtr)},
+        Lo{*reinterpret_cast<const support::ulittle16_t *>(FixupPtr + 2)} {}
+
+  const support::ulittle16_t &Hi; // First halfword
+  const support::ulittle16_t &Lo; // Second halfword
+};
+
+struct ArmRelocation {
+
+  ArmRelocation(const char *FixupPtr)
+      : Wd{*reinterpret_cast<const support::ulittle32_t *>(FixupPtr)} {}
+
+  const support::ulittle32_t &Wd;
+};
+
+std::string makeUnexpectedOpcodeError(const LinkGraph &G,
+                                      const ThumbRelocation &R,
+                                      Edge::Kind Kind) {
+  return formatv("Invalid opcode [ {0:x4}, {1:x4} ] for relocation: {2}",
+                 static_cast<uint16_t>(R.Hi), static_cast<uint16_t>(R.Lo),
+                 G.getEdgeKindName(Kind));
+}
+
+std::string makeUnexpectedOpcodeError(const LinkGraph &G,
+                                      const ArmRelocation &R, Edge::Kind Kind) {
+  return formatv("Invalid opcode {0:x8} for relocation: {1}",
+                 static_cast<uint32_t>(R.Wd), G.getEdgeKindName(Kind));
+}
+
+TEST(AArch32_ELF, readAddends) {
+  auto G = std::make_unique<LinkGraph>("foo", Triple("armv7-linux-gnueabi"), 4,
+                                       llvm::endianness::little,
+                                       getGenericEdgeKindName);
+
+  ArrayRef<char> Content = "hello, world!";
+  auto &Sec =
+      G->createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
+  orc::ExecutorAddr B1Addr(0x1000);
+  auto &B = G->createContentBlock(Sec, Content, B1Addr, 4, 0);
+
+  Symbol &TargetSymbol = G->addAnonymousSymbol(B, 0, 4, false, false);
+  Edge InvalidEdge(FirstDataRelocation - 1, 0, TargetSymbol, 0);
+
+  auto ArmCfg = getArmConfigForCPUArch(ARMBuildAttrs::v7);
+
+  auto makeReadAddendError = [](LinkGraph &G, Block &B, Edge &E) {
+    return ("In graph " + G.getName() + ", section " +
+            B.getSection().getName() +
+            " can not read implicit addend for aarch32 edge kind " +
+            G.getEdgeKindName(E.getKind()))
+        .str();
+  };
+
+  EXPECT_THAT_EXPECTED(
+      readAddend(*G, B, InvalidEdge, ArmCfg),
+      FailedWithMessage(makeReadAddendError(*G, B, InvalidEdge)));
+
+  ArmRelocation R_Arm(B.getContent().data());
+  ThumbRelocation R_Thumb(B.getContent().data());
+
+  for (Edge::Kind K = FirstDataRelocation; K < LastDataRelocation; K += 1) {
+    Edge E(K, 0, TargetSymbol, 0);
+    EXPECT_THAT_EXPECTED(readAddendData(*G, B, E), Succeeded());
+    EXPECT_THAT_EXPECTED(readAddendArm(*G, B, E),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+    EXPECT_THAT_EXPECTED(readAddendThumb(*G, B, E, ArmCfg),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+  }
+  for (Edge::Kind K = FirstArmRelocation; K < LastArmRelocation; K += 1) {
+    Edge E(K, 0, TargetSymbol, 0);
+    EXPECT_THAT_EXPECTED(readAddendData(*G, B, E),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+    EXPECT_THAT_EXPECTED(
+        readAddendArm(*G, B, E),
+        FailedWithMessage(makeUnexpectedOpcodeError(*G, R_Arm, K)));
+    EXPECT_THAT_EXPECTED(readAddendThumb(*G, B, E, ArmCfg),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+  }
+  for (Edge::Kind K = FirstThumbRelocation; K < LastThumbRelocation; K += 1) {
+    Edge E(K, 0, TargetSymbol, 0);
+    EXPECT_THAT_EXPECTED(readAddendData(*G, B, E),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+    EXPECT_THAT_EXPECTED(readAddendArm(*G, B, E),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+    EXPECT_THAT_EXPECTED(
+        readAddendThumb(*G, B, E, ArmCfg),
+        FailedWithMessage(makeUnexpectedOpcodeError(*G, R_Thumb, K)));
+  }
+}
+
 namespace llvm {
 namespace jitlink {
 namespace aarch32 {

>From ac9f24235c4f93f89d7b0231993ee2a874fdff73 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Eymen=20=C3=9Cnay?= <eymenunay at outlook.com>
Date: Wed, 25 Oct 2023 13:03:53 +0300
Subject: [PATCH 2/4] Move test to another file

---
 .../JITLink/AArch32ErrorTests.cpp             | 112 ++++++++++++++++++
 .../ExecutionEngine/JITLink/AArch32Tests.cpp  |  92 --------------
 .../ExecutionEngine/JITLink/CMakeLists.txt    |   1 +
 3 files changed, 113 insertions(+), 92 deletions(-)
 create mode 100644 llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp

diff --git a/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp b/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp
new file mode 100644
index 000000000000000..0a1155443345c3e
--- /dev/null
+++ b/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp
@@ -0,0 +1,112 @@
+//===------- AArch32ErrorTests.cpp - Unit tests for AArch32 error handling -===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <llvm/ExecutionEngine/JITLink/aarch32.h>
+
+
+#include "llvm/Testing/Support/Error.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+using namespace llvm::jitlink;
+using namespace llvm::jitlink::aarch32;
+using namespace llvm::support;
+using namespace llvm::support::endian;
+
+struct ThumbRelocation {
+  /// Create a read-only reference to a Thumb32 fixup.
+  ThumbRelocation(const char *FixupPtr)
+      : Hi{*reinterpret_cast<const support::ulittle16_t *>(FixupPtr)},
+        Lo{*reinterpret_cast<const support::ulittle16_t *>(FixupPtr + 2)} {}
+
+  const support::ulittle16_t &Hi; // First halfword
+  const support::ulittle16_t &Lo; // Second halfword
+};
+
+struct ArmRelocation {
+
+  ArmRelocation(const char *FixupPtr)
+      : Wd{*reinterpret_cast<const support::ulittle32_t *>(FixupPtr)} {}
+
+  const support::ulittle32_t &Wd;
+};
+
+std::string makeUnexpectedOpcodeError(const LinkGraph &G,
+                                      const ThumbRelocation &R,
+                                      Edge::Kind Kind) {
+  return formatv("Invalid opcode [ {0:x4}, {1:x4} ] for relocation: {2}",
+                 static_cast<uint16_t>(R.Hi), static_cast<uint16_t>(R.Lo),
+                 G.getEdgeKindName(Kind));
+}
+
+std::string makeUnexpectedOpcodeError(const LinkGraph &G,
+                                      const ArmRelocation &R, Edge::Kind Kind) {
+  return formatv("Invalid opcode {0:x8} for relocation: {1}",
+                 static_cast<uint32_t>(R.Wd), G.getEdgeKindName(Kind));
+}
+
+TEST(AArch32_ELF, readAddends) {
+  auto G = std::make_unique<LinkGraph>("foo", Triple("armv7-linux-gnueabi"), 4,
+                                       llvm::endianness::little,
+                                       getGenericEdgeKindName);
+
+  ArrayRef<char> Content = "hello, world!";
+  auto &Sec =
+      G->createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
+  orc::ExecutorAddr B1Addr(0x1000);
+  auto &B = G->createContentBlock(Sec, Content, B1Addr, 4, 0);
+
+  Symbol &TargetSymbol = G->addAnonymousSymbol(B, 0, 4, false, false);
+  Edge InvalidEdge(FirstDataRelocation - 1, 0, TargetSymbol, 0);
+
+  auto ArmCfg = getArmConfigForCPUArch(ARMBuildAttrs::v7);
+
+  auto makeReadAddendError = [](LinkGraph &G, Block &B, Edge &E) {
+    return ("In graph " + G.getName() + ", section " +
+            B.getSection().getName() +
+            " can not read implicit addend for aarch32 edge kind " +
+            G.getEdgeKindName(E.getKind()))
+        .str();
+  };
+
+  EXPECT_THAT_EXPECTED(
+      readAddend(*G, B, InvalidEdge, ArmCfg),
+      FailedWithMessage(makeReadAddendError(*G, B, InvalidEdge)));
+
+  ArmRelocation R_Arm(B.getContent().data());
+  ThumbRelocation R_Thumb(B.getContent().data());
+
+  for (Edge::Kind K = FirstDataRelocation; K < LastDataRelocation; K += 1) {
+    Edge E(K, 0, TargetSymbol, 0);
+    EXPECT_THAT_EXPECTED(readAddendData(*G, B, E), Succeeded());
+    EXPECT_THAT_EXPECTED(readAddendArm(*G, B, E),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+    EXPECT_THAT_EXPECTED(readAddendThumb(*G, B, E, ArmCfg),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+  }
+  for (Edge::Kind K = FirstArmRelocation; K < LastArmRelocation; K += 1) {
+    Edge E(K, 0, TargetSymbol, 0);
+    EXPECT_THAT_EXPECTED(readAddendData(*G, B, E),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+    EXPECT_THAT_EXPECTED(
+        readAddendArm(*G, B, E),
+        FailedWithMessage(makeUnexpectedOpcodeError(*G, R_Arm, K)));
+    EXPECT_THAT_EXPECTED(readAddendThumb(*G, B, E, ArmCfg),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+  }
+  for (Edge::Kind K = FirstThumbRelocation; K < LastThumbRelocation; K += 1) {
+    Edge E(K, 0, TargetSymbol, 0);
+    EXPECT_THAT_EXPECTED(readAddendData(*G, B, E),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+    EXPECT_THAT_EXPECTED(readAddendArm(*G, B, E),
+                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+    EXPECT_THAT_EXPECTED(
+        readAddendThumb(*G, B, E, ArmCfg),
+        FailedWithMessage(makeUnexpectedOpcodeError(*G, R_Thumb, K)));
+  }
+}
diff --git a/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp b/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp
index 1df59edcd769407..5bcfb85ac44bfb5 100644
--- a/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp
+++ b/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp
@@ -68,98 +68,6 @@ TEST(AArch32_ELF, EdgeKinds) {
   }
 }
 
-struct ThumbRelocation {
-  /// Create a read-only reference to a Thumb32 fixup.
-  ThumbRelocation(const char *FixupPtr)
-      : Hi{*reinterpret_cast<const support::ulittle16_t *>(FixupPtr)},
-        Lo{*reinterpret_cast<const support::ulittle16_t *>(FixupPtr + 2)} {}
-
-  const support::ulittle16_t &Hi; // First halfword
-  const support::ulittle16_t &Lo; // Second halfword
-};
-
-struct ArmRelocation {
-
-  ArmRelocation(const char *FixupPtr)
-      : Wd{*reinterpret_cast<const support::ulittle32_t *>(FixupPtr)} {}
-
-  const support::ulittle32_t &Wd;
-};
-
-std::string makeUnexpectedOpcodeError(const LinkGraph &G,
-                                      const ThumbRelocation &R,
-                                      Edge::Kind Kind) {
-  return formatv("Invalid opcode [ {0:x4}, {1:x4} ] for relocation: {2}",
-                 static_cast<uint16_t>(R.Hi), static_cast<uint16_t>(R.Lo),
-                 G.getEdgeKindName(Kind));
-}
-
-std::string makeUnexpectedOpcodeError(const LinkGraph &G,
-                                      const ArmRelocation &R, Edge::Kind Kind) {
-  return formatv("Invalid opcode {0:x8} for relocation: {1}",
-                 static_cast<uint32_t>(R.Wd), G.getEdgeKindName(Kind));
-}
-
-TEST(AArch32_ELF, readAddends) {
-  auto G = std::make_unique<LinkGraph>("foo", Triple("armv7-linux-gnueabi"), 4,
-                                       llvm::endianness::little,
-                                       getGenericEdgeKindName);
-
-  ArrayRef<char> Content = "hello, world!";
-  auto &Sec =
-      G->createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
-  orc::ExecutorAddr B1Addr(0x1000);
-  auto &B = G->createContentBlock(Sec, Content, B1Addr, 4, 0);
-
-  Symbol &TargetSymbol = G->addAnonymousSymbol(B, 0, 4, false, false);
-  Edge InvalidEdge(FirstDataRelocation - 1, 0, TargetSymbol, 0);
-
-  auto ArmCfg = getArmConfigForCPUArch(ARMBuildAttrs::v7);
-
-  auto makeReadAddendError = [](LinkGraph &G, Block &B, Edge &E) {
-    return ("In graph " + G.getName() + ", section " +
-            B.getSection().getName() +
-            " can not read implicit addend for aarch32 edge kind " +
-            G.getEdgeKindName(E.getKind()))
-        .str();
-  };
-
-  EXPECT_THAT_EXPECTED(
-      readAddend(*G, B, InvalidEdge, ArmCfg),
-      FailedWithMessage(makeReadAddendError(*G, B, InvalidEdge)));
-
-  ArmRelocation R_Arm(B.getContent().data());
-  ThumbRelocation R_Thumb(B.getContent().data());
-
-  for (Edge::Kind K = FirstDataRelocation; K < LastDataRelocation; K += 1) {
-    Edge E(K, 0, TargetSymbol, 0);
-    EXPECT_THAT_EXPECTED(readAddendData(*G, B, E), Succeeded());
-    EXPECT_THAT_EXPECTED(readAddendArm(*G, B, E),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
-    EXPECT_THAT_EXPECTED(readAddendThumb(*G, B, E, ArmCfg),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
-  }
-  for (Edge::Kind K = FirstArmRelocation; K < LastArmRelocation; K += 1) {
-    Edge E(K, 0, TargetSymbol, 0);
-    EXPECT_THAT_EXPECTED(readAddendData(*G, B, E),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
-    EXPECT_THAT_EXPECTED(
-        readAddendArm(*G, B, E),
-        FailedWithMessage(makeUnexpectedOpcodeError(*G, R_Arm, K)));
-    EXPECT_THAT_EXPECTED(readAddendThumb(*G, B, E, ArmCfg),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
-  }
-  for (Edge::Kind K = FirstThumbRelocation; K < LastThumbRelocation; K += 1) {
-    Edge E(K, 0, TargetSymbol, 0);
-    EXPECT_THAT_EXPECTED(readAddendData(*G, B, E),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
-    EXPECT_THAT_EXPECTED(readAddendArm(*G, B, E),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
-    EXPECT_THAT_EXPECTED(
-        readAddendThumb(*G, B, E, ArmCfg),
-        FailedWithMessage(makeUnexpectedOpcodeError(*G, R_Thumb, K)));
-  }
-}
 
 namespace llvm {
 namespace jitlink {
diff --git a/llvm/unittests/ExecutionEngine/JITLink/CMakeLists.txt b/llvm/unittests/ExecutionEngine/JITLink/CMakeLists.txt
index e535bcf47fc801e..d4886754f0a9ca4 100644
--- a/llvm/unittests/ExecutionEngine/JITLink/CMakeLists.txt
+++ b/llvm/unittests/ExecutionEngine/JITLink/CMakeLists.txt
@@ -9,6 +9,7 @@ set(LLVM_LINK_COMPONENTS
 
 add_llvm_unittest(JITLinkTests
     AArch32Tests.cpp
+    AArch32ErrorTests.cpp
     EHFrameSupportTests.cpp
     JITLinkMocks.cpp
     LinkGraphTests.cpp

>From a242afea976338da7b93dffd1f8f4d6087a382cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Eymen=20=C3=9Cnay?= <eymenunay at outlook.com>
Date: Wed, 25 Oct 2023 16:39:43 +0300
Subject: [PATCH 3/4] replace Error functions with string matchers

---
 .../JITLink/AArch32ErrorTests.cpp             | 59 ++++---------------
 1 file changed, 10 insertions(+), 49 deletions(-)

diff --git a/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp b/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp
index 0a1155443345c3e..47b6b1c9d469746 100644
--- a/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp
+++ b/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp
@@ -18,37 +18,6 @@ using namespace llvm::jitlink::aarch32;
 using namespace llvm::support;
 using namespace llvm::support::endian;
 
-struct ThumbRelocation {
-  /// Create a read-only reference to a Thumb32 fixup.
-  ThumbRelocation(const char *FixupPtr)
-      : Hi{*reinterpret_cast<const support::ulittle16_t *>(FixupPtr)},
-        Lo{*reinterpret_cast<const support::ulittle16_t *>(FixupPtr + 2)} {}
-
-  const support::ulittle16_t &Hi; // First halfword
-  const support::ulittle16_t &Lo; // Second halfword
-};
-
-struct ArmRelocation {
-
-  ArmRelocation(const char *FixupPtr)
-      : Wd{*reinterpret_cast<const support::ulittle32_t *>(FixupPtr)} {}
-
-  const support::ulittle32_t &Wd;
-};
-
-std::string makeUnexpectedOpcodeError(const LinkGraph &G,
-                                      const ThumbRelocation &R,
-                                      Edge::Kind Kind) {
-  return formatv("Invalid opcode [ {0:x4}, {1:x4} ] for relocation: {2}",
-                 static_cast<uint16_t>(R.Hi), static_cast<uint16_t>(R.Lo),
-                 G.getEdgeKindName(Kind));
-}
-
-std::string makeUnexpectedOpcodeError(const LinkGraph &G,
-                                      const ArmRelocation &R, Edge::Kind Kind) {
-  return formatv("Invalid opcode {0:x8} for relocation: {1}",
-                 static_cast<uint32_t>(R.Wd), G.getEdgeKindName(Kind));
-}
 
 TEST(AArch32_ELF, readAddends) {
   auto G = std::make_unique<LinkGraph>("foo", Triple("armv7-linux-gnueabi"), 4,
@@ -66,47 +35,39 @@ TEST(AArch32_ELF, readAddends) {
 
   auto ArmCfg = getArmConfigForCPUArch(ARMBuildAttrs::v7);
 
-  auto makeReadAddendError = [](LinkGraph &G, Block &B, Edge &E) {
-    return ("In graph " + G.getName() + ", section " +
-            B.getSection().getName() +
-            " can not read implicit addend for aarch32 edge kind " +
-            G.getEdgeKindName(E.getKind()))
-        .str();
-  };
 
   EXPECT_THAT_EXPECTED(
       readAddend(*G, B, InvalidEdge, ArmCfg),
-      FailedWithMessage(makeReadAddendError(*G, B, InvalidEdge)));
+      FailedWithMessage(testing::HasSubstr("can not read implicit addend for aarch32 edge kind Keep-Alive")));
 
-  ArmRelocation R_Arm(B.getContent().data());
-  ThumbRelocation R_Thumb(B.getContent().data());
+  std::string ReadAddendError = "can not read implicit addend for aarch32 edge kind <Unrecognized edge kind>";
 
   for (Edge::Kind K = FirstDataRelocation; K < LastDataRelocation; K += 1) {
     Edge E(K, 0, TargetSymbol, 0);
     EXPECT_THAT_EXPECTED(readAddendData(*G, B, E), Succeeded());
     EXPECT_THAT_EXPECTED(readAddendArm(*G, B, E),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+                         FailedWithMessage(testing::HasSubstr(ReadAddendError)));
     EXPECT_THAT_EXPECTED(readAddendThumb(*G, B, E, ArmCfg),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+                         FailedWithMessage(testing::HasSubstr(ReadAddendError)));
   }
   for (Edge::Kind K = FirstArmRelocation; K < LastArmRelocation; K += 1) {
     Edge E(K, 0, TargetSymbol, 0);
     EXPECT_THAT_EXPECTED(readAddendData(*G, B, E),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+                         FailedWithMessage(testing::HasSubstr(ReadAddendError)));
     EXPECT_THAT_EXPECTED(
         readAddendArm(*G, B, E),
-        FailedWithMessage(makeUnexpectedOpcodeError(*G, R_Arm, K)));
+        FailedWithMessage(testing::StartsWith("Invalid opcode")));
     EXPECT_THAT_EXPECTED(readAddendThumb(*G, B, E, ArmCfg),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+                         FailedWithMessage(testing::HasSubstr(ReadAddendError)));
   }
   for (Edge::Kind K = FirstThumbRelocation; K < LastThumbRelocation; K += 1) {
     Edge E(K, 0, TargetSymbol, 0);
     EXPECT_THAT_EXPECTED(readAddendData(*G, B, E),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+                         FailedWithMessage(testing::HasSubstr(ReadAddendError)));
     EXPECT_THAT_EXPECTED(readAddendArm(*G, B, E),
-                         FailedWithMessage(makeReadAddendError(*G, B, E)));
+                         FailedWithMessage(testing::HasSubstr(ReadAddendError)));
     EXPECT_THAT_EXPECTED(
         readAddendThumb(*G, B, E, ArmCfg),
-        FailedWithMessage(makeUnexpectedOpcodeError(*G, R_Thumb, K)));
+        FailedWithMessage(testing::StartsWith("Invalid opcode")));
   }
 }

>From 714b0da7a8bfe190b3c078cded5d834b599bea23 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Eymen=20=C3=9Cnay?= <eymenunay at outlook.com>
Date: Wed, 25 Oct 2023 18:29:00 +0300
Subject: [PATCH 4/4] Add applyFixup Error checking

---
 .../JITLink/AArch32ErrorTests.cpp             | 70 ++++++++++++++++---
 .../ExecutionEngine/JITLink/AArch32Tests.cpp  |  1 -
 2 files changed, 59 insertions(+), 12 deletions(-)

diff --git a/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp b/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp
index 47b6b1c9d469746..1b784fd9297aeaf 100644
--- a/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp
+++ b/llvm/unittests/ExecutionEngine/JITLink/AArch32ErrorTests.cpp
@@ -1,4 +1,4 @@
-//===------- AArch32ErrorTests.cpp - Unit tests for AArch32 error handling -===//
+//===------- AArch32ErrorTests.cpp - Test AArch32 error handling ----------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.
@@ -18,24 +18,23 @@ using namespace llvm::jitlink::aarch32;
 using namespace llvm::support;
 using namespace llvm::support::endian;
 
+auto G = std::make_unique<LinkGraph>("foo", Triple("armv7-linux-gnueabi"), 4,
+                                     llvm::endianness::little,
+                                     getGenericEdgeKindName);
+auto &Sec =
+    G->createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
+orc::ExecutorAddr B1Addr(0x1000);
 
-TEST(AArch32_ELF, readAddends) {
-  auto G = std::make_unique<LinkGraph>("foo", Triple("armv7-linux-gnueabi"), 4,
-                                       llvm::endianness::little,
-                                       getGenericEdgeKindName);
+auto ArmCfg = getArmConfigForCPUArch(ARMBuildAttrs::v7);
+
+TEST(AArch32_ELF, readAddendErrors) {
 
   ArrayRef<char> Content = "hello, world!";
-  auto &Sec =
-      G->createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
-  orc::ExecutorAddr B1Addr(0x1000);
   auto &B = G->createContentBlock(Sec, Content, B1Addr, 4, 0);
 
   Symbol &TargetSymbol = G->addAnonymousSymbol(B, 0, 4, false, false);
   Edge InvalidEdge(FirstDataRelocation - 1, 0, TargetSymbol, 0);
 
-  auto ArmCfg = getArmConfigForCPUArch(ARMBuildAttrs::v7);
-
-
   EXPECT_THAT_EXPECTED(
       readAddend(*G, B, InvalidEdge, ArmCfg),
       FailedWithMessage(testing::HasSubstr("can not read implicit addend for aarch32 edge kind Keep-Alive")));
@@ -71,3 +70,52 @@ TEST(AArch32_ELF, readAddends) {
         FailedWithMessage(testing::StartsWith("Invalid opcode")));
   }
 }
+
+TEST(AArch32_ELF, applyFixupErrors) {
+  char ContentArray[] = "hello, world!";
+  MutableArrayRef<char> MutableContent(ContentArray);
+  auto &B = G->createMutableContentBlock(Sec, MutableContent, B1Addr, 4, 0);
+
+  Symbol &TargetSymbol = G->addAnonymousSymbol(B, 0, 4, false, false);
+  Edge InvalidEdge(FirstDataRelocation - 1, 0, TargetSymbol, 0);
+
+  EXPECT_THAT_ERROR(applyFixup(*G, B, InvalidEdge, ArmCfg),
+                    FailedWithMessage(testing::HasSubstr(
+                        "encountered unfixable aarch32 edge kind Keep-Alive")));
+
+  std::string UnfixableEdgeError =
+      "encountered unfixable aarch32 edge kind <Unrecognized edge kind>";
+
+  for (Edge::Kind K = FirstDataRelocation; K < LastDataRelocation; K += 1) {
+    Edge E(K, 0, TargetSymbol, 0);
+    EXPECT_THAT_ERROR(applyFixupData(*G, B, E), Succeeded());
+    EXPECT_THAT_ERROR(
+        applyFixupArm(*G, B, E),
+        FailedWithMessage(testing::HasSubstr(UnfixableEdgeError)));
+    EXPECT_THAT_ERROR(
+        applyFixupThumb(*G, B, E, ArmCfg),
+        FailedWithMessage(testing::HasSubstr(UnfixableEdgeError)));
+  }
+  for (Edge::Kind K = FirstArmRelocation; K < LastArmRelocation; K += 1) {
+    Edge E(K, 0, TargetSymbol, 0);
+    EXPECT_THAT_ERROR(
+        applyFixupData(*G, B, E),
+        FailedWithMessage(testing::HasSubstr(UnfixableEdgeError)));
+    EXPECT_THAT_ERROR(applyFixupArm(*G, B, E),
+                      FailedWithMessage(testing::StartsWith("Invalid opcode")));
+    EXPECT_THAT_ERROR(
+        applyFixupThumb(*G, B, E, ArmCfg),
+        FailedWithMessage(testing::HasSubstr(UnfixableEdgeError)));
+  }
+  for (Edge::Kind K = FirstThumbRelocation; K < LastThumbRelocation; K += 1) {
+    Edge E(K, 0, TargetSymbol, 0);
+    EXPECT_THAT_ERROR(
+        applyFixupData(*G, B, E),
+        FailedWithMessage(testing::HasSubstr(UnfixableEdgeError)));
+    EXPECT_THAT_ERROR(
+        applyFixupArm(*G, B, E),
+        FailedWithMessage(testing::HasSubstr(UnfixableEdgeError)));
+    EXPECT_THAT_ERROR(applyFixupThumb(*G, B, E, ArmCfg),
+                      FailedWithMessage(testing::StartsWith("Invalid opcode")));
+  }
+}
diff --git a/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp b/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp
index 5bcfb85ac44bfb5..0ff1798fc73bb4e 100644
--- a/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp
+++ b/llvm/unittests/ExecutionEngine/JITLink/AArch32Tests.cpp
@@ -68,7 +68,6 @@ TEST(AArch32_ELF, EdgeKinds) {
   }
 }
 
-
 namespace llvm {
 namespace jitlink {
 namespace aarch32 {



More information about the llvm-commits mailing list