zend_execute.c 158 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #define ZEND_INTENSIVE_DEBUGGING 0
  21. #include <stdio.h>
  22. #include <signal.h>
  23. #include "zend.h"
  24. #include "zend_compile.h"
  25. #include "zend_execute.h"
  26. #include "zend_API.h"
  27. #include "zend_ptr_stack.h"
  28. #include "zend_constants.h"
  29. #include "zend_extensions.h"
  30. #include "zend_ini.h"
  31. #include "zend_exceptions.h"
  32. #include "zend_interfaces.h"
  33. #include "zend_closures.h"
  34. #include "zend_generators.h"
  35. #include "zend_vm.h"
  36. #include "zend_dtrace.h"
  37. #include "zend_inheritance.h"
  38. #include "zend_type_info.h"
  39. #include "zend_smart_str.h"
  40. #include "zend_observer.h"
  41. #include "zend_system_id.h"
  42. #include "Optimizer/zend_func_info.h"
  43. /* Virtual current working directory support */
  44. #include "zend_virtual_cwd.h"
  45. #ifdef HAVE_GCC_GLOBAL_REGS
  46. # if defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(i386)
  47. # define ZEND_VM_FP_GLOBAL_REG "%esi"
  48. # define ZEND_VM_IP_GLOBAL_REG "%edi"
  49. # elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__x86_64__)
  50. # define ZEND_VM_FP_GLOBAL_REG "%r14"
  51. # define ZEND_VM_IP_GLOBAL_REG "%r15"
  52. # elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__powerpc64__)
  53. # define ZEND_VM_FP_GLOBAL_REG "r14"
  54. # define ZEND_VM_IP_GLOBAL_REG "r15"
  55. # elif defined(__IBMC__) && ZEND_GCC_VERSION >= 4002 && defined(__powerpc64__)
  56. # define ZEND_VM_FP_GLOBAL_REG "r14"
  57. # define ZEND_VM_IP_GLOBAL_REG "r15"
  58. # elif defined(__GNUC__) && ZEND_GCC_VERSION >= 4008 && defined(__aarch64__)
  59. # define ZEND_VM_FP_GLOBAL_REG "x27"
  60. # define ZEND_VM_IP_GLOBAL_REG "x28"
  61. # endif
  62. #endif
  63. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  64. # pragma GCC diagnostic ignored "-Wvolatile-register-var"
  65. register zend_execute_data* volatile execute_data __asm__(ZEND_VM_FP_GLOBAL_REG);
  66. # pragma GCC diagnostic warning "-Wvolatile-register-var"
  67. #endif
  68. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  69. # define EXECUTE_DATA_D void
  70. # define EXECUTE_DATA_C
  71. # define EXECUTE_DATA_DC
  72. # define EXECUTE_DATA_CC
  73. # define NO_EXECUTE_DATA_CC
  74. #else
  75. # define EXECUTE_DATA_D zend_execute_data* execute_data
  76. # define EXECUTE_DATA_C execute_data
  77. # define EXECUTE_DATA_DC , EXECUTE_DATA_D
  78. # define EXECUTE_DATA_CC , EXECUTE_DATA_C
  79. # define NO_EXECUTE_DATA_CC , NULL
  80. #endif
  81. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  82. # define OPLINE_D void
  83. # define OPLINE_C
  84. # define OPLINE_DC
  85. # define OPLINE_CC
  86. #else
  87. # define OPLINE_D const zend_op* opline
  88. # define OPLINE_C opline
  89. # define OPLINE_DC , OPLINE_D
  90. # define OPLINE_CC , OPLINE_C
  91. #endif
  92. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  93. # pragma GCC diagnostic ignored "-Wvolatile-register-var"
  94. register const zend_op* volatile opline __asm__(ZEND_VM_IP_GLOBAL_REG);
  95. # pragma GCC diagnostic warning "-Wvolatile-register-var"
  96. #else
  97. #endif
  98. #define _CONST_CODE 0
  99. #define _TMP_CODE 1
  100. #define _VAR_CODE 2
  101. #define _UNUSED_CODE 3
  102. #define _CV_CODE 4
  103. typedef int (ZEND_FASTCALL *incdec_t)(zval *);
  104. #define get_zval_ptr(op_type, node, type) _get_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
  105. #define get_zval_ptr_deref(op_type, node, type) _get_zval_ptr_deref(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
  106. #define get_zval_ptr_undef(op_type, node, type) _get_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
  107. #define get_op_data_zval_ptr_r(op_type, node) _get_op_data_zval_ptr_r(op_type, node EXECUTE_DATA_CC OPLINE_CC)
  108. #define get_op_data_zval_ptr_deref_r(op_type, node) _get_op_data_zval_ptr_deref_r(op_type, node EXECUTE_DATA_CC OPLINE_CC)
  109. #define get_zval_ptr_ptr(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
  110. #define get_zval_ptr_ptr_undef(op_type, node, type) _get_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
  111. #define get_obj_zval_ptr(op_type, node, type) _get_obj_zval_ptr(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
  112. #define get_obj_zval_ptr_undef(op_type, node, type) _get_obj_zval_ptr_undef(op_type, node, type EXECUTE_DATA_CC OPLINE_CC)
  113. #define get_obj_zval_ptr_ptr(op_type, node, type) _get_obj_zval_ptr_ptr(op_type, node, type EXECUTE_DATA_CC)
  114. #define RETURN_VALUE_USED(opline) ((opline)->result_type != IS_UNUSED)
  115. static ZEND_FUNCTION(pass)
  116. {
  117. }
  118. ZEND_BEGIN_ARG_INFO_EX(zend_pass_function_arg_info, 0, 0, 0)
  119. ZEND_END_ARG_INFO()
  120. ZEND_API const zend_internal_function zend_pass_function = {
  121. ZEND_INTERNAL_FUNCTION, /* type */
  122. {0, 0, 0}, /* arg_flags */
  123. 0, /* fn_flags */
  124. NULL, /* name */
  125. NULL, /* scope */
  126. NULL, /* prototype */
  127. 0, /* num_args */
  128. 0, /* required_num_args */
  129. (zend_internal_arg_info *) zend_pass_function_arg_info + 1, /* arg_info */
  130. NULL, /* attributes */
  131. ZEND_FN(pass), /* handler */
  132. NULL, /* module */
  133. {NULL,NULL,NULL,NULL} /* reserved */
  134. };
  135. #define FREE_VAR_PTR_AND_EXTRACT_RESULT_IF_NECESSARY(free_var) do { \
  136. zval *__container_to_free = EX_VAR(free_var); \
  137. if (UNEXPECTED(Z_REFCOUNTED_P(__container_to_free))) { \
  138. zend_refcounted *__ref = Z_COUNTED_P(__container_to_free); \
  139. if (UNEXPECTED(!GC_DELREF(__ref))) { \
  140. zval *__zv = EX_VAR(opline->result.var); \
  141. if (EXPECTED(Z_TYPE_P(__zv) == IS_INDIRECT)) { \
  142. ZVAL_COPY(__zv, Z_INDIRECT_P(__zv)); \
  143. } \
  144. rc_dtor_func(__ref); \
  145. } \
  146. } \
  147. } while (0)
  148. #define FREE_OP(type, var) \
  149. if ((type) & (IS_TMP_VAR|IS_VAR)) { \
  150. zval_ptr_dtor_nogc(EX_VAR(var)); \
  151. }
  152. #define FREE_OP_VAR_PTR(type, var) \
  153. FREE_OP(type, var)
  154. #define CV_DEF_OF(i) (EX(func)->op_array.vars[i])
  155. #define ZEND_VM_STACK_PAGE_SLOTS (16 * 1024) /* should be a power of 2 */
  156. #define ZEND_VM_STACK_PAGE_SIZE (ZEND_VM_STACK_PAGE_SLOTS * sizeof(zval))
  157. #define ZEND_VM_STACK_PAGE_ALIGNED_SIZE(size, page_size) \
  158. (((size) + ZEND_VM_STACK_HEADER_SLOTS * sizeof(zval) \
  159. + ((page_size) - 1)) & ~((page_size) - 1))
  160. ZEND_API void zend_vm_stack_init(void)
  161. {
  162. EG(vm_stack_page_size) = ZEND_VM_STACK_PAGE_SIZE;
  163. EG(vm_stack) = zend_vm_stack_new_page(ZEND_VM_STACK_PAGE_SIZE, NULL);
  164. EG(vm_stack_top) = EG(vm_stack)->top;
  165. EG(vm_stack_end) = EG(vm_stack)->end;
  166. }
  167. ZEND_API void zend_vm_stack_init_ex(size_t page_size)
  168. {
  169. /* page_size must be a power of 2 */
  170. ZEND_ASSERT(page_size > 0 && (page_size & (page_size - 1)) == 0);
  171. EG(vm_stack_page_size) = page_size;
  172. EG(vm_stack) = zend_vm_stack_new_page(page_size, NULL);
  173. EG(vm_stack_top) = EG(vm_stack)->top;
  174. EG(vm_stack_end) = EG(vm_stack)->end;
  175. }
  176. ZEND_API void zend_vm_stack_destroy(void)
  177. {
  178. zend_vm_stack stack = EG(vm_stack);
  179. while (stack != NULL) {
  180. zend_vm_stack p = stack->prev;
  181. efree(stack);
  182. stack = p;
  183. }
  184. }
  185. ZEND_API void* zend_vm_stack_extend(size_t size)
  186. {
  187. zend_vm_stack stack;
  188. void *ptr;
  189. stack = EG(vm_stack);
  190. stack->top = EG(vm_stack_top);
  191. EG(vm_stack) = stack = zend_vm_stack_new_page(
  192. EXPECTED(size < EG(vm_stack_page_size) - (ZEND_VM_STACK_HEADER_SLOTS * sizeof(zval))) ?
  193. EG(vm_stack_page_size) : ZEND_VM_STACK_PAGE_ALIGNED_SIZE(size, EG(vm_stack_page_size)),
  194. stack);
  195. ptr = stack->top;
  196. EG(vm_stack_top) = (void*)(((char*)ptr) + size);
  197. EG(vm_stack_end) = stack->end;
  198. return ptr;
  199. }
  200. ZEND_API zval* zend_get_compiled_variable_value(const zend_execute_data *execute_data, uint32_t var)
  201. {
  202. return EX_VAR(var);
  203. }
  204. static zend_always_inline zval *_get_zval_ptr_tmp(uint32_t var EXECUTE_DATA_DC)
  205. {
  206. zval *ret = EX_VAR(var);
  207. ZEND_ASSERT(Z_TYPE_P(ret) != IS_REFERENCE);
  208. return ret;
  209. }
  210. static zend_always_inline zval *_get_zval_ptr_var(uint32_t var EXECUTE_DATA_DC)
  211. {
  212. zval *ret = EX_VAR(var);
  213. return ret;
  214. }
  215. static zend_always_inline zval *_get_zval_ptr_var_deref(uint32_t var EXECUTE_DATA_DC)
  216. {
  217. zval *ret = EX_VAR(var);
  218. ZVAL_DEREF(ret);
  219. return ret;
  220. }
  221. static zend_never_inline ZEND_COLD zval* zval_undefined_cv(uint32_t var EXECUTE_DATA_DC)
  222. {
  223. if (EXPECTED(EG(exception) == NULL)) {
  224. zend_string *cv = CV_DEF_OF(EX_VAR_TO_NUM(var));
  225. zend_error(E_WARNING, "Undefined variable $%s", ZSTR_VAL(cv));
  226. }
  227. return &EG(uninitialized_zval);
  228. }
  229. static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL _zval_undefined_op1(EXECUTE_DATA_D)
  230. {
  231. return zval_undefined_cv(EX(opline)->op1.var EXECUTE_DATA_CC);
  232. }
  233. static zend_never_inline ZEND_COLD zval* ZEND_FASTCALL _zval_undefined_op2(EXECUTE_DATA_D)
  234. {
  235. return zval_undefined_cv(EX(opline)->op2.var EXECUTE_DATA_CC);
  236. }
  237. #define ZVAL_UNDEFINED_OP1() _zval_undefined_op1(EXECUTE_DATA_C)
  238. #define ZVAL_UNDEFINED_OP2() _zval_undefined_op2(EXECUTE_DATA_C)
  239. static zend_never_inline ZEND_COLD zval *_get_zval_cv_lookup(zval *ptr, uint32_t var, int type EXECUTE_DATA_DC)
  240. {
  241. switch (type) {
  242. case BP_VAR_R:
  243. case BP_VAR_UNSET:
  244. ptr = zval_undefined_cv(var EXECUTE_DATA_CC);
  245. break;
  246. case BP_VAR_IS:
  247. ptr = &EG(uninitialized_zval);
  248. break;
  249. case BP_VAR_RW:
  250. zval_undefined_cv(var EXECUTE_DATA_CC);
  251. ZEND_FALLTHROUGH;
  252. case BP_VAR_W:
  253. ZVAL_NULL(ptr);
  254. break;
  255. }
  256. return ptr;
  257. }
  258. static zend_always_inline zval *_get_zval_ptr_cv(uint32_t var, int type EXECUTE_DATA_DC)
  259. {
  260. zval *ret = EX_VAR(var);
  261. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  262. if (type == BP_VAR_W) {
  263. ZVAL_NULL(ret);
  264. } else {
  265. return _get_zval_cv_lookup(ret, var, type EXECUTE_DATA_CC);
  266. }
  267. }
  268. return ret;
  269. }
  270. static zend_always_inline zval *_get_zval_ptr_cv_deref(uint32_t var, int type EXECUTE_DATA_DC)
  271. {
  272. zval *ret = EX_VAR(var);
  273. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  274. if (type == BP_VAR_W) {
  275. ZVAL_NULL(ret);
  276. return ret;
  277. } else {
  278. return _get_zval_cv_lookup(ret, var, type EXECUTE_DATA_CC);
  279. }
  280. }
  281. ZVAL_DEREF(ret);
  282. return ret;
  283. }
  284. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_R(uint32_t var EXECUTE_DATA_DC)
  285. {
  286. zval *ret = EX_VAR(var);
  287. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  288. return zval_undefined_cv(var EXECUTE_DATA_CC);
  289. }
  290. return ret;
  291. }
  292. static zend_always_inline zval *_get_zval_ptr_cv_deref_BP_VAR_R(uint32_t var EXECUTE_DATA_DC)
  293. {
  294. zval *ret = EX_VAR(var);
  295. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  296. return zval_undefined_cv(var EXECUTE_DATA_CC);
  297. }
  298. ZVAL_DEREF(ret);
  299. return ret;
  300. }
  301. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_IS(uint32_t var EXECUTE_DATA_DC)
  302. {
  303. zval *ret = EX_VAR(var);
  304. return ret;
  305. }
  306. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_RW(uint32_t var EXECUTE_DATA_DC)
  307. {
  308. zval *ret = EX_VAR(var);
  309. if (UNEXPECTED(Z_TYPE_P(ret) == IS_UNDEF)) {
  310. zval_undefined_cv(var EXECUTE_DATA_CC);
  311. ZVAL_NULL(ret);
  312. return ret;
  313. }
  314. return ret;
  315. }
  316. static zend_always_inline zval *_get_zval_ptr_cv_BP_VAR_W(uint32_t var EXECUTE_DATA_DC)
  317. {
  318. zval *ret = EX_VAR(var);
  319. if (Z_TYPE_P(ret) == IS_UNDEF) {
  320. ZVAL_NULL(ret);
  321. }
  322. return ret;
  323. }
  324. static zend_always_inline zval *_get_zval_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
  325. {
  326. if (op_type & (IS_TMP_VAR|IS_VAR)) {
  327. if (!ZEND_DEBUG || op_type == IS_VAR) {
  328. return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
  329. } else {
  330. ZEND_ASSERT(op_type == IS_TMP_VAR);
  331. return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
  332. }
  333. } else {
  334. if (op_type == IS_CONST) {
  335. return RT_CONSTANT(opline, node);
  336. } else if (op_type == IS_CV) {
  337. return _get_zval_ptr_cv(node.var, type EXECUTE_DATA_CC);
  338. } else {
  339. return NULL;
  340. }
  341. }
  342. }
  343. static zend_always_inline zval *_get_op_data_zval_ptr_r(int op_type, znode_op node EXECUTE_DATA_DC OPLINE_DC)
  344. {
  345. if (op_type & (IS_TMP_VAR|IS_VAR)) {
  346. if (!ZEND_DEBUG || op_type == IS_VAR) {
  347. return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
  348. } else {
  349. ZEND_ASSERT(op_type == IS_TMP_VAR);
  350. return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
  351. }
  352. } else {
  353. if (op_type == IS_CONST) {
  354. return RT_CONSTANT(opline + 1, node);
  355. } else if (op_type == IS_CV) {
  356. return _get_zval_ptr_cv_BP_VAR_R(node.var EXECUTE_DATA_CC);
  357. } else {
  358. return NULL;
  359. }
  360. }
  361. }
  362. static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_zval_ptr_deref(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
  363. {
  364. if (op_type & (IS_TMP_VAR|IS_VAR)) {
  365. if (op_type == IS_TMP_VAR) {
  366. return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
  367. } else {
  368. ZEND_ASSERT(op_type == IS_VAR);
  369. return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC);
  370. }
  371. } else {
  372. if (op_type == IS_CONST) {
  373. return RT_CONSTANT(opline, node);
  374. } else if (op_type == IS_CV) {
  375. return _get_zval_ptr_cv_deref(node.var, type EXECUTE_DATA_CC);
  376. } else {
  377. return NULL;
  378. }
  379. }
  380. }
  381. static zend_always_inline ZEND_ATTRIBUTE_UNUSED zval *_get_op_data_zval_ptr_deref_r(int op_type, znode_op node EXECUTE_DATA_DC OPLINE_DC)
  382. {
  383. if (op_type & (IS_TMP_VAR|IS_VAR)) {
  384. if (op_type == IS_TMP_VAR) {
  385. return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
  386. } else {
  387. ZEND_ASSERT(op_type == IS_VAR);
  388. return _get_zval_ptr_var_deref(node.var EXECUTE_DATA_CC);
  389. }
  390. } else {
  391. if (op_type == IS_CONST) {
  392. return RT_CONSTANT(opline + 1, node);
  393. } else if (op_type == IS_CV) {
  394. return _get_zval_ptr_cv_deref_BP_VAR_R(node.var EXECUTE_DATA_CC);
  395. } else {
  396. return NULL;
  397. }
  398. }
  399. }
  400. static zend_always_inline zval *_get_zval_ptr_undef(int op_type, znode_op node, int type EXECUTE_DATA_DC OPLINE_DC)
  401. {
  402. if (op_type & (IS_TMP_VAR|IS_VAR)) {
  403. if (!ZEND_DEBUG || op_type == IS_VAR) {
  404. return _get_zval_ptr_var(node.var EXECUTE_DATA_CC);
  405. } else {
  406. ZEND_ASSERT(op_type == IS_TMP_VAR);
  407. return _get_zval_ptr_tmp(node.var EXECUTE_DATA_CC);
  408. }
  409. } else {
  410. if (op_type == IS_CONST) {
  411. return RT_CONSTANT(opline, node);
  412. } else if (op_type == IS_CV) {
  413. return EX_VAR(node.var);
  414. } else {
  415. return NULL;
  416. }
  417. }
  418. }
  419. static zend_always_inline zval *_get_zval_ptr_ptr_var(uint32_t var EXECUTE_DATA_DC)
  420. {
  421. zval *ret = EX_VAR(var);
  422. if (EXPECTED(Z_TYPE_P(ret) == IS_INDIRECT)) {
  423. ret = Z_INDIRECT_P(ret);
  424. }
  425. return ret;
  426. }
  427. static inline zval *_get_zval_ptr_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC)
  428. {
  429. if (op_type == IS_CV) {
  430. return _get_zval_ptr_cv(node.var, type EXECUTE_DATA_CC);
  431. } else /* if (op_type == IS_VAR) */ {
  432. ZEND_ASSERT(op_type == IS_VAR);
  433. return _get_zval_ptr_ptr_var(node.var EXECUTE_DATA_CC);
  434. }
  435. }
  436. static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC)
  437. {
  438. if (op_type == IS_UNUSED) {
  439. return &EX(This);
  440. }
  441. return get_zval_ptr(op_type, op, type);
  442. }
  443. static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_undef(int op_type, znode_op op, int type EXECUTE_DATA_DC OPLINE_DC)
  444. {
  445. if (op_type == IS_UNUSED) {
  446. return &EX(This);
  447. }
  448. return get_zval_ptr_undef(op_type, op, type);
  449. }
  450. static inline ZEND_ATTRIBUTE_UNUSED zval *_get_obj_zval_ptr_ptr(int op_type, znode_op node, int type EXECUTE_DATA_DC)
  451. {
  452. if (op_type == IS_UNUSED) {
  453. return &EX(This);
  454. }
  455. return get_zval_ptr_ptr(op_type, node, type);
  456. }
  457. static inline void zend_assign_to_variable_reference(zval *variable_ptr, zval *value_ptr)
  458. {
  459. zend_reference *ref;
  460. if (EXPECTED(!Z_ISREF_P(value_ptr))) {
  461. ZVAL_NEW_REF(value_ptr, value_ptr);
  462. } else if (UNEXPECTED(variable_ptr == value_ptr)) {
  463. return;
  464. }
  465. ref = Z_REF_P(value_ptr);
  466. GC_ADDREF(ref);
  467. if (Z_REFCOUNTED_P(variable_ptr)) {
  468. zend_refcounted *garbage = Z_COUNTED_P(variable_ptr);
  469. if (GC_DELREF(garbage) == 0) {
  470. ZVAL_REF(variable_ptr, ref);
  471. rc_dtor_func(garbage);
  472. return;
  473. } else {
  474. gc_check_possible_root(garbage);
  475. }
  476. }
  477. ZVAL_REF(variable_ptr, ref);
  478. }
  479. static zend_never_inline zval* zend_assign_to_typed_property_reference(zend_property_info *prop_info, zval *prop, zval *value_ptr EXECUTE_DATA_DC)
  480. {
  481. if (!zend_verify_prop_assignable_by_ref(prop_info, value_ptr, EX_USES_STRICT_TYPES())) {
  482. return &EG(uninitialized_zval);
  483. }
  484. if (Z_ISREF_P(prop)) {
  485. ZEND_REF_DEL_TYPE_SOURCE(Z_REF_P(prop), prop_info);
  486. }
  487. zend_assign_to_variable_reference(prop, value_ptr);
  488. ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(prop), prop_info);
  489. return prop;
  490. }
  491. static zend_never_inline ZEND_COLD zval *zend_wrong_assign_to_variable_reference(zval *variable_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  492. {
  493. zend_error(E_NOTICE, "Only variables should be assigned by reference");
  494. if (UNEXPECTED(EG(exception) != NULL)) {
  495. return &EG(uninitialized_zval);
  496. }
  497. /* Use IS_TMP_VAR instead of IS_VAR to avoid ISREF check */
  498. Z_TRY_ADDREF_P(value_ptr);
  499. return zend_assign_to_variable(variable_ptr, value_ptr, IS_TMP_VAR, EX_USES_STRICT_TYPES());
  500. }
  501. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_cannot_pass_by_reference(uint32_t arg_num)
  502. {
  503. const zend_execute_data *execute_data = EG(current_execute_data);
  504. zend_string *func_name = get_function_or_method_name(EX(call)->func);
  505. const char *param_name = get_function_arg_name(EX(call)->func, arg_num);
  506. zend_throw_error(NULL, "%s(): Argument #%d%s%s%s cannot be passed by reference",
  507. ZSTR_VAL(func_name), arg_num, param_name ? " ($" : "", param_name ? param_name : "", param_name ? ")" : ""
  508. );
  509. zend_string_release(func_name);
  510. }
  511. static zend_never_inline ZEND_COLD void zend_throw_auto_init_in_prop_error(zend_property_info *prop, const char *type) {
  512. zend_string *type_str = zend_type_to_string(prop->type);
  513. zend_type_error(
  514. "Cannot auto-initialize an %s inside property %s::$%s of type %s",
  515. type,
  516. ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name),
  517. ZSTR_VAL(type_str)
  518. );
  519. zend_string_release(type_str);
  520. }
  521. static zend_never_inline ZEND_COLD void zend_throw_auto_init_in_ref_error(zend_property_info *prop, const char *type) {
  522. zend_string *type_str = zend_type_to_string(prop->type);
  523. zend_type_error(
  524. "Cannot auto-initialize an %s inside a reference held by property %s::$%s of type %s",
  525. type,
  526. ZSTR_VAL(prop->ce->name), zend_get_unmangled_property_name(prop->name),
  527. ZSTR_VAL(type_str)
  528. );
  529. zend_string_release(type_str);
  530. }
  531. static zend_never_inline ZEND_COLD void zend_throw_access_uninit_prop_by_ref_error(
  532. zend_property_info *prop) {
  533. zend_throw_error(NULL,
  534. "Cannot access uninitialized non-nullable property %s::$%s by reference",
  535. ZSTR_VAL(prop->ce->name),
  536. zend_get_unmangled_property_name(prop->name));
  537. }
  538. /* this should modify object only if it's empty */
  539. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_throw_non_object_error(zval *object, zval *property OPLINE_DC EXECUTE_DATA_DC)
  540. {
  541. zend_string *tmp_property_name;
  542. zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
  543. if (opline->opcode == ZEND_PRE_INC_OBJ
  544. || opline->opcode == ZEND_PRE_DEC_OBJ
  545. || opline->opcode == ZEND_POST_INC_OBJ
  546. || opline->opcode == ZEND_POST_DEC_OBJ) {
  547. zend_throw_error(NULL,
  548. "Attempt to increment/decrement property \"%s\" on %s",
  549. ZSTR_VAL(property_name), zend_zval_type_name(object)
  550. );
  551. } else if (opline->opcode == ZEND_FETCH_OBJ_W
  552. || opline->opcode == ZEND_FETCH_OBJ_RW
  553. || opline->opcode == ZEND_FETCH_OBJ_FUNC_ARG
  554. || opline->opcode == ZEND_ASSIGN_OBJ_REF) {
  555. zend_throw_error(NULL,
  556. "Attempt to modify property \"%s\" on %s",
  557. ZSTR_VAL(property_name), zend_zval_type_name(object)
  558. );
  559. } else {
  560. zend_throw_error(NULL,
  561. "Attempt to assign property \"%s\" on %s",
  562. ZSTR_VAL(property_name), zend_zval_type_name(object)
  563. );
  564. }
  565. zend_tmp_string_release(tmp_property_name);
  566. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  567. ZVAL_NULL(EX_VAR(opline->result.var));
  568. }
  569. }
  570. static ZEND_COLD void zend_verify_type_error_common(
  571. const zend_function *zf, const zend_arg_info *arg_info, zval *value,
  572. const char **fname, const char **fsep, const char **fclass,
  573. zend_string **need_msg, const char **given_kind)
  574. {
  575. *fname = ZSTR_VAL(zf->common.function_name);
  576. if (zf->common.scope) {
  577. *fsep = "::";
  578. *fclass = ZSTR_VAL(zf->common.scope->name);
  579. } else {
  580. *fsep = "";
  581. *fclass = "";
  582. }
  583. *need_msg = zend_type_to_string_resolved(arg_info->type, zf->common.scope);
  584. if (value) {
  585. *given_kind = zend_zval_type_name(value);
  586. } else {
  587. *given_kind = "none";
  588. }
  589. }
  590. ZEND_API ZEND_COLD void zend_verify_arg_error(
  591. const zend_function *zf, const zend_arg_info *arg_info, uint32_t arg_num, zval *value)
  592. {
  593. zend_execute_data *ptr = EG(current_execute_data)->prev_execute_data;
  594. const char *fname, *fsep, *fclass;
  595. zend_string *need_msg;
  596. const char *given_msg;
  597. zend_verify_type_error_common(
  598. zf, arg_info, value, &fname, &fsep, &fclass, &need_msg, &given_msg);
  599. ZEND_ASSERT(zf->common.type == ZEND_USER_FUNCTION
  600. && "Arginfo verification is not performed for internal functions");
  601. if (ptr && ptr->func && ZEND_USER_CODE(ptr->func->common.type)) {
  602. zend_argument_type_error(arg_num, "must be of type %s, %s given, called in %s on line %d",
  603. ZSTR_VAL(need_msg), given_msg,
  604. ZSTR_VAL(ptr->func->op_array.filename), ptr->opline->lineno
  605. );
  606. } else {
  607. zend_argument_type_error(arg_num,
  608. "must be of type %s, %s given", ZSTR_VAL(need_msg), given_msg);
  609. }
  610. zend_string_release(need_msg);
  611. }
  612. static bool zend_verify_weak_scalar_type_hint(uint32_t type_mask, zval *arg)
  613. {
  614. zend_long lval;
  615. double dval;
  616. zend_string *str;
  617. bool bval;
  618. /* Type preference order: int -> float -> string -> bool */
  619. if (type_mask & MAY_BE_LONG) {
  620. /* For an int|float union type and string value,
  621. * determine chosen type by is_numeric_string() semantics. */
  622. if ((type_mask & MAY_BE_DOUBLE) && Z_TYPE_P(arg) == IS_STRING) {
  623. zend_uchar type = is_numeric_str_function(Z_STR_P(arg), &lval, &dval);
  624. if (type == IS_LONG) {
  625. zend_string_release(Z_STR_P(arg));
  626. ZVAL_LONG(arg, lval);
  627. return 1;
  628. }
  629. if (type == IS_DOUBLE) {
  630. zend_string_release(Z_STR_P(arg));
  631. ZVAL_DOUBLE(arg, dval);
  632. return 1;
  633. }
  634. } else if (zend_parse_arg_long_weak(arg, &lval, 0)) {
  635. zval_ptr_dtor(arg);
  636. ZVAL_LONG(arg, lval);
  637. return 1;
  638. } else if (UNEXPECTED(EG(exception))) {
  639. return 0;
  640. }
  641. }
  642. if ((type_mask & MAY_BE_DOUBLE) && zend_parse_arg_double_weak(arg, &dval, 0)) {
  643. zval_ptr_dtor(arg);
  644. ZVAL_DOUBLE(arg, dval);
  645. return 1;
  646. }
  647. if ((type_mask & MAY_BE_STRING) && zend_parse_arg_str_weak(arg, &str, 0)) {
  648. /* on success "arg" is converted to IS_STRING */
  649. return 1;
  650. }
  651. if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL && zend_parse_arg_bool_weak(arg, &bval, 0)) {
  652. zval_ptr_dtor(arg);
  653. ZVAL_BOOL(arg, bval);
  654. return 1;
  655. }
  656. return 0;
  657. }
  658. #if ZEND_DEBUG
  659. static bool can_convert_to_string(zval *zv) {
  660. /* We don't call cast_object here, because this check must be side-effect free. As this
  661. * is only used for a sanity check of arginfo/zpp consistency, it's okay if we accept
  662. * more than actually allowed here. */
  663. if (Z_TYPE_P(zv) == IS_OBJECT) {
  664. return Z_OBJ_HT_P(zv)->cast_object != zend_std_cast_object_tostring
  665. || Z_OBJCE_P(zv)->__tostring;
  666. }
  667. return Z_TYPE_P(zv) <= IS_STRING;
  668. }
  669. /* Used to sanity-check internal arginfo types without performing any actual type conversions. */
  670. static bool zend_verify_weak_scalar_type_hint_no_sideeffect(uint32_t type_mask, zval *arg)
  671. {
  672. zend_long lval;
  673. double dval;
  674. bool bval;
  675. /* Pass (uint32_t)-1 as arg_num to indicate to ZPP not to emit any deprecation notice,
  676. * this is needed because the version with side effects also uses 0 (e.g. for typed properties) */
  677. if ((type_mask & MAY_BE_LONG) && zend_parse_arg_long_weak(arg, &lval, (uint32_t)-1)) {
  678. return 1;
  679. }
  680. if ((type_mask & MAY_BE_DOUBLE) && zend_parse_arg_double_weak(arg, &dval, (uint32_t)-1)) {
  681. return 1;
  682. }
  683. if ((type_mask & MAY_BE_STRING) && can_convert_to_string(arg)) {
  684. return 1;
  685. }
  686. if ((type_mask & MAY_BE_BOOL) == MAY_BE_BOOL && zend_parse_arg_bool_weak(arg, &bval, (uint32_t)-1)) {
  687. return 1;
  688. }
  689. return 0;
  690. }
  691. #endif
  692. ZEND_API bool zend_verify_scalar_type_hint(uint32_t type_mask, zval *arg, bool strict, bool is_internal_arg)
  693. {
  694. if (UNEXPECTED(strict)) {
  695. /* SSTH Exception: IS_LONG may be accepted as IS_DOUBLE (converted) */
  696. if (!(type_mask & MAY_BE_DOUBLE) || Z_TYPE_P(arg) != IS_LONG) {
  697. return 0;
  698. }
  699. } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL)) {
  700. /* NULL may be accepted only by nullable hints (this is already checked).
  701. * As an exception for internal functions, null is allowed for scalar types in weak mode. */
  702. return is_internal_arg
  703. && (type_mask & (MAY_BE_TRUE|MAY_BE_FALSE|MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING));
  704. }
  705. #if ZEND_DEBUG
  706. if (is_internal_arg) {
  707. return zend_verify_weak_scalar_type_hint_no_sideeffect(type_mask, arg);
  708. }
  709. #endif
  710. return zend_verify_weak_scalar_type_hint(type_mask, arg);
  711. }
  712. ZEND_COLD zend_never_inline void zend_verify_property_type_error(zend_property_info *info, zval *property)
  713. {
  714. zend_string *type_str;
  715. /* we _may_ land here in case reading already errored and runtime cache thus has not been updated (i.e. it contains a valid but unrelated info) */
  716. if (EG(exception)) {
  717. return;
  718. }
  719. type_str = zend_type_to_string(info->type);
  720. zend_type_error("Cannot assign %s to property %s::$%s of type %s",
  721. zend_zval_type_name(property),
  722. ZSTR_VAL(info->ce->name),
  723. zend_get_unmangled_property_name(info->name),
  724. ZSTR_VAL(type_str));
  725. zend_string_release(type_str);
  726. }
  727. ZEND_COLD void zend_match_unhandled_error(zval *value)
  728. {
  729. smart_str msg = {0};
  730. if (Z_TYPE_P(value) <= IS_STRING) {
  731. smart_str_append_scalar(&msg, value, EG(exception_string_param_max_len));
  732. } else {
  733. smart_str_appendl(&msg, "of type ", sizeof("of type ")-1);
  734. smart_str_appends(&msg, zend_zval_type_name(value));
  735. }
  736. smart_str_0(&msg);
  737. zend_throw_exception_ex(
  738. zend_ce_unhandled_match_error, 0, "Unhandled match case %s", ZSTR_VAL(msg.s));
  739. smart_str_free(&msg);
  740. }
  741. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_modification_error(
  742. zend_property_info *info) {
  743. zend_throw_error(NULL, "Cannot modify readonly property %s::$%s",
  744. ZSTR_VAL(info->ce->name), zend_get_unmangled_property_name(info->name));
  745. }
  746. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_readonly_property_indirect_modification_error(zend_property_info *info)
  747. {
  748. zend_throw_error(NULL, "Cannot indirectly modify readonly property %s::$%s",
  749. ZSTR_VAL(info->ce->name), zend_get_unmangled_property_name(info->name));
  750. }
  751. static zend_class_entry *resolve_single_class_type(zend_string *name, zend_class_entry *self_ce) {
  752. if (zend_string_equals_literal_ci(name, "self")) {
  753. return self_ce;
  754. } else if (zend_string_equals_literal_ci(name, "parent")) {
  755. return self_ce->parent;
  756. } else {
  757. return zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
  758. }
  759. }
  760. static zend_always_inline zend_class_entry *zend_ce_from_type(
  761. zend_property_info *info, zend_type *type) {
  762. ZEND_ASSERT(ZEND_TYPE_HAS_NAME(*type));
  763. zend_string *name = ZEND_TYPE_NAME(*type);
  764. if (ZSTR_HAS_CE_CACHE(name)) {
  765. zend_class_entry *ce = ZSTR_GET_CE_CACHE(name);
  766. if (!ce) {
  767. ce = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
  768. }
  769. return ce;
  770. }
  771. return resolve_single_class_type(name, info->ce);
  772. }
  773. static bool zend_check_and_resolve_property_class_type(
  774. zend_property_info *info, zend_class_entry *object_ce) {
  775. if (ZEND_TYPE_HAS_LIST(info->type)) {
  776. zend_type *list_type;
  777. if (ZEND_TYPE_IS_INTERSECTION(info->type)) {
  778. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(info->type), list_type) {
  779. zend_class_entry *ce = zend_ce_from_type(info, list_type);
  780. if (!ce || !instanceof_function(object_ce, ce)) {
  781. return false;
  782. }
  783. } ZEND_TYPE_LIST_FOREACH_END();
  784. return true;
  785. } else {
  786. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(info->type), list_type) {
  787. zend_class_entry *ce = zend_ce_from_type(info, list_type);
  788. if (ce && instanceof_function(object_ce, ce)) {
  789. return true;
  790. }
  791. } ZEND_TYPE_LIST_FOREACH_END();
  792. return false;
  793. }
  794. } else {
  795. zend_class_entry *ce = zend_ce_from_type(info, &info->type);
  796. return ce && instanceof_function(object_ce, ce);
  797. }
  798. }
  799. static zend_always_inline bool i_zend_check_property_type(zend_property_info *info, zval *property, bool strict)
  800. {
  801. ZEND_ASSERT(!Z_ISREF_P(property));
  802. if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(info->type, Z_TYPE_P(property)))) {
  803. return 1;
  804. }
  805. if (ZEND_TYPE_IS_COMPLEX(info->type) && Z_TYPE_P(property) == IS_OBJECT
  806. && zend_check_and_resolve_property_class_type(info, Z_OBJCE_P(property))) {
  807. return 1;
  808. }
  809. uint32_t type_mask = ZEND_TYPE_FULL_MASK(info->type);
  810. ZEND_ASSERT(!(type_mask & (MAY_BE_CALLABLE|MAY_BE_STATIC)));
  811. if ((type_mask & MAY_BE_ITERABLE) && zend_is_iterable(property)) {
  812. return 1;
  813. }
  814. return zend_verify_scalar_type_hint(type_mask, property, strict, 0);
  815. }
  816. static zend_always_inline bool i_zend_verify_property_type(zend_property_info *info, zval *property, bool strict)
  817. {
  818. if (i_zend_check_property_type(info, property, strict)) {
  819. return 1;
  820. }
  821. zend_verify_property_type_error(info, property);
  822. return 0;
  823. }
  824. ZEND_API bool zend_never_inline zend_verify_property_type(zend_property_info *info, zval *property, bool strict) {
  825. return i_zend_verify_property_type(info, property, strict);
  826. }
  827. static zend_never_inline zval* zend_assign_to_typed_prop(zend_property_info *info, zval *property_val, zval *value EXECUTE_DATA_DC)
  828. {
  829. zval tmp;
  830. if (UNEXPECTED(info->flags & ZEND_ACC_READONLY)) {
  831. zend_readonly_property_modification_error(info);
  832. return &EG(uninitialized_zval);
  833. }
  834. ZVAL_DEREF(value);
  835. ZVAL_COPY(&tmp, value);
  836. if (UNEXPECTED(!i_zend_verify_property_type(info, &tmp, EX_USES_STRICT_TYPES()))) {
  837. zval_ptr_dtor(&tmp);
  838. return &EG(uninitialized_zval);
  839. }
  840. return zend_assign_to_variable(property_val, &tmp, IS_TMP_VAR, EX_USES_STRICT_TYPES());
  841. }
  842. static zend_always_inline bool zend_value_instanceof_static(zval *zv) {
  843. if (Z_TYPE_P(zv) != IS_OBJECT) {
  844. return 0;
  845. }
  846. zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
  847. if (!called_scope) {
  848. return 0;
  849. }
  850. return instanceof_function(Z_OBJCE_P(zv), called_scope);
  851. }
  852. /* The cache_slot may only be NULL in debug builds, where arginfo verification of
  853. * internal functions is enabled. Avoid unnecessary checks in release builds. */
  854. #if ZEND_DEBUG
  855. # define HAVE_CACHE_SLOT (cache_slot != NULL)
  856. #else
  857. # define HAVE_CACHE_SLOT 1
  858. #endif
  859. static zend_always_inline zend_class_entry *zend_fetch_ce_from_cache_slot(
  860. void **cache_slot, zend_type *type)
  861. {
  862. if (EXPECTED(HAVE_CACHE_SLOT && *cache_slot)) {
  863. return (zend_class_entry *) *cache_slot;
  864. }
  865. zend_string *name = ZEND_TYPE_NAME(*type);
  866. zend_class_entry *ce;
  867. if (ZSTR_HAS_CE_CACHE(name)) {
  868. ce = ZSTR_GET_CE_CACHE(name);
  869. if (!ce) {
  870. ce = zend_lookup_class_ex(name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
  871. if (UNEXPECTED(!ce)) {
  872. /* Cannot resolve */
  873. return NULL;
  874. }
  875. }
  876. } else {
  877. ce = zend_fetch_class(name,
  878. ZEND_FETCH_CLASS_AUTO | ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_SILENT);
  879. if (UNEXPECTED(!ce)) {
  880. return NULL;
  881. }
  882. }
  883. if (HAVE_CACHE_SLOT) {
  884. *cache_slot = (void *) ce;
  885. }
  886. return ce;
  887. }
  888. static zend_always_inline bool zend_check_type_slow(
  889. zend_type *type, zval *arg, zend_reference *ref, void **cache_slot,
  890. bool is_return_type, bool is_internal)
  891. {
  892. uint32_t type_mask;
  893. if (ZEND_TYPE_IS_COMPLEX(*type) && EXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
  894. zend_class_entry *ce;
  895. if (UNEXPECTED(ZEND_TYPE_HAS_LIST(*type))) {
  896. zend_type *list_type;
  897. if (ZEND_TYPE_IS_INTERSECTION(*type)) {
  898. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(*type), list_type) {
  899. ce = zend_fetch_ce_from_cache_slot(cache_slot, list_type);
  900. /* If type is not an instance of one of the types taking part in the
  901. * intersection it cannot be a valid instance of the whole intersection type. */
  902. if (!ce || !instanceof_function(Z_OBJCE_P(arg), ce)) {
  903. return false;
  904. }
  905. if (HAVE_CACHE_SLOT) {
  906. cache_slot++;
  907. }
  908. } ZEND_TYPE_LIST_FOREACH_END();
  909. return true;
  910. } else {
  911. ZEND_TYPE_LIST_FOREACH(ZEND_TYPE_LIST(*type), list_type) {
  912. ce = zend_fetch_ce_from_cache_slot(cache_slot, list_type);
  913. /* Instance of a single type part of a union is sufficient to pass the type check */
  914. if (ce && instanceof_function(Z_OBJCE_P(arg), ce)) {
  915. return true;
  916. }
  917. if (HAVE_CACHE_SLOT) {
  918. cache_slot++;
  919. }
  920. } ZEND_TYPE_LIST_FOREACH_END();
  921. }
  922. } else {
  923. ce = zend_fetch_ce_from_cache_slot(cache_slot, type);
  924. /* If we have a CE we check if it satisfies the type constraint,
  925. * otherwise it will check if a standard type satisfies it. */
  926. if (ce && instanceof_function(Z_OBJCE_P(arg), ce)) {
  927. return true;
  928. }
  929. }
  930. }
  931. type_mask = ZEND_TYPE_FULL_MASK(*type);
  932. if ((type_mask & MAY_BE_CALLABLE) && zend_is_callable(arg, 0, NULL)) {
  933. return 1;
  934. }
  935. if ((type_mask & MAY_BE_ITERABLE) && zend_is_iterable(arg)) {
  936. return 1;
  937. }
  938. if ((type_mask & MAY_BE_STATIC) && zend_value_instanceof_static(arg)) {
  939. return 1;
  940. }
  941. if (ref && ZEND_REF_HAS_TYPE_SOURCES(ref)) {
  942. /* We cannot have conversions for typed refs. */
  943. return 0;
  944. }
  945. if (is_internal && is_return_type) {
  946. /* For internal returns, the type has to match exactly, because we're not
  947. * going to check it for non-debug builds, and there will be no chance to
  948. * apply coercions. */
  949. return 0;
  950. }
  951. return zend_verify_scalar_type_hint(type_mask, arg,
  952. is_return_type ? ZEND_RET_USES_STRICT_TYPES() : ZEND_ARG_USES_STRICT_TYPES(),
  953. is_internal);
  954. /* Special handling for IS_VOID is not necessary (for return types),
  955. * because this case is already checked at compile-time. */
  956. }
  957. static zend_always_inline bool zend_check_type(
  958. zend_type *type, zval *arg, void **cache_slot, zend_class_entry *scope,
  959. bool is_return_type, bool is_internal)
  960. {
  961. zend_reference *ref = NULL;
  962. ZEND_ASSERT(ZEND_TYPE_IS_SET(*type));
  963. if (UNEXPECTED(Z_ISREF_P(arg))) {
  964. ref = Z_REF_P(arg);
  965. arg = Z_REFVAL_P(arg);
  966. }
  967. if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(*type, Z_TYPE_P(arg)))) {
  968. return 1;
  969. }
  970. return zend_check_type_slow(type, arg, ref, cache_slot, is_return_type, is_internal);
  971. }
  972. ZEND_API bool zend_check_user_type_slow(
  973. zend_type *type, zval *arg, zend_reference *ref, void **cache_slot, bool is_return_type)
  974. {
  975. return zend_check_type_slow(
  976. type, arg, ref, cache_slot, is_return_type, /* is_internal */ false);
  977. }
  978. static zend_always_inline bool zend_verify_recv_arg_type(zend_function *zf, uint32_t arg_num, zval *arg, void **cache_slot)
  979. {
  980. zend_arg_info *cur_arg_info;
  981. ZEND_ASSERT(arg_num <= zf->common.num_args);
  982. cur_arg_info = &zf->common.arg_info[arg_num-1];
  983. if (ZEND_TYPE_IS_SET(cur_arg_info->type)
  984. && UNEXPECTED(!zend_check_type(&cur_arg_info->type, arg, cache_slot, zf->common.scope, 0, 0))) {
  985. zend_verify_arg_error(zf, cur_arg_info, arg_num, arg);
  986. return 0;
  987. }
  988. return 1;
  989. }
  990. static zend_always_inline bool zend_verify_variadic_arg_type(
  991. zend_function *zf, zend_arg_info *arg_info, uint32_t arg_num, zval *arg, void **cache_slot)
  992. {
  993. ZEND_ASSERT(ZEND_TYPE_IS_SET(arg_info->type));
  994. if (UNEXPECTED(!zend_check_type(&arg_info->type, arg, cache_slot, zf->common.scope, 0, 0))) {
  995. zend_verify_arg_error(zf, arg_info, arg_num, arg);
  996. return 0;
  997. }
  998. return 1;
  999. }
  1000. static zend_never_inline ZEND_ATTRIBUTE_UNUSED bool zend_verify_internal_arg_types(zend_function *fbc, zend_execute_data *call)
  1001. {
  1002. uint32_t i;
  1003. uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
  1004. zval *arg = ZEND_CALL_ARG(call, 1);
  1005. for (i = 0; i < num_args; ++i) {
  1006. zend_arg_info *cur_arg_info;
  1007. if (EXPECTED(i < fbc->common.num_args)) {
  1008. cur_arg_info = &fbc->common.arg_info[i];
  1009. } else if (UNEXPECTED(fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
  1010. cur_arg_info = &fbc->common.arg_info[fbc->common.num_args];
  1011. } else {
  1012. break;
  1013. }
  1014. if (ZEND_TYPE_IS_SET(cur_arg_info->type)
  1015. && UNEXPECTED(!zend_check_type(&cur_arg_info->type, arg, /* cache_slot */ NULL, fbc->common.scope, 0, /* is_internal */ 1))) {
  1016. return 0;
  1017. }
  1018. arg++;
  1019. }
  1020. return 1;
  1021. }
  1022. #if ZEND_DEBUG
  1023. /* Determine whether an internal call should throw, because the passed arguments violate
  1024. * an arginfo constraint. This is only checked in debug builds. In release builds, we
  1025. * trust that arginfo matches what is enforced by zend_parse_parameters. */
  1026. ZEND_API bool zend_internal_call_should_throw(zend_function *fbc, zend_execute_data *call)
  1027. {
  1028. if (fbc->internal_function.handler == ZEND_FN(pass)) {
  1029. /* Be lenient about the special pass function. */
  1030. return 0;
  1031. }
  1032. if (fbc->common.required_num_args > ZEND_CALL_NUM_ARGS(call)) {
  1033. /* Required argument not passed. */
  1034. return 1;
  1035. }
  1036. if (fbc->common.num_args < ZEND_CALL_NUM_ARGS(call)
  1037. && !(fbc->common.fn_flags & ZEND_ACC_VARIADIC)) {
  1038. /* Too many arguments passed. For internal functions (unlike userland functions),
  1039. * this should always throw. */
  1040. return 1;
  1041. }
  1042. if ((fbc->common.fn_flags & ZEND_ACC_HAS_TYPE_HINTS) &&
  1043. !zend_verify_internal_arg_types(fbc, call)) {
  1044. return 1;
  1045. }
  1046. return 0;
  1047. }
  1048. ZEND_API ZEND_COLD void zend_internal_call_arginfo_violation(zend_function *fbc)
  1049. {
  1050. zend_error(E_ERROR, "Arginfo / zpp mismatch during call of %s%s%s()",
  1051. fbc->common.scope ? ZSTR_VAL(fbc->common.scope->name) : "",
  1052. fbc->common.scope ? "::" : "",
  1053. ZSTR_VAL(fbc->common.function_name));
  1054. }
  1055. static void zend_verify_internal_read_property_type(zend_object *obj, zend_string *name, zval *val)
  1056. {
  1057. zend_property_info *prop_info =
  1058. zend_get_property_info(obj->ce, name, /* silent */ true);
  1059. if (prop_info && prop_info != ZEND_WRONG_PROPERTY_INFO && ZEND_TYPE_IS_SET(prop_info->type)) {
  1060. zend_verify_property_type(prop_info, val, /* strict */ true);
  1061. }
  1062. }
  1063. #ifndef ZEND_VERIFY_FUNC_INFO
  1064. # define ZEND_VERIFY_FUNC_INFO 0
  1065. #endif
  1066. static void zend_verify_internal_func_info(zend_function *fn, zval *retval) {
  1067. #if ZEND_VERIFY_FUNC_INFO
  1068. zend_string *name = fn->common.function_name;
  1069. uint32_t type_mask = zend_get_internal_func_info(fn, NULL, NULL);
  1070. if (!type_mask) {
  1071. return;
  1072. }
  1073. /* Always check refcount of arrays, as immutable arrays are RCN. */
  1074. if (Z_REFCOUNTED_P(retval) || Z_TYPE_P(retval) == IS_ARRAY) {
  1075. if (!(type_mask & MAY_BE_RC1)) {
  1076. zend_error_noreturn(E_CORE_ERROR, "%s() missing rc1", ZSTR_VAL(name));
  1077. }
  1078. if (Z_REFCOUNT_P(retval) > 1 && !(type_mask & MAY_BE_RCN)) {
  1079. zend_error_noreturn(E_CORE_ERROR, "%s() missing rcn", ZSTR_VAL(name));
  1080. }
  1081. }
  1082. uint32_t type = 1u << Z_TYPE_P(retval);
  1083. if (!(type_mask & type)) {
  1084. zend_error_noreturn(E_CORE_ERROR, "%s() missing type %s",
  1085. ZSTR_VAL(name), zend_get_type_by_const(Z_TYPE_P(retval)));
  1086. }
  1087. if (Z_TYPE_P(retval) == IS_ARRAY) {
  1088. HashTable *ht = Z_ARRVAL_P(retval);
  1089. uint32_t num_checked = 0;
  1090. zend_string *str;
  1091. zval *val;
  1092. ZEND_HASH_FOREACH_STR_KEY_VAL(ht, str, val) {
  1093. if (str) {
  1094. if (!(type_mask & MAY_BE_ARRAY_KEY_STRING)) {
  1095. zend_error_noreturn(E_CORE_ERROR,
  1096. "%s() missing array_key_string", ZSTR_VAL(name));
  1097. }
  1098. } else {
  1099. if (!(type_mask & MAY_BE_ARRAY_KEY_LONG)) {
  1100. zend_error_noreturn(E_CORE_ERROR,
  1101. "%s() missing array_key_long", ZSTR_VAL(name));
  1102. }
  1103. }
  1104. uint32_t array_type = 1u << (Z_TYPE_P(val) + MAY_BE_ARRAY_SHIFT);
  1105. if (!(type_mask & array_type)) {
  1106. zend_error_noreturn(E_CORE_ERROR,
  1107. "%s() missing array element type %s",
  1108. ZSTR_VAL(name), zend_get_type_by_const(Z_TYPE_P(retval)));
  1109. }
  1110. /* Don't check all elements of large arrays. */
  1111. if (++num_checked > 16) {
  1112. break;
  1113. }
  1114. } ZEND_HASH_FOREACH_END();
  1115. }
  1116. #endif
  1117. }
  1118. #endif
  1119. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_missing_arg_error(zend_execute_data *execute_data)
  1120. {
  1121. zend_execute_data *ptr = EX(prev_execute_data);
  1122. if (ptr && ptr->func && ZEND_USER_CODE(ptr->func->common.type)) {
  1123. zend_throw_error(zend_ce_argument_count_error, "Too few arguments to function %s%s%s(), %d passed in %s on line %d and %s %d expected",
  1124. EX(func)->common.scope ? ZSTR_VAL(EX(func)->common.scope->name) : "",
  1125. EX(func)->common.scope ? "::" : "",
  1126. ZSTR_VAL(EX(func)->common.function_name),
  1127. EX_NUM_ARGS(),
  1128. ZSTR_VAL(ptr->func->op_array.filename),
  1129. ptr->opline->lineno,
  1130. EX(func)->common.required_num_args == EX(func)->common.num_args ? "exactly" : "at least",
  1131. EX(func)->common.required_num_args);
  1132. } else {
  1133. zend_throw_error(zend_ce_argument_count_error, "Too few arguments to function %s%s%s(), %d passed and %s %d expected",
  1134. EX(func)->common.scope ? ZSTR_VAL(EX(func)->common.scope->name) : "",
  1135. EX(func)->common.scope ? "::" : "",
  1136. ZSTR_VAL(EX(func)->common.function_name),
  1137. EX_NUM_ARGS(),
  1138. EX(func)->common.required_num_args == EX(func)->common.num_args ? "exactly" : "at least",
  1139. EX(func)->common.required_num_args);
  1140. }
  1141. }
  1142. ZEND_API ZEND_COLD void zend_verify_return_error(const zend_function *zf, zval *value)
  1143. {
  1144. const zend_arg_info *arg_info = &zf->common.arg_info[-1];
  1145. const char *fname, *fsep, *fclass;
  1146. zend_string *need_msg;
  1147. const char *given_msg;
  1148. zend_verify_type_error_common(
  1149. zf, arg_info, value, &fname, &fsep, &fclass, &need_msg, &given_msg);
  1150. zend_type_error("%s%s%s(): Return value must be of type %s, %s returned",
  1151. fclass, fsep, fname, ZSTR_VAL(need_msg), given_msg);
  1152. zend_string_release(need_msg);
  1153. }
  1154. ZEND_API ZEND_COLD void zend_verify_never_error(const zend_function *zf)
  1155. {
  1156. zend_string *func_name = get_function_or_method_name(zf);
  1157. zend_type_error("%s(): never-returning function must not implicitly return",
  1158. ZSTR_VAL(func_name));
  1159. zend_string_release(func_name);
  1160. }
  1161. #if ZEND_DEBUG
  1162. static ZEND_COLD void zend_verify_internal_return_error(const zend_function *zf, zval *value)
  1163. {
  1164. const zend_arg_info *arg_info = &zf->common.arg_info[-1];
  1165. const char *fname, *fsep, *fclass;
  1166. zend_string *need_msg;
  1167. const char *given_msg;
  1168. zend_verify_type_error_common(
  1169. zf, arg_info, value, &fname, &fsep, &fclass, &need_msg, &given_msg);
  1170. zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): Return value must be of type %s, %s returned",
  1171. fclass, fsep, fname, ZSTR_VAL(need_msg), given_msg);
  1172. }
  1173. static ZEND_COLD void zend_verify_void_return_error(const zend_function *zf, const char *returned_msg, const char *returned_kind)
  1174. {
  1175. const char *fname = ZSTR_VAL(zf->common.function_name);
  1176. const char *fsep;
  1177. const char *fclass;
  1178. if (zf->common.scope) {
  1179. fsep = "::";
  1180. fclass = ZSTR_VAL(zf->common.scope->name);
  1181. } else {
  1182. fsep = "";
  1183. fclass = "";
  1184. }
  1185. zend_type_error("%s%s%s() must not return a value, %s%s returned",
  1186. fclass, fsep, fname, returned_msg, returned_kind);
  1187. }
  1188. ZEND_API bool zend_verify_internal_return_type(zend_function *zf, zval *ret)
  1189. {
  1190. zend_internal_arg_info *ret_info = zf->internal_function.arg_info - 1;
  1191. if (ZEND_TYPE_FULL_MASK(ret_info->type) & MAY_BE_VOID) {
  1192. if (UNEXPECTED(Z_TYPE_P(ret) != IS_NULL)) {
  1193. zend_verify_void_return_error(zf, zend_zval_type_name(ret), "");
  1194. return 0;
  1195. }
  1196. return 1;
  1197. }
  1198. if (UNEXPECTED(!zend_check_type(&ret_info->type, ret, /* cache_slot */ NULL, NULL, 1, /* is_internal */ 1))) {
  1199. zend_verify_internal_return_error(zf, ret);
  1200. return 0;
  1201. }
  1202. return 1;
  1203. }
  1204. #endif
  1205. static ZEND_COLD void zend_verify_missing_return_type(const zend_function *zf)
  1206. {
  1207. /* VERIFY_RETURN_TYPE is not emitted for "void" functions, so this is always an error. */
  1208. zend_verify_return_error(zf, NULL);
  1209. }
  1210. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_object_as_array(void)
  1211. {
  1212. zend_throw_error(NULL, "Cannot use object as array");
  1213. }
  1214. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_offset(void)
  1215. {
  1216. zend_type_error("Illegal offset type");
  1217. }
  1218. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_string_offset(const zval *offset)
  1219. {
  1220. zend_type_error("Cannot access offset of type %s on string", zend_zval_type_name(offset));
  1221. }
  1222. static zend_never_inline void zend_assign_to_object_dim(zend_object *obj, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC)
  1223. {
  1224. obj->handlers->write_dimension(obj, dim, value);
  1225. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1226. ZVAL_COPY(EX_VAR(opline->result.var), value);
  1227. }
  1228. }
  1229. static zend_always_inline int zend_binary_op(zval *ret, zval *op1, zval *op2 OPLINE_DC)
  1230. {
  1231. static const binary_op_type zend_binary_ops[] = {
  1232. add_function,
  1233. sub_function,
  1234. mul_function,
  1235. div_function,
  1236. mod_function,
  1237. shift_left_function,
  1238. shift_right_function,
  1239. concat_function,
  1240. bitwise_or_function,
  1241. bitwise_and_function,
  1242. bitwise_xor_function,
  1243. pow_function
  1244. };
  1245. /* size_t cast makes GCC to better optimize 64-bit PIC code */
  1246. size_t opcode = (size_t)opline->extended_value;
  1247. return zend_binary_ops[opcode - ZEND_ADD](ret, op1, op2);
  1248. }
  1249. static zend_never_inline void zend_binary_assign_op_obj_dim(zend_object *obj, zval *property OPLINE_DC EXECUTE_DATA_DC)
  1250. {
  1251. zval *value;
  1252. zval *z;
  1253. zval rv, res;
  1254. GC_ADDREF(obj);
  1255. if (property && UNEXPECTED(Z_ISUNDEF_P(property))) {
  1256. property = ZVAL_UNDEFINED_OP2();
  1257. }
  1258. value = get_op_data_zval_ptr_r((opline+1)->op1_type, (opline+1)->op1);
  1259. if ((z = obj->handlers->read_dimension(obj, property, BP_VAR_R, &rv)) != NULL) {
  1260. if (zend_binary_op(&res, z, value OPLINE_CC) == SUCCESS) {
  1261. obj->handlers->write_dimension(obj, property, &res);
  1262. }
  1263. if (z == &rv) {
  1264. zval_ptr_dtor(&rv);
  1265. }
  1266. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1267. ZVAL_COPY(EX_VAR(opline->result.var), &res);
  1268. }
  1269. zval_ptr_dtor(&res);
  1270. } else {
  1271. zend_use_object_as_array();
  1272. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1273. ZVAL_NULL(EX_VAR(opline->result.var));
  1274. }
  1275. }
  1276. FREE_OP((opline+1)->op1_type, (opline+1)->op1.var);
  1277. if (UNEXPECTED(GC_DELREF(obj) == 0)) {
  1278. zend_objects_store_del(obj);
  1279. }
  1280. }
  1281. static zend_never_inline void zend_binary_assign_op_typed_ref(zend_reference *ref, zval *value OPLINE_DC EXECUTE_DATA_DC)
  1282. {
  1283. zval z_copy;
  1284. /* Make sure that in-place concatenation is used if the LHS is a string. */
  1285. if (opline->extended_value == ZEND_CONCAT && Z_TYPE(ref->val) == IS_STRING) {
  1286. concat_function(&ref->val, &ref->val, value);
  1287. ZEND_ASSERT(Z_TYPE(ref->val) == IS_STRING && "Concat should return string");
  1288. return;
  1289. }
  1290. zend_binary_op(&z_copy, &ref->val, value OPLINE_CC);
  1291. if (EXPECTED(zend_verify_ref_assignable_zval(ref, &z_copy, EX_USES_STRICT_TYPES()))) {
  1292. zval_ptr_dtor(&ref->val);
  1293. ZVAL_COPY_VALUE(&ref->val, &z_copy);
  1294. } else {
  1295. zval_ptr_dtor(&z_copy);
  1296. }
  1297. }
  1298. static zend_never_inline void zend_binary_assign_op_typed_prop(zend_property_info *prop_info, zval *zptr, zval *value OPLINE_DC EXECUTE_DATA_DC)
  1299. {
  1300. zval z_copy;
  1301. /* Make sure that in-place concatenation is used if the LHS is a string. */
  1302. if (opline->extended_value == ZEND_CONCAT && Z_TYPE_P(zptr) == IS_STRING) {
  1303. concat_function(zptr, zptr, value);
  1304. ZEND_ASSERT(Z_TYPE_P(zptr) == IS_STRING && "Concat should return string");
  1305. return;
  1306. }
  1307. zend_binary_op(&z_copy, zptr, value OPLINE_CC);
  1308. if (EXPECTED(zend_verify_property_type(prop_info, &z_copy, EX_USES_STRICT_TYPES()))) {
  1309. zval_ptr_dtor(zptr);
  1310. ZVAL_COPY_VALUE(zptr, &z_copy);
  1311. } else {
  1312. zval_ptr_dtor(&z_copy);
  1313. }
  1314. }
  1315. static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type EXECUTE_DATA_DC)
  1316. {
  1317. zend_long offset;
  1318. try_again:
  1319. switch(Z_TYPE_P(dim)) {
  1320. case IS_LONG:
  1321. return Z_LVAL_P(dim);
  1322. case IS_STRING:
  1323. {
  1324. bool trailing_data = false;
  1325. /* For BC reasons we allow errors so that we can warn on leading numeric string */
  1326. if (IS_LONG == is_numeric_string_ex(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL,
  1327. /* allow errors */ true, NULL, &trailing_data)) {
  1328. if (UNEXPECTED(trailing_data) && type != BP_VAR_UNSET) {
  1329. zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
  1330. }
  1331. return offset;
  1332. }
  1333. zend_illegal_string_offset(dim);
  1334. return 0;
  1335. }
  1336. case IS_UNDEF:
  1337. ZVAL_UNDEFINED_OP2();
  1338. ZEND_FALLTHROUGH;
  1339. case IS_DOUBLE:
  1340. case IS_NULL:
  1341. case IS_FALSE:
  1342. case IS_TRUE:
  1343. zend_error(E_WARNING, "String offset cast occurred");
  1344. break;
  1345. case IS_REFERENCE:
  1346. dim = Z_REFVAL_P(dim);
  1347. goto try_again;
  1348. default:
  1349. zend_illegal_string_offset(dim);
  1350. return 0;
  1351. }
  1352. return zval_get_long_func(dim, /* is_strict */ false);
  1353. }
  1354. ZEND_API ZEND_COLD void zend_wrong_string_offset_error(void)
  1355. {
  1356. const char *msg = NULL;
  1357. const zend_execute_data *execute_data = EG(current_execute_data);
  1358. const zend_op *opline = execute_data->opline;
  1359. uint32_t var;
  1360. if (UNEXPECTED(EG(exception) != NULL)) {
  1361. return;
  1362. }
  1363. switch (opline->opcode) {
  1364. case ZEND_ASSIGN_OP:
  1365. case ZEND_ASSIGN_DIM_OP:
  1366. case ZEND_ASSIGN_OBJ_OP:
  1367. case ZEND_ASSIGN_STATIC_PROP_OP:
  1368. msg = "Cannot use assign-op operators with string offsets";
  1369. break;
  1370. case ZEND_FETCH_DIM_W:
  1371. case ZEND_FETCH_DIM_RW:
  1372. case ZEND_FETCH_DIM_FUNC_ARG:
  1373. case ZEND_FETCH_DIM_UNSET:
  1374. case ZEND_FETCH_LIST_W:
  1375. /* TODO: Encode the "reason" into opline->extended_value??? */
  1376. var = opline->result.var;
  1377. opline++;
  1378. ZEND_ASSERT(opline < execute_data->func->op_array.opcodes +
  1379. execute_data->func->op_array.last);
  1380. if (opline->op1_type == IS_VAR && opline->op1.var == var) {
  1381. switch (opline->opcode) {
  1382. case ZEND_FETCH_OBJ_W:
  1383. case ZEND_FETCH_OBJ_RW:
  1384. case ZEND_FETCH_OBJ_FUNC_ARG:
  1385. case ZEND_FETCH_OBJ_UNSET:
  1386. case ZEND_ASSIGN_OBJ:
  1387. case ZEND_ASSIGN_OBJ_OP:
  1388. case ZEND_ASSIGN_OBJ_REF:
  1389. msg = "Cannot use string offset as an object";
  1390. break;
  1391. case ZEND_FETCH_DIM_W:
  1392. case ZEND_FETCH_DIM_RW:
  1393. case ZEND_FETCH_DIM_FUNC_ARG:
  1394. case ZEND_FETCH_DIM_UNSET:
  1395. case ZEND_FETCH_LIST_W:
  1396. case ZEND_ASSIGN_DIM:
  1397. case ZEND_ASSIGN_DIM_OP:
  1398. msg = "Cannot use string offset as an array";
  1399. break;
  1400. case ZEND_ASSIGN_STATIC_PROP_OP:
  1401. case ZEND_ASSIGN_OP:
  1402. msg = "Cannot use assign-op operators with string offsets";
  1403. break;
  1404. case ZEND_PRE_INC_OBJ:
  1405. case ZEND_PRE_DEC_OBJ:
  1406. case ZEND_POST_INC_OBJ:
  1407. case ZEND_POST_DEC_OBJ:
  1408. case ZEND_PRE_INC:
  1409. case ZEND_PRE_DEC:
  1410. case ZEND_POST_INC:
  1411. case ZEND_POST_DEC:
  1412. msg = "Cannot increment/decrement string offsets";
  1413. break;
  1414. case ZEND_ASSIGN_REF:
  1415. case ZEND_ADD_ARRAY_ELEMENT:
  1416. case ZEND_INIT_ARRAY:
  1417. case ZEND_MAKE_REF:
  1418. msg = "Cannot create references to/from string offsets";
  1419. break;
  1420. case ZEND_RETURN_BY_REF:
  1421. case ZEND_VERIFY_RETURN_TYPE:
  1422. msg = "Cannot return string offsets by reference";
  1423. break;
  1424. case ZEND_UNSET_DIM:
  1425. case ZEND_UNSET_OBJ:
  1426. msg = "Cannot unset string offsets";
  1427. break;
  1428. case ZEND_YIELD:
  1429. msg = "Cannot yield string offsets by reference";
  1430. break;
  1431. case ZEND_SEND_REF:
  1432. case ZEND_SEND_VAR_EX:
  1433. case ZEND_SEND_FUNC_ARG:
  1434. msg = "Only variables can be passed by reference";
  1435. break;
  1436. case ZEND_FE_RESET_RW:
  1437. msg = "Cannot iterate on string offsets by reference";
  1438. break;
  1439. EMPTY_SWITCH_DEFAULT_CASE();
  1440. }
  1441. break;
  1442. }
  1443. if (opline->op2_type == IS_VAR && opline->op2.var == var) {
  1444. ZEND_ASSERT(opline->opcode == ZEND_ASSIGN_REF);
  1445. msg = "Cannot create references to/from string offsets";
  1446. break;
  1447. }
  1448. break;
  1449. EMPTY_SWITCH_DEFAULT_CASE();
  1450. }
  1451. ZEND_ASSERT(msg != NULL);
  1452. zend_throw_error(NULL, "%s", msg);
  1453. }
  1454. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_wrong_property_read(zval *object, zval *property)
  1455. {
  1456. zend_string *tmp_property_name;
  1457. zend_string *property_name = zval_get_tmp_string(property, &tmp_property_name);
  1458. zend_error(E_WARNING, "Attempt to read property \"%s\" on %s", ZSTR_VAL(property_name), zend_zval_type_name(object));
  1459. zend_tmp_string_release(tmp_property_name);
  1460. }
  1461. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_deprecated_function(const zend_function *fbc)
  1462. {
  1463. if (fbc->common.scope) {
  1464. zend_error(E_DEPRECATED, "Method %s::%s() is deprecated",
  1465. ZSTR_VAL(fbc->common.scope->name),
  1466. ZSTR_VAL(fbc->common.function_name)
  1467. );
  1468. } else {
  1469. zend_error(E_DEPRECATED, "Function %s() is deprecated", ZSTR_VAL(fbc->common.function_name));
  1470. }
  1471. }
  1472. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_false_to_array_deprecated(void)
  1473. {
  1474. zend_error(E_DEPRECATED, "Automatic conversion of false to array is deprecated");
  1475. }
  1476. static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC)
  1477. {
  1478. zend_uchar c;
  1479. size_t string_len;
  1480. zend_long offset;
  1481. zend_string *s;
  1482. /* separate string */
  1483. if (Z_REFCOUNTED_P(str) && Z_REFCOUNT_P(str) == 1) {
  1484. s = Z_STR_P(str);
  1485. } else {
  1486. s = zend_string_init(Z_STRVAL_P(str), Z_STRLEN_P(str), 0);
  1487. ZSTR_H(s) = ZSTR_H(Z_STR_P(str));
  1488. if (Z_REFCOUNTED_P(str)) {
  1489. GC_DELREF(Z_STR_P(str));
  1490. }
  1491. ZVAL_NEW_STR(str, s);
  1492. }
  1493. if (EXPECTED(Z_TYPE_P(dim) == IS_LONG)) {
  1494. offset = Z_LVAL_P(dim);
  1495. } else {
  1496. /* The string may be destroyed while throwing the notice.
  1497. * Temporarily increase the refcount to detect this situation. */
  1498. GC_ADDREF(s);
  1499. offset = zend_check_string_offset(dim, BP_VAR_W EXECUTE_DATA_CC);
  1500. if (UNEXPECTED(GC_DELREF(s) == 0)) {
  1501. zend_string_efree(s);
  1502. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1503. ZVAL_NULL(EX_VAR(opline->result.var));
  1504. }
  1505. return;
  1506. }
  1507. /* Illegal offset assignment */
  1508. if (UNEXPECTED(EG(exception) != NULL)) {
  1509. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1510. ZVAL_UNDEF(EX_VAR(opline->result.var));
  1511. }
  1512. return;
  1513. }
  1514. }
  1515. if (UNEXPECTED(offset < -(zend_long)ZSTR_LEN(s))) {
  1516. /* Error on negative offset */
  1517. zend_error(E_WARNING, "Illegal string offset " ZEND_LONG_FMT, offset);
  1518. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1519. ZVAL_NULL(EX_VAR(opline->result.var));
  1520. }
  1521. return;
  1522. }
  1523. if (offset < 0) { /* Handle negative offset */
  1524. offset += (zend_long)ZSTR_LEN(s);
  1525. }
  1526. if (UNEXPECTED(Z_TYPE_P(value) != IS_STRING)) {
  1527. zend_string *tmp;
  1528. /* The string may be destroyed while throwing the notice.
  1529. * Temporarily increase the refcount to detect this situation. */
  1530. GC_ADDREF(s);
  1531. if (UNEXPECTED(Z_TYPE_P(value) == IS_UNDEF)) {
  1532. zval_undefined_cv((opline+1)->op1.var EXECUTE_DATA_CC);
  1533. }
  1534. /* Convert to string, just the time to pick the 1st byte */
  1535. tmp = zval_try_get_string_func(value);
  1536. if (UNEXPECTED(GC_DELREF(s) == 0)) {
  1537. zend_string_efree(s);
  1538. if (tmp) {
  1539. zend_string_release_ex(tmp, 0);
  1540. }
  1541. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1542. ZVAL_NULL(EX_VAR(opline->result.var));
  1543. }
  1544. return;
  1545. }
  1546. if (UNEXPECTED(!tmp)) {
  1547. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1548. ZVAL_UNDEF(EX_VAR(opline->result.var));
  1549. }
  1550. return;
  1551. }
  1552. string_len = ZSTR_LEN(tmp);
  1553. c = (zend_uchar)ZSTR_VAL(tmp)[0];
  1554. zend_string_release_ex(tmp, 0);
  1555. } else {
  1556. string_len = Z_STRLEN_P(value);
  1557. c = (zend_uchar)Z_STRVAL_P(value)[0];
  1558. }
  1559. if (UNEXPECTED(string_len != 1)) {
  1560. if (string_len == 0) {
  1561. /* Error on empty input string */
  1562. zend_throw_error(NULL, "Cannot assign an empty string to a string offset");
  1563. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1564. ZVAL_NULL(EX_VAR(opline->result.var));
  1565. }
  1566. return;
  1567. }
  1568. /* The string may be destroyed while throwing the notice.
  1569. * Temporarily increase the refcount to detect this situation. */
  1570. GC_ADDREF(s);
  1571. zend_error(E_WARNING, "Only the first byte will be assigned to the string offset");
  1572. if (UNEXPECTED(GC_DELREF(s) == 0)) {
  1573. zend_string_efree(s);
  1574. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1575. ZVAL_NULL(EX_VAR(opline->result.var));
  1576. }
  1577. return;
  1578. }
  1579. /* Illegal offset assignment */
  1580. if (UNEXPECTED(EG(exception) != NULL)) {
  1581. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1582. ZVAL_UNDEF(EX_VAR(opline->result.var));
  1583. }
  1584. return;
  1585. }
  1586. }
  1587. if ((size_t)offset >= ZSTR_LEN(s)) {
  1588. /* Extend string if needed */
  1589. zend_long old_len = ZSTR_LEN(s);
  1590. ZVAL_NEW_STR(str, zend_string_extend(s, (size_t)offset + 1, 0));
  1591. memset(Z_STRVAL_P(str) + old_len, ' ', offset - old_len);
  1592. Z_STRVAL_P(str)[offset+1] = 0;
  1593. } else {
  1594. zend_string_forget_hash_val(Z_STR_P(str));
  1595. }
  1596. Z_STRVAL_P(str)[offset] = c;
  1597. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1598. /* Return the new character */
  1599. ZVAL_CHAR(EX_VAR(opline->result.var), c);
  1600. }
  1601. }
  1602. static zend_property_info *zend_get_prop_not_accepting_double(zend_reference *ref)
  1603. {
  1604. zend_property_info *prop;
  1605. ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) {
  1606. if (!(ZEND_TYPE_FULL_MASK(prop->type) & MAY_BE_DOUBLE)) {
  1607. return prop;
  1608. }
  1609. } ZEND_REF_FOREACH_TYPE_SOURCES_END();
  1610. return NULL;
  1611. }
  1612. static ZEND_COLD zend_long zend_throw_incdec_ref_error(
  1613. zend_reference *ref, zend_property_info *error_prop OPLINE_DC)
  1614. {
  1615. zend_string *type_str = zend_type_to_string(error_prop->type);
  1616. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1617. zend_type_error(
  1618. "Cannot increment a reference held by property %s::$%s of type %s past its maximal value",
  1619. ZSTR_VAL(error_prop->ce->name),
  1620. zend_get_unmangled_property_name(error_prop->name),
  1621. ZSTR_VAL(type_str));
  1622. zend_string_release(type_str);
  1623. return ZEND_LONG_MAX;
  1624. } else {
  1625. zend_type_error(
  1626. "Cannot decrement a reference held by property %s::$%s of type %s past its minimal value",
  1627. ZSTR_VAL(error_prop->ce->name),
  1628. zend_get_unmangled_property_name(error_prop->name),
  1629. ZSTR_VAL(type_str));
  1630. zend_string_release(type_str);
  1631. return ZEND_LONG_MIN;
  1632. }
  1633. }
  1634. static ZEND_COLD zend_long zend_throw_incdec_prop_error(zend_property_info *prop OPLINE_DC) {
  1635. zend_string *type_str = zend_type_to_string(prop->type);
  1636. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1637. zend_type_error("Cannot increment property %s::$%s of type %s past its maximal value",
  1638. ZSTR_VAL(prop->ce->name),
  1639. zend_get_unmangled_property_name(prop->name),
  1640. ZSTR_VAL(type_str));
  1641. zend_string_release(type_str);
  1642. return ZEND_LONG_MAX;
  1643. } else {
  1644. zend_type_error("Cannot decrement property %s::$%s of type %s past its minimal value",
  1645. ZSTR_VAL(prop->ce->name),
  1646. zend_get_unmangled_property_name(prop->name),
  1647. ZSTR_VAL(type_str));
  1648. zend_string_release(type_str);
  1649. return ZEND_LONG_MIN;
  1650. }
  1651. }
  1652. static void zend_incdec_typed_ref(zend_reference *ref, zval *copy OPLINE_DC EXECUTE_DATA_DC)
  1653. {
  1654. zval tmp;
  1655. zval *var_ptr = &ref->val;
  1656. if (!copy) {
  1657. copy = &tmp;
  1658. }
  1659. ZVAL_COPY(copy, var_ptr);
  1660. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1661. increment_function(var_ptr);
  1662. } else {
  1663. decrement_function(var_ptr);
  1664. }
  1665. if (UNEXPECTED(Z_TYPE_P(var_ptr) == IS_DOUBLE) && Z_TYPE_P(copy) == IS_LONG) {
  1666. zend_property_info *error_prop = zend_get_prop_not_accepting_double(ref);
  1667. if (UNEXPECTED(error_prop)) {
  1668. zend_long val = zend_throw_incdec_ref_error(ref, error_prop OPLINE_CC);
  1669. ZVAL_LONG(var_ptr, val);
  1670. }
  1671. } else if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, var_ptr, EX_USES_STRICT_TYPES()))) {
  1672. zval_ptr_dtor(var_ptr);
  1673. ZVAL_COPY_VALUE(var_ptr, copy);
  1674. ZVAL_UNDEF(copy);
  1675. } else if (copy == &tmp) {
  1676. zval_ptr_dtor(&tmp);
  1677. }
  1678. }
  1679. static void zend_incdec_typed_prop(zend_property_info *prop_info, zval *var_ptr, zval *copy OPLINE_DC EXECUTE_DATA_DC)
  1680. {
  1681. zval tmp;
  1682. if (!copy) {
  1683. copy = &tmp;
  1684. }
  1685. ZVAL_COPY(copy, var_ptr);
  1686. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1687. increment_function(var_ptr);
  1688. } else {
  1689. decrement_function(var_ptr);
  1690. }
  1691. if (UNEXPECTED(Z_TYPE_P(var_ptr) == IS_DOUBLE) && Z_TYPE_P(copy) == IS_LONG) {
  1692. if (!(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
  1693. zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
  1694. ZVAL_LONG(var_ptr, val);
  1695. }
  1696. } else if (UNEXPECTED(!zend_verify_property_type(prop_info, var_ptr, EX_USES_STRICT_TYPES()))) {
  1697. zval_ptr_dtor(var_ptr);
  1698. ZVAL_COPY_VALUE(var_ptr, copy);
  1699. ZVAL_UNDEF(copy);
  1700. } else if (copy == &tmp) {
  1701. zval_ptr_dtor(&tmp);
  1702. }
  1703. }
  1704. static void zend_pre_incdec_property_zval(zval *prop, zend_property_info *prop_info OPLINE_DC EXECUTE_DATA_DC)
  1705. {
  1706. if (EXPECTED(Z_TYPE_P(prop) == IS_LONG)) {
  1707. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1708. fast_long_increment_function(prop);
  1709. } else {
  1710. fast_long_decrement_function(prop);
  1711. }
  1712. if (UNEXPECTED(Z_TYPE_P(prop) != IS_LONG) && UNEXPECTED(prop_info)
  1713. && !(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
  1714. zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
  1715. ZVAL_LONG(prop, val);
  1716. }
  1717. } else {
  1718. do {
  1719. if (Z_ISREF_P(prop)) {
  1720. zend_reference *ref = Z_REF_P(prop);
  1721. prop = Z_REFVAL_P(prop);
  1722. if (UNEXPECTED(ZEND_REF_HAS_TYPE_SOURCES(ref))) {
  1723. zend_incdec_typed_ref(ref, NULL OPLINE_CC EXECUTE_DATA_CC);
  1724. break;
  1725. }
  1726. }
  1727. if (UNEXPECTED(prop_info)) {
  1728. zend_incdec_typed_prop(prop_info, prop, NULL OPLINE_CC EXECUTE_DATA_CC);
  1729. } else if (ZEND_IS_INCREMENT(opline->opcode)) {
  1730. increment_function(prop);
  1731. } else {
  1732. decrement_function(prop);
  1733. }
  1734. } while (0);
  1735. }
  1736. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1737. ZVAL_COPY(EX_VAR(opline->result.var), prop);
  1738. }
  1739. }
  1740. static void zend_post_incdec_property_zval(zval *prop, zend_property_info *prop_info OPLINE_DC EXECUTE_DATA_DC)
  1741. {
  1742. if (EXPECTED(Z_TYPE_P(prop) == IS_LONG)) {
  1743. ZVAL_LONG(EX_VAR(opline->result.var), Z_LVAL_P(prop));
  1744. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1745. fast_long_increment_function(prop);
  1746. } else {
  1747. fast_long_decrement_function(prop);
  1748. }
  1749. if (UNEXPECTED(Z_TYPE_P(prop) != IS_LONG) && UNEXPECTED(prop_info)
  1750. && !(ZEND_TYPE_FULL_MASK(prop_info->type) & MAY_BE_DOUBLE)) {
  1751. zend_long val = zend_throw_incdec_prop_error(prop_info OPLINE_CC);
  1752. ZVAL_LONG(prop, val);
  1753. }
  1754. } else {
  1755. if (Z_ISREF_P(prop)) {
  1756. zend_reference *ref = Z_REF_P(prop);
  1757. prop = Z_REFVAL_P(prop);
  1758. if (ZEND_REF_HAS_TYPE_SOURCES(ref)) {
  1759. zend_incdec_typed_ref(ref, EX_VAR(opline->result.var) OPLINE_CC EXECUTE_DATA_CC);
  1760. return;
  1761. }
  1762. }
  1763. if (UNEXPECTED(prop_info)) {
  1764. zend_incdec_typed_prop(prop_info, prop, EX_VAR(opline->result.var) OPLINE_CC EXECUTE_DATA_CC);
  1765. } else {
  1766. ZVAL_COPY(EX_VAR(opline->result.var), prop);
  1767. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1768. increment_function(prop);
  1769. } else {
  1770. decrement_function(prop);
  1771. }
  1772. }
  1773. }
  1774. }
  1775. static zend_never_inline void zend_post_incdec_overloaded_property(zend_object *object, zend_string *name, void **cache_slot OPLINE_DC EXECUTE_DATA_DC)
  1776. {
  1777. zval rv;
  1778. zval *z;
  1779. zval z_copy;
  1780. GC_ADDREF(object);
  1781. z =object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv);
  1782. if (UNEXPECTED(EG(exception))) {
  1783. OBJ_RELEASE(object);
  1784. ZVAL_UNDEF(EX_VAR(opline->result.var));
  1785. return;
  1786. }
  1787. ZVAL_COPY_DEREF(&z_copy, z);
  1788. ZVAL_COPY(EX_VAR(opline->result.var), &z_copy);
  1789. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1790. increment_function(&z_copy);
  1791. } else {
  1792. decrement_function(&z_copy);
  1793. }
  1794. object->handlers->write_property(object, name, &z_copy, cache_slot);
  1795. OBJ_RELEASE(object);
  1796. zval_ptr_dtor(&z_copy);
  1797. if (z == &rv) {
  1798. zval_ptr_dtor(z);
  1799. }
  1800. }
  1801. static zend_never_inline void zend_pre_incdec_overloaded_property(zend_object *object, zend_string *name, void **cache_slot OPLINE_DC EXECUTE_DATA_DC)
  1802. {
  1803. zval rv;
  1804. zval *z;
  1805. zval z_copy;
  1806. GC_ADDREF(object);
  1807. z = object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv);
  1808. if (UNEXPECTED(EG(exception))) {
  1809. OBJ_RELEASE(object);
  1810. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1811. ZVAL_NULL(EX_VAR(opline->result.var));
  1812. }
  1813. return;
  1814. }
  1815. ZVAL_COPY_DEREF(&z_copy, z);
  1816. if (ZEND_IS_INCREMENT(opline->opcode)) {
  1817. increment_function(&z_copy);
  1818. } else {
  1819. decrement_function(&z_copy);
  1820. }
  1821. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1822. ZVAL_COPY(EX_VAR(opline->result.var), &z_copy);
  1823. }
  1824. object->handlers->write_property(object, name, &z_copy, cache_slot);
  1825. OBJ_RELEASE(object);
  1826. zval_ptr_dtor(&z_copy);
  1827. if (z == &rv) {
  1828. zval_ptr_dtor(z);
  1829. }
  1830. }
  1831. static zend_never_inline void zend_assign_op_overloaded_property(zend_object *object, zend_string *name, void **cache_slot, zval *value OPLINE_DC EXECUTE_DATA_DC)
  1832. {
  1833. zval *z;
  1834. zval rv, res;
  1835. GC_ADDREF(object);
  1836. z = object->handlers->read_property(object, name, BP_VAR_R, cache_slot, &rv);
  1837. if (UNEXPECTED(EG(exception))) {
  1838. OBJ_RELEASE(object);
  1839. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1840. ZVAL_UNDEF(EX_VAR(opline->result.var));
  1841. }
  1842. return;
  1843. }
  1844. if (zend_binary_op(&res, z, value OPLINE_CC) == SUCCESS) {
  1845. object->handlers->write_property(object, name, &res, cache_slot);
  1846. }
  1847. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  1848. ZVAL_COPY(EX_VAR(opline->result.var), &res);
  1849. }
  1850. if (z == &rv) {
  1851. zval_ptr_dtor(z);
  1852. }
  1853. zval_ptr_dtor(&res);
  1854. OBJ_RELEASE(object);
  1855. }
  1856. /* Utility Functions for Extensions */
  1857. static void zend_extension_statement_handler(const zend_extension *extension, zend_execute_data *frame)
  1858. {
  1859. if (extension->statement_handler) {
  1860. extension->statement_handler(frame);
  1861. }
  1862. }
  1863. static void zend_extension_fcall_begin_handler(const zend_extension *extension, zend_execute_data *frame)
  1864. {
  1865. if (extension->fcall_begin_handler) {
  1866. extension->fcall_begin_handler(frame);
  1867. }
  1868. }
  1869. static void zend_extension_fcall_end_handler(const zend_extension *extension, zend_execute_data *frame)
  1870. {
  1871. if (extension->fcall_end_handler) {
  1872. extension->fcall_end_handler(frame);
  1873. }
  1874. }
  1875. static zend_always_inline HashTable *zend_get_target_symbol_table(int fetch_type EXECUTE_DATA_DC)
  1876. {
  1877. HashTable *ht;
  1878. if (EXPECTED(fetch_type & (ZEND_FETCH_GLOBAL_LOCK | ZEND_FETCH_GLOBAL))) {
  1879. ht = &EG(symbol_table);
  1880. } else {
  1881. ZEND_ASSERT(fetch_type & ZEND_FETCH_LOCAL);
  1882. if (!(EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE)) {
  1883. zend_rebuild_symbol_table();
  1884. }
  1885. ht = EX(symbol_table);
  1886. }
  1887. return ht;
  1888. }
  1889. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_undefined_offset(zend_long lval)
  1890. {
  1891. zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, lval);
  1892. }
  1893. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_undefined_index(const zend_string *offset)
  1894. {
  1895. zend_error(E_WARNING, "Undefined array key \"%s\"", ZSTR_VAL(offset));
  1896. }
  1897. ZEND_API ZEND_COLD zval* ZEND_FASTCALL zend_undefined_offset_write(HashTable *ht, zend_long lval)
  1898. {
  1899. /* The array may be destroyed while throwing the notice.
  1900. * Temporarily increase the refcount to detect this situation. */
  1901. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
  1902. GC_ADDREF(ht);
  1903. }
  1904. zend_undefined_offset(lval);
  1905. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
  1906. if (!GC_REFCOUNT(ht)) {
  1907. zend_array_destroy(ht);
  1908. }
  1909. return NULL;
  1910. }
  1911. if (EG(exception)) {
  1912. return NULL;
  1913. }
  1914. return zend_hash_index_add_new(ht, lval, &EG(uninitialized_zval));
  1915. }
  1916. ZEND_API ZEND_COLD zval* ZEND_FASTCALL zend_undefined_index_write(HashTable *ht, zend_string *offset)
  1917. {
  1918. zval *retval;
  1919. /* The array may be destroyed while throwing the notice.
  1920. * Temporarily increase the refcount to detect this situation. */
  1921. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
  1922. GC_ADDREF(ht);
  1923. }
  1924. /* Key may be released while throwing the undefined index warning. */
  1925. zend_string_addref(offset);
  1926. zend_undefined_index(offset);
  1927. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
  1928. if (!GC_REFCOUNT(ht)) {
  1929. zend_array_destroy(ht);
  1930. }
  1931. retval = NULL;
  1932. } else if (EG(exception)) {
  1933. retval = NULL;
  1934. } else {
  1935. retval = zend_hash_add_new(ht, offset, &EG(uninitialized_zval));
  1936. }
  1937. zend_string_release(offset);
  1938. return retval;
  1939. }
  1940. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_undefined_method(const zend_class_entry *ce, const zend_string *method)
  1941. {
  1942. zend_throw_error(NULL, "Call to undefined method %s::%s()", ZSTR_VAL(ce->name), ZSTR_VAL(method));
  1943. }
  1944. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_invalid_method_call(zval *object, zval *function_name)
  1945. {
  1946. zend_throw_error(NULL, "Call to a member function %s() on %s",
  1947. Z_STRVAL_P(function_name), zend_zval_type_name(object));
  1948. }
  1949. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_non_static_method_call(const zend_function *fbc)
  1950. {
  1951. zend_throw_error(
  1952. zend_ce_error,
  1953. "Non-static method %s::%s() cannot be called statically",
  1954. ZSTR_VAL(fbc->common.scope->name), ZSTR_VAL(fbc->common.function_name));
  1955. }
  1956. ZEND_COLD void ZEND_FASTCALL zend_param_must_be_ref(const zend_function *func, uint32_t arg_num)
  1957. {
  1958. const char *arg_name = get_function_arg_name(func, arg_num);
  1959. zend_error(E_WARNING, "%s%s%s(): Argument #%d%s%s%s must be passed by reference, value given",
  1960. func->common.scope ? ZSTR_VAL(func->common.scope->name) : "",
  1961. func->common.scope ? "::" : "",
  1962. ZSTR_VAL(func->common.function_name),
  1963. arg_num,
  1964. arg_name ? " ($" : "",
  1965. arg_name ? arg_name : "",
  1966. arg_name ? ")" : ""
  1967. );
  1968. }
  1969. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_scalar_as_array(void)
  1970. {
  1971. zend_throw_error(NULL, "Cannot use a scalar value as an array");
  1972. }
  1973. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_cannot_add_element(void)
  1974. {
  1975. zend_throw_error(NULL, "Cannot add element to the array as the next element is already occupied");
  1976. }
  1977. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_use_resource_as_offset(const zval *dim)
  1978. {
  1979. zend_error(E_WARNING,
  1980. "Resource ID#" ZEND_LONG_FMT " used as offset, casting to integer (" ZEND_LONG_FMT ")",
  1981. Z_RES_HANDLE_P(dim), Z_RES_HANDLE_P(dim));
  1982. }
  1983. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_new_element_for_string(void)
  1984. {
  1985. zend_throw_error(NULL, "[] operator not supported for strings");
  1986. }
  1987. static ZEND_COLD void zend_binary_assign_op_dim_slow(zval *container, zval *dim OPLINE_DC EXECUTE_DATA_DC)
  1988. {
  1989. if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
  1990. if (opline->op2_type == IS_UNUSED) {
  1991. zend_use_new_element_for_string();
  1992. } else {
  1993. zend_check_string_offset(dim, BP_VAR_RW EXECUTE_DATA_CC);
  1994. zend_wrong_string_offset_error();
  1995. }
  1996. } else {
  1997. zend_use_scalar_as_array();
  1998. }
  1999. }
  2000. static zend_never_inline zend_uchar slow_index_convert(HashTable *ht, const zval *dim, zend_value *value EXECUTE_DATA_DC)
  2001. {
  2002. switch (Z_TYPE_P(dim)) {
  2003. case IS_UNDEF: {
  2004. /* The array may be destroyed while throwing the notice.
  2005. * Temporarily increase the refcount to detect this situation. */
  2006. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
  2007. GC_ADDREF(ht);
  2008. }
  2009. ZVAL_UNDEFINED_OP2();
  2010. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
  2011. zend_array_destroy(ht);
  2012. return IS_NULL;
  2013. }
  2014. if (EG(exception)) {
  2015. return IS_NULL;
  2016. }
  2017. ZEND_FALLTHROUGH;
  2018. }
  2019. case IS_NULL:
  2020. value->str = ZSTR_EMPTY_ALLOC();
  2021. return IS_STRING;
  2022. case IS_DOUBLE:
  2023. value->lval = zend_dval_to_lval(Z_DVAL_P(dim));
  2024. if (!zend_is_long_compatible(Z_DVAL_P(dim), value->lval)) {
  2025. /* The array may be destroyed while throwing the notice.
  2026. * Temporarily increase the refcount to detect this situation. */
  2027. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
  2028. GC_ADDREF(ht);
  2029. }
  2030. zend_incompatible_double_to_long_error(Z_DVAL_P(dim));
  2031. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
  2032. zend_array_destroy(ht);
  2033. return IS_NULL;
  2034. }
  2035. if (EG(exception)) {
  2036. return IS_NULL;
  2037. }
  2038. }
  2039. return IS_LONG;
  2040. case IS_RESOURCE:
  2041. /* The array may be destroyed while throwing the notice.
  2042. * Temporarily increase the refcount to detect this situation. */
  2043. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
  2044. GC_ADDREF(ht);
  2045. }
  2046. zend_use_resource_as_offset(dim);
  2047. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && !GC_DELREF(ht)) {
  2048. zend_array_destroy(ht);
  2049. return IS_NULL;
  2050. }
  2051. if (EG(exception)) {
  2052. return IS_NULL;
  2053. }
  2054. value->lval = Z_RES_HANDLE_P(dim);
  2055. return IS_LONG;
  2056. case IS_FALSE:
  2057. value->lval = 0;
  2058. return IS_LONG;
  2059. case IS_TRUE:
  2060. value->lval = 1;
  2061. return IS_LONG;
  2062. default:
  2063. zend_illegal_offset();
  2064. return IS_NULL;
  2065. }
  2066. }
  2067. static zend_never_inline zend_uchar slow_index_convert_w(HashTable *ht, const zval *dim, zend_value *value EXECUTE_DATA_DC)
  2068. {
  2069. switch (Z_TYPE_P(dim)) {
  2070. case IS_UNDEF: {
  2071. /* The array may be destroyed while throwing the notice.
  2072. * Temporarily increase the refcount to detect this situation. */
  2073. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
  2074. GC_ADDREF(ht);
  2075. }
  2076. ZVAL_UNDEFINED_OP2();
  2077. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
  2078. if (!GC_REFCOUNT(ht)) {
  2079. zend_array_destroy(ht);
  2080. }
  2081. return IS_NULL;
  2082. }
  2083. if (EG(exception)) {
  2084. return IS_NULL;
  2085. }
  2086. ZEND_FALLTHROUGH;
  2087. }
  2088. case IS_NULL:
  2089. value->str = ZSTR_EMPTY_ALLOC();
  2090. return IS_STRING;
  2091. case IS_DOUBLE:
  2092. value->lval = zend_dval_to_lval(Z_DVAL_P(dim));
  2093. if (!zend_is_long_compatible(Z_DVAL_P(dim), value->lval)) {
  2094. /* The array may be destroyed while throwing the notice.
  2095. * Temporarily increase the refcount to detect this situation. */
  2096. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
  2097. GC_ADDREF(ht);
  2098. }
  2099. zend_incompatible_double_to_long_error(Z_DVAL_P(dim));
  2100. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
  2101. if (!GC_REFCOUNT(ht)) {
  2102. zend_array_destroy(ht);
  2103. }
  2104. return IS_NULL;
  2105. }
  2106. if (EG(exception)) {
  2107. return IS_NULL;
  2108. }
  2109. }
  2110. return IS_LONG;
  2111. case IS_RESOURCE:
  2112. /* The array may be destroyed while throwing the notice.
  2113. * Temporarily increase the refcount to detect this situation. */
  2114. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
  2115. GC_ADDREF(ht);
  2116. }
  2117. zend_use_resource_as_offset(dim);
  2118. if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE) && GC_DELREF(ht) != 1) {
  2119. if (!GC_REFCOUNT(ht)) {
  2120. zend_array_destroy(ht);
  2121. }
  2122. return IS_NULL;
  2123. }
  2124. if (EG(exception)) {
  2125. return IS_NULL;
  2126. }
  2127. value->lval = Z_RES_HANDLE_P(dim);
  2128. return IS_LONG;
  2129. case IS_FALSE:
  2130. value->lval = 0;
  2131. return IS_LONG;
  2132. case IS_TRUE:
  2133. value->lval = 1;
  2134. return IS_LONG;
  2135. default:
  2136. zend_illegal_offset();
  2137. return IS_NULL;
  2138. }
  2139. }
  2140. static zend_always_inline zval *zend_fetch_dimension_address_inner(HashTable *ht, const zval *dim, int dim_type, int type EXECUTE_DATA_DC)
  2141. {
  2142. zval *retval = NULL;
  2143. zend_string *offset_key;
  2144. zend_ulong hval;
  2145. try_again:
  2146. if (EXPECTED(Z_TYPE_P(dim) == IS_LONG)) {
  2147. hval = Z_LVAL_P(dim);
  2148. num_index:
  2149. if (type != BP_VAR_W) {
  2150. ZEND_HASH_INDEX_FIND(ht, hval, retval, num_undef);
  2151. return retval;
  2152. num_undef:
  2153. switch (type) {
  2154. case BP_VAR_R:
  2155. zend_undefined_offset(hval);
  2156. ZEND_FALLTHROUGH;
  2157. case BP_VAR_UNSET:
  2158. case BP_VAR_IS:
  2159. retval = &EG(uninitialized_zval);
  2160. break;
  2161. case BP_VAR_RW:
  2162. retval = zend_undefined_offset_write(ht, hval);
  2163. break;
  2164. }
  2165. } else {
  2166. ZEND_HASH_INDEX_LOOKUP(ht, hval, retval);
  2167. }
  2168. } else if (EXPECTED(Z_TYPE_P(dim) == IS_STRING)) {
  2169. offset_key = Z_STR_P(dim);
  2170. if (ZEND_CONST_COND(dim_type != IS_CONST, 1)) {
  2171. if (ZEND_HANDLE_NUMERIC(offset_key, hval)) {
  2172. goto num_index;
  2173. }
  2174. }
  2175. str_index:
  2176. if (type != BP_VAR_W) {
  2177. retval = zend_hash_find_ex(ht, offset_key, ZEND_CONST_COND(dim_type == IS_CONST, 0));
  2178. if (!retval) {
  2179. switch (type) {
  2180. case BP_VAR_R:
  2181. zend_undefined_index(offset_key);
  2182. ZEND_FALLTHROUGH;
  2183. case BP_VAR_UNSET:
  2184. case BP_VAR_IS:
  2185. retval = &EG(uninitialized_zval);
  2186. break;
  2187. case BP_VAR_RW:
  2188. retval = zend_undefined_index_write(ht, offset_key);
  2189. break;
  2190. }
  2191. }
  2192. } else {
  2193. retval = zend_hash_lookup(ht, offset_key);
  2194. }
  2195. } else if (EXPECTED(Z_TYPE_P(dim) == IS_REFERENCE)) {
  2196. dim = Z_REFVAL_P(dim);
  2197. goto try_again;
  2198. } else {
  2199. zend_value val;
  2200. zend_uchar t;
  2201. if (type != BP_VAR_W && type != BP_VAR_RW) {
  2202. t = slow_index_convert(ht, dim, &val EXECUTE_DATA_CC);
  2203. } else {
  2204. t = slow_index_convert_w(ht, dim, &val EXECUTE_DATA_CC);
  2205. }
  2206. if (t == IS_STRING) {
  2207. offset_key = val.str;
  2208. goto str_index;
  2209. } else if (t == IS_LONG) {
  2210. hval = val.lval;
  2211. goto num_index;
  2212. } else {
  2213. retval = (type == BP_VAR_W || type == BP_VAR_RW) ?
  2214. NULL : &EG(uninitialized_zval);
  2215. }
  2216. }
  2217. return retval;
  2218. }
  2219. static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_W(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
  2220. {
  2221. return zend_fetch_dimension_address_inner(ht, dim, IS_TMP_VAR, BP_VAR_W EXECUTE_DATA_CC);
  2222. }
  2223. static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_W_CONST(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
  2224. {
  2225. return zend_fetch_dimension_address_inner(ht, dim, IS_CONST, BP_VAR_W EXECUTE_DATA_CC);
  2226. }
  2227. static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_RW(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
  2228. {
  2229. return zend_fetch_dimension_address_inner(ht, dim, IS_TMP_VAR, BP_VAR_RW EXECUTE_DATA_CC);
  2230. }
  2231. static zend_never_inline zval* ZEND_FASTCALL zend_fetch_dimension_address_inner_RW_CONST(HashTable *ht, const zval *dim EXECUTE_DATA_DC)
  2232. {
  2233. return zend_fetch_dimension_address_inner(ht, dim, IS_CONST, BP_VAR_RW EXECUTE_DATA_CC);
  2234. }
  2235. static zend_always_inline void zend_fetch_dimension_address(zval *result, zval *container, zval *dim, int dim_type, int type EXECUTE_DATA_DC)
  2236. {
  2237. zval *retval;
  2238. if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
  2239. try_array:
  2240. SEPARATE_ARRAY(container);
  2241. fetch_from_array:
  2242. if (dim == NULL) {
  2243. retval = zend_hash_next_index_insert(Z_ARRVAL_P(container), &EG(uninitialized_zval));
  2244. if (UNEXPECTED(retval == NULL)) {
  2245. zend_cannot_add_element();
  2246. ZVAL_UNDEF(result);
  2247. return;
  2248. }
  2249. } else {
  2250. retval = zend_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, dim_type, type EXECUTE_DATA_CC);
  2251. if (UNEXPECTED(!retval)) {
  2252. /* This may fail without throwing if the array was modified while throwing an
  2253. * undefined index error. */
  2254. ZVAL_NULL(result);
  2255. return;
  2256. }
  2257. }
  2258. ZVAL_INDIRECT(result, retval);
  2259. return;
  2260. } else if (EXPECTED(Z_TYPE_P(container) == IS_REFERENCE)) {
  2261. zend_reference *ref = Z_REF_P(container);
  2262. container = Z_REFVAL_P(container);
  2263. if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
  2264. goto try_array;
  2265. } else if (EXPECTED(Z_TYPE_P(container) <= IS_FALSE)) {
  2266. if (type != BP_VAR_UNSET) {
  2267. if (ZEND_REF_HAS_TYPE_SOURCES(ref)) {
  2268. if (UNEXPECTED(!zend_verify_ref_array_assignable(ref))) {
  2269. ZVAL_UNDEF(result);
  2270. return;
  2271. }
  2272. }
  2273. array_init(container);
  2274. goto fetch_from_array;
  2275. } else {
  2276. goto return_null;
  2277. }
  2278. }
  2279. }
  2280. if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
  2281. if (dim == NULL) {
  2282. zend_use_new_element_for_string();
  2283. } else {
  2284. zend_check_string_offset(dim, type EXECUTE_DATA_CC);
  2285. zend_wrong_string_offset_error();
  2286. }
  2287. ZVAL_UNDEF(result);
  2288. } else if (EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
  2289. zend_object *obj = Z_OBJ_P(container);
  2290. GC_ADDREF(obj);
  2291. if (ZEND_CONST_COND(dim_type == IS_CV, dim != NULL) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
  2292. dim = ZVAL_UNDEFINED_OP2();
  2293. } else if (dim_type == IS_CONST && Z_EXTRA_P(dim) == ZEND_EXTRA_VALUE) {
  2294. dim++;
  2295. }
  2296. retval = obj->handlers->read_dimension(obj, dim, type, result);
  2297. if (UNEXPECTED(retval == &EG(uninitialized_zval))) {
  2298. zend_class_entry *ce = obj->ce;
  2299. ZVAL_NULL(result);
  2300. zend_error(E_NOTICE, "Indirect modification of overloaded element of %s has no effect", ZSTR_VAL(ce->name));
  2301. } else if (EXPECTED(retval && Z_TYPE_P(retval) != IS_UNDEF)) {
  2302. if (!Z_ISREF_P(retval)) {
  2303. if (result != retval) {
  2304. ZVAL_COPY(result, retval);
  2305. retval = result;
  2306. }
  2307. if (Z_TYPE_P(retval) != IS_OBJECT) {
  2308. zend_class_entry *ce = obj->ce;
  2309. zend_error(E_NOTICE, "Indirect modification of overloaded element of %s has no effect", ZSTR_VAL(ce->name));
  2310. }
  2311. } else if (UNEXPECTED(Z_REFCOUNT_P(retval) == 1)) {
  2312. ZVAL_UNREF(retval);
  2313. }
  2314. if (result != retval) {
  2315. ZVAL_INDIRECT(result, retval);
  2316. }
  2317. } else {
  2318. ZEND_ASSERT(EG(exception) && "read_dimension() returned NULL without exception");
  2319. ZVAL_UNDEF(result);
  2320. }
  2321. if (UNEXPECTED(GC_DELREF(obj) == 0)) {
  2322. zend_objects_store_del(obj);
  2323. }
  2324. } else {
  2325. if (EXPECTED(Z_TYPE_P(container) <= IS_FALSE)) {
  2326. if (type != BP_VAR_W && UNEXPECTED(Z_TYPE_P(container) == IS_UNDEF)) {
  2327. ZVAL_UNDEFINED_OP1();
  2328. }
  2329. if (type != BP_VAR_UNSET) {
  2330. HashTable *ht = zend_new_array(0);
  2331. zend_uchar old_type = Z_TYPE_P(container);
  2332. ZVAL_ARR(container, ht);
  2333. if (UNEXPECTED(old_type == IS_FALSE)) {
  2334. GC_ADDREF(ht);
  2335. zend_false_to_array_deprecated();
  2336. if (UNEXPECTED(GC_DELREF(ht) == 0)) {
  2337. zend_array_destroy(ht);
  2338. goto return_null;
  2339. }
  2340. }
  2341. goto fetch_from_array;
  2342. } else {
  2343. if (UNEXPECTED(Z_TYPE_P(container) == IS_FALSE)) {
  2344. zend_false_to_array_deprecated();
  2345. }
  2346. return_null:
  2347. /* for read-mode only */
  2348. if (ZEND_CONST_COND(dim_type == IS_CV, dim != NULL) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
  2349. ZVAL_UNDEFINED_OP2();
  2350. }
  2351. ZVAL_NULL(result);
  2352. }
  2353. } else {
  2354. if (type == BP_VAR_UNSET) {
  2355. zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
  2356. ZVAL_UNDEF(result);
  2357. } else {
  2358. zend_use_scalar_as_array();
  2359. ZVAL_UNDEF(result);
  2360. }
  2361. }
  2362. }
  2363. }
  2364. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_W(zval *container_ptr, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  2365. {
  2366. zval *result = EX_VAR(opline->result.var);
  2367. zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_W EXECUTE_DATA_CC);
  2368. }
  2369. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_RW(zval *container_ptr, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  2370. {
  2371. zval *result = EX_VAR(opline->result.var);
  2372. zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_RW EXECUTE_DATA_CC);
  2373. }
  2374. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_UNSET(zval *container_ptr, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  2375. {
  2376. zval *result = EX_VAR(opline->result.var);
  2377. zend_fetch_dimension_address(result, container_ptr, dim, dim_type, BP_VAR_UNSET EXECUTE_DATA_CC);
  2378. }
  2379. static zend_always_inline void zend_fetch_dimension_address_read(zval *result, zval *container, zval *dim, int dim_type, int type, bool is_list, int slow EXECUTE_DATA_DC)
  2380. {
  2381. zval *retval;
  2382. if (!slow) {
  2383. if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
  2384. try_array:
  2385. retval = zend_fetch_dimension_address_inner(Z_ARRVAL_P(container), dim, dim_type, type EXECUTE_DATA_CC);
  2386. ZVAL_COPY_DEREF(result, retval);
  2387. return;
  2388. } else if (EXPECTED(Z_TYPE_P(container) == IS_REFERENCE)) {
  2389. container = Z_REFVAL_P(container);
  2390. if (EXPECTED(Z_TYPE_P(container) == IS_ARRAY)) {
  2391. goto try_array;
  2392. }
  2393. }
  2394. }
  2395. if (!is_list && EXPECTED(Z_TYPE_P(container) == IS_STRING)) {
  2396. zend_string *str = Z_STR_P(container);
  2397. zend_long offset;
  2398. try_string_offset:
  2399. if (UNEXPECTED(Z_TYPE_P(dim) != IS_LONG)) {
  2400. switch (Z_TYPE_P(dim)) {
  2401. case IS_STRING:
  2402. {
  2403. bool trailing_data = false;
  2404. /* For BC reasons we allow errors so that we can warn on leading numeric string */
  2405. if (IS_LONG == is_numeric_string_ex(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset,
  2406. NULL, /* allow errors */ true, NULL, &trailing_data)) {
  2407. if (UNEXPECTED(trailing_data)) {
  2408. zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
  2409. }
  2410. goto out;
  2411. }
  2412. if (type == BP_VAR_IS) {
  2413. ZVAL_NULL(result);
  2414. return;
  2415. }
  2416. zend_illegal_string_offset(dim);
  2417. ZVAL_NULL(result);
  2418. return;
  2419. }
  2420. case IS_UNDEF:
  2421. /* The string may be destroyed while throwing the notice.
  2422. * Temporarily increase the refcount to detect this situation. */
  2423. if (!(GC_FLAGS(str) & IS_STR_INTERNED)) {
  2424. GC_ADDREF(str);
  2425. }
  2426. ZVAL_UNDEFINED_OP2();
  2427. if (!(GC_FLAGS(str) & IS_STR_INTERNED) && UNEXPECTED(GC_DELREF(str) == 0)) {
  2428. zend_string_efree(str);
  2429. ZVAL_NULL(result);
  2430. return;
  2431. }
  2432. ZEND_FALLTHROUGH;
  2433. case IS_DOUBLE:
  2434. case IS_NULL:
  2435. case IS_FALSE:
  2436. case IS_TRUE:
  2437. if (type != BP_VAR_IS) {
  2438. /* The string may be destroyed while throwing the notice.
  2439. * Temporarily increase the refcount to detect this situation. */
  2440. if (!(GC_FLAGS(str) & IS_STR_INTERNED)) {
  2441. GC_ADDREF(str);
  2442. }
  2443. zend_error(E_WARNING, "String offset cast occurred");
  2444. if (!(GC_FLAGS(str) & IS_STR_INTERNED) && UNEXPECTED(GC_DELREF(str) == 0)) {
  2445. zend_string_efree(str);
  2446. ZVAL_NULL(result);
  2447. return;
  2448. }
  2449. }
  2450. break;
  2451. case IS_REFERENCE:
  2452. dim = Z_REFVAL_P(dim);
  2453. goto try_string_offset;
  2454. default:
  2455. zend_illegal_string_offset(dim);
  2456. ZVAL_NULL(result);
  2457. return;
  2458. }
  2459. offset = zval_get_long_func(dim, /* is_strict */ false);
  2460. } else {
  2461. offset = Z_LVAL_P(dim);
  2462. }
  2463. out:
  2464. if (UNEXPECTED(ZSTR_LEN(str) < ((offset < 0) ? -(size_t)offset : ((size_t)offset + 1)))) {
  2465. if (type != BP_VAR_IS) {
  2466. zend_error(E_WARNING, "Uninitialized string offset " ZEND_LONG_FMT, offset);
  2467. ZVAL_EMPTY_STRING(result);
  2468. } else {
  2469. ZVAL_NULL(result);
  2470. }
  2471. } else {
  2472. zend_uchar c;
  2473. zend_long real_offset;
  2474. real_offset = (UNEXPECTED(offset < 0)) /* Handle negative offset */
  2475. ? (zend_long)ZSTR_LEN(str) + offset : offset;
  2476. c = (zend_uchar)ZSTR_VAL(str)[real_offset];
  2477. ZVAL_CHAR(result, c);
  2478. }
  2479. } else if (EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
  2480. zend_object *obj = Z_OBJ_P(container);
  2481. GC_ADDREF(obj);
  2482. if (ZEND_CONST_COND(dim_type == IS_CV, 1) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
  2483. dim = ZVAL_UNDEFINED_OP2();
  2484. }
  2485. if (dim_type == IS_CONST && Z_EXTRA_P(dim) == ZEND_EXTRA_VALUE) {
  2486. dim++;
  2487. }
  2488. retval = obj->handlers->read_dimension(obj, dim, type, result);
  2489. ZEND_ASSERT(result != NULL);
  2490. if (retval) {
  2491. if (result != retval) {
  2492. ZVAL_COPY_DEREF(result, retval);
  2493. } else if (UNEXPECTED(Z_ISREF_P(retval))) {
  2494. zend_unwrap_reference(result);
  2495. }
  2496. } else {
  2497. ZVAL_NULL(result);
  2498. }
  2499. if (UNEXPECTED(GC_DELREF(obj) == 0)) {
  2500. zend_objects_store_del(obj);
  2501. }
  2502. } else {
  2503. if (type != BP_VAR_IS && UNEXPECTED(Z_TYPE_P(container) == IS_UNDEF)) {
  2504. container = ZVAL_UNDEFINED_OP1();
  2505. }
  2506. if (ZEND_CONST_COND(dim_type == IS_CV, 1) && UNEXPECTED(Z_TYPE_P(dim) == IS_UNDEF)) {
  2507. ZVAL_UNDEFINED_OP2();
  2508. }
  2509. if (!is_list && type != BP_VAR_IS) {
  2510. zend_error(E_WARNING, "Trying to access array offset on value of type %s",
  2511. zend_zval_type_name(container));
  2512. }
  2513. ZVAL_NULL(result);
  2514. }
  2515. }
  2516. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_read_R(zval *container, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  2517. {
  2518. zval *result = EX_VAR(opline->result.var);
  2519. zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_R, 0, 0 EXECUTE_DATA_CC);
  2520. }
  2521. static zend_never_inline void zend_fetch_dimension_address_read_R_slow(zval *container, zval *dim OPLINE_DC EXECUTE_DATA_DC)
  2522. {
  2523. zval *result = EX_VAR(opline->result.var);
  2524. zend_fetch_dimension_address_read(result, container, dim, IS_CV, BP_VAR_R, 0, 1 EXECUTE_DATA_CC);
  2525. }
  2526. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_read_IS(zval *container, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  2527. {
  2528. zval *result = EX_VAR(opline->result.var);
  2529. zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_IS, 0, 0 EXECUTE_DATA_CC);
  2530. }
  2531. static zend_never_inline void ZEND_FASTCALL zend_fetch_dimension_address_LIST_r(zval *container, zval *dim, int dim_type OPLINE_DC EXECUTE_DATA_DC)
  2532. {
  2533. zval *result = EX_VAR(opline->result.var);
  2534. zend_fetch_dimension_address_read(result, container, dim, dim_type, BP_VAR_R, 1, 0 EXECUTE_DATA_CC);
  2535. }
  2536. ZEND_API void zend_fetch_dimension_const(zval *result, zval *container, zval *dim, int type)
  2537. {
  2538. zend_fetch_dimension_address_read(result, container, dim, IS_TMP_VAR, type, 0, 0 NO_EXECUTE_DATA_CC);
  2539. }
  2540. static zend_never_inline zval* ZEND_FASTCALL zend_find_array_dim_slow(HashTable *ht, zval *offset EXECUTE_DATA_DC)
  2541. {
  2542. zend_ulong hval;
  2543. if (Z_TYPE_P(offset) == IS_DOUBLE) {
  2544. hval = zend_dval_to_lval_safe(Z_DVAL_P(offset));
  2545. num_idx:
  2546. return zend_hash_index_find(ht, hval);
  2547. } else if (Z_TYPE_P(offset) == IS_NULL) {
  2548. str_idx:
  2549. return zend_hash_find_known_hash(ht, ZSTR_EMPTY_ALLOC());
  2550. } else if (Z_TYPE_P(offset) == IS_FALSE) {
  2551. hval = 0;
  2552. goto num_idx;
  2553. } else if (Z_TYPE_P(offset) == IS_TRUE) {
  2554. hval = 1;
  2555. goto num_idx;
  2556. } else if (Z_TYPE_P(offset) == IS_RESOURCE) {
  2557. zend_use_resource_as_offset(offset);
  2558. hval = Z_RES_HANDLE_P(offset);
  2559. goto num_idx;
  2560. } else if (/*OP2_TYPE == IS_CV &&*/ Z_TYPE_P(offset) == IS_UNDEF) {
  2561. ZVAL_UNDEFINED_OP2();
  2562. goto str_idx;
  2563. } else {
  2564. zend_type_error("Illegal offset type in isset or empty");
  2565. return NULL;
  2566. }
  2567. }
  2568. static zend_never_inline bool ZEND_FASTCALL zend_isset_dim_slow(zval *container, zval *offset EXECUTE_DATA_DC)
  2569. {
  2570. if (/*OP2_TYPE == IS_CV &&*/ UNEXPECTED(Z_TYPE_P(offset) == IS_UNDEF)) {
  2571. offset = ZVAL_UNDEFINED_OP2();
  2572. }
  2573. if (/*OP1_TYPE != IS_CONST &&*/ EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
  2574. return Z_OBJ_HT_P(container)->has_dimension(Z_OBJ_P(container), offset, 0);
  2575. } else if (EXPECTED(Z_TYPE_P(container) == IS_STRING)) { /* string offsets */
  2576. zend_long lval;
  2577. if (EXPECTED(Z_TYPE_P(offset) == IS_LONG)) {
  2578. lval = Z_LVAL_P(offset);
  2579. str_offset:
  2580. if (UNEXPECTED(lval < 0)) { /* Handle negative offset */
  2581. lval += (zend_long)Z_STRLEN_P(container);
  2582. }
  2583. if (EXPECTED(lval >= 0) && (size_t)lval < Z_STRLEN_P(container)) {
  2584. return 1;
  2585. } else {
  2586. return 0;
  2587. }
  2588. } else {
  2589. /*if (OP2_TYPE & (IS_CV|IS_VAR)) {*/
  2590. ZVAL_DEREF(offset);
  2591. /*}*/
  2592. if (Z_TYPE_P(offset) < IS_STRING /* simple scalar types */
  2593. || (Z_TYPE_P(offset) == IS_STRING /* or numeric string */
  2594. && IS_LONG == is_numeric_string(Z_STRVAL_P(offset), Z_STRLEN_P(offset), NULL, NULL, 0))) {
  2595. lval = zval_get_long_ex(offset, /* is_strict */ true);
  2596. goto str_offset;
  2597. }
  2598. return 0;
  2599. }
  2600. } else {
  2601. return 0;
  2602. }
  2603. }
  2604. static zend_never_inline bool ZEND_FASTCALL zend_isempty_dim_slow(zval *container, zval *offset EXECUTE_DATA_DC)
  2605. {
  2606. if (/*OP2_TYPE == IS_CV &&*/ UNEXPECTED(Z_TYPE_P(offset) == IS_UNDEF)) {
  2607. offset = ZVAL_UNDEFINED_OP2();
  2608. }
  2609. if (/*OP1_TYPE != IS_CONST &&*/ EXPECTED(Z_TYPE_P(container) == IS_OBJECT)) {
  2610. return !Z_OBJ_HT_P(container)->has_dimension(Z_OBJ_P(container), offset, 1);
  2611. } else if (EXPECTED(Z_TYPE_P(container) == IS_STRING)) { /* string offsets */
  2612. zend_long lval;
  2613. if (EXPECTED(Z_TYPE_P(offset) == IS_LONG)) {
  2614. lval = Z_LVAL_P(offset);
  2615. str_offset:
  2616. if (UNEXPECTED(lval < 0)) { /* Handle negative offset */
  2617. lval += (zend_long)Z_STRLEN_P(container);
  2618. }
  2619. if (EXPECTED(lval >= 0) && (size_t)lval < Z_STRLEN_P(container)) {
  2620. return (Z_STRVAL_P(container)[lval] == '0');
  2621. } else {
  2622. return 1;
  2623. }
  2624. } else {
  2625. /*if (OP2_TYPE & (IS_CV|IS_VAR)) {*/
  2626. ZVAL_DEREF(offset);
  2627. /*}*/
  2628. if (Z_TYPE_P(offset) < IS_STRING /* simple scalar types */
  2629. || (Z_TYPE_P(offset) == IS_STRING /* or numeric string */
  2630. && IS_LONG == is_numeric_string(Z_STRVAL_P(offset), Z_STRLEN_P(offset), NULL, NULL, 0))) {
  2631. lval = zval_get_long_ex(offset, /* is_strict */ true);
  2632. goto str_offset;
  2633. }
  2634. return 1;
  2635. }
  2636. } else {
  2637. return 1;
  2638. }
  2639. }
  2640. static zend_never_inline bool ZEND_FASTCALL zend_array_key_exists_fast(HashTable *ht, zval *key OPLINE_DC EXECUTE_DATA_DC)
  2641. {
  2642. zend_string *str;
  2643. zend_ulong hval;
  2644. try_again:
  2645. if (EXPECTED(Z_TYPE_P(key) == IS_STRING)) {
  2646. str = Z_STR_P(key);
  2647. if (ZEND_HANDLE_NUMERIC(str, hval)) {
  2648. goto num_key;
  2649. }
  2650. str_key:
  2651. return zend_hash_exists(ht, str);
  2652. } else if (EXPECTED(Z_TYPE_P(key) == IS_LONG)) {
  2653. hval = Z_LVAL_P(key);
  2654. num_key:
  2655. return zend_hash_index_exists(ht, hval);
  2656. } else if (EXPECTED(Z_ISREF_P(key))) {
  2657. key = Z_REFVAL_P(key);
  2658. goto try_again;
  2659. } else if (Z_TYPE_P(key) == IS_DOUBLE) {
  2660. hval = zend_dval_to_lval_safe(Z_DVAL_P(key));
  2661. goto num_key;
  2662. } else if (Z_TYPE_P(key) == IS_FALSE) {
  2663. hval = 0;
  2664. goto num_key;
  2665. } else if (Z_TYPE_P(key) == IS_TRUE) {
  2666. hval = 1;
  2667. goto num_key;
  2668. } else if (Z_TYPE_P(key) == IS_RESOURCE) {
  2669. zend_use_resource_as_offset(key);
  2670. hval = Z_RES_HANDLE_P(key);
  2671. goto num_key;
  2672. } else if (Z_TYPE_P(key) <= IS_NULL) {
  2673. if (UNEXPECTED(Z_TYPE_P(key) == IS_UNDEF)) {
  2674. ZVAL_UNDEFINED_OP1();
  2675. }
  2676. str = ZSTR_EMPTY_ALLOC();
  2677. goto str_key;
  2678. } else {
  2679. zend_illegal_offset();
  2680. return 0;
  2681. }
  2682. }
  2683. static ZEND_COLD void ZEND_FASTCALL zend_array_key_exists_error(
  2684. zval *subject, zval *key OPLINE_DC EXECUTE_DATA_DC)
  2685. {
  2686. if (Z_TYPE_P(key) == IS_UNDEF) {
  2687. ZVAL_UNDEFINED_OP1();
  2688. }
  2689. if (Z_TYPE_P(subject) == IS_UNDEF) {
  2690. ZVAL_UNDEFINED_OP2();
  2691. }
  2692. if (!EG(exception)) {
  2693. zend_type_error("array_key_exists(): Argument #2 ($array) must be of type array, %s given",
  2694. zend_zval_type_name(subject));
  2695. }
  2696. }
  2697. static zend_always_inline bool promotes_to_array(zval *val) {
  2698. return Z_TYPE_P(val) <= IS_FALSE
  2699. || (Z_ISREF_P(val) && Z_TYPE_P(Z_REFVAL_P(val)) <= IS_FALSE);
  2700. }
  2701. static zend_always_inline bool check_type_array_assignable(zend_type type) {
  2702. if (!ZEND_TYPE_IS_SET(type)) {
  2703. return 1;
  2704. }
  2705. return (ZEND_TYPE_FULL_MASK(type) & (MAY_BE_ITERABLE|MAY_BE_ARRAY)) != 0;
  2706. }
  2707. /* Checks whether an array can be assigned to the reference. Throws error if not assignable. */
  2708. ZEND_API bool zend_verify_ref_array_assignable(zend_reference *ref) {
  2709. zend_property_info *prop;
  2710. ZEND_ASSERT(ZEND_REF_HAS_TYPE_SOURCES(ref));
  2711. ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) {
  2712. if (!check_type_array_assignable(prop->type)) {
  2713. zend_throw_auto_init_in_ref_error(prop, "array");
  2714. return 0;
  2715. }
  2716. } ZEND_REF_FOREACH_TYPE_SOURCES_END();
  2717. return 1;
  2718. }
  2719. static zend_property_info *zend_object_fetch_property_type_info(
  2720. zend_object *obj, zval *slot)
  2721. {
  2722. if (EXPECTED(!ZEND_CLASS_HAS_TYPE_HINTS(obj->ce))) {
  2723. return NULL;
  2724. }
  2725. /* Not a declared property */
  2726. if (UNEXPECTED(slot < obj->properties_table ||
  2727. slot >= obj->properties_table + obj->ce->default_properties_count)) {
  2728. return NULL;
  2729. }
  2730. return zend_get_typed_property_info_for_slot(obj, slot);
  2731. }
  2732. static zend_never_inline bool zend_handle_fetch_obj_flags(
  2733. zval *result, zval *ptr, zend_object *obj, zend_property_info *prop_info, uint32_t flags)
  2734. {
  2735. switch (flags) {
  2736. case ZEND_FETCH_DIM_WRITE:
  2737. if (promotes_to_array(ptr)) {
  2738. if (!prop_info) {
  2739. prop_info = zend_object_fetch_property_type_info(obj, ptr);
  2740. if (!prop_info) {
  2741. break;
  2742. }
  2743. }
  2744. if (!check_type_array_assignable(prop_info->type)) {
  2745. zend_throw_auto_init_in_prop_error(prop_info, "array");
  2746. if (result) ZVAL_ERROR(result);
  2747. return 0;
  2748. }
  2749. }
  2750. break;
  2751. case ZEND_FETCH_REF:
  2752. if (Z_TYPE_P(ptr) != IS_REFERENCE) {
  2753. if (!prop_info) {
  2754. prop_info = zend_object_fetch_property_type_info(obj, ptr);
  2755. if (!prop_info) {
  2756. break;
  2757. }
  2758. }
  2759. if (Z_TYPE_P(ptr) == IS_UNDEF) {
  2760. if (!ZEND_TYPE_ALLOW_NULL(prop_info->type)) {
  2761. zend_throw_access_uninit_prop_by_ref_error(prop_info);
  2762. if (result) ZVAL_ERROR(result);
  2763. return 0;
  2764. }
  2765. ZVAL_NULL(ptr);
  2766. }
  2767. ZVAL_NEW_REF(ptr, ptr);
  2768. ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(ptr), prop_info);
  2769. }
  2770. break;
  2771. EMPTY_SWITCH_DEFAULT_CASE()
  2772. }
  2773. return 1;
  2774. }
  2775. static zend_always_inline void zend_fetch_property_address(zval *result, zval *container, uint32_t container_op_type, zval *prop_ptr, uint32_t prop_op_type, void **cache_slot, int type, uint32_t flags, bool init_undef OPLINE_DC EXECUTE_DATA_DC)
  2776. {
  2777. zval *ptr;
  2778. zend_object *zobj;
  2779. zend_string *name, *tmp_name;
  2780. if (container_op_type != IS_UNUSED && UNEXPECTED(Z_TYPE_P(container) != IS_OBJECT)) {
  2781. do {
  2782. if (Z_ISREF_P(container) && Z_TYPE_P(Z_REFVAL_P(container)) == IS_OBJECT) {
  2783. container = Z_REFVAL_P(container);
  2784. break;
  2785. }
  2786. if (container_op_type == IS_CV
  2787. && type != BP_VAR_W
  2788. && UNEXPECTED(Z_TYPE_P(container) == IS_UNDEF)) {
  2789. ZVAL_UNDEFINED_OP1();
  2790. }
  2791. /* this should modify object only if it's empty */
  2792. if (type == BP_VAR_UNSET) {
  2793. ZVAL_NULL(result);
  2794. return;
  2795. }
  2796. zend_throw_non_object_error(container, prop_ptr OPLINE_CC EXECUTE_DATA_CC);
  2797. ZVAL_ERROR(result);
  2798. return;
  2799. } while (0);
  2800. }
  2801. zobj = Z_OBJ_P(container);
  2802. if (prop_op_type == IS_CONST &&
  2803. EXPECTED(zobj->ce == CACHED_PTR_EX(cache_slot))) {
  2804. uintptr_t prop_offset = (uintptr_t)CACHED_PTR_EX(cache_slot + 1);
  2805. if (EXPECTED(IS_VALID_PROPERTY_OFFSET(prop_offset))) {
  2806. ptr = OBJ_PROP(zobj, prop_offset);
  2807. if (EXPECTED(Z_TYPE_P(ptr) != IS_UNDEF)) {
  2808. ZVAL_INDIRECT(result, ptr);
  2809. zend_property_info *prop_info = CACHED_PTR_EX(cache_slot + 2);
  2810. if (prop_info) {
  2811. if (UNEXPECTED(prop_info->flags & ZEND_ACC_READONLY)) {
  2812. /* For objects, W/RW/UNSET fetch modes might not actually modify object.
  2813. * Similar as with magic __get() allow them, but return the value as a copy
  2814. * to make sure no actual modification is possible. */
  2815. ZEND_ASSERT(type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET);
  2816. if (Z_TYPE_P(ptr) == IS_OBJECT) {
  2817. ZVAL_COPY(result, ptr);
  2818. } else {
  2819. zend_readonly_property_modification_error(prop_info);
  2820. ZVAL_ERROR(result);
  2821. }
  2822. return;
  2823. }
  2824. if (flags) {
  2825. zend_handle_fetch_obj_flags(result, ptr, NULL, prop_info, flags);
  2826. }
  2827. }
  2828. return;
  2829. }
  2830. } else if (EXPECTED(zobj->properties != NULL)) {
  2831. if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
  2832. if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
  2833. GC_DELREF(zobj->properties);
  2834. }
  2835. zobj->properties = zend_array_dup(zobj->properties);
  2836. }
  2837. ptr = zend_hash_find_known_hash(zobj->properties, Z_STR_P(prop_ptr));
  2838. if (EXPECTED(ptr)) {
  2839. ZVAL_INDIRECT(result, ptr);
  2840. return;
  2841. }
  2842. }
  2843. }
  2844. if (prop_op_type == IS_CONST) {
  2845. name = Z_STR_P(prop_ptr);
  2846. } else {
  2847. name = zval_get_tmp_string(prop_ptr, &tmp_name);
  2848. }
  2849. ptr = zobj->handlers->get_property_ptr_ptr(zobj, name, type, cache_slot);
  2850. if (NULL == ptr) {
  2851. ptr = zobj->handlers->read_property(zobj, name, type, cache_slot, result);
  2852. if (ptr == result) {
  2853. if (UNEXPECTED(Z_ISREF_P(ptr) && Z_REFCOUNT_P(ptr) == 1)) {
  2854. ZVAL_UNREF(ptr);
  2855. }
  2856. goto end;
  2857. }
  2858. if (UNEXPECTED(EG(exception))) {
  2859. ZVAL_ERROR(result);
  2860. goto end;
  2861. }
  2862. } else if (UNEXPECTED(Z_ISERROR_P(ptr))) {
  2863. ZVAL_ERROR(result);
  2864. goto end;
  2865. }
  2866. ZVAL_INDIRECT(result, ptr);
  2867. if (flags) {
  2868. zend_property_info *prop_info;
  2869. if (prop_op_type == IS_CONST) {
  2870. prop_info = CACHED_PTR_EX(cache_slot + 2);
  2871. if (prop_info) {
  2872. if (UNEXPECTED(!zend_handle_fetch_obj_flags(result, ptr, NULL, prop_info, flags))) {
  2873. goto end;
  2874. }
  2875. }
  2876. } else {
  2877. if (UNEXPECTED(!zend_handle_fetch_obj_flags(result, ptr, Z_OBJ_P(container), NULL, flags))) {
  2878. goto end;
  2879. }
  2880. }
  2881. }
  2882. if (init_undef && UNEXPECTED(Z_TYPE_P(ptr) == IS_UNDEF)) {
  2883. ZVAL_NULL(ptr);
  2884. }
  2885. end:
  2886. if (prop_op_type != IS_CONST) {
  2887. zend_tmp_string_release(tmp_name);
  2888. }
  2889. }
  2890. static zend_always_inline void zend_assign_to_property_reference(zval *container, uint32_t container_op_type, zval *prop_ptr, uint32_t prop_op_type, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  2891. {
  2892. zval variable, *variable_ptr = &variable;
  2893. void **cache_addr = (prop_op_type == IS_CONST) ? CACHE_ADDR(opline->extended_value & ~ZEND_RETURNS_FUNCTION) : NULL;
  2894. zend_fetch_property_address(variable_ptr, container, container_op_type, prop_ptr, prop_op_type,
  2895. cache_addr, BP_VAR_W, 0, 0 OPLINE_CC EXECUTE_DATA_CC);
  2896. if (EXPECTED(Z_TYPE_P(variable_ptr) == IS_INDIRECT)) {
  2897. variable_ptr = Z_INDIRECT_P(variable_ptr);
  2898. if (/*OP_DATA_TYPE == IS_VAR &&*/
  2899. (opline->extended_value & ZEND_RETURNS_FUNCTION) &&
  2900. UNEXPECTED(!Z_ISREF_P(value_ptr))) {
  2901. variable_ptr = zend_wrong_assign_to_variable_reference(
  2902. variable_ptr, value_ptr OPLINE_CC EXECUTE_DATA_CC);
  2903. } else {
  2904. zend_property_info *prop_info = NULL;
  2905. if (prop_op_type == IS_CONST) {
  2906. prop_info = (zend_property_info *) CACHED_PTR_EX(cache_addr + 2);
  2907. } else {
  2908. ZVAL_DEREF(container);
  2909. prop_info = zend_object_fetch_property_type_info(Z_OBJ_P(container), variable_ptr);
  2910. }
  2911. if (UNEXPECTED(prop_info)) {
  2912. variable_ptr = zend_assign_to_typed_property_reference(prop_info, variable_ptr, value_ptr EXECUTE_DATA_CC);
  2913. } else {
  2914. zend_assign_to_variable_reference(variable_ptr, value_ptr);
  2915. }
  2916. }
  2917. } else if (Z_ISERROR_P(variable_ptr)) {
  2918. variable_ptr = &EG(uninitialized_zval);
  2919. } else {
  2920. zend_throw_error(NULL, "Cannot assign by reference to overloaded object");
  2921. zval_ptr_dtor(&variable);
  2922. variable_ptr = &EG(uninitialized_zval);
  2923. }
  2924. if (UNEXPECTED(RETURN_VALUE_USED(opline))) {
  2925. ZVAL_COPY(EX_VAR(opline->result.var), variable_ptr);
  2926. }
  2927. }
  2928. static zend_never_inline void zend_assign_to_property_reference_this_const(zval *container, zval *prop_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  2929. {
  2930. zend_assign_to_property_reference(container, IS_UNUSED, prop_ptr, IS_CONST, value_ptr
  2931. OPLINE_CC EXECUTE_DATA_CC);
  2932. }
  2933. static zend_never_inline void zend_assign_to_property_reference_var_const(zval *container, zval *prop_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  2934. {
  2935. zend_assign_to_property_reference(container, IS_VAR, prop_ptr, IS_CONST, value_ptr
  2936. OPLINE_CC EXECUTE_DATA_CC);
  2937. }
  2938. static zend_never_inline void zend_assign_to_property_reference_this_var(zval *container, zval *prop_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  2939. {
  2940. zend_assign_to_property_reference(container, IS_UNUSED, prop_ptr, IS_VAR, value_ptr
  2941. OPLINE_CC EXECUTE_DATA_CC);
  2942. }
  2943. static zend_never_inline void zend_assign_to_property_reference_var_var(zval *container, zval *prop_ptr, zval *value_ptr OPLINE_DC EXECUTE_DATA_DC)
  2944. {
  2945. zend_assign_to_property_reference(container, IS_VAR, prop_ptr, IS_VAR, value_ptr
  2946. OPLINE_CC EXECUTE_DATA_CC);
  2947. }
  2948. static zend_never_inline zend_result zend_fetch_static_property_address_ex(zval **retval, zend_property_info **prop_info, uint32_t cache_slot, int fetch_type OPLINE_DC EXECUTE_DATA_DC) {
  2949. zend_string *name;
  2950. zend_class_entry *ce;
  2951. zend_property_info *property_info;
  2952. zend_uchar op1_type = opline->op1_type, op2_type = opline->op2_type;
  2953. if (EXPECTED(op2_type == IS_CONST)) {
  2954. zval *class_name = RT_CONSTANT(opline, opline->op2);
  2955. ZEND_ASSERT(op1_type != IS_CONST || CACHED_PTR(cache_slot) == NULL);
  2956. if (EXPECTED((ce = CACHED_PTR(cache_slot)) == NULL)) {
  2957. ce = zend_fetch_class_by_name(Z_STR_P(class_name), Z_STR_P(class_name + 1), ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);
  2958. if (UNEXPECTED(ce == NULL)) {
  2959. FREE_OP(op1_type, opline->op1.var);
  2960. return FAILURE;
  2961. }
  2962. if (UNEXPECTED(op1_type != IS_CONST)) {
  2963. CACHE_PTR(cache_slot, ce);
  2964. }
  2965. }
  2966. } else {
  2967. if (EXPECTED(op2_type == IS_UNUSED)) {
  2968. ce = zend_fetch_class(NULL, opline->op2.num);
  2969. if (UNEXPECTED(ce == NULL)) {
  2970. FREE_OP(op1_type, opline->op1.var);
  2971. return FAILURE;
  2972. }
  2973. } else {
  2974. ce = Z_CE_P(EX_VAR(opline->op2.var));
  2975. }
  2976. if (EXPECTED(op1_type == IS_CONST) && EXPECTED(CACHED_PTR(cache_slot) == ce)) {
  2977. *retval = CACHED_PTR(cache_slot + sizeof(void *));
  2978. *prop_info = CACHED_PTR(cache_slot + sizeof(void *) * 2);
  2979. return SUCCESS;
  2980. }
  2981. }
  2982. if (EXPECTED(op1_type == IS_CONST)) {
  2983. name = Z_STR_P(RT_CONSTANT(opline, opline->op1));
  2984. *retval = zend_std_get_static_property_with_info(ce, name, fetch_type, &property_info);
  2985. } else {
  2986. zend_string *tmp_name;
  2987. zval *varname = get_zval_ptr_undef(opline->op1_type, opline->op1, BP_VAR_R);
  2988. if (EXPECTED(Z_TYPE_P(varname) == IS_STRING)) {
  2989. name = Z_STR_P(varname);
  2990. tmp_name = NULL;
  2991. } else {
  2992. if (op1_type == IS_CV && UNEXPECTED(Z_TYPE_P(varname) == IS_UNDEF)) {
  2993. zval_undefined_cv(opline->op1.var EXECUTE_DATA_CC);
  2994. }
  2995. name = zval_get_tmp_string(varname, &tmp_name);
  2996. }
  2997. *retval = zend_std_get_static_property_with_info(ce, name, fetch_type, &property_info);
  2998. if (UNEXPECTED(op1_type != IS_CONST)) {
  2999. zend_tmp_string_release(tmp_name);
  3000. FREE_OP(op1_type, opline->op1.var);
  3001. }
  3002. }
  3003. if (UNEXPECTED(*retval == NULL)) {
  3004. return FAILURE;
  3005. }
  3006. *prop_info = property_info;
  3007. if (EXPECTED(op1_type == IS_CONST)
  3008. && EXPECTED(!(property_info->ce->ce_flags & ZEND_ACC_TRAIT))) {
  3009. CACHE_POLYMORPHIC_PTR(cache_slot, ce, *retval);
  3010. CACHE_PTR(cache_slot + sizeof(void *) * 2, property_info);
  3011. }
  3012. return SUCCESS;
  3013. }
  3014. static zend_always_inline zend_result zend_fetch_static_property_address(zval **retval, zend_property_info **prop_info, uint32_t cache_slot, int fetch_type, int flags OPLINE_DC EXECUTE_DATA_DC) {
  3015. zend_property_info *property_info;
  3016. if (opline->op1_type == IS_CONST && (opline->op2_type == IS_CONST || (opline->op2_type == IS_UNUSED && (opline->op2.num == ZEND_FETCH_CLASS_SELF || opline->op2.num == ZEND_FETCH_CLASS_PARENT))) && EXPECTED(CACHED_PTR(cache_slot) != NULL)) {
  3017. *retval = CACHED_PTR(cache_slot + sizeof(void *));
  3018. property_info = CACHED_PTR(cache_slot + sizeof(void *) * 2);
  3019. if ((fetch_type == BP_VAR_R || fetch_type == BP_VAR_RW)
  3020. && UNEXPECTED(Z_TYPE_P(*retval) == IS_UNDEF)
  3021. && UNEXPECTED(ZEND_TYPE_IS_SET(property_info->type))) {
  3022. zend_throw_error(NULL, "Typed static property %s::$%s must not be accessed before initialization",
  3023. ZSTR_VAL(property_info->ce->name),
  3024. zend_get_unmangled_property_name(property_info->name));
  3025. return FAILURE;
  3026. }
  3027. } else {
  3028. zend_result success;
  3029. success = zend_fetch_static_property_address_ex(retval, &property_info, cache_slot, fetch_type OPLINE_CC EXECUTE_DATA_CC);
  3030. if (UNEXPECTED(success != SUCCESS)) {
  3031. return FAILURE;
  3032. }
  3033. }
  3034. if (flags && ZEND_TYPE_IS_SET(property_info->type)) {
  3035. zend_handle_fetch_obj_flags(NULL, *retval, NULL, property_info, flags);
  3036. }
  3037. if (prop_info) {
  3038. *prop_info = property_info;
  3039. }
  3040. return SUCCESS;
  3041. }
  3042. ZEND_API ZEND_COLD void zend_throw_ref_type_error_type(zend_property_info *prop1, zend_property_info *prop2, zval *zv) {
  3043. zend_string *type1_str = zend_type_to_string(prop1->type);
  3044. zend_string *type2_str = zend_type_to_string(prop2->type);
  3045. zend_type_error("Reference with value of type %s held by property %s::$%s of type %s is not compatible with property %s::$%s of type %s",
  3046. zend_zval_type_name(zv),
  3047. ZSTR_VAL(prop1->ce->name),
  3048. zend_get_unmangled_property_name(prop1->name),
  3049. ZSTR_VAL(type1_str),
  3050. ZSTR_VAL(prop2->ce->name),
  3051. zend_get_unmangled_property_name(prop2->name),
  3052. ZSTR_VAL(type2_str)
  3053. );
  3054. zend_string_release(type1_str);
  3055. zend_string_release(type2_str);
  3056. }
  3057. ZEND_API ZEND_COLD void zend_throw_ref_type_error_zval(zend_property_info *prop, zval *zv) {
  3058. zend_string *type_str = zend_type_to_string(prop->type);
  3059. zend_type_error("Cannot assign %s to reference held by property %s::$%s of type %s",
  3060. zend_zval_type_name(zv),
  3061. ZSTR_VAL(prop->ce->name),
  3062. zend_get_unmangled_property_name(prop->name),
  3063. ZSTR_VAL(type_str)
  3064. );
  3065. zend_string_release(type_str);
  3066. }
  3067. ZEND_API ZEND_COLD void zend_throw_conflicting_coercion_error(zend_property_info *prop1, zend_property_info *prop2, zval *zv) {
  3068. zend_string *type1_str = zend_type_to_string(prop1->type);
  3069. zend_string *type2_str = zend_type_to_string(prop2->type);
  3070. zend_type_error("Cannot assign %s to reference held by property %s::$%s of type %s and property %s::$%s of type %s, as this would result in an inconsistent type conversion",
  3071. zend_zval_type_name(zv),
  3072. ZSTR_VAL(prop1->ce->name),
  3073. zend_get_unmangled_property_name(prop1->name),
  3074. ZSTR_VAL(type1_str),
  3075. ZSTR_VAL(prop2->ce->name),
  3076. zend_get_unmangled_property_name(prop2->name),
  3077. ZSTR_VAL(type2_str)
  3078. );
  3079. zend_string_release(type1_str);
  3080. zend_string_release(type2_str);
  3081. }
  3082. /* 1: valid, 0: invalid, -1: may be valid after type coercion */
  3083. static zend_always_inline int i_zend_verify_type_assignable_zval(
  3084. zend_property_info *info, zval *zv, bool strict) {
  3085. zend_type type = info->type;
  3086. uint32_t type_mask;
  3087. zend_uchar zv_type = Z_TYPE_P(zv);
  3088. if (EXPECTED(ZEND_TYPE_CONTAINS_CODE(type, zv_type))) {
  3089. return 1;
  3090. }
  3091. if (ZEND_TYPE_IS_COMPLEX(type) && zv_type == IS_OBJECT
  3092. && zend_check_and_resolve_property_class_type(info, Z_OBJCE_P(zv))) {
  3093. return 1;
  3094. }
  3095. type_mask = ZEND_TYPE_FULL_MASK(type);
  3096. ZEND_ASSERT(!(type_mask & (MAY_BE_CALLABLE|MAY_BE_STATIC)));
  3097. if (type_mask & MAY_BE_ITERABLE) {
  3098. return zend_is_iterable(zv);
  3099. }
  3100. /* SSTH Exception: IS_LONG may be accepted as IS_DOUBLE (converted) */
  3101. if (strict) {
  3102. if ((type_mask & MAY_BE_DOUBLE) && zv_type == IS_LONG) {
  3103. return -1;
  3104. }
  3105. return 0;
  3106. }
  3107. /* NULL may be accepted only by nullable hints (this is already checked) */
  3108. if (zv_type == IS_NULL) {
  3109. return 0;
  3110. }
  3111. /* Does not contain any type to which a coercion is possible */
  3112. if (!(type_mask & (MAY_BE_LONG|MAY_BE_DOUBLE|MAY_BE_STRING))
  3113. && (type_mask & MAY_BE_BOOL) != MAY_BE_BOOL) {
  3114. return 0;
  3115. }
  3116. /* Coercion may be necessary, check separately */
  3117. return -1;
  3118. }
  3119. ZEND_API bool ZEND_FASTCALL zend_verify_ref_assignable_zval(zend_reference *ref, zval *zv, bool strict)
  3120. {
  3121. zend_property_info *prop;
  3122. /* The value must satisfy each property type, and coerce to the same value for each property
  3123. * type. Remember the first coerced type and value we've seen for this purpose. */
  3124. zend_property_info *first_prop = NULL;
  3125. zval coerced_value;
  3126. ZVAL_UNDEF(&coerced_value);
  3127. ZEND_ASSERT(Z_TYPE_P(zv) != IS_REFERENCE);
  3128. ZEND_REF_FOREACH_TYPE_SOURCES(ref, prop) {
  3129. int result = i_zend_verify_type_assignable_zval(prop, zv, strict);
  3130. if (result == 0) {
  3131. type_error:
  3132. zend_throw_ref_type_error_zval(prop, zv);
  3133. zval_ptr_dtor(&coerced_value);
  3134. return 0;
  3135. }
  3136. if (result < 0) {
  3137. if (!first_prop) {
  3138. first_prop = prop;
  3139. ZVAL_COPY(&coerced_value, zv);
  3140. if (!zend_verify_weak_scalar_type_hint(
  3141. ZEND_TYPE_FULL_MASK(prop->type), &coerced_value)) {
  3142. goto type_error;
  3143. }
  3144. } else if (Z_ISUNDEF(coerced_value)) {
  3145. /* A previous property did not require coercion, but this one does,
  3146. * so they are incompatible. */
  3147. goto conflicting_coercion_error;
  3148. } else {
  3149. zval tmp;
  3150. ZVAL_COPY(&tmp, zv);
  3151. if (!zend_verify_weak_scalar_type_hint(ZEND_TYPE_FULL_MASK(prop->type), &tmp)) {
  3152. zval_ptr_dtor(&tmp);
  3153. goto type_error;
  3154. }
  3155. if (!zend_is_identical(&coerced_value, &tmp)) {
  3156. zval_ptr_dtor(&tmp);
  3157. goto conflicting_coercion_error;
  3158. }
  3159. zval_ptr_dtor(&tmp);
  3160. }
  3161. } else {
  3162. if (!first_prop) {
  3163. first_prop = prop;
  3164. } else if (!Z_ISUNDEF(coerced_value)) {
  3165. /* A previous property required coercion, but this one doesn't,
  3166. * so they are incompatible. */
  3167. conflicting_coercion_error:
  3168. zend_throw_conflicting_coercion_error(first_prop, prop, zv);
  3169. zval_ptr_dtor(&coerced_value);
  3170. return 0;
  3171. }
  3172. }
  3173. } ZEND_REF_FOREACH_TYPE_SOURCES_END();
  3174. if (!Z_ISUNDEF(coerced_value)) {
  3175. zval_ptr_dtor(zv);
  3176. ZVAL_COPY_VALUE(zv, &coerced_value);
  3177. }
  3178. return 1;
  3179. }
  3180. static zend_always_inline void i_zval_ptr_dtor_noref(zval *zval_ptr) {
  3181. if (Z_REFCOUNTED_P(zval_ptr)) {
  3182. zend_refcounted *ref = Z_COUNTED_P(zval_ptr);
  3183. ZEND_ASSERT(Z_TYPE_P(zval_ptr) != IS_REFERENCE);
  3184. if (!GC_DELREF(ref)) {
  3185. rc_dtor_func(ref);
  3186. } else if (UNEXPECTED(GC_MAY_LEAK(ref))) {
  3187. gc_possible_root(ref);
  3188. }
  3189. }
  3190. }
  3191. ZEND_API zval* zend_assign_to_typed_ref(zval *variable_ptr, zval *orig_value, zend_uchar value_type, bool strict)
  3192. {
  3193. bool ret;
  3194. zval value;
  3195. zend_refcounted *ref = NULL;
  3196. if (Z_ISREF_P(orig_value)) {
  3197. ref = Z_COUNTED_P(orig_value);
  3198. orig_value = Z_REFVAL_P(orig_value);
  3199. }
  3200. ZVAL_COPY(&value, orig_value);
  3201. ret = zend_verify_ref_assignable_zval(Z_REF_P(variable_ptr), &value, strict);
  3202. variable_ptr = Z_REFVAL_P(variable_ptr);
  3203. if (EXPECTED(ret)) {
  3204. i_zval_ptr_dtor_noref(variable_ptr);
  3205. ZVAL_COPY_VALUE(variable_ptr, &value);
  3206. } else {
  3207. zval_ptr_dtor_nogc(&value);
  3208. }
  3209. if (value_type & (IS_VAR|IS_TMP_VAR)) {
  3210. if (UNEXPECTED(ref)) {
  3211. if (UNEXPECTED(GC_DELREF(ref) == 0)) {
  3212. zval_ptr_dtor(orig_value);
  3213. efree_size(ref, sizeof(zend_reference));
  3214. }
  3215. } else {
  3216. i_zval_ptr_dtor_noref(orig_value);
  3217. }
  3218. }
  3219. return variable_ptr;
  3220. }
  3221. ZEND_API bool ZEND_FASTCALL zend_verify_prop_assignable_by_ref(zend_property_info *prop_info, zval *orig_val, bool strict) {
  3222. zval *val = orig_val;
  3223. if (Z_ISREF_P(val) && ZEND_REF_HAS_TYPE_SOURCES(Z_REF_P(val))) {
  3224. int result;
  3225. val = Z_REFVAL_P(val);
  3226. result = i_zend_verify_type_assignable_zval(prop_info, val, strict);
  3227. if (result > 0) {
  3228. return 1;
  3229. }
  3230. if (result < 0) {
  3231. /* This is definitely an error, but we still need to determined why: Either because
  3232. * the value is simply illegal for the type, or because or a conflicting coercion. */
  3233. zval tmp;
  3234. ZVAL_COPY(&tmp, val);
  3235. if (zend_verify_weak_scalar_type_hint(ZEND_TYPE_FULL_MASK(prop_info->type), &tmp)) {
  3236. zend_property_info *ref_prop = ZEND_REF_FIRST_SOURCE(Z_REF_P(orig_val));
  3237. zend_throw_ref_type_error_type(ref_prop, prop_info, val);
  3238. zval_ptr_dtor(&tmp);
  3239. return 0;
  3240. }
  3241. zval_ptr_dtor(&tmp);
  3242. }
  3243. } else {
  3244. ZVAL_DEREF(val);
  3245. if (i_zend_check_property_type(prop_info, val, strict)) {
  3246. return 1;
  3247. }
  3248. }
  3249. zend_verify_property_type_error(prop_info, val);
  3250. return 0;
  3251. }
  3252. ZEND_API void ZEND_FASTCALL zend_ref_add_type_source(zend_property_info_source_list *source_list, zend_property_info *prop)
  3253. {
  3254. zend_property_info_list *list;
  3255. if (source_list->ptr == NULL) {
  3256. source_list->ptr = prop;
  3257. return;
  3258. }
  3259. list = ZEND_PROPERTY_INFO_SOURCE_TO_LIST(source_list->list);
  3260. if (!ZEND_PROPERTY_INFO_SOURCE_IS_LIST(source_list->list)) {
  3261. list = emalloc(sizeof(zend_property_info_list) + (4 - 1) * sizeof(zend_property_info *));
  3262. list->ptr[0] = source_list->ptr;
  3263. list->num_allocated = 4;
  3264. list->num = 1;
  3265. } else if (list->num_allocated == list->num) {
  3266. list->num_allocated = list->num * 2;
  3267. list = erealloc(list, sizeof(zend_property_info_list) + (list->num_allocated - 1) * sizeof(zend_property_info *));
  3268. }
  3269. list->ptr[list->num++] = prop;
  3270. source_list->list = ZEND_PROPERTY_INFO_SOURCE_FROM_LIST(list);
  3271. }
  3272. ZEND_API void ZEND_FASTCALL zend_ref_del_type_source(zend_property_info_source_list *source_list, zend_property_info *prop)
  3273. {
  3274. zend_property_info_list *list = ZEND_PROPERTY_INFO_SOURCE_TO_LIST(source_list->list);
  3275. zend_property_info **ptr, **end;
  3276. ZEND_ASSERT(prop);
  3277. if (!ZEND_PROPERTY_INFO_SOURCE_IS_LIST(source_list->list)) {
  3278. ZEND_ASSERT(source_list->ptr == prop);
  3279. source_list->ptr = NULL;
  3280. return;
  3281. }
  3282. if (list->num == 1) {
  3283. ZEND_ASSERT(*list->ptr == prop);
  3284. efree(list);
  3285. source_list->ptr = NULL;
  3286. return;
  3287. }
  3288. /* Checking against end here to get a more graceful failure mode if we missed adding a type
  3289. * source at some point. */
  3290. ptr = list->ptr;
  3291. end = ptr + list->num;
  3292. while (ptr < end && *ptr != prop) {
  3293. ptr++;
  3294. }
  3295. ZEND_ASSERT(*ptr == prop);
  3296. /* Copy the last list element into the deleted slot. */
  3297. *ptr = list->ptr[--list->num];
  3298. if (list->num >= 4 && list->num * 4 == list->num_allocated) {
  3299. list->num_allocated = list->num * 2;
  3300. source_list->list = ZEND_PROPERTY_INFO_SOURCE_FROM_LIST(erealloc(list, sizeof(zend_property_info_list) + (list->num_allocated - 1) * sizeof(zend_property_info *)));
  3301. }
  3302. }
  3303. static zend_never_inline void zend_fetch_this_var(int type OPLINE_DC EXECUTE_DATA_DC)
  3304. {
  3305. zval *result = EX_VAR(opline->result.var);
  3306. switch (type) {
  3307. case BP_VAR_R:
  3308. if (EXPECTED(Z_TYPE(EX(This)) == IS_OBJECT)) {
  3309. ZVAL_OBJ(result, Z_OBJ(EX(This)));
  3310. Z_ADDREF_P(result);
  3311. } else {
  3312. ZVAL_NULL(result);
  3313. zend_error(E_WARNING, "Undefined variable $this");
  3314. }
  3315. break;
  3316. case BP_VAR_IS:
  3317. if (EXPECTED(Z_TYPE(EX(This)) == IS_OBJECT)) {
  3318. ZVAL_OBJ(result, Z_OBJ(EX(This)));
  3319. Z_ADDREF_P(result);
  3320. } else {
  3321. ZVAL_NULL(result);
  3322. }
  3323. break;
  3324. case BP_VAR_RW:
  3325. case BP_VAR_W:
  3326. ZVAL_UNDEF(result);
  3327. zend_throw_error(NULL, "Cannot re-assign $this");
  3328. break;
  3329. case BP_VAR_UNSET:
  3330. ZVAL_UNDEF(result);
  3331. zend_throw_error(NULL, "Cannot unset $this");
  3332. break;
  3333. EMPTY_SWITCH_DEFAULT_CASE()
  3334. }
  3335. }
  3336. static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_wrong_clone_call(zend_function *clone, zend_class_entry *scope)
  3337. {
  3338. zend_throw_error(NULL, "Call to %s %s::__clone() from %s%s",
  3339. zend_visibility_string(clone->common.fn_flags), ZSTR_VAL(clone->common.scope->name),
  3340. scope ? "scope " : "global scope",
  3341. scope ? ZSTR_VAL(scope->name) : ""
  3342. );
  3343. }
  3344. #if ZEND_INTENSIVE_DEBUGGING
  3345. #define CHECK_SYMBOL_TABLES() \
  3346. zend_hash_apply(&EG(symbol_table), zend_check_symbol); \
  3347. if (&EG(symbol_table)!=EX(symbol_table)) { \
  3348. zend_hash_apply(EX(symbol_table), zend_check_symbol); \
  3349. }
  3350. static void zend_check_symbol(zval *pz)
  3351. {
  3352. if (Z_TYPE_P(pz) == IS_INDIRECT) {
  3353. pz = Z_INDIRECT_P(pz);
  3354. }
  3355. if (Z_TYPE_P(pz) > 10) {
  3356. fprintf(stderr, "Warning! %x has invalid type!\n", *pz);
  3357. /* See http://support.microsoft.com/kb/190351 */
  3358. #ifdef ZEND_WIN32
  3359. fflush(stderr);
  3360. #endif
  3361. } else if (Z_TYPE_P(pz) == IS_ARRAY) {
  3362. zend_hash_apply(Z_ARRVAL_P(pz), zend_check_symbol);
  3363. } else if (Z_TYPE_P(pz) == IS_OBJECT) {
  3364. /* OBJ-TBI - doesn't support new object model! */
  3365. zend_hash_apply(Z_OBJPROP_P(pz), zend_check_symbol);
  3366. }
  3367. }
  3368. #else
  3369. #define CHECK_SYMBOL_TABLES()
  3370. #endif
  3371. ZEND_API void execute_internal(zend_execute_data *execute_data, zval *return_value)
  3372. {
  3373. execute_data->func->internal_function.handler(execute_data, return_value);
  3374. }
  3375. ZEND_API void zend_clean_and_cache_symbol_table(zend_array *symbol_table) /* {{{ */
  3376. {
  3377. /* Clean before putting into the cache, since clean could call dtors,
  3378. * which could use the cached hash. Also do this before the check for
  3379. * available cache slots, as those may be used by a dtor as well. */
  3380. zend_symtable_clean(symbol_table);
  3381. if (EG(symtable_cache_ptr) >= EG(symtable_cache_limit)) {
  3382. zend_array_destroy(symbol_table);
  3383. } else {
  3384. *(EG(symtable_cache_ptr)++) = symbol_table;
  3385. }
  3386. }
  3387. /* }}} */
  3388. static zend_always_inline void i_free_compiled_variables(zend_execute_data *execute_data) /* {{{ */
  3389. {
  3390. zval *cv = EX_VAR_NUM(0);
  3391. int count = EX(func)->op_array.last_var;
  3392. while (EXPECTED(count != 0)) {
  3393. i_zval_ptr_dtor(cv);
  3394. cv++;
  3395. count--;
  3396. }
  3397. }
  3398. /* }}} */
  3399. ZEND_API void ZEND_FASTCALL zend_free_compiled_variables(zend_execute_data *execute_data) /* {{{ */
  3400. {
  3401. i_free_compiled_variables(execute_data);
  3402. }
  3403. /* }}} */
  3404. #define ZEND_VM_INTERRUPT_CHECK() do { \
  3405. if (UNEXPECTED(EG(vm_interrupt))) { \
  3406. ZEND_VM_INTERRUPT(); \
  3407. } \
  3408. } while (0)
  3409. #define ZEND_VM_LOOP_INTERRUPT_CHECK() do { \
  3410. if (UNEXPECTED(EG(vm_interrupt))) { \
  3411. ZEND_VM_LOOP_INTERRUPT(); \
  3412. } \
  3413. } while (0)
  3414. /*
  3415. * Stack Frame Layout (the whole stack frame is allocated at once)
  3416. * ==================
  3417. *
  3418. * +========================================+
  3419. * EG(current_execute_data) -> | zend_execute_data |
  3420. * +----------------------------------------+
  3421. * EX_VAR_NUM(0) --------> | VAR[0] = ARG[1] |
  3422. * | ... |
  3423. * | VAR[op_array->num_args-1] = ARG[N] |
  3424. * | ... |
  3425. * | VAR[op_array->last_var-1] |
  3426. * | VAR[op_array->last_var] = TMP[0] |
  3427. * | ... |
  3428. * | VAR[op_array->last_var+op_array->T-1] |
  3429. * | ARG[N+1] (extra_args) |
  3430. * | ... |
  3431. * +----------------------------------------+
  3432. */
  3433. /* zend_copy_extra_args is used when the actually passed number of arguments
  3434. * (EX_NUM_ARGS) is greater than what the function defined (op_array->num_args).
  3435. *
  3436. * The extra arguments will be copied into the call frame after all the compiled variables.
  3437. *
  3438. * If there are extra arguments copied, a flag "ZEND_CALL_FREE_EXTRA_ARGS" will be set
  3439. * on the zend_execute_data, and when the executor leaves the function, the
  3440. * args will be freed in zend_leave_helper.
  3441. */
  3442. static zend_never_inline void zend_copy_extra_args(EXECUTE_DATA_D)
  3443. {
  3444. zend_op_array *op_array = &EX(func)->op_array;
  3445. uint32_t first_extra_arg = op_array->num_args;
  3446. uint32_t num_args = EX_NUM_ARGS();
  3447. zval *src;
  3448. size_t delta;
  3449. uint32_t count;
  3450. uint32_t type_flags = 0;
  3451. if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0)) {
  3452. /* Skip useless ZEND_RECV and ZEND_RECV_INIT opcodes */
  3453. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3454. opline += first_extra_arg;
  3455. #else
  3456. EX(opline) += first_extra_arg;
  3457. #endif
  3458. }
  3459. /* move extra args into separate array after all CV and TMP vars */
  3460. src = EX_VAR_NUM(num_args - 1);
  3461. delta = op_array->last_var + op_array->T - first_extra_arg;
  3462. count = num_args - first_extra_arg;
  3463. if (EXPECTED(delta != 0)) {
  3464. delta *= sizeof(zval);
  3465. do {
  3466. type_flags |= Z_TYPE_INFO_P(src);
  3467. ZVAL_COPY_VALUE((zval*)(((char*)src) + delta), src);
  3468. ZVAL_UNDEF(src);
  3469. src--;
  3470. } while (--count);
  3471. if (Z_TYPE_INFO_REFCOUNTED(type_flags)) {
  3472. ZEND_ADD_CALL_FLAG(execute_data, ZEND_CALL_FREE_EXTRA_ARGS);
  3473. }
  3474. } else {
  3475. do {
  3476. if (Z_REFCOUNTED_P(src)) {
  3477. ZEND_ADD_CALL_FLAG(execute_data, ZEND_CALL_FREE_EXTRA_ARGS);
  3478. break;
  3479. }
  3480. src--;
  3481. } while (--count);
  3482. }
  3483. }
  3484. static zend_always_inline void zend_init_cvs(uint32_t first, uint32_t last EXECUTE_DATA_DC)
  3485. {
  3486. if (EXPECTED(first < last)) {
  3487. uint32_t count = last - first;
  3488. zval *var = EX_VAR_NUM(first);
  3489. do {
  3490. ZVAL_UNDEF(var);
  3491. var++;
  3492. } while (--count);
  3493. }
  3494. }
  3495. static zend_always_inline void i_init_func_execute_data(zend_op_array *op_array, zval *return_value, bool may_be_trampoline EXECUTE_DATA_DC) /* {{{ */
  3496. {
  3497. uint32_t first_extra_arg, num_args;
  3498. ZEND_ASSERT(EX(func) == (zend_function*)op_array);
  3499. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3500. opline = op_array->opcodes;
  3501. #else
  3502. EX(opline) = op_array->opcodes;
  3503. #endif
  3504. EX(call) = NULL;
  3505. EX(return_value) = return_value;
  3506. /* Handle arguments */
  3507. first_extra_arg = op_array->num_args;
  3508. num_args = EX_NUM_ARGS();
  3509. if (UNEXPECTED(num_args > first_extra_arg)) {
  3510. if (!may_be_trampoline || EXPECTED(!(op_array->fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))) {
  3511. zend_copy_extra_args(EXECUTE_DATA_C);
  3512. }
  3513. } else if (EXPECTED((op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS) == 0)) {
  3514. /* Skip useless ZEND_RECV and ZEND_RECV_INIT opcodes */
  3515. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3516. opline += num_args;
  3517. #else
  3518. EX(opline) += num_args;
  3519. #endif
  3520. }
  3521. /* Initialize CV variables (skip arguments) */
  3522. zend_init_cvs(num_args, op_array->last_var EXECUTE_DATA_CC);
  3523. EX(run_time_cache) = RUN_TIME_CACHE(op_array);
  3524. EG(current_execute_data) = execute_data;
  3525. }
  3526. /* }}} */
  3527. static zend_always_inline void init_func_run_time_cache_i(zend_op_array *op_array) /* {{{ */
  3528. {
  3529. void **run_time_cache;
  3530. ZEND_ASSERT(RUN_TIME_CACHE(op_array) == NULL);
  3531. run_time_cache = zend_arena_alloc(&CG(arena), op_array->cache_size);
  3532. memset(run_time_cache, 0, op_array->cache_size);
  3533. ZEND_MAP_PTR_SET(op_array->run_time_cache, run_time_cache);
  3534. }
  3535. /* }}} */
  3536. static zend_never_inline void ZEND_FASTCALL init_func_run_time_cache(zend_op_array *op_array) /* {{{ */
  3537. {
  3538. init_func_run_time_cache_i(op_array);
  3539. }
  3540. /* }}} */
  3541. ZEND_API zend_function * ZEND_FASTCALL zend_fetch_function(zend_string *name) /* {{{ */
  3542. {
  3543. zval *zv = zend_hash_find(EG(function_table), name);
  3544. if (EXPECTED(zv != NULL)) {
  3545. zend_function *fbc = Z_FUNC_P(zv);
  3546. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3547. init_func_run_time_cache_i(&fbc->op_array);
  3548. }
  3549. return fbc;
  3550. }
  3551. return NULL;
  3552. } /* }}} */
  3553. ZEND_API zend_function * ZEND_FASTCALL zend_fetch_function_str(const char *name, size_t len) /* {{{ */
  3554. {
  3555. zval *zv = zend_hash_str_find(EG(function_table), name, len);
  3556. if (EXPECTED(zv != NULL)) {
  3557. zend_function *fbc = Z_FUNC_P(zv);
  3558. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3559. init_func_run_time_cache_i(&fbc->op_array);
  3560. }
  3561. return fbc;
  3562. }
  3563. return NULL;
  3564. } /* }}} */
  3565. ZEND_API void ZEND_FASTCALL zend_init_func_run_time_cache(zend_op_array *op_array) /* {{{ */
  3566. {
  3567. if (!RUN_TIME_CACHE(op_array)) {
  3568. init_func_run_time_cache_i(op_array);
  3569. }
  3570. } /* }}} */
  3571. static zend_always_inline void i_init_code_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value) /* {{{ */
  3572. {
  3573. ZEND_ASSERT(EX(func) == (zend_function*)op_array);
  3574. EX(opline) = op_array->opcodes;
  3575. EX(call) = NULL;
  3576. EX(return_value) = return_value;
  3577. zend_attach_symbol_table(execute_data);
  3578. if (!ZEND_MAP_PTR(op_array->run_time_cache)) {
  3579. void *ptr;
  3580. ZEND_ASSERT(op_array->fn_flags & ZEND_ACC_HEAP_RT_CACHE);
  3581. ptr = emalloc(op_array->cache_size + sizeof(void*));
  3582. ZEND_MAP_PTR_INIT(op_array->run_time_cache, ptr);
  3583. ptr = (char*)ptr + sizeof(void*);
  3584. ZEND_MAP_PTR_SET(op_array->run_time_cache, ptr);
  3585. memset(ptr, 0, op_array->cache_size);
  3586. }
  3587. EX(run_time_cache) = RUN_TIME_CACHE(op_array);
  3588. EG(current_execute_data) = execute_data;
  3589. }
  3590. /* }}} */
  3591. ZEND_API void zend_init_func_execute_data(zend_execute_data *ex, zend_op_array *op_array, zval *return_value) /* {{{ */
  3592. {
  3593. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3594. zend_execute_data *orig_execute_data = execute_data;
  3595. #endif
  3596. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3597. const zend_op *orig_opline = opline;
  3598. #endif
  3599. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3600. execute_data = ex;
  3601. #else
  3602. zend_execute_data *execute_data = ex;
  3603. #endif
  3604. EX(prev_execute_data) = EG(current_execute_data);
  3605. if (!RUN_TIME_CACHE(op_array)) {
  3606. init_func_run_time_cache(op_array);
  3607. }
  3608. i_init_func_execute_data(op_array, return_value, 1 EXECUTE_DATA_CC);
  3609. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3610. EX(opline) = opline;
  3611. opline = orig_opline;
  3612. #endif
  3613. #if defined(ZEND_VM_FP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  3614. execute_data = orig_execute_data;
  3615. #endif
  3616. }
  3617. /* }}} */
  3618. ZEND_API void zend_init_code_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value) /* {{{ */
  3619. {
  3620. EX(prev_execute_data) = EG(current_execute_data);
  3621. i_init_code_execute_data(execute_data, op_array, return_value);
  3622. }
  3623. /* }}} */
  3624. ZEND_API void zend_init_execute_data(zend_execute_data *execute_data, zend_op_array *op_array, zval *return_value) /* {{{ */
  3625. {
  3626. if (EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE) {
  3627. zend_init_code_execute_data(execute_data, op_array, return_value);
  3628. } else {
  3629. zend_init_func_execute_data(execute_data, op_array, return_value);
  3630. }
  3631. }
  3632. /* }}} */
  3633. zend_execute_data *zend_vm_stack_copy_call_frame(zend_execute_data *call, uint32_t passed_args, uint32_t additional_args) /* {{{ */
  3634. {
  3635. zend_execute_data *new_call;
  3636. int used_stack = (EG(vm_stack_top) - (zval*)call) + additional_args;
  3637. /* copy call frame into new stack segment */
  3638. new_call = zend_vm_stack_extend(used_stack * sizeof(zval));
  3639. *new_call = *call;
  3640. ZEND_ADD_CALL_FLAG(new_call, ZEND_CALL_ALLOCATED);
  3641. if (passed_args) {
  3642. zval *src = ZEND_CALL_ARG(call, 1);
  3643. zval *dst = ZEND_CALL_ARG(new_call, 1);
  3644. do {
  3645. ZVAL_COPY_VALUE(dst, src);
  3646. passed_args--;
  3647. src++;
  3648. dst++;
  3649. } while (passed_args);
  3650. }
  3651. /* delete old call_frame from previous stack segment */
  3652. EG(vm_stack)->prev->top = (zval*)call;
  3653. /* delete previous stack segment if it became empty */
  3654. if (UNEXPECTED(EG(vm_stack)->prev->top == ZEND_VM_STACK_ELEMENTS(EG(vm_stack)->prev))) {
  3655. zend_vm_stack r = EG(vm_stack)->prev;
  3656. EG(vm_stack)->prev = r->prev;
  3657. efree(r);
  3658. }
  3659. return new_call;
  3660. }
  3661. /* }}} */
  3662. static zend_always_inline zend_generator *zend_get_running_generator(EXECUTE_DATA_D) /* {{{ */
  3663. {
  3664. /* The generator object is stored in EX(return_value) */
  3665. zend_generator *generator = (zend_generator *) EX(return_value);
  3666. /* However control may currently be delegated to another generator.
  3667. * That's the one we're interested in. */
  3668. return generator;
  3669. }
  3670. /* }}} */
  3671. static void cleanup_unfinished_calls(zend_execute_data *execute_data, uint32_t op_num) /* {{{ */
  3672. {
  3673. if (UNEXPECTED(EX(call))) {
  3674. zend_execute_data *call = EX(call);
  3675. zend_op *opline = EX(func)->op_array.opcodes + op_num;
  3676. int level;
  3677. int do_exit;
  3678. if (UNEXPECTED(opline->opcode == ZEND_INIT_FCALL ||
  3679. opline->opcode == ZEND_INIT_FCALL_BY_NAME ||
  3680. opline->opcode == ZEND_INIT_NS_FCALL_BY_NAME ||
  3681. opline->opcode == ZEND_INIT_DYNAMIC_CALL ||
  3682. opline->opcode == ZEND_INIT_USER_CALL ||
  3683. opline->opcode == ZEND_INIT_METHOD_CALL ||
  3684. opline->opcode == ZEND_INIT_STATIC_METHOD_CALL ||
  3685. opline->opcode == ZEND_NEW)) {
  3686. ZEND_ASSERT(op_num);
  3687. opline--;
  3688. }
  3689. do {
  3690. /* If the exception was thrown during a function call there might be
  3691. * arguments pushed to the stack that have to be dtor'ed. */
  3692. /* find the number of actually passed arguments */
  3693. level = 0;
  3694. do_exit = 0;
  3695. do {
  3696. switch (opline->opcode) {
  3697. case ZEND_DO_FCALL:
  3698. case ZEND_DO_ICALL:
  3699. case ZEND_DO_UCALL:
  3700. case ZEND_DO_FCALL_BY_NAME:
  3701. level++;
  3702. break;
  3703. case ZEND_INIT_FCALL:
  3704. case ZEND_INIT_FCALL_BY_NAME:
  3705. case ZEND_INIT_NS_FCALL_BY_NAME:
  3706. case ZEND_INIT_DYNAMIC_CALL:
  3707. case ZEND_INIT_USER_CALL:
  3708. case ZEND_INIT_METHOD_CALL:
  3709. case ZEND_INIT_STATIC_METHOD_CALL:
  3710. case ZEND_NEW:
  3711. if (level == 0) {
  3712. ZEND_CALL_NUM_ARGS(call) = 0;
  3713. do_exit = 1;
  3714. }
  3715. level--;
  3716. break;
  3717. case ZEND_SEND_VAL:
  3718. case ZEND_SEND_VAL_EX:
  3719. case ZEND_SEND_VAR:
  3720. case ZEND_SEND_VAR_EX:
  3721. case ZEND_SEND_FUNC_ARG:
  3722. case ZEND_SEND_REF:
  3723. case ZEND_SEND_VAR_NO_REF:
  3724. case ZEND_SEND_VAR_NO_REF_EX:
  3725. case ZEND_SEND_USER:
  3726. if (level == 0) {
  3727. /* For named args, the number of arguments is up to date. */
  3728. if (opline->op2_type != IS_CONST) {
  3729. ZEND_CALL_NUM_ARGS(call) = opline->op2.num;
  3730. }
  3731. do_exit = 1;
  3732. }
  3733. break;
  3734. case ZEND_SEND_ARRAY:
  3735. case ZEND_SEND_UNPACK:
  3736. case ZEND_CHECK_UNDEF_ARGS:
  3737. if (level == 0) {
  3738. do_exit = 1;
  3739. }
  3740. break;
  3741. }
  3742. if (!do_exit) {
  3743. opline--;
  3744. }
  3745. } while (!do_exit);
  3746. if (call->prev_execute_data) {
  3747. /* skip current call region */
  3748. level = 0;
  3749. do_exit = 0;
  3750. do {
  3751. switch (opline->opcode) {
  3752. case ZEND_DO_FCALL:
  3753. case ZEND_DO_ICALL:
  3754. case ZEND_DO_UCALL:
  3755. case ZEND_DO_FCALL_BY_NAME:
  3756. level++;
  3757. break;
  3758. case ZEND_INIT_FCALL:
  3759. case ZEND_INIT_FCALL_BY_NAME:
  3760. case ZEND_INIT_NS_FCALL_BY_NAME:
  3761. case ZEND_INIT_DYNAMIC_CALL:
  3762. case ZEND_INIT_USER_CALL:
  3763. case ZEND_INIT_METHOD_CALL:
  3764. case ZEND_INIT_STATIC_METHOD_CALL:
  3765. case ZEND_NEW:
  3766. if (level == 0) {
  3767. do_exit = 1;
  3768. }
  3769. level--;
  3770. break;
  3771. }
  3772. opline--;
  3773. } while (!do_exit);
  3774. }
  3775. zend_vm_stack_free_args(EX(call));
  3776. if (ZEND_CALL_INFO(call) & ZEND_CALL_RELEASE_THIS) {
  3777. OBJ_RELEASE(Z_OBJ(call->This));
  3778. }
  3779. if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
  3780. zend_free_extra_named_params(call->extra_named_params);
  3781. }
  3782. if (call->func->common.fn_flags & ZEND_ACC_CLOSURE) {
  3783. zend_object_release(ZEND_CLOSURE_OBJECT(call->func));
  3784. } else if (call->func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  3785. zend_string_release_ex(call->func->common.function_name, 0);
  3786. zend_free_trampoline(call->func);
  3787. }
  3788. EX(call) = call->prev_execute_data;
  3789. zend_vm_stack_free_call_frame(call);
  3790. call = EX(call);
  3791. } while (call);
  3792. }
  3793. }
  3794. /* }}} */
  3795. static const zend_live_range *find_live_range(const zend_op_array *op_array, uint32_t op_num, uint32_t var_num) /* {{{ */
  3796. {
  3797. int i;
  3798. for (i = 0; i < op_array->last_live_range; i++) {
  3799. const zend_live_range *range = &op_array->live_range[i];
  3800. if (op_num >= range->start && op_num < range->end
  3801. && var_num == (range->var & ~ZEND_LIVE_MASK)) {
  3802. return range;
  3803. }
  3804. }
  3805. return NULL;
  3806. }
  3807. /* }}} */
  3808. static void cleanup_live_vars(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num) /* {{{ */
  3809. {
  3810. int i;
  3811. for (i = 0; i < EX(func)->op_array.last_live_range; i++) {
  3812. const zend_live_range *range = &EX(func)->op_array.live_range[i];
  3813. if (range->start > op_num) {
  3814. /* further blocks will not be relevant... */
  3815. break;
  3816. } else if (op_num < range->end) {
  3817. if (!catch_op_num || catch_op_num >= range->end) {
  3818. uint32_t kind = range->var & ZEND_LIVE_MASK;
  3819. uint32_t var_num = range->var & ~ZEND_LIVE_MASK;
  3820. zval *var = EX_VAR(var_num);
  3821. if (kind == ZEND_LIVE_TMPVAR) {
  3822. zval_ptr_dtor_nogc(var);
  3823. } else if (kind == ZEND_LIVE_NEW) {
  3824. zend_object *obj;
  3825. ZEND_ASSERT(Z_TYPE_P(var) == IS_OBJECT);
  3826. obj = Z_OBJ_P(var);
  3827. zend_object_store_ctor_failed(obj);
  3828. OBJ_RELEASE(obj);
  3829. } else if (kind == ZEND_LIVE_LOOP) {
  3830. if (Z_TYPE_P(var) != IS_ARRAY && Z_FE_ITER_P(var) != (uint32_t)-1) {
  3831. zend_hash_iterator_del(Z_FE_ITER_P(var));
  3832. }
  3833. zval_ptr_dtor_nogc(var);
  3834. } else if (kind == ZEND_LIVE_ROPE) {
  3835. zend_string **rope = (zend_string **)var;
  3836. zend_op *last = EX(func)->op_array.opcodes + op_num;
  3837. while ((last->opcode != ZEND_ROPE_ADD && last->opcode != ZEND_ROPE_INIT)
  3838. || last->result.var != var_num) {
  3839. ZEND_ASSERT(last >= EX(func)->op_array.opcodes);
  3840. last--;
  3841. }
  3842. if (last->opcode == ZEND_ROPE_INIT) {
  3843. zend_string_release_ex(*rope, 0);
  3844. } else {
  3845. int j = last->extended_value;
  3846. do {
  3847. zend_string_release_ex(rope[j], 0);
  3848. } while (j--);
  3849. }
  3850. } else if (kind == ZEND_LIVE_SILENCE) {
  3851. /* restore previous error_reporting value */
  3852. if (E_HAS_ONLY_FATAL_ERRORS(EG(error_reporting))
  3853. && !E_HAS_ONLY_FATAL_ERRORS(Z_LVAL_P(var))) {
  3854. EG(error_reporting) = Z_LVAL_P(var);
  3855. }
  3856. }
  3857. }
  3858. }
  3859. }
  3860. }
  3861. /* }}} */
  3862. ZEND_API void zend_cleanup_unfinished_execution(zend_execute_data *execute_data, uint32_t op_num, uint32_t catch_op_num) {
  3863. cleanup_unfinished_calls(execute_data, op_num);
  3864. cleanup_live_vars(execute_data, op_num, catch_op_num);
  3865. }
  3866. #if ZEND_VM_SPEC
  3867. static void zend_swap_operands(zend_op *op) /* {{{ */
  3868. {
  3869. znode_op tmp;
  3870. zend_uchar tmp_type;
  3871. tmp = op->op1;
  3872. tmp_type = op->op1_type;
  3873. op->op1 = op->op2;
  3874. op->op1_type = op->op2_type;
  3875. op->op2 = tmp;
  3876. op->op2_type = tmp_type;
  3877. }
  3878. /* }}} */
  3879. #endif
  3880. static zend_never_inline zend_execute_data *zend_init_dynamic_call_string(zend_string *function, uint32_t num_args) /* {{{ */
  3881. {
  3882. zend_function *fbc;
  3883. zval *func;
  3884. zend_class_entry *called_scope;
  3885. zend_string *lcname;
  3886. const char *colon;
  3887. if ((colon = zend_memrchr(ZSTR_VAL(function), ':', ZSTR_LEN(function))) != NULL &&
  3888. colon > ZSTR_VAL(function) &&
  3889. *(colon-1) == ':'
  3890. ) {
  3891. zend_string *mname;
  3892. size_t cname_length = colon - ZSTR_VAL(function) - 1;
  3893. size_t mname_length = ZSTR_LEN(function) - cname_length - (sizeof("::") - 1);
  3894. lcname = zend_string_init(ZSTR_VAL(function), cname_length, 0);
  3895. called_scope = zend_fetch_class_by_name(lcname, NULL, ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);
  3896. if (UNEXPECTED(called_scope == NULL)) {
  3897. zend_string_release_ex(lcname, 0);
  3898. return NULL;
  3899. }
  3900. mname = zend_string_init(ZSTR_VAL(function) + (cname_length + sizeof("::") - 1), mname_length, 0);
  3901. if (called_scope->get_static_method) {
  3902. fbc = called_scope->get_static_method(called_scope, mname);
  3903. } else {
  3904. fbc = zend_std_get_static_method(called_scope, mname, NULL);
  3905. }
  3906. if (UNEXPECTED(fbc == NULL)) {
  3907. if (EXPECTED(!EG(exception))) {
  3908. zend_undefined_method(called_scope, mname);
  3909. }
  3910. zend_string_release_ex(lcname, 0);
  3911. zend_string_release_ex(mname, 0);
  3912. return NULL;
  3913. }
  3914. zend_string_release_ex(lcname, 0);
  3915. zend_string_release_ex(mname, 0);
  3916. if (UNEXPECTED(!(fbc->common.fn_flags & ZEND_ACC_STATIC))) {
  3917. zend_non_static_method_call(fbc);
  3918. if (fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  3919. zend_string_release_ex(fbc->common.function_name, 0);
  3920. zend_free_trampoline(fbc);
  3921. }
  3922. return NULL;
  3923. }
  3924. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3925. init_func_run_time_cache(&fbc->op_array);
  3926. }
  3927. } else {
  3928. if (ZSTR_VAL(function)[0] == '\\') {
  3929. lcname = zend_string_alloc(ZSTR_LEN(function) - 1, 0);
  3930. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(function) + 1, ZSTR_LEN(function) - 1);
  3931. } else {
  3932. lcname = zend_string_tolower(function);
  3933. }
  3934. if (UNEXPECTED((func = zend_hash_find(EG(function_table), lcname)) == NULL)) {
  3935. zend_throw_error(NULL, "Call to undefined function %s()", ZSTR_VAL(function));
  3936. zend_string_release_ex(lcname, 0);
  3937. return NULL;
  3938. }
  3939. zend_string_release_ex(lcname, 0);
  3940. fbc = Z_FUNC_P(func);
  3941. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3942. init_func_run_time_cache(&fbc->op_array);
  3943. }
  3944. called_scope = NULL;
  3945. }
  3946. return zend_vm_stack_push_call_frame(ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC,
  3947. fbc, num_args, called_scope);
  3948. }
  3949. /* }}} */
  3950. static zend_never_inline zend_execute_data *zend_init_dynamic_call_object(zend_object *function, uint32_t num_args) /* {{{ */
  3951. {
  3952. zend_function *fbc;
  3953. void *object_or_called_scope;
  3954. zend_class_entry *called_scope;
  3955. zend_object *object;
  3956. uint32_t call_info;
  3957. if (EXPECTED(function->handlers->get_closure) &&
  3958. EXPECTED(function->handlers->get_closure(function, &called_scope, &fbc, &object, 0) == SUCCESS)) {
  3959. object_or_called_scope = called_scope;
  3960. if (EXPECTED(fbc->common.fn_flags & ZEND_ACC_CLOSURE)) {
  3961. /* Delay closure destruction until its invocation */
  3962. GC_ADDREF(ZEND_CLOSURE_OBJECT(fbc));
  3963. ZEND_ASSERT(ZEND_ACC_FAKE_CLOSURE == ZEND_CALL_FAKE_CLOSURE);
  3964. call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_CLOSURE |
  3965. (fbc->common.fn_flags & ZEND_ACC_FAKE_CLOSURE);
  3966. if (object) {
  3967. call_info |= ZEND_CALL_HAS_THIS;
  3968. object_or_called_scope = object;
  3969. }
  3970. } else {
  3971. call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC;
  3972. if (object) {
  3973. call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS;
  3974. GC_ADDREF(object); /* For $this pointer */
  3975. object_or_called_scope = object;
  3976. }
  3977. }
  3978. } else {
  3979. zend_throw_error(NULL, "Object of type %s is not callable", ZSTR_VAL(function->ce->name));
  3980. return NULL;
  3981. }
  3982. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  3983. init_func_run_time_cache(&fbc->op_array);
  3984. }
  3985. return zend_vm_stack_push_call_frame(call_info,
  3986. fbc, num_args, object_or_called_scope);
  3987. }
  3988. /* }}} */
  3989. static zend_never_inline zend_execute_data *zend_init_dynamic_call_array(zend_array *function, uint32_t num_args) /* {{{ */
  3990. {
  3991. zend_function *fbc;
  3992. void *object_or_called_scope;
  3993. uint32_t call_info = ZEND_CALL_NESTED_FUNCTION | ZEND_CALL_DYNAMIC;
  3994. if (zend_hash_num_elements(function) == 2) {
  3995. zval *obj;
  3996. zval *method;
  3997. obj = zend_hash_index_find(function, 0);
  3998. method = zend_hash_index_find(function, 1);
  3999. if (UNEXPECTED(!obj) || UNEXPECTED(!method)) {
  4000. zend_throw_error(NULL, "Array callback has to contain indices 0 and 1");
  4001. return NULL;
  4002. }
  4003. ZVAL_DEREF(obj);
  4004. if (UNEXPECTED(Z_TYPE_P(obj) != IS_STRING) && UNEXPECTED(Z_TYPE_P(obj) != IS_OBJECT)) {
  4005. zend_throw_error(NULL, "First array member is not a valid class name or object");
  4006. return NULL;
  4007. }
  4008. ZVAL_DEREF(method);
  4009. if (UNEXPECTED(Z_TYPE_P(method) != IS_STRING)) {
  4010. zend_throw_error(NULL, "Second array member is not a valid method");
  4011. return NULL;
  4012. }
  4013. if (Z_TYPE_P(obj) == IS_STRING) {
  4014. zend_class_entry *called_scope = zend_fetch_class_by_name(Z_STR_P(obj), NULL, ZEND_FETCH_CLASS_DEFAULT | ZEND_FETCH_CLASS_EXCEPTION);
  4015. if (UNEXPECTED(called_scope == NULL)) {
  4016. return NULL;
  4017. }
  4018. if (called_scope->get_static_method) {
  4019. fbc = called_scope->get_static_method(called_scope, Z_STR_P(method));
  4020. } else {
  4021. fbc = zend_std_get_static_method(called_scope, Z_STR_P(method), NULL);
  4022. }
  4023. if (UNEXPECTED(fbc == NULL)) {
  4024. if (EXPECTED(!EG(exception))) {
  4025. zend_undefined_method(called_scope, Z_STR_P(method));
  4026. }
  4027. return NULL;
  4028. }
  4029. if (!(fbc->common.fn_flags & ZEND_ACC_STATIC)) {
  4030. zend_non_static_method_call(fbc);
  4031. if (fbc->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  4032. zend_string_release_ex(fbc->common.function_name, 0);
  4033. zend_free_trampoline(fbc);
  4034. }
  4035. return NULL;
  4036. }
  4037. object_or_called_scope = called_scope;
  4038. } else {
  4039. zend_object *object = Z_OBJ_P(obj);
  4040. fbc = Z_OBJ_HT_P(obj)->get_method(&object, Z_STR_P(method), NULL);
  4041. if (UNEXPECTED(fbc == NULL)) {
  4042. if (EXPECTED(!EG(exception))) {
  4043. zend_undefined_method(object->ce, Z_STR_P(method));
  4044. }
  4045. return NULL;
  4046. }
  4047. if ((fbc->common.fn_flags & ZEND_ACC_STATIC) != 0) {
  4048. object_or_called_scope = object->ce;
  4049. } else {
  4050. call_info |= ZEND_CALL_RELEASE_THIS | ZEND_CALL_HAS_THIS;
  4051. GC_ADDREF(object); /* For $this pointer */
  4052. object_or_called_scope = object;
  4053. }
  4054. }
  4055. } else {
  4056. zend_throw_error(NULL, "Array callback must have exactly two elements");
  4057. return NULL;
  4058. }
  4059. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) && UNEXPECTED(!RUN_TIME_CACHE(&fbc->op_array))) {
  4060. init_func_run_time_cache(&fbc->op_array);
  4061. }
  4062. return zend_vm_stack_push_call_frame(call_info,
  4063. fbc, num_args, object_or_called_scope);
  4064. }
  4065. /* }}} */
  4066. #define ZEND_FAKE_OP_ARRAY ((zend_op_array*)(zend_intptr_t)-1)
  4067. static zend_never_inline zend_op_array* ZEND_FASTCALL zend_include_or_eval(zval *inc_filename_zv, int type) /* {{{ */
  4068. {
  4069. zend_op_array *new_op_array = NULL;
  4070. zend_string *tmp_inc_filename;
  4071. zend_string *inc_filename = zval_try_get_tmp_string(inc_filename_zv, &tmp_inc_filename);
  4072. if (UNEXPECTED(!inc_filename)) {
  4073. return NULL;
  4074. }
  4075. switch (type) {
  4076. case ZEND_INCLUDE_ONCE:
  4077. case ZEND_REQUIRE_ONCE: {
  4078. zend_file_handle file_handle;
  4079. zend_string *resolved_path;
  4080. resolved_path = zend_resolve_path(inc_filename);
  4081. if (EXPECTED(resolved_path)) {
  4082. if (zend_hash_exists(&EG(included_files), resolved_path)) {
  4083. new_op_array = ZEND_FAKE_OP_ARRAY;
  4084. zend_string_release_ex(resolved_path, 0);
  4085. break;
  4086. }
  4087. } else if (UNEXPECTED(EG(exception))) {
  4088. break;
  4089. } else if (UNEXPECTED(strlen(ZSTR_VAL(inc_filename)) != ZSTR_LEN(inc_filename))) {
  4090. zend_message_dispatcher(
  4091. (type == ZEND_INCLUDE_ONCE) ?
  4092. ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,
  4093. ZSTR_VAL(inc_filename));
  4094. break;
  4095. } else {
  4096. resolved_path = zend_string_copy(inc_filename);
  4097. }
  4098. zend_stream_init_filename_ex(&file_handle, resolved_path);
  4099. if (SUCCESS == zend_stream_open(&file_handle)) {
  4100. if (!file_handle.opened_path) {
  4101. file_handle.opened_path = zend_string_copy(resolved_path);
  4102. }
  4103. if (zend_hash_add_empty_element(&EG(included_files), file_handle.opened_path)) {
  4104. zend_op_array *op_array = zend_compile_file(&file_handle, (type==ZEND_INCLUDE_ONCE?ZEND_INCLUDE:ZEND_REQUIRE));
  4105. zend_destroy_file_handle(&file_handle);
  4106. zend_string_release_ex(resolved_path, 0);
  4107. zend_tmp_string_release(tmp_inc_filename);
  4108. return op_array;
  4109. } else {
  4110. new_op_array = ZEND_FAKE_OP_ARRAY;
  4111. }
  4112. } else if (!EG(exception)) {
  4113. zend_message_dispatcher(
  4114. (type == ZEND_INCLUDE_ONCE) ?
  4115. ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,
  4116. ZSTR_VAL(inc_filename));
  4117. }
  4118. zend_destroy_file_handle(&file_handle);
  4119. zend_string_release_ex(resolved_path, 0);
  4120. }
  4121. break;
  4122. case ZEND_INCLUDE:
  4123. case ZEND_REQUIRE:
  4124. if (UNEXPECTED(strlen(ZSTR_VAL(inc_filename)) != ZSTR_LEN(inc_filename))) {
  4125. zend_message_dispatcher(
  4126. (type == ZEND_INCLUDE) ?
  4127. ZMSG_FAILED_INCLUDE_FOPEN : ZMSG_FAILED_REQUIRE_FOPEN,
  4128. ZSTR_VAL(inc_filename));
  4129. break;
  4130. }
  4131. new_op_array = compile_filename(type, inc_filename);
  4132. break;
  4133. case ZEND_EVAL: {
  4134. char *eval_desc = zend_make_compiled_string_description("eval()'d code");
  4135. new_op_array = zend_compile_string(inc_filename, eval_desc);
  4136. efree(eval_desc);
  4137. }
  4138. break;
  4139. EMPTY_SWITCH_DEFAULT_CASE()
  4140. }
  4141. zend_tmp_string_release(tmp_inc_filename);
  4142. return new_op_array;
  4143. }
  4144. /* }}} */
  4145. static zend_never_inline bool ZEND_FASTCALL zend_fe_reset_iterator(zval *array_ptr, int by_ref OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
  4146. {
  4147. zend_class_entry *ce = Z_OBJCE_P(array_ptr);
  4148. zend_object_iterator *iter = ce->get_iterator(ce, array_ptr, by_ref);
  4149. bool is_empty;
  4150. if (UNEXPECTED(!iter) || UNEXPECTED(EG(exception))) {
  4151. if (iter) {
  4152. OBJ_RELEASE(&iter->std);
  4153. }
  4154. if (!EG(exception)) {
  4155. zend_throw_exception_ex(NULL, 0, "Object of type %s did not create an Iterator", ZSTR_VAL(ce->name));
  4156. }
  4157. ZVAL_UNDEF(EX_VAR(opline->result.var));
  4158. return 1;
  4159. }
  4160. iter->index = 0;
  4161. if (iter->funcs->rewind) {
  4162. iter->funcs->rewind(iter);
  4163. if (UNEXPECTED(EG(exception) != NULL)) {
  4164. OBJ_RELEASE(&iter->std);
  4165. ZVAL_UNDEF(EX_VAR(opline->result.var));
  4166. return 1;
  4167. }
  4168. }
  4169. is_empty = iter->funcs->valid(iter) != SUCCESS;
  4170. if (UNEXPECTED(EG(exception) != NULL)) {
  4171. OBJ_RELEASE(&iter->std);
  4172. ZVAL_UNDEF(EX_VAR(opline->result.var));
  4173. return 1;
  4174. }
  4175. iter->index = -1; /* will be set to 0 before using next handler */
  4176. ZVAL_OBJ(EX_VAR(opline->result.var), &iter->std);
  4177. Z_FE_ITER_P(EX_VAR(opline->result.var)) = (uint32_t)-1;
  4178. return is_empty;
  4179. }
  4180. /* }}} */
  4181. static zend_always_inline zend_result _zend_quick_get_constant(
  4182. const zval *key, uint32_t flags, bool check_defined_only OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
  4183. {
  4184. zval *zv;
  4185. zend_constant *c = NULL;
  4186. /* null/true/false are resolved during compilation, so don't check for them here. */
  4187. zv = zend_hash_find_known_hash(EG(zend_constants), Z_STR_P(key));
  4188. if (zv) {
  4189. c = (zend_constant*)Z_PTR_P(zv);
  4190. } else if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
  4191. key++;
  4192. zv = zend_hash_find_known_hash(EG(zend_constants), Z_STR_P(key));
  4193. if (zv) {
  4194. c = (zend_constant*)Z_PTR_P(zv);
  4195. }
  4196. }
  4197. if (!c) {
  4198. if (!check_defined_only) {
  4199. zend_throw_error(NULL, "Undefined constant \"%s\"", Z_STRVAL_P(RT_CONSTANT(opline, opline->op2)));
  4200. ZVAL_UNDEF(EX_VAR(opline->result.var));
  4201. }
  4202. return FAILURE;
  4203. }
  4204. if (!check_defined_only) {
  4205. ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
  4206. if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
  4207. zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
  4208. return SUCCESS;
  4209. }
  4210. }
  4211. CACHE_PTR(opline->extended_value, c);
  4212. return SUCCESS;
  4213. }
  4214. /* }}} */
  4215. static zend_never_inline void ZEND_FASTCALL zend_quick_get_constant(
  4216. const zval *key, uint32_t flags OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
  4217. {
  4218. _zend_quick_get_constant(key, flags, 0 OPLINE_CC EXECUTE_DATA_CC);
  4219. } /* }}} */
  4220. static zend_never_inline zend_result ZEND_FASTCALL zend_quick_check_constant(
  4221. const zval *key OPLINE_DC EXECUTE_DATA_DC) /* {{{ */
  4222. {
  4223. return _zend_quick_get_constant(key, 0, 1 OPLINE_CC EXECUTE_DATA_CC);
  4224. } /* }}} */
  4225. static zend_always_inline uint32_t zend_get_arg_offset_by_name(
  4226. zend_function *fbc, zend_string *arg_name, void **cache_slot) {
  4227. if (EXPECTED(*cache_slot == fbc)) {
  4228. return *(uintptr_t *)(cache_slot + 1);
  4229. }
  4230. // TODO: Use a hash table?
  4231. uint32_t num_args = fbc->common.num_args;
  4232. if (EXPECTED(fbc->type == ZEND_USER_FUNCTION)
  4233. || EXPECTED(fbc->common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
  4234. for (uint32_t i = 0; i < num_args; i++) {
  4235. zend_arg_info *arg_info = &fbc->op_array.arg_info[i];
  4236. if (zend_string_equals(arg_name, arg_info->name)) {
  4237. *cache_slot = fbc;
  4238. *(uintptr_t *)(cache_slot + 1) = i;
  4239. return i;
  4240. }
  4241. }
  4242. } else {
  4243. for (uint32_t i = 0; i < num_args; i++) {
  4244. zend_internal_arg_info *arg_info = &fbc->internal_function.arg_info[i];
  4245. size_t len = strlen(arg_info->name);
  4246. if (len == ZSTR_LEN(arg_name) && !memcmp(arg_info->name, ZSTR_VAL(arg_name), len)) {
  4247. *cache_slot = fbc;
  4248. *(uintptr_t *)(cache_slot + 1) = i;
  4249. return i;
  4250. }
  4251. }
  4252. }
  4253. if (fbc->common.fn_flags & ZEND_ACC_VARIADIC) {
  4254. *cache_slot = fbc;
  4255. *(uintptr_t *)(cache_slot + 1) = fbc->common.num_args;
  4256. return fbc->common.num_args;
  4257. }
  4258. return (uint32_t) -1;
  4259. }
  4260. zval * ZEND_FASTCALL zend_handle_named_arg(
  4261. zend_execute_data **call_ptr, zend_string *arg_name,
  4262. uint32_t *arg_num_ptr, void **cache_slot) {
  4263. zend_execute_data *call = *call_ptr;
  4264. zend_function *fbc = call->func;
  4265. uint32_t arg_offset = zend_get_arg_offset_by_name(fbc, arg_name, cache_slot);
  4266. if (UNEXPECTED(arg_offset == (uint32_t) -1)) {
  4267. zend_throw_error(NULL, "Unknown named parameter $%s", ZSTR_VAL(arg_name));
  4268. return NULL;
  4269. }
  4270. zval *arg;
  4271. if (UNEXPECTED(arg_offset == fbc->common.num_args)) {
  4272. /* Unknown named parameter that will be collected into a variadic. */
  4273. if (!(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS)) {
  4274. ZEND_ADD_CALL_FLAG(call, ZEND_CALL_HAS_EXTRA_NAMED_PARAMS);
  4275. call->extra_named_params = zend_new_array(0);
  4276. }
  4277. arg = zend_hash_add_empty_element(call->extra_named_params, arg_name);
  4278. if (!arg) {
  4279. zend_throw_error(NULL, "Named parameter $%s overwrites previous argument",
  4280. ZSTR_VAL(arg_name));
  4281. return NULL;
  4282. }
  4283. *arg_num_ptr = arg_offset + 1;
  4284. return arg;
  4285. }
  4286. uint32_t current_num_args = ZEND_CALL_NUM_ARGS(call);
  4287. // TODO: We may wish to optimize the arg_offset == current_num_args case,
  4288. // which is probably common (if the named parameters are in order of declaration).
  4289. if (arg_offset >= current_num_args) {
  4290. uint32_t new_num_args = arg_offset + 1;
  4291. ZEND_CALL_NUM_ARGS(call) = new_num_args;
  4292. uint32_t num_extra_args = new_num_args - current_num_args;
  4293. zend_vm_stack_extend_call_frame(call_ptr, current_num_args, num_extra_args);
  4294. call = *call_ptr;
  4295. arg = ZEND_CALL_VAR_NUM(call, arg_offset);
  4296. if (num_extra_args > 1) {
  4297. zval *zv = ZEND_CALL_VAR_NUM(call, current_num_args);
  4298. do {
  4299. ZVAL_UNDEF(zv);
  4300. zv++;
  4301. } while (zv != arg);
  4302. ZEND_ADD_CALL_FLAG(call, ZEND_CALL_MAY_HAVE_UNDEF);
  4303. }
  4304. } else {
  4305. arg = ZEND_CALL_VAR_NUM(call, arg_offset);
  4306. if (UNEXPECTED(!Z_ISUNDEF_P(arg))) {
  4307. zend_throw_error(NULL, "Named parameter $%s overwrites previous argument",
  4308. ZSTR_VAL(arg_name));
  4309. return NULL;
  4310. }
  4311. }
  4312. *arg_num_ptr = arg_offset + 1;
  4313. return arg;
  4314. }
  4315. static zend_execute_data *start_fake_frame(zend_execute_data *call, const zend_op *opline) {
  4316. zend_execute_data *old_prev_execute_data = call->prev_execute_data;
  4317. call->prev_execute_data = EG(current_execute_data);
  4318. call->opline = opline;
  4319. EG(current_execute_data) = call;
  4320. return old_prev_execute_data;
  4321. }
  4322. static void end_fake_frame(zend_execute_data *call, zend_execute_data *old_prev_execute_data) {
  4323. zend_execute_data *prev_execute_data = call->prev_execute_data;
  4324. EG(current_execute_data) = prev_execute_data;
  4325. call->prev_execute_data = old_prev_execute_data;
  4326. if (UNEXPECTED(EG(exception)) && ZEND_USER_CODE(prev_execute_data->func->common.type)) {
  4327. zend_rethrow_exception(prev_execute_data);
  4328. }
  4329. }
  4330. ZEND_API zend_result ZEND_FASTCALL zend_handle_undef_args(zend_execute_data *call) {
  4331. zend_function *fbc = call->func;
  4332. if (fbc->type == ZEND_USER_FUNCTION) {
  4333. zend_op_array *op_array = &fbc->op_array;
  4334. uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
  4335. for (uint32_t i = 0; i < num_args; i++) {
  4336. zval *arg = ZEND_CALL_VAR_NUM(call, i);
  4337. if (!Z_ISUNDEF_P(arg)) {
  4338. continue;
  4339. }
  4340. zend_op *opline = &op_array->opcodes[i];
  4341. if (EXPECTED(opline->opcode == ZEND_RECV_INIT)) {
  4342. zval *default_value = RT_CONSTANT(opline, opline->op2);
  4343. if (Z_OPT_TYPE_P(default_value) == IS_CONSTANT_AST) {
  4344. if (UNEXPECTED(!RUN_TIME_CACHE(op_array))) {
  4345. init_func_run_time_cache(op_array);
  4346. }
  4347. void *run_time_cache = RUN_TIME_CACHE(op_array);
  4348. zval *cache_val =
  4349. (zval *) ((char *) run_time_cache + Z_CACHE_SLOT_P(default_value));
  4350. if (Z_TYPE_P(cache_val) != IS_UNDEF) {
  4351. /* We keep in cache only not refcounted values */
  4352. ZVAL_COPY_VALUE(arg, cache_val);
  4353. } else {
  4354. /* Update constant inside a temporary zval, to make sure the CONSTANT_AST
  4355. * value is not accessible through back traces. */
  4356. zval tmp;
  4357. ZVAL_COPY(&tmp, default_value);
  4358. zend_execute_data *old = start_fake_frame(call, opline);
  4359. zend_result ret = zval_update_constant_ex(&tmp, fbc->op_array.scope);
  4360. end_fake_frame(call, old);
  4361. if (UNEXPECTED(ret == FAILURE)) {
  4362. zval_ptr_dtor_nogc(&tmp);
  4363. return FAILURE;
  4364. }
  4365. ZVAL_COPY_VALUE(arg, &tmp);
  4366. if (!Z_REFCOUNTED(tmp)) {
  4367. ZVAL_COPY_VALUE(cache_val, &tmp);
  4368. }
  4369. }
  4370. } else {
  4371. ZVAL_COPY(arg, default_value);
  4372. }
  4373. } else {
  4374. ZEND_ASSERT(opline->opcode == ZEND_RECV);
  4375. zend_execute_data *old = start_fake_frame(call, opline);
  4376. zend_argument_error(zend_ce_argument_count_error, i + 1, "not passed");
  4377. end_fake_frame(call, old);
  4378. return FAILURE;
  4379. }
  4380. }
  4381. return SUCCESS;
  4382. } else {
  4383. if (fbc->common.fn_flags & ZEND_ACC_USER_ARG_INFO) {
  4384. /* Magic function, let it deal with it. */
  4385. return SUCCESS;
  4386. }
  4387. uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
  4388. for (uint32_t i = 0; i < num_args; i++) {
  4389. zval *arg = ZEND_CALL_VAR_NUM(call, i);
  4390. if (!Z_ISUNDEF_P(arg)) {
  4391. continue;
  4392. }
  4393. zend_internal_arg_info *arg_info = &fbc->internal_function.arg_info[i];
  4394. if (i < fbc->common.required_num_args) {
  4395. zend_execute_data *old = start_fake_frame(call, NULL);
  4396. zend_argument_error(zend_ce_argument_count_error, i + 1, "not passed");
  4397. end_fake_frame(call, old);
  4398. return FAILURE;
  4399. }
  4400. zval default_value;
  4401. if (zend_get_default_from_internal_arg_info(&default_value, arg_info) == FAILURE) {
  4402. zend_execute_data *old = start_fake_frame(call, NULL);
  4403. zend_argument_error(zend_ce_argument_count_error, i + 1,
  4404. "must be passed explicitly, because the default value is not known");
  4405. end_fake_frame(call, old);
  4406. return FAILURE;
  4407. }
  4408. if (Z_TYPE(default_value) == IS_CONSTANT_AST) {
  4409. zend_execute_data *old = start_fake_frame(call, NULL);
  4410. zend_result ret = zval_update_constant_ex(&default_value, fbc->common.scope);
  4411. end_fake_frame(call, old);
  4412. if (ret == FAILURE) {
  4413. return FAILURE;
  4414. }
  4415. }
  4416. ZVAL_COPY_VALUE(arg, &default_value);
  4417. if (ZEND_ARG_SEND_MODE(arg_info) & ZEND_SEND_BY_REF) {
  4418. ZVAL_NEW_REF(arg, arg);
  4419. }
  4420. }
  4421. }
  4422. return SUCCESS;
  4423. }
  4424. ZEND_API void ZEND_FASTCALL zend_free_extra_named_params(zend_array *extra_named_params)
  4425. {
  4426. /* Extra named params may be shared. */
  4427. zend_array_release(extra_named_params);
  4428. }
  4429. #if defined(ZEND_VM_IP_GLOBAL_REG) && ((ZEND_VM_KIND == ZEND_VM_KIND_CALL) || (ZEND_VM_KIND == ZEND_VM_KIND_HYBRID))
  4430. /* Special versions of functions that sets EX(opline) before calling zend_vm_stack_extend() */
  4431. static zend_always_inline zend_execute_data *_zend_vm_stack_push_call_frame_ex(uint32_t used_stack, uint32_t call_info, zend_function *func, uint32_t num_args, void *object_or_called_scope) /* {{{ */
  4432. {
  4433. zend_execute_data *call = (zend_execute_data*)EG(vm_stack_top);
  4434. ZEND_ASSERT_VM_STACK_GLOBAL;
  4435. if (UNEXPECTED(used_stack > (size_t)(((char*)EG(vm_stack_end)) - (char*)call))) {
  4436. EX(opline) = opline; /* this is the only difference */
  4437. call = (zend_execute_data*)zend_vm_stack_extend(used_stack);
  4438. ZEND_ASSERT_VM_STACK_GLOBAL;
  4439. zend_vm_init_call_frame(call, call_info | ZEND_CALL_ALLOCATED, func, num_args, object_or_called_scope);
  4440. return call;
  4441. } else {
  4442. EG(vm_stack_top) = (zval*)((char*)call + used_stack);
  4443. zend_vm_init_call_frame(call, call_info, func, num_args, object_or_called_scope);
  4444. return call;
  4445. }
  4446. } /* }}} */
  4447. static zend_always_inline zend_execute_data *_zend_vm_stack_push_call_frame(uint32_t call_info, zend_function *func, uint32_t num_args, void *object_or_called_scope) /* {{{ */
  4448. {
  4449. uint32_t used_stack = zend_vm_calc_used_stack(num_args, func);
  4450. return _zend_vm_stack_push_call_frame_ex(used_stack, call_info,
  4451. func, num_args, object_or_called_scope);
  4452. } /* }}} */
  4453. #else
  4454. # define _zend_vm_stack_push_call_frame_ex zend_vm_stack_push_call_frame_ex
  4455. # define _zend_vm_stack_push_call_frame zend_vm_stack_push_call_frame
  4456. #endif
  4457. #ifdef ZEND_VM_TRACE_HANDLERS
  4458. # include "zend_vm_trace_handlers.h"
  4459. #elif defined(ZEND_VM_TRACE_LINES)
  4460. # include "zend_vm_trace_lines.h"
  4461. #elif defined(ZEND_VM_TRACE_MAP)
  4462. # include "zend_vm_trace_map.h"
  4463. #endif
  4464. #define ZEND_VM_NEXT_OPCODE_EX(check_exception, skip) \
  4465. CHECK_SYMBOL_TABLES() \
  4466. if (check_exception) { \
  4467. OPLINE = EX(opline) + (skip); \
  4468. } else { \
  4469. ZEND_ASSERT(!EG(exception)); \
  4470. OPLINE = opline + (skip); \
  4471. } \
  4472. ZEND_VM_CONTINUE()
  4473. #define ZEND_VM_NEXT_OPCODE_CHECK_EXCEPTION() \
  4474. ZEND_VM_NEXT_OPCODE_EX(1, 1)
  4475. #define ZEND_VM_NEXT_OPCODE() \
  4476. ZEND_VM_NEXT_OPCODE_EX(0, 1)
  4477. #define ZEND_VM_SET_NEXT_OPCODE(new_op) \
  4478. CHECK_SYMBOL_TABLES() \
  4479. OPLINE = new_op
  4480. #define ZEND_VM_SET_OPCODE(new_op) \
  4481. CHECK_SYMBOL_TABLES() \
  4482. OPLINE = new_op; \
  4483. ZEND_VM_INTERRUPT_CHECK()
  4484. #define ZEND_VM_SET_RELATIVE_OPCODE(opline, offset) \
  4485. ZEND_VM_SET_OPCODE(ZEND_OFFSET_TO_OPLINE(opline, offset))
  4486. #define ZEND_VM_JMP_EX(new_op, check_exception) do { \
  4487. if (check_exception && UNEXPECTED(EG(exception))) { \
  4488. HANDLE_EXCEPTION(); \
  4489. } \
  4490. ZEND_VM_SET_OPCODE(new_op); \
  4491. ZEND_VM_CONTINUE(); \
  4492. } while (0)
  4493. #define ZEND_VM_JMP(new_op) \
  4494. ZEND_VM_JMP_EX(new_op, 1)
  4495. #define ZEND_VM_INC_OPCODE() \
  4496. OPLINE++
  4497. #define ZEND_VM_REPEATABLE_OPCODE \
  4498. do {
  4499. #define ZEND_VM_REPEAT_OPCODE(_opcode) \
  4500. } while (UNEXPECTED((++opline)->opcode == _opcode)); \
  4501. OPLINE = opline; \
  4502. ZEND_VM_CONTINUE()
  4503. #define ZEND_VM_SMART_BRANCH(_result, _check) do { \
  4504. if ((_check) && UNEXPECTED(EG(exception))) { \
  4505. OPLINE = EX(opline); \
  4506. } else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPZ|IS_TMP_VAR))) { \
  4507. if (_result) { \
  4508. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  4509. } else { \
  4510. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  4511. } \
  4512. } else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPNZ|IS_TMP_VAR))) { \
  4513. if (!(_result)) { \
  4514. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  4515. } else { \
  4516. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  4517. } \
  4518. } else { \
  4519. ZVAL_BOOL(EX_VAR(opline->result.var), _result); \
  4520. ZEND_VM_SET_NEXT_OPCODE(opline + 1); \
  4521. } \
  4522. ZEND_VM_CONTINUE(); \
  4523. } while (0)
  4524. #define ZEND_VM_SMART_BRANCH_JMPZ(_result, _check) do { \
  4525. if ((_check) && UNEXPECTED(EG(exception))) { \
  4526. OPLINE = EX(opline); \
  4527. } else if (_result) { \
  4528. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  4529. } else { \
  4530. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  4531. } \
  4532. ZEND_VM_CONTINUE(); \
  4533. } while (0)
  4534. #define ZEND_VM_SMART_BRANCH_JMPNZ(_result, _check) do { \
  4535. if ((_check) && UNEXPECTED(EG(exception))) { \
  4536. OPLINE = EX(opline); \
  4537. } else if (!(_result)) { \
  4538. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  4539. } else { \
  4540. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  4541. } \
  4542. ZEND_VM_CONTINUE(); \
  4543. } while (0)
  4544. #define ZEND_VM_SMART_BRANCH_NONE(_result, _check) do { \
  4545. ZVAL_BOOL(EX_VAR(opline->result.var), _result); \
  4546. ZEND_VM_NEXT_OPCODE_EX(_check, 1); \
  4547. ZEND_VM_CONTINUE(); \
  4548. } while (0)
  4549. #define ZEND_VM_SMART_BRANCH_TRUE() do { \
  4550. if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPNZ|IS_TMP_VAR))) { \
  4551. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  4552. } else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPZ|IS_TMP_VAR))) { \
  4553. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  4554. } else { \
  4555. ZVAL_TRUE(EX_VAR(opline->result.var)); \
  4556. ZEND_VM_SET_NEXT_OPCODE(opline + 1); \
  4557. } \
  4558. ZEND_VM_CONTINUE(); \
  4559. } while (0)
  4560. #define ZEND_VM_SMART_BRANCH_TRUE_JMPZ() do { \
  4561. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  4562. ZEND_VM_CONTINUE(); \
  4563. } while (0)
  4564. #define ZEND_VM_SMART_BRANCH_TRUE_JMPNZ() do { \
  4565. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  4566. ZEND_VM_CONTINUE(); \
  4567. } while (0)
  4568. #define ZEND_VM_SMART_BRANCH_TRUE_NONE() do { \
  4569. ZVAL_TRUE(EX_VAR(opline->result.var)); \
  4570. ZEND_VM_NEXT_OPCODE(); \
  4571. } while (0)
  4572. #define ZEND_VM_SMART_BRANCH_FALSE() do { \
  4573. if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPNZ|IS_TMP_VAR))) { \
  4574. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  4575. } else if (EXPECTED(opline->result_type == (IS_SMART_BRANCH_JMPZ|IS_TMP_VAR))) { \
  4576. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  4577. } else { \
  4578. ZVAL_FALSE(EX_VAR(opline->result.var)); \
  4579. ZEND_VM_SET_NEXT_OPCODE(opline + 1); \
  4580. } \
  4581. ZEND_VM_CONTINUE(); \
  4582. } while (0)
  4583. #define ZEND_VM_SMART_BRANCH_FALSE_JMPZ() do { \
  4584. ZEND_VM_SET_OPCODE(OP_JMP_ADDR(opline + 1, (opline+1)->op2)); \
  4585. ZEND_VM_CONTINUE(); \
  4586. } while (0)
  4587. #define ZEND_VM_SMART_BRANCH_FALSE_JMPNZ() do { \
  4588. ZEND_VM_SET_NEXT_OPCODE(opline + 2); \
  4589. ZEND_VM_CONTINUE(); \
  4590. } while (0)
  4591. #define ZEND_VM_SMART_BRANCH_FALSE_NONE() do { \
  4592. ZVAL_FALSE(EX_VAR(opline->result.var)); \
  4593. ZEND_VM_NEXT_OPCODE(); \
  4594. } while (0)
  4595. #ifdef __GNUC__
  4596. # define ZEND_VM_GUARD(name) __asm__("#" #name)
  4597. #else
  4598. # define ZEND_VM_GUARD(name)
  4599. #endif
  4600. #define UNDEF_RESULT() do { \
  4601. if (opline->result_type & (IS_VAR | IS_TMP_VAR)) { \
  4602. ZVAL_UNDEF(EX_VAR(opline->result.var)); \
  4603. } \
  4604. } while (0)
  4605. /* This callback disables optimization of "vm_stack_data" variable in VM */
  4606. ZEND_API void (ZEND_FASTCALL *zend_touch_vm_stack_data)(void *vm_stack_data) = NULL;
  4607. #include "zend_vm_execute.h"
  4608. ZEND_API zend_result zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler)
  4609. {
  4610. if (opcode != ZEND_USER_OPCODE) {
  4611. if (handler == NULL) {
  4612. /* restore the original handler */
  4613. zend_user_opcodes[opcode] = opcode;
  4614. } else {
  4615. zend_user_opcodes[opcode] = ZEND_USER_OPCODE;
  4616. }
  4617. zend_user_opcode_handlers[opcode] = handler;
  4618. return SUCCESS;
  4619. }
  4620. return FAILURE;
  4621. }
  4622. ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode)
  4623. {
  4624. return zend_user_opcode_handlers[opcode];
  4625. }
  4626. ZEND_API zval *zend_get_zval_ptr(const zend_op *opline, int op_type, const znode_op *node, const zend_execute_data *execute_data)
  4627. {
  4628. zval *ret;
  4629. switch (op_type) {
  4630. case IS_CONST:
  4631. ret = RT_CONSTANT(opline, *node);
  4632. break;
  4633. case IS_TMP_VAR:
  4634. case IS_VAR:
  4635. case IS_CV:
  4636. ret = EX_VAR(node->var);
  4637. break;
  4638. default:
  4639. ret = NULL;
  4640. break;
  4641. }
  4642. return ret;
  4643. }