DataFlowSanitizer.cpp 60 KB

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