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

krakjoe / parallel / 20458720488

23 Dec 2025 10:51AM UTC coverage: 89.511% (-5.5%) from 94.99%
20458720488

Pull #363

github

web-flow
Merge 61b9a7d7d into 3c00d4ed1
Pull Request #363: Fix cache key collision

7 of 9 new or added lines in 1 file covered. (77.78%)

157 existing lines in 5 files now uncovered.

2637 of 2946 relevant lines covered (89.51%)

352.17 hits per line

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

37.56
/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)
149✔
41
{
42
        void  *mem;
149✔
43
        size_t aligned = PARALLEL_PLATFORM_ALIGNED(size);
149✔
44

45
        ZEND_ASSERT(size < PARALLEL_CACHE_CHUNK);
149✔
46

47
        if ((PCM(used) + aligned) >= PCM(size)) {
149✔
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);
149✔
60
        PCM(block) = (void *)(((char *)PCM(block)) + aligned);
149✔
61
        PCM(used) += aligned;
149✔
62

63
        return mem;
149✔
64
}
65

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

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

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

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

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

UNCOV
83
        cached = php_parallel_copy_hash_persistent(statics, php_parallel_copy_string_interned, php_parallel_cache_copy_mem);
×
84

UNCOV
85
        return zend_hash_index_update_ptr(&PCG(table), (zend_ulong)statics, cached);
×
86
} /* }}} */
87

UNCOV
88
static zend_always_inline void php_parallel_cache_type(zend_type *type)
×
89
{ /* {{{ */
UNCOV
90
        zend_type *single;
×
91

UNCOV
92
        if (!ZEND_TYPE_IS_SET(*type)) {
×
93
                return;
94
        }
95

UNCOV
96
        if (ZEND_TYPE_HAS_LIST(*type)) {
×
UNCOV
97
                zend_type_list *list = ZEND_TYPE_LIST(*type);
×
98

UNCOV
99
                list = php_parallel_cache_copy_mem(list, ZEND_TYPE_LIST_SIZE(list->num_types));
×
100

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

UNCOV
105
                ZEND_TYPE_SET_PTR(*type, list);
×
106
        }
107

UNCOV
108
        ZEND_TYPE_FOREACH(*type, single)
×
109
        {
UNCOV
110
                if (ZEND_TYPE_HAS_NAME(*single)) {
×
UNCOV
111
                        zend_string *name = ZEND_TYPE_NAME(*single);
×
112

UNCOV
113
                        ZEND_TYPE_SET_PTR(*single, php_parallel_copy_string_interned(name));
×
114
                }
115
        }
UNCOV
116
        ZEND_TYPE_FOREACH_END();
×
117
} /* }}} */
118

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

127
        cached->fn_flags |= ZEND_ACC_IMMUTABLE;
137✔
128

129
        if (statics && cached->static_variables) {
137✔
UNCOV
130
                cached->static_variables = php_parallel_cache_statics(cached->static_variables);
×
131
        }
132

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

139
        ZEND_MAP_PTR_INIT(cached->run_time_cache, NULL);
137✔
140

141
#if PHP_VERSION_ID >= 80100
142
        if (cached->num_dynamic_func_defs) {
137✔
143
                uint32_t it = 0;
12✔
144

145
                cached->dynamic_func_defs = php_parallel_cache_copy_mem(
24✔
146
                    cached->dynamic_func_defs, sizeof(zend_op_array *) * cached->num_dynamic_func_defs);
12✔
147

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

156
        if (!cached->refcount) {
137✔
157
                goto _php_parallel_cached_function_return;
137✔
158
        }
159

UNCOV
160
        cached->refcount = NULL;
×
161

UNCOV
162
        if (cached->last_literal) {
×
UNCOV
163
                zend_op *src_opline = source->op_array.opcodes;
×
UNCOV
164
                zend_op *src_end = src_opline + source->op_array.last;
×
165

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

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

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

UNCOV
199
                offset_map = emalloc(sizeof(uint32_t) * cached->last_literal);
×
200

UNCOV
201
                cached->literals = slot;
×
202

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

UNCOV
208
                        if (Z_TYPE_P(literal) == IS_ARRAY) {
×
UNCOV
209
                                ZVAL_ARR(slot, php_parallel_copy_hash_persistent(Z_ARRVAL_P(literal), php_parallel_copy_string_interned,
×
210
                                                                                 php_parallel_cache_copy_mem));
UNCOV
211
                        } else if (Z_TYPE_P(literal) == IS_STRING) {
×
UNCOV
212
                                ZVAL_STR(slot, php_parallel_copy_string_interned(Z_STR_P(literal)));
×
213
                        } else {
UNCOV
214
                                *slot = *literal;
×
215
                        }
216

UNCOV
217
                        Z_TYPE_FLAGS_P(slot) &= ~(IS_TYPE_REFCOUNTED | IS_TYPE_COLLECTABLE);
×
218

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

UNCOV
229
                        literal++;
×
UNCOV
230
                        slot++;
×
UNCOV
231
                        idx++;
×
232
                }
UNCOV
233
                cached->last_literal = new_last_literal;
×
234
        }
235

UNCOV
236
        if (cached->last_var) {
×
UNCOV
237
                zend_string **vars = cached->vars;
×
UNCOV
238
                uint32_t      it = 0, end = cached->last_var;
×
UNCOV
239
                zend_string **heap = php_parallel_cache_alloc(cached->last_var * sizeof(zend_string *));
×
240

UNCOV
241
                while (it < end) {
×
UNCOV
242
                        heap[it] = php_parallel_copy_string_interned(vars[it]);
×
UNCOV
243
                        it++;
×
244
                }
UNCOV
245
                cached->vars = heap;
×
246
        }
247

UNCOV
248
        if (cached->last) {
×
UNCOV
249
                zend_op *opcodes = php_parallel_cache_copy_mem(cached->opcodes, sizeof(zend_op) * cached->last);
×
UNCOV
250
                zend_op *opline = opcodes, *end = opline + cached->last;
×
251

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

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

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

UNCOV
320
                        opline++;
×
321
                }
UNCOV
322
                cached->opcodes = opcodes;
×
323
        }
324

UNCOV
325
        if (literal_map) {
×
UNCOV
326
                efree(literal_map);
×
327
        }
328

UNCOV
329
        if (offset_map) {
×
UNCOV
330
                efree(offset_map);
×
331
        }
332

UNCOV
333
        if (cached->arg_info) {
×
UNCOV
334
                zend_arg_info *it = cached->arg_info, *end = it + cached->num_args, *info;
×
335

UNCOV
336
                if (cached->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
×
UNCOV
337
                        it--;
×
338
                }
UNCOV
339
                if (cached->fn_flags & ZEND_ACC_VARIADIC) {
×
UNCOV
340
                        end++;
×
341
                }
342

UNCOV
343
                cached->arg_info = info = php_parallel_cache_copy_mem(it, (end - it) * sizeof(zend_arg_info));
×
344

UNCOV
345
                while (it < end) {
×
UNCOV
346
                        if (info->name) {
×
UNCOV
347
                                info->name = php_parallel_copy_string_interned(it->name);
×
348
                        }
349

UNCOV
350
                        php_parallel_cache_type(&info->type);
×
351

UNCOV
352
                        info++;
×
UNCOV
353
                        it++;
×
354
                }
UNCOV
355
                if (cached->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
×
UNCOV
356
                        cached->arg_info++;
×
357
                }
358
        }
359

UNCOV
360
        if (cached->try_catch_array) {
×
UNCOV
361
                cached->try_catch_array = php_parallel_cache_copy_mem(cached->try_catch_array,
×
UNCOV
362
                                                                      sizeof(zend_try_catch_element) * cached->last_try_catch);
×
363
        }
364

UNCOV
365
        if (cached->live_range) {
×
UNCOV
366
                cached->live_range =
×
UNCOV
367
                    php_parallel_cache_copy_mem(cached->live_range, sizeof(zend_live_range) * cached->last_live_range);
×
368
        }
369

UNCOV
370
        if (cached->function_name)
×
UNCOV
371
                cached->function_name = php_parallel_copy_string_interned(cached->function_name);
×
372

UNCOV
373
        if (cached->filename)
×
UNCOV
374
                cached->filename = php_parallel_copy_string_interned(cached->filename);
×
375

UNCOV
376
        if (cached->doc_comment)
×
UNCOV
377
                cached->doc_comment = php_parallel_copy_string_interned(cached->doc_comment);
×
378

UNCOV
379
_php_parallel_cached_function_return:
×
380
        return cached;
137✔
381
} /* }}} */
382

383
/* {{{ */
384
static zend_always_inline zend_function *php_parallel_cache_function_ex(const zend_function *source, bool statics)
161✔
385
{
386
        zend_op_array *cached;
161✔
387
        zend_string   *cache_key;
161✔
388

389
        pthread_mutex_lock(&PCG(mutex));
161✔
390

391
        /* Use function name as cache key if available, otherwise fall back to opcodes pointer */
392
        if (source->op_array.function_name) {
161✔
393
                cache_key = source->op_array.function_name;
161✔
394
                if ((cached = zend_hash_find_ptr(&PCG(table), cache_key))) {
199✔
395
                        goto _php_parallel_cached_function_return;
38✔
396
                }
397
        } else {
NEW
398
                if ((cached = zend_hash_index_find_ptr(&PCG(table), (zend_ulong)source->op_array.opcodes))) {
×
UNCOV
399
                        goto _php_parallel_cached_function_return;
×
400
                }
401
        }
402

403
        cached = php_parallel_cache_create(source, statics);
123✔
404

405
        /* Store in cache using the same key type we used for lookup */
406
        if (source->op_array.function_name) {
123✔
407
                zend_hash_add_ptr(&PCG(table), cache_key, cached);
123✔
408
        } else {
NEW
409
                zend_hash_index_add_ptr(&PCG(table), (zend_ulong)source->op_array.opcodes, cached);
×
410
        }
411

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

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

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

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

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

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

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

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

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

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

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

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

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

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

480
        return SUCCESS;
154✔
481
}
482

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

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

490
        return SUCCESS;
154✔
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