DataFlowSanitizer.cpp 133 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486
  1. //===- DataFlowSanitizer.cpp - dynamic data flow analysis -----------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  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.
  20. ///
  21. /// Argument and return value labels are passed through TLS variables
  22. /// __dfsan_arg_tls and __dfsan_retval_tls.
  23. ///
  24. /// Each byte of application memory is backed by a shadow memory byte. The
  25. /// shadow byte can represent up to 8 labels. On Linux/x86_64, memory is then
  26. /// laid out as follows:
  27. ///
  28. /// +--------------------+ 0x800000000000 (top of memory)
  29. /// | application 3 |
  30. /// +--------------------+ 0x700000000000
  31. /// | invalid |
  32. /// +--------------------+ 0x610000000000
  33. /// | origin 1 |
  34. /// +--------------------+ 0x600000000000
  35. /// | application 2 |
  36. /// +--------------------+ 0x510000000000
  37. /// | shadow 1 |
  38. /// +--------------------+ 0x500000000000
  39. /// | invalid |
  40. /// +--------------------+ 0x400000000000
  41. /// | origin 3 |
  42. /// +--------------------+ 0x300000000000
  43. /// | shadow 3 |
  44. /// +--------------------+ 0x200000000000
  45. /// | origin 2 |
  46. /// +--------------------+ 0x110000000000
  47. /// | invalid |
  48. /// +--------------------+ 0x100000000000
  49. /// | shadow 2 |
  50. /// +--------------------+ 0x010000000000
  51. /// | application 1 |
  52. /// +--------------------+ 0x000000000000
  53. ///
  54. /// MEM_TO_SHADOW(mem) = mem ^ 0x500000000000
  55. /// SHADOW_TO_ORIGIN(shadow) = shadow + 0x100000000000
  56. ///
  57. /// For more information, please refer to the design document:
  58. /// http://clang.llvm.org/docs/DataFlowSanitizerDesign.html
  59. //
  60. //===----------------------------------------------------------------------===//
  61. #include "llvm/Transforms/Instrumentation/DataFlowSanitizer.h"
  62. #include "llvm/ADT/DenseMap.h"
  63. #include "llvm/ADT/DenseSet.h"
  64. #include "llvm/ADT/DepthFirstIterator.h"
  65. #include "llvm/ADT/SmallPtrSet.h"
  66. #include "llvm/ADT/SmallVector.h"
  67. #include "llvm/ADT/StringRef.h"
  68. #include "llvm/ADT/StringSet.h"
  69. #include "llvm/ADT/Triple.h"
  70. #include "llvm/ADT/iterator.h"
  71. #include "llvm/Analysis/GlobalsModRef.h"
  72. #include "llvm/Analysis/TargetLibraryInfo.h"
  73. #include "llvm/Analysis/ValueTracking.h"
  74. #include "llvm/IR/Argument.h"
  75. #include "llvm/IR/Attributes.h"
  76. #include "llvm/IR/BasicBlock.h"
  77. #include "llvm/IR/Constant.h"
  78. #include "llvm/IR/Constants.h"
  79. #include "llvm/IR/DataLayout.h"
  80. #include "llvm/IR/DerivedTypes.h"
  81. #include "llvm/IR/Dominators.h"
  82. #include "llvm/IR/Function.h"
  83. #include "llvm/IR/GlobalAlias.h"
  84. #include "llvm/IR/GlobalValue.h"
  85. #include "llvm/IR/GlobalVariable.h"
  86. #include "llvm/IR/IRBuilder.h"
  87. #include "llvm/IR/InstVisitor.h"
  88. #include "llvm/IR/InstrTypes.h"
  89. #include "llvm/IR/Instruction.h"
  90. #include "llvm/IR/Instructions.h"
  91. #include "llvm/IR/IntrinsicInst.h"
  92. #include "llvm/IR/MDBuilder.h"
  93. #include "llvm/IR/Module.h"
  94. #include "llvm/IR/PassManager.h"
  95. #include "llvm/IR/Type.h"
  96. #include "llvm/IR/User.h"
  97. #include "llvm/IR/Value.h"
  98. #include "llvm/InitializePasses.h"
  99. #include "llvm/Pass.h"
  100. #include "llvm/Support/Alignment.h"
  101. #include "llvm/Support/Casting.h"
  102. #include "llvm/Support/CommandLine.h"
  103. #include "llvm/Support/ErrorHandling.h"
  104. #include "llvm/Support/SpecialCaseList.h"
  105. #include "llvm/Support/VirtualFileSystem.h"
  106. #include "llvm/Transforms/Instrumentation.h"
  107. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  108. #include "llvm/Transforms/Utils/Local.h"
  109. #include <algorithm>
  110. #include <cassert>
  111. #include <cstddef>
  112. #include <cstdint>
  113. #include <memory>
  114. #include <set>
  115. #include <string>
  116. #include <utility>
  117. #include <vector>
  118. using namespace llvm;
  119. // This must be consistent with ShadowWidthBits.
  120. static const Align ShadowTLSAlignment = Align(2);
  121. static const Align MinOriginAlignment = Align(4);
  122. // The size of TLS variables. These constants must be kept in sync with the ones
  123. // in dfsan.cpp.
  124. static const unsigned ArgTLSSize = 800;
  125. static const unsigned RetvalTLSSize = 800;
  126. // The -dfsan-preserve-alignment flag controls whether this pass assumes that
  127. // alignment requirements provided by the input IR are correct. For example,
  128. // if the input IR contains a load with alignment 8, this flag will cause
  129. // the shadow load to have alignment 16. This flag is disabled by default as
  130. // we have unfortunately encountered too much code (including Clang itself;
  131. // see PR14291) which performs misaligned access.
  132. static cl::opt<bool> ClPreserveAlignment(
  133. "dfsan-preserve-alignment",
  134. cl::desc("respect alignment requirements provided by input IR"), cl::Hidden,
  135. cl::init(false));
  136. // The ABI list files control how shadow parameters are passed. The pass treats
  137. // every function labelled "uninstrumented" in the ABI list file as conforming
  138. // to the "native" (i.e. unsanitized) ABI. Unless the ABI list contains
  139. // additional annotations for those functions, a call to one of those functions
  140. // will produce a warning message, as the labelling behaviour of the function is
  141. // unknown. The other supported annotations for uninstrumented functions are
  142. // "functional" and "discard", which are described below under
  143. // DataFlowSanitizer::WrapperKind.
  144. // Functions will often be labelled with both "uninstrumented" and one of
  145. // "functional" or "discard". This will leave the function unchanged by this
  146. // pass, and create a wrapper function that will call the original.
  147. //
  148. // Instrumented functions can also be annotated as "force_zero_labels", which
  149. // will make all shadow and return values set zero labels.
  150. // Functions should never be labelled with both "force_zero_labels" and
  151. // "uninstrumented" or any of the unistrumented wrapper kinds.
  152. static cl::list<std::string> ClABIListFiles(
  153. "dfsan-abilist",
  154. cl::desc("File listing native ABI functions and how the pass treats them"),
  155. cl::Hidden);
  156. // Controls whether the pass includes or ignores the labels of pointers in load
  157. // instructions.
  158. static cl::opt<bool> ClCombinePointerLabelsOnLoad(
  159. "dfsan-combine-pointer-labels-on-load",
  160. cl::desc("Combine the label of the pointer with the label of the data when "
  161. "loading from memory."),
  162. cl::Hidden, cl::init(true));
  163. // Controls whether the pass includes or ignores the labels of pointers in
  164. // stores instructions.
  165. static cl::opt<bool> ClCombinePointerLabelsOnStore(
  166. "dfsan-combine-pointer-labels-on-store",
  167. cl::desc("Combine the label of the pointer with the label of the data when "
  168. "storing in memory."),
  169. cl::Hidden, cl::init(false));
  170. // Controls whether the pass propagates labels of offsets in GEP instructions.
  171. static cl::opt<bool> ClCombineOffsetLabelsOnGEP(
  172. "dfsan-combine-offset-labels-on-gep",
  173. cl::desc(
  174. "Combine the label of the offset with the label of the pointer when "
  175. "doing pointer arithmetic."),
  176. cl::Hidden, cl::init(true));
  177. static cl::list<std::string> ClCombineTaintLookupTables(
  178. "dfsan-combine-taint-lookup-table",
  179. cl::desc(
  180. "When dfsan-combine-offset-labels-on-gep and/or "
  181. "dfsan-combine-pointer-labels-on-load are false, this flag can "
  182. "be used to re-enable combining offset and/or pointer taint when "
  183. "loading specific constant global variables (i.e. lookup tables)."),
  184. cl::Hidden);
  185. static cl::opt<bool> ClDebugNonzeroLabels(
  186. "dfsan-debug-nonzero-labels",
  187. cl::desc("Insert calls to __dfsan_nonzero_label on observing a parameter, "
  188. "load or return with a nonzero label"),
  189. cl::Hidden);
  190. // Experimental feature that inserts callbacks for certain data events.
  191. // Currently callbacks are only inserted for loads, stores, memory transfers
  192. // (i.e. memcpy and memmove), and comparisons.
  193. //
  194. // If this flag is set to true, the user must provide definitions for the
  195. // following callback functions:
  196. // void __dfsan_load_callback(dfsan_label Label, void* addr);
  197. // void __dfsan_store_callback(dfsan_label Label, void* addr);
  198. // void __dfsan_mem_transfer_callback(dfsan_label *Start, size_t Len);
  199. // void __dfsan_cmp_callback(dfsan_label CombinedLabel);
  200. static cl::opt<bool> ClEventCallbacks(
  201. "dfsan-event-callbacks",
  202. cl::desc("Insert calls to __dfsan_*_callback functions on data events."),
  203. cl::Hidden, cl::init(false));
  204. // Experimental feature that inserts callbacks for conditionals, including:
  205. // conditional branch, switch, select.
  206. // This must be true for dfsan_set_conditional_callback() to have effect.
  207. static cl::opt<bool> ClConditionalCallbacks(
  208. "dfsan-conditional-callbacks",
  209. cl::desc("Insert calls to callback functions on conditionals."), cl::Hidden,
  210. cl::init(false));
  211. // Experimental feature that inserts callbacks for data reaching a function,
  212. // either via function arguments and loads.
  213. // This must be true for dfsan_set_reaches_function_callback() to have effect.
  214. static cl::opt<bool> ClReachesFunctionCallbacks(
  215. "dfsan-reaches-function-callbacks",
  216. cl::desc("Insert calls to callback functions on data reaching a function."),
  217. cl::Hidden, cl::init(false));
  218. // Controls whether the pass tracks the control flow of select instructions.
  219. static cl::opt<bool> ClTrackSelectControlFlow(
  220. "dfsan-track-select-control-flow",
  221. cl::desc("Propagate labels from condition values of select instructions "
  222. "to results."),
  223. cl::Hidden, cl::init(true));
  224. // TODO: This default value follows MSan. DFSan may use a different value.
  225. static cl::opt<int> ClInstrumentWithCallThreshold(
  226. "dfsan-instrument-with-call-threshold",
  227. cl::desc("If the function being instrumented requires more than "
  228. "this number of origin stores, use callbacks instead of "
  229. "inline checks (-1 means never use callbacks)."),
  230. cl::Hidden, cl::init(3500));
  231. // Controls how to track origins.
  232. // * 0: do not track origins.
  233. // * 1: track origins at memory store operations.
  234. // * 2: track origins at memory load and store operations.
  235. // TODO: track callsites.
  236. static cl::opt<int> ClTrackOrigins("dfsan-track-origins",
  237. cl::desc("Track origins of labels"),
  238. cl::Hidden, cl::init(0));
  239. static cl::opt<bool> ClIgnorePersonalityRoutine(
  240. "dfsan-ignore-personality-routine",
  241. cl::desc("If a personality routine is marked uninstrumented from the ABI "
  242. "list, do not create a wrapper for it."),
  243. cl::Hidden, cl::init(false));
  244. static StringRef getGlobalTypeString(const GlobalValue &G) {
  245. // Types of GlobalVariables are always pointer types.
  246. Type *GType = G.getValueType();
  247. // For now we support excluding struct types only.
  248. if (StructType *SGType = dyn_cast<StructType>(GType)) {
  249. if (!SGType->isLiteral())
  250. return SGType->getName();
  251. }
  252. return "<unknown type>";
  253. }
  254. namespace {
  255. // Memory map parameters used in application-to-shadow address calculation.
  256. // Offset = (Addr & ~AndMask) ^ XorMask
  257. // Shadow = ShadowBase + Offset
  258. // Origin = (OriginBase + Offset) & ~3ULL
  259. struct MemoryMapParams {
  260. uint64_t AndMask;
  261. uint64_t XorMask;
  262. uint64_t ShadowBase;
  263. uint64_t OriginBase;
  264. };
  265. } // end anonymous namespace
  266. // NOLINTBEGIN(readability-identifier-naming)
  267. // aarch64 Linux
  268. const MemoryMapParams Linux_AArch64_MemoryMapParams = {
  269. 0, // AndMask (not used)
  270. 0x0B00000000000, // XorMask
  271. 0, // ShadowBase (not used)
  272. 0x0200000000000, // OriginBase
  273. };
  274. // x86_64 Linux
  275. const MemoryMapParams Linux_X86_64_MemoryMapParams = {
  276. 0, // AndMask (not used)
  277. 0x500000000000, // XorMask
  278. 0, // ShadowBase (not used)
  279. 0x100000000000, // OriginBase
  280. };
  281. // NOLINTEND(readability-identifier-naming)
  282. namespace {
  283. class DFSanABIList {
  284. std::unique_ptr<SpecialCaseList> SCL;
  285. public:
  286. DFSanABIList() = default;
  287. void set(std::unique_ptr<SpecialCaseList> List) { SCL = std::move(List); }
  288. /// Returns whether either this function or its source file are listed in the
  289. /// given category.
  290. bool isIn(const Function &F, StringRef Category) const {
  291. return isIn(*F.getParent(), Category) ||
  292. SCL->inSection("dataflow", "fun", F.getName(), Category);
  293. }
  294. /// Returns whether this global alias is listed in the given category.
  295. ///
  296. /// If GA aliases a function, the alias's name is matched as a function name
  297. /// would be. Similarly, aliases of globals are matched like globals.
  298. bool isIn(const GlobalAlias &GA, StringRef Category) const {
  299. if (isIn(*GA.getParent(), Category))
  300. return true;
  301. if (isa<FunctionType>(GA.getValueType()))
  302. return SCL->inSection("dataflow", "fun", GA.getName(), Category);
  303. return SCL->inSection("dataflow", "global", GA.getName(), Category) ||
  304. SCL->inSection("dataflow", "type", getGlobalTypeString(GA),
  305. Category);
  306. }
  307. /// Returns whether this module is listed in the given category.
  308. bool isIn(const Module &M, StringRef Category) const {
  309. return SCL->inSection("dataflow", "src", M.getModuleIdentifier(), Category);
  310. }
  311. };
  312. /// TransformedFunction is used to express the result of transforming one
  313. /// function type into another. This struct is immutable. It holds metadata
  314. /// useful for updating calls of the old function to the new type.
  315. struct TransformedFunction {
  316. TransformedFunction(FunctionType *OriginalType, FunctionType *TransformedType,
  317. std::vector<unsigned> ArgumentIndexMapping)
  318. : OriginalType(OriginalType), TransformedType(TransformedType),
  319. ArgumentIndexMapping(ArgumentIndexMapping) {}
  320. // Disallow copies.
  321. TransformedFunction(const TransformedFunction &) = delete;
  322. TransformedFunction &operator=(const TransformedFunction &) = delete;
  323. // Allow moves.
  324. TransformedFunction(TransformedFunction &&) = default;
  325. TransformedFunction &operator=(TransformedFunction &&) = default;
  326. /// Type of the function before the transformation.
  327. FunctionType *OriginalType;
  328. /// Type of the function after the transformation.
  329. FunctionType *TransformedType;
  330. /// Transforming a function may change the position of arguments. This
  331. /// member records the mapping from each argument's old position to its new
  332. /// position. Argument positions are zero-indexed. If the transformation
  333. /// from F to F' made the first argument of F into the third argument of F',
  334. /// then ArgumentIndexMapping[0] will equal 2.
  335. std::vector<unsigned> ArgumentIndexMapping;
  336. };
  337. /// Given function attributes from a call site for the original function,
  338. /// return function attributes appropriate for a call to the transformed
  339. /// function.
  340. AttributeList
  341. transformFunctionAttributes(const TransformedFunction &TransformedFunction,
  342. LLVMContext &Ctx, AttributeList CallSiteAttrs) {
  343. // Construct a vector of AttributeSet for each function argument.
  344. std::vector<llvm::AttributeSet> ArgumentAttributes(
  345. TransformedFunction.TransformedType->getNumParams());
  346. // Copy attributes from the parameter of the original function to the
  347. // transformed version. 'ArgumentIndexMapping' holds the mapping from
  348. // old argument position to new.
  349. for (unsigned I = 0, IE = TransformedFunction.ArgumentIndexMapping.size();
  350. I < IE; ++I) {
  351. unsigned TransformedIndex = TransformedFunction.ArgumentIndexMapping[I];
  352. ArgumentAttributes[TransformedIndex] = CallSiteAttrs.getParamAttrs(I);
  353. }
  354. // Copy annotations on varargs arguments.
  355. for (unsigned I = TransformedFunction.OriginalType->getNumParams(),
  356. IE = CallSiteAttrs.getNumAttrSets();
  357. I < IE; ++I) {
  358. ArgumentAttributes.push_back(CallSiteAttrs.getParamAttrs(I));
  359. }
  360. return AttributeList::get(Ctx, CallSiteAttrs.getFnAttrs(),
  361. CallSiteAttrs.getRetAttrs(),
  362. llvm::ArrayRef(ArgumentAttributes));
  363. }
  364. class DataFlowSanitizer {
  365. friend struct DFSanFunction;
  366. friend class DFSanVisitor;
  367. enum { ShadowWidthBits = 8, ShadowWidthBytes = ShadowWidthBits / 8 };
  368. enum { OriginWidthBits = 32, OriginWidthBytes = OriginWidthBits / 8 };
  369. /// How should calls to uninstrumented functions be handled?
  370. enum WrapperKind {
  371. /// This function is present in an uninstrumented form but we don't know
  372. /// how it should be handled. Print a warning and call the function anyway.
  373. /// Don't label the return value.
  374. WK_Warning,
  375. /// This function does not write to (user-accessible) memory, and its return
  376. /// value is unlabelled.
  377. WK_Discard,
  378. /// This function does not write to (user-accessible) memory, and the label
  379. /// of its return value is the union of the label of its arguments.
  380. WK_Functional,
  381. /// Instead of calling the function, a custom wrapper __dfsw_F is called,
  382. /// where F is the name of the function. This function may wrap the
  383. /// original function or provide its own implementation. WK_Custom uses an
  384. /// extra pointer argument to return the shadow. This allows the wrapped
  385. /// form of the function type to be expressed in C.
  386. WK_Custom
  387. };
  388. Module *Mod;
  389. LLVMContext *Ctx;
  390. Type *Int8Ptr;
  391. IntegerType *OriginTy;
  392. PointerType *OriginPtrTy;
  393. ConstantInt *ZeroOrigin;
  394. /// The shadow type for all primitive types and vector types.
  395. IntegerType *PrimitiveShadowTy;
  396. PointerType *PrimitiveShadowPtrTy;
  397. IntegerType *IntptrTy;
  398. ConstantInt *ZeroPrimitiveShadow;
  399. Constant *ArgTLS;
  400. ArrayType *ArgOriginTLSTy;
  401. Constant *ArgOriginTLS;
  402. Constant *RetvalTLS;
  403. Constant *RetvalOriginTLS;
  404. FunctionType *DFSanUnionLoadFnTy;
  405. FunctionType *DFSanLoadLabelAndOriginFnTy;
  406. FunctionType *DFSanUnimplementedFnTy;
  407. FunctionType *DFSanWrapperExternWeakNullFnTy;
  408. FunctionType *DFSanSetLabelFnTy;
  409. FunctionType *DFSanNonzeroLabelFnTy;
  410. FunctionType *DFSanVarargWrapperFnTy;
  411. FunctionType *DFSanConditionalCallbackFnTy;
  412. FunctionType *DFSanConditionalCallbackOriginFnTy;
  413. FunctionType *DFSanReachesFunctionCallbackFnTy;
  414. FunctionType *DFSanReachesFunctionCallbackOriginFnTy;
  415. FunctionType *DFSanCmpCallbackFnTy;
  416. FunctionType *DFSanLoadStoreCallbackFnTy;
  417. FunctionType *DFSanMemTransferCallbackFnTy;
  418. FunctionType *DFSanChainOriginFnTy;
  419. FunctionType *DFSanChainOriginIfTaintedFnTy;
  420. FunctionType *DFSanMemOriginTransferFnTy;
  421. FunctionType *DFSanMemShadowOriginTransferFnTy;
  422. FunctionType *DFSanMemShadowOriginConditionalExchangeFnTy;
  423. FunctionType *DFSanMaybeStoreOriginFnTy;
  424. FunctionCallee DFSanUnionLoadFn;
  425. FunctionCallee DFSanLoadLabelAndOriginFn;
  426. FunctionCallee DFSanUnimplementedFn;
  427. FunctionCallee DFSanWrapperExternWeakNullFn;
  428. FunctionCallee DFSanSetLabelFn;
  429. FunctionCallee DFSanNonzeroLabelFn;
  430. FunctionCallee DFSanVarargWrapperFn;
  431. FunctionCallee DFSanLoadCallbackFn;
  432. FunctionCallee DFSanStoreCallbackFn;
  433. FunctionCallee DFSanMemTransferCallbackFn;
  434. FunctionCallee DFSanConditionalCallbackFn;
  435. FunctionCallee DFSanConditionalCallbackOriginFn;
  436. FunctionCallee DFSanReachesFunctionCallbackFn;
  437. FunctionCallee DFSanReachesFunctionCallbackOriginFn;
  438. FunctionCallee DFSanCmpCallbackFn;
  439. FunctionCallee DFSanChainOriginFn;
  440. FunctionCallee DFSanChainOriginIfTaintedFn;
  441. FunctionCallee DFSanMemOriginTransferFn;
  442. FunctionCallee DFSanMemShadowOriginTransferFn;
  443. FunctionCallee DFSanMemShadowOriginConditionalExchangeFn;
  444. FunctionCallee DFSanMaybeStoreOriginFn;
  445. SmallPtrSet<Value *, 16> DFSanRuntimeFunctions;
  446. MDNode *ColdCallWeights;
  447. MDNode *OriginStoreWeights;
  448. DFSanABIList ABIList;
  449. DenseMap<Value *, Function *> UnwrappedFnMap;
  450. AttributeMask ReadOnlyNoneAttrs;
  451. StringSet<> CombineTaintLookupTableNames;
  452. /// Memory map parameters used in calculation mapping application addresses
  453. /// to shadow addresses and origin addresses.
  454. const MemoryMapParams *MapParams;
  455. Value *getShadowOffset(Value *Addr, IRBuilder<> &IRB);
  456. Value *getShadowAddress(Value *Addr, Instruction *Pos);
  457. Value *getShadowAddress(Value *Addr, Instruction *Pos, Value *ShadowOffset);
  458. std::pair<Value *, Value *>
  459. getShadowOriginAddress(Value *Addr, Align InstAlignment, Instruction *Pos);
  460. bool isInstrumented(const Function *F);
  461. bool isInstrumented(const GlobalAlias *GA);
  462. bool isForceZeroLabels(const Function *F);
  463. TransformedFunction getCustomFunctionType(FunctionType *T);
  464. WrapperKind getWrapperKind(Function *F);
  465. void addGlobalNameSuffix(GlobalValue *GV);
  466. void buildExternWeakCheckIfNeeded(IRBuilder<> &IRB, Function *F);
  467. Function *buildWrapperFunction(Function *F, StringRef NewFName,
  468. GlobalValue::LinkageTypes NewFLink,
  469. FunctionType *NewFT);
  470. void initializeCallbackFunctions(Module &M);
  471. void initializeRuntimeFunctions(Module &M);
  472. bool initializeModule(Module &M);
  473. /// Advances \p OriginAddr to point to the next 32-bit origin and then loads
  474. /// from it. Returns the origin's loaded value.
  475. Value *loadNextOrigin(Instruction *Pos, Align OriginAlign,
  476. Value **OriginAddr);
  477. /// Returns whether the given load byte size is amenable to inlined
  478. /// optimization patterns.
  479. bool hasLoadSizeForFastPath(uint64_t Size);
  480. /// Returns whether the pass tracks origins. Supports only TLS ABI mode.
  481. bool shouldTrackOrigins();
  482. /// Returns a zero constant with the shadow type of OrigTy.
  483. ///
  484. /// getZeroShadow({T1,T2,...}) = {getZeroShadow(T1),getZeroShadow(T2,...}
  485. /// getZeroShadow([n x T]) = [n x getZeroShadow(T)]
  486. /// getZeroShadow(other type) = i16(0)
  487. Constant *getZeroShadow(Type *OrigTy);
  488. /// Returns a zero constant with the shadow type of V's type.
  489. Constant *getZeroShadow(Value *V);
  490. /// Checks if V is a zero shadow.
  491. bool isZeroShadow(Value *V);
  492. /// Returns the shadow type of OrigTy.
  493. ///
  494. /// getShadowTy({T1,T2,...}) = {getShadowTy(T1),getShadowTy(T2),...}
  495. /// getShadowTy([n x T]) = [n x getShadowTy(T)]
  496. /// getShadowTy(other type) = i16
  497. Type *getShadowTy(Type *OrigTy);
  498. /// Returns the shadow type of of V's type.
  499. Type *getShadowTy(Value *V);
  500. const uint64_t NumOfElementsInArgOrgTLS = ArgTLSSize / OriginWidthBytes;
  501. public:
  502. DataFlowSanitizer(const std::vector<std::string> &ABIListFiles);
  503. bool runImpl(Module &M,
  504. llvm::function_ref<TargetLibraryInfo &(Function &)> GetTLI);
  505. };
  506. struct DFSanFunction {
  507. DataFlowSanitizer &DFS;
  508. Function *F;
  509. DominatorTree DT;
  510. bool IsNativeABI;
  511. bool IsForceZeroLabels;
  512. TargetLibraryInfo &TLI;
  513. AllocaInst *LabelReturnAlloca = nullptr;
  514. AllocaInst *OriginReturnAlloca = nullptr;
  515. DenseMap<Value *, Value *> ValShadowMap;
  516. DenseMap<Value *, Value *> ValOriginMap;
  517. DenseMap<AllocaInst *, AllocaInst *> AllocaShadowMap;
  518. DenseMap<AllocaInst *, AllocaInst *> AllocaOriginMap;
  519. struct PHIFixupElement {
  520. PHINode *Phi;
  521. PHINode *ShadowPhi;
  522. PHINode *OriginPhi;
  523. };
  524. std::vector<PHIFixupElement> PHIFixups;
  525. DenseSet<Instruction *> SkipInsts;
  526. std::vector<Value *> NonZeroChecks;
  527. struct CachedShadow {
  528. BasicBlock *Block; // The block where Shadow is defined.
  529. Value *Shadow;
  530. };
  531. /// Maps a value to its latest shadow value in terms of domination tree.
  532. DenseMap<std::pair<Value *, Value *>, CachedShadow> CachedShadows;
  533. /// Maps a value to its latest collapsed shadow value it was converted to in
  534. /// terms of domination tree. When ClDebugNonzeroLabels is on, this cache is
  535. /// used at a post process where CFG blocks are split. So it does not cache
  536. /// BasicBlock like CachedShadows, but uses domination between values.
  537. DenseMap<Value *, Value *> CachedCollapsedShadows;
  538. DenseMap<Value *, std::set<Value *>> ShadowElements;
  539. DFSanFunction(DataFlowSanitizer &DFS, Function *F, bool IsNativeABI,
  540. bool IsForceZeroLabels, TargetLibraryInfo &TLI)
  541. : DFS(DFS), F(F), IsNativeABI(IsNativeABI),
  542. IsForceZeroLabels(IsForceZeroLabels), TLI(TLI) {
  543. DT.recalculate(*F);
  544. }
  545. /// Computes the shadow address for a given function argument.
  546. ///
  547. /// Shadow = ArgTLS+ArgOffset.
  548. Value *getArgTLS(Type *T, unsigned ArgOffset, IRBuilder<> &IRB);
  549. /// Computes the shadow address for a return value.
  550. Value *getRetvalTLS(Type *T, IRBuilder<> &IRB);
  551. /// Computes the origin address for a given function argument.
  552. ///
  553. /// Origin = ArgOriginTLS[ArgNo].
  554. Value *getArgOriginTLS(unsigned ArgNo, IRBuilder<> &IRB);
  555. /// Computes the origin address for a return value.
  556. Value *getRetvalOriginTLS();
  557. Value *getOrigin(Value *V);
  558. void setOrigin(Instruction *I, Value *Origin);
  559. /// Generates IR to compute the origin of the last operand with a taint label.
  560. Value *combineOperandOrigins(Instruction *Inst);
  561. /// Before the instruction Pos, generates IR to compute the last origin with a
  562. /// taint label. Labels and origins are from vectors Shadows and Origins
  563. /// correspondingly. The generated IR is like
  564. /// Sn-1 != Zero ? On-1: ... S2 != Zero ? O2: S1 != Zero ? O1: O0
  565. /// When Zero is nullptr, it uses ZeroPrimitiveShadow. Otherwise it can be
  566. /// zeros with other bitwidths.
  567. Value *combineOrigins(const std::vector<Value *> &Shadows,
  568. const std::vector<Value *> &Origins, Instruction *Pos,
  569. ConstantInt *Zero = nullptr);
  570. Value *getShadow(Value *V);
  571. void setShadow(Instruction *I, Value *Shadow);
  572. /// Generates IR to compute the union of the two given shadows, inserting it
  573. /// before Pos. The combined value is with primitive type.
  574. Value *combineShadows(Value *V1, Value *V2, Instruction *Pos);
  575. /// Combines the shadow values of V1 and V2, then converts the combined value
  576. /// with primitive type into a shadow value with the original type T.
  577. Value *combineShadowsThenConvert(Type *T, Value *V1, Value *V2,
  578. Instruction *Pos);
  579. Value *combineOperandShadows(Instruction *Inst);
  580. /// Generates IR to load shadow and origin corresponding to bytes [\p
  581. /// Addr, \p Addr + \p Size), where addr has alignment \p
  582. /// InstAlignment, and take the union of each of those shadows. The returned
  583. /// shadow always has primitive type.
  584. ///
  585. /// When tracking loads is enabled, the returned origin is a chain at the
  586. /// current stack if the returned shadow is tainted.
  587. std::pair<Value *, Value *> loadShadowOrigin(Value *Addr, uint64_t Size,
  588. Align InstAlignment,
  589. Instruction *Pos);
  590. void storePrimitiveShadowOrigin(Value *Addr, uint64_t Size,
  591. Align InstAlignment, Value *PrimitiveShadow,
  592. Value *Origin, Instruction *Pos);
  593. /// Applies PrimitiveShadow to all primitive subtypes of T, returning
  594. /// the expanded shadow value.
  595. ///
  596. /// EFP({T1,T2, ...}, PS) = {EFP(T1,PS),EFP(T2,PS),...}
  597. /// EFP([n x T], PS) = [n x EFP(T,PS)]
  598. /// EFP(other types, PS) = PS
  599. Value *expandFromPrimitiveShadow(Type *T, Value *PrimitiveShadow,
  600. Instruction *Pos);
  601. /// Collapses Shadow into a single primitive shadow value, unioning all
  602. /// primitive shadow values in the process. Returns the final primitive
  603. /// shadow value.
  604. ///
  605. /// CTP({V1,V2, ...}) = UNION(CFP(V1,PS),CFP(V2,PS),...)
  606. /// CTP([V1,V2,...]) = UNION(CFP(V1,PS),CFP(V2,PS),...)
  607. /// CTP(other types, PS) = PS
  608. Value *collapseToPrimitiveShadow(Value *Shadow, Instruction *Pos);
  609. void storeZeroPrimitiveShadow(Value *Addr, uint64_t Size, Align ShadowAlign,
  610. Instruction *Pos);
  611. Align getShadowAlign(Align InstAlignment);
  612. // If ClConditionalCallbacks is enabled, insert a callback after a given
  613. // branch instruction using the given conditional expression.
  614. void addConditionalCallbacksIfEnabled(Instruction &I, Value *Condition);
  615. // If ClReachesFunctionCallbacks is enabled, insert a callback for each
  616. // argument and load instruction.
  617. void addReachesFunctionCallbacksIfEnabled(IRBuilder<> &IRB, Instruction &I,
  618. Value *Data);
  619. bool isLookupTableConstant(Value *P);
  620. private:
  621. /// Collapses the shadow with aggregate type into a single primitive shadow
  622. /// value.
  623. template <class AggregateType>
  624. Value *collapseAggregateShadow(AggregateType *AT, Value *Shadow,
  625. IRBuilder<> &IRB);
  626. Value *collapseToPrimitiveShadow(Value *Shadow, IRBuilder<> &IRB);
  627. /// Returns the shadow value of an argument A.
  628. Value *getShadowForTLSArgument(Argument *A);
  629. /// The fast path of loading shadows.
  630. std::pair<Value *, Value *>
  631. loadShadowFast(Value *ShadowAddr, Value *OriginAddr, uint64_t Size,
  632. Align ShadowAlign, Align OriginAlign, Value *FirstOrigin,
  633. Instruction *Pos);
  634. Align getOriginAlign(Align InstAlignment);
  635. /// Because 4 contiguous bytes share one 4-byte origin, the most accurate load
  636. /// is __dfsan_load_label_and_origin. This function returns the union of all
  637. /// labels and the origin of the first taint label. However this is an
  638. /// additional call with many instructions. To ensure common cases are fast,
  639. /// checks if it is possible to load labels and origins without using the
  640. /// callback function.
  641. ///
  642. /// When enabling tracking load instructions, we always use
  643. /// __dfsan_load_label_and_origin to reduce code size.
  644. bool useCallbackLoadLabelAndOrigin(uint64_t Size, Align InstAlignment);
  645. /// Returns a chain at the current stack with previous origin V.
  646. Value *updateOrigin(Value *V, IRBuilder<> &IRB);
  647. /// Returns a chain at the current stack with previous origin V if Shadow is
  648. /// tainted.
  649. Value *updateOriginIfTainted(Value *Shadow, Value *Origin, IRBuilder<> &IRB);
  650. /// Creates an Intptr = Origin | Origin << 32 if Intptr's size is 64. Returns
  651. /// Origin otherwise.
  652. Value *originToIntptr(IRBuilder<> &IRB, Value *Origin);
  653. /// Stores Origin into the address range [StoreOriginAddr, StoreOriginAddr +
  654. /// Size).
  655. void paintOrigin(IRBuilder<> &IRB, Value *Origin, Value *StoreOriginAddr,
  656. uint64_t StoreOriginSize, Align Alignment);
  657. /// Stores Origin in terms of its Shadow value.
  658. /// * Do not write origins for zero shadows because we do not trace origins
  659. /// for untainted sinks.
  660. /// * Use __dfsan_maybe_store_origin if there are too many origin store
  661. /// instrumentations.
  662. void storeOrigin(Instruction *Pos, Value *Addr, uint64_t Size, Value *Shadow,
  663. Value *Origin, Value *StoreOriginAddr, Align InstAlignment);
  664. /// Convert a scalar value to an i1 by comparing with 0.
  665. Value *convertToBool(Value *V, IRBuilder<> &IRB, const Twine &Name = "");
  666. bool shouldInstrumentWithCall();
  667. /// Generates IR to load shadow and origin corresponding to bytes [\p
  668. /// Addr, \p Addr + \p Size), where addr has alignment \p
  669. /// InstAlignment, and take the union of each of those shadows. The returned
  670. /// shadow always has primitive type.
  671. std::pair<Value *, Value *>
  672. loadShadowOriginSansLoadTracking(Value *Addr, uint64_t Size,
  673. Align InstAlignment, Instruction *Pos);
  674. int NumOriginStores = 0;
  675. };
  676. class DFSanVisitor : public InstVisitor<DFSanVisitor> {
  677. public:
  678. DFSanFunction &DFSF;
  679. DFSanVisitor(DFSanFunction &DFSF) : DFSF(DFSF) {}
  680. const DataLayout &getDataLayout() const {
  681. return DFSF.F->getParent()->getDataLayout();
  682. }
  683. // Combines shadow values and origins for all of I's operands.
  684. void visitInstOperands(Instruction &I);
  685. void visitUnaryOperator(UnaryOperator &UO);
  686. void visitBinaryOperator(BinaryOperator &BO);
  687. void visitBitCastInst(BitCastInst &BCI);
  688. void visitCastInst(CastInst &CI);
  689. void visitCmpInst(CmpInst &CI);
  690. void visitLandingPadInst(LandingPadInst &LPI);
  691. void visitGetElementPtrInst(GetElementPtrInst &GEPI);
  692. void visitLoadInst(LoadInst &LI);
  693. void visitStoreInst(StoreInst &SI);
  694. void visitAtomicRMWInst(AtomicRMWInst &I);
  695. void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I);
  696. void visitReturnInst(ReturnInst &RI);
  697. void visitLibAtomicLoad(CallBase &CB);
  698. void visitLibAtomicStore(CallBase &CB);
  699. void visitLibAtomicExchange(CallBase &CB);
  700. void visitLibAtomicCompareExchange(CallBase &CB);
  701. void visitCallBase(CallBase &CB);
  702. void visitPHINode(PHINode &PN);
  703. void visitExtractElementInst(ExtractElementInst &I);
  704. void visitInsertElementInst(InsertElementInst &I);
  705. void visitShuffleVectorInst(ShuffleVectorInst &I);
  706. void visitExtractValueInst(ExtractValueInst &I);
  707. void visitInsertValueInst(InsertValueInst &I);
  708. void visitAllocaInst(AllocaInst &I);
  709. void visitSelectInst(SelectInst &I);
  710. void visitMemSetInst(MemSetInst &I);
  711. void visitMemTransferInst(MemTransferInst &I);
  712. void visitBranchInst(BranchInst &BR);
  713. void visitSwitchInst(SwitchInst &SW);
  714. private:
  715. void visitCASOrRMW(Align InstAlignment, Instruction &I);
  716. // Returns false when this is an invoke of a custom function.
  717. bool visitWrappedCallBase(Function &F, CallBase &CB);
  718. // Combines origins for all of I's operands.
  719. void visitInstOperandOrigins(Instruction &I);
  720. void addShadowArguments(Function &F, CallBase &CB, std::vector<Value *> &Args,
  721. IRBuilder<> &IRB);
  722. void addOriginArguments(Function &F, CallBase &CB, std::vector<Value *> &Args,
  723. IRBuilder<> &IRB);
  724. Value *makeAddAcquireOrderingTable(IRBuilder<> &IRB);
  725. Value *makeAddReleaseOrderingTable(IRBuilder<> &IRB);
  726. };
  727. bool LibAtomicFunction(const Function &F) {
  728. // This is a bit of a hack because TargetLibraryInfo is a function pass.
  729. // The DFSan pass would need to be refactored to be function pass oriented
  730. // (like MSan is) in order to fit together nicely with TargetLibraryInfo.
  731. // We need this check to prevent them from being instrumented, or wrapped.
  732. // Match on name and number of arguments.
  733. if (!F.hasName() || F.isVarArg())
  734. return false;
  735. switch (F.arg_size()) {
  736. case 4:
  737. return F.getName() == "__atomic_load" || F.getName() == "__atomic_store";
  738. case 5:
  739. return F.getName() == "__atomic_exchange";
  740. case 6:
  741. return F.getName() == "__atomic_compare_exchange";
  742. default:
  743. return false;
  744. }
  745. }
  746. } // end anonymous namespace
  747. DataFlowSanitizer::DataFlowSanitizer(
  748. const std::vector<std::string> &ABIListFiles) {
  749. std::vector<std::string> AllABIListFiles(std::move(ABIListFiles));
  750. llvm::append_range(AllABIListFiles, ClABIListFiles);
  751. // FIXME: should we propagate vfs::FileSystem to this constructor?
  752. ABIList.set(
  753. SpecialCaseList::createOrDie(AllABIListFiles, *vfs::getRealFileSystem()));
  754. for (StringRef v : ClCombineTaintLookupTables)
  755. CombineTaintLookupTableNames.insert(v);
  756. }
  757. TransformedFunction DataFlowSanitizer::getCustomFunctionType(FunctionType *T) {
  758. SmallVector<Type *, 4> ArgTypes;
  759. // Some parameters of the custom function being constructed are
  760. // parameters of T. Record the mapping from parameters of T to
  761. // parameters of the custom function, so that parameter attributes
  762. // at call sites can be updated.
  763. std::vector<unsigned> ArgumentIndexMapping;
  764. for (unsigned I = 0, E = T->getNumParams(); I != E; ++I) {
  765. Type *ParamType = T->getParamType(I);
  766. ArgumentIndexMapping.push_back(ArgTypes.size());
  767. ArgTypes.push_back(ParamType);
  768. }
  769. for (unsigned I = 0, E = T->getNumParams(); I != E; ++I)
  770. ArgTypes.push_back(PrimitiveShadowTy);
  771. if (T->isVarArg())
  772. ArgTypes.push_back(PrimitiveShadowPtrTy);
  773. Type *RetType = T->getReturnType();
  774. if (!RetType->isVoidTy())
  775. ArgTypes.push_back(PrimitiveShadowPtrTy);
  776. if (shouldTrackOrigins()) {
  777. for (unsigned I = 0, E = T->getNumParams(); I != E; ++I)
  778. ArgTypes.push_back(OriginTy);
  779. if (T->isVarArg())
  780. ArgTypes.push_back(OriginPtrTy);
  781. if (!RetType->isVoidTy())
  782. ArgTypes.push_back(OriginPtrTy);
  783. }
  784. return TransformedFunction(
  785. T, FunctionType::get(T->getReturnType(), ArgTypes, T->isVarArg()),
  786. ArgumentIndexMapping);
  787. }
  788. bool DataFlowSanitizer::isZeroShadow(Value *V) {
  789. Type *T = V->getType();
  790. if (!isa<ArrayType>(T) && !isa<StructType>(T)) {
  791. if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
  792. return CI->isZero();
  793. return false;
  794. }
  795. return isa<ConstantAggregateZero>(V);
  796. }
  797. bool DataFlowSanitizer::hasLoadSizeForFastPath(uint64_t Size) {
  798. uint64_t ShadowSize = Size * ShadowWidthBytes;
  799. return ShadowSize % 8 == 0 || ShadowSize == 4;
  800. }
  801. bool DataFlowSanitizer::shouldTrackOrigins() {
  802. static const bool ShouldTrackOrigins = ClTrackOrigins;
  803. return ShouldTrackOrigins;
  804. }
  805. Constant *DataFlowSanitizer::getZeroShadow(Type *OrigTy) {
  806. if (!isa<ArrayType>(OrigTy) && !isa<StructType>(OrigTy))
  807. return ZeroPrimitiveShadow;
  808. Type *ShadowTy = getShadowTy(OrigTy);
  809. return ConstantAggregateZero::get(ShadowTy);
  810. }
  811. Constant *DataFlowSanitizer::getZeroShadow(Value *V) {
  812. return getZeroShadow(V->getType());
  813. }
  814. static Value *expandFromPrimitiveShadowRecursive(
  815. Value *Shadow, SmallVector<unsigned, 4> &Indices, Type *SubShadowTy,
  816. Value *PrimitiveShadow, IRBuilder<> &IRB) {
  817. if (!isa<ArrayType>(SubShadowTy) && !isa<StructType>(SubShadowTy))
  818. return IRB.CreateInsertValue(Shadow, PrimitiveShadow, Indices);
  819. if (ArrayType *AT = dyn_cast<ArrayType>(SubShadowTy)) {
  820. for (unsigned Idx = 0; Idx < AT->getNumElements(); Idx++) {
  821. Indices.push_back(Idx);
  822. Shadow = expandFromPrimitiveShadowRecursive(
  823. Shadow, Indices, AT->getElementType(), PrimitiveShadow, IRB);
  824. Indices.pop_back();
  825. }
  826. return Shadow;
  827. }
  828. if (StructType *ST = dyn_cast<StructType>(SubShadowTy)) {
  829. for (unsigned Idx = 0; Idx < ST->getNumElements(); Idx++) {
  830. Indices.push_back(Idx);
  831. Shadow = expandFromPrimitiveShadowRecursive(
  832. Shadow, Indices, ST->getElementType(Idx), PrimitiveShadow, IRB);
  833. Indices.pop_back();
  834. }
  835. return Shadow;
  836. }
  837. llvm_unreachable("Unexpected shadow type");
  838. }
  839. bool DFSanFunction::shouldInstrumentWithCall() {
  840. return ClInstrumentWithCallThreshold >= 0 &&
  841. NumOriginStores >= ClInstrumentWithCallThreshold;
  842. }
  843. Value *DFSanFunction::expandFromPrimitiveShadow(Type *T, Value *PrimitiveShadow,
  844. Instruction *Pos) {
  845. Type *ShadowTy = DFS.getShadowTy(T);
  846. if (!isa<ArrayType>(ShadowTy) && !isa<StructType>(ShadowTy))
  847. return PrimitiveShadow;
  848. if (DFS.isZeroShadow(PrimitiveShadow))
  849. return DFS.getZeroShadow(ShadowTy);
  850. IRBuilder<> IRB(Pos);
  851. SmallVector<unsigned, 4> Indices;
  852. Value *Shadow = UndefValue::get(ShadowTy);
  853. Shadow = expandFromPrimitiveShadowRecursive(Shadow, Indices, ShadowTy,
  854. PrimitiveShadow, IRB);
  855. // Caches the primitive shadow value that built the shadow value.
  856. CachedCollapsedShadows[Shadow] = PrimitiveShadow;
  857. return Shadow;
  858. }
  859. template <class AggregateType>
  860. Value *DFSanFunction::collapseAggregateShadow(AggregateType *AT, Value *Shadow,
  861. IRBuilder<> &IRB) {
  862. if (!AT->getNumElements())
  863. return DFS.ZeroPrimitiveShadow;
  864. Value *FirstItem = IRB.CreateExtractValue(Shadow, 0);
  865. Value *Aggregator = collapseToPrimitiveShadow(FirstItem, IRB);
  866. for (unsigned Idx = 1; Idx < AT->getNumElements(); Idx++) {
  867. Value *ShadowItem = IRB.CreateExtractValue(Shadow, Idx);
  868. Value *ShadowInner = collapseToPrimitiveShadow(ShadowItem, IRB);
  869. Aggregator = IRB.CreateOr(Aggregator, ShadowInner);
  870. }
  871. return Aggregator;
  872. }
  873. Value *DFSanFunction::collapseToPrimitiveShadow(Value *Shadow,
  874. IRBuilder<> &IRB) {
  875. Type *ShadowTy = Shadow->getType();
  876. if (!isa<ArrayType>(ShadowTy) && !isa<StructType>(ShadowTy))
  877. return Shadow;
  878. if (ArrayType *AT = dyn_cast<ArrayType>(ShadowTy))
  879. return collapseAggregateShadow<>(AT, Shadow, IRB);
  880. if (StructType *ST = dyn_cast<StructType>(ShadowTy))
  881. return collapseAggregateShadow<>(ST, Shadow, IRB);
  882. llvm_unreachable("Unexpected shadow type");
  883. }
  884. Value *DFSanFunction::collapseToPrimitiveShadow(Value *Shadow,
  885. Instruction *Pos) {
  886. Type *ShadowTy = Shadow->getType();
  887. if (!isa<ArrayType>(ShadowTy) && !isa<StructType>(ShadowTy))
  888. return Shadow;
  889. // Checks if the cached collapsed shadow value dominates Pos.
  890. Value *&CS = CachedCollapsedShadows[Shadow];
  891. if (CS && DT.dominates(CS, Pos))
  892. return CS;
  893. IRBuilder<> IRB(Pos);
  894. Value *PrimitiveShadow = collapseToPrimitiveShadow(Shadow, IRB);
  895. // Caches the converted primitive shadow value.
  896. CS = PrimitiveShadow;
  897. return PrimitiveShadow;
  898. }
  899. void DFSanFunction::addConditionalCallbacksIfEnabled(Instruction &I,
  900. Value *Condition) {
  901. if (!ClConditionalCallbacks) {
  902. return;
  903. }
  904. IRBuilder<> IRB(&I);
  905. Value *CondShadow = getShadow(Condition);
  906. CallInst *CI;
  907. if (DFS.shouldTrackOrigins()) {
  908. Value *CondOrigin = getOrigin(Condition);
  909. CI = IRB.CreateCall(DFS.DFSanConditionalCallbackOriginFn,
  910. {CondShadow, CondOrigin});
  911. } else {
  912. CI = IRB.CreateCall(DFS.DFSanConditionalCallbackFn, {CondShadow});
  913. }
  914. CI->addParamAttr(0, Attribute::ZExt);
  915. }
  916. void DFSanFunction::addReachesFunctionCallbacksIfEnabled(IRBuilder<> &IRB,
  917. Instruction &I,
  918. Value *Data) {
  919. if (!ClReachesFunctionCallbacks) {
  920. return;
  921. }
  922. const DebugLoc &dbgloc = I.getDebugLoc();
  923. Value *DataShadow = collapseToPrimitiveShadow(getShadow(Data), IRB);
  924. ConstantInt *CILine;
  925. llvm::Value *FilePathPtr;
  926. if (dbgloc.get() == nullptr) {
  927. CILine = llvm::ConstantInt::get(I.getContext(), llvm::APInt(32, 0));
  928. FilePathPtr = IRB.CreateGlobalStringPtr(
  929. I.getFunction()->getParent()->getSourceFileName());
  930. } else {
  931. CILine = llvm::ConstantInt::get(I.getContext(),
  932. llvm::APInt(32, dbgloc.getLine()));
  933. FilePathPtr =
  934. IRB.CreateGlobalStringPtr(dbgloc->getFilename());
  935. }
  936. llvm::Value *FunctionNamePtr =
  937. IRB.CreateGlobalStringPtr(I.getFunction()->getName());
  938. CallInst *CB;
  939. std::vector<Value *> args;
  940. if (DFS.shouldTrackOrigins()) {
  941. Value *DataOrigin = getOrigin(Data);
  942. args = { DataShadow, DataOrigin, FilePathPtr, CILine, FunctionNamePtr };
  943. CB = IRB.CreateCall(DFS.DFSanReachesFunctionCallbackOriginFn, args);
  944. } else {
  945. args = { DataShadow, FilePathPtr, CILine, FunctionNamePtr };
  946. CB = IRB.CreateCall(DFS.DFSanReachesFunctionCallbackFn, args);
  947. }
  948. CB->addParamAttr(0, Attribute::ZExt);
  949. CB->setDebugLoc(dbgloc);
  950. }
  951. Type *DataFlowSanitizer::getShadowTy(Type *OrigTy) {
  952. if (!OrigTy->isSized())
  953. return PrimitiveShadowTy;
  954. if (isa<IntegerType>(OrigTy))
  955. return PrimitiveShadowTy;
  956. if (isa<VectorType>(OrigTy))
  957. return PrimitiveShadowTy;
  958. if (ArrayType *AT = dyn_cast<ArrayType>(OrigTy))
  959. return ArrayType::get(getShadowTy(AT->getElementType()),
  960. AT->getNumElements());
  961. if (StructType *ST = dyn_cast<StructType>(OrigTy)) {
  962. SmallVector<Type *, 4> Elements;
  963. for (unsigned I = 0, N = ST->getNumElements(); I < N; ++I)
  964. Elements.push_back(getShadowTy(ST->getElementType(I)));
  965. return StructType::get(*Ctx, Elements);
  966. }
  967. return PrimitiveShadowTy;
  968. }
  969. Type *DataFlowSanitizer::getShadowTy(Value *V) {
  970. return getShadowTy(V->getType());
  971. }
  972. bool DataFlowSanitizer::initializeModule(Module &M) {
  973. Triple TargetTriple(M.getTargetTriple());
  974. const DataLayout &DL = M.getDataLayout();
  975. if (TargetTriple.getOS() != Triple::Linux)
  976. report_fatal_error("unsupported operating system");
  977. switch (TargetTriple.getArch()) {
  978. case Triple::aarch64:
  979. MapParams = &Linux_AArch64_MemoryMapParams;
  980. break;
  981. case Triple::x86_64:
  982. MapParams = &Linux_X86_64_MemoryMapParams;
  983. break;
  984. default:
  985. report_fatal_error("unsupported architecture");
  986. }
  987. Mod = &M;
  988. Ctx = &M.getContext();
  989. Int8Ptr = Type::getInt8PtrTy(*Ctx);
  990. OriginTy = IntegerType::get(*Ctx, OriginWidthBits);
  991. OriginPtrTy = PointerType::getUnqual(OriginTy);
  992. PrimitiveShadowTy = IntegerType::get(*Ctx, ShadowWidthBits);
  993. PrimitiveShadowPtrTy = PointerType::getUnqual(PrimitiveShadowTy);
  994. IntptrTy = DL.getIntPtrType(*Ctx);
  995. ZeroPrimitiveShadow = ConstantInt::getSigned(PrimitiveShadowTy, 0);
  996. ZeroOrigin = ConstantInt::getSigned(OriginTy, 0);
  997. Type *DFSanUnionLoadArgs[2] = {PrimitiveShadowPtrTy, IntptrTy};
  998. DFSanUnionLoadFnTy = FunctionType::get(PrimitiveShadowTy, DFSanUnionLoadArgs,
  999. /*isVarArg=*/false);
  1000. Type *DFSanLoadLabelAndOriginArgs[2] = {Int8Ptr, IntptrTy};
  1001. DFSanLoadLabelAndOriginFnTy =
  1002. FunctionType::get(IntegerType::get(*Ctx, 64), DFSanLoadLabelAndOriginArgs,
  1003. /*isVarArg=*/false);
  1004. DFSanUnimplementedFnTy = FunctionType::get(
  1005. Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false);
  1006. Type *DFSanWrapperExternWeakNullArgs[2] = {Int8Ptr, Int8Ptr};
  1007. DFSanWrapperExternWeakNullFnTy =
  1008. FunctionType::get(Type::getVoidTy(*Ctx), DFSanWrapperExternWeakNullArgs,
  1009. /*isVarArg=*/false);
  1010. Type *DFSanSetLabelArgs[4] = {PrimitiveShadowTy, OriginTy,
  1011. Type::getInt8PtrTy(*Ctx), IntptrTy};
  1012. DFSanSetLabelFnTy = FunctionType::get(Type::getVoidTy(*Ctx),
  1013. DFSanSetLabelArgs, /*isVarArg=*/false);
  1014. DFSanNonzeroLabelFnTy = FunctionType::get(Type::getVoidTy(*Ctx), std::nullopt,
  1015. /*isVarArg=*/false);
  1016. DFSanVarargWrapperFnTy = FunctionType::get(
  1017. Type::getVoidTy(*Ctx), Type::getInt8PtrTy(*Ctx), /*isVarArg=*/false);
  1018. DFSanConditionalCallbackFnTy =
  1019. FunctionType::get(Type::getVoidTy(*Ctx), PrimitiveShadowTy,
  1020. /*isVarArg=*/false);
  1021. Type *DFSanConditionalCallbackOriginArgs[2] = {PrimitiveShadowTy, OriginTy};
  1022. DFSanConditionalCallbackOriginFnTy = FunctionType::get(
  1023. Type::getVoidTy(*Ctx), DFSanConditionalCallbackOriginArgs,
  1024. /*isVarArg=*/false);
  1025. Type *DFSanReachesFunctionCallbackArgs[4] = {PrimitiveShadowTy, Int8Ptr,
  1026. OriginTy, Int8Ptr};
  1027. DFSanReachesFunctionCallbackFnTy =
  1028. FunctionType::get(Type::getVoidTy(*Ctx), DFSanReachesFunctionCallbackArgs,
  1029. /*isVarArg=*/false);
  1030. Type *DFSanReachesFunctionCallbackOriginArgs[5] = {
  1031. PrimitiveShadowTy, OriginTy, Int8Ptr, OriginTy, Int8Ptr};
  1032. DFSanReachesFunctionCallbackOriginFnTy = FunctionType::get(
  1033. Type::getVoidTy(*Ctx), DFSanReachesFunctionCallbackOriginArgs,
  1034. /*isVarArg=*/false);
  1035. DFSanCmpCallbackFnTy =
  1036. FunctionType::get(Type::getVoidTy(*Ctx), PrimitiveShadowTy,
  1037. /*isVarArg=*/false);
  1038. DFSanChainOriginFnTy =
  1039. FunctionType::get(OriginTy, OriginTy, /*isVarArg=*/false);
  1040. Type *DFSanChainOriginIfTaintedArgs[2] = {PrimitiveShadowTy, OriginTy};
  1041. DFSanChainOriginIfTaintedFnTy = FunctionType::get(
  1042. OriginTy, DFSanChainOriginIfTaintedArgs, /*isVarArg=*/false);
  1043. Type *DFSanMaybeStoreOriginArgs[4] = {IntegerType::get(*Ctx, ShadowWidthBits),
  1044. Int8Ptr, IntptrTy, OriginTy};
  1045. DFSanMaybeStoreOriginFnTy = FunctionType::get(
  1046. Type::getVoidTy(*Ctx), DFSanMaybeStoreOriginArgs, /*isVarArg=*/false);
  1047. Type *DFSanMemOriginTransferArgs[3] = {Int8Ptr, Int8Ptr, IntptrTy};
  1048. DFSanMemOriginTransferFnTy = FunctionType::get(
  1049. Type::getVoidTy(*Ctx), DFSanMemOriginTransferArgs, /*isVarArg=*/false);
  1050. Type *DFSanMemShadowOriginTransferArgs[3] = {Int8Ptr, Int8Ptr, IntptrTy};
  1051. DFSanMemShadowOriginTransferFnTy =
  1052. FunctionType::get(Type::getVoidTy(*Ctx), DFSanMemShadowOriginTransferArgs,
  1053. /*isVarArg=*/false);
  1054. Type *DFSanMemShadowOriginConditionalExchangeArgs[5] = {
  1055. IntegerType::get(*Ctx, 8), Int8Ptr, Int8Ptr, Int8Ptr, IntptrTy};
  1056. DFSanMemShadowOriginConditionalExchangeFnTy = FunctionType::get(
  1057. Type::getVoidTy(*Ctx), DFSanMemShadowOriginConditionalExchangeArgs,
  1058. /*isVarArg=*/false);
  1059. Type *DFSanLoadStoreCallbackArgs[2] = {PrimitiveShadowTy, Int8Ptr};
  1060. DFSanLoadStoreCallbackFnTy =
  1061. FunctionType::get(Type::getVoidTy(*Ctx), DFSanLoadStoreCallbackArgs,
  1062. /*isVarArg=*/false);
  1063. Type *DFSanMemTransferCallbackArgs[2] = {PrimitiveShadowPtrTy, IntptrTy};
  1064. DFSanMemTransferCallbackFnTy =
  1065. FunctionType::get(Type::getVoidTy(*Ctx), DFSanMemTransferCallbackArgs,
  1066. /*isVarArg=*/false);
  1067. ColdCallWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
  1068. OriginStoreWeights = MDBuilder(*Ctx).createBranchWeights(1, 1000);
  1069. return true;
  1070. }
  1071. bool DataFlowSanitizer::isInstrumented(const Function *F) {
  1072. return !ABIList.isIn(*F, "uninstrumented");
  1073. }
  1074. bool DataFlowSanitizer::isInstrumented(const GlobalAlias *GA) {
  1075. return !ABIList.isIn(*GA, "uninstrumented");
  1076. }
  1077. bool DataFlowSanitizer::isForceZeroLabels(const Function *F) {
  1078. return ABIList.isIn(*F, "force_zero_labels");
  1079. }
  1080. DataFlowSanitizer::WrapperKind DataFlowSanitizer::getWrapperKind(Function *F) {
  1081. if (ABIList.isIn(*F, "functional"))
  1082. return WK_Functional;
  1083. if (ABIList.isIn(*F, "discard"))
  1084. return WK_Discard;
  1085. if (ABIList.isIn(*F, "custom"))
  1086. return WK_Custom;
  1087. return WK_Warning;
  1088. }
  1089. void DataFlowSanitizer::addGlobalNameSuffix(GlobalValue *GV) {
  1090. std::string GVName = std::string(GV->getName()), Suffix = ".dfsan";
  1091. GV->setName(GVName + Suffix);
  1092. // Try to change the name of the function in module inline asm. We only do
  1093. // this for specific asm directives, currently only ".symver", to try to avoid
  1094. // corrupting asm which happens to contain the symbol name as a substring.
  1095. // Note that the substitution for .symver assumes that the versioned symbol
  1096. // also has an instrumented name.
  1097. std::string Asm = GV->getParent()->getModuleInlineAsm();
  1098. std::string SearchStr = ".symver " + GVName + ",";
  1099. size_t Pos = Asm.find(SearchStr);
  1100. if (Pos != std::string::npos) {
  1101. Asm.replace(Pos, SearchStr.size(), ".symver " + GVName + Suffix + ",");
  1102. Pos = Asm.find("@");
  1103. if (Pos == std::string::npos)
  1104. report_fatal_error(Twine("unsupported .symver: ", Asm));
  1105. Asm.replace(Pos, 1, Suffix + "@");
  1106. GV->getParent()->setModuleInlineAsm(Asm);
  1107. }
  1108. }
  1109. void DataFlowSanitizer::buildExternWeakCheckIfNeeded(IRBuilder<> &IRB,
  1110. Function *F) {
  1111. // If the function we are wrapping was ExternWeak, it may be null.
  1112. // The original code before calling this wrapper may have checked for null,
  1113. // but replacing with a known-to-not-be-null wrapper can break this check.
  1114. // When replacing uses of the extern weak function with the wrapper we try
  1115. // to avoid replacing uses in conditionals, but this is not perfect.
  1116. // In the case where we fail, and accidentally optimize out a null check
  1117. // for a extern weak function, add a check here to help identify the issue.
  1118. if (GlobalValue::isExternalWeakLinkage(F->getLinkage())) {
  1119. std::vector<Value *> Args;
  1120. Args.push_back(IRB.CreatePointerCast(F, IRB.getInt8PtrTy()));
  1121. Args.push_back(IRB.CreateGlobalStringPtr(F->getName()));
  1122. IRB.CreateCall(DFSanWrapperExternWeakNullFn, Args);
  1123. }
  1124. }
  1125. Function *
  1126. DataFlowSanitizer::buildWrapperFunction(Function *F, StringRef NewFName,
  1127. GlobalValue::LinkageTypes NewFLink,
  1128. FunctionType *NewFT) {
  1129. FunctionType *FT = F->getFunctionType();
  1130. Function *NewF = Function::Create(NewFT, NewFLink, F->getAddressSpace(),
  1131. NewFName, F->getParent());
  1132. NewF->copyAttributesFrom(F);
  1133. NewF->removeRetAttrs(
  1134. AttributeFuncs::typeIncompatible(NewFT->getReturnType()));
  1135. BasicBlock *BB = BasicBlock::Create(*Ctx, "entry", NewF);
  1136. if (F->isVarArg()) {
  1137. NewF->removeFnAttr("split-stack");
  1138. CallInst::Create(DFSanVarargWrapperFn,
  1139. IRBuilder<>(BB).CreateGlobalStringPtr(F->getName()), "",
  1140. BB);
  1141. new UnreachableInst(*Ctx, BB);
  1142. } else {
  1143. auto ArgIt = pointer_iterator<Argument *>(NewF->arg_begin());
  1144. std::vector<Value *> Args(ArgIt, ArgIt + FT->getNumParams());
  1145. CallInst *CI = CallInst::Create(F, Args, "", BB);
  1146. if (FT->getReturnType()->isVoidTy())
  1147. ReturnInst::Create(*Ctx, BB);
  1148. else
  1149. ReturnInst::Create(*Ctx, CI, BB);
  1150. }
  1151. return NewF;
  1152. }
  1153. // Initialize DataFlowSanitizer runtime functions and declare them in the module
  1154. void DataFlowSanitizer::initializeRuntimeFunctions(Module &M) {
  1155. LLVMContext &C = M.getContext();
  1156. {
  1157. AttributeList AL;
  1158. AL = AL.addFnAttribute(C, Attribute::NoUnwind);
  1159. AL = AL.addFnAttribute(
  1160. C, Attribute::getWithMemoryEffects(C, MemoryEffects::readOnly()));
  1161. AL = AL.addRetAttribute(C, Attribute::ZExt);
  1162. DFSanUnionLoadFn =
  1163. Mod->getOrInsertFunction("__dfsan_union_load", DFSanUnionLoadFnTy, AL);
  1164. }
  1165. {
  1166. AttributeList AL;
  1167. AL = AL.addFnAttribute(C, Attribute::NoUnwind);
  1168. AL = AL.addFnAttribute(
  1169. C, Attribute::getWithMemoryEffects(C, MemoryEffects::readOnly()));
  1170. AL = AL.addRetAttribute(C, Attribute::ZExt);
  1171. DFSanLoadLabelAndOriginFn = Mod->getOrInsertFunction(
  1172. "__dfsan_load_label_and_origin", DFSanLoadLabelAndOriginFnTy, AL);
  1173. }
  1174. DFSanUnimplementedFn =
  1175. Mod->getOrInsertFunction("__dfsan_unimplemented", DFSanUnimplementedFnTy);
  1176. DFSanWrapperExternWeakNullFn = Mod->getOrInsertFunction(
  1177. "__dfsan_wrapper_extern_weak_null", DFSanWrapperExternWeakNullFnTy);
  1178. {
  1179. AttributeList AL;
  1180. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1181. AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt);
  1182. DFSanSetLabelFn =
  1183. Mod->getOrInsertFunction("__dfsan_set_label", DFSanSetLabelFnTy, AL);
  1184. }
  1185. DFSanNonzeroLabelFn =
  1186. Mod->getOrInsertFunction("__dfsan_nonzero_label", DFSanNonzeroLabelFnTy);
  1187. DFSanVarargWrapperFn = Mod->getOrInsertFunction("__dfsan_vararg_wrapper",
  1188. DFSanVarargWrapperFnTy);
  1189. {
  1190. AttributeList AL;
  1191. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1192. AL = AL.addRetAttribute(M.getContext(), Attribute::ZExt);
  1193. DFSanChainOriginFn = Mod->getOrInsertFunction("__dfsan_chain_origin",
  1194. DFSanChainOriginFnTy, AL);
  1195. }
  1196. {
  1197. AttributeList AL;
  1198. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1199. AL = AL.addParamAttribute(M.getContext(), 1, Attribute::ZExt);
  1200. AL = AL.addRetAttribute(M.getContext(), Attribute::ZExt);
  1201. DFSanChainOriginIfTaintedFn = Mod->getOrInsertFunction(
  1202. "__dfsan_chain_origin_if_tainted", DFSanChainOriginIfTaintedFnTy, AL);
  1203. }
  1204. DFSanMemOriginTransferFn = Mod->getOrInsertFunction(
  1205. "__dfsan_mem_origin_transfer", DFSanMemOriginTransferFnTy);
  1206. DFSanMemShadowOriginTransferFn = Mod->getOrInsertFunction(
  1207. "__dfsan_mem_shadow_origin_transfer", DFSanMemShadowOriginTransferFnTy);
  1208. DFSanMemShadowOriginConditionalExchangeFn =
  1209. Mod->getOrInsertFunction("__dfsan_mem_shadow_origin_conditional_exchange",
  1210. DFSanMemShadowOriginConditionalExchangeFnTy);
  1211. {
  1212. AttributeList AL;
  1213. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1214. AL = AL.addParamAttribute(M.getContext(), 3, Attribute::ZExt);
  1215. DFSanMaybeStoreOriginFn = Mod->getOrInsertFunction(
  1216. "__dfsan_maybe_store_origin", DFSanMaybeStoreOriginFnTy, AL);
  1217. }
  1218. DFSanRuntimeFunctions.insert(
  1219. DFSanUnionLoadFn.getCallee()->stripPointerCasts());
  1220. DFSanRuntimeFunctions.insert(
  1221. DFSanLoadLabelAndOriginFn.getCallee()->stripPointerCasts());
  1222. DFSanRuntimeFunctions.insert(
  1223. DFSanUnimplementedFn.getCallee()->stripPointerCasts());
  1224. DFSanRuntimeFunctions.insert(
  1225. DFSanWrapperExternWeakNullFn.getCallee()->stripPointerCasts());
  1226. DFSanRuntimeFunctions.insert(
  1227. DFSanSetLabelFn.getCallee()->stripPointerCasts());
  1228. DFSanRuntimeFunctions.insert(
  1229. DFSanNonzeroLabelFn.getCallee()->stripPointerCasts());
  1230. DFSanRuntimeFunctions.insert(
  1231. DFSanVarargWrapperFn.getCallee()->stripPointerCasts());
  1232. DFSanRuntimeFunctions.insert(
  1233. DFSanLoadCallbackFn.getCallee()->stripPointerCasts());
  1234. DFSanRuntimeFunctions.insert(
  1235. DFSanStoreCallbackFn.getCallee()->stripPointerCasts());
  1236. DFSanRuntimeFunctions.insert(
  1237. DFSanMemTransferCallbackFn.getCallee()->stripPointerCasts());
  1238. DFSanRuntimeFunctions.insert(
  1239. DFSanConditionalCallbackFn.getCallee()->stripPointerCasts());
  1240. DFSanRuntimeFunctions.insert(
  1241. DFSanConditionalCallbackOriginFn.getCallee()->stripPointerCasts());
  1242. DFSanRuntimeFunctions.insert(
  1243. DFSanReachesFunctionCallbackFn.getCallee()->stripPointerCasts());
  1244. DFSanRuntimeFunctions.insert(
  1245. DFSanReachesFunctionCallbackOriginFn.getCallee()->stripPointerCasts());
  1246. DFSanRuntimeFunctions.insert(
  1247. DFSanCmpCallbackFn.getCallee()->stripPointerCasts());
  1248. DFSanRuntimeFunctions.insert(
  1249. DFSanChainOriginFn.getCallee()->stripPointerCasts());
  1250. DFSanRuntimeFunctions.insert(
  1251. DFSanChainOriginIfTaintedFn.getCallee()->stripPointerCasts());
  1252. DFSanRuntimeFunctions.insert(
  1253. DFSanMemOriginTransferFn.getCallee()->stripPointerCasts());
  1254. DFSanRuntimeFunctions.insert(
  1255. DFSanMemShadowOriginTransferFn.getCallee()->stripPointerCasts());
  1256. DFSanRuntimeFunctions.insert(
  1257. DFSanMemShadowOriginConditionalExchangeFn.getCallee()
  1258. ->stripPointerCasts());
  1259. DFSanRuntimeFunctions.insert(
  1260. DFSanMaybeStoreOriginFn.getCallee()->stripPointerCasts());
  1261. }
  1262. // Initializes event callback functions and declare them in the module
  1263. void DataFlowSanitizer::initializeCallbackFunctions(Module &M) {
  1264. {
  1265. AttributeList AL;
  1266. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1267. DFSanLoadCallbackFn = Mod->getOrInsertFunction(
  1268. "__dfsan_load_callback", DFSanLoadStoreCallbackFnTy, AL);
  1269. }
  1270. {
  1271. AttributeList AL;
  1272. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1273. DFSanStoreCallbackFn = Mod->getOrInsertFunction(
  1274. "__dfsan_store_callback", DFSanLoadStoreCallbackFnTy, AL);
  1275. }
  1276. DFSanMemTransferCallbackFn = Mod->getOrInsertFunction(
  1277. "__dfsan_mem_transfer_callback", DFSanMemTransferCallbackFnTy);
  1278. {
  1279. AttributeList AL;
  1280. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1281. DFSanCmpCallbackFn = Mod->getOrInsertFunction("__dfsan_cmp_callback",
  1282. DFSanCmpCallbackFnTy, AL);
  1283. }
  1284. {
  1285. AttributeList AL;
  1286. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1287. DFSanConditionalCallbackFn = Mod->getOrInsertFunction(
  1288. "__dfsan_conditional_callback", DFSanConditionalCallbackFnTy, AL);
  1289. }
  1290. {
  1291. AttributeList AL;
  1292. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1293. DFSanConditionalCallbackOriginFn =
  1294. Mod->getOrInsertFunction("__dfsan_conditional_callback_origin",
  1295. DFSanConditionalCallbackOriginFnTy, AL);
  1296. }
  1297. {
  1298. AttributeList AL;
  1299. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1300. DFSanReachesFunctionCallbackFn =
  1301. Mod->getOrInsertFunction("__dfsan_reaches_function_callback",
  1302. DFSanReachesFunctionCallbackFnTy, AL);
  1303. }
  1304. {
  1305. AttributeList AL;
  1306. AL = AL.addParamAttribute(M.getContext(), 0, Attribute::ZExt);
  1307. DFSanReachesFunctionCallbackOriginFn =
  1308. Mod->getOrInsertFunction("__dfsan_reaches_function_callback_origin",
  1309. DFSanReachesFunctionCallbackOriginFnTy, AL);
  1310. }
  1311. }
  1312. bool DataFlowSanitizer::runImpl(
  1313. Module &M, llvm::function_ref<TargetLibraryInfo &(Function &)> GetTLI) {
  1314. initializeModule(M);
  1315. if (ABIList.isIn(M, "skip"))
  1316. return false;
  1317. const unsigned InitialGlobalSize = M.global_size();
  1318. const unsigned InitialModuleSize = M.size();
  1319. bool Changed = false;
  1320. auto GetOrInsertGlobal = [this, &Changed](StringRef Name,
  1321. Type *Ty) -> Constant * {
  1322. Constant *C = Mod->getOrInsertGlobal(Name, Ty);
  1323. if (GlobalVariable *G = dyn_cast<GlobalVariable>(C)) {
  1324. Changed |= G->getThreadLocalMode() != GlobalVariable::InitialExecTLSModel;
  1325. G->setThreadLocalMode(GlobalVariable::InitialExecTLSModel);
  1326. }
  1327. return C;
  1328. };
  1329. // These globals must be kept in sync with the ones in dfsan.cpp.
  1330. ArgTLS =
  1331. GetOrInsertGlobal("__dfsan_arg_tls",
  1332. ArrayType::get(Type::getInt64Ty(*Ctx), ArgTLSSize / 8));
  1333. RetvalTLS = GetOrInsertGlobal(
  1334. "__dfsan_retval_tls",
  1335. ArrayType::get(Type::getInt64Ty(*Ctx), RetvalTLSSize / 8));
  1336. ArgOriginTLSTy = ArrayType::get(OriginTy, NumOfElementsInArgOrgTLS);
  1337. ArgOriginTLS = GetOrInsertGlobal("__dfsan_arg_origin_tls", ArgOriginTLSTy);
  1338. RetvalOriginTLS = GetOrInsertGlobal("__dfsan_retval_origin_tls", OriginTy);
  1339. (void)Mod->getOrInsertGlobal("__dfsan_track_origins", OriginTy, [&] {
  1340. Changed = true;
  1341. return new GlobalVariable(
  1342. M, OriginTy, true, GlobalValue::WeakODRLinkage,
  1343. ConstantInt::getSigned(OriginTy,
  1344. shouldTrackOrigins() ? ClTrackOrigins : 0),
  1345. "__dfsan_track_origins");
  1346. });
  1347. initializeCallbackFunctions(M);
  1348. initializeRuntimeFunctions(M);
  1349. std::vector<Function *> FnsToInstrument;
  1350. SmallPtrSet<Function *, 2> FnsWithNativeABI;
  1351. SmallPtrSet<Function *, 2> FnsWithForceZeroLabel;
  1352. SmallPtrSet<Constant *, 1> PersonalityFns;
  1353. for (Function &F : M)
  1354. if (!F.isIntrinsic() && !DFSanRuntimeFunctions.contains(&F) &&
  1355. !LibAtomicFunction(F)) {
  1356. FnsToInstrument.push_back(&F);
  1357. if (F.hasPersonalityFn())
  1358. PersonalityFns.insert(F.getPersonalityFn()->stripPointerCasts());
  1359. }
  1360. if (ClIgnorePersonalityRoutine) {
  1361. for (auto *C : PersonalityFns) {
  1362. assert(isa<Function>(C) && "Personality routine is not a function!");
  1363. Function *F = cast<Function>(C);
  1364. if (!isInstrumented(F))
  1365. llvm::erase_value(FnsToInstrument, F);
  1366. }
  1367. }
  1368. // Give function aliases prefixes when necessary, and build wrappers where the
  1369. // instrumentedness is inconsistent.
  1370. for (GlobalAlias &GA : llvm::make_early_inc_range(M.aliases())) {
  1371. // Don't stop on weak. We assume people aren't playing games with the
  1372. // instrumentedness of overridden weak aliases.
  1373. auto *F = dyn_cast<Function>(GA.getAliaseeObject());
  1374. if (!F)
  1375. continue;
  1376. bool GAInst = isInstrumented(&GA), FInst = isInstrumented(F);
  1377. if (GAInst && FInst) {
  1378. addGlobalNameSuffix(&GA);
  1379. } else if (GAInst != FInst) {
  1380. // Non-instrumented alias of an instrumented function, or vice versa.
  1381. // Replace the alias with a native-ABI wrapper of the aliasee. The pass
  1382. // below will take care of instrumenting it.
  1383. Function *NewF =
  1384. buildWrapperFunction(F, "", GA.getLinkage(), F->getFunctionType());
  1385. GA.replaceAllUsesWith(ConstantExpr::getBitCast(NewF, GA.getType()));
  1386. NewF->takeName(&GA);
  1387. GA.eraseFromParent();
  1388. FnsToInstrument.push_back(NewF);
  1389. }
  1390. }
  1391. // TODO: This could be more precise.
  1392. ReadOnlyNoneAttrs.addAttribute(Attribute::Memory);
  1393. // First, change the ABI of every function in the module. ABI-listed
  1394. // functions keep their original ABI and get a wrapper function.
  1395. for (std::vector<Function *>::iterator FI = FnsToInstrument.begin(),
  1396. FE = FnsToInstrument.end();
  1397. FI != FE; ++FI) {
  1398. Function &F = **FI;
  1399. FunctionType *FT = F.getFunctionType();
  1400. bool IsZeroArgsVoidRet = (FT->getNumParams() == 0 && !FT->isVarArg() &&
  1401. FT->getReturnType()->isVoidTy());
  1402. if (isInstrumented(&F)) {
  1403. if (isForceZeroLabels(&F))
  1404. FnsWithForceZeroLabel.insert(&F);
  1405. // Instrumented functions get a '.dfsan' suffix. This allows us to more
  1406. // easily identify cases of mismatching ABIs. This naming scheme is
  1407. // mangling-compatible (see Itanium ABI), using a vendor-specific suffix.
  1408. addGlobalNameSuffix(&F);
  1409. } else if (!IsZeroArgsVoidRet || getWrapperKind(&F) == WK_Custom) {
  1410. // Build a wrapper function for F. The wrapper simply calls F, and is
  1411. // added to FnsToInstrument so that any instrumentation according to its
  1412. // WrapperKind is done in the second pass below.
  1413. // If the function being wrapped has local linkage, then preserve the
  1414. // function's linkage in the wrapper function.
  1415. GlobalValue::LinkageTypes WrapperLinkage =
  1416. F.hasLocalLinkage() ? F.getLinkage()
  1417. : GlobalValue::LinkOnceODRLinkage;
  1418. Function *NewF = buildWrapperFunction(
  1419. &F,
  1420. (shouldTrackOrigins() ? std::string("dfso$") : std::string("dfsw$")) +
  1421. std::string(F.getName()),
  1422. WrapperLinkage, FT);
  1423. NewF->removeFnAttrs(ReadOnlyNoneAttrs);
  1424. Value *WrappedFnCst =
  1425. ConstantExpr::getBitCast(NewF, PointerType::getUnqual(FT));
  1426. // Extern weak functions can sometimes be null at execution time.
  1427. // Code will sometimes check if an extern weak function is null.
  1428. // This could look something like:
  1429. // declare extern_weak i8 @my_func(i8)
  1430. // br i1 icmp ne (i8 (i8)* @my_func, i8 (i8)* null), label %use_my_func,
  1431. // label %avoid_my_func
  1432. // The @"dfsw$my_func" wrapper is never null, so if we replace this use
  1433. // in the comparison, the icmp will simplify to false and we have
  1434. // accidentally optimized away a null check that is necessary.
  1435. // This can lead to a crash when the null extern_weak my_func is called.
  1436. //
  1437. // To prevent (the most common pattern of) this problem,
  1438. // do not replace uses in comparisons with the wrapper.
  1439. // We definitely want to replace uses in call instructions.
  1440. // Other uses (e.g. store the function address somewhere) might be
  1441. // called or compared or both - this case may not be handled correctly.
  1442. // We will default to replacing with wrapper in cases we are unsure.
  1443. auto IsNotCmpUse = [](Use &U) -> bool {
  1444. User *Usr = U.getUser();
  1445. if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Usr)) {
  1446. // This is the most common case for icmp ne null
  1447. if (CE->getOpcode() == Instruction::ICmp) {
  1448. return false;
  1449. }
  1450. }
  1451. if (Instruction *I = dyn_cast<Instruction>(Usr)) {
  1452. if (I->getOpcode() == Instruction::ICmp) {
  1453. return false;
  1454. }
  1455. }
  1456. return true;
  1457. };
  1458. F.replaceUsesWithIf(WrappedFnCst, IsNotCmpUse);
  1459. UnwrappedFnMap[WrappedFnCst] = &F;
  1460. *FI = NewF;
  1461. if (!F.isDeclaration()) {
  1462. // This function is probably defining an interposition of an
  1463. // uninstrumented function and hence needs to keep the original ABI.
  1464. // But any functions it may call need to use the instrumented ABI, so
  1465. // we instrument it in a mode which preserves the original ABI.
  1466. FnsWithNativeABI.insert(&F);
  1467. // This code needs to rebuild the iterators, as they may be invalidated
  1468. // by the push_back, taking care that the new range does not include
  1469. // any functions added by this code.
  1470. size_t N = FI - FnsToInstrument.begin(),
  1471. Count = FE - FnsToInstrument.begin();
  1472. FnsToInstrument.push_back(&F);
  1473. FI = FnsToInstrument.begin() + N;
  1474. FE = FnsToInstrument.begin() + Count;
  1475. }
  1476. // Hopefully, nobody will try to indirectly call a vararg
  1477. // function... yet.
  1478. } else if (FT->isVarArg()) {
  1479. UnwrappedFnMap[&F] = &F;
  1480. *FI = nullptr;
  1481. }
  1482. }
  1483. for (Function *F : FnsToInstrument) {
  1484. if (!F || F->isDeclaration())
  1485. continue;
  1486. removeUnreachableBlocks(*F);
  1487. DFSanFunction DFSF(*this, F, FnsWithNativeABI.count(F),
  1488. FnsWithForceZeroLabel.count(F), GetTLI(*F));
  1489. if (ClReachesFunctionCallbacks) {
  1490. // Add callback for arguments reaching this function.
  1491. for (auto &FArg : F->args()) {
  1492. Instruction *Next = &F->getEntryBlock().front();
  1493. Value *FArgShadow = DFSF.getShadow(&FArg);
  1494. if (isZeroShadow(FArgShadow))
  1495. continue;
  1496. if (Instruction *FArgShadowInst = dyn_cast<Instruction>(FArgShadow)) {
  1497. Next = FArgShadowInst->getNextNode();
  1498. }
  1499. if (shouldTrackOrigins()) {
  1500. if (Instruction *Origin =
  1501. dyn_cast<Instruction>(DFSF.getOrigin(&FArg))) {
  1502. // Ensure IRB insertion point is after loads for shadow and origin.
  1503. Instruction *OriginNext = Origin->getNextNode();
  1504. if (Next->comesBefore(OriginNext)) {
  1505. Next = OriginNext;
  1506. }
  1507. }
  1508. }
  1509. IRBuilder<> IRB(Next);
  1510. DFSF.addReachesFunctionCallbacksIfEnabled(IRB, *Next, &FArg);
  1511. }
  1512. }
  1513. // DFSanVisitor may create new basic blocks, which confuses df_iterator.
  1514. // Build a copy of the list before iterating over it.
  1515. SmallVector<BasicBlock *, 4> BBList(depth_first(&F->getEntryBlock()));
  1516. for (BasicBlock *BB : BBList) {
  1517. Instruction *Inst = &BB->front();
  1518. while (true) {
  1519. // DFSanVisitor may split the current basic block, changing the current
  1520. // instruction's next pointer and moving the next instruction to the
  1521. // tail block from which we should continue.
  1522. Instruction *Next = Inst->getNextNode();
  1523. // DFSanVisitor may delete Inst, so keep track of whether it was a
  1524. // terminator.
  1525. bool IsTerminator = Inst->isTerminator();
  1526. if (!DFSF.SkipInsts.count(Inst))
  1527. DFSanVisitor(DFSF).visit(Inst);
  1528. if (IsTerminator)
  1529. break;
  1530. Inst = Next;
  1531. }
  1532. }
  1533. // We will not necessarily be able to compute the shadow for every phi node
  1534. // until we have visited every block. Therefore, the code that handles phi
  1535. // nodes adds them to the PHIFixups list so that they can be properly
  1536. // handled here.
  1537. for (DFSanFunction::PHIFixupElement &P : DFSF.PHIFixups) {
  1538. for (unsigned Val = 0, N = P.Phi->getNumIncomingValues(); Val != N;
  1539. ++Val) {
  1540. P.ShadowPhi->setIncomingValue(
  1541. Val, DFSF.getShadow(P.Phi->getIncomingValue(Val)));
  1542. if (P.OriginPhi)
  1543. P.OriginPhi->setIncomingValue(
  1544. Val, DFSF.getOrigin(P.Phi->getIncomingValue(Val)));
  1545. }
  1546. }
  1547. // -dfsan-debug-nonzero-labels will split the CFG in all kinds of crazy
  1548. // places (i.e. instructions in basic blocks we haven't even begun visiting
  1549. // yet). To make our life easier, do this work in a pass after the main
  1550. // instrumentation.
  1551. if (ClDebugNonzeroLabels) {
  1552. for (Value *V : DFSF.NonZeroChecks) {
  1553. Instruction *Pos;
  1554. if (Instruction *I = dyn_cast<Instruction>(V))
  1555. Pos = I->getNextNode();
  1556. else
  1557. Pos = &DFSF.F->getEntryBlock().front();
  1558. while (isa<PHINode>(Pos) || isa<AllocaInst>(Pos))
  1559. Pos = Pos->getNextNode();
  1560. IRBuilder<> IRB(Pos);
  1561. Value *PrimitiveShadow = DFSF.collapseToPrimitiveShadow(V, Pos);
  1562. Value *Ne =
  1563. IRB.CreateICmpNE(PrimitiveShadow, DFSF.DFS.ZeroPrimitiveShadow);
  1564. BranchInst *BI = cast<BranchInst>(SplitBlockAndInsertIfThen(
  1565. Ne, Pos, /*Unreachable=*/false, ColdCallWeights));
  1566. IRBuilder<> ThenIRB(BI);
  1567. ThenIRB.CreateCall(DFSF.DFS.DFSanNonzeroLabelFn, {});
  1568. }
  1569. }
  1570. }
  1571. return Changed || !FnsToInstrument.empty() ||
  1572. M.global_size() != InitialGlobalSize || M.size() != InitialModuleSize;
  1573. }
  1574. Value *DFSanFunction::getArgTLS(Type *T, unsigned ArgOffset, IRBuilder<> &IRB) {
  1575. Value *Base = IRB.CreatePointerCast(DFS.ArgTLS, DFS.IntptrTy);
  1576. if (ArgOffset)
  1577. Base = IRB.CreateAdd(Base, ConstantInt::get(DFS.IntptrTy, ArgOffset));
  1578. return IRB.CreateIntToPtr(Base, PointerType::get(DFS.getShadowTy(T), 0),
  1579. "_dfsarg");
  1580. }
  1581. Value *DFSanFunction::getRetvalTLS(Type *T, IRBuilder<> &IRB) {
  1582. return IRB.CreatePointerCast(
  1583. DFS.RetvalTLS, PointerType::get(DFS.getShadowTy(T), 0), "_dfsret");
  1584. }
  1585. Value *DFSanFunction::getRetvalOriginTLS() { return DFS.RetvalOriginTLS; }
  1586. Value *DFSanFunction::getArgOriginTLS(unsigned ArgNo, IRBuilder<> &IRB) {
  1587. return IRB.CreateConstGEP2_64(DFS.ArgOriginTLSTy, DFS.ArgOriginTLS, 0, ArgNo,
  1588. "_dfsarg_o");
  1589. }
  1590. Value *DFSanFunction::getOrigin(Value *V) {
  1591. assert(DFS.shouldTrackOrigins());
  1592. if (!isa<Argument>(V) && !isa<Instruction>(V))
  1593. return DFS.ZeroOrigin;
  1594. Value *&Origin = ValOriginMap[V];
  1595. if (!Origin) {
  1596. if (Argument *A = dyn_cast<Argument>(V)) {
  1597. if (IsNativeABI)
  1598. return DFS.ZeroOrigin;
  1599. if (A->getArgNo() < DFS.NumOfElementsInArgOrgTLS) {
  1600. Instruction *ArgOriginTLSPos = &*F->getEntryBlock().begin();
  1601. IRBuilder<> IRB(ArgOriginTLSPos);
  1602. Value *ArgOriginPtr = getArgOriginTLS(A->getArgNo(), IRB);
  1603. Origin = IRB.CreateLoad(DFS.OriginTy, ArgOriginPtr);
  1604. } else {
  1605. // Overflow
  1606. Origin = DFS.ZeroOrigin;
  1607. }
  1608. } else {
  1609. Origin = DFS.ZeroOrigin;
  1610. }
  1611. }
  1612. return Origin;
  1613. }
  1614. void DFSanFunction::setOrigin(Instruction *I, Value *Origin) {
  1615. if (!DFS.shouldTrackOrigins())
  1616. return;
  1617. assert(!ValOriginMap.count(I));
  1618. assert(Origin->getType() == DFS.OriginTy);
  1619. ValOriginMap[I] = Origin;
  1620. }
  1621. Value *DFSanFunction::getShadowForTLSArgument(Argument *A) {
  1622. unsigned ArgOffset = 0;
  1623. const DataLayout &DL = F->getParent()->getDataLayout();
  1624. for (auto &FArg : F->args()) {
  1625. if (!FArg.getType()->isSized()) {
  1626. if (A == &FArg)
  1627. break;
  1628. continue;
  1629. }
  1630. unsigned Size = DL.getTypeAllocSize(DFS.getShadowTy(&FArg));
  1631. if (A != &FArg) {
  1632. ArgOffset += alignTo(Size, ShadowTLSAlignment);
  1633. if (ArgOffset > ArgTLSSize)
  1634. break; // ArgTLS overflows, uses a zero shadow.
  1635. continue;
  1636. }
  1637. if (ArgOffset + Size > ArgTLSSize)
  1638. break; // ArgTLS overflows, uses a zero shadow.
  1639. Instruction *ArgTLSPos = &*F->getEntryBlock().begin();
  1640. IRBuilder<> IRB(ArgTLSPos);
  1641. Value *ArgShadowPtr = getArgTLS(FArg.getType(), ArgOffset, IRB);
  1642. return IRB.CreateAlignedLoad(DFS.getShadowTy(&FArg), ArgShadowPtr,
  1643. ShadowTLSAlignment);
  1644. }
  1645. return DFS.getZeroShadow(A);
  1646. }
  1647. Value *DFSanFunction::getShadow(Value *V) {
  1648. if (!isa<Argument>(V) && !isa<Instruction>(V))
  1649. return DFS.getZeroShadow(V);
  1650. if (IsForceZeroLabels)
  1651. return DFS.getZeroShadow(V);
  1652. Value *&Shadow = ValShadowMap[V];
  1653. if (!Shadow) {
  1654. if (Argument *A = dyn_cast<Argument>(V)) {
  1655. if (IsNativeABI)
  1656. return DFS.getZeroShadow(V);
  1657. Shadow = getShadowForTLSArgument(A);
  1658. NonZeroChecks.push_back(Shadow);
  1659. } else {
  1660. Shadow = DFS.getZeroShadow(V);
  1661. }
  1662. }
  1663. return Shadow;
  1664. }
  1665. void DFSanFunction::setShadow(Instruction *I, Value *Shadow) {
  1666. assert(!ValShadowMap.count(I));
  1667. ValShadowMap[I] = Shadow;
  1668. }
  1669. /// Compute the integer shadow offset that corresponds to a given
  1670. /// application address.
  1671. ///
  1672. /// Offset = (Addr & ~AndMask) ^ XorMask
  1673. Value *DataFlowSanitizer::getShadowOffset(Value *Addr, IRBuilder<> &IRB) {
  1674. assert(Addr != RetvalTLS && "Reinstrumenting?");
  1675. Value *OffsetLong = IRB.CreatePointerCast(Addr, IntptrTy);
  1676. uint64_t AndMask = MapParams->AndMask;
  1677. if (AndMask)
  1678. OffsetLong =
  1679. IRB.CreateAnd(OffsetLong, ConstantInt::get(IntptrTy, ~AndMask));
  1680. uint64_t XorMask = MapParams->XorMask;
  1681. if (XorMask)
  1682. OffsetLong = IRB.CreateXor(OffsetLong, ConstantInt::get(IntptrTy, XorMask));
  1683. return OffsetLong;
  1684. }
  1685. std::pair<Value *, Value *>
  1686. DataFlowSanitizer::getShadowOriginAddress(Value *Addr, Align InstAlignment,
  1687. Instruction *Pos) {
  1688. // Returns ((Addr & shadow_mask) + origin_base - shadow_base) & ~4UL
  1689. IRBuilder<> IRB(Pos);
  1690. Value *ShadowOffset = getShadowOffset(Addr, IRB);
  1691. Value *ShadowLong = ShadowOffset;
  1692. uint64_t ShadowBase = MapParams->ShadowBase;
  1693. if (ShadowBase != 0) {
  1694. ShadowLong =
  1695. IRB.CreateAdd(ShadowLong, ConstantInt::get(IntptrTy, ShadowBase));
  1696. }
  1697. IntegerType *ShadowTy = IntegerType::get(*Ctx, ShadowWidthBits);
  1698. Value *ShadowPtr =
  1699. IRB.CreateIntToPtr(ShadowLong, PointerType::get(ShadowTy, 0));
  1700. Value *OriginPtr = nullptr;
  1701. if (shouldTrackOrigins()) {
  1702. Value *OriginLong = ShadowOffset;
  1703. uint64_t OriginBase = MapParams->OriginBase;
  1704. if (OriginBase != 0)
  1705. OriginLong =
  1706. IRB.CreateAdd(OriginLong, ConstantInt::get(IntptrTy, OriginBase));
  1707. const Align Alignment = llvm::assumeAligned(InstAlignment.value());
  1708. // When alignment is >= 4, Addr must be aligned to 4, otherwise it is UB.
  1709. // So Mask is unnecessary.
  1710. if (Alignment < MinOriginAlignment) {
  1711. uint64_t Mask = MinOriginAlignment.value() - 1;
  1712. OriginLong = IRB.CreateAnd(OriginLong, ConstantInt::get(IntptrTy, ~Mask));
  1713. }
  1714. OriginPtr = IRB.CreateIntToPtr(OriginLong, OriginPtrTy);
  1715. }
  1716. return std::make_pair(ShadowPtr, OriginPtr);
  1717. }
  1718. Value *DataFlowSanitizer::getShadowAddress(Value *Addr, Instruction *Pos,
  1719. Value *ShadowOffset) {
  1720. IRBuilder<> IRB(Pos);
  1721. return IRB.CreateIntToPtr(ShadowOffset, PrimitiveShadowPtrTy);
  1722. }
  1723. Value *DataFlowSanitizer::getShadowAddress(Value *Addr, Instruction *Pos) {
  1724. IRBuilder<> IRB(Pos);
  1725. Value *ShadowOffset = getShadowOffset(Addr, IRB);
  1726. return getShadowAddress(Addr, Pos, ShadowOffset);
  1727. }
  1728. Value *DFSanFunction::combineShadowsThenConvert(Type *T, Value *V1, Value *V2,
  1729. Instruction *Pos) {
  1730. Value *PrimitiveValue = combineShadows(V1, V2, Pos);
  1731. return expandFromPrimitiveShadow(T, PrimitiveValue, Pos);
  1732. }
  1733. // Generates IR to compute the union of the two given shadows, inserting it
  1734. // before Pos. The combined value is with primitive type.
  1735. Value *DFSanFunction::combineShadows(Value *V1, Value *V2, Instruction *Pos) {
  1736. if (DFS.isZeroShadow(V1))
  1737. return collapseToPrimitiveShadow(V2, Pos);
  1738. if (DFS.isZeroShadow(V2))
  1739. return collapseToPrimitiveShadow(V1, Pos);
  1740. if (V1 == V2)
  1741. return collapseToPrimitiveShadow(V1, Pos);
  1742. auto V1Elems = ShadowElements.find(V1);
  1743. auto V2Elems = ShadowElements.find(V2);
  1744. if (V1Elems != ShadowElements.end() && V2Elems != ShadowElements.end()) {
  1745. if (std::includes(V1Elems->second.begin(), V1Elems->second.end(),
  1746. V2Elems->second.begin(), V2Elems->second.end())) {
  1747. return collapseToPrimitiveShadow(V1, Pos);
  1748. }
  1749. if (std::includes(V2Elems->second.begin(), V2Elems->second.end(),
  1750. V1Elems->second.begin(), V1Elems->second.end())) {
  1751. return collapseToPrimitiveShadow(V2, Pos);
  1752. }
  1753. } else if (V1Elems != ShadowElements.end()) {
  1754. if (V1Elems->second.count(V2))
  1755. return collapseToPrimitiveShadow(V1, Pos);
  1756. } else if (V2Elems != ShadowElements.end()) {
  1757. if (V2Elems->second.count(V1))
  1758. return collapseToPrimitiveShadow(V2, Pos);
  1759. }
  1760. auto Key = std::make_pair(V1, V2);
  1761. if (V1 > V2)
  1762. std::swap(Key.first, Key.second);
  1763. CachedShadow &CCS = CachedShadows[Key];
  1764. if (CCS.Block && DT.dominates(CCS.Block, Pos->getParent()))
  1765. return CCS.Shadow;
  1766. // Converts inputs shadows to shadows with primitive types.
  1767. Value *PV1 = collapseToPrimitiveShadow(V1, Pos);
  1768. Value *PV2 = collapseToPrimitiveShadow(V2, Pos);
  1769. IRBuilder<> IRB(Pos);
  1770. CCS.Block = Pos->getParent();
  1771. CCS.Shadow = IRB.CreateOr(PV1, PV2);
  1772. std::set<Value *> UnionElems;
  1773. if (V1Elems != ShadowElements.end()) {
  1774. UnionElems = V1Elems->second;
  1775. } else {
  1776. UnionElems.insert(V1);
  1777. }
  1778. if (V2Elems != ShadowElements.end()) {
  1779. UnionElems.insert(V2Elems->second.begin(), V2Elems->second.end());
  1780. } else {
  1781. UnionElems.insert(V2);
  1782. }
  1783. ShadowElements[CCS.Shadow] = std::move(UnionElems);
  1784. return CCS.Shadow;
  1785. }
  1786. // A convenience function which folds the shadows of each of the operands
  1787. // of the provided instruction Inst, inserting the IR before Inst. Returns
  1788. // the computed union Value.
  1789. Value *DFSanFunction::combineOperandShadows(Instruction *Inst) {
  1790. if (Inst->getNumOperands() == 0)
  1791. return DFS.getZeroShadow(Inst);
  1792. Value *Shadow = getShadow(Inst->getOperand(0));
  1793. for (unsigned I = 1, N = Inst->getNumOperands(); I < N; ++I)
  1794. Shadow = combineShadows(Shadow, getShadow(Inst->getOperand(I)), Inst);
  1795. return expandFromPrimitiveShadow(Inst->getType(), Shadow, Inst);
  1796. }
  1797. void DFSanVisitor::visitInstOperands(Instruction &I) {
  1798. Value *CombinedShadow = DFSF.combineOperandShadows(&I);
  1799. DFSF.setShadow(&I, CombinedShadow);
  1800. visitInstOperandOrigins(I);
  1801. }
  1802. Value *DFSanFunction::combineOrigins(const std::vector<Value *> &Shadows,
  1803. const std::vector<Value *> &Origins,
  1804. Instruction *Pos, ConstantInt *Zero) {
  1805. assert(Shadows.size() == Origins.size());
  1806. size_t Size = Origins.size();
  1807. if (Size == 0)
  1808. return DFS.ZeroOrigin;
  1809. Value *Origin = nullptr;
  1810. if (!Zero)
  1811. Zero = DFS.ZeroPrimitiveShadow;
  1812. for (size_t I = 0; I != Size; ++I) {
  1813. Value *OpOrigin = Origins[I];
  1814. Constant *ConstOpOrigin = dyn_cast<Constant>(OpOrigin);
  1815. if (ConstOpOrigin && ConstOpOrigin->isNullValue())
  1816. continue;
  1817. if (!Origin) {
  1818. Origin = OpOrigin;
  1819. continue;
  1820. }
  1821. Value *OpShadow = Shadows[I];
  1822. Value *PrimitiveShadow = collapseToPrimitiveShadow(OpShadow, Pos);
  1823. IRBuilder<> IRB(Pos);
  1824. Value *Cond = IRB.CreateICmpNE(PrimitiveShadow, Zero);
  1825. Origin = IRB.CreateSelect(Cond, OpOrigin, Origin);
  1826. }
  1827. return Origin ? Origin : DFS.ZeroOrigin;
  1828. }
  1829. Value *DFSanFunction::combineOperandOrigins(Instruction *Inst) {
  1830. size_t Size = Inst->getNumOperands();
  1831. std::vector<Value *> Shadows(Size);
  1832. std::vector<Value *> Origins(Size);
  1833. for (unsigned I = 0; I != Size; ++I) {
  1834. Shadows[I] = getShadow(Inst->getOperand(I));
  1835. Origins[I] = getOrigin(Inst->getOperand(I));
  1836. }
  1837. return combineOrigins(Shadows, Origins, Inst);
  1838. }
  1839. void DFSanVisitor::visitInstOperandOrigins(Instruction &I) {
  1840. if (!DFSF.DFS.shouldTrackOrigins())
  1841. return;
  1842. Value *CombinedOrigin = DFSF.combineOperandOrigins(&I);
  1843. DFSF.setOrigin(&I, CombinedOrigin);
  1844. }
  1845. Align DFSanFunction::getShadowAlign(Align InstAlignment) {
  1846. const Align Alignment = ClPreserveAlignment ? InstAlignment : Align(1);
  1847. return Align(Alignment.value() * DFS.ShadowWidthBytes);
  1848. }
  1849. Align DFSanFunction::getOriginAlign(Align InstAlignment) {
  1850. const Align Alignment = llvm::assumeAligned(InstAlignment.value());
  1851. return Align(std::max(MinOriginAlignment, Alignment));
  1852. }
  1853. bool DFSanFunction::isLookupTableConstant(Value *P) {
  1854. if (GlobalVariable *GV = dyn_cast<GlobalVariable>(P->stripPointerCasts()))
  1855. if (GV->isConstant() && GV->hasName())
  1856. return DFS.CombineTaintLookupTableNames.count(GV->getName());
  1857. return false;
  1858. }
  1859. bool DFSanFunction::useCallbackLoadLabelAndOrigin(uint64_t Size,
  1860. Align InstAlignment) {
  1861. // When enabling tracking load instructions, we always use
  1862. // __dfsan_load_label_and_origin to reduce code size.
  1863. if (ClTrackOrigins == 2)
  1864. return true;
  1865. assert(Size != 0);
  1866. // * if Size == 1, it is sufficient to load its origin aligned at 4.
  1867. // * if Size == 2, we assume most cases Addr % 2 == 0, so it is sufficient to
  1868. // load its origin aligned at 4. If not, although origins may be lost, it
  1869. // should not happen very often.
  1870. // * if align >= 4, Addr must be aligned to 4, otherwise it is UB. When
  1871. // Size % 4 == 0, it is more efficient to load origins without callbacks.
  1872. // * Otherwise we use __dfsan_load_label_and_origin.
  1873. // This should ensure that common cases run efficiently.
  1874. if (Size <= 2)
  1875. return false;
  1876. const Align Alignment = llvm::assumeAligned(InstAlignment.value());
  1877. return Alignment < MinOriginAlignment || !DFS.hasLoadSizeForFastPath(Size);
  1878. }
  1879. Value *DataFlowSanitizer::loadNextOrigin(Instruction *Pos, Align OriginAlign,
  1880. Value **OriginAddr) {
  1881. IRBuilder<> IRB(Pos);
  1882. *OriginAddr =
  1883. IRB.CreateGEP(OriginTy, *OriginAddr, ConstantInt::get(IntptrTy, 1));
  1884. return IRB.CreateAlignedLoad(OriginTy, *OriginAddr, OriginAlign);
  1885. }
  1886. std::pair<Value *, Value *> DFSanFunction::loadShadowFast(
  1887. Value *ShadowAddr, Value *OriginAddr, uint64_t Size, Align ShadowAlign,
  1888. Align OriginAlign, Value *FirstOrigin, Instruction *Pos) {
  1889. const bool ShouldTrackOrigins = DFS.shouldTrackOrigins();
  1890. const uint64_t ShadowSize = Size * DFS.ShadowWidthBytes;
  1891. assert(Size >= 4 && "Not large enough load size for fast path!");
  1892. // Used for origin tracking.
  1893. std::vector<Value *> Shadows;
  1894. std::vector<Value *> Origins;
  1895. // Load instructions in LLVM can have arbitrary byte sizes (e.g., 3, 12, 20)
  1896. // but this function is only used in a subset of cases that make it possible
  1897. // to optimize the instrumentation.
  1898. //
  1899. // Specifically, when the shadow size in bytes (i.e., loaded bytes x shadow
  1900. // per byte) is either:
  1901. // - a multiple of 8 (common)
  1902. // - equal to 4 (only for load32)
  1903. //
  1904. // For the second case, we can fit the wide shadow in a 32-bit integer. In all
  1905. // other cases, we use a 64-bit integer to hold the wide shadow.
  1906. Type *WideShadowTy =
  1907. ShadowSize == 4 ? Type::getInt32Ty(*DFS.Ctx) : Type::getInt64Ty(*DFS.Ctx);
  1908. IRBuilder<> IRB(Pos);
  1909. Value *WideAddr = IRB.CreateBitCast(ShadowAddr, WideShadowTy->getPointerTo());
  1910. Value *CombinedWideShadow =
  1911. IRB.CreateAlignedLoad(WideShadowTy, WideAddr, ShadowAlign);
  1912. unsigned WideShadowBitWidth = WideShadowTy->getIntegerBitWidth();
  1913. const uint64_t BytesPerWideShadow = WideShadowBitWidth / DFS.ShadowWidthBits;
  1914. auto AppendWideShadowAndOrigin = [&](Value *WideShadow, Value *Origin) {
  1915. if (BytesPerWideShadow > 4) {
  1916. assert(BytesPerWideShadow == 8);
  1917. // The wide shadow relates to two origin pointers: one for the first four
  1918. // application bytes, and one for the latest four. We use a left shift to
  1919. // get just the shadow bytes that correspond to the first origin pointer,
  1920. // and then the entire shadow for the second origin pointer (which will be
  1921. // chosen by combineOrigins() iff the least-significant half of the wide
  1922. // shadow was empty but the other half was not).
  1923. Value *WideShadowLo = IRB.CreateShl(
  1924. WideShadow, ConstantInt::get(WideShadowTy, WideShadowBitWidth / 2));
  1925. Shadows.push_back(WideShadow);
  1926. Origins.push_back(DFS.loadNextOrigin(Pos, OriginAlign, &OriginAddr));
  1927. Shadows.push_back(WideShadowLo);
  1928. Origins.push_back(Origin);
  1929. } else {
  1930. Shadows.push_back(WideShadow);
  1931. Origins.push_back(Origin);
  1932. }
  1933. };
  1934. if (ShouldTrackOrigins)
  1935. AppendWideShadowAndOrigin(CombinedWideShadow, FirstOrigin);
  1936. // First OR all the WideShadows (i.e., 64bit or 32bit shadow chunks) linearly;
  1937. // then OR individual shadows within the combined WideShadow by binary ORing.
  1938. // This is fewer instructions than ORing shadows individually, since it
  1939. // needs logN shift/or instructions (N being the bytes of the combined wide
  1940. // shadow).
  1941. for (uint64_t ByteOfs = BytesPerWideShadow; ByteOfs < Size;
  1942. ByteOfs += BytesPerWideShadow) {
  1943. WideAddr = IRB.CreateGEP(WideShadowTy, WideAddr,
  1944. ConstantInt::get(DFS.IntptrTy, 1));
  1945. Value *NextWideShadow =
  1946. IRB.CreateAlignedLoad(WideShadowTy, WideAddr, ShadowAlign);
  1947. CombinedWideShadow = IRB.CreateOr(CombinedWideShadow, NextWideShadow);
  1948. if (ShouldTrackOrigins) {
  1949. Value *NextOrigin = DFS.loadNextOrigin(Pos, OriginAlign, &OriginAddr);
  1950. AppendWideShadowAndOrigin(NextWideShadow, NextOrigin);
  1951. }
  1952. }
  1953. for (unsigned Width = WideShadowBitWidth / 2; Width >= DFS.ShadowWidthBits;
  1954. Width >>= 1) {
  1955. Value *ShrShadow = IRB.CreateLShr(CombinedWideShadow, Width);
  1956. CombinedWideShadow = IRB.CreateOr(CombinedWideShadow, ShrShadow);
  1957. }
  1958. return {IRB.CreateTrunc(CombinedWideShadow, DFS.PrimitiveShadowTy),
  1959. ShouldTrackOrigins
  1960. ? combineOrigins(Shadows, Origins, Pos,
  1961. ConstantInt::getSigned(IRB.getInt64Ty(), 0))
  1962. : DFS.ZeroOrigin};
  1963. }
  1964. std::pair<Value *, Value *> DFSanFunction::loadShadowOriginSansLoadTracking(
  1965. Value *Addr, uint64_t Size, Align InstAlignment, Instruction *Pos) {
  1966. const bool ShouldTrackOrigins = DFS.shouldTrackOrigins();
  1967. // Non-escaped loads.
  1968. if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
  1969. const auto SI = AllocaShadowMap.find(AI);
  1970. if (SI != AllocaShadowMap.end()) {
  1971. IRBuilder<> IRB(Pos);
  1972. Value *ShadowLI = IRB.CreateLoad(DFS.PrimitiveShadowTy, SI->second);
  1973. const auto OI = AllocaOriginMap.find(AI);
  1974. assert(!ShouldTrackOrigins || OI != AllocaOriginMap.end());
  1975. return {ShadowLI, ShouldTrackOrigins
  1976. ? IRB.CreateLoad(DFS.OriginTy, OI->second)
  1977. : nullptr};
  1978. }
  1979. }
  1980. // Load from constant addresses.
  1981. SmallVector<const Value *, 2> Objs;
  1982. getUnderlyingObjects(Addr, Objs);
  1983. bool AllConstants = true;
  1984. for (const Value *Obj : Objs) {
  1985. if (isa<Function>(Obj) || isa<BlockAddress>(Obj))
  1986. continue;
  1987. if (isa<GlobalVariable>(Obj) && cast<GlobalVariable>(Obj)->isConstant())
  1988. continue;
  1989. AllConstants = false;
  1990. break;
  1991. }
  1992. if (AllConstants)
  1993. return {DFS.ZeroPrimitiveShadow,
  1994. ShouldTrackOrigins ? DFS.ZeroOrigin : nullptr};
  1995. if (Size == 0)
  1996. return {DFS.ZeroPrimitiveShadow,
  1997. ShouldTrackOrigins ? DFS.ZeroOrigin : nullptr};
  1998. // Use callback to load if this is not an optimizable case for origin
  1999. // tracking.
  2000. if (ShouldTrackOrigins &&
  2001. useCallbackLoadLabelAndOrigin(Size, InstAlignment)) {
  2002. IRBuilder<> IRB(Pos);
  2003. CallInst *Call =
  2004. IRB.CreateCall(DFS.DFSanLoadLabelAndOriginFn,
  2005. {IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
  2006. ConstantInt::get(DFS.IntptrTy, Size)});
  2007. Call->addRetAttr(Attribute::ZExt);
  2008. return {IRB.CreateTrunc(IRB.CreateLShr(Call, DFS.OriginWidthBits),
  2009. DFS.PrimitiveShadowTy),
  2010. IRB.CreateTrunc(Call, DFS.OriginTy)};
  2011. }
  2012. // Other cases that support loading shadows or origins in a fast way.
  2013. Value *ShadowAddr, *OriginAddr;
  2014. std::tie(ShadowAddr, OriginAddr) =
  2015. DFS.getShadowOriginAddress(Addr, InstAlignment, Pos);
  2016. const Align ShadowAlign = getShadowAlign(InstAlignment);
  2017. const Align OriginAlign = getOriginAlign(InstAlignment);
  2018. Value *Origin = nullptr;
  2019. if (ShouldTrackOrigins) {
  2020. IRBuilder<> IRB(Pos);
  2021. Origin = IRB.CreateAlignedLoad(DFS.OriginTy, OriginAddr, OriginAlign);
  2022. }
  2023. // When the byte size is small enough, we can load the shadow directly with
  2024. // just a few instructions.
  2025. switch (Size) {
  2026. case 1: {
  2027. LoadInst *LI = new LoadInst(DFS.PrimitiveShadowTy, ShadowAddr, "", Pos);
  2028. LI->setAlignment(ShadowAlign);
  2029. return {LI, Origin};
  2030. }
  2031. case 2: {
  2032. IRBuilder<> IRB(Pos);
  2033. Value *ShadowAddr1 = IRB.CreateGEP(DFS.PrimitiveShadowTy, ShadowAddr,
  2034. ConstantInt::get(DFS.IntptrTy, 1));
  2035. Value *Load =
  2036. IRB.CreateAlignedLoad(DFS.PrimitiveShadowTy, ShadowAddr, ShadowAlign);
  2037. Value *Load1 =
  2038. IRB.CreateAlignedLoad(DFS.PrimitiveShadowTy, ShadowAddr1, ShadowAlign);
  2039. return {combineShadows(Load, Load1, Pos), Origin};
  2040. }
  2041. }
  2042. bool HasSizeForFastPath = DFS.hasLoadSizeForFastPath(Size);
  2043. if (HasSizeForFastPath)
  2044. return loadShadowFast(ShadowAddr, OriginAddr, Size, ShadowAlign,
  2045. OriginAlign, Origin, Pos);
  2046. IRBuilder<> IRB(Pos);
  2047. CallInst *FallbackCall = IRB.CreateCall(
  2048. DFS.DFSanUnionLoadFn, {ShadowAddr, ConstantInt::get(DFS.IntptrTy, Size)});
  2049. FallbackCall->addRetAttr(Attribute::ZExt);
  2050. return {FallbackCall, Origin};
  2051. }
  2052. std::pair<Value *, Value *> DFSanFunction::loadShadowOrigin(Value *Addr,
  2053. uint64_t Size,
  2054. Align InstAlignment,
  2055. Instruction *Pos) {
  2056. Value *PrimitiveShadow, *Origin;
  2057. std::tie(PrimitiveShadow, Origin) =
  2058. loadShadowOriginSansLoadTracking(Addr, Size, InstAlignment, Pos);
  2059. if (DFS.shouldTrackOrigins()) {
  2060. if (ClTrackOrigins == 2) {
  2061. IRBuilder<> IRB(Pos);
  2062. auto *ConstantShadow = dyn_cast<Constant>(PrimitiveShadow);
  2063. if (!ConstantShadow || !ConstantShadow->isZeroValue())
  2064. Origin = updateOriginIfTainted(PrimitiveShadow, Origin, IRB);
  2065. }
  2066. }
  2067. return {PrimitiveShadow, Origin};
  2068. }
  2069. static AtomicOrdering addAcquireOrdering(AtomicOrdering AO) {
  2070. switch (AO) {
  2071. case AtomicOrdering::NotAtomic:
  2072. return AtomicOrdering::NotAtomic;
  2073. case AtomicOrdering::Unordered:
  2074. case AtomicOrdering::Monotonic:
  2075. case AtomicOrdering::Acquire:
  2076. return AtomicOrdering::Acquire;
  2077. case AtomicOrdering::Release:
  2078. case AtomicOrdering::AcquireRelease:
  2079. return AtomicOrdering::AcquireRelease;
  2080. case AtomicOrdering::SequentiallyConsistent:
  2081. return AtomicOrdering::SequentiallyConsistent;
  2082. }
  2083. llvm_unreachable("Unknown ordering");
  2084. }
  2085. Value *StripPointerGEPsAndCasts(Value *V) {
  2086. if (!V->getType()->isPointerTy())
  2087. return V;
  2088. // DFSan pass should be running on valid IR, but we'll
  2089. // keep a seen set to ensure there are no issues.
  2090. SmallPtrSet<const Value *, 4> Visited;
  2091. Visited.insert(V);
  2092. do {
  2093. if (auto *GEP = dyn_cast<GEPOperator>(V)) {
  2094. V = GEP->getPointerOperand();
  2095. } else if (Operator::getOpcode(V) == Instruction::BitCast) {
  2096. V = cast<Operator>(V)->getOperand(0);
  2097. if (!V->getType()->isPointerTy())
  2098. return V;
  2099. } else if (isa<GlobalAlias>(V)) {
  2100. V = cast<GlobalAlias>(V)->getAliasee();
  2101. }
  2102. } while (Visited.insert(V).second);
  2103. return V;
  2104. }
  2105. void DFSanVisitor::visitLoadInst(LoadInst &LI) {
  2106. auto &DL = LI.getModule()->getDataLayout();
  2107. uint64_t Size = DL.getTypeStoreSize(LI.getType());
  2108. if (Size == 0) {
  2109. DFSF.setShadow(&LI, DFSF.DFS.getZeroShadow(&LI));
  2110. DFSF.setOrigin(&LI, DFSF.DFS.ZeroOrigin);
  2111. return;
  2112. }
  2113. // When an application load is atomic, increase atomic ordering between
  2114. // atomic application loads and stores to ensure happen-before order; load
  2115. // shadow data after application data; store zero shadow data before
  2116. // application data. This ensure shadow loads return either labels of the
  2117. // initial application data or zeros.
  2118. if (LI.isAtomic())
  2119. LI.setOrdering(addAcquireOrdering(LI.getOrdering()));
  2120. Instruction *AfterLi = LI.getNextNode();
  2121. Instruction *Pos = LI.isAtomic() ? LI.getNextNode() : &LI;
  2122. std::vector<Value *> Shadows;
  2123. std::vector<Value *> Origins;
  2124. Value *PrimitiveShadow, *Origin;
  2125. std::tie(PrimitiveShadow, Origin) =
  2126. DFSF.loadShadowOrigin(LI.getPointerOperand(), Size, LI.getAlign(), Pos);
  2127. const bool ShouldTrackOrigins = DFSF.DFS.shouldTrackOrigins();
  2128. if (ShouldTrackOrigins) {
  2129. Shadows.push_back(PrimitiveShadow);
  2130. Origins.push_back(Origin);
  2131. }
  2132. if (ClCombinePointerLabelsOnLoad ||
  2133. DFSF.isLookupTableConstant(
  2134. StripPointerGEPsAndCasts(LI.getPointerOperand()))) {
  2135. Value *PtrShadow = DFSF.getShadow(LI.getPointerOperand());
  2136. PrimitiveShadow = DFSF.combineShadows(PrimitiveShadow, PtrShadow, Pos);
  2137. if (ShouldTrackOrigins) {
  2138. Shadows.push_back(PtrShadow);
  2139. Origins.push_back(DFSF.getOrigin(LI.getPointerOperand()));
  2140. }
  2141. }
  2142. if (!DFSF.DFS.isZeroShadow(PrimitiveShadow))
  2143. DFSF.NonZeroChecks.push_back(PrimitiveShadow);
  2144. Value *Shadow =
  2145. DFSF.expandFromPrimitiveShadow(LI.getType(), PrimitiveShadow, Pos);
  2146. DFSF.setShadow(&LI, Shadow);
  2147. if (ShouldTrackOrigins) {
  2148. DFSF.setOrigin(&LI, DFSF.combineOrigins(Shadows, Origins, Pos));
  2149. }
  2150. if (ClEventCallbacks) {
  2151. IRBuilder<> IRB(Pos);
  2152. Value *Addr8 = IRB.CreateBitCast(LI.getPointerOperand(), DFSF.DFS.Int8Ptr);
  2153. CallInst *CI =
  2154. IRB.CreateCall(DFSF.DFS.DFSanLoadCallbackFn, {PrimitiveShadow, Addr8});
  2155. CI->addParamAttr(0, Attribute::ZExt);
  2156. }
  2157. IRBuilder<> IRB(AfterLi);
  2158. DFSF.addReachesFunctionCallbacksIfEnabled(IRB, LI, &LI);
  2159. }
  2160. Value *DFSanFunction::updateOriginIfTainted(Value *Shadow, Value *Origin,
  2161. IRBuilder<> &IRB) {
  2162. assert(DFS.shouldTrackOrigins());
  2163. return IRB.CreateCall(DFS.DFSanChainOriginIfTaintedFn, {Shadow, Origin});
  2164. }
  2165. Value *DFSanFunction::updateOrigin(Value *V, IRBuilder<> &IRB) {
  2166. if (!DFS.shouldTrackOrigins())
  2167. return V;
  2168. return IRB.CreateCall(DFS.DFSanChainOriginFn, V);
  2169. }
  2170. Value *DFSanFunction::originToIntptr(IRBuilder<> &IRB, Value *Origin) {
  2171. const unsigned OriginSize = DataFlowSanitizer::OriginWidthBytes;
  2172. const DataLayout &DL = F->getParent()->getDataLayout();
  2173. unsigned IntptrSize = DL.getTypeStoreSize(DFS.IntptrTy);
  2174. if (IntptrSize == OriginSize)
  2175. return Origin;
  2176. assert(IntptrSize == OriginSize * 2);
  2177. Origin = IRB.CreateIntCast(Origin, DFS.IntptrTy, /* isSigned */ false);
  2178. return IRB.CreateOr(Origin, IRB.CreateShl(Origin, OriginSize * 8));
  2179. }
  2180. void DFSanFunction::paintOrigin(IRBuilder<> &IRB, Value *Origin,
  2181. Value *StoreOriginAddr,
  2182. uint64_t StoreOriginSize, Align Alignment) {
  2183. const unsigned OriginSize = DataFlowSanitizer::OriginWidthBytes;
  2184. const DataLayout &DL = F->getParent()->getDataLayout();
  2185. const Align IntptrAlignment = DL.getABITypeAlign(DFS.IntptrTy);
  2186. unsigned IntptrSize = DL.getTypeStoreSize(DFS.IntptrTy);
  2187. assert(IntptrAlignment >= MinOriginAlignment);
  2188. assert(IntptrSize >= OriginSize);
  2189. unsigned Ofs = 0;
  2190. Align CurrentAlignment = Alignment;
  2191. if (Alignment >= IntptrAlignment && IntptrSize > OriginSize) {
  2192. Value *IntptrOrigin = originToIntptr(IRB, Origin);
  2193. Value *IntptrStoreOriginPtr = IRB.CreatePointerCast(
  2194. StoreOriginAddr, PointerType::get(DFS.IntptrTy, 0));
  2195. for (unsigned I = 0; I < StoreOriginSize / IntptrSize; ++I) {
  2196. Value *Ptr =
  2197. I ? IRB.CreateConstGEP1_32(DFS.IntptrTy, IntptrStoreOriginPtr, I)
  2198. : IntptrStoreOriginPtr;
  2199. IRB.CreateAlignedStore(IntptrOrigin, Ptr, CurrentAlignment);
  2200. Ofs += IntptrSize / OriginSize;
  2201. CurrentAlignment = IntptrAlignment;
  2202. }
  2203. }
  2204. for (unsigned I = Ofs; I < (StoreOriginSize + OriginSize - 1) / OriginSize;
  2205. ++I) {
  2206. Value *GEP = I ? IRB.CreateConstGEP1_32(DFS.OriginTy, StoreOriginAddr, I)
  2207. : StoreOriginAddr;
  2208. IRB.CreateAlignedStore(Origin, GEP, CurrentAlignment);
  2209. CurrentAlignment = MinOriginAlignment;
  2210. }
  2211. }
  2212. Value *DFSanFunction::convertToBool(Value *V, IRBuilder<> &IRB,
  2213. const Twine &Name) {
  2214. Type *VTy = V->getType();
  2215. assert(VTy->isIntegerTy());
  2216. if (VTy->getIntegerBitWidth() == 1)
  2217. // Just converting a bool to a bool, so do nothing.
  2218. return V;
  2219. return IRB.CreateICmpNE(V, ConstantInt::get(VTy, 0), Name);
  2220. }
  2221. void DFSanFunction::storeOrigin(Instruction *Pos, Value *Addr, uint64_t Size,
  2222. Value *Shadow, Value *Origin,
  2223. Value *StoreOriginAddr, Align InstAlignment) {
  2224. // Do not write origins for zero shadows because we do not trace origins for
  2225. // untainted sinks.
  2226. const Align OriginAlignment = getOriginAlign(InstAlignment);
  2227. Value *CollapsedShadow = collapseToPrimitiveShadow(Shadow, Pos);
  2228. IRBuilder<> IRB(Pos);
  2229. if (auto *ConstantShadow = dyn_cast<Constant>(CollapsedShadow)) {
  2230. if (!ConstantShadow->isZeroValue())
  2231. paintOrigin(IRB, updateOrigin(Origin, IRB), StoreOriginAddr, Size,
  2232. OriginAlignment);
  2233. return;
  2234. }
  2235. if (shouldInstrumentWithCall()) {
  2236. IRB.CreateCall(DFS.DFSanMaybeStoreOriginFn,
  2237. {CollapsedShadow,
  2238. IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
  2239. ConstantInt::get(DFS.IntptrTy, Size), Origin});
  2240. } else {
  2241. Value *Cmp = convertToBool(CollapsedShadow, IRB, "_dfscmp");
  2242. Instruction *CheckTerm = SplitBlockAndInsertIfThen(
  2243. Cmp, &*IRB.GetInsertPoint(), false, DFS.OriginStoreWeights, &DT);
  2244. IRBuilder<> IRBNew(CheckTerm);
  2245. paintOrigin(IRBNew, updateOrigin(Origin, IRBNew), StoreOriginAddr, Size,
  2246. OriginAlignment);
  2247. ++NumOriginStores;
  2248. }
  2249. }
  2250. void DFSanFunction::storeZeroPrimitiveShadow(Value *Addr, uint64_t Size,
  2251. Align ShadowAlign,
  2252. Instruction *Pos) {
  2253. IRBuilder<> IRB(Pos);
  2254. IntegerType *ShadowTy =
  2255. IntegerType::get(*DFS.Ctx, Size * DFS.ShadowWidthBits);
  2256. Value *ExtZeroShadow = ConstantInt::get(ShadowTy, 0);
  2257. Value *ShadowAddr = DFS.getShadowAddress(Addr, Pos);
  2258. Value *ExtShadowAddr =
  2259. IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowTy));
  2260. IRB.CreateAlignedStore(ExtZeroShadow, ExtShadowAddr, ShadowAlign);
  2261. // Do not write origins for 0 shadows because we do not trace origins for
  2262. // untainted sinks.
  2263. }
  2264. void DFSanFunction::storePrimitiveShadowOrigin(Value *Addr, uint64_t Size,
  2265. Align InstAlignment,
  2266. Value *PrimitiveShadow,
  2267. Value *Origin,
  2268. Instruction *Pos) {
  2269. const bool ShouldTrackOrigins = DFS.shouldTrackOrigins() && Origin;
  2270. if (AllocaInst *AI = dyn_cast<AllocaInst>(Addr)) {
  2271. const auto SI = AllocaShadowMap.find(AI);
  2272. if (SI != AllocaShadowMap.end()) {
  2273. IRBuilder<> IRB(Pos);
  2274. IRB.CreateStore(PrimitiveShadow, SI->second);
  2275. // Do not write origins for 0 shadows because we do not trace origins for
  2276. // untainted sinks.
  2277. if (ShouldTrackOrigins && !DFS.isZeroShadow(PrimitiveShadow)) {
  2278. const auto OI = AllocaOriginMap.find(AI);
  2279. assert(OI != AllocaOriginMap.end() && Origin);
  2280. IRB.CreateStore(Origin, OI->second);
  2281. }
  2282. return;
  2283. }
  2284. }
  2285. const Align ShadowAlign = getShadowAlign(InstAlignment);
  2286. if (DFS.isZeroShadow(PrimitiveShadow)) {
  2287. storeZeroPrimitiveShadow(Addr, Size, ShadowAlign, Pos);
  2288. return;
  2289. }
  2290. IRBuilder<> IRB(Pos);
  2291. Value *ShadowAddr, *OriginAddr;
  2292. std::tie(ShadowAddr, OriginAddr) =
  2293. DFS.getShadowOriginAddress(Addr, InstAlignment, Pos);
  2294. const unsigned ShadowVecSize = 8;
  2295. assert(ShadowVecSize * DFS.ShadowWidthBits <= 128 &&
  2296. "Shadow vector is too large!");
  2297. uint64_t Offset = 0;
  2298. uint64_t LeftSize = Size;
  2299. if (LeftSize >= ShadowVecSize) {
  2300. auto *ShadowVecTy =
  2301. FixedVectorType::get(DFS.PrimitiveShadowTy, ShadowVecSize);
  2302. Value *ShadowVec = PoisonValue::get(ShadowVecTy);
  2303. for (unsigned I = 0; I != ShadowVecSize; ++I) {
  2304. ShadowVec = IRB.CreateInsertElement(
  2305. ShadowVec, PrimitiveShadow,
  2306. ConstantInt::get(Type::getInt32Ty(*DFS.Ctx), I));
  2307. }
  2308. Value *ShadowVecAddr =
  2309. IRB.CreateBitCast(ShadowAddr, PointerType::getUnqual(ShadowVecTy));
  2310. do {
  2311. Value *CurShadowVecAddr =
  2312. IRB.CreateConstGEP1_32(ShadowVecTy, ShadowVecAddr, Offset);
  2313. IRB.CreateAlignedStore(ShadowVec, CurShadowVecAddr, ShadowAlign);
  2314. LeftSize -= ShadowVecSize;
  2315. ++Offset;
  2316. } while (LeftSize >= ShadowVecSize);
  2317. Offset *= ShadowVecSize;
  2318. }
  2319. while (LeftSize > 0) {
  2320. Value *CurShadowAddr =
  2321. IRB.CreateConstGEP1_32(DFS.PrimitiveShadowTy, ShadowAddr, Offset);
  2322. IRB.CreateAlignedStore(PrimitiveShadow, CurShadowAddr, ShadowAlign);
  2323. --LeftSize;
  2324. ++Offset;
  2325. }
  2326. if (ShouldTrackOrigins) {
  2327. storeOrigin(Pos, Addr, Size, PrimitiveShadow, Origin, OriginAddr,
  2328. InstAlignment);
  2329. }
  2330. }
  2331. static AtomicOrdering addReleaseOrdering(AtomicOrdering AO) {
  2332. switch (AO) {
  2333. case AtomicOrdering::NotAtomic:
  2334. return AtomicOrdering::NotAtomic;
  2335. case AtomicOrdering::Unordered:
  2336. case AtomicOrdering::Monotonic:
  2337. case AtomicOrdering::Release:
  2338. return AtomicOrdering::Release;
  2339. case AtomicOrdering::Acquire:
  2340. case AtomicOrdering::AcquireRelease:
  2341. return AtomicOrdering::AcquireRelease;
  2342. case AtomicOrdering::SequentiallyConsistent:
  2343. return AtomicOrdering::SequentiallyConsistent;
  2344. }
  2345. llvm_unreachable("Unknown ordering");
  2346. }
  2347. void DFSanVisitor::visitStoreInst(StoreInst &SI) {
  2348. auto &DL = SI.getModule()->getDataLayout();
  2349. Value *Val = SI.getValueOperand();
  2350. uint64_t Size = DL.getTypeStoreSize(Val->getType());
  2351. if (Size == 0)
  2352. return;
  2353. // When an application store is atomic, increase atomic ordering between
  2354. // atomic application loads and stores to ensure happen-before order; load
  2355. // shadow data after application data; store zero shadow data before
  2356. // application data. This ensure shadow loads return either labels of the
  2357. // initial application data or zeros.
  2358. if (SI.isAtomic())
  2359. SI.setOrdering(addReleaseOrdering(SI.getOrdering()));
  2360. const bool ShouldTrackOrigins =
  2361. DFSF.DFS.shouldTrackOrigins() && !SI.isAtomic();
  2362. std::vector<Value *> Shadows;
  2363. std::vector<Value *> Origins;
  2364. Value *Shadow =
  2365. SI.isAtomic() ? DFSF.DFS.getZeroShadow(Val) : DFSF.getShadow(Val);
  2366. if (ShouldTrackOrigins) {
  2367. Shadows.push_back(Shadow);
  2368. Origins.push_back(DFSF.getOrigin(Val));
  2369. }
  2370. Value *PrimitiveShadow;
  2371. if (ClCombinePointerLabelsOnStore) {
  2372. Value *PtrShadow = DFSF.getShadow(SI.getPointerOperand());
  2373. if (ShouldTrackOrigins) {
  2374. Shadows.push_back(PtrShadow);
  2375. Origins.push_back(DFSF.getOrigin(SI.getPointerOperand()));
  2376. }
  2377. PrimitiveShadow = DFSF.combineShadows(Shadow, PtrShadow, &SI);
  2378. } else {
  2379. PrimitiveShadow = DFSF.collapseToPrimitiveShadow(Shadow, &SI);
  2380. }
  2381. Value *Origin = nullptr;
  2382. if (ShouldTrackOrigins)
  2383. Origin = DFSF.combineOrigins(Shadows, Origins, &SI);
  2384. DFSF.storePrimitiveShadowOrigin(SI.getPointerOperand(), Size, SI.getAlign(),
  2385. PrimitiveShadow, Origin, &SI);
  2386. if (ClEventCallbacks) {
  2387. IRBuilder<> IRB(&SI);
  2388. Value *Addr8 = IRB.CreateBitCast(SI.getPointerOperand(), DFSF.DFS.Int8Ptr);
  2389. CallInst *CI =
  2390. IRB.CreateCall(DFSF.DFS.DFSanStoreCallbackFn, {PrimitiveShadow, Addr8});
  2391. CI->addParamAttr(0, Attribute::ZExt);
  2392. }
  2393. }
  2394. void DFSanVisitor::visitCASOrRMW(Align InstAlignment, Instruction &I) {
  2395. assert(isa<AtomicRMWInst>(I) || isa<AtomicCmpXchgInst>(I));
  2396. Value *Val = I.getOperand(1);
  2397. const auto &DL = I.getModule()->getDataLayout();
  2398. uint64_t Size = DL.getTypeStoreSize(Val->getType());
  2399. if (Size == 0)
  2400. return;
  2401. // Conservatively set data at stored addresses and return with zero shadow to
  2402. // prevent shadow data races.
  2403. IRBuilder<> IRB(&I);
  2404. Value *Addr = I.getOperand(0);
  2405. const Align ShadowAlign = DFSF.getShadowAlign(InstAlignment);
  2406. DFSF.storeZeroPrimitiveShadow(Addr, Size, ShadowAlign, &I);
  2407. DFSF.setShadow(&I, DFSF.DFS.getZeroShadow(&I));
  2408. DFSF.setOrigin(&I, DFSF.DFS.ZeroOrigin);
  2409. }
  2410. void DFSanVisitor::visitAtomicRMWInst(AtomicRMWInst &I) {
  2411. visitCASOrRMW(I.getAlign(), I);
  2412. // TODO: The ordering change follows MSan. It is possible not to change
  2413. // ordering because we always set and use 0 shadows.
  2414. I.setOrdering(addReleaseOrdering(I.getOrdering()));
  2415. }
  2416. void DFSanVisitor::visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
  2417. visitCASOrRMW(I.getAlign(), I);
  2418. // TODO: The ordering change follows MSan. It is possible not to change
  2419. // ordering because we always set and use 0 shadows.
  2420. I.setSuccessOrdering(addReleaseOrdering(I.getSuccessOrdering()));
  2421. }
  2422. void DFSanVisitor::visitUnaryOperator(UnaryOperator &UO) {
  2423. visitInstOperands(UO);
  2424. }
  2425. void DFSanVisitor::visitBinaryOperator(BinaryOperator &BO) {
  2426. visitInstOperands(BO);
  2427. }
  2428. void DFSanVisitor::visitBitCastInst(BitCastInst &BCI) {
  2429. // Special case: if this is the bitcast (there is exactly 1 allowed) between
  2430. // a musttail call and a ret, don't instrument. New instructions are not
  2431. // allowed after a musttail call.
  2432. if (auto *CI = dyn_cast<CallInst>(BCI.getOperand(0)))
  2433. if (CI->isMustTailCall())
  2434. return;
  2435. visitInstOperands(BCI);
  2436. }
  2437. void DFSanVisitor::visitCastInst(CastInst &CI) { visitInstOperands(CI); }
  2438. void DFSanVisitor::visitCmpInst(CmpInst &CI) {
  2439. visitInstOperands(CI);
  2440. if (ClEventCallbacks) {
  2441. IRBuilder<> IRB(&CI);
  2442. Value *CombinedShadow = DFSF.getShadow(&CI);
  2443. CallInst *CallI =
  2444. IRB.CreateCall(DFSF.DFS.DFSanCmpCallbackFn, CombinedShadow);
  2445. CallI->addParamAttr(0, Attribute::ZExt);
  2446. }
  2447. }
  2448. void DFSanVisitor::visitLandingPadInst(LandingPadInst &LPI) {
  2449. // We do not need to track data through LandingPadInst.
  2450. //
  2451. // For the C++ exceptions, if a value is thrown, this value will be stored
  2452. // in a memory location provided by __cxa_allocate_exception(...) (on the
  2453. // throw side) or __cxa_begin_catch(...) (on the catch side).
  2454. // This memory will have a shadow, so with the loads and stores we will be
  2455. // able to propagate labels on data thrown through exceptions, without any
  2456. // special handling of the LandingPadInst.
  2457. //
  2458. // The second element in the pair result of the LandingPadInst is a
  2459. // register value, but it is for a type ID and should never be tainted.
  2460. DFSF.setShadow(&LPI, DFSF.DFS.getZeroShadow(&LPI));
  2461. DFSF.setOrigin(&LPI, DFSF.DFS.ZeroOrigin);
  2462. }
  2463. void DFSanVisitor::visitGetElementPtrInst(GetElementPtrInst &GEPI) {
  2464. if (ClCombineOffsetLabelsOnGEP ||
  2465. DFSF.isLookupTableConstant(
  2466. StripPointerGEPsAndCasts(GEPI.getPointerOperand()))) {
  2467. visitInstOperands(GEPI);
  2468. return;
  2469. }
  2470. // Only propagate shadow/origin of base pointer value but ignore those of
  2471. // offset operands.
  2472. Value *BasePointer = GEPI.getPointerOperand();
  2473. DFSF.setShadow(&GEPI, DFSF.getShadow(BasePointer));
  2474. if (DFSF.DFS.shouldTrackOrigins())
  2475. DFSF.setOrigin(&GEPI, DFSF.getOrigin(BasePointer));
  2476. }
  2477. void DFSanVisitor::visitExtractElementInst(ExtractElementInst &I) {
  2478. visitInstOperands(I);
  2479. }
  2480. void DFSanVisitor::visitInsertElementInst(InsertElementInst &I) {
  2481. visitInstOperands(I);
  2482. }
  2483. void DFSanVisitor::visitShuffleVectorInst(ShuffleVectorInst &I) {
  2484. visitInstOperands(I);
  2485. }
  2486. void DFSanVisitor::visitExtractValueInst(ExtractValueInst &I) {
  2487. IRBuilder<> IRB(&I);
  2488. Value *Agg = I.getAggregateOperand();
  2489. Value *AggShadow = DFSF.getShadow(Agg);
  2490. Value *ResShadow = IRB.CreateExtractValue(AggShadow, I.getIndices());
  2491. DFSF.setShadow(&I, ResShadow);
  2492. visitInstOperandOrigins(I);
  2493. }
  2494. void DFSanVisitor::visitInsertValueInst(InsertValueInst &I) {
  2495. IRBuilder<> IRB(&I);
  2496. Value *AggShadow = DFSF.getShadow(I.getAggregateOperand());
  2497. Value *InsShadow = DFSF.getShadow(I.getInsertedValueOperand());
  2498. Value *Res = IRB.CreateInsertValue(AggShadow, InsShadow, I.getIndices());
  2499. DFSF.setShadow(&I, Res);
  2500. visitInstOperandOrigins(I);
  2501. }
  2502. void DFSanVisitor::visitAllocaInst(AllocaInst &I) {
  2503. bool AllLoadsStores = true;
  2504. for (User *U : I.users()) {
  2505. if (isa<LoadInst>(U))
  2506. continue;
  2507. if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
  2508. if (SI->getPointerOperand() == &I)
  2509. continue;
  2510. }
  2511. AllLoadsStores = false;
  2512. break;
  2513. }
  2514. if (AllLoadsStores) {
  2515. IRBuilder<> IRB(&I);
  2516. DFSF.AllocaShadowMap[&I] = IRB.CreateAlloca(DFSF.DFS.PrimitiveShadowTy);
  2517. if (DFSF.DFS.shouldTrackOrigins()) {
  2518. DFSF.AllocaOriginMap[&I] =
  2519. IRB.CreateAlloca(DFSF.DFS.OriginTy, nullptr, "_dfsa");
  2520. }
  2521. }
  2522. DFSF.setShadow(&I, DFSF.DFS.ZeroPrimitiveShadow);
  2523. DFSF.setOrigin(&I, DFSF.DFS.ZeroOrigin);
  2524. }
  2525. void DFSanVisitor::visitSelectInst(SelectInst &I) {
  2526. Value *CondShadow = DFSF.getShadow(I.getCondition());
  2527. Value *TrueShadow = DFSF.getShadow(I.getTrueValue());
  2528. Value *FalseShadow = DFSF.getShadow(I.getFalseValue());
  2529. Value *ShadowSel = nullptr;
  2530. const bool ShouldTrackOrigins = DFSF.DFS.shouldTrackOrigins();
  2531. std::vector<Value *> Shadows;
  2532. std::vector<Value *> Origins;
  2533. Value *TrueOrigin =
  2534. ShouldTrackOrigins ? DFSF.getOrigin(I.getTrueValue()) : nullptr;
  2535. Value *FalseOrigin =
  2536. ShouldTrackOrigins ? DFSF.getOrigin(I.getFalseValue()) : nullptr;
  2537. DFSF.addConditionalCallbacksIfEnabled(I, I.getCondition());
  2538. if (isa<VectorType>(I.getCondition()->getType())) {
  2539. ShadowSel = DFSF.combineShadowsThenConvert(I.getType(), TrueShadow,
  2540. FalseShadow, &I);
  2541. if (ShouldTrackOrigins) {
  2542. Shadows.push_back(TrueShadow);
  2543. Shadows.push_back(FalseShadow);
  2544. Origins.push_back(TrueOrigin);
  2545. Origins.push_back(FalseOrigin);
  2546. }
  2547. } else {
  2548. if (TrueShadow == FalseShadow) {
  2549. ShadowSel = TrueShadow;
  2550. if (ShouldTrackOrigins) {
  2551. Shadows.push_back(TrueShadow);
  2552. Origins.push_back(TrueOrigin);
  2553. }
  2554. } else {
  2555. ShadowSel =
  2556. SelectInst::Create(I.getCondition(), TrueShadow, FalseShadow, "", &I);
  2557. if (ShouldTrackOrigins) {
  2558. Shadows.push_back(ShadowSel);
  2559. Origins.push_back(SelectInst::Create(I.getCondition(), TrueOrigin,
  2560. FalseOrigin, "", &I));
  2561. }
  2562. }
  2563. }
  2564. DFSF.setShadow(&I, ClTrackSelectControlFlow
  2565. ? DFSF.combineShadowsThenConvert(
  2566. I.getType(), CondShadow, ShadowSel, &I)
  2567. : ShadowSel);
  2568. if (ShouldTrackOrigins) {
  2569. if (ClTrackSelectControlFlow) {
  2570. Shadows.push_back(CondShadow);
  2571. Origins.push_back(DFSF.getOrigin(I.getCondition()));
  2572. }
  2573. DFSF.setOrigin(&I, DFSF.combineOrigins(Shadows, Origins, &I));
  2574. }
  2575. }
  2576. void DFSanVisitor::visitMemSetInst(MemSetInst &I) {
  2577. IRBuilder<> IRB(&I);
  2578. Value *ValShadow = DFSF.getShadow(I.getValue());
  2579. Value *ValOrigin = DFSF.DFS.shouldTrackOrigins()
  2580. ? DFSF.getOrigin(I.getValue())
  2581. : DFSF.DFS.ZeroOrigin;
  2582. IRB.CreateCall(
  2583. DFSF.DFS.DFSanSetLabelFn,
  2584. {ValShadow, ValOrigin,
  2585. IRB.CreateBitCast(I.getDest(), Type::getInt8PtrTy(*DFSF.DFS.Ctx)),
  2586. IRB.CreateZExtOrTrunc(I.getLength(), DFSF.DFS.IntptrTy)});
  2587. }
  2588. void DFSanVisitor::visitMemTransferInst(MemTransferInst &I) {
  2589. IRBuilder<> IRB(&I);
  2590. // CopyOrMoveOrigin transfers origins by refering to their shadows. So we
  2591. // need to move origins before moving shadows.
  2592. if (DFSF.DFS.shouldTrackOrigins()) {
  2593. IRB.CreateCall(
  2594. DFSF.DFS.DFSanMemOriginTransferFn,
  2595. {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
  2596. IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()),
  2597. IRB.CreateIntCast(I.getArgOperand(2), DFSF.DFS.IntptrTy, false)});
  2598. }
  2599. Value *RawDestShadow = DFSF.DFS.getShadowAddress(I.getDest(), &I);
  2600. Value *SrcShadow = DFSF.DFS.getShadowAddress(I.getSource(), &I);
  2601. Value *LenShadow =
  2602. IRB.CreateMul(I.getLength(), ConstantInt::get(I.getLength()->getType(),
  2603. DFSF.DFS.ShadowWidthBytes));
  2604. Type *Int8Ptr = Type::getInt8PtrTy(*DFSF.DFS.Ctx);
  2605. Value *DestShadow = IRB.CreateBitCast(RawDestShadow, Int8Ptr);
  2606. SrcShadow = IRB.CreateBitCast(SrcShadow, Int8Ptr);
  2607. auto *MTI = cast<MemTransferInst>(
  2608. IRB.CreateCall(I.getFunctionType(), I.getCalledOperand(),
  2609. {DestShadow, SrcShadow, LenShadow, I.getVolatileCst()}));
  2610. MTI->setDestAlignment(DFSF.getShadowAlign(I.getDestAlign().valueOrOne()));
  2611. MTI->setSourceAlignment(DFSF.getShadowAlign(I.getSourceAlign().valueOrOne()));
  2612. if (ClEventCallbacks) {
  2613. IRB.CreateCall(DFSF.DFS.DFSanMemTransferCallbackFn,
  2614. {RawDestShadow,
  2615. IRB.CreateZExtOrTrunc(I.getLength(), DFSF.DFS.IntptrTy)});
  2616. }
  2617. }
  2618. void DFSanVisitor::visitBranchInst(BranchInst &BR) {
  2619. if (!BR.isConditional())
  2620. return;
  2621. DFSF.addConditionalCallbacksIfEnabled(BR, BR.getCondition());
  2622. }
  2623. void DFSanVisitor::visitSwitchInst(SwitchInst &SW) {
  2624. DFSF.addConditionalCallbacksIfEnabled(SW, SW.getCondition());
  2625. }
  2626. static bool isAMustTailRetVal(Value *RetVal) {
  2627. // Tail call may have a bitcast between return.
  2628. if (auto *I = dyn_cast<BitCastInst>(RetVal)) {
  2629. RetVal = I->getOperand(0);
  2630. }
  2631. if (auto *I = dyn_cast<CallInst>(RetVal)) {
  2632. return I->isMustTailCall();
  2633. }
  2634. return false;
  2635. }
  2636. void DFSanVisitor::visitReturnInst(ReturnInst &RI) {
  2637. if (!DFSF.IsNativeABI && RI.getReturnValue()) {
  2638. // Don't emit the instrumentation for musttail call returns.
  2639. if (isAMustTailRetVal(RI.getReturnValue()))
  2640. return;
  2641. Value *S = DFSF.getShadow(RI.getReturnValue());
  2642. IRBuilder<> IRB(&RI);
  2643. Type *RT = DFSF.F->getFunctionType()->getReturnType();
  2644. unsigned Size = getDataLayout().getTypeAllocSize(DFSF.DFS.getShadowTy(RT));
  2645. if (Size <= RetvalTLSSize) {
  2646. // If the size overflows, stores nothing. At callsite, oversized return
  2647. // shadows are set to zero.
  2648. IRB.CreateAlignedStore(S, DFSF.getRetvalTLS(RT, IRB), ShadowTLSAlignment);
  2649. }
  2650. if (DFSF.DFS.shouldTrackOrigins()) {
  2651. Value *O = DFSF.getOrigin(RI.getReturnValue());
  2652. IRB.CreateStore(O, DFSF.getRetvalOriginTLS());
  2653. }
  2654. }
  2655. }
  2656. void DFSanVisitor::addShadowArguments(Function &F, CallBase &CB,
  2657. std::vector<Value *> &Args,
  2658. IRBuilder<> &IRB) {
  2659. FunctionType *FT = F.getFunctionType();
  2660. auto *I = CB.arg_begin();
  2661. // Adds non-variable argument shadows.
  2662. for (unsigned N = FT->getNumParams(); N != 0; ++I, --N)
  2663. Args.push_back(DFSF.collapseToPrimitiveShadow(DFSF.getShadow(*I), &CB));
  2664. // Adds variable argument shadows.
  2665. if (FT->isVarArg()) {
  2666. auto *LabelVATy = ArrayType::get(DFSF.DFS.PrimitiveShadowTy,
  2667. CB.arg_size() - FT->getNumParams());
  2668. auto *LabelVAAlloca =
  2669. new AllocaInst(LabelVATy, getDataLayout().getAllocaAddrSpace(),
  2670. "labelva", &DFSF.F->getEntryBlock().front());
  2671. for (unsigned N = 0; I != CB.arg_end(); ++I, ++N) {
  2672. auto *LabelVAPtr = IRB.CreateStructGEP(LabelVATy, LabelVAAlloca, N);
  2673. IRB.CreateStore(DFSF.collapseToPrimitiveShadow(DFSF.getShadow(*I), &CB),
  2674. LabelVAPtr);
  2675. }
  2676. Args.push_back(IRB.CreateStructGEP(LabelVATy, LabelVAAlloca, 0));
  2677. }
  2678. // Adds the return value shadow.
  2679. if (!FT->getReturnType()->isVoidTy()) {
  2680. if (!DFSF.LabelReturnAlloca) {
  2681. DFSF.LabelReturnAlloca = new AllocaInst(
  2682. DFSF.DFS.PrimitiveShadowTy, getDataLayout().getAllocaAddrSpace(),
  2683. "labelreturn", &DFSF.F->getEntryBlock().front());
  2684. }
  2685. Args.push_back(DFSF.LabelReturnAlloca);
  2686. }
  2687. }
  2688. void DFSanVisitor::addOriginArguments(Function &F, CallBase &CB,
  2689. std::vector<Value *> &Args,
  2690. IRBuilder<> &IRB) {
  2691. FunctionType *FT = F.getFunctionType();
  2692. auto *I = CB.arg_begin();
  2693. // Add non-variable argument origins.
  2694. for (unsigned N = FT->getNumParams(); N != 0; ++I, --N)
  2695. Args.push_back(DFSF.getOrigin(*I));
  2696. // Add variable argument origins.
  2697. if (FT->isVarArg()) {
  2698. auto *OriginVATy =
  2699. ArrayType::get(DFSF.DFS.OriginTy, CB.arg_size() - FT->getNumParams());
  2700. auto *OriginVAAlloca =
  2701. new AllocaInst(OriginVATy, getDataLayout().getAllocaAddrSpace(),
  2702. "originva", &DFSF.F->getEntryBlock().front());
  2703. for (unsigned N = 0; I != CB.arg_end(); ++I, ++N) {
  2704. auto *OriginVAPtr = IRB.CreateStructGEP(OriginVATy, OriginVAAlloca, N);
  2705. IRB.CreateStore(DFSF.getOrigin(*I), OriginVAPtr);
  2706. }
  2707. Args.push_back(IRB.CreateStructGEP(OriginVATy, OriginVAAlloca, 0));
  2708. }
  2709. // Add the return value origin.
  2710. if (!FT->getReturnType()->isVoidTy()) {
  2711. if (!DFSF.OriginReturnAlloca) {
  2712. DFSF.OriginReturnAlloca = new AllocaInst(
  2713. DFSF.DFS.OriginTy, getDataLayout().getAllocaAddrSpace(),
  2714. "originreturn", &DFSF.F->getEntryBlock().front());
  2715. }
  2716. Args.push_back(DFSF.OriginReturnAlloca);
  2717. }
  2718. }
  2719. bool DFSanVisitor::visitWrappedCallBase(Function &F, CallBase &CB) {
  2720. IRBuilder<> IRB(&CB);
  2721. switch (DFSF.DFS.getWrapperKind(&F)) {
  2722. case DataFlowSanitizer::WK_Warning:
  2723. CB.setCalledFunction(&F);
  2724. IRB.CreateCall(DFSF.DFS.DFSanUnimplementedFn,
  2725. IRB.CreateGlobalStringPtr(F.getName()));
  2726. DFSF.DFS.buildExternWeakCheckIfNeeded(IRB, &F);
  2727. DFSF.setShadow(&CB, DFSF.DFS.getZeroShadow(&CB));
  2728. DFSF.setOrigin(&CB, DFSF.DFS.ZeroOrigin);
  2729. return true;
  2730. case DataFlowSanitizer::WK_Discard:
  2731. CB.setCalledFunction(&F);
  2732. DFSF.DFS.buildExternWeakCheckIfNeeded(IRB, &F);
  2733. DFSF.setShadow(&CB, DFSF.DFS.getZeroShadow(&CB));
  2734. DFSF.setOrigin(&CB, DFSF.DFS.ZeroOrigin);
  2735. return true;
  2736. case DataFlowSanitizer::WK_Functional:
  2737. CB.setCalledFunction(&F);
  2738. DFSF.DFS.buildExternWeakCheckIfNeeded(IRB, &F);
  2739. visitInstOperands(CB);
  2740. return true;
  2741. case DataFlowSanitizer::WK_Custom:
  2742. // Don't try to handle invokes of custom functions, it's too complicated.
  2743. // Instead, invoke the dfsw$ wrapper, which will in turn call the __dfsw_
  2744. // wrapper.
  2745. CallInst *CI = dyn_cast<CallInst>(&CB);
  2746. if (!CI)
  2747. return false;
  2748. const bool ShouldTrackOrigins = DFSF.DFS.shouldTrackOrigins();
  2749. FunctionType *FT = F.getFunctionType();
  2750. TransformedFunction CustomFn = DFSF.DFS.getCustomFunctionType(FT);
  2751. std::string CustomFName = ShouldTrackOrigins ? "__dfso_" : "__dfsw_";
  2752. CustomFName += F.getName();
  2753. FunctionCallee CustomF = DFSF.DFS.Mod->getOrInsertFunction(
  2754. CustomFName, CustomFn.TransformedType);
  2755. if (Function *CustomFn = dyn_cast<Function>(CustomF.getCallee())) {
  2756. CustomFn->copyAttributesFrom(&F);
  2757. // Custom functions returning non-void will write to the return label.
  2758. if (!FT->getReturnType()->isVoidTy()) {
  2759. CustomFn->removeFnAttrs(DFSF.DFS.ReadOnlyNoneAttrs);
  2760. }
  2761. }
  2762. std::vector<Value *> Args;
  2763. // Adds non-variable arguments.
  2764. auto *I = CB.arg_begin();
  2765. for (unsigned N = FT->getNumParams(); N != 0; ++I, --N) {
  2766. Args.push_back(*I);
  2767. }
  2768. // Adds shadow arguments.
  2769. const unsigned ShadowArgStart = Args.size();
  2770. addShadowArguments(F, CB, Args, IRB);
  2771. // Adds origin arguments.
  2772. const unsigned OriginArgStart = Args.size();
  2773. if (ShouldTrackOrigins)
  2774. addOriginArguments(F, CB, Args, IRB);
  2775. // Adds variable arguments.
  2776. append_range(Args, drop_begin(CB.args(), FT->getNumParams()));
  2777. CallInst *CustomCI = IRB.CreateCall(CustomF, Args);
  2778. CustomCI->setCallingConv(CI->getCallingConv());
  2779. CustomCI->setAttributes(transformFunctionAttributes(
  2780. CustomFn, CI->getContext(), CI->getAttributes()));
  2781. // Update the parameter attributes of the custom call instruction to
  2782. // zero extend the shadow parameters. This is required for targets
  2783. // which consider PrimitiveShadowTy an illegal type.
  2784. for (unsigned N = 0; N < FT->getNumParams(); N++) {
  2785. const unsigned ArgNo = ShadowArgStart + N;
  2786. if (CustomCI->getArgOperand(ArgNo)->getType() ==
  2787. DFSF.DFS.PrimitiveShadowTy)
  2788. CustomCI->addParamAttr(ArgNo, Attribute::ZExt);
  2789. if (ShouldTrackOrigins) {
  2790. const unsigned OriginArgNo = OriginArgStart + N;
  2791. if (CustomCI->getArgOperand(OriginArgNo)->getType() ==
  2792. DFSF.DFS.OriginTy)
  2793. CustomCI->addParamAttr(OriginArgNo, Attribute::ZExt);
  2794. }
  2795. }
  2796. // Loads the return value shadow and origin.
  2797. if (!FT->getReturnType()->isVoidTy()) {
  2798. LoadInst *LabelLoad =
  2799. IRB.CreateLoad(DFSF.DFS.PrimitiveShadowTy, DFSF.LabelReturnAlloca);
  2800. DFSF.setShadow(CustomCI, DFSF.expandFromPrimitiveShadow(
  2801. FT->getReturnType(), LabelLoad, &CB));
  2802. if (ShouldTrackOrigins) {
  2803. LoadInst *OriginLoad =
  2804. IRB.CreateLoad(DFSF.DFS.OriginTy, DFSF.OriginReturnAlloca);
  2805. DFSF.setOrigin(CustomCI, OriginLoad);
  2806. }
  2807. }
  2808. CI->replaceAllUsesWith(CustomCI);
  2809. CI->eraseFromParent();
  2810. return true;
  2811. }
  2812. return false;
  2813. }
  2814. Value *DFSanVisitor::makeAddAcquireOrderingTable(IRBuilder<> &IRB) {
  2815. constexpr int NumOrderings = (int)AtomicOrderingCABI::seq_cst + 1;
  2816. uint32_t OrderingTable[NumOrderings] = {};
  2817. OrderingTable[(int)AtomicOrderingCABI::relaxed] =
  2818. OrderingTable[(int)AtomicOrderingCABI::acquire] =
  2819. OrderingTable[(int)AtomicOrderingCABI::consume] =
  2820. (int)AtomicOrderingCABI::acquire;
  2821. OrderingTable[(int)AtomicOrderingCABI::release] =
  2822. OrderingTable[(int)AtomicOrderingCABI::acq_rel] =
  2823. (int)AtomicOrderingCABI::acq_rel;
  2824. OrderingTable[(int)AtomicOrderingCABI::seq_cst] =
  2825. (int)AtomicOrderingCABI::seq_cst;
  2826. return ConstantDataVector::get(IRB.getContext(),
  2827. ArrayRef(OrderingTable, NumOrderings));
  2828. }
  2829. void DFSanVisitor::visitLibAtomicLoad(CallBase &CB) {
  2830. // Since we use getNextNode here, we can't have CB terminate the BB.
  2831. assert(isa<CallInst>(CB));
  2832. IRBuilder<> IRB(&CB);
  2833. Value *Size = CB.getArgOperand(0);
  2834. Value *SrcPtr = CB.getArgOperand(1);
  2835. Value *DstPtr = CB.getArgOperand(2);
  2836. Value *Ordering = CB.getArgOperand(3);
  2837. // Convert the call to have at least Acquire ordering to make sure
  2838. // the shadow operations aren't reordered before it.
  2839. Value *NewOrdering =
  2840. IRB.CreateExtractElement(makeAddAcquireOrderingTable(IRB), Ordering);
  2841. CB.setArgOperand(3, NewOrdering);
  2842. IRBuilder<> NextIRB(CB.getNextNode());
  2843. NextIRB.SetCurrentDebugLocation(CB.getDebugLoc());
  2844. // TODO: Support ClCombinePointerLabelsOnLoad
  2845. // TODO: Support ClEventCallbacks
  2846. NextIRB.CreateCall(DFSF.DFS.DFSanMemShadowOriginTransferFn,
  2847. {NextIRB.CreatePointerCast(DstPtr, NextIRB.getInt8PtrTy()),
  2848. NextIRB.CreatePointerCast(SrcPtr, NextIRB.getInt8PtrTy()),
  2849. NextIRB.CreateIntCast(Size, DFSF.DFS.IntptrTy, false)});
  2850. }
  2851. Value *DFSanVisitor::makeAddReleaseOrderingTable(IRBuilder<> &IRB) {
  2852. constexpr int NumOrderings = (int)AtomicOrderingCABI::seq_cst + 1;
  2853. uint32_t OrderingTable[NumOrderings] = {};
  2854. OrderingTable[(int)AtomicOrderingCABI::relaxed] =
  2855. OrderingTable[(int)AtomicOrderingCABI::release] =
  2856. (int)AtomicOrderingCABI::release;
  2857. OrderingTable[(int)AtomicOrderingCABI::consume] =
  2858. OrderingTable[(int)AtomicOrderingCABI::acquire] =
  2859. OrderingTable[(int)AtomicOrderingCABI::acq_rel] =
  2860. (int)AtomicOrderingCABI::acq_rel;
  2861. OrderingTable[(int)AtomicOrderingCABI::seq_cst] =
  2862. (int)AtomicOrderingCABI::seq_cst;
  2863. return ConstantDataVector::get(IRB.getContext(),
  2864. ArrayRef(OrderingTable, NumOrderings));
  2865. }
  2866. void DFSanVisitor::visitLibAtomicStore(CallBase &CB) {
  2867. IRBuilder<> IRB(&CB);
  2868. Value *Size = CB.getArgOperand(0);
  2869. Value *SrcPtr = CB.getArgOperand(1);
  2870. Value *DstPtr = CB.getArgOperand(2);
  2871. Value *Ordering = CB.getArgOperand(3);
  2872. // Convert the call to have at least Release ordering to make sure
  2873. // the shadow operations aren't reordered after it.
  2874. Value *NewOrdering =
  2875. IRB.CreateExtractElement(makeAddReleaseOrderingTable(IRB), Ordering);
  2876. CB.setArgOperand(3, NewOrdering);
  2877. // TODO: Support ClCombinePointerLabelsOnStore
  2878. // TODO: Support ClEventCallbacks
  2879. IRB.CreateCall(DFSF.DFS.DFSanMemShadowOriginTransferFn,
  2880. {IRB.CreatePointerCast(DstPtr, IRB.getInt8PtrTy()),
  2881. IRB.CreatePointerCast(SrcPtr, IRB.getInt8PtrTy()),
  2882. IRB.CreateIntCast(Size, DFSF.DFS.IntptrTy, false)});
  2883. }
  2884. void DFSanVisitor::visitLibAtomicExchange(CallBase &CB) {
  2885. // void __atomic_exchange(size_t size, void *ptr, void *val, void *ret, int
  2886. // ordering)
  2887. IRBuilder<> IRB(&CB);
  2888. Value *Size = CB.getArgOperand(0);
  2889. Value *TargetPtr = CB.getArgOperand(1);
  2890. Value *SrcPtr = CB.getArgOperand(2);
  2891. Value *DstPtr = CB.getArgOperand(3);
  2892. // This operation is not atomic for the shadow and origin memory.
  2893. // This could result in DFSan false positives or false negatives.
  2894. // For now we will assume these operations are rare, and
  2895. // the additional complexity to address this is not warrented.
  2896. // Current Target to Dest
  2897. IRB.CreateCall(DFSF.DFS.DFSanMemShadowOriginTransferFn,
  2898. {IRB.CreatePointerCast(DstPtr, IRB.getInt8PtrTy()),
  2899. IRB.CreatePointerCast(TargetPtr, IRB.getInt8PtrTy()),
  2900. IRB.CreateIntCast(Size, DFSF.DFS.IntptrTy, false)});
  2901. // Current Src to Target (overriding)
  2902. IRB.CreateCall(DFSF.DFS.DFSanMemShadowOriginTransferFn,
  2903. {IRB.CreatePointerCast(TargetPtr, IRB.getInt8PtrTy()),
  2904. IRB.CreatePointerCast(SrcPtr, IRB.getInt8PtrTy()),
  2905. IRB.CreateIntCast(Size, DFSF.DFS.IntptrTy, false)});
  2906. }
  2907. void DFSanVisitor::visitLibAtomicCompareExchange(CallBase &CB) {
  2908. // bool __atomic_compare_exchange(size_t size, void *ptr, void *expected, void
  2909. // *desired, int success_order, int failure_order)
  2910. Value *Size = CB.getArgOperand(0);
  2911. Value *TargetPtr = CB.getArgOperand(1);
  2912. Value *ExpectedPtr = CB.getArgOperand(2);
  2913. Value *DesiredPtr = CB.getArgOperand(3);
  2914. // This operation is not atomic for the shadow and origin memory.
  2915. // This could result in DFSan false positives or false negatives.
  2916. // For now we will assume these operations are rare, and
  2917. // the additional complexity to address this is not warrented.
  2918. IRBuilder<> NextIRB(CB.getNextNode());
  2919. NextIRB.SetCurrentDebugLocation(CB.getDebugLoc());
  2920. DFSF.setShadow(&CB, DFSF.DFS.getZeroShadow(&CB));
  2921. // If original call returned true, copy Desired to Target.
  2922. // If original call returned false, copy Target to Expected.
  2923. NextIRB.CreateCall(
  2924. DFSF.DFS.DFSanMemShadowOriginConditionalExchangeFn,
  2925. {NextIRB.CreateIntCast(&CB, NextIRB.getInt8Ty(), false),
  2926. NextIRB.CreatePointerCast(TargetPtr, NextIRB.getInt8PtrTy()),
  2927. NextIRB.CreatePointerCast(ExpectedPtr, NextIRB.getInt8PtrTy()),
  2928. NextIRB.CreatePointerCast(DesiredPtr, NextIRB.getInt8PtrTy()),
  2929. NextIRB.CreateIntCast(Size, DFSF.DFS.IntptrTy, false)});
  2930. }
  2931. void DFSanVisitor::visitCallBase(CallBase &CB) {
  2932. Function *F = CB.getCalledFunction();
  2933. if ((F && F->isIntrinsic()) || CB.isInlineAsm()) {
  2934. visitInstOperands(CB);
  2935. return;
  2936. }
  2937. // Calls to this function are synthesized in wrappers, and we shouldn't
  2938. // instrument them.
  2939. if (F == DFSF.DFS.DFSanVarargWrapperFn.getCallee()->stripPointerCasts())
  2940. return;
  2941. LibFunc LF;
  2942. if (DFSF.TLI.getLibFunc(CB, LF)) {
  2943. // libatomic.a functions need to have special handling because there isn't
  2944. // a good way to intercept them or compile the library with
  2945. // instrumentation.
  2946. switch (LF) {
  2947. case LibFunc_atomic_load:
  2948. if (!isa<CallInst>(CB)) {
  2949. llvm::errs() << "DFSAN -- cannot instrument invoke of libatomic load. "
  2950. "Ignoring!\n";
  2951. break;
  2952. }
  2953. visitLibAtomicLoad(CB);
  2954. return;
  2955. case LibFunc_atomic_store:
  2956. visitLibAtomicStore(CB);
  2957. return;
  2958. default:
  2959. break;
  2960. }
  2961. }
  2962. // TODO: These are not supported by TLI? They are not in the enum.
  2963. if (F && F->hasName() && !F->isVarArg()) {
  2964. if (F->getName() == "__atomic_exchange") {
  2965. visitLibAtomicExchange(CB);
  2966. return;
  2967. }
  2968. if (F->getName() == "__atomic_compare_exchange") {
  2969. visitLibAtomicCompareExchange(CB);
  2970. return;
  2971. }
  2972. }
  2973. DenseMap<Value *, Function *>::iterator UnwrappedFnIt =
  2974. DFSF.DFS.UnwrappedFnMap.find(CB.getCalledOperand());
  2975. if (UnwrappedFnIt != DFSF.DFS.UnwrappedFnMap.end())
  2976. if (visitWrappedCallBase(*UnwrappedFnIt->second, CB))
  2977. return;
  2978. IRBuilder<> IRB(&CB);
  2979. const bool ShouldTrackOrigins = DFSF.DFS.shouldTrackOrigins();
  2980. FunctionType *FT = CB.getFunctionType();
  2981. const DataLayout &DL = getDataLayout();
  2982. // Stores argument shadows.
  2983. unsigned ArgOffset = 0;
  2984. for (unsigned I = 0, N = FT->getNumParams(); I != N; ++I) {
  2985. if (ShouldTrackOrigins) {
  2986. // Ignore overflowed origins
  2987. Value *ArgShadow = DFSF.getShadow(CB.getArgOperand(I));
  2988. if (I < DFSF.DFS.NumOfElementsInArgOrgTLS &&
  2989. !DFSF.DFS.isZeroShadow(ArgShadow))
  2990. IRB.CreateStore(DFSF.getOrigin(CB.getArgOperand(I)),
  2991. DFSF.getArgOriginTLS(I, IRB));
  2992. }
  2993. unsigned Size =
  2994. DL.getTypeAllocSize(DFSF.DFS.getShadowTy(FT->getParamType(I)));
  2995. // Stop storing if arguments' size overflows. Inside a function, arguments
  2996. // after overflow have zero shadow values.
  2997. if (ArgOffset + Size > ArgTLSSize)
  2998. break;
  2999. IRB.CreateAlignedStore(DFSF.getShadow(CB.getArgOperand(I)),
  3000. DFSF.getArgTLS(FT->getParamType(I), ArgOffset, IRB),
  3001. ShadowTLSAlignment);
  3002. ArgOffset += alignTo(Size, ShadowTLSAlignment);
  3003. }
  3004. Instruction *Next = nullptr;
  3005. if (!CB.getType()->isVoidTy()) {
  3006. if (InvokeInst *II = dyn_cast<InvokeInst>(&CB)) {
  3007. if (II->getNormalDest()->getSinglePredecessor()) {
  3008. Next = &II->getNormalDest()->front();
  3009. } else {
  3010. BasicBlock *NewBB =
  3011. SplitEdge(II->getParent(), II->getNormalDest(), &DFSF.DT);
  3012. Next = &NewBB->front();
  3013. }
  3014. } else {
  3015. assert(CB.getIterator() != CB.getParent()->end());
  3016. Next = CB.getNextNode();
  3017. }
  3018. // Don't emit the epilogue for musttail call returns.
  3019. if (isa<CallInst>(CB) && cast<CallInst>(CB).isMustTailCall())
  3020. return;
  3021. // Loads the return value shadow.
  3022. IRBuilder<> NextIRB(Next);
  3023. unsigned Size = DL.getTypeAllocSize(DFSF.DFS.getShadowTy(&CB));
  3024. if (Size > RetvalTLSSize) {
  3025. // Set overflowed return shadow to be zero.
  3026. DFSF.setShadow(&CB, DFSF.DFS.getZeroShadow(&CB));
  3027. } else {
  3028. LoadInst *LI = NextIRB.CreateAlignedLoad(
  3029. DFSF.DFS.getShadowTy(&CB), DFSF.getRetvalTLS(CB.getType(), NextIRB),
  3030. ShadowTLSAlignment, "_dfsret");
  3031. DFSF.SkipInsts.insert(LI);
  3032. DFSF.setShadow(&CB, LI);
  3033. DFSF.NonZeroChecks.push_back(LI);
  3034. }
  3035. if (ShouldTrackOrigins) {
  3036. LoadInst *LI = NextIRB.CreateLoad(DFSF.DFS.OriginTy,
  3037. DFSF.getRetvalOriginTLS(), "_dfsret_o");
  3038. DFSF.SkipInsts.insert(LI);
  3039. DFSF.setOrigin(&CB, LI);
  3040. }
  3041. DFSF.addReachesFunctionCallbacksIfEnabled(NextIRB, CB, &CB);
  3042. }
  3043. }
  3044. void DFSanVisitor::visitPHINode(PHINode &PN) {
  3045. Type *ShadowTy = DFSF.DFS.getShadowTy(&PN);
  3046. PHINode *ShadowPN =
  3047. PHINode::Create(ShadowTy, PN.getNumIncomingValues(), "", &PN);
  3048. // Give the shadow phi node valid predecessors to fool SplitEdge into working.
  3049. Value *UndefShadow = UndefValue::get(ShadowTy);
  3050. for (BasicBlock *BB : PN.blocks())
  3051. ShadowPN->addIncoming(UndefShadow, BB);
  3052. DFSF.setShadow(&PN, ShadowPN);
  3053. PHINode *OriginPN = nullptr;
  3054. if (DFSF.DFS.shouldTrackOrigins()) {
  3055. OriginPN =
  3056. PHINode::Create(DFSF.DFS.OriginTy, PN.getNumIncomingValues(), "", &PN);
  3057. Value *UndefOrigin = UndefValue::get(DFSF.DFS.OriginTy);
  3058. for (BasicBlock *BB : PN.blocks())
  3059. OriginPN->addIncoming(UndefOrigin, BB);
  3060. DFSF.setOrigin(&PN, OriginPN);
  3061. }
  3062. DFSF.PHIFixups.push_back({&PN, ShadowPN, OriginPN});
  3063. }
  3064. PreservedAnalyses DataFlowSanitizerPass::run(Module &M,
  3065. ModuleAnalysisManager &AM) {
  3066. auto GetTLI = [&](Function &F) -> TargetLibraryInfo & {
  3067. auto &FAM =
  3068. AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  3069. return FAM.getResult<TargetLibraryAnalysis>(F);
  3070. };
  3071. if (!DataFlowSanitizer(ABIListFiles).runImpl(M, GetTLI))
  3072. return PreservedAnalyses::all();
  3073. PreservedAnalyses PA = PreservedAnalyses::none();
  3074. // GlobalsAA is considered stateless and does not get invalidated unless
  3075. // explicitly invalidated; PreservedAnalyses::none() is not enough. Sanitizers
  3076. // make changes that require GlobalsAA to be invalidated.
  3077. PA.abandon<GlobalsAA>();
  3078. return PA;
  3079. }