llvm.org GIT mirror llvm / 753bd2a
Turn some C-style vararg into variadic templates Module::getOrInsertFunction is using C-style vararg instead of variadic templates. From a user prospective, it forces the use of an annoying nullptr to mark the end of the vararg, and there's not type checking on the arguments. The variadic template is an obvious solution to both issues. Patch by: Serge Guelton <serge.guelton@telecom-bretagne.eu> Differential Revision: https://reviews.llvm.org/D31070 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299699 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 2 years ago
22 changed file(s) with 207 addition(s) and 253 deletion(s). Raw diff Collapse all Expand all
320320 /// or a ConstantExpr BitCast of that type if the named function has a
321321 /// different type. This version of the method takes a null terminated list of
322322 /// function arguments, which makes it easier for clients to use.
323 template
323324 Constant *getOrInsertFunction(StringRef Name, AttributeList AttributeList,
324 Type *RetTy, ...) LLVM_END_WITH_NULL;
325 Type *RetTy, ArgsTy... Args) {
326 SmallVector ArgTys{Args...};
327 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false),
328 AttributeList);
329 }
325330
326331 /// Same as above, but without the attributes.
327 Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ...)
328 LLVM_END_WITH_NULL;
332 template
333 Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ArgsTy... Args) {
334 return getOrInsertFunction(Name, AttributeList{}, RetTy, Args...);
335 }
329336
330337 /// Look up the specified function in the module symbol table. If it does not
331338 /// exist, return null.
3939
4040 Type *VoidTy = Type::getVoidTy(F.getContext());
4141 Constant *CountingFn =
42 F.getParent()->getOrInsertFunction(CountingFunctionName,
43 VoidTy, nullptr);
42 F.getParent()->getOrInsertFunction(CountingFunctionName, VoidTy);
4443 CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
4544 return true;
4645 }
110110 Type::getVoidTy(M.getContext()));
111111 break;
112112 case Intrinsic::memcpy:
113 M.getOrInsertFunction("memcpy",
114 Type::getInt8PtrTy(Context),
115 Type::getInt8PtrTy(Context),
116 Type::getInt8PtrTy(Context),
117 DL.getIntPtrType(Context), nullptr);
113 M.getOrInsertFunction(
114 "memcpy", Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context),
115 Type::getInt8PtrTy(Context), DL.getIntPtrType(Context));
118116 break;
119117 case Intrinsic::memmove:
120 M.getOrInsertFunction("memmove",
121 Type::getInt8PtrTy(Context),
122 Type::getInt8PtrTy(Context),
123 Type::getInt8PtrTy(Context),
124 DL.getIntPtrType(Context), nullptr);
118 M.getOrInsertFunction(
119 "memmove", Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context),
120 Type::getInt8PtrTy(Context), DL.getIntPtrType(Context));
125121 break;
126122 case Intrinsic::memset:
127 M.getOrInsertFunction("memset",
128 Type::getInt8PtrTy(Context),
129 Type::getInt8PtrTy(Context),
130 Type::getInt32Ty(M.getContext()),
131 DL.getIntPtrType(Context), nullptr);
123 M.getOrInsertFunction(
124 "memset", Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context),
125 Type::getInt32Ty(M.getContext()), DL.getIntPtrType(Context));
132126 break;
133127 case Intrinsic::sqrt:
134128 EnsureFPIntrinsicsExist(M, F, "sqrtf", "sqrt", "sqrtl");
10971097 // Create the function using an IR-level function.
10981098 LLVMContext &C = M.getContext();
10991099 Function *F = dyn_cast(
1100 M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C), nullptr));
1100 M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C)));
11011101 assert(F && "Function was null!");
11021102
11031103 // NOTE: If this is linkonceodr, then we can take advantage of linker deduping
449449 /* Unreachable */ true, Weights);
450450 IRBuilder<> IRBFail(CheckTerm);
451451 // FIXME: respect -fsanitize-trap / -ftrap-function here?
452 Constant *StackChkFail = F.getParent()->getOrInsertFunction(
453 "__stack_chk_fail", IRB.getVoidTy(), nullptr);
452 Constant *StackChkFail =
453 F.getParent()->getOrInsertFunction("__stack_chk_fail", IRB.getVoidTy());
454454 IRBFail.CreateCall(StackChkFail, {});
455455 }
456456
479479
480480 bool SjLjEHPrepare::runOnFunction(Function &F) {
481481 Module &M = *F.getParent();
482 RegisterFn = M.getOrInsertFunction(
483 "_Unwind_SjLj_Register", Type::getVoidTy(M.getContext()),
484 PointerType::getUnqual(FunctionContextTy), nullptr);
482 RegisterFn = M.getOrInsertFunction("_Unwind_SjLj_Register",
483 Type::getVoidTy(M.getContext()),
484 PointerType::getUnqual(FunctionContextTy));
485485 UnregisterFn = M.getOrInsertFunction(
486486 "_Unwind_SjLj_Unregister", Type::getVoidTy(M.getContext()),
487 PointerType::getUnqual(FunctionContextTy), nullptr);
487 PointerType::getUnqual(FunctionContextTy));
488488 FrameAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::frameaddress);
489489 StackAddrFn = Intrinsic::getDeclaration(&M, Intrinsic::stacksave);
490490 StackRestoreFn = Intrinsic::getDeclaration(&M, Intrinsic::stackrestore);
480480 IRBuilder<> B(FailBB);
481481 B.SetCurrentDebugLocation(DebugLoc::get(0, 0, F->getSubprogram()));
482482 if (Trip.isOSOpenBSD()) {
483 Constant *StackChkFail =
484 M->getOrInsertFunction("__stack_smash_handler",
485 Type::getVoidTy(Context),
486 Type::getInt8PtrTy(Context), nullptr);
483 Constant *StackChkFail = M->getOrInsertFunction(
484 "__stack_smash_handler", Type::getVoidTy(Context),
485 Type::getInt8PtrTy(Context));
487486
488487 B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
489488 } else {
490489 Constant *StackChkFail =
491 M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context),
492 nullptr);
490 M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
491
493492 B.CreateCall(StackChkFail, {});
494493 }
495494 B.CreateUnreachable();
18171817 Module *M = IRB.GetInsertBlock()->getParent()->getParent();
18181818 Type *StackPtrTy = Type::getInt8PtrTy(M->getContext());
18191819 Value *Fn = M->getOrInsertFunction("__safestack_pointer_address",
1820 StackPtrTy->getPointerTo(0), nullptr);
1820 StackPtrTy->getPointerTo(0));
18211821 return IRB.CreateCall(Fn);
18221822 }
18231823
465465 Value *MallocFunc = MallocF;
466466 if (!MallocFunc)
467467 // prototype malloc as "void *malloc(size_t)"
468 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
468 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy);
469469 PointerType *AllocPtrType = PointerType::getUnqual(AllocTy);
470470 CallInst *MCall = nullptr;
471471 Instruction *Result = nullptr;
559559 Type *VoidTy = Type::getVoidTy(M->getContext());
560560 Type *IntPtrTy = Type::getInt8PtrTy(M->getContext());
561561 // prototype free as "void free(void*)"
562 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
562 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy);
563563 CallInst *Result = nullptr;
564564 Value *PtrCast = Source;
565565 if (InsertBefore) {
146146 return getOrInsertFunction(Name, Ty, AttributeList());
147147 }
148148
149 // getOrInsertFunction - Look up the specified function in the module symbol
150 // table. If it does not exist, add a prototype for the function and return it.
151 // This version of the method takes a null terminated list of function
152 // arguments, which makes it easier for clients to use.
153 //
154 Constant *Module::getOrInsertFunction(StringRef Name,
155 AttributeList AttributeList, Type *RetTy,
156 ...) {
157 va_list Args;
158 va_start(Args, RetTy);
159
160 // Build the list of argument types...
161 std::vector ArgTys;
162 while (Type *ArgTy = va_arg(Args, Type*))
163 ArgTys.push_back(ArgTy);
164
165 va_end(Args);
166
167 // Build the function type and chain to the other getOrInsertFunction...
168 return getOrInsertFunction(Name,
169 FunctionType::get(RetTy, ArgTys, false),
170 AttributeList);
171 }
172
173 Constant *Module::getOrInsertFunction(StringRef Name,
174 Type *RetTy, ...) {
175 va_list Args;
176 va_start(Args, RetTy);
177
178 // Build the list of argument types...
179 std::vector ArgTys;
180 while (Type *ArgTy = va_arg(Args, Type*))
181 ArgTys.push_back(ArgTy);
182
183 va_end(Args);
184
185 // Build the function type and chain to the other getOrInsertFunction...
186 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false),
187 AttributeList());
188 }
189
190149 // getFunction - Look up the specified function in the module symbol table.
191150 // If it does not exist, return null.
192151 //
20972097 Type::getInt8PtrTy(M.getContext()));
20982098
20992099 // MSVC CRT has a function to validate security cookie.
2100 auto *SecurityCheckCookie = cast(
2101 M.getOrInsertFunction("__security_check_cookie",
2102 Type::getVoidTy(M.getContext()),
2103 Type::getInt8PtrTy(M.getContext()), nullptr));
2100 auto *SecurityCheckCookie = cast(M.getOrInsertFunction(
2101 "__security_check_cookie", Type::getVoidTy(M.getContext()),
2102 Type::getInt8PtrTy(M.getContext())));
21042103 SecurityCheckCookie->setCallingConv(CallingConv::X86_FastCall);
21052104 SecurityCheckCookie->addAttribute(1, Attribute::AttrKind::InReg);
21062105 return;
9797 LLVMContext &Ctx = M.getContext();
9898 Constant *C = M.getOrInsertFunction(
9999 "__cfi_check", Type::getVoidTy(Ctx), Type::getInt64Ty(Ctx),
100 Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx), nullptr);
100 Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx));
101101 Function *F = dyn_cast(C);
102102 F->setAlignment(4096);
103103 auto args = F->arg_begin();
114114
115115 BasicBlock *TrapBB = BasicBlock::Create(Ctx, "fail", F);
116116 IRBuilder<> IRBFail(TrapBB);
117 Constant *CFICheckFailFn = M.getOrInsertFunction(
118 "__cfi_check_fail", Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx),
119 Type::getInt8PtrTy(Ctx), nullptr);
117 Constant *CFICheckFailFn =
118 M.getOrInsertFunction("__cfi_check_fail", Type::getVoidTy(Ctx),
119 Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx));
120120 IRBFail.CreateCall(CFICheckFailFn, {&CFICheckFailData, &Addr});
121121 IRBFail.CreateBr(ExitBB);
122122
12191219 if (Res.TheKind == WholeProgramDevirtResolution::SingleImpl) {
12201220 // The type of the function in the declaration is irrelevant because every
12211221 // call site will cast it to the correct type.
1222 auto *SingleImpl = M.getOrInsertFunction(
1223 Res.SingleImplName, Type::getVoidTy(M.getContext()), nullptr);
1222 auto *SingleImpl = M.getOrInsertFunction(Res.SingleImplName,
1223 Type::getVoidTy(M.getContext()));
12241224
12251225 // This is the import phase so we should not be exporting anything.
12261226 bool IsExported = false;
15881588 IRBuilder<> IRB(*C);
15891589
15901590 // Declare our poisoning and unpoisoning functions.
1591 AsanPoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1592 kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
1591 AsanPoisonGlobals = checkSanitizerInterfaceFunction(
1592 M.getOrInsertFunction(kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy));
15931593 AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
1594 AsanUnpoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1595 kAsanUnpoisonGlobalsName, IRB.getVoidTy(), nullptr));
1594 AsanUnpoisonGlobals = checkSanitizerInterfaceFunction(
1595 M.getOrInsertFunction(kAsanUnpoisonGlobalsName, IRB.getVoidTy()));
15961596 AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
15971597
15981598 // Declare functions that register/unregister globals.
15991599 AsanRegisterGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1600 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1600 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy));
16011601 AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
1602 AsanUnregisterGlobals = checkSanitizerInterfaceFunction(
1603 M.getOrInsertFunction(kAsanUnregisterGlobalsName, IRB.getVoidTy(),
1604 IntptrTy, IntptrTy, nullptr));
1602 AsanUnregisterGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1603 kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy));
16051604 AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
16061605
16071606 // Declare the functions that find globals in a shared object and then invoke
16081607 // the (un)register function on them.
16091608 AsanRegisterImageGlobals =
16101609 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1611 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
1610 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy));
16121611 AsanRegisterImageGlobals->setLinkage(Function::ExternalLinkage);
16131612
16141613 AsanUnregisterImageGlobals =
16151614 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1616 kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
1615 kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy));
16171616 AsanUnregisterImageGlobals->setLinkage(Function::ExternalLinkage);
16181617
16191618 AsanRegisterElfGlobals = checkSanitizerInterfaceFunction(
20912090 const std::string ExpStr = Exp ? "exp_" : "";
20922091 const std::string SuffixStr = CompileKernel ? "N" : "_n";
20932092 const std::string EndingStr = Recover ? "_noabort" : "";
2094 Type *ExpType = Exp ? Type::getInt32Ty(*C) : nullptr;
2093
2094 SmallVector Args2 = {IntptrTy, IntptrTy};
2095 SmallVector Args1{1, IntptrTy};
2096 if (Exp) {
2097 Type *ExpType = Type::getInt32Ty(*C);
2098 Args2.push_back(ExpType);
2099 Args1.push_back(ExpType);
2100 }
2101
20952102 AsanErrorCallbackSized[AccessIsWrite][Exp] =
20962103 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2097 kAsanReportErrorTemplate + ExpStr + TypeStr + SuffixStr + EndingStr,
2098 IRB.getVoidTy(), IntptrTy, IntptrTy, ExpType, nullptr));
2104 kAsanReportErrorTemplate + ExpStr + TypeStr + SuffixStr +
2105 EndingStr,
2106 FunctionType::get(IRB.getVoidTy(), Args2, false)));
2107
20992108 AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] =
21002109 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21012110 ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
2102 IRB.getVoidTy(), IntptrTy, IntptrTy, ExpType, nullptr));
2111 FunctionType::get(IRB.getVoidTy(), Args2, false)));
2112
21032113 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
21042114 AccessSizeIndex++) {
21052115 const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
21062116 AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
21072117 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21082118 kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
2109 IRB.getVoidTy(), IntptrTy, ExpType, nullptr));
2119 FunctionType::get(IRB.getVoidTy(), Args1, false)));
2120
21102121 AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
21112122 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21122123 ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
2113 IRB.getVoidTy(), IntptrTy, ExpType, nullptr));
2124 FunctionType::get(IRB.getVoidTy(), Args1, false)));
21142125 }
21152126 }
21162127 }
21192130 CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
21202131 AsanMemmove = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21212132 MemIntrinCallbackPrefix + "memmove", IRB.getInt8PtrTy(),
2122 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr));
2133 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
21232134 AsanMemcpy = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21242135 MemIntrinCallbackPrefix + "memcpy", IRB.getInt8PtrTy(),
2125 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr));
2136 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
21262137 AsanMemset = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21272138 MemIntrinCallbackPrefix + "memset", IRB.getInt8PtrTy(),
2128 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy, nullptr));
2139 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy));
21292140
21302141 AsanHandleNoReturnFunc = checkSanitizerInterfaceFunction(
2131 M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy(), nullptr));
2132
2133 AsanPtrCmpFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2134 kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2135 AsanPtrSubFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2136 kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2142 M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy()));
2143
2144 AsanPtrCmpFunction = checkSanitizerInterfaceFunction(
2145 M.getOrInsertFunction(kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy));
2146 AsanPtrSubFunction = checkSanitizerInterfaceFunction(
2147 M.getOrInsertFunction(kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy));
21372148 // We insert an empty inline asm after __asan_report* to avoid callback merge.
21382149 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
21392150 StringRef(""), StringRef(""),
23682379 IRBuilder<> IRB(*C);
23692380 for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
23702381 std::string Suffix = itostr(i);
2371 AsanStackMallocFunc[i] = checkSanitizerInterfaceFunction(
2372 M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy,
2373 IntptrTy, nullptr));
2382 AsanStackMallocFunc[i] =
2383 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2384 kAsanStackMallocNameTemplate + Suffix, IntptrTy, IntptrTy));
23742385 AsanStackFreeFunc[i] = checkSanitizerInterfaceFunction(
23752386 M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
2376 IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2387 IRB.getVoidTy(), IntptrTy, IntptrTy));
23772388 }
23782389 if (ASan.UseAfterScope) {
2379 AsanPoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
2380 M.getOrInsertFunction(kAsanPoisonStackMemoryName, IRB.getVoidTy(),
2381 IntptrTy, IntptrTy, nullptr));
2382 AsanUnpoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
2383 M.getOrInsertFunction(kAsanUnpoisonStackMemoryName, IRB.getVoidTy(),
2384 IntptrTy, IntptrTy, nullptr));
2390 AsanPoisonStackMemoryFunc =
2391 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2392 kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy));
2393 AsanUnpoisonStackMemoryFunc =
2394 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2395 kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy));
23852396 }
23862397
23872398 for (size_t Val : {0x00, 0xf1, 0xf2, 0xf3, 0xf5, 0xf8}) {
23882399 std::ostringstream Name;
23892400 Name << kAsanSetShadowPrefix;
23902401 Name << std::setw(2) << std::setfill('0') << std::hex << Val;
2391 AsanSetShadowFunc[Val] =
2392 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2393 Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2402 AsanSetShadowFunc[Val] = checkSanitizerInterfaceFunction(
2403 M.getOrInsertFunction(Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy));
23942404 }
23952405
23962406 AsanAllocaPoisonFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2397 kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2407 kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy));
23982408 AsanAllocasUnpoisonFunc =
23992409 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2400 kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2410 kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy));
24012411 }
24022412
24032413 void FunctionStackPoisoner::copyToShadowInline(ArrayRef ShadowMask,
266266 SmallString<32> AlignedLoadName("__esan_aligned_load" + ByteSizeStr);
267267 EsanAlignedLoad[Idx] =
268268 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
269 AlignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
269 AlignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
270270 SmallString<32> AlignedStoreName("__esan_aligned_store" + ByteSizeStr);
271271 EsanAlignedStore[Idx] =
272272 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
273 AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
273 AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
274274 SmallString<32> UnalignedLoadName("__esan_unaligned_load" + ByteSizeStr);
275275 EsanUnalignedLoad[Idx] =
276276 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
277 UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
277 UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
278278 SmallString<32> UnalignedStoreName("__esan_unaligned_store" + ByteSizeStr);
279279 EsanUnalignedStore[Idx] =
280280 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
281 UnalignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
282 }
283 EsanUnalignedLoadN = checkSanitizerInterfaceFunction(
284 M.getOrInsertFunction("__esan_unaligned_loadN", IRB.getVoidTy(),
285 IRB.getInt8PtrTy(), IntptrTy, nullptr));
281 UnalignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
282 }
283 EsanUnalignedLoadN = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
284 "__esan_unaligned_loadN", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy));
286285 EsanUnalignedStoreN = checkSanitizerInterfaceFunction(
287286 M.getOrInsertFunction("__esan_unaligned_storeN", IRB.getVoidTy(),
288 IRB.getInt8PtrTy(), IntptrTy, nullptr));
287 IRB.getInt8PtrTy(), IntptrTy));
289288 MemmoveFn = checkSanitizerInterfaceFunction(
290289 M.getOrInsertFunction("memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
291 IRB.getInt8PtrTy(), IntptrTy, nullptr));
290 IRB.getInt8PtrTy(), IntptrTy));
292291 MemcpyFn = checkSanitizerInterfaceFunction(
293292 M.getOrInsertFunction("memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
294 IRB.getInt8PtrTy(), IntptrTy, nullptr));
293 IRB.getInt8PtrTy(), IntptrTy));
295294 MemsetFn = checkSanitizerInterfaceFunction(
296295 M.getOrInsertFunction("memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
297 IRB.getInt32Ty(), IntptrTy, nullptr));
296 IRB.getInt32Ty(), IntptrTy));
298297 }
299298
300299 bool EfficiencySanitizer::shouldIgnoreStructType(StructType *StructTy) {
531530 ReturnInst::Create(*Ctx, BasicBlock::Create(*Ctx, "", EsanDtorFunction));
532531 IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator());
533532 Function *EsanExit = checkSanitizerInterfaceFunction(
534 M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(),
535 Int8PtrTy, nullptr));
533 M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(), Int8PtrTy));
536534 EsanExit->setLinkage(Function::ExternalLinkage);
537535 IRB_Dtor.CreateCall(EsanExit, {ToolInfoArg});
538536 appendToGlobalDtors(M, EsanDtorFunction, EsanCtorAndDtorPriority);
424424 // which is not yet implemented.
425425 StringRef WarningFnName = Recover ? "__msan_warning"
426426 : "__msan_warning_noreturn";
427 WarningFn = M.getOrInsertFunction(WarningFnName, IRB.getVoidTy(), nullptr);
427 WarningFn = M.getOrInsertFunction(WarningFnName, IRB.getVoidTy());
428428
429429 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
430430 AccessSizeIndex++) {
431431 unsigned AccessSize = 1 << AccessSizeIndex;
432432 std::string FunctionName = "__msan_maybe_warning_" + itostr(AccessSize);
433 MaybeWarningFn[AccessSizeIndex] = M.getOrInsertFunction(
434 FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8),
435 IRB.getInt32Ty(), nullptr);
433 MaybeWarningFn[AccessSizeIndex] =
434 M.getOrInsertFunction(FunctionName, IRB.getVoidTy(),
435 IRB.getIntNTy(AccessSize * 8), IRB.getInt32Ty());
436436
437437 FunctionName = "__msan_maybe_store_origin_" + itostr(AccessSize);
438438 MaybeStoreOriginFn[AccessSizeIndex] = M.getOrInsertFunction(
439439 FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8),
440 IRB.getInt8PtrTy(), IRB.getInt32Ty(), nullptr);
440 IRB.getInt8PtrTy(), IRB.getInt32Ty());
441441 }
442442
443443 MsanSetAllocaOrigin4Fn = M.getOrInsertFunction(
444 "__msan_set_alloca_origin4", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy,
445 IRB.getInt8PtrTy(), IntptrTy, nullptr);
446 MsanPoisonStackFn =
447 M.getOrInsertFunction("__msan_poison_stack", IRB.getVoidTy(),
448 IRB.getInt8PtrTy(), IntptrTy, nullptr);
449 MsanChainOriginFn = M.getOrInsertFunction(
450 "__msan_chain_origin", IRB.getInt32Ty(), IRB.getInt32Ty(), nullptr);
451 MemmoveFn = M.getOrInsertFunction(
452 "__msan_memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
453 IRB.getInt8PtrTy(), IntptrTy, nullptr);
454 MemcpyFn = M.getOrInsertFunction(
455 "__msan_memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
456 IntptrTy, nullptr);
457 MemsetFn = M.getOrInsertFunction(
458 "__msan_memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(),
459 IntptrTy, nullptr);
444 "__msan_set_alloca_origin4", IRB.getVoidTy(), IRB.getInt8PtrTy(),
445 IntptrTy, IRB.getInt8PtrTy(), IntptrTy);
446 MsanPoisonStackFn = M.getOrInsertFunction(
447 "__msan_poison_stack", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy);
448 MsanChainOriginFn = M.getOrInsertFunction("__msan_chain_origin",
449 IRB.getInt32Ty(), IRB.getInt32Ty());
450 MemmoveFn =
451 M.getOrInsertFunction("__msan_memmove", IRB.getInt8PtrTy(),
452 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
453 MemcpyFn =
454 M.getOrInsertFunction("__msan_memcpy", IRB.getInt8PtrTy(),
455 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy);
456 MemsetFn =
457 M.getOrInsertFunction("__msan_memset", IRB.getInt8PtrTy(),
458 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy);
460459
461460 // Create globals.
462461 RetvalTLS = new GlobalVariable(
261261 Int32Ty = IRB.getInt32Ty();
262262
263263 SanCovFunction = checkSanitizerInterfaceFunction(
264 M.getOrInsertFunction(SanCovName, VoidTy, Int32PtrTy, nullptr));
264 M.getOrInsertFunction(SanCovName, VoidTy, Int32PtrTy));
265265 SanCovWithCheckFunction = checkSanitizerInterfaceFunction(
266 M.getOrInsertFunction(SanCovWithCheckName, VoidTy, Int32PtrTy, nullptr));
266 M.getOrInsertFunction(SanCovWithCheckName, VoidTy, Int32PtrTy));
267267 SanCovTracePCIndir = checkSanitizerInterfaceFunction(
268 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy, nullptr));
269 SanCovIndirCallFunction =
270 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
271 SanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
268 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy));
269 SanCovIndirCallFunction = checkSanitizerInterfaceFunction(
270 M.getOrInsertFunction(SanCovIndirCallName, VoidTy, IntptrTy, IntptrTy));
272271 SanCovTraceCmpFunction[0] =
273272 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
274 SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty(), nullptr));
275 SanCovTraceCmpFunction[1] = checkSanitizerInterfaceFunction(
276 M.getOrInsertFunction(SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(),
277 IRB.getInt16Ty(), nullptr));
278 SanCovTraceCmpFunction[2] = checkSanitizerInterfaceFunction(
279 M.getOrInsertFunction(SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(),
280 IRB.getInt32Ty(), nullptr));
281 SanCovTraceCmpFunction[3] =
273 SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty()));
274 SanCovTraceCmpFunction[1] =
282275 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
283 SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty, nullptr));
284
285 SanCovTraceDivFunction[0] =
276 SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(), IRB.getInt16Ty()));
277 SanCovTraceCmpFunction[2] =
286278 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
287 SanCovTraceDiv4, VoidTy, IRB.getInt32Ty(), nullptr));
288 SanCovTraceDivFunction[1] =
289 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
290 SanCovTraceDiv8, VoidTy, Int64Ty, nullptr));
291 SanCovTraceGepFunction =
292 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
293 SanCovTraceGep, VoidTy, IntptrTy, nullptr));
279 SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(), IRB.getInt32Ty()));
280 SanCovTraceCmpFunction[3] = checkSanitizerInterfaceFunction(
281 M.getOrInsertFunction(SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty));
282
283 SanCovTraceDivFunction[0] = checkSanitizerInterfaceFunction(
284 M.getOrInsertFunction(SanCovTraceDiv4, VoidTy, IRB.getInt32Ty()));
285 SanCovTraceDivFunction[1] = checkSanitizerInterfaceFunction(
286 M.getOrInsertFunction(SanCovTraceDiv8, VoidTy, Int64Ty));
287 SanCovTraceGepFunction = checkSanitizerInterfaceFunction(
288 M.getOrInsertFunction(SanCovTraceGep, VoidTy, IntptrTy));
294289 SanCovTraceSwitchFunction =
295290 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
296 SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy, nullptr));
291 SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy));
297292
298293 // We insert an empty inline asm after cov callbacks to avoid callback merge.
299294 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
301296 /*hasSideEffects=*/true);
302297
303298 SanCovTracePC = checkSanitizerInterfaceFunction(
304 M.getOrInsertFunction(SanCovTracePCName, VoidTy, nullptr));
305 SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
306 SanCovTracePCGuardName, VoidTy, Int32PtrTy, nullptr));
299 M.getOrInsertFunction(SanCovTracePCName, VoidTy));
300 SanCovTracePCGuard = checkSanitizerInterfaceFunction(
301 M.getOrInsertFunction(SanCovTracePCGuardName, VoidTy, Int32PtrTy));
307302 SanCovTraceEnter = checkSanitizerInterfaceFunction(
308 M.getOrInsertFunction(SanCovTraceEnterName, VoidTy, Int32PtrTy, nullptr));
303 M.getOrInsertFunction(SanCovTraceEnterName, VoidTy, Int32PtrTy));
309304 SanCovTraceBB = checkSanitizerInterfaceFunction(
310 M.getOrInsertFunction(SanCovTraceBBName, VoidTy, Int32PtrTy, nullptr));
305 M.getOrInsertFunction(SanCovTraceBBName, VoidTy, Int32PtrTy));
311306
312307 // At this point we create a dummy array of guards because we don't
313308 // know how many elements we will need.
159159 Attribute::NoUnwind);
160160 // Initialize the callbacks.
161161 TsanFuncEntry = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
162 "__tsan_func_entry", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
162 "__tsan_func_entry", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
163163 TsanFuncExit = checkSanitizerInterfaceFunction(
164 M.getOrInsertFunction("__tsan_func_exit", Attr, IRB.getVoidTy(), nullptr));
164 M.getOrInsertFunction("__tsan_func_exit", Attr, IRB.getVoidTy()));
165165 TsanIgnoreBegin = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
166 "__tsan_ignore_thread_begin", Attr, IRB.getVoidTy(), nullptr));
167 TsanIgnoreEnd = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
168 "__tsan_ignore_thread_end", Attr, IRB.getVoidTy(), nullptr));
166 "__tsan_ignore_thread_begin", Attr, IRB.getVoidTy()));
167 TsanIgnoreEnd = checkSanitizerInterfaceFunction(
168 M.getOrInsertFunction("__tsan_ignore_thread_end", Attr, IRB.getVoidTy()));
169169 OrdTy = IRB.getInt32Ty();
170170 for (size_t i = 0; i < kNumberOfAccessSizes; ++i) {
171171 const unsigned ByteSize = 1U << i;
174174 std::string BitSizeStr = utostr(BitSize);
175175 SmallString<32> ReadName("__tsan_read" + ByteSizeStr);
176176 TsanRead[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
177 ReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
177 ReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
178178
179179 SmallString<32> WriteName("__tsan_write" + ByteSizeStr);
180180 TsanWrite[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
181 WriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
181 WriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
182182
183183 SmallString<64> UnalignedReadName("__tsan_unaligned_read" + ByteSizeStr);
184184 TsanUnalignedRead[i] =
185185 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
186 UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
186 UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
187187
188188 SmallString<64> UnalignedWriteName("__tsan_unaligned_write" + ByteSizeStr);
189189 TsanUnalignedWrite[i] =
190190 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
191 UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
191 UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
192192
193193 Type *Ty = Type::getIntNTy(M.getContext(), BitSize);
194194 Type *PtrTy = Ty->getPointerTo();
195195 SmallString<32> AtomicLoadName("__tsan_atomic" + BitSizeStr + "_load");
196196 TsanAtomicLoad[i] = checkSanitizerInterfaceFunction(
197 M.getOrInsertFunction(AtomicLoadName, Attr, Ty, PtrTy, OrdTy, nullptr));
197 M.getOrInsertFunction(AtomicLoadName, Attr, Ty, PtrTy, OrdTy));
198198
199199 SmallString<32> AtomicStoreName("__tsan_atomic" + BitSizeStr + "_store");
200200 TsanAtomicStore[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
201 AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy, nullptr));
201 AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy));
202202
203203 for (int op = AtomicRMWInst::FIRST_BINOP;
204204 op <= AtomicRMWInst::LAST_BINOP; ++op) {
222222 continue;
223223 SmallString<32> RMWName("__tsan_atomic" + itostr(BitSize) + NamePart);
224224 TsanAtomicRMW[op][i] = checkSanitizerInterfaceFunction(
225 M.getOrInsertFunction(RMWName, Attr, Ty, PtrTy, Ty, OrdTy, nullptr));
225 M.getOrInsertFunction(RMWName, Attr, Ty, PtrTy, Ty, OrdTy));
226226 }
227227
228228 SmallString<32> AtomicCASName("__tsan_atomic" + BitSizeStr +
229229 "_compare_exchange_val");
230230 TsanAtomicCAS[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
231 AtomicCASName, Attr, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy, nullptr));
231 AtomicCASName, Attr, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy));
232232 }
233233 TsanVptrUpdate = checkSanitizerInterfaceFunction(
234234 M.getOrInsertFunction("__tsan_vptr_update", Attr, IRB.getVoidTy(),
235 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), nullptr));
235 IRB.getInt8PtrTy(), IRB.getInt8PtrTy()));
236236 TsanVptrLoad = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
237 "__tsan_vptr_read", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
237 "__tsan_vptr_read", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
238238 TsanAtomicThreadFence = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
239 "__tsan_atomic_thread_fence", Attr, IRB.getVoidTy(), OrdTy, nullptr));
239 "__tsan_atomic_thread_fence", Attr, IRB.getVoidTy(), OrdTy));
240240 TsanAtomicSignalFence = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
241 "__tsan_atomic_signal_fence", Attr, IRB.getVoidTy(), OrdTy, nullptr));
241 "__tsan_atomic_signal_fence", Attr, IRB.getVoidTy(), OrdTy));
242242
243243 MemmoveFn = checkSanitizerInterfaceFunction(
244 M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
245 IRB.getInt8PtrTy(), IntptrTy, nullptr));
244 M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(),
245 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
246246 MemcpyFn = checkSanitizerInterfaceFunction(
247 M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
248 IRB.getInt8PtrTy(), IntptrTy, nullptr));
247 M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(),
248 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
249249 MemsetFn = checkSanitizerInterfaceFunction(
250 M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
251 IRB.getInt32Ty(), IntptrTy, nullptr));
250 M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(),
251 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy));
252252 }
253253
254254 bool ThreadSanitizer::doInitialization(Module &M) {
820820 Type *Int8PtrTy = DestInt8PtrTy;
821821
822822 Module *M = TheStore->getModule();
823 Value *MSP =
824 M->getOrInsertFunction("memset_pattern16", Builder.getVoidTy(),
825 Int8PtrTy, Int8PtrTy, IntPtr, (void *)nullptr);
823 Value *MSP = M->getOrInsertFunction("memset_pattern16", Builder.getVoidTy(),
824 Int8PtrTy, Int8PtrTy, IntPtr);
826825 inferLibFuncAttributes(*M->getFunction("memset_pattern16"), *TLI);
827826
828827 // Otherwise we should form a memset_pattern16. PatternValue is known to be
722722 Module *M = B.GetInsertBlock()->getModule();
723723 LLVMContext &Context = B.GetInsertBlock()->getContext();
724724 Constant *StrLen = M->getOrInsertFunction("strlen", DL.getIntPtrType(Context),
725 B.getInt8PtrTy(), nullptr);
725 B.getInt8PtrTy());
726726 inferLibFuncAttributes(*M->getFunction("strlen"), *TLI);
727727 CallInst *CI = B.CreateCall(StrLen, castToCStr(Ptr, B), "strlen");
728728 if (const Function *F = dyn_cast(StrLen->stripPointerCasts()))
739739 Module *M = B.GetInsertBlock()->getModule();
740740 Type *I8Ptr = B.getInt8PtrTy();
741741 Type *I32Ty = B.getInt32Ty();
742 Constant *StrChr =
743 M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty, nullptr);
742 Constant *StrChr = M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty);
744743 inferLibFuncAttributes(*M->getFunction("strchr"), *TLI);
745744 CallInst *CI = B.CreateCall(
746745 StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
756755
757756 Module *M = B.GetInsertBlock()->getModule();
758757 LLVMContext &Context = B.GetInsertBlock()->getContext();
759 Value *StrNCmp = M->getOrInsertFunction("strncmp", B.getInt32Ty(),
760 B.getInt8PtrTy(), B.getInt8PtrTy(),
761 DL.getIntPtrType(Context), nullptr);
758 Value *StrNCmp =
759 M->getOrInsertFunction("strncmp", B.getInt32Ty(), B.getInt8PtrTy(),
760 B.getInt8PtrTy(), DL.getIntPtrType(Context));
762761 inferLibFuncAttributes(*M->getFunction("strncmp"), *TLI);
763762 CallInst *CI = B.CreateCall(
764763 StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "strncmp");
776775
777776 Module *M = B.GetInsertBlock()->getModule();
778777 Type *I8Ptr = B.getInt8PtrTy();
779 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, nullptr);
778 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
780779 inferLibFuncAttributes(*M->getFunction(Name), *TLI);
781780 CallInst *CI =
782781 B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
792791
793792 Module *M = B.GetInsertBlock()->getModule();
794793 Type *I8Ptr = B.getInt8PtrTy();
795 Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr,
796 Len->getType(), nullptr);
794 Value *StrNCpy =
795 M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, Len->getType());
797796 inferLibFuncAttributes(*M->getFunction(Name), *TLI);
798797 CallInst *CI = B.CreateCall(
799798 StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, "strncpy");
816815 Value *MemCpy = M->getOrInsertFunction(
817816 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
818817 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
819 DL.getIntPtrType(Context), nullptr);
818 DL.getIntPtrType(Context));
820819 Dst = castToCStr(Dst, B);
821820 Src = castToCStr(Src, B);
822821 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
832831
833832 Module *M = B.GetInsertBlock()->getModule();
834833 LLVMContext &Context = B.GetInsertBlock()->getContext();
835 Value *MemChr = M->getOrInsertFunction("memchr", B.getInt8PtrTy(),
836 B.getInt8PtrTy(), B.getInt32Ty(),
837 DL.getIntPtrType(Context), nullptr);
834 Value *MemChr =
835 M->getOrInsertFunction("memchr", B.getInt8PtrTy(), B.getInt8PtrTy(),
836 B.getInt32Ty(), DL.getIntPtrType(Context));
838837 inferLibFuncAttributes(*M->getFunction("memchr"), *TLI);
839838 CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, "memchr");
840839
851850
852851 Module *M = B.GetInsertBlock()->getModule();
853852 LLVMContext &Context = B.GetInsertBlock()->getContext();
854 Value *MemCmp = M->getOrInsertFunction("memcmp", B.getInt32Ty(),
855 B.getInt8PtrTy(), B.getInt8PtrTy(),
856 DL.getIntPtrType(Context), nullptr);
853 Value *MemCmp =
854 M->getOrInsertFunction("memcmp", B.getInt32Ty(), B.getInt8PtrTy(),
855 B.getInt8PtrTy(), DL.getIntPtrType(Context));
857856 inferLibFuncAttributes(*M->getFunction("memcmp"), *TLI);
858857 CallInst *CI = B.CreateCall(
859858 MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "memcmp");
885884 appendTypeSuffix(Op, Name, NameBuffer);
886885
887886 Module *M = B.GetInsertBlock()->getModule();
888 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
889 Op->getType(), nullptr);
887 Value *Callee = M->getOrInsertFunction(Name, Op->getType(), Op->getType());
890888 CallInst *CI = B.CreateCall(Callee, Op, Name);
891889 CI->setAttributes(Attrs);
892890 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
902900
903901 Module *M = B.GetInsertBlock()->getModule();
904902 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(),
905 Op2->getType(), nullptr);
903 Op2->getType());
906904 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
907905 CI->setAttributes(Attrs);
908906 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
917915 return nullptr;
918916
919917 Module *M = B.GetInsertBlock()->getModule();
920 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
921 B.getInt32Ty(), nullptr);
918 Value *PutChar =
919 M->getOrInsertFunction("putchar", B.getInt32Ty(), B.getInt32Ty());
922920 inferLibFuncAttributes(*M->getFunction("putchar"), *TLI);
923921 CallInst *CI = B.CreateCall(PutChar,
924922 B.CreateIntCast(Char,
939937
940938 Module *M = B.GetInsertBlock()->getModule();
941939 Value *PutS =
942 M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy(), nullptr);
940 M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy());
943941 inferLibFuncAttributes(*M->getFunction("puts"), *TLI);
944942 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), "puts");
945943 if (const Function *F = dyn_cast(PutS->stripPointerCasts()))
954952
955953 Module *M = B.GetInsertBlock()->getModule();
956954 Constant *F = M->getOrInsertFunction("fputc", B.getInt32Ty(), B.getInt32Ty(),
957 File->getType(), nullptr);
955 File->getType());
958956 if (File->getType()->isPointerTy())
959957 inferLibFuncAttributes(*M->getFunction("fputc"), *TLI);
960958 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
973971
974972 Module *M = B.GetInsertBlock()->getModule();
975973 StringRef FPutsName = TLI->getName(LibFunc_fputs);
976 Constant *F = M->getOrInsertFunction(
977 FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType(), nullptr);
974 Constant *F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
975 B.getInt8PtrTy(), File->getType());
978976 if (File->getType()->isPointerTy())
979977 inferLibFuncAttributes(*M->getFunction(FPutsName), *TLI);
980978 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs");
994992 StringRef FWriteName = TLI->getName(LibFunc_fwrite);
995993 Constant *F = M->getOrInsertFunction(
996994 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
997 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType(),
998 nullptr);
995 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
996
999997 if (File->getType()->isPointerTy())
1000998 inferLibFuncAttributes(*M->getFunction(FWriteName), *TLI);
1001999 CallInst *CI =
818818 const DataLayout &DL = M->getDataLayout();
819819 IntegerType *PtrType = DL.getIntPtrType((B.GetInsertBlock()->getContext()));
820820 Value *Calloc = M->getOrInsertFunction("calloc", Attrs, B.getInt8PtrTy(),
821 PtrType, PtrType, nullptr);
821 PtrType, PtrType);
822822 CallInst *CI = B.CreateCall(Calloc, { Num, Size }, "calloc");
823823
824824 if (const auto *F = dyn_cast(Calloc->stripPointerCasts()))
12161216 One = ConstantExpr::getFPExtend(One, Op->getType());
12171217
12181218 Module *M = CI->getModule();
1219 Value *NewCallee =
1220 M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),
1221 Op->getType(), B.getInt32Ty(), nullptr);
1219 Value *NewCallee = M->getOrInsertFunction(
1220 TLI->getName(LdExp), Op->getType(), Op->getType(), B.getInt32Ty());
12221221 CallInst *CI = B.CreateCall(NewCallee, {One, LdExpArg});
12231222 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
12241223 CI->setCallingConv(F->getCallingConv());
14411440 }
14421441
14431442 Module *M = OrigCallee->getParent();
1444 Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),
1445 ResTy, ArgTy, nullptr);
1443 Value *Callee =
1444 M->getOrInsertFunction(Name, OrigCallee->getAttributes(), ResTy, ArgTy);
14461445
14471446 if (Instruction *ArgInst = dyn_cast(Arg)) {
14481447 // If the argument is an instruction, it must dominate all uses so put our
605605 // If the program doesn't explicitly call exit, we will need the Exit
606606 // function later on to make an explicit call, so get the function now.
607607 Constant *Exit = Mod->getOrInsertFunction("exit", Type::getVoidTy(Context),
608 Type::getInt32Ty(Context),
609 nullptr);
608 Type::getInt32Ty(Context));
610609
611610 // Run static constructors.
612611 if (!ForceInterpreter) {