Coverage Report

Created: 2025-09-27 06:26

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
990
  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
18
  SPL_ADD_CLASS(AppendIterator, z_list, sub, allow, ce_flags); \
161
18
  SPL_ADD_CLASS(ArrayIterator, z_list, sub, allow, ce_flags); \
162
18
  SPL_ADD_CLASS(ArrayObject, z_list, sub, allow, ce_flags); \
163
18
  SPL_ADD_CLASS(BadFunctionCallException, z_list, sub, allow, ce_flags); \
164
18
  SPL_ADD_CLASS(BadMethodCallException, z_list, sub, allow, ce_flags); \
165
18
  SPL_ADD_CLASS(CachingIterator, z_list, sub, allow, ce_flags); \
166
18
  SPL_ADD_CLASS(CallbackFilterIterator, z_list, sub, allow, ce_flags); \
167
18
  SPL_ADD_CLASS(DirectoryIterator, z_list, sub, allow, ce_flags); \
168
18
  SPL_ADD_CLASS(DomainException, z_list, sub, allow, ce_flags); \
169
18
  SPL_ADD_CLASS(EmptyIterator, z_list, sub, allow, ce_flags); \
170
18
  SPL_ADD_CLASS(FilesystemIterator, z_list, sub, allow, ce_flags); \
171
18
  SPL_ADD_CLASS(FilterIterator, z_list, sub, allow, ce_flags); \
172
18
  SPL_ADD_CLASS(GlobIterator, z_list, sub, allow, ce_flags); \
173
18
  SPL_ADD_CLASS(InfiniteIterator, z_list, sub, allow, ce_flags); \
174
18
  SPL_ADD_CLASS(InvalidArgumentException, z_list, sub, allow, ce_flags); \
175
18
  SPL_ADD_CLASS(IteratorIterator, z_list, sub, allow, ce_flags); \
176
18
  SPL_ADD_CLASS(LengthException, z_list, sub, allow, ce_flags); \
177
18
  SPL_ADD_CLASS(LimitIterator, z_list, sub, allow, ce_flags); \
178
18
  SPL_ADD_CLASS(LogicException, z_list, sub, allow, ce_flags); \
179
18
  SPL_ADD_CLASS(MultipleIterator, z_list, sub, allow, ce_flags); \
180
18
  SPL_ADD_CLASS(NoRewindIterator, z_list, sub, allow, ce_flags); \
181
18
  SPL_ADD_CLASS(OuterIterator, z_list, sub, allow, ce_flags); \
182
18
  SPL_ADD_CLASS(OutOfBoundsException, z_list, sub, allow, ce_flags); \
183
18
  SPL_ADD_CLASS(OutOfRangeException, z_list, sub, allow, ce_flags); \
184
18
  SPL_ADD_CLASS(OverflowException, z_list, sub, allow, ce_flags); \
185
18
  SPL_ADD_CLASS(ParentIterator, z_list, sub, allow, ce_flags); \
186
18
  SPL_ADD_CLASS(RangeException, z_list, sub, allow, ce_flags); \
187
18
  SPL_ADD_CLASS(RecursiveArrayIterator, z_list, sub, allow, ce_flags); \
188
18
  SPL_ADD_CLASS(RecursiveCachingIterator, z_list, sub, allow, ce_flags); \
189
18
  SPL_ADD_CLASS(RecursiveCallbackFilterIterator, z_list, sub, allow, ce_flags); \
190
18
  SPL_ADD_CLASS(RecursiveDirectoryIterator, z_list, sub, allow, ce_flags); \
191
18
  SPL_ADD_CLASS(RecursiveFilterIterator, z_list, sub, allow, ce_flags); \
192
18
  SPL_ADD_CLASS(RecursiveIterator, z_list, sub, allow, ce_flags); \
193
18
  SPL_ADD_CLASS(RecursiveIteratorIterator, z_list, sub, allow, ce_flags); \
194
18
  SPL_ADD_CLASS(RecursiveRegexIterator, z_list, sub, allow, ce_flags); \
195
18
  SPL_ADD_CLASS(RecursiveTreeIterator, z_list, sub, allow, ce_flags); \
196
18
  SPL_ADD_CLASS(RegexIterator, z_list, sub, allow, ce_flags); \
197
18
  SPL_ADD_CLASS(RuntimeException, z_list, sub, allow, ce_flags); \
198
18
  SPL_ADD_CLASS(SeekableIterator, z_list, sub, allow, ce_flags); \
199
18
  SPL_ADD_CLASS(SplDoublyLinkedList, z_list, sub, allow, ce_flags); \
200
18
  SPL_ADD_CLASS(SplFileInfo, z_list, sub, allow, ce_flags); \
201
18
  SPL_ADD_CLASS(SplFileObject, z_list, sub, allow, ce_flags); \
202
18
  SPL_ADD_CLASS(SplFixedArray, z_list, sub, allow, ce_flags); \
203
18
  SPL_ADD_CLASS(SplHeap, z_list, sub, allow, ce_flags); \
204
18
  SPL_ADD_CLASS(SplMinHeap, z_list, sub, allow, ce_flags); \
205
18
  SPL_ADD_CLASS(SplMaxHeap, z_list, sub, allow, ce_flags); \
206
18
  SPL_ADD_CLASS(SplObjectStorage, z_list, sub, allow, ce_flags); \
207
18
  SPL_ADD_CLASS(SplObserver, z_list, sub, allow, ce_flags); \
208
18
  SPL_ADD_CLASS(SplPriorityQueue, z_list, sub, allow, ce_flags); \
209
18
  SPL_ADD_CLASS(SplQueue, z_list, sub, allow, ce_flags); \
210
18
  SPL_ADD_CLASS(SplStack, z_list, sub, allow, ce_flags); \
211
18
  SPL_ADD_CLASS(SplSubject, z_list, sub, allow, ce_flags); \
212
18
  SPL_ADD_CLASS(SplTempFileObject, z_list, sub, allow, ce_flags); \
213
18
  SPL_ADD_CLASS(UnderflowException, z_list, sub, allow, ce_flags); \
214
18
  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
  if (zend_parse_parameters_none() == FAILURE) {
220
0
    RETURN_THROWS();
221
0
  }
222
223
0
  array_init(return_value);
224
225
0
  SPL_LIST_CLASSES(return_value, 0, 0, 0)
226
0
}
227
/* }}} */
228
229
static int spl_autoload(zend_string *class_name, zend_string *lc_name, const char *ext, int ext_len) /* {{{ */
230
0
{
231
0
  zend_string *class_file;
232
0
  zval dummy;
233
0
  zend_file_handle file_handle;
234
0
  zend_op_array *new_op_array;
235
0
  zval result;
236
0
  int ret;
237
238
0
  class_file = zend_strpprintf(0, "%s%.*s", ZSTR_VAL(lc_name), ext_len, ext);
239
240
0
#if DEFAULT_SLASH != '\\'
241
0
  {
242
0
    char *ptr = ZSTR_VAL(class_file);
243
0
    char *end = ptr + ZSTR_LEN(class_file);
244
245
0
    while ((ptr = memchr(ptr, '\\', (end - ptr))) != NULL) {
246
0
      *ptr = DEFAULT_SLASH;
247
0
    }
248
0
  }
249
0
#endif
250
251
0
  zend_stream_init_filename_ex(&file_handle, class_file);
252
0
  ret = php_stream_open_for_zend_ex(&file_handle, USE_PATH|STREAM_OPEN_FOR_INCLUDE);
253
254
0
  if (ret == SUCCESS) {
255
0
    zend_string *opened_path;
256
0
    if (!file_handle.opened_path) {
257
0
      file_handle.opened_path = zend_string_copy(class_file);
258
0
    }
259
0
    opened_path = zend_string_copy(file_handle.opened_path);
260
0
    ZVAL_NULL(&dummy);
261
0
    if (zend_hash_add(&EG(included_files), opened_path, &dummy)) {
262
0
      new_op_array = zend_compile_file(&file_handle, ZEND_REQUIRE);
263
0
    } else {
264
0
      new_op_array = NULL;
265
0
    }
266
0
    zend_string_release_ex(opened_path, 0);
267
0
    if (new_op_array) {
268
0
      uint32_t orig_jit_trace_num = EG(jit_trace_num);
269
270
0
      ZVAL_UNDEF(&result);
271
0
      zend_execute(new_op_array, &result);
272
0
      EG(jit_trace_num) = orig_jit_trace_num;
273
274
0
      destroy_op_array(new_op_array);
275
0
      efree(new_op_array);
276
0
      if (!EG(exception)) {
277
0
        zval_ptr_dtor(&result);
278
0
      }
279
280
0
      zend_destroy_file_handle(&file_handle);
281
0
      zend_string_release(class_file);
282
0
      return zend_hash_exists(EG(class_table), lc_name);
283
0
    }
284
0
  }
285
0
  zend_destroy_file_handle(&file_handle);
286
0
  zend_string_release(class_file);
287
0
  return 0;
288
0
} /* }}} */
289
290
/* {{{ Default autoloader implementation */
291
PHP_FUNCTION(spl_autoload)
292
0
{
293
0
  int pos_len, pos1_len;
294
0
  char *pos, *pos1;
295
0
  zend_string *class_name, *lc_name, *file_exts = NULL;
296
297
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|S!", &class_name, &file_exts) == FAILURE) {
298
0
    RETURN_THROWS();
299
0
  }
300
301
0
  if (!file_exts) {
302
0
    file_exts = spl_autoload_extensions;
303
0
  }
304
305
0
  if (file_exts == NULL) { /* autoload_extensions is not initialized, set to defaults */
306
0
    pos = SPL_DEFAULT_FILE_EXTENSIONS;
307
0
    pos_len = sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1;
308
0
  } else {
309
0
    pos = ZSTR_VAL(file_exts);
310
0
    pos_len = (int)ZSTR_LEN(file_exts);
311
0
  }
312
313
0
  lc_name = zend_string_tolower(class_name);
314
0
  while (pos && *pos && !EG(exception)) {
315
0
    pos1 = strchr(pos, ',');
316
0
    if (pos1) {
317
0
      pos1_len = (int)(pos1 - pos);
318
0
    } else {
319
0
      pos1_len = pos_len;
320
0
    }
321
0
    if (spl_autoload(class_name, lc_name, pos, pos1_len)) {
322
0
      break; /* loaded */
323
0
    }
324
0
    pos = pos1 ? pos1 + 1 : NULL;
325
0
    pos_len = pos1? pos_len - pos1_len - 1 : 0;
326
0
  }
327
0
  zend_string_release(lc_name);
328
0
} /* }}} */
329
330
/* {{{ Register and return default file extensions for spl_autoload */
331
PHP_FUNCTION(spl_autoload_extensions)
332
0
{
333
0
  zend_string *file_exts = NULL;
334
335
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &file_exts) == FAILURE) {
336
0
    RETURN_THROWS();
337
0
  }
338
339
0
  if (file_exts) {
340
0
    if (spl_autoload_extensions) {
341
0
      zend_string_release_ex(spl_autoload_extensions, 0);
342
0
    }
343
0
    spl_autoload_extensions = zend_string_copy(file_exts);
344
0
  }
345
346
0
  if (spl_autoload_extensions == NULL) {
347
0
    RETURN_STRINGL(SPL_DEFAULT_FILE_EXTENSIONS, sizeof(SPL_DEFAULT_FILE_EXTENSIONS) - 1);
348
0
  } else {
349
0
    zend_string_addref(spl_autoload_extensions);
350
0
    RETURN_STR(spl_autoload_extensions);
351
0
  }
352
0
} /* }}} */
353
354
typedef struct {
355
  zend_function *func_ptr;
356
  zend_object *obj;
357
  zend_object *closure;
358
  zend_class_entry *ce;
359
} autoload_func_info;
360
361
468
static void autoload_func_info_destroy(autoload_func_info *alfi) {
362
468
  if (alfi->obj) {
363
0
    zend_object_release(alfi->obj);
364
0
  }
365
468
  if (alfi->func_ptr &&
366
468
    UNEXPECTED(alfi->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
367
0
    zend_string_release_ex(alfi->func_ptr->common.function_name, 0);
368
0
    zend_free_trampoline(alfi->func_ptr);
369
0
  }
370
468
  if (alfi->closure) {
371
432
    zend_object_release(alfi->closure);
372
432
  }
373
468
  efree(alfi);
374
468
}
375
376
static void autoload_func_info_zval_dtor(zval *element)
377
468
{
378
468
  autoload_func_info_destroy(Z_PTR_P(element));
379
468
}
380
381
static autoload_func_info *autoload_func_info_from_fci(
382
468
    zend_fcall_info *fci, zend_fcall_info_cache *fcc) {
383
468
  autoload_func_info *alfi = emalloc(sizeof(autoload_func_info));
384
468
  alfi->ce = fcc->calling_scope;
385
468
  alfi->func_ptr = fcc->function_handler;
386
468
  alfi->obj = fcc->object;
387
468
  if (alfi->obj) {
388
0
    GC_ADDREF(alfi->obj);
389
0
  }
390
468
  if (Z_TYPE(fci->function_name) == IS_OBJECT) {
391
432
    alfi->closure = Z_OBJ(fci->function_name);
392
432
    GC_ADDREF(alfi->closure);
393
432
  } else {
394
36
    alfi->closure = NULL;
395
36
  }
396
468
  return alfi;
397
468
}
398
399
static bool autoload_func_info_equals(
400
13
    const autoload_func_info *alfi1, const autoload_func_info *alfi2) {
401
13
  if (UNEXPECTED(
402
13
    (alfi1->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) &&
403
13
    (alfi2->func_ptr->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)
404
13
  )) {
405
0
    return alfi1->obj == alfi2->obj
406
0
      && alfi1->ce == alfi2->ce
407
0
      && alfi1->closure == alfi2->closure
408
0
      && zend_string_equals(alfi1->func_ptr->common.function_name, alfi2->func_ptr->common.function_name)
409
0
    ;
410
0
  }
411
13
  return alfi1->func_ptr == alfi2->func_ptr
412
0
    && alfi1->obj == alfi2->obj
413
0
    && alfi1->ce == alfi2->ce
414
0
    && alfi1->closure == alfi2->closure;
415
13
}
416
417
171k
static zend_class_entry *spl_perform_autoload(zend_string *class_name, zend_string *lc_name) {
418
171k
  if (!spl_autoload_functions) {
419
170k
    return NULL;
420
170k
  }
421
422
  /* We don't use ZEND_HASH_MAP_FOREACH here,
423
   * because autoloaders may be added/removed during autoloading. */
424
985
  HashPosition pos;
425
985
  zend_hash_internal_pointer_reset_ex(spl_autoload_functions, &pos);
426
1.21k
  while (1) {
427
1.08k
    autoload_func_info *alfi =
428
1.08k
      zend_hash_get_current_data_ptr_ex(spl_autoload_functions, &pos);
429
1.08k
    if (!alfi) {
430
84
      break;
431
84
    }
432
433
998
    zend_function *func = alfi->func_ptr;
434
998
    if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
435
0
      func = emalloc(sizeof(zend_op_array));
436
0
      memcpy(func, alfi->func_ptr, sizeof(zend_op_array));
437
0
      zend_string_addref(func->op_array.function_name);
438
0
    }
439
440
998
    zval param;
441
998
    ZVAL_STR(&param, class_name);
442
998
    zend_call_known_function(func, alfi->obj, alfi->ce, NULL, 1, &param, NULL);
443
998
    if (EG(exception)) {
444
273
      break;
445
273
    }
446
447
725
    if (ZSTR_HAS_CE_CACHE(class_name) &&  ZSTR_GET_CE_CACHE(class_name)) {
448
250
      return (zend_class_entry*)ZSTR_GET_CE_CACHE(class_name);
449
475
    } else {
450
475
      zend_class_entry *ce = zend_hash_find_ptr(EG(class_table), lc_name);
451
475
      if (ce) {
452
245
        return ce;
453
245
      }
454
475
    }
455
456
230
    zend_hash_move_forward_ex(spl_autoload_functions, &pos);
457
230
  }
458
490
  return NULL;
459
985
}
460
461
/* {{{ Try all registered autoload function to load the requested class */
462
PHP_FUNCTION(spl_autoload_call)
463
0
{
464
0
  zend_string *class_name;
465
466
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &class_name) == FAILURE) {
467
0
    RETURN_THROWS();
468
0
  }
469
470
0
  zend_string *lc_name = zend_string_tolower(class_name);
471
0
  spl_perform_autoload(class_name, lc_name);
472
0
  zend_string_release(lc_name);
473
0
} /* }}} */
474
475
#define HT_MOVE_TAIL_TO_HEAD(ht)                    \
476
13
  ZEND_ASSERT(!HT_IS_PACKED(ht));                    \
477
13
  do {                               \
478
13
    Bucket tmp = (ht)->arData[(ht)->nNumUsed-1];        \
479
13
    memmove((ht)->arData + 1, (ht)->arData,         \
480
13
      sizeof(Bucket) * ((ht)->nNumUsed - 1));       \
481
13
    (ht)->arData[0] = tmp;                  \
482
13
    zend_hash_rehash(ht);                     \
483
13
  } while (0)
484
485
468
static Bucket *spl_find_registered_function(autoload_func_info *find_alfi) {
486
468
  if (!spl_autoload_functions) {
487
0
    return NULL;
488
0
  }
489
490
468
  autoload_func_info *alfi;
491
962
  ZEND_HASH_MAP_FOREACH_PTR(spl_autoload_functions, alfi) {
492
962
    if (autoload_func_info_equals(alfi, find_alfi)) {
493
0
      return _p;
494
0
    }
495
962
  } ZEND_HASH_FOREACH_END();
496
468
  return NULL;
497
468
}
498
499
/* {{{ Register given function as autoloader */
500
PHP_FUNCTION(spl_autoload_register)
501
473
{
502
473
  bool do_throw = 1;
503
473
  bool prepend  = 0;
504
473
  zend_fcall_info fci = {0};
505
473
  zend_fcall_info_cache fcc;
506
473
  autoload_func_info *alfi;
507
508
1.41k
  ZEND_PARSE_PARAMETERS_START(0, 3)
509
1.41k
    Z_PARAM_OPTIONAL
510
1.89k
    Z_PARAM_FUNC_OR_NULL(fci, fcc)
511
1.43k
    Z_PARAM_BOOL(do_throw)
512
65
    Z_PARAM_BOOL(prepend)
513
473
  ZEND_PARSE_PARAMETERS_END();
514
515
468
  if (!do_throw) {
516
0
    php_error_docref(NULL, E_NOTICE, "Argument #2 ($do_throw) has been ignored, "
517
0
      "spl_autoload_register() will always throw");
518
0
  }
519
520
468
  if (!spl_autoload_functions) {
521
455
    ALLOC_HASHTABLE(spl_autoload_functions);
522
455
    zend_hash_init(spl_autoload_functions, 1, NULL, autoload_func_info_zval_dtor, 0);
523
    /* Initialize as non-packed hash table for prepend functionality. */
524
455
    zend_hash_real_init_mixed(spl_autoload_functions);
525
455
  }
526
527
  /* If first arg is not null */
528
468
  if (ZEND_FCI_INITIALIZED(fci)) {
529
468
    if (!fcc.function_handler) {
530
      /* Call trampoline has been cleared by zpp. Refetch it, because we want to deal
531
       * with it ourselves. It is important that it is not refetched on every call,
532
       * because calls may occur from different scopes. */
533
0
      zend_is_callable_ex(&fci.function_name, NULL, IS_CALLABLE_SUPPRESS_DEPRECATIONS, NULL, &fcc, NULL);
534
0
    }
535
536
468
    if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
537
0
      fcc.function_handler->internal_function.handler == zif_spl_autoload_call) {
538
0
      zend_argument_value_error(1, "must not be the spl_autoload_call() function");
539
0
      RETURN_THROWS();
540
0
    }
541
542
468
    alfi = autoload_func_info_from_fci(&fci, &fcc);
543
468
    if (UNEXPECTED(alfi->func_ptr == &EG(trampoline))) {
544
0
      zend_function *copy = emalloc(sizeof(zend_op_array));
545
546
0
      memcpy(copy, alfi->func_ptr, sizeof(zend_op_array));
547
0
      alfi->func_ptr->common.function_name = NULL;
548
0
      alfi->func_ptr = copy;
549
0
    }
550
468
  } else {
551
0
    alfi = emalloc(sizeof(autoload_func_info));
552
0
    alfi->func_ptr = zend_hash_str_find_ptr(
553
0
      CG(function_table), "spl_autoload", sizeof("spl_autoload") - 1);
554
0
    alfi->obj = NULL;
555
0
    alfi->ce = NULL;
556
0
    alfi->closure = NULL;
557
0
  }
558
559
468
  if (spl_find_registered_function(alfi)) {
560
0
    autoload_func_info_destroy(alfi);
561
0
    RETURN_TRUE;
562
0
  }
563
564
468
  zend_hash_next_index_insert_ptr(spl_autoload_functions, alfi);
565
468
  if (prepend && spl_autoload_functions->nNumOfElements > 1) {
566
    /* Move the newly created element to the head of the hashtable */
567
13
    HT_MOVE_TAIL_TO_HEAD(spl_autoload_functions);
568
13
  }
569
570
468
  RETURN_TRUE;
571
468
} /* }}} */
572
573
/* {{{ Unregister given function as autoloader */
574
PHP_FUNCTION(spl_autoload_unregister)
575
0
{
576
0
  zend_fcall_info fci;
577
0
  zend_fcall_info_cache fcc;
578
579
0
  ZEND_PARSE_PARAMETERS_START(1, 1)
580
0
    Z_PARAM_FUNC(fci, fcc)
581
0
  ZEND_PARSE_PARAMETERS_END();
582
583
0
  if (fcc.function_handler && zend_string_equals_literal(
584
0
      fcc.function_handler->common.function_name, "spl_autoload_call")) {
585
0
    php_error_docref(NULL, E_DEPRECATED,
586
0
      "Using spl_autoload_call() as a callback for spl_autoload_unregister() is deprecated,"
587
0
      " to remove all registered autoloaders, call spl_autoload_unregister()"
588
0
      " for all values returned from spl_autoload_functions()");
589
0
    if (UNEXPECTED(EG(exception))) {
590
0
      RETURN_THROWS();
591
0
    }
592
0
    if (spl_autoload_functions) {
593
      /* Don't destroy the hash table, as we might be iterating over it right now. */
594
0
      zend_hash_clean(spl_autoload_functions);
595
0
    }
596
0
    RETURN_TRUE;
597
0
  }
598
599
0
  if (!fcc.function_handler) {
600
    /* Call trampoline has been cleared by zpp. Refetch it, because we want to deal
601
     * with it ourselves. It is important that it is not refetched on every call,
602
     * because calls may occur from different scopes. */
603
0
    zend_is_callable_ex(&fci.function_name, NULL, 0, NULL, &fcc, NULL);
604
0
  }
605
606
0
  autoload_func_info *alfi = autoload_func_info_from_fci(&fci, &fcc);
607
0
  Bucket *p = spl_find_registered_function(alfi);
608
0
  autoload_func_info_destroy(alfi);
609
0
  if (p) {
610
0
    zend_hash_del_bucket(spl_autoload_functions, p);
611
0
    RETURN_TRUE;
612
0
  }
613
614
0
  RETURN_FALSE;
615
0
} /* }}} */
616
617
/* {{{ Return all registered autoloader functions */
618
PHP_FUNCTION(spl_autoload_functions)
619
0
{
620
0
  autoload_func_info *alfi;
621
622
0
  if (zend_parse_parameters_none() == FAILURE) {
623
0
    RETURN_THROWS();
624
0
  }
625
626
0
  array_init(return_value);
627
0
  if (spl_autoload_functions) {
628
0
    ZEND_HASH_MAP_FOREACH_PTR(spl_autoload_functions, alfi) {
629
0
      if (alfi->closure) {
630
0
        GC_ADDREF(alfi->closure);
631
0
        add_next_index_object(return_value, alfi->closure);
632
0
      } else if (alfi->func_ptr->common.scope) {
633
0
        zval tmp;
634
635
0
        array_init(&tmp);
636
0
        if (alfi->obj) {
637
0
          GC_ADDREF(alfi->obj);
638
0
          add_next_index_object(&tmp, alfi->obj);
639
0
        } else {
640
0
          add_next_index_str(&tmp, zend_string_copy(alfi->ce->name));
641
0
        }
642
0
        add_next_index_str(&tmp, zend_string_copy(alfi->func_ptr->common.function_name));
643
0
        add_next_index_zval(return_value, &tmp);
644
0
      } else {
645
0
        add_next_index_str(return_value, zend_string_copy(alfi->func_ptr->common.function_name));
646
0
      }
647
0
    } ZEND_HASH_FOREACH_END();
648
0
  }
649
0
} /* }}} */
650
651
/* {{{ Return hash id for given object */
652
PHP_FUNCTION(spl_object_hash)
653
252
{
654
252
  zend_object *obj;
655
656
756
  ZEND_PARSE_PARAMETERS_START(1, 1)
657
1.00k
    Z_PARAM_OBJ(obj)
658
252
  ZEND_PARSE_PARAMETERS_END();
659
660
250
  RETURN_NEW_STR(php_spl_object_hash(obj));
661
250
}
662
/* }}} */
663
664
/* {{{ Returns the integer object handle for the given object */
665
PHP_FUNCTION(spl_object_id)
666
318
{
667
318
  zend_object *obj;
668
669
954
  ZEND_PARSE_PARAMETERS_START(1, 1)
670
1.27k
    Z_PARAM_OBJ(obj)
671
318
  ZEND_PARSE_PARAMETERS_END();
672
673
318
  RETURN_LONG((zend_long)obj->handle);
674
318
}
675
/* }}} */
676
677
PHPAPI zend_string *php_spl_object_hash(zend_object *obj) /* {{{*/
678
250
{
679
250
  return strpprintf(32, "%016zx0000000000000000", (intptr_t)obj->handle);
680
250
}
681
/* }}} */
682
683
static void spl_build_class_list_string(zval *entry, char **list) /* {{{ */
684
495
{
685
495
  char *res;
686
687
495
  spprintf(&res, 0, "%s, %s", *list, Z_STRVAL_P(entry));
688
495
  efree(*list);
689
495
  *list = res;
690
495
} /* }}} */
691
692
/* {{{ PHP_MINFO(spl) */
693
PHP_MINFO_FUNCTION(spl)
694
9
{
695
9
  zval list, *zv;
696
9
  char *strg;
697
698
9
  php_info_print_table_start();
699
9
  php_info_print_table_row(2, "SPL support", "enabled");
700
701
9
  array_init(&list);
702
9
  SPL_LIST_CLASSES(&list, 0, 1, ZEND_ACC_INTERFACE)
703
9
  strg = estrdup("");
704
108
  ZEND_HASH_MAP_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
705
108
    spl_build_class_list_string(zv, &strg);
706
108
  } ZEND_HASH_FOREACH_END();
707
9
  zend_array_destroy(Z_ARR(list));
708
9
  php_info_print_table_row(2, "Interfaces", strg + 2);
709
9
  efree(strg);
710
711
9
  array_init(&list);
712
9
  SPL_LIST_CLASSES(&list, 0, -1, ZEND_ACC_INTERFACE)
713
9
  strg = estrdup("");
714
918
  ZEND_HASH_MAP_FOREACH_VAL(Z_ARRVAL_P(&list), zv) {
715
918
    spl_build_class_list_string(zv, &strg);
716
918
  } ZEND_HASH_FOREACH_END();
717
9
  zend_array_destroy(Z_ARR(list));
718
9
  php_info_print_table_row(2, "Classes", strg + 2);
719
9
  efree(strg);
720
721
9
  php_info_print_table_end();
722
9
}
723
/* }}} */
724
725
/* {{{ PHP_MINIT_FUNCTION(spl) */
726
PHP_MINIT_FUNCTION(spl)
727
16
{
728
16
  zend_autoload = spl_perform_autoload;
729
730
16
  PHP_MINIT(spl_exceptions)(INIT_FUNC_ARGS_PASSTHRU);
731
16
  PHP_MINIT(spl_iterators)(INIT_FUNC_ARGS_PASSTHRU);
732
16
  PHP_MINIT(spl_array)(INIT_FUNC_ARGS_PASSTHRU);
733
16
  PHP_MINIT(spl_directory)(INIT_FUNC_ARGS_PASSTHRU);
734
16
  PHP_MINIT(spl_dllist)(INIT_FUNC_ARGS_PASSTHRU);
735
16
  PHP_MINIT(spl_heap)(INIT_FUNC_ARGS_PASSTHRU);
736
16
  PHP_MINIT(spl_fixedarray)(INIT_FUNC_ARGS_PASSTHRU);
737
16
  PHP_MINIT(spl_observer)(INIT_FUNC_ARGS_PASSTHRU);
738
739
16
  return SUCCESS;
740
16
}
741
/* }}} */
742
743
PHP_RINIT_FUNCTION(spl) /* {{{ */
744
278k
{
745
278k
  spl_autoload_extensions = NULL;
746
278k
  spl_autoload_functions = NULL;
747
278k
  return SUCCESS;
748
278k
} /* }}} */
749
750
PHP_RSHUTDOWN_FUNCTION(spl) /* {{{ */
751
278k
{
752
278k
  if (spl_autoload_extensions) {
753
0
    zend_string_release_ex(spl_autoload_extensions, 0);
754
0
    spl_autoload_extensions = NULL;
755
0
  }
756
278k
  if (spl_autoload_functions) {
757
455
    zend_hash_destroy(spl_autoload_functions);
758
455
    FREE_HASHTABLE(spl_autoload_functions);
759
    spl_autoload_functions = NULL;
760
455
  }
761
278k
  return SUCCESS;
762
278k
} /* }}} */
763
764
static const zend_module_dep spl_deps[] = {
765
  ZEND_MOD_REQUIRED("json")
766
  ZEND_MOD_END
767
};
768
769
zend_module_entry spl_module_entry = {
770
  STANDARD_MODULE_HEADER_EX, NULL,
771
  spl_deps,
772
  "SPL",
773
  ext_functions,
774
  PHP_MINIT(spl),
775
  NULL,
776
  PHP_RINIT(spl),
777
  PHP_RSHUTDOWN(spl),
778
  PHP_MINFO(spl),
779
  PHP_SPL_VERSION,
780
  STANDARD_MODULE_PROPERTIES
781
};