• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

krakjoe / parallel / 22638709538

03 Mar 2026 07:14PM UTC coverage: 94.658% (-0.4%) from 95.012%
22638709538

Pull #370

github

web-flow
Merge ee2704365 into 3ae6833bb
Pull Request #370: fix cache key collision

1 of 1 new or added line in 1 file covered. (100.0%)

6 existing lines in 3 files now uncovered.

2764 of 2920 relevant lines covered (94.66%)

396.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.81
/src/cache.c
1
/*
2
  +----------------------------------------------------------------------+
3
  | parallel                                                             |
4
  +----------------------------------------------------------------------+
5
  | Copyright (c) Joe Watkins 2019-2024                                  |
6
  +----------------------------------------------------------------------+
7
  | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
11
  | If you did not receive a copy of the PHP license and are unable to   |
12
  | obtain it through the world-wide-web, please send a note to          |
13
  | license@php.net so we can mail you a copy immediately.               |
14
  +----------------------------------------------------------------------+
15
  | Author: krakjoe                                                      |
16
  +----------------------------------------------------------------------+
17
 */
18
#ifndef HAVE_PARALLEL_CACHE
19
#define HAVE_PARALLEL_CACHE
20

21
#include "parallel.h"
22

23
static struct {
24
        pthread_mutex_t mutex;
25
        HashTable       table;
26
        struct {
27
                size_t size;
28
                size_t used;
29
                void  *mem;
30
                void  *block;
31
        } memory;
32
} php_parallel_cache_globals = {PTHREAD_MUTEX_INITIALIZER};
33

34
#define PCG(e) php_parallel_cache_globals.e
35
#define PCM(e) PCG(memory).e
36

37
#define PARALLEL_CACHE_CHUNK PARALLEL_PLATFORM_ALIGNED((1024 * 1024) * 8)
38

39
/* {{{ */
40
static zend_always_inline void *php_parallel_cache_alloc(size_t size)
552✔
41
{
42
        void  *mem;
552✔
43
        size_t aligned = PARALLEL_PLATFORM_ALIGNED(size);
552✔
44

45
        ZEND_ASSERT(size < PARALLEL_CACHE_CHUNK);
552✔
46

47
        if ((PCM(used) + aligned) >= PCM(size)) {
552✔
48
                PCM(size) = PARALLEL_PLATFORM_ALIGNED(PCM(size) + PARALLEL_CACHE_CHUNK);
×
49
                PCM(mem) = (void *)realloc(PCM(mem), PCM(size));
×
50

51
                if (!PCM(mem)) {
×
52
                        /* out of memory */
53
                        return NULL;
54
                }
55

56
                PCM(block) = (void *)(((char *)PCM(mem)) + PCM(used));
×
57
        }
58

59
        mem = PCM(block);
552✔
60
        PCM(block) = (void *)(((char *)PCM(block)) + aligned);
552✔
61
        PCM(used) += aligned;
552✔
62

63
        return mem;
552✔
64
}
65

66
static zend_always_inline void *php_parallel_cache_copy_mem(void *source, zend_long size)
343✔
67
{
68
        void *destination = php_parallel_cache_alloc(size);
27✔
69

70
        memcpy(destination, source, size);
316✔
71

72
        return destination;
343✔
73
} /* }}} */
74

75
static zend_always_inline HashTable *php_parallel_cache_statics(HashTable *statics)
7✔
76
{ /* {{{ */
77
        HashTable *cached = zend_hash_index_find_ptr(&PCG(table), (zend_ulong)statics);
14✔
78

79
        if (cached) {
×
80
                return cached;
81
        }
82

83
        cached = php_parallel_copy_hash_persistent(statics, php_parallel_copy_string_interned, php_parallel_cache_copy_mem,
7✔
84
                                                   PHP_PARALLEL_COPY_STORAGE_CACHE_POOL);
85

86
        return zend_hash_index_update_ptr(&PCG(table), (zend_ulong)statics, cached);
7✔
87
} /* }}} */
88

89
static zend_always_inline void php_parallel_cache_type(zend_type *type)
41✔
90
{ /* {{{ */
91
        zend_type *single;
41✔
92

93
        if (!ZEND_TYPE_IS_SET(*type)) {
41✔
94
                return;
95
        }
96

97
        if (ZEND_TYPE_HAS_LIST(*type)) {
25✔
98
                zend_type_list *list = ZEND_TYPE_LIST(*type);
1✔
99

100
                list = php_parallel_cache_copy_mem(list, ZEND_TYPE_LIST_SIZE(list->num_types));
1✔
101

102
                if (ZEND_TYPE_USES_ARENA(*type)) {
1✔
UNCOV
103
                        ZEND_TYPE_FULL_MASK(*type) &= ~_ZEND_TYPE_ARENA_BIT;
×
104
                }
105

106
                ZEND_TYPE_SET_PTR(*type, list);
1✔
107
        }
108

109
        ZEND_TYPE_FOREACH(*type, single)
25✔
110
        {
111
                if (ZEND_TYPE_HAS_NAME(*single)) {
26✔
112
                        zend_string *name = ZEND_TYPE_NAME(*single);
15✔
113

114
                        ZEND_TYPE_SET_PTR(*single, php_parallel_copy_string_interned(name));
15✔
115
                }
116
        }
117
        ZEND_TYPE_FOREACH_END();
26✔
118
} /* }}} */
119

120
/* {{{ */
121
static zend_op_array *php_parallel_cache_create(const zend_function *source, bool statics)
131✔
122
{
123
        zend_op_array *cached = php_parallel_cache_copy_mem((void *)source, sizeof(zend_op_array));
131✔
124
        uint32_t      *literal_map = NULL;
131✔
125
        uint32_t      *offset_map = NULL;
131✔
126
        uint32_t       new_last_literal = cached->last_literal;
131✔
127

128
        cached->fn_flags |= ZEND_ACC_IMMUTABLE;
131✔
129

130
        if (statics && cached->static_variables) {
131✔
131
                cached->static_variables = php_parallel_cache_statics(cached->static_variables);
14✔
132
        }
133

134
#if PHP_VERSION_ID >= 80200
135
        ZEND_MAP_PTR_INIT(cached->static_variables_ptr, cached->static_variables);
136
#else
137
        ZEND_MAP_PTR_INIT(cached->static_variables_ptr, &cached->static_variables);
131✔
138
#endif
139

140
        ZEND_MAP_PTR_INIT(cached->run_time_cache, NULL);
131✔
141

142
#if PHP_VERSION_ID >= 80100
143
        if (cached->num_dynamic_func_defs) {
144
                uint32_t it = 0;
145

146
                cached->dynamic_func_defs = php_parallel_cache_copy_mem(
147
                    cached->dynamic_func_defs, sizeof(zend_op_array *) * cached->num_dynamic_func_defs);
148

149
                while (it < cached->num_dynamic_func_defs) {
150
                        cached->dynamic_func_defs[it] =
151
                            (zend_op_array *)php_parallel_cache_create((zend_function *)cached->dynamic_func_defs[it], statics);
152
                        it++;
153
                }
154
        }
155
#endif
156

157
        if (!cached->refcount) {
131✔
158
                goto _php_parallel_cached_function_return;
1✔
159
        }
160

161
        cached->refcount = NULL;
130✔
162

163
        if (cached->last_literal) {
130✔
164
                zend_op *src_opline = source->op_array.opcodes;
130✔
165
                zend_op *src_end = src_opline + source->op_array.last;
130✔
166

167
                // A map to keep track of which literals are referenced by the
168
                // `ZEND_INIT_FCALL` opcodes we found so that we can expand those later
169
                literal_map = emalloc(sizeof(uint32_t) * cached->last_literal);
130✔
170
                memset(literal_map, 0, sizeof(uint32_t) * cached->last_literal);
130✔
171

172
                // Search for `ZEND_INIT_FCALL` opcodes and remember the indexes for the
173
                // literals, as we are rewriting them later to `ZEND_INIT_FCALL_BY_NAME`
174
                // which requires a second, lower cased literal just in the next literal
175
                // slot.
176
                while (src_opline < src_end) {
986✔
177
                        if (src_opline->opcode == ZEND_INIT_FCALL && src_opline->op2_type == IS_CONST) {
856✔
178
                                uint32_t idx;
44✔
179
#if ZEND_USE_ABS_CONST_ADDR
180
                                idx = (zval *)src_opline->op2.zv - source->op_array.literals;
181
#else
182
                                idx = ((zval *)((char *)src_opline + src_opline->op2.constant) - source->op_array.literals);
44✔
183
#endif
184
                                if (idx < cached->last_literal) {
44✔
185
                                        if (literal_map[idx] == 0) {
44✔
186
                                                literal_map[idx] = 1;
44✔
187
                                                new_last_literal++;
44✔
188
                                        }
189
                                }
190
                        }
191
                        src_opline++;
856✔
192
                }
193
        }
194

195
        if (new_last_literal) {
130✔
196
                zval    *literal = source->op_array.literals;
130✔
197
                zval    *slot = php_parallel_cache_alloc(sizeof(zval) * new_last_literal);
130✔
198
                uint32_t idx = 0;
130✔
199

200
                offset_map = emalloc(sizeof(uint32_t) * cached->last_literal);
130✔
201

202
                cached->literals = slot;
130✔
203

204
                for (uint32_t i = 0; i < cached->last_literal; i++) {
664✔
205
                        /* Record the mapping from old literal index (i) to new literal index (idx)
206
                           so we can update opcode operands later. */
207
                        offset_map[i] = idx;
534✔
208

209
                        if (Z_TYPE_P(literal) == IS_ARRAY) {
534✔
210
                                ZVAL_ARR(slot, php_parallel_copy_hash_persistent(Z_ARRVAL_P(literal), php_parallel_copy_string_interned,
7✔
211
                                                                                 php_parallel_cache_copy_mem,
212
                                                                                 PHP_PARALLEL_COPY_STORAGE_CACHE_POOL));
213
                        } else if (Z_TYPE_P(literal) == IS_STRING) {
527✔
214
                                ZVAL_STR(slot, php_parallel_copy_string_interned(Z_STR_P(literal)));
320✔
215
                        } else {
216
                                *slot = *literal;
207✔
217
                        }
218

219
                        Z_TYPE_FLAGS_P(slot) &= ~(IS_TYPE_REFCOUNTED | IS_TYPE_COLLECTABLE);
534✔
220

221
                        /* If this literal was used by INIT_FCALL, insert its lowercased version next. */
222
                        if (literal_map[i]) {
534✔
223
                                zend_string *lower = zend_string_tolower(Z_STR_P(slot));
44✔
224
                                slot++;
44✔
225
                                idx++;
44✔
226
                                ZVAL_STR(slot, php_parallel_copy_string_interned(lower));
44✔
227
                                zend_string_release(lower);
44✔
228
                                Z_TYPE_FLAGS_P(slot) &= ~(IS_TYPE_REFCOUNTED | IS_TYPE_COLLECTABLE);
44✔
229
                        }
230

231
                        literal++;
534✔
232
                        slot++;
534✔
233
                        idx++;
534✔
234
                }
235
                cached->last_literal = new_last_literal;
130✔
236
        }
237

238
        if (cached->last_var) {
130✔
239
                zend_string **vars = cached->vars;
79✔
240
                uint32_t      it = 0, end = cached->last_var;
79✔
241
                zend_string **heap = php_parallel_cache_alloc(cached->last_var * sizeof(zend_string *));
79✔
242

243
                while (it < end) {
189✔
244
                        heap[it] = php_parallel_copy_string_interned(vars[it]);
110✔
245
                        it++;
110✔
246
                }
247
                cached->vars = heap;
79✔
248
        }
249

250
        if (cached->last) {
130✔
251
                zend_op *opcodes = php_parallel_cache_copy_mem(cached->opcodes, sizeof(zend_op) * cached->last);
130✔
252
                zend_op *opline = opcodes, *end = opline + cached->last;
130✔
253

254
                while (opline < end) {
986✔
255
                        /* Replace ZEND_INIT_FCALL with ZEND_INIT_FCALL_BY_NAME.
256
                           We must clear op1_type (IS_UNUSED) and op1.var (0) to invalidate the
257
                           original thread's cache slot. */
258
                        if (opline->opcode == ZEND_INIT_FCALL) {
856✔
259
                                opline->opcode = ZEND_INIT_FCALL_BY_NAME;
44✔
260
                                opline->op1_type = IS_UNUSED;
44✔
261
                                opline->op1.var = 0;
44✔
262
                                ZEND_VM_SET_OPCODE_HANDLER(opline);
44✔
263
                        }
264

265
                        /* Remap IS_CONST operands to their new locations in the expanded literal table
266
                           using the offset_map we built earlier. */
267
                        if (opline->op1_type == IS_CONST) {
856✔
268
                                uint32_t idx;
292✔
269
                                zend_op *src_opline = source->op_array.opcodes + (opline - opcodes);
292✔
270
#if ZEND_USE_ABS_CONST_ADDR
271
                                idx = (zval *)src_opline->op1.zv - source->op_array.literals;
272
                                opline->op1.zv = &cached->literals[offset_map[idx]];
273
#else
274
                                idx = ((zval *)((char *)src_opline + src_opline->op1.constant) - source->op_array.literals);
292✔
275
                                opline->op1.constant = (char *)&cached->literals[offset_map[idx]] - (char *)opline;
292✔
276
#endif
277
                                if (opline->opcode == ZEND_SEND_VAL || opline->opcode == ZEND_SEND_VAL_EX ||
292✔
278
                                    opline->opcode == ZEND_QM_ASSIGN) {
279
                                        zend_vm_set_opcode_handler_ex(opline, 0, 0, 0);
41✔
280
                                }
281
                        }
282
                        if (opline->op2_type == IS_CONST) {
856✔
283
                                uint32_t idx;
149✔
284
                                zend_op *src_opline = source->op_array.opcodes + (opline - opcodes);
149✔
285
#if ZEND_USE_ABS_CONST_ADDR
286
                                idx = (zval *)src_opline->op2.zv - source->op_array.literals;
287
                                opline->op2.zv = &cached->literals[offset_map[idx]];
288
#else
289
                                idx = ((zval *)((char *)src_opline + src_opline->op2.constant) - source->op_array.literals);
149✔
290
                                opline->op2.constant = (char *)&cached->literals[offset_map[idx]] - (char *)opline;
149✔
291
#endif
292
                        }
293
#if ZEND_USE_ABS_JMP_ADDR
294
                        switch (opline->opcode) {
295
                        case ZEND_JMP:
296
                        case ZEND_FAST_CALL:
297
                                opline->op1.jmp_addr = &opcodes[opline->op1.jmp_addr - source->op_array.opcodes];
298
                                break;
299
#if PHP_VERSION_ID < 80200
300
                        case ZEND_JMPZNZ:
301
#endif
302
                        case ZEND_JMPZ:
303
                        case ZEND_JMPNZ:
304
                        case ZEND_JMPZ_EX:
305
                        case ZEND_JMPNZ_EX:
306
                        case ZEND_JMP_SET:
307
                        case ZEND_COALESCE:
308
                        case ZEND_FE_RESET_R:
309
                        case ZEND_FE_RESET_RW:
310
                        case ZEND_ASSERT_CHECK:
311
                                opline->op2.jmp_addr = &opcodes[opline->op2.jmp_addr - source->op_array.opcodes];
312
                                break;
313

314
                        case ZEND_CATCH:
315
                                if (!(opline->extended_value & ZEND_LAST_CATCH)) {
316
                                        opline->op2.jmp_addr = &opcodes[opline->op2.jmp_addr - source->op_array.opcodes];
317
                                }
318
                                break;
319
                        }
320
#endif
321

322
                        opline++;
856✔
323
                }
324
                cached->opcodes = opcodes;
130✔
325
        }
326

327
        if (literal_map) {
130✔
328
                efree(literal_map);
130✔
329
        }
330

331
        if (offset_map) {
130✔
332
                efree(offset_map);
130✔
333
        }
334

335
        if (cached->arg_info) {
130✔
336
                zend_arg_info *it = cached->arg_info, *end = it + cached->num_args, *info;
33✔
337

338
                if (cached->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
33✔
339
                        it--;
6✔
340
                }
341
                if (cached->fn_flags & ZEND_ACC_VARIADIC) {
33✔
342
                        end++;
2✔
343
                }
344

345
                cached->arg_info = info = php_parallel_cache_copy_mem(it, (end - it) * sizeof(zend_arg_info));
33✔
346

347
                while (it < end) {
74✔
348
                        if (info->name) {
41✔
349
                                info->name = php_parallel_copy_string_interned(it->name);
35✔
350
                        }
351

352
                        php_parallel_cache_type(&info->type);
41✔
353

354
                        info++;
41✔
355
                        it++;
41✔
356
                }
357
                if (cached->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
33✔
358
                        cached->arg_info++;
6✔
359
                }
360
        }
361

362
        if (cached->try_catch_array) {
130✔
363
                cached->try_catch_array = php_parallel_cache_copy_mem(cached->try_catch_array,
6✔
364
                                                                      sizeof(zend_try_catch_element) * cached->last_try_catch);
6✔
365
        }
366

367
        if (cached->live_range) {
130✔
368
                cached->live_range =
30✔
369
                    php_parallel_cache_copy_mem(cached->live_range, sizeof(zend_live_range) * cached->last_live_range);
30✔
370
        }
371

372
        if (cached->function_name)
130✔
373
                cached->function_name = php_parallel_copy_string_interned(cached->function_name);
130✔
374

375
        if (cached->filename)
130✔
376
                cached->filename = php_parallel_copy_string_interned(cached->filename);
130✔
377

378
        if (cached->doc_comment)
130✔
379
                cached->doc_comment = php_parallel_copy_string_interned(cached->doc_comment);
1✔
380

381
_php_parallel_cached_function_return:
129✔
382
        return cached;
131✔
383
} /* }}} */
384

385
/* {{{ */
386
static zend_always_inline zend_function *php_parallel_cache_function_ex(const zend_function *source, bool statics)
167✔
387
{
388
        zend_op_array *cached;
167✔
389

390
        pthread_mutex_lock(&PCG(mutex));
167✔
391

392
        if ((cached = zend_hash_index_find_ptr(&PCG(table), (zend_ulong)source->op_array.opcodes))) {
203✔
393
#if PHP_VERSION_ID >= 80400
394
                /* On PHP 8.4+ closures have unique names (e.g. {closure:file.php:42}).
395
                 * Validate that the cached entry belongs to the same closure, not a
396
                 * different one that was allocated at the same opcodes address after the
397
                 * original was freed (fixes #309). */
398
                if (!zend_string_equals(cached->function_name, source->op_array.function_name)) {
399
                        goto _php_parallel_cache_miss;
400
                }
401
#endif
402
                goto _php_parallel_cached_function_return;
36✔
403
        }
404

405
#if PHP_VERSION_ID >= 80400
406
_php_parallel_cache_miss:
407
#endif
408
        cached = php_parallel_cache_create(source, statics);
131✔
409

410
        zend_hash_index_update_ptr(&PCG(table), (zend_ulong)source->op_array.opcodes, cached);
131✔
411

412
_php_parallel_cached_function_return:
167✔
413
        pthread_mutex_unlock(&PCG(mutex));
167✔
414

415
        return (zend_function *)cached;
167✔
416
} /* }}} */
417

418
zend_function *php_parallel_cache_closure(const zend_function *source, zend_function *closure)
155✔
419
{ /* {{{ */
420
        zend_op_array *cache;
155✔
421

422
        cache = (zend_op_array *)php_parallel_cache_function_ex((zend_function *)source, 0);
155✔
423

424
        if (!closure) {
155✔
425
                closure = php_parallel_copy_mem(cache, sizeof(zend_op_array), 1);
117✔
426
        } else {
427
                memcpy(closure, cache, sizeof(zend_op_array));
38✔
428
        }
429

430
        if (source->op_array.static_variables) {
155✔
431
                HashTable *statics = ZEND_MAP_PTR_GET(source->op_array.static_variables_ptr);
33✔
432

433
                if (statics) {
33✔
434
                        closure->op_array.static_variables = php_parallel_copy_hash_ctor(statics, 1);
33✔
435

436
#if PHP_VERSION_ID >= 80200
437
                        ZEND_MAP_PTR_INIT(closure->op_array.static_variables_ptr, closure->op_array.static_variables);
438
#else
439
                        ZEND_MAP_PTR_INIT(closure->op_array.static_variables_ptr, &closure->op_array.static_variables);
33✔
440
#endif
441
                }
442
        }
443

444
#if PHP_VERSION_ID >= 80100
445
        if (source->op_array.num_dynamic_func_defs) {
446
                uint32_t it = 0;
447
                /* Use regular persistent memory for dynamic_func_defs array, not cache pool */
448
                closure->op_array.dynamic_func_defs =
449
                    pemalloc(sizeof(zend_op_array *) * source->op_array.num_dynamic_func_defs, 1);
450
                memcpy(closure->op_array.dynamic_func_defs, source->op_array.dynamic_func_defs,
451
                       sizeof(zend_op_array *) * source->op_array.num_dynamic_func_defs);
452
                while (it < source->op_array.num_dynamic_func_defs) {
453
                        closure->op_array.dynamic_func_defs[it] = (zend_op_array *)php_parallel_cache_closure(
454
                            (zend_function *)source->op_array.dynamic_func_defs[it], NULL);
455
                        it++;
456
                }
457
        }
458
#endif
459

460
        return closure;
155✔
461
} /* }}} */
462

463
zend_function *php_parallel_cache_function(const zend_function *source)
12✔
464
{ /* {{{ */
465
        return php_parallel_cache_function_ex(source, 1);
12✔
466
} /* }}} */
467

468
/* {{{ */
469
PHP_MINIT_FUNCTION(PARALLEL_CACHE)
292✔
470
{
471
        zend_hash_init(&PCG(table), 32, NULL, NULL, 1);
292✔
472

473
        PCM(size) = PARALLEL_CACHE_CHUNK;
292✔
474
        PCM(mem) = PCM(block) = malloc(PCM(size));
292✔
475

476
        if (!PCM(mem)) {
292✔
477
                /* out of memory */
478
        }
292✔
479

480
        return SUCCESS;
292✔
481
}
482

483
PHP_MSHUTDOWN_FUNCTION(PARALLEL_CACHE)
292✔
484
{
485
        zend_hash_destroy(&PCG(table));
292✔
486

487
        if (PCM(mem))
292✔
488
                free(PCM(mem));
292✔
489

490
        return SUCCESS;
292✔
491
} /* }}} */
492
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc