This source file includes following definitions.
- rebuild_object_properties
- zend_std_get_properties
- zend_std_get_gc
- zend_std_get_debug_info
- zend_std_call_getter
- zend_std_call_setter
- zend_std_call_unsetter
- zend_std_call_issetter
- zend_verify_property_access
- is_derived_class
- zend_get_property_info_quick
- zend_get_property_info
- zend_check_property_access
- zend_get_property_guard
- zend_std_read_property
- zend_std_write_property
- zend_std_read_dimension
- zend_std_write_dimension
- zend_std_has_dimension
- zend_std_get_property_ptr_ptr
- zend_std_unset_property
- zend_std_unset_dimension
- zend_std_call_user_call
- zend_check_private_int
- zend_check_private
- zend_check_protected
- zend_get_user_call_function
- zend_std_get_method
- zend_std_callstatic_user_call
- zend_get_user_callstatic_function
- zend_std_get_static_method
- zend_std_get_static_property
- zend_std_unset_static_property
- zend_std_get_constructor
- zend_std_compare_objects
- zend_std_has_property
- zend_std_object_get_class
- zend_std_object_get_class_name
- zend_std_cast_object_tostring
- zend_std_get_closure
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 #include "zend.h"
23 #include "zend_globals.h"
24 #include "zend_variables.h"
25 #include "zend_API.h"
26 #include "zend_objects.h"
27 #include "zend_objects_API.h"
28 #include "zend_object_handlers.h"
29 #include "zend_interfaces.h"
30 #include "zend_closures.h"
31 #include "zend_compile.h"
32
33 #define DEBUG_OBJECT_HANDLERS 0
34
35 #define Z_OBJ_P(zval_p) \
36 ((zend_object*)(EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zval_p)].bucket.obj.object))
37
38 #define Z_OBJ_PROTECT_RECURSION(zval_p) \
39 do { \
40 if (EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zval_p)].apply_count++ >= 3) { \
41 zend_error(E_ERROR, "Nesting level too deep - recursive dependency?"); \
42 } \
43 } while (0)
44
45
46 #define Z_OBJ_UNPROTECT_RECURSION(zval_p) \
47 EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(zval_p)].apply_count--
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 ZEND_API void rebuild_object_properties(zend_object *zobj)
67 {
68 if (!zobj->properties) {
69 HashPosition pos;
70 zend_property_info *prop_info;
71 zend_class_entry *ce = zobj->ce;
72
73 ALLOC_HASHTABLE(zobj->properties);
74 zend_hash_init(zobj->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
75 if (ce->default_properties_count) {
76 for (zend_hash_internal_pointer_reset_ex(&ce->properties_info, &pos);
77 zend_hash_get_current_data_ex(&ce->properties_info, (void**)&prop_info, &pos) == SUCCESS;
78 zend_hash_move_forward_ex(&ce->properties_info, &pos)) {
79 if (
80 (prop_info->flags & ZEND_ACC_STATIC) == 0 &&
81 prop_info->offset >= 0 &&
82 zobj->properties_table[prop_info->offset]) {
83 zend_hash_quick_add(zobj->properties, prop_info->name, prop_info->name_length+1, prop_info->h, (void**)&zobj->properties_table[prop_info->offset], sizeof(zval*), (void**)&zobj->properties_table[prop_info->offset]);
84 }
85 }
86 while (ce->parent && ce->parent->default_properties_count) {
87 ce = ce->parent;
88 for (zend_hash_internal_pointer_reset_ex(&ce->properties_info, &pos);
89 zend_hash_get_current_data_ex(&ce->properties_info, (void**)&prop_info, &pos) == SUCCESS;
90 zend_hash_move_forward_ex(&ce->properties_info, &pos)) {
91 if (prop_info->ce == ce &&
92 (prop_info->flags & ZEND_ACC_STATIC) == 0 &&
93 (prop_info->flags & ZEND_ACC_PRIVATE) != 0 &&
94 prop_info->offset >= 0 &&
95 zobj->properties_table[prop_info->offset]) {
96 zend_hash_quick_add(zobj->properties, prop_info->name, prop_info->name_length+1, prop_info->h, (void**)&zobj->properties_table[prop_info->offset], sizeof(zval*), (void**)&zobj->properties_table[prop_info->offset]);
97 }
98 }
99 }
100 }
101 }
102 }
103
104
105 ZEND_API HashTable *zend_std_get_properties(zval *object TSRMLS_DC)
106 {
107 zend_object *zobj;
108 zobj = Z_OBJ_P(object);
109 if (!zobj->properties) {
110 rebuild_object_properties(zobj);
111 }
112 return zobj->properties;
113 }
114
115
116 ZEND_API HashTable *zend_std_get_gc(zval *object, zval ***table, int *n TSRMLS_DC)
117 {
118 if (Z_OBJ_HANDLER_P(object, get_properties) != zend_std_get_properties) {
119 *table = NULL;
120 *n = 0;
121 return Z_OBJ_HANDLER_P(object, get_properties)(object TSRMLS_CC);
122 } else {
123 zend_object *zobj = Z_OBJ_P(object);
124
125 if (zobj->properties) {
126 *table = NULL;
127 *n = 0;
128 return zobj->properties;
129 } else {
130 *table = zobj->properties_table;
131 *n = zobj->ce->default_properties_count;
132 return NULL;
133 }
134 }
135 }
136
137
138 ZEND_API HashTable *zend_std_get_debug_info(zval *object, int *is_temp TSRMLS_DC)
139 {
140 zend_class_entry *ce = Z_OBJCE_P(object);
141 zval *retval = NULL;
142
143 if (!ce->__debugInfo) {
144 *is_temp = 0;
145 return Z_OBJ_HANDLER_P(object, get_properties)
146 ? Z_OBJ_HANDLER_P(object, get_properties)(object TSRMLS_CC)
147 : NULL;
148 }
149
150 zend_call_method_with_0_params(&object, ce, &ce->__debugInfo, ZEND_DEBUGINFO_FUNC_NAME, &retval);
151 if (retval && Z_TYPE_P(retval) == IS_ARRAY) {
152 HashTable *ht = Z_ARRVAL_P(retval);
153 if (Z_REFCOUNT_P(retval) <= 1) {
154 *is_temp = 1;
155 efree(retval);
156 return ht;
157 } else {
158 *is_temp = 0;
159 zval_ptr_dtor(&retval);
160 }
161 return ht;
162 }
163 if (retval && Z_TYPE_P(retval) == IS_NULL) {
164 zval ret;
165 array_init(&ret);
166 *is_temp = 1;
167 zval_ptr_dtor(&retval);
168 return Z_ARRVAL(ret);
169 }
170
171 zend_error_noreturn(E_ERROR, ZEND_DEBUGINFO_FUNC_NAME "() must return an array");
172
173 return NULL;
174 }
175
176
177 static zval *zend_std_call_getter(zval *object, zval *member TSRMLS_DC)
178 {
179 zval *retval = NULL;
180 zend_class_entry *ce = Z_OBJCE_P(object);
181
182
183
184
185
186
187
188 SEPARATE_ARG_IF_REF(member);
189
190 zend_call_method_with_1_params(&object, ce, &ce->__get, ZEND_GET_FUNC_NAME, &retval, member);
191
192 zval_ptr_dtor(&member);
193
194 if (retval) {
195 Z_DELREF_P(retval);
196 }
197
198 return retval;
199 }
200
201
202 static int zend_std_call_setter(zval *object, zval *member, zval *value TSRMLS_DC)
203 {
204 zval *retval = NULL;
205 int result;
206 zend_class_entry *ce = Z_OBJCE_P(object);
207
208 SEPARATE_ARG_IF_REF(member);
209 Z_ADDREF_P(value);
210
211
212
213
214
215
216
217 zend_call_method_with_2_params(&object, ce, &ce->__set, ZEND_SET_FUNC_NAME, &retval, member, value);
218
219 zval_ptr_dtor(&member);
220 zval_ptr_dtor(&value);
221
222 if (retval) {
223 result = i_zend_is_true(retval) ? SUCCESS : FAILURE;
224 zval_ptr_dtor(&retval);
225 return result;
226 } else {
227 return FAILURE;
228 }
229 }
230
231
232 static void zend_std_call_unsetter(zval *object, zval *member TSRMLS_DC)
233 {
234 zend_class_entry *ce = Z_OBJCE_P(object);
235
236
237
238
239
240 SEPARATE_ARG_IF_REF(member);
241
242 zend_call_method_with_1_params(&object, ce, &ce->__unset, ZEND_UNSET_FUNC_NAME, NULL, member);
243
244 zval_ptr_dtor(&member);
245 }
246
247
248 static zval *zend_std_call_issetter(zval *object, zval *member TSRMLS_DC)
249 {
250 zval *retval = NULL;
251 zend_class_entry *ce = Z_OBJCE_P(object);
252
253
254
255
256
257
258
259 SEPARATE_ARG_IF_REF(member);
260
261 zend_call_method_with_1_params(&object, ce, &ce->__isset, ZEND_ISSET_FUNC_NAME, &retval, member);
262
263 zval_ptr_dtor(&member);
264
265 return retval;
266 }
267
268
269 static zend_always_inline int zend_verify_property_access(zend_property_info *property_info, zend_class_entry *ce TSRMLS_DC)
270 {
271 switch (property_info->flags & ZEND_ACC_PPP_MASK) {
272 case ZEND_ACC_PUBLIC:
273 return 1;
274 case ZEND_ACC_PROTECTED:
275 return zend_check_protected(property_info->ce, EG(scope));
276 case ZEND_ACC_PRIVATE:
277 if ((ce==EG(scope) || property_info->ce == EG(scope)) && EG(scope)) {
278 return 1;
279 } else {
280 return 0;
281 }
282 break;
283 }
284 return 0;
285 }
286
287
288 static zend_always_inline zend_bool is_derived_class(zend_class_entry *child_class, zend_class_entry *parent_class)
289 {
290 child_class = child_class->parent;
291 while (child_class) {
292 if (child_class == parent_class) {
293 return 1;
294 }
295 child_class = child_class->parent;
296 }
297
298 return 0;
299 }
300
301
302 static zend_always_inline struct _zend_property_info *zend_get_property_info_quick(zend_class_entry *ce, zval *member, int silent, const zend_literal *key TSRMLS_DC)
303 {
304 zend_property_info *property_info;
305 zend_property_info *scope_property_info;
306 zend_bool denied_access = 0;
307 ulong h;
308
309 if (key && (property_info = CACHED_POLYMORPHIC_PTR(key->cache_slot, ce)) != NULL) {
310 return property_info;
311 }
312
313 if (UNEXPECTED(Z_STRVAL_P(member)[0] == '\0')) {
314 if (!silent) {
315 if (Z_STRLEN_P(member) == 0) {
316 zend_error_noreturn(E_ERROR, "Cannot access empty property");
317 } else {
318 zend_error_noreturn(E_ERROR, "Cannot access property started with '\\0'");
319 }
320 }
321 return NULL;
322 }
323 property_info = NULL;
324 h = key ? key->hash_value : zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
325 if (zend_hash_quick_find(&ce->properties_info, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, h, (void **) &property_info)==SUCCESS) {
326 if (UNEXPECTED((property_info->flags & ZEND_ACC_SHADOW) != 0)) {
327
328 property_info = NULL;
329 } else {
330 if (EXPECTED(zend_verify_property_access(property_info, ce TSRMLS_CC) != 0)) {
331 if (EXPECTED((property_info->flags & ZEND_ACC_CHANGED) != 0)
332 && EXPECTED(!(property_info->flags & ZEND_ACC_PRIVATE))) {
333
334
335
336
337 } else {
338 if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) != 0) && !silent) {
339 zend_error(E_STRICT, "Accessing static property %s::$%s as non static", ce->name, Z_STRVAL_P(member));
340 }
341 if (key) {
342 CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, property_info);
343 }
344 return property_info;
345 }
346 } else {
347
348 denied_access = 1;
349 }
350 }
351 }
352 if (EG(scope) != ce
353 && EG(scope)
354 && is_derived_class(ce, EG(scope))
355 && zend_hash_quick_find(&EG(scope)->properties_info, Z_STRVAL_P(member), Z_STRLEN_P(member)+1, h, (void **) &scope_property_info)==SUCCESS
356 && scope_property_info->flags & ZEND_ACC_PRIVATE) {
357 if (key) {
358 CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, scope_property_info);
359 }
360 return scope_property_info;
361 } else if (property_info) {
362 if (UNEXPECTED(denied_access != 0)) {
363
364 if (!silent) {
365 zend_error_noreturn(E_ERROR, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ce->name, Z_STRVAL_P(member));
366 }
367 return NULL;
368 } else {
369
370 if (key) {
371 CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, property_info);
372 }
373 }
374 } else {
375 EG(std_property_info).flags = ZEND_ACC_PUBLIC;
376 EG(std_property_info).name = Z_STRVAL_P(member);
377 EG(std_property_info).name_length = Z_STRLEN_P(member);
378 EG(std_property_info).h = h;
379 EG(std_property_info).ce = ce;
380 EG(std_property_info).offset = -1;
381 property_info = &EG(std_property_info);
382 }
383 return property_info;
384 }
385
386
387 ZEND_API struct _zend_property_info *zend_get_property_info(zend_class_entry *ce, zval *member, int silent TSRMLS_DC)
388 {
389 return zend_get_property_info_quick(ce, member, silent, NULL TSRMLS_CC);
390 }
391
392
393 ZEND_API int zend_check_property_access(zend_object *zobj, const char *prop_info_name, int prop_info_name_len TSRMLS_DC)
394 {
395 zend_property_info *property_info;
396 const char *class_name, *prop_name;
397 zval member;
398 int prop_name_len;
399
400 zend_unmangle_property_name_ex(prop_info_name, prop_info_name_len, &class_name, &prop_name, &prop_name_len);
401 ZVAL_STRINGL(&member, prop_name, prop_name_len, 0);
402 property_info = zend_get_property_info_quick(zobj->ce, &member, 1, NULL TSRMLS_CC);
403 if (!property_info) {
404 return FAILURE;
405 }
406 if (class_name && class_name[0] != '*') {
407 if (!(property_info->flags & ZEND_ACC_PRIVATE)) {
408
409 return FAILURE;
410 } else if (strcmp(prop_info_name+1, property_info->name+1)) {
411
412 return FAILURE;
413 }
414 }
415 return zend_verify_property_access(property_info, zobj->ce TSRMLS_CC) ? SUCCESS : FAILURE;
416 }
417
418
419 static int zend_get_property_guard(zend_object *zobj, zend_property_info *property_info, zval *member, zend_guard **pguard)
420 {
421 zend_property_info info;
422 zend_guard stub;
423
424 if (!property_info) {
425 property_info = &info;
426 info.name = Z_STRVAL_P(member);
427 info.name_length = Z_STRLEN_P(member);
428 info.h = zend_get_hash_value(Z_STRVAL_P(member), Z_STRLEN_P(member) + 1);
429 } else if(property_info->name[0] == '\0'){
430 const char *class_name = NULL, *prop_name = NULL;
431 zend_unmangle_property_name(property_info->name, property_info->name_length, &class_name, &prop_name);
432 if(class_name) {
433
434 info.name = prop_name;
435 info.name_length = strlen(prop_name);
436 info.h = zend_get_hash_value(info.name, info.name_length+1);
437 property_info = &info;
438 }
439 }
440 if (!zobj->guards) {
441 ALLOC_HASHTABLE(zobj->guards);
442 zend_hash_init(zobj->guards, 0, NULL, NULL, 0);
443 } else if (zend_hash_quick_find(zobj->guards, property_info->name, property_info->name_length+1, property_info->h, (void **) pguard) == SUCCESS) {
444 return SUCCESS;
445 }
446 stub.in_get = 0;
447 stub.in_set = 0;
448 stub.in_unset = 0;
449 stub.in_isset = 0;
450 return zend_hash_quick_add(zobj->guards, property_info->name, property_info->name_length+1, property_info->h, (void**)&stub, sizeof(stub), (void**) pguard);
451 }
452
453
454 zval *zend_std_read_property(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC)
455 {
456 zend_object *zobj;
457 zval *tmp_member = NULL;
458 zval **retval;
459 zval *rv = NULL;
460 zend_property_info *property_info;
461 int silent;
462
463 silent = (type == BP_VAR_IS);
464 zobj = Z_OBJ_P(object);
465
466 if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
467 ALLOC_ZVAL(tmp_member);
468 *tmp_member = *member;
469 INIT_PZVAL(tmp_member);
470 zval_copy_ctor(tmp_member);
471 convert_to_string(tmp_member);
472 member = tmp_member;
473 key = NULL;
474 }
475
476 #if DEBUG_OBJECT_HANDLERS
477 fprintf(stderr, "Read object #%d property: %s\n", Z_OBJ_HANDLE_P(object), Z_STRVAL_P(member));
478 #endif
479
480
481 property_info = zend_get_property_info_quick(zobj->ce, member, silent || (zobj->ce->__get != NULL), key TSRMLS_CC);
482
483 if (UNEXPECTED(!property_info) ||
484 ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
485 property_info->offset >= 0) ?
486 (zobj->properties ?
487 ((retval = (zval**)zobj->properties_table[property_info->offset]) == NULL) :
488 (*(retval = &zobj->properties_table[property_info->offset]) == NULL)) :
489 (UNEXPECTED(!zobj->properties) ||
490 UNEXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &retval) == FAILURE)))) {
491 zend_guard *guard = NULL;
492
493 if (zobj->ce->__get &&
494 zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
495 !guard->in_get) {
496
497 Z_ADDREF_P(object);
498 if (PZVAL_IS_REF(object)) {
499 SEPARATE_ZVAL(&object);
500 }
501 guard->in_get = 1;
502 rv = zend_std_call_getter(object, member TSRMLS_CC);
503 guard->in_get = 0;
504
505 if (rv) {
506 retval = &rv;
507 if (!Z_ISREF_P(rv) &&
508 (type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)) {
509 if (Z_REFCOUNT_P(rv) > 0) {
510 zval *tmp = rv;
511
512 ALLOC_ZVAL(rv);
513 *rv = *tmp;
514 zval_copy_ctor(rv);
515 Z_UNSET_ISREF_P(rv);
516 Z_SET_REFCOUNT_P(rv, 0);
517 }
518 if (UNEXPECTED(Z_TYPE_P(rv) != IS_OBJECT)) {
519 zend_error(E_NOTICE, "Indirect modification of overloaded property %s::$%s has no effect", zobj->ce->name, Z_STRVAL_P(member));
520 }
521 }
522 } else {
523 retval = &EG(uninitialized_zval_ptr);
524 }
525 if (EXPECTED(*retval != object)) {
526 zval_ptr_dtor(&object);
527 } else {
528 Z_DELREF_P(object);
529 }
530 } else {
531 if (zobj->ce->__get && guard && guard->in_get == 1) {
532 if (Z_STRVAL_P(member)[0] == '\0') {
533 if (Z_STRLEN_P(member) == 0) {
534 zend_error(E_ERROR, "Cannot access empty property");
535 } else {
536 zend_error(E_ERROR, "Cannot access property started with '\\0'");
537 }
538 }
539 }
540 if (!silent) {
541 zend_error(E_NOTICE,"Undefined property: %s::$%s", zobj->ce->name, Z_STRVAL_P(member));
542 }
543 retval = &EG(uninitialized_zval_ptr);
544 }
545 }
546 if (UNEXPECTED(tmp_member != NULL)) {
547 Z_ADDREF_PP(retval);
548 zval_ptr_dtor(&tmp_member);
549 Z_DELREF_PP(retval);
550 }
551 return *retval;
552 }
553
554
555 ZEND_API void zend_std_write_property(zval *object, zval *member, zval *value, const zend_literal *key TSRMLS_DC)
556 {
557 zend_object *zobj;
558 zval *tmp_member = NULL;
559 zval **variable_ptr;
560 zend_property_info *property_info;
561
562 zobj = Z_OBJ_P(object);
563
564 if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
565 ALLOC_ZVAL(tmp_member);
566 *tmp_member = *member;
567 INIT_PZVAL(tmp_member);
568 zval_copy_ctor(tmp_member);
569 convert_to_string(tmp_member);
570 member = tmp_member;
571 key = NULL;
572 }
573
574 property_info = zend_get_property_info_quick(zobj->ce, member, (zobj->ce->__set != NULL), key TSRMLS_CC);
575
576 if (EXPECTED(property_info != NULL) &&
577 ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
578 property_info->offset >= 0) ?
579 (zobj->properties ?
580 ((variable_ptr = (zval**)zobj->properties_table[property_info->offset]) != NULL) :
581 (*(variable_ptr = &zobj->properties_table[property_info->offset]) != NULL)) :
582 (EXPECTED(zobj->properties != NULL) &&
583 EXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &variable_ptr) == SUCCESS)))) {
584
585 if (EXPECTED(*variable_ptr != value)) {
586
587
588 if (PZVAL_IS_REF(*variable_ptr)) {
589 zval garbage = **variable_ptr;
590
591
592 Z_TYPE_PP(variable_ptr) = Z_TYPE_P(value);
593 (*variable_ptr)->value = value->value;
594 if (Z_REFCOUNT_P(value) > 0) {
595 zval_copy_ctor(*variable_ptr);
596 } else {
597 efree(value);
598 }
599 zval_dtor(&garbage);
600 } else {
601 zval *garbage = *variable_ptr;
602
603
604 Z_ADDREF_P(value);
605 if (PZVAL_IS_REF(value)) {
606 SEPARATE_ZVAL(&value);
607 }
608 *variable_ptr = value;
609 zval_ptr_dtor(&garbage);
610 }
611 }
612 } else {
613 zend_guard *guard = NULL;
614
615 if (zobj->ce->__set &&
616 zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
617 !guard->in_set) {
618 Z_ADDREF_P(object);
619 if (PZVAL_IS_REF(object)) {
620 SEPARATE_ZVAL(&object);
621 }
622 guard->in_set = 1;
623 if (zend_std_call_setter(object, member, value TSRMLS_CC) != SUCCESS) {
624
625 }
626 guard->in_set = 0;
627 zval_ptr_dtor(&object);
628 } else if (EXPECTED(property_info != NULL)) {
629
630 Z_ADDREF_P(value);
631 if (PZVAL_IS_REF(value)) {
632 SEPARATE_ZVAL(&value);
633 }
634 if (EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
635 property_info->offset >= 0) {
636 if (!zobj->properties) {
637 zobj->properties_table[property_info->offset] = value;
638 } else if (zobj->properties_table[property_info->offset]) {
639 *(zval**)zobj->properties_table[property_info->offset] = value;
640 } else {
641 zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &value, sizeof(zval *), (void**)&zobj->properties_table[property_info->offset]);
642 }
643 } else {
644 if (!zobj->properties) {
645 rebuild_object_properties(zobj);
646 }
647 zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &value, sizeof(zval *), NULL);
648 }
649 } else if (zobj->ce->__set && guard && guard->in_set == 1) {
650 if (Z_STRVAL_P(member)[0] == '\0') {
651 if (Z_STRLEN_P(member) == 0) {
652 zend_error(E_ERROR, "Cannot access empty property");
653 } else {
654 zend_error(E_ERROR, "Cannot access property started with '\\0'");
655 }
656 }
657 }
658 }
659
660 if (UNEXPECTED(tmp_member != NULL)) {
661 zval_ptr_dtor(&tmp_member);
662 }
663 }
664
665
666 zval *zend_std_read_dimension(zval *object, zval *offset, int type TSRMLS_DC)
667 {
668 zend_class_entry *ce = Z_OBJCE_P(object);
669 zval *retval;
670
671 if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC) != 0)) {
672 if(offset == NULL) {
673
674 ALLOC_INIT_ZVAL(offset);
675 } else {
676 SEPARATE_ARG_IF_REF(offset);
677 }
678 zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset);
679
680 zval_ptr_dtor(&offset);
681
682 if (UNEXPECTED(!retval)) {
683 if (UNEXPECTED(!EG(exception))) {
684 zend_error_noreturn(E_ERROR, "Undefined offset for object of type %s used as array", ce->name);
685 }
686 return 0;
687 }
688
689
690 Z_DELREF_P(retval);
691
692 return retval;
693 } else {
694 zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
695 return 0;
696 }
697 }
698
699
700 static void zend_std_write_dimension(zval *object, zval *offset, zval *value TSRMLS_DC)
701 {
702 zend_class_entry *ce = Z_OBJCE_P(object);
703
704 if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC) != 0)) {
705 if (!offset) {
706 ALLOC_INIT_ZVAL(offset);
707 } else {
708 SEPARATE_ARG_IF_REF(offset);
709 }
710 zend_call_method_with_2_params(&object, ce, NULL, "offsetset", NULL, offset, value);
711 zval_ptr_dtor(&offset);
712 } else {
713 zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
714 }
715 }
716
717
718 static int zend_std_has_dimension(zval *object, zval *offset, int check_empty TSRMLS_DC)
719 {
720 zend_class_entry *ce = Z_OBJCE_P(object);
721 zval *retval;
722 int result;
723
724 if (EXPECTED(instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC) != 0)) {
725 SEPARATE_ARG_IF_REF(offset);
726 zend_call_method_with_1_params(&object, ce, NULL, "offsetexists", &retval, offset);
727 if (EXPECTED(retval != NULL)) {
728 result = i_zend_is_true(retval);
729 zval_ptr_dtor(&retval);
730 if (check_empty && result && EXPECTED(!EG(exception))) {
731 zend_call_method_with_1_params(&object, ce, NULL, "offsetget", &retval, offset);
732 if (retval) {
733 result = i_zend_is_true(retval);
734 zval_ptr_dtor(&retval);
735 }
736 }
737 } else {
738 result = 0;
739 }
740 zval_ptr_dtor(&offset);
741 } else {
742 zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
743 return 0;
744 }
745 return result;
746 }
747
748
749 static zval **zend_std_get_property_ptr_ptr(zval *object, zval *member, int type, const zend_literal *key TSRMLS_DC)
750 {
751 zend_object *zobj;
752 zval tmp_member;
753 zval **retval;
754 zend_property_info *property_info;
755
756 zobj = Z_OBJ_P(object);
757
758 if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
759 tmp_member = *member;
760 zval_copy_ctor(&tmp_member);
761 convert_to_string(&tmp_member);
762 member = &tmp_member;
763 key = NULL;
764 }
765
766 #if DEBUG_OBJECT_HANDLERS
767 fprintf(stderr, "Ptr object #%d property: %s\n", Z_OBJ_HANDLE_P(object), Z_STRVAL_P(member));
768 #endif
769
770 property_info = zend_get_property_info_quick(zobj->ce, member, (zobj->ce->__get != NULL), key TSRMLS_CC);
771
772 if (UNEXPECTED(!property_info) ||
773 ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
774 property_info->offset >= 0) ?
775 (zobj->properties ?
776 ((retval = (zval**)zobj->properties_table[property_info->offset]) == NULL) :
777 (*(retval = &zobj->properties_table[property_info->offset]) == NULL)) :
778 (UNEXPECTED(!zobj->properties) ||
779 UNEXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &retval) == FAILURE)))) {
780 zval *new_zval;
781 zend_guard *guard;
782
783 if (!zobj->ce->__get ||
784 zend_get_property_guard(zobj, property_info, member, &guard) != SUCCESS ||
785 (property_info && guard->in_get)) {
786
787 new_zval = &EG(uninitialized_zval);
788
789 Z_ADDREF_P(new_zval);
790 if (EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
791 property_info->offset >= 0) {
792 if (!zobj->properties) {
793 zobj->properties_table[property_info->offset] = new_zval;
794 retval = &zobj->properties_table[property_info->offset];
795 } else if (zobj->properties_table[property_info->offset]) {
796 *(zval**)zobj->properties_table[property_info->offset] = new_zval;
797 retval = (zval**)zobj->properties_table[property_info->offset];
798 } else {
799 zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &new_zval, sizeof(zval *), (void**)&zobj->properties_table[property_info->offset]);
800 retval = (zval**)zobj->properties_table[property_info->offset];
801 }
802 } else {
803 if (!zobj->properties) {
804 rebuild_object_properties(zobj);
805 }
806 zend_hash_quick_update(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, &new_zval, sizeof(zval *), (void **) &retval);
807 }
808
809
810
811 if (UNEXPECTED(type == BP_VAR_RW || type == BP_VAR_R)) {
812 zend_error(E_NOTICE, "Undefined property: %s::$%s", zobj->ce->name, Z_STRVAL_P(member));
813 }
814 } else {
815
816 retval = NULL;
817 }
818 }
819 if (UNEXPECTED(member == &tmp_member)) {
820 zval_dtor(member);
821 }
822 return retval;
823 }
824
825
826 static void zend_std_unset_property(zval *object, zval *member, const zend_literal *key TSRMLS_DC)
827 {
828 zend_object *zobj;
829 zval *tmp_member = NULL;
830 zend_property_info *property_info;
831
832 zobj = Z_OBJ_P(object);
833
834 if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
835 ALLOC_ZVAL(tmp_member);
836 *tmp_member = *member;
837 INIT_PZVAL(tmp_member);
838 zval_copy_ctor(tmp_member);
839 convert_to_string(tmp_member);
840 member = tmp_member;
841 key = NULL;
842 }
843
844 property_info = zend_get_property_info_quick(zobj->ce, member, (zobj->ce->__unset != NULL), key TSRMLS_CC);
845
846 if (EXPECTED(property_info != NULL) &&
847 EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
848 !zobj->properties &&
849 property_info->offset >= 0 &&
850 EXPECTED(zobj->properties_table[property_info->offset] != NULL)) {
851 zval_ptr_dtor(&zobj->properties_table[property_info->offset]);
852 zobj->properties_table[property_info->offset] = NULL;
853 } else if (UNEXPECTED(!property_info) ||
854 !zobj->properties ||
855 UNEXPECTED(zend_hash_quick_del(zobj->properties, property_info->name, property_info->name_length+1, property_info->h) == FAILURE)) {
856 zend_guard *guard = NULL;
857
858 if (zobj->ce->__unset &&
859 zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
860 !guard->in_unset) {
861
862 Z_ADDREF_P(object);
863 if (PZVAL_IS_REF(object)) {
864 SEPARATE_ZVAL(&object);
865 }
866 guard->in_unset = 1;
867 zend_std_call_unsetter(object, member TSRMLS_CC);
868 guard->in_unset = 0;
869 zval_ptr_dtor(&object);
870 } else if (zobj->ce->__unset && guard && guard->in_unset == 1) {
871 if (Z_STRVAL_P(member)[0] == '\0') {
872 if (Z_STRLEN_P(member) == 0) {
873 zend_error(E_ERROR, "Cannot access empty property");
874 } else {
875 zend_error(E_ERROR, "Cannot access property started with '\\0'");
876 }
877 }
878 }
879 } else if (EXPECTED(property_info != NULL) &&
880 EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
881 property_info->offset >= 0) {
882 zobj->properties_table[property_info->offset] = NULL;
883 }
884
885 if (UNEXPECTED(tmp_member != NULL)) {
886 zval_ptr_dtor(&tmp_member);
887 }
888 }
889
890
891 static void zend_std_unset_dimension(zval *object, zval *offset TSRMLS_DC)
892 {
893 zend_class_entry *ce = Z_OBJCE_P(object);
894
895 if (instanceof_function_ex(ce, zend_ce_arrayaccess, 1 TSRMLS_CC)) {
896 SEPARATE_ARG_IF_REF(offset);
897 zend_call_method_with_1_params(&object, ce, NULL, "offsetunset", NULL, offset);
898 zval_ptr_dtor(&offset);
899 } else {
900 zend_error_noreturn(E_ERROR, "Cannot use object of type %s as array", ce->name);
901 }
902 }
903
904
905 ZEND_API void zend_std_call_user_call(INTERNAL_FUNCTION_PARAMETERS)
906 {
907 zend_internal_function *func = (zend_internal_function *)EG(current_execute_data)->function_state.function;
908 zval *method_name_ptr, *method_args_ptr;
909 zval *method_result_ptr = NULL;
910 zend_class_entry *ce = Z_OBJCE_P(this_ptr);
911
912 ALLOC_ZVAL(method_args_ptr);
913 INIT_PZVAL(method_args_ptr);
914 array_init_size(method_args_ptr, ZEND_NUM_ARGS());
915
916 if (UNEXPECTED(zend_copy_parameters_array(ZEND_NUM_ARGS(), method_args_ptr TSRMLS_CC) == FAILURE)) {
917 zval_dtor(method_args_ptr);
918 zend_error_noreturn(E_ERROR, "Cannot get arguments for __call");
919 RETURN_FALSE;
920 }
921
922 ALLOC_ZVAL(method_name_ptr);
923 INIT_PZVAL(method_name_ptr);
924 ZVAL_STRING(method_name_ptr, func->function_name, 0);
925
926
927
928
929
930
931 zend_call_method_with_2_params(&this_ptr, ce, &ce->__call, ZEND_CALL_FUNC_NAME, &method_result_ptr, method_name_ptr, method_args_ptr);
932
933 if (method_result_ptr) {
934 RETVAL_ZVAL_FAST(method_result_ptr);
935 zval_ptr_dtor(&method_result_ptr);
936 }
937
938
939 zval_ptr_dtor(&method_args_ptr);
940 zval_ptr_dtor(&method_name_ptr);
941
942
943 efree(func);
944 }
945
946
947
948
949
950
951 static inline zend_function *zend_check_private_int(zend_function *fbc, zend_class_entry *ce, char *function_name_strval, int function_name_strlen, ulong hash_value TSRMLS_DC)
952 {
953 if (!ce) {
954 return 0;
955 }
956
957
958
959
960
961
962
963 if (fbc->common.scope == ce && EG(scope) == ce) {
964
965 return fbc;
966 }
967
968
969
970 ce = ce->parent;
971 while (ce) {
972 if (ce == EG(scope)) {
973 if (zend_hash_quick_find(&ce->function_table, function_name_strval, function_name_strlen+1, hash_value, (void **) &fbc)==SUCCESS
974 && fbc->op_array.fn_flags & ZEND_ACC_PRIVATE
975 && fbc->common.scope == EG(scope)) {
976 return fbc;
977 }
978 break;
979 }
980 ce = ce->parent;
981 }
982 return NULL;
983 }
984
985
986 ZEND_API int zend_check_private(zend_function *fbc, zend_class_entry *ce, char *function_name_strval, int function_name_strlen TSRMLS_DC)
987 {
988 return zend_check_private_int(fbc, ce, function_name_strval, function_name_strlen, zend_hash_func(function_name_strval, function_name_strlen+1) TSRMLS_CC) != NULL;
989 }
990
991
992
993
994 ZEND_API int zend_check_protected(zend_class_entry *ce, zend_class_entry *scope)
995 {
996 zend_class_entry *fbc_scope = ce;
997
998
999
1000
1001 while (fbc_scope) {
1002 if (fbc_scope==scope) {
1003 return 1;
1004 }
1005 fbc_scope = fbc_scope->parent;
1006 }
1007
1008
1009
1010
1011 while (scope) {
1012 if (scope==ce) {
1013 return 1;
1014 }
1015 scope = scope->parent;
1016 }
1017 return 0;
1018 }
1019
1020
1021 static inline union _zend_function *zend_get_user_call_function(zend_class_entry *ce, const char *method_name, int method_len)
1022 {
1023 zend_internal_function *call_user_call = emalloc(sizeof(zend_internal_function));
1024 call_user_call->type = ZEND_INTERNAL_FUNCTION;
1025 call_user_call->module = (ce->type == ZEND_INTERNAL_CLASS) ? ce->info.internal.module : NULL;
1026 call_user_call->handler = zend_std_call_user_call;
1027 call_user_call->arg_info = NULL;
1028 call_user_call->num_args = 0;
1029 call_user_call->scope = ce;
1030 call_user_call->fn_flags = ZEND_ACC_CALL_VIA_HANDLER;
1031 call_user_call->function_name = estrndup(method_name, method_len);
1032
1033 return (union _zend_function *)call_user_call;
1034 }
1035
1036
1037 static union _zend_function *zend_std_get_method(zval **object_ptr, char *method_name, int method_len, const zend_literal *key TSRMLS_DC)
1038 {
1039 zend_function *fbc;
1040 zval *object = *object_ptr;
1041 zend_object *zobj = Z_OBJ_P(object);
1042 ulong hash_value;
1043 char *lc_method_name;
1044 ALLOCA_FLAG(use_heap)
1045
1046 if (EXPECTED(key != NULL)) {
1047 lc_method_name = Z_STRVAL(key->constant);
1048 hash_value = key->hash_value;
1049 } else {
1050 lc_method_name = do_alloca(method_len+1, use_heap);
1051
1052 zend_str_tolower_copy(lc_method_name, method_name, method_len);
1053 hash_value = zend_hash_func(lc_method_name, method_len+1);
1054 }
1055
1056 if (UNEXPECTED(zend_hash_quick_find(&zobj->ce->function_table, lc_method_name, method_len+1, hash_value, (void **)&fbc) == FAILURE)) {
1057 if (UNEXPECTED(!key)) {
1058 free_alloca(lc_method_name, use_heap);
1059 }
1060 if (zobj->ce->__call) {
1061 return zend_get_user_call_function(zobj->ce, method_name, method_len);
1062 } else {
1063 return NULL;
1064 }
1065 }
1066
1067
1068 if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
1069 zend_function *updated_fbc;
1070
1071
1072
1073
1074 updated_fbc = zend_check_private_int(fbc, Z_OBJ_HANDLER_P(object, get_class_entry)(object TSRMLS_CC), lc_method_name, method_len, hash_value TSRMLS_CC);
1075 if (EXPECTED(updated_fbc != NULL)) {
1076 fbc = updated_fbc;
1077 } else {
1078 if (zobj->ce->__call) {
1079 fbc = zend_get_user_call_function(zobj->ce, method_name, method_len);
1080 } else {
1081 zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : "");
1082 }
1083 }
1084 } else {
1085
1086
1087
1088 if (EG(scope) &&
1089 is_derived_class(fbc->common.scope, EG(scope)) &&
1090 fbc->op_array.fn_flags & ZEND_ACC_CHANGED) {
1091 zend_function *priv_fbc;
1092
1093 if (zend_hash_quick_find(&EG(scope)->function_table, lc_method_name, method_len+1, hash_value, (void **) &priv_fbc)==SUCCESS
1094 && priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE
1095 && priv_fbc->common.scope == EG(scope)) {
1096 fbc = priv_fbc;
1097 }
1098 }
1099 if ((fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
1100
1101
1102
1103 if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), EG(scope)))) {
1104 if (zobj->ce->__call) {
1105 fbc = zend_get_user_call_function(zobj->ce, method_name, method_len);
1106 } else {
1107 zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), method_name, EG(scope) ? EG(scope)->name : "");
1108 }
1109 }
1110 }
1111 }
1112
1113 if (UNEXPECTED(!key)) {
1114 free_alloca(lc_method_name, use_heap);
1115 }
1116 return fbc;
1117 }
1118
1119
1120 ZEND_API void zend_std_callstatic_user_call(INTERNAL_FUNCTION_PARAMETERS)
1121 {
1122 zend_internal_function *func = (zend_internal_function *)EG(current_execute_data)->function_state.function;
1123 zval *method_name_ptr, *method_args_ptr;
1124 zval *method_result_ptr = NULL;
1125 zend_class_entry *ce = EG(scope);
1126
1127 ALLOC_ZVAL(method_args_ptr);
1128 INIT_PZVAL(method_args_ptr);
1129 array_init_size(method_args_ptr, ZEND_NUM_ARGS());
1130
1131 if (UNEXPECTED(zend_copy_parameters_array(ZEND_NUM_ARGS(), method_args_ptr TSRMLS_CC) == FAILURE)) {
1132 zval_dtor(method_args_ptr);
1133 zend_error_noreturn(E_ERROR, "Cannot get arguments for " ZEND_CALLSTATIC_FUNC_NAME);
1134 RETURN_FALSE;
1135 }
1136
1137 ALLOC_ZVAL(method_name_ptr);
1138 INIT_PZVAL(method_name_ptr);
1139 ZVAL_STRING(method_name_ptr, func->function_name, 0);
1140
1141
1142
1143
1144
1145 zend_call_method_with_2_params(NULL, ce, &ce->__callstatic, ZEND_CALLSTATIC_FUNC_NAME, &method_result_ptr, method_name_ptr, method_args_ptr);
1146
1147 if (method_result_ptr) {
1148 RETVAL_ZVAL_FAST(method_result_ptr);
1149 zval_ptr_dtor(&method_result_ptr);
1150 }
1151
1152
1153 zval_ptr_dtor(&method_args_ptr);
1154 zval_ptr_dtor(&method_name_ptr);
1155
1156
1157 efree(func);
1158 }
1159
1160
1161 static inline union _zend_function *zend_get_user_callstatic_function(zend_class_entry *ce, const char *method_name, int method_len)
1162 {
1163 zend_internal_function *callstatic_user_call = emalloc(sizeof(zend_internal_function));
1164 callstatic_user_call->type = ZEND_INTERNAL_FUNCTION;
1165 callstatic_user_call->module = (ce->type == ZEND_INTERNAL_CLASS) ? ce->info.internal.module : NULL;
1166 callstatic_user_call->handler = zend_std_callstatic_user_call;
1167 callstatic_user_call->arg_info = NULL;
1168 callstatic_user_call->num_args = 0;
1169 callstatic_user_call->scope = ce;
1170 callstatic_user_call->fn_flags = ZEND_ACC_STATIC | ZEND_ACC_PUBLIC | ZEND_ACC_CALL_VIA_HANDLER;
1171 callstatic_user_call->function_name = estrndup(method_name, method_len);
1172
1173 return (zend_function *)callstatic_user_call;
1174 }
1175
1176
1177
1178
1179 ZEND_API zend_function *zend_std_get_static_method(zend_class_entry *ce, const char *function_name_strval, int function_name_strlen, const zend_literal *key TSRMLS_DC)
1180 {
1181 zend_function *fbc = NULL;
1182 char *lc_class_name, *lc_function_name = NULL;
1183 ulong hash_value;
1184 ALLOCA_FLAG(use_heap)
1185
1186 if (EXPECTED(key != NULL)) {
1187 lc_function_name = Z_STRVAL(key->constant);
1188 hash_value = key->hash_value;
1189 } else {
1190 lc_function_name = do_alloca(function_name_strlen+1, use_heap);
1191
1192 zend_str_tolower_copy(lc_function_name, function_name_strval, function_name_strlen);
1193 hash_value = zend_hash_func(lc_function_name, function_name_strlen+1);
1194 }
1195
1196 if (function_name_strlen == ce->name_length && ce->constructor) {
1197 lc_class_name = zend_str_tolower_dup(ce->name, ce->name_length);
1198
1199
1200
1201 if (!memcmp(lc_class_name, lc_function_name, function_name_strlen) && memcmp(ce->constructor->common.function_name, "__", sizeof("__") - 1)) {
1202 fbc = ce->constructor;
1203 }
1204 efree(lc_class_name);
1205 }
1206 if (EXPECTED(!fbc) &&
1207 UNEXPECTED(zend_hash_quick_find(&ce->function_table, lc_function_name, function_name_strlen+1, hash_value, (void **) &fbc)==FAILURE)) {
1208 if (UNEXPECTED(!key)) {
1209 free_alloca(lc_function_name, use_heap);
1210 }
1211
1212 if (ce->__call &&
1213 EG(This) &&
1214 Z_OBJ_HT_P(EG(This))->get_class_entry &&
1215 instanceof_function(Z_OBJCE_P(EG(This)), ce TSRMLS_CC)) {
1216 return zend_get_user_call_function(ce, function_name_strval, function_name_strlen);
1217 } else if (ce->__callstatic) {
1218 return zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
1219 } else {
1220 return NULL;
1221 }
1222 }
1223
1224 #if MBO_0
1225
1226
1227 if (UNEXPECTED(!(fbc->common.fn_flags & ZEND_ACC_STATIC))) {
1228 zend_error_noreturn(E_ERROR, "Cannot call non static method %s::%s() without object", ZEND_FN_SCOPE_NAME(fbc), fbc->common.function_name);
1229 }
1230 #endif
1231 if (fbc->op_array.fn_flags & ZEND_ACC_PUBLIC) {
1232
1233 } else if (fbc->op_array.fn_flags & ZEND_ACC_PRIVATE) {
1234 zend_function *updated_fbc;
1235
1236
1237
1238 updated_fbc = zend_check_private_int(fbc, EG(scope), lc_function_name, function_name_strlen, hash_value TSRMLS_CC);
1239 if (EXPECTED(updated_fbc != NULL)) {
1240 fbc = updated_fbc;
1241 } else {
1242 if (ce->__callstatic) {
1243 fbc = zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
1244 } else {
1245 zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), function_name_strval, EG(scope) ? EG(scope)->name : "");
1246 }
1247 }
1248 } else if ((fbc->common.fn_flags & ZEND_ACC_PROTECTED)) {
1249
1250
1251 if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(fbc), EG(scope)))) {
1252 if (ce->__callstatic) {
1253 fbc = zend_get_user_callstatic_function(ce, function_name_strval, function_name_strlen);
1254 } else {
1255 zend_error_noreturn(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(fbc->common.fn_flags), ZEND_FN_SCOPE_NAME(fbc), function_name_strval, EG(scope) ? EG(scope)->name : "");
1256 }
1257 }
1258 }
1259
1260 if (UNEXPECTED(!key)) {
1261 free_alloca(lc_function_name, use_heap);
1262 }
1263
1264 return fbc;
1265 }
1266
1267
1268 ZEND_API zval **zend_std_get_static_property(zend_class_entry *ce, const char *property_name, int property_name_len, zend_bool silent, const zend_literal *key TSRMLS_DC)
1269 {
1270 zend_property_info *property_info;
1271 ulong hash_value;
1272
1273 if (UNEXPECTED(!key) ||
1274 (property_info = CACHED_POLYMORPHIC_PTR(key->cache_slot, ce)) == NULL) {
1275 if (EXPECTED(key != NULL)) {
1276 hash_value = key->hash_value;
1277 } else {
1278 hash_value = zend_hash_func(property_name, property_name_len+1);
1279 }
1280
1281 if (UNEXPECTED(zend_hash_quick_find(&ce->properties_info, property_name, property_name_len+1, hash_value, (void **) &property_info)==FAILURE)) {
1282 if (!silent) {
1283 zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
1284 }
1285 return NULL;
1286 }
1287
1288 #if DEBUG_OBJECT_HANDLERS
1289 zend_printf("Access type for %s::%s is %s\n", ce->name, property_name, zend_visibility_string(property_info->flags));
1290 #endif
1291
1292 if (UNEXPECTED(!zend_verify_property_access(property_info, ce TSRMLS_CC))) {
1293 if (!silent) {
1294 zend_error_noreturn(E_ERROR, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ce->name, property_name);
1295 }
1296 return NULL;
1297 }
1298
1299 if (UNEXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0)) {
1300 if (!silent) {
1301 zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
1302 }
1303 return NULL;
1304 }
1305
1306 zend_update_class_constants(ce TSRMLS_CC);
1307
1308 if (EXPECTED(key != NULL)) {
1309 CACHE_POLYMORPHIC_PTR(key->cache_slot, ce, property_info);
1310 }
1311 }
1312
1313 if (UNEXPECTED(CE_STATIC_MEMBERS(ce) == NULL) ||
1314 UNEXPECTED(CE_STATIC_MEMBERS(ce)[property_info->offset] == NULL)) {
1315 if (!silent) {
1316 zend_error_noreturn(E_ERROR, "Access to undeclared static property: %s::$%s", ce->name, property_name);
1317 }
1318 return NULL;
1319 }
1320
1321 return &CE_STATIC_MEMBERS(ce)[property_info->offset];
1322 }
1323
1324
1325 ZEND_API zend_bool zend_std_unset_static_property(zend_class_entry *ce, const char *property_name, int property_name_len, const zend_literal *key TSRMLS_DC)
1326 {
1327 zend_error_noreturn(E_ERROR, "Attempt to unset static property %s::$%s", ce->name, property_name);
1328 return 0;
1329 }
1330
1331
1332 ZEND_API union _zend_function *zend_std_get_constructor(zval *object TSRMLS_DC)
1333 {
1334 zend_object *zobj = Z_OBJ_P(object);
1335 zend_function *constructor = zobj->ce->constructor;
1336
1337 if (constructor) {
1338 if (constructor->op_array.fn_flags & ZEND_ACC_PUBLIC) {
1339
1340 } else if (constructor->op_array.fn_flags & ZEND_ACC_PRIVATE) {
1341
1342
1343 if (UNEXPECTED(constructor->common.scope != EG(scope))) {
1344 if (EG(scope)) {
1345 zend_error_noreturn(E_ERROR, "Call to private %s::%s() from context '%s'", constructor->common.scope->name, constructor->common.function_name, EG(scope)->name);
1346 } else {
1347 zend_error_noreturn(E_ERROR, "Call to private %s::%s() from invalid context", constructor->common.scope->name, constructor->common.function_name);
1348 }
1349 }
1350 } else if ((constructor->common.fn_flags & ZEND_ACC_PROTECTED)) {
1351
1352
1353
1354
1355 if (UNEXPECTED(!zend_check_protected(zend_get_function_root_class(constructor), EG(scope)))) {
1356 if (EG(scope)) {
1357 zend_error_noreturn(E_ERROR, "Call to protected %s::%s() from context '%s'", constructor->common.scope->name, constructor->common.function_name, EG(scope)->name);
1358 } else {
1359 zend_error_noreturn(E_ERROR, "Call to protected %s::%s() from invalid context", constructor->common.scope->name, constructor->common.function_name);
1360 }
1361 }
1362 }
1363 }
1364
1365 return constructor;
1366 }
1367
1368
1369 int zend_compare_symbol_tables_i(HashTable *ht1, HashTable *ht2 TSRMLS_DC);
1370
1371 static int zend_std_compare_objects(zval *o1, zval *o2 TSRMLS_DC)
1372 {
1373 zend_object *zobj1, *zobj2;
1374
1375 zobj1 = Z_OBJ_P(o1);
1376 zobj2 = Z_OBJ_P(o2);
1377
1378 if (zobj1->ce != zobj2->ce) {
1379 return 1;
1380 }
1381 if (!zobj1->properties && !zobj2->properties) {
1382 int i;
1383
1384 Z_OBJ_PROTECT_RECURSION(o1);
1385 Z_OBJ_PROTECT_RECURSION(o2);
1386 for (i = 0; i < zobj1->ce->default_properties_count; i++) {
1387 if (zobj1->properties_table[i]) {
1388 if (zobj2->properties_table[i]) {
1389 zval result;
1390
1391 if (compare_function(&result, zobj1->properties_table[i], zobj2->properties_table[i] TSRMLS_CC)==FAILURE) {
1392 Z_OBJ_UNPROTECT_RECURSION(o1);
1393 Z_OBJ_UNPROTECT_RECURSION(o2);
1394 return 1;
1395 }
1396 if (Z_LVAL(result) != 0) {
1397 Z_OBJ_UNPROTECT_RECURSION(o1);
1398 Z_OBJ_UNPROTECT_RECURSION(o2);
1399 return Z_LVAL(result);
1400 }
1401 } else {
1402 Z_OBJ_UNPROTECT_RECURSION(o1);
1403 Z_OBJ_UNPROTECT_RECURSION(o2);
1404 return 1;
1405 }
1406 } else {
1407 if (zobj2->properties_table[i]) {
1408 Z_OBJ_UNPROTECT_RECURSION(o1);
1409 Z_OBJ_UNPROTECT_RECURSION(o2);
1410 return 1;
1411 }
1412 }
1413 }
1414 Z_OBJ_UNPROTECT_RECURSION(o1);
1415 Z_OBJ_UNPROTECT_RECURSION(o2);
1416 return 0;
1417 } else {
1418 if (!zobj1->properties) {
1419 rebuild_object_properties(zobj1);
1420 }
1421 if (!zobj2->properties) {
1422 rebuild_object_properties(zobj2);
1423 }
1424 return zend_compare_symbol_tables_i(zobj1->properties, zobj2->properties TSRMLS_CC);
1425 }
1426 }
1427
1428
1429 static int zend_std_has_property(zval *object, zval *member, int has_set_exists, const zend_literal *key TSRMLS_DC)
1430 {
1431 zend_object *zobj;
1432 int result;
1433 zval **value = NULL;
1434 zval *tmp_member = NULL;
1435 zend_property_info *property_info;
1436
1437 zobj = Z_OBJ_P(object);
1438
1439 if (UNEXPECTED(Z_TYPE_P(member) != IS_STRING)) {
1440 ALLOC_ZVAL(tmp_member);
1441 *tmp_member = *member;
1442 INIT_PZVAL(tmp_member);
1443 zval_copy_ctor(tmp_member);
1444 convert_to_string(tmp_member);
1445 member = tmp_member;
1446 key = NULL;
1447 }
1448
1449 #if DEBUG_OBJECT_HANDLERS
1450 fprintf(stderr, "Read object #%d property: %s\n", Z_OBJ_HANDLE_P(object), Z_STRVAL_P(member));
1451 #endif
1452
1453 property_info = zend_get_property_info_quick(zobj->ce, member, 1, key TSRMLS_CC);
1454
1455 if (UNEXPECTED(!property_info) ||
1456 ((EXPECTED((property_info->flags & ZEND_ACC_STATIC) == 0) &&
1457 property_info->offset >= 0) ?
1458 (zobj->properties ?
1459 ((value = (zval**)zobj->properties_table[property_info->offset]) == NULL) :
1460 (*(value = &zobj->properties_table[property_info->offset]) == NULL)) :
1461 (UNEXPECTED(!zobj->properties) ||
1462 UNEXPECTED(zend_hash_quick_find(zobj->properties, property_info->name, property_info->name_length+1, property_info->h, (void **) &value) == FAILURE)))) {
1463 zend_guard *guard;
1464
1465 result = 0;
1466 if ((has_set_exists != 2) &&
1467 zobj->ce->__isset &&
1468 zend_get_property_guard(zobj, property_info, member, &guard) == SUCCESS &&
1469 !guard->in_isset) {
1470 zval *rv;
1471
1472
1473 Z_ADDREF_P(object);
1474 if (PZVAL_IS_REF(object)) {
1475 SEPARATE_ZVAL(&object);
1476 }
1477 guard->in_isset = 1;
1478 rv = zend_std_call_issetter(object, member TSRMLS_CC);
1479 if (rv) {
1480 result = zend_is_true(rv);
1481 zval_ptr_dtor(&rv);
1482 if (has_set_exists && result) {
1483 if (EXPECTED(!EG(exception)) && zobj->ce->__get && !guard->in_get) {
1484 guard->in_get = 1;
1485 rv = zend_std_call_getter(object, member TSRMLS_CC);
1486 guard->in_get = 0;
1487 if (rv) {
1488 Z_ADDREF_P(rv);
1489 result = i_zend_is_true(rv);
1490 zval_ptr_dtor(&rv);
1491 } else {
1492 result = 0;
1493 }
1494 } else {
1495 result = 0;
1496 }
1497 }
1498 }
1499 guard->in_isset = 0;
1500 zval_ptr_dtor(&object);
1501 }
1502 } else {
1503 switch (has_set_exists) {
1504 case 0:
1505 result = (Z_TYPE_PP(value) != IS_NULL);
1506 break;
1507 default:
1508 result = zend_is_true(*value);
1509 break;
1510 case 2:
1511 result = 1;
1512 break;
1513 }
1514 }
1515
1516 if (UNEXPECTED(tmp_member != NULL)) {
1517 zval_ptr_dtor(&tmp_member);
1518 }
1519 return result;
1520 }
1521
1522
1523 zend_class_entry *zend_std_object_get_class(const zval *object TSRMLS_DC)
1524 {
1525 zend_object *zobj;
1526 zobj = Z_OBJ_P(object);
1527
1528 return zobj->ce;
1529 }
1530
1531
1532 int zend_std_object_get_class_name(const zval *object, const char **class_name, zend_uint *class_name_len, int parent TSRMLS_DC)
1533 {
1534 zend_object *zobj;
1535 zend_class_entry *ce;
1536 zobj = Z_OBJ_P(object);
1537
1538 if (parent) {
1539 if (!zobj->ce->parent) {
1540 return FAILURE;
1541 }
1542 ce = zobj->ce->parent;
1543 } else {
1544 ce = zobj->ce;
1545 }
1546
1547 *class_name_len = ce->name_length;
1548 *class_name = estrndup(ce->name, ce->name_length);
1549 return SUCCESS;
1550 }
1551
1552
1553 ZEND_API int zend_std_cast_object_tostring(zval *readobj, zval *writeobj, int type TSRMLS_DC)
1554 {
1555 zval *retval;
1556 zend_class_entry *ce;
1557
1558 switch (type) {
1559 case IS_STRING:
1560 ce = Z_OBJCE_P(readobj);
1561 if (ce->__tostring &&
1562 (zend_call_method_with_0_params(&readobj, ce, &ce->__tostring, "__tostring", &retval) || EG(exception))) {
1563 if (UNEXPECTED(EG(exception) != NULL)) {
1564 if (retval) {
1565 zval_ptr_dtor(&retval);
1566 }
1567 EG(exception) = NULL;
1568 zend_error_noreturn(E_ERROR, "Method %s::__toString() must not throw an exception", ce->name);
1569 return FAILURE;
1570 }
1571 if (EXPECTED(Z_TYPE_P(retval) == IS_STRING)) {
1572 INIT_PZVAL(writeobj);
1573 if (readobj == writeobj) {
1574 zval_dtor(readobj);
1575 }
1576 ZVAL_ZVAL(writeobj, retval, 1, 1);
1577 if (Z_TYPE_P(writeobj) != type) {
1578 convert_to_explicit_type(writeobj, type);
1579 }
1580 return SUCCESS;
1581 } else {
1582 zval_ptr_dtor(&retval);
1583 INIT_PZVAL(writeobj);
1584 if (readobj == writeobj) {
1585 zval_dtor(readobj);
1586 }
1587 ZVAL_EMPTY_STRING(writeobj);
1588 zend_error(E_RECOVERABLE_ERROR, "Method %s::__toString() must return a string value", ce->name);
1589 return SUCCESS;
1590 }
1591 }
1592 return FAILURE;
1593 case IS_BOOL:
1594 INIT_PZVAL(writeobj);
1595 ZVAL_BOOL(writeobj, 1);
1596 return SUCCESS;
1597 case IS_LONG:
1598 ce = Z_OBJCE_P(readobj);
1599 zend_error(E_NOTICE, "Object of class %s could not be converted to int", ce->name);
1600 INIT_PZVAL(writeobj);
1601 if (readobj == writeobj) {
1602 zval_dtor(readobj);
1603 }
1604 ZVAL_LONG(writeobj, 1);
1605 return SUCCESS;
1606 case IS_DOUBLE:
1607 ce = Z_OBJCE_P(readobj);
1608 zend_error(E_NOTICE, "Object of class %s could not be converted to double", ce->name);
1609 INIT_PZVAL(writeobj);
1610 if (readobj == writeobj) {
1611 zval_dtor(readobj);
1612 }
1613 ZVAL_DOUBLE(writeobj, 1);
1614 return SUCCESS;
1615 default:
1616 INIT_PZVAL(writeobj);
1617 Z_TYPE_P(writeobj) = IS_NULL;
1618 break;
1619 }
1620 return FAILURE;
1621 }
1622
1623
1624 int zend_std_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr TSRMLS_DC)
1625 {
1626 zend_class_entry *ce;
1627 if (Z_TYPE_P(obj) != IS_OBJECT) {
1628 return FAILURE;
1629 }
1630
1631 ce = Z_OBJCE_P(obj);
1632
1633 if (zend_hash_find(&ce->function_table, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME), (void**)fptr_ptr) == FAILURE) {
1634 return FAILURE;
1635 }
1636
1637 *ce_ptr = ce;
1638 if ((*fptr_ptr)->common.fn_flags & ZEND_ACC_STATIC) {
1639 if (zobj_ptr) {
1640 *zobj_ptr = NULL;
1641 }
1642 } else {
1643 if (zobj_ptr) {
1644 *zobj_ptr = obj;
1645 }
1646 }
1647 return SUCCESS;
1648 }
1649
1650
1651 ZEND_API zend_object_handlers std_object_handlers = {
1652 zend_objects_store_add_ref,
1653 zend_objects_store_del_ref,
1654 zend_objects_clone_obj,
1655
1656 zend_std_read_property,
1657 zend_std_write_property,
1658 zend_std_read_dimension,
1659 zend_std_write_dimension,
1660 zend_std_get_property_ptr_ptr,
1661 NULL,
1662 NULL,
1663 zend_std_has_property,
1664 zend_std_unset_property,
1665 zend_std_has_dimension,
1666 zend_std_unset_dimension,
1667 zend_std_get_properties,
1668 zend_std_get_method,
1669 NULL,
1670 zend_std_get_constructor,
1671 zend_std_object_get_class,
1672 zend_std_object_get_class_name,
1673 zend_std_compare_objects,
1674 zend_std_cast_object_tostring,
1675 NULL,
1676 zend_std_get_debug_info,
1677 zend_std_get_closure,
1678 zend_std_get_gc,
1679 NULL,
1680 NULL,
1681 };
1682
1683
1684
1685
1686
1687
1688
1689