zend_object_handlers.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include "zend.h"
  21. #include "zend_globals.h"
  22. #include "zend_variables.h"
  23. #include "zend_API.h"
  24. #include "zend_objects.h"
  25. #include "zend_objects_API.h"
  26. #include "zend_object_handlers.h"
  27. #include "zend_interfaces.h"
  28. #include "zend_exceptions.h"
  29. #include "zend_closures.h"
  30. #include "zend_compile.h"
  31. #include "zend_hash.h"
  32. #define DEBUG_OBJECT_HANDLERS 0
  33. #define ZEND_WRONG_PROPERTY_OFFSET 0
  34. /* guard flags */
  35. #define IN_GET (1<<0)
  36. #define IN_SET (1<<1)
  37. #define IN_UNSET (1<<2)
  38. #define IN_ISSET (1<<3)
  39. /*
  40. __X accessors explanation:
  41. if we have __get and property that is not part of the properties array is
  42. requested, we call __get handler. If it fails, we return uninitialized.
  43. if we have __set and property that is not part of the properties array is
  44. set, we call __set handler. If it fails, we do not change the array.
  45. for both handlers above, when we are inside __get/__set, no further calls for
  46. __get/__set for this property of this object will be made, to prevent endless
  47. recursion and enable accessors to change properties array.
  48. if we have __call and method which is not part of the class function table is
  49. called, we cal __call handler.
  50. */
  51. ZEND_API void rebuild_object_properties(zend_object *zobj) /* {{{ */
  52. {
  53. if (!zobj->properties) {
  54. zend_property_info *prop_info;
  55. zend_class_entry *ce = zobj->ce;
  56. zobj->properties = zend_new_array(ce->default_properties_count);
  57. if (ce->default_properties_count) {
  58. zend_hash_real_init_mixed(zobj->properties);
  59. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
  60. if (/*prop_info->ce == ce &&*/
  61. (prop_info->flags & ZEND_ACC_STATIC) == 0) {
  62. if (UNEXPECTED(Z_TYPE_P(OBJ_PROP(zobj, prop_info->offset)) == IS_UNDEF)) {
  63. HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
  64. }
  65. _zend_hash_append_ind(zobj->properties, prop_info->name,
  66. OBJ_PROP(zobj, prop_info->offset));
  67. }
  68. } ZEND_HASH_FOREACH_END();
  69. while (ce->parent && ce->parent->default_properties_count) {
  70. ce = ce->parent;
  71. ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
  72. if (prop_info->ce == ce &&
  73. (prop_info->flags & ZEND_ACC_STATIC) == 0 &&
  74. (prop_info->flags & ZEND_ACC_PRIVATE) != 0) {
  75. zval zv;
  76. if (UNEXPECTED(Z_TYPE_P(OBJ_PROP(zobj, prop_info->offset)) == IS_UNDEF)) {
  77. HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
  78. }
  79. ZVAL_INDIRECT(&zv, OBJ_PROP(zobj, prop_info->offset));
  80. zend_hash_add(zobj->properties, prop_info->name, &zv);
  81. }
  82. } ZEND_HASH_FOREACH_END();
  83. }
  84. }
  85. }
  86. }
  87. /* }}} */
  88. ZEND_API HashTable *zend_std_get_properties(zval *object) /* {{{ */
  89. {
  90. zend_object *zobj;
  91. zobj = Z_OBJ_P(object);
  92. if (!zobj->properties) {
  93. rebuild_object_properties(zobj);
  94. }
  95. return zobj->properties;
  96. }
  97. /* }}} */
  98. ZEND_API HashTable *zend_std_get_gc(zval *object, zval **table, int *n) /* {{{ */
  99. {
  100. if (Z_OBJ_HANDLER_P(object, get_properties) != zend_std_get_properties) {
  101. *table = NULL;
  102. *n = 0;
  103. return Z_OBJ_HANDLER_P(object, get_properties)(object);
  104. } else {
  105. zend_object *zobj = Z_OBJ_P(object);
  106. if (zobj->properties) {
  107. *table = NULL;
  108. *n = 0;
  109. if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)
  110. && EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
  111. GC_DELREF(zobj->properties);
  112. zobj->properties = zend_array_dup(zobj->properties);
  113. }
  114. return zobj->properties;
  115. } else {
  116. *table = zobj->properties_table;
  117. *n = zobj->ce->default_properties_count;
  118. return NULL;
  119. }
  120. }
  121. }
  122. /* }}} */
  123. ZEND_API HashTable *zend_std_get_debug_info(zval *object, int *is_temp) /* {{{ */
  124. {
  125. zend_class_entry *ce = Z_OBJCE_P(object);
  126. zval retval;
  127. HashTable *ht;
  128. if (!ce->__debugInfo) {
  129. *is_temp = 0;
  130. return Z_OBJ_HANDLER_P(object, get_properties)
  131. ? Z_OBJ_HANDLER_P(object, get_properties)(object)
  132. : NULL;
  133. }
  134. zend_call_method_with_0_params(object, ce, &ce->__debugInfo, ZEND_DEBUGINFO_FUNC_NAME, &retval);
  135. if (Z_TYPE(retval) == IS_ARRAY) {
  136. if (!Z_REFCOUNTED(retval)) {
  137. *is_temp = 1;
  138. return zend_array_dup(Z_ARRVAL(retval));
  139. } else if (Z_REFCOUNT(retval) <= 1) {
  140. *is_temp = 1;
  141. ht = Z_ARR(retval);
  142. return ht;
  143. } else {
  144. *is_temp = 0;
  145. zval_ptr_dtor(&retval);
  146. return Z_ARRVAL(retval);
  147. }
  148. } else if (Z_TYPE(retval) == IS_NULL) {
  149. *is_temp = 1;
  150. ht = zend_new_array(0);
  151. return ht;
  152. }
  153. zend_error_noreturn(E_ERROR, ZEND_DEBUGINFO_FUNC_NAME "() must return an array");
  154. return NULL; /* Compilers are dumb and don't understand that noreturn means that the function does NOT need a return value... */
  155. }
  156. /* }}} */
  157. static void zend_std_call_getter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */
  158. {
  159. zend_class_entry *ce = zobj->ce;
  160. zend_class_entry *orig_fake_scope = EG(fake_scope);
  161. zend_fcall_info fci;
  162. zend_fcall_info_cache fcic;
  163. zval member;
  164. EG(fake_scope) = NULL;
  165. /* __get handler is called with one argument:
  166. property name
  167. it should return whether the call was successful or not
  168. */
  169. ZVAL_STR(&member, prop_name);
  170. fci.size = sizeof(fci);
  171. fci.object = zobj;
  172. fci.retval = retval;
  173. fci.param_count = 1;
  174. fci.params = &member;
  175. fci.no_separation = 1;
  176. ZVAL_UNDEF(&fci.function_name); /* Unused */
  177. fcic.function_handler = ce->__get;
  178. fcic.called_scope = ce;
  179. fcic.object = zobj;
  180. zend_call_function(&fci, &fcic);
  181. EG(fake_scope) = orig_fake_scope;
  182. }
  183. /* }}} */
  184. static void zend_std_call_setter(zend_object *zobj, zend_string *prop_name, zval *value) /* {{{ */
  185. {
  186. zend_class_entry *ce = zobj->ce;
  187. zend_class_entry *orig_fake_scope = EG(fake_scope);
  188. zend_fcall_info fci;
  189. zend_fcall_info_cache fcic;
  190. zval args[2], ret;
  191. EG(fake_scope) = NULL;
  192. /* __set handler is called with two arguments:
  193. property name
  194. value to be set
  195. */
  196. ZVAL_STR(&args[0], prop_name);
  197. ZVAL_COPY_VALUE(&args[1], value);
  198. ZVAL_UNDEF(&ret);
  199. fci.size = sizeof(fci);
  200. fci.object = zobj;
  201. fci.retval = &ret;
  202. fci.param_count = 2;
  203. fci.params = args;
  204. fci.no_separation = 1;
  205. ZVAL_UNDEF(&fci.function_name); /* Unused */
  206. fcic.function_handler = ce->__set;
  207. fcic.called_scope = ce;
  208. fcic.object = zobj;
  209. zend_call_function(&fci, &fcic);
  210. zval_ptr_dtor(&ret);
  211. EG(fake_scope) = orig_fake_scope;
  212. }
  213. /* }}} */
  214. static void zend_std_call_unsetter(zend_object *zobj, zend_string *prop_name) /* {{{ */
  215. {
  216. zend_class_entry *ce = zobj->ce;
  217. zend_class_entry *orig_fake_scope = EG(fake_scope);
  218. zend_fcall_info fci;
  219. zend_fcall_info_cache fcic;
  220. zval ret, member;
  221. EG(fake_scope) = NULL;
  222. /* __unset handler is called with one argument:
  223. property name
  224. */
  225. ZVAL_STR(&member, prop_name);
  226. ZVAL_UNDEF(&ret);
  227. fci.size = sizeof(fci);
  228. fci.object = zobj;
  229. fci.retval = &ret;
  230. fci.param_count = 1;
  231. fci.params = &member;
  232. fci.no_separation = 1;
  233. ZVAL_UNDEF(&fci.function_name); /* Unused */
  234. fcic.function_handler = ce->__unset;
  235. fcic.called_scope = ce;
  236. fcic.object = zobj;
  237. zend_call_function(&fci, &fcic);
  238. zval_ptr_dtor(&ret);
  239. EG(fake_scope) = orig_fake_scope;
  240. }
  241. /* }}} */
  242. static void zend_std_call_issetter(zend_object *zobj, zend_string *prop_name, zval *retval) /* {{{ */
  243. {
  244. zend_class_entry *ce = zobj->ce;
  245. zend_class_entry *orig_fake_scope = EG(fake_scope);
  246. zend_fcall_info fci;
  247. zend_fcall_info_cache fcic;
  248. zval member;
  249. EG(fake_scope) = NULL;
  250. /* __isset handler is called with one argument:
  251. property name
  252. it should return whether the property is set or not
  253. */
  254. ZVAL_STR(&member, prop_name);
  255. fci.size = sizeof(fci);
  256. fci.object = zobj;
  257. fci.retval = retval;
  258. fci.param_count = 1;
  259. fci.params = &member;
  260. fci.no_separation = 1;
  261. ZVAL_UNDEF(&fci.function_name); /* Unused */
  262. fcic.function_handler = ce->__isset;
  263. fcic.called_scope = ce;
  264. fcic.object = zobj;
  265. zend_call_function(&fci, &fcic);
  266. EG(fake_scope) = orig_fake_scope;
  267. }
  268. /* }}} */
  269. static zend_always_inline int zend_verify_property_access(zend_property_info *property_info, zend_class_entry *ce) /* {{{ */
  270. {
  271. zend_class_entry *scope;
  272. if (property_info->flags & ZEND_ACC_PUBLIC) {
  273. return 1;
  274. } else if (property_info->flags & ZEND_ACC_PRIVATE) {
  275. if (EG(fake_scope)) {
  276. scope = EG(fake_scope);
  277. } else {
  278. scope = zend_get_executed_scope();
  279. }
  280. return (ce == scope || property_info->ce == scope);
  281. } else if (property_info->flags & ZEND_ACC_PROTECTED) {
  282. if (EG(fake_scope)) {
  283. scope = EG(fake_scope);
  284. } else {
  285. scope = zend_get_executed_scope();
  286. }
  287. return zend_check_protected(property_info->ce, scope);
  288. }
  289. return 0;
  290. }
  291. /* }}} */
  292. static zend_always_inline zend_bool is_derived_class(zend_class_entry *child_class, zend_class_entry *parent_class) /* {{{ */
  293. {
  294. child_class = child_class->parent;
  295. while (child_class) {
  296. if (child_class == parent_class) {
  297. return 1;
  298. }
  299. child_class = child_class->parent;
  300. }
  301. return 0;
  302. }
  303. /* }}} */
  304. static zend_always_inline uintptr_t zend_get_property_offset(zend_class_entry *ce, zend_string *member, int silent, void **cache_slot) /* {{{ */
  305. {
  306. zval *zv;
  307. zend_property_info *property_info = NULL;
  308. uint32_t flags;
  309. zend_class_entry *scope;
  310. if (cache_slot && EXPECTED(ce == CACHED_PTR_EX(cache_slot))) {
  311. return (uintptr_t)CACHED_PTR_EX(cache_slot + 1);
  312. }
  313. if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0)) {
  314. goto exit_dynamic;
  315. }
  316. zv = zend_hash_find(&ce->properties_info, member);
  317. if (EXPECTED(zv != NULL)) {
  318. property_info = (zend_property_info*)Z_PTR_P(zv);
  319. flags = property_info->flags;
  320. if (UNEXPECTED((flags & ZEND_ACC_SHADOW) != 0)) {
  321. /* if it's a shadow - go to access it's private */
  322. property_info = NULL;
  323. } else {
  324. if (EXPECTED(zend_verify_property_access(property_info, ce) != 0)) {
  325. if (UNEXPECTED(!(flags & ZEND_ACC_CHANGED))
  326. || UNEXPECTED((flags & ZEND_ACC_PRIVATE))) {
  327. if (UNEXPECTED((flags & ZEND_ACC_STATIC) != 0)) {
  328. if (!silent) {
  329. zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member));
  330. }
  331. return ZEND_DYNAMIC_PROPERTY_OFFSET;
  332. }
  333. goto exit;
  334. }
  335. } else {
  336. /* Try to look in the scope instead */
  337. property_info = ZEND_WRONG_PROPERTY_INFO;
  338. }
  339. }
  340. }
  341. if (EG(fake_scope)) {
  342. scope = EG(fake_scope);
  343. } else {
  344. scope = zend_get_executed_scope();
  345. }
  346. if (scope != ce
  347. && scope
  348. && is_derived_class(ce, scope)
  349. && (zv = zend_hash_find(&scope->properties_info, member)) != NULL
  350. && ((zend_property_info*)Z_PTR_P(zv))->flags & ZEND_ACC_PRIVATE) {
  351. property_info = (zend_property_info*)Z_PTR_P(zv);
  352. if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) != 0)) {
  353. return ZEND_DYNAMIC_PROPERTY_OFFSET;
  354. }
  355. } else if (UNEXPECTED(property_info == NULL)) {
  356. exit_dynamic:
  357. if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0' && ZSTR_LEN(member) != 0)) {
  358. if (!silent) {
  359. zend_throw_error(NULL, "Cannot access property started with '\\0'");
  360. }
  361. return ZEND_WRONG_PROPERTY_OFFSET;
  362. }
  363. if (cache_slot) {
  364. CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
  365. }
  366. return ZEND_DYNAMIC_PROPERTY_OFFSET;
  367. } else if (UNEXPECTED(property_info == ZEND_WRONG_PROPERTY_INFO)) {
  368. /* Information was available, but we were denied access. Error out. */
  369. if (!silent) {
  370. zend_throw_error(NULL, "Cannot access %s property %s::$%s", zend_visibility_string(flags), ZSTR_VAL(ce->name), ZSTR_VAL(member));
  371. }
  372. return ZEND_WRONG_PROPERTY_OFFSET;
  373. }
  374. exit:
  375. if (cache_slot) {
  376. CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)(uintptr_t)property_info->offset);
  377. }
  378. return property_info->offset;
  379. }
  380. /* }}} */
  381. ZEND_API zend_property_info *zend_get_property_info(zend_class_entry *ce, zend_string *member, int silent) /* {{{ */
  382. {
  383. zval *zv;
  384. zend_property_info *property_info = NULL;
  385. uint32_t flags;
  386. zend_class_entry *scope;
  387. if (UNEXPECTED(zend_hash_num_elements(&ce->properties_info) == 0)) {
  388. goto exit_dynamic;
  389. }
  390. zv = zend_hash_find(&ce->properties_info, member);
  391. if (EXPECTED(zv != NULL)) {
  392. property_info = (zend_property_info*)Z_PTR_P(zv);
  393. flags = property_info->flags;
  394. if (UNEXPECTED((flags & ZEND_ACC_SHADOW) != 0)) {
  395. /* if it's a shadow - go to access it's private */
  396. property_info = NULL;
  397. } else {
  398. if (EXPECTED(zend_verify_property_access(property_info, ce) != 0)) {
  399. if (UNEXPECTED(!(flags & ZEND_ACC_CHANGED))
  400. || UNEXPECTED((flags & ZEND_ACC_PRIVATE))) {
  401. if (UNEXPECTED((flags & ZEND_ACC_STATIC) != 0)) {
  402. if (!silent) {
  403. zend_error(E_NOTICE, "Accessing static property %s::$%s as non static", ZSTR_VAL(ce->name), ZSTR_VAL(member));
  404. }
  405. }
  406. goto exit;
  407. }
  408. } else {
  409. /* Try to look in the scope instead */
  410. property_info = ZEND_WRONG_PROPERTY_INFO;
  411. }
  412. }
  413. }
  414. if (EG(fake_scope)) {
  415. scope = EG(fake_scope);
  416. } else {
  417. scope = zend_get_executed_scope();
  418. }
  419. if (scope != ce
  420. && scope
  421. && is_derived_class(ce, scope)
  422. && (zv = zend_hash_find(&scope->properties_info, member)) != NULL
  423. && ((zend_property_info*)Z_PTR_P(zv))->flags & ZEND_ACC_PRIVATE) {
  424. property_info = (zend_property_info*)Z_PTR_P(zv);
  425. } else if (UNEXPECTED(property_info == NULL)) {
  426. exit_dynamic:
  427. if (UNEXPECTED(ZSTR_VAL(member)[0] == '\0' && ZSTR_LEN(member) != 0)) {
  428. if (!silent) {
  429. zend_throw_error(NULL, "Cannot access property started with '\\0'");
  430. }
  431. return ZEND_WRONG_PROPERTY_INFO;
  432. }
  433. return NULL;
  434. } else if (UNEXPECTED(property_info == ZEND_WRONG_PROPERTY_INFO)) {
  435. /* Information was available, but we were denied access. Error out. */
  436. if (!silent) {
  437. zend_throw_error(NULL, "Cannot access %s property %s::$%s", zend_visibility_string(flags), ZSTR_VAL(ce->name), ZSTR_VAL(member));
  438. }
  439. return ZEND_WRONG_PROPERTY_INFO;
  440. }
  441. exit:
  442. return property_info;
  443. }
  444. /* }}} */
  445. ZEND_API int zend_check_property_access(zend_object *zobj, zend_string *prop_info_name) /* {{{ */
  446. {
  447. zend_property_info *property_info;
  448. const char *class_name = NULL;
  449. const char *prop_name;
  450. zend_string *member;
  451. size_t prop_name_len;
  452. if (ZSTR_VAL(prop_info_name)[0] == 0) {
  453. zend_unmangle_property_name_ex(prop_info_name, &class_name, &prop_name, &prop_name_len);
  454. member = zend_string_init(prop_name, prop_name_len, 0);
  455. } else {
  456. member = zend_string_copy(prop_info_name);
  457. }
  458. property_info = zend_get_property_info(zobj->ce, member, 1);
  459. zend_string_release_ex(member, 0);
  460. if (property_info == NULL) {
  461. /* undefined public property */
  462. if (class_name && class_name[0] != '*') {
  463. /* we we're looking for a private prop */
  464. return FAILURE;
  465. }
  466. return SUCCESS;
  467. } else if (property_info == ZEND_WRONG_PROPERTY_INFO) {
  468. return FAILURE;
  469. }
  470. if (class_name && class_name[0] != '*') {
  471. if (!(property_info->flags & ZEND_ACC_PRIVATE)) {
  472. /* we we're looking for a private prop but found a non private one of the same name */
  473. return FAILURE;
  474. } else if (strcmp(ZSTR_VAL(prop_info_name)+1, ZSTR_VAL(property_info->name)+1)) {
  475. /* we we're looking for a private prop but found a private one of the same name but another class */
  476. return FAILURE;
  477. }
  478. }
  479. return zend_verify_property_access(property_info, zobj->ce) ? SUCCESS : FAILURE;
  480. }
  481. /* }}} */
  482. static void zend_property_guard_dtor(zval *el) /* {{{ */ {
  483. uint32_t *ptr = (uint32_t*)Z_PTR_P(el);
  484. if (EXPECTED(!(((zend_uintptr_t)ptr) & 1))) {
  485. efree_size(ptr, sizeof(uint32_t));
  486. }
  487. }
  488. /* }}} */
  489. ZEND_API uint32_t *zend_get_property_guard(zend_object *zobj, zend_string *member) /* {{{ */
  490. {
  491. HashTable *guards;
  492. zval *zv;
  493. uint32_t *ptr;
  494. ZEND_ASSERT(zobj->ce->ce_flags & ZEND_ACC_USE_GUARDS);
  495. zv = zobj->properties_table + zobj->ce->default_properties_count;
  496. if (EXPECTED(Z_TYPE_P(zv) == IS_STRING)) {
  497. zend_string *str = Z_STR_P(zv);
  498. if (EXPECTED(str == member) ||
  499. /* hash values are always pred-calculated here */
  500. (EXPECTED(ZSTR_H(str) == ZSTR_H(member)) &&
  501. EXPECTED(zend_string_equal_content(str, member)))) {
  502. return &Z_PROPERTY_GUARD_P(zv);
  503. } else if (EXPECTED(Z_PROPERTY_GUARD_P(zv) == 0)) {
  504. zval_ptr_dtor_str(zv);
  505. ZVAL_STR_COPY(zv, member);
  506. return &Z_PROPERTY_GUARD_P(zv);
  507. } else {
  508. ALLOC_HASHTABLE(guards);
  509. zend_hash_init(guards, 8, NULL, zend_property_guard_dtor, 0);
  510. /* mark pointer as "special" using low bit */
  511. zend_hash_add_new_ptr(guards, str,
  512. (void*)(((zend_uintptr_t)&Z_PROPERTY_GUARD_P(zv)) | 1));
  513. zval_ptr_dtor_str(zv);
  514. ZVAL_ARR(zv, guards);
  515. }
  516. } else if (EXPECTED(Z_TYPE_P(zv) == IS_ARRAY)) {
  517. guards = Z_ARRVAL_P(zv);
  518. ZEND_ASSERT(guards != NULL);
  519. zv = zend_hash_find(guards, member);
  520. if (zv != NULL) {
  521. return (uint32_t*)(((zend_uintptr_t)Z_PTR_P(zv)) & ~1);
  522. }
  523. } else {
  524. ZEND_ASSERT(Z_TYPE_P(zv) == IS_UNDEF);
  525. ZVAL_STR_COPY(zv, member);
  526. Z_PROPERTY_GUARD_P(zv) = 0;
  527. return &Z_PROPERTY_GUARD_P(zv);
  528. }
  529. /* we have to allocate uint32_t separately because ht->arData may be reallocated */
  530. ptr = (uint32_t*)emalloc(sizeof(uint32_t));
  531. *ptr = 0;
  532. return (uint32_t*)zend_hash_add_new_ptr(guards, member, ptr);
  533. }
  534. /* }}} */
  535. ZEND_API zval *zend_std_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) /* {{{ */
  536. {
  537. zend_object *zobj;
  538. zend_string *name, *tmp_name;
  539. zval *retval;
  540. uintptr_t property_offset;
  541. uint32_t *guard = NULL;
  542. zobj = Z_OBJ_P(object);
  543. name = zval_get_tmp_string(member, &tmp_name);
  544. #if DEBUG_OBJECT_HANDLERS
  545. fprintf(stderr, "Read object #%d property: %s\n", Z_OBJ_HANDLE_P(object), ZSTR_VAL(name));
  546. #endif
  547. /* make zend_get_property_info silent if we have getter - we may want to use it */
  548. property_offset = zend_get_property_offset(zobj->ce, name, (type == BP_VAR_IS) || (zobj->ce->__get != NULL), cache_slot);
  549. if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
  550. retval = OBJ_PROP(zobj, property_offset);
  551. if (EXPECTED(Z_TYPE_P(retval) != IS_UNDEF)) {
  552. goto exit;
  553. }
  554. } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
  555. if (EXPECTED(zobj->properties != NULL)) {
  556. if (!IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(property_offset)) {
  557. uintptr_t idx = ZEND_DECODE_DYN_PROP_OFFSET(property_offset);
  558. if (EXPECTED(idx < zobj->properties->nNumUsed * sizeof(Bucket))) {
  559. Bucket *p = (Bucket*)((char*)zobj->properties->arData + idx);
  560. if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF) &&
  561. (EXPECTED(p->key == name) ||
  562. (EXPECTED(p->h == ZSTR_H(name)) &&
  563. EXPECTED(p->key != NULL) &&
  564. EXPECTED(zend_string_equal_content(p->key, name))))) {
  565. retval = &p->val;
  566. goto exit;
  567. }
  568. }
  569. CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
  570. }
  571. retval = zend_hash_find(zobj->properties, name);
  572. if (EXPECTED(retval)) {
  573. if (cache_slot) {
  574. uintptr_t idx = (char*)retval - (char*)zobj->properties->arData;
  575. CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx));
  576. }
  577. goto exit;
  578. }
  579. }
  580. } else if (UNEXPECTED(EG(exception))) {
  581. retval = &EG(uninitialized_zval);
  582. goto exit;
  583. }
  584. /* magic isset */
  585. if ((type == BP_VAR_IS) && zobj->ce->__isset) {
  586. zval tmp_result;
  587. guard = zend_get_property_guard(zobj, name);
  588. if (!((*guard) & IN_ISSET)) {
  589. if (!tmp_name && !ZSTR_IS_INTERNED(name)) {
  590. tmp_name = zend_string_copy(name);
  591. }
  592. GC_ADDREF(zobj);
  593. ZVAL_UNDEF(&tmp_result);
  594. *guard |= IN_ISSET;
  595. zend_std_call_issetter(zobj, name, &tmp_result);
  596. *guard &= ~IN_ISSET;
  597. if (!zend_is_true(&tmp_result)) {
  598. retval = &EG(uninitialized_zval);
  599. OBJ_RELEASE(zobj);
  600. zval_ptr_dtor(&tmp_result);
  601. goto exit;
  602. }
  603. zval_ptr_dtor(&tmp_result);
  604. if (zobj->ce->__get && !((*guard) & IN_GET)) {
  605. goto call_getter;
  606. }
  607. OBJ_RELEASE(zobj);
  608. } else if (zobj->ce->__get && !((*guard) & IN_GET)) {
  609. goto call_getter_addref;
  610. }
  611. } else if (zobj->ce->__get) {
  612. /* magic get */
  613. guard = zend_get_property_guard(zobj, name);
  614. if (!((*guard) & IN_GET)) {
  615. /* have getter - try with it! */
  616. call_getter_addref:
  617. GC_ADDREF(zobj);
  618. call_getter:
  619. *guard |= IN_GET; /* prevent circular getting */
  620. zend_std_call_getter(zobj, name, rv);
  621. *guard &= ~IN_GET;
  622. if (Z_TYPE_P(rv) != IS_UNDEF) {
  623. retval = rv;
  624. if (!Z_ISREF_P(rv) &&
  625. (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)) {
  626. if (UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
  627. zend_error(E_NOTICE, "Indirect modification of overloaded property %s::$%s has no effect", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
  628. }
  629. }
  630. } else {
  631. retval = &EG(uninitialized_zval);
  632. }
  633. OBJ_RELEASE(zobj);
  634. goto exit;
  635. } else if (ZSTR_VAL(name)[0] == '\0' && ZSTR_LEN(name) != 0) {
  636. zend_throw_error(NULL, "Cannot access property started with '\\0'");
  637. retval = &EG(uninitialized_zval);
  638. goto exit;
  639. }
  640. }
  641. if ((type != BP_VAR_IS)) {
  642. zend_error(E_NOTICE,"Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
  643. }
  644. retval = &EG(uninitialized_zval);
  645. exit:
  646. zend_tmp_string_release(tmp_name);
  647. return retval;
  648. }
  649. /* }}} */
  650. ZEND_API void zend_std_write_property(zval *object, zval *member, zval *value, void **cache_slot) /* {{{ */
  651. {
  652. zend_object *zobj;
  653. zend_string *name, *tmp_name;
  654. zval *variable_ptr;
  655. uintptr_t property_offset;
  656. zobj = Z_OBJ_P(object);
  657. name = zval_get_tmp_string(member, &tmp_name);
  658. property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__set != NULL), cache_slot);
  659. if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
  660. variable_ptr = OBJ_PROP(zobj, property_offset);
  661. if (Z_TYPE_P(variable_ptr) != IS_UNDEF) {
  662. goto found;
  663. }
  664. } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
  665. if (EXPECTED(zobj->properties != NULL)) {
  666. if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
  667. if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
  668. GC_DELREF(zobj->properties);
  669. }
  670. zobj->properties = zend_array_dup(zobj->properties);
  671. }
  672. if ((variable_ptr = zend_hash_find(zobj->properties, name)) != NULL) {
  673. found:
  674. zend_assign_to_variable(variable_ptr, value, IS_CV);
  675. goto exit;
  676. }
  677. }
  678. } else if (UNEXPECTED(EG(exception))) {
  679. goto exit;
  680. }
  681. /* magic set */
  682. if (zobj->ce->__set) {
  683. uint32_t *guard = zend_get_property_guard(zobj, name);
  684. if (!((*guard) & IN_SET)) {
  685. GC_ADDREF(zobj);
  686. (*guard) |= IN_SET; /* prevent circular setting */
  687. zend_std_call_setter(zobj, name, value);
  688. (*guard) &= ~IN_SET;
  689. OBJ_RELEASE(zobj);
  690. } else if (EXPECTED(!IS_WRONG_PROPERTY_OFFSET(property_offset))) {
  691. goto write_std_property;
  692. } else {
  693. if (ZSTR_VAL(name)[0] == '\0' && ZSTR_LEN(name) != 0) {
  694. zend_throw_error(NULL, "Cannot access property started with '\\0'");
  695. goto exit;
  696. }
  697. }
  698. } else if (EXPECTED(!IS_WRONG_PROPERTY_OFFSET(property_offset))) {
  699. write_std_property:
  700. if (Z_REFCOUNTED_P(value)) {
  701. if (Z_ISREF_P(value)) {
  702. /* if we assign referenced variable, we should separate it */
  703. value = Z_REFVAL_P(value);
  704. if (Z_REFCOUNTED_P(value)) {
  705. Z_ADDREF_P(value);
  706. }
  707. } else {
  708. Z_ADDREF_P(value);
  709. }
  710. }
  711. if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
  712. ZVAL_COPY_VALUE(OBJ_PROP(zobj, property_offset), value);
  713. } else {
  714. if (!zobj->properties) {
  715. rebuild_object_properties(zobj);
  716. }
  717. zend_hash_add_new(zobj->properties, name, value);
  718. }
  719. }
  720. exit:
  721. zend_tmp_string_release(tmp_name);
  722. }
  723. /* }}} */
  724. ZEND_API zval *zend_std_read_dimension(zval *object, zval *offset, int type, zval *rv) /* {{{ */
  725. {
  726. zend_class_entry *ce = Z_OBJCE_P(object);
  727. zval tmp_offset, tmp_object;
  728. if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1) != 0)) {
  729. if (offset == NULL) {
  730. /* [] construct */
  731. ZVAL_NULL(&tmp_offset);
  732. } else {
  733. ZVAL_COPY_DEREF(&tmp_offset, offset);
  734. }
  735. ZVAL_COPY(&tmp_object, object);
  736. if (type == BP_VAR_IS) {
  737. zend_call_method_with_1_params(&tmp_object, ce, NULL, "offsetexists", rv, &tmp_offset);
  738. if (UNEXPECTED(Z_ISUNDEF_P(rv))) {
  739. zval_ptr_dtor(&tmp_object);
  740. zval_ptr_dtor(&tmp_offset);
  741. return NULL;
  742. }
  743. if (!i_zend_is_true(rv)) {
  744. zval_ptr_dtor(&tmp_object);
  745. zval_ptr_dtor(&tmp_offset);
  746. zval_ptr_dtor(rv);
  747. return &EG(uninitialized_zval);
  748. }
  749. zval_ptr_dtor(rv);
  750. }
  751. zend_call_method_with_1_params(&tmp_object, ce, NULL, "offsetget", rv, &tmp_offset);
  752. zval_ptr_dtor(&tmp_object);
  753. zval_ptr_dtor(&tmp_offset);
  754. if (UNEXPECTED(Z_TYPE_P(rv) == IS_UNDEF)) {
  755. if (UNEXPECTED(!EG(exception))) {
  756. zend_throw_error(NULL, "Undefined offset for object of type %s used as array", ZSTR_VAL(ce->name));
  757. }
  758. return NULL;
  759. }
  760. return rv;
  761. } else {
  762. zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(ce->name));
  763. return NULL;
  764. }
  765. }
  766. /* }}} */
  767. ZEND_API void zend_std_write_dimension(zval *object, zval *offset, zval *value) /* {{{ */
  768. {
  769. zend_class_entry *ce = Z_OBJCE_P(object);
  770. zval tmp_offset, tmp_object;
  771. if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1) != 0)) {
  772. if (!offset) {
  773. ZVAL_NULL(&tmp_offset);
  774. } else {
  775. ZVAL_COPY_DEREF(&tmp_offset, offset);
  776. }
  777. ZVAL_COPY(&tmp_object, object);
  778. zend_call_method_with_2_params(&tmp_object, ce, NULL, "offsetset", NULL, &tmp_offset, value);
  779. zval_ptr_dtor(&tmp_object);
  780. zval_ptr_dtor(&tmp_offset);
  781. } else {
  782. zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(ce->name));
  783. }
  784. }
  785. /* }}} */
  786. ZEND_API int zend_std_has_dimension(zval *object, zval *offset, int check_empty) /* {{{ */
  787. {
  788. zend_class_entry *ce = Z_OBJCE_P(object);
  789. zval retval, tmp_offset, tmp_object;
  790. int result;
  791. if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1) != 0)) {
  792. ZVAL_COPY_DEREF(&tmp_offset, offset);
  793. ZVAL_COPY(&tmp_object, object);
  794. zend_call_method_with_1_params(&tmp_object, ce, NULL, "offsetexists", &retval, &tmp_offset);
  795. result = i_zend_is_true(&retval);
  796. zval_ptr_dtor(&retval);
  797. if (check_empty && result && EXPECTED(!EG(exception))) {
  798. zend_call_method_with_1_params(&tmp_object, ce, NULL, "offsetget", &retval, &tmp_offset);
  799. result = i_zend_is_true(&retval);
  800. zval_ptr_dtor(&retval);
  801. }
  802. zval_ptr_dtor(&tmp_object);
  803. zval_ptr_dtor(&tmp_offset);
  804. } else {
  805. zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(ce->name));
  806. return 0;
  807. }
  808. return result;
  809. }
  810. /* }}} */
  811. ZEND_API zval *zend_std_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) /* {{{ */
  812. {
  813. zend_object *zobj;
  814. zend_string *name, *tmp_name;
  815. zval *retval = NULL;
  816. uintptr_t property_offset;
  817. zobj = Z_OBJ_P(object);
  818. name = zval_get_tmp_string(member, &tmp_name);
  819. #if DEBUG_OBJECT_HANDLERS
  820. fprintf(stderr, "Ptr object #%d property: %s\n", Z_OBJ_HANDLE_P(object), ZSTR_VAL(name));
  821. #endif
  822. property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__get != NULL), cache_slot);
  823. if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
  824. retval = OBJ_PROP(zobj, property_offset);
  825. if (UNEXPECTED(Z_TYPE_P(retval) == IS_UNDEF)) {
  826. if (EXPECTED(!zobj->ce->__get) ||
  827. UNEXPECTED((*zend_get_property_guard(zobj, name)) & IN_GET)) {
  828. ZVAL_NULL(retval);
  829. /* Notice is thrown after creation of the property, to avoid EG(std_property_info)
  830. * being overwritten in an error handler. */
  831. if (UNEXPECTED(type == BP_VAR_RW || type == BP_VAR_R)) {
  832. zend_error(E_NOTICE, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
  833. }
  834. } else {
  835. /* we do have getter - fail and let it try again with usual get/set */
  836. retval = NULL;
  837. }
  838. }
  839. } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
  840. if (EXPECTED(zobj->properties)) {
  841. if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
  842. if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
  843. GC_DELREF(zobj->properties);
  844. }
  845. zobj->properties = zend_array_dup(zobj->properties);
  846. }
  847. if (EXPECTED((retval = zend_hash_find(zobj->properties, name)) != NULL)) {
  848. zend_tmp_string_release(tmp_name);
  849. return retval;
  850. }
  851. }
  852. if (EXPECTED(!zobj->ce->__get) ||
  853. UNEXPECTED((*zend_get_property_guard(zobj, name)) & IN_GET)) {
  854. if (UNEXPECTED(!zobj->properties)) {
  855. rebuild_object_properties(zobj);
  856. }
  857. retval = zend_hash_update(zobj->properties, name, &EG(uninitialized_zval));
  858. /* Notice is thrown after creation of the property, to avoid EG(std_property_info)
  859. * being overwritten in an error handler. */
  860. if (UNEXPECTED(type == BP_VAR_RW || type == BP_VAR_R)) {
  861. zend_error(E_NOTICE, "Undefined property: %s::$%s", ZSTR_VAL(zobj->ce->name), ZSTR_VAL(name));
  862. }
  863. }
  864. }
  865. zend_tmp_string_release(tmp_name);
  866. return retval;
  867. }
  868. /* }}} */
  869. ZEND_API void zend_std_unset_property(zval *object, zval *member, void **cache_slot) /* {{{ */
  870. {
  871. zend_object *zobj;
  872. zend_string *name, *tmp_name;
  873. uintptr_t property_offset;
  874. zobj = Z_OBJ_P(object);
  875. name = zval_get_tmp_string(member, &tmp_name);
  876. property_offset = zend_get_property_offset(zobj->ce, name, (zobj->ce->__unset != NULL), cache_slot);
  877. if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
  878. zval *slot = OBJ_PROP(zobj, property_offset);
  879. if (Z_TYPE_P(slot) != IS_UNDEF) {
  880. zval_ptr_dtor(slot);
  881. ZVAL_UNDEF(slot);
  882. if (zobj->properties) {
  883. HT_FLAGS(zobj->properties) |= HASH_FLAG_HAS_EMPTY_IND;
  884. }
  885. goto exit;
  886. }
  887. } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))
  888. && EXPECTED(zobj->properties != NULL)) {
  889. if (UNEXPECTED(GC_REFCOUNT(zobj->properties) > 1)) {
  890. if (EXPECTED(!(GC_FLAGS(zobj->properties) & IS_ARRAY_IMMUTABLE))) {
  891. GC_DELREF(zobj->properties);
  892. }
  893. zobj->properties = zend_array_dup(zobj->properties);
  894. }
  895. if (EXPECTED(zend_hash_del(zobj->properties, name) != FAILURE)) {
  896. goto exit;
  897. }
  898. } else if (UNEXPECTED(EG(exception))) {
  899. goto exit;
  900. }
  901. /* magic unset */
  902. if (zobj->ce->__unset) {
  903. uint32_t *guard = zend_get_property_guard(zobj, name);
  904. if (!((*guard) & IN_UNSET)) {
  905. /* have unseter - try with it! */
  906. (*guard) |= IN_UNSET; /* prevent circular unsetting */
  907. zend_std_call_unsetter(zobj, name);
  908. (*guard) &= ~IN_UNSET;
  909. } else {
  910. if (ZSTR_VAL(name)[0] == '\0' && ZSTR_LEN(name) != 0) {
  911. zend_throw_error(NULL, "Cannot access property started with '\\0'");
  912. goto exit;
  913. }
  914. }
  915. }
  916. exit:
  917. zend_tmp_string_release(tmp_name);
  918. }
  919. /* }}} */
  920. ZEND_API void zend_std_unset_dimension(zval *object, zval *offset) /* {{{ */
  921. {
  922. zend_class_entry *ce = Z_OBJCE_P(object);
  923. zval tmp_offset, tmp_object;
  924. if (instanceof_function_ex(ce, zend_ce_arrayaccess, 1)) {
  925. ZVAL_COPY_DEREF(&tmp_offset, offset);
  926. ZVAL_COPY(&tmp_object, object);
  927. zend_call_method_with_1_params(&tmp_object, ce, NULL, "offsetunset", NULL, &tmp_offset);
  928. zval_ptr_dtor(&tmp_object);
  929. zval_ptr_dtor(&tmp_offset);
  930. } else {
  931. zend_throw_error(NULL, "Cannot use object of type %s as array", ZSTR_VAL(ce->name));
  932. }
  933. }
  934. /* }}} */
  935. static zend_always_inline zend_function *zend_get_parent_private(zend_class_entry *scope, zend_class_entry *ce, zend_string *function_name) /* {{{ */
  936. {
  937. zval *func;
  938. zend_function *fbc;
  939. ce = ce->parent;
  940. while (ce) {
  941. if (ce == scope) {
  942. if ((func = zend_hash_find(&ce->function_table, function_name))) {
  943. fbc = Z_FUNC_P(func);
  944. if (fbc->common.fn_flags & ZEND_ACC_PRIVATE
  945. && fbc->common.scope == scope) {
  946. return fbc;
  947. }
  948. }
  949. break;
  950. }
  951. ce = ce->parent;
  952. }
  953. return NULL;
  954. }
  955. /* }}} */
  956. /* Ensures that we're allowed to call a private method.
  957. * Returns the function address that should be called, or NULL
  958. * if no such function exists.
  959. */
  960. ZEND_API int zend_check_private(zend_function *fbc, zend_class_entry *ce, zend_string *function_name) /* {{{ */
  961. {
  962. zend_class_entry *scope;
  963. if (!ce) {
  964. return 0;
  965. }
  966. /* We may call a private function if:
  967. * 1. The class of our object is the same as the scope, and the private
  968. * function (EX(fbc)) has the same scope.
  969. * 2. One of our parent classes are the same as the scope, and it contains
  970. * a private function with the same name that has the same scope.
  971. */
  972. scope = zend_get_executed_scope();
  973. if (fbc->common.scope == ce && scope == ce) {
  974. /* rule #1 checks out ok, allow the function call */
  975. return 1;
  976. }
  977. /* Check rule #2 */
  978. return zend_get_parent_private(scope, ce, function_name) != NULL;
  979. }
  980. /* }}} */
  981. /* Ensures that we're allowed to call a protected method.
  982. */
  983. ZEND_API int zend_check_protected(zend_class_entry *ce, zend_class_entry *scope) /* {{{ */
  984. {
  985. zend_class_entry *fbc_scope = ce;
  986. /* Is the context that's calling the function, the same as one of
  987. * the function's parents?
  988. */
  989. while (fbc_scope) {
  990. if (fbc_scope==scope) {
  991. return 1;
  992. }
  993. fbc_scope = fbc_scope->parent;
  994. }
  995. /* Is the function's scope the same as our current object context,
  996. * or any of the parents of our context?
  997. */
  998. while (scope) {
  999. if (scope==ce) {
  1000. return 1;
  1001. }
  1002. scope = scope->parent;
  1003. }
  1004. return 0;
  1005. }
  1006. /* }}} */
  1007. ZEND_API zend_function *zend_get_call_trampoline_func(zend_class_entry *ce, zend_string *method_name, int is_static) /* {{{ */
  1008. {
  1009. size_t mname_len;
  1010. zend_op_array *func;
  1011. zend_function *fbc = is_static ? ce->__callstatic : ce->__call;
  1012. ZEND_ASSERT(fbc);
  1013. if (EXPECTED(EG(trampoline).common.function_name == NULL)) {
  1014. func = &EG(trampoline).op_array;
  1015. } else {
  1016. func = ecalloc(1, sizeof(zend_op_array));
  1017. }
  1018. func->type = ZEND_USER_FUNCTION;
  1019. func->arg_flags[0] = 0;
  1020. func->arg_flags[1] = 0;
  1021. func->arg_flags[2] = 0;
  1022. func->fn_flags = ZEND_ACC_CALL_VIA_TRAMPOLINE | ZEND_ACC_PUBLIC;
  1023. if (is_static) {
  1024. func->fn_flags |= ZEND_ACC_STATIC;
  1025. }
  1026. func->opcodes = &EG(call_trampoline_op);
  1027. func->run_time_cache = (void*)(intptr_t)-1;
  1028. func->scope = fbc->common.scope;
  1029. /* reserve space for arguments, local and temorary variables */
  1030. func->T = (fbc->type == ZEND_USER_FUNCTION)? MAX(fbc->op_array.last_var + fbc->op_array.T, 2) : 2;
  1031. func->filename = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.filename : ZSTR_EMPTY_ALLOC();
  1032. func->line_start = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_start : 0;
  1033. func->line_end = (fbc->type == ZEND_USER_FUNCTION)? fbc->op_array.line_end : 0;
  1034. //??? keep compatibility for "\0" characters
  1035. //??? see: Zend/tests/bug46238.phpt
  1036. if (UNEXPECTED((mname_len = strlen(ZSTR_VAL(method_name))) != ZSTR_LEN(method_name))) {
  1037. func->function_name = zend_string_init(ZSTR_VAL(method_name), mname_len, 0);
  1038. } else {
  1039. func->function_name = zend_string_copy(method_name);
  1040. }
  1041. return (zend_function*)func;
  1042. }
  1043. /* }}} */
  1044. static zend_always_inline zend_function *zend_get_user_call_function(zend_class_entry *ce, zend_string *method_name) /* {{{ */
  1045. {
  1046. return zend_get_call_trampoline_func(ce, method_name, 0);
  1047. }
  1048. /* }}} */
  1049. ZEND_API zend_function *zend_std_get_method(zend_object **obj_ptr, zend_string *method_name, const zval *key) /* {{{ */
  1050. {
  1051. zend_object *zobj = *obj_ptr;
  1052. zval *func;
  1053. zend_function *fbc;
  1054. zend_string *lc_method_name;
  1055. zend_class_entry *scope = NULL;
  1056. ALLOCA_FLAG(use_heap);
  1057. if (EXPECTED(key != NULL)) {
  1058. lc_method_name = Z_STR_P(key);
  1059. #ifdef ZEND_ALLOCA_MAX_SIZE
  1060. use_heap = 0;
  1061. #endif
  1062. } else {
  1063. ZSTR_ALLOCA_ALLOC(lc_method_name, ZSTR_LEN(method_name), use_heap);
  1064. zend_str_tolower_copy(ZSTR_VAL(lc_method_name), ZSTR_VAL(method_name), ZSTR_LEN(method_name));
  1065. }
  1066. if (UNEXPECTED((func = zend_hash_find(&zobj->ce->function_table, lc_method_name)) == NULL)) {
  1067. if (UNEXPECTED(!key)) {
  1068. ZSTR_ALLOCA_FREE(lc_method_name, use_heap);
  1069. }
  1070. if (zobj->ce->__call) {
  1071. return zend_get_user_call_function(zobj->ce, method_name);
  1072. } else {
  1073. return NULL;
  1074. }
  1075. }
  1076. fbc = Z_FUNC_P(func);
  1077. /* Check access level */
  1078. if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
  1079. /* Ensure that if we're calling a private function, we're allowed to do so.
  1080. * If we're not and __call() handler exists, invoke it, otherwise error out.
  1081. */
  1082. scope = zend_get_executed_scope();
  1083. if (fbc->common.scope != scope || zobj->ce != scope) {
  1084. zend_function *updated_fbc = zend_get_parent_private(scope, zobj->ce, lc_method_name);
  1085. if (EXPECTED(updated_fbc != NULL)) {
  1086. fbc = updated_fbc;
  1087. } else {
  1088. if (zobj->ce->__call) {
  1089. fbc = zend_get_user_call_function(zobj->ce, method_name);
  1090. } else {
  1091. scope = zend_get_executed_scope();
  1092. zend_throw_error(NULL, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), ZSTR_VAL(method_name), scope ? ZSTR_VAL(scope->name) : "");
  1093. fbc = NULL;
  1094. }
  1095. }
  1096. }
  1097. } else if (fbc->op_array.fn_flags & (ZEND_ACC_CHANGED|ZEND_ACC_PROTECTED)) {
  1098. /* Ensure that we haven't overridden a private function and end up calling
  1099. * the overriding public function...
  1100. */
  1101. scope = zend_get_executed_scope();
  1102. do {
  1103. if (fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
  1104. zend_function *priv_fbc = zend_get_parent_private(scope, fbc->common.scope, lc_method_name);
  1105. if (priv_fbc) {
  1106. fbc = priv_fbc;
  1107. break;
  1108. } else if (!(fbc->op_array.fn_flags & ZEND_ACC_PROTECTED)) {
  1109. break;
  1110. }
  1111. }
  1112. /* Ensure that if we're calling a protected function, we're allowed to do so.
  1113. * If we're not and __call() handler exists, invoke it, otherwise error out.
  1114. */
  1115. if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), scope))) {
  1116. if (zobj->ce->__call) {
  1117. fbc = zend_get_user_call_function(zobj->ce, method_name);
  1118. } else {
  1119. zend_throw_error(NULL, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), ZSTR_VAL(method_name), scope ? ZSTR_VAL(scope->name) : "");
  1120. fbc = NULL;
  1121. }
  1122. }
  1123. } while (0);
  1124. }
  1125. if (UNEXPECTED(!key)) {
  1126. ZSTR_ALLOCA_FREE(lc_method_name, use_heap);
  1127. }
  1128. return fbc;
  1129. }
  1130. /* }}} */
  1131. static zend_always_inline zend_function *zend_get_user_callstatic_function(zend_class_entry *ce, zend_string *method_name) /* {{{ */
  1132. {
  1133. return zend_get_call_trampoline_func(ce, method_name, 1);
  1134. }
  1135. /* }}} */
  1136. ZEND_API zend_function *zend_std_get_static_method(zend_class_entry *ce, zend_string *function_name, const zval *key) /* {{{ */
  1137. {
  1138. zend_function *fbc = NULL;
  1139. zend_string *lc_function_name;
  1140. zend_object *object;
  1141. zend_class_entry *scope;
  1142. if (EXPECTED(key != NULL)) {
  1143. lc_function_name = Z_STR_P(key);
  1144. } else {
  1145. lc_function_name = zend_string_tolower(function_name);
  1146. }
  1147. do {
  1148. zval *func = zend_hash_find(&ce->function_table, lc_function_name);
  1149. if (EXPECTED(func != NULL)) {
  1150. fbc = Z_FUNC_P(func);
  1151. } else if (ce->constructor
  1152. && ZSTR_LEN(lc_function_name) == ZSTR_LEN(ce->name)
  1153. && zend_binary_strncasecmp(ZSTR_VAL(lc_function_name), ZSTR_LEN(lc_function_name), ZSTR_VAL(ce->name), ZSTR_LEN(lc_function_name), ZSTR_LEN(lc_function_name)) == 0
  1154. /* Only change the method to the constructor if the constructor isn't called __construct
  1155. * we check for __ so we can be binary safe for lowering, we should use ZEND_CONSTRUCTOR_FUNC_NAME
  1156. */
  1157. && (ZSTR_VAL(ce->constructor->common.function_name)[0] != '_'
  1158. || ZSTR_VAL(ce->constructor->common.function_name)[1] != '_')) {
  1159. fbc = ce->constructor;
  1160. } else {
  1161. if (UNEXPECTED(!key)) {
  1162. zend_string_release_ex(lc_function_name, 0);
  1163. }
  1164. if (ce->__call &&
  1165. (object = zend_get_this_object(EG(current_execute_data))) != NULL &&
  1166. instanceof_function(object->ce, ce)) {
  1167. /* Call the top-level defined __call().
  1168. * see: tests/classes/__call_004.phpt */
  1169. zend_class_entry *call_ce = object->ce;
  1170. while (!call_ce->__call) {
  1171. call_ce = call_ce->parent;
  1172. }
  1173. return zend_get_user_call_function(call_ce, function_name);
  1174. } else if (ce->__callstatic) {
  1175. return zend_get_user_callstatic_function(ce, function_name);
  1176. } else {
  1177. return NULL;
  1178. }
  1179. }
  1180. } while (0);
  1181. #if MBO_0
  1182. /* right now this function is used for non static method lookup too */
  1183. /* Is the function static */
  1184. if (UNEXPECTED(!(fbc->common.fn_flags & ZEND_ACC_STATIC))) {
  1185. zend_error_noreturn(E_ERROR, "Cannot call non static method %s::%s() without object", ZEND_FN_SCOPE_NAME(fbc), ZSTR_VAL(fbc->common.function_name));
  1186. }
  1187. #endif
  1188. if (fbc->op_array.fn_flags & ZEND_ACC_PUBLIC) {
  1189. /* No further checks necessary, most common case */
  1190. } else if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
  1191. /* Ensure that if we're calling a private function, we're allowed to do so.
  1192. */
  1193. scope = zend_get_executed_scope();
  1194. if (UNEXPECTED(fbc->common.scope != scope)) {
  1195. if (ce->__callstatic) {
  1196. fbc = zend_get_user_callstatic_function(ce, function_name);
  1197. } else {
  1198. zend_throw_error(NULL, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), ZSTR_VAL(function_name), scope ? ZSTR_VAL(scope->name) : "");
  1199. fbc = NULL;
  1200. }
  1201. }
  1202. } else if ((fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
  1203. /* Ensure that if we're calling a protected function, we're allowed to do so.
  1204. */
  1205. scope = zend_get_executed_scope();
  1206. if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), scope))) {
  1207. if (ce->__callstatic) {
  1208. fbc = zend_get_user_callstatic_function(ce, function_name);
  1209. } else {
  1210. zend_throw_error(NULL, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), ZSTR_VAL(function_name), scope ? ZSTR_VAL(scope->name) : "");
  1211. fbc = NULL;
  1212. }
  1213. }
  1214. }
  1215. if (UNEXPECTED(!key)) {
  1216. zend_string_release_ex(lc_function_name, 0);
  1217. }
  1218. return fbc;
  1219. }
  1220. /* }}} */
  1221. ZEND_API void zend_class_init_statics(zend_class_entry *class_type) /* {{{ */
  1222. {
  1223. int i;
  1224. zval *p;
  1225. if (!CE_STATIC_MEMBERS(class_type) && class_type->default_static_members_count) {
  1226. if (class_type->parent) {
  1227. zend_class_init_statics(class_type->parent);
  1228. }
  1229. #if ZTS
  1230. CG(static_members_table)[(zend_intptr_t)(class_type->static_members_table)] = emalloc(sizeof(zval) * class_type->default_static_members_count);
  1231. #else
  1232. class_type->static_members_table = emalloc(sizeof(zval) * class_type->default_static_members_count);
  1233. #endif
  1234. for (i = 0; i < class_type->default_static_members_count; i++) {
  1235. p = &class_type->default_static_members_table[i];
  1236. if (Z_TYPE_P(p) == IS_INDIRECT) {
  1237. zval *q = &CE_STATIC_MEMBERS(class_type->parent)[i];
  1238. ZVAL_DEINDIRECT(q);
  1239. ZVAL_INDIRECT(&CE_STATIC_MEMBERS(class_type)[i], q);
  1240. } else {
  1241. ZVAL_COPY_OR_DUP(&CE_STATIC_MEMBERS(class_type)[i], p);
  1242. }
  1243. }
  1244. }
  1245. } /* }}} */
  1246. ZEND_API zval *zend_std_get_static_property(zend_class_entry *ce, zend_string *property_name, zend_bool silent) /* {{{ */
  1247. {
  1248. zend_property_info *property_info = zend_hash_find_ptr(&ce->properties_info, property_name);
  1249. zval *ret;
  1250. if (UNEXPECTED(property_info == NULL)) {
  1251. goto undeclared_property;
  1252. }
  1253. if (UNEXPECTED(!zend_verify_property_access(property_info, ce))) {
  1254. if (!silent) {
  1255. zend_throw_error(NULL, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ZSTR_VAL(ce->name), ZSTR_VAL(property_name));
  1256. }
  1257. return NULL;
  1258. }
  1259. if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0)) {
  1260. goto undeclared_property;
  1261. }
  1262. if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
  1263. if (UNEXPECTED(zend_update_class_constants(ce)) != SUCCESS) {
  1264. return NULL;
  1265. }
  1266. }
  1267. /* check if static properties were destoyed */
  1268. if (UNEXPECTED(CE_STATIC_MEMBERS(ce) == NULL)) {
  1269. if (ce->type == ZEND_INTERNAL_CLASS) {
  1270. zend_class_init_statics(ce);
  1271. } else {
  1272. undeclared_property:
  1273. if (!silent) {
  1274. zend_throw_error(NULL, "Access to undeclared static property: %s::$%s", ZSTR_VAL(ce->name), ZSTR_VAL(property_name));
  1275. }
  1276. return NULL;
  1277. }
  1278. }
  1279. ret = CE_STATIC_MEMBERS(ce) + property_info->offset;
  1280. ZVAL_DEINDIRECT(ret);
  1281. return ret;
  1282. }
  1283. /* }}} */
  1284. ZEND_API ZEND_COLD zend_bool zend_std_unset_static_property(zend_class_entry *ce, zend_string *property_name) /* {{{ */
  1285. {
  1286. zend_throw_error(NULL, "Attempt to unset static property %s::$%s", ZSTR_VAL(ce->name), ZSTR_VAL(property_name));
  1287. return 0;
  1288. }
  1289. /* }}} */
  1290. ZEND_API zend_function *zend_std_get_constructor(zend_object *zobj) /* {{{ */
  1291. {
  1292. zend_function *constructor = zobj->ce->constructor;
  1293. zend_class_entry *scope;
  1294. if (constructor) {
  1295. if (constructor->op_array.fn_flags & ZEND_ACC_PUBLIC) {
  1296. /* No further checks necessary */
  1297. } else if (constructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
  1298. /* Ensure that if we're calling a private function, we're allowed to do so.
  1299. */
  1300. if (EG(fake_scope)) {
  1301. scope = EG(fake_scope);
  1302. } else {
  1303. scope = zend_get_executed_scope();
  1304. }
  1305. if (UNEXPECTED(constructor->common.scope != scope)) {
  1306. if (scope) {
  1307. zend_throw_error(NULL, "Call to private %s::%s() from context '%s'", ZSTR_VAL(constructor->common.scope->name), ZSTR_VAL(constructor->common.function_name), ZSTR_VAL(scope->name));
  1308. constructor = NULL;
  1309. } else {
  1310. zend_throw_error(NULL, "Call to private %s::%s() from invalid context", ZSTR_VAL(constructor->common.scope->name), ZSTR_VAL(constructor->common.function_name));
  1311. constructor = NULL;
  1312. }
  1313. }
  1314. } else if ((constructor->common.fn_flags & ZEND_ACC_PROTECTED)) {
  1315. /* Ensure that if we're calling a protected function, we're allowed to do so.
  1316. * Constructors only have prototype if they are defined by an interface but
  1317. * it is the compilers responsibility to take care of the prototype.
  1318. */
  1319. if (EG(fake_scope)) {
  1320. scope = EG(fake_scope);
  1321. } else {
  1322. scope = zend_get_executed_scope();
  1323. }
  1324. if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(constructor), scope))) {
  1325. if (scope) {
  1326. zend_throw_error(NULL, "Call to protected %s::%s() from context '%s'", ZSTR_VAL(constructor->common.scope->name), ZSTR_VAL(constructor->common.function_name), ZSTR_VAL(scope->name));
  1327. constructor = NULL;
  1328. } else {
  1329. zend_throw_error(NULL, "Call to protected %s::%s() from invalid context", ZSTR_VAL(constructor->common.scope->name), ZSTR_VAL(constructor->common.function_name));
  1330. constructor = NULL;
  1331. }
  1332. }
  1333. }
  1334. }
  1335. return constructor;
  1336. }
  1337. /* }}} */
  1338. ZEND_API int zend_std_compare_objects(zval *o1, zval *o2) /* {{{ */
  1339. {
  1340. zend_object *zobj1, *zobj2;
  1341. zobj1 = Z_OBJ_P(o1);
  1342. zobj2 = Z_OBJ_P(o2);
  1343. if (zobj1 == zobj2) {
  1344. return 0; /* the same object */
  1345. }
  1346. if (zobj1->ce != zobj2->ce) {
  1347. return 1; /* different classes */
  1348. }
  1349. if (!zobj1->properties && !zobj2->properties) {
  1350. zend_property_info *info;
  1351. if (!zobj1->ce->default_properties_count) {
  1352. return 0;
  1353. }
  1354. /* It's enough to protect only one of the objects.
  1355. * The second one may be referenced from the first and this may cause
  1356. * false recursion detection.
  1357. */
  1358. /* use bitwise OR to make only one conditional jump */
  1359. if (UNEXPECTED(Z_IS_RECURSIVE_P(o1))) {
  1360. zend_error_noreturn(E_ERROR, "Nesting level too deep - recursive dependency?");
  1361. }
  1362. Z_PROTECT_RECURSION_P(o1);
  1363. ZEND_HASH_FOREACH_PTR(&zobj1->ce->properties_info, info) {
  1364. zval *p1 = OBJ_PROP(zobj1, info->offset);
  1365. zval *p2 = OBJ_PROP(zobj2, info->offset);
  1366. if (info->flags & ZEND_ACC_STATIC) {
  1367. continue;
  1368. }
  1369. if (Z_TYPE_P(p1) != IS_UNDEF) {
  1370. if (Z_TYPE_P(p2) != IS_UNDEF) {
  1371. zval result;
  1372. if (compare_function(&result, p1, p2)==FAILURE) {
  1373. Z_UNPROTECT_RECURSION_P(o1);
  1374. return 1;
  1375. }
  1376. if (Z_LVAL(result) != 0) {
  1377. Z_UNPROTECT_RECURSION_P(o1);
  1378. return Z_LVAL(result);
  1379. }
  1380. } else {
  1381. Z_UNPROTECT_RECURSION_P(o1);
  1382. return 1;
  1383. }
  1384. } else {
  1385. if (Z_TYPE_P(p2) != IS_UNDEF) {
  1386. Z_UNPROTECT_RECURSION_P(o1);
  1387. return 1;
  1388. }
  1389. }
  1390. } ZEND_HASH_FOREACH_END();
  1391. Z_UNPROTECT_RECURSION_P(o1);
  1392. return 0;
  1393. } else {
  1394. if (!zobj1->properties) {
  1395. rebuild_object_properties(zobj1);
  1396. }
  1397. if (!zobj2->properties) {
  1398. rebuild_object_properties(zobj2);
  1399. }
  1400. return zend_compare_symbol_tables(zobj1->properties, zobj2->properties);
  1401. }
  1402. }
  1403. /* }}} */
  1404. ZEND_API int zend_std_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
  1405. {
  1406. zend_object *zobj;
  1407. int result;
  1408. zval *value = NULL;
  1409. zend_string *name, *tmp_name;
  1410. uintptr_t property_offset;
  1411. zobj = Z_OBJ_P(object);
  1412. name = zval_get_tmp_string(member, &tmp_name);
  1413. property_offset = zend_get_property_offset(zobj->ce, name, 1, cache_slot);
  1414. if (EXPECTED(IS_VALID_PROPERTY_OFFSET(property_offset))) {
  1415. value = OBJ_PROP(zobj, property_offset);
  1416. if (Z_TYPE_P(value) != IS_UNDEF) {
  1417. goto found;
  1418. }
  1419. } else if (EXPECTED(IS_DYNAMIC_PROPERTY_OFFSET(property_offset))) {
  1420. if (EXPECTED(zobj->properties != NULL)) {
  1421. if (!IS_UNKNOWN_DYNAMIC_PROPERTY_OFFSET(property_offset)) {
  1422. uintptr_t idx = ZEND_DECODE_DYN_PROP_OFFSET(property_offset);
  1423. if (EXPECTED(idx < zobj->properties->nNumUsed * sizeof(Bucket))) {
  1424. Bucket *p = (Bucket*)((char*)zobj->properties->arData + idx);
  1425. if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF) &&
  1426. (EXPECTED(p->key == name) ||
  1427. (EXPECTED(p->h == ZSTR_H(name)) &&
  1428. EXPECTED(p->key != NULL) &&
  1429. EXPECTED(zend_string_equal_content(p->key, name))))) {
  1430. value = &p->val;
  1431. goto found;
  1432. }
  1433. }
  1434. CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_DYNAMIC_PROPERTY_OFFSET);
  1435. }
  1436. value = zend_hash_find(zobj->properties, name);
  1437. if (value) {
  1438. if (cache_slot) {
  1439. uintptr_t idx = (char*)value - (char*)zobj->properties->arData;
  1440. CACHE_PTR_EX(cache_slot + 1, (void*)ZEND_ENCODE_DYN_PROP_OFFSET(idx));
  1441. }
  1442. found:
  1443. if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY) {
  1444. result = zend_is_true(value);
  1445. } else if (has_set_exists < ZEND_PROPERTY_NOT_EMPTY) {
  1446. ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_ISSET);
  1447. ZVAL_DEREF(value);
  1448. result = (Z_TYPE_P(value) != IS_NULL);
  1449. } else {
  1450. ZEND_ASSERT(has_set_exists == ZEND_PROPERTY_EXISTS);
  1451. result = 1;
  1452. }
  1453. goto exit;
  1454. }
  1455. }
  1456. } else if (UNEXPECTED(EG(exception))) {
  1457. result = 0;
  1458. goto exit;
  1459. }
  1460. result = 0;
  1461. if ((has_set_exists != ZEND_PROPERTY_EXISTS) && zobj->ce->__isset) {
  1462. uint32_t *guard = zend_get_property_guard(zobj, name);
  1463. if (!((*guard) & IN_ISSET)) {
  1464. zval rv;
  1465. /* have issetter - try with it! */
  1466. if (!tmp_name && !ZSTR_IS_INTERNED(name)) {
  1467. tmp_name = zend_string_copy(name);
  1468. }
  1469. GC_ADDREF(zobj);
  1470. (*guard) |= IN_ISSET; /* prevent circular getting */
  1471. zend_std_call_issetter(zobj, name, &rv);
  1472. result = zend_is_true(&rv);
  1473. zval_ptr_dtor(&rv);
  1474. if (has_set_exists == ZEND_PROPERTY_NOT_EMPTY && result) {
  1475. if (EXPECTED(!EG(exception)) && zobj->ce->__get && !((*guard) & IN_GET)) {
  1476. (*guard) |= IN_GET;
  1477. zend_std_call_getter(zobj, name, &rv);
  1478. (*guard) &= ~IN_GET;
  1479. result = i_zend_is_true(&rv);
  1480. zval_ptr_dtor(&rv);
  1481. } else {
  1482. result = 0;
  1483. }
  1484. }
  1485. (*guard) &= ~IN_ISSET;
  1486. OBJ_RELEASE(zobj);
  1487. }
  1488. }
  1489. exit:
  1490. zend_tmp_string_release(tmp_name);
  1491. return result;
  1492. }
  1493. /* }}} */
  1494. ZEND_API zend_string *zend_std_get_class_name(const zend_object *zobj) /* {{{ */
  1495. {
  1496. return zend_string_copy(zobj->ce->name);
  1497. }
  1498. /* }}} */
  1499. ZEND_API int zend_std_cast_object_tostring(zval *readobj, zval *writeobj, int type) /* {{{ */
  1500. {
  1501. zval retval;
  1502. zend_class_entry *ce;
  1503. switch (type) {
  1504. case IS_STRING:
  1505. ce = Z_OBJCE_P(readobj);
  1506. if (ce->__tostring &&
  1507. (zend_call_method_with_0_params(readobj, ce, &ce->__tostring, "__tostring", &retval) || EG(exception))) {
  1508. if (UNEXPECTED(EG(exception) != NULL)) {
  1509. zval *msg, ex, rv;
  1510. zval_ptr_dtor(&retval);
  1511. ZVAL_OBJ(&ex, EG(exception));
  1512. EG(exception) = NULL;
  1513. msg = zend_read_property(Z_OBJCE(ex), &ex, "message", sizeof("message") - 1, 1, &rv);
  1514. if (UNEXPECTED(Z_TYPE_P(msg) != IS_STRING)) {
  1515. ZVAL_EMPTY_STRING(&rv);
  1516. msg = &rv;
  1517. }
  1518. zend_error_noreturn(E_ERROR,
  1519. "Method %s::__toString() must not throw an exception, caught %s: %s",
  1520. ZSTR_VAL(ce->name), ZSTR_VAL(Z_OBJCE(ex)->name), Z_STRVAL_P(msg));
  1521. return FAILURE;
  1522. }
  1523. if (EXPECTED(Z_TYPE(retval) == IS_STRING)) {
  1524. ZVAL_COPY_VALUE(writeobj, &retval);
  1525. return SUCCESS;
  1526. } else {
  1527. zval_ptr_dtor(&retval);
  1528. ZVAL_EMPTY_STRING(writeobj);
  1529. zend_error(E_RECOVERABLE_ERROR, "Method %s::__toString() must return a string value", ZSTR_VAL(ce->name));
  1530. return SUCCESS;
  1531. }
  1532. }
  1533. return FAILURE;
  1534. case _IS_BOOL:
  1535. ZVAL_TRUE(writeobj);
  1536. return SUCCESS;
  1537. case IS_LONG:
  1538. ce = Z_OBJCE_P(readobj);
  1539. zend_error(E_NOTICE, "Object of class %s could not be converted to int", ZSTR_VAL(ce->name));
  1540. ZVAL_LONG(writeobj, 1);
  1541. return SUCCESS;
  1542. case IS_DOUBLE:
  1543. ce = Z_OBJCE_P(readobj);
  1544. zend_error(E_NOTICE, "Object of class %s could not be converted to float", ZSTR_VAL(ce->name));
  1545. ZVAL_DOUBLE(writeobj, 1);
  1546. return SUCCESS;
  1547. case _IS_NUMBER:
  1548. ce = Z_OBJCE_P(readobj);
  1549. zend_error(E_NOTICE, "Object of class %s could not be converted to number", ZSTR_VAL(ce->name));
  1550. ZVAL_LONG(writeobj, 1);
  1551. return SUCCESS;
  1552. default:
  1553. ZVAL_NULL(writeobj);
  1554. break;
  1555. }
  1556. return FAILURE;
  1557. }
  1558. /* }}} */
  1559. ZEND_API int zend_std_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **obj_ptr) /* {{{ */
  1560. {
  1561. zval *func;
  1562. zend_class_entry *ce = Z_OBJCE_P(obj);
  1563. if ((func = zend_hash_find_ex(&ce->function_table, ZSTR_KNOWN(ZEND_STR_MAGIC_INVOKE), 1)) == NULL) {
  1564. return FAILURE;
  1565. }
  1566. *fptr_ptr = Z_FUNC_P(func);
  1567. *ce_ptr = ce;
  1568. if ((*fptr_ptr)->common.fn_flags & ZEND_ACC_STATIC) {
  1569. if (obj_ptr) {
  1570. *obj_ptr = NULL;
  1571. }
  1572. } else {
  1573. if (obj_ptr) {
  1574. *obj_ptr = Z_OBJ_P(obj);
  1575. }
  1576. }
  1577. return SUCCESS;
  1578. }
  1579. /* }}} */
  1580. ZEND_API const zend_object_handlers std_object_handlers = {
  1581. 0, /* offset */
  1582. zend_object_std_dtor, /* free_obj */
  1583. zend_objects_destroy_object, /* dtor_obj */
  1584. zend_objects_clone_obj, /* clone_obj */
  1585. zend_std_read_property, /* read_property */
  1586. zend_std_write_property, /* write_property */
  1587. zend_std_read_dimension, /* read_dimension */
  1588. zend_std_write_dimension, /* write_dimension */
  1589. zend_std_get_property_ptr_ptr, /* get_property_ptr_ptr */
  1590. NULL, /* get */
  1591. NULL, /* set */
  1592. zend_std_has_property, /* has_property */
  1593. zend_std_unset_property, /* unset_property */
  1594. zend_std_has_dimension, /* has_dimension */
  1595. zend_std_unset_dimension, /* unset_dimension */
  1596. zend_std_get_properties, /* get_properties */
  1597. zend_std_get_method, /* get_method */
  1598. NULL, /* call_method */
  1599. zend_std_get_constructor, /* get_constructor */
  1600. zend_std_get_class_name, /* get_class_name */
  1601. zend_std_compare_objects, /* compare_objects */
  1602. zend_std_cast_object_tostring, /* cast_object */
  1603. NULL, /* count_elements */
  1604. zend_std_get_debug_info, /* get_debug_info */
  1605. zend_std_get_closure, /* get_closure */
  1606. zend_std_get_gc, /* get_gc */
  1607. NULL, /* do_operation */
  1608. NULL, /* compare */
  1609. };
  1610. /*
  1611. * Local variables:
  1612. * tab-width: 4
  1613. * c-basic-offset: 4
  1614. * indent-tabs-mode: t
  1615. * End:
  1616. * vim600: sw=4 ts=4 fdm=marker
  1617. * vim<600: sw=4 ts=4
  1618. */