llvm.org GIT mirror llvm / 9d54400
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. Differential Revision: https://reviews.llvm.org/D31070 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299949 91177308-0d34-0410-b5e6-96231b3b80d8 Serge Guelton 2 years ago
30 changed file(s) with 171 addition(s) and 200 deletion(s). Raw diff Collapse all Expand all
7373
7474 //declare i32 @getchar()
7575 getchar_func = cast(module->
76 getOrInsertFunction("getchar", IntegerType::getInt32Ty(C), NULL));
76 getOrInsertFunction("getchar", IntegerType::getInt32Ty(C)));
7777
7878 //declare i32 @putchar(i32)
7979 putchar_func = cast(module->
8080 getOrInsertFunction("putchar", IntegerType::getInt32Ty(C),
81 IntegerType::getInt32Ty(C), NULL));
81 IntegerType::getInt32Ty(C)));
8282
8383 //Function header
8484
8585 //define void @brainf()
8686 brainf_func = cast(module->
87 getOrInsertFunction("brainf", Type::getVoidTy(C), NULL));
87 getOrInsertFunction("brainf", Type::getVoidTy(C)));
8888
8989 builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func));
9090
155155 //declare i32 @puts(i8 *)
156156 Function *puts_func = cast(module->
157157 getOrInsertFunction("puts", IntegerType::getInt32Ty(C),
158 PointerType::getUnqual(IntegerType::getInt8Ty(C)), NULL));
158 PointerType::getUnqual(IntegerType::getInt8Ty(C))));
159159
160160 //brainf.aberror:
161161 aberrorbb = BasicBlock::Create(C, label, brainf_func);
7676 getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()),
7777 IntegerType::getInt32Ty(mod->getContext()),
7878 PointerType::getUnqual(PointerType::getUnqual(
79 IntegerType::getInt8Ty(mod->getContext()))), NULL));
79 IntegerType::getInt8Ty(mod->getContext())))));
8080 {
8181 Function::arg_iterator args = main_func->arg_begin();
8282 Value *arg_0 = &*args++;
5353 // to return an int and take an int parameter.
5454 Function *FibF =
5555 cast(M->getOrInsertFunction("fib", Type::getInt32Ty(Context),
56 Type::getInt32Ty(Context),
57 nullptr));
56 Type::getInt32Ty(Context)));
5857
5958 // Add a basic block to the function.
6059 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF);
6868
6969 // Create the add1 function entry and insert this entry into module M. The
7070 // function will have a return type of "int" and take an argument of "int".
71 // The '0' terminates the list of argument types.
7271 Function *Add1F =
7372 cast(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
74 Type::getInt32Ty(Context),
75 nullptr));
73 Type::getInt32Ty(Context)));
7674
7775 // Add a basic block to the function. As before, it automatically inserts
7876 // because of the last argument.
10199 // Now we're going to create function `foo', which returns an int and takes no
102100 // arguments.
103101 Function *FooF =
104 cast(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
105 nullptr));
102 cast(M->getOrInsertFunction("foo", Type::getInt32Ty(Context)));
106103
107104 // Add a basic block to the FooF function.
108105 BB = BasicBlock::Create(Context, "EntryBlock", FooF);
5353 Function *Add1F =
5454 cast(M->getOrInsertFunction("add1",
5555 Type::getInt32Ty(M->getContext()),
56 Type::getInt32Ty(M->getContext()),
57 nullptr));
56 Type::getInt32Ty(M->getContext())));
5857
5958 // Add a basic block to the function. As before, it automatically inserts
6059 // because of the last argument.
8483 Function *FibF =
8584 cast(M->getOrInsertFunction("fib",
8685 Type::getInt32Ty(M->getContext()),
87 Type::getInt32Ty(M->getContext()),
88 nullptr));
86 Type::getInt32Ty(M->getContext())));
8987
9088 // Add a basic block to the function.
9189 BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);
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 Constant *getOrInsertFunction(StringRef Name, AttributeList AttributeList,
324 Type *RetTy, ...) LLVM_END_WITH_NULL;
323 template
324 Constant *getOrInsertFunction(StringRef Name,
325 AttributeList AttributeList,
326 Type *RetTy, ArgsTy... Args)
327 {
328 SmallVector ArgTys{Args...};
329 return getOrInsertFunction(Name,
330 FunctionType::get(RetTy, ArgTys, false),
331 AttributeList);
332 }
325333
326334 /// Same as above, but without the attributes.
327 Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ...)
328 LLVM_END_WITH_NULL;
335 template
336 Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ArgsTy... Args) {
337 return getOrInsertFunction(Name, AttributeList{}, RetTy, Args...);
338 }
329339
330340 /// Look up the specified function in the module symbol table. If it does not
331341 /// exist, return null.
4040 Type *VoidTy = Type::getVoidTy(F.getContext());
4141 Constant *CountingFn =
4242 F.getParent()->getOrInsertFunction(CountingFunctionName,
43 VoidTy, nullptr);
43 VoidTy);
4444 CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
4545 return true;
4646 }
114114 Type::getInt8PtrTy(Context),
115115 Type::getInt8PtrTy(Context),
116116 Type::getInt8PtrTy(Context),
117 DL.getIntPtrType(Context), nullptr);
117 DL.getIntPtrType(Context));
118118 break;
119119 case Intrinsic::memmove:
120120 M.getOrInsertFunction("memmove",
121121 Type::getInt8PtrTy(Context),
122122 Type::getInt8PtrTy(Context),
123123 Type::getInt8PtrTy(Context),
124 DL.getIntPtrType(Context), nullptr);
124 DL.getIntPtrType(Context));
125125 break;
126126 case Intrinsic::memset:
127127 M.getOrInsertFunction("memset",
128128 Type::getInt8PtrTy(Context),
129129 Type::getInt8PtrTy(Context),
130130 Type::getInt32Ty(M.getContext()),
131 DL.getIntPtrType(Context), nullptr);
131 DL.getIntPtrType(Context));
132132 break;
133133 case Intrinsic::sqrt:
134134 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
450450 IRBuilder<> IRBFail(CheckTerm);
451451 // FIXME: respect -fsanitize-trap / -ftrap-function here?
452452 Constant *StackChkFail = F.getParent()->getOrInsertFunction(
453 "__stack_chk_fail", IRB.getVoidTy(), nullptr);
453 "__stack_chk_fail", IRB.getVoidTy());
454454 IRBFail.CreateCall(StackChkFail, {});
455455 }
456456
481481 Module &M = *F.getParent();
482482 RegisterFn = M.getOrInsertFunction(
483483 "_Unwind_SjLj_Register", Type::getVoidTy(M.getContext()),
484 PointerType::getUnqual(FunctionContextTy), nullptr);
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);
483483 Constant *StackChkFail =
484484 M->getOrInsertFunction("__stack_smash_handler",
485485 Type::getVoidTy(Context),
486 Type::getInt8PtrTy(Context), nullptr);
486 Type::getInt8PtrTy(Context));
487487
488488 B.CreateCall(StackChkFail, B.CreateGlobalStringPtr(F->getName(), "SSH"));
489489 } else {
490490 Constant *StackChkFail =
491 M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context),
492 nullptr);
491 M->getOrInsertFunction("__stack_chk_fail", Type::getVoidTy(Context));
492
493493 B.CreateCall(StackChkFail, {});
494494 }
495495 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 //
21452145 Type *VoidTy = Type::getVoidTy(Ctx);
21462146 Module *M = Func->getParent();
21472147 Constant *CF = M->getOrInsertFunction(HexagonVolatileMemcpyName, VoidTy,
2148 Int32PtrTy, Int32PtrTy, Int32Ty,
2149 nullptr);
2148 Int32PtrTy, Int32PtrTy, Int32Ty);
21502149 Function *Fn = cast(CF);
21512150 Fn->setLinkage(Function::ExternalLinkage);
21522151
419419 Attribute::ReadNone);
420420 A = A.addAttribute(C, AttributeList::FunctionIndex,
421421 Attribute::NoInline);
422 Value *F = (M->getOrInsertFunction(Name, A, MyVoid, T, nullptr));
422 Value *F = (M->getOrInsertFunction(Name, A, MyVoid, T));
423423 CallInst::Create(F, Params, "", &I);
424424 } else if (const CallInst *CI = dyn_cast(&I)) {
425425 FunctionType *FT = CI->getFunctionType();
21002100 auto *SecurityCheckCookie = cast(
21012101 M.getOrInsertFunction("__security_check_cookie",
21022102 Type::getVoidTy(M.getContext()),
2103 Type::getInt8PtrTy(M.getContext()), nullptr));
2103 Type::getInt8PtrTy(M.getContext())));
21042104 SecurityCheckCookie->setCallingConv(CallingConv::X86_FastCall);
21052105 SecurityCheckCookie->addAttribute(1, Attribute::AttrKind::InReg);
21062106 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 // Take over the existing function. The frontend emits a weak stub so that the
103103 // linker knows about the symbol; this pass replaces the function body.
119119 IRBuilder<> IRBFail(TrapBB);
120120 Constant *CFICheckFailFn = M.getOrInsertFunction(
121121 "__cfi_check_fail", Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx),
122 Type::getInt8PtrTy(Ctx), nullptr);
122 Type::getInt8PtrTy(Ctx));
123123 IRBFail.CreateCall(CFICheckFailFn, {&CFICheckFailData, &Addr});
124124 IRBFail.CreateBr(ExitBB);
125125
12201220 // The type of the function in the declaration is irrelevant because every
12211221 // call site will cast it to the correct type.
12221222 auto *SingleImpl = M.getOrInsertFunction(
1223 Res.SingleImplName, Type::getVoidTy(M.getContext()), nullptr);
1223 Res.SingleImplName, Type::getVoidTy(M.getContext()));
12241224
12251225 // This is the import phase so we should not be exporting anything.
12261226 bool IsExported = false;
15671567
15681568 // Declare our poisoning and unpoisoning functions.
15691569 AsanPoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1570 kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
1570 kAsanPoisonGlobalsName, IRB.getVoidTy(), IntptrTy));
15711571 AsanPoisonGlobals->setLinkage(Function::ExternalLinkage);
15721572 AsanUnpoisonGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1573 kAsanUnpoisonGlobalsName, IRB.getVoidTy(), nullptr));
1573 kAsanUnpoisonGlobalsName, IRB.getVoidTy()));
15741574 AsanUnpoisonGlobals->setLinkage(Function::ExternalLinkage);
15751575
15761576 // Declare functions that register/unregister globals.
15771577 AsanRegisterGlobals = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1578 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
1578 kAsanRegisterGlobalsName, IRB.getVoidTy(), IntptrTy, IntptrTy));
15791579 AsanRegisterGlobals->setLinkage(Function::ExternalLinkage);
15801580 AsanUnregisterGlobals = checkSanitizerInterfaceFunction(
15811581 M.getOrInsertFunction(kAsanUnregisterGlobalsName, IRB.getVoidTy(),
1582 IntptrTy, IntptrTy, nullptr));
1582 IntptrTy, IntptrTy));
15831583 AsanUnregisterGlobals->setLinkage(Function::ExternalLinkage);
15841584
15851585 // Declare the functions that find globals in a shared object and then invoke
15861586 // the (un)register function on them.
15871587 AsanRegisterImageGlobals =
15881588 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1589 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
1589 kAsanRegisterImageGlobalsName, IRB.getVoidTy(), IntptrTy));
15901590 AsanRegisterImageGlobals->setLinkage(Function::ExternalLinkage);
15911591
15921592 AsanUnregisterImageGlobals =
15931593 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1594 kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy, nullptr));
1594 kAsanUnregisterImageGlobalsName, IRB.getVoidTy(), IntptrTy));
15951595 AsanUnregisterImageGlobals->setLinkage(Function::ExternalLinkage);
15961596 }
15971597
19621962 const std::string ExpStr = Exp ? "exp_" : "";
19631963 const std::string SuffixStr = CompileKernel ? "N" : "_n";
19641964 const std::string EndingStr = Recover ? "_noabort" : "";
1965 Type *ExpType = Exp ? Type::getInt32Ty(*C) : nullptr;
1966 AsanErrorCallbackSized[AccessIsWrite][Exp] =
1967 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1968 kAsanReportErrorTemplate + ExpStr + TypeStr + SuffixStr + EndingStr,
1969 IRB.getVoidTy(), IntptrTy, IntptrTy, ExpType, nullptr));
1970 AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] =
1971 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1972 ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
1973 IRB.getVoidTy(), IntptrTy, IntptrTy, ExpType, nullptr));
1974 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
1975 AccessSizeIndex++) {
1976 const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
1977 AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
1978 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1979 kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
1980 IRB.getVoidTy(), IntptrTy, ExpType, nullptr));
1981 AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
1982 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1983 ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
1984 IRB.getVoidTy(), IntptrTy, ExpType, nullptr));
1965
1966 SmallVector Args2 = {IntptrTy, IntptrTy};
1967 SmallVector Args1{1, IntptrTy};
1968 if (Exp) {
1969 Type *ExpType = Type::getInt32Ty(*C);
1970 Args2.push_back(ExpType);
1971 Args1.push_back(ExpType);
19851972 }
1986 }
1973 AsanErrorCallbackSized[AccessIsWrite][Exp] =
1974 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1975 kAsanReportErrorTemplate + ExpStr + TypeStr + SuffixStr +
1976 EndingStr,
1977 FunctionType::get(IRB.getVoidTy(), Args2, false)));
1978
1979 AsanMemoryAccessCallbackSized[AccessIsWrite][Exp] =
1980 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1981 ClMemoryAccessCallbackPrefix + ExpStr + TypeStr + "N" + EndingStr,
1982 FunctionType::get(IRB.getVoidTy(), Args2, false)));
1983
1984 for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
1985 AccessSizeIndex++) {
1986 const std::string Suffix = TypeStr + itostr(1ULL << AccessSizeIndex);
1987 AsanErrorCallback[AccessIsWrite][Exp][AccessSizeIndex] =
1988 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1989 kAsanReportErrorTemplate + ExpStr + Suffix + EndingStr,
1990 FunctionType::get(IRB.getVoidTy(), Args1, false)));
1991
1992 AsanMemoryAccessCallback[AccessIsWrite][Exp][AccessSizeIndex] =
1993 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
1994 ClMemoryAccessCallbackPrefix + ExpStr + Suffix + EndingStr,
1995 FunctionType::get(IRB.getVoidTy(), Args1, false)));
1996 }
1997 }
19871998 }
19881999
19892000 const std::string MemIntrinCallbackPrefix =
19902001 CompileKernel ? std::string("") : ClMemoryAccessCallbackPrefix;
19912002 AsanMemmove = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
19922003 MemIntrinCallbackPrefix + "memmove", IRB.getInt8PtrTy(),
1993 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr));
2004 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
19942005 AsanMemcpy = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
19952006 MemIntrinCallbackPrefix + "memcpy", IRB.getInt8PtrTy(),
1996 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy, nullptr));
2007 IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IntptrTy));
19972008 AsanMemset = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
19982009 MemIntrinCallbackPrefix + "memset", IRB.getInt8PtrTy(),
1999 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy, nullptr));
2010 IRB.getInt8PtrTy(), IRB.getInt32Ty(), IntptrTy));
20002011
20012012 AsanHandleNoReturnFunc = checkSanitizerInterfaceFunction(
2002 M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy(), nullptr));
2013 M.getOrInsertFunction(kAsanHandleNoReturnName, IRB.getVoidTy()));
20032014
20042015 AsanPtrCmpFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2005 kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2016 kAsanPtrCmp, IRB.getVoidTy(), IntptrTy, IntptrTy));
20062017 AsanPtrSubFunction = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2007 kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2018 kAsanPtrSub, IRB.getVoidTy(), IntptrTy, IntptrTy));
20082019 // We insert an empty inline asm after __asan_report* to avoid callback merge.
20092020 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
20102021 StringRef(""), StringRef(""),
22412252 std::string Suffix = itostr(i);
22422253 AsanStackMallocFunc[i] = checkSanitizerInterfaceFunction(
22432254 M.getOrInsertFunction(kAsanStackMallocNameTemplate + Suffix, IntptrTy,
2244 IntptrTy, nullptr));
2255 IntptrTy));
22452256 AsanStackFreeFunc[i] = checkSanitizerInterfaceFunction(
22462257 M.getOrInsertFunction(kAsanStackFreeNameTemplate + Suffix,
2247 IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2258 IRB.getVoidTy(), IntptrTy, IntptrTy));
22482259 }
22492260 if (ASan.UseAfterScope) {
22502261 AsanPoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
22512262 M.getOrInsertFunction(kAsanPoisonStackMemoryName, IRB.getVoidTy(),
2252 IntptrTy, IntptrTy, nullptr));
2263 IntptrTy, IntptrTy));
22532264 AsanUnpoisonStackMemoryFunc = checkSanitizerInterfaceFunction(
22542265 M.getOrInsertFunction(kAsanUnpoisonStackMemoryName, IRB.getVoidTy(),
2255 IntptrTy, IntptrTy, nullptr));
2266 IntptrTy, IntptrTy));
22562267 }
22572268
22582269 for (size_t Val : {0x00, 0xf1, 0xf2, 0xf3, 0xf5, 0xf8}) {
22612272 Name << std::setw(2) << std::setfill('0') << std::hex << Val;
22622273 AsanSetShadowFunc[Val] =
22632274 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2264 Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2275 Name.str(), IRB.getVoidTy(), IntptrTy, IntptrTy));
22652276 }
22662277
22672278 AsanAllocaPoisonFunc = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2268 kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2279 kAsanAllocaPoison, IRB.getVoidTy(), IntptrTy, IntptrTy));
22692280 AsanAllocasUnpoisonFunc =
22702281 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
2271 kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy, nullptr));
2282 kAsanAllocasUnpoison, IRB.getVoidTy(), IntptrTy, IntptrTy));
22722283 }
22732284
22742285 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));
281 UnalignedStoreName, IRB.getVoidTy(), IRB.getInt8PtrTy()));
282282 }
283283 EsanUnalignedLoadN = checkSanitizerInterfaceFunction(
284284 M.getOrInsertFunction("__esan_unaligned_loadN", IRB.getVoidTy(),
285 IRB.getInt8PtrTy(), IntptrTy, nullptr));
285 IRB.getInt8PtrTy(), IntptrTy));
286286 EsanUnalignedStoreN = checkSanitizerInterfaceFunction(
287287 M.getOrInsertFunction("__esan_unaligned_storeN", IRB.getVoidTy(),
288 IRB.getInt8PtrTy(), IntptrTy, nullptr));
288 IRB.getInt8PtrTy(), IntptrTy));
289289 MemmoveFn = checkSanitizerInterfaceFunction(
290290 M.getOrInsertFunction("memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
291 IRB.getInt8PtrTy(), IntptrTy, nullptr));
291 IRB.getInt8PtrTy(), IntptrTy));
292292 MemcpyFn = checkSanitizerInterfaceFunction(
293293 M.getOrInsertFunction("memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
294 IRB.getInt8PtrTy(), IntptrTy, nullptr));
294 IRB.getInt8PtrTy(), IntptrTy));
295295 MemsetFn = checkSanitizerInterfaceFunction(
296296 M.getOrInsertFunction("memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
297 IRB.getInt32Ty(), IntptrTy, nullptr));
297 IRB.getInt32Ty(), IntptrTy));
298298 }
299299
300300 bool EfficiencySanitizer::shouldIgnoreStructType(StructType *StructTy) {
532532 IRBuilder<> IRB_Dtor(EsanDtorFunction->getEntryBlock().getTerminator());
533533 Function *EsanExit = checkSanitizerInterfaceFunction(
534534 M.getOrInsertFunction(EsanExitName, IRB_Dtor.getVoidTy(),
535 Int8PtrTy, nullptr));
535 Int8PtrTy));
536536 EsanExit->setLinkage(Function::ExternalLinkage);
537537 IRB_Dtor.CreateCall(EsanExit, {ToolInfoArg});
538538 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++) {
432432 std::string FunctionName = "__msan_maybe_warning_" + itostr(AccessSize);
433433 MaybeWarningFn[AccessSizeIndex] = M.getOrInsertFunction(
434434 FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8),
435 IRB.getInt32Ty(), nullptr);
435 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(
444444 "__msan_set_alloca_origin4", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy,
445 IRB.getInt8PtrTy(), IntptrTy, nullptr);
445 IRB.getInt8PtrTy(), IntptrTy);
446446 MsanPoisonStackFn =
447447 M.getOrInsertFunction("__msan_poison_stack", IRB.getVoidTy(),
448 IRB.getInt8PtrTy(), IntptrTy, nullptr);
448 IRB.getInt8PtrTy(), IntptrTy);
449449 MsanChainOriginFn = M.getOrInsertFunction(
450 "__msan_chain_origin", IRB.getInt32Ty(), IRB.getInt32Ty(), nullptr);
450 "__msan_chain_origin", IRB.getInt32Ty(), IRB.getInt32Ty());
451451 MemmoveFn = M.getOrInsertFunction(
452452 "__msan_memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
453 IRB.getInt8PtrTy(), IntptrTy, nullptr);
453 IRB.getInt8PtrTy(), IntptrTy);
454454 MemcpyFn = M.getOrInsertFunction(
455455 "__msan_memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
456 IntptrTy, nullptr);
456 IntptrTy);
457457 MemsetFn = M.getOrInsertFunction(
458458 "__msan_memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(),
459 IntptrTy, nullptr);
459 IntptrTy);
460460
461461 // Create globals.
462462 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));
268 M.getOrInsertFunction(SanCovTracePCIndirName, VoidTy, IntptrTy));
269269 SanCovIndirCallFunction =
270270 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
271 SanCovIndirCallName, VoidTy, IntptrTy, IntptrTy, nullptr));
271 SanCovIndirCallName, VoidTy, IntptrTy, IntptrTy));
272272 SanCovTraceCmpFunction[0] =
273273 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
274 SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty(), nullptr));
274 SanCovTraceCmp1, VoidTy, IRB.getInt8Ty(), IRB.getInt8Ty()));
275275 SanCovTraceCmpFunction[1] = checkSanitizerInterfaceFunction(
276276 M.getOrInsertFunction(SanCovTraceCmp2, VoidTy, IRB.getInt16Ty(),
277 IRB.getInt16Ty(), nullptr));
277 IRB.getInt16Ty()));
278278 SanCovTraceCmpFunction[2] = checkSanitizerInterfaceFunction(
279279 M.getOrInsertFunction(SanCovTraceCmp4, VoidTy, IRB.getInt32Ty(),
280 IRB.getInt32Ty(), nullptr));
280 IRB.getInt32Ty()));
281281 SanCovTraceCmpFunction[3] =
282282 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
283 SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty, nullptr));
283 SanCovTraceCmp8, VoidTy, Int64Ty, Int64Ty));
284284
285285 SanCovTraceDivFunction[0] =
286286 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
287 SanCovTraceDiv4, VoidTy, IRB.getInt32Ty(), nullptr));
287 SanCovTraceDiv4, VoidTy, IRB.getInt32Ty()));
288288 SanCovTraceDivFunction[1] =
289289 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
290 SanCovTraceDiv8, VoidTy, Int64Ty, nullptr));
290 SanCovTraceDiv8, VoidTy, Int64Ty));
291291 SanCovTraceGepFunction =
292292 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
293 SanCovTraceGep, VoidTy, IntptrTy, nullptr));
293 SanCovTraceGep, VoidTy, IntptrTy));
294294 SanCovTraceSwitchFunction =
295295 checkSanitizerInterfaceFunction(M.getOrInsertFunction(
296 SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy, nullptr));
296 SanCovTraceSwitchName, VoidTy, Int64Ty, Int64PtrTy));
297297
298298 // We insert an empty inline asm after cov callbacks to avoid callback merge.
299299 EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
301301 /*hasSideEffects=*/true);
302302
303303 SanCovTracePC = checkSanitizerInterfaceFunction(
304 M.getOrInsertFunction(SanCovTracePCName, VoidTy, nullptr));
304 M.getOrInsertFunction(SanCovTracePCName, VoidTy));
305305 SanCovTracePCGuard = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
306 SanCovTracePCGuardName, VoidTy, Int32PtrTy, nullptr));
306 SanCovTracePCGuardName, VoidTy, Int32PtrTy));
307307 SanCovTraceEnter = checkSanitizerInterfaceFunction(
308 M.getOrInsertFunction(SanCovTraceEnterName, VoidTy, Int32PtrTy, nullptr));
308 M.getOrInsertFunction(SanCovTraceEnterName, VoidTy, Int32PtrTy));
309309 SanCovTraceBB = checkSanitizerInterfaceFunction(
310 M.getOrInsertFunction(SanCovTraceBBName, VoidTy, Int32PtrTy, nullptr));
310 M.getOrInsertFunction(SanCovTraceBBName, VoidTy, Int32PtrTy));
311311
312312 // At this point we create a dummy array of guards because we don't
313313 // 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));
166 "__tsan_ignore_thread_begin", Attr, IRB.getVoidTy()));
167167 TsanIgnoreEnd = checkSanitizerInterfaceFunction(M.getOrInsertFunction(
168 "__tsan_ignore_thread_end", Attr, IRB.getVoidTy(), nullptr));
168 "__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(
244244 M.getOrInsertFunction("memmove", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
245 IRB.getInt8PtrTy(), IntptrTy, nullptr));
245 IRB.getInt8PtrTy(), IntptrTy));
246246 MemcpyFn = checkSanitizerInterfaceFunction(
247247 M.getOrInsertFunction("memcpy", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
248 IRB.getInt8PtrTy(), IntptrTy, nullptr));
248 IRB.getInt8PtrTy(), IntptrTy));
249249 MemsetFn = checkSanitizerInterfaceFunction(
250250 M.getOrInsertFunction("memset", Attr, IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
251 IRB.getInt32Ty(), IntptrTy, nullptr));
251 IRB.getInt32Ty(), IntptrTy));
252252 }
253253
254254 bool ThreadSanitizer::doInitialization(Module &M) {
822822 Module *M = TheStore->getModule();
823823 Value *MSP =
824824 M->getOrInsertFunction("memset_pattern16", Builder.getVoidTy(),
825 Int8PtrTy, Int8PtrTy, IntPtr, (void *)nullptr);
825 Int8PtrTy, Int8PtrTy, IntPtr);
826826 inferLibFuncAttributes(*M->getFunction("memset_pattern16"), *TLI);
827827
828828 // 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()))
740740 Type *I8Ptr = B.getInt8PtrTy();
741741 Type *I32Ty = B.getInt32Ty();
742742 Constant *StrChr =
743 M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty, nullptr);
743 M->getOrInsertFunction("strchr", I8Ptr, I8Ptr, I32Ty);
744744 inferLibFuncAttributes(*M->getFunction("strchr"), *TLI);
745745 CallInst *CI = B.CreateCall(
746746 StrChr, {castToCStr(Ptr, B), ConstantInt::get(I32Ty, C)}, "strchr");
758758 LLVMContext &Context = B.GetInsertBlock()->getContext();
759759 Value *StrNCmp = M->getOrInsertFunction("strncmp", B.getInt32Ty(),
760760 B.getInt8PtrTy(), B.getInt8PtrTy(),
761 DL.getIntPtrType(Context), nullptr);
761 DL.getIntPtrType(Context));
762762 inferLibFuncAttributes(*M->getFunction("strncmp"), *TLI);
763763 CallInst *CI = B.CreateCall(
764764 StrNCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "strncmp");
776776
777777 Module *M = B.GetInsertBlock()->getModule();
778778 Type *I8Ptr = B.getInt8PtrTy();
779 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr, nullptr);
779 Value *StrCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr);
780780 inferLibFuncAttributes(*M->getFunction(Name), *TLI);
781781 CallInst *CI =
782782 B.CreateCall(StrCpy, {castToCStr(Dst, B), castToCStr(Src, B)}, Name);
793793 Module *M = B.GetInsertBlock()->getModule();
794794 Type *I8Ptr = B.getInt8PtrTy();
795795 Value *StrNCpy = M->getOrInsertFunction(Name, I8Ptr, I8Ptr, I8Ptr,
796 Len->getType(), nullptr);
796 Len->getType());
797797 inferLibFuncAttributes(*M->getFunction(Name), *TLI);
798798 CallInst *CI = B.CreateCall(
799799 StrNCpy, {castToCStr(Dst, B), castToCStr(Src, B), Len}, "strncpy");
816816 Value *MemCpy = M->getOrInsertFunction(
817817 "__memcpy_chk", AttributeList::get(M->getContext(), AS), B.getInt8PtrTy(),
818818 B.getInt8PtrTy(), B.getInt8PtrTy(), DL.getIntPtrType(Context),
819 DL.getIntPtrType(Context), nullptr);
819 DL.getIntPtrType(Context));
820820 Dst = castToCStr(Dst, B);
821821 Src = castToCStr(Src, B);
822822 CallInst *CI = B.CreateCall(MemCpy, {Dst, Src, Len, ObjSize});
834834 LLVMContext &Context = B.GetInsertBlock()->getContext();
835835 Value *MemChr = M->getOrInsertFunction("memchr", B.getInt8PtrTy(),
836836 B.getInt8PtrTy(), B.getInt32Ty(),
837 DL.getIntPtrType(Context), nullptr);
837 DL.getIntPtrType(Context));
838838 inferLibFuncAttributes(*M->getFunction("memchr"), *TLI);
839839 CallInst *CI = B.CreateCall(MemChr, {castToCStr(Ptr, B), Val, Len}, "memchr");
840840
853853 LLVMContext &Context = B.GetInsertBlock()->getContext();
854854 Value *MemCmp = M->getOrInsertFunction("memcmp", B.getInt32Ty(),
855855 B.getInt8PtrTy(), B.getInt8PtrTy(),
856 DL.getIntPtrType(Context), nullptr);
856 DL.getIntPtrType(Context));
857857 inferLibFuncAttributes(*M->getFunction("memcmp"), *TLI);
858858 CallInst *CI = B.CreateCall(
859859 MemCmp, {castToCStr(Ptr1, B), castToCStr(Ptr2, B), Len}, "memcmp");
886886
887887 Module *M = B.GetInsertBlock()->getModule();
888888 Value *Callee = M->getOrInsertFunction(Name, Op->getType(),
889 Op->getType(), nullptr);
889 Op->getType());
890890 CallInst *CI = B.CreateCall(Callee, Op, Name);
891891 CI->setAttributes(Attrs);
892892 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
902902
903903 Module *M = B.GetInsertBlock()->getModule();
904904 Value *Callee = M->getOrInsertFunction(Name, Op1->getType(), Op1->getType(),
905 Op2->getType(), nullptr);
905 Op2->getType());
906906 CallInst *CI = B.CreateCall(Callee, {Op1, Op2}, Name);
907907 CI->setAttributes(Attrs);
908908 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
917917 return nullptr;
918918
919919 Module *M = B.GetInsertBlock()->getModule();
920 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(),
921 B.getInt32Ty(), nullptr);
920 Value *PutChar = M->getOrInsertFunction("putchar", B.getInt32Ty(), B.getInt32Ty());
922921 inferLibFuncAttributes(*M->getFunction("putchar"), *TLI);
923922 CallInst *CI = B.CreateCall(PutChar,
924923 B.CreateIntCast(Char,
939938
940939 Module *M = B.GetInsertBlock()->getModule();
941940 Value *PutS =
942 M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy(), nullptr);
941 M->getOrInsertFunction("puts", B.getInt32Ty(), B.getInt8PtrTy());
943942 inferLibFuncAttributes(*M->getFunction("puts"), *TLI);
944943 CallInst *CI = B.CreateCall(PutS, castToCStr(Str, B), "puts");
945944 if (const Function *F = dyn_cast(PutS->stripPointerCasts()))
954953
955954 Module *M = B.GetInsertBlock()->getModule();
956955 Constant *F = M->getOrInsertFunction("fputc", B.getInt32Ty(), B.getInt32Ty(),
957 File->getType(), nullptr);
956 File->getType());
958957 if (File->getType()->isPointerTy())
959958 inferLibFuncAttributes(*M->getFunction("fputc"), *TLI);
960959 Char = B.CreateIntCast(Char, B.getInt32Ty(), /*isSigned*/true,
974973 Module *M = B.GetInsertBlock()->getModule();
975974 StringRef FPutsName = TLI->getName(LibFunc_fputs);
976975 Constant *F = M->getOrInsertFunction(
977 FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType(), nullptr);
976 FPutsName, B.getInt32Ty(), B.getInt8PtrTy(), File->getType());
978977 if (File->getType()->isPointerTy())
979978 inferLibFuncAttributes(*M->getFunction(FPutsName), *TLI);
980979 CallInst *CI = B.CreateCall(F, {castToCStr(Str, B), File}, "fputs");
994993 StringRef FWriteName = TLI->getName(LibFunc_fwrite);
995994 Constant *F = M->getOrInsertFunction(
996995 FWriteName, DL.getIntPtrType(Context), B.getInt8PtrTy(),
997 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType(),
998 nullptr);
996 DL.getIntPtrType(Context), DL.getIntPtrType(Context), File->getType());
997
999998 if (File->getType()->isPointerTy())
1000999 inferLibFuncAttributes(*M->getFunction(FWriteName), *TLI);
10011000 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()))
12181218 Module *M = CI->getModule();
12191219 Value *NewCallee =
12201220 M->getOrInsertFunction(TLI->getName(LdExp), Op->getType(),
1221 Op->getType(), B.getInt32Ty(), nullptr);
1221 Op->getType(), B.getInt32Ty());
12221222 CallInst *CI = B.CreateCall(NewCallee, {One, LdExpArg});
12231223 if (const Function *F = dyn_cast(Callee->stripPointerCasts()))
12241224 CI->setCallingConv(F->getCallingConv());
14421442
14431443 Module *M = OrigCallee->getParent();
14441444 Value *Callee = M->getOrInsertFunction(Name, OrigCallee->getAttributes(),
1445 ResTy, ArgTy, nullptr);
1445 ResTy, ArgTy);
14461446
14471447 if (Instruction *ArgInst = dyn_cast(Arg)) {
14481448 // If the argument is an instruction, it must dominate all uses so put our
840840 // Prototype: void *getPointerToNamedFunction(const char* Name)
841841 Constant *resolverFunc = Safe->getOrInsertFunction(
842842 "getPointerToNamedFunction", Type::getInt8PtrTy(Safe->getContext()),
843 Type::getInt8PtrTy(Safe->getContext()), (Type *)nullptr);
843 Type::getInt8PtrTy(Safe->getContext()));
844844
845845 // Use the function we just added to get addresses of functions we need.
846846 for (Module::iterator F = Safe->begin(), E = Safe->end(); F != E; ++F) {
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) {