ffi.c 229 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Dmitry Stogov <dmitry@zend.com> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. # include "config.h"
  18. #endif
  19. #include "php.h"
  20. #include "php_ffi.h"
  21. #include "ext/standard/info.h"
  22. #include "php_scandir.h"
  23. #include "zend_exceptions.h"
  24. #include "zend_closures.h"
  25. #include "main/SAPI.h"
  26. #include "ffi_arginfo.h"
  27. #include <ffi.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #ifdef HAVE_GLOB
  32. #ifdef PHP_WIN32
  33. #include "win32/glob.h"
  34. #else
  35. #include <glob.h>
  36. #endif
  37. #endif
  38. #ifndef __BIGGEST_ALIGNMENT__
  39. /* XXX need something better, perhaps with regard to SIMD, etc. */
  40. # define __BIGGEST_ALIGNMENT__ sizeof(size_t)
  41. #endif
  42. ZEND_DECLARE_MODULE_GLOBALS(ffi)
  43. typedef enum _zend_ffi_tag_kind {
  44. ZEND_FFI_TAG_ENUM,
  45. ZEND_FFI_TAG_STRUCT,
  46. ZEND_FFI_TAG_UNION
  47. } zend_ffi_tag_kind;
  48. static const char *zend_ffi_tag_kind_name[3] = {"enum", "struct", "union"};
  49. typedef struct _zend_ffi_tag {
  50. zend_ffi_tag_kind kind;
  51. zend_ffi_type *type;
  52. } zend_ffi_tag;
  53. typedef enum _zend_ffi_type_kind {
  54. ZEND_FFI_TYPE_VOID,
  55. ZEND_FFI_TYPE_FLOAT,
  56. ZEND_FFI_TYPE_DOUBLE,
  57. #ifdef HAVE_LONG_DOUBLE
  58. ZEND_FFI_TYPE_LONGDOUBLE,
  59. #endif
  60. ZEND_FFI_TYPE_UINT8,
  61. ZEND_FFI_TYPE_SINT8,
  62. ZEND_FFI_TYPE_UINT16,
  63. ZEND_FFI_TYPE_SINT16,
  64. ZEND_FFI_TYPE_UINT32,
  65. ZEND_FFI_TYPE_SINT32,
  66. ZEND_FFI_TYPE_UINT64,
  67. ZEND_FFI_TYPE_SINT64,
  68. ZEND_FFI_TYPE_ENUM,
  69. ZEND_FFI_TYPE_BOOL,
  70. ZEND_FFI_TYPE_CHAR,
  71. ZEND_FFI_TYPE_POINTER,
  72. ZEND_FFI_TYPE_FUNC,
  73. ZEND_FFI_TYPE_ARRAY,
  74. ZEND_FFI_TYPE_STRUCT,
  75. } zend_ffi_type_kind;
  76. typedef enum _zend_ffi_flags {
  77. ZEND_FFI_FLAG_CONST = (1 << 0),
  78. ZEND_FFI_FLAG_OWNED = (1 << 1),
  79. ZEND_FFI_FLAG_PERSISTENT = (1 << 2),
  80. } zend_ffi_flags;
  81. struct _zend_ffi_type {
  82. zend_ffi_type_kind kind;
  83. size_t size;
  84. uint32_t align;
  85. uint32_t attr;
  86. union {
  87. struct {
  88. zend_string *tag_name;
  89. zend_ffi_type_kind kind;
  90. } enumeration;
  91. struct {
  92. zend_ffi_type *type;
  93. zend_long length;
  94. } array;
  95. struct {
  96. zend_ffi_type *type;
  97. } pointer;
  98. struct {
  99. zend_string *tag_name;
  100. HashTable fields;
  101. } record;
  102. struct {
  103. zend_ffi_type *ret_type;
  104. HashTable *args;
  105. ffi_abi abi;
  106. } func;
  107. };
  108. };
  109. typedef struct _zend_ffi_field {
  110. size_t offset;
  111. bool is_const;
  112. bool is_nested; /* part of nested anonymous struct */
  113. uint8_t first_bit;
  114. uint8_t bits;
  115. zend_ffi_type *type;
  116. } zend_ffi_field;
  117. typedef enum _zend_ffi_symbol_kind {
  118. ZEND_FFI_SYM_TYPE,
  119. ZEND_FFI_SYM_CONST,
  120. ZEND_FFI_SYM_VAR,
  121. ZEND_FFI_SYM_FUNC
  122. } zend_ffi_symbol_kind;
  123. typedef struct _zend_ffi_symbol {
  124. zend_ffi_symbol_kind kind;
  125. bool is_const;
  126. zend_ffi_type *type;
  127. union {
  128. void *addr;
  129. int64_t value;
  130. };
  131. } zend_ffi_symbol;
  132. typedef struct _zend_ffi_scope {
  133. HashTable *symbols;
  134. HashTable *tags;
  135. } zend_ffi_scope;
  136. typedef struct _zend_ffi {
  137. zend_object std;
  138. DL_HANDLE lib;
  139. HashTable *symbols;
  140. HashTable *tags;
  141. bool persistent;
  142. } zend_ffi;
  143. #define ZEND_FFI_TYPE_OWNED (1<<0)
  144. #define ZEND_FFI_TYPE(t) \
  145. ((zend_ffi_type*)(((uintptr_t)(t)) & ~ZEND_FFI_TYPE_OWNED))
  146. #define ZEND_FFI_TYPE_IS_OWNED(t) \
  147. (((uintptr_t)(t)) & ZEND_FFI_TYPE_OWNED)
  148. #define ZEND_FFI_TYPE_MAKE_OWNED(t) \
  149. ((zend_ffi_type*)(((uintptr_t)(t)) | ZEND_FFI_TYPE_OWNED))
  150. #define ZEND_FFI_SIZEOF_ARG \
  151. MAX(FFI_SIZEOF_ARG, sizeof(double))
  152. typedef struct _zend_ffi_cdata {
  153. zend_object std;
  154. zend_ffi_type *type;
  155. void *ptr;
  156. void *ptr_holder;
  157. zend_ffi_flags flags;
  158. } zend_ffi_cdata;
  159. typedef struct _zend_ffi_ctype {
  160. zend_object std;
  161. zend_ffi_type *type;
  162. } zend_ffi_ctype;
  163. static zend_class_entry *zend_ffi_exception_ce;
  164. static zend_class_entry *zend_ffi_parser_exception_ce;
  165. static zend_class_entry *zend_ffi_ce;
  166. static zend_class_entry *zend_ffi_cdata_ce;
  167. static zend_class_entry *zend_ffi_ctype_ce;
  168. static zend_object_handlers zend_ffi_handlers;
  169. static zend_object_handlers zend_ffi_cdata_handlers;
  170. static zend_object_handlers zend_ffi_cdata_value_handlers;
  171. static zend_object_handlers zend_ffi_cdata_free_handlers;
  172. static zend_object_handlers zend_ffi_ctype_handlers;
  173. static zend_internal_function zend_ffi_new_fn;
  174. static zend_internal_function zend_ffi_cast_fn;
  175. static zend_internal_function zend_ffi_type_fn;
  176. /* forward declarations */
  177. static void _zend_ffi_type_dtor(zend_ffi_type *type);
  178. static void zend_ffi_finalize_type(zend_ffi_dcl *dcl);
  179. static int zend_ffi_is_same_type(zend_ffi_type *type1, zend_ffi_type *type2);
  180. static zend_ffi_type *zend_ffi_remember_type(zend_ffi_type *type);
  181. static char *zend_ffi_parse_directives(const char *filename, char *code_pos, char **scope_name, char **lib, bool preload);
  182. static ZEND_FUNCTION(ffi_trampoline);
  183. static ZEND_COLD void zend_ffi_return_unsupported(zend_ffi_type *type);
  184. static ZEND_COLD void zend_ffi_pass_unsupported(zend_ffi_type *type);
  185. static ZEND_COLD void zend_ffi_assign_incompatible(zval *arg, zend_ffi_type *type);
  186. #if FFI_CLOSURES
  187. static void *zend_ffi_create_callback(zend_ffi_type *type, zval *value);
  188. #endif
  189. static zend_always_inline void zend_ffi_type_dtor(zend_ffi_type *type) /* {{{ */
  190. {
  191. if (UNEXPECTED(ZEND_FFI_TYPE_IS_OWNED(type))) {
  192. _zend_ffi_type_dtor(type);
  193. return;
  194. }
  195. }
  196. /* }}} */
  197. static zend_always_inline void zend_ffi_object_init(zend_object *object, zend_class_entry *ce) /* {{{ */
  198. {
  199. GC_SET_REFCOUNT(object, 1);
  200. GC_TYPE_INFO(object) = GC_OBJECT | (IS_OBJ_DESTRUCTOR_CALLED << GC_FLAGS_SHIFT);
  201. object->ce = ce;
  202. object->properties = NULL;
  203. zend_objects_store_put(object);
  204. }
  205. /* }}} */
  206. static zend_object *zend_ffi_cdata_new(zend_class_entry *class_type) /* {{{ */
  207. {
  208. zend_ffi_cdata *cdata;
  209. cdata = emalloc(sizeof(zend_ffi_cdata));
  210. zend_ffi_object_init(&cdata->std, class_type);
  211. cdata->std.handlers = &zend_ffi_cdata_handlers;
  212. cdata->type = NULL;
  213. cdata->ptr = NULL;
  214. cdata->flags = 0;
  215. return &cdata->std;
  216. }
  217. /* }}} */
  218. static int zend_ffi_is_compatible_type(zend_ffi_type *dst_type, zend_ffi_type *src_type) /* {{{ */
  219. {
  220. while (1) {
  221. if (dst_type == src_type) {
  222. return 1;
  223. } else if (dst_type->kind == src_type->kind) {
  224. if (dst_type->kind < ZEND_FFI_TYPE_POINTER) {
  225. return 1;
  226. } else if (dst_type->kind == ZEND_FFI_TYPE_POINTER) {
  227. dst_type = ZEND_FFI_TYPE(dst_type->pointer.type);
  228. src_type = ZEND_FFI_TYPE(src_type->pointer.type);
  229. if (dst_type->kind == ZEND_FFI_TYPE_VOID ||
  230. src_type->kind == ZEND_FFI_TYPE_VOID) {
  231. return 1;
  232. }
  233. } else if (dst_type->kind == ZEND_FFI_TYPE_ARRAY &&
  234. (dst_type->array.length == src_type->array.length ||
  235. dst_type->array.length == 0)) {
  236. dst_type = ZEND_FFI_TYPE(dst_type->array.type);
  237. src_type = ZEND_FFI_TYPE(src_type->array.type);
  238. } else {
  239. break;
  240. }
  241. } else if (dst_type->kind == ZEND_FFI_TYPE_POINTER &&
  242. src_type->kind == ZEND_FFI_TYPE_ARRAY) {
  243. dst_type = ZEND_FFI_TYPE(dst_type->pointer.type);
  244. src_type = ZEND_FFI_TYPE(src_type->array.type);
  245. if (dst_type->kind == ZEND_FFI_TYPE_VOID) {
  246. return 1;
  247. }
  248. } else {
  249. break;
  250. }
  251. }
  252. return 0;
  253. }
  254. /* }}} */
  255. static ffi_type* zend_ffi_face_struct_add_fields(ffi_type* t, zend_ffi_type *type, int *i, size_t size)
  256. {
  257. zend_ffi_field *field;
  258. ZEND_HASH_FOREACH_PTR(&type->record.fields, field) {
  259. switch (ZEND_FFI_TYPE(field->type)->kind) {
  260. case ZEND_FFI_TYPE_FLOAT:
  261. t->elements[(*i)++] = &ffi_type_float;
  262. break;
  263. case ZEND_FFI_TYPE_DOUBLE:
  264. t->elements[(*i)++] = &ffi_type_double;
  265. break;
  266. #ifdef HAVE_LONG_DOUBLE
  267. case ZEND_FFI_TYPE_LONGDOUBLE:
  268. t->elements[(*i)++] = &ffi_type_longdouble;
  269. break;
  270. #endif
  271. case ZEND_FFI_TYPE_SINT8:
  272. case ZEND_FFI_TYPE_UINT8:
  273. case ZEND_FFI_TYPE_BOOL:
  274. case ZEND_FFI_TYPE_CHAR:
  275. t->elements[(*i)++] = &ffi_type_uint8;
  276. break;
  277. case ZEND_FFI_TYPE_SINT16:
  278. case ZEND_FFI_TYPE_UINT16:
  279. t->elements[(*i)++] = &ffi_type_uint16;
  280. break;
  281. case ZEND_FFI_TYPE_SINT32:
  282. case ZEND_FFI_TYPE_UINT32:
  283. t->elements[(*i)++] = &ffi_type_uint32;
  284. break;
  285. case ZEND_FFI_TYPE_SINT64:
  286. case ZEND_FFI_TYPE_UINT64:
  287. t->elements[(*i)++] = &ffi_type_uint64;
  288. break;
  289. case ZEND_FFI_TYPE_POINTER:
  290. t->elements[(*i)++] = &ffi_type_pointer;
  291. break;
  292. case ZEND_FFI_TYPE_STRUCT: {
  293. zend_ffi_type *field_type = ZEND_FFI_TYPE(field->type);
  294. /* for unions we use only the first field */
  295. int num_fields = !(field_type->attr & ZEND_FFI_ATTR_UNION) ?
  296. zend_hash_num_elements(&field_type->record.fields) : 1;
  297. if (num_fields > 1) {
  298. size += sizeof(ffi_type*) * (num_fields - 1);
  299. t = erealloc(t, size);
  300. t->elements = (ffi_type**)(t + 1);
  301. }
  302. t = zend_ffi_face_struct_add_fields(t, field_type, i, size);
  303. break;
  304. }
  305. default:
  306. t->elements[(*i)++] = &ffi_type_void;
  307. break;
  308. }
  309. if (type->attr & ZEND_FFI_ATTR_UNION) {
  310. /* for unions we use only the first field */
  311. break;
  312. }
  313. } ZEND_HASH_FOREACH_END();
  314. return t;
  315. }
  316. static ffi_type *zend_ffi_make_fake_struct_type(zend_ffi_type *type) /* {{{ */
  317. {
  318. /* for unions we use only the first field */
  319. int num_fields = !(type->attr & ZEND_FFI_ATTR_UNION) ?
  320. zend_hash_num_elements(&type->record.fields) : 1;
  321. size_t size = sizeof(ffi_type) + sizeof(ffi_type*) * (num_fields + 1);
  322. ffi_type *t = emalloc(size);
  323. int i;
  324. t->size = type->size;
  325. t->alignment = type->align;
  326. t->type = FFI_TYPE_STRUCT;
  327. t->elements = (ffi_type**)(t + 1);
  328. i = 0;
  329. t = zend_ffi_face_struct_add_fields(t, type, &i, size);
  330. t->elements[i] = NULL;
  331. return t;
  332. }
  333. /* }}} */
  334. static ffi_type *zend_ffi_get_type(zend_ffi_type *type) /* {{{ */
  335. {
  336. zend_ffi_type_kind kind = type->kind;
  337. again:
  338. switch (kind) {
  339. case ZEND_FFI_TYPE_FLOAT:
  340. return &ffi_type_float;
  341. case ZEND_FFI_TYPE_DOUBLE:
  342. return &ffi_type_double;
  343. #ifdef HAVE_LONG_DOUBLE
  344. case ZEND_FFI_TYPE_LONGDOUBLE:
  345. return &ffi_type_longdouble;
  346. #endif
  347. case ZEND_FFI_TYPE_UINT8:
  348. return &ffi_type_uint8;
  349. case ZEND_FFI_TYPE_SINT8:
  350. return &ffi_type_sint8;
  351. case ZEND_FFI_TYPE_UINT16:
  352. return &ffi_type_uint16;
  353. case ZEND_FFI_TYPE_SINT16:
  354. return &ffi_type_sint16;
  355. case ZEND_FFI_TYPE_UINT32:
  356. return &ffi_type_uint32;
  357. case ZEND_FFI_TYPE_SINT32:
  358. return &ffi_type_sint32;
  359. case ZEND_FFI_TYPE_UINT64:
  360. return &ffi_type_uint64;
  361. case ZEND_FFI_TYPE_SINT64:
  362. return &ffi_type_sint64;
  363. case ZEND_FFI_TYPE_POINTER:
  364. return &ffi_type_pointer;
  365. case ZEND_FFI_TYPE_VOID:
  366. return &ffi_type_void;
  367. case ZEND_FFI_TYPE_BOOL:
  368. return &ffi_type_uint8;
  369. case ZEND_FFI_TYPE_CHAR:
  370. return &ffi_type_sint8;
  371. case ZEND_FFI_TYPE_ENUM:
  372. kind = type->enumeration.kind;
  373. goto again;
  374. case ZEND_FFI_TYPE_STRUCT:
  375. return zend_ffi_make_fake_struct_type(type);
  376. default:
  377. break;
  378. }
  379. return NULL;
  380. }
  381. /* }}} */
  382. static zend_never_inline zend_ffi_cdata *zend_ffi_cdata_to_zval_slow(void *ptr, zend_ffi_type *type, zend_ffi_flags flags) /* {{{ */
  383. {
  384. zend_ffi_cdata *cdata = emalloc(sizeof(zend_ffi_cdata));
  385. zend_ffi_object_init(&cdata->std, zend_ffi_cdata_ce);
  386. cdata->std.handlers =
  387. (type->kind < ZEND_FFI_TYPE_POINTER) ?
  388. &zend_ffi_cdata_value_handlers :
  389. &zend_ffi_cdata_handlers;
  390. cdata->type = type;
  391. cdata->flags = flags;
  392. cdata->ptr = ptr;
  393. return cdata;
  394. }
  395. /* }}} */
  396. static zend_never_inline zend_ffi_cdata *zend_ffi_cdata_to_zval_slow_ptr(void *ptr, zend_ffi_type *type, zend_ffi_flags flags) /* {{{ */
  397. {
  398. zend_ffi_cdata *cdata = emalloc(sizeof(zend_ffi_cdata));
  399. zend_ffi_object_init(&cdata->std, zend_ffi_cdata_ce);
  400. cdata->std.handlers = &zend_ffi_cdata_handlers;
  401. cdata->type = type;
  402. cdata->flags = flags;
  403. cdata->ptr = (void*)&cdata->ptr_holder;
  404. *(void**)cdata->ptr = *(void**)ptr;
  405. return cdata;
  406. }
  407. /* }}} */
  408. static zend_never_inline zend_ffi_cdata *zend_ffi_cdata_to_zval_slow_ret(void *ptr, zend_ffi_type *type, zend_ffi_flags flags) /* {{{ */
  409. {
  410. zend_ffi_cdata *cdata = emalloc(sizeof(zend_ffi_cdata));
  411. zend_ffi_object_init(&cdata->std, zend_ffi_cdata_ce);
  412. cdata->std.handlers =
  413. (type->kind < ZEND_FFI_TYPE_POINTER) ?
  414. &zend_ffi_cdata_value_handlers :
  415. &zend_ffi_cdata_handlers;
  416. cdata->type = type;
  417. cdata->flags = flags;
  418. if (type->kind == ZEND_FFI_TYPE_POINTER) {
  419. cdata->ptr = (void*)&cdata->ptr_holder;
  420. *(void**)cdata->ptr = *(void**)ptr;
  421. } else if (type->kind == ZEND_FFI_TYPE_STRUCT) {
  422. cdata->ptr = emalloc(type->size);
  423. cdata->flags |= ZEND_FFI_FLAG_OWNED;
  424. memcpy(cdata->ptr, ptr, type->size);
  425. } else {
  426. cdata->ptr = ptr;
  427. }
  428. return cdata;
  429. }
  430. /* }}} */
  431. static zend_always_inline void zend_ffi_cdata_to_zval(zend_ffi_cdata *cdata, void *ptr, zend_ffi_type *type, int read_type, zval *rv, zend_ffi_flags flags, bool is_ret, bool debug_union) /* {{{ */
  432. {
  433. if (read_type == BP_VAR_R) {
  434. zend_ffi_type_kind kind = type->kind;
  435. again:
  436. switch (kind) {
  437. case ZEND_FFI_TYPE_FLOAT:
  438. ZVAL_DOUBLE(rv, *(float*)ptr);
  439. return;
  440. case ZEND_FFI_TYPE_DOUBLE:
  441. ZVAL_DOUBLE(rv, *(double*)ptr);
  442. return;
  443. #ifdef HAVE_LONG_DOUBLE
  444. case ZEND_FFI_TYPE_LONGDOUBLE:
  445. ZVAL_DOUBLE(rv, *(long double*)ptr);
  446. return;
  447. #endif
  448. case ZEND_FFI_TYPE_UINT8:
  449. ZVAL_LONG(rv, *(uint8_t*)ptr);
  450. return;
  451. case ZEND_FFI_TYPE_SINT8:
  452. ZVAL_LONG(rv, *(int8_t*)ptr);
  453. return;
  454. case ZEND_FFI_TYPE_UINT16:
  455. ZVAL_LONG(rv, *(uint16_t*)ptr);
  456. return;
  457. case ZEND_FFI_TYPE_SINT16:
  458. ZVAL_LONG(rv, *(int16_t*)ptr);
  459. return;
  460. case ZEND_FFI_TYPE_UINT32:
  461. ZVAL_LONG(rv, *(uint32_t*)ptr);
  462. return;
  463. case ZEND_FFI_TYPE_SINT32:
  464. ZVAL_LONG(rv, *(int32_t*)ptr);
  465. return;
  466. case ZEND_FFI_TYPE_UINT64:
  467. ZVAL_LONG(rv, *(uint64_t*)ptr);
  468. return;
  469. case ZEND_FFI_TYPE_SINT64:
  470. ZVAL_LONG(rv, *(int64_t*)ptr);
  471. return;
  472. case ZEND_FFI_TYPE_BOOL:
  473. ZVAL_BOOL(rv, *(uint8_t*)ptr);
  474. return;
  475. case ZEND_FFI_TYPE_CHAR:
  476. ZVAL_CHAR(rv, *(char*)ptr);
  477. return;
  478. case ZEND_FFI_TYPE_ENUM:
  479. kind = type->enumeration.kind;
  480. goto again;
  481. case ZEND_FFI_TYPE_POINTER:
  482. if (*(void**)ptr == NULL) {
  483. ZVAL_NULL(rv);
  484. return;
  485. } else if (debug_union) {
  486. ZVAL_STR(rv, zend_strpprintf(0, "%p", *(void**)ptr));
  487. return;
  488. } else if ((type->attr & ZEND_FFI_ATTR_CONST) && ZEND_FFI_TYPE(type->pointer.type)->kind == ZEND_FFI_TYPE_CHAR) {
  489. ZVAL_STRING(rv, *(char**)ptr);
  490. return;
  491. }
  492. if (!cdata) {
  493. if (is_ret) {
  494. cdata = zend_ffi_cdata_to_zval_slow_ret(ptr, type, flags);
  495. } else {
  496. cdata = zend_ffi_cdata_to_zval_slow_ptr(ptr, type, flags);
  497. }
  498. } else {
  499. GC_ADDREF(&cdata->std);
  500. }
  501. ZVAL_OBJ(rv, &cdata->std);
  502. return;
  503. default:
  504. break;
  505. }
  506. }
  507. if (!cdata) {
  508. if (is_ret) {
  509. cdata = zend_ffi_cdata_to_zval_slow_ret(ptr, type, flags);
  510. } else {
  511. cdata = zend_ffi_cdata_to_zval_slow(ptr, type, flags);
  512. }
  513. } else {
  514. GC_ADDREF(&cdata->std);
  515. }
  516. ZVAL_OBJ(rv, &cdata->std);
  517. }
  518. /* }}} */
  519. static uint64_t zend_ffi_bit_field_read(void *ptr, zend_ffi_field *field) /* {{{ */
  520. {
  521. size_t bit = field->first_bit;
  522. size_t last_bit = bit + field->bits - 1;
  523. uint8_t *p = (uint8_t *) ptr + bit / 8;
  524. uint8_t *last_p = (uint8_t *) ptr + last_bit / 8;
  525. size_t pos = bit % 8;
  526. size_t insert_pos = 0;
  527. uint8_t mask;
  528. uint64_t val = 0;
  529. /* Bitfield fits into a single byte */
  530. if (p == last_p) {
  531. mask = (1U << field->bits) - 1U;
  532. return (*p >> pos) & mask;
  533. }
  534. /* Read partial prefix byte */
  535. if (pos != 0) {
  536. size_t num_bits = 8 - pos;
  537. mask = ((1U << num_bits) - 1U) << pos;
  538. val = (*p++ >> pos) & mask;
  539. insert_pos += num_bits;
  540. }
  541. /* Read full bytes */
  542. while (p < last_p) {
  543. val |= *p++ << insert_pos;
  544. insert_pos += 8;
  545. }
  546. /* Read partial suffix byte */
  547. if (p == last_p) {
  548. size_t num_bits = last_bit % 8 + 1;
  549. mask = (1U << num_bits) - 1U;
  550. val |= (*p & mask) << insert_pos;
  551. }
  552. return val;
  553. }
  554. /* }}} */
  555. static void zend_ffi_bit_field_to_zval(void *ptr, zend_ffi_field *field, zval *rv) /* {{{ */
  556. {
  557. uint64_t val = zend_ffi_bit_field_read(ptr, field);
  558. if (ZEND_FFI_TYPE(field->type)->kind == ZEND_FFI_TYPE_CHAR
  559. || ZEND_FFI_TYPE(field->type)->kind == ZEND_FFI_TYPE_SINT8
  560. || ZEND_FFI_TYPE(field->type)->kind == ZEND_FFI_TYPE_SINT16
  561. || ZEND_FFI_TYPE(field->type)->kind == ZEND_FFI_TYPE_SINT32
  562. || ZEND_FFI_TYPE(field->type)->kind == ZEND_FFI_TYPE_SINT64) {
  563. /* Sign extend */
  564. uint64_t shift = 64 - (field->bits % 64);
  565. if (shift != 0) {
  566. val = (int64_t)(val << shift) >> shift;
  567. }
  568. }
  569. ZVAL_LONG(rv, val);
  570. }
  571. /* }}} */
  572. static int zend_ffi_zval_to_bit_field(void *ptr, zend_ffi_field *field, zval *value) /* {{{ */
  573. {
  574. uint64_t val = zval_get_long(value);
  575. size_t bit = field->first_bit;
  576. size_t last_bit = bit + field->bits - 1;
  577. uint8_t *p = (uint8_t *) ptr + bit / 8;
  578. uint8_t *last_p = (uint8_t *) ptr + last_bit / 8;
  579. size_t pos = bit % 8;
  580. uint8_t mask;
  581. /* Bitfield fits into a single byte */
  582. if (p == last_p) {
  583. mask = ((1U << field->bits) - 1U) << pos;
  584. *p = (*p & ~mask) | ((val << pos) & mask);
  585. return SUCCESS;
  586. }
  587. /* Write partial prefix byte */
  588. if (pos != 0) {
  589. size_t num_bits = 8 - pos;
  590. mask = ((1U << num_bits) - 1U) << pos;
  591. *p = (*p & ~mask) | ((val << pos) & mask);
  592. p++;
  593. val >>= num_bits;
  594. }
  595. /* Write full bytes */
  596. while (p < last_p) {
  597. *p++ = val;
  598. val >>= 8;
  599. }
  600. /* Write partial suffix byte */
  601. if (p == last_p) {
  602. size_t num_bits = last_bit % 8 + 1;
  603. mask = (1U << num_bits) - 1U;
  604. *p = (*p & ~mask) | (val & mask);
  605. }
  606. return SUCCESS;
  607. }
  608. /* }}} */
  609. static zend_always_inline int zend_ffi_zval_to_cdata(void *ptr, zend_ffi_type *type, zval *value) /* {{{ */
  610. {
  611. zend_long lval;
  612. double dval;
  613. zend_string *tmp_str;
  614. zend_string *str;
  615. zend_ffi_type_kind kind = type->kind;
  616. again:
  617. switch (kind) {
  618. case ZEND_FFI_TYPE_FLOAT:
  619. dval = zval_get_double(value);
  620. *(float*)ptr = dval;
  621. break;
  622. case ZEND_FFI_TYPE_DOUBLE:
  623. dval = zval_get_double(value);
  624. *(double*)ptr = dval;
  625. break;
  626. #ifdef HAVE_LONG_DOUBLE
  627. case ZEND_FFI_TYPE_LONGDOUBLE:
  628. dval = zval_get_double(value);
  629. *(long double*)ptr = dval;
  630. break;
  631. #endif
  632. case ZEND_FFI_TYPE_UINT8:
  633. lval = zval_get_long(value);
  634. *(uint8_t*)ptr = lval;
  635. break;
  636. case ZEND_FFI_TYPE_SINT8:
  637. lval = zval_get_long(value);
  638. *(int8_t*)ptr = lval;
  639. break;
  640. case ZEND_FFI_TYPE_UINT16:
  641. lval = zval_get_long(value);
  642. *(uint16_t*)ptr = lval;
  643. break;
  644. case ZEND_FFI_TYPE_SINT16:
  645. lval = zval_get_long(value);
  646. *(int16_t*)ptr = lval;
  647. break;
  648. case ZEND_FFI_TYPE_UINT32:
  649. lval = zval_get_long(value);
  650. *(uint32_t*)ptr = lval;
  651. break;
  652. case ZEND_FFI_TYPE_SINT32:
  653. lval = zval_get_long(value);
  654. *(int32_t*)ptr = lval;
  655. break;
  656. case ZEND_FFI_TYPE_UINT64:
  657. lval = zval_get_long(value);
  658. *(uint64_t*)ptr = lval;
  659. break;
  660. case ZEND_FFI_TYPE_SINT64:
  661. lval = zval_get_long(value);
  662. *(int64_t*)ptr = lval;
  663. break;
  664. case ZEND_FFI_TYPE_BOOL:
  665. *(uint8_t*)ptr = zend_is_true(value);
  666. break;
  667. case ZEND_FFI_TYPE_CHAR:
  668. str = zval_get_tmp_string(value, &tmp_str);
  669. if (ZSTR_LEN(str) == 1) {
  670. *(char*)ptr = ZSTR_VAL(str)[0];
  671. } else {
  672. zend_ffi_assign_incompatible(value, type);
  673. return FAILURE;
  674. }
  675. zend_tmp_string_release(tmp_str);
  676. break;
  677. case ZEND_FFI_TYPE_ENUM:
  678. kind = type->enumeration.kind;
  679. goto again;
  680. case ZEND_FFI_TYPE_POINTER:
  681. if (Z_TYPE_P(value) == IS_NULL) {
  682. *(void**)ptr = NULL;
  683. break;
  684. } else if (Z_TYPE_P(value) == IS_OBJECT && Z_OBJCE_P(value) == zend_ffi_cdata_ce) {
  685. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(value);
  686. if (zend_ffi_is_compatible_type(type, ZEND_FFI_TYPE(cdata->type))) {
  687. if (ZEND_FFI_TYPE(cdata->type)->kind == ZEND_FFI_TYPE_POINTER) {
  688. *(void**)ptr = *(void**)cdata->ptr;
  689. } else {
  690. if (cdata->flags & ZEND_FFI_FLAG_OWNED) {
  691. zend_throw_error(zend_ffi_exception_ce, "Attempt to perform assign of owned C pointer");
  692. return FAILURE;
  693. }
  694. *(void**)ptr = cdata->ptr;
  695. }
  696. return SUCCESS;
  697. /* Allow transparent assignment of not-owned CData to compatible pointers */
  698. } else if (ZEND_FFI_TYPE(cdata->type)->kind != ZEND_FFI_TYPE_POINTER
  699. && zend_ffi_is_compatible_type(ZEND_FFI_TYPE(type->pointer.type), ZEND_FFI_TYPE(cdata->type))) {
  700. if (cdata->flags & ZEND_FFI_FLAG_OWNED) {
  701. zend_throw_error(zend_ffi_exception_ce, "Attempt to perform assign pointer to owned C data");
  702. return FAILURE;
  703. }
  704. *(void**)ptr = cdata->ptr;
  705. return SUCCESS;
  706. }
  707. #if FFI_CLOSURES
  708. } else if (ZEND_FFI_TYPE(type->pointer.type)->kind == ZEND_FFI_TYPE_FUNC) {
  709. void *callback = zend_ffi_create_callback(ZEND_FFI_TYPE(type->pointer.type), value);
  710. if (callback) {
  711. *(void**)ptr = callback;
  712. break;
  713. } else {
  714. return FAILURE;
  715. }
  716. #endif
  717. }
  718. zend_ffi_assign_incompatible(value, type);
  719. return FAILURE;
  720. case ZEND_FFI_TYPE_STRUCT:
  721. case ZEND_FFI_TYPE_ARRAY:
  722. default:
  723. if (Z_TYPE_P(value) == IS_OBJECT && Z_OBJCE_P(value) == zend_ffi_cdata_ce) {
  724. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(value);
  725. if (zend_ffi_is_compatible_type(type, ZEND_FFI_TYPE(cdata->type)) &&
  726. type->size == ZEND_FFI_TYPE(cdata->type)->size) {
  727. memcpy(ptr, cdata->ptr, type->size);
  728. return SUCCESS;
  729. }
  730. }
  731. zend_ffi_assign_incompatible(value, type);
  732. return FAILURE;
  733. }
  734. return SUCCESS;
  735. }
  736. /* }}} */
  737. #if defined(ZEND_WIN32) && (defined(HAVE_FFI_FASTCALL) || defined(HAVE_FFI_STDCALL) || defined(HAVE_FFI_VECTORCALL_PARTIAL))
  738. static size_t zend_ffi_arg_size(zend_ffi_type *type) /* {{{ */
  739. {
  740. zend_ffi_type *arg_type;
  741. size_t arg_size = 0;
  742. ZEND_HASH_FOREACH_PTR(type->func.args, arg_type) {
  743. arg_size += MAX(ZEND_FFI_TYPE(arg_type)->size, sizeof(size_t));
  744. } ZEND_HASH_FOREACH_END();
  745. return arg_size;
  746. }
  747. /* }}} */
  748. #endif
  749. static zend_always_inline zend_string *zend_ffi_mangled_func_name(zend_string *name, zend_ffi_type *type) /* {{{ */
  750. {
  751. #ifdef ZEND_WIN32
  752. switch (type->func.abi) {
  753. # ifdef HAVE_FFI_FASTCALL
  754. case FFI_FASTCALL:
  755. return strpprintf(0, "@%s@%zu", ZSTR_VAL(name), zend_ffi_arg_size(type));
  756. # endif
  757. # ifdef HAVE_FFI_STDCALL
  758. case FFI_STDCALL:
  759. return strpprintf(0, "_%s@%zu", ZSTR_VAL(name), zend_ffi_arg_size(type));
  760. # endif
  761. # ifdef HAVE_FFI_VECTORCALL_PARTIAL
  762. case FFI_VECTORCALL_PARTIAL:
  763. return strpprintf(0, "%s@@%zu", ZSTR_VAL(name), zend_ffi_arg_size(type));
  764. # endif
  765. }
  766. #endif
  767. return zend_string_copy(name);
  768. }
  769. /* }}} */
  770. #if FFI_CLOSURES
  771. typedef struct _zend_ffi_callback_data {
  772. zend_fcall_info_cache fcc;
  773. zend_ffi_type *type;
  774. void *code;
  775. void *callback;
  776. ffi_cif cif;
  777. uint32_t arg_count;
  778. ffi_type *ret_type;
  779. ffi_type *arg_types[0];
  780. } zend_ffi_callback_data;
  781. static void zend_ffi_callback_hash_dtor(zval *zv) /* {{{ */
  782. {
  783. zend_ffi_callback_data *callback_data = Z_PTR_P(zv);
  784. ffi_closure_free(callback_data->callback);
  785. if (callback_data->fcc.function_handler->common.fn_flags & ZEND_ACC_CLOSURE) {
  786. OBJ_RELEASE(ZEND_CLOSURE_OBJECT(callback_data->fcc.function_handler));
  787. }
  788. for (int i = 0; i < callback_data->arg_count; ++i) {
  789. if (callback_data->arg_types[i]->type == FFI_TYPE_STRUCT) {
  790. efree(callback_data->arg_types[i]);
  791. }
  792. }
  793. if (callback_data->ret_type->type == FFI_TYPE_STRUCT) {
  794. efree(callback_data->ret_type);
  795. }
  796. efree(callback_data);
  797. }
  798. /* }}} */
  799. static void zend_ffi_callback_trampoline(ffi_cif* cif, void* ret, void** args, void* data) /* {{{ */
  800. {
  801. zend_ffi_callback_data *callback_data = (zend_ffi_callback_data*)data;
  802. zend_fcall_info fci;
  803. zend_ffi_type *ret_type;
  804. zval retval;
  805. ALLOCA_FLAG(use_heap)
  806. fci.size = sizeof(zend_fcall_info);
  807. ZVAL_UNDEF(&fci.function_name);
  808. fci.retval = &retval;
  809. fci.params = do_alloca(sizeof(zval) *callback_data->arg_count, use_heap);
  810. fci.object = NULL;
  811. fci.param_count = callback_data->arg_count;
  812. fci.named_params = NULL;
  813. if (callback_data->type->func.args) {
  814. int n = 0;
  815. zend_ffi_type *arg_type;
  816. ZEND_HASH_FOREACH_PTR(callback_data->type->func.args, arg_type) {
  817. arg_type = ZEND_FFI_TYPE(arg_type);
  818. zend_ffi_cdata_to_zval(NULL, args[n], arg_type, BP_VAR_R, &fci.params[n], (zend_ffi_flags)(arg_type->attr & ZEND_FFI_ATTR_CONST), 0, 0);
  819. n++;
  820. } ZEND_HASH_FOREACH_END();
  821. }
  822. ZVAL_UNDEF(&retval);
  823. if (zend_call_function(&fci, &callback_data->fcc) != SUCCESS) {
  824. zend_throw_error(zend_ffi_exception_ce, "Cannot call callback");
  825. }
  826. if (callback_data->arg_count) {
  827. int n = 0;
  828. for (n = 0; n < callback_data->arg_count; n++) {
  829. zval_ptr_dtor(&fci.params[n]);
  830. }
  831. }
  832. free_alloca(fci.params, use_heap);
  833. if (EG(exception)) {
  834. zend_error(E_ERROR, "Throwing from FFI callbacks is not allowed");
  835. }
  836. ret_type = ZEND_FFI_TYPE(callback_data->type->func.ret_type);
  837. if (ret_type->kind != ZEND_FFI_TYPE_VOID) {
  838. zend_ffi_zval_to_cdata(ret, ret_type, &retval);
  839. }
  840. zval_ptr_dtor(&retval);
  841. }
  842. /* }}} */
  843. static void *zend_ffi_create_callback(zend_ffi_type *type, zval *value) /* {{{ */
  844. {
  845. zend_fcall_info_cache fcc;
  846. char *error = NULL;
  847. uint32_t arg_count;
  848. void *code;
  849. void *callback;
  850. zend_ffi_callback_data *callback_data;
  851. if (type->attr & ZEND_FFI_ATTR_VARIADIC) {
  852. zend_throw_error(zend_ffi_exception_ce, "Variadic function closures are not supported");
  853. return NULL;
  854. }
  855. if (!zend_is_callable_ex(value, NULL, 0, NULL, &fcc, &error)) {
  856. zend_throw_error(zend_ffi_exception_ce, "Attempt to assign an invalid callback, %s", error);
  857. return NULL;
  858. }
  859. arg_count = type->func.args ? zend_hash_num_elements(type->func.args) : 0;
  860. if (arg_count < fcc.function_handler->common.required_num_args) {
  861. zend_throw_error(zend_ffi_exception_ce, "Attempt to assign an invalid callback, insufficient number of arguments");
  862. return NULL;
  863. }
  864. callback = ffi_closure_alloc(sizeof(ffi_closure), &code);
  865. if (!callback) {
  866. zend_throw_error(zend_ffi_exception_ce, "Cannot allocate callback");
  867. return NULL;
  868. }
  869. callback_data = emalloc(sizeof(zend_ffi_callback_data) + sizeof(ffi_type*) * arg_count);
  870. memcpy(&callback_data->fcc, &fcc, sizeof(zend_fcall_info_cache));
  871. callback_data->type = type;
  872. callback_data->callback = callback;
  873. callback_data->code = code;
  874. callback_data->arg_count = arg_count;
  875. if (type->func.args) {
  876. int n = 0;
  877. zend_ffi_type *arg_type;
  878. ZEND_HASH_FOREACH_PTR(type->func.args, arg_type) {
  879. arg_type = ZEND_FFI_TYPE(arg_type);
  880. callback_data->arg_types[n] = zend_ffi_get_type(arg_type);
  881. if (!callback_data->arg_types[n]) {
  882. zend_ffi_pass_unsupported(arg_type);
  883. for (int i = 0; i < n; ++i) {
  884. if (callback_data->arg_types[i]->type == FFI_TYPE_STRUCT) {
  885. efree(callback_data->arg_types[i]);
  886. }
  887. }
  888. efree(callback_data);
  889. ffi_closure_free(callback);
  890. return NULL;
  891. }
  892. n++;
  893. } ZEND_HASH_FOREACH_END();
  894. }
  895. callback_data->ret_type = zend_ffi_get_type(ZEND_FFI_TYPE(type->func.ret_type));
  896. if (!callback_data->ret_type) {
  897. zend_ffi_return_unsupported(type->func.ret_type);
  898. for (int i = 0; i < callback_data->arg_count; ++i) {
  899. if (callback_data->arg_types[i]->type == FFI_TYPE_STRUCT) {
  900. efree(callback_data->arg_types[i]);
  901. }
  902. }
  903. efree(callback_data);
  904. ffi_closure_free(callback);
  905. return NULL;
  906. }
  907. if (ffi_prep_cif(&callback_data->cif, type->func.abi, callback_data->arg_count, callback_data->ret_type, callback_data->arg_types) != FFI_OK) {
  908. zend_throw_error(zend_ffi_exception_ce, "Cannot prepare callback CIF");
  909. goto free_on_failure;
  910. }
  911. if (ffi_prep_closure_loc(callback, &callback_data->cif, zend_ffi_callback_trampoline, callback_data, code) != FFI_OK) {
  912. zend_throw_error(zend_ffi_exception_ce, "Cannot prepare callback");
  913. free_on_failure: ;
  914. for (int i = 0; i < callback_data->arg_count; ++i) {
  915. if (callback_data->arg_types[i]->type == FFI_TYPE_STRUCT) {
  916. efree(callback_data->arg_types[i]);
  917. }
  918. }
  919. if (callback_data->ret_type->type == FFI_TYPE_STRUCT) {
  920. efree(callback_data->ret_type);
  921. }
  922. efree(callback_data);
  923. ffi_closure_free(callback);
  924. return NULL;
  925. }
  926. if (!FFI_G(callbacks)) {
  927. FFI_G(callbacks) = emalloc(sizeof(HashTable));
  928. zend_hash_init(FFI_G(callbacks), 0, NULL, zend_ffi_callback_hash_dtor, 0);
  929. }
  930. zend_hash_next_index_insert_ptr(FFI_G(callbacks), callback_data);
  931. if (fcc.function_handler->common.fn_flags & ZEND_ACC_CLOSURE) {
  932. GC_ADDREF(ZEND_CLOSURE_OBJECT(fcc.function_handler));
  933. }
  934. return code;
  935. }
  936. /* }}} */
  937. #endif
  938. static zval *zend_ffi_cdata_get(zend_object *obj, zend_string *member, int read_type, void **cache_slot, zval *rv) /* {{{ */
  939. {
  940. zend_ffi_cdata *cdata = (zend_ffi_cdata*)obj;
  941. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  942. #if 0
  943. if (UNEXPECTED(!cdata->ptr)) {
  944. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  945. return &EG(uninitialized_zval);
  946. }
  947. #endif
  948. if (UNEXPECTED(!zend_string_equals_literal(member, "cdata"))) {
  949. zend_throw_error(zend_ffi_exception_ce, "Only 'cdata' property may be read");
  950. return &EG(uninitialized_zval);;
  951. }
  952. zend_ffi_cdata_to_zval(cdata, cdata->ptr, type, BP_VAR_R, rv, 0, 0, 0);
  953. return rv;
  954. }
  955. /* }}} */
  956. static zval *zend_ffi_cdata_set(zend_object *obj, zend_string *member, zval *value, void **cache_slot) /* {{{ */
  957. {
  958. zend_ffi_cdata *cdata = (zend_ffi_cdata*)obj;
  959. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  960. #if 0
  961. if (UNEXPECTED(!cdata->ptr)) {
  962. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  963. return &EG(uninitialized_zval);;
  964. }
  965. #endif
  966. if (UNEXPECTED(!zend_string_equals_literal(member, "cdata"))) {
  967. zend_throw_error(zend_ffi_exception_ce, "Only 'cdata' property may be set");
  968. return &EG(uninitialized_zval);;
  969. }
  970. zend_ffi_zval_to_cdata(cdata->ptr, type, value);
  971. return value;
  972. }
  973. /* }}} */
  974. static int zend_ffi_cdata_cast_object(zend_object *readobj, zval *writeobj, int type) /* {{{ */
  975. {
  976. if (type == IS_STRING) {
  977. zend_ffi_cdata *cdata = (zend_ffi_cdata*)readobj;
  978. zend_ffi_type *ctype = ZEND_FFI_TYPE(cdata->type);
  979. void *ptr = cdata->ptr;
  980. zend_ffi_type_kind kind = ctype->kind;
  981. again:
  982. switch (kind) {
  983. case ZEND_FFI_TYPE_FLOAT:
  984. ZVAL_DOUBLE(writeobj, *(float*)ptr);
  985. break;
  986. case ZEND_FFI_TYPE_DOUBLE:
  987. ZVAL_DOUBLE(writeobj, *(double*)ptr);
  988. break;
  989. #ifdef HAVE_LONG_DOUBLE
  990. case ZEND_FFI_TYPE_LONGDOUBLE:
  991. ZVAL_DOUBLE(writeobj, *(long double*)ptr);
  992. break;
  993. #endif
  994. case ZEND_FFI_TYPE_UINT8:
  995. ZVAL_LONG(writeobj, *(uint8_t*)ptr);
  996. break;
  997. case ZEND_FFI_TYPE_SINT8:
  998. ZVAL_LONG(writeobj, *(int8_t*)ptr);
  999. break;
  1000. case ZEND_FFI_TYPE_UINT16:
  1001. ZVAL_LONG(writeobj, *(uint16_t*)ptr);
  1002. break;
  1003. case ZEND_FFI_TYPE_SINT16:
  1004. ZVAL_LONG(writeobj, *(int16_t*)ptr);
  1005. break;
  1006. case ZEND_FFI_TYPE_UINT32:
  1007. ZVAL_LONG(writeobj, *(uint32_t*)ptr);
  1008. break;
  1009. case ZEND_FFI_TYPE_SINT32:
  1010. ZVAL_LONG(writeobj, *(int32_t*)ptr);
  1011. break;
  1012. case ZEND_FFI_TYPE_UINT64:
  1013. ZVAL_LONG(writeobj, *(uint64_t*)ptr);
  1014. break;
  1015. case ZEND_FFI_TYPE_SINT64:
  1016. ZVAL_LONG(writeobj, *(int64_t*)ptr);
  1017. break;
  1018. case ZEND_FFI_TYPE_BOOL:
  1019. ZVAL_BOOL(writeobj, *(uint8_t*)ptr);
  1020. break;
  1021. case ZEND_FFI_TYPE_CHAR:
  1022. ZVAL_CHAR(writeobj, *(char*)ptr);
  1023. return SUCCESS;
  1024. case ZEND_FFI_TYPE_ENUM:
  1025. kind = ctype->enumeration.kind;
  1026. goto again;
  1027. case ZEND_FFI_TYPE_POINTER:
  1028. if (*(void**)ptr == NULL) {
  1029. ZVAL_NULL(writeobj);
  1030. break;
  1031. } else if ((ctype->attr & ZEND_FFI_ATTR_CONST) && ZEND_FFI_TYPE(ctype->pointer.type)->kind == ZEND_FFI_TYPE_CHAR) {
  1032. ZVAL_STRING(writeobj, *(char**)ptr);
  1033. return SUCCESS;
  1034. }
  1035. return FAILURE;
  1036. default:
  1037. return FAILURE;
  1038. }
  1039. convert_to_string(writeobj);
  1040. return SUCCESS;
  1041. } else if (type == _IS_BOOL) {
  1042. ZVAL_TRUE(writeobj);
  1043. return SUCCESS;
  1044. }
  1045. return FAILURE;
  1046. }
  1047. /* }}} */
  1048. static zval *zend_ffi_cdata_read_field(zend_object *obj, zend_string *field_name, int read_type, void **cache_slot, zval *rv) /* {{{ */
  1049. {
  1050. zend_ffi_cdata *cdata = (zend_ffi_cdata*)obj;
  1051. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  1052. void *ptr = cdata->ptr;
  1053. zend_ffi_field *field;
  1054. if (cache_slot && *cache_slot == type) {
  1055. field = *(cache_slot + 1);
  1056. } else {
  1057. if (type->kind == ZEND_FFI_TYPE_POINTER) {
  1058. type = ZEND_FFI_TYPE(type->pointer.type);
  1059. }
  1060. if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
  1061. if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
  1062. zend_throw_error(zend_ffi_exception_ce, "Attempt to read field '%s' of non C struct/union", ZSTR_VAL(field_name));
  1063. return &EG(uninitialized_zval);
  1064. }
  1065. }
  1066. field = zend_hash_find_ptr(&type->record.fields, field_name);
  1067. if (UNEXPECTED(!field)) {
  1068. zend_throw_error(zend_ffi_exception_ce, "Attempt to read undefined field '%s' of C struct/union", ZSTR_VAL(field_name));
  1069. return &EG(uninitialized_zval);
  1070. }
  1071. if (cache_slot) {
  1072. *cache_slot = type;
  1073. *(cache_slot + 1) = field;
  1074. }
  1075. }
  1076. if (ZEND_FFI_TYPE(cdata->type)->kind == ZEND_FFI_TYPE_POINTER) {
  1077. /* transparently dereference the pointer */
  1078. if (UNEXPECTED(!ptr)) {
  1079. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1080. return &EG(uninitialized_zval);
  1081. }
  1082. ptr = (void*)(*(char**)ptr);
  1083. if (UNEXPECTED(!ptr)) {
  1084. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1085. return &EG(uninitialized_zval);
  1086. }
  1087. type = ZEND_FFI_TYPE(type->pointer.type);
  1088. }
  1089. #if 0
  1090. if (UNEXPECTED(!ptr)) {
  1091. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1092. return &EG(uninitialized_zval);
  1093. }
  1094. #endif
  1095. if (EXPECTED(!field->bits)) {
  1096. zend_ffi_type *field_type = field->type;
  1097. if (ZEND_FFI_TYPE_IS_OWNED(field_type)) {
  1098. field_type = ZEND_FFI_TYPE(field_type);
  1099. if (!(field_type->attr & ZEND_FFI_ATTR_STORED)
  1100. && field_type->kind == ZEND_FFI_TYPE_POINTER) {
  1101. field->type = field_type = zend_ffi_remember_type(field_type);
  1102. }
  1103. }
  1104. ptr = (void*)(((char*)ptr) + field->offset);
  1105. zend_ffi_cdata_to_zval(NULL, ptr, field_type, read_type, rv, (cdata->flags & ZEND_FFI_FLAG_CONST) | (zend_ffi_flags)field->is_const, 0, 0);
  1106. } else {
  1107. zend_ffi_bit_field_to_zval(ptr, field, rv);
  1108. }
  1109. return rv;
  1110. }
  1111. /* }}} */
  1112. static zval *zend_ffi_cdata_write_field(zend_object *obj, zend_string *field_name, zval *value, void **cache_slot) /* {{{ */
  1113. {
  1114. zend_ffi_cdata *cdata = (zend_ffi_cdata*)obj;
  1115. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  1116. void *ptr = cdata->ptr;
  1117. zend_ffi_field *field;
  1118. if (cache_slot && *cache_slot == type) {
  1119. field = *(cache_slot + 1);
  1120. } else {
  1121. if (type->kind == ZEND_FFI_TYPE_POINTER) {
  1122. type = ZEND_FFI_TYPE(type->pointer.type);
  1123. }
  1124. if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
  1125. if (UNEXPECTED(type->kind != ZEND_FFI_TYPE_STRUCT)) {
  1126. zend_throw_error(zend_ffi_exception_ce, "Attempt to assign field '%s' of non C struct/union", ZSTR_VAL(field_name));
  1127. return value;
  1128. }
  1129. }
  1130. field = zend_hash_find_ptr(&type->record.fields, field_name);
  1131. if (UNEXPECTED(!field)) {
  1132. zend_throw_error(zend_ffi_exception_ce, "Attempt to assign undefined field '%s' of C struct/union", ZSTR_VAL(field_name));
  1133. return value;
  1134. }
  1135. if (cache_slot) {
  1136. *cache_slot = type;
  1137. *(cache_slot + 1) = field;
  1138. }
  1139. }
  1140. if (ZEND_FFI_TYPE(cdata->type)->kind == ZEND_FFI_TYPE_POINTER) {
  1141. /* transparently dereference the pointer */
  1142. if (UNEXPECTED(!ptr)) {
  1143. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1144. return value;
  1145. }
  1146. ptr = (void*)(*(char**)ptr);
  1147. if (UNEXPECTED(!ptr)) {
  1148. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1149. return value;
  1150. }
  1151. }
  1152. #if 0
  1153. if (UNEXPECTED(!ptr)) {
  1154. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1155. return value;
  1156. }
  1157. #endif
  1158. if (UNEXPECTED(cdata->flags & ZEND_FFI_FLAG_CONST)) {
  1159. zend_throw_error(zend_ffi_exception_ce, "Attempt to assign read-only location");
  1160. return value;
  1161. } else if (UNEXPECTED(field->is_const)) {
  1162. zend_throw_error(zend_ffi_exception_ce, "Attempt to assign read-only field '%s'", ZSTR_VAL(field_name));
  1163. return value;
  1164. }
  1165. if (EXPECTED(!field->bits)) {
  1166. ptr = (void*)(((char*)ptr) + field->offset);
  1167. zend_ffi_zval_to_cdata(ptr, ZEND_FFI_TYPE(field->type), value);
  1168. } else {
  1169. zend_ffi_zval_to_bit_field(ptr, field, value);
  1170. }
  1171. return value;
  1172. }
  1173. /* }}} */
  1174. static zval *zend_ffi_cdata_read_dim(zend_object *obj, zval *offset, int read_type, zval *rv) /* {{{ */
  1175. {
  1176. zend_ffi_cdata *cdata = (zend_ffi_cdata*)obj;
  1177. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  1178. zend_long dim = zval_get_long(offset);
  1179. zend_ffi_type *dim_type;
  1180. void *ptr;
  1181. zend_ffi_flags is_const;
  1182. if (EXPECTED(type->kind == ZEND_FFI_TYPE_ARRAY)) {
  1183. if (UNEXPECTED((zend_ulong)(dim) >= (zend_ulong)type->array.length)
  1184. && (UNEXPECTED(dim < 0) || UNEXPECTED(type->array.length != 0))) {
  1185. zend_throw_error(zend_ffi_exception_ce, "C array index out of bounds");
  1186. return &EG(uninitialized_zval);
  1187. }
  1188. is_const = (cdata->flags & ZEND_FFI_FLAG_CONST) | (zend_ffi_flags)(type->attr & ZEND_FFI_ATTR_CONST);
  1189. dim_type = type->array.type;
  1190. if (ZEND_FFI_TYPE_IS_OWNED(dim_type)) {
  1191. dim_type = ZEND_FFI_TYPE(dim_type);
  1192. if (!(dim_type->attr & ZEND_FFI_ATTR_STORED)
  1193. && dim_type->kind == ZEND_FFI_TYPE_POINTER) {
  1194. type->array.type = dim_type = zend_ffi_remember_type(dim_type);
  1195. }
  1196. }
  1197. #if 0
  1198. if (UNEXPECTED(!cdata->ptr)) {
  1199. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1200. return &EG(uninitialized_zval);
  1201. }
  1202. #endif
  1203. ptr = (void*)(((char*)cdata->ptr) + dim_type->size * dim);
  1204. } else if (EXPECTED(type->kind == ZEND_FFI_TYPE_POINTER)) {
  1205. is_const = (cdata->flags & ZEND_FFI_FLAG_CONST) | (zend_ffi_flags)(type->attr & ZEND_FFI_ATTR_CONST);
  1206. dim_type = type->pointer.type;
  1207. if (ZEND_FFI_TYPE_IS_OWNED(dim_type)) {
  1208. dim_type = ZEND_FFI_TYPE(dim_type);
  1209. if (!(dim_type->attr & ZEND_FFI_ATTR_STORED)
  1210. && dim_type->kind == ZEND_FFI_TYPE_POINTER) {
  1211. type->pointer.type = dim_type = zend_ffi_remember_type(dim_type);
  1212. }
  1213. }
  1214. if (UNEXPECTED(!cdata->ptr)) {
  1215. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1216. return &EG(uninitialized_zval);
  1217. }
  1218. ptr = (void*)((*(char**)cdata->ptr) + dim_type->size * dim);
  1219. } else {
  1220. zend_throw_error(zend_ffi_exception_ce, "Attempt to read element of non C array");
  1221. return &EG(uninitialized_zval);
  1222. }
  1223. zend_ffi_cdata_to_zval(NULL, ptr, dim_type, read_type, rv, is_const, 0, 0);
  1224. return rv;
  1225. }
  1226. /* }}} */
  1227. static void zend_ffi_cdata_write_dim(zend_object *obj, zval *offset, zval *value) /* {{{ */
  1228. {
  1229. zend_ffi_cdata *cdata = (zend_ffi_cdata*)obj;
  1230. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  1231. zend_long dim;
  1232. void *ptr;
  1233. zend_ffi_flags is_const;
  1234. if (offset == NULL) {
  1235. zend_throw_error(zend_ffi_exception_ce, "Cannot add next element to object of type FFI\\CData");
  1236. return;
  1237. }
  1238. dim = zval_get_long(offset);
  1239. if (EXPECTED(type->kind == ZEND_FFI_TYPE_ARRAY)) {
  1240. if (UNEXPECTED((zend_ulong)(dim) >= (zend_ulong)type->array.length)
  1241. && (UNEXPECTED(dim < 0) || UNEXPECTED(type->array.length != 0))) {
  1242. zend_throw_error(zend_ffi_exception_ce, "C array index out of bounds");
  1243. return;
  1244. }
  1245. is_const = (cdata->flags & ZEND_FFI_FLAG_CONST) | (zend_ffi_flags)(type->attr & ZEND_FFI_ATTR_CONST);
  1246. type = ZEND_FFI_TYPE(type->array.type);
  1247. #if 0
  1248. if (UNEXPECTED(!cdata->ptr)) {
  1249. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1250. return;
  1251. }
  1252. #endif
  1253. ptr = (void*)(((char*)cdata->ptr) + type->size * dim);
  1254. } else if (EXPECTED(type->kind == ZEND_FFI_TYPE_POINTER)) {
  1255. is_const = (cdata->flags & ZEND_FFI_FLAG_CONST) | (zend_ffi_flags)(type->attr & ZEND_FFI_ATTR_CONST);
  1256. type = ZEND_FFI_TYPE(type->pointer.type);
  1257. if (UNEXPECTED(!cdata->ptr)) {
  1258. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1259. return;
  1260. }
  1261. ptr = (void*)((*(char**)cdata->ptr) + type->size * dim);
  1262. } else {
  1263. zend_throw_error(zend_ffi_exception_ce, "Attempt to assign element of non C array");
  1264. return;
  1265. }
  1266. if (UNEXPECTED(is_const)) {
  1267. zend_throw_error(zend_ffi_exception_ce, "Attempt to assign read-only location");
  1268. return;
  1269. }
  1270. zend_ffi_zval_to_cdata(ptr, type, value);
  1271. }
  1272. /* }}} */
  1273. #define MAX_TYPE_NAME_LEN 256
  1274. typedef struct _zend_ffi_ctype_name_buf {
  1275. char *start;
  1276. char *end;
  1277. char buf[MAX_TYPE_NAME_LEN];
  1278. } zend_ffi_ctype_name_buf;
  1279. static int zend_ffi_ctype_name_prepend(zend_ffi_ctype_name_buf *buf, const char *str, size_t len) /* {{{ */
  1280. {
  1281. buf->start -= len;
  1282. if (buf->start < buf->buf) {
  1283. return 0;
  1284. }
  1285. memcpy(buf->start, str, len);
  1286. return 1;
  1287. }
  1288. /* }}} */
  1289. static int zend_ffi_ctype_name_append(zend_ffi_ctype_name_buf *buf, const char *str, size_t len) /* {{{ */
  1290. {
  1291. if (buf->end + len > buf->buf + MAX_TYPE_NAME_LEN) {
  1292. return 0;
  1293. }
  1294. memcpy(buf->end, str, len);
  1295. buf->end += len;
  1296. return 1;
  1297. }
  1298. /* }}} */
  1299. static int zend_ffi_ctype_name(zend_ffi_ctype_name_buf *buf, const zend_ffi_type *type) /* {{{ */
  1300. {
  1301. const char *name = NULL;
  1302. int is_ptr = 0;
  1303. while (1) {
  1304. switch (type->kind) {
  1305. case ZEND_FFI_TYPE_VOID:
  1306. name = "void";
  1307. break;
  1308. case ZEND_FFI_TYPE_FLOAT:
  1309. name = "float";
  1310. break;
  1311. case ZEND_FFI_TYPE_DOUBLE:
  1312. name = "double";
  1313. break;
  1314. #ifdef HAVE_LONG_DOUBLE
  1315. case ZEND_FFI_TYPE_LONGDOUBLE:
  1316. name = "long double";
  1317. break;
  1318. #endif
  1319. case ZEND_FFI_TYPE_UINT8:
  1320. name = "uint8_t";
  1321. break;
  1322. case ZEND_FFI_TYPE_SINT8:
  1323. name = "int8_t";
  1324. break;
  1325. case ZEND_FFI_TYPE_UINT16:
  1326. name = "uint16_t";
  1327. break;
  1328. case ZEND_FFI_TYPE_SINT16:
  1329. name = "int16_t";
  1330. break;
  1331. case ZEND_FFI_TYPE_UINT32:
  1332. name = "uint32_t";
  1333. break;
  1334. case ZEND_FFI_TYPE_SINT32:
  1335. name = "int32_t";
  1336. break;
  1337. case ZEND_FFI_TYPE_UINT64:
  1338. name = "uint64_t";
  1339. break;
  1340. case ZEND_FFI_TYPE_SINT64:
  1341. name = "int64_t";
  1342. break;
  1343. case ZEND_FFI_TYPE_ENUM:
  1344. if (type->enumeration.tag_name) {
  1345. zend_ffi_ctype_name_prepend(buf, ZSTR_VAL(type->enumeration.tag_name), ZSTR_LEN(type->enumeration.tag_name));
  1346. } else {
  1347. zend_ffi_ctype_name_prepend(buf, "<anonymous>", sizeof("<anonymous>")-1);
  1348. }
  1349. name = "enum ";
  1350. break;
  1351. case ZEND_FFI_TYPE_BOOL:
  1352. name = "bool";
  1353. break;
  1354. case ZEND_FFI_TYPE_CHAR:
  1355. name = "char";
  1356. break;
  1357. case ZEND_FFI_TYPE_POINTER:
  1358. if (!zend_ffi_ctype_name_prepend(buf, "*", 1)) {
  1359. return 0;
  1360. }
  1361. is_ptr = 1;
  1362. type = ZEND_FFI_TYPE(type->pointer.type);
  1363. break;
  1364. case ZEND_FFI_TYPE_FUNC:
  1365. if (is_ptr) {
  1366. is_ptr = 0;
  1367. if (!zend_ffi_ctype_name_prepend(buf, "(", 1)
  1368. || !zend_ffi_ctype_name_append(buf, ")", 1)) {
  1369. return 0;
  1370. }
  1371. }
  1372. if (!zend_ffi_ctype_name_append(buf, "(", 1)
  1373. || !zend_ffi_ctype_name_append(buf, ")", 1)) {
  1374. return 0;
  1375. }
  1376. type = ZEND_FFI_TYPE(type->func.ret_type);
  1377. break;
  1378. case ZEND_FFI_TYPE_ARRAY:
  1379. if (is_ptr) {
  1380. is_ptr = 0;
  1381. if (!zend_ffi_ctype_name_prepend(buf, "(", 1)
  1382. || !zend_ffi_ctype_name_append(buf, ")", 1)) {
  1383. return 0;
  1384. }
  1385. }
  1386. if (!zend_ffi_ctype_name_append(buf, "[", 1)) {
  1387. return 0;
  1388. }
  1389. if (type->attr & ZEND_FFI_ATTR_VLA) {
  1390. if (!zend_ffi_ctype_name_append(buf, "*", 1)) {
  1391. return 0;
  1392. }
  1393. } else if (!(type->attr & ZEND_FFI_ATTR_INCOMPLETE_ARRAY)) {
  1394. char str[MAX_LENGTH_OF_LONG + 1];
  1395. char *s = zend_print_long_to_buf(str + sizeof(str) - 1, type->array.length);
  1396. if (!zend_ffi_ctype_name_append(buf, s, strlen(s))) {
  1397. return 0;
  1398. }
  1399. }
  1400. if (!zend_ffi_ctype_name_append(buf, "]", 1)) {
  1401. return 0;
  1402. }
  1403. type = ZEND_FFI_TYPE(type->array.type);
  1404. break;
  1405. case ZEND_FFI_TYPE_STRUCT:
  1406. if (type->attr & ZEND_FFI_ATTR_UNION) {
  1407. if (type->record.tag_name) {
  1408. zend_ffi_ctype_name_prepend(buf, ZSTR_VAL(type->record.tag_name), ZSTR_LEN(type->record.tag_name));
  1409. } else {
  1410. zend_ffi_ctype_name_prepend(buf, "<anonymous>", sizeof("<anonymous>")-1);
  1411. }
  1412. name = "union ";
  1413. } else {
  1414. if (type->record.tag_name) {
  1415. zend_ffi_ctype_name_prepend(buf, ZSTR_VAL(type->record.tag_name), ZSTR_LEN(type->record.tag_name));
  1416. } else {
  1417. zend_ffi_ctype_name_prepend(buf, "<anonymous>", sizeof("<anonymous>")-1);
  1418. }
  1419. name = "struct ";
  1420. }
  1421. break;
  1422. default:
  1423. ZEND_UNREACHABLE();
  1424. }
  1425. if (name) {
  1426. break;
  1427. }
  1428. }
  1429. // if (buf->start != buf->end && *buf->start != '[') {
  1430. // if (!zend_ffi_ctype_name_prepend(buf, " ", 1)) {
  1431. // return 0;
  1432. // }
  1433. // }
  1434. return zend_ffi_ctype_name_prepend(buf, name, strlen(name));
  1435. }
  1436. /* }}} */
  1437. static ZEND_COLD void zend_ffi_return_unsupported(zend_ffi_type *type) /* {{{ */
  1438. {
  1439. type = ZEND_FFI_TYPE(type);
  1440. if (type->kind == ZEND_FFI_TYPE_STRUCT) {
  1441. zend_throw_error(zend_ffi_exception_ce, "FFI return struct/union is not implemented");
  1442. } else if (type->kind == ZEND_FFI_TYPE_ARRAY) {
  1443. zend_throw_error(zend_ffi_exception_ce, "FFI return array is not implemented");
  1444. } else {
  1445. zend_throw_error(zend_ffi_exception_ce, "FFI internal error. Unsupported return type");
  1446. }
  1447. }
  1448. /* }}} */
  1449. static ZEND_COLD void zend_ffi_pass_unsupported(zend_ffi_type *type) /* {{{ */
  1450. {
  1451. type = ZEND_FFI_TYPE(type);
  1452. if (type->kind == ZEND_FFI_TYPE_STRUCT) {
  1453. zend_throw_error(zend_ffi_exception_ce, "FFI passing struct/union is not implemented");
  1454. } else if (type->kind == ZEND_FFI_TYPE_ARRAY) {
  1455. zend_throw_error(zend_ffi_exception_ce, "FFI passing array is not implemented");
  1456. } else {
  1457. zend_throw_error(zend_ffi_exception_ce, "FFI internal error. Unsupported parameter type");
  1458. }
  1459. }
  1460. /* }}} */
  1461. static ZEND_COLD void zend_ffi_pass_incompatible(zval *arg, zend_ffi_type *type, uint32_t n, zend_execute_data *execute_data) /* {{{ */
  1462. {
  1463. zend_ffi_ctype_name_buf buf1, buf2;
  1464. buf1.start = buf1.end = buf1.buf + ((MAX_TYPE_NAME_LEN * 3) / 4);
  1465. if (!zend_ffi_ctype_name(&buf1, type)) {
  1466. zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%s'", n + 1, ZSTR_VAL(EX(func)->internal_function.function_name));
  1467. } else {
  1468. *buf1.end = 0;
  1469. if (Z_TYPE_P(arg) == IS_OBJECT && Z_OBJCE_P(arg) == zend_ffi_cdata_ce) {
  1470. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(arg);
  1471. type = ZEND_FFI_TYPE(cdata->type);
  1472. buf2.start = buf2.end = buf2.buf + ((MAX_TYPE_NAME_LEN * 3) / 4);
  1473. if (!zend_ffi_ctype_name(&buf2, type)) {
  1474. zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%s', expecting '%s'", n + 1, ZSTR_VAL(EX(func)->internal_function.function_name), buf1.start);
  1475. } else {
  1476. *buf2.end = 0;
  1477. zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%s', expecting '%s', found '%s'", n + 1, ZSTR_VAL(EX(func)->internal_function.function_name), buf1.start, buf2.start);
  1478. }
  1479. } else {
  1480. zend_throw_error(zend_ffi_exception_ce, "Passing incompatible argument %d of C function '%s', expecting '%s', found PHP '%s'", n + 1, ZSTR_VAL(EX(func)->internal_function.function_name), buf1.start, zend_zval_type_name(arg));
  1481. }
  1482. }
  1483. }
  1484. /* }}} */
  1485. static ZEND_COLD void zend_ffi_assign_incompatible(zval *arg, zend_ffi_type *type) /* {{{ */
  1486. {
  1487. zend_ffi_ctype_name_buf buf1, buf2;
  1488. buf1.start = buf1.end = buf1.buf + ((MAX_TYPE_NAME_LEN * 3) / 4);
  1489. if (!zend_ffi_ctype_name(&buf1, type)) {
  1490. zend_throw_error(zend_ffi_exception_ce, "Incompatible types when assigning");
  1491. } else {
  1492. *buf1.end = 0;
  1493. if (Z_TYPE_P(arg) == IS_OBJECT && Z_OBJCE_P(arg) == zend_ffi_cdata_ce) {
  1494. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(arg);
  1495. type = ZEND_FFI_TYPE(cdata->type);
  1496. buf2.start = buf2.end = buf2.buf + ((MAX_TYPE_NAME_LEN * 3) / 4);
  1497. if (!zend_ffi_ctype_name(&buf2, type)) {
  1498. zend_throw_error(zend_ffi_exception_ce, "Incompatible types when assigning to type '%s'", buf1.start);
  1499. } else {
  1500. *buf2.end = 0;
  1501. zend_throw_error(zend_ffi_exception_ce, "Incompatible types when assigning to type '%s' from type '%s'", buf1.start, buf2.start);
  1502. }
  1503. } else {
  1504. zend_throw_error(zend_ffi_exception_ce, "Incompatible types when assigning to type '%s' from PHP '%s'", buf1.start, zend_zval_type_name(arg));
  1505. }
  1506. }
  1507. }
  1508. /* }}} */
  1509. static zend_string *zend_ffi_get_class_name(zend_string *prefix, const zend_ffi_type *type) /* {{{ */
  1510. {
  1511. zend_ffi_ctype_name_buf buf;
  1512. buf.start = buf.end = buf.buf + ((MAX_TYPE_NAME_LEN * 3) / 4);
  1513. if (!zend_ffi_ctype_name(&buf, type)) {
  1514. return zend_string_copy(prefix);
  1515. } else {
  1516. return zend_string_concat3(
  1517. ZSTR_VAL(prefix), ZSTR_LEN(prefix), ":", 1, buf.start, buf.end - buf.start);
  1518. }
  1519. }
  1520. /* }}} */
  1521. static zend_string *zend_ffi_cdata_get_class_name(const zend_object *zobj) /* {{{ */
  1522. {
  1523. zend_ffi_cdata *cdata = (zend_ffi_cdata*)zobj;
  1524. return zend_ffi_get_class_name(zobj->ce->name, ZEND_FFI_TYPE(cdata->type));
  1525. }
  1526. /* }}} */
  1527. static int zend_ffi_cdata_compare_objects(zval *o1, zval *o2) /* {{{ */
  1528. {
  1529. if (Z_TYPE_P(o1) == IS_OBJECT && Z_OBJCE_P(o1) == zend_ffi_cdata_ce &&
  1530. Z_TYPE_P(o2) == IS_OBJECT && Z_OBJCE_P(o2) == zend_ffi_cdata_ce) {
  1531. zend_ffi_cdata *cdata1 = (zend_ffi_cdata*)Z_OBJ_P(o1);
  1532. zend_ffi_cdata *cdata2 = (zend_ffi_cdata*)Z_OBJ_P(o2);
  1533. zend_ffi_type *type1 = ZEND_FFI_TYPE(cdata1->type);
  1534. zend_ffi_type *type2 = ZEND_FFI_TYPE(cdata2->type);
  1535. if (type1->kind == ZEND_FFI_TYPE_POINTER && type2->kind == ZEND_FFI_TYPE_POINTER) {
  1536. void *ptr1 = *(void**)cdata1->ptr;
  1537. void *ptr2 = *(void**)cdata2->ptr;
  1538. if (!ptr1 || !ptr2) {
  1539. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1540. return 0;
  1541. }
  1542. return ptr1 == ptr2 ? 0 : (ptr1 < ptr2 ? -1 : 1);
  1543. }
  1544. }
  1545. zend_throw_error(zend_ffi_exception_ce, "Comparison of incompatible C types");
  1546. return 0;
  1547. }
  1548. /* }}} */
  1549. static int zend_ffi_cdata_count_elements(zend_object *obj, zend_long *count) /* {{{ */
  1550. {
  1551. zend_ffi_cdata *cdata = (zend_ffi_cdata*)obj;
  1552. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  1553. if (type->kind != ZEND_FFI_TYPE_ARRAY) {
  1554. zend_throw_error(zend_ffi_exception_ce, "Attempt to count() on non C array");
  1555. return FAILURE;
  1556. } else {
  1557. *count = type->array.length;
  1558. return SUCCESS;
  1559. }
  1560. }
  1561. /* }}} */
  1562. static zend_object* zend_ffi_add(zend_ffi_cdata *base_cdata, zend_ffi_type *base_type, zend_long offset) /* {{{ */
  1563. {
  1564. char *ptr;
  1565. zend_ffi_type *ptr_type;
  1566. zend_ffi_cdata *cdata =
  1567. (zend_ffi_cdata*)zend_ffi_cdata_new(zend_ffi_cdata_ce);
  1568. if (base_type->kind == ZEND_FFI_TYPE_POINTER) {
  1569. if (ZEND_FFI_TYPE_IS_OWNED(base_cdata->type)) {
  1570. if (!(base_type->attr & ZEND_FFI_ATTR_STORED)) {
  1571. if (GC_REFCOUNT(&base_cdata->std) == 1) {
  1572. /* transfer type ownership */
  1573. base_cdata->type = base_type;
  1574. base_type = ZEND_FFI_TYPE_MAKE_OWNED(base_type);
  1575. } else {
  1576. base_cdata->type = base_type = zend_ffi_remember_type(base_type);
  1577. }
  1578. }
  1579. }
  1580. cdata->type = base_type;
  1581. ptr = (char*)(*(void**)base_cdata->ptr);
  1582. ptr_type = ZEND_FFI_TYPE(base_type)->pointer.type;
  1583. } else {
  1584. zend_ffi_type *new_type = emalloc(sizeof(zend_ffi_type));
  1585. new_type->kind = ZEND_FFI_TYPE_POINTER;
  1586. new_type->attr = 0;
  1587. new_type->size = sizeof(void*);
  1588. new_type->align = _Alignof(void*);
  1589. ptr_type = base_type->array.type;
  1590. if (ZEND_FFI_TYPE_IS_OWNED(ptr_type)) {
  1591. ptr_type = ZEND_FFI_TYPE(ptr_type);
  1592. if (!(ptr_type->attr & ZEND_FFI_ATTR_STORED)) {
  1593. if (GC_REFCOUNT(&base_cdata->std) == 1) {
  1594. /* transfer type ownership */
  1595. base_type->array.type = ptr_type;
  1596. ptr_type = ZEND_FFI_TYPE_MAKE_OWNED(ptr_type);
  1597. } else {
  1598. base_type->array.type = ptr_type = zend_ffi_remember_type(ptr_type);
  1599. }
  1600. }
  1601. }
  1602. new_type->pointer.type = ptr_type;
  1603. cdata->type = ZEND_FFI_TYPE_MAKE_OWNED(new_type);
  1604. ptr = (char*)base_cdata->ptr;
  1605. }
  1606. cdata->ptr = &cdata->ptr_holder;
  1607. cdata->ptr_holder = ptr +
  1608. (ptrdiff_t) (offset * ZEND_FFI_TYPE(ptr_type)->size);
  1609. cdata->flags = base_cdata->flags & ZEND_FFI_FLAG_CONST;
  1610. return &cdata->std;
  1611. }
  1612. /* }}} */
  1613. static int zend_ffi_cdata_do_operation(zend_uchar opcode, zval *result, zval *op1, zval *op2) /* {{{ */
  1614. {
  1615. zend_long offset;
  1616. ZVAL_DEREF(op1);
  1617. ZVAL_DEREF(op2);
  1618. if (Z_TYPE_P(op1) == IS_OBJECT && Z_OBJCE_P(op1) == zend_ffi_cdata_ce) {
  1619. zend_ffi_cdata *cdata1 = (zend_ffi_cdata*)Z_OBJ_P(op1);
  1620. zend_ffi_type *type1 = ZEND_FFI_TYPE(cdata1->type);
  1621. if (type1->kind == ZEND_FFI_TYPE_POINTER || type1->kind == ZEND_FFI_TYPE_ARRAY) {
  1622. if (opcode == ZEND_ADD) {
  1623. offset = zval_get_long(op2);
  1624. ZVAL_OBJ(result, zend_ffi_add(cdata1, type1, offset));
  1625. if (result == op1) {
  1626. OBJ_RELEASE(&cdata1->std);
  1627. }
  1628. return SUCCESS;
  1629. } else if (opcode == ZEND_SUB) {
  1630. if (Z_TYPE_P(op2) == IS_OBJECT && Z_OBJCE_P(op2) == zend_ffi_cdata_ce) {
  1631. zend_ffi_cdata *cdata2 = (zend_ffi_cdata*)Z_OBJ_P(op2);
  1632. zend_ffi_type *type2 = ZEND_FFI_TYPE(cdata2->type);
  1633. if (type2->kind == ZEND_FFI_TYPE_POINTER || type2->kind == ZEND_FFI_TYPE_ARRAY) {
  1634. zend_ffi_type *t1, *t2;
  1635. char *p1, *p2;
  1636. if (type1->kind == ZEND_FFI_TYPE_POINTER) {
  1637. t1 = ZEND_FFI_TYPE(type1->pointer.type);
  1638. p1 = (char*)(*(void**)cdata1->ptr);
  1639. } else {
  1640. t1 = ZEND_FFI_TYPE(type1->array.type);
  1641. p1 = cdata1->ptr;
  1642. }
  1643. if (type2->kind == ZEND_FFI_TYPE_POINTER) {
  1644. t2 = ZEND_FFI_TYPE(type2->pointer.type);
  1645. p2 = (char*)(*(void**)cdata2->ptr);
  1646. } else {
  1647. t2 = ZEND_FFI_TYPE(type2->array.type);
  1648. p2 = cdata2->ptr;
  1649. }
  1650. if (zend_ffi_is_same_type(t1, t2)) {
  1651. ZVAL_LONG(result,
  1652. (zend_long)(p1 - p2) / (zend_long)t1->size);
  1653. return SUCCESS;
  1654. }
  1655. }
  1656. }
  1657. offset = zval_get_long(op2);
  1658. ZVAL_OBJ(result, zend_ffi_add(cdata1, type1, -offset));
  1659. if (result == op1) {
  1660. OBJ_RELEASE(&cdata1->std);
  1661. }
  1662. return SUCCESS;
  1663. }
  1664. }
  1665. } else if (Z_TYPE_P(op2) == IS_OBJECT && Z_OBJCE_P(op2) == zend_ffi_cdata_ce) {
  1666. zend_ffi_cdata *cdata2 = (zend_ffi_cdata*)Z_OBJ_P(op2);
  1667. zend_ffi_type *type2 = ZEND_FFI_TYPE(cdata2->type);
  1668. if (type2->kind == ZEND_FFI_TYPE_POINTER || type2->kind == ZEND_FFI_TYPE_ARRAY) {
  1669. if (opcode == ZEND_ADD) {
  1670. offset = zval_get_long(op1);
  1671. ZVAL_OBJ(result, zend_ffi_add(cdata2, type2, offset));
  1672. return SUCCESS;
  1673. }
  1674. }
  1675. }
  1676. return FAILURE;
  1677. }
  1678. /* }}} */
  1679. typedef struct _zend_ffi_cdata_iterator {
  1680. zend_object_iterator it;
  1681. zend_long key;
  1682. zval value;
  1683. bool by_ref;
  1684. } zend_ffi_cdata_iterator;
  1685. static void zend_ffi_cdata_it_dtor(zend_object_iterator *iter) /* {{{ */
  1686. {
  1687. zval_ptr_dtor(&((zend_ffi_cdata_iterator*)iter)->value);
  1688. zval_ptr_dtor(&iter->data);
  1689. }
  1690. /* }}} */
  1691. static int zend_ffi_cdata_it_valid(zend_object_iterator *it) /* {{{ */
  1692. {
  1693. zend_ffi_cdata_iterator *iter = (zend_ffi_cdata_iterator*)it;
  1694. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ(iter->it.data);
  1695. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  1696. return (iter->key >= 0 && iter->key < type->array.length) ? SUCCESS : FAILURE;
  1697. }
  1698. /* }}} */
  1699. static zval *zend_ffi_cdata_it_get_current_data(zend_object_iterator *it) /* {{{ */
  1700. {
  1701. zend_ffi_cdata_iterator *iter = (zend_ffi_cdata_iterator*)it;
  1702. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ(iter->it.data);
  1703. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  1704. zend_ffi_type *dim_type;
  1705. void *ptr;
  1706. if (!cdata->ptr) {
  1707. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1708. return &EG(uninitialized_zval);
  1709. }
  1710. dim_type = type->array.type;
  1711. if (ZEND_FFI_TYPE_IS_OWNED(dim_type)) {
  1712. dim_type = ZEND_FFI_TYPE(dim_type);
  1713. if (!(dim_type->attr & ZEND_FFI_ATTR_STORED)
  1714. && dim_type->kind == ZEND_FFI_TYPE_POINTER) {
  1715. type->array.type = dim_type = zend_ffi_remember_type(dim_type);
  1716. }
  1717. }
  1718. ptr = (void*)((char*)cdata->ptr + dim_type->size * iter->it.index);
  1719. zval_ptr_dtor(&iter->value);
  1720. zend_ffi_cdata_to_zval(NULL, ptr, dim_type, iter->by_ref ? BP_VAR_RW : BP_VAR_R, &iter->value, (cdata->flags & ZEND_FFI_FLAG_CONST) | (zend_ffi_flags)(type->attr & ZEND_FFI_ATTR_CONST), 0, 0);
  1721. return &iter->value;
  1722. }
  1723. /* }}} */
  1724. static void zend_ffi_cdata_it_get_current_key(zend_object_iterator *it, zval *key) /* {{{ */
  1725. {
  1726. zend_ffi_cdata_iterator *iter = (zend_ffi_cdata_iterator*)it;
  1727. ZVAL_LONG(key, iter->key);
  1728. }
  1729. /* }}} */
  1730. static void zend_ffi_cdata_it_move_forward(zend_object_iterator *it) /* {{{ */
  1731. {
  1732. zend_ffi_cdata_iterator *iter = (zend_ffi_cdata_iterator*)it;
  1733. iter->key++;
  1734. }
  1735. /* }}} */
  1736. static void zend_ffi_cdata_it_rewind(zend_object_iterator *it) /* {{{ */
  1737. {
  1738. zend_ffi_cdata_iterator *iter = (zend_ffi_cdata_iterator*)it;
  1739. iter->key = 0;
  1740. }
  1741. /* }}} */
  1742. static const zend_object_iterator_funcs zend_ffi_cdata_it_funcs = {
  1743. zend_ffi_cdata_it_dtor,
  1744. zend_ffi_cdata_it_valid,
  1745. zend_ffi_cdata_it_get_current_data,
  1746. zend_ffi_cdata_it_get_current_key,
  1747. zend_ffi_cdata_it_move_forward,
  1748. zend_ffi_cdata_it_rewind,
  1749. NULL,
  1750. NULL, /* get_gc */
  1751. };
  1752. static zend_object_iterator *zend_ffi_cdata_get_iterator(zend_class_entry *ce, zval *object, int by_ref) /* {{{ */
  1753. {
  1754. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(object);
  1755. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  1756. zend_ffi_cdata_iterator *iter;
  1757. if (type->kind != ZEND_FFI_TYPE_ARRAY) {
  1758. zend_throw_error(zend_ffi_exception_ce, "Attempt to iterate on non C array");
  1759. return NULL;
  1760. }
  1761. iter = emalloc(sizeof(zend_ffi_cdata_iterator));
  1762. zend_iterator_init(&iter->it);
  1763. Z_ADDREF_P(object);
  1764. ZVAL_OBJ(&iter->it.data, Z_OBJ_P(object));
  1765. iter->it.funcs = &zend_ffi_cdata_it_funcs;
  1766. iter->key = 0;
  1767. iter->by_ref = by_ref;
  1768. ZVAL_UNDEF(&iter->value);
  1769. return &iter->it;
  1770. }
  1771. /* }}} */
  1772. static HashTable *zend_ffi_cdata_get_debug_info(zend_object *obj, int *is_temp) /* {{{ */
  1773. {
  1774. zend_ffi_cdata *cdata = (zend_ffi_cdata*)obj;
  1775. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  1776. void *ptr = cdata->ptr;
  1777. HashTable *ht = NULL;
  1778. zend_string *key;
  1779. zend_ffi_field *f;
  1780. zend_long n;
  1781. zval tmp;
  1782. if (!cdata->ptr) {
  1783. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1784. return NULL;
  1785. }
  1786. switch (type->kind) {
  1787. case ZEND_FFI_TYPE_BOOL:
  1788. case ZEND_FFI_TYPE_CHAR:
  1789. case ZEND_FFI_TYPE_ENUM:
  1790. case ZEND_FFI_TYPE_FLOAT:
  1791. case ZEND_FFI_TYPE_DOUBLE:
  1792. #ifdef HAVE_LONG_DOUBLE
  1793. case ZEND_FFI_TYPE_LONGDOUBLE:
  1794. #endif
  1795. case ZEND_FFI_TYPE_UINT8:
  1796. case ZEND_FFI_TYPE_SINT8:
  1797. case ZEND_FFI_TYPE_UINT16:
  1798. case ZEND_FFI_TYPE_SINT16:
  1799. case ZEND_FFI_TYPE_UINT32:
  1800. case ZEND_FFI_TYPE_SINT32:
  1801. case ZEND_FFI_TYPE_UINT64:
  1802. case ZEND_FFI_TYPE_SINT64:
  1803. zend_ffi_cdata_to_zval(cdata, ptr, type, BP_VAR_R, &tmp, ZEND_FFI_FLAG_CONST, 0, 0);
  1804. ht = zend_new_array(1);
  1805. zend_hash_str_add(ht, "cdata", sizeof("cdata")-1, &tmp);
  1806. *is_temp = 1;
  1807. return ht;
  1808. break;
  1809. case ZEND_FFI_TYPE_POINTER:
  1810. if (*(void**)ptr == NULL) {
  1811. ZVAL_NULL(&tmp);
  1812. ht = zend_new_array(1);
  1813. zend_hash_index_add_new(ht, 0, &tmp);
  1814. *is_temp = 1;
  1815. return ht;
  1816. } else if (ZEND_FFI_TYPE(type->pointer.type)->kind == ZEND_FFI_TYPE_VOID) {
  1817. ZVAL_LONG(&tmp, (uintptr_t)*(void**)ptr);
  1818. ht = zend_new_array(1);
  1819. zend_hash_index_add_new(ht, 0, &tmp);
  1820. *is_temp = 1;
  1821. return ht;
  1822. } else {
  1823. zend_ffi_cdata_to_zval(NULL, *(void**)ptr, ZEND_FFI_TYPE(type->pointer.type), BP_VAR_R, &tmp, ZEND_FFI_FLAG_CONST, 0, 0);
  1824. ht = zend_new_array(1);
  1825. zend_hash_index_add_new(ht, 0, &tmp);
  1826. *is_temp = 1;
  1827. return ht;
  1828. }
  1829. break;
  1830. case ZEND_FFI_TYPE_STRUCT:
  1831. ht = zend_new_array(zend_hash_num_elements(&type->record.fields));
  1832. ZEND_HASH_FOREACH_STR_KEY_PTR(&type->record.fields, key, f) {
  1833. if (key) {
  1834. if (!f->bits) {
  1835. void *f_ptr = (void*)(((char*)ptr) + f->offset);
  1836. zend_ffi_cdata_to_zval(NULL, f_ptr, ZEND_FFI_TYPE(f->type), BP_VAR_R, &tmp, ZEND_FFI_FLAG_CONST, 0, type->attr & ZEND_FFI_ATTR_UNION);
  1837. zend_hash_add(ht, key, &tmp);
  1838. } else {
  1839. zend_ffi_bit_field_to_zval(ptr, f, &tmp);
  1840. zend_hash_add(ht, key, &tmp);
  1841. }
  1842. }
  1843. } ZEND_HASH_FOREACH_END();
  1844. *is_temp = 1;
  1845. return ht;
  1846. case ZEND_FFI_TYPE_ARRAY:
  1847. ht = zend_new_array(type->array.length);
  1848. for (n = 0; n < type->array.length; n++) {
  1849. zend_ffi_cdata_to_zval(NULL, ptr, ZEND_FFI_TYPE(type->array.type), BP_VAR_R, &tmp, ZEND_FFI_FLAG_CONST, 0, 0);
  1850. zend_hash_index_add(ht, n, &tmp);
  1851. ptr = (void*)(((char*)ptr) + ZEND_FFI_TYPE(type->array.type)->size);
  1852. }
  1853. *is_temp = 1;
  1854. return ht;
  1855. case ZEND_FFI_TYPE_FUNC:
  1856. ht = zend_new_array(0);
  1857. // TODO: function name ???
  1858. *is_temp = 1;
  1859. return ht;
  1860. break;
  1861. default:
  1862. ZEND_UNREACHABLE();
  1863. break;
  1864. }
  1865. return NULL;
  1866. }
  1867. /* }}} */
  1868. static int zend_ffi_cdata_get_closure(zend_object *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr, bool check_only) /* {{{ */
  1869. {
  1870. zend_ffi_cdata *cdata = (zend_ffi_cdata*)obj;
  1871. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  1872. zend_function *func;
  1873. if (type->kind != ZEND_FFI_TYPE_POINTER) {
  1874. if (!check_only) {
  1875. zend_throw_error(zend_ffi_exception_ce, "Attempt to call non C function pointer");
  1876. }
  1877. return FAILURE;
  1878. }
  1879. type = ZEND_FFI_TYPE(type->pointer.type);
  1880. if (type->kind != ZEND_FFI_TYPE_FUNC) {
  1881. if (!check_only) {
  1882. zend_throw_error(zend_ffi_exception_ce, "Attempt to call non C function pointer");
  1883. }
  1884. return FAILURE;
  1885. }
  1886. if (!cdata->ptr) {
  1887. if (!check_only) {
  1888. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  1889. }
  1890. return FAILURE;
  1891. }
  1892. if (EXPECTED(EG(trampoline).common.function_name == NULL)) {
  1893. func = &EG(trampoline);
  1894. } else {
  1895. func = ecalloc(sizeof(zend_internal_function), 1);
  1896. }
  1897. func->type = ZEND_INTERNAL_FUNCTION;
  1898. func->common.arg_flags[0] = 0;
  1899. func->common.arg_flags[1] = 0;
  1900. func->common.arg_flags[2] = 0;
  1901. func->common.fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE;
  1902. func->common.function_name = ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE);
  1903. /* set to 0 to avoid arg_info[] allocation, because all values are passed by value anyway */
  1904. func->common.num_args = 0;
  1905. func->common.required_num_args = type->func.args ? zend_hash_num_elements(type->func.args) : 0;
  1906. func->common.scope = NULL;
  1907. func->common.prototype = NULL;
  1908. func->common.arg_info = NULL;
  1909. func->internal_function.handler = ZEND_FN(ffi_trampoline);
  1910. func->internal_function.module = NULL;
  1911. func->internal_function.reserved[0] = type;
  1912. func->internal_function.reserved[1] = *(void**)cdata->ptr;
  1913. *ce_ptr = NULL;
  1914. *fptr_ptr= func;
  1915. *obj_ptr = NULL;
  1916. return SUCCESS;
  1917. }
  1918. /* }}} */
  1919. static zend_object *zend_ffi_ctype_new(zend_class_entry *class_type) /* {{{ */
  1920. {
  1921. zend_ffi_ctype *ctype;
  1922. ctype = emalloc(sizeof(zend_ffi_ctype));
  1923. zend_ffi_object_init(&ctype->std, class_type);
  1924. ctype->std.handlers = &zend_ffi_ctype_handlers;
  1925. ctype->type = NULL;
  1926. return &ctype->std;
  1927. }
  1928. /* }}} */
  1929. static void zend_ffi_ctype_free_obj(zend_object *object) /* {{{ */
  1930. {
  1931. zend_ffi_ctype *ctype = (zend_ffi_ctype*)object;
  1932. zend_ffi_type_dtor(ctype->type);
  1933. }
  1934. /* }}} */
  1935. static int zend_ffi_is_same_type(zend_ffi_type *type1, zend_ffi_type *type2) /* {{{ */
  1936. {
  1937. while (1) {
  1938. if (type1 == type2) {
  1939. return 1;
  1940. } else if (type1->kind == type2->kind) {
  1941. if (type1->kind < ZEND_FFI_TYPE_POINTER) {
  1942. return 1;
  1943. } else if (type1->kind == ZEND_FFI_TYPE_POINTER) {
  1944. type1 = ZEND_FFI_TYPE(type1->pointer.type);
  1945. type2 = ZEND_FFI_TYPE(type2->pointer.type);
  1946. if (type1->kind == ZEND_FFI_TYPE_VOID ||
  1947. type2->kind == ZEND_FFI_TYPE_VOID) {
  1948. return 1;
  1949. }
  1950. } else if (type1->kind == ZEND_FFI_TYPE_ARRAY &&
  1951. type1->array.length == type2->array.length) {
  1952. type1 = ZEND_FFI_TYPE(type1->array.type);
  1953. type2 = ZEND_FFI_TYPE(type2->array.type);
  1954. } else {
  1955. break;
  1956. }
  1957. } else {
  1958. break;
  1959. }
  1960. }
  1961. return 0;
  1962. }
  1963. /* }}} */
  1964. static zend_string *zend_ffi_ctype_get_class_name(const zend_object *zobj) /* {{{ */
  1965. {
  1966. zend_ffi_ctype *ctype = (zend_ffi_ctype*)zobj;
  1967. return zend_ffi_get_class_name(zobj->ce->name, ZEND_FFI_TYPE(ctype->type));
  1968. }
  1969. /* }}} */
  1970. static int zend_ffi_ctype_compare_objects(zval *o1, zval *o2) /* {{{ */
  1971. {
  1972. if (Z_TYPE_P(o1) == IS_OBJECT && Z_OBJCE_P(o1) == zend_ffi_ctype_ce &&
  1973. Z_TYPE_P(o2) == IS_OBJECT && Z_OBJCE_P(o2) == zend_ffi_ctype_ce) {
  1974. zend_ffi_ctype *ctype1 = (zend_ffi_ctype*)Z_OBJ_P(o1);
  1975. zend_ffi_ctype *ctype2 = (zend_ffi_ctype*)Z_OBJ_P(o2);
  1976. zend_ffi_type *type1 = ZEND_FFI_TYPE(ctype1->type);
  1977. zend_ffi_type *type2 = ZEND_FFI_TYPE(ctype2->type);
  1978. if (zend_ffi_is_same_type(type1, type2)) {
  1979. return 0;
  1980. } else {
  1981. return 1;
  1982. }
  1983. }
  1984. zend_throw_error(zend_ffi_exception_ce, "Comparison of incompatible C types");
  1985. return 0;
  1986. }
  1987. /* }}} */
  1988. static HashTable *zend_ffi_ctype_get_debug_info(zend_object *obj, int *is_temp) /* {{{ */
  1989. {
  1990. return NULL;
  1991. }
  1992. /* }}} */
  1993. static zend_object *zend_ffi_new(zend_class_entry *class_type) /* {{{ */
  1994. {
  1995. zend_ffi *ffi;
  1996. ffi = emalloc(sizeof(zend_ffi));
  1997. zend_ffi_object_init(&ffi->std, class_type);
  1998. ffi->std.handlers = &zend_ffi_handlers;
  1999. ffi->lib = NULL;
  2000. ffi->symbols = NULL;
  2001. ffi->tags = NULL;
  2002. ffi->persistent = 0;
  2003. return &ffi->std;
  2004. }
  2005. /* }}} */
  2006. static void _zend_ffi_type_dtor(zend_ffi_type *type) /* {{{ */
  2007. {
  2008. type = ZEND_FFI_TYPE(type);
  2009. switch (type->kind) {
  2010. case ZEND_FFI_TYPE_ENUM:
  2011. if (type->enumeration.tag_name) {
  2012. zend_string_release(type->enumeration.tag_name);
  2013. }
  2014. break;
  2015. case ZEND_FFI_TYPE_STRUCT:
  2016. if (type->record.tag_name) {
  2017. zend_string_release(type->record.tag_name);
  2018. }
  2019. zend_hash_destroy(&type->record.fields);
  2020. break;
  2021. case ZEND_FFI_TYPE_POINTER:
  2022. zend_ffi_type_dtor(type->pointer.type);
  2023. break;
  2024. case ZEND_FFI_TYPE_ARRAY:
  2025. zend_ffi_type_dtor(type->array.type);
  2026. break;
  2027. case ZEND_FFI_TYPE_FUNC:
  2028. if (type->func.args) {
  2029. zend_hash_destroy(type->func.args);
  2030. pefree(type->func.args, type->attr & ZEND_FFI_ATTR_PERSISTENT);
  2031. }
  2032. zend_ffi_type_dtor(type->func.ret_type);
  2033. break;
  2034. default:
  2035. break;
  2036. }
  2037. pefree(type, type->attr & ZEND_FFI_ATTR_PERSISTENT);
  2038. }
  2039. /* }}} */
  2040. static void zend_ffi_type_hash_dtor(zval *zv) /* {{{ */
  2041. {
  2042. zend_ffi_type *type = Z_PTR_P(zv);
  2043. zend_ffi_type_dtor(type);
  2044. }
  2045. /* }}} */
  2046. static void zend_ffi_field_hash_dtor(zval *zv) /* {{{ */
  2047. {
  2048. zend_ffi_field *field = Z_PTR_P(zv);
  2049. zend_ffi_type_dtor(field->type);
  2050. efree(field);
  2051. }
  2052. /* }}} */
  2053. static void zend_ffi_field_hash_persistent_dtor(zval *zv) /* {{{ */
  2054. {
  2055. zend_ffi_field *field = Z_PTR_P(zv);
  2056. zend_ffi_type_dtor(field->type);
  2057. free(field);
  2058. }
  2059. /* }}} */
  2060. static void zend_ffi_symbol_hash_dtor(zval *zv) /* {{{ */
  2061. {
  2062. zend_ffi_symbol *sym = Z_PTR_P(zv);
  2063. zend_ffi_type_dtor(sym->type);
  2064. efree(sym);
  2065. }
  2066. /* }}} */
  2067. static void zend_ffi_symbol_hash_persistent_dtor(zval *zv) /* {{{ */
  2068. {
  2069. zend_ffi_symbol *sym = Z_PTR_P(zv);
  2070. zend_ffi_type_dtor(sym->type);
  2071. free(sym);
  2072. }
  2073. /* }}} */
  2074. static void zend_ffi_tag_hash_dtor(zval *zv) /* {{{ */
  2075. {
  2076. zend_ffi_tag *tag = Z_PTR_P(zv);
  2077. zend_ffi_type_dtor(tag->type);
  2078. efree(tag);
  2079. }
  2080. /* }}} */
  2081. static void zend_ffi_tag_hash_persistent_dtor(zval *zv) /* {{{ */
  2082. {
  2083. zend_ffi_tag *tag = Z_PTR_P(zv);
  2084. zend_ffi_type_dtor(tag->type);
  2085. free(tag);
  2086. }
  2087. /* }}} */
  2088. static void zend_ffi_cdata_dtor(zend_ffi_cdata *cdata) /* {{{ */
  2089. {
  2090. zend_ffi_type_dtor(cdata->type);
  2091. if (cdata->flags & ZEND_FFI_FLAG_OWNED) {
  2092. if (cdata->ptr != (void*)&cdata->ptr_holder) {
  2093. pefree(cdata->ptr, cdata->flags & ZEND_FFI_FLAG_PERSISTENT);
  2094. } else {
  2095. pefree(cdata->ptr_holder, cdata->flags & ZEND_FFI_FLAG_PERSISTENT);
  2096. }
  2097. }
  2098. }
  2099. /* }}} */
  2100. static void zend_ffi_scope_hash_dtor(zval *zv) /* {{{ */
  2101. {
  2102. zend_ffi_scope *scope = Z_PTR_P(zv);
  2103. if (scope->symbols) {
  2104. zend_hash_destroy(scope->symbols);
  2105. free(scope->symbols);
  2106. }
  2107. if (scope->tags) {
  2108. zend_hash_destroy(scope->tags);
  2109. free(scope->tags);
  2110. }
  2111. free(scope);
  2112. }
  2113. /* }}} */
  2114. static void zend_ffi_free_obj(zend_object *object) /* {{{ */
  2115. {
  2116. zend_ffi *ffi = (zend_ffi*)object;
  2117. if (ffi->persistent) {
  2118. return;
  2119. }
  2120. if (ffi->lib) {
  2121. DL_UNLOAD(ffi->lib);
  2122. ffi->lib = NULL;
  2123. }
  2124. if (ffi->symbols) {
  2125. zend_hash_destroy(ffi->symbols);
  2126. efree(ffi->symbols);
  2127. }
  2128. if (ffi->tags) {
  2129. zend_hash_destroy(ffi->tags);
  2130. efree(ffi->tags);
  2131. }
  2132. }
  2133. /* }}} */
  2134. static void zend_ffi_cdata_free_obj(zend_object *object) /* {{{ */
  2135. {
  2136. zend_ffi_cdata *cdata = (zend_ffi_cdata*)object;
  2137. zend_ffi_cdata_dtor(cdata);
  2138. }
  2139. /* }}} */
  2140. static zend_object *zend_ffi_cdata_clone_obj(zend_object *obj) /* {{{ */
  2141. {
  2142. zend_ffi_cdata *old_cdata = (zend_ffi_cdata*)obj;
  2143. zend_ffi_type *type = ZEND_FFI_TYPE(old_cdata->type);
  2144. zend_ffi_cdata *new_cdata;
  2145. new_cdata = (zend_ffi_cdata*)zend_ffi_cdata_new(zend_ffi_cdata_ce);
  2146. if (type->kind < ZEND_FFI_TYPE_POINTER) {
  2147. new_cdata->std.handlers = &zend_ffi_cdata_value_handlers;
  2148. }
  2149. new_cdata->type = type;
  2150. new_cdata->ptr = emalloc(type->size);
  2151. memcpy(new_cdata->ptr, old_cdata->ptr, type->size);
  2152. new_cdata->flags |= ZEND_FFI_FLAG_OWNED;
  2153. return &new_cdata->std;
  2154. }
  2155. /* }}} */
  2156. static zval *zend_ffi_read_var(zend_object *obj, zend_string *var_name, int read_type, void **cache_slot, zval *rv) /* {{{ */
  2157. {
  2158. zend_ffi *ffi = (zend_ffi*)obj;
  2159. zend_ffi_symbol *sym = NULL;
  2160. if (ffi->symbols) {
  2161. sym = zend_hash_find_ptr(ffi->symbols, var_name);
  2162. if (sym && sym->kind != ZEND_FFI_SYM_VAR && sym->kind != ZEND_FFI_SYM_CONST && sym->kind != ZEND_FFI_SYM_FUNC) {
  2163. sym = NULL;
  2164. }
  2165. }
  2166. if (!sym) {
  2167. zend_throw_error(zend_ffi_exception_ce, "Attempt to read undefined C variable '%s'", ZSTR_VAL(var_name));
  2168. return &EG(uninitialized_zval);
  2169. }
  2170. if (sym->kind == ZEND_FFI_SYM_VAR) {
  2171. zend_ffi_cdata_to_zval(NULL, sym->addr, ZEND_FFI_TYPE(sym->type), read_type, rv, (zend_ffi_flags)sym->is_const, 0, 0);
  2172. } else if (sym->kind == ZEND_FFI_SYM_FUNC) {
  2173. zend_ffi_cdata *cdata;
  2174. zend_ffi_type *new_type = emalloc(sizeof(zend_ffi_type));
  2175. new_type->kind = ZEND_FFI_TYPE_POINTER;
  2176. new_type->attr = 0;
  2177. new_type->size = sizeof(void*);
  2178. new_type->align = _Alignof(void*);
  2179. new_type->pointer.type = ZEND_FFI_TYPE(sym->type);
  2180. cdata = emalloc(sizeof(zend_ffi_cdata));
  2181. zend_ffi_object_init(&cdata->std, zend_ffi_cdata_ce);
  2182. cdata->std.handlers = &zend_ffi_cdata_handlers;
  2183. cdata->type = ZEND_FFI_TYPE_MAKE_OWNED(new_type);
  2184. cdata->flags = ZEND_FFI_FLAG_CONST;
  2185. cdata->ptr_holder = sym->addr;
  2186. cdata->ptr = &cdata->ptr_holder;
  2187. ZVAL_OBJ(rv, &cdata->std);
  2188. } else {
  2189. ZVAL_LONG(rv, sym->value);
  2190. }
  2191. return rv;
  2192. }
  2193. /* }}} */
  2194. static zval *zend_ffi_write_var(zend_object *obj, zend_string *var_name, zval *value, void **cache_slot) /* {{{ */
  2195. {
  2196. zend_ffi *ffi = (zend_ffi*)obj;
  2197. zend_ffi_symbol *sym = NULL;
  2198. if (ffi->symbols) {
  2199. sym = zend_hash_find_ptr(ffi->symbols, var_name);
  2200. if (sym && sym->kind != ZEND_FFI_SYM_VAR) {
  2201. sym = NULL;
  2202. }
  2203. }
  2204. if (!sym) {
  2205. zend_throw_error(zend_ffi_exception_ce, "Attempt to assign undefined C variable '%s'", ZSTR_VAL(var_name));
  2206. return value;
  2207. }
  2208. if (sym->is_const) {
  2209. zend_throw_error(zend_ffi_exception_ce, "Attempt to assign read-only C variable '%s'", ZSTR_VAL(var_name));
  2210. return value;
  2211. }
  2212. zend_ffi_zval_to_cdata(sym->addr, ZEND_FFI_TYPE(sym->type), value);
  2213. return value;
  2214. }
  2215. /* }}} */
  2216. static int zend_ffi_pass_arg(zval *arg, zend_ffi_type *type, ffi_type **pass_type, void **arg_values, uint32_t n, zend_execute_data *execute_data) /* {{{ */
  2217. {
  2218. zend_long lval;
  2219. double dval;
  2220. zend_string *str, *tmp_str;
  2221. zend_ffi_type_kind kind = type->kind;
  2222. ZVAL_DEREF(arg);
  2223. again:
  2224. switch (kind) {
  2225. case ZEND_FFI_TYPE_FLOAT:
  2226. dval = zval_get_double(arg);
  2227. *pass_type = &ffi_type_float;
  2228. *(float*)arg_values[n] = (float)dval;
  2229. break;
  2230. case ZEND_FFI_TYPE_DOUBLE:
  2231. dval = zval_get_double(arg);
  2232. *pass_type = &ffi_type_double;
  2233. *(double*)arg_values[n] = dval;
  2234. break;
  2235. #ifdef HAVE_LONG_DOUBLE
  2236. case ZEND_FFI_TYPE_LONGDOUBLE:
  2237. dval = zval_get_double(arg);
  2238. *pass_type = &ffi_type_double;
  2239. *(long double*)arg_values[n] = (long double)dval;
  2240. break;
  2241. #endif
  2242. case ZEND_FFI_TYPE_UINT8:
  2243. lval = zval_get_long(arg);
  2244. *pass_type = &ffi_type_uint8;
  2245. *(uint8_t*)arg_values[n] = (uint8_t)lval;
  2246. break;
  2247. case ZEND_FFI_TYPE_SINT8:
  2248. lval = zval_get_long(arg);
  2249. *pass_type = &ffi_type_sint8;
  2250. *(int8_t*)arg_values[n] = (int8_t)lval;
  2251. break;
  2252. case ZEND_FFI_TYPE_UINT16:
  2253. lval = zval_get_long(arg);
  2254. *pass_type = &ffi_type_uint16;
  2255. *(uint16_t*)arg_values[n] = (uint16_t)lval;
  2256. break;
  2257. case ZEND_FFI_TYPE_SINT16:
  2258. lval = zval_get_long(arg);
  2259. *pass_type = &ffi_type_sint16;
  2260. *(int16_t*)arg_values[n] = (int16_t)lval;
  2261. break;
  2262. case ZEND_FFI_TYPE_UINT32:
  2263. lval = zval_get_long(arg);
  2264. *pass_type = &ffi_type_uint32;
  2265. *(uint32_t*)arg_values[n] = (uint32_t)lval;
  2266. break;
  2267. case ZEND_FFI_TYPE_SINT32:
  2268. lval = zval_get_long(arg);
  2269. *pass_type = &ffi_type_sint32;
  2270. *(int32_t*)arg_values[n] = (int32_t)lval;
  2271. break;
  2272. case ZEND_FFI_TYPE_UINT64:
  2273. lval = zval_get_long(arg);
  2274. *pass_type = &ffi_type_uint64;
  2275. *(uint64_t*)arg_values[n] = (uint64_t)lval;
  2276. break;
  2277. case ZEND_FFI_TYPE_SINT64:
  2278. lval = zval_get_long(arg);
  2279. *pass_type = &ffi_type_sint64;
  2280. *(int64_t*)arg_values[n] = (int64_t)lval;
  2281. break;
  2282. case ZEND_FFI_TYPE_POINTER:
  2283. *pass_type = &ffi_type_pointer;
  2284. if (Z_TYPE_P(arg) == IS_NULL) {
  2285. *(void**)arg_values[n] = NULL;
  2286. return SUCCESS;
  2287. } else if (Z_TYPE_P(arg) == IS_STRING
  2288. && ((ZEND_FFI_TYPE(type->pointer.type)->kind == ZEND_FFI_TYPE_CHAR)
  2289. || (ZEND_FFI_TYPE(type->pointer.type)->kind == ZEND_FFI_TYPE_VOID))) {
  2290. *(void**)arg_values[n] = Z_STRVAL_P(arg);
  2291. return SUCCESS;
  2292. } else if (Z_TYPE_P(arg) == IS_OBJECT && Z_OBJCE_P(arg) == zend_ffi_cdata_ce) {
  2293. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(arg);
  2294. if (zend_ffi_is_compatible_type(type, ZEND_FFI_TYPE(cdata->type))) {
  2295. if (ZEND_FFI_TYPE(cdata->type)->kind == ZEND_FFI_TYPE_POINTER) {
  2296. if (!cdata->ptr) {
  2297. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  2298. return FAILURE;
  2299. }
  2300. *(void**)arg_values[n] = *(void**)cdata->ptr;
  2301. } else {
  2302. *(void**)arg_values[n] = cdata->ptr;
  2303. }
  2304. return SUCCESS;
  2305. }
  2306. #if FFI_CLOSURES
  2307. } else if (ZEND_FFI_TYPE(type->pointer.type)->kind == ZEND_FFI_TYPE_FUNC) {
  2308. void *callback = zend_ffi_create_callback(ZEND_FFI_TYPE(type->pointer.type), arg);
  2309. if (callback) {
  2310. *(void**)arg_values[n] = callback;
  2311. break;
  2312. } else {
  2313. return FAILURE;
  2314. }
  2315. #endif
  2316. }
  2317. zend_ffi_pass_incompatible(arg, type, n, execute_data);
  2318. return FAILURE;
  2319. case ZEND_FFI_TYPE_BOOL:
  2320. *pass_type = &ffi_type_uint8;
  2321. *(uint8_t*)arg_values[n] = zend_is_true(arg);
  2322. break;
  2323. case ZEND_FFI_TYPE_CHAR:
  2324. str = zval_get_tmp_string(arg, &tmp_str);
  2325. *pass_type = &ffi_type_sint8;
  2326. *(char*)arg_values[n] = ZSTR_VAL(str)[0];
  2327. if (ZSTR_LEN(str) != 1) {
  2328. zend_ffi_pass_incompatible(arg, type, n, execute_data);
  2329. }
  2330. zend_tmp_string_release(tmp_str);
  2331. break;
  2332. case ZEND_FFI_TYPE_ENUM:
  2333. kind = type->enumeration.kind;
  2334. goto again;
  2335. case ZEND_FFI_TYPE_STRUCT:
  2336. if (Z_TYPE_P(arg) == IS_OBJECT && Z_OBJCE_P(arg) == zend_ffi_cdata_ce) {
  2337. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(arg);
  2338. if (zend_ffi_is_compatible_type(type, ZEND_FFI_TYPE(cdata->type))) {
  2339. *pass_type = zend_ffi_make_fake_struct_type(type);;
  2340. arg_values[n] = cdata->ptr;
  2341. break;
  2342. }
  2343. }
  2344. zend_ffi_pass_incompatible(arg, type, n, execute_data);
  2345. return FAILURE;
  2346. default:
  2347. zend_ffi_pass_unsupported(type);
  2348. return FAILURE;
  2349. }
  2350. return SUCCESS;
  2351. }
  2352. /* }}} */
  2353. static int zend_ffi_pass_var_arg(zval *arg, ffi_type **pass_type, void **arg_values, uint32_t n, zend_execute_data *execute_data) /* {{{ */
  2354. {
  2355. ZVAL_DEREF(arg);
  2356. switch (Z_TYPE_P(arg)) {
  2357. case IS_NULL:
  2358. *pass_type = &ffi_type_pointer;
  2359. *(void**)arg_values[n] = NULL;
  2360. break;
  2361. case IS_FALSE:
  2362. *pass_type = &ffi_type_uint8;
  2363. *(uint8_t*)arg_values[n] = 0;
  2364. break;
  2365. case IS_TRUE:
  2366. *pass_type = &ffi_type_uint8;
  2367. *(uint8_t*)arg_values[n] = 1;
  2368. break;
  2369. case IS_LONG:
  2370. if (sizeof(zend_long) == 4) {
  2371. *pass_type = &ffi_type_sint32;
  2372. *(int32_t*)arg_values[n] = Z_LVAL_P(arg);
  2373. } else {
  2374. *pass_type = &ffi_type_sint64;
  2375. *(int64_t*)arg_values[n] = Z_LVAL_P(arg);
  2376. }
  2377. break;
  2378. case IS_DOUBLE:
  2379. *pass_type = &ffi_type_double;
  2380. *(double*)arg_values[n] = Z_DVAL_P(arg);
  2381. break;
  2382. case IS_STRING:
  2383. *pass_type = &ffi_type_pointer;
  2384. *(char**)arg_values[n] = Z_STRVAL_P(arg);
  2385. break;
  2386. case IS_OBJECT:
  2387. if (Z_OBJCE_P(arg) == zend_ffi_cdata_ce) {
  2388. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(arg);
  2389. zend_ffi_type *type = ZEND_FFI_TYPE(cdata->type);
  2390. return zend_ffi_pass_arg(arg, type, pass_type, arg_values, n, execute_data);
  2391. }
  2392. ZEND_FALLTHROUGH;
  2393. default:
  2394. zend_throw_error(zend_ffi_exception_ce, "Unsupported argument type");
  2395. return FAILURE;
  2396. }
  2397. return SUCCESS;
  2398. }
  2399. /* }}} */
  2400. static ZEND_FUNCTION(ffi_trampoline) /* {{{ */
  2401. {
  2402. zend_ffi_type *type = EX(func)->internal_function.reserved[0];
  2403. void *addr = EX(func)->internal_function.reserved[1];
  2404. ffi_cif cif;
  2405. ffi_type *ret_type = NULL;
  2406. ffi_type **arg_types = NULL;
  2407. void **arg_values = NULL;
  2408. uint32_t n, arg_count;
  2409. void *ret;
  2410. zend_ffi_type *arg_type;
  2411. ALLOCA_FLAG(arg_types_use_heap = 0)
  2412. ALLOCA_FLAG(arg_values_use_heap = 0)
  2413. ALLOCA_FLAG(ret_use_heap = 0)
  2414. ZEND_ASSERT(type->kind == ZEND_FFI_TYPE_FUNC);
  2415. arg_count = type->func.args ? zend_hash_num_elements(type->func.args) : 0;
  2416. if (type->attr & ZEND_FFI_ATTR_VARIADIC) {
  2417. if (arg_count > EX_NUM_ARGS()) {
  2418. zend_throw_error(zend_ffi_exception_ce, "Incorrect number of arguments for C function '%s', expecting at least %d parameter%s", ZSTR_VAL(EX(func)->internal_function.function_name), arg_count, (arg_count != 1) ? "s" : "");
  2419. goto exit;
  2420. }
  2421. if (EX_NUM_ARGS()) {
  2422. arg_types = do_alloca(
  2423. sizeof(ffi_type*) * EX_NUM_ARGS(), arg_types_use_heap);
  2424. arg_values = do_alloca(
  2425. (sizeof(void*) + ZEND_FFI_SIZEOF_ARG) * EX_NUM_ARGS(), arg_values_use_heap);
  2426. n = 0;
  2427. if (type->func.args) {
  2428. ZEND_HASH_FOREACH_PTR(type->func.args, arg_type) {
  2429. arg_type = ZEND_FFI_TYPE(arg_type);
  2430. arg_values[n] = ((char*)arg_values) + (sizeof(void*) * EX_NUM_ARGS()) + (ZEND_FFI_SIZEOF_ARG * n);
  2431. if (zend_ffi_pass_arg(EX_VAR_NUM(n), arg_type, &arg_types[n], arg_values, n, execute_data) != SUCCESS) {
  2432. free_alloca(arg_types, arg_types_use_heap);
  2433. free_alloca(arg_values, arg_values_use_heap);
  2434. goto exit;
  2435. }
  2436. n++;
  2437. } ZEND_HASH_FOREACH_END();
  2438. }
  2439. for (; n < EX_NUM_ARGS(); n++) {
  2440. arg_values[n] = ((char*)arg_values) + (sizeof(void*) * EX_NUM_ARGS()) + (ZEND_FFI_SIZEOF_ARG * n);
  2441. if (zend_ffi_pass_var_arg(EX_VAR_NUM(n), &arg_types[n], arg_values, n, execute_data) != SUCCESS) {
  2442. free_alloca(arg_types, arg_types_use_heap);
  2443. free_alloca(arg_values, arg_values_use_heap);
  2444. goto exit;
  2445. }
  2446. }
  2447. }
  2448. ret_type = zend_ffi_get_type(ZEND_FFI_TYPE(type->func.ret_type));
  2449. if (!ret_type) {
  2450. zend_ffi_return_unsupported(type->func.ret_type);
  2451. free_alloca(arg_types, arg_types_use_heap);
  2452. free_alloca(arg_values, arg_values_use_heap);
  2453. goto exit;
  2454. }
  2455. if (ffi_prep_cif_var(&cif, type->func.abi, arg_count, EX_NUM_ARGS(), ret_type, arg_types) != FFI_OK) {
  2456. zend_throw_error(zend_ffi_exception_ce, "Cannot prepare callback CIF");
  2457. free_alloca(arg_types, arg_types_use_heap);
  2458. free_alloca(arg_values, arg_values_use_heap);
  2459. goto exit;
  2460. }
  2461. } else {
  2462. if (arg_count != EX_NUM_ARGS()) {
  2463. zend_throw_error(zend_ffi_exception_ce, "Incorrect number of arguments for C function '%s', expecting exactly %d parameter%s", ZSTR_VAL(EX(func)->internal_function.function_name), arg_count, (arg_count != 1) ? "s" : "");
  2464. goto exit;
  2465. }
  2466. if (EX_NUM_ARGS()) {
  2467. arg_types = do_alloca(
  2468. (sizeof(ffi_type*) + sizeof(ffi_type)) * EX_NUM_ARGS(), arg_types_use_heap);
  2469. arg_values = do_alloca(
  2470. (sizeof(void*) + ZEND_FFI_SIZEOF_ARG) * EX_NUM_ARGS(), arg_values_use_heap);
  2471. n = 0;
  2472. if (type->func.args) {
  2473. ZEND_HASH_FOREACH_PTR(type->func.args, arg_type) {
  2474. arg_type = ZEND_FFI_TYPE(arg_type);
  2475. arg_values[n] = ((char*)arg_values) + (sizeof(void*) * EX_NUM_ARGS()) + (ZEND_FFI_SIZEOF_ARG * n);
  2476. if (zend_ffi_pass_arg(EX_VAR_NUM(n), arg_type, &arg_types[n], arg_values, n, execute_data) != SUCCESS) {
  2477. free_alloca(arg_types, arg_types_use_heap);
  2478. free_alloca(arg_values, arg_values_use_heap);
  2479. goto exit;
  2480. }
  2481. n++;
  2482. } ZEND_HASH_FOREACH_END();
  2483. }
  2484. }
  2485. ret_type = zend_ffi_get_type(ZEND_FFI_TYPE(type->func.ret_type));
  2486. if (!ret_type) {
  2487. zend_ffi_return_unsupported(type->func.ret_type);
  2488. free_alloca(arg_types, arg_types_use_heap);
  2489. free_alloca(arg_values, arg_values_use_heap);
  2490. goto exit;
  2491. }
  2492. if (ffi_prep_cif(&cif, type->func.abi, arg_count, ret_type, arg_types) != FFI_OK) {
  2493. zend_throw_error(zend_ffi_exception_ce, "Cannot prepare callback CIF");
  2494. free_alloca(arg_types, arg_types_use_heap);
  2495. free_alloca(arg_values, arg_values_use_heap);
  2496. goto exit;
  2497. }
  2498. }
  2499. ret = do_alloca(MAX(ret_type->size, sizeof(ffi_arg)), ret_use_heap);
  2500. ffi_call(&cif, addr, ret, arg_values);
  2501. for (n = 0; n < arg_count; n++) {
  2502. if (arg_types[n]->type == FFI_TYPE_STRUCT) {
  2503. efree(arg_types[n]);
  2504. }
  2505. }
  2506. if (ret_type->type == FFI_TYPE_STRUCT) {
  2507. efree(ret_type);
  2508. }
  2509. if (EX_NUM_ARGS()) {
  2510. free_alloca(arg_types, arg_types_use_heap);
  2511. free_alloca(arg_values, arg_values_use_heap);
  2512. }
  2513. zend_ffi_cdata_to_zval(NULL, ret, ZEND_FFI_TYPE(type->func.ret_type), BP_VAR_R, return_value, 0, 1, 0);
  2514. free_alloca(ret, ret_use_heap);
  2515. exit:
  2516. zend_string_release(EX(func)->common.function_name);
  2517. if (EX(func)->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  2518. zend_free_trampoline(EX(func));
  2519. EX(func) = NULL;
  2520. }
  2521. }
  2522. /* }}} */
  2523. static zend_function *zend_ffi_get_func(zend_object **obj, zend_string *name, const zval *key) /* {{{ */
  2524. {
  2525. zend_ffi *ffi = (zend_ffi*)*obj;
  2526. zend_ffi_symbol *sym = NULL;
  2527. zend_function *func;
  2528. zend_ffi_type *type;
  2529. if (ZSTR_LEN(name) == sizeof("new") -1
  2530. && (ZSTR_VAL(name)[0] == 'n' || ZSTR_VAL(name)[0] == 'N')
  2531. && (ZSTR_VAL(name)[1] == 'e' || ZSTR_VAL(name)[1] == 'E')
  2532. && (ZSTR_VAL(name)[2] == 'w' || ZSTR_VAL(name)[2] == 'W')) {
  2533. return (zend_function*)&zend_ffi_new_fn;
  2534. } else if (ZSTR_LEN(name) == sizeof("cast") -1
  2535. && (ZSTR_VAL(name)[0] == 'c' || ZSTR_VAL(name)[0] == 'C')
  2536. && (ZSTR_VAL(name)[1] == 'a' || ZSTR_VAL(name)[1] == 'A')
  2537. && (ZSTR_VAL(name)[2] == 's' || ZSTR_VAL(name)[2] == 'S')
  2538. && (ZSTR_VAL(name)[3] == 't' || ZSTR_VAL(name)[3] == 'T')) {
  2539. return (zend_function*)&zend_ffi_cast_fn;
  2540. } else if (ZSTR_LEN(name) == sizeof("type") -1
  2541. && (ZSTR_VAL(name)[0] == 't' || ZSTR_VAL(name)[0] == 'T')
  2542. && (ZSTR_VAL(name)[1] == 'y' || ZSTR_VAL(name)[1] == 'Y')
  2543. && (ZSTR_VAL(name)[2] == 'p' || ZSTR_VAL(name)[2] == 'P')
  2544. && (ZSTR_VAL(name)[3] == 'e' || ZSTR_VAL(name)[3] == 'E')) {
  2545. return (zend_function*)&zend_ffi_type_fn;
  2546. }
  2547. if (ffi->symbols) {
  2548. sym = zend_hash_find_ptr(ffi->symbols, name);
  2549. if (sym && sym->kind != ZEND_FFI_SYM_FUNC) {
  2550. sym = NULL;
  2551. }
  2552. }
  2553. if (!sym) {
  2554. zend_throw_error(zend_ffi_exception_ce, "Attempt to call undefined C function '%s'", ZSTR_VAL(name));
  2555. return NULL;
  2556. }
  2557. type = ZEND_FFI_TYPE(sym->type);
  2558. ZEND_ASSERT(type->kind == ZEND_FFI_TYPE_FUNC);
  2559. if (EXPECTED(EG(trampoline).common.function_name == NULL)) {
  2560. func = &EG(trampoline);
  2561. } else {
  2562. func = ecalloc(sizeof(zend_internal_function), 1);
  2563. }
  2564. func->common.type = ZEND_INTERNAL_FUNCTION;
  2565. func->common.arg_flags[0] = 0;
  2566. func->common.arg_flags[1] = 0;
  2567. func->common.arg_flags[2] = 0;
  2568. func->common.fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE;
  2569. func->common.function_name = zend_string_copy(name);
  2570. /* set to 0 to avoid arg_info[] allocation, because all values are passed by value anyway */
  2571. func->common.num_args = 0;
  2572. func->common.required_num_args = type->func.args ? zend_hash_num_elements(type->func.args) : 0;
  2573. func->common.scope = NULL;
  2574. func->common.prototype = NULL;
  2575. func->common.arg_info = NULL;
  2576. func->internal_function.handler = ZEND_FN(ffi_trampoline);
  2577. func->internal_function.module = NULL;
  2578. func->internal_function.reserved[0] = type;
  2579. func->internal_function.reserved[1] = sym->addr;
  2580. return func;
  2581. }
  2582. /* }}} */
  2583. static zend_never_inline int zend_ffi_disabled(void) /* {{{ */
  2584. {
  2585. zend_throw_error(zend_ffi_exception_ce, "FFI API is restricted by \"ffi.enable\" configuration directive");
  2586. return 0;
  2587. }
  2588. /* }}} */
  2589. static zend_always_inline int zend_ffi_validate_api_restriction(zend_execute_data *execute_data) /* {{{ */
  2590. {
  2591. if (EXPECTED(FFI_G(restriction) > ZEND_FFI_ENABLED)) {
  2592. ZEND_ASSERT(FFI_G(restriction) == ZEND_FFI_PRELOAD);
  2593. if (FFI_G(is_cli)
  2594. || (execute_data->prev_execute_data
  2595. && (execute_data->prev_execute_data->func->common.fn_flags & ZEND_ACC_PRELOADED))
  2596. || (CG(compiler_options) & ZEND_COMPILE_PRELOAD)) {
  2597. return 1;
  2598. }
  2599. } else if (EXPECTED(FFI_G(restriction) == ZEND_FFI_ENABLED)) {
  2600. return 1;
  2601. }
  2602. return zend_ffi_disabled();
  2603. }
  2604. /* }}} */
  2605. #define ZEND_FFI_VALIDATE_API_RESTRICTION() do { \
  2606. if (UNEXPECTED(!zend_ffi_validate_api_restriction(execute_data))) { \
  2607. RETURN_THROWS(); \
  2608. } \
  2609. } while (0)
  2610. ZEND_METHOD(FFI, cdef) /* {{{ */
  2611. {
  2612. zend_string *code = NULL;
  2613. zend_string *lib = NULL;
  2614. zend_ffi *ffi = NULL;
  2615. DL_HANDLE handle = NULL;
  2616. void *addr;
  2617. ZEND_FFI_VALIDATE_API_RESTRICTION();
  2618. ZEND_PARSE_PARAMETERS_START(0, 2)
  2619. Z_PARAM_OPTIONAL
  2620. Z_PARAM_STR(code)
  2621. Z_PARAM_STR_OR_NULL(lib)
  2622. ZEND_PARSE_PARAMETERS_END();
  2623. if (lib) {
  2624. handle = DL_LOAD(ZSTR_VAL(lib));
  2625. if (!handle) {
  2626. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s'", ZSTR_VAL(lib));
  2627. RETURN_THROWS();
  2628. }
  2629. #ifdef RTLD_DEFAULT
  2630. } else if (1) {
  2631. // TODO: this might need to be disabled or protected ???
  2632. handle = RTLD_DEFAULT;
  2633. #endif
  2634. }
  2635. FFI_G(symbols) = NULL;
  2636. FFI_G(tags) = NULL;
  2637. if (code && ZSTR_LEN(code)) {
  2638. /* Parse C definitions */
  2639. FFI_G(default_type_attr) = ZEND_FFI_ATTR_STORED;
  2640. if (zend_ffi_parse_decl(ZSTR_VAL(code), ZSTR_LEN(code)) != SUCCESS) {
  2641. if (FFI_G(symbols)) {
  2642. zend_hash_destroy(FFI_G(symbols));
  2643. efree(FFI_G(symbols));
  2644. FFI_G(symbols) = NULL;
  2645. }
  2646. if (FFI_G(tags)) {
  2647. zend_hash_destroy(FFI_G(tags));
  2648. efree(FFI_G(tags));
  2649. FFI_G(tags) = NULL;
  2650. }
  2651. RETURN_THROWS();
  2652. }
  2653. if (FFI_G(symbols)) {
  2654. zend_string *name;
  2655. zend_ffi_symbol *sym;
  2656. ZEND_HASH_FOREACH_STR_KEY_PTR(FFI_G(symbols), name, sym) {
  2657. if (sym->kind == ZEND_FFI_SYM_VAR) {
  2658. addr = DL_FETCH_SYMBOL(handle, ZSTR_VAL(name));
  2659. if (!addr) {
  2660. zend_throw_error(zend_ffi_exception_ce, "Failed resolving C variable '%s'", ZSTR_VAL(name));
  2661. RETURN_THROWS();
  2662. }
  2663. sym->addr = addr;
  2664. } else if (sym->kind == ZEND_FFI_SYM_FUNC) {
  2665. zend_string *mangled_name = zend_ffi_mangled_func_name(name, ZEND_FFI_TYPE(sym->type));
  2666. addr = DL_FETCH_SYMBOL(handle, ZSTR_VAL(mangled_name));
  2667. zend_string_release(mangled_name);
  2668. if (!addr) {
  2669. zend_throw_error(zend_ffi_exception_ce, "Failed resolving C function '%s'", ZSTR_VAL(name));
  2670. RETURN_THROWS();
  2671. }
  2672. sym->addr = addr;
  2673. }
  2674. } ZEND_HASH_FOREACH_END();
  2675. }
  2676. }
  2677. ffi = (zend_ffi*)zend_ffi_new(zend_ffi_ce);
  2678. ffi->lib = handle;
  2679. ffi->symbols = FFI_G(symbols);
  2680. ffi->tags = FFI_G(tags);
  2681. FFI_G(symbols) = NULL;
  2682. FFI_G(tags) = NULL;
  2683. RETURN_OBJ(&ffi->std);
  2684. }
  2685. /* }}} */
  2686. static int zend_ffi_same_types(zend_ffi_type *old, zend_ffi_type *type) /* {{{ */
  2687. {
  2688. if (old == type) {
  2689. return 1;
  2690. }
  2691. if (old->kind != type->kind
  2692. || old->size != type->size
  2693. || old->align != type->align
  2694. || old->attr != type->attr) {
  2695. return 0;
  2696. }
  2697. switch (old->kind) {
  2698. case ZEND_FFI_TYPE_ENUM:
  2699. return old->enumeration.kind == type->enumeration.kind;
  2700. case ZEND_FFI_TYPE_ARRAY:
  2701. return old->array.length == type->array.length
  2702. && zend_ffi_same_types(ZEND_FFI_TYPE(old->array.type), ZEND_FFI_TYPE(type->array.type));
  2703. case ZEND_FFI_TYPE_POINTER:
  2704. return zend_ffi_same_types(ZEND_FFI_TYPE(old->pointer.type), ZEND_FFI_TYPE(type->pointer.type));
  2705. case ZEND_FFI_TYPE_STRUCT:
  2706. if (zend_hash_num_elements(&old->record.fields) != zend_hash_num_elements(&type->record.fields)) {
  2707. return 0;
  2708. } else {
  2709. zend_ffi_field *old_field, *field;
  2710. zend_string *key;
  2711. Bucket *b = type->record.fields.arData;
  2712. ZEND_HASH_FOREACH_STR_KEY_PTR(&old->record.fields, key, old_field) {
  2713. while (Z_TYPE(b->val) == IS_UNDEF) {
  2714. b++;
  2715. }
  2716. if (key) {
  2717. if (!b->key
  2718. || !zend_string_equals(key, b->key)) {
  2719. return 0;
  2720. }
  2721. } else if (b->key) {
  2722. return 0;
  2723. }
  2724. field = Z_PTR(b->val);
  2725. if (old_field->offset != field->offset
  2726. || old_field->is_const != field->is_const
  2727. || old_field->is_nested != field->is_nested
  2728. || old_field->first_bit != field->first_bit
  2729. || old_field->bits != field->bits
  2730. || !zend_ffi_same_types(ZEND_FFI_TYPE(old_field->type), ZEND_FFI_TYPE(field->type))) {
  2731. return 0;
  2732. }
  2733. b++;
  2734. } ZEND_HASH_FOREACH_END();
  2735. }
  2736. break;
  2737. case ZEND_FFI_TYPE_FUNC:
  2738. if (old->func.abi != type->func.abi
  2739. || ((old->func.args ? zend_hash_num_elements(old->func.args) : 0) != (type->func.args ? zend_hash_num_elements(type->func.args) : 0))
  2740. || !zend_ffi_same_types(ZEND_FFI_TYPE(old->func.ret_type), ZEND_FFI_TYPE(type->func.ret_type))) {
  2741. return 0;
  2742. } else if (old->func.args) {
  2743. zend_ffi_type *arg_type;
  2744. Bucket *b = type->func.args->arData;
  2745. ZEND_HASH_FOREACH_PTR(old->func.args, arg_type) {
  2746. while (Z_TYPE(b->val) == IS_UNDEF) {
  2747. b++;
  2748. }
  2749. if (!zend_ffi_same_types(ZEND_FFI_TYPE(arg_type), ZEND_FFI_TYPE(Z_PTR(b->val)))) {
  2750. return 0;
  2751. }
  2752. b++;
  2753. } ZEND_HASH_FOREACH_END();
  2754. }
  2755. break;
  2756. default:
  2757. break;
  2758. }
  2759. return 1;
  2760. }
  2761. /* }}} */
  2762. static int zend_ffi_same_symbols(zend_ffi_symbol *old, zend_ffi_symbol *sym) /* {{{ */
  2763. {
  2764. if (old->kind != sym->kind || old->is_const != sym->is_const) {
  2765. return 0;
  2766. }
  2767. if (old->kind == ZEND_FFI_SYM_CONST) {
  2768. if (old->value != sym->value) {
  2769. return 0;
  2770. }
  2771. }
  2772. return zend_ffi_same_types(ZEND_FFI_TYPE(old->type), ZEND_FFI_TYPE(sym->type));
  2773. }
  2774. /* }}} */
  2775. static int zend_ffi_same_tags(zend_ffi_tag *old, zend_ffi_tag *tag) /* {{{ */
  2776. {
  2777. if (old->kind != tag->kind) {
  2778. return 0;
  2779. }
  2780. return zend_ffi_same_types(ZEND_FFI_TYPE(old->type), ZEND_FFI_TYPE(tag->type));
  2781. }
  2782. /* }}} */
  2783. static int zend_ffi_subst_old_type(zend_ffi_type **dcl, zend_ffi_type *old, zend_ffi_type *type) /* {{{ */
  2784. {
  2785. zend_ffi_type *dcl_type;
  2786. zend_ffi_field *field;
  2787. if (ZEND_FFI_TYPE(*dcl) == type) {
  2788. *dcl = old;
  2789. return 1;
  2790. }
  2791. dcl_type = *dcl;
  2792. switch (dcl_type->kind) {
  2793. case ZEND_FFI_TYPE_POINTER:
  2794. return zend_ffi_subst_old_type(&dcl_type->pointer.type, old, type);
  2795. case ZEND_FFI_TYPE_ARRAY:
  2796. return zend_ffi_subst_old_type(&dcl_type->array.type, old, type);
  2797. case ZEND_FFI_TYPE_FUNC:
  2798. if (zend_ffi_subst_old_type(&dcl_type->func.ret_type, old, type)) {
  2799. return 1;
  2800. }
  2801. if (dcl_type->func.args) {
  2802. zval *zv;
  2803. ZEND_HASH_FOREACH_VAL(dcl_type->func.args, zv) {
  2804. if (zend_ffi_subst_old_type((zend_ffi_type**)&Z_PTR_P(zv), old, type)) {
  2805. return 1;
  2806. }
  2807. } ZEND_HASH_FOREACH_END();
  2808. }
  2809. break;
  2810. case ZEND_FFI_TYPE_STRUCT:
  2811. ZEND_HASH_FOREACH_PTR(&dcl_type->record.fields, field) {
  2812. if (zend_ffi_subst_old_type(&field->type, old, type)) {
  2813. return 1;
  2814. }
  2815. } ZEND_HASH_FOREACH_END();
  2816. break;
  2817. default:
  2818. break;
  2819. }
  2820. return 0;
  2821. } /* }}} */
  2822. static void zend_ffi_cleanup_type(zend_ffi_type *old, zend_ffi_type *type) /* {{{ */
  2823. {
  2824. zend_ffi_symbol *sym;
  2825. zend_ffi_tag *tag;
  2826. if (FFI_G(symbols)) {
  2827. ZEND_HASH_FOREACH_PTR(FFI_G(symbols), sym) {
  2828. zend_ffi_subst_old_type(&sym->type, old, type);
  2829. } ZEND_HASH_FOREACH_END();
  2830. }
  2831. if (FFI_G(tags)) {
  2832. ZEND_HASH_FOREACH_PTR(FFI_G(tags), tag) {
  2833. zend_ffi_subst_old_type(&tag->type, old, type);
  2834. } ZEND_HASH_FOREACH_END();
  2835. }
  2836. }
  2837. /* }}} */
  2838. static zend_ffi_type *zend_ffi_remember_type(zend_ffi_type *type) /* {{{ */
  2839. {
  2840. if (!FFI_G(weak_types)) {
  2841. FFI_G(weak_types) = emalloc(sizeof(HashTable));
  2842. zend_hash_init(FFI_G(weak_types), 0, NULL, zend_ffi_type_hash_dtor, 0);
  2843. }
  2844. // TODO: avoid dups ???
  2845. type->attr |= ZEND_FFI_ATTR_STORED;
  2846. zend_hash_next_index_insert_ptr(FFI_G(weak_types), ZEND_FFI_TYPE_MAKE_OWNED(type));
  2847. return type;
  2848. }
  2849. /* }}} */
  2850. static zend_ffi *zend_ffi_load(const char *filename, bool preload) /* {{{ */
  2851. {
  2852. struct stat buf;
  2853. int fd;
  2854. char *code, *code_pos, *scope_name, *lib;
  2855. size_t code_size, scope_name_len;
  2856. zend_ffi *ffi;
  2857. DL_HANDLE handle = NULL;
  2858. zend_ffi_scope *scope = NULL;
  2859. zend_string *name;
  2860. zend_ffi_symbol *sym;
  2861. zend_ffi_tag *tag;
  2862. void *addr;
  2863. if (stat(filename, &buf) != 0) {
  2864. if (preload) {
  2865. zend_error(E_WARNING, "FFI: failed pre-loading '%s', file doesn't exist", filename);
  2866. } else {
  2867. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', file doesn't exist", filename);
  2868. }
  2869. return NULL;
  2870. }
  2871. if ((buf.st_mode & S_IFMT) != S_IFREG) {
  2872. if (preload) {
  2873. zend_error(E_WARNING, "FFI: failed pre-loading '%s', not a regular file", filename);
  2874. } else {
  2875. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', not a regular file", filename);
  2876. }
  2877. return NULL;
  2878. }
  2879. code_size = buf.st_size;
  2880. code = emalloc(code_size + 1);
  2881. fd = open(filename, O_RDONLY, 0);
  2882. if (fd < 0 || read(fd, code, code_size) != code_size) {
  2883. if (preload) {
  2884. zend_error(E_WARNING, "FFI: Failed pre-loading '%s', cannot read_file", filename);
  2885. } else {
  2886. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', cannot read_file", filename);
  2887. }
  2888. efree(code);
  2889. close(fd);
  2890. return NULL;
  2891. }
  2892. close(fd);
  2893. code[code_size] = 0;
  2894. FFI_G(symbols) = NULL;
  2895. FFI_G(tags) = NULL;
  2896. FFI_G(persistent) = preload;
  2897. FFI_G(default_type_attr) = preload ?
  2898. ZEND_FFI_ATTR_STORED | ZEND_FFI_ATTR_PERSISTENT :
  2899. ZEND_FFI_ATTR_STORED;
  2900. scope_name = NULL;
  2901. scope_name_len = 0;
  2902. lib = NULL;
  2903. code_pos = zend_ffi_parse_directives(filename, code, &scope_name, &lib, preload);
  2904. if (!code_pos) {
  2905. efree(code);
  2906. FFI_G(persistent) = 0;
  2907. return NULL;
  2908. }
  2909. code_size -= code_pos - code;
  2910. if (zend_ffi_parse_decl(code_pos, code_size) != SUCCESS) {
  2911. if (preload) {
  2912. zend_error(E_WARNING, "FFI: failed pre-loading '%s'", filename);
  2913. } else {
  2914. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s'", filename);
  2915. }
  2916. goto cleanup;
  2917. }
  2918. if (lib) {
  2919. handle = DL_LOAD(lib);
  2920. if (!handle) {
  2921. if (preload) {
  2922. zend_error(E_WARNING, "FFI: Failed pre-loading '%s'", lib);
  2923. } else {
  2924. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s'", lib);
  2925. }
  2926. goto cleanup;
  2927. }
  2928. #ifdef RTLD_DEFAULT
  2929. } else if (1) {
  2930. // TODO: this might need to be disabled or protected ???
  2931. handle = RTLD_DEFAULT;
  2932. #endif
  2933. }
  2934. if (preload) {
  2935. if (!scope_name) {
  2936. scope_name = "C";
  2937. }
  2938. scope_name_len = strlen(scope_name);
  2939. if (FFI_G(scopes)) {
  2940. scope = zend_hash_str_find_ptr(FFI_G(scopes), scope_name, scope_name_len);
  2941. }
  2942. }
  2943. if (FFI_G(symbols)) {
  2944. ZEND_HASH_FOREACH_STR_KEY_PTR(FFI_G(symbols), name, sym) {
  2945. if (sym->kind == ZEND_FFI_SYM_VAR) {
  2946. addr = DL_FETCH_SYMBOL(handle, ZSTR_VAL(name));
  2947. if (!addr) {
  2948. if (preload) {
  2949. zend_error(E_WARNING, "FFI: failed pre-loading '%s', cannot resolve C variable '%s'", filename, ZSTR_VAL(name));
  2950. } else {
  2951. zend_throw_error(zend_ffi_exception_ce, "Failed resolving C variable '%s'", ZSTR_VAL(name));
  2952. }
  2953. if (lib) {
  2954. DL_UNLOAD(handle);
  2955. }
  2956. goto cleanup;
  2957. }
  2958. sym->addr = addr;
  2959. } else if (sym->kind == ZEND_FFI_SYM_FUNC) {
  2960. zend_string *mangled_name = zend_ffi_mangled_func_name(name, ZEND_FFI_TYPE(sym->type));
  2961. addr = DL_FETCH_SYMBOL(handle, ZSTR_VAL(mangled_name));
  2962. zend_string_release(mangled_name);
  2963. if (!addr) {
  2964. if (preload) {
  2965. zend_error(E_WARNING, "failed pre-loading '%s', cannot resolve C function '%s'", filename, ZSTR_VAL(name));
  2966. } else {
  2967. zend_throw_error(zend_ffi_exception_ce, "Failed resolving C function '%s'", ZSTR_VAL(name));
  2968. }
  2969. if (lib) {
  2970. DL_UNLOAD(handle);
  2971. }
  2972. goto cleanup;
  2973. }
  2974. sym->addr = addr;
  2975. }
  2976. if (scope && scope->symbols) {
  2977. zend_ffi_symbol *old_sym = zend_hash_find_ptr(scope->symbols, name);
  2978. if (old_sym) {
  2979. if (zend_ffi_same_symbols(old_sym, sym)) {
  2980. if (ZEND_FFI_TYPE_IS_OWNED(sym->type)
  2981. && ZEND_FFI_TYPE(old_sym->type) != ZEND_FFI_TYPE(sym->type)) {
  2982. zend_ffi_type *type = ZEND_FFI_TYPE(sym->type);
  2983. zend_ffi_cleanup_type(ZEND_FFI_TYPE(old_sym->type), ZEND_FFI_TYPE(type));
  2984. zend_ffi_type_dtor(type);
  2985. }
  2986. } else {
  2987. zend_error(E_WARNING, "FFI: failed pre-loading '%s', redefinition of '%s'", filename, ZSTR_VAL(name));
  2988. if (lib) {
  2989. DL_UNLOAD(handle);
  2990. }
  2991. goto cleanup;
  2992. }
  2993. }
  2994. }
  2995. } ZEND_HASH_FOREACH_END();
  2996. }
  2997. if (preload) {
  2998. if (scope && scope->tags && FFI_G(tags)) {
  2999. ZEND_HASH_FOREACH_STR_KEY_PTR(FFI_G(tags), name, tag) {
  3000. zend_ffi_tag *old_tag = zend_hash_find_ptr(scope->tags, name);
  3001. if (old_tag) {
  3002. if (zend_ffi_same_tags(old_tag, tag)) {
  3003. if (ZEND_FFI_TYPE_IS_OWNED(tag->type)
  3004. && ZEND_FFI_TYPE(old_tag->type) != ZEND_FFI_TYPE(tag->type)) {
  3005. zend_ffi_type *type = ZEND_FFI_TYPE(tag->type);
  3006. zend_ffi_cleanup_type(ZEND_FFI_TYPE(old_tag->type), ZEND_FFI_TYPE(type));
  3007. zend_ffi_type_dtor(type);
  3008. }
  3009. } else {
  3010. zend_error(E_WARNING, "FFI: failed pre-loading '%s', redefinition of '%s %s'", filename, zend_ffi_tag_kind_name[tag->kind], ZSTR_VAL(name));
  3011. if (lib) {
  3012. DL_UNLOAD(handle);
  3013. }
  3014. goto cleanup;
  3015. }
  3016. }
  3017. } ZEND_HASH_FOREACH_END();
  3018. }
  3019. if (!scope) {
  3020. scope = malloc(sizeof(zend_ffi_scope));
  3021. scope->symbols = FFI_G(symbols);
  3022. scope->tags = FFI_G(tags);
  3023. if (!FFI_G(scopes)) {
  3024. FFI_G(scopes) = malloc(sizeof(HashTable));
  3025. zend_hash_init(FFI_G(scopes), 0, NULL, zend_ffi_scope_hash_dtor, 1);
  3026. }
  3027. zend_hash_str_add_ptr(FFI_G(scopes), scope_name, scope_name_len, scope);
  3028. } else {
  3029. if (FFI_G(symbols)) {
  3030. if (!scope->symbols) {
  3031. scope->symbols = FFI_G(symbols);
  3032. FFI_G(symbols) = NULL;
  3033. } else {
  3034. ZEND_HASH_FOREACH_STR_KEY_PTR(FFI_G(symbols), name, sym) {
  3035. if (!zend_hash_add_ptr(scope->symbols, name, sym)) {
  3036. zend_ffi_type_dtor(sym->type);
  3037. free(sym);
  3038. }
  3039. } ZEND_HASH_FOREACH_END();
  3040. FFI_G(symbols)->pDestructor = NULL;
  3041. zend_hash_destroy(FFI_G(symbols));
  3042. }
  3043. }
  3044. if (FFI_G(tags)) {
  3045. if (!scope->tags) {
  3046. scope->tags = FFI_G(tags);
  3047. FFI_G(tags) = NULL;
  3048. } else {
  3049. ZEND_HASH_FOREACH_STR_KEY_PTR(FFI_G(tags), name, tag) {
  3050. if (!zend_hash_add_ptr(scope->tags, name, tag)) {
  3051. zend_ffi_type_dtor(tag->type);
  3052. free(tag);
  3053. }
  3054. } ZEND_HASH_FOREACH_END();
  3055. FFI_G(tags)->pDestructor = NULL;
  3056. zend_hash_destroy(FFI_G(tags));
  3057. }
  3058. }
  3059. }
  3060. if (EG(objects_store).object_buckets) {
  3061. ffi = (zend_ffi*)zend_ffi_new(zend_ffi_ce);
  3062. } else {
  3063. ffi = ecalloc(1, sizeof(zend_ffi));
  3064. }
  3065. ffi->symbols = scope->symbols;
  3066. ffi->tags = scope->tags;
  3067. ffi->persistent = 1;
  3068. } else {
  3069. ffi = (zend_ffi*)zend_ffi_new(zend_ffi_ce);
  3070. ffi->lib = handle;
  3071. ffi->symbols = FFI_G(symbols);
  3072. ffi->tags = FFI_G(tags);
  3073. }
  3074. efree(code);
  3075. FFI_G(symbols) = NULL;
  3076. FFI_G(tags) = NULL;
  3077. FFI_G(persistent) = 0;
  3078. return ffi;
  3079. cleanup:
  3080. efree(code);
  3081. if (FFI_G(symbols)) {
  3082. zend_hash_destroy(FFI_G(symbols));
  3083. pefree(FFI_G(symbols), preload);
  3084. FFI_G(symbols) = NULL;
  3085. }
  3086. if (FFI_G(tags)) {
  3087. zend_hash_destroy(FFI_G(tags));
  3088. pefree(FFI_G(tags), preload);
  3089. FFI_G(tags) = NULL;
  3090. }
  3091. FFI_G(persistent) = 0;
  3092. return NULL;
  3093. }
  3094. /* }}} */
  3095. ZEND_METHOD(FFI, load) /* {{{ */
  3096. {
  3097. zend_string *fn;
  3098. zend_ffi *ffi;
  3099. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3100. ZEND_PARSE_PARAMETERS_START(1, 1)
  3101. Z_PARAM_STR(fn)
  3102. ZEND_PARSE_PARAMETERS_END();
  3103. if (CG(compiler_options) & ZEND_COMPILE_PRELOAD_IN_CHILD) {
  3104. zend_throw_error(zend_ffi_exception_ce, "FFI::load() doesn't work in conjunction with \"opcache.preload_user\". Use \"ffi.preload\" instead.");
  3105. RETURN_THROWS();
  3106. }
  3107. ffi = zend_ffi_load(ZSTR_VAL(fn), (CG(compiler_options) & ZEND_COMPILE_PRELOAD) != 0);
  3108. if (ffi) {
  3109. RETURN_OBJ(&ffi->std);
  3110. }
  3111. }
  3112. /* }}} */
  3113. ZEND_METHOD(FFI, scope) /* {{{ */
  3114. {
  3115. zend_string *scope_name;
  3116. zend_ffi_scope *scope = NULL;
  3117. zend_ffi *ffi;
  3118. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3119. ZEND_PARSE_PARAMETERS_START(1, 1)
  3120. Z_PARAM_STR(scope_name)
  3121. ZEND_PARSE_PARAMETERS_END();
  3122. if (FFI_G(scopes)) {
  3123. scope = zend_hash_find_ptr(FFI_G(scopes), scope_name);
  3124. }
  3125. if (!scope) {
  3126. zend_throw_error(zend_ffi_exception_ce, "Failed loading scope '%s'", ZSTR_VAL(scope_name));
  3127. RETURN_THROWS();
  3128. }
  3129. ffi = (zend_ffi*)zend_ffi_new(zend_ffi_ce);
  3130. ffi->symbols = scope->symbols;
  3131. ffi->tags = scope->tags;
  3132. ffi->persistent = 1;
  3133. RETURN_OBJ(&ffi->std);
  3134. }
  3135. /* }}} */
  3136. static void zend_ffi_cleanup_dcl(zend_ffi_dcl *dcl) /* {{{ */
  3137. {
  3138. if (dcl) {
  3139. zend_ffi_type_dtor(dcl->type);
  3140. dcl->type = NULL;
  3141. }
  3142. }
  3143. /* }}} */
  3144. static void zend_ffi_throw_parser_error(const char *format, ...) /* {{{ */
  3145. {
  3146. va_list va;
  3147. char *message = NULL;
  3148. va_start(va, format);
  3149. zend_vspprintf(&message, 0, format, va);
  3150. if (EG(current_execute_data)) {
  3151. zend_throw_exception(zend_ffi_parser_exception_ce, message, 0);
  3152. } else {
  3153. zend_error(E_WARNING, "FFI Parser: %s", message);
  3154. }
  3155. efree(message);
  3156. va_end(va);
  3157. }
  3158. /* }}} */
  3159. static int zend_ffi_validate_vla(zend_ffi_type *type) /* {{{ */
  3160. {
  3161. if (!FFI_G(allow_vla) && (type->attr & ZEND_FFI_ATTR_VLA)) {
  3162. zend_ffi_throw_parser_error("\"[*]\" is not allowed in other than function prototype scope at line %d", FFI_G(line));
  3163. return FAILURE;
  3164. }
  3165. return SUCCESS;
  3166. }
  3167. /* }}} */
  3168. static int zend_ffi_validate_incomplete_type(zend_ffi_type *type, bool allow_incomplete_tag, bool allow_incomplete_array) /* {{{ */
  3169. {
  3170. if (!allow_incomplete_tag && (type->attr & ZEND_FFI_ATTR_INCOMPLETE_TAG)) {
  3171. if (FFI_G(tags)) {
  3172. zend_string *key;
  3173. zend_ffi_tag *tag;
  3174. ZEND_HASH_FOREACH_STR_KEY_PTR(FFI_G(tags), key, tag) {
  3175. if (ZEND_FFI_TYPE(tag->type) == type) {
  3176. if (type->kind == ZEND_FFI_TYPE_ENUM) {
  3177. zend_ffi_throw_parser_error("Incomplete enum \"%s\" at line %d", ZSTR_VAL(key), FFI_G(line));
  3178. } else if (type->attr & ZEND_FFI_ATTR_UNION) {
  3179. zend_ffi_throw_parser_error("Incomplete union \"%s\" at line %d", ZSTR_VAL(key), FFI_G(line));
  3180. } else {
  3181. zend_ffi_throw_parser_error("Incomplete struct \"%s\" at line %d", ZSTR_VAL(key), FFI_G(line));
  3182. }
  3183. return FAILURE;
  3184. }
  3185. } ZEND_HASH_FOREACH_END();
  3186. }
  3187. if (FFI_G(symbols)) {
  3188. zend_string *key;
  3189. zend_ffi_symbol *sym;
  3190. ZEND_HASH_FOREACH_STR_KEY_PTR(FFI_G(symbols), key, sym) {
  3191. if (type == ZEND_FFI_TYPE(sym->type)) {
  3192. zend_ffi_throw_parser_error("Incomplete C type %s at line %d", ZSTR_VAL(key), FFI_G(line));
  3193. return FAILURE;
  3194. }
  3195. } ZEND_HASH_FOREACH_END();
  3196. }
  3197. zend_ffi_throw_parser_error("Incomplete type at line %d", FFI_G(line));
  3198. return FAILURE;
  3199. } else if (!allow_incomplete_array && (type->attr & ZEND_FFI_ATTR_INCOMPLETE_ARRAY)) {
  3200. zend_ffi_throw_parser_error("\"[]\" is not allowed at line %d", FFI_G(line));
  3201. return FAILURE;
  3202. } else if (!FFI_G(allow_vla) && (type->attr & ZEND_FFI_ATTR_VLA)) {
  3203. zend_ffi_throw_parser_error("\"[*]\" is not allowed in other than function prototype scope at line %d", FFI_G(line));
  3204. return FAILURE;
  3205. }
  3206. return SUCCESS;
  3207. }
  3208. /* }}} */
  3209. static int zend_ffi_validate_type(zend_ffi_type *type, bool allow_incomplete_tag, bool allow_incomplete_array) /* {{{ */
  3210. {
  3211. if (type->kind == ZEND_FFI_TYPE_VOID) {
  3212. zend_ffi_throw_parser_error("void type is not allowed at line %d", FFI_G(line));
  3213. return FAILURE;
  3214. }
  3215. return zend_ffi_validate_incomplete_type(type, allow_incomplete_tag, allow_incomplete_array);
  3216. }
  3217. /* }}} */
  3218. static int zend_ffi_validate_var_type(zend_ffi_type *type, bool allow_incomplete_array) /* {{{ */
  3219. {
  3220. if (type->kind == ZEND_FFI_TYPE_FUNC) {
  3221. zend_ffi_throw_parser_error("function type is not allowed at line %d", FFI_G(line));
  3222. return FAILURE;
  3223. }
  3224. return zend_ffi_validate_type(type, 0, allow_incomplete_array);
  3225. }
  3226. /* }}} */
  3227. void zend_ffi_validate_type_name(zend_ffi_dcl *dcl) /* {{{ */
  3228. {
  3229. zend_ffi_finalize_type(dcl);
  3230. if (zend_ffi_validate_var_type(ZEND_FFI_TYPE(dcl->type), 0) != SUCCESS) {
  3231. zend_ffi_cleanup_dcl(dcl);
  3232. LONGJMP(FFI_G(bailout), FAILURE);
  3233. }
  3234. }
  3235. /* }}} */
  3236. static int zend_ffi_subst_type(zend_ffi_type **dcl, zend_ffi_type *type) /* {{{ */
  3237. {
  3238. zend_ffi_type *dcl_type;
  3239. zend_ffi_field *field;
  3240. if (*dcl == type) {
  3241. *dcl = ZEND_FFI_TYPE_MAKE_OWNED(type);
  3242. return 1;
  3243. }
  3244. dcl_type = *dcl;
  3245. switch (dcl_type->kind) {
  3246. case ZEND_FFI_TYPE_POINTER:
  3247. return zend_ffi_subst_type(&dcl_type->pointer.type, type);
  3248. case ZEND_FFI_TYPE_ARRAY:
  3249. return zend_ffi_subst_type(&dcl_type->array.type, type);
  3250. case ZEND_FFI_TYPE_FUNC:
  3251. if (zend_ffi_subst_type(&dcl_type->func.ret_type, type)) {
  3252. return 1;
  3253. }
  3254. if (dcl_type->func.args) {
  3255. zval *zv;
  3256. ZEND_HASH_FOREACH_VAL(dcl_type->func.args, zv) {
  3257. if (zend_ffi_subst_type((zend_ffi_type**)&Z_PTR_P(zv), type)) {
  3258. return 1;
  3259. }
  3260. } ZEND_HASH_FOREACH_END();
  3261. }
  3262. break;
  3263. case ZEND_FFI_TYPE_STRUCT:
  3264. ZEND_HASH_FOREACH_PTR(&dcl_type->record.fields, field) {
  3265. if (zend_ffi_subst_type(&field->type, type)) {
  3266. return 1;
  3267. }
  3268. } ZEND_HASH_FOREACH_END();
  3269. break;
  3270. default:
  3271. break;
  3272. }
  3273. return 0;
  3274. } /* }}} */
  3275. static void zend_ffi_tags_cleanup(zend_ffi_dcl *dcl) /* {{{ */
  3276. {
  3277. zend_ffi_tag *tag;
  3278. ZEND_HASH_FOREACH_PTR(FFI_G(tags), tag) {
  3279. if (ZEND_FFI_TYPE_IS_OWNED(tag->type)) {
  3280. zend_ffi_type *type = ZEND_FFI_TYPE(tag->type);
  3281. zend_ffi_subst_type(&dcl->type, type);
  3282. tag->type = type;
  3283. }
  3284. } ZEND_HASH_FOREACH_END();
  3285. zend_hash_destroy(FFI_G(tags));
  3286. efree(FFI_G(tags));
  3287. }
  3288. /* }}} */
  3289. ZEND_METHOD(FFI, new) /* {{{ */
  3290. {
  3291. zend_string *type_def = NULL;
  3292. zend_object *type_obj = NULL;
  3293. zend_ffi_type *type, *type_ptr;
  3294. zend_ffi_cdata *cdata;
  3295. void *ptr;
  3296. bool owned = 1;
  3297. bool persistent = 0;
  3298. bool is_const = 0;
  3299. zend_ffi_flags flags = ZEND_FFI_FLAG_OWNED;
  3300. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3301. ZEND_PARSE_PARAMETERS_START(1, 3)
  3302. Z_PARAM_OBJ_OF_CLASS_OR_STR(type_obj, zend_ffi_ctype_ce, type_def)
  3303. Z_PARAM_OPTIONAL
  3304. Z_PARAM_BOOL(owned)
  3305. Z_PARAM_BOOL(persistent)
  3306. ZEND_PARSE_PARAMETERS_END();
  3307. if (!owned) {
  3308. flags &= ~ZEND_FFI_FLAG_OWNED;
  3309. }
  3310. if (persistent) {
  3311. flags |= ZEND_FFI_FLAG_PERSISTENT;
  3312. }
  3313. if (type_def) {
  3314. zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT;
  3315. if (Z_TYPE(EX(This)) == IS_OBJECT) {
  3316. zend_ffi *ffi = (zend_ffi*)Z_OBJ(EX(This));
  3317. FFI_G(symbols) = ffi->symbols;
  3318. FFI_G(tags) = ffi->tags;
  3319. } else {
  3320. FFI_G(symbols) = NULL;
  3321. FFI_G(tags) = NULL;
  3322. }
  3323. FFI_G(default_type_attr) = 0;
  3324. if (zend_ffi_parse_type(ZSTR_VAL(type_def), ZSTR_LEN(type_def), &dcl) != SUCCESS) {
  3325. zend_ffi_type_dtor(dcl.type);
  3326. if (Z_TYPE(EX(This)) != IS_OBJECT) {
  3327. if (FFI_G(tags)) {
  3328. zend_hash_destroy(FFI_G(tags));
  3329. efree(FFI_G(tags));
  3330. FFI_G(tags) = NULL;
  3331. }
  3332. if (FFI_G(symbols)) {
  3333. zend_hash_destroy(FFI_G(symbols));
  3334. efree(FFI_G(symbols));
  3335. FFI_G(symbols) = NULL;
  3336. }
  3337. }
  3338. return;
  3339. }
  3340. type = ZEND_FFI_TYPE(dcl.type);
  3341. if (dcl.attr & ZEND_FFI_ATTR_CONST) {
  3342. is_const = 1;
  3343. }
  3344. if (Z_TYPE(EX(This)) != IS_OBJECT) {
  3345. if (FFI_G(tags)) {
  3346. zend_ffi_tags_cleanup(&dcl);
  3347. }
  3348. if (FFI_G(symbols)) {
  3349. zend_hash_destroy(FFI_G(symbols));
  3350. efree(FFI_G(symbols));
  3351. FFI_G(symbols) = NULL;
  3352. }
  3353. }
  3354. FFI_G(symbols) = NULL;
  3355. FFI_G(tags) = NULL;
  3356. type_ptr = dcl.type;
  3357. } else {
  3358. zend_ffi_ctype *ctype = (zend_ffi_ctype*) type_obj;
  3359. type_ptr = type = ctype->type;
  3360. if (ZEND_FFI_TYPE_IS_OWNED(type)) {
  3361. type = ZEND_FFI_TYPE(type);
  3362. if (!(type->attr & ZEND_FFI_ATTR_STORED)) {
  3363. if (GC_REFCOUNT(&ctype->std) == 1) {
  3364. /* transfer type ownership */
  3365. ctype->type = type;
  3366. } else {
  3367. ctype->type = type_ptr = type = zend_ffi_remember_type(type);
  3368. }
  3369. }
  3370. }
  3371. }
  3372. if (type->size == 0) {
  3373. zend_throw_error(zend_ffi_exception_ce, "Cannot instantiate FFI\\CData of zero size");
  3374. zend_ffi_type_dtor(type_ptr);
  3375. return;
  3376. }
  3377. ptr = pemalloc(type->size, flags & ZEND_FFI_FLAG_PERSISTENT);
  3378. memset(ptr, 0, type->size);
  3379. cdata = (zend_ffi_cdata*)zend_ffi_cdata_new(zend_ffi_cdata_ce);
  3380. if (type->kind < ZEND_FFI_TYPE_POINTER) {
  3381. cdata->std.handlers = &zend_ffi_cdata_value_handlers;
  3382. }
  3383. cdata->type = type_ptr;
  3384. cdata->ptr = ptr;
  3385. cdata->flags = flags;
  3386. if (is_const) {
  3387. cdata->flags |= ZEND_FFI_FLAG_CONST;
  3388. }
  3389. RETURN_OBJ(&cdata->std);
  3390. }
  3391. /* }}} */
  3392. ZEND_METHOD(FFI, free) /* {{{ */
  3393. {
  3394. zval *zv;
  3395. zend_ffi_cdata *cdata;
  3396. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3397. ZEND_PARSE_PARAMETERS_START(1, 1)
  3398. Z_PARAM_OBJECT_OF_CLASS_EX(zv, zend_ffi_cdata_ce, 0, 1);
  3399. ZEND_PARSE_PARAMETERS_END();
  3400. cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
  3401. if (ZEND_FFI_TYPE(cdata->type)->kind == ZEND_FFI_TYPE_POINTER) {
  3402. if (!cdata->ptr) {
  3403. zend_throw_error(zend_ffi_exception_ce, "NULL pointer dereference");
  3404. RETURN_THROWS();
  3405. }
  3406. if (cdata->ptr != (void*)&cdata->ptr_holder) {
  3407. pefree(*(void**)cdata->ptr, cdata->flags & ZEND_FFI_FLAG_PERSISTENT);
  3408. } else {
  3409. pefree(cdata->ptr_holder, (cdata->flags & ZEND_FFI_FLAG_PERSISTENT) || !is_zend_ptr(cdata->ptr_holder));
  3410. }
  3411. *(void**)cdata->ptr = NULL;
  3412. } else if (!(cdata->flags & ZEND_FFI_FLAG_OWNED)) {
  3413. pefree(cdata->ptr, cdata->flags & ZEND_FFI_FLAG_PERSISTENT);
  3414. cdata->ptr = NULL;
  3415. cdata->flags &= ~(ZEND_FFI_FLAG_OWNED|ZEND_FFI_FLAG_PERSISTENT);
  3416. cdata->std.handlers = &zend_ffi_cdata_free_handlers;
  3417. } else {
  3418. zend_throw_error(zend_ffi_exception_ce, "free() non a C pointer");
  3419. }
  3420. }
  3421. /* }}} */
  3422. ZEND_METHOD(FFI, cast) /* {{{ */
  3423. {
  3424. zend_string *type_def = NULL;
  3425. zend_object *ztype = NULL;
  3426. zend_ffi_type *old_type, *type, *type_ptr;
  3427. zend_ffi_cdata *old_cdata, *cdata;
  3428. bool is_const = 0;
  3429. zval *zv, *arg;
  3430. void *ptr;
  3431. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3432. ZEND_PARSE_PARAMETERS_START(2, 2)
  3433. Z_PARAM_OBJ_OF_CLASS_OR_STR(ztype, zend_ffi_ctype_ce, type_def)
  3434. Z_PARAM_ZVAL(zv)
  3435. ZEND_PARSE_PARAMETERS_END();
  3436. arg = zv;
  3437. ZVAL_DEREF(zv);
  3438. if (type_def) {
  3439. zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT;
  3440. if (Z_TYPE(EX(This)) == IS_OBJECT) {
  3441. zend_ffi *ffi = (zend_ffi*)Z_OBJ(EX(This));
  3442. FFI_G(symbols) = ffi->symbols;
  3443. FFI_G(tags) = ffi->tags;
  3444. } else {
  3445. FFI_G(symbols) = NULL;
  3446. FFI_G(tags) = NULL;
  3447. }
  3448. FFI_G(default_type_attr) = 0;
  3449. if (zend_ffi_parse_type(ZSTR_VAL(type_def), ZSTR_LEN(type_def), &dcl) != SUCCESS) {
  3450. zend_ffi_type_dtor(dcl.type);
  3451. if (Z_TYPE(EX(This)) != IS_OBJECT) {
  3452. if (FFI_G(tags)) {
  3453. zend_hash_destroy(FFI_G(tags));
  3454. efree(FFI_G(tags));
  3455. FFI_G(tags) = NULL;
  3456. }
  3457. if (FFI_G(symbols)) {
  3458. zend_hash_destroy(FFI_G(symbols));
  3459. efree(FFI_G(symbols));
  3460. FFI_G(symbols) = NULL;
  3461. }
  3462. }
  3463. return;
  3464. }
  3465. type = ZEND_FFI_TYPE(dcl.type);
  3466. if (dcl.attr & ZEND_FFI_ATTR_CONST) {
  3467. is_const = 1;
  3468. }
  3469. if (Z_TYPE(EX(This)) != IS_OBJECT) {
  3470. if (FFI_G(tags)) {
  3471. zend_ffi_tags_cleanup(&dcl);
  3472. }
  3473. if (FFI_G(symbols)) {
  3474. zend_hash_destroy(FFI_G(symbols));
  3475. efree(FFI_G(symbols));
  3476. FFI_G(symbols) = NULL;
  3477. }
  3478. }
  3479. FFI_G(symbols) = NULL;
  3480. FFI_G(tags) = NULL;
  3481. type_ptr = dcl.type;
  3482. } else {
  3483. zend_ffi_ctype *ctype = (zend_ffi_ctype*) ztype;
  3484. type_ptr = type = ctype->type;
  3485. if (ZEND_FFI_TYPE_IS_OWNED(type)) {
  3486. type = ZEND_FFI_TYPE(type);
  3487. if (!(type->attr & ZEND_FFI_ATTR_STORED)) {
  3488. if (GC_REFCOUNT(&ctype->std) == 1) {
  3489. /* transfer type ownership */
  3490. ctype->type = type;
  3491. } else {
  3492. ctype->type = type_ptr = type = zend_ffi_remember_type(type);
  3493. }
  3494. }
  3495. }
  3496. }
  3497. if (Z_TYPE_P(zv) != IS_OBJECT || Z_OBJCE_P(zv) != zend_ffi_cdata_ce) {
  3498. if (type->kind < ZEND_FFI_TYPE_POINTER && Z_TYPE_P(zv) < IS_STRING) {
  3499. /* numeric conversion */
  3500. cdata = (zend_ffi_cdata*)zend_ffi_cdata_new(zend_ffi_cdata_ce);
  3501. cdata->std.handlers = &zend_ffi_cdata_value_handlers;
  3502. cdata->type = type_ptr;
  3503. cdata->ptr = emalloc(type->size);
  3504. zend_ffi_zval_to_cdata(cdata->ptr, type, zv);
  3505. cdata->flags = ZEND_FFI_FLAG_OWNED;
  3506. if (is_const) {
  3507. cdata->flags |= ZEND_FFI_FLAG_CONST;
  3508. }
  3509. RETURN_OBJ(&cdata->std);
  3510. } else if (type->kind == ZEND_FFI_TYPE_POINTER && Z_TYPE_P(zv) == IS_LONG) {
  3511. /* number to pointer conversion */
  3512. cdata = (zend_ffi_cdata*)zend_ffi_cdata_new(zend_ffi_cdata_ce);
  3513. cdata->type = type_ptr;
  3514. cdata->ptr = &cdata->ptr_holder;
  3515. cdata->ptr_holder = (void*)(intptr_t)Z_LVAL_P(zv);
  3516. if (is_const) {
  3517. cdata->flags |= ZEND_FFI_FLAG_CONST;
  3518. }
  3519. RETURN_OBJ(&cdata->std);
  3520. } else if (type->kind == ZEND_FFI_TYPE_POINTER && Z_TYPE_P(zv) == IS_NULL) {
  3521. /* null -> pointer */
  3522. cdata = (zend_ffi_cdata*)zend_ffi_cdata_new(zend_ffi_cdata_ce);
  3523. cdata->type = type_ptr;
  3524. cdata->ptr = &cdata->ptr_holder;
  3525. cdata->ptr_holder = NULL;
  3526. if (is_const) {
  3527. cdata->flags |= ZEND_FFI_FLAG_CONST;
  3528. }
  3529. RETURN_OBJ(&cdata->std);
  3530. } else {
  3531. zend_wrong_parameter_class_error(2, "FFI\\CData", zv);
  3532. RETURN_THROWS();
  3533. }
  3534. }
  3535. old_cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
  3536. old_type = ZEND_FFI_TYPE(old_cdata->type);
  3537. ptr = old_cdata->ptr;
  3538. cdata = (zend_ffi_cdata*)zend_ffi_cdata_new(zend_ffi_cdata_ce);
  3539. if (type->kind < ZEND_FFI_TYPE_POINTER) {
  3540. cdata->std.handlers = &zend_ffi_cdata_value_handlers;
  3541. }
  3542. cdata->type = type_ptr;
  3543. if (old_type->kind == ZEND_FFI_TYPE_POINTER
  3544. && type->kind != ZEND_FFI_TYPE_POINTER
  3545. && ZEND_FFI_TYPE(old_type->pointer.type)->kind == ZEND_FFI_TYPE_VOID) {
  3546. /* automatically dereference void* pointers ??? */
  3547. cdata->ptr = *(void**)ptr;
  3548. } else if (old_type->kind == ZEND_FFI_TYPE_ARRAY
  3549. && type->kind == ZEND_FFI_TYPE_POINTER
  3550. && zend_ffi_is_compatible_type(ZEND_FFI_TYPE(old_type->array.type), ZEND_FFI_TYPE(type->pointer.type))) { cdata->ptr = &cdata->ptr_holder;
  3551. cdata->ptr = &cdata->ptr_holder;
  3552. cdata->ptr_holder = old_cdata->ptr;
  3553. } else if (old_type->kind == ZEND_FFI_TYPE_POINTER
  3554. && type->kind == ZEND_FFI_TYPE_ARRAY
  3555. && zend_ffi_is_compatible_type(ZEND_FFI_TYPE(old_type->pointer.type), ZEND_FFI_TYPE(type->array.type))) {
  3556. cdata->ptr = old_cdata->ptr_holder;
  3557. } else if (type->size > old_type->size) {
  3558. zend_object_release(&cdata->std);
  3559. zend_throw_error(zend_ffi_exception_ce, "attempt to cast to larger type");
  3560. RETURN_THROWS();
  3561. } else if (ptr != &old_cdata->ptr_holder) {
  3562. cdata->ptr = ptr;
  3563. } else {
  3564. cdata->ptr = &cdata->ptr_holder;
  3565. cdata->ptr_holder = old_cdata->ptr_holder;
  3566. }
  3567. if (is_const) {
  3568. cdata->flags |= ZEND_FFI_FLAG_CONST;
  3569. }
  3570. if (old_cdata->flags & ZEND_FFI_FLAG_OWNED) {
  3571. if (GC_REFCOUNT(&old_cdata->std) == 1 && Z_REFCOUNT_P(arg) == 1) {
  3572. /* transfer ownership */
  3573. old_cdata->flags &= ~ZEND_FFI_FLAG_OWNED;
  3574. cdata->flags |= ZEND_FFI_FLAG_OWNED;
  3575. } else {
  3576. //???zend_throw_error(zend_ffi_exception_ce, "Attempt to cast owned C pointer");
  3577. }
  3578. }
  3579. RETURN_OBJ(&cdata->std);
  3580. }
  3581. /* }}} */
  3582. ZEND_METHOD(FFI, type) /* {{{ */
  3583. {
  3584. zend_ffi_ctype *ctype;
  3585. zend_ffi_dcl dcl = ZEND_FFI_ATTR_INIT;
  3586. zend_string *type_def;
  3587. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3588. ZEND_PARSE_PARAMETERS_START(1, 1)
  3589. Z_PARAM_STR(type_def);
  3590. ZEND_PARSE_PARAMETERS_END();
  3591. if (Z_TYPE(EX(This)) == IS_OBJECT) {
  3592. zend_ffi *ffi = (zend_ffi*)Z_OBJ(EX(This));
  3593. FFI_G(symbols) = ffi->symbols;
  3594. FFI_G(tags) = ffi->tags;
  3595. } else {
  3596. FFI_G(symbols) = NULL;
  3597. FFI_G(tags) = NULL;
  3598. }
  3599. FFI_G(default_type_attr) = 0;
  3600. if (zend_ffi_parse_type(ZSTR_VAL(type_def), ZSTR_LEN(type_def), &dcl) != SUCCESS) {
  3601. zend_ffi_type_dtor(dcl.type);
  3602. if (Z_TYPE(EX(This)) != IS_OBJECT) {
  3603. if (FFI_G(tags)) {
  3604. zend_hash_destroy(FFI_G(tags));
  3605. efree(FFI_G(tags));
  3606. FFI_G(tags) = NULL;
  3607. }
  3608. if (FFI_G(symbols)) {
  3609. zend_hash_destroy(FFI_G(symbols));
  3610. efree(FFI_G(symbols));
  3611. FFI_G(symbols) = NULL;
  3612. }
  3613. }
  3614. return;
  3615. }
  3616. if (Z_TYPE(EX(This)) != IS_OBJECT) {
  3617. if (FFI_G(tags)) {
  3618. zend_ffi_tags_cleanup(&dcl);
  3619. }
  3620. if (FFI_G(symbols)) {
  3621. zend_hash_destroy(FFI_G(symbols));
  3622. efree(FFI_G(symbols));
  3623. FFI_G(symbols) = NULL;
  3624. }
  3625. }
  3626. FFI_G(symbols) = NULL;
  3627. FFI_G(tags) = NULL;
  3628. ctype = (zend_ffi_ctype*)zend_ffi_ctype_new(zend_ffi_ctype_ce);
  3629. ctype->type = dcl.type;
  3630. RETURN_OBJ(&ctype->std);
  3631. }
  3632. /* }}} */
  3633. ZEND_METHOD(FFI, typeof) /* {{{ */
  3634. {
  3635. zval *zv, *arg;
  3636. zend_ffi_ctype *ctype;
  3637. zend_ffi_type *type;
  3638. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3639. ZEND_PARSE_PARAMETERS_START(1, 1)
  3640. Z_PARAM_ZVAL(zv);
  3641. ZEND_PARSE_PARAMETERS_END();
  3642. arg = zv;
  3643. ZVAL_DEREF(zv);
  3644. if (Z_TYPE_P(zv) == IS_OBJECT && Z_OBJCE_P(zv) == zend_ffi_cdata_ce) {
  3645. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
  3646. type = cdata->type;
  3647. if (ZEND_FFI_TYPE_IS_OWNED(type)) {
  3648. type = ZEND_FFI_TYPE(type);
  3649. if (!(type->attr & ZEND_FFI_ATTR_STORED)) {
  3650. if (GC_REFCOUNT(&cdata->std) == 1 && Z_REFCOUNT_P(arg) == 1) {
  3651. /* transfer type ownership */
  3652. cdata->type = type;
  3653. type = ZEND_FFI_TYPE_MAKE_OWNED(type);
  3654. } else {
  3655. cdata->type = type = zend_ffi_remember_type(type);
  3656. }
  3657. }
  3658. }
  3659. } else {
  3660. zend_wrong_parameter_class_error(1, "FFI\\CData", zv);
  3661. RETURN_THROWS();
  3662. }
  3663. ctype = (zend_ffi_ctype*)zend_ffi_ctype_new(zend_ffi_ctype_ce);
  3664. ctype->type = type;
  3665. RETURN_OBJ(&ctype->std);
  3666. }
  3667. /* }}} */
  3668. ZEND_METHOD(FFI, arrayType) /* {{{ */
  3669. {
  3670. zval *ztype;
  3671. zend_ffi_ctype *ctype;
  3672. zend_ffi_type *type;
  3673. HashTable *dims;
  3674. zval *val;
  3675. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3676. ZEND_PARSE_PARAMETERS_START(2, 2)
  3677. Z_PARAM_OBJECT_OF_CLASS(ztype, zend_ffi_ctype_ce)
  3678. Z_PARAM_ARRAY_HT(dims)
  3679. ZEND_PARSE_PARAMETERS_END();
  3680. ctype = (zend_ffi_ctype*)Z_OBJ_P(ztype);
  3681. type = ZEND_FFI_TYPE(ctype->type);
  3682. if (type->kind == ZEND_FFI_TYPE_FUNC) {
  3683. zend_throw_error(zend_ffi_exception_ce, "Array of functions is not allowed");
  3684. RETURN_THROWS();
  3685. } else if (type->kind == ZEND_FFI_TYPE_ARRAY && (type->attr & ZEND_FFI_ATTR_INCOMPLETE_ARRAY)) {
  3686. zend_throw_error(zend_ffi_exception_ce, "Only the leftmost array can be undimensioned");
  3687. RETURN_THROWS();
  3688. } else if (type->kind == ZEND_FFI_TYPE_VOID) {
  3689. zend_throw_error(zend_ffi_exception_ce, "Array of void type is not allowed");
  3690. RETURN_THROWS();
  3691. } else if (type->attr & ZEND_FFI_ATTR_INCOMPLETE_TAG) {
  3692. zend_throw_error(zend_ffi_exception_ce, "Array of incomplete type is not allowed");
  3693. RETURN_THROWS();
  3694. }
  3695. if (ZEND_FFI_TYPE_IS_OWNED(ctype->type)) {
  3696. if (!(type->attr & ZEND_FFI_ATTR_STORED)) {
  3697. if (GC_REFCOUNT(&ctype->std) == 1) {
  3698. /* transfer type ownership */
  3699. ctype->type = type;
  3700. type = ZEND_FFI_TYPE_MAKE_OWNED(type);
  3701. } else {
  3702. ctype->type = type = zend_ffi_remember_type(type);
  3703. }
  3704. }
  3705. }
  3706. ZEND_HASH_REVERSE_FOREACH_VAL(dims, val) {
  3707. zend_long n = zval_get_long(val);
  3708. zend_ffi_type *new_type;
  3709. if (n < 0) {
  3710. zend_throw_error(zend_ffi_exception_ce, "negative array index");
  3711. zend_ffi_type_dtor(type);
  3712. RETURN_THROWS();
  3713. } else if (ZEND_FFI_TYPE(type)->kind == ZEND_FFI_TYPE_ARRAY && (ZEND_FFI_TYPE(type)->attr & ZEND_FFI_ATTR_INCOMPLETE_ARRAY)) {
  3714. zend_throw_error(zend_ffi_exception_ce, "only the leftmost array can be undimensioned");
  3715. zend_ffi_type_dtor(type);
  3716. RETURN_THROWS();
  3717. }
  3718. new_type = emalloc(sizeof(zend_ffi_type));
  3719. new_type->kind = ZEND_FFI_TYPE_ARRAY;
  3720. new_type->attr = 0;
  3721. new_type->size = n * ZEND_FFI_TYPE(type)->size;
  3722. new_type->align = ZEND_FFI_TYPE(type)->align;
  3723. new_type->array.type = type;
  3724. new_type->array.length = n;
  3725. if (n == 0) {
  3726. new_type->attr |= ZEND_FFI_ATTR_INCOMPLETE_ARRAY;
  3727. }
  3728. type = ZEND_FFI_TYPE_MAKE_OWNED(new_type);
  3729. } ZEND_HASH_FOREACH_END();
  3730. ctype = (zend_ffi_ctype*)zend_ffi_ctype_new(zend_ffi_ctype_ce);
  3731. ctype->type = type;
  3732. RETURN_OBJ(&ctype->std);
  3733. }
  3734. /* }}} */
  3735. ZEND_METHOD(FFI, addr) /* {{{ */
  3736. {
  3737. zend_ffi_type *type, *new_type;
  3738. zend_ffi_cdata *cdata, *new_cdata;
  3739. zval *zv, *arg;
  3740. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3741. ZEND_PARSE_PARAMETERS_START(1, 1)
  3742. Z_PARAM_ZVAL(zv)
  3743. ZEND_PARSE_PARAMETERS_END();
  3744. arg = zv;
  3745. ZVAL_DEREF(zv);
  3746. if (Z_TYPE_P(zv) != IS_OBJECT || Z_OBJCE_P(zv) != zend_ffi_cdata_ce) {
  3747. zend_wrong_parameter_class_error(1, "FFI\\CData", zv);
  3748. RETURN_THROWS();
  3749. }
  3750. cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
  3751. type = ZEND_FFI_TYPE(cdata->type);
  3752. new_type = emalloc(sizeof(zend_ffi_type));
  3753. new_type->kind = ZEND_FFI_TYPE_POINTER;
  3754. new_type->attr = 0;
  3755. new_type->size = sizeof(void*);
  3756. new_type->align = _Alignof(void*);
  3757. /* life-time (source must relive the resulting pointer) ??? */
  3758. new_type->pointer.type = type;
  3759. new_cdata = (zend_ffi_cdata*)zend_ffi_cdata_new(zend_ffi_cdata_ce);
  3760. new_cdata->type = ZEND_FFI_TYPE_MAKE_OWNED(new_type);
  3761. new_cdata->ptr_holder = cdata->ptr;
  3762. new_cdata->ptr = &new_cdata->ptr_holder;
  3763. if (GC_REFCOUNT(&cdata->std) == 1 && Z_REFCOUNT_P(arg) == 1) {
  3764. if (ZEND_FFI_TYPE_IS_OWNED(cdata->type)) {
  3765. /* transfer type ownership */
  3766. cdata->type = type;
  3767. new_type->pointer.type = ZEND_FFI_TYPE_MAKE_OWNED(type);
  3768. }
  3769. if (cdata->flags & ZEND_FFI_FLAG_OWNED) {
  3770. /* transfer ownership */
  3771. cdata->flags &= ~ZEND_FFI_FLAG_OWNED;
  3772. new_cdata->flags |= ZEND_FFI_FLAG_OWNED;
  3773. }
  3774. }
  3775. RETURN_OBJ(&new_cdata->std);
  3776. }
  3777. /* }}} */
  3778. ZEND_METHOD(FFI, sizeof) /* {{{ */
  3779. {
  3780. zval *zv;
  3781. zend_ffi_type *type;
  3782. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3783. ZEND_PARSE_PARAMETERS_START(1, 1)
  3784. Z_PARAM_ZVAL(zv);
  3785. ZEND_PARSE_PARAMETERS_END();
  3786. ZVAL_DEREF(zv);
  3787. if (Z_TYPE_P(zv) == IS_OBJECT && Z_OBJCE_P(zv) == zend_ffi_cdata_ce) {
  3788. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
  3789. type = ZEND_FFI_TYPE(cdata->type);
  3790. } else if (Z_TYPE_P(zv) == IS_OBJECT && Z_OBJCE_P(zv) == zend_ffi_ctype_ce) {
  3791. zend_ffi_ctype *ctype = (zend_ffi_ctype*)Z_OBJ_P(zv);
  3792. type = ZEND_FFI_TYPE(ctype->type);
  3793. } else {
  3794. zend_wrong_parameter_class_error(1, "FFI\\CData or FFI\\CType", zv);
  3795. RETURN_THROWS();
  3796. }
  3797. RETURN_LONG(type->size);
  3798. }
  3799. /* }}} */
  3800. ZEND_METHOD(FFI, alignof) /* {{{ */
  3801. {
  3802. zval *zv;
  3803. zend_ffi_type *type;
  3804. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3805. ZEND_PARSE_PARAMETERS_START(1, 1)
  3806. Z_PARAM_ZVAL(zv);
  3807. ZEND_PARSE_PARAMETERS_END();
  3808. ZVAL_DEREF(zv);
  3809. if (Z_TYPE_P(zv) == IS_OBJECT && Z_OBJCE_P(zv) == zend_ffi_cdata_ce) {
  3810. zend_ffi_cdata *cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
  3811. type = ZEND_FFI_TYPE(cdata->type);
  3812. } else if (Z_TYPE_P(zv) == IS_OBJECT && Z_OBJCE_P(zv) == zend_ffi_ctype_ce) {
  3813. zend_ffi_ctype *ctype = (zend_ffi_ctype*)Z_OBJ_P(zv);
  3814. type = ZEND_FFI_TYPE(ctype->type);
  3815. } else {
  3816. zend_wrong_parameter_class_error(1, "FFI\\CData or FFI\\CType", zv);
  3817. RETURN_THROWS();
  3818. }
  3819. RETURN_LONG(type->align);
  3820. }
  3821. /* }}} */
  3822. ZEND_METHOD(FFI, memcpy) /* {{{ */
  3823. {
  3824. zval *zv1, *zv2;
  3825. zend_ffi_cdata *cdata1, *cdata2;
  3826. zend_ffi_type *type1, *type2;
  3827. void *ptr1, *ptr2;
  3828. zend_long size;
  3829. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3830. ZEND_PARSE_PARAMETERS_START(3, 3)
  3831. Z_PARAM_OBJECT_OF_CLASS_EX(zv1, zend_ffi_cdata_ce, 0, 1);
  3832. Z_PARAM_ZVAL(zv2)
  3833. Z_PARAM_LONG(size)
  3834. ZEND_PARSE_PARAMETERS_END();
  3835. cdata1 = (zend_ffi_cdata*)Z_OBJ_P(zv1);
  3836. type1 = ZEND_FFI_TYPE(cdata1->type);
  3837. if (type1->kind == ZEND_FFI_TYPE_POINTER) {
  3838. ptr1 = *(void**)cdata1->ptr;
  3839. } else {
  3840. ptr1 = cdata1->ptr;
  3841. if (type1->kind != ZEND_FFI_TYPE_POINTER && size > type1->size) {
  3842. zend_throw_error(zend_ffi_exception_ce, "Attempt to write over data boundary");
  3843. RETURN_THROWS();
  3844. }
  3845. }
  3846. ZVAL_DEREF(zv2);
  3847. if (Z_TYPE_P(zv2) == IS_STRING) {
  3848. ptr2 = Z_STRVAL_P(zv2);
  3849. if (size > Z_STRLEN_P(zv2)) {
  3850. zend_throw_error(zend_ffi_exception_ce, "Attempt to read over string boundary");
  3851. RETURN_THROWS();
  3852. }
  3853. } else if (Z_TYPE_P(zv2) == IS_OBJECT && Z_OBJCE_P(zv2) == zend_ffi_cdata_ce) {
  3854. cdata2 = (zend_ffi_cdata*)Z_OBJ_P(zv2);
  3855. type2 = ZEND_FFI_TYPE(cdata2->type);
  3856. if (type2->kind == ZEND_FFI_TYPE_POINTER) {
  3857. ptr2 = *(void**)cdata2->ptr;
  3858. } else {
  3859. ptr2 = cdata2->ptr;
  3860. if (type2->kind != ZEND_FFI_TYPE_POINTER && size > type2->size) {
  3861. zend_throw_error(zend_ffi_exception_ce, "Attempt to read over data boundary");
  3862. RETURN_THROWS();
  3863. }
  3864. }
  3865. } else {
  3866. zend_wrong_parameter_class_error(2, "FFI\\CData or string", zv2);
  3867. RETURN_THROWS();
  3868. }
  3869. memcpy(ptr1, ptr2, size);
  3870. }
  3871. /* }}} */
  3872. ZEND_METHOD(FFI, memcmp) /* {{{ */
  3873. {
  3874. zval *zv1, *zv2;
  3875. zend_ffi_cdata *cdata1, *cdata2;
  3876. zend_ffi_type *type1, *type2;
  3877. void *ptr1, *ptr2;
  3878. zend_long size;
  3879. int ret;
  3880. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3881. ZEND_PARSE_PARAMETERS_START(3, 3)
  3882. Z_PARAM_ZVAL(zv1);
  3883. Z_PARAM_ZVAL(zv2);
  3884. Z_PARAM_LONG(size)
  3885. ZEND_PARSE_PARAMETERS_END();
  3886. ZVAL_DEREF(zv1);
  3887. if (Z_TYPE_P(zv1) == IS_STRING) {
  3888. ptr1 = Z_STRVAL_P(zv1);
  3889. if (size > Z_STRLEN_P(zv1)) {
  3890. zend_throw_error(zend_ffi_exception_ce, "attempt to read over string boundary");
  3891. RETURN_THROWS();
  3892. }
  3893. } else if (Z_TYPE_P(zv1) == IS_OBJECT && Z_OBJCE_P(zv1) == zend_ffi_cdata_ce) {
  3894. cdata1 = (zend_ffi_cdata*)Z_OBJ_P(zv1);
  3895. type1 = ZEND_FFI_TYPE(cdata1->type);
  3896. if (type1->kind == ZEND_FFI_TYPE_POINTER) {
  3897. ptr1 = *(void**)cdata1->ptr;
  3898. } else {
  3899. ptr1 = cdata1->ptr;
  3900. if (type1->kind != ZEND_FFI_TYPE_POINTER && size > type1->size) {
  3901. zend_throw_error(zend_ffi_exception_ce, "attempt to read over data boundary");
  3902. RETURN_THROWS();
  3903. }
  3904. }
  3905. } else {
  3906. zend_wrong_parameter_class_error(1, "FFI\\CData or string", zv1);
  3907. RETURN_THROWS();
  3908. }
  3909. ZVAL_DEREF(zv2);
  3910. if (Z_TYPE_P(zv2) == IS_STRING) {
  3911. ptr2 = Z_STRVAL_P(zv2);
  3912. if (size > Z_STRLEN_P(zv2)) {
  3913. zend_throw_error(zend_ffi_exception_ce, "Attempt to read over string boundary");
  3914. RETURN_THROWS();
  3915. }
  3916. } else if (Z_TYPE_P(zv2) == IS_OBJECT && Z_OBJCE_P(zv2) == zend_ffi_cdata_ce) {
  3917. cdata2 = (zend_ffi_cdata*)Z_OBJ_P(zv2);
  3918. type2 = ZEND_FFI_TYPE(cdata2->type);
  3919. if (type2->kind == ZEND_FFI_TYPE_POINTER) {
  3920. ptr2 = *(void**)cdata2->ptr;
  3921. } else {
  3922. ptr2 = cdata2->ptr;
  3923. if (type2->kind != ZEND_FFI_TYPE_POINTER && size > type2->size) {
  3924. zend_throw_error(zend_ffi_exception_ce, "Attempt to read over data boundary");
  3925. RETURN_THROWS();
  3926. }
  3927. }
  3928. } else {
  3929. zend_wrong_parameter_class_error(2, "FFI\\CData or string", zv2);
  3930. RETURN_THROWS();
  3931. }
  3932. ret = memcmp(ptr1, ptr2, size);
  3933. if (ret == 0) {
  3934. RETVAL_LONG(0);
  3935. } else if (ret < 0) {
  3936. RETVAL_LONG(-1);
  3937. } else {
  3938. RETVAL_LONG(1);
  3939. }
  3940. }
  3941. /* }}} */
  3942. ZEND_METHOD(FFI, memset) /* {{{ */
  3943. {
  3944. zval *zv;
  3945. zend_ffi_cdata *cdata;
  3946. zend_ffi_type *type;
  3947. void *ptr;
  3948. zend_long ch, size;
  3949. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3950. ZEND_PARSE_PARAMETERS_START(3, 3)
  3951. Z_PARAM_OBJECT_OF_CLASS_EX(zv, zend_ffi_cdata_ce, 0, 1);
  3952. Z_PARAM_LONG(ch)
  3953. Z_PARAM_LONG(size)
  3954. ZEND_PARSE_PARAMETERS_END();
  3955. cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
  3956. type = ZEND_FFI_TYPE(cdata->type);
  3957. if (type->kind == ZEND_FFI_TYPE_POINTER) {
  3958. ptr = *(void**)cdata->ptr;
  3959. } else {
  3960. ptr = cdata->ptr;
  3961. if (type->kind != ZEND_FFI_TYPE_POINTER && size > type->size) {
  3962. zend_throw_error(zend_ffi_exception_ce, "attempt to write over data boundary");
  3963. RETURN_THROWS();
  3964. }
  3965. }
  3966. memset(ptr, ch, size);
  3967. }
  3968. /* }}} */
  3969. ZEND_METHOD(FFI, string) /* {{{ */
  3970. {
  3971. zval *zv;
  3972. zend_ffi_cdata *cdata;
  3973. zend_ffi_type *type;
  3974. void *ptr;
  3975. zend_long size;
  3976. bool size_is_null = 1;
  3977. ZEND_FFI_VALIDATE_API_RESTRICTION();
  3978. ZEND_PARSE_PARAMETERS_START(1, 2)
  3979. Z_PARAM_OBJECT_OF_CLASS_EX(zv, zend_ffi_cdata_ce, 0, 1);
  3980. Z_PARAM_OPTIONAL
  3981. Z_PARAM_LONG_OR_NULL(size, size_is_null)
  3982. ZEND_PARSE_PARAMETERS_END();
  3983. cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
  3984. type = ZEND_FFI_TYPE(cdata->type);
  3985. if (!size_is_null) {
  3986. if (type->kind == ZEND_FFI_TYPE_POINTER) {
  3987. ptr = *(void**)cdata->ptr;
  3988. } else {
  3989. ptr = cdata->ptr;
  3990. if (type->kind != ZEND_FFI_TYPE_POINTER && size > type->size) {
  3991. zend_throw_error(zend_ffi_exception_ce, "attempt to read over data boundary");
  3992. RETURN_THROWS();
  3993. }
  3994. }
  3995. RETURN_STRINGL((char*)ptr, size);
  3996. } else {
  3997. if (type->kind == ZEND_FFI_TYPE_POINTER && ZEND_FFI_TYPE(type->pointer.type)->kind == ZEND_FFI_TYPE_CHAR) {
  3998. ptr = *(void**)cdata->ptr;
  3999. } else if (type->kind == ZEND_FFI_TYPE_ARRAY && ZEND_FFI_TYPE(type->array.type)->kind == ZEND_FFI_TYPE_CHAR) {
  4000. ptr = cdata->ptr;
  4001. } else {
  4002. zend_throw_error(zend_ffi_exception_ce, "FFI\\Cdata is not a C string");
  4003. RETURN_THROWS();
  4004. }
  4005. RETURN_STRING((char*)ptr);
  4006. }
  4007. }
  4008. /* }}} */
  4009. ZEND_METHOD(FFI, isNull) /* {{{ */
  4010. {
  4011. zval *zv;
  4012. zend_ffi_cdata *cdata;
  4013. zend_ffi_type *type;
  4014. ZEND_FFI_VALIDATE_API_RESTRICTION();
  4015. ZEND_PARSE_PARAMETERS_START(1, 1)
  4016. Z_PARAM_ZVAL(zv);
  4017. ZEND_PARSE_PARAMETERS_END();
  4018. ZVAL_DEREF(zv);
  4019. if (Z_TYPE_P(zv) != IS_OBJECT || Z_OBJCE_P(zv) != zend_ffi_cdata_ce) {
  4020. zend_wrong_parameter_class_error(1, "FFI\\CData", zv);
  4021. RETURN_THROWS();
  4022. }
  4023. cdata = (zend_ffi_cdata*)Z_OBJ_P(zv);
  4024. type = ZEND_FFI_TYPE(cdata->type);
  4025. if (type->kind != ZEND_FFI_TYPE_POINTER){
  4026. zend_throw_error(zend_ffi_exception_ce, "FFI\\Cdata is not a pointer");
  4027. RETURN_THROWS();
  4028. }
  4029. RETURN_BOOL(*(void**)cdata->ptr == NULL);
  4030. }
  4031. /* }}} */
  4032. ZEND_METHOD(FFI_CType, getName) /* {{{ */
  4033. {
  4034. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4035. if (zend_parse_parameters_none() == FAILURE) {
  4036. RETURN_THROWS();
  4037. }
  4038. zend_ffi_ctype_name_buf buf;
  4039. buf.start = buf.end = buf.buf + ((MAX_TYPE_NAME_LEN * 3) / 4);
  4040. if (!zend_ffi_ctype_name(&buf, ZEND_FFI_TYPE(ctype->type))) {
  4041. RETURN_STR_COPY(Z_OBJ_P(ZEND_THIS)->ce->name);
  4042. } else {
  4043. size_t len = buf.end - buf.start;
  4044. zend_string *res = zend_string_init(buf.start, len, 0);
  4045. RETURN_STR(res);
  4046. }
  4047. }
  4048. /* }}} */
  4049. ZEND_METHOD(FFI_CType, getKind) /* {{{ */
  4050. {
  4051. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4052. zend_ffi_type *type;
  4053. if (zend_parse_parameters_none() == FAILURE) {
  4054. RETURN_THROWS();
  4055. }
  4056. type = ZEND_FFI_TYPE(ctype->type);
  4057. RETURN_LONG(type->kind);
  4058. }
  4059. /* }}} */
  4060. ZEND_METHOD(FFI_CType, getSize) /* {{{ */
  4061. {
  4062. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4063. zend_ffi_type *type;
  4064. if (zend_parse_parameters_none() == FAILURE) {
  4065. RETURN_THROWS();
  4066. }
  4067. type = ZEND_FFI_TYPE(ctype->type);
  4068. RETURN_LONG(type->size);
  4069. }
  4070. /* }}} */
  4071. ZEND_METHOD(FFI_CType, getAlignment) /* {{{ */
  4072. {
  4073. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4074. zend_ffi_type *type;
  4075. if (zend_parse_parameters_none() == FAILURE) {
  4076. RETURN_THROWS();
  4077. }
  4078. type = ZEND_FFI_TYPE(ctype->type);
  4079. RETURN_LONG(type->align);
  4080. }
  4081. /* }}} */
  4082. ZEND_METHOD(FFI_CType, getAttributes) /* {{{ */
  4083. {
  4084. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4085. zend_ffi_type *type;
  4086. if (zend_parse_parameters_none() == FAILURE) {
  4087. RETURN_THROWS();
  4088. }
  4089. type = ZEND_FFI_TYPE(ctype->type);
  4090. RETURN_LONG(type->attr);
  4091. }
  4092. /* }}} */
  4093. ZEND_METHOD(FFI_CType, getEnumKind) /* {{{ */
  4094. {
  4095. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4096. zend_ffi_type *type;
  4097. if (zend_parse_parameters_none() == FAILURE) {
  4098. RETURN_THROWS();
  4099. }
  4100. type = ZEND_FFI_TYPE(ctype->type);
  4101. if (type->kind != ZEND_FFI_TYPE_ENUM) {
  4102. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not an enumeration");
  4103. RETURN_THROWS();
  4104. }
  4105. RETURN_LONG(type->enumeration.kind);
  4106. }
  4107. /* }}} */
  4108. ZEND_METHOD(FFI_CType, getArrayElementType) /* {{{ */
  4109. {
  4110. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4111. zend_ffi_type *type;
  4112. zend_ffi_ctype *ret;
  4113. if (zend_parse_parameters_none() == FAILURE) {
  4114. RETURN_THROWS();
  4115. }
  4116. type = ZEND_FFI_TYPE(ctype->type);
  4117. if (type->kind != ZEND_FFI_TYPE_ARRAY) {
  4118. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not an array");
  4119. RETURN_THROWS();
  4120. }
  4121. ret = (zend_ffi_ctype*)zend_ffi_ctype_new(zend_ffi_ctype_ce);
  4122. ret->type = ZEND_FFI_TYPE(type->array.type);
  4123. RETURN_OBJ(&ret->std);
  4124. }
  4125. /* }}} */
  4126. ZEND_METHOD(FFI_CType, getArrayLength) /* {{{ */
  4127. {
  4128. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4129. zend_ffi_type *type;
  4130. if (zend_parse_parameters_none() == FAILURE) {
  4131. RETURN_THROWS();
  4132. }
  4133. type = ZEND_FFI_TYPE(ctype->type);
  4134. if (type->kind != ZEND_FFI_TYPE_ARRAY) {
  4135. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not an array");
  4136. RETURN_THROWS();
  4137. }
  4138. RETURN_LONG(type->array.length);
  4139. }
  4140. /* }}} */
  4141. ZEND_METHOD(FFI_CType, getPointerType) /* {{{ */
  4142. {
  4143. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4144. zend_ffi_ctype *ret;
  4145. zend_ffi_type *type;
  4146. if (zend_parse_parameters_none() == FAILURE) {
  4147. RETURN_THROWS();
  4148. }
  4149. type = ZEND_FFI_TYPE(ctype->type);
  4150. if (type->kind != ZEND_FFI_TYPE_POINTER) {
  4151. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not a pointer");
  4152. RETURN_THROWS();
  4153. }
  4154. ret = (zend_ffi_ctype*)zend_ffi_ctype_new(zend_ffi_ctype_ce);
  4155. ret->type = ZEND_FFI_TYPE(type->pointer.type);
  4156. RETURN_OBJ(&ret->std);
  4157. }
  4158. /* }}} */
  4159. ZEND_METHOD(FFI_CType, getStructFieldNames) /* {{{ */
  4160. {
  4161. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4162. zend_ffi_type *type;
  4163. HashTable *ht;
  4164. zend_string* name;
  4165. zval zv;
  4166. if (zend_parse_parameters_none() == FAILURE) {
  4167. RETURN_THROWS();
  4168. }
  4169. type = ZEND_FFI_TYPE(ctype->type);
  4170. if (type->kind != ZEND_FFI_TYPE_STRUCT) {
  4171. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not a structure");
  4172. RETURN_THROWS();
  4173. }
  4174. ht = zend_new_array(zend_hash_num_elements(&type->record.fields));
  4175. RETVAL_ARR(ht);
  4176. ZEND_HASH_FOREACH_STR_KEY(&type->record.fields, name) {
  4177. ZVAL_STR_COPY(&zv, name);
  4178. zend_hash_next_index_insert_new(ht, &zv);
  4179. } ZEND_HASH_FOREACH_END();
  4180. }
  4181. /* }}} */
  4182. ZEND_METHOD(FFI_CType, getStructFieldOffset) /* {{{ */
  4183. {
  4184. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4185. zend_ffi_type *type;
  4186. zend_string *name;
  4187. zend_ffi_field *ptr;
  4188. ZEND_PARSE_PARAMETERS_START(1, 1)
  4189. Z_PARAM_STR(name)
  4190. ZEND_PARSE_PARAMETERS_END();
  4191. type = ZEND_FFI_TYPE(ctype->type);
  4192. if (type->kind != ZEND_FFI_TYPE_STRUCT) {
  4193. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not a structure");
  4194. RETURN_THROWS();
  4195. }
  4196. ptr = zend_hash_find_ptr(&type->record.fields, name);
  4197. if (!ptr) {
  4198. zend_throw_error(zend_ffi_exception_ce, "Wrong field name");
  4199. RETURN_THROWS();
  4200. }
  4201. RETURN_LONG(ptr->offset);
  4202. }
  4203. /* }}} */
  4204. ZEND_METHOD(FFI_CType, getStructFieldType) /* {{{ */
  4205. {
  4206. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4207. zend_ffi_type *type;
  4208. zend_string *name;
  4209. zend_ffi_field *ptr;
  4210. zend_ffi_ctype *ret;
  4211. ZEND_PARSE_PARAMETERS_START(1, 1)
  4212. Z_PARAM_STR(name)
  4213. ZEND_PARSE_PARAMETERS_END();
  4214. type = ZEND_FFI_TYPE(ctype->type);
  4215. if (type->kind != ZEND_FFI_TYPE_STRUCT) {
  4216. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not a structure");
  4217. RETURN_THROWS();
  4218. }
  4219. ptr = zend_hash_find_ptr(&type->record.fields, name);
  4220. if (!ptr) {
  4221. zend_throw_error(zend_ffi_exception_ce, "Wrong field name");
  4222. RETURN_THROWS();
  4223. }
  4224. ret = (zend_ffi_ctype*)zend_ffi_ctype_new(zend_ffi_ctype_ce);
  4225. ret->type = ZEND_FFI_TYPE(ptr->type);
  4226. RETURN_OBJ(&ret->std);
  4227. }
  4228. /* }}} */
  4229. ZEND_METHOD(FFI_CType, getFuncABI) /* {{{ */
  4230. {
  4231. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4232. zend_ffi_type *type;
  4233. if (zend_parse_parameters_none() == FAILURE) {
  4234. RETURN_THROWS();
  4235. }
  4236. type = ZEND_FFI_TYPE(ctype->type);
  4237. if (type->kind != ZEND_FFI_TYPE_FUNC) {
  4238. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not a function");
  4239. RETURN_THROWS();
  4240. }
  4241. RETURN_LONG(type->func.abi);
  4242. }
  4243. /* }}} */
  4244. ZEND_METHOD(FFI_CType, getFuncReturnType) /* {{{ */
  4245. {
  4246. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4247. zend_ffi_ctype *ret;
  4248. zend_ffi_type *type;
  4249. if (zend_parse_parameters_none() == FAILURE) {
  4250. RETURN_THROWS();
  4251. }
  4252. type = ZEND_FFI_TYPE(ctype->type);
  4253. if (type->kind != ZEND_FFI_TYPE_FUNC) {
  4254. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not a function");
  4255. RETURN_THROWS();
  4256. }
  4257. ret = (zend_ffi_ctype*)zend_ffi_ctype_new(zend_ffi_ctype_ce);
  4258. ret->type = ZEND_FFI_TYPE(type->func.ret_type);
  4259. RETURN_OBJ(&ret->std);
  4260. }
  4261. /* }}} */
  4262. ZEND_METHOD(FFI_CType, getFuncParameterCount) /* {{{ */
  4263. {
  4264. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4265. zend_ffi_type *type;
  4266. if (zend_parse_parameters_none() == FAILURE) {
  4267. RETURN_THROWS();
  4268. }
  4269. type = ZEND_FFI_TYPE(ctype->type);
  4270. if (type->kind != ZEND_FFI_TYPE_FUNC) {
  4271. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not a function");
  4272. RETURN_THROWS();
  4273. }
  4274. RETURN_LONG(type->func.args ? zend_hash_num_elements(type->func.args) : 0);
  4275. }
  4276. /* }}} */
  4277. ZEND_METHOD(FFI_CType, getFuncParameterType) /* {{{ */
  4278. {
  4279. zend_ffi_ctype *ctype = (zend_ffi_ctype*)(Z_OBJ_P(ZEND_THIS));
  4280. zend_ffi_type *type, *ptr;
  4281. zend_long n;
  4282. zend_ffi_ctype *ret;
  4283. ZEND_PARSE_PARAMETERS_START(1, 1)
  4284. Z_PARAM_LONG(n)
  4285. ZEND_PARSE_PARAMETERS_END();
  4286. type = ZEND_FFI_TYPE(ctype->type);
  4287. if (type->kind != ZEND_FFI_TYPE_FUNC) {
  4288. zend_throw_error(zend_ffi_exception_ce, "FFI\\CType is not a function");
  4289. RETURN_THROWS();
  4290. }
  4291. if (!type->func.args) {
  4292. zend_throw_error(zend_ffi_exception_ce, "Wrong argument number");
  4293. RETURN_THROWS();
  4294. }
  4295. ptr = zend_hash_index_find_ptr(type->func.args, n);
  4296. if (!ptr) {
  4297. zend_throw_error(zend_ffi_exception_ce, "Wrong argument number");
  4298. RETURN_THROWS();
  4299. }
  4300. ret = (zend_ffi_ctype*)zend_ffi_ctype_new(zend_ffi_ctype_ce);
  4301. ret->type = ZEND_FFI_TYPE(ptr);
  4302. RETURN_OBJ(&ret->std);
  4303. }
  4304. /* }}} */
  4305. static char *zend_ffi_parse_directives(const char *filename, char *code_pos, char **scope_name, char **lib, bool preload) /* {{{ */
  4306. {
  4307. char *p;
  4308. *scope_name = NULL;
  4309. *lib = NULL;
  4310. while (*code_pos == '#') {
  4311. if (strncmp(code_pos, "#define FFI_SCOPE", sizeof("#define FFI_SCOPE") - 1) == 0
  4312. && (code_pos[sizeof("#define FFI_SCOPE") - 1] == ' '
  4313. || code_pos[sizeof("#define FFI_SCOPE") - 1] == '\t')) {
  4314. p = code_pos + sizeof("#define FFI_SCOPE");
  4315. while (*p == ' ' || *p == '\t') {
  4316. p++;
  4317. }
  4318. if (*p != '"') {
  4319. if (preload) {
  4320. zend_error(E_WARNING, "FFI: failed pre-loading '%s', bad FFI_SCOPE define", filename);
  4321. } else {
  4322. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', bad FFI_SCOPE define", filename);
  4323. }
  4324. return NULL;
  4325. }
  4326. p++;
  4327. if (*scope_name) {
  4328. if (preload) {
  4329. zend_error(E_WARNING, "FFI: failed pre-loading '%s', FFI_SCOPE defined twice", filename);
  4330. } else {
  4331. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', FFI_SCOPE defined twice", filename);
  4332. }
  4333. return NULL;
  4334. }
  4335. *scope_name = p;
  4336. while (1) {
  4337. if (*p == '\"') {
  4338. *p = 0;
  4339. p++;
  4340. break;
  4341. } else if (*p <= ' ') {
  4342. if (preload) {
  4343. zend_error(E_WARNING, "FFI: failed pre-loading '%s', bad FFI_SCOPE define", filename);
  4344. } else {
  4345. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', bad FFI_SCOPE define", filename);
  4346. }
  4347. return NULL;
  4348. }
  4349. p++;
  4350. }
  4351. while (*p == ' ' || *p == '\t') {
  4352. p++;
  4353. }
  4354. while (*p == '\r' || *p == '\n') {
  4355. p++;
  4356. }
  4357. code_pos = p;
  4358. } else if (strncmp(code_pos, "#define FFI_LIB", sizeof("#define FFI_LIB") - 1) == 0
  4359. && (code_pos[sizeof("#define FFI_LIB") - 1] == ' '
  4360. || code_pos[sizeof("#define FFI_LIB") - 1] == '\t')) {
  4361. p = code_pos + sizeof("#define FFI_LIB");
  4362. while (*p == ' ' || *p == '\t') {
  4363. p++;
  4364. }
  4365. if (*p != '"') {
  4366. if (preload) {
  4367. zend_error(E_WARNING, "FFI: failed pre-loading '%s', bad FFI_LIB define", filename);
  4368. } else {
  4369. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', bad FFI_LIB define", filename);
  4370. }
  4371. return NULL;
  4372. }
  4373. p++;
  4374. if (*lib) {
  4375. if (preload) {
  4376. zend_error(E_WARNING, "FFI: failed pre-loading '%s', FFI_LIB defined twice", filename);
  4377. } else {
  4378. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', FFI_LIB defined twice", filename);
  4379. }
  4380. return NULL;
  4381. }
  4382. *lib = p;
  4383. while (1) {
  4384. if (*p == '\"') {
  4385. *p = 0;
  4386. p++;
  4387. break;
  4388. } else if (*p <= ' ') {
  4389. if (preload) {
  4390. zend_error(E_WARNING, "FFI: failed pre-loading '%s', bad FFI_LIB define", filename);
  4391. } else {
  4392. zend_throw_error(zend_ffi_exception_ce, "Failed loading '%s', bad FFI_LIB define", filename);
  4393. }
  4394. return NULL;
  4395. }
  4396. p++;
  4397. }
  4398. while (*p == ' ' || *p == '\t') {
  4399. p++;
  4400. }
  4401. while (*p == '\r' || *p == '\n') {
  4402. p++;
  4403. }
  4404. code_pos = p;
  4405. } else {
  4406. break;
  4407. }
  4408. }
  4409. return code_pos;
  4410. }
  4411. /* }}} */
  4412. static ZEND_COLD zend_function *zend_fake_get_constructor(zend_object *object) /* {{{ */
  4413. {
  4414. zend_throw_error(NULL, "Instantiation of %s is not allowed", ZSTR_VAL(object->ce->name));
  4415. return NULL;
  4416. }
  4417. /* }}} */
  4418. static ZEND_COLD zend_never_inline void zend_bad_array_access(zend_class_entry *ce) /* {{{ */
  4419. {
  4420. zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(ce->name));
  4421. }
  4422. /* }}} */
  4423. static ZEND_COLD zval *zend_fake_read_dimension(zend_object *obj, zval *offset, int type, zval *rv) /* {{{ */
  4424. {
  4425. zend_bad_array_access(obj->ce);
  4426. return NULL;
  4427. }
  4428. /* }}} */
  4429. static ZEND_COLD void zend_fake_write_dimension(zend_object *obj, zval *offset, zval *value) /* {{{ */
  4430. {
  4431. zend_bad_array_access(obj->ce);
  4432. }
  4433. /* }}} */
  4434. static ZEND_COLD int zend_fake_has_dimension(zend_object *obj, zval *offset, int check_empty) /* {{{ */
  4435. {
  4436. zend_bad_array_access(obj->ce);
  4437. return 0;
  4438. }
  4439. /* }}} */
  4440. static ZEND_COLD void zend_fake_unset_dimension(zend_object *obj, zval *offset) /* {{{ */
  4441. {
  4442. zend_bad_array_access(obj->ce);
  4443. }
  4444. /* }}} */
  4445. static ZEND_COLD zend_never_inline void zend_bad_property_access(zend_class_entry *ce) /* {{{ */
  4446. {
  4447. zend_throw_error(NULL, "Cannot access property of object of type %s", ZSTR_VAL(ce->name));
  4448. }
  4449. /* }}} */
  4450. static ZEND_COLD zval *zend_fake_read_property(zend_object *obj, zend_string *member, int type, void **cache_slot, zval *rv) /* {{{ */
  4451. {
  4452. zend_bad_property_access(obj->ce);
  4453. return &EG(uninitialized_zval);
  4454. }
  4455. /* }}} */
  4456. static ZEND_COLD zval *zend_fake_write_property(zend_object *obj, zend_string *member, zval *value, void **cache_slot) /* {{{ */
  4457. {
  4458. zend_bad_array_access(obj->ce);
  4459. return value;
  4460. }
  4461. /* }}} */
  4462. static ZEND_COLD int zend_fake_has_property(zend_object *obj, zend_string *member, int has_set_exists, void **cache_slot) /* {{{ */
  4463. {
  4464. zend_bad_array_access(obj->ce);
  4465. return 0;
  4466. }
  4467. /* }}} */
  4468. static ZEND_COLD void zend_fake_unset_property(zend_object *obj, zend_string *member, void **cache_slot) /* {{{ */
  4469. {
  4470. zend_bad_array_access(obj->ce);
  4471. }
  4472. /* }}} */
  4473. static zval *zend_fake_get_property_ptr_ptr(zend_object *obj, zend_string *member, int type, void **cache_slot) /* {{{ */
  4474. {
  4475. return NULL;
  4476. }
  4477. /* }}} */
  4478. static ZEND_COLD zend_function *zend_fake_get_method(zend_object **obj_ptr, zend_string *method_name, const zval *key) /* {{{ */
  4479. {
  4480. zend_class_entry *ce = (*obj_ptr)->ce;
  4481. zend_throw_error(NULL, "Object of type %s does not support method calls", ZSTR_VAL(ce->name));
  4482. return NULL;
  4483. }
  4484. /* }}} */
  4485. static HashTable *zend_fake_get_properties(zend_object *obj) /* {{{ */
  4486. {
  4487. return (HashTable*)&zend_empty_array;
  4488. }
  4489. /* }}} */
  4490. static HashTable *zend_fake_get_gc(zend_object *ob, zval **table, int *n) /* {{{ */
  4491. {
  4492. *table = NULL;
  4493. *n = 0;
  4494. return NULL;
  4495. }
  4496. /* }}} */
  4497. static int zend_fake_cast_object(zend_object *obj, zval *result, int type)
  4498. {
  4499. switch (type) {
  4500. case _IS_BOOL:
  4501. ZVAL_TRUE(result);
  4502. return SUCCESS;
  4503. default:
  4504. return FAILURE;
  4505. }
  4506. }
  4507. static ZEND_COLD zend_never_inline void zend_ffi_use_after_free(void) /* {{{ */
  4508. {
  4509. zend_throw_error(zend_ffi_exception_ce, "Use after free()");
  4510. }
  4511. /* }}} */
  4512. static zend_object *zend_ffi_free_clone_obj(zend_object *obj) /* {{{ */
  4513. {
  4514. zend_ffi_use_after_free();
  4515. return NULL;
  4516. }
  4517. /* }}} */
  4518. static ZEND_COLD zval *zend_ffi_free_read_dimension(zend_object *obj, zval *offset, int type, zval *rv) /* {{{ */
  4519. {
  4520. zend_ffi_use_after_free();
  4521. return NULL;
  4522. }
  4523. /* }}} */
  4524. static ZEND_COLD void zend_ffi_free_write_dimension(zend_object *obj, zval *offset, zval *value) /* {{{ */
  4525. {
  4526. zend_ffi_use_after_free();
  4527. }
  4528. /* }}} */
  4529. static ZEND_COLD int zend_ffi_free_has_dimension(zend_object *obj, zval *offset, int check_empty) /* {{{ */
  4530. {
  4531. zend_ffi_use_after_free();
  4532. return 0;
  4533. }
  4534. /* }}} */
  4535. static ZEND_COLD void zend_ffi_free_unset_dimension(zend_object *obj, zval *offset) /* {{{ */
  4536. {
  4537. zend_ffi_use_after_free();
  4538. }
  4539. /* }}} */
  4540. static ZEND_COLD zval *zend_ffi_free_read_property(zend_object *obj, zend_string *member, int type, void **cache_slot, zval *rv) /* {{{ */
  4541. {
  4542. zend_ffi_use_after_free();
  4543. return &EG(uninitialized_zval);
  4544. }
  4545. /* }}} */
  4546. static ZEND_COLD zval *zend_ffi_free_write_property(zend_object *obj, zend_string *member, zval *value, void **cache_slot) /* {{{ */
  4547. {
  4548. zend_ffi_use_after_free();
  4549. return value;
  4550. }
  4551. /* }}} */
  4552. static ZEND_COLD int zend_ffi_free_has_property(zend_object *obj, zend_string *member, int has_set_exists, void **cache_slot) /* {{{ */
  4553. {
  4554. zend_ffi_use_after_free();
  4555. return 0;
  4556. }
  4557. /* }}} */
  4558. static ZEND_COLD void zend_ffi_free_unset_property(zend_object *obj, zend_string *member, void **cache_slot) /* {{{ */
  4559. {
  4560. zend_ffi_use_after_free();
  4561. }
  4562. /* }}} */
  4563. static HashTable *zend_ffi_free_get_debug_info(zend_object *obj, int *is_temp) /* {{{ */
  4564. {
  4565. zend_ffi_use_after_free();
  4566. return NULL;
  4567. }
  4568. /* }}} */
  4569. static ZEND_INI_MH(OnUpdateFFIEnable) /* {{{ */
  4570. {
  4571. if (zend_string_equals_literal_ci(new_value, "preload")) {
  4572. FFI_G(restriction) = ZEND_FFI_PRELOAD;
  4573. } else {
  4574. FFI_G(restriction) = (zend_ffi_api_restriction)zend_ini_parse_bool(new_value);
  4575. }
  4576. return SUCCESS;
  4577. }
  4578. /* }}} */
  4579. static ZEND_INI_DISP(zend_ffi_enable_displayer_cb) /* {{{ */
  4580. {
  4581. if (FFI_G(restriction) == ZEND_FFI_PRELOAD) {
  4582. ZEND_PUTS("preload");
  4583. } else if (FFI_G(restriction) == ZEND_FFI_ENABLED) {
  4584. ZEND_PUTS("On");
  4585. } else {
  4586. ZEND_PUTS("Off");
  4587. }
  4588. }
  4589. /* }}} */
  4590. ZEND_INI_BEGIN()
  4591. ZEND_INI_ENTRY3_EX("ffi.enable", "preload", ZEND_INI_SYSTEM, OnUpdateFFIEnable, NULL, NULL, NULL, zend_ffi_enable_displayer_cb)
  4592. STD_ZEND_INI_ENTRY("ffi.preload", NULL, ZEND_INI_SYSTEM, OnUpdateString, preload, zend_ffi_globals, ffi_globals)
  4593. ZEND_INI_END()
  4594. static int zend_ffi_preload_glob(const char *filename) /* {{{ */
  4595. {
  4596. #ifdef HAVE_GLOB
  4597. glob_t globbuf;
  4598. int ret;
  4599. unsigned int i;
  4600. memset(&globbuf, 0, sizeof(glob_t));
  4601. ret = glob(filename, 0, NULL, &globbuf);
  4602. #ifdef GLOB_NOMATCH
  4603. if (ret == GLOB_NOMATCH || !globbuf.gl_pathc) {
  4604. #else
  4605. if (!globbuf.gl_pathc) {
  4606. #endif
  4607. /* pass */
  4608. } else {
  4609. for(i=0 ; i<globbuf.gl_pathc; i++) {
  4610. zend_ffi *ffi = zend_ffi_load(globbuf.gl_pathv[i], 1);
  4611. if (!ffi) {
  4612. globfree(&globbuf);
  4613. return FAILURE;
  4614. }
  4615. efree(ffi);
  4616. }
  4617. globfree(&globbuf);
  4618. }
  4619. #else
  4620. zend_ffi *ffi = zend_ffi_load(filename, 1);
  4621. if (!ffi) {
  4622. return FAILURE;
  4623. }
  4624. efree(ffi);
  4625. #endif
  4626. return SUCCESS;
  4627. }
  4628. /* }}} */
  4629. static int zend_ffi_preload(char *preload) /* {{{ */
  4630. {
  4631. zend_ffi *ffi;
  4632. char *s = NULL, *e, *filename;
  4633. bool is_glob = 0;
  4634. e = preload;
  4635. while (*e) {
  4636. switch (*e) {
  4637. case ZEND_PATHS_SEPARATOR:
  4638. if (s) {
  4639. filename = estrndup(s, e-s);
  4640. s = NULL;
  4641. if (!is_glob) {
  4642. ffi = zend_ffi_load(filename, 1);
  4643. efree(filename);
  4644. if (!ffi) {
  4645. return FAILURE;
  4646. }
  4647. efree(ffi);
  4648. } else {
  4649. int ret = zend_ffi_preload_glob(filename);
  4650. efree(filename);
  4651. if (ret != SUCCESS) {
  4652. return FAILURE;
  4653. }
  4654. is_glob = 0;
  4655. }
  4656. }
  4657. break;
  4658. case '*':
  4659. case '?':
  4660. case '[':
  4661. is_glob = 1;
  4662. break;
  4663. default:
  4664. if (!s) {
  4665. s = e;
  4666. }
  4667. break;
  4668. }
  4669. e++;
  4670. }
  4671. if (s) {
  4672. filename = estrndup(s, e-s);
  4673. if (!is_glob) {
  4674. ffi = zend_ffi_load(filename, 1);
  4675. efree(filename);
  4676. if (!ffi) {
  4677. return FAILURE;
  4678. }
  4679. efree(ffi);
  4680. } else {
  4681. int ret = zend_ffi_preload_glob(filename);
  4682. efree(filename);
  4683. if (ret != SUCCESS) {
  4684. return FAILURE;
  4685. }
  4686. }
  4687. }
  4688. return SUCCESS;
  4689. }
  4690. /* }}} */
  4691. #define REGISTER_FFI_TYPE_CONSTANT(name) \
  4692. zend_declare_class_constant_long(zend_ffi_ctype_ce, #name, sizeof(#name) - 1, ZEND_FFI_ ## name)
  4693. /* {{{ ZEND_MINIT_FUNCTION */
  4694. ZEND_MINIT_FUNCTION(ffi)
  4695. {
  4696. REGISTER_INI_ENTRIES();
  4697. FFI_G(is_cli) = strcmp(sapi_module.name, "cli") == 0;
  4698. zend_ffi_exception_ce = register_class_FFI_Exception(zend_ce_error);
  4699. zend_ffi_parser_exception_ce = register_class_FFI_ParserException(zend_ffi_exception_ce);
  4700. zend_ffi_ce = register_class_FFI();
  4701. zend_ffi_ce->create_object = zend_ffi_new;
  4702. memcpy(&zend_ffi_new_fn, zend_hash_str_find_ptr(&zend_ffi_ce->function_table, "new", sizeof("new")-1), sizeof(zend_internal_function));
  4703. zend_ffi_new_fn.fn_flags &= ~ZEND_ACC_STATIC;
  4704. memcpy(&zend_ffi_cast_fn, zend_hash_str_find_ptr(&zend_ffi_ce->function_table, "cast", sizeof("cast")-1), sizeof(zend_internal_function));
  4705. zend_ffi_cast_fn.fn_flags &= ~ZEND_ACC_STATIC;
  4706. memcpy(&zend_ffi_type_fn, zend_hash_str_find_ptr(&zend_ffi_ce->function_table, "type", sizeof("type")-1), sizeof(zend_internal_function));
  4707. zend_ffi_type_fn.fn_flags &= ~ZEND_ACC_STATIC;
  4708. memcpy(&zend_ffi_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  4709. zend_ffi_handlers.get_constructor = zend_fake_get_constructor;
  4710. zend_ffi_handlers.free_obj = zend_ffi_free_obj;
  4711. zend_ffi_handlers.clone_obj = NULL;
  4712. zend_ffi_handlers.read_property = zend_ffi_read_var;
  4713. zend_ffi_handlers.write_property = zend_ffi_write_var;
  4714. zend_ffi_handlers.read_dimension = zend_fake_read_dimension;
  4715. zend_ffi_handlers.write_dimension = zend_fake_write_dimension;
  4716. zend_ffi_handlers.get_property_ptr_ptr = zend_fake_get_property_ptr_ptr;
  4717. zend_ffi_handlers.has_property = zend_fake_has_property;
  4718. zend_ffi_handlers.unset_property = zend_fake_unset_property;
  4719. zend_ffi_handlers.has_dimension = zend_fake_has_dimension;
  4720. zend_ffi_handlers.unset_dimension = zend_fake_unset_dimension;
  4721. zend_ffi_handlers.get_method = zend_ffi_get_func;
  4722. zend_ffi_handlers.compare = NULL;
  4723. zend_ffi_handlers.cast_object = zend_fake_cast_object;
  4724. zend_ffi_handlers.get_debug_info = NULL;
  4725. zend_ffi_handlers.get_closure = NULL;
  4726. zend_ffi_handlers.get_properties = zend_fake_get_properties;
  4727. zend_ffi_handlers.get_gc = zend_fake_get_gc;
  4728. zend_declare_class_constant_long(zend_ffi_ce, "__BIGGEST_ALIGNMENT__", sizeof("__BIGGEST_ALIGNMENT__")-1, __BIGGEST_ALIGNMENT__);
  4729. zend_ffi_cdata_ce = register_class_FFI_CData();
  4730. zend_ffi_cdata_ce->create_object = zend_ffi_cdata_new;
  4731. zend_ffi_cdata_ce->get_iterator = zend_ffi_cdata_get_iterator;
  4732. memcpy(&zend_ffi_cdata_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  4733. zend_ffi_cdata_handlers.get_constructor = zend_fake_get_constructor;
  4734. zend_ffi_cdata_handlers.free_obj = zend_ffi_cdata_free_obj;
  4735. zend_ffi_cdata_handlers.clone_obj = zend_ffi_cdata_clone_obj;
  4736. zend_ffi_cdata_handlers.read_property = zend_ffi_cdata_read_field;
  4737. zend_ffi_cdata_handlers.write_property = zend_ffi_cdata_write_field;
  4738. zend_ffi_cdata_handlers.read_dimension = zend_ffi_cdata_read_dim;
  4739. zend_ffi_cdata_handlers.write_dimension = zend_ffi_cdata_write_dim;
  4740. zend_ffi_cdata_handlers.get_property_ptr_ptr = zend_fake_get_property_ptr_ptr;
  4741. zend_ffi_cdata_handlers.has_property = zend_fake_has_property;
  4742. zend_ffi_cdata_handlers.unset_property = zend_fake_unset_property;
  4743. zend_ffi_cdata_handlers.has_dimension = zend_fake_has_dimension;
  4744. zend_ffi_cdata_handlers.unset_dimension = zend_fake_unset_dimension;
  4745. zend_ffi_cdata_handlers.get_method = zend_fake_get_method;
  4746. zend_ffi_cdata_handlers.get_class_name = zend_ffi_cdata_get_class_name;
  4747. zend_ffi_cdata_handlers.do_operation = zend_ffi_cdata_do_operation;
  4748. zend_ffi_cdata_handlers.compare = zend_ffi_cdata_compare_objects;
  4749. zend_ffi_cdata_handlers.cast_object = zend_ffi_cdata_cast_object;
  4750. zend_ffi_cdata_handlers.count_elements = zend_ffi_cdata_count_elements;
  4751. zend_ffi_cdata_handlers.get_debug_info = zend_ffi_cdata_get_debug_info;
  4752. zend_ffi_cdata_handlers.get_closure = zend_ffi_cdata_get_closure;
  4753. zend_ffi_cdata_handlers.get_properties = zend_fake_get_properties;
  4754. zend_ffi_cdata_handlers.get_gc = zend_fake_get_gc;
  4755. memcpy(&zend_ffi_cdata_value_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  4756. zend_ffi_cdata_value_handlers.get_constructor = zend_fake_get_constructor;
  4757. zend_ffi_cdata_value_handlers.free_obj = zend_ffi_cdata_free_obj;
  4758. zend_ffi_cdata_value_handlers.clone_obj = zend_ffi_cdata_clone_obj;
  4759. zend_ffi_cdata_value_handlers.read_property = zend_ffi_cdata_get;
  4760. zend_ffi_cdata_value_handlers.write_property = zend_ffi_cdata_set;
  4761. zend_ffi_cdata_value_handlers.read_dimension = zend_fake_read_dimension;
  4762. zend_ffi_cdata_value_handlers.write_dimension = zend_fake_write_dimension;
  4763. zend_ffi_cdata_value_handlers.get_property_ptr_ptr = zend_fake_get_property_ptr_ptr;
  4764. zend_ffi_cdata_value_handlers.has_property = zend_fake_has_property;
  4765. zend_ffi_cdata_value_handlers.unset_property = zend_fake_unset_property;
  4766. zend_ffi_cdata_value_handlers.has_dimension = zend_fake_has_dimension;
  4767. zend_ffi_cdata_value_handlers.unset_dimension = zend_fake_unset_dimension;
  4768. zend_ffi_cdata_value_handlers.get_method = zend_fake_get_method;
  4769. zend_ffi_cdata_value_handlers.get_class_name = zend_ffi_cdata_get_class_name;
  4770. zend_ffi_cdata_value_handlers.compare = zend_ffi_cdata_compare_objects;
  4771. zend_ffi_cdata_value_handlers.cast_object = zend_ffi_cdata_cast_object;
  4772. zend_ffi_cdata_value_handlers.count_elements = NULL;
  4773. zend_ffi_cdata_value_handlers.get_debug_info = zend_ffi_cdata_get_debug_info;
  4774. zend_ffi_cdata_value_handlers.get_closure = NULL;
  4775. zend_ffi_cdata_value_handlers.get_properties = zend_fake_get_properties;
  4776. zend_ffi_cdata_value_handlers.get_gc = zend_fake_get_gc;
  4777. memcpy(&zend_ffi_cdata_free_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  4778. zend_ffi_cdata_free_handlers.get_constructor = zend_fake_get_constructor;
  4779. zend_ffi_cdata_free_handlers.free_obj = zend_ffi_cdata_free_obj;
  4780. zend_ffi_cdata_free_handlers.clone_obj = zend_ffi_free_clone_obj;
  4781. zend_ffi_cdata_free_handlers.read_property = zend_ffi_free_read_property;
  4782. zend_ffi_cdata_free_handlers.write_property = zend_ffi_free_write_property;
  4783. zend_ffi_cdata_free_handlers.read_dimension = zend_ffi_free_read_dimension;
  4784. zend_ffi_cdata_free_handlers.write_dimension = zend_ffi_free_write_dimension;
  4785. zend_ffi_cdata_free_handlers.get_property_ptr_ptr = zend_fake_get_property_ptr_ptr;
  4786. zend_ffi_cdata_free_handlers.has_property = zend_ffi_free_has_property;
  4787. zend_ffi_cdata_free_handlers.unset_property = zend_ffi_free_unset_property;
  4788. zend_ffi_cdata_free_handlers.has_dimension = zend_ffi_free_has_dimension;
  4789. zend_ffi_cdata_free_handlers.unset_dimension = zend_ffi_free_unset_dimension;
  4790. zend_ffi_cdata_free_handlers.get_method = zend_fake_get_method;
  4791. zend_ffi_cdata_free_handlers.get_class_name = zend_ffi_cdata_get_class_name;
  4792. zend_ffi_cdata_free_handlers.compare = zend_ffi_cdata_compare_objects;
  4793. zend_ffi_cdata_free_handlers.cast_object = zend_fake_cast_object;
  4794. zend_ffi_cdata_free_handlers.count_elements = NULL;
  4795. zend_ffi_cdata_free_handlers.get_debug_info = zend_ffi_free_get_debug_info;
  4796. zend_ffi_cdata_free_handlers.get_closure = NULL;
  4797. zend_ffi_cdata_free_handlers.get_properties = zend_fake_get_properties;
  4798. zend_ffi_cdata_free_handlers.get_gc = zend_fake_get_gc;
  4799. zend_ffi_ctype_ce = register_class_FFI_CType();
  4800. zend_ffi_ctype_ce->create_object = zend_ffi_ctype_new;
  4801. memcpy(&zend_ffi_ctype_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
  4802. zend_ffi_ctype_handlers.get_constructor = zend_fake_get_constructor;
  4803. zend_ffi_ctype_handlers.free_obj = zend_ffi_ctype_free_obj;
  4804. zend_ffi_ctype_handlers.clone_obj = NULL;
  4805. zend_ffi_ctype_handlers.read_property = zend_fake_read_property;
  4806. zend_ffi_ctype_handlers.write_property = zend_fake_write_property;
  4807. zend_ffi_ctype_handlers.read_dimension = zend_fake_read_dimension;
  4808. zend_ffi_ctype_handlers.write_dimension = zend_fake_write_dimension;
  4809. zend_ffi_ctype_handlers.get_property_ptr_ptr = zend_fake_get_property_ptr_ptr;
  4810. zend_ffi_ctype_handlers.has_property = zend_fake_has_property;
  4811. zend_ffi_ctype_handlers.unset_property = zend_fake_unset_property;
  4812. zend_ffi_ctype_handlers.has_dimension = zend_fake_has_dimension;
  4813. zend_ffi_ctype_handlers.unset_dimension = zend_fake_unset_dimension;
  4814. //zend_ffi_ctype_handlers.get_method = zend_fake_get_method;
  4815. zend_ffi_ctype_handlers.get_class_name = zend_ffi_ctype_get_class_name;
  4816. zend_ffi_ctype_handlers.compare = zend_ffi_ctype_compare_objects;
  4817. zend_ffi_ctype_handlers.cast_object = zend_fake_cast_object;
  4818. zend_ffi_ctype_handlers.count_elements = NULL;
  4819. zend_ffi_ctype_handlers.get_debug_info = zend_ffi_ctype_get_debug_info;
  4820. zend_ffi_ctype_handlers.get_closure = NULL;
  4821. zend_ffi_ctype_handlers.get_properties = zend_fake_get_properties;
  4822. zend_ffi_ctype_handlers.get_gc = zend_fake_get_gc;
  4823. REGISTER_FFI_TYPE_CONSTANT(TYPE_VOID);
  4824. REGISTER_FFI_TYPE_CONSTANT(TYPE_FLOAT);
  4825. REGISTER_FFI_TYPE_CONSTANT(TYPE_DOUBLE);
  4826. #ifdef HAVE_LONG_DOUBLE
  4827. REGISTER_FFI_TYPE_CONSTANT(TYPE_LONGDOUBLE);
  4828. #endif
  4829. REGISTER_FFI_TYPE_CONSTANT(TYPE_UINT8);
  4830. REGISTER_FFI_TYPE_CONSTANT(TYPE_SINT8);
  4831. REGISTER_FFI_TYPE_CONSTANT(TYPE_UINT16);
  4832. REGISTER_FFI_TYPE_CONSTANT(TYPE_SINT16);
  4833. REGISTER_FFI_TYPE_CONSTANT(TYPE_UINT32);
  4834. REGISTER_FFI_TYPE_CONSTANT(TYPE_SINT32);
  4835. REGISTER_FFI_TYPE_CONSTANT(TYPE_UINT64);
  4836. REGISTER_FFI_TYPE_CONSTANT(TYPE_SINT64);
  4837. REGISTER_FFI_TYPE_CONSTANT(TYPE_ENUM);
  4838. REGISTER_FFI_TYPE_CONSTANT(TYPE_BOOL);
  4839. REGISTER_FFI_TYPE_CONSTANT(TYPE_CHAR);
  4840. REGISTER_FFI_TYPE_CONSTANT(TYPE_POINTER);
  4841. REGISTER_FFI_TYPE_CONSTANT(TYPE_FUNC);
  4842. REGISTER_FFI_TYPE_CONSTANT(TYPE_ARRAY);
  4843. REGISTER_FFI_TYPE_CONSTANT(TYPE_STRUCT);
  4844. REGISTER_FFI_TYPE_CONSTANT(ATTR_CONST);
  4845. REGISTER_FFI_TYPE_CONSTANT(ATTR_INCOMPLETE_TAG);
  4846. REGISTER_FFI_TYPE_CONSTANT(ATTR_VARIADIC);
  4847. REGISTER_FFI_TYPE_CONSTANT(ATTR_INCOMPLETE_ARRAY);
  4848. REGISTER_FFI_TYPE_CONSTANT(ATTR_VLA);
  4849. REGISTER_FFI_TYPE_CONSTANT(ATTR_UNION);
  4850. REGISTER_FFI_TYPE_CONSTANT(ATTR_PACKED);
  4851. REGISTER_FFI_TYPE_CONSTANT(ATTR_MS_STRUCT);
  4852. REGISTER_FFI_TYPE_CONSTANT(ATTR_GCC_STRUCT);
  4853. REGISTER_FFI_TYPE_CONSTANT(ABI_DEFAULT);
  4854. REGISTER_FFI_TYPE_CONSTANT(ABI_CDECL);
  4855. REGISTER_FFI_TYPE_CONSTANT(ABI_FASTCALL);
  4856. REGISTER_FFI_TYPE_CONSTANT(ABI_THISCALL);
  4857. REGISTER_FFI_TYPE_CONSTANT(ABI_STDCALL);
  4858. REGISTER_FFI_TYPE_CONSTANT(ABI_PASCAL);
  4859. REGISTER_FFI_TYPE_CONSTANT(ABI_REGISTER);
  4860. REGISTER_FFI_TYPE_CONSTANT(ABI_MS);
  4861. REGISTER_FFI_TYPE_CONSTANT(ABI_SYSV);
  4862. REGISTER_FFI_TYPE_CONSTANT(ABI_VECTORCALL);
  4863. if (FFI_G(preload)) {
  4864. if (zend_ffi_preload(FFI_G(preload)) != SUCCESS) {
  4865. return FAILURE;
  4866. }
  4867. }
  4868. return SUCCESS;
  4869. }
  4870. /* }}} */
  4871. /* {{{ ZEND_RSHUTDOWN_FUNCTION */
  4872. ZEND_RSHUTDOWN_FUNCTION(ffi)
  4873. {
  4874. if (FFI_G(callbacks)) {
  4875. zend_hash_destroy(FFI_G(callbacks));
  4876. efree(FFI_G(callbacks));
  4877. FFI_G(callbacks) = NULL;
  4878. }
  4879. if (FFI_G(weak_types)) {
  4880. #if 0
  4881. fprintf(stderr, "WeakTypes: %d\n", zend_hash_num_elements(FFI_G(weak_types)));
  4882. #endif
  4883. zend_hash_destroy(FFI_G(weak_types));
  4884. efree(FFI_G(weak_types));
  4885. FFI_G(weak_types) = NULL;
  4886. }
  4887. return SUCCESS;
  4888. }
  4889. /* }}} */
  4890. /* {{{ ZEND_MINFO_FUNCTION */
  4891. ZEND_MINFO_FUNCTION(ffi)
  4892. {
  4893. php_info_print_table_start();
  4894. php_info_print_table_header(2, "FFI support", "enabled");
  4895. php_info_print_table_end();
  4896. DISPLAY_INI_ENTRIES();
  4897. }
  4898. /* }}} */
  4899. static const zend_ffi_type zend_ffi_type_void = {.kind=ZEND_FFI_TYPE_VOID, .size=1, .align=1};
  4900. static const zend_ffi_type zend_ffi_type_char = {.kind=ZEND_FFI_TYPE_CHAR, .size=1, .align=_Alignof(char)};
  4901. static const zend_ffi_type zend_ffi_type_bool = {.kind=ZEND_FFI_TYPE_BOOL, .size=1, .align=_Alignof(uint8_t)};
  4902. static const zend_ffi_type zend_ffi_type_sint8 = {.kind=ZEND_FFI_TYPE_SINT8, .size=1, .align=_Alignof(int8_t)};
  4903. static const zend_ffi_type zend_ffi_type_uint8 = {.kind=ZEND_FFI_TYPE_UINT8, .size=1, .align=_Alignof(uint8_t)};
  4904. static const zend_ffi_type zend_ffi_type_sint16 = {.kind=ZEND_FFI_TYPE_SINT16, .size=2, .align=_Alignof(int16_t)};
  4905. static const zend_ffi_type zend_ffi_type_uint16 = {.kind=ZEND_FFI_TYPE_UINT16, .size=2, .align=_Alignof(uint16_t)};
  4906. static const zend_ffi_type zend_ffi_type_sint32 = {.kind=ZEND_FFI_TYPE_SINT32, .size=4, .align=_Alignof(int32_t)};
  4907. static const zend_ffi_type zend_ffi_type_uint32 = {.kind=ZEND_FFI_TYPE_UINT32, .size=4, .align=_Alignof(uint32_t)};
  4908. static const zend_ffi_type zend_ffi_type_sint64 = {.kind=ZEND_FFI_TYPE_SINT64, .size=8, .align=_Alignof(int64_t)};
  4909. static const zend_ffi_type zend_ffi_type_uint64 = {.kind=ZEND_FFI_TYPE_UINT64, .size=8, .align=_Alignof(uint64_t)};
  4910. static const zend_ffi_type zend_ffi_type_float = {.kind=ZEND_FFI_TYPE_FLOAT, .size=sizeof(float), .align=_Alignof(float)};
  4911. static const zend_ffi_type zend_ffi_type_double = {.kind=ZEND_FFI_TYPE_DOUBLE, .size=sizeof(double), .align=_Alignof(double)};
  4912. #ifdef HAVE_LONG_DOUBLE
  4913. static const zend_ffi_type zend_ffi_type_long_double = {.kind=ZEND_FFI_TYPE_LONGDOUBLE, .size=sizeof(long double), .align=_Alignof(long double)};
  4914. #endif
  4915. static const zend_ffi_type zend_ffi_type_ptr = {.kind=ZEND_FFI_TYPE_POINTER, .size=sizeof(void*), .align=_Alignof(void*), .pointer.type = (zend_ffi_type*)&zend_ffi_type_void};
  4916. const struct {
  4917. const char *name;
  4918. const zend_ffi_type *type;
  4919. } zend_ffi_types[] = {
  4920. {"void", &zend_ffi_type_void},
  4921. {"char", &zend_ffi_type_char},
  4922. {"bool", &zend_ffi_type_bool},
  4923. {"int8_t", &zend_ffi_type_sint8},
  4924. {"uint8_t", &zend_ffi_type_uint8},
  4925. {"int16_t", &zend_ffi_type_sint16},
  4926. {"uint16_t", &zend_ffi_type_uint16},
  4927. {"int32_t", &zend_ffi_type_sint32},
  4928. {"uint32_t", &zend_ffi_type_uint32},
  4929. {"int64_t", &zend_ffi_type_sint64},
  4930. {"uint64_t", &zend_ffi_type_uint64},
  4931. {"float", &zend_ffi_type_float},
  4932. {"double", &zend_ffi_type_double},
  4933. #ifdef HAVE_LONG_DOUBLE
  4934. {"long double", &zend_ffi_type_long_double},
  4935. #endif
  4936. #if SIZEOF_SIZE_T == 4
  4937. {"uintptr_t", &zend_ffi_type_uint32},
  4938. {"intptr_t", &zend_ffi_type_sint32},
  4939. {"size_t", &zend_ffi_type_uint32},
  4940. {"ssize_t", &zend_ffi_type_sint32},
  4941. {"ptrdiff_t", &zend_ffi_type_sint32},
  4942. #else
  4943. {"uintptr_t", &zend_ffi_type_uint64},
  4944. {"intptr_t", &zend_ffi_type_sint64},
  4945. {"size_t", &zend_ffi_type_uint64},
  4946. {"ssize_t", &zend_ffi_type_sint64},
  4947. {"ptrdiff_t", &zend_ffi_type_sint64},
  4948. #endif
  4949. #if SIZEOF_OFF_T == 4
  4950. {"off_t", &zend_ffi_type_sint32},
  4951. #else
  4952. {"off_t", &zend_ffi_type_sint64},
  4953. #endif
  4954. {"va_list", &zend_ffi_type_ptr},
  4955. {"__builtin_va_list", &zend_ffi_type_ptr},
  4956. {"__gnuc_va_list", &zend_ffi_type_ptr},
  4957. };
  4958. /* {{{ ZEND_GINIT_FUNCTION */
  4959. static ZEND_GINIT_FUNCTION(ffi)
  4960. {
  4961. size_t i;
  4962. #if defined(COMPILE_DL_FFI) && defined(ZTS)
  4963. ZEND_TSRMLS_CACHE_UPDATE();
  4964. #endif
  4965. memset(ffi_globals, 0, sizeof(*ffi_globals));
  4966. zend_hash_init(&ffi_globals->types, 0, NULL, NULL, 1);
  4967. for (i = 0; i < sizeof(zend_ffi_types)/sizeof(zend_ffi_types[0]); i++) {
  4968. zend_hash_str_add_new_ptr(&ffi_globals->types, zend_ffi_types[i].name, strlen(zend_ffi_types[i].name), (void*)zend_ffi_types[i].type);
  4969. }
  4970. }
  4971. /* }}} */
  4972. /* {{{ ZEND_GINIT_FUNCTION */
  4973. static ZEND_GSHUTDOWN_FUNCTION(ffi)
  4974. {
  4975. if (ffi_globals->scopes) {
  4976. zend_hash_destroy(ffi_globals->scopes);
  4977. free(ffi_globals->scopes);
  4978. }
  4979. zend_hash_destroy(&ffi_globals->types);
  4980. }
  4981. /* }}} */
  4982. /* {{{ ffi_module_entry */
  4983. zend_module_entry ffi_module_entry = {
  4984. STANDARD_MODULE_HEADER,
  4985. "FFI", /* Extension name */
  4986. NULL, /* zend_function_entry */
  4987. ZEND_MINIT(ffi), /* ZEND_MINIT - Module initialization */
  4988. NULL, /* ZEND_MSHUTDOWN - Module shutdown */
  4989. NULL, /* ZEND_RINIT - Request initialization */
  4990. ZEND_RSHUTDOWN(ffi), /* ZEND_RSHUTDOWN - Request shutdown */
  4991. ZEND_MINFO(ffi), /* ZEND_MINFO - Module info */
  4992. PHP_VERSION, /* Version */
  4993. ZEND_MODULE_GLOBALS(ffi),
  4994. ZEND_GINIT(ffi),
  4995. ZEND_GSHUTDOWN(ffi),
  4996. NULL,
  4997. STANDARD_MODULE_PROPERTIES_EX
  4998. };
  4999. /* }}} */
  5000. #ifdef COMPILE_DL_FFI
  5001. # ifdef ZTS
  5002. ZEND_TSRMLS_CACHE_DEFINE()
  5003. # endif
  5004. ZEND_GET_MODULE(ffi)
  5005. #endif
  5006. /* parser callbacks */
  5007. void zend_ffi_parser_error(const char *format, ...) /* {{{ */
  5008. {
  5009. va_list va;
  5010. char *message = NULL;
  5011. va_start(va, format);
  5012. zend_vspprintf(&message, 0, format, va);
  5013. if (EG(current_execute_data)) {
  5014. zend_throw_exception(zend_ffi_parser_exception_ce, message, 0);
  5015. } else {
  5016. zend_error(E_WARNING, "FFI Parser: %s", message);
  5017. }
  5018. efree(message);
  5019. va_end(va);
  5020. LONGJMP(FFI_G(bailout), FAILURE);
  5021. }
  5022. /* }}} */
  5023. static void zend_ffi_finalize_type(zend_ffi_dcl *dcl) /* {{{ */
  5024. {
  5025. if (!dcl->type) {
  5026. switch (dcl->flags & ZEND_FFI_DCL_TYPE_SPECIFIERS) {
  5027. case ZEND_FFI_DCL_VOID:
  5028. dcl->type = (zend_ffi_type*)&zend_ffi_type_void;
  5029. break;
  5030. case ZEND_FFI_DCL_CHAR:
  5031. dcl->type = (zend_ffi_type*)&zend_ffi_type_char;
  5032. break;
  5033. case ZEND_FFI_DCL_CHAR|ZEND_FFI_DCL_SIGNED:
  5034. dcl->type = (zend_ffi_type*)&zend_ffi_type_sint8;
  5035. break;
  5036. case ZEND_FFI_DCL_CHAR|ZEND_FFI_DCL_UNSIGNED:
  5037. case ZEND_FFI_DCL_BOOL:
  5038. dcl->type = (zend_ffi_type*)&zend_ffi_type_uint8;
  5039. break;
  5040. case ZEND_FFI_DCL_SHORT:
  5041. case ZEND_FFI_DCL_SHORT|ZEND_FFI_DCL_SIGNED:
  5042. case ZEND_FFI_DCL_SHORT|ZEND_FFI_DCL_INT:
  5043. case ZEND_FFI_DCL_SHORT|ZEND_FFI_DCL_SIGNED|ZEND_FFI_DCL_INT:
  5044. dcl->type = (zend_ffi_type*)&zend_ffi_type_sint16;
  5045. break;
  5046. case ZEND_FFI_DCL_SHORT|ZEND_FFI_DCL_UNSIGNED:
  5047. case ZEND_FFI_DCL_SHORT|ZEND_FFI_DCL_UNSIGNED|ZEND_FFI_DCL_INT:
  5048. dcl->type = (zend_ffi_type*)&zend_ffi_type_uint16;
  5049. break;
  5050. case ZEND_FFI_DCL_INT:
  5051. case ZEND_FFI_DCL_SIGNED:
  5052. case ZEND_FFI_DCL_SIGNED|ZEND_FFI_DCL_INT:
  5053. dcl->type = (zend_ffi_type*)&zend_ffi_type_sint32;
  5054. break;
  5055. case ZEND_FFI_DCL_UNSIGNED:
  5056. case ZEND_FFI_DCL_UNSIGNED|ZEND_FFI_DCL_INT:
  5057. dcl->type = (zend_ffi_type*)&zend_ffi_type_uint32;
  5058. break;
  5059. case ZEND_FFI_DCL_LONG:
  5060. case ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_SIGNED:
  5061. case ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_INT:
  5062. case ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_SIGNED|ZEND_FFI_DCL_INT:
  5063. if (sizeof(long) == 4) {
  5064. dcl->type = (zend_ffi_type*)&zend_ffi_type_sint32;
  5065. } else {
  5066. dcl->type = (zend_ffi_type*)&zend_ffi_type_sint64;
  5067. }
  5068. break;
  5069. case ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_UNSIGNED:
  5070. case ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_UNSIGNED|ZEND_FFI_DCL_INT:
  5071. if (sizeof(long) == 4) {
  5072. dcl->type = (zend_ffi_type*)&zend_ffi_type_uint32;
  5073. } else {
  5074. dcl->type = (zend_ffi_type*)&zend_ffi_type_uint64;
  5075. }
  5076. break;
  5077. case ZEND_FFI_DCL_LONG_LONG|ZEND_FFI_DCL_LONG:
  5078. case ZEND_FFI_DCL_LONG_LONG|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_SIGNED:
  5079. case ZEND_FFI_DCL_LONG_LONG|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_INT:
  5080. case ZEND_FFI_DCL_LONG_LONG|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_SIGNED|ZEND_FFI_DCL_INT:
  5081. dcl->type = (zend_ffi_type*)&zend_ffi_type_sint64;
  5082. break;
  5083. case ZEND_FFI_DCL_LONG_LONG|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_UNSIGNED:
  5084. case ZEND_FFI_DCL_LONG_LONG|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_UNSIGNED|ZEND_FFI_DCL_INT:
  5085. dcl->type = (zend_ffi_type*)&zend_ffi_type_uint64;
  5086. break;
  5087. case ZEND_FFI_DCL_FLOAT:
  5088. dcl->type = (zend_ffi_type*)&zend_ffi_type_float;
  5089. break;
  5090. case ZEND_FFI_DCL_DOUBLE:
  5091. dcl->type = (zend_ffi_type*)&zend_ffi_type_double;
  5092. break;
  5093. case ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_DOUBLE:
  5094. #ifdef _WIN32
  5095. dcl->type = (zend_ffi_type*)&zend_ffi_type_double;
  5096. #else
  5097. dcl->type = (zend_ffi_type*)&zend_ffi_type_long_double;
  5098. #endif
  5099. break;
  5100. case ZEND_FFI_DCL_FLOAT|ZEND_FFI_DCL_COMPLEX:
  5101. case ZEND_FFI_DCL_DOUBLE|ZEND_FFI_DCL_COMPLEX:
  5102. case ZEND_FFI_DCL_DOUBLE|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_COMPLEX:
  5103. zend_ffi_parser_error("Unsupported type _Complex at line %d", FFI_G(line));
  5104. break;
  5105. default:
  5106. zend_ffi_parser_error("Unsupported type specifier combination at line %d", FFI_G(line));
  5107. break;
  5108. }
  5109. dcl->flags &= ~ZEND_FFI_DCL_TYPE_SPECIFIERS;
  5110. dcl->flags |= ZEND_FFI_DCL_TYPEDEF_NAME;
  5111. }
  5112. }
  5113. /* }}} */
  5114. int zend_ffi_is_typedef_name(const char *name, size_t name_len) /* {{{ */
  5115. {
  5116. zend_ffi_symbol *sym;
  5117. zend_ffi_type *type;
  5118. if (FFI_G(symbols)) {
  5119. sym = zend_hash_str_find_ptr(FFI_G(symbols), name, name_len);
  5120. if (sym) {
  5121. return (sym->kind == ZEND_FFI_SYM_TYPE);
  5122. }
  5123. }
  5124. type = zend_hash_str_find_ptr(&FFI_G(types), name, name_len);
  5125. if (type) {
  5126. return 1;
  5127. }
  5128. return 0;
  5129. }
  5130. /* }}} */
  5131. void zend_ffi_resolve_typedef(const char *name, size_t name_len, zend_ffi_dcl *dcl) /* {{{ */
  5132. {
  5133. zend_ffi_symbol *sym;
  5134. zend_ffi_type *type;
  5135. if (FFI_G(symbols)) {
  5136. sym = zend_hash_str_find_ptr(FFI_G(symbols), name, name_len);
  5137. if (sym && sym->kind == ZEND_FFI_SYM_TYPE) {
  5138. dcl->type = ZEND_FFI_TYPE(sym->type);;
  5139. if (sym->is_const) {
  5140. dcl->attr |= ZEND_FFI_ATTR_CONST;
  5141. }
  5142. return;
  5143. }
  5144. }
  5145. type = zend_hash_str_find_ptr(&FFI_G(types), name, name_len);
  5146. if (type) {
  5147. dcl->type = type;
  5148. return;
  5149. }
  5150. zend_ffi_parser_error("Undefined C type \"%.*s\" at line %d", name_len, name, FFI_G(line));
  5151. }
  5152. /* }}} */
  5153. void zend_ffi_resolve_const(const char *name, size_t name_len, zend_ffi_val *val) /* {{{ */
  5154. {
  5155. zend_ffi_symbol *sym;
  5156. if (UNEXPECTED(FFI_G(attribute_parsing))) {
  5157. val->kind = ZEND_FFI_VAL_NAME;
  5158. val->str = name;
  5159. val->len = name_len;
  5160. return;
  5161. } else if (FFI_G(symbols)) {
  5162. sym = zend_hash_str_find_ptr(FFI_G(symbols), name, name_len);
  5163. if (sym && sym->kind == ZEND_FFI_SYM_CONST) {
  5164. val->i64 = sym->value;
  5165. switch (sym->type->kind) {
  5166. case ZEND_FFI_TYPE_SINT8:
  5167. case ZEND_FFI_TYPE_SINT16:
  5168. case ZEND_FFI_TYPE_SINT32:
  5169. val->kind = ZEND_FFI_VAL_INT32;
  5170. break;
  5171. case ZEND_FFI_TYPE_SINT64:
  5172. val->kind = ZEND_FFI_VAL_INT64;
  5173. break;
  5174. case ZEND_FFI_TYPE_UINT8:
  5175. case ZEND_FFI_TYPE_UINT16:
  5176. case ZEND_FFI_TYPE_UINT32:
  5177. val->kind = ZEND_FFI_VAL_UINT32;
  5178. break;
  5179. case ZEND_FFI_TYPE_UINT64:
  5180. val->kind = ZEND_FFI_VAL_UINT64;
  5181. break;
  5182. default:
  5183. ZEND_UNREACHABLE();
  5184. }
  5185. return;
  5186. }
  5187. }
  5188. val->kind = ZEND_FFI_VAL_ERROR;
  5189. }
  5190. /* }}} */
  5191. void zend_ffi_make_enum_type(zend_ffi_dcl *dcl) /* {{{ */
  5192. {
  5193. zend_ffi_type *type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
  5194. type->kind = ZEND_FFI_TYPE_ENUM;
  5195. type->attr = FFI_G(default_type_attr) | (dcl->attr & ZEND_FFI_ENUM_ATTRS);
  5196. type->enumeration.tag_name = NULL;
  5197. if (type->attr & ZEND_FFI_ATTR_PACKED) {
  5198. type->size = zend_ffi_type_uint8.size;
  5199. type->align = zend_ffi_type_uint8.align;
  5200. type->enumeration.kind = ZEND_FFI_TYPE_UINT8;
  5201. } else {
  5202. type->size = zend_ffi_type_uint32.size;
  5203. type->align = zend_ffi_type_uint32.align;
  5204. type->enumeration.kind = ZEND_FFI_TYPE_UINT32;
  5205. }
  5206. dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(type);
  5207. dcl->attr &= ~ZEND_FFI_ENUM_ATTRS;
  5208. }
  5209. /* }}} */
  5210. void zend_ffi_add_enum_val(zend_ffi_dcl *enum_dcl, const char *name, size_t name_len, zend_ffi_val *val, int64_t *min, int64_t *max, int64_t *last) /* {{{ */
  5211. {
  5212. zend_ffi_symbol *sym;
  5213. const zend_ffi_type *sym_type;
  5214. int64_t value;
  5215. zend_ffi_type *enum_type = ZEND_FFI_TYPE(enum_dcl->type);
  5216. bool overflow = 0;
  5217. bool is_signed =
  5218. (enum_type->enumeration.kind == ZEND_FFI_TYPE_SINT8 ||
  5219. enum_type->enumeration.kind == ZEND_FFI_TYPE_SINT16 ||
  5220. enum_type->enumeration.kind == ZEND_FFI_TYPE_SINT32 ||
  5221. enum_type->enumeration.kind == ZEND_FFI_TYPE_SINT64);
  5222. ZEND_ASSERT(enum_type && enum_type->kind == ZEND_FFI_TYPE_ENUM);
  5223. if (val->kind == ZEND_FFI_VAL_EMPTY) {
  5224. if (is_signed) {
  5225. if (*last == 0x7FFFFFFFFFFFFFFFLL) {
  5226. overflow = 1;
  5227. }
  5228. } else {
  5229. if ((*min != 0 || *max != 0)
  5230. && (uint64_t)*last == 0xFFFFFFFFFFFFFFFFULL) {
  5231. overflow = 1;
  5232. }
  5233. }
  5234. value = *last + 1;
  5235. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  5236. if (!is_signed && val->ch < 0) {
  5237. if ((uint64_t)*max > 0x7FFFFFFFFFFFFFFFULL) {
  5238. overflow = 1;
  5239. } else {
  5240. is_signed = 1;
  5241. }
  5242. }
  5243. value = val->ch;
  5244. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  5245. if (!is_signed && val->i64 < 0) {
  5246. if ((uint64_t)*max > 0x7FFFFFFFFFFFFFFFULL) {
  5247. overflow = 1;
  5248. } else {
  5249. is_signed = 1;
  5250. }
  5251. }
  5252. value = val->i64;
  5253. } else if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) {
  5254. if (is_signed && val->u64 > 0x7FFFFFFFFFFFFFFFULL) {
  5255. overflow = 1;
  5256. }
  5257. value = val->u64;
  5258. } else {
  5259. zend_ffi_parser_error("Enumerator value \"%.*s\" must be an integer at line %d", name_len, name, FFI_G(line));
  5260. return;
  5261. }
  5262. if (overflow) {
  5263. zend_ffi_parser_error("Overflow in enumeration values \"%.*s\" at line %d", name_len, name, FFI_G(line));
  5264. return;
  5265. }
  5266. if (is_signed) {
  5267. *min = MIN(*min, value);
  5268. *max = MAX(*max, value);
  5269. if ((enum_type->attr & ZEND_FFI_ATTR_PACKED)
  5270. && *min >= -0x7FLL-1 && *max <= 0x7FLL) {
  5271. sym_type = &zend_ffi_type_sint8;
  5272. } else if ((enum_type->attr & ZEND_FFI_ATTR_PACKED)
  5273. && *min >= -0x7FFFLL-1 && *max <= 0x7FFFLL) {
  5274. sym_type = &zend_ffi_type_sint16;
  5275. } else if (*min >= -0x7FFFFFFFLL-1 && *max <= 0x7FFFFFFFLL) {
  5276. sym_type = &zend_ffi_type_sint32;
  5277. } else {
  5278. sym_type = &zend_ffi_type_sint64;
  5279. }
  5280. } else {
  5281. *min = MIN((uint64_t)*min, (uint64_t)value);
  5282. *max = MAX((uint64_t)*max, (uint64_t)value);
  5283. if ((enum_type->attr & ZEND_FFI_ATTR_PACKED)
  5284. && (uint64_t)*max <= 0xFFULL) {
  5285. sym_type = &zend_ffi_type_uint8;
  5286. } else if ((enum_type->attr & ZEND_FFI_ATTR_PACKED)
  5287. && (uint64_t)*max <= 0xFFFFULL) {
  5288. sym_type = &zend_ffi_type_uint16;
  5289. } else if ((uint64_t)*max <= 0xFFFFFFFFULL) {
  5290. sym_type = &zend_ffi_type_uint32;
  5291. } else {
  5292. sym_type = &zend_ffi_type_uint64;
  5293. }
  5294. }
  5295. enum_type->enumeration.kind = sym_type->kind;
  5296. enum_type->size = sym_type->size;
  5297. enum_type->align = sym_type->align;
  5298. *last = value;
  5299. if (!FFI_G(symbols)) {
  5300. FFI_G(symbols) = pemalloc(sizeof(HashTable), FFI_G(persistent));
  5301. zend_hash_init(FFI_G(symbols), 0, NULL, FFI_G(persistent) ? zend_ffi_symbol_hash_persistent_dtor : zend_ffi_symbol_hash_dtor, FFI_G(persistent));
  5302. }
  5303. sym = zend_hash_str_find_ptr(FFI_G(symbols), name, name_len);
  5304. if (sym) {
  5305. zend_ffi_parser_error("Redeclaration of \"%.*s\" at line %d", name_len, name, FFI_G(line));
  5306. } else {
  5307. sym = pemalloc(sizeof(zend_ffi_symbol), FFI_G(persistent));
  5308. sym->kind = ZEND_FFI_SYM_CONST;
  5309. sym->type = (zend_ffi_type*)sym_type;
  5310. sym->value = value;
  5311. zend_hash_str_add_new_ptr(FFI_G(symbols), name, name_len, sym);
  5312. }
  5313. }
  5314. /* }}} */
  5315. void zend_ffi_make_struct_type(zend_ffi_dcl *dcl) /* {{{ */
  5316. {
  5317. zend_ffi_type *type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
  5318. type->kind = ZEND_FFI_TYPE_STRUCT;
  5319. type->attr = FFI_G(default_type_attr) | (dcl->attr & ZEND_FFI_STRUCT_ATTRS);
  5320. type->size = 0;
  5321. type->align = dcl->align > 1 ? dcl->align : 1;
  5322. if (dcl->flags & ZEND_FFI_DCL_UNION) {
  5323. type->attr |= ZEND_FFI_ATTR_UNION;
  5324. }
  5325. dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(type);
  5326. type->record.tag_name = NULL;
  5327. zend_hash_init(&type->record.fields, 0, NULL, FFI_G(persistent) ? zend_ffi_field_hash_persistent_dtor :zend_ffi_field_hash_dtor, FFI_G(persistent));
  5328. dcl->attr &= ~ZEND_FFI_STRUCT_ATTRS;
  5329. dcl->align = 0;
  5330. }
  5331. /* }}} */
  5332. static int zend_ffi_validate_prev_field_type(zend_ffi_type *struct_type) /* {{{ */
  5333. {
  5334. if (zend_hash_num_elements(&struct_type->record.fields) > 0) {
  5335. zend_ffi_field *field = NULL;
  5336. ZEND_HASH_REVERSE_FOREACH_PTR(&struct_type->record.fields, field) {
  5337. break;
  5338. } ZEND_HASH_FOREACH_END();
  5339. if (ZEND_FFI_TYPE(field->type)->attr & ZEND_FFI_ATTR_INCOMPLETE_ARRAY) {
  5340. zend_ffi_throw_parser_error("Flexible array member not at end of struct at line %d", FFI_G(line));
  5341. return FAILURE;
  5342. }
  5343. }
  5344. return SUCCESS;
  5345. }
  5346. /* }}} */
  5347. static int zend_ffi_validate_field_type(zend_ffi_type *type, zend_ffi_type *struct_type) /* {{{ */
  5348. {
  5349. if (type == struct_type) {
  5350. zend_ffi_throw_parser_error("Struct/union can't contain an instance of itself at line %d", FFI_G(line));
  5351. return FAILURE;
  5352. } else if (zend_ffi_validate_var_type(type, 1) != SUCCESS) {
  5353. return FAILURE;
  5354. } else if (struct_type->attr & ZEND_FFI_ATTR_UNION) {
  5355. if (type->attr & ZEND_FFI_ATTR_INCOMPLETE_ARRAY) {
  5356. zend_ffi_throw_parser_error("Flexible array member in union at line %d", FFI_G(line));
  5357. return FAILURE;
  5358. }
  5359. }
  5360. return zend_ffi_validate_prev_field_type(struct_type);
  5361. }
  5362. /* }}} */
  5363. void zend_ffi_add_field(zend_ffi_dcl *struct_dcl, const char *name, size_t name_len, zend_ffi_dcl *field_dcl) /* {{{ */
  5364. {
  5365. zend_ffi_field *field;
  5366. zend_ffi_type *struct_type = ZEND_FFI_TYPE(struct_dcl->type);
  5367. zend_ffi_type *field_type;
  5368. ZEND_ASSERT(struct_type && struct_type->kind == ZEND_FFI_TYPE_STRUCT);
  5369. zend_ffi_finalize_type(field_dcl);
  5370. field_type = ZEND_FFI_TYPE(field_dcl->type);
  5371. if (zend_ffi_validate_field_type(field_type, struct_type) != SUCCESS) {
  5372. zend_ffi_cleanup_dcl(field_dcl);
  5373. LONGJMP(FFI_G(bailout), FAILURE);
  5374. }
  5375. field = pemalloc(sizeof(zend_ffi_field), FFI_G(persistent));
  5376. if (!(struct_type->attr & ZEND_FFI_ATTR_PACKED) && !(field_dcl->attr & ZEND_FFI_ATTR_PACKED)) {
  5377. struct_type->align = MAX(struct_type->align, MAX(field_type->align, field_dcl->align));
  5378. }
  5379. if (struct_type->attr & ZEND_FFI_ATTR_UNION) {
  5380. field->offset = 0;
  5381. struct_type->size = MAX(struct_type->size, field_type->size);
  5382. } else {
  5383. if (!(struct_type->attr & ZEND_FFI_ATTR_PACKED) && !(field_dcl->attr & ZEND_FFI_ATTR_PACKED)) {
  5384. uint32_t field_align = MAX(field_type->align, field_dcl->align);
  5385. struct_type->size = ((struct_type->size + (field_align - 1)) / field_align) * field_align;
  5386. }
  5387. field->offset = struct_type->size;
  5388. struct_type->size += field_type->size;
  5389. }
  5390. field->type = field_dcl->type;
  5391. field->is_const = (bool)(field_dcl->attr & ZEND_FFI_ATTR_CONST);
  5392. field->is_nested = 0;
  5393. field->first_bit = 0;
  5394. field->bits = 0;
  5395. field_dcl->type = field_type; /* reset "owned" flag */
  5396. if (!zend_hash_str_add_ptr(&struct_type->record.fields, name, name_len, field)) {
  5397. zend_ffi_type_dtor(field->type);
  5398. pefree(field, FFI_G(persistent));
  5399. zend_ffi_parser_error("Duplicate field name \"%.*s\" at line %d", name_len, name, FFI_G(line));
  5400. }
  5401. }
  5402. /* }}} */
  5403. void zend_ffi_add_anonymous_field(zend_ffi_dcl *struct_dcl, zend_ffi_dcl *field_dcl) /* {{{ */
  5404. {
  5405. zend_ffi_type *struct_type = ZEND_FFI_TYPE(struct_dcl->type);
  5406. zend_ffi_type *field_type;
  5407. zend_ffi_field *field;
  5408. zend_string *key;
  5409. ZEND_ASSERT(struct_type && struct_type->kind == ZEND_FFI_TYPE_STRUCT);
  5410. zend_ffi_finalize_type(field_dcl);
  5411. field_type = ZEND_FFI_TYPE(field_dcl->type);
  5412. if (field_type->kind != ZEND_FFI_TYPE_STRUCT) {
  5413. zend_ffi_cleanup_dcl(field_dcl);
  5414. zend_ffi_parser_error("Declaration does not declare anything at line %d", FFI_G(line));
  5415. return;
  5416. }
  5417. if (!(struct_type->attr & ZEND_FFI_ATTR_PACKED) && !(field_dcl->attr & ZEND_FFI_ATTR_PACKED)) {
  5418. struct_type->align = MAX(struct_type->align, MAX(field_type->align, field_dcl->align));
  5419. }
  5420. if (!(struct_type->attr & ZEND_FFI_ATTR_UNION)) {
  5421. if (zend_ffi_validate_prev_field_type(struct_type) != SUCCESS) {
  5422. zend_ffi_cleanup_dcl(field_dcl);
  5423. LONGJMP(FFI_G(bailout), FAILURE);
  5424. }
  5425. if (!(struct_type->attr & ZEND_FFI_ATTR_PACKED) && !(field_dcl->attr & ZEND_FFI_ATTR_PACKED)) {
  5426. uint32_t field_align = MAX(field_type->align, field_dcl->align);
  5427. struct_type->size = ((struct_type->size + (field_align - 1)) / field_align) * field_align;
  5428. }
  5429. }
  5430. ZEND_HASH_FOREACH_STR_KEY_PTR(&field_type->record.fields, key, field) {
  5431. zend_ffi_field *new_field = pemalloc(sizeof(zend_ffi_field), FFI_G(persistent));
  5432. if (struct_type->attr & ZEND_FFI_ATTR_UNION) {
  5433. new_field->offset = field->offset;
  5434. } else {
  5435. new_field->offset = struct_type->size + field->offset;
  5436. }
  5437. new_field->type = field->type;
  5438. new_field->is_const = field->is_const;
  5439. new_field->is_nested = 1;
  5440. new_field->first_bit = field->first_bit;
  5441. new_field->bits = field->bits;
  5442. field->type = ZEND_FFI_TYPE(field->type); /* reset "owned" flag */
  5443. if (key) {
  5444. if (!zend_hash_add_ptr(&struct_type->record.fields, key, new_field)) {
  5445. zend_ffi_type_dtor(new_field->type);
  5446. pefree(new_field, FFI_G(persistent));
  5447. zend_ffi_parser_error("Duplicate field name \"%s\" at line %d", ZSTR_VAL(key), FFI_G(line));
  5448. return;
  5449. }
  5450. } else {
  5451. zend_hash_next_index_insert_ptr(&struct_type->record.fields, field);
  5452. }
  5453. } ZEND_HASH_FOREACH_END();
  5454. if (struct_type->attr & ZEND_FFI_ATTR_UNION) {
  5455. struct_type->size = MAX(struct_type->size, field_type->size);
  5456. } else {
  5457. struct_type->size += field_type->size;
  5458. }
  5459. zend_ffi_type_dtor(field_dcl->type);
  5460. field_dcl->type = NULL;
  5461. }
  5462. /* }}} */
  5463. void zend_ffi_add_bit_field(zend_ffi_dcl *struct_dcl, const char *name, size_t name_len, zend_ffi_dcl *field_dcl, zend_ffi_val *bits) /* {{{ */
  5464. {
  5465. zend_ffi_type *struct_type = ZEND_FFI_TYPE(struct_dcl->type);
  5466. zend_ffi_type *field_type;
  5467. zend_ffi_field *field;
  5468. ZEND_ASSERT(struct_type && struct_type->kind == ZEND_FFI_TYPE_STRUCT);
  5469. zend_ffi_finalize_type(field_dcl);
  5470. field_type = ZEND_FFI_TYPE(field_dcl->type);
  5471. if (zend_ffi_validate_field_type(field_type, struct_type) != SUCCESS) {
  5472. zend_ffi_cleanup_dcl(field_dcl);
  5473. LONGJMP(FFI_G(bailout), FAILURE);
  5474. }
  5475. if (field_type->kind < ZEND_FFI_TYPE_UINT8 || field_type->kind > ZEND_FFI_TYPE_BOOL) {
  5476. zend_ffi_cleanup_dcl(field_dcl);
  5477. zend_ffi_parser_error("Wrong type of bit field \"%.*s\" at line %d", name ? name_len : sizeof("<anonymous>")-1, name ? name : "<anonymous>", FFI_G(line));
  5478. }
  5479. if (bits->kind == ZEND_FFI_VAL_INT32 || bits->kind == ZEND_FFI_VAL_INT64) {
  5480. if (bits->i64 < 0) {
  5481. zend_ffi_cleanup_dcl(field_dcl);
  5482. zend_ffi_parser_error("Negative width in bit-field \"%.*s\" at line %d", name ? name_len : sizeof("<anonymous>")-1, name ? name : "<anonymous>", FFI_G(line));
  5483. } else if (bits->i64 == 0) {
  5484. zend_ffi_cleanup_dcl(field_dcl);
  5485. if (name) {
  5486. zend_ffi_parser_error("Zero width in bit-field \"%.*s\" at line %d", name ? name_len : sizeof("<anonymous>")-1, name ? name : "<anonymous>", FFI_G(line));
  5487. }
  5488. return;
  5489. } else if (bits->i64 > field_type->size * 8) {
  5490. zend_ffi_cleanup_dcl(field_dcl);
  5491. zend_ffi_parser_error("Width of \"%.*s\" exceeds its type at line %d", name ? name_len : sizeof("<anonymous>")-1, name ? name : "<anonymous>", FFI_G(line));
  5492. }
  5493. } else if (bits->kind == ZEND_FFI_VAL_UINT32 || bits->kind == ZEND_FFI_VAL_UINT64) {
  5494. if (bits->u64 == 0) {
  5495. zend_ffi_cleanup_dcl(field_dcl);
  5496. if (name) {
  5497. zend_ffi_parser_error("Zero width in bit-field \"%.*s\" at line %d", name ? name_len : sizeof("<anonymous>")-1, name ? name : "<anonymous>", FFI_G(line));
  5498. }
  5499. return;
  5500. } else if (bits->u64 > field_type->size * 8) {
  5501. zend_ffi_cleanup_dcl(field_dcl);
  5502. zend_ffi_parser_error("Width of \"%.*s\" exceeds its type at line %d", name ? name_len : sizeof("<anonymous>")-1, name ? name : "<anonymous>", FFI_G(line));
  5503. }
  5504. } else {
  5505. zend_ffi_cleanup_dcl(field_dcl);
  5506. zend_ffi_parser_error("Bit field \"%.*s\" width not an integer constant at line %d", name ? name_len : sizeof("<anonymous>")-1, name ? name : "<anonymous>", FFI_G(line));
  5507. }
  5508. field = pemalloc(sizeof(zend_ffi_field), FFI_G(persistent));
  5509. if (!(struct_type->attr & ZEND_FFI_ATTR_PACKED)) {
  5510. struct_type->align = MAX(struct_type->align, sizeof(uint32_t));
  5511. }
  5512. if (struct_type->attr & ZEND_FFI_ATTR_UNION) {
  5513. field->offset = 0;
  5514. field->first_bit = 0;
  5515. field->bits = bits->u64;
  5516. if (struct_type->attr & ZEND_FFI_ATTR_PACKED) {
  5517. struct_type->size = MAX(struct_type->size, (bits->u64 + 7) / 8);
  5518. } else {
  5519. struct_type->size = MAX(struct_type->size, ((bits->u64 + 31) / 32) * 4);
  5520. }
  5521. } else {
  5522. zend_ffi_field *prev_field = NULL;
  5523. if (zend_hash_num_elements(&struct_type->record.fields) > 0) {
  5524. ZEND_HASH_REVERSE_FOREACH_PTR(&struct_type->record.fields, prev_field) {
  5525. break;
  5526. } ZEND_HASH_FOREACH_END();
  5527. }
  5528. if (prev_field && prev_field->bits) {
  5529. field->offset = prev_field->offset;
  5530. field->first_bit = prev_field->first_bit + prev_field->bits;
  5531. field->bits = bits->u64;
  5532. } else {
  5533. field->offset = struct_type->size;
  5534. field->first_bit = 0;
  5535. field->bits = bits->u64;
  5536. }
  5537. if (struct_type->attr & ZEND_FFI_ATTR_PACKED) {
  5538. struct_type->size = field->offset + ((field->first_bit + field->bits) + 7) / 8;
  5539. } else {
  5540. struct_type->size = field->offset + (((field->first_bit + field->bits) + 31) / 32) * 4;
  5541. }
  5542. }
  5543. field->type = field_dcl->type;
  5544. field->is_const = (bool)(field_dcl->attr & ZEND_FFI_ATTR_CONST);
  5545. field->is_nested = 0;
  5546. field_dcl->type = field_type; /* reset "owned" flag */
  5547. if (name) {
  5548. if (!zend_hash_str_add_ptr(&struct_type->record.fields, name, name_len, field)) {
  5549. zend_ffi_type_dtor(field->type);
  5550. pefree(field, FFI_G(persistent));
  5551. zend_ffi_parser_error("Duplicate field name \"%.*s\" at line %d", name_len, name, FFI_G(line));
  5552. }
  5553. } else {
  5554. zend_hash_next_index_insert_ptr(&struct_type->record.fields, field);
  5555. }
  5556. }
  5557. /* }}} */
  5558. void zend_ffi_adjust_struct_size(zend_ffi_dcl *dcl) /* {{{ */
  5559. {
  5560. zend_ffi_type *struct_type = ZEND_FFI_TYPE(dcl->type);
  5561. ZEND_ASSERT(struct_type->kind == ZEND_FFI_TYPE_STRUCT);
  5562. if (dcl->align > struct_type->align) {
  5563. struct_type->align = dcl->align;
  5564. }
  5565. if (!(struct_type->attr & ZEND_FFI_ATTR_PACKED)) {
  5566. struct_type->size = ((struct_type->size + (struct_type->align - 1)) / struct_type->align) * struct_type->align;
  5567. }
  5568. dcl->align = 0;
  5569. }
  5570. /* }}} */
  5571. void zend_ffi_make_pointer_type(zend_ffi_dcl *dcl) /* {{{ */
  5572. {
  5573. zend_ffi_type *type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
  5574. type->kind = ZEND_FFI_TYPE_POINTER;
  5575. type->attr = FFI_G(default_type_attr) | (dcl->attr & ZEND_FFI_POINTER_ATTRS);
  5576. type->size = sizeof(void*);
  5577. type->align = _Alignof(void*);
  5578. zend_ffi_finalize_type(dcl);
  5579. if (zend_ffi_validate_vla(ZEND_FFI_TYPE(dcl->type)) != SUCCESS) {
  5580. zend_ffi_cleanup_dcl(dcl);
  5581. LONGJMP(FFI_G(bailout), FAILURE);
  5582. }
  5583. type->pointer.type = dcl->type;
  5584. dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(type);
  5585. dcl->flags &= ~ZEND_FFI_DCL_TYPE_QUALIFIERS;
  5586. dcl->attr &= ~ZEND_FFI_POINTER_ATTRS;
  5587. dcl->align = 0;
  5588. }
  5589. /* }}} */
  5590. static int zend_ffi_validate_array_element_type(zend_ffi_type *type) /* {{{ */
  5591. {
  5592. if (type->kind == ZEND_FFI_TYPE_FUNC) {
  5593. zend_ffi_throw_parser_error("Array of functions is not allowed at line %d", FFI_G(line));
  5594. return FAILURE;
  5595. } else if (type->kind == ZEND_FFI_TYPE_ARRAY && (type->attr & ZEND_FFI_ATTR_INCOMPLETE_ARRAY)) {
  5596. zend_ffi_throw_parser_error("Only the leftmost array can be undimensioned at line %d", FFI_G(line));
  5597. return FAILURE;
  5598. }
  5599. return zend_ffi_validate_type(type, 0, 1);
  5600. }
  5601. /* }}} */
  5602. void zend_ffi_make_array_type(zend_ffi_dcl *dcl, zend_ffi_val *len) /* {{{ */
  5603. {
  5604. int length = 0;
  5605. zend_ffi_type *element_type;
  5606. zend_ffi_type *type;
  5607. zend_ffi_finalize_type(dcl);
  5608. element_type = ZEND_FFI_TYPE(dcl->type);
  5609. if (len->kind == ZEND_FFI_VAL_EMPTY) {
  5610. length = 0;
  5611. } else if (len->kind == ZEND_FFI_VAL_UINT32 || len->kind == ZEND_FFI_VAL_UINT64) {
  5612. length = len->u64;
  5613. } else if (len->kind == ZEND_FFI_VAL_INT32 || len->kind == ZEND_FFI_VAL_INT64) {
  5614. length = len->i64;
  5615. } else if (len->kind == ZEND_FFI_VAL_CHAR) {
  5616. length = len->ch;
  5617. } else {
  5618. zend_ffi_cleanup_dcl(dcl);
  5619. zend_ffi_parser_error("Unsupported array index type at line %d", FFI_G(line));
  5620. return;
  5621. }
  5622. if (length < 0) {
  5623. zend_ffi_cleanup_dcl(dcl);
  5624. zend_ffi_parser_error("Negative array index at line %d", FFI_G(line));
  5625. return;
  5626. }
  5627. if (zend_ffi_validate_array_element_type(element_type) != SUCCESS) {
  5628. zend_ffi_cleanup_dcl(dcl);
  5629. LONGJMP(FFI_G(bailout), FAILURE);
  5630. }
  5631. type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
  5632. type->kind = ZEND_FFI_TYPE_ARRAY;
  5633. type->attr = FFI_G(default_type_attr) | (dcl->attr & ZEND_FFI_ARRAY_ATTRS);
  5634. type->size = length * element_type->size;
  5635. type->align = element_type->align;
  5636. type->array.type = dcl->type;
  5637. type->array.length = length;
  5638. dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(type);
  5639. dcl->flags &= ~ZEND_FFI_DCL_TYPE_QUALIFIERS;
  5640. dcl->attr &= ~ZEND_FFI_ARRAY_ATTRS;
  5641. dcl->align = 0;
  5642. }
  5643. /* }}} */
  5644. static int zend_ffi_validate_func_ret_type(zend_ffi_type *type) /* {{{ */
  5645. {
  5646. if (type->kind == ZEND_FFI_TYPE_FUNC) {
  5647. zend_ffi_throw_parser_error("Function returning function is not allowed at line %d", FFI_G(line));
  5648. return FAILURE;
  5649. } else if (type->kind == ZEND_FFI_TYPE_ARRAY) {
  5650. zend_ffi_throw_parser_error("Function returning array is not allowed at line %d", FFI_G(line));
  5651. return FAILURE;
  5652. }
  5653. return zend_ffi_validate_incomplete_type(type, 1, 0);
  5654. }
  5655. /* }}} */
  5656. void zend_ffi_make_func_type(zend_ffi_dcl *dcl, HashTable *args, zend_ffi_dcl *nested_dcl) /* {{{ */
  5657. {
  5658. zend_ffi_type *type;
  5659. zend_ffi_type *ret_type;
  5660. zend_ffi_finalize_type(dcl);
  5661. ret_type = ZEND_FFI_TYPE(dcl->type);
  5662. if (args) {
  5663. int no_args = 0;
  5664. zend_ffi_type *arg_type;
  5665. ZEND_HASH_FOREACH_PTR(args, arg_type) {
  5666. arg_type = ZEND_FFI_TYPE(arg_type);
  5667. if (arg_type->kind == ZEND_FFI_TYPE_VOID) {
  5668. if (zend_hash_num_elements(args) != 1) {
  5669. zend_ffi_cleanup_dcl(nested_dcl);
  5670. zend_ffi_cleanup_dcl(dcl);
  5671. zend_hash_destroy(args);
  5672. pefree(args, FFI_G(persistent));
  5673. zend_ffi_parser_error("void type is not allowed at line %d", FFI_G(line));
  5674. return;
  5675. } else {
  5676. no_args = 1;
  5677. }
  5678. }
  5679. } ZEND_HASH_FOREACH_END();
  5680. if (no_args) {
  5681. zend_hash_destroy(args);
  5682. pefree(args, FFI_G(persistent));
  5683. args = NULL;
  5684. }
  5685. }
  5686. #ifdef HAVE_FFI_VECTORCALL_PARTIAL
  5687. if (dcl->abi == ZEND_FFI_ABI_VECTORCALL && args) {
  5688. zend_ulong i;
  5689. zend_ffi_type *arg_type;
  5690. ZEND_HASH_FOREACH_NUM_KEY_PTR(args, i, arg_type) {
  5691. arg_type = ZEND_FFI_TYPE(arg_type);
  5692. # ifdef _WIN64
  5693. if (i >= 4 && i <= 5 && (arg_type->kind == ZEND_FFI_TYPE_FLOAT || arg_type->kind == ZEND_FFI_TYPE_DOUBLE)) {
  5694. # else
  5695. if (i < 6 && (arg_type->kind == ZEND_FFI_TYPE_FLOAT || arg_type->kind == ZEND_FFI_TYPE_DOUBLE)) {
  5696. # endif
  5697. zend_ffi_cleanup_dcl(nested_dcl);
  5698. zend_ffi_cleanup_dcl(dcl);
  5699. zend_hash_destroy(args);
  5700. pefree(args, FFI_G(persistent));
  5701. zend_ffi_parser_error("Type float/double is not allowed at position " ZEND_ULONG_FMT " with __vectorcall at line %d", i+1, FFI_G(line));
  5702. return;
  5703. }
  5704. } ZEND_HASH_FOREACH_END();
  5705. }
  5706. #endif
  5707. if (zend_ffi_validate_func_ret_type(ret_type) != SUCCESS) {
  5708. zend_ffi_cleanup_dcl(nested_dcl);
  5709. zend_ffi_cleanup_dcl(dcl);
  5710. if (args) {
  5711. zend_hash_destroy(args);
  5712. pefree(args, FFI_G(persistent));
  5713. }
  5714. LONGJMP(FFI_G(bailout), FAILURE);
  5715. }
  5716. type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
  5717. type->kind = ZEND_FFI_TYPE_FUNC;
  5718. type->attr = FFI_G(default_type_attr) | (dcl->attr & ZEND_FFI_FUNC_ATTRS);
  5719. type->size = sizeof(void*);
  5720. type->align = 1;
  5721. type->func.ret_type = dcl->type;
  5722. switch (dcl->abi) {
  5723. case ZEND_FFI_ABI_DEFAULT:
  5724. case ZEND_FFI_ABI_CDECL:
  5725. type->func.abi = FFI_DEFAULT_ABI;
  5726. break;
  5727. #ifdef HAVE_FFI_FASTCALL
  5728. case ZEND_FFI_ABI_FASTCALL:
  5729. type->func.abi = FFI_FASTCALL;
  5730. break;
  5731. #endif
  5732. #ifdef HAVE_FFI_THISCALL
  5733. case ZEND_FFI_ABI_THISCALL:
  5734. type->func.abi = FFI_THISCALL;
  5735. break;
  5736. #endif
  5737. #ifdef HAVE_FFI_STDCALL
  5738. case ZEND_FFI_ABI_STDCALL:
  5739. type->func.abi = FFI_STDCALL;
  5740. break;
  5741. #endif
  5742. #ifdef HAVE_FFI_PASCAL
  5743. case ZEND_FFI_ABI_PASCAL:
  5744. type->func.abi = FFI_PASCAL;
  5745. break;
  5746. #endif
  5747. #ifdef HAVE_FFI_REGISTER
  5748. case ZEND_FFI_ABI_REGISTER:
  5749. type->func.abi = FFI_REGISTER;
  5750. break;
  5751. #endif
  5752. #ifdef HAVE_FFI_MS_CDECL
  5753. case ZEND_FFI_ABI_MS:
  5754. type->func.abi = FFI_MS_CDECL;
  5755. break;
  5756. #endif
  5757. #ifdef HAVE_FFI_SYSV
  5758. case ZEND_FFI_ABI_SYSV:
  5759. type->func.abi = FFI_SYSV;
  5760. break;
  5761. #endif
  5762. #ifdef HAVE_FFI_VECTORCALL_PARTIAL
  5763. case ZEND_FFI_ABI_VECTORCALL:
  5764. type->func.abi = FFI_VECTORCALL_PARTIAL;
  5765. break;
  5766. #endif
  5767. default:
  5768. type->func.abi = FFI_DEFAULT_ABI;
  5769. zend_ffi_cleanup_dcl(nested_dcl);
  5770. if (args) {
  5771. zend_hash_destroy(args);
  5772. pefree(args, FFI_G(persistent));
  5773. }
  5774. type->func.args = NULL;
  5775. _zend_ffi_type_dtor(type);
  5776. zend_ffi_parser_error("Unsupported calling convention line %d", FFI_G(line));
  5777. break;
  5778. }
  5779. type->func.args = args;
  5780. dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(type);
  5781. dcl->attr &= ~ZEND_FFI_FUNC_ATTRS;
  5782. dcl->align = 0;
  5783. dcl->abi = 0;
  5784. }
  5785. /* }}} */
  5786. void zend_ffi_add_arg(HashTable **args, const char *name, size_t name_len, zend_ffi_dcl *arg_dcl) /* {{{ */
  5787. {
  5788. zend_ffi_type *type;
  5789. if (!*args) {
  5790. *args = pemalloc(sizeof(HashTable), FFI_G(persistent));
  5791. zend_hash_init(*args, 0, NULL, zend_ffi_type_hash_dtor, FFI_G(persistent));
  5792. }
  5793. zend_ffi_finalize_type(arg_dcl);
  5794. type = ZEND_FFI_TYPE(arg_dcl->type);
  5795. if (type->kind == ZEND_FFI_TYPE_ARRAY) {
  5796. if (ZEND_FFI_TYPE_IS_OWNED(arg_dcl->type)) {
  5797. type->kind = ZEND_FFI_TYPE_POINTER;
  5798. type->size = sizeof(void*);
  5799. } else {
  5800. zend_ffi_type *new_type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
  5801. new_type->kind = ZEND_FFI_TYPE_POINTER;
  5802. new_type->attr = FFI_G(default_type_attr) | (type->attr & ZEND_FFI_POINTER_ATTRS);
  5803. new_type->size = sizeof(void*);
  5804. new_type->align = _Alignof(void*);
  5805. new_type->pointer.type = ZEND_FFI_TYPE(type->array.type);
  5806. arg_dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(new_type);
  5807. }
  5808. } else if (type->kind == ZEND_FFI_TYPE_FUNC) {
  5809. zend_ffi_type *new_type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
  5810. new_type->kind = ZEND_FFI_TYPE_POINTER;
  5811. new_type->attr = FFI_G(default_type_attr);
  5812. new_type->size = sizeof(void*);
  5813. new_type->align = _Alignof(void*);
  5814. new_type->pointer.type = arg_dcl->type;
  5815. arg_dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(new_type);
  5816. }
  5817. if (zend_ffi_validate_incomplete_type(type, 1, 1) != SUCCESS) {
  5818. zend_ffi_cleanup_dcl(arg_dcl);
  5819. zend_hash_destroy(*args);
  5820. pefree(*args, FFI_G(persistent));
  5821. *args = NULL;
  5822. LONGJMP(FFI_G(bailout), FAILURE);
  5823. }
  5824. zend_hash_next_index_insert_ptr(*args, (void*)arg_dcl->type);
  5825. }
  5826. /* }}} */
  5827. void zend_ffi_declare(const char *name, size_t name_len, zend_ffi_dcl *dcl) /* {{{ */
  5828. {
  5829. zend_ffi_symbol *sym;
  5830. if (!FFI_G(symbols)) {
  5831. FFI_G(symbols) = pemalloc(sizeof(HashTable), FFI_G(persistent));
  5832. zend_hash_init(FFI_G(symbols), 0, NULL, FFI_G(persistent) ? zend_ffi_symbol_hash_persistent_dtor : zend_ffi_symbol_hash_dtor, FFI_G(persistent));
  5833. }
  5834. zend_ffi_finalize_type(dcl);
  5835. sym = zend_hash_str_find_ptr(FFI_G(symbols), name, name_len);
  5836. if (sym) {
  5837. if ((dcl->flags & ZEND_FFI_DCL_STORAGE_CLASS) == ZEND_FFI_DCL_TYPEDEF
  5838. && sym->kind == ZEND_FFI_SYM_TYPE
  5839. && zend_ffi_is_same_type(ZEND_FFI_TYPE(sym->type), ZEND_FFI_TYPE(dcl->type))
  5840. && sym->is_const == (bool)(dcl->attr & ZEND_FFI_ATTR_CONST)) {
  5841. /* allowed redeclaration */
  5842. zend_ffi_type_dtor(dcl->type);
  5843. return;
  5844. } else if ((dcl->flags & ZEND_FFI_DCL_STORAGE_CLASS) == 0
  5845. || (dcl->flags & ZEND_FFI_DCL_STORAGE_CLASS) == ZEND_FFI_DCL_EXTERN) {
  5846. zend_ffi_type *type = ZEND_FFI_TYPE(dcl->type);
  5847. if (type->kind == ZEND_FFI_TYPE_FUNC) {
  5848. if (sym->kind == ZEND_FFI_SYM_FUNC
  5849. && zend_ffi_same_types(ZEND_FFI_TYPE(sym->type), type)) {
  5850. /* allowed redeclaration */
  5851. zend_ffi_type_dtor(dcl->type);
  5852. return;
  5853. }
  5854. } else {
  5855. if (sym->kind == ZEND_FFI_SYM_VAR
  5856. && zend_ffi_is_same_type(ZEND_FFI_TYPE(sym->type), type)
  5857. && sym->is_const == (bool)(dcl->attr & ZEND_FFI_ATTR_CONST)) {
  5858. /* allowed redeclaration */
  5859. zend_ffi_type_dtor(dcl->type);
  5860. return;
  5861. }
  5862. }
  5863. }
  5864. zend_ffi_parser_error("Redeclaration of \"%.*s\" at line %d", name_len, name, FFI_G(line));
  5865. } else {
  5866. if ((dcl->flags & ZEND_FFI_DCL_STORAGE_CLASS) == ZEND_FFI_DCL_TYPEDEF) {
  5867. if (zend_ffi_validate_vla(ZEND_FFI_TYPE(dcl->type)) != SUCCESS) {
  5868. zend_ffi_cleanup_dcl(dcl);
  5869. LONGJMP(FFI_G(bailout), FAILURE);
  5870. }
  5871. if (dcl->align && dcl->align > ZEND_FFI_TYPE(dcl->type)->align) {
  5872. if (ZEND_FFI_TYPE_IS_OWNED(dcl->type)) {
  5873. ZEND_FFI_TYPE(dcl->type)->align = dcl->align;
  5874. } else {
  5875. zend_ffi_type *type = pemalloc(sizeof(zend_ffi_type), FFI_G(persistent));
  5876. memcpy(type, ZEND_FFI_TYPE(dcl->type), sizeof(zend_ffi_type));
  5877. type->attr |= FFI_G(default_type_attr);
  5878. type->align = dcl->align;
  5879. dcl->type = ZEND_FFI_TYPE_MAKE_OWNED(type);
  5880. }
  5881. }
  5882. sym = pemalloc(sizeof(zend_ffi_symbol), FFI_G(persistent));
  5883. sym->kind = ZEND_FFI_SYM_TYPE;
  5884. sym->type = dcl->type;
  5885. sym->is_const = (bool)(dcl->attr & ZEND_FFI_ATTR_CONST);
  5886. dcl->type = ZEND_FFI_TYPE(dcl->type); /* reset "owned" flag */
  5887. zend_hash_str_add_new_ptr(FFI_G(symbols), name, name_len, sym);
  5888. } else {
  5889. zend_ffi_type *type;
  5890. type = ZEND_FFI_TYPE(dcl->type);
  5891. if (zend_ffi_validate_type(type, (dcl->flags & ZEND_FFI_DCL_STORAGE_CLASS) == ZEND_FFI_DCL_EXTERN, 1) != SUCCESS) {
  5892. zend_ffi_cleanup_dcl(dcl);
  5893. LONGJMP(FFI_G(bailout), FAILURE);
  5894. }
  5895. if ((dcl->flags & ZEND_FFI_DCL_STORAGE_CLASS) == 0 ||
  5896. (dcl->flags & ZEND_FFI_DCL_STORAGE_CLASS) == ZEND_FFI_DCL_EXTERN) {
  5897. sym = pemalloc(sizeof(zend_ffi_symbol), FFI_G(persistent));
  5898. sym->kind = (type->kind == ZEND_FFI_TYPE_FUNC) ? ZEND_FFI_SYM_FUNC : ZEND_FFI_SYM_VAR;
  5899. sym->type = dcl->type;
  5900. sym->is_const = (bool)(dcl->attr & ZEND_FFI_ATTR_CONST);
  5901. dcl->type = type; /* reset "owned" flag */
  5902. zend_hash_str_add_new_ptr(FFI_G(symbols), name, name_len, sym);
  5903. } else {
  5904. /* useless declarartion */
  5905. zend_ffi_type_dtor(dcl->type);
  5906. }
  5907. }
  5908. }
  5909. }
  5910. /* }}} */
  5911. void zend_ffi_declare_tag(const char *name, size_t name_len, zend_ffi_dcl *dcl, bool incomplete) /* {{{ */
  5912. {
  5913. zend_ffi_tag *tag;
  5914. zend_ffi_type *type;
  5915. if (!FFI_G(tags)) {
  5916. FFI_G(tags) = pemalloc(sizeof(HashTable), FFI_G(persistent));
  5917. zend_hash_init(FFI_G(tags), 0, NULL, FFI_G(persistent) ? zend_ffi_tag_hash_persistent_dtor : zend_ffi_tag_hash_dtor, FFI_G(persistent));
  5918. }
  5919. tag = zend_hash_str_find_ptr(FFI_G(tags), name, name_len);
  5920. if (tag) {
  5921. zend_ffi_type *type = ZEND_FFI_TYPE(tag->type);
  5922. if (dcl->flags & ZEND_FFI_DCL_STRUCT) {
  5923. if (tag->kind != ZEND_FFI_TAG_STRUCT) {
  5924. zend_ffi_parser_error("\"%.*s\" defined as wrong kind of tag at line %d", name_len, name, FFI_G(line));
  5925. return;
  5926. } else if (!incomplete && !(type->attr & ZEND_FFI_ATTR_INCOMPLETE_TAG)) {
  5927. zend_ffi_parser_error("Redefinition of \"struct %.*s\" at line %d", name_len, name, FFI_G(line));
  5928. return;
  5929. }
  5930. } else if (dcl->flags & ZEND_FFI_DCL_UNION) {
  5931. if (tag->kind != ZEND_FFI_TAG_UNION) {
  5932. zend_ffi_parser_error("\"%.*s\" defined as wrong kind of tag at line %d", name_len, name, FFI_G(line));
  5933. return;
  5934. } else if (!incomplete && !(type->attr & ZEND_FFI_ATTR_INCOMPLETE_TAG)) {
  5935. zend_ffi_parser_error("Redefinition of \"union %.*s\" at line %d", name_len, name, FFI_G(line));
  5936. return;
  5937. }
  5938. } else if (dcl->flags & ZEND_FFI_DCL_ENUM) {
  5939. if (tag->kind != ZEND_FFI_TAG_ENUM) {
  5940. zend_ffi_parser_error("\"%.*s\" defined as wrong kind of tag at line %d", name_len, name, FFI_G(line));
  5941. return;
  5942. } else if (!incomplete && !(type->attr & ZEND_FFI_ATTR_INCOMPLETE_TAG)) {
  5943. zend_ffi_parser_error("Redefinition of \"enum %.*s\" at line %d", name_len, name, FFI_G(line));
  5944. return;
  5945. }
  5946. } else {
  5947. ZEND_UNREACHABLE();
  5948. return;
  5949. }
  5950. dcl->type = type;
  5951. if (!incomplete) {
  5952. type->attr &= ~ZEND_FFI_ATTR_INCOMPLETE_TAG;
  5953. }
  5954. } else {
  5955. zend_ffi_tag *tag = pemalloc(sizeof(zend_ffi_tag), FFI_G(persistent));
  5956. zend_string *tag_name = zend_string_init(name, name_len, FFI_G(persistent));
  5957. if (dcl->flags & ZEND_FFI_DCL_STRUCT) {
  5958. tag->kind = ZEND_FFI_TAG_STRUCT;
  5959. zend_ffi_make_struct_type(dcl);
  5960. type = ZEND_FFI_TYPE(dcl->type);
  5961. type->record.tag_name = zend_string_copy(tag_name);
  5962. } else if (dcl->flags & ZEND_FFI_DCL_UNION) {
  5963. tag->kind = ZEND_FFI_TAG_UNION;
  5964. zend_ffi_make_struct_type(dcl);
  5965. type = ZEND_FFI_TYPE(dcl->type);
  5966. type->record.tag_name = zend_string_copy(tag_name);
  5967. } else if (dcl->flags & ZEND_FFI_DCL_ENUM) {
  5968. tag->kind = ZEND_FFI_TAG_ENUM;
  5969. zend_ffi_make_enum_type(dcl);
  5970. type = ZEND_FFI_TYPE(dcl->type);
  5971. type->enumeration.tag_name = zend_string_copy(tag_name);
  5972. } else {
  5973. ZEND_UNREACHABLE();
  5974. }
  5975. tag->type = ZEND_FFI_TYPE_MAKE_OWNED(dcl->type);
  5976. dcl->type = ZEND_FFI_TYPE(dcl->type);
  5977. if (incomplete) {
  5978. dcl->type->attr |= ZEND_FFI_ATTR_INCOMPLETE_TAG;
  5979. }
  5980. zend_hash_add_new_ptr(FFI_G(tags), tag_name, tag);
  5981. zend_string_release(tag_name);
  5982. }
  5983. }
  5984. /* }}} */
  5985. void zend_ffi_set_abi(zend_ffi_dcl *dcl, uint16_t abi) /* {{{ */
  5986. {
  5987. if (dcl->abi != ZEND_FFI_ABI_DEFAULT) {
  5988. zend_ffi_parser_error("Multiple calling convention specifiers at line %d", FFI_G(line));
  5989. } else {
  5990. dcl->abi = abi;
  5991. }
  5992. }
  5993. /* }}} */
  5994. #define SIMPLE_ATTRIBUTES(_) \
  5995. _(cdecl) \
  5996. _(fastcall) \
  5997. _(thiscall) \
  5998. _(stdcall) \
  5999. _(ms_abi) \
  6000. _(sysv_abi) \
  6001. _(vectorcall) \
  6002. _(aligned) \
  6003. _(packed) \
  6004. _(ms_struct) \
  6005. _(gcc_struct) \
  6006. _(const) \
  6007. _(malloc) \
  6008. _(deprecated) \
  6009. _(nothrow) \
  6010. _(leaf) \
  6011. _(pure) \
  6012. _(noreturn) \
  6013. _(warn_unused_result)
  6014. #define ATTR_ID(name) attr_ ## name,
  6015. #define ATTR_NAME(name) {sizeof(#name)-1, #name},
  6016. void zend_ffi_add_attribute(zend_ffi_dcl *dcl, const char *name, size_t name_len) /* {{{ */
  6017. {
  6018. enum {
  6019. SIMPLE_ATTRIBUTES(ATTR_ID)
  6020. attr_unsupported
  6021. };
  6022. static const struct {
  6023. size_t len;
  6024. const char * const name;
  6025. } names[] = {
  6026. SIMPLE_ATTRIBUTES(ATTR_NAME)
  6027. {0, NULL}
  6028. };
  6029. int id;
  6030. if (name_len > 4
  6031. && name[0] == '_'
  6032. && name[1] == '_'
  6033. && name[name_len-2] == '_'
  6034. && name[name_len-1] == '_') {
  6035. name += 2;
  6036. name_len -= 4;
  6037. }
  6038. for (id = 0; names[id].len != 0; id++) {
  6039. if (name_len == names[id].len) {
  6040. if (memcmp(name, names[id].name, name_len) == 0) {
  6041. break;
  6042. }
  6043. }
  6044. }
  6045. switch (id) {
  6046. case attr_cdecl:
  6047. zend_ffi_set_abi(dcl, ZEND_FFI_ABI_CDECL);
  6048. break;
  6049. case attr_fastcall:
  6050. zend_ffi_set_abi(dcl, ZEND_FFI_ABI_FASTCALL);
  6051. break;
  6052. case attr_thiscall:
  6053. zend_ffi_set_abi(dcl, ZEND_FFI_ABI_THISCALL);
  6054. break;
  6055. case attr_stdcall:
  6056. zend_ffi_set_abi(dcl, ZEND_FFI_ABI_STDCALL);
  6057. break;
  6058. case attr_ms_abi:
  6059. zend_ffi_set_abi(dcl, ZEND_FFI_ABI_MS);
  6060. break;
  6061. case attr_sysv_abi:
  6062. zend_ffi_set_abi(dcl, ZEND_FFI_ABI_SYSV);
  6063. break;
  6064. case attr_vectorcall:
  6065. zend_ffi_set_abi(dcl, ZEND_FFI_ABI_VECTORCALL);
  6066. break;
  6067. case attr_aligned:
  6068. dcl->align = __BIGGEST_ALIGNMENT__;
  6069. break;
  6070. case attr_packed:
  6071. dcl->attr |= ZEND_FFI_ATTR_PACKED;
  6072. break;
  6073. case attr_ms_struct:
  6074. dcl->attr |= ZEND_FFI_ATTR_MS_STRUCT;
  6075. break;
  6076. case attr_gcc_struct:
  6077. dcl->attr |= ZEND_FFI_ATTR_GCC_STRUCT;
  6078. break;
  6079. case attr_unsupported:
  6080. zend_ffi_parser_error("Unsupported attribute \"%.*s\" at line %d", name_len, name, FFI_G(line));
  6081. break;
  6082. default:
  6083. /* ignore */
  6084. break;
  6085. }
  6086. }
  6087. /* }}} */
  6088. #define VALUE_ATTRIBUTES(_) \
  6089. _(regparam) \
  6090. _(aligned) \
  6091. _(mode) \
  6092. _(nonnull) \
  6093. _(alloc_size) \
  6094. _(format) \
  6095. _(deprecated)
  6096. void zend_ffi_add_attribute_value(zend_ffi_dcl *dcl, const char *name, size_t name_len, int n, zend_ffi_val *val) /* {{{ */
  6097. {
  6098. enum {
  6099. VALUE_ATTRIBUTES(ATTR_ID)
  6100. attr_unsupported
  6101. };
  6102. static const struct {
  6103. size_t len;
  6104. const char * const name;
  6105. } names[] = {
  6106. VALUE_ATTRIBUTES(ATTR_NAME)
  6107. {0, NULL}
  6108. };
  6109. int id;
  6110. if (name_len > 4
  6111. && name[0] == '_'
  6112. && name[1] == '_'
  6113. && name[name_len-2] == '_'
  6114. && name[name_len-1] == '_') {
  6115. name += 2;
  6116. name_len -= 4;
  6117. }
  6118. for (id = 0; names[id].len != 0; id++) {
  6119. if (name_len == names[id].len) {
  6120. if (memcmp(name, names[id].name, name_len) == 0) {
  6121. break;
  6122. }
  6123. }
  6124. }
  6125. switch (id) {
  6126. case attr_regparam:
  6127. if (n == 0
  6128. && (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_INT64 || val->kind == ZEND_FFI_VAL_UINT64)
  6129. && val->i64 == 3) {
  6130. zend_ffi_set_abi(dcl, ZEND_FFI_ABI_REGISTER);
  6131. } else {
  6132. zend_ffi_parser_error("Incorrect \"regparam\" value at line %d", FFI_G(line));
  6133. }
  6134. break;
  6135. case attr_aligned:
  6136. if (n == 0
  6137. && (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_INT64 || val->kind == ZEND_FFI_VAL_UINT64)
  6138. && val->i64 > 0 && val->i64 <= 0x80000000 && (val->i64 & (val->i64 - 1)) == 0) {
  6139. dcl->align = val->i64;
  6140. } else {
  6141. zend_ffi_parser_error("Incorrect \"alignment\" value at line %d", FFI_G(line));
  6142. }
  6143. break;
  6144. case attr_mode:
  6145. if (n == 0
  6146. && (val->kind == ZEND_FFI_VAL_NAME)) {
  6147. const char *str = val->str;
  6148. size_t len = val->len;
  6149. if (len > 4
  6150. && str[0] == '_'
  6151. && str[1] == '_'
  6152. && str[len-2] == '_'
  6153. && str[len-1] == '_') {
  6154. str += 2;
  6155. len -= 4;
  6156. }
  6157. // TODO: Add support for vector type 'VnXX' ???
  6158. if (len == 2) {
  6159. if (str[1] == 'I') {
  6160. if (dcl->flags & (ZEND_FFI_DCL_TYPE_SPECIFIERS-(ZEND_FFI_DCL_CHAR|ZEND_FFI_DCL_SHORT|ZEND_FFI_DCL_INT|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_LONG_LONG|ZEND_FFI_DCL_SIGNED|ZEND_FFI_DCL_UNSIGNED))) {
  6161. /* inappropriate type */
  6162. } else if (str[0] == 'Q') {
  6163. dcl->flags &= ~(ZEND_FFI_DCL_CHAR|ZEND_FFI_DCL_SHORT|ZEND_FFI_DCL_INT|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_LONG_LONG);
  6164. dcl->flags |= ZEND_FFI_DCL_CHAR;
  6165. break;
  6166. } else if (str[0] == 'H') {
  6167. dcl->flags &= ~(ZEND_FFI_DCL_CHAR|ZEND_FFI_DCL_SHORT|ZEND_FFI_DCL_INT|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_LONG_LONG);
  6168. dcl->flags |= ZEND_FFI_DCL_SHORT;
  6169. break;
  6170. } else if (str[0] == 'S') {
  6171. dcl->flags &= ~(ZEND_FFI_DCL_CHAR|ZEND_FFI_DCL_SHORT|ZEND_FFI_DCL_INT|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_LONG_LONG);
  6172. dcl->flags |= ZEND_FFI_DCL_INT;
  6173. break;
  6174. } else if (str[0] == 'D') {
  6175. dcl->flags &= ~(ZEND_FFI_DCL_CHAR|ZEND_FFI_DCL_SHORT|ZEND_FFI_DCL_INT|ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_LONG_LONG);
  6176. if (sizeof(long) == 8) {
  6177. dcl->flags |= ZEND_FFI_DCL_LONG;
  6178. } else {
  6179. dcl->flags |= ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_LONG_LONG;
  6180. }
  6181. break;
  6182. }
  6183. } else if (str[1] == 'F') {
  6184. if (dcl->flags & (ZEND_FFI_DCL_TYPE_SPECIFIERS-(ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_FLOAT|ZEND_FFI_DCL_DOUBLE))) {
  6185. /* inappropriate type */
  6186. } else if (str[0] == 'S') {
  6187. dcl->flags &= ~(ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_FLOAT|ZEND_FFI_DCL_DOUBLE);
  6188. dcl->flags |= ZEND_FFI_DCL_FLOAT;
  6189. break;
  6190. } else if (str[0] == 'D') {
  6191. dcl->flags &= ~(ZEND_FFI_DCL_LONG|ZEND_FFI_DCL_FLOAT|ZEND_FFI_DCL_DOUBLE);
  6192. dcl->flags |= ZEND_FFI_DCL_DOUBLE;
  6193. break;
  6194. }
  6195. }
  6196. }
  6197. }
  6198. zend_ffi_parser_error("Unsupported \"mode\" value at line %d", FFI_G(line));
  6199. // TODO: ???
  6200. case attr_unsupported:
  6201. zend_ffi_parser_error("Unsupported attribute \"%.*s\" at line %d", name_len, name, FFI_G(line));
  6202. break;
  6203. default:
  6204. /* ignore */
  6205. break;
  6206. }
  6207. }
  6208. /* }}} */
  6209. void zend_ffi_add_msvc_attribute_value(zend_ffi_dcl *dcl, const char *name, size_t name_len, zend_ffi_val *val) /* {{{ */
  6210. {
  6211. if (name_len == sizeof("align")-1 && memcmp(name, "align", sizeof("align")-1) == 0) {
  6212. if ((val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_INT64 || val->kind == ZEND_FFI_VAL_UINT64)
  6213. && val->i64 > 0 && val->i64 <= 0x80000000 && (val->i64 & (val->i64 - 1)) == 0) {
  6214. dcl->align = val->i64;
  6215. } else {
  6216. zend_ffi_parser_error("Incorrect \"alignment\" value at line %d", FFI_G(line));
  6217. }
  6218. } else {
  6219. /* ignore */
  6220. }
  6221. }
  6222. /* }}} */
  6223. static int zend_ffi_nested_type(zend_ffi_type *type, zend_ffi_type *nested_type) /* {{{ */
  6224. {
  6225. nested_type = ZEND_FFI_TYPE(nested_type);
  6226. switch (nested_type->kind) {
  6227. case ZEND_FFI_TYPE_POINTER:
  6228. /* "char" is used as a terminator of nested declaration */
  6229. if (nested_type->pointer.type == &zend_ffi_type_char) {
  6230. nested_type->pointer.type = type;
  6231. return zend_ffi_validate_vla(ZEND_FFI_TYPE(type));
  6232. } else {
  6233. return zend_ffi_nested_type(type, nested_type->pointer.type);
  6234. }
  6235. break;
  6236. case ZEND_FFI_TYPE_ARRAY:
  6237. /* "char" is used as a terminator of nested declaration */
  6238. if (nested_type->array.type == &zend_ffi_type_char) {
  6239. nested_type->array.type = type;
  6240. if (zend_ffi_validate_array_element_type(ZEND_FFI_TYPE(type)) != SUCCESS) {
  6241. return FAILURE;
  6242. }
  6243. } else {
  6244. if (zend_ffi_nested_type(type, nested_type->array.type) != SUCCESS) {
  6245. return FAILURE;
  6246. }
  6247. }
  6248. nested_type->size = nested_type->array.length * ZEND_FFI_TYPE(nested_type->array.type)->size;
  6249. nested_type->align = ZEND_FFI_TYPE(nested_type->array.type)->align;
  6250. return SUCCESS;
  6251. break;
  6252. case ZEND_FFI_TYPE_FUNC:
  6253. /* "char" is used as a terminator of nested declaration */
  6254. if (nested_type->func.ret_type == &zend_ffi_type_char) {
  6255. nested_type->func.ret_type = type;
  6256. return zend_ffi_validate_func_ret_type(ZEND_FFI_TYPE(type));
  6257. } else {
  6258. return zend_ffi_nested_type(type, nested_type->func.ret_type);
  6259. }
  6260. break;
  6261. default:
  6262. ZEND_UNREACHABLE();
  6263. }
  6264. }
  6265. /* }}} */
  6266. void zend_ffi_nested_declaration(zend_ffi_dcl *dcl, zend_ffi_dcl *nested_dcl) /* {{{ */
  6267. {
  6268. /* "char" is used as a terminator of nested declaration */
  6269. zend_ffi_finalize_type(dcl);
  6270. if (!nested_dcl->type || nested_dcl->type == &zend_ffi_type_char) {
  6271. nested_dcl->type = dcl->type;
  6272. } else {
  6273. if (zend_ffi_nested_type(dcl->type, nested_dcl->type) != SUCCESS) {
  6274. zend_ffi_cleanup_dcl(nested_dcl);
  6275. LONGJMP(FFI_G(bailout), FAILURE);
  6276. }
  6277. }
  6278. dcl->type = nested_dcl->type;
  6279. }
  6280. /* }}} */
  6281. void zend_ffi_align_as_type(zend_ffi_dcl *dcl, zend_ffi_dcl *align_dcl) /* {{{ */
  6282. {
  6283. zend_ffi_finalize_type(align_dcl);
  6284. dcl->align = MAX(align_dcl->align, ZEND_FFI_TYPE(align_dcl->type)->align);
  6285. }
  6286. /* }}} */
  6287. void zend_ffi_align_as_val(zend_ffi_dcl *dcl, zend_ffi_val *align_val) /* {{{ */
  6288. {
  6289. switch (align_val->kind) {
  6290. case ZEND_FFI_VAL_INT32:
  6291. case ZEND_FFI_VAL_UINT32:
  6292. dcl->align = zend_ffi_type_uint32.align;
  6293. break;
  6294. case ZEND_FFI_VAL_INT64:
  6295. case ZEND_FFI_VAL_UINT64:
  6296. dcl->align = zend_ffi_type_uint64.align;
  6297. break;
  6298. case ZEND_FFI_VAL_FLOAT:
  6299. dcl->align = zend_ffi_type_float.align;
  6300. break;
  6301. case ZEND_FFI_VAL_DOUBLE:
  6302. dcl->align = zend_ffi_type_double.align;
  6303. break;
  6304. #ifdef HAVE_LONG_DOUBLE
  6305. case ZEND_FFI_VAL_LONG_DOUBLE:
  6306. dcl->align = zend_ffi_type_long_double.align;
  6307. break;
  6308. #endif
  6309. case ZEND_FFI_VAL_CHAR:
  6310. case ZEND_FFI_VAL_STRING:
  6311. dcl->align = zend_ffi_type_char.align;
  6312. break;
  6313. default:
  6314. break;
  6315. }
  6316. }
  6317. /* }}} */
  6318. #define zend_ffi_expr_bool(val) do { \
  6319. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) { \
  6320. val->kind = ZEND_FFI_VAL_INT32; \
  6321. val->i64 = !!val->u64; \
  6322. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) { \
  6323. val->kind = ZEND_FFI_VAL_INT32; \
  6324. val->i64 = !!val->i64; \
  6325. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6326. val->kind = ZEND_FFI_VAL_INT32; \
  6327. val->i64 = !!val->d; \
  6328. } else if (val->kind == ZEND_FFI_VAL_CHAR) { \
  6329. val->kind = ZEND_FFI_VAL_INT32; \
  6330. val->i64 = !!val->ch; \
  6331. } else { \
  6332. val->kind = ZEND_FFI_VAL_ERROR; \
  6333. } \
  6334. } while (0)
  6335. #define zend_ffi_expr_math(val, op2, OP) do { \
  6336. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) { \
  6337. if (op2->kind == ZEND_FFI_VAL_UINT32 || op2->kind == ZEND_FFI_VAL_UINT64) { \
  6338. val->kind = MAX(val->kind, op2->kind); \
  6339. val->u64 = val->u64 OP op2->u64; \
  6340. } else if (op2->kind == ZEND_FFI_VAL_INT32) { \
  6341. val->u64 = val->u64 OP op2->i64; \
  6342. } else if (op2->kind == ZEND_FFI_VAL_INT64) { \
  6343. val->u64 = val->u64 OP op2->i64; \
  6344. } else if (op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6345. val->kind = op2->kind; \
  6346. val->d = (zend_ffi_double)val->u64 OP op2->d; \
  6347. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6348. val->u64 = val->u64 OP op2->ch; \
  6349. } else { \
  6350. val->kind = ZEND_FFI_VAL_ERROR; \
  6351. } \
  6352. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) { \
  6353. if (op2->kind == ZEND_FFI_VAL_UINT32) { \
  6354. val->i64 = val->i64 OP op2->u64; \
  6355. } else if (op2->kind == ZEND_FFI_VAL_UINT64) { \
  6356. val->i64 = val->i64 OP op2->u64; \
  6357. } else if (op2->kind == ZEND_FFI_VAL_INT32 || op2->kind == ZEND_FFI_VAL_INT64) { \
  6358. val->kind = MAX(val->kind, op2->kind); \
  6359. val->i64 = val->i64 OP op2->i64; \
  6360. } else if (op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6361. val->kind = op2->kind; \
  6362. val->d = (zend_ffi_double)val->i64 OP op2->d; \
  6363. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6364. val->i64 = val->i64 OP op2->ch; \
  6365. } else { \
  6366. val->kind = ZEND_FFI_VAL_ERROR; \
  6367. } \
  6368. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6369. if (op2->kind == ZEND_FFI_VAL_UINT32 || op2->kind == ZEND_FFI_VAL_UINT64) { \
  6370. val->d = val->d OP (zend_ffi_double)op2->u64; \
  6371. } else if (op2->kind == ZEND_FFI_VAL_INT32 ||op2->kind == ZEND_FFI_VAL_INT64) { \
  6372. val->d = val->d OP (zend_ffi_double)op2->i64; \
  6373. } else if (op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6374. val->kind = MAX(val->kind, op2->kind); \
  6375. val->d = val->d OP op2->d; \
  6376. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6377. val->d = val->d OP (zend_ffi_double)op2->ch; \
  6378. } else { \
  6379. val->kind = ZEND_FFI_VAL_ERROR; \
  6380. } \
  6381. } else if (val->kind == ZEND_FFI_VAL_CHAR) { \
  6382. if (op2->kind == ZEND_FFI_VAL_UINT32 || op2->kind == ZEND_FFI_VAL_UINT64) { \
  6383. val->kind = op2->kind; \
  6384. val->u64 = val->ch OP op2->u64; \
  6385. } else if (op2->kind == ZEND_FFI_VAL_INT32 || op2->kind == ZEND_FFI_VAL_INT64) { \
  6386. val->kind = ZEND_FFI_VAL_INT64; \
  6387. val->i64 = val->ch OP op2->i64; \
  6388. } else if (op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6389. val->kind = op2->kind; \
  6390. val->d = (zend_ffi_double)val->ch OP op2->d; \
  6391. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6392. val->ch = val->ch OP op2->ch; \
  6393. } else { \
  6394. val->kind = ZEND_FFI_VAL_ERROR; \
  6395. } \
  6396. } else { \
  6397. val->kind = ZEND_FFI_VAL_ERROR; \
  6398. } \
  6399. } while (0)
  6400. #define zend_ffi_expr_int_math(val, op2, OP) do { \
  6401. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) { \
  6402. if (op2->kind == ZEND_FFI_VAL_UINT32 || op2->kind == ZEND_FFI_VAL_UINT64) { \
  6403. val->kind = MAX(val->kind, op2->kind); \
  6404. val->u64 = val->u64 OP op2->u64; \
  6405. } else if (op2->kind == ZEND_FFI_VAL_INT32) { \
  6406. val->u64 = val->u64 OP op2->i64; \
  6407. } else if (op2->kind == ZEND_FFI_VAL_INT64) { \
  6408. val->u64 = val->u64 OP op2->i64; \
  6409. } else if (op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6410. val->u64 = val->u64 OP (uint64_t)op2->d; \
  6411. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6412. val->u64 = val->u64 OP op2->ch; \
  6413. } else { \
  6414. val->kind = ZEND_FFI_VAL_ERROR; \
  6415. } \
  6416. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) { \
  6417. if (op2->kind == ZEND_FFI_VAL_UINT32) { \
  6418. val->i64 = val->i64 OP op2->u64; \
  6419. } else if (op2->kind == ZEND_FFI_VAL_UINT64) { \
  6420. val->i64 = val->i64 OP op2->u64; \
  6421. } else if (op2->kind == ZEND_FFI_VAL_INT32 || op2->kind == ZEND_FFI_VAL_INT64) { \
  6422. val->kind = MAX(val->kind, op2->kind); \
  6423. val->i64 = val->i64 OP op2->i64; \
  6424. } else if (op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6425. val->u64 = val->u64 OP (int64_t)op2->d; \
  6426. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6427. val->i64 = val->i64 OP op2->ch; \
  6428. } else { \
  6429. val->kind = ZEND_FFI_VAL_ERROR; \
  6430. } \
  6431. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6432. if (op2->kind == ZEND_FFI_VAL_UINT32 || op2->kind == ZEND_FFI_VAL_UINT64) { \
  6433. val->kind = op2->kind; \
  6434. val->u64 = (uint64_t)val->d OP op2->u64; \
  6435. } else if (op2->kind == ZEND_FFI_VAL_INT32 || op2->kind == ZEND_FFI_VAL_INT64) { \
  6436. val->kind = op2->kind; \
  6437. val->i64 = (int64_t)val->d OP op2->i64; \
  6438. } else { \
  6439. val->kind = ZEND_FFI_VAL_ERROR; \
  6440. } \
  6441. } else if (val->kind == ZEND_FFI_VAL_CHAR) { \
  6442. if (op2->kind == ZEND_FFI_VAL_UINT32 || op2->kind == ZEND_FFI_VAL_UINT64) { \
  6443. val->kind = op2->kind; \
  6444. val->u64 = (uint64_t)val->ch OP op2->u64; \
  6445. } else if (op2->kind == ZEND_FFI_VAL_INT32 || op2->kind == ZEND_FFI_VAL_INT64) { \
  6446. val->kind = op2->kind; \
  6447. val->i64 = (int64_t)val->ch OP op2->u64; \
  6448. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6449. val->ch = val->ch OP op2->ch; \
  6450. } else { \
  6451. val->kind = ZEND_FFI_VAL_ERROR; \
  6452. } \
  6453. } else { \
  6454. val->kind = ZEND_FFI_VAL_ERROR; \
  6455. } \
  6456. } while (0)
  6457. #define zend_ffi_expr_cmp(val, op2, OP) do { \
  6458. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) { \
  6459. if (op2->kind == ZEND_FFI_VAL_UINT32 || op2->kind == ZEND_FFI_VAL_UINT64) { \
  6460. val->kind = ZEND_FFI_VAL_INT32; \
  6461. val->i64 = val->u64 OP op2->u64; \
  6462. } else if (op2->kind == ZEND_FFI_VAL_INT32 || op2->kind == ZEND_FFI_VAL_INT64) { \
  6463. val->kind = ZEND_FFI_VAL_INT32; \
  6464. val->i64 = val->u64 OP op2->u64; /*signed/unsigned */ \
  6465. } else if (op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6466. val->kind = ZEND_FFI_VAL_INT32; \
  6467. val->i64 = (zend_ffi_double)val->u64 OP op2->d; \
  6468. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6469. val->kind = ZEND_FFI_VAL_INT32; \
  6470. val->i64 = val->u64 OP op2->d; \
  6471. } else { \
  6472. val->kind = ZEND_FFI_VAL_ERROR; \
  6473. } \
  6474. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) { \
  6475. if (op2->kind == ZEND_FFI_VAL_UINT32 || op2->kind == ZEND_FFI_VAL_UINT64) { \
  6476. val->kind = ZEND_FFI_VAL_INT32; \
  6477. val->i64 = val->i64 OP op2->i64; /* signed/unsigned */ \
  6478. } else if (op2->kind == ZEND_FFI_VAL_INT32 || op2->kind == ZEND_FFI_VAL_INT64) { \
  6479. val->kind = ZEND_FFI_VAL_INT32; \
  6480. val->i64 = val->i64 OP op2->i64; \
  6481. } else if (op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6482. val->kind = ZEND_FFI_VAL_INT32; \
  6483. val->i64 = (zend_ffi_double)val->i64 OP op2->d; \
  6484. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6485. val->kind = ZEND_FFI_VAL_INT32; \
  6486. val->i64 = val->i64 OP op2->ch; \
  6487. } else { \
  6488. val->kind = ZEND_FFI_VAL_ERROR; \
  6489. } \
  6490. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6491. if (op2->kind == ZEND_FFI_VAL_UINT32 || op2->kind == ZEND_FFI_VAL_UINT64) { \
  6492. val->kind = ZEND_FFI_VAL_INT32; \
  6493. val->i64 = val->d OP (zend_ffi_double)op2->u64; \
  6494. } else if (op2->kind == ZEND_FFI_VAL_INT32 ||op2->kind == ZEND_FFI_VAL_INT64) { \
  6495. val->kind = ZEND_FFI_VAL_INT32; \
  6496. val->i64 = val->d OP (zend_ffi_double)op2->i64; \
  6497. } else if (op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6498. val->kind = ZEND_FFI_VAL_INT32; \
  6499. val->i64 = val->d OP op2->d; \
  6500. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6501. val->kind = ZEND_FFI_VAL_INT32; \
  6502. val->i64 = val->d OP (zend_ffi_double)op2->ch; \
  6503. } else { \
  6504. val->kind = ZEND_FFI_VAL_ERROR; \
  6505. } \
  6506. } else if (val->kind == ZEND_FFI_VAL_CHAR) { \
  6507. if (op2->kind == ZEND_FFI_VAL_UINT32 || op2->kind == ZEND_FFI_VAL_UINT64) { \
  6508. val->kind = ZEND_FFI_VAL_INT32; \
  6509. val->i64 = val->ch OP op2->i64; /* signed/unsigned */ \
  6510. } else if (op2->kind == ZEND_FFI_VAL_INT32 || op2->kind == ZEND_FFI_VAL_INT64) { \
  6511. val->kind = ZEND_FFI_VAL_INT32; \
  6512. val->i64 = val->ch OP op2->i64; \
  6513. } else if (op2->kind == ZEND_FFI_VAL_FLOAT || op2->kind == ZEND_FFI_VAL_DOUBLE || op2->kind == ZEND_FFI_VAL_LONG_DOUBLE) { \
  6514. val->kind = ZEND_FFI_VAL_INT32; \
  6515. val->i64 = (zend_ffi_double)val->ch OP op2->d; \
  6516. } else if (op2->kind == ZEND_FFI_VAL_CHAR) { \
  6517. val->kind = ZEND_FFI_VAL_INT32; \
  6518. val->i64 = val->ch OP op2->ch; \
  6519. } else { \
  6520. val->kind = ZEND_FFI_VAL_ERROR; \
  6521. } \
  6522. } else { \
  6523. val->kind = ZEND_FFI_VAL_ERROR; \
  6524. } \
  6525. } while (0)
  6526. void zend_ffi_expr_conditional(zend_ffi_val *val, zend_ffi_val *op2, zend_ffi_val *op3) /* {{{ */
  6527. {
  6528. zend_ffi_expr_bool(val);
  6529. if (val->kind == ZEND_FFI_VAL_INT32) {
  6530. if (val->i64) {
  6531. *val = *op2;
  6532. } else {
  6533. *val = *op3;
  6534. }
  6535. }
  6536. }
  6537. /* }}} */
  6538. void zend_ffi_expr_bool_or(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6539. {
  6540. zend_ffi_expr_bool(val);
  6541. zend_ffi_expr_bool(op2);
  6542. if (val->kind == ZEND_FFI_VAL_INT32 && op2->kind == ZEND_FFI_VAL_INT32) {
  6543. val->i64 = val->i64 || op2->i64;
  6544. } else {
  6545. val->kind = ZEND_FFI_VAL_ERROR;
  6546. }
  6547. }
  6548. /* }}} */
  6549. void zend_ffi_expr_bool_and(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6550. {
  6551. zend_ffi_expr_bool(val);
  6552. zend_ffi_expr_bool(op2);
  6553. if (val->kind == ZEND_FFI_VAL_INT32 && op2->kind == ZEND_FFI_VAL_INT32) {
  6554. val->i64 = val->i64 && op2->i64;
  6555. } else {
  6556. val->kind = ZEND_FFI_VAL_ERROR;
  6557. }
  6558. }
  6559. /* }}} */
  6560. void zend_ffi_expr_bw_or(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6561. {
  6562. zend_ffi_expr_int_math(val, op2, |);
  6563. }
  6564. /* }}} */
  6565. void zend_ffi_expr_bw_xor(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6566. {
  6567. zend_ffi_expr_int_math(val, op2, ^);
  6568. }
  6569. /* }}} */
  6570. void zend_ffi_expr_bw_and(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6571. {
  6572. zend_ffi_expr_int_math(val, op2, &);
  6573. }
  6574. /* }}} */
  6575. void zend_ffi_expr_is_equal(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6576. {
  6577. zend_ffi_expr_cmp(val, op2, ==);
  6578. }
  6579. /* }}} */
  6580. void zend_ffi_expr_is_not_equal(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6581. {
  6582. zend_ffi_expr_cmp(val, op2, !=);
  6583. }
  6584. /* }}} */
  6585. void zend_ffi_expr_is_less(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6586. {
  6587. zend_ffi_expr_cmp(val, op2, <);
  6588. }
  6589. /* }}} */
  6590. void zend_ffi_expr_is_greater(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6591. {
  6592. zend_ffi_expr_cmp(val, op2, >);
  6593. }
  6594. /* }}} */
  6595. void zend_ffi_expr_is_less_or_equal(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6596. {
  6597. zend_ffi_expr_cmp(val, op2, <=);
  6598. }
  6599. /* }}} */
  6600. void zend_ffi_expr_is_greater_or_equal(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6601. {
  6602. zend_ffi_expr_cmp(val, op2, >=);
  6603. }
  6604. /* }}} */
  6605. void zend_ffi_expr_shift_left(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6606. {
  6607. zend_ffi_expr_int_math(val, op2, <<);
  6608. }
  6609. /* }}} */
  6610. void zend_ffi_expr_shift_right(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6611. {
  6612. zend_ffi_expr_int_math(val, op2, >>);
  6613. }
  6614. /* }}} */
  6615. void zend_ffi_expr_add(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6616. {
  6617. zend_ffi_expr_math(val, op2, +);
  6618. }
  6619. /* }}} */
  6620. void zend_ffi_expr_sub(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6621. {
  6622. zend_ffi_expr_math(val, op2, -);
  6623. }
  6624. /* }}} */
  6625. void zend_ffi_expr_mul(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6626. {
  6627. zend_ffi_expr_math(val, op2, *);
  6628. }
  6629. /* }}} */
  6630. void zend_ffi_expr_div(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6631. {
  6632. zend_ffi_expr_math(val, op2, /);
  6633. }
  6634. /* }}} */
  6635. void zend_ffi_expr_mod(zend_ffi_val *val, zend_ffi_val *op2) /* {{{ */
  6636. {
  6637. zend_ffi_expr_int_math(val, op2, %); // ???
  6638. }
  6639. /* }}} */
  6640. void zend_ffi_expr_cast(zend_ffi_val *val, zend_ffi_dcl *dcl) /* {{{ */
  6641. {
  6642. zend_ffi_finalize_type(dcl);
  6643. switch (ZEND_FFI_TYPE(dcl->type)->kind) {
  6644. case ZEND_FFI_TYPE_FLOAT:
  6645. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) {
  6646. val->kind = ZEND_FFI_VAL_FLOAT;
  6647. val->d = val->u64;
  6648. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6649. val->kind = ZEND_FFI_VAL_FLOAT;
  6650. val->d = val->i64;
  6651. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6652. val->kind = ZEND_FFI_VAL_FLOAT;
  6653. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6654. val->kind = ZEND_FFI_VAL_FLOAT;
  6655. val->d = val->ch;
  6656. } else {
  6657. val->kind = ZEND_FFI_VAL_ERROR;
  6658. }
  6659. break;
  6660. case ZEND_FFI_TYPE_DOUBLE:
  6661. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) {
  6662. val->kind = ZEND_FFI_VAL_DOUBLE;
  6663. val->d = val->u64;
  6664. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6665. val->kind = ZEND_FFI_VAL_DOUBLE;
  6666. val->d = val->i64;
  6667. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6668. val->kind = ZEND_FFI_VAL_DOUBLE;
  6669. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6670. val->kind = ZEND_FFI_VAL_DOUBLE;
  6671. val->d = val->ch;
  6672. } else {
  6673. val->kind = ZEND_FFI_VAL_ERROR;
  6674. }
  6675. break;
  6676. #ifdef HAVE_LONG_DOUBLE
  6677. case ZEND_FFI_TYPE_LONGDOUBLE:
  6678. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) {
  6679. val->kind = ZEND_FFI_VAL_LONG_DOUBLE;
  6680. val->d = val->u64;
  6681. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6682. val->kind = ZEND_FFI_VAL_LONG_DOUBLE;
  6683. val->d = val->i64;
  6684. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6685. val->kind = ZEND_FFI_VAL_LONG_DOUBLE;
  6686. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6687. val->kind = ZEND_FFI_VAL_LONG_DOUBLE;
  6688. val->d = val->ch;
  6689. } else {
  6690. val->kind = ZEND_FFI_VAL_ERROR;
  6691. }
  6692. break;
  6693. #endif
  6694. case ZEND_FFI_TYPE_UINT8:
  6695. case ZEND_FFI_TYPE_UINT16:
  6696. case ZEND_FFI_TYPE_UINT32:
  6697. case ZEND_FFI_TYPE_BOOL:
  6698. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64 || val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6699. val->kind = ZEND_FFI_VAL_UINT32;
  6700. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6701. val->kind = ZEND_FFI_VAL_UINT32;
  6702. val->u64 = val->d;
  6703. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6704. val->kind = ZEND_FFI_VAL_UINT32;
  6705. val->u64 = val->ch;
  6706. } else {
  6707. val->kind = ZEND_FFI_VAL_ERROR;
  6708. }
  6709. break;
  6710. case ZEND_FFI_TYPE_SINT8:
  6711. case ZEND_FFI_TYPE_SINT16:
  6712. case ZEND_FFI_TYPE_SINT32:
  6713. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64 || val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6714. val->kind = ZEND_FFI_VAL_INT32;
  6715. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6716. val->kind = ZEND_FFI_VAL_INT32;
  6717. val->i64 = val->d;
  6718. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6719. val->kind = ZEND_FFI_VAL_INT32;
  6720. val->i64 = val->ch;
  6721. } else {
  6722. val->kind = ZEND_FFI_VAL_ERROR;
  6723. }
  6724. break;
  6725. case ZEND_FFI_TYPE_UINT64:
  6726. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64 || val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6727. val->kind = ZEND_FFI_VAL_UINT64;
  6728. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6729. val->kind = ZEND_FFI_VAL_UINT64;
  6730. val->u64 = val->d;
  6731. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6732. val->kind = ZEND_FFI_VAL_UINT64;
  6733. val->u64 = val->ch;
  6734. } else {
  6735. val->kind = ZEND_FFI_VAL_ERROR;
  6736. }
  6737. break;
  6738. case ZEND_FFI_TYPE_SINT64:
  6739. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) {
  6740. val->kind = ZEND_FFI_VAL_CHAR;
  6741. val->ch = val->u64;
  6742. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6743. val->kind = ZEND_FFI_VAL_CHAR;
  6744. val->ch = val->i64;
  6745. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6746. val->kind = ZEND_FFI_VAL_CHAR;
  6747. val->ch = val->d;
  6748. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6749. } else {
  6750. val->kind = ZEND_FFI_VAL_ERROR;
  6751. }
  6752. break;
  6753. case ZEND_FFI_TYPE_CHAR:
  6754. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64 || val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6755. val->kind = ZEND_FFI_VAL_UINT32;
  6756. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6757. val->kind = ZEND_FFI_VAL_UINT32;
  6758. val->u64 = val->d;
  6759. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6760. val->kind = ZEND_FFI_VAL_UINT32;
  6761. val->u64 = val->ch;
  6762. } else {
  6763. val->kind = ZEND_FFI_VAL_ERROR;
  6764. }
  6765. break;
  6766. default:
  6767. val->kind = ZEND_FFI_VAL_ERROR;
  6768. break;
  6769. }
  6770. zend_ffi_type_dtor(dcl->type);
  6771. }
  6772. /* }}} */
  6773. void zend_ffi_expr_plus(zend_ffi_val *val) /* {{{ */
  6774. {
  6775. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) {
  6776. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6777. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6778. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6779. } else {
  6780. val->kind = ZEND_FFI_VAL_ERROR;
  6781. }
  6782. }
  6783. /* }}} */
  6784. void zend_ffi_expr_neg(zend_ffi_val *val) /* {{{ */
  6785. {
  6786. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) {
  6787. val->u64 = -val->u64;
  6788. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6789. val->i64 = -val->i64;
  6790. } else if (val->kind == ZEND_FFI_VAL_FLOAT || val->kind == ZEND_FFI_VAL_DOUBLE || val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6791. val->d = -val->d;
  6792. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6793. val->ch = -val->ch;
  6794. } else {
  6795. val->kind = ZEND_FFI_VAL_ERROR;
  6796. }
  6797. }
  6798. /* }}} */
  6799. void zend_ffi_expr_bw_not(zend_ffi_val *val) /* {{{ */
  6800. {
  6801. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_UINT64) {
  6802. val->u64 = ~val->u64;
  6803. } else if (val->kind == ZEND_FFI_VAL_INT32 || val->kind == ZEND_FFI_VAL_INT64) {
  6804. val->i64 = ~val->i64;
  6805. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6806. val->ch = ~val->ch;
  6807. } else {
  6808. val->kind = ZEND_FFI_VAL_ERROR;
  6809. }
  6810. }
  6811. /* }}} */
  6812. void zend_ffi_expr_bool_not(zend_ffi_val *val) /* {{{ */
  6813. {
  6814. zend_ffi_expr_bool(val);
  6815. if (val->kind == ZEND_FFI_VAL_INT32) {
  6816. val->i64 = !val->i64;
  6817. }
  6818. }
  6819. /* }}} */
  6820. void zend_ffi_expr_sizeof_val(zend_ffi_val *val) /* {{{ */
  6821. {
  6822. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_INT32) {
  6823. val->kind = ZEND_FFI_VAL_UINT32;
  6824. val->u64 = zend_ffi_type_uint32.size;
  6825. } else if (val->kind == ZEND_FFI_VAL_UINT64 || val->kind == ZEND_FFI_VAL_INT64) {
  6826. val->kind = ZEND_FFI_VAL_UINT32;
  6827. val->u64 = zend_ffi_type_uint64.size;
  6828. } else if (val->kind == ZEND_FFI_VAL_FLOAT) {
  6829. val->kind = ZEND_FFI_VAL_UINT32;
  6830. val->u64 = zend_ffi_type_float.size;
  6831. } else if (val->kind == ZEND_FFI_VAL_DOUBLE) {
  6832. val->kind = ZEND_FFI_VAL_UINT32;
  6833. val->u64 = zend_ffi_type_double.size;
  6834. } else if (val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6835. val->kind = ZEND_FFI_VAL_UINT32;
  6836. #ifdef _WIN32
  6837. val->u64 = zend_ffi_type_double.size;
  6838. #else
  6839. val->u64 = zend_ffi_type_long_double.size;
  6840. #endif
  6841. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6842. val->kind = ZEND_FFI_VAL_UINT32;
  6843. val->u64 = zend_ffi_type_char.size;
  6844. } else if (val->kind == ZEND_FFI_VAL_STRING) {
  6845. if (memchr(val->str, '\\', val->len)) {
  6846. // TODO: support for escape sequences ???
  6847. val->kind = ZEND_FFI_VAL_ERROR;
  6848. } else {
  6849. val->kind = ZEND_FFI_VAL_UINT32;
  6850. val->u64 = val->len + 1;
  6851. }
  6852. } else {
  6853. val->kind = ZEND_FFI_VAL_ERROR;
  6854. }
  6855. }
  6856. /* }}} */
  6857. void zend_ffi_expr_sizeof_type(zend_ffi_val *val, zend_ffi_dcl *dcl) /* {{{ */
  6858. {
  6859. zend_ffi_type *type;
  6860. zend_ffi_finalize_type(dcl);
  6861. type = ZEND_FFI_TYPE(dcl->type);
  6862. val->kind = (type->size > 0xffffffff) ? ZEND_FFI_VAL_UINT64 : ZEND_FFI_VAL_UINT32;
  6863. val->u64 = type->size;
  6864. zend_ffi_type_dtor(dcl->type);
  6865. }
  6866. /* }}} */
  6867. void zend_ffi_expr_alignof_val(zend_ffi_val *val) /* {{{ */
  6868. {
  6869. if (val->kind == ZEND_FFI_VAL_UINT32 || val->kind == ZEND_FFI_VAL_INT32) {
  6870. val->kind = ZEND_FFI_VAL_UINT32;
  6871. val->u64 = zend_ffi_type_uint32.align;
  6872. } else if (val->kind == ZEND_FFI_VAL_UINT64 || val->kind == ZEND_FFI_VAL_INT64) {
  6873. val->kind = ZEND_FFI_VAL_UINT32;
  6874. val->u64 = zend_ffi_type_uint64.align;
  6875. } else if (val->kind == ZEND_FFI_VAL_FLOAT) {
  6876. val->kind = ZEND_FFI_VAL_UINT32;
  6877. val->u64 = zend_ffi_type_float.align;
  6878. } else if (val->kind == ZEND_FFI_VAL_DOUBLE) {
  6879. val->kind = ZEND_FFI_VAL_UINT32;
  6880. val->u64 = zend_ffi_type_double.align;
  6881. #ifdef HAVE_LONG_DOUBLE
  6882. } else if (val->kind == ZEND_FFI_VAL_LONG_DOUBLE) {
  6883. val->kind = ZEND_FFI_VAL_UINT32;
  6884. val->u64 = zend_ffi_type_long_double.align;
  6885. #endif
  6886. } else if (val->kind == ZEND_FFI_VAL_CHAR) {
  6887. val->kind = ZEND_FFI_VAL_UINT32;
  6888. val->u64 = zend_ffi_type_char.size;
  6889. } else if (val->kind == ZEND_FFI_VAL_STRING) {
  6890. val->kind = ZEND_FFI_VAL_UINT32;
  6891. val->u64 = _Alignof(char*);
  6892. } else {
  6893. val->kind = ZEND_FFI_VAL_ERROR;
  6894. }
  6895. }
  6896. /* }}} */
  6897. void zend_ffi_expr_alignof_type(zend_ffi_val *val, zend_ffi_dcl *dcl) /* {{{ */
  6898. {
  6899. zend_ffi_finalize_type(dcl);
  6900. val->kind = ZEND_FFI_VAL_UINT32;
  6901. val->u64 = ZEND_FFI_TYPE(dcl->type)->align;
  6902. zend_ffi_type_dtor(dcl->type);
  6903. }
  6904. /* }}} */
  6905. void zend_ffi_val_number(zend_ffi_val *val, int base, const char *str, size_t str_len) /* {{{ */
  6906. {
  6907. int u = 0;
  6908. int l = 0;
  6909. if (str[str_len-1] == 'u' || str[str_len-1] == 'U') {
  6910. u = 1;
  6911. if (str[str_len-2] == 'l' || str[str_len-2] == 'L') {
  6912. l = 1;
  6913. if (str[str_len-3] == 'l' || str[str_len-3] == 'L') {
  6914. l = 2;
  6915. }
  6916. }
  6917. } else if (str[str_len-1] == 'l' || str[str_len-1] == 'L') {
  6918. l = 1;
  6919. if (str[str_len-2] == 'l' || str[str_len-2] == 'L') {
  6920. l = 2;
  6921. if (str[str_len-3] == 'u' || str[str_len-3] == 'U') {
  6922. u = 1;
  6923. }
  6924. } else if (str[str_len-2] == 'u' || str[str_len-2] == 'U') {
  6925. u = 1;
  6926. }
  6927. }
  6928. if (u) {
  6929. val->u64 = strtoull(str, NULL, base);
  6930. if (l == 0) {
  6931. val->kind = ZEND_FFI_VAL_UINT32;
  6932. } else if (l == 1) {
  6933. val->kind = (sizeof(long) == 4) ? ZEND_FFI_VAL_UINT32 : ZEND_FFI_VAL_UINT64;
  6934. } else if (l == 2) {
  6935. val->kind = ZEND_FFI_VAL_UINT64;
  6936. }
  6937. } else {
  6938. val->i64 = strtoll(str, NULL, base);
  6939. if (l == 0) {
  6940. val->kind = ZEND_FFI_VAL_INT32;
  6941. } else if (l == 1) {
  6942. val->kind = (sizeof(long) == 4) ? ZEND_FFI_VAL_INT32 : ZEND_FFI_VAL_INT64;
  6943. } else if (l == 2) {
  6944. val->kind = ZEND_FFI_VAL_INT64;
  6945. }
  6946. }
  6947. }
  6948. /* }}} */
  6949. void zend_ffi_val_float_number(zend_ffi_val *val, const char *str, size_t str_len) /* {{{ */
  6950. {
  6951. val->d = strtold(str, NULL);
  6952. if (str[str_len-1] == 'f' || str[str_len-1] == 'F') {
  6953. val->kind = ZEND_FFI_VAL_FLOAT;
  6954. } else if (str[str_len-1] == 'l' || str[str_len-1] == 'L') {
  6955. val->kind = ZEND_FFI_VAL_LONG_DOUBLE;
  6956. } else {
  6957. val->kind = ZEND_FFI_VAL_DOUBLE;
  6958. }
  6959. }
  6960. /* }}} */
  6961. void zend_ffi_val_string(zend_ffi_val *val, const char *str, size_t str_len) /* {{{ */
  6962. {
  6963. if (str[0] != '\"') {
  6964. val->kind = ZEND_FFI_VAL_ERROR;
  6965. } else {
  6966. val->kind = ZEND_FFI_VAL_STRING;
  6967. val->str = str + 1;
  6968. val->len = str_len - 2;
  6969. }
  6970. }
  6971. /* }}} */
  6972. void zend_ffi_val_character(zend_ffi_val *val, const char *str, size_t str_len) /* {{{ */
  6973. {
  6974. int n;
  6975. if (str[0] != '\'') {
  6976. val->kind = ZEND_FFI_VAL_ERROR;
  6977. } else {
  6978. val->kind = ZEND_FFI_VAL_CHAR;
  6979. if (str_len == 3) {
  6980. val->ch = str[1];
  6981. } else if (str[1] == '\\') {
  6982. if (str[2] == 'a') {
  6983. } else if (str[2] == 'b' && str_len == 4) {
  6984. val->ch = '\b';
  6985. } else if (str[2] == 'f' && str_len == 4) {
  6986. val->ch = '\f';
  6987. } else if (str[2] == 'n' && str_len == 4) {
  6988. val->ch = '\n';
  6989. } else if (str[2] == 'r' && str_len == 4) {
  6990. val->ch = '\r';
  6991. } else if (str[2] == 't' && str_len == 4) {
  6992. val->ch = '\t';
  6993. } else if (str[2] == 'v' && str_len == 4) {
  6994. val->ch = '\v';
  6995. } else if (str[2] >= '0' && str[2] <= '7') {
  6996. n = str[2] - '0';
  6997. if (str[3] >= '0' && str[3] <= '7') {
  6998. n = n * 8 + (str[3] - '0');
  6999. if ((str[4] >= '0' && str[4] <= '7') && str_len == 6) {
  7000. n = n * 8 + (str[4] - '0');
  7001. } else if (str_len != 5) {
  7002. val->kind = ZEND_FFI_VAL_ERROR;
  7003. }
  7004. } else if (str_len != 4) {
  7005. val->kind = ZEND_FFI_VAL_ERROR;
  7006. }
  7007. if (n <= 0xff) {
  7008. val->ch = n;
  7009. } else {
  7010. val->kind = ZEND_FFI_VAL_ERROR;
  7011. }
  7012. } else if (str[2] == 'x') {
  7013. if (str[3] >= '0' && str[3] <= '9') {
  7014. n = str[3] - '0';
  7015. } else if (str[3] >= 'A' && str[3] <= 'F') {
  7016. n = str[3] - 'A';
  7017. } else if (str[3] >= 'a' && str[3] <= 'f') {
  7018. n = str[3] - 'a';
  7019. } else {
  7020. val->kind = ZEND_FFI_VAL_ERROR;
  7021. return;
  7022. }
  7023. if ((str[4] >= '0' && str[4] <= '9') && str_len == 6) {
  7024. n = n * 16 + (str[4] - '0');
  7025. } else if ((str[4] >= 'A' && str[4] <= 'F') && str_len == 6) {
  7026. n = n * 16 + (str[4] - 'A');
  7027. } else if ((str[4] >= 'a' && str[4] <= 'f') && str_len == 6) {
  7028. n = n * 16 + (str[4] - 'a');
  7029. } else if (str_len != 5) {
  7030. val->kind = ZEND_FFI_VAL_ERROR;
  7031. return;
  7032. }
  7033. val->ch = n;
  7034. } else if (str_len == 4) {
  7035. val->ch = str[2];
  7036. } else {
  7037. val->kind = ZEND_FFI_VAL_ERROR;
  7038. }
  7039. } else {
  7040. val->kind = ZEND_FFI_VAL_ERROR;
  7041. }
  7042. }
  7043. }
  7044. /* }}} */