DataFlowSanitizer.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628
  1. //===-- DataFlowSanitizer.cpp - dynamic data flow analysis ----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. /// \file
  10. /// This file is a part of DataFlowSanitizer, a generalised dynamic data flow
  11. /// analysis.
  12. ///
  13. /// Unlike other Sanitizer tools, this tool is not designed to detect a specific
  14. /// class of bugs on its own. Instead, it provides a generic dynamic data flow
  15. /// analysis framework to be used by clients to help detect application-specific
  16. /// issues within their own code.
  17. ///
  18. /// The analysis is based on automatic propagation of data flow labels (also
  19. /// known as taint labels) through a program as it performs computation. Each
  20. /// byte of application memory is backed by two bytes of shadow memory which
  21. /// hold the label. On Linux/x86_64, memory is laid out as follows:
  22. ///
  23. /// +--------------------+ 0x800000000000 (top of memory)
  24. /// | application memory |
  25. /// +--------------------+ 0x700000008000 (kAppAddr)
  26. /// | |
  27. /// | unused |
  28. /// | |
  29. /// +--------------------+ 0x200200000000 (kUnusedAddr)
  30. /// | union table |
  31. /// +--------------------+ 0x200000000000 (kUnionTableAddr)
  32. /// | shadow memory |
  33. /// +--------------------+ 0x000000010000 (kShadowAddr)
  34. /// | reserved by kernel |
  35. /// +--------------------+ 0x000000000000
  36. ///
  37. /// To derive a shadow memory address from an application memory address,
  38. /// bits 44-46 are cleared to bring the address into the range
  39. /// [0x000000008000,0x100000000000). Then the address is shifted left by 1 to
  40. /// account for the double byte representation of shadow labels and move the
  41. /// address into the shadow memory range. See the function
  42. /// DataFlowSanitizer::getShadowAddress below.
  43. ///
  44. /// For more information, please refer to the design document:
  45. /// http://clang.llvm.org/docs/DataFlowSanitizerDesign.html
  46. #include "llvm/Transforms/Instrumentation.h"
  47. #include "llvm/ADT/DenseMap.h"
  48. #include "llvm/ADT/DenseSet.h"
  49. #include "llvm/ADT/DepthFirstIterator.h"
  50. #include "llvm/ADT/StringExtras.h"
  51. #include "llvm/ADT/Triple.h"
  52. #include "llvm/Analysis/ValueTracking.h"
  53. #include "llvm/IR/Dominators.h"
  54. #include "llvm/IR/DebugInfo.h"
  55. #include "llvm/IR/IRBuilder.h"
  56. #include "llvm/IR/InlineAsm.h"
  57. #include "llvm/IR/InstVisitor.h"
  58. #include "llvm/IR/LLVMContext.h"
  59. #include "llvm/IR/MDBuilder.h"
  60. #include "llvm/IR/Type.h"
  61. #include "llvm/IR/Value.h"
  62. #include "llvm/Pass.h"
  63. #include "llvm/Support/CommandLine.h"
  64. #include "llvm/Support/SpecialCaseList.h"
  65. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  66. #include "llvm/Transforms/Utils/Local.h"
  67. #include <algorithm>
  68. #include <iterator>
  69. #include <set>
  70. #include <utility>
  71. using namespace llvm;
  72. // External symbol to be used when generating the shadow address for
  73. // architectures with multiple VMAs. Instead of using a constant integer
  74. // the runtime will set the external mask based on the VMA range.
  75. static const char *const kDFSanExternShadowPtrMask = "__dfsan_shadow_ptr_mask";
  76. // The -dfsan-preserve-alignment flag controls whether this pass assumes that
  77. // alignment requirements provided by the input IR are correct. For example,
  78. // if the input IR contains a load with alignment 8, this flag will cause
  79. // the shadow load to have alignment 16. This flag is disabled by default as
  80. // we have unfortunately encountered too much code (including Clang itself;
  81. // see PR14291) which performs misaligned access.
  82. static cl::opt<bool> ClPreserveAlignment(
  83. "dfsan-preserve-alignment",
  84. cl::desc("respect alignment requirements provided by input IR"), cl::Hidden,
  85. cl::init(false));
  86. // The ABI list files control how shadow parameters are passed. The pass treats
  87. // every function labelled "uninstrumented" in the ABI list file as conforming
  88. // to the "native" (i.e. unsanitized) ABI. Unless the ABI list contains
  89. // additional annotations for those functions, a call to one of those functions
  90. // will produce a warning message, as the labelling behaviour of the function is
  91. // unknown. The other supported annotations are "functional" and "discard",
  92. // which are described below under DataFlowSanitizer::WrapperKind.
  93. static cl::list<std::string> ClABIListFiles(
  94. "dfsan-abilist",
  95. cl::desc("File listing native ABI functions and how the pass treats them"),
  96. cl::Hidden);
  97. // Controls whether the pass uses IA_Args or IA_TLS as the ABI for instrumented
  98. // functions (see DataFlowSanitizer::InstrumentedABI below).
  99. static cl::opt<bool> ClArgsABI(
  100. "dfsan-args-abi",
  101. cl::desc("Use the argument ABI rather than the TLS ABI"),
  102. cl::Hidden);
  103. // Controls whether the pass includes or ignores the labels of pointers in load
  104. // instructions.
  105. static cl::opt<bool> ClCombinePointerLabelsOnLoad(
  106. "dfsan-combine-pointer-labels-on-load",
  107. cl::desc("Combine the label of the pointer with the label of the data when "
  108. "loading from memory."),
  109. cl::Hidden, cl::init(true));
  110. // Controls whether the pass includes or ignores the labels of pointers in
  111. // stores instructions.
  112. static cl::opt<bool> ClCombinePointerLabelsOnStore(
  113. "dfsan-combine-pointer-labels-on-store",
  114. cl::desc("Combine the label of the pointer with the label of the data when "
  115. "storing in memory."),
  116. cl::Hidden, cl::init(false));
  117. static cl::opt<bool> ClDebugNonzeroLabels(
  118. "dfsan-debug-nonzero-labels",
  119. cl::desc("Insert calls to __dfsan_nonzero_label on observing a parameter, "
  120. "load or return with a nonzero label"),
  121. cl::Hidden);
  122. namespace {
  123. StringRef GetGlobalTypeString(const GlobalValue &G) {
  124. // Types of GlobalVariables are always pointer types.
  125. Type *GType = G.getValueType();
  126. // For now we support blacklisting struct types only.
  127. if (StructType *SGType = dyn_cast<StructType>(GType)) {
  128. if (!SGType->isLiteral())
  129. return SGType->getName();
  130. }
  131. return "<unknown type>";
  132. }
  133. class DFSanABIList {
  134. std::unique_ptr<SpecialCaseList> SCL;
  135. public:
  136. DFSanABIList() {}
  137. void set(std::unique_ptr<SpecialCaseList> List) { SCL = std::move(List); }
  138. /// Returns whether either this function or its source file are listed in the
  139. /// given category.
  140. bool isIn(const Function &F, StringRef Category) const {
  141. return isIn(*F.getParent(), Category) ||
  142. SCL->inSection("fun", F.getName(), Category);
  143. }
  144. /// Returns whether this global alias is listed in the given category.
  145. ///
  146. /// If GA aliases a function, the alias's name is matched as a function name
  147. /// would be. Similarly, aliases of globals are matched like globals.
  148. bool isIn(const GlobalAlias &GA, StringRef Category) const {
  149. if (isIn(*GA.getParent(), Category))
  150. return true;
  151. if (isa<FunctionType>(GA.getValueType()))
  152. return SCL->inSection("fun", GA.getName(), Category);
  153. return SCL->inSection("global", GA.getName(), Category) ||
  154. SCL->inSection("type", GetGlobalTypeString(GA), Category);
  155. }
  156. /// Returns whether this module is listed in the given category.
  157. bool isIn(const Module &M, StringRef Category) const {
  158. return SCL->inSection("src", M.getModuleIdentifier(), Category);
  159. }
  160. };
  161. class DataFlowSanitizer : public ModulePass {
  162. friend struct DFSanFunction;
  163. friend class DFSanVisitor;
  164. enum {
  165. ShadowWidth = 16
  166. };
  167. /// Which ABI should be used for instrumented functions?
  168. enum InstrumentedABI {
  169. /// Argument and return value labels are passed through additional
  170. /// arguments and by modifying the return type.
  171. IA_Args,
  172. /// Argument and return value labels are passed through TLS variables
  173. /// __dfsan_arg_tls and __dfsan_retval_tls.
  174. IA_TLS
  175. };
  176. /// How should calls to uninstrumented functions be handled?
  177. enum WrapperKind {
  178. /// This function is present in an uninstrumented form but we don't know
  179. /// how it should be handled. Print a warning and call the function anyway.
  180. /// Don't label the return value.
  181. WK_Warning,
  182. /// This function does not write to (user-accessible) memory, and its return
  183. /// value is unlabelled.
  184. WK_Discard,
  185. /// This function does not write to (user-accessible) memory, and the label
  186. /// of its return value is the union of the label of its arguments.
  187. WK_Functional,
  188. /// Instead of calling the function, a custom wrapper __dfsw_F is called,
  189. /// where F is the name of the function. This function may wrap the
  190. /// original function or provide its own implementation. This is similar to
  191. /// the IA_Args ABI, except that IA_Args uses a struct return type to
  192. /// pass the return value shadow in a register, while WK_Custom uses an
  193. /// extra pointer argument to return the shadow. This allows the wrapped
  194. /// form of the function type to be expressed in C.
  195. WK_Custom
  196. };
  197. Module *Mod;
  198. LLVMContext *Ctx;
  199. IntegerType *ShadowTy;
  200. PointerType *ShadowPtrTy;
  201. IntegerType *IntptrTy;
  202. ConstantInt *ZeroShadow;
  203. ConstantInt *ShadowPtrMask;
  204. ConstantInt *ShadowPtrMul;
  205. Constant *ArgTLS;
  206. Constant *RetvalTLS;
  207. void *(*GetArgTLSPtr)();
  208. void *(*GetRetvalTLSPtr)();
  209. Constant *GetArgTLS;
  210. Constant *GetRetvalTLS;
  211. Constant *ExternalShadowMask;
  212. FunctionType *DFSanUnionFnTy;
  213. FunctionType *DFSanUnionLoadFnTy;
  214. FunctionType *DFSanUnimplementedFnTy;
  215. FunctionType *DFSanSetLabelFnTy;
  216. FunctionType *DFSanNonzeroLabelFnTy;
  217. FunctionType *DFSanVarargWrapperFnTy;
  218. Constant *DFSanUnionFn;
  219. Constant *DFSanCheckedUnionFn;
  220. Constant *DFSanUnionLoadFn;
  221. Constant *DFSanUnimplementedFn;
  222. Constant *DFSanSetLabelFn;
  223. Constant *DFSanNonzeroLabelFn;
  224. Constant *DFSanVarargWrapperFn;
  225. MDNode *ColdCallWeights;
  226. DFSanABIList ABIList;
  227. DenseMap<Value *, Function *> UnwrappedFnMap;
  228. AttrBuilder ReadOnlyNoneAttrs;
  229. bool DFSanRuntimeShadowMask;
  230. Value *getShadowAddress(Value *Addr, Instruction *Pos);
  231. bool isInstrumented(const Function *F);
  232. bool isInstrumented(const GlobalAlias *GA);
  233. FunctionType *getArgsFunctionType(FunctionType *T);
  234. FunctionType *getTrampolineFunctionType(FunctionType *T);
  235. FunctionType *getCustomFunctionType(FunctionType *T);
  236. InstrumentedABI getInstrumentedABI();
  237. WrapperKind getWrapperKind(Function *F);
  238. void addGlobalNamePrefix(GlobalValue *GV);
  239. Function *buildWrapperFunction(Function *F, StringRef NewFName,
  240. GlobalValue::LinkageTypes NewFLink,
  241. FunctionType *NewFT);
  242. Constant *getOrBuildTrampolineFunction(FunctionType *FT, StringRef FName);
  243. public:
  244. DataFlowSanitizer(
  245. const std::vector<std::string> &ABIListFiles = std::vector<std::string>(),
  246. void *(*getArgTLS)() = nullptr, void *(*getRetValTLS)() = nullptr);
  247. static char ID;
  248. bool doInitialization(Module &M) override;
  249. bool runOnModule(Module &M) override;
  250. };
  251. struct DFSanFunction {
  252. DataFlowSanitizer &DFS;
  253. Function *F;
  254. DominatorTree DT;
  255. DataFlowSanitizer::InstrumentedABI IA;
  256. bool IsNativeABI;
  257. Value *ArgTLSPtr;
  258. Value *RetvalTLSPtr;
  259. AllocaInst *LabelReturnAlloca;
  260. DenseMap<Value *, Value *> ValShadowMap;
  261. DenseMap<AllocaInst *, AllocaInst *> AllocaShadowMap;
  262. std::vector<std::pair<PHINode *, PHINode *> > PHIFixups;
  263. DenseSet<Instruction *> SkipInsts;
  264. std::vector<Value *> NonZeroChecks;
  265. bool AvoidNewBlocks;
  266. struct CachedCombinedShadow {
  267. BasicBlock *Block;
  268. Value *Shadow;
  269. };
  270. DenseMap<std::pair<Value *, Value *>, CachedCombinedShadow>
  271. CachedCombinedShadows;
  272. DenseMap<Value *, std::set<Value *>> ShadowElements;
  273. DFSanFunction(DataFlowSanitizer &DFS, Function *F, bool IsNativeABI)
  274. : DFS(DFS), F(F), IA(DFS.getInstrumentedABI()),
  275. IsNativeABI(IsNativeABI), ArgTLSPtr(nullptr), RetvalTLSPtr(nullptr),
  276. LabelReturnAlloca(nullptr) {
  277. DT.recalculate(*F);
  278. // FIXME: Need to track down the register allocator issue which causes poor
  279. // performance in pathological cases with large numbers of basic blocks.
  280. AvoidNewBlocks = F->size() > 1000;
  281. }
  282. Value *getArgTLSPtr();
  283. Value *getArgTLS(unsigned Index, Instruction *Pos);
  284. Value *getRetvalTLS();
  285. Value *getShadow(Value *V);
  286. void setShadow(Instruction *I, Value *Shadow);
  287. Value *combineShadows(Value *V1, Value *V2, Instruction *Pos);
  288. Value *combineOperandShadows(Instruction *Inst);
  289. Value *loadShadow(Value *ShadowAddr, uint64_t Size, uint64_t Align,
  290. Instruction *Pos);
  291. void storeShadow(Value *Addr, uint64_t Size, uint64_t Align, Value *Shadow,
  292. Instruction *Pos);
  293. };
  294. class DFSanVisitor : public InstVisitor<DFSanVisitor> {
  295. public:
  296. DFSanFunction &DFSF;
  297. DFSanVisitor(DFSanFunction &DFSF) : DFSF(DFSF) {}
  298. const DataLayout &getDataLayout() const {
  299. return DFSF.F->getParent()->getDataLayout();
  300. }
  301. void visitOperandShadowInst(Instruction &I);
  302. void visitBinaryOperator(BinaryOperator &BO);
  303. void visitCastInst(CastInst &CI);
  304. void visitCmpInst(CmpInst &CI);
  305. void visitGetElementPtrInst(GetElementPtrInst &GEPI);
  306. void visitLoadInst(LoadInst &LI);
  307. void visitStoreInst(StoreInst &SI);
  308. void visitReturnInst(ReturnInst &RI);
  309. void visitCallSite(CallSite CS);
  310. void visitPHINode(PHINode &PN);
  311. void visitExtractElementInst(ExtractElementInst &I);
  312. void visitInsertElementInst(InsertElementInst &I);
  313. void visitShuffleVectorInst(ShuffleVectorInst &I);
  314. void visitExtractValueInst(ExtractValueInst &I);
  315. void visitInsertValueInst(InsertValueInst &I);
  316. void visitAllocaInst(AllocaInst &I);
  317. void visitSelectInst(SelectInst &I);
  318. void visitMemSetInst(MemSetInst &I);
  319. void visitMemTransferInst(MemTransferInst &I);
  320. };
  321. }
  322. char DataFlowSanitizer::ID;
  323. INITIALIZE_PASS(DataFlowSanitizer, "dfsan",
  324. "DataFlowSanitizer: dynamic data flow analysis.", false, false)
  325. ModulePass *
  326. llvm::createDataFlowSanitizerPass(const std::vector<std::string> &ABIListFiles,
  327. void *(*getArgTLS)(),
  328. void *(*getRetValTLS)()) {
  329. return new DataFlowSanitizer(ABIListFiles, getArgTLS, getRetValTLS);
  330. }
  331. DataFlowSanitizer::DataFlowSanitizer(
  332. const std::vector<std::string> &ABIListFiles, void *(*getArgTLS)(),
  333. void *(*getRetValTLS)())
  334. : ModulePass(ID), GetArgTLSPtr(getArgTLS), GetRetvalTLSPtr(getRetValTLS),
  335. DFSanRuntimeShadowMask(false) {
  336. std::vector<std::string> AllABIListFiles(std::move(ABIListFiles));
  337. AllABIListFiles.insert(AllABIListFiles.end(), ClABIListFiles.begin(),
  338. ClABIListFiles.end());
  339. ABIList.set(SpecialCaseList::createOrDie(AllABIListFiles));
  340. }
  341. FunctionType *DataFlowSanitizer::getArgsFunctionType(FunctionType *T) {
  342. llvm::SmallVector<Type *, 4> ArgTypes(T->param_begin(), T->param_end());
  343. ArgTypes.append(T->getNumParams(), ShadowTy);
  344. if (T->isVarArg())
  345. ArgTypes.push_back(ShadowPtrTy);
  346. Type *RetType = T->getReturnType();
  347. if (!RetType->isVoidTy())
  348. RetType = StructType::get(RetType, ShadowTy);
  349. return FunctionType::get(RetType, ArgTypes, T->isVarArg());
  350. }
  351. FunctionType *DataFlowSanitizer::getTrampolineFunctionType(FunctionType *T) {
  352. assert(!T->isVarArg());
  353. llvm::SmallVector<Type *, 4> ArgTypes;
  354. ArgTypes.push_back(T->getPointerTo());
  355. ArgTypes.append(T->param_begin(), T->param_end());
  356. ArgTypes.append(T->getNumParams(), ShadowTy);
  357. Type *RetType = T->getReturnType();
  358. if (!RetType->isVoidTy())
  359. ArgTypes.push_back(ShadowPtrTy);
  360. return FunctionType::get(T->getReturnType(), ArgTypes, false);
  361. }
  362. FunctionType *DataFlowSanitizer::getCustomFunctionType(FunctionType *T) {
  363. llvm::SmallVector<Type *, 4> ArgTypes;
  364. for (FunctionType::param_iterator i = T->param_begin(), e = T->param_end();
  365. i != e; ++i) {
  366. FunctionType *FT;
  367. if (isa<PointerType>(*i) && (FT = dyn_cast<FunctionType>(cast<PointerType>(
  368. *i)->getElementType()))) {
  369. ArgTypes.push_back(getTrampolineFunctionType(FT)->getPointerTo());
  370. ArgTypes.push_back(Type::getInt8PtrTy(*Ctx));
  371. } else {
  372. ArgTypes.push_back(*i);
  373. }
  374. }
  375. for (unsigned i = 0, e = T->getNumParams(); i != e; ++i)
  376. ArgTypes.push_back(ShadowTy);
  377. if (T->isVarArg())
  378. ArgTypes.push_back(ShadowPtrTy);
  379. Type *RetType = T->getReturnType();
  380. if (!RetType->isVoidTy())
  381. ArgTypes.push_back(ShadowPtrTy);
  382. return FunctionType::get(T->getReturnType(), ArgTypes, T->isVarArg());
  383. }
  384. bool DataFlowSanitizer::doInitialization(Module &M) {
  385. llvm::Triple TargetTriple(M.getTargetTriple());
  386. bool IsX86_64 = TargetTriple.getArch() == llvm::Triple::x86_64;
  387. bool IsMIPS64 = TargetTriple.getArch() == llvm::Triple::mips64 ||
  388. TargetTriple.getArch() == llvm::Triple::mips64el;
  389. bool IsAArch64 = TargetTriple.getArch() == llvm::Triple::aarch64 ||
  390. TargetTriple.getArch() == llvm::Triple::aarch64_be;
  391. const DataLayout &DL = M.getDataLayout();
  392. Mod = &M;
  393. Ctx = &M.getContext();
  394. ShadowTy = IntegerType::get(*Ctx, ShadowWidth);
  395. ShadowPtrTy = PointerType::getUnqual(ShadowTy);
  396. IntptrTy = DL.getIntPtrType(*Ctx);
  397. ZeroShadow = ConstantInt::getSigned(ShadowTy, 0);
  398. ShadowPtrMul = ConstantInt::getSigned(IntptrTy, ShadowWidth / 8);
  399. if (IsX86_64)
  400. ShadowPtrMask = ConstantInt::getSigned(IntptrTy, ~0x700000000000LL);
  401. else if (IsMIPS64)
  402. ShadowPtrMask = ConstantInt::getSigned(IntptrTy, ~0xF000000000LL);
  403. // AArch64 supports multiple VMAs and the shadow mask is set at runtime.
  404. else if (IsAArch64)
  405. DFSanRuntimeShadowMask = true;
  406. else
  407. report_fatal_error("unsupported triple");
  408. Type *DFSanUnionArgs[2] = { ShadowTy, ShadowTy };
  409. DFSanUnionFnTy =
  410. FunctionType::get(ShadowTy, DFSanUnionArgs, /*isVarArg=*/ false);
  411. Type *DFSanUnionLoadArgs[2] = { ShadowPtrTy, IntptrTy };
  412. DFSanUnionLoadFnTy =
  413. FunctionType::get(ShadowTy, DFSanUnionLoadArgs, /*isVarArg=*/ false);
  414. DFSanUnimplementedFnTy = FunctionType::get(
  415. Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false);
  416. Type *DFSanSetLabelArgs[3] = { ShadowTy, Type::getInt8PtrTy(*Ctx), IntptrTy };
  417. DFSanSetLabelFnTy = FunctionType::get(Type::getVoidTy(*Ctx),
  418. DFSanSetLabelArgs, /*isVarArg=*/false);
  419. DFSanNonzeroLabelFnTy = FunctionType::get(
  420. Type::getVoidTy(*Ctx), None, /*isVarArg=*/false);
  421. DFSanVarargWrapperFnTy = FunctionType::get(
  422. Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false);
  423. if (GetArgTLSPtr) {
  424. Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
  425. ArgTLS = nullptr;
  426. GetArgTLS = ConstantExpr::getIntToPtr(
  427. ConstantInt::get(IntptrTy, uintptr_t(GetArgTLSPtr)),
  428. PointerType::getUnqual(
  429. FunctionType::get(PointerType::getUnqual(ArgTLSTy), (Type*)nullptr)));
  430. }
  431. if (GetRetvalTLSPtr) {
  432. RetvalTLS = nullptr;
  433. GetRetvalTLS = ConstantExpr::getIntToPtr(
  434. ConstantInt::get(IntptrTy, uintptr_t(GetRetvalTLSPtr)),
  435. PointerType::getUnqual(
  436. FunctionType::get(PointerType::getUnqual(ShadowTy), (Type*)nullptr)));
  437. }
  438. ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
  439. return true;
  440. }
  441. bool DataFlowSanitizer::isInstrumented(const Function *F) {
  442. return !ABIList.isIn(*F, "uninstrumented");
  443. }
  444. bool DataFlowSanitizer::isInstrumented(const GlobalAlias *GA) {
  445. return !ABIList.isIn(*GA, "uninstrumented");
  446. }
  447. DataFlowSanitizer::InstrumentedABI DataFlowSanitizer::getInstrumentedABI() {
  448. return ClArgsABI ? IA_Args : IA_TLS;
  449. }
  450. DataFlowSanitizer::WrapperKind DataFlowSanitizer::getWrapperKind(Function *F) {
  451. if (ABIList.isIn(*F, "functional"))
  452. return WK_Functional;
  453. if (ABIList.isIn(*F, "discard"))
  454. return WK_Discard;
  455. if (ABIList.isIn(*F, "custom"))
  456. return WK_Custom;
  457. return WK_Warning;
  458. }
  459. void DataFlowSanitizer::addGlobalNamePrefix(GlobalValue *GV) {
  460. std::string GVName = GV->getName(), Prefix = "dfs$";
  461. GV->setName(Prefix + GVName);
  462. // Try to change the name of the function in module inline asm. We only do
  463. // this for specific asm directives, currently only ".symver", to try to avoid
  464. // corrupting asm which happens to contain the symbol name as a substring.
  465. // Note that the substitution for .symver assumes that the versioned symbol
  466. // also has an instrumented name.
  467. std::string Asm = GV->getParent()->getModuleInlineAsm();
  468. std::string SearchStr = ".symver " + GVName + ",";
  469. size_t Pos = Asm.find(SearchStr);
  470. if (Pos != std::string::npos) {
  471. Asm.replace(Pos, SearchStr.size(),
  472. ".symver " + Prefix + GVName + "," + Prefix);
  473. GV->getParent()->setModuleInlineAsm(Asm);
  474. }
  475. }
  476. Function *
  477. DataFlowSanitizer::buildWrapperFunction(Function *F, StringRef NewFName,
  478. GlobalValue::LinkageTypes NewFLink,
  479. FunctionType *NewFT) {
  480. FunctionType *FT = F->getFunctionType();
  481. Function *NewF = Function::Create(NewFT, NewFLink, NewFName,
  482. F->getParent());
  483. NewF->copyAttributesFrom(F);
  484. NewF->removeAttributes(
  485. AttributeList::ReturnIndex,
  486. AttributeFuncs::typeIncompatible(NewFT->getReturnType()));
  487. BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF);
  488. if (F->isVarArg()) {
  489. NewF->removeAttributes(AttributeList::FunctionIndex,
  490. AttrBuilder().addAttribute("split-stack"));
  491. CallInst::Create(DFSanVarargWrapperFn,
  492. IRBuilder<>(BB).CreateGlobalStringPtr(F->getName()), "",
  493. BB);
  494. new UnreachableInst(*Ctx, BB);
  495. } else {
  496. std::vector<Value *> Args;
  497. unsigned n = FT->getNumParams();
  498. for (Function::arg_iterator ai = NewF->arg_begin(); n != 0; ++ai, --n)
  499. Args.push_back(&*ai);
  500. CallInst *CI = CallInst::Create(F, Args, "", BB);
  501. if (FT->getReturnType()->isVoidTy())
  502. ReturnInst::Create(*Ctx, BB);
  503. else
  504. ReturnInst::Create(*Ctx, CI, BB);
  505. }
  506. return NewF;
  507. }
  508. Constant *DataFlowSanitizer::getOrBuildTrampolineFunction(FunctionType *FT,
  509. StringRef FName) {
  510. FunctionType *FTT = getTrampolineFunctionType(FT);
  511. Constant *C = Mod->getOrInsertFunction(FName, FTT);
  512. Function *F = dyn_cast<Function>(C);
  513. if (F && F->isDeclaration()) {
  514. F->setLinkage(GlobalValue::LinkOnceODRLinkage);
  515. BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", F);
  516. std::vector<Value *> Args;
  517. Function::arg_iterator AI = F->arg_begin(); ++AI;
  518. for (unsigned N = FT->getNumParams(); N != 0; ++AI, --N)
  519. Args.push_back(&*AI);
  520. CallInst *CI = CallInst::Create(&*F->arg_begin(), Args, "", BB);
  521. ReturnInst *RI;
  522. if (FT->getReturnType()->isVoidTy())
  523. RI = ReturnInst::Create(*Ctx, BB);
  524. else
  525. RI = ReturnInst::Create(*Ctx, CI, BB);
  526. DFSanFunction DFSF(*this, F, /*IsNativeABI=*/true);
  527. Function::arg_iterator ValAI = F->arg_begin(), ShadowAI = AI; ++ValAI;
  528. for (unsigned N = FT->getNumParams(); N != 0; ++ValAI, ++ShadowAI, --N)
  529. DFSF.ValShadowMap[&*ValAI] = &*ShadowAI;
  530. DFSanVisitor(DFSF).visitCallInst(*CI);
  531. if (!FT->getReturnType()->isVoidTy())
  532. new StoreInst(DFSF.getShadow(RI->getReturnValue()),
  533. &*std::prev(F->arg_end()), RI);
  534. }
  535. return C;
  536. }
  537. bool DataFlowSanitizer::runOnModule(Module &M) {
  538. if (ABIList.isIn(M, "skip"))
  539. return false;
  540. if (!GetArgTLSPtr) {
  541. Type *ArgTLSTy = ArrayType::get(ShadowTy, 64);
  542. ArgTLS = Mod->getOrInsertGlobal("__dfsan_arg_tls", ArgTLSTy);
  543. if (GlobalVariable *G = dyn_cast<GlobalVariable>(ArgTLS))
  544. G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
  545. }
  546. if (!GetRetvalTLSPtr) {
  547. RetvalTLS = Mod->getOrInsertGlobal("__dfsan_retval_tls", ShadowTy);
  548. if (GlobalVariable *G = dyn_cast<GlobalVariable>(RetvalTLS))
  549. G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
  550. }
  551. ExternalShadowMask =
  552. Mod->getOrInsertGlobal(kDFSanExternShadowPtrMask, IntptrTy);
  553. DFSanUnionFn = Mod->getOrInsertFunction("__dfsan_union", DFSanUnionFnTy);
  554. if (Function *F = dyn_cast<Function>(DFSanUnionFn)) {
  555. F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
  556. F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
  557. F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
  558. F->addParamAttr(0, Attribute::ZExt);
  559. F->addParamAttr(1, Attribute::ZExt);
  560. }
  561. DFSanCheckedUnionFn = Mod->getOrInsertFunction("dfsan_union", DFSanUnionFnTy);
  562. if (Function *F = dyn_cast<Function>(DFSanCheckedUnionFn)) {
  563. F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
  564. F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadNone);
  565. F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
  566. F->addParamAttr(0, Attribute::ZExt);
  567. F->addParamAttr(1, Attribute::ZExt);
  568. }
  569. DFSanUnionLoadFn =
  570. Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy);
  571. if (Function *F = dyn_cast<Function>(DFSanUnionLoadFn)) {
  572. F->addAttribute(AttributeList::FunctionIndex, Attribute::NoUnwind);
  573. F->addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly);
  574. F->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
  575. }
  576. DFSanUnimplementedFn =
  577. Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
  578. DFSanSetLabelFn =
  579. Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy);
  580. if (Function *F = dyn_cast<Function>(DFSanSetLabelFn)) {
  581. F->addParamAttr(0, Attribute::ZExt);
  582. }
  583. DFSanNonzeroLabelFn =
  584. Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy);
  585. DFSanVarargWrapperFn = Mod->getOrInsertFunction("__dfsan_vararg_wrapper",
  586. DFSanVarargWrapperFnTy);
  587. std::vector<Function *> FnsToInstrument;
  588. llvm::SmallPtrSet<Function *, 2> FnsWithNativeABI;
  589. for (Function &i : M) {
  590. if (!i.isIntrinsic() &&
  591. &i != DFSanUnionFn &&
  592. &i != DFSanCheckedUnionFn &&
  593. &i != DFSanUnionLoadFn &&
  594. &i != DFSanUnimplementedFn &&
  595. &i != DFSanSetLabelFn &&
  596. &i != DFSanNonzeroLabelFn &&
  597. &i != DFSanVarargWrapperFn)
  598. FnsToInstrument.push_back(&i);
  599. }
  600. // Give function aliases prefixes when necessary, and build wrappers where the
  601. // instrumentedness is inconsistent.
  602. for (Module::alias_iterator i = M.alias_begin(), e = M.alias_end(); i != e;) {
  603. GlobalAlias *GA = &*i;
  604. ++i;
  605. // Don't stop on weak. We assume people aren't playing games with the
  606. // instrumentedness of overridden weak aliases.
  607. if (auto F = dyn_cast<Function>(GA->getBaseObject())) {
  608. bool GAInst = isInstrumented(GA), FInst = isInstrumented(F);
  609. if (GAInst && FInst) {
  610. addGlobalNamePrefix(GA);
  611. } else if (GAInst != FInst) {
  612. // Non-instrumented alias of an instrumented function, or vice versa.
  613. // Replace the alias with a native-ABI wrapper of the aliasee. The pass
  614. // below will take care of instrumenting it.
  615. Function *NewF =
  616. buildWrapperFunction(F, "", GA->getLinkage(), F->getFunctionType());
  617. GA->replaceAllUsesWith(ConstantExpr::getBitCast(NewF, GA->getType()));
  618. NewF->takeName(GA);
  619. GA->eraseFromParent();
  620. FnsToInstrument.push_back(NewF);
  621. }
  622. }
  623. }
  624. ReadOnlyNoneAttrs.addAttribute(Attribute::ReadOnly)
  625. .addAttribute(Attribute::ReadNone);
  626. // First, change the ABI of every function in the module. ABI-listed
  627. // functions keep their original ABI and get a wrapper function.
  628. for (std::vector<Function *>::iterator i = FnsToInstrument.begin(),
  629. e = FnsToInstrument.end();
  630. i != e; ++i) {
  631. Function &F = **i;
  632. FunctionType *FT = F.getFunctionType();
  633. bool IsZeroArgsVoidRet = (FT->getNumParams() == 0 && !FT->isVarArg() &&
  634. FT->getReturnType()->isVoidTy());
  635. if (isInstrumented(&F)) {
  636. // Instrumented functions get a 'dfs$' prefix. This allows us to more
  637. // easily identify cases of mismatching ABIs.
  638. if (getInstrumentedABI() == IA_Args && !IsZeroArgsVoidRet) {
  639. FunctionType *NewFT = getArgsFunctionType(FT);
  640. Function *NewF = Function::Create(NewFT, F.getLinkage(), "", &M);
  641. NewF->copyAttributesFrom(&F);
  642. NewF->removeAttributes(
  643. AttributeList::ReturnIndex,
  644. AttributeFuncs::typeIncompatible(NewFT->getReturnType()));
  645. for (Function::arg_iterator FArg = F.arg_begin(),
  646. NewFArg = NewF->arg_begin(),
  647. FArgEnd = F.arg_end();
  648. FArg != FArgEnd; ++FArg, ++NewFArg) {
  649. FArg->replaceAllUsesWith(&*NewFArg);
  650. }
  651. NewF->getBasicBlockList().splice(NewF->begin(), F.getBasicBlockList());
  652. for (Function::user_iterator UI = F.user_begin(), UE = F.user_end();
  653. UI != UE;) {
  654. BlockAddress *BA = dyn_cast<BlockAddress>(*UI);
  655. ++UI;
  656. if (BA) {
  657. BA->replaceAllUsesWith(
  658. BlockAddress::get(NewF, BA->getBasicBlock()));
  659. delete BA;
  660. }
  661. }
  662. F.replaceAllUsesWith(
  663. ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT)));
  664. NewF->takeName(&F);
  665. F.eraseFromParent();
  666. *i = NewF;
  667. addGlobalNamePrefix(NewF);
  668. } else {
  669. addGlobalNamePrefix(&F);
  670. }
  671. } else if (!IsZeroArgsVoidRet || getWrapperKind(&F) == WK_Custom) {
  672. // Build a wrapper function for F. The wrapper simply calls F, and is
  673. // added to FnsToInstrument so that any instrumentation according to its
  674. // WrapperKind is done in the second pass below.
  675. FunctionType *NewFT = getInstrumentedABI() == IA_Args
  676. ? getArgsFunctionType(FT)
  677. : FT;
  678. Function *NewF = buildWrapperFunction(
  679. &F, std::string("dfsw$") + std::string(F.getName()),
  680. GlobalValue::LinkOnceODRLinkage, NewFT);
  681. if (getInstrumentedABI() == IA_TLS)
  682. NewF->removeAttributes(AttributeList::FunctionIndex, ReadOnlyNoneAttrs);
  683. Value *WrappedFnCst =
  684. ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT));
  685. F.replaceAllUsesWith(WrappedFnCst);
  686. UnwrappedFnMap[WrappedFnCst] = &F;
  687. *i = NewF;
  688. if (!F.isDeclaration()) {
  689. // This function is probably defining an interposition of an
  690. // uninstrumented function and hence needs to keep the original ABI.
  691. // But any functions it may call need to use the instrumented ABI, so
  692. // we instrument it in a mode which preserves the original ABI.
  693. FnsWithNativeABI.insert(&F);
  694. // This code needs to rebuild the iterators, as they may be invalidated
  695. // by the push_back, taking care that the new range does not include
  696. // any functions added by this code.
  697. size_t N = i - FnsToInstrument.begin(),
  698. Count = e - FnsToInstrument.begin();
  699. FnsToInstrument.push_back(&F);
  700. i = FnsToInstrument.begin() + N;
  701. e = FnsToInstrument.begin() + Count;
  702. }
  703. // Hopefully, nobody will try to indirectly call a vararg
  704. // function... yet.
  705. } else if (FT->isVarArg()) {
  706. UnwrappedFnMap[&F] = &F;
  707. *i = nullptr;
  708. }
  709. }
  710. for (Function *i : FnsToInstrument) {
  711. if (!i || i->isDeclaration())
  712. continue;
  713. removeUnreachableBlocks(*i);
  714. DFSanFunction DFSF(*this, i, FnsWithNativeABI.count(i));
  715. // DFSanVisitor may create new basic blocks, which confuses df_iterator.
  716. // Build a copy of the list before iterating over it.
  717. llvm::SmallVector<BasicBlock *, 4> BBList(depth_first(&i->getEntryBlock()));
  718. for (BasicBlock *i : BBList) {
  719. Instruction *Inst = &i->front();
  720. while (1) {
  721. // DFSanVisitor may split the current basic block, changing the current
  722. // instruction's next pointer and moving the next instruction to the
  723. // tail block from which we should continue.
  724. Instruction *Next = Inst->getNextNode();
  725. // DFSanVisitor may delete Inst, so keep track of whether it was a
  726. // terminator.
  727. bool IsTerminator = isa<TerminatorInst>(Inst);
  728. if (!DFSF.SkipInsts.count(Inst))
  729. DFSanVisitor(DFSF).visit(Inst);
  730. if (IsTerminator)
  731. break;
  732. Inst = Next;
  733. }
  734. }
  735. // We will not necessarily be able to compute the shadow for every phi node
  736. // until we have visited every block. Therefore, the code that handles phi
  737. // nodes adds them to the PHIFixups list so that they can be properly
  738. // handled here.
  739. for (std::vector<std::pair<PHINode *, PHINode *> >::iterator
  740. i = DFSF.PHIFixups.begin(),
  741. e = DFSF.PHIFixups.end();
  742. i != e; ++i) {
  743. for (unsigned val = 0, n = i->first->getNumIncomingValues(); val != n;
  744. ++val) {
  745. i->second->setIncomingValue(
  746. val, DFSF.getShadow(i->first->getIncomingValue(val)));
  747. }
  748. }
  749. // -dfsan-debug-nonzero-labels will split the CFG in all kinds of crazy
  750. // places (i.e. instructions in basic blocks we haven't even begun visiting
  751. // yet). To make our life easier, do this work in a pass after the main
  752. // instrumentation.
  753. if (ClDebugNonzeroLabels) {
  754. for (Value *V : DFSF.NonZeroChecks) {
  755. Instruction *Pos;
  756. if (Instruction *I = dyn_cast<Instruction>(V))
  757. Pos = I->getNextNode();
  758. else
  759. Pos = &DFSF.F->getEntryBlock().front();
  760. while (isa<PHINode>(Pos) || isa<AllocaInst>(Pos))
  761. Pos = Pos->getNextNode();
  762. IRBuilder<> IRB(Pos);
  763. Value *Ne = IRB.CreateICmpNE(V, DFSF.DFS.ZeroShadow);
  764. BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
  765. Ne, Pos, /*Unreachable=*/false, ColdCallWeights));
  766. IRBuilder<> ThenIRB(BI);
  767. ThenIRB.CreateCall(DFSF.DFS.DFSanNonzeroLabelFn, {});
  768. }
  769. }
  770. }
  771. return false;
  772. }
  773. Value *DFSanFunction::getArgTLSPtr() {
  774. if (ArgTLSPtr)
  775. return ArgTLSPtr;
  776. if (DFS.ArgTLS)
  777. return ArgTLSPtr = DFS.ArgTLS;
  778. IRBuilder<> IRB(&F->getEntryBlock().front());
  779. return ArgTLSPtr = IRB.CreateCall(DFS.GetArgTLS, {});
  780. }
  781. Value *DFSanFunction::getRetvalTLS() {
  782. if (RetvalTLSPtr)
  783. return RetvalTLSPtr;
  784. if (DFS.RetvalTLS)
  785. return RetvalTLSPtr = DFS.RetvalTLS;
  786. IRBuilder<> IRB(&F->getEntryBlock().front());
  787. return RetvalTLSPtr = IRB.CreateCall(DFS.GetRetvalTLS, {});
  788. }
  789. Value *DFSanFunction::getArgTLS(unsigned Idx, Instruction *Pos) {
  790. IRBuilder<> IRB(Pos);
  791. return IRB.CreateConstGEP2_64(getArgTLSPtr(), 0, Idx);
  792. }
  793. Value *DFSanFunction::getShadow(Value *V) {
  794. if (!isa<Argument>(V) && !isa<Instruction>(V))
  795. return DFS.ZeroShadow;
  796. Value *&Shadow = ValShadowMap[V];
  797. if (!Shadow) {
  798. if (Argument *A = dyn_cast<Argument>(V)) {
  799. if (IsNativeABI)
  800. return DFS.ZeroShadow;
  801. switch (IA) {
  802. case DataFlowSanitizer::IA_TLS: {
  803. Value *ArgTLSPtr = getArgTLSPtr();
  804. Instruction *ArgTLSPos =
  805. DFS.ArgTLS ? &*F->getEntryBlock().begin()
  806. : cast<Instruction>(ArgTLSPtr)->getNextNode();
  807. IRBuilder<> IRB(ArgTLSPos);
  808. Shadow = IRB.CreateLoad(getArgTLS(A->getArgNo(), ArgTLSPos));
  809. break;
  810. }
  811. case DataFlowSanitizer::IA_Args: {
  812. unsigned ArgIdx = A->getArgNo() + F->arg_size() / 2;
  813. Function::arg_iterator i = F->arg_begin();
  814. while (ArgIdx--)
  815. ++i;
  816. Shadow = &*i;
  817. assert(Shadow->getType() == DFS.ShadowTy);
  818. break;
  819. }
  820. }
  821. NonZeroChecks.push_back(Shadow);
  822. } else {
  823. Shadow = DFS.ZeroShadow;
  824. }
  825. }
  826. return Shadow;
  827. }
  828. void DFSanFunction::setShadow(Instruction *I, Value *Shadow) {
  829. assert(!ValShadowMap.count(I));
  830. assert(Shadow->getType() == DFS.ShadowTy);
  831. ValShadowMap[I] = Shadow;
  832. }
  833. Value *DataFlowSanitizer::getShadowAddress(Value *Addr, Instruction *Pos) {
  834. assert(Addr != RetvalTLS && "Reinstrumenting?");
  835. IRBuilder<> IRB(Pos);
  836. Value *ShadowPtrMaskValue;
  837. if (DFSanRuntimeShadowMask)
  838. ShadowPtrMaskValue = IRB.CreateLoad(IntptrTy, ExternalShadowMask);
  839. else
  840. ShadowPtrMaskValue = ShadowPtrMask;
  841. return IRB.CreateIntToPtr(
  842. IRB.CreateMul(
  843. IRB.CreateAnd(IRB.CreatePtrToInt(Addr, IntptrTy),
  844. IRB.CreatePtrToInt(ShadowPtrMaskValue, IntptrTy)),
  845. ShadowPtrMul),
  846. ShadowPtrTy);
  847. }
  848. // Generates IR to compute the union of the two given shadows, inserting it
  849. // before Pos. Returns the computed union Value.
  850. Value *DFSanFunction::combineShadows(Value *V1, Value *V2, Instruction *Pos) {
  851. if (V1 == DFS.ZeroShadow)
  852. return V2;
  853. if (V2 == DFS.ZeroShadow)
  854. return V1;
  855. if (V1 == V2)
  856. return V1;
  857. auto V1Elems = ShadowElements.find(V1);
  858. auto V2Elems = ShadowElements.find(V2);
  859. if (V1Elems != ShadowElements.end() && V2Elems != ShadowElements.end()) {
  860. if (std::includes(V1Elems->second.begin(), V1Elems->second.end(),
  861. V2Elems->second.begin(), V2Elems->second.end())) {
  862. return V1;
  863. } else if (std::includes(V2Elems->second.begin(), V2Elems->second.end(),
  864. V1Elems->second.begin(), V1Elems->second.end())) {
  865. return V2;
  866. }
  867. } else if (V1Elems != ShadowElements.end()) {
  868. if (V1Elems->second.count(V2))
  869. return V1;
  870. } else if (V2Elems != ShadowElements.end()) {
  871. if (V2Elems->second.count(V1))
  872. return V2;
  873. }
  874. auto Key = std::make_pair(V1, V2);
  875. if (V1 > V2)
  876. std::swap(Key.first, Key.second);
  877. CachedCombinedShadow &CCS = CachedCombinedShadows[Key];
  878. if (CCS.Block && DT.dominates(CCS.Block, Pos->getParent()))
  879. return CCS.Shadow;
  880. IRBuilder<> IRB(Pos);
  881. if (AvoidNewBlocks) {
  882. CallInst *Call = IRB.CreateCall(DFS.DFSanCheckedUnionFn, {V1, V2});
  883. Call->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
  884. Call->addParamAttr(0, Attribute::ZExt);
  885. Call->addParamAttr(1, Attribute::ZExt);
  886. CCS.Block = Pos->getParent();
  887. CCS.Shadow = Call;
  888. } else {
  889. BasicBlock *Head = Pos->getParent();
  890. Value *Ne = IRB.CreateICmpNE(V1, V2);
  891. BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
  892. Ne, Pos, /*Unreachable=*/false, DFS.ColdCallWeights, &DT));
  893. IRBuilder<> ThenIRB(BI);
  894. CallInst *Call = ThenIRB.CreateCall(DFS.DFSanUnionFn, {V1, V2});
  895. Call->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
  896. Call->addParamAttr(0, Attribute::ZExt);
  897. Call->addParamAttr(1, Attribute::ZExt);
  898. BasicBlock *Tail = BI->getSuccessor(0);
  899. PHINode *Phi = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front());
  900. Phi->addIncoming(Call, Call->getParent());
  901. Phi->addIncoming(V1, Head);
  902. CCS.Block = Tail;
  903. CCS.Shadow = Phi;
  904. }
  905. std::set<Value *> UnionElems;
  906. if (V1Elems != ShadowElements.end()) {
  907. UnionElems = V1Elems->second;
  908. } else {
  909. UnionElems.insert(V1);
  910. }
  911. if (V2Elems != ShadowElements.end()) {
  912. UnionElems.insert(V2Elems->second.begin(), V2Elems->second.end());
  913. } else {
  914. UnionElems.insert(V2);
  915. }
  916. ShadowElements[CCS.Shadow] = std::move(UnionElems);
  917. return CCS.Shadow;
  918. }
  919. // A convenience function which folds the shadows of each of the operands
  920. // of the provided instruction Inst, inserting the IR before Inst. Returns
  921. // the computed union Value.
  922. Value *DFSanFunction::combineOperandShadows(Instruction *Inst) {
  923. if (Inst->getNumOperands() == 0)
  924. return DFS.ZeroShadow;
  925. Value *Shadow = getShadow(Inst->getOperand(0));
  926. for (unsigned i = 1, n = Inst->getNumOperands(); i != n; ++i) {
  927. Shadow = combineShadows(Shadow, getShadow(Inst->getOperand(i)), Inst);
  928. }
  929. return Shadow;
  930. }
  931. void DFSanVisitor::visitOperandShadowInst(Instruction &I) {
  932. Value *CombinedShadow = DFSF.combineOperandShadows(&I);
  933. DFSF.setShadow(&I, CombinedShadow);
  934. }
  935. // Generates IR to load shadow corresponding to bytes [Addr, Addr+Size), where
  936. // Addr has alignment Align, and take the union of each of those shadows.
  937. Value *DFSanFunction::loadShadow(Value *Addr, uint64_t Size, uint64_t Align,
  938. Instruction *Pos) {
  939. if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
  940. llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
  941. AllocaShadowMap.find(AI);
  942. if (i != AllocaShadowMap.end()) {
  943. IRBuilder<> IRB(Pos);
  944. return IRB.CreateLoad(i->second);
  945. }
  946. }
  947. uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
  948. SmallVector<Value *, 2> Objs;
  949. GetUnderlyingObjects(Addr, Objs, Pos->getModule()->getDataLayout());
  950. bool AllConstants = true;
  951. for (Value *Obj : Objs) {
  952. if (isa<Function>(Obj) || isa<BlockAddress>(Obj))
  953. continue;
  954. if (isa<GlobalVariable>(Obj) && cast<GlobalVariable>(Obj)->isConstant())
  955. continue;
  956. AllConstants = false;
  957. break;
  958. }
  959. if (AllConstants)
  960. return DFS.ZeroShadow;
  961. Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
  962. switch (Size) {
  963. case 0:
  964. return DFS.ZeroShadow;
  965. case 1: {
  966. LoadInst *LI = new LoadInst(ShadowAddr, "", Pos);
  967. LI->setAlignment(ShadowAlign);
  968. return LI;
  969. }
  970. case 2: {
  971. IRBuilder<> IRB(Pos);
  972. Value *ShadowAddr1 = IRB.CreateGEP(DFS.ShadowTy, ShadowAddr,
  973. ConstantInt::get(DFS.IntptrTy, 1));
  974. return combineShadows(IRB.CreateAlignedLoad(ShadowAddr, ShadowAlign),
  975. IRB.CreateAlignedLoad(ShadowAddr1, ShadowAlign), Pos);
  976. }
  977. }
  978. if (!AvoidNewBlocks && Size % (64 / DFS.ShadowWidth) == 0) {
  979. // Fast path for the common case where each byte has identical shadow: load
  980. // shadow 64 bits at a time, fall out to a __dfsan_union_load call if any
  981. // shadow is non-equal.
  982. BasicBlock *FallbackBB = BasicBlock::Create(*DFS.Ctx, "", F);
  983. IRBuilder<> FallbackIRB(FallbackBB);
  984. CallInst *FallbackCall = FallbackIRB.CreateCall(
  985. DFS.DFSanUnionLoadFn,
  986. {ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size)});
  987. FallbackCall->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
  988. // Compare each of the shadows stored in the loaded 64 bits to each other,
  989. // by computing (WideShadow rotl ShadowWidth) == WideShadow.
  990. IRBuilder<> IRB(Pos);
  991. Value *WideAddr =
  992. IRB.CreateBitCast(ShadowAddr, Type::getInt64PtrTy(*DFS.Ctx));
  993. Value *WideShadow = IRB.CreateAlignedLoad(WideAddr, ShadowAlign);
  994. Value *TruncShadow = IRB.CreateTrunc(WideShadow, DFS.ShadowTy);
  995. Value *ShlShadow = IRB.CreateShl(WideShadow, DFS.ShadowWidth);
  996. Value *ShrShadow = IRB.CreateLShr(WideShadow, 64 - DFS.ShadowWidth);
  997. Value *RotShadow = IRB.CreateOr(ShlShadow, ShrShadow);
  998. Value *ShadowsEq = IRB.CreateICmpEQ(WideShadow, RotShadow);
  999. BasicBlock *Head = Pos->getParent();
  1000. BasicBlock *Tail = Head->splitBasicBlock(Pos->getIterator());
  1001. if (DomTreeNode *OldNode = DT.getNode(Head)) {
  1002. std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
  1003. DomTreeNode *NewNode = DT.addNewBlock(Tail, Head);
  1004. for (auto Child : Children)
  1005. DT.changeImmediateDominator(Child, NewNode);
  1006. }
  1007. // In the following code LastBr will refer to the previous basic block's
  1008. // conditional branch instruction, whose true successor is fixed up to point
  1009. // to the next block during the loop below or to the tail after the final
  1010. // iteration.
  1011. BranchInst *LastBr = BranchInst::Create(FallbackBB, FallbackBB, ShadowsEq);
  1012. ReplaceInstWithInst(Head->getTerminator(), LastBr);
  1013. DT.addNewBlock(FallbackBB, Head);
  1014. for (uint64_t Ofs = 64 / DFS.ShadowWidth; Ofs != Size;
  1015. Ofs += 64 / DFS.ShadowWidth) {
  1016. BasicBlock *NextBB = BasicBlock::Create(*DFS.Ctx, "", F);
  1017. DT.addNewBlock(NextBB, LastBr->getParent());
  1018. IRBuilder<> NextIRB(NextBB);
  1019. WideAddr = NextIRB.CreateGEP(Type::getInt64Ty(*DFS.Ctx), WideAddr,
  1020. ConstantInt::get(DFS.IntptrTy, 1));
  1021. Value *NextWideShadow = NextIRB.CreateAlignedLoad(WideAddr, ShadowAlign);
  1022. ShadowsEq = NextIRB.CreateICmpEQ(WideShadow, NextWideShadow);
  1023. LastBr->setSuccessor(0, NextBB);
  1024. LastBr = NextIRB.CreateCondBr(ShadowsEq, FallbackBB, FallbackBB);
  1025. }
  1026. LastBr->setSuccessor(0, Tail);
  1027. FallbackIRB.CreateBr(Tail);
  1028. PHINode *Shadow = PHINode::Create(DFS.ShadowTy, 2, "", &Tail->front());
  1029. Shadow->addIncoming(FallbackCall, FallbackBB);
  1030. Shadow->addIncoming(TruncShadow, LastBr->getParent());
  1031. return Shadow;
  1032. }
  1033. IRBuilder<> IRB(Pos);
  1034. CallInst *FallbackCall = IRB.CreateCall(
  1035. DFS.DFSanUnionLoadFn, {ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size)});
  1036. FallbackCall->addAttribute(AttributeList::ReturnIndex, Attribute::ZExt);
  1037. return FallbackCall;
  1038. }
  1039. void DFSanVisitor::visitLoadInst(LoadInst &LI) {
  1040. auto &DL = LI.getModule()->getDataLayout();
  1041. uint64_t Size = DL.getTypeStoreSize(LI.getType());
  1042. if (Size == 0) {
  1043. DFSF.setShadow(&LI, DFSF.DFS.ZeroShadow);
  1044. return;
  1045. }
  1046. uint64_t Align;
  1047. if (ClPreserveAlignment) {
  1048. Align = LI.getAlignment();
  1049. if (Align == 0)
  1050. Align = DL.getABITypeAlignment(LI.getType());
  1051. } else {
  1052. Align = 1;
  1053. }
  1054. IRBuilder<> IRB(&LI);
  1055. Value *Shadow = DFSF.loadShadow(LI.getPointerOperand(), Size, Align, &LI);
  1056. if (ClCombinePointerLabelsOnLoad) {
  1057. Value *PtrShadow = DFSF.getShadow(LI.getPointerOperand());
  1058. Shadow = DFSF.combineShadows(Shadow, PtrShadow, &LI);
  1059. }
  1060. if (Shadow != DFSF.DFS.ZeroShadow)
  1061. DFSF.NonZeroChecks.push_back(Shadow);
  1062. DFSF.setShadow(&LI, Shadow);
  1063. }
  1064. void DFSanFunction::storeShadow(Value *Addr, uint64_t Size, uint64_t Align,
  1065. Value *Shadow, Instruction *Pos) {
  1066. if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
  1067. llvm::DenseMap<AllocaInst *, AllocaInst *>::iterator i =
  1068. AllocaShadowMap.find(AI);
  1069. if (i != AllocaShadowMap.end()) {
  1070. IRBuilder<> IRB(Pos);
  1071. IRB.CreateStore(Shadow, i->second);
  1072. return;
  1073. }
  1074. }
  1075. uint64_t ShadowAlign = Align * DFS.ShadowWidth / 8;
  1076. IRBuilder<> IRB(Pos);
  1077. Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
  1078. if (Shadow == DFS.ZeroShadow) {
  1079. IntegerType *ShadowTy = IntegerType::get(*DFS.Ctx, Size * DFS.ShadowWidth);
  1080. Value *ExtZeroShadow = ConstantInt::get(ShadowTy, 0);
  1081. Value *ExtShadowAddr =
  1082. IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowTy));
  1083. IRB.CreateAlignedStore(ExtZeroShadow, ExtShadowAddr, ShadowAlign);
  1084. return;
  1085. }
  1086. const unsigned ShadowVecSize = 128 / DFS.ShadowWidth;
  1087. uint64_t Offset = 0;
  1088. if (Size >= ShadowVecSize) {
  1089. VectorType *ShadowVecTy = VectorType::get(DFS.ShadowTy, ShadowVecSize);
  1090. Value *ShadowVec = UndefValue::get(ShadowVecTy);
  1091. for (unsigned i = 0; i != ShadowVecSize; ++i) {
  1092. ShadowVec = IRB.CreateInsertElement(
  1093. ShadowVec, Shadow, ConstantInt::get(Type::getInt32Ty(*DFS.Ctx), i));
  1094. }
  1095. Value *ShadowVecAddr =
  1096. IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowVecTy));
  1097. do {
  1098. Value *CurShadowVecAddr =
  1099. IRB.CreateConstGEP1_32(ShadowVecTy, ShadowVecAddr, Offset);
  1100. IRB.CreateAlignedStore(ShadowVec, CurShadowVecAddr, ShadowAlign);
  1101. Size -= ShadowVecSize;
  1102. ++Offset;
  1103. } while (Size >= ShadowVecSize);
  1104. Offset *= ShadowVecSize;
  1105. }
  1106. while (Size > 0) {
  1107. Value *CurShadowAddr =
  1108. IRB.CreateConstGEP1_32(DFS.ShadowTy, ShadowAddr, Offset);
  1109. IRB.CreateAlignedStore(Shadow, CurShadowAddr, ShadowAlign);
  1110. --Size;
  1111. ++Offset;
  1112. }
  1113. }
  1114. void DFSanVisitor::visitStoreInst(StoreInst &SI) {
  1115. auto &DL = SI.getModule()->getDataLayout();
  1116. uint64_t Size = DL.getTypeStoreSize(SI.getValueOperand()->getType());
  1117. if (Size == 0)
  1118. return;
  1119. uint64_t Align;
  1120. if (ClPreserveAlignment) {
  1121. Align = SI.getAlignment();
  1122. if (Align == 0)
  1123. Align = DL.getABITypeAlignment(SI.getValueOperand()->getType());
  1124. } else {
  1125. Align = 1;
  1126. }
  1127. Value* Shadow = DFSF.getShadow(SI.getValueOperand());
  1128. if (ClCombinePointerLabelsOnStore) {
  1129. Value *PtrShadow = DFSF.getShadow(SI.getPointerOperand());
  1130. Shadow = DFSF.combineShadows(Shadow, PtrShadow, &SI);
  1131. }
  1132. DFSF.storeShadow(SI.getPointerOperand(), Size, Align, Shadow, &SI);
  1133. }
  1134. void DFSanVisitor::visitBinaryOperator(BinaryOperator &BO) {
  1135. visitOperandShadowInst(BO);
  1136. }
  1137. void DFSanVisitor::visitCastInst(CastInst &CI) { visitOperandShadowInst(CI); }
  1138. void DFSanVisitor::visitCmpInst(CmpInst &CI) { visitOperandShadowInst(CI); }
  1139. void DFSanVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
  1140. visitOperandShadowInst(GEPI);
  1141. }
  1142. void DFSanVisitor::visitExtractElementInst(ExtractElementInst &I) {
  1143. visitOperandShadowInst(I);
  1144. }
  1145. void DFSanVisitor::visitInsertElementInst(InsertElementInst &I) {
  1146. visitOperandShadowInst(I);
  1147. }
  1148. void DFSanVisitor::visitShuffleVectorInst(ShuffleVectorInst &I) {
  1149. visitOperandShadowInst(I);
  1150. }
  1151. void DFSanVisitor::visitExtractValueInst(ExtractValueInst &I) {
  1152. visitOperandShadowInst(I);
  1153. }
  1154. void DFSanVisitor::visitInsertValueInst(InsertValueInst &I) {
  1155. visitOperandShadowInst(I);
  1156. }
  1157. void DFSanVisitor::visitAllocaInst(AllocaInst &I) {
  1158. bool AllLoadsStores = true;
  1159. for (User *U : I.users()) {
  1160. if (isa<LoadInst>(U))
  1161. continue;
  1162. if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  1163. if (SI->getPointerOperand() == &I)
  1164. continue;
  1165. }
  1166. AllLoadsStores = false;
  1167. break;
  1168. }
  1169. if (AllLoadsStores) {
  1170. IRBuilder<> IRB(&I);
  1171. DFSF.AllocaShadowMap[&I] = IRB.CreateAlloca(DFSF.DFS.ShadowTy);
  1172. }
  1173. DFSF.setShadow(&I, DFSF.DFS.ZeroShadow);
  1174. }
  1175. void DFSanVisitor::visitSelectInst(SelectInst &I) {
  1176. Value *CondShadow = DFSF.getShadow(I.getCondition());
  1177. Value *TrueShadow = DFSF.getShadow(I.getTrueValue());
  1178. Value *FalseShadow = DFSF.getShadow(I.getFalseValue());
  1179. if (isa<VectorType>(I.getCondition()->getType())) {
  1180. DFSF.setShadow(
  1181. &I,
  1182. DFSF.combineShadows(
  1183. CondShadow, DFSF.combineShadows(TrueShadow, FalseShadow, &I), &I));
  1184. } else {
  1185. Value *ShadowSel;
  1186. if (TrueShadow == FalseShadow) {
  1187. ShadowSel = TrueShadow;
  1188. } else {
  1189. ShadowSel =
  1190. SelectInst::Create(I.getCondition(), TrueShadow, FalseShadow, "", &I);
  1191. }
  1192. DFSF.setShadow(&I, DFSF.combineShadows(CondShadow, ShadowSel, &I));
  1193. }
  1194. }
  1195. void DFSanVisitor::visitMemSetInst(MemSetInst &I) {
  1196. IRBuilder<> IRB(&I);
  1197. Value *ValShadow = DFSF.getShadow(I.getValue());
  1198. IRB.CreateCall(DFSF.DFS.DFSanSetLabelFn,
  1199. {ValShadow, IRB.CreateBitCast(I.getDest(), Type::getInt8PtrTy(
  1200. *DFSF.DFS.Ctx)),
  1201. IRB.CreateZExtOrTrunc(I.getLength(), DFSF.DFS.IntptrTy)});
  1202. }
  1203. void DFSanVisitor::visitMemTransferInst(MemTransferInst &I) {
  1204. IRBuilder<> IRB(&I);
  1205. Value *DestShadow = DFSF.DFS.getShadowAddress(I.getDest(), &I);
  1206. Value *SrcShadow = DFSF.DFS.getShadowAddress(I.getSource(), &I);
  1207. Value *LenShadow = IRB.CreateMul(
  1208. I.getLength(),
  1209. ConstantInt::get(I.getLength()->getType(), DFSF.DFS.ShadowWidth / 8));
  1210. Value *AlignShadow;
  1211. if (ClPreserveAlignment) {
  1212. AlignShadow = IRB.CreateMul(I.getAlignmentCst(),
  1213. ConstantInt::get(I.getAlignmentCst()->getType(),
  1214. DFSF.DFS.ShadowWidth / 8));
  1215. } else {
  1216. AlignShadow = ConstantInt::get(I.getAlignmentCst()->getType(),
  1217. DFSF.DFS.ShadowWidth / 8);
  1218. }
  1219. Type *Int8Ptr = Type::getInt8PtrTy(*DFSF.DFS.Ctx);
  1220. DestShadow = IRB.CreateBitCast(DestShadow, Int8Ptr);
  1221. SrcShadow = IRB.CreateBitCast(SrcShadow, Int8Ptr);
  1222. IRB.CreateCall(I.getCalledValue(), {DestShadow, SrcShadow, LenShadow,
  1223. AlignShadow, I.getVolatileCst()});
  1224. }
  1225. void DFSanVisitor::visitReturnInst(ReturnInst &RI) {
  1226. if (!DFSF.IsNativeABI && RI.getReturnValue()) {
  1227. switch (DFSF.IA) {
  1228. case DataFlowSanitizer::IA_TLS: {
  1229. Value *S = DFSF.getShadow(RI.getReturnValue());
  1230. IRBuilder<> IRB(&RI);
  1231. IRB.CreateStore(S, DFSF.getRetvalTLS());
  1232. break;
  1233. }
  1234. case DataFlowSanitizer::IA_Args: {
  1235. IRBuilder<> IRB(&RI);
  1236. Type *RT = DFSF.F->getFunctionType()->getReturnType();
  1237. Value *InsVal =
  1238. IRB.CreateInsertValue(UndefValue::get(RT), RI.getReturnValue(), 0);
  1239. Value *InsShadow =
  1240. IRB.CreateInsertValue(InsVal, DFSF.getShadow(RI.getReturnValue()), 1);
  1241. RI.setOperand(0, InsShadow);
  1242. break;
  1243. }
  1244. }
  1245. }
  1246. }
  1247. void DFSanVisitor::visitCallSite(CallSite CS) {
  1248. Function *F = CS.getCalledFunction();
  1249. if ((F && F->isIntrinsic()) || isa<InlineAsm>(CS.getCalledValue())) {
  1250. visitOperandShadowInst(*CS.getInstruction());
  1251. return;
  1252. }
  1253. // Calls to this function are synthesized in wrappers, and we shouldn't
  1254. // instrument them.
  1255. if (F == DFSF.DFS.DFSanVarargWrapperFn)
  1256. return;
  1257. IRBuilder<> IRB(CS.getInstruction());
  1258. DenseMap<Value *, Function *>::iterator i =
  1259. DFSF.DFS.UnwrappedFnMap.find(CS.getCalledValue());
  1260. if (i != DFSF.DFS.UnwrappedFnMap.end()) {
  1261. Function *F = i->second;
  1262. switch (DFSF.DFS.getWrapperKind(F)) {
  1263. case DataFlowSanitizer::WK_Warning: {
  1264. CS.setCalledFunction(F);
  1265. IRB.CreateCall(DFSF.DFS.DFSanUnimplementedFn,
  1266. IRB.CreateGlobalStringPtr(F->getName()));
  1267. DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
  1268. return;
  1269. }
  1270. case DataFlowSanitizer::WK_Discard: {
  1271. CS.setCalledFunction(F);
  1272. DFSF.setShadow(CS.getInstruction(), DFSF.DFS.ZeroShadow);
  1273. return;
  1274. }
  1275. case DataFlowSanitizer::WK_Functional: {
  1276. CS.setCalledFunction(F);
  1277. visitOperandShadowInst(*CS.getInstruction());
  1278. return;
  1279. }
  1280. case DataFlowSanitizer::WK_Custom: {
  1281. // Don't try to handle invokes of custom functions, it's too complicated.
  1282. // Instead, invoke the dfsw$ wrapper, which will in turn call the __dfsw_
  1283. // wrapper.
  1284. if (CallInst *CI = dyn_cast<CallInst>(CS.getInstruction())) {
  1285. FunctionType *FT = F->getFunctionType();
  1286. FunctionType *CustomFT = DFSF.DFS.getCustomFunctionType(FT);
  1287. std::string CustomFName = "__dfsw_";
  1288. CustomFName += F->getName();
  1289. Constant *CustomF =
  1290. DFSF.DFS.Mod->getOrInsertFunction(CustomFName, CustomFT);
  1291. if (Function *CustomFn = dyn_cast<Function>(CustomF)) {
  1292. CustomFn->copyAttributesFrom(F);
  1293. // Custom functions returning non-void will write to the return label.
  1294. if (!FT->getReturnType()->isVoidTy()) {
  1295. CustomFn->removeAttributes(AttributeList::FunctionIndex,
  1296. DFSF.DFS.ReadOnlyNoneAttrs);
  1297. }
  1298. }
  1299. std::vector<Value *> Args;
  1300. CallSite::arg_iterator i = CS.arg_begin();
  1301. for (unsigned n = FT->getNumParams(); n != 0; ++i, --n) {
  1302. Type *T = (*i)->getType();
  1303. FunctionType *ParamFT;
  1304. if (isa<PointerType>(T) &&
  1305. (ParamFT = dyn_cast<FunctionType>(
  1306. cast<PointerType>(T)->getElementType()))) {
  1307. std::string TName = "dfst";
  1308. TName += utostr(FT->getNumParams() - n);
  1309. TName += "$";
  1310. TName += F->getName();
  1311. Constant *T = DFSF.DFS.getOrBuildTrampolineFunction(ParamFT, TName);
  1312. Args.push_back(T);
  1313. Args.push_back(
  1314. IRB.CreateBitCast(*i, Type::getInt8PtrTy(*DFSF.DFS.Ctx)));
  1315. } else {
  1316. Args.push_back(*i);
  1317. }
  1318. }
  1319. i = CS.arg_begin();
  1320. for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
  1321. Args.push_back(DFSF.getShadow(*i));
  1322. if (FT->isVarArg()) {
  1323. auto *LabelVATy = ArrayType::get(DFSF.DFS.ShadowTy,
  1324. CS.arg_size() - FT->getNumParams());
  1325. auto *LabelVAAlloca = new AllocaInst(
  1326. LabelVATy, getDataLayout().getAllocaAddrSpace(),
  1327. "labelva", &DFSF.F->getEntryBlock().front());
  1328. for (unsigned n = 0; i != CS.arg_end(); ++i, ++n) {
  1329. auto LabelVAPtr = IRB.CreateStructGEP(LabelVATy, LabelVAAlloca, n);
  1330. IRB.CreateStore(DFSF.getShadow(*i), LabelVAPtr);
  1331. }
  1332. Args.push_back(IRB.CreateStructGEP(LabelVATy, LabelVAAlloca, 0));
  1333. }
  1334. if (!FT->getReturnType()->isVoidTy()) {
  1335. if (!DFSF.LabelReturnAlloca) {
  1336. DFSF.LabelReturnAlloca =
  1337. new AllocaInst(DFSF.DFS.ShadowTy,
  1338. getDataLayout().getAllocaAddrSpace(),
  1339. "labelreturn", &DFSF.F->getEntryBlock().front());
  1340. }
  1341. Args.push_back(DFSF.LabelReturnAlloca);
  1342. }
  1343. for (i = CS.arg_begin() + FT->getNumParams(); i != CS.arg_end(); ++i)
  1344. Args.push_back(*i);
  1345. CallInst *CustomCI = IRB.CreateCall(CustomF, Args);
  1346. CustomCI->setCallingConv(CI->getCallingConv());
  1347. CustomCI->setAttributes(CI->getAttributes());
  1348. if (!FT->getReturnType()->isVoidTy()) {
  1349. LoadInst *LabelLoad = IRB.CreateLoad(DFSF.LabelReturnAlloca);
  1350. DFSF.setShadow(CustomCI, LabelLoad);
  1351. }
  1352. CI->replaceAllUsesWith(CustomCI);
  1353. CI->eraseFromParent();
  1354. return;
  1355. }
  1356. break;
  1357. }
  1358. }
  1359. }
  1360. FunctionType *FT = cast<FunctionType>(
  1361. CS.getCalledValue()->getType()->getPointerElementType());
  1362. if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
  1363. for (unsigned i = 0, n = FT->getNumParams(); i != n; ++i) {
  1364. IRB.CreateStore(DFSF.getShadow(CS.getArgument(i)),
  1365. DFSF.getArgTLS(i, CS.getInstruction()));
  1366. }
  1367. }
  1368. Instruction *Next = nullptr;
  1369. if (!CS.getType()->isVoidTy()) {
  1370. if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
  1371. if (II->getNormalDest()->getSinglePredecessor()) {
  1372. Next = &II->getNormalDest()->front();
  1373. } else {
  1374. BasicBlock *NewBB =
  1375. SplitEdge(II->getParent(), II->getNormalDest(), &DFSF.DT);
  1376. Next = &NewBB->front();
  1377. }
  1378. } else {
  1379. assert(CS->getIterator() != CS->getParent()->end());
  1380. Next = CS->getNextNode();
  1381. }
  1382. if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_TLS) {
  1383. IRBuilder<> NextIRB(Next);
  1384. LoadInst *LI = NextIRB.CreateLoad(DFSF.getRetvalTLS());
  1385. DFSF.SkipInsts.insert(LI);
  1386. DFSF.setShadow(CS.getInstruction(), LI);
  1387. DFSF.NonZeroChecks.push_back(LI);
  1388. }
  1389. }
  1390. // Do all instrumentation for IA_Args down here to defer tampering with the
  1391. // CFG in a way that SplitEdge may be able to detect.
  1392. if (DFSF.DFS.getInstrumentedABI() == DataFlowSanitizer::IA_Args) {
  1393. FunctionType *NewFT = DFSF.DFS.getArgsFunctionType(FT);
  1394. Value *Func =
  1395. IRB.CreateBitCast(CS.getCalledValue(), PointerType::getUnqual(NewFT));
  1396. std::vector<Value *> Args;
  1397. CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
  1398. for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
  1399. Args.push_back(*i);
  1400. i = CS.arg_begin();
  1401. for (unsigned n = FT->getNumParams(); n != 0; ++i, --n)
  1402. Args.push_back(DFSF.getShadow(*i));
  1403. if (FT->isVarArg()) {
  1404. unsigned VarArgSize = CS.arg_size() - FT->getNumParams();
  1405. ArrayType *VarArgArrayTy = ArrayType::get(DFSF.DFS.ShadowTy, VarArgSize);
  1406. AllocaInst *VarArgShadow =
  1407. new AllocaInst(VarArgArrayTy, getDataLayout().getAllocaAddrSpace(),
  1408. "", &DFSF.F->getEntryBlock().front());
  1409. Args.push_back(IRB.CreateConstGEP2_32(VarArgArrayTy, VarArgShadow, 0, 0));
  1410. for (unsigned n = 0; i != e; ++i, ++n) {
  1411. IRB.CreateStore(
  1412. DFSF.getShadow(*i),
  1413. IRB.CreateConstGEP2_32(VarArgArrayTy, VarArgShadow, 0, n));
  1414. Args.push_back(*i);
  1415. }
  1416. }
  1417. CallSite NewCS;
  1418. if (InvokeInst *II = dyn_cast<InvokeInst>(CS.getInstruction())) {
  1419. NewCS = IRB.CreateInvoke(Func, II->getNormalDest(), II->getUnwindDest(),
  1420. Args);
  1421. } else {
  1422. NewCS = IRB.CreateCall(Func, Args);
  1423. }
  1424. NewCS.setCallingConv(CS.getCallingConv());
  1425. NewCS.setAttributes(CS.getAttributes().removeAttributes(
  1426. *DFSF.DFS.Ctx, AttributeList::ReturnIndex,
  1427. AttributeFuncs::typeIncompatible(NewCS.getInstruction()->getType())));
  1428. if (Next) {
  1429. ExtractValueInst *ExVal =
  1430. ExtractValueInst::Create(NewCS.getInstruction(), 0, "", Next);
  1431. DFSF.SkipInsts.insert(ExVal);
  1432. ExtractValueInst *ExShadow =
  1433. ExtractValueInst::Create(NewCS.getInstruction(), 1, "", Next);
  1434. DFSF.SkipInsts.insert(ExShadow);
  1435. DFSF.setShadow(ExVal, ExShadow);
  1436. DFSF.NonZeroChecks.push_back(ExShadow);
  1437. CS.getInstruction()->replaceAllUsesWith(ExVal);
  1438. }
  1439. CS.getInstruction()->eraseFromParent();
  1440. }
  1441. }
  1442. void DFSanVisitor::visitPHINode(PHINode &PN) {
  1443. PHINode *ShadowPN =
  1444. PHINode::Create(DFSF.DFS.ShadowTy, PN.getNumIncomingValues(), "", &PN);
  1445. // Give the shadow phi node valid predecessors to fool SplitEdge into working.
  1446. Value *UndefShadow = UndefValue::get(DFSF.DFS.ShadowTy);
  1447. for (PHINode::block_iterator i = PN.block_begin(), e = PN.block_end(); i != e;
  1448. ++i) {
  1449. ShadowPN->addIncoming(UndefShadow, *i);
  1450. }
  1451. DFSF.PHIFixups.push_back(std::make_pair(&PN, ShadowPN));
  1452. DFSF.setShadow(&PN, ShadowPN);
  1453. }