sanitizer_common_interceptors.inc 361 KB

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