Coverage Report

Created: 2025-11-16 06:23

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
1.72G
  ZEND_ASSERT((expr) || (HT_FLAGS(ht) & HASH_FLAG_ALLOW_COW_VIOLATION))
39
#else
40
# define HT_ASSERT(ht, expr)
41
#endif
42
43
1.71G
#define HT_ASSERT_RC1(ht) HT_ASSERT(ht, GC_REFCOUNT(ht) == 1)
44
45
1.17k
#define HT_POISONED_PTR ((HashTable *) (intptr_t) -1)
46
47
#if ZEND_DEBUG
48
49
2.61G
#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
2.61G
{
56
2.61G
  if ((HT_FLAGS(ht) & HASH_FLAG_CONSISTENCY) == HT_OK) {
57
2.61G
    return;
58
2.61G
  }
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
2.61G
#define IS_CONSISTENT(a) _zend_is_inconsistent(a, __FILE__, __LINE__);
76
10.0M
#define SET_INCONSISTENT(n) do { \
77
10.0M
    HT_FLAGS(ht) = (HT_FLAGS(ht) & ~HASH_FLAG_CONSISTENCY) | (n); \
78
10.0M
  } 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
864M
  if ((ht)->nNumUsed >= (ht)->nTableSize) {   \
86
71.8k
    zend_hash_do_resize(ht);          \
87
71.8k
  }
88
89
134k
ZEND_API void *zend_hash_str_find_ptr_lc(const HashTable *ht, const char *str, size_t len) {
90
134k
  void *result;
91
134k
  char *lc_str;
92
93
  /* Stack allocate small strings to improve performance */
94
134k
  ALLOCA_FLAG(use_heap)
95
96
134k
  lc_str = zend_str_tolower_copy(do_alloca(len + 1, use_heap), str, len);
97
134k
  result = zend_hash_str_find_ptr(ht, lc_str, len);
98
134k
  free_alloca(lc_str, use_heap);
99
100
134k
  return result;
101
134k
}
102
103
4.35k
ZEND_API void *zend_hash_find_ptr_lc(const HashTable *ht, zend_string *key) {
104
4.35k
  void *result;
105
4.35k
  zend_string *lc_key = zend_string_tolower(key);
106
4.35k
  result = zend_hash_find_ptr(ht, lc_key);
107
4.35k
  zend_string_release(lc_key);
108
4.35k
  return result;
109
4.35k
}
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
10.5M
{
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
10.5M
  if (nSize <= HT_MIN_SIZE) {
122
9.63M
    return HT_MIN_SIZE;
123
9.63M
  } 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
939k
  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
10.5M
}
147
148
static zend_always_inline void zend_hash_real_init_packed_ex(HashTable *ht)
149
2.98M
{
150
2.98M
  void *data;
151
152
2.98M
  if (UNEXPECTED(GC_FLAGS(ht) & IS_ARRAY_PERSISTENT)) {
153
1.16k
    data = pemalloc(HT_PACKED_SIZE_EX(ht->nTableSize, HT_MIN_MASK), 1);
154
2.98M
  } else if (EXPECTED(ht->nTableSize == HT_MIN_SIZE)) {
155
    /* Use specialized API with constant allocation amount for a particularly common case. */
156
2.97M
    data = emalloc(HT_PACKED_SIZE_EX(HT_MIN_SIZE, HT_MIN_MASK));
157
2.97M
  } else {
158
13.3k
    data = emalloc(HT_PACKED_SIZE_EX(ht->nTableSize, HT_MIN_MASK));
159
13.3k
  }
160
2.98M
  HT_SET_DATA_ADDR(ht, data);
161
  /* Don't overwrite iterator count. */
162
2.98M
  ht->u.v.flags = HASH_FLAG_PACKED | HASH_FLAG_STATIC_KEYS;
163
2.98M
  HT_HASH_RESET_PACKED(ht);
164
2.98M
}
165
166
static zend_always_inline void zend_hash_real_init_mixed_ex(HashTable *ht)
167
3.82M
{
168
3.82M
  void *data;
169
3.82M
  uint32_t nSize = ht->nTableSize;
170
171
3.82M
  ZEND_ASSERT(HT_SIZE_TO_MASK(nSize));
172
173
3.82M
  if (UNEXPECTED(GC_FLAGS(ht) & IS_ARRAY_PERSISTENT)) {
174
4.79k
    data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), 1);
175
3.81M
  } else if (EXPECTED(nSize == HT_MIN_SIZE)) {
176
3.32M
    data = emalloc(HT_SIZE_EX(HT_MIN_SIZE, HT_SIZE_TO_MASK(HT_MIN_SIZE)));
177
3.32M
    ht->nTableMask = HT_SIZE_TO_MASK(HT_MIN_SIZE);
178
3.32M
    HT_SET_DATA_ADDR(ht, data);
179
    /* Don't overwrite iterator count. */
180
3.32M
    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
3.32M
    do {
190
3.32M
      __m128i xmm0 = _mm_setzero_si128();
191
3.32M
      xmm0 = _mm_cmpeq_epi8(xmm0, xmm0);
192
3.32M
      _mm_storeu_si128((__m128i*)&HT_HASH_EX(data,  0), xmm0);
193
3.32M
      _mm_storeu_si128((__m128i*)&HT_HASH_EX(data,  4), xmm0);
194
3.32M
      _mm_storeu_si128((__m128i*)&HT_HASH_EX(data,  8), xmm0);
195
3.32M
      _mm_storeu_si128((__m128i*)&HT_HASH_EX(data, 12), xmm0);
196
3.32M
    } 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
3.32M
    return;
224
3.32M
  } else {
225
492k
    data = emalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)));
226
492k
  }
227
497k
  ht->nTableMask = HT_SIZE_TO_MASK(nSize);
228
497k
  HT_SET_DATA_ADDR(ht, data);
229
497k
  HT_FLAGS(ht) = HASH_FLAG_STATIC_KEYS;
230
497k
  HT_HASH_RESET(ht);
231
497k
}
232
233
static zend_always_inline void zend_hash_real_init_ex(HashTable *ht, bool packed)
234
29.3k
{
235
29.3k
  HT_ASSERT_RC1(ht);
236
29.3k
  ZEND_ASSERT(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED);
237
29.3k
  if (packed) {
238
81
    zend_hash_real_init_packed_ex(ht);
239
29.3k
  } else {
240
29.3k
    zend_hash_real_init_mixed_ex(ht);
241
29.3k
  }
242
29.3k
}
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
10.3M
{
263
10.3M
  GC_SET_REFCOUNT(ht, 1);
264
10.3M
  GC_TYPE_INFO(ht) = GC_ARRAY | (persistent ? ((GC_PERSISTENT|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT) : 0);
265
10.3M
  HT_FLAGS(ht) = HASH_FLAG_UNINITIALIZED;
266
10.3M
  ht->nTableMask = HT_MIN_MASK;
267
10.3M
  HT_SET_DATA_ADDR(ht, &uninitialized_bucket);
268
10.3M
  ht->nNumUsed = 0;
269
10.3M
  ht->nNumOfElements = 0;
270
10.3M
  ht->nInternalPointer = 0;
271
10.3M
  ht->nNextFreeElement = ZEND_LONG_MIN;
272
10.3M
  ht->pDestructor = pDestructor;
273
10.3M
  ht->nTableSize = zend_hash_check_size(nSize);
274
10.3M
}
275
276
ZEND_API void ZEND_FASTCALL _zend_hash_init(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, bool persistent)
277
4.09M
{
278
4.09M
  _zend_hash_init_int(ht, nSize, pDestructor, persistent);
279
4.09M
}
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
6.30M
{
290
6.30M
  HashTable *ht = emalloc(sizeof(HashTable));
291
6.30M
  _zend_hash_init_int(ht, nSize, ZVAL_PTR_DTOR, false);
292
6.30M
  return ht;
293
6.30M
}
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
218k
{
312
218k
  HT_ASSERT_RC1(ht);
313
218k
  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
218k
  uint32_t newTableSize = ht->nTableSize * 2;
317
218k
  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
218k
  ht->nTableSize = newTableSize;
319
218k
}
320
321
ZEND_API void ZEND_FASTCALL zend_hash_real_init(HashTable *ht, bool packed)
322
29.3k
{
323
29.3k
  IS_CONSISTENT(ht);
324
325
29.3k
  HT_ASSERT_RC1(ht);
326
29.3k
  zend_hash_real_init_ex(ht, packed);
327
29.3k
}
328
329
ZEND_API void ZEND_FASTCALL zend_hash_real_init_packed(HashTable *ht)
330
486k
{
331
486k
  IS_CONSISTENT(ht);
332
333
486k
  HT_ASSERT_RC1(ht);
334
486k
  zend_hash_real_init_packed_ex(ht);
335
486k
}
336
337
ZEND_API void ZEND_FASTCALL zend_hash_real_init_mixed(HashTable *ht)
338
3.79M
{
339
3.79M
  IS_CONSISTENT(ht);
340
341
3.79M
  HT_ASSERT_RC1(ht);
342
3.79M
  zend_hash_real_init_mixed_ex(ht);
343
3.79M
}
344
345
ZEND_API void ZEND_FASTCALL zend_hash_packed_to_hash(HashTable *ht)
346
15.6k
{
347
15.6k
  void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
348
15.6k
  zval *src = ht->arPacked;
349
15.6k
  Bucket *dst;
350
15.6k
  uint32_t i;
351
15.6k
  uint32_t nSize = ht->nTableSize;
352
353
15.6k
  ZEND_ASSERT(HT_SIZE_TO_MASK(nSize));
354
355
15.6k
  HT_ASSERT_RC1(ht);
356
  // Alloc before assign to avoid inconsistencies on OOM
357
15.6k
  new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
358
15.6k
  HT_FLAGS(ht) &= ~HASH_FLAG_PACKED;
359
15.6k
  ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
360
15.6k
  HT_SET_DATA_ADDR(ht, new_data);
361
15.6k
  dst = ht->arData;
362
181k
  for (i = 0; i < ht->nNumUsed; i++) {
363
165k
    ZVAL_COPY_VALUE(&dst->val, src);
364
165k
    dst->h = i;
365
165k
    dst->key = NULL;
366
165k
    dst++;
367
165k
    src++;
368
165k
  }
369
15.6k
  pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
370
15.6k
  zend_hash_rehash(ht);
371
15.6k
}
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
289k
{
397
289k
  HT_ASSERT_RC1(ht);
398
399
289k
  if (nSize == 0) return;
400
401
286k
  ZEND_ASSERT(HT_SIZE_TO_MASK(nSize));
402
403
286k
  if (UNEXPECTED(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
404
29.3k
    if (nSize > ht->nTableSize) {
405
1.59k
      ht->nTableSize = zend_hash_check_size(nSize);
406
1.59k
    }
407
29.3k
    zend_hash_real_init(ht, packed);
408
257k
  } else {
409
257k
    if (packed) {
410
35
      ZEND_ASSERT(HT_IS_PACKED(ht));
411
35
      if (nSize > ht->nTableSize) {
412
1
        uint32_t newTableSize = zend_hash_check_size(nSize);
413
1
        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
1
        ht->nTableSize = newTableSize;
415
1
      }
416
257k
    } else {
417
257k
      ZEND_ASSERT(!HT_IS_PACKED(ht));
418
257k
      if (nSize > ht->nTableSize) {
419
184k
        void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
420
184k
        Bucket *old_buckets = ht->arData;
421
184k
        nSize = zend_hash_check_size(nSize);
422
184k
        new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
423
184k
        ht->nTableSize = nSize;
424
184k
        ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
425
184k
        HT_SET_DATA_ADDR(ht, new_data);
426
184k
        memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
427
184k
        pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
428
184k
        zend_hash_rehash(ht);
429
184k
      }
430
257k
    }
431
257k
  }
432
286k
}
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
1.25k
{
461
1.25k
  zval *val;
462
1.25k
  uint32_t num = ht->nNumOfElements;
463
464
7.11k
  ZEND_HASH_MAP_FOREACH_VAL(ht, val) {
465
7.11k
    if (Z_TYPE_P(val) == IS_INDIRECT) {
466
2.21k
      if (UNEXPECTED(Z_TYPE_P(Z_INDIRECT_P(val)) == IS_UNDEF)) {
467
1.25k
        num--;
468
1.25k
      }
469
2.21k
    }
470
7.11k
  } ZEND_HASH_FOREACH_END();
471
1.25k
  return num;
472
1.25k
}
473
/* }}} */
474
475
ZEND_API uint32_t zend_array_count(HashTable *ht)
476
18.2k
{
477
18.2k
  uint32_t num;
478
18.2k
  if (UNEXPECTED(HT_FLAGS(ht) & HASH_FLAG_HAS_EMPTY_IND)) {
479
1.25k
    num = zend_array_recalc_elements(ht);
480
1.25k
    if (UNEXPECTED(ht->nNumOfElements == num)) {
481
254
      HT_FLAGS(ht) &= ~HASH_FLAG_HAS_EMPTY_IND;
482
254
    }
483
17.0k
  } else if (UNEXPECTED(ht == &EG(symbol_table))) {
484
0
    num = zend_array_recalc_elements(ht);
485
17.0k
  } else {
486
17.0k
    num = zend_hash_num_elements(ht);
487
17.0k
  }
488
18.2k
  return num;
489
18.2k
}
490
/* }}} */
491
492
static zend_always_inline HashPosition _zend_hash_get_valid_pos(const HashTable *ht, HashPosition pos)
493
10.7k
{
494
10.7k
  if (HT_IS_PACKED(ht)) {
495
4.35k
    while (pos < ht->nNumUsed && Z_ISUNDEF(ht->arPacked[pos])) {
496
796
      pos++;
497
796
    }
498
7.16k
  } else {
499
7.40k
    while (pos < ht->nNumUsed && Z_ISUNDEF(ht->arData[pos].val)) {
500
244
      pos++;
501
244
    }
502
7.16k
  }
503
10.7k
  return pos;
504
10.7k
}
505
506
static zend_always_inline HashPosition _zend_hash_get_current_pos(const HashTable *ht)
507
922
{
508
922
  return _zend_hash_get_valid_pos(ht, ht->nInternalPointer);
509
922
}
510
511
ZEND_API HashPosition ZEND_FASTCALL zend_hash_get_current_pos(const HashTable *ht)
512
185
{
513
185
  return _zend_hash_get_current_pos(ht);
514
185
}
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
83
static void zend_hash_remove_iterator_copies(uint32_t idx) {
522
83
  HashTableIterator *iterators = EG(ht_iterators);
523
524
83
  HashTableIterator *iter = iterators + idx;
525
83
  uint32_t next_idx = iter->next_copy;
526
566
  while (next_idx != idx) {
527
483
    uint32_t cur_idx = next_idx;
528
483
    HashTableIterator *cur_iter = iterators + cur_idx;
529
483
    next_idx = cur_iter->next_copy;
530
483
    cur_iter->next_copy = cur_idx; // avoid recursion in zend_hash_iterator_del
531
483
    zend_hash_iterator_del(cur_idx);
532
483
  }
533
83
  iter->next_copy = idx;
534
83
}
535
536
ZEND_API uint32_t ZEND_FASTCALL zend_hash_iterator_add(HashTable *ht, HashPosition pos)
537
2.25k
{
538
2.25k
  HashTableIterator *iter = EG(ht_iterators);
539
2.25k
  HashTableIterator *end  = iter + EG(ht_iterators_count);
540
2.25k
  uint32_t idx;
541
542
2.25k
  if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
543
2.25k
    HT_INC_ITERATORS_COUNT(ht);
544
2.25k
  }
545
15.8k
  while (iter != end) {
546
15.7k
    if (iter->ht == NULL) {
547
2.21k
      iter->ht = ht;
548
2.21k
      iter->pos = pos;
549
2.21k
      idx = iter - EG(ht_iterators);
550
2.21k
      iter->next_copy = idx;
551
2.21k
      if (idx + 1 > EG(ht_iterators_used)) {
552
2.21k
        EG(ht_iterators_used) = idx + 1;
553
2.21k
      }
554
2.21k
      return idx;
555
2.21k
    }
556
13.5k
    iter++;
557
13.5k
  }
558
42
  if (EG(ht_iterators) == EG(ht_iterators_slots)) {
559
6
    EG(ht_iterators) = emalloc(sizeof(HashTableIterator) * (EG(ht_iterators_count) + 8));
560
6
    memcpy(EG(ht_iterators), EG(ht_iterators_slots), sizeof(HashTableIterator) * EG(ht_iterators_count));
561
36
  } else {
562
36
    EG(ht_iterators) = erealloc(EG(ht_iterators), sizeof(HashTableIterator) * (EG(ht_iterators_count) + 8));
563
36
  }
564
42
  iter = EG(ht_iterators) + EG(ht_iterators_count);
565
42
  EG(ht_iterators_count) += 8;
566
42
  iter->ht = ht;
567
42
  iter->pos = pos;
568
42
  memset(iter + 1, 0, sizeof(HashTableIterator) * 7);
569
42
  idx = iter - EG(ht_iterators);
570
42
  iter->next_copy = idx;
571
42
  EG(ht_iterators_used) = idx + 1;
572
42
  return idx;
573
2.25k
}
574
575
// To avoid losing track of the HashTable when separating arrays, we track all copies at once.
576
812
static zend_always_inline bool zend_hash_iterator_find_copy_pos(uint32_t idx, HashTable *ht) {
577
812
  HashTableIterator *iter = EG(ht_iterators) + idx;
578
579
812
  uint32_t next_idx = iter->next_copy;
580
812
  if (EXPECTED(next_idx != idx)) {
581
75
    HashTableIterator *copy_iter;
582
80
    while (next_idx != idx) {
583
80
      copy_iter = EG(ht_iterators) + next_idx;
584
80
      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
75
        if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
588
65
          && EXPECTED(!HT_ITERATORS_OVERFLOW(iter->ht))) {
589
65
          HT_DEC_ITERATORS_COUNT(iter->ht);
590
65
        }
591
75
        if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
592
75
          HT_INC_ITERATORS_COUNT(ht);
593
75
        }
594
75
        iter->ht = copy_iter->ht;
595
75
        iter->pos = copy_iter->pos;
596
75
        zend_hash_remove_iterator_copies(idx);
597
75
        return true;
598
75
      }
599
5
      next_idx = copy_iter->next_copy;
600
5
    }
601
0
    zend_hash_remove_iterator_copies(idx);
602
0
  }
603
604
737
  return false;
605
812
}
606
607
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos(uint32_t idx, HashTable *ht)
608
1.60k
{
609
1.60k
  HashTableIterator *iter = EG(ht_iterators) + idx;
610
611
1.60k
  ZEND_ASSERT(idx != (uint32_t)-1);
612
1.60k
  if (UNEXPECTED(iter->ht != ht) && !zend_hash_iterator_find_copy_pos(idx, ht)) {
613
5
    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
5
    if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
618
5
      HT_INC_ITERATORS_COUNT(ht);
619
5
    }
620
5
    iter->ht = ht;
621
5
    iter->pos = _zend_hash_get_current_pos(ht);
622
5
  }
623
1.60k
  return iter->pos;
624
1.60k
}
625
626
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterator_pos_ex(uint32_t idx, zval *array)
627
6.60k
{
628
6.60k
  HashTable *ht = Z_ARRVAL_P(array);
629
6.60k
  HashTableIterator *iter = EG(ht_iterators) + idx;
630
631
6.60k
  ZEND_ASSERT(idx != (uint32_t)-1);
632
6.60k
  if (UNEXPECTED(iter->ht != ht) && !zend_hash_iterator_find_copy_pos(idx, ht)) {
633
732
    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
732
    if (UNEXPECTED(GC_REFCOUNT(ht) > 1)) {
640
389
      ZVAL_ARR(array, zend_array_dup(ht));
641
389
      GC_TRY_DELREF(ht);
642
389
      iter = EG(ht_iterators) + idx;
643
389
      ht = Z_ARRVAL_P(array);
644
389
    }
645
646
732
    if (EXPECTED(!HT_ITERATORS_OVERFLOW(ht))) {
647
732
      HT_INC_ITERATORS_COUNT(ht);
648
732
    }
649
732
    iter->ht = ht;
650
732
    iter->pos = _zend_hash_get_current_pos(ht);
651
732
  }
652
6.60k
  return iter->pos;
653
6.60k
}
654
655
ZEND_API void ZEND_FASTCALL zend_hash_iterator_del(uint32_t idx)
656
2.23k
{
657
2.23k
  HashTableIterator *iter = EG(ht_iterators) + idx;
658
659
2.23k
  ZEND_ASSERT(idx != (uint32_t)-1);
660
661
2.23k
  if (EXPECTED(iter->ht) && EXPECTED(iter->ht != HT_POISONED_PTR)
662
1.80k
      && EXPECTED(!HT_ITERATORS_OVERFLOW(iter->ht))) {
663
1.80k
    ZEND_ASSERT(HT_ITERATORS_COUNT(iter->ht) != 0);
664
1.80k
    HT_DEC_ITERATORS_COUNT(iter->ht);
665
1.80k
  }
666
2.23k
  iter->ht = NULL;
667
668
2.23k
  if (UNEXPECTED(iter->next_copy != idx)) {
669
8
    zend_hash_remove_iterator_copies(idx);
670
8
  }
671
672
2.23k
  if (idx == EG(ht_iterators_used) - 1) {
673
2.23k
    while (idx > 0 && EG(ht_iterators)[idx - 1].ht == NULL) {
674
26
      idx--;
675
26
    }
676
2.21k
    EG(ht_iterators_used) = idx;
677
2.21k
  }
678
2.23k
}
679
680
static zend_never_inline void ZEND_FASTCALL _zend_hash_iterators_remove(const HashTable *ht)
681
787
{
682
787
  HashTableIterator *iter = EG(ht_iterators);
683
787
  const HashTableIterator *end = iter + EG(ht_iterators_used);
684
685
14.8k
  while (iter != end) {
686
14.0k
    if (iter->ht == ht) {
687
1.17k
      iter->ht = HT_POISONED_PTR;
688
1.17k
    }
689
14.0k
    iter++;
690
14.0k
  }
691
787
}
692
693
static zend_always_inline void zend_hash_iterators_remove(const HashTable *ht)
694
6.98M
{
695
6.98M
  if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
696
787
    _zend_hash_iterators_remove(ht);
697
787
  }
698
6.98M
}
699
700
ZEND_API HashPosition ZEND_FASTCALL zend_hash_iterators_lower_pos(const HashTable *ht, HashPosition start)
701
1.17k
{
702
1.17k
  const HashTableIterator *iter = EG(ht_iterators);
703
1.17k
  const HashTableIterator *end = iter + EG(ht_iterators_used);
704
1.17k
  HashPosition res = ht->nNumUsed;
705
706
2.01k
  while (iter != end) {
707
837
    if (iter->ht == ht) {
708
822
      if (iter->pos >= start && iter->pos < res) {
709
219
        res = iter->pos;
710
219
      }
711
822
    }
712
837
    iter++;
713
837
  }
714
1.17k
  return res;
715
1.17k
}
716
717
ZEND_API void ZEND_FASTCALL _zend_hash_iterators_update(const HashTable *ht, HashPosition from, HashPosition to)
718
214
{
719
214
  HashTableIterator *iter = EG(ht_iterators);
720
214
  const HashTableIterator *end = iter + EG(ht_iterators_used);
721
722
481
  while (iter != end) {
723
267
    if (iter->ht == ht && iter->pos == from) {
724
186
      iter->pos = to;
725
186
    }
726
267
    iter++;
727
267
  }
728
214
}
729
730
ZEND_API void ZEND_FASTCALL zend_hash_iterators_advance(const HashTable *ht, HashPosition step)
731
7
{
732
7
  HashTableIterator *iter = EG(ht_iterators);
733
7
  const HashTableIterator *end = iter + EG(ht_iterators_used);
734
735
14
  while (iter != end) {
736
7
    if (iter->ht == ht) {
737
7
      iter->pos += step;
738
7
    }
739
7
    iter++;
740
7
  }
741
7
}
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
43.6M
{
746
43.6M
  uint32_t nIndex;
747
43.6M
  uint32_t idx;
748
43.6M
  Bucket *p, *arData;
749
750
43.6M
  ZEND_ASSERT(ZSTR_H(key) != 0 && "Hash must be known");
751
752
43.6M
  arData = ht->arData;
753
43.6M
  nIndex = ZSTR_H(key) | ht->nTableMask;
754
43.6M
  idx = HT_HASH_EX(arData, nIndex);
755
756
43.6M
  if (UNEXPECTED(idx == HT_INVALID_IDX)) {
757
16.9M
    return NULL;
758
16.9M
  }
759
26.6M
  p = HT_HASH_TO_BUCKET_EX(arData, idx);
760
26.6M
  if (EXPECTED(p->key == key)) { /* check for the same interned string */
761
17.6M
    return p;
762
17.6M
  }
763
764
10.0M
  while (1) {
765
10.0M
    if (p->h == ZSTR_H(key) &&
766
2.39M
        EXPECTED(p->key) &&
767
2.39M
        zend_string_equal_content(p->key, key)) {
768
2.35M
      return p;
769
2.35M
    }
770
7.65M
    idx = Z_NEXT(p->val);
771
7.65M
    if (idx == HT_INVALID_IDX) {
772
6.52M
      return NULL;
773
6.52M
    }
774
1.13M
    p = HT_HASH_TO_BUCKET_EX(arData, idx);
775
1.13M
    if (p->key == key) { /* check for the same interned string */
776
115k
      return p;
777
115k
    }
778
1.13M
  }
779
8.98M
}
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
3.80M
{
783
3.80M
  uint32_t nIndex;
784
3.80M
  uint32_t idx;
785
3.80M
  Bucket *p, *arData;
786
787
3.80M
  arData = ht->arData;
788
3.80M
  nIndex = h | ht->nTableMask;
789
3.80M
  idx = HT_HASH_EX(arData, nIndex);
790
3.99M
  while (idx != HT_INVALID_IDX) {
791
1.12M
    ZEND_ASSERT(idx < HT_IDX_TO_HASH(ht->nTableSize));
792
1.12M
    p = HT_HASH_TO_BUCKET_EX(arData, idx);
793
1.12M
    if ((p->h == h)
794
941k
       && p->key
795
941k
       && zend_string_equals_cstr(p->key, str, len)) {
796
941k
      return p;
797
941k
    }
798
183k
    idx = Z_NEXT(p->val);
799
183k
  }
800
2.86M
  return NULL;
801
3.80M
}
802
803
static zend_always_inline Bucket *zend_hash_index_find_bucket(const HashTable *ht, zend_ulong h)
804
1.70G
{
805
1.70G
  uint32_t nIndex;
806
1.70G
  uint32_t idx;
807
1.70G
  Bucket *p, *arData;
808
809
1.70G
  arData = ht->arData;
810
1.70G
  nIndex = h | ht->nTableMask;
811
1.70G
  idx = HT_HASH_EX(arData, nIndex);
812
2.01G
  while (idx != HT_INVALID_IDX) {
813
1.14G
    ZEND_ASSERT(idx < HT_IDX_TO_HASH(ht->nTableSize));
814
1.14G
    p = HT_HASH_TO_BUCKET_EX(arData, idx);
815
1.14G
    if (p->h == h && !p->key) {
816
840M
      return p;
817
840M
    }
818
302M
    idx = Z_NEXT(p->val);
819
302M
  }
820
868M
  return NULL;
821
1.70G
}
822
823
static zend_always_inline zval *_zend_hash_add_or_update_i(HashTable *ht, zend_string *key, zval *pData, uint32_t flag)
824
6.41M
{
825
6.41M
  zend_ulong h;
826
6.41M
  uint32_t nIndex;
827
6.41M
  uint32_t idx;
828
6.41M
  Bucket *p, *arData;
829
830
6.41M
  IS_CONSISTENT(ht);
831
6.41M
  HT_ASSERT_RC1(ht);
832
6.41M
  zend_string_hash_val(key);
833
834
6.41M
  if (UNEXPECTED(HT_FLAGS(ht) & (HASH_FLAG_UNINITIALIZED|HASH_FLAG_PACKED))) {
835
1.00M
    if (EXPECTED(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
836
998k
      zend_hash_real_init_mixed(ht);
837
998k
      goto add_to_hash;
838
998k
    } else {
839
6.26k
      zend_hash_packed_to_hash(ht);
840
6.26k
    }
841
5.41M
  } else if ((flag & HASH_ADD_NEW) == 0 || ZEND_DEBUG) {
842
5.41M
    p = zend_hash_find_bucket(ht, key);
843
844
5.41M
    if (p) {
845
674k
      zval *data;
846
847
674k
      ZEND_ASSERT((flag & HASH_ADD_NEW) == 0);
848
674k
      if (flag & HASH_LOOKUP) {
849
176k
        return &p->val;
850
497k
      } else if (flag & HASH_ADD) {
851
126k
        if (!(flag & HASH_UPDATE_INDIRECT)) {
852
125k
          return NULL;
853
125k
        }
854
684
        ZEND_ASSERT(&p->val != pData);
855
684
        data = &p->val;
856
684
        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
684
        } else {
862
684
          return NULL;
863
684
        }
864
371k
      } else {
865
371k
        ZEND_ASSERT(&p->val != pData);
866
371k
        data = &p->val;
867
371k
        if ((flag & HASH_UPDATE_INDIRECT) && Z_TYPE_P(data) == IS_INDIRECT) {
868
0
          data = Z_INDIRECT_P(data);
869
0
        }
870
371k
      }
871
371k
      if (ht->pDestructor) {
872
344k
        ht->pDestructor(data);
873
344k
      }
874
371k
      ZVAL_COPY_VALUE(data, pData);
875
371k
      return data;
876
674k
    }
877
5.41M
  }
878
879
4.74M
  ZEND_HASH_IF_FULL_DO_RESIZE(ht);   /* If the Hash table is full, resize it */
880
881
5.74M
add_to_hash:
882
5.74M
  if (!ZSTR_IS_INTERNED(key)) {
883
2.21M
    zend_string_addref(key);
884
2.21M
    HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
885
2.21M
  }
886
5.74M
  idx = ht->nNumUsed++;
887
5.74M
  ht->nNumOfElements++;
888
5.74M
  arData = ht->arData;
889
5.74M
  p = arData + idx;
890
5.74M
  p->key = key;
891
5.74M
  p->h = h = ZSTR_H(key);
892
5.74M
  nIndex = h | ht->nTableMask;
893
5.74M
  Z_NEXT(p->val) = HT_HASH_EX(arData, nIndex);
894
5.74M
  HT_HASH_EX(arData, nIndex) = HT_IDX_TO_HASH(idx);
895
5.74M
  if (flag & HASH_LOOKUP) {
896
245k
    ZVAL_NULL(&p->val);
897
5.49M
  } else {
898
5.49M
    ZVAL_COPY_VALUE(&p->val, pData);
899
5.49M
  }
900
901
5.74M
  return &p->val;
902
4.74M
}
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
289k
{
906
289k
  zend_string *key;
907
289k
  uint32_t nIndex;
908
289k
  uint32_t idx;
909
289k
  Bucket *p;
910
911
289k
  IS_CONSISTENT(ht);
912
289k
  HT_ASSERT_RC1(ht);
913
914
289k
  if (UNEXPECTED(HT_FLAGS(ht) & (HASH_FLAG_UNINITIALIZED|HASH_FLAG_PACKED))) {
915
146k
    if (EXPECTED(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
916
146k
      zend_hash_real_init_mixed(ht);
917
146k
      goto add_to_hash;
918
146k
    } else {
919
50
      zend_hash_packed_to_hash(ht);
920
50
    }
921
146k
  } else if ((flag & HASH_ADD_NEW) == 0) {
922
142k
    p = zend_hash_str_find_bucket(ht, str, len, h);
923
924
142k
    if (p) {
925
103k
      zval *data;
926
927
103k
      if (flag & HASH_LOOKUP) {
928
0
        return &p->val;
929
103k
      } 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
103k
      } else {
944
103k
        ZEND_ASSERT(&p->val != pData);
945
103k
        data = &p->val;
946
103k
        if ((flag & HASH_UPDATE_INDIRECT) && Z_TYPE_P(data) == IS_INDIRECT) {
947
0
          data = Z_INDIRECT_P(data);
948
0
        }
949
103k
      }
950
103k
      if (ht->pDestructor) {
951
103k
        ht->pDestructor(data);
952
103k
      }
953
103k
      ZVAL_COPY_VALUE(data, pData);
954
103k
      return data;
955
103k
    }
956
142k
  }
957
958
39.3k
  ZEND_HASH_IF_FULL_DO_RESIZE(ht);   /* If the Hash table is full, resize it */
959
960
186k
add_to_hash:
961
186k
  idx = ht->nNumUsed++;
962
186k
  ht->nNumOfElements++;
963
186k
  p = ht->arData + idx;
964
186k
  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
186k
  p->h = ZSTR_H(key) = h;
971
186k
  HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
972
186k
  if (flag & HASH_LOOKUP) {
973
0
    ZVAL_NULL(&p->val);
974
186k
  } else {
975
186k
    ZVAL_COPY_VALUE(&p->val, pData);
976
186k
  }
977
186k
  nIndex = h | ht->nTableMask;
978
186k
  Z_NEXT(p->val) = HT_HASH(ht, nIndex);
979
186k
  HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
980
981
186k
  return &p->val;
982
39.3k
}
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
744k
{
1000
744k
  return _zend_hash_add_or_update_i(ht, key, pData, HASH_ADD);
1001
744k
}
1002
1003
ZEND_API zval* ZEND_FASTCALL zend_hash_update(HashTable *ht, zend_string *key, zval *pData)
1004
3.55M
{
1005
3.55M
  return _zend_hash_add_or_update_i(ht, key, pData, HASH_UPDATE);
1006
3.55M
}
1007
1008
ZEND_API zval* ZEND_FASTCALL zend_hash_update_ind(HashTable *ht, zend_string *key, zval *pData)
1009
4.09k
{
1010
4.09k
  return _zend_hash_add_or_update_i(ht, key, pData, HASH_UPDATE | HASH_UPDATE_INDIRECT);
1011
4.09k
}
1012
1013
ZEND_API zval* ZEND_FASTCALL zend_hash_add_new(HashTable *ht, zend_string *key, zval *pData)
1014
1.69M
{
1015
1.69M
  return _zend_hash_add_or_update_i(ht, key, pData, HASH_ADD_NEW);
1016
1.69M
}
1017
1018
ZEND_API zval* ZEND_FASTCALL zend_hash_lookup(HashTable *ht, zend_string *key)
1019
422k
{
1020
422k
  return _zend_hash_add_or_update_i(ht, key, NULL, HASH_LOOKUP);
1021
422k
}
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
284k
{
1039
284k
  zend_ulong h = zend_hash_func(str, len);
1040
1041
284k
  return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_UPDATE);
1042
284k
}
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
5.27k
{
1053
5.27k
  zend_ulong h = zend_hash_func(str, len);
1054
1055
5.27k
  return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_ADD);
1056
5.27k
}
1057
1058
ZEND_API zval* ZEND_FASTCALL zend_hash_str_add_new(HashTable *ht, const char *str, size_t len, zval *pData)
1059
206
{
1060
206
  zend_ulong h = zend_hash_func(str, len);
1061
1062
206
  return _zend_hash_str_add_or_update_i(ht, str, len, h, pData, HASH_ADD_NEW);
1063
206
}
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
3.92k
{
1074
3.92k
  zval dummy;
1075
1076
3.92k
  ZVAL_NULL(&dummy);
1077
3.92k
  return zend_hash_index_add(ht, h, &dummy);
1078
3.92k
}
1079
1080
ZEND_API zval* ZEND_FASTCALL zend_hash_add_empty_element(HashTable *ht, zend_string *key)
1081
384k
{
1082
384k
  zval dummy;
1083
1084
384k
  ZVAL_NULL(&dummy);
1085
384k
  return zend_hash_add(ht, key, &dummy);
1086
384k
}
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
879M
{
1098
879M
  uint32_t nIndex;
1099
879M
  uint32_t idx;
1100
879M
  Bucket *p;
1101
879M
  zval *zv;
1102
1103
879M
  IS_CONSISTENT(ht);
1104
879M
  HT_ASSERT_RC1(ht);
1105
1106
879M
  if ((flag & HASH_ADD_NEXT) && h == ZEND_LONG_MIN) {
1107
2.41M
    h = 0;
1108
2.41M
  }
1109
1110
879M
  if (HT_IS_PACKED(ht)) {
1111
16.7M
    if ((flag & (HASH_ADD_NEW|HASH_ADD_NEXT)) != (HASH_ADD_NEW|HASH_ADD_NEXT)
1112
15.8M
     && h < ht->nNumUsed) {
1113
27.7k
      zv = ht->arPacked + h;
1114
27.7k
      if (Z_TYPE_P(zv) != IS_UNDEF) {
1115
24.7k
        if (flag & HASH_LOOKUP) {
1116
20
          return zv;
1117
20
        }
1118
42.9k
replace:
1119
42.9k
        if (flag & HASH_ADD) {
1120
14.7k
          return NULL;
1121
14.7k
        }
1122
28.2k
        if (ht->pDestructor) {
1123
28.2k
          ht->pDestructor(zv);
1124
28.2k
        }
1125
28.2k
        ZVAL_COPY_VALUE(zv, pData);
1126
28.2k
        return zv;
1127
42.9k
      } else { /* we have to keep the order :( */
1128
3.00k
        goto convert_to_hash;
1129
3.00k
      }
1130
16.7M
    } else if (EXPECTED(h < ht->nTableSize)) {
1131
19.2M
add_to_packed:
1132
19.2M
      zv = ht->arPacked + h;
1133
      /* incremental initialization of empty Buckets */
1134
19.2M
      if ((flag & (HASH_ADD_NEW|HASH_ADD_NEXT)) != (HASH_ADD_NEW|HASH_ADD_NEXT)) {
1135
16.9M
        if (h > ht->nNumUsed) {
1136
59.7k
          zval *q = ht->arPacked + ht->nNumUsed;
1137
221k
          while (q != zv) {
1138
161k
            ZVAL_UNDEF(q);
1139
161k
            q++;
1140
161k
          }
1141
59.7k
        }
1142
16.9M
      }
1143
19.2M
      ht->nNextFreeElement = ht->nNumUsed = h + 1;
1144
19.2M
      ht->nNumOfElements++;
1145
19.2M
      if (flag & HASH_LOOKUP) {
1146
13.0k
        ZVAL_NULL(zv);
1147
19.1M
      } else {
1148
19.1M
        ZVAL_COPY_VALUE(zv, pData);
1149
19.1M
      }
1150
1151
19.2M
      return zv;
1152
16.4M
    } else if ((h >> 1) < ht->nTableSize &&
1153
219k
               (ht->nTableSize >> 1) < ht->nNumOfElements) {
1154
218k
      zend_hash_packed_grow(ht);
1155
218k
      goto add_to_packed;
1156
218k
    } else {
1157
5.89k
      if (ht->nNumUsed >= ht->nTableSize) {
1158
825
        ht->nTableSize += ht->nTableSize;
1159
825
      }
1160
8.90k
convert_to_hash:
1161
8.90k
      zend_hash_packed_to_hash(ht);
1162
8.90k
    }
1163
862M
  } else if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) {
1164
2.58M
    if (h < ht->nTableSize) {
1165
2.49M
      zend_hash_real_init_packed_ex(ht);
1166
2.49M
      goto add_to_packed;
1167
2.49M
    }
1168
82.9k
    zend_hash_real_init_mixed(ht);
1169
859M
  } else {
1170
859M
    if ((flag & HASH_ADD_NEW) == 0 || ZEND_DEBUG) {
1171
859M
      p = zend_hash_index_find_bucket(ht, h);
1172
859M
      if (p) {
1173
235k
        if (flag & HASH_LOOKUP) {
1174
217k
          return &p->val;
1175
217k
        }
1176
18.3k
        ZEND_ASSERT((flag & HASH_ADD_NEW) == 0);
1177
18.3k
        zv = &p->val;
1178
18.3k
        goto replace;
1179
18.3k
      }
1180
859M
    }
1181
859M
    ZEND_HASH_IF_FULL_DO_RESIZE(ht);   /* If the Hash table is full, resize it */
1182
859M
  }
1183
1184
859M
  idx = ht->nNumUsed++;
1185
859M
  nIndex = h | ht->nTableMask;
1186
859M
  p = ht->arData + idx;
1187
859M
  Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1188
859M
  HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1189
859M
  if ((zend_long)h >= ht->nNextFreeElement) {
1190
4.69M
    ht->nNextFreeElement = (zend_long)h < ZEND_LONG_MAX ? h + 1 : ZEND_LONG_MAX;
1191
4.69M
  }
1192
859M
  ht->nNumOfElements++;
1193
859M
  p->h = h;
1194
859M
  p->key = NULL;
1195
859M
  if (flag & HASH_LOOKUP) {
1196
948k
    ZVAL_NULL(&p->val);
1197
858M
  } else {
1198
858M
    ZVAL_COPY_VALUE(&p->val, pData);
1199
858M
  }
1200
1201
859M
  return &p->val;
1202
879M
}
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
32.9k
{
1224
32.9k
  return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_ADD);
1225
32.9k
}
1226
1227
ZEND_API zval* ZEND_FASTCALL zend_hash_index_add_new(HashTable *ht, zend_ulong h, zval *pData)
1228
858M
{
1229
858M
  return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_ADD | HASH_ADD_NEW);
1230
858M
}
1231
1232
ZEND_API zval* ZEND_FASTCALL zend_hash_index_update(HashTable *ht, zend_ulong h, zval *pData)
1233
8.23M
{
1234
8.23M
  return _zend_hash_index_add_or_update_i(ht, h, pData, HASH_UPDATE);
1235
8.23M
}
1236
1237
ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData)
1238
8.97M
{
1239
8.97M
  return _zend_hash_index_add_or_update_i(ht, ht->nNextFreeElement, pData, HASH_ADD | HASH_ADD_NEXT);
1240
8.97M
}
1241
1242
ZEND_API zval* ZEND_FASTCALL zend_hash_next_index_insert_new(HashTable *ht, zval *pData)
1243
2.28M
{
1244
2.28M
  return _zend_hash_index_add_or_update_i(ht, ht->nNextFreeElement, pData, HASH_ADD | HASH_ADD_NEW | HASH_ADD_NEXT);
1245
2.28M
}
1246
1247
ZEND_API zval* ZEND_FASTCALL zend_hash_index_lookup(HashTable *ht, zend_ulong h)
1248
1.17M
{
1249
1.17M
  return _zend_hash_index_add_or_update_i(ht, h, NULL, HASH_LOOKUP);
1250
1.17M
}
1251
1252
ZEND_API zval* ZEND_FASTCALL zend_hash_set_bucket_key(HashTable *ht, Bucket *b, zend_string *key)
1253
7.15k
{
1254
7.15k
  uint32_t nIndex;
1255
7.15k
  uint32_t idx, i;
1256
7.15k
  Bucket *p, *arData;
1257
1258
7.15k
  IS_CONSISTENT(ht);
1259
7.15k
  HT_ASSERT_RC1(ht);
1260
7.15k
  ZEND_ASSERT(!HT_IS_PACKED(ht));
1261
1262
7.15k
  (void)zend_string_hash_val(key);
1263
7.15k
  p = zend_hash_find_bucket(ht, key);
1264
7.15k
  if (UNEXPECTED(p)) {
1265
145
    return (p == b) ? &p->val : NULL;
1266
145
  }
1267
1268
7.01k
  if (!ZSTR_IS_INTERNED(key)) {
1269
244
    zend_string_addref(key);
1270
244
    HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
1271
244
  }
1272
1273
7.01k
  arData = ht->arData;
1274
1275
  /* del from hash */
1276
7.01k
  idx = HT_IDX_TO_HASH(b - arData);
1277
7.01k
  nIndex = b->h | ht->nTableMask;
1278
7.01k
  i = HT_HASH_EX(arData, nIndex);
1279
7.01k
  if (i == idx) {
1280
7.00k
    HT_HASH_EX(arData, nIndex) = Z_NEXT(b->val);
1281
7.00k
  } else {
1282
5
    p = HT_HASH_TO_BUCKET_EX(arData, i);
1283
5
    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
5
    Z_NEXT(p->val) = Z_NEXT(b->val);
1288
5
  }
1289
7.01k
  zend_string_release(b->key);
1290
1291
  /* add to hash */
1292
7.01k
  idx = b - arData;
1293
7.01k
  b->key = key;
1294
7.01k
  b->h = ZSTR_H(key);
1295
7.01k
  nIndex = b->h | ht->nTableMask;
1296
7.01k
  idx = HT_IDX_TO_HASH(idx);
1297
7.01k
  i = HT_HASH_EX(arData, nIndex);
1298
7.01k
  if (i == HT_INVALID_IDX || i < idx) {
1299
7.00k
    Z_NEXT(b->val) = i;
1300
7.00k
    HT_HASH_EX(arData, nIndex) = idx;
1301
7.00k
  } else {
1302
8
    p = HT_HASH_TO_BUCKET_EX(arData, i);
1303
8
    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
8
    Z_NEXT(b->val) = Z_NEXT(p->val);
1308
8
    Z_NEXT(p->val) = idx;
1309
8
  }
1310
7.01k
  return &b->val;
1311
7.15k
}
1312
1313
static void ZEND_FASTCALL zend_hash_do_resize(HashTable *ht)
1314
71.8k
{
1315
1316
71.8k
  IS_CONSISTENT(ht);
1317
71.8k
  HT_ASSERT_RC1(ht);
1318
1319
71.8k
  ZEND_ASSERT(!HT_IS_PACKED(ht));
1320
71.8k
  if (ht->nNumUsed > ht->nNumOfElements + (ht->nNumOfElements >> 5)) { /* additional term is there to amortize the cost of compaction */
1321
35.4k
    zend_hash_rehash(ht);
1322
36.4k
  } else if (ht->nTableSize < HT_MAX_SIZE) { /* Let's double the table size */
1323
36.4k
    void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
1324
36.4k
    uint32_t nSize = ht->nTableSize + ht->nTableSize;
1325
36.4k
    Bucket *old_buckets = ht->arData;
1326
1327
36.4k
    ZEND_ASSERT(HT_SIZE_TO_MASK(nSize));
1328
1329
36.4k
    new_data = pemalloc(HT_SIZE_EX(nSize, HT_SIZE_TO_MASK(nSize)), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
1330
36.4k
    ht->nTableSize = nSize;
1331
36.4k
    ht->nTableMask = HT_SIZE_TO_MASK(ht->nTableSize);
1332
36.4k
    HT_SET_DATA_ADDR(ht, new_data);
1333
36.4k
    memcpy(ht->arData, old_buckets, sizeof(Bucket) * ht->nNumUsed);
1334
36.4k
    pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
1335
36.4k
    zend_hash_rehash(ht);
1336
36.4k
  } 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
71.8k
}
1340
1341
ZEND_API void ZEND_FASTCALL zend_hash_rehash(HashTable *ht)
1342
272k
{
1343
272k
  Bucket *p;
1344
272k
  uint32_t nIndex, i;
1345
1346
272k
  IS_CONSISTENT(ht);
1347
1348
272k
  if (UNEXPECTED(ht->nNumOfElements == 0)) {
1349
24
    if (!(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
1350
24
      ht->nNumUsed = 0;
1351
24
      HT_HASH_RESET(ht);
1352
      /* Even if the array is empty, we still need to reset the iterator positions. */
1353
24
      ht->nInternalPointer = 0;
1354
24
      if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
1355
11
        HashTableIterator *iter = EG(ht_iterators);
1356
11
        HashTableIterator *end  = iter + EG(ht_iterators_used);
1357
22
        while (iter != end) {
1358
11
          if (iter->ht == ht) {
1359
11
            iter->pos = 0;
1360
11
          }
1361
11
          iter++;
1362
11
        }
1363
11
      }
1364
24
    }
1365
24
    return;
1366
24
  }
1367
1368
271k
  HT_HASH_RESET(ht);
1369
271k
  i = 0;
1370
271k
  p = ht->arData;
1371
271k
  if (HT_IS_WITHOUT_HOLES(ht)) {
1372
1.69M
    do {
1373
1.69M
      nIndex = p->h | ht->nTableMask;
1374
1.69M
      Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1375
1.69M
      HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(i);
1376
1.69M
      p++;
1377
1.69M
    } while (++i < ht->nNumUsed);
1378
228k
  } else {
1379
43.6k
    uint32_t old_num_used = ht->nNumUsed;
1380
4.57M
    do {
1381
4.57M
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) {
1382
43.6k
        uint32_t j = i;
1383
43.6k
        Bucket *q = p;
1384
1385
43.6k
        if (EXPECTED(!HT_HAS_ITERATORS(ht))) {
1386
609M
          while (++i < ht->nNumUsed) {
1387
609M
            p++;
1388
609M
            if (EXPECTED(Z_TYPE_INFO(p->val) != IS_UNDEF)) {
1389
176M
              ZVAL_COPY_VALUE(&q->val, &p->val);
1390
176M
              q->h = p->h;
1391
176M
              nIndex = q->h | ht->nTableMask;
1392
176M
              q->key = p->key;
1393
176M
              Z_NEXT(q->val) = HT_HASH(ht, nIndex);
1394
176M
              HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(j);
1395
176M
              if (UNEXPECTED(ht->nInternalPointer > j && ht->nInternalPointer <= i)) {
1396
2
                ht->nInternalPointer = j;
1397
2
              }
1398
176M
              q++;
1399
176M
              j++;
1400
176M
            }
1401
609M
          }
1402
43.5k
        } else {
1403
40
          uint32_t iter_pos = zend_hash_iterators_lower_pos(ht, i + 1);
1404
1405
321
          while (++i < ht->nNumUsed) {
1406
281
            p++;
1407
281
            if (EXPECTED(Z_TYPE_INFO(p->val) != IS_UNDEF)) {
1408
112
              ZVAL_COPY_VALUE(&q->val, &p->val);
1409
112
              q->h = p->h;
1410
112
              nIndex = q->h | ht->nTableMask;
1411
112
              q->key = p->key;
1412
112
              Z_NEXT(q->val) = HT_HASH(ht, nIndex);
1413
112
              HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(j);
1414
112
              if (UNEXPECTED(ht->nInternalPointer > j && ht->nInternalPointer <= i)) {
1415
2
                ht->nInternalPointer = j;
1416
2
              }
1417
112
              if (UNEXPECTED(i >= iter_pos)) {
1418
35
                do {
1419
35
                  zend_hash_iterators_update(ht, iter_pos, j);
1420
35
                  iter_pos = zend_hash_iterators_lower_pos(ht, iter_pos + 1);
1421
35
                } while (iter_pos < i);
1422
35
              }
1423
112
              q++;
1424
112
              j++;
1425
112
            }
1426
281
          }
1427
40
        }
1428
43.6k
        ht->nNumUsed = j;
1429
43.6k
        break;
1430
43.6k
      }
1431
4.52M
      nIndex = p->h | ht->nTableMask;
1432
4.52M
      Z_NEXT(p->val) = HT_HASH(ht, nIndex);
1433
4.52M
      HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(i);
1434
4.52M
      p++;
1435
4.52M
    } 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
43.6k
    if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
1440
40
      _zend_hash_iterators_update(ht, old_num_used, ht->nNumUsed);
1441
40
    }
1442
43.6k
  }
1443
271k
}
1444
1445
static zend_always_inline void zend_hash_iterators_clamp_max(const HashTable *ht, uint32_t max)
1446
61.0M
{
1447
61.0M
  if (UNEXPECTED(HT_HAS_ITERATORS(ht))) {
1448
829
    HashTableIterator *iter = EG(ht_iterators);
1449
829
    const HashTableIterator *end = iter + EG(ht_iterators_used);
1450
1.67k
    while (iter != end) {
1451
845
      if (iter->ht == ht) {
1452
829
        iter->pos = MIN(iter->pos, max);
1453
829
      }
1454
845
      iter++;
1455
845
    }
1456
829
  }
1457
61.0M
}
1458
1459
static zend_always_inline void _zend_hash_packed_del_val(HashTable *ht, uint32_t idx, zval *zv)
1460
11.0k
{
1461
11.0k
  idx = HT_HASH_TO_IDX(idx);
1462
11.0k
  ht->nNumOfElements--;
1463
11.0k
  if (ht->nNumUsed - 1 == idx) {
1464
22.4k
    do {
1465
22.4k
      ht->nNumUsed--;
1466
22.4k
    } while (ht->nNumUsed > 0 && (UNEXPECTED(Z_TYPE(ht->arPacked[ht->nNumUsed-1]) == IS_UNDEF)));
1467
9.81k
    ht->nInternalPointer = MIN(ht->nInternalPointer, ht->nNumUsed);
1468
9.81k
    zend_hash_iterators_clamp_max(ht, ht->nNumUsed);
1469
9.81k
  }
1470
11.0k
  if (ht->pDestructor) {
1471
11.0k
    zval tmp;
1472
11.0k
    ZVAL_COPY_VALUE(&tmp, zv);
1473
11.0k
    ZVAL_UNDEF(zv);
1474
11.0k
    ht->pDestructor(&tmp);
1475
11.0k
  } else {
1476
0
    ZVAL_UNDEF(zv);
1477
0
  }
1478
11.0k
}
1479
1480
static zend_always_inline void _zend_hash_del_el_ex(HashTable *ht, uint32_t idx, Bucket *p, Bucket *prev)
1481
822M
{
1482
822M
  if (prev) {
1483
75.8M
    Z_NEXT(prev->val) = Z_NEXT(p->val);
1484
746M
  } else {
1485
746M
    HT_HASH(ht, p->h | ht->nTableMask) = Z_NEXT(p->val);
1486
746M
  }
1487
822M
  idx = HT_HASH_TO_IDX(idx);
1488
822M
  ht->nNumOfElements--;
1489
822M
  if (ht->nNumUsed - 1 == idx) {
1490
209M
    do {
1491
209M
      ht->nNumUsed--;
1492
209M
    } while (ht->nNumUsed > 0 && (UNEXPECTED(Z_TYPE(ht->arData[ht->nNumUsed-1].val) == IS_UNDEF)));
1493
61.0M
    ht->nInternalPointer = MIN(ht->nInternalPointer, ht->nNumUsed);
1494
61.0M
    zend_hash_iterators_clamp_max(ht, ht->nNumUsed);
1495
61.0M
  }
1496
822M
  if (ht->pDestructor) {
1497
1.34M
    zval tmp;
1498
1.34M
    ZVAL_COPY_VALUE(&tmp, &p->val);
1499
1.34M
    ZVAL_UNDEF(&p->val);
1500
1.34M
    ht->pDestructor(&tmp);
1501
821M
  } else {
1502
821M
    ZVAL_UNDEF(&p->val);
1503
821M
  }
1504
822M
}
1505
1506
static zend_always_inline void _zend_hash_del_el(HashTable *ht, uint32_t idx, Bucket *p)
1507
822M
{
1508
822M
  Bucket *prev = NULL;
1509
822M
  uint32_t nIndex;
1510
822M
  uint32_t i;
1511
1512
822M
  nIndex = p->h | ht->nTableMask;
1513
822M
  i = HT_HASH(ht, nIndex);
1514
1515
822M
  if (i != idx) {
1516
75.8M
    prev = HT_HASH_TO_BUCKET(ht, i);
1517
82.6M
    while (Z_NEXT(prev->val) != idx) {
1518
6.78M
      i = Z_NEXT(prev->val);
1519
6.78M
      prev = HT_HASH_TO_BUCKET(ht, i);
1520
6.78M
    }
1521
75.8M
  }
1522
1523
822M
  if (p->key) {
1524
1.32M
    zend_string_release(p->key);
1525
1.32M
    p->key = NULL;
1526
1.32M
  }
1527
822M
  _zend_hash_del_el_ex(ht, idx, p, prev);
1528
822M
}
1529
1530
ZEND_API void ZEND_FASTCALL zend_hash_packed_del_val(HashTable *ht, zval *zv)
1531
1.43k
{
1532
1.43k
  IS_CONSISTENT(ht);
1533
1.43k
  HT_ASSERT_RC1(ht);
1534
1.43k
  ZEND_ASSERT(HT_IS_PACKED(ht));
1535
1.43k
  _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(zv - ht->arPacked), zv);
1536
1.43k
}
1537
1538
1539
ZEND_API void ZEND_FASTCALL zend_hash_del_bucket(HashTable *ht, Bucket *p)
1540
821M
{
1541
821M
  IS_CONSISTENT(ht);
1542
821M
  HT_ASSERT_RC1(ht);
1543
821M
  ZEND_ASSERT(!HT_IS_PACKED(ht));
1544
821M
  _zend_hash_del_el(ht, HT_IDX_TO_HASH(p - ht->arData), p);
1545
821M
}
1546
1547
ZEND_API zend_result ZEND_FASTCALL zend_hash_del(HashTable *ht, zend_string *key)
1548
246k
{
1549
246k
  zend_ulong h;
1550
246k
  uint32_t nIndex;
1551
246k
  uint32_t idx;
1552
246k
  Bucket *p;
1553
246k
  Bucket *prev = NULL;
1554
1555
246k
  IS_CONSISTENT(ht);
1556
246k
  HT_ASSERT_RC1(ht);
1557
1558
246k
  h = zend_string_hash_val(key);
1559
246k
  nIndex = h | ht->nTableMask;
1560
1561
246k
  idx = HT_HASH(ht, nIndex);
1562
252k
  while (idx != HT_INVALID_IDX) {
1563
199k
    p = HT_HASH_TO_BUCKET(ht, idx);
1564
199k
    if ((p->key == key) ||
1565
6.46k
      (p->h == h &&
1566
1.81k
         p->key &&
1567
193k
         zend_string_equal_content(p->key, key))) {
1568
193k
      zend_string_release(p->key);
1569
193k
      p->key = NULL;
1570
193k
      _zend_hash_del_el_ex(ht, idx, p, prev);
1571
193k
      return SUCCESS;
1572
193k
    }
1573
5.95k
    prev = p;
1574
5.95k
    idx = Z_NEXT(p->val);
1575
5.95k
  }
1576
52.8k
  return FAILURE;
1577
246k
}
1578
1579
ZEND_API zend_result ZEND_FASTCALL zend_hash_del_ind(HashTable *ht, zend_string *key)
1580
187
{
1581
187
  zend_ulong h;
1582
187
  uint32_t nIndex;
1583
187
  uint32_t idx;
1584
187
  Bucket *p;
1585
187
  Bucket *prev = NULL;
1586
1587
187
  IS_CONSISTENT(ht);
1588
187
  HT_ASSERT_RC1(ht);
1589
1590
187
  h = zend_string_hash_val(key);
1591
187
  nIndex = h | ht->nTableMask;
1592
1593
187
  idx = HT_HASH(ht, nIndex);
1594
187
  while (idx != HT_INVALID_IDX) {
1595
127
    p = HT_HASH_TO_BUCKET(ht, idx);
1596
127
    if ((p->key == key) ||
1597
1
      (p->h == h &&
1598
1
         p->key &&
1599
127
         zend_string_equal_content(p->key, key))) {
1600
127
      if (Z_TYPE(p->val) == IS_INDIRECT) {
1601
45
        zval *data = Z_INDIRECT(p->val);
1602
1603
45
        if (UNEXPECTED(Z_TYPE_P(data) == IS_UNDEF)) {
1604
0
          return FAILURE;
1605
45
        } else {
1606
45
          if (ht->pDestructor) {
1607
45
            zval tmp;
1608
45
            ZVAL_COPY_VALUE(&tmp, data);
1609
45
            ZVAL_UNDEF(data);
1610
45
            ht->pDestructor(&tmp);
1611
45
          } else {
1612
0
            ZVAL_UNDEF(data);
1613
0
          }
1614
45
          HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
1615
45
        }
1616
82
      } else {
1617
82
        zend_string_release(p->key);
1618
82
        p->key = NULL;
1619
82
        _zend_hash_del_el_ex(ht, idx, p, prev);
1620
82
      }
1621
127
      return SUCCESS;
1622
127
    }
1623
0
    prev = p;
1624
0
    idx = Z_NEXT(p->val);
1625
0
  }
1626
60
  return FAILURE;
1627
187
}
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
1.24k
{
1676
1.24k
  zend_ulong h;
1677
1.24k
  uint32_t nIndex;
1678
1.24k
  uint32_t idx;
1679
1.24k
  Bucket *p;
1680
1.24k
  Bucket *prev = NULL;
1681
1682
1.24k
  IS_CONSISTENT(ht);
1683
1.24k
  HT_ASSERT_RC1(ht);
1684
1685
1.24k
  h = zend_inline_hash_func(str, len);
1686
1.24k
  nIndex = h | ht->nTableMask;
1687
1688
1.24k
  idx = HT_HASH(ht, nIndex);
1689
1.39k
  while (idx != HT_INVALID_IDX) {
1690
1.32k
    p = HT_HASH_TO_BUCKET(ht, idx);
1691
1.32k
    if ((p->h == h)
1692
1.18k
       && p->key
1693
1.18k
       && zend_string_equals_cstr(p->key, str, len)) {
1694
1.18k
      zend_string_release(p->key);
1695
1.18k
      p->key = NULL;
1696
1.18k
      _zend_hash_del_el_ex(ht, idx, p, prev);
1697
1.18k
      return SUCCESS;
1698
1.18k
    }
1699
144
    prev = p;
1700
144
    idx = Z_NEXT(p->val);
1701
144
  }
1702
64
  return FAILURE;
1703
1.24k
}
1704
1705
ZEND_API zend_result ZEND_FASTCALL zend_hash_index_del(HashTable *ht, zend_ulong h)
1706
174k
{
1707
174k
  uint32_t nIndex;
1708
174k
  uint32_t idx;
1709
174k
  Bucket *p;
1710
174k
  Bucket *prev = NULL;
1711
1712
174k
  IS_CONSISTENT(ht);
1713
174k
  HT_ASSERT_RC1(ht);
1714
1715
174k
  if (HT_IS_PACKED(ht)) {
1716
8.91k
    if (h < ht->nNumUsed) {
1717
8.87k
      zval *zv = ht->arPacked + h;
1718
8.87k
      if (Z_TYPE_P(zv) != IS_UNDEF) {
1719
8.85k
        _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(h), zv);
1720
8.85k
        return SUCCESS;
1721
8.85k
      }
1722
8.87k
    }
1723
64
    return FAILURE;
1724
8.91k
  }
1725
165k
  nIndex = h | ht->nTableMask;
1726
1727
165k
  idx = HT_HASH(ht, nIndex);
1728
761k
  while (idx != HT_INVALID_IDX) {
1729
648k
    p = HT_HASH_TO_BUCKET(ht, idx);
1730
648k
    if ((p->h == h) && (p->key == NULL)) {
1731
52.6k
      _zend_hash_del_el_ex(ht, idx, p, prev);
1732
52.6k
      return SUCCESS;
1733
52.6k
    }
1734
596k
    prev = p;
1735
596k
    idx = Z_NEXT(p->val);
1736
596k
  }
1737
112k
  return FAILURE;
1738
165k
}
1739
1740
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
1741
2.57M
{
1742
2.57M
  IS_CONSISTENT(ht);
1743
2.57M
  HT_ASSERT(ht, GC_REFCOUNT(ht) <= 1);
1744
1745
2.57M
  if (ht->nNumUsed) {
1746
710k
    if (HT_IS_PACKED(ht)) {
1747
160k
      if (ht->pDestructor) {
1748
159k
        zval *zv = ht->arPacked;
1749
159k
        zval *end = zv + ht->nNumUsed;
1750
1751
159k
        SET_INCONSISTENT(HT_IS_DESTROYING);
1752
159k
        if (HT_IS_WITHOUT_HOLES(ht)) {
1753
713k
          do {
1754
713k
            ht->pDestructor(zv);
1755
713k
          } while (++zv != end);
1756
158k
        } else {
1757
2.61k
          do {
1758
2.61k
            if (EXPECTED(Z_TYPE_P(zv) != IS_UNDEF)) {
1759
788
              ht->pDestructor(zv);
1760
788
            }
1761
2.61k
          } while (++zv != end);
1762
644
        }
1763
159k
        SET_INCONSISTENT(HT_DESTROYED);
1764
159k
      }
1765
160k
      zend_hash_iterators_remove(ht);
1766
549k
    } else {
1767
549k
      Bucket *p = ht->arData;
1768
549k
      Bucket *end = p + ht->nNumUsed;
1769
1770
549k
      if (ht->pDestructor) {
1771
39.0k
        SET_INCONSISTENT(HT_IS_DESTROYING);
1772
1773
39.0k
        if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
1774
22.9k
          if (HT_IS_WITHOUT_HOLES(ht)) {
1775
88.3k
            do {
1776
88.3k
              ht->pDestructor(&p->val);
1777
88.3k
            } while (++p != end);
1778
22.9k
          } 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
22.9k
        } else if (HT_IS_WITHOUT_HOLES(ht)) {
1786
23.5k
          do {
1787
23.5k
            ht->pDestructor(&p->val);
1788
23.5k
            if (EXPECTED(p->key)) {
1789
23.1k
              zend_string_release(p->key);
1790
23.1k
            }
1791
23.5k
          } while (++p != end);
1792
16.1k
        } 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
39.0k
        SET_INCONSISTENT(HT_DESTROYED);
1804
510k
      } else {
1805
510k
        if (!HT_HAS_STATIC_KEYS_ONLY(ht)) {
1806
1.91M
          do {
1807
1.91M
            if (EXPECTED(p->key)) {
1808
1.91M
              zend_string_release(p->key);
1809
1.91M
            }
1810
1.91M
          } while (++p != end);
1811
308k
        }
1812
510k
      }
1813
549k
      zend_hash_iterators_remove(ht);
1814
549k
    }
1815
1.86M
  } else if (EXPECTED(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
1816
1.73M
    return;
1817
1.73M
  }
1818
834k
  pefree(HT_GET_DATA_ADDR(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
1819
834k
}
1820
1821
ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht)
1822
6.27M
{
1823
6.27M
  IS_CONSISTENT(ht);
1824
6.27M
  HT_ASSERT(ht, GC_REFCOUNT(ht) <= 1);
1825
1826
  /* break possible cycles */
1827
6.27M
  GC_REMOVE_FROM_BUFFER(ht);
1828
6.27M
  GC_TYPE_INFO(ht) = GC_NULL /*???| (GC_WHITE << 16)*/;
1829
1830
6.27M
  if (ht->nNumUsed) {
1831
    /* In some rare cases destructors of regular arrays may be changed */
1832
4.37M
    if (UNEXPECTED(ht->pDestructor != ZVAL_PTR_DTOR)) {
1833
2.52k
      zend_hash_destroy(ht);
1834
2.52k
      goto free_ht;
1835
2.52k
    }
1836
1837
4.37M
    SET_INCONSISTENT(HT_IS_DESTROYING);
1838
1839
4.37M
    if (HT_IS_PACKED(ht)) {
1840
1.94M
      zval *zv = ht->arPacked;
1841
1.94M
      zval *end = zv + ht->nNumUsed;
1842
1843
19.5M
      do {
1844
19.5M
        i_zval_ptr_dtor(zv);
1845
19.5M
      } while (++zv != end);
1846
2.42M
    } else {
1847
2.42M
      Bucket *p = ht->arData;
1848
2.42M
      Bucket *end = p + ht->nNumUsed;
1849
1850
2.42M
      if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
1851
7.90M
        do {
1852
7.90M
          i_zval_ptr_dtor(&p->val);
1853
7.90M
        } while (++p != end);
1854
1.90M
      } else if (HT_IS_WITHOUT_HOLES(ht)) {
1855
2.02M
        do {
1856
2.02M
          i_zval_ptr_dtor(&p->val);
1857
2.02M
          if (EXPECTED(p->key)) {
1858
1.78M
            zend_string_release_ex(p->key, 0);
1859
1.78M
          }
1860
2.02M
        } while (++p != end);
1861
524k
      } else {
1862
10
        do {
1863
10
          if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1864
5
            i_zval_ptr_dtor(&p->val);
1865
5
            if (EXPECTED(p->key)) {
1866
5
              zend_string_release_ex(p->key, 0);
1867
5
            }
1868
5
          }
1869
10
        } while (++p != end);
1870
5
      }
1871
2.42M
    }
1872
4.37M
  } else if (EXPECTED(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
1873
1.47M
    goto free_ht;
1874
1.47M
  }
1875
4.80M
  SET_INCONSISTENT(HT_DESTROYED);
1876
4.80M
  efree(HT_GET_DATA_ADDR(ht));
1877
6.27M
free_ht:
1878
6.27M
  zend_hash_iterators_remove(ht);
1879
6.27M
  FREE_HASHTABLE(ht);
1880
6.27M
}
1881
1882
ZEND_API void ZEND_FASTCALL zend_hash_clean(HashTable *ht)
1883
655k
{
1884
655k
  IS_CONSISTENT(ht);
1885
655k
  HT_ASSERT_RC1(ht);
1886
1887
655k
  if (ht->nNumUsed) {
1888
198k
    if (HT_IS_PACKED(ht)) {
1889
2.13k
      zval *zv = ht->arPacked;
1890
2.13k
      zval *end = zv + ht->nNumUsed;
1891
1892
2.13k
      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
196k
    } else {
1908
196k
      Bucket *p = ht->arData;
1909
196k
      Bucket *end = p + ht->nNumUsed;
1910
1911
196k
      if (ht->pDestructor) {
1912
426
        if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
1913
426
          if (HT_IS_WITHOUT_HOLES(ht)) {
1914
755
            do {
1915
755
              ht->pDestructor(&p->val);
1916
755
            } while (++p != end);
1917
401
          } else {
1918
105
            do {
1919
105
              if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1920
70
                ht->pDestructor(&p->val);
1921
70
              }
1922
105
            } while (++p != end);
1923
25
          }
1924
426
        } 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
195k
      } else {
1942
195k
        if (!HT_HAS_STATIC_KEYS_ONLY(ht)) {
1943
570k
          do {
1944
570k
            if (EXPECTED(p->key)) {
1945
487k
              zend_string_release(p->key);
1946
487k
            }
1947
570k
          } while (++p != end);
1948
88.4k
        }
1949
195k
      }
1950
196k
      HT_HASH_RESET(ht);
1951
196k
    }
1952
198k
  }
1953
655k
  ht->nNumUsed = 0;
1954
655k
  ht->nNumOfElements = 0;
1955
655k
  ht->nNextFreeElement = ZEND_LONG_MIN;
1956
655k
  ht->nInternalPointer = 0;
1957
655k
}
1958
1959
ZEND_API void ZEND_FASTCALL zend_symtable_clean(HashTable *ht)
1960
2.36k
{
1961
2.36k
  Bucket *p, *end;
1962
1963
2.36k
  IS_CONSISTENT(ht);
1964
2.36k
  HT_ASSERT_RC1(ht);
1965
1966
2.36k
  if (ht->nNumUsed) {
1967
2.19k
    ZEND_ASSERT(!HT_IS_PACKED(ht));
1968
2.19k
    p = ht->arData;
1969
2.19k
    end = p + ht->nNumUsed;
1970
2.19k
    if (HT_HAS_STATIC_KEYS_ONLY(ht)) {
1971
7.01k
      do {
1972
7.01k
        i_zval_ptr_dtor(&p->val);
1973
7.01k
      } while (++p != end);
1974
1.73k
    } else if (HT_IS_WITHOUT_HOLES(ht)) {
1975
4.35k
      do {
1976
4.35k
        i_zval_ptr_dtor(&p->val);
1977
4.35k
        if (EXPECTED(p->key)) {
1978
4.35k
          zend_string_release(p->key);
1979
4.35k
        }
1980
4.35k
      } while (++p != end);
1981
456
    } 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
2.19k
    HT_HASH_RESET(ht);
1992
2.19k
  }
1993
2.36k
  ht->nNumUsed = 0;
1994
1.96k
  ht->nNumOfElements = 0;
1995
1.96k
  ht->nNextFreeElement = ZEND_LONG_MIN;
1996
1.96k
  ht->nInternalPointer = 0;
1997
1.96k
}
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
495k
{
2030
495k
  uint32_t idx;
2031
2032
495k
  IS_CONSISTENT(ht);
2033
495k
  HT_ASSERT_RC1(ht);
2034
2035
495k
  idx = ht->nNumUsed;
2036
495k
  if (HT_IS_PACKED(ht)) {
2037
6.21k
    zval *zv = ht->arPacked + ht->nNumUsed;
2038
2039
7.13k
    while (idx > 0) {
2040
921
      idx--;
2041
921
      zv--;
2042
921
      if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2043
800
      _zend_hash_packed_del_val(ht, HT_IDX_TO_HASH(idx), zv);
2044
800
    }
2045
489k
  } else {
2046
489k
    Bucket *p = ht->arData + ht->nNumUsed;
2047
2048
1.76M
    while (idx > 0) {
2049
1.27M
      idx--;
2050
1.27M
      p--;
2051
1.27M
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2052
1.25M
      _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2053
1.25M
    }
2054
489k
  }
2055
2056
495k
  if (!(HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED)) {
2057
254k
    pefree(HT_GET_DATA_ADDR(ht), GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
2058
254k
  }
2059
2060
495k
  SET_INCONSISTENT(HT_DESTROYED);
2061
495k
}
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
251
{
2074
251
  uint32_t idx;
2075
251
  int result;
2076
2077
251
  IS_CONSISTENT(ht);
2078
251
  if (HT_IS_PACKED(ht)) {
2079
897
    for (idx = 0; idx < ht->nNumUsed; idx++) {
2080
662
      zval *zv = ht->arPacked + idx;
2081
2082
662
      if (UNEXPECTED(Z_TYPE_P(zv) == IS_UNDEF)) continue;
2083
662
      result = apply_func(zv);
2084
2085
662
      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
662
      if (result & ZEND_HASH_APPLY_STOP) {
2090
0
        break;
2091
0
      }
2092
662
    }
2093
235
  } else {
2094
224
    for (idx = 0; idx < ht->nNumUsed; idx++) {
2095
208
      Bucket *p = ht->arData + idx;
2096
2097
208
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2098
208
      result = apply_func(&p->val);
2099
2100
208
      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
208
      if (result & ZEND_HASH_APPLY_STOP) {
2105
0
        break;
2106
0
      }
2107
208
    }
2108
16
  }
2109
251
}
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
273k
{
2207
273k
  uint32_t idx;
2208
273k
  int result;
2209
2210
273k
  IS_CONSISTENT(ht);
2211
2212
273k
  idx = ht->nNumUsed;
2213
273k
  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
273k
  } else {
2232
273k
    Bucket *p;
2233
2234
1.88M
    while (idx > 0) {
2235
1.60M
      idx--;
2236
1.60M
      p = ht->arData + idx;
2237
1.60M
      if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2238
2239
1.56M
      result = apply_func(&p->val);
2240
2241
1.56M
      if (result & ZEND_HASH_APPLY_REMOVE) {
2242
35.0k
        HT_ASSERT_RC1(ht);
2243
35.0k
        _zend_hash_del_el(ht, HT_IDX_TO_HASH(idx), p);
2244
35.0k
      }
2245
1.56M
      if (result & ZEND_HASH_APPLY_STOP) {
2246
49
        break;
2247
49
      }
2248
1.56M
    }
2249
273k
  }
2250
273k
}
2251
2252
2253
ZEND_API void ZEND_FASTCALL zend_hash_copy(HashTable *target, const HashTable *source, copy_ctor_func_t pCopyConstructor)
2254
294k
{
2255
294k
  uint32_t idx;
2256
294k
  zval *new_entry, *data;
2257
2258
294k
  IS_CONSISTENT(source);
2259
294k
  IS_CONSISTENT(target);
2260
294k
  HT_ASSERT_RC1(target);
2261
2262
294k
  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
2.16M
  for (idx = 0; idx < source->nNumUsed; idx++) {
2276
1.87M
    Bucket *p = source->arData + idx;
2277
2278
1.87M
    if (UNEXPECTED(Z_TYPE(p->val) == IS_UNDEF)) continue;
2279
2280
    /* INDIRECT element may point to UNDEF-ined slots */
2281
1.87M
    data = &p->val;
2282
1.87M
    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
1.87M
    if (p->key) {
2289
1.87M
      new_entry = zend_hash_update(target, p->key, data);
2290
1.87M
    } else {
2291
25
      new_entry = zend_hash_index_update(target, p->h, data);
2292
25
    }
2293
1.87M
    if (pCopyConstructor) {
2294
16
      pCopyConstructor(new_entry);
2295
16
    }
2296
1.87M
  }
2297
294k
}
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
47.3k
{
2302
47.3k
  if (with_holes) {
2303
1.79k
    if (!packed && Z_TYPE_INFO_P(data) == IS_INDIRECT) {
2304
0
      data = Z_INDIRECT_P(data);
2305
0
    }
2306
1.79k
    if (UNEXPECTED(Z_TYPE_INFO_P(data) == IS_UNDEF)) {
2307
1.01k
      return 0;
2308
1.01k
    }
2309
45.5k
  } else if (!packed) {
2310
    /* INDIRECT element may point to UNDEF-ined slots */
2311
28.0k
    if (Z_TYPE_INFO_P(data) == IS_INDIRECT) {
2312
10.6k
      data = Z_INDIRECT_P(data);
2313
10.6k
      if (UNEXPECTED(Z_TYPE_INFO_P(data) == IS_UNDEF)) {
2314
7.34k
        return 0;
2315
7.34k
      }
2316
10.6k
    }
2317
28.0k
  }
2318
2319
38.9k
  do {
2320
38.9k
    if (Z_OPT_REFCOUNTED_P(data)) {
2321
17.1k
      if (Z_ISREF_P(data) && Z_REFCOUNT_P(data) == 1 &&
2322
117
          (Z_TYPE_P(Z_REFVAL_P(data)) != IS_ARRAY ||
2323
104
            Z_ARRVAL_P(Z_REFVAL_P(data)) != source)) {
2324
104
        data = Z_REFVAL_P(data);
2325
104
        if (!Z_OPT_REFCOUNTED_P(data)) {
2326
104
          break;
2327
104
        }
2328
104
      }
2329
17.0k
      Z_ADDREF_P(data);
2330
17.0k
    }
2331
38.9k
  } while (0);
2332
38.9k
  ZVAL_COPY_VALUE(dest, data);
2333
2334
38.9k
  return 1;
2335
47.3k
}
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
28.1k
{
2339
28.1k
  if (!zend_array_dup_value(source, &p->val, &q->val, packed, with_holes)) {
2340
7.36k
    return 0;
2341
7.36k
  }
2342
2343
20.7k
  if (!packed) {
2344
20.7k
    uint32_t nIndex;
2345
2346
20.7k
    q->h = p->h;
2347
20.7k
    q->key = p->key;
2348
20.7k
    if (!static_keys && q->key) {
2349
6.31k
      zend_string_addref(q->key);
2350
6.31k
    }
2351
2352
20.7k
    nIndex = q->h | target->nTableMask;
2353
20.7k
    Z_NEXT(q->val) = HT_HASH(target, nIndex);
2354
20.7k
    HT_HASH(target, nIndex) = HT_IDX_TO_HASH(idx);
2355
20.7k
  }
2356
20.7k
  return 1;
2357
28.1k
}
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
483
static void zend_array_dup_ht_iterators(const HashTable *source, HashTable *target) {
2361
483
  uint32_t iter_index = 0;
2362
483
  uint32_t end_index = EG(ht_iterators_used);
2363
2364
13.8k
  while (iter_index != end_index) {
2365
13.3k
    HashTableIterator *iter = &EG(ht_iterators)[iter_index];
2366
13.3k
    if (iter->ht == source) {
2367
483
      uint32_t copy_idx = zend_hash_iterator_add(target, iter->pos);
2368
      /* Refetch iter because the memory may be reallocated. */
2369
483
      iter = &EG(ht_iterators)[iter_index];
2370
483
      HashTableIterator *copy_iter = EG(ht_iterators) + copy_idx;
2371
483
      copy_iter->next_copy = iter->next_copy;
2372
483
      iter->next_copy = copy_idx;
2373
483
    }
2374
13.3k
    iter_index++;
2375
13.3k
  }
2376
483
}
2377
2378
static zend_always_inline void zend_array_dup_packed_elements(const HashTable *source, HashTable *target, bool with_holes)
2379
3.03k
{
2380
3.03k
  zval *p = source->arPacked;
2381
3.03k
  zval *q = target->arPacked;
2382
3.03k
  const zval *end = p + source->nNumUsed;
2383
2384
19.2k
  do {
2385
19.2k
    if (!zend_array_dup_value(source, p, q, true, with_holes)) {
2386
1.00k
      if (with_holes) {
2387
1.00k
        ZVAL_UNDEF(q);
2388
1.00k
      }
2389
1.00k
    }
2390
19.2k
    p++; q++;
2391
19.2k
  } while (p != end);
2392
2393
3.03k
  if (UNEXPECTED(HT_HAS_ITERATORS(source))) {
2394
429
    zend_array_dup_ht_iterators(source, target);
2395
429
  }
2396
3.03k
}
2397
2398
static zend_always_inline uint32_t zend_array_dup_elements(const HashTable *source, HashTable *target, bool static_keys, bool with_holes)
2399
3.68k
{
2400
3.68k
  uint32_t idx = 0;
2401
3.68k
  Bucket *p = source->arData;
2402
3.68k
  Bucket *q = target->arData;
2403
3.68k
  const Bucket *end = p + source->nNumUsed;
2404
2405
3.68k
  if (UNEXPECTED(HT_HAS_ITERATORS(source))) {
2406
54
    zend_array_dup_ht_iterators(source, target);
2407
54
  }
2408
2409
21.1k
  do {
2410
21.1k
    if (!zend_array_dup_element(source, target, idx, p, q, false, static_keys, with_holes)) {
2411
1.07k
      uint32_t target_idx = idx;
2412
2413
1.07k
      idx++; p++;
2414
1.07k
      if (EXPECTED(!HT_HAS_ITERATORS(target))) {
2415
8.02k
        while (p != end) {
2416
6.96k
          if (zend_array_dup_element(source, target, target_idx, p, q, false, static_keys, with_holes)) {
2417
679
            if (source->nInternalPointer == idx) {
2418
0
              target->nInternalPointer = target_idx;
2419
0
            }
2420
679
            target_idx++; q++;
2421
679
          }
2422
6.96k
          idx++; p++;
2423
6.96k
        }
2424
1.06k
      } else {
2425
10
        target->nNumUsed = source->nNumUsed;
2426
10
        uint32_t iter_pos = zend_hash_iterators_lower_pos(target, idx);
2427
2428
30
        while (p != end) {
2429
20
          if (zend_array_dup_element(source, target, target_idx, p, q, false, static_keys, with_holes)) {
2430
15
            if (source->nInternalPointer == idx) {
2431
0
              target->nInternalPointer = target_idx;
2432
0
            }
2433
15
            if (UNEXPECTED(idx >= iter_pos)) {
2434
5
              do {
2435
5
                zend_hash_iterators_update(target, iter_pos, target_idx);
2436
5
                iter_pos = zend_hash_iterators_lower_pos(target, iter_pos + 1);
2437
5
              } while (iter_pos < idx);
2438
5
            }
2439
15
            target_idx++; q++;
2440
15
          }
2441
20
          idx++; p++;
2442
20
        }
2443
10
      }
2444
1.07k
      return target_idx;
2445
1.07k
    }
2446
20.0k
    idx++; p++; q++;
2447
20.0k
  } while (p != end);
2448
2.60k
  return idx;
2449
3.68k
}
2450
2451
ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(const HashTable *source)
2452
21.5k
{
2453
21.5k
  uint32_t idx;
2454
21.5k
  HashTable *target;
2455
2456
21.5k
  IS_CONSISTENT(source);
2457
2458
21.5k
  ALLOC_HASHTABLE(target);
2459
21.5k
  GC_SET_REFCOUNT(target, 1);
2460
21.5k
  GC_TYPE_INFO(target) = GC_ARRAY;
2461
2462
21.5k
  target->pDestructor = ZVAL_PTR_DTOR;
2463
2464
21.5k
  if (source->nNumOfElements == 0) {
2465
7.31k
    HT_FLAGS(target) = HASH_FLAG_UNINITIALIZED;
2466
7.31k
    target->nTableMask = HT_MIN_MASK;
2467
7.31k
    target->nNumUsed = 0;
2468
7.31k
    target->nNumOfElements = 0;
2469
7.31k
    target->nNextFreeElement = source->nNextFreeElement;
2470
7.31k
    target->nInternalPointer = 0;
2471
7.31k
    target->nTableSize = HT_MIN_SIZE;
2472
7.31k
    HT_SET_DATA_ADDR(target, &uninitialized_bucket);
2473
14.2k
  } else if (GC_FLAGS(source) & IS_ARRAY_IMMUTABLE) {
2474
7.51k
    ZEND_ASSERT(!(HT_FLAGS(source) & HASH_FLAG_HAS_EMPTY_IND));
2475
7.51k
    HT_FLAGS(target) = HT_FLAGS(source) & HASH_FLAG_MASK;
2476
7.51k
    target->nTableMask = source->nTableMask;
2477
7.51k
    target->nNumUsed = source->nNumUsed;
2478
7.51k
    target->nNumOfElements = source->nNumOfElements;
2479
7.51k
    target->nNextFreeElement = source->nNextFreeElement;
2480
7.51k
    target->nTableSize = source->nTableSize;
2481
7.51k
    if (HT_IS_PACKED(source)) {
2482
3.83k
      HT_SET_DATA_ADDR(target, emalloc(HT_PACKED_SIZE(target)));
2483
3.83k
      target->nInternalPointer = source->nInternalPointer;
2484
3.83k
      memcpy(HT_GET_DATA_ADDR(target), HT_GET_DATA_ADDR(source), HT_PACKED_USED_SIZE(source));
2485
3.83k
    } else {
2486
3.68k
      HT_SET_DATA_ADDR(target, emalloc(HT_SIZE(target)));
2487
3.68k
      target->nInternalPointer = source->nInternalPointer;
2488
3.68k
      memcpy(HT_GET_DATA_ADDR(target), HT_GET_DATA_ADDR(source), HT_USED_SIZE(source));
2489
3.68k
    }
2490
7.51k
  } else if (HT_IS_PACKED(source)) {
2491
3.03k
    ZEND_ASSERT(!(HT_FLAGS(source) & HASH_FLAG_HAS_EMPTY_IND));
2492
3.03k
    HT_FLAGS(target) = HT_FLAGS(source) & HASH_FLAG_MASK;
2493
3.03k
    target->nTableMask = HT_MIN_MASK;
2494
3.03k
    target->nNumUsed = source->nNumUsed;
2495
3.03k
    target->nNumOfElements = source->nNumOfElements;
2496
3.03k
    target->nNextFreeElement = source->nNextFreeElement;
2497
3.03k
    target->nTableSize = source->nTableSize;
2498
3.03k
    HT_SET_DATA_ADDR(target, emalloc(HT_PACKED_SIZE_EX(target->nTableSize, HT_MIN_MASK)));
2499
3.03k
    target->nInternalPointer =
2500
3.03k
      (source->nInternalPointer < source->nNumUsed) ?
2501
3.03k
        source->nInternalPointer : 0;
2502
2503
3.03k
    HT_HASH_RESET_PACKED(target);
2504
2505
3.03k
    if (HT_IS_WITHOUT_HOLES(target)) {
2506
2.83k
      zend_array_dup_packed_elements(source, target, false);
2507
2.83k
    } else {
2508
204
      zend_array_dup_packed_elements(source, target, true);
2509
204
    }
2510
3.68k
  } else {
2511
    /* Indirects are removed during duplication, remove HASH_FLAG_HAS_EMPTY_IND accordingly. */
2512
3.68k
    HT_FLAGS(target) = HT_FLAGS(source) & (HASH_FLAG_MASK & ~HASH_FLAG_HAS_EMPTY_IND);
2513
3.68k
    target->nTableMask = source->nTableMask;
2514
3.68k
    target->nNextFreeElement = source->nNextFreeElement;
2515
3.68k
    target->nInternalPointer =
2516
3.68k
      (source->nInternalPointer < source->nNumUsed) ?
2517
3.68k
        source->nInternalPointer : 0;
2518
2519
3.68k
    target->nTableSize = source->nTableSize;
2520
3.68k
    HT_SET_DATA_ADDR(target, emalloc(HT_SIZE(target)));
2521
3.68k
    HT_HASH_RESET(target);
2522
2523
3.68k
    if (HT_HAS_STATIC_KEYS_ONLY(target)) {
2524
2.67k
      if (HT_IS_WITHOUT_HOLES(source)) {
2525
2.66k
        idx = zend_array_dup_elements(source, target, true, false);
2526
2.66k
      } else {
2527
14
        idx = zend_array_dup_elements(source, target, true, true);
2528
14
      }
2529
2.67k
    } else {
2530
1.00k
      if (HT_IS_WITHOUT_HOLES(source)) {
2531
1.00k
        idx = zend_array_dup_elements(source, target, false, false);
2532
1.00k
      } else {
2533
0
        idx = zend_array_dup_elements(source, target, false, true);
2534
0
      }
2535
1.00k
    }
2536
3.68k
    target->nNumUsed = idx;
2537
3.68k
    target->nNumOfElements = idx;
2538
3.68k
  }
2539
21.5k
  return target;
2540
21.5k
}
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
5.36k
{
2565
5.36k
  uint32_t idx;
2566
5.36k
  Bucket *p;
2567
5.36k
  zval *t, *s;
2568
2569
5.36k
  IS_CONSISTENT(source);
2570
5.36k
  IS_CONSISTENT(target);
2571
5.36k
  HT_ASSERT_RC1(target);
2572
2573
5.36k
  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
5.36k
  } else {
2610
5.36k
    if (HT_IS_PACKED(source)) {
2611
9.45k
      for (idx = 0; idx < source->nNumUsed; idx++) {
2612
8.16k
        s = source->arPacked + idx;
2613
8.16k
        if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2614
865
          continue;
2615
865
        }
2616
7.29k
        t = zend_hash_index_add(target, idx, s);
2617
7.29k
        if (t && pCopyConstructor) {
2618
5.57k
          pCopyConstructor(t);
2619
5.57k
        }
2620
7.29k
      }
2621
1.29k
      return;
2622
1.29k
    }
2623
2624
10.9k
    for (idx = 0; idx < source->nNumUsed; idx++) {
2625
6.91k
      p = source->arData + idx;
2626
6.91k
      s = &p->val;
2627
6.91k
      if (UNEXPECTED(Z_TYPE_P(s) == IS_INDIRECT)) {
2628
0
        s = Z_INDIRECT_P(s);
2629
0
      }
2630
6.91k
      if (UNEXPECTED(Z_TYPE_P(s) == IS_UNDEF)) {
2631
0
        continue;
2632
0
      }
2633
6.91k
      if (p->key) {
2634
1.03k
        t = _zend_hash_add_or_update_i(target, p->key, s, HASH_ADD | HASH_UPDATE_INDIRECT);
2635
1.03k
        if (t && pCopyConstructor) {
2636
350
          pCopyConstructor(t);
2637
350
        }
2638
5.88k
      } else {
2639
5.88k
        t = zend_hash_index_add(target, p->h, s);
2640
5.88k
        if (t && pCopyConstructor) {
2641
5.37k
          pCopyConstructor(t);
2642
5.37k
        }
2643
5.88k
      }
2644
6.91k
    }
2645
4.07k
  }
2646
5.36k
}
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
36.9M
{
2686
36.9M
  Bucket *p;
2687
2688
36.9M
  IS_CONSISTENT(ht);
2689
2690
36.9M
  (void)zend_string_hash_val(key);
2691
36.9M
  p = zend_hash_find_bucket(ht, key);
2692
36.9M
  return p ? &p->val : NULL;
2693
36.9M
}
2694
2695
ZEND_API zval* ZEND_FASTCALL zend_hash_find_known_hash(const HashTable *ht, const zend_string *key)
2696
1.29M
{
2697
1.29M
  Bucket *p;
2698
2699
1.29M
  IS_CONSISTENT(ht);
2700
2701
1.29M
  p = zend_hash_find_bucket(ht, key);
2702
1.29M
  return p ? &p->val : NULL;
2703
1.29M
}
2704
2705
ZEND_API zval* ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *str, size_t len)
2706
3.66M
{
2707
3.66M
  zend_ulong h;
2708
3.66M
  Bucket *p;
2709
2710
3.66M
  IS_CONSISTENT(ht);
2711
2712
3.66M
  h = zend_inline_hash_func(str, len);
2713
3.66M
  p = zend_hash_str_find_bucket(ht, str, len, h);
2714
3.66M
  return p ? &p->val : NULL;
2715
3.66M
}
2716
2717
ZEND_API zval* ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h)
2718
849M
{
2719
849M
  Bucket *p;
2720
2721
849M
  IS_CONSISTENT(ht);
2722
2723
849M
  if (HT_IS_PACKED(ht)) {
2724
26.4k
    if (h < ht->nNumUsed) {
2725
23.2k
      zval *zv = ht->arPacked + h;
2726
2727
23.2k
      if (Z_TYPE_P(zv) != IS_UNDEF) {
2728
22.9k
        return zv;
2729
22.9k
      }
2730
23.2k
    }
2731
3.50k
    return NULL;
2732
26.4k
  }
2733
2734
849M
  p = zend_hash_index_find_bucket(ht, h);
2735
849M
  return p ? &p->val : NULL;
2736
849M
}
2737
2738
ZEND_API zval* ZEND_FASTCALL _zend_hash_index_find(const HashTable *ht, zend_ulong h)
2739
3.06k
{
2740
3.06k
  Bucket *p;
2741
2742
3.06k
  IS_CONSISTENT(ht);
2743
3.06k
  ZEND_ASSERT(!HT_IS_PACKED(ht));
2744
2745
3.06k
  p = zend_hash_index_find_bucket(ht, h);
2746
3.06k
  return p ? &p->val : NULL;
2747
3.06k
}
2748
2749
ZEND_API void ZEND_FASTCALL zend_hash_internal_pointer_reset_ex(const HashTable *ht, HashPosition *pos)
2750
2.54k
{
2751
2.54k
  IS_CONSISTENT(ht);
2752
2.54k
  HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2753
2.54k
  *pos = _zend_hash_get_valid_pos(ht, 0);
2754
2.54k
}
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
37
{
2762
37
  uint32_t idx;
2763
2764
37
  IS_CONSISTENT(ht);
2765
37
  HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2766
2767
37
  idx = ht->nNumUsed;
2768
37
  if (HT_IS_PACKED(ht)) {
2769
21
    while (idx > 0) {
2770
21
      idx--;
2771
21
      if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) {
2772
21
        *pos = idx;
2773
21
        return;
2774
21
      }
2775
21
    }
2776
21
  } else {
2777
16
    while (idx > 0) {
2778
16
      idx--;
2779
16
      if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
2780
16
        *pos = idx;
2781
16
        return;
2782
16
      }
2783
16
    }
2784
16
  }
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
1.58k
{
2791
1.58k
  uint32_t idx;
2792
2793
1.58k
  IS_CONSISTENT(ht);
2794
1.58k
  HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2795
2796
1.58k
  idx = _zend_hash_get_valid_pos(ht, *pos);
2797
1.58k
  if (idx < ht->nNumUsed) {
2798
1.58k
    if (HT_IS_PACKED(ht)) {
2799
532
      while (1) {
2800
532
        idx++;
2801
532
        if (idx >= ht->nNumUsed) {
2802
160
          *pos = ht->nNumUsed;
2803
160
          return SUCCESS;
2804
160
        }
2805
372
        if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) {
2806
369
          *pos = idx;
2807
369
          return SUCCESS;
2808
369
        }
2809
372
      }
2810
1.05k
    } else {
2811
1.06k
      while (1) {
2812
1.06k
        idx++;
2813
1.06k
        if (idx >= ht->nNumUsed) {
2814
504
          *pos = ht->nNumUsed;
2815
504
          return SUCCESS;
2816
504
        }
2817
563
        if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
2818
555
          *pos = idx;
2819
555
          return SUCCESS;
2820
555
        }
2821
563
      }
2822
1.05k
    }
2823
1.58k
  } else {
2824
0
    return FAILURE;
2825
0
  }
2826
1.58k
}
2827
2828
ZEND_API zend_result ZEND_FASTCALL zend_hash_move_backwards_ex(const HashTable *ht, HashPosition *pos)
2829
22
{
2830
22
  uint32_t idx = *pos;
2831
2832
22
  IS_CONSISTENT(ht);
2833
22
  HT_ASSERT(ht, &ht->nInternalPointer != pos || GC_REFCOUNT(ht) == 1);
2834
2835
22
  if (idx < ht->nNumUsed) {
2836
22
    if (HT_IS_PACKED(ht)) {
2837
6
      while (idx > 0) {
2838
6
        idx--;
2839
6
        if (Z_TYPE(ht->arPacked[idx]) != IS_UNDEF) {
2840
6
          *pos = idx;
2841
6
          return SUCCESS;
2842
6
        }
2843
6
      }
2844
16
    } else {
2845
16
      while (idx > 0) {
2846
5
        idx--;
2847
5
        if (Z_TYPE(ht->arData[idx].val) != IS_UNDEF) {
2848
5
          *pos = idx;
2849
5
          return SUCCESS;
2850
5
        }
2851
5
      }
2852
16
    }
2853
11
    *pos = ht->nNumUsed;
2854
11
    return SUCCESS;
2855
22
  } else {
2856
0
    return FAILURE;
2857
0
  }
2858
22
}
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
896
{
2863
896
  uint32_t idx;
2864
896
  Bucket *p;
2865
2866
896
  IS_CONSISTENT(ht);
2867
896
  idx = _zend_hash_get_valid_pos(ht, *pos);
2868
896
  if (idx < ht->nNumUsed) {
2869
600
    if (HT_IS_PACKED(ht)) {
2870
0
      *num_index = idx;
2871
0
      return HASH_KEY_IS_LONG;
2872
0
    }
2873
600
    p = ht->arData + idx;
2874
600
    if (p->key) {
2875
494
      *str_index = p->key;
2876
494
      return HASH_KEY_IS_STRING;
2877
494
    } else {
2878
106
      *num_index = p->h;
2879
106
      return HASH_KEY_IS_LONG;
2880
106
    }
2881
600
  }
2882
296
  return HASH_KEY_NON_EXISTENT;
2883
896
}
2884
2885
ZEND_API void ZEND_FASTCALL zend_hash_get_current_key_zval_ex(const HashTable *ht, zval *key, const HashPosition *pos)
2886
542
{
2887
542
  uint32_t idx;
2888
542
  Bucket *p;
2889
2890
542
  IS_CONSISTENT(ht);
2891
542
  idx = _zend_hash_get_valid_pos(ht, *pos);
2892
542
  if (idx >= ht->nNumUsed) {
2893
0
    ZVAL_NULL(key);
2894
542
  } else {
2895
542
    if (HT_IS_PACKED(ht)) {
2896
275
      ZVAL_LONG(key, idx);
2897
275
      return;
2898
275
    }
2899
267
    p = ht->arData + idx;
2900
267
    if (p->key) {
2901
193
      ZVAL_STR_COPY(key, p->key);
2902
193
    } else {
2903
74
      ZVAL_LONG(key, p->h);
2904
74
    }
2905
267
  }
2906
542
}
2907
2908
ZEND_API zend_hash_key_type ZEND_FASTCALL zend_hash_get_current_key_type_ex(const HashTable *ht, const HashPosition *pos)
2909
1.50k
{
2910
1.50k
  uint32_t idx;
2911
1.50k
  Bucket *p;
2912
2913
1.50k
  IS_CONSISTENT(ht);
2914
1.50k
  idx = _zend_hash_get_valid_pos(ht, *pos);
2915
1.50k
  if (idx < ht->nNumUsed) {
2916
967
    if (HT_IS_PACKED(ht)) {
2917
265
      return HASH_KEY_IS_LONG;
2918
265
    }
2919
702
    p = ht->arData + idx;
2920
702
    if (p->key) {
2921
559
      return HASH_KEY_IS_STRING;
2922
559
    } else {
2923
143
      return HASH_KEY_IS_LONG;
2924
143
    }
2925
702
  }
2926
534
  return HASH_KEY_NON_EXISTENT;
2927
1.50k
}
2928
2929
2930
ZEND_API zval* ZEND_FASTCALL zend_hash_get_current_data_ex(const HashTable *ht, const HashPosition *pos)
2931
2.72k
{
2932
2.72k
  uint32_t idx;
2933
2.72k
  Bucket *p;
2934
2935
2.72k
  IS_CONSISTENT(ht);
2936
2.72k
  idx = _zend_hash_get_valid_pos(ht, *pos);
2937
2.72k
  if (idx < ht->nNumUsed) {
2938
2.54k
    if (HT_IS_PACKED(ht)) {
2939
473
      return &ht->arPacked[idx];
2940
473
    }
2941
2.06k
    p = ht->arData + idx;
2942
2.06k
    return &p->val;
2943
2.54k
  } else {
2944
179
    return NULL;
2945
179
  }
2946
2.72k
}
2947
2948
ZEND_API void zend_hash_bucket_swap(Bucket *p, Bucket *q)
2949
4.79k
{
2950
4.79k
  zval val;
2951
4.79k
  zend_ulong h;
2952
4.79k
  zend_string *key;
2953
2954
4.79k
  val = p->val;
2955
4.79k
  h = p->h;
2956
4.79k
  key = p->key;
2957
2958
4.79k
  p->val = q->val;
2959
4.79k
  p->h = q->h;
2960
4.79k
  p->key = q->key;
2961
2962
4.79k
  q->val = val;
2963
4.79k
  q->h = h;
2964
4.79k
  q->key = key;
2965
4.79k
}
2966
2967
ZEND_API void zend_hash_bucket_renum_swap(Bucket *p, Bucket *q)
2968
71.3k
{
2969
71.3k
  zval val;
2970
2971
71.3k
  val = p->val;
2972
71.3k
  p->val = q->val;
2973
71.3k
  q->val = val;
2974
71.3k
}
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
576
{
2993
576
  Bucket *p;
2994
576
  uint32_t i, j;
2995
2996
576
  IS_CONSISTENT(ht);
2997
2998
576
  if (!(ht->nNumOfElements>1) && !(renumber && ht->nNumOfElements>0)) {
2999
    /* Doesn't require sorting */
3000
12
    return;
3001
12
  }
3002
3003
564
  if (HT_IS_PACKED(ht)) {
3004
0
    zend_hash_packed_to_hash(ht); // TODO: ???
3005
0
  }
3006
3007
564
  if (HT_IS_WITHOUT_HOLES(ht)) {
3008
    /* Store original order of elements in extra space to allow stable sorting. */
3009
26.9k
    for (i = 0; i < ht->nNumUsed; i++) {
3010
26.3k
      Z_EXTRA(ht->arData[i].val) = i;
3011
26.3k
    }
3012
564
  } 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
564
  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
564
    HT_HASH_RESET(ht);
3034
564
  }
3035
3036
564
  sort((void *)ht->arData, ht->nNumUsed, sizeof(Bucket), (compare_func_t) compar,
3037
564
      (swap_func_t)(renumber? zend_hash_bucket_renum_swap :
3038
564
        (HT_IS_PACKED(ht) ? zend_hash_bucket_packed_swap : zend_hash_bucket_swap)));
3039
3040
564
  ht->nInternalPointer = 0;
3041
3042
564
  if (renumber) {
3043
24.2k
    for (j = 0; j < i; j++) {
3044
23.7k
      p = ht->arData + j;
3045
23.7k
      p->h = j;
3046
23.7k
      if (p->key) {
3047
222
        zend_string_release(p->key);
3048
222
        p->key = NULL;
3049
222
      }
3050
23.7k
    }
3051
3052
457
    ht->nNextFreeElement = i;
3053
457
  }
3054
564
  if (HT_IS_PACKED(ht)) {
3055
0
    if (!renumber) {
3056
0
      zend_hash_packed_to_hash(ht);
3057
0
    }
3058
564
  } else {
3059
564
    if (renumber) {
3060
457
      void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
3061
457
      Bucket *old_buckets = ht->arData;
3062
457
      zval *zv;
3063
3064
457
      new_data = pemalloc(HT_PACKED_SIZE_EX(ht->nTableSize, HT_MIN_MASK), (GC_FLAGS(ht) & IS_ARRAY_PERSISTENT));
3065
457
      HT_FLAGS(ht) |= HASH_FLAG_PACKED | HASH_FLAG_STATIC_KEYS;
3066
457
      ht->nTableMask = HT_MIN_MASK;
3067
457
      HT_SET_DATA_ADDR(ht, new_data);
3068
457
      p = old_buckets;
3069
457
      zv = ht->arPacked;
3070
35.0k
      for (i = 0; i < ht->nTableSize; i++) {
3071
34.6k
        ZVAL_COPY_VALUE(zv, &p->val);
3072
34.6k
        zv++;
3073
34.6k
        p++;
3074
34.6k
      }
3075
457
      pefree(old_data, GC_FLAGS(ht) & IS_ARRAY_PERSISTENT);
3076
457
      HT_HASH_RESET_PACKED(ht);
3077
457
    } else {
3078
107
      zend_hash_rehash(ht);
3079
107
    }
3080
564
  }
3081
564
}
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
32
{
3085
32
  HT_ASSERT_RC1(ht);
3086
32
  zend_hash_sort_internal(ht, sort, compar, renumber);
3087
32
}
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
544
{
3091
544
  HT_ASSERT_RC1(ht);
3092
3093
  /* Unpack the array early to avoid RCn assertion failures. */
3094
544
  if (HT_IS_PACKED(ht)) {
3095
435
    zend_hash_packed_to_hash(ht);
3096
435
  }
3097
3098
  /* Adding a refcount prevents the array from going away. */
3099
544
  GC_ADDREF(ht);
3100
3101
544
  zend_hash_sort_internal(ht, sort, compar, renumber);
3102
3103
544
  if (UNEXPECTED(GC_DELREF(ht) == 0)) {
3104
5
    zend_array_destroy(ht);
3105
539
  } else {
3106
539
    gc_check_possible_root((zend_refcounted *)ht);
3107
539
  }
3108
544
}
3109
3110
5.24k
static zend_always_inline int zend_hash_compare_impl(const HashTable *ht1, const HashTable *ht2, compare_func_t compar, bool ordered) {
3111
5.24k
  uint32_t idx1, idx2;
3112
5.24k
  zend_string *key1, *key2;
3113
5.24k
  zend_ulong h1, h2;
3114
5.24k
  zval *pData1, *pData2;;
3115
5.24k
  int result;
3116
3117
5.24k
  if (ht1->nNumOfElements != ht2->nNumOfElements) {
3118
1.48k
    return ht1->nNumOfElements > ht2->nNumOfElements ? 1 : -1;
3119
1.48k
  }
3120
3121
5.46k
  for (idx1 = 0, idx2 = 0; idx1 < ht1->nNumUsed; idx1++) {
3122
3.55k
    if (HT_IS_PACKED(ht1)) {
3123
1.97k
      pData1 = ht1->arPacked + idx1;
3124
1.97k
      h1 = idx1;
3125
1.97k
      key1 = NULL;
3126
1.97k
    } else {
3127
1.57k
      Bucket *p = ht1->arData + idx1;
3128
1.57k
      pData1 = &p->val;
3129
1.57k
      h1 = p->h;
3130
1.57k
      key1 = p->key;
3131
1.57k
    }
3132
3133
3.55k
    if (Z_TYPE_P(pData1) == IS_UNDEF) continue;
3134
2.68k
    if (ordered) {
3135
1.79k
      if (HT_IS_PACKED(ht2)) {
3136
1.68k
        while (1) {
3137
1.68k
          ZEND_ASSERT(idx2 != ht2->nNumUsed);
3138
1.68k
          pData2 = ht2->arPacked + idx2;
3139
1.68k
          h2 = idx2;
3140
1.68k
          key2 = NULL;
3141
1.68k
          if (Z_TYPE_P(pData2) != IS_UNDEF) break;
3142
1.14k
          idx2++;
3143
1.14k
        }
3144
1.25k
      } else {
3145
1.25k
        while (1) {
3146
1.25k
          Bucket *p;
3147
1.25k
          ZEND_ASSERT(idx2 != ht2->nNumUsed);
3148
1.25k
          p = ht2->arData + idx2;
3149
1.25k
          pData2 = &p->val;
3150
1.25k
          h2 = p->h;
3151
1.25k
          key2 = p->key;
3152
1.25k
          if (Z_TYPE_P(pData2) != IS_UNDEF) break;
3153
0
          idx2++;
3154
0
        }
3155
1.25k
      }
3156
1.79k
      if (key1 == NULL && key2 == NULL) { /* numeric indices */
3157
733
        if (h1 != h2) {
3158
150
          return h1 > h2 ? 1 : -1;
3159
150
        }
3160
1.06k
      } else if (key1 != NULL && key2 != NULL) { /* string indices */
3161
812
        if (ZSTR_LEN(key1) != ZSTR_LEN(key2)) {
3162
230
          return ZSTR_LEN(key1) > ZSTR_LEN(key2) ? 1 : -1;
3163
230
        }
3164
3165
582
        result = memcmp(ZSTR_VAL(key1), ZSTR_VAL(key2), ZSTR_LEN(key1));
3166
582
        if (result != 0) {
3167
272
          return result;
3168
272
        }
3169
582
      } else {
3170
        /* Mixed key types: A string key is considered as larger */
3171
248
        return key1 != NULL ? 1 : -1;
3172
248
      }
3173
893
      idx2++;
3174
893
    } else {
3175
888
      if (key1 == NULL) { /* numeric index */
3176
477
        pData2 = zend_hash_index_find(ht2, h1);
3177
477
        if (pData2 == NULL) {
3178
224
          return 1;
3179
224
        }
3180
477
      } else { /* string index */
3181
411
        pData2 = zend_hash_find(ht2, key1);
3182
411
        if (pData2 == NULL) {
3183
300
          return 1;
3184
300
        }
3185
411
      }
3186
888
    }
3187
3188
1.25k
    if (Z_TYPE_P(pData1) == IS_INDIRECT) {
3189
44
      pData1 = Z_INDIRECT_P(pData1);
3190
44
    }
3191
1.25k
    if (Z_TYPE_P(pData2) == IS_INDIRECT) {
3192
44
      pData2 = Z_INDIRECT_P(pData2);
3193
44
    }
3194
3195
1.25k
    if (Z_TYPE_P(pData1) == IS_UNDEF) {
3196
17
      if (Z_TYPE_P(pData2) != IS_UNDEF) {
3197
0
        return -1;
3198
0
      }
3199
1.24k
    } else if (Z_TYPE_P(pData2) == IS_UNDEF) {
3200
0
      return 1;
3201
1.24k
    } else {
3202
1.24k
      result = compar(pData1, pData2);
3203
1.24k
      if (result != 0) {
3204
428
        return result;
3205
428
      }
3206
1.24k
    }
3207
1.25k
  }
3208
3209
1.90k
  return 0;
3210
3.75k
}
3211
3212
ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, bool ordered)
3213
5.25k
{
3214
5.25k
  int result;
3215
5.25k
  IS_CONSISTENT(ht1);
3216
5.25k
  IS_CONSISTENT(ht2);
3217
3218
5.25k
  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
5.25k
  if (UNEXPECTED(GC_IS_RECURSIVE(ht1))) {
3227
15
    zend_throw_error(NULL, "Nesting level too deep - recursive dependency?");
3228
15
    return ZEND_UNCOMPARABLE;
3229
15
  }
3230
3231
5.24k
  GC_TRY_PROTECT_RECURSION(ht1);
3232
5.24k
  result = zend_hash_compare_impl(ht1, ht2, compar, ordered);
3233
5.24k
  GC_TRY_UNPROTECT_RECURSION(ht1);
3234
3235
5.24k
  return result;
3236
5.25k
}
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
80.0k
{
3308
80.0k
  const char *tmp = key;
3309
3310
80.0k
  const char *end = key + length;
3311
3312
80.0k
  if (*tmp == '-') {
3313
7.10k
    tmp++;
3314
7.10k
  }
3315
3316
80.0k
  if ((*tmp == '0' && length > 1) /* numbers with leading zeros */
3317
71.0k
   || (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
12.0k
       *tmp > '2')) { /* overflow */
3321
12.0k
    return 0;
3322
12.0k
  }
3323
67.9k
  *idx = (*tmp - '0');
3324
154k
  while (1) {
3325
154k
    ++tmp;
3326
154k
    if (tmp == end) {
3327
48.5k
      if (*key == '-') {
3328
2.99k
        if (*idx-1 > ZEND_LONG_MAX) { /* overflow */
3329
830
          return 0;
3330
830
        }
3331
2.16k
        *idx = 0 - *idx;
3332
45.5k
      } else if (*idx > ZEND_LONG_MAX) { /* overflow */
3333
490
        return 0;
3334
490
      }
3335
47.2k
      return 1;
3336
48.5k
    }
3337
105k
    if (*tmp <= '9' && *tmp >= '0') {
3338
86.2k
      *idx = (*idx * 10) + (*tmp - '0');
3339
86.2k
    } else {
3340
19.4k
      return 0;
3341
19.4k
    }
3342
105k
  }
3343
67.9k
}
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
263
{
3351
263
  zend_ulong num_key;
3352
263
  zend_string *str_key;
3353
263
  zval *zv;
3354
3355
263
  if (UNEXPECTED(HT_IS_PACKED(ht))) {
3356
30
    goto convert;
3357
30
  }
3358
3359
1.19k
  ZEND_HASH_MAP_FOREACH_STR_KEY(ht, str_key) {
3360
1.19k
    if (!str_key) {
3361
7
      goto convert;
3362
7
    }
3363
1.19k
  } ZEND_HASH_FOREACH_END();
3364
3365
226
  if (!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE)) {
3366
48
    GC_ADDREF(ht);
3367
48
  }
3368
3369
226
  return ht;
3370
3371
37
convert:
3372
37
  {
3373
37
    HashTable *new_ht = zend_new_array(zend_hash_num_elements(ht));
3374
3375
243
    ZEND_HASH_FOREACH_KEY_VAL(ht, num_key, str_key, zv) {
3376
243
      if (!str_key) {
3377
95
        str_key = zend_long_to_str(num_key);
3378
95
        zend_string_delref(str_key);
3379
95
      }
3380
243
      do {
3381
102
        if (Z_OPT_REFCOUNTED_P(zv)) {
3382
2
          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
2
          Z_ADDREF_P(zv);
3389
2
        }
3390
102
      } while (0);
3391
243
      zend_hash_update(new_ht, str_key, zv);
3392
243
    } ZEND_HASH_FOREACH_END();
3393
3394
37
    return new_ht;
3395
233
  }
3396
233
}
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
1.53k
{
3404
1.53k
  zend_ulong num_key;
3405
1.53k
  zend_string *str_key;
3406
1.53k
  zval *zv;
3407
3408
1.53k
  if (!HT_IS_PACKED(ht)) {
3409
35.3k
    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
35.3k
      if (str_key && ZEND_HANDLE_NUMERIC(str_key, num_key)) {
3416
52
        goto convert;
3417
52
      }
3418
35.3k
    } ZEND_HASH_FOREACH_END();
3419
1.53k
  }
3420
3421
1.48k
  if (always_duplicate) {
3422
1.47k
    return zend_array_dup(ht);
3423
1.47k
  }
3424
3425
7
  if (EXPECTED(!(GC_FLAGS(ht) & IS_ARRAY_IMMUTABLE))) {
3426
7
    GC_ADDREF(ht);
3427
7
  }
3428
3429
7
  return ht;
3430
3431
52
convert:
3432
52
  {
3433
52
    HashTable *new_ht = zend_new_array(zend_hash_num_elements(ht));
3434
3435
1.07k
    ZEND_HASH_MAP_FOREACH_KEY_VAL_IND(ht, num_key, str_key, zv) {
3436
1.07k
      do {
3437
466
        if (Z_OPT_REFCOUNTED_P(zv)) {
3438
221
          if (Z_ISREF_P(zv) && Z_REFCOUNT_P(zv) == 1) {
3439
12
            zv = Z_REFVAL_P(zv);
3440
12
            if (!Z_OPT_REFCOUNTED_P(zv)) {
3441
0
              break;
3442
0
            }
3443
12
          }
3444
221
          Z_ADDREF_P(zv);
3445
221
        }
3446
466
      } while (0);
3447
      /* Again, thank ArrayObject for `!str_key ||`. */
3448
1.07k
      if (!str_key || ZEND_HANDLE_NUMERIC(str_key, num_key)) {
3449
84
        zend_hash_index_update(new_ht, num_key, zv);
3450
382
      } else {
3451
382
        zend_hash_update(new_ht, str_key, zv);
3452
382
      }
3453
1.07k
    } ZEND_HASH_FOREACH_END();
3454
3455
52
    return new_ht;
3456
52
  }
3457
52
}