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 
13 #include "llvm/Transforms/Utils/ModuleUtils.h"
14 #include "llvm/Analysis/TargetLibraryInfo.h"
15 #include "llvm/Analysis/VectorUtils.h"
16 #include "llvm/IR/DerivedTypes.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/IRBuilder.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/raw_ostream.h"
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "moduleutils"
24 
25 static void appendToGlobalArray(const char *Array, Module &M, Function *F,
26                                 int Priority, Constant *Data) {
27   IRBuilder<> IRB(M.getContext());
28   FunctionType *FnTy = FunctionType::get(IRB.getVoidTy(), false);
29 
30   // Get the current set of static global constructors and add the new ctor
31   // to the list.
32   SmallVector<Constant *, 16> CurrentCtors;
33   StructType *EltTy = StructType::get(
34       IRB.getInt32Ty(), PointerType::getUnqual(FnTy), IRB.getInt8PtrTy());
35   if (GlobalVariable *GVCtor = M.getNamedGlobal(Array)) {
36     if (Constant *Init = GVCtor->getInitializer()) {
37       unsigned n = Init->getNumOperands();
38       CurrentCtors.reserve(n + 1);
39       for (unsigned i = 0; i != n; ++i)
40         CurrentCtors.push_back(cast<Constant>(Init->getOperand(i)));
41     }
42     GVCtor->eraseFromParent();
43   }
44 
45   // Build a 3 field global_ctor entry.  We don't take a comdat key.
46   Constant *CSVals[3];
47   CSVals[0] = IRB.getInt32(Priority);
48   CSVals[1] = F;
49   CSVals[2] = Data ? ConstantExpr::getPointerCast(Data, IRB.getInt8PtrTy())
50                    : Constant::getNullValue(IRB.getInt8PtrTy());
51   Constant *RuntimeCtorInit =
52       ConstantStruct::get(EltTy, makeArrayRef(CSVals, EltTy->getNumElements()));
53 
54   CurrentCtors.push_back(RuntimeCtorInit);
55 
56   // Create a new initializer.
57   ArrayType *AT = ArrayType::get(EltTy, CurrentCtors.size());
58   Constant *NewInit = ConstantArray::get(AT, CurrentCtors);
59 
60   // Create the new global variable and replace all uses of
61   // the old global variable with the new one.
62   (void)new GlobalVariable(M, NewInit->getType(), false,
63                            GlobalValue::AppendingLinkage, NewInit, Array);
64 }
65 
66 void llvm::appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data) {
67   appendToGlobalArray("llvm.global_ctors", M, F, Priority, Data);
68 }
69 
70 void llvm::appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data) {
71   appendToGlobalArray("llvm.global_dtors", M, F, Priority, Data);
72 }
73 
74 static void appendToUsedList(Module &M, StringRef Name, ArrayRef<GlobalValue *> Values) {
75   GlobalVariable *GV = M.getGlobalVariable(Name);
76   SmallPtrSet<Constant *, 16> InitAsSet;
77   SmallVector<Constant *, 16> Init;
78   if (GV) {
79     auto *CA = cast<ConstantArray>(GV->getInitializer());
80     for (auto &Op : CA->operands()) {
81       Constant *C = cast_or_null<Constant>(Op);
82       if (InitAsSet.insert(C).second)
83         Init.push_back(C);
84     }
85     GV->eraseFromParent();
86   }
87 
88   Type *Int8PtrTy = llvm::Type::getInt8PtrTy(M.getContext());
89   for (auto *V : Values) {
90     Constant *C = ConstantExpr::getBitCast(V, Int8PtrTy);
91     if (InitAsSet.insert(C).second)
92       Init.push_back(C);
93   }
94 
95   if (Init.empty())
96     return;
97 
98   ArrayType *ATy = ArrayType::get(Int8PtrTy, Init.size());
99   GV = new llvm::GlobalVariable(M, ATy, false, GlobalValue::AppendingLinkage,
100                                 ConstantArray::get(ATy, Init), Name);
101   GV->setSection("llvm.metadata");
102 }
103 
104 void llvm::appendToUsed(Module &M, ArrayRef<GlobalValue *> Values) {
105   appendToUsedList(M, "llvm.used", Values);
106 }
107 
108 void llvm::appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values) {
109   appendToUsedList(M, "llvm.compiler.used", Values);
110 }
111 
112 FunctionCallee
113 llvm::declareSanitizerInitFunction(Module &M, StringRef InitName,
114                                    ArrayRef<Type *> InitArgTypes) {
115   assert(!InitName.empty() && "Expected init function name");
116   return M.getOrInsertFunction(
117       InitName,
118       FunctionType::get(Type::getVoidTy(M.getContext()), InitArgTypes, false),
119       AttributeList());
120 }
121 
122 Function *llvm::createSanitizerCtor(Module &M, StringRef CtorName) {
123   Function *Ctor = Function::Create(
124       FunctionType::get(Type::getVoidTy(M.getContext()), false),
125       GlobalValue::InternalLinkage, CtorName, &M);
126   BasicBlock *CtorBB = BasicBlock::Create(M.getContext(), "", Ctor);
127   ReturnInst::Create(M.getContext(), CtorBB);
128   return Ctor;
129 }
130 
131 std::pair<Function *, FunctionCallee> llvm::createSanitizerCtorAndInitFunctions(
132     Module &M, StringRef CtorName, StringRef InitName,
133     ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
134     StringRef VersionCheckName) {
135   assert(!InitName.empty() && "Expected init function name");
136   assert(InitArgs.size() == InitArgTypes.size() &&
137          "Sanitizer's init function expects different number of arguments");
138   FunctionCallee InitFunction =
139       declareSanitizerInitFunction(M, InitName, InitArgTypes);
140   Function *Ctor = createSanitizerCtor(M, CtorName);
141   IRBuilder<> IRB(Ctor->getEntryBlock().getTerminator());
142   IRB.CreateCall(InitFunction, InitArgs);
143   if (!VersionCheckName.empty()) {
144     FunctionCallee VersionCheckFunction = M.getOrInsertFunction(
145         VersionCheckName, FunctionType::get(IRB.getVoidTy(), {}, false),
146         AttributeList());
147     IRB.CreateCall(VersionCheckFunction, {});
148   }
149   return std::make_pair(Ctor, InitFunction);
150 }
151 
152 std::pair<Function *, FunctionCallee>
153 llvm::getOrCreateSanitizerCtorAndInitFunctions(
154     Module &M, StringRef CtorName, StringRef InitName,
155     ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
156     function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
157     StringRef VersionCheckName) {
158   assert(!CtorName.empty() && "Expected ctor function name");
159 
160   if (Function *Ctor = M.getFunction(CtorName))
161     // FIXME: Sink this logic into the module, similar to the handling of
162     // globals. This will make moving to a concurrent model much easier.
163     if (Ctor->arg_size() == 0 ||
164         Ctor->getReturnType() == Type::getVoidTy(M.getContext()))
165       return {Ctor, declareSanitizerInitFunction(M, InitName, InitArgTypes)};
166 
167   Function *Ctor;
168   FunctionCallee InitFunction;
169   std::tie(Ctor, InitFunction) = llvm::createSanitizerCtorAndInitFunctions(
170       M, CtorName, InitName, InitArgTypes, InitArgs, VersionCheckName);
171   FunctionsCreatedCallback(Ctor, InitFunction);
172   return std::make_pair(Ctor, InitFunction);
173 }
174 
175 Function *llvm::getOrCreateInitFunction(Module &M, StringRef Name) {
176   assert(!Name.empty() && "Expected init function name");
177   if (Function *F = M.getFunction(Name)) {
178     if (F->arg_size() != 0 ||
179         F->getReturnType() != Type::getVoidTy(M.getContext())) {
180       std::string Err;
181       raw_string_ostream Stream(Err);
182       Stream << "Sanitizer interface function defined with wrong type: " << *F;
183       report_fatal_error(Err);
184     }
185     return F;
186   }
187   Function *F =
188       cast<Function>(M.getOrInsertFunction(Name, AttributeList(),
189                                            Type::getVoidTy(M.getContext()))
190                          .getCallee());
191 
192   appendToGlobalCtors(M, F, 0);
193 
194   return F;
195 }
196 
197 void llvm::filterDeadComdatFunctions(
198     Module &M, SmallVectorImpl<Function *> &DeadComdatFunctions) {
199   // Build a map from the comdat to the number of entries in that comdat we
200   // think are dead. If this fully covers the comdat group, then the entire
201   // group is dead. If we find another entry in the comdat group though, we'll
202   // have to preserve the whole group.
203   SmallDenseMap<Comdat *, int, 16> ComdatEntriesCovered;
204   for (Function *F : DeadComdatFunctions) {
205     Comdat *C = F->getComdat();
206     assert(C && "Expected all input GVs to be in a comdat!");
207     ComdatEntriesCovered[C] += 1;
208   }
209 
210   auto CheckComdat = [&](Comdat &C) {
211     auto CI = ComdatEntriesCovered.find(&C);
212     if (CI == ComdatEntriesCovered.end())
213       return;
214 
215     // If this could have been covered by a dead entry, just subtract one to
216     // account for it.
217     if (CI->second > 0) {
218       CI->second -= 1;
219       return;
220     }
221 
222     // If we've already accounted for all the entries that were dead, the
223     // entire comdat is alive so remove it from the map.
224     ComdatEntriesCovered.erase(CI);
225   };
226 
227   auto CheckAllComdats = [&] {
228     for (Function &F : M.functions())
229       if (Comdat *C = F.getComdat()) {
230         CheckComdat(*C);
231         if (ComdatEntriesCovered.empty())
232           return;
233       }
234     for (GlobalVariable &GV : M.globals())
235       if (Comdat *C = GV.getComdat()) {
236         CheckComdat(*C);
237         if (ComdatEntriesCovered.empty())
238           return;
239       }
240     for (GlobalAlias &GA : M.aliases())
241       if (Comdat *C = GA.getComdat()) {
242         CheckComdat(*C);
243         if (ComdatEntriesCovered.empty())
244           return;
245       }
246   };
247   CheckAllComdats();
248 
249   if (ComdatEntriesCovered.empty()) {
250     DeadComdatFunctions.clear();
251     return;
252   }
253 
254   // Remove the entries that were not covering.
255   erase_if(DeadComdatFunctions, [&](GlobalValue *GV) {
256     return ComdatEntriesCovered.find(GV->getComdat()) ==
257            ComdatEntriesCovered.end();
258   });
259 }
260 
261 std::string llvm::getUniqueModuleId(Module *M) {
262   MD5 Md5;
263   bool ExportsSymbols = false;
264   auto AddGlobal = [&](GlobalValue &GV) {
265     if (GV.isDeclaration() || GV.getName().startswith("llvm.") ||
266         !GV.hasExternalLinkage() || GV.hasComdat())
267       return;
268     ExportsSymbols = true;
269     Md5.update(GV.getName());
270     Md5.update(ArrayRef<uint8_t>{0});
271   };
272 
273   for (auto &F : *M)
274     AddGlobal(F);
275   for (auto &GV : M->globals())
276     AddGlobal(GV);
277   for (auto &GA : M->aliases())
278     AddGlobal(GA);
279   for (auto &IF : M->ifuncs())
280     AddGlobal(IF);
281 
282   if (!ExportsSymbols)
283     return "";
284 
285   MD5::MD5Result R;
286   Md5.final(R);
287 
288   SmallString<32> Str;
289   MD5::stringifyResult(R, Str);
290   return ("$" + Str).str();
291 }
292 
293 void VFABI::setVectorVariantNames(
294     CallInst *CI, const SmallVector<std::string, 8> &VariantMappings) {
295   if (VariantMappings.empty())
296     return;
297 
298   SmallString<256> Buffer;
299   llvm::raw_svector_ostream Out(Buffer);
300   for (const std::string &VariantMapping : VariantMappings)
301     Out << VariantMapping << ",";
302   // Get rid of the trailing ','.
303   assert(!Buffer.str().empty() && "Must have at least one char.");
304   Buffer.pop_back();
305 
306   Module *M = CI->getModule();
307 #ifndef NDEBUG
308   for (const std::string &VariantMapping : VariantMappings) {
309     LLVM_DEBUG(dbgs() << "VFABI: adding mapping '" << VariantMapping << "'\n");
310     Optional<VFInfo> VI = VFABI::tryDemangleForVFABI(VariantMapping, *M);
311     assert(VI.hasValue() && "Cannot add an invalid VFABI name.");
312     assert(M->getNamedValue(VI.getValue().VectorName) &&
313            "Cannot add variant to attribute: "
314            "vector function declaration is missing.");
315   }
316 #endif
317   CI->addAttribute(
318       AttributeList::FunctionIndex,
319       Attribute::get(M->getContext(), MappingsAttrName, Buffer.str()));
320 }
321