Coverage Report

Created: 2025-12-14 06:05

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