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

krakjoe / parallel / 22640252886

03 Mar 2026 08:00PM UTC coverage: 94.938% (-0.07%) from 95.012%
22640252886

Pull #370

github

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

40 of 44 new or added lines in 2 files covered. (90.91%)

2 existing lines in 1 file now uncovered.

2851 of 3003 relevant lines covered (94.94%)

2216.75 hits per line

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

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

45
        ZEND_ASSERT(size < PARALLEL_CACHE_CHUNK);
3,652✔
46

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

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

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

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

63
        return mem;
3,652✔
64
}
65

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

70
        memcpy(destination, source, size);
2,161✔
71

72
        return destination;
2,253✔
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)
266✔
90
{ /* {{{ */
91
        zend_type *single;
266✔
92

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

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

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

102
                if (ZEND_TYPE_USES_ARENA(*type)) {
6✔
103
                        ZEND_TYPE_FULL_MASK(*type) &= ~_ZEND_TYPE_ARENA_BIT;
5✔
104
                }
105

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

109
        ZEND_TYPE_FOREACH(*type, single)
150✔
110
        {
111
                if (ZEND_TYPE_HAS_NAME(*single)) {
156✔
112
                        zend_string *name = ZEND_TYPE_NAME(*single);
90✔
113

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

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

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

130
        if (statics && cached->static_variables) {
871✔
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);
592✔
136
#else
137
        ZEND_MAP_PTR_INIT(cached->static_variables_ptr, &cached->static_variables);
279✔
138
#endif
139

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

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

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

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

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

161
        cached->refcount = NULL;
865✔
162

163
        if (cached->last_literal) {
865✔
164
                zend_op *src_opline = source->op_array.opcodes;
865✔
165
                zend_op *src_end = src_opline + source->op_array.last;
865✔
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);
865✔
170
                memset(literal_map, 0, sizeof(uint32_t) * cached->last_literal);
865✔
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) {
6,603✔
177
                        if (src_opline->opcode == ZEND_INIT_FCALL && src_opline->op2_type == IS_CONST) {
5,738✔
178
                                uint32_t idx;
270✔
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);
270✔
183
#endif
184
                                if (idx < cached->last_literal) {
270✔
185
                                        if (literal_map[idx] == 0) {
270✔
186
                                                literal_map[idx] = 1;
270✔
187
                                                new_last_literal++;
270✔
188
                                        }
189
                                }
190
                        }
191
                        src_opline++;
5,738✔
192
                }
193
        }
194

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

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

202
                cached->literals = slot;
865✔
203

204
                for (uint32_t i = 0; i < cached->last_literal; i++) {
4,325✔
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;
3,460✔
208

209
                        if (Z_TYPE_P(literal) == IS_ARRAY) {
3,460✔
210
                                ZVAL_ARR(slot, php_parallel_copy_hash_persistent(Z_ARRVAL_P(literal), php_parallel_copy_string_interned,
42✔
211
                                                                                 php_parallel_cache_copy_mem,
212
                                                                                 PHP_PARALLEL_COPY_STORAGE_CACHE_POOL));
213
                        } else if (Z_TYPE_P(literal) == IS_STRING) {
3,418✔
214
                                ZVAL_STR(slot, php_parallel_copy_string_interned(Z_STR_P(literal)));
1,986✔
215
                        } else {
216
                                *slot = *literal;
1,432✔
217
                        }
218

219
                        Z_TYPE_FLAGS_P(slot) &= ~(IS_TYPE_REFCOUNTED | IS_TYPE_COLLECTABLE);
3,460✔
220

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

231
                        literal++;
3,460✔
232
                        slot++;
3,460✔
233
                        idx++;
3,460✔
234
                }
235
                cached->last_literal = new_last_literal;
865✔
236
        }
237

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

243
                while (it < end) {
1,269✔
244
                        heap[it] = php_parallel_copy_string_interned(vars[it]);
735✔
245
                        it++;
735✔
246
                }
247
                cached->vars = heap;
534✔
248
        }
249

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

254
                while (opline < end) {
6,603✔
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) {
5,738✔
259
                                opline->opcode = ZEND_INIT_FCALL_BY_NAME;
270✔
260
                                opline->op1_type = IS_UNUSED;
270✔
261
                                opline->op1.var = 0;
270✔
262
                                ZEND_VM_SET_OPCODE_HANDLER(opline);
270✔
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) {
5,738✔
268
                                uint32_t idx;
1,833✔
269
                                zend_op *src_opline = source->op_array.opcodes + (opline - opcodes);
1,833✔
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);
1,833✔
275
                                opline->op1.constant = (char *)&cached->literals[offset_map[idx]] - (char *)opline;
1,833✔
276
#endif
277
                                if (opline->opcode == ZEND_SEND_VAL || opline->opcode == ZEND_SEND_VAL_EX ||
1,833✔
278
                                    opline->opcode == ZEND_QM_ASSIGN) {
279
                                        zend_vm_set_opcode_handler_ex(opline, 0, 0, 0);
255✔
280
                                }
281
                        }
282
                        if (opline->op2_type == IS_CONST) {
5,738✔
283
                                uint32_t idx;
1,014✔
284
                                zend_op *src_opline = source->op_array.opcodes + (opline - opcodes);
1,014✔
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);
1,014✔
290
                                opline->op2.constant = (char *)&cached->literals[offset_map[idx]] - (char *)opline;
1,014✔
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++;
5,738✔
323
                }
324
                cached->opcodes = opcodes;
865✔
325
        }
326

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

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

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

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

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

347
                while (it < end) {
484✔
348
                        if (info->name) {
266✔
349
                                info->name = php_parallel_copy_string_interned(it->name);
230✔
350
                        }
351

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

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

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

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

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

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

378
        if (cached->doc_comment)
865✔
379
                cached->doc_comment = php_parallel_copy_string_interned(cached->doc_comment);
6✔
380

381
_php_parallel_cached_function_return:
859✔
382
        return cached;
871✔
383
} /* }}} */
384

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

390
        pthread_mutex_lock(&PCG(mutex));
1,037✔
391

392
        /* Use a composite key mixing opcodes and refcount pointers.
393
         * opcodes alone can be reused by the allocator after the original closure
394
         * is freed (#309, #369). refcount is a separate allocation per compilation,
395
         * so combining both makes key collisions between different closures
396
         * virtually impossible. */
397
        zend_ulong cache_key = (zend_ulong)source->op_array.opcodes
1,037✔
398
                             ^ ((zend_ulong)source->op_array.refcount * 2654435761u);
1,037✔
399

400
        if ((cached = zend_hash_index_find_ptr(&PCG(table), cache_key))) {
1,273✔
401
                goto _php_parallel_cached_function_return;
236✔
402
        }
403

404
        cached = php_parallel_cache_create(source, statics);
801✔
405

406
        zend_hash_index_update_ptr(&PCG(table), cache_key, cached);
801✔
407

408
_php_parallel_cached_function_return:
1,037✔
409
        pthread_mutex_unlock(&PCG(mutex));
1,037✔
410

411
        return (zend_function *)cached;
1,037✔
412
} /* }}} */
413

414
zend_function *php_parallel_cache_closure(const zend_function *source, zend_function *closure)
1,025✔
415
{ /* {{{ */
416
        zend_op_array *cache;
1,025✔
417

418
        cache = (zend_op_array *)php_parallel_cache_function_ex((zend_function *)source, 0);
1,025✔
419

420
        if (!closure) {
1,025✔
421
                closure = php_parallel_copy_mem(cache, sizeof(zend_op_array), 1);
797✔
422
        } else {
423
                memcpy(closure, cache, sizeof(zend_op_array));
228✔
424
        }
425

426
        if (source->op_array.static_variables) {
1,025✔
427
                HashTable *statics = ZEND_MAP_PTR_GET(source->op_array.static_variables_ptr);
243✔
428

429
                if (statics) {
243✔
430
                        closure->op_array.static_variables = php_parallel_copy_hash_ctor(statics, 1);
198✔
431

432
#if PHP_VERSION_ID >= 80200
433
                        ZEND_MAP_PTR_INIT(closure->op_array.static_variables_ptr, closure->op_array.static_variables);
132✔
434
#else
435
                        ZEND_MAP_PTR_INIT(closure->op_array.static_variables_ptr, &closure->op_array.static_variables);
66✔
436
#endif
437
                }
438
        }
439

440
#if PHP_VERSION_ID >= 80100
441
        if (source->op_array.num_dynamic_func_defs) {
870✔
442
                uint32_t it = 0;
65✔
443
                /* Use regular persistent memory for dynamic_func_defs array, not cache pool */
444
                closure->op_array.dynamic_func_defs =
130✔
445
                    pemalloc(sizeof(zend_op_array *) * source->op_array.num_dynamic_func_defs, 1);
65✔
446
                memcpy(closure->op_array.dynamic_func_defs, source->op_array.dynamic_func_defs,
205✔
447
                       sizeof(zend_op_array *) * source->op_array.num_dynamic_func_defs);
65✔
448
                while (it < source->op_array.num_dynamic_func_defs) {
140✔
449
                        closure->op_array.dynamic_func_defs[it] = (zend_op_array *)php_parallel_cache_closure(
150✔
450
                            (zend_function *)source->op_array.dynamic_func_defs[it], NULL);
75✔
451
                        it++;
75✔
452
                }
453
        }
454
#endif
455

456
        return closure;
1,025✔
457
} /* }}} */
458

459
zend_function *php_parallel_cache_function(const zend_function *source)
12✔
460
{ /* {{{ */
461
        return php_parallel_cache_function_ex(source, 1);
12✔
462
} /* }}} */
463

464
/* {{{ */
465
PHP_MINIT_FUNCTION(PARALLEL_CACHE)
1,117✔
466
{
467
        zend_hash_init(&PCG(table), 32, NULL, NULL, 1);
1,117✔
468

469
        PCM(size) = PARALLEL_CACHE_CHUNK;
1,117✔
470
        PCM(mem) = PCM(block) = malloc(PCM(size));
1,117✔
471

472
        if (!PCM(mem)) {
1,117✔
473
                /* out of memory */
474
        }
1,117✔
475

476
        return SUCCESS;
1,117✔
477
}
478

479
PHP_MSHUTDOWN_FUNCTION(PARALLEL_CACHE)
1,117✔
480
{
481
        zend_hash_destroy(&PCG(table));
1,117✔
482

483
        if (PCM(mem))
1,117✔
484
                free(PCM(mem));
1,117✔
485

486
        return SUCCESS;
1,117✔
487
} /* }}} */
488
#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