Coverage Report

Created: 2025-11-16 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/spl/php_spl.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | https://www.php.net/license/3_01.txt                                 |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Authors: Marcus Boerger <helly@php.net>                              |
14
   +----------------------------------------------------------------------+
15
 */
16
17
#ifdef HAVE_CONFIG_H
18
#include <config.h>
19
#endif
20
21
#include "php.h"
22
#include "php_main.h"
23
#include "ext/standard/info.h"
24
#include "php_spl.h"
25
#include "php_spl_arginfo.h"
26
#include "spl_functions.h"
27
#include "spl_array.h"
28
#include "spl_directory.h"
29
#include "spl_iterators.h"
30
#include "spl_exceptions.h"
31
#include "spl_observer.h"
32
#include "spl_dllist.h"
33
#include "spl_fixedarray.h"
34
#include "spl_heap.h"
35
#include "zend_exceptions.h"
36
#include "zend_interfaces.h"
37
#include "main/snprintf.h"
38
39
ZEND_TLS zend_string *spl_autoload_extensions;
40
ZEND_TLS HashTable *spl_autoload_functions;
41
42
0
#define SPL_DEFAULT_FILE_EXTENSIONS ".inc,.php"
43
44
static zend_class_entry * spl_find_ce_by_name(zend_string *name, bool autoload)
45
0
{
46
0
  zend_class_entry *ce;
47
48
0
  if (!autoload) {
49
0
    zend_string *lc_name = zend_string_tolower(name);
50
51
0
    ce = zend_hash_find_ptr(EG(class_table), lc_name);
52
0
    zend_string_release(lc_name);
53
0
  } else {
54
0
    ce = zend_lookup_class(name);
55
0
  }
56
0
  if (ce == NULL) {
57
0
    php_error_docref(NULL, E_WARNING, "Class %s does not exist%s", ZSTR_VAL(name), autoload ? " and could not be loaded" : "");
58
0
    return NULL;
59
0
  }
60
61
0
  return ce;
62
0
}
63
64
/* {{{ Return an array containing the names of all parent classes */
65
PHP_FUNCTION(class_parents)
66
0
{
67
0
  zval *obj;
68
0
  zend_class_entry *parent_class, *ce;
69
0
  bool autoload = true;
70
71
  /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */
72
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
73
0
    RETURN_THROWS();
74
0
  }
75
76
0
  if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
77
0
    zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(obj));
78
0
    RETURN_THROWS();
79
0
  }
80
81
0
  if (Z_TYPE_P(obj) == IS_STRING) {
82
0
    if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
83
0
      RETURN_FALSE;
84
0
    }
85
0
  } else {
86
0
    ce = Z_OBJCE_P(obj);
87
0
  }
88
89
0
  array_init(return_value);
90
0
  parent_class = ce->parent;
91
0
  while (parent_class) {
92
0
    spl_add_class_name(return_value, parent_class, 0, 0);
93
0
    parent_class = parent_class->parent;
94
0
  }
95
0
}
96
/* }}} */
97
98
/* {{{ Return all classes and interfaces implemented by SPL */
99
PHP_FUNCTION(class_implements)
100
0
{
101
0
  zval *obj;
102
0
  bool autoload = true;
103
0
  zend_class_entry *ce;
104
105
  /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */
106
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
107
0
    RETURN_THROWS();
108
0
  }
109
0
  if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
110
0
    zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(obj));
111
0
    RETURN_THROWS();
112
0
  }
113
114
0
  if (Z_TYPE_P(obj) == IS_STRING) {
115
0
    if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
116
0
      RETURN_FALSE;
117
0
    }
118
0
  } else {
119
0
    ce = Z_OBJCE_P(obj);
120
0
  }
121
122
0
  array_init(return_value);
123
0
  spl_add_interfaces(return_value, ce, 1, ZEND_ACC_INTERFACE);
124
0
}
125
/* }}} */
126
127
/* {{{ Return all traits used by a class. */
128
PHP_FUNCTION(class_uses)
129
0
{
130
0
  zval *obj;
131
0
  bool autoload = true;
132
0
  zend_class_entry *ce;
133
134
  /* We do not use Z_PARAM_OBJ_OR_STR here to be able to exclude int, float, and bool which are bogus class names */
135
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "z|b", &obj, &autoload) == FAILURE) {
136
0
    RETURN_THROWS();
137
0
  }
138
0
  if (Z_TYPE_P(obj) != IS_OBJECT && Z_TYPE_P(obj) != IS_STRING) {
139
0
    zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_value_name(obj));
140
0
    RETURN_THROWS();
141
0
  }
142
143
0
  if (Z_TYPE_P(obj) == IS_STRING) {
144
0
    if (NULL == (ce = spl_find_ce_by_name(Z_STR_P(obj), autoload))) {
145
0
      RETURN_FALSE;
146
0
    }
147
0
  } else {
148
0
    ce = Z_OBJCE_P(obj);
149
0
  }
150
151
0
  array_init(return_value);
152
0
  spl_add_traits(return_value, ce, 1, ZEND_ACC_TRAIT);
153
0
}
154
/* }}} */
155
156
#define SPL_ADD_CLASS(class_name, z_list, sub, allow, ce_flags) \
157
880
  spl_add_classes(spl_ce_ ## class_name, z_list, sub, allow, ce_flags)
158
159
#define SPL_LIST_CLASSES(z_list, sub, allow, ce_flags) \
160
16
  SPL_ADD_CLASS(AppendIterator, z_list, sub, allow, ce_flags); \
161
16
  SPL_ADD_CLASS(ArrayIterator, z_list, sub, allow, ce_flags); \
162
16
  SPL_ADD_CLASS(ArrayObject, z_list, sub, allow, ce_flags); \
163
16
  SPL_ADD_CLASS(BadFunctionCallException, z_list, sub, allow, ce_flags); \
164
16
  SPL_ADD_CLASS(BadMethodCallException, z_list, sub, allow, ce_flags); \
165
16
  SPL_ADD_CLASS(CachingIterator, z_list, sub, allow, ce_flags); \
166
16
  SPL_ADD_CLASS(CallbackFilterIterator, z_list, sub, allow, ce_flags); \
167
16
  SPL_ADD_CLASS(DirectoryIterator, z_list, sub, allow, ce_flags); \
168
16
  SPL_ADD_CLASS(DomainException, z_list, sub, allow, ce_flags); \
169
16
  SPL_ADD_CLASS(EmptyIterator, z_list, sub, allow, ce_flags); \
170
16
  SPL_ADD_CLASS(FilesystemIterator, z_list, sub, allow, ce_flags); \
171
16
  SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \
172
16
  SPL_ADD_CLASS(GlobIterator, z_list, sub, allow, ce_flags); \
173
16
  SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \
174
16
  SPL_ADD_CLASS(InvalidArgumentException, z_list, sub, allow, ce_flags); \
175
16
  SPL_ADD_CLASS(IteratorIterator, z_list, sub, allow, ce_flags); \
176
16
  SPL_ADD_CLASS(LengthException, z_list, sub, allow, ce_flags); \
177
16
  SPL_ADD_CLASS(LimitIterator, z_list, sub, allow, ce_flags); \
178
16
  SPL_ADD_CLASS(LogicException, z_list, sub, allow, ce_flags); \
179
16
  SPL_ADD_CLASS(MultipleIterator, z_list, sub, allow, ce_flags); \
180
16
  SPL_ADD_CLASS(NoRewindIterator, z_list, sub, allow, ce_flags); \
181
16
  SPL_ADD_CLASS(OuterIterator, z_list, sub, allow, ce_flags); \
182
16
  SPL_ADD_CLASS(OutOfBoundsException, z_list, sub, allow, ce_flags); \
183
16
  SPL_ADD_CLASS(OutOfRangeException, z_list, sub, allow, ce_flags); \
184
16
  SPL_ADD_CLASS(OverflowException, z_list, sub, allow, ce_flags); \
185
16
  SPL_ADD_CLASS(ParentIterator, z_list, sub, allow, ce_flags); \
186
16
  SPL_ADD_CLASS(RangeException, z_list, sub, allow, ce_flags); \
187
16
  SPL_ADD_CLASS(RecursiveArrayIterator, z_list, sub, allow, ce_flags); \
188
16
  SPL_ADD_CLASS(RecursiveCachingIterator, z_list, sub, allow, ce_flags); \
189
16
  SPL_ADD_CLASS(RecursiveCallbackFilterIterator, z_list, sub, allow, ce_flags); \
190
16
  SPL_ADD_CLASS(RecursiveDirectoryIterator, z_list, sub, allow, ce_flags); \
191
16
  SPL_ADD_CLASS(RecursiveFilterIterator, z_list, sub, allow, ce_flags); \
192
16
  SPL_ADD_CLASS(RecursiveIterator, z_list, sub, allow, ce_flags); \
193
16
  SPL_ADD_CLASS(RecursiveIteratorIterator, z_list, sub, allow, ce_flags); \
194
16
  SPL_ADD_CLASS(RecursiveRegexIterator, z_list, sub, allow, ce_flags); \
195
16
  SPL_ADD_CLASS(RecursiveTreeIterator, z_list, sub, allow, ce_flags); \
196
16
  SPL_ADD_CLASS(RegexIterator, z_list, sub, allow, ce_flags); \
197
16
  SPL_ADD_CLASS(RuntimeException, z_list, sub, allow, ce_flags); \
198
16
  SPL_ADD_CLASS(SeekableIterator, z_list, sub, allow, ce_flags); \
199
16
  SPL_ADD_CLASS(SplDoublyLinkedList, z_list, sub, allow, ce_flags); \
200
16
  SPL_ADD_CLASS(SplFileInfo, z_list, sub, allow, ce_flags); \
201
16
  SPL_ADD_CLASS(SplFileObject, z_list, sub, allow, ce_flags); \
202
16
  SPL_ADD_CLASS(SplFixedArray, z_list, sub, allow, ce_flags); \
203
16
  SPL_ADD_CLASS(SplHeap, z_list, sub, allow, ce_flags); \
204
16
  SPL_ADD_CLASS(SplMinHeap, z_list, sub, allow, ce_flags); \
205
16
  SPL_ADD_CLASS(SplMaxHeap, z_list, sub, allow, ce_flags); \
206
16
  SPL_ADD_CLASS(SplObjectStorage, z_list, sub, allow, ce_flags); \
207
16
  SPL_ADD_CLASS(SplObserver, z_list, sub, allow, ce_flags); \
208
16
  SPL_ADD_CLASS(SplPriorityQueue, z_list, sub, allow, ce_flags); \
209
16
  SPL_ADD_CLASS(SplQueue, z_list, sub, allow, ce_flags); \
210
16
  SPL_ADD_CLASS(SplStack, z_list, sub, allow, ce_flags); \
211
16
  SPL_ADD_CLASS(SplSubject, z_list, sub, allow, ce_flags); \
212
16
  SPL_ADD_CLASS(SplTempFileObject, z_list, sub, allow, ce_flags); \
213
16
  SPL_ADD_CLASS(UnderflowException, z_list, sub, allow, ce_flags); \
214
16
  SPL_ADD_CLASS(UnexpectedValueException, z_list, sub, allow, ce_flags); \
215
216
/* {{{ Return an array containing the names of all classes and interfaces defined in SPL */
217
PHP_FUNCTION(spl_classes)
218
0
{
219
0
  ZEND_PARSE_PARAMETERS_NONE();
220
221
0
  array_init(return_value);
222
223
0
  SPL_LIST_CLASSES(return_value, 0, 0, 0)
224
0
}
225
/* }}} */
226
227
static int spl_autoload(zend_string *class_name, zend_string *lc_name, const char *ext, int ext_len) /* {{{ */
228
0
{
229
0
  zend_string *class_file;
230
0
  zval dummy;
231
0
  zend_file_handle file_handle;
232
0
  zend_op_array *new_op_array;
233
0
  zval result;
234
0
  int ret;
235
236
0
  class_file = zend_strpprintf(0, "%s%.*s", ZSTR_VAL(lc_name), ext_len, ext);
237
238
0
#if DEFAULT_SLASH != '\\'
239
0
  {
240
0
    char *ptr = ZSTR_VAL(class_file);
241
0
    char *end = ptr + ZSTR_LEN(class_file);
242
243
0
    while ((ptr = memchr(ptr, '\\', (end - ptr))) != NULL) {
244
0
      *ptr = DEFAULT_SLASH;
245
0
    }
246
0
  }
247
0
#endif
248
249
0
  zend_stream_init_filename_ex(&file_handle, class_file);
250
0
  ret = php_stream_open_for_zend_ex(&file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE);
251
252
0
  if (ret == SUCCESS) {
253
0
    zend_string *opened_path;
254
0
    if (!file_handle.opened_path) {
255
0
      file_handle.opened_path = zend_string_copy(class_file);
256
0
    }
257
0
    opened_path = zend_string_copy(file_handle.opened_path);
258
0
    ZVAL_NULL(&dummy);
259
0
    if (zend_hash_add(&EG(included_files), opened_path, &dummy)) {
260
0
      new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
261
0
    } else {
262
0
      new_op_array = NULL;
263
0
    }
264
0
    zend_string_release_ex(opened_path, 0);
265
0
    if (new_op_array) {
266
0
      uint32_t orig_jit_trace_num = EG(jit_trace_num);
267
268
0
      ZVAL_UNDEF(&result);
269
0
      zend_execute(new_op_array, &result);
270
0
      EG(jit_trace_num) = orig_jit_trace_num;
271
272
0
      destroy_op_array(new_op_array);
273
0
      efree(new_op_array);
274
0
      if (!EG(exception)) {
275
0
        zval_ptr_dtor(&result);
276
0
      }
277
278
0
      zend_destroy_file_handle(&file_handle);
279
0
      zend_string_release(class_file);
280
0
      return zend_hash_exists(EG(class_table), lc_name);
281
0
    }
282
0
  }
283
0
  zend_destroy_file_handle(&file_handle);
284
0
  zend_string_release(class_file);
285
0
  return 0;
286
0
} /* }}} */
287
288
/* {{{ Default autoloader implementation */
289
PHP_FUNCTION(spl_autoload)
290
0
{
291
0
  int pos_len, pos1_len;
292
0
  char *pos, *pos1;
293
0
  zend_string *class_name, *lc_name, *file_exts = NULL;
294
295
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &class_name, &file_exts) == FAILURE) {
296
0
    RETURN_THROWS();
297
0
  }
298
299
0
  if (!file_exts) {
300
0
    file_exts = spl_autoload_extensions;
301
0
  }
302
303
0
  if (file_exts == NULL) { /* autoload_extensions is not initialized, set to defaults */
304
0
    pos = SPL_DEFAULT_FILE_EXTENSIONS;
305
0
    pos_len = sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1;
306
0
  } else {
307
0
    pos = ZSTR_VAL(file_exts);
308
0
    pos_len = (int)ZSTR_LEN(file_exts);
309
0
  }
310
311
0
  lc_name = zend_string_tolower(class_name);
312
0
  while (pos && *pos && !EG(exception)) {
313
0
    pos1 = strchr(pos, ',');
314
0
    if (pos1) {
315
0
      pos1_len = (int)(pos1 - pos);
316
0
    } else {
317
0
      pos1_len = pos_len;
318
0
    }
319
0
    if (spl_autoload(class_name, lc_name, pos, pos1_len)) {
320
0
      break; /* loaded */
321
0
    }
322
0
    pos = pos1 ? pos1 + 1 : NULL;
323
0
    pos_len = pos1? pos_len - pos1_len - 1 : 0;
324
0
  }
325
0
  zend_string_release(lc_name);
326
0
} /* }}} */
327
328
/* {{{ Register and return default file extensions for spl_autoload */
329
PHP_FUNCTION(spl_autoload_extensions)
330
0
{
331
0
  zend_string *file_exts = NULL;
332
333
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &file_exts) == FAILURE) {
334
0
    RETURN_THROWS();
335
0
  }
336
337
0
  if (file_exts) {
338
0
    if (spl_autoload_extensions) {
339
0
      zend_string_release_ex(spl_autoload_extensions, 0);
340
0
    }
341
0
    spl_autoload_extensions = zend_string_copy(file_exts);
342
0
  }
343
344
0
  if (spl_autoload_extensions == NULL) {
345
0
    RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1);
346
0
  } else {
347
0
    zend_string_addref(spl_autoload_extensions);
348
0
    RETURN_STR(spl_autoload_extensions);
349
0
  }
350
0
} /* }}} */
351
352
typedef struct {
353
  zend_function *func_ptr;
354
  zend_object *obj;
355
  zend_object *closure;
356
  zend_class_entry *ce;
357
} autoload_func_info;
358
359
447
static void autoload_func_info_destroy(autoload_func_info *alfi) {
360
447
  if (alfi->obj) {
361
0
    zend_object_release(alfi->obj);
362
0
  }
363
447
  if (alfi->func_ptr &&
364
447
    UNEXPECTED(alfi->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
365
0
    zend_string_release_ex(alfi->func_ptr->common.function_name, 0);
366
0
    zend_free_trampoline(alfi->func_ptr);
367
0
  }
368
447
  if (alfi->closure) {
369
417
    zend_object_release(alfi->closure);
370
417
  }
371
447
  efree(alfi);
372
447
}
373
374
static void autoload_func_info_zval_dtor(zval *element)
375
447
{
376
447
  autoload_func_info_destroy(Z_PTR_P(element));
377
447
}
378
379
static autoload_func_info *autoload_func_info_from_fci(
380
447
    zend_fcall_info *fci, zend_fcall_info_cache *fcc) {
381
447
  autoload_func_info *alfi = emalloc(sizeof(autoload_func_info));
382
447
  alfi->ce = fcc->calling_scope;
383
447
  alfi->func_ptr = fcc->function_handler;
384
447
  alfi->obj = fcc->object;
385
447
  if (alfi->obj) {
386
0
    GC_ADDREF(alfi->obj);
387
0
  }
388
447
  if (Z_TYPE(fci->function_name) == IS_OBJECT) {
389
417
    alfi->closure = Z_OBJ(fci->function_name);
390
417
    GC_ADDREF(alfi->closure);
391
417
  } else {
392
30
    alfi->closure = NULL;
393
30
  }
394
447
  return alfi;
395
447
}
396
397
static bool autoload_func_info_equals(
398
13
    const autoload_func_info *alfi1, const autoload_func_info *alfi2) {
399
13
  if (UNEXPECTED(
400
13
    (alfi1->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) &&
401
13
    (alfi2->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)
402
13
  )) {
403
0
    return alfi1->obj == alfi2->obj
404
0
      && alfi1->ce == alfi2->ce
405
0
      && alfi1->closure == alfi2->closure
406
0
      && zend_string_equals(alfi1->func_ptr->common.function_name, alfi2->func_ptr->common.function_name)
407
0
    ;
408
0
  }
409
13
  return alfi1->func_ptr == alfi2->func_ptr
410
0
    && alfi1->obj == alfi2->obj
411
0
    && alfi1->ce == alfi2->ce
412
0
    && alfi1->closure == alfi2->closure;
413
13
}
414
415
136k
static zend_class_entry *spl_perform_autoload(zend_string *class_name, zend_string *lc_name) {
416
136k
  if (!spl_autoload_functions) {
417
135k
    return NULL;
418
135k
  }
419
420
  /* We don't use ZEND_HASH_MAP_FOREACH here,
421
   * because autoloaders may be added/removed during autoloading. */
422
965
  HashPosition pos;
423
965
  zend_hash_internal_pointer_reset_ex(spl_autoload_functions, &pos);
424
1.19k
  while (1) {
425
1.06k
    autoload_func_info *alfi =
426
1.06k
      zend_hash_get_current_data_ptr_ex(spl_autoload_functions, &pos);
427
1.06k
    if (!alfi) {
428
84
      break;
429
84
    }
430
431
978
    zend_function *func = alfi->func_ptr;
432
978
    if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
433
0
      func = emalloc(sizeof(zend_op_array));
434
0
      memcpy(func, alfi->func_ptr, sizeof(zend_op_array));
435
0
      zend_string_addref(func->op_array.function_name);
436
0
    }
437
438
978
    zval param;
439
978
    ZVAL_STR(&param, class_name);
440
978
    zend_call_known_function(func, alfi->obj, alfi->ce, NULL, 1, &param, NULL);
441
978
    if (EG(exception)) {
442
270
      break;
443
270
    }
444
445
708
    if (ZSTR_HAS_CE_CACHE(class_name) &&  ZSTR_GET_CE_CACHE(class_name)) {
446
248
      return (zend_class_entry*)ZSTR_GET_CE_CACHE(class_name);
447
460
    } else {
448
460
      zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), lc_name);
449
460
      if (ce) {
450
231
        return ce;
451
231
      }
452
460
    }
453
454
229
    zend_hash_move_forward_ex(spl_autoload_functions, &pos);
455
229
  }
456
486
  return NULL;
457
965
}
458
459
/* {{{ Try all registered autoload function to load the requested class */
460
PHP_FUNCTION(spl_autoload_call)
461
0
{
462
0
  zend_string *class_name;
463
464
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &class_name) == FAILURE) {
465
0
    RETURN_THROWS();
466
0
  }
467
468
0
  zend_string *lc_name = zend_string_tolower(class_name);
469
0
  spl_perform_autoload(class_name, lc_name);
470
0
  zend_string_release(lc_name);
471
0
} /* }}} */
472
473
#define HT_MOVE_TAIL_TO_HEAD(ht)                    \
474
13
  ZEND_ASSERT(!HT_IS_PACKED(ht));                    \
475
13
  do {                               \
476
13
    Bucket tmp = (ht)->arData[(ht)->nNumUsed-1];        \
477
13
    memmove((ht)->arData + 1, (ht)->arData,         \
478
13
      sizeof(Bucket) * ((ht)->nNumUsed - 1));       \
479
13
    (ht)->arData[0] = tmp;                  \
480
13
    zend_hash_rehash(ht);                     \
481
13
  } while (0)
482
483
447
static Bucket *spl_find_registered_function(autoload_func_info *find_alfi) {
484
447
  if (!spl_autoload_functions) {
485
0
    return NULL;
486
0
  }
487
488
447
  autoload_func_info *alfi;
489
920
  ZEND_HASH_MAP_FOREACH_PTR(spl_autoload_functions, alfi) {
490
920
    if (autoload_func_info_equals(alfi, find_alfi)) {
491
0
      return _p;
492
0
    }
493
920
  } ZEND_HASH_FOREACH_END();
494
447
  return NULL;
495
447
}
496
497
/* {{{ Register given function as autoloader */
498
PHP_FUNCTION(spl_autoload_register)
499
452
{
500
452
  bool do_throw = 1;
501
452
  bool prepend  = 0;
502
452
  zend_fcall_info fci = {0};
503
452
  zend_fcall_info_cache fcc;
504
452
  autoload_func_info *alfi;
505
506
1.35k
  ZEND_PARSE_PARAMETERS_START(0, 3)
507
1.35k
    Z_PARAM_OPTIONAL
508
1.80k
    Z_PARAM_FUNC_OR_NULL(fci, fcc)
509
1.36k
    Z_PARAM_BOOL(do_throw)
510
65
    Z_PARAM_BOOL(prepend)
511
452
  ZEND_PARSE_PARAMETERS_END();
512
513
447
  if (!do_throw) {
514
0
    php_error_docref(NULL, E_NOTICE, "Argument #2 ($do_throw) has been ignored, "
515
0
      "spl_autoload_register() will always throw");
516
0
  }
517
518
447
  if (!spl_autoload_functions) {
519
434
    ALLOC_HASHTABLE(spl_autoload_functions);
520
434
    zend_hash_init(spl_autoload_functions, 1, NULL, autoload_func_info_zval_dtor, 0);
521
    /* Initialize as non-packed hash table for prepend functionality. */
522
434
    zend_hash_real_init_mixed(spl_autoload_functions);
523
434
  }
524
525
  /* If first arg is not null */
526
447
  if (ZEND_FCI_INITIALIZED(fci)) {
527
447
    if (!fcc.function_handler) {
528
      /* Call trampoline has been cleared by zpp. Refetch it, because we want to deal
529
       * with it ourselves. It is important that it is not refetched on every call,
530
       * because calls may occur from different scopes. */
531
0
      zend_is_callable_ex(&fci.function_name, NULL, IS_CALLABLE_SUPPRESS_DEPRECATIONS, NULL, &fcc, NULL);
532
0
    }
533
534
447
    if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
535
0
      fcc.function_handler->internal_function.handler == zif_spl_autoload_call) {
536
0
      zend_argument_value_error(1, "must not be the spl_autoload_call() function");
537
0
      RETURN_THROWS();
538
0
    }
539
540
447
    alfi = autoload_func_info_from_fci(&fci, &fcc);
541
447
    if (UNEXPECTED(alfi->func_ptr == &EG(trampoline))) {
542
0
      zend_function *copy = emalloc(sizeof(zend_op_array));
543
544
0
      memcpy(copy, alfi->func_ptr, sizeof(zend_op_array));
545
0
      alfi->func_ptr->common.function_name = NULL;
546
0
      alfi->func_ptr = copy;
547
0
    }
548
447
  } else {
549
0
    alfi = emalloc(sizeof(autoload_func_info));
550
0
    alfi->func_ptr = zend_hash_str_find_ptr(
551
0
      CG(function_table), "spl_autoload", sizeof("spl_autoload") - 1);
552
0
    alfi->obj = NULL;
553
0
    alfi->ce = NULL;
554
0
    alfi->closure = NULL;
555
0
  }
556
557
447
  if (spl_find_registered_function(alfi)) {
558
0
    autoload_func_info_destroy(alfi);
559
0
    RETURN_TRUE;
560
0
  }
561
562
447
  zend_hash_next_index_insert_ptr(spl_autoload_functions, alfi);
563
447
  if (prepend && spl_autoload_functions->nNumOfElements > 1) {
564
    /* Move the newly created element to the head of the hashtable */
565
13
    HT_MOVE_TAIL_TO_HEAD(spl_autoload_functions);
566
13
  }
567
568
447
  RETURN_TRUE;
569
447
} /* }}} */
570
571
/* {{{ Unregister given function as autoloader */
572
PHP_FUNCTION(spl_autoload_unregister)
573
0
{
574
0
  zend_fcall_info fci;
575
0
  zend_fcall_info_cache fcc;
576
577
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
578
0
    Z_PARAM_FUNC(fci, fcc)
579
0
  ZEND_PARSE_PARAMETERS_END();
580
581
0
  if (fcc.function_handler && zend_string_equals_literal(
582
0
      fcc.function_handler->common.function_name, "spl_autoload_call")) {
583
0
    php_error_docref(NULL, E_DEPRECATED,
584
0
      "Using spl_autoload_call() as a callback for spl_autoload_unregister() is deprecated,"
585
0
      " to remove all registered autoloaders, call spl_autoload_unregister()"
586
0
      " for all values returned from spl_autoload_functions()");
587
0
    if (UNEXPECTED(EG(exception))) {
588
0
      RETURN_THROWS();
589
0
    }
590
0
    if (spl_autoload_functions) {
591
      /* Don't destroy the hash table, as we might be iterating over it right now. */
592
0
      zend_hash_clean(spl_autoload_functions);
593
0
    }
594
0
    RETURN_TRUE;
595
0
  }
596
597
0
  if (!fcc.function_handler) {
598
    /* Call trampoline has been cleared by zpp. Refetch it, because we want to deal
599
     * with it ourselves. It is important that it is not refetched on every call,
600
     * because calls may occur from different scopes. */
601
0
    zend_is_callable_ex(&fci.function_name, NULL, 0, NULL, &fcc, NULL);
602
0
  }
603
604
0
  autoload_func_info *alfi = autoload_func_info_from_fci(&fci, &fcc);
605
0
  Bucket *p = spl_find_registered_function(alfi);
606
0
  autoload_func_info_destroy(alfi);
607
0
  if (p) {
608
0
    zend_hash_del_bucket(spl_autoload_functions, p);
609
0
    RETURN_TRUE;
610
0
  }
611
612
0
  RETURN_FALSE;
613
0
} /* }}} */
614
615
/* {{{ Return all registered autoloader functions */
616
PHP_FUNCTION(spl_autoload_functions)
617
0
{
618
0
  autoload_func_info *alfi;
619
620
0
  ZEND_PARSE_PARAMETERS_NONE();
621
622
0
  array_init(return_value);
623
0
  if (spl_autoload_functions) {
624
0
    ZEND_HASH_MAP_FOREACH_PTR(spl_autoload_functions, alfi) {
625
0
      if (alfi->closure) {
626
0
        GC_ADDREF(alfi->closure);
627
0
        add_next_index_object(return_value, alfi->closure);
628
0
      } else if (alfi->func_ptr->common.scope) {
629
0
        zval tmp;
630
631
0
        array_init(&tmp);
632
0
        if (alfi->obj) {
633
0
          GC_ADDREF(alfi->obj);
634
0
          add_next_index_object(&tmp, alfi->obj);
635
0
        } else {
636
0
          add_next_index_str(&tmp, zend_string_copy(alfi->ce->name));
637
0
        }
638
0
        add_next_index_str(&tmp, zend_string_copy(alfi->func_ptr->common.function_name));
639
0
        add_next_index_zval(return_value, &tmp);
640
0
      } else {
641
0
        add_next_index_str(return_value, zend_string_copy(alfi->func_ptr->common.function_name));
642
0
      }
643
0
    } ZEND_HASH_FOREACH_END();
644
0
  }
645
0
} /* }}} */
646
647
/* {{{ Return hash id for given object */
648
PHP_FUNCTION(spl_object_hash)
649
252
{
650
252
  zend_object *obj;
651
652
756
  ZEND_PARSE_PARAMETERS_START(1, 1)
653
1.00k
    Z_PARAM_OBJ(obj)
654
252
  ZEND_PARSE_PARAMETERS_END();
655
656
250
  RETURN_NEW_STR(php_spl_object_hash(obj));
657
250
}
658
/* }}} */
659
660
/* {{{ Returns the integer object handle for the given object */
661
PHP_FUNCTION(spl_object_id)
662
359
{
663
359
  zend_object *obj;
664
665
1.07k
  ZEND_PARSE_PARAMETERS_START(1, 1)
666
1.43k
    Z_PARAM_OBJ(obj)
667
359
  ZEND_PARSE_PARAMETERS_END();
668
669
359
  RETURN_LONG((zend_long)obj->handle);
670
359
}
671
/* }}} */
672
673
PHPAPI zend_string *php_spl_object_hash(zend_object *obj) /* {{{*/
674
250
{
675
250
  return strpprintf(32, "%016zx0000000000000000", (intptr_t)obj->handle);
676
250
}
677
/* }}} */
678
679
static void spl_build_class_list_string(zval *entry, char **list) /* {{{ */
680
440
{
681
440
  char *res;
682
683
440
  spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_P(entry));
684
440
  efree(*list);
685
440
  *list = res;
686
440
} /* }}} */
687
688
/* {{{ PHP_MINFO(spl) */
689
PHP_MINFO_FUNCTION(spl)
690
8
{
691
8
  zval list, *zv;
692
8
  char *strg;
693
694
8
  php_info_print_table_start();
695
8
  php_info_print_table_row(2, "SPL support", "enabled");
696
697
8
  array_init(&list);
698
8
  SPL_LIST_CLASSES(&list, 0, 1, ZEND_ACC_INTERFACE)
699
8
  strg = estrdup("");
700
96
  ZEND_HASH_MAP_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
701
96
    spl_build_class_list_string(zv, &strg);
702
96
  } ZEND_HASH_FOREACH_END();
703
8
  zend_array_destroy(Z_ARR(list));
704
8
  php_info_print_table_row(2, "Interfaces", strg + 2);
705
8
  efree(strg);
706
707
8
  array_init(&list);
708
8
  SPL_LIST_CLASSES(&list, 0, -1, ZEND_ACC_INTERFACE)
709
8
  strg = estrdup("");
710
816
  ZEND_HASH_MAP_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
711
816
    spl_build_class_list_string(zv, &strg);
712
816
  } ZEND_HASH_FOREACH_END();
713
8
  zend_array_destroy(Z_ARR(list));
714
8
  php_info_print_table_row(2, "Classes", strg + 2);
715
8
  efree(strg);
716
717
8
  php_info_print_table_end();
718
8
}
719
/* }}} */
720
721
/* {{{ PHP_MINIT_FUNCTION(spl) */
722
PHP_MINIT_FUNCTION(spl)
723
16
{
724
16
  zend_autoload = spl_perform_autoload;
725
726
16
  PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
727
16
  PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU);
728
16
  PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
729
16
  PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
730
16
  PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU);
731
16
  PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU);
732
16
  PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU);
733
16
  PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU);
734
735
16
  return SUCCESS;
736
16
}
737
/* }}} */
738
739
PHP_RINIT_FUNCTION(spl) /* {{{ */
740
247k
{
741
247k
  spl_autoload_extensions = NULL;
742
247k
  spl_autoload_functions = NULL;
743
247k
  return SUCCESS;
744
247k
} /* }}} */
745
746
PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
747
247k
{
748
247k
  if (spl_autoload_extensions) {
749
0
    zend_string_release_ex(spl_autoload_extensions, 0);
750
0
    spl_autoload_extensions = NULL;
751
0
  }
752
247k
  if (spl_autoload_functions) {
753
434
    zend_hash_destroy(spl_autoload_functions);
754
434
    FREE_HASHTABLE(spl_autoload_functions);
755
    spl_autoload_functions = NULL;
756
434
  }
757
247k
  return SUCCESS;
758
247k
} /* }}} */
759
760
static const zend_module_dep spl_deps[] = {
761
  ZEND_MOD_REQUIRED("json")
762
  ZEND_MOD_END
763
};
764
765
zend_module_entry spl_module_entry = {
766
  STANDARD_MODULE_HEADER_EX, NULL,
767
  spl_deps,
768
  "SPL",
769
  ext_functions,
770
  PHP_MINIT(spl),
771
  NULL,
772
  PHP_RINIT(spl),
773
  PHP_RSHUTDOWN(spl),
774
  PHP_MINFO(spl),
775
  PHP_SPL_VERSION,
776
  STANDARD_MODULE_PROPERTIES
777
};