sanitizer_common_interceptors.inc 364 KB

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