MemorySanitizer.cpp 206 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391
  1. //===- MemorySanitizer.cpp - detector of uninitialized reads --------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file
  10. /// This file is a part of 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 overwriting 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. ///
  92. /// Instrumenting inline assembly.
  93. ///
  94. /// For inline assembly code LLVM has little idea about which memory locations
  95. /// become initialized depending on the arguments. It can be possible to figure
  96. /// out which arguments are meant to point to inputs and outputs, but the
  97. /// actual semantics can be only visible at runtime. In the Linux kernel it's
  98. /// also possible that the arguments only indicate the offset for a base taken
  99. /// from a segment register, so it's dangerous to treat any asm() arguments as
  100. /// pointers. We take a conservative approach generating calls to
  101. /// __msan_instrument_asm_store(ptr, size)
  102. /// , which defer the memory unpoisoning to the runtime library.
  103. /// The latter can perform more complex address checks to figure out whether
  104. /// it's safe to touch the shadow memory.
  105. /// Like with atomic operations, we call __msan_instrument_asm_store() before
  106. /// the assembly call, so that changes to the shadow memory will be seen by
  107. /// other threads together with main memory initialization.
  108. ///
  109. /// KernelMemorySanitizer (KMSAN) implementation.
  110. ///
  111. /// The major differences between KMSAN and MSan instrumentation are:
  112. /// - KMSAN always tracks the origins and implies msan-keep-going=true;
  113. /// - KMSAN allocates shadow and origin memory for each page separately, so
  114. /// there are no explicit accesses to shadow and origin in the
  115. /// instrumentation.
  116. /// Shadow and origin values for a particular X-byte memory location
  117. /// (X=1,2,4,8) are accessed through pointers obtained via the
  118. /// __msan_metadata_ptr_for_load_X(ptr)
  119. /// __msan_metadata_ptr_for_store_X(ptr)
  120. /// functions. The corresponding functions check that the X-byte accesses
  121. /// are possible and returns the pointers to shadow and origin memory.
  122. /// Arbitrary sized accesses are handled with:
  123. /// __msan_metadata_ptr_for_load_n(ptr, size)
  124. /// __msan_metadata_ptr_for_store_n(ptr, size);
  125. /// - TLS variables are stored in a single per-task struct. A call to a
  126. /// function __msan_get_context_state() returning a pointer to that struct
  127. /// is inserted into every instrumented function before the entry block;
  128. /// - __msan_warning() takes a 32-bit origin parameter;
  129. /// - local variables are poisoned with __msan_poison_alloca() upon function
  130. /// entry and unpoisoned with __msan_unpoison_alloca() before leaving the
  131. /// function;
  132. /// - the pass doesn't declare any global variables or add global constructors
  133. /// to the translation unit.
  134. ///
  135. /// Also, KMSAN currently ignores uninitialized memory passed into inline asm
  136. /// calls, making sure we're on the safe side wrt. possible false positives.
  137. ///
  138. /// KernelMemorySanitizer only supports X86_64 at the moment.
  139. ///
  140. //
  141. // FIXME: This sanitizer does not yet handle scalable vectors
  142. //
  143. //===----------------------------------------------------------------------===//
  144. #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
  145. #include "llvm/ADT/APInt.h"
  146. #include "llvm/ADT/ArrayRef.h"
  147. #include "llvm/ADT/DepthFirstIterator.h"
  148. #include "llvm/ADT/SmallSet.h"
  149. #include "llvm/ADT/SmallString.h"
  150. #include "llvm/ADT/SmallVector.h"
  151. #include "llvm/ADT/StringExtras.h"
  152. #include "llvm/ADT/StringRef.h"
  153. #include "llvm/ADT/Triple.h"
  154. #include "llvm/Analysis/TargetLibraryInfo.h"
  155. #include "llvm/Analysis/ValueTracking.h"
  156. #include "llvm/IR/Argument.h"
  157. #include "llvm/IR/Attributes.h"
  158. #include "llvm/IR/BasicBlock.h"
  159. #include "llvm/IR/CallingConv.h"
  160. #include "llvm/IR/Constant.h"
  161. #include "llvm/IR/Constants.h"
  162. #include "llvm/IR/DataLayout.h"
  163. #include "llvm/IR/DerivedTypes.h"
  164. #include "llvm/IR/Function.h"
  165. #include "llvm/IR/GlobalValue.h"
  166. #include "llvm/IR/GlobalVariable.h"
  167. #include "llvm/IR/IRBuilder.h"
  168. #include "llvm/IR/InlineAsm.h"
  169. #include "llvm/IR/InstVisitor.h"
  170. #include "llvm/IR/InstrTypes.h"
  171. #include "llvm/IR/Instruction.h"
  172. #include "llvm/IR/Instructions.h"
  173. #include "llvm/IR/IntrinsicInst.h"
  174. #include "llvm/IR/Intrinsics.h"
  175. #include "llvm/IR/IntrinsicsX86.h"
  176. #include "llvm/IR/LLVMContext.h"
  177. #include "llvm/IR/MDBuilder.h"
  178. #include "llvm/IR/Module.h"
  179. #include "llvm/IR/Type.h"
  180. #include "llvm/IR/Value.h"
  181. #include "llvm/IR/ValueMap.h"
  182. #include "llvm/InitializePasses.h"
  183. #include "llvm/Pass.h"
  184. #include "llvm/Support/Alignment.h"
  185. #include "llvm/Support/AtomicOrdering.h"
  186. #include "llvm/Support/Casting.h"
  187. #include "llvm/Support/CommandLine.h"
  188. #include "llvm/Support/Compiler.h"
  189. #include "llvm/Support/Debug.h"
  190. #include "llvm/Support/ErrorHandling.h"
  191. #include "llvm/Support/MathExtras.h"
  192. #include "llvm/Support/raw_ostream.h"
  193. #include "llvm/Transforms/Instrumentation.h"
  194. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  195. #include "llvm/Transforms/Utils/Local.h"
  196. #include "llvm/Transforms/Utils/ModuleUtils.h"
  197. #include <algorithm>
  198. #include <cassert>
  199. #include <cstddef>
  200. #include <cstdint>
  201. #include <memory>
  202. #include <string>
  203. #include <tuple>
  204. using namespace llvm;
  205. #define DEBUG_TYPE "msan"
  206. static const unsigned kOriginSize = 4;
  207. static const Align kMinOriginAlignment = Align(4);
  208. static const Align kShadowTLSAlignment = Align(8);
  209. // These constants must be kept in sync with the ones in msan.h.
  210. static const unsigned kParamTLSSize = 800;
  211. static const unsigned kRetvalTLSSize = 800;
  212. // Accesses sizes are powers of two: 1, 2, 4, 8.
  213. static const size_t kNumberOfAccessSizes = 4;
  214. /// Track origins of uninitialized values.
  215. ///
  216. /// Adds a section to MemorySanitizer report that points to the allocation
  217. /// (stack or heap) the uninitialized bits came from originally.
  218. static cl::opt<int> ClTrackOrigins("msan-track-origins",
  219. cl::desc("Track origins (allocation sites) of poisoned memory"),
  220. cl::Hidden, cl::init(0));
  221. static cl::opt<bool> ClKeepGoing("msan-keep-going",
  222. cl::desc("keep going after reporting a UMR"),
  223. cl::Hidden, cl::init(false));
  224. static cl::opt<bool> ClPoisonStack("msan-poison-stack",
  225. cl::desc("poison uninitialized stack variables"),
  226. cl::Hidden, cl::init(true));
  227. static cl::opt<bool> ClPoisonStackWithCall("msan-poison-stack-with-call",
  228. cl::desc("poison uninitialized stack variables with a call"),
  229. cl::Hidden, cl::init(false));
  230. static cl::opt<int> ClPoisonStackPattern("msan-poison-stack-pattern",
  231. cl::desc("poison uninitialized stack variables with the given pattern"),
  232. cl::Hidden, cl::init(0xff));
  233. static cl::opt<bool> ClPoisonUndef("msan-poison-undef",
  234. cl::desc("poison undef temps"),
  235. cl::Hidden, cl::init(true));
  236. static cl::opt<bool> ClHandleICmp("msan-handle-icmp",
  237. cl::desc("propagate shadow through ICmpEQ and ICmpNE"),
  238. cl::Hidden, cl::init(true));
  239. static cl::opt<bool> ClHandleICmpExact("msan-handle-icmp-exact",
  240. cl::desc("exact handling of relational integer ICmp"),
  241. cl::Hidden, cl::init(false));
  242. static cl::opt<bool> ClHandleLifetimeIntrinsics(
  243. "msan-handle-lifetime-intrinsics",
  244. cl::desc(
  245. "when possible, poison scoped variables at the beginning of the scope "
  246. "(slower, but more precise)"),
  247. cl::Hidden, cl::init(true));
  248. // When compiling the Linux kernel, we sometimes see false positives related to
  249. // MSan being unable to understand that inline assembly calls may initialize
  250. // local variables.
  251. // This flag makes the compiler conservatively unpoison every memory location
  252. // passed into an assembly call. Note that this may cause false positives.
  253. // Because it's impossible to figure out the array sizes, we can only unpoison
  254. // the first sizeof(type) bytes for each type* pointer.
  255. // The instrumentation is only enabled in KMSAN builds, and only if
  256. // -msan-handle-asm-conservative is on. This is done because we may want to
  257. // quickly disable assembly instrumentation when it breaks.
  258. static cl::opt<bool> ClHandleAsmConservative(
  259. "msan-handle-asm-conservative",
  260. cl::desc("conservative handling of inline assembly"), cl::Hidden,
  261. cl::init(true));
  262. // This flag controls whether we check the shadow of the address
  263. // operand of load or store. Such bugs are very rare, since load from
  264. // a garbage address typically results in SEGV, but still happen
  265. // (e.g. only lower bits of address are garbage, or the access happens
  266. // early at program startup where malloc-ed memory is more likely to
  267. // be zeroed. As of 2012-08-28 this flag adds 20% slowdown.
  268. static cl::opt<bool> ClCheckAccessAddress("msan-check-access-address",
  269. cl::desc("report accesses through a pointer which has poisoned shadow"),
  270. cl::Hidden, cl::init(true));
  271. static cl::opt<bool> ClEagerChecks(
  272. "msan-eager-checks",
  273. cl::desc("check arguments and return values at function call boundaries"),
  274. cl::Hidden, cl::init(false));
  275. static cl::opt<bool> ClDumpStrictInstructions("msan-dump-strict-instructions",
  276. cl::desc("print out instructions with default strict semantics"),
  277. cl::Hidden, cl::init(false));
  278. static cl::opt<int> ClInstrumentationWithCallThreshold(
  279. "msan-instrumentation-with-call-threshold",
  280. cl::desc(
  281. "If the function being instrumented requires more than "
  282. "this number of checks and origin stores, use callbacks instead of "
  283. "inline checks (-1 means never use callbacks)."),
  284. cl::Hidden, cl::init(3500));
  285. static cl::opt<bool>
  286. ClEnableKmsan("msan-kernel",
  287. cl::desc("Enable KernelMemorySanitizer instrumentation"),
  288. cl::Hidden, cl::init(false));
  289. static cl::opt<bool>
  290. ClDisableChecks("msan-disable-checks",
  291. cl::desc("Apply no_sanitize to the whole file"), cl::Hidden,
  292. cl::init(false));
  293. // This is an experiment to enable handling of cases where shadow is a non-zero
  294. // compile-time constant. For some unexplainable reason they were silently
  295. // ignored in the instrumentation.
  296. static cl::opt<bool> ClCheckConstantShadow("msan-check-constant-shadow",
  297. cl::desc("Insert checks for constant shadow values"),
  298. cl::Hidden, cl::init(false));
  299. // This is off by default because of a bug in gold:
  300. // https://sourceware.org/bugzilla/show_bug.cgi?id=19002
  301. static cl::opt<bool> ClWithComdat("msan-with-comdat",
  302. cl::desc("Place MSan constructors in comdat sections"),
  303. cl::Hidden, cl::init(false));
  304. // These options allow to specify custom memory map parameters
  305. // See MemoryMapParams for details.
  306. static cl::opt<uint64_t> ClAndMask("msan-and-mask",
  307. cl::desc("Define custom MSan AndMask"),
  308. cl::Hidden, cl::init(0));
  309. static cl::opt<uint64_t> ClXorMask("msan-xor-mask",
  310. cl::desc("Define custom MSan XorMask"),
  311. cl::Hidden, cl::init(0));
  312. static cl::opt<uint64_t> ClShadowBase("msan-shadow-base",
  313. cl::desc("Define custom MSan ShadowBase"),
  314. cl::Hidden, cl::init(0));
  315. static cl::opt<uint64_t> ClOriginBase("msan-origin-base",
  316. cl::desc("Define custom MSan OriginBase"),
  317. cl::Hidden, cl::init(0));
  318. const char kMsanModuleCtorName[] = "msan.module_ctor";
  319. const char kMsanInitName[] = "__msan_init";
  320. namespace {
  321. // Memory map parameters used in application-to-shadow address calculation.
  322. // Offset = (Addr & ~AndMask) ^ XorMask
  323. // Shadow = ShadowBase + Offset
  324. // Origin = OriginBase + Offset
  325. struct MemoryMapParams {
  326. uint64_t AndMask;
  327. uint64_t XorMask;
  328. uint64_t ShadowBase;
  329. uint64_t OriginBase;
  330. };
  331. struct PlatformMemoryMapParams {
  332. const MemoryMapParams *bits32;
  333. const MemoryMapParams *bits64;
  334. };
  335. } // end anonymous namespace
  336. // i386 Linux
  337. static const MemoryMapParams Linux_I386_MemoryMapParams = {
  338. 0x000080000000, // AndMask
  339. 0, // XorMask (not used)
  340. 0, // ShadowBase (not used)
  341. 0x000040000000, // OriginBase
  342. };
  343. // x86_64 Linux
  344. static const MemoryMapParams Linux_X86_64_MemoryMapParams = {
  345. #ifdef MSAN_LINUX_X86_64_OLD_MAPPING
  346. 0x400000000000, // AndMask
  347. 0, // XorMask (not used)
  348. 0, // ShadowBase (not used)
  349. 0x200000000000, // OriginBase
  350. #else
  351. 0, // AndMask (not used)
  352. 0x500000000000, // XorMask
  353. 0, // ShadowBase (not used)
  354. 0x100000000000, // OriginBase
  355. #endif
  356. };
  357. // mips64 Linux
  358. static const MemoryMapParams Linux_MIPS64_MemoryMapParams = {
  359. 0, // AndMask (not used)
  360. 0x008000000000, // XorMask
  361. 0, // ShadowBase (not used)
  362. 0x002000000000, // OriginBase
  363. };
  364. // ppc64 Linux
  365. static const MemoryMapParams Linux_PowerPC64_MemoryMapParams = {
  366. 0xE00000000000, // AndMask
  367. 0x100000000000, // XorMask
  368. 0x080000000000, // ShadowBase
  369. 0x1C0000000000, // OriginBase
  370. };
  371. // s390x Linux
  372. static const MemoryMapParams Linux_S390X_MemoryMapParams = {
  373. 0xC00000000000, // AndMask
  374. 0, // XorMask (not used)
  375. 0x080000000000, // ShadowBase
  376. 0x1C0000000000, // OriginBase
  377. };
  378. // aarch64 Linux
  379. static const MemoryMapParams Linux_AArch64_MemoryMapParams = {
  380. 0, // AndMask (not used)
  381. 0x06000000000, // XorMask
  382. 0, // ShadowBase (not used)
  383. 0x01000000000, // OriginBase
  384. };
  385. // i386 FreeBSD
  386. static const MemoryMapParams FreeBSD_I386_MemoryMapParams = {
  387. 0x000180000000, // AndMask
  388. 0x000040000000, // XorMask
  389. 0x000020000000, // ShadowBase
  390. 0x000700000000, // OriginBase
  391. };
  392. // x86_64 FreeBSD
  393. static const MemoryMapParams FreeBSD_X86_64_MemoryMapParams = {
  394. 0xc00000000000, // AndMask
  395. 0x200000000000, // XorMask
  396. 0x100000000000, // ShadowBase
  397. 0x380000000000, // OriginBase
  398. };
  399. // x86_64 NetBSD
  400. static const MemoryMapParams NetBSD_X86_64_MemoryMapParams = {
  401. 0, // AndMask
  402. 0x500000000000, // XorMask
  403. 0, // ShadowBase
  404. 0x100000000000, // OriginBase
  405. };
  406. static const PlatformMemoryMapParams Linux_X86_MemoryMapParams = {
  407. &Linux_I386_MemoryMapParams,
  408. &Linux_X86_64_MemoryMapParams,
  409. };
  410. static const PlatformMemoryMapParams Linux_MIPS_MemoryMapParams = {
  411. nullptr,
  412. &Linux_MIPS64_MemoryMapParams,
  413. };
  414. static const PlatformMemoryMapParams Linux_PowerPC_MemoryMapParams = {
  415. nullptr,
  416. &Linux_PowerPC64_MemoryMapParams,
  417. };
  418. static const PlatformMemoryMapParams Linux_S390_MemoryMapParams = {
  419. nullptr,
  420. &Linux_S390X_MemoryMapParams,
  421. };
  422. static const PlatformMemoryMapParams Linux_ARM_MemoryMapParams = {
  423. nullptr,
  424. &Linux_AArch64_MemoryMapParams,
  425. };
  426. static const PlatformMemoryMapParams FreeBSD_X86_MemoryMapParams = {
  427. &FreeBSD_I386_MemoryMapParams,
  428. &FreeBSD_X86_64_MemoryMapParams,
  429. };
  430. static const PlatformMemoryMapParams NetBSD_X86_MemoryMapParams = {
  431. nullptr,
  432. &NetBSD_X86_64_MemoryMapParams,
  433. };
  434. namespace {
  435. /// Instrument functions of a module to detect uninitialized reads.
  436. ///
  437. /// Instantiating MemorySanitizer inserts the msan runtime library API function
  438. /// declarations into the module if they don't exist already. Instantiating
  439. /// ensures the __msan_init function is in the list of global constructors for
  440. /// the module.
  441. class MemorySanitizer {
  442. public:
  443. MemorySanitizer(Module &M, MemorySanitizerOptions Options)
  444. : CompileKernel(Options.Kernel), TrackOrigins(Options.TrackOrigins),
  445. Recover(Options.Recover), EagerChecks(Options.EagerChecks) {
  446. initializeModule(M);
  447. }
  448. // MSan cannot be moved or copied because of MapParams.
  449. MemorySanitizer(MemorySanitizer &&) = delete;
  450. MemorySanitizer &operator=(MemorySanitizer &&) = delete;
  451. MemorySanitizer(const MemorySanitizer &) = delete;
  452. MemorySanitizer &operator=(const MemorySanitizer &) = delete;
  453. bool sanitizeFunction(Function &F, TargetLibraryInfo &TLI);
  454. private:
  455. friend struct MemorySanitizerVisitor;
  456. friend struct VarArgAMD64Helper;
  457. friend struct VarArgMIPS64Helper;
  458. friend struct VarArgAArch64Helper;
  459. friend struct VarArgPowerPC64Helper;
  460. friend struct VarArgSystemZHelper;
  461. void initializeModule(Module &M);
  462. void initializeCallbacks(Module &M);
  463. void createKernelApi(Module &M);
  464. void createUserspaceApi(Module &M);
  465. /// True if we're compiling the Linux kernel.
  466. bool CompileKernel;
  467. /// Track origins (allocation points) of uninitialized values.
  468. int TrackOrigins;
  469. bool Recover;
  470. bool EagerChecks;
  471. LLVMContext *C;
  472. Type *IntptrTy;
  473. Type *OriginTy;
  474. // XxxTLS variables represent the per-thread state in MSan and per-task state
  475. // in KMSAN.
  476. // For the userspace these point to thread-local globals. In the kernel land
  477. // they point to the members of a per-task struct obtained via a call to
  478. // __msan_get_context_state().
  479. /// Thread-local shadow storage for function parameters.
  480. Value *ParamTLS;
  481. /// Thread-local origin storage for function parameters.
  482. Value *ParamOriginTLS;
  483. /// Thread-local shadow storage for function return value.
  484. Value *RetvalTLS;
  485. /// Thread-local origin storage for function return value.
  486. Value *RetvalOriginTLS;
  487. /// Thread-local shadow storage for in-register va_arg function
  488. /// parameters (x86_64-specific).
  489. Value *VAArgTLS;
  490. /// Thread-local shadow storage for in-register va_arg function
  491. /// parameters (x86_64-specific).
  492. Value *VAArgOriginTLS;
  493. /// Thread-local shadow storage for va_arg overflow area
  494. /// (x86_64-specific).
  495. Value *VAArgOverflowSizeTLS;
  496. /// Are the instrumentation callbacks set up?
  497. bool CallbacksInitialized = false;
  498. /// The run-time callback to print a warning.
  499. FunctionCallee WarningFn;
  500. // These arrays are indexed by log2(AccessSize).
  501. FunctionCallee MaybeWarningFn[kNumberOfAccessSizes];
  502. FunctionCallee MaybeStoreOriginFn[kNumberOfAccessSizes];
  503. /// Run-time helper that generates a new origin value for a stack
  504. /// allocation.
  505. FunctionCallee MsanSetAllocaOrigin4Fn;
  506. /// Run-time helper that poisons stack on function entry.
  507. FunctionCallee MsanPoisonStackFn;
  508. /// Run-time helper that records a store (or any event) of an
  509. /// uninitialized value and returns an updated origin id encoding this info.
  510. FunctionCallee MsanChainOriginFn;
  511. /// Run-time helper that paints an origin over a region.
  512. FunctionCallee MsanSetOriginFn;
  513. /// MSan runtime replacements for memmove, memcpy and memset.
  514. FunctionCallee MemmoveFn, MemcpyFn, MemsetFn;
  515. /// KMSAN callback for task-local function argument shadow.
  516. StructType *MsanContextStateTy;
  517. FunctionCallee MsanGetContextStateFn;
  518. /// Functions for poisoning/unpoisoning local variables
  519. FunctionCallee MsanPoisonAllocaFn, MsanUnpoisonAllocaFn;
  520. /// Each of the MsanMetadataPtrXxx functions returns a pair of shadow/origin
  521. /// pointers.
  522. FunctionCallee MsanMetadataPtrForLoadN, MsanMetadataPtrForStoreN;
  523. FunctionCallee MsanMetadataPtrForLoad_1_8[4];
  524. FunctionCallee MsanMetadataPtrForStore_1_8[4];
  525. FunctionCallee MsanInstrumentAsmStoreFn;
  526. /// Helper to choose between different MsanMetadataPtrXxx().
  527. FunctionCallee getKmsanShadowOriginAccessFn(bool isStore, int size);
  528. /// Memory map parameters used in application-to-shadow calculation.
  529. const MemoryMapParams *MapParams;
  530. /// Custom memory map parameters used when -msan-shadow-base or
  531. // -msan-origin-base is provided.
  532. MemoryMapParams CustomMapParams;
  533. MDNode *ColdCallWeights;
  534. /// Branch weights for origin store.
  535. MDNode *OriginStoreWeights;
  536. };
  537. void insertModuleCtor(Module &M) {
  538. getOrCreateSanitizerCtorAndInitFunctions(
  539. M, kMsanModuleCtorName, kMsanInitName,
  540. /*InitArgTypes=*/{},
  541. /*InitArgs=*/{},
  542. // This callback is invoked when the functions are created the first
  543. // time. Hook them into the global ctors list in that case:
  544. [&](Function *Ctor, FunctionCallee) {
  545. if (!ClWithComdat) {
  546. appendToGlobalCtors(M, Ctor, 0);
  547. return;
  548. }
  549. Comdat *MsanCtorComdat = M.getOrInsertComdat(kMsanModuleCtorName);
  550. Ctor->setComdat(MsanCtorComdat);
  551. appendToGlobalCtors(M, Ctor, 0, Ctor);
  552. });
  553. }
  554. /// A legacy function pass for msan instrumentation.
  555. ///
  556. /// Instruments functions to detect uninitialized reads.
  557. struct MemorySanitizerLegacyPass : public FunctionPass {
  558. // Pass identification, replacement for typeid.
  559. static char ID;
  560. MemorySanitizerLegacyPass(MemorySanitizerOptions Options = {})
  561. : FunctionPass(ID), Options(Options) {
  562. initializeMemorySanitizerLegacyPassPass(*PassRegistry::getPassRegistry());
  563. }
  564. StringRef getPassName() const override { return "MemorySanitizerLegacyPass"; }
  565. void getAnalysisUsage(AnalysisUsage &AU) const override {
  566. AU.addRequired<TargetLibraryInfoWrapperPass>();
  567. }
  568. bool runOnFunction(Function &F) override {
  569. return MSan->sanitizeFunction(
  570. F, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F));
  571. }
  572. bool doInitialization(Module &M) override;
  573. Optional<MemorySanitizer> MSan;
  574. MemorySanitizerOptions Options;
  575. };
  576. template <class T> T getOptOrDefault(const cl::opt<T> &Opt, T Default) {
  577. return (Opt.getNumOccurrences() > 0) ? Opt : Default;
  578. }
  579. } // end anonymous namespace
  580. MemorySanitizerOptions::MemorySanitizerOptions(int TO, bool R, bool K,
  581. bool EagerChecks)
  582. : Kernel(getOptOrDefault(ClEnableKmsan, K)),
  583. TrackOrigins(getOptOrDefault(ClTrackOrigins, Kernel ? 2 : TO)),
  584. Recover(getOptOrDefault(ClKeepGoing, Kernel || R)),
  585. EagerChecks(getOptOrDefault(ClEagerChecks, EagerChecks)) {}
  586. PreservedAnalyses MemorySanitizerPass::run(Function &F,
  587. FunctionAnalysisManager &FAM) {
  588. MemorySanitizer Msan(*F.getParent(), Options);
  589. if (Msan.sanitizeFunction(F, FAM.getResult<TargetLibraryAnalysis>(F)))
  590. return PreservedAnalyses::none();
  591. return PreservedAnalyses::all();
  592. }
  593. PreservedAnalyses
  594. ModuleMemorySanitizerPass::run(Module &M, ModuleAnalysisManager &AM) {
  595. if (Options.Kernel)
  596. return PreservedAnalyses::all();
  597. insertModuleCtor(M);
  598. return PreservedAnalyses::none();
  599. }
  600. void MemorySanitizerPass::printPipeline(
  601. raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
  602. static_cast<PassInfoMixin<MemorySanitizerPass> *>(this)->printPipeline(
  603. OS, MapClassName2PassName);
  604. OS << "<";
  605. if (Options.Recover)
  606. OS << "recover;";
  607. if (Options.Kernel)
  608. OS << "kernel;";
  609. if (Options.EagerChecks)
  610. OS << "eager-checks;";
  611. OS << "track-origins=" << Options.TrackOrigins;
  612. OS << ">";
  613. }
  614. char MemorySanitizerLegacyPass::ID = 0;
  615. INITIALIZE_PASS_BEGIN(MemorySanitizerLegacyPass, "msan",
  616. "MemorySanitizer: detects uninitialized reads.", false,
  617. false)
  618. INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
  619. INITIALIZE_PASS_END(MemorySanitizerLegacyPass, "msan",
  620. "MemorySanitizer: detects uninitialized reads.", false,
  621. false)
  622. FunctionPass *
  623. llvm::createMemorySanitizerLegacyPassPass(MemorySanitizerOptions Options) {
  624. return new MemorySanitizerLegacyPass(Options);
  625. }
  626. /// Create a non-const global initialized with the given string.
  627. ///
  628. /// Creates a writable global for Str so that we can pass it to the
  629. /// run-time lib. Runtime uses first 4 bytes of the string to store the
  630. /// frame ID, so the string needs to be mutable.
  631. static GlobalVariable *createPrivateNonConstGlobalForString(Module &M,
  632. StringRef Str) {
  633. Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
  634. return new GlobalVariable(M, StrConst->getType(), /*isConstant=*/false,
  635. GlobalValue::PrivateLinkage, StrConst, "");
  636. }
  637. /// Create KMSAN API callbacks.
  638. void MemorySanitizer::createKernelApi(Module &M) {
  639. IRBuilder<> IRB(*C);
  640. // These will be initialized in insertKmsanPrologue().
  641. RetvalTLS = nullptr;
  642. RetvalOriginTLS = nullptr;
  643. ParamTLS = nullptr;
  644. ParamOriginTLS = nullptr;
  645. VAArgTLS = nullptr;
  646. VAArgOriginTLS = nullptr;
  647. VAArgOverflowSizeTLS = nullptr;
  648. WarningFn = M.getOrInsertFunction("__msan_warning", IRB.getVoidTy(),
  649. IRB.getInt32Ty());
  650. // Requests the per-task context state (kmsan_context_state*) from the
  651. // runtime library.
  652. MsanContextStateTy = StructType::get(
  653. ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8),
  654. ArrayType::get(IRB.getInt64Ty(), kRetvalTLSSize / 8),
  655. ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8),
  656. ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8), /* va_arg_origin */
  657. IRB.getInt64Ty(), ArrayType::get(OriginTy, kParamTLSSize / 4), OriginTy,
  658. OriginTy);
  659. MsanGetContextStateFn = M.getOrInsertFunction(
  660. "__msan_get_context_state", PointerType::get(MsanContextStateTy, 0));
  661. Type *RetTy = StructType::get(PointerType::get(IRB.getInt8Ty(), 0),
  662. PointerType::get(IRB.getInt32Ty(), 0));
  663. for (int ind = 0, size = 1; ind < 4; ind++, size <<= 1) {
  664. std::string name_load =
  665. "__msan_metadata_ptr_for_load_" + std::to_string(size);
  666. std::string name_store =
  667. "__msan_metadata_ptr_for_store_" + std::to_string(size);
  668. MsanMetadataPtrForLoad_1_8[ind] = M.getOrInsertFunction(
  669. name_load, RetTy, PointerType::get(IRB.getInt8Ty(), 0));
  670. MsanMetadataPtrForStore_1_8[ind] = M.getOrInsertFunction(
  671. name_store, RetTy, PointerType::get(IRB.getInt8Ty(), 0));
  672. }
  673. MsanMetadataPtrForLoadN = M.getOrInsertFunction(
  674. "__msan_metadata_ptr_for_load_n", RetTy,
  675. PointerType::get(IRB.getInt8Ty(), 0), IRB.getInt64Ty());
  676. MsanMetadataPtrForStoreN = M.getOrInsertFunction(
  677. "__msan_metadata_ptr_for_store_n", RetTy,
  678. PointerType::get(IRB.getInt8Ty(), 0), IRB.getInt64Ty());
  679. // Functions for poisoning and unpoisoning memory.
  680. MsanPoisonAllocaFn =
  681. M.getOrInsertFunction("__msan_poison_alloca", IRB.getVoidTy(),
  682. IRB.getInt8PtrTy(), IntptrTy, IRB.getInt8PtrTy());
  683. MsanUnpoisonAllocaFn = M.getOrInsertFunction(
  684. "__msan_unpoison_alloca", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy);
  685. }
  686. static Constant *getOrInsertGlobal(Module &M, StringRef Name, Type *Ty) {
  687. return M.getOrInsertGlobal(Name, Ty, [&] {
  688. return new GlobalVariable(M, Ty, false, GlobalVariable::ExternalLinkage,
  689. nullptr, Name, nullptr,
  690. GlobalVariable::InitialExecTLSModel);
  691. });
  692. }
  693. /// Insert declarations for userspace-specific functions and globals.
  694. void MemorySanitizer::createUserspaceApi(Module &M) {
  695. IRBuilder<> IRB(*C);
  696. // Create the callback.
  697. // FIXME: this function should have "Cold" calling conv,
  698. // which is not yet implemented.
  699. StringRef WarningFnName = Recover ? "__msan_warning_with_origin"
  700. : "__msan_warning_with_origin_noreturn";
  701. WarningFn =
  702. M.getOrInsertFunction(WarningFnName, IRB.getVoidTy(), IRB.getInt32Ty());
  703. // Create the global TLS variables.
  704. RetvalTLS =
  705. getOrInsertGlobal(M, "__msan_retval_tls",
  706. ArrayType::get(IRB.getInt64Ty(), kRetvalTLSSize / 8));
  707. RetvalOriginTLS = getOrInsertGlobal(M, "__msan_retval_origin_tls", OriginTy);
  708. ParamTLS =
  709. getOrInsertGlobal(M, "__msan_param_tls",
  710. ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8));
  711. ParamOriginTLS =
  712. getOrInsertGlobal(M, "__msan_param_origin_tls",
  713. ArrayType::get(OriginTy, kParamTLSSize / 4));
  714. VAArgTLS =
  715. getOrInsertGlobal(M, "__msan_va_arg_tls",
  716. ArrayType::get(IRB.getInt64Ty(), kParamTLSSize / 8));
  717. VAArgOriginTLS =
  718. getOrInsertGlobal(M, "__msan_va_arg_origin_tls",
  719. ArrayType::get(OriginTy, kParamTLSSize / 4));
  720. VAArgOverflowSizeTLS =
  721. getOrInsertGlobal(M, "__msan_va_arg_overflow_size_tls", IRB.getInt64Ty());
  722. for (size_t AccessSizeIndex = 0; AccessSizeIndex < kNumberOfAccessSizes;
  723. AccessSizeIndex++) {
  724. unsigned AccessSize = 1 << AccessSizeIndex;
  725. std::string FunctionName = "__msan_maybe_warning_" + itostr(AccessSize);
  726. SmallVector<std::pair<unsigned, Attribute>, 2> MaybeWarningFnAttrs;
  727. MaybeWarningFnAttrs.push_back(std::make_pair(
  728. AttributeList::FirstArgIndex, Attribute::get(*C, Attribute::ZExt)));
  729. MaybeWarningFnAttrs.push_back(std::make_pair(
  730. AttributeList::FirstArgIndex + 1, Attribute::get(*C, Attribute::ZExt)));
  731. MaybeWarningFn[AccessSizeIndex] = M.getOrInsertFunction(
  732. FunctionName, AttributeList::get(*C, MaybeWarningFnAttrs),
  733. IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8), IRB.getInt32Ty());
  734. FunctionName = "__msan_maybe_store_origin_" + itostr(AccessSize);
  735. SmallVector<std::pair<unsigned, Attribute>, 2> MaybeStoreOriginFnAttrs;
  736. MaybeStoreOriginFnAttrs.push_back(std::make_pair(
  737. AttributeList::FirstArgIndex, Attribute::get(*C, Attribute::ZExt)));
  738. MaybeStoreOriginFnAttrs.push_back(std::make_pair(
  739. AttributeList::FirstArgIndex + 2, Attribute::get(*C, Attribute::ZExt)));
  740. MaybeStoreOriginFn[AccessSizeIndex] = M.getOrInsertFunction(
  741. FunctionName, AttributeList::get(*C, MaybeStoreOriginFnAttrs),
  742. IRB.getVoidTy(), IRB.getIntNTy(AccessSize * 8), IRB.getInt8PtrTy(),
  743. IRB.getInt32Ty());
  744. }
  745. MsanSetAllocaOrigin4Fn = M.getOrInsertFunction(
  746. "__msan_set_alloca_origin4", IRB.getVoidTy(), IRB.getInt8PtrTy(), IntptrTy,
  747. IRB.getInt8PtrTy(), IntptrTy);
  748. MsanPoisonStackFn =
  749. M.getOrInsertFunction("__msan_poison_stack", IRB.getVoidTy(),
  750. IRB.getInt8PtrTy(), IntptrTy);
  751. }
  752. /// Insert extern declaration of runtime-provided functions and globals.
  753. void MemorySanitizer::initializeCallbacks(Module &M) {
  754. // Only do this once.
  755. if (CallbacksInitialized)
  756. return;
  757. IRBuilder<> IRB(*C);
  758. // Initialize callbacks that are common for kernel and userspace
  759. // instrumentation.
  760. MsanChainOriginFn = M.getOrInsertFunction(
  761. "__msan_chain_origin", IRB.getInt32Ty(), IRB.getInt32Ty());
  762. MsanSetOriginFn =
  763. M.getOrInsertFunction("__msan_set_origin", IRB.getVoidTy(),
  764. IRB.getInt8PtrTy(), IntptrTy, IRB.getInt32Ty());
  765. MemmoveFn = M.getOrInsertFunction(
  766. "__msan_memmove", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
  767. IRB.getInt8PtrTy(), IntptrTy);
  768. MemcpyFn = M.getOrInsertFunction(
  769. "__msan_memcpy", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt8PtrTy(),
  770. IntptrTy);
  771. MemsetFn = M.getOrInsertFunction(
  772. "__msan_memset", IRB.getInt8PtrTy(), IRB.getInt8PtrTy(), IRB.getInt32Ty(),
  773. IntptrTy);
  774. MsanInstrumentAsmStoreFn =
  775. M.getOrInsertFunction("__msan_instrument_asm_store", IRB.getVoidTy(),
  776. PointerType::get(IRB.getInt8Ty(), 0), IntptrTy);
  777. if (CompileKernel) {
  778. createKernelApi(M);
  779. } else {
  780. createUserspaceApi(M);
  781. }
  782. CallbacksInitialized = true;
  783. }
  784. FunctionCallee MemorySanitizer::getKmsanShadowOriginAccessFn(bool isStore,
  785. int size) {
  786. FunctionCallee *Fns =
  787. isStore ? MsanMetadataPtrForStore_1_8 : MsanMetadataPtrForLoad_1_8;
  788. switch (size) {
  789. case 1:
  790. return Fns[0];
  791. case 2:
  792. return Fns[1];
  793. case 4:
  794. return Fns[2];
  795. case 8:
  796. return Fns[3];
  797. default:
  798. return nullptr;
  799. }
  800. }
  801. /// Module-level initialization.
  802. ///
  803. /// inserts a call to __msan_init to the module's constructor list.
  804. void MemorySanitizer::initializeModule(Module &M) {
  805. auto &DL = M.getDataLayout();
  806. bool ShadowPassed = ClShadowBase.getNumOccurrences() > 0;
  807. bool OriginPassed = ClOriginBase.getNumOccurrences() > 0;
  808. // Check the overrides first
  809. if (ShadowPassed || OriginPassed) {
  810. CustomMapParams.AndMask = ClAndMask;
  811. CustomMapParams.XorMask = ClXorMask;
  812. CustomMapParams.ShadowBase = ClShadowBase;
  813. CustomMapParams.OriginBase = ClOriginBase;
  814. MapParams = &CustomMapParams;
  815. } else {
  816. Triple TargetTriple(M.getTargetTriple());
  817. switch (TargetTriple.getOS()) {
  818. case Triple::FreeBSD:
  819. switch (TargetTriple.getArch()) {
  820. case Triple::x86_64:
  821. MapParams = FreeBSD_X86_MemoryMapParams.bits64;
  822. break;
  823. case Triple::x86:
  824. MapParams = FreeBSD_X86_MemoryMapParams.bits32;
  825. break;
  826. default:
  827. report_fatal_error("unsupported architecture");
  828. }
  829. break;
  830. case Triple::NetBSD:
  831. switch (TargetTriple.getArch()) {
  832. case Triple::x86_64:
  833. MapParams = NetBSD_X86_MemoryMapParams.bits64;
  834. break;
  835. default:
  836. report_fatal_error("unsupported architecture");
  837. }
  838. break;
  839. case Triple::Linux:
  840. switch (TargetTriple.getArch()) {
  841. case Triple::x86_64:
  842. MapParams = Linux_X86_MemoryMapParams.bits64;
  843. break;
  844. case Triple::x86:
  845. MapParams = Linux_X86_MemoryMapParams.bits32;
  846. break;
  847. case Triple::mips64:
  848. case Triple::mips64el:
  849. MapParams = Linux_MIPS_MemoryMapParams.bits64;
  850. break;
  851. case Triple::ppc64:
  852. case Triple::ppc64le:
  853. MapParams = Linux_PowerPC_MemoryMapParams.bits64;
  854. break;
  855. case Triple::systemz:
  856. MapParams = Linux_S390_MemoryMapParams.bits64;
  857. break;
  858. case Triple::aarch64:
  859. case Triple::aarch64_be:
  860. MapParams = Linux_ARM_MemoryMapParams.bits64;
  861. break;
  862. default:
  863. report_fatal_error("unsupported architecture");
  864. }
  865. break;
  866. default:
  867. report_fatal_error("unsupported operating system");
  868. }
  869. }
  870. C = &(M.getContext());
  871. IRBuilder<> IRB(*C);
  872. IntptrTy = IRB.getIntPtrTy(DL);
  873. OriginTy = IRB.getInt32Ty();
  874. ColdCallWeights = MDBuilder(*C).createBranchWeights(1, 1000);
  875. OriginStoreWeights = MDBuilder(*C).createBranchWeights(1, 1000);
  876. if (!CompileKernel) {
  877. if (TrackOrigins)
  878. M.getOrInsertGlobal("__msan_track_origins", IRB.getInt32Ty(), [&] {
  879. return new GlobalVariable(
  880. M, IRB.getInt32Ty(), true, GlobalValue::WeakODRLinkage,
  881. IRB.getInt32(TrackOrigins), "__msan_track_origins");
  882. });
  883. if (Recover)
  884. M.getOrInsertGlobal("__msan_keep_going", IRB.getInt32Ty(), [&] {
  885. return new GlobalVariable(M, IRB.getInt32Ty(), true,
  886. GlobalValue::WeakODRLinkage,
  887. IRB.getInt32(Recover), "__msan_keep_going");
  888. });
  889. }
  890. }
  891. bool MemorySanitizerLegacyPass::doInitialization(Module &M) {
  892. if (!Options.Kernel)
  893. insertModuleCtor(M);
  894. MSan.emplace(M, Options);
  895. return true;
  896. }
  897. namespace {
  898. /// A helper class that handles instrumentation of VarArg
  899. /// functions on a particular platform.
  900. ///
  901. /// Implementations are expected to insert the instrumentation
  902. /// necessary to propagate argument shadow through VarArg function
  903. /// calls. Visit* methods are called during an InstVisitor pass over
  904. /// the function, and should avoid creating new basic blocks. A new
  905. /// instance of this class is created for each instrumented function.
  906. struct VarArgHelper {
  907. virtual ~VarArgHelper() = default;
  908. /// Visit a CallBase.
  909. virtual void visitCallBase(CallBase &CB, IRBuilder<> &IRB) = 0;
  910. /// Visit a va_start call.
  911. virtual void visitVAStartInst(VAStartInst &I) = 0;
  912. /// Visit a va_copy call.
  913. virtual void visitVACopyInst(VACopyInst &I) = 0;
  914. /// Finalize function instrumentation.
  915. ///
  916. /// This method is called after visiting all interesting (see above)
  917. /// instructions in a function.
  918. virtual void finalizeInstrumentation() = 0;
  919. };
  920. struct MemorySanitizerVisitor;
  921. } // end anonymous namespace
  922. static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
  923. MemorySanitizerVisitor &Visitor);
  924. static unsigned TypeSizeToSizeIndex(unsigned TypeSize) {
  925. if (TypeSize <= 8) return 0;
  926. return Log2_32_Ceil((TypeSize + 7) / 8);
  927. }
  928. namespace {
  929. /// This class does all the work for a given function. Store and Load
  930. /// instructions store and load corresponding shadow and origin
  931. /// values. Most instructions propagate shadow from arguments to their
  932. /// return values. Certain instructions (most importantly, BranchInst)
  933. /// test their argument shadow and print reports (with a runtime call) if it's
  934. /// non-zero.
  935. struct MemorySanitizerVisitor : public InstVisitor<MemorySanitizerVisitor> {
  936. Function &F;
  937. MemorySanitizer &MS;
  938. SmallVector<PHINode *, 16> ShadowPHINodes, OriginPHINodes;
  939. ValueMap<Value*, Value*> ShadowMap, OriginMap;
  940. std::unique_ptr<VarArgHelper> VAHelper;
  941. const TargetLibraryInfo *TLI;
  942. Instruction *FnPrologueEnd;
  943. // The following flags disable parts of MSan instrumentation based on
  944. // exclusion list contents and command-line options.
  945. bool InsertChecks;
  946. bool PropagateShadow;
  947. bool PoisonStack;
  948. bool PoisonUndef;
  949. struct ShadowOriginAndInsertPoint {
  950. Value *Shadow;
  951. Value *Origin;
  952. Instruction *OrigIns;
  953. ShadowOriginAndInsertPoint(Value *S, Value *O, Instruction *I)
  954. : Shadow(S), Origin(O), OrigIns(I) {}
  955. };
  956. SmallVector<ShadowOriginAndInsertPoint, 16> InstrumentationList;
  957. bool InstrumentLifetimeStart = ClHandleLifetimeIntrinsics;
  958. SmallSet<AllocaInst *, 16> AllocaSet;
  959. SmallVector<std::pair<IntrinsicInst *, AllocaInst *>, 16> LifetimeStartList;
  960. SmallVector<StoreInst *, 16> StoreList;
  961. MemorySanitizerVisitor(Function &F, MemorySanitizer &MS,
  962. const TargetLibraryInfo &TLI)
  963. : F(F), MS(MS), VAHelper(CreateVarArgHelper(F, MS, *this)), TLI(&TLI) {
  964. bool SanitizeFunction =
  965. F.hasFnAttribute(Attribute::SanitizeMemory) && !ClDisableChecks;
  966. InsertChecks = SanitizeFunction;
  967. PropagateShadow = SanitizeFunction;
  968. PoisonStack = SanitizeFunction && ClPoisonStack;
  969. PoisonUndef = SanitizeFunction && ClPoisonUndef;
  970. // In the presence of unreachable blocks, we may see Phi nodes with
  971. // incoming nodes from such blocks. Since InstVisitor skips unreachable
  972. // blocks, such nodes will not have any shadow value associated with them.
  973. // It's easier to remove unreachable blocks than deal with missing shadow.
  974. removeUnreachableBlocks(F);
  975. MS.initializeCallbacks(*F.getParent());
  976. FnPrologueEnd = IRBuilder<>(F.getEntryBlock().getFirstNonPHI())
  977. .CreateIntrinsic(Intrinsic::donothing, {}, {});
  978. if (MS.CompileKernel) {
  979. IRBuilder<> IRB(FnPrologueEnd);
  980. insertKmsanPrologue(IRB);
  981. }
  982. LLVM_DEBUG(if (!InsertChecks) dbgs()
  983. << "MemorySanitizer is not inserting checks into '"
  984. << F.getName() << "'\n");
  985. }
  986. bool isInPrologue(Instruction &I) {
  987. return I.getParent() == FnPrologueEnd->getParent() &&
  988. (&I == FnPrologueEnd || I.comesBefore(FnPrologueEnd));
  989. }
  990. Value *updateOrigin(Value *V, IRBuilder<> &IRB) {
  991. if (MS.TrackOrigins <= 1) return V;
  992. return IRB.CreateCall(MS.MsanChainOriginFn, V);
  993. }
  994. Value *originToIntptr(IRBuilder<> &IRB, Value *Origin) {
  995. const DataLayout &DL = F.getParent()->getDataLayout();
  996. unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
  997. if (IntptrSize == kOriginSize) return Origin;
  998. assert(IntptrSize == kOriginSize * 2);
  999. Origin = IRB.CreateIntCast(Origin, MS.IntptrTy, /* isSigned */ false);
  1000. return IRB.CreateOr(Origin, IRB.CreateShl(Origin, kOriginSize * 8));
  1001. }
  1002. /// Fill memory range with the given origin value.
  1003. void paintOrigin(IRBuilder<> &IRB, Value *Origin, Value *OriginPtr,
  1004. unsigned Size, Align Alignment) {
  1005. const DataLayout &DL = F.getParent()->getDataLayout();
  1006. const Align IntptrAlignment = DL.getABITypeAlign(MS.IntptrTy);
  1007. unsigned IntptrSize = DL.getTypeStoreSize(MS.IntptrTy);
  1008. assert(IntptrAlignment >= kMinOriginAlignment);
  1009. assert(IntptrSize >= kOriginSize);
  1010. unsigned Ofs = 0;
  1011. Align CurrentAlignment = Alignment;
  1012. if (Alignment >= IntptrAlignment && IntptrSize > kOriginSize) {
  1013. Value *IntptrOrigin = originToIntptr(IRB, Origin);
  1014. Value *IntptrOriginPtr =
  1015. IRB.CreatePointerCast(OriginPtr, PointerType::get(MS.IntptrTy, 0));
  1016. for (unsigned i = 0; i < Size / IntptrSize; ++i) {
  1017. Value *Ptr = i ? IRB.CreateConstGEP1_32(MS.IntptrTy, IntptrOriginPtr, i)
  1018. : IntptrOriginPtr;
  1019. IRB.CreateAlignedStore(IntptrOrigin, Ptr, CurrentAlignment);
  1020. Ofs += IntptrSize / kOriginSize;
  1021. CurrentAlignment = IntptrAlignment;
  1022. }
  1023. }
  1024. for (unsigned i = Ofs; i < (Size + kOriginSize - 1) / kOriginSize; ++i) {
  1025. Value *GEP =
  1026. i ? IRB.CreateConstGEP1_32(MS.OriginTy, OriginPtr, i) : OriginPtr;
  1027. IRB.CreateAlignedStore(Origin, GEP, CurrentAlignment);
  1028. CurrentAlignment = kMinOriginAlignment;
  1029. }
  1030. }
  1031. void storeOrigin(IRBuilder<> &IRB, Value *Addr, Value *Shadow, Value *Origin,
  1032. Value *OriginPtr, Align Alignment, bool AsCall) {
  1033. const DataLayout &DL = F.getParent()->getDataLayout();
  1034. const Align OriginAlignment = std::max(kMinOriginAlignment, Alignment);
  1035. unsigned StoreSize = DL.getTypeStoreSize(Shadow->getType());
  1036. Value *ConvertedShadow = convertShadowToScalar(Shadow, IRB);
  1037. if (auto *ConstantShadow = dyn_cast<Constant>(ConvertedShadow)) {
  1038. if (ClCheckConstantShadow && !ConstantShadow->isZeroValue())
  1039. paintOrigin(IRB, updateOrigin(Origin, IRB), OriginPtr, StoreSize,
  1040. OriginAlignment);
  1041. return;
  1042. }
  1043. unsigned TypeSizeInBits = DL.getTypeSizeInBits(ConvertedShadow->getType());
  1044. unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
  1045. if (AsCall && SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) {
  1046. FunctionCallee Fn = MS.MaybeStoreOriginFn[SizeIndex];
  1047. Value *ConvertedShadow2 =
  1048. IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
  1049. CallBase *CB = IRB.CreateCall(
  1050. Fn, {ConvertedShadow2,
  1051. IRB.CreatePointerCast(Addr, IRB.getInt8PtrTy()), Origin});
  1052. CB->addParamAttr(0, Attribute::ZExt);
  1053. CB->addParamAttr(2, Attribute::ZExt);
  1054. } else {
  1055. Value *Cmp = convertToBool(ConvertedShadow, IRB, "_mscmp");
  1056. Instruction *CheckTerm = SplitBlockAndInsertIfThen(
  1057. Cmp, &*IRB.GetInsertPoint(), false, MS.OriginStoreWeights);
  1058. IRBuilder<> IRBNew(CheckTerm);
  1059. paintOrigin(IRBNew, updateOrigin(Origin, IRBNew), OriginPtr, StoreSize,
  1060. OriginAlignment);
  1061. }
  1062. }
  1063. void materializeStores(bool InstrumentWithCalls) {
  1064. for (StoreInst *SI : StoreList) {
  1065. IRBuilder<> IRB(SI);
  1066. Value *Val = SI->getValueOperand();
  1067. Value *Addr = SI->getPointerOperand();
  1068. Value *Shadow = SI->isAtomic() ? getCleanShadow(Val) : getShadow(Val);
  1069. Value *ShadowPtr, *OriginPtr;
  1070. Type *ShadowTy = Shadow->getType();
  1071. const Align Alignment = SI->getAlign();
  1072. const Align OriginAlignment = std::max(kMinOriginAlignment, Alignment);
  1073. std::tie(ShadowPtr, OriginPtr) =
  1074. getShadowOriginPtr(Addr, IRB, ShadowTy, Alignment, /*isStore*/ true);
  1075. StoreInst *NewSI = IRB.CreateAlignedStore(Shadow, ShadowPtr, Alignment);
  1076. LLVM_DEBUG(dbgs() << " STORE: " << *NewSI << "\n");
  1077. (void)NewSI;
  1078. if (SI->isAtomic())
  1079. SI->setOrdering(addReleaseOrdering(SI->getOrdering()));
  1080. if (MS.TrackOrigins && !SI->isAtomic())
  1081. storeOrigin(IRB, Addr, Shadow, getOrigin(Val), OriginPtr,
  1082. OriginAlignment, InstrumentWithCalls);
  1083. }
  1084. }
  1085. /// Helper function to insert a warning at IRB's current insert point.
  1086. void insertWarningFn(IRBuilder<> &IRB, Value *Origin) {
  1087. if (!Origin)
  1088. Origin = (Value *)IRB.getInt32(0);
  1089. assert(Origin->getType()->isIntegerTy());
  1090. IRB.CreateCall(MS.WarningFn, Origin)->setCannotMerge();
  1091. // FIXME: Insert UnreachableInst if !MS.Recover?
  1092. // This may invalidate some of the following checks and needs to be done
  1093. // at the very end.
  1094. }
  1095. void materializeOneCheck(Instruction *OrigIns, Value *Shadow, Value *Origin,
  1096. bool AsCall) {
  1097. IRBuilder<> IRB(OrigIns);
  1098. LLVM_DEBUG(dbgs() << " SHAD0 : " << *Shadow << "\n");
  1099. Value *ConvertedShadow = convertShadowToScalar(Shadow, IRB);
  1100. LLVM_DEBUG(dbgs() << " SHAD1 : " << *ConvertedShadow << "\n");
  1101. if (auto *ConstantShadow = dyn_cast<Constant>(ConvertedShadow)) {
  1102. if (ClCheckConstantShadow && !ConstantShadow->isZeroValue()) {
  1103. insertWarningFn(IRB, Origin);
  1104. }
  1105. return;
  1106. }
  1107. const DataLayout &DL = OrigIns->getModule()->getDataLayout();
  1108. unsigned TypeSizeInBits = DL.getTypeSizeInBits(ConvertedShadow->getType());
  1109. unsigned SizeIndex = TypeSizeToSizeIndex(TypeSizeInBits);
  1110. if (AsCall && SizeIndex < kNumberOfAccessSizes && !MS.CompileKernel) {
  1111. FunctionCallee Fn = MS.MaybeWarningFn[SizeIndex];
  1112. Value *ConvertedShadow2 =
  1113. IRB.CreateZExt(ConvertedShadow, IRB.getIntNTy(8 * (1 << SizeIndex)));
  1114. CallBase *CB = IRB.CreateCall(
  1115. Fn, {ConvertedShadow2,
  1116. MS.TrackOrigins && Origin ? Origin : (Value *)IRB.getInt32(0)});
  1117. CB->addParamAttr(0, Attribute::ZExt);
  1118. CB->addParamAttr(1, Attribute::ZExt);
  1119. } else {
  1120. Value *Cmp = convertToBool(ConvertedShadow, IRB, "_mscmp");
  1121. Instruction *CheckTerm = SplitBlockAndInsertIfThen(
  1122. Cmp, OrigIns,
  1123. /* Unreachable */ !MS.Recover, MS.ColdCallWeights);
  1124. IRB.SetInsertPoint(CheckTerm);
  1125. insertWarningFn(IRB, Origin);
  1126. LLVM_DEBUG(dbgs() << " CHECK: " << *Cmp << "\n");
  1127. }
  1128. }
  1129. void materializeChecks(bool InstrumentWithCalls) {
  1130. for (const auto &ShadowData : InstrumentationList) {
  1131. Instruction *OrigIns = ShadowData.OrigIns;
  1132. Value *Shadow = ShadowData.Shadow;
  1133. Value *Origin = ShadowData.Origin;
  1134. materializeOneCheck(OrigIns, Shadow, Origin, InstrumentWithCalls);
  1135. }
  1136. LLVM_DEBUG(dbgs() << "DONE:\n" << F);
  1137. }
  1138. // Returns the last instruction in the new prologue
  1139. void insertKmsanPrologue(IRBuilder<> &IRB) {
  1140. Value *ContextState = IRB.CreateCall(MS.MsanGetContextStateFn, {});
  1141. Constant *Zero = IRB.getInt32(0);
  1142. MS.ParamTLS = IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
  1143. {Zero, IRB.getInt32(0)}, "param_shadow");
  1144. MS.RetvalTLS = IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
  1145. {Zero, IRB.getInt32(1)}, "retval_shadow");
  1146. MS.VAArgTLS = IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
  1147. {Zero, IRB.getInt32(2)}, "va_arg_shadow");
  1148. MS.VAArgOriginTLS = IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
  1149. {Zero, IRB.getInt32(3)}, "va_arg_origin");
  1150. MS.VAArgOverflowSizeTLS =
  1151. IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
  1152. {Zero, IRB.getInt32(4)}, "va_arg_overflow_size");
  1153. MS.ParamOriginTLS = IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
  1154. {Zero, IRB.getInt32(5)}, "param_origin");
  1155. MS.RetvalOriginTLS =
  1156. IRB.CreateGEP(MS.MsanContextStateTy, ContextState,
  1157. {Zero, IRB.getInt32(6)}, "retval_origin");
  1158. }
  1159. /// Add MemorySanitizer instrumentation to a function.
  1160. bool runOnFunction() {
  1161. // Iterate all BBs in depth-first order and create shadow instructions
  1162. // for all instructions (where applicable).
  1163. // For PHI nodes we create dummy shadow PHIs which will be finalized later.
  1164. for (BasicBlock *BB : depth_first(FnPrologueEnd->getParent()))
  1165. visit(*BB);
  1166. // Finalize PHI nodes.
  1167. for (PHINode *PN : ShadowPHINodes) {
  1168. PHINode *PNS = cast<PHINode>(getShadow(PN));
  1169. PHINode *PNO = MS.TrackOrigins ? cast<PHINode>(getOrigin(PN)) : nullptr;
  1170. size_t NumValues = PN->getNumIncomingValues();
  1171. for (size_t v = 0; v < NumValues; v++) {
  1172. PNS->addIncoming(getShadow(PN, v), PN->getIncomingBlock(v));
  1173. if (PNO) PNO->addIncoming(getOrigin(PN, v), PN->getIncomingBlock(v));
  1174. }
  1175. }
  1176. VAHelper->finalizeInstrumentation();
  1177. // Poison llvm.lifetime.start intrinsics, if we haven't fallen back to
  1178. // instrumenting only allocas.
  1179. if (InstrumentLifetimeStart) {
  1180. for (auto Item : LifetimeStartList) {
  1181. instrumentAlloca(*Item.second, Item.first);
  1182. AllocaSet.erase(Item.second);
  1183. }
  1184. }
  1185. // Poison the allocas for which we didn't instrument the corresponding
  1186. // lifetime intrinsics.
  1187. for (AllocaInst *AI : AllocaSet)
  1188. instrumentAlloca(*AI);
  1189. bool InstrumentWithCalls = ClInstrumentationWithCallThreshold >= 0 &&
  1190. InstrumentationList.size() + StoreList.size() >
  1191. (unsigned)ClInstrumentationWithCallThreshold;
  1192. // Insert shadow value checks.
  1193. materializeChecks(InstrumentWithCalls);
  1194. // Delayed instrumentation of StoreInst.
  1195. // This may not add new address checks.
  1196. materializeStores(InstrumentWithCalls);
  1197. return true;
  1198. }
  1199. /// Compute the shadow type that corresponds to a given Value.
  1200. Type *getShadowTy(Value *V) {
  1201. return getShadowTy(V->getType());
  1202. }
  1203. /// Compute the shadow type that corresponds to a given Type.
  1204. Type *getShadowTy(Type *OrigTy) {
  1205. if (!OrigTy->isSized()) {
  1206. return nullptr;
  1207. }
  1208. // For integer type, shadow is the same as the original type.
  1209. // This may return weird-sized types like i1.
  1210. if (IntegerType *IT = dyn_cast<IntegerType>(OrigTy))
  1211. return IT;
  1212. const DataLayout &DL = F.getParent()->getDataLayout();
  1213. if (VectorType *VT = dyn_cast<VectorType>(OrigTy)) {
  1214. uint32_t EltSize = DL.getTypeSizeInBits(VT->getElementType());
  1215. return FixedVectorType::get(IntegerType::get(*MS.C, EltSize),
  1216. cast<FixedVectorType>(VT)->getNumElements());
  1217. }
  1218. if (ArrayType *AT = dyn_cast<ArrayType>(OrigTy)) {
  1219. return ArrayType::get(getShadowTy(AT->getElementType()),
  1220. AT->getNumElements());
  1221. }
  1222. if (StructType *ST = dyn_cast<StructType>(OrigTy)) {
  1223. SmallVector<Type*, 4> Elements;
  1224. for (unsigned i = 0, n = ST->getNumElements(); i < n; i++)
  1225. Elements.push_back(getShadowTy(ST->getElementType(i)));
  1226. StructType *Res = StructType::get(*MS.C, Elements, ST->isPacked());
  1227. LLVM_DEBUG(dbgs() << "getShadowTy: " << *ST << " ===> " << *Res << "\n");
  1228. return Res;
  1229. }
  1230. uint32_t TypeSize = DL.getTypeSizeInBits(OrigTy);
  1231. return IntegerType::get(*MS.C, TypeSize);
  1232. }
  1233. /// Flatten a vector type.
  1234. Type *getShadowTyNoVec(Type *ty) {
  1235. if (VectorType *vt = dyn_cast<VectorType>(ty))
  1236. return IntegerType::get(*MS.C,
  1237. vt->getPrimitiveSizeInBits().getFixedSize());
  1238. return ty;
  1239. }
  1240. /// Extract combined shadow of struct elements as a bool
  1241. Value *collapseStructShadow(StructType *Struct, Value *Shadow,
  1242. IRBuilder<> &IRB) {
  1243. Value *FalseVal = IRB.getIntN(/* width */ 1, /* value */ 0);
  1244. Value *Aggregator = FalseVal;
  1245. for (unsigned Idx = 0; Idx < Struct->getNumElements(); Idx++) {
  1246. // Combine by ORing together each element's bool shadow
  1247. Value *ShadowItem = IRB.CreateExtractValue(Shadow, Idx);
  1248. Value *ShadowInner = convertShadowToScalar(ShadowItem, IRB);
  1249. Value *ShadowBool = convertToBool(ShadowInner, IRB);
  1250. if (Aggregator != FalseVal)
  1251. Aggregator = IRB.CreateOr(Aggregator, ShadowBool);
  1252. else
  1253. Aggregator = ShadowBool;
  1254. }
  1255. return Aggregator;
  1256. }
  1257. // Extract combined shadow of array elements
  1258. Value *collapseArrayShadow(ArrayType *Array, Value *Shadow,
  1259. IRBuilder<> &IRB) {
  1260. if (!Array->getNumElements())
  1261. return IRB.getIntN(/* width */ 1, /* value */ 0);
  1262. Value *FirstItem = IRB.CreateExtractValue(Shadow, 0);
  1263. Value *Aggregator = convertShadowToScalar(FirstItem, IRB);
  1264. for (unsigned Idx = 1; Idx < Array->getNumElements(); Idx++) {
  1265. Value *ShadowItem = IRB.CreateExtractValue(Shadow, Idx);
  1266. Value *ShadowInner = convertShadowToScalar(ShadowItem, IRB);
  1267. Aggregator = IRB.CreateOr(Aggregator, ShadowInner);
  1268. }
  1269. return Aggregator;
  1270. }
  1271. /// Convert a shadow value to it's flattened variant. The resulting
  1272. /// shadow may not necessarily have the same bit width as the input
  1273. /// value, but it will always be comparable to zero.
  1274. Value *convertShadowToScalar(Value *V, IRBuilder<> &IRB) {
  1275. if (StructType *Struct = dyn_cast<StructType>(V->getType()))
  1276. return collapseStructShadow(Struct, V, IRB);
  1277. if (ArrayType *Array = dyn_cast<ArrayType>(V->getType()))
  1278. return collapseArrayShadow(Array, V, IRB);
  1279. Type *Ty = V->getType();
  1280. Type *NoVecTy = getShadowTyNoVec(Ty);
  1281. if (Ty == NoVecTy) return V;
  1282. return IRB.CreateBitCast(V, NoVecTy);
  1283. }
  1284. // Convert a scalar value to an i1 by comparing with 0
  1285. Value *convertToBool(Value *V, IRBuilder<> &IRB, const Twine &name = "") {
  1286. Type *VTy = V->getType();
  1287. assert(VTy->isIntegerTy());
  1288. if (VTy->getIntegerBitWidth() == 1)
  1289. // Just converting a bool to a bool, so do nothing.
  1290. return V;
  1291. return IRB.CreateICmpNE(V, ConstantInt::get(VTy, 0), name);
  1292. }
  1293. /// Compute the integer shadow offset that corresponds to a given
  1294. /// application address.
  1295. ///
  1296. /// Offset = (Addr & ~AndMask) ^ XorMask
  1297. Value *getShadowPtrOffset(Value *Addr, IRBuilder<> &IRB) {
  1298. Value *OffsetLong = IRB.CreatePointerCast(Addr, MS.IntptrTy);
  1299. uint64_t AndMask = MS.MapParams->AndMask;
  1300. if (AndMask)
  1301. OffsetLong =
  1302. IRB.CreateAnd(OffsetLong, ConstantInt::get(MS.IntptrTy, ~AndMask));
  1303. uint64_t XorMask = MS.MapParams->XorMask;
  1304. if (XorMask)
  1305. OffsetLong =
  1306. IRB.CreateXor(OffsetLong, ConstantInt::get(MS.IntptrTy, XorMask));
  1307. return OffsetLong;
  1308. }
  1309. /// Compute the shadow and origin addresses corresponding to a given
  1310. /// application address.
  1311. ///
  1312. /// Shadow = ShadowBase + Offset
  1313. /// Origin = (OriginBase + Offset) & ~3ULL
  1314. std::pair<Value *, Value *>
  1315. getShadowOriginPtrUserspace(Value *Addr, IRBuilder<> &IRB, Type *ShadowTy,
  1316. MaybeAlign Alignment) {
  1317. Value *ShadowOffset = getShadowPtrOffset(Addr, IRB);
  1318. Value *ShadowLong = ShadowOffset;
  1319. uint64_t ShadowBase = MS.MapParams->ShadowBase;
  1320. if (ShadowBase != 0) {
  1321. ShadowLong =
  1322. IRB.CreateAdd(ShadowLong,
  1323. ConstantInt::get(MS.IntptrTy, ShadowBase));
  1324. }
  1325. Value *ShadowPtr =
  1326. IRB.CreateIntToPtr(ShadowLong, PointerType::get(ShadowTy, 0));
  1327. Value *OriginPtr = nullptr;
  1328. if (MS.TrackOrigins) {
  1329. Value *OriginLong = ShadowOffset;
  1330. uint64_t OriginBase = MS.MapParams->OriginBase;
  1331. if (OriginBase != 0)
  1332. OriginLong = IRB.CreateAdd(OriginLong,
  1333. ConstantInt::get(MS.IntptrTy, OriginBase));
  1334. if (!Alignment || *Alignment < kMinOriginAlignment) {
  1335. uint64_t Mask = kMinOriginAlignment.value() - 1;
  1336. OriginLong =
  1337. IRB.CreateAnd(OriginLong, ConstantInt::get(MS.IntptrTy, ~Mask));
  1338. }
  1339. OriginPtr =
  1340. IRB.CreateIntToPtr(OriginLong, PointerType::get(MS.OriginTy, 0));
  1341. }
  1342. return std::make_pair(ShadowPtr, OriginPtr);
  1343. }
  1344. std::pair<Value *, Value *> getShadowOriginPtrKernel(Value *Addr,
  1345. IRBuilder<> &IRB,
  1346. Type *ShadowTy,
  1347. bool isStore) {
  1348. Value *ShadowOriginPtrs;
  1349. const DataLayout &DL = F.getParent()->getDataLayout();
  1350. int Size = DL.getTypeStoreSize(ShadowTy);
  1351. FunctionCallee Getter = MS.getKmsanShadowOriginAccessFn(isStore, Size);
  1352. Value *AddrCast =
  1353. IRB.CreatePointerCast(Addr, PointerType::get(IRB.getInt8Ty(), 0));
  1354. if (Getter) {
  1355. ShadowOriginPtrs = IRB.CreateCall(Getter, AddrCast);
  1356. } else {
  1357. Value *SizeVal = ConstantInt::get(MS.IntptrTy, Size);
  1358. ShadowOriginPtrs = IRB.CreateCall(isStore ? MS.MsanMetadataPtrForStoreN
  1359. : MS.MsanMetadataPtrForLoadN,
  1360. {AddrCast, SizeVal});
  1361. }
  1362. Value *ShadowPtr = IRB.CreateExtractValue(ShadowOriginPtrs, 0);
  1363. ShadowPtr = IRB.CreatePointerCast(ShadowPtr, PointerType::get(ShadowTy, 0));
  1364. Value *OriginPtr = IRB.CreateExtractValue(ShadowOriginPtrs, 1);
  1365. return std::make_pair(ShadowPtr, OriginPtr);
  1366. }
  1367. std::pair<Value *, Value *> getShadowOriginPtr(Value *Addr, IRBuilder<> &IRB,
  1368. Type *ShadowTy,
  1369. MaybeAlign Alignment,
  1370. bool isStore) {
  1371. if (MS.CompileKernel)
  1372. return getShadowOriginPtrKernel(Addr, IRB, ShadowTy, isStore);
  1373. return getShadowOriginPtrUserspace(Addr, IRB, ShadowTy, Alignment);
  1374. }
  1375. /// Compute the shadow address for a given function argument.
  1376. ///
  1377. /// Shadow = ParamTLS+ArgOffset.
  1378. Value *getShadowPtrForArgument(Value *A, IRBuilder<> &IRB,
  1379. int ArgOffset) {
  1380. Value *Base = IRB.CreatePointerCast(MS.ParamTLS, MS.IntptrTy);
  1381. if (ArgOffset)
  1382. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  1383. return IRB.CreateIntToPtr(Base, PointerType::get(getShadowTy(A), 0),
  1384. "_msarg");
  1385. }
  1386. /// Compute the origin address for a given function argument.
  1387. Value *getOriginPtrForArgument(Value *A, IRBuilder<> &IRB,
  1388. int ArgOffset) {
  1389. if (!MS.TrackOrigins)
  1390. return nullptr;
  1391. Value *Base = IRB.CreatePointerCast(MS.ParamOriginTLS, MS.IntptrTy);
  1392. if (ArgOffset)
  1393. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  1394. return IRB.CreateIntToPtr(Base, PointerType::get(MS.OriginTy, 0),
  1395. "_msarg_o");
  1396. }
  1397. /// Compute the shadow address for a retval.
  1398. Value *getShadowPtrForRetval(Value *A, IRBuilder<> &IRB) {
  1399. return IRB.CreatePointerCast(MS.RetvalTLS,
  1400. PointerType::get(getShadowTy(A), 0),
  1401. "_msret");
  1402. }
  1403. /// Compute the origin address for a retval.
  1404. Value *getOriginPtrForRetval(IRBuilder<> &IRB) {
  1405. // We keep a single origin for the entire retval. Might be too optimistic.
  1406. return MS.RetvalOriginTLS;
  1407. }
  1408. /// Set SV to be the shadow value for V.
  1409. void setShadow(Value *V, Value *SV) {
  1410. assert(!ShadowMap.count(V) && "Values may only have one shadow");
  1411. ShadowMap[V] = PropagateShadow ? SV : getCleanShadow(V);
  1412. }
  1413. /// Set Origin to be the origin value for V.
  1414. void setOrigin(Value *V, Value *Origin) {
  1415. if (!MS.TrackOrigins) return;
  1416. assert(!OriginMap.count(V) && "Values may only have one origin");
  1417. LLVM_DEBUG(dbgs() << "ORIGIN: " << *V << " ==> " << *Origin << "\n");
  1418. OriginMap[V] = Origin;
  1419. }
  1420. Constant *getCleanShadow(Type *OrigTy) {
  1421. Type *ShadowTy = getShadowTy(OrigTy);
  1422. if (!ShadowTy)
  1423. return nullptr;
  1424. return Constant::getNullValue(ShadowTy);
  1425. }
  1426. /// Create a clean shadow value for a given value.
  1427. ///
  1428. /// Clean shadow (all zeroes) means all bits of the value are defined
  1429. /// (initialized).
  1430. Constant *getCleanShadow(Value *V) {
  1431. return getCleanShadow(V->getType());
  1432. }
  1433. /// Create a dirty shadow of a given shadow type.
  1434. Constant *getPoisonedShadow(Type *ShadowTy) {
  1435. assert(ShadowTy);
  1436. if (isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy))
  1437. return Constant::getAllOnesValue(ShadowTy);
  1438. if (ArrayType *AT = dyn_cast<ArrayType>(ShadowTy)) {
  1439. SmallVector<Constant *, 4> Vals(AT->getNumElements(),
  1440. getPoisonedShadow(AT->getElementType()));
  1441. return ConstantArray::get(AT, Vals);
  1442. }
  1443. if (StructType *ST = dyn_cast<StructType>(ShadowTy)) {
  1444. SmallVector<Constant *, 4> Vals;
  1445. for (unsigned i = 0, n = ST->getNumElements(); i < n; i++)
  1446. Vals.push_back(getPoisonedShadow(ST->getElementType(i)));
  1447. return ConstantStruct::get(ST, Vals);
  1448. }
  1449. llvm_unreachable("Unexpected shadow type");
  1450. }
  1451. /// Create a dirty shadow for a given value.
  1452. Constant *getPoisonedShadow(Value *V) {
  1453. Type *ShadowTy = getShadowTy(V);
  1454. if (!ShadowTy)
  1455. return nullptr;
  1456. return getPoisonedShadow(ShadowTy);
  1457. }
  1458. /// Create a clean (zero) origin.
  1459. Value *getCleanOrigin() {
  1460. return Constant::getNullValue(MS.OriginTy);
  1461. }
  1462. /// Get the shadow value for a given Value.
  1463. ///
  1464. /// This function either returns the value set earlier with setShadow,
  1465. /// or extracts if from ParamTLS (for function arguments).
  1466. Value *getShadow(Value *V) {
  1467. if (Instruction *I = dyn_cast<Instruction>(V)) {
  1468. if (!PropagateShadow || I->getMetadata("nosanitize"))
  1469. return getCleanShadow(V);
  1470. // For instructions the shadow is already stored in the map.
  1471. Value *Shadow = ShadowMap[V];
  1472. if (!Shadow) {
  1473. LLVM_DEBUG(dbgs() << "No shadow: " << *V << "\n" << *(I->getParent()));
  1474. (void)I;
  1475. assert(Shadow && "No shadow for a value");
  1476. }
  1477. return Shadow;
  1478. }
  1479. if (UndefValue *U = dyn_cast<UndefValue>(V)) {
  1480. Value *AllOnes = (PropagateShadow && PoisonUndef) ? getPoisonedShadow(V)
  1481. : getCleanShadow(V);
  1482. LLVM_DEBUG(dbgs() << "Undef: " << *U << " ==> " << *AllOnes << "\n");
  1483. (void)U;
  1484. return AllOnes;
  1485. }
  1486. if (Argument *A = dyn_cast<Argument>(V)) {
  1487. // For arguments we compute the shadow on demand and store it in the map.
  1488. Value **ShadowPtr = &ShadowMap[V];
  1489. if (*ShadowPtr)
  1490. return *ShadowPtr;
  1491. Function *F = A->getParent();
  1492. IRBuilder<> EntryIRB(FnPrologueEnd);
  1493. unsigned ArgOffset = 0;
  1494. const DataLayout &DL = F->getParent()->getDataLayout();
  1495. for (auto &FArg : F->args()) {
  1496. if (!FArg.getType()->isSized()) {
  1497. LLVM_DEBUG(dbgs() << "Arg is not sized\n");
  1498. continue;
  1499. }
  1500. unsigned Size = FArg.hasByValAttr()
  1501. ? DL.getTypeAllocSize(FArg.getParamByValType())
  1502. : DL.getTypeAllocSize(FArg.getType());
  1503. if (A == &FArg) {
  1504. bool Overflow = ArgOffset + Size > kParamTLSSize;
  1505. if (FArg.hasByValAttr()) {
  1506. // ByVal pointer itself has clean shadow. We copy the actual
  1507. // argument shadow to the underlying memory.
  1508. // Figure out maximal valid memcpy alignment.
  1509. const Align ArgAlign = DL.getValueOrABITypeAlignment(
  1510. MaybeAlign(FArg.getParamAlignment()), FArg.getParamByValType());
  1511. Value *CpShadowPtr, *CpOriginPtr;
  1512. std::tie(CpShadowPtr, CpOriginPtr) =
  1513. getShadowOriginPtr(V, EntryIRB, EntryIRB.getInt8Ty(), ArgAlign,
  1514. /*isStore*/ true);
  1515. if (!PropagateShadow || Overflow) {
  1516. // ParamTLS overflow.
  1517. EntryIRB.CreateMemSet(
  1518. CpShadowPtr, Constant::getNullValue(EntryIRB.getInt8Ty()),
  1519. Size, ArgAlign);
  1520. } else {
  1521. Value *Base = getShadowPtrForArgument(&FArg, EntryIRB, ArgOffset);
  1522. const Align CopyAlign = std::min(ArgAlign, kShadowTLSAlignment);
  1523. Value *Cpy = EntryIRB.CreateMemCpy(CpShadowPtr, CopyAlign, Base,
  1524. CopyAlign, Size);
  1525. LLVM_DEBUG(dbgs() << " ByValCpy: " << *Cpy << "\n");
  1526. (void)Cpy;
  1527. if (MS.TrackOrigins) {
  1528. Value *OriginPtr =
  1529. getOriginPtrForArgument(&FArg, EntryIRB, ArgOffset);
  1530. // FIXME: OriginSize should be:
  1531. // alignTo(V % kMinOriginAlignment + Size, kMinOriginAlignment)
  1532. unsigned OriginSize = alignTo(Size, kMinOriginAlignment);
  1533. EntryIRB.CreateMemCpy(
  1534. CpOriginPtr,
  1535. /* by getShadowOriginPtr */ kMinOriginAlignment, OriginPtr,
  1536. /* by origin_tls[ArgOffset] */ kMinOriginAlignment,
  1537. OriginSize);
  1538. }
  1539. }
  1540. }
  1541. if (!PropagateShadow || Overflow || FArg.hasByValAttr() ||
  1542. (MS.EagerChecks && FArg.hasAttribute(Attribute::NoUndef))) {
  1543. *ShadowPtr = getCleanShadow(V);
  1544. setOrigin(A, getCleanOrigin());
  1545. } else {
  1546. // Shadow over TLS
  1547. Value *Base = getShadowPtrForArgument(&FArg, EntryIRB, ArgOffset);
  1548. *ShadowPtr = EntryIRB.CreateAlignedLoad(getShadowTy(&FArg), Base,
  1549. kShadowTLSAlignment);
  1550. if (MS.TrackOrigins) {
  1551. Value *OriginPtr =
  1552. getOriginPtrForArgument(&FArg, EntryIRB, ArgOffset);
  1553. setOrigin(A, EntryIRB.CreateLoad(MS.OriginTy, OriginPtr));
  1554. }
  1555. }
  1556. LLVM_DEBUG(dbgs()
  1557. << " ARG: " << FArg << " ==> " << **ShadowPtr << "\n");
  1558. break;
  1559. }
  1560. ArgOffset += alignTo(Size, kShadowTLSAlignment);
  1561. }
  1562. assert(*ShadowPtr && "Could not find shadow for an argument");
  1563. return *ShadowPtr;
  1564. }
  1565. // For everything else the shadow is zero.
  1566. return getCleanShadow(V);
  1567. }
  1568. /// Get the shadow for i-th argument of the instruction I.
  1569. Value *getShadow(Instruction *I, int i) {
  1570. return getShadow(I->getOperand(i));
  1571. }
  1572. /// Get the origin for a value.
  1573. Value *getOrigin(Value *V) {
  1574. if (!MS.TrackOrigins) return nullptr;
  1575. if (!PropagateShadow) return getCleanOrigin();
  1576. if (isa<Constant>(V)) return getCleanOrigin();
  1577. assert((isa<Instruction>(V) || isa<Argument>(V)) &&
  1578. "Unexpected value type in getOrigin()");
  1579. if (Instruction *I = dyn_cast<Instruction>(V)) {
  1580. if (I->getMetadata("nosanitize"))
  1581. return getCleanOrigin();
  1582. }
  1583. Value *Origin = OriginMap[V];
  1584. assert(Origin && "Missing origin");
  1585. return Origin;
  1586. }
  1587. /// Get the origin for i-th argument of the instruction I.
  1588. Value *getOrigin(Instruction *I, int i) {
  1589. return getOrigin(I->getOperand(i));
  1590. }
  1591. /// Remember the place where a shadow check should be inserted.
  1592. ///
  1593. /// This location will be later instrumented with a check that will print a
  1594. /// UMR warning in runtime if the shadow value is not 0.
  1595. void insertShadowCheck(Value *Shadow, Value *Origin, Instruction *OrigIns) {
  1596. assert(Shadow);
  1597. if (!InsertChecks) return;
  1598. #ifndef NDEBUG
  1599. Type *ShadowTy = Shadow->getType();
  1600. assert((isa<IntegerType>(ShadowTy) || isa<VectorType>(ShadowTy) ||
  1601. isa<StructType>(ShadowTy) || isa<ArrayType>(ShadowTy)) &&
  1602. "Can only insert checks for integer, vector, and aggregate shadow "
  1603. "types");
  1604. #endif
  1605. InstrumentationList.push_back(
  1606. ShadowOriginAndInsertPoint(Shadow, Origin, OrigIns));
  1607. }
  1608. /// Remember the place where a shadow check should be inserted.
  1609. ///
  1610. /// This location will be later instrumented with a check that will print a
  1611. /// UMR warning in runtime if the value is not fully defined.
  1612. void insertShadowCheck(Value *Val, Instruction *OrigIns) {
  1613. assert(Val);
  1614. Value *Shadow, *Origin;
  1615. if (ClCheckConstantShadow) {
  1616. Shadow = getShadow(Val);
  1617. if (!Shadow) return;
  1618. Origin = getOrigin(Val);
  1619. } else {
  1620. Shadow = dyn_cast_or_null<Instruction>(getShadow(Val));
  1621. if (!Shadow) return;
  1622. Origin = dyn_cast_or_null<Instruction>(getOrigin(Val));
  1623. }
  1624. insertShadowCheck(Shadow, Origin, OrigIns);
  1625. }
  1626. AtomicOrdering addReleaseOrdering(AtomicOrdering a) {
  1627. switch (a) {
  1628. case AtomicOrdering::NotAtomic:
  1629. return AtomicOrdering::NotAtomic;
  1630. case AtomicOrdering::Unordered:
  1631. case AtomicOrdering::Monotonic:
  1632. case AtomicOrdering::Release:
  1633. return AtomicOrdering::Release;
  1634. case AtomicOrdering::Acquire:
  1635. case AtomicOrdering::AcquireRelease:
  1636. return AtomicOrdering::AcquireRelease;
  1637. case AtomicOrdering::SequentiallyConsistent:
  1638. return AtomicOrdering::SequentiallyConsistent;
  1639. }
  1640. llvm_unreachable("Unknown ordering");
  1641. }
  1642. Value *makeAddReleaseOrderingTable(IRBuilder<> &IRB) {
  1643. constexpr int NumOrderings = (int)AtomicOrderingCABI::seq_cst + 1;
  1644. uint32_t OrderingTable[NumOrderings] = {};
  1645. OrderingTable[(int)AtomicOrderingCABI::relaxed] =
  1646. OrderingTable[(int)AtomicOrderingCABI::release] =
  1647. (int)AtomicOrderingCABI::release;
  1648. OrderingTable[(int)AtomicOrderingCABI::consume] =
  1649. OrderingTable[(int)AtomicOrderingCABI::acquire] =
  1650. OrderingTable[(int)AtomicOrderingCABI::acq_rel] =
  1651. (int)AtomicOrderingCABI::acq_rel;
  1652. OrderingTable[(int)AtomicOrderingCABI::seq_cst] =
  1653. (int)AtomicOrderingCABI::seq_cst;
  1654. return ConstantDataVector::get(IRB.getContext(),
  1655. makeArrayRef(OrderingTable, NumOrderings));
  1656. }
  1657. AtomicOrdering addAcquireOrdering(AtomicOrdering a) {
  1658. switch (a) {
  1659. case AtomicOrdering::NotAtomic:
  1660. return AtomicOrdering::NotAtomic;
  1661. case AtomicOrdering::Unordered:
  1662. case AtomicOrdering::Monotonic:
  1663. case AtomicOrdering::Acquire:
  1664. return AtomicOrdering::Acquire;
  1665. case AtomicOrdering::Release:
  1666. case AtomicOrdering::AcquireRelease:
  1667. return AtomicOrdering::AcquireRelease;
  1668. case AtomicOrdering::SequentiallyConsistent:
  1669. return AtomicOrdering::SequentiallyConsistent;
  1670. }
  1671. llvm_unreachable("Unknown ordering");
  1672. }
  1673. Value *makeAddAcquireOrderingTable(IRBuilder<> &IRB) {
  1674. constexpr int NumOrderings = (int)AtomicOrderingCABI::seq_cst + 1;
  1675. uint32_t OrderingTable[NumOrderings] = {};
  1676. OrderingTable[(int)AtomicOrderingCABI::relaxed] =
  1677. OrderingTable[(int)AtomicOrderingCABI::acquire] =
  1678. OrderingTable[(int)AtomicOrderingCABI::consume] =
  1679. (int)AtomicOrderingCABI::acquire;
  1680. OrderingTable[(int)AtomicOrderingCABI::release] =
  1681. OrderingTable[(int)AtomicOrderingCABI::acq_rel] =
  1682. (int)AtomicOrderingCABI::acq_rel;
  1683. OrderingTable[(int)AtomicOrderingCABI::seq_cst] =
  1684. (int)AtomicOrderingCABI::seq_cst;
  1685. return ConstantDataVector::get(IRB.getContext(),
  1686. makeArrayRef(OrderingTable, NumOrderings));
  1687. }
  1688. // ------------------- Visitors.
  1689. using InstVisitor<MemorySanitizerVisitor>::visit;
  1690. void visit(Instruction &I) {
  1691. if (I.getMetadata("nosanitize"))
  1692. return;
  1693. // Don't want to visit if we're in the prologue
  1694. if (isInPrologue(I))
  1695. return;
  1696. InstVisitor<MemorySanitizerVisitor>::visit(I);
  1697. }
  1698. /// Instrument LoadInst
  1699. ///
  1700. /// Loads the corresponding shadow and (optionally) origin.
  1701. /// Optionally, checks that the load address is fully defined.
  1702. void visitLoadInst(LoadInst &I) {
  1703. assert(I.getType()->isSized() && "Load type must have size");
  1704. assert(!I.getMetadata("nosanitize"));
  1705. IRBuilder<> IRB(I.getNextNode());
  1706. Type *ShadowTy = getShadowTy(&I);
  1707. Value *Addr = I.getPointerOperand();
  1708. Value *ShadowPtr = nullptr, *OriginPtr = nullptr;
  1709. const Align Alignment = assumeAligned(I.getAlignment());
  1710. if (PropagateShadow) {
  1711. std::tie(ShadowPtr, OriginPtr) =
  1712. getShadowOriginPtr(Addr, IRB, ShadowTy, Alignment, /*isStore*/ false);
  1713. setShadow(&I,
  1714. IRB.CreateAlignedLoad(ShadowTy, ShadowPtr, Alignment, "_msld"));
  1715. } else {
  1716. setShadow(&I, getCleanShadow(&I));
  1717. }
  1718. if (ClCheckAccessAddress)
  1719. insertShadowCheck(I.getPointerOperand(), &I);
  1720. if (I.isAtomic())
  1721. I.setOrdering(addAcquireOrdering(I.getOrdering()));
  1722. if (MS.TrackOrigins) {
  1723. if (PropagateShadow) {
  1724. const Align OriginAlignment = std::max(kMinOriginAlignment, Alignment);
  1725. setOrigin(
  1726. &I, IRB.CreateAlignedLoad(MS.OriginTy, OriginPtr, OriginAlignment));
  1727. } else {
  1728. setOrigin(&I, getCleanOrigin());
  1729. }
  1730. }
  1731. }
  1732. /// Instrument StoreInst
  1733. ///
  1734. /// Stores the corresponding shadow and (optionally) origin.
  1735. /// Optionally, checks that the store address is fully defined.
  1736. void visitStoreInst(StoreInst &I) {
  1737. StoreList.push_back(&I);
  1738. if (ClCheckAccessAddress)
  1739. insertShadowCheck(I.getPointerOperand(), &I);
  1740. }
  1741. void handleCASOrRMW(Instruction &I) {
  1742. assert(isa<AtomicRMWInst>(I) || isa<AtomicCmpXchgInst>(I));
  1743. IRBuilder<> IRB(&I);
  1744. Value *Addr = I.getOperand(0);
  1745. Value *Val = I.getOperand(1);
  1746. Value *ShadowPtr = getShadowOriginPtr(Addr, IRB, Val->getType(), Align(1),
  1747. /*isStore*/ true)
  1748. .first;
  1749. if (ClCheckAccessAddress)
  1750. insertShadowCheck(Addr, &I);
  1751. // Only test the conditional argument of cmpxchg instruction.
  1752. // The other argument can potentially be uninitialized, but we can not
  1753. // detect this situation reliably without possible false positives.
  1754. if (isa<AtomicCmpXchgInst>(I))
  1755. insertShadowCheck(Val, &I);
  1756. IRB.CreateStore(getCleanShadow(Val), ShadowPtr);
  1757. setShadow(&I, getCleanShadow(&I));
  1758. setOrigin(&I, getCleanOrigin());
  1759. }
  1760. void visitAtomicRMWInst(AtomicRMWInst &I) {
  1761. handleCASOrRMW(I);
  1762. I.setOrdering(addReleaseOrdering(I.getOrdering()));
  1763. }
  1764. void visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
  1765. handleCASOrRMW(I);
  1766. I.setSuccessOrdering(addReleaseOrdering(I.getSuccessOrdering()));
  1767. }
  1768. // Vector manipulation.
  1769. void visitExtractElementInst(ExtractElementInst &I) {
  1770. insertShadowCheck(I.getOperand(1), &I);
  1771. IRBuilder<> IRB(&I);
  1772. setShadow(&I, IRB.CreateExtractElement(getShadow(&I, 0), I.getOperand(1),
  1773. "_msprop"));
  1774. setOrigin(&I, getOrigin(&I, 0));
  1775. }
  1776. void visitInsertElementInst(InsertElementInst &I) {
  1777. insertShadowCheck(I.getOperand(2), &I);
  1778. IRBuilder<> IRB(&I);
  1779. setShadow(&I, IRB.CreateInsertElement(getShadow(&I, 0), getShadow(&I, 1),
  1780. I.getOperand(2), "_msprop"));
  1781. setOriginForNaryOp(I);
  1782. }
  1783. void visitShuffleVectorInst(ShuffleVectorInst &I) {
  1784. IRBuilder<> IRB(&I);
  1785. setShadow(&I, IRB.CreateShuffleVector(getShadow(&I, 0), getShadow(&I, 1),
  1786. I.getShuffleMask(), "_msprop"));
  1787. setOriginForNaryOp(I);
  1788. }
  1789. // Casts.
  1790. void visitSExtInst(SExtInst &I) {
  1791. IRBuilder<> IRB(&I);
  1792. setShadow(&I, IRB.CreateSExt(getShadow(&I, 0), I.getType(), "_msprop"));
  1793. setOrigin(&I, getOrigin(&I, 0));
  1794. }
  1795. void visitZExtInst(ZExtInst &I) {
  1796. IRBuilder<> IRB(&I);
  1797. setShadow(&I, IRB.CreateZExt(getShadow(&I, 0), I.getType(), "_msprop"));
  1798. setOrigin(&I, getOrigin(&I, 0));
  1799. }
  1800. void visitTruncInst(TruncInst &I) {
  1801. IRBuilder<> IRB(&I);
  1802. setShadow(&I, IRB.CreateTrunc(getShadow(&I, 0), I.getType(), "_msprop"));
  1803. setOrigin(&I, getOrigin(&I, 0));
  1804. }
  1805. void visitBitCastInst(BitCastInst &I) {
  1806. // Special case: if this is the bitcast (there is exactly 1 allowed) between
  1807. // a musttail call and a ret, don't instrument. New instructions are not
  1808. // allowed after a musttail call.
  1809. if (auto *CI = dyn_cast<CallInst>(I.getOperand(0)))
  1810. if (CI->isMustTailCall())
  1811. return;
  1812. IRBuilder<> IRB(&I);
  1813. setShadow(&I, IRB.CreateBitCast(getShadow(&I, 0), getShadowTy(&I)));
  1814. setOrigin(&I, getOrigin(&I, 0));
  1815. }
  1816. void visitPtrToIntInst(PtrToIntInst &I) {
  1817. IRBuilder<> IRB(&I);
  1818. setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false,
  1819. "_msprop_ptrtoint"));
  1820. setOrigin(&I, getOrigin(&I, 0));
  1821. }
  1822. void visitIntToPtrInst(IntToPtrInst &I) {
  1823. IRBuilder<> IRB(&I);
  1824. setShadow(&I, IRB.CreateIntCast(getShadow(&I, 0), getShadowTy(&I), false,
  1825. "_msprop_inttoptr"));
  1826. setOrigin(&I, getOrigin(&I, 0));
  1827. }
  1828. void visitFPToSIInst(CastInst& I) { handleShadowOr(I); }
  1829. void visitFPToUIInst(CastInst& I) { handleShadowOr(I); }
  1830. void visitSIToFPInst(CastInst& I) { handleShadowOr(I); }
  1831. void visitUIToFPInst(CastInst& I) { handleShadowOr(I); }
  1832. void visitFPExtInst(CastInst& I) { handleShadowOr(I); }
  1833. void visitFPTruncInst(CastInst& I) { handleShadowOr(I); }
  1834. /// Propagate shadow for bitwise AND.
  1835. ///
  1836. /// This code is exact, i.e. if, for example, a bit in the left argument
  1837. /// is defined and 0, then neither the value not definedness of the
  1838. /// corresponding bit in B don't affect the resulting shadow.
  1839. void visitAnd(BinaryOperator &I) {
  1840. IRBuilder<> IRB(&I);
  1841. // "And" of 0 and a poisoned value results in unpoisoned value.
  1842. // 1&1 => 1; 0&1 => 0; p&1 => p;
  1843. // 1&0 => 0; 0&0 => 0; p&0 => 0;
  1844. // 1&p => p; 0&p => 0; p&p => p;
  1845. // S = (S1 & S2) | (V1 & S2) | (S1 & V2)
  1846. Value *S1 = getShadow(&I, 0);
  1847. Value *S2 = getShadow(&I, 1);
  1848. Value *V1 = I.getOperand(0);
  1849. Value *V2 = I.getOperand(1);
  1850. if (V1->getType() != S1->getType()) {
  1851. V1 = IRB.CreateIntCast(V1, S1->getType(), false);
  1852. V2 = IRB.CreateIntCast(V2, S2->getType(), false);
  1853. }
  1854. Value *S1S2 = IRB.CreateAnd(S1, S2);
  1855. Value *V1S2 = IRB.CreateAnd(V1, S2);
  1856. Value *S1V2 = IRB.CreateAnd(S1, V2);
  1857. setShadow(&I, IRB.CreateOr({S1S2, V1S2, S1V2}));
  1858. setOriginForNaryOp(I);
  1859. }
  1860. void visitOr(BinaryOperator &I) {
  1861. IRBuilder<> IRB(&I);
  1862. // "Or" of 1 and a poisoned value results in unpoisoned value.
  1863. // 1|1 => 1; 0|1 => 1; p|1 => 1;
  1864. // 1|0 => 1; 0|0 => 0; p|0 => p;
  1865. // 1|p => 1; 0|p => p; p|p => p;
  1866. // S = (S1 & S2) | (~V1 & S2) | (S1 & ~V2)
  1867. Value *S1 = getShadow(&I, 0);
  1868. Value *S2 = getShadow(&I, 1);
  1869. Value *V1 = IRB.CreateNot(I.getOperand(0));
  1870. Value *V2 = IRB.CreateNot(I.getOperand(1));
  1871. if (V1->getType() != S1->getType()) {
  1872. V1 = IRB.CreateIntCast(V1, S1->getType(), false);
  1873. V2 = IRB.CreateIntCast(V2, S2->getType(), false);
  1874. }
  1875. Value *S1S2 = IRB.CreateAnd(S1, S2);
  1876. Value *V1S2 = IRB.CreateAnd(V1, S2);
  1877. Value *S1V2 = IRB.CreateAnd(S1, V2);
  1878. setShadow(&I, IRB.CreateOr({S1S2, V1S2, S1V2}));
  1879. setOriginForNaryOp(I);
  1880. }
  1881. /// Default propagation of shadow and/or origin.
  1882. ///
  1883. /// This class implements the general case of shadow propagation, used in all
  1884. /// cases where we don't know and/or don't care about what the operation
  1885. /// actually does. It converts all input shadow values to a common type
  1886. /// (extending or truncating as necessary), and bitwise OR's them.
  1887. ///
  1888. /// This is much cheaper than inserting checks (i.e. requiring inputs to be
  1889. /// fully initialized), and less prone to false positives.
  1890. ///
  1891. /// This class also implements the general case of origin propagation. For a
  1892. /// Nary operation, result origin is set to the origin of an argument that is
  1893. /// not entirely initialized. If there is more than one such arguments, the
  1894. /// rightmost of them is picked. It does not matter which one is picked if all
  1895. /// arguments are initialized.
  1896. template <bool CombineShadow>
  1897. class Combiner {
  1898. Value *Shadow = nullptr;
  1899. Value *Origin = nullptr;
  1900. IRBuilder<> &IRB;
  1901. MemorySanitizerVisitor *MSV;
  1902. public:
  1903. Combiner(MemorySanitizerVisitor *MSV, IRBuilder<> &IRB)
  1904. : IRB(IRB), MSV(MSV) {}
  1905. /// Add a pair of shadow and origin values to the mix.
  1906. Combiner &Add(Value *OpShadow, Value *OpOrigin) {
  1907. if (CombineShadow) {
  1908. assert(OpShadow);
  1909. if (!Shadow)
  1910. Shadow = OpShadow;
  1911. else {
  1912. OpShadow = MSV->CreateShadowCast(IRB, OpShadow, Shadow->getType());
  1913. Shadow = IRB.CreateOr(Shadow, OpShadow, "_msprop");
  1914. }
  1915. }
  1916. if (MSV->MS.TrackOrigins) {
  1917. assert(OpOrigin);
  1918. if (!Origin) {
  1919. Origin = OpOrigin;
  1920. } else {
  1921. Constant *ConstOrigin = dyn_cast<Constant>(OpOrigin);
  1922. // No point in adding something that might result in 0 origin value.
  1923. if (!ConstOrigin || !ConstOrigin->isNullValue()) {
  1924. Value *FlatShadow = MSV->convertShadowToScalar(OpShadow, IRB);
  1925. Value *Cond =
  1926. IRB.CreateICmpNE(FlatShadow, MSV->getCleanShadow(FlatShadow));
  1927. Origin = IRB.CreateSelect(Cond, OpOrigin, Origin);
  1928. }
  1929. }
  1930. }
  1931. return *this;
  1932. }
  1933. /// Add an application value to the mix.
  1934. Combiner &Add(Value *V) {
  1935. Value *OpShadow = MSV->getShadow(V);
  1936. Value *OpOrigin = MSV->MS.TrackOrigins ? MSV->getOrigin(V) : nullptr;
  1937. return Add(OpShadow, OpOrigin);
  1938. }
  1939. /// Set the current combined values as the given instruction's shadow
  1940. /// and origin.
  1941. void Done(Instruction *I) {
  1942. if (CombineShadow) {
  1943. assert(Shadow);
  1944. Shadow = MSV->CreateShadowCast(IRB, Shadow, MSV->getShadowTy(I));
  1945. MSV->setShadow(I, Shadow);
  1946. }
  1947. if (MSV->MS.TrackOrigins) {
  1948. assert(Origin);
  1949. MSV->setOrigin(I, Origin);
  1950. }
  1951. }
  1952. };
  1953. using ShadowAndOriginCombiner = Combiner<true>;
  1954. using OriginCombiner = Combiner<false>;
  1955. /// Propagate origin for arbitrary operation.
  1956. void setOriginForNaryOp(Instruction &I) {
  1957. if (!MS.TrackOrigins) return;
  1958. IRBuilder<> IRB(&I);
  1959. OriginCombiner OC(this, IRB);
  1960. for (Use &Op : I.operands())
  1961. OC.Add(Op.get());
  1962. OC.Done(&I);
  1963. }
  1964. size_t VectorOrPrimitiveTypeSizeInBits(Type *Ty) {
  1965. assert(!(Ty->isVectorTy() && Ty->getScalarType()->isPointerTy()) &&
  1966. "Vector of pointers is not a valid shadow type");
  1967. return Ty->isVectorTy() ? cast<FixedVectorType>(Ty)->getNumElements() *
  1968. Ty->getScalarSizeInBits()
  1969. : Ty->getPrimitiveSizeInBits();
  1970. }
  1971. /// Cast between two shadow types, extending or truncating as
  1972. /// necessary.
  1973. Value *CreateShadowCast(IRBuilder<> &IRB, Value *V, Type *dstTy,
  1974. bool Signed = false) {
  1975. Type *srcTy = V->getType();
  1976. size_t srcSizeInBits = VectorOrPrimitiveTypeSizeInBits(srcTy);
  1977. size_t dstSizeInBits = VectorOrPrimitiveTypeSizeInBits(dstTy);
  1978. if (srcSizeInBits > 1 && dstSizeInBits == 1)
  1979. return IRB.CreateICmpNE(V, getCleanShadow(V));
  1980. if (dstTy->isIntegerTy() && srcTy->isIntegerTy())
  1981. return IRB.CreateIntCast(V, dstTy, Signed);
  1982. if (dstTy->isVectorTy() && srcTy->isVectorTy() &&
  1983. cast<FixedVectorType>(dstTy)->getNumElements() ==
  1984. cast<FixedVectorType>(srcTy)->getNumElements())
  1985. return IRB.CreateIntCast(V, dstTy, Signed);
  1986. Value *V1 = IRB.CreateBitCast(V, Type::getIntNTy(*MS.C, srcSizeInBits));
  1987. Value *V2 =
  1988. IRB.CreateIntCast(V1, Type::getIntNTy(*MS.C, dstSizeInBits), Signed);
  1989. return IRB.CreateBitCast(V2, dstTy);
  1990. // TODO: handle struct types.
  1991. }
  1992. /// Cast an application value to the type of its own shadow.
  1993. Value *CreateAppToShadowCast(IRBuilder<> &IRB, Value *V) {
  1994. Type *ShadowTy = getShadowTy(V);
  1995. if (V->getType() == ShadowTy)
  1996. return V;
  1997. if (V->getType()->isPtrOrPtrVectorTy())
  1998. return IRB.CreatePtrToInt(V, ShadowTy);
  1999. else
  2000. return IRB.CreateBitCast(V, ShadowTy);
  2001. }
  2002. /// Propagate shadow for arbitrary operation.
  2003. void handleShadowOr(Instruction &I) {
  2004. IRBuilder<> IRB(&I);
  2005. ShadowAndOriginCombiner SC(this, IRB);
  2006. for (Use &Op : I.operands())
  2007. SC.Add(Op.get());
  2008. SC.Done(&I);
  2009. }
  2010. void visitFNeg(UnaryOperator &I) { handleShadowOr(I); }
  2011. // Handle multiplication by constant.
  2012. //
  2013. // Handle a special case of multiplication by constant that may have one or
  2014. // more zeros in the lower bits. This makes corresponding number of lower bits
  2015. // of the result zero as well. We model it by shifting the other operand
  2016. // shadow left by the required number of bits. Effectively, we transform
  2017. // (X * (A * 2**B)) to ((X << B) * A) and instrument (X << B) as (Sx << B).
  2018. // We use multiplication by 2**N instead of shift to cover the case of
  2019. // multiplication by 0, which may occur in some elements of a vector operand.
  2020. void handleMulByConstant(BinaryOperator &I, Constant *ConstArg,
  2021. Value *OtherArg) {
  2022. Constant *ShadowMul;
  2023. Type *Ty = ConstArg->getType();
  2024. if (auto *VTy = dyn_cast<VectorType>(Ty)) {
  2025. unsigned NumElements = cast<FixedVectorType>(VTy)->getNumElements();
  2026. Type *EltTy = VTy->getElementType();
  2027. SmallVector<Constant *, 16> Elements;
  2028. for (unsigned Idx = 0; Idx < NumElements; ++Idx) {
  2029. if (ConstantInt *Elt =
  2030. dyn_cast<ConstantInt>(ConstArg->getAggregateElement(Idx))) {
  2031. const APInt &V = Elt->getValue();
  2032. APInt V2 = APInt(V.getBitWidth(), 1) << V.countTrailingZeros();
  2033. Elements.push_back(ConstantInt::get(EltTy, V2));
  2034. } else {
  2035. Elements.push_back(ConstantInt::get(EltTy, 1));
  2036. }
  2037. }
  2038. ShadowMul = ConstantVector::get(Elements);
  2039. } else {
  2040. if (ConstantInt *Elt = dyn_cast<ConstantInt>(ConstArg)) {
  2041. const APInt &V = Elt->getValue();
  2042. APInt V2 = APInt(V.getBitWidth(), 1) << V.countTrailingZeros();
  2043. ShadowMul = ConstantInt::get(Ty, V2);
  2044. } else {
  2045. ShadowMul = ConstantInt::get(Ty, 1);
  2046. }
  2047. }
  2048. IRBuilder<> IRB(&I);
  2049. setShadow(&I,
  2050. IRB.CreateMul(getShadow(OtherArg), ShadowMul, "msprop_mul_cst"));
  2051. setOrigin(&I, getOrigin(OtherArg));
  2052. }
  2053. void visitMul(BinaryOperator &I) {
  2054. Constant *constOp0 = dyn_cast<Constant>(I.getOperand(0));
  2055. Constant *constOp1 = dyn_cast<Constant>(I.getOperand(1));
  2056. if (constOp0 && !constOp1)
  2057. handleMulByConstant(I, constOp0, I.getOperand(1));
  2058. else if (constOp1 && !constOp0)
  2059. handleMulByConstant(I, constOp1, I.getOperand(0));
  2060. else
  2061. handleShadowOr(I);
  2062. }
  2063. void visitFAdd(BinaryOperator &I) { handleShadowOr(I); }
  2064. void visitFSub(BinaryOperator &I) { handleShadowOr(I); }
  2065. void visitFMul(BinaryOperator &I) { handleShadowOr(I); }
  2066. void visitAdd(BinaryOperator &I) { handleShadowOr(I); }
  2067. void visitSub(BinaryOperator &I) { handleShadowOr(I); }
  2068. void visitXor(BinaryOperator &I) { handleShadowOr(I); }
  2069. void handleIntegerDiv(Instruction &I) {
  2070. IRBuilder<> IRB(&I);
  2071. // Strict on the second argument.
  2072. insertShadowCheck(I.getOperand(1), &I);
  2073. setShadow(&I, getShadow(&I, 0));
  2074. setOrigin(&I, getOrigin(&I, 0));
  2075. }
  2076. void visitUDiv(BinaryOperator &I) { handleIntegerDiv(I); }
  2077. void visitSDiv(BinaryOperator &I) { handleIntegerDiv(I); }
  2078. void visitURem(BinaryOperator &I) { handleIntegerDiv(I); }
  2079. void visitSRem(BinaryOperator &I) { handleIntegerDiv(I); }
  2080. // Floating point division is side-effect free. We can not require that the
  2081. // divisor is fully initialized and must propagate shadow. See PR37523.
  2082. void visitFDiv(BinaryOperator &I) { handleShadowOr(I); }
  2083. void visitFRem(BinaryOperator &I) { handleShadowOr(I); }
  2084. /// Instrument == and != comparisons.
  2085. ///
  2086. /// Sometimes the comparison result is known even if some of the bits of the
  2087. /// arguments are not.
  2088. void handleEqualityComparison(ICmpInst &I) {
  2089. IRBuilder<> IRB(&I);
  2090. Value *A = I.getOperand(0);
  2091. Value *B = I.getOperand(1);
  2092. Value *Sa = getShadow(A);
  2093. Value *Sb = getShadow(B);
  2094. // Get rid of pointers and vectors of pointers.
  2095. // For ints (and vectors of ints), types of A and Sa match,
  2096. // and this is a no-op.
  2097. A = IRB.CreatePointerCast(A, Sa->getType());
  2098. B = IRB.CreatePointerCast(B, Sb->getType());
  2099. // A == B <==> (C = A^B) == 0
  2100. // A != B <==> (C = A^B) != 0
  2101. // Sc = Sa | Sb
  2102. Value *C = IRB.CreateXor(A, B);
  2103. Value *Sc = IRB.CreateOr(Sa, Sb);
  2104. // Now dealing with i = (C == 0) comparison (or C != 0, does not matter now)
  2105. // Result is defined if one of the following is true
  2106. // * there is a defined 1 bit in C
  2107. // * C is fully defined
  2108. // Si = !(C & ~Sc) && Sc
  2109. Value *Zero = Constant::getNullValue(Sc->getType());
  2110. Value *MinusOne = Constant::getAllOnesValue(Sc->getType());
  2111. Value *Si =
  2112. IRB.CreateAnd(IRB.CreateICmpNE(Sc, Zero),
  2113. IRB.CreateICmpEQ(
  2114. IRB.CreateAnd(IRB.CreateXor(Sc, MinusOne), C), Zero));
  2115. Si->setName("_msprop_icmp");
  2116. setShadow(&I, Si);
  2117. setOriginForNaryOp(I);
  2118. }
  2119. /// Build the lowest possible value of V, taking into account V's
  2120. /// uninitialized bits.
  2121. Value *getLowestPossibleValue(IRBuilder<> &IRB, Value *A, Value *Sa,
  2122. bool isSigned) {
  2123. if (isSigned) {
  2124. // Split shadow into sign bit and other bits.
  2125. Value *SaOtherBits = IRB.CreateLShr(IRB.CreateShl(Sa, 1), 1);
  2126. Value *SaSignBit = IRB.CreateXor(Sa, SaOtherBits);
  2127. // Maximise the undefined shadow bit, minimize other undefined bits.
  2128. return
  2129. IRB.CreateOr(IRB.CreateAnd(A, IRB.CreateNot(SaOtherBits)), SaSignBit);
  2130. } else {
  2131. // Minimize undefined bits.
  2132. return IRB.CreateAnd(A, IRB.CreateNot(Sa));
  2133. }
  2134. }
  2135. /// Build the highest possible value of V, taking into account V's
  2136. /// uninitialized bits.
  2137. Value *getHighestPossibleValue(IRBuilder<> &IRB, Value *A, Value *Sa,
  2138. bool isSigned) {
  2139. if (isSigned) {
  2140. // Split shadow into sign bit and other bits.
  2141. Value *SaOtherBits = IRB.CreateLShr(IRB.CreateShl(Sa, 1), 1);
  2142. Value *SaSignBit = IRB.CreateXor(Sa, SaOtherBits);
  2143. // Minimise the undefined shadow bit, maximise other undefined bits.
  2144. return
  2145. IRB.CreateOr(IRB.CreateAnd(A, IRB.CreateNot(SaSignBit)), SaOtherBits);
  2146. } else {
  2147. // Maximize undefined bits.
  2148. return IRB.CreateOr(A, Sa);
  2149. }
  2150. }
  2151. /// Instrument relational comparisons.
  2152. ///
  2153. /// This function does exact shadow propagation for all relational
  2154. /// comparisons of integers, pointers and vectors of those.
  2155. /// FIXME: output seems suboptimal when one of the operands is a constant
  2156. void handleRelationalComparisonExact(ICmpInst &I) {
  2157. IRBuilder<> IRB(&I);
  2158. Value *A = I.getOperand(0);
  2159. Value *B = I.getOperand(1);
  2160. Value *Sa = getShadow(A);
  2161. Value *Sb = getShadow(B);
  2162. // Get rid of pointers and vectors of pointers.
  2163. // For ints (and vectors of ints), types of A and Sa match,
  2164. // and this is a no-op.
  2165. A = IRB.CreatePointerCast(A, Sa->getType());
  2166. B = IRB.CreatePointerCast(B, Sb->getType());
  2167. // Let [a0, a1] be the interval of possible values of A, taking into account
  2168. // its undefined bits. Let [b0, b1] be the interval of possible values of B.
  2169. // Then (A cmp B) is defined iff (a0 cmp b1) == (a1 cmp b0).
  2170. bool IsSigned = I.isSigned();
  2171. Value *S1 = IRB.CreateICmp(I.getPredicate(),
  2172. getLowestPossibleValue(IRB, A, Sa, IsSigned),
  2173. getHighestPossibleValue(IRB, B, Sb, IsSigned));
  2174. Value *S2 = IRB.CreateICmp(I.getPredicate(),
  2175. getHighestPossibleValue(IRB, A, Sa, IsSigned),
  2176. getLowestPossibleValue(IRB, B, Sb, IsSigned));
  2177. Value *Si = IRB.CreateXor(S1, S2);
  2178. setShadow(&I, Si);
  2179. setOriginForNaryOp(I);
  2180. }
  2181. /// Instrument signed relational comparisons.
  2182. ///
  2183. /// Handle sign bit tests: x<0, x>=0, x<=-1, x>-1 by propagating the highest
  2184. /// bit of the shadow. Everything else is delegated to handleShadowOr().
  2185. void handleSignedRelationalComparison(ICmpInst &I) {
  2186. Constant *constOp;
  2187. Value *op = nullptr;
  2188. CmpInst::Predicate pre;
  2189. if ((constOp = dyn_cast<Constant>(I.getOperand(1)))) {
  2190. op = I.getOperand(0);
  2191. pre = I.getPredicate();
  2192. } else if ((constOp = dyn_cast<Constant>(I.getOperand(0)))) {
  2193. op = I.getOperand(1);
  2194. pre = I.getSwappedPredicate();
  2195. } else {
  2196. handleShadowOr(I);
  2197. return;
  2198. }
  2199. if ((constOp->isNullValue() &&
  2200. (pre == CmpInst::ICMP_SLT || pre == CmpInst::ICMP_SGE)) ||
  2201. (constOp->isAllOnesValue() &&
  2202. (pre == CmpInst::ICMP_SGT || pre == CmpInst::ICMP_SLE))) {
  2203. IRBuilder<> IRB(&I);
  2204. Value *Shadow = IRB.CreateICmpSLT(getShadow(op), getCleanShadow(op),
  2205. "_msprop_icmp_s");
  2206. setShadow(&I, Shadow);
  2207. setOrigin(&I, getOrigin(op));
  2208. } else {
  2209. handleShadowOr(I);
  2210. }
  2211. }
  2212. void visitICmpInst(ICmpInst &I) {
  2213. if (!ClHandleICmp) {
  2214. handleShadowOr(I);
  2215. return;
  2216. }
  2217. if (I.isEquality()) {
  2218. handleEqualityComparison(I);
  2219. return;
  2220. }
  2221. assert(I.isRelational());
  2222. if (ClHandleICmpExact) {
  2223. handleRelationalComparisonExact(I);
  2224. return;
  2225. }
  2226. if (I.isSigned()) {
  2227. handleSignedRelationalComparison(I);
  2228. return;
  2229. }
  2230. assert(I.isUnsigned());
  2231. if ((isa<Constant>(I.getOperand(0)) || isa<Constant>(I.getOperand(1)))) {
  2232. handleRelationalComparisonExact(I);
  2233. return;
  2234. }
  2235. handleShadowOr(I);
  2236. }
  2237. void visitFCmpInst(FCmpInst &I) {
  2238. handleShadowOr(I);
  2239. }
  2240. void handleShift(BinaryOperator &I) {
  2241. IRBuilder<> IRB(&I);
  2242. // If any of the S2 bits are poisoned, the whole thing is poisoned.
  2243. // Otherwise perform the same shift on S1.
  2244. Value *S1 = getShadow(&I, 0);
  2245. Value *S2 = getShadow(&I, 1);
  2246. Value *S2Conv = IRB.CreateSExt(IRB.CreateICmpNE(S2, getCleanShadow(S2)),
  2247. S2->getType());
  2248. Value *V2 = I.getOperand(1);
  2249. Value *Shift = IRB.CreateBinOp(I.getOpcode(), S1, V2);
  2250. setShadow(&I, IRB.CreateOr(Shift, S2Conv));
  2251. setOriginForNaryOp(I);
  2252. }
  2253. void visitShl(BinaryOperator &I) { handleShift(I); }
  2254. void visitAShr(BinaryOperator &I) { handleShift(I); }
  2255. void visitLShr(BinaryOperator &I) { handleShift(I); }
  2256. void handleFunnelShift(IntrinsicInst &I) {
  2257. IRBuilder<> IRB(&I);
  2258. // If any of the S2 bits are poisoned, the whole thing is poisoned.
  2259. // Otherwise perform the same shift on S0 and S1.
  2260. Value *S0 = getShadow(&I, 0);
  2261. Value *S1 = getShadow(&I, 1);
  2262. Value *S2 = getShadow(&I, 2);
  2263. Value *S2Conv =
  2264. IRB.CreateSExt(IRB.CreateICmpNE(S2, getCleanShadow(S2)), S2->getType());
  2265. Value *V2 = I.getOperand(2);
  2266. Function *Intrin = Intrinsic::getDeclaration(
  2267. I.getModule(), I.getIntrinsicID(), S2Conv->getType());
  2268. Value *Shift = IRB.CreateCall(Intrin, {S0, S1, V2});
  2269. setShadow(&I, IRB.CreateOr(Shift, S2Conv));
  2270. setOriginForNaryOp(I);
  2271. }
  2272. /// Instrument llvm.memmove
  2273. ///
  2274. /// At this point we don't know if llvm.memmove will be inlined or not.
  2275. /// If we don't instrument it and it gets inlined,
  2276. /// our interceptor will not kick in and we will lose the memmove.
  2277. /// If we instrument the call here, but it does not get inlined,
  2278. /// we will memove the shadow twice: which is bad in case
  2279. /// of overlapping regions. So, we simply lower the intrinsic to a call.
  2280. ///
  2281. /// Similar situation exists for memcpy and memset.
  2282. void visitMemMoveInst(MemMoveInst &I) {
  2283. IRBuilder<> IRB(&I);
  2284. IRB.CreateCall(
  2285. MS.MemmoveFn,
  2286. {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
  2287. IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()),
  2288. IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
  2289. I.eraseFromParent();
  2290. }
  2291. // Similar to memmove: avoid copying shadow twice.
  2292. // This is somewhat unfortunate as it may slowdown small constant memcpys.
  2293. // FIXME: consider doing manual inline for small constant sizes and proper
  2294. // alignment.
  2295. void visitMemCpyInst(MemCpyInst &I) {
  2296. IRBuilder<> IRB(&I);
  2297. IRB.CreateCall(
  2298. MS.MemcpyFn,
  2299. {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
  2300. IRB.CreatePointerCast(I.getArgOperand(1), IRB.getInt8PtrTy()),
  2301. IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
  2302. I.eraseFromParent();
  2303. }
  2304. // Same as memcpy.
  2305. void visitMemSetInst(MemSetInst &I) {
  2306. IRBuilder<> IRB(&I);
  2307. IRB.CreateCall(
  2308. MS.MemsetFn,
  2309. {IRB.CreatePointerCast(I.getArgOperand(0), IRB.getInt8PtrTy()),
  2310. IRB.CreateIntCast(I.getArgOperand(1), IRB.getInt32Ty(), false),
  2311. IRB.CreateIntCast(I.getArgOperand(2), MS.IntptrTy, false)});
  2312. I.eraseFromParent();
  2313. }
  2314. void visitVAStartInst(VAStartInst &I) {
  2315. VAHelper->visitVAStartInst(I);
  2316. }
  2317. void visitVACopyInst(VACopyInst &I) {
  2318. VAHelper->visitVACopyInst(I);
  2319. }
  2320. /// Handle vector store-like intrinsics.
  2321. ///
  2322. /// Instrument intrinsics that look like a simple SIMD store: writes memory,
  2323. /// has 1 pointer argument and 1 vector argument, returns void.
  2324. bool handleVectorStoreIntrinsic(IntrinsicInst &I) {
  2325. IRBuilder<> IRB(&I);
  2326. Value* Addr = I.getArgOperand(0);
  2327. Value *Shadow = getShadow(&I, 1);
  2328. Value *ShadowPtr, *OriginPtr;
  2329. // We don't know the pointer alignment (could be unaligned SSE store!).
  2330. // Have to assume to worst case.
  2331. std::tie(ShadowPtr, OriginPtr) = getShadowOriginPtr(
  2332. Addr, IRB, Shadow->getType(), Align(1), /*isStore*/ true);
  2333. IRB.CreateAlignedStore(Shadow, ShadowPtr, Align(1));
  2334. if (ClCheckAccessAddress)
  2335. insertShadowCheck(Addr, &I);
  2336. // FIXME: factor out common code from materializeStores
  2337. if (MS.TrackOrigins) IRB.CreateStore(getOrigin(&I, 1), OriginPtr);
  2338. return true;
  2339. }
  2340. /// Handle vector load-like intrinsics.
  2341. ///
  2342. /// Instrument intrinsics that look like a simple SIMD load: reads memory,
  2343. /// has 1 pointer argument, returns a vector.
  2344. bool handleVectorLoadIntrinsic(IntrinsicInst &I) {
  2345. IRBuilder<> IRB(&I);
  2346. Value *Addr = I.getArgOperand(0);
  2347. Type *ShadowTy = getShadowTy(&I);
  2348. Value *ShadowPtr = nullptr, *OriginPtr = nullptr;
  2349. if (PropagateShadow) {
  2350. // We don't know the pointer alignment (could be unaligned SSE load!).
  2351. // Have to assume to worst case.
  2352. const Align Alignment = Align(1);
  2353. std::tie(ShadowPtr, OriginPtr) =
  2354. getShadowOriginPtr(Addr, IRB, ShadowTy, Alignment, /*isStore*/ false);
  2355. setShadow(&I,
  2356. IRB.CreateAlignedLoad(ShadowTy, ShadowPtr, Alignment, "_msld"));
  2357. } else {
  2358. setShadow(&I, getCleanShadow(&I));
  2359. }
  2360. if (ClCheckAccessAddress)
  2361. insertShadowCheck(Addr, &I);
  2362. if (MS.TrackOrigins) {
  2363. if (PropagateShadow)
  2364. setOrigin(&I, IRB.CreateLoad(MS.OriginTy, OriginPtr));
  2365. else
  2366. setOrigin(&I, getCleanOrigin());
  2367. }
  2368. return true;
  2369. }
  2370. /// Handle (SIMD arithmetic)-like intrinsics.
  2371. ///
  2372. /// Instrument intrinsics with any number of arguments of the same type,
  2373. /// equal to the return type. The type should be simple (no aggregates or
  2374. /// pointers; vectors are fine).
  2375. /// Caller guarantees that this intrinsic does not access memory.
  2376. bool maybeHandleSimpleNomemIntrinsic(IntrinsicInst &I) {
  2377. Type *RetTy = I.getType();
  2378. if (!(RetTy->isIntOrIntVectorTy() ||
  2379. RetTy->isFPOrFPVectorTy() ||
  2380. RetTy->isX86_MMXTy()))
  2381. return false;
  2382. unsigned NumArgOperands = I.arg_size();
  2383. for (unsigned i = 0; i < NumArgOperands; ++i) {
  2384. Type *Ty = I.getArgOperand(i)->getType();
  2385. if (Ty != RetTy)
  2386. return false;
  2387. }
  2388. IRBuilder<> IRB(&I);
  2389. ShadowAndOriginCombiner SC(this, IRB);
  2390. for (unsigned i = 0; i < NumArgOperands; ++i)
  2391. SC.Add(I.getArgOperand(i));
  2392. SC.Done(&I);
  2393. return true;
  2394. }
  2395. /// Heuristically instrument unknown intrinsics.
  2396. ///
  2397. /// The main purpose of this code is to do something reasonable with all
  2398. /// random intrinsics we might encounter, most importantly - SIMD intrinsics.
  2399. /// We recognize several classes of intrinsics by their argument types and
  2400. /// ModRefBehaviour and apply special instrumentation when we are reasonably
  2401. /// sure that we know what the intrinsic does.
  2402. ///
  2403. /// We special-case intrinsics where this approach fails. See llvm.bswap
  2404. /// handling as an example of that.
  2405. bool handleUnknownIntrinsic(IntrinsicInst &I) {
  2406. unsigned NumArgOperands = I.arg_size();
  2407. if (NumArgOperands == 0)
  2408. return false;
  2409. if (NumArgOperands == 2 &&
  2410. I.getArgOperand(0)->getType()->isPointerTy() &&
  2411. I.getArgOperand(1)->getType()->isVectorTy() &&
  2412. I.getType()->isVoidTy() &&
  2413. !I.onlyReadsMemory()) {
  2414. // This looks like a vector store.
  2415. return handleVectorStoreIntrinsic(I);
  2416. }
  2417. if (NumArgOperands == 1 &&
  2418. I.getArgOperand(0)->getType()->isPointerTy() &&
  2419. I.getType()->isVectorTy() &&
  2420. I.onlyReadsMemory()) {
  2421. // This looks like a vector load.
  2422. return handleVectorLoadIntrinsic(I);
  2423. }
  2424. if (I.doesNotAccessMemory())
  2425. if (maybeHandleSimpleNomemIntrinsic(I))
  2426. return true;
  2427. // FIXME: detect and handle SSE maskstore/maskload
  2428. return false;
  2429. }
  2430. void handleInvariantGroup(IntrinsicInst &I) {
  2431. setShadow(&I, getShadow(&I, 0));
  2432. setOrigin(&I, getOrigin(&I, 0));
  2433. }
  2434. void handleLifetimeStart(IntrinsicInst &I) {
  2435. if (!PoisonStack)
  2436. return;
  2437. AllocaInst *AI = llvm::findAllocaForValue(I.getArgOperand(1));
  2438. if (!AI)
  2439. InstrumentLifetimeStart = false;
  2440. LifetimeStartList.push_back(std::make_pair(&I, AI));
  2441. }
  2442. void handleBswap(IntrinsicInst &I) {
  2443. IRBuilder<> IRB(&I);
  2444. Value *Op = I.getArgOperand(0);
  2445. Type *OpType = Op->getType();
  2446. Function *BswapFunc = Intrinsic::getDeclaration(
  2447. F.getParent(), Intrinsic::bswap, makeArrayRef(&OpType, 1));
  2448. setShadow(&I, IRB.CreateCall(BswapFunc, getShadow(Op)));
  2449. setOrigin(&I, getOrigin(Op));
  2450. }
  2451. // Instrument vector convert intrinsic.
  2452. //
  2453. // This function instruments intrinsics like cvtsi2ss:
  2454. // %Out = int_xxx_cvtyyy(%ConvertOp)
  2455. // or
  2456. // %Out = int_xxx_cvtyyy(%CopyOp, %ConvertOp)
  2457. // Intrinsic converts \p NumUsedElements elements of \p ConvertOp to the same
  2458. // number \p Out elements, and (if has 2 arguments) copies the rest of the
  2459. // elements from \p CopyOp.
  2460. // In most cases conversion involves floating-point value which may trigger a
  2461. // hardware exception when not fully initialized. For this reason we require
  2462. // \p ConvertOp[0:NumUsedElements] to be fully initialized and trap otherwise.
  2463. // We copy the shadow of \p CopyOp[NumUsedElements:] to \p
  2464. // Out[NumUsedElements:]. This means that intrinsics without \p CopyOp always
  2465. // return a fully initialized value.
  2466. void handleVectorConvertIntrinsic(IntrinsicInst &I, int NumUsedElements,
  2467. bool HasRoundingMode = false) {
  2468. IRBuilder<> IRB(&I);
  2469. Value *CopyOp, *ConvertOp;
  2470. assert((!HasRoundingMode ||
  2471. isa<ConstantInt>(I.getArgOperand(I.arg_size() - 1))) &&
  2472. "Invalid rounding mode");
  2473. switch (I.arg_size() - HasRoundingMode) {
  2474. case 2:
  2475. CopyOp = I.getArgOperand(0);
  2476. ConvertOp = I.getArgOperand(1);
  2477. break;
  2478. case 1:
  2479. ConvertOp = I.getArgOperand(0);
  2480. CopyOp = nullptr;
  2481. break;
  2482. default:
  2483. llvm_unreachable("Cvt intrinsic with unsupported number of arguments.");
  2484. }
  2485. // The first *NumUsedElements* elements of ConvertOp are converted to the
  2486. // same number of output elements. The rest of the output is copied from
  2487. // CopyOp, or (if not available) filled with zeroes.
  2488. // Combine shadow for elements of ConvertOp that are used in this operation,
  2489. // and insert a check.
  2490. // FIXME: consider propagating shadow of ConvertOp, at least in the case of
  2491. // int->any conversion.
  2492. Value *ConvertShadow = getShadow(ConvertOp);
  2493. Value *AggShadow = nullptr;
  2494. if (ConvertOp->getType()->isVectorTy()) {
  2495. AggShadow = IRB.CreateExtractElement(
  2496. ConvertShadow, ConstantInt::get(IRB.getInt32Ty(), 0));
  2497. for (int i = 1; i < NumUsedElements; ++i) {
  2498. Value *MoreShadow = IRB.CreateExtractElement(
  2499. ConvertShadow, ConstantInt::get(IRB.getInt32Ty(), i));
  2500. AggShadow = IRB.CreateOr(AggShadow, MoreShadow);
  2501. }
  2502. } else {
  2503. AggShadow = ConvertShadow;
  2504. }
  2505. assert(AggShadow->getType()->isIntegerTy());
  2506. insertShadowCheck(AggShadow, getOrigin(ConvertOp), &I);
  2507. // Build result shadow by zero-filling parts of CopyOp shadow that come from
  2508. // ConvertOp.
  2509. if (CopyOp) {
  2510. assert(CopyOp->getType() == I.getType());
  2511. assert(CopyOp->getType()->isVectorTy());
  2512. Value *ResultShadow = getShadow(CopyOp);
  2513. Type *EltTy = cast<VectorType>(ResultShadow->getType())->getElementType();
  2514. for (int i = 0; i < NumUsedElements; ++i) {
  2515. ResultShadow = IRB.CreateInsertElement(
  2516. ResultShadow, ConstantInt::getNullValue(EltTy),
  2517. ConstantInt::get(IRB.getInt32Ty(), i));
  2518. }
  2519. setShadow(&I, ResultShadow);
  2520. setOrigin(&I, getOrigin(CopyOp));
  2521. } else {
  2522. setShadow(&I, getCleanShadow(&I));
  2523. setOrigin(&I, getCleanOrigin());
  2524. }
  2525. }
  2526. // Given a scalar or vector, extract lower 64 bits (or less), and return all
  2527. // zeroes if it is zero, and all ones otherwise.
  2528. Value *Lower64ShadowExtend(IRBuilder<> &IRB, Value *S, Type *T) {
  2529. if (S->getType()->isVectorTy())
  2530. S = CreateShadowCast(IRB, S, IRB.getInt64Ty(), /* Signed */ true);
  2531. assert(S->getType()->getPrimitiveSizeInBits() <= 64);
  2532. Value *S2 = IRB.CreateICmpNE(S, getCleanShadow(S));
  2533. return CreateShadowCast(IRB, S2, T, /* Signed */ true);
  2534. }
  2535. // Given a vector, extract its first element, and return all
  2536. // zeroes if it is zero, and all ones otherwise.
  2537. Value *LowerElementShadowExtend(IRBuilder<> &IRB, Value *S, Type *T) {
  2538. Value *S1 = IRB.CreateExtractElement(S, (uint64_t)0);
  2539. Value *S2 = IRB.CreateICmpNE(S1, getCleanShadow(S1));
  2540. return CreateShadowCast(IRB, S2, T, /* Signed */ true);
  2541. }
  2542. Value *VariableShadowExtend(IRBuilder<> &IRB, Value *S) {
  2543. Type *T = S->getType();
  2544. assert(T->isVectorTy());
  2545. Value *S2 = IRB.CreateICmpNE(S, getCleanShadow(S));
  2546. return IRB.CreateSExt(S2, T);
  2547. }
  2548. // Instrument vector shift intrinsic.
  2549. //
  2550. // This function instruments intrinsics like int_x86_avx2_psll_w.
  2551. // Intrinsic shifts %In by %ShiftSize bits.
  2552. // %ShiftSize may be a vector. In that case the lower 64 bits determine shift
  2553. // size, and the rest is ignored. Behavior is defined even if shift size is
  2554. // greater than register (or field) width.
  2555. void handleVectorShiftIntrinsic(IntrinsicInst &I, bool Variable) {
  2556. assert(I.arg_size() == 2);
  2557. IRBuilder<> IRB(&I);
  2558. // If any of the S2 bits are poisoned, the whole thing is poisoned.
  2559. // Otherwise perform the same shift on S1.
  2560. Value *S1 = getShadow(&I, 0);
  2561. Value *S2 = getShadow(&I, 1);
  2562. Value *S2Conv = Variable ? VariableShadowExtend(IRB, S2)
  2563. : Lower64ShadowExtend(IRB, S2, getShadowTy(&I));
  2564. Value *V1 = I.getOperand(0);
  2565. Value *V2 = I.getOperand(1);
  2566. Value *Shift = IRB.CreateCall(I.getFunctionType(), I.getCalledOperand(),
  2567. {IRB.CreateBitCast(S1, V1->getType()), V2});
  2568. Shift = IRB.CreateBitCast(Shift, getShadowTy(&I));
  2569. setShadow(&I, IRB.CreateOr(Shift, S2Conv));
  2570. setOriginForNaryOp(I);
  2571. }
  2572. // Get an X86_MMX-sized vector type.
  2573. Type *getMMXVectorTy(unsigned EltSizeInBits) {
  2574. const unsigned X86_MMXSizeInBits = 64;
  2575. assert(EltSizeInBits != 0 && (X86_MMXSizeInBits % EltSizeInBits) == 0 &&
  2576. "Illegal MMX vector element size");
  2577. return FixedVectorType::get(IntegerType::get(*MS.C, EltSizeInBits),
  2578. X86_MMXSizeInBits / EltSizeInBits);
  2579. }
  2580. // Returns a signed counterpart for an (un)signed-saturate-and-pack
  2581. // intrinsic.
  2582. Intrinsic::ID getSignedPackIntrinsic(Intrinsic::ID id) {
  2583. switch (id) {
  2584. case Intrinsic::x86_sse2_packsswb_128:
  2585. case Intrinsic::x86_sse2_packuswb_128:
  2586. return Intrinsic::x86_sse2_packsswb_128;
  2587. case Intrinsic::x86_sse2_packssdw_128:
  2588. case Intrinsic::x86_sse41_packusdw:
  2589. return Intrinsic::x86_sse2_packssdw_128;
  2590. case Intrinsic::x86_avx2_packsswb:
  2591. case Intrinsic::x86_avx2_packuswb:
  2592. return Intrinsic::x86_avx2_packsswb;
  2593. case Intrinsic::x86_avx2_packssdw:
  2594. case Intrinsic::x86_avx2_packusdw:
  2595. return Intrinsic::x86_avx2_packssdw;
  2596. case Intrinsic::x86_mmx_packsswb:
  2597. case Intrinsic::x86_mmx_packuswb:
  2598. return Intrinsic::x86_mmx_packsswb;
  2599. case Intrinsic::x86_mmx_packssdw:
  2600. return Intrinsic::x86_mmx_packssdw;
  2601. default:
  2602. llvm_unreachable("unexpected intrinsic id");
  2603. }
  2604. }
  2605. // Instrument vector pack intrinsic.
  2606. //
  2607. // This function instruments intrinsics like x86_mmx_packsswb, that
  2608. // packs elements of 2 input vectors into half as many bits with saturation.
  2609. // Shadow is propagated with the signed variant of the same intrinsic applied
  2610. // to sext(Sa != zeroinitializer), sext(Sb != zeroinitializer).
  2611. // EltSizeInBits is used only for x86mmx arguments.
  2612. void handleVectorPackIntrinsic(IntrinsicInst &I, unsigned EltSizeInBits = 0) {
  2613. assert(I.arg_size() == 2);
  2614. bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
  2615. IRBuilder<> IRB(&I);
  2616. Value *S1 = getShadow(&I, 0);
  2617. Value *S2 = getShadow(&I, 1);
  2618. assert(isX86_MMX || S1->getType()->isVectorTy());
  2619. // SExt and ICmpNE below must apply to individual elements of input vectors.
  2620. // In case of x86mmx arguments, cast them to appropriate vector types and
  2621. // back.
  2622. Type *T = isX86_MMX ? getMMXVectorTy(EltSizeInBits) : S1->getType();
  2623. if (isX86_MMX) {
  2624. S1 = IRB.CreateBitCast(S1, T);
  2625. S2 = IRB.CreateBitCast(S2, T);
  2626. }
  2627. Value *S1_ext = IRB.CreateSExt(
  2628. IRB.CreateICmpNE(S1, Constant::getNullValue(T)), T);
  2629. Value *S2_ext = IRB.CreateSExt(
  2630. IRB.CreateICmpNE(S2, Constant::getNullValue(T)), T);
  2631. if (isX86_MMX) {
  2632. Type *X86_MMXTy = Type::getX86_MMXTy(*MS.C);
  2633. S1_ext = IRB.CreateBitCast(S1_ext, X86_MMXTy);
  2634. S2_ext = IRB.CreateBitCast(S2_ext, X86_MMXTy);
  2635. }
  2636. Function *ShadowFn = Intrinsic::getDeclaration(
  2637. F.getParent(), getSignedPackIntrinsic(I.getIntrinsicID()));
  2638. Value *S =
  2639. IRB.CreateCall(ShadowFn, {S1_ext, S2_ext}, "_msprop_vector_pack");
  2640. if (isX86_MMX) S = IRB.CreateBitCast(S, getShadowTy(&I));
  2641. setShadow(&I, S);
  2642. setOriginForNaryOp(I);
  2643. }
  2644. // Instrument sum-of-absolute-differences intrinsic.
  2645. void handleVectorSadIntrinsic(IntrinsicInst &I) {
  2646. const unsigned SignificantBitsPerResultElement = 16;
  2647. bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
  2648. Type *ResTy = isX86_MMX ? IntegerType::get(*MS.C, 64) : I.getType();
  2649. unsigned ZeroBitsPerResultElement =
  2650. ResTy->getScalarSizeInBits() - SignificantBitsPerResultElement;
  2651. IRBuilder<> IRB(&I);
  2652. Value *S = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1));
  2653. S = IRB.CreateBitCast(S, ResTy);
  2654. S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)),
  2655. ResTy);
  2656. S = IRB.CreateLShr(S, ZeroBitsPerResultElement);
  2657. S = IRB.CreateBitCast(S, getShadowTy(&I));
  2658. setShadow(&I, S);
  2659. setOriginForNaryOp(I);
  2660. }
  2661. // Instrument multiply-add intrinsic.
  2662. void handleVectorPmaddIntrinsic(IntrinsicInst &I,
  2663. unsigned EltSizeInBits = 0) {
  2664. bool isX86_MMX = I.getOperand(0)->getType()->isX86_MMXTy();
  2665. Type *ResTy = isX86_MMX ? getMMXVectorTy(EltSizeInBits * 2) : I.getType();
  2666. IRBuilder<> IRB(&I);
  2667. Value *S = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1));
  2668. S = IRB.CreateBitCast(S, ResTy);
  2669. S = IRB.CreateSExt(IRB.CreateICmpNE(S, Constant::getNullValue(ResTy)),
  2670. ResTy);
  2671. S = IRB.CreateBitCast(S, getShadowTy(&I));
  2672. setShadow(&I, S);
  2673. setOriginForNaryOp(I);
  2674. }
  2675. // Instrument compare-packed intrinsic.
  2676. // Basically, an or followed by sext(icmp ne 0) to end up with all-zeros or
  2677. // all-ones shadow.
  2678. void handleVectorComparePackedIntrinsic(IntrinsicInst &I) {
  2679. IRBuilder<> IRB(&I);
  2680. Type *ResTy = getShadowTy(&I);
  2681. Value *S0 = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1));
  2682. Value *S = IRB.CreateSExt(
  2683. IRB.CreateICmpNE(S0, Constant::getNullValue(ResTy)), ResTy);
  2684. setShadow(&I, S);
  2685. setOriginForNaryOp(I);
  2686. }
  2687. // Instrument compare-scalar intrinsic.
  2688. // This handles both cmp* intrinsics which return the result in the first
  2689. // element of a vector, and comi* which return the result as i32.
  2690. void handleVectorCompareScalarIntrinsic(IntrinsicInst &I) {
  2691. IRBuilder<> IRB(&I);
  2692. Value *S0 = IRB.CreateOr(getShadow(&I, 0), getShadow(&I, 1));
  2693. Value *S = LowerElementShadowExtend(IRB, S0, getShadowTy(&I));
  2694. setShadow(&I, S);
  2695. setOriginForNaryOp(I);
  2696. }
  2697. // Instrument generic vector reduction intrinsics
  2698. // by ORing together all their fields.
  2699. void handleVectorReduceIntrinsic(IntrinsicInst &I) {
  2700. IRBuilder<> IRB(&I);
  2701. Value *S = IRB.CreateOrReduce(getShadow(&I, 0));
  2702. setShadow(&I, S);
  2703. setOrigin(&I, getOrigin(&I, 0));
  2704. }
  2705. // Instrument vector.reduce.or intrinsic.
  2706. // Valid (non-poisoned) set bits in the operand pull low the
  2707. // corresponding shadow bits.
  2708. void handleVectorReduceOrIntrinsic(IntrinsicInst &I) {
  2709. IRBuilder<> IRB(&I);
  2710. Value *OperandShadow = getShadow(&I, 0);
  2711. Value *OperandUnsetBits = IRB.CreateNot(I.getOperand(0));
  2712. Value *OperandUnsetOrPoison = IRB.CreateOr(OperandUnsetBits, OperandShadow);
  2713. // Bit N is clean if any field's bit N is 1 and unpoison
  2714. Value *OutShadowMask = IRB.CreateAndReduce(OperandUnsetOrPoison);
  2715. // Otherwise, it is clean if every field's bit N is unpoison
  2716. Value *OrShadow = IRB.CreateOrReduce(OperandShadow);
  2717. Value *S = IRB.CreateAnd(OutShadowMask, OrShadow);
  2718. setShadow(&I, S);
  2719. setOrigin(&I, getOrigin(&I, 0));
  2720. }
  2721. // Instrument vector.reduce.and intrinsic.
  2722. // Valid (non-poisoned) unset bits in the operand pull down the
  2723. // corresponding shadow bits.
  2724. void handleVectorReduceAndIntrinsic(IntrinsicInst &I) {
  2725. IRBuilder<> IRB(&I);
  2726. Value *OperandShadow = getShadow(&I, 0);
  2727. Value *OperandSetOrPoison = IRB.CreateOr(I.getOperand(0), OperandShadow);
  2728. // Bit N is clean if any field's bit N is 0 and unpoison
  2729. Value *OutShadowMask = IRB.CreateAndReduce(OperandSetOrPoison);
  2730. // Otherwise, it is clean if every field's bit N is unpoison
  2731. Value *OrShadow = IRB.CreateOrReduce(OperandShadow);
  2732. Value *S = IRB.CreateAnd(OutShadowMask, OrShadow);
  2733. setShadow(&I, S);
  2734. setOrigin(&I, getOrigin(&I, 0));
  2735. }
  2736. void handleStmxcsr(IntrinsicInst &I) {
  2737. IRBuilder<> IRB(&I);
  2738. Value* Addr = I.getArgOperand(0);
  2739. Type *Ty = IRB.getInt32Ty();
  2740. Value *ShadowPtr =
  2741. getShadowOriginPtr(Addr, IRB, Ty, Align(1), /*isStore*/ true).first;
  2742. IRB.CreateStore(getCleanShadow(Ty),
  2743. IRB.CreatePointerCast(ShadowPtr, Ty->getPointerTo()));
  2744. if (ClCheckAccessAddress)
  2745. insertShadowCheck(Addr, &I);
  2746. }
  2747. void handleLdmxcsr(IntrinsicInst &I) {
  2748. if (!InsertChecks) return;
  2749. IRBuilder<> IRB(&I);
  2750. Value *Addr = I.getArgOperand(0);
  2751. Type *Ty = IRB.getInt32Ty();
  2752. const Align Alignment = Align(1);
  2753. Value *ShadowPtr, *OriginPtr;
  2754. std::tie(ShadowPtr, OriginPtr) =
  2755. getShadowOriginPtr(Addr, IRB, Ty, Alignment, /*isStore*/ false);
  2756. if (ClCheckAccessAddress)
  2757. insertShadowCheck(Addr, &I);
  2758. Value *Shadow = IRB.CreateAlignedLoad(Ty, ShadowPtr, Alignment, "_ldmxcsr");
  2759. Value *Origin = MS.TrackOrigins ? IRB.CreateLoad(MS.OriginTy, OriginPtr)
  2760. : getCleanOrigin();
  2761. insertShadowCheck(Shadow, Origin, &I);
  2762. }
  2763. void handleMaskedStore(IntrinsicInst &I) {
  2764. IRBuilder<> IRB(&I);
  2765. Value *V = I.getArgOperand(0);
  2766. Value *Addr = I.getArgOperand(1);
  2767. const Align Alignment(
  2768. cast<ConstantInt>(I.getArgOperand(2))->getZExtValue());
  2769. Value *Mask = I.getArgOperand(3);
  2770. Value *Shadow = getShadow(V);
  2771. Value *ShadowPtr;
  2772. Value *OriginPtr;
  2773. std::tie(ShadowPtr, OriginPtr) = getShadowOriginPtr(
  2774. Addr, IRB, Shadow->getType(), Alignment, /*isStore*/ true);
  2775. if (ClCheckAccessAddress) {
  2776. insertShadowCheck(Addr, &I);
  2777. // Uninitialized mask is kind of like uninitialized address, but not as
  2778. // scary.
  2779. insertShadowCheck(Mask, &I);
  2780. }
  2781. IRB.CreateMaskedStore(Shadow, ShadowPtr, Alignment, Mask);
  2782. if (MS.TrackOrigins) {
  2783. auto &DL = F.getParent()->getDataLayout();
  2784. paintOrigin(IRB, getOrigin(V), OriginPtr,
  2785. DL.getTypeStoreSize(Shadow->getType()),
  2786. std::max(Alignment, kMinOriginAlignment));
  2787. }
  2788. }
  2789. bool handleMaskedLoad(IntrinsicInst &I) {
  2790. IRBuilder<> IRB(&I);
  2791. Value *Addr = I.getArgOperand(0);
  2792. const Align Alignment(
  2793. cast<ConstantInt>(I.getArgOperand(1))->getZExtValue());
  2794. Value *Mask = I.getArgOperand(2);
  2795. Value *PassThru = I.getArgOperand(3);
  2796. Type *ShadowTy = getShadowTy(&I);
  2797. Value *ShadowPtr, *OriginPtr;
  2798. if (PropagateShadow) {
  2799. std::tie(ShadowPtr, OriginPtr) =
  2800. getShadowOriginPtr(Addr, IRB, ShadowTy, Alignment, /*isStore*/ false);
  2801. setShadow(&I, IRB.CreateMaskedLoad(ShadowTy, ShadowPtr, Alignment, Mask,
  2802. getShadow(PassThru), "_msmaskedld"));
  2803. } else {
  2804. setShadow(&I, getCleanShadow(&I));
  2805. }
  2806. if (ClCheckAccessAddress) {
  2807. insertShadowCheck(Addr, &I);
  2808. insertShadowCheck(Mask, &I);
  2809. }
  2810. if (MS.TrackOrigins) {
  2811. if (PropagateShadow) {
  2812. // Choose between PassThru's and the loaded value's origins.
  2813. Value *MaskedPassThruShadow = IRB.CreateAnd(
  2814. getShadow(PassThru), IRB.CreateSExt(IRB.CreateNeg(Mask), ShadowTy));
  2815. Value *Acc = IRB.CreateExtractElement(
  2816. MaskedPassThruShadow, ConstantInt::get(IRB.getInt32Ty(), 0));
  2817. for (int i = 1, N = cast<FixedVectorType>(PassThru->getType())
  2818. ->getNumElements();
  2819. i < N; ++i) {
  2820. Value *More = IRB.CreateExtractElement(
  2821. MaskedPassThruShadow, ConstantInt::get(IRB.getInt32Ty(), i));
  2822. Acc = IRB.CreateOr(Acc, More);
  2823. }
  2824. Value *Origin = IRB.CreateSelect(
  2825. IRB.CreateICmpNE(Acc, Constant::getNullValue(Acc->getType())),
  2826. getOrigin(PassThru), IRB.CreateLoad(MS.OriginTy, OriginPtr));
  2827. setOrigin(&I, Origin);
  2828. } else {
  2829. setOrigin(&I, getCleanOrigin());
  2830. }
  2831. }
  2832. return true;
  2833. }
  2834. // Instrument BMI / BMI2 intrinsics.
  2835. // All of these intrinsics are Z = I(X, Y)
  2836. // where the types of all operands and the result match, and are either i32 or i64.
  2837. // The following instrumentation happens to work for all of them:
  2838. // Sz = I(Sx, Y) | (sext (Sy != 0))
  2839. void handleBmiIntrinsic(IntrinsicInst &I) {
  2840. IRBuilder<> IRB(&I);
  2841. Type *ShadowTy = getShadowTy(&I);
  2842. // If any bit of the mask operand is poisoned, then the whole thing is.
  2843. Value *SMask = getShadow(&I, 1);
  2844. SMask = IRB.CreateSExt(IRB.CreateICmpNE(SMask, getCleanShadow(ShadowTy)),
  2845. ShadowTy);
  2846. // Apply the same intrinsic to the shadow of the first operand.
  2847. Value *S = IRB.CreateCall(I.getCalledFunction(),
  2848. {getShadow(&I, 0), I.getOperand(1)});
  2849. S = IRB.CreateOr(SMask, S);
  2850. setShadow(&I, S);
  2851. setOriginForNaryOp(I);
  2852. }
  2853. SmallVector<int, 8> getPclmulMask(unsigned Width, bool OddElements) {
  2854. SmallVector<int, 8> Mask;
  2855. for (unsigned X = OddElements ? 1 : 0; X < Width; X += 2) {
  2856. Mask.append(2, X);
  2857. }
  2858. return Mask;
  2859. }
  2860. // Instrument pclmul intrinsics.
  2861. // These intrinsics operate either on odd or on even elements of the input
  2862. // vectors, depending on the constant in the 3rd argument, ignoring the rest.
  2863. // Replace the unused elements with copies of the used ones, ex:
  2864. // (0, 1, 2, 3) -> (0, 0, 2, 2) (even case)
  2865. // or
  2866. // (0, 1, 2, 3) -> (1, 1, 3, 3) (odd case)
  2867. // and then apply the usual shadow combining logic.
  2868. void handlePclmulIntrinsic(IntrinsicInst &I) {
  2869. IRBuilder<> IRB(&I);
  2870. unsigned Width =
  2871. cast<FixedVectorType>(I.getArgOperand(0)->getType())->getNumElements();
  2872. assert(isa<ConstantInt>(I.getArgOperand(2)) &&
  2873. "pclmul 3rd operand must be a constant");
  2874. unsigned Imm = cast<ConstantInt>(I.getArgOperand(2))->getZExtValue();
  2875. Value *Shuf0 = IRB.CreateShuffleVector(getShadow(&I, 0),
  2876. getPclmulMask(Width, Imm & 0x01));
  2877. Value *Shuf1 = IRB.CreateShuffleVector(getShadow(&I, 1),
  2878. getPclmulMask(Width, Imm & 0x10));
  2879. ShadowAndOriginCombiner SOC(this, IRB);
  2880. SOC.Add(Shuf0, getOrigin(&I, 0));
  2881. SOC.Add(Shuf1, getOrigin(&I, 1));
  2882. SOC.Done(&I);
  2883. }
  2884. // Instrument _mm_*_sd intrinsics
  2885. void handleUnarySdIntrinsic(IntrinsicInst &I) {
  2886. IRBuilder<> IRB(&I);
  2887. Value *First = getShadow(&I, 0);
  2888. Value *Second = getShadow(&I, 1);
  2889. // High word of first operand, low word of second
  2890. Value *Shadow =
  2891. IRB.CreateShuffleVector(First, Second, llvm::makeArrayRef<int>({2, 1}));
  2892. setShadow(&I, Shadow);
  2893. setOriginForNaryOp(I);
  2894. }
  2895. void handleBinarySdIntrinsic(IntrinsicInst &I) {
  2896. IRBuilder<> IRB(&I);
  2897. Value *First = getShadow(&I, 0);
  2898. Value *Second = getShadow(&I, 1);
  2899. Value *OrShadow = IRB.CreateOr(First, Second);
  2900. // High word of first operand, low word of both OR'd together
  2901. Value *Shadow = IRB.CreateShuffleVector(First, OrShadow,
  2902. llvm::makeArrayRef<int>({2, 1}));
  2903. setShadow(&I, Shadow);
  2904. setOriginForNaryOp(I);
  2905. }
  2906. // Instrument abs intrinsic.
  2907. // handleUnknownIntrinsic can't handle it because of the last
  2908. // is_int_min_poison argument which does not match the result type.
  2909. void handleAbsIntrinsic(IntrinsicInst &I) {
  2910. assert(I.getType()->isIntOrIntVectorTy());
  2911. assert(I.getArgOperand(0)->getType() == I.getType());
  2912. // FIXME: Handle is_int_min_poison.
  2913. IRBuilder<> IRB(&I);
  2914. setShadow(&I, getShadow(&I, 0));
  2915. setOrigin(&I, getOrigin(&I, 0));
  2916. }
  2917. void visitIntrinsicInst(IntrinsicInst &I) {
  2918. switch (I.getIntrinsicID()) {
  2919. case Intrinsic::abs:
  2920. handleAbsIntrinsic(I);
  2921. break;
  2922. case Intrinsic::lifetime_start:
  2923. handleLifetimeStart(I);
  2924. break;
  2925. case Intrinsic::launder_invariant_group:
  2926. case Intrinsic::strip_invariant_group:
  2927. handleInvariantGroup(I);
  2928. break;
  2929. case Intrinsic::bswap:
  2930. handleBswap(I);
  2931. break;
  2932. case Intrinsic::masked_store:
  2933. handleMaskedStore(I);
  2934. break;
  2935. case Intrinsic::masked_load:
  2936. handleMaskedLoad(I);
  2937. break;
  2938. case Intrinsic::vector_reduce_and:
  2939. handleVectorReduceAndIntrinsic(I);
  2940. break;
  2941. case Intrinsic::vector_reduce_or:
  2942. handleVectorReduceOrIntrinsic(I);
  2943. break;
  2944. case Intrinsic::vector_reduce_add:
  2945. case Intrinsic::vector_reduce_xor:
  2946. case Intrinsic::vector_reduce_mul:
  2947. handleVectorReduceIntrinsic(I);
  2948. break;
  2949. case Intrinsic::x86_sse_stmxcsr:
  2950. handleStmxcsr(I);
  2951. break;
  2952. case Intrinsic::x86_sse_ldmxcsr:
  2953. handleLdmxcsr(I);
  2954. break;
  2955. case Intrinsic::x86_avx512_vcvtsd2usi64:
  2956. case Intrinsic::x86_avx512_vcvtsd2usi32:
  2957. case Intrinsic::x86_avx512_vcvtss2usi64:
  2958. case Intrinsic::x86_avx512_vcvtss2usi32:
  2959. case Intrinsic::x86_avx512_cvttss2usi64:
  2960. case Intrinsic::x86_avx512_cvttss2usi:
  2961. case Intrinsic::x86_avx512_cvttsd2usi64:
  2962. case Intrinsic::x86_avx512_cvttsd2usi:
  2963. case Intrinsic::x86_avx512_cvtusi2ss:
  2964. case Intrinsic::x86_avx512_cvtusi642sd:
  2965. case Intrinsic::x86_avx512_cvtusi642ss:
  2966. handleVectorConvertIntrinsic(I, 1, true);
  2967. break;
  2968. case Intrinsic::x86_sse2_cvtsd2si64:
  2969. case Intrinsic::x86_sse2_cvtsd2si:
  2970. case Intrinsic::x86_sse2_cvtsd2ss:
  2971. case Intrinsic::x86_sse2_cvttsd2si64:
  2972. case Intrinsic::x86_sse2_cvttsd2si:
  2973. case Intrinsic::x86_sse_cvtss2si64:
  2974. case Intrinsic::x86_sse_cvtss2si:
  2975. case Intrinsic::x86_sse_cvttss2si64:
  2976. case Intrinsic::x86_sse_cvttss2si:
  2977. handleVectorConvertIntrinsic(I, 1);
  2978. break;
  2979. case Intrinsic::x86_sse_cvtps2pi:
  2980. case Intrinsic::x86_sse_cvttps2pi:
  2981. handleVectorConvertIntrinsic(I, 2);
  2982. break;
  2983. case Intrinsic::x86_avx512_psll_w_512:
  2984. case Intrinsic::x86_avx512_psll_d_512:
  2985. case Intrinsic::x86_avx512_psll_q_512:
  2986. case Intrinsic::x86_avx512_pslli_w_512:
  2987. case Intrinsic::x86_avx512_pslli_d_512:
  2988. case Intrinsic::x86_avx512_pslli_q_512:
  2989. case Intrinsic::x86_avx512_psrl_w_512:
  2990. case Intrinsic::x86_avx512_psrl_d_512:
  2991. case Intrinsic::x86_avx512_psrl_q_512:
  2992. case Intrinsic::x86_avx512_psra_w_512:
  2993. case Intrinsic::x86_avx512_psra_d_512:
  2994. case Intrinsic::x86_avx512_psra_q_512:
  2995. case Intrinsic::x86_avx512_psrli_w_512:
  2996. case Intrinsic::x86_avx512_psrli_d_512:
  2997. case Intrinsic::x86_avx512_psrli_q_512:
  2998. case Intrinsic::x86_avx512_psrai_w_512:
  2999. case Intrinsic::x86_avx512_psrai_d_512:
  3000. case Intrinsic::x86_avx512_psrai_q_512:
  3001. case Intrinsic::x86_avx512_psra_q_256:
  3002. case Intrinsic::x86_avx512_psra_q_128:
  3003. case Intrinsic::x86_avx512_psrai_q_256:
  3004. case Intrinsic::x86_avx512_psrai_q_128:
  3005. case Intrinsic::x86_avx2_psll_w:
  3006. case Intrinsic::x86_avx2_psll_d:
  3007. case Intrinsic::x86_avx2_psll_q:
  3008. case Intrinsic::x86_avx2_pslli_w:
  3009. case Intrinsic::x86_avx2_pslli_d:
  3010. case Intrinsic::x86_avx2_pslli_q:
  3011. case Intrinsic::x86_avx2_psrl_w:
  3012. case Intrinsic::x86_avx2_psrl_d:
  3013. case Intrinsic::x86_avx2_psrl_q:
  3014. case Intrinsic::x86_avx2_psra_w:
  3015. case Intrinsic::x86_avx2_psra_d:
  3016. case Intrinsic::x86_avx2_psrli_w:
  3017. case Intrinsic::x86_avx2_psrli_d:
  3018. case Intrinsic::x86_avx2_psrli_q:
  3019. case Intrinsic::x86_avx2_psrai_w:
  3020. case Intrinsic::x86_avx2_psrai_d:
  3021. case Intrinsic::x86_sse2_psll_w:
  3022. case Intrinsic::x86_sse2_psll_d:
  3023. case Intrinsic::x86_sse2_psll_q:
  3024. case Intrinsic::x86_sse2_pslli_w:
  3025. case Intrinsic::x86_sse2_pslli_d:
  3026. case Intrinsic::x86_sse2_pslli_q:
  3027. case Intrinsic::x86_sse2_psrl_w:
  3028. case Intrinsic::x86_sse2_psrl_d:
  3029. case Intrinsic::x86_sse2_psrl_q:
  3030. case Intrinsic::x86_sse2_psra_w:
  3031. case Intrinsic::x86_sse2_psra_d:
  3032. case Intrinsic::x86_sse2_psrli_w:
  3033. case Intrinsic::x86_sse2_psrli_d:
  3034. case Intrinsic::x86_sse2_psrli_q:
  3035. case Intrinsic::x86_sse2_psrai_w:
  3036. case Intrinsic::x86_sse2_psrai_d:
  3037. case Intrinsic::x86_mmx_psll_w:
  3038. case Intrinsic::x86_mmx_psll_d:
  3039. case Intrinsic::x86_mmx_psll_q:
  3040. case Intrinsic::x86_mmx_pslli_w:
  3041. case Intrinsic::x86_mmx_pslli_d:
  3042. case Intrinsic::x86_mmx_pslli_q:
  3043. case Intrinsic::x86_mmx_psrl_w:
  3044. case Intrinsic::x86_mmx_psrl_d:
  3045. case Intrinsic::x86_mmx_psrl_q:
  3046. case Intrinsic::x86_mmx_psra_w:
  3047. case Intrinsic::x86_mmx_psra_d:
  3048. case Intrinsic::x86_mmx_psrli_w:
  3049. case Intrinsic::x86_mmx_psrli_d:
  3050. case Intrinsic::x86_mmx_psrli_q:
  3051. case Intrinsic::x86_mmx_psrai_w:
  3052. case Intrinsic::x86_mmx_psrai_d:
  3053. handleVectorShiftIntrinsic(I, /* Variable */ false);
  3054. break;
  3055. case Intrinsic::x86_avx2_psllv_d:
  3056. case Intrinsic::x86_avx2_psllv_d_256:
  3057. case Intrinsic::x86_avx512_psllv_d_512:
  3058. case Intrinsic::x86_avx2_psllv_q:
  3059. case Intrinsic::x86_avx2_psllv_q_256:
  3060. case Intrinsic::x86_avx512_psllv_q_512:
  3061. case Intrinsic::x86_avx2_psrlv_d:
  3062. case Intrinsic::x86_avx2_psrlv_d_256:
  3063. case Intrinsic::x86_avx512_psrlv_d_512:
  3064. case Intrinsic::x86_avx2_psrlv_q:
  3065. case Intrinsic::x86_avx2_psrlv_q_256:
  3066. case Intrinsic::x86_avx512_psrlv_q_512:
  3067. case Intrinsic::x86_avx2_psrav_d:
  3068. case Intrinsic::x86_avx2_psrav_d_256:
  3069. case Intrinsic::x86_avx512_psrav_d_512:
  3070. case Intrinsic::x86_avx512_psrav_q_128:
  3071. case Intrinsic::x86_avx512_psrav_q_256:
  3072. case Intrinsic::x86_avx512_psrav_q_512:
  3073. handleVectorShiftIntrinsic(I, /* Variable */ true);
  3074. break;
  3075. case Intrinsic::x86_sse2_packsswb_128:
  3076. case Intrinsic::x86_sse2_packssdw_128:
  3077. case Intrinsic::x86_sse2_packuswb_128:
  3078. case Intrinsic::x86_sse41_packusdw:
  3079. case Intrinsic::x86_avx2_packsswb:
  3080. case Intrinsic::x86_avx2_packssdw:
  3081. case Intrinsic::x86_avx2_packuswb:
  3082. case Intrinsic::x86_avx2_packusdw:
  3083. handleVectorPackIntrinsic(I);
  3084. break;
  3085. case Intrinsic::x86_mmx_packsswb:
  3086. case Intrinsic::x86_mmx_packuswb:
  3087. handleVectorPackIntrinsic(I, 16);
  3088. break;
  3089. case Intrinsic::x86_mmx_packssdw:
  3090. handleVectorPackIntrinsic(I, 32);
  3091. break;
  3092. case Intrinsic::x86_mmx_psad_bw:
  3093. case Intrinsic::x86_sse2_psad_bw:
  3094. case Intrinsic::x86_avx2_psad_bw:
  3095. handleVectorSadIntrinsic(I);
  3096. break;
  3097. case Intrinsic::x86_sse2_pmadd_wd:
  3098. case Intrinsic::x86_avx2_pmadd_wd:
  3099. case Intrinsic::x86_ssse3_pmadd_ub_sw_128:
  3100. case Intrinsic::x86_avx2_pmadd_ub_sw:
  3101. handleVectorPmaddIntrinsic(I);
  3102. break;
  3103. case Intrinsic::x86_ssse3_pmadd_ub_sw:
  3104. handleVectorPmaddIntrinsic(I, 8);
  3105. break;
  3106. case Intrinsic::x86_mmx_pmadd_wd:
  3107. handleVectorPmaddIntrinsic(I, 16);
  3108. break;
  3109. case Intrinsic::x86_sse_cmp_ss:
  3110. case Intrinsic::x86_sse2_cmp_sd:
  3111. case Intrinsic::x86_sse_comieq_ss:
  3112. case Intrinsic::x86_sse_comilt_ss:
  3113. case Intrinsic::x86_sse_comile_ss:
  3114. case Intrinsic::x86_sse_comigt_ss:
  3115. case Intrinsic::x86_sse_comige_ss:
  3116. case Intrinsic::x86_sse_comineq_ss:
  3117. case Intrinsic::x86_sse_ucomieq_ss:
  3118. case Intrinsic::x86_sse_ucomilt_ss:
  3119. case Intrinsic::x86_sse_ucomile_ss:
  3120. case Intrinsic::x86_sse_ucomigt_ss:
  3121. case Intrinsic::x86_sse_ucomige_ss:
  3122. case Intrinsic::x86_sse_ucomineq_ss:
  3123. case Intrinsic::x86_sse2_comieq_sd:
  3124. case Intrinsic::x86_sse2_comilt_sd:
  3125. case Intrinsic::x86_sse2_comile_sd:
  3126. case Intrinsic::x86_sse2_comigt_sd:
  3127. case Intrinsic::x86_sse2_comige_sd:
  3128. case Intrinsic::x86_sse2_comineq_sd:
  3129. case Intrinsic::x86_sse2_ucomieq_sd:
  3130. case Intrinsic::x86_sse2_ucomilt_sd:
  3131. case Intrinsic::x86_sse2_ucomile_sd:
  3132. case Intrinsic::x86_sse2_ucomigt_sd:
  3133. case Intrinsic::x86_sse2_ucomige_sd:
  3134. case Intrinsic::x86_sse2_ucomineq_sd:
  3135. handleVectorCompareScalarIntrinsic(I);
  3136. break;
  3137. case Intrinsic::x86_sse_cmp_ps:
  3138. case Intrinsic::x86_sse2_cmp_pd:
  3139. // FIXME: For x86_avx_cmp_pd_256 and x86_avx_cmp_ps_256 this function
  3140. // generates reasonably looking IR that fails in the backend with "Do not
  3141. // know how to split the result of this operator!".
  3142. handleVectorComparePackedIntrinsic(I);
  3143. break;
  3144. case Intrinsic::x86_bmi_bextr_32:
  3145. case Intrinsic::x86_bmi_bextr_64:
  3146. case Intrinsic::x86_bmi_bzhi_32:
  3147. case Intrinsic::x86_bmi_bzhi_64:
  3148. case Intrinsic::x86_bmi_pdep_32:
  3149. case Intrinsic::x86_bmi_pdep_64:
  3150. case Intrinsic::x86_bmi_pext_32:
  3151. case Intrinsic::x86_bmi_pext_64:
  3152. handleBmiIntrinsic(I);
  3153. break;
  3154. case Intrinsic::x86_pclmulqdq:
  3155. case Intrinsic::x86_pclmulqdq_256:
  3156. case Intrinsic::x86_pclmulqdq_512:
  3157. handlePclmulIntrinsic(I);
  3158. break;
  3159. case Intrinsic::x86_sse41_round_sd:
  3160. handleUnarySdIntrinsic(I);
  3161. break;
  3162. case Intrinsic::x86_sse2_max_sd:
  3163. case Intrinsic::x86_sse2_min_sd:
  3164. handleBinarySdIntrinsic(I);
  3165. break;
  3166. case Intrinsic::fshl:
  3167. case Intrinsic::fshr:
  3168. handleFunnelShift(I);
  3169. break;
  3170. case Intrinsic::is_constant:
  3171. // The result of llvm.is.constant() is always defined.
  3172. setShadow(&I, getCleanShadow(&I));
  3173. setOrigin(&I, getCleanOrigin());
  3174. break;
  3175. default:
  3176. if (!handleUnknownIntrinsic(I))
  3177. visitInstruction(I);
  3178. break;
  3179. }
  3180. }
  3181. void visitLibAtomicLoad(CallBase &CB) {
  3182. // Since we use getNextNode here, we can't have CB terminate the BB.
  3183. assert(isa<CallInst>(CB));
  3184. IRBuilder<> IRB(&CB);
  3185. Value *Size = CB.getArgOperand(0);
  3186. Value *SrcPtr = CB.getArgOperand(1);
  3187. Value *DstPtr = CB.getArgOperand(2);
  3188. Value *Ordering = CB.getArgOperand(3);
  3189. // Convert the call to have at least Acquire ordering to make sure
  3190. // the shadow operations aren't reordered before it.
  3191. Value *NewOrdering =
  3192. IRB.CreateExtractElement(makeAddAcquireOrderingTable(IRB), Ordering);
  3193. CB.setArgOperand(3, NewOrdering);
  3194. IRBuilder<> NextIRB(CB.getNextNode());
  3195. NextIRB.SetCurrentDebugLocation(CB.getDebugLoc());
  3196. Value *SrcShadowPtr, *SrcOriginPtr;
  3197. std::tie(SrcShadowPtr, SrcOriginPtr) =
  3198. getShadowOriginPtr(SrcPtr, NextIRB, NextIRB.getInt8Ty(), Align(1),
  3199. /*isStore*/ false);
  3200. Value *DstShadowPtr =
  3201. getShadowOriginPtr(DstPtr, NextIRB, NextIRB.getInt8Ty(), Align(1),
  3202. /*isStore*/ true)
  3203. .first;
  3204. NextIRB.CreateMemCpy(DstShadowPtr, Align(1), SrcShadowPtr, Align(1), Size);
  3205. if (MS.TrackOrigins) {
  3206. Value *SrcOrigin = NextIRB.CreateAlignedLoad(MS.OriginTy, SrcOriginPtr,
  3207. kMinOriginAlignment);
  3208. Value *NewOrigin = updateOrigin(SrcOrigin, NextIRB);
  3209. NextIRB.CreateCall(MS.MsanSetOriginFn, {DstPtr, Size, NewOrigin});
  3210. }
  3211. }
  3212. void visitLibAtomicStore(CallBase &CB) {
  3213. IRBuilder<> IRB(&CB);
  3214. Value *Size = CB.getArgOperand(0);
  3215. Value *DstPtr = CB.getArgOperand(2);
  3216. Value *Ordering = CB.getArgOperand(3);
  3217. // Convert the call to have at least Release ordering to make sure
  3218. // the shadow operations aren't reordered after it.
  3219. Value *NewOrdering =
  3220. IRB.CreateExtractElement(makeAddReleaseOrderingTable(IRB), Ordering);
  3221. CB.setArgOperand(3, NewOrdering);
  3222. Value *DstShadowPtr =
  3223. getShadowOriginPtr(DstPtr, IRB, IRB.getInt8Ty(), Align(1),
  3224. /*isStore*/ true)
  3225. .first;
  3226. // Atomic store always paints clean shadow/origin. See file header.
  3227. IRB.CreateMemSet(DstShadowPtr, getCleanShadow(IRB.getInt8Ty()), Size,
  3228. Align(1));
  3229. }
  3230. void visitCallBase(CallBase &CB) {
  3231. assert(!CB.getMetadata("nosanitize"));
  3232. if (CB.isInlineAsm()) {
  3233. // For inline asm (either a call to asm function, or callbr instruction),
  3234. // do the usual thing: check argument shadow and mark all outputs as
  3235. // clean. Note that any side effects of the inline asm that are not
  3236. // immediately visible in its constraints are not handled.
  3237. if (ClHandleAsmConservative && MS.CompileKernel)
  3238. visitAsmInstruction(CB);
  3239. else
  3240. visitInstruction(CB);
  3241. return;
  3242. }
  3243. LibFunc LF;
  3244. if (TLI->getLibFunc(CB, LF)) {
  3245. // libatomic.a functions need to have special handling because there isn't
  3246. // a good way to intercept them or compile the library with
  3247. // instrumentation.
  3248. switch (LF) {
  3249. case LibFunc_atomic_load:
  3250. if (!isa<CallInst>(CB)) {
  3251. llvm::errs() << "MSAN -- cannot instrument invoke of libatomic load."
  3252. "Ignoring!\n";
  3253. break;
  3254. }
  3255. visitLibAtomicLoad(CB);
  3256. return;
  3257. case LibFunc_atomic_store:
  3258. visitLibAtomicStore(CB);
  3259. return;
  3260. default:
  3261. break;
  3262. }
  3263. }
  3264. if (auto *Call = dyn_cast<CallInst>(&CB)) {
  3265. assert(!isa<IntrinsicInst>(Call) && "intrinsics are handled elsewhere");
  3266. // We are going to insert code that relies on the fact that the callee
  3267. // will become a non-readonly function after it is instrumented by us. To
  3268. // prevent this code from being optimized out, mark that function
  3269. // non-readonly in advance.
  3270. AttributeMask B;
  3271. B.addAttribute(Attribute::ReadOnly)
  3272. .addAttribute(Attribute::ReadNone)
  3273. .addAttribute(Attribute::WriteOnly)
  3274. .addAttribute(Attribute::ArgMemOnly)
  3275. .addAttribute(Attribute::Speculatable);
  3276. Call->removeFnAttrs(B);
  3277. if (Function *Func = Call->getCalledFunction()) {
  3278. Func->removeFnAttrs(B);
  3279. }
  3280. maybeMarkSanitizerLibraryCallNoBuiltin(Call, TLI);
  3281. }
  3282. IRBuilder<> IRB(&CB);
  3283. bool MayCheckCall = MS.EagerChecks;
  3284. if (Function *Func = CB.getCalledFunction()) {
  3285. // __sanitizer_unaligned_{load,store} functions may be called by users
  3286. // and always expects shadows in the TLS. So don't check them.
  3287. MayCheckCall &= !Func->getName().startswith("__sanitizer_unaligned_");
  3288. }
  3289. unsigned ArgOffset = 0;
  3290. LLVM_DEBUG(dbgs() << " CallSite: " << CB << "\n");
  3291. for (auto ArgIt = CB.arg_begin(), End = CB.arg_end(); ArgIt != End;
  3292. ++ArgIt) {
  3293. Value *A = *ArgIt;
  3294. unsigned i = ArgIt - CB.arg_begin();
  3295. if (!A->getType()->isSized()) {
  3296. LLVM_DEBUG(dbgs() << "Arg " << i << " is not sized: " << CB << "\n");
  3297. continue;
  3298. }
  3299. unsigned Size = 0;
  3300. const DataLayout &DL = F.getParent()->getDataLayout();
  3301. bool ByVal = CB.paramHasAttr(i, Attribute::ByVal);
  3302. bool NoUndef = CB.paramHasAttr(i, Attribute::NoUndef);
  3303. bool EagerCheck = MayCheckCall && !ByVal && NoUndef;
  3304. if (EagerCheck) {
  3305. insertShadowCheck(A, &CB);
  3306. Size = DL.getTypeAllocSize(A->getType());
  3307. } else {
  3308. Value *Store = nullptr;
  3309. // Compute the Shadow for arg even if it is ByVal, because
  3310. // in that case getShadow() will copy the actual arg shadow to
  3311. // __msan_param_tls.
  3312. Value *ArgShadow = getShadow(A);
  3313. Value *ArgShadowBase = getShadowPtrForArgument(A, IRB, ArgOffset);
  3314. LLVM_DEBUG(dbgs() << " Arg#" << i << ": " << *A
  3315. << " Shadow: " << *ArgShadow << "\n");
  3316. if (ByVal) {
  3317. // ByVal requires some special handling as it's too big for a single
  3318. // load
  3319. assert(A->getType()->isPointerTy() &&
  3320. "ByVal argument is not a pointer!");
  3321. Size = DL.getTypeAllocSize(CB.getParamByValType(i));
  3322. if (ArgOffset + Size > kParamTLSSize)
  3323. break;
  3324. const MaybeAlign ParamAlignment(CB.getParamAlign(i));
  3325. MaybeAlign Alignment = llvm::None;
  3326. if (ParamAlignment)
  3327. Alignment = std::min(*ParamAlignment, kShadowTLSAlignment);
  3328. Value *AShadowPtr, *AOriginPtr;
  3329. std::tie(AShadowPtr, AOriginPtr) =
  3330. getShadowOriginPtr(A, IRB, IRB.getInt8Ty(), Alignment,
  3331. /*isStore*/ false);
  3332. if (!PropagateShadow) {
  3333. Store = IRB.CreateMemSet(ArgShadowBase,
  3334. Constant::getNullValue(IRB.getInt8Ty()),
  3335. Size, Alignment);
  3336. } else {
  3337. Store = IRB.CreateMemCpy(ArgShadowBase, Alignment, AShadowPtr,
  3338. Alignment, Size);
  3339. if (MS.TrackOrigins) {
  3340. Value *ArgOriginBase = getOriginPtrForArgument(A, IRB, ArgOffset);
  3341. // FIXME: OriginSize should be:
  3342. // alignTo(A % kMinOriginAlignment + Size, kMinOriginAlignment)
  3343. unsigned OriginSize = alignTo(Size, kMinOriginAlignment);
  3344. IRB.CreateMemCpy(
  3345. ArgOriginBase,
  3346. /* by origin_tls[ArgOffset] */ kMinOriginAlignment,
  3347. AOriginPtr,
  3348. /* by getShadowOriginPtr */ kMinOriginAlignment, OriginSize);
  3349. }
  3350. }
  3351. } else {
  3352. // Any other parameters mean we need bit-grained tracking of uninit
  3353. // data
  3354. Size = DL.getTypeAllocSize(A->getType());
  3355. if (ArgOffset + Size > kParamTLSSize)
  3356. break;
  3357. Store = IRB.CreateAlignedStore(ArgShadow, ArgShadowBase,
  3358. kShadowTLSAlignment);
  3359. Constant *Cst = dyn_cast<Constant>(ArgShadow);
  3360. if (MS.TrackOrigins && !(Cst && Cst->isNullValue())) {
  3361. IRB.CreateStore(getOrigin(A),
  3362. getOriginPtrForArgument(A, IRB, ArgOffset));
  3363. }
  3364. }
  3365. (void)Store;
  3366. assert(Store != nullptr);
  3367. LLVM_DEBUG(dbgs() << " Param:" << *Store << "\n");
  3368. }
  3369. assert(Size != 0);
  3370. ArgOffset += alignTo(Size, kShadowTLSAlignment);
  3371. }
  3372. LLVM_DEBUG(dbgs() << " done with call args\n");
  3373. FunctionType *FT = CB.getFunctionType();
  3374. if (FT->isVarArg()) {
  3375. VAHelper->visitCallBase(CB, IRB);
  3376. }
  3377. // Now, get the shadow for the RetVal.
  3378. if (!CB.getType()->isSized())
  3379. return;
  3380. // Don't emit the epilogue for musttail call returns.
  3381. if (isa<CallInst>(CB) && cast<CallInst>(CB).isMustTailCall())
  3382. return;
  3383. if (MayCheckCall && CB.hasRetAttr(Attribute::NoUndef)) {
  3384. setShadow(&CB, getCleanShadow(&CB));
  3385. setOrigin(&CB, getCleanOrigin());
  3386. return;
  3387. }
  3388. IRBuilder<> IRBBefore(&CB);
  3389. // Until we have full dynamic coverage, make sure the retval shadow is 0.
  3390. Value *Base = getShadowPtrForRetval(&CB, IRBBefore);
  3391. IRBBefore.CreateAlignedStore(getCleanShadow(&CB), Base,
  3392. kShadowTLSAlignment);
  3393. BasicBlock::iterator NextInsn;
  3394. if (isa<CallInst>(CB)) {
  3395. NextInsn = ++CB.getIterator();
  3396. assert(NextInsn != CB.getParent()->end());
  3397. } else {
  3398. BasicBlock *NormalDest = cast<InvokeInst>(CB).getNormalDest();
  3399. if (!NormalDest->getSinglePredecessor()) {
  3400. // FIXME: this case is tricky, so we are just conservative here.
  3401. // Perhaps we need to split the edge between this BB and NormalDest,
  3402. // but a naive attempt to use SplitEdge leads to a crash.
  3403. setShadow(&CB, getCleanShadow(&CB));
  3404. setOrigin(&CB, getCleanOrigin());
  3405. return;
  3406. }
  3407. // FIXME: NextInsn is likely in a basic block that has not been visited yet.
  3408. // Anything inserted there will be instrumented by MSan later!
  3409. NextInsn = NormalDest->getFirstInsertionPt();
  3410. assert(NextInsn != NormalDest->end() &&
  3411. "Could not find insertion point for retval shadow load");
  3412. }
  3413. IRBuilder<> IRBAfter(&*NextInsn);
  3414. Value *RetvalShadow = IRBAfter.CreateAlignedLoad(
  3415. getShadowTy(&CB), getShadowPtrForRetval(&CB, IRBAfter),
  3416. kShadowTLSAlignment, "_msret");
  3417. setShadow(&CB, RetvalShadow);
  3418. if (MS.TrackOrigins)
  3419. setOrigin(&CB, IRBAfter.CreateLoad(MS.OriginTy,
  3420. getOriginPtrForRetval(IRBAfter)));
  3421. }
  3422. bool isAMustTailRetVal(Value *RetVal) {
  3423. if (auto *I = dyn_cast<BitCastInst>(RetVal)) {
  3424. RetVal = I->getOperand(0);
  3425. }
  3426. if (auto *I = dyn_cast<CallInst>(RetVal)) {
  3427. return I->isMustTailCall();
  3428. }
  3429. return false;
  3430. }
  3431. void visitReturnInst(ReturnInst &I) {
  3432. IRBuilder<> IRB(&I);
  3433. Value *RetVal = I.getReturnValue();
  3434. if (!RetVal) return;
  3435. // Don't emit the epilogue for musttail call returns.
  3436. if (isAMustTailRetVal(RetVal)) return;
  3437. Value *ShadowPtr = getShadowPtrForRetval(RetVal, IRB);
  3438. bool HasNoUndef =
  3439. F.hasRetAttribute(Attribute::NoUndef);
  3440. bool StoreShadow = !(MS.EagerChecks && HasNoUndef);
  3441. // FIXME: Consider using SpecialCaseList to specify a list of functions that
  3442. // must always return fully initialized values. For now, we hardcode "main".
  3443. bool EagerCheck = (MS.EagerChecks && HasNoUndef) || (F.getName() == "main");
  3444. Value *Shadow = getShadow(RetVal);
  3445. bool StoreOrigin = true;
  3446. if (EagerCheck) {
  3447. insertShadowCheck(RetVal, &I);
  3448. Shadow = getCleanShadow(RetVal);
  3449. StoreOrigin = false;
  3450. }
  3451. // The caller may still expect information passed over TLS if we pass our
  3452. // check
  3453. if (StoreShadow) {
  3454. IRB.CreateAlignedStore(Shadow, ShadowPtr, kShadowTLSAlignment);
  3455. if (MS.TrackOrigins && StoreOrigin)
  3456. IRB.CreateStore(getOrigin(RetVal), getOriginPtrForRetval(IRB));
  3457. }
  3458. }
  3459. void visitPHINode(PHINode &I) {
  3460. IRBuilder<> IRB(&I);
  3461. if (!PropagateShadow) {
  3462. setShadow(&I, getCleanShadow(&I));
  3463. setOrigin(&I, getCleanOrigin());
  3464. return;
  3465. }
  3466. ShadowPHINodes.push_back(&I);
  3467. setShadow(&I, IRB.CreatePHI(getShadowTy(&I), I.getNumIncomingValues(),
  3468. "_msphi_s"));
  3469. if (MS.TrackOrigins)
  3470. setOrigin(&I, IRB.CreatePHI(MS.OriginTy, I.getNumIncomingValues(),
  3471. "_msphi_o"));
  3472. }
  3473. Value *getLocalVarDescription(AllocaInst &I) {
  3474. SmallString<2048> StackDescriptionStorage;
  3475. raw_svector_ostream StackDescription(StackDescriptionStorage);
  3476. // We create a string with a description of the stack allocation and
  3477. // pass it into __msan_set_alloca_origin.
  3478. // It will be printed by the run-time if stack-originated UMR is found.
  3479. // The first 4 bytes of the string are set to '----' and will be replaced
  3480. // by __msan_va_arg_overflow_size_tls at the first call.
  3481. StackDescription << "----" << I.getName() << "@" << F.getName();
  3482. return createPrivateNonConstGlobalForString(*F.getParent(),
  3483. StackDescription.str());
  3484. }
  3485. void poisonAllocaUserspace(AllocaInst &I, IRBuilder<> &IRB, Value *Len) {
  3486. if (PoisonStack && ClPoisonStackWithCall) {
  3487. IRB.CreateCall(MS.MsanPoisonStackFn,
  3488. {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len});
  3489. } else {
  3490. Value *ShadowBase, *OriginBase;
  3491. std::tie(ShadowBase, OriginBase) = getShadowOriginPtr(
  3492. &I, IRB, IRB.getInt8Ty(), Align(1), /*isStore*/ true);
  3493. Value *PoisonValue = IRB.getInt8(PoisonStack ? ClPoisonStackPattern : 0);
  3494. IRB.CreateMemSet(ShadowBase, PoisonValue, Len, I.getAlign());
  3495. }
  3496. if (PoisonStack && MS.TrackOrigins) {
  3497. Value *Descr = getLocalVarDescription(I);
  3498. IRB.CreateCall(MS.MsanSetAllocaOrigin4Fn,
  3499. {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len,
  3500. IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy()),
  3501. IRB.CreatePointerCast(&F, MS.IntptrTy)});
  3502. }
  3503. }
  3504. void poisonAllocaKmsan(AllocaInst &I, IRBuilder<> &IRB, Value *Len) {
  3505. Value *Descr = getLocalVarDescription(I);
  3506. if (PoisonStack) {
  3507. IRB.CreateCall(MS.MsanPoisonAllocaFn,
  3508. {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len,
  3509. IRB.CreatePointerCast(Descr, IRB.getInt8PtrTy())});
  3510. } else {
  3511. IRB.CreateCall(MS.MsanUnpoisonAllocaFn,
  3512. {IRB.CreatePointerCast(&I, IRB.getInt8PtrTy()), Len});
  3513. }
  3514. }
  3515. void instrumentAlloca(AllocaInst &I, Instruction *InsPoint = nullptr) {
  3516. if (!InsPoint)
  3517. InsPoint = &I;
  3518. IRBuilder<> IRB(InsPoint->getNextNode());
  3519. const DataLayout &DL = F.getParent()->getDataLayout();
  3520. uint64_t TypeSize = DL.getTypeAllocSize(I.getAllocatedType());
  3521. Value *Len = ConstantInt::get(MS.IntptrTy, TypeSize);
  3522. if (I.isArrayAllocation())
  3523. Len = IRB.CreateMul(Len, I.getArraySize());
  3524. if (MS.CompileKernel)
  3525. poisonAllocaKmsan(I, IRB, Len);
  3526. else
  3527. poisonAllocaUserspace(I, IRB, Len);
  3528. }
  3529. void visitAllocaInst(AllocaInst &I) {
  3530. setShadow(&I, getCleanShadow(&I));
  3531. setOrigin(&I, getCleanOrigin());
  3532. // We'll get to this alloca later unless it's poisoned at the corresponding
  3533. // llvm.lifetime.start.
  3534. AllocaSet.insert(&I);
  3535. }
  3536. void visitSelectInst(SelectInst& I) {
  3537. IRBuilder<> IRB(&I);
  3538. // a = select b, c, d
  3539. Value *B = I.getCondition();
  3540. Value *C = I.getTrueValue();
  3541. Value *D = I.getFalseValue();
  3542. Value *Sb = getShadow(B);
  3543. Value *Sc = getShadow(C);
  3544. Value *Sd = getShadow(D);
  3545. // Result shadow if condition shadow is 0.
  3546. Value *Sa0 = IRB.CreateSelect(B, Sc, Sd);
  3547. Value *Sa1;
  3548. if (I.getType()->isAggregateType()) {
  3549. // To avoid "sign extending" i1 to an arbitrary aggregate type, we just do
  3550. // an extra "select". This results in much more compact IR.
  3551. // Sa = select Sb, poisoned, (select b, Sc, Sd)
  3552. Sa1 = getPoisonedShadow(getShadowTy(I.getType()));
  3553. } else {
  3554. // Sa = select Sb, [ (c^d) | Sc | Sd ], [ b ? Sc : Sd ]
  3555. // If Sb (condition is poisoned), look for bits in c and d that are equal
  3556. // and both unpoisoned.
  3557. // If !Sb (condition is unpoisoned), simply pick one of Sc and Sd.
  3558. // Cast arguments to shadow-compatible type.
  3559. C = CreateAppToShadowCast(IRB, C);
  3560. D = CreateAppToShadowCast(IRB, D);
  3561. // Result shadow if condition shadow is 1.
  3562. Sa1 = IRB.CreateOr({IRB.CreateXor(C, D), Sc, Sd});
  3563. }
  3564. Value *Sa = IRB.CreateSelect(Sb, Sa1, Sa0, "_msprop_select");
  3565. setShadow(&I, Sa);
  3566. if (MS.TrackOrigins) {
  3567. // Origins are always i32, so any vector conditions must be flattened.
  3568. // FIXME: consider tracking vector origins for app vectors?
  3569. if (B->getType()->isVectorTy()) {
  3570. Type *FlatTy = getShadowTyNoVec(B->getType());
  3571. B = IRB.CreateICmpNE(IRB.CreateBitCast(B, FlatTy),
  3572. ConstantInt::getNullValue(FlatTy));
  3573. Sb = IRB.CreateICmpNE(IRB.CreateBitCast(Sb, FlatTy),
  3574. ConstantInt::getNullValue(FlatTy));
  3575. }
  3576. // a = select b, c, d
  3577. // Oa = Sb ? Ob : (b ? Oc : Od)
  3578. setOrigin(
  3579. &I, IRB.CreateSelect(Sb, getOrigin(I.getCondition()),
  3580. IRB.CreateSelect(B, getOrigin(I.getTrueValue()),
  3581. getOrigin(I.getFalseValue()))));
  3582. }
  3583. }
  3584. void visitLandingPadInst(LandingPadInst &I) {
  3585. // Do nothing.
  3586. // See https://github.com/google/sanitizers/issues/504
  3587. setShadow(&I, getCleanShadow(&I));
  3588. setOrigin(&I, getCleanOrigin());
  3589. }
  3590. void visitCatchSwitchInst(CatchSwitchInst &I) {
  3591. setShadow(&I, getCleanShadow(&I));
  3592. setOrigin(&I, getCleanOrigin());
  3593. }
  3594. void visitFuncletPadInst(FuncletPadInst &I) {
  3595. setShadow(&I, getCleanShadow(&I));
  3596. setOrigin(&I, getCleanOrigin());
  3597. }
  3598. void visitGetElementPtrInst(GetElementPtrInst &I) {
  3599. handleShadowOr(I);
  3600. }
  3601. void visitExtractValueInst(ExtractValueInst &I) {
  3602. IRBuilder<> IRB(&I);
  3603. Value *Agg = I.getAggregateOperand();
  3604. LLVM_DEBUG(dbgs() << "ExtractValue: " << I << "\n");
  3605. Value *AggShadow = getShadow(Agg);
  3606. LLVM_DEBUG(dbgs() << " AggShadow: " << *AggShadow << "\n");
  3607. Value *ResShadow = IRB.CreateExtractValue(AggShadow, I.getIndices());
  3608. LLVM_DEBUG(dbgs() << " ResShadow: " << *ResShadow << "\n");
  3609. setShadow(&I, ResShadow);
  3610. setOriginForNaryOp(I);
  3611. }
  3612. void visitInsertValueInst(InsertValueInst &I) {
  3613. IRBuilder<> IRB(&I);
  3614. LLVM_DEBUG(dbgs() << "InsertValue: " << I << "\n");
  3615. Value *AggShadow = getShadow(I.getAggregateOperand());
  3616. Value *InsShadow = getShadow(I.getInsertedValueOperand());
  3617. LLVM_DEBUG(dbgs() << " AggShadow: " << *AggShadow << "\n");
  3618. LLVM_DEBUG(dbgs() << " InsShadow: " << *InsShadow << "\n");
  3619. Value *Res = IRB.CreateInsertValue(AggShadow, InsShadow, I.getIndices());
  3620. LLVM_DEBUG(dbgs() << " Res: " << *Res << "\n");
  3621. setShadow(&I, Res);
  3622. setOriginForNaryOp(I);
  3623. }
  3624. void dumpInst(Instruction &I) {
  3625. if (CallInst *CI = dyn_cast<CallInst>(&I)) {
  3626. errs() << "ZZZ call " << CI->getCalledFunction()->getName() << "\n";
  3627. } else {
  3628. errs() << "ZZZ " << I.getOpcodeName() << "\n";
  3629. }
  3630. errs() << "QQQ " << I << "\n";
  3631. }
  3632. void visitResumeInst(ResumeInst &I) {
  3633. LLVM_DEBUG(dbgs() << "Resume: " << I << "\n");
  3634. // Nothing to do here.
  3635. }
  3636. void visitCleanupReturnInst(CleanupReturnInst &CRI) {
  3637. LLVM_DEBUG(dbgs() << "CleanupReturn: " << CRI << "\n");
  3638. // Nothing to do here.
  3639. }
  3640. void visitCatchReturnInst(CatchReturnInst &CRI) {
  3641. LLVM_DEBUG(dbgs() << "CatchReturn: " << CRI << "\n");
  3642. // Nothing to do here.
  3643. }
  3644. void instrumentAsmArgument(Value *Operand, Instruction &I, IRBuilder<> &IRB,
  3645. const DataLayout &DL, bool isOutput) {
  3646. // For each assembly argument, we check its value for being initialized.
  3647. // If the argument is a pointer, we assume it points to a single element
  3648. // of the corresponding type (or to a 8-byte word, if the type is unsized).
  3649. // Each such pointer is instrumented with a call to the runtime library.
  3650. Type *OpType = Operand->getType();
  3651. // Check the operand value itself.
  3652. insertShadowCheck(Operand, &I);
  3653. if (!OpType->isPointerTy() || !isOutput) {
  3654. assert(!isOutput);
  3655. return;
  3656. }
  3657. Type *ElType = OpType->getPointerElementType();
  3658. if (!ElType->isSized())
  3659. return;
  3660. int Size = DL.getTypeStoreSize(ElType);
  3661. Value *Ptr = IRB.CreatePointerCast(Operand, IRB.getInt8PtrTy());
  3662. Value *SizeVal = ConstantInt::get(MS.IntptrTy, Size);
  3663. IRB.CreateCall(MS.MsanInstrumentAsmStoreFn, {Ptr, SizeVal});
  3664. }
  3665. /// Get the number of output arguments returned by pointers.
  3666. int getNumOutputArgs(InlineAsm *IA, CallBase *CB) {
  3667. int NumRetOutputs = 0;
  3668. int NumOutputs = 0;
  3669. Type *RetTy = cast<Value>(CB)->getType();
  3670. if (!RetTy->isVoidTy()) {
  3671. // Register outputs are returned via the CallInst return value.
  3672. auto *ST = dyn_cast<StructType>(RetTy);
  3673. if (ST)
  3674. NumRetOutputs = ST->getNumElements();
  3675. else
  3676. NumRetOutputs = 1;
  3677. }
  3678. InlineAsm::ConstraintInfoVector Constraints = IA->ParseConstraints();
  3679. for (const InlineAsm::ConstraintInfo &Info : Constraints) {
  3680. switch (Info.Type) {
  3681. case InlineAsm::isOutput:
  3682. NumOutputs++;
  3683. break;
  3684. default:
  3685. break;
  3686. }
  3687. }
  3688. return NumOutputs - NumRetOutputs;
  3689. }
  3690. void visitAsmInstruction(Instruction &I) {
  3691. // Conservative inline assembly handling: check for poisoned shadow of
  3692. // asm() arguments, then unpoison the result and all the memory locations
  3693. // pointed to by those arguments.
  3694. // An inline asm() statement in C++ contains lists of input and output
  3695. // arguments used by the assembly code. These are mapped to operands of the
  3696. // CallInst as follows:
  3697. // - nR register outputs ("=r) are returned by value in a single structure
  3698. // (SSA value of the CallInst);
  3699. // - nO other outputs ("=m" and others) are returned by pointer as first
  3700. // nO operands of the CallInst;
  3701. // - nI inputs ("r", "m" and others) are passed to CallInst as the
  3702. // remaining nI operands.
  3703. // The total number of asm() arguments in the source is nR+nO+nI, and the
  3704. // corresponding CallInst has nO+nI+1 operands (the last operand is the
  3705. // function to be called).
  3706. const DataLayout &DL = F.getParent()->getDataLayout();
  3707. CallBase *CB = cast<CallBase>(&I);
  3708. IRBuilder<> IRB(&I);
  3709. InlineAsm *IA = cast<InlineAsm>(CB->getCalledOperand());
  3710. int OutputArgs = getNumOutputArgs(IA, CB);
  3711. // The last operand of a CallInst is the function itself.
  3712. int NumOperands = CB->getNumOperands() - 1;
  3713. // Check input arguments. Doing so before unpoisoning output arguments, so
  3714. // that we won't overwrite uninit values before checking them.
  3715. for (int i = OutputArgs; i < NumOperands; i++) {
  3716. Value *Operand = CB->getOperand(i);
  3717. instrumentAsmArgument(Operand, I, IRB, DL, /*isOutput*/ false);
  3718. }
  3719. // Unpoison output arguments. This must happen before the actual InlineAsm
  3720. // call, so that the shadow for memory published in the asm() statement
  3721. // remains valid.
  3722. for (int i = 0; i < OutputArgs; i++) {
  3723. Value *Operand = CB->getOperand(i);
  3724. instrumentAsmArgument(Operand, I, IRB, DL, /*isOutput*/ true);
  3725. }
  3726. setShadow(&I, getCleanShadow(&I));
  3727. setOrigin(&I, getCleanOrigin());
  3728. }
  3729. void visitFreezeInst(FreezeInst &I) {
  3730. // Freeze always returns a fully defined value.
  3731. setShadow(&I, getCleanShadow(&I));
  3732. setOrigin(&I, getCleanOrigin());
  3733. }
  3734. void visitInstruction(Instruction &I) {
  3735. // Everything else: stop propagating and check for poisoned shadow.
  3736. if (ClDumpStrictInstructions)
  3737. dumpInst(I);
  3738. LLVM_DEBUG(dbgs() << "DEFAULT: " << I << "\n");
  3739. for (size_t i = 0, n = I.getNumOperands(); i < n; i++) {
  3740. Value *Operand = I.getOperand(i);
  3741. if (Operand->getType()->isSized())
  3742. insertShadowCheck(Operand, &I);
  3743. }
  3744. setShadow(&I, getCleanShadow(&I));
  3745. setOrigin(&I, getCleanOrigin());
  3746. }
  3747. };
  3748. /// AMD64-specific implementation of VarArgHelper.
  3749. struct VarArgAMD64Helper : public VarArgHelper {
  3750. // An unfortunate workaround for asymmetric lowering of va_arg stuff.
  3751. // See a comment in visitCallBase for more details.
  3752. static const unsigned AMD64GpEndOffset = 48; // AMD64 ABI Draft 0.99.6 p3.5.7
  3753. static const unsigned AMD64FpEndOffsetSSE = 176;
  3754. // If SSE is disabled, fp_offset in va_list is zero.
  3755. static const unsigned AMD64FpEndOffsetNoSSE = AMD64GpEndOffset;
  3756. unsigned AMD64FpEndOffset;
  3757. Function &F;
  3758. MemorySanitizer &MS;
  3759. MemorySanitizerVisitor &MSV;
  3760. Value *VAArgTLSCopy = nullptr;
  3761. Value *VAArgTLSOriginCopy = nullptr;
  3762. Value *VAArgOverflowSize = nullptr;
  3763. SmallVector<CallInst*, 16> VAStartInstrumentationList;
  3764. enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
  3765. VarArgAMD64Helper(Function &F, MemorySanitizer &MS,
  3766. MemorySanitizerVisitor &MSV)
  3767. : F(F), MS(MS), MSV(MSV) {
  3768. AMD64FpEndOffset = AMD64FpEndOffsetSSE;
  3769. for (const auto &Attr : F.getAttributes().getFnAttrs()) {
  3770. if (Attr.isStringAttribute() &&
  3771. (Attr.getKindAsString() == "target-features")) {
  3772. if (Attr.getValueAsString().contains("-sse"))
  3773. AMD64FpEndOffset = AMD64FpEndOffsetNoSSE;
  3774. break;
  3775. }
  3776. }
  3777. }
  3778. ArgKind classifyArgument(Value* arg) {
  3779. // A very rough approximation of X86_64 argument classification rules.
  3780. Type *T = arg->getType();
  3781. if (T->isFPOrFPVectorTy() || T->isX86_MMXTy())
  3782. return AK_FloatingPoint;
  3783. if (T->isIntegerTy() && T->getPrimitiveSizeInBits() <= 64)
  3784. return AK_GeneralPurpose;
  3785. if (T->isPointerTy())
  3786. return AK_GeneralPurpose;
  3787. return AK_Memory;
  3788. }
  3789. // For VarArg functions, store the argument shadow in an ABI-specific format
  3790. // that corresponds to va_list layout.
  3791. // We do this because Clang lowers va_arg in the frontend, and this pass
  3792. // only sees the low level code that deals with va_list internals.
  3793. // A much easier alternative (provided that Clang emits va_arg instructions)
  3794. // would have been to associate each live instance of va_list with a copy of
  3795. // MSanParamTLS, and extract shadow on va_arg() call in the argument list
  3796. // order.
  3797. void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {
  3798. unsigned GpOffset = 0;
  3799. unsigned FpOffset = AMD64GpEndOffset;
  3800. unsigned OverflowOffset = AMD64FpEndOffset;
  3801. const DataLayout &DL = F.getParent()->getDataLayout();
  3802. for (auto ArgIt = CB.arg_begin(), End = CB.arg_end(); ArgIt != End;
  3803. ++ArgIt) {
  3804. Value *A = *ArgIt;
  3805. unsigned ArgNo = CB.getArgOperandNo(ArgIt);
  3806. bool IsFixed = ArgNo < CB.getFunctionType()->getNumParams();
  3807. bool IsByVal = CB.paramHasAttr(ArgNo, Attribute::ByVal);
  3808. if (IsByVal) {
  3809. // ByVal arguments always go to the overflow area.
  3810. // Fixed arguments passed through the overflow area will be stepped
  3811. // over by va_start, so don't count them towards the offset.
  3812. if (IsFixed)
  3813. continue;
  3814. assert(A->getType()->isPointerTy());
  3815. Type *RealTy = CB.getParamByValType(ArgNo);
  3816. uint64_t ArgSize = DL.getTypeAllocSize(RealTy);
  3817. Value *ShadowBase = getShadowPtrForVAArgument(
  3818. RealTy, IRB, OverflowOffset, alignTo(ArgSize, 8));
  3819. Value *OriginBase = nullptr;
  3820. if (MS.TrackOrigins)
  3821. OriginBase = getOriginPtrForVAArgument(RealTy, IRB, OverflowOffset);
  3822. OverflowOffset += alignTo(ArgSize, 8);
  3823. if (!ShadowBase)
  3824. continue;
  3825. Value *ShadowPtr, *OriginPtr;
  3826. std::tie(ShadowPtr, OriginPtr) =
  3827. MSV.getShadowOriginPtr(A, IRB, IRB.getInt8Ty(), kShadowTLSAlignment,
  3828. /*isStore*/ false);
  3829. IRB.CreateMemCpy(ShadowBase, kShadowTLSAlignment, ShadowPtr,
  3830. kShadowTLSAlignment, ArgSize);
  3831. if (MS.TrackOrigins)
  3832. IRB.CreateMemCpy(OriginBase, kShadowTLSAlignment, OriginPtr,
  3833. kShadowTLSAlignment, ArgSize);
  3834. } else {
  3835. ArgKind AK = classifyArgument(A);
  3836. if (AK == AK_GeneralPurpose && GpOffset >= AMD64GpEndOffset)
  3837. AK = AK_Memory;
  3838. if (AK == AK_FloatingPoint && FpOffset >= AMD64FpEndOffset)
  3839. AK = AK_Memory;
  3840. Value *ShadowBase, *OriginBase = nullptr;
  3841. switch (AK) {
  3842. case AK_GeneralPurpose:
  3843. ShadowBase =
  3844. getShadowPtrForVAArgument(A->getType(), IRB, GpOffset, 8);
  3845. if (MS.TrackOrigins)
  3846. OriginBase =
  3847. getOriginPtrForVAArgument(A->getType(), IRB, GpOffset);
  3848. GpOffset += 8;
  3849. break;
  3850. case AK_FloatingPoint:
  3851. ShadowBase =
  3852. getShadowPtrForVAArgument(A->getType(), IRB, FpOffset, 16);
  3853. if (MS.TrackOrigins)
  3854. OriginBase =
  3855. getOriginPtrForVAArgument(A->getType(), IRB, FpOffset);
  3856. FpOffset += 16;
  3857. break;
  3858. case AK_Memory:
  3859. if (IsFixed)
  3860. continue;
  3861. uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
  3862. ShadowBase =
  3863. getShadowPtrForVAArgument(A->getType(), IRB, OverflowOffset, 8);
  3864. if (MS.TrackOrigins)
  3865. OriginBase =
  3866. getOriginPtrForVAArgument(A->getType(), IRB, OverflowOffset);
  3867. OverflowOffset += alignTo(ArgSize, 8);
  3868. }
  3869. // Take fixed arguments into account for GpOffset and FpOffset,
  3870. // but don't actually store shadows for them.
  3871. // TODO(glider): don't call get*PtrForVAArgument() for them.
  3872. if (IsFixed)
  3873. continue;
  3874. if (!ShadowBase)
  3875. continue;
  3876. Value *Shadow = MSV.getShadow(A);
  3877. IRB.CreateAlignedStore(Shadow, ShadowBase, kShadowTLSAlignment);
  3878. if (MS.TrackOrigins) {
  3879. Value *Origin = MSV.getOrigin(A);
  3880. unsigned StoreSize = DL.getTypeStoreSize(Shadow->getType());
  3881. MSV.paintOrigin(IRB, Origin, OriginBase, StoreSize,
  3882. std::max(kShadowTLSAlignment, kMinOriginAlignment));
  3883. }
  3884. }
  3885. }
  3886. Constant *OverflowSize =
  3887. ConstantInt::get(IRB.getInt64Ty(), OverflowOffset - AMD64FpEndOffset);
  3888. IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
  3889. }
  3890. /// Compute the shadow address for a given va_arg.
  3891. Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
  3892. unsigned ArgOffset, unsigned ArgSize) {
  3893. // Make sure we don't overflow __msan_va_arg_tls.
  3894. if (ArgOffset + ArgSize > kParamTLSSize)
  3895. return nullptr;
  3896. Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
  3897. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  3898. return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
  3899. "_msarg_va_s");
  3900. }
  3901. /// Compute the origin address for a given va_arg.
  3902. Value *getOriginPtrForVAArgument(Type *Ty, IRBuilder<> &IRB, int ArgOffset) {
  3903. Value *Base = IRB.CreatePointerCast(MS.VAArgOriginTLS, MS.IntptrTy);
  3904. // getOriginPtrForVAArgument() is always called after
  3905. // getShadowPtrForVAArgument(), so __msan_va_arg_origin_tls can never
  3906. // overflow.
  3907. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  3908. return IRB.CreateIntToPtr(Base, PointerType::get(MS.OriginTy, 0),
  3909. "_msarg_va_o");
  3910. }
  3911. void unpoisonVAListTagForInst(IntrinsicInst &I) {
  3912. IRBuilder<> IRB(&I);
  3913. Value *VAListTag = I.getArgOperand(0);
  3914. Value *ShadowPtr, *OriginPtr;
  3915. const Align Alignment = Align(8);
  3916. std::tie(ShadowPtr, OriginPtr) =
  3917. MSV.getShadowOriginPtr(VAListTag, IRB, IRB.getInt8Ty(), Alignment,
  3918. /*isStore*/ true);
  3919. // Unpoison the whole __va_list_tag.
  3920. // FIXME: magic ABI constants.
  3921. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  3922. /* size */ 24, Alignment, false);
  3923. // We shouldn't need to zero out the origins, as they're only checked for
  3924. // nonzero shadow.
  3925. }
  3926. void visitVAStartInst(VAStartInst &I) override {
  3927. if (F.getCallingConv() == CallingConv::Win64)
  3928. return;
  3929. VAStartInstrumentationList.push_back(&I);
  3930. unpoisonVAListTagForInst(I);
  3931. }
  3932. void visitVACopyInst(VACopyInst &I) override {
  3933. if (F.getCallingConv() == CallingConv::Win64) return;
  3934. unpoisonVAListTagForInst(I);
  3935. }
  3936. void finalizeInstrumentation() override {
  3937. assert(!VAArgOverflowSize && !VAArgTLSCopy &&
  3938. "finalizeInstrumentation called twice");
  3939. if (!VAStartInstrumentationList.empty()) {
  3940. // If there is a va_start in this function, make a backup copy of
  3941. // va_arg_tls somewhere in the function entry block.
  3942. IRBuilder<> IRB(MSV.FnPrologueEnd);
  3943. VAArgOverflowSize =
  3944. IRB.CreateLoad(IRB.getInt64Ty(), MS.VAArgOverflowSizeTLS);
  3945. Value *CopySize =
  3946. IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AMD64FpEndOffset),
  3947. VAArgOverflowSize);
  3948. VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
  3949. IRB.CreateMemCpy(VAArgTLSCopy, Align(8), MS.VAArgTLS, Align(8), CopySize);
  3950. if (MS.TrackOrigins) {
  3951. VAArgTLSOriginCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
  3952. IRB.CreateMemCpy(VAArgTLSOriginCopy, Align(8), MS.VAArgOriginTLS,
  3953. Align(8), CopySize);
  3954. }
  3955. }
  3956. // Instrument va_start.
  3957. // Copy va_list shadow from the backup copy of the TLS contents.
  3958. for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
  3959. CallInst *OrigInst = VAStartInstrumentationList[i];
  3960. IRBuilder<> IRB(OrigInst->getNextNode());
  3961. Value *VAListTag = OrigInst->getArgOperand(0);
  3962. Type *RegSaveAreaPtrTy = Type::getInt64PtrTy(*MS.C);
  3963. Value *RegSaveAreaPtrPtr = IRB.CreateIntToPtr(
  3964. IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  3965. ConstantInt::get(MS.IntptrTy, 16)),
  3966. PointerType::get(RegSaveAreaPtrTy, 0));
  3967. Value *RegSaveAreaPtr =
  3968. IRB.CreateLoad(RegSaveAreaPtrTy, RegSaveAreaPtrPtr);
  3969. Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
  3970. const Align Alignment = Align(16);
  3971. std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
  3972. MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.getInt8Ty(),
  3973. Alignment, /*isStore*/ true);
  3974. IRB.CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
  3975. AMD64FpEndOffset);
  3976. if (MS.TrackOrigins)
  3977. IRB.CreateMemCpy(RegSaveAreaOriginPtr, Alignment, VAArgTLSOriginCopy,
  3978. Alignment, AMD64FpEndOffset);
  3979. Type *OverflowArgAreaPtrTy = Type::getInt64PtrTy(*MS.C);
  3980. Value *OverflowArgAreaPtrPtr = IRB.CreateIntToPtr(
  3981. IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  3982. ConstantInt::get(MS.IntptrTy, 8)),
  3983. PointerType::get(OverflowArgAreaPtrTy, 0));
  3984. Value *OverflowArgAreaPtr =
  3985. IRB.CreateLoad(OverflowArgAreaPtrTy, OverflowArgAreaPtrPtr);
  3986. Value *OverflowArgAreaShadowPtr, *OverflowArgAreaOriginPtr;
  3987. std::tie(OverflowArgAreaShadowPtr, OverflowArgAreaOriginPtr) =
  3988. MSV.getShadowOriginPtr(OverflowArgAreaPtr, IRB, IRB.getInt8Ty(),
  3989. Alignment, /*isStore*/ true);
  3990. Value *SrcPtr = IRB.CreateConstGEP1_32(IRB.getInt8Ty(), VAArgTLSCopy,
  3991. AMD64FpEndOffset);
  3992. IRB.CreateMemCpy(OverflowArgAreaShadowPtr, Alignment, SrcPtr, Alignment,
  3993. VAArgOverflowSize);
  3994. if (MS.TrackOrigins) {
  3995. SrcPtr = IRB.CreateConstGEP1_32(IRB.getInt8Ty(), VAArgTLSOriginCopy,
  3996. AMD64FpEndOffset);
  3997. IRB.CreateMemCpy(OverflowArgAreaOriginPtr, Alignment, SrcPtr, Alignment,
  3998. VAArgOverflowSize);
  3999. }
  4000. }
  4001. }
  4002. };
  4003. /// MIPS64-specific implementation of VarArgHelper.
  4004. struct VarArgMIPS64Helper : public VarArgHelper {
  4005. Function &F;
  4006. MemorySanitizer &MS;
  4007. MemorySanitizerVisitor &MSV;
  4008. Value *VAArgTLSCopy = nullptr;
  4009. Value *VAArgSize = nullptr;
  4010. SmallVector<CallInst*, 16> VAStartInstrumentationList;
  4011. VarArgMIPS64Helper(Function &F, MemorySanitizer &MS,
  4012. MemorySanitizerVisitor &MSV) : F(F), MS(MS), MSV(MSV) {}
  4013. void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {
  4014. unsigned VAArgOffset = 0;
  4015. const DataLayout &DL = F.getParent()->getDataLayout();
  4016. for (auto ArgIt = CB.arg_begin() + CB.getFunctionType()->getNumParams(),
  4017. End = CB.arg_end();
  4018. ArgIt != End; ++ArgIt) {
  4019. Triple TargetTriple(F.getParent()->getTargetTriple());
  4020. Value *A = *ArgIt;
  4021. Value *Base;
  4022. uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
  4023. if (TargetTriple.getArch() == Triple::mips64) {
  4024. // Adjusting the shadow for argument with size < 8 to match the placement
  4025. // of bits in big endian system
  4026. if (ArgSize < 8)
  4027. VAArgOffset += (8 - ArgSize);
  4028. }
  4029. Base = getShadowPtrForVAArgument(A->getType(), IRB, VAArgOffset, ArgSize);
  4030. VAArgOffset += ArgSize;
  4031. VAArgOffset = alignTo(VAArgOffset, 8);
  4032. if (!Base)
  4033. continue;
  4034. IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
  4035. }
  4036. Constant *TotalVAArgSize = ConstantInt::get(IRB.getInt64Ty(), VAArgOffset);
  4037. // Here using VAArgOverflowSizeTLS as VAArgSizeTLS to avoid creation of
  4038. // a new class member i.e. it is the total size of all VarArgs.
  4039. IRB.CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
  4040. }
  4041. /// Compute the shadow address for a given va_arg.
  4042. Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
  4043. unsigned ArgOffset, unsigned ArgSize) {
  4044. // Make sure we don't overflow __msan_va_arg_tls.
  4045. if (ArgOffset + ArgSize > kParamTLSSize)
  4046. return nullptr;
  4047. Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
  4048. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  4049. return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
  4050. "_msarg");
  4051. }
  4052. void visitVAStartInst(VAStartInst &I) override {
  4053. IRBuilder<> IRB(&I);
  4054. VAStartInstrumentationList.push_back(&I);
  4055. Value *VAListTag = I.getArgOperand(0);
  4056. Value *ShadowPtr, *OriginPtr;
  4057. const Align Alignment = Align(8);
  4058. std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
  4059. VAListTag, IRB, IRB.getInt8Ty(), Alignment, /*isStore*/ true);
  4060. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  4061. /* size */ 8, Alignment, false);
  4062. }
  4063. void visitVACopyInst(VACopyInst &I) override {
  4064. IRBuilder<> IRB(&I);
  4065. VAStartInstrumentationList.push_back(&I);
  4066. Value *VAListTag = I.getArgOperand(0);
  4067. Value *ShadowPtr, *OriginPtr;
  4068. const Align Alignment = Align(8);
  4069. std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
  4070. VAListTag, IRB, IRB.getInt8Ty(), Alignment, /*isStore*/ true);
  4071. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  4072. /* size */ 8, Alignment, false);
  4073. }
  4074. void finalizeInstrumentation() override {
  4075. assert(!VAArgSize && !VAArgTLSCopy &&
  4076. "finalizeInstrumentation called twice");
  4077. IRBuilder<> IRB(MSV.FnPrologueEnd);
  4078. VAArgSize = IRB.CreateLoad(IRB.getInt64Ty(), MS.VAArgOverflowSizeTLS);
  4079. Value *CopySize = IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0),
  4080. VAArgSize);
  4081. if (!VAStartInstrumentationList.empty()) {
  4082. // If there is a va_start in this function, make a backup copy of
  4083. // va_arg_tls somewhere in the function entry block.
  4084. VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
  4085. IRB.CreateMemCpy(VAArgTLSCopy, Align(8), MS.VAArgTLS, Align(8), CopySize);
  4086. }
  4087. // Instrument va_start.
  4088. // Copy va_list shadow from the backup copy of the TLS contents.
  4089. for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
  4090. CallInst *OrigInst = VAStartInstrumentationList[i];
  4091. IRBuilder<> IRB(OrigInst->getNextNode());
  4092. Value *VAListTag = OrigInst->getArgOperand(0);
  4093. Type *RegSaveAreaPtrTy = Type::getInt64PtrTy(*MS.C);
  4094. Value *RegSaveAreaPtrPtr =
  4095. IRB.CreateIntToPtr(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  4096. PointerType::get(RegSaveAreaPtrTy, 0));
  4097. Value *RegSaveAreaPtr =
  4098. IRB.CreateLoad(RegSaveAreaPtrTy, RegSaveAreaPtrPtr);
  4099. Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
  4100. const Align Alignment = Align(8);
  4101. std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
  4102. MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.getInt8Ty(),
  4103. Alignment, /*isStore*/ true);
  4104. IRB.CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
  4105. CopySize);
  4106. }
  4107. }
  4108. };
  4109. /// AArch64-specific implementation of VarArgHelper.
  4110. struct VarArgAArch64Helper : public VarArgHelper {
  4111. static const unsigned kAArch64GrArgSize = 64;
  4112. static const unsigned kAArch64VrArgSize = 128;
  4113. static const unsigned AArch64GrBegOffset = 0;
  4114. static const unsigned AArch64GrEndOffset = kAArch64GrArgSize;
  4115. // Make VR space aligned to 16 bytes.
  4116. static const unsigned AArch64VrBegOffset = AArch64GrEndOffset;
  4117. static const unsigned AArch64VrEndOffset = AArch64VrBegOffset
  4118. + kAArch64VrArgSize;
  4119. static const unsigned AArch64VAEndOffset = AArch64VrEndOffset;
  4120. Function &F;
  4121. MemorySanitizer &MS;
  4122. MemorySanitizerVisitor &MSV;
  4123. Value *VAArgTLSCopy = nullptr;
  4124. Value *VAArgOverflowSize = nullptr;
  4125. SmallVector<CallInst*, 16> VAStartInstrumentationList;
  4126. enum ArgKind { AK_GeneralPurpose, AK_FloatingPoint, AK_Memory };
  4127. VarArgAArch64Helper(Function &F, MemorySanitizer &MS,
  4128. MemorySanitizerVisitor &MSV) : F(F), MS(MS), MSV(MSV) {}
  4129. ArgKind classifyArgument(Value* arg) {
  4130. Type *T = arg->getType();
  4131. if (T->isFPOrFPVectorTy())
  4132. return AK_FloatingPoint;
  4133. if ((T->isIntegerTy() && T->getPrimitiveSizeInBits() <= 64)
  4134. || (T->isPointerTy()))
  4135. return AK_GeneralPurpose;
  4136. return AK_Memory;
  4137. }
  4138. // The instrumentation stores the argument shadow in a non ABI-specific
  4139. // format because it does not know which argument is named (since Clang,
  4140. // like x86_64 case, lowers the va_args in the frontend and this pass only
  4141. // sees the low level code that deals with va_list internals).
  4142. // The first seven GR registers are saved in the first 56 bytes of the
  4143. // va_arg tls arra, followers by the first 8 FP/SIMD registers, and then
  4144. // the remaining arguments.
  4145. // Using constant offset within the va_arg TLS array allows fast copy
  4146. // in the finalize instrumentation.
  4147. void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {
  4148. unsigned GrOffset = AArch64GrBegOffset;
  4149. unsigned VrOffset = AArch64VrBegOffset;
  4150. unsigned OverflowOffset = AArch64VAEndOffset;
  4151. const DataLayout &DL = F.getParent()->getDataLayout();
  4152. for (auto ArgIt = CB.arg_begin(), End = CB.arg_end(); ArgIt != End;
  4153. ++ArgIt) {
  4154. Value *A = *ArgIt;
  4155. unsigned ArgNo = CB.getArgOperandNo(ArgIt);
  4156. bool IsFixed = ArgNo < CB.getFunctionType()->getNumParams();
  4157. ArgKind AK = classifyArgument(A);
  4158. if (AK == AK_GeneralPurpose && GrOffset >= AArch64GrEndOffset)
  4159. AK = AK_Memory;
  4160. if (AK == AK_FloatingPoint && VrOffset >= AArch64VrEndOffset)
  4161. AK = AK_Memory;
  4162. Value *Base;
  4163. switch (AK) {
  4164. case AK_GeneralPurpose:
  4165. Base = getShadowPtrForVAArgument(A->getType(), IRB, GrOffset, 8);
  4166. GrOffset += 8;
  4167. break;
  4168. case AK_FloatingPoint:
  4169. Base = getShadowPtrForVAArgument(A->getType(), IRB, VrOffset, 8);
  4170. VrOffset += 16;
  4171. break;
  4172. case AK_Memory:
  4173. // Don't count fixed arguments in the overflow area - va_start will
  4174. // skip right over them.
  4175. if (IsFixed)
  4176. continue;
  4177. uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
  4178. Base = getShadowPtrForVAArgument(A->getType(), IRB, OverflowOffset,
  4179. alignTo(ArgSize, 8));
  4180. OverflowOffset += alignTo(ArgSize, 8);
  4181. break;
  4182. }
  4183. // Count Gp/Vr fixed arguments to their respective offsets, but don't
  4184. // bother to actually store a shadow.
  4185. if (IsFixed)
  4186. continue;
  4187. if (!Base)
  4188. continue;
  4189. IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
  4190. }
  4191. Constant *OverflowSize =
  4192. ConstantInt::get(IRB.getInt64Ty(), OverflowOffset - AArch64VAEndOffset);
  4193. IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
  4194. }
  4195. /// Compute the shadow address for a given va_arg.
  4196. Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
  4197. unsigned ArgOffset, unsigned ArgSize) {
  4198. // Make sure we don't overflow __msan_va_arg_tls.
  4199. if (ArgOffset + ArgSize > kParamTLSSize)
  4200. return nullptr;
  4201. Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
  4202. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  4203. return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
  4204. "_msarg");
  4205. }
  4206. void visitVAStartInst(VAStartInst &I) override {
  4207. IRBuilder<> IRB(&I);
  4208. VAStartInstrumentationList.push_back(&I);
  4209. Value *VAListTag = I.getArgOperand(0);
  4210. Value *ShadowPtr, *OriginPtr;
  4211. const Align Alignment = Align(8);
  4212. std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
  4213. VAListTag, IRB, IRB.getInt8Ty(), Alignment, /*isStore*/ true);
  4214. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  4215. /* size */ 32, Alignment, false);
  4216. }
  4217. void visitVACopyInst(VACopyInst &I) override {
  4218. IRBuilder<> IRB(&I);
  4219. VAStartInstrumentationList.push_back(&I);
  4220. Value *VAListTag = I.getArgOperand(0);
  4221. Value *ShadowPtr, *OriginPtr;
  4222. const Align Alignment = Align(8);
  4223. std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
  4224. VAListTag, IRB, IRB.getInt8Ty(), Alignment, /*isStore*/ true);
  4225. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  4226. /* size */ 32, Alignment, false);
  4227. }
  4228. // Retrieve a va_list field of 'void*' size.
  4229. Value* getVAField64(IRBuilder<> &IRB, Value *VAListTag, int offset) {
  4230. Value *SaveAreaPtrPtr =
  4231. IRB.CreateIntToPtr(
  4232. IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  4233. ConstantInt::get(MS.IntptrTy, offset)),
  4234. Type::getInt64PtrTy(*MS.C));
  4235. return IRB.CreateLoad(Type::getInt64Ty(*MS.C), SaveAreaPtrPtr);
  4236. }
  4237. // Retrieve a va_list field of 'int' size.
  4238. Value* getVAField32(IRBuilder<> &IRB, Value *VAListTag, int offset) {
  4239. Value *SaveAreaPtr =
  4240. IRB.CreateIntToPtr(
  4241. IRB.CreateAdd(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  4242. ConstantInt::get(MS.IntptrTy, offset)),
  4243. Type::getInt32PtrTy(*MS.C));
  4244. Value *SaveArea32 = IRB.CreateLoad(IRB.getInt32Ty(), SaveAreaPtr);
  4245. return IRB.CreateSExt(SaveArea32, MS.IntptrTy);
  4246. }
  4247. void finalizeInstrumentation() override {
  4248. assert(!VAArgOverflowSize && !VAArgTLSCopy &&
  4249. "finalizeInstrumentation called twice");
  4250. if (!VAStartInstrumentationList.empty()) {
  4251. // If there is a va_start in this function, make a backup copy of
  4252. // va_arg_tls somewhere in the function entry block.
  4253. IRBuilder<> IRB(MSV.FnPrologueEnd);
  4254. VAArgOverflowSize =
  4255. IRB.CreateLoad(IRB.getInt64Ty(), MS.VAArgOverflowSizeTLS);
  4256. Value *CopySize =
  4257. IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, AArch64VAEndOffset),
  4258. VAArgOverflowSize);
  4259. VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
  4260. IRB.CreateMemCpy(VAArgTLSCopy, Align(8), MS.VAArgTLS, Align(8), CopySize);
  4261. }
  4262. Value *GrArgSize = ConstantInt::get(MS.IntptrTy, kAArch64GrArgSize);
  4263. Value *VrArgSize = ConstantInt::get(MS.IntptrTy, kAArch64VrArgSize);
  4264. // Instrument va_start, copy va_list shadow from the backup copy of
  4265. // the TLS contents.
  4266. for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
  4267. CallInst *OrigInst = VAStartInstrumentationList[i];
  4268. IRBuilder<> IRB(OrigInst->getNextNode());
  4269. Value *VAListTag = OrigInst->getArgOperand(0);
  4270. // The variadic ABI for AArch64 creates two areas to save the incoming
  4271. // argument registers (one for 64-bit general register xn-x7 and another
  4272. // for 128-bit FP/SIMD vn-v7).
  4273. // We need then to propagate the shadow arguments on both regions
  4274. // 'va::__gr_top + va::__gr_offs' and 'va::__vr_top + va::__vr_offs'.
  4275. // The remaining arguments are saved on shadow for 'va::stack'.
  4276. // One caveat is it requires only to propagate the non-named arguments,
  4277. // however on the call site instrumentation 'all' the arguments are
  4278. // saved. So to copy the shadow values from the va_arg TLS array
  4279. // we need to adjust the offset for both GR and VR fields based on
  4280. // the __{gr,vr}_offs value (since they are stores based on incoming
  4281. // named arguments).
  4282. // Read the stack pointer from the va_list.
  4283. Value *StackSaveAreaPtr = getVAField64(IRB, VAListTag, 0);
  4284. // Read both the __gr_top and __gr_off and add them up.
  4285. Value *GrTopSaveAreaPtr = getVAField64(IRB, VAListTag, 8);
  4286. Value *GrOffSaveArea = getVAField32(IRB, VAListTag, 24);
  4287. Value *GrRegSaveAreaPtr = IRB.CreateAdd(GrTopSaveAreaPtr, GrOffSaveArea);
  4288. // Read both the __vr_top and __vr_off and add them up.
  4289. Value *VrTopSaveAreaPtr = getVAField64(IRB, VAListTag, 16);
  4290. Value *VrOffSaveArea = getVAField32(IRB, VAListTag, 28);
  4291. Value *VrRegSaveAreaPtr = IRB.CreateAdd(VrTopSaveAreaPtr, VrOffSaveArea);
  4292. // It does not know how many named arguments is being used and, on the
  4293. // callsite all the arguments were saved. Since __gr_off is defined as
  4294. // '0 - ((8 - named_gr) * 8)', the idea is to just propagate the variadic
  4295. // argument by ignoring the bytes of shadow from named arguments.
  4296. Value *GrRegSaveAreaShadowPtrOff =
  4297. IRB.CreateAdd(GrArgSize, GrOffSaveArea);
  4298. Value *GrRegSaveAreaShadowPtr =
  4299. MSV.getShadowOriginPtr(GrRegSaveAreaPtr, IRB, IRB.getInt8Ty(),
  4300. Align(8), /*isStore*/ true)
  4301. .first;
  4302. Value *GrSrcPtr = IRB.CreateInBoundsGEP(IRB.getInt8Ty(), VAArgTLSCopy,
  4303. GrRegSaveAreaShadowPtrOff);
  4304. Value *GrCopySize = IRB.CreateSub(GrArgSize, GrRegSaveAreaShadowPtrOff);
  4305. IRB.CreateMemCpy(GrRegSaveAreaShadowPtr, Align(8), GrSrcPtr, Align(8),
  4306. GrCopySize);
  4307. // Again, but for FP/SIMD values.
  4308. Value *VrRegSaveAreaShadowPtrOff =
  4309. IRB.CreateAdd(VrArgSize, VrOffSaveArea);
  4310. Value *VrRegSaveAreaShadowPtr =
  4311. MSV.getShadowOriginPtr(VrRegSaveAreaPtr, IRB, IRB.getInt8Ty(),
  4312. Align(8), /*isStore*/ true)
  4313. .first;
  4314. Value *VrSrcPtr = IRB.CreateInBoundsGEP(
  4315. IRB.getInt8Ty(),
  4316. IRB.CreateInBoundsGEP(IRB.getInt8Ty(), VAArgTLSCopy,
  4317. IRB.getInt32(AArch64VrBegOffset)),
  4318. VrRegSaveAreaShadowPtrOff);
  4319. Value *VrCopySize = IRB.CreateSub(VrArgSize, VrRegSaveAreaShadowPtrOff);
  4320. IRB.CreateMemCpy(VrRegSaveAreaShadowPtr, Align(8), VrSrcPtr, Align(8),
  4321. VrCopySize);
  4322. // And finally for remaining arguments.
  4323. Value *StackSaveAreaShadowPtr =
  4324. MSV.getShadowOriginPtr(StackSaveAreaPtr, IRB, IRB.getInt8Ty(),
  4325. Align(16), /*isStore*/ true)
  4326. .first;
  4327. Value *StackSrcPtr =
  4328. IRB.CreateInBoundsGEP(IRB.getInt8Ty(), VAArgTLSCopy,
  4329. IRB.getInt32(AArch64VAEndOffset));
  4330. IRB.CreateMemCpy(StackSaveAreaShadowPtr, Align(16), StackSrcPtr,
  4331. Align(16), VAArgOverflowSize);
  4332. }
  4333. }
  4334. };
  4335. /// PowerPC64-specific implementation of VarArgHelper.
  4336. struct VarArgPowerPC64Helper : public VarArgHelper {
  4337. Function &F;
  4338. MemorySanitizer &MS;
  4339. MemorySanitizerVisitor &MSV;
  4340. Value *VAArgTLSCopy = nullptr;
  4341. Value *VAArgSize = nullptr;
  4342. SmallVector<CallInst*, 16> VAStartInstrumentationList;
  4343. VarArgPowerPC64Helper(Function &F, MemorySanitizer &MS,
  4344. MemorySanitizerVisitor &MSV) : F(F), MS(MS), MSV(MSV) {}
  4345. void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {
  4346. // For PowerPC, we need to deal with alignment of stack arguments -
  4347. // they are mostly aligned to 8 bytes, but vectors and i128 arrays
  4348. // are aligned to 16 bytes, byvals can be aligned to 8 or 16 bytes,
  4349. // For that reason, we compute current offset from stack pointer (which is
  4350. // always properly aligned), and offset for the first vararg, then subtract
  4351. // them.
  4352. unsigned VAArgBase;
  4353. Triple TargetTriple(F.getParent()->getTargetTriple());
  4354. // Parameter save area starts at 48 bytes from frame pointer for ABIv1,
  4355. // and 32 bytes for ABIv2. This is usually determined by target
  4356. // endianness, but in theory could be overridden by function attribute.
  4357. if (TargetTriple.getArch() == Triple::ppc64)
  4358. VAArgBase = 48;
  4359. else
  4360. VAArgBase = 32;
  4361. unsigned VAArgOffset = VAArgBase;
  4362. const DataLayout &DL = F.getParent()->getDataLayout();
  4363. for (auto ArgIt = CB.arg_begin(), End = CB.arg_end(); ArgIt != End;
  4364. ++ArgIt) {
  4365. Value *A = *ArgIt;
  4366. unsigned ArgNo = CB.getArgOperandNo(ArgIt);
  4367. bool IsFixed = ArgNo < CB.getFunctionType()->getNumParams();
  4368. bool IsByVal = CB.paramHasAttr(ArgNo, Attribute::ByVal);
  4369. if (IsByVal) {
  4370. assert(A->getType()->isPointerTy());
  4371. Type *RealTy = CB.getParamByValType(ArgNo);
  4372. uint64_t ArgSize = DL.getTypeAllocSize(RealTy);
  4373. MaybeAlign ArgAlign = CB.getParamAlign(ArgNo);
  4374. if (!ArgAlign || *ArgAlign < Align(8))
  4375. ArgAlign = Align(8);
  4376. VAArgOffset = alignTo(VAArgOffset, ArgAlign);
  4377. if (!IsFixed) {
  4378. Value *Base = getShadowPtrForVAArgument(
  4379. RealTy, IRB, VAArgOffset - VAArgBase, ArgSize);
  4380. if (Base) {
  4381. Value *AShadowPtr, *AOriginPtr;
  4382. std::tie(AShadowPtr, AOriginPtr) =
  4383. MSV.getShadowOriginPtr(A, IRB, IRB.getInt8Ty(),
  4384. kShadowTLSAlignment, /*isStore*/ false);
  4385. IRB.CreateMemCpy(Base, kShadowTLSAlignment, AShadowPtr,
  4386. kShadowTLSAlignment, ArgSize);
  4387. }
  4388. }
  4389. VAArgOffset += alignTo(ArgSize, 8);
  4390. } else {
  4391. Value *Base;
  4392. uint64_t ArgSize = DL.getTypeAllocSize(A->getType());
  4393. uint64_t ArgAlign = 8;
  4394. if (A->getType()->isArrayTy()) {
  4395. // Arrays are aligned to element size, except for long double
  4396. // arrays, which are aligned to 8 bytes.
  4397. Type *ElementTy = A->getType()->getArrayElementType();
  4398. if (!ElementTy->isPPC_FP128Ty())
  4399. ArgAlign = DL.getTypeAllocSize(ElementTy);
  4400. } else if (A->getType()->isVectorTy()) {
  4401. // Vectors are naturally aligned.
  4402. ArgAlign = DL.getTypeAllocSize(A->getType());
  4403. }
  4404. if (ArgAlign < 8)
  4405. ArgAlign = 8;
  4406. VAArgOffset = alignTo(VAArgOffset, ArgAlign);
  4407. if (DL.isBigEndian()) {
  4408. // Adjusting the shadow for argument with size < 8 to match the placement
  4409. // of bits in big endian system
  4410. if (ArgSize < 8)
  4411. VAArgOffset += (8 - ArgSize);
  4412. }
  4413. if (!IsFixed) {
  4414. Base = getShadowPtrForVAArgument(A->getType(), IRB,
  4415. VAArgOffset - VAArgBase, ArgSize);
  4416. if (Base)
  4417. IRB.CreateAlignedStore(MSV.getShadow(A), Base, kShadowTLSAlignment);
  4418. }
  4419. VAArgOffset += ArgSize;
  4420. VAArgOffset = alignTo(VAArgOffset, 8);
  4421. }
  4422. if (IsFixed)
  4423. VAArgBase = VAArgOffset;
  4424. }
  4425. Constant *TotalVAArgSize = ConstantInt::get(IRB.getInt64Ty(),
  4426. VAArgOffset - VAArgBase);
  4427. // Here using VAArgOverflowSizeTLS as VAArgSizeTLS to avoid creation of
  4428. // a new class member i.e. it is the total size of all VarArgs.
  4429. IRB.CreateStore(TotalVAArgSize, MS.VAArgOverflowSizeTLS);
  4430. }
  4431. /// Compute the shadow address for a given va_arg.
  4432. Value *getShadowPtrForVAArgument(Type *Ty, IRBuilder<> &IRB,
  4433. unsigned ArgOffset, unsigned ArgSize) {
  4434. // Make sure we don't overflow __msan_va_arg_tls.
  4435. if (ArgOffset + ArgSize > kParamTLSSize)
  4436. return nullptr;
  4437. Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
  4438. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  4439. return IRB.CreateIntToPtr(Base, PointerType::get(MSV.getShadowTy(Ty), 0),
  4440. "_msarg");
  4441. }
  4442. void visitVAStartInst(VAStartInst &I) override {
  4443. IRBuilder<> IRB(&I);
  4444. VAStartInstrumentationList.push_back(&I);
  4445. Value *VAListTag = I.getArgOperand(0);
  4446. Value *ShadowPtr, *OriginPtr;
  4447. const Align Alignment = Align(8);
  4448. std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
  4449. VAListTag, IRB, IRB.getInt8Ty(), Alignment, /*isStore*/ true);
  4450. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  4451. /* size */ 8, Alignment, false);
  4452. }
  4453. void visitVACopyInst(VACopyInst &I) override {
  4454. IRBuilder<> IRB(&I);
  4455. Value *VAListTag = I.getArgOperand(0);
  4456. Value *ShadowPtr, *OriginPtr;
  4457. const Align Alignment = Align(8);
  4458. std::tie(ShadowPtr, OriginPtr) = MSV.getShadowOriginPtr(
  4459. VAListTag, IRB, IRB.getInt8Ty(), Alignment, /*isStore*/ true);
  4460. // Unpoison the whole __va_list_tag.
  4461. // FIXME: magic ABI constants.
  4462. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  4463. /* size */ 8, Alignment, false);
  4464. }
  4465. void finalizeInstrumentation() override {
  4466. assert(!VAArgSize && !VAArgTLSCopy &&
  4467. "finalizeInstrumentation called twice");
  4468. IRBuilder<> IRB(MSV.FnPrologueEnd);
  4469. VAArgSize = IRB.CreateLoad(IRB.getInt64Ty(), MS.VAArgOverflowSizeTLS);
  4470. Value *CopySize = IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, 0),
  4471. VAArgSize);
  4472. if (!VAStartInstrumentationList.empty()) {
  4473. // If there is a va_start in this function, make a backup copy of
  4474. // va_arg_tls somewhere in the function entry block.
  4475. VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
  4476. IRB.CreateMemCpy(VAArgTLSCopy, Align(8), MS.VAArgTLS, Align(8), CopySize);
  4477. }
  4478. // Instrument va_start.
  4479. // Copy va_list shadow from the backup copy of the TLS contents.
  4480. for (size_t i = 0, n = VAStartInstrumentationList.size(); i < n; i++) {
  4481. CallInst *OrigInst = VAStartInstrumentationList[i];
  4482. IRBuilder<> IRB(OrigInst->getNextNode());
  4483. Value *VAListTag = OrigInst->getArgOperand(0);
  4484. Type *RegSaveAreaPtrTy = Type::getInt64PtrTy(*MS.C);
  4485. Value *RegSaveAreaPtrPtr =
  4486. IRB.CreateIntToPtr(IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  4487. PointerType::get(RegSaveAreaPtrTy, 0));
  4488. Value *RegSaveAreaPtr =
  4489. IRB.CreateLoad(RegSaveAreaPtrTy, RegSaveAreaPtrPtr);
  4490. Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
  4491. const Align Alignment = Align(8);
  4492. std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
  4493. MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.getInt8Ty(),
  4494. Alignment, /*isStore*/ true);
  4495. IRB.CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
  4496. CopySize);
  4497. }
  4498. }
  4499. };
  4500. /// SystemZ-specific implementation of VarArgHelper.
  4501. struct VarArgSystemZHelper : public VarArgHelper {
  4502. static const unsigned SystemZGpOffset = 16;
  4503. static const unsigned SystemZGpEndOffset = 56;
  4504. static const unsigned SystemZFpOffset = 128;
  4505. static const unsigned SystemZFpEndOffset = 160;
  4506. static const unsigned SystemZMaxVrArgs = 8;
  4507. static const unsigned SystemZRegSaveAreaSize = 160;
  4508. static const unsigned SystemZOverflowOffset = 160;
  4509. static const unsigned SystemZVAListTagSize = 32;
  4510. static const unsigned SystemZOverflowArgAreaPtrOffset = 16;
  4511. static const unsigned SystemZRegSaveAreaPtrOffset = 24;
  4512. Function &F;
  4513. MemorySanitizer &MS;
  4514. MemorySanitizerVisitor &MSV;
  4515. Value *VAArgTLSCopy = nullptr;
  4516. Value *VAArgTLSOriginCopy = nullptr;
  4517. Value *VAArgOverflowSize = nullptr;
  4518. SmallVector<CallInst *, 16> VAStartInstrumentationList;
  4519. enum class ArgKind {
  4520. GeneralPurpose,
  4521. FloatingPoint,
  4522. Vector,
  4523. Memory,
  4524. Indirect,
  4525. };
  4526. enum class ShadowExtension { None, Zero, Sign };
  4527. VarArgSystemZHelper(Function &F, MemorySanitizer &MS,
  4528. MemorySanitizerVisitor &MSV)
  4529. : F(F), MS(MS), MSV(MSV) {}
  4530. ArgKind classifyArgument(Type *T, bool IsSoftFloatABI) {
  4531. // T is a SystemZABIInfo::classifyArgumentType() output, and there are
  4532. // only a few possibilities of what it can be. In particular, enums, single
  4533. // element structs and large types have already been taken care of.
  4534. // Some i128 and fp128 arguments are converted to pointers only in the
  4535. // back end.
  4536. if (T->isIntegerTy(128) || T->isFP128Ty())
  4537. return ArgKind::Indirect;
  4538. if (T->isFloatingPointTy())
  4539. return IsSoftFloatABI ? ArgKind::GeneralPurpose : ArgKind::FloatingPoint;
  4540. if (T->isIntegerTy() || T->isPointerTy())
  4541. return ArgKind::GeneralPurpose;
  4542. if (T->isVectorTy())
  4543. return ArgKind::Vector;
  4544. return ArgKind::Memory;
  4545. }
  4546. ShadowExtension getShadowExtension(const CallBase &CB, unsigned ArgNo) {
  4547. // ABI says: "One of the simple integer types no more than 64 bits wide.
  4548. // ... If such an argument is shorter than 64 bits, replace it by a full
  4549. // 64-bit integer representing the same number, using sign or zero
  4550. // extension". Shadow for an integer argument has the same type as the
  4551. // argument itself, so it can be sign or zero extended as well.
  4552. bool ZExt = CB.paramHasAttr(ArgNo, Attribute::ZExt);
  4553. bool SExt = CB.paramHasAttr(ArgNo, Attribute::SExt);
  4554. if (ZExt) {
  4555. assert(!SExt);
  4556. return ShadowExtension::Zero;
  4557. }
  4558. if (SExt) {
  4559. assert(!ZExt);
  4560. return ShadowExtension::Sign;
  4561. }
  4562. return ShadowExtension::None;
  4563. }
  4564. void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {
  4565. bool IsSoftFloatABI = CB.getCalledFunction()
  4566. ->getFnAttribute("use-soft-float")
  4567. .getValueAsBool();
  4568. unsigned GpOffset = SystemZGpOffset;
  4569. unsigned FpOffset = SystemZFpOffset;
  4570. unsigned VrIndex = 0;
  4571. unsigned OverflowOffset = SystemZOverflowOffset;
  4572. const DataLayout &DL = F.getParent()->getDataLayout();
  4573. for (auto ArgIt = CB.arg_begin(), End = CB.arg_end(); ArgIt != End;
  4574. ++ArgIt) {
  4575. Value *A = *ArgIt;
  4576. unsigned ArgNo = CB.getArgOperandNo(ArgIt);
  4577. bool IsFixed = ArgNo < CB.getFunctionType()->getNumParams();
  4578. // SystemZABIInfo does not produce ByVal parameters.
  4579. assert(!CB.paramHasAttr(ArgNo, Attribute::ByVal));
  4580. Type *T = A->getType();
  4581. ArgKind AK = classifyArgument(T, IsSoftFloatABI);
  4582. if (AK == ArgKind::Indirect) {
  4583. T = PointerType::get(T, 0);
  4584. AK = ArgKind::GeneralPurpose;
  4585. }
  4586. if (AK == ArgKind::GeneralPurpose && GpOffset >= SystemZGpEndOffset)
  4587. AK = ArgKind::Memory;
  4588. if (AK == ArgKind::FloatingPoint && FpOffset >= SystemZFpEndOffset)
  4589. AK = ArgKind::Memory;
  4590. if (AK == ArgKind::Vector && (VrIndex >= SystemZMaxVrArgs || !IsFixed))
  4591. AK = ArgKind::Memory;
  4592. Value *ShadowBase = nullptr;
  4593. Value *OriginBase = nullptr;
  4594. ShadowExtension SE = ShadowExtension::None;
  4595. switch (AK) {
  4596. case ArgKind::GeneralPurpose: {
  4597. // Always keep track of GpOffset, but store shadow only for varargs.
  4598. uint64_t ArgSize = 8;
  4599. if (GpOffset + ArgSize <= kParamTLSSize) {
  4600. if (!IsFixed) {
  4601. SE = getShadowExtension(CB, ArgNo);
  4602. uint64_t GapSize = 0;
  4603. if (SE == ShadowExtension::None) {
  4604. uint64_t ArgAllocSize = DL.getTypeAllocSize(T);
  4605. assert(ArgAllocSize <= ArgSize);
  4606. GapSize = ArgSize - ArgAllocSize;
  4607. }
  4608. ShadowBase = getShadowAddrForVAArgument(IRB, GpOffset + GapSize);
  4609. if (MS.TrackOrigins)
  4610. OriginBase = getOriginPtrForVAArgument(IRB, GpOffset + GapSize);
  4611. }
  4612. GpOffset += ArgSize;
  4613. } else {
  4614. GpOffset = kParamTLSSize;
  4615. }
  4616. break;
  4617. }
  4618. case ArgKind::FloatingPoint: {
  4619. // Always keep track of FpOffset, but store shadow only for varargs.
  4620. uint64_t ArgSize = 8;
  4621. if (FpOffset + ArgSize <= kParamTLSSize) {
  4622. if (!IsFixed) {
  4623. // PoP says: "A short floating-point datum requires only the
  4624. // left-most 32 bit positions of a floating-point register".
  4625. // Therefore, in contrast to AK_GeneralPurpose and AK_Memory,
  4626. // don't extend shadow and don't mind the gap.
  4627. ShadowBase = getShadowAddrForVAArgument(IRB, FpOffset);
  4628. if (MS.TrackOrigins)
  4629. OriginBase = getOriginPtrForVAArgument(IRB, FpOffset);
  4630. }
  4631. FpOffset += ArgSize;
  4632. } else {
  4633. FpOffset = kParamTLSSize;
  4634. }
  4635. break;
  4636. }
  4637. case ArgKind::Vector: {
  4638. // Keep track of VrIndex. No need to store shadow, since vector varargs
  4639. // go through AK_Memory.
  4640. assert(IsFixed);
  4641. VrIndex++;
  4642. break;
  4643. }
  4644. case ArgKind::Memory: {
  4645. // Keep track of OverflowOffset and store shadow only for varargs.
  4646. // Ignore fixed args, since we need to copy only the vararg portion of
  4647. // the overflow area shadow.
  4648. if (!IsFixed) {
  4649. uint64_t ArgAllocSize = DL.getTypeAllocSize(T);
  4650. uint64_t ArgSize = alignTo(ArgAllocSize, 8);
  4651. if (OverflowOffset + ArgSize <= kParamTLSSize) {
  4652. SE = getShadowExtension(CB, ArgNo);
  4653. uint64_t GapSize =
  4654. SE == ShadowExtension::None ? ArgSize - ArgAllocSize : 0;
  4655. ShadowBase =
  4656. getShadowAddrForVAArgument(IRB, OverflowOffset + GapSize);
  4657. if (MS.TrackOrigins)
  4658. OriginBase =
  4659. getOriginPtrForVAArgument(IRB, OverflowOffset + GapSize);
  4660. OverflowOffset += ArgSize;
  4661. } else {
  4662. OverflowOffset = kParamTLSSize;
  4663. }
  4664. }
  4665. break;
  4666. }
  4667. case ArgKind::Indirect:
  4668. llvm_unreachable("Indirect must be converted to GeneralPurpose");
  4669. }
  4670. if (ShadowBase == nullptr)
  4671. continue;
  4672. Value *Shadow = MSV.getShadow(A);
  4673. if (SE != ShadowExtension::None)
  4674. Shadow = MSV.CreateShadowCast(IRB, Shadow, IRB.getInt64Ty(),
  4675. /*Signed*/ SE == ShadowExtension::Sign);
  4676. ShadowBase = IRB.CreateIntToPtr(
  4677. ShadowBase, PointerType::get(Shadow->getType(), 0), "_msarg_va_s");
  4678. IRB.CreateStore(Shadow, ShadowBase);
  4679. if (MS.TrackOrigins) {
  4680. Value *Origin = MSV.getOrigin(A);
  4681. unsigned StoreSize = DL.getTypeStoreSize(Shadow->getType());
  4682. MSV.paintOrigin(IRB, Origin, OriginBase, StoreSize,
  4683. kMinOriginAlignment);
  4684. }
  4685. }
  4686. Constant *OverflowSize = ConstantInt::get(
  4687. IRB.getInt64Ty(), OverflowOffset - SystemZOverflowOffset);
  4688. IRB.CreateStore(OverflowSize, MS.VAArgOverflowSizeTLS);
  4689. }
  4690. Value *getShadowAddrForVAArgument(IRBuilder<> &IRB, unsigned ArgOffset) {
  4691. Value *Base = IRB.CreatePointerCast(MS.VAArgTLS, MS.IntptrTy);
  4692. return IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  4693. }
  4694. Value *getOriginPtrForVAArgument(IRBuilder<> &IRB, int ArgOffset) {
  4695. Value *Base = IRB.CreatePointerCast(MS.VAArgOriginTLS, MS.IntptrTy);
  4696. Base = IRB.CreateAdd(Base, ConstantInt::get(MS.IntptrTy, ArgOffset));
  4697. return IRB.CreateIntToPtr(Base, PointerType::get(MS.OriginTy, 0),
  4698. "_msarg_va_o");
  4699. }
  4700. void unpoisonVAListTagForInst(IntrinsicInst &I) {
  4701. IRBuilder<> IRB(&I);
  4702. Value *VAListTag = I.getArgOperand(0);
  4703. Value *ShadowPtr, *OriginPtr;
  4704. const Align Alignment = Align(8);
  4705. std::tie(ShadowPtr, OriginPtr) =
  4706. MSV.getShadowOriginPtr(VAListTag, IRB, IRB.getInt8Ty(), Alignment,
  4707. /*isStore*/ true);
  4708. IRB.CreateMemSet(ShadowPtr, Constant::getNullValue(IRB.getInt8Ty()),
  4709. SystemZVAListTagSize, Alignment, false);
  4710. }
  4711. void visitVAStartInst(VAStartInst &I) override {
  4712. VAStartInstrumentationList.push_back(&I);
  4713. unpoisonVAListTagForInst(I);
  4714. }
  4715. void visitVACopyInst(VACopyInst &I) override { unpoisonVAListTagForInst(I); }
  4716. void copyRegSaveArea(IRBuilder<> &IRB, Value *VAListTag) {
  4717. Type *RegSaveAreaPtrTy = Type::getInt64PtrTy(*MS.C);
  4718. Value *RegSaveAreaPtrPtr = IRB.CreateIntToPtr(
  4719. IRB.CreateAdd(
  4720. IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  4721. ConstantInt::get(MS.IntptrTy, SystemZRegSaveAreaPtrOffset)),
  4722. PointerType::get(RegSaveAreaPtrTy, 0));
  4723. Value *RegSaveAreaPtr = IRB.CreateLoad(RegSaveAreaPtrTy, RegSaveAreaPtrPtr);
  4724. Value *RegSaveAreaShadowPtr, *RegSaveAreaOriginPtr;
  4725. const Align Alignment = Align(8);
  4726. std::tie(RegSaveAreaShadowPtr, RegSaveAreaOriginPtr) =
  4727. MSV.getShadowOriginPtr(RegSaveAreaPtr, IRB, IRB.getInt8Ty(), Alignment,
  4728. /*isStore*/ true);
  4729. // TODO(iii): copy only fragments filled by visitCallBase()
  4730. IRB.CreateMemCpy(RegSaveAreaShadowPtr, Alignment, VAArgTLSCopy, Alignment,
  4731. SystemZRegSaveAreaSize);
  4732. if (MS.TrackOrigins)
  4733. IRB.CreateMemCpy(RegSaveAreaOriginPtr, Alignment, VAArgTLSOriginCopy,
  4734. Alignment, SystemZRegSaveAreaSize);
  4735. }
  4736. void copyOverflowArea(IRBuilder<> &IRB, Value *VAListTag) {
  4737. Type *OverflowArgAreaPtrTy = Type::getInt64PtrTy(*MS.C);
  4738. Value *OverflowArgAreaPtrPtr = IRB.CreateIntToPtr(
  4739. IRB.CreateAdd(
  4740. IRB.CreatePtrToInt(VAListTag, MS.IntptrTy),
  4741. ConstantInt::get(MS.IntptrTy, SystemZOverflowArgAreaPtrOffset)),
  4742. PointerType::get(OverflowArgAreaPtrTy, 0));
  4743. Value *OverflowArgAreaPtr =
  4744. IRB.CreateLoad(OverflowArgAreaPtrTy, OverflowArgAreaPtrPtr);
  4745. Value *OverflowArgAreaShadowPtr, *OverflowArgAreaOriginPtr;
  4746. const Align Alignment = Align(8);
  4747. std::tie(OverflowArgAreaShadowPtr, OverflowArgAreaOriginPtr) =
  4748. MSV.getShadowOriginPtr(OverflowArgAreaPtr, IRB, IRB.getInt8Ty(),
  4749. Alignment, /*isStore*/ true);
  4750. Value *SrcPtr = IRB.CreateConstGEP1_32(IRB.getInt8Ty(), VAArgTLSCopy,
  4751. SystemZOverflowOffset);
  4752. IRB.CreateMemCpy(OverflowArgAreaShadowPtr, Alignment, SrcPtr, Alignment,
  4753. VAArgOverflowSize);
  4754. if (MS.TrackOrigins) {
  4755. SrcPtr = IRB.CreateConstGEP1_32(IRB.getInt8Ty(), VAArgTLSOriginCopy,
  4756. SystemZOverflowOffset);
  4757. IRB.CreateMemCpy(OverflowArgAreaOriginPtr, Alignment, SrcPtr, Alignment,
  4758. VAArgOverflowSize);
  4759. }
  4760. }
  4761. void finalizeInstrumentation() override {
  4762. assert(!VAArgOverflowSize && !VAArgTLSCopy &&
  4763. "finalizeInstrumentation called twice");
  4764. if (!VAStartInstrumentationList.empty()) {
  4765. // If there is a va_start in this function, make a backup copy of
  4766. // va_arg_tls somewhere in the function entry block.
  4767. IRBuilder<> IRB(MSV.FnPrologueEnd);
  4768. VAArgOverflowSize =
  4769. IRB.CreateLoad(IRB.getInt64Ty(), MS.VAArgOverflowSizeTLS);
  4770. Value *CopySize =
  4771. IRB.CreateAdd(ConstantInt::get(MS.IntptrTy, SystemZOverflowOffset),
  4772. VAArgOverflowSize);
  4773. VAArgTLSCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
  4774. IRB.CreateMemCpy(VAArgTLSCopy, Align(8), MS.VAArgTLS, Align(8), CopySize);
  4775. if (MS.TrackOrigins) {
  4776. VAArgTLSOriginCopy = IRB.CreateAlloca(Type::getInt8Ty(*MS.C), CopySize);
  4777. IRB.CreateMemCpy(VAArgTLSOriginCopy, Align(8), MS.VAArgOriginTLS,
  4778. Align(8), CopySize);
  4779. }
  4780. }
  4781. // Instrument va_start.
  4782. // Copy va_list shadow from the backup copy of the TLS contents.
  4783. for (size_t VaStartNo = 0, VaStartNum = VAStartInstrumentationList.size();
  4784. VaStartNo < VaStartNum; VaStartNo++) {
  4785. CallInst *OrigInst = VAStartInstrumentationList[VaStartNo];
  4786. IRBuilder<> IRB(OrigInst->getNextNode());
  4787. Value *VAListTag = OrigInst->getArgOperand(0);
  4788. copyRegSaveArea(IRB, VAListTag);
  4789. copyOverflowArea(IRB, VAListTag);
  4790. }
  4791. }
  4792. };
  4793. /// A no-op implementation of VarArgHelper.
  4794. struct VarArgNoOpHelper : public VarArgHelper {
  4795. VarArgNoOpHelper(Function &F, MemorySanitizer &MS,
  4796. MemorySanitizerVisitor &MSV) {}
  4797. void visitCallBase(CallBase &CB, IRBuilder<> &IRB) override {}
  4798. void visitVAStartInst(VAStartInst &I) override {}
  4799. void visitVACopyInst(VACopyInst &I) override {}
  4800. void finalizeInstrumentation() override {}
  4801. };
  4802. } // end anonymous namespace
  4803. static VarArgHelper *CreateVarArgHelper(Function &Func, MemorySanitizer &Msan,
  4804. MemorySanitizerVisitor &Visitor) {
  4805. // VarArg handling is only implemented on AMD64. False positives are possible
  4806. // on other platforms.
  4807. Triple TargetTriple(Func.getParent()->getTargetTriple());
  4808. if (TargetTriple.getArch() == Triple::x86_64)
  4809. return new VarArgAMD64Helper(Func, Msan, Visitor);
  4810. else if (TargetTriple.isMIPS64())
  4811. return new VarArgMIPS64Helper(Func, Msan, Visitor);
  4812. else if (TargetTriple.getArch() == Triple::aarch64)
  4813. return new VarArgAArch64Helper(Func, Msan, Visitor);
  4814. else if (TargetTriple.getArch() == Triple::ppc64 ||
  4815. TargetTriple.getArch() == Triple::ppc64le)
  4816. return new VarArgPowerPC64Helper(Func, Msan, Visitor);
  4817. else if (TargetTriple.getArch() == Triple::systemz)
  4818. return new VarArgSystemZHelper(Func, Msan, Visitor);
  4819. else
  4820. return new VarArgNoOpHelper(Func, Msan, Visitor);
  4821. }
  4822. bool MemorySanitizer::sanitizeFunction(Function &F, TargetLibraryInfo &TLI) {
  4823. if (!CompileKernel && F.getName() == kMsanModuleCtorName)
  4824. return false;
  4825. if (F.hasFnAttribute(Attribute::DisableSanitizerInstrumentation))
  4826. return false;
  4827. MemorySanitizerVisitor Visitor(F, *this, TLI);
  4828. // Clear out readonly/readnone attributes.
  4829. AttributeMask B;
  4830. B.addAttribute(Attribute::ReadOnly)
  4831. .addAttribute(Attribute::ReadNone)
  4832. .addAttribute(Attribute::WriteOnly)
  4833. .addAttribute(Attribute::ArgMemOnly)
  4834. .addAttribute(Attribute::Speculatable);
  4835. F.removeFnAttrs(B);
  4836. return Visitor.runOnFunction();
  4837. }