llvm.org GIT mirror llvm / 8701bbc
Revert "Turn some C-style vararg into variadic templates" This reverts commit r299699, the examples needs to be updated. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299702 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 2 years ago
22 changed file(s) with 253 addition(s) and 207 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
324323 Constant *getOrInsertFunction(StringRef Name, AttributeList AttributeList,
325 Type *RetTy, ArgsTy... Args) {
326 SmallVector ArgTys{Args...};
327 return getOrInsertFunction(Name, FunctionType::get(RetTy, ArgTys, false),
328 AttributeList);
329 }
324 Type *RetTy, ...) LLVM_END_WITH_NULL;
330325
331326 /// Same as above, but without the attributes.
332 template
333 Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ArgsTy... Args) {
334 return getOrInsertFunction(Name, AttributeList{}, RetTy, Args...);
335 }
327 Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ...)
328 LLVM_END_WITH_NULL;
336329
337330 /// Look up the specified function in the module symbol table. If it does not
338331 /// exist, return null.
3939
4040 Type *VoidTy = Type::getVoidTy(F.getContext());
4141 Constant *CountingFn =
42 F.getParent()->getOrInsertFunction(CountingFunctionName, VoidTy);
42 F.getParent()->getOrInsertFunction(CountingFunctionName,
43 VoidTy, nullptr);
4344 CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
4445 return true;
4546 }
110110 Type::getVoidTy(M.getContext()));
111111 break;
112112 case Intrinsic::memcpy:
113 M.getOrInsertFunction(
114 "memcpy", Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context),
115 Type::getInt8PtrTy(Context), DL.getIntPtrType(Context));
113 M.getOrInsertFunction("memcpy",
114 Type::getInt8PtrTy(Context),
115 Type::getInt8PtrTy(Context),
116 Type::getInt8PtrTy(Context),
117 DL.getIntPtrType(Context), nullptr);
116118 break;
117119 case Intrinsic::memmove:
118 M.getOrInsertFunction(
119 "memmove", Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context),
120 Type::getInt8PtrTy(Context), DL.getIntPtrType(Context));
120 M.getOrInsertFunction("memmove",
121 Type::getInt8PtrTy(Context),
122 Type::getInt8PtrTy(Context),
123 Type::getInt8PtrTy(Context),
124 DL.getIntPtrType(Context), nullptr);
121125 break;
122126 case Intrinsic::memset:
123 M.getOrInsertFunction(
124 "memset", Type::getInt8PtrTy(Context), Type::getInt8PtrTy(Context),
125 Type::getInt32Ty(M.getContext()), DL.getIntPtrType(Context));
127 M.getOrInsertFunction("memset",
128 Type::getInt8PtrTy(Context),
129 Type::getInt8PtrTy(Context),
130 Type::getInt32Ty(M.getContext()),
131 DL.getIntPtrType(Context), nullptr);
126132 break;
127133 case Intrinsic::sqrt:
128134 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)));
1100 M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C), nullptr));
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 =
453 F.getParent()->getOrInsertFunction("__stack_chk_fail", IRB.getVoidTy());
452 Constant *StackChkFail = F.getParent()->getOrInsertFunction(
453 "__stack_chk_fail", IRB.getVoidTy(), nullptr);
454454 IRBFail.CreateCall(StackChkFail, {});
455455 }
456456
479479
480480 bool SjLjEHPrepare::runOnFunction(Function &F) {
481481 Module &M = *F.getParent();
482 RegisterFn = M.getOrInsertFunction("_Unwind_SjLj_Register",
483 Type::getVoidTy(M.getContext()),
484 PointerType::getUnqual(FunctionContextTy));
482 RegisterFn = M.getOrInsertFunction(
483 "_Unwind_SjLj_Register", Type::getVoidTy(M.getContext()),
484 PointerType::getUnqual(FunctionContextTy), nullptr);
485485 UnregisterFn = M.getOrInsertFunction(
486486 "_Unwind_SjLj_Unregister", Type::getVoidTy(M.getContext()),
487 PointerType::getUnqual(FunctionContextTy));
487 PointerType::getUnqual(FunctionContextTy), nullptr);
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 = M->getOrInsertFunction(
484 "__stack_smash_handler", Type::getVoidTy(Context),
485 Type::getInt8PtrTy(Context));
483 Constant *StackChkFail =
484 M->getOrInsertFunction("__stack_smash_handler",
485 Type::getVoidTy(Context),
486 Type::getInt8PtrTy(Context), nullptr);
486487
487488 B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
488489 } else {
489490 Constant *StackChkFail =
490 M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
491
491 M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context),
492 nullptr);
492493 B.CreateCall(StackChkFail, {});
493494 }
494495 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));
1820 StackPtrTy->getPointerTo(0), nullptr);
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);
468 MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, nullptr);
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);
562 Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, nullptr);
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
149190 // getFunction - Look up the specified function in the module symbol table.
150191 // If it does not exist, return null.
151192 //
20972097 Type::getInt8PtrTy(M.getContext()));
20982098
20992099 // MSVC CRT has a function to validate security cookie.
2100 auto *SecurityCheckCookie = cast(M.getOrInsertFunction(
2101 "__security_check_cookie", Type::getVoidTy(M.getContext()),
2102 Type::getInt8PtrTy(M.getContext())));
2100 auto *SecurityCheckCookie = cast(
2101 M.getOrInsertFunction("__security_check_cookie",
2102 Type::getVoidTy(M.getContext()),
2103 Type::getInt8PtrTy(M.getContext()), nullptr));
21032104 SecurityCheckCookie->setCallingConv(CallingConv::X86_FastCall);
21042105 SecurityCheckCookie->addAttribute(1, Attribute::AttrKind::InReg);
21052106 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));
100 Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx), nullptr);
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 =
118 M.getOrInsertFunction("__cfi_check_fail", Type::getVoidTy(Ctx),
119 Type::getInt8PtrTy(Ctx), Type::getInt8PtrTy(Ctx));
117 Constant *CFICheckFailFn = M.getOrInsertFunction(
118 "__cfi_check_fail", Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx),
119 Type::getInt8PtrTy(Ctx), nullptr);
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(Res.SingleImplName,
1223 Type::getVoidTy(M.getContext()));
1222 auto *SingleImpl = M.getOrInsertFunction(
1223 Res.SingleImplName, Type::getVoidTy(M.getContext()), nullptr);
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(
1592 M.getOrInsertFunction(kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy));
1591 AsanPoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1592 kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
15931593 AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
1594 AsanUnpoisonGlobals = checkSanitizerInterfaceFunction(
1595 M.getOrInsertFunction(kAsanUnpoisonGlobalsName, IRB.getVoidTy()));
1594 AsanUnpoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1595 kAsanUnpoisonGlobalsName, IRB.getVoidTy(), nullptr));
15961596 AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
15971597
15981598 // Declare functions that register/unregister globals.
15991599 AsanRegisterGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1600 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy));
1600 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
16011601 AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
1602 AsanUnregisterGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1603 kAsanUnregisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy));
1602 AsanUnregisterGlobals = checkSanitizerInterfaceFunction(
1603 M.getOrInsertFunction(kAsanUnregisterGlobalsName, IRB.getVoidTy(),
1604 IntptrTy, IntptrTy, nullptr));
16041605 AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
16051606
16061607 // Declare the functions that find globals in a shared object and then invoke
16071608 // the (un)register function on them.
16081609 AsanRegisterImageGlobals =
16091610 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1610 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy));
1611 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
16111612 AsanRegisterImageGlobals->setLinkage(Function::ExternalLinkage);
16121613
16131614 AsanUnregisterImageGlobals =
16141615 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1615 kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy));
1616 kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
16161617 AsanUnregisterImageGlobals->setLinkage(Function::ExternalLinkage);
16171618
16181619 AsanRegisterElfGlobals = checkSanitizerInterfaceFunction(
20902091 const std::string ExpStr = Exp ? "exp_" : "";
20912092 const std::string SuffixStr = CompileKernel ? "N" : "_n";
20922093 const std::string EndingStr = Recover ? "_noabort" : "";
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
2094 Type *ExpType = Exp ? Type::getInt32Ty(*C) : nullptr;
21022095 AsanErrorCallbackSized[AccessIsWrite][Exp] =
21032096 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2104 kAsanReportErrorTemplate + ExpStr + TypeStr + SuffixStr +
2105 EndingStr,
2106 FunctionType::get(IRB.getVoidTy(), Args2, false)));
2107
2097 kAsanReportErrorTemplate + ExpStr + TypeStr + SuffixStr + EndingStr,
2098 IRB.getVoidTy(), IntptrTy, IntptrTy, ExpType, nullptr));
21082099 AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] =
21092100 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21102101 ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
2111 FunctionType::get(IRB.getVoidTy(), Args2, false)));
2112
2102 IRB.getVoidTy(), IntptrTy, IntptrTy, ExpType, nullptr));
21132103 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
21142104 AccessSizeIndex++) {
21152105 const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
21162106 AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
21172107 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21182108 kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
2119 FunctionType::get(IRB.getVoidTy(), Args1, false)));
2120
2109 IRB.getVoidTy(), IntptrTy, ExpType, nullptr));
21212110 AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
21222111 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21232112 ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
2124 FunctionType::get(IRB.getVoidTy(), Args1, false)));
2113 IRB.getVoidTy(), IntptrTy, ExpType, nullptr));
21252114 }
21262115 }
21272116 }
21302119 CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
21312120 AsanMemmove = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21322121 MemIntrinCallbackPrefix + "memmove", IRB.getInt8PtrTy(),
2133 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
2122 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr));
21342123 AsanMemcpy = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21352124 MemIntrinCallbackPrefix + "memcpy", IRB.getInt8PtrTy(),
2136 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
2125 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr));
21372126 AsanMemset = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
21382127 MemIntrinCallbackPrefix + "memset", IRB.getInt8PtrTy(),
2139 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy));
2128 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy, nullptr));
21402129
21412130 AsanHandleNoReturnFunc = checkSanitizerInterfaceFunction(
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));
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));
21482137 // We insert an empty inline asm after __asan_report* to avoid callback merge.
21492138 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
21502139 StringRef(""), StringRef(""),
23792368 IRBuilder<> IRB(*C);
23802369 for (int i = 0; i <= kMaxAsanStackMallocSizeClass; i++) {
23812370 std::string Suffix = itostr(i);
2382 AsanStackMallocFunc[i] =
2383 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2384 kAsanStackMallocNameTemplate + Suffix, IntptrTy, IntptrTy));
2371 AsanStackMallocFunc[i] = checkSanitizerInterfaceFunction(
2372 M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy,
2373 IntptrTy, nullptr));
23852374 AsanStackFreeFunc[i] = checkSanitizerInterfaceFunction(
23862375 M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
2387 IRB.getVoidTy(), IntptrTy, IntptrTy));
2376 IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
23882377 }
23892378 if (ASan.UseAfterScope) {
2390 AsanPoisonStackMemoryFunc =
2391 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2392 kAsanPoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy));
2393 AsanUnpoisonStackMemoryFunc =
2394 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2395 kAsanUnpoisonStackMemoryName, IRB.getVoidTy(), IntptrTy, IntptrTy));
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));
23962385 }
23972386
23982387 for (size_t Val : {0x00, 0xf1, 0xf2, 0xf3, 0xf5, 0xf8}) {
23992388 std::ostringstream Name;
24002389 Name << kAsanSetShadowPrefix;
24012390 Name << std::setw(2) << std::setfill('0') << std::hex << Val;
2402 AsanSetShadowFunc[Val] = checkSanitizerInterfaceFunction(
2403 M.getOrInsertFunction(Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy));
2391 AsanSetShadowFunc[Val] =
2392 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2393 Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
24042394 }
24052395
24062396 AsanAllocaPoisonFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2407 kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy));
2397 kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
24082398 AsanAllocasUnpoisonFunc =
24092399 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2410 kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy));
2400 kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
24112401 }
24122402
24132403 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()));
269 AlignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
270270 SmallString<32> AlignedStoreName("__esan_aligned_store" + ByteSizeStr);
271271 EsanAlignedStore[Idx] =
272272 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
273 AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
273 AlignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
274274 SmallString<32> UnalignedLoadName("__esan_unaligned_load" + ByteSizeStr);
275275 EsanUnalignedLoad[Idx] =
276276 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
277 UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
277 UnalignedLoadName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
278278 SmallString<32> UnalignedStoreName("__esan_unaligned_store" + ByteSizeStr);
279279 EsanUnalignedStore[Idx] =
280280 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
281 UnalignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
282 }
283 EsanUnalignedLoadN = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
284 "__esan_unaligned_loadN", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy));
281 UnalignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
282 }
283 EsanUnalignedLoadN = checkSanitizerInterfaceFunction(
284 M.getOrInsertFunction("__esan_unaligned_loadN", IRB.getVoidTy(),
285 IRB.getInt8PtrTy(), IntptrTy, nullptr));
285286 EsanUnalignedStoreN = checkSanitizerInterfaceFunction(
286287 M.getOrInsertFunction("__esan_unaligned_storeN", IRB.getVoidTy(),
287 IRB.getInt8PtrTy(), IntptrTy));
288 IRB.getInt8PtrTy(), IntptrTy, nullptr));
288289 MemmoveFn = checkSanitizerInterfaceFunction(
289290 M.getOrInsertFunction("memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
290 IRB.getInt8PtrTy(), IntptrTy));
291 IRB.getInt8PtrTy(), IntptrTy, nullptr));
291292 MemcpyFn = checkSanitizerInterfaceFunction(
292293 M.getOrInsertFunction("memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
293 IRB.getInt8PtrTy(), IntptrTy));
294 IRB.getInt8PtrTy(), IntptrTy, nullptr));
294295 MemsetFn = checkSanitizerInterfaceFunction(
295296 M.getOrInsertFunction("memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
296 IRB.getInt32Ty(), IntptrTy));
297 IRB.getInt32Ty(), IntptrTy, nullptr));
297298 }
298299
299300 bool EfficiencySanitizer::shouldIgnoreStructType(StructType *StructTy) {
530531 ReturnInst::Create(*Ctx, BasicBlock::Create(*Ctx, "", EsanDtorFunction));
531532 IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator());
532533 Function *EsanExit = checkSanitizerInterfaceFunction(
533 M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(), Int8PtrTy));
534 M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(),
535 Int8PtrTy, nullptr));
534536 EsanExit->setLinkage(Function::ExternalLinkage);
535537 IRB_Dtor.CreateCall(EsanExit, {ToolInfoArg});
536538 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());
427 WarningFn = M.getOrInsertFunction(WarningFnName, IRB.getVoidTy(), nullptr);
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] =
434 M.getOrInsertFunction(FunctionName, IRB.getVoidTy(),
435 IRB.getIntNTy(AccessSize * 8), IRB.getInt32Ty());
433 MaybeWarningFn[AccessSizeIndex] = M.getOrInsertFunction(
434 FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8),
435 IRB.getInt32Ty(), nullptr);
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());
440 IRB.getInt8PtrTy(), IRB.getInt32Ty(), nullptr);
441441 }
442442
443443 MsanSetAllocaOrigin4Fn = M.getOrInsertFunction(
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);
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);
459460
460461 // Create globals.
461462 RetvalTLS = new GlobalVariable(
261261 Int32Ty = IRB.getInt32Ty();
262262
263263 SanCovFunction = checkSanitizerInterfaceFunction(
264 M.getOrInsertFunction(SanCovName, VoidTy, Int32PtrTy));
264 M.getOrInsertFunction(SanCovName, VoidTy, Int32PtrTy, nullptr));
265265 SanCovWithCheckFunction = checkSanitizerInterfaceFunction(
266 M.getOrInsertFunction(SanCovWithCheckName, VoidTy, Int32PtrTy));
266 M.getOrInsertFunction(SanCovWithCheckName, VoidTy, Int32PtrTy, nullptr));
267267 SanCovTracePCIndir = checkSanitizerInterfaceFunction(
268 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy));
269 SanCovIndirCallFunction = checkSanitizerInterfaceFunction(
270 M.getOrInsertFunction(SanCovIndirCallName, VoidTy, IntptrTy, IntptrTy));
268 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy, nullptr));
269 SanCovIndirCallFunction =
270 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
271 SanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
271272 SanCovTraceCmpFunction[0] =
272273 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
273 SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty()));
274 SanCovTraceCmpFunction[1] =
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] =
275282 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
276 SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(), IRB.getInt16Ty()));
277 SanCovTraceCmpFunction[2] =
283 SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty, nullptr));
284
285 SanCovTraceDivFunction[0] =
278286 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
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));
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));
289294 SanCovTraceSwitchFunction =
290295 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
291 SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy));
296 SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy, nullptr));
292297
293298 // We insert an empty inline asm after cov callbacks to avoid callback merge.
294299 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
296301 /*hasSideEffects=*/true);
297302
298303 SanCovTracePC = checkSanitizerInterfaceFunction(
299 M.getOrInsertFunction(SanCovTracePCName, VoidTy));
300 SanCovTracePCGuard = checkSanitizerInterfaceFunction(
301 M.getOrInsertFunction(SanCovTracePCGuardName, VoidTy, Int32PtrTy));
304 M.getOrInsertFunction(SanCovTracePCName, VoidTy, nullptr));
305 SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
306 SanCovTracePCGuardName, VoidTy, Int32PtrTy, nullptr));
302307 SanCovTraceEnter = checkSanitizerInterfaceFunction(
303 M.getOrInsertFunction(SanCovTraceEnterName, VoidTy, Int32PtrTy));
308 M.getOrInsertFunction(SanCovTraceEnterName, VoidTy, Int32PtrTy, nullptr));
304309 SanCovTraceBB = checkSanitizerInterfaceFunction(
305 M.getOrInsertFunction(SanCovTraceBBName, VoidTy, Int32PtrTy));
310 M.getOrInsertFunction(SanCovTraceBBName, VoidTy, Int32PtrTy, nullptr));
306311
307312 // At this point we create a dummy array of guards because we don't
308313 // 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()));
162 "__tsan_func_entry", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
163163 TsanFuncExit = checkSanitizerInterfaceFunction(
164 M.getOrInsertFunction("__tsan_func_exit", Attr, IRB.getVoidTy()));
164 M.getOrInsertFunction("__tsan_func_exit", Attr, IRB.getVoidTy(), nullptr));
165165 TsanIgnoreBegin = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
166 "__tsan_ignore_thread_begin", Attr, IRB.getVoidTy()));
167 TsanIgnoreEnd = checkSanitizerInterfaceFunction(
168 M.getOrInsertFunction("__tsan_ignore_thread_end", Attr, IRB.getVoidTy()));
166 "__tsan_ignore_thread_begin", Attr, IRB.getVoidTy(), nullptr));
167 TsanIgnoreEnd = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
168 "__tsan_ignore_thread_end", Attr, IRB.getVoidTy(), nullptr));
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()));
177 ReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
178178
179179 SmallString<32> WriteName("__tsan_write" + ByteSizeStr);
180180 TsanWrite[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
181 WriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
181 WriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
182182
183183 SmallString<64> UnalignedReadName("__tsan_unaligned_read" + ByteSizeStr);
184184 TsanUnalignedRead[i] =
185185 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
186 UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
186 UnalignedReadName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
187187
188188 SmallString<64> UnalignedWriteName("__tsan_unaligned_write" + ByteSizeStr);
189189 TsanUnalignedWrite[i] =
190190 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
191 UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
191 UnalignedWriteName, Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
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));
197 M.getOrInsertFunction(AtomicLoadName, Attr, Ty, PtrTy, OrdTy, nullptr));
198198
199199 SmallString<32> AtomicStoreName("__tsan_atomic" + BitSizeStr + "_store");
200200 TsanAtomicStore[i] = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
201 AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy));
201 AtomicStoreName, Attr, IRB.getVoidTy(), PtrTy, Ty, OrdTy, nullptr));
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));
225 M.getOrInsertFunction(RMWName, Attr, Ty, PtrTy, Ty, OrdTy, nullptr));
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));
231 AtomicCASName, Attr, Ty, PtrTy, Ty, Ty, OrdTy, OrdTy, nullptr));
232232 }
233233 TsanVptrUpdate = checkSanitizerInterfaceFunction(
234234 M.getOrInsertFunction("__tsan_vptr_update", Attr, IRB.getVoidTy(),
235 IRB.getInt8PtrTy(), IRB.getInt8PtrTy()));
235 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), nullptr));
236236 TsanVptrLoad = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
237 "__tsan_vptr_read", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy()));
237 "__tsan_vptr_read", Attr, IRB.getVoidTy(), IRB.getInt8PtrTy(), nullptr));
238238 TsanAtomicThreadFence = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
239 "__tsan_atomic_thread_fence", Attr, IRB.getVoidTy(), OrdTy));
239 "__tsan_atomic_thread_fence", Attr, IRB.getVoidTy(), OrdTy, nullptr));
240240 TsanAtomicSignalFence = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
241 "__tsan_atomic_signal_fence", Attr, IRB.getVoidTy(), OrdTy));
241 "__tsan_atomic_signal_fence", Attr, IRB.getVoidTy(), OrdTy, nullptr));
242242
243243 MemmoveFn = checkSanitizerInterfaceFunction(
244 M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(),
245 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
244 M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
245 IRB.getInt8PtrTy(), IntptrTy, nullptr));
246246 MemcpyFn = checkSanitizerInterfaceFunction(
247 M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(),
248 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
247 M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
248 IRB.getInt8PtrTy(), IntptrTy, nullptr));
249249 MemsetFn = checkSanitizerInterfaceFunction(
250 M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(),
251 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy));
250 M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
251 IRB.getInt32Ty(), IntptrTy, nullptr));
252252 }
253253
254254 bool ThreadSanitizer::doInitialization(Module &M) {
820820 Type *Int8PtrTy = DestInt8PtrTy;
821821
822822 Module *M = TheStore->getModule();
823 Value *MSP = M->getOrInsertFunction("memset_pattern16", Builder.getVoidTy(),
824 Int8PtrTy, Int8PtrTy, IntPtr);
823 Value *MSP =
824 M->getOrInsertFunction("memset_pattern16", Builder.getVoidTy(),
825 Int8PtrTy, Int8PtrTy, IntPtr, (void *)nullptr);
825826 inferLibFuncAttributes(*M->getFunction("memset_pattern16"), *TLI);
826827
827828 // 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());
725 B.getInt8PtrTy(), nullptr);
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 = M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty);
742 Constant *StrChr =
743 M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty, nullptr);
743744 inferLibFuncAttributes(*M->getFunction("strchr"), *TLI);
744745 CallInst *CI = B.CreateCall(
745746 StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
755756
756757 Module *M = B.GetInsertBlock()->getModule();
757758 LLVMContext &Context = B.GetInsertBlock()->getContext();
758 Value *StrNCmp =
759 M->getOrInsertFunction("strncmp", B.getInt32Ty(), B.getInt8PtrTy(),
760 B.getInt8PtrTy(), DL.getIntPtrType(Context));
759 Value *StrNCmp = M->getOrInsertFunction("strncmp", B.getInt32Ty(),
760 B.getInt8PtrTy(), B.getInt8PtrTy(),
761 DL.getIntPtrType(Context), nullptr);
761762 inferLibFuncAttributes(*M->getFunction("strncmp"), *TLI);
762763 CallInst *CI = B.CreateCall(
763764 StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "strncmp");
775776
776777 Module *M = B.GetInsertBlock()->getModule();
777778 Type *I8Ptr = B.getInt8PtrTy();
778 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
779 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, nullptr);
779780 inferLibFuncAttributes(*M->getFunction(Name), *TLI);
780781 CallInst *CI =
781782 B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
791792
792793 Module *M = B.GetInsertBlock()->getModule();
793794 Type *I8Ptr = B.getInt8PtrTy();
794 Value *StrNCpy =
795 M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, Len->getType());
795 Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr,
796 Len->getType(), nullptr);
796797 inferLibFuncAttributes(*M->getFunction(Name), *TLI);
797798 CallInst *CI = B.CreateCall(
798799 StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, "strncpy");
815816 Value *MemCpy = M->getOrInsertFunction(
816817 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
817818 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
818 DL.getIntPtrType(Context));
819 DL.getIntPtrType(Context), nullptr);
819820 Dst = castToCStr(Dst, B);
820821 Src = castToCStr(Src, B);
821822 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
831832
832833 Module *M = B.GetInsertBlock()->getModule();
833834 LLVMContext &Context = B.GetInsertBlock()->getContext();
834 Value *MemChr =
835 M->getOrInsertFunction("memchr", B.getInt8PtrTy(), B.getInt8PtrTy(),
836 B.getInt32Ty(), DL.getIntPtrType(Context));
835 Value *MemChr = M->getOrInsertFunction("memchr", B.getInt8PtrTy(),
836 B.getInt8PtrTy(), B.getInt32Ty(),
837 DL.getIntPtrType(Context), nullptr);
837838 inferLibFuncAttributes(*M->getFunction("memchr"), *TLI);
838839 CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, "memchr");
839840
850851
851852 Module *M = B.GetInsertBlock()->getModule();
852853 LLVMContext &Context = B.GetInsertBlock()->getContext();
853 Value *MemCmp =
854 M->getOrInsertFunction("memcmp", B.getInt32Ty(), B.getInt8PtrTy(),
855 B.getInt8PtrTy(), DL.getIntPtrType(Context));
854 Value *MemCmp = M->getOrInsertFunction("memcmp", B.getInt32Ty(),
855 B.getInt8PtrTy(), B.getInt8PtrTy(),
856 DL.getIntPtrType(Context), nullptr);
856857 inferLibFuncAttributes(*M->getFunction("memcmp"), *TLI);
857858 CallInst *CI = B.CreateCall(
858859 MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "memcmp");
884885 appendTypeSuffix(Op, Name, NameBuffer);
885886
886887 Module *M = B.GetInsertBlock()->getModule();
887 Value *Callee = M->getOrInsertFunction(Name, Op->getType(), Op->getType());
888 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
889 Op->getType(), nullptr);
888890 CallInst *CI = B.CreateCall(Callee, Op, Name);
889891 CI->setAttributes(Attrs);
890892 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
900902
901903 Module *M = B.GetInsertBlock()->getModule();
902904 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(),
903 Op2->getType());
905 Op2->getType(), nullptr);
904906 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
905907 CI->setAttributes(Attrs);
906908 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
915917 return nullptr;
916918
917919 Module *M = B.GetInsertBlock()->getModule();
918 Value *PutChar =
919 M->getOrInsertFunction("putchar", B.getInt32Ty(), B.getInt32Ty());
920 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
921 B.getInt32Ty(), nullptr);
920922 inferLibFuncAttributes(*M->getFunction("putchar"), *TLI);
921923 CallInst *CI = B.CreateCall(PutChar,
922924 B.CreateIntCast(Char,
937939
938940 Module *M = B.GetInsertBlock()->getModule();
939941 Value *PutS =
940 M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy());
942 M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy(), nullptr);
941943 inferLibFuncAttributes(*M->getFunction("puts"), *TLI);
942944 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), "puts");
943945 if (const Function *F = dyn_cast(PutS->stripPointerCasts()))
952954
953955 Module *M = B.GetInsertBlock()->getModule();
954956 Constant *F = M->getOrInsertFunction("fputc", B.getInt32Ty(), B.getInt32Ty(),
955 File->getType());
957 File->getType(), nullptr);
956958 if (File->getType()->isPointerTy())
957959 inferLibFuncAttributes(*M->getFunction("fputc"), *TLI);
958960 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
971973
972974 Module *M = B.GetInsertBlock()->getModule();
973975 StringRef FPutsName = TLI->getName(LibFunc_fputs);
974 Constant *F = M->getOrInsertFunction(FPutsName, B.getInt32Ty(),
975 B.getInt8PtrTy(), File->getType());
976 Constant *F = M->getOrInsertFunction(
977 FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType(), nullptr);
976978 if (File->getType()->isPointerTy())
977979 inferLibFuncAttributes(*M->getFunction(FPutsName), *TLI);
978980 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs");
992994 StringRef FWriteName = TLI->getName(LibFunc_fwrite);
993995 Constant *F = M->getOrInsertFunction(
994996 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
995 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
996
997 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType(),
998 nullptr);
997999 if (File->getType()->isPointerTy())
9981000 inferLibFuncAttributes(*M->getFunction(FWriteName), *TLI);
9991001 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);
821 PtrType, PtrType, nullptr);
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 = M->getOrInsertFunction(
1220 TLI->getName(LdExp), Op->getType(), Op->getType(), B.getInt32Ty());
1219 Value *NewCallee =
1220 M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),
1221 Op->getType(), B.getInt32Ty(), nullptr);
12211222 CallInst *CI = B.CreateCall(NewCallee, {One, LdExpArg});
12221223 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
12231224 CI->setCallingConv(F->getCallingConv());
14401441 }
14411442
14421443 Module *M = OrigCallee->getParent();
1443 Value *Callee =
1444 M->getOrInsertFunction(Name, OrigCallee->getAttributes(), ResTy, ArgTy);
1444 Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),
1445 ResTy, ArgTy, nullptr);
14451446
14461447 if (Instruction *ArgInst = dyn_cast(Arg)) {
14471448 // 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));
608 Type::getInt32Ty(Context),
609 nullptr);
609610
610611 // Run static constructors.
611612 if (!ForceInterpreter) {