memorysanitizer.cpp 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024
  1. //===-- MemorySanitizer.cpp - detector of uninitialized reads -------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. /// \file
  10. /// This file is a part of MemorySanitizer, a detector of uninitialized
  11. /// reads.
  12. ///
  13. /// The algorithm of the tool is similar to Memcheck
  14. /// (http://goo.gl/QKbem). We associate a few shadow bits with every
  15. /// byte of the application memory, poison the shadow of the malloc-ed
  16. /// or alloca-ed memory, load the shadow bits on every memory read,
  17. /// propagate the shadow bits through some of the arithmetic
  18. /// instruction (including MOV), store the shadow bits on every memory
  19. /// write, report a bug on some other instructions (e.g. JMP) if the
  20. /// associated shadow is poisoned.
  21. ///
  22. /// But there are differences too. The first and the major one:
  23. /// compiler instrumentation instead of binary instrumentation. This
  24. /// gives us much better register allocation, possible compiler
  25. /// optimizations and a fast start-up. But this brings the major issue
  26. /// as well: msan needs to see all program events, including system
  27. /// calls and reads/writes in system libraries, so we either need to
  28. /// compile *everything* with msan or use a binary translation
  29. /// component (e.g. DynamoRIO) to instrument pre-built libraries.
  30. /// Another difference from Memcheck is that we use 8 shadow bits per
  31. /// byte of application memory and use a direct shadow mapping. This
  32. /// greatly simplifies the instrumentation code and avoids races on
  33. /// shadow updates (Memcheck is single-threaded so races are not a
  34. /// concern there. Memcheck uses 2 shadow bits per byte with a slow
  35. /// path storage that uses 8 bits per byte).
  36. ///
  37. /// The default value of shadow is 0, which means "clean" (not poisoned).
  38. ///
  39. /// Every module initializer should call __msan_init to ensure that the
  40. /// shadow memory is ready. On error, __msan_warning is called. Since
  41. /// parameters and return values may be passed via registers, we have a
  42. /// specialized thread-local shadow for return values
  43. /// (__msan_retval_tls) and parameters (__msan_param_tls).
  44. ///
  45. /// Origin tracking.
  46. ///
  47. /// MemorySanitizer can track origins (allocation points) of all uninitialized
  48. /// values. This behavior is controlled with a flag (msan-track-origins) and is
  49. /// disabled by default.
  50. ///
  51. /// Origins are 4-byte values created and interpreted by the runtime library.
  52. /// They are stored in a second shadow mapping, one 4-byte value for 4 bytes
  53. /// of application memory. Propagation of origins is basically a bunch of
  54. /// "select" instructions that pick the origin of a dirty argument, if an
  55. /// instruction has one.
  56. ///
  57. /// Every 4 aligned, consecutive bytes of application memory have one origin
  58. /// value associated with them. If these bytes contain uninitialized data
  59. /// coming from 2 different allocations, the last store wins. Because of this,
  60. /// MemorySanitizer reports can show unrelated origins, but this is unlikely in
  61. /// practice.
  62. ///
  63. /// Origins are meaningless for fully initialized values, so MemorySanitizer
  64. /// avoids storing origin to memory when a fully initialized value is stored.
  65. /// This way it avoids needless overwritting origin of the 4-byte region on
  66. /// a short (i.e. 1 byte) clean store, and it is also good for performance.
  67. ///
  68. /// Atomic handling.
  69. ///
  70. /// Ideally, every atomic store of application value should update the
  71. /// corresponding shadow location in an atomic way. Unfortunately, atomic store
  72. /// of two disjoint locations can not be done without severe slowdown.
  73. ///
  74. /// Therefore, we implement an approximation that may err on the safe side.
  75. /// In this implementation, every atomically accessed location in the program
  76. /// may only change from (partially) uninitialized to fully initialized, but
  77. /// not the other way around. We load the shadow _after_ the application load,
  78. /// and we store the shadow _before_ the app store. Also, we always store clean
  79. /// shadow (if the application store is atomic). This way, if the store-load
  80. /// pair constitutes a happens-before arc, shadow store and load are correctly
  81. /// ordered such that the load will get either the value that was stored, or
  82. /// some later value (which is always clean).
  83. ///
  84. /// This does not work very well with Compare-And-Swap (CAS) and
  85. /// Read-Modify-Write (RMW) operations. To follow the above logic, CAS and RMW
  86. /// must store the new shadow before the app operation, and load the shadow
  87. /// after the app operation. Computers don't work this way. Current
  88. /// implementation ignores the load aspect of CAS/RMW, always returning a clean
  89. /// value. It implements the store part as a simple atomic store by storing a
  90. /// clean shadow.
  91. #include "llvm/Transforms/Instrumentation.h"
  92. #include "llvm/ADT/DepthFirstIterator.h"
  93. #include "llvm/ADT/SmallString.h"
  94. #include "llvm/ADT/SmallVector.h"
  95. #include "llvm/ADT/StringExtras.h"
  96. #include "llvm/ADT/Triple.h"
  97. #include "llvm/IR/DataLayout.h"
  98. #include "llvm/IR/Function.h"
  99. #include "llvm/IR/IRBuilder.h"
  100. #include "llvm/IR/InlineAsm.h"
  101. #include "llvm/IR/InstVisitor.h"
  102. #include "llvm/IR/IntrinsicInst.h"
  103. #include "llvm/IR/LLVMContext.h"
  104. #include "llvm/IR/MDBuilder.h"
  105. #include "llvm/IR/Module.h"
  106. #include "llvm/IR/Type.h"
  107. #include "llvm/IR/ValueMap.h"
  108. #include "llvm/Support/CommandLine.h"
  109. #include "llvm/Support/Compiler.h"
  110. #include "llvm/Support/Debug.h"
  111. #include "llvm/Support/raw_ostream.h"
  112. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  113. #include "llvm/Transforms/Utils/Local.h"
  114. #include "llvm/Transforms/Utils/ModuleUtils.h"
  115. using namespace llvm;
  116. #define DEBUG_TYPE "msan"
  117. static const unsigned kOriginSize = 4;
  118. static const unsigned kMinOriginAlignment = 4;
  119. static const unsigned kShadowTLSAlignment = 8;
  120. // These constants must be kept in sync with the ones in msan.h.
  121. static const unsigned kParamTLSSize = 800;
  122. static const unsigned kRetvalTLSSize = 800;
  123. // Accesses sizes are powers of two: 1, 2, 4, 8.
  124. static const size_t kNumberOfAccessSizes = 4;
  125. /// \brief Track origins of uninitialized values.
  126. ///
  127. /// Adds a section to MemorySanitizer report that points to the allocation
  128. /// (stack or heap) the uninitialized bits came from originally.
  129. static cl::opt<int> ClTrackOrigins("msan-track-origins",
  130. cl::desc("Track origins (allocation sites) of poisoned memory"),
  131. cl::Hidden, cl::init(0));
  132. static cl::opt<bool> ClKeepGoing("msan-keep-going",
  133. cl::desc("keep going after reporting a UMR"),
  134. cl::Hidden, cl::init(false));
  135. static cl::opt<bool> ClPoisonStack("msan-poison-stack",
  136. cl::desc("poison uninitialized stack variables"),
  137. cl::Hidden, cl::init(true));
  138. static cl::opt<bool> ClPoisonStackWithCall("msan-poison-stack-with-call",
  139. cl::desc("poison uninitialized stack variables with a call"),
  140. cl::Hidden, cl::init(false));
  141. static cl::opt<int> ClPoisonStackPattern("msan-poison-stack-pattern",
  142. cl::desc("poison uninitialized stack variables with the given patter"),
  143. cl::Hidden, cl::init(0xff));
  144. static cl::opt<bool> ClPoisonUndef("msan-poison-undef",
  145. cl::desc("poison undef temps"),
  146. cl::Hidden, cl::init(true));
  147. static cl::opt<bool> ClHandleICmp("msan-handle-icmp",
  148. cl::desc("propagate shadow through ICmpEQ and ICmpNE"),
  149. cl::Hidden, cl::init(true));
  150. static cl::opt<bool> ClHandleICmpExact("msan-handle-icmp-exact",
  151. cl::desc("exact handling of relational integer ICmp"),
  152. cl::Hidden, cl::init(false));
  153. // This flag controls whether we check the shadow of the address
  154. // operand of load or store. Such bugs are very rare, since load from
  155. // a garbage address typically results in SEGV, but still happen
  156. // (e.g. only lower bits of address are garbage, or the access happens
  157. // early at program startup where malloc-ed memory is more likely to
  158. // be zeroed. As of 2012-08-28 this flag adds 20% slowdown.
  159. static cl::opt<bool> ClCheckAccessAddress("msan-check-access-address",
  160. cl::desc("report accesses through a pointer which has poisoned shadow"),
  161. cl::Hidden, cl::init(true));
  162. static cl::opt<bool> ClDumpStrictInstructions("msan-dump-strict-instructions",
  163. cl::desc("print out instructions with default strict semantics"),
  164. cl::Hidden, cl::init(false));
  165. static cl::opt<int> ClInstrumentationWithCallThreshold(
  166. "msan-instrumentation-with-call-threshold",
  167. cl::desc(
  168. "If the function being instrumented requires more than "
  169. "this number of checks and origin stores, use callbacks instead of "
  170. "inline checks (-1 means never use callbacks)."),
  171. cl::Hidden, cl::init(3500));
  172. // This is an experiment to enable handling of cases where shadow is a non-zero
  173. // compile-time constant. For some unexplainable reason they were silently
  174. // ignored in the instrumentation.
  175. static cl::opt<bool> ClCheckConstantShadow("msan-check-constant-shadow",
  176. cl::desc("Insert checks for constant shadow values"),
  177. cl::Hidden, cl::init(false));
  178. static const char *const kMsanModuleCtorName = "msan.module_ctor";
  179. static const char *const kMsanInitName = "__msan_init";
  180. namespace {
  181. // Memory map parameters used in application-to-shadow address calculation.
  182. // Offset = (Addr & ~AndMask) ^ XorMask
  183. // Shadow = ShadowBase + Offset
  184. // Origin = OriginBase + Offset
  185. struct MemoryMapParams {
  186. uint64_t AndMask;
  187. uint64_t XorMask;
  188. uint64_t ShadowBase;
  189. uint64_t OriginBase;
  190. };
  191. struct PlatformMemoryMapParams {
  192. const MemoryMapParams *bits32;
  193. const MemoryMapParams *bits64;
  194. };
  195. // i386 Linux
  196. static const MemoryMapParams Linux_I386_MemoryMapParams = {
  197. 0x000080000000, // AndMask
  198. 0, // XorMask (not used)
  199. 0, // ShadowBase (not used)
  200. 0x000040000000, // OriginBase
  201. };
  202. // x86_64 Linux
  203. static const MemoryMapParams Linux_X86_64_MemoryMapParams = {
  204. 0x400000000000, // AndMask
  205. 0, // XorMask (not used)
  206. 0, // ShadowBase (not used)
  207. 0x200000000000, // OriginBase
  208. };
  209. // mips64 Linux
  210. static const MemoryMapParams Linux_MIPS64_MemoryMapParams = {
  211. 0x004000000000, // AndMask
  212. 0, // XorMask (not used)
  213. 0, // ShadowBase (not used)
  214. 0x002000000000, // OriginBase
  215. };
  216. // ppc64 Linux
  217. static const MemoryMapParams Linux_PowerPC64_MemoryMapParams = {
  218. 0x200000000000, // AndMask
  219. 0x100000000000, // XorMask
  220. 0x080000000000, // ShadowBase
  221. 0x1C0000000000, // OriginBase
  222. };
  223. // i386 FreeBSD
  224. static const MemoryMapParams FreeBSD_I386_MemoryMapParams = {
  225. 0x000180000000, // AndMask
  226. 0x000040000000, // XorMask
  227. 0x000020000000, // ShadowBase
  228. 0x000700000000, // OriginBase
  229. };
  230. // x86_64 FreeBSD
  231. static const MemoryMapParams FreeBSD_X86_64_MemoryMapParams = {
  232. 0xc00000000000, // AndMask
  233. 0x200000000000, // XorMask
  234. 0x100000000000, // ShadowBase
  235. 0x380000000000, // OriginBase
  236. };
  237. static const PlatformMemoryMapParams Linux_X86_MemoryMapParams = {
  238. &Linux_I386_MemoryMapParams,
  239. &Linux_X86_64_MemoryMapParams,
  240. };
  241. static const PlatformMemoryMapParams Linux_MIPS_MemoryMapParams = {
  242. NULL,
  243. &Linux_MIPS64_MemoryMapParams,
  244. };
  245. static const PlatformMemoryMapParams Linux_PowerPC_MemoryMapParams = {
  246. NULL,
  247. &Linux_PowerPC64_MemoryMapParams,
  248. };
  249. static const PlatformMemoryMapParams FreeBSD_X86_MemoryMapParams = {
  250. &FreeBSD_I386_MemoryMapParams,
  251. &FreeBSD_X86_64_MemoryMapParams,
  252. };
  253. /// \brief An instrumentation pass implementing detection of uninitialized
  254. /// reads.
  255. ///
  256. /// MemorySanitizer: instrument the code in module to find
  257. /// uninitialized reads.
  258. class MemorySanitizer : public FunctionPass {
  259. public:
  260. MemorySanitizer(int TrackOrigins = 0)
  261. : FunctionPass(ID),
  262. TrackOrigins(std::max(TrackOrigins, (int)ClTrackOrigins)),
  263. WarningFn(nullptr) {}
  264. const char *getPassName() const override { return "MemorySanitizer"; }
  265. bool runOnFunction(Function &F) override;
  266. bool doInitialization(Module &M) override;
  267. static char ID; // Pass identification, replacement for typeid.
  268. private:
  269. void initializeCallbacks(Module &M);
  270. /// \brief Track origins (allocation points) of uninitialized values.
  271. int TrackOrigins;
  272. LLVMContext *C;
  273. Type *IntptrTy;
  274. Type *OriginTy;
  275. /// \brief Thread-local shadow storage for function parameters.
  276. GlobalVariable *ParamTLS;
  277. /// \brief Thread-local origin storage for function parameters.
  278. GlobalVariable *ParamOriginTLS;
  279. /// \brief Thread-local shadow storage for function return value.
  280. GlobalVariable *RetvalTLS;
  281. /// \brief Thread-local origin storage for function return value.
  282. GlobalVariable *RetvalOriginTLS;
  283. /// \brief Thread-local shadow storage for in-register va_arg function
  284. /// parameters (x86_64-specific).
  285. GlobalVariable *VAArgTLS;
  286. /// \brief Thread-local shadow storage for va_arg overflow area
  287. /// (x86_64-specific).
  288. GlobalVariable *VAArgOverflowSizeTLS;
  289. /// \brief Thread-local space used to pass origin value to the UMR reporting
  290. /// function.
  291. GlobalVariable *OriginTLS;
  292. /// \brief The run-time callback to print a warning.
  293. Value *WarningFn;
  294. // These arrays are indexed by log2(AccessSize).
  295. Value *MaybeWarningFn[kNumberOfAccessSizes];
  296. Value *MaybeStoreOriginFn[kNumberOfAccessSizes];
  297. /// \brief Run-time helper that generates a new origin value for a stack
  298. /// allocation.
  299. Value *MsanSetAllocaOrigin4Fn;
  300. /// \brief Run-time helper that poisons stack on function entry.
  301. Value *MsanPoisonStackFn;
  302. /// \brief Run-time helper that records a store (or any event) of an
  303. /// uninitialized value and returns an updated origin id encoding this info.
  304. Value *MsanChainOriginFn;
  305. /// \brief MSan runtime replacements for memmove, memcpy and memset.
  306. Value *MemmoveFn, *MemcpyFn, *MemsetFn;
  307. /// \brief Memory map parameters used in application-to-shadow calculation.
  308. const MemoryMapParams *MapParams;
  309. MDNode *ColdCallWeights;
  310. /// \brief Branch weights for origin store.
  311. MDNode *OriginStoreWeights;
  312. /// \brief An empty volatile inline asm that prevents callback merge.
  313. InlineAsm *EmptyAsm;
  314. Function *MsanCtorFunction;
  315. friend struct MemorySanitizerVisitor;
  316. friend struct VarArgAMD64Helper;
  317. friend struct VarArgMIPS64Helper;
  318. };
  319. } // namespace
  320. char MemorySanitizer::ID = 0;
  321. INITIALIZE_PASS(MemorySanitizer, "msan",
  322. "MemorySanitizer: detects uninitialized reads.",
  323. false, false)
  324. FunctionPass *llvm::createMemorySanitizerPass(int TrackOrigins) {
  325. return new MemorySanitizer(TrackOrigins);
  326. }
  327. /// \brief Create a non-const global initialized with the given string.
  328. ///
  329. /// Creates a writable global for Str so that we can pass it to the
  330. /// run-time lib. Runtime uses first 4 bytes of the string to store the
  331. /// frame ID, so the string needs to be mutable.
  332. static GlobalVariable *createPrivateNonConstGlobalForString(Module &M,
  333. StringRef Str) {
  334. Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
  335. return new GlobalVariable(M, StrConst->getType(), /*isConstant=*/false,
  336. GlobalValue::PrivateLinkage, StrConst, "");
  337. }
  338. /// \brief Insert extern declaration of runtime-provided functions and globals.
  339. void MemorySanitizer::initializeCallbacks(Module &M) {
  340. // Only do this once.
  341. if (WarningFn)
  342. return;
  343. IRBuilder<> IRB(*C);
  344. // Create the callback.
  345. // FIXME: this function should have "Cold" calling conv,
  346. // which is not yet implemented.
  347. StringRef WarningFnName = ClKeepGoing ? "__msan_warning"
  348. : "__msan_warning_noreturn";
  349. WarningFn = M.getOrInsertFunction(WarningFnName, IRB.getVoidTy(), nullptr);
  350. for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
  351. AccessSizeIndex++) {
  352. unsigned AccessSize = 1 << AccessSizeIndex;
  353. std::string FunctionName = "__msan_maybe_warning_" + itostr(AccessSize);
  354. MaybeWarningFn[AccessSizeIndex] = M.getOrInsertFunction(
  355. FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8),
  356. IRB.getInt32Ty(), nullptr);
  357. FunctionName = "__msan_maybe_store_origin_" + itostr(AccessSize);
  358. MaybeStoreOriginFn[AccessSizeIndex] = M.getOrInsertFunction(
  359. FunctionName, IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8),
  360. IRB.getInt8PtrTy(), IRB.getInt32Ty(), nullptr);
  361. }
  362. MsanSetAllocaOrigin4Fn = M.getOrInsertFunction(
  363. "__msan_set_alloca_origin4", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy,
  364. IRB.getInt8PtrTy(), IntptrTy, nullptr);
  365. MsanPoisonStackFn =
  366. M.getOrInsertFunction("__msan_poison_stack", IRB.getVoidTy(),
  367. IRB.getInt8PtrTy(), IntptrTy, nullptr);
  368. MsanChainOriginFn = M.getOrInsertFunction(
  369. "__msan_chain_origin", IRB.getInt32Ty(), IRB.getInt32Ty(), nullptr);
  370. MemmoveFn = M.getOrInsertFunction(
  371. "__msan_memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
  372. IRB.getInt8PtrTy(), IntptrTy, nullptr);
  373. MemcpyFn = M.getOrInsertFunction(
  374. "__msan_memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
  375. IntptrTy, nullptr);
  376. MemsetFn = M.getOrInsertFunction(
  377. "__msan_memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(),
  378. IntptrTy, nullptr);
  379. // Create globals.
  380. RetvalTLS = new GlobalVariable(
  381. M, ArrayType::get(IRB.getInt64Ty(), kRetvalTLSSize / 8), false,
  382. GlobalVariable::ExternalLinkage, nullptr, "__msan_retval_tls", nullptr,
  383. GlobalVariable::InitialExecTLSModel);
  384. RetvalOriginTLS = new GlobalVariable(
  385. M, OriginTy, false, GlobalVariable::ExternalLinkage, nullptr,
  386. "__msan_retval_origin_tls", nullptr, GlobalVariable::InitialExecTLSModel);
  387. ParamTLS = new GlobalVariable(
  388. M, ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), false,
  389. GlobalVariable::ExternalLinkage, nullptr, "__msan_param_tls", nullptr,
  390. GlobalVariable::InitialExecTLSModel);
  391. ParamOriginTLS = new GlobalVariable(
  392. M, ArrayType::get(OriginTy, kParamTLSSize / 4), false,
  393. GlobalVariable::ExternalLinkage, nullptr, "__msan_param_origin_tls",
  394. nullptr, GlobalVariable::InitialExecTLSModel);
  395. VAArgTLS = new GlobalVariable(
  396. M, ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), false,
  397. GlobalVariable::ExternalLinkage, nullptr, "__msan_va_arg_tls", nullptr,
  398. GlobalVariable::InitialExecTLSModel);
  399. VAArgOverflowSizeTLS = new GlobalVariable(
  400. M, IRB.getInt64Ty(), false, GlobalVariable::ExternalLinkage, nullptr,
  401. "__msan_va_arg_overflow_size_tls", nullptr,
  402. GlobalVariable::InitialExecTLSModel);
  403. OriginTLS = new GlobalVariable(
  404. M, IRB.getInt32Ty(), false, GlobalVariable::ExternalLinkage, nullptr,
  405. "__msan_origin_tls", nullptr, GlobalVariable::InitialExecTLSModel);
  406. // We insert an empty inline asm after __msan_report* to avoid callback merge.
  407. EmptyAsm = InlineAsm::get(FunctionType::get(IRB.getVoidTy(), false),
  408. StringRef(""), StringRef(""),
  409. /*hasSideEffects=*/true);
  410. }
  411. /// \brief Module-level initialization.
  412. ///
  413. /// inserts a call to __msan_init to the module's constructor list.
  414. bool MemorySanitizer::doInitialization(Module &M) {
  415. auto &DL = M.getDataLayout();
  416. Triple TargetTriple(M.getTargetTriple());
  417. switch (TargetTriple.getOS()) {
  418. case Triple::FreeBSD:
  419. switch (TargetTriple.getArch()) {
  420. case Triple::x86_64:
  421. MapParams = FreeBSD_X86_MemoryMapParams.bits64;
  422. break;
  423. case Triple::x86:
  424. MapParams = FreeBSD_X86_MemoryMapParams.bits32;
  425. break;
  426. default:
  427. report_fatal_error("unsupported architecture");
  428. }
  429. break;
  430. case Triple::Linux:
  431. switch (TargetTriple.getArch()) {
  432. case Triple::x86_64:
  433. MapParams = Linux_X86_MemoryMapParams.bits64;
  434. break;
  435. case Triple::x86:
  436. MapParams = Linux_X86_MemoryMapParams.bits32;
  437. break;
  438. case Triple::mips64:
  439. case Triple::mips64el:
  440. MapParams = Linux_MIPS_MemoryMapParams.bits64;
  441. break;
  442. case Triple::ppc64:
  443. case Triple::ppc64le:
  444. MapParams = Linux_PowerPC_MemoryMapParams.bits64;
  445. break;
  446. default:
  447. report_fatal_error("unsupported architecture");
  448. }
  449. break;
  450. default:
  451. report_fatal_error("unsupported operating system");
  452. }
  453. C = &(M.getContext());
  454. IRBuilder<> IRB(*C);
  455. IntptrTy = IRB.getIntPtrTy(DL);
  456. OriginTy = IRB.getInt32Ty();
  457. ColdCallWeights = MDBuilder(*C).createBranchWeights(1, 1000);
  458. OriginStoreWeights = MDBuilder(*C).createBranchWeights(1, 1000);
  459. std::tie(MsanCtorFunction, std::ignore) =
  460. createSanitizerCtorAndInitFunctions(M, kMsanModuleCtorName, kMsanInitName,
  461. /*InitArgTypes=*/{},
  462. /*InitArgs=*/{});
  463. appendToGlobalCtors(M, MsanCtorFunction, 0);
  464. if (TrackOrigins)
  465. new GlobalVariable(M, IRB.getInt32Ty(), true, GlobalValue::WeakODRLinkage,
  466. IRB.getInt32(TrackOrigins), "__msan_track_origins");
  467. if (ClKeepGoing)
  468. new GlobalVariable(M, IRB.getInt32Ty(), true, GlobalValue::WeakODRLinkage,
  469. IRB.getInt32(ClKeepGoing), "__msan_keep_going");
  470. return true;
  471. }
  472. namespace {
  473. /// \brief A helper class that handles instrumentation of VarArg
  474. /// functions on a particular platform.
  475. ///
  476. /// Implementations are expected to insert the instrumentation
  477. /// necessary to propagate argument shadow through VarArg function
  478. /// calls. Visit* methods are called during an InstVisitor pass over
  479. /// the function, and should avoid creating new basic blocks. A new
  480. /// instance of this class is created for each instrumented function.
  481. struct VarArgHelper {
  482. /// \brief Visit a CallSite.
  483. virtual void visitCallSite(CallSite &CS, IRBuilder<> &IRB) = 0;
  484. /// \brief Visit a va_start call.
  485. virtual void visitVAStartInst(VAStartInst &I) = 0;
  486. /// \brief Visit a va_copy call.
  487. virtual void visitVACopyInst(VACopyInst &I) = 0;
  488. /// \brief Finalize function instrumentation.
  489. ///
  490. /// This method is called after visiting all interesting (see above)
  491. /// instructions in a function.
  492. virtual void finalizeInstrumentation() = 0;
  493. virtual ~VarArgHelper() {}
  494. };
  495. struct MemorySanitizerVisitor;
  496. VarArgHelper*
  497. CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
  498. MemorySanitizerVisitor &Visitor);
  499. unsigned TypeSizeToSizeIndex(unsigned TypeSize) {
  500. if (TypeSize <= 8) return 0;
  501. return Log2_32_Ceil(TypeSize / 8);
  502. }
  503. /// This class does all the work for a given function. Store and Load
  504. /// instructions store and load corresponding shadow and origin
  505. /// values. Most instructions propagate shadow from arguments to their
  506. /// return values. Certain instructions (most importantly, BranchInst)
  507. /// test their argument shadow and print reports (with a runtime call) if it's
  508. /// non-zero.
  509. struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
  510. Function &F;
  511. MemorySanitizer &MS;
  512. SmallVector<PHINode *, 16> ShadowPHINodes, OriginPHINodes;
  513. ValueMap<Value*, Value*> ShadowMap, OriginMap;
  514. std::unique_ptr<VarArgHelper> VAHelper;
  515. // The following flags disable parts of MSan instrumentation based on
  516. // blacklist contents and command-line options.
  517. bool InsertChecks;
  518. bool PropagateShadow;
  519. bool PoisonStack;
  520. bool PoisonUndef;
  521. bool CheckReturnValue;
  522. struct ShadowOriginAndInsertPoint {
  523. Value *Shadow;
  524. Value *Origin;
  525. Instruction *OrigIns;
  526. ShadowOriginAndInsertPoint(Value *S, Value *O, Instruction *I)
  527. : Shadow(S), Origin(O), OrigIns(I) { }
  528. };
  529. SmallVector<ShadowOriginAndInsertPoint, 16> InstrumentationList;
  530. SmallVector<Instruction*, 16> StoreList;
  531. MemorySanitizerVisitor(Function &F, MemorySanitizer &MS)
  532. : F(F), MS(MS), VAHelper(CreateVarArgHelper(F, MS, *this)) {
  533. bool SanitizeFunction = F.hasFnAttribute(Attribute::SanitizeMemory);
  534. InsertChecks = SanitizeFunction;
  535. PropagateShadow = SanitizeFunction;
  536. PoisonStack = SanitizeFunction && ClPoisonStack;
  537. PoisonUndef = SanitizeFunction && ClPoisonUndef;
  538. // FIXME: Consider using SpecialCaseList to specify a list of functions that
  539. // must always return fully initialized values. For now, we hardcode "main".
  540. CheckReturnValue = SanitizeFunction && (F.getName() == "main");
  541. DEBUG(if (!InsertChecks)
  542. dbgs() << "MemorySanitizer is not inserting checks into '"
  543. << F.getName() << "'\n");
  544. }
  545. Value *updateOrigin(Value *V, IRBuilder<> &IRB) {
  546. if (MS.TrackOrigins <= 1) return V;
  547. return IRB.CreateCall(MS.MsanChainOriginFn, V);
  548. }
  549. Value *originToIntptr(IRBuilder<> &IRB, Value *Origin) {
  550. const DataLayout &DL = F.getParent()->getDataLayout();
  551. unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
  552. if (IntptrSize == kOriginSize) return Origin;
  553. assert(IntptrSize == kOriginSize * 2);
  554. Origin = IRB.CreateIntCast(Origin, MS.IntptrTy, /* isSigned */ false);
  555. return IRB.CreateOr(Origin, IRB.CreateShl(Origin, kOriginSize * 8));
  556. }
  557. /// \brief Fill memory range with the given origin value.
  558. void paintOrigin(IRBuilder<> &IRB, Value *Origin, Value *OriginPtr,
  559. unsigned Size, unsigned Alignment) {
  560. const DataLayout &DL = F.getParent()->getDataLayout();
  561. unsigned IntptrAlignment = DL.getABITypeAlignment(MS.IntptrTy);
  562. unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
  563. assert(IntptrAlignment >= kMinOriginAlignment);
  564. assert(IntptrSize >= kOriginSize);
  565. unsigned Ofs = 0;
  566. unsigned CurrentAlignment = Alignment;
  567. if (Alignment >= IntptrAlignment && IntptrSize > kOriginSize) {
  568. Value *IntptrOrigin = originToIntptr(IRB, Origin);
  569. Value *IntptrOriginPtr =
  570. IRB.CreatePointerCast(OriginPtr, PointerType::get(MS.IntptrTy, 0));
  571. for (unsigned i = 0; i < Size / IntptrSize; ++i) {
  572. Value *Ptr = i ? IRB.CreateConstGEP1_32(MS.IntptrTy, IntptrOriginPtr, i)
  573. : IntptrOriginPtr;
  574. IRB.CreateAlignedStore(IntptrOrigin, Ptr, CurrentAlignment);
  575. Ofs += IntptrSize / kOriginSize;
  576. CurrentAlignment = IntptrAlignment;
  577. }
  578. }
  579. for (unsigned i = Ofs; i < (Size + kOriginSize - 1) / kOriginSize; ++i) {
  580. Value *GEP =
  581. i ? IRB.CreateConstGEP1_32(nullptr, OriginPtr, i) : OriginPtr;
  582. IRB.CreateAlignedStore(Origin, GEP, CurrentAlignment);
  583. CurrentAlignment = kMinOriginAlignment;
  584. }
  585. }
  586. void storeOrigin(IRBuilder<> &IRB, Value *Addr, Value *Shadow, Value *Origin,
  587. unsigned Alignment, bool AsCall) {
  588. const DataLayout &DL = F.getParent()->getDataLayout();
  589. unsigned OriginAlignment = std::max(kMinOriginAlignment, Alignment);
  590. unsigned StoreSize = DL.getTypeStoreSize(Shadow->getType());
  591. if (isa<StructType>(Shadow->getType())) {
  592. paintOrigin(IRB, updateOrigin(Origin, IRB),
  593. getOriginPtr(Addr, IRB, Alignment), StoreSize,
  594. OriginAlignment);
  595. } else {
  596. Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB);
  597. Constant *ConstantShadow = dyn_cast_or_null<Constant>(ConvertedShadow);
  598. if (ConstantShadow) {
  599. if (ClCheckConstantShadow && !ConstantShadow->isZeroValue())
  600. paintOrigin(IRB, updateOrigin(Origin, IRB),
  601. getOriginPtr(Addr, IRB, Alignment), StoreSize,
  602. OriginAlignment);
  603. return;
  604. }
  605. unsigned TypeSizeInBits =
  606. DL.getTypeSizeInBits(ConvertedShadow->getType());
  607. unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
  608. if (AsCall && SizeIndex < kNumberOfAccessSizes) {
  609. Value *Fn = MS.MaybeStoreOriginFn[SizeIndex];
  610. Value *ConvertedShadow2 = IRB.CreateZExt(
  611. ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
  612. IRB.CreateCall(Fn, {ConvertedShadow2,
  613. IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()),
  614. Origin});
  615. } else {
  616. Value *Cmp = IRB.CreateICmpNE(
  617. ConvertedShadow, getCleanShadow(ConvertedShadow), "_mscmp");
  618. Instruction *CheckTerm = SplitBlockAndInsertIfThen(
  619. Cmp, IRB.GetInsertPoint(), false, MS.OriginStoreWeights);
  620. IRBuilder<> IRBNew(CheckTerm);
  621. paintOrigin(IRBNew, updateOrigin(Origin, IRBNew),
  622. getOriginPtr(Addr, IRBNew, Alignment), StoreSize,
  623. OriginAlignment);
  624. }
  625. }
  626. }
  627. void materializeStores(bool InstrumentWithCalls) {
  628. for (auto Inst : StoreList) {
  629. StoreInst &SI = *dyn_cast<StoreInst>(Inst);
  630. IRBuilder<> IRB(&SI);
  631. Value *Val = SI.getValueOperand();
  632. Value *Addr = SI.getPointerOperand();
  633. Value *Shadow = SI.isAtomic() ? getCleanShadow(Val) : getShadow(Val);
  634. Value *ShadowPtr = getShadowPtr(Addr, Shadow->getType(), IRB);
  635. StoreInst *NewSI =
  636. IRB.CreateAlignedStore(Shadow, ShadowPtr, SI.getAlignment());
  637. DEBUG(dbgs() << " STORE: " << *NewSI << "\n");
  638. (void)NewSI;
  639. if (ClCheckAccessAddress) insertShadowCheck(Addr, &SI);
  640. if (SI.isAtomic()) SI.setOrdering(addReleaseOrdering(SI.getOrdering()));
  641. if (MS.TrackOrigins && !SI.isAtomic())
  642. storeOrigin(IRB, Addr, Shadow, getOrigin(Val), SI.getAlignment(),
  643. InstrumentWithCalls);
  644. }
  645. }
  646. void materializeOneCheck(Instruction *OrigIns, Value *Shadow, Value *Origin,
  647. bool AsCall) {
  648. IRBuilder<> IRB(OrigIns);
  649. DEBUG(dbgs() << " SHAD0 : " << *Shadow << "\n");
  650. Value *ConvertedShadow = convertToShadowTyNoVec(Shadow, IRB);
  651. DEBUG(dbgs() << " SHAD1 : " << *ConvertedShadow << "\n");
  652. Constant *ConstantShadow = dyn_cast_or_null<Constant>(ConvertedShadow);
  653. if (ConstantShadow) {
  654. if (ClCheckConstantShadow && !ConstantShadow->isZeroValue()) {
  655. if (MS.TrackOrigins) {
  656. IRB.CreateStore(Origin ? (Value *)Origin : (Value *)IRB.getInt32(0),
  657. MS.OriginTLS);
  658. }
  659. IRB.CreateCall(MS.WarningFn, {});
  660. IRB.CreateCall(MS.EmptyAsm, {});
  661. // FIXME: Insert UnreachableInst if !ClKeepGoing?
  662. // This may invalidate some of the following checks and needs to be done
  663. // at the very end.
  664. }
  665. return;
  666. }
  667. const DataLayout &DL = OrigIns->getModule()->getDataLayout();
  668. unsigned TypeSizeInBits = DL.getTypeSizeInBits(ConvertedShadow->getType());
  669. unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
  670. if (AsCall && SizeIndex < kNumberOfAccessSizes) {
  671. Value *Fn = MS.MaybeWarningFn[SizeIndex];
  672. Value *ConvertedShadow2 =
  673. IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
  674. IRB.CreateCall(Fn, {ConvertedShadow2, MS.TrackOrigins && Origin
  675. ? Origin
  676. : (Value *)IRB.getInt32(0)});
  677. } else {
  678. Value *Cmp = IRB.CreateICmpNE(ConvertedShadow,
  679. getCleanShadow(ConvertedShadow), "_mscmp");
  680. Instruction *CheckTerm = SplitBlockAndInsertIfThen(
  681. Cmp, OrigIns,
  682. /* Unreachable */ !ClKeepGoing, MS.ColdCallWeights);
  683. IRB.SetInsertPoint(CheckTerm);
  684. if (MS.TrackOrigins) {
  685. IRB.CreateStore(Origin ? (Value *)Origin : (Value *)IRB.getInt32(0),
  686. MS.OriginTLS);
  687. }
  688. IRB.CreateCall(MS.WarningFn, {});
  689. IRB.CreateCall(MS.EmptyAsm, {});
  690. DEBUG(dbgs() << " CHECK: " << *Cmp << "\n");
  691. }
  692. }
  693. void materializeChecks(bool InstrumentWithCalls) {
  694. for (const auto &ShadowData : InstrumentationList) {
  695. Instruction *OrigIns = ShadowData.OrigIns;
  696. Value *Shadow = ShadowData.Shadow;
  697. Value *Origin = ShadowData.Origin;
  698. materializeOneCheck(OrigIns, Shadow, Origin, InstrumentWithCalls);
  699. }
  700. DEBUG(dbgs() << "DONE:\n" << F);
  701. }
  702. /// \brief Add MemorySanitizer instrumentation to a function.
  703. bool runOnFunction() {
  704. MS.initializeCallbacks(*F.getParent());
  705. // In the presence of unreachable blocks, we may see Phi nodes with
  706. // incoming nodes from such blocks. Since InstVisitor skips unreachable
  707. // blocks, such nodes will not have any shadow value associated with them.
  708. // It's easier to remove unreachable blocks than deal with missing shadow.
  709. removeUnreachableBlocks(F);
  710. // Iterate all BBs in depth-first order and create shadow instructions
  711. // for all instructions (where applicable).
  712. // For PHI nodes we create dummy shadow PHIs which will be finalized later.
  713. for (BasicBlock *BB : depth_first(&F.getEntryBlock()))
  714. visit(*BB);
  715. // Finalize PHI nodes.
  716. for (PHINode *PN : ShadowPHINodes) {
  717. PHINode *PNS = cast<PHINode>(getShadow(PN));
  718. PHINode *PNO = MS.TrackOrigins ? cast<PHINode>(getOrigin(PN)) : nullptr;
  719. size_t NumValues = PN->getNumIncomingValues();
  720. for (size_t v = 0; v < NumValues; v++) {
  721. PNS->addIncoming(getShadow(PN, v), PN->getIncomingBlock(v));
  722. if (PNO) PNO->addIncoming(getOrigin(PN, v), PN->getIncomingBlock(v));
  723. }
  724. }
  725. VAHelper->finalizeInstrumentation();
  726. bool InstrumentWithCalls = ClInstrumentationWithCallThreshold >= 0 &&
  727. InstrumentationList.size() + StoreList.size() >
  728. (unsigned)ClInstrumentationWithCallThreshold;
  729. // Delayed instrumentation of StoreInst.
  730. // This may add new checks to be inserted later.
  731. materializeStores(InstrumentWithCalls);
  732. // Insert shadow value checks.
  733. materializeChecks(InstrumentWithCalls);
  734. return true;
  735. }
  736. /// \brief Compute the shadow type that corresponds to a given Value.
  737. Type *getShadowTy(Value *V) {
  738. return getShadowTy(V->getType());
  739. }
  740. /// \brief Compute the shadow type that corresponds to a given Type.
  741. Type *getShadowTy(Type *OrigTy) {
  742. if (!OrigTy->isSized()) {
  743. return nullptr;
  744. }
  745. // For integer type, shadow is the same as the original type.
  746. // This may return weird-sized types like i1.
  747. if (IntegerType *IT = dyn_cast<IntegerType>(OrigTy))
  748. return IT;
  749. const DataLayout &DL = F.getParent()->getDataLayout();
  750. if (VectorType *VT = dyn_cast<VectorType>(OrigTy)) {
  751. uint32_t EltSize = DL.getTypeSizeInBits(VT->getElementType());
  752. return VectorType::get(IntegerType::get(*MS.C, EltSize),
  753. VT->getNumElements());
  754. }
  755. if (ArrayType *AT = dyn_cast<ArrayType>(OrigTy)) {
  756. return ArrayType::get(getShadowTy(AT->getElementType()),
  757. AT->getNumElements());
  758. }
  759. if (StructType *ST = dyn_cast<StructType>(OrigTy)) {
  760. SmallVector<Type*, 4> Elements;
  761. for (unsigned i = 0, n = ST->getNumElements(); i < n; i++)
  762. Elements.push_back(getShadowTy(ST->getElementType(i)));
  763. StructType *Res = StructType::get(*MS.C, Elements, ST->isPacked());
  764. DEBUG(dbgs() << "getShadowTy: " << *ST << " ===> " << *Res << "\n");
  765. return Res;
  766. }
  767. uint32_t TypeSize = DL.getTypeSizeInBits(OrigTy);
  768. return IntegerType::get(*MS.C, TypeSize);
  769. }
  770. /// \brief Flatten a vector type.
  771. Type *getShadowTyNoVec(Type *ty) {
  772. if (VectorType *vt = dyn_cast<VectorType>(ty))
  773. return IntegerType::get(*MS.C, vt->getBitWidth());
  774. return ty;
  775. }
  776. /// \brief Convert a shadow value to it's flattened variant.
  777. Value *convertToShadowTyNoVec(Value *V, IRBuilder<> &IRB) {
  778. Type *Ty = V->getType();
  779. Type *NoVecTy = getShadowTyNoVec(Ty);
  780. if (Ty == NoVecTy) return V;
  781. return IRB.CreateBitCast(V, NoVecTy);
  782. }
  783. /// \brief Compute the integer shadow offset that corresponds to a given
  784. /// application address.
  785. ///
  786. /// Offset = (Addr & ~AndMask) ^ XorMask
  787. Value *getShadowPtrOffset(Value *Addr, IRBuilder<> &IRB) {
  788. uint64_t AndMask = MS.MapParams->AndMask;
  789. assert(AndMask != 0 && "AndMask shall be specified");
  790. Value *OffsetLong =
  791. IRB.CreateAnd(IRB.CreatePointerCast(Addr, MS.IntptrTy),
  792. ConstantInt::get(MS.IntptrTy, ~AndMask));
  793. uint64_t XorMask = MS.MapParams->XorMask;
  794. if (XorMask != 0)
  795. OffsetLong = IRB.CreateXor(OffsetLong,
  796. ConstantInt::get(MS.IntptrTy, XorMask));
  797. return OffsetLong;
  798. }
  799. /// \brief Compute the shadow address that corresponds to a given application
  800. /// address.
  801. ///
  802. /// Shadow = ShadowBase + Offset
  803. Value *getShadowPtr(Value *Addr, Type *ShadowTy,
  804. IRBuilder<> &IRB) {
  805. Value *ShadowLong = getShadowPtrOffset(Addr, IRB);
  806. uint64_t ShadowBase = MS.MapParams->ShadowBase;
  807. if (ShadowBase != 0)
  808. ShadowLong =
  809. IRB.CreateAdd(ShadowLong,
  810. ConstantInt::get(MS.IntptrTy, ShadowBase));
  811. return IRB.CreateIntToPtr(ShadowLong, PointerType::get(ShadowTy, 0));
  812. }
  813. /// \brief Compute the origin address that corresponds to a given application
  814. /// address.
  815. ///
  816. /// OriginAddr = (OriginBase + Offset) & ~3ULL
  817. Value *getOriginPtr(Value *Addr, IRBuilder<> &IRB, unsigned Alignment) {
  818. Value *OriginLong = getShadowPtrOffset(Addr, IRB);
  819. uint64_t OriginBase = MS.MapParams->OriginBase;
  820. if (OriginBase != 0)
  821. OriginLong =
  822. IRB.CreateAdd(OriginLong,
  823. ConstantInt::get(MS.IntptrTy, OriginBase));
  824. if (Alignment < kMinOriginAlignment) {
  825. uint64_t Mask = kMinOriginAlignment - 1;
  826. OriginLong = IRB.CreateAnd(OriginLong,
  827. ConstantInt::get(MS.IntptrTy, ~Mask));
  828. }
  829. return IRB.CreateIntToPtr(OriginLong,
  830. PointerType::get(IRB.getInt32Ty(), 0));
  831. }
  832. /// \brief Compute the shadow address for a given function argument.
  833. ///
  834. /// Shadow = ParamTLS+ArgOffset.
  835. Value *getShadowPtrForArgument(Value *A, IRBuilder<> &IRB,
  836. int ArgOffset) {
  837. Value *Base = IRB.CreatePointerCast(MS.ParamTLS, MS.IntptrTy);
  838. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  839. return IRB.CreateIntToPtr(Base, PointerType::get(getShadowTy(A), 0),
  840. "_msarg");
  841. }
  842. /// \brief Compute the origin address for a given function argument.
  843. Value *getOriginPtrForArgument(Value *A, IRBuilder<> &IRB,
  844. int ArgOffset) {
  845. if (!MS.TrackOrigins) return nullptr;
  846. Value *Base = IRB.CreatePointerCast(MS.ParamOriginTLS, MS.IntptrTy);
  847. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  848. return IRB.CreateIntToPtr(Base, PointerType::get(MS.OriginTy, 0),
  849. "_msarg_o");
  850. }
  851. /// \brief Compute the shadow address for a retval.
  852. Value *getShadowPtrForRetval(Value *A, IRBuilder<> &IRB) {
  853. Value *Base = IRB.CreatePointerCast(MS.RetvalTLS, MS.IntptrTy);
  854. return IRB.CreateIntToPtr(Base, PointerType::get(getShadowTy(A), 0),
  855. "_msret");
  856. }
  857. /// \brief Compute the origin address for a retval.
  858. Value *getOriginPtrForRetval(IRBuilder<> &IRB) {
  859. // We keep a single origin for the entire retval. Might be too optimistic.
  860. return MS.RetvalOriginTLS;
  861. }
  862. /// \brief Set SV to be the shadow value for V.
  863. void setShadow(Value *V, Value *SV) {
  864. assert(!ShadowMap.count(V) && "Values may only have one shadow");
  865. ShadowMap[V] = PropagateShadow ? SV : getCleanShadow(V);
  866. }
  867. /// \brief Set Origin to be the origin value for V.
  868. void setOrigin(Value *V, Value *Origin) {
  869. if (!MS.TrackOrigins) return;
  870. assert(!OriginMap.count(V) && "Values may only have one origin");
  871. DEBUG(dbgs() << "ORIGIN: " << *V << " ==> " << *Origin << "\n");
  872. OriginMap[V] = Origin;
  873. }
  874. /// \brief Create a clean shadow value for a given value.
  875. ///
  876. /// Clean shadow (all zeroes) means all bits of the value are defined
  877. /// (initialized).
  878. Constant *getCleanShadow(Value *V) {
  879. Type *ShadowTy = getShadowTy(V);
  880. if (!ShadowTy)
  881. return nullptr;
  882. return Constant::getNullValue(ShadowTy);
  883. }
  884. /// \brief Create a dirty shadow of a given shadow type.
  885. Constant *getPoisonedShadow(Type *ShadowTy) {
  886. assert(ShadowTy);
  887. if (isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy))
  888. return Constant::getAllOnesValue(ShadowTy);
  889. if (ArrayType *AT = dyn_cast<ArrayType>(ShadowTy)) {
  890. SmallVector<Constant *, 4> Vals(AT->getNumElements(),
  891. getPoisonedShadow(AT->getElementType()));
  892. return ConstantArray::get(AT, Vals);
  893. }
  894. if (StructType *ST = dyn_cast<StructType>(ShadowTy)) {
  895. SmallVector<Constant *, 4> Vals;
  896. for (unsigned i = 0, n = ST->getNumElements(); i < n; i++)
  897. Vals.push_back(getPoisonedShadow(ST->getElementType(i)));
  898. return ConstantStruct::get(ST, Vals);
  899. }
  900. llvm_unreachable("Unexpected shadow type");
  901. }
  902. /// \brief Create a dirty shadow for a given value.
  903. Constant *getPoisonedShadow(Value *V) {
  904. Type *ShadowTy = getShadowTy(V);
  905. if (!ShadowTy)
  906. return nullptr;
  907. return getPoisonedShadow(ShadowTy);
  908. }
  909. /// \brief Create a clean (zero) origin.
  910. Value *getCleanOrigin() {
  911. return Constant::getNullValue(MS.OriginTy);
  912. }
  913. /// \brief Get the shadow value for a given Value.
  914. ///
  915. /// This function either returns the value set earlier with setShadow,
  916. /// or extracts if from ParamTLS (for function arguments).
  917. Value *getShadow(Value *V) {
  918. if (!PropagateShadow) return getCleanShadow(V);
  919. if (Instruction *I = dyn_cast<Instruction>(V)) {
  920. // For instructions the shadow is already stored in the map.
  921. Value *Shadow = ShadowMap[V];
  922. if (!Shadow) {
  923. DEBUG(dbgs() << "No shadow: " << *V << "\n" << *(I->getParent()));
  924. (void)I;
  925. assert(Shadow && "No shadow for a value");
  926. }
  927. return Shadow;
  928. }
  929. if (UndefValue *U = dyn_cast<UndefValue>(V)) {
  930. Value *AllOnes = PoisonUndef ? getPoisonedShadow(V) : getCleanShadow(V);
  931. DEBUG(dbgs() << "Undef: " << *U << " ==> " << *AllOnes << "\n");
  932. (void)U;
  933. return AllOnes;
  934. }
  935. if (Argument *A = dyn_cast<Argument>(V)) {
  936. // For arguments we compute the shadow on demand and store it in the map.
  937. Value **ShadowPtr = &ShadowMap[V];
  938. if (*ShadowPtr)
  939. return *ShadowPtr;
  940. Function *F = A->getParent();
  941. IRBuilder<> EntryIRB(F->getEntryBlock().getFirstNonPHI());
  942. unsigned ArgOffset = 0;
  943. const DataLayout &DL = F->getParent()->getDataLayout();
  944. for (auto &FArg : F->args()) {
  945. if (!FArg.getType()->isSized()) {
  946. DEBUG(dbgs() << "Arg is not sized\n");
  947. continue;
  948. }
  949. unsigned Size =
  950. FArg.hasByValAttr()
  951. ? DL.getTypeAllocSize(FArg.getType()->getPointerElementType())
  952. : DL.getTypeAllocSize(FArg.getType());
  953. if (A == &FArg) {
  954. bool Overflow = ArgOffset + Size > kParamTLSSize;
  955. Value *Base = getShadowPtrForArgument(&FArg, EntryIRB, ArgOffset);
  956. if (FArg.hasByValAttr()) {
  957. // ByVal pointer itself has clean shadow. We copy the actual
  958. // argument shadow to the underlying memory.
  959. // Figure out maximal valid memcpy alignment.
  960. unsigned ArgAlign = FArg.getParamAlignment();
  961. if (ArgAlign == 0) {
  962. Type *EltType = A->getType()->getPointerElementType();
  963. ArgAlign = DL.getABITypeAlignment(EltType);
  964. }
  965. if (Overflow) {
  966. // ParamTLS overflow.
  967. EntryIRB.CreateMemSet(
  968. getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB),
  969. Constant::getNullValue(EntryIRB.getInt8Ty()), Size, ArgAlign);
  970. } else {
  971. unsigned CopyAlign = std::min(ArgAlign, kShadowTLSAlignment);
  972. Value *Cpy = EntryIRB.CreateMemCpy(
  973. getShadowPtr(V, EntryIRB.getInt8Ty(), EntryIRB), Base, Size,
  974. CopyAlign);
  975. DEBUG(dbgs() << " ByValCpy: " << *Cpy << "\n");
  976. (void)Cpy;
  977. }
  978. *ShadowPtr = getCleanShadow(V);
  979. } else {
  980. if (Overflow) {
  981. // ParamTLS overflow.
  982. *ShadowPtr = getCleanShadow(V);
  983. } else {
  984. *ShadowPtr =
  985. EntryIRB.CreateAlignedLoad(Base, kShadowTLSAlignment);
  986. }
  987. }
  988. DEBUG(dbgs() << " ARG: " << FArg << " ==> " <<
  989. **ShadowPtr << "\n");
  990. if (MS.TrackOrigins && !Overflow) {
  991. Value *OriginPtr =
  992. getOriginPtrForArgument(&FArg, EntryIRB, ArgOffset);
  993. setOrigin(A, EntryIRB.CreateLoad(OriginPtr));
  994. } else {
  995. setOrigin(A, getCleanOrigin());
  996. }
  997. }
  998. ArgOffset += RoundUpToAlignment(Size, kShadowTLSAlignment);
  999. }
  1000. assert(*ShadowPtr && "Could not find shadow for an argument");
  1001. return *ShadowPtr;
  1002. }
  1003. // For everything else the shadow is zero.
  1004. return getCleanShadow(V);
  1005. }
  1006. /// \brief Get the shadow for i-th argument of the instruction I.
  1007. Value *getShadow(Instruction *I, int i) {
  1008. return getShadow(I->getOperand(i));
  1009. }
  1010. /// \brief Get the origin for a value.
  1011. Value *getOrigin(Value *V) {
  1012. if (!MS.TrackOrigins) return nullptr;
  1013. if (!PropagateShadow) return getCleanOrigin();
  1014. if (isa<Constant>(V)) return getCleanOrigin();
  1015. assert((isa<Instruction>(V) || isa<Argument>(V)) &&
  1016. "Unexpected value type in getOrigin()");
  1017. Value *Origin = OriginMap[V];
  1018. assert(Origin && "Missing origin");
  1019. return Origin;
  1020. }
  1021. /// \brief Get the origin for i-th argument of the instruction I.
  1022. Value *getOrigin(Instruction *I, int i) {
  1023. return getOrigin(I->getOperand(i));
  1024. }
  1025. /// \brief Remember the place where a shadow check should be inserted.
  1026. ///
  1027. /// This location will be later instrumented with a check that will print a
  1028. /// UMR warning in runtime if the shadow value is not 0.
  1029. void insertShadowCheck(Value *Shadow, Value *Origin, Instruction *OrigIns) {
  1030. assert(Shadow);
  1031. if (!InsertChecks) return;
  1032. #ifndef NDEBUG
  1033. Type *ShadowTy = Shadow->getType();
  1034. assert((isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy)) &&
  1035. "Can only insert checks for integer and vector shadow types");
  1036. #endif
  1037. InstrumentationList.push_back(
  1038. ShadowOriginAndInsertPoint(Shadow, Origin, OrigIns));
  1039. }
  1040. /// \brief Remember the place where a shadow check should be inserted.
  1041. ///
  1042. /// This location will be later instrumented with a check that will print a
  1043. /// UMR warning in runtime if the value is not fully defined.
  1044. void insertShadowCheck(Value *Val, Instruction *OrigIns) {
  1045. assert(Val);
  1046. Value *Shadow, *Origin;
  1047. if (ClCheckConstantShadow) {
  1048. Shadow = getShadow(Val);
  1049. if (!Shadow) return;
  1050. Origin = getOrigin(Val);
  1051. } else {
  1052. Shadow = dyn_cast_or_null<Instruction>(getShadow(Val));
  1053. if (!Shadow) return;
  1054. Origin = dyn_cast_or_null<Instruction>(getOrigin(Val));
  1055. }
  1056. insertShadowCheck(Shadow, Origin, OrigIns);
  1057. }
  1058. AtomicOrdering addReleaseOrdering(AtomicOrdering a) {
  1059. switch (a) {
  1060. case NotAtomic:
  1061. return NotAtomic;
  1062. case Unordered:
  1063. case Monotonic:
  1064. case Release:
  1065. return Release;
  1066. case Acquire:
  1067. case AcquireRelease:
  1068. return AcquireRelease;
  1069. case SequentiallyConsistent:
  1070. return SequentiallyConsistent;
  1071. }
  1072. llvm_unreachable("Unknown ordering");
  1073. }
  1074. AtomicOrdering addAcquireOrdering(AtomicOrdering a) {
  1075. switch (a) {
  1076. case NotAtomic:
  1077. return NotAtomic;
  1078. case Unordered:
  1079. case Monotonic:
  1080. case Acquire:
  1081. return Acquire;
  1082. case Release:
  1083. case AcquireRelease:
  1084. return AcquireRelease;
  1085. case SequentiallyConsistent:
  1086. return SequentiallyConsistent;
  1087. }
  1088. llvm_unreachable("Unknown ordering");
  1089. }
  1090. // ------------------- Visitors.
  1091. /// \brief Instrument LoadInst
  1092. ///
  1093. /// Loads the corresponding shadow and (optionally) origin.
  1094. /// Optionally, checks that the load address is fully defined.
  1095. void visitLoadInst(LoadInst &I) {
  1096. assert(I.getType()->isSized() && "Load type must have size");
  1097. IRBuilder<> IRB(I.getNextNode());
  1098. Type *ShadowTy = getShadowTy(&I);
  1099. Value *Addr = I.getPointerOperand();
  1100. if (PropagateShadow && !I.getMetadata("nosanitize")) {
  1101. Value *ShadowPtr = getShadowPtr(Addr, ShadowTy, IRB);
  1102. setShadow(&I,
  1103. IRB.CreateAlignedLoad(ShadowPtr, I.getAlignment(), "_msld"));
  1104. } else {
  1105. setShadow(&I, getCleanShadow(&I));
  1106. }
  1107. if (ClCheckAccessAddress)
  1108. insertShadowCheck(I.getPointerOperand(), &I);
  1109. if (I.isAtomic())
  1110. I.setOrdering(addAcquireOrdering(I.getOrdering()));
  1111. if (MS.TrackOrigins) {
  1112. if (PropagateShadow) {
  1113. unsigned Alignment = I.getAlignment();
  1114. unsigned OriginAlignment = std::max(kMinOriginAlignment, Alignment);
  1115. setOrigin(&I, IRB.CreateAlignedLoad(getOriginPtr(Addr, IRB, Alignment),
  1116. OriginAlignment));
  1117. } else {
  1118. setOrigin(&I, getCleanOrigin());
  1119. }
  1120. }
  1121. }
  1122. /// \brief Instrument StoreInst
  1123. ///
  1124. /// Stores the corresponding shadow and (optionally) origin.
  1125. /// Optionally, checks that the store address is fully defined.
  1126. void visitStoreInst(StoreInst &I) {
  1127. StoreList.push_back(&I);
  1128. }
  1129. void handleCASOrRMW(Instruction &I) {
  1130. assert(isa<AtomicRMWInst>(I) || isa<AtomicCmpXchgInst>(I));
  1131. IRBuilder<> IRB(&I);
  1132. Value *Addr = I.getOperand(0);
  1133. Value *ShadowPtr = getShadowPtr(Addr, I.getType(), IRB);
  1134. if (ClCheckAccessAddress)
  1135. insertShadowCheck(Addr, &I);
  1136. // Only test the conditional argument of cmpxchg instruction.
  1137. // The other argument can potentially be uninitialized, but we can not
  1138. // detect this situation reliably without possible false positives.
  1139. if (isa<AtomicCmpXchgInst>(I))
  1140. insertShadowCheck(I.getOperand(1), &I);
  1141. IRB.CreateStore(getCleanShadow(&I), ShadowPtr);
  1142. setShadow(&I, getCleanShadow(&I));
  1143. setOrigin(&I, getCleanOrigin());
  1144. }
  1145. void visitAtomicRMWInst(AtomicRMWInst &I) {
  1146. handleCASOrRMW(I);
  1147. I.setOrdering(addReleaseOrdering(I.getOrdering()));
  1148. }
  1149. void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
  1150. handleCASOrRMW(I);
  1151. I.setSuccessOrdering(addReleaseOrdering(I.getSuccessOrdering()));
  1152. }
  1153. // Vector manipulation.
  1154. void visitExtractElementInst(ExtractElementInst &I) {
  1155. insertShadowCheck(I.getOperand(1), &I);
  1156. IRBuilder<> IRB(&I);
  1157. setShadow(&I, IRB.CreateExtractElement(getShadow(&I, 0), I.getOperand(1),
  1158. "_msprop"));
  1159. setOrigin(&I, getOrigin(&I, 0));
  1160. }
  1161. void visitInsertElementInst(InsertElementInst &I) {
  1162. insertShadowCheck(I.getOperand(2), &I);
  1163. IRBuilder<> IRB(&I);
  1164. setShadow(&I, IRB.CreateInsertElement(getShadow(&I, 0), getShadow(&I, 1),
  1165. I.getOperand(2), "_msprop"));
  1166. setOriginForNaryOp(I);
  1167. }
  1168. void visitShuffleVectorInst(ShuffleVectorInst &I) {
  1169. insertShadowCheck(I.getOperand(2), &I);
  1170. IRBuilder<> IRB(&I);
  1171. setShadow(&I, IRB.CreateShuffleVector(getShadow(&I, 0), getShadow(&I, 1),
  1172. I.getOperand(2), "_msprop"));
  1173. setOriginForNaryOp(I);
  1174. }
  1175. // Casts.
  1176. void visitSExtInst(SExtInst &I) {
  1177. IRBuilder<> IRB(&I);
  1178. setShadow(&I, IRB.CreateSExt(getShadow(&I, 0), I.getType(), "_msprop"));
  1179. setOrigin(&I, getOrigin(&I, 0));
  1180. }
  1181. void visitZExtInst(ZExtInst &I) {
  1182. IRBuilder<> IRB(&I);
  1183. setShadow(&I, IRB.CreateZExt(getShadow(&I, 0), I.getType(), "_msprop"));
  1184. setOrigin(&I, getOrigin(&I, 0));
  1185. }
  1186. void visitTruncInst(TruncInst &I) {
  1187. IRBuilder<> IRB(&I);
  1188. setShadow(&I, IRB.CreateTrunc(getShadow(&I, 0), I.getType(), "_msprop"));
  1189. setOrigin(&I, getOrigin(&I, 0));
  1190. }
  1191. void visitBitCastInst(BitCastInst &I) {
  1192. IRBuilder<> IRB(&I);
  1193. setShadow(&I, IRB.CreateBitCast(getShadow(&I, 0), getShadowTy(&I)));
  1194. setOrigin(&I, getOrigin(&I, 0));
  1195. }
  1196. void visitPtrToIntInst(PtrToIntInst &I) {
  1197. IRBuilder<> IRB(&I);
  1198. setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false,
  1199. "_msprop_ptrtoint"));
  1200. setOrigin(&I, getOrigin(&I, 0));
  1201. }
  1202. void visitIntToPtrInst(IntToPtrInst &I) {
  1203. IRBuilder<> IRB(&I);
  1204. setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false,
  1205. "_msprop_inttoptr"));
  1206. setOrigin(&I, getOrigin(&I, 0));
  1207. }
  1208. void visitFPToSIInst(CastInst& I) { handleShadowOr(I); }
  1209. void visitFPToUIInst(CastInst& I) { handleShadowOr(I); }
  1210. void visitSIToFPInst(CastInst& I) { handleShadowOr(I); }
  1211. void visitUIToFPInst(CastInst& I) { handleShadowOr(I); }
  1212. void visitFPExtInst(CastInst& I) { handleShadowOr(I); }
  1213. void visitFPTruncInst(CastInst& I) { handleShadowOr(I); }
  1214. /// \brief Propagate shadow for bitwise AND.
  1215. ///
  1216. /// This code is exact, i.e. if, for example, a bit in the left argument
  1217. /// is defined and 0, then neither the value not definedness of the
  1218. /// corresponding bit in B don't affect the resulting shadow.
  1219. void visitAnd(BinaryOperator &I) {
  1220. IRBuilder<> IRB(&I);
  1221. // "And" of 0 and a poisoned value results in unpoisoned value.
  1222. // 1&1 => 1; 0&1 => 0; p&1 => p;
  1223. // 1&0 => 0; 0&0 => 0; p&0 => 0;
  1224. // 1&p => p; 0&p => 0; p&p => p;
  1225. // S = (S1 & S2) | (V1 & S2) | (S1 & V2)
  1226. Value *S1 = getShadow(&I, 0);
  1227. Value *S2 = getShadow(&I, 1);
  1228. Value *V1 = I.getOperand(0);
  1229. Value *V2 = I.getOperand(1);
  1230. if (V1->getType() != S1->getType()) {
  1231. V1 = IRB.CreateIntCast(V1, S1->getType(), false);
  1232. V2 = IRB.CreateIntCast(V2, S2->getType(), false);
  1233. }
  1234. Value *S1S2 = IRB.CreateAnd(S1, S2);
  1235. Value *V1S2 = IRB.CreateAnd(V1, S2);
  1236. Value *S1V2 = IRB.CreateAnd(S1, V2);
  1237. setShadow(&I, IRB.CreateOr(S1S2, IRB.CreateOr(V1S2, S1V2)));
  1238. setOriginForNaryOp(I);
  1239. }
  1240. void visitOr(BinaryOperator &I) {
  1241. IRBuilder<> IRB(&I);
  1242. // "Or" of 1 and a poisoned value results in unpoisoned value.
  1243. // 1|1 => 1; 0|1 => 1; p|1 => 1;
  1244. // 1|0 => 1; 0|0 => 0; p|0 => p;
  1245. // 1|p => 1; 0|p => p; p|p => p;
  1246. // S = (S1 & S2) | (~V1 & S2) | (S1 & ~V2)
  1247. Value *S1 = getShadow(&I, 0);
  1248. Value *S2 = getShadow(&I, 1);
  1249. Value *V1 = IRB.CreateNot(I.getOperand(0));
  1250. Value *V2 = IRB.CreateNot(I.getOperand(1));
  1251. if (V1->getType() != S1->getType()) {
  1252. V1 = IRB.CreateIntCast(V1, S1->getType(), false);
  1253. V2 = IRB.CreateIntCast(V2, S2->getType(), false);
  1254. }
  1255. Value *S1S2 = IRB.CreateAnd(S1, S2);
  1256. Value *V1S2 = IRB.CreateAnd(V1, S2);
  1257. Value *S1V2 = IRB.CreateAnd(S1, V2);
  1258. setShadow(&I, IRB.CreateOr(S1S2, IRB.CreateOr(V1S2, S1V2)));
  1259. setOriginForNaryOp(I);
  1260. }
  1261. /// \brief Default propagation of shadow and/or origin.
  1262. ///
  1263. /// This class implements the general case of shadow propagation, used in all
  1264. /// cases where we don't know and/or don't care about what the operation
  1265. /// actually does. It converts all input shadow values to a common type
  1266. /// (extending or truncating as necessary), and bitwise OR's them.
  1267. ///
  1268. /// This is much cheaper than inserting checks (i.e. requiring inputs to be
  1269. /// fully initialized), and less prone to false positives.
  1270. ///
  1271. /// This class also implements the general case of origin propagation. For a
  1272. /// Nary operation, result origin is set to the origin of an argument that is
  1273. /// not entirely initialized. If there is more than one such arguments, the
  1274. /// rightmost of them is picked. It does not matter which one is picked if all
  1275. /// arguments are initialized.
  1276. template <bool CombineShadow>
  1277. class Combiner {
  1278. Value *Shadow;
  1279. Value *Origin;
  1280. IRBuilder<> &IRB;
  1281. MemorySanitizerVisitor *MSV;
  1282. public:
  1283. Combiner(MemorySanitizerVisitor *MSV, IRBuilder<> &IRB) :
  1284. Shadow(nullptr), Origin(nullptr), IRB(IRB), MSV(MSV) {}
  1285. /// \brief Add a pair of shadow and origin values to the mix.
  1286. Combiner &Add(Value *OpShadow, Value *OpOrigin) {
  1287. if (CombineShadow) {
  1288. assert(OpShadow);
  1289. if (!Shadow)
  1290. Shadow = OpShadow;
  1291. else {
  1292. OpShadow = MSV->CreateShadowCast(IRB, OpShadow, Shadow->getType());
  1293. Shadow = IRB.CreateOr(Shadow, OpShadow, "_msprop");
  1294. }
  1295. }
  1296. if (MSV->MS.TrackOrigins) {
  1297. assert(OpOrigin);
  1298. if (!Origin) {
  1299. Origin = OpOrigin;
  1300. } else {
  1301. Constant *ConstOrigin = dyn_cast<Constant>(OpOrigin);
  1302. // No point in adding something that might result in 0 origin value.
  1303. if (!ConstOrigin || !ConstOrigin->isNullValue()) {
  1304. Value *FlatShadow = MSV->convertToShadowTyNoVec(OpShadow, IRB);
  1305. Value *Cond =
  1306. IRB.CreateICmpNE(FlatShadow, MSV->getCleanShadow(FlatShadow));
  1307. Origin = IRB.CreateSelect(Cond, OpOrigin, Origin);
  1308. }
  1309. }
  1310. }
  1311. return *this;
  1312. }
  1313. /// \brief Add an application value to the mix.
  1314. Combiner &Add(Value *V) {
  1315. Value *OpShadow = MSV->getShadow(V);
  1316. Value *OpOrigin = MSV->MS.TrackOrigins ? MSV->getOrigin(V) : nullptr;
  1317. return Add(OpShadow, OpOrigin);
  1318. }
  1319. /// \brief Set the current combined values as the given instruction's shadow
  1320. /// and origin.
  1321. void Done(Instruction *I) {
  1322. if (CombineShadow) {
  1323. assert(Shadow);
  1324. Shadow = MSV->CreateShadowCast(IRB, Shadow, MSV->getShadowTy(I));
  1325. MSV->setShadow(I, Shadow);
  1326. }
  1327. if (MSV->MS.TrackOrigins) {
  1328. assert(Origin);
  1329. MSV->setOrigin(I, Origin);
  1330. }
  1331. }
  1332. };
  1333. typedef Combiner<true> ShadowAndOriginCombiner;
  1334. typedef Combiner<false> OriginCombiner;
  1335. /// \brief Propagate origin for arbitrary operation.
  1336. void setOriginForNaryOp(Instruction &I) {
  1337. if (!MS.TrackOrigins) return;
  1338. IRBuilder<> IRB(&I);
  1339. OriginCombiner OC(this, IRB);
  1340. for (Instruction::op_iterator OI = I.op_begin(); OI != I.op_end(); ++OI)
  1341. OC.Add(OI->get());
  1342. OC.Done(&I);
  1343. }
  1344. size_t VectorOrPrimitiveTypeSizeInBits(Type *Ty) {
  1345. assert(!(Ty->isVectorTy() && Ty->getScalarType()->isPointerTy()) &&
  1346. "Vector of pointers is not a valid shadow type");
  1347. return Ty->isVectorTy() ?
  1348. Ty->getVectorNumElements() * Ty->getScalarSizeInBits() :
  1349. Ty->getPrimitiveSizeInBits();
  1350. }
  1351. /// \brief Cast between two shadow types, extending or truncating as
  1352. /// necessary.
  1353. Value *CreateShadowCast(IRBuilder<> &IRB, Value *V, Type *dstTy,
  1354. bool Signed = false) {
  1355. Type *srcTy = V->getType();
  1356. if (dstTy->isIntegerTy() && srcTy->isIntegerTy())
  1357. return IRB.CreateIntCast(V, dstTy, Signed);
  1358. if (dstTy->isVectorTy() && srcTy->isVectorTy() &&
  1359. dstTy->getVectorNumElements() == srcTy->getVectorNumElements())
  1360. return IRB.CreateIntCast(V, dstTy, Signed);
  1361. size_t srcSizeInBits = VectorOrPrimitiveTypeSizeInBits(srcTy);
  1362. size_t dstSizeInBits = VectorOrPrimitiveTypeSizeInBits(dstTy);
  1363. Value *V1 = IRB.CreateBitCast(V, Type::getIntNTy(*MS.C, srcSizeInBits));
  1364. Value *V2 =
  1365. IRB.CreateIntCast(V1, Type::getIntNTy(*MS.C, dstSizeInBits), Signed);
  1366. return IRB.CreateBitCast(V2, dstTy);
  1367. // TODO: handle struct types.
  1368. }
  1369. /// \brief Cast an application value to the type of its own shadow.
  1370. Value *CreateAppToShadowCast(IRBuilder<> &IRB, Value *V) {
  1371. Type *ShadowTy = getShadowTy(V);
  1372. if (V->getType() == ShadowTy)
  1373. return V;
  1374. if (V->getType()->isPtrOrPtrVectorTy())
  1375. return IRB.CreatePtrToInt(V, ShadowTy);
  1376. else
  1377. return IRB.CreateBitCast(V, ShadowTy);
  1378. }
  1379. /// \brief Propagate shadow for arbitrary operation.
  1380. void handleShadowOr(Instruction &I) {
  1381. IRBuilder<> IRB(&I);
  1382. ShadowAndOriginCombiner SC(this, IRB);
  1383. for (Instruction::op_iterator OI = I.op_begin(); OI != I.op_end(); ++OI)
  1384. SC.Add(OI->get());
  1385. SC.Done(&I);
  1386. }
  1387. // \brief Handle multiplication by constant.
  1388. //
  1389. // Handle a special case of multiplication by constant that may have one or
  1390. // more zeros in the lower bits. This makes corresponding number of lower bits
  1391. // of the result zero as well. We model it by shifting the other operand
  1392. // shadow left by the required number of bits. Effectively, we transform
  1393. // (X * (A * 2**B)) to ((X << B) * A) and instrument (X << B) as (Sx << B).
  1394. // We use multiplication by 2**N instead of shift to cover the case of
  1395. // multiplication by 0, which may occur in some elements of a vector operand.
  1396. void handleMulByConstant(BinaryOperator &I, Constant *ConstArg,
  1397. Value *OtherArg) {
  1398. Constant *ShadowMul;
  1399. Type *Ty = ConstArg->getType();
  1400. if (Ty->isVectorTy()) {
  1401. unsigned NumElements = Ty->getVectorNumElements();
  1402. Type *EltTy = Ty->getSequentialElementType();
  1403. SmallVector<Constant *, 16> Elements;
  1404. for (unsigned Idx = 0; Idx < NumElements; ++Idx) {
  1405. ConstantInt *Elt =
  1406. dyn_cast<ConstantInt>(ConstArg->getAggregateElement(Idx));
  1407. APInt V = Elt->getValue();
  1408. APInt V2 = APInt(V.getBitWidth(), 1) << V.countTrailingZeros();
  1409. Elements.push_back(ConstantInt::get(EltTy, V2));
  1410. }
  1411. ShadowMul = ConstantVector::get(Elements);
  1412. } else {
  1413. ConstantInt *Elt = dyn_cast<ConstantInt>(ConstArg);
  1414. APInt V = Elt->getValue();
  1415. APInt V2 = APInt(V.getBitWidth(), 1) << V.countTrailingZeros();
  1416. ShadowMul = ConstantInt::get(Elt->getType(), V2);
  1417. }
  1418. IRBuilder<> IRB(&I);
  1419. setShadow(&I,
  1420. IRB.CreateMul(getShadow(OtherArg), ShadowMul, "msprop_mul_cst"));
  1421. setOrigin(&I, getOrigin(OtherArg));
  1422. }
  1423. void visitMul(BinaryOperator &I) {
  1424. Constant *constOp0 = dyn_cast<Constant>(I.getOperand(0));
  1425. Constant *constOp1 = dyn_cast<Constant>(I.getOperand(1));
  1426. if (constOp0 && !constOp1)
  1427. handleMulByConstant(I, constOp0, I.getOperand(1));
  1428. else if (constOp1 && !constOp0)
  1429. handleMulByConstant(I, constOp1, I.getOperand(0));
  1430. else
  1431. handleShadowOr(I);
  1432. }
  1433. void visitFAdd(BinaryOperator &I) { handleShadowOr(I); }
  1434. void visitFSub(BinaryOperator &I) { handleShadowOr(I); }
  1435. void visitFMul(BinaryOperator &I) { handleShadowOr(I); }
  1436. void visitAdd(BinaryOperator &I) { handleShadowOr(I); }
  1437. void visitSub(BinaryOperator &I) { handleShadowOr(I); }
  1438. void visitXor(BinaryOperator &I) { handleShadowOr(I); }
  1439. void handleDiv(Instruction &I) {
  1440. IRBuilder<> IRB(&I);
  1441. // Strict on the second argument.
  1442. insertShadowCheck(I.getOperand(1), &I);
  1443. setShadow(&I, getShadow(&I, 0));
  1444. setOrigin(&I, getOrigin(&I, 0));
  1445. }
  1446. void visitUDiv(BinaryOperator &I) { handleDiv(I); }
  1447. void visitSDiv(BinaryOperator &I) { handleDiv(I); }
  1448. void visitFDiv(BinaryOperator &I) { handleDiv(I); }
  1449. void visitURem(BinaryOperator &I) { handleDiv(I); }
  1450. void visitSRem(BinaryOperator &I) { handleDiv(I); }
  1451. void visitFRem(BinaryOperator &I) { handleDiv(I); }
  1452. /// \brief Instrument == and != comparisons.
  1453. ///
  1454. /// Sometimes the comparison result is known even if some of the bits of the
  1455. /// arguments are not.
  1456. void handleEqualityComparison(ICmpInst &I) {
  1457. IRBuilder<> IRB(&I);
  1458. Value *A = I.getOperand(0);
  1459. Value *B = I.getOperand(1);
  1460. Value *Sa = getShadow(A);
  1461. Value *Sb = getShadow(B);
  1462. // Get rid of pointers and vectors of pointers.
  1463. // For ints (and vectors of ints), types of A and Sa match,
  1464. // and this is a no-op.
  1465. A = IRB.CreatePointerCast(A, Sa->getType());
  1466. B = IRB.CreatePointerCast(B, Sb->getType());
  1467. // A == B <==> (C = A^B) == 0
  1468. // A != B <==> (C = A^B) != 0
  1469. // Sc = Sa | Sb
  1470. Value *C = IRB.CreateXor(A, B);
  1471. Value *Sc = IRB.CreateOr(Sa, Sb);
  1472. // Now dealing with i = (C == 0) comparison (or C != 0, does not matter now)
  1473. // Result is defined if one of the following is true
  1474. // * there is a defined 1 bit in C
  1475. // * C is fully defined
  1476. // Si = !(C & ~Sc) && Sc
  1477. Value *Zero = Constant::getNullValue(Sc->getType());
  1478. Value *MinusOne = Constant::getAllOnesValue(Sc->getType());
  1479. Value *Si =
  1480. IRB.CreateAnd(IRB.CreateICmpNE(Sc, Zero),
  1481. IRB.CreateICmpEQ(
  1482. IRB.CreateAnd(IRB.CreateXor(Sc, MinusOne), C), Zero));
  1483. Si->setName("_msprop_icmp");
  1484. setShadow(&I, Si);
  1485. setOriginForNaryOp(I);
  1486. }
  1487. /// \brief Build the lowest possible value of V, taking into account V's
  1488. /// uninitialized bits.
  1489. Value *getLowestPossibleValue(IRBuilder<> &IRB, Value *A, Value *Sa,
  1490. bool isSigned) {
  1491. if (isSigned) {
  1492. // Split shadow into sign bit and other bits.
  1493. Value *SaOtherBits = IRB.CreateLShr(IRB.CreateShl(Sa, 1), 1);
  1494. Value *SaSignBit = IRB.CreateXor(Sa, SaOtherBits);
  1495. // Maximise the undefined shadow bit, minimize other undefined bits.
  1496. return
  1497. IRB.CreateOr(IRB.CreateAnd(A, IRB.CreateNot(SaOtherBits)), SaSignBit);
  1498. } else {
  1499. // Minimize undefined bits.
  1500. return IRB.CreateAnd(A, IRB.CreateNot(Sa));
  1501. }
  1502. }
  1503. /// \brief Build the highest possible value of V, taking into account V's
  1504. /// uninitialized bits.
  1505. Value *getHighestPossibleValue(IRBuilder<> &IRB, Value *A, Value *Sa,
  1506. bool isSigned) {
  1507. if (isSigned) {
  1508. // Split shadow into sign bit and other bits.
  1509. Value *SaOtherBits = IRB.CreateLShr(IRB.CreateShl(Sa, 1), 1);
  1510. Value *SaSignBit = IRB.CreateXor(Sa, SaOtherBits);
  1511. // Minimise the undefined shadow bit, maximise other undefined bits.
  1512. return
  1513. IRB.CreateOr(IRB.CreateAnd(A, IRB.CreateNot(SaSignBit)), SaOtherBits);
  1514. } else {
  1515. // Maximize undefined bits.
  1516. return IRB.CreateOr(A, Sa);
  1517. }
  1518. }
  1519. /// \brief Instrument relational comparisons.
  1520. ///
  1521. /// This function does exact shadow propagation for all relational
  1522. /// comparisons of integers, pointers and vectors of those.
  1523. /// FIXME: output seems suboptimal when one of the operands is a constant
  1524. void handleRelationalComparisonExact(ICmpInst &I) {
  1525. IRBuilder<> IRB(&I);
  1526. Value *A = I.getOperand(0);
  1527. Value *B = I.getOperand(1);
  1528. Value *Sa = getShadow(A);
  1529. Value *Sb = getShadow(B);
  1530. // Get rid of pointers and vectors of pointers.
  1531. // For ints (and vectors of ints), types of A and Sa match,
  1532. // and this is a no-op.
  1533. A = IRB.CreatePointerCast(A, Sa->getType());
  1534. B = IRB.CreatePointerCast(B, Sb->getType());
  1535. // Let [a0, a1] be the interval of possible values of A, taking into account
  1536. // its undefined bits. Let [b0, b1] be the interval of possible values of B.
  1537. // Then (A cmp B) is defined iff (a0 cmp b1) == (a1 cmp b0).
  1538. bool IsSigned = I.isSigned();
  1539. Value *S1 = IRB.CreateICmp(I.getPredicate(),
  1540. getLowestPossibleValue(IRB, A, Sa, IsSigned),
  1541. getHighestPossibleValue(IRB, B, Sb, IsSigned));
  1542. Value *S2 = IRB.CreateICmp(I.getPredicate(),
  1543. getHighestPossibleValue(IRB, A, Sa, IsSigned),
  1544. getLowestPossibleValue(IRB, B, Sb, IsSigned));
  1545. Value *Si = IRB.CreateXor(S1, S2);
  1546. setShadow(&I, Si);
  1547. setOriginForNaryOp(I);
  1548. }
  1549. /// \brief Instrument signed relational comparisons.
  1550. ///
  1551. /// Handle (x<0) and (x>=0) comparisons (essentially, sign bit tests) by
  1552. /// propagating the highest bit of the shadow. Everything else is delegated
  1553. /// to handleShadowOr().
  1554. void handleSignedRelationalComparison(ICmpInst &I) {
  1555. Constant *constOp0 = dyn_cast<Constant>(I.getOperand(0));
  1556. Constant *constOp1 = dyn_cast<Constant>(I.getOperand(1));
  1557. Value* op = nullptr;
  1558. CmpInst::Predicate pre = I.getPredicate();
  1559. if (constOp0 && constOp0->isNullValue() &&
  1560. (pre == CmpInst::ICMP_SGT || pre == CmpInst::ICMP_SLE)) {
  1561. op = I.getOperand(1);
  1562. } else if (constOp1 && constOp1->isNullValue() &&
  1563. (pre == CmpInst::ICMP_SLT || pre == CmpInst::ICMP_SGE)) {
  1564. op = I.getOperand(0);
  1565. }
  1566. if (op) {
  1567. IRBuilder<> IRB(&I);
  1568. Value* Shadow =
  1569. IRB.CreateICmpSLT(getShadow(op), getCleanShadow(op), "_msprop_icmpslt");
  1570. setShadow(&I, Shadow);
  1571. setOrigin(&I, getOrigin(op));
  1572. } else {
  1573. handleShadowOr(I);
  1574. }
  1575. }
  1576. void visitICmpInst(ICmpInst &I) {
  1577. if (!ClHandleICmp) {
  1578. handleShadowOr(I);
  1579. return;
  1580. }
  1581. if (I.isEquality()) {
  1582. handleEqualityComparison(I);
  1583. return;
  1584. }
  1585. assert(I.isRelational());
  1586. if (ClHandleICmpExact) {
  1587. handleRelationalComparisonExact(I);
  1588. return;
  1589. }
  1590. if (I.isSigned()) {
  1591. handleSignedRelationalComparison(I);
  1592. return;
  1593. }
  1594. assert(I.isUnsigned());
  1595. if ((isa<Constant>(I.getOperand(0)) || isa<Constant>(I.getOperand(1)))) {
  1596. handleRelationalComparisonExact(I);
  1597. return;
  1598. }
  1599. handleShadowOr(I);
  1600. }
  1601. void visitFCmpInst(FCmpInst &I) {
  1602. handleShadowOr(I);
  1603. }
  1604. void handleShift(BinaryOperator &I) {
  1605. IRBuilder<> IRB(&I);
  1606. // If any of the S2 bits are poisoned, the whole thing is poisoned.
  1607. // Otherwise perform the same shift on S1.
  1608. Value *S1 = getShadow(&I, 0);
  1609. Value *S2 = getShadow(&I, 1);
  1610. Value *S2Conv = IRB.CreateSExt(IRB.CreateICmpNE(S2, getCleanShadow(S2)),
  1611. S2->getType());
  1612. Value *V2 = I.getOperand(1);
  1613. Value *Shift = IRB.CreateBinOp(I.getOpcode(), S1, V2);
  1614. setShadow(&I, IRB.CreateOr(Shift, S2Conv));
  1615. setOriginForNaryOp(I);
  1616. }
  1617. void visitShl(BinaryOperator &I) { handleShift(I); }
  1618. void visitAShr(BinaryOperator &I) { handleShift(I); }
  1619. void visitLShr(BinaryOperator &I) { handleShift(I); }
  1620. /// \brief Instrument llvm.memmove
  1621. ///
  1622. /// At this point we don't know if llvm.memmove will be inlined or not.
  1623. /// If we don't instrument it and it gets inlined,
  1624. /// our interceptor will not kick in and we will lose the memmove.
  1625. /// If we instrument the call here, but it does not get inlined,
  1626. /// we will memove the shadow twice: which is bad in case
  1627. /// of overlapping regions. So, we simply lower the intrinsic to a call.
  1628. ///
  1629. /// Similar situation exists for memcpy and memset.
  1630. void visitMemMoveInst(MemMoveInst &I) {
  1631. IRBuilder<> IRB(&I);
  1632. IRB.CreateCall(
  1633. MS.MemmoveFn,
  1634. {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
  1635. IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()),
  1636. IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
  1637. I.eraseFromParent();
  1638. }
  1639. // Similar to memmove: avoid copying shadow twice.
  1640. // This is somewhat unfortunate as it may slowdown small constant memcpys.
  1641. // FIXME: consider doing manual inline for small constant sizes and proper
  1642. // alignment.
  1643. void visitMemCpyInst(MemCpyInst &I) {
  1644. IRBuilder<> IRB(&I);
  1645. IRB.CreateCall(
  1646. MS.MemcpyFn,
  1647. {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
  1648. IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()),
  1649. IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
  1650. I.eraseFromParent();
  1651. }
  1652. // Same as memcpy.
  1653. void visitMemSetInst(MemSetInst &I) {
  1654. IRBuilder<> IRB(&I);
  1655. IRB.CreateCall(
  1656. MS.MemsetFn,
  1657. {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
  1658. IRB.CreateIntCast(I.getArgOperand(1), IRB.getInt32Ty(), false),
  1659. IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
  1660. I.eraseFromParent();
  1661. }
  1662. void visitVAStartInst(VAStartInst &I) {
  1663. VAHelper->visitVAStartInst(I);
  1664. }
  1665. void visitVACopyInst(VACopyInst &I) {
  1666. VAHelper->visitVACopyInst(I);
  1667. }
  1668. enum IntrinsicKind {
  1669. IK_DoesNotAccessMemory,
  1670. IK_OnlyReadsMemory,
  1671. IK_WritesMemory
  1672. };
  1673. static IntrinsicKind getIntrinsicKind(Intrinsic::ID iid) {
  1674. const int DoesNotAccessMemory = IK_DoesNotAccessMemory;
  1675. const int OnlyReadsArgumentPointees = IK_OnlyReadsMemory;
  1676. const int OnlyReadsMemory = IK_OnlyReadsMemory;
  1677. const int OnlyAccessesArgumentPointees = IK_WritesMemory;
  1678. const int UnknownModRefBehavior = IK_WritesMemory;
  1679. #define GET_INTRINSIC_MODREF_BEHAVIOR
  1680. #define ModRefBehavior IntrinsicKind
  1681. #include "llvm/IR/Intrinsics.gen"
  1682. #undef ModRefBehavior
  1683. #undef GET_INTRINSIC_MODREF_BEHAVIOR
  1684. }
  1685. /// \brief Handle vector store-like intrinsics.
  1686. ///
  1687. /// Instrument intrinsics that look like a simple SIMD store: writes memory,
  1688. /// has 1 pointer argument and 1 vector argument, returns void.
  1689. bool handleVectorStoreIntrinsic(IntrinsicInst &I) {
  1690. IRBuilder<> IRB(&I);
  1691. Value* Addr = I.getArgOperand(0);
  1692. Value *Shadow = getShadow(&I, 1);
  1693. Value *ShadowPtr = getShadowPtr(Addr, Shadow->getType(), IRB);
  1694. // We don't know the pointer alignment (could be unaligned SSE store!).
  1695. // Have to assume to worst case.
  1696. IRB.CreateAlignedStore(Shadow, ShadowPtr, 1);
  1697. if (ClCheckAccessAddress)
  1698. insertShadowCheck(Addr, &I);
  1699. // FIXME: use ClStoreCleanOrigin
  1700. // FIXME: factor out common code from materializeStores
  1701. if (MS.TrackOrigins)
  1702. IRB.CreateStore(getOrigin(&I, 1), getOriginPtr(Addr, IRB, 1));
  1703. return true;
  1704. }
  1705. /// \brief Handle vector load-like intrinsics.
  1706. ///
  1707. /// Instrument intrinsics that look like a simple SIMD load: reads memory,
  1708. /// has 1 pointer argument, returns a vector.
  1709. bool handleVectorLoadIntrinsic(IntrinsicInst &I) {
  1710. IRBuilder<> IRB(&I);
  1711. Value *Addr = I.getArgOperand(0);
  1712. Type *ShadowTy = getShadowTy(&I);
  1713. if (PropagateShadow) {
  1714. Value *ShadowPtr = getShadowPtr(Addr, ShadowTy, IRB);
  1715. // We don't know the pointer alignment (could be unaligned SSE load!).
  1716. // Have to assume to worst case.
  1717. setShadow(&I, IRB.CreateAlignedLoad(ShadowPtr, 1, "_msld"));
  1718. } else {
  1719. setShadow(&I, getCleanShadow(&I));
  1720. }
  1721. if (ClCheckAccessAddress)
  1722. insertShadowCheck(Addr, &I);
  1723. if (MS.TrackOrigins) {
  1724. if (PropagateShadow)
  1725. setOrigin(&I, IRB.CreateLoad(getOriginPtr(Addr, IRB, 1)));
  1726. else
  1727. setOrigin(&I, getCleanOrigin());
  1728. }
  1729. return true;
  1730. }
  1731. /// \brief Handle (SIMD arithmetic)-like intrinsics.
  1732. ///
  1733. /// Instrument intrinsics with any number of arguments of the same type,
  1734. /// equal to the return type. The type should be simple (no aggregates or
  1735. /// pointers; vectors are fine).
  1736. /// Caller guarantees that this intrinsic does not access memory.
  1737. bool maybeHandleSimpleNomemIntrinsic(IntrinsicInst &I) {
  1738. Type *RetTy = I.getType();
  1739. if (!(RetTy->isIntOrIntVectorTy() ||
  1740. RetTy->isFPOrFPVectorTy() ||
  1741. RetTy->isX86_MMXTy()))
  1742. return false;
  1743. unsigned NumArgOperands = I.getNumArgOperands();
  1744. for (unsigned i = 0; i < NumArgOperands; ++i) {
  1745. Type *Ty = I.getArgOperand(i)->getType();
  1746. if (Ty != RetTy)
  1747. return false;
  1748. }
  1749. IRBuilder<> IRB(&I);
  1750. ShadowAndOriginCombiner SC(this, IRB);
  1751. for (unsigned i = 0; i < NumArgOperands; ++i)
  1752. SC.Add(I.getArgOperand(i));
  1753. SC.Done(&I);
  1754. return true;
  1755. }
  1756. /// \brief Heuristically instrument unknown intrinsics.
  1757. ///
  1758. /// The main purpose of this code is to do something reasonable with all
  1759. /// random intrinsics we might encounter, most importantly - SIMD intrinsics.
  1760. /// We recognize several classes of intrinsics by their argument types and
  1761. /// ModRefBehaviour and apply special intrumentation when we are reasonably
  1762. /// sure that we know what the intrinsic does.
  1763. ///
  1764. /// We special-case intrinsics where this approach fails. See llvm.bswap
  1765. /// handling as an example of that.
  1766. bool handleUnknownIntrinsic(IntrinsicInst &I) {
  1767. unsigned NumArgOperands = I.getNumArgOperands();
  1768. if (NumArgOperands == 0)
  1769. return false;
  1770. Intrinsic::ID iid = I.getIntrinsicID();
  1771. IntrinsicKind IK = getIntrinsicKind(iid);
  1772. bool OnlyReadsMemory = IK == IK_OnlyReadsMemory;
  1773. bool WritesMemory = IK == IK_WritesMemory;
  1774. assert(!(OnlyReadsMemory && WritesMemory));
  1775. if (NumArgOperands == 2 &&
  1776. I.getArgOperand(0)->getType()->isPointerTy() &&
  1777. I.getArgOperand(1)->getType()->isVectorTy() &&
  1778. I.getType()->isVoidTy() &&
  1779. WritesMemory) {
  1780. // This looks like a vector store.
  1781. return handleVectorStoreIntrinsic(I);
  1782. }
  1783. if (NumArgOperands == 1 &&
  1784. I.getArgOperand(0)->getType()->isPointerTy() &&
  1785. I.getType()->isVectorTy() &&
  1786. OnlyReadsMemory) {
  1787. // This looks like a vector load.
  1788. return handleVectorLoadIntrinsic(I);
  1789. }
  1790. if (!OnlyReadsMemory && !WritesMemory)
  1791. if (maybeHandleSimpleNomemIntrinsic(I))
  1792. return true;
  1793. // FIXME: detect and handle SSE maskstore/maskload
  1794. return false;
  1795. }
  1796. void handleBswap(IntrinsicInst &I) {
  1797. IRBuilder<> IRB(&I);
  1798. Value *Op = I.getArgOperand(0);
  1799. Type *OpType = Op->getType();
  1800. Function *BswapFunc = Intrinsic::getDeclaration(
  1801. F.getParent(), Intrinsic::bswap, makeArrayRef(&OpType, 1));
  1802. setShadow(&I, IRB.CreateCall(BswapFunc, getShadow(Op)));
  1803. setOrigin(&I, getOrigin(Op));
  1804. }
  1805. // \brief Instrument vector convert instrinsic.
  1806. //
  1807. // This function instruments intrinsics like cvtsi2ss:
  1808. // %Out = int_xxx_cvtyyy(%ConvertOp)
  1809. // or
  1810. // %Out = int_xxx_cvtyyy(%CopyOp, %ConvertOp)
  1811. // Intrinsic converts \p NumUsedElements elements of \p ConvertOp to the same
  1812. // number \p Out elements, and (if has 2 arguments) copies the rest of the
  1813. // elements from \p CopyOp.
  1814. // In most cases conversion involves floating-point value which may trigger a
  1815. // hardware exception when not fully initialized. For this reason we require
  1816. // \p ConvertOp[0:NumUsedElements] to be fully initialized and trap otherwise.
  1817. // We copy the shadow of \p CopyOp[NumUsedElements:] to \p
  1818. // Out[NumUsedElements:]. This means that intrinsics without \p CopyOp always
  1819. // return a fully initialized value.
  1820. void handleVectorConvertIntrinsic(IntrinsicInst &I, int NumUsedElements) {
  1821. IRBuilder<> IRB(&I);
  1822. Value *CopyOp, *ConvertOp;
  1823. switch (I.getNumArgOperands()) {
  1824. case 3:
  1825. assert(isa<ConstantInt>(I.getArgOperand(2)) && "Invalid rounding mode");
  1826. case 2:
  1827. CopyOp = I.getArgOperand(0);
  1828. ConvertOp = I.getArgOperand(1);
  1829. break;
  1830. case 1:
  1831. ConvertOp = I.getArgOperand(0);
  1832. CopyOp = nullptr;
  1833. break;
  1834. default:
  1835. llvm_unreachable("Cvt intrinsic with unsupported number of arguments.");
  1836. }
  1837. // The first *NumUsedElements* elements of ConvertOp are converted to the
  1838. // same number of output elements. The rest of the output is copied from
  1839. // CopyOp, or (if not available) filled with zeroes.
  1840. // Combine shadow for elements of ConvertOp that are used in this operation,
  1841. // and insert a check.
  1842. // FIXME: consider propagating shadow of ConvertOp, at least in the case of
  1843. // int->any conversion.
  1844. Value *ConvertShadow = getShadow(ConvertOp);
  1845. Value *AggShadow = nullptr;
  1846. if (ConvertOp->getType()->isVectorTy()) {
  1847. AggShadow = IRB.CreateExtractElement(
  1848. ConvertShadow, ConstantInt::get(IRB.getInt32Ty(), 0));
  1849. for (int i = 1; i < NumUsedElements; ++i) {
  1850. Value *MoreShadow = IRB.CreateExtractElement(
  1851. ConvertShadow, ConstantInt::get(IRB.getInt32Ty(), i));
  1852. AggShadow = IRB.CreateOr(AggShadow, MoreShadow);
  1853. }
  1854. } else {
  1855. AggShadow = ConvertShadow;
  1856. }
  1857. assert(AggShadow->getType()->isIntegerTy());
  1858. insertShadowCheck(AggShadow, getOrigin(ConvertOp), &I);
  1859. // Build result shadow by zero-filling parts of CopyOp shadow that come from
  1860. // ConvertOp.
  1861. if (CopyOp) {
  1862. assert(CopyOp->getType() == I.getType());
  1863. assert(CopyOp->getType()->isVectorTy());
  1864. Value *ResultShadow = getShadow(CopyOp);
  1865. Type *EltTy = ResultShadow->getType()->getVectorElementType();
  1866. for (int i = 0; i < NumUsedElements; ++i) {
  1867. ResultShadow = IRB.CreateInsertElement(
  1868. ResultShadow, ConstantInt::getNullValue(EltTy),
  1869. ConstantInt::get(IRB.getInt32Ty(), i));
  1870. }
  1871. setShadow(&I, ResultShadow);
  1872. setOrigin(&I, getOrigin(CopyOp));
  1873. } else {
  1874. setShadow(&I, getCleanShadow(&I));
  1875. setOrigin(&I, getCleanOrigin());
  1876. }
  1877. }
  1878. // Given a scalar or vector, extract lower 64 bits (or less), and return all
  1879. // zeroes if it is zero, and all ones otherwise.
  1880. Value *Lower64ShadowExtend(IRBuilder<> &IRB, Value *S, Type *T) {
  1881. if (S->getType()->isVectorTy())
  1882. S = CreateShadowCast(IRB, S, IRB.getInt64Ty(), /* Signed */ true);
  1883. assert(S->getType()->getPrimitiveSizeInBits() <= 64);
  1884. Value *S2 = IRB.CreateICmpNE(S, getCleanShadow(S));
  1885. return CreateShadowCast(IRB, S2, T, /* Signed */ true);
  1886. }
  1887. Value *VariableShadowExtend(IRBuilder<> &IRB, Value *S) {
  1888. Type *T = S->getType();
  1889. assert(T->isVectorTy());
  1890. Value *S2 = IRB.CreateICmpNE(S, getCleanShadow(S));
  1891. return IRB.CreateSExt(S2, T);
  1892. }
  1893. // \brief Instrument vector shift instrinsic.
  1894. //
  1895. // This function instruments intrinsics like int_x86_avx2_psll_w.
  1896. // Intrinsic shifts %In by %ShiftSize bits.
  1897. // %ShiftSize may be a vector. In that case the lower 64 bits determine shift
  1898. // size, and the rest is ignored. Behavior is defined even if shift size is
  1899. // greater than register (or field) width.
  1900. void handleVectorShiftIntrinsic(IntrinsicInst &I, bool Variable) {
  1901. assert(I.getNumArgOperands() == 2);
  1902. IRBuilder<> IRB(&I);
  1903. // If any of the S2 bits are poisoned, the whole thing is poisoned.
  1904. // Otherwise perform the same shift on S1.
  1905. Value *S1 = getShadow(&I, 0);
  1906. Value *S2 = getShadow(&I, 1);
  1907. Value *S2Conv = Variable ? VariableShadowExtend(IRB, S2)
  1908. : Lower64ShadowExtend(IRB, S2, getShadowTy(&I));
  1909. Value *V1 = I.getOperand(0);
  1910. Value *V2 = I.getOperand(1);
  1911. Value *Shift = IRB.CreateCall(I.getCalledValue(),
  1912. {IRB.CreateBitCast(S1, V1->getType()), V2});
  1913. Shift = IRB.CreateBitCast(Shift, getShadowTy(&I));
  1914. setShadow(&I, IRB.CreateOr(Shift, S2Conv));
  1915. setOriginForNaryOp(I);
  1916. }
  1917. // \brief Get an X86_MMX-sized vector type.
  1918. Type *getMMXVectorTy(unsigned EltSizeInBits) {
  1919. const unsigned X86_MMXSizeInBits = 64;
  1920. return VectorType::get(IntegerType::get(*MS.C, EltSizeInBits),
  1921. X86_MMXSizeInBits / EltSizeInBits);
  1922. }
  1923. // \brief Returns a signed counterpart for an (un)signed-saturate-and-pack
  1924. // intrinsic.
  1925. Intrinsic::ID getSignedPackIntrinsic(Intrinsic::ID id) {
  1926. switch (id) {
  1927. case llvm::Intrinsic::x86_sse2_packsswb_128:
  1928. case llvm::Intrinsic::x86_sse2_packuswb_128:
  1929. return llvm::Intrinsic::x86_sse2_packsswb_128;
  1930. case llvm::Intrinsic::x86_sse2_packssdw_128:
  1931. case llvm::Intrinsic::x86_sse41_packusdw:
  1932. return llvm::Intrinsic::x86_sse2_packssdw_128;
  1933. case llvm::Intrinsic::x86_avx2_packsswb:
  1934. case llvm::Intrinsic::x86_avx2_packuswb:
  1935. return llvm::Intrinsic::x86_avx2_packsswb;
  1936. case llvm::Intrinsic::x86_avx2_packssdw:
  1937. case llvm::Intrinsic::x86_avx2_packusdw:
  1938. return llvm::Intrinsic::x86_avx2_packssdw;
  1939. case llvm::Intrinsic::x86_mmx_packsswb:
  1940. case llvm::Intrinsic::x86_mmx_packuswb:
  1941. return llvm::Intrinsic::x86_mmx_packsswb;
  1942. case llvm::Intrinsic::x86_mmx_packssdw:
  1943. return llvm::Intrinsic::x86_mmx_packssdw;
  1944. default:
  1945. llvm_unreachable("unexpected intrinsic id");
  1946. }
  1947. }
  1948. // \brief Instrument vector pack instrinsic.
  1949. //
  1950. // This function instruments intrinsics like x86_mmx_packsswb, that
  1951. // packs elements of 2 input vectors into half as many bits with saturation.
  1952. // Shadow is propagated with the signed variant of the same intrinsic applied
  1953. // to sext(Sa != zeroinitializer), sext(Sb != zeroinitializer).
  1954. // EltSizeInBits is used only for x86mmx arguments.
  1955. void handleVectorPackIntrinsic(IntrinsicInst &I, unsigned EltSizeInBits = 0) {
  1956. assert(I.getNumArgOperands() == 2);
  1957. bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
  1958. IRBuilder<> IRB(&I);
  1959. Value *S1 = getShadow(&I, 0);
  1960. Value *S2 = getShadow(&I, 1);
  1961. assert(isX86_MMX || S1->getType()->isVectorTy());
  1962. // SExt and ICmpNE below must apply to individual elements of input vectors.
  1963. // In case of x86mmx arguments, cast them to appropriate vector types and
  1964. // back.
  1965. Type *T = isX86_MMX ? getMMXVectorTy(EltSizeInBits) : S1->getType();
  1966. if (isX86_MMX) {
  1967. S1 = IRB.CreateBitCast(S1, T);
  1968. S2 = IRB.CreateBitCast(S2, T);
  1969. }
  1970. Value *S1_ext = IRB.CreateSExt(
  1971. IRB.CreateICmpNE(S1, llvm::Constant::getNullValue(T)), T);
  1972. Value *S2_ext = IRB.CreateSExt(
  1973. IRB.CreateICmpNE(S2, llvm::Constant::getNullValue(T)), T);
  1974. if (isX86_MMX) {
  1975. Type *X86_MMXTy = Type::getX86_MMXTy(*MS.C);
  1976. S1_ext = IRB.CreateBitCast(S1_ext, X86_MMXTy);
  1977. S2_ext = IRB.CreateBitCast(S2_ext, X86_MMXTy);
  1978. }
  1979. Function *ShadowFn = Intrinsic::getDeclaration(
  1980. F.getParent(), getSignedPackIntrinsic(I.getIntrinsicID()));
  1981. Value *S =
  1982. IRB.CreateCall(ShadowFn, {S1_ext, S2_ext}, "_msprop_vector_pack");
  1983. if (isX86_MMX) S = IRB.CreateBitCast(S, getShadowTy(&I));
  1984. setShadow(&I, S);
  1985. setOriginForNaryOp(I);
  1986. }
  1987. // \brief Instrument sum-of-absolute-differencies intrinsic.
  1988. void handleVectorSadIntrinsic(IntrinsicInst &I) {
  1989. const unsigned SignificantBitsPerResultElement = 16;
  1990. bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
  1991. Type *ResTy = isX86_MMX ? IntegerType::get(*MS.C, 64) : I.getType();
  1992. unsigned ZeroBitsPerResultElement =
  1993. ResTy->getScalarSizeInBits() - SignificantBitsPerResultElement;
  1994. IRBuilder<> IRB(&I);
  1995. Value *S = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1));
  1996. S = IRB.CreateBitCast(S, ResTy);
  1997. S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)),
  1998. ResTy);
  1999. S = IRB.CreateLShr(S, ZeroBitsPerResultElement);
  2000. S = IRB.CreateBitCast(S, getShadowTy(&I));
  2001. setShadow(&I, S);
  2002. setOriginForNaryOp(I);
  2003. }
  2004. // \brief Instrument multiply-add intrinsic.
  2005. void handleVectorPmaddIntrinsic(IntrinsicInst &I,
  2006. unsigned EltSizeInBits = 0) {
  2007. bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
  2008. Type *ResTy = isX86_MMX ? getMMXVectorTy(EltSizeInBits * 2) : I.getType();
  2009. IRBuilder<> IRB(&I);
  2010. Value *S = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1));
  2011. S = IRB.CreateBitCast(S, ResTy);
  2012. S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)),
  2013. ResTy);
  2014. S = IRB.CreateBitCast(S, getShadowTy(&I));
  2015. setShadow(&I, S);
  2016. setOriginForNaryOp(I);
  2017. }
  2018. void visitIntrinsicInst(IntrinsicInst &I) {
  2019. switch (I.getIntrinsicID()) {
  2020. case llvm::Intrinsic::bswap:
  2021. handleBswap(I);
  2022. break;
  2023. case llvm::Intrinsic::x86_avx512_cvtsd2usi64:
  2024. case llvm::Intrinsic::x86_avx512_cvtsd2usi:
  2025. case llvm::Intrinsic::x86_avx512_cvtss2usi64:
  2026. case llvm::Intrinsic::x86_avx512_cvtss2usi:
  2027. case llvm::Intrinsic::x86_avx512_cvttss2usi64:
  2028. case llvm::Intrinsic::x86_avx512_cvttss2usi:
  2029. case llvm::Intrinsic::x86_avx512_cvttsd2usi64:
  2030. case llvm::Intrinsic::x86_avx512_cvttsd2usi:
  2031. case llvm::Intrinsic::x86_avx512_cvtusi2sd:
  2032. case llvm::Intrinsic::x86_avx512_cvtusi2ss:
  2033. case llvm::Intrinsic::x86_avx512_cvtusi642sd:
  2034. case llvm::Intrinsic::x86_avx512_cvtusi642ss:
  2035. case llvm::Intrinsic::x86_sse2_cvtsd2si64:
  2036. case llvm::Intrinsic::x86_sse2_cvtsd2si:
  2037. case llvm::Intrinsic::x86_sse2_cvtsd2ss:
  2038. case llvm::Intrinsic::x86_sse2_cvtsi2sd:
  2039. case llvm::Intrinsic::x86_sse2_cvtsi642sd:
  2040. case llvm::Intrinsic::x86_sse2_cvtss2sd:
  2041. case llvm::Intrinsic::x86_sse2_cvttsd2si64:
  2042. case llvm::Intrinsic::x86_sse2_cvttsd2si:
  2043. case llvm::Intrinsic::x86_sse_cvtsi2ss:
  2044. case llvm::Intrinsic::x86_sse_cvtsi642ss:
  2045. case llvm::Intrinsic::x86_sse_cvtss2si64:
  2046. case llvm::Intrinsic::x86_sse_cvtss2si:
  2047. case llvm::Intrinsic::x86_sse_cvttss2si64:
  2048. case llvm::Intrinsic::x86_sse_cvttss2si:
  2049. handleVectorConvertIntrinsic(I, 1);
  2050. break;
  2051. case llvm::Intrinsic::x86_sse2_cvtdq2pd:
  2052. case llvm::Intrinsic::x86_sse2_cvtps2pd:
  2053. case llvm::Intrinsic::x86_sse_cvtps2pi:
  2054. case llvm::Intrinsic::x86_sse_cvttps2pi:
  2055. handleVectorConvertIntrinsic(I, 2);
  2056. break;
  2057. case llvm::Intrinsic::x86_avx2_psll_w:
  2058. case llvm::Intrinsic::x86_avx2_psll_d:
  2059. case llvm::Intrinsic::x86_avx2_psll_q:
  2060. case llvm::Intrinsic::x86_avx2_pslli_w:
  2061. case llvm::Intrinsic::x86_avx2_pslli_d:
  2062. case llvm::Intrinsic::x86_avx2_pslli_q:
  2063. case llvm::Intrinsic::x86_avx2_psrl_w:
  2064. case llvm::Intrinsic::x86_avx2_psrl_d:
  2065. case llvm::Intrinsic::x86_avx2_psrl_q:
  2066. case llvm::Intrinsic::x86_avx2_psra_w:
  2067. case llvm::Intrinsic::x86_avx2_psra_d:
  2068. case llvm::Intrinsic::x86_avx2_psrli_w:
  2069. case llvm::Intrinsic::x86_avx2_psrli_d:
  2070. case llvm::Intrinsic::x86_avx2_psrli_q:
  2071. case llvm::Intrinsic::x86_avx2_psrai_w:
  2072. case llvm::Intrinsic::x86_avx2_psrai_d:
  2073. case llvm::Intrinsic::x86_sse2_psll_w:
  2074. case llvm::Intrinsic::x86_sse2_psll_d:
  2075. case llvm::Intrinsic::x86_sse2_psll_q:
  2076. case llvm::Intrinsic::x86_sse2_pslli_w:
  2077. case llvm::Intrinsic::x86_sse2_pslli_d:
  2078. case llvm::Intrinsic::x86_sse2_pslli_q:
  2079. case llvm::Intrinsic::x86_sse2_psrl_w:
  2080. case llvm::Intrinsic::x86_sse2_psrl_d:
  2081. case llvm::Intrinsic::x86_sse2_psrl_q:
  2082. case llvm::Intrinsic::x86_sse2_psra_w:
  2083. case llvm::Intrinsic::x86_sse2_psra_d:
  2084. case llvm::Intrinsic::x86_sse2_psrli_w:
  2085. case llvm::Intrinsic::x86_sse2_psrli_d:
  2086. case llvm::Intrinsic::x86_sse2_psrli_q:
  2087. case llvm::Intrinsic::x86_sse2_psrai_w:
  2088. case llvm::Intrinsic::x86_sse2_psrai_d:
  2089. case llvm::Intrinsic::x86_mmx_psll_w:
  2090. case llvm::Intrinsic::x86_mmx_psll_d:
  2091. case llvm::Intrinsic::x86_mmx_psll_q:
  2092. case llvm::Intrinsic::x86_mmx_pslli_w:
  2093. case llvm::Intrinsic::x86_mmx_pslli_d:
  2094. case llvm::Intrinsic::x86_mmx_pslli_q:
  2095. case llvm::Intrinsic::x86_mmx_psrl_w:
  2096. case llvm::Intrinsic::x86_mmx_psrl_d:
  2097. case llvm::Intrinsic::x86_mmx_psrl_q:
  2098. case llvm::Intrinsic::x86_mmx_psra_w:
  2099. case llvm::Intrinsic::x86_mmx_psra_d:
  2100. case llvm::Intrinsic::x86_mmx_psrli_w:
  2101. case llvm::Intrinsic::x86_mmx_psrli_d:
  2102. case llvm::Intrinsic::x86_mmx_psrli_q:
  2103. case llvm::Intrinsic::x86_mmx_psrai_w:
  2104. case llvm::Intrinsic::x86_mmx_psrai_d:
  2105. handleVectorShiftIntrinsic(I, /* Variable */ false);
  2106. break;
  2107. case llvm::Intrinsic::x86_avx2_psllv_d:
  2108. case llvm::Intrinsic::x86_avx2_psllv_d_256:
  2109. case llvm::Intrinsic::x86_avx2_psllv_q:
  2110. case llvm::Intrinsic::x86_avx2_psllv_q_256:
  2111. case llvm::Intrinsic::x86_avx2_psrlv_d:
  2112. case llvm::Intrinsic::x86_avx2_psrlv_d_256:
  2113. case llvm::Intrinsic::x86_avx2_psrlv_q:
  2114. case llvm::Intrinsic::x86_avx2_psrlv_q_256:
  2115. case llvm::Intrinsic::x86_avx2_psrav_d:
  2116. case llvm::Intrinsic::x86_avx2_psrav_d_256:
  2117. handleVectorShiftIntrinsic(I, /* Variable */ true);
  2118. break;
  2119. case llvm::Intrinsic::x86_sse2_packsswb_128:
  2120. case llvm::Intrinsic::x86_sse2_packssdw_128:
  2121. case llvm::Intrinsic::x86_sse2_packuswb_128:
  2122. case llvm::Intrinsic::x86_sse41_packusdw:
  2123. case llvm::Intrinsic::x86_avx2_packsswb:
  2124. case llvm::Intrinsic::x86_avx2_packssdw:
  2125. case llvm::Intrinsic::x86_avx2_packuswb:
  2126. case llvm::Intrinsic::x86_avx2_packusdw:
  2127. handleVectorPackIntrinsic(I);
  2128. break;
  2129. case llvm::Intrinsic::x86_mmx_packsswb:
  2130. case llvm::Intrinsic::x86_mmx_packuswb:
  2131. handleVectorPackIntrinsic(I, 16);
  2132. break;
  2133. case llvm::Intrinsic::x86_mmx_packssdw:
  2134. handleVectorPackIntrinsic(I, 32);
  2135. break;
  2136. case llvm::Intrinsic::x86_mmx_psad_bw:
  2137. case llvm::Intrinsic::x86_sse2_psad_bw:
  2138. case llvm::Intrinsic::x86_avx2_psad_bw:
  2139. handleVectorSadIntrinsic(I);
  2140. break;
  2141. case llvm::Intrinsic::x86_sse2_pmadd_wd:
  2142. case llvm::Intrinsic::x86_avx2_pmadd_wd:
  2143. case llvm::Intrinsic::x86_ssse3_pmadd_ub_sw_128:
  2144. case llvm::Intrinsic::x86_avx2_pmadd_ub_sw:
  2145. handleVectorPmaddIntrinsic(I);
  2146. break;
  2147. case llvm::Intrinsic::x86_ssse3_pmadd_ub_sw:
  2148. handleVectorPmaddIntrinsic(I, 8);
  2149. break;
  2150. case llvm::Intrinsic::x86_mmx_pmadd_wd:
  2151. handleVectorPmaddIntrinsic(I, 16);
  2152. break;
  2153. default:
  2154. if (!handleUnknownIntrinsic(I))
  2155. visitInstruction(I);
  2156. break;
  2157. }
  2158. }
  2159. void visitCallSite(CallSite CS) {
  2160. Instruction &I = *CS.getInstruction();
  2161. assert((CS.isCall() || CS.isInvoke()) && "Unknown type of CallSite");
  2162. if (CS.isCall()) {
  2163. CallInst *Call = cast<CallInst>(&I);
  2164. // For inline asm, do the usual thing: check argument shadow and mark all
  2165. // outputs as clean. Note that any side effects of the inline asm that are
  2166. // not immediately visible in its constraints are not handled.
  2167. if (Call->isInlineAsm()) {
  2168. visitInstruction(I);
  2169. return;
  2170. }
  2171. assert(!isa<IntrinsicInst>(&I) && "intrinsics are handled elsewhere");
  2172. // We are going to insert code that relies on the fact that the callee
  2173. // will become a non-readonly function after it is instrumented by us. To
  2174. // prevent this code from being optimized out, mark that function
  2175. // non-readonly in advance.
  2176. if (Function *Func = Call->getCalledFunction()) {
  2177. // Clear out readonly/readnone attributes.
  2178. AttrBuilder B;
  2179. B.addAttribute(Attribute::ReadOnly)
  2180. .addAttribute(Attribute::ReadNone);
  2181. Func->removeAttributes(AttributeSet::FunctionIndex,
  2182. AttributeSet::get(Func->getContext(),
  2183. AttributeSet::FunctionIndex,
  2184. B));
  2185. }
  2186. }
  2187. IRBuilder<> IRB(&I);
  2188. unsigned ArgOffset = 0;
  2189. DEBUG(dbgs() << " CallSite: " << I << "\n");
  2190. for (CallSite::arg_iterator ArgIt = CS.arg_begin(), End = CS.arg_end();
  2191. ArgIt != End; ++ArgIt) {
  2192. Value *A = *ArgIt;
  2193. unsigned i = ArgIt - CS.arg_begin();
  2194. if (!A->getType()->isSized()) {
  2195. DEBUG(dbgs() << "Arg " << i << " is not sized: " << I << "\n");
  2196. continue;
  2197. }
  2198. unsigned Size = 0;
  2199. Value *Store = nullptr;
  2200. // Compute the Shadow for arg even if it is ByVal, because
  2201. // in that case getShadow() will copy the actual arg shadow to
  2202. // __msan_param_tls.
  2203. Value *ArgShadow = getShadow(A);
  2204. Value *ArgShadowBase = getShadowPtrForArgument(A, IRB, ArgOffset);
  2205. DEBUG(dbgs() << " Arg#" << i << ": " << *A <<
  2206. " Shadow: " << *ArgShadow << "\n");
  2207. bool ArgIsInitialized = false;
  2208. const DataLayout &DL = F.getParent()->getDataLayout();
  2209. if (CS.paramHasAttr(i + 1, Attribute::ByVal)) {
  2210. assert(A->getType()->isPointerTy() &&
  2211. "ByVal argument is not a pointer!");
  2212. Size = DL.getTypeAllocSize(A->getType()->getPointerElementType());
  2213. if (ArgOffset + Size > kParamTLSSize) break;
  2214. unsigned ParamAlignment = CS.getParamAlignment(i + 1);
  2215. unsigned Alignment = std::min(ParamAlignment, kShadowTLSAlignment);
  2216. Store = IRB.CreateMemCpy(ArgShadowBase,
  2217. getShadowPtr(A, Type::getInt8Ty(*MS.C), IRB),
  2218. Size, Alignment);
  2219. } else {
  2220. Size = DL.getTypeAllocSize(A->getType());
  2221. if (ArgOffset + Size > kParamTLSSize) break;
  2222. Store = IRB.CreateAlignedStore(ArgShadow, ArgShadowBase,
  2223. kShadowTLSAlignment);
  2224. Constant *Cst = dyn_cast<Constant>(ArgShadow);
  2225. if (Cst && Cst->isNullValue()) ArgIsInitialized = true;
  2226. }
  2227. if (MS.TrackOrigins && !ArgIsInitialized)
  2228. IRB.CreateStore(getOrigin(A),
  2229. getOriginPtrForArgument(A, IRB, ArgOffset));
  2230. (void)Store;
  2231. assert(Size != 0 && Store != nullptr);
  2232. DEBUG(dbgs() << " Param:" << *Store << "\n");
  2233. ArgOffset += RoundUpToAlignment(Size, 8);
  2234. }
  2235. DEBUG(dbgs() << " done with call args\n");
  2236. FunctionType *FT =
  2237. cast<FunctionType>(CS.getCalledValue()->getType()->getContainedType(0));
  2238. if (FT->isVarArg()) {
  2239. VAHelper->visitCallSite(CS, IRB);
  2240. }
  2241. // Now, get the shadow for the RetVal.
  2242. if (!I.getType()->isSized()) return;
  2243. IRBuilder<> IRBBefore(&I);
  2244. // Until we have full dynamic coverage, make sure the retval shadow is 0.
  2245. Value *Base = getShadowPtrForRetval(&I, IRBBefore);
  2246. IRBBefore.CreateAlignedStore(getCleanShadow(&I), Base, kShadowTLSAlignment);
  2247. Instruction *NextInsn = nullptr;
  2248. if (CS.isCall()) {
  2249. NextInsn = I.getNextNode();
  2250. } else {
  2251. BasicBlock *NormalDest = cast<InvokeInst>(&I)->getNormalDest();
  2252. if (!NormalDest->getSinglePredecessor()) {
  2253. // FIXME: this case is tricky, so we are just conservative here.
  2254. // Perhaps we need to split the edge between this BB and NormalDest,
  2255. // but a naive attempt to use SplitEdge leads to a crash.
  2256. setShadow(&I, getCleanShadow(&I));
  2257. setOrigin(&I, getCleanOrigin());
  2258. return;
  2259. }
  2260. NextInsn = NormalDest->getFirstInsertionPt();
  2261. assert(NextInsn &&
  2262. "Could not find insertion point for retval shadow load");
  2263. }
  2264. IRBuilder<> IRBAfter(NextInsn);
  2265. Value *RetvalShadow =
  2266. IRBAfter.CreateAlignedLoad(getShadowPtrForRetval(&I, IRBAfter),
  2267. kShadowTLSAlignment, "_msret");
  2268. setShadow(&I, RetvalShadow);
  2269. if (MS.TrackOrigins)
  2270. setOrigin(&I, IRBAfter.CreateLoad(getOriginPtrForRetval(IRBAfter)));
  2271. }
  2272. void visitReturnInst(ReturnInst &I) {
  2273. IRBuilder<> IRB(&I);
  2274. Value *RetVal = I.getReturnValue();
  2275. if (!RetVal) return;
  2276. Value *ShadowPtr = getShadowPtrForRetval(RetVal, IRB);
  2277. if (CheckReturnValue) {
  2278. insertShadowCheck(RetVal, &I);
  2279. Value *Shadow = getCleanShadow(RetVal);
  2280. IRB.CreateAlignedStore(Shadow, ShadowPtr, kShadowTLSAlignment);
  2281. } else {
  2282. Value *Shadow = getShadow(RetVal);
  2283. IRB.CreateAlignedStore(Shadow, ShadowPtr, kShadowTLSAlignment);
  2284. // FIXME: make it conditional if ClStoreCleanOrigin==0
  2285. if (MS.TrackOrigins)
  2286. IRB.CreateStore(getOrigin(RetVal), getOriginPtrForRetval(IRB));
  2287. }
  2288. }
  2289. void visitPHINode(PHINode &I) {
  2290. IRBuilder<> IRB(&I);
  2291. if (!PropagateShadow) {
  2292. setShadow(&I, getCleanShadow(&I));
  2293. setOrigin(&I, getCleanOrigin());
  2294. return;
  2295. }
  2296. ShadowPHINodes.push_back(&I);
  2297. setShadow(&I, IRB.CreatePHI(getShadowTy(&I), I.getNumIncomingValues(),
  2298. "_msphi_s"));
  2299. if (MS.TrackOrigins)
  2300. setOrigin(&I, IRB.CreatePHI(MS.OriginTy, I.getNumIncomingValues(),
  2301. "_msphi_o"));
  2302. }
  2303. void visitAllocaInst(AllocaInst &I) {
  2304. setShadow(&I, getCleanShadow(&I));
  2305. setOrigin(&I, getCleanOrigin());
  2306. IRBuilder<> IRB(I.getNextNode());
  2307. const DataLayout &DL = F.getParent()->getDataLayout();
  2308. uint64_t Size = DL.getTypeAllocSize(I.getAllocatedType());
  2309. if (PoisonStack && ClPoisonStackWithCall) {
  2310. IRB.CreateCall(MS.MsanPoisonStackFn,
  2311. {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()),
  2312. ConstantInt::get(MS.IntptrTy, Size)});
  2313. } else {
  2314. Value *ShadowBase = getShadowPtr(&I, Type::getInt8PtrTy(*MS.C), IRB);
  2315. Value *PoisonValue = IRB.getInt8(PoisonStack ? ClPoisonStackPattern : 0);
  2316. IRB.CreateMemSet(ShadowBase, PoisonValue, Size, I.getAlignment());
  2317. }
  2318. if (PoisonStack && MS.TrackOrigins) {
  2319. SmallString<2048> StackDescriptionStorage;
  2320. raw_svector_ostream StackDescription(StackDescriptionStorage);
  2321. // We create a string with a description of the stack allocation and
  2322. // pass it into __msan_set_alloca_origin.
  2323. // It will be printed by the run-time if stack-originated UMR is found.
  2324. // The first 4 bytes of the string are set to '----' and will be replaced
  2325. // by __msan_va_arg_overflow_size_tls at the first call.
  2326. StackDescription << "----" << I.getName() << "@" << F.getName();
  2327. Value *Descr =
  2328. createPrivateNonConstGlobalForString(*F.getParent(),
  2329. StackDescription.str());
  2330. IRB.CreateCall(MS.MsanSetAllocaOrigin4Fn,
  2331. {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()),
  2332. ConstantInt::get(MS.IntptrTy, Size),
  2333. IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy()),
  2334. IRB.CreatePointerCast(&F, MS.IntptrTy)});
  2335. }
  2336. }
  2337. void visitSelectInst(SelectInst& I) {
  2338. IRBuilder<> IRB(&I);
  2339. // a = select b, c, d
  2340. Value *B = I.getCondition();
  2341. Value *C = I.getTrueValue();
  2342. Value *D = I.getFalseValue();
  2343. Value *Sb = getShadow(B);
  2344. Value *Sc = getShadow(C);
  2345. Value *Sd = getShadow(D);
  2346. // Result shadow if condition shadow is 0.
  2347. Value *Sa0 = IRB.CreateSelect(B, Sc, Sd);
  2348. Value *Sa1;
  2349. if (I.getType()->isAggregateType()) {
  2350. // To avoid "sign extending" i1 to an arbitrary aggregate type, we just do
  2351. // an extra "select". This results in much more compact IR.
  2352. // Sa = select Sb, poisoned, (select b, Sc, Sd)
  2353. Sa1 = getPoisonedShadow(getShadowTy(I.getType()));
  2354. } else {
  2355. // Sa = select Sb, [ (c^d) | Sc | Sd ], [ b ? Sc : Sd ]
  2356. // If Sb (condition is poisoned), look for bits in c and d that are equal
  2357. // and both unpoisoned.
  2358. // If !Sb (condition is unpoisoned), simply pick one of Sc and Sd.
  2359. // Cast arguments to shadow-compatible type.
  2360. C = CreateAppToShadowCast(IRB, C);
  2361. D = CreateAppToShadowCast(IRB, D);
  2362. // Result shadow if condition shadow is 1.
  2363. Sa1 = IRB.CreateOr(IRB.CreateXor(C, D), IRB.CreateOr(Sc, Sd));
  2364. }
  2365. Value *Sa = IRB.CreateSelect(Sb, Sa1, Sa0, "_msprop_select");
  2366. setShadow(&I, Sa);
  2367. if (MS.TrackOrigins) {
  2368. // Origins are always i32, so any vector conditions must be flattened.
  2369. // FIXME: consider tracking vector origins for app vectors?
  2370. if (B->getType()->isVectorTy()) {
  2371. Type *FlatTy = getShadowTyNoVec(B->getType());
  2372. B = IRB.CreateICmpNE(IRB.CreateBitCast(B, FlatTy),
  2373. ConstantInt::getNullValue(FlatTy));
  2374. Sb = IRB.CreateICmpNE(IRB.CreateBitCast(Sb, FlatTy),
  2375. ConstantInt::getNullValue(FlatTy));
  2376. }
  2377. // a = select b, c, d
  2378. // Oa = Sb ? Ob : (b ? Oc : Od)
  2379. setOrigin(
  2380. &I, IRB.CreateSelect(Sb, getOrigin(I.getCondition()),
  2381. IRB.CreateSelect(B, getOrigin(I.getTrueValue()),
  2382. getOrigin(I.getFalseValue()))));
  2383. }
  2384. }
  2385. void visitLandingPadInst(LandingPadInst &I) {
  2386. // Do nothing.
  2387. // See http://code.google.com/p/memory-sanitizer/issues/detail?id=1
  2388. setShadow(&I, getCleanShadow(&I));
  2389. setOrigin(&I, getCleanOrigin());
  2390. }
  2391. void visitGetElementPtrInst(GetElementPtrInst &I) {
  2392. handleShadowOr(I);
  2393. }
  2394. void visitExtractValueInst(ExtractValueInst &I) {
  2395. IRBuilder<> IRB(&I);
  2396. Value *Agg = I.getAggregateOperand();
  2397. DEBUG(dbgs() << "ExtractValue: " << I << "\n");
  2398. Value *AggShadow = getShadow(Agg);
  2399. DEBUG(dbgs() << " AggShadow: " << *AggShadow << "\n");
  2400. Value *ResShadow = IRB.CreateExtractValue(AggShadow, I.getIndices());
  2401. DEBUG(dbgs() << " ResShadow: " << *ResShadow << "\n");
  2402. setShadow(&I, ResShadow);
  2403. setOriginForNaryOp(I);
  2404. }
  2405. void visitInsertValueInst(InsertValueInst &I) {
  2406. IRBuilder<> IRB(&I);
  2407. DEBUG(dbgs() << "InsertValue: " << I << "\n");
  2408. Value *AggShadow = getShadow(I.getAggregateOperand());
  2409. Value *InsShadow = getShadow(I.getInsertedValueOperand());
  2410. DEBUG(dbgs() << " AggShadow: " << *AggShadow << "\n");
  2411. DEBUG(dbgs() << " InsShadow: " << *InsShadow << "\n");
  2412. Value *Res = IRB.CreateInsertValue(AggShadow, InsShadow, I.getIndices());
  2413. DEBUG(dbgs() << " Res: " << *Res << "\n");
  2414. setShadow(&I, Res);
  2415. setOriginForNaryOp(I);
  2416. }
  2417. void dumpInst(Instruction &I) {
  2418. if (CallInst *CI = dyn_cast<CallInst>(&I)) {
  2419. errs() << "ZZZ call " << CI->getCalledFunction()->getName() << "\n";
  2420. } else {
  2421. errs() << "ZZZ " << I.getOpcodeName() << "\n";
  2422. }
  2423. errs() << "QQQ " << I << "\n";
  2424. }
  2425. void visitResumeInst(ResumeInst &I) {
  2426. DEBUG(dbgs() << "Resume: " << I << "\n");
  2427. // Nothing to do here.
  2428. }
  2429. void visitInstruction(Instruction &I) {
  2430. // Everything else: stop propagating and check for poisoned shadow.
  2431. if (ClDumpStrictInstructions)
  2432. dumpInst(I);
  2433. DEBUG(dbgs() << "DEFAULT: " << I << "\n");
  2434. for (size_t i = 0, n = I.getNumOperands(); i < n; i++)
  2435. insertShadowCheck(I.getOperand(i), &I);
  2436. setShadow(&I, getCleanShadow(&I));
  2437. setOrigin(&I, getCleanOrigin());
  2438. }
  2439. };
  2440. /// \brief AMD64-specific implementation of VarArgHelper.
  2441. struct VarArgAMD64Helper : public VarArgHelper {
  2442. // An unfortunate workaround for asymmetric lowering of va_arg stuff.
  2443. // See a comment in visitCallSite for more details.
  2444. static const unsigned AMD64GpEndOffset = 48; // AMD64 ABI Draft 0.99.6 p3.5.7
  2445. static const unsigned AMD64FpEndOffset = 176;
  2446. Function &F;
  2447. MemorySanitizer &MS;
  2448. MemorySanitizerVisitor &MSV;
  2449. Value *VAArgTLSCopy;
  2450. Value *VAArgOverflowSize;
  2451. SmallVector<CallInst*, 16> VAStartInstrumentationList;
  2452. VarArgAMD64Helper(Function &F, MemorySanitizer &MS,
  2453. MemorySanitizerVisitor &MSV)
  2454. : F(F), MS(MS), MSV(MSV), VAArgTLSCopy(nullptr),
  2455. VAArgOverflowSize(nullptr) {}
  2456. enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
  2457. ArgKind classifyArgument(Value* arg) {
  2458. // A very rough approximation of X86_64 argument classification rules.
  2459. Type *T = arg->getType();
  2460. if (T->isFPOrFPVectorTy() || T->isX86_MMXTy())
  2461. return AK_FloatingPoint;
  2462. if (T->isIntegerTy() && T->getPrimitiveSizeInBits() <= 64)
  2463. return AK_GeneralPurpose;
  2464. if (T->isPointerTy())
  2465. return AK_GeneralPurpose;
  2466. return AK_Memory;
  2467. }
  2468. // For VarArg functions, store the argument shadow in an ABI-specific format
  2469. // that corresponds to va_list layout.
  2470. // We do this because Clang lowers va_arg in the frontend, and this pass
  2471. // only sees the low level code that deals with va_list internals.
  2472. // A much easier alternative (provided that Clang emits va_arg instructions)
  2473. // would have been to associate each live instance of va_list with a copy of
  2474. // MSanParamTLS, and extract shadow on va_arg() call in the argument list
  2475. // order.
  2476. void visitCallSite(CallSite &CS, IRBuilder<> &IRB) override {
  2477. unsigned GpOffset = 0;
  2478. unsigned FpOffset = AMD64GpEndOffset;
  2479. unsigned OverflowOffset = AMD64FpEndOffset;
  2480. const DataLayout &DL = F.getParent()->getDataLayout();
  2481. for (CallSite::arg_iterator ArgIt = CS.arg_begin(), End = CS.arg_end();
  2482. ArgIt != End; ++ArgIt) {
  2483. Value *A = *ArgIt;
  2484. unsigned ArgNo = CS.getArgumentNo(ArgIt);
  2485. bool IsByVal = CS.paramHasAttr(ArgNo + 1, Attribute::ByVal);
  2486. if (IsByVal) {
  2487. // ByVal arguments always go to the overflow area.
  2488. assert(A->getType()->isPointerTy());
  2489. Type *RealTy = A->getType()->getPointerElementType();
  2490. uint64_t ArgSize = DL.getTypeAllocSize(RealTy);
  2491. Value *Base = getShadowPtrForVAArgument(RealTy, IRB, OverflowOffset);
  2492. OverflowOffset += RoundUpToAlignment(ArgSize, 8);
  2493. IRB.CreateMemCpy(Base, MSV.getShadowPtr(A, IRB.getInt8Ty(), IRB),
  2494. ArgSize, kShadowTLSAlignment);
  2495. } else {
  2496. ArgKind AK = classifyArgument(A);
  2497. if (AK == AK_GeneralPurpose && GpOffset >= AMD64GpEndOffset)
  2498. AK = AK_Memory;
  2499. if (AK == AK_FloatingPoint && FpOffset >= AMD64FpEndOffset)
  2500. AK = AK_Memory;
  2501. Value *Base;
  2502. switch (AK) {
  2503. case AK_GeneralPurpose:
  2504. Base = getShadowPtrForVAArgument(A->getType(), IRB, GpOffset);
  2505. GpOffset += 8;
  2506. break;
  2507. case AK_FloatingPoint:
  2508. Base = getShadowPtrForVAArgument(A->getType(), IRB, FpOffset);
  2509. FpOffset += 16;
  2510. break;
  2511. default: assert(AK == AK_Memory); // HLSL Change - set as default case rather than case AK_Memory
  2512. uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
  2513. Base = getShadowPtrForVAArgument(A->getType(), IRB, OverflowOffset);
  2514. OverflowOffset += RoundUpToAlignment(ArgSize, 8);
  2515. }
  2516. IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
  2517. }
  2518. }
  2519. Constant *OverflowSize =
  2520. ConstantInt::get(IRB.getInt64Ty(), OverflowOffset - AMD64FpEndOffset);
  2521. IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
  2522. }
  2523. /// \brief Compute the shadow address for a given va_arg.
  2524. Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
  2525. int ArgOffset) {
  2526. Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
  2527. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  2528. return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
  2529. "_msarg");
  2530. }
  2531. void visitVAStartInst(VAStartInst &I) override {
  2532. IRBuilder<> IRB(&I);
  2533. VAStartInstrumentationList.push_back(&I);
  2534. Value *VAListTag = I.getArgOperand(0);
  2535. Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
  2536. // Unpoison the whole __va_list_tag.
  2537. // FIXME: magic ABI constants.
  2538. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  2539. /* size */24, /* alignment */8, false);
  2540. }
  2541. void visitVACopyInst(VACopyInst &I) override {
  2542. IRBuilder<> IRB(&I);
  2543. Value *VAListTag = I.getArgOperand(0);
  2544. Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
  2545. // Unpoison the whole __va_list_tag.
  2546. // FIXME: magic ABI constants.
  2547. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  2548. /* size */24, /* alignment */8, false);
  2549. }
  2550. void finalizeInstrumentation() override {
  2551. assert(!VAArgOverflowSize && !VAArgTLSCopy &&
  2552. "finalizeInstrumentation called twice");
  2553. if (!VAStartInstrumentationList.empty()) {
  2554. // If there is a va_start in this function, make a backup copy of
  2555. // va_arg_tls somewhere in the function entry block.
  2556. IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
  2557. VAArgOverflowSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS);
  2558. Value *CopySize =
  2559. IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AMD64FpEndOffset),
  2560. VAArgOverflowSize);
  2561. VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
  2562. IRB.CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8);
  2563. }
  2564. // Instrument va_start.
  2565. // Copy va_list shadow from the backup copy of the TLS contents.
  2566. for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
  2567. CallInst *OrigInst = VAStartInstrumentationList[i];
  2568. IRBuilder<> IRB(OrigInst->getNextNode());
  2569. Value *VAListTag = OrigInst->getArgOperand(0);
  2570. Value *RegSaveAreaPtrPtr =
  2571. IRB.CreateIntToPtr(
  2572. IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  2573. ConstantInt::get(MS.IntptrTy, 16)),
  2574. Type::getInt64PtrTy(*MS.C));
  2575. Value *RegSaveAreaPtr = IRB.CreateLoad(RegSaveAreaPtrPtr);
  2576. Value *RegSaveAreaShadowPtr =
  2577. MSV.getShadowPtr(RegSaveAreaPtr, IRB.getInt8Ty(), IRB);
  2578. IRB.CreateMemCpy(RegSaveAreaShadowPtr, VAArgTLSCopy,
  2579. AMD64FpEndOffset, 16);
  2580. Value *OverflowArgAreaPtrPtr =
  2581. IRB.CreateIntToPtr(
  2582. IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  2583. ConstantInt::get(MS.IntptrTy, 8)),
  2584. Type::getInt64PtrTy(*MS.C));
  2585. Value *OverflowArgAreaPtr = IRB.CreateLoad(OverflowArgAreaPtrPtr);
  2586. Value *OverflowArgAreaShadowPtr =
  2587. MSV.getShadowPtr(OverflowArgAreaPtr, IRB.getInt8Ty(), IRB);
  2588. Value *SrcPtr = IRB.CreateConstGEP1_32(IRB.getInt8Ty(), VAArgTLSCopy,
  2589. AMD64FpEndOffset);
  2590. IRB.CreateMemCpy(OverflowArgAreaShadowPtr, SrcPtr, VAArgOverflowSize, 16);
  2591. }
  2592. }
  2593. };
  2594. /// \brief MIPS64-specific implementation of VarArgHelper.
  2595. struct VarArgMIPS64Helper : public VarArgHelper {
  2596. Function &F;
  2597. MemorySanitizer &MS;
  2598. MemorySanitizerVisitor &MSV;
  2599. Value *VAArgTLSCopy;
  2600. Value *VAArgSize;
  2601. SmallVector<CallInst*, 16> VAStartInstrumentationList;
  2602. VarArgMIPS64Helper(Function &F, MemorySanitizer &MS,
  2603. MemorySanitizerVisitor &MSV)
  2604. : F(F), MS(MS), MSV(MSV), VAArgTLSCopy(nullptr),
  2605. VAArgSize(nullptr) {}
  2606. void visitCallSite(CallSite &CS, IRBuilder<> &IRB) override {
  2607. unsigned VAArgOffset = 0;
  2608. const DataLayout &DL = F.getParent()->getDataLayout();
  2609. for (CallSite::arg_iterator ArgIt = CS.arg_begin() + 1, End = CS.arg_end();
  2610. ArgIt != End; ++ArgIt) {
  2611. Value *A = *ArgIt;
  2612. Value *Base;
  2613. uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
  2614. #if defined(__MIPSEB__) || defined(MIPSEB)
  2615. // Adjusting the shadow for argument with size < 8 to match the placement
  2616. // of bits in big endian system
  2617. if (ArgSize < 8)
  2618. VAArgOffset += (8 - ArgSize);
  2619. #endif
  2620. Base = getShadowPtrForVAArgument(A->getType(), IRB, VAArgOffset);
  2621. VAArgOffset += ArgSize;
  2622. VAArgOffset = RoundUpToAlignment(VAArgOffset, 8);
  2623. IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
  2624. }
  2625. Constant *TotalVAArgSize = ConstantInt::get(IRB.getInt64Ty(), VAArgOffset);
  2626. // Here using VAArgOverflowSizeTLS as VAArgSizeTLS to avoid creation of
  2627. // a new class member i.e. it is the total size of all VarArgs.
  2628. IRB.CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
  2629. }
  2630. /// \brief Compute the shadow address for a given va_arg.
  2631. Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
  2632. int ArgOffset) {
  2633. Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
  2634. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  2635. return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
  2636. "_msarg");
  2637. }
  2638. void visitVAStartInst(VAStartInst &I) override {
  2639. IRBuilder<> IRB(&I);
  2640. VAStartInstrumentationList.push_back(&I);
  2641. Value *VAListTag = I.getArgOperand(0);
  2642. Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
  2643. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  2644. /* size */8, /* alignment */8, false);
  2645. }
  2646. void visitVACopyInst(VACopyInst &I) override {
  2647. IRBuilder<> IRB(&I);
  2648. Value *VAListTag = I.getArgOperand(0);
  2649. Value *ShadowPtr = MSV.getShadowPtr(VAListTag, IRB.getInt8Ty(), IRB);
  2650. // Unpoison the whole __va_list_tag.
  2651. // FIXME: magic ABI constants.
  2652. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  2653. /* size */8, /* alignment */8, false);
  2654. }
  2655. void finalizeInstrumentation() override {
  2656. assert(!VAArgSize && !VAArgTLSCopy &&
  2657. "finalizeInstrumentation called twice");
  2658. IRBuilder<> IRB(F.getEntryBlock().getFirstNonPHI());
  2659. VAArgSize = IRB.CreateLoad(MS.VAArgOverflowSizeTLS);
  2660. Value *CopySize = IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0),
  2661. VAArgSize);
  2662. if (!VAStartInstrumentationList.empty()) {
  2663. // If there is a va_start in this function, make a backup copy of
  2664. // va_arg_tls somewhere in the function entry block.
  2665. VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
  2666. IRB.CreateMemCpy(VAArgTLSCopy, MS.VAArgTLS, CopySize, 8);
  2667. }
  2668. // Instrument va_start.
  2669. // Copy va_list shadow from the backup copy of the TLS contents.
  2670. for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
  2671. CallInst *OrigInst = VAStartInstrumentationList[i];
  2672. IRBuilder<> IRB(OrigInst->getNextNode());
  2673. Value *VAListTag = OrigInst->getArgOperand(0);
  2674. Value *RegSaveAreaPtrPtr =
  2675. IRB.CreateIntToPtr(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  2676. Type::getInt64PtrTy(*MS.C));
  2677. Value *RegSaveAreaPtr = IRB.CreateLoad(RegSaveAreaPtrPtr);
  2678. Value *RegSaveAreaShadowPtr =
  2679. MSV.getShadowPtr(RegSaveAreaPtr, IRB.getInt8Ty(), IRB);
  2680. IRB.CreateMemCpy(RegSaveAreaShadowPtr, VAArgTLSCopy, CopySize, 8);
  2681. }
  2682. }
  2683. };
  2684. /// \brief A no-op implementation of VarArgHelper.
  2685. struct VarArgNoOpHelper : public VarArgHelper {
  2686. VarArgNoOpHelper(Function &F, MemorySanitizer &MS,
  2687. MemorySanitizerVisitor &MSV) {}
  2688. void visitCallSite(CallSite &CS, IRBuilder<> &IRB) override {}
  2689. void visitVAStartInst(VAStartInst &I) override {}
  2690. void visitVACopyInst(VACopyInst &I) override {}
  2691. void finalizeInstrumentation() override {}
  2692. };
  2693. VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
  2694. MemorySanitizerVisitor &Visitor) {
  2695. // VarArg handling is only implemented on AMD64. False positives are possible
  2696. // on other platforms.
  2697. llvm::Triple TargetTriple(Func.getParent()->getTargetTriple());
  2698. if (TargetTriple.getArch() == llvm::Triple::x86_64)
  2699. return new VarArgAMD64Helper(Func, Msan, Visitor);
  2700. else if (TargetTriple.getArch() == llvm::Triple::mips64 ||
  2701. TargetTriple.getArch() == llvm::Triple::mips64el)
  2702. return new VarArgMIPS64Helper(Func, Msan, Visitor);
  2703. else
  2704. return new VarArgNoOpHelper(Func, Msan, Visitor);
  2705. }
  2706. } // namespace
  2707. bool MemorySanitizer::runOnFunction(Function &F) {
  2708. if (&F == MsanCtorFunction)
  2709. return false;
  2710. MemorySanitizerVisitor Visitor(F, *this);
  2711. // Clear out readonly/readnone attributes.
  2712. AttrBuilder B;
  2713. B.addAttribute(Attribute::ReadOnly)
  2714. .addAttribute(Attribute::ReadNone);
  2715. F.removeAttributes(AttributeSet::FunctionIndex,
  2716. AttributeSet::get(F.getContext(),
  2717. AttributeSet::FunctionIndex, B));
  2718. return Visitor.runOnFunction();
  2719. }