sanitizer_common_interceptors.inc 367 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695
  1. //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
  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. // Common function interceptors for tools like AddressSanitizer,
  10. // ThreadSanitizer, MemorySanitizer, etc.
  11. //
  12. // This file should be included into the tool's interceptor file,
  13. // which has to define its own macros:
  14. // COMMON_INTERCEPTOR_ENTER
  15. // COMMON_INTERCEPTOR_ENTER_NOIGNORE
  16. // COMMON_INTERCEPTOR_READ_RANGE
  17. // COMMON_INTERCEPTOR_WRITE_RANGE
  18. // COMMON_INTERCEPTOR_INITIALIZE_RANGE
  19. // COMMON_INTERCEPTOR_DIR_ACQUIRE
  20. // COMMON_INTERCEPTOR_FD_ACQUIRE
  21. // COMMON_INTERCEPTOR_FD_RELEASE
  22. // COMMON_INTERCEPTOR_FD_ACCESS
  23. // COMMON_INTERCEPTOR_SET_THREAD_NAME
  24. // COMMON_INTERCEPTOR_DLOPEN
  25. // COMMON_INTERCEPTOR_ON_EXIT
  26. // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
  27. // COMMON_INTERCEPTOR_HANDLE_RECVMSG
  28. // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
  29. // COMMON_INTERCEPTOR_MEMSET_IMPL
  30. // COMMON_INTERCEPTOR_MEMMOVE_IMPL
  31. // COMMON_INTERCEPTOR_MEMCPY_IMPL
  32. // COMMON_INTERCEPTOR_MMAP_IMPL
  33. // COMMON_INTERCEPTOR_COPY_STRING
  34. // COMMON_INTERCEPTOR_STRNDUP_IMPL
  35. // COMMON_INTERCEPTOR_STRERROR
  36. //===----------------------------------------------------------------------===//
  37. #include "interception/interception.h"
  38. #include "sanitizer_addrhashmap.h"
  39. #include "sanitizer_errno.h"
  40. #include "sanitizer_placement_new.h"
  41. #include "sanitizer_platform_interceptors.h"
  42. #include "sanitizer_symbolizer.h"
  43. #include "sanitizer_tls_get_addr.h"
  44. #include <stdarg.h>
  45. #if SANITIZER_INTERCEPTOR_HOOKS
  46. #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...) f(__VA_ARGS__);
  47. #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...) \
  48. SANITIZER_INTERFACE_WEAK_DEF(void, f, __VA_ARGS__) {}
  49. #else
  50. #define DECLARE_WEAK_INTERCEPTOR_HOOK(f, ...)
  51. #define CALL_WEAK_INTERCEPTOR_HOOK(f, ...)
  52. #endif // SANITIZER_INTERCEPTOR_HOOKS
  53. #if SANITIZER_WINDOWS && !defined(va_copy)
  54. #define va_copy(dst, src) ((dst) = (src))
  55. #endif // _WIN32
  56. #if SANITIZER_FREEBSD
  57. #define pthread_setname_np pthread_set_name_np
  58. #define inet_aton __inet_aton
  59. #define inet_pton __inet_pton
  60. #define iconv __bsd_iconv
  61. #endif
  62. #if SANITIZER_NETBSD
  63. #define clock_getres __clock_getres50
  64. #define clock_gettime __clock_gettime50
  65. #define clock_settime __clock_settime50
  66. #define ctime __ctime50
  67. #define ctime_r __ctime_r50
  68. #define devname __devname50
  69. #define fgetpos __fgetpos50
  70. #define fsetpos __fsetpos50
  71. #define fstatvfs __fstatvfs90
  72. #define fstatvfs1 __fstatvfs190
  73. #define fts_children __fts_children60
  74. #define fts_close __fts_close60
  75. #define fts_open __fts_open60
  76. #define fts_read __fts_read60
  77. #define fts_set __fts_set60
  78. #define getitimer __getitimer50
  79. #define getmntinfo __getmntinfo90
  80. #define getpwent __getpwent50
  81. #define getpwnam __getpwnam50
  82. #define getpwnam_r __getpwnam_r50
  83. #define getpwuid __getpwuid50
  84. #define getpwuid_r __getpwuid_r50
  85. #define getutent __getutent50
  86. #define getutxent __getutxent50
  87. #define getutxid __getutxid50
  88. #define getutxline __getutxline50
  89. #define getvfsstat __getvfsstat90
  90. #define pututxline __pututxline50
  91. #define glob __glob30
  92. #define gmtime __gmtime50
  93. #define gmtime_r __gmtime_r50
  94. #define localtime __locatime50
  95. #define localtime_r __localtime_r50
  96. #define mktime __mktime50
  97. #define lstat __lstat50
  98. #define opendir __opendir30
  99. #define readdir __readdir30
  100. #define readdir_r __readdir_r30
  101. #define scandir __scandir30
  102. #define setitimer __setitimer50
  103. #define setlocale __setlocale50
  104. #define shmctl __shmctl50
  105. #define sigaltstack __sigaltstack14
  106. #define sigemptyset __sigemptyset14
  107. #define sigfillset __sigfillset14
  108. #define sigpending __sigpending14
  109. #define sigprocmask __sigprocmask14
  110. #define sigtimedwait __sigtimedwait50
  111. #define stat __stat50
  112. #define statvfs __statvfs90
  113. #define statvfs1 __statvfs190
  114. #define time __time50
  115. #define times __times13
  116. #define unvis __unvis50
  117. #define wait3 __wait350
  118. #define wait4 __wait450
  119. extern const unsigned short *_ctype_tab_;
  120. extern const short *_toupper_tab_;
  121. extern const short *_tolower_tab_;
  122. #endif
  123. #if SANITIZER_MUSL && \
  124. (defined(__i386__) || defined(__arm__) || SANITIZER_MIPS32 || SANITIZER_PPC32)
  125. // musl 1.2.0 on existing 32-bit architectures uses new symbol names for the
  126. // time-related functions that take 64-bit time_t values. See
  127. // https://musl.libc.org/time64.html
  128. #define adjtime __adjtime64
  129. #define adjtimex __adjtimex_time64
  130. #define aio_suspend __aio_suspend_time64
  131. #define clock_adjtime __clock_adjtime64
  132. #define clock_getres __clock_getres_time64
  133. #define clock_gettime __clock_gettime64
  134. #define clock_nanosleep __clock_nanosleep_time64
  135. #define clock_settime __clock_settime64
  136. #define cnd_timedwait __cnd_timedwait_time64
  137. #define ctime __ctime64
  138. #define ctime_r __ctime64_r
  139. #define difftime __difftime64
  140. #define dlsym __dlsym_time64
  141. #define fstatat __fstatat_time64
  142. #define fstat __fstat_time64
  143. #define ftime __ftime64
  144. #define futimens __futimens_time64
  145. #define futimesat __futimesat_time64
  146. #define futimes __futimes_time64
  147. #define getitimer __getitimer_time64
  148. #define getrusage __getrusage_time64
  149. #define gettimeofday __gettimeofday_time64
  150. #define gmtime __gmtime64
  151. #define gmtime_r __gmtime64_r
  152. #define localtime __localtime64
  153. #define localtime_r __localtime64_r
  154. #define lstat __lstat_time64
  155. #define lutimes __lutimes_time64
  156. #define mktime __mktime64
  157. #define mq_timedreceive __mq_timedreceive_time64
  158. #define mq_timedsend __mq_timedsend_time64
  159. #define mtx_timedlock __mtx_timedlock_time64
  160. #define nanosleep __nanosleep_time64
  161. #define ppoll __ppoll_time64
  162. #define pselect __pselect_time64
  163. #define pthread_cond_timedwait __pthread_cond_timedwait_time64
  164. #define pthread_mutex_timedlock __pthread_mutex_timedlock_time64
  165. #define pthread_rwlock_timedrdlock __pthread_rwlock_timedrdlock_time64
  166. #define pthread_rwlock_timedwrlock __pthread_rwlock_timedwrlock_time64
  167. #define pthread_timedjoin_np __pthread_timedjoin_np_time64
  168. #define recvmmsg __recvmmsg_time64
  169. #define sched_rr_get_interval __sched_rr_get_interval_time64
  170. #define select __select_time64
  171. #define semtimedop __semtimedop_time64
  172. #define sem_timedwait __sem_timedwait_time64
  173. #define setitimer __setitimer_time64
  174. #define settimeofday __settimeofday_time64
  175. #define sigtimedwait __sigtimedwait_time64
  176. #define stat __stat_time64
  177. #define stime __stime64
  178. #define thrd_sleep __thrd_sleep_time64
  179. #define timegm __timegm_time64
  180. #define timerfd_gettime __timerfd_gettime64
  181. #define timerfd_settime __timerfd_settime64
  182. #define timer_gettime __timer_gettime64
  183. #define timer_settime __timer_settime64
  184. #define timespec_get __timespec_get_time64
  185. #define time __time64
  186. #define utimensat __utimensat_time64
  187. #define utimes __utimes_time64
  188. #define utime __utime64
  189. #define wait3 __wait3_time64
  190. #define wait4 __wait4_time64
  191. #endif
  192. // Platform-specific options.
  193. #if SANITIZER_APPLE
  194. #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE 0
  195. #elif SANITIZER_WINDOWS64
  196. #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE 0
  197. #else
  198. #define PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE 1
  199. #endif // SANITIZER_APPLE
  200. #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
  201. #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
  202. #endif
  203. #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
  204. #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
  205. #endif
  206. #ifndef COMMON_INTERCEPTOR_FD_ACCESS
  207. #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
  208. #endif
  209. #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
  210. #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
  211. #endif
  212. #ifndef COMMON_INTERCEPTOR_FILE_OPEN
  213. #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
  214. #endif
  215. #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
  216. #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
  217. #endif
  218. #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
  219. #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) {}
  220. #endif
  221. #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
  222. #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
  223. #endif
  224. #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
  225. #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
  226. COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
  227. #endif
  228. #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
  229. #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
  230. #endif
  231. #define COMMON_INTERCEPTOR_READ_STRING(ctx, s, n) \
  232. COMMON_INTERCEPTOR_READ_RANGE((ctx), (s), \
  233. common_flags()->strict_string_checks ? (internal_strlen(s)) + 1 : (n) )
  234. #ifndef COMMON_INTERCEPTOR_DLOPEN
  235. #define COMMON_INTERCEPTOR_DLOPEN(filename, flag) \
  236. ({ CheckNoDeepBind(filename, flag); REAL(dlopen)(filename, flag); })
  237. #endif
  238. #ifndef COMMON_INTERCEPTOR_GET_TLS_RANGE
  239. #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) *begin = *end = 0;
  240. #endif
  241. #ifndef COMMON_INTERCEPTOR_ACQUIRE
  242. #define COMMON_INTERCEPTOR_ACQUIRE(ctx, u) {}
  243. #endif
  244. #ifndef COMMON_INTERCEPTOR_RELEASE
  245. #define COMMON_INTERCEPTOR_RELEASE(ctx, u) {}
  246. #endif
  247. #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_START
  248. #define COMMON_INTERCEPTOR_USER_CALLBACK_START() {}
  249. #endif
  250. #ifndef COMMON_INTERCEPTOR_USER_CALLBACK_END
  251. #define COMMON_INTERCEPTOR_USER_CALLBACK_END() {}
  252. #endif
  253. #ifdef SANITIZER_NLDBL_VERSION
  254. #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
  255. COMMON_INTERCEPT_FUNCTION_VER(fn, SANITIZER_NLDBL_VERSION)
  256. #else
  257. #define COMMON_INTERCEPT_FUNCTION_LDBL(fn) \
  258. COMMON_INTERCEPT_FUNCTION(fn)
  259. #endif
  260. #if SANITIZER_GLIBC
  261. // If we could not find the versioned symbol, fall back to an unversioned
  262. // lookup. This is needed to work around a GLibc bug that causes dlsym
  263. // with RTLD_NEXT to return the oldest versioned symbol.
  264. // See https://sourceware.org/bugzilla/show_bug.cgi?id=14932.
  265. // For certain symbols (e.g. regexec) we have to perform a versioned lookup,
  266. // but that versioned symbol will only exist for architectures where the
  267. // oldest Glibc version pre-dates support for that architecture.
  268. // For example, regexec@GLIBC_2.3.4 exists on x86_64, but not RISC-V.
  269. // See also https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98920.
  270. #define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver) \
  271. COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(fn, ver)
  272. #else
  273. #define COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(fn, ver) \
  274. COMMON_INTERCEPT_FUNCTION(fn)
  275. #endif
  276. #ifndef COMMON_INTERCEPTOR_MEMSET_IMPL
  277. #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size) \
  278. { \
  279. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
  280. return internal_memset(dst, v, size); \
  281. COMMON_INTERCEPTOR_ENTER(ctx, memset, dst, v, size); \
  282. if (common_flags()->intercept_intrin) \
  283. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
  284. return REAL(memset)(dst, v, size); \
  285. }
  286. #endif
  287. #ifndef COMMON_INTERCEPTOR_MEMMOVE_IMPL
  288. #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size) \
  289. { \
  290. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) \
  291. return internal_memmove(dst, src, size); \
  292. COMMON_INTERCEPTOR_ENTER(ctx, memmove, dst, src, size); \
  293. if (common_flags()->intercept_intrin) { \
  294. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
  295. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
  296. } \
  297. return REAL(memmove)(dst, src, size); \
  298. }
  299. #endif
  300. #ifndef COMMON_INTERCEPTOR_MEMCPY_IMPL
  301. #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size) \
  302. { \
  303. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) { \
  304. return internal_memmove(dst, src, size); \
  305. } \
  306. COMMON_INTERCEPTOR_ENTER(ctx, memcpy, dst, src, size); \
  307. if (common_flags()->intercept_intrin) { \
  308. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, size); \
  309. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, size); \
  310. } \
  311. return REAL(memcpy)(dst, src, size); \
  312. }
  313. #endif
  314. #ifndef COMMON_INTERCEPTOR_MMAP_IMPL
  315. #define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, \
  316. off) \
  317. { return REAL(mmap)(addr, sz, prot, flags, fd, off); }
  318. #endif
  319. #ifndef COMMON_INTERCEPTOR_COPY_STRING
  320. #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {}
  321. #endif
  322. #ifndef COMMON_INTERCEPTOR_STRNDUP_IMPL
  323. #define COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size) \
  324. COMMON_INTERCEPTOR_ENTER(ctx, strndup, s, size); \
  325. uptr copy_length = internal_strnlen(s, size); \
  326. char *new_mem = (char *)WRAP(malloc)(copy_length + 1); \
  327. if (common_flags()->intercept_strndup) { \
  328. COMMON_INTERCEPTOR_READ_STRING(ctx, s, Min(size, copy_length + 1)); \
  329. } \
  330. if (new_mem) { \
  331. COMMON_INTERCEPTOR_COPY_STRING(ctx, new_mem, s, copy_length); \
  332. internal_memcpy(new_mem, s, copy_length); \
  333. new_mem[copy_length] = '\0'; \
  334. } \
  335. return new_mem;
  336. #endif
  337. #ifndef COMMON_INTERCEPTOR_STRERROR
  338. #define COMMON_INTERCEPTOR_STRERROR() {}
  339. #endif
  340. struct FileMetadata {
  341. // For open_memstream().
  342. char **addr;
  343. SIZE_T *size;
  344. };
  345. struct CommonInterceptorMetadata {
  346. enum {
  347. CIMT_INVALID = 0,
  348. CIMT_FILE
  349. } type;
  350. union {
  351. FileMetadata file;
  352. };
  353. };
  354. #if SI_POSIX
  355. typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
  356. static MetadataHashMap *interceptor_metadata_map;
  357. UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
  358. const FileMetadata &file) {
  359. MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
  360. CHECK(h.created());
  361. h->type = CommonInterceptorMetadata::CIMT_FILE;
  362. h->file = file;
  363. }
  364. UNUSED static const FileMetadata *GetInterceptorMetadata(
  365. __sanitizer_FILE *addr) {
  366. MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
  367. /* remove */ false,
  368. /* create */ false);
  369. if (addr && h.exists()) {
  370. CHECK(!h.created());
  371. CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
  372. return &h->file;
  373. } else {
  374. return 0;
  375. }
  376. }
  377. UNUSED static void DeleteInterceptorMetadata(void *addr) {
  378. MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
  379. CHECK(h.exists());
  380. }
  381. #endif // SI_POSIX
  382. #if SANITIZER_INTERCEPT_STRLEN
  383. INTERCEPTOR(SIZE_T, strlen, const char *s) {
  384. // Sometimes strlen is called prior to InitializeCommonInterceptors,
  385. // in which case the REAL(strlen) typically used in
  386. // COMMON_INTERCEPTOR_ENTER will fail. We use internal_strlen here
  387. // to handle that.
  388. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  389. return internal_strlen(s);
  390. void *ctx;
  391. COMMON_INTERCEPTOR_ENTER(ctx, strlen, s);
  392. SIZE_T result = REAL(strlen)(s);
  393. if (common_flags()->intercept_strlen)
  394. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, result + 1);
  395. return result;
  396. }
  397. #define INIT_STRLEN COMMON_INTERCEPT_FUNCTION(strlen)
  398. #else
  399. #define INIT_STRLEN
  400. #endif
  401. #if SANITIZER_INTERCEPT_STRNLEN
  402. INTERCEPTOR(SIZE_T, strnlen, const char *s, SIZE_T maxlen) {
  403. void *ctx;
  404. COMMON_INTERCEPTOR_ENTER(ctx, strnlen, s, maxlen);
  405. SIZE_T length = REAL(strnlen)(s, maxlen);
  406. if (common_flags()->intercept_strlen)
  407. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, Min(length + 1, maxlen));
  408. return length;
  409. }
  410. #define INIT_STRNLEN COMMON_INTERCEPT_FUNCTION(strnlen)
  411. #else
  412. #define INIT_STRNLEN
  413. #endif
  414. #if SANITIZER_INTERCEPT_STRNDUP
  415. INTERCEPTOR(char*, strndup, const char *s, uptr size) {
  416. void *ctx;
  417. COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
  418. }
  419. #define INIT_STRNDUP COMMON_INTERCEPT_FUNCTION(strndup)
  420. #else
  421. #define INIT_STRNDUP
  422. #endif // SANITIZER_INTERCEPT_STRNDUP
  423. #if SANITIZER_INTERCEPT___STRNDUP
  424. INTERCEPTOR(char*, __strndup, const char *s, uptr size) {
  425. void *ctx;
  426. COMMON_INTERCEPTOR_STRNDUP_IMPL(ctx, s, size);
  427. }
  428. #define INIT___STRNDUP COMMON_INTERCEPT_FUNCTION(__strndup)
  429. #else
  430. #define INIT___STRNDUP
  431. #endif // SANITIZER_INTERCEPT___STRNDUP
  432. #if SANITIZER_INTERCEPT_TEXTDOMAIN
  433. INTERCEPTOR(char*, textdomain, const char *domainname) {
  434. void *ctx;
  435. COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
  436. if (domainname) COMMON_INTERCEPTOR_READ_STRING(ctx, domainname, 0);
  437. char *domain = REAL(textdomain)(domainname);
  438. if (domain) {
  439. COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, internal_strlen(domain) + 1);
  440. }
  441. return domain;
  442. }
  443. #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
  444. #else
  445. #define INIT_TEXTDOMAIN
  446. #endif
  447. #if SANITIZER_INTERCEPT_STRCMP
  448. static inline int CharCmpX(unsigned char c1, unsigned char c2) {
  449. return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
  450. }
  451. DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, uptr called_pc,
  452. const char *s1, const char *s2, int result)
  453. INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
  454. void *ctx;
  455. COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
  456. unsigned char c1, c2;
  457. uptr i;
  458. for (i = 0;; i++) {
  459. c1 = (unsigned char)s1[i];
  460. c2 = (unsigned char)s2[i];
  461. if (c1 != c2 || c1 == '\0') break;
  462. }
  463. if (common_flags()->intercept_strcmp) {
  464. COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
  465. COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
  466. }
  467. int result = CharCmpX(c1, c2);
  468. CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcmp, GET_CALLER_PC(), s1,
  469. s2, result);
  470. return result;
  471. }
  472. DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, uptr called_pc,
  473. const char *s1, const char *s2, uptr n,
  474. int result)
  475. INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
  476. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  477. return internal_strncmp(s1, s2, size);
  478. void *ctx;
  479. COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
  480. unsigned char c1 = 0, c2 = 0;
  481. uptr i;
  482. for (i = 0; i < size; i++) {
  483. c1 = (unsigned char)s1[i];
  484. c2 = (unsigned char)s2[i];
  485. if (c1 != c2 || c1 == '\0') break;
  486. }
  487. uptr i1 = i;
  488. uptr i2 = i;
  489. if (common_flags()->strict_string_checks) {
  490. for (; i1 < size && s1[i1]; i1++) {}
  491. for (; i2 < size && s2[i2]; i2++) {}
  492. }
  493. COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
  494. COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
  495. int result = CharCmpX(c1, c2);
  496. CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncmp, GET_CALLER_PC(), s1,
  497. s2, size, result);
  498. return result;
  499. }
  500. #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
  501. #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
  502. #else
  503. #define INIT_STRCMP
  504. #define INIT_STRNCMP
  505. #endif
  506. #if SANITIZER_INTERCEPT_STRCASECMP
  507. static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
  508. int c1_low = ToLower(c1);
  509. int c2_low = ToLower(c2);
  510. return c1_low - c2_low;
  511. }
  512. DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, uptr called_pc,
  513. const char *s1, const char *s2, int result)
  514. INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
  515. void *ctx;
  516. COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
  517. unsigned char c1 = 0, c2 = 0;
  518. uptr i;
  519. for (i = 0;; i++) {
  520. c1 = (unsigned char)s1[i];
  521. c2 = (unsigned char)s2[i];
  522. if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
  523. }
  524. COMMON_INTERCEPTOR_READ_STRING(ctx, s1, i + 1);
  525. COMMON_INTERCEPTOR_READ_STRING(ctx, s2, i + 1);
  526. int result = CharCaseCmp(c1, c2);
  527. CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasecmp, GET_CALLER_PC(),
  528. s1, s2, result);
  529. return result;
  530. }
  531. DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, uptr called_pc,
  532. const char *s1, const char *s2, uptr size,
  533. int result)
  534. INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T size) {
  535. void *ctx;
  536. COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, size);
  537. unsigned char c1 = 0, c2 = 0;
  538. uptr i;
  539. for (i = 0; i < size; i++) {
  540. c1 = (unsigned char)s1[i];
  541. c2 = (unsigned char)s2[i];
  542. if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
  543. }
  544. uptr i1 = i;
  545. uptr i2 = i;
  546. if (common_flags()->strict_string_checks) {
  547. for (; i1 < size && s1[i1]; i1++) {}
  548. for (; i2 < size && s2[i2]; i2++) {}
  549. }
  550. COMMON_INTERCEPTOR_READ_RANGE((ctx), (s1), Min(i1 + 1, size));
  551. COMMON_INTERCEPTOR_READ_RANGE((ctx), (s2), Min(i2 + 1, size));
  552. int result = CharCaseCmp(c1, c2);
  553. CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strncasecmp, GET_CALLER_PC(),
  554. s1, s2, size, result);
  555. return result;
  556. }
  557. #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
  558. #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
  559. #else
  560. #define INIT_STRCASECMP
  561. #define INIT_STRNCASECMP
  562. #endif
  563. #if SANITIZER_INTERCEPT_STRSTR || SANITIZER_INTERCEPT_STRCASESTR
  564. static inline void StrstrCheck(void *ctx, char *r, const char *s1,
  565. const char *s2) {
  566. uptr len1 = internal_strlen(s1);
  567. uptr len2 = internal_strlen(s2);
  568. COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r ? r - s1 + len2 : len1 + 1);
  569. COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2 + 1);
  570. }
  571. #endif
  572. #if SANITIZER_INTERCEPT_STRSTR
  573. DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, uptr called_pc,
  574. const char *s1, const char *s2, char *result)
  575. INTERCEPTOR(char*, strstr, const char *s1, const char *s2) {
  576. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  577. return internal_strstr(s1, s2);
  578. void *ctx;
  579. COMMON_INTERCEPTOR_ENTER(ctx, strstr, s1, s2);
  580. char *r = REAL(strstr)(s1, s2);
  581. if (common_flags()->intercept_strstr)
  582. StrstrCheck(ctx, r, s1, s2);
  583. CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strstr, GET_CALLER_PC(), s1,
  584. s2, r);
  585. return r;
  586. }
  587. #define INIT_STRSTR COMMON_INTERCEPT_FUNCTION(strstr);
  588. #else
  589. #define INIT_STRSTR
  590. #endif
  591. #if SANITIZER_INTERCEPT_STRCASESTR
  592. DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, uptr called_pc,
  593. const char *s1, const char *s2, char *result)
  594. INTERCEPTOR(char*, strcasestr, const char *s1, const char *s2) {
  595. void *ctx;
  596. COMMON_INTERCEPTOR_ENTER(ctx, strcasestr, s1, s2);
  597. char *r = REAL(strcasestr)(s1, s2);
  598. if (common_flags()->intercept_strstr)
  599. StrstrCheck(ctx, r, s1, s2);
  600. CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_strcasestr, GET_CALLER_PC(),
  601. s1, s2, r);
  602. return r;
  603. }
  604. #define INIT_STRCASESTR COMMON_INTERCEPT_FUNCTION(strcasestr);
  605. #else
  606. #define INIT_STRCASESTR
  607. #endif
  608. #if SANITIZER_INTERCEPT_STRTOK
  609. INTERCEPTOR(char*, strtok, char *str, const char *delimiters) {
  610. void *ctx;
  611. COMMON_INTERCEPTOR_ENTER(ctx, strtok, str, delimiters);
  612. if (!common_flags()->intercept_strtok) {
  613. return REAL(strtok)(str, delimiters);
  614. }
  615. if (common_flags()->strict_string_checks) {
  616. // If strict_string_checks is enabled, we check the whole first argument
  617. // string on the first call (strtok saves this string in a static buffer
  618. // for subsequent calls). We do not need to check strtok's result.
  619. // As the delimiters can change, we check them every call.
  620. if (str != nullptr) {
  621. COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
  622. }
  623. COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters,
  624. internal_strlen(delimiters) + 1);
  625. return REAL(strtok)(str, delimiters);
  626. } else {
  627. // However, when strict_string_checks is disabled we cannot check the
  628. // whole string on the first call. Instead, we check the result string
  629. // which is guaranteed to be a NULL-terminated substring of the first
  630. // argument. We also conservatively check one character of str and the
  631. // delimiters.
  632. if (str != nullptr) {
  633. COMMON_INTERCEPTOR_READ_STRING(ctx, str, 1);
  634. }
  635. COMMON_INTERCEPTOR_READ_RANGE(ctx, delimiters, 1);
  636. char *result = REAL(strtok)(str, delimiters);
  637. if (result != nullptr) {
  638. COMMON_INTERCEPTOR_READ_RANGE(ctx, result, internal_strlen(result) + 1);
  639. } else if (str != nullptr) {
  640. // No delimiter were found, it's safe to assume that the entire str was
  641. // scanned.
  642. COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
  643. }
  644. return result;
  645. }
  646. }
  647. #define INIT_STRTOK COMMON_INTERCEPT_FUNCTION(strtok)
  648. #else
  649. #define INIT_STRTOK
  650. #endif
  651. #if SANITIZER_INTERCEPT_MEMMEM
  652. DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, uptr called_pc,
  653. const void *s1, SIZE_T len1, const void *s2,
  654. SIZE_T len2, void *result)
  655. INTERCEPTOR(void*, memmem, const void *s1, SIZE_T len1, const void *s2,
  656. SIZE_T len2) {
  657. void *ctx;
  658. COMMON_INTERCEPTOR_ENTER(ctx, memmem, s1, len1, s2, len2);
  659. void *r = REAL(memmem)(s1, len1, s2, len2);
  660. if (common_flags()->intercept_memmem) {
  661. COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, len1);
  662. COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, len2);
  663. }
  664. CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memmem, GET_CALLER_PC(),
  665. s1, len1, s2, len2, r);
  666. return r;
  667. }
  668. #define INIT_MEMMEM COMMON_INTERCEPT_FUNCTION(memmem);
  669. #else
  670. #define INIT_MEMMEM
  671. #endif // SANITIZER_INTERCEPT_MEMMEM
  672. #if SANITIZER_INTERCEPT_STRCHR
  673. INTERCEPTOR(char*, strchr, const char *s, int c) {
  674. void *ctx;
  675. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  676. return internal_strchr(s, c);
  677. COMMON_INTERCEPTOR_ENTER(ctx, strchr, s, c);
  678. char *result = REAL(strchr)(s, c);
  679. if (common_flags()->intercept_strchr) {
  680. // Keep strlen as macro argument, as macro may ignore it.
  681. COMMON_INTERCEPTOR_READ_STRING(ctx, s,
  682. (result ? result - s : internal_strlen(s)) + 1);
  683. }
  684. return result;
  685. }
  686. #define INIT_STRCHR COMMON_INTERCEPT_FUNCTION(strchr)
  687. #else
  688. #define INIT_STRCHR
  689. #endif
  690. #if SANITIZER_INTERCEPT_STRCHRNUL
  691. INTERCEPTOR(char*, strchrnul, const char *s, int c) {
  692. void *ctx;
  693. COMMON_INTERCEPTOR_ENTER(ctx, strchrnul, s, c);
  694. char *result = REAL(strchrnul)(s, c);
  695. uptr len = result - s + 1;
  696. if (common_flags()->intercept_strchr)
  697. COMMON_INTERCEPTOR_READ_STRING(ctx, s, len);
  698. return result;
  699. }
  700. #define INIT_STRCHRNUL COMMON_INTERCEPT_FUNCTION(strchrnul)
  701. #else
  702. #define INIT_STRCHRNUL
  703. #endif
  704. #if SANITIZER_INTERCEPT_STRRCHR
  705. INTERCEPTOR(char*, strrchr, const char *s, int c) {
  706. void *ctx;
  707. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  708. return internal_strrchr(s, c);
  709. COMMON_INTERCEPTOR_ENTER(ctx, strrchr, s, c);
  710. if (common_flags()->intercept_strchr)
  711. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
  712. return REAL(strrchr)(s, c);
  713. }
  714. #define INIT_STRRCHR COMMON_INTERCEPT_FUNCTION(strrchr)
  715. #else
  716. #define INIT_STRRCHR
  717. #endif
  718. #if SANITIZER_INTERCEPT_STRSPN
  719. INTERCEPTOR(SIZE_T, strspn, const char *s1, const char *s2) {
  720. void *ctx;
  721. COMMON_INTERCEPTOR_ENTER(ctx, strspn, s1, s2);
  722. SIZE_T r = REAL(strspn)(s1, s2);
  723. if (common_flags()->intercept_strspn) {
  724. COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
  725. COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
  726. }
  727. return r;
  728. }
  729. INTERCEPTOR(SIZE_T, strcspn, const char *s1, const char *s2) {
  730. void *ctx;
  731. COMMON_INTERCEPTOR_ENTER(ctx, strcspn, s1, s2);
  732. SIZE_T r = REAL(strcspn)(s1, s2);
  733. if (common_flags()->intercept_strspn) {
  734. COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
  735. COMMON_INTERCEPTOR_READ_STRING(ctx, s1, r + 1);
  736. }
  737. return r;
  738. }
  739. #define INIT_STRSPN \
  740. COMMON_INTERCEPT_FUNCTION(strspn); \
  741. COMMON_INTERCEPT_FUNCTION(strcspn);
  742. #else
  743. #define INIT_STRSPN
  744. #endif
  745. #if SANITIZER_INTERCEPT_STRPBRK
  746. INTERCEPTOR(char *, strpbrk, const char *s1, const char *s2) {
  747. void *ctx;
  748. COMMON_INTERCEPTOR_ENTER(ctx, strpbrk, s1, s2);
  749. char *r = REAL(strpbrk)(s1, s2);
  750. if (common_flags()->intercept_strpbrk) {
  751. COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, internal_strlen(s2) + 1);
  752. COMMON_INTERCEPTOR_READ_STRING(ctx, s1,
  753. r ? r - s1 + 1 : internal_strlen(s1) + 1);
  754. }
  755. return r;
  756. }
  757. #define INIT_STRPBRK COMMON_INTERCEPT_FUNCTION(strpbrk);
  758. #else
  759. #define INIT_STRPBRK
  760. #endif
  761. #if SANITIZER_INTERCEPT_MEMSET
  762. INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
  763. void *ctx;
  764. COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, dst, v, size);
  765. }
  766. #define INIT_MEMSET COMMON_INTERCEPT_FUNCTION(memset)
  767. #else
  768. #define INIT_MEMSET
  769. #endif
  770. #if SANITIZER_INTERCEPT_MEMMOVE
  771. INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
  772. void *ctx;
  773. COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
  774. }
  775. #define INIT_MEMMOVE COMMON_INTERCEPT_FUNCTION(memmove)
  776. #else
  777. #define INIT_MEMMOVE
  778. #endif
  779. #if SANITIZER_INTERCEPT_MEMCPY
  780. INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
  781. // On OS X, calling internal_memcpy here will cause memory corruptions,
  782. // because memcpy and memmove are actually aliases of the same
  783. // implementation. We need to use internal_memmove here.
  784. // N.B.: If we switch this to internal_ we'll have to use internal_memmove
  785. // due to memcpy being an alias of memmove on OS X.
  786. void *ctx;
  787. #if PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE
  788. COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, dst, src, size);
  789. #else
  790. COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, dst, src, size);
  791. #endif
  792. }
  793. #define INIT_MEMCPY \
  794. do { \
  795. if (PLATFORM_HAS_DIFFERENT_MEMCPY_AND_MEMMOVE) { \
  796. COMMON_INTERCEPT_FUNCTION(memcpy); \
  797. } else { \
  798. ASSIGN_REAL(memcpy, memmove); \
  799. } \
  800. CHECK(REAL(memcpy)); \
  801. } while (false)
  802. #else
  803. #define INIT_MEMCPY
  804. #endif
  805. #if SANITIZER_INTERCEPT_MEMCMP
  806. DECLARE_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, uptr called_pc,
  807. const void *s1, const void *s2, uptr n,
  808. int result)
  809. // Common code for `memcmp` and `bcmp`.
  810. int MemcmpInterceptorCommon(void *ctx,
  811. int (*real_fn)(const void *, const void *, uptr),
  812. const void *a1, const void *a2, uptr size) {
  813. if (common_flags()->intercept_memcmp) {
  814. if (common_flags()->strict_memcmp) {
  815. // Check the entire regions even if the first bytes of the buffers are
  816. // different.
  817. COMMON_INTERCEPTOR_READ_RANGE(ctx, a1, size);
  818. COMMON_INTERCEPTOR_READ_RANGE(ctx, a2, size);
  819. // Fallthrough to REAL(memcmp) below.
  820. } else {
  821. unsigned char c1 = 0, c2 = 0;
  822. const unsigned char *s1 = (const unsigned char*)a1;
  823. const unsigned char *s2 = (const unsigned char*)a2;
  824. uptr i;
  825. for (i = 0; i < size; i++) {
  826. c1 = s1[i];
  827. c2 = s2[i];
  828. if (c1 != c2) break;
  829. }
  830. COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
  831. COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
  832. int r = CharCmpX(c1, c2);
  833. CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(),
  834. a1, a2, size, r);
  835. return r;
  836. }
  837. }
  838. int result = real_fn(a1, a2, size);
  839. CALL_WEAK_INTERCEPTOR_HOOK(__sanitizer_weak_hook_memcmp, GET_CALLER_PC(), a1,
  840. a2, size, result);
  841. return result;
  842. }
  843. INTERCEPTOR(int, memcmp, const void *a1, const void *a2, uptr size) {
  844. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  845. return internal_memcmp(a1, a2, size);
  846. void *ctx;
  847. COMMON_INTERCEPTOR_ENTER(ctx, memcmp, a1, a2, size);
  848. return MemcmpInterceptorCommon(ctx, REAL(memcmp), a1, a2, size);
  849. }
  850. #define INIT_MEMCMP COMMON_INTERCEPT_FUNCTION(memcmp)
  851. #else
  852. #define INIT_MEMCMP
  853. #endif
  854. #if SANITIZER_INTERCEPT_BCMP
  855. INTERCEPTOR(int, bcmp, const void *a1, const void *a2, uptr size) {
  856. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  857. return internal_memcmp(a1, a2, size);
  858. void *ctx;
  859. COMMON_INTERCEPTOR_ENTER(ctx, bcmp, a1, a2, size);
  860. return MemcmpInterceptorCommon(ctx, REAL(bcmp), a1, a2, size);
  861. }
  862. #define INIT_BCMP COMMON_INTERCEPT_FUNCTION(bcmp)
  863. #else
  864. #define INIT_BCMP
  865. #endif
  866. #if SANITIZER_INTERCEPT_MEMCHR
  867. INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
  868. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  869. return internal_memchr(s, c, n);
  870. void *ctx;
  871. COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
  872. #if SANITIZER_WINDOWS
  873. void *res;
  874. if (REAL(memchr)) {
  875. res = REAL(memchr)(s, c, n);
  876. } else {
  877. res = internal_memchr(s, c, n);
  878. }
  879. #else
  880. void *res = REAL(memchr)(s, c, n);
  881. #endif
  882. uptr len = res ? (char *)res - (const char *)s + 1 : n;
  883. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
  884. return res;
  885. }
  886. #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
  887. #else
  888. #define INIT_MEMCHR
  889. #endif
  890. #if SANITIZER_INTERCEPT_MEMRCHR
  891. INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
  892. void *ctx;
  893. COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
  894. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
  895. return REAL(memrchr)(s, c, n);
  896. }
  897. #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
  898. #else
  899. #define INIT_MEMRCHR
  900. #endif
  901. #if SANITIZER_INTERCEPT_FREXP
  902. INTERCEPTOR(double, frexp, double x, int *exp) {
  903. void *ctx;
  904. COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
  905. // Assuming frexp() always writes to |exp|.
  906. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
  907. double res = REAL(frexp)(x, exp);
  908. COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
  909. return res;
  910. }
  911. #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
  912. #else
  913. #define INIT_FREXP
  914. #endif // SANITIZER_INTERCEPT_FREXP
  915. #if SANITIZER_INTERCEPT_FREXPF_FREXPL
  916. INTERCEPTOR(float, frexpf, float x, int *exp) {
  917. void *ctx;
  918. COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
  919. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
  920. float res = REAL(frexpf)(x, exp);
  921. COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
  922. return res;
  923. }
  924. INTERCEPTOR(long double, frexpl, long double x, int *exp) {
  925. void *ctx;
  926. COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
  927. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
  928. long double res = REAL(frexpl)(x, exp);
  929. COMMON_INTERCEPTOR_INITIALIZE_RANGE(exp, sizeof(*exp));
  930. return res;
  931. }
  932. #define INIT_FREXPF_FREXPL \
  933. COMMON_INTERCEPT_FUNCTION(frexpf); \
  934. COMMON_INTERCEPT_FUNCTION_LDBL(frexpl)
  935. #else
  936. #define INIT_FREXPF_FREXPL
  937. #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
  938. #if SI_POSIX
  939. static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
  940. SIZE_T iovlen, SIZE_T maxlen) {
  941. for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
  942. SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
  943. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
  944. maxlen -= sz;
  945. }
  946. }
  947. static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
  948. SIZE_T iovlen, SIZE_T maxlen) {
  949. COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
  950. for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
  951. SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
  952. COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
  953. maxlen -= sz;
  954. }
  955. }
  956. #endif
  957. #if SANITIZER_INTERCEPT_READ
  958. INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
  959. void *ctx;
  960. COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
  961. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  962. // FIXME: under ASan the call below may write to freed memory and corrupt
  963. // its metadata. See
  964. // https://github.com/google/sanitizers/issues/321.
  965. SSIZE_T res = REAL(read)(fd, ptr, count);
  966. if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
  967. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  968. return res;
  969. }
  970. #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
  971. #else
  972. #define INIT_READ
  973. #endif
  974. #if SANITIZER_INTERCEPT_FREAD
  975. INTERCEPTOR(SIZE_T, fread, void *ptr, SIZE_T size, SIZE_T nmemb, void *file) {
  976. // libc file streams can call user-supplied functions, see fopencookie.
  977. void *ctx;
  978. COMMON_INTERCEPTOR_ENTER(ctx, fread, ptr, size, nmemb, file);
  979. // FIXME: under ASan the call below may write to freed memory and corrupt
  980. // its metadata. See
  981. // https://github.com/google/sanitizers/issues/321.
  982. SIZE_T res = REAL(fread)(ptr, size, nmemb, file);
  983. if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res * size);
  984. return res;
  985. }
  986. #define INIT_FREAD COMMON_INTERCEPT_FUNCTION(fread)
  987. #else
  988. #define INIT_FREAD
  989. #endif
  990. #if SANITIZER_INTERCEPT_PREAD
  991. INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
  992. void *ctx;
  993. COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
  994. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  995. // FIXME: under ASan the call below may write to freed memory and corrupt
  996. // its metadata. See
  997. // https://github.com/google/sanitizers/issues/321.
  998. SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
  999. if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
  1000. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  1001. return res;
  1002. }
  1003. #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
  1004. #else
  1005. #define INIT_PREAD
  1006. #endif
  1007. #if SANITIZER_INTERCEPT_PREAD64
  1008. INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
  1009. void *ctx;
  1010. COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
  1011. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  1012. // FIXME: under ASan the call below may write to freed memory and corrupt
  1013. // its metadata. See
  1014. // https://github.com/google/sanitizers/issues/321.
  1015. SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
  1016. if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
  1017. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  1018. return res;
  1019. }
  1020. #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
  1021. #else
  1022. #define INIT_PREAD64
  1023. #endif
  1024. #if SANITIZER_INTERCEPT_READV
  1025. INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
  1026. int iovcnt) {
  1027. void *ctx;
  1028. COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
  1029. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  1030. SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
  1031. if (res > 0) write_iovec(ctx, iov, iovcnt, res);
  1032. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  1033. return res;
  1034. }
  1035. #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
  1036. #else
  1037. #define INIT_READV
  1038. #endif
  1039. #if SANITIZER_INTERCEPT_PREADV
  1040. INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
  1041. OFF_T offset) {
  1042. void *ctx;
  1043. COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
  1044. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  1045. SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
  1046. if (res > 0) write_iovec(ctx, iov, iovcnt, res);
  1047. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  1048. return res;
  1049. }
  1050. #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
  1051. #else
  1052. #define INIT_PREADV
  1053. #endif
  1054. #if SANITIZER_INTERCEPT_PREADV64
  1055. INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
  1056. OFF64_T offset) {
  1057. void *ctx;
  1058. COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
  1059. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  1060. SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
  1061. if (res > 0) write_iovec(ctx, iov, iovcnt, res);
  1062. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  1063. return res;
  1064. }
  1065. #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
  1066. #else
  1067. #define INIT_PREADV64
  1068. #endif
  1069. #if SANITIZER_INTERCEPT_WRITE
  1070. INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
  1071. void *ctx;
  1072. COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
  1073. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  1074. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  1075. SSIZE_T res = REAL(write)(fd, ptr, count);
  1076. // FIXME: this check should be _before_ the call to REAL(write), not after
  1077. if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
  1078. return res;
  1079. }
  1080. #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
  1081. #else
  1082. #define INIT_WRITE
  1083. #endif
  1084. #if SANITIZER_INTERCEPT_FWRITE
  1085. INTERCEPTOR(SIZE_T, fwrite, const void *p, uptr size, uptr nmemb, void *file) {
  1086. // libc file streams can call user-supplied functions, see fopencookie.
  1087. void *ctx;
  1088. COMMON_INTERCEPTOR_ENTER(ctx, fwrite, p, size, nmemb, file);
  1089. SIZE_T res = REAL(fwrite)(p, size, nmemb, file);
  1090. if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, p, res * size);
  1091. return res;
  1092. }
  1093. #define INIT_FWRITE COMMON_INTERCEPT_FUNCTION(fwrite)
  1094. #else
  1095. #define INIT_FWRITE
  1096. #endif
  1097. #if SANITIZER_INTERCEPT_PWRITE
  1098. INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
  1099. void *ctx;
  1100. COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
  1101. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  1102. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  1103. SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
  1104. if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
  1105. return res;
  1106. }
  1107. #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
  1108. #else
  1109. #define INIT_PWRITE
  1110. #endif
  1111. #if SANITIZER_INTERCEPT_PWRITE64
  1112. INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
  1113. OFF64_T offset) {
  1114. void *ctx;
  1115. COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
  1116. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  1117. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  1118. SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
  1119. if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
  1120. return res;
  1121. }
  1122. #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
  1123. #else
  1124. #define INIT_PWRITE64
  1125. #endif
  1126. #if SANITIZER_INTERCEPT_WRITEV
  1127. INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
  1128. int iovcnt) {
  1129. void *ctx;
  1130. COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
  1131. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  1132. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  1133. SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
  1134. if (res > 0) read_iovec(ctx, iov, iovcnt, res);
  1135. return res;
  1136. }
  1137. #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
  1138. #else
  1139. #define INIT_WRITEV
  1140. #endif
  1141. #if SANITIZER_INTERCEPT_PWRITEV
  1142. INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
  1143. OFF_T offset) {
  1144. void *ctx;
  1145. COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
  1146. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  1147. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  1148. SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
  1149. if (res > 0) read_iovec(ctx, iov, iovcnt, res);
  1150. return res;
  1151. }
  1152. #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
  1153. #else
  1154. #define INIT_PWRITEV
  1155. #endif
  1156. #if SANITIZER_INTERCEPT_PWRITEV64
  1157. INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
  1158. OFF64_T offset) {
  1159. void *ctx;
  1160. COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
  1161. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  1162. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  1163. SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
  1164. if (res > 0) read_iovec(ctx, iov, iovcnt, res);
  1165. return res;
  1166. }
  1167. #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
  1168. #else
  1169. #define INIT_PWRITEV64
  1170. #endif
  1171. #if SANITIZER_INTERCEPT_FGETS
  1172. INTERCEPTOR(char *, fgets, char *s, SIZE_T size, void *file) {
  1173. // libc file streams can call user-supplied functions, see fopencookie.
  1174. void *ctx;
  1175. COMMON_INTERCEPTOR_ENTER(ctx, fgets, s, size, file);
  1176. // FIXME: under ASan the call below may write to freed memory and corrupt
  1177. // its metadata. See
  1178. // https://github.com/google/sanitizers/issues/321.
  1179. char *res = REAL(fgets)(s, size, file);
  1180. if (res)
  1181. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
  1182. return res;
  1183. }
  1184. #define INIT_FGETS COMMON_INTERCEPT_FUNCTION(fgets)
  1185. #else
  1186. #define INIT_FGETS
  1187. #endif
  1188. #if SANITIZER_INTERCEPT_FPUTS
  1189. INTERCEPTOR_WITH_SUFFIX(int, fputs, char *s, void *file) {
  1190. // libc file streams can call user-supplied functions, see fopencookie.
  1191. void *ctx;
  1192. COMMON_INTERCEPTOR_ENTER(ctx, fputs, s, file);
  1193. if (!SANITIZER_APPLE || s) { // `fputs(NULL, file)` is supported on Darwin.
  1194. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
  1195. }
  1196. return REAL(fputs)(s, file);
  1197. }
  1198. #define INIT_FPUTS COMMON_INTERCEPT_FUNCTION(fputs)
  1199. #else
  1200. #define INIT_FPUTS
  1201. #endif
  1202. #if SANITIZER_INTERCEPT_PUTS
  1203. INTERCEPTOR(int, puts, char *s) {
  1204. // libc file streams can call user-supplied functions, see fopencookie.
  1205. void *ctx;
  1206. COMMON_INTERCEPTOR_ENTER(ctx, puts, s);
  1207. if (!SANITIZER_APPLE || s) { // `puts(NULL)` is supported on Darwin.
  1208. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
  1209. }
  1210. return REAL(puts)(s);
  1211. }
  1212. #define INIT_PUTS COMMON_INTERCEPT_FUNCTION(puts)
  1213. #else
  1214. #define INIT_PUTS
  1215. #endif
  1216. #if SANITIZER_INTERCEPT_PRCTL
  1217. INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
  1218. unsigned long arg4, unsigned long arg5) {
  1219. void *ctx;
  1220. COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
  1221. static const int PR_SET_NAME = 15;
  1222. static const int PR_SET_VMA = 0x53564d41;
  1223. static const int PR_SCHED_CORE = 62;
  1224. static const int PR_SCHED_CORE_GET = 0;
  1225. if (option == PR_SET_VMA && arg2 == 0UL) {
  1226. char *name = (char *)arg5;
  1227. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  1228. }
  1229. int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
  1230. if (option == PR_SET_NAME) {
  1231. char buff[16];
  1232. internal_strncpy(buff, (char *)arg2, 15);
  1233. buff[15] = 0;
  1234. COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
  1235. } else if (res != -1 && option == PR_SCHED_CORE && arg2 == PR_SCHED_CORE_GET) {
  1236. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64*)(arg5), sizeof(u64));
  1237. }
  1238. return res;
  1239. }
  1240. #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
  1241. #else
  1242. #define INIT_PRCTL
  1243. #endif // SANITIZER_INTERCEPT_PRCTL
  1244. #if SANITIZER_INTERCEPT_TIME
  1245. INTERCEPTOR(unsigned long, time, unsigned long *t) {
  1246. void *ctx;
  1247. COMMON_INTERCEPTOR_ENTER(ctx, time, t);
  1248. unsigned long local_t;
  1249. unsigned long res = REAL(time)(&local_t);
  1250. if (t && res != (unsigned long)-1) {
  1251. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
  1252. *t = local_t;
  1253. }
  1254. return res;
  1255. }
  1256. #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
  1257. #else
  1258. #define INIT_TIME
  1259. #endif // SANITIZER_INTERCEPT_TIME
  1260. #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
  1261. static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
  1262. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
  1263. #if !SANITIZER_SOLARIS
  1264. if (tm->tm_zone) {
  1265. // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
  1266. // can point to shared memory and tsan would report a data race.
  1267. COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
  1268. internal_strlen(tm->tm_zone) + 1);
  1269. }
  1270. #endif
  1271. }
  1272. INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
  1273. void *ctx;
  1274. COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
  1275. __sanitizer_tm *res = REAL(localtime)(timep);
  1276. if (res) {
  1277. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  1278. unpoison_tm(ctx, res);
  1279. }
  1280. return res;
  1281. }
  1282. INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
  1283. void *ctx;
  1284. COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
  1285. __sanitizer_tm *res = REAL(localtime_r)(timep, result);
  1286. if (res) {
  1287. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  1288. unpoison_tm(ctx, res);
  1289. }
  1290. return res;
  1291. }
  1292. INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
  1293. void *ctx;
  1294. COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
  1295. __sanitizer_tm *res = REAL(gmtime)(timep);
  1296. if (res) {
  1297. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  1298. unpoison_tm(ctx, res);
  1299. }
  1300. return res;
  1301. }
  1302. INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
  1303. void *ctx;
  1304. COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
  1305. __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
  1306. if (res) {
  1307. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  1308. unpoison_tm(ctx, res);
  1309. }
  1310. return res;
  1311. }
  1312. INTERCEPTOR(char *, ctime, unsigned long *timep) {
  1313. void *ctx;
  1314. COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
  1315. // FIXME: under ASan the call below may write to freed memory and corrupt
  1316. // its metadata. See
  1317. // https://github.com/google/sanitizers/issues/321.
  1318. char *res = REAL(ctime)(timep);
  1319. if (res) {
  1320. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  1321. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  1322. }
  1323. return res;
  1324. }
  1325. INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
  1326. void *ctx;
  1327. COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
  1328. // FIXME: under ASan the call below may write to freed memory and corrupt
  1329. // its metadata. See
  1330. // https://github.com/google/sanitizers/issues/321.
  1331. char *res = REAL(ctime_r)(timep, result);
  1332. if (res) {
  1333. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  1334. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  1335. }
  1336. return res;
  1337. }
  1338. INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
  1339. void *ctx;
  1340. COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
  1341. // FIXME: under ASan the call below may write to freed memory and corrupt
  1342. // its metadata. See
  1343. // https://github.com/google/sanitizers/issues/321.
  1344. char *res = REAL(asctime)(tm);
  1345. if (res) {
  1346. COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
  1347. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  1348. }
  1349. return res;
  1350. }
  1351. INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
  1352. void *ctx;
  1353. COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
  1354. // FIXME: under ASan the call below may write to freed memory and corrupt
  1355. // its metadata. See
  1356. // https://github.com/google/sanitizers/issues/321.
  1357. char *res = REAL(asctime_r)(tm, result);
  1358. if (res) {
  1359. COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
  1360. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  1361. }
  1362. return res;
  1363. }
  1364. INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
  1365. void *ctx;
  1366. COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
  1367. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
  1368. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
  1369. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
  1370. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
  1371. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
  1372. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
  1373. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
  1374. long res = REAL(mktime)(tm);
  1375. if (res != -1) unpoison_tm(ctx, tm);
  1376. return res;
  1377. }
  1378. #define INIT_LOCALTIME_AND_FRIENDS \
  1379. COMMON_INTERCEPT_FUNCTION(localtime); \
  1380. COMMON_INTERCEPT_FUNCTION(localtime_r); \
  1381. COMMON_INTERCEPT_FUNCTION(gmtime); \
  1382. COMMON_INTERCEPT_FUNCTION(gmtime_r); \
  1383. COMMON_INTERCEPT_FUNCTION(ctime); \
  1384. COMMON_INTERCEPT_FUNCTION(ctime_r); \
  1385. COMMON_INTERCEPT_FUNCTION(asctime); \
  1386. COMMON_INTERCEPT_FUNCTION(asctime_r); \
  1387. COMMON_INTERCEPT_FUNCTION(mktime);
  1388. #else
  1389. #define INIT_LOCALTIME_AND_FRIENDS
  1390. #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
  1391. #if SANITIZER_INTERCEPT_STRPTIME
  1392. INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
  1393. void *ctx;
  1394. COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
  1395. if (format)
  1396. COMMON_INTERCEPTOR_READ_RANGE(ctx, format, internal_strlen(format) + 1);
  1397. // FIXME: under ASan the call below may write to freed memory and corrupt
  1398. // its metadata. See
  1399. // https://github.com/google/sanitizers/issues/321.
  1400. char *res = REAL(strptime)(s, format, tm);
  1401. COMMON_INTERCEPTOR_READ_STRING(ctx, s, res ? res - s : 0);
  1402. if (res && tm) {
  1403. // Do not call unpoison_tm here, because strptime does not, in fact,
  1404. // initialize the entire struct tm. For example, tm_zone pointer is left
  1405. // uninitialized.
  1406. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
  1407. }
  1408. return res;
  1409. }
  1410. #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
  1411. #else
  1412. #define INIT_STRPTIME
  1413. #endif
  1414. #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
  1415. #include "sanitizer_common_interceptors_format.inc"
  1416. #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
  1417. { \
  1418. void *ctx; \
  1419. va_list ap; \
  1420. va_start(ap, format); \
  1421. COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
  1422. int res = WRAP(vname)(__VA_ARGS__, ap); \
  1423. va_end(ap); \
  1424. return res; \
  1425. }
  1426. #endif
  1427. #if SANITIZER_INTERCEPT_SCANF
  1428. #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
  1429. { \
  1430. void *ctx; \
  1431. COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
  1432. va_list aq; \
  1433. va_copy(aq, ap); \
  1434. int res = REAL(vname)(__VA_ARGS__); \
  1435. if (res > 0) \
  1436. scanf_common(ctx, res, allowGnuMalloc, format, aq); \
  1437. va_end(aq); \
  1438. return res; \
  1439. }
  1440. INTERCEPTOR(int, vscanf, const char *format, va_list ap)
  1441. VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
  1442. INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
  1443. VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
  1444. INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
  1445. VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
  1446. #if SANITIZER_INTERCEPT_ISOC99_SCANF
  1447. INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
  1448. VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
  1449. INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
  1450. va_list ap)
  1451. VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
  1452. INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
  1453. VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
  1454. #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
  1455. INTERCEPTOR(int, scanf, const char *format, ...)
  1456. FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
  1457. INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
  1458. FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
  1459. INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
  1460. FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
  1461. #if SANITIZER_INTERCEPT_ISOC99_SCANF
  1462. INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
  1463. FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
  1464. INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
  1465. FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
  1466. INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
  1467. FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
  1468. #endif
  1469. #endif
  1470. #if SANITIZER_INTERCEPT_SCANF
  1471. #define INIT_SCANF \
  1472. COMMON_INTERCEPT_FUNCTION_LDBL(scanf); \
  1473. COMMON_INTERCEPT_FUNCTION_LDBL(sscanf); \
  1474. COMMON_INTERCEPT_FUNCTION_LDBL(fscanf); \
  1475. COMMON_INTERCEPT_FUNCTION_LDBL(vscanf); \
  1476. COMMON_INTERCEPT_FUNCTION_LDBL(vsscanf); \
  1477. COMMON_INTERCEPT_FUNCTION_LDBL(vfscanf);
  1478. #else
  1479. #define INIT_SCANF
  1480. #endif
  1481. #if SANITIZER_INTERCEPT_ISOC99_SCANF
  1482. #define INIT_ISOC99_SCANF \
  1483. COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
  1484. COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
  1485. COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
  1486. COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
  1487. COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
  1488. COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
  1489. #else
  1490. #define INIT_ISOC99_SCANF
  1491. #endif
  1492. #if SANITIZER_INTERCEPT_PRINTF
  1493. #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
  1494. void *ctx; \
  1495. COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
  1496. va_list aq; \
  1497. va_copy(aq, ap);
  1498. #define VPRINTF_INTERCEPTOR_RETURN() \
  1499. va_end(aq);
  1500. #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
  1501. { \
  1502. VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
  1503. if (common_flags()->check_printf) \
  1504. printf_common(ctx, format, aq); \
  1505. int res = REAL(vname)(__VA_ARGS__); \
  1506. VPRINTF_INTERCEPTOR_RETURN(); \
  1507. return res; \
  1508. }
  1509. // FIXME: under ASan the REAL() call below may write to freed memory and
  1510. // corrupt its metadata. See
  1511. // https://github.com/google/sanitizers/issues/321.
  1512. #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
  1513. { \
  1514. VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
  1515. if (common_flags()->check_printf) { \
  1516. printf_common(ctx, format, aq); \
  1517. } \
  1518. int res = REAL(vname)(str, __VA_ARGS__); \
  1519. if (res >= 0) { \
  1520. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
  1521. } \
  1522. VPRINTF_INTERCEPTOR_RETURN(); \
  1523. return res; \
  1524. }
  1525. // FIXME: under ASan the REAL() call below may write to freed memory and
  1526. // corrupt its metadata. See
  1527. // https://github.com/google/sanitizers/issues/321.
  1528. #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
  1529. { \
  1530. VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
  1531. if (common_flags()->check_printf) { \
  1532. printf_common(ctx, format, aq); \
  1533. } \
  1534. int res = REAL(vname)(str, size, __VA_ARGS__); \
  1535. if (res >= 0) { \
  1536. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
  1537. } \
  1538. VPRINTF_INTERCEPTOR_RETURN(); \
  1539. return res; \
  1540. }
  1541. // FIXME: under ASan the REAL() call below may write to freed memory and
  1542. // corrupt its metadata. See
  1543. // https://github.com/google/sanitizers/issues/321.
  1544. #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
  1545. { \
  1546. VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
  1547. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
  1548. if (common_flags()->check_printf) { \
  1549. printf_common(ctx, format, aq); \
  1550. } \
  1551. int res = REAL(vname)(strp, __VA_ARGS__); \
  1552. if (res >= 0) { \
  1553. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
  1554. } \
  1555. VPRINTF_INTERCEPTOR_RETURN(); \
  1556. return res; \
  1557. }
  1558. INTERCEPTOR(int, vprintf, const char *format, va_list ap)
  1559. VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
  1560. INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
  1561. va_list ap)
  1562. VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
  1563. INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
  1564. va_list ap)
  1565. VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
  1566. #if SANITIZER_INTERCEPT___PRINTF_CHK
  1567. INTERCEPTOR(int, __vsnprintf_chk, char *str, SIZE_T size, int flag,
  1568. SIZE_T size_to, const char *format, va_list ap)
  1569. VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
  1570. #endif
  1571. #if SANITIZER_INTERCEPT_PRINTF_L
  1572. INTERCEPTOR(int, vsnprintf_l, char *str, SIZE_T size, void *loc,
  1573. const char *format, va_list ap)
  1574. VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf_l, str, size, loc, format, ap)
  1575. INTERCEPTOR(int, snprintf_l, char *str, SIZE_T size, void *loc,
  1576. const char *format, ...)
  1577. FORMAT_INTERCEPTOR_IMPL(snprintf_l, vsnprintf_l, str, size, loc, format)
  1578. #endif // SANITIZER_INTERCEPT_PRINTF_L
  1579. INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
  1580. VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
  1581. #if SANITIZER_INTERCEPT___PRINTF_CHK
  1582. INTERCEPTOR(int, __vsprintf_chk, char *str, int flag, SIZE_T size_to,
  1583. const char *format, va_list ap)
  1584. VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
  1585. #endif
  1586. INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
  1587. VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
  1588. #if SANITIZER_INTERCEPT_ISOC99_PRINTF
  1589. INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
  1590. VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
  1591. INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
  1592. const char *format, va_list ap)
  1593. VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
  1594. INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
  1595. va_list ap)
  1596. VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
  1597. INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
  1598. va_list ap)
  1599. VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
  1600. ap)
  1601. #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
  1602. INTERCEPTOR(int, printf, const char *format, ...)
  1603. FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
  1604. INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
  1605. FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
  1606. #if SANITIZER_INTERCEPT___PRINTF_CHK
  1607. INTERCEPTOR(int, __fprintf_chk, __sanitizer_FILE *stream, SIZE_T size,
  1608. const char *format, ...)
  1609. FORMAT_INTERCEPTOR_IMPL(__fprintf_chk, vfprintf, stream, format)
  1610. #endif
  1611. INTERCEPTOR(int, sprintf, char *str, const char *format, ...)
  1612. FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format)
  1613. #if SANITIZER_INTERCEPT___PRINTF_CHK
  1614. INTERCEPTOR(int, __sprintf_chk, char *str, int flag, SIZE_T size_to,
  1615. const char *format, ...)
  1616. FORMAT_INTERCEPTOR_IMPL(__sprintf_chk, vsprintf, str, format)
  1617. #endif
  1618. INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
  1619. FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
  1620. #if SANITIZER_INTERCEPT___PRINTF_CHK
  1621. INTERCEPTOR(int, __snprintf_chk, char *str, SIZE_T size, int flag,
  1622. SIZE_T size_to, const char *format, ...)
  1623. FORMAT_INTERCEPTOR_IMPL(__snprintf_chk, vsnprintf, str, size, format)
  1624. #endif
  1625. INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
  1626. FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
  1627. #if SANITIZER_INTERCEPT_ISOC99_PRINTF
  1628. INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
  1629. FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
  1630. INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
  1631. ...)
  1632. FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
  1633. INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
  1634. FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
  1635. INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
  1636. const char *format, ...)
  1637. FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
  1638. format)
  1639. #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
  1640. #endif // SANITIZER_INTERCEPT_PRINTF
  1641. #if SANITIZER_INTERCEPT_PRINTF
  1642. #define INIT_PRINTF \
  1643. COMMON_INTERCEPT_FUNCTION_LDBL(printf); \
  1644. COMMON_INTERCEPT_FUNCTION_LDBL(sprintf); \
  1645. COMMON_INTERCEPT_FUNCTION_LDBL(snprintf); \
  1646. COMMON_INTERCEPT_FUNCTION_LDBL(asprintf); \
  1647. COMMON_INTERCEPT_FUNCTION_LDBL(fprintf); \
  1648. COMMON_INTERCEPT_FUNCTION_LDBL(vprintf); \
  1649. COMMON_INTERCEPT_FUNCTION_LDBL(vsprintf); \
  1650. COMMON_INTERCEPT_FUNCTION_LDBL(vsnprintf); \
  1651. COMMON_INTERCEPT_FUNCTION_LDBL(vasprintf); \
  1652. COMMON_INTERCEPT_FUNCTION_LDBL(vfprintf);
  1653. #else
  1654. #define INIT_PRINTF
  1655. #endif
  1656. #if SANITIZER_INTERCEPT___PRINTF_CHK
  1657. #define INIT___PRINTF_CHK \
  1658. COMMON_INTERCEPT_FUNCTION(__sprintf_chk); \
  1659. COMMON_INTERCEPT_FUNCTION(__snprintf_chk); \
  1660. COMMON_INTERCEPT_FUNCTION(__vsprintf_chk); \
  1661. COMMON_INTERCEPT_FUNCTION(__vsnprintf_chk); \
  1662. COMMON_INTERCEPT_FUNCTION(__fprintf_chk);
  1663. #else
  1664. #define INIT___PRINTF_CHK
  1665. #endif
  1666. #if SANITIZER_INTERCEPT_PRINTF_L
  1667. #define INIT_PRINTF_L \
  1668. COMMON_INTERCEPT_FUNCTION(snprintf_l); \
  1669. COMMON_INTERCEPT_FUNCTION(vsnprintf_l);
  1670. #else
  1671. #define INIT_PRINTF_L
  1672. #endif
  1673. #if SANITIZER_INTERCEPT_ISOC99_PRINTF
  1674. #define INIT_ISOC99_PRINTF \
  1675. COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
  1676. COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
  1677. COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
  1678. COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
  1679. COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
  1680. COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
  1681. COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
  1682. COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
  1683. #else
  1684. #define INIT_ISOC99_PRINTF
  1685. #endif
  1686. #if SANITIZER_INTERCEPT_IOCTL
  1687. #include "sanitizer_common_interceptors_ioctl.inc"
  1688. #include "sanitizer_interceptors_ioctl_netbsd.inc"
  1689. INTERCEPTOR(int, ioctl, int d, unsigned long request, ...) {
  1690. // We need a frame pointer, because we call into ioctl_common_[pre|post] which
  1691. // can trigger a report and we need to be able to unwind through this
  1692. // function. On Mac in debug mode we might not have a frame pointer, because
  1693. // ioctl_common_[pre|post] doesn't get inlined here.
  1694. ENABLE_FRAME_POINTER;
  1695. void *ctx;
  1696. va_list ap;
  1697. va_start(ap, request);
  1698. void *arg = va_arg(ap, void *);
  1699. va_end(ap);
  1700. COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
  1701. CHECK(ioctl_initialized);
  1702. // Note: TSan does not use common flags, and they are zero-initialized.
  1703. // This effectively disables ioctl handling in TSan.
  1704. if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
  1705. // Although request is unsigned long, the rest of the interceptor uses it
  1706. // as just "unsigned" to save space, because we know that all values fit in
  1707. // "unsigned" - they are compile-time constants.
  1708. const ioctl_desc *desc = ioctl_lookup(request);
  1709. ioctl_desc decoded_desc;
  1710. if (!desc) {
  1711. VPrintf(2, "Decoding unknown ioctl 0x%lx\n", request);
  1712. if (!ioctl_decode(request, &decoded_desc))
  1713. Printf("WARNING: failed decoding unknown ioctl 0x%lx\n", request);
  1714. else
  1715. desc = &decoded_desc;
  1716. }
  1717. if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
  1718. int res = REAL(ioctl)(d, request, arg);
  1719. // FIXME: some ioctls have different return values for success and failure.
  1720. if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
  1721. return res;
  1722. }
  1723. #define INIT_IOCTL \
  1724. ioctl_init(); \
  1725. COMMON_INTERCEPT_FUNCTION(ioctl);
  1726. #else
  1727. #define INIT_IOCTL
  1728. #endif
  1729. #if SANITIZER_POSIX
  1730. UNUSED static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
  1731. if (pwd) {
  1732. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
  1733. if (pwd->pw_name)
  1734. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_name,
  1735. internal_strlen(pwd->pw_name) + 1);
  1736. if (pwd->pw_passwd)
  1737. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_passwd,
  1738. internal_strlen(pwd->pw_passwd) + 1);
  1739. #if !SANITIZER_ANDROID
  1740. if (pwd->pw_gecos)
  1741. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_gecos,
  1742. internal_strlen(pwd->pw_gecos) + 1);
  1743. #endif
  1744. #if SANITIZER_APPLE || SANITIZER_FREEBSD || SANITIZER_NETBSD
  1745. if (pwd->pw_class)
  1746. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_class,
  1747. internal_strlen(pwd->pw_class) + 1);
  1748. #endif
  1749. if (pwd->pw_dir)
  1750. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_dir,
  1751. internal_strlen(pwd->pw_dir) + 1);
  1752. if (pwd->pw_shell)
  1753. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd->pw_shell,
  1754. internal_strlen(pwd->pw_shell) + 1);
  1755. }
  1756. }
  1757. UNUSED static void unpoison_group(void *ctx, __sanitizer_group *grp) {
  1758. if (grp) {
  1759. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
  1760. if (grp->gr_name)
  1761. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_name,
  1762. internal_strlen(grp->gr_name) + 1);
  1763. if (grp->gr_passwd)
  1764. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_passwd,
  1765. internal_strlen(grp->gr_passwd) + 1);
  1766. char **p = grp->gr_mem;
  1767. for (; *p; ++p) {
  1768. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
  1769. }
  1770. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp->gr_mem,
  1771. (p - grp->gr_mem + 1) * sizeof(*p));
  1772. }
  1773. }
  1774. #endif // SANITIZER_POSIX
  1775. #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
  1776. INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
  1777. void *ctx;
  1778. COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
  1779. if (name)
  1780. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  1781. __sanitizer_passwd *res = REAL(getpwnam)(name);
  1782. unpoison_passwd(ctx, res);
  1783. return res;
  1784. }
  1785. INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
  1786. void *ctx;
  1787. COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
  1788. __sanitizer_passwd *res = REAL(getpwuid)(uid);
  1789. unpoison_passwd(ctx, res);
  1790. return res;
  1791. }
  1792. INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
  1793. void *ctx;
  1794. COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
  1795. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  1796. __sanitizer_group *res = REAL(getgrnam)(name);
  1797. unpoison_group(ctx, res);
  1798. return res;
  1799. }
  1800. INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
  1801. void *ctx;
  1802. COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
  1803. __sanitizer_group *res = REAL(getgrgid)(gid);
  1804. unpoison_group(ctx, res);
  1805. return res;
  1806. }
  1807. #define INIT_GETPWNAM_AND_FRIENDS \
  1808. COMMON_INTERCEPT_FUNCTION(getpwnam); \
  1809. COMMON_INTERCEPT_FUNCTION(getpwuid); \
  1810. COMMON_INTERCEPT_FUNCTION(getgrnam); \
  1811. COMMON_INTERCEPT_FUNCTION(getgrgid);
  1812. #else
  1813. #define INIT_GETPWNAM_AND_FRIENDS
  1814. #endif
  1815. #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
  1816. INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
  1817. char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
  1818. void *ctx;
  1819. COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
  1820. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  1821. // FIXME: under ASan the call below may write to freed memory and corrupt
  1822. // its metadata. See
  1823. // https://github.com/google/sanitizers/issues/321.
  1824. int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
  1825. if (!res && result)
  1826. unpoison_passwd(ctx, *result);
  1827. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1828. return res;
  1829. }
  1830. INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
  1831. SIZE_T buflen, __sanitizer_passwd **result) {
  1832. void *ctx;
  1833. COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
  1834. // FIXME: under ASan the call below may write to freed memory and corrupt
  1835. // its metadata. See
  1836. // https://github.com/google/sanitizers/issues/321.
  1837. int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
  1838. if (!res && result)
  1839. unpoison_passwd(ctx, *result);
  1840. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1841. return res;
  1842. }
  1843. INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
  1844. char *buf, SIZE_T buflen, __sanitizer_group **result) {
  1845. void *ctx;
  1846. COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
  1847. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  1848. // FIXME: under ASan the call below may write to freed memory and corrupt
  1849. // its metadata. See
  1850. // https://github.com/google/sanitizers/issues/321.
  1851. int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
  1852. if (!res && result)
  1853. unpoison_group(ctx, *result);
  1854. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1855. return res;
  1856. }
  1857. INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
  1858. SIZE_T buflen, __sanitizer_group **result) {
  1859. void *ctx;
  1860. COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
  1861. // FIXME: under ASan the call below may write to freed memory and corrupt
  1862. // its metadata. See
  1863. // https://github.com/google/sanitizers/issues/321.
  1864. int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
  1865. if (!res && result)
  1866. unpoison_group(ctx, *result);
  1867. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1868. return res;
  1869. }
  1870. #define INIT_GETPWNAM_R_AND_FRIENDS \
  1871. COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
  1872. COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
  1873. COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
  1874. COMMON_INTERCEPT_FUNCTION(getgrgid_r);
  1875. #else
  1876. #define INIT_GETPWNAM_R_AND_FRIENDS
  1877. #endif
  1878. #if SANITIZER_INTERCEPT_GETPWENT
  1879. INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
  1880. void *ctx;
  1881. COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
  1882. __sanitizer_passwd *res = REAL(getpwent)(dummy);
  1883. unpoison_passwd(ctx, res);
  1884. return res;
  1885. }
  1886. INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
  1887. void *ctx;
  1888. COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
  1889. __sanitizer_group *res = REAL(getgrent)(dummy);
  1890. unpoison_group(ctx, res);
  1891. return res;
  1892. }
  1893. #define INIT_GETPWENT \
  1894. COMMON_INTERCEPT_FUNCTION(getpwent); \
  1895. COMMON_INTERCEPT_FUNCTION(getgrent);
  1896. #else
  1897. #define INIT_GETPWENT
  1898. #endif
  1899. #if SANITIZER_INTERCEPT_FGETPWENT
  1900. INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
  1901. void *ctx;
  1902. COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
  1903. __sanitizer_passwd *res = REAL(fgetpwent)(fp);
  1904. unpoison_passwd(ctx, res);
  1905. return res;
  1906. }
  1907. INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
  1908. void *ctx;
  1909. COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
  1910. __sanitizer_group *res = REAL(fgetgrent)(fp);
  1911. unpoison_group(ctx, res);
  1912. return res;
  1913. }
  1914. #define INIT_FGETPWENT \
  1915. COMMON_INTERCEPT_FUNCTION(fgetpwent); \
  1916. COMMON_INTERCEPT_FUNCTION(fgetgrent);
  1917. #else
  1918. #define INIT_FGETPWENT
  1919. #endif
  1920. #if SANITIZER_INTERCEPT_GETPWENT_R
  1921. INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
  1922. SIZE_T buflen, __sanitizer_passwd **pwbufp) {
  1923. void *ctx;
  1924. COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
  1925. // FIXME: under ASan the call below may write to freed memory and corrupt
  1926. // its metadata. See
  1927. // https://github.com/google/sanitizers/issues/321.
  1928. int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
  1929. if (!res && pwbufp)
  1930. unpoison_passwd(ctx, *pwbufp);
  1931. if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
  1932. return res;
  1933. }
  1934. INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
  1935. __sanitizer_group **pwbufp) {
  1936. void *ctx;
  1937. COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
  1938. // FIXME: under ASan the call below may write to freed memory and corrupt
  1939. // its metadata. See
  1940. // https://github.com/google/sanitizers/issues/321.
  1941. int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
  1942. if (!res && pwbufp)
  1943. unpoison_group(ctx, *pwbufp);
  1944. if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
  1945. return res;
  1946. }
  1947. #define INIT_GETPWENT_R \
  1948. COMMON_INTERCEPT_FUNCTION(getpwent_r); \
  1949. COMMON_INTERCEPT_FUNCTION(getgrent_r);
  1950. #else
  1951. #define INIT_GETPWENT_R
  1952. #endif
  1953. #if SANITIZER_INTERCEPT_FGETPWENT_R
  1954. INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
  1955. SIZE_T buflen, __sanitizer_passwd **pwbufp) {
  1956. void *ctx;
  1957. COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
  1958. // FIXME: under ASan the call below may write to freed memory and corrupt
  1959. // its metadata. See
  1960. // https://github.com/google/sanitizers/issues/321.
  1961. int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
  1962. if (!res && pwbufp)
  1963. unpoison_passwd(ctx, *pwbufp);
  1964. if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
  1965. return res;
  1966. }
  1967. #define INIT_FGETPWENT_R \
  1968. COMMON_INTERCEPT_FUNCTION(fgetpwent_r);
  1969. #else
  1970. #define INIT_FGETPWENT_R
  1971. #endif
  1972. #if SANITIZER_INTERCEPT_FGETGRENT_R
  1973. INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
  1974. SIZE_T buflen, __sanitizer_group **pwbufp) {
  1975. void *ctx;
  1976. COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
  1977. // FIXME: under ASan the call below may write to freed memory and corrupt
  1978. // its metadata. See
  1979. // https://github.com/google/sanitizers/issues/321.
  1980. int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
  1981. if (!res && pwbufp)
  1982. unpoison_group(ctx, *pwbufp);
  1983. if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
  1984. return res;
  1985. }
  1986. #define INIT_FGETGRENT_R \
  1987. COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
  1988. #else
  1989. #define INIT_FGETGRENT_R
  1990. #endif
  1991. #if SANITIZER_INTERCEPT_SETPWENT
  1992. // The only thing these interceptors do is disable any nested interceptors.
  1993. // These functions may open nss modules and call uninstrumented functions from
  1994. // them, and we don't want things like strlen() to trigger.
  1995. INTERCEPTOR(void, setpwent, int dummy) {
  1996. void *ctx;
  1997. COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
  1998. REAL(setpwent)(dummy);
  1999. }
  2000. INTERCEPTOR(void, endpwent, int dummy) {
  2001. void *ctx;
  2002. COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
  2003. REAL(endpwent)(dummy);
  2004. }
  2005. INTERCEPTOR(void, setgrent, int dummy) {
  2006. void *ctx;
  2007. COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
  2008. REAL(setgrent)(dummy);
  2009. }
  2010. INTERCEPTOR(void, endgrent, int dummy) {
  2011. void *ctx;
  2012. COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
  2013. REAL(endgrent)(dummy);
  2014. }
  2015. #define INIT_SETPWENT \
  2016. COMMON_INTERCEPT_FUNCTION(setpwent); \
  2017. COMMON_INTERCEPT_FUNCTION(endpwent); \
  2018. COMMON_INTERCEPT_FUNCTION(setgrent); \
  2019. COMMON_INTERCEPT_FUNCTION(endgrent);
  2020. #else
  2021. #define INIT_SETPWENT
  2022. #endif
  2023. #if SANITIZER_INTERCEPT_CLOCK_GETTIME
  2024. INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
  2025. void *ctx;
  2026. COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
  2027. // FIXME: under ASan the call below may write to freed memory and corrupt
  2028. // its metadata. See
  2029. // https://github.com/google/sanitizers/issues/321.
  2030. int res = REAL(clock_getres)(clk_id, tp);
  2031. if (!res && tp) {
  2032. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
  2033. }
  2034. return res;
  2035. }
  2036. INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
  2037. void *ctx;
  2038. COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
  2039. // FIXME: under ASan the call below may write to freed memory and corrupt
  2040. // its metadata. See
  2041. // https://github.com/google/sanitizers/issues/321.
  2042. int res = REAL(clock_gettime)(clk_id, tp);
  2043. if (!res) {
  2044. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
  2045. }
  2046. return res;
  2047. }
  2048. #if SANITIZER_GLIBC
  2049. namespace __sanitizer {
  2050. extern "C" {
  2051. int real_clock_gettime(u32 clk_id, void *tp) {
  2052. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  2053. return internal_clock_gettime(clk_id, tp);
  2054. return REAL(clock_gettime)(clk_id, tp);
  2055. }
  2056. } // extern "C"
  2057. } // namespace __sanitizer
  2058. #endif
  2059. INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
  2060. void *ctx;
  2061. COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
  2062. COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
  2063. return REAL(clock_settime)(clk_id, tp);
  2064. }
  2065. #define INIT_CLOCK_GETTIME \
  2066. COMMON_INTERCEPT_FUNCTION(clock_getres); \
  2067. COMMON_INTERCEPT_FUNCTION(clock_gettime); \
  2068. COMMON_INTERCEPT_FUNCTION(clock_settime);
  2069. #else
  2070. #define INIT_CLOCK_GETTIME
  2071. #endif
  2072. #if SANITIZER_INTERCEPT_CLOCK_GETCPUCLOCKID
  2073. INTERCEPTOR(int, clock_getcpuclockid, pid_t pid,
  2074. __sanitizer_clockid_t *clockid) {
  2075. void *ctx;
  2076. COMMON_INTERCEPTOR_ENTER(ctx, clock_getcpuclockid, pid, clockid);
  2077. int res = REAL(clock_getcpuclockid)(pid, clockid);
  2078. if (!res && clockid) {
  2079. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
  2080. }
  2081. return res;
  2082. }
  2083. INTERCEPTOR(int, pthread_getcpuclockid, uptr thread,
  2084. __sanitizer_clockid_t *clockid) {
  2085. void *ctx;
  2086. COMMON_INTERCEPTOR_ENTER(ctx, pthread_getcpuclockid, thread, clockid);
  2087. int res = REAL(pthread_getcpuclockid)(thread, clockid);
  2088. if (!res && clockid) {
  2089. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, clockid, sizeof *clockid);
  2090. }
  2091. return res;
  2092. }
  2093. #define INIT_CLOCK_GETCPUCLOCKID \
  2094. COMMON_INTERCEPT_FUNCTION(clock_getcpuclockid); \
  2095. COMMON_INTERCEPT_FUNCTION(pthread_getcpuclockid);
  2096. #else
  2097. #define INIT_CLOCK_GETCPUCLOCKID
  2098. #endif
  2099. #if SANITIZER_INTERCEPT_GETITIMER
  2100. INTERCEPTOR(int, getitimer, int which, void *curr_value) {
  2101. void *ctx;
  2102. COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
  2103. // FIXME: under ASan the call below may write to freed memory and corrupt
  2104. // its metadata. See
  2105. // https://github.com/google/sanitizers/issues/321.
  2106. int res = REAL(getitimer)(which, curr_value);
  2107. if (!res && curr_value) {
  2108. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
  2109. }
  2110. return res;
  2111. }
  2112. INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
  2113. void *ctx;
  2114. COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
  2115. if (new_value) {
  2116. // itimerval can contain padding that may be legitimately uninitialized
  2117. const struct __sanitizer_itimerval *nv =
  2118. (const struct __sanitizer_itimerval *)new_value;
  2119. COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_sec,
  2120. sizeof(__sanitizer_time_t));
  2121. COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_interval.tv_usec,
  2122. sizeof(__sanitizer_suseconds_t));
  2123. COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_sec,
  2124. sizeof(__sanitizer_time_t));
  2125. COMMON_INTERCEPTOR_READ_RANGE(ctx, &nv->it_value.tv_usec,
  2126. sizeof(__sanitizer_suseconds_t));
  2127. }
  2128. // FIXME: under ASan the call below may write to freed memory and corrupt
  2129. // its metadata. See
  2130. // https://github.com/google/sanitizers/issues/321.
  2131. int res = REAL(setitimer)(which, new_value, old_value);
  2132. if (!res && old_value) {
  2133. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
  2134. }
  2135. return res;
  2136. }
  2137. #define INIT_GETITIMER \
  2138. COMMON_INTERCEPT_FUNCTION(getitimer); \
  2139. COMMON_INTERCEPT_FUNCTION(setitimer);
  2140. #else
  2141. #define INIT_GETITIMER
  2142. #endif
  2143. #if SANITIZER_INTERCEPT_GLOB
  2144. static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
  2145. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
  2146. // +1 for NULL pointer at the end.
  2147. if (pglob->gl_pathv)
  2148. COMMON_INTERCEPTOR_WRITE_RANGE(
  2149. ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
  2150. for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
  2151. char *p = pglob->gl_pathv[i];
  2152. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, internal_strlen(p) + 1);
  2153. }
  2154. }
  2155. #if SANITIZER_SOLARIS
  2156. INTERCEPTOR(int, glob, const char *pattern, int flags,
  2157. int (*errfunc)(const char *epath, int eerrno),
  2158. __sanitizer_glob_t *pglob) {
  2159. void *ctx;
  2160. COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
  2161. COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
  2162. int res = REAL(glob)(pattern, flags, errfunc, pglob);
  2163. if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
  2164. return res;
  2165. }
  2166. #else
  2167. static THREADLOCAL __sanitizer_glob_t *pglob_copy;
  2168. static void wrapped_gl_closedir(void *dir) {
  2169. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  2170. pglob_copy->gl_closedir(dir);
  2171. }
  2172. static void *wrapped_gl_readdir(void *dir) {
  2173. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  2174. return pglob_copy->gl_readdir(dir);
  2175. }
  2176. static void *wrapped_gl_opendir(const char *s) {
  2177. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  2178. COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
  2179. return pglob_copy->gl_opendir(s);
  2180. }
  2181. static int wrapped_gl_lstat(const char *s, void *st) {
  2182. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  2183. COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
  2184. return pglob_copy->gl_lstat(s, st);
  2185. }
  2186. static int wrapped_gl_stat(const char *s, void *st) {
  2187. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  2188. COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, internal_strlen(s) + 1);
  2189. return pglob_copy->gl_stat(s, st);
  2190. }
  2191. static const __sanitizer_glob_t kGlobCopy = {
  2192. 0, 0, 0,
  2193. 0, wrapped_gl_closedir, wrapped_gl_readdir,
  2194. wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
  2195. INTERCEPTOR(int, glob, const char *pattern, int flags,
  2196. int (*errfunc)(const char *epath, int eerrno),
  2197. __sanitizer_glob_t *pglob) {
  2198. void *ctx;
  2199. COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
  2200. COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
  2201. __sanitizer_glob_t glob_copy;
  2202. internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
  2203. if (flags & glob_altdirfunc) {
  2204. Swap(pglob->gl_closedir, glob_copy.gl_closedir);
  2205. Swap(pglob->gl_readdir, glob_copy.gl_readdir);
  2206. Swap(pglob->gl_opendir, glob_copy.gl_opendir);
  2207. Swap(pglob->gl_lstat, glob_copy.gl_lstat);
  2208. Swap(pglob->gl_stat, glob_copy.gl_stat);
  2209. pglob_copy = &glob_copy;
  2210. }
  2211. int res = REAL(glob)(pattern, flags, errfunc, pglob);
  2212. if (flags & glob_altdirfunc) {
  2213. Swap(pglob->gl_closedir, glob_copy.gl_closedir);
  2214. Swap(pglob->gl_readdir, glob_copy.gl_readdir);
  2215. Swap(pglob->gl_opendir, glob_copy.gl_opendir);
  2216. Swap(pglob->gl_lstat, glob_copy.gl_lstat);
  2217. Swap(pglob->gl_stat, glob_copy.gl_stat);
  2218. }
  2219. pglob_copy = 0;
  2220. if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
  2221. return res;
  2222. }
  2223. #endif // SANITIZER_SOLARIS
  2224. #define INIT_GLOB \
  2225. COMMON_INTERCEPT_FUNCTION(glob);
  2226. #else // SANITIZER_INTERCEPT_GLOB
  2227. #define INIT_GLOB
  2228. #endif // SANITIZER_INTERCEPT_GLOB
  2229. #if SANITIZER_INTERCEPT_GLOB64
  2230. INTERCEPTOR(int, glob64, const char *pattern, int flags,
  2231. int (*errfunc)(const char *epath, int eerrno),
  2232. __sanitizer_glob_t *pglob) {
  2233. void *ctx;
  2234. COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
  2235. COMMON_INTERCEPTOR_READ_STRING(ctx, pattern, 0);
  2236. __sanitizer_glob_t glob_copy;
  2237. internal_memcpy(&glob_copy, &kGlobCopy, sizeof(glob_copy));
  2238. if (flags & glob_altdirfunc) {
  2239. Swap(pglob->gl_closedir, glob_copy.gl_closedir);
  2240. Swap(pglob->gl_readdir, glob_copy.gl_readdir);
  2241. Swap(pglob->gl_opendir, glob_copy.gl_opendir);
  2242. Swap(pglob->gl_lstat, glob_copy.gl_lstat);
  2243. Swap(pglob->gl_stat, glob_copy.gl_stat);
  2244. pglob_copy = &glob_copy;
  2245. }
  2246. int res = REAL(glob64)(pattern, flags, errfunc, pglob);
  2247. if (flags & glob_altdirfunc) {
  2248. Swap(pglob->gl_closedir, glob_copy.gl_closedir);
  2249. Swap(pglob->gl_readdir, glob_copy.gl_readdir);
  2250. Swap(pglob->gl_opendir, glob_copy.gl_opendir);
  2251. Swap(pglob->gl_lstat, glob_copy.gl_lstat);
  2252. Swap(pglob->gl_stat, glob_copy.gl_stat);
  2253. }
  2254. pglob_copy = 0;
  2255. if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
  2256. return res;
  2257. }
  2258. #define INIT_GLOB64 \
  2259. COMMON_INTERCEPT_FUNCTION(glob64);
  2260. #else // SANITIZER_INTERCEPT_GLOB64
  2261. #define INIT_GLOB64
  2262. #endif // SANITIZER_INTERCEPT_GLOB64
  2263. #if SANITIZER_INTERCEPT___B64_TO
  2264. INTERCEPTOR(int, __b64_ntop, unsigned char const *src, SIZE_T srclength,
  2265. char *target, SIZE_T targsize) {
  2266. void *ctx;
  2267. COMMON_INTERCEPTOR_ENTER(ctx, __b64_ntop, src, srclength, target, targsize);
  2268. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, srclength);
  2269. int res = REAL(__b64_ntop)(src, srclength, target, targsize);
  2270. if (res >= 0)
  2271. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res + 1);
  2272. return res;
  2273. }
  2274. INTERCEPTOR(int, __b64_pton, char const *src, char *target, SIZE_T targsize) {
  2275. void *ctx;
  2276. COMMON_INTERCEPTOR_ENTER(ctx, __b64_pton, src, target, targsize);
  2277. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  2278. int res = REAL(__b64_pton)(src, target, targsize);
  2279. if (res >= 0)
  2280. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, target, res);
  2281. return res;
  2282. }
  2283. #define INIT___B64_TO \
  2284. COMMON_INTERCEPT_FUNCTION(__b64_ntop); \
  2285. COMMON_INTERCEPT_FUNCTION(__b64_pton);
  2286. #else // SANITIZER_INTERCEPT___B64_TO
  2287. #define INIT___B64_TO
  2288. #endif // SANITIZER_INTERCEPT___B64_TO
  2289. #if SANITIZER_INTERCEPT_DN_COMP_EXPAND
  2290. # if __GLIBC_PREREQ(2, 34)
  2291. // Changed with https://sourceware.org/git/?p=glibc.git;h=640bbdf
  2292. # define DN_COMP_INTERCEPTOR_NAME dn_comp
  2293. # define DN_EXPAND_INTERCEPTOR_NAME dn_expand
  2294. # else
  2295. # define DN_COMP_INTERCEPTOR_NAME __dn_comp
  2296. # define DN_EXPAND_INTERCEPTOR_NAME __dn_expand
  2297. # endif
  2298. INTERCEPTOR(int, DN_COMP_INTERCEPTOR_NAME, unsigned char *exp_dn,
  2299. unsigned char *comp_dn, int length, unsigned char **dnptrs,
  2300. unsigned char **lastdnptr) {
  2301. void *ctx;
  2302. COMMON_INTERCEPTOR_ENTER(ctx, DN_COMP_INTERCEPTOR_NAME, exp_dn, comp_dn,
  2303. length, dnptrs, lastdnptr);
  2304. int res = REAL(DN_COMP_INTERCEPTOR_NAME)(exp_dn, comp_dn, length, dnptrs,
  2305. lastdnptr);
  2306. if (res >= 0) {
  2307. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, comp_dn, res);
  2308. if (dnptrs && lastdnptr) {
  2309. unsigned char **p = dnptrs;
  2310. for (; p != lastdnptr && *p; ++p)
  2311. ;
  2312. if (p != lastdnptr)
  2313. ++p;
  2314. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dnptrs, (p - dnptrs) * sizeof(*p));
  2315. }
  2316. }
  2317. return res;
  2318. }
  2319. INTERCEPTOR(int, DN_EXPAND_INTERCEPTOR_NAME, unsigned char const *base,
  2320. unsigned char const *end, unsigned char const *src, char *dest,
  2321. int space) {
  2322. void *ctx;
  2323. COMMON_INTERCEPTOR_ENTER(ctx, DN_EXPAND_INTERCEPTOR_NAME, base, end, src,
  2324. dest, space);
  2325. // TODO: add read check if __dn_comp intercept added
  2326. int res = REAL(DN_EXPAND_INTERCEPTOR_NAME)(base, end, src, dest, space);
  2327. if (res >= 0)
  2328. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, internal_strlen(dest) + 1);
  2329. return res;
  2330. }
  2331. # define INIT_DN_COMP_EXPAND \
  2332. COMMON_INTERCEPT_FUNCTION(DN_COMP_INTERCEPTOR_NAME); \
  2333. COMMON_INTERCEPT_FUNCTION(DN_EXPAND_INTERCEPTOR_NAME);
  2334. #else // SANITIZER_INTERCEPT_DN_COMP_EXPAND
  2335. # define INIT_DN_COMP_EXPAND
  2336. #endif // SANITIZER_INTERCEPT_DN_COMP_EXPAND
  2337. #if SANITIZER_INTERCEPT_POSIX_SPAWN
  2338. template <class RealSpawnPtr>
  2339. static int PosixSpawnImpl(void *ctx, RealSpawnPtr *real_posix_spawn, pid_t *pid,
  2340. const char *file_or_path, const void *file_actions,
  2341. const void *attrp, char *const argv[],
  2342. char *const envp[]) {
  2343. COMMON_INTERCEPTOR_READ_RANGE(ctx, file_or_path,
  2344. internal_strlen(file_or_path) + 1);
  2345. if (argv) {
  2346. for (char *const *s = argv; ; ++s) {
  2347. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(*s));
  2348. if (!*s) break;
  2349. COMMON_INTERCEPTOR_READ_RANGE(ctx, *s, internal_strlen(*s) + 1);
  2350. }
  2351. }
  2352. if (envp) {
  2353. for (char *const *s = envp; ; ++s) {
  2354. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(*s));
  2355. if (!*s) break;
  2356. COMMON_INTERCEPTOR_READ_RANGE(ctx, *s, internal_strlen(*s) + 1);
  2357. }
  2358. }
  2359. int res =
  2360. real_posix_spawn(pid, file_or_path, file_actions, attrp, argv, envp);
  2361. if (res == 0)
  2362. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pid, sizeof(*pid));
  2363. return res;
  2364. }
  2365. INTERCEPTOR(int, posix_spawn, pid_t *pid, const char *path,
  2366. const void *file_actions, const void *attrp, char *const argv[],
  2367. char *const envp[]) {
  2368. void *ctx;
  2369. COMMON_INTERCEPTOR_ENTER(ctx, posix_spawn, pid, path, file_actions, attrp,
  2370. argv, envp);
  2371. return PosixSpawnImpl(ctx, REAL(posix_spawn), pid, path, file_actions, attrp,
  2372. argv, envp);
  2373. }
  2374. INTERCEPTOR(int, posix_spawnp, pid_t *pid, const char *file,
  2375. const void *file_actions, const void *attrp, char *const argv[],
  2376. char *const envp[]) {
  2377. void *ctx;
  2378. COMMON_INTERCEPTOR_ENTER(ctx, posix_spawnp, pid, file, file_actions, attrp,
  2379. argv, envp);
  2380. return PosixSpawnImpl(ctx, REAL(posix_spawnp), pid, file, file_actions, attrp,
  2381. argv, envp);
  2382. }
  2383. # define INIT_POSIX_SPAWN \
  2384. COMMON_INTERCEPT_FUNCTION(posix_spawn); \
  2385. COMMON_INTERCEPT_FUNCTION(posix_spawnp);
  2386. #else // SANITIZER_INTERCEPT_POSIX_SPAWN
  2387. # define INIT_POSIX_SPAWN
  2388. #endif // SANITIZER_INTERCEPT_POSIX_SPAWN
  2389. #if SANITIZER_INTERCEPT_WAIT
  2390. // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
  2391. // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
  2392. // details.
  2393. INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
  2394. void *ctx;
  2395. COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
  2396. // FIXME: under ASan the call below may write to freed memory and corrupt
  2397. // its metadata. See
  2398. // https://github.com/google/sanitizers/issues/321.
  2399. int res = REAL(wait)(status);
  2400. if (res != -1 && status)
  2401. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
  2402. return res;
  2403. }
  2404. // On FreeBSD id_t is always 64-bit wide.
  2405. #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
  2406. INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
  2407. int options) {
  2408. #else
  2409. INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
  2410. int options) {
  2411. #endif
  2412. void *ctx;
  2413. COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
  2414. // FIXME: under ASan the call below may write to freed memory and corrupt
  2415. // its metadata. See
  2416. // https://github.com/google/sanitizers/issues/321.
  2417. int res = REAL(waitid)(idtype, id, infop, options);
  2418. if (res != -1 && infop)
  2419. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
  2420. return res;
  2421. }
  2422. INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
  2423. void *ctx;
  2424. COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
  2425. // FIXME: under ASan the call below may write to freed memory and corrupt
  2426. // its metadata. See
  2427. // https://github.com/google/sanitizers/issues/321.
  2428. int res = REAL(waitpid)(pid, status, options);
  2429. if (res != -1 && status)
  2430. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
  2431. return res;
  2432. }
  2433. INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
  2434. void *ctx;
  2435. COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
  2436. // FIXME: under ASan the call below may write to freed memory and corrupt
  2437. // its metadata. See
  2438. // https://github.com/google/sanitizers/issues/321.
  2439. int res = REAL(wait3)(status, options, rusage);
  2440. if (res != -1) {
  2441. if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
  2442. if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
  2443. }
  2444. return res;
  2445. }
  2446. #if SANITIZER_ANDROID
  2447. INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
  2448. void *ctx;
  2449. COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
  2450. // FIXME: under ASan the call below may write to freed memory and corrupt
  2451. // its metadata. See
  2452. // https://github.com/google/sanitizers/issues/321.
  2453. int res = REAL(__wait4)(pid, status, options, rusage);
  2454. if (res != -1) {
  2455. if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
  2456. if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
  2457. }
  2458. return res;
  2459. }
  2460. #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
  2461. #else
  2462. INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
  2463. void *ctx;
  2464. COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
  2465. // FIXME: under ASan the call below may write to freed memory and corrupt
  2466. // its metadata. See
  2467. // https://github.com/google/sanitizers/issues/321.
  2468. int res = REAL(wait4)(pid, status, options, rusage);
  2469. if (res != -1) {
  2470. if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
  2471. if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
  2472. }
  2473. return res;
  2474. }
  2475. #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
  2476. #endif // SANITIZER_ANDROID
  2477. #define INIT_WAIT \
  2478. COMMON_INTERCEPT_FUNCTION(wait); \
  2479. COMMON_INTERCEPT_FUNCTION(waitid); \
  2480. COMMON_INTERCEPT_FUNCTION(waitpid); \
  2481. COMMON_INTERCEPT_FUNCTION(wait3);
  2482. #else
  2483. #define INIT_WAIT
  2484. #define INIT_WAIT4
  2485. #endif
  2486. #if SANITIZER_INTERCEPT_INET
  2487. INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
  2488. void *ctx;
  2489. COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
  2490. uptr sz = __sanitizer_in_addr_sz(af);
  2491. if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
  2492. // FIXME: figure out read size based on the address family.
  2493. // FIXME: under ASan the call below may write to freed memory and corrupt
  2494. // its metadata. See
  2495. // https://github.com/google/sanitizers/issues/321.
  2496. char *res = REAL(inet_ntop)(af, src, dst, size);
  2497. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  2498. return res;
  2499. }
  2500. INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
  2501. void *ctx;
  2502. COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
  2503. COMMON_INTERCEPTOR_READ_STRING(ctx, src, 0);
  2504. // FIXME: figure out read size based on the address family.
  2505. // FIXME: under ASan the call below may write to freed memory and corrupt
  2506. // its metadata. See
  2507. // https://github.com/google/sanitizers/issues/321.
  2508. int res = REAL(inet_pton)(af, src, dst);
  2509. if (res == 1) {
  2510. uptr sz = __sanitizer_in_addr_sz(af);
  2511. if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
  2512. }
  2513. return res;
  2514. }
  2515. #define INIT_INET \
  2516. COMMON_INTERCEPT_FUNCTION(inet_ntop); \
  2517. COMMON_INTERCEPT_FUNCTION(inet_pton);
  2518. #else
  2519. #define INIT_INET
  2520. #endif
  2521. #if SANITIZER_INTERCEPT_INET
  2522. INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
  2523. void *ctx;
  2524. COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
  2525. if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, internal_strlen(cp) + 1);
  2526. // FIXME: under ASan the call below may write to freed memory and corrupt
  2527. // its metadata. See
  2528. // https://github.com/google/sanitizers/issues/321.
  2529. int res = REAL(inet_aton)(cp, dst);
  2530. if (res != 0) {
  2531. uptr sz = __sanitizer_in_addr_sz(af_inet);
  2532. if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
  2533. }
  2534. return res;
  2535. }
  2536. #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
  2537. #else
  2538. #define INIT_INET_ATON
  2539. #endif
  2540. #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
  2541. INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
  2542. void *ctx;
  2543. COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
  2544. // FIXME: under ASan the call below may write to freed memory and corrupt
  2545. // its metadata. See
  2546. // https://github.com/google/sanitizers/issues/321.
  2547. int res = REAL(pthread_getschedparam)(thread, policy, param);
  2548. if (res == 0) {
  2549. if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
  2550. if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
  2551. }
  2552. return res;
  2553. }
  2554. #define INIT_PTHREAD_GETSCHEDPARAM \
  2555. COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
  2556. #else
  2557. #define INIT_PTHREAD_GETSCHEDPARAM
  2558. #endif
  2559. #if SANITIZER_INTERCEPT_GETADDRINFO
  2560. INTERCEPTOR(int, getaddrinfo, char *node, char *service,
  2561. struct __sanitizer_addrinfo *hints,
  2562. struct __sanitizer_addrinfo **out) {
  2563. void *ctx;
  2564. COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
  2565. if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, internal_strlen(node) + 1);
  2566. if (service)
  2567. COMMON_INTERCEPTOR_READ_RANGE(ctx, service, internal_strlen(service) + 1);
  2568. if (hints)
  2569. COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
  2570. // FIXME: under ASan the call below may write to freed memory and corrupt
  2571. // its metadata. See
  2572. // https://github.com/google/sanitizers/issues/321.
  2573. int res = REAL(getaddrinfo)(node, service, hints, out);
  2574. if (res == 0 && out) {
  2575. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
  2576. struct __sanitizer_addrinfo *p = *out;
  2577. while (p) {
  2578. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
  2579. if (p->ai_addr)
  2580. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
  2581. if (p->ai_canonname)
  2582. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
  2583. internal_strlen(p->ai_canonname) + 1);
  2584. p = p->ai_next;
  2585. }
  2586. }
  2587. return res;
  2588. }
  2589. #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
  2590. #else
  2591. #define INIT_GETADDRINFO
  2592. #endif
  2593. #if SANITIZER_INTERCEPT_GETNAMEINFO
  2594. INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
  2595. unsigned hostlen, char *serv, unsigned servlen, int flags) {
  2596. void *ctx;
  2597. COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
  2598. serv, servlen, flags);
  2599. // FIXME: consider adding READ_RANGE(sockaddr, salen)
  2600. // There is padding in in_addr that may make this too noisy
  2601. // FIXME: under ASan the call below may write to freed memory and corrupt
  2602. // its metadata. See
  2603. // https://github.com/google/sanitizers/issues/321.
  2604. int res =
  2605. REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
  2606. if (res == 0) {
  2607. if (host && hostlen)
  2608. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, internal_strlen(host) + 1);
  2609. if (serv && servlen)
  2610. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, internal_strlen(serv) + 1);
  2611. }
  2612. return res;
  2613. }
  2614. #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
  2615. #else
  2616. #define INIT_GETNAMEINFO
  2617. #endif
  2618. #if SANITIZER_INTERCEPT_GETSOCKNAME
  2619. INTERCEPTOR(int, getsockname, int sock_fd, void *addr, unsigned *addrlen) {
  2620. void *ctx;
  2621. COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
  2622. unsigned addr_sz;
  2623. if (addrlen) {
  2624. COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
  2625. addr_sz = *addrlen;
  2626. }
  2627. // FIXME: under ASan the call below may write to freed memory and corrupt
  2628. // its metadata. See
  2629. // https://github.com/google/sanitizers/issues/321.
  2630. int res = REAL(getsockname)(sock_fd, addr, addrlen);
  2631. if (!res && addr && addrlen) {
  2632. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
  2633. }
  2634. return res;
  2635. }
  2636. #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
  2637. #else
  2638. #define INIT_GETSOCKNAME
  2639. #endif
  2640. #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
  2641. static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
  2642. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
  2643. if (h->h_name)
  2644. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, internal_strlen(h->h_name) + 1);
  2645. char **p = h->h_aliases;
  2646. while (*p) {
  2647. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
  2648. ++p;
  2649. }
  2650. COMMON_INTERCEPTOR_WRITE_RANGE(
  2651. ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
  2652. p = h->h_addr_list;
  2653. while (*p) {
  2654. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
  2655. ++p;
  2656. }
  2657. COMMON_INTERCEPTOR_WRITE_RANGE(
  2658. ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
  2659. }
  2660. #endif
  2661. #if SANITIZER_INTERCEPT_GETHOSTBYNAME
  2662. INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
  2663. void *ctx;
  2664. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
  2665. struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
  2666. if (res) write_hostent(ctx, res);
  2667. return res;
  2668. }
  2669. INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
  2670. int type) {
  2671. void *ctx;
  2672. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
  2673. COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
  2674. struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
  2675. if (res) write_hostent(ctx, res);
  2676. return res;
  2677. }
  2678. INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
  2679. void *ctx;
  2680. COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
  2681. struct __sanitizer_hostent *res = REAL(gethostent)(fake);
  2682. if (res) write_hostent(ctx, res);
  2683. return res;
  2684. }
  2685. #define INIT_GETHOSTBYNAME \
  2686. COMMON_INTERCEPT_FUNCTION(gethostent); \
  2687. COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
  2688. COMMON_INTERCEPT_FUNCTION(gethostbyname);
  2689. #else
  2690. #define INIT_GETHOSTBYNAME
  2691. #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME
  2692. #if SANITIZER_INTERCEPT_GETHOSTBYNAME2
  2693. INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
  2694. void *ctx;
  2695. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
  2696. struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
  2697. if (res) write_hostent(ctx, res);
  2698. return res;
  2699. }
  2700. #define INIT_GETHOSTBYNAME2 COMMON_INTERCEPT_FUNCTION(gethostbyname2);
  2701. #else
  2702. #define INIT_GETHOSTBYNAME2
  2703. #endif // SANITIZER_INTERCEPT_GETHOSTBYNAME2
  2704. #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
  2705. INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
  2706. char *buf, SIZE_T buflen, __sanitizer_hostent **result,
  2707. int *h_errnop) {
  2708. void *ctx;
  2709. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
  2710. h_errnop);
  2711. // FIXME: under ASan the call below may write to freed memory and corrupt
  2712. // its metadata. See
  2713. // https://github.com/google/sanitizers/issues/321.
  2714. int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
  2715. if (result) {
  2716. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  2717. if (res == 0 && *result) write_hostent(ctx, *result);
  2718. }
  2719. if (h_errnop)
  2720. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
  2721. return res;
  2722. }
  2723. #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
  2724. #else
  2725. #define INIT_GETHOSTBYNAME_R
  2726. #endif
  2727. #if SANITIZER_INTERCEPT_GETHOSTENT_R
  2728. INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
  2729. SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
  2730. void *ctx;
  2731. COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
  2732. h_errnop);
  2733. // FIXME: under ASan the call below may write to freed memory and corrupt
  2734. // its metadata. See
  2735. // https://github.com/google/sanitizers/issues/321.
  2736. int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
  2737. if (result) {
  2738. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  2739. if (res == 0 && *result) write_hostent(ctx, *result);
  2740. }
  2741. if (h_errnop)
  2742. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
  2743. return res;
  2744. }
  2745. #define INIT_GETHOSTENT_R \
  2746. COMMON_INTERCEPT_FUNCTION(gethostent_r);
  2747. #else
  2748. #define INIT_GETHOSTENT_R
  2749. #endif
  2750. #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
  2751. INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
  2752. struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
  2753. __sanitizer_hostent **result, int *h_errnop) {
  2754. void *ctx;
  2755. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
  2756. buflen, result, h_errnop);
  2757. COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
  2758. // FIXME: under ASan the call below may write to freed memory and corrupt
  2759. // its metadata. See
  2760. // https://github.com/google/sanitizers/issues/321.
  2761. int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
  2762. h_errnop);
  2763. if (result) {
  2764. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  2765. if (res == 0 && *result) write_hostent(ctx, *result);
  2766. }
  2767. if (h_errnop)
  2768. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
  2769. return res;
  2770. }
  2771. #define INIT_GETHOSTBYADDR_R \
  2772. COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
  2773. #else
  2774. #define INIT_GETHOSTBYADDR_R
  2775. #endif
  2776. #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
  2777. INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
  2778. struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
  2779. __sanitizer_hostent **result, int *h_errnop) {
  2780. void *ctx;
  2781. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
  2782. result, h_errnop);
  2783. // FIXME: under ASan the call below may write to freed memory and corrupt
  2784. // its metadata. See
  2785. // https://github.com/google/sanitizers/issues/321.
  2786. int res =
  2787. REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
  2788. if (result) {
  2789. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  2790. if (res == 0 && *result) write_hostent(ctx, *result);
  2791. }
  2792. if (h_errnop)
  2793. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
  2794. return res;
  2795. }
  2796. #define INIT_GETHOSTBYNAME2_R \
  2797. COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
  2798. #else
  2799. #define INIT_GETHOSTBYNAME2_R
  2800. #endif
  2801. #if SANITIZER_INTERCEPT_GETSOCKOPT
  2802. INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
  2803. int *optlen) {
  2804. void *ctx;
  2805. COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
  2806. optlen);
  2807. if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
  2808. // FIXME: under ASan the call below may write to freed memory and corrupt
  2809. // its metadata. See
  2810. // https://github.com/google/sanitizers/issues/321.
  2811. int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
  2812. if (res == 0)
  2813. if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
  2814. return res;
  2815. }
  2816. #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
  2817. #else
  2818. #define INIT_GETSOCKOPT
  2819. #endif
  2820. #if SANITIZER_INTERCEPT_ACCEPT
  2821. INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
  2822. void *ctx;
  2823. COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
  2824. unsigned addrlen0 = 0;
  2825. if (addrlen) {
  2826. COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
  2827. addrlen0 = *addrlen;
  2828. }
  2829. int fd2 = REAL(accept)(fd, addr, addrlen);
  2830. if (fd2 >= 0) {
  2831. if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
  2832. if (addr && addrlen)
  2833. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
  2834. }
  2835. return fd2;
  2836. }
  2837. #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
  2838. #else
  2839. #define INIT_ACCEPT
  2840. #endif
  2841. #if SANITIZER_INTERCEPT_ACCEPT4
  2842. INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
  2843. void *ctx;
  2844. COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
  2845. unsigned addrlen0 = 0;
  2846. if (addrlen) {
  2847. COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
  2848. addrlen0 = *addrlen;
  2849. }
  2850. // FIXME: under ASan the call below may write to freed memory and corrupt
  2851. // its metadata. See
  2852. // https://github.com/google/sanitizers/issues/321.
  2853. int fd2 = REAL(accept4)(fd, addr, addrlen, f);
  2854. if (fd2 >= 0) {
  2855. if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
  2856. if (addr && addrlen)
  2857. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
  2858. }
  2859. return fd2;
  2860. }
  2861. #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
  2862. #else
  2863. #define INIT_ACCEPT4
  2864. #endif
  2865. #if SANITIZER_INTERCEPT_PACCEPT
  2866. INTERCEPTOR(int, paccept, int fd, void *addr, unsigned *addrlen,
  2867. __sanitizer_sigset_t *set, int f) {
  2868. void *ctx;
  2869. COMMON_INTERCEPTOR_ENTER(ctx, paccept, fd, addr, addrlen, set, f);
  2870. unsigned addrlen0 = 0;
  2871. if (addrlen) {
  2872. COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
  2873. addrlen0 = *addrlen;
  2874. }
  2875. if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
  2876. int fd2 = REAL(paccept)(fd, addr, addrlen, set, f);
  2877. if (fd2 >= 0) {
  2878. if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
  2879. if (addr && addrlen)
  2880. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
  2881. }
  2882. return fd2;
  2883. }
  2884. #define INIT_PACCEPT COMMON_INTERCEPT_FUNCTION(paccept);
  2885. #else
  2886. #define INIT_PACCEPT
  2887. #endif
  2888. #if SANITIZER_INTERCEPT_MODF
  2889. INTERCEPTOR(double, modf, double x, double *iptr) {
  2890. void *ctx;
  2891. COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
  2892. // FIXME: under ASan the call below may write to freed memory and corrupt
  2893. // its metadata. See
  2894. // https://github.com/google/sanitizers/issues/321.
  2895. double res = REAL(modf)(x, iptr);
  2896. if (iptr) {
  2897. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
  2898. }
  2899. return res;
  2900. }
  2901. INTERCEPTOR(float, modff, float x, float *iptr) {
  2902. void *ctx;
  2903. COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
  2904. // FIXME: under ASan the call below may write to freed memory and corrupt
  2905. // its metadata. See
  2906. // https://github.com/google/sanitizers/issues/321.
  2907. float res = REAL(modff)(x, iptr);
  2908. if (iptr) {
  2909. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
  2910. }
  2911. return res;
  2912. }
  2913. INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
  2914. void *ctx;
  2915. COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
  2916. // FIXME: under ASan the call below may write to freed memory and corrupt
  2917. // its metadata. See
  2918. // https://github.com/google/sanitizers/issues/321.
  2919. long double res = REAL(modfl)(x, iptr);
  2920. if (iptr) {
  2921. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
  2922. }
  2923. return res;
  2924. }
  2925. #define INIT_MODF \
  2926. COMMON_INTERCEPT_FUNCTION(modf); \
  2927. COMMON_INTERCEPT_FUNCTION(modff); \
  2928. COMMON_INTERCEPT_FUNCTION_LDBL(modfl);
  2929. #else
  2930. #define INIT_MODF
  2931. #endif
  2932. #if SANITIZER_INTERCEPT_RECVMSG || SANITIZER_INTERCEPT_RECVMMSG
  2933. static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
  2934. SSIZE_T maxlen) {
  2935. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
  2936. if (msg->msg_name && msg->msg_namelen)
  2937. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
  2938. if (msg->msg_iov && msg->msg_iovlen)
  2939. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
  2940. sizeof(*msg->msg_iov) * msg->msg_iovlen);
  2941. write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
  2942. if (msg->msg_control && msg->msg_controllen)
  2943. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
  2944. }
  2945. #endif
  2946. #if SANITIZER_INTERCEPT_RECVMSG
  2947. INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
  2948. int flags) {
  2949. void *ctx;
  2950. COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
  2951. // FIXME: under ASan the call below may write to freed memory and corrupt
  2952. // its metadata. See
  2953. // https://github.com/google/sanitizers/issues/321.
  2954. SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
  2955. if (res >= 0) {
  2956. if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  2957. if (msg) {
  2958. write_msghdr(ctx, msg, res);
  2959. COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
  2960. }
  2961. }
  2962. return res;
  2963. }
  2964. #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
  2965. #else
  2966. #define INIT_RECVMSG
  2967. #endif
  2968. #if SANITIZER_INTERCEPT_RECVMMSG
  2969. INTERCEPTOR(int, recvmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
  2970. unsigned int vlen, int flags, void *timeout) {
  2971. void *ctx;
  2972. COMMON_INTERCEPTOR_ENTER(ctx, recvmmsg, fd, msgvec, vlen, flags, timeout);
  2973. if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
  2974. int res = REAL(recvmmsg)(fd, msgvec, vlen, flags, timeout);
  2975. if (res >= 0) {
  2976. if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  2977. for (int i = 0; i < res; ++i) {
  2978. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
  2979. sizeof(msgvec[i].msg_len));
  2980. write_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
  2981. COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, &msgvec[i].msg_hdr);
  2982. }
  2983. }
  2984. return res;
  2985. }
  2986. #define INIT_RECVMMSG COMMON_INTERCEPT_FUNCTION(recvmmsg);
  2987. #else
  2988. #define INIT_RECVMMSG
  2989. #endif
  2990. #if SANITIZER_INTERCEPT_SENDMSG || SANITIZER_INTERCEPT_SENDMMSG
  2991. static void read_msghdr_control(void *ctx, void *control, uptr controllen) {
  2992. const unsigned kCmsgDataOffset =
  2993. RoundUpTo(sizeof(__sanitizer_cmsghdr), sizeof(uptr));
  2994. char *p = (char *)control;
  2995. char *const control_end = p + controllen;
  2996. while (true) {
  2997. if (p + sizeof(__sanitizer_cmsghdr) > control_end) break;
  2998. __sanitizer_cmsghdr *cmsg = (__sanitizer_cmsghdr *)p;
  2999. COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_len, sizeof(cmsg->cmsg_len));
  3000. if (p + RoundUpTo(cmsg->cmsg_len, sizeof(uptr)) > control_end) break;
  3001. COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_level,
  3002. sizeof(cmsg->cmsg_level));
  3003. COMMON_INTERCEPTOR_READ_RANGE(ctx, &cmsg->cmsg_type,
  3004. sizeof(cmsg->cmsg_type));
  3005. if (cmsg->cmsg_len > kCmsgDataOffset) {
  3006. char *data = p + kCmsgDataOffset;
  3007. unsigned data_len = cmsg->cmsg_len - kCmsgDataOffset;
  3008. if (data_len > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, data, data_len);
  3009. }
  3010. p += RoundUpTo(cmsg->cmsg_len, sizeof(uptr));
  3011. }
  3012. }
  3013. static void read_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
  3014. SSIZE_T maxlen) {
  3015. #define R(f) \
  3016. COMMON_INTERCEPTOR_READ_RANGE(ctx, &msg->msg_##f, sizeof(msg->msg_##f))
  3017. R(name);
  3018. R(namelen);
  3019. R(iov);
  3020. R(iovlen);
  3021. R(control);
  3022. R(controllen);
  3023. R(flags);
  3024. #undef R
  3025. if (msg->msg_name && msg->msg_namelen)
  3026. COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_name, msg->msg_namelen);
  3027. if (msg->msg_iov && msg->msg_iovlen)
  3028. COMMON_INTERCEPTOR_READ_RANGE(ctx, msg->msg_iov,
  3029. sizeof(*msg->msg_iov) * msg->msg_iovlen);
  3030. read_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
  3031. if (msg->msg_control && msg->msg_controllen)
  3032. read_msghdr_control(ctx, msg->msg_control, msg->msg_controllen);
  3033. }
  3034. #endif
  3035. #if SANITIZER_INTERCEPT_SENDMSG
  3036. INTERCEPTOR(SSIZE_T, sendmsg, int fd, struct __sanitizer_msghdr *msg,
  3037. int flags) {
  3038. void *ctx;
  3039. COMMON_INTERCEPTOR_ENTER(ctx, sendmsg, fd, msg, flags);
  3040. if (fd >= 0) {
  3041. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  3042. COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  3043. }
  3044. SSIZE_T res = REAL(sendmsg)(fd, msg, flags);
  3045. if (common_flags()->intercept_send && res >= 0 && msg)
  3046. read_msghdr(ctx, msg, res);
  3047. return res;
  3048. }
  3049. #define INIT_SENDMSG COMMON_INTERCEPT_FUNCTION(sendmsg);
  3050. #else
  3051. #define INIT_SENDMSG
  3052. #endif
  3053. #if SANITIZER_INTERCEPT_SENDMMSG
  3054. INTERCEPTOR(int, sendmmsg, int fd, struct __sanitizer_mmsghdr *msgvec,
  3055. unsigned vlen, int flags) {
  3056. void *ctx;
  3057. COMMON_INTERCEPTOR_ENTER(ctx, sendmmsg, fd, msgvec, vlen, flags);
  3058. if (fd >= 0) {
  3059. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  3060. COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  3061. }
  3062. int res = REAL(sendmmsg)(fd, msgvec, vlen, flags);
  3063. if (res >= 0 && msgvec) {
  3064. for (int i = 0; i < res; ++i) {
  3065. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &msgvec[i].msg_len,
  3066. sizeof(msgvec[i].msg_len));
  3067. if (common_flags()->intercept_send)
  3068. read_msghdr(ctx, &msgvec[i].msg_hdr, msgvec[i].msg_len);
  3069. }
  3070. }
  3071. return res;
  3072. }
  3073. #define INIT_SENDMMSG COMMON_INTERCEPT_FUNCTION(sendmmsg);
  3074. #else
  3075. #define INIT_SENDMMSG
  3076. #endif
  3077. #if SANITIZER_INTERCEPT_SYSMSG
  3078. INTERCEPTOR(int, msgsnd, int msqid, const void *msgp, SIZE_T msgsz,
  3079. int msgflg) {
  3080. void *ctx;
  3081. COMMON_INTERCEPTOR_ENTER(ctx, msgsnd, msqid, msgp, msgsz, msgflg);
  3082. if (msgp)
  3083. COMMON_INTERCEPTOR_READ_RANGE(ctx, msgp, sizeof(long) + msgsz);
  3084. int res = REAL(msgsnd)(msqid, msgp, msgsz, msgflg);
  3085. return res;
  3086. }
  3087. INTERCEPTOR(SSIZE_T, msgrcv, int msqid, void *msgp, SIZE_T msgsz,
  3088. long msgtyp, int msgflg) {
  3089. void *ctx;
  3090. COMMON_INTERCEPTOR_ENTER(ctx, msgrcv, msqid, msgp, msgsz, msgtyp, msgflg);
  3091. SSIZE_T len = REAL(msgrcv)(msqid, msgp, msgsz, msgtyp, msgflg);
  3092. if (len != -1)
  3093. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msgp, sizeof(long) + len);
  3094. return len;
  3095. }
  3096. #define INIT_SYSMSG \
  3097. COMMON_INTERCEPT_FUNCTION(msgsnd); \
  3098. COMMON_INTERCEPT_FUNCTION(msgrcv);
  3099. #else
  3100. #define INIT_SYSMSG
  3101. #endif
  3102. #if SANITIZER_INTERCEPT_GETPEERNAME
  3103. INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
  3104. void *ctx;
  3105. COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
  3106. unsigned addr_sz;
  3107. if (addrlen) {
  3108. COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
  3109. addr_sz = *addrlen;
  3110. }
  3111. // FIXME: under ASan the call below may write to freed memory and corrupt
  3112. // its metadata. See
  3113. // https://github.com/google/sanitizers/issues/321.
  3114. int res = REAL(getpeername)(sockfd, addr, addrlen);
  3115. if (!res && addr && addrlen) {
  3116. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
  3117. }
  3118. return res;
  3119. }
  3120. #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
  3121. #else
  3122. #define INIT_GETPEERNAME
  3123. #endif
  3124. #if SANITIZER_INTERCEPT_SYSINFO
  3125. INTERCEPTOR(int, sysinfo, void *info) {
  3126. void *ctx;
  3127. // FIXME: under ASan the call below may write to freed memory and corrupt
  3128. // its metadata. See
  3129. // https://github.com/google/sanitizers/issues/321.
  3130. COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
  3131. int res = REAL(sysinfo)(info);
  3132. if (!res && info)
  3133. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
  3134. return res;
  3135. }
  3136. #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
  3137. #else
  3138. #define INIT_SYSINFO
  3139. #endif
  3140. #if SANITIZER_INTERCEPT_READDIR
  3141. INTERCEPTOR(__sanitizer_dirent *, opendir, const char *path) {
  3142. void *ctx;
  3143. COMMON_INTERCEPTOR_ENTER(ctx, opendir, path);
  3144. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  3145. __sanitizer_dirent *res = REAL(opendir)(path);
  3146. if (res)
  3147. COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path);
  3148. return res;
  3149. }
  3150. INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
  3151. void *ctx;
  3152. COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
  3153. // FIXME: under ASan the call below may write to freed memory and corrupt
  3154. // its metadata. See
  3155. // https://github.com/google/sanitizers/issues/321.
  3156. __sanitizer_dirent *res = REAL(readdir)(dirp);
  3157. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
  3158. return res;
  3159. }
  3160. INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
  3161. __sanitizer_dirent **result) {
  3162. void *ctx;
  3163. COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
  3164. // FIXME: under ASan the call below may write to freed memory and corrupt
  3165. // its metadata. See
  3166. // https://github.com/google/sanitizers/issues/321.
  3167. int res = REAL(readdir_r)(dirp, entry, result);
  3168. if (!res) {
  3169. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  3170. if (*result)
  3171. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
  3172. }
  3173. return res;
  3174. }
  3175. #define INIT_READDIR \
  3176. COMMON_INTERCEPT_FUNCTION(opendir); \
  3177. COMMON_INTERCEPT_FUNCTION(readdir); \
  3178. COMMON_INTERCEPT_FUNCTION(readdir_r);
  3179. #else
  3180. #define INIT_READDIR
  3181. #endif
  3182. #if SANITIZER_INTERCEPT_READDIR64
  3183. INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
  3184. void *ctx;
  3185. COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
  3186. // FIXME: under ASan the call below may write to freed memory and corrupt
  3187. // its metadata. See
  3188. // https://github.com/google/sanitizers/issues/321.
  3189. __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
  3190. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
  3191. return res;
  3192. }
  3193. INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
  3194. __sanitizer_dirent64 **result) {
  3195. void *ctx;
  3196. COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
  3197. // FIXME: under ASan the call below may write to freed memory and corrupt
  3198. // its metadata. See
  3199. // https://github.com/google/sanitizers/issues/321.
  3200. int res = REAL(readdir64_r)(dirp, entry, result);
  3201. if (!res) {
  3202. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  3203. if (*result)
  3204. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
  3205. }
  3206. return res;
  3207. }
  3208. #define INIT_READDIR64 \
  3209. COMMON_INTERCEPT_FUNCTION(readdir64); \
  3210. COMMON_INTERCEPT_FUNCTION(readdir64_r);
  3211. #else
  3212. #define INIT_READDIR64
  3213. #endif
  3214. #if SANITIZER_INTERCEPT_PTRACE
  3215. INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
  3216. void *ctx;
  3217. COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
  3218. __sanitizer_iovec local_iovec;
  3219. if (data) {
  3220. if (request == ptrace_setregs) {
  3221. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
  3222. } else if (request == ptrace_setfpregs) {
  3223. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
  3224. } else if (request == ptrace_setfpxregs) {
  3225. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
  3226. } else if (request == ptrace_setvfpregs) {
  3227. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
  3228. } else if (request == ptrace_setsiginfo) {
  3229. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
  3230. // Some kernel might zero the iovec::iov_base in case of invalid
  3231. // write access. In this case copy the invalid address for further
  3232. // inspection.
  3233. } else if (request == ptrace_setregset || request == ptrace_getregset) {
  3234. __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
  3235. COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec));
  3236. local_iovec = *iovec;
  3237. if (request == ptrace_setregset)
  3238. COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec->iov_base, iovec->iov_len);
  3239. }
  3240. }
  3241. // FIXME: under ASan the call below may write to freed memory and corrupt
  3242. // its metadata. See
  3243. // https://github.com/google/sanitizers/issues/321.
  3244. uptr res = REAL(ptrace)(request, pid, addr, data);
  3245. if (!res && data) {
  3246. // Note that PEEK* requests assign different meaning to the return value.
  3247. // This function does not handle them (nor does it need to).
  3248. if (request == ptrace_getregs) {
  3249. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
  3250. } else if (request == ptrace_getfpregs) {
  3251. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
  3252. } else if (request == ptrace_getfpxregs) {
  3253. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
  3254. } else if (request == ptrace_getvfpregs) {
  3255. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_vfpregs_struct_sz);
  3256. } else if (request == ptrace_getsiginfo) {
  3257. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
  3258. } else if (request == ptrace_geteventmsg) {
  3259. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
  3260. } else if (request == ptrace_getregset) {
  3261. __sanitizer_iovec *iovec = (__sanitizer_iovec*)data;
  3262. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec, sizeof(*iovec));
  3263. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, local_iovec.iov_base,
  3264. local_iovec.iov_len);
  3265. }
  3266. }
  3267. return res;
  3268. }
  3269. #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
  3270. #else
  3271. #define INIT_PTRACE
  3272. #endif
  3273. #if SANITIZER_INTERCEPT_SETLOCALE
  3274. static void unpoison_ctype_arrays(void *ctx) {
  3275. #if SANITIZER_NETBSD
  3276. // These arrays contain 256 regular elements in unsigned char range + 1 EOF
  3277. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _ctype_tab_, 257 * sizeof(short));
  3278. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _toupper_tab_, 257 * sizeof(short));
  3279. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, _tolower_tab_, 257 * sizeof(short));
  3280. #endif
  3281. }
  3282. INTERCEPTOR(char *, setlocale, int category, char *locale) {
  3283. void *ctx;
  3284. COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
  3285. if (locale)
  3286. COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, internal_strlen(locale) + 1);
  3287. char *res = REAL(setlocale)(category, locale);
  3288. if (res) {
  3289. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  3290. unpoison_ctype_arrays(ctx);
  3291. }
  3292. return res;
  3293. }
  3294. #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
  3295. #else
  3296. #define INIT_SETLOCALE
  3297. #endif
  3298. #if SANITIZER_INTERCEPT_GETCWD
  3299. INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
  3300. void *ctx;
  3301. COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
  3302. // FIXME: under ASan the call below may write to freed memory and corrupt
  3303. // its metadata. See
  3304. // https://github.com/google/sanitizers/issues/321.
  3305. char *res = REAL(getcwd)(buf, size);
  3306. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  3307. return res;
  3308. }
  3309. #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
  3310. #else
  3311. #define INIT_GETCWD
  3312. #endif
  3313. #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
  3314. INTERCEPTOR(char *, get_current_dir_name, int fake) {
  3315. void *ctx;
  3316. COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
  3317. // FIXME: under ASan the call below may write to freed memory and corrupt
  3318. // its metadata. See
  3319. // https://github.com/google/sanitizers/issues/321.
  3320. char *res = REAL(get_current_dir_name)(fake);
  3321. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  3322. return res;
  3323. }
  3324. #define INIT_GET_CURRENT_DIR_NAME \
  3325. COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
  3326. #else
  3327. #define INIT_GET_CURRENT_DIR_NAME
  3328. #endif
  3329. UNUSED static inline void FixRealStrtolEndptr(const char *nptr, char **endptr) {
  3330. CHECK(endptr);
  3331. if (nptr == *endptr) {
  3332. // No digits were found at strtol call, we need to find out the last
  3333. // symbol accessed by strtoll on our own.
  3334. // We get this symbol by skipping leading blanks and optional +/- sign.
  3335. while (IsSpace(*nptr)) nptr++;
  3336. if (*nptr == '+' || *nptr == '-') nptr++;
  3337. *endptr = const_cast<char *>(nptr);
  3338. }
  3339. CHECK(*endptr >= nptr);
  3340. }
  3341. UNUSED static inline void StrtolFixAndCheck(void *ctx, const char *nptr,
  3342. char **endptr, char *real_endptr, int base) {
  3343. if (endptr) {
  3344. *endptr = real_endptr;
  3345. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
  3346. }
  3347. // If base has unsupported value, strtol can exit with EINVAL
  3348. // without reading any characters. So do additional checks only
  3349. // if base is valid.
  3350. bool is_valid_base = (base == 0) || (2 <= base && base <= 36);
  3351. if (is_valid_base) {
  3352. FixRealStrtolEndptr(nptr, &real_endptr);
  3353. }
  3354. COMMON_INTERCEPTOR_READ_STRING(ctx, nptr, is_valid_base ?
  3355. (real_endptr - nptr) + 1 : 0);
  3356. }
  3357. #if SANITIZER_INTERCEPT_STRTOIMAX
  3358. INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
  3359. void *ctx;
  3360. COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
  3361. // FIXME: under ASan the call below may write to freed memory and corrupt
  3362. // its metadata. See
  3363. // https://github.com/google/sanitizers/issues/321.
  3364. char *real_endptr;
  3365. INTMAX_T res = REAL(strtoimax)(nptr, &real_endptr, base);
  3366. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  3367. return res;
  3368. }
  3369. INTERCEPTOR(UINTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
  3370. void *ctx;
  3371. COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
  3372. // FIXME: under ASan the call below may write to freed memory and corrupt
  3373. // its metadata. See
  3374. // https://github.com/google/sanitizers/issues/321.
  3375. char *real_endptr;
  3376. UINTMAX_T res = REAL(strtoumax)(nptr, &real_endptr, base);
  3377. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  3378. return res;
  3379. }
  3380. #define INIT_STRTOIMAX \
  3381. COMMON_INTERCEPT_FUNCTION(strtoimax); \
  3382. COMMON_INTERCEPT_FUNCTION(strtoumax);
  3383. #else
  3384. #define INIT_STRTOIMAX
  3385. #endif
  3386. #if SANITIZER_INTERCEPT_MBSTOWCS
  3387. INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
  3388. void *ctx;
  3389. COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
  3390. // FIXME: under ASan the call below may write to freed memory and corrupt
  3391. // its metadata. See
  3392. // https://github.com/google/sanitizers/issues/321.
  3393. SIZE_T res = REAL(mbstowcs)(dest, src, len);
  3394. if (res != (SIZE_T) - 1 && dest) {
  3395. SIZE_T write_cnt = res + (res < len);
  3396. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
  3397. }
  3398. return res;
  3399. }
  3400. INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
  3401. void *ps) {
  3402. void *ctx;
  3403. COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
  3404. if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  3405. if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
  3406. // FIXME: under ASan the call below may write to freed memory and corrupt
  3407. // its metadata. See
  3408. // https://github.com/google/sanitizers/issues/321.
  3409. SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
  3410. if (res != (SIZE_T)(-1) && dest && src) {
  3411. // This function, and several others, may or may not write the terminating
  3412. // \0 character. They write it iff they clear *src.
  3413. SIZE_T write_cnt = res + !*src;
  3414. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
  3415. }
  3416. return res;
  3417. }
  3418. #define INIT_MBSTOWCS \
  3419. COMMON_INTERCEPT_FUNCTION(mbstowcs); \
  3420. COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
  3421. #else
  3422. #define INIT_MBSTOWCS
  3423. #endif
  3424. #if SANITIZER_INTERCEPT_MBSNRTOWCS
  3425. INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
  3426. SIZE_T len, void *ps) {
  3427. void *ctx;
  3428. COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
  3429. if (src) {
  3430. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  3431. if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
  3432. }
  3433. if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
  3434. // FIXME: under ASan the call below may write to freed memory and corrupt
  3435. // its metadata. See
  3436. // https://github.com/google/sanitizers/issues/321.
  3437. SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
  3438. if (res != (SIZE_T)(-1) && dest && src) {
  3439. SIZE_T write_cnt = res + !*src;
  3440. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
  3441. }
  3442. return res;
  3443. }
  3444. #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
  3445. #else
  3446. #define INIT_MBSNRTOWCS
  3447. #endif
  3448. #if SANITIZER_INTERCEPT_WCSTOMBS
  3449. INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
  3450. void *ctx;
  3451. COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
  3452. // FIXME: under ASan the call below may write to freed memory and corrupt
  3453. // its metadata. See
  3454. // https://github.com/google/sanitizers/issues/321.
  3455. SIZE_T res = REAL(wcstombs)(dest, src, len);
  3456. if (res != (SIZE_T) - 1 && dest) {
  3457. SIZE_T write_cnt = res + (res < len);
  3458. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
  3459. }
  3460. return res;
  3461. }
  3462. INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
  3463. void *ps) {
  3464. void *ctx;
  3465. COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
  3466. if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  3467. if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
  3468. // FIXME: under ASan the call below may write to freed memory and corrupt
  3469. // its metadata. See
  3470. // https://github.com/google/sanitizers/issues/321.
  3471. SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
  3472. if (res != (SIZE_T) - 1 && dest && src) {
  3473. SIZE_T write_cnt = res + !*src;
  3474. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
  3475. }
  3476. return res;
  3477. }
  3478. #define INIT_WCSTOMBS \
  3479. COMMON_INTERCEPT_FUNCTION(wcstombs); \
  3480. COMMON_INTERCEPT_FUNCTION(wcsrtombs);
  3481. #else
  3482. #define INIT_WCSTOMBS
  3483. #endif
  3484. #if SANITIZER_INTERCEPT_WCSNRTOMBS
  3485. INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
  3486. SIZE_T len, void *ps) {
  3487. void *ctx;
  3488. COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
  3489. if (src) {
  3490. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  3491. if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
  3492. }
  3493. if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
  3494. // FIXME: under ASan the call below may write to freed memory and corrupt
  3495. // its metadata. See
  3496. // https://github.com/google/sanitizers/issues/321.
  3497. SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
  3498. if (res != ((SIZE_T)-1) && dest && src) {
  3499. SIZE_T write_cnt = res + !*src;
  3500. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
  3501. }
  3502. return res;
  3503. }
  3504. #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
  3505. #else
  3506. #define INIT_WCSNRTOMBS
  3507. #endif
  3508. #if SANITIZER_INTERCEPT_WCRTOMB
  3509. INTERCEPTOR(SIZE_T, wcrtomb, char *dest, wchar_t src, void *ps) {
  3510. void *ctx;
  3511. COMMON_INTERCEPTOR_ENTER(ctx, wcrtomb, dest, src, ps);
  3512. if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
  3513. if (!dest)
  3514. return REAL(wcrtomb)(dest, src, ps);
  3515. char local_dest[32];
  3516. SIZE_T res = REAL(wcrtomb)(local_dest, src, ps);
  3517. if (res != ((SIZE_T)-1)) {
  3518. CHECK_LE(res, sizeof(local_dest));
  3519. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
  3520. REAL(memcpy)(dest, local_dest, res);
  3521. }
  3522. return res;
  3523. }
  3524. #define INIT_WCRTOMB COMMON_INTERCEPT_FUNCTION(wcrtomb);
  3525. #else
  3526. #define INIT_WCRTOMB
  3527. #endif
  3528. #if SANITIZER_INTERCEPT_WCTOMB
  3529. INTERCEPTOR(int, wctomb, char *dest, wchar_t src) {
  3530. void *ctx;
  3531. COMMON_INTERCEPTOR_ENTER(ctx, wctomb, dest, src);
  3532. if (!dest)
  3533. return REAL(wctomb)(dest, src);
  3534. char local_dest[32];
  3535. int res = REAL(wctomb)(local_dest, src);
  3536. if (res != -1) {
  3537. CHECK_LE(res, sizeof(local_dest));
  3538. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, res);
  3539. REAL(memcpy)(dest, local_dest, res);
  3540. }
  3541. return res;
  3542. }
  3543. #define INIT_WCTOMB COMMON_INTERCEPT_FUNCTION(wctomb);
  3544. #else
  3545. #define INIT_WCTOMB
  3546. #endif
  3547. #if SANITIZER_INTERCEPT_TCGETATTR
  3548. INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
  3549. void *ctx;
  3550. COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
  3551. // FIXME: under ASan the call below may write to freed memory and corrupt
  3552. // its metadata. See
  3553. // https://github.com/google/sanitizers/issues/321.
  3554. int res = REAL(tcgetattr)(fd, termios_p);
  3555. if (!res && termios_p)
  3556. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
  3557. return res;
  3558. }
  3559. #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
  3560. #else
  3561. #define INIT_TCGETATTR
  3562. #endif
  3563. #if SANITIZER_INTERCEPT_REALPATH
  3564. INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
  3565. void *ctx;
  3566. COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
  3567. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  3568. // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
  3569. // version of a versioned symbol. For realpath(), this gives us something
  3570. // (called __old_realpath) that does not handle NULL in the second argument.
  3571. // Handle it as part of the interceptor.
  3572. char *allocated_path = nullptr;
  3573. if (!resolved_path)
  3574. allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
  3575. char *res = REAL(realpath)(path, resolved_path);
  3576. if (allocated_path && !res)
  3577. WRAP(free)(allocated_path);
  3578. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  3579. return res;
  3580. }
  3581. # define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
  3582. #else
  3583. #define INIT_REALPATH
  3584. #endif
  3585. #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
  3586. INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
  3587. void *ctx;
  3588. COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
  3589. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  3590. char *res = REAL(canonicalize_file_name)(path);
  3591. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  3592. return res;
  3593. }
  3594. #define INIT_CANONICALIZE_FILE_NAME \
  3595. COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
  3596. #else
  3597. #define INIT_CANONICALIZE_FILE_NAME
  3598. #endif
  3599. #if SANITIZER_INTERCEPT_CONFSTR
  3600. INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
  3601. void *ctx;
  3602. COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
  3603. // FIXME: under ASan the call below may write to freed memory and corrupt
  3604. // its metadata. See
  3605. // https://github.com/google/sanitizers/issues/321.
  3606. SIZE_T res = REAL(confstr)(name, buf, len);
  3607. if (buf && res)
  3608. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
  3609. return res;
  3610. }
  3611. #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
  3612. #else
  3613. #define INIT_CONFSTR
  3614. #endif
  3615. #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
  3616. INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
  3617. void *ctx;
  3618. COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
  3619. // FIXME: under ASan the call below may write to freed memory and corrupt
  3620. // its metadata. See
  3621. // https://github.com/google/sanitizers/issues/321.
  3622. int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
  3623. if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
  3624. return res;
  3625. }
  3626. #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
  3627. #else
  3628. #define INIT_SCHED_GETAFFINITY
  3629. #endif
  3630. #if SANITIZER_INTERCEPT_SCHED_GETPARAM
  3631. INTERCEPTOR(int, sched_getparam, int pid, void *param) {
  3632. void *ctx;
  3633. COMMON_INTERCEPTOR_ENTER(ctx, sched_getparam, pid, param);
  3634. int res = REAL(sched_getparam)(pid, param);
  3635. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, struct_sched_param_sz);
  3636. return res;
  3637. }
  3638. #define INIT_SCHED_GETPARAM COMMON_INTERCEPT_FUNCTION(sched_getparam);
  3639. #else
  3640. #define INIT_SCHED_GETPARAM
  3641. #endif
  3642. #if SANITIZER_INTERCEPT_STRERROR
  3643. INTERCEPTOR(char *, strerror, int errnum) {
  3644. void *ctx;
  3645. COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
  3646. COMMON_INTERCEPTOR_STRERROR();
  3647. char *res = REAL(strerror)(errnum);
  3648. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  3649. return res;
  3650. }
  3651. #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
  3652. #else
  3653. #define INIT_STRERROR
  3654. #endif
  3655. #if SANITIZER_INTERCEPT_STRERROR_R
  3656. // There are 2 versions of strerror_r:
  3657. // * POSIX version returns 0 on success, negative error code on failure,
  3658. // writes message to buf.
  3659. // * GNU version returns message pointer, which points to either buf or some
  3660. // static storage.
  3661. #if ((_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE) || \
  3662. SANITIZER_APPLE || SANITIZER_ANDROID || SANITIZER_NETBSD || \
  3663. SANITIZER_FREEBSD
  3664. // POSIX version. Spec is not clear on whether buf is NULL-terminated.
  3665. // At least on OSX, buf contents are valid even when the call fails.
  3666. INTERCEPTOR(int, strerror_r, int errnum, char *buf, SIZE_T buflen) {
  3667. void *ctx;
  3668. COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
  3669. // FIXME: under ASan the call below may write to freed memory and corrupt
  3670. // its metadata. See
  3671. // https://github.com/google/sanitizers/issues/321.
  3672. int res = REAL(strerror_r)(errnum, buf, buflen);
  3673. SIZE_T sz = internal_strnlen(buf, buflen);
  3674. if (sz < buflen) ++sz;
  3675. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
  3676. return res;
  3677. }
  3678. #else
  3679. // GNU version.
  3680. INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
  3681. void *ctx;
  3682. COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
  3683. // FIXME: under ASan the call below may write to freed memory and corrupt
  3684. // its metadata. See
  3685. // https://github.com/google/sanitizers/issues/321.
  3686. char *res = REAL(strerror_r)(errnum, buf, buflen);
  3687. if (res == buf)
  3688. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  3689. else
  3690. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  3691. return res;
  3692. }
  3693. #endif //(_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !_GNU_SOURCE ||
  3694. //SANITIZER_APPLE
  3695. #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
  3696. #else
  3697. #define INIT_STRERROR_R
  3698. #endif
  3699. #if SANITIZER_INTERCEPT_XPG_STRERROR_R
  3700. INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
  3701. void *ctx;
  3702. COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
  3703. // FIXME: under ASan the call below may write to freed memory and corrupt
  3704. // its metadata. See
  3705. // https://github.com/google/sanitizers/issues/321.
  3706. int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
  3707. // This version always returns a null-terminated string.
  3708. if (buf && buflen)
  3709. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
  3710. return res;
  3711. }
  3712. #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
  3713. #else
  3714. #define INIT_XPG_STRERROR_R
  3715. #endif
  3716. #if SANITIZER_INTERCEPT_SCANDIR
  3717. typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
  3718. typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
  3719. const struct __sanitizer_dirent **);
  3720. static THREADLOCAL scandir_filter_f scandir_filter;
  3721. static THREADLOCAL scandir_compar_f scandir_compar;
  3722. static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
  3723. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  3724. COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
  3725. return scandir_filter(dir);
  3726. }
  3727. static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
  3728. const struct __sanitizer_dirent **b) {
  3729. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  3730. COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
  3731. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
  3732. COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
  3733. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
  3734. return scandir_compar(a, b);
  3735. }
  3736. INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
  3737. scandir_filter_f filter, scandir_compar_f compar) {
  3738. void *ctx;
  3739. COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
  3740. if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
  3741. scandir_filter = filter;
  3742. scandir_compar = compar;
  3743. // FIXME: under ASan the call below may write to freed memory and corrupt
  3744. // its metadata. See
  3745. // https://github.com/google/sanitizers/issues/321.
  3746. int res = REAL(scandir)(dirp, namelist,
  3747. filter ? wrapped_scandir_filter : nullptr,
  3748. compar ? wrapped_scandir_compar : nullptr);
  3749. scandir_filter = nullptr;
  3750. scandir_compar = nullptr;
  3751. if (namelist && res > 0) {
  3752. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
  3753. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
  3754. for (int i = 0; i < res; ++i)
  3755. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
  3756. (*namelist)[i]->d_reclen);
  3757. }
  3758. return res;
  3759. }
  3760. #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
  3761. #else
  3762. #define INIT_SCANDIR
  3763. #endif
  3764. #if SANITIZER_INTERCEPT_SCANDIR64
  3765. typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
  3766. typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
  3767. const struct __sanitizer_dirent64 **);
  3768. static THREADLOCAL scandir64_filter_f scandir64_filter;
  3769. static THREADLOCAL scandir64_compar_f scandir64_compar;
  3770. static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
  3771. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  3772. COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
  3773. return scandir64_filter(dir);
  3774. }
  3775. static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
  3776. const struct __sanitizer_dirent64 **b) {
  3777. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  3778. COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
  3779. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
  3780. COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
  3781. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
  3782. return scandir64_compar(a, b);
  3783. }
  3784. INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
  3785. scandir64_filter_f filter, scandir64_compar_f compar) {
  3786. void *ctx;
  3787. COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
  3788. if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, internal_strlen(dirp) + 1);
  3789. scandir64_filter = filter;
  3790. scandir64_compar = compar;
  3791. // FIXME: under ASan the call below may write to freed memory and corrupt
  3792. // its metadata. See
  3793. // https://github.com/google/sanitizers/issues/321.
  3794. int res =
  3795. REAL(scandir64)(dirp, namelist,
  3796. filter ? wrapped_scandir64_filter : nullptr,
  3797. compar ? wrapped_scandir64_compar : nullptr);
  3798. scandir64_filter = nullptr;
  3799. scandir64_compar = nullptr;
  3800. if (namelist && res > 0) {
  3801. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
  3802. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
  3803. for (int i = 0; i < res; ++i)
  3804. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
  3805. (*namelist)[i]->d_reclen);
  3806. }
  3807. return res;
  3808. }
  3809. #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
  3810. #else
  3811. #define INIT_SCANDIR64
  3812. #endif
  3813. #if SANITIZER_INTERCEPT_GETGROUPS
  3814. INTERCEPTOR(int, getgroups, int size, u32 *lst) {
  3815. void *ctx;
  3816. COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
  3817. // FIXME: under ASan the call below may write to freed memory and corrupt
  3818. // its metadata. See
  3819. // https://github.com/google/sanitizers/issues/321.
  3820. int res = REAL(getgroups)(size, lst);
  3821. if (res >= 0 && lst && size > 0)
  3822. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
  3823. return res;
  3824. }
  3825. #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
  3826. #else
  3827. #define INIT_GETGROUPS
  3828. #endif
  3829. #if SANITIZER_INTERCEPT_POLL
  3830. static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
  3831. __sanitizer_nfds_t nfds) {
  3832. for (unsigned i = 0; i < nfds; ++i) {
  3833. COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
  3834. COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
  3835. }
  3836. }
  3837. static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
  3838. __sanitizer_nfds_t nfds) {
  3839. for (unsigned i = 0; i < nfds; ++i)
  3840. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
  3841. sizeof(fds[i].revents));
  3842. }
  3843. INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
  3844. int timeout) {
  3845. void *ctx;
  3846. COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
  3847. if (fds && nfds) read_pollfd(ctx, fds, nfds);
  3848. int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
  3849. if (fds && nfds) write_pollfd(ctx, fds, nfds);
  3850. return res;
  3851. }
  3852. #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
  3853. #else
  3854. #define INIT_POLL
  3855. #endif
  3856. #if SANITIZER_INTERCEPT_PPOLL
  3857. INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
  3858. void *timeout_ts, __sanitizer_sigset_t *sigmask) {
  3859. void *ctx;
  3860. COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
  3861. if (fds && nfds) read_pollfd(ctx, fds, nfds);
  3862. if (timeout_ts)
  3863. COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
  3864. if (sigmask) COMMON_INTERCEPTOR_READ_RANGE(ctx, sigmask, sizeof(*sigmask));
  3865. int res =
  3866. COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
  3867. if (fds && nfds) write_pollfd(ctx, fds, nfds);
  3868. return res;
  3869. }
  3870. #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
  3871. #else
  3872. #define INIT_PPOLL
  3873. #endif
  3874. #if SANITIZER_INTERCEPT_WORDEXP
  3875. INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
  3876. void *ctx;
  3877. COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
  3878. if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, internal_strlen(s) + 1);
  3879. // FIXME: under ASan the call below may write to freed memory and corrupt
  3880. // its metadata. See
  3881. // https://github.com/google/sanitizers/issues/321.
  3882. int res = REAL(wordexp)(s, p, flags);
  3883. if (!res && p) {
  3884. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
  3885. uptr we_wordc =
  3886. ((flags & wordexp_wrde_dooffs) ? p->we_offs : 0) + p->we_wordc;
  3887. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
  3888. sizeof(*p->we_wordv) * (we_wordc + 1));
  3889. for (uptr i = 0; i < we_wordc; ++i) {
  3890. char *w = p->we_wordv[i];
  3891. if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, internal_strlen(w) + 1);
  3892. }
  3893. }
  3894. return res;
  3895. }
  3896. #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
  3897. #else
  3898. #define INIT_WORDEXP
  3899. #endif
  3900. #if SANITIZER_INTERCEPT_SIGWAIT
  3901. INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
  3902. void *ctx;
  3903. COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
  3904. if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
  3905. // FIXME: under ASan the call below may write to freed memory and corrupt
  3906. // its metadata. See
  3907. // https://github.com/google/sanitizers/issues/321.
  3908. int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwait)(set, sig);
  3909. if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
  3910. return res;
  3911. }
  3912. #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
  3913. #else
  3914. #define INIT_SIGWAIT
  3915. #endif
  3916. #if SANITIZER_INTERCEPT_SIGWAITINFO
  3917. INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
  3918. void *ctx;
  3919. COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
  3920. if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
  3921. // FIXME: under ASan the call below may write to freed memory and corrupt
  3922. // its metadata. See
  3923. // https://github.com/google/sanitizers/issues/321.
  3924. int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigwaitinfo)(set, info);
  3925. if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
  3926. return res;
  3927. }
  3928. #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
  3929. #else
  3930. #define INIT_SIGWAITINFO
  3931. #endif
  3932. #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
  3933. INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
  3934. void *timeout) {
  3935. void *ctx;
  3936. COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
  3937. if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
  3938. if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
  3939. // FIXME: under ASan the call below may write to freed memory and corrupt
  3940. // its metadata. See
  3941. // https://github.com/google/sanitizers/issues/321.
  3942. int res = COMMON_INTERCEPTOR_BLOCK_REAL(sigtimedwait)(set, info, timeout);
  3943. if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
  3944. return res;
  3945. }
  3946. #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
  3947. #else
  3948. #define INIT_SIGTIMEDWAIT
  3949. #endif
  3950. #if SANITIZER_INTERCEPT_SIGSETOPS
  3951. INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
  3952. void *ctx;
  3953. COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
  3954. // FIXME: under ASan the call below may write to freed memory and corrupt
  3955. // its metadata. See
  3956. // https://github.com/google/sanitizers/issues/321.
  3957. int res = REAL(sigemptyset)(set);
  3958. if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
  3959. return res;
  3960. }
  3961. INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
  3962. void *ctx;
  3963. COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
  3964. // FIXME: under ASan the call below may write to freed memory and corrupt
  3965. // its metadata. See
  3966. // https://github.com/google/sanitizers/issues/321.
  3967. int res = REAL(sigfillset)(set);
  3968. if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
  3969. return res;
  3970. }
  3971. #define INIT_SIGSETOPS \
  3972. COMMON_INTERCEPT_FUNCTION(sigemptyset); \
  3973. COMMON_INTERCEPT_FUNCTION(sigfillset);
  3974. #else
  3975. #define INIT_SIGSETOPS
  3976. #endif
  3977. #if SANITIZER_INTERCEPT_SIGSET_LOGICOPS
  3978. INTERCEPTOR(int, sigandset, __sanitizer_sigset_t *dst,
  3979. __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
  3980. void *ctx;
  3981. COMMON_INTERCEPTOR_ENTER(ctx, sigandset, dst, src1, src2);
  3982. if (src1)
  3983. COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
  3984. if (src2)
  3985. COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
  3986. int res = REAL(sigandset)(dst, src1, src2);
  3987. if (!res && dst)
  3988. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
  3989. return res;
  3990. }
  3991. INTERCEPTOR(int, sigorset, __sanitizer_sigset_t *dst,
  3992. __sanitizer_sigset_t *src1, __sanitizer_sigset_t *src2) {
  3993. void *ctx;
  3994. COMMON_INTERCEPTOR_ENTER(ctx, sigorset, dst, src1, src2);
  3995. if (src1)
  3996. COMMON_INTERCEPTOR_READ_RANGE(ctx, src1, sizeof(*src1));
  3997. if (src2)
  3998. COMMON_INTERCEPTOR_READ_RANGE(ctx, src2, sizeof(*src2));
  3999. int res = REAL(sigorset)(dst, src1, src2);
  4000. if (!res && dst)
  4001. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
  4002. return res;
  4003. }
  4004. #define INIT_SIGSET_LOGICOPS \
  4005. COMMON_INTERCEPT_FUNCTION(sigandset); \
  4006. COMMON_INTERCEPT_FUNCTION(sigorset);
  4007. #else
  4008. #define INIT_SIGSET_LOGICOPS
  4009. #endif
  4010. #if SANITIZER_INTERCEPT_SIGPENDING
  4011. INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
  4012. void *ctx;
  4013. COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
  4014. // FIXME: under ASan the call below may write to freed memory and corrupt
  4015. // its metadata. See
  4016. // https://github.com/google/sanitizers/issues/321.
  4017. int res = REAL(sigpending)(set);
  4018. if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
  4019. return res;
  4020. }
  4021. #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
  4022. #else
  4023. #define INIT_SIGPENDING
  4024. #endif
  4025. #if SANITIZER_INTERCEPT_SIGPROCMASK
  4026. INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
  4027. __sanitizer_sigset_t *oldset) {
  4028. void *ctx;
  4029. COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
  4030. if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
  4031. // FIXME: under ASan the call below may write to freed memory and corrupt
  4032. // its metadata. See
  4033. // https://github.com/google/sanitizers/issues/321.
  4034. int res = REAL(sigprocmask)(how, set, oldset);
  4035. if (!res && oldset)
  4036. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
  4037. return res;
  4038. }
  4039. #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
  4040. #else
  4041. #define INIT_SIGPROCMASK
  4042. #endif
  4043. #if SANITIZER_INTERCEPT_PTHREAD_SIGMASK
  4044. INTERCEPTOR(int, pthread_sigmask, int how, __sanitizer_sigset_t *set,
  4045. __sanitizer_sigset_t *oldset) {
  4046. void *ctx;
  4047. COMMON_INTERCEPTOR_ENTER(ctx, pthread_sigmask, how, set, oldset);
  4048. if (set) COMMON_INTERCEPTOR_READ_RANGE(ctx, set, sizeof(*set));
  4049. // FIXME: under ASan the call below may write to freed memory and corrupt
  4050. // its metadata. See
  4051. // https://github.com/google/sanitizers/issues/321.
  4052. int res = REAL(pthread_sigmask)(how, set, oldset);
  4053. if (!res && oldset)
  4054. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
  4055. return res;
  4056. }
  4057. #define INIT_PTHREAD_SIGMASK COMMON_INTERCEPT_FUNCTION(pthread_sigmask);
  4058. #else
  4059. #define INIT_PTHREAD_SIGMASK
  4060. #endif
  4061. #if SANITIZER_INTERCEPT_BACKTRACE
  4062. INTERCEPTOR(int, backtrace, void **buffer, int size) {
  4063. void *ctx;
  4064. COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
  4065. // FIXME: under ASan the call below may write to freed memory and corrupt
  4066. // its metadata. See
  4067. // https://github.com/google/sanitizers/issues/321.
  4068. int res = REAL(backtrace)(buffer, size);
  4069. if (res && buffer)
  4070. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
  4071. return res;
  4072. }
  4073. INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
  4074. void *ctx;
  4075. COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
  4076. if (buffer && size)
  4077. COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
  4078. // FIXME: under ASan the call below may write to freed memory and corrupt
  4079. // its metadata. See
  4080. // https://github.com/google/sanitizers/issues/321.
  4081. char **res = REAL(backtrace_symbols)(buffer, size);
  4082. if (res && size) {
  4083. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
  4084. for (int i = 0; i < size; ++i)
  4085. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], internal_strlen(res[i]) + 1);
  4086. }
  4087. return res;
  4088. }
  4089. #define INIT_BACKTRACE \
  4090. COMMON_INTERCEPT_FUNCTION(backtrace); \
  4091. COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
  4092. #else
  4093. #define INIT_BACKTRACE
  4094. #endif
  4095. #if SANITIZER_INTERCEPT__EXIT
  4096. INTERCEPTOR(void, _exit, int status) {
  4097. void *ctx;
  4098. COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
  4099. COMMON_INTERCEPTOR_USER_CALLBACK_START();
  4100. int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
  4101. COMMON_INTERCEPTOR_USER_CALLBACK_END();
  4102. if (status == 0) status = status1;
  4103. REAL(_exit)(status);
  4104. }
  4105. #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
  4106. #else
  4107. #define INIT__EXIT
  4108. #endif
  4109. #if SANITIZER_INTERCEPT___LIBC_MUTEX
  4110. INTERCEPTOR(int, __libc_thr_setcancelstate, int state, int *oldstate)
  4111. ALIAS(WRAPPER_NAME(pthread_setcancelstate));
  4112. #define INIT___LIBC_THR_SETCANCELSTATE \
  4113. COMMON_INTERCEPT_FUNCTION(__libc_thr_setcancelstate)
  4114. #else
  4115. #define INIT___LIBC_THR_SETCANCELSTATE
  4116. #endif
  4117. #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
  4118. static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
  4119. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
  4120. if (mnt->mnt_fsname)
  4121. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
  4122. internal_strlen(mnt->mnt_fsname) + 1);
  4123. if (mnt->mnt_dir)
  4124. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
  4125. internal_strlen(mnt->mnt_dir) + 1);
  4126. if (mnt->mnt_type)
  4127. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
  4128. internal_strlen(mnt->mnt_type) + 1);
  4129. if (mnt->mnt_opts)
  4130. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
  4131. internal_strlen(mnt->mnt_opts) + 1);
  4132. }
  4133. #endif
  4134. #if SANITIZER_INTERCEPT_GETMNTENT
  4135. INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
  4136. void *ctx;
  4137. COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
  4138. __sanitizer_mntent *res = REAL(getmntent)(fp);
  4139. if (res) write_mntent(ctx, res);
  4140. return res;
  4141. }
  4142. #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
  4143. #else
  4144. #define INIT_GETMNTENT
  4145. #endif
  4146. #if SANITIZER_INTERCEPT_GETMNTENT_R
  4147. INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
  4148. __sanitizer_mntent *mntbuf, char *buf, int buflen) {
  4149. void *ctx;
  4150. COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
  4151. __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
  4152. if (res) write_mntent(ctx, res);
  4153. return res;
  4154. }
  4155. #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
  4156. #else
  4157. #define INIT_GETMNTENT_R
  4158. #endif
  4159. #if SANITIZER_INTERCEPT_STATFS
  4160. INTERCEPTOR(int, statfs, char *path, void *buf) {
  4161. void *ctx;
  4162. COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
  4163. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  4164. // FIXME: under ASan the call below may write to freed memory and corrupt
  4165. // its metadata. See
  4166. // https://github.com/google/sanitizers/issues/321.
  4167. int res = REAL(statfs)(path, buf);
  4168. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
  4169. return res;
  4170. }
  4171. INTERCEPTOR(int, fstatfs, int fd, void *buf) {
  4172. void *ctx;
  4173. COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
  4174. // FIXME: under ASan the call below may write to freed memory and corrupt
  4175. // its metadata. See
  4176. // https://github.com/google/sanitizers/issues/321.
  4177. int res = REAL(fstatfs)(fd, buf);
  4178. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
  4179. return res;
  4180. }
  4181. #define INIT_STATFS \
  4182. COMMON_INTERCEPT_FUNCTION(statfs); \
  4183. COMMON_INTERCEPT_FUNCTION(fstatfs);
  4184. #else
  4185. #define INIT_STATFS
  4186. #endif
  4187. #if SANITIZER_INTERCEPT_STATFS64
  4188. INTERCEPTOR(int, statfs64, char *path, void *buf) {
  4189. void *ctx;
  4190. COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
  4191. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  4192. // FIXME: under ASan the call below may write to freed memory and corrupt
  4193. // its metadata. See
  4194. // https://github.com/google/sanitizers/issues/321.
  4195. int res = REAL(statfs64)(path, buf);
  4196. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
  4197. return res;
  4198. }
  4199. INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
  4200. void *ctx;
  4201. COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
  4202. // FIXME: under ASan the call below may write to freed memory and corrupt
  4203. // its metadata. See
  4204. // https://github.com/google/sanitizers/issues/321.
  4205. int res = REAL(fstatfs64)(fd, buf);
  4206. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
  4207. return res;
  4208. }
  4209. #define INIT_STATFS64 \
  4210. COMMON_INTERCEPT_FUNCTION(statfs64); \
  4211. COMMON_INTERCEPT_FUNCTION(fstatfs64);
  4212. #else
  4213. #define INIT_STATFS64
  4214. #endif
  4215. #if SANITIZER_INTERCEPT_STATVFS
  4216. INTERCEPTOR(int, statvfs, char *path, void *buf) {
  4217. void *ctx;
  4218. COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
  4219. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  4220. // FIXME: under ASan the call below may write to freed memory and corrupt
  4221. // its metadata. See
  4222. // https://github.com/google/sanitizers/issues/321.
  4223. int res = REAL(statvfs)(path, buf);
  4224. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
  4225. return res;
  4226. }
  4227. INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
  4228. void *ctx;
  4229. COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
  4230. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  4231. // FIXME: under ASan the call below may write to freed memory and corrupt
  4232. // its metadata. See
  4233. // https://github.com/google/sanitizers/issues/321.
  4234. int res = REAL(fstatvfs)(fd, buf);
  4235. if (!res) {
  4236. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
  4237. if (fd >= 0)
  4238. COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  4239. }
  4240. return res;
  4241. }
  4242. #define INIT_STATVFS \
  4243. COMMON_INTERCEPT_FUNCTION(statvfs); \
  4244. COMMON_INTERCEPT_FUNCTION(fstatvfs);
  4245. #else
  4246. #define INIT_STATVFS
  4247. #endif
  4248. #if SANITIZER_INTERCEPT_STATVFS64
  4249. INTERCEPTOR(int, statvfs64, char *path, void *buf) {
  4250. void *ctx;
  4251. COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
  4252. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  4253. // FIXME: under ASan the call below may write to freed memory and corrupt
  4254. // its metadata. See
  4255. // https://github.com/google/sanitizers/issues/321.
  4256. int res = REAL(statvfs64)(path, buf);
  4257. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
  4258. return res;
  4259. }
  4260. INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
  4261. void *ctx;
  4262. COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
  4263. // FIXME: under ASan the call below may write to freed memory and corrupt
  4264. // its metadata. See
  4265. // https://github.com/google/sanitizers/issues/321.
  4266. int res = REAL(fstatvfs64)(fd, buf);
  4267. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
  4268. return res;
  4269. }
  4270. #define INIT_STATVFS64 \
  4271. COMMON_INTERCEPT_FUNCTION(statvfs64); \
  4272. COMMON_INTERCEPT_FUNCTION(fstatvfs64);
  4273. #else
  4274. #define INIT_STATVFS64
  4275. #endif
  4276. #if SANITIZER_INTERCEPT_INITGROUPS
  4277. INTERCEPTOR(int, initgroups, char *user, u32 group) {
  4278. void *ctx;
  4279. COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
  4280. if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, internal_strlen(user) + 1);
  4281. int res = REAL(initgroups)(user, group);
  4282. return res;
  4283. }
  4284. #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
  4285. #else
  4286. #define INIT_INITGROUPS
  4287. #endif
  4288. #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
  4289. INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
  4290. void *ctx;
  4291. COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
  4292. if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
  4293. char *res = REAL(ether_ntoa)(addr);
  4294. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  4295. return res;
  4296. }
  4297. INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
  4298. void *ctx;
  4299. COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
  4300. if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
  4301. __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
  4302. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
  4303. return res;
  4304. }
  4305. #define INIT_ETHER_NTOA_ATON \
  4306. COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
  4307. COMMON_INTERCEPT_FUNCTION(ether_aton);
  4308. #else
  4309. #define INIT_ETHER_NTOA_ATON
  4310. #endif
  4311. #if SANITIZER_INTERCEPT_ETHER_HOST
  4312. INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
  4313. void *ctx;
  4314. COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
  4315. if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
  4316. // FIXME: under ASan the call below may write to freed memory and corrupt
  4317. // its metadata. See
  4318. // https://github.com/google/sanitizers/issues/321.
  4319. int res = REAL(ether_ntohost)(hostname, addr);
  4320. if (!res && hostname)
  4321. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
  4322. return res;
  4323. }
  4324. INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
  4325. void *ctx;
  4326. COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
  4327. if (hostname)
  4328. COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
  4329. // FIXME: under ASan the call below may write to freed memory and corrupt
  4330. // its metadata. See
  4331. // https://github.com/google/sanitizers/issues/321.
  4332. int res = REAL(ether_hostton)(hostname, addr);
  4333. if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
  4334. return res;
  4335. }
  4336. INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
  4337. char *hostname) {
  4338. void *ctx;
  4339. COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
  4340. if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, internal_strlen(line) + 1);
  4341. // FIXME: under ASan the call below may write to freed memory and corrupt
  4342. // its metadata. See
  4343. // https://github.com/google/sanitizers/issues/321.
  4344. int res = REAL(ether_line)(line, addr, hostname);
  4345. if (!res) {
  4346. if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
  4347. if (hostname)
  4348. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, internal_strlen(hostname) + 1);
  4349. }
  4350. return res;
  4351. }
  4352. #define INIT_ETHER_HOST \
  4353. COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
  4354. COMMON_INTERCEPT_FUNCTION(ether_hostton); \
  4355. COMMON_INTERCEPT_FUNCTION(ether_line);
  4356. #else
  4357. #define INIT_ETHER_HOST
  4358. #endif
  4359. #if SANITIZER_INTERCEPT_ETHER_R
  4360. INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
  4361. void *ctx;
  4362. COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
  4363. if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
  4364. // FIXME: under ASan the call below may write to freed memory and corrupt
  4365. // its metadata. See
  4366. // https://github.com/google/sanitizers/issues/321.
  4367. char *res = REAL(ether_ntoa_r)(addr, buf);
  4368. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  4369. return res;
  4370. }
  4371. INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
  4372. __sanitizer_ether_addr *addr) {
  4373. void *ctx;
  4374. COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
  4375. if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, internal_strlen(buf) + 1);
  4376. // FIXME: under ASan the call below may write to freed memory and corrupt
  4377. // its metadata. See
  4378. // https://github.com/google/sanitizers/issues/321.
  4379. __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
  4380. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
  4381. return res;
  4382. }
  4383. #define INIT_ETHER_R \
  4384. COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
  4385. COMMON_INTERCEPT_FUNCTION(ether_aton_r);
  4386. #else
  4387. #define INIT_ETHER_R
  4388. #endif
  4389. #if SANITIZER_INTERCEPT_SHMCTL
  4390. INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
  4391. void *ctx;
  4392. COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
  4393. // FIXME: under ASan the call below may write to freed memory and corrupt
  4394. // its metadata. See
  4395. // https://github.com/google/sanitizers/issues/321.
  4396. int res = REAL(shmctl)(shmid, cmd, buf);
  4397. if (res >= 0) {
  4398. unsigned sz = 0;
  4399. if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
  4400. sz = sizeof(__sanitizer_shmid_ds);
  4401. else if (cmd == shmctl_ipc_info)
  4402. sz = struct_shminfo_sz;
  4403. else if (cmd == shmctl_shm_info)
  4404. sz = struct_shm_info_sz;
  4405. if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
  4406. }
  4407. return res;
  4408. }
  4409. #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
  4410. #else
  4411. #define INIT_SHMCTL
  4412. #endif
  4413. #if SANITIZER_INTERCEPT_RANDOM_R
  4414. INTERCEPTOR(int, random_r, void *buf, u32 *result) {
  4415. void *ctx;
  4416. COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
  4417. // FIXME: under ASan the call below may write to freed memory and corrupt
  4418. // its metadata. See
  4419. // https://github.com/google/sanitizers/issues/321.
  4420. int res = REAL(random_r)(buf, result);
  4421. if (!res && result)
  4422. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  4423. return res;
  4424. }
  4425. #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
  4426. #else
  4427. #define INIT_RANDOM_R
  4428. #endif
  4429. // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
  4430. // its metadata. See
  4431. // https://github.com/google/sanitizers/issues/321.
  4432. #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
  4433. SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED || \
  4434. SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
  4435. SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
  4436. SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
  4437. SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
  4438. SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
  4439. #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
  4440. INTERCEPTOR(int, fn, void *attr, void *r) { \
  4441. void *ctx; \
  4442. COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
  4443. int res = REAL(fn)(attr, r); \
  4444. if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
  4445. return res; \
  4446. }
  4447. #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
  4448. INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
  4449. #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
  4450. INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
  4451. #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
  4452. INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
  4453. #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
  4454. INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
  4455. #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
  4456. INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
  4457. #endif
  4458. #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
  4459. INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
  4460. INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
  4461. INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
  4462. INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
  4463. INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
  4464. void *ctx;
  4465. COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
  4466. // FIXME: under ASan the call below may write to freed memory and corrupt
  4467. // its metadata. See
  4468. // https://github.com/google/sanitizers/issues/321.
  4469. int res = REAL(pthread_attr_getstack)(attr, addr, size);
  4470. if (!res) {
  4471. if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
  4472. if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
  4473. }
  4474. return res;
  4475. }
  4476. // We may need to call the real pthread_attr_getstack from the run-time
  4477. // in sanitizer_common, but we don't want to include the interception headers
  4478. // there. So, just define this function here.
  4479. namespace __sanitizer {
  4480. extern "C" {
  4481. int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
  4482. return REAL(pthread_attr_getstack)(attr, addr, size);
  4483. }
  4484. } // extern "C"
  4485. } // namespace __sanitizer
  4486. #define INIT_PTHREAD_ATTR_GET \
  4487. COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
  4488. COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
  4489. COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
  4490. COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
  4491. COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
  4492. #else
  4493. #define INIT_PTHREAD_ATTR_GET
  4494. #endif
  4495. #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET_SCHED
  4496. INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
  4497. INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
  4498. #define INIT_PTHREAD_ATTR_GET_SCHED \
  4499. COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
  4500. COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy);
  4501. #else
  4502. #define INIT_PTHREAD_ATTR_GET_SCHED
  4503. #endif
  4504. #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
  4505. INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
  4506. #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
  4507. COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
  4508. #else
  4509. #define INIT_PTHREAD_ATTR_GETINHERITSCHED
  4510. #endif
  4511. #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
  4512. INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
  4513. void *cpuset) {
  4514. void *ctx;
  4515. COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
  4516. cpuset);
  4517. // FIXME: under ASan the call below may write to freed memory and corrupt
  4518. // its metadata. See
  4519. // https://github.com/google/sanitizers/issues/321.
  4520. int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
  4521. if (!res && cpusetsize && cpuset)
  4522. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
  4523. return res;
  4524. }
  4525. #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
  4526. COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
  4527. #else
  4528. #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
  4529. #endif
  4530. #if SANITIZER_INTERCEPT_PTHREAD_GETAFFINITY_NP
  4531. INTERCEPTOR(int, pthread_getaffinity_np, void *attr, SIZE_T cpusetsize,
  4532. void *cpuset) {
  4533. void *ctx;
  4534. COMMON_INTERCEPTOR_ENTER(ctx, pthread_getaffinity_np, attr, cpusetsize,
  4535. cpuset);
  4536. // FIXME: under ASan the call below may write to freed memory and corrupt
  4537. // its metadata. See
  4538. // https://github.com/google/sanitizers/issues/321.
  4539. int res = REAL(pthread_getaffinity_np)(attr, cpusetsize, cpuset);
  4540. if (!res && cpusetsize && cpuset)
  4541. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
  4542. return res;
  4543. }
  4544. #define INIT_PTHREAD_GETAFFINITY_NP \
  4545. COMMON_INTERCEPT_FUNCTION(pthread_getaffinity_np);
  4546. #else
  4547. #define INIT_PTHREAD_GETAFFINITY_NP
  4548. #endif
  4549. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
  4550. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
  4551. #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
  4552. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
  4553. #else
  4554. #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
  4555. #endif
  4556. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
  4557. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
  4558. #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
  4559. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
  4560. #else
  4561. #define INIT_PTHREAD_MUTEXATTR_GETTYPE
  4562. #endif
  4563. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
  4564. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
  4565. #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
  4566. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
  4567. #else
  4568. #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
  4569. #endif
  4570. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
  4571. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
  4572. #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
  4573. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
  4574. #else
  4575. #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
  4576. #endif
  4577. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
  4578. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
  4579. #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
  4580. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
  4581. #else
  4582. #define INIT_PTHREAD_MUTEXATTR_GETROBUST
  4583. #endif
  4584. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
  4585. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
  4586. #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
  4587. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
  4588. #else
  4589. #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
  4590. #endif
  4591. #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
  4592. INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
  4593. #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
  4594. COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
  4595. #else
  4596. #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
  4597. #endif
  4598. #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
  4599. INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
  4600. #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
  4601. COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
  4602. #else
  4603. #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
  4604. #endif
  4605. #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
  4606. INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
  4607. #define INIT_PTHREAD_CONDATTR_GETPSHARED \
  4608. COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
  4609. #else
  4610. #define INIT_PTHREAD_CONDATTR_GETPSHARED
  4611. #endif
  4612. #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
  4613. INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
  4614. #define INIT_PTHREAD_CONDATTR_GETCLOCK \
  4615. COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
  4616. #else
  4617. #define INIT_PTHREAD_CONDATTR_GETCLOCK
  4618. #endif
  4619. #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
  4620. INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
  4621. #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
  4622. COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
  4623. #else
  4624. #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
  4625. #endif
  4626. #if SANITIZER_INTERCEPT_TMPNAM
  4627. INTERCEPTOR(char *, tmpnam, char *s) {
  4628. void *ctx;
  4629. COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
  4630. char *res = REAL(tmpnam)(s);
  4631. if (res) {
  4632. if (s)
  4633. // FIXME: under ASan the call below may write to freed memory and corrupt
  4634. // its metadata. See
  4635. // https://github.com/google/sanitizers/issues/321.
  4636. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
  4637. else
  4638. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  4639. }
  4640. return res;
  4641. }
  4642. #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
  4643. #else
  4644. #define INIT_TMPNAM
  4645. #endif
  4646. #if SANITIZER_INTERCEPT_TMPNAM_R
  4647. INTERCEPTOR(char *, tmpnam_r, char *s) {
  4648. void *ctx;
  4649. COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
  4650. // FIXME: under ASan the call below may write to freed memory and corrupt
  4651. // its metadata. See
  4652. // https://github.com/google/sanitizers/issues/321.
  4653. char *res = REAL(tmpnam_r)(s);
  4654. if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, internal_strlen(s) + 1);
  4655. return res;
  4656. }
  4657. #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
  4658. #else
  4659. #define INIT_TMPNAM_R
  4660. #endif
  4661. #if SANITIZER_INTERCEPT_PTSNAME
  4662. INTERCEPTOR(char *, ptsname, int fd) {
  4663. void *ctx;
  4664. COMMON_INTERCEPTOR_ENTER(ctx, ptsname, fd);
  4665. char *res = REAL(ptsname)(fd);
  4666. if (res != nullptr)
  4667. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  4668. return res;
  4669. }
  4670. #define INIT_PTSNAME COMMON_INTERCEPT_FUNCTION(ptsname);
  4671. #else
  4672. #define INIT_PTSNAME
  4673. #endif
  4674. #if SANITIZER_INTERCEPT_PTSNAME_R
  4675. INTERCEPTOR(int, ptsname_r, int fd, char *name, SIZE_T namesize) {
  4676. void *ctx;
  4677. COMMON_INTERCEPTOR_ENTER(ctx, ptsname_r, fd, name, namesize);
  4678. int res = REAL(ptsname_r)(fd, name, namesize);
  4679. if (res == 0)
  4680. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
  4681. return res;
  4682. }
  4683. #define INIT_PTSNAME_R COMMON_INTERCEPT_FUNCTION(ptsname_r);
  4684. #else
  4685. #define INIT_PTSNAME_R
  4686. #endif
  4687. #if SANITIZER_INTERCEPT_TTYNAME
  4688. INTERCEPTOR(char *, ttyname, int fd) {
  4689. void *ctx;
  4690. COMMON_INTERCEPTOR_ENTER(ctx, ttyname, fd);
  4691. char *res = REAL(ttyname)(fd);
  4692. if (res != nullptr)
  4693. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  4694. return res;
  4695. }
  4696. #define INIT_TTYNAME COMMON_INTERCEPT_FUNCTION(ttyname);
  4697. #else
  4698. #define INIT_TTYNAME
  4699. #endif
  4700. #if SANITIZER_INTERCEPT_TTYNAME_R
  4701. INTERCEPTOR(int, ttyname_r, int fd, char *name, SIZE_T namesize) {
  4702. void *ctx;
  4703. COMMON_INTERCEPTOR_ENTER(ctx, ttyname_r, fd, name, namesize);
  4704. int res = REAL(ttyname_r)(fd, name, namesize);
  4705. if (res == 0)
  4706. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
  4707. return res;
  4708. }
  4709. #define INIT_TTYNAME_R COMMON_INTERCEPT_FUNCTION(ttyname_r);
  4710. #else
  4711. #define INIT_TTYNAME_R
  4712. #endif
  4713. #if SANITIZER_INTERCEPT_TEMPNAM
  4714. INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
  4715. void *ctx;
  4716. COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
  4717. if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, internal_strlen(dir) + 1);
  4718. if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, internal_strlen(pfx) + 1);
  4719. char *res = REAL(tempnam)(dir, pfx);
  4720. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  4721. return res;
  4722. }
  4723. #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
  4724. #else
  4725. #define INIT_TEMPNAM
  4726. #endif
  4727. #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && !SANITIZER_NETBSD
  4728. INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
  4729. void *ctx;
  4730. COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
  4731. COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
  4732. COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
  4733. return REAL(pthread_setname_np)(thread, name);
  4734. }
  4735. #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
  4736. #elif SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP && SANITIZER_NETBSD
  4737. INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name, void *arg) {
  4738. void *ctx;
  4739. char newname[32]; // PTHREAD_MAX_NAMELEN_NP=32
  4740. COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name, arg);
  4741. COMMON_INTERCEPTOR_READ_STRING(ctx, name, 0);
  4742. internal_snprintf(newname, sizeof(newname), name, arg);
  4743. COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, newname);
  4744. return REAL(pthread_setname_np)(thread, name, arg);
  4745. }
  4746. #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
  4747. #else
  4748. #define INIT_PTHREAD_SETNAME_NP
  4749. #endif
  4750. #if SANITIZER_INTERCEPT_PTHREAD_GETNAME_NP
  4751. INTERCEPTOR(int, pthread_getname_np, uptr thread, char *name, SIZE_T len) {
  4752. void *ctx;
  4753. COMMON_INTERCEPTOR_ENTER(ctx, pthread_getname_np, thread, name, len);
  4754. int res = REAL(pthread_getname_np)(thread, name, len);
  4755. if (!res)
  4756. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strnlen(name, len) + 1);
  4757. return res;
  4758. }
  4759. #define INIT_PTHREAD_GETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_getname_np);
  4760. #else
  4761. #define INIT_PTHREAD_GETNAME_NP
  4762. #endif
  4763. #if SANITIZER_INTERCEPT_SINCOS
  4764. INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
  4765. void *ctx;
  4766. COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
  4767. // FIXME: under ASan the call below may write to freed memory and corrupt
  4768. // its metadata. See
  4769. // https://github.com/google/sanitizers/issues/321.
  4770. REAL(sincos)(x, sin, cos);
  4771. if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
  4772. if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
  4773. }
  4774. INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
  4775. void *ctx;
  4776. COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
  4777. // FIXME: under ASan the call below may write to freed memory and corrupt
  4778. // its metadata. See
  4779. // https://github.com/google/sanitizers/issues/321.
  4780. REAL(sincosf)(x, sin, cos);
  4781. if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
  4782. if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
  4783. }
  4784. INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
  4785. void *ctx;
  4786. COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
  4787. // FIXME: under ASan the call below may write to freed memory and corrupt
  4788. // its metadata. See
  4789. // https://github.com/google/sanitizers/issues/321.
  4790. REAL(sincosl)(x, sin, cos);
  4791. if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
  4792. if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
  4793. }
  4794. #define INIT_SINCOS \
  4795. COMMON_INTERCEPT_FUNCTION(sincos); \
  4796. COMMON_INTERCEPT_FUNCTION(sincosf); \
  4797. COMMON_INTERCEPT_FUNCTION_LDBL(sincosl);
  4798. #else
  4799. #define INIT_SINCOS
  4800. #endif
  4801. #if SANITIZER_INTERCEPT_REMQUO
  4802. INTERCEPTOR(double, remquo, double x, double y, int *quo) {
  4803. void *ctx;
  4804. COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
  4805. // FIXME: under ASan the call below may write to freed memory and corrupt
  4806. // its metadata. See
  4807. // https://github.com/google/sanitizers/issues/321.
  4808. double res = REAL(remquo)(x, y, quo);
  4809. if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
  4810. return res;
  4811. }
  4812. INTERCEPTOR(float, remquof, float x, float y, int *quo) {
  4813. void *ctx;
  4814. COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
  4815. // FIXME: under ASan the call below may write to freed memory and corrupt
  4816. // its metadata. See
  4817. // https://github.com/google/sanitizers/issues/321.
  4818. float res = REAL(remquof)(x, y, quo);
  4819. if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
  4820. return res;
  4821. }
  4822. #define INIT_REMQUO \
  4823. COMMON_INTERCEPT_FUNCTION(remquo); \
  4824. COMMON_INTERCEPT_FUNCTION(remquof);
  4825. #else
  4826. #define INIT_REMQUO
  4827. #endif
  4828. #if SANITIZER_INTERCEPT_REMQUOL
  4829. INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
  4830. void *ctx;
  4831. COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
  4832. // FIXME: under ASan the call below may write to freed memory and corrupt
  4833. // its metadata. See
  4834. // https://github.com/google/sanitizers/issues/321.
  4835. long double res = REAL(remquol)(x, y, quo);
  4836. if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
  4837. return res;
  4838. }
  4839. #define INIT_REMQUOL \
  4840. COMMON_INTERCEPT_FUNCTION_LDBL(remquol);
  4841. #else
  4842. #define INIT_REMQUOL
  4843. #endif
  4844. #if SANITIZER_INTERCEPT_LGAMMA
  4845. extern int signgam;
  4846. INTERCEPTOR(double, lgamma, double x) {
  4847. void *ctx;
  4848. COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
  4849. double res = REAL(lgamma)(x);
  4850. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
  4851. return res;
  4852. }
  4853. INTERCEPTOR(float, lgammaf, float x) {
  4854. void *ctx;
  4855. COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
  4856. float res = REAL(lgammaf)(x);
  4857. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
  4858. return res;
  4859. }
  4860. #define INIT_LGAMMA \
  4861. COMMON_INTERCEPT_FUNCTION(lgamma); \
  4862. COMMON_INTERCEPT_FUNCTION(lgammaf);
  4863. #else
  4864. #define INIT_LGAMMA
  4865. #endif
  4866. #if SANITIZER_INTERCEPT_LGAMMAL
  4867. INTERCEPTOR(long double, lgammal, long double x) {
  4868. void *ctx;
  4869. COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
  4870. long double res = REAL(lgammal)(x);
  4871. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
  4872. return res;
  4873. }
  4874. #define INIT_LGAMMAL \
  4875. COMMON_INTERCEPT_FUNCTION_LDBL(lgammal);
  4876. #else
  4877. #define INIT_LGAMMAL
  4878. #endif
  4879. #if SANITIZER_INTERCEPT_LGAMMA_R
  4880. INTERCEPTOR(double, lgamma_r, double x, int *signp) {
  4881. void *ctx;
  4882. COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
  4883. // FIXME: under ASan the call below may write to freed memory and corrupt
  4884. // its metadata. See
  4885. // https://github.com/google/sanitizers/issues/321.
  4886. double res = REAL(lgamma_r)(x, signp);
  4887. if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
  4888. return res;
  4889. }
  4890. INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
  4891. void *ctx;
  4892. COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
  4893. // FIXME: under ASan the call below may write to freed memory and corrupt
  4894. // its metadata. See
  4895. // https://github.com/google/sanitizers/issues/321.
  4896. float res = REAL(lgammaf_r)(x, signp);
  4897. if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
  4898. return res;
  4899. }
  4900. #define INIT_LGAMMA_R \
  4901. COMMON_INTERCEPT_FUNCTION(lgamma_r); \
  4902. COMMON_INTERCEPT_FUNCTION(lgammaf_r);
  4903. #else
  4904. #define INIT_LGAMMA_R
  4905. #endif
  4906. #if SANITIZER_INTERCEPT_LGAMMAL_R
  4907. INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
  4908. void *ctx;
  4909. COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
  4910. // FIXME: under ASan the call below may write to freed memory and corrupt
  4911. // its metadata. See
  4912. // https://github.com/google/sanitizers/issues/321.
  4913. long double res = REAL(lgammal_r)(x, signp);
  4914. if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
  4915. return res;
  4916. }
  4917. #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION_LDBL(lgammal_r);
  4918. #else
  4919. #define INIT_LGAMMAL_R
  4920. #endif
  4921. #if SANITIZER_INTERCEPT_DRAND48_R
  4922. INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
  4923. void *ctx;
  4924. COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
  4925. // FIXME: under ASan the call below may write to freed memory and corrupt
  4926. // its metadata. See
  4927. // https://github.com/google/sanitizers/issues/321.
  4928. int res = REAL(drand48_r)(buffer, result);
  4929. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  4930. return res;
  4931. }
  4932. INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
  4933. void *ctx;
  4934. COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
  4935. // FIXME: under ASan the call below may write to freed memory and corrupt
  4936. // its metadata. See
  4937. // https://github.com/google/sanitizers/issues/321.
  4938. int res = REAL(lrand48_r)(buffer, result);
  4939. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  4940. return res;
  4941. }
  4942. #define INIT_DRAND48_R \
  4943. COMMON_INTERCEPT_FUNCTION(drand48_r); \
  4944. COMMON_INTERCEPT_FUNCTION(lrand48_r);
  4945. #else
  4946. #define INIT_DRAND48_R
  4947. #endif
  4948. #if SANITIZER_INTERCEPT_RAND_R
  4949. INTERCEPTOR(int, rand_r, unsigned *seedp) {
  4950. void *ctx;
  4951. COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
  4952. COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
  4953. return REAL(rand_r)(seedp);
  4954. }
  4955. #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
  4956. #else
  4957. #define INIT_RAND_R
  4958. #endif
  4959. #if SANITIZER_INTERCEPT_GETLINE
  4960. INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
  4961. void *ctx;
  4962. COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
  4963. // FIXME: under ASan the call below may write to freed memory and corrupt
  4964. // its metadata. See
  4965. // https://github.com/google/sanitizers/issues/321.
  4966. SSIZE_T res = REAL(getline)(lineptr, n, stream);
  4967. if (res > 0) {
  4968. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
  4969. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
  4970. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
  4971. }
  4972. return res;
  4973. }
  4974. // FIXME: under ASan the call below may write to freed memory and corrupt its
  4975. // metadata. See
  4976. // https://github.com/google/sanitizers/issues/321.
  4977. #define GETDELIM_INTERCEPTOR_IMPL(vname) \
  4978. { \
  4979. void *ctx; \
  4980. COMMON_INTERCEPTOR_ENTER(ctx, vname, lineptr, n, delim, stream); \
  4981. SSIZE_T res = REAL(vname)(lineptr, n, delim, stream); \
  4982. if (res > 0) { \
  4983. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr)); \
  4984. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n)); \
  4985. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1); \
  4986. } \
  4987. return res; \
  4988. }
  4989. INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
  4990. void *stream)
  4991. GETDELIM_INTERCEPTOR_IMPL(__getdelim)
  4992. // There's no __getdelim() on FreeBSD so we supply the getdelim() interceptor
  4993. // with its own body.
  4994. INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
  4995. void *stream)
  4996. GETDELIM_INTERCEPTOR_IMPL(getdelim)
  4997. #define INIT_GETLINE \
  4998. COMMON_INTERCEPT_FUNCTION(getline); \
  4999. COMMON_INTERCEPT_FUNCTION(__getdelim); \
  5000. COMMON_INTERCEPT_FUNCTION(getdelim);
  5001. #else
  5002. #define INIT_GETLINE
  5003. #endif
  5004. #if SANITIZER_INTERCEPT_ICONV
  5005. INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
  5006. char **outbuf, SIZE_T *outbytesleft) {
  5007. void *ctx;
  5008. COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
  5009. outbytesleft);
  5010. if (inbytesleft)
  5011. COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
  5012. if (inbuf && inbytesleft)
  5013. COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
  5014. if (outbytesleft)
  5015. COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
  5016. void *outbuf_orig = outbuf ? *outbuf : nullptr;
  5017. // FIXME: under ASan the call below may write to freed memory and corrupt
  5018. // its metadata. See
  5019. // https://github.com/google/sanitizers/issues/321.
  5020. SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
  5021. if (outbuf && *outbuf > outbuf_orig) {
  5022. SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
  5023. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
  5024. }
  5025. return res;
  5026. }
  5027. #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
  5028. #else
  5029. #define INIT_ICONV
  5030. #endif
  5031. #if SANITIZER_INTERCEPT_TIMES
  5032. INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
  5033. void *ctx;
  5034. COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
  5035. // FIXME: under ASan the call below may write to freed memory and corrupt
  5036. // its metadata. See
  5037. // https://github.com/google/sanitizers/issues/321.
  5038. __sanitizer_clock_t res = REAL(times)(tms);
  5039. if (res != (__sanitizer_clock_t)-1 && tms)
  5040. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
  5041. return res;
  5042. }
  5043. #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
  5044. #else
  5045. #define INIT_TIMES
  5046. #endif
  5047. #if SANITIZER_S390 && \
  5048. (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
  5049. extern "C" uptr __tls_get_offset_wrapper(void *arg, uptr (*fn)(void *arg));
  5050. DEFINE_REAL(uptr, __tls_get_offset, void *arg)
  5051. #endif
  5052. #if SANITIZER_INTERCEPT_TLS_GET_ADDR
  5053. #if !SANITIZER_S390
  5054. #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
  5055. // If you see any crashes around this functions, there are 2 known issues with
  5056. // it: 1. __tls_get_addr can be called with mis-aligned stack due to:
  5057. // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58066
  5058. // 2. It can be called recursively if sanitizer code uses __tls_get_addr
  5059. // to access thread local variables (it should not happen normally,
  5060. // because sanitizers use initial-exec tls model).
  5061. INTERCEPTOR(void *, __tls_get_addr, void *arg) {
  5062. void *ctx;
  5063. COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
  5064. void *res = REAL(__tls_get_addr)(arg);
  5065. uptr tls_begin, tls_end;
  5066. COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
  5067. DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
  5068. if (dtv) {
  5069. // New DTLS block has been allocated.
  5070. COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
  5071. }
  5072. return res;
  5073. }
  5074. #if SANITIZER_PPC
  5075. // On PowerPC, we also need to intercept __tls_get_addr_opt, which has
  5076. // mostly the same semantics as __tls_get_addr, but its presence enables
  5077. // some optimizations in linker (which are safe to ignore here).
  5078. extern "C" __attribute__((alias("__interceptor___tls_get_addr"),
  5079. visibility("default")))
  5080. void *__tls_get_addr_opt(void *arg);
  5081. #endif
  5082. #else // SANITIZER_S390
  5083. // On s390, we have to intercept two functions here:
  5084. // - __tls_get_addr_internal, which is a glibc-internal function that is like
  5085. // the usual __tls_get_addr, but returns a TP-relative offset instead of
  5086. // a proper pointer. It is used by dlsym for TLS symbols.
  5087. // - __tls_get_offset, which is like the above, but also takes a GOT-relative
  5088. // descriptor offset as an argument instead of a pointer. GOT address
  5089. // is passed in r12, so it's necessary to write it in assembly. This is
  5090. // the function used by the compiler.
  5091. #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_offset)
  5092. INTERCEPTOR(uptr, __tls_get_addr_internal, void *arg) {
  5093. void *ctx;
  5094. COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr_internal, arg);
  5095. uptr res = __tls_get_offset_wrapper(arg, REAL(__tls_get_offset));
  5096. uptr tp = reinterpret_cast<uptr>(__builtin_thread_pointer());
  5097. void *ptr = reinterpret_cast<void *>(res + tp);
  5098. uptr tls_begin, tls_end;
  5099. COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
  5100. DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, ptr, tls_begin, tls_end);
  5101. if (dtv) {
  5102. // New DTLS block has been allocated.
  5103. COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
  5104. }
  5105. return res;
  5106. }
  5107. #endif // SANITIZER_S390
  5108. #else
  5109. #define INIT_TLS_GET_ADDR
  5110. #endif
  5111. #if SANITIZER_S390 && \
  5112. (SANITIZER_INTERCEPT_TLS_GET_ADDR || SANITIZER_INTERCEPT_TLS_GET_OFFSET)
  5113. extern "C" uptr __tls_get_offset(void *arg);
  5114. extern "C" uptr __interceptor___tls_get_offset(void *arg);
  5115. // We need a hidden symbol aliasing the above, so that we can jump
  5116. // directly to it from the assembly below.
  5117. extern "C" __attribute__((alias("__interceptor___tls_get_addr_internal"),
  5118. visibility("hidden")))
  5119. uptr __tls_get_addr_hidden(void *arg);
  5120. // Now carefully intercept __tls_get_offset.
  5121. asm(
  5122. ".text\n"
  5123. // The __intercept_ version has to exist, so that gen_dynamic_list.py
  5124. // exports our symbol.
  5125. ".weak __tls_get_offset\n"
  5126. ".type __tls_get_offset, @function\n"
  5127. "__tls_get_offset:\n"
  5128. ".global __interceptor___tls_get_offset\n"
  5129. ".type __interceptor___tls_get_offset, @function\n"
  5130. "__interceptor___tls_get_offset:\n"
  5131. #ifdef __s390x__
  5132. "la %r2, 0(%r2,%r12)\n"
  5133. "jg __tls_get_addr_hidden\n"
  5134. #else
  5135. "basr %r3,0\n"
  5136. "0: la %r2,0(%r2,%r12)\n"
  5137. "l %r4,1f-0b(%r3)\n"
  5138. "b 0(%r4,%r3)\n"
  5139. "1: .long __tls_get_addr_hidden - 0b\n"
  5140. #endif
  5141. ".size __interceptor___tls_get_offset, .-__interceptor___tls_get_offset\n"
  5142. // Assembly wrapper to call REAL(__tls_get_offset)(arg)
  5143. ".type __tls_get_offset_wrapper, @function\n"
  5144. "__tls_get_offset_wrapper:\n"
  5145. #ifdef __s390x__
  5146. "sgr %r2,%r12\n"
  5147. #else
  5148. "sr %r2,%r12\n"
  5149. #endif
  5150. "br %r3\n"
  5151. ".size __tls_get_offset_wrapper, .-__tls_get_offset_wrapper\n"
  5152. );
  5153. #endif
  5154. #if SANITIZER_INTERCEPT_LISTXATTR
  5155. INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
  5156. void *ctx;
  5157. COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
  5158. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  5159. // FIXME: under ASan the call below may write to freed memory and corrupt
  5160. // its metadata. See
  5161. // https://github.com/google/sanitizers/issues/321.
  5162. SSIZE_T res = REAL(listxattr)(path, list, size);
  5163. // Here and below, size == 0 is a special case where nothing is written to the
  5164. // buffer, and res contains the desired buffer size.
  5165. if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
  5166. return res;
  5167. }
  5168. INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
  5169. void *ctx;
  5170. COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
  5171. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  5172. // FIXME: under ASan the call below may write to freed memory and corrupt
  5173. // its metadata. See
  5174. // https://github.com/google/sanitizers/issues/321.
  5175. SSIZE_T res = REAL(llistxattr)(path, list, size);
  5176. if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
  5177. return res;
  5178. }
  5179. INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
  5180. void *ctx;
  5181. COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
  5182. // FIXME: under ASan the call below may write to freed memory and corrupt
  5183. // its metadata. See
  5184. // https://github.com/google/sanitizers/issues/321.
  5185. SSIZE_T res = REAL(flistxattr)(fd, list, size);
  5186. if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
  5187. return res;
  5188. }
  5189. #define INIT_LISTXATTR \
  5190. COMMON_INTERCEPT_FUNCTION(listxattr); \
  5191. COMMON_INTERCEPT_FUNCTION(llistxattr); \
  5192. COMMON_INTERCEPT_FUNCTION(flistxattr);
  5193. #else
  5194. #define INIT_LISTXATTR
  5195. #endif
  5196. #if SANITIZER_INTERCEPT_GETXATTR
  5197. INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
  5198. SIZE_T size) {
  5199. void *ctx;
  5200. COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
  5201. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  5202. if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  5203. // FIXME: under ASan the call below may write to freed memory and corrupt
  5204. // its metadata. See
  5205. // https://github.com/google/sanitizers/issues/321.
  5206. SSIZE_T res = REAL(getxattr)(path, name, value, size);
  5207. if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
  5208. return res;
  5209. }
  5210. INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
  5211. SIZE_T size) {
  5212. void *ctx;
  5213. COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
  5214. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  5215. if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  5216. // FIXME: under ASan the call below may write to freed memory and corrupt
  5217. // its metadata. See
  5218. // https://github.com/google/sanitizers/issues/321.
  5219. SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
  5220. if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
  5221. return res;
  5222. }
  5223. INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
  5224. SIZE_T size) {
  5225. void *ctx;
  5226. COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
  5227. if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  5228. // FIXME: under ASan the call below may write to freed memory and corrupt
  5229. // its metadata. See
  5230. // https://github.com/google/sanitizers/issues/321.
  5231. SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
  5232. if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
  5233. return res;
  5234. }
  5235. #define INIT_GETXATTR \
  5236. COMMON_INTERCEPT_FUNCTION(getxattr); \
  5237. COMMON_INTERCEPT_FUNCTION(lgetxattr); \
  5238. COMMON_INTERCEPT_FUNCTION(fgetxattr);
  5239. #else
  5240. #define INIT_GETXATTR
  5241. #endif
  5242. #if SANITIZER_INTERCEPT_GETRESID
  5243. INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
  5244. void *ctx;
  5245. COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
  5246. // FIXME: under ASan the call below may write to freed memory and corrupt
  5247. // its metadata. See
  5248. // https://github.com/google/sanitizers/issues/321.
  5249. int res = REAL(getresuid)(ruid, euid, suid);
  5250. if (res >= 0) {
  5251. if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
  5252. if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
  5253. if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
  5254. }
  5255. return res;
  5256. }
  5257. INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
  5258. void *ctx;
  5259. COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
  5260. // FIXME: under ASan the call below may write to freed memory and corrupt
  5261. // its metadata. See
  5262. // https://github.com/google/sanitizers/issues/321.
  5263. int res = REAL(getresgid)(rgid, egid, sgid);
  5264. if (res >= 0) {
  5265. if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
  5266. if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
  5267. if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
  5268. }
  5269. return res;
  5270. }
  5271. #define INIT_GETRESID \
  5272. COMMON_INTERCEPT_FUNCTION(getresuid); \
  5273. COMMON_INTERCEPT_FUNCTION(getresgid);
  5274. #else
  5275. #define INIT_GETRESID
  5276. #endif
  5277. #if SANITIZER_INTERCEPT_GETIFADDRS
  5278. // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
  5279. // intercept freeifaddrs(). If that ceases to be the case, we might need to
  5280. // intercept it to poison the memory again.
  5281. INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
  5282. void *ctx;
  5283. COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
  5284. // FIXME: under ASan the call below may write to freed memory and corrupt
  5285. // its metadata. See
  5286. // https://github.com/google/sanitizers/issues/321.
  5287. int res = REAL(getifaddrs)(ifap);
  5288. if (res == 0 && ifap) {
  5289. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
  5290. __sanitizer_ifaddrs *p = *ifap;
  5291. while (p) {
  5292. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
  5293. if (p->ifa_name)
  5294. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
  5295. internal_strlen(p->ifa_name) + 1);
  5296. if (p->ifa_addr)
  5297. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
  5298. if (p->ifa_netmask)
  5299. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
  5300. // On Linux this is a union, but the other member also points to a
  5301. // struct sockaddr, so the following is sufficient.
  5302. if (p->ifa_dstaddr)
  5303. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
  5304. // FIXME(smatveev): Unpoison p->ifa_data as well.
  5305. p = p->ifa_next;
  5306. }
  5307. }
  5308. return res;
  5309. }
  5310. #define INIT_GETIFADDRS \
  5311. COMMON_INTERCEPT_FUNCTION(getifaddrs);
  5312. #else
  5313. #define INIT_GETIFADDRS
  5314. #endif
  5315. #if SANITIZER_INTERCEPT_IF_INDEXTONAME
  5316. INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
  5317. void *ctx;
  5318. COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
  5319. // FIXME: under ASan the call below may write to freed memory and corrupt
  5320. // its metadata. See
  5321. // https://github.com/google/sanitizers/issues/321.
  5322. char *res = REAL(if_indextoname)(ifindex, ifname);
  5323. if (res && ifname)
  5324. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
  5325. return res;
  5326. }
  5327. INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
  5328. void *ctx;
  5329. COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
  5330. if (ifname)
  5331. COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, internal_strlen(ifname) + 1);
  5332. return REAL(if_nametoindex)(ifname);
  5333. }
  5334. #define INIT_IF_INDEXTONAME \
  5335. COMMON_INTERCEPT_FUNCTION(if_indextoname); \
  5336. COMMON_INTERCEPT_FUNCTION(if_nametoindex);
  5337. #else
  5338. #define INIT_IF_INDEXTONAME
  5339. #endif
  5340. #if SANITIZER_INTERCEPT_CAPGET
  5341. INTERCEPTOR(int, capget, void *hdrp, void *datap) {
  5342. void *ctx;
  5343. COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
  5344. if (hdrp)
  5345. COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
  5346. // FIXME: under ASan the call below may write to freed memory and corrupt
  5347. // its metadata. See
  5348. // https://github.com/google/sanitizers/issues/321.
  5349. int res = REAL(capget)(hdrp, datap);
  5350. if (res == 0 && datap)
  5351. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
  5352. // We can also return -1 and write to hdrp->version if the version passed in
  5353. // hdrp->version is unsupported. But that's not a trivial condition to check,
  5354. // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
  5355. return res;
  5356. }
  5357. INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
  5358. void *ctx;
  5359. COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
  5360. if (hdrp)
  5361. COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
  5362. if (datap)
  5363. COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
  5364. return REAL(capset)(hdrp, datap);
  5365. }
  5366. #define INIT_CAPGET \
  5367. COMMON_INTERCEPT_FUNCTION(capget); \
  5368. COMMON_INTERCEPT_FUNCTION(capset);
  5369. #else
  5370. #define INIT_CAPGET
  5371. #endif
  5372. #if SANITIZER_INTERCEPT_AEABI_MEM
  5373. INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
  5374. void *ctx;
  5375. COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
  5376. }
  5377. INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
  5378. void *ctx;
  5379. COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
  5380. }
  5381. INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
  5382. void *ctx;
  5383. COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size);
  5384. }
  5385. INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
  5386. void *ctx;
  5387. COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
  5388. }
  5389. INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
  5390. void *ctx;
  5391. COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
  5392. }
  5393. INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
  5394. void *ctx;
  5395. COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size);
  5396. }
  5397. // Note the argument order.
  5398. INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
  5399. void *ctx;
  5400. COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
  5401. }
  5402. INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
  5403. void *ctx;
  5404. COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
  5405. }
  5406. INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
  5407. void *ctx;
  5408. COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size);
  5409. }
  5410. INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
  5411. void *ctx;
  5412. COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
  5413. }
  5414. INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
  5415. void *ctx;
  5416. COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
  5417. }
  5418. INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
  5419. void *ctx;
  5420. COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
  5421. }
  5422. #define INIT_AEABI_MEM \
  5423. COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
  5424. COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
  5425. COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
  5426. COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
  5427. COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
  5428. COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
  5429. COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
  5430. COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
  5431. COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
  5432. COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
  5433. COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
  5434. COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
  5435. #else
  5436. #define INIT_AEABI_MEM
  5437. #endif // SANITIZER_INTERCEPT_AEABI_MEM
  5438. #if SANITIZER_INTERCEPT___BZERO
  5439. INTERCEPTOR(void *, __bzero, void *block, uptr size) {
  5440. void *ctx;
  5441. COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
  5442. }
  5443. #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
  5444. #else
  5445. #define INIT___BZERO
  5446. #endif // SANITIZER_INTERCEPT___BZERO
  5447. #if SANITIZER_INTERCEPT_BZERO
  5448. INTERCEPTOR(void *, bzero, void *block, uptr size) {
  5449. void *ctx;
  5450. COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, 0, size);
  5451. }
  5452. #define INIT_BZERO COMMON_INTERCEPT_FUNCTION(bzero);
  5453. #else
  5454. #define INIT_BZERO
  5455. #endif // SANITIZER_INTERCEPT_BZERO
  5456. #if SANITIZER_INTERCEPT_FTIME
  5457. INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
  5458. void *ctx;
  5459. COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
  5460. // FIXME: under ASan the call below may write to freed memory and corrupt
  5461. // its metadata. See
  5462. // https://github.com/google/sanitizers/issues/321.
  5463. int res = REAL(ftime)(tp);
  5464. if (tp)
  5465. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
  5466. return res;
  5467. }
  5468. #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
  5469. #else
  5470. #define INIT_FTIME
  5471. #endif // SANITIZER_INTERCEPT_FTIME
  5472. #if SANITIZER_INTERCEPT_XDR
  5473. INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
  5474. unsigned size, int op) {
  5475. void *ctx;
  5476. COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
  5477. // FIXME: under ASan the call below may write to freed memory and corrupt
  5478. // its metadata. See
  5479. // https://github.com/google/sanitizers/issues/321.
  5480. REAL(xdrmem_create)(xdrs, addr, size, op);
  5481. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
  5482. if (op == __sanitizer_XDR_ENCODE) {
  5483. // It's not obvious how much data individual xdr_ routines write.
  5484. // Simply unpoison the entire target buffer in advance.
  5485. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
  5486. }
  5487. }
  5488. INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
  5489. void *ctx;
  5490. COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
  5491. // FIXME: under ASan the call below may write to freed memory and corrupt
  5492. // its metadata. See
  5493. // https://github.com/google/sanitizers/issues/321.
  5494. REAL(xdrstdio_create)(xdrs, file, op);
  5495. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
  5496. }
  5497. // FIXME: under ASan the call below may write to freed memory and corrupt
  5498. // its metadata. See
  5499. // https://github.com/google/sanitizers/issues/321.
  5500. #define XDR_INTERCEPTOR(F, T) \
  5501. INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
  5502. void *ctx; \
  5503. COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
  5504. if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
  5505. COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
  5506. int res = REAL(F)(xdrs, p); \
  5507. if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
  5508. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
  5509. return res; \
  5510. }
  5511. XDR_INTERCEPTOR(xdr_short, short)
  5512. XDR_INTERCEPTOR(xdr_u_short, unsigned short)
  5513. XDR_INTERCEPTOR(xdr_int, int)
  5514. XDR_INTERCEPTOR(xdr_u_int, unsigned)
  5515. XDR_INTERCEPTOR(xdr_long, long)
  5516. XDR_INTERCEPTOR(xdr_u_long, unsigned long)
  5517. XDR_INTERCEPTOR(xdr_hyper, long long)
  5518. XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
  5519. XDR_INTERCEPTOR(xdr_longlong_t, long long)
  5520. XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
  5521. XDR_INTERCEPTOR(xdr_int8_t, u8)
  5522. XDR_INTERCEPTOR(xdr_uint8_t, u8)
  5523. XDR_INTERCEPTOR(xdr_int16_t, u16)
  5524. XDR_INTERCEPTOR(xdr_uint16_t, u16)
  5525. XDR_INTERCEPTOR(xdr_int32_t, u32)
  5526. XDR_INTERCEPTOR(xdr_uint32_t, u32)
  5527. XDR_INTERCEPTOR(xdr_int64_t, u64)
  5528. XDR_INTERCEPTOR(xdr_uint64_t, u64)
  5529. XDR_INTERCEPTOR(xdr_quad_t, long long)
  5530. XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
  5531. XDR_INTERCEPTOR(xdr_bool, bool)
  5532. XDR_INTERCEPTOR(xdr_enum, int)
  5533. XDR_INTERCEPTOR(xdr_char, char)
  5534. XDR_INTERCEPTOR(xdr_u_char, unsigned char)
  5535. XDR_INTERCEPTOR(xdr_float, float)
  5536. XDR_INTERCEPTOR(xdr_double, double)
  5537. // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
  5538. // wrapstring, sizeof
  5539. INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
  5540. unsigned maxsize) {
  5541. void *ctx;
  5542. COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
  5543. if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
  5544. COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
  5545. COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
  5546. COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
  5547. }
  5548. // FIXME: under ASan the call below may write to freed memory and corrupt
  5549. // its metadata. See
  5550. // https://github.com/google/sanitizers/issues/321.
  5551. int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
  5552. if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
  5553. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
  5554. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
  5555. if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
  5556. }
  5557. return res;
  5558. }
  5559. INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
  5560. unsigned maxsize) {
  5561. void *ctx;
  5562. COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
  5563. if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
  5564. COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
  5565. COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, internal_strlen(*p) + 1);
  5566. }
  5567. // FIXME: under ASan the call below may write to freed memory and corrupt
  5568. // its metadata. See
  5569. // https://github.com/google/sanitizers/issues/321.
  5570. int res = REAL(xdr_string)(xdrs, p, maxsize);
  5571. if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
  5572. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
  5573. if (res && *p)
  5574. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, internal_strlen(*p) + 1);
  5575. }
  5576. return res;
  5577. }
  5578. #define INIT_XDR \
  5579. COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
  5580. COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
  5581. COMMON_INTERCEPT_FUNCTION(xdr_short); \
  5582. COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
  5583. COMMON_INTERCEPT_FUNCTION(xdr_int); \
  5584. COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
  5585. COMMON_INTERCEPT_FUNCTION(xdr_long); \
  5586. COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
  5587. COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
  5588. COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
  5589. COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
  5590. COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
  5591. COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
  5592. COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
  5593. COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
  5594. COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
  5595. COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
  5596. COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
  5597. COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
  5598. COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
  5599. COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
  5600. COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
  5601. COMMON_INTERCEPT_FUNCTION(xdr_bool); \
  5602. COMMON_INTERCEPT_FUNCTION(xdr_enum); \
  5603. COMMON_INTERCEPT_FUNCTION(xdr_char); \
  5604. COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
  5605. COMMON_INTERCEPT_FUNCTION(xdr_float); \
  5606. COMMON_INTERCEPT_FUNCTION(xdr_double); \
  5607. COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
  5608. COMMON_INTERCEPT_FUNCTION(xdr_string);
  5609. #else
  5610. #define INIT_XDR
  5611. #endif // SANITIZER_INTERCEPT_XDR
  5612. #if SANITIZER_INTERCEPT_XDRREC
  5613. typedef int (*xdrrec_cb)(char*, char*, int);
  5614. struct XdrRecWrapper {
  5615. char *handle;
  5616. xdrrec_cb rd, wr;
  5617. };
  5618. typedef AddrHashMap<XdrRecWrapper *, 11> XdrRecWrapMap;
  5619. static XdrRecWrapMap *xdrrec_wrap_map;
  5620. static int xdrrec_wr_wrap(char *handle, char *buf, int count) {
  5621. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  5622. COMMON_INTERCEPTOR_INITIALIZE_RANGE(buf, count);
  5623. XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
  5624. return wrap->wr(wrap->handle, buf, count);
  5625. }
  5626. static int xdrrec_rd_wrap(char *handle, char *buf, int count) {
  5627. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  5628. XdrRecWrapper *wrap = (XdrRecWrapper *)handle;
  5629. return wrap->rd(wrap->handle, buf, count);
  5630. }
  5631. // This doesn't apply to the solaris version as it has a different function
  5632. // signature.
  5633. INTERCEPTOR(void, xdrrec_create, __sanitizer_XDR *xdr, unsigned sndsize,
  5634. unsigned rcvsize, char *handle, int (*rd)(char*, char*, int),
  5635. int (*wr)(char*, char*, int)) {
  5636. void *ctx;
  5637. COMMON_INTERCEPTOR_ENTER(ctx, xdrrec_create, xdr, sndsize, rcvsize,
  5638. handle, rd, wr);
  5639. COMMON_INTERCEPTOR_READ_RANGE(ctx, &xdr->x_op, sizeof xdr->x_op);
  5640. // We can't allocate a wrapper on the stack, as the handle is used outside
  5641. // this stack frame. So we put it on the heap, and keep track of it with
  5642. // the HashMap (keyed by x_private). When we later need to xdr_destroy,
  5643. // we can index the map, free the wrapper, and then clean the map entry.
  5644. XdrRecWrapper *wrap_data =
  5645. (XdrRecWrapper *)InternalAlloc(sizeof(XdrRecWrapper));
  5646. wrap_data->handle = handle;
  5647. wrap_data->rd = rd;
  5648. wrap_data->wr = wr;
  5649. if (wr)
  5650. wr = xdrrec_wr_wrap;
  5651. if (rd)
  5652. rd = xdrrec_rd_wrap;
  5653. handle = (char *)wrap_data;
  5654. REAL(xdrrec_create)(xdr, sndsize, rcvsize, handle, rd, wr);
  5655. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdr, sizeof *xdr);
  5656. XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, false, true);
  5657. *wrap = wrap_data;
  5658. }
  5659. // We have to intercept this to be able to free wrapper memory;
  5660. // otherwise it's not necessary.
  5661. INTERCEPTOR(void, xdr_destroy, __sanitizer_XDR *xdr) {
  5662. void *ctx;
  5663. COMMON_INTERCEPTOR_ENTER(ctx, xdr_destroy, xdr);
  5664. XdrRecWrapMap::Handle wrap(xdrrec_wrap_map, xdr->x_private, true);
  5665. InternalFree(*wrap);
  5666. REAL(xdr_destroy)(xdr);
  5667. }
  5668. #define INIT_XDRREC_LINUX \
  5669. static u64 xdrrec_wrap_mem[sizeof(XdrRecWrapMap) / sizeof(u64) + 1]; \
  5670. xdrrec_wrap_map = new ((void *)&xdrrec_wrap_mem) XdrRecWrapMap(); \
  5671. COMMON_INTERCEPT_FUNCTION(xdrrec_create); \
  5672. COMMON_INTERCEPT_FUNCTION(xdr_destroy);
  5673. #else
  5674. #define INIT_XDRREC_LINUX
  5675. #endif
  5676. #if SANITIZER_INTERCEPT_TSEARCH
  5677. INTERCEPTOR(void *, tsearch, void *key, void **rootp,
  5678. int (*compar)(const void *, const void *)) {
  5679. void *ctx;
  5680. COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
  5681. // FIXME: under ASan the call below may write to freed memory and corrupt
  5682. // its metadata. See
  5683. // https://github.com/google/sanitizers/issues/321.
  5684. void *res = REAL(tsearch)(key, rootp, compar);
  5685. if (res && *(void **)res == key)
  5686. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
  5687. return res;
  5688. }
  5689. #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
  5690. #else
  5691. #define INIT_TSEARCH
  5692. #endif
  5693. #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
  5694. SANITIZER_INTERCEPT_OPEN_MEMSTREAM
  5695. void unpoison_file(__sanitizer_FILE *fp) {
  5696. #if SANITIZER_HAS_STRUCT_FILE
  5697. COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
  5698. #if SANITIZER_NETBSD
  5699. if (fp->_bf._base && fp->_bf._size > 0)
  5700. COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_bf._base,
  5701. fp->_bf._size);
  5702. #else
  5703. if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
  5704. COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
  5705. fp->_IO_read_end - fp->_IO_read_base);
  5706. if (fp->_IO_write_base && fp->_IO_write_base < fp->_IO_write_end)
  5707. COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_write_base,
  5708. fp->_IO_write_end - fp->_IO_write_base);
  5709. #endif
  5710. #endif // SANITIZER_HAS_STRUCT_FILE
  5711. }
  5712. #endif
  5713. #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
  5714. // These guys are called when a .c source is built with -O2.
  5715. INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
  5716. void *ctx;
  5717. COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
  5718. int res = REAL(__uflow)(fp);
  5719. unpoison_file(fp);
  5720. return res;
  5721. }
  5722. INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
  5723. void *ctx;
  5724. COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
  5725. int res = REAL(__underflow)(fp);
  5726. unpoison_file(fp);
  5727. return res;
  5728. }
  5729. INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
  5730. void *ctx;
  5731. COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
  5732. int res = REAL(__overflow)(fp, ch);
  5733. unpoison_file(fp);
  5734. return res;
  5735. }
  5736. INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
  5737. void *ctx;
  5738. COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
  5739. int res = REAL(__wuflow)(fp);
  5740. unpoison_file(fp);
  5741. return res;
  5742. }
  5743. INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
  5744. void *ctx;
  5745. COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
  5746. int res = REAL(__wunderflow)(fp);
  5747. unpoison_file(fp);
  5748. return res;
  5749. }
  5750. INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
  5751. void *ctx;
  5752. COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
  5753. int res = REAL(__woverflow)(fp, ch);
  5754. unpoison_file(fp);
  5755. return res;
  5756. }
  5757. #define INIT_LIBIO_INTERNALS \
  5758. COMMON_INTERCEPT_FUNCTION(__uflow); \
  5759. COMMON_INTERCEPT_FUNCTION(__underflow); \
  5760. COMMON_INTERCEPT_FUNCTION(__overflow); \
  5761. COMMON_INTERCEPT_FUNCTION(__wuflow); \
  5762. COMMON_INTERCEPT_FUNCTION(__wunderflow); \
  5763. COMMON_INTERCEPT_FUNCTION(__woverflow);
  5764. #else
  5765. #define INIT_LIBIO_INTERNALS
  5766. #endif
  5767. #if SANITIZER_INTERCEPT_FOPEN
  5768. INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
  5769. void *ctx;
  5770. COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
  5771. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  5772. COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
  5773. __sanitizer_FILE *res = REAL(fopen)(path, mode);
  5774. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
  5775. if (res) unpoison_file(res);
  5776. return res;
  5777. }
  5778. INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
  5779. void *ctx;
  5780. COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
  5781. COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
  5782. __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
  5783. if (res) unpoison_file(res);
  5784. return res;
  5785. }
  5786. INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
  5787. __sanitizer_FILE *fp) {
  5788. void *ctx;
  5789. COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
  5790. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  5791. COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
  5792. COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
  5793. __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
  5794. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
  5795. if (res) unpoison_file(res);
  5796. return res;
  5797. }
  5798. #define INIT_FOPEN \
  5799. COMMON_INTERCEPT_FUNCTION(fopen); \
  5800. COMMON_INTERCEPT_FUNCTION(fdopen); \
  5801. COMMON_INTERCEPT_FUNCTION(freopen);
  5802. #else
  5803. #define INIT_FOPEN
  5804. #endif
  5805. #if SANITIZER_INTERCEPT_FLOPEN
  5806. INTERCEPTOR(int, flopen, const char *path, int flags, ...) {
  5807. void *ctx;
  5808. va_list ap;
  5809. va_start(ap, flags);
  5810. u16 mode = static_cast<u16>(va_arg(ap, u32));
  5811. va_end(ap);
  5812. COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
  5813. if (path) {
  5814. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  5815. }
  5816. return REAL(flopen)(path, flags, mode);
  5817. }
  5818. INTERCEPTOR(int, flopenat, int dirfd, const char *path, int flags, ...) {
  5819. void *ctx;
  5820. va_list ap;
  5821. va_start(ap, flags);
  5822. u16 mode = static_cast<u16>(va_arg(ap, u32));
  5823. va_end(ap);
  5824. COMMON_INTERCEPTOR_ENTER(ctx, flopen, path, flags, mode);
  5825. if (path) {
  5826. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  5827. }
  5828. return REAL(flopenat)(dirfd, path, flags, mode);
  5829. }
  5830. #define INIT_FLOPEN \
  5831. COMMON_INTERCEPT_FUNCTION(flopen); \
  5832. COMMON_INTERCEPT_FUNCTION(flopenat);
  5833. #else
  5834. #define INIT_FLOPEN
  5835. #endif
  5836. #if SANITIZER_INTERCEPT_FOPEN64
  5837. INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
  5838. void *ctx;
  5839. COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
  5840. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  5841. COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
  5842. __sanitizer_FILE *res = REAL(fopen64)(path, mode);
  5843. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
  5844. if (res) unpoison_file(res);
  5845. return res;
  5846. }
  5847. INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
  5848. __sanitizer_FILE *fp) {
  5849. void *ctx;
  5850. COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
  5851. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  5852. COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, internal_strlen(mode) + 1);
  5853. COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
  5854. __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
  5855. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
  5856. if (res) unpoison_file(res);
  5857. return res;
  5858. }
  5859. #define INIT_FOPEN64 \
  5860. COMMON_INTERCEPT_FUNCTION(fopen64); \
  5861. COMMON_INTERCEPT_FUNCTION(freopen64);
  5862. #else
  5863. #define INIT_FOPEN64
  5864. #endif
  5865. #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
  5866. INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
  5867. void *ctx;
  5868. COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
  5869. // FIXME: under ASan the call below may write to freed memory and corrupt
  5870. // its metadata. See
  5871. // https://github.com/google/sanitizers/issues/321.
  5872. __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
  5873. if (res) {
  5874. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
  5875. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
  5876. unpoison_file(res);
  5877. FileMetadata file = {ptr, sizeloc};
  5878. SetInterceptorMetadata(res, file);
  5879. }
  5880. return res;
  5881. }
  5882. INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
  5883. SIZE_T *sizeloc) {
  5884. void *ctx;
  5885. COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
  5886. __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
  5887. if (res) {
  5888. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
  5889. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
  5890. unpoison_file(res);
  5891. FileMetadata file = {(char **)ptr, sizeloc};
  5892. SetInterceptorMetadata(res, file);
  5893. }
  5894. return res;
  5895. }
  5896. INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
  5897. const char *mode) {
  5898. void *ctx;
  5899. COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
  5900. // FIXME: under ASan the call below may write to freed memory and corrupt
  5901. // its metadata. See
  5902. // https://github.com/google/sanitizers/issues/321.
  5903. __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
  5904. if (res) unpoison_file(res);
  5905. return res;
  5906. }
  5907. #define INIT_OPEN_MEMSTREAM \
  5908. COMMON_INTERCEPT_FUNCTION(open_memstream); \
  5909. COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
  5910. COMMON_INTERCEPT_FUNCTION(fmemopen);
  5911. #else
  5912. #define INIT_OPEN_MEMSTREAM
  5913. #endif
  5914. #if SANITIZER_INTERCEPT_OBSTACK
  5915. static void initialize_obstack(__sanitizer_obstack *obstack) {
  5916. COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
  5917. if (obstack->chunk)
  5918. COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
  5919. sizeof(*obstack->chunk));
  5920. }
  5921. INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
  5922. int align, void *(*alloc_fn)(uptr arg, uptr sz),
  5923. void (*free_fn)(uptr arg, void *p)) {
  5924. void *ctx;
  5925. COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
  5926. free_fn);
  5927. int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
  5928. if (res) initialize_obstack(obstack);
  5929. return res;
  5930. }
  5931. INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
  5932. int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
  5933. void *ctx;
  5934. COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
  5935. free_fn);
  5936. int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
  5937. if (res) initialize_obstack(obstack);
  5938. return res;
  5939. }
  5940. INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
  5941. void *ctx;
  5942. COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
  5943. REAL(_obstack_newchunk)(obstack, length);
  5944. if (obstack->chunk)
  5945. COMMON_INTERCEPTOR_INITIALIZE_RANGE(
  5946. obstack->chunk, obstack->next_free - (char *)obstack->chunk);
  5947. }
  5948. #define INIT_OBSTACK \
  5949. COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
  5950. COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
  5951. COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
  5952. #else
  5953. #define INIT_OBSTACK
  5954. #endif
  5955. #if SANITIZER_INTERCEPT_FFLUSH
  5956. INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
  5957. void *ctx;
  5958. COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
  5959. if (fp)
  5960. unpoison_file(fp);
  5961. int res = REAL(fflush)(fp);
  5962. // FIXME: handle fp == NULL
  5963. if (fp) {
  5964. const FileMetadata *m = GetInterceptorMetadata(fp);
  5965. if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
  5966. }
  5967. return res;
  5968. }
  5969. #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
  5970. #else
  5971. #define INIT_FFLUSH
  5972. #endif
  5973. #if SANITIZER_INTERCEPT_FCLOSE
  5974. INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
  5975. void *ctx;
  5976. COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
  5977. COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
  5978. const FileMetadata *m = GetInterceptorMetadata(fp);
  5979. if (fp)
  5980. unpoison_file(fp);
  5981. int res = REAL(fclose)(fp);
  5982. if (m) {
  5983. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
  5984. DeleteInterceptorMetadata(fp);
  5985. }
  5986. return res;
  5987. }
  5988. #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
  5989. #else
  5990. #define INIT_FCLOSE
  5991. #endif
  5992. #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
  5993. INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
  5994. void *ctx;
  5995. COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
  5996. if (filename) COMMON_INTERCEPTOR_READ_STRING(ctx, filename, 0);
  5997. void *res = COMMON_INTERCEPTOR_DLOPEN(filename, flag);
  5998. Symbolizer::GetOrInit()->InvalidateModuleList();
  5999. COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
  6000. return res;
  6001. }
  6002. INTERCEPTOR(int, dlclose, void *handle) {
  6003. void *ctx;
  6004. COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
  6005. int res = REAL(dlclose)(handle);
  6006. Symbolizer::GetOrInit()->InvalidateModuleList();
  6007. COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
  6008. return res;
  6009. }
  6010. #define INIT_DLOPEN_DLCLOSE \
  6011. COMMON_INTERCEPT_FUNCTION(dlopen); \
  6012. COMMON_INTERCEPT_FUNCTION(dlclose);
  6013. #else
  6014. #define INIT_DLOPEN_DLCLOSE
  6015. #endif
  6016. #if SANITIZER_INTERCEPT_GETPASS
  6017. INTERCEPTOR(char *, getpass, const char *prompt) {
  6018. void *ctx;
  6019. COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
  6020. if (prompt)
  6021. COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, internal_strlen(prompt)+1);
  6022. char *res = REAL(getpass)(prompt);
  6023. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res)+1);
  6024. return res;
  6025. }
  6026. #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
  6027. #else
  6028. #define INIT_GETPASS
  6029. #endif
  6030. #if SANITIZER_INTERCEPT_TIMERFD
  6031. INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
  6032. void *old_value) {
  6033. void *ctx;
  6034. COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
  6035. old_value);
  6036. COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
  6037. int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
  6038. if (res != -1 && old_value)
  6039. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
  6040. return res;
  6041. }
  6042. INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
  6043. void *ctx;
  6044. COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
  6045. int res = REAL(timerfd_gettime)(fd, curr_value);
  6046. if (res != -1 && curr_value)
  6047. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
  6048. return res;
  6049. }
  6050. #define INIT_TIMERFD \
  6051. COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
  6052. COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
  6053. #else
  6054. #define INIT_TIMERFD
  6055. #endif
  6056. #if SANITIZER_INTERCEPT_MLOCKX
  6057. // Linux kernel has a bug that leads to kernel deadlock if a process
  6058. // maps TBs of memory and then calls mlock().
  6059. static void MlockIsUnsupported() {
  6060. static atomic_uint8_t printed;
  6061. if (atomic_exchange(&printed, 1, memory_order_relaxed))
  6062. return;
  6063. VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
  6064. SanitizerToolName);
  6065. }
  6066. INTERCEPTOR(int, mlock, const void *addr, uptr len) {
  6067. MlockIsUnsupported();
  6068. return 0;
  6069. }
  6070. INTERCEPTOR(int, munlock, const void *addr, uptr len) {
  6071. MlockIsUnsupported();
  6072. return 0;
  6073. }
  6074. INTERCEPTOR(int, mlockall, int flags) {
  6075. MlockIsUnsupported();
  6076. return 0;
  6077. }
  6078. INTERCEPTOR(int, munlockall, void) {
  6079. MlockIsUnsupported();
  6080. return 0;
  6081. }
  6082. #define INIT_MLOCKX \
  6083. COMMON_INTERCEPT_FUNCTION(mlock); \
  6084. COMMON_INTERCEPT_FUNCTION(munlock); \
  6085. COMMON_INTERCEPT_FUNCTION(mlockall); \
  6086. COMMON_INTERCEPT_FUNCTION(munlockall);
  6087. #else
  6088. #define INIT_MLOCKX
  6089. #endif // SANITIZER_INTERCEPT_MLOCKX
  6090. #if SANITIZER_INTERCEPT_FOPENCOOKIE
  6091. struct WrappedCookie {
  6092. void *real_cookie;
  6093. __sanitizer_cookie_io_functions_t real_io_funcs;
  6094. };
  6095. static uptr wrapped_read(void *cookie, char *buf, uptr size) {
  6096. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  6097. WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
  6098. __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
  6099. return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
  6100. }
  6101. static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
  6102. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  6103. WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
  6104. __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
  6105. return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
  6106. }
  6107. static int wrapped_seek(void *cookie, u64 *offset, int whence) {
  6108. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  6109. COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
  6110. WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
  6111. __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
  6112. return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
  6113. : -1;
  6114. }
  6115. static int wrapped_close(void *cookie) {
  6116. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  6117. WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
  6118. __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
  6119. int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
  6120. InternalFree(wrapped_cookie);
  6121. return res;
  6122. }
  6123. INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
  6124. __sanitizer_cookie_io_functions_t io_funcs) {
  6125. void *ctx;
  6126. COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
  6127. WrappedCookie *wrapped_cookie =
  6128. (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
  6129. wrapped_cookie->real_cookie = cookie;
  6130. wrapped_cookie->real_io_funcs = io_funcs;
  6131. __sanitizer_FILE *res =
  6132. REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
  6133. wrapped_seek, wrapped_close});
  6134. return res;
  6135. }
  6136. #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
  6137. #else
  6138. #define INIT_FOPENCOOKIE
  6139. #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
  6140. #if SANITIZER_INTERCEPT_SEM
  6141. INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
  6142. void *ctx;
  6143. COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
  6144. // Workaround a bug in glibc's "old" semaphore implementation by
  6145. // zero-initializing the sem_t contents. This has to be done here because
  6146. // interceptors bind to the lowest version before glibc 2.36, hitting the
  6147. // buggy code path while the non-sanitized build of the same code works fine.
  6148. REAL(memset)(s, 0, sizeof(*s));
  6149. int res = REAL(sem_init)(s, pshared, value);
  6150. return res;
  6151. }
  6152. INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
  6153. void *ctx;
  6154. COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
  6155. int res = REAL(sem_destroy)(s);
  6156. return res;
  6157. }
  6158. INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
  6159. void *ctx;
  6160. COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
  6161. int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
  6162. if (res == 0) {
  6163. COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
  6164. }
  6165. return res;
  6166. }
  6167. INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
  6168. void *ctx;
  6169. COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
  6170. int res = REAL(sem_trywait)(s);
  6171. if (res == 0) {
  6172. COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
  6173. }
  6174. return res;
  6175. }
  6176. INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
  6177. void *ctx;
  6178. COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
  6179. COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
  6180. int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
  6181. if (res == 0) {
  6182. COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
  6183. }
  6184. return res;
  6185. }
  6186. INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
  6187. void *ctx;
  6188. COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
  6189. COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
  6190. int res = REAL(sem_post)(s);
  6191. return res;
  6192. }
  6193. INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
  6194. void *ctx;
  6195. COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
  6196. int res = REAL(sem_getvalue)(s, sval);
  6197. if (res == 0) {
  6198. COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
  6199. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
  6200. }
  6201. return res;
  6202. }
  6203. INTERCEPTOR(__sanitizer_sem_t *, sem_open, const char *name, int oflag, ...) {
  6204. void *ctx;
  6205. va_list ap;
  6206. va_start(ap, oflag);
  6207. u32 mode = va_arg(ap, u32);
  6208. u32 value = va_arg(ap, u32);
  6209. COMMON_INTERCEPTOR_ENTER(ctx, sem_open, name, oflag, mode, value);
  6210. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  6211. __sanitizer_sem_t *s = REAL(sem_open)(name, oflag, mode, value);
  6212. if (s)
  6213. COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, sizeof(*s));
  6214. va_end(ap);
  6215. return s;
  6216. }
  6217. INTERCEPTOR(int, sem_unlink, const char *name) {
  6218. void *ctx;
  6219. COMMON_INTERCEPTOR_ENTER(ctx, sem_unlink, name);
  6220. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  6221. return REAL(sem_unlink)(name);
  6222. }
  6223. # define INIT_SEM \
  6224. COMMON_INTERCEPT_FUNCTION(sem_init); \
  6225. COMMON_INTERCEPT_FUNCTION(sem_destroy); \
  6226. COMMON_INTERCEPT_FUNCTION(sem_wait); \
  6227. COMMON_INTERCEPT_FUNCTION(sem_trywait); \
  6228. COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
  6229. COMMON_INTERCEPT_FUNCTION(sem_post); \
  6230. COMMON_INTERCEPT_FUNCTION(sem_getvalue); \
  6231. COMMON_INTERCEPT_FUNCTION(sem_open); \
  6232. COMMON_INTERCEPT_FUNCTION(sem_unlink);
  6233. #else
  6234. # define INIT_SEM
  6235. #endif // SANITIZER_INTERCEPT_SEM
  6236. #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
  6237. INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
  6238. void *ctx;
  6239. COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
  6240. int res = REAL(pthread_setcancelstate)(state, oldstate);
  6241. if (res == 0 && oldstate != nullptr)
  6242. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
  6243. return res;
  6244. }
  6245. INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
  6246. void *ctx;
  6247. COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
  6248. int res = REAL(pthread_setcanceltype)(type, oldtype);
  6249. if (res == 0 && oldtype != nullptr)
  6250. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
  6251. return res;
  6252. }
  6253. #define INIT_PTHREAD_SETCANCEL \
  6254. COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
  6255. COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
  6256. #else
  6257. #define INIT_PTHREAD_SETCANCEL
  6258. #endif
  6259. #if SANITIZER_INTERCEPT_MINCORE
  6260. INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
  6261. void *ctx;
  6262. COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
  6263. int res = REAL(mincore)(addr, length, vec);
  6264. if (res == 0) {
  6265. uptr page_size = GetPageSizeCached();
  6266. uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
  6267. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
  6268. }
  6269. return res;
  6270. }
  6271. #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
  6272. #else
  6273. #define INIT_MINCORE
  6274. #endif
  6275. #if SANITIZER_INTERCEPT_PROCESS_VM_READV
  6276. INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
  6277. uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
  6278. uptr flags) {
  6279. void *ctx;
  6280. COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
  6281. remote_iov, riovcnt, flags);
  6282. SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
  6283. riovcnt, flags);
  6284. if (res > 0)
  6285. write_iovec(ctx, local_iov, liovcnt, res);
  6286. return res;
  6287. }
  6288. INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
  6289. uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
  6290. uptr flags) {
  6291. void *ctx;
  6292. COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
  6293. remote_iov, riovcnt, flags);
  6294. SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
  6295. riovcnt, flags);
  6296. if (res > 0)
  6297. read_iovec(ctx, local_iov, liovcnt, res);
  6298. return res;
  6299. }
  6300. #define INIT_PROCESS_VM_READV \
  6301. COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
  6302. COMMON_INTERCEPT_FUNCTION(process_vm_writev);
  6303. #else
  6304. #define INIT_PROCESS_VM_READV
  6305. #endif
  6306. #if SANITIZER_INTERCEPT_CTERMID
  6307. INTERCEPTOR(char *, ctermid, char *s) {
  6308. void *ctx;
  6309. COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
  6310. char *res = REAL(ctermid)(s);
  6311. if (res) {
  6312. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  6313. }
  6314. return res;
  6315. }
  6316. #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
  6317. #else
  6318. #define INIT_CTERMID
  6319. #endif
  6320. #if SANITIZER_INTERCEPT_CTERMID_R
  6321. INTERCEPTOR(char *, ctermid_r, char *s) {
  6322. void *ctx;
  6323. COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
  6324. char *res = REAL(ctermid_r)(s);
  6325. if (res) {
  6326. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  6327. }
  6328. return res;
  6329. }
  6330. #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
  6331. #else
  6332. #define INIT_CTERMID_R
  6333. #endif
  6334. #if SANITIZER_INTERCEPT_RECV_RECVFROM
  6335. INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
  6336. void *ctx;
  6337. COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
  6338. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6339. SSIZE_T res = REAL(recv)(fd, buf, len, flags);
  6340. if (res > 0) {
  6341. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
  6342. }
  6343. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  6344. return res;
  6345. }
  6346. INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
  6347. void *srcaddr, int *addrlen) {
  6348. void *ctx;
  6349. COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
  6350. addrlen);
  6351. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6352. SIZE_T srcaddr_sz;
  6353. if (srcaddr) srcaddr_sz = *addrlen;
  6354. (void)srcaddr_sz; // prevent "set but not used" warning
  6355. SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
  6356. if (res > 0)
  6357. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
  6358. if (res >= 0 && srcaddr)
  6359. COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
  6360. Min((SIZE_T)*addrlen, srcaddr_sz));
  6361. return res;
  6362. }
  6363. #define INIT_RECV_RECVFROM \
  6364. COMMON_INTERCEPT_FUNCTION(recv); \
  6365. COMMON_INTERCEPT_FUNCTION(recvfrom);
  6366. #else
  6367. #define INIT_RECV_RECVFROM
  6368. #endif
  6369. #if SANITIZER_INTERCEPT_SEND_SENDTO
  6370. INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
  6371. void *ctx;
  6372. COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
  6373. if (fd >= 0) {
  6374. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6375. COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  6376. }
  6377. SSIZE_T res = REAL(send)(fd, buf, len, flags);
  6378. if (common_flags()->intercept_send && res > 0)
  6379. COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
  6380. return res;
  6381. }
  6382. INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
  6383. void *dstaddr, int addrlen) {
  6384. void *ctx;
  6385. COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
  6386. if (fd >= 0) {
  6387. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6388. COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  6389. }
  6390. // Can't check dstaddr as it may have uninitialized padding at the end.
  6391. SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
  6392. if (common_flags()->intercept_send && res > 0)
  6393. COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
  6394. return res;
  6395. }
  6396. #define INIT_SEND_SENDTO \
  6397. COMMON_INTERCEPT_FUNCTION(send); \
  6398. COMMON_INTERCEPT_FUNCTION(sendto);
  6399. #else
  6400. #define INIT_SEND_SENDTO
  6401. #endif
  6402. #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
  6403. INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
  6404. void *ctx;
  6405. COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
  6406. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6407. int res = REAL(eventfd_read)(fd, value);
  6408. if (res == 0) {
  6409. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
  6410. if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  6411. }
  6412. return res;
  6413. }
  6414. INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
  6415. void *ctx;
  6416. COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
  6417. if (fd >= 0) {
  6418. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6419. COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  6420. }
  6421. int res = REAL(eventfd_write)(fd, value);
  6422. return res;
  6423. }
  6424. #define INIT_EVENTFD_READ_WRITE \
  6425. COMMON_INTERCEPT_FUNCTION(eventfd_read); \
  6426. COMMON_INTERCEPT_FUNCTION(eventfd_write)
  6427. #else
  6428. #define INIT_EVENTFD_READ_WRITE
  6429. #endif
  6430. #if SANITIZER_INTERCEPT_STAT
  6431. INTERCEPTOR(int, stat, const char *path, void *buf) {
  6432. void *ctx;
  6433. COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
  6434. if (common_flags()->intercept_stat)
  6435. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6436. int res = REAL(stat)(path, buf);
  6437. if (!res)
  6438. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
  6439. return res;
  6440. }
  6441. #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
  6442. #else
  6443. #define INIT_STAT
  6444. #endif
  6445. #if SANITIZER_INTERCEPT_STAT64
  6446. INTERCEPTOR(int, stat64, const char *path, void *buf) {
  6447. void *ctx;
  6448. COMMON_INTERCEPTOR_ENTER(ctx, stat64, path, buf);
  6449. if (common_flags()->intercept_stat)
  6450. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6451. int res = REAL(stat64)(path, buf);
  6452. if (!res)
  6453. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
  6454. return res;
  6455. }
  6456. #define INIT_STAT64 COMMON_INTERCEPT_FUNCTION(stat64)
  6457. #else
  6458. #define INIT_STAT64
  6459. #endif
  6460. #if SANITIZER_INTERCEPT_LSTAT
  6461. INTERCEPTOR(int, lstat, const char *path, void *buf) {
  6462. void *ctx;
  6463. COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
  6464. if (common_flags()->intercept_stat)
  6465. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6466. int res = REAL(lstat)(path, buf);
  6467. if (!res)
  6468. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
  6469. return res;
  6470. }
  6471. #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
  6472. #else
  6473. #define INIT_LSTAT
  6474. #endif
  6475. #if SANITIZER_INTERCEPT_STAT64
  6476. INTERCEPTOR(int, lstat64, const char *path, void *buf) {
  6477. void *ctx;
  6478. COMMON_INTERCEPTOR_ENTER(ctx, lstat64, path, buf);
  6479. if (common_flags()->intercept_stat)
  6480. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6481. int res = REAL(lstat64)(path, buf);
  6482. if (!res)
  6483. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
  6484. return res;
  6485. }
  6486. #define INIT_LSTAT64 COMMON_INTERCEPT_FUNCTION(lstat64)
  6487. #else
  6488. #define INIT_LSTAT64
  6489. #endif
  6490. #if SANITIZER_INTERCEPT___XSTAT
  6491. INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
  6492. void *ctx;
  6493. COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
  6494. if (common_flags()->intercept_stat)
  6495. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6496. int res = REAL(__xstat)(version, path, buf);
  6497. if (!res)
  6498. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
  6499. return res;
  6500. }
  6501. #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
  6502. #else
  6503. #define INIT___XSTAT
  6504. #endif
  6505. #if SANITIZER_INTERCEPT___XSTAT64
  6506. INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
  6507. void *ctx;
  6508. COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
  6509. if (common_flags()->intercept_stat)
  6510. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6511. int res = REAL(__xstat64)(version, path, buf);
  6512. if (!res)
  6513. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
  6514. return res;
  6515. }
  6516. #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
  6517. #else
  6518. #define INIT___XSTAT64
  6519. #endif
  6520. #if SANITIZER_INTERCEPT___LXSTAT
  6521. INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
  6522. void *ctx;
  6523. COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
  6524. if (common_flags()->intercept_stat)
  6525. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6526. int res = REAL(__lxstat)(version, path, buf);
  6527. if (!res)
  6528. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
  6529. return res;
  6530. }
  6531. #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
  6532. #else
  6533. #define INIT___LXSTAT
  6534. #endif
  6535. #if SANITIZER_INTERCEPT___LXSTAT64
  6536. INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
  6537. void *ctx;
  6538. COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
  6539. if (common_flags()->intercept_stat)
  6540. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6541. int res = REAL(__lxstat64)(version, path, buf);
  6542. if (!res)
  6543. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
  6544. return res;
  6545. }
  6546. #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
  6547. #else
  6548. #define INIT___LXSTAT64
  6549. #endif
  6550. // FIXME: add other *stat interceptor
  6551. #if SANITIZER_INTERCEPT_UTMP
  6552. INTERCEPTOR(void *, getutent, int dummy) {
  6553. void *ctx;
  6554. COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
  6555. void *res = REAL(getutent)(dummy);
  6556. if (res)
  6557. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
  6558. return res;
  6559. }
  6560. INTERCEPTOR(void *, getutid, void *ut) {
  6561. void *ctx;
  6562. COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
  6563. void *res = REAL(getutid)(ut);
  6564. if (res)
  6565. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
  6566. return res;
  6567. }
  6568. INTERCEPTOR(void *, getutline, void *ut) {
  6569. void *ctx;
  6570. COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
  6571. void *res = REAL(getutline)(ut);
  6572. if (res)
  6573. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
  6574. return res;
  6575. }
  6576. #define INIT_UTMP \
  6577. COMMON_INTERCEPT_FUNCTION(getutent); \
  6578. COMMON_INTERCEPT_FUNCTION(getutid); \
  6579. COMMON_INTERCEPT_FUNCTION(getutline);
  6580. #else
  6581. #define INIT_UTMP
  6582. #endif
  6583. #if SANITIZER_INTERCEPT_UTMPX
  6584. INTERCEPTOR(void *, getutxent, int dummy) {
  6585. void *ctx;
  6586. COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
  6587. void *res = REAL(getutxent)(dummy);
  6588. if (res)
  6589. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
  6590. return res;
  6591. }
  6592. INTERCEPTOR(void *, getutxid, void *ut) {
  6593. void *ctx;
  6594. COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
  6595. void *res = REAL(getutxid)(ut);
  6596. if (res)
  6597. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
  6598. return res;
  6599. }
  6600. INTERCEPTOR(void *, getutxline, void *ut) {
  6601. void *ctx;
  6602. COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
  6603. void *res = REAL(getutxline)(ut);
  6604. if (res)
  6605. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
  6606. return res;
  6607. }
  6608. INTERCEPTOR(void *, pututxline, const void *ut) {
  6609. void *ctx;
  6610. COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
  6611. if (ut)
  6612. COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
  6613. void *res = REAL(pututxline)(ut);
  6614. if (res)
  6615. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
  6616. return res;
  6617. }
  6618. #define INIT_UTMPX \
  6619. COMMON_INTERCEPT_FUNCTION(getutxent); \
  6620. COMMON_INTERCEPT_FUNCTION(getutxid); \
  6621. COMMON_INTERCEPT_FUNCTION(getutxline); \
  6622. COMMON_INTERCEPT_FUNCTION(pututxline);
  6623. #else
  6624. #define INIT_UTMPX
  6625. #endif
  6626. #if SANITIZER_INTERCEPT_GETLOADAVG
  6627. INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
  6628. void *ctx;
  6629. COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
  6630. int res = REAL(getloadavg)(loadavg, nelem);
  6631. if (res > 0)
  6632. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
  6633. return res;
  6634. }
  6635. #define INIT_GETLOADAVG \
  6636. COMMON_INTERCEPT_FUNCTION(getloadavg);
  6637. #else
  6638. #define INIT_GETLOADAVG
  6639. #endif
  6640. #if SANITIZER_INTERCEPT_MCHECK_MPROBE
  6641. INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
  6642. return 0;
  6643. }
  6644. INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
  6645. return 0;
  6646. }
  6647. INTERCEPTOR(int, mprobe, void *ptr) {
  6648. return 0;
  6649. }
  6650. #endif
  6651. INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
  6652. void *ctx;
  6653. COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
  6654. SIZE_T res = REAL(wcslen)(s);
  6655. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
  6656. return res;
  6657. }
  6658. INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
  6659. void *ctx;
  6660. COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
  6661. SIZE_T res = REAL(wcsnlen)(s, n);
  6662. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
  6663. return res;
  6664. }
  6665. #define INIT_WCSLEN \
  6666. COMMON_INTERCEPT_FUNCTION(wcslen); \
  6667. COMMON_INTERCEPT_FUNCTION(wcsnlen);
  6668. #if SANITIZER_INTERCEPT_WCSCAT
  6669. INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
  6670. void *ctx;
  6671. COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
  6672. SIZE_T src_size = internal_wcslen(src);
  6673. SIZE_T dst_size = internal_wcslen(dst);
  6674. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
  6675. COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
  6676. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
  6677. (src_size + 1) * sizeof(wchar_t));
  6678. return REAL(wcscat)(dst, src);
  6679. }
  6680. INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
  6681. void *ctx;
  6682. COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
  6683. SIZE_T src_size = internal_wcsnlen(src, n);
  6684. SIZE_T dst_size = internal_wcslen(dst);
  6685. COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
  6686. Min(src_size + 1, n) * sizeof(wchar_t));
  6687. COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
  6688. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
  6689. (src_size + 1) * sizeof(wchar_t));
  6690. return REAL(wcsncat)(dst, src, n);
  6691. }
  6692. #define INIT_WCSCAT \
  6693. COMMON_INTERCEPT_FUNCTION(wcscat); \
  6694. COMMON_INTERCEPT_FUNCTION(wcsncat);
  6695. #else
  6696. #define INIT_WCSCAT
  6697. #endif
  6698. #if SANITIZER_INTERCEPT_WCSDUP
  6699. INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {
  6700. void *ctx;
  6701. COMMON_INTERCEPTOR_ENTER(ctx, wcsdup, s);
  6702. SIZE_T len = internal_wcslen(s);
  6703. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (len + 1));
  6704. wchar_t *result = REAL(wcsdup)(s);
  6705. if (result)
  6706. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(wchar_t) * (len + 1));
  6707. return result;
  6708. }
  6709. #define INIT_WCSDUP COMMON_INTERCEPT_FUNCTION(wcsdup);
  6710. #else
  6711. #define INIT_WCSDUP
  6712. #endif
  6713. #if SANITIZER_INTERCEPT_STRXFRM
  6714. static SIZE_T RealStrLen(const char *str) { return internal_strlen(str); }
  6715. static SIZE_T RealStrLen(const wchar_t *str) { return internal_wcslen(str); }
  6716. #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
  6717. { \
  6718. void *ctx; \
  6719. COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
  6720. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
  6721. sizeof(*src) * (RealStrLen(src) + 1)); \
  6722. SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
  6723. if (res < len) \
  6724. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
  6725. return res; \
  6726. }
  6727. INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
  6728. STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
  6729. }
  6730. INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
  6731. void *locale) {
  6732. STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
  6733. }
  6734. #define INIT_STRXFRM \
  6735. COMMON_INTERCEPT_FUNCTION(strxfrm); \
  6736. COMMON_INTERCEPT_FUNCTION(strxfrm_l);
  6737. #else
  6738. #define INIT_STRXFRM
  6739. #endif
  6740. #if SANITIZER_INTERCEPT___STRXFRM_L
  6741. INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
  6742. void *locale) {
  6743. STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
  6744. }
  6745. #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
  6746. #else
  6747. #define INIT___STRXFRM_L
  6748. #endif
  6749. #if SANITIZER_INTERCEPT_WCSXFRM
  6750. INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
  6751. STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
  6752. }
  6753. INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
  6754. void *locale) {
  6755. STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
  6756. }
  6757. #define INIT_WCSXFRM \
  6758. COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
  6759. COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
  6760. #else
  6761. #define INIT_WCSXFRM
  6762. #endif
  6763. #if SANITIZER_INTERCEPT___WCSXFRM_L
  6764. INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
  6765. void *locale) {
  6766. STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
  6767. }
  6768. #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
  6769. #else
  6770. #define INIT___WCSXFRM_L
  6771. #endif
  6772. #if SANITIZER_INTERCEPT_ACCT
  6773. INTERCEPTOR(int, acct, const char *file) {
  6774. void *ctx;
  6775. COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
  6776. if (file)
  6777. COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
  6778. return REAL(acct)(file);
  6779. }
  6780. #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
  6781. #else
  6782. #define INIT_ACCT
  6783. #endif
  6784. #if SANITIZER_INTERCEPT_USER_FROM_UID
  6785. INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
  6786. void *ctx;
  6787. const char *user;
  6788. COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
  6789. user = REAL(user_from_uid)(uid, nouser);
  6790. if (user)
  6791. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, internal_strlen(user) + 1);
  6792. return user;
  6793. }
  6794. #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
  6795. #else
  6796. #define INIT_USER_FROM_UID
  6797. #endif
  6798. #if SANITIZER_INTERCEPT_UID_FROM_USER
  6799. INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
  6800. void *ctx;
  6801. int res;
  6802. COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
  6803. if (name)
  6804. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  6805. res = REAL(uid_from_user)(name, uid);
  6806. if (uid)
  6807. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
  6808. return res;
  6809. }
  6810. #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
  6811. #else
  6812. #define INIT_UID_FROM_USER
  6813. #endif
  6814. #if SANITIZER_INTERCEPT_GROUP_FROM_GID
  6815. INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
  6816. void *ctx;
  6817. const char *group;
  6818. COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
  6819. group = REAL(group_from_gid)(gid, nogroup);
  6820. if (group)
  6821. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, internal_strlen(group) + 1);
  6822. return group;
  6823. }
  6824. #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
  6825. #else
  6826. #define INIT_GROUP_FROM_GID
  6827. #endif
  6828. #if SANITIZER_INTERCEPT_GID_FROM_GROUP
  6829. INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
  6830. void *ctx;
  6831. int res;
  6832. COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
  6833. if (group)
  6834. COMMON_INTERCEPTOR_READ_RANGE(ctx, group, internal_strlen(group) + 1);
  6835. res = REAL(gid_from_group)(group, gid);
  6836. if (gid)
  6837. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
  6838. return res;
  6839. }
  6840. #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
  6841. #else
  6842. #define INIT_GID_FROM_GROUP
  6843. #endif
  6844. #if SANITIZER_INTERCEPT_ACCESS
  6845. INTERCEPTOR(int, access, const char *path, int mode) {
  6846. void *ctx;
  6847. COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
  6848. if (path)
  6849. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  6850. return REAL(access)(path, mode);
  6851. }
  6852. #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
  6853. #else
  6854. #define INIT_ACCESS
  6855. #endif
  6856. #if SANITIZER_INTERCEPT_FACCESSAT
  6857. INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
  6858. void *ctx;
  6859. COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
  6860. if (path)
  6861. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  6862. return REAL(faccessat)(fd, path, mode, flags);
  6863. }
  6864. #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
  6865. #else
  6866. #define INIT_FACCESSAT
  6867. #endif
  6868. #if SANITIZER_INTERCEPT_GETGROUPLIST
  6869. INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
  6870. int *ngroups) {
  6871. void *ctx;
  6872. int res;
  6873. COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
  6874. if (name)
  6875. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  6876. if (ngroups)
  6877. COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
  6878. res = REAL(getgrouplist)(name, basegid, groups, ngroups);
  6879. if (!res && groups && ngroups) {
  6880. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
  6881. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
  6882. }
  6883. return res;
  6884. }
  6885. #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
  6886. #else
  6887. #define INIT_GETGROUPLIST
  6888. #endif
  6889. #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
  6890. INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
  6891. int maxgrp, int *ngroups) {
  6892. void *ctx;
  6893. int res;
  6894. COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
  6895. maxgrp, ngroups);
  6896. if (name)
  6897. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  6898. res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
  6899. if (!res && groups && ngroups) {
  6900. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
  6901. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
  6902. }
  6903. return res;
  6904. }
  6905. #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
  6906. #else
  6907. #define INIT_GETGROUPMEMBERSHIP
  6908. #endif
  6909. #if SANITIZER_INTERCEPT_READLINK
  6910. INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
  6911. void* ctx;
  6912. COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
  6913. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  6914. SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
  6915. if (res > 0)
  6916. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
  6917. return res;
  6918. }
  6919. #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
  6920. #else
  6921. #define INIT_READLINK
  6922. #endif
  6923. #if SANITIZER_INTERCEPT_READLINKAT
  6924. INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
  6925. SIZE_T bufsiz) {
  6926. void* ctx;
  6927. COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
  6928. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  6929. SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
  6930. if (res > 0)
  6931. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
  6932. return res;
  6933. }
  6934. #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
  6935. #else
  6936. #define INIT_READLINKAT
  6937. #endif
  6938. #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
  6939. INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
  6940. struct file_handle *handle, int *mount_id, int flags) {
  6941. void* ctx;
  6942. COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
  6943. mount_id, flags);
  6944. COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, internal_strlen(pathname) + 1);
  6945. __sanitizer_file_handle *sanitizer_handle =
  6946. reinterpret_cast<__sanitizer_file_handle*>(handle);
  6947. COMMON_INTERCEPTOR_READ_RANGE(
  6948. ctx, &sanitizer_handle->handle_bytes,
  6949. sizeof(sanitizer_handle->handle_bytes));
  6950. int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
  6951. if (!res) {
  6952. COMMON_INTERCEPTOR_WRITE_RANGE(
  6953. ctx, &sanitizer_handle->handle_bytes,
  6954. sizeof(sanitizer_handle->handle_bytes));
  6955. COMMON_INTERCEPTOR_WRITE_RANGE(
  6956. ctx, &sanitizer_handle->handle_type,
  6957. sizeof(sanitizer_handle->handle_type));
  6958. COMMON_INTERCEPTOR_WRITE_RANGE(
  6959. ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
  6960. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
  6961. }
  6962. return res;
  6963. }
  6964. #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
  6965. #else
  6966. #define INIT_NAME_TO_HANDLE_AT
  6967. #endif
  6968. #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
  6969. INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
  6970. int flags) {
  6971. void* ctx;
  6972. COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
  6973. __sanitizer_file_handle *sanitizer_handle =
  6974. reinterpret_cast<__sanitizer_file_handle*>(handle);
  6975. COMMON_INTERCEPTOR_READ_RANGE(
  6976. ctx, &sanitizer_handle->handle_bytes,
  6977. sizeof(sanitizer_handle->handle_bytes));
  6978. COMMON_INTERCEPTOR_READ_RANGE(
  6979. ctx, &sanitizer_handle->handle_type,
  6980. sizeof(sanitizer_handle->handle_type));
  6981. COMMON_INTERCEPTOR_READ_RANGE(
  6982. ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
  6983. return REAL(open_by_handle_at)(mount_fd, handle, flags);
  6984. }
  6985. #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
  6986. #else
  6987. #define INIT_OPEN_BY_HANDLE_AT
  6988. #endif
  6989. #if SANITIZER_INTERCEPT_STRLCPY
  6990. INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
  6991. void *ctx;
  6992. SIZE_T res;
  6993. COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
  6994. if (src) {
  6995. // Keep strnlen as macro argument, as macro may ignore it.
  6996. COMMON_INTERCEPTOR_READ_STRING(
  6997. ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
  6998. }
  6999. res = REAL(strlcpy)(dst, src, size);
  7000. COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, internal_strlen(dst) + 1);
  7001. return res;
  7002. }
  7003. INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
  7004. void *ctx;
  7005. SIZE_T len = 0;
  7006. COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
  7007. // src is checked in the strlcpy() interceptor
  7008. if (dst) {
  7009. len = internal_strnlen(dst, size);
  7010. COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
  7011. }
  7012. // Reuse the rest of the code in the strlcpy() interceptor
  7013. return WRAP(strlcpy)(dst + len, src, size - len) + len;
  7014. }
  7015. #define INIT_STRLCPY \
  7016. COMMON_INTERCEPT_FUNCTION(strlcpy); \
  7017. COMMON_INTERCEPT_FUNCTION(strlcat);
  7018. #else
  7019. #define INIT_STRLCPY
  7020. #endif
  7021. #if SANITIZER_INTERCEPT_MMAP
  7022. INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
  7023. OFF_T off) {
  7024. void *ctx;
  7025. if (common_flags()->detect_write_exec)
  7026. ReportMmapWriteExec(prot, flags);
  7027. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  7028. return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
  7029. COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
  7030. COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
  7031. }
  7032. INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
  7033. void *ctx;
  7034. if (common_flags()->detect_write_exec)
  7035. ReportMmapWriteExec(prot, 0);
  7036. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  7037. return (int)internal_mprotect(addr, sz, prot);
  7038. COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
  7039. MprotectMallocZones(addr, prot);
  7040. return REAL(mprotect)(addr, sz, prot);
  7041. }
  7042. #define INIT_MMAP \
  7043. COMMON_INTERCEPT_FUNCTION(mmap); \
  7044. COMMON_INTERCEPT_FUNCTION(mprotect);
  7045. #else
  7046. #define INIT_MMAP
  7047. #endif
  7048. #if SANITIZER_INTERCEPT_MMAP64
  7049. INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
  7050. OFF64_T off) {
  7051. void *ctx;
  7052. if (common_flags()->detect_write_exec)
  7053. ReportMmapWriteExec(prot, flags);
  7054. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  7055. return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
  7056. COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
  7057. COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
  7058. }
  7059. #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
  7060. #else
  7061. #define INIT_MMAP64
  7062. #endif
  7063. #if SANITIZER_INTERCEPT_DEVNAME
  7064. INTERCEPTOR(char *, devname, u64 dev, u32 type) {
  7065. void *ctx;
  7066. char *name;
  7067. COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
  7068. name = REAL(devname)(dev, type);
  7069. if (name)
  7070. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
  7071. return name;
  7072. }
  7073. #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
  7074. #else
  7075. #define INIT_DEVNAME
  7076. #endif
  7077. #if SANITIZER_INTERCEPT_DEVNAME_R
  7078. #if SANITIZER_NETBSD
  7079. #define DEVNAME_R_RETTYPE int
  7080. #define DEVNAME_R_SUCCESS(x) (!(x))
  7081. #else
  7082. #define DEVNAME_R_RETTYPE char*
  7083. #define DEVNAME_R_SUCCESS(x) (x)
  7084. #endif
  7085. INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
  7086. uptr len) {
  7087. void *ctx;
  7088. COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
  7089. DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
  7090. if (DEVNAME_R_SUCCESS(res))
  7091. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, internal_strlen(path) + 1);
  7092. return res;
  7093. }
  7094. #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
  7095. #else
  7096. #define INIT_DEVNAME_R
  7097. #endif
  7098. #if SANITIZER_INTERCEPT_FGETLN
  7099. INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
  7100. void *ctx;
  7101. COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
  7102. char *str = REAL(fgetln)(stream, len);
  7103. if (str && len) {
  7104. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
  7105. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
  7106. }
  7107. return str;
  7108. }
  7109. #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
  7110. #else
  7111. #define INIT_FGETLN
  7112. #endif
  7113. #if SANITIZER_INTERCEPT_STRMODE
  7114. INTERCEPTOR(void, strmode, u32 mode, char *bp) {
  7115. void *ctx;
  7116. COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
  7117. REAL(strmode)(mode, bp);
  7118. if (bp)
  7119. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, internal_strlen(bp) + 1);
  7120. }
  7121. #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
  7122. #else
  7123. #define INIT_STRMODE
  7124. #endif
  7125. #if SANITIZER_INTERCEPT_TTYENT
  7126. INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
  7127. void *ctx;
  7128. COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
  7129. struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
  7130. if (ttyent)
  7131. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
  7132. return ttyent;
  7133. }
  7134. INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
  7135. void *ctx;
  7136. COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
  7137. if (name)
  7138. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  7139. struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
  7140. if (ttyent)
  7141. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
  7142. return ttyent;
  7143. }
  7144. #define INIT_TTYENT \
  7145. COMMON_INTERCEPT_FUNCTION(getttyent); \
  7146. COMMON_INTERCEPT_FUNCTION(getttynam);
  7147. #else
  7148. #define INIT_TTYENT
  7149. #endif
  7150. #if SANITIZER_INTERCEPT_TTYENTPATH
  7151. INTERCEPTOR(int, setttyentpath, char *path) {
  7152. void *ctx;
  7153. COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
  7154. if (path)
  7155. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  7156. return REAL(setttyentpath)(path);
  7157. }
  7158. #define INIT_TTYENTPATH COMMON_INTERCEPT_FUNCTION(setttyentpath);
  7159. #else
  7160. #define INIT_TTYENTPATH
  7161. #endif
  7162. #if SANITIZER_INTERCEPT_PROTOENT
  7163. static void write_protoent(void *ctx, struct __sanitizer_protoent *p) {
  7164. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
  7165. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, internal_strlen(p->p_name) + 1);
  7166. SIZE_T pp_size = 1; // One handles the trailing \0
  7167. for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
  7168. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, internal_strlen(*pp) + 1);
  7169. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
  7170. pp_size * sizeof(char **));
  7171. }
  7172. INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
  7173. void *ctx;
  7174. COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
  7175. struct __sanitizer_protoent *p = REAL(getprotoent)();
  7176. if (p)
  7177. write_protoent(ctx, p);
  7178. return p;
  7179. }
  7180. INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
  7181. void *ctx;
  7182. COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
  7183. if (name)
  7184. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  7185. struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
  7186. if (p)
  7187. write_protoent(ctx, p);
  7188. return p;
  7189. }
  7190. INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
  7191. void *ctx;
  7192. COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
  7193. struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
  7194. if (p)
  7195. write_protoent(ctx, p);
  7196. return p;
  7197. }
  7198. #define INIT_PROTOENT \
  7199. COMMON_INTERCEPT_FUNCTION(getprotoent); \
  7200. COMMON_INTERCEPT_FUNCTION(getprotobyname); \
  7201. COMMON_INTERCEPT_FUNCTION(getprotobynumber)
  7202. #else
  7203. #define INIT_PROTOENT
  7204. #endif
  7205. #if SANITIZER_INTERCEPT_PROTOENT_R
  7206. INTERCEPTOR(int, getprotoent_r, struct __sanitizer_protoent *result_buf,
  7207. char *buf, SIZE_T buflen, struct __sanitizer_protoent **result) {
  7208. void *ctx;
  7209. COMMON_INTERCEPTOR_ENTER(ctx, getprotoent_r, result_buf, buf, buflen,
  7210. result);
  7211. int res = REAL(getprotoent_r)(result_buf, buf, buflen, result);
  7212. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
  7213. if (!res && *result)
  7214. write_protoent(ctx, *result);
  7215. return res;
  7216. }
  7217. INTERCEPTOR(int, getprotobyname_r, const char *name,
  7218. struct __sanitizer_protoent *result_buf, char *buf, SIZE_T buflen,
  7219. struct __sanitizer_protoent **result) {
  7220. void *ctx;
  7221. COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname_r, name, result_buf, buf,
  7222. buflen, result);
  7223. if (name)
  7224. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  7225. int res = REAL(getprotobyname_r)(name, result_buf, buf, buflen, result);
  7226. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
  7227. if (!res && *result)
  7228. write_protoent(ctx, *result);
  7229. return res;
  7230. }
  7231. INTERCEPTOR(int, getprotobynumber_r, int num,
  7232. struct __sanitizer_protoent *result_buf, char *buf,
  7233. SIZE_T buflen, struct __sanitizer_protoent **result) {
  7234. void *ctx;
  7235. COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber_r, num, result_buf, buf,
  7236. buflen, result);
  7237. int res = REAL(getprotobynumber_r)(num, result_buf, buf, buflen, result);
  7238. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
  7239. if (!res && *result)
  7240. write_protoent(ctx, *result);
  7241. return res;
  7242. }
  7243. #define INIT_PROTOENT_R \
  7244. COMMON_INTERCEPT_FUNCTION(getprotoent_r); \
  7245. COMMON_INTERCEPT_FUNCTION(getprotobyname_r); \
  7246. COMMON_INTERCEPT_FUNCTION(getprotobynumber_r);
  7247. #else
  7248. #define INIT_PROTOENT_R
  7249. #endif
  7250. #if SANITIZER_INTERCEPT_NETENT
  7251. INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
  7252. void *ctx;
  7253. COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
  7254. struct __sanitizer_netent *n = REAL(getnetent)();
  7255. if (n) {
  7256. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
  7257. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
  7258. SIZE_T nn_size = 1; // One handles the trailing \0
  7259. for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
  7260. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
  7261. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
  7262. nn_size * sizeof(char **));
  7263. }
  7264. return n;
  7265. }
  7266. INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
  7267. void *ctx;
  7268. COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
  7269. if (name)
  7270. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  7271. struct __sanitizer_netent *n = REAL(getnetbyname)(name);
  7272. if (n) {
  7273. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
  7274. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
  7275. SIZE_T nn_size = 1; // One handles the trailing \0
  7276. for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
  7277. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
  7278. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
  7279. nn_size * sizeof(char **));
  7280. }
  7281. return n;
  7282. }
  7283. INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
  7284. void *ctx;
  7285. COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
  7286. struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
  7287. if (n) {
  7288. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
  7289. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
  7290. SIZE_T nn_size = 1; // One handles the trailing \0
  7291. for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
  7292. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
  7293. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
  7294. nn_size * sizeof(char **));
  7295. }
  7296. return n;
  7297. }
  7298. #define INIT_NETENT \
  7299. COMMON_INTERCEPT_FUNCTION(getnetent); \
  7300. COMMON_INTERCEPT_FUNCTION(getnetbyname); \
  7301. COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
  7302. #else
  7303. #define INIT_NETENT
  7304. #endif
  7305. #if SANITIZER_INTERCEPT_GETMNTINFO
  7306. INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
  7307. void *ctx;
  7308. COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
  7309. int cnt = REAL(getmntinfo)(mntbufp, flags);
  7310. if (cnt > 0 && mntbufp) {
  7311. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
  7312. if (*mntbufp)
  7313. #if SANITIZER_NETBSD
  7314. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
  7315. #else
  7316. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
  7317. #endif
  7318. }
  7319. return cnt;
  7320. }
  7321. #define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
  7322. #else
  7323. #define INIT_GETMNTINFO
  7324. #endif
  7325. #if SANITIZER_INTERCEPT_MI_VECTOR_HASH
  7326. INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
  7327. u32 hashes[3]) {
  7328. void *ctx;
  7329. COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
  7330. if (key)
  7331. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
  7332. REAL(mi_vector_hash)(key, len, seed, hashes);
  7333. if (hashes)
  7334. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
  7335. }
  7336. #define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
  7337. #else
  7338. #define INIT_MI_VECTOR_HASH
  7339. #endif
  7340. #if SANITIZER_INTERCEPT_SETVBUF
  7341. INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
  7342. SIZE_T size) {
  7343. void *ctx;
  7344. COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
  7345. int ret = REAL(setvbuf)(stream, buf, mode, size);
  7346. if (buf)
  7347. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
  7348. if (stream)
  7349. unpoison_file(stream);
  7350. return ret;
  7351. }
  7352. INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
  7353. void *ctx;
  7354. COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
  7355. REAL(setbuf)(stream, buf);
  7356. if (buf) {
  7357. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
  7358. }
  7359. if (stream)
  7360. unpoison_file(stream);
  7361. }
  7362. INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, SIZE_T size) {
  7363. void *ctx;
  7364. COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, size);
  7365. REAL(setbuffer)(stream, buf, size);
  7366. if (buf) {
  7367. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
  7368. }
  7369. if (stream)
  7370. unpoison_file(stream);
  7371. }
  7372. INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
  7373. void *ctx;
  7374. COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
  7375. REAL(setlinebuf)(stream);
  7376. if (stream)
  7377. unpoison_file(stream);
  7378. }
  7379. #define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
  7380. COMMON_INTERCEPT_FUNCTION(setbuf); \
  7381. COMMON_INTERCEPT_FUNCTION(setbuffer); \
  7382. COMMON_INTERCEPT_FUNCTION(setlinebuf)
  7383. #else
  7384. #define INIT_SETVBUF
  7385. #endif
  7386. #if SANITIZER_INTERCEPT_GETVFSSTAT
  7387. INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
  7388. void *ctx;
  7389. COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
  7390. int ret = REAL(getvfsstat)(buf, bufsize, flags);
  7391. if (buf && ret > 0)
  7392. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
  7393. return ret;
  7394. }
  7395. #define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
  7396. #else
  7397. #define INIT_GETVFSSTAT
  7398. #endif
  7399. #if SANITIZER_INTERCEPT_REGEX
  7400. INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
  7401. void *ctx;
  7402. COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
  7403. if (pattern)
  7404. COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, internal_strlen(pattern) + 1);
  7405. int res = REAL(regcomp)(preg, pattern, cflags);
  7406. if (preg)
  7407. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
  7408. return res;
  7409. }
  7410. INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
  7411. struct __sanitizer_regmatch *pmatch[], int eflags) {
  7412. void *ctx;
  7413. COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
  7414. if (preg)
  7415. COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
  7416. if (string)
  7417. COMMON_INTERCEPTOR_READ_RANGE(ctx, string, internal_strlen(string) + 1);
  7418. int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
  7419. if (!res && pmatch)
  7420. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
  7421. return res;
  7422. }
  7423. INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
  7424. SIZE_T errbuf_size) {
  7425. void *ctx;
  7426. COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
  7427. if (preg)
  7428. COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
  7429. SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
  7430. if (errbuf)
  7431. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, internal_strlen(errbuf) + 1);
  7432. return res;
  7433. }
  7434. INTERCEPTOR(void, regfree, const void *preg) {
  7435. void *ctx;
  7436. COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
  7437. if (preg)
  7438. COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
  7439. REAL(regfree)(preg);
  7440. }
  7441. #define INIT_REGEX \
  7442. COMMON_INTERCEPT_FUNCTION(regcomp); \
  7443. COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(regexec, "GLIBC_2.3.4"); \
  7444. COMMON_INTERCEPT_FUNCTION(regerror); \
  7445. COMMON_INTERCEPT_FUNCTION(regfree);
  7446. #else
  7447. #define INIT_REGEX
  7448. #endif
  7449. #if SANITIZER_INTERCEPT_REGEXSUB
  7450. INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
  7451. const struct __sanitizer_regmatch *rm, const char *str) {
  7452. void *ctx;
  7453. COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
  7454. if (sub)
  7455. COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
  7456. // The implementation demands and hardcodes 10 elements
  7457. if (rm)
  7458. COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
  7459. if (str)
  7460. COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
  7461. SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
  7462. if (res > 0 && buf)
  7463. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
  7464. return res;
  7465. }
  7466. INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
  7467. const struct __sanitizer_regmatch *rm, const char *sstr) {
  7468. void *ctx;
  7469. COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
  7470. if (sub)
  7471. COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
  7472. // Hardcode 10 elements as this is hardcoded size
  7473. if (rm)
  7474. COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
  7475. if (sstr)
  7476. COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, internal_strlen(sstr) + 1);
  7477. SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
  7478. if (res > 0 && buf) {
  7479. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
  7480. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, internal_strlen(*buf) + 1);
  7481. }
  7482. return res;
  7483. }
  7484. #define INIT_REGEXSUB \
  7485. COMMON_INTERCEPT_FUNCTION(regnsub); \
  7486. COMMON_INTERCEPT_FUNCTION(regasub);
  7487. #else
  7488. #define INIT_REGEXSUB
  7489. #endif
  7490. #if SANITIZER_INTERCEPT_FTS
  7491. INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
  7492. int (*compar)(void **, void **)) {
  7493. void *ctx;
  7494. COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
  7495. if (path_argv) {
  7496. for (char *const *pa = path_argv; ; ++pa) {
  7497. COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
  7498. if (!*pa)
  7499. break;
  7500. COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
  7501. }
  7502. }
  7503. // TODO(kamil): handle compar callback
  7504. void *fts = REAL(fts_open)(path_argv, options, compar);
  7505. if (fts)
  7506. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
  7507. return fts;
  7508. }
  7509. INTERCEPTOR(void *, fts_read, void *ftsp) {
  7510. void *ctx;
  7511. COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
  7512. if (ftsp)
  7513. COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  7514. void *ftsent = REAL(fts_read)(ftsp);
  7515. if (ftsent)
  7516. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
  7517. return ftsent;
  7518. }
  7519. INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
  7520. void *ctx;
  7521. COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
  7522. if (ftsp)
  7523. COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  7524. void *ftsent = REAL(fts_children)(ftsp, options);
  7525. if (ftsent)
  7526. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
  7527. return ftsent;
  7528. }
  7529. INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
  7530. void *ctx;
  7531. COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
  7532. if (ftsp)
  7533. COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  7534. if (f)
  7535. COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
  7536. return REAL(fts_set)(ftsp, f, options);
  7537. }
  7538. INTERCEPTOR(int, fts_close, void *ftsp) {
  7539. void *ctx;
  7540. COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
  7541. if (ftsp)
  7542. COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  7543. return REAL(fts_close)(ftsp);
  7544. }
  7545. #define INIT_FTS \
  7546. COMMON_INTERCEPT_FUNCTION(fts_open); \
  7547. COMMON_INTERCEPT_FUNCTION(fts_read); \
  7548. COMMON_INTERCEPT_FUNCTION(fts_children); \
  7549. COMMON_INTERCEPT_FUNCTION(fts_set); \
  7550. COMMON_INTERCEPT_FUNCTION(fts_close);
  7551. #else
  7552. #define INIT_FTS
  7553. #endif
  7554. #if SANITIZER_INTERCEPT_SYSCTL
  7555. INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
  7556. SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
  7557. void *ctx;
  7558. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  7559. return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
  7560. COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
  7561. newlen);
  7562. if (name)
  7563. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
  7564. if (oldlenp)
  7565. COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
  7566. if (newp && newlen)
  7567. COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
  7568. int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
  7569. if (!res) {
  7570. if (oldlenp) {
  7571. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
  7572. if (oldp)
  7573. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
  7574. }
  7575. }
  7576. return res;
  7577. }
  7578. INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
  7579. void *newp, SIZE_T newlen) {
  7580. void *ctx;
  7581. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  7582. return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
  7583. COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
  7584. newlen);
  7585. if (sname)
  7586. COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  7587. if (oldlenp)
  7588. COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
  7589. if (newp && newlen)
  7590. COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
  7591. int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
  7592. if (!res) {
  7593. if (oldlenp) {
  7594. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
  7595. if (oldp)
  7596. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
  7597. }
  7598. }
  7599. return res;
  7600. }
  7601. INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
  7602. SIZE_T *namelenp) {
  7603. void *ctx;
  7604. COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
  7605. if (sname)
  7606. COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  7607. if (namelenp)
  7608. COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
  7609. int res = REAL(sysctlnametomib)(sname, name, namelenp);
  7610. if (!res) {
  7611. if (namelenp) {
  7612. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
  7613. if (name)
  7614. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
  7615. }
  7616. }
  7617. return res;
  7618. }
  7619. #define INIT_SYSCTL \
  7620. COMMON_INTERCEPT_FUNCTION(sysctl); \
  7621. COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
  7622. COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
  7623. #else
  7624. #define INIT_SYSCTL
  7625. #endif
  7626. #if SANITIZER_INTERCEPT_ASYSCTL
  7627. INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
  7628. void *ctx;
  7629. COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
  7630. if (name)
  7631. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
  7632. void *res = REAL(asysctl)(name, namelen, len);
  7633. if (res && len) {
  7634. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
  7635. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
  7636. }
  7637. return res;
  7638. }
  7639. INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
  7640. void *ctx;
  7641. COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
  7642. if (sname)
  7643. COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  7644. void *res = REAL(asysctlbyname)(sname, len);
  7645. if (res && len) {
  7646. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
  7647. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
  7648. }
  7649. return res;
  7650. }
  7651. #define INIT_ASYSCTL \
  7652. COMMON_INTERCEPT_FUNCTION(asysctl); \
  7653. COMMON_INTERCEPT_FUNCTION(asysctlbyname);
  7654. #else
  7655. #define INIT_ASYSCTL
  7656. #endif
  7657. #if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
  7658. INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
  7659. unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
  7660. int v) {
  7661. void *ctx;
  7662. COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
  7663. csz, rnode, v);
  7664. if (sname)
  7665. COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  7666. if (namelenp)
  7667. COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
  7668. if (csz)
  7669. COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
  7670. // Skip rnode, it's rarely used and not trivial to sanitize
  7671. // It's also used mostly internally
  7672. int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
  7673. if (!res) {
  7674. if (namelenp) {
  7675. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
  7676. if (name)
  7677. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
  7678. }
  7679. if (csz) {
  7680. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
  7681. if (cname)
  7682. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
  7683. }
  7684. }
  7685. return res;
  7686. }
  7687. #define INIT_SYSCTLGETMIBINFO \
  7688. COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
  7689. #else
  7690. #define INIT_SYSCTLGETMIBINFO
  7691. #endif
  7692. #if SANITIZER_INTERCEPT_NL_LANGINFO
  7693. INTERCEPTOR(char *, nl_langinfo, long item) {
  7694. void *ctx;
  7695. COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
  7696. char *ret = REAL(nl_langinfo)(item);
  7697. if (ret)
  7698. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
  7699. return ret;
  7700. }
  7701. #define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
  7702. #else
  7703. #define INIT_NL_LANGINFO
  7704. #endif
  7705. #if SANITIZER_INTERCEPT_MODCTL
  7706. INTERCEPTOR(int, modctl, int operation, void *argp) {
  7707. void *ctx;
  7708. int ret;
  7709. COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
  7710. if (operation == modctl_load) {
  7711. if (argp) {
  7712. __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
  7713. COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
  7714. if (ml->ml_filename)
  7715. COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
  7716. internal_strlen(ml->ml_filename) + 1);
  7717. if (ml->ml_props)
  7718. COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
  7719. }
  7720. ret = REAL(modctl)(operation, argp);
  7721. } else if (operation == modctl_unload) {
  7722. if (argp) {
  7723. const char *name = (const char *)argp;
  7724. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  7725. }
  7726. ret = REAL(modctl)(operation, argp);
  7727. } else if (operation == modctl_stat) {
  7728. uptr iov_len;
  7729. struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
  7730. if (iov) {
  7731. COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
  7732. iov_len = iov->iov_len;
  7733. }
  7734. ret = REAL(modctl)(operation, argp);
  7735. if (iov)
  7736. COMMON_INTERCEPTOR_WRITE_RANGE(
  7737. ctx, iov->iov_base, Min(iov_len, iov->iov_len));
  7738. } else if (operation == modctl_exists) {
  7739. ret = REAL(modctl)(operation, argp);
  7740. } else {
  7741. ret = REAL(modctl)(operation, argp);
  7742. }
  7743. return ret;
  7744. }
  7745. #define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
  7746. #else
  7747. #define INIT_MODCTL
  7748. #endif
  7749. #if SANITIZER_INTERCEPT_STRTONUM
  7750. INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
  7751. long long maxval, const char **errstr) {
  7752. void *ctx;
  7753. COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
  7754. // TODO(kamil): Implement strtoll as a common inteceptor
  7755. char *real_endptr;
  7756. long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
  7757. StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
  7758. ret = REAL(strtonum)(nptr, minval, maxval, errstr);
  7759. if (errstr) {
  7760. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
  7761. if (*errstr)
  7762. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, internal_strlen(*errstr) + 1);
  7763. }
  7764. return ret;
  7765. }
  7766. #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
  7767. #else
  7768. #define INIT_STRTONUM
  7769. #endif
  7770. #if SANITIZER_INTERCEPT_FPARSELN
  7771. INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
  7772. SIZE_T *lineno, const char delim[3], int flags) {
  7773. void *ctx;
  7774. COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
  7775. if (lineno)
  7776. COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
  7777. if (delim)
  7778. COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
  7779. char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
  7780. if (ret) {
  7781. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
  7782. if (len)
  7783. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
  7784. if (lineno)
  7785. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
  7786. }
  7787. return ret;
  7788. }
  7789. #define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
  7790. #else
  7791. #define INIT_FPARSELN
  7792. #endif
  7793. #if SANITIZER_INTERCEPT_STATVFS1
  7794. INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
  7795. void *ctx;
  7796. COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
  7797. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  7798. int res = REAL(statvfs1)(path, buf, flags);
  7799. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
  7800. return res;
  7801. }
  7802. INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
  7803. void *ctx;
  7804. COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
  7805. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  7806. int res = REAL(fstatvfs1)(fd, buf, flags);
  7807. if (!res) {
  7808. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
  7809. if (fd >= 0)
  7810. COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  7811. }
  7812. return res;
  7813. }
  7814. #define INIT_STATVFS1 \
  7815. COMMON_INTERCEPT_FUNCTION(statvfs1); \
  7816. COMMON_INTERCEPT_FUNCTION(fstatvfs1);
  7817. #else
  7818. #define INIT_STATVFS1
  7819. #endif
  7820. #if SANITIZER_INTERCEPT_STRTOI
  7821. INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
  7822. INTMAX_T low, INTMAX_T high, int *rstatus) {
  7823. void *ctx;
  7824. COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
  7825. char *real_endptr;
  7826. INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
  7827. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  7828. if (rstatus)
  7829. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
  7830. return ret;
  7831. }
  7832. INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
  7833. UINTMAX_T low, UINTMAX_T high, int *rstatus) {
  7834. void *ctx;
  7835. COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
  7836. char *real_endptr;
  7837. UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
  7838. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  7839. if (rstatus)
  7840. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
  7841. return ret;
  7842. }
  7843. #define INIT_STRTOI \
  7844. COMMON_INTERCEPT_FUNCTION(strtoi); \
  7845. COMMON_INTERCEPT_FUNCTION(strtou)
  7846. #else
  7847. #define INIT_STRTOI
  7848. #endif
  7849. #if SANITIZER_INTERCEPT_CAPSICUM
  7850. #define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
  7851. { \
  7852. void *ctx; \
  7853. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
  7854. if (rights) \
  7855. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
  7856. __sanitizer_cap_rights_t *ret = \
  7857. REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
  7858. if (ret) \
  7859. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
  7860. return ret; \
  7861. }
  7862. #define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
  7863. { \
  7864. void *ctx; \
  7865. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
  7866. if (rights) \
  7867. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
  7868. __sanitizer_cap_rights_t *ret = \
  7869. REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
  7870. if (ret) \
  7871. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
  7872. return ret; \
  7873. }
  7874. #define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
  7875. { \
  7876. void *ctx; \
  7877. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
  7878. if (rights) \
  7879. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
  7880. __sanitizer_cap_rights_t *ret = \
  7881. REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
  7882. if (ret) \
  7883. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
  7884. return ret; \
  7885. }
  7886. #define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
  7887. { \
  7888. void *ctx; \
  7889. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
  7890. if (rights) \
  7891. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
  7892. return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
  7893. }
  7894. INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
  7895. __sanitizer_cap_rights_t *rights) {
  7896. CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
  7897. }
  7898. INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
  7899. __sanitizer_cap_rights_t *rights) {
  7900. CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
  7901. }
  7902. INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
  7903. __sanitizer_cap_rights_t *rights) {
  7904. CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
  7905. }
  7906. INTERCEPTOR(bool, cap_rights_is_set,
  7907. __sanitizer_cap_rights_t *rights) {
  7908. CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
  7909. }
  7910. INTERCEPTOR(int, cap_rights_limit, int fd,
  7911. const __sanitizer_cap_rights_t *rights) {
  7912. void *ctx;
  7913. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
  7914. if (rights)
  7915. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
  7916. return REAL(cap_rights_limit)(fd, rights);
  7917. }
  7918. INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
  7919. void *ctx;
  7920. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
  7921. int ret = REAL(cap_rights_get)(fd, rights);
  7922. if (!ret && rights)
  7923. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
  7924. return ret;
  7925. }
  7926. INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
  7927. void *ctx;
  7928. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
  7929. if (rights)
  7930. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
  7931. return REAL(cap_rights_is_valid(rights));
  7932. }
  7933. INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
  7934. __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
  7935. void *ctx;
  7936. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
  7937. if (src)
  7938. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  7939. __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
  7940. if (dst)
  7941. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
  7942. return ret;
  7943. }
  7944. INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
  7945. __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
  7946. void *ctx;
  7947. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
  7948. if (src)
  7949. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  7950. __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
  7951. if (dst)
  7952. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
  7953. return ret;
  7954. }
  7955. INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
  7956. const __sanitizer_cap_rights *little) {
  7957. void *ctx;
  7958. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
  7959. if (little)
  7960. COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
  7961. if (big)
  7962. COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
  7963. return REAL(cap_rights_contains)(big, little);
  7964. }
  7965. INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
  7966. void *ctx;
  7967. COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
  7968. if (cmds)
  7969. COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
  7970. return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
  7971. }
  7972. INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
  7973. void *ctx;
  7974. COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
  7975. int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
  7976. if (!ret && cmds)
  7977. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
  7978. return ret;
  7979. }
  7980. #define INIT_CAPSICUM \
  7981. COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
  7982. COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
  7983. COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
  7984. COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
  7985. COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
  7986. COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
  7987. COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
  7988. COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
  7989. COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
  7990. COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
  7991. COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
  7992. COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
  7993. #else
  7994. #define INIT_CAPSICUM
  7995. #endif
  7996. #if SANITIZER_INTERCEPT_SHA1
  7997. INTERCEPTOR(void, SHA1Init, void *context) {
  7998. void *ctx;
  7999. COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
  8000. REAL(SHA1Init)(context);
  8001. if (context)
  8002. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
  8003. }
  8004. INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
  8005. void *ctx;
  8006. COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
  8007. if (data && len > 0)
  8008. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8009. if (context)
  8010. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
  8011. REAL(SHA1Update)(context, data, len);
  8012. if (context)
  8013. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
  8014. }
  8015. INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
  8016. void *ctx;
  8017. COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
  8018. if (context)
  8019. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
  8020. REAL(SHA1Final)(digest, context);
  8021. if (digest)
  8022. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
  8023. }
  8024. INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
  8025. void *ctx;
  8026. COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
  8027. if (state)
  8028. COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
  8029. if (buffer)
  8030. COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
  8031. REAL(SHA1Transform)(state, buffer);
  8032. if (state)
  8033. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
  8034. }
  8035. INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
  8036. void *ctx;
  8037. COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
  8038. if (context)
  8039. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
  8040. char *ret = REAL(SHA1End)(context, buf);
  8041. if (ret)
  8042. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  8043. return ret;
  8044. }
  8045. INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
  8046. void *ctx;
  8047. COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
  8048. if (filename)
  8049. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8050. char *ret = REAL(SHA1File)(filename, buf);
  8051. if (ret)
  8052. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  8053. return ret;
  8054. }
  8055. INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
  8056. OFF_T length) {
  8057. void *ctx;
  8058. COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
  8059. if (filename)
  8060. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8061. char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
  8062. if (ret)
  8063. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  8064. return ret;
  8065. }
  8066. INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
  8067. void *ctx;
  8068. COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
  8069. if (data)
  8070. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8071. char *ret = REAL(SHA1Data)(data, len, buf);
  8072. if (ret)
  8073. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  8074. return ret;
  8075. }
  8076. #define INIT_SHA1 \
  8077. COMMON_INTERCEPT_FUNCTION(SHA1Init); \
  8078. COMMON_INTERCEPT_FUNCTION(SHA1Update); \
  8079. COMMON_INTERCEPT_FUNCTION(SHA1Final); \
  8080. COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
  8081. COMMON_INTERCEPT_FUNCTION(SHA1End); \
  8082. COMMON_INTERCEPT_FUNCTION(SHA1File); \
  8083. COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
  8084. COMMON_INTERCEPT_FUNCTION(SHA1Data)
  8085. #else
  8086. #define INIT_SHA1
  8087. #endif
  8088. #if SANITIZER_INTERCEPT_MD4
  8089. INTERCEPTOR(void, MD4Init, void *context) {
  8090. void *ctx;
  8091. COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
  8092. REAL(MD4Init)(context);
  8093. if (context)
  8094. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
  8095. }
  8096. INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
  8097. unsigned int len) {
  8098. void *ctx;
  8099. COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
  8100. if (data && len > 0)
  8101. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8102. if (context)
  8103. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
  8104. REAL(MD4Update)(context, data, len);
  8105. if (context)
  8106. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
  8107. }
  8108. INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
  8109. void *ctx;
  8110. COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
  8111. if (context)
  8112. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
  8113. REAL(MD4Final)(digest, context);
  8114. if (digest)
  8115. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
  8116. }
  8117. INTERCEPTOR(char *, MD4End, void *context, char *buf) {
  8118. void *ctx;
  8119. COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
  8120. if (context)
  8121. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
  8122. char *ret = REAL(MD4End)(context, buf);
  8123. if (ret)
  8124. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
  8125. return ret;
  8126. }
  8127. INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
  8128. void *ctx;
  8129. COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
  8130. if (filename)
  8131. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8132. char *ret = REAL(MD4File)(filename, buf);
  8133. if (ret)
  8134. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
  8135. return ret;
  8136. }
  8137. INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
  8138. char *buf) {
  8139. void *ctx;
  8140. COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
  8141. if (data && len > 0)
  8142. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8143. char *ret = REAL(MD4Data)(data, len, buf);
  8144. if (ret)
  8145. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
  8146. return ret;
  8147. }
  8148. #define INIT_MD4 \
  8149. COMMON_INTERCEPT_FUNCTION(MD4Init); \
  8150. COMMON_INTERCEPT_FUNCTION(MD4Update); \
  8151. COMMON_INTERCEPT_FUNCTION(MD4Final); \
  8152. COMMON_INTERCEPT_FUNCTION(MD4End); \
  8153. COMMON_INTERCEPT_FUNCTION(MD4File); \
  8154. COMMON_INTERCEPT_FUNCTION(MD4Data)
  8155. #else
  8156. #define INIT_MD4
  8157. #endif
  8158. #if SANITIZER_INTERCEPT_RMD160
  8159. INTERCEPTOR(void, RMD160Init, void *context) {
  8160. void *ctx;
  8161. COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
  8162. REAL(RMD160Init)(context);
  8163. if (context)
  8164. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
  8165. }
  8166. INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
  8167. void *ctx;
  8168. COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
  8169. if (data && len > 0)
  8170. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8171. if (context)
  8172. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
  8173. REAL(RMD160Update)(context, data, len);
  8174. if (context)
  8175. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
  8176. }
  8177. INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
  8178. void *ctx;
  8179. COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
  8180. if (context)
  8181. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
  8182. REAL(RMD160Final)(digest, context);
  8183. if (digest)
  8184. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
  8185. }
  8186. INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
  8187. void *ctx;
  8188. COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
  8189. if (state)
  8190. COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
  8191. if (buffer)
  8192. COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
  8193. REAL(RMD160Transform)(state, buffer);
  8194. if (state)
  8195. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
  8196. }
  8197. INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
  8198. void *ctx;
  8199. COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
  8200. if (context)
  8201. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
  8202. char *ret = REAL(RMD160End)(context, buf);
  8203. if (ret)
  8204. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  8205. return ret;
  8206. }
  8207. INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
  8208. void *ctx;
  8209. COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
  8210. if (filename)
  8211. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8212. char *ret = REAL(RMD160File)(filename, buf);
  8213. if (ret)
  8214. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  8215. return ret;
  8216. }
  8217. INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
  8218. OFF_T length) {
  8219. void *ctx;
  8220. COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
  8221. if (filename)
  8222. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8223. char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
  8224. if (ret)
  8225. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  8226. return ret;
  8227. }
  8228. INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
  8229. void *ctx;
  8230. COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
  8231. if (data && len > 0)
  8232. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8233. char *ret = REAL(RMD160Data)(data, len, buf);
  8234. if (ret)
  8235. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  8236. return ret;
  8237. }
  8238. #define INIT_RMD160 \
  8239. COMMON_INTERCEPT_FUNCTION(RMD160Init); \
  8240. COMMON_INTERCEPT_FUNCTION(RMD160Update); \
  8241. COMMON_INTERCEPT_FUNCTION(RMD160Final); \
  8242. COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
  8243. COMMON_INTERCEPT_FUNCTION(RMD160End); \
  8244. COMMON_INTERCEPT_FUNCTION(RMD160File); \
  8245. COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
  8246. COMMON_INTERCEPT_FUNCTION(RMD160Data)
  8247. #else
  8248. #define INIT_RMD160
  8249. #endif
  8250. #if SANITIZER_INTERCEPT_MD5
  8251. INTERCEPTOR(void, MD5Init, void *context) {
  8252. void *ctx;
  8253. COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
  8254. REAL(MD5Init)(context);
  8255. if (context)
  8256. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
  8257. }
  8258. INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
  8259. unsigned int len) {
  8260. void *ctx;
  8261. COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
  8262. if (data && len > 0)
  8263. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8264. if (context)
  8265. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
  8266. REAL(MD5Update)(context, data, len);
  8267. if (context)
  8268. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
  8269. }
  8270. INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
  8271. void *ctx;
  8272. COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
  8273. if (context)
  8274. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
  8275. REAL(MD5Final)(digest, context);
  8276. if (digest)
  8277. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
  8278. }
  8279. INTERCEPTOR(char *, MD5End, void *context, char *buf) {
  8280. void *ctx;
  8281. COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
  8282. if (context)
  8283. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
  8284. char *ret = REAL(MD5End)(context, buf);
  8285. if (ret)
  8286. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
  8287. return ret;
  8288. }
  8289. INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
  8290. void *ctx;
  8291. COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
  8292. if (filename)
  8293. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8294. char *ret = REAL(MD5File)(filename, buf);
  8295. if (ret)
  8296. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
  8297. return ret;
  8298. }
  8299. INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
  8300. char *buf) {
  8301. void *ctx;
  8302. COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
  8303. if (data && len > 0)
  8304. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8305. char *ret = REAL(MD5Data)(data, len, buf);
  8306. if (ret)
  8307. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
  8308. return ret;
  8309. }
  8310. #define INIT_MD5 \
  8311. COMMON_INTERCEPT_FUNCTION(MD5Init); \
  8312. COMMON_INTERCEPT_FUNCTION(MD5Update); \
  8313. COMMON_INTERCEPT_FUNCTION(MD5Final); \
  8314. COMMON_INTERCEPT_FUNCTION(MD5End); \
  8315. COMMON_INTERCEPT_FUNCTION(MD5File); \
  8316. COMMON_INTERCEPT_FUNCTION(MD5Data)
  8317. #else
  8318. #define INIT_MD5
  8319. #endif
  8320. #if SANITIZER_INTERCEPT_FSEEK
  8321. INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
  8322. void *ctx;
  8323. COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
  8324. return REAL(fseek)(stream, offset, whence);
  8325. }
  8326. INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
  8327. void *ctx;
  8328. COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
  8329. return REAL(fseeko)(stream, offset, whence);
  8330. }
  8331. INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
  8332. void *ctx;
  8333. COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
  8334. return REAL(ftell)(stream);
  8335. }
  8336. INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
  8337. void *ctx;
  8338. COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
  8339. return REAL(ftello)(stream);
  8340. }
  8341. INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
  8342. void *ctx;
  8343. COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
  8344. return REAL(rewind)(stream);
  8345. }
  8346. INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
  8347. void *ctx;
  8348. COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
  8349. int ret = REAL(fgetpos)(stream, pos);
  8350. if (pos && !ret)
  8351. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
  8352. return ret;
  8353. }
  8354. INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
  8355. void *ctx;
  8356. COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
  8357. if (pos)
  8358. COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
  8359. return REAL(fsetpos)(stream, pos);
  8360. }
  8361. #define INIT_FSEEK \
  8362. COMMON_INTERCEPT_FUNCTION(fseek); \
  8363. COMMON_INTERCEPT_FUNCTION(fseeko); \
  8364. COMMON_INTERCEPT_FUNCTION(ftell); \
  8365. COMMON_INTERCEPT_FUNCTION(ftello); \
  8366. COMMON_INTERCEPT_FUNCTION(rewind); \
  8367. COMMON_INTERCEPT_FUNCTION(fgetpos); \
  8368. COMMON_INTERCEPT_FUNCTION(fsetpos)
  8369. #else
  8370. #define INIT_FSEEK
  8371. #endif
  8372. #if SANITIZER_INTERCEPT_MD2
  8373. INTERCEPTOR(void, MD2Init, void *context) {
  8374. void *ctx;
  8375. COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
  8376. REAL(MD2Init)(context);
  8377. if (context)
  8378. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
  8379. }
  8380. INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
  8381. unsigned int len) {
  8382. void *ctx;
  8383. COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
  8384. if (data && len > 0)
  8385. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8386. if (context)
  8387. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
  8388. REAL(MD2Update)(context, data, len);
  8389. if (context)
  8390. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
  8391. }
  8392. INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
  8393. void *ctx;
  8394. COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
  8395. if (context)
  8396. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
  8397. REAL(MD2Final)(digest, context);
  8398. if (digest)
  8399. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
  8400. }
  8401. INTERCEPTOR(char *, MD2End, void *context, char *buf) {
  8402. void *ctx;
  8403. COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
  8404. if (context)
  8405. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
  8406. char *ret = REAL(MD2End)(context, buf);
  8407. if (ret)
  8408. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
  8409. return ret;
  8410. }
  8411. INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
  8412. void *ctx;
  8413. COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
  8414. if (filename)
  8415. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8416. char *ret = REAL(MD2File)(filename, buf);
  8417. if (ret)
  8418. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
  8419. return ret;
  8420. }
  8421. INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
  8422. char *buf) {
  8423. void *ctx;
  8424. COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
  8425. if (data && len > 0)
  8426. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8427. char *ret = REAL(MD2Data)(data, len, buf);
  8428. if (ret)
  8429. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
  8430. return ret;
  8431. }
  8432. #define INIT_MD2 \
  8433. COMMON_INTERCEPT_FUNCTION(MD2Init); \
  8434. COMMON_INTERCEPT_FUNCTION(MD2Update); \
  8435. COMMON_INTERCEPT_FUNCTION(MD2Final); \
  8436. COMMON_INTERCEPT_FUNCTION(MD2End); \
  8437. COMMON_INTERCEPT_FUNCTION(MD2File); \
  8438. COMMON_INTERCEPT_FUNCTION(MD2Data)
  8439. #else
  8440. #define INIT_MD2
  8441. #endif
  8442. #if SANITIZER_INTERCEPT_SHA2
  8443. #define SHA2_INTERCEPTORS(LEN, SHA2_STATE_T) \
  8444. INTERCEPTOR(void, SHA##LEN##_Init, void *context) { \
  8445. void *ctx; \
  8446. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Init, context); \
  8447. REAL(SHA##LEN##_Init)(context); \
  8448. if (context) \
  8449. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
  8450. } \
  8451. INTERCEPTOR(void, SHA##LEN##_Update, void *context, \
  8452. const u8 *data, SIZE_T len) { \
  8453. void *ctx; \
  8454. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Update, context, data, len); \
  8455. if (data && len > 0) \
  8456. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
  8457. if (context) \
  8458. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
  8459. REAL(SHA##LEN##_Update)(context, data, len); \
  8460. if (context) \
  8461. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
  8462. } \
  8463. INTERCEPTOR(void, SHA##LEN##_Final, u8 digest[LEN/8], \
  8464. void *context) { \
  8465. void *ctx; \
  8466. CHECK_EQ(SHA##LEN##_digest_length, LEN/8); \
  8467. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Final, digest, context); \
  8468. if (context) \
  8469. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
  8470. REAL(SHA##LEN##_Final)(digest, context); \
  8471. if (digest) \
  8472. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, \
  8473. sizeof(digest[0]) * \
  8474. SHA##LEN##_digest_length); \
  8475. } \
  8476. INTERCEPTOR(char *, SHA##LEN##_End, void *context, char *buf) { \
  8477. void *ctx; \
  8478. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_End, context, buf); \
  8479. if (context) \
  8480. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
  8481. char *ret = REAL(SHA##LEN##_End)(context, buf); \
  8482. if (ret) \
  8483. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
  8484. return ret; \
  8485. } \
  8486. INTERCEPTOR(char *, SHA##LEN##_File, const char *filename, char *buf) { \
  8487. void *ctx; \
  8488. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_File, filename, buf); \
  8489. if (filename) \
  8490. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);\
  8491. char *ret = REAL(SHA##LEN##_File)(filename, buf); \
  8492. if (ret) \
  8493. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
  8494. return ret; \
  8495. } \
  8496. INTERCEPTOR(char *, SHA##LEN##_FileChunk, const char *filename, char *buf, \
  8497. OFF_T offset, OFF_T length) { \
  8498. void *ctx; \
  8499. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_FileChunk, filename, buf, offset, \
  8500. length); \
  8501. if (filename) \
  8502. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);\
  8503. char *ret = REAL(SHA##LEN##_FileChunk)(filename, buf, offset, length); \
  8504. if (ret) \
  8505. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
  8506. return ret; \
  8507. } \
  8508. INTERCEPTOR(char *, SHA##LEN##_Data, u8 *data, SIZE_T len, char *buf) { \
  8509. void *ctx; \
  8510. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Data, data, len, buf); \
  8511. if (data && len > 0) \
  8512. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
  8513. char *ret = REAL(SHA##LEN##_Data)(data, len, buf); \
  8514. if (ret) \
  8515. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
  8516. return ret; \
  8517. }
  8518. SHA2_INTERCEPTORS(224, u32)
  8519. SHA2_INTERCEPTORS(256, u32)
  8520. SHA2_INTERCEPTORS(384, u64)
  8521. SHA2_INTERCEPTORS(512, u64)
  8522. #define INIT_SHA2_INTECEPTORS(LEN) \
  8523. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Init); \
  8524. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Update); \
  8525. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Final); \
  8526. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_End); \
  8527. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_File); \
  8528. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_FileChunk); \
  8529. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Data)
  8530. #define INIT_SHA2 \
  8531. INIT_SHA2_INTECEPTORS(224); \
  8532. INIT_SHA2_INTECEPTORS(256); \
  8533. INIT_SHA2_INTECEPTORS(384); \
  8534. INIT_SHA2_INTECEPTORS(512)
  8535. #undef SHA2_INTERCEPTORS
  8536. #else
  8537. #define INIT_SHA2
  8538. #endif
  8539. #if SANITIZER_INTERCEPT_VIS
  8540. INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
  8541. void *ctx;
  8542. COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
  8543. char *end = REAL(vis)(dst, c, flag, nextc);
  8544. // dst is NULL terminated and end points to the NULL char
  8545. if (dst && end)
  8546. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
  8547. return end;
  8548. }
  8549. INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
  8550. void *ctx;
  8551. COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
  8552. char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
  8553. // nvis cannot make sure the dst is NULL terminated
  8554. if (dst && end)
  8555. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
  8556. return end;
  8557. }
  8558. INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
  8559. void *ctx;
  8560. COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
  8561. if (src)
  8562. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8563. int len = REAL(strvis)(dst, src, flag);
  8564. if (dst)
  8565. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  8566. return len;
  8567. }
  8568. INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
  8569. void *ctx;
  8570. COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
  8571. if (src)
  8572. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8573. int len = REAL(stravis)(dst, src, flag);
  8574. if (dst) {
  8575. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
  8576. if (*dst)
  8577. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
  8578. }
  8579. return len;
  8580. }
  8581. INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
  8582. void *ctx;
  8583. COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
  8584. if (src)
  8585. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8586. int len = REAL(strnvis)(dst, dlen, src, flag);
  8587. // The interface will be valid even if there is no space for NULL char
  8588. if (dst && len > 0)
  8589. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  8590. return len;
  8591. }
  8592. INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
  8593. void *ctx;
  8594. COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
  8595. if (src)
  8596. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8597. int ret = REAL(strvisx)(dst, src, len, flag);
  8598. if (dst)
  8599. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8600. return ret;
  8601. }
  8602. INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
  8603. int flag) {
  8604. void *ctx;
  8605. COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
  8606. if (src)
  8607. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8608. int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
  8609. if (dst && ret >= 0)
  8610. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8611. return ret;
  8612. }
  8613. INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
  8614. int flag, int *cerr_ptr) {
  8615. void *ctx;
  8616. COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
  8617. if (src)
  8618. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8619. // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
  8620. // according to the implementation
  8621. if (cerr_ptr)
  8622. COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
  8623. int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
  8624. if (dst && ret >= 0)
  8625. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8626. if (cerr_ptr)
  8627. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
  8628. return ret;
  8629. }
  8630. INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
  8631. const char *extra) {
  8632. void *ctx;
  8633. COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
  8634. if (extra)
  8635. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8636. char *end = REAL(svis)(dst, c, flag, nextc, extra);
  8637. if (dst && end)
  8638. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
  8639. return end;
  8640. }
  8641. INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
  8642. const char *extra) {
  8643. void *ctx;
  8644. COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
  8645. if (extra)
  8646. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8647. char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
  8648. if (dst && end)
  8649. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
  8650. Min((SIZE_T)(end - dst + 1), dlen));
  8651. return end;
  8652. }
  8653. INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
  8654. const char *extra) {
  8655. void *ctx;
  8656. COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
  8657. if (src)
  8658. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8659. if (extra)
  8660. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8661. int len = REAL(strsvis)(dst, src, flag, extra);
  8662. if (dst)
  8663. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  8664. return len;
  8665. }
  8666. INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
  8667. const char *extra) {
  8668. void *ctx;
  8669. COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
  8670. if (src)
  8671. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8672. if (extra)
  8673. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8674. int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
  8675. // The interface will be valid even if there is no space for NULL char
  8676. if (dst && len >= 0)
  8677. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  8678. return len;
  8679. }
  8680. INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
  8681. const char *extra) {
  8682. void *ctx;
  8683. COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
  8684. if (src)
  8685. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8686. if (extra)
  8687. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8688. int ret = REAL(strsvisx)(dst, src, len, flag, extra);
  8689. if (dst)
  8690. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8691. return ret;
  8692. }
  8693. INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
  8694. int flag, const char *extra) {
  8695. void *ctx;
  8696. COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
  8697. if (src)
  8698. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8699. if (extra)
  8700. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8701. int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
  8702. if (dst && ret >= 0)
  8703. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8704. return ret;
  8705. }
  8706. INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
  8707. SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
  8708. void *ctx;
  8709. COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
  8710. cerr_ptr);
  8711. if (src)
  8712. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8713. if (extra)
  8714. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8715. // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
  8716. // according to the implementation
  8717. if (cerr_ptr)
  8718. COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
  8719. int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
  8720. if (dst && ret >= 0)
  8721. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8722. if (cerr_ptr)
  8723. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
  8724. return ret;
  8725. }
  8726. INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
  8727. void *ctx;
  8728. COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
  8729. if (astate)
  8730. COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
  8731. int ret = REAL(unvis)(cp, c, astate, flag);
  8732. if (ret == unvis_valid || ret == unvis_validpush) {
  8733. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
  8734. }
  8735. return ret;
  8736. }
  8737. INTERCEPTOR(int, strunvis, char *dst, const char *src) {
  8738. void *ctx;
  8739. COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
  8740. if (src)
  8741. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8742. int ret = REAL(strunvis)(dst, src);
  8743. if (ret != -1)
  8744. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8745. return ret;
  8746. }
  8747. INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
  8748. void *ctx;
  8749. COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
  8750. if (src)
  8751. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8752. int ret = REAL(strnunvis)(dst, dlen, src);
  8753. if (ret != -1)
  8754. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8755. return ret;
  8756. }
  8757. INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
  8758. void *ctx;
  8759. COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
  8760. if (src)
  8761. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8762. int ret = REAL(strunvisx)(dst, src, flag);
  8763. if (ret != -1)
  8764. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8765. return ret;
  8766. }
  8767. INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
  8768. int flag) {
  8769. void *ctx;
  8770. COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
  8771. if (src)
  8772. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8773. int ret = REAL(strnunvisx)(dst, dlen, src, flag);
  8774. if (ret != -1)
  8775. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8776. return ret;
  8777. }
  8778. #define INIT_VIS \
  8779. COMMON_INTERCEPT_FUNCTION(vis); \
  8780. COMMON_INTERCEPT_FUNCTION(nvis); \
  8781. COMMON_INTERCEPT_FUNCTION(strvis); \
  8782. COMMON_INTERCEPT_FUNCTION(stravis); \
  8783. COMMON_INTERCEPT_FUNCTION(strnvis); \
  8784. COMMON_INTERCEPT_FUNCTION(strvisx); \
  8785. COMMON_INTERCEPT_FUNCTION(strnvisx); \
  8786. COMMON_INTERCEPT_FUNCTION(strenvisx); \
  8787. COMMON_INTERCEPT_FUNCTION(svis); \
  8788. COMMON_INTERCEPT_FUNCTION(snvis); \
  8789. COMMON_INTERCEPT_FUNCTION(strsvis); \
  8790. COMMON_INTERCEPT_FUNCTION(strsnvis); \
  8791. COMMON_INTERCEPT_FUNCTION(strsvisx); \
  8792. COMMON_INTERCEPT_FUNCTION(strsnvisx); \
  8793. COMMON_INTERCEPT_FUNCTION(strsenvisx); \
  8794. COMMON_INTERCEPT_FUNCTION(unvis); \
  8795. COMMON_INTERCEPT_FUNCTION(strunvis); \
  8796. COMMON_INTERCEPT_FUNCTION(strnunvis); \
  8797. COMMON_INTERCEPT_FUNCTION(strunvisx); \
  8798. COMMON_INTERCEPT_FUNCTION(strnunvisx)
  8799. #else
  8800. #define INIT_VIS
  8801. #endif
  8802. #if SANITIZER_INTERCEPT_CDB
  8803. INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
  8804. void *ctx;
  8805. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
  8806. if (path)
  8807. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  8808. struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
  8809. if (cdbr)
  8810. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
  8811. return cdbr;
  8812. }
  8813. INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
  8814. int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
  8815. void *ctx;
  8816. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
  8817. cookie);
  8818. if (base && size)
  8819. COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
  8820. struct __sanitizer_cdbr *cdbr =
  8821. REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
  8822. if (cdbr)
  8823. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
  8824. return cdbr;
  8825. }
  8826. INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
  8827. void *ctx;
  8828. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
  8829. if (cdbr)
  8830. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  8831. return REAL(cdbr_entries)(cdbr);
  8832. }
  8833. INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
  8834. const void **data, SIZE_T *datalen) {
  8835. void *ctx;
  8836. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
  8837. if (cdbr)
  8838. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  8839. int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
  8840. if (!ret) {
  8841. if (data)
  8842. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
  8843. if (datalen)
  8844. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
  8845. if (data && datalen)
  8846. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
  8847. }
  8848. return ret;
  8849. }
  8850. INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
  8851. SIZE_T keylen, const void **data, SIZE_T *datalen) {
  8852. void *ctx;
  8853. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
  8854. if (cdbr)
  8855. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  8856. if (key)
  8857. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
  8858. int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
  8859. if (!ret) {
  8860. if (data)
  8861. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
  8862. if (datalen)
  8863. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
  8864. if (data && datalen)
  8865. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
  8866. }
  8867. return ret;
  8868. }
  8869. INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
  8870. void *ctx;
  8871. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
  8872. if (cdbr)
  8873. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  8874. REAL(cdbr_close)(cdbr);
  8875. }
  8876. INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
  8877. void *ctx;
  8878. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
  8879. struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
  8880. if (ret)
  8881. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
  8882. return ret;
  8883. }
  8884. INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
  8885. SIZE_T keylen, const void *data, SIZE_T datalen) {
  8886. void *ctx;
  8887. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
  8888. if (cdbw)
  8889. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  8890. if (data && datalen)
  8891. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
  8892. if (key && keylen)
  8893. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
  8894. int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
  8895. if (!ret && cdbw)
  8896. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  8897. return ret;
  8898. }
  8899. INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
  8900. SIZE_T datalen, u32 *index) {
  8901. void *ctx;
  8902. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
  8903. if (cdbw)
  8904. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  8905. if (data && datalen)
  8906. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
  8907. int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
  8908. if (!ret) {
  8909. if (index)
  8910. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
  8911. if (cdbw)
  8912. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  8913. }
  8914. return ret;
  8915. }
  8916. INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
  8917. SIZE_T keylen, u32 index) {
  8918. void *ctx;
  8919. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
  8920. if (cdbw)
  8921. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  8922. if (key && keylen)
  8923. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
  8924. int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
  8925. if (!ret && cdbw)
  8926. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  8927. return ret;
  8928. }
  8929. INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
  8930. const char descr[16], u32 (*seedgen)(void)) {
  8931. void *ctx;
  8932. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
  8933. COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
  8934. if (cdbw)
  8935. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  8936. if (descr)
  8937. COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
  8938. if (seedgen)
  8939. COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
  8940. int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
  8941. if (!ret) {
  8942. if (cdbw)
  8943. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  8944. if (output >= 0)
  8945. COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
  8946. }
  8947. return ret;
  8948. }
  8949. INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
  8950. void *ctx;
  8951. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
  8952. if (cdbw)
  8953. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  8954. REAL(cdbw_close)(cdbw);
  8955. }
  8956. #define INIT_CDB \
  8957. COMMON_INTERCEPT_FUNCTION(cdbr_open); \
  8958. COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
  8959. COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
  8960. COMMON_INTERCEPT_FUNCTION(cdbr_get); \
  8961. COMMON_INTERCEPT_FUNCTION(cdbr_find); \
  8962. COMMON_INTERCEPT_FUNCTION(cdbr_close); \
  8963. COMMON_INTERCEPT_FUNCTION(cdbw_open); \
  8964. COMMON_INTERCEPT_FUNCTION(cdbw_put); \
  8965. COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
  8966. COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
  8967. COMMON_INTERCEPT_FUNCTION(cdbw_output); \
  8968. COMMON_INTERCEPT_FUNCTION(cdbw_close)
  8969. #else
  8970. #define INIT_CDB
  8971. #endif
  8972. #if SANITIZER_INTERCEPT_GETFSENT
  8973. INTERCEPTOR(void *, getfsent) {
  8974. void *ctx;
  8975. COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
  8976. void *ret = REAL(getfsent)();
  8977. if (ret)
  8978. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
  8979. return ret;
  8980. }
  8981. INTERCEPTOR(void *, getfsspec, const char *spec) {
  8982. void *ctx;
  8983. COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
  8984. if (spec)
  8985. COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, internal_strlen(spec) + 1);
  8986. void *ret = REAL(getfsspec)(spec);
  8987. if (ret)
  8988. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
  8989. return ret;
  8990. }
  8991. INTERCEPTOR(void *, getfsfile, const char *file) {
  8992. void *ctx;
  8993. COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
  8994. if (file)
  8995. COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
  8996. void *ret = REAL(getfsfile)(file);
  8997. if (ret)
  8998. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
  8999. return ret;
  9000. }
  9001. #define INIT_GETFSENT \
  9002. COMMON_INTERCEPT_FUNCTION(getfsent); \
  9003. COMMON_INTERCEPT_FUNCTION(getfsspec); \
  9004. COMMON_INTERCEPT_FUNCTION(getfsfile);
  9005. #else
  9006. #define INIT_GETFSENT
  9007. #endif
  9008. #if SANITIZER_INTERCEPT_ARC4RANDOM
  9009. INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
  9010. void *ctx;
  9011. COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
  9012. REAL(arc4random_buf)(buf, len);
  9013. if (buf && len)
  9014. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
  9015. }
  9016. INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
  9017. void *ctx;
  9018. COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
  9019. if (dat && datlen)
  9020. COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
  9021. REAL(arc4random_addrandom)(dat, datlen);
  9022. }
  9023. #define INIT_ARC4RANDOM \
  9024. COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
  9025. COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
  9026. #else
  9027. #define INIT_ARC4RANDOM
  9028. #endif
  9029. #if SANITIZER_INTERCEPT_POPEN
  9030. INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
  9031. void *ctx;
  9032. COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
  9033. if (command)
  9034. COMMON_INTERCEPTOR_READ_RANGE(ctx, command, internal_strlen(command) + 1);
  9035. if (type)
  9036. COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
  9037. __sanitizer_FILE *res = REAL(popen)(command, type);
  9038. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
  9039. if (res) unpoison_file(res);
  9040. return res;
  9041. }
  9042. #define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
  9043. #else
  9044. #define INIT_POPEN
  9045. #endif
  9046. #if SANITIZER_INTERCEPT_POPENVE
  9047. INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
  9048. char *const *argv, char *const *envp, const char *type) {
  9049. void *ctx;
  9050. COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
  9051. if (path)
  9052. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  9053. if (argv) {
  9054. for (char *const *pa = argv; ; ++pa) {
  9055. COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
  9056. if (!*pa)
  9057. break;
  9058. COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
  9059. }
  9060. }
  9061. if (envp) {
  9062. for (char *const *pa = envp; ; ++pa) {
  9063. COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
  9064. if (!*pa)
  9065. break;
  9066. COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
  9067. }
  9068. }
  9069. if (type)
  9070. COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
  9071. __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
  9072. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
  9073. if (res) unpoison_file(res);
  9074. return res;
  9075. }
  9076. #define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
  9077. #else
  9078. #define INIT_POPENVE
  9079. #endif
  9080. #if SANITIZER_INTERCEPT_PCLOSE
  9081. INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
  9082. void *ctx;
  9083. COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
  9084. COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
  9085. const FileMetadata *m = GetInterceptorMetadata(fp);
  9086. int res = REAL(pclose)(fp);
  9087. if (m) {
  9088. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
  9089. DeleteInterceptorMetadata(fp);
  9090. }
  9091. return res;
  9092. }
  9093. #define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
  9094. #else
  9095. #define INIT_PCLOSE
  9096. #endif
  9097. #if SANITIZER_INTERCEPT_FUNOPEN
  9098. typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
  9099. typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
  9100. typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
  9101. typedef int (*funopen_closefn)(void *cookie);
  9102. struct WrappedFunopenCookie {
  9103. void *real_cookie;
  9104. funopen_readfn real_read;
  9105. funopen_writefn real_write;
  9106. funopen_seekfn real_seek;
  9107. funopen_closefn real_close;
  9108. };
  9109. static int wrapped_funopen_read(void *cookie, char *buf, int len) {
  9110. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9111. WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  9112. funopen_readfn real_read = wrapped_cookie->real_read;
  9113. return real_read(wrapped_cookie->real_cookie, buf, len);
  9114. }
  9115. static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
  9116. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9117. WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  9118. funopen_writefn real_write = wrapped_cookie->real_write;
  9119. return real_write(wrapped_cookie->real_cookie, buf, len);
  9120. }
  9121. static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
  9122. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9123. WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  9124. funopen_seekfn real_seek = wrapped_cookie->real_seek;
  9125. return real_seek(wrapped_cookie->real_cookie, offset, whence);
  9126. }
  9127. static int wrapped_funopen_close(void *cookie) {
  9128. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  9129. WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  9130. funopen_closefn real_close = wrapped_cookie->real_close;
  9131. int res = real_close(wrapped_cookie->real_cookie);
  9132. InternalFree(wrapped_cookie);
  9133. return res;
  9134. }
  9135. INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
  9136. funopen_writefn writefn, funopen_seekfn seekfn,
  9137. funopen_closefn closefn) {
  9138. void *ctx;
  9139. COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
  9140. closefn);
  9141. WrappedFunopenCookie *wrapped_cookie =
  9142. (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
  9143. wrapped_cookie->real_cookie = cookie;
  9144. wrapped_cookie->real_read = readfn;
  9145. wrapped_cookie->real_write = writefn;
  9146. wrapped_cookie->real_seek = seekfn;
  9147. wrapped_cookie->real_close = closefn;
  9148. __sanitizer_FILE *res =
  9149. REAL(funopen)(wrapped_cookie,
  9150. readfn ? wrapped_funopen_read : nullptr,
  9151. writefn ? wrapped_funopen_write : nullptr,
  9152. seekfn ? wrapped_funopen_seek : nullptr,
  9153. closefn ? wrapped_funopen_close : nullptr);
  9154. if (res)
  9155. unpoison_file(res);
  9156. return res;
  9157. }
  9158. #define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
  9159. #else
  9160. #define INIT_FUNOPEN
  9161. #endif
  9162. #if SANITIZER_INTERCEPT_FUNOPEN2
  9163. typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
  9164. typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
  9165. typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
  9166. typedef int (*funopen2_flushfn)(void *cookie);
  9167. typedef int (*funopen2_closefn)(void *cookie);
  9168. struct WrappedFunopen2Cookie {
  9169. void *real_cookie;
  9170. funopen2_readfn real_read;
  9171. funopen2_writefn real_write;
  9172. funopen2_seekfn real_seek;
  9173. funopen2_flushfn real_flush;
  9174. funopen2_closefn real_close;
  9175. };
  9176. static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
  9177. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9178. WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  9179. funopen2_readfn real_read = wrapped_cookie->real_read;
  9180. return real_read(wrapped_cookie->real_cookie, buf, len);
  9181. }
  9182. static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
  9183. SIZE_T len) {
  9184. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9185. WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  9186. funopen2_writefn real_write = wrapped_cookie->real_write;
  9187. return real_write(wrapped_cookie->real_cookie, buf, len);
  9188. }
  9189. static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
  9190. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9191. WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  9192. funopen2_seekfn real_seek = wrapped_cookie->real_seek;
  9193. return real_seek(wrapped_cookie->real_cookie, offset, whence);
  9194. }
  9195. static int wrapped_funopen2_flush(void *cookie) {
  9196. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  9197. WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  9198. funopen2_flushfn real_flush = wrapped_cookie->real_flush;
  9199. return real_flush(wrapped_cookie->real_cookie);
  9200. }
  9201. static int wrapped_funopen2_close(void *cookie) {
  9202. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  9203. WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  9204. funopen2_closefn real_close = wrapped_cookie->real_close;
  9205. int res = real_close(wrapped_cookie->real_cookie);
  9206. InternalFree(wrapped_cookie);
  9207. return res;
  9208. }
  9209. INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
  9210. funopen2_writefn writefn, funopen2_seekfn seekfn,
  9211. funopen2_flushfn flushfn, funopen2_closefn closefn) {
  9212. void *ctx;
  9213. COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
  9214. flushfn, closefn);
  9215. WrappedFunopen2Cookie *wrapped_cookie =
  9216. (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
  9217. wrapped_cookie->real_cookie = cookie;
  9218. wrapped_cookie->real_read = readfn;
  9219. wrapped_cookie->real_write = writefn;
  9220. wrapped_cookie->real_seek = seekfn;
  9221. wrapped_cookie->real_flush = flushfn;
  9222. wrapped_cookie->real_close = closefn;
  9223. __sanitizer_FILE *res =
  9224. REAL(funopen2)(wrapped_cookie,
  9225. readfn ? wrapped_funopen2_read : nullptr,
  9226. writefn ? wrapped_funopen2_write : nullptr,
  9227. seekfn ? wrapped_funopen2_seek : nullptr,
  9228. flushfn ? wrapped_funopen2_flush : nullptr,
  9229. closefn ? wrapped_funopen2_close : nullptr);
  9230. if (res)
  9231. unpoison_file(res);
  9232. return res;
  9233. }
  9234. #define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
  9235. #else
  9236. #define INIT_FUNOPEN2
  9237. #endif
  9238. #if SANITIZER_INTERCEPT_FDEVNAME
  9239. INTERCEPTOR(char *, fdevname, int fd) {
  9240. void *ctx;
  9241. COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
  9242. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  9243. char *name = REAL(fdevname)(fd);
  9244. if (name) {
  9245. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
  9246. if (fd > 0)
  9247. COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  9248. }
  9249. return name;
  9250. }
  9251. INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
  9252. void *ctx;
  9253. COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
  9254. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  9255. char *name = REAL(fdevname_r)(fd, buf, len);
  9256. if (name && buf && len > 0) {
  9257. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
  9258. if (fd > 0)
  9259. COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  9260. }
  9261. return name;
  9262. }
  9263. #define INIT_FDEVNAME \
  9264. COMMON_INTERCEPT_FUNCTION(fdevname); \
  9265. COMMON_INTERCEPT_FUNCTION(fdevname_r);
  9266. #else
  9267. #define INIT_FDEVNAME
  9268. #endif
  9269. #if SANITIZER_INTERCEPT_GETUSERSHELL
  9270. INTERCEPTOR(char *, getusershell) {
  9271. void *ctx;
  9272. COMMON_INTERCEPTOR_ENTER(ctx, getusershell);
  9273. char *res = REAL(getusershell)();
  9274. if (res)
  9275. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  9276. return res;
  9277. }
  9278. #define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
  9279. #else
  9280. #define INIT_GETUSERSHELL
  9281. #endif
  9282. #if SANITIZER_INTERCEPT_SL_INIT
  9283. INTERCEPTOR(void *, sl_init) {
  9284. void *ctx;
  9285. COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
  9286. void *res = REAL(sl_init)();
  9287. if (res)
  9288. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
  9289. return res;
  9290. }
  9291. INTERCEPTOR(int, sl_add, void *sl, char *item) {
  9292. void *ctx;
  9293. COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
  9294. if (sl)
  9295. COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  9296. if (item)
  9297. COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
  9298. int res = REAL(sl_add)(sl, item);
  9299. if (!res)
  9300. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  9301. return res;
  9302. }
  9303. INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
  9304. void *ctx;
  9305. COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
  9306. if (sl)
  9307. COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  9308. if (item)
  9309. COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
  9310. char *res = REAL(sl_find)(sl, item);
  9311. if (res)
  9312. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  9313. return res;
  9314. }
  9315. INTERCEPTOR(void, sl_free, void *sl, int freeall) {
  9316. void *ctx;
  9317. COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
  9318. if (sl)
  9319. COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  9320. REAL(sl_free)(sl, freeall);
  9321. }
  9322. #define INIT_SL_INIT \
  9323. COMMON_INTERCEPT_FUNCTION(sl_init); \
  9324. COMMON_INTERCEPT_FUNCTION(sl_add); \
  9325. COMMON_INTERCEPT_FUNCTION(sl_find); \
  9326. COMMON_INTERCEPT_FUNCTION(sl_free);
  9327. #else
  9328. #define INIT_SL_INIT
  9329. #endif
  9330. #if SANITIZER_INTERCEPT_GETRANDOM
  9331. INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
  9332. void *ctx;
  9333. COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
  9334. SSIZE_T n = REAL(getrandom)(buf, buflen, flags);
  9335. if (n > 0) {
  9336. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
  9337. }
  9338. return n;
  9339. }
  9340. #define INIT_GETRANDOM COMMON_INTERCEPT_FUNCTION(getrandom)
  9341. #else
  9342. #define INIT_GETRANDOM
  9343. #endif
  9344. #if SANITIZER_INTERCEPT_CRYPT
  9345. INTERCEPTOR(char *, crypt, char *key, char *salt) {
  9346. void *ctx;
  9347. COMMON_INTERCEPTOR_ENTER(ctx, crypt, key, salt);
  9348. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, internal_strlen(key) + 1);
  9349. COMMON_INTERCEPTOR_READ_RANGE(ctx, salt, internal_strlen(salt) + 1);
  9350. char *res = REAL(crypt)(key, salt);
  9351. if (res != nullptr)
  9352. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  9353. return res;
  9354. }
  9355. #define INIT_CRYPT COMMON_INTERCEPT_FUNCTION(crypt);
  9356. #else
  9357. #define INIT_CRYPT
  9358. #endif
  9359. #if SANITIZER_INTERCEPT_CRYPT_R
  9360. INTERCEPTOR(char *, crypt_r, char *key, char *salt, void *data) {
  9361. void *ctx;
  9362. COMMON_INTERCEPTOR_ENTER(ctx, crypt_r, key, salt, data);
  9363. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, internal_strlen(key) + 1);
  9364. COMMON_INTERCEPTOR_READ_RANGE(ctx, salt, internal_strlen(salt) + 1);
  9365. char *res = REAL(crypt_r)(key, salt, data);
  9366. if (res != nullptr) {
  9367. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data,
  9368. __sanitizer::struct_crypt_data_sz);
  9369. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  9370. }
  9371. return res;
  9372. }
  9373. #define INIT_CRYPT_R COMMON_INTERCEPT_FUNCTION(crypt_r);
  9374. #else
  9375. #define INIT_CRYPT_R
  9376. #endif
  9377. #if SANITIZER_INTERCEPT_GETENTROPY
  9378. INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {
  9379. void *ctx;
  9380. COMMON_INTERCEPTOR_ENTER(ctx, getentropy, buf, buflen);
  9381. int r = REAL(getentropy)(buf, buflen);
  9382. if (r == 0) {
  9383. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
  9384. }
  9385. return r;
  9386. }
  9387. #define INIT_GETENTROPY COMMON_INTERCEPT_FUNCTION(getentropy)
  9388. #else
  9389. #define INIT_GETENTROPY
  9390. #endif
  9391. #if SANITIZER_INTERCEPT_QSORT_R
  9392. typedef int (*qsort_r_compar_f)(const void *, const void *, void *);
  9393. struct qsort_r_compar_params {
  9394. SIZE_T size;
  9395. qsort_r_compar_f compar;
  9396. void *arg;
  9397. };
  9398. static int wrapped_qsort_r_compar(const void *a, const void *b, void *arg) {
  9399. qsort_r_compar_params *params = (qsort_r_compar_params *)arg;
  9400. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9401. COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, params->size);
  9402. COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, params->size);
  9403. return params->compar(a, b, params->arg);
  9404. }
  9405. INTERCEPTOR(void, qsort_r, void *base, SIZE_T nmemb, SIZE_T size,
  9406. qsort_r_compar_f compar, void *arg) {
  9407. void *ctx;
  9408. COMMON_INTERCEPTOR_ENTER(ctx, qsort_r, base, nmemb, size, compar, arg);
  9409. // Run the comparator over all array elements to detect any memory issues.
  9410. if (nmemb > 1) {
  9411. for (SIZE_T i = 0; i < nmemb - 1; ++i) {
  9412. void *p = (void *)((char *)base + i * size);
  9413. void *q = (void *)((char *)base + (i + 1) * size);
  9414. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9415. compar(p, q, arg);
  9416. }
  9417. }
  9418. qsort_r_compar_params params = {size, compar, arg};
  9419. REAL(qsort_r)(base, nmemb, size, wrapped_qsort_r_compar, &params);
  9420. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
  9421. }
  9422. # define INIT_QSORT_R COMMON_INTERCEPT_FUNCTION(qsort_r)
  9423. #else
  9424. # define INIT_QSORT_R
  9425. #endif
  9426. #if SANITIZER_INTERCEPT_QSORT && SANITIZER_INTERCEPT_QSORT_R
  9427. INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
  9428. qsort_r_compar_f compar) {
  9429. void *ctx;
  9430. COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
  9431. WRAP(qsort_r)(base, nmemb, size, compar, nullptr);
  9432. }
  9433. # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
  9434. #elif SANITIZER_INTERCEPT_QSORT && !SANITIZER_INTERCEPT_QSORT_R
  9435. // Glibc qsort uses a temporary buffer allocated either on stack or on heap.
  9436. // Poisoned memory from there may get copied into the comparator arguments,
  9437. // where it needs to be dealt with. But even that is not enough - the results of
  9438. // the sort may be copied into the input/output array based on the results of
  9439. // the comparator calls, but directly from the temp memory, bypassing the
  9440. // unpoisoning done in wrapped_qsort_compar. We deal with this by, again,
  9441. // unpoisoning the entire array after the sort is done.
  9442. //
  9443. // We can not check that the entire array is initialized at the beginning. IMHO,
  9444. // it's fine for parts of the sorted objects to contain uninitialized memory,
  9445. // ex. as padding in structs.
  9446. typedef int (*qsort_compar_f)(const void *, const void *);
  9447. static THREADLOCAL qsort_compar_f qsort_compar;
  9448. static THREADLOCAL SIZE_T qsort_size;
  9449. static int wrapped_qsort_compar(const void *a, const void *b) {
  9450. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  9451. COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, qsort_size);
  9452. COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, qsort_size);
  9453. return qsort_compar(a, b);
  9454. }
  9455. INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
  9456. qsort_compar_f compar) {
  9457. void *ctx;
  9458. COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
  9459. // Run the comparator over all array elements to detect any memory issues.
  9460. if (nmemb > 1) {
  9461. for (SIZE_T i = 0; i < nmemb - 1; ++i) {
  9462. void *p = (void *)((char *)base + i * size);
  9463. void *q = (void *)((char *)base + (i + 1) * size);
  9464. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  9465. compar(p, q);
  9466. }
  9467. }
  9468. qsort_compar_f old_compar = qsort_compar;
  9469. SIZE_T old_size = qsort_size;
  9470. // Handle qsort() implementations that recurse using an
  9471. // interposable function call:
  9472. bool already_wrapped = compar == wrapped_qsort_compar;
  9473. if (already_wrapped) {
  9474. // This case should only happen if the qsort() implementation calls itself
  9475. // using a preemptible function call (e.g. the FreeBSD libc version).
  9476. // Check that the size and comparator arguments are as expected.
  9477. CHECK_NE(compar, qsort_compar);
  9478. CHECK_EQ(qsort_size, size);
  9479. } else {
  9480. qsort_compar = compar;
  9481. qsort_size = size;
  9482. }
  9483. REAL(qsort)(base, nmemb, size, wrapped_qsort_compar);
  9484. if (!already_wrapped) {
  9485. qsort_compar = old_compar;
  9486. qsort_size = old_size;
  9487. }
  9488. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
  9489. }
  9490. # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
  9491. #else
  9492. # define INIT_QSORT
  9493. #endif
  9494. #if SANITIZER_INTERCEPT_BSEARCH
  9495. typedef int (*bsearch_compar_f)(const void *, const void *);
  9496. struct bsearch_compar_params {
  9497. const void *key;
  9498. bsearch_compar_f compar;
  9499. };
  9500. static int wrapped_bsearch_compar(const void *key, const void *b) {
  9501. const bsearch_compar_params *params = (const bsearch_compar_params *)key;
  9502. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  9503. return params->compar(params->key, b);
  9504. }
  9505. INTERCEPTOR(void *, bsearch, const void *key, const void *base, SIZE_T nmemb,
  9506. SIZE_T size, bsearch_compar_f compar) {
  9507. void *ctx;
  9508. COMMON_INTERCEPTOR_ENTER(ctx, bsearch, key, base, nmemb, size, compar);
  9509. bsearch_compar_params params = {key, compar};
  9510. return REAL(bsearch)(&params, base, nmemb, size, wrapped_bsearch_compar);
  9511. }
  9512. # define INIT_BSEARCH COMMON_INTERCEPT_FUNCTION(bsearch)
  9513. #else
  9514. # define INIT_BSEARCH
  9515. #endif
  9516. #if SANITIZER_INTERCEPT_SIGALTSTACK
  9517. INTERCEPTOR(int, sigaltstack, void *ss, void *oss) {
  9518. void *ctx;
  9519. COMMON_INTERCEPTOR_ENTER(ctx, sigaltstack, ss, oss);
  9520. int r = REAL(sigaltstack)(ss, oss);
  9521. if (r == 0 && oss != nullptr) {
  9522. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oss, struct_stack_t_sz);
  9523. }
  9524. return r;
  9525. }
  9526. #define INIT_SIGALTSTACK COMMON_INTERCEPT_FUNCTION(sigaltstack)
  9527. #else
  9528. #define INIT_SIGALTSTACK
  9529. #endif
  9530. #if SANITIZER_INTERCEPT_PROCCTL
  9531. INTERCEPTOR(int, procctl, int idtype, u64 id, int cmd, uptr data) {
  9532. void *ctx;
  9533. COMMON_INTERCEPTOR_ENTER(ctx, procctl, idtype, id, cmd, data);
  9534. static const int PROC_REAP_ACQUIRE = 2;
  9535. static const int PROC_REAP_RELEASE = 3;
  9536. static const int PROC_REAP_STATUS = 4;
  9537. static const int PROC_REAP_GETPIDS = 5;
  9538. static const int PROC_REAP_KILL = 6;
  9539. if (cmd < PROC_REAP_ACQUIRE || cmd > PROC_REAP_KILL) {
  9540. COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, sizeof(int));
  9541. } else {
  9542. // reap_acquire/reap_release bears no arguments.
  9543. if (cmd > PROC_REAP_RELEASE) {
  9544. unsigned int reapsz;
  9545. switch (cmd) {
  9546. case PROC_REAP_STATUS:
  9547. reapsz = struct_procctl_reaper_status_sz;
  9548. break;
  9549. case PROC_REAP_GETPIDS:
  9550. reapsz = struct_procctl_reaper_pids_sz;
  9551. break;
  9552. case PROC_REAP_KILL:
  9553. reapsz = struct_procctl_reaper_kill_sz;
  9554. break;
  9555. }
  9556. COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)data, reapsz);
  9557. }
  9558. }
  9559. return REAL(procctl)(idtype, id, cmd, data);
  9560. }
  9561. #define INIT_PROCCTL COMMON_INTERCEPT_FUNCTION(procctl)
  9562. #else
  9563. #define INIT_PROCCTL
  9564. #endif
  9565. #if SANITIZER_INTERCEPT_UNAME
  9566. INTERCEPTOR(int, uname, struct utsname *utsname) {
  9567. #if SANITIZER_LINUX
  9568. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  9569. return internal_uname(utsname);
  9570. #endif
  9571. void *ctx;
  9572. COMMON_INTERCEPTOR_ENTER(ctx, uname, utsname);
  9573. int res = REAL(uname)(utsname);
  9574. if (!res)
  9575. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
  9576. __sanitizer::struct_utsname_sz);
  9577. return res;
  9578. }
  9579. #define INIT_UNAME COMMON_INTERCEPT_FUNCTION(uname)
  9580. #else
  9581. #define INIT_UNAME
  9582. #endif
  9583. #if SANITIZER_INTERCEPT___XUNAME
  9584. // FreeBSD's <sys/utsname.h> define uname() as
  9585. // static __inline int uname(struct utsname *name) {
  9586. // return __xuname(SYS_NMLN, (void*)name);
  9587. // }
  9588. INTERCEPTOR(int, __xuname, int size, void *utsname) {
  9589. void *ctx;
  9590. COMMON_INTERCEPTOR_ENTER(ctx, __xuname, size, utsname);
  9591. int res = REAL(__xuname)(size, utsname);
  9592. if (!res)
  9593. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
  9594. __sanitizer::struct_utsname_sz);
  9595. return res;
  9596. }
  9597. #define INIT___XUNAME COMMON_INTERCEPT_FUNCTION(__xuname)
  9598. #else
  9599. #define INIT___XUNAME
  9600. #endif
  9601. #if SANITIZER_INTERCEPT_HEXDUMP
  9602. INTERCEPTOR(void, hexdump, const void *ptr, int length, const char *header, int flags) {
  9603. void *ctx;
  9604. COMMON_INTERCEPTOR_ENTER(ctx, hexdump, ptr, length, header, flags);
  9605. COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, length);
  9606. COMMON_INTERCEPTOR_READ_RANGE(ctx, header, internal_strlen(header) + 1);
  9607. REAL(hexdump)(ptr, length, header, flags);
  9608. }
  9609. #define INIT_HEXDUMP COMMON_INTERCEPT_FUNCTION(hexdump);
  9610. #else
  9611. #define INIT_HEXDUMP
  9612. #endif
  9613. #include "sanitizer_common_interceptors_netbsd_compat.inc"
  9614. static void InitializeCommonInterceptors() {
  9615. #if SI_POSIX
  9616. static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
  9617. interceptor_metadata_map = new ((void *)&metadata_mem) MetadataHashMap();
  9618. #endif
  9619. INIT_MMAP;
  9620. INIT_MMAP64;
  9621. INIT_TEXTDOMAIN;
  9622. INIT_STRLEN;
  9623. INIT_STRNLEN;
  9624. INIT_STRNDUP;
  9625. INIT___STRNDUP;
  9626. INIT_STRCMP;
  9627. INIT_STRNCMP;
  9628. INIT_STRCASECMP;
  9629. INIT_STRNCASECMP;
  9630. INIT_STRSTR;
  9631. INIT_STRCASESTR;
  9632. INIT_STRCHR;
  9633. INIT_STRCHRNUL;
  9634. INIT_STRRCHR;
  9635. INIT_STRSPN;
  9636. INIT_STRTOK;
  9637. INIT_STRPBRK;
  9638. INIT_STRXFRM;
  9639. INIT___STRXFRM_L;
  9640. INIT_MEMSET;
  9641. INIT_MEMMOVE;
  9642. INIT_MEMCPY;
  9643. INIT_MEMCHR;
  9644. INIT_MEMCMP;
  9645. INIT_BCMP;
  9646. INIT_MEMRCHR;
  9647. INIT_MEMMEM;
  9648. INIT_READ;
  9649. INIT_FREAD;
  9650. INIT_PREAD;
  9651. INIT_PREAD64;
  9652. INIT_READV;
  9653. INIT_PREADV;
  9654. INIT_PREADV64;
  9655. INIT_WRITE;
  9656. INIT_FWRITE;
  9657. INIT_PWRITE;
  9658. INIT_PWRITE64;
  9659. INIT_WRITEV;
  9660. INIT_PWRITEV;
  9661. INIT_PWRITEV64;
  9662. INIT_FGETS;
  9663. INIT_FPUTS;
  9664. INIT_PUTS;
  9665. INIT_PRCTL;
  9666. INIT_LOCALTIME_AND_FRIENDS;
  9667. INIT_STRPTIME;
  9668. INIT_SCANF;
  9669. INIT_ISOC99_SCANF;
  9670. INIT_PRINTF;
  9671. INIT_PRINTF_L;
  9672. INIT_ISOC99_PRINTF;
  9673. INIT_FREXP;
  9674. INIT_FREXPF_FREXPL;
  9675. INIT_GETPWNAM_AND_FRIENDS;
  9676. INIT_GETPWNAM_R_AND_FRIENDS;
  9677. INIT_GETPWENT;
  9678. INIT_FGETPWENT;
  9679. INIT_GETPWENT_R;
  9680. INIT_FGETPWENT_R;
  9681. INIT_FGETGRENT_R;
  9682. INIT_SETPWENT;
  9683. INIT_CLOCK_GETTIME;
  9684. INIT_CLOCK_GETCPUCLOCKID;
  9685. INIT_GETITIMER;
  9686. INIT_TIME;
  9687. INIT_GLOB;
  9688. INIT_GLOB64;
  9689. INIT___B64_TO;
  9690. INIT_DN_COMP_EXPAND;
  9691. INIT_POSIX_SPAWN;
  9692. INIT_WAIT;
  9693. INIT_WAIT4;
  9694. INIT_INET;
  9695. INIT_PTHREAD_GETSCHEDPARAM;
  9696. INIT_GETADDRINFO;
  9697. INIT_GETNAMEINFO;
  9698. INIT_GETSOCKNAME;
  9699. INIT_GETHOSTBYNAME;
  9700. INIT_GETHOSTBYNAME2;
  9701. INIT_GETHOSTBYNAME_R;
  9702. INIT_GETHOSTBYNAME2_R;
  9703. INIT_GETHOSTBYADDR_R;
  9704. INIT_GETHOSTENT_R;
  9705. INIT_GETSOCKOPT;
  9706. INIT_ACCEPT;
  9707. INIT_ACCEPT4;
  9708. INIT_PACCEPT;
  9709. INIT_MODF;
  9710. INIT_RECVMSG;
  9711. INIT_SENDMSG;
  9712. INIT_RECVMMSG;
  9713. INIT_SENDMMSG;
  9714. INIT_SYSMSG;
  9715. INIT_GETPEERNAME;
  9716. INIT_IOCTL;
  9717. INIT_INET_ATON;
  9718. INIT_SYSINFO;
  9719. INIT_READDIR;
  9720. INIT_READDIR64;
  9721. INIT_PTRACE;
  9722. INIT_SETLOCALE;
  9723. INIT_GETCWD;
  9724. INIT_GET_CURRENT_DIR_NAME;
  9725. INIT_STRTOIMAX;
  9726. INIT_MBSTOWCS;
  9727. INIT_MBSNRTOWCS;
  9728. INIT_WCSTOMBS;
  9729. INIT_WCSNRTOMBS;
  9730. INIT_WCRTOMB;
  9731. INIT_WCTOMB;
  9732. INIT_TCGETATTR;
  9733. INIT_REALPATH;
  9734. INIT_CANONICALIZE_FILE_NAME;
  9735. INIT_CONFSTR;
  9736. INIT_SCHED_GETAFFINITY;
  9737. INIT_SCHED_GETPARAM;
  9738. INIT_STRERROR;
  9739. INIT_STRERROR_R;
  9740. INIT_XPG_STRERROR_R;
  9741. INIT_SCANDIR;
  9742. INIT_SCANDIR64;
  9743. INIT_GETGROUPS;
  9744. INIT_POLL;
  9745. INIT_PPOLL;
  9746. INIT_WORDEXP;
  9747. INIT_SIGWAIT;
  9748. INIT_SIGWAITINFO;
  9749. INIT_SIGTIMEDWAIT;
  9750. INIT_SIGSETOPS;
  9751. INIT_SIGSET_LOGICOPS;
  9752. INIT_SIGPENDING;
  9753. INIT_SIGPROCMASK;
  9754. INIT_PTHREAD_SIGMASK;
  9755. INIT_BACKTRACE;
  9756. INIT__EXIT;
  9757. INIT___LIBC_THR_SETCANCELSTATE;
  9758. INIT_GETMNTENT;
  9759. INIT_GETMNTENT_R;
  9760. INIT_STATFS;
  9761. INIT_STATFS64;
  9762. INIT_STATVFS;
  9763. INIT_STATVFS64;
  9764. INIT_INITGROUPS;
  9765. INIT_ETHER_NTOA_ATON;
  9766. INIT_ETHER_HOST;
  9767. INIT_ETHER_R;
  9768. INIT_SHMCTL;
  9769. INIT_RANDOM_R;
  9770. INIT_PTHREAD_ATTR_GET;
  9771. INIT_PTHREAD_ATTR_GET_SCHED;
  9772. INIT_PTHREAD_ATTR_GETINHERITSCHED;
  9773. INIT_PTHREAD_ATTR_GETAFFINITY_NP;
  9774. INIT_PTHREAD_GETAFFINITY_NP;
  9775. INIT_PTHREAD_MUTEXATTR_GETPSHARED;
  9776. INIT_PTHREAD_MUTEXATTR_GETTYPE;
  9777. INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
  9778. INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
  9779. INIT_PTHREAD_MUTEXATTR_GETROBUST;
  9780. INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
  9781. INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
  9782. INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
  9783. INIT_PTHREAD_CONDATTR_GETPSHARED;
  9784. INIT_PTHREAD_CONDATTR_GETCLOCK;
  9785. INIT_PTHREAD_BARRIERATTR_GETPSHARED;
  9786. INIT_TMPNAM;
  9787. INIT_TMPNAM_R;
  9788. INIT_PTSNAME;
  9789. INIT_PTSNAME_R;
  9790. INIT_TTYNAME;
  9791. INIT_TTYNAME_R;
  9792. INIT_TEMPNAM;
  9793. INIT_PTHREAD_SETNAME_NP;
  9794. INIT_PTHREAD_GETNAME_NP;
  9795. INIT_SINCOS;
  9796. INIT_REMQUO;
  9797. INIT_REMQUOL;
  9798. INIT_LGAMMA;
  9799. INIT_LGAMMAL;
  9800. INIT_LGAMMA_R;
  9801. INIT_LGAMMAL_R;
  9802. INIT_DRAND48_R;
  9803. INIT_RAND_R;
  9804. INIT_GETLINE;
  9805. INIT_ICONV;
  9806. INIT_TIMES;
  9807. INIT_TLS_GET_ADDR;
  9808. INIT_LISTXATTR;
  9809. INIT_GETXATTR;
  9810. INIT_GETRESID;
  9811. INIT_GETIFADDRS;
  9812. INIT_IF_INDEXTONAME;
  9813. INIT_CAPGET;
  9814. INIT_AEABI_MEM;
  9815. INIT___BZERO;
  9816. INIT_BZERO;
  9817. INIT_FTIME;
  9818. INIT_XDR;
  9819. INIT_XDRREC_LINUX;
  9820. INIT_TSEARCH;
  9821. INIT_LIBIO_INTERNALS;
  9822. INIT_FOPEN;
  9823. INIT_FOPEN64;
  9824. INIT_FLOPEN;
  9825. INIT_OPEN_MEMSTREAM;
  9826. INIT_OBSTACK;
  9827. INIT_FFLUSH;
  9828. INIT_FCLOSE;
  9829. INIT_DLOPEN_DLCLOSE;
  9830. INIT_GETPASS;
  9831. INIT_TIMERFD;
  9832. INIT_MLOCKX;
  9833. INIT_FOPENCOOKIE;
  9834. INIT_SEM;
  9835. INIT_PTHREAD_SETCANCEL;
  9836. INIT_MINCORE;
  9837. INIT_PROCESS_VM_READV;
  9838. INIT_CTERMID;
  9839. INIT_CTERMID_R;
  9840. INIT_RECV_RECVFROM;
  9841. INIT_SEND_SENDTO;
  9842. INIT_STAT;
  9843. INIT_STAT64;
  9844. INIT_EVENTFD_READ_WRITE;
  9845. INIT_LSTAT;
  9846. INIT_LSTAT64;
  9847. INIT___XSTAT;
  9848. INIT___XSTAT64;
  9849. INIT___LXSTAT;
  9850. INIT___LXSTAT64;
  9851. // FIXME: add other *stat interceptors.
  9852. INIT_UTMP;
  9853. INIT_UTMPX;
  9854. INIT_GETLOADAVG;
  9855. INIT_WCSLEN;
  9856. INIT_WCSCAT;
  9857. INIT_WCSDUP;
  9858. INIT_WCSXFRM;
  9859. INIT___WCSXFRM_L;
  9860. INIT_ACCT;
  9861. INIT_USER_FROM_UID;
  9862. INIT_UID_FROM_USER;
  9863. INIT_GROUP_FROM_GID;
  9864. INIT_GID_FROM_GROUP;
  9865. INIT_ACCESS;
  9866. INIT_FACCESSAT;
  9867. INIT_GETGROUPLIST;
  9868. INIT_GETGROUPMEMBERSHIP;
  9869. INIT_READLINK;
  9870. INIT_READLINKAT;
  9871. INIT_NAME_TO_HANDLE_AT;
  9872. INIT_OPEN_BY_HANDLE_AT;
  9873. INIT_STRLCPY;
  9874. INIT_DEVNAME;
  9875. INIT_DEVNAME_R;
  9876. INIT_FGETLN;
  9877. INIT_STRMODE;
  9878. INIT_TTYENT;
  9879. INIT_PROTOENT;
  9880. INIT_PROTOENT_R;
  9881. INIT_NETENT;
  9882. INIT_GETMNTINFO;
  9883. INIT_MI_VECTOR_HASH;
  9884. INIT_SETVBUF;
  9885. INIT_GETVFSSTAT;
  9886. INIT_REGEX;
  9887. INIT_REGEXSUB;
  9888. INIT_FTS;
  9889. INIT_SYSCTL;
  9890. INIT_ASYSCTL;
  9891. INIT_SYSCTLGETMIBINFO;
  9892. INIT_NL_LANGINFO;
  9893. INIT_MODCTL;
  9894. INIT_STRTONUM;
  9895. INIT_FPARSELN;
  9896. INIT_STATVFS1;
  9897. INIT_STRTOI;
  9898. INIT_CAPSICUM;
  9899. INIT_SHA1;
  9900. INIT_MD4;
  9901. INIT_RMD160;
  9902. INIT_MD5;
  9903. INIT_FSEEK;
  9904. INIT_MD2;
  9905. INIT_SHA2;
  9906. INIT_VIS;
  9907. INIT_CDB;
  9908. INIT_GETFSENT;
  9909. INIT_ARC4RANDOM;
  9910. INIT_POPEN;
  9911. INIT_POPENVE;
  9912. INIT_PCLOSE;
  9913. INIT_FUNOPEN;
  9914. INIT_FUNOPEN2;
  9915. INIT_FDEVNAME;
  9916. INIT_GETUSERSHELL;
  9917. INIT_SL_INIT;
  9918. INIT_GETRANDOM;
  9919. INIT_CRYPT;
  9920. INIT_CRYPT_R;
  9921. INIT_GETENTROPY;
  9922. INIT_QSORT;
  9923. INIT_QSORT_R;
  9924. INIT_BSEARCH;
  9925. INIT_SIGALTSTACK;
  9926. INIT_PROCCTL
  9927. INIT_UNAME;
  9928. INIT___XUNAME;
  9929. INIT_HEXDUMP;
  9930. INIT___PRINTF_CHK;
  9931. }