zend_API.c 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931
  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. | Andrei Zmievski <andrei@php.net> |
  18. | Dmitry Stogov <dmitry@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. #include "zend.h"
  22. #include "zend_execute.h"
  23. #include "zend_API.h"
  24. #include "zend_modules.h"
  25. #include "zend_extensions.h"
  26. #include "zend_constants.h"
  27. #include "zend_interfaces.h"
  28. #include "zend_exceptions.h"
  29. #include "zend_closures.h"
  30. #include "zend_inheritance.h"
  31. #include "zend_ini.h"
  32. #include <stdarg.h>
  33. /* these variables are true statics/globals, and have to be mutex'ed on every access */
  34. ZEND_API HashTable module_registry;
  35. static zend_module_entry **module_request_startup_handlers;
  36. static zend_module_entry **module_request_shutdown_handlers;
  37. static zend_module_entry **module_post_deactivate_handlers;
  38. static zend_class_entry **class_cleanup_handlers;
  39. ZEND_API zend_result _zend_get_parameters_array_ex(uint32_t param_count, zval *argument_array) /* {{{ */
  40. {
  41. zval *param_ptr;
  42. uint32_t arg_count;
  43. param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
  44. arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  45. if (param_count>arg_count) {
  46. return FAILURE;
  47. }
  48. while (param_count-->0) {
  49. ZVAL_COPY_VALUE(argument_array, param_ptr);
  50. argument_array++;
  51. param_ptr++;
  52. }
  53. return SUCCESS;
  54. }
  55. /* }}} */
  56. ZEND_API zend_result zend_copy_parameters_array(uint32_t param_count, zval *argument_array) /* {{{ */
  57. {
  58. zval *param_ptr;
  59. uint32_t arg_count;
  60. param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
  61. arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  62. if (param_count>arg_count) {
  63. return FAILURE;
  64. }
  65. while (param_count-->0) {
  66. Z_TRY_ADDREF_P(param_ptr);
  67. zend_hash_next_index_insert_new(Z_ARRVAL_P(argument_array), param_ptr);
  68. param_ptr++;
  69. }
  70. return SUCCESS;
  71. }
  72. /* }}} */
  73. ZEND_API ZEND_COLD void zend_wrong_param_count(void) /* {{{ */
  74. {
  75. const char *space;
  76. const char *class_name = get_active_class_name(&space);
  77. zend_argument_count_error("Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name());
  78. }
  79. /* }}} */
  80. /* Argument parsing API -- andrei */
  81. ZEND_API const char *zend_get_type_by_const(int type) /* {{{ */
  82. {
  83. switch(type) {
  84. case IS_FALSE:
  85. case IS_TRUE:
  86. case _IS_BOOL:
  87. return "bool";
  88. case IS_LONG:
  89. return "int";
  90. case IS_DOUBLE:
  91. return "float";
  92. case IS_STRING:
  93. return "string";
  94. case IS_OBJECT:
  95. return "object";
  96. case IS_RESOURCE:
  97. return "resource";
  98. case IS_NULL:
  99. return "null";
  100. case IS_CALLABLE:
  101. return "callable";
  102. case IS_ITERABLE:
  103. return "iterable";
  104. case IS_ARRAY:
  105. return "array";
  106. case IS_VOID:
  107. return "void";
  108. case IS_MIXED:
  109. return "mixed";
  110. case _IS_NUMBER:
  111. return "number";
  112. EMPTY_SWITCH_DEFAULT_CASE()
  113. }
  114. }
  115. /* }}} */
  116. ZEND_API const char *zend_zval_type_name(const zval *arg) /* {{{ */
  117. {
  118. ZVAL_DEREF(arg);
  119. if (Z_ISUNDEF_P(arg)) {
  120. return "null";
  121. }
  122. if (Z_TYPE_P(arg) == IS_OBJECT) {
  123. return ZSTR_VAL(Z_OBJCE_P(arg)->name);
  124. }
  125. return zend_get_type_by_const(Z_TYPE_P(arg));
  126. }
  127. /* }}} */
  128. /* This API exists *only* for use in gettype().
  129. * For anything else, you likely want zend_zval_type_name(). */
  130. ZEND_API zend_string *zend_zval_get_legacy_type(const zval *arg) /* {{{ */
  131. {
  132. switch (Z_TYPE_P(arg)) {
  133. case IS_NULL:
  134. return ZSTR_KNOWN(ZEND_STR_NULL);
  135. case IS_FALSE:
  136. case IS_TRUE:
  137. return ZSTR_KNOWN(ZEND_STR_BOOLEAN);
  138. case IS_LONG:
  139. return ZSTR_KNOWN(ZEND_STR_INTEGER);
  140. case IS_DOUBLE:
  141. return ZSTR_KNOWN(ZEND_STR_DOUBLE);
  142. case IS_STRING:
  143. return ZSTR_KNOWN(ZEND_STR_STRING);
  144. case IS_ARRAY:
  145. return ZSTR_KNOWN(ZEND_STR_ARRAY);
  146. case IS_OBJECT:
  147. return ZSTR_KNOWN(ZEND_STR_OBJECT);
  148. case IS_RESOURCE:
  149. if (zend_rsrc_list_get_rsrc_type(Z_RES_P(arg))) {
  150. return ZSTR_KNOWN(ZEND_STR_RESOURCE);
  151. } else {
  152. return ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE);
  153. }
  154. default:
  155. return NULL;
  156. }
  157. }
  158. /* }}} */
  159. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */
  160. {
  161. int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  162. zend_string *func_name = get_active_function_or_method_name();
  163. zend_argument_count_error("%s() expects exactly 0 arguments, %d given", ZSTR_VAL(func_name), num_args);
  164. zend_string_release(func_name);
  165. }
  166. /* }}} */
  167. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(uint32_t min_num_args, uint32_t max_num_args) /* {{{ */
  168. {
  169. uint32_t num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  170. zend_string *func_name = get_active_function_or_method_name();
  171. zend_argument_count_error(
  172. "%s() expects %s %d argument%s, %d given",
  173. ZSTR_VAL(func_name),
  174. min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
  175. num_args < min_num_args ? min_num_args : max_num_args,
  176. (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
  177. num_args
  178. );
  179. zend_string_release(func_name);
  180. }
  181. /* }}} */
  182. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_error(int error_code, uint32_t num, char *name, zend_expected_type expected_type, zval *arg) /* {{{ */
  183. {
  184. switch (error_code) {
  185. case ZPP_ERROR_WRONG_CALLBACK:
  186. zend_wrong_callback_error(num, name);
  187. break;
  188. case ZPP_ERROR_WRONG_CALLBACK_OR_NULL:
  189. zend_wrong_callback_or_null_error(num, name);
  190. break;
  191. case ZPP_ERROR_WRONG_CLASS:
  192. zend_wrong_parameter_class_error(num, name, arg);
  193. break;
  194. case ZPP_ERROR_WRONG_CLASS_OR_NULL:
  195. zend_wrong_parameter_class_or_null_error(num, name, arg);
  196. break;
  197. case ZPP_ERROR_WRONG_CLASS_OR_STRING:
  198. zend_wrong_parameter_class_or_string_error(num, name, arg);
  199. break;
  200. case ZPP_ERROR_WRONG_CLASS_OR_STRING_OR_NULL:
  201. zend_wrong_parameter_class_or_string_or_null_error(num, name, arg);
  202. break;
  203. case ZPP_ERROR_WRONG_CLASS_OR_LONG:
  204. zend_wrong_parameter_class_or_long_error(num, name, arg);
  205. break;
  206. case ZPP_ERROR_WRONG_CLASS_OR_LONG_OR_NULL:
  207. zend_wrong_parameter_class_or_long_or_null_error(num, name, arg);
  208. break;
  209. case ZPP_ERROR_WRONG_ARG:
  210. zend_wrong_parameter_type_error(num, expected_type, arg);
  211. break;
  212. case ZPP_ERROR_UNEXPECTED_EXTRA_NAMED:
  213. zend_unexpected_extra_named_error();
  214. break;
  215. case ZPP_ERROR_FAILURE:
  216. ZEND_ASSERT(EG(exception) && "Should have produced an error already");
  217. break;
  218. EMPTY_SWITCH_DEFAULT_CASE()
  219. }
  220. }
  221. /* }}} */
  222. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(uint32_t num, zend_expected_type expected_type, zval *arg) /* {{{ */
  223. {
  224. static const char * const expected_error[] = {
  225. Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
  226. NULL
  227. };
  228. if (EG(exception)) {
  229. return;
  230. }
  231. if ((expected_type == Z_EXPECTED_PATH || expected_type == Z_EXPECTED_PATH_OR_NULL)
  232. && Z_TYPE_P(arg) == IS_STRING) {
  233. zend_argument_value_error(num, "must not contain any null bytes");
  234. return;
  235. }
  236. zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_type_name(arg));
  237. }
  238. /* }}} */
  239. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(uint32_t num, const char *name, zval *arg) /* {{{ */
  240. {
  241. if (EG(exception)) {
  242. return;
  243. }
  244. zend_argument_type_error(num, "must be of type %s, %s given", name, zend_zval_type_name(arg));
  245. }
  246. /* }}} */
  247. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */
  248. {
  249. if (EG(exception)) {
  250. return;
  251. }
  252. zend_argument_type_error(num, "must be of type ?%s, %s given", name, zend_zval_type_name(arg));
  253. }
  254. /* }}} */
  255. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_error(uint32_t num, const char *name, zval *arg) /* {{{ */
  256. {
  257. if (EG(exception)) {
  258. return;
  259. }
  260. zend_argument_type_error(num, "must be of type %s|int, %s given", name, zend_zval_type_name(arg));
  261. }
  262. /* }}} */
  263. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_long_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */
  264. {
  265. if (EG(exception)) {
  266. return;
  267. }
  268. zend_argument_type_error(num, "must be of type %s|int|null, %s given", name, zend_zval_type_name(arg));
  269. }
  270. /* }}} */
  271. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_error(uint32_t num, const char *name, zval *arg) /* {{{ */
  272. {
  273. if (EG(exception)) {
  274. return;
  275. }
  276. zend_argument_type_error(num, "must be of type %s|string, %s given", name, zend_zval_type_name(arg));
  277. }
  278. /* }}} */
  279. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_string_or_null_error(uint32_t num, const char *name, zval *arg) /* {{{ */
  280. {
  281. if (EG(exception)) {
  282. return;
  283. }
  284. zend_argument_type_error(num, "must be of type %s|string|null, %s given", name, zend_zval_type_name(arg));
  285. }
  286. /* }}} */
  287. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(uint32_t num, char *error) /* {{{ */
  288. {
  289. if (!EG(exception)) {
  290. zend_argument_type_error(num, "must be a valid callback, %s", error);
  291. }
  292. efree(error);
  293. }
  294. /* }}} */
  295. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_or_null_error(uint32_t num, char *error) /* {{{ */
  296. {
  297. if (!EG(exception)) {
  298. zend_argument_type_error(num, "must be a valid callback or null, %s", error);
  299. }
  300. efree(error);
  301. }
  302. /* }}} */
  303. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_unexpected_extra_named_error(void)
  304. {
  305. const char *space;
  306. const char *class_name = get_active_class_name(&space);
  307. zend_argument_count_error("%s%s%s() does not accept unknown named parameters",
  308. class_name, space, get_active_function_name());
  309. }
  310. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_error_variadic(zend_class_entry *error_ce, uint32_t arg_num, const char *format, va_list va) /* {{{ */
  311. {
  312. zend_string *func_name;
  313. const char *arg_name;
  314. char *message = NULL;
  315. if (EG(exception)) {
  316. return;
  317. }
  318. func_name = get_active_function_or_method_name();
  319. arg_name = get_active_function_arg_name(arg_num);
  320. zend_vspprintf(&message, 0, format, va);
  321. zend_throw_error(error_ce, "%s(): Argument #%d%s%s%s %s",
  322. ZSTR_VAL(func_name), arg_num,
  323. arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "", message
  324. );
  325. efree(message);
  326. zend_string_release(func_name);
  327. }
  328. /* }}} */
  329. ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */
  330. {
  331. va_list va;
  332. va_start(va, format);
  333. zend_argument_error_variadic(error_ce, arg_num, format, va);
  334. va_end(va);
  335. }
  336. /* }}} */
  337. ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format, ...) /* {{{ */
  338. {
  339. va_list va;
  340. va_start(va, format);
  341. zend_argument_error_variadic(zend_ce_type_error, arg_num, format, va);
  342. va_end(va);
  343. }
  344. /* }}} */
  345. ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format, ...) /* {{{ */
  346. {
  347. va_list va;
  348. va_start(va, format);
  349. zend_argument_error_variadic(zend_ce_value_error, arg_num, format, va);
  350. va_end(va);
  351. }
  352. /* }}} */
  353. ZEND_API bool ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, uint32_t num, bool check_null) /* {{{ */
  354. {
  355. zend_class_entry *ce_base = *pce;
  356. if (check_null && Z_TYPE_P(arg) == IS_NULL) {
  357. *pce = NULL;
  358. return 1;
  359. }
  360. if (!try_convert_to_string(arg)) {
  361. *pce = NULL;
  362. return 0;
  363. }
  364. *pce = zend_lookup_class(Z_STR_P(arg));
  365. if (ce_base) {
  366. if ((!*pce || !instanceof_function(*pce, ce_base))) {
  367. zend_argument_type_error(num, "must be a class name derived from %s, %s given", ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
  368. *pce = NULL;
  369. return 0;
  370. }
  371. }
  372. if (!*pce) {
  373. zend_argument_type_error(num, "must be a valid class name, %s given", Z_STRVAL_P(arg));
  374. return 0;
  375. }
  376. return 1;
  377. }
  378. /* }}} */
  379. static ZEND_COLD bool zend_null_arg_deprecated(const char *fallback_type, uint32_t arg_num) {
  380. zend_function *func = EG(current_execute_data)->func;
  381. ZEND_ASSERT(arg_num > 0);
  382. uint32_t arg_offset = arg_num - 1;
  383. if (arg_offset >= func->common.num_args) {
  384. ZEND_ASSERT(func->common.fn_flags & ZEND_ACC_VARIADIC);
  385. arg_offset = func->common.num_args;
  386. }
  387. zend_arg_info *arg_info = &func->common.arg_info[arg_offset];
  388. zend_string *func_name = get_active_function_or_method_name();
  389. const char *arg_name = get_active_function_arg_name(arg_num);
  390. /* If no type is specified in arginfo, use the specified fallback_type determined through
  391. * zend_parse_parameters instead. */
  392. zend_string *type_str = zend_type_to_string(arg_info->type);
  393. const char *type = type_str ? ZSTR_VAL(type_str) : fallback_type;
  394. zend_error(E_DEPRECATED,
  395. "%s(): Passing null to parameter #%" PRIu32 "%s%s%s of type %s is deprecated",
  396. ZSTR_VAL(func_name), arg_num,
  397. arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "",
  398. type);
  399. zend_string_release(func_name);
  400. if (type_str) {
  401. zend_string_release(type_str);
  402. }
  403. return !EG(exception);
  404. }
  405. ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_weak(zval *arg, bool *dest, uint32_t arg_num) /* {{{ */
  406. {
  407. if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
  408. if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("bool", arg_num)) {
  409. return 0;
  410. }
  411. *dest = zend_is_true(arg);
  412. } else {
  413. return 0;
  414. }
  415. return 1;
  416. }
  417. /* }}} */
  418. ZEND_API bool ZEND_FASTCALL zend_parse_arg_bool_slow(zval *arg, bool *dest, uint32_t arg_num) /* {{{ */
  419. {
  420. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  421. return 0;
  422. }
  423. return zend_parse_arg_bool_weak(arg, dest, arg_num);
  424. }
  425. /* }}} */
  426. ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_weak(zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */
  427. {
  428. if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
  429. if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
  430. return 0;
  431. }
  432. if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
  433. return 0;
  434. } else {
  435. zend_long lval = zend_dval_to_lval(Z_DVAL_P(arg));
  436. if (UNEXPECTED(!zend_is_long_compatible(Z_DVAL_P(arg), lval))) {
  437. /* Check arg_num is not (uint32_t)-1, as otherwise its called by
  438. * zend_verify_weak_scalar_type_hint_no_sideeffect() */
  439. if (arg_num != (uint32_t)-1) {
  440. zend_incompatible_double_to_long_error(Z_DVAL_P(arg));
  441. }
  442. if (UNEXPECTED(EG(exception))) {
  443. return 0;
  444. }
  445. }
  446. *dest = lval;
  447. }
  448. } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
  449. double d;
  450. zend_uchar type;
  451. if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
  452. if (EXPECTED(type != 0)) {
  453. zend_long lval;
  454. if (UNEXPECTED(zend_isnan(d))) {
  455. return 0;
  456. }
  457. if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
  458. return 0;
  459. }
  460. lval = zend_dval_to_lval(d);
  461. /* This only checks for a fractional part as if doesn't fit it already throws a TypeError */
  462. if (UNEXPECTED(!zend_is_long_compatible(d, lval))) {
  463. /* Check arg_num is not (uint32_t)-1, as otherwise its called by
  464. * zend_verify_weak_scalar_type_hint_no_sideeffect() */
  465. if (arg_num != (uint32_t)-1) {
  466. zend_incompatible_string_to_long_error(Z_STR_P(arg));
  467. }
  468. if (UNEXPECTED(EG(exception))) {
  469. return 0;
  470. }
  471. }
  472. *dest = lval;
  473. } else {
  474. return 0;
  475. }
  476. }
  477. if (UNEXPECTED(EG(exception))) {
  478. return 0;
  479. }
  480. } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
  481. if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("int", arg_num)) {
  482. return 0;
  483. }
  484. *dest = 0;
  485. } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
  486. *dest = 1;
  487. } else {
  488. return 0;
  489. }
  490. return 1;
  491. }
  492. /* }}} */
  493. ZEND_API bool ZEND_FASTCALL zend_parse_arg_long_slow(zval *arg, zend_long *dest, uint32_t arg_num) /* {{{ */
  494. {
  495. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  496. return 0;
  497. }
  498. return zend_parse_arg_long_weak(arg, dest, arg_num);
  499. }
  500. /* }}} */
  501. ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_weak(zval *arg, double *dest, uint32_t arg_num) /* {{{ */
  502. {
  503. if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
  504. *dest = (double)Z_LVAL_P(arg);
  505. } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
  506. zend_long l;
  507. zend_uchar type;
  508. if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) {
  509. if (EXPECTED(type != 0)) {
  510. *dest = (double)(l);
  511. } else {
  512. return 0;
  513. }
  514. }
  515. if (UNEXPECTED(EG(exception))) {
  516. return 0;
  517. }
  518. } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
  519. if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("float", arg_num)) {
  520. return 0;
  521. }
  522. *dest = 0.0;
  523. } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
  524. *dest = 1.0;
  525. } else {
  526. return 0;
  527. }
  528. return 1;
  529. }
  530. /* }}} */
  531. ZEND_API bool ZEND_FASTCALL zend_parse_arg_double_slow(zval *arg, double *dest, uint32_t arg_num) /* {{{ */
  532. {
  533. if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
  534. /* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
  535. *dest = (double)Z_LVAL_P(arg);
  536. } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  537. return 0;
  538. }
  539. return zend_parse_arg_double_weak(arg, dest, arg_num);
  540. }
  541. /* }}} */
  542. ZEND_API bool ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest, uint32_t arg_num) /* {{{ */
  543. {
  544. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  545. return 0;
  546. }
  547. if (Z_TYPE_P(arg) == IS_STRING) {
  548. zend_string *str = Z_STR_P(arg);
  549. zend_long lval;
  550. double dval;
  551. zend_uchar type = is_numeric_str_function(str, &lval, &dval);
  552. if (type == IS_LONG) {
  553. ZVAL_LONG(arg, lval);
  554. } else if (type == IS_DOUBLE) {
  555. ZVAL_DOUBLE(arg, dval);
  556. } else {
  557. return 0;
  558. }
  559. zend_string_release(str);
  560. } else if (Z_TYPE_P(arg) < IS_TRUE) {
  561. if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("int|float", arg_num)) {
  562. return 0;
  563. }
  564. ZVAL_LONG(arg, 0);
  565. } else if (Z_TYPE_P(arg) == IS_TRUE) {
  566. ZVAL_LONG(arg, 1);
  567. } else {
  568. return 0;
  569. }
  570. *dest = arg;
  571. return 1;
  572. }
  573. /* }}} */
  574. ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest, uint32_t arg_num) /* {{{ */
  575. {
  576. if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
  577. if (UNEXPECTED(Z_TYPE_P(arg) == IS_NULL) && !zend_null_arg_deprecated("string", arg_num)) {
  578. return 0;
  579. }
  580. convert_to_string(arg);
  581. *dest = Z_STR_P(arg);
  582. } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
  583. zend_object *zobj = Z_OBJ_P(arg);
  584. zval obj;
  585. if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
  586. OBJ_RELEASE(zobj);
  587. ZVAL_COPY_VALUE(arg, &obj);
  588. *dest = Z_STR_P(arg);
  589. return 1;
  590. }
  591. return 0;
  592. } else {
  593. return 0;
  594. }
  595. return 1;
  596. }
  597. /* }}} */
  598. ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest, uint32_t arg_num) /* {{{ */
  599. {
  600. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  601. return 0;
  602. }
  603. return zend_parse_arg_str_weak(arg, dest, arg_num);
  604. }
  605. /* }}} */
  606. ZEND_API bool ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long, uint32_t arg_num) /* {{{ */
  607. {
  608. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  609. return 0;
  610. }
  611. if (zend_parse_arg_long_weak(arg, dest_long, arg_num)) {
  612. *dest_str = NULL;
  613. return 1;
  614. } else if (zend_parse_arg_str_weak(arg, dest_str, arg_num)) {
  615. *dest_long = 0;
  616. return 1;
  617. } else {
  618. return 0;
  619. }
  620. }
  621. /* }}} */
  622. static const char *zend_parse_arg_impl(zval *arg, va_list *va, const char **spec, char **error, uint32_t arg_num) /* {{{ */
  623. {
  624. const char *spec_walk = *spec;
  625. char c = *spec_walk++;
  626. bool check_null = 0;
  627. bool separate = 0;
  628. zval *real_arg = arg;
  629. /* scan through modifiers */
  630. ZVAL_DEREF(arg);
  631. while (1) {
  632. if (*spec_walk == '/') {
  633. SEPARATE_ZVAL_NOREF(arg);
  634. real_arg = arg;
  635. separate = 1;
  636. } else if (*spec_walk == '!') {
  637. check_null = 1;
  638. } else {
  639. break;
  640. }
  641. spec_walk++;
  642. }
  643. switch (c) {
  644. case 'l':
  645. {
  646. zend_long *p = va_arg(*va, zend_long *);
  647. bool *is_null = NULL;
  648. if (check_null) {
  649. is_null = va_arg(*va, bool *);
  650. }
  651. if (!zend_parse_arg_long(arg, p, is_null, check_null, arg_num)) {
  652. return check_null ? "?int" : "int";
  653. }
  654. }
  655. break;
  656. case 'd':
  657. {
  658. double *p = va_arg(*va, double *);
  659. bool *is_null = NULL;
  660. if (check_null) {
  661. is_null = va_arg(*va, bool *);
  662. }
  663. if (!zend_parse_arg_double(arg, p, is_null, check_null, arg_num)) {
  664. return check_null ? "?float" : "float";
  665. }
  666. }
  667. break;
  668. case 'n':
  669. {
  670. zval **p = va_arg(*va, zval **);
  671. if (!zend_parse_arg_number(arg, p, check_null, arg_num)) {
  672. return check_null ? "int|float|null" : "int|float";
  673. }
  674. }
  675. break;
  676. case 's':
  677. {
  678. char **p = va_arg(*va, char **);
  679. size_t *pl = va_arg(*va, size_t *);
  680. if (!zend_parse_arg_string(arg, p, pl, check_null, arg_num)) {
  681. return check_null ? "?string" : "string";
  682. }
  683. }
  684. break;
  685. case 'p':
  686. {
  687. char **p = va_arg(*va, char **);
  688. size_t *pl = va_arg(*va, size_t *);
  689. if (!zend_parse_arg_path(arg, p, pl, check_null, arg_num)) {
  690. if (Z_TYPE_P(arg) == IS_STRING) {
  691. zend_spprintf(error, 0, "must not contain any null bytes");
  692. return "";
  693. } else {
  694. return check_null ? "?string" : "string";
  695. }
  696. }
  697. }
  698. break;
  699. case 'P':
  700. {
  701. zend_string **str = va_arg(*va, zend_string **);
  702. if (!zend_parse_arg_path_str(arg, str, check_null, arg_num)) {
  703. if (Z_TYPE_P(arg) == IS_STRING) {
  704. zend_spprintf(error, 0, "must not contain any null bytes");
  705. return "";
  706. } else {
  707. return check_null ? "?string" : "string";
  708. }
  709. }
  710. }
  711. break;
  712. case 'S':
  713. {
  714. zend_string **str = va_arg(*va, zend_string **);
  715. if (!zend_parse_arg_str(arg, str, check_null, arg_num)) {
  716. return check_null ? "?string" : "string";
  717. }
  718. }
  719. break;
  720. case 'b':
  721. {
  722. bool *p = va_arg(*va, bool *);
  723. bool *is_null = NULL;
  724. if (check_null) {
  725. is_null = va_arg(*va, bool *);
  726. }
  727. if (!zend_parse_arg_bool(arg, p, is_null, check_null, arg_num)) {
  728. return check_null ? "?bool" : "bool";
  729. }
  730. }
  731. break;
  732. case 'r':
  733. {
  734. zval **p = va_arg(*va, zval **);
  735. if (!zend_parse_arg_resource(arg, p, check_null)) {
  736. return check_null ? "resource or null" : "resource";
  737. }
  738. }
  739. break;
  740. case 'A':
  741. case 'a':
  742. {
  743. zval **p = va_arg(*va, zval **);
  744. if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) {
  745. return check_null ? "?array" : "array";
  746. }
  747. }
  748. break;
  749. case 'H':
  750. case 'h':
  751. {
  752. HashTable **p = va_arg(*va, HashTable **);
  753. if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H', separate)) {
  754. return check_null ? "?array" : "array";
  755. }
  756. }
  757. break;
  758. case 'o':
  759. {
  760. zval **p = va_arg(*va, zval **);
  761. if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
  762. return check_null ? "?object" : "object";
  763. }
  764. }
  765. break;
  766. case 'O':
  767. {
  768. zval **p = va_arg(*va, zval **);
  769. zend_class_entry *ce = va_arg(*va, zend_class_entry *);
  770. if (!zend_parse_arg_object(arg, p, ce, check_null)) {
  771. if (ce) {
  772. if (check_null) {
  773. zend_spprintf(error, 0, "must be of type ?%s, %s given", ZSTR_VAL(ce->name), zend_zval_type_name(arg));
  774. return "";
  775. } else {
  776. return ZSTR_VAL(ce->name);
  777. }
  778. } else {
  779. return check_null ? "?object" : "object";
  780. }
  781. }
  782. }
  783. break;
  784. case 'C':
  785. {
  786. zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
  787. zend_class_entry *ce_base = *pce;
  788. if (check_null && Z_TYPE_P(arg) == IS_NULL) {
  789. *pce = NULL;
  790. break;
  791. }
  792. if (!try_convert_to_string(arg)) {
  793. *pce = NULL;
  794. return ""; /* try_convert_to_string() throws an exception */
  795. }
  796. if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
  797. *pce = NULL;
  798. } else {
  799. *pce = lookup;
  800. }
  801. if (ce_base) {
  802. if ((!*pce || !instanceof_function(*pce, ce_base))) {
  803. zend_spprintf(error, 0, "must be a class name derived from %s%s, %s given",
  804. ZSTR_VAL(ce_base->name), check_null ? " or null" : "", Z_STRVAL_P(arg));
  805. *pce = NULL;
  806. return "";
  807. }
  808. }
  809. if (!*pce) {
  810. zend_spprintf(error, 0, "must be a valid class name%s, %s given",
  811. check_null ? " or null" : "", Z_STRVAL_P(arg));
  812. return "";
  813. }
  814. break;
  815. }
  816. break;
  817. case 'f':
  818. {
  819. zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
  820. zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
  821. char *is_callable_error = NULL;
  822. if (check_null && Z_TYPE_P(arg) == IS_NULL) {
  823. fci->size = 0;
  824. fcc->function_handler = 0;
  825. break;
  826. }
  827. if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
  828. ZEND_ASSERT(!is_callable_error);
  829. /* Release call trampolines: The function may not get called, in which case
  830. * the trampoline will leak. Force it to be refetched during
  831. * zend_call_function instead. */
  832. zend_release_fcall_info_cache(fcc);
  833. break;
  834. }
  835. if (is_callable_error) {
  836. zend_spprintf(error, 0, "must be a valid callback%s, %s", check_null ? " or null" : "", is_callable_error);
  837. efree(is_callable_error);
  838. return "";
  839. } else {
  840. return check_null ? "a valid callback or null" : "a valid callback";
  841. }
  842. }
  843. case 'z':
  844. {
  845. zval **p = va_arg(*va, zval **);
  846. zend_parse_arg_zval_deref(real_arg, p, check_null);
  847. }
  848. break;
  849. case 'Z': /* replace with 'z' */
  850. case 'L': /* replace with 'l' */
  851. ZEND_ASSERT(0 && "ZPP modifier no longer supported");
  852. ZEND_FALLTHROUGH;
  853. default:
  854. return "unknown";
  855. }
  856. *spec = spec_walk;
  857. return NULL;
  858. }
  859. /* }}} */
  860. static zend_result zend_parse_arg(uint32_t arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
  861. {
  862. const char *expected_type = NULL;
  863. char *error = NULL;
  864. expected_type = zend_parse_arg_impl(arg, va, spec, &error, arg_num);
  865. if (expected_type) {
  866. if (EG(exception)) {
  867. return FAILURE;
  868. }
  869. if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
  870. if (error) {
  871. if (strcmp(error, "must not contain any null bytes") == 0) {
  872. zend_argument_value_error(arg_num, "%s", error);
  873. } else {
  874. zend_argument_type_error(arg_num, "%s", error);
  875. }
  876. efree(error);
  877. } else {
  878. zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_type_name(arg));
  879. }
  880. } else if (error) {
  881. efree(error);
  882. }
  883. return FAILURE;
  884. }
  885. return SUCCESS;
  886. }
  887. /* }}} */
  888. ZEND_API zend_result zend_parse_parameter(int flags, uint32_t arg_num, zval *arg, const char *spec, ...)
  889. {
  890. va_list va;
  891. zend_result ret;
  892. va_start(va, spec);
  893. ret = zend_parse_arg(arg_num, arg, &va, &spec, flags);
  894. va_end(va);
  895. return ret;
  896. }
  897. static ZEND_COLD void zend_parse_parameters_debug_error(const char *msg) {
  898. zend_function *active_function = EG(current_execute_data)->func;
  899. const char *class_name = active_function->common.scope
  900. ? ZSTR_VAL(active_function->common.scope->name) : "";
  901. zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s",
  902. class_name, class_name[0] ? "::" : "",
  903. ZSTR_VAL(active_function->common.function_name), msg);
  904. }
  905. static zend_result zend_parse_va_args(uint32_t num_args, const char *type_spec, va_list *va, int flags) /* {{{ */
  906. {
  907. const char *spec_walk;
  908. char c;
  909. uint32_t i;
  910. uint32_t min_num_args = 0;
  911. uint32_t max_num_args = 0;
  912. uint32_t post_varargs = 0;
  913. zval *arg;
  914. bool have_varargs = 0;
  915. bool have_optional_args = 0;
  916. zval **varargs = NULL;
  917. uint32_t *n_varargs = NULL;
  918. for (spec_walk = type_spec; *spec_walk; spec_walk++) {
  919. c = *spec_walk;
  920. switch (c) {
  921. case 'l': case 'd':
  922. case 's': case 'b':
  923. case 'r': case 'a':
  924. case 'o': case 'O':
  925. case 'z': case 'Z':
  926. case 'C': case 'h':
  927. case 'f': case 'A':
  928. case 'H': case 'p':
  929. case 'S': case 'P':
  930. case 'L': case 'n':
  931. max_num_args++;
  932. break;
  933. case '|':
  934. min_num_args = max_num_args;
  935. have_optional_args = 1;
  936. break;
  937. case '/':
  938. case '!':
  939. /* Pass */
  940. break;
  941. case '*':
  942. case '+':
  943. if (have_varargs) {
  944. zend_parse_parameters_debug_error(
  945. "only one varargs specifier (* or +) is permitted");
  946. return FAILURE;
  947. }
  948. have_varargs = 1;
  949. /* we expect at least one parameter in varargs */
  950. if (c == '+') {
  951. max_num_args++;
  952. }
  953. /* mark the beginning of varargs */
  954. post_varargs = max_num_args;
  955. if (ZEND_CALL_INFO(EG(current_execute_data)) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
  956. zend_unexpected_extra_named_error();
  957. return FAILURE;
  958. }
  959. break;
  960. default:
  961. zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
  962. return FAILURE;
  963. }
  964. }
  965. /* with no optional arguments the minimum number of arguments must be the same as the maximum */
  966. if (!have_optional_args) {
  967. min_num_args = max_num_args;
  968. }
  969. if (have_varargs) {
  970. /* calculate how many required args are at the end of the specifier list */
  971. post_varargs = max_num_args - post_varargs;
  972. max_num_args = UINT32_MAX;
  973. }
  974. if (num_args < min_num_args || num_args > max_num_args) {
  975. if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
  976. zend_string *func_name = get_active_function_or_method_name();
  977. zend_argument_count_error("%s() expects %s %d argument%s, %d given",
  978. ZSTR_VAL(func_name),
  979. min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
  980. num_args < min_num_args ? min_num_args : max_num_args,
  981. (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
  982. num_args
  983. );
  984. zend_string_release(func_name);
  985. }
  986. return FAILURE;
  987. }
  988. if (num_args > ZEND_CALL_NUM_ARGS(EG(current_execute_data))) {
  989. zend_parse_parameters_debug_error("could not obtain parameters for parsing");
  990. return FAILURE;
  991. }
  992. i = 0;
  993. while (num_args-- > 0) {
  994. if (*type_spec == '|') {
  995. type_spec++;
  996. }
  997. if (*type_spec == '*' || *type_spec == '+') {
  998. uint32_t num_varargs = num_args + 1 - post_varargs;
  999. /* eat up the passed in storage even if it won't be filled in with varargs */
  1000. varargs = va_arg(*va, zval **);
  1001. n_varargs = va_arg(*va, uint32_t *);
  1002. type_spec++;
  1003. if (num_varargs > 0) {
  1004. *n_varargs = num_varargs;
  1005. *varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
  1006. /* adjust how many args we have left and restart loop */
  1007. num_args += 1 - num_varargs;
  1008. i += num_varargs;
  1009. continue;
  1010. } else {
  1011. *varargs = NULL;
  1012. *n_varargs = 0;
  1013. }
  1014. }
  1015. arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
  1016. if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
  1017. /* clean up varargs array if it was used */
  1018. if (varargs && *varargs) {
  1019. *varargs = NULL;
  1020. }
  1021. return FAILURE;
  1022. }
  1023. i++;
  1024. }
  1025. return SUCCESS;
  1026. }
  1027. /* }}} */
  1028. ZEND_API zend_result zend_parse_parameters_ex(int flags, uint32_t num_args, const char *type_spec, ...) /* {{{ */
  1029. {
  1030. va_list va;
  1031. zend_result retval;
  1032. va_start(va, type_spec);
  1033. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  1034. va_end(va);
  1035. return retval;
  1036. }
  1037. /* }}} */
  1038. ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec, ...) /* {{{ */
  1039. {
  1040. va_list va;
  1041. zend_result retval;
  1042. int flags = 0;
  1043. va_start(va, type_spec);
  1044. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  1045. va_end(va);
  1046. return retval;
  1047. }
  1048. /* }}} */
  1049. ZEND_API zend_result zend_parse_method_parameters(uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
  1050. {
  1051. va_list va;
  1052. zend_result retval;
  1053. int flags = 0;
  1054. const char *p = type_spec;
  1055. zval **object;
  1056. zend_class_entry *ce;
  1057. /* Just checking this_ptr is not enough, because fcall_common_helper does not set
  1058. * Z_OBJ(EG(This)) to NULL when calling an internal function with common.scope == NULL.
  1059. * In that case EG(This) would still be the $this from the calling code and we'd take the
  1060. * wrong branch here. */
  1061. bool is_method = EG(current_execute_data)->func->common.scope != NULL;
  1062. if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) {
  1063. va_start(va, type_spec);
  1064. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  1065. va_end(va);
  1066. } else {
  1067. p++;
  1068. va_start(va, type_spec);
  1069. object = va_arg(va, zval **);
  1070. ce = va_arg(va, zend_class_entry *);
  1071. *object = this_ptr;
  1072. if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
  1073. zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s()",
  1074. ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name(), ZSTR_VAL(ce->name), get_active_function_name());
  1075. }
  1076. retval = zend_parse_va_args(num_args, p, &va, flags);
  1077. va_end(va);
  1078. }
  1079. return retval;
  1080. }
  1081. /* }}} */
  1082. ZEND_API zend_result zend_parse_method_parameters_ex(int flags, uint32_t num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
  1083. {
  1084. va_list va;
  1085. zend_result retval;
  1086. const char *p = type_spec;
  1087. zval **object;
  1088. zend_class_entry *ce;
  1089. if (!this_ptr) {
  1090. va_start(va, type_spec);
  1091. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  1092. va_end(va);
  1093. } else {
  1094. p++;
  1095. va_start(va, type_spec);
  1096. object = va_arg(va, zval **);
  1097. ce = va_arg(va, zend_class_entry *);
  1098. *object = this_ptr;
  1099. if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
  1100. if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
  1101. zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s()",
  1102. ZSTR_VAL(ce->name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name());
  1103. }
  1104. va_end(va);
  1105. return FAILURE;
  1106. }
  1107. retval = zend_parse_va_args(num_args, p, &va, flags);
  1108. va_end(va);
  1109. }
  1110. return retval;
  1111. }
  1112. /* }}} */
  1113. /* This function should be called after the constructor has been called
  1114. * because it may call __set from the uninitialized object otherwise. */
  1115. ZEND_API void zend_merge_properties(zval *obj, HashTable *properties) /* {{{ */
  1116. {
  1117. zend_object *zobj = Z_OBJ_P(obj);
  1118. zend_object_write_property_t write_property = zobj->handlers->write_property;
  1119. zend_class_entry *old_scope = EG(fake_scope);
  1120. zend_string *key;
  1121. zval *value;
  1122. EG(fake_scope) = Z_OBJCE_P(obj);
  1123. ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, value) {
  1124. if (key) {
  1125. write_property(zobj, key, value, NULL);
  1126. }
  1127. } ZEND_HASH_FOREACH_END();
  1128. EG(fake_scope) = old_scope;
  1129. }
  1130. /* }}} */
  1131. static zend_class_mutable_data *zend_allocate_mutable_data(zend_class_entry *class_type) /* {{{ */
  1132. {
  1133. zend_class_mutable_data *mutable_data;
  1134. ZEND_ASSERT(ZEND_MAP_PTR(class_type->mutable_data) != NULL);
  1135. ZEND_ASSERT(ZEND_MAP_PTR_GET_IMM(class_type->mutable_data) == NULL);
  1136. mutable_data = zend_arena_alloc(&CG(arena), sizeof(zend_class_mutable_data));
  1137. memset(mutable_data, 0, sizeof(zend_class_mutable_data));
  1138. mutable_data->ce_flags = class_type->ce_flags;
  1139. ZEND_MAP_PTR_SET_IMM(class_type->mutable_data, mutable_data);
  1140. return mutable_data;
  1141. }
  1142. /* }}} */
  1143. ZEND_API HashTable *zend_separate_class_constants_table(zend_class_entry *class_type) /* {{{ */
  1144. {
  1145. zend_class_mutable_data *mutable_data;
  1146. HashTable *constants_table;
  1147. zend_string *key;
  1148. zend_class_constant *new_c, *c;
  1149. constants_table = zend_arena_alloc(&CG(arena), sizeof(HashTable));
  1150. zend_hash_init(constants_table, zend_hash_num_elements(&class_type->constants_table), NULL, NULL, 0);
  1151. zend_hash_extend(constants_table, zend_hash_num_elements(&class_type->constants_table), 0);
  1152. ZEND_HASH_FOREACH_STR_KEY_PTR(&class_type->constants_table, key, c) {
  1153. if (c->ce == class_type) {
  1154. if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
  1155. new_c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
  1156. memcpy(new_c, c, sizeof(zend_class_constant));
  1157. c = new_c;
  1158. }
  1159. Z_TRY_ADDREF(c->value);
  1160. } else {
  1161. if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
  1162. c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), key);
  1163. ZEND_ASSERT(c);
  1164. }
  1165. }
  1166. _zend_hash_append_ptr(constants_table, key, c);
  1167. } ZEND_HASH_FOREACH_END();
  1168. ZEND_ASSERT(ZEND_MAP_PTR(class_type->mutable_data) != NULL);
  1169. mutable_data = ZEND_MAP_PTR_GET_IMM(class_type->mutable_data);
  1170. if (!mutable_data) {
  1171. mutable_data = zend_allocate_mutable_data(class_type);
  1172. }
  1173. mutable_data->constants_table = constants_table;
  1174. return constants_table;
  1175. }
  1176. static zend_result update_property(zval *val, zend_property_info *prop_info) {
  1177. if (ZEND_TYPE_IS_SET(prop_info->type)) {
  1178. zval tmp;
  1179. ZVAL_COPY(&tmp, val);
  1180. if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) {
  1181. zval_ptr_dtor(&tmp);
  1182. return FAILURE;
  1183. }
  1184. /* property initializers must always be evaluated with strict types */;
  1185. if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) {
  1186. zval_ptr_dtor(&tmp);
  1187. return FAILURE;
  1188. }
  1189. zval_ptr_dtor(val);
  1190. ZVAL_COPY_VALUE(val, &tmp);
  1191. return SUCCESS;
  1192. }
  1193. return zval_update_constant_ex(val, prop_info->ce);
  1194. }
  1195. ZEND_API zend_result zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
  1196. {
  1197. zend_class_mutable_data *mutable_data = NULL;
  1198. zval *default_properties_table = NULL;
  1199. zval *static_members_table = NULL;
  1200. zend_class_constant *c;
  1201. zval *val;
  1202. uint32_t ce_flags;
  1203. ce_flags = class_type->ce_flags;
  1204. if (ce_flags & ZEND_ACC_CONSTANTS_UPDATED) {
  1205. return SUCCESS;
  1206. }
  1207. bool uses_mutable_data = ZEND_MAP_PTR(class_type->mutable_data) != NULL;
  1208. if (uses_mutable_data) {
  1209. mutable_data = ZEND_MAP_PTR_GET_IMM(class_type->mutable_data);
  1210. if (mutable_data) {
  1211. ce_flags = mutable_data->ce_flags;
  1212. if (ce_flags & ZEND_ACC_CONSTANTS_UPDATED) {
  1213. return SUCCESS;
  1214. }
  1215. } else {
  1216. mutable_data = zend_allocate_mutable_data(class_type);
  1217. }
  1218. }
  1219. if (class_type->parent) {
  1220. if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
  1221. return FAILURE;
  1222. }
  1223. }
  1224. if (ce_flags & ZEND_ACC_HAS_AST_CONSTANTS) {
  1225. HashTable *constants_table;
  1226. if (uses_mutable_data) {
  1227. constants_table = mutable_data->constants_table;
  1228. if (!constants_table) {
  1229. constants_table = zend_separate_class_constants_table(class_type);
  1230. }
  1231. } else {
  1232. constants_table = &class_type->constants_table;
  1233. }
  1234. zend_string *name;
  1235. ZEND_HASH_FOREACH_STR_KEY_VAL(constants_table, name, val) {
  1236. c = Z_PTR_P(val);
  1237. if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
  1238. if (c->ce != class_type) {
  1239. Z_PTR_P(val) = c = zend_hash_find_ptr(CE_CONSTANTS_TABLE(c->ce), name);
  1240. if (Z_TYPE(c->value) != IS_CONSTANT_AST) {
  1241. continue;
  1242. }
  1243. }
  1244. val = &c->value;
  1245. if (UNEXPECTED(zval_update_constant_ex(val, c->ce) != SUCCESS)) {
  1246. return FAILURE;
  1247. }
  1248. }
  1249. } ZEND_HASH_FOREACH_END();
  1250. }
  1251. if (class_type->default_static_members_count) {
  1252. static_members_table = CE_STATIC_MEMBERS(class_type);
  1253. if (!static_members_table) {
  1254. zend_class_init_statics(class_type);
  1255. static_members_table = CE_STATIC_MEMBERS(class_type);
  1256. }
  1257. }
  1258. default_properties_table = class_type->default_properties_table;
  1259. if (uses_mutable_data && (ce_flags & ZEND_ACC_HAS_AST_PROPERTIES)) {
  1260. zval *src, *dst, *end;
  1261. default_properties_table = mutable_data->default_properties_table;
  1262. if (!default_properties_table) {
  1263. default_properties_table = zend_arena_alloc(&CG(arena), sizeof(zval) * class_type->default_properties_count);
  1264. src = class_type->default_properties_table;
  1265. dst = default_properties_table;
  1266. end = dst + class_type->default_properties_count;
  1267. do {
  1268. ZVAL_COPY_PROP(dst, src);
  1269. src++;
  1270. dst++;
  1271. } while (dst != end);
  1272. mutable_data->default_properties_table = default_properties_table;
  1273. }
  1274. }
  1275. if (ce_flags & (ZEND_ACC_HAS_AST_PROPERTIES|ZEND_ACC_HAS_AST_STATICS)) {
  1276. zend_property_info *prop_info;
  1277. /* Use the default properties table to also update initializers of private properties
  1278. * that have been shadowed in a child class. */
  1279. for (uint32_t i = 0; i < class_type->default_properties_count; i++) {
  1280. val = &default_properties_table[i];
  1281. prop_info = class_type->properties_info_table[i];
  1282. if (Z_TYPE_P(val) == IS_CONSTANT_AST
  1283. && UNEXPECTED(update_property(val, prop_info) != SUCCESS)) {
  1284. return FAILURE;
  1285. }
  1286. }
  1287. if (class_type->default_static_members_count) {
  1288. ZEND_HASH_FOREACH_PTR(&class_type->properties_info, prop_info) {
  1289. if (prop_info->flags & ZEND_ACC_STATIC) {
  1290. val = static_members_table + prop_info->offset;
  1291. if (Z_TYPE_P(val) == IS_CONSTANT_AST
  1292. && UNEXPECTED(update_property(val, prop_info) != SUCCESS)) {
  1293. return FAILURE;
  1294. }
  1295. }
  1296. } ZEND_HASH_FOREACH_END();
  1297. }
  1298. }
  1299. ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
  1300. ce_flags &= ~ZEND_ACC_HAS_AST_CONSTANTS;
  1301. ce_flags &= ~ZEND_ACC_HAS_AST_PROPERTIES;
  1302. ce_flags &= ~ZEND_ACC_HAS_AST_STATICS;
  1303. if (uses_mutable_data) {
  1304. mutable_data->ce_flags = ce_flags;
  1305. } else {
  1306. class_type->ce_flags = ce_flags;
  1307. }
  1308. return SUCCESS;
  1309. }
  1310. /* }}} */
  1311. static zend_always_inline void _object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
  1312. {
  1313. if (class_type->default_properties_count) {
  1314. zval *src = CE_DEFAULT_PROPERTIES_TABLE(class_type);
  1315. zval *dst = object->properties_table;
  1316. zval *end = src + class_type->default_properties_count;
  1317. if (UNEXPECTED(class_type->type == ZEND_INTERNAL_CLASS)) {
  1318. do {
  1319. ZVAL_COPY_OR_DUP_PROP(dst, src);
  1320. src++;
  1321. dst++;
  1322. } while (src != end);
  1323. } else {
  1324. do {
  1325. ZVAL_COPY_PROP(dst, src);
  1326. src++;
  1327. dst++;
  1328. } while (src != end);
  1329. }
  1330. }
  1331. }
  1332. /* }}} */
  1333. ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
  1334. {
  1335. object->properties = NULL;
  1336. _object_properties_init(object, class_type);
  1337. }
  1338. /* }}} */
  1339. ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties) /* {{{ */
  1340. {
  1341. object->properties = properties;
  1342. if (object->ce->default_properties_count) {
  1343. zval *prop;
  1344. zend_string *key;
  1345. zend_property_info *property_info;
  1346. ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
  1347. property_info = zend_get_property_info(object->ce, key, 1);
  1348. if (property_info != ZEND_WRONG_PROPERTY_INFO &&
  1349. property_info &&
  1350. (property_info->flags & ZEND_ACC_STATIC) == 0) {
  1351. zval *slot = OBJ_PROP(object, property_info->offset);
  1352. if (UNEXPECTED(ZEND_TYPE_IS_SET(property_info->type))) {
  1353. zval tmp;
  1354. ZVAL_COPY_VALUE(&tmp, prop);
  1355. if (UNEXPECTED(!zend_verify_property_type(property_info, &tmp, 0))) {
  1356. continue;
  1357. }
  1358. ZVAL_COPY_VALUE(slot, &tmp);
  1359. } else {
  1360. ZVAL_COPY_VALUE(slot, prop);
  1361. }
  1362. ZVAL_INDIRECT(prop, slot);
  1363. }
  1364. } ZEND_HASH_FOREACH_END();
  1365. }
  1366. }
  1367. /* }}} */
  1368. ZEND_API void object_properties_load(zend_object *object, HashTable *properties) /* {{{ */
  1369. {
  1370. zval *prop, tmp;
  1371. zend_string *key;
  1372. zend_long h;
  1373. zend_property_info *property_info;
  1374. ZEND_HASH_FOREACH_KEY_VAL(properties, h, key, prop) {
  1375. if (key) {
  1376. if (ZSTR_VAL(key)[0] == '\0') {
  1377. const char *class_name, *prop_name;
  1378. size_t prop_name_len;
  1379. if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
  1380. zend_string *pname = zend_string_init(prop_name, prop_name_len, 0);
  1381. zend_class_entry *prev_scope = EG(fake_scope);
  1382. if (class_name && class_name[0] != '*') {
  1383. zend_string *cname = zend_string_init(class_name, strlen(class_name), 0);
  1384. EG(fake_scope) = zend_lookup_class(cname);
  1385. zend_string_release_ex(cname, 0);
  1386. }
  1387. property_info = zend_get_property_info(object->ce, pname, 1);
  1388. zend_string_release_ex(pname, 0);
  1389. EG(fake_scope) = prev_scope;
  1390. } else {
  1391. property_info = ZEND_WRONG_PROPERTY_INFO;
  1392. }
  1393. } else {
  1394. property_info = zend_get_property_info(object->ce, key, 1);
  1395. }
  1396. if (property_info != ZEND_WRONG_PROPERTY_INFO &&
  1397. property_info &&
  1398. (property_info->flags & ZEND_ACC_STATIC) == 0) {
  1399. zval *slot = OBJ_PROP(object, property_info->offset);
  1400. zval_ptr_dtor(slot);
  1401. ZVAL_COPY_VALUE(slot, prop);
  1402. zval_add_ref(slot);
  1403. if (object->properties) {
  1404. ZVAL_INDIRECT(&tmp, slot);
  1405. zend_hash_update(object->properties, key, &tmp);
  1406. }
  1407. } else {
  1408. if (!object->properties) {
  1409. rebuild_object_properties(object);
  1410. }
  1411. prop = zend_hash_update(object->properties, key, prop);
  1412. zval_add_ref(prop);
  1413. }
  1414. } else {
  1415. if (!object->properties) {
  1416. rebuild_object_properties(object);
  1417. }
  1418. prop = zend_hash_index_update(object->properties, h, prop);
  1419. zval_add_ref(prop);
  1420. }
  1421. } ZEND_HASH_FOREACH_END();
  1422. }
  1423. /* }}} */
  1424. /* This function requires 'properties' to contain all props declared in the
  1425. * class and all props being public. If only a subset is given or the class
  1426. * has protected members then you need to merge the properties separately by
  1427. * calling zend_merge_properties(). */
  1428. static zend_always_inline zend_result _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
  1429. {
  1430. if (UNEXPECTED(class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS|ZEND_ACC_ENUM))) {
  1431. if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
  1432. zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
  1433. } else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
  1434. zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
  1435. } else if (class_type->ce_flags & ZEND_ACC_ENUM) {
  1436. zend_throw_error(NULL, "Cannot instantiate enum %s", ZSTR_VAL(class_type->name));
  1437. } else {
  1438. zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
  1439. }
  1440. ZVAL_NULL(arg);
  1441. Z_OBJ_P(arg) = NULL;
  1442. return FAILURE;
  1443. }
  1444. if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
  1445. if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
  1446. ZVAL_NULL(arg);
  1447. Z_OBJ_P(arg) = NULL;
  1448. return FAILURE;
  1449. }
  1450. }
  1451. if (class_type->create_object == NULL) {
  1452. zend_object *obj = zend_objects_new(class_type);
  1453. ZVAL_OBJ(arg, obj);
  1454. if (properties) {
  1455. object_properties_init_ex(obj, properties);
  1456. } else {
  1457. _object_properties_init(obj, class_type);
  1458. }
  1459. } else {
  1460. ZVAL_OBJ(arg, class_type->create_object(class_type));
  1461. }
  1462. return SUCCESS;
  1463. }
  1464. /* }}} */
  1465. ZEND_API zend_result object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
  1466. {
  1467. return _object_and_properties_init(arg, class_type, properties);
  1468. }
  1469. /* }}} */
  1470. ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type) /* {{{ */
  1471. {
  1472. return _object_and_properties_init(arg, class_type, NULL);
  1473. }
  1474. /* }}} */
  1475. ZEND_API void object_init(zval *arg) /* {{{ */
  1476. {
  1477. ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def));
  1478. }
  1479. /* }}} */
  1480. ZEND_API void add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
  1481. {
  1482. zval tmp;
  1483. ZVAL_LONG(&tmp, n);
  1484. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1485. }
  1486. /* }}} */
  1487. ZEND_API void add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
  1488. {
  1489. zval tmp;
  1490. ZVAL_NULL(&tmp);
  1491. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1492. }
  1493. /* }}} */
  1494. ZEND_API void add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, bool b) /* {{{ */
  1495. {
  1496. zval tmp;
  1497. ZVAL_BOOL(&tmp, b);
  1498. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1499. }
  1500. /* }}} */
  1501. ZEND_API void add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
  1502. {
  1503. zval tmp;
  1504. ZVAL_RES(&tmp, r);
  1505. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1506. }
  1507. /* }}} */
  1508. ZEND_API void add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
  1509. {
  1510. zval tmp;
  1511. ZVAL_DOUBLE(&tmp, d);
  1512. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1513. }
  1514. /* }}} */
  1515. ZEND_API void add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
  1516. {
  1517. zval tmp;
  1518. ZVAL_STR(&tmp, str);
  1519. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1520. }
  1521. /* }}} */
  1522. ZEND_API void add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
  1523. {
  1524. zval tmp;
  1525. ZVAL_STRING(&tmp, str);
  1526. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1527. }
  1528. /* }}} */
  1529. ZEND_API void add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
  1530. {
  1531. zval tmp;
  1532. ZVAL_STRINGL(&tmp, str, length);
  1533. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1534. }
  1535. /* }}} */
  1536. ZEND_API void add_assoc_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */
  1537. {
  1538. zval tmp;
  1539. ZVAL_ARR(&tmp, arr);
  1540. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1541. }
  1542. /* }}} */
  1543. ZEND_API void add_assoc_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */
  1544. {
  1545. zval tmp;
  1546. ZVAL_OBJ(&tmp, obj);
  1547. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1548. }
  1549. /* }}} */
  1550. ZEND_API void add_assoc_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref) /* {{{ */
  1551. {
  1552. zval tmp;
  1553. ZVAL_REF(&tmp, ref);
  1554. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1555. }
  1556. /* }}} */
  1557. ZEND_API void add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
  1558. {
  1559. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
  1560. }
  1561. /* }}} */
  1562. ZEND_API void add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
  1563. {
  1564. zval tmp;
  1565. ZVAL_LONG(&tmp, n);
  1566. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1567. }
  1568. /* }}} */
  1569. ZEND_API void add_index_null(zval *arg, zend_ulong index) /* {{{ */
  1570. {
  1571. zval tmp;
  1572. ZVAL_NULL(&tmp);
  1573. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1574. }
  1575. /* }}} */
  1576. ZEND_API void add_index_bool(zval *arg, zend_ulong index, bool b) /* {{{ */
  1577. {
  1578. zval tmp;
  1579. ZVAL_BOOL(&tmp, b);
  1580. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1581. }
  1582. /* }}} */
  1583. ZEND_API void add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */
  1584. {
  1585. zval tmp;
  1586. ZVAL_RES(&tmp, r);
  1587. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1588. }
  1589. /* }}} */
  1590. ZEND_API void add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
  1591. {
  1592. zval tmp;
  1593. ZVAL_DOUBLE(&tmp, d);
  1594. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1595. }
  1596. /* }}} */
  1597. ZEND_API void add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
  1598. {
  1599. zval tmp;
  1600. ZVAL_STR(&tmp, str);
  1601. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1602. }
  1603. /* }}} */
  1604. ZEND_API void add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
  1605. {
  1606. zval tmp;
  1607. ZVAL_STRING(&tmp, str);
  1608. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1609. }
  1610. /* }}} */
  1611. ZEND_API void add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
  1612. {
  1613. zval tmp;
  1614. ZVAL_STRINGL(&tmp, str, length);
  1615. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1616. }
  1617. /* }}} */
  1618. ZEND_API void add_index_array(zval *arg, zend_ulong index, zend_array *arr) /* {{{ */
  1619. {
  1620. zval tmp;
  1621. ZVAL_ARR(&tmp, arr);
  1622. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1623. }
  1624. /* }}} */
  1625. ZEND_API void add_index_object(zval *arg, zend_ulong index, zend_object *obj) /* {{{ */
  1626. {
  1627. zval tmp;
  1628. ZVAL_OBJ(&tmp, obj);
  1629. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1630. }
  1631. /* }}} */
  1632. ZEND_API void add_index_reference(zval *arg, zend_ulong index, zend_reference *ref) /* {{{ */
  1633. {
  1634. zval tmp;
  1635. ZVAL_REF(&tmp, ref);
  1636. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1637. }
  1638. /* }}} */
  1639. ZEND_API zend_result add_next_index_long(zval *arg, zend_long n) /* {{{ */
  1640. {
  1641. zval tmp;
  1642. ZVAL_LONG(&tmp, n);
  1643. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1644. }
  1645. /* }}} */
  1646. ZEND_API zend_result add_next_index_null(zval *arg) /* {{{ */
  1647. {
  1648. zval tmp;
  1649. ZVAL_NULL(&tmp);
  1650. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1651. }
  1652. /* }}} */
  1653. ZEND_API zend_result add_next_index_bool(zval *arg, bool b) /* {{{ */
  1654. {
  1655. zval tmp;
  1656. ZVAL_BOOL(&tmp, b);
  1657. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1658. }
  1659. /* }}} */
  1660. ZEND_API zend_result add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */
  1661. {
  1662. zval tmp;
  1663. ZVAL_RES(&tmp, r);
  1664. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1665. }
  1666. /* }}} */
  1667. ZEND_API zend_result add_next_index_double(zval *arg, double d) /* {{{ */
  1668. {
  1669. zval tmp;
  1670. ZVAL_DOUBLE(&tmp, d);
  1671. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1672. }
  1673. /* }}} */
  1674. ZEND_API zend_result add_next_index_str(zval *arg, zend_string *str) /* {{{ */
  1675. {
  1676. zval tmp;
  1677. ZVAL_STR(&tmp, str);
  1678. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1679. }
  1680. /* }}} */
  1681. ZEND_API zend_result add_next_index_string(zval *arg, const char *str) /* {{{ */
  1682. {
  1683. zval tmp;
  1684. ZVAL_STRING(&tmp, str);
  1685. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1686. }
  1687. /* }}} */
  1688. ZEND_API zend_result add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */
  1689. {
  1690. zval tmp;
  1691. ZVAL_STRINGL(&tmp, str, length);
  1692. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1693. }
  1694. /* }}} */
  1695. ZEND_API zend_result add_next_index_array(zval *arg, zend_array *arr) /* {{{ */
  1696. {
  1697. zval tmp;
  1698. ZVAL_ARR(&tmp, arr);
  1699. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1700. }
  1701. /* }}} */
  1702. ZEND_API zend_result add_next_index_object(zval *arg, zend_object *obj) /* {{{ */
  1703. {
  1704. zval tmp;
  1705. ZVAL_OBJ(&tmp, obj);
  1706. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1707. }
  1708. /* }}} */
  1709. ZEND_API zend_result add_next_index_reference(zval *arg, zend_reference *ref) /* {{{ */
  1710. {
  1711. zval tmp;
  1712. ZVAL_REF(&tmp, ref);
  1713. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1714. }
  1715. /* }}} */
  1716. ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
  1717. {
  1718. zval *result;
  1719. switch (Z_TYPE_P(key)) {
  1720. case IS_STRING:
  1721. result = zend_symtable_update(ht, Z_STR_P(key), value);
  1722. break;
  1723. case IS_NULL:
  1724. result = zend_hash_update(ht, ZSTR_EMPTY_ALLOC(), value);
  1725. break;
  1726. case IS_RESOURCE:
  1727. zend_use_resource_as_offset(key);
  1728. result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value);
  1729. break;
  1730. case IS_FALSE:
  1731. result = zend_hash_index_update(ht, 0, value);
  1732. break;
  1733. case IS_TRUE:
  1734. result = zend_hash_index_update(ht, 1, value);
  1735. break;
  1736. case IS_LONG:
  1737. result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
  1738. break;
  1739. case IS_DOUBLE:
  1740. result = zend_hash_index_update(ht, zend_dval_to_lval_safe(Z_DVAL_P(key)), value);
  1741. break;
  1742. default:
  1743. zend_type_error("Illegal offset type");
  1744. result = NULL;
  1745. }
  1746. if (result) {
  1747. Z_TRY_ADDREF_P(result);
  1748. return SUCCESS;
  1749. } else {
  1750. return FAILURE;
  1751. }
  1752. }
  1753. /* }}} */
  1754. ZEND_API void add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
  1755. {
  1756. zval tmp;
  1757. ZVAL_LONG(&tmp, n);
  1758. add_property_zval_ex(arg, key, key_len, &tmp);
  1759. }
  1760. /* }}} */
  1761. ZEND_API void add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */
  1762. {
  1763. zval tmp;
  1764. ZVAL_BOOL(&tmp, b);
  1765. add_property_zval_ex(arg, key, key_len, &tmp);
  1766. }
  1767. /* }}} */
  1768. ZEND_API void add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
  1769. {
  1770. zval tmp;
  1771. ZVAL_NULL(&tmp);
  1772. add_property_zval_ex(arg, key, key_len, &tmp);
  1773. }
  1774. /* }}} */
  1775. ZEND_API void add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
  1776. {
  1777. zval tmp;
  1778. ZVAL_RES(&tmp, r);
  1779. add_property_zval_ex(arg, key, key_len, &tmp);
  1780. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1781. }
  1782. /* }}} */
  1783. ZEND_API void add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
  1784. {
  1785. zval tmp;
  1786. ZVAL_DOUBLE(&tmp, d);
  1787. add_property_zval_ex(arg, key, key_len, &tmp);
  1788. }
  1789. /* }}} */
  1790. ZEND_API void add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
  1791. {
  1792. zval tmp;
  1793. ZVAL_STR(&tmp, str);
  1794. add_property_zval_ex(arg, key, key_len, &tmp);
  1795. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1796. }
  1797. /* }}} */
  1798. ZEND_API void add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
  1799. {
  1800. zval tmp;
  1801. ZVAL_STRING(&tmp, str);
  1802. add_property_zval_ex(arg, key, key_len, &tmp);
  1803. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1804. }
  1805. /* }}} */
  1806. ZEND_API void add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
  1807. {
  1808. zval tmp;
  1809. ZVAL_STRINGL(&tmp, str, length);
  1810. add_property_zval_ex(arg, key, key_len, &tmp);
  1811. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1812. }
  1813. /* }}} */
  1814. ZEND_API void add_property_array_ex(zval *arg, const char *key, size_t key_len, zend_array *arr) /* {{{ */
  1815. {
  1816. zval tmp;
  1817. ZVAL_ARR(&tmp, arr);
  1818. add_property_zval_ex(arg, key, key_len, &tmp);
  1819. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1820. }
  1821. /* }}} */
  1822. ZEND_API void add_property_object_ex(zval *arg, const char *key, size_t key_len, zend_object *obj) /* {{{ */
  1823. {
  1824. zval tmp;
  1825. ZVAL_OBJ(&tmp, obj);
  1826. add_property_zval_ex(arg, key, key_len, &tmp);
  1827. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1828. }
  1829. /* }}} */
  1830. ZEND_API void add_property_reference_ex(zval *arg, const char *key, size_t key_len, zend_reference *ref) /* {{{ */
  1831. {
  1832. zval tmp;
  1833. ZVAL_REF(&tmp, ref);
  1834. add_property_zval_ex(arg, key, key_len, &tmp);
  1835. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1836. }
  1837. /* }}} */
  1838. ZEND_API void add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
  1839. {
  1840. zend_string *str;
  1841. str = zend_string_init(key, key_len, 0);
  1842. Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL);
  1843. zend_string_release_ex(str, 0);
  1844. }
  1845. /* }}} */
  1846. ZEND_API zend_result zend_startup_module_ex(zend_module_entry *module) /* {{{ */
  1847. {
  1848. size_t name_len;
  1849. zend_string *lcname;
  1850. if (module->module_started) {
  1851. return SUCCESS;
  1852. }
  1853. module->module_started = 1;
  1854. /* Check module dependencies */
  1855. if (module->deps) {
  1856. const zend_module_dep *dep = module->deps;
  1857. while (dep->name) {
  1858. if (dep->type == MODULE_DEP_REQUIRED) {
  1859. zend_module_entry *req_mod;
  1860. name_len = strlen(dep->name);
  1861. lcname = zend_string_alloc(name_len, 0);
  1862. zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
  1863. if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) {
  1864. zend_string_efree(lcname);
  1865. /* TODO: Check version relationship */
  1866. zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because required module \"%s\" is not loaded", module->name, dep->name);
  1867. module->module_started = 0;
  1868. return FAILURE;
  1869. }
  1870. zend_string_efree(lcname);
  1871. }
  1872. ++dep;
  1873. }
  1874. }
  1875. /* Initialize module globals */
  1876. if (module->globals_size) {
  1877. #ifdef ZTS
  1878. ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
  1879. #else
  1880. if (module->globals_ctor) {
  1881. module->globals_ctor(module->globals_ptr);
  1882. }
  1883. #endif
  1884. }
  1885. if (module->module_startup_func) {
  1886. EG(current_module) = module;
  1887. if (module->module_startup_func(module->type, module->module_number)==FAILURE) {
  1888. zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name);
  1889. EG(current_module) = NULL;
  1890. return FAILURE;
  1891. }
  1892. EG(current_module) = NULL;
  1893. }
  1894. return SUCCESS;
  1895. }
  1896. /* }}} */
  1897. static int zend_startup_module_zval(zval *zv) /* {{{ */
  1898. {
  1899. zend_module_entry *module = Z_PTR_P(zv);
  1900. return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
  1901. }
  1902. /* }}} */
  1903. static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
  1904. {
  1905. Bucket *b1 = base;
  1906. Bucket *b2;
  1907. Bucket *end = b1 + count;
  1908. Bucket tmp;
  1909. zend_module_entry *m, *r;
  1910. while (b1 < end) {
  1911. try_again:
  1912. m = (zend_module_entry*)Z_PTR(b1->val);
  1913. if (!m->module_started && m->deps) {
  1914. const zend_module_dep *dep = m->deps;
  1915. while (dep->name) {
  1916. if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
  1917. b2 = b1 + 1;
  1918. while (b2 < end) {
  1919. r = (zend_module_entry*)Z_PTR(b2->val);
  1920. if (strcasecmp(dep->name, r->name) == 0) {
  1921. tmp = *b1;
  1922. *b1 = *b2;
  1923. *b2 = tmp;
  1924. goto try_again;
  1925. }
  1926. b2++;
  1927. }
  1928. }
  1929. dep++;
  1930. }
  1931. }
  1932. b1++;
  1933. }
  1934. }
  1935. /* }}} */
  1936. ZEND_API void zend_collect_module_handlers(void) /* {{{ */
  1937. {
  1938. zend_module_entry *module;
  1939. int startup_count = 0;
  1940. int shutdown_count = 0;
  1941. int post_deactivate_count = 0;
  1942. zend_class_entry *ce;
  1943. int class_count = 0;
  1944. /* Collect extensions with request startup/shutdown handlers */
  1945. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  1946. if (module->request_startup_func) {
  1947. startup_count++;
  1948. }
  1949. if (module->request_shutdown_func) {
  1950. shutdown_count++;
  1951. }
  1952. if (module->post_deactivate_func) {
  1953. post_deactivate_count++;
  1954. }
  1955. } ZEND_HASH_FOREACH_END();
  1956. module_request_startup_handlers = (zend_module_entry**)malloc(
  1957. sizeof(zend_module_entry*) *
  1958. (startup_count + 1 +
  1959. shutdown_count + 1 +
  1960. post_deactivate_count + 1));
  1961. module_request_startup_handlers[startup_count] = NULL;
  1962. module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
  1963. module_request_shutdown_handlers[shutdown_count] = NULL;
  1964. module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
  1965. module_post_deactivate_handlers[post_deactivate_count] = NULL;
  1966. startup_count = 0;
  1967. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  1968. if (module->request_startup_func) {
  1969. module_request_startup_handlers[startup_count++] = module;
  1970. }
  1971. if (module->request_shutdown_func) {
  1972. module_request_shutdown_handlers[--shutdown_count] = module;
  1973. }
  1974. if (module->post_deactivate_func) {
  1975. module_post_deactivate_handlers[--post_deactivate_count] = module;
  1976. }
  1977. } ZEND_HASH_FOREACH_END();
  1978. /* Collect internal classes with static members */
  1979. ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
  1980. if (ce->type == ZEND_INTERNAL_CLASS &&
  1981. ce->default_static_members_count > 0) {
  1982. class_count++;
  1983. }
  1984. } ZEND_HASH_FOREACH_END();
  1985. class_cleanup_handlers = (zend_class_entry**)malloc(
  1986. sizeof(zend_class_entry*) *
  1987. (class_count + 1));
  1988. class_cleanup_handlers[class_count] = NULL;
  1989. if (class_count) {
  1990. ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
  1991. if (ce->type == ZEND_INTERNAL_CLASS &&
  1992. ce->default_static_members_count > 0) {
  1993. class_cleanup_handlers[--class_count] = ce;
  1994. }
  1995. } ZEND_HASH_FOREACH_END();
  1996. }
  1997. }
  1998. /* }}} */
  1999. ZEND_API void zend_startup_modules(void) /* {{{ */
  2000. {
  2001. zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
  2002. zend_hash_apply(&module_registry, zend_startup_module_zval);
  2003. }
  2004. /* }}} */
  2005. ZEND_API void zend_destroy_modules(void) /* {{{ */
  2006. {
  2007. free(class_cleanup_handlers);
  2008. free(module_request_startup_handlers);
  2009. zend_hash_graceful_reverse_destroy(&module_registry);
  2010. }
  2011. /* }}} */
  2012. ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module) /* {{{ */
  2013. {
  2014. size_t name_len;
  2015. zend_string *lcname;
  2016. zend_module_entry *module_ptr;
  2017. if (!module) {
  2018. return NULL;
  2019. }
  2020. #if 0
  2021. zend_printf("%s: Registering module %d\n", module->name, module->module_number);
  2022. #endif
  2023. /* Check module dependencies */
  2024. if (module->deps) {
  2025. const zend_module_dep *dep = module->deps;
  2026. while (dep->name) {
  2027. if (dep->type == MODULE_DEP_CONFLICTS) {
  2028. name_len = strlen(dep->name);
  2029. lcname = zend_string_alloc(name_len, 0);
  2030. zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
  2031. if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) {
  2032. zend_string_efree(lcname);
  2033. /* TODO: Check version relationship */
  2034. zend_error(E_CORE_WARNING, "Cannot load module \"%s\" because conflicting module \"%s\" is already loaded", module->name, dep->name);
  2035. return NULL;
  2036. }
  2037. zend_string_efree(lcname);
  2038. }
  2039. ++dep;
  2040. }
  2041. }
  2042. name_len = strlen(module->name);
  2043. lcname = zend_string_alloc(name_len, module->type == MODULE_PERSISTENT);
  2044. zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
  2045. lcname = zend_new_interned_string(lcname);
  2046. if ((module_ptr = zend_hash_add_mem(&module_registry, lcname, module, sizeof(zend_module_entry))) == NULL) {
  2047. zend_error(E_CORE_WARNING, "Module \"%s\" is already loaded", module->name);
  2048. zend_string_release(lcname);
  2049. return NULL;
  2050. }
  2051. module = module_ptr;
  2052. EG(current_module) = module;
  2053. if (module->functions && zend_register_functions(NULL, module->functions, NULL, module->type)==FAILURE) {
  2054. zend_hash_del(&module_registry, lcname);
  2055. zend_string_release(lcname);
  2056. EG(current_module) = NULL;
  2057. zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
  2058. return NULL;
  2059. }
  2060. EG(current_module) = NULL;
  2061. zend_string_release(lcname);
  2062. return module;
  2063. }
  2064. /* }}} */
  2065. ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
  2066. {
  2067. module->module_number = zend_next_free_module();
  2068. module->type = MODULE_PERSISTENT;
  2069. return zend_register_module_ex(module);
  2070. }
  2071. /* }}} */
  2072. static void zend_check_magic_method_args(
  2073. uint32_t num_args, const zend_class_entry *ce, const zend_function *fptr, int error_type)
  2074. {
  2075. if (fptr->common.num_args != num_args) {
  2076. if (num_args == 0) {
  2077. zend_error(error_type, "Method %s::%s() cannot take arguments",
  2078. ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
  2079. } else if (num_args == 1) {
  2080. zend_error(error_type, "Method %s::%s() must take exactly 1 argument",
  2081. ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
  2082. } else {
  2083. zend_error(error_type, "Method %s::%s() must take exactly %" PRIu32 " arguments",
  2084. ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name), num_args);
  2085. }
  2086. return;
  2087. }
  2088. for (uint32_t i = 0; i < num_args; i++) {
  2089. if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, i + 1)) {
  2090. zend_error(error_type, "Method %s::%s() cannot take arguments by reference",
  2091. ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
  2092. return;
  2093. }
  2094. }
  2095. }
  2096. static void zend_check_magic_method_arg_type(uint32_t arg_num, const zend_class_entry *ce, const zend_function *fptr, int error_type, int arg_type)
  2097. {
  2098. if (
  2099. ZEND_TYPE_IS_SET(fptr->common.arg_info[arg_num].type)
  2100. && !(ZEND_TYPE_FULL_MASK(fptr->common.arg_info[arg_num].type) & arg_type)
  2101. ) {
  2102. zend_error(error_type, "%s::%s(): Parameter #%d ($%s) must be of type %s when declared",
  2103. ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
  2104. arg_num + 1, ZSTR_VAL(fptr->common.arg_info[arg_num].name),
  2105. ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(arg_type))));
  2106. }
  2107. }
  2108. static void zend_check_magic_method_return_type(const zend_class_entry *ce, const zend_function *fptr, int error_type, int return_type)
  2109. {
  2110. if (!(fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
  2111. /* For backwards compatibility reasons, do not enforce the return type if it is not set. */
  2112. return;
  2113. }
  2114. if (ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & MAY_BE_NEVER) {
  2115. /* It is always legal to specify the never type. */
  2116. return;
  2117. }
  2118. bool is_complex_type = ZEND_TYPE_IS_COMPLEX(fptr->common.arg_info[-1].type);
  2119. uint32_t extra_types = ZEND_TYPE_PURE_MASK(fptr->common.arg_info[-1].type) & ~return_type;
  2120. if (extra_types & MAY_BE_STATIC) {
  2121. extra_types &= ~MAY_BE_STATIC;
  2122. is_complex_type = true;
  2123. }
  2124. if (extra_types || (is_complex_type && return_type != MAY_BE_OBJECT)) {
  2125. zend_error(error_type, "%s::%s(): Return type must be %s when declared",
  2126. ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name),
  2127. ZSTR_VAL(zend_type_to_string((zend_type) ZEND_TYPE_INIT_MASK(return_type))));
  2128. }
  2129. }
  2130. static void zend_check_magic_method_non_static(
  2131. const zend_class_entry *ce, const zend_function *fptr, int error_type)
  2132. {
  2133. if (fptr->common.fn_flags & ZEND_ACC_STATIC) {
  2134. zend_error(error_type, "Method %s::%s() cannot be static",
  2135. ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
  2136. }
  2137. }
  2138. static void zend_check_magic_method_static(
  2139. const zend_class_entry *ce, const zend_function *fptr, int error_type)
  2140. {
  2141. if (!(fptr->common.fn_flags & ZEND_ACC_STATIC)) {
  2142. zend_error(error_type, "Method %s::%s() must be static",
  2143. ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
  2144. }
  2145. }
  2146. static void zend_check_magic_method_public(
  2147. const zend_class_entry *ce, const zend_function *fptr, int error_type)
  2148. {
  2149. // TODO: Remove this warning after adding proper visibility handling.
  2150. if (!(fptr->common.fn_flags & ZEND_ACC_PUBLIC)) {
  2151. zend_error(E_WARNING, "The magic method %s::%s() must have public visibility",
  2152. ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
  2153. }
  2154. }
  2155. static void zend_check_magic_method_no_return_type(
  2156. const zend_class_entry *ce, const zend_function *fptr, int error_type)
  2157. {
  2158. if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
  2159. zend_error_noreturn(error_type, "Method %s::%s() cannot declare a return type",
  2160. ZSTR_VAL(ce->name), ZSTR_VAL(fptr->common.function_name));
  2161. }
  2162. }
  2163. ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, zend_string *lcname, int error_type) /* {{{ */
  2164. {
  2165. if (ZSTR_VAL(fptr->common.function_name)[0] != '_'
  2166. || ZSTR_VAL(fptr->common.function_name)[1] != '_') {
  2167. return;
  2168. }
  2169. if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
  2170. zend_check_magic_method_non_static(ce, fptr, error_type);
  2171. zend_check_magic_method_no_return_type(ce, fptr, error_type);
  2172. } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
  2173. zend_check_magic_method_args(0, ce, fptr, error_type);
  2174. zend_check_magic_method_non_static(ce, fptr, error_type);
  2175. zend_check_magic_method_no_return_type(ce, fptr, error_type);
  2176. } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
  2177. zend_check_magic_method_args(0, ce, fptr, error_type);
  2178. zend_check_magic_method_non_static(ce, fptr, error_type);
  2179. zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
  2180. } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
  2181. zend_check_magic_method_args(1, ce, fptr, error_type);
  2182. zend_check_magic_method_non_static(ce, fptr, error_type);
  2183. zend_check_magic_method_public(ce, fptr, error_type);
  2184. zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
  2185. } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
  2186. zend_check_magic_method_args(2, ce, fptr, error_type);
  2187. zend_check_magic_method_non_static(ce, fptr, error_type);
  2188. zend_check_magic_method_public(ce, fptr, error_type);
  2189. zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
  2190. zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
  2191. } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
  2192. zend_check_magic_method_args(1, ce, fptr, error_type);
  2193. zend_check_magic_method_non_static(ce, fptr, error_type);
  2194. zend_check_magic_method_public(ce, fptr, error_type);
  2195. zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
  2196. zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
  2197. } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
  2198. zend_check_magic_method_args(1, ce, fptr, error_type);
  2199. zend_check_magic_method_non_static(ce, fptr, error_type);
  2200. zend_check_magic_method_public(ce, fptr, error_type);
  2201. zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
  2202. zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_BOOL);
  2203. } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
  2204. zend_check_magic_method_args(2, ce, fptr, error_type);
  2205. zend_check_magic_method_non_static(ce, fptr, error_type);
  2206. zend_check_magic_method_public(ce, fptr, error_type);
  2207. zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
  2208. zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
  2209. } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
  2210. zend_check_magic_method_args(2, ce, fptr, error_type);
  2211. zend_check_magic_method_static(ce, fptr, error_type);
  2212. zend_check_magic_method_public(ce, fptr, error_type);
  2213. zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_STRING);
  2214. zend_check_magic_method_arg_type(1, ce, fptr, error_type, MAY_BE_ARRAY);
  2215. } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
  2216. zend_check_magic_method_args(0, ce, fptr, error_type);
  2217. zend_check_magic_method_non_static(ce, fptr, error_type);
  2218. zend_check_magic_method_public(ce, fptr, error_type);
  2219. zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_STRING);
  2220. } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
  2221. zend_check_magic_method_args(0, ce, fptr, error_type);
  2222. zend_check_magic_method_non_static(ce, fptr, error_type);
  2223. zend_check_magic_method_public(ce, fptr, error_type);
  2224. zend_check_magic_method_return_type(ce, fptr, error_type, (MAY_BE_ARRAY | MAY_BE_NULL));
  2225. } else if (zend_string_equals_literal(lcname, "__serialize")) {
  2226. zend_check_magic_method_args(0, ce, fptr, error_type);
  2227. zend_check_magic_method_non_static(ce, fptr, error_type);
  2228. zend_check_magic_method_public(ce, fptr, error_type);
  2229. zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
  2230. } else if (zend_string_equals_literal(lcname, "__unserialize")) {
  2231. zend_check_magic_method_args(1, ce, fptr, error_type);
  2232. zend_check_magic_method_non_static(ce, fptr, error_type);
  2233. zend_check_magic_method_public(ce, fptr, error_type);
  2234. zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
  2235. zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
  2236. } else if (zend_string_equals_literal(lcname, "__set_state")) {
  2237. zend_check_magic_method_args(1, ce, fptr, error_type);
  2238. zend_check_magic_method_static(ce, fptr, error_type);
  2239. zend_check_magic_method_public(ce, fptr, error_type);
  2240. zend_check_magic_method_arg_type(0, ce, fptr, error_type, MAY_BE_ARRAY);
  2241. zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_OBJECT);
  2242. } else if (zend_string_equals_literal(lcname, "__invoke")) {
  2243. zend_check_magic_method_non_static(ce, fptr, error_type);
  2244. zend_check_magic_method_public(ce, fptr, error_type);
  2245. } else if (zend_string_equals_literal(lcname, "__sleep")) {
  2246. zend_check_magic_method_args(0, ce, fptr, error_type);
  2247. zend_check_magic_method_non_static(ce, fptr, error_type);
  2248. zend_check_magic_method_public(ce, fptr, error_type);
  2249. zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_ARRAY);
  2250. } else if (zend_string_equals_literal(lcname, "__wakeup")) {
  2251. zend_check_magic_method_args(0, ce, fptr, error_type);
  2252. zend_check_magic_method_non_static(ce, fptr, error_type);
  2253. zend_check_magic_method_public(ce, fptr, error_type);
  2254. zend_check_magic_method_return_type(ce, fptr, error_type, MAY_BE_VOID);
  2255. }
  2256. }
  2257. /* }}} */
  2258. ZEND_API void zend_add_magic_method(zend_class_entry *ce, zend_function *fptr, zend_string *lcname)
  2259. {
  2260. if (ZSTR_VAL(lcname)[0] != '_' || ZSTR_VAL(lcname)[1] != '_') {
  2261. /* pass */
  2262. } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME)) {
  2263. ce->clone = fptr;
  2264. } else if (zend_string_equals_literal(lcname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
  2265. ce->constructor = fptr;
  2266. ce->constructor->common.fn_flags |= ZEND_ACC_CTOR;
  2267. } else if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME)) {
  2268. ce->destructor = fptr;
  2269. } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
  2270. ce->__get = fptr;
  2271. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  2272. } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
  2273. ce->__set = fptr;
  2274. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  2275. } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
  2276. ce->__call = fptr;
  2277. } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
  2278. ce->__unset = fptr;
  2279. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  2280. } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
  2281. ce->__isset = fptr;
  2282. ce->ce_flags |= ZEND_ACC_USE_GUARDS;
  2283. } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
  2284. ce->__callstatic = fptr;
  2285. } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME)) {
  2286. ce->__tostring = fptr;
  2287. } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME)) {
  2288. ce->__debugInfo = fptr;
  2289. } else if (zend_string_equals_literal(lcname, "__serialize")) {
  2290. ce->__serialize = fptr;
  2291. } else if (zend_string_equals_literal(lcname, "__unserialize")) {
  2292. ce->__unserialize = fptr;
  2293. }
  2294. }
  2295. ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arg_info_toString, 0, 0, IS_STRING, 0)
  2296. ZEND_END_ARG_INFO()
  2297. /* registers all functions in *library_functions in the function hash */
  2298. ZEND_API zend_result zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
  2299. {
  2300. const zend_function_entry *ptr = functions;
  2301. zend_function function, *reg_function;
  2302. zend_internal_function *internal_function = (zend_internal_function *)&function;
  2303. int count=0, unload=0;
  2304. HashTable *target_function_table = function_table;
  2305. int error_type;
  2306. zend_string *lowercase_name;
  2307. size_t fname_len;
  2308. if (type==MODULE_PERSISTENT) {
  2309. error_type = E_CORE_WARNING;
  2310. } else {
  2311. error_type = E_WARNING;
  2312. }
  2313. if (!target_function_table) {
  2314. target_function_table = CG(function_table);
  2315. }
  2316. internal_function->type = ZEND_INTERNAL_FUNCTION;
  2317. internal_function->module = EG(current_module);
  2318. memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
  2319. while (ptr->fname) {
  2320. fname_len = strlen(ptr->fname);
  2321. internal_function->handler = ptr->handler;
  2322. internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1);
  2323. internal_function->scope = scope;
  2324. internal_function->prototype = NULL;
  2325. internal_function->attributes = NULL;
  2326. if (ptr->flags) {
  2327. if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
  2328. if (ptr->flags != ZEND_ACC_DEPRECATED && scope) {
  2329. zend_error(error_type, "Invalid access level for %s%s%s() - access must be exactly one of public, protected or private", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  2330. }
  2331. internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
  2332. } else {
  2333. internal_function->fn_flags = ptr->flags;
  2334. }
  2335. } else {
  2336. internal_function->fn_flags = ZEND_ACC_PUBLIC;
  2337. }
  2338. if (ptr->arg_info) {
  2339. zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
  2340. internal_function->arg_info = (zend_internal_arg_info*)ptr->arg_info+1;
  2341. internal_function->num_args = ptr->num_args;
  2342. /* Currently you cannot denote that the function can accept less arguments than num_args */
  2343. if (info->required_num_args == (zend_uintptr_t)-1) {
  2344. internal_function->required_num_args = ptr->num_args;
  2345. } else {
  2346. internal_function->required_num_args = info->required_num_args;
  2347. }
  2348. if (ZEND_ARG_SEND_MODE(info)) {
  2349. internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
  2350. }
  2351. if (ZEND_ARG_IS_VARIADIC(&ptr->arg_info[ptr->num_args])) {
  2352. internal_function->fn_flags |= ZEND_ACC_VARIADIC;
  2353. /* Don't count the variadic argument */
  2354. internal_function->num_args--;
  2355. }
  2356. if (ZEND_TYPE_IS_SET(info->type)) {
  2357. if (ZEND_TYPE_HAS_NAME(info->type)) {
  2358. const char *type_name = ZEND_TYPE_LITERAL_NAME(info->type);
  2359. if (!scope && (!strcasecmp(type_name, "self") || !strcasecmp(type_name, "parent"))) {
  2360. zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", type_name);
  2361. }
  2362. }
  2363. internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
  2364. }
  2365. } else {
  2366. zend_error(E_CORE_WARNING, "Missing arginfo for %s%s%s()",
  2367. scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  2368. internal_function->arg_info = NULL;
  2369. internal_function->num_args = 0;
  2370. internal_function->required_num_args = 0;
  2371. }
  2372. /* If not specified, add __toString() return type for compatibility with Stringable
  2373. * interface. */
  2374. if (scope && zend_string_equals_literal_ci(internal_function->function_name, "__tostring") &&
  2375. !(internal_function->fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
  2376. internal_function->arg_info = (zend_internal_arg_info *) arg_info_toString + 1;
  2377. internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
  2378. internal_function->num_args = internal_function->required_num_args = 0;
  2379. }
  2380. zend_set_function_arg_flags((zend_function*)internal_function);
  2381. if (ptr->flags & ZEND_ACC_ABSTRACT) {
  2382. if (scope) {
  2383. /* This is a class that must be abstract itself. Here we set the check info. */
  2384. scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
  2385. if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
  2386. /* Since the class is not an interface it needs to be declared as a abstract class. */
  2387. /* Since here we are handling internal functions only we can add the keyword flag. */
  2388. /* This time we set the flag for the keyword 'abstract'. */
  2389. scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
  2390. }
  2391. }
  2392. if ((ptr->flags & ZEND_ACC_STATIC) && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
  2393. zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  2394. }
  2395. } else {
  2396. if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
  2397. zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
  2398. return FAILURE;
  2399. }
  2400. if (!internal_function->handler) {
  2401. zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  2402. zend_unregister_functions(functions, count, target_function_table);
  2403. return FAILURE;
  2404. }
  2405. }
  2406. lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
  2407. lowercase_name = zend_new_interned_string(lowercase_name);
  2408. reg_function = malloc(sizeof(zend_internal_function));
  2409. memcpy(reg_function, &function, sizeof(zend_internal_function));
  2410. if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
  2411. unload=1;
  2412. free(reg_function);
  2413. zend_string_release(lowercase_name);
  2414. break;
  2415. }
  2416. /* Get parameter count including variadic parameter. */
  2417. uint32_t num_args = reg_function->common.num_args;
  2418. if (reg_function->common.fn_flags & ZEND_ACC_VARIADIC) {
  2419. num_args++;
  2420. }
  2421. /* If types of arguments have to be checked */
  2422. if (reg_function->common.arg_info && num_args) {
  2423. uint32_t i;
  2424. for (i = 0; i < num_args; i++) {
  2425. zend_internal_arg_info *arg_info = &reg_function->internal_function.arg_info[i];
  2426. ZEND_ASSERT(arg_info->name && "Parameter must have a name");
  2427. if (ZEND_TYPE_IS_SET(arg_info->type)) {
  2428. reg_function->common.fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
  2429. }
  2430. #if ZEND_DEBUG
  2431. for (uint32_t j = 0; j < i; j++) {
  2432. if (!strcmp(arg_info->name, reg_function->internal_function.arg_info[j].name)) {
  2433. zend_error_noreturn(E_CORE_ERROR,
  2434. "Duplicate parameter name $%s for function %s%s%s()", arg_info->name,
  2435. scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  2436. }
  2437. }
  2438. #endif
  2439. }
  2440. }
  2441. if (reg_function->common.arg_info &&
  2442. (reg_function->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS))) {
  2443. /* convert "const char*" class type names into "zend_string*" */
  2444. uint32_t i;
  2445. zend_arg_info *arg_info = reg_function->common.arg_info - 1;
  2446. zend_arg_info *new_arg_info;
  2447. /* Treat return type as an extra argument */
  2448. num_args++;
  2449. new_arg_info = malloc(sizeof(zend_arg_info) * num_args);
  2450. memcpy(new_arg_info, arg_info, sizeof(zend_arg_info) * num_args);
  2451. reg_function->common.arg_info = new_arg_info + 1;
  2452. for (i = 0; i < num_args; i++) {
  2453. if (ZEND_TYPE_IS_COMPLEX(new_arg_info[i].type)) {
  2454. ZEND_ASSERT(ZEND_TYPE_HAS_NAME(new_arg_info[i].type)
  2455. && "Should be stored as simple name");
  2456. const char *class_name = ZEND_TYPE_LITERAL_NAME(new_arg_info[i].type);
  2457. size_t num_types = 1;
  2458. const char *p = class_name;
  2459. while ((p = strchr(p, '|'))) {
  2460. num_types++;
  2461. p++;
  2462. }
  2463. if (num_types == 1) {
  2464. /* Simple class type */
  2465. ZEND_TYPE_SET_PTR(new_arg_info[i].type,
  2466. zend_string_init_interned(class_name, strlen(class_name), 1));
  2467. } else {
  2468. /* Union type */
  2469. zend_type_list *list = malloc(ZEND_TYPE_LIST_SIZE(num_types));
  2470. list->num_types = num_types;
  2471. ZEND_TYPE_SET_LIST(new_arg_info[i].type, list);
  2472. ZEND_TYPE_FULL_MASK(new_arg_info[i].type) |= _ZEND_TYPE_UNION_BIT;
  2473. const char *start = class_name;
  2474. uint32_t j = 0;
  2475. while (true) {
  2476. const char *end = strchr(start, '|');
  2477. zend_string *str = zend_string_init_interned(
  2478. start, end ? end - start : strlen(start), 1);
  2479. list->types[j] = (zend_type) ZEND_TYPE_INIT_CLASS(str, 0, 0);
  2480. if (!end) {
  2481. break;
  2482. }
  2483. start = end + 1;
  2484. j++;
  2485. }
  2486. }
  2487. }
  2488. }
  2489. }
  2490. if (scope) {
  2491. zend_check_magic_method_implementation(
  2492. scope, reg_function, lowercase_name, E_CORE_ERROR);
  2493. zend_add_magic_method(scope, reg_function, lowercase_name);
  2494. }
  2495. ptr++;
  2496. count++;
  2497. zend_string_release(lowercase_name);
  2498. }
  2499. if (unload) { /* before unloading, display all remaining bad function in the module */
  2500. while (ptr->fname) {
  2501. fname_len = strlen(ptr->fname);
  2502. lowercase_name = zend_string_alloc(fname_len, 0);
  2503. zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
  2504. if (zend_hash_exists(target_function_table, lowercase_name)) {
  2505. zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  2506. }
  2507. zend_string_efree(lowercase_name);
  2508. ptr++;
  2509. }
  2510. zend_unregister_functions(functions, count, target_function_table);
  2511. return FAILURE;
  2512. }
  2513. return SUCCESS;
  2514. }
  2515. /* }}} */
  2516. /* count=-1 means erase all functions, otherwise,
  2517. * erase the first count functions
  2518. */
  2519. ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
  2520. {
  2521. const zend_function_entry *ptr = functions;
  2522. int i=0;
  2523. HashTable *target_function_table = function_table;
  2524. zend_string *lowercase_name;
  2525. size_t fname_len;
  2526. if (!target_function_table) {
  2527. target_function_table = CG(function_table);
  2528. }
  2529. while (ptr->fname) {
  2530. if (count!=-1 && i>=count) {
  2531. break;
  2532. }
  2533. fname_len = strlen(ptr->fname);
  2534. lowercase_name = zend_string_alloc(fname_len, 0);
  2535. zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
  2536. zend_hash_del(target_function_table, lowercase_name);
  2537. zend_string_efree(lowercase_name);
  2538. ptr++;
  2539. i++;
  2540. }
  2541. }
  2542. /* }}} */
  2543. ZEND_API zend_result zend_startup_module(zend_module_entry *module) /* {{{ */
  2544. {
  2545. if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
  2546. return SUCCESS;
  2547. }
  2548. return FAILURE;
  2549. }
  2550. /* }}} */
  2551. ZEND_API zend_result zend_get_module_started(const char *module_name) /* {{{ */
  2552. {
  2553. zend_module_entry *module;
  2554. module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
  2555. return (module && module->module_started) ? SUCCESS : FAILURE;
  2556. }
  2557. /* }}} */
  2558. static int clean_module_class(zval *el, void *arg) /* {{{ */
  2559. {
  2560. zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
  2561. int module_number = *(int *)arg;
  2562. if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
  2563. return ZEND_HASH_APPLY_REMOVE;
  2564. } else {
  2565. return ZEND_HASH_APPLY_KEEP;
  2566. }
  2567. }
  2568. /* }}} */
  2569. static void clean_module_classes(int module_number) /* {{{ */
  2570. {
  2571. zend_hash_apply_with_argument(EG(class_table), clean_module_class, (void *) &module_number);
  2572. }
  2573. /* }}} */
  2574. void module_destructor(zend_module_entry *module) /* {{{ */
  2575. {
  2576. #if ZEND_RC_DEBUG
  2577. bool orig_rc_debug = zend_rc_debug;
  2578. #endif
  2579. if (module->type == MODULE_TEMPORARY) {
  2580. #if ZEND_RC_DEBUG
  2581. /* FIXME: Loading extensions during the request breaks some invariants.
  2582. * In particular, it will create persistent interned strings, which is
  2583. * not allowed at this stage. */
  2584. zend_rc_debug = false;
  2585. #endif
  2586. zend_clean_module_rsrc_dtors(module->module_number);
  2587. clean_module_constants(module->module_number);
  2588. clean_module_classes(module->module_number);
  2589. }
  2590. if (module->module_started && module->module_shutdown_func) {
  2591. #if 0
  2592. zend_printf("%s: Module shutdown\n", module->name);
  2593. #endif
  2594. module->module_shutdown_func(module->type, module->module_number);
  2595. }
  2596. if (module->module_started
  2597. && !module->module_shutdown_func
  2598. && module->type == MODULE_TEMPORARY) {
  2599. zend_unregister_ini_entries_ex(module->module_number, module->type);
  2600. }
  2601. /* Deinitialize module globals */
  2602. if (module->globals_size) {
  2603. #ifdef ZTS
  2604. if (*module->globals_id_ptr) {
  2605. ts_free_id(*module->globals_id_ptr);
  2606. }
  2607. #else
  2608. if (module->globals_dtor) {
  2609. module->globals_dtor(module->globals_ptr);
  2610. }
  2611. #endif
  2612. }
  2613. module->module_started=0;
  2614. if (module->type == MODULE_TEMPORARY && module->functions) {
  2615. zend_unregister_functions(module->functions, -1, NULL);
  2616. }
  2617. #if HAVE_LIBDL
  2618. if (module->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
  2619. DL_UNLOAD(module->handle);
  2620. }
  2621. #endif
  2622. #if ZEND_RC_DEBUG
  2623. zend_rc_debug = orig_rc_debug;
  2624. #endif
  2625. }
  2626. /* }}} */
  2627. ZEND_API void zend_activate_modules(void) /* {{{ */
  2628. {
  2629. zend_module_entry **p = module_request_startup_handlers;
  2630. while (*p) {
  2631. zend_module_entry *module = *p;
  2632. if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
  2633. zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
  2634. exit(1);
  2635. }
  2636. p++;
  2637. }
  2638. }
  2639. /* }}} */
  2640. ZEND_API void zend_deactivate_modules(void) /* {{{ */
  2641. {
  2642. EG(current_execute_data) = NULL; /* we're no longer executing anything */
  2643. if (EG(full_tables_cleanup)) {
  2644. zend_module_entry *module;
  2645. ZEND_HASH_REVERSE_FOREACH_PTR(&module_registry, module) {
  2646. if (module->request_shutdown_func) {
  2647. zend_try {
  2648. module->request_shutdown_func(module->type, module->module_number);
  2649. } zend_end_try();
  2650. }
  2651. } ZEND_HASH_FOREACH_END();
  2652. } else {
  2653. zend_module_entry **p = module_request_shutdown_handlers;
  2654. while (*p) {
  2655. zend_module_entry *module = *p;
  2656. zend_try {
  2657. module->request_shutdown_func(module->type, module->module_number);
  2658. } zend_end_try();
  2659. p++;
  2660. }
  2661. }
  2662. }
  2663. /* }}} */
  2664. ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
  2665. {
  2666. if (EG(full_tables_cleanup)) {
  2667. zend_module_entry *module;
  2668. zval *zv;
  2669. zend_string *key;
  2670. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  2671. if (module->post_deactivate_func) {
  2672. module->post_deactivate_func();
  2673. }
  2674. } ZEND_HASH_FOREACH_END();
  2675. ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL(&module_registry, key, zv) {
  2676. module = Z_PTR_P(zv);
  2677. if (module->type != MODULE_TEMPORARY) {
  2678. break;
  2679. }
  2680. module_destructor(module);
  2681. free(module);
  2682. zend_string_release_ex(key, 0);
  2683. } ZEND_HASH_FOREACH_END_DEL();
  2684. } else {
  2685. zend_module_entry **p = module_post_deactivate_handlers;
  2686. while (*p) {
  2687. zend_module_entry *module = *p;
  2688. module->post_deactivate_func();
  2689. p++;
  2690. }
  2691. }
  2692. }
  2693. /* }}} */
  2694. /* return the next free module number */
  2695. ZEND_API int zend_next_free_module(void) /* {{{ */
  2696. {
  2697. return zend_hash_num_elements(&module_registry) + 1;
  2698. }
  2699. /* }}} */
  2700. static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
  2701. {
  2702. zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
  2703. zend_string *lowercase_name;
  2704. *class_entry = *orig_class_entry;
  2705. class_entry->type = ZEND_INTERNAL_CLASS;
  2706. zend_initialize_class_data(class_entry, 0);
  2707. zend_alloc_ce_cache(class_entry->name);
  2708. class_entry->ce_flags = orig_class_entry->ce_flags | ce_flags | ZEND_ACC_CONSTANTS_UPDATED | ZEND_ACC_LINKED | ZEND_ACC_RESOLVED_PARENT | ZEND_ACC_RESOLVED_INTERFACES;
  2709. class_entry->info.internal.module = EG(current_module);
  2710. if (class_entry->info.internal.builtin_functions) {
  2711. zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type);
  2712. }
  2713. lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT);
  2714. lowercase_name = zend_new_interned_string(lowercase_name);
  2715. zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
  2716. zend_string_release_ex(lowercase_name, 1);
  2717. if (class_entry->__tostring && !zend_string_equals_literal(class_entry->name, "Stringable")
  2718. && !(class_entry->ce_flags & ZEND_ACC_TRAIT)) {
  2719. ZEND_ASSERT(zend_ce_stringable
  2720. && "Should be registered before first class using __toString()");
  2721. zend_do_implement_interface(class_entry, zend_ce_stringable);
  2722. }
  2723. return class_entry;
  2724. }
  2725. /* }}} */
  2726. /* If parent_ce is not NULL then it inherits from parent_ce
  2727. * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
  2728. * If both parent_ce and parent_name are NULL it does a regular class registration
  2729. * If parent_name is specified but not found NULL is returned
  2730. */
  2731. ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
  2732. {
  2733. zend_class_entry *register_class;
  2734. register_class = zend_register_internal_class(class_entry);
  2735. if (parent_ce) {
  2736. zend_do_inheritance(register_class, parent_ce);
  2737. zend_build_properties_info_table(register_class);
  2738. }
  2739. return register_class;
  2740. }
  2741. /* }}} */
  2742. ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
  2743. {
  2744. zend_class_entry *interface_entry;
  2745. va_list interface_list;
  2746. va_start(interface_list, num_interfaces);
  2747. while (num_interfaces--) {
  2748. interface_entry = va_arg(interface_list, zend_class_entry *);
  2749. if (interface_entry == zend_ce_stringable
  2750. && zend_class_implements_interface(class_entry, zend_ce_stringable)) {
  2751. /* Stringable is implemented automatically,
  2752. * silently ignore an explicit implementation. */
  2753. continue;
  2754. }
  2755. zend_do_implement_interface(class_entry, interface_entry);
  2756. }
  2757. va_end(interface_list);
  2758. }
  2759. /* }}} */
  2760. /* A class that contains at least one abstract method automatically becomes an abstract class.
  2761. */
  2762. ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry) /* {{{ */
  2763. {
  2764. return do_register_internal_class(orig_class_entry, 0);
  2765. }
  2766. /* }}} */
  2767. ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry) /* {{{ */
  2768. {
  2769. return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
  2770. }
  2771. /* }}} */
  2772. ZEND_API zend_result zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, bool persistent) /* {{{ */
  2773. {
  2774. zend_string *lcname;
  2775. zval zv, *ret;
  2776. /* TODO: Move this out of here in 7.4. */
  2777. if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) {
  2778. persistent = 0;
  2779. }
  2780. if (name[0] == '\\') {
  2781. lcname = zend_string_alloc(name_len-1, persistent);
  2782. zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
  2783. } else {
  2784. lcname = zend_string_alloc(name_len, persistent);
  2785. zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
  2786. }
  2787. zend_assert_valid_class_name(lcname);
  2788. lcname = zend_new_interned_string(lcname);
  2789. ZVAL_ALIAS_PTR(&zv, ce);
  2790. ret = zend_hash_add(CG(class_table), lcname, &zv);
  2791. zend_string_release_ex(lcname, 0);
  2792. if (ret) {
  2793. if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
  2794. ce->refcount++;
  2795. }
  2796. return SUCCESS;
  2797. }
  2798. return FAILURE;
  2799. }
  2800. /* }}} */
  2801. // TODO num_symbol_tables as unsigned int?
  2802. ZEND_API zend_result zend_set_hash_symbol(zval *symbol, const char *name, size_t name_length, bool is_ref, int num_symbol_tables, ...) /* {{{ */
  2803. {
  2804. HashTable *symbol_table;
  2805. va_list symbol_table_list;
  2806. if (num_symbol_tables <= 0) return FAILURE;
  2807. if (is_ref) {
  2808. ZVAL_MAKE_REF(symbol);
  2809. }
  2810. va_start(symbol_table_list, num_symbol_tables);
  2811. while (num_symbol_tables-- > 0) {
  2812. symbol_table = va_arg(symbol_table_list, HashTable *);
  2813. zend_hash_str_update(symbol_table, name, name_length, symbol);
  2814. Z_TRY_ADDREF_P(symbol);
  2815. }
  2816. va_end(symbol_table_list);
  2817. return SUCCESS;
  2818. }
  2819. /* }}} */
  2820. /* Disabled functions support */
  2821. static void zend_disable_function(const char *function_name, size_t function_name_length)
  2822. {
  2823. zend_hash_str_del(CG(function_table), function_name, function_name_length);
  2824. }
  2825. ZEND_API void zend_disable_functions(const char *function_list) /* {{{ */
  2826. {
  2827. if (!function_list || !*function_list) {
  2828. return;
  2829. }
  2830. const char *s = NULL, *e = function_list;
  2831. while (*e) {
  2832. switch (*e) {
  2833. case ' ':
  2834. case ',':
  2835. if (s) {
  2836. zend_disable_function(s, e - s);
  2837. s = NULL;
  2838. }
  2839. break;
  2840. default:
  2841. if (!s) {
  2842. s = e;
  2843. }
  2844. break;
  2845. }
  2846. e++;
  2847. }
  2848. if (s) {
  2849. zend_disable_function(s, e - s);
  2850. }
  2851. /* Rehash the function table after deleting functions. This ensures that all internal
  2852. * functions are contiguous, which means we don't need to perform full table cleanup
  2853. * on shutdown. */
  2854. zend_hash_rehash(CG(function_table));
  2855. }
  2856. /* }}} */
  2857. #ifdef ZEND_WIN32
  2858. #pragma optimize("", off)
  2859. #endif
  2860. static ZEND_COLD zend_object *display_disabled_class(zend_class_entry *class_type) /* {{{ */
  2861. {
  2862. zend_object *intern;
  2863. intern = zend_objects_new(class_type);
  2864. /* Initialize default properties */
  2865. if (EXPECTED(class_type->default_properties_count != 0)) {
  2866. zval *p = intern->properties_table;
  2867. zval *end = p + class_type->default_properties_count;
  2868. do {
  2869. ZVAL_UNDEF(p);
  2870. p++;
  2871. } while (p != end);
  2872. }
  2873. zend_error(E_WARNING, "%s() has been disabled for security reasons", ZSTR_VAL(class_type->name));
  2874. return intern;
  2875. }
  2876. #ifdef ZEND_WIN32
  2877. #pragma optimize("", on)
  2878. #endif
  2879. /* }}} */
  2880. static const zend_function_entry disabled_class_new[] = {
  2881. ZEND_FE_END
  2882. };
  2883. ZEND_API zend_result zend_disable_class(const char *class_name, size_t class_name_length) /* {{{ */
  2884. {
  2885. zend_class_entry *disabled_class;
  2886. zend_string *key;
  2887. zend_function *fn;
  2888. zend_property_info *prop;
  2889. key = zend_string_alloc(class_name_length, 0);
  2890. zend_str_tolower_copy(ZSTR_VAL(key), class_name, class_name_length);
  2891. disabled_class = zend_hash_find_ptr(CG(class_table), key);
  2892. zend_string_release_ex(key, 0);
  2893. if (!disabled_class) {
  2894. return FAILURE;
  2895. }
  2896. /* Will be reset by INIT_CLASS_ENTRY. */
  2897. free(disabled_class->interfaces);
  2898. INIT_CLASS_ENTRY_INIT_METHODS((*disabled_class), disabled_class_new);
  2899. disabled_class->create_object = display_disabled_class;
  2900. ZEND_HASH_FOREACH_PTR(&disabled_class->function_table, fn) {
  2901. if ((fn->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS)) &&
  2902. fn->common.scope == disabled_class) {
  2903. zend_free_internal_arg_info(&fn->internal_function);
  2904. }
  2905. } ZEND_HASH_FOREACH_END();
  2906. zend_hash_clean(&disabled_class->function_table);
  2907. ZEND_HASH_FOREACH_PTR(&disabled_class->properties_info, prop) {
  2908. if (prop->ce == disabled_class) {
  2909. zend_string_release(prop->name);
  2910. zend_type_release(prop->type, /* persistent */ 1);
  2911. free(prop);
  2912. }
  2913. } ZEND_HASH_FOREACH_END();
  2914. zend_hash_clean(&disabled_class->properties_info);
  2915. return SUCCESS;
  2916. }
  2917. /* }}} */
  2918. static zend_always_inline zend_class_entry *get_scope(zend_execute_data *frame)
  2919. {
  2920. return frame && frame->func ? frame->func->common.scope : NULL;
  2921. }
  2922. static bool zend_is_callable_check_class(zend_string *name, zend_class_entry *scope, zend_execute_data *frame, zend_fcall_info_cache *fcc, bool *strict_class, char **error) /* {{{ */
  2923. {
  2924. bool ret = 0;
  2925. zend_class_entry *ce;
  2926. size_t name_len = ZSTR_LEN(name);
  2927. zend_string *lcname;
  2928. ALLOCA_FLAG(use_heap);
  2929. ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
  2930. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
  2931. *strict_class = 0;
  2932. if (zend_string_equals_literal(lcname, "self")) {
  2933. if (!scope) {
  2934. if (error) *error = estrdup("cannot access \"self\" when no class scope is active");
  2935. } else {
  2936. fcc->called_scope = zend_get_called_scope(frame);
  2937. if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope)) {
  2938. fcc->called_scope = scope;
  2939. }
  2940. fcc->calling_scope = scope;
  2941. if (!fcc->object) {
  2942. fcc->object = zend_get_this_object(frame);
  2943. }
  2944. ret = 1;
  2945. }
  2946. } else if (zend_string_equals_literal(lcname, "parent")) {
  2947. if (!scope) {
  2948. if (error) *error = estrdup("cannot access \"parent\" when no class scope is active");
  2949. } else if (!scope->parent) {
  2950. if (error) *error = estrdup("cannot access \"parent\" when current class scope has no parent");
  2951. } else {
  2952. fcc->called_scope = zend_get_called_scope(frame);
  2953. if (!fcc->called_scope || !instanceof_function(fcc->called_scope, scope->parent)) {
  2954. fcc->called_scope = scope->parent;
  2955. }
  2956. fcc->calling_scope = scope->parent;
  2957. if (!fcc->object) {
  2958. fcc->object = zend_get_this_object(frame);
  2959. }
  2960. *strict_class = 1;
  2961. ret = 1;
  2962. }
  2963. } else if (zend_string_equals_literal(lcname, "static")) {
  2964. zend_class_entry *called_scope = zend_get_called_scope(frame);
  2965. if (!called_scope) {
  2966. if (error) *error = estrdup("cannot access \"static\" when no class scope is active");
  2967. } else {
  2968. fcc->called_scope = called_scope;
  2969. fcc->calling_scope = called_scope;
  2970. if (!fcc->object) {
  2971. fcc->object = zend_get_this_object(frame);
  2972. }
  2973. *strict_class = 1;
  2974. ret = 1;
  2975. }
  2976. } else if ((ce = zend_lookup_class(name)) != NULL) {
  2977. zend_class_entry *scope = get_scope(frame);
  2978. fcc->calling_scope = ce;
  2979. if (scope && !fcc->object) {
  2980. zend_object *object = zend_get_this_object(frame);
  2981. if (object &&
  2982. instanceof_function(object->ce, scope) &&
  2983. instanceof_function(scope, ce)) {
  2984. fcc->object = object;
  2985. fcc->called_scope = object->ce;
  2986. } else {
  2987. fcc->called_scope = ce;
  2988. }
  2989. } else {
  2990. fcc->called_scope = fcc->object ? fcc->object->ce : ce;
  2991. }
  2992. *strict_class = 1;
  2993. ret = 1;
  2994. } else {
  2995. if (error) zend_spprintf(error, 0, "class \"%.*s\" not found", (int)name_len, ZSTR_VAL(name));
  2996. }
  2997. ZSTR_ALLOCA_FREE(lcname, use_heap);
  2998. return ret;
  2999. }
  3000. /* }}} */
  3001. ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) {
  3002. if (fcc->function_handler &&
  3003. (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  3004. if (fcc->function_handler->common.function_name) {
  3005. zend_string_release_ex(fcc->function_handler->common.function_name, 0);
  3006. }
  3007. zend_free_trampoline(fcc->function_handler);
  3008. fcc->function_handler = NULL;
  3009. }
  3010. }
  3011. static zend_always_inline bool zend_is_callable_check_func(int check_flags, zval *callable, zend_execute_data *frame, zend_fcall_info_cache *fcc, bool strict_class, char **error) /* {{{ */
  3012. {
  3013. zend_class_entry *ce_org = fcc->calling_scope;
  3014. bool retval = 0;
  3015. zend_string *mname, *cname;
  3016. zend_string *lmname;
  3017. const char *colon;
  3018. size_t clen;
  3019. HashTable *ftable;
  3020. int call_via_handler = 0;
  3021. zend_class_entry *scope;
  3022. zval *zv;
  3023. ALLOCA_FLAG(use_heap)
  3024. fcc->calling_scope = NULL;
  3025. if (!ce_org) {
  3026. zend_function *func;
  3027. zend_string *lmname;
  3028. /* Check if function with given name exists.
  3029. * This may be a compound name that includes namespace name */
  3030. if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
  3031. /* Skip leading \ */
  3032. ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap);
  3033. zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1);
  3034. func = zend_fetch_function(lmname);
  3035. ZSTR_ALLOCA_FREE(lmname, use_heap);
  3036. } else {
  3037. lmname = Z_STR_P(callable);
  3038. func = zend_fetch_function(lmname);
  3039. if (!func) {
  3040. ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap);
  3041. zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable));
  3042. func = zend_fetch_function(lmname);
  3043. ZSTR_ALLOCA_FREE(lmname, use_heap);
  3044. }
  3045. }
  3046. if (EXPECTED(func != NULL)) {
  3047. fcc->function_handler = func;
  3048. return 1;
  3049. }
  3050. }
  3051. /* Split name into class/namespace and method/function names */
  3052. if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
  3053. colon > Z_STRVAL_P(callable) &&
  3054. *(colon-1) == ':'
  3055. ) {
  3056. size_t mlen;
  3057. colon--;
  3058. clen = colon - Z_STRVAL_P(callable);
  3059. mlen = Z_STRLEN_P(callable) - clen - 2;
  3060. if (colon == Z_STRVAL_P(callable)) {
  3061. if (error) *error = estrdup("invalid function name");
  3062. return 0;
  3063. }
  3064. /* This is a compound name.
  3065. * Try to fetch class and then find static method. */
  3066. if (ce_org) {
  3067. scope = ce_org;
  3068. } else {
  3069. scope = get_scope(frame);
  3070. }
  3071. cname = zend_string_init_interned(Z_STRVAL_P(callable), clen, 0);
  3072. if (ZSTR_HAS_CE_CACHE(cname) && ZSTR_GET_CE_CACHE(cname)) {
  3073. fcc->calling_scope = ZSTR_GET_CE_CACHE(cname);
  3074. if (scope && !fcc->object) {
  3075. zend_object *object = zend_get_this_object(frame);
  3076. if (object &&
  3077. instanceof_function(object->ce, scope) &&
  3078. instanceof_function(scope, fcc->calling_scope)) {
  3079. fcc->object = object;
  3080. fcc->called_scope = object->ce;
  3081. } else {
  3082. fcc->called_scope = fcc->calling_scope;
  3083. }
  3084. } else {
  3085. fcc->called_scope = fcc->object ? fcc->object->ce : fcc->calling_scope;
  3086. }
  3087. strict_class = 1;
  3088. } else if (!zend_is_callable_check_class(cname, scope, frame, fcc, &strict_class, error)) {
  3089. zend_string_release_ex(cname, 0);
  3090. return 0;
  3091. }
  3092. zend_string_release_ex(cname, 0);
  3093. ftable = &fcc->calling_scope->function_table;
  3094. if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
  3095. if (error) zend_spprintf(error, 0, "class %s is not a subclass of %s", ZSTR_VAL(ce_org->name), ZSTR_VAL(fcc->calling_scope->name));
  3096. return 0;
  3097. }
  3098. mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
  3099. } else if (ce_org) {
  3100. /* Try to fetch find static method of given class. */
  3101. mname = Z_STR_P(callable);
  3102. zend_string_addref(mname);
  3103. ftable = &ce_org->function_table;
  3104. fcc->calling_scope = ce_org;
  3105. } else {
  3106. /* We already checked for plain function before. */
  3107. if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
  3108. zend_spprintf(error, 0, "function \"%s\" not found or invalid function name", Z_STRVAL_P(callable));
  3109. }
  3110. return 0;
  3111. }
  3112. lmname = zend_string_tolower(mname);
  3113. if (strict_class &&
  3114. fcc->calling_scope &&
  3115. zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
  3116. fcc->function_handler = fcc->calling_scope->constructor;
  3117. if (fcc->function_handler) {
  3118. retval = 1;
  3119. }
  3120. } else if ((zv = zend_hash_find(ftable, lmname)) != NULL) {
  3121. fcc->function_handler = Z_PTR_P(zv);
  3122. retval = 1;
  3123. if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
  3124. !strict_class) {
  3125. scope = get_scope(frame);
  3126. if (scope &&
  3127. instanceof_function(fcc->function_handler->common.scope, scope)) {
  3128. zv = zend_hash_find(&scope->function_table, lmname);
  3129. if (zv != NULL) {
  3130. zend_function *priv_fbc = Z_PTR_P(zv);
  3131. if ((priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE)
  3132. && priv_fbc->common.scope == scope) {
  3133. fcc->function_handler = priv_fbc;
  3134. }
  3135. }
  3136. }
  3137. }
  3138. if (!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC) &&
  3139. (fcc->calling_scope &&
  3140. ((fcc->object && fcc->calling_scope->__call) ||
  3141. (!fcc->object && fcc->calling_scope->__callstatic)))) {
  3142. scope = get_scope(frame);
  3143. if (fcc->function_handler->common.scope != scope) {
  3144. if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
  3145. || !zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope)) {
  3146. retval = 0;
  3147. fcc->function_handler = NULL;
  3148. goto get_function_via_handler;
  3149. }
  3150. }
  3151. }
  3152. } else {
  3153. get_function_via_handler:
  3154. if (fcc->object && fcc->calling_scope == ce_org) {
  3155. if (strict_class && ce_org->__call) {
  3156. fcc->function_handler = zend_get_call_trampoline_func(ce_org, mname, 0);
  3157. call_via_handler = 1;
  3158. retval = 1;
  3159. } else {
  3160. fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
  3161. if (fcc->function_handler) {
  3162. if (strict_class &&
  3163. (!fcc->function_handler->common.scope ||
  3164. !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
  3165. zend_release_fcall_info_cache(fcc);
  3166. } else {
  3167. retval = 1;
  3168. call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
  3169. }
  3170. }
  3171. }
  3172. } else if (fcc->calling_scope) {
  3173. if (fcc->calling_scope->get_static_method) {
  3174. fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
  3175. } else {
  3176. fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
  3177. }
  3178. if (fcc->function_handler) {
  3179. retval = 1;
  3180. call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
  3181. if (call_via_handler && !fcc->object) {
  3182. zend_object *object = zend_get_this_object(frame);
  3183. if (object &&
  3184. instanceof_function(object->ce, fcc->calling_scope)) {
  3185. fcc->object = object;
  3186. }
  3187. }
  3188. }
  3189. }
  3190. }
  3191. if (retval) {
  3192. if (fcc->calling_scope && !call_via_handler) {
  3193. if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) {
  3194. retval = 0;
  3195. if (error) {
  3196. zend_spprintf(error, 0, "cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
  3197. }
  3198. } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
  3199. retval = 0;
  3200. if (error) {
  3201. zend_spprintf(error, 0, "non-static method %s::%s() cannot be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
  3202. }
  3203. }
  3204. if (retval
  3205. && !(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)) {
  3206. scope = get_scope(frame);
  3207. if (fcc->function_handler->common.scope != scope) {
  3208. if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
  3209. || (!zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope))) {
  3210. if (error) {
  3211. if (*error) {
  3212. efree(*error);
  3213. }
  3214. zend_spprintf(error, 0, "cannot access %s method %s::%s()", zend_visibility_string(fcc->function_handler->common.fn_flags), ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
  3215. }
  3216. retval = 0;
  3217. }
  3218. }
  3219. }
  3220. }
  3221. } else if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
  3222. if (fcc->calling_scope) {
  3223. if (error) zend_spprintf(error, 0, "class %s does not have a method \"%s\"", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
  3224. } else {
  3225. if (error) zend_spprintf(error, 0, "function %s() does not exist", ZSTR_VAL(mname));
  3226. }
  3227. }
  3228. zend_string_release_ex(lmname, 0);
  3229. zend_string_release_ex(mname, 0);
  3230. if (fcc->object) {
  3231. fcc->called_scope = fcc->object->ce;
  3232. if (fcc->function_handler
  3233. && (fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
  3234. fcc->object = NULL;
  3235. }
  3236. }
  3237. return retval;
  3238. }
  3239. /* }}} */
  3240. ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object) /* {{{ */
  3241. {
  3242. try_again:
  3243. switch (Z_TYPE_P(callable)) {
  3244. case IS_STRING:
  3245. if (object) {
  3246. return zend_create_member_string(object->ce->name, Z_STR_P(callable));
  3247. }
  3248. return zend_string_copy(Z_STR_P(callable));
  3249. case IS_ARRAY:
  3250. {
  3251. zval *method = NULL;
  3252. zval *obj = NULL;
  3253. if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
  3254. obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0);
  3255. method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1);
  3256. }
  3257. if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) {
  3258. return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
  3259. }
  3260. if (Z_TYPE_P(obj) == IS_STRING) {
  3261. return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method));
  3262. } else if (Z_TYPE_P(obj) == IS_OBJECT) {
  3263. return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
  3264. } else {
  3265. return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
  3266. }
  3267. }
  3268. case IS_OBJECT:
  3269. {
  3270. zend_class_entry *ce = Z_OBJCE_P(callable);
  3271. return zend_string_concat2(
  3272. ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
  3273. "::__invoke", sizeof("::__invoke") - 1);
  3274. }
  3275. case IS_REFERENCE:
  3276. callable = Z_REFVAL_P(callable);
  3277. goto try_again;
  3278. default:
  3279. return zval_get_string_func(callable);
  3280. }
  3281. }
  3282. /* }}} */
  3283. ZEND_API zend_string *zend_get_callable_name(zval *callable) /* {{{ */
  3284. {
  3285. return zend_get_callable_name_ex(callable, NULL);
  3286. }
  3287. /* }}} */
  3288. ZEND_API bool zend_is_callable_at_frame(
  3289. zval *callable, zend_object *object, zend_execute_data *frame,
  3290. uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */
  3291. {
  3292. bool ret;
  3293. zend_fcall_info_cache fcc_local;
  3294. bool strict_class = 0;
  3295. if (fcc == NULL) {
  3296. fcc = &fcc_local;
  3297. }
  3298. if (error) {
  3299. *error = NULL;
  3300. }
  3301. fcc->calling_scope = NULL;
  3302. fcc->called_scope = NULL;
  3303. fcc->function_handler = NULL;
  3304. fcc->object = NULL;
  3305. again:
  3306. switch (Z_TYPE_P(callable)) {
  3307. case IS_STRING:
  3308. if (object) {
  3309. fcc->object = object;
  3310. fcc->calling_scope = object->ce;
  3311. }
  3312. if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
  3313. fcc->called_scope = fcc->calling_scope;
  3314. return 1;
  3315. }
  3316. check_func:
  3317. ret = zend_is_callable_check_func(check_flags, callable, frame, fcc, strict_class, error);
  3318. if (fcc == &fcc_local) {
  3319. zend_release_fcall_info_cache(fcc);
  3320. }
  3321. return ret;
  3322. case IS_ARRAY:
  3323. {
  3324. zval *method = NULL;
  3325. zval *obj = NULL;
  3326. if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
  3327. obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
  3328. method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
  3329. }
  3330. do {
  3331. if (obj == NULL || method == NULL) {
  3332. break;
  3333. }
  3334. ZVAL_DEREF(method);
  3335. if (Z_TYPE_P(method) != IS_STRING) {
  3336. break;
  3337. }
  3338. ZVAL_DEREF(obj);
  3339. if (Z_TYPE_P(obj) == IS_STRING) {
  3340. if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
  3341. return 1;
  3342. }
  3343. if (!zend_is_callable_check_class(Z_STR_P(obj), get_scope(frame), frame, fcc, &strict_class, error)) {
  3344. return 0;
  3345. }
  3346. } else if (Z_TYPE_P(obj) == IS_OBJECT) {
  3347. fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
  3348. fcc->object = Z_OBJ_P(obj);
  3349. if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
  3350. fcc->called_scope = fcc->calling_scope;
  3351. return 1;
  3352. }
  3353. } else {
  3354. break;
  3355. }
  3356. callable = method;
  3357. goto check_func;
  3358. } while (0);
  3359. if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
  3360. if (!obj || (!Z_ISREF_P(obj)?
  3361. (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) :
  3362. (Z_TYPE_P(Z_REFVAL_P(obj)) != IS_STRING && Z_TYPE_P(Z_REFVAL_P(obj)) != IS_OBJECT))) {
  3363. if (error) *error = estrdup("first array member is not a valid class name or object");
  3364. } else {
  3365. if (error) *error = estrdup("second array member is not a valid method");
  3366. }
  3367. } else {
  3368. if (error) *error = estrdup("array must have exactly two members");
  3369. }
  3370. }
  3371. return 0;
  3372. case IS_OBJECT:
  3373. if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(Z_OBJ_P(callable), &fcc->calling_scope, &fcc->function_handler, &fcc->object, 1) == SUCCESS) {
  3374. fcc->called_scope = fcc->calling_scope;
  3375. if (fcc == &fcc_local) {
  3376. zend_release_fcall_info_cache(fcc);
  3377. }
  3378. return 1;
  3379. }
  3380. if (error) *error = estrdup("no array or string given");
  3381. return 0;
  3382. case IS_REFERENCE:
  3383. callable = Z_REFVAL_P(callable);
  3384. goto again;
  3385. default:
  3386. if (error) *error = estrdup("no array or string given");
  3387. return 0;
  3388. }
  3389. }
  3390. /* }}} */
  3391. ZEND_API bool zend_is_callable_ex(zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error) /* {{{ */
  3392. {
  3393. /* Determine callability at the first parent user frame. */
  3394. zend_execute_data *frame = EG(current_execute_data);
  3395. while (frame && (!frame->func || !ZEND_USER_CODE(frame->func->type))) {
  3396. frame = frame->prev_execute_data;
  3397. }
  3398. bool ret = zend_is_callable_at_frame(callable, object, frame, check_flags, fcc, error);
  3399. if (callable_name) {
  3400. *callable_name = zend_get_callable_name_ex(callable, object);
  3401. }
  3402. return ret;
  3403. }
  3404. ZEND_API bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */
  3405. {
  3406. return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
  3407. }
  3408. /* }}} */
  3409. ZEND_API bool zend_make_callable(zval *callable, zend_string **callable_name) /* {{{ */
  3410. {
  3411. zend_fcall_info_cache fcc;
  3412. if (zend_is_callable_ex(callable, NULL, 0, callable_name, &fcc, NULL)) {
  3413. if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) {
  3414. zval_ptr_dtor_str(callable);
  3415. array_init(callable);
  3416. add_next_index_str(callable, zend_string_copy(fcc.calling_scope->name));
  3417. add_next_index_str(callable, zend_string_copy(fcc.function_handler->common.function_name));
  3418. }
  3419. zend_release_fcall_info_cache(&fcc);
  3420. return 1;
  3421. }
  3422. return 0;
  3423. }
  3424. /* }}} */
  3425. ZEND_API zend_result zend_fcall_info_init(zval *callable, uint32_t check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error) /* {{{ */
  3426. {
  3427. if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
  3428. return FAILURE;
  3429. }
  3430. fci->size = sizeof(*fci);
  3431. fci->object = fcc->object;
  3432. ZVAL_COPY_VALUE(&fci->function_name, callable);
  3433. fci->retval = NULL;
  3434. fci->param_count = 0;
  3435. fci->params = NULL;
  3436. fci->named_params = NULL;
  3437. return SUCCESS;
  3438. }
  3439. /* }}} */
  3440. ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, bool free_mem) /* {{{ */
  3441. {
  3442. if (fci->params) {
  3443. zval *p = fci->params;
  3444. zval *end = p + fci->param_count;
  3445. while (p != end) {
  3446. i_zval_ptr_dtor(p);
  3447. p++;
  3448. }
  3449. if (free_mem) {
  3450. efree(fci->params);
  3451. fci->params = NULL;
  3452. }
  3453. }
  3454. fci->param_count = 0;
  3455. }
  3456. /* }}} */
  3457. ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, uint32_t *param_count, zval **params) /* {{{ */
  3458. {
  3459. *param_count = fci->param_count;
  3460. *params = fci->params;
  3461. fci->param_count = 0;
  3462. fci->params = NULL;
  3463. }
  3464. /* }}} */
  3465. ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, uint32_t param_count, zval *params) /* {{{ */
  3466. {
  3467. zend_fcall_info_args_clear(fci, 1);
  3468. fci->param_count = param_count;
  3469. fci->params = params;
  3470. }
  3471. /* }}} */
  3472. ZEND_API zend_result zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
  3473. {
  3474. zval *arg, *params;
  3475. uint32_t n = 1;
  3476. zend_fcall_info_args_clear(fci, !args);
  3477. if (!args) {
  3478. return SUCCESS;
  3479. }
  3480. if (Z_TYPE_P(args) != IS_ARRAY) {
  3481. return FAILURE;
  3482. }
  3483. fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
  3484. fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
  3485. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
  3486. if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
  3487. ZVAL_NEW_REF(params, arg);
  3488. Z_TRY_ADDREF_P(arg);
  3489. } else {
  3490. ZVAL_COPY(params, arg);
  3491. }
  3492. params++;
  3493. n++;
  3494. } ZEND_HASH_FOREACH_END();
  3495. return SUCCESS;
  3496. }
  3497. /* }}} */
  3498. ZEND_API zend_result zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
  3499. {
  3500. return zend_fcall_info_args_ex(fci, NULL, args);
  3501. }
  3502. /* }}} */
  3503. ZEND_API void zend_fcall_info_argp(zend_fcall_info *fci, uint32_t argc, zval *argv) /* {{{ */
  3504. {
  3505. zend_fcall_info_args_clear(fci, !argc);
  3506. if (argc) {
  3507. fci->param_count = argc;
  3508. fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
  3509. for (uint32_t i = 0; i < argc; ++i) {
  3510. ZVAL_COPY(&fci->params[i], &argv[i]);
  3511. }
  3512. }
  3513. }
  3514. /* }}} */
  3515. ZEND_API void zend_fcall_info_argv(zend_fcall_info *fci, uint32_t argc, va_list *argv) /* {{{ */
  3516. {
  3517. zend_fcall_info_args_clear(fci, !argc);
  3518. if (argc) {
  3519. zval *arg;
  3520. fci->param_count = argc;
  3521. fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
  3522. for (uint32_t i = 0; i < argc; ++i) {
  3523. arg = va_arg(*argv, zval *);
  3524. ZVAL_COPY(&fci->params[i], arg);
  3525. }
  3526. }
  3527. }
  3528. /* }}} */
  3529. ZEND_API void zend_fcall_info_argn(zend_fcall_info *fci, uint32_t argc, ...) /* {{{ */
  3530. {
  3531. va_list argv;
  3532. va_start(argv, argc);
  3533. zend_fcall_info_argv(fci, argc, &argv);
  3534. va_end(argv);
  3535. }
  3536. /* }}} */
  3537. ZEND_API zend_result zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
  3538. {
  3539. zval retval, *org_params = NULL;
  3540. uint32_t org_count = 0;
  3541. zend_result result;
  3542. fci->retval = retval_ptr ? retval_ptr : &retval;
  3543. if (args) {
  3544. zend_fcall_info_args_save(fci, &org_count, &org_params);
  3545. zend_fcall_info_args(fci, args);
  3546. }
  3547. result = zend_call_function(fci, fcc);
  3548. if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
  3549. zval_ptr_dtor(&retval);
  3550. }
  3551. if (args) {
  3552. zend_fcall_info_args_restore(fci, org_count, org_params);
  3553. }
  3554. return result;
  3555. }
  3556. /* }}} */
  3557. ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
  3558. {
  3559. zend_string *lname;
  3560. size_t name_len = strlen(module_name);
  3561. zend_module_entry *module;
  3562. lname = zend_string_alloc(name_len, 0);
  3563. zend_str_tolower_copy(ZSTR_VAL(lname), module_name, name_len);
  3564. module = zend_hash_find_ptr(&module_registry, lname);
  3565. zend_string_efree(lname);
  3566. return module ? module->version : NULL;
  3567. }
  3568. /* }}} */
  3569. static zend_always_inline bool is_persistent_class(zend_class_entry *ce) {
  3570. return (ce->type & ZEND_INTERNAL_CLASS)
  3571. && ce->info.internal.module->type == MODULE_PERSISTENT;
  3572. }
  3573. ZEND_API zend_property_info *zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_type type) /* {{{ */
  3574. {
  3575. zend_property_info *property_info, *property_info_ptr;
  3576. if (ZEND_TYPE_IS_SET(type)) {
  3577. ce->ce_flags |= ZEND_ACC_HAS_TYPE_HINTS;
  3578. }
  3579. if (ce->type == ZEND_INTERNAL_CLASS) {
  3580. property_info = pemalloc(sizeof(zend_property_info), 1);
  3581. } else {
  3582. property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
  3583. if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
  3584. ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
  3585. if (access_type & ZEND_ACC_STATIC) {
  3586. ce->ce_flags |= ZEND_ACC_HAS_AST_STATICS;
  3587. } else {
  3588. ce->ce_flags |= ZEND_ACC_HAS_AST_PROPERTIES;
  3589. }
  3590. }
  3591. }
  3592. if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) {
  3593. zval_make_interned_string(property);
  3594. }
  3595. if (!(access_type & ZEND_ACC_PPP_MASK)) {
  3596. access_type |= ZEND_ACC_PUBLIC;
  3597. }
  3598. if (access_type & ZEND_ACC_STATIC) {
  3599. if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
  3600. (property_info_ptr->flags & ZEND_ACC_STATIC) != 0) {
  3601. property_info->offset = property_info_ptr->offset;
  3602. zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
  3603. zend_hash_del(&ce->properties_info, name);
  3604. } else {
  3605. property_info->offset = ce->default_static_members_count++;
  3606. ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
  3607. }
  3608. ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
  3609. if (!ZEND_MAP_PTR(ce->static_members_table)) {
  3610. if (ce->type == ZEND_INTERNAL_CLASS &&
  3611. ce->info.internal.module->type == MODULE_PERSISTENT) {
  3612. ZEND_MAP_PTR_NEW(ce->static_members_table);
  3613. } else {
  3614. ZEND_MAP_PTR_INIT(ce->static_members_table,
  3615. zend_arena_alloc(&CG(arena), sizeof(zval **)));
  3616. ZEND_MAP_PTR_SET(ce->static_members_table, NULL);
  3617. }
  3618. }
  3619. } else {
  3620. zval *property_default_ptr;
  3621. if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
  3622. (property_info_ptr->flags & ZEND_ACC_STATIC) == 0) {
  3623. property_info->offset = property_info_ptr->offset;
  3624. zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
  3625. zend_hash_del(&ce->properties_info, name);
  3626. ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
  3627. ZEND_ASSERT(ce->properties_info_table != NULL);
  3628. ce->properties_info_table[OBJ_PROP_TO_NUM(property_info->offset)] = property_info;
  3629. } else {
  3630. property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
  3631. ce->default_properties_count++;
  3632. ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
  3633. /* For user classes this is handled during linking */
  3634. if (ce->type == ZEND_INTERNAL_CLASS) {
  3635. ce->properties_info_table = perealloc(ce->properties_info_table, sizeof(zend_property_info *) * ce->default_properties_count, 1);
  3636. ce->properties_info_table[ce->default_properties_count - 1] = property_info;
  3637. }
  3638. }
  3639. property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
  3640. ZVAL_COPY_VALUE(property_default_ptr, property);
  3641. Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0;
  3642. }
  3643. if (ce->type & ZEND_INTERNAL_CLASS) {
  3644. /* Must be interned to avoid ZTS data races */
  3645. if (is_persistent_class(ce)) {
  3646. name = zend_new_interned_string(zend_string_copy(name));
  3647. }
  3648. if (Z_REFCOUNTED_P(property)) {
  3649. zend_error_noreturn(E_CORE_ERROR, "Internal zvals cannot be refcounted");
  3650. }
  3651. }
  3652. if (access_type & ZEND_ACC_PUBLIC) {
  3653. property_info->name = zend_string_copy(name);
  3654. } else if (access_type & ZEND_ACC_PRIVATE) {
  3655. property_info->name = zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
  3656. } else {
  3657. ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
  3658. property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
  3659. }
  3660. property_info->name = zend_new_interned_string(property_info->name);
  3661. property_info->flags = access_type;
  3662. property_info->doc_comment = doc_comment;
  3663. property_info->attributes = NULL;
  3664. property_info->ce = ce;
  3665. property_info->type = type;
  3666. if (is_persistent_class(ce)) {
  3667. zend_type *single_type;
  3668. ZEND_TYPE_FOREACH(property_info->type, single_type) {
  3669. if (ZEND_TYPE_HAS_NAME(*single_type)) {
  3670. zend_string *name = zend_new_interned_string(ZEND_TYPE_NAME(*single_type));
  3671. ZEND_TYPE_SET_PTR(*single_type, name);
  3672. zend_alloc_ce_cache(name);
  3673. }
  3674. } ZEND_TYPE_FOREACH_END();
  3675. }
  3676. zend_hash_update_ptr(&ce->properties_info, name, property_info);
  3677. return property_info;
  3678. }
  3679. /* }}} */
  3680. ZEND_API zend_result zend_try_assign_typed_ref_ex(zend_reference *ref, zval *val, bool strict) /* {{{ */
  3681. {
  3682. if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, val, strict))) {
  3683. zval_ptr_dtor(val);
  3684. return FAILURE;
  3685. } else {
  3686. zval_ptr_dtor(&ref->val);
  3687. ZVAL_COPY_VALUE(&ref->val, val);
  3688. return SUCCESS;
  3689. }
  3690. }
  3691. /* }}} */
  3692. ZEND_API zend_result zend_try_assign_typed_ref(zend_reference *ref, zval *val) /* {{{ */
  3693. {
  3694. return zend_try_assign_typed_ref_ex(ref, val, ZEND_ARG_USES_STRICT_TYPES());
  3695. }
  3696. /* }}} */
  3697. ZEND_API zend_result zend_try_assign_typed_ref_null(zend_reference *ref) /* {{{ */
  3698. {
  3699. zval tmp;
  3700. ZVAL_NULL(&tmp);
  3701. return zend_try_assign_typed_ref(ref, &tmp);
  3702. }
  3703. /* }}} */
  3704. ZEND_API zend_result zend_try_assign_typed_ref_bool(zend_reference *ref, bool val) /* {{{ */
  3705. {
  3706. zval tmp;
  3707. ZVAL_BOOL(&tmp, val);
  3708. return zend_try_assign_typed_ref(ref, &tmp);
  3709. }
  3710. /* }}} */
  3711. ZEND_API zend_result zend_try_assign_typed_ref_long(zend_reference *ref, zend_long lval) /* {{{ */
  3712. {
  3713. zval tmp;
  3714. ZVAL_LONG(&tmp, lval);
  3715. return zend_try_assign_typed_ref(ref, &tmp);
  3716. }
  3717. /* }}} */
  3718. ZEND_API zend_result zend_try_assign_typed_ref_double(zend_reference *ref, double dval) /* {{{ */
  3719. {
  3720. zval tmp;
  3721. ZVAL_DOUBLE(&tmp, dval);
  3722. return zend_try_assign_typed_ref(ref, &tmp);
  3723. }
  3724. /* }}} */
  3725. ZEND_API zend_result zend_try_assign_typed_ref_empty_string(zend_reference *ref) /* {{{ */
  3726. {
  3727. zval tmp;
  3728. ZVAL_EMPTY_STRING(&tmp);
  3729. return zend_try_assign_typed_ref(ref, &tmp);
  3730. }
  3731. /* }}} */
  3732. ZEND_API zend_result zend_try_assign_typed_ref_str(zend_reference *ref, zend_string *str) /* {{{ */
  3733. {
  3734. zval tmp;
  3735. ZVAL_STR(&tmp, str);
  3736. return zend_try_assign_typed_ref(ref, &tmp);
  3737. }
  3738. /* }}} */
  3739. ZEND_API zend_result zend_try_assign_typed_ref_string(zend_reference *ref, const char *string) /* {{{ */
  3740. {
  3741. zval tmp;
  3742. ZVAL_STRING(&tmp, string);
  3743. return zend_try_assign_typed_ref(ref, &tmp);
  3744. }
  3745. /* }}} */
  3746. ZEND_API zend_result zend_try_assign_typed_ref_stringl(zend_reference *ref, const char *string, size_t len) /* {{{ */
  3747. {
  3748. zval tmp;
  3749. ZVAL_STRINGL(&tmp, string, len);
  3750. return zend_try_assign_typed_ref(ref, &tmp);
  3751. }
  3752. /* }}} */
  3753. ZEND_API zend_result zend_try_assign_typed_ref_arr(zend_reference *ref, zend_array *arr) /* {{{ */
  3754. {
  3755. zval tmp;
  3756. ZVAL_ARR(&tmp, arr);
  3757. return zend_try_assign_typed_ref(ref, &tmp);
  3758. }
  3759. /* }}} */
  3760. ZEND_API zend_result zend_try_assign_typed_ref_res(zend_reference *ref, zend_resource *res) /* {{{ */
  3761. {
  3762. zval tmp;
  3763. ZVAL_RES(&tmp, res);
  3764. return zend_try_assign_typed_ref(ref, &tmp);
  3765. }
  3766. /* }}} */
  3767. ZEND_API zend_result zend_try_assign_typed_ref_zval(zend_reference *ref, zval *zv) /* {{{ */
  3768. {
  3769. zval tmp;
  3770. ZVAL_COPY_VALUE(&tmp, zv);
  3771. return zend_try_assign_typed_ref(ref, &tmp);
  3772. }
  3773. /* }}} */
  3774. ZEND_API zend_result zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, bool strict) /* {{{ */
  3775. {
  3776. zval tmp;
  3777. ZVAL_COPY_VALUE(&tmp, zv);
  3778. return zend_try_assign_typed_ref_ex(ref, &tmp, strict);
  3779. }
  3780. /* }}} */
  3781. ZEND_API void zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
  3782. {
  3783. zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
  3784. }
  3785. /* }}} */
  3786. ZEND_API void zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
  3787. {
  3788. zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce));
  3789. zend_declare_property_ex(ce, key, property, access_type, NULL);
  3790. zend_string_release(key);
  3791. }
  3792. /* }}} */
  3793. ZEND_API void zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */
  3794. {
  3795. zval property;
  3796. ZVAL_NULL(&property);
  3797. zend_declare_property(ce, name, name_length, &property, access_type);
  3798. }
  3799. /* }}} */
  3800. ZEND_API void zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
  3801. {
  3802. zval property;
  3803. ZVAL_BOOL(&property, value);
  3804. zend_declare_property(ce, name, name_length, &property, access_type);
  3805. }
  3806. /* }}} */
  3807. ZEND_API void zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
  3808. {
  3809. zval property;
  3810. ZVAL_LONG(&property, value);
  3811. zend_declare_property(ce, name, name_length, &property, access_type);
  3812. }
  3813. /* }}} */
  3814. ZEND_API void zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */
  3815. {
  3816. zval property;
  3817. ZVAL_DOUBLE(&property, value);
  3818. zend_declare_property(ce, name, name_length, &property, access_type);
  3819. }
  3820. /* }}} */
  3821. ZEND_API void zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */
  3822. {
  3823. zval property;
  3824. ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
  3825. zend_declare_property(ce, name, name_length, &property, access_type);
  3826. }
  3827. /* }}} */
  3828. ZEND_API void zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type) /* {{{ */
  3829. {
  3830. zval property;
  3831. ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type & ZEND_INTERNAL_CLASS));
  3832. zend_declare_property(ce, name, name_length, &property, access_type);
  3833. }
  3834. /* }}} */
  3835. ZEND_API zend_class_constant *zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int flags, zend_string *doc_comment) /* {{{ */
  3836. {
  3837. zend_class_constant *c;
  3838. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  3839. if (!(flags & ZEND_ACC_PUBLIC)) {
  3840. zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3841. }
  3842. }
  3843. if (zend_string_equals_literal_ci(name, "class")) {
  3844. zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
  3845. "A class constant must not be called 'class'; it is reserved for class name fetching");
  3846. }
  3847. if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
  3848. zval_make_interned_string(value);
  3849. }
  3850. if (ce->type == ZEND_INTERNAL_CLASS) {
  3851. c = pemalloc(sizeof(zend_class_constant), 1);
  3852. } else {
  3853. c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
  3854. }
  3855. ZVAL_COPY_VALUE(&c->value, value);
  3856. ZEND_CLASS_CONST_FLAGS(c) = flags;
  3857. c->doc_comment = doc_comment;
  3858. c->attributes = NULL;
  3859. c->ce = ce;
  3860. if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
  3861. ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
  3862. ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
  3863. if (ce->type == ZEND_INTERNAL_CLASS && !ZEND_MAP_PTR(ce->mutable_data)) {
  3864. ZEND_MAP_PTR_NEW(ce->mutable_data);
  3865. }
  3866. }
  3867. if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
  3868. zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
  3869. "Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3870. }
  3871. return c;
  3872. }
  3873. /* }}} */
  3874. ZEND_API void zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
  3875. {
  3876. zend_string *key;
  3877. if (ce->type == ZEND_INTERNAL_CLASS) {
  3878. key = zend_string_init_interned(name, name_length, 1);
  3879. } else {
  3880. key = zend_string_init(name, name_length, 0);
  3881. }
  3882. zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
  3883. zend_string_release(key);
  3884. }
  3885. /* }}} */
  3886. ZEND_API void zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
  3887. {
  3888. zval constant;
  3889. ZVAL_NULL(&constant);
  3890. zend_declare_class_constant(ce, name, name_length, &constant);
  3891. }
  3892. /* }}} */
  3893. ZEND_API void zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
  3894. {
  3895. zval constant;
  3896. ZVAL_LONG(&constant, value);
  3897. zend_declare_class_constant(ce, name, name_length, &constant);
  3898. }
  3899. /* }}} */
  3900. ZEND_API void zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, bool value) /* {{{ */
  3901. {
  3902. zval constant;
  3903. ZVAL_BOOL(&constant, value);
  3904. zend_declare_class_constant(ce, name, name_length, &constant);
  3905. }
  3906. /* }}} */
  3907. ZEND_API void zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
  3908. {
  3909. zval constant;
  3910. ZVAL_DOUBLE(&constant, value);
  3911. zend_declare_class_constant(ce, name, name_length, &constant);
  3912. }
  3913. /* }}} */
  3914. ZEND_API void zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length) /* {{{ */
  3915. {
  3916. zval constant;
  3917. ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
  3918. zend_declare_class_constant(ce, name, name_length, &constant);
  3919. }
  3920. /* }}} */
  3921. ZEND_API void zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */
  3922. {
  3923. zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
  3924. }
  3925. /* }}} */
  3926. ZEND_API void zend_update_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, zval *value) /* {{{ */
  3927. {
  3928. zend_class_entry *old_scope = EG(fake_scope);
  3929. EG(fake_scope) = scope;
  3930. object->handlers->write_property(object, name, value, NULL);
  3931. EG(fake_scope) = old_scope;
  3932. }
  3933. /* }}} */
  3934. ZEND_API void zend_update_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zval *value) /* {{{ */
  3935. {
  3936. zend_string *property;
  3937. zend_class_entry *old_scope = EG(fake_scope);
  3938. EG(fake_scope) = scope;
  3939. property = zend_string_init(name, name_length, 0);
  3940. object->handlers->write_property(object, property, value, NULL);
  3941. zend_string_release_ex(property, 0);
  3942. EG(fake_scope) = old_scope;
  3943. }
  3944. /* }}} */
  3945. ZEND_API void zend_update_property_null(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
  3946. {
  3947. zval tmp;
  3948. ZVAL_NULL(&tmp);
  3949. zend_update_property(scope, object, name, name_length, &tmp);
  3950. }
  3951. /* }}} */
  3952. ZEND_API void zend_unset_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length) /* {{{ */
  3953. {
  3954. zend_string *property;
  3955. zend_class_entry *old_scope = EG(fake_scope);
  3956. EG(fake_scope) = scope;
  3957. property = zend_string_init(name, name_length, 0);
  3958. object->handlers->unset_property(object, property, 0);
  3959. zend_string_release_ex(property, 0);
  3960. EG(fake_scope) = old_scope;
  3961. }
  3962. /* }}} */
  3963. ZEND_API void zend_update_property_bool(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */
  3964. {
  3965. zval tmp;
  3966. ZVAL_BOOL(&tmp, value);
  3967. zend_update_property(scope, object, name, name_length, &tmp);
  3968. }
  3969. /* }}} */
  3970. ZEND_API void zend_update_property_long(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_long value) /* {{{ */
  3971. {
  3972. zval tmp;
  3973. ZVAL_LONG(&tmp, value);
  3974. zend_update_property(scope, object, name, name_length, &tmp);
  3975. }
  3976. /* }}} */
  3977. ZEND_API void zend_update_property_double(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, double value) /* {{{ */
  3978. {
  3979. zval tmp;
  3980. ZVAL_DOUBLE(&tmp, value);
  3981. zend_update_property(scope, object, name, name_length, &tmp);
  3982. }
  3983. /* }}} */
  3984. ZEND_API void zend_update_property_str(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, zend_string *value) /* {{{ */
  3985. {
  3986. zval tmp;
  3987. ZVAL_STR(&tmp, value);
  3988. zend_update_property(scope, object, name, name_length, &tmp);
  3989. }
  3990. /* }}} */
  3991. ZEND_API void zend_update_property_string(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value) /* {{{ */
  3992. {
  3993. zval tmp;
  3994. ZVAL_STRING(&tmp, value);
  3995. Z_SET_REFCOUNT(tmp, 0);
  3996. zend_update_property(scope, object, name, name_length, &tmp);
  3997. }
  3998. /* }}} */
  3999. ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
  4000. {
  4001. zval tmp;
  4002. ZVAL_STRINGL(&tmp, value, value_len);
  4003. Z_SET_REFCOUNT(tmp, 0);
  4004. zend_update_property(scope, object, name, name_length, &tmp);
  4005. }
  4006. /* }}} */
  4007. ZEND_API zend_result zend_update_static_property_ex(zend_class_entry *scope, zend_string *name, zval *value) /* {{{ */
  4008. {
  4009. zval *property, tmp;
  4010. zend_property_info *prop_info;
  4011. zend_class_entry *old_scope = EG(fake_scope);
  4012. if (UNEXPECTED(!(scope->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
  4013. if (UNEXPECTED(zend_update_class_constants(scope)) != SUCCESS) {
  4014. return FAILURE;
  4015. }
  4016. }
  4017. EG(fake_scope) = scope;
  4018. property = zend_std_get_static_property_with_info(scope, name, BP_VAR_W, &prop_info);
  4019. EG(fake_scope) = old_scope;
  4020. if (!property) {
  4021. return FAILURE;
  4022. }
  4023. ZEND_ASSERT(!Z_ISREF_P(value));
  4024. Z_TRY_ADDREF_P(value);
  4025. if (ZEND_TYPE_IS_SET(prop_info->type)) {
  4026. ZVAL_COPY_VALUE(&tmp, value);
  4027. if (!zend_verify_property_type(prop_info, &tmp, /* strict */ 0)) {
  4028. Z_TRY_DELREF_P(value);
  4029. return FAILURE;
  4030. }
  4031. value = &tmp;
  4032. }
  4033. zend_assign_to_variable(property, value, IS_TMP_VAR, /* strict */ 0);
  4034. return SUCCESS;
  4035. }
  4036. /* }}} */
  4037. ZEND_API zend_result zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */
  4038. {
  4039. zend_string *key = zend_string_init(name, name_length, 0);
  4040. bool retval = zend_update_static_property_ex(scope, key, value);
  4041. zend_string_efree(key);
  4042. return retval;
  4043. }
  4044. /* }}} */
  4045. ZEND_API zend_result zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */
  4046. {
  4047. zval tmp;
  4048. ZVAL_NULL(&tmp);
  4049. return zend_update_static_property(scope, name, name_length, &tmp);
  4050. }
  4051. /* }}} */
  4052. ZEND_API zend_result zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
  4053. {
  4054. zval tmp;
  4055. ZVAL_BOOL(&tmp, value);
  4056. return zend_update_static_property(scope, name, name_length, &tmp);
  4057. }
  4058. /* }}} */
  4059. ZEND_API zend_result zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
  4060. {
  4061. zval tmp;
  4062. ZVAL_LONG(&tmp, value);
  4063. return zend_update_static_property(scope, name, name_length, &tmp);
  4064. }
  4065. /* }}} */
  4066. ZEND_API zend_result zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */
  4067. {
  4068. zval tmp;
  4069. ZVAL_DOUBLE(&tmp, value);
  4070. return zend_update_static_property(scope, name, name_length, &tmp);
  4071. }
  4072. /* }}} */
  4073. ZEND_API zend_result zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */
  4074. {
  4075. zval tmp;
  4076. ZVAL_STRING(&tmp, value);
  4077. Z_SET_REFCOUNT(tmp, 0);
  4078. return zend_update_static_property(scope, name, name_length, &tmp);
  4079. }
  4080. /* }}} */
  4081. ZEND_API zend_result zend_update_static_property_stringl(zend_class_entry *scope, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
  4082. {
  4083. zval tmp;
  4084. ZVAL_STRINGL(&tmp, value, value_len);
  4085. Z_SET_REFCOUNT(tmp, 0);
  4086. return zend_update_static_property(scope, name, name_length, &tmp);
  4087. }
  4088. /* }}} */
  4089. ZEND_API zval *zend_read_property_ex(zend_class_entry *scope, zend_object *object, zend_string *name, bool silent, zval *rv) /* {{{ */
  4090. {
  4091. zval *value;
  4092. zend_class_entry *old_scope = EG(fake_scope);
  4093. EG(fake_scope) = scope;
  4094. value = object->handlers->read_property(object, name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
  4095. EG(fake_scope) = old_scope;
  4096. return value;
  4097. }
  4098. /* }}} */
  4099. ZEND_API zval *zend_read_property(zend_class_entry *scope, zend_object *object, const char *name, size_t name_length, bool silent, zval *rv) /* {{{ */
  4100. {
  4101. zval *value;
  4102. zend_string *str;
  4103. str = zend_string_init(name, name_length, 0);
  4104. value = zend_read_property_ex(scope, object, str, silent, rv);
  4105. zend_string_release_ex(str, 0);
  4106. return value;
  4107. }
  4108. /* }}} */
  4109. ZEND_API zval *zend_read_static_property_ex(zend_class_entry *scope, zend_string *name, bool silent) /* {{{ */
  4110. {
  4111. zval *property;
  4112. zend_class_entry *old_scope = EG(fake_scope);
  4113. EG(fake_scope) = scope;
  4114. property = zend_std_get_static_property(scope, name, silent ? BP_VAR_IS : BP_VAR_R);
  4115. EG(fake_scope) = old_scope;
  4116. return property;
  4117. }
  4118. /* }}} */
  4119. ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, bool silent) /* {{{ */
  4120. {
  4121. zend_string *key = zend_string_init(name, name_length, 0);
  4122. zval *property = zend_read_static_property_ex(scope, key, silent);
  4123. zend_string_efree(key);
  4124. return property;
  4125. }
  4126. /* }}} */
  4127. ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */
  4128. {
  4129. current->handling = EG(error_handling);
  4130. current->exception = EG(exception_class);
  4131. }
  4132. /* }}} */
  4133. ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */
  4134. {
  4135. if (current) {
  4136. zend_save_error_handling(current);
  4137. }
  4138. ZEND_ASSERT(error_handling == EH_THROW || exception_class == NULL);
  4139. EG(error_handling) = error_handling;
  4140. EG(exception_class) = exception_class;
  4141. }
  4142. /* }}} */
  4143. ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */
  4144. {
  4145. EG(error_handling) = saved->handling;
  4146. EG(exception_class) = saved->exception;
  4147. }
  4148. /* }}} */
  4149. ZEND_API ZEND_COLD const char *zend_get_object_type(const zend_class_entry *ce) /* {{{ */
  4150. {
  4151. if(ce->ce_flags & ZEND_ACC_TRAIT) {
  4152. return "trait";
  4153. } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  4154. return "interface";
  4155. } else {
  4156. return "class";
  4157. }
  4158. }
  4159. /* }}} */
  4160. ZEND_API bool zend_is_iterable(zval *iterable) /* {{{ */
  4161. {
  4162. switch (Z_TYPE_P(iterable)) {
  4163. case IS_ARRAY:
  4164. return 1;
  4165. case IS_OBJECT:
  4166. return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
  4167. default:
  4168. return 0;
  4169. }
  4170. }
  4171. /* }}} */
  4172. ZEND_API bool zend_is_countable(zval *countable) /* {{{ */
  4173. {
  4174. switch (Z_TYPE_P(countable)) {
  4175. case IS_ARRAY:
  4176. return 1;
  4177. case IS_OBJECT:
  4178. if (Z_OBJ_HT_P(countable)->count_elements) {
  4179. return 1;
  4180. }
  4181. return zend_class_implements_interface(Z_OBJCE_P(countable), zend_ce_countable);
  4182. default:
  4183. return 0;
  4184. }
  4185. }
  4186. /* }}} */
  4187. static zend_result get_default_via_ast(zval *default_value_zval, const char *default_value) {
  4188. zend_ast *ast;
  4189. zend_arena *ast_arena;
  4190. zend_string *code = zend_string_concat3(
  4191. "<?php ", sizeof("<?php ") - 1, default_value, strlen(default_value), ";", 1);
  4192. ast = zend_compile_string_to_ast(code, &ast_arena, ZSTR_EMPTY_ALLOC());
  4193. zend_string_release(code);
  4194. if (!ast) {
  4195. return FAILURE;
  4196. }
  4197. zend_ast_list *statement_list = zend_ast_get_list(ast);
  4198. zend_ast **const_expr_ast_ptr = &statement_list->child[0];
  4199. zend_arena *original_ast_arena = CG(ast_arena);
  4200. uint32_t original_compiler_options = CG(compiler_options);
  4201. zend_file_context original_file_context;
  4202. CG(ast_arena) = ast_arena;
  4203. /* Disable constant substitution, to make getDefaultValueConstant() work. */
  4204. CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
  4205. zend_file_context_begin(&original_file_context);
  4206. zend_const_expr_to_zval(default_value_zval, const_expr_ast_ptr, /* allow_dynamic */ true);
  4207. CG(ast_arena) = original_ast_arena;
  4208. CG(compiler_options) = original_compiler_options;
  4209. zend_file_context_end(&original_file_context);
  4210. zend_ast_destroy(ast);
  4211. zend_arena_destroy(ast_arena);
  4212. return SUCCESS;
  4213. }
  4214. static zend_string *try_parse_string(const char *str, size_t len, char quote) {
  4215. if (len == 0) {
  4216. return ZSTR_EMPTY_ALLOC();
  4217. }
  4218. for (size_t i = 0; i < len; i++) {
  4219. if (str[i] == '\\' || str[i] == quote) {
  4220. return NULL;
  4221. }
  4222. }
  4223. return zend_string_init(str, len, 0);
  4224. }
  4225. ZEND_API zend_result zend_get_default_from_internal_arg_info(
  4226. zval *default_value_zval, zend_internal_arg_info *arg_info)
  4227. {
  4228. const char *default_value = arg_info->default_value;
  4229. if (!default_value) {
  4230. return FAILURE;
  4231. }
  4232. /* Avoid going through the full AST machinery for some simple and common cases. */
  4233. size_t default_value_len = strlen(default_value);
  4234. zend_ulong lval;
  4235. if (default_value_len == sizeof("null")-1
  4236. && !memcmp(default_value, "null", sizeof("null")-1)) {
  4237. ZVAL_NULL(default_value_zval);
  4238. return SUCCESS;
  4239. } else if (default_value_len == sizeof("true")-1
  4240. && !memcmp(default_value, "true", sizeof("true")-1)) {
  4241. ZVAL_TRUE(default_value_zval);
  4242. return SUCCESS;
  4243. } else if (default_value_len == sizeof("false")-1
  4244. && !memcmp(default_value, "false", sizeof("false")-1)) {
  4245. ZVAL_FALSE(default_value_zval);
  4246. return SUCCESS;
  4247. } else if (default_value_len >= 2
  4248. && (default_value[0] == '\'' || default_value[0] == '"')
  4249. && default_value[default_value_len - 1] == default_value[0]) {
  4250. zend_string *str = try_parse_string(
  4251. default_value + 1, default_value_len - 2, default_value[0]);
  4252. if (str) {
  4253. ZVAL_STR(default_value_zval, str);
  4254. return SUCCESS;
  4255. }
  4256. } else if (default_value_len == sizeof("[]")-1
  4257. && !memcmp(default_value, "[]", sizeof("[]")-1)) {
  4258. ZVAL_EMPTY_ARRAY(default_value_zval);
  4259. return SUCCESS;
  4260. } else if (ZEND_HANDLE_NUMERIC_STR(default_value, default_value_len, lval)) {
  4261. ZVAL_LONG(default_value_zval, lval);
  4262. return SUCCESS;
  4263. }
  4264. #if 0
  4265. fprintf(stderr, "Evaluating %s via AST\n", default_value);
  4266. #endif
  4267. return get_default_via_ast(default_value_zval, default_value);
  4268. }