Coverage Report

Created: 2025-12-14 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_hash.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine                                                          |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 2.00 of the Zend license,     |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | http://www.zend.com/license/2_00.txt.                                |
11
   | If you did not receive a copy of the Zend license and are unable to  |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@zend.com so we can mail you a copy immediately.              |
14
   +----------------------------------------------------------------------+
15
   | Authors: Andi Gutmans <andi@php.net>                                 |
16
   |          Zeev Suraski <zeev@php.net>                                 |
17
   |          Dmitry Stogov <dmitry@php.net>                              |
18
   +----------------------------------------------------------------------+
19
*/
20
21
#include "zend.h"
22
#include "zend_globals.h"
23
#include "zend_variables.h"
24
25
#if defined(__aarch64__) || defined(_M_ARM64)
26
# include <arm_neon.h>
27
#endif
28
29
/* Prefer to use AVX2 instructions for better latency and throughput */
30
#if defined(__AVX2__)
31
# include <immintrin.h>
32
#elif defined( __SSE2__)
33
# include <emmintrin.h>
34
#endif
35
36
#if ZEND_DEBUG
37
# define HT_ASSERT(ht, expr) \
38
964M
  ZEND_ASSERT((expr) || (HT_FLAGS(ht) & HASH_FLAG_ALLOW_COW_VIOLATION))
39
#else
40
# define HT_ASSERT(ht, expr)
41
#endif
42
43
960M
#define HT_ASSERT_RC1(ht) HT_ASSERT(ht, GC_REFCOUNT(ht) == 1)
44
45
370
#define HT_POISONED_PTR ((HashTable *) (intptr_t) -1)
46
47
#if ZEND_DEBUG
48
49
1.45G
#define HT_OK         0x00
50
0
#define HT_IS_DESTROYING    0x01
51
0
#define HT_DESTROYED      0x02
52
0
#define HT_CLEANING       0x03
53
54
static void _zend_is_inconsistent(const HashTable *ht, const char *file, int line)
55
1.45G
{
56
1.45G
  if ((HT_FLAGS(ht) & HASH_FLAG_CONSISTENCY) == HT_OK) {
57
1.45G
    return;
58
1.45G
  }
59
0
  switch (HT_FLAGS(ht) & HASH_FLAG_CONSISTENCY) {
60
0
    case HT_IS_DESTROYING:
61
0
      zend_output_debug_string(1, "%s(%d) : ht=%p is being destroyed", file, line, ht);
62
0
      break;
63
0
    case HT_DESTROYED:
64
0
      zend_output_debug_string(1, "%s(%d) : ht=%p is already destroyed", file, line, ht);
65
0
      break;
66
0
    case HT_CLEANING:
67
0
      zend_output_debug_string(1, "%s(%d) : ht=%p is being cleaned", file, line, ht);
68
0
      break;
69
0
    default:
70
0
      zend_output_debug_string(1, "%s(%d) : ht=%p is inconsistent", file, line, ht);
71
0
      break;
72
0
  }
73
0
  ZEND_UNREACHABLE();
74
0
}
75
1.45G
#define IS_CONSISTENT(a) _zend_is_inconsistent(a, __FILE__, __LINE__);
76
5.45M
#define SET_INCONSISTENT(n) do { \
77
5.45M
    HT_FLAGS(ht) = (HT_FLAGS(ht) & ~HASH_FLAG_CONSISTENCY) | (n); \
78
5.45M
  } while (0)
79
#else
80
#define IS_CONSISTENT(a)
81
#define SET_INCONSISTENT(n)
82
#endif
83
84
#define ZEND_HASH_IF_FULL_DO_RESIZE(ht)       \
85
478M
  if ((ht)->nNumUsed >= (ht)->nTableSize) {   \
86
18.6k
    zend_hash_do_resize(ht);          \
87
18.6k
  }
88
89
538
ZEND_API void *zend_hash_str_find_ptr_lc(const HashTable *ht, const char *str, size_t len) {
90
538
  void *result;
91
538
  char *lc_str;
92
93
  /* Stack allocate small strings to improve performance */
94
538
  ALLOCA_FLAG(use_heap)
95
96
538
  lc_str = zend_str_tolower_copy(do_alloca(len + 1, use_heap), str, len);
97
538
  result = zend_hash_str_find_ptr(ht, lc_str, len);
98
538
  free_alloca(lc_str, use_heap);
99
100
538
  return result;
101
538
}
102
103
1.25k
ZEND_API void *zend_hash_find_ptr_lc(const HashTable *ht, zend_string *key) {
104
1.25k
  void *result;
105
1.25k
  zend_string *lc_key = zend_string_tolower(key);
106
1.25k
  result = zend_hash_find_ptr(ht, lc_key);
107
1.25k
  zend_string_release(lc_key);
108
1.25k
  return result;
109
1.25k
}
110
111
static void ZEND_FASTCALL zend_hash_do_resize(HashTable *ht);
112
113
static zend_always_inline uint32_t zend_hash_check_size(uint32_t nSize)
114
3.59M
{
115
#ifdef ZEND_WIN32
116
  unsigned long index;
117
#endif
118
119
  /* Use big enough power of 2 */
120
  /* size should be between HT_MIN_SIZE and HT_MAX_SIZE */
121
3.59M
  if (nSize <= HT_MIN_SIZE) {
122
3.45M
    return HT_MIN_SIZE;
123
3.45M
  } else if (UNEXPECTED(nSize > HT_MAX_SIZE)) {
124
0
    zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", nSize, sizeof(Bucket), sizeof(Bucket));
125
0
  }
126
127
#ifdef ZEND_WIN32
128
  if (BitScanReverse(&index, nSize - 1)) {
129
    return 0x2u << ((31 - index) ^ 0x1f);
130
  } else {
131
    /* nSize is ensured to be in the valid range, fall back to it
132
       rather than using an undefined bis scan result. */
133
    return nSize;
134
  }
135
#elif (defined(__GNUC__) || __has_builtin(__builtin_clz))  && defined(PHP_HAVE_BUILTIN_CLZ)
136
144k
  return 0x2u << (__builtin_clz(nSize - 1) ^ 0x1f);
137
#else
138
  nSize -= 1;
139
  nSize |= (nSize >> 1);
140
  nSize |= (nSize >> 2);
141
  nSize |= (nSize >> 4);
142
  nSize |= (nSize >> 8);
143
  nSize |= (nSize >> 16);
144
  return nSize + 1;
145
#endif
146
3.59M
}
147
148
static zend_always_inline void zend_hash_real_init_packed_ex(HashTable *ht)
149
1.37M
{
150
1.37M
  void *data;
151
152
1.37M
  if (UNEXPECTED(GC_FLAGS(ht) & IS_ARRAY_PERSISTENT)) {
153
144
    data = pemalloc(HT_PACKED_SIZE_EX(ht->nTableSize, HT_MIN_MASK), 1);
154
1.37M
  } else if (EXPECTED(ht->nTableSize == HT_MIN_SIZE)) {
155
    /* Use specialized API with constant allocation amount for a particularly common case. */
156
1.36M
    data = emalloc(HT_PACKED_SIZE_EX(HT_MIN_SIZE, HT_MIN_MASK));
157
1.36M
  } else {
158
8.01k
    data = emalloc(HT_PACKED_SIZE_EX(ht->nTableSize, HT_MIN_MASK));
159
8.01k
  }
160
1.37M
  HT_SET_DATA_ADDR(ht, data);
161
  /* Don't overwrite iterator count. */
162
1.37M
  ht->u.v.flags = HASH_FLAG_PACKED | HASH_FLAG_STATIC_KEYS;
163
1.37M
  HT_HASH_RESET_PACKED(ht);
164
1.37M
}
165
166
static zend_always_inline void zend_hash_real_init_mixed_ex(HashTable *ht)
167
1.66M
{
168
1.66M
  void *data;
169
1.66M
  uint32_t nSize = ht->nTableSize;
170
171
1.66M
  ZEND_ASSERT(HT_SIZE_TO_MASK(nSize));
172
173
1.66M
  if (UNEXPECTED(GC_FLAGS(ht) & IS_ARRAY_PERSISTENT)) {
174
604
    data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), 1);
175
1.66M
  } else if (EXPECTED(nSize == HT_MIN_SIZE)) {
176
1.56M
    data = emalloc(HT_SIZE_EX(HT_MIN_SIZE, HT_SIZE_TO_MASK(HT_MIN_SIZE)));
177
1.56M
    ht->nTableMask = HT_SIZE_TO_MASK(HT_MIN_SIZE);
178
1.56M
    HT_SET_DATA_ADDR(ht, data);
179
    /* Don't overwrite iterator count. */
180
1.56M
    ht->u.v.flags = HASH_FLAG_STATIC_KEYS;
181
#if defined(__AVX2__)
182
    do {
183
      __m256i ymm0 = _mm256_setzero_si256();
184
      ymm0 = _mm256_cmpeq_epi64(ymm0, ymm0);
185
      _mm256_storeu_si256((__m256i*)&HT_HASH_EX(data,  0), ymm0);
186
      _mm256_storeu_si256((__m256i*)&HT_HASH_EX(data,  8), ymm0);
187
    } while(0);
188
#elif defined (__SSE2__)
189
1.56M
    do {
190
1.56M
      __m128i xmm0 = _mm_setzero_si128();
191
1.56M
      xmm0 = _mm_cmpeq_epi8(xmm0, xmm0);
192
1.56M
      _mm_storeu_si128((__m128i*)&HT_HASH_EX(data,  0), xmm0);
193
1.56M
      _mm_storeu_si128((__m128i*)&HT_HASH_EX(data,  4), xmm0);
194
1.56M
      _mm_storeu_si128((__m128i*)&HT_HASH_EX(data,  8), xmm0);
195
1.56M
      _mm_storeu_si128((__m128i*)&HT_HASH_EX(data, 12), xmm0);
196
1.56M
    } while (0);
197
#elif defined(__aarch64__) || defined(_M_ARM64)
198
    do {
199
      int32x4_t t = vdupq_n_s32(-1);
200
      vst1q_s32((int32_t*)&HT_HASH_EX(data,  0), t);
201
      vst1q_s32((int32_t*)&HT_HASH_EX(data,  4), t);
202
      vst1q_s32((int32_t*)&HT_HASH_EX(data,  8), t);
203
      vst1q_s32((int32_t*)&HT_HASH_EX(data, 12), t);
204
    } while (0);
205
#else
206
    HT_HASH_EX(data,  0) = -1;
207
    HT_HASH_EX(data,  1) = -1;
208
    HT_HASH_EX(data,  2) = -1;
209
    HT_HASH_EX(data,  3) = -1;
210
    HT_HASH_EX(data,  4) = -1;
211
    HT_HASH_EX(data,  5) = -1;
212
    HT_HASH_EX(data,  6) = -1;
213
    HT_HASH_EX(data,  7) = -1;
214
    HT_HASH_EX(data,  8) = -1;
215
    HT_HASH_EX(data,  9) = -1;
216
    HT_HASH_EX(data, 10) = -1;
217
    HT_HASH_EX(data, 11) = -1;
218
    HT_HASH_EX(data, 12) = -1;
219
    HT_HASH_EX(data, 13) = -1;
220
    HT_HASH_EX(data, 14) = -1;
221
    HT_HASH_EX(data, 15) = -1;
222
#endif
223
1.56M
    return;
224
1.56M
  } else {
225
96.9k
    data = emalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)));
226
96.9k
  }
227
97.5k
  ht->nTableMask = HT_SIZE_TO_MASK(nSize);
228
97.5k
  HT_SET_DATA_ADDR(ht, data);
229
97.5k
  HT_FLAGS(ht) = HASH_FLAG_STATIC_KEYS;
230
97.5k
  HT_HASH_RESET(ht);
231
97.5k
}
232
233
static zend_always_inline void zend_hash_real_init_ex(HashTable *ht, bool packed)
234
13.8k
{
235
13.8k
  HT_ASSERT_RC1(ht);
236
13.8k
  ZEND_ASSERT(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED);
237
13.8k
  if (packed) {
238
13
    zend_hash_real_init_packed_ex(ht);
239
13.8k
  } else {
240
13.8k
    zend_hash_real_init_mixed_ex(ht);
241
13.8k
  }
242
13.8k
}
243
244
static const uint32_t uninitialized_bucket[-HT_MIN_MASK] =
245
  {HT_INVALID_IDX, HT_INVALID_IDX};
246
247
ZEND_API const HashTable zend_empty_array = {
248
  .gc.refcount = 2,
249
  .gc.u.type_info = IS_ARRAY | (GC_IMMUTABLE << GC_FLAGS_SHIFT),
250
  .u.flags = HASH_FLAG_UNINITIALIZED,
251
  .nTableMask = HT_MIN_MASK,
252
  {.arData = (Bucket*)&uninitialized_bucket[2]},
253
  .nNumUsed = 0,
254
  .nNumOfElements = 0,
255
  .nTableSize = HT_MIN_SIZE,
256
  .nInternalPointer = 0,
257
  .nNextFreeElement = ZEND_LONG_MIN,
258
  .pDestructor = ZVAL_PTR_DTOR
259
};
260
261
static zend_always_inline void _zend_hash_init_int(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, bool persistent)
262
3.59M
{
263
3.59M
  GC_SET_REFCOUNT(ht, 1);
264
3.59M
  GC_TYPE_INFO(ht) = GC_ARRAY | (persistent ? ((GC_PERSISTENT|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT) : 0);
265
3.59M
  HT_FLAGS(ht) = HASH_FLAG_UNINITIALIZED;
266
3.59M
  ht->nTableMask = HT_MIN_MASK;
267
3.59M
  HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
268
3.59M
  ht->nNumUsed = 0;
269
3.59M
  ht->nNumOfElements = 0;
270
3.59M
  ht->nInternalPointer = 0;
271
3.59M
  ht->nNextFreeElement = ZEND_LONG_MIN;
272
3.59M
  ht->pDestructor = pDestructor;
273
3.59M
  ht->nTableSize = zend_hash_check_size(nSize);
274
3.59M
}
275
276
ZEND_API void ZEND_FASTCALL _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, bool persistent)
277
602k
{
278
602k
  _zend_hash_init_int(ht, nSize, pDestructor, persistent);
279
602k
}
280
281
ZEND_API HashTable* ZEND_FASTCALL _zend_new_array_0(void)
282
0
{
283
0
  HashTable *ht = emalloc(sizeof(HashTable));
284
0
  _zend_hash_init_int(ht, HT_MIN_SIZE, ZVAL_PTR_DTOR, false);
285
0
  return ht;
286
0
}
287
288
ZEND_API HashTable* ZEND_FASTCALL _zend_new_array(uint32_t nSize)
289
2.99M
{
290
2.99M
  HashTable *ht = emalloc(sizeof(HashTable));
291
2.99M
  _zend_hash_init_int(ht, nSize, ZVAL_PTR_DTOR, false);
292
2.99M
  return ht;
293
2.99M
}
294
295
ZEND_API HashTable* ZEND_FASTCALL zend_new_pair(const zval *val1, const zval *val2)
296
0
{
297
0
  zval *zv;
298
0
  HashTable *ht = emalloc(sizeof(HashTable));
299
0
  _zend_hash_init_int(ht, HT_MIN_SIZE, ZVAL_PTR_DTOR, false);
300
0
  ht->nNumUsed = ht->nNumOfElements = ht->nNextFreeElement = 2;
301
0
  zend_hash_real_init_packed_ex(ht);
302
303
0
  zv = ht->arPacked;
304
0
  ZVAL_COPY_VALUE(zv, val1);
305
0
  zv++;
306
0
  ZVAL_COPY_VALUE(zv, val2);
307
0
  return ht;
308
0
}
309
310
ZEND_API void ZEND_FASTCALL zend_hash_packed_grow(HashTable *ht)
311
50.9k
{
312
50.9k
  HT_ASSERT_RC1(ht);
313
50.9k
  if (ht->nTableSize >= HT_MAX_SIZE) {
314
0
    zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", ht->nTableSize * 2, sizeof(Bucket), sizeof(Bucket));
315
0
  }
316
50.9k
  uint32_t newTableSize = ht->nTableSize * 2;
317
50.9k
  HT_SET_DATA_ADDR(ht, perealloc2(HT_GET_DATA_ADDR(ht), HT_PACKED_SIZE_EX(newTableSize, HT_MIN_MASK), HT_PACKED_USED_SIZE(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
318
50.9k
  ht->nTableSize = newTableSize;
319
50.9k
}
320
321
ZEND_API void ZEND_FASTCALL zend_hash_real_init(HashTable *ht, bool packed)
322
13.8k
{
323
13.8k
  IS_CONSISTENT(ht);
324
325
13.8k
  HT_ASSERT_RC1(ht);
326
13.8k
  zend_hash_real_init_ex(ht, packed);
327
13.8k
}
328
329
ZEND_API void ZEND_FASTCALL zend_hash_real_init_packed(HashTable *ht)
330
249k
{
331
249k
  IS_CONSISTENT(ht);
332
333
249k
  HT_ASSERT_RC1(ht);
334
249k
  zend_hash_real_init_packed_ex(ht);
335
249k
}
336
337
ZEND_API void ZEND_FASTCALL zend_hash_real_init_mixed(HashTable *ht)
338
1.64M
{
339
1.64M
  IS_CONSISTENT(ht);
340
341
1.64M
  HT_ASSERT_RC1(ht);
342
1.64M
  zend_hash_real_init_mixed_ex(ht);
343
1.64M
}
344
345
ZEND_API void ZEND_FASTCALL zend_hash_packed_to_hash(HashTable *ht)
346
3.65k
{
347
3.65k
  void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
348
3.65k
  zval *src = ht->arPacked;
349
3.65k
  Bucket *dst;
350
3.65k
  uint32_t i;
351
3.65k
  uint32_t nSize = ht->nTableSize;
352
353
3.65k
  ZEND_ASSERT(HT_SIZE_TO_MASK(nSize));
354
355
3.65k
  HT_ASSERT_RC1(ht);
356
  // Alloc before assign to avoid inconsistencies on OOM
357
3.65k
  new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
358
3.65k
  HT_FLAGS(ht) &= ~HASH_FLAG_PACKED;
359
3.65k
  ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
360
3.65k
  HT_SET_DATA_ADDR(ht, new_data);
361
3.65k
  dst = ht->arData;
362
35.3k
  for (i = 0; i < ht->nNumUsed; i++) {
363
31.6k
    ZVAL_COPY_VALUE(&dst->val, src);
364
31.6k
    dst->h = i;
365
31.6k
    dst->key = NULL;
366
31.6k
    dst++;
367
31.6k
    src++;
368
31.6k
  }
369
3.65k
  pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
370
3.65k
  zend_hash_rehash(ht);
371
3.65k
}
372
373
ZEND_API void ZEND_FASTCALL zend_hash_to_packed(HashTable *ht)
374
0
{
375
0
  void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
376
0
  Bucket *src = ht->arData;
377
0
  zval *dst;
378
0
  uint32_t i;
379
380
0
  HT_ASSERT_RC1(ht);
381
0
  new_data = pemalloc(HT_PACKED_SIZE_EX(ht->nTableSize, HT_MIN_MASK), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
382
0
  HT_FLAGS(ht) |= HASH_FLAG_PACKED | HASH_FLAG_STATIC_KEYS;
383
0
  ht->nTableMask = HT_MIN_MASK;
384
0
  HT_SET_DATA_ADDR(ht, new_data);
385
0
  HT_HASH_RESET_PACKED(ht);
386
0
  dst = ht->arPacked;
387
0
  for (i = 0; i < ht->nNumUsed; i++) {
388
0
    ZVAL_COPY_VALUE(dst, &src->val);
389
0
    dst++;
390
0
    src++;
391
0
  }
392
0
  pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
393
0
}
394
395
ZEND_API void ZEND_FASTCALL zend_hash_extend(HashTable *ht, uint32_t nSize, bool packed)
396
30.4k
{
397
30.4k
  HT_ASSERT_RC1(ht);
398
399
30.4k
  if (nSize == 0) return;
400
401
29.4k
  ZEND_ASSERT(HT_SIZE_TO_MASK(nSize));
402
403
29.4k
  if (UNEXPECTED(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
404
13.8k
    if (nSize > ht->nTableSize) {
405
224
      ht->nTableSize = zend_hash_check_size(nSize);
406
224
    }
407
13.8k
    zend_hash_real_init(ht, packed);
408
15.5k
  } else {
409
15.5k
    if (packed) {
410
22
      ZEND_ASSERT(HT_IS_PACKED(ht));
411
22
      if (nSize > ht->nTableSize) {
412
0
        uint32_t newTableSize = zend_hash_check_size(nSize);
413
0
        HT_SET_DATA_ADDR(ht, perealloc2(HT_GET_DATA_ADDR(ht), HT_PACKED_SIZE_EX(newTableSize, HT_MIN_MASK), HT_PACKED_USED_SIZE(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
414
0
        ht->nTableSize = newTableSize;
415
0
      }
416
15.5k
    } else {
417
15.5k
      ZEND_ASSERT(!HT_IS_PACKED(ht));
418
15.5k
      if (nSize > ht->nTableSize) {
419
250
        void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
420
250
        Bucket *old_buckets = ht->arData;
421
250
        nSize = zend_hash_check_size(nSize);
422
250
        new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
423
250
        ht->nTableSize = nSize;
424
250
        ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
425
250
        HT_SET_DATA_ADDR(ht, new_data);
426
250
        memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
427
250
        pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
428
250
        zend_hash_rehash(ht);
429
250
      }
430
15.5k
    }
431
15.5k
  }
432
29.4k
}
433
434
ZEND_API void ZEND_FASTCALL zend_hash_discard(HashTable *ht, uint32_t nNumUsed)
435
0
{
436
0
  Bucket *p, *end, *arData;
437
0
  uint32_t nIndex;
438
439
0
  ZEND_ASSERT(!HT_IS_PACKED(ht));
440
0
  arData = ht->arData;
441
0
  p = arData + ht->nNumUsed;
442
0
  end = arData + nNumUsed;
443
0
  ht->nNumUsed = nNumUsed;
444
0
  while (p != end) {
445
0
    p--;
446
0
    if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
447
0
    ht->nNumOfElements--;
448
    /* Collision pointers always directed from higher to lower buckets */
449
#if 0
450
    if (!(Z_NEXT(p->val) == HT_INVALID_IDX || HT_HASH_TO_BUCKET_EX(arData, Z_NEXT(p->val)) < p)) {
451
      abort();
452
    }
453
#endif
454
0
    nIndex = p->h | ht->nTableMask;
455
0
    HT_HASH_EX(arData, nIndex) = Z_NEXT(p->val);
456
0
  }
457
0
}
458
459
static uint32_t zend_array_recalc_elements(const HashTable *ht)
460
510
{
461
510
  zval *val;
462
510
  uint32_t num = ht->nNumOfElements;
463
464
2.87k
  ZEND_HASH_MAP_FOREACH_VAL(ht, val) {
465
2.87k
    if (Z_TYPE_P(val) == IS_INDIRECT) {
466
898
      if (UNEXPECTED(Z_TYPE_P(Z_INDIRECT_P(val)) == IS_UNDEF)) {
467
524
        num--;
468
524
      }
469
898
    }
470
2.87k
  } ZEND_HASH_FOREACH_END();
471
510
  return num;
472
510
}
473
/* }}} */
474
475
ZEND_API uint32_t zend_array_count(HashTable *ht)
476
8.70k
{
477
8.70k
  uint32_t num;
478
8.70k
  if (UNEXPECTED(HT_FLAGS(ht) & HASH_FLAG_HAS_EMPTY_IND)) {
479
510
    num = zend_array_recalc_elements(ht);
480
510
    if (UNEXPECTED(ht->nNumOfElements == num)) {
481
92
      HT_FLAGS(ht) &= ~HASH_FLAG_HAS_EMPTY_IND;
482
92
    }
483
8.19k
  } else if (UNEXPECTED(ht == &EG(symbol_table))) {
484
0
    num = zend_array_recalc_elements(ht);
485
8.19k
  } else {
486
8.19k
    num = zend_hash_num_elements(ht);
487
8.19k
  }
488
8.70k
  return num;
489
8.70k
}
490
/* }}} */
491
492
static zend_always_inline HashPosition _zend_hash_get_valid_pos(const HashTable *ht, HashPosition pos)
493
4.65k
{
494
4.65k
  if (HT_IS_PACKED(ht)) {
495
1.86k
    while (pos < ht->nNumUsed && Z_ISUNDEF(ht->arPacked[pos])) {
496
428
      pos++;
497
428
    }
498
3.21k
  } else {
499
3.29k
    while (pos < ht->nNumUsed && Z_ISUNDEF(ht->arData[pos].val)) {
500
78
      pos++;
501
78
    }
502
3.21k
  }
503
4.65k
  return pos;
504
4.65k
}
505
506
static zend_always_inline HashPosition _zend_hash_get_current_pos(const HashTable *ht)
507
303
{
508
303
  return _zend_hash_get_valid_pos(ht, ht->nInternalPointer);
509
303
}
510
511
ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos(const HashTable *ht)
512
87
{
513
87
  return _zend_hash_get_current_pos(ht);
514
87
}
515
516
ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos_ex(const HashTable *ht, HashPosition pos)
517
0
{
518
0
  return _zend_hash_get_valid_pos(ht, pos);
519
0
}
520
521
30
static void zend_hash_remove_iterator_copies(uint32_t idx) {
522
30
  HashTableIterator *iterators = EG(ht_iterators);
523
524
30
  HashTableIterator *iter = iterators + idx;
525
30
  uint32_t next_idx = iter->next_copy;
526
198
  while (next_idx != idx) {
527
168
    uint32_t cur_idx = next_idx;
528
168
    HashTableIterator *cur_iter = iterators + cur_idx;
529
168
    next_idx = cur_iter->next_copy;
530
168
    cur_iter->next_copy = cur_idx; // avoid recursion in zend_hash_iterator_del
531
168
    zend_hash_iterator_del(cur_idx);
532
168
  }
533
30
  iter->next_copy = idx;
534
30
}
535
536
ZEND_API uint32_t ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPosition pos)
537
1.01k
{
538
1.01k
  HashTableIterator *iter = EG(ht_iterators);
539
1.01k
  HashTableIterator *end  = iter + EG(ht_iterators_count);
540
1.01k
  uint32_t idx;
541
542
1.01k
  if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
543
1.01k
    HT_INC_ITERATORS_COUNT(ht);
544
1.01k
  }
545
5.55k
  while (iter != end) {
546
5.54k
    if (iter->ht == NULL) {
547
1.00k
      iter->ht = ht;
548
1.00k
      iter->pos = pos;
549
1.00k
      idx = iter - EG(ht_iterators);
550
1.00k
      iter->next_copy = idx;
551
1.00k
      if (idx + 1 > EG(ht_iterators_used)) {
552
1.00k
        EG(ht_iterators_used) = idx + 1;
553
1.00k
      }
554
1.00k
      return idx;
555
1.00k
    }
556
4.54k
    iter++;
557
4.54k
  }
558
14
  if (EG(ht_iterators) == EG(ht_iterators_slots)) {
559
2
    EG(ht_iterators) = emalloc(sizeof(HashTableIterator) * (EG(ht_iterators_count) + 8));
560
2
    memcpy(EG(ht_iterators), EG(ht_iterators_slots), sizeof(HashTableIterator) * EG(ht_iterators_count));
561
12
  } else {
562
12
    EG(ht_iterators) = erealloc(EG(ht_iterators), sizeof(HashTableIterator) * (EG(ht_iterators_count) + 8));
563
12
  }
564
14
  iter = EG(ht_iterators) + EG(ht_iterators_count);
565
14
  EG(ht_iterators_count) += 8;
566
14
  iter->ht = ht;
567
14
  iter->pos = pos;
568
14
  memset(iter + 1, 0, sizeof(HashTableIterator) * 7);
569
14
  idx = iter - EG(ht_iterators);
570
14
  iter->next_copy = idx;
571
14
  EG(ht_iterators_used) = idx + 1;
572
14
  return idx;
573
1.01k
}
574
575
// To avoid losing track of the HashTable when separating arrays, we track all copies at once.
576
242
static zend_always_inline bool zend_hash_iterator_find_copy_pos(uint32_t idx, HashTable *ht) {
577
242
  HashTableIterator *iter = EG(ht_iterators) + idx;
578
579
242
  uint32_t next_idx = iter->next_copy;
580
242
  if (EXPECTED(next_idx != idx)) {
581
26
    HashTableIterator *copy_iter;
582
28
    while (next_idx != idx) {
583
28
      copy_iter = EG(ht_iterators) + next_idx;
584
28
      if (copy_iter->ht == ht) {
585
        // We have found the hashtable we are actually iterating over
586
        // Now clean any intermittent copies and replace the original index by the found one
587
26
        if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
588
22
          && EXPECTED(!HT_ITERATORS_OVERFLOW(iter->ht))) {
589
22
          HT_DEC_ITERATORS_COUNT(iter->ht);
590
22
        }
591
26
        if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
592
26
          HT_INC_ITERATORS_COUNT(ht);
593
26
        }
594
26
        iter->ht = copy_iter->ht;
595
26
        iter->pos = copy_iter->pos;
596
26
        zend_hash_remove_iterator_copies(idx);
597
26
        return true;
598
26
      }
599
2
      next_idx = copy_iter->next_copy;
600
2
    }
601
0
    zend_hash_remove_iterator_copies(idx);
602
0
  }
603
604
216
  return false;
605
242
}
606
607
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos(uint32_t idx, HashTable *ht)
608
770
{
609
770
  HashTableIterator *iter = EG(ht_iterators) + idx;
610
611
770
  ZEND_ASSERT(idx != (uint32_t)-1);
612
770
  if (UNEXPECTED(iter->ht != ht) && !zend_hash_iterator_find_copy_pos(idx, ht)) {
613
2
    if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
614
0
        && EXPECTED(!HT_ITERATORS_OVERFLOW(iter->ht))) {
615
0
      HT_DEC_ITERATORS_COUNT(iter->ht);
616
0
    }
617
2
    if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
618
2
      HT_INC_ITERATORS_COUNT(ht);
619
2
    }
620
2
    iter->ht = ht;
621
2
    iter->pos = _zend_hash_get_current_pos(ht);
622
2
  }
623
770
  return iter->pos;
624
770
}
625
626
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval *array)
627
3.02k
{
628
3.02k
  HashTable *ht = Z_ARRVAL_P(array);
629
3.02k
  HashTableIterator *iter = EG(ht_iterators) + idx;
630
631
3.02k
  ZEND_ASSERT(idx != (uint32_t)-1);
632
3.02k
  if (UNEXPECTED(iter->ht != ht) && !zend_hash_iterator_find_copy_pos(idx, ht)) {
633
214
    if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
634
0
        && EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
635
0
      HT_DEC_ITERATORS_COUNT(iter->ht);
636
0
    }
637
638
    /* Inlined SEPARATE_ARRAY() with updating of iterator when EG(ht_iterators) grows. */
639
214
    if (UNEXPECTED(GC_REFCOUNT(ht) > 1)) {
640
130
      ZVAL_ARR(array, zend_array_dup(ht));
641
130
      GC_TRY_DELREF(ht);
642
130
      iter = EG(ht_iterators) + idx;
643
130
      ht = Z_ARRVAL_P(array);
644
130
    }
645
646
214
    if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
647
214
      HT_INC_ITERATORS_COUNT(ht);
648
214
    }
649
214
    iter->ht = ht;
650
214
    iter->pos = _zend_hash_get_current_pos(ht);
651
214
  }
652
3.02k
  return iter->pos;
653
3.02k
}
654
655
ZEND_API void ZEND_FASTCALL zend_hash_iterator_del(uint32_t idx)
656
1.00k
{
657
1.00k
  HashTableIterator *iter = EG(ht_iterators) + idx;
658
659
1.00k
  ZEND_ASSERT(idx != (uint32_t)-1);
660
661
1.00k
  if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
662
855
      && EXPECTED(!HT_ITERATORS_OVERFLOW(iter->ht))) {
663
855
    ZEND_ASSERT(HT_ITERATORS_COUNT(iter->ht) != 0);
664
855
    HT_DEC_ITERATORS_COUNT(iter->ht);
665
855
  }
666
1.00k
  iter->ht = NULL;
667
668
1.00k
  if (UNEXPECTED(iter->next_copy != idx)) {
669
4
    zend_hash_remove_iterator_copies(idx);
670
4
  }
671
672
1.00k
  if (idx == EG(ht_iterators_used) - 1) {
673
1.00k
    while (idx > 0 && EG(ht_iterators)[idx - 1].ht == NULL) {
674
12
      idx--;
675
12
    }
676
993
    EG(ht_iterators_used) = idx;
677
993
  }
678
1.00k
}
679
680
static zend_never_inline void ZEND_FASTCALL _zend_hash_iterators_remove(const HashTable *ht)
681
240
{
682
240
  HashTableIterator *iter = EG(ht_iterators);
683
240
  const HashTableIterator *end = iter + EG(ht_iterators_used);
684
685
4.90k
  while (iter != end) {
686
4.66k
    if (iter->ht == ht) {
687
370
      iter->ht = HT_POISONED_PTR;
688
370
    }
689
4.66k
    iter++;
690
4.66k
  }
691
240
}
692
693
static zend_always_inline void zend_hash_iterators_remove(const HashTable *ht)
694
3.08M
{
695
3.08M
  if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
696
240
    _zend_hash_iterators_remove(ht);
697
240
  }
698
3.08M
}
699
700
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterators_lower_pos(const HashTable *ht, HashPosition start)
701
252
{
702
252
  const HashTableIterator *iter = EG(ht_iterators);
703
252
  const HashTableIterator *end = iter + EG(ht_iterators_used);
704
252
  HashPosition res = ht->nNumUsed;
705
706
488
  while (iter != end) {
707
236
    if (iter->ht == ht) {
708
230
      if (iter->pos >= start && iter->pos < res) {
709
60
        res = iter->pos;
710
60
      }
711
230
    }
712
236
    iter++;
713
236
  }
714
252
  return res;
715
252
}
716
717
ZEND_API void ZEND_FASTCALL _zend_hash_iterators_update(const HashTable *ht, HashPosition from, HashPosition to)
718
62
{
719
62
  HashTableIterator *iter = EG(ht_iterators);
720
62
  const HashTableIterator *end = iter + EG(ht_iterators_used);
721
722
144
  while (iter != end) {
723
82
    if (iter->ht == ht && iter->pos == from) {
724
50
      iter->pos = to;
725
50
    }
726
82
    iter++;
727
82
  }
728
62
}
729
730
ZEND_API void ZEND_FASTCALL zend_hash_iterators_advance(const HashTable *ht, HashPosition step)
731
2
{
732
2
  HashTableIterator *iter = EG(ht_iterators);
733
2
  const HashTableIterator *end = iter + EG(ht_iterators_used);
734
735
4
  while (iter != end) {
736
2
    if (iter->ht == ht) {
737
2
      iter->pos += step;
738
2
    }
739
2
    iter++;
740
2
  }
741
2
}
742
743
/* Hash must be known and precomputed before */
744
static zend_always_inline Bucket *zend_hash_find_bucket(const HashTable *ht, const zend_string *key)
745
5.88M
{
746
5.88M
  uint32_t nIndex;
747
5.88M
  uint32_t idx;
748
5.88M
  Bucket *p, *arData;
749
750
5.88M
  ZEND_ASSERT(ZSTR_H(key) != 0 && "Hash must be known");
751
752
5.88M
  arData = ht->arData;
753
5.88M
  nIndex = ZSTR_H(key) | ht->nTableMask;
754
5.88M
  idx = HT_HASH_EX(arData, nIndex);
755
756
5.88M
  if (UNEXPECTED(idx == HT_INVALID_IDX)) {
757
1.90M
    return NULL;
758
1.90M
  }
759
3.97M
  p = HT_HASH_TO_BUCKET_EX(arData, idx);
760
3.97M
  if (EXPECTED(p->key == key)) { /* check for the same interned string */
761
3.11M
    return p;
762
3.11M
  }
763
764
965k
  while (1) {
765
965k
    if (p->h == ZSTR_H(key) &&
766
286k
        EXPECTED(p->key) &&
767
286k
        zend_string_equal_content(p->key, key)) {
768
286k
      return p;
769
286k
    }
770
678k
    idx = Z_NEXT(p->val);
771
678k
    if (idx == HT_INVALID_IDX) {
772
536k
      return NULL;
773
536k
    }
774
142k
    p = HT_HASH_TO_BUCKET_EX(arData, idx);
775
142k
    if (p->key == key) { /* check for the same interned string */
776
34.1k
      return p;
777
34.1k
    }
778
142k
  }
779
856k
}
780
781
static zend_always_inline Bucket *zend_hash_str_find_bucket(const HashTable *ht, const char *str, size_t len, zend_ulong h)
782
308k
{
783
308k
  uint32_t nIndex;
784
308k
  uint32_t idx;
785
308k
  Bucket *p, *arData;
786
787
308k
  arData = ht->arData;
788
308k
  nIndex = h | ht->nTableMask;
789
308k
  idx = HT_HASH_EX(arData, nIndex);
790
340k
  while (idx != HT_INVALID_IDX) {
791
225k
    ZEND_ASSERT(idx < HT_IDX_TO_HASH(ht->nTableSize));
792
225k
    p = HT_HASH_TO_BUCKET_EX(arData, idx);
793
225k
    if ((p->h == h)
794
192k
       && p->key
795
192k
       && zend_string_equals_cstr(p->key, str, len)) {
796
192k
      return p;
797
192k
    }
798
32.2k
    idx = Z_NEXT(p->val);
799
32.2k
  }
800
115k
  return NULL;
801
308k
}
802
803
static zend_always_inline Bucket *zend_hash_index_find_bucket(const HashTable *ht, zend_ulong h)
804
960M
{
805
960M
  uint32_t nIndex;
806
960M
  uint32_t idx;
807
960M
  Bucket *p, *arData;
808
809
960M
  arData = ht->arData;
810
960M
  nIndex = h | ht->nTableMask;
811
960M
  idx = HT_HASH_EX(arData, nIndex);
812
1.02G
  while (idx != HT_INVALID_IDX) {
813
543M
    ZEND_ASSERT(idx < HT_IDX_TO_HASH(ht->nTableSize));
814
543M
    p = HT_HASH_TO_BUCKET_EX(arData, idx);
815
543M
    if (p->h == h && !p->key) {
816
482M
      return p;
817
482M
    }
818
60.7M
    idx = Z_NEXT(p->val);
819
60.7M
  }
820
478M
  return NULL;
821
960M
}
822
823
static zend_always_inline zval *_zend_hash_add_or_update_i(HashTable *ht, zend_string *key, zval *pData, uint32_t flag)
824
1.27M
{
825
1.27M
  zend_ulong h;
826
1.27M
  uint32_t nIndex;
827
1.27M
  uint32_t idx;
828
1.27M
  Bucket *p, *arData;
829
830
1.27M
  IS_CONSISTENT(ht);
831
1.27M
  HT_ASSERT_RC1(ht);
832
1.27M
  zend_string_hash_val(key);
833
834
1.27M
  if (UNEXPECTED(HT_FLAGS(ht) & (HASH_FLAG_UNINITIALIZED|HASH_FLAG_PACKED))) {
835
224k
    if (EXPECTED(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
836
221k
      zend_hash_real_init_mixed(ht);
837
221k
      goto add_to_hash;
838
221k
    } else {
839
2.69k
      zend_hash_packed_to_hash(ht);
840
2.69k
    }
841
1.05M
  } else if ((flag & HASH_ADD_NEW) == 0 || ZEND_DEBUG) {
842
1.05M
    p = zend_hash_find_bucket(ht, key);
843
844
1.05M
    if (p) {
845
235k
      zval *data;
846
847
235k
      ZEND_ASSERT((flag & HASH_ADD_NEW) == 0);
848
235k
      if (flag & HASH_LOOKUP) {
849
5.59k
        return &p->val;
850
229k
      } else if (flag & HASH_ADD) {
851
75.4k
        if (!(flag & HASH_UPDATE_INDIRECT)) {
852
75.0k
          return NULL;
853
75.0k
        }
854
408
        ZEND_ASSERT(&p->val != pData);
855
408
        data = &p->val;
856
408
        if (Z_TYPE_P(data) == IS_INDIRECT) {
857
0
          data = Z_INDIRECT_P(data);
858
0
          if (Z_TYPE_P(data) != IS_UNDEF) {
859
0
            return NULL;
860
0
          }
861
408
        } else {
862
408
          return NULL;
863
408
        }
864
154k
      } else {
865
154k
        ZEND_ASSERT(&p->val != pData);
866
154k
        data = &p->val;
867
154k
        if ((flag & HASH_UPDATE_INDIRECT) && Z_TYPE_P(data) == IS_INDIRECT) {
868
0
          data = Z_INDIRECT_P(data);
869
0
        }
870
154k
      }
871
154k
      if (ht->pDestructor) {
872
142k
        ht->pDestructor(data);
873
142k
      }
874
154k
      ZVAL_COPY_VALUE(data, pData);
875
154k
      return data;
876
235k
    }
877
1.05M
  }
878
879
822k
  ZEND_HASH_IF_FULL_DO_RESIZE(ht);   /* If the Hash table is full, resize it */
880
881
1.04M
add_to_hash:
882
1.04M
  if (!ZSTR_IS_INTERNED(key)) {
883
263k
    zend_string_addref(key);
884
263k
    HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
885
263k
  }
886
1.04M
  idx = ht->nNumUsed++;
887
1.04M
  ht->nNumOfElements++;
888
1.04M
  arData = ht->arData;
889
1.04M
  p = arData + idx;
890
1.04M
  p->key = key;
891
1.04M
  p->h = h = ZSTR_H(key);
892
1.04M
  nIndex = h | ht->nTableMask;
893
1.04M
  Z_NEXT(p->val) = HT_HASH_EX(arData, nIndex);
894
1.04M
  HT_HASH_EX(arData, nIndex) = HT_IDX_TO_HASH(idx);
895
1.04M
  if (flag & HASH_LOOKUP) {
896
7.26k
    ZVAL_NULL(&p->val);
897
1.03M
  } else {
898
1.03M
    ZVAL_COPY_VALUE(&p->val, pData);
899
1.03M
  }
900
901
1.04M
  return &p->val;
902
822k
}
903
904
static zend_always_inline zval *_zend_hash_str_add_or_update_i(HashTable *ht, const char *str, size_t len, zend_ulong h, zval *pData, uint32_t flag)
905
5.62k
{
906
5.62k
  zend_string *key;
907
5.62k
  uint32_t nIndex;
908
5.62k
  uint32_t idx;
909
5.62k
  Bucket *p;
910
911
5.62k
  IS_CONSISTENT(ht);
912
5.62k
  HT_ASSERT_RC1(ht);
913
914
5.62k
  if (UNEXPECTED(HT_FLAGS(ht) & (HASH_FLAG_UNINITIALIZED|HASH_FLAG_PACKED))) {
915
3.09k
    if (EXPECTED(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
916
3.09k
      zend_hash_real_init_mixed(ht);
917
3.09k
      goto add_to_hash;
918
3.09k
    } else {
919
0
      zend_hash_packed_to_hash(ht);
920
0
    }
921
3.09k
  } else if ((flag & HASH_ADD_NEW) == 0) {
922
2.49k
    p = zend_hash_str_find_bucket(ht, str, len, h);
923
924
2.49k
    if (p) {
925
0
      zval *data;
926
927
0
      if (flag & HASH_LOOKUP) {
928
0
        return &p->val;
929
0
      } else if (flag & HASH_ADD) {
930
0
        if (!(flag & HASH_UPDATE_INDIRECT)) {
931
0
          return NULL;
932
0
        }
933
0
        ZEND_ASSERT(&p->val != pData);
934
0
        data = &p->val;
935
0
        if (Z_TYPE_P(data) == IS_INDIRECT) {
936
0
          data = Z_INDIRECT_P(data);
937
0
          if (Z_TYPE_P(data) != IS_UNDEF) {
938
0
            return NULL;
939
0
          }
940
0
        } else {
941
0
          return NULL;
942
0
        }
943
0
      } else {
944
0
        ZEND_ASSERT(&p->val != pData);
945
0
        data = &p->val;
946
0
        if ((flag & HASH_UPDATE_INDIRECT) && Z_TYPE_P(data) == IS_INDIRECT) {
947
0
          data = Z_INDIRECT_P(data);
948
0
        }
949
0
      }
950
0
      if (ht->pDestructor) {
951
0
        ht->pDestructor(data);
952
0
      }
953
0
      ZVAL_COPY_VALUE(data, pData);
954
0
      return data;
955
0
    }
956
2.49k
  }
957
958
2.53k
  ZEND_HASH_IF_FULL_DO_RESIZE(ht);   /* If the Hash table is full, resize it */
959
960
5.62k
add_to_hash:
961
5.62k
  idx = ht->nNumUsed++;
962
5.62k
  ht->nNumOfElements++;
963
5.62k
  p = ht->arData + idx;
964
5.62k
  p->key = key = zend_string_init(str, len, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
965
#if ZEND_RC_DEBUG
966
  if (GC_FLAGS(ht) & GC_PERSISTENT_LOCAL) {
967
    GC_MAKE_PERSISTENT_LOCAL(key);
968
  }
969
#endif
970
5.62k
  p->h = ZSTR_H(key) = h;
971
5.62k
  HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
972
5.62k
  if (flag & HASH_LOOKUP) {
973
0
    ZVAL_NULL(&p->val);
974
5.62k
  } else {
975
5.62k
    ZVAL_COPY_VALUE(&p->val, pData);
976
5.62k
  }
977
5.62k
  nIndex = h | ht->nTableMask;
978
5.62k
  Z_NEXT(p->val) = HT_HASH(ht, nIndex);
979
5.62k
  HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
980
981
5.62k
  return &p->val;
982
2.53k
}
983
984
ZEND_API zval* ZEND_FASTCALL zend_hash_add_or_update(HashTable *ht, zend_string *key, zval *pData, uint32_t flag)
985
0
{
986
0
  if (flag == HASH_ADD) {
987
0
    return zend_hash_add(ht, key, pData);
988
0
  } else if (flag == HASH_ADD_NEW) {
989
0
    return zend_hash_add_new(ht, key, pData);
990
0
  } else if (flag == HASH_UPDATE) {
991
0
    return zend_hash_update(ht, key, pData);
992
0
  } else {
993
0
    ZEND_ASSERT(flag == (HASH_UPDATE|HASH_UPDATE_INDIRECT));
994
0
    return zend_hash_update_ind(ht, key, pData);
995
0
  }
996
0
}
997
998
ZEND_API zval* ZEND_FASTCALL zend_hash_add(HashTable *ht, zend_string *key, zval *pData)
999
190k
{
1000
190k
  return _zend_hash_add_or_update_i(ht, key, pData, HASH_ADD);
1001
190k
}
1002
1003
ZEND_API zval* ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key, zval *pData)
1004
559k
{
1005
559k
  return _zend_hash_add_or_update_i(ht, key, pData, HASH_UPDATE);
1006
559k
}
1007
1008
ZEND_API zval* ZEND_FASTCALL zend_hash_update_ind(HashTable *ht, zend_string *key, zval *pData)
1009
972
{
1010
972
  return _zend_hash_add_or_update_i(ht, key, pData, HASH_UPDATE | HASH_UPDATE_INDIRECT);
1011
972
}
1012
1013
ZEND_API zval* ZEND_FASTCALL zend_hash_add_new(HashTable *ht, zend_string *key, zval *pData)
1014
515k
{
1015
515k
  return _zend_hash_add_or_update_i(ht, key, pData, HASH_ADD_NEW);
1016
515k
}
1017
1018
ZEND_API zval* ZEND_FASTCALL zend_hash_lookup(HashTable *ht, zend_string *key)
1019
12.8k
{
1020
12.8k
  return _zend_hash_add_or_update_i(ht, key, NULL, HASH_LOOKUP);
1021
12.8k
}
1022
1023
ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_or_update(HashTable *ht, const char *str, size_t len, zval *pData, uint32_t flag)
1024
0
{
1025
0
  if (flag == HASH_ADD) {
1026
0
    return zend_hash_str_add(ht, str, len, pData);
1027
0
  } else if (flag == HASH_ADD_NEW) {
1028
0
    return zend_hash_str_add_new(ht, str, len, pData);
1029
0
  } else if (flag == HASH_UPDATE) {
1030
0
    return zend_hash_str_update(ht, str, len, pData);
1031
0
  } else {
1032
0
    ZEND_ASSERT(flag == (HASH_UPDATE|HASH_UPDATE_INDIRECT));
1033
0
    return zend_hash_str_update_ind(ht, str, len, pData);
1034
0
  }
1035
0
}
1036
1037
ZEND_API zval* ZEND_FASTCALL zend_hash_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
1038
2.91k
{
1039
2.91k
  zend_ulong h = zend_hash_func(str, len);
1040
1041
2.91k
  return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_UPDATE);
1042
2.91k
}
1043
1044
ZEND_API zval* ZEND_FASTCALL zend_hash_str_update_ind(HashTable *ht, const char *str, size_t len, zval *pData)
1045
0
{
1046
0
  zend_ulong h = zend_hash_func(str, len);
1047
1048
0
  return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_UPDATE | HASH_UPDATE_INDIRECT);
1049
0
}
1050
1051
ZEND_API zval* ZEND_FASTCALL zend_hash_str_add(HashTable *ht, const char *str, size_t len, zval *pData)
1052
2.62k
{
1053
2.62k
  zend_ulong h = zend_hash_func(str, len);
1054
1055
2.62k
  return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_ADD);
1056
2.62k
}
1057
1058
ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_new(HashTable *ht, const char *str, size_t len, zval *pData)
1059
90
{
1060
90
  zend_ulong h = zend_hash_func(str, len);
1061
1062
90
  return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_ADD_NEW);
1063
90
}
1064
1065
ZEND_API zval* ZEND_FASTCALL zend_hash_str_lookup(HashTable *ht, const char *str, size_t len)
1066
0
{
1067
0
  zend_ulong h = zend_hash_func(str, len);
1068
1069
0
  return _zend_hash_str_add_or_update_i(ht, str, len, h, NULL, HASH_LOOKUP);
1070
0
}
1071
1072
ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_empty_element(HashTable *ht, zend_ulong h)
1073
178
{
1074
178
  zval dummy;
1075
1076
178
  ZVAL_NULL(&dummy);
1077
178
  return zend_hash_index_add(ht, h, &dummy);
1078
178
}
1079
1080
ZEND_API zval* ZEND_FASTCALL zend_hash_add_empty_element(HashTable *ht, zend_string *key)
1081
97.0k
{
1082
97.0k
  zval dummy;
1083
1084
97.0k
  ZVAL_NULL(&dummy);
1085
97.0k
  return zend_hash_add(ht, key, &dummy);
1086
97.0k
}
1087
1088
ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_empty_element(HashTable *ht, const char *str, size_t len)
1089
0
{
1090
0
  zval dummy;
1091
1092
0
  ZVAL_NULL(&dummy);
1093
0
  return zend_hash_str_add(ht, str, len, &dummy);
1094
0
}
1095
1096
static zend_always_inline zval *_zend_hash_index_add_or_update_i(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag)
1097
480M
{
1098
480M
  uint32_t nIndex;
1099
480M
  uint32_t idx;
1100
480M
  Bucket *p;
1101
480M
  zval *zv;
1102
1103
480M
  IS_CONSISTENT(ht);
1104
480M
  HT_ASSERT_RC1(ht);
1105
1106
480M
  if ((flag & HASH_ADD_NEXT) && h == ZEND_LONG_MIN) {
1107
1.12M
    h = 0;
1108
1.12M
  }
1109
1110
480M
  if (HT_IS_PACKED(ht)) {
1111
1.07M
    if ((flag & (HASH_ADD_NEW|HASH_ADD_NEXT)) != (HASH_ADD_NEW|HASH_ADD_NEXT)
1112
204k
     && h < ht->nNumUsed) {
1113
636
      zv = ht->arPacked + h;
1114
636
      if (Z_TYPE_P(zv) != IS_UNDEF) {
1115
419
        if (flag & HASH_LOOKUP) {
1116
0
          return zv;
1117
0
        }
1118
4.82k
replace:
1119
4.82k
        if (flag & HASH_ADD) {
1120
2.80k
          return NULL;
1121
2.80k
        }
1122
2.02k
        if (ht->pDestructor) {
1123
2.02k
          ht->pDestructor(zv);
1124
2.02k
        }
1125
2.02k
        ZVAL_COPY_VALUE(zv, pData);
1126
2.02k
        return zv;
1127
4.82k
      } else { /* we have to keep the order :( */
1128
217
        goto convert_to_hash;
1129
217
      }
1130
1.07M
    } else if (EXPECTED(h < ht->nTableSize)) {
1131
2.20M
add_to_packed:
1132
2.20M
      zv = ht->arPacked + h;
1133
      /* incremental initialization of empty Buckets */
1134
2.20M
      if ((flag & (HASH_ADD_NEW|HASH_ADD_NEXT)) != (HASH_ADD_NEW|HASH_ADD_NEXT)) {
1135
228k
        if (h > ht->nNumUsed) {
1136
4.04k
          zval *q = ht->arPacked + ht->nNumUsed;
1137
15.1k
          while (q != zv) {
1138
11.0k
            ZVAL_UNDEF(q);
1139
11.0k
            q++;
1140
11.0k
          }
1141
4.04k
        }
1142
228k
      }
1143
2.20M
      ht->nNextFreeElement = ht->nNumUsed = h + 1;
1144
2.20M
      ht->nNumOfElements++;
1145
2.20M
      if (flag & HASH_LOOKUP) {
1146
7.46k
        ZVAL_NULL(zv);
1147
2.19M
      } else {
1148
2.19M
        ZVAL_COPY_VALUE(zv, pData);
1149
2.19M
      }
1150
1151
2.20M
      return zv;
1152
1.02M
    } else if ((h >> 1) < ht->nTableSize &&
1153
50.9k
               (ht->nTableSize >> 1) < ht->nNumOfElements) {
1154
50.8k
      zend_hash_packed_grow(ht);
1155
50.8k
      goto add_to_packed;
1156
50.8k
    } else {
1157
446
      if (ht->nNumUsed >= ht->nTableSize) {
1158
13
        ht->nTableSize += ht->nTableSize;
1159
13
      }
1160
663
convert_to_hash:
1161
663
      zend_hash_packed_to_hash(ht);
1162
663
    }
1163
479M
  } else if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) {
1164
1.16M
    if (h < ht->nTableSize) {
1165
1.12M
      zend_hash_real_init_packed_ex(ht);
1166
1.12M
      goto add_to_packed;
1167
1.12M
    }
1168
37.2k
    zend_hash_real_init_mixed(ht);
1169
477M
  } else {
1170
477M
    if ((flag & HASH_ADD_NEW) == 0 || ZEND_DEBUG) {
1171
477M
      p = zend_hash_index_find_bucket(ht, h);
1172
477M
      if (p) {
1173
5.03k
        if (flag & HASH_LOOKUP) {
1174
632
          return &p->val;
1175
632
        }
1176
4.40k
        ZEND_ASSERT((flag & HASH_ADD_NEW) == 0);
1177
4.40k
        zv = &p->val;
1178
4.40k
        goto replace;
1179
4.40k
      }
1180
477M
    }
1181
477M
    ZEND_HASH_IF_FULL_DO_RESIZE(ht);   /* If the Hash table is full, resize it */
1182
477M
  }
1183
1184
477M
  idx = ht->nNumUsed++;
1185
477M
  nIndex = h | ht->nTableMask;
1186
477M
  p = ht->arData + idx;
1187
477M
  Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1188
477M
  HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1189
477M
  if ((zend_long)h >= ht->nNextFreeElement) {
1190
2.69M
    ht->nNextFreeElement = (zend_long)h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
1191
2.69M
  }
1192
477M
  ht->nNumOfElements++;
1193
477M
  p->h = h;
1194
477M
  p->key = NULL;
1195
477M
  if (flag & HASH_LOOKUP) {
1196
5.94k
    ZVAL_NULL(&p->val);
1197
477M
  } else {
1198
477M
    ZVAL_COPY_VALUE(&p->val, pData);
1199
477M
  }
1200
1201
477M
  return &p->val;
1202
480M
}
1203
1204
ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_or_update(HashTable *ht, zend_ulong h, zval *pData, uint32_t flag)
1205
0
{
1206
0
  if (flag == HASH_ADD) {
1207
0
    return zend_hash_index_add(ht, h, pData);
1208
0
  } else if (flag == (HASH_ADD|HASH_ADD_NEW)) {
1209
0
    return zend_hash_index_add_new(ht, h, pData);
1210
0
  } else if (flag == (HASH_ADD|HASH_ADD_NEXT)) {
1211
0
    ZEND_ASSERT(h == ht->nNextFreeElement);
1212
0
    return zend_hash_next_index_insert(ht, pData);
1213
0
  } else if (flag == (HASH_ADD|HASH_ADD_NEW|HASH_ADD_NEXT)) {
1214
0
    ZEND_ASSERT(h == ht->nNextFreeElement);
1215
0
    return zend_hash_next_index_insert_new(ht, pData);
1216
0
  } else {
1217
0
    ZEND_ASSERT(flag == HASH_UPDATE);
1218
0
    return zend_hash_index_update(ht, h, pData);
1219
0
  }
1220
0
}
1221
1222
ZEND_API zval* ZEND_FASTCALL zend_hash_index_add(HashTable *ht, zend_ulong h, zval *pData)
1223
5.03k
{
1224
5.03k
  return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_ADD);
1225
5.03k
}
1226
1227
ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData)
1228
477M
{
1229
477M
  return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_ADD | HASH_ADD_NEW);
1230
477M
}
1231
1232
ZEND_API zval* ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData)
1233
6.18k
{
1234
6.18k
  return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_UPDATE);
1235
6.18k
}
1236
1237
ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData)
1238
214k
{
1239
214k
  return _zend_hash_index_add_or_update_i(ht, ht->nNextFreeElement, pData, HASH_ADD | HASH_ADD_NEXT);
1240
214k
}
1241
1242
ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert_new(HashTable *ht, zval *pData)
1243
1.97M
{
1244
1.97M
  return _zend_hash_index_add_or_update_i(ht, ht->nNextFreeElement, pData, HASH_ADD | HASH_ADD_NEW | HASH_ADD_NEXT);
1245
1.97M
}
1246
1247
ZEND_API zval* ZEND_FASTCALL zend_hash_index_lookup(HashTable *ht, zend_ulong h)
1248
14.0k
{
1249
14.0k
  return _zend_hash_index_add_or_update_i(ht, h, NULL, HASH_LOOKUP);
1250
14.0k
}
1251
1252
ZEND_API zval* ZEND_FASTCALL zend_hash_set_bucket_key(HashTable *ht, Bucket *b, zend_string *key)
1253
2.83k
{
1254
2.83k
  uint32_t nIndex;
1255
2.83k
  uint32_t idx, i;
1256
2.83k
  Bucket *p, *arData;
1257
1258
2.83k
  IS_CONSISTENT(ht);
1259
2.83k
  HT_ASSERT_RC1(ht);
1260
2.83k
  ZEND_ASSERT(!HT_IS_PACKED(ht));
1261
1262
2.83k
  (void)zend_string_hash_val(key);
1263
2.83k
  p = zend_hash_find_bucket(ht, key);
1264
2.83k
  if (UNEXPECTED(p)) {
1265
58
    return (p == b) ? &p->val : NULL;
1266
58
  }
1267
1268
2.77k
  if (!ZSTR_IS_INTERNED(key)) {
1269
20
    zend_string_addref(key);
1270
20
    HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1271
20
  }
1272
1273
2.77k
  arData = ht->arData;
1274
1275
  /* del from hash */
1276
2.77k
  idx = HT_IDX_TO_HASH(b - arData);
1277
2.77k
  nIndex = b->h | ht->nTableMask;
1278
2.77k
  i = HT_HASH_EX(arData, nIndex);
1279
2.77k
  if (i == idx) {
1280
2.77k
    HT_HASH_EX(arData, nIndex) = Z_NEXT(b->val);
1281
2.77k
  } else {
1282
2
    p = HT_HASH_TO_BUCKET_EX(arData, i);
1283
2
    while (Z_NEXT(p->val) != idx) {
1284
0
      i = Z_NEXT(p->val);
1285
0
      p = HT_HASH_TO_BUCKET_EX(arData, i);
1286
0
    }
1287
2
    Z_NEXT(p->val) = Z_NEXT(b->val);
1288
2
  }
1289
2.77k
  zend_string_release(b->key);
1290
1291
  /* add to hash */
1292
2.77k
  idx = b - arData;
1293
2.77k
  b->key = key;
1294
2.77k
  b->h = ZSTR_H(key);
1295
2.77k
  nIndex = b->h | ht->nTableMask;
1296
2.77k
  idx = HT_IDX_TO_HASH(idx);
1297
2.77k
  i = HT_HASH_EX(arData, nIndex);
1298
2.77k
  if (i == HT_INVALID_IDX || i < idx) {
1299
2.77k
    Z_NEXT(b->val) = i;
1300
2.77k
    HT_HASH_EX(arData, nIndex) = idx;
1301
2.77k
  } else {
1302
1
    p = HT_HASH_TO_BUCKET_EX(arData, i);
1303
1
    while (Z_NEXT(p->val) != HT_INVALID_IDX && Z_NEXT(p->val) > idx) {
1304
0
      i = Z_NEXT(p->val);
1305
0
      p = HT_HASH_TO_BUCKET_EX(arData, i);
1306
0
    }
1307
1
    Z_NEXT(b->val) = Z_NEXT(p->val);
1308
1
    Z_NEXT(p->val) = idx;
1309
1
  }
1310
2.77k
  return &b->val;
1311
2.83k
}
1312
1313
static void ZEND_FASTCALL zend_hash_do_resize(HashTable *ht)
1314
18.6k
{
1315
1316
18.6k
  IS_CONSISTENT(ht);
1317
18.6k
  HT_ASSERT_RC1(ht);
1318
1319
18.6k
  ZEND_ASSERT(!HT_IS_PACKED(ht));
1320
18.6k
  if (ht->nNumUsed > ht->nNumOfElements + (ht->nNumOfElements >> 5)) { /* additional term is there to amortize the cost of compaction */
1321
10.5k
    zend_hash_rehash(ht);
1322
10.5k
  } else if (ht->nTableSize < HT_MAX_SIZE) { /* Let's double the table size */
1323
8.09k
    void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
1324
8.09k
    uint32_t nSize = ht->nTableSize + ht->nTableSize;
1325
8.09k
    Bucket *old_buckets = ht->arData;
1326
1327
8.09k
    ZEND_ASSERT(HT_SIZE_TO_MASK(nSize));
1328
1329
8.09k
    new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
1330
8.09k
    ht->nTableSize = nSize;
1331
8.09k
    ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
1332
8.09k
    HT_SET_DATA_ADDR(ht, new_data);
1333
8.09k
    memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
1334
8.09k
    pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
1335
8.09k
    zend_hash_rehash(ht);
1336
8.09k
  } else {
1337
0
    zend_error_noreturn(E_ERROR, "Possible integer overflow in memory allocation (%u * %zu + %zu)", ht->nTableSize * 2, sizeof(Bucket) + sizeof(uint32_t), sizeof(Bucket));
1338
0
  }
1339
18.6k
}
1340
1341
ZEND_API void ZEND_FASTCALL zend_hash_rehash(HashTable *ht)
1342
22.6k
{
1343
22.6k
  Bucket *p;
1344
22.6k
  uint32_t nIndex, i;
1345
1346
22.6k
  IS_CONSISTENT(ht);
1347
1348
22.6k
  if (UNEXPECTED(ht->nNumOfElements == 0)) {
1349
8
    if (!(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
1350
8
      ht->nNumUsed = 0;
1351
8
      HT_HASH_RESET(ht);
1352
      /* Even if the array is empty, we still need to reset the iterator positions. */
1353
8
      ht->nInternalPointer = 0;
1354
8
      if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
1355
4
        HashTableIterator *iter = EG(ht_iterators);
1356
4
        HashTableIterator *end  = iter + EG(ht_iterators_used);
1357
8
        while (iter != end) {
1358
4
          if (iter->ht == ht) {
1359
4
            iter->pos = 0;
1360
4
          }
1361
4
          iter++;
1362
4
        }
1363
4
      }
1364
8
    }
1365
8
    return;
1366
8
  }
1367
1368
22.6k
  HT_HASH_RESET(ht);
1369
22.6k
  i = 0;
1370
22.6k
  p = ht->arData;
1371
22.6k
  if (HT_IS_WITHOUT_HOLES(ht)) {
1372
123k
    do {
1373
123k
      nIndex = p->h | ht->nTableMask;
1374
123k
      Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1375
123k
      HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(i);
1376
123k
      p++;
1377
123k
    } while (++i < ht->nNumUsed);
1378
11.7k
  } else {
1379
11.7k
    uint32_t old_num_used = ht->nNumUsed;
1380
371k
    do {
1381
371k
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) {
1382
11.7k
        uint32_t j = i;
1383
11.7k
        Bucket *q = p;
1384
1385
11.7k
        if (EXPECTED(!HT_HAS_ITERATORS(ht))) {
1386
243M
          while (++i < ht->nNumUsed) {
1387
243M
            p++;
1388
243M
            if (EXPECTED(Z_TYPE_INFO(p->val) != IS_UNDEF)) {
1389
47.9M
              ZVAL_COPY_VALUE(&q->val, &p->val);
1390
47.9M
              q->h = p->h;
1391
47.9M
              nIndex = q->h | ht->nTableMask;
1392
47.9M
              q->key = p->key;
1393
47.9M
              Z_NEXT(q->val) = HT_HASH(ht, nIndex);
1394
47.9M
              HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(j);
1395
47.9M
              if (UNEXPECTED(ht->nInternalPointer > j && ht->nInternalPointer <= i)) {
1396
1
                ht->nInternalPointer = j;
1397
1
              }
1398
47.9M
              q++;
1399
47.9M
              j++;
1400
47.9M
            }
1401
243M
          }
1402
11.7k
        } else {
1403
14
          uint32_t iter_pos = zend_hash_iterators_lower_pos(ht, i + 1);
1404
1405
124
          while (++i < ht->nNumUsed) {
1406
110
            p++;
1407
110
            if (EXPECTED(Z_TYPE_INFO(p->val) != IS_UNDEF)) {
1408
39
              ZVAL_COPY_VALUE(&q->val, &p->val);
1409
39
              q->h = p->h;
1410
39
              nIndex = q->h | ht->nTableMask;
1411
39
              q->key = p->key;
1412
39
              Z_NEXT(q->val) = HT_HASH(ht, nIndex);
1413
39
              HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(j);
1414
39
              if (UNEXPECTED(ht->nInternalPointer > j && ht->nInternalPointer <= i)) {
1415
1
                ht->nInternalPointer = j;
1416
1
              }
1417
39
              if (UNEXPECTED(i >= iter_pos)) {
1418
14
                do {
1419
14
                  zend_hash_iterators_update(ht, iter_pos, j);
1420
14
                  iter_pos = zend_hash_iterators_lower_pos(ht, iter_pos + 1);
1421
14
                } while (iter_pos < i);
1422
14
              }
1423
39
              q++;
1424
39
              j++;
1425
39
            }
1426
110
          }
1427
14
        }
1428
11.7k
        ht->nNumUsed = j;
1429
11.7k
        break;
1430
11.7k
      }
1431
359k
      nIndex = p->h | ht->nTableMask;
1432
359k
      Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1433
359k
      HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(i);
1434
359k
      p++;
1435
359k
    } while (++i < ht->nNumUsed);
1436
1437
    /* Migrate pointer to one past the end of the array to the new one past the end, so that
1438
     * newly inserted elements are picked up correctly. */
1439
11.7k
    if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
1440
14
      _zend_hash_iterators_update(ht, old_num_used, ht->nNumUsed);
1441
14
    }
1442
11.7k
  }
1443
22.6k
}
1444
1445
static zend_always_inline void zend_hash_iterators_clamp_max(const HashTable *ht, uint32_t max)
1446
17.4M
{
1447
17.4M
  if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
1448
416
    HashTableIterator *iter = EG(ht_iterators);
1449
416
    const HashTableIterator *end = iter + EG(ht_iterators_used);
1450
842
    while (iter != end) {
1451
426
      if (iter->ht == ht) {
1452
416
        iter->pos = MIN(iter->pos, max);
1453
416
      }
1454
426
      iter++;
1455
426
    }
1456
416
  }
1457
17.4M
}
1458
1459
static zend_always_inline void _zend_hash_packed_del_val(HashTable *ht, uint32_t idx, zval *zv)
1460
1.70k
{
1461
1.70k
  idx = HT_HASH_TO_IDX(idx);
1462
1.70k
  ht->nNumOfElements--;
1463
1.70k
  if (ht->nNumUsed - 1 == idx) {
1464
4.08k
    do {
1465
4.08k
      ht->nNumUsed--;
1466
4.08k
    } while (ht->nNumUsed > 0 && (UNEXPECTED(Z_TYPE(ht->arPacked[ht->nNumUsed-1]) == IS_UNDEF)));
1467
1.27k
    ht->nInternalPointer = MIN(ht->nInternalPointer, ht->nNumUsed);
1468
1.27k
    zend_hash_iterators_clamp_max(ht, ht->nNumUsed);
1469
1.27k
  }
1470
1.70k
  if (ht->pDestructor) {
1471
1.70k
    zval tmp;
1472
1.70k
    ZVAL_COPY_VALUE(&tmp, zv);
1473
1.70k
    ZVAL_UNDEF(zv);
1474
1.70k
    ht->pDestructor(&tmp);
1475
1.70k
  } else {
1476
0
    ZVAL_UNDEF(zv);
1477
0
  }
1478
1.70k
}
1479
1480
static zend_always_inline void _zend_hash_del_el_ex(HashTable *ht, uint32_t idx, Bucket *p, Bucket *prev)
1481
477M
{
1482
477M
  if (prev) {
1483
23.1M
    Z_NEXT(prev->val) = Z_NEXT(p->val);
1484
454M
  } else {
1485
454M
    HT_HASH(ht, p->h | ht->nTableMask) = Z_NEXT(p->val);
1486
454M
  }
1487
477M
  idx = HT_HASH_TO_IDX(idx);
1488
477M
  ht->nNumOfElements--;
1489
477M
  if (ht->nNumUsed - 1 == idx) {
1490
112M
    do {
1491
112M
      ht->nNumUsed--;
1492
112M
    } while (ht->nNumUsed > 0 && (UNEXPECTED(Z_TYPE(ht->arData[ht->nNumUsed-1].val) == IS_UNDEF)));
1493
17.4M
    ht->nInternalPointer = MIN(ht->nInternalPointer, ht->nNumUsed);
1494
17.4M
    zend_hash_iterators_clamp_max(ht, ht->nNumUsed);
1495
17.4M
  }
1496
477M
  if (ht->pDestructor) {
1497
341k
    zval tmp;
1498
341k
    ZVAL_COPY_VALUE(&tmp, &p->val);
1499
341k
    ZVAL_UNDEF(&p->val);
1500
341k
    ht->pDestructor(&tmp);
1501
477M
  } else {
1502
477M
    ZVAL_UNDEF(&p->val);
1503
477M
  }
1504
477M
}
1505
1506
static zend_always_inline void _zend_hash_del_el(HashTable *ht, uint32_t idx, Bucket *p)
1507
477M
{
1508
477M
  Bucket *prev = NULL;
1509
477M
  uint32_t nIndex;
1510
477M
  uint32_t i;
1511
1512
477M
  nIndex = p->h | ht->nTableMask;
1513
477M
  i = HT_HASH(ht, nIndex);
1514
1515
477M
  if (i != idx) {
1516
23.1M
    prev = HT_HASH_TO_BUCKET(ht, i);
1517
24.8M
    while (Z_NEXT(prev->val) != idx) {
1518
1.67M
      i = Z_NEXT(prev->val);
1519
1.67M
      prev = HT_HASH_TO_BUCKET(ht, i);
1520
1.67M
    }
1521
23.1M
  }
1522
1523
477M
  if (p->key) {
1524
335k
    zend_string_release(p->key);
1525
335k
    p->key = NULL;
1526
335k
  }
1527
477M
  _zend_hash_del_el_ex(ht, idx, p, prev);
1528
477M
}
1529
1530
ZEND_API void ZEND_FASTCALL zend_hash_packed_del_val(HashTable *ht, zval *zv)
1531
300
{
1532
300
  IS_CONSISTENT(ht);
1533
300
  HT_ASSERT_RC1(ht);
1534
300
  ZEND_ASSERT(HT_IS_PACKED(ht));
1535
300
  _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(zv - ht->arPacked), zv);
1536
300
}
1537
1538
1539
ZEND_API void ZEND_FASTCALL zend_hash_del_bucket(HashTable *ht, Bucket *p)
1540
477M
{
1541
477M
  IS_CONSISTENT(ht);
1542
477M
  HT_ASSERT_RC1(ht);
1543
477M
  ZEND_ASSERT(!HT_IS_PACKED(ht));
1544
477M
  _zend_hash_del_el(ht, HT_IDX_TO_HASH(p - ht->arData), p);
1545
477M
}
1546
1547
ZEND_API zend_result ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key)
1548
33.5k
{
1549
33.5k
  zend_ulong h;
1550
33.5k
  uint32_t nIndex;
1551
33.5k
  uint32_t idx;
1552
33.5k
  Bucket *p;
1553
33.5k
  Bucket *prev = NULL;
1554
1555
33.5k
  IS_CONSISTENT(ht);
1556
33.5k
  HT_ASSERT_RC1(ht);
1557
1558
33.5k
  h = zend_string_hash_val(key);
1559
33.5k
  nIndex = h | ht->nTableMask;
1560
1561
33.5k
  idx = HT_HASH(ht, nIndex);
1562
34.0k
  while (idx != HT_INVALID_IDX) {
1563
25.5k
    p = HT_HASH_TO_BUCKET(ht, idx);
1564
25.5k
    if ((p->key == key) ||
1565
524
      (p->h == h &&
1566
21
         p->key &&
1567
25.0k
         zend_string_equal_content(p->key, key))) {
1568
25.0k
      zend_string_release(p->key);
1569
25.0k
      p->key = NULL;
1570
25.0k
      _zend_hash_del_el_ex(ht, idx, p, prev);
1571
25.0k
      return SUCCESS;
1572
25.0k
    }
1573
507
    prev = p;
1574
507
    idx = Z_NEXT(p->val);
1575
507
  }
1576
8.49k
  return FAILURE;
1577
33.5k
}
1578
1579
ZEND_API zend_result ZEND_FASTCALL zend_hash_del_ind(HashTable *ht, zend_string *key)
1580
68
{
1581
68
  zend_ulong h;
1582
68
  uint32_t nIndex;
1583
68
  uint32_t idx;
1584
68
  Bucket *p;
1585
68
  Bucket *prev = NULL;
1586
1587
68
  IS_CONSISTENT(ht);
1588
68
  HT_ASSERT_RC1(ht);
1589
1590
68
  h = zend_string_hash_val(key);
1591
68
  nIndex = h | ht->nTableMask;
1592
1593
68
  idx = HT_HASH(ht, nIndex);
1594
68
  while (idx != HT_INVALID_IDX) {
1595
46
    p = HT_HASH_TO_BUCKET(ht, idx);
1596
46
    if ((p->key == key) ||
1597
0
      (p->h == h &&
1598
0
         p->key &&
1599
46
         zend_string_equal_content(p->key, key))) {
1600
46
      if (Z_TYPE(p->val) == IS_INDIRECT) {
1601
20
        zval *data = Z_INDIRECT(p->val);
1602
1603
20
        if (UNEXPECTED(Z_TYPE_P(data) == IS_UNDEF)) {
1604
0
          return FAILURE;
1605
20
        } else {
1606
20
          if (ht->pDestructor) {
1607
20
            zval tmp;
1608
20
            ZVAL_COPY_VALUE(&tmp, data);
1609
20
            ZVAL_UNDEF(data);
1610
20
            ht->pDestructor(&tmp);
1611
20
          } else {
1612
0
            ZVAL_UNDEF(data);
1613
0
          }
1614
20
          HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
1615
20
        }
1616
26
      } else {
1617
26
        zend_string_release(p->key);
1618
26
        p->key = NULL;
1619
26
        _zend_hash_del_el_ex(ht, idx, p, prev);
1620
26
      }
1621
46
      return SUCCESS;
1622
46
    }
1623
0
    prev = p;
1624
0
    idx = Z_NEXT(p->val);
1625
0
  }
1626
22
  return FAILURE;
1627
68
}
1628
1629
ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del_ind(HashTable *ht, const char *str, size_t len)
1630
0
{
1631
0
  zend_ulong h;
1632
0
  uint32_t nIndex;
1633
0
  uint32_t idx;
1634
0
  Bucket *p;
1635
0
  Bucket *prev = NULL;
1636
1637
0
  IS_CONSISTENT(ht);
1638
0
  HT_ASSERT_RC1(ht);
1639
1640
0
  h = zend_inline_hash_func(str, len);
1641
0
  nIndex = h | ht->nTableMask;
1642
1643
0
  idx = HT_HASH(ht, nIndex);
1644
0
  while (idx != HT_INVALID_IDX) {
1645
0
    p = HT_HASH_TO_BUCKET(ht, idx);
1646
0
    if ((p->h == h)
1647
0
       && p->key
1648
0
       && zend_string_equals_cstr(p->key, str, len)) {
1649
0
      if (Z_TYPE(p->val) == IS_INDIRECT) {
1650
0
        zval *data = Z_INDIRECT(p->val);
1651
1652
0
        if (UNEXPECTED(Z_TYPE_P(data) == IS_UNDEF)) {
1653
0
          return FAILURE;
1654
0
        } else {
1655
0
          if (ht->pDestructor) {
1656
0
            ht->pDestructor(data);
1657
0
          }
1658
0
          ZVAL_UNDEF(data);
1659
0
          HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
1660
0
        }
1661
0
      } else {
1662
0
        zend_string_release(p->key);
1663
0
        p->key = NULL;
1664
0
        _zend_hash_del_el_ex(ht, idx, p, prev);
1665
0
      }
1666
0
      return SUCCESS;
1667
0
    }
1668
0
    prev = p;
1669
0
    idx = Z_NEXT(p->val);
1670
0
  }
1671
0
  return FAILURE;
1672
0
}
1673
1674
ZEND_API zend_result ZEND_FASTCALL zend_hash_str_del(HashTable *ht, const char *str, size_t len)
1675
76
{
1676
76
  zend_ulong h;
1677
76
  uint32_t nIndex;
1678
76
  uint32_t idx;
1679
76
  Bucket *p;
1680
76
  Bucket *prev = NULL;
1681
1682
76
  IS_CONSISTENT(ht);
1683
76
  HT_ASSERT_RC1(ht);
1684
1685
76
  h = zend_inline_hash_func(str, len);
1686
76
  nIndex = h | ht->nTableMask;
1687
1688
76
  idx = HT_HASH(ht, nIndex);
1689
94
  while (idx != HT_INVALID_IDX) {
1690
86
    p = HT_HASH_TO_BUCKET(ht, idx);
1691
86
    if ((p->h == h)
1692
68
       && p->key
1693
68
       && zend_string_equals_cstr(p->key, str, len)) {
1694
68
      zend_string_release(p->key);
1695
68
      p->key = NULL;
1696
68
      _zend_hash_del_el_ex(ht, idx, p, prev);
1697
68
      return SUCCESS;
1698
68
    }
1699
18
    prev = p;
1700
18
    idx = Z_NEXT(p->val);
1701
18
  }
1702
8
  return FAILURE;
1703
76
}
1704
1705
ZEND_API zend_result ZEND_FASTCALL zend_hash_index_del(HashTable *ht, zend_ulong h)
1706
3.26k
{
1707
3.26k
  uint32_t nIndex;
1708
3.26k
  uint32_t idx;
1709
3.26k
  Bucket *p;
1710
3.26k
  Bucket *prev = NULL;
1711
1712
3.26k
  IS_CONSISTENT(ht);
1713
3.26k
  HT_ASSERT_RC1(ht);
1714
1715
3.26k
  if (HT_IS_PACKED(ht)) {
1716
1.38k
    if (h < ht->nNumUsed) {
1717
1.35k
      zval *zv = ht->arPacked + h;
1718
1.35k
      if (Z_TYPE_P(zv) != IS_UNDEF) {
1719
1.34k
        _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(h), zv);
1720
1.34k
        return SUCCESS;
1721
1.34k
      }
1722
1.35k
    }
1723
45
    return FAILURE;
1724
1.38k
  }
1725
1.87k
  nIndex = h | ht->nTableMask;
1726
1727
1.87k
  idx = HT_HASH(ht, nIndex);
1728
2.69k
  while (idx != HT_INVALID_IDX) {
1729
2.19k
    p = HT_HASH_TO_BUCKET(ht, idx);
1730
2.19k
    if ((p->h == h) && (p->key == NULL)) {
1731
1.37k
      _zend_hash_del_el_ex(ht, idx, p, prev);
1732
1.37k
      return SUCCESS;
1733
1.37k
    }
1734
822
    prev = p;
1735
822
    idx = Z_NEXT(p->val);
1736
822
  }
1737
504
  return FAILURE;
1738
1.87k
}
1739
1740
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
1741
423k
{
1742
423k
  IS_CONSISTENT(ht);
1743
423k
  HT_ASSERT(ht, GC_REFCOUNT(ht) <= 1);
1744
1745
423k
  if (ht->nNumUsed) {
1746
96.5k
    if (HT_IS_PACKED(ht)) {
1747
2.25k
      if (ht->pDestructor) {
1748
2.20k
        zval *zv = ht->arPacked;
1749
2.20k
        zval *end = zv + ht->nNumUsed;
1750
1751
2.20k
        SET_INCONSISTENT(HT_IS_DESTROYING);
1752
2.20k
        if (HT_IS_WITHOUT_HOLES(ht)) {
1753
2.40k
          do {
1754
2.40k
            ht->pDestructor(zv);
1755
2.40k
          } while (++zv != end);
1756
1.83k
        } else {
1757
794
          do {
1758
794
            if (EXPECTED(Z_TYPE_P(zv) != IS_UNDEF)) {
1759
394
              ht->pDestructor(zv);
1760
394
            }
1761
794
          } while (++zv != end);
1762
362
        }
1763
2.20k
        SET_INCONSISTENT(HT_DESTROYED);
1764
2.20k
      }
1765
2.25k
      zend_hash_iterators_remove(ht);
1766
94.3k
    } else {
1767
94.3k
      Bucket *p = ht->arData;
1768
94.3k
      Bucket *end = p + ht->nNumUsed;
1769
1770
94.3k
      if (ht->pDestructor) {
1771
5.99k
        SET_INCONSISTENT(HT_IS_DESTROYING);
1772
1773
5.99k
        if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
1774
2.97k
          if (HT_IS_WITHOUT_HOLES(ht)) {
1775
9.44k
            do {
1776
9.44k
              ht->pDestructor(&p->val);
1777
9.44k
            } while (++p != end);
1778
2.97k
          } else {
1779
0
            do {
1780
0
              if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1781
0
                ht->pDestructor(&p->val);
1782
0
              }
1783
0
            } while (++p != end);
1784
0
          }
1785
3.01k
        } else if (HT_IS_WITHOUT_HOLES(ht)) {
1786
4.28k
          do {
1787
4.28k
            ht->pDestructor(&p->val);
1788
4.28k
            if (EXPECTED(p->key)) {
1789
4.17k
              zend_string_release(p->key);
1790
4.17k
            }
1791
4.28k
          } while (++p != end);
1792
3.01k
        } else {
1793
0
          do {
1794
0
            if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1795
0
              ht->pDestructor(&p->val);
1796
0
              if (EXPECTED(p->key)) {
1797
0
                zend_string_release(p->key);
1798
0
              }
1799
0
            }
1800
0
          } while (++p != end);
1801
0
        }
1802
1803
5.99k
        SET_INCONSISTENT(HT_DESTROYED);
1804
88.3k
      } else {
1805
88.3k
        if (!HT_HAS_STATIC_KEYS_ONLY(ht)) {
1806
86.5k
          do {
1807
86.5k
            if (EXPECTED(p->key)) {
1808
86.5k
              zend_string_release(p->key);
1809
86.5k
            }
1810
86.5k
          } while (++p != end);
1811
10.1k
        }
1812
88.3k
      }
1813
94.3k
      zend_hash_iterators_remove(ht);
1814
94.3k
    }
1815
326k
  } else if (EXPECTED(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
1816
264k
    return;
1817
264k
  }
1818
158k
  pefree(HT_GET_DATA_ADDR(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
1819
158k
}
1820
1821
ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht)
1822
2.98M
{
1823
2.98M
  IS_CONSISTENT(ht);
1824
2.98M
  HT_ASSERT(ht, GC_REFCOUNT(ht) <= 1);
1825
1826
  /* break possible cycles */
1827
2.98M
  GC_REMOVE_FROM_BUFFER(ht);
1828
2.98M
  GC_TYPE_INFO(ht) = GC_NULL /*???| (GC_WHITE << 16)*/;
1829
1830
2.98M
  if (ht->nNumUsed) {
1831
    /* In some rare cases destructors of regular arrays may be changed */
1832
2.55M
    if (UNEXPECTED(ht->pDestructor != ZVAL_PTR_DTOR)) {
1833
38
      zend_hash_destroy(ht);
1834
38
      goto free_ht;
1835
38
    }
1836
1837
2.55M
    SET_INCONSISTENT(HT_IS_DESTROYING);
1838
1839
2.55M
    if (HT_IS_PACKED(ht)) {
1840
1.35M
      zval *zv = ht->arPacked;
1841
1.35M
      zval *end = zv + ht->nNumUsed;
1842
1843
3.19M
      do {
1844
3.19M
        i_zval_ptr_dtor(zv);
1845
3.19M
      } while (++zv != end);
1846
1.35M
    } else {
1847
1.19M
      Bucket *p = ht->arData;
1848
1.19M
      Bucket *end = p + ht->nNumUsed;
1849
1850
1.19M
      if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
1851
5.09M
        do {
1852
5.09M
          i_zval_ptr_dtor(&p->val);
1853
5.09M
        } while (++p != end);
1854
1.17M
      } else if (HT_IS_WITHOUT_HOLES(ht)) {
1855
27.3k
        do {
1856
27.3k
          i_zval_ptr_dtor(&p->val);
1857
27.3k
          if (EXPECTED(p->key)) {
1858
26.9k
            zend_string_release_ex(p->key, 0);
1859
26.9k
          }
1860
27.3k
        } while (++p != end);
1861
17.0k
      } else {
1862
4
        do {
1863
4
          if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1864
2
            i_zval_ptr_dtor(&p->val);
1865
2
            if (EXPECTED(p->key)) {
1866
2
              zend_string_release_ex(p->key, 0);
1867
2
            }
1868
2
          }
1869
4
        } while (++p != end);
1870
2
      }
1871
1.19M
    }
1872
2.55M
  } else if (EXPECTED(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
1873
183k
    goto free_ht;
1874
183k
  }
1875
2.80M
  SET_INCONSISTENT(HT_DESTROYED);
1876
2.80M
  efree(HT_GET_DATA_ADDR(ht));
1877
2.98M
free_ht:
1878
2.98M
  zend_hash_iterators_remove(ht);
1879
2.98M
  FREE_HASHTABLE(ht);
1880
2.98M
}
1881
1882
ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht)
1883
202k
{
1884
202k
  IS_CONSISTENT(ht);
1885
202k
  HT_ASSERT_RC1(ht);
1886
1887
202k
  if (ht->nNumUsed) {
1888
105k
    if (HT_IS_PACKED(ht)) {
1889
1.18k
      zval *zv = ht->arPacked;
1890
1.18k
      zval *end = zv + ht->nNumUsed;
1891
1892
1.18k
      if (ht->pDestructor) {
1893
0
        if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
1894
0
          if (HT_IS_WITHOUT_HOLES(ht)) {
1895
0
            do {
1896
0
              ht->pDestructor(zv);
1897
0
            } while (++zv != end);
1898
0
          } else {
1899
0
            do {
1900
0
              if (EXPECTED(Z_TYPE_P(zv) != IS_UNDEF)) {
1901
0
                ht->pDestructor(zv);
1902
0
              }
1903
0
            } while (++zv != end);
1904
0
          }
1905
0
        }
1906
0
      }
1907
104k
    } else {
1908
104k
      Bucket *p = ht->arData;
1909
104k
      Bucket *end = p + ht->nNumUsed;
1910
1911
104k
      if (ht->pDestructor) {
1912
180
        if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
1913
180
          if (HT_IS_WITHOUT_HOLES(ht)) {
1914
344
            do {
1915
344
              ht->pDestructor(&p->val);
1916
344
            } while (++p != end);
1917
170
          } else {
1918
42
            do {
1919
42
              if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1920
28
                ht->pDestructor(&p->val);
1921
28
              }
1922
42
            } while (++p != end);
1923
10
          }
1924
180
        } else if (HT_IS_WITHOUT_HOLES(ht)) {
1925
0
          do {
1926
0
            ht->pDestructor(&p->val);
1927
0
            if (EXPECTED(p->key)) {
1928
0
              zend_string_release(p->key);
1929
0
            }
1930
0
          } while (++p != end);
1931
0
        } else {
1932
0
          do {
1933
0
            if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1934
0
              ht->pDestructor(&p->val);
1935
0
              if (EXPECTED(p->key)) {
1936
0
                zend_string_release(p->key);
1937
0
              }
1938
0
            }
1939
0
          } while (++p != end);
1940
0
        }
1941
104k
      } else {
1942
104k
        if (!HT_HAS_STATIC_KEYS_ONLY(ht)) {
1943
331k
          do {
1944
331k
            if (EXPECTED(p->key)) {
1945
280k
              zend_string_release(p->key);
1946
280k
            }
1947
331k
          } while (++p != end);
1948
46.4k
        }
1949
104k
      }
1950
104k
      HT_HASH_RESET(ht);
1951
104k
    }
1952
105k
  }
1953
202k
  ht->nNumUsed = 0;
1954
202k
  ht->nNumOfElements = 0;
1955
202k
  ht->nNextFreeElement = ZEND_LONG_MIN;
1956
202k
  ht->nInternalPointer = 0;
1957
202k
}
1958
1959
ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht)
1960
1.22k
{
1961
1.22k
  Bucket *p, *end;
1962
1963
1.22k
  IS_CONSISTENT(ht);
1964
1.22k
  HT_ASSERT_RC1(ht);
1965
1966
1.22k
  if (ht->nNumUsed) {
1967
1.10k
    ZEND_ASSERT(!HT_IS_PACKED(ht));
1968
1.10k
    p = ht->arData;
1969
1.10k
    end = p + ht->nNumUsed;
1970
1.10k
    if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
1971
5.92k
      do {
1972
5.92k
        i_zval_ptr_dtor(&p->val);
1973
5.92k
      } while (++p != end);
1974
959
    } else if (HT_IS_WITHOUT_HOLES(ht)) {
1975
466
      do {
1976
466
        i_zval_ptr_dtor(&p->val);
1977
466
        if (EXPECTED(p->key)) {
1978
466
          zend_string_release(p->key);
1979
466
        }
1980
466
      } while (++p != end);
1981
146
    } else {
1982
0
      do {
1983
0
        if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1984
0
          i_zval_ptr_dtor(&p->val);
1985
0
          if (EXPECTED(p->key)) {
1986
0
            zend_string_release(p->key);
1987
0
          }
1988
0
        }
1989
0
      } while (++p != end);
1990
0
    }
1991
1.10k
    HT_HASH_RESET(ht);
1992
1.10k
  }
1993
1.22k
  ht->nNumUsed = 0;
1994
694
  ht->nNumOfElements = 0;
1995
694
  ht->nNextFreeElement = ZEND_LONG_MIN;
1996
694
  ht->nInternalPointer = 0;
1997
694
}
1998
1999
ZEND_API void ZEND_FASTCALL zend_hash_graceful_destroy(HashTable *ht)
2000
0
{
2001
0
  uint32_t idx;
2002
2003
0
  IS_CONSISTENT(ht);
2004
0
  HT_ASSERT_RC1(ht);
2005
2006
0
  if (HT_IS_PACKED(ht)) {
2007
0
    zval *zv = ht->arPacked;
2008
2009
0
    for (idx = 0; idx < ht->nNumUsed; idx++, zv++) {
2010
0
      if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2011
0
      _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2012
0
    }
2013
0
  } else {
2014
0
    Bucket *p = ht->arData;
2015
2016
0
    for (idx = 0; idx < ht->nNumUsed; idx++, p++) {
2017
0
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2018
0
      _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2019
0
    }
2020
0
  }
2021
0
  if (!(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
2022
0
    pefree(HT_GET_DATA_ADDR(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
2023
0
  }
2024
2025
0
  SET_INCONSISTENT(HT_DESTROYED);
2026
0
}
2027
2028
ZEND_API void ZEND_FASTCALL zend_hash_graceful_reverse_destroy(HashTable *ht)
2029
77.2k
{
2030
77.2k
  uint32_t idx;
2031
2032
77.2k
  IS_CONSISTENT(ht);
2033
77.2k
  HT_ASSERT_RC1(ht);
2034
2035
77.2k
  idx = ht->nNumUsed;
2036
77.2k
  if (HT_IS_PACKED(ht)) {
2037
84
    zval *zv = ht->arPacked + ht->nNumUsed;
2038
2039
199
    while (idx > 0) {
2040
115
      idx--;
2041
115
      zv--;
2042
115
      if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2043
61
      _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2044
61
    }
2045
77.1k
  } else {
2046
77.1k
    Bucket *p = ht->arData + ht->nNumUsed;
2047
2048
386k
    while (idx > 0) {
2049
309k
      idx--;
2050
309k
      p--;
2051
309k
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2052
299k
      _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2053
299k
    }
2054
77.1k
  }
2055
2056
77.2k
  if (!(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
2057
38.7k
    pefree(HT_GET_DATA_ADDR(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
2058
38.7k
  }
2059
2060
77.2k
  SET_INCONSISTENT(HT_DESTROYED);
2061
77.2k
}
2062
2063
/* This is used to recurse elements and selectively delete certain entries
2064
 * from a hashtable. apply_func() receives the data and decides if the entry
2065
 * should be deleted or recursion should be stopped. The following three
2066
 * return codes are possible:
2067
 * ZEND_HASH_APPLY_KEEP   - continue
2068
 * ZEND_HASH_APPLY_STOP   - stop iteration
2069
 * ZEND_HASH_APPLY_REMOVE - delete the element, combinable with the former
2070
 */
2071
2072
ZEND_API void ZEND_FASTCALL zend_hash_apply(HashTable *ht, apply_func_t apply_func)
2073
110
{
2074
110
  uint32_t idx;
2075
110
  int result;
2076
2077
110
  IS_CONSISTENT(ht);
2078
110
  if (HT_IS_PACKED(ht)) {
2079
626
    for (idx = 0; idx < ht->nNumUsed; idx++) {
2080
518
      zval *zv = ht->arPacked + idx;
2081
2082
518
      if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2083
518
      result = apply_func(zv);
2084
2085
518
      if (result & ZEND_HASH_APPLY_REMOVE) {
2086
0
        HT_ASSERT_RC1(ht);
2087
0
        _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2088
0
      }
2089
518
      if (result & ZEND_HASH_APPLY_STOP) {
2090
0
        break;
2091
0
      }
2092
518
    }
2093
108
  } else {
2094
28
    for (idx = 0; idx < ht->nNumUsed; idx++) {
2095
26
      Bucket *p = ht->arData + idx;
2096
2097
26
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2098
26
      result = apply_func(&p->val);
2099
2100
26
      if (result & ZEND_HASH_APPLY_REMOVE) {
2101
0
        HT_ASSERT_RC1(ht);
2102
0
        _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2103
0
      }
2104
26
      if (result & ZEND_HASH_APPLY_STOP) {
2105
0
        break;
2106
0
      }
2107
26
    }
2108
2
  }
2109
110
}
2110
2111
2112
ZEND_API void ZEND_FASTCALL zend_hash_apply_with_argument(HashTable *ht, apply_func_arg_t apply_func, void *argument)
2113
0
{
2114
0
  uint32_t idx;
2115
0
  int result;
2116
2117
0
  IS_CONSISTENT(ht);
2118
0
  if (HT_IS_PACKED(ht)) {
2119
0
    for (idx = 0; idx < ht->nNumUsed; idx++) {
2120
0
      zval *zv = ht->arPacked + idx;
2121
0
      if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2122
0
      result = apply_func(zv, argument);
2123
2124
0
      if (result & ZEND_HASH_APPLY_REMOVE) {
2125
0
        HT_ASSERT_RC1(ht);
2126
0
        _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2127
0
      }
2128
0
      if (result & ZEND_HASH_APPLY_STOP) {
2129
0
        break;
2130
0
      }
2131
0
    }
2132
0
  } else {
2133
0
    for (idx = 0; idx < ht->nNumUsed; idx++) {
2134
0
      Bucket *p = ht->arData + idx;
2135
0
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2136
0
      result = apply_func(&p->val, argument);
2137
2138
0
      if (result & ZEND_HASH_APPLY_REMOVE) {
2139
0
        HT_ASSERT_RC1(ht);
2140
0
        _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2141
0
      }
2142
0
      if (result & ZEND_HASH_APPLY_STOP) {
2143
0
        break;
2144
0
      }
2145
0
    }
2146
0
  }
2147
0
}
2148
2149
2150
ZEND_API void zend_hash_apply_with_arguments(HashTable *ht, apply_func_args_t apply_func, int num_args, ...)
2151
0
{
2152
0
  uint32_t idx;
2153
0
  va_list args;
2154
0
  zend_hash_key hash_key;
2155
0
  int result;
2156
2157
0
  IS_CONSISTENT(ht);
2158
2159
0
  if (HT_IS_PACKED(ht)) {
2160
0
    for (idx = 0; idx < ht->nNumUsed; idx++) {
2161
0
      zval *zv = ht->arPacked + idx;
2162
2163
0
      if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2164
0
      va_start(args, num_args);
2165
0
      hash_key.h = idx;
2166
0
      hash_key.key = NULL;
2167
2168
0
      result = apply_func(zv, num_args, args, &hash_key);
2169
2170
0
      if (result & ZEND_HASH_APPLY_REMOVE) {
2171
0
        HT_ASSERT_RC1(ht);
2172
0
        _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2173
0
      }
2174
0
      if (result & ZEND_HASH_APPLY_STOP) {
2175
0
        va_end(args);
2176
0
        break;
2177
0
      }
2178
0
      va_end(args);
2179
0
    }
2180
0
  } else {
2181
0
    for (idx = 0; idx < ht->nNumUsed; idx++) {
2182
0
      Bucket *p = ht->arData + idx;
2183
2184
0
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2185
0
      va_start(args, num_args);
2186
0
      hash_key.h = p->h;
2187
0
      hash_key.key = p->key;
2188
2189
0
      result = apply_func(&p->val, num_args, args, &hash_key);
2190
2191
0
      if (result & ZEND_HASH_APPLY_REMOVE) {
2192
0
        HT_ASSERT_RC1(ht);
2193
0
        _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2194
0
      }
2195
0
      if (result & ZEND_HASH_APPLY_STOP) {
2196
0
        va_end(args);
2197
0
        break;
2198
0
      }
2199
0
      va_end(args);
2200
0
    }
2201
0
  }
2202
0
}
2203
2204
2205
ZEND_API void ZEND_FASTCALL zend_hash_reverse_apply(HashTable *ht, apply_func_t apply_func)
2206
51.4k
{
2207
51.4k
  uint32_t idx;
2208
51.4k
  int result;
2209
2210
51.4k
  IS_CONSISTENT(ht);
2211
2212
51.4k
  idx = ht->nNumUsed;
2213
51.4k
  if (HT_IS_PACKED(ht)) {
2214
0
    zval *zv;
2215
2216
0
    while (idx > 0) {
2217
0
      idx--;
2218
0
      zv = ht->arPacked + idx;
2219
0
      if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2220
2221
0
      result = apply_func(zv);
2222
2223
0
      if (result & ZEND_HASH_APPLY_REMOVE) {
2224
0
        HT_ASSERT_RC1(ht);
2225
0
        _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2226
0
      }
2227
0
      if (result & ZEND_HASH_APPLY_STOP) {
2228
0
        break;
2229
0
      }
2230
0
    }
2231
51.4k
  } else {
2232
51.4k
    Bucket *p;
2233
2234
539k
    while (idx > 0) {
2235
487k
      idx--;
2236
487k
      p = ht->arData + idx;
2237
487k
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2238
2239
467k
      result = apply_func(&p->val);
2240
2241
467k
      if (result & ZEND_HASH_APPLY_REMOVE) {
2242
16.6k
        HT_ASSERT_RC1(ht);
2243
16.6k
        _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2244
16.6k
      }
2245
467k
      if (result & ZEND_HASH_APPLY_STOP) {
2246
28
        break;
2247
28
      }
2248
467k
    }
2249
51.4k
  }
2250
51.4k
}
2251
2252
2253
ZEND_API void ZEND_FASTCALL zend_hash_copy(HashTable *target, const HashTable *source, copy_ctor_func_t pCopyConstructor)
2254
8.07k
{
2255
8.07k
  uint32_t idx;
2256
8.07k
  zval *new_entry, *data;
2257
2258
8.07k
  IS_CONSISTENT(source);
2259
8.07k
  IS_CONSISTENT(target);
2260
8.07k
  HT_ASSERT_RC1(target);
2261
2262
8.07k
  if (HT_IS_PACKED(source)) {
2263
0
    for (idx = 0; idx < source->nNumUsed; idx++) {
2264
0
      zval *zv = source->arPacked + idx;
2265
0
      if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2266
2267
0
      new_entry = zend_hash_index_update(target, idx, zv);
2268
0
      if (pCopyConstructor) {
2269
0
        pCopyConstructor(new_entry);
2270
0
      }
2271
0
    }
2272
0
    return;
2273
0
  }
2274
2275
86.4k
  for (idx = 0; idx < source->nNumUsed; idx++) {
2276
78.4k
    Bucket *p = source->arData + idx;
2277
2278
78.4k
    if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2279
2280
    /* INDIRECT element may point to UNDEF-ined slots */
2281
78.4k
    data = &p->val;
2282
78.4k
    if (Z_TYPE_P(data) == IS_INDIRECT) {
2283
0
      data = Z_INDIRECT_P(data);
2284
0
      if (UNEXPECTED(Z_TYPE_P(data) == IS_UNDEF)) {
2285
0
        continue;
2286
0
      }
2287
0
    }
2288
78.4k
    if (p->key) {
2289
78.4k
      new_entry = zend_hash_update(target, p->key, data);
2290
78.4k
    } else {
2291
10
      new_entry = zend_hash_index_update(target, p->h, data);
2292
10
    }
2293
78.4k
    if (pCopyConstructor) {
2294
4
      pCopyConstructor(new_entry);
2295
4
    }
2296
78.4k
  }
2297
8.07k
}
2298
2299
2300
static zend_always_inline bool zend_array_dup_value(const HashTable *source, zval *data, zval *dest, bool packed, bool with_holes)
2301
16.8k
{
2302
16.8k
  if (with_holes) {
2303
22
    if (!packed && Z_TYPE_INFO_P(data) == IS_INDIRECT) {
2304
0
      data = Z_INDIRECT_P(data);
2305
0
    }
2306
22
    if (UNEXPECTED(Z_TYPE_INFO_P(data) == IS_UNDEF)) {
2307
8
      return 0;
2308
8
    }
2309
16.7k
  } else if (!packed) {
2310
    /* INDIRECT element may point to UNDEF-ined slots */
2311
11.3k
    if (Z_TYPE_INFO_P(data) == IS_INDIRECT) {
2312
6.80k
      data = Z_INDIRECT_P(data);
2313
6.80k
      if (UNEXPECTED(Z_TYPE_INFO_P(data) == IS_UNDEF)) {
2314
4.74k
        return 0;
2315
4.74k
      }
2316
6.80k
    }
2317
11.3k
  }
2318
2319
12.0k
  do {
2320
12.0k
    if (Z_OPT_REFCOUNTED_P(data)) {
2321
4.91k
      if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1 &&
2322
54
          (Z_TYPE_P(Z_REFVAL_P(data)) != IS_ARRAY ||
2323
50
            Z_ARRVAL_P(Z_REFVAL_P(data)) != source)) {
2324
50
        data = Z_REFVAL_P(data);
2325
50
        if (!Z_OPT_REFCOUNTED_P(data)) {
2326
50
          break;
2327
50
        }
2328
50
      }
2329
4.86k
      Z_ADDREF_P(data);
2330
4.86k
    }
2331
12.0k
  } while (0);
2332
12.0k
  ZVAL_COPY_VALUE(dest, data);
2333
2334
12.0k
  return 1;
2335
16.8k
}
2336
2337
static zend_always_inline bool zend_array_dup_element(const HashTable *source, HashTable *target, uint32_t idx, Bucket *p, Bucket *q, bool packed, bool static_keys, bool with_holes)
2338
11.3k
{
2339
11.3k
  if (!zend_array_dup_value(source, &p->val, &q->val, packed, with_holes)) {
2340
4.75k
    return 0;
2341
4.75k
  }
2342
2343
6.57k
  if (!packed) {
2344
6.57k
    uint32_t nIndex;
2345
2346
6.57k
    q->h = p->h;
2347
6.57k
    q->key = p->key;
2348
6.57k
    if (!static_keys && q->key) {
2349
331
      zend_string_addref(q->key);
2350
331
    }
2351
2352
6.57k
    nIndex = q->h | target->nTableMask;
2353
6.57k
    Z_NEXT(q->val) = HT_HASH(target, nIndex);
2354
6.57k
    HT_HASH(target, nIndex) = HT_IDX_TO_HASH(idx);
2355
6.57k
  }
2356
6.57k
  return 1;
2357
11.3k
}
2358
2359
// We need to duplicate iterators to be able to search through all copy-on-write copies to find the actually iterated HashTable and position back
2360
168
static void zend_array_dup_ht_iterators(const HashTable *source, HashTable *target) {
2361
168
  uint32_t iter_index = 0;
2362
168
  uint32_t end_index = EG(ht_iterators_used);
2363
2364
4.63k
  while (iter_index != end_index) {
2365
4.46k
    HashTableIterator *iter = &EG(ht_iterators)[iter_index];
2366
4.46k
    if (iter->ht == source) {
2367
168
      uint32_t copy_idx = zend_hash_iterator_add(target, iter->pos);
2368
      /* Refetch iter because the memory may be reallocated. */
2369
168
      iter = &EG(ht_iterators)[iter_index];
2370
168
      HashTableIterator *copy_iter = EG(ht_iterators) + copy_idx;
2371
168
      copy_iter->next_copy = iter->next_copy;
2372
168
      iter->next_copy = copy_idx;
2373
168
    }
2374
4.46k
    iter_index++;
2375
4.46k
  }
2376
168
}
2377
2378
static zend_always_inline void zend_array_dup_packed_elements(const HashTable *source, HashTable *target, bool with_holes)
2379
465
{
2380
465
  zval *p = source->arPacked;
2381
465
  zval *q = target->arPacked;
2382
465
  const zval *end = p + source->nNumUsed;
2383
2384
5.48k
  do {
2385
5.48k
    if (!zend_array_dup_value(source, p, q, true, with_holes)) {
2386
2
      if (with_holes) {
2387
2
        ZVAL_UNDEF(q);
2388
2
      }
2389
2
    }
2390
5.48k
    p++; q++;
2391
5.48k
  } while (p != end);
2392
2393
465
  if (UNEXPECTED(HT_HAS_ITERATORS(source))) {
2394
154
    zend_array_dup_ht_iterators(source, target);
2395
154
  }
2396
465
}
2397
2398
static zend_always_inline uint32_t zend_array_dup_elements(const HashTable *source, HashTable *target, bool static_keys, bool with_holes)
2399
1.38k
{
2400
1.38k
  uint32_t idx = 0;
2401
1.38k
  Bucket *p = source->arData;
2402
1.38k
  Bucket *q = target->arData;
2403
1.38k
  const Bucket *end = p + source->nNumUsed;
2404
2405
1.38k
  if (UNEXPECTED(HT_HAS_ITERATORS(source))) {
2406
14
    zend_array_dup_ht_iterators(source, target);
2407
14
  }
2408
2409
6.91k
  do {
2410
6.91k
    if (!zend_array_dup_element(source, target, idx, p, q, false, static_keys, with_holes)) {
2411
638
      uint32_t target_idx = idx;
2412
2413
638
      idx++; p++;
2414
638
      if (EXPECTED(!HT_HAS_ITERATORS(target))) {
2415
5.04k
        while (p != end) {
2416
4.41k
          if (zend_array_dup_element(source, target, target_idx, p, q, false, static_keys, with_holes)) {
2417
298
            if (source->nInternalPointer == idx) {
2418
0
              target->nInternalPointer = target_idx;
2419
0
            }
2420
298
            target_idx++; q++;
2421
298
          }
2422
4.41k
          idx++; p++;
2423
4.41k
        }
2424
634
      } else {
2425
4
        target->nNumUsed = source->nNumUsed;
2426
4
        uint32_t iter_pos = zend_hash_iterators_lower_pos(target, idx);
2427
2428
12
        while (p != end) {
2429
8
          if (zend_array_dup_element(source, target, target_idx, p, q, false, static_keys, with_holes)) {
2430
6
            if (source->nInternalPointer == idx) {
2431
0
              target->nInternalPointer = target_idx;
2432
0
            }
2433
6
            if (UNEXPECTED(idx >= iter_pos)) {
2434
2
              do {
2435
2
                zend_hash_iterators_update(target, iter_pos, target_idx);
2436
2
                iter_pos = zend_hash_iterators_lower_pos(target, iter_pos + 1);
2437
2
              } while (iter_pos < idx);
2438
2
            }
2439
6
            target_idx++; q++;
2440
6
          }
2441
8
          idx++; p++;
2442
8
        }
2443
4
      }
2444
638
      return target_idx;
2445
638
    }
2446
6.27k
    idx++; p++; q++;
2447
6.27k
  } while (p != end);
2448
748
  return idx;
2449
1.38k
}
2450
2451
ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(const HashTable *source)
2452
8.62k
{
2453
8.62k
  uint32_t idx;
2454
8.62k
  HashTable *target;
2455
2456
8.62k
  IS_CONSISTENT(source);
2457
2458
8.62k
  ALLOC_HASHTABLE(target);
2459
8.62k
  GC_SET_REFCOUNT(target, 1);
2460
8.62k
  GC_TYPE_INFO(target) = GC_ARRAY;
2461
2462
8.62k
  target->pDestructor = ZVAL_PTR_DTOR;
2463
2464
8.62k
  if (source->nNumOfElements == 0) {
2465
3.52k
    HT_FLAGS(target) = HASH_FLAG_UNINITIALIZED;
2466
3.52k
    target->nTableMask = HT_MIN_MASK;
2467
3.52k
    target->nNumUsed = 0;
2468
3.52k
    target->nNumOfElements = 0;
2469
3.52k
    target->nNextFreeElement = source->nNextFreeElement;
2470
3.52k
    target->nInternalPointer = 0;
2471
3.52k
    target->nTableSize = HT_MIN_SIZE;
2472
3.52k
    HT_SET_DATA_ADDR(target, &uninitialized_bucket);
2473
5.10k
  } else if (GC_FLAGS(source) & IS_ARRAY_IMMUTABLE) {
2474
3.25k
    ZEND_ASSERT(!(HT_FLAGS(source) & HASH_FLAG_HAS_EMPTY_IND));
2475
3.25k
    HT_FLAGS(target) = HT_FLAGS(source) & HASH_FLAG_MASK;
2476
3.25k
    target->nTableMask = source->nTableMask;
2477
3.25k
    target->nNumUsed = source->nNumUsed;
2478
3.25k
    target->nNumOfElements = source->nNumOfElements;
2479
3.25k
    target->nNextFreeElement = source->nNextFreeElement;
2480
3.25k
    target->nTableSize = source->nTableSize;
2481
3.25k
    if (HT_IS_PACKED(source)) {
2482
1.59k
      HT_SET_DATA_ADDR(target, emalloc(HT_PACKED_SIZE(target)));
2483
1.59k
      target->nInternalPointer = source->nInternalPointer;
2484
1.59k
      memcpy(HT_GET_DATA_ADDR(target), HT_GET_DATA_ADDR(source), HT_PACKED_USED_SIZE(source));
2485
1.66k
    } else {
2486
1.66k
      HT_SET_DATA_ADDR(target, emalloc(HT_SIZE(target)));
2487
1.66k
      target->nInternalPointer = source->nInternalPointer;
2488
1.66k
      memcpy(HT_GET_DATA_ADDR(target), HT_GET_DATA_ADDR(source), HT_USED_SIZE(source));
2489
1.66k
    }
2490
3.25k
  } else if (HT_IS_PACKED(source)) {
2491
465
    ZEND_ASSERT(!(HT_FLAGS(source) & HASH_FLAG_HAS_EMPTY_IND));
2492
465
    HT_FLAGS(target) = HT_FLAGS(source) & HASH_FLAG_MASK;
2493
465
    target->nTableMask = HT_MIN_MASK;
2494
465
    target->nNumUsed = source->nNumUsed;
2495
465
    target->nNumOfElements = source->nNumOfElements;
2496
465
    target->nNextFreeElement = source->nNextFreeElement;
2497
465
    target->nTableSize = source->nTableSize;
2498
465
    HT_SET_DATA_ADDR(target, emalloc(HT_PACKED_SIZE_EX(target->nTableSize, HT_MIN_MASK)));
2499
465
    target->nInternalPointer =
2500
465
      (source->nInternalPointer < source->nNumUsed) ?
2501
465
        source->nInternalPointer : 0;
2502
2503
465
    HT_HASH_RESET_PACKED(target);
2504
2505
465
    if (HT_IS_WITHOUT_HOLES(target)) {
2506
463
      zend_array_dup_packed_elements(source, target, false);
2507
463
    } else {
2508
2
      zend_array_dup_packed_elements(source, target, true);
2509
2
    }
2510
1.38k
  } else {
2511
    /* Indirects are removed during duplication, remove HASH_FLAG_HAS_EMPTY_IND accordingly. */
2512
1.38k
    HT_FLAGS(target) = HT_FLAGS(source) & (HASH_FLAG_MASK & ~HASH_FLAG_HAS_EMPTY_IND);
2513
1.38k
    target->nTableMask = source->nTableMask;
2514
1.38k
    target->nNextFreeElement = source->nNextFreeElement;
2515
1.38k
    target->nInternalPointer =
2516
1.38k
      (source->nInternalPointer < source->nNumUsed) ?
2517
1.38k
        source->nInternalPointer : 0;
2518
2519
1.38k
    target->nTableSize = source->nTableSize;
2520
1.38k
    HT_SET_DATA_ADDR(target, emalloc(HT_SIZE(target)));
2521
1.38k
    HT_HASH_RESET(target);
2522
2523
1.38k
    if (HT_HAS_STATIC_KEYS_ONLY(target)) {
2524
1.35k
      if (HT_IS_WITHOUT_HOLES(source)) {
2525
1.35k
        idx = zend_array_dup_elements(source, target, true, false);
2526
1.35k
      } else {
2527
4
        idx = zend_array_dup_elements(source, target, true, true);
2528
4
      }
2529
1.35k
    } else {
2530
29
      if (HT_IS_WITHOUT_HOLES(source)) {
2531
29
        idx = zend_array_dup_elements(source, target, false, false);
2532
29
      } else {
2533
0
        idx = zend_array_dup_elements(source, target, false, true);
2534
0
      }
2535
29
    }
2536
1.38k
    target->nNumUsed = idx;
2537
1.38k
    target->nNumOfElements = idx;
2538
1.38k
  }
2539
8.62k
  return target;
2540
8.62k
}
2541
2542
ZEND_API HashTable* zend_array_to_list(const HashTable *source)
2543
0
{
2544
0
  HashTable *result = _zend_new_array(zend_hash_num_elements(source));
2545
0
  zend_hash_real_init_packed(result);
2546
2547
0
  ZEND_HASH_FILL_PACKED(result) {
2548
0
    zval *entry;
2549
2550
0
    ZEND_HASH_FOREACH_VAL(source, entry) {
2551
0
      if (UNEXPECTED(Z_ISREF_P(entry) && Z_REFCOUNT_P(entry) == 1)) {
2552
0
        entry = Z_REFVAL_P(entry);
2553
0
      }
2554
0
      Z_TRY_ADDREF_P(entry);
2555
0
      ZEND_HASH_FILL_ADD(entry);
2556
0
    } ZEND_HASH_FOREACH_END();
2557
0
  } ZEND_HASH_FILL_END();
2558
2559
0
  return result;
2560
0
}
2561
2562
2563
ZEND_API void ZEND_FASTCALL zend_hash_merge(HashTable *target, const HashTable *source, copy_ctor_func_t pCopyConstructor, bool overwrite)
2564
751
{
2565
751
  uint32_t idx;
2566
751
  Bucket *p;
2567
751
  zval *t, *s;
2568
2569
751
  IS_CONSISTENT(source);
2570
751
  IS_CONSISTENT(target);
2571
751
  HT_ASSERT_RC1(target);
2572
2573
751
  if (overwrite) {
2574
0
    if (HT_IS_PACKED(source)) {
2575
0
      for (idx = 0; idx < source->nNumUsed; idx++) {
2576
0
        s = source->arPacked + idx;
2577
0
        if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2578
0
          continue;
2579
0
        }
2580
0
        t = zend_hash_index_update(target, idx, s);
2581
0
        if (pCopyConstructor) {
2582
0
          pCopyConstructor(t);
2583
0
        }
2584
0
      }
2585
0
      return;
2586
0
    }
2587
2588
0
    for (idx = 0; idx < source->nNumUsed; idx++) {
2589
0
      p = source->arData + idx;
2590
0
      s = &p->val;
2591
0
      if (UNEXPECTED(Z_TYPE_P(s) == IS_INDIRECT)) {
2592
0
        s = Z_INDIRECT_P(s);
2593
0
      }
2594
0
      if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2595
0
        continue;
2596
0
      }
2597
0
      if (p->key) {
2598
0
        t = _zend_hash_add_or_update_i(target, p->key, s, HASH_UPDATE | HASH_UPDATE_INDIRECT);
2599
0
        if (pCopyConstructor) {
2600
0
          pCopyConstructor(t);
2601
0
        }
2602
0
      } else {
2603
0
        t = zend_hash_index_update(target, p->h, s);
2604
0
        if (pCopyConstructor) {
2605
0
          pCopyConstructor(t);
2606
0
        }
2607
0
      }
2608
0
    }
2609
751
  } else {
2610
751
    if (HT_IS_PACKED(source)) {
2611
876
      for (idx = 0; idx < source->nNumUsed; idx++) {
2612
825
        s = source->arPacked + idx;
2613
825
        if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2614
28
          continue;
2615
28
        }
2616
797
        t = zend_hash_index_add(target, idx, s);
2617
797
        if (t && pCopyConstructor) {
2618
697
          pCopyConstructor(t);
2619
697
        }
2620
797
      }
2621
51
      return;
2622
51
    }
2623
2624
1.21k
    for (idx = 0; idx < source->nNumUsed; idx++) {
2625
513
      p = source->arData + idx;
2626
513
      s = &p->val;
2627
513
      if (UNEXPECTED(Z_TYPE_P(s) == IS_INDIRECT)) {
2628
0
        s = Z_INDIRECT_P(s);
2629
0
      }
2630
513
      if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2631
0
        continue;
2632
0
      }
2633
513
      if (p->key) {
2634
420
        t = _zend_hash_add_or_update_i(target, p->key, s, HASH_ADD | HASH_UPDATE_INDIRECT);
2635
420
        if (t && pCopyConstructor) {
2636
12
          pCopyConstructor(t);
2637
12
        }
2638
420
      } else {
2639
93
        t = zend_hash_index_add(target, p->h, s);
2640
93
        if (t && pCopyConstructor) {
2641
91
          pCopyConstructor(t);
2642
91
        }
2643
93
      }
2644
513
    }
2645
700
  }
2646
751
}
2647
2648
2649
static bool ZEND_FASTCALL zend_hash_replace_checker_wrapper(HashTable *target, zval *source_data, zend_ulong h, zend_string *key, void *pParam, merge_checker_func_t merge_checker_func)
2650
0
{
2651
0
  zend_hash_key hash_key;
2652
2653
0
  hash_key.h = h;
2654
0
  hash_key.key = key;
2655
0
  return merge_checker_func(target, source_data, &hash_key, pParam);
2656
0
}
2657
2658
2659
ZEND_API void ZEND_FASTCALL zend_hash_merge_ex(HashTable *target, const HashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam)
2660
0
{
2661
0
  uint32_t idx;
2662
0
  Bucket *p;
2663
0
  zval *t;
2664
2665
0
  IS_CONSISTENT(source);
2666
0
  IS_CONSISTENT(target);
2667
0
  HT_ASSERT_RC1(target);
2668
2669
0
  ZEND_ASSERT(!HT_IS_PACKED(source));
2670
0
  for (idx = 0; idx < source->nNumUsed; idx++) {
2671
0
    p = source->arData + idx;
2672
0
    if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2673
0
    if (zend_hash_replace_checker_wrapper(target, &p->val, p->h, p->key, pParam, pMergeSource)) {
2674
0
      t = zend_hash_update(target, p->key, &p->val);
2675
0
      if (pCopyConstructor) {
2676
0
        pCopyConstructor(t);
2677
0
      }
2678
0
    }
2679
0
  }
2680
0
}
2681
2682
2683
/* Returns the hash table data if found and NULL if not. */
2684
ZEND_API zval* ZEND_FASTCALL zend_hash_find(const HashTable *ht, zend_string *key)
2685
4.10M
{
2686
4.10M
  Bucket *p;
2687
2688
4.10M
  IS_CONSISTENT(ht);
2689
2690
4.10M
  (void)zend_string_hash_val(key);
2691
4.10M
  p = zend_hash_find_bucket(ht, key);
2692
4.10M
  return p ? &p->val : NULL;
2693
4.10M
}
2694
2695
ZEND_API zval* ZEND_FASTCALL zend_hash_find_known_hash(const HashTable *ht, const zend_string *key)
2696
719k
{
2697
719k
  Bucket *p;
2698
2699
719k
  IS_CONSISTENT(ht);
2700
2701
719k
  p = zend_hash_find_bucket(ht, key);
2702
719k
  return p ? &p->val : NULL;
2703
719k
}
2704
2705
ZEND_API zval* ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *str, size_t len)
2706
306k
{
2707
306k
  zend_ulong h;
2708
306k
  Bucket *p;
2709
2710
306k
  IS_CONSISTENT(ht);
2711
2712
306k
  h = zend_inline_hash_func(str, len);
2713
306k
  p = zend_hash_str_find_bucket(ht, str, len, h);
2714
306k
  return p ? &p->val : NULL;
2715
306k
}
2716
2717
ZEND_API zval* ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h)
2718
482M
{
2719
482M
  Bucket *p;
2720
2721
482M
  IS_CONSISTENT(ht);
2722
2723
482M
  if (HT_IS_PACKED(ht)) {
2724
9.29k
    if (h < ht->nNumUsed) {
2725
7.82k
      zval *zv = ht->arPacked + h;
2726
2727
7.82k
      if (Z_TYPE_P(zv) != IS_UNDEF) {
2728
7.68k
        return zv;
2729
7.68k
      }
2730
7.82k
    }
2731
1.61k
    return NULL;
2732
9.29k
  }
2733
2734
482M
  p = zend_hash_index_find_bucket(ht, h);
2735
482M
  return p ? &p->val : NULL;
2736
482M
}
2737
2738
ZEND_API zval* ZEND_FASTCALL _zend_hash_index_find(const HashTable *ht, zend_ulong h)
2739
1.10k
{
2740
1.10k
  Bucket *p;
2741
2742
1.10k
  IS_CONSISTENT(ht);
2743
1.10k
  ZEND_ASSERT(!HT_IS_PACKED(ht));
2744
2745
1.10k
  p = zend_hash_index_find_bucket(ht, h);
2746
1.10k
  return p ? &p->val : NULL;
2747
1.10k
}
2748
2749
ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_reset_ex(const HashTable *ht, HashPosition *pos)
2750
1.06k
{
2751
1.06k
  IS_CONSISTENT(ht);
2752
1.06k
  HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2753
1.06k
  *pos = _zend_hash_get_valid_pos(ht, 0);
2754
1.06k
}
2755
2756
2757
/* This function will be extremely optimized by remembering
2758
 * the end of the list
2759
 */
2760
ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_end_ex(const HashTable *ht, HashPosition *pos)
2761
16
{
2762
16
  uint32_t idx;
2763
2764
16
  IS_CONSISTENT(ht);
2765
16
  HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2766
2767
16
  idx = ht->nNumUsed;
2768
16
  if (HT_IS_PACKED(ht)) {
2769
8
    while (idx > 0) {
2770
8
      idx--;
2771
8
      if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) {
2772
8
        *pos = idx;
2773
8
        return;
2774
8
      }
2775
8
    }
2776
8
  } else {
2777
8
    while (idx > 0) {
2778
8
      idx--;
2779
8
      if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
2780
8
        *pos = idx;
2781
8
        return;
2782
8
      }
2783
8
    }
2784
8
  }
2785
0
  *pos = ht->nNumUsed;
2786
0
}
2787
2788
2789
ZEND_API zend_result ZEND_FASTCALL zend_hash_move_forward_ex(const HashTable *ht, HashPosition *pos)
2790
775
{
2791
775
  uint32_t idx;
2792
2793
775
  IS_CONSISTENT(ht);
2794
775
  HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2795
2796
775
  idx = _zend_hash_get_valid_pos(ht, *pos);
2797
775
  if (idx < ht->nNumUsed) {
2798
775
    if (HT_IS_PACKED(ht)) {
2799
263
      while (1) {
2800
263
        idx++;
2801
263
        if (idx >= ht->nNumUsed) {
2802
81
          *pos = ht->nNumUsed;
2803
81
          return SUCCESS;
2804
81
        }
2805
182
        if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) {
2806
182
          *pos = idx;
2807
182
          return SUCCESS;
2808
182
        }
2809
182
      }
2810
512
    } else {
2811
514
      while (1) {
2812
514
        idx++;
2813
514
        if (idx >= ht->nNumUsed) {
2814
262
          *pos = ht->nNumUsed;
2815
262
          return SUCCESS;
2816
262
        }
2817
252
        if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
2818
250
          *pos = idx;
2819
250
          return SUCCESS;
2820
250
        }
2821
252
      }
2822
512
    }
2823
775
  } else {
2824
0
    return FAILURE;
2825
0
  }
2826
775
}
2827
2828
ZEND_API zend_result ZEND_FASTCALL zend_hash_move_backwards_ex(const HashTable *ht, HashPosition *pos)
2829
8
{
2830
8
  uint32_t idx = *pos;
2831
2832
8
  IS_CONSISTENT(ht);
2833
8
  HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2834
2835
8
  if (idx < ht->nNumUsed) {
2836
8
    if (HT_IS_PACKED(ht)) {
2837
2
      while (idx > 0) {
2838
2
        idx--;
2839
2
        if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) {
2840
2
          *pos = idx;
2841
2
          return SUCCESS;
2842
2
        }
2843
2
      }
2844
6
    } else {
2845
6
      while (idx > 0) {
2846
2
        idx--;
2847
2
        if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
2848
2
          *pos = idx;
2849
2
          return SUCCESS;
2850
2
        }
2851
2
      }
2852
6
    }
2853
4
    *pos = ht->nNumUsed;
2854
4
    return SUCCESS;
2855
8
  } else {
2856
0
    return FAILURE;
2857
0
  }
2858
8
}
2859
2860
2861
ZEND_API zend_hash_key_type ZEND_FASTCALL zend_hash_get_current_key_ex(const HashTable *ht, zend_string **str_index, zend_ulong *num_index, const HashPosition *pos)
2862
346
{
2863
346
  uint32_t idx;
2864
346
  Bucket *p;
2865
2866
346
  IS_CONSISTENT(ht);
2867
346
  idx = _zend_hash_get_valid_pos(ht, *pos);
2868
346
  if (idx < ht->nNumUsed) {
2869
234
    if (HT_IS_PACKED(ht)) {
2870
0
      *num_index = idx;
2871
0
      return HASH_KEY_IS_LONG;
2872
0
    }
2873
234
    p = ht->arData + idx;
2874
234
    if (p->key) {
2875
182
      *str_index = p->key;
2876
182
      return HASH_KEY_IS_STRING;
2877
182
    } else {
2878
52
      *num_index = p->h;
2879
52
      return HASH_KEY_IS_LONG;
2880
52
    }
2881
234
  }
2882
112
  return HASH_KEY_NON_EXISTENT;
2883
346
}
2884
2885
ZEND_API void ZEND_FASTCALL zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, const HashPosition *pos)
2886
273
{
2887
273
  uint32_t idx;
2888
273
  Bucket *p;
2889
2890
273
  IS_CONSISTENT(ht);
2891
273
  idx = _zend_hash_get_valid_pos(ht, *pos);
2892
273
  if (idx >= ht->nNumUsed) {
2893
0
    ZVAL_NULL(key);
2894
273
  } else {
2895
273
    if (HT_IS_PACKED(ht)) {
2896
139
      ZVAL_LONG(key, idx);
2897
139
      return;
2898
139
    }
2899
134
    p = ht->arData + idx;
2900
134
    if (p->key) {
2901
92
      ZVAL_STR_COPY(key, p->key);
2902
92
    } else {
2903
42
      ZVAL_LONG(key, p->h);
2904
42
    }
2905
134
  }
2906
273
}
2907
2908
ZEND_API zend_hash_key_type ZEND_FASTCALL zend_hash_get_current_key_type_ex(const HashTable *ht, const HashPosition *pos)
2909
715
{
2910
715
  uint32_t idx;
2911
715
  Bucket *p;
2912
2913
715
  IS_CONSISTENT(ht);
2914
715
  idx = _zend_hash_get_valid_pos(ht, *pos);
2915
715
  if (idx < ht->nNumUsed) {
2916
445
    if (HT_IS_PACKED(ht)) {
2917
147
      return HASH_KEY_IS_LONG;
2918
147
    }
2919
298
    p = ht->arData + idx;
2920
298
    if (p->key) {
2921
236
      return HASH_KEY_IS_STRING;
2922
236
    } else {
2923
62
      return HASH_KEY_IS_LONG;
2924
62
    }
2925
298
  }
2926
270
  return HASH_KEY_NON_EXISTENT;
2927
715
}
2928
2929
2930
ZEND_API zval* ZEND_FASTCALL zend_hash_get_current_data_ex(const HashTable *ht, const HashPosition *pos)
2931
1.18k
{
2932
1.18k
  uint32_t idx;
2933
1.18k
  Bucket *p;
2934
2935
1.18k
  IS_CONSISTENT(ht);
2936
1.18k
  idx = _zend_hash_get_valid_pos(ht, *pos);
2937
1.18k
  if (idx < ht->nNumUsed) {
2938
1.10k
    if (HT_IS_PACKED(ht)) {
2939
215
      return &ht->arPacked[idx];
2940
215
    }
2941
890
    p = ht->arData + idx;
2942
890
    return &p->val;
2943
1.10k
  } else {
2944
76
    return NULL;
2945
76
  }
2946
1.18k
}
2947
2948
ZEND_API void zend_hash_bucket_swap(Bucket *p, Bucket *q)
2949
2.42k
{
2950
2.42k
  zval val;
2951
2.42k
  zend_ulong h;
2952
2.42k
  zend_string *key;
2953
2954
2.42k
  val = p->val;
2955
2.42k
  h = p->h;
2956
2.42k
  key = p->key;
2957
2958
2.42k
  p->val = q->val;
2959
2.42k
  p->h = q->h;
2960
2.42k
  p->key = q->key;
2961
2962
2.42k
  q->val = val;
2963
2.42k
  q->h = h;
2964
2.42k
  q->key = key;
2965
2.42k
}
2966
2967
ZEND_API void zend_hash_bucket_renum_swap(Bucket *p, Bucket *q)
2968
65.2k
{
2969
65.2k
  zval val;
2970
2971
65.2k
  val = p->val;
2972
65.2k
  p->val = q->val;
2973
65.2k
  q->val = val;
2974
65.2k
}
2975
2976
ZEND_API void zend_hash_bucket_packed_swap(Bucket *p, Bucket *q)
2977
0
{
2978
0
  zval val;
2979
0
  zend_ulong h;
2980
2981
0
  val = p->val;
2982
0
  h = p->h;
2983
2984
0
  p->val = q->val;
2985
0
  p->h = q->h;
2986
2987
0
  q->val = val;
2988
0
  q->h = h;
2989
0
}
2990
2991
static void zend_hash_sort_internal(HashTable *ht, sort_func_t sort, bucket_compare_func_t compar, bool renumber)
2992
358
{
2993
358
  Bucket *p;
2994
358
  uint32_t i, j;
2995
2996
358
  IS_CONSISTENT(ht);
2997
2998
358
  if (!(ht->nNumOfElements>1) && !(renumber && ht->nNumOfElements>0)) {
2999
    /* Doesn't require sorting */
3000
4
    return;
3001
4
  }
3002
3003
354
  if (HT_IS_PACKED(ht)) {
3004
0
    zend_hash_packed_to_hash(ht); // TODO: ???
3005
0
  }
3006
3007
354
  if (HT_IS_WITHOUT_HOLES(ht)) {
3008
    /* Store original order of elements in extra space to allow stable sorting. */
3009
23.4k
    for (i = 0; i < ht->nNumUsed; i++) {
3010
23.1k
      Z_EXTRA(ht->arData[i].val) = i;
3011
23.1k
    }
3012
354
  } else {
3013
    /* Remove holes and store original order. */
3014
0
    for (j = 0, i = 0; j < ht->nNumUsed; j++) {
3015
0
      p = ht->arData + j;
3016
0
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
3017
0
      if (i != j) {
3018
0
        ht->arData[i] = *p;
3019
0
      }
3020
0
      Z_EXTRA(ht->arData[i].val) = i;
3021
0
      i++;
3022
0
    }
3023
0
    ht->nNumUsed = i;
3024
0
  }
3025
3026
354
  if (!HT_IS_PACKED(ht)) {
3027
    /* We broke the hash collisions chains overriding Z_NEXT() by Z_EXTRA().
3028
     * Reset the hash headers table as well to avoid possible inconsistent
3029
     * access on recursive data structures.
3030
       *
3031
       * See Zend/tests/bug63882_2.phpt
3032
     */
3033
354
    HT_HASH_RESET(ht);
3034
354
  }
3035
3036
354
  sort((void *)ht->arData, ht->nNumUsed, sizeof(Bucket), (compare_func_t) compar,
3037
354
      (swap_func_t)(renumber? zend_hash_bucket_renum_swap :
3038
354
        (HT_IS_PACKED(ht) ? zend_hash_bucket_packed_swap : zend_hash_bucket_swap)));
3039
3040
354
  ht->nInternalPointer = 0;
3041
3042
354
  if (renumber) {
3043
22.1k
    for (j = 0; j < i; j++) {
3044
21.8k
      p = ht->arData + j;
3045
21.8k
      p->h = j;
3046
21.8k
      if (p->key) {
3047
77
        zend_string_release(p->key);
3048
77
        p->key = NULL;
3049
77
      }
3050
21.8k
    }
3051
3052
308
    ht->nNextFreeElement = i;
3053
308
  }
3054
354
  if (HT_IS_PACKED(ht)) {
3055
0
    if (!renumber) {
3056
0
      zend_hash_packed_to_hash(ht);
3057
0
    }
3058
354
  } else {
3059
354
    if (renumber) {
3060
308
      void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
3061
308
      Bucket *old_buckets = ht->arData;
3062
308
      zval *zv;
3063
3064
308
      new_data = pemalloc(HT_PACKED_SIZE_EX(ht->nTableSize, HT_MIN_MASK), (GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
3065
308
      HT_FLAGS(ht) |= HASH_FLAG_PACKED | HASH_FLAG_STATIC_KEYS;
3066
308
      ht->nTableMask = HT_MIN_MASK;
3067
308
      HT_SET_DATA_ADDR(ht, new_data);
3068
308
      p = old_buckets;
3069
308
      zv = ht->arPacked;
3070
31.9k
      for (i = 0; i < ht->nTableSize; i++) {
3071
31.6k
        ZVAL_COPY_VALUE(zv, &p->val);
3072
31.6k
        zv++;
3073
31.6k
        p++;
3074
31.6k
      }
3075
308
      pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
3076
308
      HT_HASH_RESET_PACKED(ht);
3077
308
    } else {
3078
46
      zend_hash_rehash(ht);
3079
46
    }
3080
354
  }
3081
354
}
3082
3083
ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, bucket_compare_func_t compar, bool renumber)
3084
10
{
3085
10
  HT_ASSERT_RC1(ht);
3086
10
  zend_hash_sort_internal(ht, sort, compar, renumber);
3087
10
}
3088
3089
ZEND_API void ZEND_FASTCALL zend_array_sort_ex(HashTable *ht, sort_func_t sort, bucket_compare_func_t compar, bool renumber)
3090
348
{
3091
348
  HT_ASSERT_RC1(ht);
3092
3093
  /* Unpack the array early to avoid RCn assertion failures. */
3094
348
  if (HT_IS_PACKED(ht)) {
3095
301
    zend_hash_packed_to_hash(ht);
3096
301
  }
3097
3098
  /* Adding a refcount prevents the array from going away. */
3099
348
  GC_ADDREF(ht);
3100
3101
348
  zend_hash_sort_internal(ht, sort, compar, renumber);
3102
3103
348
  if (UNEXPECTED(GC_DELREF(ht) == 0)) {
3104
2
    zend_array_destroy(ht);
3105
346
  } else {
3106
346
    gc_check_possible_root((zend_refcounted *)ht);
3107
346
  }
3108
348
}
3109
3110
523
static zend_always_inline int zend_hash_compare_impl(const HashTable *ht1, const HashTable *ht2, compare_func_t compar, bool ordered) {
3111
523
  uint32_t idx1, idx2;
3112
523
  zend_string *key1, *key2;
3113
523
  zend_ulong h1, h2;
3114
523
  zval *pData1, *pData2;;
3115
523
  int result;
3116
3117
523
  if (ht1->nNumOfElements != ht2->nNumOfElements) {
3118
431
    return ht1->nNumOfElements > ht2->nNumOfElements ? 1 : -1;
3119
431
  }
3120
3121
206
  for (idx1 = 0, idx2 = 0; idx1 < ht1->nNumUsed; idx1++) {
3122
160
    if (HT_IS_PACKED(ht1)) {
3123
96
      pData1 = ht1->arPacked + idx1;
3124
96
      h1 = idx1;
3125
96
      key1 = NULL;
3126
96
    } else {
3127
64
      Bucket *p = ht1->arData + idx1;
3128
64
      pData1 = &p->val;
3129
64
      h1 = p->h;
3130
64
      key1 = p->key;
3131
64
    }
3132
3133
160
    if (Z_TYPE_P(pData1) == IS_UNDEF) continue;
3134
154
    if (ordered) {
3135
40
      if (HT_IS_PACKED(ht2)) {
3136
36
        while (1) {
3137
36
          ZEND_ASSERT(idx2 != ht2->nNumUsed);
3138
36
          pData2 = ht2->arPacked + idx2;
3139
36
          h2 = idx2;
3140
36
          key2 = NULL;
3141
36
          if (Z_TYPE_P(pData2) != IS_UNDEF) break;
3142
2
          idx2++;
3143
2
        }
3144
34
      } else {
3145
6
        while (1) {
3146
6
          Bucket *p;
3147
6
          ZEND_ASSERT(idx2 != ht2->nNumUsed);
3148
6
          p = ht2->arData + idx2;
3149
6
          pData2 = &p->val;
3150
6
          h2 = p->h;
3151
6
          key2 = p->key;
3152
6
          if (Z_TYPE_P(pData2) != IS_UNDEF) break;
3153
0
          idx2++;
3154
0
        }
3155
6
      }
3156
40
      if (key1 == NULL && key2 == NULL) { /* numeric indices */
3157
36
        if (h1 != h2) {
3158
2
          return h1 > h2 ? 1 : -1;
3159
2
        }
3160
36
      } else if (key1 != NULL && key2 != NULL) { /* string indices */
3161
0
        if (ZSTR_LEN(key1) != ZSTR_LEN(key2)) {
3162
0
          return ZSTR_LEN(key1) > ZSTR_LEN(key2) ? 1 : -1;
3163
0
        }
3164
3165
0
        result = memcmp(ZSTR_VAL(key1), ZSTR_VAL(key2), ZSTR_LEN(key1));
3166
0
        if (result != 0) {
3167
0
          return result;
3168
0
        }
3169
4
      } else {
3170
        /* Mixed key types: A string key is considered as larger */
3171
4
        return key1 != NULL ? 1 : -1;
3172
4
      }
3173
34
      idx2++;
3174
114
    } else {
3175
114
      if (key1 == NULL) { /* numeric index */
3176
52
        pData2 = zend_hash_index_find(ht2, h1);
3177
52
        if (pData2 == NULL) {
3178
0
          return 1;
3179
0
        }
3180
62
      } else { /* string index */
3181
62
        pData2 = zend_hash_find(ht2, key1);
3182
62
        if (pData2 == NULL) {
3183
2
          return 1;
3184
2
        }
3185
62
      }
3186
114
    }
3187
3188
146
    if (Z_TYPE_P(pData1) == IS_INDIRECT) {
3189
20
      pData1 = Z_INDIRECT_P(pData1);
3190
20
    }
3191
146
    if (Z_TYPE_P(pData2) == IS_INDIRECT) {
3192
20
      pData2 = Z_INDIRECT_P(pData2);
3193
20
    }
3194
3195
146
    if (Z_TYPE_P(pData1) == IS_UNDEF) {
3196
8
      if (Z_TYPE_P(pData2) != IS_UNDEF) {
3197
0
        return -1;
3198
0
      }
3199
138
    } else if (Z_TYPE_P(pData2) == IS_UNDEF) {
3200
0
      return 1;
3201
138
    } else {
3202
138
      result = compar(pData1, pData2);
3203
138
      if (result != 0) {
3204
38
        return result;
3205
38
      }
3206
138
    }
3207
146
  }
3208
3209
46
  return 0;
3210
92
}
3211
3212
ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, bool ordered)
3213
529
{
3214
529
  int result;
3215
529
  IS_CONSISTENT(ht1);
3216
529
  IS_CONSISTENT(ht2);
3217
3218
529
  if (ht1 == ht2) {
3219
0
    return 0;
3220
0
  }
3221
3222
  /* It's enough to protect only one of the arrays.
3223
   * The second one may be referenced from the first and this may cause
3224
   * false recursion detection.
3225
   */
3226
529
  if (UNEXPECTED(GC_IS_RECURSIVE(ht1))) {
3227
6
    zend_throw_error(NULL, "Nesting level too deep - recursive dependency?");
3228
6
    return ZEND_UNCOMPARABLE;
3229
6
  }
3230
3231
523
  GC_TRY_PROTECT_RECURSION(ht1);
3232
523
  result = zend_hash_compare_impl(ht1, ht2, compar, ordered);
3233
523
  GC_TRY_UNPROTECT_RECURSION(ht1);
3234
3235
523
  return result;
3236
529
}
3237
3238
3239
ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag)
3240
0
{
3241
0
  uint32_t idx;
3242
0
  zval *res;
3243
3244
0
  IS_CONSISTENT(ht);
3245
3246
0
  if (ht->nNumOfElements == 0 ) {
3247
0
    return NULL;
3248
0
  }
3249
3250
0
  if (HT_IS_PACKED(ht)) {
3251
0
    zval *zv;
3252
3253
0
    idx = 0;
3254
0
    while (1) {
3255
0
      if (idx == ht->nNumUsed) {
3256
0
        return NULL;
3257
0
      }
3258
0
      if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) break;
3259
0
      idx++;
3260
0
    }
3261
0
    res = ht->arPacked + idx;
3262
0
    for (; idx < ht->nNumUsed; idx++) {
3263
0
      zv = ht->arPacked + idx;
3264
0
      if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
3265
3266
0
      if (flag) {
3267
0
        if (compar(res, zv) < 0) { /* max */
3268
0
          res = zv;
3269
0
        }
3270
0
      } else {
3271
0
        if (compar(res, zv) > 0) { /* min */
3272
0
          res = zv;
3273
0
        }
3274
0
      }
3275
0
    }
3276
0
  } else {
3277
0
    Bucket *p;
3278
3279
0
    idx = 0;
3280
0
    while (1) {
3281
0
      if (idx == ht->nNumUsed) {
3282
0
        return NULL;
3283
0
      }
3284
0
      if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) break;
3285
0
      idx++;
3286
0
    }
3287
0
    res = &ht->arData[idx].val;
3288
0
    for (; idx < ht->nNumUsed; idx++) {
3289
0
      p = ht->arData + idx;
3290
0
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
3291
3292
0
      if (flag) {
3293
0
        if (compar(res, &p->val) < 0) { /* max */
3294
0
          res = &p->val;
3295
0
        }
3296
0
      } else {
3297
0
        if (compar(res, &p->val) > 0) { /* min */
3298
0
          res = &p->val;
3299
0
        }
3300
0
      }
3301
0
    }
3302
0
  }
3303
0
  return res;
3304
0
}
3305
3306
ZEND_API bool ZEND_FASTCALL _zend_handle_numeric_str_ex(const char *key, size_t length, zend_ulong *idx)
3307
7.84k
{
3308
7.84k
  const char *tmp = key;
3309
3310
7.84k
  const char *end = key + length;
3311
3312
7.84k
  if (*tmp == '-') {
3313
98
    tmp++;
3314
98
  }
3315
3316
7.84k
  if ((*tmp == '0' && length > 1) /* numbers with leading zeros */
3317
7.50k
   || (end - tmp > MAX_LENGTH_OF_LONG - 1) /* number too long */
3318
0
   || (SIZEOF_ZEND_LONG == 4 &&
3319
0
       end - tmp == MAX_LENGTH_OF_LONG - 1 &&
3320
454
       *tmp > '2')) { /* overflow */
3321
454
    return 0;
3322
454
  }
3323
7.39k
  *idx = (*tmp - '0');
3324
8.39k
  while (1) {
3325
8.39k
    ++tmp;
3326
8.39k
    if (tmp == end) {
3327
3.19k
      if (*key == '-') {
3328
40
        if (*idx-1 > ZEND_LONG_MAX) { /* overflow */
3329
0
          return 0;
3330
0
        }
3331
40
        *idx = 0 - *idx;
3332
3.15k
      } else if (*idx > ZEND_LONG_MAX) { /* overflow */
3333
8
        return 0;
3334
8
      }
3335
3.18k
      return 1;
3336
3.19k
    }
3337
5.19k
    if (*tmp <= '9' && *tmp >= '0') {
3338
1.00k
      *idx = (*idx * 10) + (*tmp - '0');
3339
4.19k
    } else {
3340
4.19k
      return 0;
3341
4.19k
    }
3342
5.19k
  }
3343
7.39k
}
3344
3345
/* Takes a "symtable" hashtable (contains integer and non-numeric string keys)
3346
 * and converts it to a "proptable" (contains only string keys).
3347
 * If the symtable didn't need duplicating, its refcount is incremented.
3348
 */
3349
ZEND_API HashTable* ZEND_FASTCALL zend_symtable_to_proptable(HashTable *ht)
3350
112
{
3351
112
  zend_ulong num_key;
3352
112
  zend_string *str_key;
3353
112
  zval *zv;
3354
3355
112
  if (UNEXPECTED(HT_IS_PACKED(ht))) {
3356
8
    goto convert;
3357
8
  }
3358
3359
536
  ZEND_HASH_MAP_FOREACH_STR_KEY(ht, str_key) {
3360
536
    if (!str_key) {
3361
2
      goto convert;
3362
2
    }
3363
536
  } ZEND_HASH_FOREACH_END();
3364
3365
102
  if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
3366
20
    GC_ADDREF(ht);
3367
20
  }
3368
3369
102
  return ht;
3370
3371
10
convert:
3372
10
  {
3373
10
    HashTable *new_ht = zend_new_array(zend_hash_num_elements(ht));
3374
3375
64
    ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, str_key, zv) {
3376
64
      if (!str_key) {
3377
24
        str_key = zend_long_to_str(num_key);
3378
24
        zend_string_delref(str_key);
3379
24
      }
3380
64
      do {
3381
26
        if (Z_OPT_REFCOUNTED_P(zv)) {
3382
0
          if (Z_ISREF_P(zv) && Z_REFCOUNT_P(zv) == 1) {
3383
0
            zv = Z_REFVAL_P(zv);
3384
0
            if (!Z_OPT_REFCOUNTED_P(zv)) {
3385
0
              break;
3386
0
            }
3387
0
          }
3388
0
          Z_ADDREF_P(zv);
3389
0
        }
3390
26
      } while (0);
3391
64
      zend_hash_update(new_ht, str_key, zv);
3392
64
    } ZEND_HASH_FOREACH_END();
3393
3394
10
    return new_ht;
3395
104
  }
3396
104
}
3397
3398
/* Takes a "proptable" hashtable (contains only string keys) and converts it to
3399
 * a "symtable" (contains integer and non-numeric string keys).
3400
 * If the proptable didn't need duplicating, its refcount is incremented.
3401
 */
3402
ZEND_API HashTable* ZEND_FASTCALL zend_proptable_to_symtable(HashTable *ht, bool always_duplicate)
3403
951
{
3404
951
  zend_ulong num_key;
3405
951
  zend_string *str_key;
3406
951
  zval *zv;
3407
3408
951
  if (!HT_IS_PACKED(ht)) {
3409
22.6k
    ZEND_HASH_MAP_FOREACH_STR_KEY(ht, str_key) {
3410
      /* The `str_key &&` here might seem redundant: property tables should
3411
       * only have string keys. Unfortunately, this isn't true, at the very
3412
       * least because of ArrayObject, which stores a symtable where the
3413
       * property table should be.
3414
       */
3415
22.6k
      if (str_key && ZEND_HANDLE_NUMERIC(str_key, num_key)) {
3416
24
        goto convert;
3417
24
      }
3418
22.6k
    } ZEND_HASH_FOREACH_END();
3419
951
  }
3420
3421
927
  if (always_duplicate) {
3422
925
    return zend_array_dup(ht);
3423
925
  }
3424
3425
2
  if (EXPECTED(!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE))) {
3426
2
    GC_ADDREF(ht);
3427
2
  }
3428
3429
2
  return ht;
3430
3431
24
convert:
3432
24
  {
3433
24
    HashTable *new_ht = zend_new_array(zend_hash_num_elements(ht));
3434
3435
588
    ZEND_HASH_MAP_FOREACH_KEY_VAL_IND(ht, num_key, str_key, zv) {
3436
588
      do {
3437
255
        if (Z_OPT_REFCOUNTED_P(zv)) {
3438
132
          if (Z_ISREF_P(zv) && Z_REFCOUNT_P(zv) == 1) {
3439
4
            zv = Z_REFVAL_P(zv);
3440
4
            if (!Z_OPT_REFCOUNTED_P(zv)) {
3441
0
              break;
3442
0
            }
3443
4
          }
3444
132
          Z_ADDREF_P(zv);
3445
132
        }
3446
255
      } while (0);
3447
      /* Again, thank ArrayObject for `!str_key ||`. */
3448
588
      if (!str_key || ZEND_HANDLE_NUMERIC(str_key, num_key)) {
3449
39
        zend_hash_index_update(new_ht, num_key, zv);
3450
216
      } else {
3451
216
        zend_hash_update(new_ht, str_key, zv);
3452
216
      }
3453
588
    } ZEND_HASH_FOREACH_END();
3454
3455
24
    return new_ht;
3456
24
  }
3457
24
}