sanitizer_common_interceptors.inc 175 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896
  1. //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // Common function interceptors for tools like AddressSanitizer,
  9. // ThreadSanitizer, MemorySanitizer, etc.
  10. //
  11. // This file should be included into the tool's interceptor file,
  12. // which has to define it's own macros:
  13. // COMMON_INTERCEPTOR_ENTER
  14. // COMMON_INTERCEPTOR_ENTER_NOIGNORE
  15. // COMMON_INTERCEPTOR_READ_RANGE
  16. // COMMON_INTERCEPTOR_WRITE_RANGE
  17. // COMMON_INTERCEPTOR_INITIALIZE_RANGE
  18. // COMMON_INTERCEPTOR_FD_ACQUIRE
  19. // COMMON_INTERCEPTOR_FD_RELEASE
  20. // COMMON_INTERCEPTOR_FD_ACCESS
  21. // COMMON_INTERCEPTOR_SET_THREAD_NAME
  22. // COMMON_INTERCEPTOR_ON_EXIT
  23. // COMMON_INTERCEPTOR_MUTEX_LOCK
  24. // COMMON_INTERCEPTOR_MUTEX_UNLOCK
  25. // COMMON_INTERCEPTOR_MUTEX_REPAIR
  26. // COMMON_INTERCEPTOR_SET_PTHREAD_NAME
  27. // COMMON_INTERCEPTOR_HANDLE_RECVMSG
  28. // COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
  29. //===----------------------------------------------------------------------===//
  30. #include "interception/interception.h"
  31. #include "sanitizer_addrhashmap.h"
  32. #include "sanitizer_placement_new.h"
  33. #include "sanitizer_platform_interceptors.h"
  34. #include "sanitizer_tls_get_addr.h"
  35. #include <stdarg.h>
  36. #if SANITIZER_WINDOWS && !defined(va_copy)
  37. #define va_copy(dst, src) ((dst) = (src))
  38. #endif // _WIN32
  39. #if SANITIZER_FREEBSD
  40. #define pthread_setname_np pthread_set_name_np
  41. #endif
  42. #ifndef COMMON_INTERCEPTOR_INITIALIZE_RANGE
  43. #define COMMON_INTERCEPTOR_INITIALIZE_RANGE(p, size) {}
  44. #endif
  45. #ifndef COMMON_INTERCEPTOR_UNPOISON_PARAM
  46. #define COMMON_INTERCEPTOR_UNPOISON_PARAM(count) {}
  47. #endif
  48. #ifndef COMMON_INTERCEPTOR_FD_ACCESS
  49. #define COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd) {}
  50. #endif
  51. #ifndef COMMON_INTERCEPTOR_MUTEX_LOCK
  52. #define COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m) {}
  53. #endif
  54. #ifndef COMMON_INTERCEPTOR_MUTEX_UNLOCK
  55. #define COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m) {}
  56. #endif
  57. #ifndef COMMON_INTERCEPTOR_MUTEX_REPAIR
  58. #define COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m) {}
  59. #endif
  60. #ifndef COMMON_INTERCEPTOR_HANDLE_RECVMSG
  61. #define COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg) ((void)(msg))
  62. #endif
  63. #ifndef COMMON_INTERCEPTOR_FILE_OPEN
  64. #define COMMON_INTERCEPTOR_FILE_OPEN(ctx, file, path) {}
  65. #endif
  66. #ifndef COMMON_INTERCEPTOR_FILE_CLOSE
  67. #define COMMON_INTERCEPTOR_FILE_CLOSE(ctx, file) {}
  68. #endif
  69. #ifndef COMMON_INTERCEPTOR_LIBRARY_LOADED
  70. #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, map) {}
  71. #endif
  72. #ifndef COMMON_INTERCEPTOR_LIBRARY_UNLOADED
  73. #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() {}
  74. #endif
  75. #ifndef COMMON_INTERCEPTOR_ENTER_NOIGNORE
  76. #define COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, ...) \
  77. COMMON_INTERCEPTOR_ENTER(ctx, __VA_ARGS__)
  78. #endif
  79. #ifndef COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED
  80. #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (0)
  81. #endif
  82. struct FileMetadata {
  83. // For open_memstream().
  84. char **addr;
  85. SIZE_T *size;
  86. };
  87. struct CommonInterceptorMetadata {
  88. enum {
  89. CIMT_INVALID = 0,
  90. CIMT_FILE
  91. } type;
  92. union {
  93. FileMetadata file;
  94. };
  95. };
  96. typedef AddrHashMap<CommonInterceptorMetadata, 31051> MetadataHashMap;
  97. static MetadataHashMap *interceptor_metadata_map;
  98. #if SI_NOT_WINDOWS
  99. UNUSED static void SetInterceptorMetadata(__sanitizer_FILE *addr,
  100. const FileMetadata &file) {
  101. MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr);
  102. CHECK(h.created());
  103. h->type = CommonInterceptorMetadata::CIMT_FILE;
  104. h->file = file;
  105. }
  106. UNUSED static const FileMetadata *GetInterceptorMetadata(
  107. __sanitizer_FILE *addr) {
  108. MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr,
  109. /* remove */ false,
  110. /* create */ false);
  111. if (h.exists()) {
  112. CHECK(!h.created());
  113. CHECK(h->type == CommonInterceptorMetadata::CIMT_FILE);
  114. return &h->file;
  115. } else {
  116. return 0;
  117. }
  118. }
  119. UNUSED static void DeleteInterceptorMetadata(void *addr) {
  120. MetadataHashMap::Handle h(interceptor_metadata_map, (uptr)addr, true);
  121. CHECK(h.exists());
  122. }
  123. #endif // SI_NOT_WINDOWS
  124. #if SANITIZER_INTERCEPT_TEXTDOMAIN
  125. INTERCEPTOR(char*, textdomain, const char *domainname) {
  126. void *ctx;
  127. COMMON_INTERCEPTOR_ENTER(ctx, textdomain, domainname);
  128. char* domain = REAL(textdomain)(domainname);
  129. if (domain) {
  130. COMMON_INTERCEPTOR_INITIALIZE_RANGE(domain, REAL(strlen)(domain) + 1);
  131. }
  132. return domain;
  133. }
  134. #define INIT_TEXTDOMAIN COMMON_INTERCEPT_FUNCTION(textdomain)
  135. #else
  136. #define INIT_TEXTDOMAIN
  137. #endif
  138. #if SANITIZER_INTERCEPT_STRCMP
  139. static inline int CharCmpX(unsigned char c1, unsigned char c2) {
  140. return (c1 == c2) ? 0 : (c1 < c2) ? -1 : 1;
  141. }
  142. INTERCEPTOR(int, strcmp, const char *s1, const char *s2) {
  143. void *ctx;
  144. COMMON_INTERCEPTOR_ENTER(ctx, strcmp, s1, s2);
  145. unsigned char c1, c2;
  146. uptr i;
  147. for (i = 0;; i++) {
  148. c1 = (unsigned char)s1[i];
  149. c2 = (unsigned char)s2[i];
  150. if (c1 != c2 || c1 == '\0') break;
  151. }
  152. COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
  153. COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
  154. return CharCmpX(c1, c2);
  155. }
  156. INTERCEPTOR(int, strncmp, const char *s1, const char *s2, uptr size) {
  157. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  158. return internal_strncmp(s1, s2, size);
  159. void *ctx;
  160. COMMON_INTERCEPTOR_ENTER(ctx, strncmp, s1, s2, size);
  161. unsigned char c1 = 0, c2 = 0;
  162. uptr i;
  163. for (i = 0; i < size; i++) {
  164. c1 = (unsigned char)s1[i];
  165. c2 = (unsigned char)s2[i];
  166. if (c1 != c2 || c1 == '\0') break;
  167. }
  168. COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, size));
  169. COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, size));
  170. return CharCmpX(c1, c2);
  171. }
  172. #define INIT_STRCMP COMMON_INTERCEPT_FUNCTION(strcmp)
  173. #define INIT_STRNCMP COMMON_INTERCEPT_FUNCTION(strncmp)
  174. #else
  175. #define INIT_STRCMP
  176. #define INIT_STRNCMP
  177. #endif
  178. #if SANITIZER_INTERCEPT_STRCASECMP
  179. static inline int CharCaseCmp(unsigned char c1, unsigned char c2) {
  180. int c1_low = ToLower(c1);
  181. int c2_low = ToLower(c2);
  182. return c1_low - c2_low;
  183. }
  184. INTERCEPTOR(int, strcasecmp, const char *s1, const char *s2) {
  185. void *ctx;
  186. COMMON_INTERCEPTOR_ENTER(ctx, strcasecmp, s1, s2);
  187. unsigned char c1 = 0, c2 = 0;
  188. uptr i;
  189. for (i = 0;; i++) {
  190. c1 = (unsigned char)s1[i];
  191. c2 = (unsigned char)s2[i];
  192. if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
  193. }
  194. COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, i + 1);
  195. COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, i + 1);
  196. return CharCaseCmp(c1, c2);
  197. }
  198. INTERCEPTOR(int, strncasecmp, const char *s1, const char *s2, SIZE_T n) {
  199. void *ctx;
  200. COMMON_INTERCEPTOR_ENTER(ctx, strncasecmp, s1, s2, n);
  201. unsigned char c1 = 0, c2 = 0;
  202. uptr i;
  203. for (i = 0; i < n; i++) {
  204. c1 = (unsigned char)s1[i];
  205. c2 = (unsigned char)s2[i];
  206. if (CharCaseCmp(c1, c2) != 0 || c1 == '\0') break;
  207. }
  208. COMMON_INTERCEPTOR_READ_RANGE(ctx, s1, Min(i + 1, n));
  209. COMMON_INTERCEPTOR_READ_RANGE(ctx, s2, Min(i + 1, n));
  210. return CharCaseCmp(c1, c2);
  211. }
  212. #define INIT_STRCASECMP COMMON_INTERCEPT_FUNCTION(strcasecmp)
  213. #define INIT_STRNCASECMP COMMON_INTERCEPT_FUNCTION(strncasecmp)
  214. #else
  215. #define INIT_STRCASECMP
  216. #define INIT_STRNCASECMP
  217. #endif
  218. #if SANITIZER_INTERCEPT_MEMCHR
  219. INTERCEPTOR(void*, memchr, const void *s, int c, SIZE_T n) {
  220. void *ctx;
  221. COMMON_INTERCEPTOR_ENTER(ctx, memchr, s, c, n);
  222. void *res = REAL(memchr)(s, c, n);
  223. uptr len = res ? (char*)res - (char*)s + 1 : n;
  224. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, len);
  225. return res;
  226. }
  227. #define INIT_MEMCHR COMMON_INTERCEPT_FUNCTION(memchr)
  228. #else
  229. #define INIT_MEMCHR
  230. #endif
  231. #if SANITIZER_INTERCEPT_MEMRCHR
  232. INTERCEPTOR(void*, memrchr, const void *s, int c, SIZE_T n) {
  233. void *ctx;
  234. COMMON_INTERCEPTOR_ENTER(ctx, memrchr, s, c, n);
  235. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, n);
  236. return REAL(memrchr)(s, c, n);
  237. }
  238. #define INIT_MEMRCHR COMMON_INTERCEPT_FUNCTION(memrchr)
  239. #else
  240. #define INIT_MEMRCHR
  241. #endif
  242. #if SANITIZER_INTERCEPT_FREXP
  243. INTERCEPTOR(double, frexp, double x, int *exp) {
  244. void *ctx;
  245. COMMON_INTERCEPTOR_ENTER(ctx, frexp, x, exp);
  246. // Assuming frexp() always writes to |exp|.
  247. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
  248. double res = REAL(frexp)(x, exp);
  249. return res;
  250. }
  251. #define INIT_FREXP COMMON_INTERCEPT_FUNCTION(frexp);
  252. #else
  253. #define INIT_FREXP
  254. #endif // SANITIZER_INTERCEPT_FREXP
  255. #if SANITIZER_INTERCEPT_FREXPF_FREXPL
  256. INTERCEPTOR(float, frexpf, float x, int *exp) {
  257. void *ctx;
  258. COMMON_INTERCEPTOR_ENTER(ctx, frexpf, x, exp);
  259. // FIXME: under ASan the call below may write to freed memory and corrupt
  260. // its metadata. See
  261. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  262. float res = REAL(frexpf)(x, exp);
  263. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
  264. return res;
  265. }
  266. INTERCEPTOR(long double, frexpl, long double x, int *exp) {
  267. void *ctx;
  268. COMMON_INTERCEPTOR_ENTER(ctx, frexpl, x, exp);
  269. // FIXME: under ASan the call below may write to freed memory and corrupt
  270. // its metadata. See
  271. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  272. long double res = REAL(frexpl)(x, exp);
  273. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, exp, sizeof(*exp));
  274. return res;
  275. }
  276. #define INIT_FREXPF_FREXPL \
  277. COMMON_INTERCEPT_FUNCTION(frexpf); \
  278. COMMON_INTERCEPT_FUNCTION(frexpl)
  279. #else
  280. #define INIT_FREXPF_FREXPL
  281. #endif // SANITIZER_INTERCEPT_FREXPF_FREXPL
  282. #if SI_NOT_WINDOWS
  283. static void write_iovec(void *ctx, struct __sanitizer_iovec *iovec,
  284. SIZE_T iovlen, SIZE_T maxlen) {
  285. for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
  286. SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
  287. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iovec[i].iov_base, sz);
  288. maxlen -= sz;
  289. }
  290. }
  291. static void read_iovec(void *ctx, struct __sanitizer_iovec *iovec,
  292. SIZE_T iovlen, SIZE_T maxlen) {
  293. COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec, sizeof(*iovec) * iovlen);
  294. for (SIZE_T i = 0; i < iovlen && maxlen; ++i) {
  295. SSIZE_T sz = Min(iovec[i].iov_len, maxlen);
  296. COMMON_INTERCEPTOR_READ_RANGE(ctx, iovec[i].iov_base, sz);
  297. maxlen -= sz;
  298. }
  299. }
  300. #endif
  301. #if SANITIZER_INTERCEPT_READ
  302. INTERCEPTOR(SSIZE_T, read, int fd, void *ptr, SIZE_T count) {
  303. void *ctx;
  304. COMMON_INTERCEPTOR_ENTER(ctx, read, fd, ptr, count);
  305. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  306. // FIXME: under ASan the call below may write to freed memory and corrupt
  307. // its metadata. See
  308. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  309. SSIZE_T res = REAL(read)(fd, ptr, count);
  310. if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
  311. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  312. return res;
  313. }
  314. #define INIT_READ COMMON_INTERCEPT_FUNCTION(read)
  315. #else
  316. #define INIT_READ
  317. #endif
  318. #if SANITIZER_INTERCEPT_PREAD
  319. INTERCEPTOR(SSIZE_T, pread, int fd, void *ptr, SIZE_T count, OFF_T offset) {
  320. void *ctx;
  321. COMMON_INTERCEPTOR_ENTER(ctx, pread, fd, ptr, count, offset);
  322. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  323. // FIXME: under ASan the call below may write to freed memory and corrupt
  324. // its metadata. See
  325. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  326. SSIZE_T res = REAL(pread)(fd, ptr, count, offset);
  327. if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
  328. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  329. return res;
  330. }
  331. #define INIT_PREAD COMMON_INTERCEPT_FUNCTION(pread)
  332. #else
  333. #define INIT_PREAD
  334. #endif
  335. #if SANITIZER_INTERCEPT_PREAD64
  336. INTERCEPTOR(SSIZE_T, pread64, int fd, void *ptr, SIZE_T count, OFF64_T offset) {
  337. void *ctx;
  338. COMMON_INTERCEPTOR_ENTER(ctx, pread64, fd, ptr, count, offset);
  339. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  340. // FIXME: under ASan the call below may write to freed memory and corrupt
  341. // its metadata. See
  342. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  343. SSIZE_T res = REAL(pread64)(fd, ptr, count, offset);
  344. if (res > 0) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, res);
  345. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  346. return res;
  347. }
  348. #define INIT_PREAD64 COMMON_INTERCEPT_FUNCTION(pread64)
  349. #else
  350. #define INIT_PREAD64
  351. #endif
  352. #if SANITIZER_INTERCEPT_READV
  353. INTERCEPTOR_WITH_SUFFIX(SSIZE_T, readv, int fd, __sanitizer_iovec *iov,
  354. int iovcnt) {
  355. void *ctx;
  356. COMMON_INTERCEPTOR_ENTER(ctx, readv, fd, iov, iovcnt);
  357. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  358. SSIZE_T res = REAL(readv)(fd, iov, iovcnt);
  359. if (res > 0) write_iovec(ctx, iov, iovcnt, res);
  360. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  361. return res;
  362. }
  363. #define INIT_READV COMMON_INTERCEPT_FUNCTION(readv)
  364. #else
  365. #define INIT_READV
  366. #endif
  367. #if SANITIZER_INTERCEPT_PREADV
  368. INTERCEPTOR(SSIZE_T, preadv, int fd, __sanitizer_iovec *iov, int iovcnt,
  369. OFF_T offset) {
  370. void *ctx;
  371. COMMON_INTERCEPTOR_ENTER(ctx, preadv, fd, iov, iovcnt, offset);
  372. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  373. SSIZE_T res = REAL(preadv)(fd, iov, iovcnt, offset);
  374. if (res > 0) write_iovec(ctx, iov, iovcnt, res);
  375. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  376. return res;
  377. }
  378. #define INIT_PREADV COMMON_INTERCEPT_FUNCTION(preadv)
  379. #else
  380. #define INIT_PREADV
  381. #endif
  382. #if SANITIZER_INTERCEPT_PREADV64
  383. INTERCEPTOR(SSIZE_T, preadv64, int fd, __sanitizer_iovec *iov, int iovcnt,
  384. OFF64_T offset) {
  385. void *ctx;
  386. COMMON_INTERCEPTOR_ENTER(ctx, preadv64, fd, iov, iovcnt, offset);
  387. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  388. SSIZE_T res = REAL(preadv64)(fd, iov, iovcnt, offset);
  389. if (res > 0) write_iovec(ctx, iov, iovcnt, res);
  390. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  391. return res;
  392. }
  393. #define INIT_PREADV64 COMMON_INTERCEPT_FUNCTION(preadv64)
  394. #else
  395. #define INIT_PREADV64
  396. #endif
  397. #if SANITIZER_INTERCEPT_WRITE
  398. INTERCEPTOR(SSIZE_T, write, int fd, void *ptr, SIZE_T count) {
  399. void *ctx;
  400. COMMON_INTERCEPTOR_ENTER(ctx, write, fd, ptr, count);
  401. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  402. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  403. SSIZE_T res = REAL(write)(fd, ptr, count);
  404. // FIXME: this check should be _before_ the call to REAL(write), not after
  405. if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
  406. return res;
  407. }
  408. #define INIT_WRITE COMMON_INTERCEPT_FUNCTION(write)
  409. #else
  410. #define INIT_WRITE
  411. #endif
  412. #if SANITIZER_INTERCEPT_PWRITE
  413. INTERCEPTOR(SSIZE_T, pwrite, int fd, void *ptr, SIZE_T count, OFF_T offset) {
  414. void *ctx;
  415. COMMON_INTERCEPTOR_ENTER(ctx, pwrite, fd, ptr, count, offset);
  416. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  417. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  418. SSIZE_T res = REAL(pwrite)(fd, ptr, count, offset);
  419. if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
  420. return res;
  421. }
  422. #define INIT_PWRITE COMMON_INTERCEPT_FUNCTION(pwrite)
  423. #else
  424. #define INIT_PWRITE
  425. #endif
  426. #if SANITIZER_INTERCEPT_PWRITE64
  427. INTERCEPTOR(SSIZE_T, pwrite64, int fd, void *ptr, OFF64_T count,
  428. OFF64_T offset) {
  429. void *ctx;
  430. COMMON_INTERCEPTOR_ENTER(ctx, pwrite64, fd, ptr, count, offset);
  431. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  432. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  433. SSIZE_T res = REAL(pwrite64)(fd, ptr, count, offset);
  434. if (res > 0) COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, res);
  435. return res;
  436. }
  437. #define INIT_PWRITE64 COMMON_INTERCEPT_FUNCTION(pwrite64)
  438. #else
  439. #define INIT_PWRITE64
  440. #endif
  441. #if SANITIZER_INTERCEPT_WRITEV
  442. INTERCEPTOR_WITH_SUFFIX(SSIZE_T, writev, int fd, __sanitizer_iovec *iov,
  443. int iovcnt) {
  444. void *ctx;
  445. COMMON_INTERCEPTOR_ENTER(ctx, writev, fd, iov, iovcnt);
  446. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  447. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  448. SSIZE_T res = REAL(writev)(fd, iov, iovcnt);
  449. if (res > 0) read_iovec(ctx, iov, iovcnt, res);
  450. return res;
  451. }
  452. #define INIT_WRITEV COMMON_INTERCEPT_FUNCTION(writev)
  453. #else
  454. #define INIT_WRITEV
  455. #endif
  456. #if SANITIZER_INTERCEPT_PWRITEV
  457. INTERCEPTOR(SSIZE_T, pwritev, int fd, __sanitizer_iovec *iov, int iovcnt,
  458. OFF_T offset) {
  459. void *ctx;
  460. COMMON_INTERCEPTOR_ENTER(ctx, pwritev, fd, iov, iovcnt, offset);
  461. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  462. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  463. SSIZE_T res = REAL(pwritev)(fd, iov, iovcnt, offset);
  464. if (res > 0) read_iovec(ctx, iov, iovcnt, res);
  465. return res;
  466. }
  467. #define INIT_PWRITEV COMMON_INTERCEPT_FUNCTION(pwritev)
  468. #else
  469. #define INIT_PWRITEV
  470. #endif
  471. #if SANITIZER_INTERCEPT_PWRITEV64
  472. INTERCEPTOR(SSIZE_T, pwritev64, int fd, __sanitizer_iovec *iov, int iovcnt,
  473. OFF64_T offset) {
  474. void *ctx;
  475. COMMON_INTERCEPTOR_ENTER(ctx, pwritev64, fd, iov, iovcnt, offset);
  476. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  477. if (fd >= 0) COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  478. SSIZE_T res = REAL(pwritev64)(fd, iov, iovcnt, offset);
  479. if (res > 0) read_iovec(ctx, iov, iovcnt, res);
  480. return res;
  481. }
  482. #define INIT_PWRITEV64 COMMON_INTERCEPT_FUNCTION(pwritev64)
  483. #else
  484. #define INIT_PWRITEV64
  485. #endif
  486. #if SANITIZER_INTERCEPT_PRCTL
  487. INTERCEPTOR(int, prctl, int option, unsigned long arg2,
  488. unsigned long arg3, // NOLINT
  489. unsigned long arg4, unsigned long arg5) { // NOLINT
  490. void *ctx;
  491. COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
  492. static const int PR_SET_NAME = 15;
  493. int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
  494. if (option == PR_SET_NAME) {
  495. char buff[16];
  496. internal_strncpy(buff, (char *)arg2, 15);
  497. buff[15] = 0;
  498. COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
  499. }
  500. return res;
  501. }
  502. #define INIT_PRCTL COMMON_INTERCEPT_FUNCTION(prctl)
  503. #else
  504. #define INIT_PRCTL
  505. #endif // SANITIZER_INTERCEPT_PRCTL
  506. #if SANITIZER_INTERCEPT_TIME
  507. INTERCEPTOR(unsigned long, time, unsigned long *t) {
  508. void *ctx;
  509. COMMON_INTERCEPTOR_ENTER(ctx, time, t);
  510. unsigned long local_t;
  511. unsigned long res = REAL(time)(&local_t);
  512. if (t && res != (unsigned long)-1) {
  513. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, t, sizeof(*t));
  514. *t = local_t;
  515. }
  516. return res;
  517. }
  518. #define INIT_TIME COMMON_INTERCEPT_FUNCTION(time);
  519. #else
  520. #define INIT_TIME
  521. #endif // SANITIZER_INTERCEPT_TIME
  522. #if SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
  523. static void unpoison_tm(void *ctx, __sanitizer_tm *tm) {
  524. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
  525. if (tm->tm_zone) {
  526. // Can not use COMMON_INTERCEPTOR_WRITE_RANGE here, because tm->tm_zone
  527. // can point to shared memory and tsan would report a data race.
  528. COMMON_INTERCEPTOR_INITIALIZE_RANGE(tm->tm_zone,
  529. REAL(strlen(tm->tm_zone)) + 1);
  530. }
  531. }
  532. INTERCEPTOR(__sanitizer_tm *, localtime, unsigned long *timep) {
  533. void *ctx;
  534. COMMON_INTERCEPTOR_ENTER(ctx, localtime, timep);
  535. __sanitizer_tm *res = REAL(localtime)(timep);
  536. if (res) {
  537. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  538. unpoison_tm(ctx, res);
  539. }
  540. return res;
  541. }
  542. INTERCEPTOR(__sanitizer_tm *, localtime_r, unsigned long *timep, void *result) {
  543. void *ctx;
  544. COMMON_INTERCEPTOR_ENTER(ctx, localtime_r, timep, result);
  545. __sanitizer_tm *res = REAL(localtime_r)(timep, result);
  546. if (res) {
  547. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  548. unpoison_tm(ctx, res);
  549. }
  550. return res;
  551. }
  552. INTERCEPTOR(__sanitizer_tm *, gmtime, unsigned long *timep) {
  553. void *ctx;
  554. COMMON_INTERCEPTOR_ENTER(ctx, gmtime, timep);
  555. __sanitizer_tm *res = REAL(gmtime)(timep);
  556. if (res) {
  557. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  558. unpoison_tm(ctx, res);
  559. }
  560. return res;
  561. }
  562. INTERCEPTOR(__sanitizer_tm *, gmtime_r, unsigned long *timep, void *result) {
  563. void *ctx;
  564. COMMON_INTERCEPTOR_ENTER(ctx, gmtime_r, timep, result);
  565. __sanitizer_tm *res = REAL(gmtime_r)(timep, result);
  566. if (res) {
  567. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  568. unpoison_tm(ctx, res);
  569. }
  570. return res;
  571. }
  572. INTERCEPTOR(char *, ctime, unsigned long *timep) {
  573. void *ctx;
  574. COMMON_INTERCEPTOR_ENTER(ctx, ctime, timep);
  575. // FIXME: under ASan the call below may write to freed memory and corrupt
  576. // its metadata. See
  577. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  578. char *res = REAL(ctime)(timep);
  579. if (res) {
  580. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  581. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  582. }
  583. return res;
  584. }
  585. INTERCEPTOR(char *, ctime_r, unsigned long *timep, char *result) {
  586. void *ctx;
  587. COMMON_INTERCEPTOR_ENTER(ctx, ctime_r, timep, result);
  588. // FIXME: under ASan the call below may write to freed memory and corrupt
  589. // its metadata. See
  590. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  591. char *res = REAL(ctime_r)(timep, result);
  592. if (res) {
  593. COMMON_INTERCEPTOR_READ_RANGE(ctx, timep, sizeof(*timep));
  594. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  595. }
  596. return res;
  597. }
  598. INTERCEPTOR(char *, asctime, __sanitizer_tm *tm) {
  599. void *ctx;
  600. COMMON_INTERCEPTOR_ENTER(ctx, asctime, tm);
  601. // FIXME: under ASan the call below may write to freed memory and corrupt
  602. // its metadata. See
  603. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  604. char *res = REAL(asctime)(tm);
  605. if (res) {
  606. COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
  607. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  608. }
  609. return res;
  610. }
  611. INTERCEPTOR(char *, asctime_r, __sanitizer_tm *tm, char *result) {
  612. void *ctx;
  613. COMMON_INTERCEPTOR_ENTER(ctx, asctime_r, tm, result);
  614. // FIXME: under ASan the call below may write to freed memory and corrupt
  615. // its metadata. See
  616. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  617. char *res = REAL(asctime_r)(tm, result);
  618. if (res) {
  619. COMMON_INTERCEPTOR_READ_RANGE(ctx, tm, sizeof(*tm));
  620. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  621. }
  622. return res;
  623. }
  624. INTERCEPTOR(long, mktime, __sanitizer_tm *tm) {
  625. void *ctx;
  626. COMMON_INTERCEPTOR_ENTER(ctx, mktime, tm);
  627. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_sec, sizeof(tm->tm_sec));
  628. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_min, sizeof(tm->tm_min));
  629. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_hour, sizeof(tm->tm_hour));
  630. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mday, sizeof(tm->tm_mday));
  631. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_mon, sizeof(tm->tm_mon));
  632. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_year, sizeof(tm->tm_year));
  633. COMMON_INTERCEPTOR_READ_RANGE(ctx, &tm->tm_isdst, sizeof(tm->tm_isdst));
  634. long res = REAL(mktime)(tm);
  635. if (res != -1) unpoison_tm(ctx, tm);
  636. return res;
  637. }
  638. #define INIT_LOCALTIME_AND_FRIENDS \
  639. COMMON_INTERCEPT_FUNCTION(localtime); \
  640. COMMON_INTERCEPT_FUNCTION(localtime_r); \
  641. COMMON_INTERCEPT_FUNCTION(gmtime); \
  642. COMMON_INTERCEPT_FUNCTION(gmtime_r); \
  643. COMMON_INTERCEPT_FUNCTION(ctime); \
  644. COMMON_INTERCEPT_FUNCTION(ctime_r); \
  645. COMMON_INTERCEPT_FUNCTION(asctime); \
  646. COMMON_INTERCEPT_FUNCTION(asctime_r); \
  647. COMMON_INTERCEPT_FUNCTION(mktime);
  648. #else
  649. #define INIT_LOCALTIME_AND_FRIENDS
  650. #endif // SANITIZER_INTERCEPT_LOCALTIME_AND_FRIENDS
  651. #if SANITIZER_INTERCEPT_STRPTIME
  652. INTERCEPTOR(char *, strptime, char *s, char *format, __sanitizer_tm *tm) {
  653. void *ctx;
  654. COMMON_INTERCEPTOR_ENTER(ctx, strptime, s, format, tm);
  655. if (format)
  656. COMMON_INTERCEPTOR_READ_RANGE(ctx, format, REAL(strlen)(format) + 1);
  657. // FIXME: under ASan the call below may write to freed memory and corrupt
  658. // its metadata. See
  659. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  660. char *res = REAL(strptime)(s, format, tm);
  661. if (res) {
  662. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, res - s);
  663. // Do not call unpoison_tm here, because strptime does not, in fact,
  664. // initialize the entire struct tm. For example, tm_zone pointer is left
  665. // uninitialized.
  666. if (tm) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tm, sizeof(*tm));
  667. }
  668. return res;
  669. }
  670. #define INIT_STRPTIME COMMON_INTERCEPT_FUNCTION(strptime);
  671. #else
  672. #define INIT_STRPTIME
  673. #endif
  674. #if SANITIZER_INTERCEPT_SCANF || SANITIZER_INTERCEPT_PRINTF
  675. #include "sanitizer_common_interceptors_format.inc"
  676. #define FORMAT_INTERCEPTOR_IMPL(name, vname, ...) \
  677. { \
  678. void *ctx; \
  679. va_list ap; \
  680. va_start(ap, format); \
  681. COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__, ap); \
  682. int res = WRAP(vname)(__VA_ARGS__, ap); \
  683. va_end(ap); \
  684. return res; \
  685. }
  686. #endif
  687. #if SANITIZER_INTERCEPT_SCANF
  688. #define VSCANF_INTERCEPTOR_IMPL(vname, allowGnuMalloc, ...) \
  689. { \
  690. void *ctx; \
  691. COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
  692. va_list aq; \
  693. va_copy(aq, ap); \
  694. int res = REAL(vname)(__VA_ARGS__); \
  695. if (res > 0) \
  696. scanf_common(ctx, res, allowGnuMalloc, format, aq); \
  697. va_end(aq); \
  698. return res; \
  699. }
  700. INTERCEPTOR(int, vscanf, const char *format, va_list ap)
  701. VSCANF_INTERCEPTOR_IMPL(vscanf, true, format, ap)
  702. INTERCEPTOR(int, vsscanf, const char *str, const char *format, va_list ap)
  703. VSCANF_INTERCEPTOR_IMPL(vsscanf, true, str, format, ap)
  704. INTERCEPTOR(int, vfscanf, void *stream, const char *format, va_list ap)
  705. VSCANF_INTERCEPTOR_IMPL(vfscanf, true, stream, format, ap)
  706. #if SANITIZER_INTERCEPT_ISOC99_SCANF
  707. INTERCEPTOR(int, __isoc99_vscanf, const char *format, va_list ap)
  708. VSCANF_INTERCEPTOR_IMPL(__isoc99_vscanf, false, format, ap)
  709. INTERCEPTOR(int, __isoc99_vsscanf, const char *str, const char *format,
  710. va_list ap)
  711. VSCANF_INTERCEPTOR_IMPL(__isoc99_vsscanf, false, str, format, ap)
  712. INTERCEPTOR(int, __isoc99_vfscanf, void *stream, const char *format, va_list ap)
  713. VSCANF_INTERCEPTOR_IMPL(__isoc99_vfscanf, false, stream, format, ap)
  714. #endif // SANITIZER_INTERCEPT_ISOC99_SCANF
  715. INTERCEPTOR(int, scanf, const char *format, ...)
  716. FORMAT_INTERCEPTOR_IMPL(scanf, vscanf, format)
  717. INTERCEPTOR(int, fscanf, void *stream, const char *format, ...)
  718. FORMAT_INTERCEPTOR_IMPL(fscanf, vfscanf, stream, format)
  719. INTERCEPTOR(int, sscanf, const char *str, const char *format, ...)
  720. FORMAT_INTERCEPTOR_IMPL(sscanf, vsscanf, str, format)
  721. #if SANITIZER_INTERCEPT_ISOC99_SCANF
  722. INTERCEPTOR(int, __isoc99_scanf, const char *format, ...)
  723. FORMAT_INTERCEPTOR_IMPL(__isoc99_scanf, __isoc99_vscanf, format)
  724. INTERCEPTOR(int, __isoc99_fscanf, void *stream, const char *format, ...)
  725. FORMAT_INTERCEPTOR_IMPL(__isoc99_fscanf, __isoc99_vfscanf, stream, format)
  726. INTERCEPTOR(int, __isoc99_sscanf, const char *str, const char *format, ...)
  727. FORMAT_INTERCEPTOR_IMPL(__isoc99_sscanf, __isoc99_vsscanf, str, format)
  728. #endif
  729. #endif
  730. #if SANITIZER_INTERCEPT_SCANF
  731. #define INIT_SCANF \
  732. COMMON_INTERCEPT_FUNCTION(scanf); \
  733. COMMON_INTERCEPT_FUNCTION(sscanf); \
  734. COMMON_INTERCEPT_FUNCTION(fscanf); \
  735. COMMON_INTERCEPT_FUNCTION(vscanf); \
  736. COMMON_INTERCEPT_FUNCTION(vsscanf); \
  737. COMMON_INTERCEPT_FUNCTION(vfscanf);
  738. #else
  739. #define INIT_SCANF
  740. #endif
  741. #if SANITIZER_INTERCEPT_ISOC99_SCANF
  742. #define INIT_ISOC99_SCANF \
  743. COMMON_INTERCEPT_FUNCTION(__isoc99_scanf); \
  744. COMMON_INTERCEPT_FUNCTION(__isoc99_sscanf); \
  745. COMMON_INTERCEPT_FUNCTION(__isoc99_fscanf); \
  746. COMMON_INTERCEPT_FUNCTION(__isoc99_vscanf); \
  747. COMMON_INTERCEPT_FUNCTION(__isoc99_vsscanf); \
  748. COMMON_INTERCEPT_FUNCTION(__isoc99_vfscanf);
  749. #else
  750. #define INIT_ISOC99_SCANF
  751. #endif
  752. #if SANITIZER_INTERCEPT_PRINTF
  753. #define VPRINTF_INTERCEPTOR_ENTER(vname, ...) \
  754. void *ctx; \
  755. COMMON_INTERCEPTOR_ENTER(ctx, vname, __VA_ARGS__); \
  756. va_list aq; \
  757. va_copy(aq, ap);
  758. #define VPRINTF_INTERCEPTOR_RETURN() \
  759. va_end(aq);
  760. #define VPRINTF_INTERCEPTOR_IMPL(vname, ...) \
  761. { \
  762. VPRINTF_INTERCEPTOR_ENTER(vname, __VA_ARGS__); \
  763. if (common_flags()->check_printf) \
  764. printf_common(ctx, format, aq); \
  765. int res = REAL(vname)(__VA_ARGS__); \
  766. VPRINTF_INTERCEPTOR_RETURN(); \
  767. return res; \
  768. }
  769. // FIXME: under ASan the REAL() call below may write to freed memory and
  770. // corrupt its metadata. See
  771. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  772. #define VSPRINTF_INTERCEPTOR_IMPL(vname, str, ...) \
  773. { \
  774. VPRINTF_INTERCEPTOR_ENTER(vname, str, __VA_ARGS__) \
  775. if (common_flags()->check_printf) { \
  776. printf_common(ctx, format, aq); \
  777. } \
  778. int res = REAL(vname)(str, __VA_ARGS__); \
  779. if (res >= 0) { \
  780. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, res + 1); \
  781. } \
  782. VPRINTF_INTERCEPTOR_RETURN(); \
  783. return res; \
  784. }
  785. // FIXME: under ASan the REAL() call below may write to freed memory and
  786. // corrupt its metadata. See
  787. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  788. #define VSNPRINTF_INTERCEPTOR_IMPL(vname, str, size, ...) \
  789. { \
  790. VPRINTF_INTERCEPTOR_ENTER(vname, str, size, __VA_ARGS__) \
  791. if (common_flags()->check_printf) { \
  792. printf_common(ctx, format, aq); \
  793. } \
  794. int res = REAL(vname)(str, size, __VA_ARGS__); \
  795. if (res >= 0) { \
  796. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, Min(size, (SIZE_T)(res + 1))); \
  797. } \
  798. VPRINTF_INTERCEPTOR_RETURN(); \
  799. return res; \
  800. }
  801. // FIXME: under ASan the REAL() call below may write to freed memory and
  802. // corrupt its metadata. See
  803. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  804. #define VASPRINTF_INTERCEPTOR_IMPL(vname, strp, ...) \
  805. { \
  806. VPRINTF_INTERCEPTOR_ENTER(vname, strp, __VA_ARGS__) \
  807. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, strp, sizeof(char *)); \
  808. if (common_flags()->check_printf) { \
  809. printf_common(ctx, format, aq); \
  810. } \
  811. int res = REAL(vname)(strp, __VA_ARGS__); \
  812. if (res >= 0) { \
  813. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *strp, res + 1); \
  814. } \
  815. VPRINTF_INTERCEPTOR_RETURN(); \
  816. return res; \
  817. }
  818. INTERCEPTOR(int, vprintf, const char *format, va_list ap)
  819. VPRINTF_INTERCEPTOR_IMPL(vprintf, format, ap)
  820. INTERCEPTOR(int, vfprintf, __sanitizer_FILE *stream, const char *format,
  821. va_list ap)
  822. VPRINTF_INTERCEPTOR_IMPL(vfprintf, stream, format, ap)
  823. INTERCEPTOR(int, vsnprintf, char *str, SIZE_T size, const char *format,
  824. va_list ap)
  825. VSNPRINTF_INTERCEPTOR_IMPL(vsnprintf, str, size, format, ap)
  826. INTERCEPTOR(int, vsprintf, char *str, const char *format, va_list ap)
  827. VSPRINTF_INTERCEPTOR_IMPL(vsprintf, str, format, ap)
  828. INTERCEPTOR(int, vasprintf, char **strp, const char *format, va_list ap)
  829. VASPRINTF_INTERCEPTOR_IMPL(vasprintf, strp, format, ap)
  830. #if SANITIZER_INTERCEPT_ISOC99_PRINTF
  831. INTERCEPTOR(int, __isoc99_vprintf, const char *format, va_list ap)
  832. VPRINTF_INTERCEPTOR_IMPL(__isoc99_vprintf, format, ap)
  833. INTERCEPTOR(int, __isoc99_vfprintf, __sanitizer_FILE *stream,
  834. const char *format, va_list ap)
  835. VPRINTF_INTERCEPTOR_IMPL(__isoc99_vfprintf, stream, format, ap)
  836. INTERCEPTOR(int, __isoc99_vsnprintf, char *str, SIZE_T size, const char *format,
  837. va_list ap)
  838. VSNPRINTF_INTERCEPTOR_IMPL(__isoc99_vsnprintf, str, size, format, ap)
  839. INTERCEPTOR(int, __isoc99_vsprintf, char *str, const char *format,
  840. va_list ap)
  841. VSPRINTF_INTERCEPTOR_IMPL(__isoc99_vsprintf, str, format,
  842. ap)
  843. #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
  844. INTERCEPTOR(int, printf, const char *format, ...)
  845. FORMAT_INTERCEPTOR_IMPL(printf, vprintf, format)
  846. INTERCEPTOR(int, fprintf, __sanitizer_FILE *stream, const char *format, ...)
  847. FORMAT_INTERCEPTOR_IMPL(fprintf, vfprintf, stream, format)
  848. INTERCEPTOR(int, sprintf, char *str, const char *format, ...) // NOLINT
  849. FORMAT_INTERCEPTOR_IMPL(sprintf, vsprintf, str, format) // NOLINT
  850. INTERCEPTOR(int, snprintf, char *str, SIZE_T size, const char *format, ...)
  851. FORMAT_INTERCEPTOR_IMPL(snprintf, vsnprintf, str, size, format)
  852. INTERCEPTOR(int, asprintf, char **strp, const char *format, ...)
  853. FORMAT_INTERCEPTOR_IMPL(asprintf, vasprintf, strp, format)
  854. #if SANITIZER_INTERCEPT_ISOC99_PRINTF
  855. INTERCEPTOR(int, __isoc99_printf, const char *format, ...)
  856. FORMAT_INTERCEPTOR_IMPL(__isoc99_printf, __isoc99_vprintf, format)
  857. INTERCEPTOR(int, __isoc99_fprintf, __sanitizer_FILE *stream, const char *format,
  858. ...)
  859. FORMAT_INTERCEPTOR_IMPL(__isoc99_fprintf, __isoc99_vfprintf, stream, format)
  860. INTERCEPTOR(int, __isoc99_sprintf, char *str, const char *format, ...)
  861. FORMAT_INTERCEPTOR_IMPL(__isoc99_sprintf, __isoc99_vsprintf, str, format)
  862. INTERCEPTOR(int, __isoc99_snprintf, char *str, SIZE_T size,
  863. const char *format, ...)
  864. FORMAT_INTERCEPTOR_IMPL(__isoc99_snprintf, __isoc99_vsnprintf, str, size,
  865. format)
  866. #endif // SANITIZER_INTERCEPT_ISOC99_PRINTF
  867. #endif // SANITIZER_INTERCEPT_PRINTF
  868. #if SANITIZER_INTERCEPT_PRINTF
  869. #define INIT_PRINTF \
  870. COMMON_INTERCEPT_FUNCTION(printf); \
  871. COMMON_INTERCEPT_FUNCTION(sprintf); \
  872. COMMON_INTERCEPT_FUNCTION(snprintf); \
  873. COMMON_INTERCEPT_FUNCTION(asprintf); \
  874. COMMON_INTERCEPT_FUNCTION(fprintf); \
  875. COMMON_INTERCEPT_FUNCTION(vprintf); \
  876. COMMON_INTERCEPT_FUNCTION(vsprintf); \
  877. COMMON_INTERCEPT_FUNCTION(vsnprintf); \
  878. COMMON_INTERCEPT_FUNCTION(vasprintf); \
  879. COMMON_INTERCEPT_FUNCTION(vfprintf);
  880. #else
  881. #define INIT_PRINTF
  882. #endif
  883. #if SANITIZER_INTERCEPT_ISOC99_PRINTF
  884. #define INIT_ISOC99_PRINTF \
  885. COMMON_INTERCEPT_FUNCTION(__isoc99_printf); \
  886. COMMON_INTERCEPT_FUNCTION(__isoc99_sprintf); \
  887. COMMON_INTERCEPT_FUNCTION(__isoc99_snprintf); \
  888. COMMON_INTERCEPT_FUNCTION(__isoc99_fprintf); \
  889. COMMON_INTERCEPT_FUNCTION(__isoc99_vprintf); \
  890. COMMON_INTERCEPT_FUNCTION(__isoc99_vsprintf); \
  891. COMMON_INTERCEPT_FUNCTION(__isoc99_vsnprintf); \
  892. COMMON_INTERCEPT_FUNCTION(__isoc99_vfprintf);
  893. #else
  894. #define INIT_ISOC99_PRINTF
  895. #endif
  896. #if SANITIZER_INTERCEPT_IOCTL
  897. #include "sanitizer_common_interceptors_ioctl.inc"
  898. INTERCEPTOR(int, ioctl, int d, unsigned request, void *arg) {
  899. void *ctx;
  900. COMMON_INTERCEPTOR_ENTER(ctx, ioctl, d, request, arg);
  901. CHECK(ioctl_initialized);
  902. // Note: TSan does not use common flags, and they are zero-initialized.
  903. // This effectively disables ioctl handling in TSan.
  904. if (!common_flags()->handle_ioctl) return REAL(ioctl)(d, request, arg);
  905. const ioctl_desc *desc = ioctl_lookup(request);
  906. ioctl_desc decoded_desc;
  907. if (!desc) {
  908. VPrintf(2, "Decoding unknown ioctl 0x%x\n", request);
  909. if (!ioctl_decode(request, &decoded_desc))
  910. Printf("WARNING: failed decoding unknown ioctl 0x%x\n", request);
  911. else
  912. desc = &decoded_desc;
  913. }
  914. if (desc) ioctl_common_pre(ctx, desc, d, request, arg);
  915. int res = REAL(ioctl)(d, request, arg);
  916. // FIXME: some ioctls have different return values for success and failure.
  917. if (desc && res != -1) ioctl_common_post(ctx, desc, res, d, request, arg);
  918. return res;
  919. }
  920. #define INIT_IOCTL \
  921. ioctl_init(); \
  922. COMMON_INTERCEPT_FUNCTION(ioctl);
  923. #else
  924. #define INIT_IOCTL
  925. #endif
  926. #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS || \
  927. SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT || \
  928. SANITIZER_INTERCEPT_GETPWENT_R || SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
  929. static void unpoison_passwd(void *ctx, __sanitizer_passwd *pwd) {
  930. if (pwd) {
  931. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwd, sizeof(*pwd));
  932. if (pwd->pw_name)
  933. COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_name,
  934. REAL(strlen)(pwd->pw_name) + 1);
  935. if (pwd->pw_passwd)
  936. COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_passwd,
  937. REAL(strlen)(pwd->pw_passwd) + 1);
  938. #if !SANITIZER_ANDROID
  939. if (pwd->pw_gecos)
  940. COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_gecos,
  941. REAL(strlen)(pwd->pw_gecos) + 1);
  942. #endif
  943. #if SANITIZER_MAC
  944. if (pwd->pw_class)
  945. COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_class,
  946. REAL(strlen)(pwd->pw_class) + 1);
  947. #endif
  948. if (pwd->pw_dir)
  949. COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_dir,
  950. REAL(strlen)(pwd->pw_dir) + 1);
  951. if (pwd->pw_shell)
  952. COMMON_INTERCEPTOR_INITIALIZE_RANGE(pwd->pw_shell,
  953. REAL(strlen)(pwd->pw_shell) + 1);
  954. }
  955. }
  956. static void unpoison_group(void *ctx, __sanitizer_group *grp) {
  957. if (grp) {
  958. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, grp, sizeof(*grp));
  959. if (grp->gr_name)
  960. COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_name,
  961. REAL(strlen)(grp->gr_name) + 1);
  962. if (grp->gr_passwd)
  963. COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_passwd,
  964. REAL(strlen)(grp->gr_passwd) + 1);
  965. char **p = grp->gr_mem;
  966. for (; *p; ++p) {
  967. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*p, REAL(strlen)(*p) + 1);
  968. }
  969. COMMON_INTERCEPTOR_INITIALIZE_RANGE(grp->gr_mem,
  970. (p - grp->gr_mem + 1) * sizeof(*p));
  971. }
  972. }
  973. #endif // SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS ||
  974. // SANITIZER_INTERCEPT_GETPWENT || SANITIZER_INTERCEPT_FGETPWENT ||
  975. // SANITIZER_INTERCEPT_GETPWENT_R ||
  976. // SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
  977. #if SANITIZER_INTERCEPT_GETPWNAM_AND_FRIENDS
  978. INTERCEPTOR(__sanitizer_passwd *, getpwnam, const char *name) {
  979. void *ctx;
  980. COMMON_INTERCEPTOR_ENTER(ctx, getpwnam, name);
  981. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
  982. __sanitizer_passwd *res = REAL(getpwnam)(name);
  983. if (res != 0) unpoison_passwd(ctx, res);
  984. return res;
  985. }
  986. INTERCEPTOR(__sanitizer_passwd *, getpwuid, u32 uid) {
  987. void *ctx;
  988. COMMON_INTERCEPTOR_ENTER(ctx, getpwuid, uid);
  989. __sanitizer_passwd *res = REAL(getpwuid)(uid);
  990. if (res != 0) unpoison_passwd(ctx, res);
  991. return res;
  992. }
  993. INTERCEPTOR(__sanitizer_group *, getgrnam, const char *name) {
  994. void *ctx;
  995. COMMON_INTERCEPTOR_ENTER(ctx, getgrnam, name);
  996. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
  997. __sanitizer_group *res = REAL(getgrnam)(name);
  998. if (res != 0) unpoison_group(ctx, res);
  999. return res;
  1000. }
  1001. INTERCEPTOR(__sanitizer_group *, getgrgid, u32 gid) {
  1002. void *ctx;
  1003. COMMON_INTERCEPTOR_ENTER(ctx, getgrgid, gid);
  1004. __sanitizer_group *res = REAL(getgrgid)(gid);
  1005. if (res != 0) unpoison_group(ctx, res);
  1006. return res;
  1007. }
  1008. #define INIT_GETPWNAM_AND_FRIENDS \
  1009. COMMON_INTERCEPT_FUNCTION(getpwnam); \
  1010. COMMON_INTERCEPT_FUNCTION(getpwuid); \
  1011. COMMON_INTERCEPT_FUNCTION(getgrnam); \
  1012. COMMON_INTERCEPT_FUNCTION(getgrgid);
  1013. #else
  1014. #define INIT_GETPWNAM_AND_FRIENDS
  1015. #endif
  1016. #if SANITIZER_INTERCEPT_GETPWNAM_R_AND_FRIENDS
  1017. INTERCEPTOR(int, getpwnam_r, const char *name, __sanitizer_passwd *pwd,
  1018. char *buf, SIZE_T buflen, __sanitizer_passwd **result) {
  1019. void *ctx;
  1020. COMMON_INTERCEPTOR_ENTER(ctx, getpwnam_r, name, pwd, buf, buflen, result);
  1021. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
  1022. // FIXME: under ASan the call below may write to freed memory and corrupt
  1023. // its metadata. See
  1024. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1025. int res = REAL(getpwnam_r)(name, pwd, buf, buflen, result);
  1026. if (!res) {
  1027. if (result && *result) unpoison_passwd(ctx, *result);
  1028. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
  1029. }
  1030. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1031. return res;
  1032. }
  1033. INTERCEPTOR(int, getpwuid_r, u32 uid, __sanitizer_passwd *pwd, char *buf,
  1034. SIZE_T buflen, __sanitizer_passwd **result) {
  1035. void *ctx;
  1036. COMMON_INTERCEPTOR_ENTER(ctx, getpwuid_r, uid, pwd, buf, buflen, result);
  1037. // FIXME: under ASan the call below may write to freed memory and corrupt
  1038. // its metadata. See
  1039. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1040. int res = REAL(getpwuid_r)(uid, pwd, buf, buflen, result);
  1041. if (!res) {
  1042. if (result && *result) unpoison_passwd(ctx, *result);
  1043. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
  1044. }
  1045. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1046. return res;
  1047. }
  1048. INTERCEPTOR(int, getgrnam_r, const char *name, __sanitizer_group *grp,
  1049. char *buf, SIZE_T buflen, __sanitizer_group **result) {
  1050. void *ctx;
  1051. COMMON_INTERCEPTOR_ENTER(ctx, getgrnam_r, name, grp, buf, buflen, result);
  1052. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
  1053. // FIXME: under ASan the call below may write to freed memory and corrupt
  1054. // its metadata. See
  1055. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1056. int res = REAL(getgrnam_r)(name, grp, buf, buflen, result);
  1057. if (!res) {
  1058. if (result && *result) unpoison_group(ctx, *result);
  1059. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
  1060. }
  1061. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1062. return res;
  1063. }
  1064. INTERCEPTOR(int, getgrgid_r, u32 gid, __sanitizer_group *grp, char *buf,
  1065. SIZE_T buflen, __sanitizer_group **result) {
  1066. void *ctx;
  1067. COMMON_INTERCEPTOR_ENTER(ctx, getgrgid_r, gid, grp, buf, buflen, result);
  1068. // FIXME: under ASan the call below may write to freed memory and corrupt
  1069. // its metadata. See
  1070. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1071. int res = REAL(getgrgid_r)(gid, grp, buf, buflen, result);
  1072. if (!res) {
  1073. if (result && *result) unpoison_group(ctx, *result);
  1074. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
  1075. }
  1076. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1077. return res;
  1078. }
  1079. #define INIT_GETPWNAM_R_AND_FRIENDS \
  1080. COMMON_INTERCEPT_FUNCTION(getpwnam_r); \
  1081. COMMON_INTERCEPT_FUNCTION(getpwuid_r); \
  1082. COMMON_INTERCEPT_FUNCTION(getgrnam_r); \
  1083. COMMON_INTERCEPT_FUNCTION(getgrgid_r);
  1084. #else
  1085. #define INIT_GETPWNAM_R_AND_FRIENDS
  1086. #endif
  1087. #if SANITIZER_INTERCEPT_GETPWENT
  1088. INTERCEPTOR(__sanitizer_passwd *, getpwent, int dummy) {
  1089. void *ctx;
  1090. COMMON_INTERCEPTOR_ENTER(ctx, getpwent, dummy);
  1091. __sanitizer_passwd *res = REAL(getpwent)(dummy);
  1092. if (res != 0) unpoison_passwd(ctx, res);
  1093. return res;
  1094. }
  1095. INTERCEPTOR(__sanitizer_group *, getgrent, int dummy) {
  1096. void *ctx;
  1097. COMMON_INTERCEPTOR_ENTER(ctx, getgrent, dummy);
  1098. __sanitizer_group *res = REAL(getgrent)(dummy);
  1099. if (res != 0) unpoison_group(ctx, res);;
  1100. return res;
  1101. }
  1102. #define INIT_GETPWENT \
  1103. COMMON_INTERCEPT_FUNCTION(getpwent); \
  1104. COMMON_INTERCEPT_FUNCTION(getgrent);
  1105. #else
  1106. #define INIT_GETPWENT
  1107. #endif
  1108. #if SANITIZER_INTERCEPT_FGETPWENT
  1109. INTERCEPTOR(__sanitizer_passwd *, fgetpwent, void *fp) {
  1110. void *ctx;
  1111. COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent, fp);
  1112. __sanitizer_passwd *res = REAL(fgetpwent)(fp);
  1113. if (res != 0) unpoison_passwd(ctx, res);
  1114. return res;
  1115. }
  1116. INTERCEPTOR(__sanitizer_group *, fgetgrent, void *fp) {
  1117. void *ctx;
  1118. COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent, fp);
  1119. __sanitizer_group *res = REAL(fgetgrent)(fp);
  1120. if (res != 0) unpoison_group(ctx, res);
  1121. return res;
  1122. }
  1123. #define INIT_FGETPWENT \
  1124. COMMON_INTERCEPT_FUNCTION(fgetpwent); \
  1125. COMMON_INTERCEPT_FUNCTION(fgetgrent);
  1126. #else
  1127. #define INIT_FGETPWENT
  1128. #endif
  1129. #if SANITIZER_INTERCEPT_GETPWENT_R
  1130. INTERCEPTOR(int, getpwent_r, __sanitizer_passwd *pwbuf, char *buf,
  1131. SIZE_T buflen, __sanitizer_passwd **pwbufp) {
  1132. void *ctx;
  1133. COMMON_INTERCEPTOR_ENTER(ctx, getpwent_r, pwbuf, buf, buflen, pwbufp);
  1134. // FIXME: under ASan the call below may write to freed memory and corrupt
  1135. // its metadata. See
  1136. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1137. int res = REAL(getpwent_r)(pwbuf, buf, buflen, pwbufp);
  1138. if (!res) {
  1139. if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
  1140. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
  1141. }
  1142. if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
  1143. return res;
  1144. }
  1145. INTERCEPTOR(int, fgetpwent_r, void *fp, __sanitizer_passwd *pwbuf, char *buf,
  1146. SIZE_T buflen, __sanitizer_passwd **pwbufp) {
  1147. void *ctx;
  1148. COMMON_INTERCEPTOR_ENTER(ctx, fgetpwent_r, fp, pwbuf, buf, buflen, pwbufp);
  1149. // FIXME: under ASan the call below may write to freed memory and corrupt
  1150. // its metadata. See
  1151. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1152. int res = REAL(fgetpwent_r)(fp, pwbuf, buf, buflen, pwbufp);
  1153. if (!res) {
  1154. if (pwbufp && *pwbufp) unpoison_passwd(ctx, *pwbufp);
  1155. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
  1156. }
  1157. if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
  1158. return res;
  1159. }
  1160. INTERCEPTOR(int, getgrent_r, __sanitizer_group *pwbuf, char *buf, SIZE_T buflen,
  1161. __sanitizer_group **pwbufp) {
  1162. void *ctx;
  1163. COMMON_INTERCEPTOR_ENTER(ctx, getgrent_r, pwbuf, buf, buflen, pwbufp);
  1164. // FIXME: under ASan the call below may write to freed memory and corrupt
  1165. // its metadata. See
  1166. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1167. int res = REAL(getgrent_r)(pwbuf, buf, buflen, pwbufp);
  1168. if (!res) {
  1169. if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
  1170. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
  1171. }
  1172. if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
  1173. return res;
  1174. }
  1175. INTERCEPTOR(int, fgetgrent_r, void *fp, __sanitizer_group *pwbuf, char *buf,
  1176. SIZE_T buflen, __sanitizer_group **pwbufp) {
  1177. void *ctx;
  1178. COMMON_INTERCEPTOR_ENTER(ctx, fgetgrent_r, fp, pwbuf, buf, buflen, pwbufp);
  1179. // FIXME: under ASan the call below may write to freed memory and corrupt
  1180. // its metadata. See
  1181. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1182. int res = REAL(fgetgrent_r)(fp, pwbuf, buf, buflen, pwbufp);
  1183. if (!res) {
  1184. if (pwbufp && *pwbufp) unpoison_group(ctx, *pwbufp);
  1185. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
  1186. }
  1187. if (pwbufp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pwbufp, sizeof(*pwbufp));
  1188. return res;
  1189. }
  1190. #define INIT_GETPWENT_R \
  1191. COMMON_INTERCEPT_FUNCTION(getpwent_r); \
  1192. COMMON_INTERCEPT_FUNCTION(fgetpwent_r); \
  1193. COMMON_INTERCEPT_FUNCTION(getgrent_r); \
  1194. COMMON_INTERCEPT_FUNCTION(fgetgrent_r);
  1195. #else
  1196. #define INIT_GETPWENT_R
  1197. #endif
  1198. #if SANITIZER_INTERCEPT_SETPWENT
  1199. // The only thing these interceptors do is disable any nested interceptors.
  1200. // These functions may open nss modules and call uninstrumented functions from
  1201. // them, and we don't want things like strlen() to trigger.
  1202. INTERCEPTOR(void, setpwent, int dummy) {
  1203. void *ctx;
  1204. COMMON_INTERCEPTOR_ENTER(ctx, setpwent, dummy);
  1205. REAL(setpwent)(dummy);
  1206. }
  1207. INTERCEPTOR(void, endpwent, int dummy) {
  1208. void *ctx;
  1209. COMMON_INTERCEPTOR_ENTER(ctx, endpwent, dummy);
  1210. REAL(endpwent)(dummy);
  1211. }
  1212. INTERCEPTOR(void, setgrent, int dummy) {
  1213. void *ctx;
  1214. COMMON_INTERCEPTOR_ENTER(ctx, setgrent, dummy);
  1215. REAL(setgrent)(dummy);
  1216. }
  1217. INTERCEPTOR(void, endgrent, int dummy) {
  1218. void *ctx;
  1219. COMMON_INTERCEPTOR_ENTER(ctx, endgrent, dummy);
  1220. REAL(endgrent)(dummy);
  1221. }
  1222. #define INIT_SETPWENT \
  1223. COMMON_INTERCEPT_FUNCTION(setpwent); \
  1224. COMMON_INTERCEPT_FUNCTION(endpwent); \
  1225. COMMON_INTERCEPT_FUNCTION(setgrent); \
  1226. COMMON_INTERCEPT_FUNCTION(endgrent);
  1227. #else
  1228. #define INIT_SETPWENT
  1229. #endif
  1230. #if SANITIZER_INTERCEPT_CLOCK_GETTIME
  1231. INTERCEPTOR(int, clock_getres, u32 clk_id, void *tp) {
  1232. void *ctx;
  1233. COMMON_INTERCEPTOR_ENTER(ctx, clock_getres, clk_id, tp);
  1234. // FIXME: under ASan the call below may write to freed memory and corrupt
  1235. // its metadata. See
  1236. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1237. int res = REAL(clock_getres)(clk_id, tp);
  1238. if (!res && tp) {
  1239. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
  1240. }
  1241. return res;
  1242. }
  1243. INTERCEPTOR(int, clock_gettime, u32 clk_id, void *tp) {
  1244. void *ctx;
  1245. COMMON_INTERCEPTOR_ENTER(ctx, clock_gettime, clk_id, tp);
  1246. // FIXME: under ASan the call below may write to freed memory and corrupt
  1247. // its metadata. See
  1248. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1249. int res = REAL(clock_gettime)(clk_id, tp);
  1250. if (!res) {
  1251. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, struct_timespec_sz);
  1252. }
  1253. return res;
  1254. }
  1255. INTERCEPTOR(int, clock_settime, u32 clk_id, const void *tp) {
  1256. void *ctx;
  1257. COMMON_INTERCEPTOR_ENTER(ctx, clock_settime, clk_id, tp);
  1258. COMMON_INTERCEPTOR_READ_RANGE(ctx, tp, struct_timespec_sz);
  1259. return REAL(clock_settime)(clk_id, tp);
  1260. }
  1261. #define INIT_CLOCK_GETTIME \
  1262. COMMON_INTERCEPT_FUNCTION(clock_getres); \
  1263. COMMON_INTERCEPT_FUNCTION(clock_gettime); \
  1264. COMMON_INTERCEPT_FUNCTION(clock_settime);
  1265. #else
  1266. #define INIT_CLOCK_GETTIME
  1267. #endif
  1268. #if SANITIZER_INTERCEPT_GETITIMER
  1269. INTERCEPTOR(int, getitimer, int which, void *curr_value) {
  1270. void *ctx;
  1271. COMMON_INTERCEPTOR_ENTER(ctx, getitimer, which, curr_value);
  1272. // FIXME: under ASan the call below may write to freed memory and corrupt
  1273. // its metadata. See
  1274. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1275. int res = REAL(getitimer)(which, curr_value);
  1276. if (!res && curr_value) {
  1277. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerval_sz);
  1278. }
  1279. return res;
  1280. }
  1281. INTERCEPTOR(int, setitimer, int which, const void *new_value, void *old_value) {
  1282. void *ctx;
  1283. COMMON_INTERCEPTOR_ENTER(ctx, setitimer, which, new_value, old_value);
  1284. if (new_value)
  1285. COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerval_sz);
  1286. // FIXME: under ASan the call below may write to freed memory and corrupt
  1287. // its metadata. See
  1288. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1289. int res = REAL(setitimer)(which, new_value, old_value);
  1290. if (!res && old_value) {
  1291. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerval_sz);
  1292. }
  1293. return res;
  1294. }
  1295. #define INIT_GETITIMER \
  1296. COMMON_INTERCEPT_FUNCTION(getitimer); \
  1297. COMMON_INTERCEPT_FUNCTION(setitimer);
  1298. #else
  1299. #define INIT_GETITIMER
  1300. #endif
  1301. #if SANITIZER_INTERCEPT_GLOB
  1302. static void unpoison_glob_t(void *ctx, __sanitizer_glob_t *pglob) {
  1303. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pglob, sizeof(*pglob));
  1304. // +1 for NULL pointer at the end.
  1305. if (pglob->gl_pathv)
  1306. COMMON_INTERCEPTOR_WRITE_RANGE(
  1307. ctx, pglob->gl_pathv, (pglob->gl_pathc + 1) * sizeof(*pglob->gl_pathv));
  1308. for (SIZE_T i = 0; i < pglob->gl_pathc; ++i) {
  1309. char *p = pglob->gl_pathv[i];
  1310. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, REAL(strlen)(p) + 1);
  1311. }
  1312. }
  1313. static THREADLOCAL __sanitizer_glob_t *pglob_copy;
  1314. static void wrapped_gl_closedir(void *dir) {
  1315. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  1316. IndirectExternCall(pglob_copy->gl_closedir)(dir);
  1317. }
  1318. static void *wrapped_gl_readdir(void *dir) {
  1319. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  1320. return IndirectExternCall(pglob_copy->gl_readdir)(dir);
  1321. }
  1322. static void *wrapped_gl_opendir(const char *s) {
  1323. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  1324. COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
  1325. return IndirectExternCall(pglob_copy->gl_opendir)(s);
  1326. }
  1327. static int wrapped_gl_lstat(const char *s, void *st) {
  1328. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  1329. COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
  1330. return IndirectExternCall(pglob_copy->gl_lstat)(s, st);
  1331. }
  1332. static int wrapped_gl_stat(const char *s, void *st) {
  1333. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  1334. COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, REAL(strlen)(s) + 1);
  1335. return IndirectExternCall(pglob_copy->gl_stat)(s, st);
  1336. }
  1337. INTERCEPTOR(int, glob, const char *pattern, int flags,
  1338. int (*errfunc)(const char *epath, int eerrno),
  1339. __sanitizer_glob_t *pglob) {
  1340. void *ctx;
  1341. COMMON_INTERCEPTOR_ENTER(ctx, glob, pattern, flags, errfunc, pglob);
  1342. __sanitizer_glob_t glob_copy = {
  1343. 0, 0, 0,
  1344. 0, wrapped_gl_closedir, wrapped_gl_readdir,
  1345. wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
  1346. if (flags & glob_altdirfunc) {
  1347. Swap(pglob->gl_closedir, glob_copy.gl_closedir);
  1348. Swap(pglob->gl_readdir, glob_copy.gl_readdir);
  1349. Swap(pglob->gl_opendir, glob_copy.gl_opendir);
  1350. Swap(pglob->gl_lstat, glob_copy.gl_lstat);
  1351. Swap(pglob->gl_stat, glob_copy.gl_stat);
  1352. pglob_copy = &glob_copy;
  1353. }
  1354. int res = REAL(glob)(pattern, flags, errfunc, pglob);
  1355. if (flags & glob_altdirfunc) {
  1356. Swap(pglob->gl_closedir, glob_copy.gl_closedir);
  1357. Swap(pglob->gl_readdir, glob_copy.gl_readdir);
  1358. Swap(pglob->gl_opendir, glob_copy.gl_opendir);
  1359. Swap(pglob->gl_lstat, glob_copy.gl_lstat);
  1360. Swap(pglob->gl_stat, glob_copy.gl_stat);
  1361. }
  1362. pglob_copy = 0;
  1363. if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
  1364. return res;
  1365. }
  1366. INTERCEPTOR(int, glob64, const char *pattern, int flags,
  1367. int (*errfunc)(const char *epath, int eerrno),
  1368. __sanitizer_glob_t *pglob) {
  1369. void *ctx;
  1370. COMMON_INTERCEPTOR_ENTER(ctx, glob64, pattern, flags, errfunc, pglob);
  1371. __sanitizer_glob_t glob_copy = {
  1372. 0, 0, 0,
  1373. 0, wrapped_gl_closedir, wrapped_gl_readdir,
  1374. wrapped_gl_opendir, wrapped_gl_lstat, wrapped_gl_stat};
  1375. if (flags & glob_altdirfunc) {
  1376. Swap(pglob->gl_closedir, glob_copy.gl_closedir);
  1377. Swap(pglob->gl_readdir, glob_copy.gl_readdir);
  1378. Swap(pglob->gl_opendir, glob_copy.gl_opendir);
  1379. Swap(pglob->gl_lstat, glob_copy.gl_lstat);
  1380. Swap(pglob->gl_stat, glob_copy.gl_stat);
  1381. pglob_copy = &glob_copy;
  1382. }
  1383. int res = REAL(glob64)(pattern, flags, errfunc, pglob);
  1384. if (flags & glob_altdirfunc) {
  1385. Swap(pglob->gl_closedir, glob_copy.gl_closedir);
  1386. Swap(pglob->gl_readdir, glob_copy.gl_readdir);
  1387. Swap(pglob->gl_opendir, glob_copy.gl_opendir);
  1388. Swap(pglob->gl_lstat, glob_copy.gl_lstat);
  1389. Swap(pglob->gl_stat, glob_copy.gl_stat);
  1390. }
  1391. pglob_copy = 0;
  1392. if ((!res || res == glob_nomatch) && pglob) unpoison_glob_t(ctx, pglob);
  1393. return res;
  1394. }
  1395. #define INIT_GLOB \
  1396. COMMON_INTERCEPT_FUNCTION(glob); \
  1397. COMMON_INTERCEPT_FUNCTION(glob64);
  1398. #else // SANITIZER_INTERCEPT_GLOB
  1399. #define INIT_GLOB
  1400. #endif // SANITIZER_INTERCEPT_GLOB
  1401. #if SANITIZER_INTERCEPT_WAIT
  1402. // According to sys/wait.h, wait(), waitid(), waitpid() may have symbol version
  1403. // suffixes on Darwin. See the declaration of INTERCEPTOR_WITH_SUFFIX for
  1404. // details.
  1405. INTERCEPTOR_WITH_SUFFIX(int, wait, int *status) {
  1406. void *ctx;
  1407. COMMON_INTERCEPTOR_ENTER(ctx, wait, status);
  1408. // FIXME: under ASan the call below may write to freed memory and corrupt
  1409. // its metadata. See
  1410. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1411. int res = REAL(wait)(status);
  1412. if (res != -1 && status)
  1413. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
  1414. return res;
  1415. }
  1416. // On FreeBSD id_t is always 64-bit wide.
  1417. #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
  1418. INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, long long id, void *infop,
  1419. int options) {
  1420. #else
  1421. INTERCEPTOR_WITH_SUFFIX(int, waitid, int idtype, int id, void *infop,
  1422. int options) {
  1423. #endif
  1424. void *ctx;
  1425. COMMON_INTERCEPTOR_ENTER(ctx, waitid, idtype, id, infop, options);
  1426. // FIXME: under ASan the call below may write to freed memory and corrupt
  1427. // its metadata. See
  1428. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1429. int res = REAL(waitid)(idtype, id, infop, options);
  1430. if (res != -1 && infop)
  1431. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, infop, siginfo_t_sz);
  1432. return res;
  1433. }
  1434. INTERCEPTOR_WITH_SUFFIX(int, waitpid, int pid, int *status, int options) {
  1435. void *ctx;
  1436. COMMON_INTERCEPTOR_ENTER(ctx, waitpid, pid, status, options);
  1437. // FIXME: under ASan the call below may write to freed memory and corrupt
  1438. // its metadata. See
  1439. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1440. int res = REAL(waitpid)(pid, status, options);
  1441. if (res != -1 && status)
  1442. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
  1443. return res;
  1444. }
  1445. INTERCEPTOR(int, wait3, int *status, int options, void *rusage) {
  1446. void *ctx;
  1447. COMMON_INTERCEPTOR_ENTER(ctx, wait3, status, options, rusage);
  1448. // FIXME: under ASan the call below may write to freed memory and corrupt
  1449. // its metadata. See
  1450. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1451. int res = REAL(wait3)(status, options, rusage);
  1452. if (res != -1) {
  1453. if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
  1454. if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
  1455. }
  1456. return res;
  1457. }
  1458. #if SANITIZER_ANDROID
  1459. INTERCEPTOR(int, __wait4, int pid, int *status, int options, void *rusage) {
  1460. void *ctx;
  1461. COMMON_INTERCEPTOR_ENTER(ctx, __wait4, pid, status, options, rusage);
  1462. // FIXME: under ASan the call below may write to freed memory and corrupt
  1463. // its metadata. See
  1464. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1465. int res = REAL(__wait4)(pid, status, options, rusage);
  1466. if (res != -1) {
  1467. if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
  1468. if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
  1469. }
  1470. return res;
  1471. }
  1472. #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(__wait4);
  1473. #else
  1474. INTERCEPTOR(int, wait4, int pid, int *status, int options, void *rusage) {
  1475. void *ctx;
  1476. COMMON_INTERCEPTOR_ENTER(ctx, wait4, pid, status, options, rusage);
  1477. // FIXME: under ASan the call below may write to freed memory and corrupt
  1478. // its metadata. See
  1479. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1480. int res = REAL(wait4)(pid, status, options, rusage);
  1481. if (res != -1) {
  1482. if (status) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, status, sizeof(*status));
  1483. if (rusage) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rusage, struct_rusage_sz);
  1484. }
  1485. return res;
  1486. }
  1487. #define INIT_WAIT4 COMMON_INTERCEPT_FUNCTION(wait4);
  1488. #endif // SANITIZER_ANDROID
  1489. #define INIT_WAIT \
  1490. COMMON_INTERCEPT_FUNCTION(wait); \
  1491. COMMON_INTERCEPT_FUNCTION(waitid); \
  1492. COMMON_INTERCEPT_FUNCTION(waitpid); \
  1493. COMMON_INTERCEPT_FUNCTION(wait3);
  1494. #else
  1495. #define INIT_WAIT
  1496. #define INIT_WAIT4
  1497. #endif
  1498. #if SANITIZER_INTERCEPT_INET
  1499. INTERCEPTOR(char *, inet_ntop, int af, const void *src, char *dst, u32 size) {
  1500. void *ctx;
  1501. COMMON_INTERCEPTOR_ENTER(ctx, inet_ntop, af, src, dst, size);
  1502. uptr sz = __sanitizer_in_addr_sz(af);
  1503. if (sz) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sz);
  1504. // FIXME: figure out read size based on the address family.
  1505. // FIXME: under ASan the call below may write to freed memory and corrupt
  1506. // its metadata. See
  1507. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1508. char *res = REAL(inet_ntop)(af, src, dst, size);
  1509. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  1510. return res;
  1511. }
  1512. INTERCEPTOR(int, inet_pton, int af, const char *src, void *dst) {
  1513. void *ctx;
  1514. COMMON_INTERCEPTOR_ENTER(ctx, inet_pton, af, src, dst);
  1515. // FIXME: figure out read size based on the address family.
  1516. // FIXME: under ASan the call below may write to freed memory and corrupt
  1517. // its metadata. See
  1518. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1519. int res = REAL(inet_pton)(af, src, dst);
  1520. if (res == 1) {
  1521. uptr sz = __sanitizer_in_addr_sz(af);
  1522. if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
  1523. }
  1524. return res;
  1525. }
  1526. #define INIT_INET \
  1527. COMMON_INTERCEPT_FUNCTION(inet_ntop); \
  1528. COMMON_INTERCEPT_FUNCTION(inet_pton);
  1529. #else
  1530. #define INIT_INET
  1531. #endif
  1532. #if SANITIZER_INTERCEPT_INET
  1533. INTERCEPTOR(int, inet_aton, const char *cp, void *dst) {
  1534. void *ctx;
  1535. COMMON_INTERCEPTOR_ENTER(ctx, inet_aton, cp, dst);
  1536. if (cp) COMMON_INTERCEPTOR_READ_RANGE(ctx, cp, REAL(strlen)(cp) + 1);
  1537. // FIXME: under ASan the call below may write to freed memory and corrupt
  1538. // its metadata. See
  1539. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1540. int res = REAL(inet_aton)(cp, dst);
  1541. if (res != 0) {
  1542. uptr sz = __sanitizer_in_addr_sz(af_inet);
  1543. if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sz);
  1544. }
  1545. return res;
  1546. }
  1547. #define INIT_INET_ATON COMMON_INTERCEPT_FUNCTION(inet_aton);
  1548. #else
  1549. #define INIT_INET_ATON
  1550. #endif
  1551. #if SANITIZER_INTERCEPT_PTHREAD_GETSCHEDPARAM
  1552. INTERCEPTOR(int, pthread_getschedparam, uptr thread, int *policy, int *param) {
  1553. void *ctx;
  1554. COMMON_INTERCEPTOR_ENTER(ctx, pthread_getschedparam, thread, policy, param);
  1555. // FIXME: under ASan the call below may write to freed memory and corrupt
  1556. // its metadata. See
  1557. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1558. int res = REAL(pthread_getschedparam)(thread, policy, param);
  1559. if (res == 0) {
  1560. if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
  1561. if (param) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, param, sizeof(*param));
  1562. }
  1563. return res;
  1564. }
  1565. #define INIT_PTHREAD_GETSCHEDPARAM \
  1566. COMMON_INTERCEPT_FUNCTION(pthread_getschedparam);
  1567. #else
  1568. #define INIT_PTHREAD_GETSCHEDPARAM
  1569. #endif
  1570. #if SANITIZER_INTERCEPT_GETADDRINFO
  1571. INTERCEPTOR(int, getaddrinfo, char *node, char *service,
  1572. struct __sanitizer_addrinfo *hints,
  1573. struct __sanitizer_addrinfo **out) {
  1574. void *ctx;
  1575. COMMON_INTERCEPTOR_ENTER(ctx, getaddrinfo, node, service, hints, out);
  1576. if (node) COMMON_INTERCEPTOR_READ_RANGE(ctx, node, REAL(strlen)(node) + 1);
  1577. if (service)
  1578. COMMON_INTERCEPTOR_READ_RANGE(ctx, service, REAL(strlen)(service) + 1);
  1579. if (hints)
  1580. COMMON_INTERCEPTOR_READ_RANGE(ctx, hints, sizeof(__sanitizer_addrinfo));
  1581. // FIXME: under ASan the call below may write to freed memory and corrupt
  1582. // its metadata. See
  1583. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1584. int res = REAL(getaddrinfo)(node, service, hints, out);
  1585. if (res == 0 && out) {
  1586. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, out, sizeof(*out));
  1587. struct __sanitizer_addrinfo *p = *out;
  1588. while (p) {
  1589. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
  1590. if (p->ai_addr)
  1591. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_addr, p->ai_addrlen);
  1592. if (p->ai_canonname)
  1593. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ai_canonname,
  1594. REAL(strlen)(p->ai_canonname) + 1);
  1595. p = p->ai_next;
  1596. }
  1597. }
  1598. return res;
  1599. }
  1600. #define INIT_GETADDRINFO COMMON_INTERCEPT_FUNCTION(getaddrinfo);
  1601. #else
  1602. #define INIT_GETADDRINFO
  1603. #endif
  1604. #if SANITIZER_INTERCEPT_GETNAMEINFO
  1605. INTERCEPTOR(int, getnameinfo, void *sockaddr, unsigned salen, char *host,
  1606. unsigned hostlen, char *serv, unsigned servlen, int flags) {
  1607. void *ctx;
  1608. COMMON_INTERCEPTOR_ENTER(ctx, getnameinfo, sockaddr, salen, host, hostlen,
  1609. serv, servlen, flags);
  1610. // FIXME: consider adding READ_RANGE(sockaddr, salen)
  1611. // There is padding in in_addr that may make this too noisy
  1612. // FIXME: under ASan the call below may write to freed memory and corrupt
  1613. // its metadata. See
  1614. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1615. int res =
  1616. REAL(getnameinfo)(sockaddr, salen, host, hostlen, serv, servlen, flags);
  1617. if (res == 0) {
  1618. if (host && hostlen)
  1619. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, host, REAL(strlen)(host) + 1);
  1620. if (serv && servlen)
  1621. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, serv, REAL(strlen)(serv) + 1);
  1622. }
  1623. return res;
  1624. }
  1625. #define INIT_GETNAMEINFO COMMON_INTERCEPT_FUNCTION(getnameinfo);
  1626. #else
  1627. #define INIT_GETNAMEINFO
  1628. #endif
  1629. #if SANITIZER_INTERCEPT_GETSOCKNAME
  1630. INTERCEPTOR(int, getsockname, int sock_fd, void *addr, int *addrlen) {
  1631. void *ctx;
  1632. COMMON_INTERCEPTOR_ENTER(ctx, getsockname, sock_fd, addr, addrlen);
  1633. COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
  1634. int addrlen_in = *addrlen;
  1635. // FIXME: under ASan the call below may write to freed memory and corrupt
  1636. // its metadata. See
  1637. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1638. int res = REAL(getsockname)(sock_fd, addr, addrlen);
  1639. if (res == 0) {
  1640. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addrlen_in, *addrlen));
  1641. }
  1642. return res;
  1643. }
  1644. #define INIT_GETSOCKNAME COMMON_INTERCEPT_FUNCTION(getsockname);
  1645. #else
  1646. #define INIT_GETSOCKNAME
  1647. #endif
  1648. #if SANITIZER_INTERCEPT_GETHOSTBYNAME || SANITIZER_INTERCEPT_GETHOSTBYNAME_R
  1649. static void write_hostent(void *ctx, struct __sanitizer_hostent *h) {
  1650. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h, sizeof(__sanitizer_hostent));
  1651. if (h->h_name)
  1652. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h->h_name, REAL(strlen)(h->h_name) + 1);
  1653. char **p = h->h_aliases;
  1654. while (*p) {
  1655. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
  1656. ++p;
  1657. }
  1658. COMMON_INTERCEPTOR_WRITE_RANGE(
  1659. ctx, h->h_aliases, (p - h->h_aliases + 1) * sizeof(*h->h_aliases));
  1660. p = h->h_addr_list;
  1661. while (*p) {
  1662. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, h->h_length);
  1663. ++p;
  1664. }
  1665. COMMON_INTERCEPTOR_WRITE_RANGE(
  1666. ctx, h->h_addr_list, (p - h->h_addr_list + 1) * sizeof(*h->h_addr_list));
  1667. }
  1668. #endif
  1669. #if SANITIZER_INTERCEPT_GETHOSTBYNAME
  1670. INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname, char *name) {
  1671. void *ctx;
  1672. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname, name);
  1673. struct __sanitizer_hostent *res = REAL(gethostbyname)(name);
  1674. if (res) write_hostent(ctx, res);
  1675. return res;
  1676. }
  1677. INTERCEPTOR(struct __sanitizer_hostent *, gethostbyaddr, void *addr, int len,
  1678. int type) {
  1679. void *ctx;
  1680. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr, addr, len, type);
  1681. COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
  1682. struct __sanitizer_hostent *res = REAL(gethostbyaddr)(addr, len, type);
  1683. if (res) write_hostent(ctx, res);
  1684. return res;
  1685. }
  1686. INTERCEPTOR(struct __sanitizer_hostent *, gethostent, int fake) {
  1687. void *ctx;
  1688. COMMON_INTERCEPTOR_ENTER(ctx, gethostent, fake);
  1689. struct __sanitizer_hostent *res = REAL(gethostent)(fake);
  1690. if (res) write_hostent(ctx, res);
  1691. return res;
  1692. }
  1693. INTERCEPTOR(struct __sanitizer_hostent *, gethostbyname2, char *name, int af) {
  1694. void *ctx;
  1695. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2, name, af);
  1696. struct __sanitizer_hostent *res = REAL(gethostbyname2)(name, af);
  1697. if (res) write_hostent(ctx, res);
  1698. return res;
  1699. }
  1700. #define INIT_GETHOSTBYNAME \
  1701. COMMON_INTERCEPT_FUNCTION(gethostent); \
  1702. COMMON_INTERCEPT_FUNCTION(gethostbyaddr); \
  1703. COMMON_INTERCEPT_FUNCTION(gethostbyname); \
  1704. COMMON_INTERCEPT_FUNCTION(gethostbyname2);
  1705. #else
  1706. #define INIT_GETHOSTBYNAME
  1707. #endif
  1708. #if SANITIZER_INTERCEPT_GETHOSTBYNAME_R
  1709. INTERCEPTOR(int, gethostbyname_r, char *name, struct __sanitizer_hostent *ret,
  1710. char *buf, SIZE_T buflen, __sanitizer_hostent **result,
  1711. int *h_errnop) {
  1712. void *ctx;
  1713. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname_r, name, ret, buf, buflen, result,
  1714. h_errnop);
  1715. // FIXME: under ASan the call below may write to freed memory and corrupt
  1716. // its metadata. See
  1717. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1718. int res = REAL(gethostbyname_r)(name, ret, buf, buflen, result, h_errnop);
  1719. if (result) {
  1720. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1721. if (res == 0 && *result) write_hostent(ctx, *result);
  1722. }
  1723. if (h_errnop)
  1724. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
  1725. return res;
  1726. }
  1727. #define INIT_GETHOSTBYNAME_R COMMON_INTERCEPT_FUNCTION(gethostbyname_r);
  1728. #else
  1729. #define INIT_GETHOSTBYNAME_R
  1730. #endif
  1731. #if SANITIZER_INTERCEPT_GETHOSTENT_R
  1732. INTERCEPTOR(int, gethostent_r, struct __sanitizer_hostent *ret, char *buf,
  1733. SIZE_T buflen, __sanitizer_hostent **result, int *h_errnop) {
  1734. void *ctx;
  1735. COMMON_INTERCEPTOR_ENTER(ctx, gethostent_r, ret, buf, buflen, result,
  1736. h_errnop);
  1737. // FIXME: under ASan the call below may write to freed memory and corrupt
  1738. // its metadata. See
  1739. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1740. int res = REAL(gethostent_r)(ret, buf, buflen, result, h_errnop);
  1741. if (result) {
  1742. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1743. if (res == 0 && *result) write_hostent(ctx, *result);
  1744. }
  1745. if (h_errnop)
  1746. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
  1747. return res;
  1748. }
  1749. #define INIT_GETHOSTENT_R \
  1750. COMMON_INTERCEPT_FUNCTION(gethostent_r);
  1751. #else
  1752. #define INIT_GETHOSTENT_R
  1753. #endif
  1754. #if SANITIZER_INTERCEPT_GETHOSTBYADDR_R
  1755. INTERCEPTOR(int, gethostbyaddr_r, void *addr, int len, int type,
  1756. struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
  1757. __sanitizer_hostent **result, int *h_errnop) {
  1758. void *ctx;
  1759. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyaddr_r, addr, len, type, ret, buf,
  1760. buflen, result, h_errnop);
  1761. COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, len);
  1762. // FIXME: under ASan the call below may write to freed memory and corrupt
  1763. // its metadata. See
  1764. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1765. int res = REAL(gethostbyaddr_r)(addr, len, type, ret, buf, buflen, result,
  1766. h_errnop);
  1767. if (result) {
  1768. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1769. if (res == 0 && *result) write_hostent(ctx, *result);
  1770. }
  1771. if (h_errnop)
  1772. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
  1773. return res;
  1774. }
  1775. #define INIT_GETHOSTBYADDR_R \
  1776. COMMON_INTERCEPT_FUNCTION(gethostbyaddr_r);
  1777. #else
  1778. #define INIT_GETHOSTBYADDR_R
  1779. #endif
  1780. #if SANITIZER_INTERCEPT_GETHOSTBYNAME2_R
  1781. INTERCEPTOR(int, gethostbyname2_r, char *name, int af,
  1782. struct __sanitizer_hostent *ret, char *buf, SIZE_T buflen,
  1783. __sanitizer_hostent **result, int *h_errnop) {
  1784. void *ctx;
  1785. COMMON_INTERCEPTOR_ENTER(ctx, gethostbyname2_r, name, af, ret, buf, buflen,
  1786. result, h_errnop);
  1787. // FIXME: under ASan the call below may write to freed memory and corrupt
  1788. // its metadata. See
  1789. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1790. int res =
  1791. REAL(gethostbyname2_r)(name, af, ret, buf, buflen, result, h_errnop);
  1792. if (result) {
  1793. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  1794. if (res == 0 && *result) write_hostent(ctx, *result);
  1795. }
  1796. if (h_errnop)
  1797. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, h_errnop, sizeof(*h_errnop));
  1798. return res;
  1799. }
  1800. #define INIT_GETHOSTBYNAME2_R \
  1801. COMMON_INTERCEPT_FUNCTION(gethostbyname2_r);
  1802. #else
  1803. #define INIT_GETHOSTBYNAME2_R
  1804. #endif
  1805. #if SANITIZER_INTERCEPT_GETSOCKOPT
  1806. INTERCEPTOR(int, getsockopt, int sockfd, int level, int optname, void *optval,
  1807. int *optlen) {
  1808. void *ctx;
  1809. COMMON_INTERCEPTOR_ENTER(ctx, getsockopt, sockfd, level, optname, optval,
  1810. optlen);
  1811. if (optlen) COMMON_INTERCEPTOR_READ_RANGE(ctx, optlen, sizeof(*optlen));
  1812. // FIXME: under ASan the call below may write to freed memory and corrupt
  1813. // its metadata. See
  1814. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1815. int res = REAL(getsockopt)(sockfd, level, optname, optval, optlen);
  1816. if (res == 0)
  1817. if (optval && optlen) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, optval, *optlen);
  1818. return res;
  1819. }
  1820. #define INIT_GETSOCKOPT COMMON_INTERCEPT_FUNCTION(getsockopt);
  1821. #else
  1822. #define INIT_GETSOCKOPT
  1823. #endif
  1824. #if SANITIZER_INTERCEPT_ACCEPT
  1825. INTERCEPTOR(int, accept, int fd, void *addr, unsigned *addrlen) {
  1826. void *ctx;
  1827. COMMON_INTERCEPTOR_ENTER(ctx, accept, fd, addr, addrlen);
  1828. unsigned addrlen0 = 0;
  1829. if (addrlen) {
  1830. COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
  1831. addrlen0 = *addrlen;
  1832. }
  1833. int fd2 = REAL(accept)(fd, addr, addrlen);
  1834. if (fd2 >= 0) {
  1835. if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
  1836. if (addr && addrlen)
  1837. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
  1838. }
  1839. return fd2;
  1840. }
  1841. #define INIT_ACCEPT COMMON_INTERCEPT_FUNCTION(accept);
  1842. #else
  1843. #define INIT_ACCEPT
  1844. #endif
  1845. #if SANITIZER_INTERCEPT_ACCEPT4
  1846. INTERCEPTOR(int, accept4, int fd, void *addr, unsigned *addrlen, int f) {
  1847. void *ctx;
  1848. COMMON_INTERCEPTOR_ENTER(ctx, accept4, fd, addr, addrlen, f);
  1849. unsigned addrlen0 = 0;
  1850. if (addrlen) {
  1851. COMMON_INTERCEPTOR_READ_RANGE(ctx, addrlen, sizeof(*addrlen));
  1852. addrlen0 = *addrlen;
  1853. }
  1854. // FIXME: under ASan the call below may write to freed memory and corrupt
  1855. // its metadata. See
  1856. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1857. int fd2 = REAL(accept4)(fd, addr, addrlen, f);
  1858. if (fd2 >= 0) {
  1859. if (fd >= 0) COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, fd2);
  1860. if (addr && addrlen)
  1861. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(*addrlen, addrlen0));
  1862. }
  1863. return fd2;
  1864. }
  1865. #define INIT_ACCEPT4 COMMON_INTERCEPT_FUNCTION(accept4);
  1866. #else
  1867. #define INIT_ACCEPT4
  1868. #endif
  1869. #if SANITIZER_INTERCEPT_MODF
  1870. INTERCEPTOR(double, modf, double x, double *iptr) {
  1871. void *ctx;
  1872. COMMON_INTERCEPTOR_ENTER(ctx, modf, x, iptr);
  1873. // FIXME: under ASan the call below may write to freed memory and corrupt
  1874. // its metadata. See
  1875. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1876. double res = REAL(modf)(x, iptr);
  1877. if (iptr) {
  1878. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
  1879. }
  1880. return res;
  1881. }
  1882. INTERCEPTOR(float, modff, float x, float *iptr) {
  1883. void *ctx;
  1884. COMMON_INTERCEPTOR_ENTER(ctx, modff, x, iptr);
  1885. // FIXME: under ASan the call below may write to freed memory and corrupt
  1886. // its metadata. See
  1887. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1888. float res = REAL(modff)(x, iptr);
  1889. if (iptr) {
  1890. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
  1891. }
  1892. return res;
  1893. }
  1894. INTERCEPTOR(long double, modfl, long double x, long double *iptr) {
  1895. void *ctx;
  1896. COMMON_INTERCEPTOR_ENTER(ctx, modfl, x, iptr);
  1897. // FIXME: under ASan the call below may write to freed memory and corrupt
  1898. // its metadata. See
  1899. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1900. long double res = REAL(modfl)(x, iptr);
  1901. if (iptr) {
  1902. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iptr, sizeof(*iptr));
  1903. }
  1904. return res;
  1905. }
  1906. #define INIT_MODF \
  1907. COMMON_INTERCEPT_FUNCTION(modf); \
  1908. COMMON_INTERCEPT_FUNCTION(modff); \
  1909. COMMON_INTERCEPT_FUNCTION(modfl);
  1910. #else
  1911. #define INIT_MODF
  1912. #endif
  1913. #if SANITIZER_INTERCEPT_RECVMSG
  1914. static void write_msghdr(void *ctx, struct __sanitizer_msghdr *msg,
  1915. SSIZE_T maxlen) {
  1916. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg, sizeof(*msg));
  1917. if (msg->msg_name && msg->msg_namelen)
  1918. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_name, msg->msg_namelen);
  1919. if (msg->msg_iov && msg->msg_iovlen)
  1920. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_iov,
  1921. sizeof(*msg->msg_iov) * msg->msg_iovlen);
  1922. write_iovec(ctx, msg->msg_iov, msg->msg_iovlen, maxlen);
  1923. if (msg->msg_control && msg->msg_controllen)
  1924. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, msg->msg_control, msg->msg_controllen);
  1925. }
  1926. INTERCEPTOR(SSIZE_T, recvmsg, int fd, struct __sanitizer_msghdr *msg,
  1927. int flags) {
  1928. void *ctx;
  1929. COMMON_INTERCEPTOR_ENTER(ctx, recvmsg, fd, msg, flags);
  1930. // FIXME: under ASan the call below may write to freed memory and corrupt
  1931. // its metadata. See
  1932. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1933. SSIZE_T res = REAL(recvmsg)(fd, msg, flags);
  1934. if (res >= 0) {
  1935. if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  1936. if (msg) {
  1937. write_msghdr(ctx, msg, res);
  1938. COMMON_INTERCEPTOR_HANDLE_RECVMSG(ctx, msg);
  1939. }
  1940. }
  1941. return res;
  1942. }
  1943. #define INIT_RECVMSG COMMON_INTERCEPT_FUNCTION(recvmsg);
  1944. #else
  1945. #define INIT_RECVMSG
  1946. #endif
  1947. #if SANITIZER_INTERCEPT_GETPEERNAME
  1948. INTERCEPTOR(int, getpeername, int sockfd, void *addr, unsigned *addrlen) {
  1949. void *ctx;
  1950. COMMON_INTERCEPTOR_ENTER(ctx, getpeername, sockfd, addr, addrlen);
  1951. unsigned addr_sz;
  1952. if (addrlen) addr_sz = *addrlen;
  1953. // FIXME: under ASan the call below may write to freed memory and corrupt
  1954. // its metadata. See
  1955. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1956. int res = REAL(getpeername)(sockfd, addr, addrlen);
  1957. if (!res && addr && addrlen)
  1958. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, Min(addr_sz, *addrlen));
  1959. return res;
  1960. }
  1961. #define INIT_GETPEERNAME COMMON_INTERCEPT_FUNCTION(getpeername);
  1962. #else
  1963. #define INIT_GETPEERNAME
  1964. #endif
  1965. #if SANITIZER_INTERCEPT_SYSINFO
  1966. INTERCEPTOR(int, sysinfo, void *info) {
  1967. void *ctx;
  1968. // FIXME: under ASan the call below may write to freed memory and corrupt
  1969. // its metadata. See
  1970. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1971. COMMON_INTERCEPTOR_ENTER(ctx, sysinfo, info);
  1972. int res = REAL(sysinfo)(info);
  1973. if (!res && info)
  1974. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, struct_sysinfo_sz);
  1975. return res;
  1976. }
  1977. #define INIT_SYSINFO COMMON_INTERCEPT_FUNCTION(sysinfo);
  1978. #else
  1979. #define INIT_SYSINFO
  1980. #endif
  1981. #if SANITIZER_INTERCEPT_READDIR
  1982. INTERCEPTOR(__sanitizer_dirent *, readdir, void *dirp) {
  1983. void *ctx;
  1984. COMMON_INTERCEPTOR_ENTER(ctx, readdir, dirp);
  1985. // FIXME: under ASan the call below may write to freed memory and corrupt
  1986. // its metadata. See
  1987. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1988. __sanitizer_dirent *res = REAL(readdir)(dirp);
  1989. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
  1990. return res;
  1991. }
  1992. INTERCEPTOR(int, readdir_r, void *dirp, __sanitizer_dirent *entry,
  1993. __sanitizer_dirent **result) {
  1994. void *ctx;
  1995. COMMON_INTERCEPTOR_ENTER(ctx, readdir_r, dirp, entry, result);
  1996. // FIXME: under ASan the call below may write to freed memory and corrupt
  1997. // its metadata. See
  1998. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  1999. int res = REAL(readdir_r)(dirp, entry, result);
  2000. if (!res) {
  2001. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  2002. if (*result)
  2003. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
  2004. }
  2005. return res;
  2006. }
  2007. #define INIT_READDIR \
  2008. COMMON_INTERCEPT_FUNCTION(readdir); \
  2009. COMMON_INTERCEPT_FUNCTION(readdir_r);
  2010. #else
  2011. #define INIT_READDIR
  2012. #endif
  2013. #if SANITIZER_INTERCEPT_READDIR64
  2014. INTERCEPTOR(__sanitizer_dirent64 *, readdir64, void *dirp) {
  2015. void *ctx;
  2016. COMMON_INTERCEPTOR_ENTER(ctx, readdir64, dirp);
  2017. // FIXME: under ASan the call below may write to freed memory and corrupt
  2018. // its metadata. See
  2019. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2020. __sanitizer_dirent64 *res = REAL(readdir64)(dirp);
  2021. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, res->d_reclen);
  2022. return res;
  2023. }
  2024. INTERCEPTOR(int, readdir64_r, void *dirp, __sanitizer_dirent64 *entry,
  2025. __sanitizer_dirent64 **result) {
  2026. void *ctx;
  2027. COMMON_INTERCEPTOR_ENTER(ctx, readdir64_r, dirp, entry, result);
  2028. // FIXME: under ASan the call below may write to freed memory and corrupt
  2029. // its metadata. See
  2030. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2031. int res = REAL(readdir64_r)(dirp, entry, result);
  2032. if (!res) {
  2033. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  2034. if (*result)
  2035. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *result, (*result)->d_reclen);
  2036. }
  2037. return res;
  2038. }
  2039. #define INIT_READDIR64 \
  2040. COMMON_INTERCEPT_FUNCTION(readdir64); \
  2041. COMMON_INTERCEPT_FUNCTION(readdir64_r);
  2042. #else
  2043. #define INIT_READDIR64
  2044. #endif
  2045. #if SANITIZER_INTERCEPT_PTRACE
  2046. INTERCEPTOR(uptr, ptrace, int request, int pid, void *addr, void *data) {
  2047. void *ctx;
  2048. COMMON_INTERCEPTOR_ENTER(ctx, ptrace, request, pid, addr, data);
  2049. if (data) {
  2050. if (request == ptrace_setregs)
  2051. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_regs_struct_sz);
  2052. else if (request == ptrace_setfpregs)
  2053. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpregs_struct_sz);
  2054. else if (request == ptrace_setfpxregs)
  2055. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
  2056. else if (request == ptrace_setsiginfo)
  2057. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, siginfo_t_sz);
  2058. else if (request == ptrace_setregset) {
  2059. __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
  2060. COMMON_INTERCEPTOR_READ_RANGE(ctx, iov->iov_base, iov->iov_len);
  2061. }
  2062. }
  2063. // FIXME: under ASan the call below may write to freed memory and corrupt
  2064. // its metadata. See
  2065. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2066. uptr res = REAL(ptrace)(request, pid, addr, data);
  2067. if (!res && data) {
  2068. // Note that PEEK* requests assign different meaning to the return value.
  2069. // This function does not handle them (nor does it need to).
  2070. if (request == ptrace_getregs)
  2071. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_regs_struct_sz);
  2072. else if (request == ptrace_getfpregs)
  2073. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpregs_struct_sz);
  2074. else if (request == ptrace_getfpxregs)
  2075. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, struct_user_fpxregs_struct_sz);
  2076. else if (request == ptrace_getsiginfo)
  2077. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, siginfo_t_sz);
  2078. else if (request == ptrace_geteventmsg)
  2079. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(unsigned long));
  2080. else if (request == ptrace_getregset) {
  2081. __sanitizer_iovec *iov = (__sanitizer_iovec *)data;
  2082. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, iov->iov_base, iov->iov_len);
  2083. }
  2084. }
  2085. return res;
  2086. }
  2087. #define INIT_PTRACE COMMON_INTERCEPT_FUNCTION(ptrace);
  2088. #else
  2089. #define INIT_PTRACE
  2090. #endif
  2091. #if SANITIZER_INTERCEPT_SETLOCALE
  2092. INTERCEPTOR(char *, setlocale, int category, char *locale) {
  2093. void *ctx;
  2094. COMMON_INTERCEPTOR_ENTER(ctx, setlocale, category, locale);
  2095. if (locale)
  2096. COMMON_INTERCEPTOR_READ_RANGE(ctx, locale, REAL(strlen)(locale) + 1);
  2097. char *res = REAL(setlocale)(category, locale);
  2098. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  2099. return res;
  2100. }
  2101. #define INIT_SETLOCALE COMMON_INTERCEPT_FUNCTION(setlocale);
  2102. #else
  2103. #define INIT_SETLOCALE
  2104. #endif
  2105. #if SANITIZER_INTERCEPT_GETCWD
  2106. INTERCEPTOR(char *, getcwd, char *buf, SIZE_T size) {
  2107. void *ctx;
  2108. COMMON_INTERCEPTOR_ENTER(ctx, getcwd, buf, size);
  2109. // FIXME: under ASan the call below may write to freed memory and corrupt
  2110. // its metadata. See
  2111. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2112. char *res = REAL(getcwd)(buf, size);
  2113. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  2114. return res;
  2115. }
  2116. #define INIT_GETCWD COMMON_INTERCEPT_FUNCTION(getcwd);
  2117. #else
  2118. #define INIT_GETCWD
  2119. #endif
  2120. #if SANITIZER_INTERCEPT_GET_CURRENT_DIR_NAME
  2121. INTERCEPTOR(char *, get_current_dir_name, int fake) {
  2122. void *ctx;
  2123. COMMON_INTERCEPTOR_ENTER(ctx, get_current_dir_name, fake);
  2124. // FIXME: under ASan the call below may write to freed memory and corrupt
  2125. // its metadata. See
  2126. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2127. char *res = REAL(get_current_dir_name)(fake);
  2128. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  2129. return res;
  2130. }
  2131. #define INIT_GET_CURRENT_DIR_NAME \
  2132. COMMON_INTERCEPT_FUNCTION(get_current_dir_name);
  2133. #else
  2134. #define INIT_GET_CURRENT_DIR_NAME
  2135. #endif
  2136. #if SANITIZER_INTERCEPT_STRTOIMAX
  2137. INTERCEPTOR(INTMAX_T, strtoimax, const char *nptr, char **endptr, int base) {
  2138. void *ctx;
  2139. COMMON_INTERCEPTOR_ENTER(ctx, strtoimax, nptr, endptr, base);
  2140. // FIXME: under ASan the call below may write to freed memory and corrupt
  2141. // its metadata. See
  2142. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2143. INTMAX_T res = REAL(strtoimax)(nptr, endptr, base);
  2144. if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
  2145. return res;
  2146. }
  2147. INTERCEPTOR(INTMAX_T, strtoumax, const char *nptr, char **endptr, int base) {
  2148. void *ctx;
  2149. COMMON_INTERCEPTOR_ENTER(ctx, strtoumax, nptr, endptr, base);
  2150. // FIXME: under ASan the call below may write to freed memory and corrupt
  2151. // its metadata. See
  2152. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2153. INTMAX_T res = REAL(strtoumax)(nptr, endptr, base);
  2154. if (endptr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, endptr, sizeof(*endptr));
  2155. return res;
  2156. }
  2157. #define INIT_STRTOIMAX \
  2158. COMMON_INTERCEPT_FUNCTION(strtoimax); \
  2159. COMMON_INTERCEPT_FUNCTION(strtoumax);
  2160. #else
  2161. #define INIT_STRTOIMAX
  2162. #endif
  2163. #if SANITIZER_INTERCEPT_MBSTOWCS
  2164. INTERCEPTOR(SIZE_T, mbstowcs, wchar_t *dest, const char *src, SIZE_T len) {
  2165. void *ctx;
  2166. COMMON_INTERCEPTOR_ENTER(ctx, mbstowcs, dest, src, len);
  2167. // FIXME: under ASan the call below may write to freed memory and corrupt
  2168. // its metadata. See
  2169. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2170. SIZE_T res = REAL(mbstowcs)(dest, src, len);
  2171. if (res != (SIZE_T) - 1 && dest) {
  2172. SIZE_T write_cnt = res + (res < len);
  2173. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
  2174. }
  2175. return res;
  2176. }
  2177. INTERCEPTOR(SIZE_T, mbsrtowcs, wchar_t *dest, const char **src, SIZE_T len,
  2178. void *ps) {
  2179. void *ctx;
  2180. COMMON_INTERCEPTOR_ENTER(ctx, mbsrtowcs, dest, src, len, ps);
  2181. if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  2182. if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
  2183. // FIXME: under ASan the call below may write to freed memory and corrupt
  2184. // its metadata. See
  2185. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2186. SIZE_T res = REAL(mbsrtowcs)(dest, src, len, ps);
  2187. if (res != (SIZE_T)(-1) && dest && src) {
  2188. // This function, and several others, may or may not write the terminating
  2189. // \0 character. They write it iff they clear *src.
  2190. SIZE_T write_cnt = res + !*src;
  2191. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
  2192. }
  2193. return res;
  2194. }
  2195. #define INIT_MBSTOWCS \
  2196. COMMON_INTERCEPT_FUNCTION(mbstowcs); \
  2197. COMMON_INTERCEPT_FUNCTION(mbsrtowcs);
  2198. #else
  2199. #define INIT_MBSTOWCS
  2200. #endif
  2201. #if SANITIZER_INTERCEPT_MBSNRTOWCS
  2202. INTERCEPTOR(SIZE_T, mbsnrtowcs, wchar_t *dest, const char **src, SIZE_T nms,
  2203. SIZE_T len, void *ps) {
  2204. void *ctx;
  2205. COMMON_INTERCEPTOR_ENTER(ctx, mbsnrtowcs, dest, src, nms, len, ps);
  2206. if (src) {
  2207. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  2208. if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
  2209. }
  2210. if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
  2211. // FIXME: under ASan the call below may write to freed memory and corrupt
  2212. // its metadata. See
  2213. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2214. SIZE_T res = REAL(mbsnrtowcs)(dest, src, nms, len, ps);
  2215. if (res != (SIZE_T)(-1) && dest && src) {
  2216. SIZE_T write_cnt = res + !*src;
  2217. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt * sizeof(wchar_t));
  2218. }
  2219. return res;
  2220. }
  2221. #define INIT_MBSNRTOWCS COMMON_INTERCEPT_FUNCTION(mbsnrtowcs);
  2222. #else
  2223. #define INIT_MBSNRTOWCS
  2224. #endif
  2225. #if SANITIZER_INTERCEPT_WCSTOMBS
  2226. INTERCEPTOR(SIZE_T, wcstombs, char *dest, const wchar_t *src, SIZE_T len) {
  2227. void *ctx;
  2228. COMMON_INTERCEPTOR_ENTER(ctx, wcstombs, dest, src, len);
  2229. // FIXME: under ASan the call below may write to freed memory and corrupt
  2230. // its metadata. See
  2231. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2232. SIZE_T res = REAL(wcstombs)(dest, src, len);
  2233. if (res != (SIZE_T) - 1 && dest) {
  2234. SIZE_T write_cnt = res + (res < len);
  2235. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
  2236. }
  2237. return res;
  2238. }
  2239. INTERCEPTOR(SIZE_T, wcsrtombs, char *dest, const wchar_t **src, SIZE_T len,
  2240. void *ps) {
  2241. void *ctx;
  2242. COMMON_INTERCEPTOR_ENTER(ctx, wcsrtombs, dest, src, len, ps);
  2243. if (src) COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  2244. if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
  2245. // FIXME: under ASan the call below may write to freed memory and corrupt
  2246. // its metadata. See
  2247. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2248. SIZE_T res = REAL(wcsrtombs)(dest, src, len, ps);
  2249. if (res != (SIZE_T) - 1 && dest && src) {
  2250. SIZE_T write_cnt = res + !*src;
  2251. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
  2252. }
  2253. return res;
  2254. }
  2255. #define INIT_WCSTOMBS \
  2256. COMMON_INTERCEPT_FUNCTION(wcstombs); \
  2257. COMMON_INTERCEPT_FUNCTION(wcsrtombs);
  2258. #else
  2259. #define INIT_WCSTOMBS
  2260. #endif
  2261. #if SANITIZER_INTERCEPT_WCSNRTOMBS
  2262. INTERCEPTOR(SIZE_T, wcsnrtombs, char *dest, const wchar_t **src, SIZE_T nms,
  2263. SIZE_T len, void *ps) {
  2264. void *ctx;
  2265. COMMON_INTERCEPTOR_ENTER(ctx, wcsnrtombs, dest, src, nms, len, ps);
  2266. if (src) {
  2267. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  2268. if (nms) COMMON_INTERCEPTOR_READ_RANGE(ctx, *src, nms);
  2269. }
  2270. if (ps) COMMON_INTERCEPTOR_READ_RANGE(ctx, ps, mbstate_t_sz);
  2271. // FIXME: under ASan the call below may write to freed memory and corrupt
  2272. // its metadata. See
  2273. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2274. SIZE_T res = REAL(wcsnrtombs)(dest, src, nms, len, ps);
  2275. if (res != (SIZE_T) - 1 && dest && src) {
  2276. SIZE_T write_cnt = res + !*src;
  2277. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, write_cnt);
  2278. }
  2279. return res;
  2280. }
  2281. #define INIT_WCSNRTOMBS COMMON_INTERCEPT_FUNCTION(wcsnrtombs);
  2282. #else
  2283. #define INIT_WCSNRTOMBS
  2284. #endif
  2285. #if SANITIZER_INTERCEPT_TCGETATTR
  2286. INTERCEPTOR(int, tcgetattr, int fd, void *termios_p) {
  2287. void *ctx;
  2288. COMMON_INTERCEPTOR_ENTER(ctx, tcgetattr, fd, termios_p);
  2289. // FIXME: under ASan the call below may write to freed memory and corrupt
  2290. // its metadata. See
  2291. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2292. int res = REAL(tcgetattr)(fd, termios_p);
  2293. if (!res && termios_p)
  2294. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, termios_p, struct_termios_sz);
  2295. return res;
  2296. }
  2297. #define INIT_TCGETATTR COMMON_INTERCEPT_FUNCTION(tcgetattr);
  2298. #else
  2299. #define INIT_TCGETATTR
  2300. #endif
  2301. #if SANITIZER_INTERCEPT_REALPATH
  2302. INTERCEPTOR(char *, realpath, const char *path, char *resolved_path) {
  2303. void *ctx;
  2304. COMMON_INTERCEPTOR_ENTER(ctx, realpath, path, resolved_path);
  2305. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  2306. // Workaround a bug in glibc where dlsym(RTLD_NEXT, ...) returns the oldest
  2307. // version of a versioned symbol. For realpath(), this gives us something
  2308. // (called __old_realpath) that does not handle NULL in the second argument.
  2309. // Handle it as part of the interceptor.
  2310. char *allocated_path = 0;
  2311. if (!resolved_path)
  2312. allocated_path = resolved_path = (char *)WRAP(malloc)(path_max + 1);
  2313. char *res = REAL(realpath)(path, resolved_path);
  2314. if (allocated_path && !res) WRAP(free)(allocated_path);
  2315. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  2316. return res;
  2317. }
  2318. #define INIT_REALPATH COMMON_INTERCEPT_FUNCTION(realpath);
  2319. #else
  2320. #define INIT_REALPATH
  2321. #endif
  2322. #if SANITIZER_INTERCEPT_CANONICALIZE_FILE_NAME
  2323. INTERCEPTOR(char *, canonicalize_file_name, const char *path) {
  2324. void *ctx;
  2325. COMMON_INTERCEPTOR_ENTER(ctx, canonicalize_file_name, path);
  2326. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  2327. char *res = REAL(canonicalize_file_name)(path);
  2328. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  2329. return res;
  2330. }
  2331. #define INIT_CANONICALIZE_FILE_NAME \
  2332. COMMON_INTERCEPT_FUNCTION(canonicalize_file_name);
  2333. #else
  2334. #define INIT_CANONICALIZE_FILE_NAME
  2335. #endif
  2336. #if SANITIZER_INTERCEPT_CONFSTR
  2337. INTERCEPTOR(SIZE_T, confstr, int name, char *buf, SIZE_T len) {
  2338. void *ctx;
  2339. COMMON_INTERCEPTOR_ENTER(ctx, confstr, name, buf, len);
  2340. // FIXME: under ASan the call below may write to freed memory and corrupt
  2341. // its metadata. See
  2342. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2343. SIZE_T res = REAL(confstr)(name, buf, len);
  2344. if (buf && res)
  2345. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res < len ? res : len);
  2346. return res;
  2347. }
  2348. #define INIT_CONFSTR COMMON_INTERCEPT_FUNCTION(confstr);
  2349. #else
  2350. #define INIT_CONFSTR
  2351. #endif
  2352. #if SANITIZER_INTERCEPT_SCHED_GETAFFINITY
  2353. INTERCEPTOR(int, sched_getaffinity, int pid, SIZE_T cpusetsize, void *mask) {
  2354. void *ctx;
  2355. COMMON_INTERCEPTOR_ENTER(ctx, sched_getaffinity, pid, cpusetsize, mask);
  2356. // FIXME: under ASan the call below may write to freed memory and corrupt
  2357. // its metadata. See
  2358. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2359. int res = REAL(sched_getaffinity)(pid, cpusetsize, mask);
  2360. if (mask && !res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, cpusetsize);
  2361. return res;
  2362. }
  2363. #define INIT_SCHED_GETAFFINITY COMMON_INTERCEPT_FUNCTION(sched_getaffinity);
  2364. #else
  2365. #define INIT_SCHED_GETAFFINITY
  2366. #endif
  2367. #if SANITIZER_INTERCEPT_STRERROR
  2368. INTERCEPTOR(char *, strerror, int errnum) {
  2369. void *ctx;
  2370. COMMON_INTERCEPTOR_ENTER(ctx, strerror, errnum);
  2371. char *res = REAL(strerror)(errnum);
  2372. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
  2373. return res;
  2374. }
  2375. #define INIT_STRERROR COMMON_INTERCEPT_FUNCTION(strerror);
  2376. #else
  2377. #define INIT_STRERROR
  2378. #endif
  2379. #if SANITIZER_INTERCEPT_STRERROR_R
  2380. INTERCEPTOR(char *, strerror_r, int errnum, char *buf, SIZE_T buflen) {
  2381. void *ctx;
  2382. COMMON_INTERCEPTOR_ENTER(ctx, strerror_r, errnum, buf, buflen);
  2383. // FIXME: under ASan the call below may write to freed memory and corrupt
  2384. // its metadata. See
  2385. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2386. char *res = REAL(strerror_r)(errnum, buf, buflen);
  2387. // There are 2 versions of strerror_r:
  2388. // * POSIX version returns 0 on success, negative error code on failure,
  2389. // writes message to buf.
  2390. // * GNU version returns message pointer, which points to either buf or some
  2391. // static storage.
  2392. SIZE_T posix_res = (SIZE_T)res;
  2393. if (posix_res < 1024 || posix_res > (SIZE_T) - 1024) {
  2394. // POSIX version. Spec is not clear on whether buf is NULL-terminated.
  2395. // At least on OSX, buf contents are valid even when the call fails.
  2396. SIZE_T sz = internal_strnlen(buf, buflen);
  2397. if (sz < buflen) ++sz;
  2398. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
  2399. } else {
  2400. // GNU version.
  2401. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  2402. }
  2403. return res;
  2404. }
  2405. #define INIT_STRERROR_R COMMON_INTERCEPT_FUNCTION(strerror_r);
  2406. #else
  2407. #define INIT_STRERROR_R
  2408. #endif
  2409. #if SANITIZER_INTERCEPT_XPG_STRERROR_R
  2410. INTERCEPTOR(int, __xpg_strerror_r, int errnum, char *buf, SIZE_T buflen) {
  2411. void *ctx;
  2412. COMMON_INTERCEPTOR_ENTER(ctx, __xpg_strerror_r, errnum, buf, buflen);
  2413. // FIXME: under ASan the call below may write to freed memory and corrupt
  2414. // its metadata. See
  2415. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2416. int res = REAL(__xpg_strerror_r)(errnum, buf, buflen);
  2417. // This version always returns a null-terminated string.
  2418. if (buf && buflen)
  2419. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
  2420. return res;
  2421. }
  2422. #define INIT_XPG_STRERROR_R COMMON_INTERCEPT_FUNCTION(__xpg_strerror_r);
  2423. #else
  2424. #define INIT_XPG_STRERROR_R
  2425. #endif
  2426. #if SANITIZER_INTERCEPT_SCANDIR
  2427. typedef int (*scandir_filter_f)(const struct __sanitizer_dirent *);
  2428. typedef int (*scandir_compar_f)(const struct __sanitizer_dirent **,
  2429. const struct __sanitizer_dirent **);
  2430. static THREADLOCAL scandir_filter_f scandir_filter;
  2431. static THREADLOCAL scandir_compar_f scandir_compar;
  2432. static int wrapped_scandir_filter(const struct __sanitizer_dirent *dir) {
  2433. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  2434. COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
  2435. return IndirectExternCall(scandir_filter)(dir);
  2436. }
  2437. static int wrapped_scandir_compar(const struct __sanitizer_dirent **a,
  2438. const struct __sanitizer_dirent **b) {
  2439. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  2440. COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
  2441. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
  2442. COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
  2443. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
  2444. return IndirectExternCall(scandir_compar)(a, b);
  2445. }
  2446. INTERCEPTOR(int, scandir, char *dirp, __sanitizer_dirent ***namelist,
  2447. scandir_filter_f filter, scandir_compar_f compar) {
  2448. void *ctx;
  2449. COMMON_INTERCEPTOR_ENTER(ctx, scandir, dirp, namelist, filter, compar);
  2450. if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
  2451. scandir_filter = filter;
  2452. scandir_compar = compar;
  2453. // FIXME: under ASan the call below may write to freed memory and corrupt
  2454. // its metadata. See
  2455. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2456. int res = REAL(scandir)(dirp, namelist, filter ? wrapped_scandir_filter : 0,
  2457. compar ? wrapped_scandir_compar : 0);
  2458. scandir_filter = 0;
  2459. scandir_compar = 0;
  2460. if (namelist && res > 0) {
  2461. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
  2462. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
  2463. for (int i = 0; i < res; ++i)
  2464. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
  2465. (*namelist)[i]->d_reclen);
  2466. }
  2467. return res;
  2468. }
  2469. #define INIT_SCANDIR COMMON_INTERCEPT_FUNCTION(scandir);
  2470. #else
  2471. #define INIT_SCANDIR
  2472. #endif
  2473. #if SANITIZER_INTERCEPT_SCANDIR64
  2474. typedef int (*scandir64_filter_f)(const struct __sanitizer_dirent64 *);
  2475. typedef int (*scandir64_compar_f)(const struct __sanitizer_dirent64 **,
  2476. const struct __sanitizer_dirent64 **);
  2477. static THREADLOCAL scandir64_filter_f scandir64_filter;
  2478. static THREADLOCAL scandir64_compar_f scandir64_compar;
  2479. static int wrapped_scandir64_filter(const struct __sanitizer_dirent64 *dir) {
  2480. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  2481. COMMON_INTERCEPTOR_INITIALIZE_RANGE(dir, dir->d_reclen);
  2482. return IndirectExternCall(scandir64_filter)(dir);
  2483. }
  2484. static int wrapped_scandir64_compar(const struct __sanitizer_dirent64 **a,
  2485. const struct __sanitizer_dirent64 **b) {
  2486. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  2487. COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, sizeof(*a));
  2488. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*a, (*a)->d_reclen);
  2489. COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, sizeof(*b));
  2490. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*b, (*b)->d_reclen);
  2491. return IndirectExternCall(scandir64_compar)(a, b);
  2492. }
  2493. INTERCEPTOR(int, scandir64, char *dirp, __sanitizer_dirent64 ***namelist,
  2494. scandir64_filter_f filter, scandir64_compar_f compar) {
  2495. void *ctx;
  2496. COMMON_INTERCEPTOR_ENTER(ctx, scandir64, dirp, namelist, filter, compar);
  2497. if (dirp) COMMON_INTERCEPTOR_READ_RANGE(ctx, dirp, REAL(strlen)(dirp) + 1);
  2498. scandir64_filter = filter;
  2499. scandir64_compar = compar;
  2500. // FIXME: under ASan the call below may write to freed memory and corrupt
  2501. // its metadata. See
  2502. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2503. int res =
  2504. REAL(scandir64)(dirp, namelist, filter ? wrapped_scandir64_filter : 0,
  2505. compar ? wrapped_scandir64_compar : 0);
  2506. scandir64_filter = 0;
  2507. scandir64_compar = 0;
  2508. if (namelist && res > 0) {
  2509. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelist, sizeof(*namelist));
  2510. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *namelist, sizeof(**namelist) * res);
  2511. for (int i = 0; i < res; ++i)
  2512. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (*namelist)[i],
  2513. (*namelist)[i]->d_reclen);
  2514. }
  2515. return res;
  2516. }
  2517. #define INIT_SCANDIR64 COMMON_INTERCEPT_FUNCTION(scandir64);
  2518. #else
  2519. #define INIT_SCANDIR64
  2520. #endif
  2521. #if SANITIZER_INTERCEPT_GETGROUPS
  2522. INTERCEPTOR(int, getgroups, int size, u32 *lst) {
  2523. void *ctx;
  2524. COMMON_INTERCEPTOR_ENTER(ctx, getgroups, size, lst);
  2525. // FIXME: under ASan the call below may write to freed memory and corrupt
  2526. // its metadata. See
  2527. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2528. int res = REAL(getgroups)(size, lst);
  2529. if (res && lst) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lst, res * sizeof(*lst));
  2530. return res;
  2531. }
  2532. #define INIT_GETGROUPS COMMON_INTERCEPT_FUNCTION(getgroups);
  2533. #else
  2534. #define INIT_GETGROUPS
  2535. #endif
  2536. #if SANITIZER_INTERCEPT_POLL
  2537. static void read_pollfd(void *ctx, __sanitizer_pollfd *fds,
  2538. __sanitizer_nfds_t nfds) {
  2539. for (unsigned i = 0; i < nfds; ++i) {
  2540. COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].fd, sizeof(fds[i].fd));
  2541. COMMON_INTERCEPTOR_READ_RANGE(ctx, &fds[i].events, sizeof(fds[i].events));
  2542. }
  2543. }
  2544. static void write_pollfd(void *ctx, __sanitizer_pollfd *fds,
  2545. __sanitizer_nfds_t nfds) {
  2546. for (unsigned i = 0; i < nfds; ++i)
  2547. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &fds[i].revents,
  2548. sizeof(fds[i].revents));
  2549. }
  2550. INTERCEPTOR(int, poll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
  2551. int timeout) {
  2552. void *ctx;
  2553. COMMON_INTERCEPTOR_ENTER(ctx, poll, fds, nfds, timeout);
  2554. if (fds && nfds) read_pollfd(ctx, fds, nfds);
  2555. int res = COMMON_INTERCEPTOR_BLOCK_REAL(poll)(fds, nfds, timeout);
  2556. if (fds && nfds) write_pollfd(ctx, fds, nfds);
  2557. return res;
  2558. }
  2559. #define INIT_POLL COMMON_INTERCEPT_FUNCTION(poll);
  2560. #else
  2561. #define INIT_POLL
  2562. #endif
  2563. #if SANITIZER_INTERCEPT_PPOLL
  2564. INTERCEPTOR(int, ppoll, __sanitizer_pollfd *fds, __sanitizer_nfds_t nfds,
  2565. void *timeout_ts, __sanitizer_sigset_t *sigmask) {
  2566. void *ctx;
  2567. COMMON_INTERCEPTOR_ENTER(ctx, ppoll, fds, nfds, timeout_ts, sigmask);
  2568. if (fds && nfds) read_pollfd(ctx, fds, nfds);
  2569. if (timeout_ts)
  2570. COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout_ts, struct_timespec_sz);
  2571. // FIXME: read sigmask when all of sigemptyset, etc are intercepted.
  2572. int res =
  2573. COMMON_INTERCEPTOR_BLOCK_REAL(ppoll)(fds, nfds, timeout_ts, sigmask);
  2574. if (fds && nfds) write_pollfd(ctx, fds, nfds);
  2575. return res;
  2576. }
  2577. #define INIT_PPOLL COMMON_INTERCEPT_FUNCTION(ppoll);
  2578. #else
  2579. #define INIT_PPOLL
  2580. #endif
  2581. #if SANITIZER_INTERCEPT_WORDEXP
  2582. INTERCEPTOR(int, wordexp, char *s, __sanitizer_wordexp_t *p, int flags) {
  2583. void *ctx;
  2584. COMMON_INTERCEPTOR_ENTER(ctx, wordexp, s, p, flags);
  2585. if (s) COMMON_INTERCEPTOR_READ_RANGE(ctx, s, REAL(strlen)(s) + 1);
  2586. // FIXME: under ASan the call below may write to freed memory and corrupt
  2587. // its metadata. See
  2588. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2589. int res = REAL(wordexp)(s, p, flags);
  2590. if (!res && p) {
  2591. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
  2592. if (p->we_wordc)
  2593. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->we_wordv,
  2594. sizeof(*p->we_wordv) * p->we_wordc);
  2595. for (uptr i = 0; i < p->we_wordc; ++i) {
  2596. char *w = p->we_wordv[i];
  2597. if (w) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, w, REAL(strlen)(w) + 1);
  2598. }
  2599. }
  2600. return res;
  2601. }
  2602. #define INIT_WORDEXP COMMON_INTERCEPT_FUNCTION(wordexp);
  2603. #else
  2604. #define INIT_WORDEXP
  2605. #endif
  2606. #if SANITIZER_INTERCEPT_SIGWAIT
  2607. INTERCEPTOR(int, sigwait, __sanitizer_sigset_t *set, int *sig) {
  2608. void *ctx;
  2609. COMMON_INTERCEPTOR_ENTER(ctx, sigwait, set, sig);
  2610. // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
  2611. // FIXME: under ASan the call below may write to freed memory and corrupt
  2612. // its metadata. See
  2613. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2614. int res = REAL(sigwait)(set, sig);
  2615. if (!res && sig) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sig, sizeof(*sig));
  2616. return res;
  2617. }
  2618. #define INIT_SIGWAIT COMMON_INTERCEPT_FUNCTION(sigwait);
  2619. #else
  2620. #define INIT_SIGWAIT
  2621. #endif
  2622. #if SANITIZER_INTERCEPT_SIGWAITINFO
  2623. INTERCEPTOR(int, sigwaitinfo, __sanitizer_sigset_t *set, void *info) {
  2624. void *ctx;
  2625. COMMON_INTERCEPTOR_ENTER(ctx, sigwaitinfo, set, info);
  2626. // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
  2627. // FIXME: under ASan the call below may write to freed memory and corrupt
  2628. // its metadata. See
  2629. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2630. int res = REAL(sigwaitinfo)(set, info);
  2631. if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
  2632. return res;
  2633. }
  2634. #define INIT_SIGWAITINFO COMMON_INTERCEPT_FUNCTION(sigwaitinfo);
  2635. #else
  2636. #define INIT_SIGWAITINFO
  2637. #endif
  2638. #if SANITIZER_INTERCEPT_SIGTIMEDWAIT
  2639. INTERCEPTOR(int, sigtimedwait, __sanitizer_sigset_t *set, void *info,
  2640. void *timeout) {
  2641. void *ctx;
  2642. COMMON_INTERCEPTOR_ENTER(ctx, sigtimedwait, set, info, timeout);
  2643. if (timeout) COMMON_INTERCEPTOR_READ_RANGE(ctx, timeout, struct_timespec_sz);
  2644. // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
  2645. // FIXME: under ASan the call below may write to freed memory and corrupt
  2646. // its metadata. See
  2647. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2648. int res = REAL(sigtimedwait)(set, info, timeout);
  2649. if (res > 0 && info) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, info, siginfo_t_sz);
  2650. return res;
  2651. }
  2652. #define INIT_SIGTIMEDWAIT COMMON_INTERCEPT_FUNCTION(sigtimedwait);
  2653. #else
  2654. #define INIT_SIGTIMEDWAIT
  2655. #endif
  2656. #if SANITIZER_INTERCEPT_SIGSETOPS
  2657. INTERCEPTOR(int, sigemptyset, __sanitizer_sigset_t *set) {
  2658. void *ctx;
  2659. COMMON_INTERCEPTOR_ENTER(ctx, sigemptyset, set);
  2660. // FIXME: under ASan the call below may write to freed memory and corrupt
  2661. // its metadata. See
  2662. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2663. int res = REAL(sigemptyset)(set);
  2664. if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
  2665. return res;
  2666. }
  2667. INTERCEPTOR(int, sigfillset, __sanitizer_sigset_t *set) {
  2668. void *ctx;
  2669. COMMON_INTERCEPTOR_ENTER(ctx, sigfillset, set);
  2670. // FIXME: under ASan the call below may write to freed memory and corrupt
  2671. // its metadata. See
  2672. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2673. int res = REAL(sigfillset)(set);
  2674. if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
  2675. return res;
  2676. }
  2677. #define INIT_SIGSETOPS \
  2678. COMMON_INTERCEPT_FUNCTION(sigemptyset); \
  2679. COMMON_INTERCEPT_FUNCTION(sigfillset);
  2680. #else
  2681. #define INIT_SIGSETOPS
  2682. #endif
  2683. #if SANITIZER_INTERCEPT_SIGPENDING
  2684. INTERCEPTOR(int, sigpending, __sanitizer_sigset_t *set) {
  2685. void *ctx;
  2686. COMMON_INTERCEPTOR_ENTER(ctx, sigpending, set);
  2687. // FIXME: under ASan the call below may write to freed memory and corrupt
  2688. // its metadata. See
  2689. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2690. int res = REAL(sigpending)(set);
  2691. if (!res && set) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, set, sizeof(*set));
  2692. return res;
  2693. }
  2694. #define INIT_SIGPENDING COMMON_INTERCEPT_FUNCTION(sigpending);
  2695. #else
  2696. #define INIT_SIGPENDING
  2697. #endif
  2698. #if SANITIZER_INTERCEPT_SIGPROCMASK
  2699. INTERCEPTOR(int, sigprocmask, int how, __sanitizer_sigset_t *set,
  2700. __sanitizer_sigset_t *oldset) {
  2701. void *ctx;
  2702. COMMON_INTERCEPTOR_ENTER(ctx, sigprocmask, how, set, oldset);
  2703. // FIXME: read sigset_t when all of sigemptyset, etc are intercepted
  2704. // FIXME: under ASan the call below may write to freed memory and corrupt
  2705. // its metadata. See
  2706. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2707. int res = REAL(sigprocmask)(how, set, oldset);
  2708. if (!res && oldset)
  2709. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldset, sizeof(*oldset));
  2710. return res;
  2711. }
  2712. #define INIT_SIGPROCMASK COMMON_INTERCEPT_FUNCTION(sigprocmask);
  2713. #else
  2714. #define INIT_SIGPROCMASK
  2715. #endif
  2716. #if SANITIZER_INTERCEPT_BACKTRACE
  2717. INTERCEPTOR(int, backtrace, void **buffer, int size) {
  2718. void *ctx;
  2719. COMMON_INTERCEPTOR_ENTER(ctx, backtrace, buffer, size);
  2720. // FIXME: under ASan the call below may write to freed memory and corrupt
  2721. // its metadata. See
  2722. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2723. int res = REAL(backtrace)(buffer, size);
  2724. if (res && buffer)
  2725. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buffer, res * sizeof(*buffer));
  2726. return res;
  2727. }
  2728. INTERCEPTOR(char **, backtrace_symbols, void **buffer, int size) {
  2729. void *ctx;
  2730. COMMON_INTERCEPTOR_ENTER(ctx, backtrace_symbols, buffer, size);
  2731. if (buffer && size)
  2732. COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, size * sizeof(*buffer));
  2733. // FIXME: under ASan the call below may write to freed memory and corrupt
  2734. // its metadata. See
  2735. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2736. char **res = REAL(backtrace_symbols)(buffer, size);
  2737. if (res && size) {
  2738. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, size * sizeof(*res));
  2739. for (int i = 0; i < size; ++i)
  2740. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res[i], REAL(strlen(res[i])) + 1);
  2741. }
  2742. return res;
  2743. }
  2744. #define INIT_BACKTRACE \
  2745. COMMON_INTERCEPT_FUNCTION(backtrace); \
  2746. COMMON_INTERCEPT_FUNCTION(backtrace_symbols);
  2747. #else
  2748. #define INIT_BACKTRACE
  2749. #endif
  2750. #if SANITIZER_INTERCEPT__EXIT
  2751. INTERCEPTOR(void, _exit, int status) {
  2752. void *ctx;
  2753. COMMON_INTERCEPTOR_ENTER(ctx, _exit, status);
  2754. int status1 = COMMON_INTERCEPTOR_ON_EXIT(ctx);
  2755. if (status == 0) status = status1;
  2756. REAL(_exit)(status);
  2757. }
  2758. #define INIT__EXIT COMMON_INTERCEPT_FUNCTION(_exit);
  2759. #else
  2760. #define INIT__EXIT
  2761. #endif
  2762. #if SANITIZER_INTERCEPT_PHTREAD_MUTEX
  2763. INTERCEPTOR(int, pthread_mutex_lock, void *m) {
  2764. void *ctx;
  2765. COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_lock, m);
  2766. int res = REAL(pthread_mutex_lock)(m);
  2767. if (res == errno_EOWNERDEAD)
  2768. COMMON_INTERCEPTOR_MUTEX_REPAIR(ctx, m);
  2769. if (res == 0 || res == errno_EOWNERDEAD)
  2770. COMMON_INTERCEPTOR_MUTEX_LOCK(ctx, m);
  2771. return res;
  2772. }
  2773. INTERCEPTOR(int, pthread_mutex_unlock, void *m) {
  2774. void *ctx;
  2775. COMMON_INTERCEPTOR_ENTER(ctx, pthread_mutex_unlock, m);
  2776. COMMON_INTERCEPTOR_MUTEX_UNLOCK(ctx, m);
  2777. return REAL(pthread_mutex_unlock)(m);
  2778. }
  2779. #define INIT_PTHREAD_MUTEX_LOCK COMMON_INTERCEPT_FUNCTION(pthread_mutex_lock)
  2780. #define INIT_PTHREAD_MUTEX_UNLOCK \
  2781. COMMON_INTERCEPT_FUNCTION(pthread_mutex_unlock)
  2782. #else
  2783. #define INIT_PTHREAD_MUTEX_LOCK
  2784. #define INIT_PTHREAD_MUTEX_UNLOCK
  2785. #endif
  2786. #if SANITIZER_INTERCEPT_GETMNTENT || SANITIZER_INTERCEPT_GETMNTENT_R
  2787. static void write_mntent(void *ctx, __sanitizer_mntent *mnt) {
  2788. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt, sizeof(*mnt));
  2789. if (mnt->mnt_fsname)
  2790. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_fsname,
  2791. REAL(strlen)(mnt->mnt_fsname) + 1);
  2792. if (mnt->mnt_dir)
  2793. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_dir,
  2794. REAL(strlen)(mnt->mnt_dir) + 1);
  2795. if (mnt->mnt_type)
  2796. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_type,
  2797. REAL(strlen)(mnt->mnt_type) + 1);
  2798. if (mnt->mnt_opts)
  2799. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mnt->mnt_opts,
  2800. REAL(strlen)(mnt->mnt_opts) + 1);
  2801. }
  2802. #endif
  2803. #if SANITIZER_INTERCEPT_GETMNTENT
  2804. INTERCEPTOR(__sanitizer_mntent *, getmntent, void *fp) {
  2805. void *ctx;
  2806. COMMON_INTERCEPTOR_ENTER(ctx, getmntent, fp);
  2807. __sanitizer_mntent *res = REAL(getmntent)(fp);
  2808. if (res) write_mntent(ctx, res);
  2809. return res;
  2810. }
  2811. #define INIT_GETMNTENT COMMON_INTERCEPT_FUNCTION(getmntent);
  2812. #else
  2813. #define INIT_GETMNTENT
  2814. #endif
  2815. #if SANITIZER_INTERCEPT_GETMNTENT_R
  2816. INTERCEPTOR(__sanitizer_mntent *, getmntent_r, void *fp,
  2817. __sanitizer_mntent *mntbuf, char *buf, int buflen) {
  2818. void *ctx;
  2819. COMMON_INTERCEPTOR_ENTER(ctx, getmntent_r, fp, mntbuf, buf, buflen);
  2820. __sanitizer_mntent *res = REAL(getmntent_r)(fp, mntbuf, buf, buflen);
  2821. if (res) write_mntent(ctx, res);
  2822. return res;
  2823. }
  2824. #define INIT_GETMNTENT_R COMMON_INTERCEPT_FUNCTION(getmntent_r);
  2825. #else
  2826. #define INIT_GETMNTENT_R
  2827. #endif
  2828. #if SANITIZER_INTERCEPT_STATFS
  2829. INTERCEPTOR(int, statfs, char *path, void *buf) {
  2830. void *ctx;
  2831. COMMON_INTERCEPTOR_ENTER(ctx, statfs, path, buf);
  2832. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  2833. // FIXME: under ASan the call below may write to freed memory and corrupt
  2834. // its metadata. See
  2835. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2836. int res = REAL(statfs)(path, buf);
  2837. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
  2838. return res;
  2839. }
  2840. INTERCEPTOR(int, fstatfs, int fd, void *buf) {
  2841. void *ctx;
  2842. COMMON_INTERCEPTOR_ENTER(ctx, fstatfs, fd, buf);
  2843. // FIXME: under ASan the call below may write to freed memory and corrupt
  2844. // its metadata. See
  2845. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2846. int res = REAL(fstatfs)(fd, buf);
  2847. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs_sz);
  2848. return res;
  2849. }
  2850. #define INIT_STATFS \
  2851. COMMON_INTERCEPT_FUNCTION(statfs); \
  2852. COMMON_INTERCEPT_FUNCTION(fstatfs);
  2853. #else
  2854. #define INIT_STATFS
  2855. #endif
  2856. #if SANITIZER_INTERCEPT_STATFS64
  2857. INTERCEPTOR(int, statfs64, char *path, void *buf) {
  2858. void *ctx;
  2859. COMMON_INTERCEPTOR_ENTER(ctx, statfs64, path, buf);
  2860. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  2861. // FIXME: under ASan the call below may write to freed memory and corrupt
  2862. // its metadata. See
  2863. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2864. int res = REAL(statfs64)(path, buf);
  2865. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
  2866. return res;
  2867. }
  2868. INTERCEPTOR(int, fstatfs64, int fd, void *buf) {
  2869. void *ctx;
  2870. COMMON_INTERCEPTOR_ENTER(ctx, fstatfs64, fd, buf);
  2871. // FIXME: under ASan the call below may write to freed memory and corrupt
  2872. // its metadata. See
  2873. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2874. int res = REAL(fstatfs64)(fd, buf);
  2875. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statfs64_sz);
  2876. return res;
  2877. }
  2878. #define INIT_STATFS64 \
  2879. COMMON_INTERCEPT_FUNCTION(statfs64); \
  2880. COMMON_INTERCEPT_FUNCTION(fstatfs64);
  2881. #else
  2882. #define INIT_STATFS64
  2883. #endif
  2884. #if SANITIZER_INTERCEPT_STATVFS
  2885. INTERCEPTOR(int, statvfs, char *path, void *buf) {
  2886. void *ctx;
  2887. COMMON_INTERCEPTOR_ENTER(ctx, statvfs, path, buf);
  2888. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  2889. // FIXME: under ASan the call below may write to freed memory and corrupt
  2890. // its metadata. See
  2891. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2892. int res = REAL(statvfs)(path, buf);
  2893. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
  2894. return res;
  2895. }
  2896. INTERCEPTOR(int, fstatvfs, int fd, void *buf) {
  2897. void *ctx;
  2898. COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs, fd, buf);
  2899. // FIXME: under ASan the call below may write to freed memory and corrupt
  2900. // its metadata. See
  2901. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2902. int res = REAL(fstatvfs)(fd, buf);
  2903. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
  2904. return res;
  2905. }
  2906. #define INIT_STATVFS \
  2907. COMMON_INTERCEPT_FUNCTION(statvfs); \
  2908. COMMON_INTERCEPT_FUNCTION(fstatvfs);
  2909. #else
  2910. #define INIT_STATVFS
  2911. #endif
  2912. #if SANITIZER_INTERCEPT_STATVFS64
  2913. INTERCEPTOR(int, statvfs64, char *path, void *buf) {
  2914. void *ctx;
  2915. COMMON_INTERCEPTOR_ENTER(ctx, statvfs64, path, buf);
  2916. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  2917. // FIXME: under ASan the call below may write to freed memory and corrupt
  2918. // its metadata. See
  2919. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2920. int res = REAL(statvfs64)(path, buf);
  2921. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
  2922. return res;
  2923. }
  2924. INTERCEPTOR(int, fstatvfs64, int fd, void *buf) {
  2925. void *ctx;
  2926. COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs64, fd, buf);
  2927. // FIXME: under ASan the call below may write to freed memory and corrupt
  2928. // its metadata. See
  2929. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2930. int res = REAL(fstatvfs64)(fd, buf);
  2931. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs64_sz);
  2932. return res;
  2933. }
  2934. #define INIT_STATVFS64 \
  2935. COMMON_INTERCEPT_FUNCTION(statvfs64); \
  2936. COMMON_INTERCEPT_FUNCTION(fstatvfs64);
  2937. #else
  2938. #define INIT_STATVFS64
  2939. #endif
  2940. #if SANITIZER_INTERCEPT_INITGROUPS
  2941. INTERCEPTOR(int, initgroups, char *user, u32 group) {
  2942. void *ctx;
  2943. COMMON_INTERCEPTOR_ENTER(ctx, initgroups, user, group);
  2944. if (user) COMMON_INTERCEPTOR_READ_RANGE(ctx, user, REAL(strlen)(user) + 1);
  2945. int res = REAL(initgroups)(user, group);
  2946. return res;
  2947. }
  2948. #define INIT_INITGROUPS COMMON_INTERCEPT_FUNCTION(initgroups);
  2949. #else
  2950. #define INIT_INITGROUPS
  2951. #endif
  2952. #if SANITIZER_INTERCEPT_ETHER_NTOA_ATON
  2953. INTERCEPTOR(char *, ether_ntoa, __sanitizer_ether_addr *addr) {
  2954. void *ctx;
  2955. COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa, addr);
  2956. if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
  2957. char *res = REAL(ether_ntoa)(addr);
  2958. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
  2959. return res;
  2960. }
  2961. INTERCEPTOR(__sanitizer_ether_addr *, ether_aton, char *buf) {
  2962. void *ctx;
  2963. COMMON_INTERCEPTOR_ENTER(ctx, ether_aton, buf);
  2964. if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
  2965. __sanitizer_ether_addr *res = REAL(ether_aton)(buf);
  2966. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, sizeof(*res));
  2967. return res;
  2968. }
  2969. #define INIT_ETHER_NTOA_ATON \
  2970. COMMON_INTERCEPT_FUNCTION(ether_ntoa); \
  2971. COMMON_INTERCEPT_FUNCTION(ether_aton);
  2972. #else
  2973. #define INIT_ETHER_NTOA_ATON
  2974. #endif
  2975. #if SANITIZER_INTERCEPT_ETHER_HOST
  2976. INTERCEPTOR(int, ether_ntohost, char *hostname, __sanitizer_ether_addr *addr) {
  2977. void *ctx;
  2978. COMMON_INTERCEPTOR_ENTER(ctx, ether_ntohost, hostname, addr);
  2979. if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
  2980. // FIXME: under ASan the call below may write to freed memory and corrupt
  2981. // its metadata. See
  2982. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2983. int res = REAL(ether_ntohost)(hostname, addr);
  2984. if (!res && hostname)
  2985. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
  2986. return res;
  2987. }
  2988. INTERCEPTOR(int, ether_hostton, char *hostname, __sanitizer_ether_addr *addr) {
  2989. void *ctx;
  2990. COMMON_INTERCEPTOR_ENTER(ctx, ether_hostton, hostname, addr);
  2991. if (hostname)
  2992. COMMON_INTERCEPTOR_READ_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
  2993. // FIXME: under ASan the call below may write to freed memory and corrupt
  2994. // its metadata. See
  2995. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  2996. int res = REAL(ether_hostton)(hostname, addr);
  2997. if (!res && addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
  2998. return res;
  2999. }
  3000. INTERCEPTOR(int, ether_line, char *line, __sanitizer_ether_addr *addr,
  3001. char *hostname) {
  3002. void *ctx;
  3003. COMMON_INTERCEPTOR_ENTER(ctx, ether_line, line, addr, hostname);
  3004. if (line) COMMON_INTERCEPTOR_READ_RANGE(ctx, line, REAL(strlen)(line) + 1);
  3005. // FIXME: under ASan the call below may write to freed memory and corrupt
  3006. // its metadata. See
  3007. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3008. int res = REAL(ether_line)(line, addr, hostname);
  3009. if (!res) {
  3010. if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
  3011. if (hostname)
  3012. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hostname, REAL(strlen)(hostname) + 1);
  3013. }
  3014. return res;
  3015. }
  3016. #define INIT_ETHER_HOST \
  3017. COMMON_INTERCEPT_FUNCTION(ether_ntohost); \
  3018. COMMON_INTERCEPT_FUNCTION(ether_hostton); \
  3019. COMMON_INTERCEPT_FUNCTION(ether_line);
  3020. #else
  3021. #define INIT_ETHER_HOST
  3022. #endif
  3023. #if SANITIZER_INTERCEPT_ETHER_R
  3024. INTERCEPTOR(char *, ether_ntoa_r, __sanitizer_ether_addr *addr, char *buf) {
  3025. void *ctx;
  3026. COMMON_INTERCEPTOR_ENTER(ctx, ether_ntoa_r, addr, buf);
  3027. if (addr) COMMON_INTERCEPTOR_READ_RANGE(ctx, addr, sizeof(*addr));
  3028. // FIXME: under ASan the call below may write to freed memory and corrupt
  3029. // its metadata. See
  3030. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3031. char *res = REAL(ether_ntoa_r)(addr, buf);
  3032. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, REAL(strlen)(res) + 1);
  3033. return res;
  3034. }
  3035. INTERCEPTOR(__sanitizer_ether_addr *, ether_aton_r, char *buf,
  3036. __sanitizer_ether_addr *addr) {
  3037. void *ctx;
  3038. COMMON_INTERCEPTOR_ENTER(ctx, ether_aton_r, buf, addr);
  3039. if (buf) COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, REAL(strlen)(buf) + 1);
  3040. // FIXME: under ASan the call below may write to freed memory and corrupt
  3041. // its metadata. See
  3042. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3043. __sanitizer_ether_addr *res = REAL(ether_aton_r)(buf, addr);
  3044. if (res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(*res));
  3045. return res;
  3046. }
  3047. #define INIT_ETHER_R \
  3048. COMMON_INTERCEPT_FUNCTION(ether_ntoa_r); \
  3049. COMMON_INTERCEPT_FUNCTION(ether_aton_r);
  3050. #else
  3051. #define INIT_ETHER_R
  3052. #endif
  3053. #if SANITIZER_INTERCEPT_SHMCTL
  3054. INTERCEPTOR(int, shmctl, int shmid, int cmd, void *buf) {
  3055. void *ctx;
  3056. COMMON_INTERCEPTOR_ENTER(ctx, shmctl, shmid, cmd, buf);
  3057. // FIXME: under ASan the call below may write to freed memory and corrupt
  3058. // its metadata. See
  3059. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3060. int res = REAL(shmctl)(shmid, cmd, buf);
  3061. if (res >= 0) {
  3062. unsigned sz = 0;
  3063. if (cmd == shmctl_ipc_stat || cmd == shmctl_shm_stat)
  3064. sz = sizeof(__sanitizer_shmid_ds);
  3065. else if (cmd == shmctl_ipc_info)
  3066. sz = struct_shminfo_sz;
  3067. else if (cmd == shmctl_shm_info)
  3068. sz = struct_shm_info_sz;
  3069. if (sz) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sz);
  3070. }
  3071. return res;
  3072. }
  3073. #define INIT_SHMCTL COMMON_INTERCEPT_FUNCTION(shmctl);
  3074. #else
  3075. #define INIT_SHMCTL
  3076. #endif
  3077. #if SANITIZER_INTERCEPT_RANDOM_R
  3078. INTERCEPTOR(int, random_r, void *buf, u32 *result) {
  3079. void *ctx;
  3080. COMMON_INTERCEPTOR_ENTER(ctx, random_r, buf, result);
  3081. // FIXME: under ASan the call below may write to freed memory and corrupt
  3082. // its metadata. See
  3083. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3084. int res = REAL(random_r)(buf, result);
  3085. if (!res && result)
  3086. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  3087. return res;
  3088. }
  3089. #define INIT_RANDOM_R COMMON_INTERCEPT_FUNCTION(random_r);
  3090. #else
  3091. #define INIT_RANDOM_R
  3092. #endif
  3093. // FIXME: under ASan the REAL() call below may write to freed memory and corrupt
  3094. // its metadata. See
  3095. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3096. #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET || \
  3097. SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSSCHED || \
  3098. SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GET || \
  3099. SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GET || \
  3100. SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GET || \
  3101. SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GET
  3102. #define INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(fn, sz) \
  3103. INTERCEPTOR(int, fn, void *attr, void *r) { \
  3104. void *ctx; \
  3105. COMMON_INTERCEPTOR_ENTER(ctx, fn, attr, r); \
  3106. int res = REAL(fn)(attr, r); \
  3107. if (!res && r) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, r, sz); \
  3108. return res; \
  3109. }
  3110. #define INTERCEPTOR_PTHREAD_ATTR_GET(what, sz) \
  3111. INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_attr_get##what, sz)
  3112. #define INTERCEPTOR_PTHREAD_MUTEXATTR_GET(what, sz) \
  3113. INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_mutexattr_get##what, sz)
  3114. #define INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(what, sz) \
  3115. INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_rwlockattr_get##what, sz)
  3116. #define INTERCEPTOR_PTHREAD_CONDATTR_GET(what, sz) \
  3117. INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_condattr_get##what, sz)
  3118. #define INTERCEPTOR_PTHREAD_BARRIERATTR_GET(what, sz) \
  3119. INTERCEPTOR_PTHREAD_OBJECT_ATTR_GET(pthread_barrierattr_get##what, sz)
  3120. #endif
  3121. #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GET
  3122. INTERCEPTOR_PTHREAD_ATTR_GET(detachstate, sizeof(int))
  3123. INTERCEPTOR_PTHREAD_ATTR_GET(guardsize, sizeof(SIZE_T))
  3124. INTERCEPTOR_PTHREAD_ATTR_GET(schedparam, struct_sched_param_sz)
  3125. INTERCEPTOR_PTHREAD_ATTR_GET(schedpolicy, sizeof(int))
  3126. INTERCEPTOR_PTHREAD_ATTR_GET(scope, sizeof(int))
  3127. INTERCEPTOR_PTHREAD_ATTR_GET(stacksize, sizeof(SIZE_T))
  3128. INTERCEPTOR(int, pthread_attr_getstack, void *attr, void **addr, SIZE_T *size) {
  3129. void *ctx;
  3130. COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getstack, attr, addr, size);
  3131. // FIXME: under ASan the call below may write to freed memory and corrupt
  3132. // its metadata. See
  3133. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3134. int res = REAL(pthread_attr_getstack)(attr, addr, size);
  3135. if (!res) {
  3136. if (addr) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, addr, sizeof(*addr));
  3137. if (size) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, size, sizeof(*size));
  3138. }
  3139. return res;
  3140. }
  3141. // We may need to call the real pthread_attr_getstack from the run-time
  3142. // in sanitizer_common, but we don't want to include the interception headers
  3143. // there. So, just define this function here.
  3144. namespace __sanitizer {
  3145. extern "C" {
  3146. int real_pthread_attr_getstack(void *attr, void **addr, SIZE_T *size) {
  3147. return REAL(pthread_attr_getstack)(attr, addr, size);
  3148. }
  3149. } // extern "C"
  3150. } // namespace __sanitizer
  3151. #define INIT_PTHREAD_ATTR_GET \
  3152. COMMON_INTERCEPT_FUNCTION(pthread_attr_getdetachstate); \
  3153. COMMON_INTERCEPT_FUNCTION(pthread_attr_getguardsize); \
  3154. COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedparam); \
  3155. COMMON_INTERCEPT_FUNCTION(pthread_attr_getschedpolicy); \
  3156. COMMON_INTERCEPT_FUNCTION(pthread_attr_getscope); \
  3157. COMMON_INTERCEPT_FUNCTION(pthread_attr_getstacksize); \
  3158. COMMON_INTERCEPT_FUNCTION(pthread_attr_getstack);
  3159. #else
  3160. #define INIT_PTHREAD_ATTR_GET
  3161. #endif
  3162. #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETINHERITSCHED
  3163. INTERCEPTOR_PTHREAD_ATTR_GET(inheritsched, sizeof(int))
  3164. #define INIT_PTHREAD_ATTR_GETINHERITSCHED \
  3165. COMMON_INTERCEPT_FUNCTION(pthread_attr_getinheritsched);
  3166. #else
  3167. #define INIT_PTHREAD_ATTR_GETINHERITSCHED
  3168. #endif
  3169. #if SANITIZER_INTERCEPT_PTHREAD_ATTR_GETAFFINITY_NP
  3170. INTERCEPTOR(int, pthread_attr_getaffinity_np, void *attr, SIZE_T cpusetsize,
  3171. void *cpuset) {
  3172. void *ctx;
  3173. COMMON_INTERCEPTOR_ENTER(ctx, pthread_attr_getaffinity_np, attr, cpusetsize,
  3174. cpuset);
  3175. // FIXME: under ASan the call below may write to freed memory and corrupt
  3176. // its metadata. See
  3177. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3178. int res = REAL(pthread_attr_getaffinity_np)(attr, cpusetsize, cpuset);
  3179. if (!res && cpusetsize && cpuset)
  3180. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cpuset, cpusetsize);
  3181. return res;
  3182. }
  3183. #define INIT_PTHREAD_ATTR_GETAFFINITY_NP \
  3184. COMMON_INTERCEPT_FUNCTION(pthread_attr_getaffinity_np);
  3185. #else
  3186. #define INIT_PTHREAD_ATTR_GETAFFINITY_NP
  3187. #endif
  3188. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPSHARED
  3189. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(pshared, sizeof(int))
  3190. #define INIT_PTHREAD_MUTEXATTR_GETPSHARED \
  3191. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getpshared);
  3192. #else
  3193. #define INIT_PTHREAD_MUTEXATTR_GETPSHARED
  3194. #endif
  3195. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETTYPE
  3196. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(type, sizeof(int))
  3197. #define INIT_PTHREAD_MUTEXATTR_GETTYPE \
  3198. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_gettype);
  3199. #else
  3200. #define INIT_PTHREAD_MUTEXATTR_GETTYPE
  3201. #endif
  3202. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPROTOCOL
  3203. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(protocol, sizeof(int))
  3204. #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL \
  3205. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprotocol);
  3206. #else
  3207. #define INIT_PTHREAD_MUTEXATTR_GETPROTOCOL
  3208. #endif
  3209. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETPRIOCEILING
  3210. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(prioceiling, sizeof(int))
  3211. #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING \
  3212. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getprioceiling);
  3213. #else
  3214. #define INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING
  3215. #endif
  3216. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST
  3217. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust, sizeof(int))
  3218. #define INIT_PTHREAD_MUTEXATTR_GETROBUST \
  3219. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust);
  3220. #else
  3221. #define INIT_PTHREAD_MUTEXATTR_GETROBUST
  3222. #endif
  3223. #if SANITIZER_INTERCEPT_PTHREAD_MUTEXATTR_GETROBUST_NP
  3224. INTERCEPTOR_PTHREAD_MUTEXATTR_GET(robust_np, sizeof(int))
  3225. #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP \
  3226. COMMON_INTERCEPT_FUNCTION(pthread_mutexattr_getrobust_np);
  3227. #else
  3228. #define INIT_PTHREAD_MUTEXATTR_GETROBUST_NP
  3229. #endif
  3230. #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETPSHARED
  3231. INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(pshared, sizeof(int))
  3232. #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED \
  3233. COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getpshared);
  3234. #else
  3235. #define INIT_PTHREAD_RWLOCKATTR_GETPSHARED
  3236. #endif
  3237. #if SANITIZER_INTERCEPT_PTHREAD_RWLOCKATTR_GETKIND_NP
  3238. INTERCEPTOR_PTHREAD_RWLOCKATTR_GET(kind_np, sizeof(int))
  3239. #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP \
  3240. COMMON_INTERCEPT_FUNCTION(pthread_rwlockattr_getkind_np);
  3241. #else
  3242. #define INIT_PTHREAD_RWLOCKATTR_GETKIND_NP
  3243. #endif
  3244. #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETPSHARED
  3245. INTERCEPTOR_PTHREAD_CONDATTR_GET(pshared, sizeof(int))
  3246. #define INIT_PTHREAD_CONDATTR_GETPSHARED \
  3247. COMMON_INTERCEPT_FUNCTION(pthread_condattr_getpshared);
  3248. #else
  3249. #define INIT_PTHREAD_CONDATTR_GETPSHARED
  3250. #endif
  3251. #if SANITIZER_INTERCEPT_PTHREAD_CONDATTR_GETCLOCK
  3252. INTERCEPTOR_PTHREAD_CONDATTR_GET(clock, sizeof(int))
  3253. #define INIT_PTHREAD_CONDATTR_GETCLOCK \
  3254. COMMON_INTERCEPT_FUNCTION(pthread_condattr_getclock);
  3255. #else
  3256. #define INIT_PTHREAD_CONDATTR_GETCLOCK
  3257. #endif
  3258. #if SANITIZER_INTERCEPT_PTHREAD_BARRIERATTR_GETPSHARED
  3259. INTERCEPTOR_PTHREAD_BARRIERATTR_GET(pshared, sizeof(int)) // !mac !android
  3260. #define INIT_PTHREAD_BARRIERATTR_GETPSHARED \
  3261. COMMON_INTERCEPT_FUNCTION(pthread_barrierattr_getpshared);
  3262. #else
  3263. #define INIT_PTHREAD_BARRIERATTR_GETPSHARED
  3264. #endif
  3265. #if SANITIZER_INTERCEPT_TMPNAM
  3266. INTERCEPTOR(char *, tmpnam, char *s) {
  3267. void *ctx;
  3268. COMMON_INTERCEPTOR_ENTER(ctx, tmpnam, s);
  3269. char *res = REAL(tmpnam)(s);
  3270. if (res) {
  3271. if (s)
  3272. // FIXME: under ASan the call below may write to freed memory and corrupt
  3273. // its metadata. See
  3274. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3275. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
  3276. else
  3277. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
  3278. }
  3279. return res;
  3280. }
  3281. #define INIT_TMPNAM COMMON_INTERCEPT_FUNCTION(tmpnam);
  3282. #else
  3283. #define INIT_TMPNAM
  3284. #endif
  3285. #if SANITIZER_INTERCEPT_TMPNAM_R
  3286. INTERCEPTOR(char *, tmpnam_r, char *s) {
  3287. void *ctx;
  3288. COMMON_INTERCEPTOR_ENTER(ctx, tmpnam_r, s);
  3289. // FIXME: under ASan the call below may write to freed memory and corrupt
  3290. // its metadata. See
  3291. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3292. char *res = REAL(tmpnam_r)(s);
  3293. if (res && s) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, s, REAL(strlen)(s) + 1);
  3294. return res;
  3295. }
  3296. #define INIT_TMPNAM_R COMMON_INTERCEPT_FUNCTION(tmpnam_r);
  3297. #else
  3298. #define INIT_TMPNAM_R
  3299. #endif
  3300. #if SANITIZER_INTERCEPT_TEMPNAM
  3301. INTERCEPTOR(char *, tempnam, char *dir, char *pfx) {
  3302. void *ctx;
  3303. COMMON_INTERCEPTOR_ENTER(ctx, tempnam, dir, pfx);
  3304. if (dir) COMMON_INTERCEPTOR_READ_RANGE(ctx, dir, REAL(strlen)(dir) + 1);
  3305. if (pfx) COMMON_INTERCEPTOR_READ_RANGE(ctx, pfx, REAL(strlen)(pfx) + 1);
  3306. char *res = REAL(tempnam)(dir, pfx);
  3307. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res) + 1);
  3308. return res;
  3309. }
  3310. #define INIT_TEMPNAM COMMON_INTERCEPT_FUNCTION(tempnam);
  3311. #else
  3312. #define INIT_TEMPNAM
  3313. #endif
  3314. #if SANITIZER_INTERCEPT_PTHREAD_SETNAME_NP
  3315. INTERCEPTOR(int, pthread_setname_np, uptr thread, const char *name) {
  3316. void *ctx;
  3317. COMMON_INTERCEPTOR_ENTER(ctx, pthread_setname_np, thread, name);
  3318. COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name);
  3319. return REAL(pthread_setname_np)(thread, name);
  3320. }
  3321. #define INIT_PTHREAD_SETNAME_NP COMMON_INTERCEPT_FUNCTION(pthread_setname_np);
  3322. #else
  3323. #define INIT_PTHREAD_SETNAME_NP
  3324. #endif
  3325. #if SANITIZER_INTERCEPT_SINCOS
  3326. INTERCEPTOR(void, sincos, double x, double *sin, double *cos) {
  3327. void *ctx;
  3328. COMMON_INTERCEPTOR_ENTER(ctx, sincos, x, sin, cos);
  3329. // FIXME: under ASan the call below may write to freed memory and corrupt
  3330. // its metadata. See
  3331. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3332. REAL(sincos)(x, sin, cos);
  3333. if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
  3334. if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
  3335. }
  3336. INTERCEPTOR(void, sincosf, float x, float *sin, float *cos) {
  3337. void *ctx;
  3338. COMMON_INTERCEPTOR_ENTER(ctx, sincosf, x, sin, cos);
  3339. // FIXME: under ASan the call below may write to freed memory and corrupt
  3340. // its metadata. See
  3341. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3342. REAL(sincosf)(x, sin, cos);
  3343. if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
  3344. if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
  3345. }
  3346. INTERCEPTOR(void, sincosl, long double x, long double *sin, long double *cos) {
  3347. void *ctx;
  3348. COMMON_INTERCEPTOR_ENTER(ctx, sincosl, x, sin, cos);
  3349. // FIXME: under ASan the call below may write to freed memory and corrupt
  3350. // its metadata. See
  3351. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3352. REAL(sincosl)(x, sin, cos);
  3353. if (sin) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sin, sizeof(*sin));
  3354. if (cos) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cos, sizeof(*cos));
  3355. }
  3356. #define INIT_SINCOS \
  3357. COMMON_INTERCEPT_FUNCTION(sincos); \
  3358. COMMON_INTERCEPT_FUNCTION(sincosf); \
  3359. COMMON_INTERCEPT_FUNCTION(sincosl);
  3360. #else
  3361. #define INIT_SINCOS
  3362. #endif
  3363. #if SANITIZER_INTERCEPT_REMQUO
  3364. INTERCEPTOR(double, remquo, double x, double y, int *quo) {
  3365. void *ctx;
  3366. COMMON_INTERCEPTOR_ENTER(ctx, remquo, x, y, quo);
  3367. // FIXME: under ASan the call below may write to freed memory and corrupt
  3368. // its metadata. See
  3369. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3370. double res = REAL(remquo)(x, y, quo);
  3371. if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
  3372. return res;
  3373. }
  3374. INTERCEPTOR(float, remquof, float x, float y, int *quo) {
  3375. void *ctx;
  3376. COMMON_INTERCEPTOR_ENTER(ctx, remquof, x, y, quo);
  3377. // FIXME: under ASan the call below may write to freed memory and corrupt
  3378. // its metadata. See
  3379. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3380. float res = REAL(remquof)(x, y, quo);
  3381. if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
  3382. return res;
  3383. }
  3384. INTERCEPTOR(long double, remquol, long double x, long double y, int *quo) {
  3385. void *ctx;
  3386. COMMON_INTERCEPTOR_ENTER(ctx, remquol, x, y, quo);
  3387. // FIXME: under ASan the call below may write to freed memory and corrupt
  3388. // its metadata. See
  3389. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3390. long double res = REAL(remquol)(x, y, quo);
  3391. if (quo) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, quo, sizeof(*quo));
  3392. return res;
  3393. }
  3394. #define INIT_REMQUO \
  3395. COMMON_INTERCEPT_FUNCTION(remquo); \
  3396. COMMON_INTERCEPT_FUNCTION(remquof); \
  3397. COMMON_INTERCEPT_FUNCTION(remquol);
  3398. #else
  3399. #define INIT_REMQUO
  3400. #endif
  3401. #if SANITIZER_INTERCEPT_LGAMMA
  3402. extern int signgam;
  3403. INTERCEPTOR(double, lgamma, double x) {
  3404. void *ctx;
  3405. COMMON_INTERCEPTOR_ENTER(ctx, lgamma, x);
  3406. double res = REAL(lgamma)(x);
  3407. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
  3408. return res;
  3409. }
  3410. INTERCEPTOR(float, lgammaf, float x) {
  3411. void *ctx;
  3412. COMMON_INTERCEPTOR_ENTER(ctx, lgammaf, x);
  3413. float res = REAL(lgammaf)(x);
  3414. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
  3415. return res;
  3416. }
  3417. INTERCEPTOR(long double, lgammal, long double x) {
  3418. void *ctx;
  3419. COMMON_INTERCEPTOR_ENTER(ctx, lgammal, x);
  3420. long double res = REAL(lgammal)(x);
  3421. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, &signgam, sizeof(signgam));
  3422. return res;
  3423. }
  3424. #define INIT_LGAMMA \
  3425. COMMON_INTERCEPT_FUNCTION(lgamma); \
  3426. COMMON_INTERCEPT_FUNCTION(lgammaf); \
  3427. COMMON_INTERCEPT_FUNCTION(lgammal);
  3428. #else
  3429. #define INIT_LGAMMA
  3430. #endif
  3431. #if SANITIZER_INTERCEPT_LGAMMA_R
  3432. INTERCEPTOR(double, lgamma_r, double x, int *signp) {
  3433. void *ctx;
  3434. COMMON_INTERCEPTOR_ENTER(ctx, lgamma_r, x, signp);
  3435. // FIXME: under ASan the call below may write to freed memory and corrupt
  3436. // its metadata. See
  3437. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3438. double res = REAL(lgamma_r)(x, signp);
  3439. if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
  3440. return res;
  3441. }
  3442. INTERCEPTOR(float, lgammaf_r, float x, int *signp) {
  3443. void *ctx;
  3444. COMMON_INTERCEPTOR_ENTER(ctx, lgammaf_r, x, signp);
  3445. // FIXME: under ASan the call below may write to freed memory and corrupt
  3446. // its metadata. See
  3447. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3448. float res = REAL(lgammaf_r)(x, signp);
  3449. if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
  3450. return res;
  3451. }
  3452. #define INIT_LGAMMA_R \
  3453. COMMON_INTERCEPT_FUNCTION(lgamma_r); \
  3454. COMMON_INTERCEPT_FUNCTION(lgammaf_r);
  3455. #else
  3456. #define INIT_LGAMMA_R
  3457. #endif
  3458. #if SANITIZER_INTERCEPT_LGAMMAL_R
  3459. INTERCEPTOR(long double, lgammal_r, long double x, int *signp) {
  3460. void *ctx;
  3461. COMMON_INTERCEPTOR_ENTER(ctx, lgammal_r, x, signp);
  3462. // FIXME: under ASan the call below may write to freed memory and corrupt
  3463. // its metadata. See
  3464. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3465. long double res = REAL(lgammal_r)(x, signp);
  3466. if (signp) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, signp, sizeof(*signp));
  3467. return res;
  3468. }
  3469. #define INIT_LGAMMAL_R COMMON_INTERCEPT_FUNCTION(lgammal_r);
  3470. #else
  3471. #define INIT_LGAMMAL_R
  3472. #endif
  3473. #if SANITIZER_INTERCEPT_DRAND48_R
  3474. INTERCEPTOR(int, drand48_r, void *buffer, double *result) {
  3475. void *ctx;
  3476. COMMON_INTERCEPTOR_ENTER(ctx, drand48_r, buffer, result);
  3477. // FIXME: under ASan the call below may write to freed memory and corrupt
  3478. // its metadata. See
  3479. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3480. int res = REAL(drand48_r)(buffer, result);
  3481. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  3482. return res;
  3483. }
  3484. INTERCEPTOR(int, lrand48_r, void *buffer, long *result) {
  3485. void *ctx;
  3486. COMMON_INTERCEPTOR_ENTER(ctx, lrand48_r, buffer, result);
  3487. // FIXME: under ASan the call below may write to freed memory and corrupt
  3488. // its metadata. See
  3489. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3490. int res = REAL(lrand48_r)(buffer, result);
  3491. if (result) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(*result));
  3492. return res;
  3493. }
  3494. #define INIT_DRAND48_R \
  3495. COMMON_INTERCEPT_FUNCTION(drand48_r); \
  3496. COMMON_INTERCEPT_FUNCTION(lrand48_r);
  3497. #else
  3498. #define INIT_DRAND48_R
  3499. #endif
  3500. #if SANITIZER_INTERCEPT_RAND_R
  3501. INTERCEPTOR(int, rand_r, unsigned *seedp) {
  3502. void *ctx;
  3503. COMMON_INTERCEPTOR_ENTER(ctx, rand_r, seedp);
  3504. COMMON_INTERCEPTOR_READ_RANGE(ctx, seedp, sizeof(*seedp));
  3505. return REAL(rand_r)(seedp);
  3506. }
  3507. #define INIT_RAND_R COMMON_INTERCEPT_FUNCTION(rand_r);
  3508. #else
  3509. #define INIT_RAND_R
  3510. #endif
  3511. #if SANITIZER_INTERCEPT_GETLINE
  3512. INTERCEPTOR(SSIZE_T, getline, char **lineptr, SIZE_T *n, void *stream) {
  3513. void *ctx;
  3514. COMMON_INTERCEPTOR_ENTER(ctx, getline, lineptr, n, stream);
  3515. // FIXME: under ASan the call below may write to freed memory and corrupt
  3516. // its metadata. See
  3517. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3518. SSIZE_T res = REAL(getline)(lineptr, n, stream);
  3519. if (res > 0) {
  3520. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
  3521. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
  3522. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
  3523. }
  3524. return res;
  3525. }
  3526. INTERCEPTOR(SSIZE_T, __getdelim, char **lineptr, SIZE_T *n, int delim,
  3527. void *stream) {
  3528. void *ctx;
  3529. COMMON_INTERCEPTOR_ENTER(ctx, __getdelim, lineptr, n, delim, stream);
  3530. // FIXME: under ASan the call below may write to freed memory and corrupt
  3531. // its metadata. See
  3532. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3533. SSIZE_T res = REAL(__getdelim)(lineptr, n, delim, stream);
  3534. if (res > 0) {
  3535. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineptr, sizeof(*lineptr));
  3536. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
  3537. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *lineptr, res + 1);
  3538. }
  3539. return res;
  3540. }
  3541. INTERCEPTOR(SSIZE_T, getdelim, char **lineptr, SIZE_T *n, int delim,
  3542. void *stream) {
  3543. return __getdelim(lineptr, n, delim, stream);
  3544. }
  3545. #define INIT_GETLINE \
  3546. COMMON_INTERCEPT_FUNCTION(getline); \
  3547. COMMON_INTERCEPT_FUNCTION(__getdelim); \
  3548. COMMON_INTERCEPT_FUNCTION(getdelim);
  3549. #else
  3550. #define INIT_GETLINE
  3551. #endif
  3552. #if SANITIZER_INTERCEPT_ICONV
  3553. INTERCEPTOR(SIZE_T, iconv, void *cd, char **inbuf, SIZE_T *inbytesleft,
  3554. char **outbuf, SIZE_T *outbytesleft) {
  3555. void *ctx;
  3556. COMMON_INTERCEPTOR_ENTER(ctx, iconv, cd, inbuf, inbytesleft, outbuf,
  3557. outbytesleft);
  3558. if (inbytesleft)
  3559. COMMON_INTERCEPTOR_READ_RANGE(ctx, inbytesleft, sizeof(*inbytesleft));
  3560. if (inbuf && inbytesleft)
  3561. COMMON_INTERCEPTOR_READ_RANGE(ctx, *inbuf, *inbytesleft);
  3562. if (outbytesleft)
  3563. COMMON_INTERCEPTOR_READ_RANGE(ctx, outbytesleft, sizeof(*outbytesleft));
  3564. void *outbuf_orig = outbuf ? *outbuf : 0;
  3565. // FIXME: under ASan the call below may write to freed memory and corrupt
  3566. // its metadata. See
  3567. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3568. SIZE_T res = REAL(iconv)(cd, inbuf, inbytesleft, outbuf, outbytesleft);
  3569. if (res != (SIZE_T) - 1 && outbuf && *outbuf > outbuf_orig) {
  3570. SIZE_T sz = (char *)*outbuf - (char *)outbuf_orig;
  3571. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, outbuf_orig, sz);
  3572. }
  3573. return res;
  3574. }
  3575. #define INIT_ICONV COMMON_INTERCEPT_FUNCTION(iconv);
  3576. #else
  3577. #define INIT_ICONV
  3578. #endif
  3579. #if SANITIZER_INTERCEPT_TIMES
  3580. INTERCEPTOR(__sanitizer_clock_t, times, void *tms) {
  3581. void *ctx;
  3582. COMMON_INTERCEPTOR_ENTER(ctx, times, tms);
  3583. // FIXME: under ASan the call below may write to freed memory and corrupt
  3584. // its metadata. See
  3585. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3586. __sanitizer_clock_t res = REAL(times)(tms);
  3587. if (res != (__sanitizer_clock_t)-1 && tms)
  3588. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tms, struct_tms_sz);
  3589. return res;
  3590. }
  3591. #define INIT_TIMES COMMON_INTERCEPT_FUNCTION(times);
  3592. #else
  3593. #define INIT_TIMES
  3594. #endif
  3595. #if SANITIZER_INTERCEPT_TLS_GET_ADDR
  3596. #define INIT_TLS_GET_ADDR COMMON_INTERCEPT_FUNCTION(__tls_get_addr)
  3597. INTERCEPTOR(void *, __tls_get_addr, void *arg) {
  3598. void *ctx;
  3599. COMMON_INTERCEPTOR_ENTER(ctx, __tls_get_addr, arg);
  3600. void *res = REAL(__tls_get_addr)(arg);
  3601. DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res);
  3602. if (dtv) {
  3603. // New DTLS block has been allocated.
  3604. COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
  3605. }
  3606. return res;
  3607. }
  3608. #else
  3609. #define INIT_TLS_GET_ADDR
  3610. #endif
  3611. #if SANITIZER_INTERCEPT_LISTXATTR
  3612. INTERCEPTOR(SSIZE_T, listxattr, const char *path, char *list, SIZE_T size) {
  3613. void *ctx;
  3614. COMMON_INTERCEPTOR_ENTER(ctx, listxattr, path, list, size);
  3615. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  3616. // FIXME: under ASan the call below may write to freed memory and corrupt
  3617. // its metadata. See
  3618. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3619. SSIZE_T res = REAL(listxattr)(path, list, size);
  3620. // Here and below, size == 0 is a special case where nothing is written to the
  3621. // buffer, and res contains the desired buffer size.
  3622. if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
  3623. return res;
  3624. }
  3625. INTERCEPTOR(SSIZE_T, llistxattr, const char *path, char *list, SIZE_T size) {
  3626. void *ctx;
  3627. COMMON_INTERCEPTOR_ENTER(ctx, llistxattr, path, list, size);
  3628. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  3629. // FIXME: under ASan the call below may write to freed memory and corrupt
  3630. // its metadata. See
  3631. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3632. SSIZE_T res = REAL(llistxattr)(path, list, size);
  3633. if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
  3634. return res;
  3635. }
  3636. INTERCEPTOR(SSIZE_T, flistxattr, int fd, char *list, SIZE_T size) {
  3637. void *ctx;
  3638. COMMON_INTERCEPTOR_ENTER(ctx, flistxattr, fd, list, size);
  3639. // FIXME: under ASan the call below may write to freed memory and corrupt
  3640. // its metadata. See
  3641. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3642. SSIZE_T res = REAL(flistxattr)(fd, list, size);
  3643. if (size && res > 0 && list) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, list, res);
  3644. return res;
  3645. }
  3646. #define INIT_LISTXATTR \
  3647. COMMON_INTERCEPT_FUNCTION(listxattr); \
  3648. COMMON_INTERCEPT_FUNCTION(llistxattr); \
  3649. COMMON_INTERCEPT_FUNCTION(flistxattr);
  3650. #else
  3651. #define INIT_LISTXATTR
  3652. #endif
  3653. #if SANITIZER_INTERCEPT_GETXATTR
  3654. INTERCEPTOR(SSIZE_T, getxattr, const char *path, const char *name, char *value,
  3655. SIZE_T size) {
  3656. void *ctx;
  3657. COMMON_INTERCEPTOR_ENTER(ctx, getxattr, path, name, value, size);
  3658. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  3659. if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
  3660. // FIXME: under ASan the call below may write to freed memory and corrupt
  3661. // its metadata. See
  3662. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3663. SSIZE_T res = REAL(getxattr)(path, name, value, size);
  3664. if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
  3665. return res;
  3666. }
  3667. INTERCEPTOR(SSIZE_T, lgetxattr, const char *path, const char *name, char *value,
  3668. SIZE_T size) {
  3669. void *ctx;
  3670. COMMON_INTERCEPTOR_ENTER(ctx, lgetxattr, path, name, value, size);
  3671. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  3672. if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
  3673. // FIXME: under ASan the call below may write to freed memory and corrupt
  3674. // its metadata. See
  3675. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3676. SSIZE_T res = REAL(lgetxattr)(path, name, value, size);
  3677. if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
  3678. return res;
  3679. }
  3680. INTERCEPTOR(SSIZE_T, fgetxattr, int fd, const char *name, char *value,
  3681. SIZE_T size) {
  3682. void *ctx;
  3683. COMMON_INTERCEPTOR_ENTER(ctx, fgetxattr, fd, name, value, size);
  3684. if (name) COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
  3685. // FIXME: under ASan the call below may write to freed memory and corrupt
  3686. // its metadata. See
  3687. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3688. SSIZE_T res = REAL(fgetxattr)(fd, name, value, size);
  3689. if (size && res > 0 && value) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, res);
  3690. return res;
  3691. }
  3692. #define INIT_GETXATTR \
  3693. COMMON_INTERCEPT_FUNCTION(getxattr); \
  3694. COMMON_INTERCEPT_FUNCTION(lgetxattr); \
  3695. COMMON_INTERCEPT_FUNCTION(fgetxattr);
  3696. #else
  3697. #define INIT_GETXATTR
  3698. #endif
  3699. #if SANITIZER_INTERCEPT_GETRESID
  3700. INTERCEPTOR(int, getresuid, void *ruid, void *euid, void *suid) {
  3701. void *ctx;
  3702. COMMON_INTERCEPTOR_ENTER(ctx, getresuid, ruid, euid, suid);
  3703. // FIXME: under ASan the call below may write to freed memory and corrupt
  3704. // its metadata. See
  3705. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3706. int res = REAL(getresuid)(ruid, euid, suid);
  3707. if (res >= 0) {
  3708. if (ruid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ruid, uid_t_sz);
  3709. if (euid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, euid, uid_t_sz);
  3710. if (suid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, suid, uid_t_sz);
  3711. }
  3712. return res;
  3713. }
  3714. INTERCEPTOR(int, getresgid, void *rgid, void *egid, void *sgid) {
  3715. void *ctx;
  3716. COMMON_INTERCEPTOR_ENTER(ctx, getresgid, rgid, egid, sgid);
  3717. // FIXME: under ASan the call below may write to freed memory and corrupt
  3718. // its metadata. See
  3719. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3720. int res = REAL(getresgid)(rgid, egid, sgid);
  3721. if (res >= 0) {
  3722. if (rgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rgid, gid_t_sz);
  3723. if (egid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, egid, gid_t_sz);
  3724. if (sgid) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sgid, gid_t_sz);
  3725. }
  3726. return res;
  3727. }
  3728. #define INIT_GETRESID \
  3729. COMMON_INTERCEPT_FUNCTION(getresuid); \
  3730. COMMON_INTERCEPT_FUNCTION(getresgid);
  3731. #else
  3732. #define INIT_GETRESID
  3733. #endif
  3734. #if SANITIZER_INTERCEPT_GETIFADDRS
  3735. // As long as getifaddrs()/freeifaddrs() use calloc()/free(), we don't need to
  3736. // intercept freeifaddrs(). If that ceases to be the case, we might need to
  3737. // intercept it to poison the memory again.
  3738. INTERCEPTOR(int, getifaddrs, __sanitizer_ifaddrs **ifap) {
  3739. void *ctx;
  3740. COMMON_INTERCEPTOR_ENTER(ctx, getifaddrs, ifap);
  3741. // FIXME: under ASan the call below may write to freed memory and corrupt
  3742. // its metadata. See
  3743. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3744. int res = REAL(getifaddrs)(ifap);
  3745. if (res == 0 && ifap) {
  3746. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifap, sizeof(void *));
  3747. __sanitizer_ifaddrs *p = *ifap;
  3748. while (p) {
  3749. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(__sanitizer_ifaddrs));
  3750. if (p->ifa_name)
  3751. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_name,
  3752. REAL(strlen)(p->ifa_name) + 1);
  3753. if (p->ifa_addr)
  3754. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_addr, struct_sockaddr_sz);
  3755. if (p->ifa_netmask)
  3756. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_netmask, struct_sockaddr_sz);
  3757. // On Linux this is a union, but the other member also points to a
  3758. // struct sockaddr, so the following is sufficient.
  3759. if (p->ifa_dstaddr)
  3760. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->ifa_dstaddr, struct_sockaddr_sz);
  3761. // FIXME(smatveev): Unpoison p->ifa_data as well.
  3762. p = p->ifa_next;
  3763. }
  3764. }
  3765. return res;
  3766. }
  3767. #define INIT_GETIFADDRS \
  3768. COMMON_INTERCEPT_FUNCTION(getifaddrs);
  3769. #else
  3770. #define INIT_GETIFADDRS
  3771. #endif
  3772. #if SANITIZER_INTERCEPT_IF_INDEXTONAME
  3773. INTERCEPTOR(char *, if_indextoname, unsigned int ifindex, char* ifname) {
  3774. void *ctx;
  3775. COMMON_INTERCEPTOR_ENTER(ctx, if_indextoname, ifindex, ifname);
  3776. // FIXME: under ASan the call below may write to freed memory and corrupt
  3777. // its metadata. See
  3778. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3779. char *res = REAL(if_indextoname)(ifindex, ifname);
  3780. if (res && ifname)
  3781. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
  3782. return res;
  3783. }
  3784. INTERCEPTOR(unsigned int, if_nametoindex, const char* ifname) {
  3785. void *ctx;
  3786. COMMON_INTERCEPTOR_ENTER(ctx, if_nametoindex, ifname);
  3787. if (ifname)
  3788. COMMON_INTERCEPTOR_READ_RANGE(ctx, ifname, REAL(strlen)(ifname) + 1);
  3789. return REAL(if_nametoindex)(ifname);
  3790. }
  3791. #define INIT_IF_INDEXTONAME \
  3792. COMMON_INTERCEPT_FUNCTION(if_indextoname); \
  3793. COMMON_INTERCEPT_FUNCTION(if_nametoindex);
  3794. #else
  3795. #define INIT_IF_INDEXTONAME
  3796. #endif
  3797. #if SANITIZER_INTERCEPT_CAPGET
  3798. INTERCEPTOR(int, capget, void *hdrp, void *datap) {
  3799. void *ctx;
  3800. COMMON_INTERCEPTOR_ENTER(ctx, capget, hdrp, datap);
  3801. if (hdrp)
  3802. COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
  3803. // FIXME: under ASan the call below may write to freed memory and corrupt
  3804. // its metadata. See
  3805. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3806. int res = REAL(capget)(hdrp, datap);
  3807. if (res == 0 && datap)
  3808. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datap, __user_cap_data_struct_sz);
  3809. // We can also return -1 and write to hdrp->version if the version passed in
  3810. // hdrp->version is unsupported. But that's not a trivial condition to check,
  3811. // and anyway COMMON_INTERCEPTOR_READ_RANGE protects us to some extent.
  3812. return res;
  3813. }
  3814. INTERCEPTOR(int, capset, void *hdrp, const void *datap) {
  3815. void *ctx;
  3816. COMMON_INTERCEPTOR_ENTER(ctx, capset, hdrp, datap);
  3817. if (hdrp)
  3818. COMMON_INTERCEPTOR_READ_RANGE(ctx, hdrp, __user_cap_header_struct_sz);
  3819. if (datap)
  3820. COMMON_INTERCEPTOR_READ_RANGE(ctx, datap, __user_cap_data_struct_sz);
  3821. return REAL(capset)(hdrp, datap);
  3822. }
  3823. #define INIT_CAPGET \
  3824. COMMON_INTERCEPT_FUNCTION(capget); \
  3825. COMMON_INTERCEPT_FUNCTION(capset);
  3826. #else
  3827. #define INIT_CAPGET
  3828. #endif
  3829. #if SANITIZER_INTERCEPT_AEABI_MEM
  3830. DECLARE_REAL_AND_INTERCEPTOR(void *, memmove, void *, const void *, uptr);
  3831. DECLARE_REAL_AND_INTERCEPTOR(void *, memcpy, void *, const void *, uptr);
  3832. DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr);
  3833. INTERCEPTOR(void *, __aeabi_memmove, void *to, const void *from, uptr size) {
  3834. return WRAP(memmove)(to, from, size);
  3835. }
  3836. INTERCEPTOR(void *, __aeabi_memmove4, void *to, const void *from, uptr size) {
  3837. return WRAP(memmove)(to, from, size);
  3838. }
  3839. INTERCEPTOR(void *, __aeabi_memmove8, void *to, const void *from, uptr size) {
  3840. return WRAP(memmove)(to, from, size);
  3841. }
  3842. INTERCEPTOR(void *, __aeabi_memcpy, void *to, const void *from, uptr size) {
  3843. return WRAP(memcpy)(to, from, size);
  3844. }
  3845. INTERCEPTOR(void *, __aeabi_memcpy4, void *to, const void *from, uptr size) {
  3846. return WRAP(memcpy)(to, from, size);
  3847. }
  3848. INTERCEPTOR(void *, __aeabi_memcpy8, void *to, const void *from, uptr size) {
  3849. return WRAP(memcpy)(to, from, size);
  3850. }
  3851. // Note the argument order.
  3852. INTERCEPTOR(void *, __aeabi_memset, void *block, uptr size, int c) {
  3853. return WRAP(memset)(block, c, size);
  3854. }
  3855. INTERCEPTOR(void *, __aeabi_memset4, void *block, uptr size, int c) {
  3856. return WRAP(memset)(block, c, size);
  3857. }
  3858. INTERCEPTOR(void *, __aeabi_memset8, void *block, uptr size, int c) {
  3859. return WRAP(memset)(block, c, size);
  3860. }
  3861. INTERCEPTOR(void *, __aeabi_memclr, void *block, uptr size) {
  3862. return WRAP(memset)(block, 0, size);
  3863. }
  3864. INTERCEPTOR(void *, __aeabi_memclr4, void *block, uptr size) {
  3865. return WRAP(memset)(block, 0, size);
  3866. }
  3867. INTERCEPTOR(void *, __aeabi_memclr8, void *block, uptr size) {
  3868. return WRAP(memset)(block, 0, size);
  3869. }
  3870. #define INIT_AEABI_MEM \
  3871. COMMON_INTERCEPT_FUNCTION(__aeabi_memmove); \
  3872. COMMON_INTERCEPT_FUNCTION(__aeabi_memmove4); \
  3873. COMMON_INTERCEPT_FUNCTION(__aeabi_memmove8); \
  3874. COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy); \
  3875. COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy4); \
  3876. COMMON_INTERCEPT_FUNCTION(__aeabi_memcpy8); \
  3877. COMMON_INTERCEPT_FUNCTION(__aeabi_memset); \
  3878. COMMON_INTERCEPT_FUNCTION(__aeabi_memset4); \
  3879. COMMON_INTERCEPT_FUNCTION(__aeabi_memset8); \
  3880. COMMON_INTERCEPT_FUNCTION(__aeabi_memclr); \
  3881. COMMON_INTERCEPT_FUNCTION(__aeabi_memclr4); \
  3882. COMMON_INTERCEPT_FUNCTION(__aeabi_memclr8);
  3883. #else
  3884. #define INIT_AEABI_MEM
  3885. #endif // SANITIZER_INTERCEPT_AEABI_MEM
  3886. #if SANITIZER_INTERCEPT___BZERO
  3887. DECLARE_REAL_AND_INTERCEPTOR(void *, memset, void *, int, uptr);
  3888. INTERCEPTOR(void *, __bzero, void *block, uptr size) {
  3889. return WRAP(memset)(block, 0, size);
  3890. }
  3891. #define INIT___BZERO COMMON_INTERCEPT_FUNCTION(__bzero);
  3892. #else
  3893. #define INIT___BZERO
  3894. #endif // SANITIZER_INTERCEPT___BZERO
  3895. #if SANITIZER_INTERCEPT_FTIME
  3896. INTERCEPTOR(int, ftime, __sanitizer_timeb *tp) {
  3897. void *ctx;
  3898. COMMON_INTERCEPTOR_ENTER(ctx, ftime, tp);
  3899. // FIXME: under ASan the call below may write to freed memory and corrupt
  3900. // its metadata. See
  3901. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3902. int res = REAL(ftime)(tp);
  3903. if (tp)
  3904. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, tp, sizeof(*tp));
  3905. return res;
  3906. }
  3907. #define INIT_FTIME COMMON_INTERCEPT_FUNCTION(ftime);
  3908. #else
  3909. #define INIT_FTIME
  3910. #endif // SANITIZER_INTERCEPT_FTIME
  3911. #if SANITIZER_INTERCEPT_XDR
  3912. INTERCEPTOR(void, xdrmem_create, __sanitizer_XDR *xdrs, uptr addr,
  3913. unsigned size, int op) {
  3914. void *ctx;
  3915. COMMON_INTERCEPTOR_ENTER(ctx, xdrmem_create, xdrs, addr, size, op);
  3916. // FIXME: under ASan the call below may write to freed memory and corrupt
  3917. // its metadata. See
  3918. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3919. REAL(xdrmem_create)(xdrs, addr, size, op);
  3920. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
  3921. if (op == __sanitizer_XDR_ENCODE) {
  3922. // It's not obvious how much data individual xdr_ routines write.
  3923. // Simply unpoison the entire target buffer in advance.
  3924. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (void *)addr, size);
  3925. }
  3926. }
  3927. INTERCEPTOR(void, xdrstdio_create, __sanitizer_XDR *xdrs, void *file, int op) {
  3928. void *ctx;
  3929. COMMON_INTERCEPTOR_ENTER(ctx, xdrstdio_create, xdrs, file, op);
  3930. // FIXME: under ASan the call below may write to freed memory and corrupt
  3931. // its metadata. See
  3932. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3933. REAL(xdrstdio_create)(xdrs, file, op);
  3934. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, xdrs, sizeof(*xdrs));
  3935. }
  3936. // FIXME: under ASan the call below may write to freed memory and corrupt
  3937. // its metadata. See
  3938. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3939. #define XDR_INTERCEPTOR(F, T) \
  3940. INTERCEPTOR(int, F, __sanitizer_XDR *xdrs, T *p) { \
  3941. void *ctx; \
  3942. COMMON_INTERCEPTOR_ENTER(ctx, F, xdrs, p); \
  3943. if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) \
  3944. COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p)); \
  3945. int res = REAL(F)(xdrs, p); \
  3946. if (res && p && xdrs->x_op == __sanitizer_XDR_DECODE) \
  3947. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p)); \
  3948. return res; \
  3949. }
  3950. XDR_INTERCEPTOR(xdr_short, short)
  3951. XDR_INTERCEPTOR(xdr_u_short, unsigned short)
  3952. XDR_INTERCEPTOR(xdr_int, int)
  3953. XDR_INTERCEPTOR(xdr_u_int, unsigned)
  3954. XDR_INTERCEPTOR(xdr_long, long)
  3955. XDR_INTERCEPTOR(xdr_u_long, unsigned long)
  3956. XDR_INTERCEPTOR(xdr_hyper, long long)
  3957. XDR_INTERCEPTOR(xdr_u_hyper, unsigned long long)
  3958. XDR_INTERCEPTOR(xdr_longlong_t, long long)
  3959. XDR_INTERCEPTOR(xdr_u_longlong_t, unsigned long long)
  3960. XDR_INTERCEPTOR(xdr_int8_t, u8)
  3961. XDR_INTERCEPTOR(xdr_uint8_t, u8)
  3962. XDR_INTERCEPTOR(xdr_int16_t, u16)
  3963. XDR_INTERCEPTOR(xdr_uint16_t, u16)
  3964. XDR_INTERCEPTOR(xdr_int32_t, u32)
  3965. XDR_INTERCEPTOR(xdr_uint32_t, u32)
  3966. XDR_INTERCEPTOR(xdr_int64_t, u64)
  3967. XDR_INTERCEPTOR(xdr_uint64_t, u64)
  3968. XDR_INTERCEPTOR(xdr_quad_t, long long)
  3969. XDR_INTERCEPTOR(xdr_u_quad_t, unsigned long long)
  3970. XDR_INTERCEPTOR(xdr_bool, bool)
  3971. XDR_INTERCEPTOR(xdr_enum, int)
  3972. XDR_INTERCEPTOR(xdr_char, char)
  3973. XDR_INTERCEPTOR(xdr_u_char, unsigned char)
  3974. XDR_INTERCEPTOR(xdr_float, float)
  3975. XDR_INTERCEPTOR(xdr_double, double)
  3976. // FIXME: intercept xdr_array, opaque, union, vector, reference, pointer,
  3977. // wrapstring, sizeof
  3978. INTERCEPTOR(int, xdr_bytes, __sanitizer_XDR *xdrs, char **p, unsigned *sizep,
  3979. unsigned maxsize) {
  3980. void *ctx;
  3981. COMMON_INTERCEPTOR_ENTER(ctx, xdr_bytes, xdrs, p, sizep, maxsize);
  3982. if (p && sizep && xdrs->x_op == __sanitizer_XDR_ENCODE) {
  3983. COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
  3984. COMMON_INTERCEPTOR_READ_RANGE(ctx, sizep, sizeof(*sizep));
  3985. COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, *sizep);
  3986. }
  3987. // FIXME: under ASan the call below may write to freed memory and corrupt
  3988. // its metadata. See
  3989. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  3990. int res = REAL(xdr_bytes)(xdrs, p, sizep, maxsize);
  3991. if (p && sizep && xdrs->x_op == __sanitizer_XDR_DECODE) {
  3992. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
  3993. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizep, sizeof(*sizep));
  3994. if (res && *p && *sizep) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, *sizep);
  3995. }
  3996. return res;
  3997. }
  3998. INTERCEPTOR(int, xdr_string, __sanitizer_XDR *xdrs, char **p,
  3999. unsigned maxsize) {
  4000. void *ctx;
  4001. COMMON_INTERCEPTOR_ENTER(ctx, xdr_string, xdrs, p, maxsize);
  4002. if (p && xdrs->x_op == __sanitizer_XDR_ENCODE) {
  4003. COMMON_INTERCEPTOR_READ_RANGE(ctx, p, sizeof(*p));
  4004. COMMON_INTERCEPTOR_READ_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
  4005. }
  4006. // FIXME: under ASan the call below may write to freed memory and corrupt
  4007. // its metadata. See
  4008. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  4009. int res = REAL(xdr_string)(xdrs, p, maxsize);
  4010. if (p && xdrs->x_op == __sanitizer_XDR_DECODE) {
  4011. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
  4012. if (res && *p)
  4013. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *p, REAL(strlen)(*p) + 1);
  4014. }
  4015. return res;
  4016. }
  4017. #define INIT_XDR \
  4018. COMMON_INTERCEPT_FUNCTION(xdrmem_create); \
  4019. COMMON_INTERCEPT_FUNCTION(xdrstdio_create); \
  4020. COMMON_INTERCEPT_FUNCTION(xdr_short); \
  4021. COMMON_INTERCEPT_FUNCTION(xdr_u_short); \
  4022. COMMON_INTERCEPT_FUNCTION(xdr_int); \
  4023. COMMON_INTERCEPT_FUNCTION(xdr_u_int); \
  4024. COMMON_INTERCEPT_FUNCTION(xdr_long); \
  4025. COMMON_INTERCEPT_FUNCTION(xdr_u_long); \
  4026. COMMON_INTERCEPT_FUNCTION(xdr_hyper); \
  4027. COMMON_INTERCEPT_FUNCTION(xdr_u_hyper); \
  4028. COMMON_INTERCEPT_FUNCTION(xdr_longlong_t); \
  4029. COMMON_INTERCEPT_FUNCTION(xdr_u_longlong_t); \
  4030. COMMON_INTERCEPT_FUNCTION(xdr_int8_t); \
  4031. COMMON_INTERCEPT_FUNCTION(xdr_uint8_t); \
  4032. COMMON_INTERCEPT_FUNCTION(xdr_int16_t); \
  4033. COMMON_INTERCEPT_FUNCTION(xdr_uint16_t); \
  4034. COMMON_INTERCEPT_FUNCTION(xdr_int32_t); \
  4035. COMMON_INTERCEPT_FUNCTION(xdr_uint32_t); \
  4036. COMMON_INTERCEPT_FUNCTION(xdr_int64_t); \
  4037. COMMON_INTERCEPT_FUNCTION(xdr_uint64_t); \
  4038. COMMON_INTERCEPT_FUNCTION(xdr_quad_t); \
  4039. COMMON_INTERCEPT_FUNCTION(xdr_u_quad_t); \
  4040. COMMON_INTERCEPT_FUNCTION(xdr_bool); \
  4041. COMMON_INTERCEPT_FUNCTION(xdr_enum); \
  4042. COMMON_INTERCEPT_FUNCTION(xdr_char); \
  4043. COMMON_INTERCEPT_FUNCTION(xdr_u_char); \
  4044. COMMON_INTERCEPT_FUNCTION(xdr_float); \
  4045. COMMON_INTERCEPT_FUNCTION(xdr_double); \
  4046. COMMON_INTERCEPT_FUNCTION(xdr_bytes); \
  4047. COMMON_INTERCEPT_FUNCTION(xdr_string);
  4048. #else
  4049. #define INIT_XDR
  4050. #endif // SANITIZER_INTERCEPT_XDR
  4051. #if SANITIZER_INTERCEPT_TSEARCH
  4052. INTERCEPTOR(void *, tsearch, void *key, void **rootp,
  4053. int (*compar)(const void *, const void *)) {
  4054. void *ctx;
  4055. COMMON_INTERCEPTOR_ENTER(ctx, tsearch, key, rootp, compar);
  4056. // FIXME: under ASan the call below may write to freed memory and corrupt
  4057. // its metadata. See
  4058. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  4059. void *res = REAL(tsearch)(key, rootp, compar);
  4060. if (res && *(void **)res == key)
  4061. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, sizeof(void *));
  4062. return res;
  4063. }
  4064. #define INIT_TSEARCH COMMON_INTERCEPT_FUNCTION(tsearch);
  4065. #else
  4066. #define INIT_TSEARCH
  4067. #endif
  4068. #if SANITIZER_INTERCEPT_LIBIO_INTERNALS || SANITIZER_INTERCEPT_FOPEN || \
  4069. SANITIZER_INTERCEPT_OPEN_MEMSTREAM
  4070. void unpoison_file(__sanitizer_FILE *fp) {
  4071. #if SANITIZER_HAS_STRUCT_FILE
  4072. COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp, sizeof(*fp));
  4073. if (fp->_IO_read_base && fp->_IO_read_base < fp->_IO_read_end)
  4074. COMMON_INTERCEPTOR_INITIALIZE_RANGE(fp->_IO_read_base,
  4075. fp->_IO_read_end - fp->_IO_read_base);
  4076. #endif // SANITIZER_HAS_STRUCT_FILE
  4077. }
  4078. #endif
  4079. #if SANITIZER_INTERCEPT_LIBIO_INTERNALS
  4080. // These guys are called when a .c source is built with -O2.
  4081. INTERCEPTOR(int, __uflow, __sanitizer_FILE *fp) {
  4082. void *ctx;
  4083. COMMON_INTERCEPTOR_ENTER(ctx, __uflow, fp);
  4084. int res = REAL(__uflow)(fp);
  4085. unpoison_file(fp);
  4086. return res;
  4087. }
  4088. INTERCEPTOR(int, __underflow, __sanitizer_FILE *fp) {
  4089. void *ctx;
  4090. COMMON_INTERCEPTOR_ENTER(ctx, __underflow, fp);
  4091. int res = REAL(__underflow)(fp);
  4092. unpoison_file(fp);
  4093. return res;
  4094. }
  4095. INTERCEPTOR(int, __overflow, __sanitizer_FILE *fp, int ch) {
  4096. void *ctx;
  4097. COMMON_INTERCEPTOR_ENTER(ctx, __overflow, fp, ch);
  4098. int res = REAL(__overflow)(fp, ch);
  4099. unpoison_file(fp);
  4100. return res;
  4101. }
  4102. INTERCEPTOR(int, __wuflow, __sanitizer_FILE *fp) {
  4103. void *ctx;
  4104. COMMON_INTERCEPTOR_ENTER(ctx, __wuflow, fp);
  4105. int res = REAL(__wuflow)(fp);
  4106. unpoison_file(fp);
  4107. return res;
  4108. }
  4109. INTERCEPTOR(int, __wunderflow, __sanitizer_FILE *fp) {
  4110. void *ctx;
  4111. COMMON_INTERCEPTOR_ENTER(ctx, __wunderflow, fp);
  4112. int res = REAL(__wunderflow)(fp);
  4113. unpoison_file(fp);
  4114. return res;
  4115. }
  4116. INTERCEPTOR(int, __woverflow, __sanitizer_FILE *fp, int ch) {
  4117. void *ctx;
  4118. COMMON_INTERCEPTOR_ENTER(ctx, __woverflow, fp, ch);
  4119. int res = REAL(__woverflow)(fp, ch);
  4120. unpoison_file(fp);
  4121. return res;
  4122. }
  4123. #define INIT_LIBIO_INTERNALS \
  4124. COMMON_INTERCEPT_FUNCTION(__uflow); \
  4125. COMMON_INTERCEPT_FUNCTION(__underflow); \
  4126. COMMON_INTERCEPT_FUNCTION(__overflow); \
  4127. COMMON_INTERCEPT_FUNCTION(__wuflow); \
  4128. COMMON_INTERCEPT_FUNCTION(__wunderflow); \
  4129. COMMON_INTERCEPT_FUNCTION(__woverflow);
  4130. #else
  4131. #define INIT_LIBIO_INTERNALS
  4132. #endif
  4133. #if SANITIZER_INTERCEPT_FOPEN
  4134. INTERCEPTOR(__sanitizer_FILE *, fopen, const char *path, const char *mode) {
  4135. void *ctx;
  4136. COMMON_INTERCEPTOR_ENTER(ctx, fopen, path, mode);
  4137. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  4138. COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
  4139. __sanitizer_FILE *res = REAL(fopen)(path, mode);
  4140. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
  4141. if (res) unpoison_file(res);
  4142. return res;
  4143. }
  4144. INTERCEPTOR(__sanitizer_FILE *, fdopen, int fd, const char *mode) {
  4145. void *ctx;
  4146. COMMON_INTERCEPTOR_ENTER(ctx, fdopen, fd, mode);
  4147. COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
  4148. __sanitizer_FILE *res = REAL(fdopen)(fd, mode);
  4149. if (res) unpoison_file(res);
  4150. return res;
  4151. }
  4152. INTERCEPTOR(__sanitizer_FILE *, freopen, const char *path, const char *mode,
  4153. __sanitizer_FILE *fp) {
  4154. void *ctx;
  4155. COMMON_INTERCEPTOR_ENTER(ctx, freopen, path, mode, fp);
  4156. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  4157. COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
  4158. COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
  4159. __sanitizer_FILE *res = REAL(freopen)(path, mode, fp);
  4160. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
  4161. if (res) unpoison_file(res);
  4162. return res;
  4163. }
  4164. #define INIT_FOPEN \
  4165. COMMON_INTERCEPT_FUNCTION(fopen); \
  4166. COMMON_INTERCEPT_FUNCTION(fdopen); \
  4167. COMMON_INTERCEPT_FUNCTION(freopen);
  4168. #else
  4169. #define INIT_FOPEN
  4170. #endif
  4171. #if SANITIZER_INTERCEPT_FOPEN64
  4172. INTERCEPTOR(__sanitizer_FILE *, fopen64, const char *path, const char *mode) {
  4173. void *ctx;
  4174. COMMON_INTERCEPTOR_ENTER(ctx, fopen64, path, mode);
  4175. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  4176. COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
  4177. __sanitizer_FILE *res = REAL(fopen64)(path, mode);
  4178. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
  4179. if (res) unpoison_file(res);
  4180. return res;
  4181. }
  4182. INTERCEPTOR(__sanitizer_FILE *, freopen64, const char *path, const char *mode,
  4183. __sanitizer_FILE *fp) {
  4184. void *ctx;
  4185. COMMON_INTERCEPTOR_ENTER(ctx, freopen64, path, mode, fp);
  4186. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
  4187. COMMON_INTERCEPTOR_READ_RANGE(ctx, mode, REAL(strlen)(mode) + 1);
  4188. COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
  4189. __sanitizer_FILE *res = REAL(freopen64)(path, mode, fp);
  4190. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, path);
  4191. if (res) unpoison_file(res);
  4192. return res;
  4193. }
  4194. #define INIT_FOPEN64 \
  4195. COMMON_INTERCEPT_FUNCTION(fopen64); \
  4196. COMMON_INTERCEPT_FUNCTION(freopen64);
  4197. #else
  4198. #define INIT_FOPEN64
  4199. #endif
  4200. #if SANITIZER_INTERCEPT_OPEN_MEMSTREAM
  4201. INTERCEPTOR(__sanitizer_FILE *, open_memstream, char **ptr, SIZE_T *sizeloc) {
  4202. void *ctx;
  4203. COMMON_INTERCEPTOR_ENTER(ctx, open_memstream, ptr, sizeloc);
  4204. // FIXME: under ASan the call below may write to freed memory and corrupt
  4205. // its metadata. See
  4206. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  4207. __sanitizer_FILE *res = REAL(open_memstream)(ptr, sizeloc);
  4208. if (res) {
  4209. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
  4210. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
  4211. unpoison_file(res);
  4212. FileMetadata file = {ptr, sizeloc};
  4213. SetInterceptorMetadata(res, file);
  4214. }
  4215. return res;
  4216. }
  4217. INTERCEPTOR(__sanitizer_FILE *, open_wmemstream, wchar_t **ptr,
  4218. SIZE_T *sizeloc) {
  4219. void *ctx;
  4220. COMMON_INTERCEPTOR_ENTER(ctx, open_wmemstream, ptr, sizeloc);
  4221. __sanitizer_FILE *res = REAL(open_wmemstream)(ptr, sizeloc);
  4222. if (res) {
  4223. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, sizeof(*ptr));
  4224. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sizeloc, sizeof(*sizeloc));
  4225. unpoison_file(res);
  4226. FileMetadata file = {(char **)ptr, sizeloc};
  4227. SetInterceptorMetadata(res, file);
  4228. }
  4229. return res;
  4230. }
  4231. INTERCEPTOR(__sanitizer_FILE *, fmemopen, void *buf, SIZE_T size,
  4232. const char *mode) {
  4233. void *ctx;
  4234. COMMON_INTERCEPTOR_ENTER(ctx, fmemopen, buf, size, mode);
  4235. // FIXME: under ASan the call below may write to freed memory and corrupt
  4236. // its metadata. See
  4237. // https://code.google.com/p/address-sanitizer/issues/detail?id=321.
  4238. __sanitizer_FILE *res = REAL(fmemopen)(buf, size, mode);
  4239. if (res) unpoison_file(res);
  4240. return res;
  4241. }
  4242. #define INIT_OPEN_MEMSTREAM \
  4243. COMMON_INTERCEPT_FUNCTION(open_memstream); \
  4244. COMMON_INTERCEPT_FUNCTION(open_wmemstream); \
  4245. COMMON_INTERCEPT_FUNCTION(fmemopen);
  4246. #else
  4247. #define INIT_OPEN_MEMSTREAM
  4248. #endif
  4249. #if SANITIZER_INTERCEPT_OBSTACK
  4250. static void initialize_obstack(__sanitizer_obstack *obstack) {
  4251. COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack, sizeof(*obstack));
  4252. if (obstack->chunk)
  4253. COMMON_INTERCEPTOR_INITIALIZE_RANGE(obstack->chunk,
  4254. sizeof(*obstack->chunk));
  4255. }
  4256. INTERCEPTOR(int, _obstack_begin_1, __sanitizer_obstack *obstack, int sz,
  4257. int align, void *(*alloc_fn)(uptr arg, uptr sz),
  4258. void (*free_fn)(uptr arg, void *p)) {
  4259. void *ctx;
  4260. COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin_1, obstack, sz, align, alloc_fn,
  4261. free_fn);
  4262. int res = REAL(_obstack_begin_1)(obstack, sz, align, alloc_fn, free_fn);
  4263. if (res) initialize_obstack(obstack);
  4264. return res;
  4265. }
  4266. INTERCEPTOR(int, _obstack_begin, __sanitizer_obstack *obstack, int sz,
  4267. int align, void *(*alloc_fn)(uptr sz), void (*free_fn)(void *p)) {
  4268. void *ctx;
  4269. COMMON_INTERCEPTOR_ENTER(ctx, _obstack_begin, obstack, sz, align, alloc_fn,
  4270. free_fn);
  4271. int res = REAL(_obstack_begin)(obstack, sz, align, alloc_fn, free_fn);
  4272. if (res) initialize_obstack(obstack);
  4273. return res;
  4274. }
  4275. INTERCEPTOR(void, _obstack_newchunk, __sanitizer_obstack *obstack, int length) {
  4276. void *ctx;
  4277. COMMON_INTERCEPTOR_ENTER(ctx, _obstack_newchunk, obstack, length);
  4278. REAL(_obstack_newchunk)(obstack, length);
  4279. if (obstack->chunk)
  4280. COMMON_INTERCEPTOR_INITIALIZE_RANGE(
  4281. obstack->chunk, obstack->next_free - (char *)obstack->chunk);
  4282. }
  4283. #define INIT_OBSTACK \
  4284. COMMON_INTERCEPT_FUNCTION(_obstack_begin_1); \
  4285. COMMON_INTERCEPT_FUNCTION(_obstack_begin); \
  4286. COMMON_INTERCEPT_FUNCTION(_obstack_newchunk);
  4287. #else
  4288. #define INIT_OBSTACK
  4289. #endif
  4290. #if SANITIZER_INTERCEPT_FFLUSH
  4291. INTERCEPTOR(int, fflush, __sanitizer_FILE *fp) {
  4292. void *ctx;
  4293. COMMON_INTERCEPTOR_ENTER(ctx, fflush, fp);
  4294. int res = REAL(fflush)(fp);
  4295. // FIXME: handle fp == NULL
  4296. if (fp) {
  4297. const FileMetadata *m = GetInterceptorMetadata(fp);
  4298. if (m) COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
  4299. }
  4300. return res;
  4301. }
  4302. #define INIT_FFLUSH COMMON_INTERCEPT_FUNCTION(fflush);
  4303. #else
  4304. #define INIT_FFLUSH
  4305. #endif
  4306. #if SANITIZER_INTERCEPT_FCLOSE
  4307. INTERCEPTOR(int, fclose, __sanitizer_FILE *fp) {
  4308. void *ctx;
  4309. COMMON_INTERCEPTOR_ENTER(ctx, fclose, fp);
  4310. if (fp) {
  4311. COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
  4312. const FileMetadata *m = GetInterceptorMetadata(fp);
  4313. if (m) {
  4314. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
  4315. DeleteInterceptorMetadata(fp);
  4316. }
  4317. }
  4318. return REAL(fclose)(fp);
  4319. }
  4320. #define INIT_FCLOSE COMMON_INTERCEPT_FUNCTION(fclose);
  4321. #else
  4322. #define INIT_FCLOSE
  4323. #endif
  4324. #if SANITIZER_INTERCEPT_DLOPEN_DLCLOSE
  4325. INTERCEPTOR(void*, dlopen, const char *filename, int flag) {
  4326. void *ctx;
  4327. COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlopen, filename, flag);
  4328. void *res = REAL(dlopen)(filename, flag);
  4329. COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
  4330. return res;
  4331. }
  4332. INTERCEPTOR(int, dlclose, void *handle) {
  4333. void *ctx;
  4334. COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
  4335. int res = REAL(dlclose)(handle);
  4336. COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
  4337. return res;
  4338. }
  4339. #define INIT_DLOPEN_DLCLOSE \
  4340. COMMON_INTERCEPT_FUNCTION(dlopen); \
  4341. COMMON_INTERCEPT_FUNCTION(dlclose);
  4342. #else
  4343. #define INIT_DLOPEN_DLCLOSE
  4344. #endif
  4345. #if SANITIZER_INTERCEPT_GETPASS
  4346. INTERCEPTOR(char *, getpass, const char *prompt) {
  4347. void *ctx;
  4348. COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
  4349. if (prompt)
  4350. COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, REAL(strlen)(prompt)+1);
  4351. char *res = REAL(getpass)(prompt);
  4352. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, REAL(strlen)(res)+1);
  4353. return res;
  4354. }
  4355. #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
  4356. #else
  4357. #define INIT_GETPASS
  4358. #endif
  4359. #if SANITIZER_INTERCEPT_TIMERFD
  4360. INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
  4361. void *old_value) {
  4362. void *ctx;
  4363. COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
  4364. old_value);
  4365. COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
  4366. int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
  4367. if (res != -1 && old_value)
  4368. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
  4369. return res;
  4370. }
  4371. INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
  4372. void *ctx;
  4373. COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
  4374. int res = REAL(timerfd_gettime)(fd, curr_value);
  4375. if (res != -1 && curr_value)
  4376. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
  4377. return res;
  4378. }
  4379. #define INIT_TIMERFD \
  4380. COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
  4381. COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
  4382. #else
  4383. #define INIT_TIMERFD
  4384. #endif
  4385. #if SANITIZER_INTERCEPT_MLOCKX
  4386. // Linux kernel has a bug that leads to kernel deadlock if a process
  4387. // maps TBs of memory and then calls mlock().
  4388. static void MlockIsUnsupported() {
  4389. static atomic_uint8_t printed;
  4390. if (atomic_exchange(&printed, 1, memory_order_relaxed))
  4391. return;
  4392. VPrintf(1, "INFO: %s ignores mlock/mlockall/munlock/munlockall\n",
  4393. SanitizerToolName);
  4394. }
  4395. INTERCEPTOR(int, mlock, const void *addr, uptr len) {
  4396. MlockIsUnsupported();
  4397. return 0;
  4398. }
  4399. INTERCEPTOR(int, munlock, const void *addr, uptr len) {
  4400. MlockIsUnsupported();
  4401. return 0;
  4402. }
  4403. INTERCEPTOR(int, mlockall, int flags) {
  4404. MlockIsUnsupported();
  4405. return 0;
  4406. }
  4407. INTERCEPTOR(int, munlockall, void) {
  4408. MlockIsUnsupported();
  4409. return 0;
  4410. }
  4411. #define INIT_MLOCKX \
  4412. COMMON_INTERCEPT_FUNCTION(mlock); \
  4413. COMMON_INTERCEPT_FUNCTION(munlock); \
  4414. COMMON_INTERCEPT_FUNCTION(mlockall); \
  4415. COMMON_INTERCEPT_FUNCTION(munlockall);
  4416. #else
  4417. #define INIT_MLOCKX
  4418. #endif // SANITIZER_INTERCEPT_MLOCKX
  4419. static void InitializeCommonInterceptors() {
  4420. static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
  4421. interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
  4422. INIT_TEXTDOMAIN;
  4423. INIT_STRCMP;
  4424. INIT_STRNCMP;
  4425. INIT_STRCASECMP;
  4426. INIT_STRNCASECMP;
  4427. INIT_MEMCHR;
  4428. INIT_MEMRCHR;
  4429. INIT_READ;
  4430. INIT_PREAD;
  4431. INIT_PREAD64;
  4432. INIT_READV;
  4433. INIT_PREADV;
  4434. INIT_PREADV64;
  4435. INIT_WRITE;
  4436. INIT_PWRITE;
  4437. INIT_PWRITE64;
  4438. INIT_WRITEV;
  4439. INIT_PWRITEV;
  4440. INIT_PWRITEV64;
  4441. INIT_PRCTL;
  4442. INIT_LOCALTIME_AND_FRIENDS;
  4443. INIT_STRPTIME;
  4444. INIT_SCANF;
  4445. INIT_ISOC99_SCANF;
  4446. INIT_PRINTF;
  4447. INIT_ISOC99_PRINTF;
  4448. INIT_FREXP;
  4449. INIT_FREXPF_FREXPL;
  4450. INIT_GETPWNAM_AND_FRIENDS;
  4451. INIT_GETPWNAM_R_AND_FRIENDS;
  4452. INIT_GETPWENT;
  4453. INIT_FGETPWENT;
  4454. INIT_GETPWENT_R;
  4455. INIT_SETPWENT;
  4456. INIT_CLOCK_GETTIME;
  4457. INIT_GETITIMER;
  4458. INIT_TIME;
  4459. INIT_GLOB;
  4460. INIT_WAIT;
  4461. INIT_WAIT4;
  4462. INIT_INET;
  4463. INIT_PTHREAD_GETSCHEDPARAM;
  4464. INIT_GETADDRINFO;
  4465. INIT_GETNAMEINFO;
  4466. INIT_GETSOCKNAME;
  4467. INIT_GETHOSTBYNAME;
  4468. INIT_GETHOSTBYNAME_R;
  4469. INIT_GETHOSTBYNAME2_R;
  4470. INIT_GETHOSTBYADDR_R;
  4471. INIT_GETHOSTENT_R;
  4472. INIT_GETSOCKOPT;
  4473. INIT_ACCEPT;
  4474. INIT_ACCEPT4;
  4475. INIT_MODF;
  4476. INIT_RECVMSG;
  4477. INIT_GETPEERNAME;
  4478. INIT_IOCTL;
  4479. INIT_INET_ATON;
  4480. INIT_SYSINFO;
  4481. INIT_READDIR;
  4482. INIT_READDIR64;
  4483. INIT_PTRACE;
  4484. INIT_SETLOCALE;
  4485. INIT_GETCWD;
  4486. INIT_GET_CURRENT_DIR_NAME;
  4487. INIT_STRTOIMAX;
  4488. INIT_MBSTOWCS;
  4489. INIT_MBSNRTOWCS;
  4490. INIT_WCSTOMBS;
  4491. INIT_WCSNRTOMBS;
  4492. INIT_TCGETATTR;
  4493. INIT_REALPATH;
  4494. INIT_CANONICALIZE_FILE_NAME;
  4495. INIT_CONFSTR;
  4496. INIT_SCHED_GETAFFINITY;
  4497. INIT_STRERROR;
  4498. INIT_STRERROR_R;
  4499. INIT_XPG_STRERROR_R;
  4500. INIT_SCANDIR;
  4501. INIT_SCANDIR64;
  4502. INIT_GETGROUPS;
  4503. INIT_POLL;
  4504. INIT_PPOLL;
  4505. INIT_WORDEXP;
  4506. INIT_SIGWAIT;
  4507. INIT_SIGWAITINFO;
  4508. INIT_SIGTIMEDWAIT;
  4509. INIT_SIGSETOPS;
  4510. INIT_SIGPENDING;
  4511. INIT_SIGPROCMASK;
  4512. INIT_BACKTRACE;
  4513. INIT__EXIT;
  4514. INIT_PTHREAD_MUTEX_LOCK;
  4515. INIT_PTHREAD_MUTEX_UNLOCK;
  4516. INIT_GETMNTENT;
  4517. INIT_GETMNTENT_R;
  4518. INIT_STATFS;
  4519. INIT_STATFS64;
  4520. INIT_STATVFS;
  4521. INIT_STATVFS64;
  4522. INIT_INITGROUPS;
  4523. INIT_ETHER_NTOA_ATON;
  4524. INIT_ETHER_HOST;
  4525. INIT_ETHER_R;
  4526. INIT_SHMCTL;
  4527. INIT_RANDOM_R;
  4528. INIT_PTHREAD_ATTR_GET;
  4529. INIT_PTHREAD_ATTR_GETINHERITSCHED;
  4530. INIT_PTHREAD_ATTR_GETAFFINITY_NP;
  4531. INIT_PTHREAD_MUTEXATTR_GETPSHARED;
  4532. INIT_PTHREAD_MUTEXATTR_GETTYPE;
  4533. INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
  4534. INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
  4535. INIT_PTHREAD_MUTEXATTR_GETROBUST;
  4536. INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
  4537. INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
  4538. INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
  4539. INIT_PTHREAD_CONDATTR_GETPSHARED;
  4540. INIT_PTHREAD_CONDATTR_GETCLOCK;
  4541. INIT_PTHREAD_BARRIERATTR_GETPSHARED;
  4542. INIT_TMPNAM;
  4543. INIT_TMPNAM_R;
  4544. INIT_TEMPNAM;
  4545. INIT_PTHREAD_SETNAME_NP;
  4546. INIT_SINCOS;
  4547. INIT_REMQUO;
  4548. INIT_LGAMMA;
  4549. INIT_LGAMMA_R;
  4550. INIT_LGAMMAL_R;
  4551. INIT_DRAND48_R;
  4552. INIT_RAND_R;
  4553. INIT_GETLINE;
  4554. INIT_ICONV;
  4555. INIT_TIMES;
  4556. INIT_TLS_GET_ADDR;
  4557. INIT_LISTXATTR;
  4558. INIT_GETXATTR;
  4559. INIT_GETRESID;
  4560. INIT_GETIFADDRS;
  4561. INIT_IF_INDEXTONAME;
  4562. INIT_CAPGET;
  4563. INIT_AEABI_MEM;
  4564. INIT___BZERO;
  4565. INIT_FTIME;
  4566. INIT_XDR;
  4567. INIT_TSEARCH;
  4568. INIT_LIBIO_INTERNALS;
  4569. INIT_FOPEN;
  4570. INIT_FOPEN64;
  4571. INIT_OPEN_MEMSTREAM;
  4572. INIT_OBSTACK;
  4573. INIT_FFLUSH;
  4574. INIT_FCLOSE;
  4575. INIT_DLOPEN_DLCLOSE;
  4576. INIT_GETPASS;
  4577. INIT_TIMERFD;
  4578. INIT_MLOCKX;
  4579. }