ModuleUtils.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. //===-- ModuleUtils.cpp - Functions to manipulate Modules -----------------===//
  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. // This family of functions perform manipulations on Modules.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Transforms/Utils/ModuleUtils.h"
  13. #include "llvm/Analysis/VectorUtils.h"
  14. #include "llvm/IR/DerivedTypes.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/IRBuilder.h"
  17. #include "llvm/IR/MDBuilder.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include "llvm/Support/xxhash.h"
  21. using namespace llvm;
  22. #define DEBUG_TYPE "moduleutils"
  23. static void appendToGlobalArray(StringRef ArrayName, Module &M, Function *F,
  24. int Priority, Constant *Data) {
  25. IRBuilder<> IRB(M.getContext());
  26. FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
  27. // Get the current set of static global constructors and add the new ctor
  28. // to the list.
  29. SmallVector<Constant *, 16> CurrentCtors;
  30. StructType *EltTy = StructType::get(
  31. IRB.getInt32Ty(), PointerType::get(FnTy, F->getAddressSpace()),
  32. IRB.getInt8PtrTy());
  33. if (GlobalVariable *GVCtor = M.getNamedGlobal(ArrayName)) {
  34. if (Constant *Init = GVCtor->getInitializer()) {
  35. unsigned n = Init->getNumOperands();
  36. CurrentCtors.reserve(n + 1);
  37. for (unsigned i = 0; i != n; ++i)
  38. CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
  39. }
  40. GVCtor->eraseFromParent();
  41. }
  42. // Build a 3 field global_ctor entry. We don't take a comdat key.
  43. Constant *CSVals[3];
  44. CSVals[0] = IRB.getInt32(Priority);
  45. CSVals[1] = F;
  46. CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getInt8PtrTy())
  47. : Constant::getNullValue(IRB.getInt8PtrTy());
  48. Constant *RuntimeCtorInit =
  49. ConstantStruct::get(EltTy, ArrayRef(CSVals, EltTy->getNumElements()));
  50. CurrentCtors.push_back(RuntimeCtorInit);
  51. // Create a new initializer.
  52. ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
  53. Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
  54. // Create the new global variable and replace all uses of
  55. // the old global variable with the new one.
  56. (void)new GlobalVariable(M, NewInit->getType(), false,
  57. GlobalValue::AppendingLinkage, NewInit, ArrayName);
  58. }
  59. void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data) {
  60. appendToGlobalArray("llvm.global_ctors", M, F, Priority, Data);
  61. }
  62. void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data) {
  63. appendToGlobalArray("llvm.global_dtors", M, F, Priority, Data);
  64. }
  65. static void collectUsedGlobals(GlobalVariable *GV,
  66. SmallSetVector<Constant *, 16> &Init) {
  67. if (!GV || !GV->hasInitializer())
  68. return;
  69. auto *CA = cast<ConstantArray>(GV->getInitializer());
  70. for (Use &Op : CA->operands())
  71. Init.insert(cast<Constant>(Op));
  72. }
  73. static void appendToUsedList(Module &M, StringRef Name, ArrayRef<GlobalValue *> Values) {
  74. GlobalVariable *GV = M.getGlobalVariable(Name);
  75. SmallSetVector<Constant *, 16> Init;
  76. collectUsedGlobals(GV, Init);
  77. if (GV)
  78. GV->eraseFromParent();
  79. Type *ArrayEltTy = llvm::Type::getInt8PtrTy(M.getContext());
  80. for (auto *V : Values)
  81. Init.insert(ConstantExpr::getPointerBitCastOrAddrSpaceCast(V, ArrayEltTy));
  82. if (Init.empty())
  83. return;
  84. ArrayType *ATy = ArrayType::get(ArrayEltTy, Init.size());
  85. GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
  86. ConstantArray::get(ATy, Init.getArrayRef()),
  87. Name);
  88. GV->setSection("llvm.metadata");
  89. }
  90. void llvm::appendToUsed(Module &M, ArrayRef<GlobalValue *> Values) {
  91. appendToUsedList(M, "llvm.used", Values);
  92. }
  93. void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
  94. appendToUsedList(M, "llvm.compiler.used", Values);
  95. }
  96. static void removeFromUsedList(Module &M, StringRef Name,
  97. function_ref<bool(Constant *)> ShouldRemove) {
  98. GlobalVariable *GV = M.getNamedGlobal(Name);
  99. if (!GV)
  100. return;
  101. SmallSetVector<Constant *, 16> Init;
  102. collectUsedGlobals(GV, Init);
  103. Type *ArrayEltTy = cast<ArrayType>(GV->getValueType())->getElementType();
  104. SmallVector<Constant *, 16> NewInit;
  105. for (Constant *MaybeRemoved : Init) {
  106. if (!ShouldRemove(MaybeRemoved->stripPointerCasts()))
  107. NewInit.push_back(MaybeRemoved);
  108. }
  109. if (!NewInit.empty()) {
  110. ArrayType *ATy = ArrayType::get(ArrayEltTy, NewInit.size());
  111. GlobalVariable *NewGV =
  112. new GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
  113. ConstantArray::get(ATy, NewInit), "", GV,
  114. GV->getThreadLocalMode(), GV->getAddressSpace());
  115. NewGV->setSection(GV->getSection());
  116. NewGV->takeName(GV);
  117. }
  118. GV->eraseFromParent();
  119. }
  120. void llvm::removeFromUsedLists(Module &M,
  121. function_ref<bool(Constant *)> ShouldRemove) {
  122. removeFromUsedList(M, "llvm.used", ShouldRemove);
  123. removeFromUsedList(M, "llvm.compiler.used", ShouldRemove);
  124. }
  125. void llvm::setKCFIType(Module &M, Function &F, StringRef MangledType) {
  126. if (!M.getModuleFlag("kcfi"))
  127. return;
  128. // Matches CodeGenModule::CreateKCFITypeId in Clang.
  129. LLVMContext &Ctx = M.getContext();
  130. MDBuilder MDB(Ctx);
  131. F.setMetadata(
  132. LLVMContext::MD_kcfi_type,
  133. MDNode::get(Ctx, MDB.createConstant(ConstantInt::get(
  134. Type::getInt32Ty(Ctx),
  135. static_cast<uint32_t>(xxHash64(MangledType))))));
  136. // If the module was compiled with -fpatchable-function-entry, ensure
  137. // we use the same patchable-function-prefix.
  138. if (auto *MD = mdconst::extract_or_null<ConstantInt>(
  139. M.getModuleFlag("kcfi-offset"))) {
  140. if (unsigned Offset = MD->getZExtValue())
  141. F.addFnAttr("patchable-function-prefix", std::to_string(Offset));
  142. }
  143. }
  144. FunctionCallee llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
  145. ArrayRef<Type *> InitArgTypes,
  146. bool Weak) {
  147. assert(!InitName.empty() && "Expected init function name");
  148. auto *VoidTy = Type::getVoidTy(M.getContext());
  149. auto *FnTy = FunctionType::get(VoidTy, InitArgTypes, false);
  150. auto FnCallee = M.getOrInsertFunction(InitName, FnTy);
  151. auto *Fn = cast<Function>(FnCallee.getCallee());
  152. if (Weak && Fn->isDeclaration())
  153. Fn->setLinkage(Function::ExternalWeakLinkage);
  154. return FnCallee;
  155. }
  156. Function *llvm::createSanitizerCtor(Module &M, StringRef CtorName) {
  157. Function *Ctor = Function::createWithDefaultAttr(
  158. FunctionType::get(Type::getVoidTy(M.getContext()), false),
  159. GlobalValue::InternalLinkage, M.getDataLayout().getProgramAddressSpace(),
  160. CtorName, &M);
  161. Ctor->addFnAttr(Attribute::NoUnwind);
  162. setKCFIType(M, *Ctor, "_ZTSFvvE"); // void (*)(void)
  163. BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
  164. ReturnInst::Create(M.getContext(), CtorBB);
  165. // Ensure Ctor cannot be discarded, even if in a comdat.
  166. appendToUsed(M, {Ctor});
  167. return Ctor;
  168. }
  169. std::pair<Function *, FunctionCallee> llvm::createSanitizerCtorAndInitFunctions(
  170. Module &M, StringRef CtorName, StringRef InitName,
  171. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  172. StringRef VersionCheckName, bool Weak) {
  173. assert(!InitName.empty() && "Expected init function name");
  174. assert(InitArgs.size() == InitArgTypes.size() &&
  175. "Sanitizer's init function expects different number of arguments");
  176. FunctionCallee InitFunction =
  177. declareSanitizerInitFunction(M, InitName, InitArgTypes, Weak);
  178. Function *Ctor = createSanitizerCtor(M, CtorName);
  179. IRBuilder<> IRB(M.getContext());
  180. BasicBlock *RetBB = &Ctor->getEntryBlock();
  181. if (Weak) {
  182. RetBB->setName("ret");
  183. auto *EntryBB = BasicBlock::Create(M.getContext(), "entry", Ctor, RetBB);
  184. auto *CallInitBB =
  185. BasicBlock::Create(M.getContext(), "callfunc", Ctor, RetBB);
  186. auto *InitFn = cast<Function>(InitFunction.getCallee());
  187. auto *InitFnPtr =
  188. PointerType::get(InitFn->getType(), InitFn->getAddressSpace());
  189. IRB.SetInsertPoint(EntryBB);
  190. Value *InitNotNull =
  191. IRB.CreateICmpNE(InitFn, ConstantPointerNull::get(InitFnPtr));
  192. IRB.CreateCondBr(InitNotNull, CallInitBB, RetBB);
  193. IRB.SetInsertPoint(CallInitBB);
  194. } else {
  195. IRB.SetInsertPoint(RetBB->getTerminator());
  196. }
  197. IRB.CreateCall(InitFunction, InitArgs);
  198. if (!VersionCheckName.empty()) {
  199. FunctionCallee VersionCheckFunction = M.getOrInsertFunction(
  200. VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
  201. AttributeList());
  202. IRB.CreateCall(VersionCheckFunction, {});
  203. }
  204. if (Weak)
  205. IRB.CreateBr(RetBB);
  206. return std::make_pair(Ctor, InitFunction);
  207. }
  208. std::pair<Function *, FunctionCallee>
  209. llvm::getOrCreateSanitizerCtorAndInitFunctions(
  210. Module &M, StringRef CtorName, StringRef InitName,
  211. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  212. function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
  213. StringRef VersionCheckName, bool Weak) {
  214. assert(!CtorName.empty() && "Expected ctor function name");
  215. if (Function *Ctor = M.getFunction(CtorName))
  216. // FIXME: Sink this logic into the module, similar to the handling of
  217. // globals. This will make moving to a concurrent model much easier.
  218. if (Ctor->arg_empty() ||
  219. Ctor->getReturnType() == Type::getVoidTy(M.getContext()))
  220. return {Ctor,
  221. declareSanitizerInitFunction(M, InitName, InitArgTypes, Weak)};
  222. Function *Ctor;
  223. FunctionCallee InitFunction;
  224. std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions(
  225. M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName, Weak);
  226. FunctionsCreatedCallback(Ctor, InitFunction);
  227. return std::make_pair(Ctor, InitFunction);
  228. }
  229. void llvm::filterDeadComdatFunctions(
  230. SmallVectorImpl<Function *> &DeadComdatFunctions) {
  231. SmallPtrSet<Function *, 32> MaybeDeadFunctions;
  232. SmallPtrSet<Comdat *, 32> MaybeDeadComdats;
  233. for (Function *F : DeadComdatFunctions) {
  234. MaybeDeadFunctions.insert(F);
  235. if (Comdat *C = F->getComdat())
  236. MaybeDeadComdats.insert(C);
  237. }
  238. // Find comdats for which all users are dead now.
  239. SmallPtrSet<Comdat *, 32> DeadComdats;
  240. for (Comdat *C : MaybeDeadComdats) {
  241. auto IsUserDead = [&](GlobalObject *GO) {
  242. auto *F = dyn_cast<Function>(GO);
  243. return F && MaybeDeadFunctions.contains(F);
  244. };
  245. if (all_of(C->getUsers(), IsUserDead))
  246. DeadComdats.insert(C);
  247. }
  248. // Only keep functions which have no comdat or a dead comdat.
  249. erase_if(DeadComdatFunctions, [&](Function *F) {
  250. Comdat *C = F->getComdat();
  251. return C && !DeadComdats.contains(C);
  252. });
  253. }
  254. std::string llvm::getUniqueModuleId(Module *M) {
  255. MD5 Md5;
  256. bool ExportsSymbols = false;
  257. auto AddGlobal = [&](GlobalValue &GV) {
  258. if (GV.isDeclaration() || GV.getName().startswith("llvm.") ||
  259. !GV.hasExternalLinkage() || GV.hasComdat())
  260. return;
  261. ExportsSymbols = true;
  262. Md5.update(GV.getName());
  263. Md5.update(ArrayRef<uint8_t>{0});
  264. };
  265. for (auto &F : *M)
  266. AddGlobal(F);
  267. for (auto &GV : M->globals())
  268. AddGlobal(GV);
  269. for (auto &GA : M->aliases())
  270. AddGlobal(GA);
  271. for (auto &IF : M->ifuncs())
  272. AddGlobal(IF);
  273. if (!ExportsSymbols)
  274. return "";
  275. MD5::MD5Result R;
  276. Md5.final(R);
  277. SmallString<32> Str;
  278. MD5::stringifyResult(R, Str);
  279. return ("." + Str).str();
  280. }
  281. void VFABI::setVectorVariantNames(CallInst *CI,
  282. ArrayRef<std::string> VariantMappings) {
  283. if (VariantMappings.empty())
  284. return;
  285. SmallString<256> Buffer;
  286. llvm::raw_svector_ostream Out(Buffer);
  287. for (const std::string &VariantMapping : VariantMappings)
  288. Out << VariantMapping << ",";
  289. // Get rid of the trailing ','.
  290. assert(!Buffer.str().empty() && "Must have at least one char.");
  291. Buffer.pop_back();
  292. Module *M = CI->getModule();
  293. #ifndef NDEBUG
  294. for (const std::string &VariantMapping : VariantMappings) {
  295. LLVM_DEBUG(dbgs() << "VFABI: adding mapping '" << VariantMapping << "'\n");
  296. std::optional<VFInfo> VI = VFABI::tryDemangleForVFABI(VariantMapping, *M);
  297. assert(VI && "Cannot add an invalid VFABI name.");
  298. assert(M->getNamedValue(VI->VectorName) &&
  299. "Cannot add variant to attribute: "
  300. "vector function declaration is missing.");
  301. }
  302. #endif
  303. CI->addFnAttr(
  304. Attribute::get(M->getContext(), MappingsAttrName, Buffer.str()));
  305. }
  306. void llvm::embedBufferInModule(Module &M, MemoryBufferRef Buf,
  307. StringRef SectionName, Align Alignment) {
  308. // Embed the memory buffer into the module.
  309. Constant *ModuleConstant = ConstantDataArray::get(
  310. M.getContext(), ArrayRef(Buf.getBufferStart(), Buf.getBufferSize()));
  311. GlobalVariable *GV = new GlobalVariable(
  312. M, ModuleConstant->getType(), true, GlobalValue::PrivateLinkage,
  313. ModuleConstant, "llvm.embedded.object");
  314. GV->setSection(SectionName);
  315. GV->setAlignment(Alignment);
  316. LLVMContext &Ctx = M.getContext();
  317. NamedMDNode *MD = M.getOrInsertNamedMetadata("llvm.embedded.objects");
  318. Metadata *MDVals[] = {ConstantAsMetadata::get(GV),
  319. MDString::get(Ctx, SectionName)};
  320. MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
  321. GV->setMetadata(LLVMContext::MD_exclude, llvm::MDNode::get(Ctx, {}));
  322. appendToCompilerUsed(M, GV);
  323. }
  324. bool llvm::lowerGlobalIFuncUsersAsGlobalCtor(
  325. Module &M, ArrayRef<GlobalIFunc *> FilteredIFuncsToLower) {
  326. SmallVector<GlobalIFunc *, 32> AllIFuncs;
  327. ArrayRef<GlobalIFunc *> IFuncsToLower = FilteredIFuncsToLower;
  328. if (FilteredIFuncsToLower.empty()) { // Default to lowering all ifuncs
  329. for (GlobalIFunc &GI : M.ifuncs())
  330. AllIFuncs.push_back(&GI);
  331. IFuncsToLower = AllIFuncs;
  332. }
  333. bool UnhandledUsers = false;
  334. LLVMContext &Ctx = M.getContext();
  335. const DataLayout &DL = M.getDataLayout();
  336. PointerType *TableEntryTy =
  337. Ctx.supportsTypedPointers()
  338. ? PointerType::get(Type::getInt8Ty(Ctx), DL.getProgramAddressSpace())
  339. : PointerType::get(Ctx, DL.getProgramAddressSpace());
  340. ArrayType *FuncPtrTableTy =
  341. ArrayType::get(TableEntryTy, IFuncsToLower.size());
  342. Align PtrAlign = DL.getABITypeAlign(TableEntryTy);
  343. // Create a global table of function pointers we'll initialize in a global
  344. // constructor.
  345. auto *FuncPtrTable = new GlobalVariable(
  346. M, FuncPtrTableTy, false, GlobalValue::InternalLinkage,
  347. PoisonValue::get(FuncPtrTableTy), "", nullptr,
  348. GlobalVariable::NotThreadLocal, DL.getDefaultGlobalsAddressSpace());
  349. FuncPtrTable->setAlignment(PtrAlign);
  350. // Create a function to initialize the function pointer table.
  351. Function *NewCtor = Function::Create(
  352. FunctionType::get(Type::getVoidTy(Ctx), false), Function::InternalLinkage,
  353. DL.getProgramAddressSpace(), "", &M);
  354. BasicBlock *BB = BasicBlock::Create(Ctx, "", NewCtor);
  355. IRBuilder<> InitBuilder(BB);
  356. size_t TableIndex = 0;
  357. for (GlobalIFunc *GI : IFuncsToLower) {
  358. Function *ResolvedFunction = GI->getResolverFunction();
  359. // We don't know what to pass to a resolver function taking arguments
  360. //
  361. // FIXME: Is this even valid? clang and gcc don't complain but this
  362. // probably should be invalid IR. We could just pass through undef.
  363. if (!std::empty(ResolvedFunction->getFunctionType()->params())) {
  364. LLVM_DEBUG(dbgs() << "Not lowering ifunc resolver function "
  365. << ResolvedFunction->getName() << " with parameters\n");
  366. UnhandledUsers = true;
  367. continue;
  368. }
  369. // Initialize the function pointer table.
  370. CallInst *ResolvedFunc = InitBuilder.CreateCall(ResolvedFunction);
  371. Value *Casted = InitBuilder.CreatePointerCast(ResolvedFunc, TableEntryTy);
  372. Constant *GEP = cast<Constant>(InitBuilder.CreateConstInBoundsGEP2_32(
  373. FuncPtrTableTy, FuncPtrTable, 0, TableIndex++));
  374. InitBuilder.CreateAlignedStore(Casted, GEP, PtrAlign);
  375. // Update all users to load a pointer from the global table.
  376. for (User *User : make_early_inc_range(GI->users())) {
  377. Instruction *UserInst = dyn_cast<Instruction>(User);
  378. if (!UserInst) {
  379. // TODO: Should handle constantexpr casts in user instructions. Probably
  380. // can't do much about constant initializers.
  381. UnhandledUsers = true;
  382. continue;
  383. }
  384. IRBuilder<> UseBuilder(UserInst);
  385. LoadInst *ResolvedTarget =
  386. UseBuilder.CreateAlignedLoad(TableEntryTy, GEP, PtrAlign);
  387. Value *ResolvedCast =
  388. UseBuilder.CreatePointerCast(ResolvedTarget, GI->getType());
  389. UserInst->replaceUsesOfWith(GI, ResolvedCast);
  390. }
  391. // If we handled all users, erase the ifunc.
  392. if (GI->use_empty())
  393. GI->eraseFromParent();
  394. }
  395. InitBuilder.CreateRetVoid();
  396. PointerType *ConstantDataTy = Ctx.supportsTypedPointers()
  397. ? PointerType::get(Type::getInt8Ty(Ctx), 0)
  398. : PointerType::get(Ctx, 0);
  399. // TODO: Is this the right priority? Probably should be before any other
  400. // constructors?
  401. const int Priority = 10;
  402. appendToGlobalCtors(M, NewCtor, Priority,
  403. ConstantPointerNull::get(ConstantDataTy));
  404. return UnhandledUsers;
  405. }