sanitizer_common_interceptors.inc 361 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534
  1. //===-- sanitizer_common_interceptors.inc -----------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Common function interceptors for tools like AddressSanitizer,
  10. // ThreadSanitizer, MemorySanitizer, etc.
  11. //
  12. // This file should be included into the tool's interceptor file,
  13. // which has to define its own macros:
  14. // COMMON_INTERCEPTOR_ENTER
  15. // COMMON_INTERCEPTOR_ENTER_NOIGNORE
  16. // COMMON_INTERCEPTOR_READ_RANGE
  17. // COMMON_INTERCEPTOR_WRITE_RANGE
  18. // COMMON_INTERCEPTOR_INITIALIZE_RANGE
  19. // COMMON_INTERCEPTOR_DIR_ACQUIRE
  20. // COMMON_INTERCEPTOR_FD_ACQUIRE
  21. // COMMON_INTERCEPTOR_FD_RELEASE
  22. // COMMON_INTERCEPTOR_FD_ACCESS
  23. // COMMON_INTERCEPTOR_SET_THREAD_NAME
  24. // COMMON_INTERCEPTOR_DLOPEN
  25. // COMMON_INTERCEPTOR_ON_EXIT
  26. // COMMON_INTERCEPTOR_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_DLOPEN
  185. #define COMMON_INTERCEPTOR_DLOPEN(filename, flag) \
  186. ({ CheckNoDeepBind(filename, flag); REAL(dlopen)(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. void *res = COMMON_INTERCEPTOR_DLOPEN(filename, flag);
  5913. Symbolizer::GetOrInit()->InvalidateModuleList();
  5914. COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, res);
  5915. return res;
  5916. }
  5917. INTERCEPTOR(int, dlclose, void *handle) {
  5918. void *ctx;
  5919. COMMON_INTERCEPTOR_ENTER_NOIGNORE(ctx, dlclose, handle);
  5920. int res = REAL(dlclose)(handle);
  5921. Symbolizer::GetOrInit()->InvalidateModuleList();
  5922. COMMON_INTERCEPTOR_LIBRARY_UNLOADED();
  5923. return res;
  5924. }
  5925. #define INIT_DLOPEN_DLCLOSE \
  5926. COMMON_INTERCEPT_FUNCTION(dlopen); \
  5927. COMMON_INTERCEPT_FUNCTION(dlclose);
  5928. #else
  5929. #define INIT_DLOPEN_DLCLOSE
  5930. #endif
  5931. #if SANITIZER_INTERCEPT_GETPASS
  5932. INTERCEPTOR(char *, getpass, const char *prompt) {
  5933. void *ctx;
  5934. COMMON_INTERCEPTOR_ENTER(ctx, getpass, prompt);
  5935. if (prompt)
  5936. COMMON_INTERCEPTOR_READ_RANGE(ctx, prompt, internal_strlen(prompt)+1);
  5937. char *res = REAL(getpass)(prompt);
  5938. if (res) COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res)+1);
  5939. return res;
  5940. }
  5941. #define INIT_GETPASS COMMON_INTERCEPT_FUNCTION(getpass);
  5942. #else
  5943. #define INIT_GETPASS
  5944. #endif
  5945. #if SANITIZER_INTERCEPT_TIMERFD
  5946. INTERCEPTOR(int, timerfd_settime, int fd, int flags, void *new_value,
  5947. void *old_value) {
  5948. void *ctx;
  5949. COMMON_INTERCEPTOR_ENTER(ctx, timerfd_settime, fd, flags, new_value,
  5950. old_value);
  5951. COMMON_INTERCEPTOR_READ_RANGE(ctx, new_value, struct_itimerspec_sz);
  5952. int res = REAL(timerfd_settime)(fd, flags, new_value, old_value);
  5953. if (res != -1 && old_value)
  5954. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, old_value, struct_itimerspec_sz);
  5955. return res;
  5956. }
  5957. INTERCEPTOR(int, timerfd_gettime, int fd, void *curr_value) {
  5958. void *ctx;
  5959. COMMON_INTERCEPTOR_ENTER(ctx, timerfd_gettime, fd, curr_value);
  5960. int res = REAL(timerfd_gettime)(fd, curr_value);
  5961. if (res != -1 && curr_value)
  5962. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, curr_value, struct_itimerspec_sz);
  5963. return res;
  5964. }
  5965. #define INIT_TIMERFD \
  5966. COMMON_INTERCEPT_FUNCTION(timerfd_settime); \
  5967. COMMON_INTERCEPT_FUNCTION(timerfd_gettime);
  5968. #else
  5969. #define INIT_TIMERFD
  5970. #endif
  5971. #if SANITIZER_INTERCEPT_MLOCKX
  5972. // Linux kernel has a bug that leads to kernel deadlock if a process
  5973. // maps TBs of memory and then calls mlock().
  5974. static void MlockIsUnsupported() {
  5975. static atomic_uint8_t printed;
  5976. if (atomic_exchange(&printed, 1, memory_order_relaxed))
  5977. return;
  5978. VPrintf(1, "%s ignores mlock/mlockall/munlock/munlockall\n",
  5979. SanitizerToolName);
  5980. }
  5981. INTERCEPTOR(int, mlock, const void *addr, uptr len) {
  5982. MlockIsUnsupported();
  5983. return 0;
  5984. }
  5985. INTERCEPTOR(int, munlock, const void *addr, uptr len) {
  5986. MlockIsUnsupported();
  5987. return 0;
  5988. }
  5989. INTERCEPTOR(int, mlockall, int flags) {
  5990. MlockIsUnsupported();
  5991. return 0;
  5992. }
  5993. INTERCEPTOR(int, munlockall, void) {
  5994. MlockIsUnsupported();
  5995. return 0;
  5996. }
  5997. #define INIT_MLOCKX \
  5998. COMMON_INTERCEPT_FUNCTION(mlock); \
  5999. COMMON_INTERCEPT_FUNCTION(munlock); \
  6000. COMMON_INTERCEPT_FUNCTION(mlockall); \
  6001. COMMON_INTERCEPT_FUNCTION(munlockall);
  6002. #else
  6003. #define INIT_MLOCKX
  6004. #endif // SANITIZER_INTERCEPT_MLOCKX
  6005. #if SANITIZER_INTERCEPT_FOPENCOOKIE
  6006. struct WrappedCookie {
  6007. void *real_cookie;
  6008. __sanitizer_cookie_io_functions_t real_io_funcs;
  6009. };
  6010. static uptr wrapped_read(void *cookie, char *buf, uptr size) {
  6011. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  6012. WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
  6013. __sanitizer_cookie_io_read real_read = wrapped_cookie->real_io_funcs.read;
  6014. return real_read ? real_read(wrapped_cookie->real_cookie, buf, size) : 0;
  6015. }
  6016. static uptr wrapped_write(void *cookie, const char *buf, uptr size) {
  6017. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  6018. WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
  6019. __sanitizer_cookie_io_write real_write = wrapped_cookie->real_io_funcs.write;
  6020. return real_write ? real_write(wrapped_cookie->real_cookie, buf, size) : size;
  6021. }
  6022. static int wrapped_seek(void *cookie, u64 *offset, int whence) {
  6023. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  6024. COMMON_INTERCEPTOR_INITIALIZE_RANGE(offset, sizeof(*offset));
  6025. WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
  6026. __sanitizer_cookie_io_seek real_seek = wrapped_cookie->real_io_funcs.seek;
  6027. return real_seek ? real_seek(wrapped_cookie->real_cookie, offset, whence)
  6028. : -1;
  6029. }
  6030. static int wrapped_close(void *cookie) {
  6031. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  6032. WrappedCookie *wrapped_cookie = (WrappedCookie *)cookie;
  6033. __sanitizer_cookie_io_close real_close = wrapped_cookie->real_io_funcs.close;
  6034. int res = real_close ? real_close(wrapped_cookie->real_cookie) : 0;
  6035. InternalFree(wrapped_cookie);
  6036. return res;
  6037. }
  6038. INTERCEPTOR(__sanitizer_FILE *, fopencookie, void *cookie, const char *mode,
  6039. __sanitizer_cookie_io_functions_t io_funcs) {
  6040. void *ctx;
  6041. COMMON_INTERCEPTOR_ENTER(ctx, fopencookie, cookie, mode, io_funcs);
  6042. WrappedCookie *wrapped_cookie =
  6043. (WrappedCookie *)InternalAlloc(sizeof(WrappedCookie));
  6044. wrapped_cookie->real_cookie = cookie;
  6045. wrapped_cookie->real_io_funcs = io_funcs;
  6046. __sanitizer_FILE *res =
  6047. REAL(fopencookie)(wrapped_cookie, mode, {wrapped_read, wrapped_write,
  6048. wrapped_seek, wrapped_close});
  6049. return res;
  6050. }
  6051. #define INIT_FOPENCOOKIE COMMON_INTERCEPT_FUNCTION(fopencookie);
  6052. #else
  6053. #define INIT_FOPENCOOKIE
  6054. #endif // SANITIZER_INTERCEPT_FOPENCOOKIE
  6055. #if SANITIZER_INTERCEPT_SEM
  6056. INTERCEPTOR(int, sem_init, __sanitizer_sem_t *s, int pshared, unsigned value) {
  6057. void *ctx;
  6058. COMMON_INTERCEPTOR_ENTER(ctx, sem_init, s, pshared, value);
  6059. // Workaround a bug in glibc's "old" semaphore implementation by
  6060. // zero-initializing the sem_t contents. This has to be done here because
  6061. // interceptors bind to the lowest symbols version by default, hitting the
  6062. // buggy code path while the non-sanitized build of the same code works fine.
  6063. REAL(memset)(s, 0, sizeof(*s));
  6064. int res = REAL(sem_init)(s, pshared, value);
  6065. return res;
  6066. }
  6067. INTERCEPTOR(int, sem_destroy, __sanitizer_sem_t *s) {
  6068. void *ctx;
  6069. COMMON_INTERCEPTOR_ENTER(ctx, sem_destroy, s);
  6070. int res = REAL(sem_destroy)(s);
  6071. return res;
  6072. }
  6073. INTERCEPTOR(int, sem_wait, __sanitizer_sem_t *s) {
  6074. void *ctx;
  6075. COMMON_INTERCEPTOR_ENTER(ctx, sem_wait, s);
  6076. int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_wait)(s);
  6077. if (res == 0) {
  6078. COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
  6079. }
  6080. return res;
  6081. }
  6082. INTERCEPTOR(int, sem_trywait, __sanitizer_sem_t *s) {
  6083. void *ctx;
  6084. COMMON_INTERCEPTOR_ENTER(ctx, sem_trywait, s);
  6085. int res = REAL(sem_trywait)(s);
  6086. if (res == 0) {
  6087. COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
  6088. }
  6089. return res;
  6090. }
  6091. INTERCEPTOR(int, sem_timedwait, __sanitizer_sem_t *s, void *abstime) {
  6092. void *ctx;
  6093. COMMON_INTERCEPTOR_ENTER(ctx, sem_timedwait, s, abstime);
  6094. COMMON_INTERCEPTOR_READ_RANGE(ctx, abstime, struct_timespec_sz);
  6095. int res = COMMON_INTERCEPTOR_BLOCK_REAL(sem_timedwait)(s, abstime);
  6096. if (res == 0) {
  6097. COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
  6098. }
  6099. return res;
  6100. }
  6101. INTERCEPTOR(int, sem_post, __sanitizer_sem_t *s) {
  6102. void *ctx;
  6103. COMMON_INTERCEPTOR_ENTER(ctx, sem_post, s);
  6104. COMMON_INTERCEPTOR_RELEASE(ctx, (uptr)s);
  6105. int res = REAL(sem_post)(s);
  6106. return res;
  6107. }
  6108. INTERCEPTOR(int, sem_getvalue, __sanitizer_sem_t *s, int *sval) {
  6109. void *ctx;
  6110. COMMON_INTERCEPTOR_ENTER(ctx, sem_getvalue, s, sval);
  6111. int res = REAL(sem_getvalue)(s, sval);
  6112. if (res == 0) {
  6113. COMMON_INTERCEPTOR_ACQUIRE(ctx, (uptr)s);
  6114. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sval, sizeof(*sval));
  6115. }
  6116. return res;
  6117. }
  6118. INTERCEPTOR(__sanitizer_sem_t *, sem_open, const char *name, int oflag, ...) {
  6119. void *ctx;
  6120. va_list ap;
  6121. va_start(ap, oflag);
  6122. u32 mode = va_arg(ap, u32);
  6123. u32 value = va_arg(ap, u32);
  6124. COMMON_INTERCEPTOR_ENTER(ctx, sem_open, name, oflag, mode, value);
  6125. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  6126. __sanitizer_sem_t *s = REAL(sem_open)(name, oflag, mode, value);
  6127. if (s)
  6128. COMMON_INTERCEPTOR_INITIALIZE_RANGE(s, sizeof(*s));
  6129. va_end(ap);
  6130. return s;
  6131. }
  6132. INTERCEPTOR(int, sem_unlink, const char *name) {
  6133. void *ctx;
  6134. COMMON_INTERCEPTOR_ENTER(ctx, sem_unlink, name);
  6135. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  6136. return REAL(sem_unlink)(name);
  6137. }
  6138. # define INIT_SEM \
  6139. COMMON_INTERCEPT_FUNCTION(sem_init); \
  6140. COMMON_INTERCEPT_FUNCTION(sem_destroy); \
  6141. COMMON_INTERCEPT_FUNCTION(sem_wait); \
  6142. COMMON_INTERCEPT_FUNCTION(sem_trywait); \
  6143. COMMON_INTERCEPT_FUNCTION(sem_timedwait); \
  6144. COMMON_INTERCEPT_FUNCTION(sem_post); \
  6145. COMMON_INTERCEPT_FUNCTION(sem_getvalue); \
  6146. COMMON_INTERCEPT_FUNCTION(sem_open); \
  6147. COMMON_INTERCEPT_FUNCTION(sem_unlink);
  6148. #else
  6149. # define INIT_SEM
  6150. #endif // SANITIZER_INTERCEPT_SEM
  6151. #if SANITIZER_INTERCEPT_PTHREAD_SETCANCEL
  6152. INTERCEPTOR(int, pthread_setcancelstate, int state, int *oldstate) {
  6153. void *ctx;
  6154. COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcancelstate, state, oldstate);
  6155. int res = REAL(pthread_setcancelstate)(state, oldstate);
  6156. if (res == 0 && oldstate != nullptr)
  6157. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldstate, sizeof(*oldstate));
  6158. return res;
  6159. }
  6160. INTERCEPTOR(int, pthread_setcanceltype, int type, int *oldtype) {
  6161. void *ctx;
  6162. COMMON_INTERCEPTOR_ENTER(ctx, pthread_setcanceltype, type, oldtype);
  6163. int res = REAL(pthread_setcanceltype)(type, oldtype);
  6164. if (res == 0 && oldtype != nullptr)
  6165. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldtype, sizeof(*oldtype));
  6166. return res;
  6167. }
  6168. #define INIT_PTHREAD_SETCANCEL \
  6169. COMMON_INTERCEPT_FUNCTION(pthread_setcancelstate); \
  6170. COMMON_INTERCEPT_FUNCTION(pthread_setcanceltype);
  6171. #else
  6172. #define INIT_PTHREAD_SETCANCEL
  6173. #endif
  6174. #if SANITIZER_INTERCEPT_MINCORE
  6175. INTERCEPTOR(int, mincore, void *addr, uptr length, unsigned char *vec) {
  6176. void *ctx;
  6177. COMMON_INTERCEPTOR_ENTER(ctx, mincore, addr, length, vec);
  6178. int res = REAL(mincore)(addr, length, vec);
  6179. if (res == 0) {
  6180. uptr page_size = GetPageSizeCached();
  6181. uptr vec_size = ((length + page_size - 1) & (~(page_size - 1))) / page_size;
  6182. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, vec, vec_size);
  6183. }
  6184. return res;
  6185. }
  6186. #define INIT_MINCORE COMMON_INTERCEPT_FUNCTION(mincore);
  6187. #else
  6188. #define INIT_MINCORE
  6189. #endif
  6190. #if SANITIZER_INTERCEPT_PROCESS_VM_READV
  6191. INTERCEPTOR(SSIZE_T, process_vm_readv, int pid, __sanitizer_iovec *local_iov,
  6192. uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
  6193. uptr flags) {
  6194. void *ctx;
  6195. COMMON_INTERCEPTOR_ENTER(ctx, process_vm_readv, pid, local_iov, liovcnt,
  6196. remote_iov, riovcnt, flags);
  6197. SSIZE_T res = REAL(process_vm_readv)(pid, local_iov, liovcnt, remote_iov,
  6198. riovcnt, flags);
  6199. if (res > 0)
  6200. write_iovec(ctx, local_iov, liovcnt, res);
  6201. return res;
  6202. }
  6203. INTERCEPTOR(SSIZE_T, process_vm_writev, int pid, __sanitizer_iovec *local_iov,
  6204. uptr liovcnt, __sanitizer_iovec *remote_iov, uptr riovcnt,
  6205. uptr flags) {
  6206. void *ctx;
  6207. COMMON_INTERCEPTOR_ENTER(ctx, process_vm_writev, pid, local_iov, liovcnt,
  6208. remote_iov, riovcnt, flags);
  6209. SSIZE_T res = REAL(process_vm_writev)(pid, local_iov, liovcnt, remote_iov,
  6210. riovcnt, flags);
  6211. if (res > 0)
  6212. read_iovec(ctx, local_iov, liovcnt, res);
  6213. return res;
  6214. }
  6215. #define INIT_PROCESS_VM_READV \
  6216. COMMON_INTERCEPT_FUNCTION(process_vm_readv); \
  6217. COMMON_INTERCEPT_FUNCTION(process_vm_writev);
  6218. #else
  6219. #define INIT_PROCESS_VM_READV
  6220. #endif
  6221. #if SANITIZER_INTERCEPT_CTERMID
  6222. INTERCEPTOR(char *, ctermid, char *s) {
  6223. void *ctx;
  6224. COMMON_INTERCEPTOR_ENTER(ctx, ctermid, s);
  6225. char *res = REAL(ctermid)(s);
  6226. if (res) {
  6227. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  6228. }
  6229. return res;
  6230. }
  6231. #define INIT_CTERMID COMMON_INTERCEPT_FUNCTION(ctermid);
  6232. #else
  6233. #define INIT_CTERMID
  6234. #endif
  6235. #if SANITIZER_INTERCEPT_CTERMID_R
  6236. INTERCEPTOR(char *, ctermid_r, char *s) {
  6237. void *ctx;
  6238. COMMON_INTERCEPTOR_ENTER(ctx, ctermid_r, s);
  6239. char *res = REAL(ctermid_r)(s);
  6240. if (res) {
  6241. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  6242. }
  6243. return res;
  6244. }
  6245. #define INIT_CTERMID_R COMMON_INTERCEPT_FUNCTION(ctermid_r);
  6246. #else
  6247. #define INIT_CTERMID_R
  6248. #endif
  6249. #if SANITIZER_INTERCEPT_RECV_RECVFROM
  6250. INTERCEPTOR(SSIZE_T, recv, int fd, void *buf, SIZE_T len, int flags) {
  6251. void *ctx;
  6252. COMMON_INTERCEPTOR_ENTER(ctx, recv, fd, buf, len, flags);
  6253. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6254. SSIZE_T res = REAL(recv)(fd, buf, len, flags);
  6255. if (res > 0) {
  6256. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
  6257. }
  6258. if (res >= 0 && fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  6259. return res;
  6260. }
  6261. INTERCEPTOR(SSIZE_T, recvfrom, int fd, void *buf, SIZE_T len, int flags,
  6262. void *srcaddr, int *addrlen) {
  6263. void *ctx;
  6264. COMMON_INTERCEPTOR_ENTER(ctx, recvfrom, fd, buf, len, flags, srcaddr,
  6265. addrlen);
  6266. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6267. SIZE_T srcaddr_sz;
  6268. if (srcaddr) srcaddr_sz = *addrlen;
  6269. (void)srcaddr_sz; // prevent "set but not used" warning
  6270. SSIZE_T res = REAL(recvfrom)(fd, buf, len, flags, srcaddr, addrlen);
  6271. if (res > 0)
  6272. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, Min((SIZE_T)res, len));
  6273. if (res >= 0 && srcaddr)
  6274. COMMON_INTERCEPTOR_INITIALIZE_RANGE(srcaddr,
  6275. Min((SIZE_T)*addrlen, srcaddr_sz));
  6276. return res;
  6277. }
  6278. #define INIT_RECV_RECVFROM \
  6279. COMMON_INTERCEPT_FUNCTION(recv); \
  6280. COMMON_INTERCEPT_FUNCTION(recvfrom);
  6281. #else
  6282. #define INIT_RECV_RECVFROM
  6283. #endif
  6284. #if SANITIZER_INTERCEPT_SEND_SENDTO
  6285. INTERCEPTOR(SSIZE_T, send, int fd, void *buf, SIZE_T len, int flags) {
  6286. void *ctx;
  6287. COMMON_INTERCEPTOR_ENTER(ctx, send, fd, buf, len, flags);
  6288. if (fd >= 0) {
  6289. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6290. COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  6291. }
  6292. SSIZE_T res = REAL(send)(fd, buf, len, flags);
  6293. if (common_flags()->intercept_send && res > 0)
  6294. COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
  6295. return res;
  6296. }
  6297. INTERCEPTOR(SSIZE_T, sendto, int fd, void *buf, SIZE_T len, int flags,
  6298. void *dstaddr, int addrlen) {
  6299. void *ctx;
  6300. COMMON_INTERCEPTOR_ENTER(ctx, sendto, fd, buf, len, flags, dstaddr, addrlen);
  6301. if (fd >= 0) {
  6302. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6303. COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  6304. }
  6305. // Can't check dstaddr as it may have uninitialized padding at the end.
  6306. SSIZE_T res = REAL(sendto)(fd, buf, len, flags, dstaddr, addrlen);
  6307. if (common_flags()->intercept_send && res > 0)
  6308. COMMON_INTERCEPTOR_READ_RANGE(ctx, buf, Min((SIZE_T)res, len));
  6309. return res;
  6310. }
  6311. #define INIT_SEND_SENDTO \
  6312. COMMON_INTERCEPT_FUNCTION(send); \
  6313. COMMON_INTERCEPT_FUNCTION(sendto);
  6314. #else
  6315. #define INIT_SEND_SENDTO
  6316. #endif
  6317. #if SANITIZER_INTERCEPT_EVENTFD_READ_WRITE
  6318. INTERCEPTOR(int, eventfd_read, int fd, u64 *value) {
  6319. void *ctx;
  6320. COMMON_INTERCEPTOR_ENTER(ctx, eventfd_read, fd, value);
  6321. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6322. int res = REAL(eventfd_read)(fd, value);
  6323. if (res == 0) {
  6324. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, value, sizeof(*value));
  6325. if (fd >= 0) COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  6326. }
  6327. return res;
  6328. }
  6329. INTERCEPTOR(int, eventfd_write, int fd, u64 value) {
  6330. void *ctx;
  6331. COMMON_INTERCEPTOR_ENTER(ctx, eventfd_write, fd, value);
  6332. if (fd >= 0) {
  6333. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  6334. COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd);
  6335. }
  6336. int res = REAL(eventfd_write)(fd, value);
  6337. return res;
  6338. }
  6339. #define INIT_EVENTFD_READ_WRITE \
  6340. COMMON_INTERCEPT_FUNCTION(eventfd_read); \
  6341. COMMON_INTERCEPT_FUNCTION(eventfd_write)
  6342. #else
  6343. #define INIT_EVENTFD_READ_WRITE
  6344. #endif
  6345. #if SANITIZER_INTERCEPT_STAT
  6346. INTERCEPTOR(int, stat, const char *path, void *buf) {
  6347. void *ctx;
  6348. COMMON_INTERCEPTOR_ENTER(ctx, stat, path, buf);
  6349. if (common_flags()->intercept_stat)
  6350. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6351. int res = REAL(stat)(path, buf);
  6352. if (!res)
  6353. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
  6354. return res;
  6355. }
  6356. #define INIT_STAT COMMON_INTERCEPT_FUNCTION(stat)
  6357. #else
  6358. #define INIT_STAT
  6359. #endif
  6360. #if SANITIZER_INTERCEPT_LSTAT
  6361. INTERCEPTOR(int, lstat, const char *path, void *buf) {
  6362. void *ctx;
  6363. COMMON_INTERCEPTOR_ENTER(ctx, lstat, path, buf);
  6364. if (common_flags()->intercept_stat)
  6365. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6366. int res = REAL(lstat)(path, buf);
  6367. if (!res)
  6368. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
  6369. return res;
  6370. }
  6371. #define INIT_LSTAT COMMON_INTERCEPT_FUNCTION(lstat)
  6372. #else
  6373. #define INIT_LSTAT
  6374. #endif
  6375. #if SANITIZER_INTERCEPT___XSTAT
  6376. INTERCEPTOR(int, __xstat, int version, const char *path, void *buf) {
  6377. void *ctx;
  6378. COMMON_INTERCEPTOR_ENTER(ctx, __xstat, version, path, buf);
  6379. if (common_flags()->intercept_stat)
  6380. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6381. int res = REAL(__xstat)(version, path, buf);
  6382. if (!res)
  6383. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
  6384. return res;
  6385. }
  6386. #define INIT___XSTAT COMMON_INTERCEPT_FUNCTION(__xstat)
  6387. #else
  6388. #define INIT___XSTAT
  6389. #endif
  6390. #if SANITIZER_INTERCEPT___XSTAT64
  6391. INTERCEPTOR(int, __xstat64, int version, const char *path, void *buf) {
  6392. void *ctx;
  6393. COMMON_INTERCEPTOR_ENTER(ctx, __xstat64, version, path, buf);
  6394. if (common_flags()->intercept_stat)
  6395. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6396. int res = REAL(__xstat64)(version, path, buf);
  6397. if (!res)
  6398. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
  6399. return res;
  6400. }
  6401. #define INIT___XSTAT64 COMMON_INTERCEPT_FUNCTION(__xstat64)
  6402. #else
  6403. #define INIT___XSTAT64
  6404. #endif
  6405. #if SANITIZER_INTERCEPT___LXSTAT
  6406. INTERCEPTOR(int, __lxstat, int version, const char *path, void *buf) {
  6407. void *ctx;
  6408. COMMON_INTERCEPTOR_ENTER(ctx, __lxstat, version, path, buf);
  6409. if (common_flags()->intercept_stat)
  6410. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6411. int res = REAL(__lxstat)(version, path, buf);
  6412. if (!res)
  6413. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat_sz);
  6414. return res;
  6415. }
  6416. #define INIT___LXSTAT COMMON_INTERCEPT_FUNCTION(__lxstat)
  6417. #else
  6418. #define INIT___LXSTAT
  6419. #endif
  6420. #if SANITIZER_INTERCEPT___LXSTAT64
  6421. INTERCEPTOR(int, __lxstat64, int version, const char *path, void *buf) {
  6422. void *ctx;
  6423. COMMON_INTERCEPTOR_ENTER(ctx, __lxstat64, version, path, buf);
  6424. if (common_flags()->intercept_stat)
  6425. COMMON_INTERCEPTOR_READ_STRING(ctx, path, 0);
  6426. int res = REAL(__lxstat64)(version, path, buf);
  6427. if (!res)
  6428. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer::struct_stat64_sz);
  6429. return res;
  6430. }
  6431. #define INIT___LXSTAT64 COMMON_INTERCEPT_FUNCTION(__lxstat64)
  6432. #else
  6433. #define INIT___LXSTAT64
  6434. #endif
  6435. // FIXME: add other *stat interceptor
  6436. #if SANITIZER_INTERCEPT_UTMP
  6437. INTERCEPTOR(void *, getutent, int dummy) {
  6438. void *ctx;
  6439. COMMON_INTERCEPTOR_ENTER(ctx, getutent, dummy);
  6440. void *res = REAL(getutent)(dummy);
  6441. if (res)
  6442. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
  6443. return res;
  6444. }
  6445. INTERCEPTOR(void *, getutid, void *ut) {
  6446. void *ctx;
  6447. COMMON_INTERCEPTOR_ENTER(ctx, getutid, ut);
  6448. void *res = REAL(getutid)(ut);
  6449. if (res)
  6450. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
  6451. return res;
  6452. }
  6453. INTERCEPTOR(void *, getutline, void *ut) {
  6454. void *ctx;
  6455. COMMON_INTERCEPTOR_ENTER(ctx, getutline, ut);
  6456. void *res = REAL(getutline)(ut);
  6457. if (res)
  6458. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmp_sz);
  6459. return res;
  6460. }
  6461. #define INIT_UTMP \
  6462. COMMON_INTERCEPT_FUNCTION(getutent); \
  6463. COMMON_INTERCEPT_FUNCTION(getutid); \
  6464. COMMON_INTERCEPT_FUNCTION(getutline);
  6465. #else
  6466. #define INIT_UTMP
  6467. #endif
  6468. #if SANITIZER_INTERCEPT_UTMPX
  6469. INTERCEPTOR(void *, getutxent, int dummy) {
  6470. void *ctx;
  6471. COMMON_INTERCEPTOR_ENTER(ctx, getutxent, dummy);
  6472. void *res = REAL(getutxent)(dummy);
  6473. if (res)
  6474. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
  6475. return res;
  6476. }
  6477. INTERCEPTOR(void *, getutxid, void *ut) {
  6478. void *ctx;
  6479. COMMON_INTERCEPTOR_ENTER(ctx, getutxid, ut);
  6480. void *res = REAL(getutxid)(ut);
  6481. if (res)
  6482. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
  6483. return res;
  6484. }
  6485. INTERCEPTOR(void *, getutxline, void *ut) {
  6486. void *ctx;
  6487. COMMON_INTERCEPTOR_ENTER(ctx, getutxline, ut);
  6488. void *res = REAL(getutxline)(ut);
  6489. if (res)
  6490. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, __sanitizer::struct_utmpx_sz);
  6491. return res;
  6492. }
  6493. INTERCEPTOR(void *, pututxline, const void *ut) {
  6494. void *ctx;
  6495. COMMON_INTERCEPTOR_ENTER(ctx, pututxline, ut);
  6496. if (ut)
  6497. COMMON_INTERCEPTOR_READ_RANGE(ctx, ut, __sanitizer::struct_utmpx_sz);
  6498. void *res = REAL(pututxline)(ut);
  6499. if (res)
  6500. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_utmpx_sz);
  6501. return res;
  6502. }
  6503. #define INIT_UTMPX \
  6504. COMMON_INTERCEPT_FUNCTION(getutxent); \
  6505. COMMON_INTERCEPT_FUNCTION(getutxid); \
  6506. COMMON_INTERCEPT_FUNCTION(getutxline); \
  6507. COMMON_INTERCEPT_FUNCTION(pututxline);
  6508. #else
  6509. #define INIT_UTMPX
  6510. #endif
  6511. #if SANITIZER_INTERCEPT_GETLOADAVG
  6512. INTERCEPTOR(int, getloadavg, double *loadavg, int nelem) {
  6513. void *ctx;
  6514. COMMON_INTERCEPTOR_ENTER(ctx, getloadavg, loadavg, nelem);
  6515. int res = REAL(getloadavg)(loadavg, nelem);
  6516. if (res > 0)
  6517. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, loadavg, res * sizeof(*loadavg));
  6518. return res;
  6519. }
  6520. #define INIT_GETLOADAVG \
  6521. COMMON_INTERCEPT_FUNCTION(getloadavg);
  6522. #else
  6523. #define INIT_GETLOADAVG
  6524. #endif
  6525. #if SANITIZER_INTERCEPT_MCHECK_MPROBE
  6526. INTERCEPTOR(int, mcheck, void (*abortfunc)(int mstatus)) {
  6527. return 0;
  6528. }
  6529. INTERCEPTOR(int, mcheck_pedantic, void (*abortfunc)(int mstatus)) {
  6530. return 0;
  6531. }
  6532. INTERCEPTOR(int, mprobe, void *ptr) {
  6533. return 0;
  6534. }
  6535. #endif
  6536. INTERCEPTOR(SIZE_T, wcslen, const wchar_t *s) {
  6537. void *ctx;
  6538. COMMON_INTERCEPTOR_ENTER(ctx, wcslen, s);
  6539. SIZE_T res = REAL(wcslen)(s);
  6540. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (res + 1));
  6541. return res;
  6542. }
  6543. INTERCEPTOR(SIZE_T, wcsnlen, const wchar_t *s, SIZE_T n) {
  6544. void *ctx;
  6545. COMMON_INTERCEPTOR_ENTER(ctx, wcsnlen, s, n);
  6546. SIZE_T res = REAL(wcsnlen)(s, n);
  6547. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * Min(res + 1, n));
  6548. return res;
  6549. }
  6550. #define INIT_WCSLEN \
  6551. COMMON_INTERCEPT_FUNCTION(wcslen); \
  6552. COMMON_INTERCEPT_FUNCTION(wcsnlen);
  6553. #if SANITIZER_INTERCEPT_WCSCAT
  6554. INTERCEPTOR(wchar_t *, wcscat, wchar_t *dst, const wchar_t *src) {
  6555. void *ctx;
  6556. COMMON_INTERCEPTOR_ENTER(ctx, wcscat, dst, src);
  6557. SIZE_T src_size = internal_wcslen(src);
  6558. SIZE_T dst_size = internal_wcslen(dst);
  6559. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, (src_size + 1) * sizeof(wchar_t));
  6560. COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
  6561. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
  6562. (src_size + 1) * sizeof(wchar_t));
  6563. return REAL(wcscat)(dst, src);
  6564. }
  6565. INTERCEPTOR(wchar_t *, wcsncat, wchar_t *dst, const wchar_t *src, SIZE_T n) {
  6566. void *ctx;
  6567. COMMON_INTERCEPTOR_ENTER(ctx, wcsncat, dst, src, n);
  6568. SIZE_T src_size = internal_wcsnlen(src, n);
  6569. SIZE_T dst_size = internal_wcslen(dst);
  6570. COMMON_INTERCEPTOR_READ_RANGE(ctx, src,
  6571. Min(src_size + 1, n) * sizeof(wchar_t));
  6572. COMMON_INTERCEPTOR_READ_RANGE(ctx, dst, (dst_size + 1) * sizeof(wchar_t));
  6573. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst + dst_size,
  6574. (src_size + 1) * sizeof(wchar_t));
  6575. return REAL(wcsncat)(dst, src, n);
  6576. }
  6577. #define INIT_WCSCAT \
  6578. COMMON_INTERCEPT_FUNCTION(wcscat); \
  6579. COMMON_INTERCEPT_FUNCTION(wcsncat);
  6580. #else
  6581. #define INIT_WCSCAT
  6582. #endif
  6583. #if SANITIZER_INTERCEPT_WCSDUP
  6584. INTERCEPTOR(wchar_t *, wcsdup, wchar_t *s) {
  6585. void *ctx;
  6586. COMMON_INTERCEPTOR_ENTER(ctx, wcsdup, s);
  6587. SIZE_T len = internal_wcslen(s);
  6588. COMMON_INTERCEPTOR_READ_RANGE(ctx, s, sizeof(wchar_t) * (len + 1));
  6589. wchar_t *result = REAL(wcsdup)(s);
  6590. if (result)
  6591. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof(wchar_t) * (len + 1));
  6592. return result;
  6593. }
  6594. #define INIT_WCSDUP COMMON_INTERCEPT_FUNCTION(wcsdup);
  6595. #else
  6596. #define INIT_WCSDUP
  6597. #endif
  6598. #if SANITIZER_INTERCEPT_STRXFRM
  6599. static SIZE_T RealStrLen(const char *str) { return internal_strlen(str); }
  6600. static SIZE_T RealStrLen(const wchar_t *str) { return internal_wcslen(str); }
  6601. #define STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len, ...) \
  6602. { \
  6603. void *ctx; \
  6604. COMMON_INTERCEPTOR_ENTER(ctx, strxfrm, dest, src, len, ##__VA_ARGS__); \
  6605. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, \
  6606. sizeof(*src) * (RealStrLen(src) + 1)); \
  6607. SIZE_T res = REAL(strxfrm)(dest, src, len, ##__VA_ARGS__); \
  6608. if (res < len) \
  6609. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dest, sizeof(*src) * (res + 1)); \
  6610. return res; \
  6611. }
  6612. INTERCEPTOR(SIZE_T, strxfrm, char *dest, const char *src, SIZE_T len) {
  6613. STRXFRM_INTERCEPTOR_IMPL(strxfrm, dest, src, len);
  6614. }
  6615. INTERCEPTOR(SIZE_T, strxfrm_l, char *dest, const char *src, SIZE_T len,
  6616. void *locale) {
  6617. STRXFRM_INTERCEPTOR_IMPL(strxfrm_l, dest, src, len, locale);
  6618. }
  6619. #define INIT_STRXFRM \
  6620. COMMON_INTERCEPT_FUNCTION(strxfrm); \
  6621. COMMON_INTERCEPT_FUNCTION(strxfrm_l);
  6622. #else
  6623. #define INIT_STRXFRM
  6624. #endif
  6625. #if SANITIZER_INTERCEPT___STRXFRM_L
  6626. INTERCEPTOR(SIZE_T, __strxfrm_l, char *dest, const char *src, SIZE_T len,
  6627. void *locale) {
  6628. STRXFRM_INTERCEPTOR_IMPL(__strxfrm_l, dest, src, len, locale);
  6629. }
  6630. #define INIT___STRXFRM_L COMMON_INTERCEPT_FUNCTION(__strxfrm_l);
  6631. #else
  6632. #define INIT___STRXFRM_L
  6633. #endif
  6634. #if SANITIZER_INTERCEPT_WCSXFRM
  6635. INTERCEPTOR(SIZE_T, wcsxfrm, wchar_t *dest, const wchar_t *src, SIZE_T len) {
  6636. STRXFRM_INTERCEPTOR_IMPL(wcsxfrm, dest, src, len);
  6637. }
  6638. INTERCEPTOR(SIZE_T, wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
  6639. void *locale) {
  6640. STRXFRM_INTERCEPTOR_IMPL(wcsxfrm_l, dest, src, len, locale);
  6641. }
  6642. #define INIT_WCSXFRM \
  6643. COMMON_INTERCEPT_FUNCTION(wcsxfrm); \
  6644. COMMON_INTERCEPT_FUNCTION(wcsxfrm_l);
  6645. #else
  6646. #define INIT_WCSXFRM
  6647. #endif
  6648. #if SANITIZER_INTERCEPT___WCSXFRM_L
  6649. INTERCEPTOR(SIZE_T, __wcsxfrm_l, wchar_t *dest, const wchar_t *src, SIZE_T len,
  6650. void *locale) {
  6651. STRXFRM_INTERCEPTOR_IMPL(__wcsxfrm_l, dest, src, len, locale);
  6652. }
  6653. #define INIT___WCSXFRM_L COMMON_INTERCEPT_FUNCTION(__wcsxfrm_l);
  6654. #else
  6655. #define INIT___WCSXFRM_L
  6656. #endif
  6657. #if SANITIZER_INTERCEPT_ACCT
  6658. INTERCEPTOR(int, acct, const char *file) {
  6659. void *ctx;
  6660. COMMON_INTERCEPTOR_ENTER(ctx, acct, file);
  6661. if (file)
  6662. COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
  6663. return REAL(acct)(file);
  6664. }
  6665. #define INIT_ACCT COMMON_INTERCEPT_FUNCTION(acct)
  6666. #else
  6667. #define INIT_ACCT
  6668. #endif
  6669. #if SANITIZER_INTERCEPT_USER_FROM_UID
  6670. INTERCEPTOR(const char *, user_from_uid, u32 uid, int nouser) {
  6671. void *ctx;
  6672. const char *user;
  6673. COMMON_INTERCEPTOR_ENTER(ctx, user_from_uid, uid, nouser);
  6674. user = REAL(user_from_uid)(uid, nouser);
  6675. if (user)
  6676. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, user, internal_strlen(user) + 1);
  6677. return user;
  6678. }
  6679. #define INIT_USER_FROM_UID COMMON_INTERCEPT_FUNCTION(user_from_uid)
  6680. #else
  6681. #define INIT_USER_FROM_UID
  6682. #endif
  6683. #if SANITIZER_INTERCEPT_UID_FROM_USER
  6684. INTERCEPTOR(int, uid_from_user, const char *name, u32 *uid) {
  6685. void *ctx;
  6686. int res;
  6687. COMMON_INTERCEPTOR_ENTER(ctx, uid_from_user, name, uid);
  6688. if (name)
  6689. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  6690. res = REAL(uid_from_user)(name, uid);
  6691. if (uid)
  6692. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, uid, sizeof(*uid));
  6693. return res;
  6694. }
  6695. #define INIT_UID_FROM_USER COMMON_INTERCEPT_FUNCTION(uid_from_user)
  6696. #else
  6697. #define INIT_UID_FROM_USER
  6698. #endif
  6699. #if SANITIZER_INTERCEPT_GROUP_FROM_GID
  6700. INTERCEPTOR(const char *, group_from_gid, u32 gid, int nogroup) {
  6701. void *ctx;
  6702. const char *group;
  6703. COMMON_INTERCEPTOR_ENTER(ctx, group_from_gid, gid, nogroup);
  6704. group = REAL(group_from_gid)(gid, nogroup);
  6705. if (group)
  6706. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, group, internal_strlen(group) + 1);
  6707. return group;
  6708. }
  6709. #define INIT_GROUP_FROM_GID COMMON_INTERCEPT_FUNCTION(group_from_gid)
  6710. #else
  6711. #define INIT_GROUP_FROM_GID
  6712. #endif
  6713. #if SANITIZER_INTERCEPT_GID_FROM_GROUP
  6714. INTERCEPTOR(int, gid_from_group, const char *group, u32 *gid) {
  6715. void *ctx;
  6716. int res;
  6717. COMMON_INTERCEPTOR_ENTER(ctx, gid_from_group, group, gid);
  6718. if (group)
  6719. COMMON_INTERCEPTOR_READ_RANGE(ctx, group, internal_strlen(group) + 1);
  6720. res = REAL(gid_from_group)(group, gid);
  6721. if (gid)
  6722. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, gid, sizeof(*gid));
  6723. return res;
  6724. }
  6725. #define INIT_GID_FROM_GROUP COMMON_INTERCEPT_FUNCTION(gid_from_group)
  6726. #else
  6727. #define INIT_GID_FROM_GROUP
  6728. #endif
  6729. #if SANITIZER_INTERCEPT_ACCESS
  6730. INTERCEPTOR(int, access, const char *path, int mode) {
  6731. void *ctx;
  6732. COMMON_INTERCEPTOR_ENTER(ctx, access, path, mode);
  6733. if (path)
  6734. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  6735. return REAL(access)(path, mode);
  6736. }
  6737. #define INIT_ACCESS COMMON_INTERCEPT_FUNCTION(access)
  6738. #else
  6739. #define INIT_ACCESS
  6740. #endif
  6741. #if SANITIZER_INTERCEPT_FACCESSAT
  6742. INTERCEPTOR(int, faccessat, int fd, const char *path, int mode, int flags) {
  6743. void *ctx;
  6744. COMMON_INTERCEPTOR_ENTER(ctx, faccessat, fd, path, mode, flags);
  6745. if (path)
  6746. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  6747. return REAL(faccessat)(fd, path, mode, flags);
  6748. }
  6749. #define INIT_FACCESSAT COMMON_INTERCEPT_FUNCTION(faccessat)
  6750. #else
  6751. #define INIT_FACCESSAT
  6752. #endif
  6753. #if SANITIZER_INTERCEPT_GETGROUPLIST
  6754. INTERCEPTOR(int, getgrouplist, const char *name, u32 basegid, u32 *groups,
  6755. int *ngroups) {
  6756. void *ctx;
  6757. int res;
  6758. COMMON_INTERCEPTOR_ENTER(ctx, getgrouplist, name, basegid, groups, ngroups);
  6759. if (name)
  6760. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  6761. if (ngroups)
  6762. COMMON_INTERCEPTOR_READ_RANGE(ctx, ngroups, sizeof(*ngroups));
  6763. res = REAL(getgrouplist)(name, basegid, groups, ngroups);
  6764. if (!res && groups && ngroups) {
  6765. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
  6766. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
  6767. }
  6768. return res;
  6769. }
  6770. #define INIT_GETGROUPLIST COMMON_INTERCEPT_FUNCTION(getgrouplist);
  6771. #else
  6772. #define INIT_GETGROUPLIST
  6773. #endif
  6774. #if SANITIZER_INTERCEPT_GETGROUPMEMBERSHIP
  6775. INTERCEPTOR(int, getgroupmembership, const char *name, u32 basegid, u32 *groups,
  6776. int maxgrp, int *ngroups) {
  6777. void *ctx;
  6778. int res;
  6779. COMMON_INTERCEPTOR_ENTER(ctx, getgroupmembership, name, basegid, groups,
  6780. maxgrp, ngroups);
  6781. if (name)
  6782. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  6783. res = REAL(getgroupmembership)(name, basegid, groups, maxgrp, ngroups);
  6784. if (!res && groups && ngroups) {
  6785. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, groups, sizeof(*groups) * (*ngroups));
  6786. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ngroups, sizeof(*ngroups));
  6787. }
  6788. return res;
  6789. }
  6790. #define INIT_GETGROUPMEMBERSHIP COMMON_INTERCEPT_FUNCTION(getgroupmembership);
  6791. #else
  6792. #define INIT_GETGROUPMEMBERSHIP
  6793. #endif
  6794. #if SANITIZER_INTERCEPT_READLINK
  6795. INTERCEPTOR(SSIZE_T, readlink, const char *path, char *buf, SIZE_T bufsiz) {
  6796. void* ctx;
  6797. COMMON_INTERCEPTOR_ENTER(ctx, readlink, path, buf, bufsiz);
  6798. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  6799. SSIZE_T res = REAL(readlink)(path, buf, bufsiz);
  6800. if (res > 0)
  6801. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
  6802. return res;
  6803. }
  6804. #define INIT_READLINK COMMON_INTERCEPT_FUNCTION(readlink)
  6805. #else
  6806. #define INIT_READLINK
  6807. #endif
  6808. #if SANITIZER_INTERCEPT_READLINKAT
  6809. INTERCEPTOR(SSIZE_T, readlinkat, int dirfd, const char *path, char *buf,
  6810. SIZE_T bufsiz) {
  6811. void* ctx;
  6812. COMMON_INTERCEPTOR_ENTER(ctx, readlinkat, dirfd, path, buf, bufsiz);
  6813. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  6814. SSIZE_T res = REAL(readlinkat)(dirfd, path, buf, bufsiz);
  6815. if (res > 0)
  6816. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, res);
  6817. return res;
  6818. }
  6819. #define INIT_READLINKAT COMMON_INTERCEPT_FUNCTION(readlinkat)
  6820. #else
  6821. #define INIT_READLINKAT
  6822. #endif
  6823. #if SANITIZER_INTERCEPT_NAME_TO_HANDLE_AT
  6824. INTERCEPTOR(int, name_to_handle_at, int dirfd, const char *pathname,
  6825. struct file_handle *handle, int *mount_id, int flags) {
  6826. void* ctx;
  6827. COMMON_INTERCEPTOR_ENTER(ctx, name_to_handle_at, dirfd, pathname, handle,
  6828. mount_id, flags);
  6829. COMMON_INTERCEPTOR_READ_RANGE(ctx, pathname, internal_strlen(pathname) + 1);
  6830. __sanitizer_file_handle *sanitizer_handle =
  6831. reinterpret_cast<__sanitizer_file_handle*>(handle);
  6832. COMMON_INTERCEPTOR_READ_RANGE(
  6833. ctx, &sanitizer_handle->handle_bytes,
  6834. sizeof(sanitizer_handle->handle_bytes));
  6835. int res = REAL(name_to_handle_at)(dirfd, pathname, handle, mount_id, flags);
  6836. if (!res) {
  6837. COMMON_INTERCEPTOR_WRITE_RANGE(
  6838. ctx, &sanitizer_handle->handle_bytes,
  6839. sizeof(sanitizer_handle->handle_bytes));
  6840. COMMON_INTERCEPTOR_WRITE_RANGE(
  6841. ctx, &sanitizer_handle->handle_type,
  6842. sizeof(sanitizer_handle->handle_type));
  6843. COMMON_INTERCEPTOR_WRITE_RANGE(
  6844. ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
  6845. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mount_id, sizeof(*mount_id));
  6846. }
  6847. return res;
  6848. }
  6849. #define INIT_NAME_TO_HANDLE_AT COMMON_INTERCEPT_FUNCTION(name_to_handle_at)
  6850. #else
  6851. #define INIT_NAME_TO_HANDLE_AT
  6852. #endif
  6853. #if SANITIZER_INTERCEPT_OPEN_BY_HANDLE_AT
  6854. INTERCEPTOR(int, open_by_handle_at, int mount_fd, struct file_handle* handle,
  6855. int flags) {
  6856. void* ctx;
  6857. COMMON_INTERCEPTOR_ENTER(ctx, open_by_handle_at, mount_fd, handle, flags);
  6858. __sanitizer_file_handle *sanitizer_handle =
  6859. reinterpret_cast<__sanitizer_file_handle*>(handle);
  6860. COMMON_INTERCEPTOR_READ_RANGE(
  6861. ctx, &sanitizer_handle->handle_bytes,
  6862. sizeof(sanitizer_handle->handle_bytes));
  6863. COMMON_INTERCEPTOR_READ_RANGE(
  6864. ctx, &sanitizer_handle->handle_type,
  6865. sizeof(sanitizer_handle->handle_type));
  6866. COMMON_INTERCEPTOR_READ_RANGE(
  6867. ctx, &sanitizer_handle->f_handle, sanitizer_handle->handle_bytes);
  6868. return REAL(open_by_handle_at)(mount_fd, handle, flags);
  6869. }
  6870. #define INIT_OPEN_BY_HANDLE_AT COMMON_INTERCEPT_FUNCTION(open_by_handle_at)
  6871. #else
  6872. #define INIT_OPEN_BY_HANDLE_AT
  6873. #endif
  6874. #if SANITIZER_INTERCEPT_STRLCPY
  6875. INTERCEPTOR(SIZE_T, strlcpy, char *dst, char *src, SIZE_T size) {
  6876. void *ctx;
  6877. SIZE_T res;
  6878. COMMON_INTERCEPTOR_ENTER(ctx, strlcpy, dst, src, size);
  6879. if (src) {
  6880. // Keep strnlen as macro argument, as macro may ignore it.
  6881. COMMON_INTERCEPTOR_READ_STRING(
  6882. ctx, src, Min(internal_strnlen(src, size), size - 1) + 1);
  6883. }
  6884. res = REAL(strlcpy)(dst, src, size);
  6885. COMMON_INTERCEPTOR_COPY_STRING(ctx, dst, src, internal_strlen(dst) + 1);
  6886. return res;
  6887. }
  6888. INTERCEPTOR(SIZE_T, strlcat, char *dst, char *src, SIZE_T size) {
  6889. void *ctx;
  6890. SIZE_T len = 0;
  6891. COMMON_INTERCEPTOR_ENTER(ctx, strlcat, dst, src, size);
  6892. // src is checked in the strlcpy() interceptor
  6893. if (dst) {
  6894. len = internal_strnlen(dst, size);
  6895. COMMON_INTERCEPTOR_READ_STRING(ctx, dst, Min(len, size - 1) + 1);
  6896. }
  6897. // Reuse the rest of the code in the strlcpy() interceptor
  6898. return WRAP(strlcpy)(dst + len, src, size - len) + len;
  6899. }
  6900. #define INIT_STRLCPY \
  6901. COMMON_INTERCEPT_FUNCTION(strlcpy); \
  6902. COMMON_INTERCEPT_FUNCTION(strlcat);
  6903. #else
  6904. #define INIT_STRLCPY
  6905. #endif
  6906. #if SANITIZER_INTERCEPT_MMAP
  6907. INTERCEPTOR(void *, mmap, void *addr, SIZE_T sz, int prot, int flags, int fd,
  6908. OFF_T off) {
  6909. void *ctx;
  6910. if (common_flags()->detect_write_exec)
  6911. ReportMmapWriteExec(prot, flags);
  6912. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  6913. return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
  6914. COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off);
  6915. COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off);
  6916. }
  6917. INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) {
  6918. void *ctx;
  6919. if (common_flags()->detect_write_exec)
  6920. ReportMmapWriteExec(prot, 0);
  6921. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  6922. return (int)internal_mprotect(addr, sz, prot);
  6923. COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot);
  6924. MprotectMallocZones(addr, prot);
  6925. return REAL(mprotect)(addr, sz, prot);
  6926. }
  6927. #define INIT_MMAP \
  6928. COMMON_INTERCEPT_FUNCTION(mmap); \
  6929. COMMON_INTERCEPT_FUNCTION(mprotect);
  6930. #else
  6931. #define INIT_MMAP
  6932. #endif
  6933. #if SANITIZER_INTERCEPT_MMAP64
  6934. INTERCEPTOR(void *, mmap64, void *addr, SIZE_T sz, int prot, int flags, int fd,
  6935. OFF64_T off) {
  6936. void *ctx;
  6937. if (common_flags()->detect_write_exec)
  6938. ReportMmapWriteExec(prot, flags);
  6939. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  6940. return (void *)internal_mmap(addr, sz, prot, flags, fd, off);
  6941. COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off);
  6942. COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap64, addr, sz, prot, flags, fd, off);
  6943. }
  6944. #define INIT_MMAP64 COMMON_INTERCEPT_FUNCTION(mmap64);
  6945. #else
  6946. #define INIT_MMAP64
  6947. #endif
  6948. #if SANITIZER_INTERCEPT_DEVNAME
  6949. INTERCEPTOR(char *, devname, u64 dev, u32 type) {
  6950. void *ctx;
  6951. char *name;
  6952. COMMON_INTERCEPTOR_ENTER(ctx, devname, dev, type);
  6953. name = REAL(devname)(dev, type);
  6954. if (name)
  6955. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
  6956. return name;
  6957. }
  6958. #define INIT_DEVNAME COMMON_INTERCEPT_FUNCTION(devname);
  6959. #else
  6960. #define INIT_DEVNAME
  6961. #endif
  6962. #if SANITIZER_INTERCEPT_DEVNAME_R
  6963. #if SANITIZER_NETBSD
  6964. #define DEVNAME_R_RETTYPE int
  6965. #define DEVNAME_R_SUCCESS(x) (!(x))
  6966. #else
  6967. #define DEVNAME_R_RETTYPE char*
  6968. #define DEVNAME_R_SUCCESS(x) (x)
  6969. #endif
  6970. INTERCEPTOR(DEVNAME_R_RETTYPE, devname_r, u64 dev, u32 type, char *path,
  6971. uptr len) {
  6972. void *ctx;
  6973. COMMON_INTERCEPTOR_ENTER(ctx, devname_r, dev, type, path, len);
  6974. DEVNAME_R_RETTYPE res = REAL(devname_r)(dev, type, path, len);
  6975. if (DEVNAME_R_SUCCESS(res))
  6976. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, path, internal_strlen(path) + 1);
  6977. return res;
  6978. }
  6979. #define INIT_DEVNAME_R COMMON_INTERCEPT_FUNCTION(devname_r);
  6980. #else
  6981. #define INIT_DEVNAME_R
  6982. #endif
  6983. #if SANITIZER_INTERCEPT_FGETLN
  6984. INTERCEPTOR(char *, fgetln, __sanitizer_FILE *stream, SIZE_T *len) {
  6985. void *ctx;
  6986. COMMON_INTERCEPTOR_ENTER(ctx, fgetln, stream, len);
  6987. char *str = REAL(fgetln)(stream, len);
  6988. if (str && len) {
  6989. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
  6990. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, str, *len);
  6991. }
  6992. return str;
  6993. }
  6994. #define INIT_FGETLN COMMON_INTERCEPT_FUNCTION(fgetln)
  6995. #else
  6996. #define INIT_FGETLN
  6997. #endif
  6998. #if SANITIZER_INTERCEPT_STRMODE
  6999. INTERCEPTOR(void, strmode, u32 mode, char *bp) {
  7000. void *ctx;
  7001. COMMON_INTERCEPTOR_ENTER(ctx, strmode, mode, bp);
  7002. REAL(strmode)(mode, bp);
  7003. if (bp)
  7004. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, bp, internal_strlen(bp) + 1);
  7005. }
  7006. #define INIT_STRMODE COMMON_INTERCEPT_FUNCTION(strmode)
  7007. #else
  7008. #define INIT_STRMODE
  7009. #endif
  7010. #if SANITIZER_INTERCEPT_TTYENT
  7011. INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
  7012. void *ctx;
  7013. COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
  7014. struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
  7015. if (ttyent)
  7016. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
  7017. return ttyent;
  7018. }
  7019. INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
  7020. void *ctx;
  7021. COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
  7022. if (name)
  7023. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  7024. struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
  7025. if (ttyent)
  7026. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
  7027. return ttyent;
  7028. }
  7029. #define INIT_TTYENT \
  7030. COMMON_INTERCEPT_FUNCTION(getttyent); \
  7031. COMMON_INTERCEPT_FUNCTION(getttynam);
  7032. #else
  7033. #define INIT_TTYENT
  7034. #endif
  7035. #if SANITIZER_INTERCEPT_TTYENTPATH
  7036. INTERCEPTOR(int, setttyentpath, char *path) {
  7037. void *ctx;
  7038. COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
  7039. if (path)
  7040. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  7041. return REAL(setttyentpath)(path);
  7042. }
  7043. #define INIT_TTYENTPATH COMMON_INTERCEPT_FUNCTION(setttyentpath);
  7044. #else
  7045. #define INIT_TTYENTPATH
  7046. #endif
  7047. #if SANITIZER_INTERCEPT_PROTOENT
  7048. static void write_protoent(void *ctx, struct __sanitizer_protoent *p) {
  7049. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
  7050. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, internal_strlen(p->p_name) + 1);
  7051. SIZE_T pp_size = 1; // One handles the trailing \0
  7052. for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
  7053. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, internal_strlen(*pp) + 1);
  7054. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
  7055. pp_size * sizeof(char **));
  7056. }
  7057. INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
  7058. void *ctx;
  7059. COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
  7060. struct __sanitizer_protoent *p = REAL(getprotoent)();
  7061. if (p)
  7062. write_protoent(ctx, p);
  7063. return p;
  7064. }
  7065. INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
  7066. void *ctx;
  7067. COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
  7068. if (name)
  7069. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  7070. struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
  7071. if (p)
  7072. write_protoent(ctx, p);
  7073. return p;
  7074. }
  7075. INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
  7076. void *ctx;
  7077. COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
  7078. struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
  7079. if (p)
  7080. write_protoent(ctx, p);
  7081. return p;
  7082. }
  7083. #define INIT_PROTOENT \
  7084. COMMON_INTERCEPT_FUNCTION(getprotoent); \
  7085. COMMON_INTERCEPT_FUNCTION(getprotobyname); \
  7086. COMMON_INTERCEPT_FUNCTION(getprotobynumber)
  7087. #else
  7088. #define INIT_PROTOENT
  7089. #endif
  7090. #if SANITIZER_INTERCEPT_PROTOENT_R
  7091. INTERCEPTOR(int, getprotoent_r, struct __sanitizer_protoent *result_buf,
  7092. char *buf, SIZE_T buflen, struct __sanitizer_protoent **result) {
  7093. void *ctx;
  7094. COMMON_INTERCEPTOR_ENTER(ctx, getprotoent_r, result_buf, buf, buflen,
  7095. result);
  7096. int res = REAL(getprotoent_r)(result_buf, buf, buflen, result);
  7097. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
  7098. if (!res && *result)
  7099. write_protoent(ctx, *result);
  7100. return res;
  7101. }
  7102. INTERCEPTOR(int, getprotobyname_r, const char *name,
  7103. struct __sanitizer_protoent *result_buf, char *buf, SIZE_T buflen,
  7104. struct __sanitizer_protoent **result) {
  7105. void *ctx;
  7106. COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname_r, name, result_buf, buf,
  7107. buflen, result);
  7108. if (name)
  7109. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  7110. int res = REAL(getprotobyname_r)(name, result_buf, buf, buflen, result);
  7111. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
  7112. if (!res && *result)
  7113. write_protoent(ctx, *result);
  7114. return res;
  7115. }
  7116. INTERCEPTOR(int, getprotobynumber_r, int num,
  7117. struct __sanitizer_protoent *result_buf, char *buf,
  7118. SIZE_T buflen, struct __sanitizer_protoent **result) {
  7119. void *ctx;
  7120. COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber_r, num, result_buf, buf,
  7121. buflen, result);
  7122. int res = REAL(getprotobynumber_r)(num, result_buf, buf, buflen, result);
  7123. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, result, sizeof *result);
  7124. if (!res && *result)
  7125. write_protoent(ctx, *result);
  7126. return res;
  7127. }
  7128. #define INIT_PROTOENT_R \
  7129. COMMON_INTERCEPT_FUNCTION(getprotoent_r); \
  7130. COMMON_INTERCEPT_FUNCTION(getprotobyname_r); \
  7131. COMMON_INTERCEPT_FUNCTION(getprotobynumber_r);
  7132. #else
  7133. #define INIT_PROTOENT_R
  7134. #endif
  7135. #if SANITIZER_INTERCEPT_NETENT
  7136. INTERCEPTOR(struct __sanitizer_netent *, getnetent) {
  7137. void *ctx;
  7138. COMMON_INTERCEPTOR_ENTER(ctx, getnetent);
  7139. struct __sanitizer_netent *n = REAL(getnetent)();
  7140. if (n) {
  7141. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
  7142. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
  7143. SIZE_T nn_size = 1; // One handles the trailing \0
  7144. for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
  7145. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
  7146. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
  7147. nn_size * sizeof(char **));
  7148. }
  7149. return n;
  7150. }
  7151. INTERCEPTOR(struct __sanitizer_netent *, getnetbyname, const char *name) {
  7152. void *ctx;
  7153. COMMON_INTERCEPTOR_ENTER(ctx, getnetbyname, name);
  7154. if (name)
  7155. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  7156. struct __sanitizer_netent *n = REAL(getnetbyname)(name);
  7157. if (n) {
  7158. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
  7159. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
  7160. SIZE_T nn_size = 1; // One handles the trailing \0
  7161. for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
  7162. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
  7163. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
  7164. nn_size * sizeof(char **));
  7165. }
  7166. return n;
  7167. }
  7168. INTERCEPTOR(struct __sanitizer_netent *, getnetbyaddr, u32 net, int type) {
  7169. void *ctx;
  7170. COMMON_INTERCEPTOR_ENTER(ctx, getnetbyaddr, net, type);
  7171. struct __sanitizer_netent *n = REAL(getnetbyaddr)(net, type);
  7172. if (n) {
  7173. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n, sizeof(*n));
  7174. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_name, internal_strlen(n->n_name) + 1);
  7175. SIZE_T nn_size = 1; // One handles the trailing \0
  7176. for (char **nn = n->n_aliases; *nn; ++nn, ++nn_size)
  7177. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *nn, internal_strlen(*nn) + 1);
  7178. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, n->n_aliases,
  7179. nn_size * sizeof(char **));
  7180. }
  7181. return n;
  7182. }
  7183. #define INIT_NETENT \
  7184. COMMON_INTERCEPT_FUNCTION(getnetent); \
  7185. COMMON_INTERCEPT_FUNCTION(getnetbyname); \
  7186. COMMON_INTERCEPT_FUNCTION(getnetbyaddr)
  7187. #else
  7188. #define INIT_NETENT
  7189. #endif
  7190. #if SANITIZER_INTERCEPT_GETMNTINFO
  7191. INTERCEPTOR(int, getmntinfo, void **mntbufp, int flags) {
  7192. void *ctx;
  7193. COMMON_INTERCEPTOR_ENTER(ctx, getmntinfo, mntbufp, flags);
  7194. int cnt = REAL(getmntinfo)(mntbufp, flags);
  7195. if (cnt > 0 && mntbufp) {
  7196. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mntbufp, sizeof(void *));
  7197. if (*mntbufp)
  7198. #if SANITIZER_NETBSD
  7199. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statvfs_sz);
  7200. #else
  7201. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *mntbufp, cnt * struct_statfs_sz);
  7202. #endif
  7203. }
  7204. return cnt;
  7205. }
  7206. #define INIT_GETMNTINFO COMMON_INTERCEPT_FUNCTION(getmntinfo)
  7207. #else
  7208. #define INIT_GETMNTINFO
  7209. #endif
  7210. #if SANITIZER_INTERCEPT_MI_VECTOR_HASH
  7211. INTERCEPTOR(void, mi_vector_hash, const void *key, SIZE_T len, u32 seed,
  7212. u32 hashes[3]) {
  7213. void *ctx;
  7214. COMMON_INTERCEPTOR_ENTER(ctx, mi_vector_hash, key, len, seed, hashes);
  7215. if (key)
  7216. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, len);
  7217. REAL(mi_vector_hash)(key, len, seed, hashes);
  7218. if (hashes)
  7219. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, hashes, sizeof(hashes[0]) * 3);
  7220. }
  7221. #define INIT_MI_VECTOR_HASH COMMON_INTERCEPT_FUNCTION(mi_vector_hash)
  7222. #else
  7223. #define INIT_MI_VECTOR_HASH
  7224. #endif
  7225. #if SANITIZER_INTERCEPT_SETVBUF
  7226. INTERCEPTOR(int, setvbuf, __sanitizer_FILE *stream, char *buf, int mode,
  7227. SIZE_T size) {
  7228. void *ctx;
  7229. COMMON_INTERCEPTOR_ENTER(ctx, setvbuf, stream, buf, mode, size);
  7230. int ret = REAL(setvbuf)(stream, buf, mode, size);
  7231. if (buf)
  7232. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
  7233. if (stream)
  7234. unpoison_file(stream);
  7235. return ret;
  7236. }
  7237. INTERCEPTOR(void, setbuf, __sanitizer_FILE *stream, char *buf) {
  7238. void *ctx;
  7239. COMMON_INTERCEPTOR_ENTER(ctx, setbuf, stream, buf);
  7240. REAL(setbuf)(stream, buf);
  7241. if (buf) {
  7242. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, __sanitizer_bufsiz);
  7243. }
  7244. if (stream)
  7245. unpoison_file(stream);
  7246. }
  7247. INTERCEPTOR(void, setbuffer, __sanitizer_FILE *stream, char *buf, SIZE_T size) {
  7248. void *ctx;
  7249. COMMON_INTERCEPTOR_ENTER(ctx, setbuffer, stream, buf, size);
  7250. REAL(setbuffer)(stream, buf, size);
  7251. if (buf) {
  7252. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, size);
  7253. }
  7254. if (stream)
  7255. unpoison_file(stream);
  7256. }
  7257. INTERCEPTOR(void, setlinebuf, __sanitizer_FILE *stream) {
  7258. void *ctx;
  7259. COMMON_INTERCEPTOR_ENTER(ctx, setlinebuf, stream);
  7260. REAL(setlinebuf)(stream);
  7261. if (stream)
  7262. unpoison_file(stream);
  7263. }
  7264. #define INIT_SETVBUF COMMON_INTERCEPT_FUNCTION(setvbuf); \
  7265. COMMON_INTERCEPT_FUNCTION(setbuf); \
  7266. COMMON_INTERCEPT_FUNCTION(setbuffer); \
  7267. COMMON_INTERCEPT_FUNCTION(setlinebuf)
  7268. #else
  7269. #define INIT_SETVBUF
  7270. #endif
  7271. #if SANITIZER_INTERCEPT_GETVFSSTAT
  7272. INTERCEPTOR(int, getvfsstat, void *buf, SIZE_T bufsize, int flags) {
  7273. void *ctx;
  7274. COMMON_INTERCEPTOR_ENTER(ctx, getvfsstat, buf, bufsize, flags);
  7275. int ret = REAL(getvfsstat)(buf, bufsize, flags);
  7276. if (buf && ret > 0)
  7277. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, ret * struct_statvfs_sz);
  7278. return ret;
  7279. }
  7280. #define INIT_GETVFSSTAT COMMON_INTERCEPT_FUNCTION(getvfsstat)
  7281. #else
  7282. #define INIT_GETVFSSTAT
  7283. #endif
  7284. #if SANITIZER_INTERCEPT_REGEX
  7285. INTERCEPTOR(int, regcomp, void *preg, const char *pattern, int cflags) {
  7286. void *ctx;
  7287. COMMON_INTERCEPTOR_ENTER(ctx, regcomp, preg, pattern, cflags);
  7288. if (pattern)
  7289. COMMON_INTERCEPTOR_READ_RANGE(ctx, pattern, internal_strlen(pattern) + 1);
  7290. int res = REAL(regcomp)(preg, pattern, cflags);
  7291. if (!res)
  7292. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, preg, struct_regex_sz);
  7293. return res;
  7294. }
  7295. INTERCEPTOR(int, regexec, const void *preg, const char *string, SIZE_T nmatch,
  7296. struct __sanitizer_regmatch *pmatch[], int eflags) {
  7297. void *ctx;
  7298. COMMON_INTERCEPTOR_ENTER(ctx, regexec, preg, string, nmatch, pmatch, eflags);
  7299. if (preg)
  7300. COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
  7301. if (string)
  7302. COMMON_INTERCEPTOR_READ_RANGE(ctx, string, internal_strlen(string) + 1);
  7303. int res = REAL(regexec)(preg, string, nmatch, pmatch, eflags);
  7304. if (!res && pmatch)
  7305. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pmatch, nmatch * struct_regmatch_sz);
  7306. return res;
  7307. }
  7308. INTERCEPTOR(SIZE_T, regerror, int errcode, const void *preg, char *errbuf,
  7309. SIZE_T errbuf_size) {
  7310. void *ctx;
  7311. COMMON_INTERCEPTOR_ENTER(ctx, regerror, errcode, preg, errbuf, errbuf_size);
  7312. if (preg)
  7313. COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
  7314. SIZE_T res = REAL(regerror)(errcode, preg, errbuf, errbuf_size);
  7315. if (errbuf)
  7316. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errbuf, internal_strlen(errbuf) + 1);
  7317. return res;
  7318. }
  7319. INTERCEPTOR(void, regfree, const void *preg) {
  7320. void *ctx;
  7321. COMMON_INTERCEPTOR_ENTER(ctx, regfree, preg);
  7322. if (preg)
  7323. COMMON_INTERCEPTOR_READ_RANGE(ctx, preg, struct_regex_sz);
  7324. REAL(regfree)(preg);
  7325. }
  7326. #define INIT_REGEX \
  7327. COMMON_INTERCEPT_FUNCTION(regcomp); \
  7328. COMMON_INTERCEPT_FUNCTION_GLIBC_VER_MIN(regexec, "GLIBC_2.3.4"); \
  7329. COMMON_INTERCEPT_FUNCTION(regerror); \
  7330. COMMON_INTERCEPT_FUNCTION(regfree);
  7331. #else
  7332. #define INIT_REGEX
  7333. #endif
  7334. #if SANITIZER_INTERCEPT_REGEXSUB
  7335. INTERCEPTOR(SSIZE_T, regnsub, char *buf, SIZE_T bufsiz, const char *sub,
  7336. const struct __sanitizer_regmatch *rm, const char *str) {
  7337. void *ctx;
  7338. COMMON_INTERCEPTOR_ENTER(ctx, regnsub, buf, bufsiz, sub, rm, str);
  7339. if (sub)
  7340. COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
  7341. // The implementation demands and hardcodes 10 elements
  7342. if (rm)
  7343. COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
  7344. if (str)
  7345. COMMON_INTERCEPTOR_READ_RANGE(ctx, str, internal_strlen(str) + 1);
  7346. SSIZE_T res = REAL(regnsub)(buf, bufsiz, sub, rm, str);
  7347. if (res > 0 && buf)
  7348. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
  7349. return res;
  7350. }
  7351. INTERCEPTOR(SSIZE_T, regasub, char **buf, const char *sub,
  7352. const struct __sanitizer_regmatch *rm, const char *sstr) {
  7353. void *ctx;
  7354. COMMON_INTERCEPTOR_ENTER(ctx, regasub, buf, sub, rm, sstr);
  7355. if (sub)
  7356. COMMON_INTERCEPTOR_READ_RANGE(ctx, sub, internal_strlen(sub) + 1);
  7357. // Hardcode 10 elements as this is hardcoded size
  7358. if (rm)
  7359. COMMON_INTERCEPTOR_READ_RANGE(ctx, rm, 10 * struct_regmatch_sz);
  7360. if (sstr)
  7361. COMMON_INTERCEPTOR_READ_RANGE(ctx, sstr, internal_strlen(sstr) + 1);
  7362. SSIZE_T res = REAL(regasub)(buf, sub, rm, sstr);
  7363. if (res > 0 && buf) {
  7364. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, sizeof(char *));
  7365. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *buf, internal_strlen(*buf) + 1);
  7366. }
  7367. return res;
  7368. }
  7369. #define INIT_REGEXSUB \
  7370. COMMON_INTERCEPT_FUNCTION(regnsub); \
  7371. COMMON_INTERCEPT_FUNCTION(regasub);
  7372. #else
  7373. #define INIT_REGEXSUB
  7374. #endif
  7375. #if SANITIZER_INTERCEPT_FTS
  7376. INTERCEPTOR(void *, fts_open, char *const *path_argv, int options,
  7377. int (*compar)(void **, void **)) {
  7378. void *ctx;
  7379. COMMON_INTERCEPTOR_ENTER(ctx, fts_open, path_argv, options, compar);
  7380. if (path_argv) {
  7381. for (char *const *pa = path_argv; ; ++pa) {
  7382. COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
  7383. if (!*pa)
  7384. break;
  7385. COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
  7386. }
  7387. }
  7388. // TODO(kamil): handle compar callback
  7389. void *fts = REAL(fts_open)(path_argv, options, compar);
  7390. if (fts)
  7391. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, fts, struct_FTS_sz);
  7392. return fts;
  7393. }
  7394. INTERCEPTOR(void *, fts_read, void *ftsp) {
  7395. void *ctx;
  7396. COMMON_INTERCEPTOR_ENTER(ctx, fts_read, ftsp);
  7397. if (ftsp)
  7398. COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  7399. void *ftsent = REAL(fts_read)(ftsp);
  7400. if (ftsent)
  7401. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
  7402. return ftsent;
  7403. }
  7404. INTERCEPTOR(void *, fts_children, void *ftsp, int options) {
  7405. void *ctx;
  7406. COMMON_INTERCEPTOR_ENTER(ctx, fts_children, ftsp, options);
  7407. if (ftsp)
  7408. COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  7409. void *ftsent = REAL(fts_children)(ftsp, options);
  7410. if (ftsent)
  7411. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ftsent, struct_FTSENT_sz);
  7412. return ftsent;
  7413. }
  7414. INTERCEPTOR(int, fts_set, void *ftsp, void *f, int options) {
  7415. void *ctx;
  7416. COMMON_INTERCEPTOR_ENTER(ctx, fts_set, ftsp, f, options);
  7417. if (ftsp)
  7418. COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  7419. if (f)
  7420. COMMON_INTERCEPTOR_READ_RANGE(ctx, f, struct_FTSENT_sz);
  7421. return REAL(fts_set)(ftsp, f, options);
  7422. }
  7423. INTERCEPTOR(int, fts_close, void *ftsp) {
  7424. void *ctx;
  7425. COMMON_INTERCEPTOR_ENTER(ctx, fts_close, ftsp);
  7426. if (ftsp)
  7427. COMMON_INTERCEPTOR_READ_RANGE(ctx, ftsp, struct_FTS_sz);
  7428. return REAL(fts_close)(ftsp);
  7429. }
  7430. #define INIT_FTS \
  7431. COMMON_INTERCEPT_FUNCTION(fts_open); \
  7432. COMMON_INTERCEPT_FUNCTION(fts_read); \
  7433. COMMON_INTERCEPT_FUNCTION(fts_children); \
  7434. COMMON_INTERCEPT_FUNCTION(fts_set); \
  7435. COMMON_INTERCEPT_FUNCTION(fts_close);
  7436. #else
  7437. #define INIT_FTS
  7438. #endif
  7439. #if SANITIZER_INTERCEPT_SYSCTL
  7440. INTERCEPTOR(int, sysctl, int *name, unsigned int namelen, void *oldp,
  7441. SIZE_T *oldlenp, void *newp, SIZE_T newlen) {
  7442. void *ctx;
  7443. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  7444. return internal_sysctl(name, namelen, oldp, oldlenp, newp, newlen);
  7445. COMMON_INTERCEPTOR_ENTER(ctx, sysctl, name, namelen, oldp, oldlenp, newp,
  7446. newlen);
  7447. if (name)
  7448. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, namelen * sizeof(*name));
  7449. if (oldlenp)
  7450. COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
  7451. if (newp && newlen)
  7452. COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
  7453. int res = REAL(sysctl)(name, namelen, oldp, oldlenp, newp, newlen);
  7454. if (!res) {
  7455. if (oldlenp) {
  7456. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
  7457. if (oldp)
  7458. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
  7459. }
  7460. }
  7461. return res;
  7462. }
  7463. INTERCEPTOR(int, sysctlbyname, char *sname, void *oldp, SIZE_T *oldlenp,
  7464. void *newp, SIZE_T newlen) {
  7465. void *ctx;
  7466. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  7467. return internal_sysctlbyname(sname, oldp, oldlenp, newp, newlen);
  7468. COMMON_INTERCEPTOR_ENTER(ctx, sysctlbyname, sname, oldp, oldlenp, newp,
  7469. newlen);
  7470. if (sname)
  7471. COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  7472. if (oldlenp)
  7473. COMMON_INTERCEPTOR_READ_RANGE(ctx, oldlenp, sizeof(*oldlenp));
  7474. if (newp && newlen)
  7475. COMMON_INTERCEPTOR_READ_RANGE(ctx, newp, newlen);
  7476. int res = REAL(sysctlbyname)(sname, oldp, oldlenp, newp, newlen);
  7477. if (!res) {
  7478. if (oldlenp) {
  7479. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldlenp, sizeof(*oldlenp));
  7480. if (oldp)
  7481. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oldp, *oldlenp);
  7482. }
  7483. }
  7484. return res;
  7485. }
  7486. INTERCEPTOR(int, sysctlnametomib, const char *sname, int *name,
  7487. SIZE_T *namelenp) {
  7488. void *ctx;
  7489. COMMON_INTERCEPTOR_ENTER(ctx, sysctlnametomib, sname, name, namelenp);
  7490. if (sname)
  7491. COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  7492. if (namelenp)
  7493. COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
  7494. int res = REAL(sysctlnametomib)(sname, name, namelenp);
  7495. if (!res) {
  7496. if (namelenp) {
  7497. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
  7498. if (name)
  7499. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
  7500. }
  7501. }
  7502. return res;
  7503. }
  7504. #define INIT_SYSCTL \
  7505. COMMON_INTERCEPT_FUNCTION(sysctl); \
  7506. COMMON_INTERCEPT_FUNCTION(sysctlbyname); \
  7507. COMMON_INTERCEPT_FUNCTION(sysctlnametomib);
  7508. #else
  7509. #define INIT_SYSCTL
  7510. #endif
  7511. #if SANITIZER_INTERCEPT_ASYSCTL
  7512. INTERCEPTOR(void *, asysctl, const int *name, SIZE_T namelen, SIZE_T *len) {
  7513. void *ctx;
  7514. COMMON_INTERCEPTOR_ENTER(ctx, asysctl, name, namelen, len);
  7515. if (name)
  7516. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, sizeof(*name) * namelen);
  7517. void *res = REAL(asysctl)(name, namelen, len);
  7518. if (res && len) {
  7519. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
  7520. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
  7521. }
  7522. return res;
  7523. }
  7524. INTERCEPTOR(void *, asysctlbyname, const char *sname, SIZE_T *len) {
  7525. void *ctx;
  7526. COMMON_INTERCEPTOR_ENTER(ctx, asysctlbyname, sname, len);
  7527. if (sname)
  7528. COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  7529. void *res = REAL(asysctlbyname)(sname, len);
  7530. if (res && len) {
  7531. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
  7532. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, *len);
  7533. }
  7534. return res;
  7535. }
  7536. #define INIT_ASYSCTL \
  7537. COMMON_INTERCEPT_FUNCTION(asysctl); \
  7538. COMMON_INTERCEPT_FUNCTION(asysctlbyname);
  7539. #else
  7540. #define INIT_ASYSCTL
  7541. #endif
  7542. #if SANITIZER_INTERCEPT_SYSCTLGETMIBINFO
  7543. INTERCEPTOR(int, sysctlgetmibinfo, char *sname, int *name,
  7544. unsigned int *namelenp, char *cname, SIZE_T *csz, void **rnode,
  7545. int v) {
  7546. void *ctx;
  7547. COMMON_INTERCEPTOR_ENTER(ctx, sysctlgetmibinfo, sname, name, namelenp, cname,
  7548. csz, rnode, v);
  7549. if (sname)
  7550. COMMON_INTERCEPTOR_READ_RANGE(ctx, sname, internal_strlen(sname) + 1);
  7551. if (namelenp)
  7552. COMMON_INTERCEPTOR_READ_RANGE(ctx, namelenp, sizeof(*namelenp));
  7553. if (csz)
  7554. COMMON_INTERCEPTOR_READ_RANGE(ctx, csz, sizeof(*csz));
  7555. // Skip rnode, it's rarely used and not trivial to sanitize
  7556. // It's also used mostly internally
  7557. int res = REAL(sysctlgetmibinfo)(sname, name, namelenp, cname, csz, rnode, v);
  7558. if (!res) {
  7559. if (namelenp) {
  7560. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, namelenp, sizeof(*namelenp));
  7561. if (name)
  7562. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, *namelenp * sizeof(*name));
  7563. }
  7564. if (csz) {
  7565. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, csz, sizeof(*csz));
  7566. if (cname)
  7567. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cname, *csz);
  7568. }
  7569. }
  7570. return res;
  7571. }
  7572. #define INIT_SYSCTLGETMIBINFO \
  7573. COMMON_INTERCEPT_FUNCTION(sysctlgetmibinfo);
  7574. #else
  7575. #define INIT_SYSCTLGETMIBINFO
  7576. #endif
  7577. #if SANITIZER_INTERCEPT_NL_LANGINFO
  7578. INTERCEPTOR(char *, nl_langinfo, long item) {
  7579. void *ctx;
  7580. COMMON_INTERCEPTOR_ENTER(ctx, nl_langinfo, item);
  7581. char *ret = REAL(nl_langinfo)(item);
  7582. if (ret)
  7583. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
  7584. return ret;
  7585. }
  7586. #define INIT_NL_LANGINFO COMMON_INTERCEPT_FUNCTION(nl_langinfo)
  7587. #else
  7588. #define INIT_NL_LANGINFO
  7589. #endif
  7590. #if SANITIZER_INTERCEPT_MODCTL
  7591. INTERCEPTOR(int, modctl, int operation, void *argp) {
  7592. void *ctx;
  7593. int ret;
  7594. COMMON_INTERCEPTOR_ENTER(ctx, modctl, operation, argp);
  7595. if (operation == modctl_load) {
  7596. if (argp) {
  7597. __sanitizer_modctl_load_t *ml = (__sanitizer_modctl_load_t *)argp;
  7598. COMMON_INTERCEPTOR_READ_RANGE(ctx, ml, sizeof(*ml));
  7599. if (ml->ml_filename)
  7600. COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_filename,
  7601. internal_strlen(ml->ml_filename) + 1);
  7602. if (ml->ml_props)
  7603. COMMON_INTERCEPTOR_READ_RANGE(ctx, ml->ml_props, ml->ml_propslen);
  7604. }
  7605. ret = REAL(modctl)(operation, argp);
  7606. } else if (operation == modctl_unload) {
  7607. if (argp) {
  7608. const char *name = (const char *)argp;
  7609. COMMON_INTERCEPTOR_READ_RANGE(ctx, name, internal_strlen(name) + 1);
  7610. }
  7611. ret = REAL(modctl)(operation, argp);
  7612. } else if (operation == modctl_stat) {
  7613. uptr iov_len;
  7614. struct __sanitizer_iovec *iov = (struct __sanitizer_iovec *)argp;
  7615. if (iov) {
  7616. COMMON_INTERCEPTOR_READ_RANGE(ctx, iov, sizeof(*iov));
  7617. iov_len = iov->iov_len;
  7618. }
  7619. ret = REAL(modctl)(operation, argp);
  7620. if (iov)
  7621. COMMON_INTERCEPTOR_WRITE_RANGE(
  7622. ctx, iov->iov_base, Min(iov_len, iov->iov_len));
  7623. } else if (operation == modctl_exists) {
  7624. ret = REAL(modctl)(operation, argp);
  7625. } else {
  7626. ret = REAL(modctl)(operation, argp);
  7627. }
  7628. return ret;
  7629. }
  7630. #define INIT_MODCTL COMMON_INTERCEPT_FUNCTION(modctl)
  7631. #else
  7632. #define INIT_MODCTL
  7633. #endif
  7634. #if SANITIZER_INTERCEPT_STRTONUM
  7635. INTERCEPTOR(long long, strtonum, const char *nptr, long long minval,
  7636. long long maxval, const char **errstr) {
  7637. void *ctx;
  7638. COMMON_INTERCEPTOR_ENTER(ctx, strtonum, nptr, minval, maxval, errstr);
  7639. // TODO(kamil): Implement strtoll as a common inteceptor
  7640. char *real_endptr;
  7641. long long ret = (long long)REAL(strtoimax)(nptr, &real_endptr, 10);
  7642. StrtolFixAndCheck(ctx, nptr, nullptr, real_endptr, 10);
  7643. ret = REAL(strtonum)(nptr, minval, maxval, errstr);
  7644. if (errstr) {
  7645. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, errstr, sizeof(const char *));
  7646. if (*errstr)
  7647. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *errstr, internal_strlen(*errstr) + 1);
  7648. }
  7649. return ret;
  7650. }
  7651. #define INIT_STRTONUM COMMON_INTERCEPT_FUNCTION(strtonum)
  7652. #else
  7653. #define INIT_STRTONUM
  7654. #endif
  7655. #if SANITIZER_INTERCEPT_FPARSELN
  7656. INTERCEPTOR(char *, fparseln, __sanitizer_FILE *stream, SIZE_T *len,
  7657. SIZE_T *lineno, const char delim[3], int flags) {
  7658. void *ctx;
  7659. COMMON_INTERCEPTOR_ENTER(ctx, fparseln, stream, len, lineno, delim, flags);
  7660. if (lineno)
  7661. COMMON_INTERCEPTOR_READ_RANGE(ctx, lineno, sizeof(*lineno));
  7662. if (delim)
  7663. COMMON_INTERCEPTOR_READ_RANGE(ctx, delim, sizeof(delim[0]) * 3);
  7664. char *ret = REAL(fparseln)(stream, len, lineno, delim, flags);
  7665. if (ret) {
  7666. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, internal_strlen(ret) + 1);
  7667. if (len)
  7668. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, len, sizeof(*len));
  7669. if (lineno)
  7670. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, lineno, sizeof(*lineno));
  7671. }
  7672. return ret;
  7673. }
  7674. #define INIT_FPARSELN COMMON_INTERCEPT_FUNCTION(fparseln)
  7675. #else
  7676. #define INIT_FPARSELN
  7677. #endif
  7678. #if SANITIZER_INTERCEPT_STATVFS1
  7679. INTERCEPTOR(int, statvfs1, const char *path, void *buf, int flags) {
  7680. void *ctx;
  7681. COMMON_INTERCEPTOR_ENTER(ctx, statvfs1, path, buf, flags);
  7682. if (path) COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  7683. int res = REAL(statvfs1)(path, buf, flags);
  7684. if (!res) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
  7685. return res;
  7686. }
  7687. INTERCEPTOR(int, fstatvfs1, int fd, void *buf, int flags) {
  7688. void *ctx;
  7689. COMMON_INTERCEPTOR_ENTER(ctx, fstatvfs1, fd, buf, flags);
  7690. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  7691. int res = REAL(fstatvfs1)(fd, buf, flags);
  7692. if (!res) {
  7693. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, struct_statvfs_sz);
  7694. if (fd >= 0)
  7695. COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  7696. }
  7697. return res;
  7698. }
  7699. #define INIT_STATVFS1 \
  7700. COMMON_INTERCEPT_FUNCTION(statvfs1); \
  7701. COMMON_INTERCEPT_FUNCTION(fstatvfs1);
  7702. #else
  7703. #define INIT_STATVFS1
  7704. #endif
  7705. #if SANITIZER_INTERCEPT_STRTOI
  7706. INTERCEPTOR(INTMAX_T, strtoi, const char *nptr, char **endptr, int base,
  7707. INTMAX_T low, INTMAX_T high, int *rstatus) {
  7708. void *ctx;
  7709. COMMON_INTERCEPTOR_ENTER(ctx, strtoi, nptr, endptr, base, low, high, rstatus);
  7710. char *real_endptr;
  7711. INTMAX_T ret = REAL(strtoi)(nptr, &real_endptr, base, low, high, rstatus);
  7712. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  7713. if (rstatus)
  7714. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
  7715. return ret;
  7716. }
  7717. INTERCEPTOR(UINTMAX_T, strtou, const char *nptr, char **endptr, int base,
  7718. UINTMAX_T low, UINTMAX_T high, int *rstatus) {
  7719. void *ctx;
  7720. COMMON_INTERCEPTOR_ENTER(ctx, strtou, nptr, endptr, base, low, high, rstatus);
  7721. char *real_endptr;
  7722. UINTMAX_T ret = REAL(strtou)(nptr, &real_endptr, base, low, high, rstatus);
  7723. StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base);
  7724. if (rstatus)
  7725. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rstatus, sizeof(*rstatus));
  7726. return ret;
  7727. }
  7728. #define INIT_STRTOI \
  7729. COMMON_INTERCEPT_FUNCTION(strtoi); \
  7730. COMMON_INTERCEPT_FUNCTION(strtou)
  7731. #else
  7732. #define INIT_STRTOI
  7733. #endif
  7734. #if SANITIZER_INTERCEPT_CAPSICUM
  7735. #define CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights, ...) \
  7736. { \
  7737. void *ctx; \
  7738. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_init, rights, ##__VA_ARGS__); \
  7739. if (rights) \
  7740. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
  7741. __sanitizer_cap_rights_t *ret = \
  7742. REAL(cap_rights_init)(rights, ##__VA_ARGS__); \
  7743. if (ret) \
  7744. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
  7745. return ret; \
  7746. }
  7747. #define CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights, ...) \
  7748. { \
  7749. void *ctx; \
  7750. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_set, rights, ##__VA_ARGS__); \
  7751. if (rights) \
  7752. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
  7753. __sanitizer_cap_rights_t *ret = \
  7754. REAL(cap_rights_set)(rights, ##__VA_ARGS__); \
  7755. if (ret) \
  7756. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
  7757. return ret; \
  7758. }
  7759. #define CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights, ...) \
  7760. { \
  7761. void *ctx; \
  7762. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_clear, rights, ##__VA_ARGS__); \
  7763. if (rights) \
  7764. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
  7765. __sanitizer_cap_rights_t *ret = \
  7766. REAL(cap_rights_clear)(rights, ##__VA_ARGS__); \
  7767. if (ret) \
  7768. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret)); \
  7769. return ret; \
  7770. }
  7771. #define CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights, ...) \
  7772. { \
  7773. void *ctx; \
  7774. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_set, rights, ##__VA_ARGS__); \
  7775. if (rights) \
  7776. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights)); \
  7777. return REAL(cap_rights_is_set)(rights, ##__VA_ARGS__); \
  7778. }
  7779. INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_init,
  7780. __sanitizer_cap_rights_t *rights) {
  7781. CAP_RIGHTS_INIT_INTERCEPTOR(cap_rights_init, rights);
  7782. }
  7783. INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_set,
  7784. __sanitizer_cap_rights_t *rights) {
  7785. CAP_RIGHTS_SET_INTERCEPTOR(cap_rights_set, rights);
  7786. }
  7787. INTERCEPTOR(__sanitizer_cap_rights_t *, cap_rights_clear,
  7788. __sanitizer_cap_rights_t *rights) {
  7789. CAP_RIGHTS_CLEAR_INTERCEPTOR(cap_rights_clear, rights);
  7790. }
  7791. INTERCEPTOR(bool, cap_rights_is_set,
  7792. __sanitizer_cap_rights_t *rights) {
  7793. CAP_RIGHTS_IS_SET_INTERCEPTOR(cap_rights_is_set, rights);
  7794. }
  7795. INTERCEPTOR(int, cap_rights_limit, int fd,
  7796. const __sanitizer_cap_rights_t *rights) {
  7797. void *ctx;
  7798. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_limit, fd, rights);
  7799. if (rights)
  7800. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
  7801. return REAL(cap_rights_limit)(fd, rights);
  7802. }
  7803. INTERCEPTOR(int, cap_rights_get, int fd, __sanitizer_cap_rights_t *rights) {
  7804. void *ctx;
  7805. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_get, fd, rights);
  7806. int ret = REAL(cap_rights_get)(fd, rights);
  7807. if (!ret && rights)
  7808. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, rights, sizeof(*rights));
  7809. return ret;
  7810. }
  7811. INTERCEPTOR(bool, cap_rights_is_valid, const __sanitizer_cap_rights_t *rights) {
  7812. void *ctx;
  7813. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_is_valid, rights);
  7814. if (rights)
  7815. COMMON_INTERCEPTOR_READ_RANGE(ctx, rights, sizeof(*rights));
  7816. return REAL(cap_rights_is_valid(rights));
  7817. }
  7818. INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_merge,
  7819. __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
  7820. void *ctx;
  7821. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_merge, dst, src);
  7822. if (src)
  7823. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  7824. __sanitizer_cap_rights *ret = REAL(cap_rights_merge)(dst, src);
  7825. if (dst)
  7826. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
  7827. return ret;
  7828. }
  7829. INTERCEPTOR(__sanitizer_cap_rights *, cap_rights_remove,
  7830. __sanitizer_cap_rights *dst, const __sanitizer_cap_rights *src) {
  7831. void *ctx;
  7832. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_remove, dst, src);
  7833. if (src)
  7834. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, sizeof(*src));
  7835. __sanitizer_cap_rights *ret = REAL(cap_rights_remove)(dst, src);
  7836. if (dst)
  7837. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(*dst));
  7838. return ret;
  7839. }
  7840. INTERCEPTOR(bool, cap_rights_contains, const __sanitizer_cap_rights *big,
  7841. const __sanitizer_cap_rights *little) {
  7842. void *ctx;
  7843. COMMON_INTERCEPTOR_ENTER(ctx, cap_rights_contains, big, little);
  7844. if (little)
  7845. COMMON_INTERCEPTOR_READ_RANGE(ctx, little, sizeof(*little));
  7846. if (big)
  7847. COMMON_INTERCEPTOR_READ_RANGE(ctx, big, sizeof(*big));
  7848. return REAL(cap_rights_contains)(big, little);
  7849. }
  7850. INTERCEPTOR(int, cap_ioctls_limit, int fd, const uptr *cmds, SIZE_T ncmds) {
  7851. void *ctx;
  7852. COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_limit, fd, cmds, ncmds);
  7853. if (cmds)
  7854. COMMON_INTERCEPTOR_READ_RANGE(ctx, cmds, sizeof(*cmds) * ncmds);
  7855. return REAL(cap_ioctls_limit)(fd, cmds, ncmds);
  7856. }
  7857. INTERCEPTOR(int, cap_ioctls_get, int fd, uptr *cmds, SIZE_T maxcmds) {
  7858. void *ctx;
  7859. COMMON_INTERCEPTOR_ENTER(ctx, cap_ioctls_get, fd, cmds, maxcmds);
  7860. int ret = REAL(cap_ioctls_get)(fd, cmds, maxcmds);
  7861. if (!ret && cmds)
  7862. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cmds, sizeof(*cmds) * maxcmds);
  7863. return ret;
  7864. }
  7865. #define INIT_CAPSICUM \
  7866. COMMON_INTERCEPT_FUNCTION(cap_rights_init); \
  7867. COMMON_INTERCEPT_FUNCTION(cap_rights_set); \
  7868. COMMON_INTERCEPT_FUNCTION(cap_rights_clear); \
  7869. COMMON_INTERCEPT_FUNCTION(cap_rights_is_set); \
  7870. COMMON_INTERCEPT_FUNCTION(cap_rights_get); \
  7871. COMMON_INTERCEPT_FUNCTION(cap_rights_limit); \
  7872. COMMON_INTERCEPT_FUNCTION(cap_rights_contains); \
  7873. COMMON_INTERCEPT_FUNCTION(cap_rights_remove); \
  7874. COMMON_INTERCEPT_FUNCTION(cap_rights_merge); \
  7875. COMMON_INTERCEPT_FUNCTION(cap_rights_is_valid); \
  7876. COMMON_INTERCEPT_FUNCTION(cap_ioctls_get); \
  7877. COMMON_INTERCEPT_FUNCTION(cap_ioctls_limit)
  7878. #else
  7879. #define INIT_CAPSICUM
  7880. #endif
  7881. #if SANITIZER_INTERCEPT_SHA1
  7882. INTERCEPTOR(void, SHA1Init, void *context) {
  7883. void *ctx;
  7884. COMMON_INTERCEPTOR_ENTER(ctx, SHA1Init, context);
  7885. REAL(SHA1Init)(context);
  7886. if (context)
  7887. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
  7888. }
  7889. INTERCEPTOR(void, SHA1Update, void *context, const u8 *data, unsigned len) {
  7890. void *ctx;
  7891. COMMON_INTERCEPTOR_ENTER(ctx, SHA1Update, context, data, len);
  7892. if (data && len > 0)
  7893. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  7894. if (context)
  7895. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
  7896. REAL(SHA1Update)(context, data, len);
  7897. if (context)
  7898. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA1_CTX_sz);
  7899. }
  7900. INTERCEPTOR(void, SHA1Final, u8 digest[20], void *context) {
  7901. void *ctx;
  7902. COMMON_INTERCEPTOR_ENTER(ctx, SHA1Final, digest, context);
  7903. if (context)
  7904. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
  7905. REAL(SHA1Final)(digest, context);
  7906. if (digest)
  7907. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
  7908. }
  7909. INTERCEPTOR(void, SHA1Transform, u32 state[5], u8 buffer[64]) {
  7910. void *ctx;
  7911. COMMON_INTERCEPTOR_ENTER(ctx, SHA1Transform, state, buffer);
  7912. if (state)
  7913. COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
  7914. if (buffer)
  7915. COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u8) * 64);
  7916. REAL(SHA1Transform)(state, buffer);
  7917. if (state)
  7918. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
  7919. }
  7920. INTERCEPTOR(char *, SHA1End, void *context, char *buf) {
  7921. void *ctx;
  7922. COMMON_INTERCEPTOR_ENTER(ctx, SHA1End, context, buf);
  7923. if (context)
  7924. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA1_CTX_sz);
  7925. char *ret = REAL(SHA1End)(context, buf);
  7926. if (ret)
  7927. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  7928. return ret;
  7929. }
  7930. INTERCEPTOR(char *, SHA1File, char *filename, char *buf) {
  7931. void *ctx;
  7932. COMMON_INTERCEPTOR_ENTER(ctx, SHA1File, filename, buf);
  7933. if (filename)
  7934. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  7935. char *ret = REAL(SHA1File)(filename, buf);
  7936. if (ret)
  7937. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  7938. return ret;
  7939. }
  7940. INTERCEPTOR(char *, SHA1FileChunk, char *filename, char *buf, OFF_T offset,
  7941. OFF_T length) {
  7942. void *ctx;
  7943. COMMON_INTERCEPTOR_ENTER(ctx, SHA1FileChunk, filename, buf, offset, length);
  7944. if (filename)
  7945. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  7946. char *ret = REAL(SHA1FileChunk)(filename, buf, offset, length);
  7947. if (ret)
  7948. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  7949. return ret;
  7950. }
  7951. INTERCEPTOR(char *, SHA1Data, u8 *data, SIZE_T len, char *buf) {
  7952. void *ctx;
  7953. COMMON_INTERCEPTOR_ENTER(ctx, SHA1Data, data, len, buf);
  7954. if (data)
  7955. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  7956. char *ret = REAL(SHA1Data)(data, len, buf);
  7957. if (ret)
  7958. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA1_return_length);
  7959. return ret;
  7960. }
  7961. #define INIT_SHA1 \
  7962. COMMON_INTERCEPT_FUNCTION(SHA1Init); \
  7963. COMMON_INTERCEPT_FUNCTION(SHA1Update); \
  7964. COMMON_INTERCEPT_FUNCTION(SHA1Final); \
  7965. COMMON_INTERCEPT_FUNCTION(SHA1Transform); \
  7966. COMMON_INTERCEPT_FUNCTION(SHA1End); \
  7967. COMMON_INTERCEPT_FUNCTION(SHA1File); \
  7968. COMMON_INTERCEPT_FUNCTION(SHA1FileChunk); \
  7969. COMMON_INTERCEPT_FUNCTION(SHA1Data)
  7970. #else
  7971. #define INIT_SHA1
  7972. #endif
  7973. #if SANITIZER_INTERCEPT_MD4
  7974. INTERCEPTOR(void, MD4Init, void *context) {
  7975. void *ctx;
  7976. COMMON_INTERCEPTOR_ENTER(ctx, MD4Init, context);
  7977. REAL(MD4Init)(context);
  7978. if (context)
  7979. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
  7980. }
  7981. INTERCEPTOR(void, MD4Update, void *context, const unsigned char *data,
  7982. unsigned int len) {
  7983. void *ctx;
  7984. COMMON_INTERCEPTOR_ENTER(ctx, MD4Update, context, data, len);
  7985. if (data && len > 0)
  7986. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  7987. if (context)
  7988. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
  7989. REAL(MD4Update)(context, data, len);
  7990. if (context)
  7991. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD4_CTX_sz);
  7992. }
  7993. INTERCEPTOR(void, MD4Final, unsigned char digest[16], void *context) {
  7994. void *ctx;
  7995. COMMON_INTERCEPTOR_ENTER(ctx, MD4Final, digest, context);
  7996. if (context)
  7997. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
  7998. REAL(MD4Final)(digest, context);
  7999. if (digest)
  8000. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
  8001. }
  8002. INTERCEPTOR(char *, MD4End, void *context, char *buf) {
  8003. void *ctx;
  8004. COMMON_INTERCEPTOR_ENTER(ctx, MD4End, context, buf);
  8005. if (context)
  8006. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD4_CTX_sz);
  8007. char *ret = REAL(MD4End)(context, buf);
  8008. if (ret)
  8009. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
  8010. return ret;
  8011. }
  8012. INTERCEPTOR(char *, MD4File, const char *filename, char *buf) {
  8013. void *ctx;
  8014. COMMON_INTERCEPTOR_ENTER(ctx, MD4File, filename, buf);
  8015. if (filename)
  8016. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8017. char *ret = REAL(MD4File)(filename, buf);
  8018. if (ret)
  8019. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
  8020. return ret;
  8021. }
  8022. INTERCEPTOR(char *, MD4Data, const unsigned char *data, unsigned int len,
  8023. char *buf) {
  8024. void *ctx;
  8025. COMMON_INTERCEPTOR_ENTER(ctx, MD4Data, data, len, buf);
  8026. if (data && len > 0)
  8027. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8028. char *ret = REAL(MD4Data)(data, len, buf);
  8029. if (ret)
  8030. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD4_return_length);
  8031. return ret;
  8032. }
  8033. #define INIT_MD4 \
  8034. COMMON_INTERCEPT_FUNCTION(MD4Init); \
  8035. COMMON_INTERCEPT_FUNCTION(MD4Update); \
  8036. COMMON_INTERCEPT_FUNCTION(MD4Final); \
  8037. COMMON_INTERCEPT_FUNCTION(MD4End); \
  8038. COMMON_INTERCEPT_FUNCTION(MD4File); \
  8039. COMMON_INTERCEPT_FUNCTION(MD4Data)
  8040. #else
  8041. #define INIT_MD4
  8042. #endif
  8043. #if SANITIZER_INTERCEPT_RMD160
  8044. INTERCEPTOR(void, RMD160Init, void *context) {
  8045. void *ctx;
  8046. COMMON_INTERCEPTOR_ENTER(ctx, RMD160Init, context);
  8047. REAL(RMD160Init)(context);
  8048. if (context)
  8049. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
  8050. }
  8051. INTERCEPTOR(void, RMD160Update, void *context, const u8 *data, unsigned len) {
  8052. void *ctx;
  8053. COMMON_INTERCEPTOR_ENTER(ctx, RMD160Update, context, data, len);
  8054. if (data && len > 0)
  8055. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8056. if (context)
  8057. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
  8058. REAL(RMD160Update)(context, data, len);
  8059. if (context)
  8060. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, RMD160_CTX_sz);
  8061. }
  8062. INTERCEPTOR(void, RMD160Final, u8 digest[20], void *context) {
  8063. void *ctx;
  8064. COMMON_INTERCEPTOR_ENTER(ctx, RMD160Final, digest, context);
  8065. if (context)
  8066. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
  8067. REAL(RMD160Final)(digest, context);
  8068. if (digest)
  8069. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(u8) * 20);
  8070. }
  8071. INTERCEPTOR(void, RMD160Transform, u32 state[5], u16 buffer[16]) {
  8072. void *ctx;
  8073. COMMON_INTERCEPTOR_ENTER(ctx, RMD160Transform, state, buffer);
  8074. if (state)
  8075. COMMON_INTERCEPTOR_READ_RANGE(ctx, state, sizeof(u32) * 5);
  8076. if (buffer)
  8077. COMMON_INTERCEPTOR_READ_RANGE(ctx, buffer, sizeof(u32) * 16);
  8078. REAL(RMD160Transform)(state, buffer);
  8079. if (state)
  8080. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, state, sizeof(u32) * 5);
  8081. }
  8082. INTERCEPTOR(char *, RMD160End, void *context, char *buf) {
  8083. void *ctx;
  8084. COMMON_INTERCEPTOR_ENTER(ctx, RMD160End, context, buf);
  8085. if (context)
  8086. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, RMD160_CTX_sz);
  8087. char *ret = REAL(RMD160End)(context, buf);
  8088. if (ret)
  8089. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  8090. return ret;
  8091. }
  8092. INTERCEPTOR(char *, RMD160File, char *filename, char *buf) {
  8093. void *ctx;
  8094. COMMON_INTERCEPTOR_ENTER(ctx, RMD160File, filename, buf);
  8095. if (filename)
  8096. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8097. char *ret = REAL(RMD160File)(filename, buf);
  8098. if (ret)
  8099. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  8100. return ret;
  8101. }
  8102. INTERCEPTOR(char *, RMD160FileChunk, char *filename, char *buf, OFF_T offset,
  8103. OFF_T length) {
  8104. void *ctx;
  8105. COMMON_INTERCEPTOR_ENTER(ctx, RMD160FileChunk, filename, buf, offset, length);
  8106. if (filename)
  8107. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8108. char *ret = REAL(RMD160FileChunk)(filename, buf, offset, length);
  8109. if (ret)
  8110. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  8111. return ret;
  8112. }
  8113. INTERCEPTOR(char *, RMD160Data, u8 *data, SIZE_T len, char *buf) {
  8114. void *ctx;
  8115. COMMON_INTERCEPTOR_ENTER(ctx, RMD160Data, data, len, buf);
  8116. if (data && len > 0)
  8117. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8118. char *ret = REAL(RMD160Data)(data, len, buf);
  8119. if (ret)
  8120. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, RMD160_return_length);
  8121. return ret;
  8122. }
  8123. #define INIT_RMD160 \
  8124. COMMON_INTERCEPT_FUNCTION(RMD160Init); \
  8125. COMMON_INTERCEPT_FUNCTION(RMD160Update); \
  8126. COMMON_INTERCEPT_FUNCTION(RMD160Final); \
  8127. COMMON_INTERCEPT_FUNCTION(RMD160Transform); \
  8128. COMMON_INTERCEPT_FUNCTION(RMD160End); \
  8129. COMMON_INTERCEPT_FUNCTION(RMD160File); \
  8130. COMMON_INTERCEPT_FUNCTION(RMD160FileChunk); \
  8131. COMMON_INTERCEPT_FUNCTION(RMD160Data)
  8132. #else
  8133. #define INIT_RMD160
  8134. #endif
  8135. #if SANITIZER_INTERCEPT_MD5
  8136. INTERCEPTOR(void, MD5Init, void *context) {
  8137. void *ctx;
  8138. COMMON_INTERCEPTOR_ENTER(ctx, MD5Init, context);
  8139. REAL(MD5Init)(context);
  8140. if (context)
  8141. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
  8142. }
  8143. INTERCEPTOR(void, MD5Update, void *context, const unsigned char *data,
  8144. unsigned int len) {
  8145. void *ctx;
  8146. COMMON_INTERCEPTOR_ENTER(ctx, MD5Update, context, data, len);
  8147. if (data && len > 0)
  8148. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8149. if (context)
  8150. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
  8151. REAL(MD5Update)(context, data, len);
  8152. if (context)
  8153. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD5_CTX_sz);
  8154. }
  8155. INTERCEPTOR(void, MD5Final, unsigned char digest[16], void *context) {
  8156. void *ctx;
  8157. COMMON_INTERCEPTOR_ENTER(ctx, MD5Final, digest, context);
  8158. if (context)
  8159. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
  8160. REAL(MD5Final)(digest, context);
  8161. if (digest)
  8162. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
  8163. }
  8164. INTERCEPTOR(char *, MD5End, void *context, char *buf) {
  8165. void *ctx;
  8166. COMMON_INTERCEPTOR_ENTER(ctx, MD5End, context, buf);
  8167. if (context)
  8168. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD5_CTX_sz);
  8169. char *ret = REAL(MD5End)(context, buf);
  8170. if (ret)
  8171. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
  8172. return ret;
  8173. }
  8174. INTERCEPTOR(char *, MD5File, const char *filename, char *buf) {
  8175. void *ctx;
  8176. COMMON_INTERCEPTOR_ENTER(ctx, MD5File, filename, buf);
  8177. if (filename)
  8178. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8179. char *ret = REAL(MD5File)(filename, buf);
  8180. if (ret)
  8181. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
  8182. return ret;
  8183. }
  8184. INTERCEPTOR(char *, MD5Data, const unsigned char *data, unsigned int len,
  8185. char *buf) {
  8186. void *ctx;
  8187. COMMON_INTERCEPTOR_ENTER(ctx, MD5Data, data, len, buf);
  8188. if (data && len > 0)
  8189. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8190. char *ret = REAL(MD5Data)(data, len, buf);
  8191. if (ret)
  8192. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD5_return_length);
  8193. return ret;
  8194. }
  8195. #define INIT_MD5 \
  8196. COMMON_INTERCEPT_FUNCTION(MD5Init); \
  8197. COMMON_INTERCEPT_FUNCTION(MD5Update); \
  8198. COMMON_INTERCEPT_FUNCTION(MD5Final); \
  8199. COMMON_INTERCEPT_FUNCTION(MD5End); \
  8200. COMMON_INTERCEPT_FUNCTION(MD5File); \
  8201. COMMON_INTERCEPT_FUNCTION(MD5Data)
  8202. #else
  8203. #define INIT_MD5
  8204. #endif
  8205. #if SANITIZER_INTERCEPT_FSEEK
  8206. INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
  8207. void *ctx;
  8208. COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
  8209. return REAL(fseek)(stream, offset, whence);
  8210. }
  8211. INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
  8212. void *ctx;
  8213. COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
  8214. return REAL(fseeko)(stream, offset, whence);
  8215. }
  8216. INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
  8217. void *ctx;
  8218. COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
  8219. return REAL(ftell)(stream);
  8220. }
  8221. INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
  8222. void *ctx;
  8223. COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
  8224. return REAL(ftello)(stream);
  8225. }
  8226. INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
  8227. void *ctx;
  8228. COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
  8229. return REAL(rewind)(stream);
  8230. }
  8231. INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
  8232. void *ctx;
  8233. COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
  8234. int ret = REAL(fgetpos)(stream, pos);
  8235. if (pos && !ret)
  8236. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
  8237. return ret;
  8238. }
  8239. INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
  8240. void *ctx;
  8241. COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
  8242. if (pos)
  8243. COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
  8244. return REAL(fsetpos)(stream, pos);
  8245. }
  8246. #define INIT_FSEEK \
  8247. COMMON_INTERCEPT_FUNCTION(fseek); \
  8248. COMMON_INTERCEPT_FUNCTION(fseeko); \
  8249. COMMON_INTERCEPT_FUNCTION(ftell); \
  8250. COMMON_INTERCEPT_FUNCTION(ftello); \
  8251. COMMON_INTERCEPT_FUNCTION(rewind); \
  8252. COMMON_INTERCEPT_FUNCTION(fgetpos); \
  8253. COMMON_INTERCEPT_FUNCTION(fsetpos)
  8254. #else
  8255. #define INIT_FSEEK
  8256. #endif
  8257. #if SANITIZER_INTERCEPT_MD2
  8258. INTERCEPTOR(void, MD2Init, void *context) {
  8259. void *ctx;
  8260. COMMON_INTERCEPTOR_ENTER(ctx, MD2Init, context);
  8261. REAL(MD2Init)(context);
  8262. if (context)
  8263. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
  8264. }
  8265. INTERCEPTOR(void, MD2Update, void *context, const unsigned char *data,
  8266. unsigned int len) {
  8267. void *ctx;
  8268. COMMON_INTERCEPTOR_ENTER(ctx, MD2Update, context, data, len);
  8269. if (data && len > 0)
  8270. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8271. if (context)
  8272. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
  8273. REAL(MD2Update)(context, data, len);
  8274. if (context)
  8275. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, MD2_CTX_sz);
  8276. }
  8277. INTERCEPTOR(void, MD2Final, unsigned char digest[16], void *context) {
  8278. void *ctx;
  8279. COMMON_INTERCEPTOR_ENTER(ctx, MD2Final, digest, context);
  8280. if (context)
  8281. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
  8282. REAL(MD2Final)(digest, context);
  8283. if (digest)
  8284. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, sizeof(unsigned char) * 16);
  8285. }
  8286. INTERCEPTOR(char *, MD2End, void *context, char *buf) {
  8287. void *ctx;
  8288. COMMON_INTERCEPTOR_ENTER(ctx, MD2End, context, buf);
  8289. if (context)
  8290. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, MD2_CTX_sz);
  8291. char *ret = REAL(MD2End)(context, buf);
  8292. if (ret)
  8293. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
  8294. return ret;
  8295. }
  8296. INTERCEPTOR(char *, MD2File, const char *filename, char *buf) {
  8297. void *ctx;
  8298. COMMON_INTERCEPTOR_ENTER(ctx, MD2File, filename, buf);
  8299. if (filename)
  8300. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);
  8301. char *ret = REAL(MD2File)(filename, buf);
  8302. if (ret)
  8303. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
  8304. return ret;
  8305. }
  8306. INTERCEPTOR(char *, MD2Data, const unsigned char *data, unsigned int len,
  8307. char *buf) {
  8308. void *ctx;
  8309. COMMON_INTERCEPTOR_ENTER(ctx, MD2Data, data, len, buf);
  8310. if (data && len > 0)
  8311. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len);
  8312. char *ret = REAL(MD2Data)(data, len, buf);
  8313. if (ret)
  8314. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, MD2_return_length);
  8315. return ret;
  8316. }
  8317. #define INIT_MD2 \
  8318. COMMON_INTERCEPT_FUNCTION(MD2Init); \
  8319. COMMON_INTERCEPT_FUNCTION(MD2Update); \
  8320. COMMON_INTERCEPT_FUNCTION(MD2Final); \
  8321. COMMON_INTERCEPT_FUNCTION(MD2End); \
  8322. COMMON_INTERCEPT_FUNCTION(MD2File); \
  8323. COMMON_INTERCEPT_FUNCTION(MD2Data)
  8324. #else
  8325. #define INIT_MD2
  8326. #endif
  8327. #if SANITIZER_INTERCEPT_SHA2
  8328. #define SHA2_INTERCEPTORS(LEN, SHA2_STATE_T) \
  8329. INTERCEPTOR(void, SHA##LEN##_Init, void *context) { \
  8330. void *ctx; \
  8331. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Init, context); \
  8332. REAL(SHA##LEN##_Init)(context); \
  8333. if (context) \
  8334. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
  8335. } \
  8336. INTERCEPTOR(void, SHA##LEN##_Update, void *context, \
  8337. const u8 *data, SIZE_T len) { \
  8338. void *ctx; \
  8339. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Update, context, data, len); \
  8340. if (data && len > 0) \
  8341. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
  8342. if (context) \
  8343. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
  8344. REAL(SHA##LEN##_Update)(context, data, len); \
  8345. if (context) \
  8346. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
  8347. } \
  8348. INTERCEPTOR(void, SHA##LEN##_Final, u8 digest[LEN/8], \
  8349. void *context) { \
  8350. void *ctx; \
  8351. CHECK_EQ(SHA##LEN##_digest_length, LEN/8); \
  8352. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Final, digest, context); \
  8353. if (context) \
  8354. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
  8355. REAL(SHA##LEN##_Final)(digest, context); \
  8356. if (digest) \
  8357. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, digest, \
  8358. sizeof(digest[0]) * \
  8359. SHA##LEN##_digest_length); \
  8360. } \
  8361. INTERCEPTOR(char *, SHA##LEN##_End, void *context, char *buf) { \
  8362. void *ctx; \
  8363. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_End, context, buf); \
  8364. if (context) \
  8365. COMMON_INTERCEPTOR_READ_RANGE(ctx, context, SHA##LEN##_CTX_sz); \
  8366. char *ret = REAL(SHA##LEN##_End)(context, buf); \
  8367. if (ret) \
  8368. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
  8369. return ret; \
  8370. } \
  8371. INTERCEPTOR(char *, SHA##LEN##_File, const char *filename, char *buf) { \
  8372. void *ctx; \
  8373. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_File, filename, buf); \
  8374. if (filename) \
  8375. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);\
  8376. char *ret = REAL(SHA##LEN##_File)(filename, buf); \
  8377. if (ret) \
  8378. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
  8379. return ret; \
  8380. } \
  8381. INTERCEPTOR(char *, SHA##LEN##_FileChunk, const char *filename, char *buf, \
  8382. OFF_T offset, OFF_T length) { \
  8383. void *ctx; \
  8384. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_FileChunk, filename, buf, offset, \
  8385. length); \
  8386. if (filename) \
  8387. COMMON_INTERCEPTOR_READ_RANGE(ctx, filename, internal_strlen(filename) + 1);\
  8388. char *ret = REAL(SHA##LEN##_FileChunk)(filename, buf, offset, length); \
  8389. if (ret) \
  8390. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
  8391. return ret; \
  8392. } \
  8393. INTERCEPTOR(char *, SHA##LEN##_Data, u8 *data, SIZE_T len, char *buf) { \
  8394. void *ctx; \
  8395. COMMON_INTERCEPTOR_ENTER(ctx, SHA##LEN##_Data, data, len, buf); \
  8396. if (data && len > 0) \
  8397. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, len); \
  8398. char *ret = REAL(SHA##LEN##_Data)(data, len, buf); \
  8399. if (ret) \
  8400. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, SHA##LEN##_return_length); \
  8401. return ret; \
  8402. }
  8403. SHA2_INTERCEPTORS(224, u32)
  8404. SHA2_INTERCEPTORS(256, u32)
  8405. SHA2_INTERCEPTORS(384, u64)
  8406. SHA2_INTERCEPTORS(512, u64)
  8407. #define INIT_SHA2_INTECEPTORS(LEN) \
  8408. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Init); \
  8409. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Update); \
  8410. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Final); \
  8411. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_End); \
  8412. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_File); \
  8413. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_FileChunk); \
  8414. COMMON_INTERCEPT_FUNCTION(SHA##LEN##_Data)
  8415. #define INIT_SHA2 \
  8416. INIT_SHA2_INTECEPTORS(224); \
  8417. INIT_SHA2_INTECEPTORS(256); \
  8418. INIT_SHA2_INTECEPTORS(384); \
  8419. INIT_SHA2_INTECEPTORS(512)
  8420. #undef SHA2_INTERCEPTORS
  8421. #else
  8422. #define INIT_SHA2
  8423. #endif
  8424. #if SANITIZER_INTERCEPT_VIS
  8425. INTERCEPTOR(char *, vis, char *dst, int c, int flag, int nextc) {
  8426. void *ctx;
  8427. COMMON_INTERCEPTOR_ENTER(ctx, vis, dst, c, flag, nextc);
  8428. char *end = REAL(vis)(dst, c, flag, nextc);
  8429. // dst is NULL terminated and end points to the NULL char
  8430. if (dst && end)
  8431. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
  8432. return end;
  8433. }
  8434. INTERCEPTOR(char *, nvis, char *dst, SIZE_T dlen, int c, int flag, int nextc) {
  8435. void *ctx;
  8436. COMMON_INTERCEPTOR_ENTER(ctx, nvis, dst, dlen, c, flag, nextc);
  8437. char *end = REAL(nvis)(dst, dlen, c, flag, nextc);
  8438. // nvis cannot make sure the dst is NULL terminated
  8439. if (dst && end)
  8440. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
  8441. return end;
  8442. }
  8443. INTERCEPTOR(int, strvis, char *dst, const char *src, int flag) {
  8444. void *ctx;
  8445. COMMON_INTERCEPTOR_ENTER(ctx, strvis, dst, src, flag);
  8446. if (src)
  8447. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8448. int len = REAL(strvis)(dst, src, flag);
  8449. if (dst)
  8450. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  8451. return len;
  8452. }
  8453. INTERCEPTOR(int, stravis, char **dst, const char *src, int flag) {
  8454. void *ctx;
  8455. COMMON_INTERCEPTOR_ENTER(ctx, stravis, dst, src, flag);
  8456. if (src)
  8457. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8458. int len = REAL(stravis)(dst, src, flag);
  8459. if (dst) {
  8460. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, sizeof(char *));
  8461. if (*dst)
  8462. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *dst, len + 1);
  8463. }
  8464. return len;
  8465. }
  8466. INTERCEPTOR(int, strnvis, char *dst, SIZE_T dlen, const char *src, int flag) {
  8467. void *ctx;
  8468. COMMON_INTERCEPTOR_ENTER(ctx, strnvis, dst, dlen, src, flag);
  8469. if (src)
  8470. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8471. int len = REAL(strnvis)(dst, dlen, src, flag);
  8472. // The interface will be valid even if there is no space for NULL char
  8473. if (dst && len > 0)
  8474. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  8475. return len;
  8476. }
  8477. INTERCEPTOR(int, strvisx, char *dst, const char *src, SIZE_T len, int flag) {
  8478. void *ctx;
  8479. COMMON_INTERCEPTOR_ENTER(ctx, strvisx, dst, src, len, flag);
  8480. if (src)
  8481. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8482. int ret = REAL(strvisx)(dst, src, len, flag);
  8483. if (dst)
  8484. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8485. return ret;
  8486. }
  8487. INTERCEPTOR(int, strnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
  8488. int flag) {
  8489. void *ctx;
  8490. COMMON_INTERCEPTOR_ENTER(ctx, strnvisx, dst, dlen, src, len, flag);
  8491. if (src)
  8492. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8493. int ret = REAL(strnvisx)(dst, dlen, src, len, flag);
  8494. if (dst && ret >= 0)
  8495. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8496. return ret;
  8497. }
  8498. INTERCEPTOR(int, strenvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
  8499. int flag, int *cerr_ptr) {
  8500. void *ctx;
  8501. COMMON_INTERCEPTOR_ENTER(ctx, strenvisx, dst, dlen, src, len, flag, cerr_ptr);
  8502. if (src)
  8503. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8504. // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
  8505. // according to the implementation
  8506. if (cerr_ptr)
  8507. COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
  8508. int ret = REAL(strenvisx)(dst, dlen, src, len, flag, cerr_ptr);
  8509. if (dst && ret >= 0)
  8510. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8511. if (cerr_ptr)
  8512. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
  8513. return ret;
  8514. }
  8515. INTERCEPTOR(char *, svis, char *dst, int c, int flag, int nextc,
  8516. const char *extra) {
  8517. void *ctx;
  8518. COMMON_INTERCEPTOR_ENTER(ctx, svis, dst, c, flag, nextc, extra);
  8519. if (extra)
  8520. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8521. char *end = REAL(svis)(dst, c, flag, nextc, extra);
  8522. if (dst && end)
  8523. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, end - dst + 1);
  8524. return end;
  8525. }
  8526. INTERCEPTOR(char *, snvis, char *dst, SIZE_T dlen, int c, int flag, int nextc,
  8527. const char *extra) {
  8528. void *ctx;
  8529. COMMON_INTERCEPTOR_ENTER(ctx, snvis, dst, dlen, c, flag, nextc, extra);
  8530. if (extra)
  8531. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8532. char *end = REAL(snvis)(dst, dlen, c, flag, nextc, extra);
  8533. if (dst && end)
  8534. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst,
  8535. Min((SIZE_T)(end - dst + 1), dlen));
  8536. return end;
  8537. }
  8538. INTERCEPTOR(int, strsvis, char *dst, const char *src, int flag,
  8539. const char *extra) {
  8540. void *ctx;
  8541. COMMON_INTERCEPTOR_ENTER(ctx, strsvis, dst, src, flag, extra);
  8542. if (src)
  8543. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8544. if (extra)
  8545. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8546. int len = REAL(strsvis)(dst, src, flag, extra);
  8547. if (dst)
  8548. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  8549. return len;
  8550. }
  8551. INTERCEPTOR(int, strsnvis, char *dst, SIZE_T dlen, const char *src, int flag,
  8552. const char *extra) {
  8553. void *ctx;
  8554. COMMON_INTERCEPTOR_ENTER(ctx, strsnvis, dst, dlen, src, flag, extra);
  8555. if (src)
  8556. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8557. if (extra)
  8558. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8559. int len = REAL(strsnvis)(dst, dlen, src, flag, extra);
  8560. // The interface will be valid even if there is no space for NULL char
  8561. if (dst && len >= 0)
  8562. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, len + 1);
  8563. return len;
  8564. }
  8565. INTERCEPTOR(int, strsvisx, char *dst, const char *src, SIZE_T len, int flag,
  8566. const char *extra) {
  8567. void *ctx;
  8568. COMMON_INTERCEPTOR_ENTER(ctx, strsvisx, dst, src, len, flag, extra);
  8569. if (src)
  8570. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8571. if (extra)
  8572. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8573. int ret = REAL(strsvisx)(dst, src, len, flag, extra);
  8574. if (dst)
  8575. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8576. return ret;
  8577. }
  8578. INTERCEPTOR(int, strsnvisx, char *dst, SIZE_T dlen, const char *src, SIZE_T len,
  8579. int flag, const char *extra) {
  8580. void *ctx;
  8581. COMMON_INTERCEPTOR_ENTER(ctx, strsnvisx, dst, dlen, src, len, flag, extra);
  8582. if (src)
  8583. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8584. if (extra)
  8585. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8586. int ret = REAL(strsnvisx)(dst, dlen, src, len, flag, extra);
  8587. if (dst && ret >= 0)
  8588. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8589. return ret;
  8590. }
  8591. INTERCEPTOR(int, strsenvisx, char *dst, SIZE_T dlen, const char *src,
  8592. SIZE_T len, int flag, const char *extra, int *cerr_ptr) {
  8593. void *ctx;
  8594. COMMON_INTERCEPTOR_ENTER(ctx, strsenvisx, dst, dlen, src, len, flag, extra,
  8595. cerr_ptr);
  8596. if (src)
  8597. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, len);
  8598. if (extra)
  8599. COMMON_INTERCEPTOR_READ_RANGE(ctx, extra, internal_strlen(extra) + 1);
  8600. // FIXME: only need to be checked when "flag | VIS_NOLOCALE" doesn't hold
  8601. // according to the implementation
  8602. if (cerr_ptr)
  8603. COMMON_INTERCEPTOR_READ_RANGE(ctx, cerr_ptr, sizeof(int));
  8604. int ret = REAL(strsenvisx)(dst, dlen, src, len, flag, extra, cerr_ptr);
  8605. if (dst && ret >= 0)
  8606. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8607. if (cerr_ptr)
  8608. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cerr_ptr, sizeof(int));
  8609. return ret;
  8610. }
  8611. INTERCEPTOR(int, unvis, char *cp, int c, int *astate, int flag) {
  8612. void *ctx;
  8613. COMMON_INTERCEPTOR_ENTER(ctx, unvis, cp, c, astate, flag);
  8614. if (astate)
  8615. COMMON_INTERCEPTOR_READ_RANGE(ctx, astate, sizeof(*astate));
  8616. int ret = REAL(unvis)(cp, c, astate, flag);
  8617. if (ret == unvis_valid || ret == unvis_validpush) {
  8618. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cp, sizeof(*cp));
  8619. }
  8620. return ret;
  8621. }
  8622. INTERCEPTOR(int, strunvis, char *dst, const char *src) {
  8623. void *ctx;
  8624. COMMON_INTERCEPTOR_ENTER(ctx, strunvis, dst, src);
  8625. if (src)
  8626. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8627. int ret = REAL(strunvis)(dst, src);
  8628. if (ret != -1)
  8629. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8630. return ret;
  8631. }
  8632. INTERCEPTOR(int, strnunvis, char *dst, SIZE_T dlen, const char *src) {
  8633. void *ctx;
  8634. COMMON_INTERCEPTOR_ENTER(ctx, strnunvis, dst, dlen, src);
  8635. if (src)
  8636. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8637. int ret = REAL(strnunvis)(dst, dlen, src);
  8638. if (ret != -1)
  8639. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8640. return ret;
  8641. }
  8642. INTERCEPTOR(int, strunvisx, char *dst, const char *src, int flag) {
  8643. void *ctx;
  8644. COMMON_INTERCEPTOR_ENTER(ctx, strunvisx, dst, src, flag);
  8645. if (src)
  8646. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8647. int ret = REAL(strunvisx)(dst, src, flag);
  8648. if (ret != -1)
  8649. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8650. return ret;
  8651. }
  8652. INTERCEPTOR(int, strnunvisx, char *dst, SIZE_T dlen, const char *src,
  8653. int flag) {
  8654. void *ctx;
  8655. COMMON_INTERCEPTOR_ENTER(ctx, strnunvisx, dst, dlen, src, flag);
  8656. if (src)
  8657. COMMON_INTERCEPTOR_READ_RANGE(ctx, src, internal_strlen(src) + 1);
  8658. int ret = REAL(strnunvisx)(dst, dlen, src, flag);
  8659. if (ret != -1)
  8660. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, dst, ret + 1);
  8661. return ret;
  8662. }
  8663. #define INIT_VIS \
  8664. COMMON_INTERCEPT_FUNCTION(vis); \
  8665. COMMON_INTERCEPT_FUNCTION(nvis); \
  8666. COMMON_INTERCEPT_FUNCTION(strvis); \
  8667. COMMON_INTERCEPT_FUNCTION(stravis); \
  8668. COMMON_INTERCEPT_FUNCTION(strnvis); \
  8669. COMMON_INTERCEPT_FUNCTION(strvisx); \
  8670. COMMON_INTERCEPT_FUNCTION(strnvisx); \
  8671. COMMON_INTERCEPT_FUNCTION(strenvisx); \
  8672. COMMON_INTERCEPT_FUNCTION(svis); \
  8673. COMMON_INTERCEPT_FUNCTION(snvis); \
  8674. COMMON_INTERCEPT_FUNCTION(strsvis); \
  8675. COMMON_INTERCEPT_FUNCTION(strsnvis); \
  8676. COMMON_INTERCEPT_FUNCTION(strsvisx); \
  8677. COMMON_INTERCEPT_FUNCTION(strsnvisx); \
  8678. COMMON_INTERCEPT_FUNCTION(strsenvisx); \
  8679. COMMON_INTERCEPT_FUNCTION(unvis); \
  8680. COMMON_INTERCEPT_FUNCTION(strunvis); \
  8681. COMMON_INTERCEPT_FUNCTION(strnunvis); \
  8682. COMMON_INTERCEPT_FUNCTION(strunvisx); \
  8683. COMMON_INTERCEPT_FUNCTION(strnunvisx)
  8684. #else
  8685. #define INIT_VIS
  8686. #endif
  8687. #if SANITIZER_INTERCEPT_CDB
  8688. INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open, const char *path, int flags) {
  8689. void *ctx;
  8690. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open, path, flags);
  8691. if (path)
  8692. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  8693. struct __sanitizer_cdbr *cdbr = REAL(cdbr_open)(path, flags);
  8694. if (cdbr)
  8695. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
  8696. return cdbr;
  8697. }
  8698. INTERCEPTOR(struct __sanitizer_cdbr *, cdbr_open_mem, void *base, SIZE_T size,
  8699. int flags, void (*unmap)(void *, void *, SIZE_T), void *cookie) {
  8700. void *ctx;
  8701. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_open_mem, base, size, flags, unmap,
  8702. cookie);
  8703. if (base && size)
  8704. COMMON_INTERCEPTOR_READ_RANGE(ctx, base, size);
  8705. struct __sanitizer_cdbr *cdbr =
  8706. REAL(cdbr_open_mem)(base, size, flags, unmap, cookie);
  8707. if (cdbr)
  8708. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbr, sizeof(*cdbr));
  8709. return cdbr;
  8710. }
  8711. INTERCEPTOR(u32, cdbr_entries, struct __sanitizer_cdbr *cdbr) {
  8712. void *ctx;
  8713. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_entries, cdbr);
  8714. if (cdbr)
  8715. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  8716. return REAL(cdbr_entries)(cdbr);
  8717. }
  8718. INTERCEPTOR(int, cdbr_get, struct __sanitizer_cdbr *cdbr, u32 index,
  8719. const void **data, SIZE_T *datalen) {
  8720. void *ctx;
  8721. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_get, cdbr, index, data, datalen);
  8722. if (cdbr)
  8723. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  8724. int ret = REAL(cdbr_get)(cdbr, index, data, datalen);
  8725. if (!ret) {
  8726. if (data)
  8727. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
  8728. if (datalen)
  8729. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
  8730. if (data && datalen)
  8731. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
  8732. }
  8733. return ret;
  8734. }
  8735. INTERCEPTOR(int, cdbr_find, struct __sanitizer_cdbr *cdbr, const void *key,
  8736. SIZE_T keylen, const void **data, SIZE_T *datalen) {
  8737. void *ctx;
  8738. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_find, cdbr, key, keylen, data, datalen);
  8739. if (cdbr)
  8740. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  8741. if (key)
  8742. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
  8743. int ret = REAL(cdbr_find)(cdbr, key, keylen, data, datalen);
  8744. if (!ret) {
  8745. if (data)
  8746. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data, sizeof(*data));
  8747. if (datalen)
  8748. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, datalen, sizeof(*datalen));
  8749. if (data && datalen)
  8750. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *data, *datalen);
  8751. }
  8752. return ret;
  8753. }
  8754. INTERCEPTOR(void, cdbr_close, struct __sanitizer_cdbr *cdbr) {
  8755. void *ctx;
  8756. COMMON_INTERCEPTOR_ENTER(ctx, cdbr_close, cdbr);
  8757. if (cdbr)
  8758. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbr, sizeof(*cdbr));
  8759. REAL(cdbr_close)(cdbr);
  8760. }
  8761. INTERCEPTOR(struct __sanitizer_cdbw *, cdbw_open) {
  8762. void *ctx;
  8763. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_open);
  8764. struct __sanitizer_cdbw *ret = REAL(cdbw_open)();
  8765. if (ret)
  8766. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, sizeof(*ret));
  8767. return ret;
  8768. }
  8769. INTERCEPTOR(int, cdbw_put, struct __sanitizer_cdbw *cdbw, const void *key,
  8770. SIZE_T keylen, const void *data, SIZE_T datalen) {
  8771. void *ctx;
  8772. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put, cdbw, key, keylen, data, datalen);
  8773. if (cdbw)
  8774. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  8775. if (data && datalen)
  8776. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
  8777. if (key && keylen)
  8778. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
  8779. int ret = REAL(cdbw_put)(cdbw, key, keylen, data, datalen);
  8780. if (!ret && cdbw)
  8781. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  8782. return ret;
  8783. }
  8784. INTERCEPTOR(int, cdbw_put_data, struct __sanitizer_cdbw *cdbw, const void *data,
  8785. SIZE_T datalen, u32 *index) {
  8786. void *ctx;
  8787. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_data, cdbw, data, datalen, index);
  8788. if (cdbw)
  8789. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  8790. if (data && datalen)
  8791. COMMON_INTERCEPTOR_READ_RANGE(ctx, data, datalen);
  8792. int ret = REAL(cdbw_put_data)(cdbw, data, datalen, index);
  8793. if (!ret) {
  8794. if (index)
  8795. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, index, sizeof(*index));
  8796. if (cdbw)
  8797. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  8798. }
  8799. return ret;
  8800. }
  8801. INTERCEPTOR(int, cdbw_put_key, struct __sanitizer_cdbw *cdbw, const void *key,
  8802. SIZE_T keylen, u32 index) {
  8803. void *ctx;
  8804. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_put_key, cdbw, key, keylen, index);
  8805. if (cdbw)
  8806. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  8807. if (key && keylen)
  8808. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, keylen);
  8809. int ret = REAL(cdbw_put_key)(cdbw, key, keylen, index);
  8810. if (!ret && cdbw)
  8811. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  8812. return ret;
  8813. }
  8814. INTERCEPTOR(int, cdbw_output, struct __sanitizer_cdbw *cdbw, int output,
  8815. const char descr[16], u32 (*seedgen)(void)) {
  8816. void *ctx;
  8817. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_output, cdbw, output, descr, seedgen);
  8818. COMMON_INTERCEPTOR_FD_ACCESS(ctx, output);
  8819. if (cdbw)
  8820. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  8821. if (descr)
  8822. COMMON_INTERCEPTOR_READ_RANGE(ctx, descr, internal_strnlen(descr, 16));
  8823. if (seedgen)
  8824. COMMON_INTERCEPTOR_READ_RANGE(ctx, (void *)seedgen, sizeof(seedgen));
  8825. int ret = REAL(cdbw_output)(cdbw, output, descr, seedgen);
  8826. if (!ret) {
  8827. if (cdbw)
  8828. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, cdbw, sizeof(*cdbw));
  8829. if (output >= 0)
  8830. COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, output);
  8831. }
  8832. return ret;
  8833. }
  8834. INTERCEPTOR(void, cdbw_close, struct __sanitizer_cdbw *cdbw) {
  8835. void *ctx;
  8836. COMMON_INTERCEPTOR_ENTER(ctx, cdbw_close, cdbw);
  8837. if (cdbw)
  8838. COMMON_INTERCEPTOR_READ_RANGE(ctx, cdbw, sizeof(*cdbw));
  8839. REAL(cdbw_close)(cdbw);
  8840. }
  8841. #define INIT_CDB \
  8842. COMMON_INTERCEPT_FUNCTION(cdbr_open); \
  8843. COMMON_INTERCEPT_FUNCTION(cdbr_open_mem); \
  8844. COMMON_INTERCEPT_FUNCTION(cdbr_entries); \
  8845. COMMON_INTERCEPT_FUNCTION(cdbr_get); \
  8846. COMMON_INTERCEPT_FUNCTION(cdbr_find); \
  8847. COMMON_INTERCEPT_FUNCTION(cdbr_close); \
  8848. COMMON_INTERCEPT_FUNCTION(cdbw_open); \
  8849. COMMON_INTERCEPT_FUNCTION(cdbw_put); \
  8850. COMMON_INTERCEPT_FUNCTION(cdbw_put_data); \
  8851. COMMON_INTERCEPT_FUNCTION(cdbw_put_key); \
  8852. COMMON_INTERCEPT_FUNCTION(cdbw_output); \
  8853. COMMON_INTERCEPT_FUNCTION(cdbw_close)
  8854. #else
  8855. #define INIT_CDB
  8856. #endif
  8857. #if SANITIZER_INTERCEPT_GETFSENT
  8858. INTERCEPTOR(void *, getfsent) {
  8859. void *ctx;
  8860. COMMON_INTERCEPTOR_ENTER(ctx, getfsent);
  8861. void *ret = REAL(getfsent)();
  8862. if (ret)
  8863. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
  8864. return ret;
  8865. }
  8866. INTERCEPTOR(void *, getfsspec, const char *spec) {
  8867. void *ctx;
  8868. COMMON_INTERCEPTOR_ENTER(ctx, getfsspec, spec);
  8869. if (spec)
  8870. COMMON_INTERCEPTOR_READ_RANGE(ctx, spec, internal_strlen(spec) + 1);
  8871. void *ret = REAL(getfsspec)(spec);
  8872. if (ret)
  8873. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
  8874. return ret;
  8875. }
  8876. INTERCEPTOR(void *, getfsfile, const char *file) {
  8877. void *ctx;
  8878. COMMON_INTERCEPTOR_ENTER(ctx, getfsfile, file);
  8879. if (file)
  8880. COMMON_INTERCEPTOR_READ_RANGE(ctx, file, internal_strlen(file) + 1);
  8881. void *ret = REAL(getfsfile)(file);
  8882. if (ret)
  8883. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ret, struct_fstab_sz);
  8884. return ret;
  8885. }
  8886. #define INIT_GETFSENT \
  8887. COMMON_INTERCEPT_FUNCTION(getfsent); \
  8888. COMMON_INTERCEPT_FUNCTION(getfsspec); \
  8889. COMMON_INTERCEPT_FUNCTION(getfsfile);
  8890. #else
  8891. #define INIT_GETFSENT
  8892. #endif
  8893. #if SANITIZER_INTERCEPT_ARC4RANDOM
  8894. INTERCEPTOR(void, arc4random_buf, void *buf, SIZE_T len) {
  8895. void *ctx;
  8896. COMMON_INTERCEPTOR_ENTER(ctx, arc4random_buf, buf, len);
  8897. REAL(arc4random_buf)(buf, len);
  8898. if (buf && len)
  8899. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, len);
  8900. }
  8901. INTERCEPTOR(void, arc4random_addrandom, u8 *dat, int datlen) {
  8902. void *ctx;
  8903. COMMON_INTERCEPTOR_ENTER(ctx, arc4random_addrandom, dat, datlen);
  8904. if (dat && datlen)
  8905. COMMON_INTERCEPTOR_READ_RANGE(ctx, dat, datlen);
  8906. REAL(arc4random_addrandom)(dat, datlen);
  8907. }
  8908. #define INIT_ARC4RANDOM \
  8909. COMMON_INTERCEPT_FUNCTION(arc4random_buf); \
  8910. COMMON_INTERCEPT_FUNCTION(arc4random_addrandom);
  8911. #else
  8912. #define INIT_ARC4RANDOM
  8913. #endif
  8914. #if SANITIZER_INTERCEPT_POPEN
  8915. INTERCEPTOR(__sanitizer_FILE *, popen, const char *command, const char *type) {
  8916. void *ctx;
  8917. COMMON_INTERCEPTOR_ENTER(ctx, popen, command, type);
  8918. if (command)
  8919. COMMON_INTERCEPTOR_READ_RANGE(ctx, command, internal_strlen(command) + 1);
  8920. if (type)
  8921. COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
  8922. __sanitizer_FILE *res = REAL(popen)(command, type);
  8923. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
  8924. if (res) unpoison_file(res);
  8925. return res;
  8926. }
  8927. #define INIT_POPEN COMMON_INTERCEPT_FUNCTION(popen)
  8928. #else
  8929. #define INIT_POPEN
  8930. #endif
  8931. #if SANITIZER_INTERCEPT_POPENVE
  8932. INTERCEPTOR(__sanitizer_FILE *, popenve, const char *path,
  8933. char *const *argv, char *const *envp, const char *type) {
  8934. void *ctx;
  8935. COMMON_INTERCEPTOR_ENTER(ctx, popenve, path, argv, envp, type);
  8936. if (path)
  8937. COMMON_INTERCEPTOR_READ_RANGE(ctx, path, internal_strlen(path) + 1);
  8938. if (argv) {
  8939. for (char *const *pa = argv; ; ++pa) {
  8940. COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
  8941. if (!*pa)
  8942. break;
  8943. COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
  8944. }
  8945. }
  8946. if (envp) {
  8947. for (char *const *pa = envp; ; ++pa) {
  8948. COMMON_INTERCEPTOR_READ_RANGE(ctx, pa, sizeof(char **));
  8949. if (!*pa)
  8950. break;
  8951. COMMON_INTERCEPTOR_READ_RANGE(ctx, *pa, internal_strlen(*pa) + 1);
  8952. }
  8953. }
  8954. if (type)
  8955. COMMON_INTERCEPTOR_READ_RANGE(ctx, type, internal_strlen(type) + 1);
  8956. __sanitizer_FILE *res = REAL(popenve)(path, argv, envp, type);
  8957. COMMON_INTERCEPTOR_FILE_OPEN(ctx, res, nullptr);
  8958. if (res) unpoison_file(res);
  8959. return res;
  8960. }
  8961. #define INIT_POPENVE COMMON_INTERCEPT_FUNCTION(popenve)
  8962. #else
  8963. #define INIT_POPENVE
  8964. #endif
  8965. #if SANITIZER_INTERCEPT_PCLOSE
  8966. INTERCEPTOR(int, pclose, __sanitizer_FILE *fp) {
  8967. void *ctx;
  8968. COMMON_INTERCEPTOR_ENTER(ctx, pclose, fp);
  8969. COMMON_INTERCEPTOR_FILE_CLOSE(ctx, fp);
  8970. const FileMetadata *m = GetInterceptorMetadata(fp);
  8971. int res = REAL(pclose)(fp);
  8972. if (m) {
  8973. COMMON_INTERCEPTOR_INITIALIZE_RANGE(*m->addr, *m->size);
  8974. DeleteInterceptorMetadata(fp);
  8975. }
  8976. return res;
  8977. }
  8978. #define INIT_PCLOSE COMMON_INTERCEPT_FUNCTION(pclose);
  8979. #else
  8980. #define INIT_PCLOSE
  8981. #endif
  8982. #if SANITIZER_INTERCEPT_FUNOPEN
  8983. typedef int (*funopen_readfn)(void *cookie, char *buf, int len);
  8984. typedef int (*funopen_writefn)(void *cookie, const char *buf, int len);
  8985. typedef OFF_T (*funopen_seekfn)(void *cookie, OFF_T offset, int whence);
  8986. typedef int (*funopen_closefn)(void *cookie);
  8987. struct WrappedFunopenCookie {
  8988. void *real_cookie;
  8989. funopen_readfn real_read;
  8990. funopen_writefn real_write;
  8991. funopen_seekfn real_seek;
  8992. funopen_closefn real_close;
  8993. };
  8994. static int wrapped_funopen_read(void *cookie, char *buf, int len) {
  8995. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  8996. WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  8997. funopen_readfn real_read = wrapped_cookie->real_read;
  8998. return real_read(wrapped_cookie->real_cookie, buf, len);
  8999. }
  9000. static int wrapped_funopen_write(void *cookie, const char *buf, int len) {
  9001. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9002. WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  9003. funopen_writefn real_write = wrapped_cookie->real_write;
  9004. return real_write(wrapped_cookie->real_cookie, buf, len);
  9005. }
  9006. static OFF_T wrapped_funopen_seek(void *cookie, OFF_T offset, int whence) {
  9007. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9008. WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  9009. funopen_seekfn real_seek = wrapped_cookie->real_seek;
  9010. return real_seek(wrapped_cookie->real_cookie, offset, whence);
  9011. }
  9012. static int wrapped_funopen_close(void *cookie) {
  9013. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  9014. WrappedFunopenCookie *wrapped_cookie = (WrappedFunopenCookie *)cookie;
  9015. funopen_closefn real_close = wrapped_cookie->real_close;
  9016. int res = real_close(wrapped_cookie->real_cookie);
  9017. InternalFree(wrapped_cookie);
  9018. return res;
  9019. }
  9020. INTERCEPTOR(__sanitizer_FILE *, funopen, void *cookie, funopen_readfn readfn,
  9021. funopen_writefn writefn, funopen_seekfn seekfn,
  9022. funopen_closefn closefn) {
  9023. void *ctx;
  9024. COMMON_INTERCEPTOR_ENTER(ctx, funopen, cookie, readfn, writefn, seekfn,
  9025. closefn);
  9026. WrappedFunopenCookie *wrapped_cookie =
  9027. (WrappedFunopenCookie *)InternalAlloc(sizeof(WrappedFunopenCookie));
  9028. wrapped_cookie->real_cookie = cookie;
  9029. wrapped_cookie->real_read = readfn;
  9030. wrapped_cookie->real_write = writefn;
  9031. wrapped_cookie->real_seek = seekfn;
  9032. wrapped_cookie->real_close = closefn;
  9033. __sanitizer_FILE *res =
  9034. REAL(funopen)(wrapped_cookie,
  9035. readfn ? wrapped_funopen_read : nullptr,
  9036. writefn ? wrapped_funopen_write : nullptr,
  9037. seekfn ? wrapped_funopen_seek : nullptr,
  9038. closefn ? wrapped_funopen_close : nullptr);
  9039. if (res)
  9040. unpoison_file(res);
  9041. return res;
  9042. }
  9043. #define INIT_FUNOPEN COMMON_INTERCEPT_FUNCTION(funopen)
  9044. #else
  9045. #define INIT_FUNOPEN
  9046. #endif
  9047. #if SANITIZER_INTERCEPT_FUNOPEN2
  9048. typedef SSIZE_T (*funopen2_readfn)(void *cookie, void *buf, SIZE_T len);
  9049. typedef SSIZE_T (*funopen2_writefn)(void *cookie, const void *buf, SIZE_T len);
  9050. typedef OFF_T (*funopen2_seekfn)(void *cookie, OFF_T offset, int whence);
  9051. typedef int (*funopen2_flushfn)(void *cookie);
  9052. typedef int (*funopen2_closefn)(void *cookie);
  9053. struct WrappedFunopen2Cookie {
  9054. void *real_cookie;
  9055. funopen2_readfn real_read;
  9056. funopen2_writefn real_write;
  9057. funopen2_seekfn real_seek;
  9058. funopen2_flushfn real_flush;
  9059. funopen2_closefn real_close;
  9060. };
  9061. static SSIZE_T wrapped_funopen2_read(void *cookie, void *buf, SIZE_T len) {
  9062. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9063. WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  9064. funopen2_readfn real_read = wrapped_cookie->real_read;
  9065. return real_read(wrapped_cookie->real_cookie, buf, len);
  9066. }
  9067. static SSIZE_T wrapped_funopen2_write(void *cookie, const void *buf,
  9068. SIZE_T len) {
  9069. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9070. WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  9071. funopen2_writefn real_write = wrapped_cookie->real_write;
  9072. return real_write(wrapped_cookie->real_cookie, buf, len);
  9073. }
  9074. static OFF_T wrapped_funopen2_seek(void *cookie, OFF_T offset, int whence) {
  9075. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9076. WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  9077. funopen2_seekfn real_seek = wrapped_cookie->real_seek;
  9078. return real_seek(wrapped_cookie->real_cookie, offset, whence);
  9079. }
  9080. static int wrapped_funopen2_flush(void *cookie) {
  9081. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  9082. WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  9083. funopen2_flushfn real_flush = wrapped_cookie->real_flush;
  9084. return real_flush(wrapped_cookie->real_cookie);
  9085. }
  9086. static int wrapped_funopen2_close(void *cookie) {
  9087. COMMON_INTERCEPTOR_UNPOISON_PARAM(1);
  9088. WrappedFunopen2Cookie *wrapped_cookie = (WrappedFunopen2Cookie *)cookie;
  9089. funopen2_closefn real_close = wrapped_cookie->real_close;
  9090. int res = real_close(wrapped_cookie->real_cookie);
  9091. InternalFree(wrapped_cookie);
  9092. return res;
  9093. }
  9094. INTERCEPTOR(__sanitizer_FILE *, funopen2, void *cookie, funopen2_readfn readfn,
  9095. funopen2_writefn writefn, funopen2_seekfn seekfn,
  9096. funopen2_flushfn flushfn, funopen2_closefn closefn) {
  9097. void *ctx;
  9098. COMMON_INTERCEPTOR_ENTER(ctx, funopen2, cookie, readfn, writefn, seekfn,
  9099. flushfn, closefn);
  9100. WrappedFunopen2Cookie *wrapped_cookie =
  9101. (WrappedFunopen2Cookie *)InternalAlloc(sizeof(WrappedFunopen2Cookie));
  9102. wrapped_cookie->real_cookie = cookie;
  9103. wrapped_cookie->real_read = readfn;
  9104. wrapped_cookie->real_write = writefn;
  9105. wrapped_cookie->real_seek = seekfn;
  9106. wrapped_cookie->real_flush = flushfn;
  9107. wrapped_cookie->real_close = closefn;
  9108. __sanitizer_FILE *res =
  9109. REAL(funopen2)(wrapped_cookie,
  9110. readfn ? wrapped_funopen2_read : nullptr,
  9111. writefn ? wrapped_funopen2_write : nullptr,
  9112. seekfn ? wrapped_funopen2_seek : nullptr,
  9113. flushfn ? wrapped_funopen2_flush : nullptr,
  9114. closefn ? wrapped_funopen2_close : nullptr);
  9115. if (res)
  9116. unpoison_file(res);
  9117. return res;
  9118. }
  9119. #define INIT_FUNOPEN2 COMMON_INTERCEPT_FUNCTION(funopen2)
  9120. #else
  9121. #define INIT_FUNOPEN2
  9122. #endif
  9123. #if SANITIZER_INTERCEPT_FDEVNAME
  9124. INTERCEPTOR(char *, fdevname, int fd) {
  9125. void *ctx;
  9126. COMMON_INTERCEPTOR_ENTER(ctx, fdevname, fd);
  9127. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  9128. char *name = REAL(fdevname)(fd);
  9129. if (name) {
  9130. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, name, internal_strlen(name) + 1);
  9131. if (fd > 0)
  9132. COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  9133. }
  9134. return name;
  9135. }
  9136. INTERCEPTOR(char *, fdevname_r, int fd, char *buf, SIZE_T len) {
  9137. void *ctx;
  9138. COMMON_INTERCEPTOR_ENTER(ctx, fdevname_r, fd, buf, len);
  9139. COMMON_INTERCEPTOR_FD_ACCESS(ctx, fd);
  9140. char *name = REAL(fdevname_r)(fd, buf, len);
  9141. if (name && buf && len > 0) {
  9142. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, internal_strlen(buf) + 1);
  9143. if (fd > 0)
  9144. COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd);
  9145. }
  9146. return name;
  9147. }
  9148. #define INIT_FDEVNAME \
  9149. COMMON_INTERCEPT_FUNCTION(fdevname); \
  9150. COMMON_INTERCEPT_FUNCTION(fdevname_r);
  9151. #else
  9152. #define INIT_FDEVNAME
  9153. #endif
  9154. #if SANITIZER_INTERCEPT_GETUSERSHELL
  9155. INTERCEPTOR(char *, getusershell) {
  9156. void *ctx;
  9157. COMMON_INTERCEPTOR_ENTER(ctx, getusershell);
  9158. char *res = REAL(getusershell)();
  9159. if (res)
  9160. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  9161. return res;
  9162. }
  9163. #define INIT_GETUSERSHELL COMMON_INTERCEPT_FUNCTION(getusershell);
  9164. #else
  9165. #define INIT_GETUSERSHELL
  9166. #endif
  9167. #if SANITIZER_INTERCEPT_SL_INIT
  9168. INTERCEPTOR(void *, sl_init) {
  9169. void *ctx;
  9170. COMMON_INTERCEPTOR_ENTER(ctx, sl_init);
  9171. void *res = REAL(sl_init)();
  9172. if (res)
  9173. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, __sanitizer::struct_StringList_sz);
  9174. return res;
  9175. }
  9176. INTERCEPTOR(int, sl_add, void *sl, char *item) {
  9177. void *ctx;
  9178. COMMON_INTERCEPTOR_ENTER(ctx, sl_add, sl, item);
  9179. if (sl)
  9180. COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  9181. if (item)
  9182. COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
  9183. int res = REAL(sl_add)(sl, item);
  9184. if (!res)
  9185. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  9186. return res;
  9187. }
  9188. INTERCEPTOR(char *, sl_find, void *sl, const char *item) {
  9189. void *ctx;
  9190. COMMON_INTERCEPTOR_ENTER(ctx, sl_find, sl, item);
  9191. if (sl)
  9192. COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  9193. if (item)
  9194. COMMON_INTERCEPTOR_READ_RANGE(ctx, item, internal_strlen(item) + 1);
  9195. char *res = REAL(sl_find)(sl, item);
  9196. if (res)
  9197. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, res, internal_strlen(res) + 1);
  9198. return res;
  9199. }
  9200. INTERCEPTOR(void, sl_free, void *sl, int freeall) {
  9201. void *ctx;
  9202. COMMON_INTERCEPTOR_ENTER(ctx, sl_free, sl, freeall);
  9203. if (sl)
  9204. COMMON_INTERCEPTOR_READ_RANGE(ctx, sl, __sanitizer::struct_StringList_sz);
  9205. REAL(sl_free)(sl, freeall);
  9206. }
  9207. #define INIT_SL_INIT \
  9208. COMMON_INTERCEPT_FUNCTION(sl_init); \
  9209. COMMON_INTERCEPT_FUNCTION(sl_add); \
  9210. COMMON_INTERCEPT_FUNCTION(sl_find); \
  9211. COMMON_INTERCEPT_FUNCTION(sl_free);
  9212. #else
  9213. #define INIT_SL_INIT
  9214. #endif
  9215. #if SANITIZER_INTERCEPT_GETRANDOM
  9216. INTERCEPTOR(SSIZE_T, getrandom, void *buf, SIZE_T buflen, unsigned int flags) {
  9217. void *ctx;
  9218. COMMON_INTERCEPTOR_ENTER(ctx, getrandom, buf, buflen, flags);
  9219. SSIZE_T n = REAL(getrandom)(buf, buflen, flags);
  9220. if (n > 0) {
  9221. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, n);
  9222. }
  9223. return n;
  9224. }
  9225. #define INIT_GETRANDOM COMMON_INTERCEPT_FUNCTION(getrandom)
  9226. #else
  9227. #define INIT_GETRANDOM
  9228. #endif
  9229. #if SANITIZER_INTERCEPT_CRYPT
  9230. INTERCEPTOR(char *, crypt, char *key, char *salt) {
  9231. void *ctx;
  9232. COMMON_INTERCEPTOR_ENTER(ctx, crypt, key, salt);
  9233. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, internal_strlen(key) + 1);
  9234. COMMON_INTERCEPTOR_READ_RANGE(ctx, salt, internal_strlen(salt) + 1);
  9235. char *res = REAL(crypt)(key, salt);
  9236. if (res != nullptr)
  9237. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  9238. return res;
  9239. }
  9240. #define INIT_CRYPT COMMON_INTERCEPT_FUNCTION(crypt);
  9241. #else
  9242. #define INIT_CRYPT
  9243. #endif
  9244. #if SANITIZER_INTERCEPT_CRYPT_R
  9245. INTERCEPTOR(char *, crypt_r, char *key, char *salt, void *data) {
  9246. void *ctx;
  9247. COMMON_INTERCEPTOR_ENTER(ctx, crypt_r, key, salt, data);
  9248. COMMON_INTERCEPTOR_READ_RANGE(ctx, key, internal_strlen(key) + 1);
  9249. COMMON_INTERCEPTOR_READ_RANGE(ctx, salt, internal_strlen(salt) + 1);
  9250. char *res = REAL(crypt_r)(key, salt, data);
  9251. if (res != nullptr) {
  9252. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, data,
  9253. __sanitizer::struct_crypt_data_sz);
  9254. COMMON_INTERCEPTOR_INITIALIZE_RANGE(res, internal_strlen(res) + 1);
  9255. }
  9256. return res;
  9257. }
  9258. #define INIT_CRYPT_R COMMON_INTERCEPT_FUNCTION(crypt_r);
  9259. #else
  9260. #define INIT_CRYPT_R
  9261. #endif
  9262. #if SANITIZER_INTERCEPT_GETENTROPY
  9263. INTERCEPTOR(int, getentropy, void *buf, SIZE_T buflen) {
  9264. void *ctx;
  9265. COMMON_INTERCEPTOR_ENTER(ctx, getentropy, buf, buflen);
  9266. int r = REAL(getentropy)(buf, buflen);
  9267. if (r == 0) {
  9268. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, buf, buflen);
  9269. }
  9270. return r;
  9271. }
  9272. #define INIT_GETENTROPY COMMON_INTERCEPT_FUNCTION(getentropy)
  9273. #else
  9274. #define INIT_GETENTROPY
  9275. #endif
  9276. #if SANITIZER_INTERCEPT_QSORT_R
  9277. typedef int (*qsort_r_compar_f)(const void *, const void *, void *);
  9278. struct qsort_r_compar_params {
  9279. SIZE_T size;
  9280. qsort_r_compar_f compar;
  9281. void *arg;
  9282. };
  9283. static int wrapped_qsort_r_compar(const void *a, const void *b, void *arg) {
  9284. qsort_r_compar_params *params = (qsort_r_compar_params *)arg;
  9285. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9286. COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, params->size);
  9287. COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, params->size);
  9288. return params->compar(a, b, params->arg);
  9289. }
  9290. INTERCEPTOR(void, qsort_r, void *base, SIZE_T nmemb, SIZE_T size,
  9291. qsort_r_compar_f compar, void *arg) {
  9292. void *ctx;
  9293. COMMON_INTERCEPTOR_ENTER(ctx, qsort_r, base, nmemb, size, compar, arg);
  9294. // Run the comparator over all array elements to detect any memory issues.
  9295. if (nmemb > 1) {
  9296. for (SIZE_T i = 0; i < nmemb - 1; ++i) {
  9297. void *p = (void *)((char *)base + i * size);
  9298. void *q = (void *)((char *)base + (i + 1) * size);
  9299. COMMON_INTERCEPTOR_UNPOISON_PARAM(3);
  9300. compar(p, q, arg);
  9301. }
  9302. }
  9303. qsort_r_compar_params params = {size, compar, arg};
  9304. REAL(qsort_r)(base, nmemb, size, wrapped_qsort_r_compar, &params);
  9305. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
  9306. }
  9307. # define INIT_QSORT_R COMMON_INTERCEPT_FUNCTION(qsort_r)
  9308. #else
  9309. # define INIT_QSORT_R
  9310. #endif
  9311. #if SANITIZER_INTERCEPT_QSORT && SANITIZER_INTERCEPT_QSORT_R
  9312. INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
  9313. qsort_r_compar_f compar) {
  9314. void *ctx;
  9315. COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
  9316. WRAP(qsort_r)(base, nmemb, size, compar, nullptr);
  9317. }
  9318. # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
  9319. #elif SANITIZER_INTERCEPT_QSORT && !SANITIZER_INTERCEPT_QSORT_R
  9320. // Glibc qsort uses a temporary buffer allocated either on stack or on heap.
  9321. // Poisoned memory from there may get copied into the comparator arguments,
  9322. // where it needs to be dealt with. But even that is not enough - the results of
  9323. // the sort may be copied into the input/output array based on the results of
  9324. // the comparator calls, but directly from the temp memory, bypassing the
  9325. // unpoisoning done in wrapped_qsort_compar. We deal with this by, again,
  9326. // unpoisoning the entire array after the sort is done.
  9327. //
  9328. // We can not check that the entire array is initialized at the beginning. IMHO,
  9329. // it's fine for parts of the sorted objects to contain uninitialized memory,
  9330. // ex. as padding in structs.
  9331. typedef int (*qsort_compar_f)(const void *, const void *);
  9332. static THREADLOCAL qsort_compar_f qsort_compar;
  9333. static THREADLOCAL SIZE_T qsort_size;
  9334. static int wrapped_qsort_compar(const void *a, const void *b) {
  9335. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  9336. COMMON_INTERCEPTOR_INITIALIZE_RANGE(a, qsort_size);
  9337. COMMON_INTERCEPTOR_INITIALIZE_RANGE(b, qsort_size);
  9338. return qsort_compar(a, b);
  9339. }
  9340. INTERCEPTOR(void, qsort, void *base, SIZE_T nmemb, SIZE_T size,
  9341. qsort_compar_f compar) {
  9342. void *ctx;
  9343. COMMON_INTERCEPTOR_ENTER(ctx, qsort, base, nmemb, size, compar);
  9344. // Run the comparator over all array elements to detect any memory issues.
  9345. if (nmemb > 1) {
  9346. for (SIZE_T i = 0; i < nmemb - 1; ++i) {
  9347. void *p = (void *)((char *)base + i * size);
  9348. void *q = (void *)((char *)base + (i + 1) * size);
  9349. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  9350. compar(p, q);
  9351. }
  9352. }
  9353. qsort_compar_f old_compar = qsort_compar;
  9354. SIZE_T old_size = qsort_size;
  9355. // Handle qsort() implementations that recurse using an
  9356. // interposable function call:
  9357. bool already_wrapped = compar == wrapped_qsort_compar;
  9358. if (already_wrapped) {
  9359. // This case should only happen if the qsort() implementation calls itself
  9360. // using a preemptible function call (e.g. the FreeBSD libc version).
  9361. // Check that the size and comparator arguments are as expected.
  9362. CHECK_NE(compar, qsort_compar);
  9363. CHECK_EQ(qsort_size, size);
  9364. } else {
  9365. qsort_compar = compar;
  9366. qsort_size = size;
  9367. }
  9368. REAL(qsort)(base, nmemb, size, wrapped_qsort_compar);
  9369. if (!already_wrapped) {
  9370. qsort_compar = old_compar;
  9371. qsort_size = old_size;
  9372. }
  9373. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, base, nmemb * size);
  9374. }
  9375. # define INIT_QSORT COMMON_INTERCEPT_FUNCTION(qsort)
  9376. #else
  9377. # define INIT_QSORT
  9378. #endif
  9379. #if SANITIZER_INTERCEPT_BSEARCH
  9380. typedef int (*bsearch_compar_f)(const void *, const void *);
  9381. struct bsearch_compar_params {
  9382. const void *key;
  9383. bsearch_compar_f compar;
  9384. };
  9385. static int wrapped_bsearch_compar(const void *key, const void *b) {
  9386. const bsearch_compar_params *params = (const bsearch_compar_params *)key;
  9387. COMMON_INTERCEPTOR_UNPOISON_PARAM(2);
  9388. return params->compar(params->key, b);
  9389. }
  9390. INTERCEPTOR(void *, bsearch, const void *key, const void *base, SIZE_T nmemb,
  9391. SIZE_T size, bsearch_compar_f compar) {
  9392. void *ctx;
  9393. COMMON_INTERCEPTOR_ENTER(ctx, bsearch, key, base, nmemb, size, compar);
  9394. bsearch_compar_params params = {key, compar};
  9395. return REAL(bsearch)(&params, base, nmemb, size, wrapped_bsearch_compar);
  9396. }
  9397. # define INIT_BSEARCH COMMON_INTERCEPT_FUNCTION(bsearch)
  9398. #else
  9399. # define INIT_BSEARCH
  9400. #endif
  9401. #if SANITIZER_INTERCEPT_SIGALTSTACK
  9402. INTERCEPTOR(int, sigaltstack, void *ss, void *oss) {
  9403. void *ctx;
  9404. COMMON_INTERCEPTOR_ENTER(ctx, sigaltstack, ss, oss);
  9405. int r = REAL(sigaltstack)(ss, oss);
  9406. if (r == 0 && oss != nullptr) {
  9407. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, oss, struct_stack_t_sz);
  9408. }
  9409. return r;
  9410. }
  9411. #define INIT_SIGALTSTACK COMMON_INTERCEPT_FUNCTION(sigaltstack)
  9412. #else
  9413. #define INIT_SIGALTSTACK
  9414. #endif
  9415. #if SANITIZER_INTERCEPT_UNAME
  9416. INTERCEPTOR(int, uname, struct utsname *utsname) {
  9417. #if SANITIZER_LINUX
  9418. if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED)
  9419. return internal_uname(utsname);
  9420. #endif
  9421. void *ctx;
  9422. COMMON_INTERCEPTOR_ENTER(ctx, uname, utsname);
  9423. int res = REAL(uname)(utsname);
  9424. if (!res)
  9425. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
  9426. __sanitizer::struct_utsname_sz);
  9427. return res;
  9428. }
  9429. #define INIT_UNAME COMMON_INTERCEPT_FUNCTION(uname)
  9430. #else
  9431. #define INIT_UNAME
  9432. #endif
  9433. #if SANITIZER_INTERCEPT___XUNAME
  9434. // FreeBSD's <sys/utsname.h> define uname() as
  9435. // static __inline int uname(struct utsname *name) {
  9436. // return __xuname(SYS_NMLN, (void*)name);
  9437. // }
  9438. INTERCEPTOR(int, __xuname, int size, void *utsname) {
  9439. void *ctx;
  9440. COMMON_INTERCEPTOR_ENTER(ctx, __xuname, size, utsname);
  9441. int res = REAL(__xuname)(size, utsname);
  9442. if (!res)
  9443. COMMON_INTERCEPTOR_WRITE_RANGE(ctx, utsname,
  9444. __sanitizer::struct_utsname_sz);
  9445. return res;
  9446. }
  9447. #define INIT___XUNAME COMMON_INTERCEPT_FUNCTION(__xuname)
  9448. #else
  9449. #define INIT___XUNAME
  9450. #endif
  9451. #include "sanitizer_common_interceptors_netbsd_compat.inc"
  9452. static void InitializeCommonInterceptors() {
  9453. #if SI_POSIX
  9454. static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
  9455. interceptor_metadata_map = new ((void *)&metadata_mem) MetadataHashMap();
  9456. #endif
  9457. INIT_MMAP;
  9458. INIT_MMAP64;
  9459. INIT_TEXTDOMAIN;
  9460. INIT_STRLEN;
  9461. INIT_STRNLEN;
  9462. INIT_STRNDUP;
  9463. INIT___STRNDUP;
  9464. INIT_STRCMP;
  9465. INIT_STRNCMP;
  9466. INIT_STRCASECMP;
  9467. INIT_STRNCASECMP;
  9468. INIT_STRSTR;
  9469. INIT_STRCASESTR;
  9470. INIT_STRCHR;
  9471. INIT_STRCHRNUL;
  9472. INIT_STRRCHR;
  9473. INIT_STRSPN;
  9474. INIT_STRTOK;
  9475. INIT_STRPBRK;
  9476. INIT_STRXFRM;
  9477. INIT___STRXFRM_L;
  9478. INIT_MEMSET;
  9479. INIT_MEMMOVE;
  9480. INIT_MEMCPY;
  9481. INIT_MEMCHR;
  9482. INIT_MEMCMP;
  9483. INIT_BCMP;
  9484. INIT_MEMRCHR;
  9485. INIT_MEMMEM;
  9486. INIT_READ;
  9487. INIT_FREAD;
  9488. INIT_PREAD;
  9489. INIT_PREAD64;
  9490. INIT_READV;
  9491. INIT_PREADV;
  9492. INIT_PREADV64;
  9493. INIT_WRITE;
  9494. INIT_FWRITE;
  9495. INIT_PWRITE;
  9496. INIT_PWRITE64;
  9497. INIT_WRITEV;
  9498. INIT_PWRITEV;
  9499. INIT_PWRITEV64;
  9500. INIT_FGETS;
  9501. INIT_FPUTS;
  9502. INIT_PUTS;
  9503. INIT_PRCTL;
  9504. INIT_LOCALTIME_AND_FRIENDS;
  9505. INIT_STRPTIME;
  9506. INIT_SCANF;
  9507. INIT_ISOC99_SCANF;
  9508. INIT_PRINTF;
  9509. INIT_PRINTF_L;
  9510. INIT_ISOC99_PRINTF;
  9511. INIT_FREXP;
  9512. INIT_FREXPF_FREXPL;
  9513. INIT_GETPWNAM_AND_FRIENDS;
  9514. INIT_GETPWNAM_R_AND_FRIENDS;
  9515. INIT_GETPWENT;
  9516. INIT_FGETPWENT;
  9517. INIT_GETPWENT_R;
  9518. INIT_FGETPWENT_R;
  9519. INIT_FGETGRENT_R;
  9520. INIT_SETPWENT;
  9521. INIT_CLOCK_GETTIME;
  9522. INIT_CLOCK_GETCPUCLOCKID;
  9523. INIT_GETITIMER;
  9524. INIT_TIME;
  9525. INIT_GLOB;
  9526. INIT_GLOB64;
  9527. INIT_POSIX_SPAWN;
  9528. INIT_WAIT;
  9529. INIT_WAIT4;
  9530. INIT_INET;
  9531. INIT_PTHREAD_GETSCHEDPARAM;
  9532. INIT_GETADDRINFO;
  9533. INIT_GETNAMEINFO;
  9534. INIT_GETSOCKNAME;
  9535. INIT_GETHOSTBYNAME;
  9536. INIT_GETHOSTBYNAME2;
  9537. INIT_GETHOSTBYNAME_R;
  9538. INIT_GETHOSTBYNAME2_R;
  9539. INIT_GETHOSTBYADDR_R;
  9540. INIT_GETHOSTENT_R;
  9541. INIT_GETSOCKOPT;
  9542. INIT_ACCEPT;
  9543. INIT_ACCEPT4;
  9544. INIT_PACCEPT;
  9545. INIT_MODF;
  9546. INIT_RECVMSG;
  9547. INIT_SENDMSG;
  9548. INIT_RECVMMSG;
  9549. INIT_SENDMMSG;
  9550. INIT_SYSMSG;
  9551. INIT_GETPEERNAME;
  9552. INIT_IOCTL;
  9553. INIT_INET_ATON;
  9554. INIT_SYSINFO;
  9555. INIT_READDIR;
  9556. INIT_READDIR64;
  9557. INIT_PTRACE;
  9558. INIT_SETLOCALE;
  9559. INIT_GETCWD;
  9560. INIT_GET_CURRENT_DIR_NAME;
  9561. INIT_STRTOIMAX;
  9562. INIT_MBSTOWCS;
  9563. INIT_MBSNRTOWCS;
  9564. INIT_WCSTOMBS;
  9565. INIT_WCSNRTOMBS;
  9566. INIT_WCRTOMB;
  9567. INIT_WCTOMB;
  9568. INIT_TCGETATTR;
  9569. INIT_REALPATH;
  9570. INIT_CANONICALIZE_FILE_NAME;
  9571. INIT_CONFSTR;
  9572. INIT_SCHED_GETAFFINITY;
  9573. INIT_SCHED_GETPARAM;
  9574. INIT_STRERROR;
  9575. INIT_STRERROR_R;
  9576. INIT_XPG_STRERROR_R;
  9577. INIT_SCANDIR;
  9578. INIT_SCANDIR64;
  9579. INIT_GETGROUPS;
  9580. INIT_POLL;
  9581. INIT_PPOLL;
  9582. INIT_WORDEXP;
  9583. INIT_SIGWAIT;
  9584. INIT_SIGWAITINFO;
  9585. INIT_SIGTIMEDWAIT;
  9586. INIT_SIGSETOPS;
  9587. INIT_SIGSET_LOGICOPS;
  9588. INIT_SIGPENDING;
  9589. INIT_SIGPROCMASK;
  9590. INIT_PTHREAD_SIGMASK;
  9591. INIT_BACKTRACE;
  9592. INIT__EXIT;
  9593. INIT_PTHREAD_MUTEX_LOCK;
  9594. INIT_PTHREAD_MUTEX_UNLOCK;
  9595. INIT___PTHREAD_MUTEX_LOCK;
  9596. INIT___PTHREAD_MUTEX_UNLOCK;
  9597. INIT___LIBC_MUTEX_LOCK;
  9598. INIT___LIBC_MUTEX_UNLOCK;
  9599. INIT___LIBC_THR_SETCANCELSTATE;
  9600. INIT_GETMNTENT;
  9601. INIT_GETMNTENT_R;
  9602. INIT_STATFS;
  9603. INIT_STATFS64;
  9604. INIT_STATVFS;
  9605. INIT_STATVFS64;
  9606. INIT_INITGROUPS;
  9607. INIT_ETHER_NTOA_ATON;
  9608. INIT_ETHER_HOST;
  9609. INIT_ETHER_R;
  9610. INIT_SHMCTL;
  9611. INIT_RANDOM_R;
  9612. INIT_PTHREAD_ATTR_GET;
  9613. INIT_PTHREAD_ATTR_GET_SCHED;
  9614. INIT_PTHREAD_ATTR_GETINHERITSCHED;
  9615. INIT_PTHREAD_ATTR_GETAFFINITY_NP;
  9616. INIT_PTHREAD_MUTEXATTR_GETPSHARED;
  9617. INIT_PTHREAD_MUTEXATTR_GETTYPE;
  9618. INIT_PTHREAD_MUTEXATTR_GETPROTOCOL;
  9619. INIT_PTHREAD_MUTEXATTR_GETPRIOCEILING;
  9620. INIT_PTHREAD_MUTEXATTR_GETROBUST;
  9621. INIT_PTHREAD_MUTEXATTR_GETROBUST_NP;
  9622. INIT_PTHREAD_RWLOCKATTR_GETPSHARED;
  9623. INIT_PTHREAD_RWLOCKATTR_GETKIND_NP;
  9624. INIT_PTHREAD_CONDATTR_GETPSHARED;
  9625. INIT_PTHREAD_CONDATTR_GETCLOCK;
  9626. INIT_PTHREAD_BARRIERATTR_GETPSHARED;
  9627. INIT_TMPNAM;
  9628. INIT_TMPNAM_R;
  9629. INIT_PTSNAME;
  9630. INIT_PTSNAME_R;
  9631. INIT_TTYNAME;
  9632. INIT_TTYNAME_R;
  9633. INIT_TEMPNAM;
  9634. INIT_PTHREAD_SETNAME_NP;
  9635. INIT_PTHREAD_GETNAME_NP;
  9636. INIT_SINCOS;
  9637. INIT_REMQUO;
  9638. INIT_REMQUOL;
  9639. INIT_LGAMMA;
  9640. INIT_LGAMMAL;
  9641. INIT_LGAMMA_R;
  9642. INIT_LGAMMAL_R;
  9643. INIT_DRAND48_R;
  9644. INIT_RAND_R;
  9645. INIT_GETLINE;
  9646. INIT_ICONV;
  9647. INIT_TIMES;
  9648. INIT_TLS_GET_ADDR;
  9649. INIT_LISTXATTR;
  9650. INIT_GETXATTR;
  9651. INIT_GETRESID;
  9652. INIT_GETIFADDRS;
  9653. INIT_IF_INDEXTONAME;
  9654. INIT_CAPGET;
  9655. INIT_AEABI_MEM;
  9656. INIT___BZERO;
  9657. INIT_BZERO;
  9658. INIT_FTIME;
  9659. INIT_XDR;
  9660. INIT_XDRREC_LINUX;
  9661. INIT_TSEARCH;
  9662. INIT_LIBIO_INTERNALS;
  9663. INIT_FOPEN;
  9664. INIT_FOPEN64;
  9665. INIT_FLOPEN;
  9666. INIT_OPEN_MEMSTREAM;
  9667. INIT_OBSTACK;
  9668. INIT_FFLUSH;
  9669. INIT_FCLOSE;
  9670. INIT_DLOPEN_DLCLOSE;
  9671. INIT_GETPASS;
  9672. INIT_TIMERFD;
  9673. INIT_MLOCKX;
  9674. INIT_FOPENCOOKIE;
  9675. INIT_SEM;
  9676. INIT_PTHREAD_SETCANCEL;
  9677. INIT_MINCORE;
  9678. INIT_PROCESS_VM_READV;
  9679. INIT_CTERMID;
  9680. INIT_CTERMID_R;
  9681. INIT_RECV_RECVFROM;
  9682. INIT_SEND_SENDTO;
  9683. INIT_STAT;
  9684. INIT_EVENTFD_READ_WRITE;
  9685. INIT_LSTAT;
  9686. INIT___XSTAT;
  9687. INIT___XSTAT64;
  9688. INIT___LXSTAT;
  9689. INIT___LXSTAT64;
  9690. // FIXME: add other *stat interceptors.
  9691. INIT_UTMP;
  9692. INIT_UTMPX;
  9693. INIT_GETLOADAVG;
  9694. INIT_WCSLEN;
  9695. INIT_WCSCAT;
  9696. INIT_WCSDUP;
  9697. INIT_WCSXFRM;
  9698. INIT___WCSXFRM_L;
  9699. INIT_ACCT;
  9700. INIT_USER_FROM_UID;
  9701. INIT_UID_FROM_USER;
  9702. INIT_GROUP_FROM_GID;
  9703. INIT_GID_FROM_GROUP;
  9704. INIT_ACCESS;
  9705. INIT_FACCESSAT;
  9706. INIT_GETGROUPLIST;
  9707. INIT_GETGROUPMEMBERSHIP;
  9708. INIT_READLINK;
  9709. INIT_READLINKAT;
  9710. INIT_NAME_TO_HANDLE_AT;
  9711. INIT_OPEN_BY_HANDLE_AT;
  9712. INIT_STRLCPY;
  9713. INIT_DEVNAME;
  9714. INIT_DEVNAME_R;
  9715. INIT_FGETLN;
  9716. INIT_STRMODE;
  9717. INIT_TTYENT;
  9718. INIT_PROTOENT;
  9719. INIT_PROTOENT_R;
  9720. INIT_NETENT;
  9721. INIT_GETMNTINFO;
  9722. INIT_MI_VECTOR_HASH;
  9723. INIT_SETVBUF;
  9724. INIT_GETVFSSTAT;
  9725. INIT_REGEX;
  9726. INIT_REGEXSUB;
  9727. INIT_FTS;
  9728. INIT_SYSCTL;
  9729. INIT_ASYSCTL;
  9730. INIT_SYSCTLGETMIBINFO;
  9731. INIT_NL_LANGINFO;
  9732. INIT_MODCTL;
  9733. INIT_STRTONUM;
  9734. INIT_FPARSELN;
  9735. INIT_STATVFS1;
  9736. INIT_STRTOI;
  9737. INIT_CAPSICUM;
  9738. INIT_SHA1;
  9739. INIT_MD4;
  9740. INIT_RMD160;
  9741. INIT_MD5;
  9742. INIT_FSEEK;
  9743. INIT_MD2;
  9744. INIT_SHA2;
  9745. INIT_VIS;
  9746. INIT_CDB;
  9747. INIT_GETFSENT;
  9748. INIT_ARC4RANDOM;
  9749. INIT_POPEN;
  9750. INIT_POPENVE;
  9751. INIT_PCLOSE;
  9752. INIT_FUNOPEN;
  9753. INIT_FUNOPEN2;
  9754. INIT_FDEVNAME;
  9755. INIT_GETUSERSHELL;
  9756. INIT_SL_INIT;
  9757. INIT_GETRANDOM;
  9758. INIT_CRYPT;
  9759. INIT_CRYPT_R;
  9760. INIT_GETENTROPY;
  9761. INIT_QSORT;
  9762. INIT_QSORT_R;
  9763. INIT_BSEARCH;
  9764. INIT_SIGALTSTACK;
  9765. INIT_UNAME;
  9766. INIT___XUNAME;
  9767. INIT___PRINTF_CHK;
  9768. }