1 //===------ LinkGraphTests.cpp - Unit tests for core JITLink classes ------===// 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 9 #include "llvm/ADT/STLExtras.h" 10 #include "llvm/ExecutionEngine/JITLink/JITLink.h" 11 #include "llvm/Support/Endian.h" 12 #include "llvm/Support/Memory.h" 13 #include "gtest/gtest.h" 14 15 using namespace llvm; 16 using namespace llvm::jitlink; 17 18 static auto RWFlags = 19 sys::Memory::ProtectionFlags(sys::Memory::MF_READ | sys::Memory::MF_WRITE); 20 21 static const char BlockContentBytes[] = { 22 0x54, 0x68, 0x65, 0x72, 0x65, 0x20, 0x77, 0x61, 0x73, 0x20, 0x6d, 0x6f, 23 0x76, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x20, 0x61, 0x74, 0x20, 0x74, 0x68, 24 0x65, 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2c, 0x20, 0x66, 25 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x6f, 0x72, 0x64, 0x20, 26 0x68, 0x61, 0x64, 0x20, 0x70, 0x61, 0x73, 0x73, 0x65, 0x64, 0x20, 0x61, 27 0x72, 0x6f, 0x75, 0x6e, 0x64, 0x0a, 0x54, 0x68, 0x61, 0x74, 0x20, 0x74, 28 0x68, 0x65, 0x20, 0x63, 0x6f, 0x6c, 0x74, 0x20, 0x66, 0x72, 0x6f, 0x6d, 29 0x20, 0x4f, 0x6c, 0x64, 0x20, 0x52, 0x65, 0x67, 0x72, 0x65, 0x74, 0x20, 30 0x68, 0x61, 0x64, 0x20, 0x67, 0x6f, 0x74, 0x20, 0x61, 0x77, 0x61, 0x79, 31 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20, 0x68, 0x61, 0x64, 0x20, 0x6a, 0x6f, 32 0x69, 0x6e, 0x65, 0x64, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x69, 0x6c, 33 0x64, 0x20, 0x62, 0x75, 0x73, 0x68, 0x20, 0x68, 0x6f, 0x72, 0x73, 0x65, 34 0x73, 0x20, 0x2d, 0x2d, 0x20, 0x68, 0x65, 0x20, 0x77, 0x61, 0x73, 0x20, 35 0x77, 0x6f, 0x72, 0x74, 0x68, 0x20, 0x61, 0x20, 0x74, 0x68, 0x6f, 0x75, 36 0x73, 0x61, 0x6e, 0x64, 0x20, 0x70, 0x6f, 0x75, 0x6e, 0x64, 0x2c, 0x0a, 37 0x53, 0x6f, 0x20, 0x61, 0x6c, 0x6c, 0x20, 0x74, 0x68, 0x65, 0x20, 0x63, 38 0x72, 0x61, 0x63, 0x6b, 0x73, 0x20, 0x68, 0x61, 0x64, 0x20, 0x67, 0x61, 39 0x74, 0x68, 0x65, 0x72, 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x74, 0x68, 40 0x65, 0x20, 0x66, 0x72, 0x61, 0x79, 0x2e, 0x0a, 0x41, 0x6c, 0x6c, 0x20, 41 0x74, 0x68, 0x65, 0x20, 0x74, 0x72, 0x69, 0x65, 0x64, 0x20, 0x61, 0x6e, 42 0x64, 0x20, 0x6e, 0x6f, 0x74, 0x65, 0x64, 0x20, 0x72, 0x69, 0x64, 0x65, 43 0x72, 0x73, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x74, 0x68, 0x65, 0x20, 44 0x73, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x20, 0x6e, 0x65, 0x61, 45 0x72, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x66, 0x61, 0x72, 0x0a, 0x48, 0x61, 46 0x64, 0x20, 0x6d, 0x75, 0x73, 0x74, 0x65, 0x72, 0x65, 0x64, 0x20, 0x61, 47 0x74, 0x20, 0x74, 0x68, 0x65, 0x20, 0x68, 0x6f, 0x6d, 0x65, 0x73, 0x74, 48 0x65, 0x61, 0x64, 0x20, 0x6f, 0x76, 0x65, 0x72, 0x6e, 0x69, 0x67, 0x68, 49 0x74, 0x2c, 0x0a, 0x46, 0x6f, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x62, 50 0x75, 0x73, 0x68, 0x6d, 0x65, 0x6e, 0x20, 0x6c, 0x6f, 0x76, 0x65, 0x20, 51 0x68, 0x61, 0x72, 0x64, 0x20, 0x72, 0x69, 0x64, 0x69, 0x6e, 0x67, 0x20, 52 0x77, 0x68, 0x65, 0x72, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x77, 0x69, 53 0x6c, 0x64, 0x20, 0x62, 0x75, 0x73, 0x68, 0x20, 0x68, 0x6f, 0x72, 0x73, 54 0x65, 0x73, 0x20, 0x61, 0x72, 0x65, 0x2c, 0x0a, 0x41, 0x6e, 0x64, 0x20, 55 0x74, 0x68, 0x65, 0x20, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x2d, 0x68, 0x6f, 56 0x72, 0x73, 0x65, 0x20, 0x73, 0x6e, 0x75, 0x66, 0x66, 0x73, 0x20, 0x74, 57 0x68, 0x65, 0x20, 0x62, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x20, 0x77, 0x69, 58 0x74, 0x68, 0x20, 0x64, 0x65, 0x6c, 0x69, 0x67, 0x68, 0x74, 0x2e, 0x00}; 59 60 static ArrayRef<char> BlockContent(BlockContentBytes); 61 62 TEST(LinkGraphTest, Construction) { 63 // Check that LinkGraph construction works as expected. 64 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, 65 getGenericEdgeKindName); 66 EXPECT_EQ(G.getName(), "foo"); 67 EXPECT_EQ(G.getTargetTriple().str(), "x86_64-apple-darwin"); 68 EXPECT_EQ(G.getPointerSize(), 8U); 69 EXPECT_EQ(G.getEndianness(), support::little); 70 EXPECT_TRUE(llvm::empty(G.external_symbols())); 71 EXPECT_TRUE(llvm::empty(G.absolute_symbols())); 72 EXPECT_TRUE(llvm::empty(G.defined_symbols())); 73 EXPECT_TRUE(llvm::empty(G.blocks())); 74 } 75 76 TEST(LinkGraphTest, AddressAccess) { 77 // Check that we can get addresses for blocks, symbols, and edges. 78 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, 79 getGenericEdgeKindName); 80 81 auto &Sec1 = G.createSection("__data.1", RWFlags); 82 auto &B1 = G.createContentBlock(Sec1, BlockContent, 0x1000, 8, 0); 83 auto &S1 = G.addDefinedSymbol(B1, 4, "S1", 4, Linkage::Strong, Scope::Default, 84 false, false); 85 B1.addEdge(Edge::FirstRelocation, 8, S1, 0); 86 auto &E1 = *B1.edges().begin(); 87 88 EXPECT_EQ(B1.getAddress(), 0x1000U) << "Incorrect block address"; 89 EXPECT_EQ(S1.getAddress(), 0x1004U) << "Incorrect symbol address"; 90 EXPECT_EQ(B1.getFixupAddress(E1), 0x1008U) << "Incorrect fixup address"; 91 } 92 93 TEST(LinkGraphTest, BlockAndSymbolIteration) { 94 // Check that we can iterate over blocks within Sections and across sections. 95 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, 96 getGenericEdgeKindName); 97 auto &Sec1 = G.createSection("__data.1", RWFlags); 98 auto &B1 = G.createContentBlock(Sec1, BlockContent, 0x1000, 8, 0); 99 auto &B2 = G.createContentBlock(Sec1, BlockContent, 0x2000, 8, 0); 100 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", 4, Linkage::Strong, Scope::Default, 101 false, false); 102 auto &S2 = G.addDefinedSymbol(B2, 4, "S2", 4, Linkage::Strong, Scope::Default, 103 false, false); 104 105 auto &Sec2 = G.createSection("__data.2", RWFlags); 106 auto &B3 = G.createContentBlock(Sec2, BlockContent, 0x3000, 8, 0); 107 auto &B4 = G.createContentBlock(Sec2, BlockContent, 0x4000, 8, 0); 108 auto &S3 = G.addDefinedSymbol(B3, 0, "S3", 4, Linkage::Strong, Scope::Default, 109 false, false); 110 auto &S4 = G.addDefinedSymbol(B4, 4, "S4", 4, Linkage::Strong, Scope::Default, 111 false, false); 112 113 // Check that iteration of blocks within a section behaves as expected. 114 EXPECT_EQ(std::distance(Sec1.blocks().begin(), Sec1.blocks().end()), 2); 115 EXPECT_TRUE(llvm::count(Sec1.blocks(), &B1)); 116 EXPECT_TRUE(llvm::count(Sec1.blocks(), &B2)); 117 118 // Check that iteration of symbols within a section behaves as expected. 119 EXPECT_EQ(std::distance(Sec1.symbols().begin(), Sec1.symbols().end()), 2); 120 EXPECT_TRUE(llvm::count(Sec1.symbols(), &S1)); 121 EXPECT_TRUE(llvm::count(Sec1.symbols(), &S2)); 122 123 // Check that iteration of blocks across sections behaves as expected. 124 EXPECT_EQ(std::distance(G.blocks().begin(), G.blocks().end()), 4); 125 EXPECT_TRUE(llvm::count(G.blocks(), &B1)); 126 EXPECT_TRUE(llvm::count(G.blocks(), &B2)); 127 EXPECT_TRUE(llvm::count(G.blocks(), &B3)); 128 EXPECT_TRUE(llvm::count(G.blocks(), &B4)); 129 130 // Check that iteration of defined symbols across sections behaves as 131 // expected. 132 EXPECT_EQ( 133 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 4); 134 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S1)); 135 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S2)); 136 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S3)); 137 EXPECT_TRUE(llvm::count(G.defined_symbols(), &S4)); 138 } 139 140 TEST(LinkGraphTest, MakeExternal) { 141 // Check that we can make a defined symbol external. 142 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, 143 getGenericEdgeKindName); 144 auto &Sec = G.createSection("__data", RWFlags); 145 146 // Create an initial block. 147 auto &B1 = G.createContentBlock(Sec, BlockContent, 0x1000, 8, 0); 148 149 // Add a symbol to the block. 150 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", 4, Linkage::Strong, Scope::Default, 151 false, false); 152 153 EXPECT_TRUE(S1.isDefined()) << "Symbol should be defined"; 154 EXPECT_FALSE(S1.isExternal()) << "Symbol should not be external"; 155 EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute"; 156 EXPECT_TRUE(&S1.getBlock()) << "Symbol should have a non-null block"; 157 EXPECT_EQ(S1.getAddress(), 0x1000U) << "Unexpected symbol address"; 158 159 EXPECT_EQ( 160 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 1U) 161 << "Unexpected number of defined symbols"; 162 EXPECT_EQ( 163 std::distance(G.external_symbols().begin(), G.external_symbols().end()), 164 0U) 165 << "Unexpected number of external symbols"; 166 167 // Make S1 external, confirm that the its flags are updated and that it is 168 // moved from the defined symbols to the externals list. 169 G.makeExternal(S1); 170 171 EXPECT_FALSE(S1.isDefined()) << "Symbol should not be defined"; 172 EXPECT_TRUE(S1.isExternal()) << "Symbol should be external"; 173 EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute"; 174 EXPECT_EQ(S1.getAddress(), 0U) << "Unexpected symbol address"; 175 176 EXPECT_EQ( 177 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 0U) 178 << "Unexpected number of defined symbols"; 179 EXPECT_EQ( 180 std::distance(G.external_symbols().begin(), G.external_symbols().end()), 181 1U) 182 << "Unexpected number of external symbols"; 183 } 184 185 TEST(LinkGraphTest, MakeDefined) { 186 // Check that we can make an external symbol defined. 187 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, 188 getGenericEdgeKindName); 189 auto &Sec = G.createSection("__data", RWFlags); 190 191 // Create an initial block. 192 auto &B1 = G.createContentBlock(Sec, BlockContent, 0x1000, 8, 0); 193 194 // Add an external symbol. 195 auto &S1 = G.addExternalSymbol("S1", 4, Linkage::Strong); 196 197 EXPECT_FALSE(S1.isDefined()) << "Symbol should not be defined"; 198 EXPECT_TRUE(S1.isExternal()) << "Symbol should be external"; 199 EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute"; 200 EXPECT_EQ(S1.getAddress(), 0U) << "Unexpected symbol address"; 201 202 EXPECT_EQ( 203 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 0U) 204 << "Unexpected number of defined symbols"; 205 EXPECT_EQ( 206 std::distance(G.external_symbols().begin(), G.external_symbols().end()), 207 1U) 208 << "Unexpected number of external symbols"; 209 210 // Make S1 defined, confirm that its flags are updated and that it is 211 // moved from the defined symbols to the externals list. 212 G.makeDefined(S1, B1, 0, 4, Linkage::Strong, Scope::Default, false); 213 214 EXPECT_TRUE(S1.isDefined()) << "Symbol should be defined"; 215 EXPECT_FALSE(S1.isExternal()) << "Symbol should not be external"; 216 EXPECT_FALSE(S1.isAbsolute()) << "Symbol should not be absolute"; 217 EXPECT_TRUE(&S1.getBlock()) << "Symbol should have a non-null block"; 218 EXPECT_EQ(S1.getAddress(), 0x1000U) << "Unexpected symbol address"; 219 220 EXPECT_EQ( 221 std::distance(G.defined_symbols().begin(), G.defined_symbols().end()), 1U) 222 << "Unexpected number of defined symbols"; 223 EXPECT_EQ( 224 std::distance(G.external_symbols().begin(), G.external_symbols().end()), 225 0U) 226 << "Unexpected number of external symbols"; 227 } 228 229 TEST(LinkGraphTest, TransferDefinedSymbol) { 230 // Check that we can transfer a defined symbol from one block to another. 231 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, 232 getGenericEdgeKindName); 233 auto &Sec = G.createSection("__data", RWFlags); 234 235 // Create an initial block. 236 auto &B1 = G.createContentBlock(Sec, BlockContent, 0x1000, 8, 0); 237 auto &B2 = G.createContentBlock(Sec, BlockContent, 0x2000, 8, 0); 238 auto &B3 = G.createContentBlock(Sec, BlockContent.slice(0, 32), 0x3000, 8, 0); 239 240 // Add a symbol. 241 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", B1.getSize(), Linkage::Strong, 242 Scope::Default, false, false); 243 244 // Transfer with zero offset, explicit size. 245 G.transferDefinedSymbol(S1, B2, 0, 64); 246 247 EXPECT_EQ(&S1.getBlock(), &B2) << "Block was not updated"; 248 EXPECT_EQ(S1.getOffset(), 0U) << "Unexpected offset"; 249 EXPECT_EQ(S1.getSize(), 64U) << "Size was not updated"; 250 251 // Transfer with non-zero offset, implicit truncation. 252 G.transferDefinedSymbol(S1, B3, 16, None); 253 254 EXPECT_EQ(&S1.getBlock(), &B3) << "Block was not updated"; 255 EXPECT_EQ(S1.getOffset(), 16U) << "Offset was not updated"; 256 EXPECT_EQ(S1.getSize(), 16U) << "Size was not updated"; 257 } 258 259 TEST(LinkGraphTest, SplitBlock) { 260 // Check that the LinkGraph::splitBlock test works as expected. 261 LinkGraph G("foo", Triple("x86_64-apple-darwin"), 8, support::little, 262 getGenericEdgeKindName); 263 auto &Sec = G.createSection("__data", RWFlags); 264 265 // Create the block to split. 266 auto &B1 = G.createContentBlock(Sec, BlockContent, 0x1000, 8, 0); 267 268 // Add some symbols to the block. 269 auto &S1 = G.addDefinedSymbol(B1, 0, "S1", 4, Linkage::Strong, Scope::Default, 270 false, false); 271 auto &S2 = G.addDefinedSymbol(B1, 4, "S2", 4, Linkage::Strong, Scope::Default, 272 false, false); 273 auto &S3 = G.addDefinedSymbol(B1, 8, "S3", 4, Linkage::Strong, Scope::Default, 274 false, false); 275 auto &S4 = G.addDefinedSymbol(B1, 12, "S4", 4, Linkage::Strong, 276 Scope::Default, false, false); 277 278 // Add an extra block, EB, and target symbols, and use these to add edges 279 // from B1 to EB. 280 auto &EB = G.createContentBlock(Sec, BlockContent, 0x2000, 8, 0); 281 auto &ES1 = G.addDefinedSymbol(EB, 0, "TS1", 4, Linkage::Strong, 282 Scope::Default, false, false); 283 auto &ES2 = G.addDefinedSymbol(EB, 4, "TS2", 4, Linkage::Strong, 284 Scope::Default, false, false); 285 auto &ES3 = G.addDefinedSymbol(EB, 8, "TS3", 4, Linkage::Strong, 286 Scope::Default, false, false); 287 auto &ES4 = G.addDefinedSymbol(EB, 12, "TS4", 4, Linkage::Strong, 288 Scope::Default, false, false); 289 290 // Add edges from B1 to EB. 291 B1.addEdge(Edge::FirstRelocation, 0, ES1, 0); 292 B1.addEdge(Edge::FirstRelocation, 4, ES2, 0); 293 B1.addEdge(Edge::FirstRelocation, 8, ES3, 0); 294 B1.addEdge(Edge::FirstRelocation, 12, ES4, 0); 295 296 // Split B1. 297 auto &B2 = G.splitBlock(B1, 8); 298 299 // Check that the block addresses and content matches what we would expect. 300 EXPECT_EQ(B1.getAddress(), 0x1008U); 301 EXPECT_EQ(B1.getContent(), BlockContent.slice(8)); 302 303 EXPECT_EQ(B2.getAddress(), 0x1000U); 304 EXPECT_EQ(B2.getContent(), BlockContent.slice(0, 8)); 305 306 // Check that symbols in B1 were transferred as expected: 307 // We expect S1 and S2 to have been transferred to B2, and S3 and S4 to have 308 // remained attached to B1. Symbols S3 and S4 should have had their offsets 309 // slid to account for the change in address of B2. 310 EXPECT_EQ(&S1.getBlock(), &B2); 311 EXPECT_EQ(S1.getOffset(), 0U); 312 313 EXPECT_EQ(&S2.getBlock(), &B2); 314 EXPECT_EQ(S2.getOffset(), 4U); 315 316 EXPECT_EQ(&S3.getBlock(), &B1); 317 EXPECT_EQ(S3.getOffset(), 0U); 318 319 EXPECT_EQ(&S4.getBlock(), &B1); 320 EXPECT_EQ(S4.getOffset(), 4U); 321 322 // Check that edges in B1 have been transferred as expected: 323 // Both blocks should now have two edges each at offsets 0 and 4. 324 EXPECT_EQ(llvm::size(B1.edges()), 2); 325 if (size(B1.edges()) == 2) { 326 auto *E1 = &*B1.edges().begin(); 327 auto *E2 = &*(B1.edges().begin() + 1); 328 if (E2->getOffset() < E1->getOffset()) 329 std::swap(E1, E2); 330 EXPECT_EQ(E1->getOffset(), 0U); 331 EXPECT_EQ(E2->getOffset(), 4U); 332 } 333 334 EXPECT_EQ(llvm::size(B2.edges()), 2); 335 if (size(B2.edges()) == 2) { 336 auto *E1 = &*B2.edges().begin(); 337 auto *E2 = &*(B2.edges().begin() + 1); 338 if (E2->getOffset() < E1->getOffset()) 339 std::swap(E1, E2); 340 EXPECT_EQ(E1->getOffset(), 0U); 341 EXPECT_EQ(E2->getOffset(), 4U); 342 } 343 } 344