Skip to content

Commit 212b11d

Browse files
committed
Remove custom alloca
Use do_alloca in place of alloca Add support for Windows
1 parent be9adc4 commit 212b11d

File tree

6 files changed

+44
-503
lines changed

6 files changed

+44
-503
lines changed

Zend/Optimizer/dce.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ int dce_optimize_op_array(zend_op_array *op_array, zend_ssa *ssa, bool reorder_d
530530

531531
/* DCE of CV operations that changes arguments may affect vararg functions. */
532532
bool has_varargs = (ssa->cfg.flags & ZEND_FUNC_VARARG) != 0;
533+
ALLOCA_FLAG(use_heap);
533534

534535
context ctx;
535536
ctx.ssa = ssa;
@@ -538,18 +539,18 @@ int dce_optimize_op_array(zend_op_array *op_array, zend_ssa *ssa, bool reorder_d
538539

539540
/* We have no dedicated phi vector, so we use the whole ssa var vector instead */
540541
ctx.instr_worklist_len = zend_bitset_len(op_array->last);
541-
ctx.instr_worklist = alloca(sizeof(zend_ulong) * ctx.instr_worklist_len);
542+
ctx.instr_worklist = do_alloca(sizeof(zend_ulong) * ctx.instr_worklist_len, use_heap);
542543
memset(ctx.instr_worklist, 0, sizeof(zend_ulong) * ctx.instr_worklist_len);
543544
ctx.phi_worklist_len = zend_bitset_len(ssa->vars_count);
544-
ctx.phi_worklist = alloca(sizeof(zend_ulong) * ctx.phi_worklist_len);
545+
ctx.phi_worklist = do_alloca(sizeof(zend_ulong) * ctx.phi_worklist_len, use_heap);
545546
memset(ctx.phi_worklist, 0, sizeof(zend_ulong) * ctx.phi_worklist_len);
546-
ctx.phi_worklist_no_val = alloca(sizeof(zend_ulong) * ctx.phi_worklist_len);
547+
ctx.phi_worklist_no_val = do_alloca(sizeof(zend_ulong) * ctx.phi_worklist_len, use_heap);
547548
memset(ctx.phi_worklist_no_val, 0, sizeof(zend_ulong) * ctx.phi_worklist_len);
548549

549550
/* Optimistically assume all instructions and phis to be dead */
550-
ctx.instr_dead = alloca(sizeof(zend_ulong) * ctx.instr_worklist_len);
551+
ctx.instr_dead = do_alloca(sizeof(zend_ulong) * ctx.instr_worklist_len, use_heap);
551552
memset(ctx.instr_dead, 0, sizeof(zend_ulong) * ctx.instr_worklist_len);
552-
ctx.phi_dead = alloca(sizeof(zend_ulong) * ctx.phi_worklist_len);
553+
ctx.phi_dead = do_alloca(sizeof(zend_ulong) * ctx.phi_worklist_len, use_heap);
553554
memset(ctx.phi_dead, 0xff, sizeof(zend_ulong) * ctx.phi_worklist_len);
554555

555556
/* Mark non-CV phis as live. Even if the result is unused, we generally cannot remove one
@@ -664,5 +665,11 @@ int dce_optimize_op_array(zend_op_array *op_array, zend_ssa *ssa, bool reorder_d
664665
}
665666
} FOREACH_PHI_END();
666667

668+
free_alloca(ctx.instr_worklist, use_heap);
669+
free_alloca(ctx.phi_worklist, use_heap);
670+
free_alloca(ctx.phi_worklist_no_val, use_heap);
671+
free_alloca(ctx.instr_dead, use_heap);
672+
free_alloca(ctx.phi_dead, use_heap);
673+
667674
return removed_ops;
668675
}

Zend/zend_portability.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,25 @@ char *alloca();
350350
# define ALLOCA_FLAG(name) \
351351
bool name;
352352
# define SET_ALLOCA_FLAG(name) \
353-
name = 1
353+
name = true
354354
# define do_alloca_ex(size, limit, use_heap) \
355355
((use_heap = (UNEXPECTED((size) > (limit)))) ? emalloc(size) : alloca(size))
356356
# define do_alloca(size, use_heap) \
357357
do_alloca_ex(size, ZEND_ALLOCA_MAX_SIZE, use_heap)
358358
# define free_alloca(p, use_heap) \
359359
do { if (UNEXPECTED(use_heap)) efree(p); } while (0)
360+
#elif defined(PHP_WIN32)
361+
# define ZEND_ALLOCA_MAX_SIZE (32 * 1024)
362+
# define ALLOCA_FLAG(name) \
363+
bool name;
364+
# define SET_ALLOCA_FLAG(name) \
365+
name = true
366+
# define do_alloca_ex(size, limit, use_heap) \
367+
((use_heap = (UNEXPECTED((size) > (limit)))) ? emalloc(size) : _malloca(size))
368+
# define do_alloca(size, use_heap) \
369+
do_alloca_ex(size, ZEND_ALLOCA_MAX_SIZE, use_heap)
370+
# define free_alloca(p, use_heap) \
371+
do { if (UNEXPECTED(use_heap)) { efree(p); } else { _freea(p); } } while (0)
360372
#else
361373
# define ALLOCA_FLAG(name)
362374
# define SET_ALLOCA_FLAG(name)

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,7 @@ PHP_INSTALL_HEADERS([Zend/Optimizer], [ \
16101610
PHP_ADD_SOURCES(TSRM, TSRM.c, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
16111611

16121612
PHP_ADD_SOURCES(main, main.c snprintf.c spprintf.c \
1613-
fopen_wrappers.c alloca.c php_scandir.c \
1613+
fopen_wrappers.c php_scandir.c \
16141614
php_ini_builder.c \
16151615
php_ini.c SAPI.c rfc1867.c php_content_types.c strlcpy.c \
16161616
strlcat.c explicit_bzero.c reentrancy.c php_variables.c php_ticks.c \

ext/standard/crypt_sha256.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#ifdef PHP_WIN32
1212
# define __alignof__ __alignof
13-
# define alloca _alloca
1413
#else
1514
# ifndef HAVE_ALIGNOF
1615
# include <stddef.h>
@@ -370,14 +369,15 @@ char * php_sha256_crypt_r(const char *key, const char *salt, char *buffer, int b
370369

371370
salt_len = MIN(strcspn(salt, "$"), SALT_LEN_MAX);
372371
key_len = strlen(key);
372+
ALLOCA_FLAG(use_heap);
373373

374374
if ((key - (char *) 0) % __alignof__ (uint32_t) != 0) {
375-
char *tmp = (char *) alloca(key_len + __alignof__(uint32_t));
375+
char *tmp = (char *) do_alloca(key_len + __alignof__(uint32_t), use_heap);
376376
key = copied_key = memcpy(tmp + __alignof__(uint32_t) - (tmp - (char *) 0) % __alignof__(uint32_t), key, key_len);
377377
}
378378

379379
if ((salt - (char *) 0) % __alignof__(uint32_t) != 0) {
380-
char *tmp = (char *) alloca(salt_len + 1 + __alignof__(uint32_t));
380+
char *tmp = (char *) do_alloca(salt_len + 1 + __alignof__(uint32_t), use_heap);
381381
salt = copied_salt =
382382
memcpy(tmp + __alignof__(uint32_t) - (tmp - (char *) 0) % __alignof__ (uint32_t), salt, salt_len);
383383
copied_salt[salt_len] = 0;
@@ -443,7 +443,7 @@ char * php_sha256_crypt_r(const char *key, const char *salt, char *buffer, int b
443443
sha256_finish_ctx(&alt_ctx, temp_result);
444444

445445
/* Create byte sequence P. */
446-
cp = p_bytes = alloca(key_len);
446+
cp = p_bytes = do_alloca(key_len, use_heap);
447447
for (cnt = key_len; cnt >= 32; cnt -= 32) {
448448
cp = __php_mempcpy((void *)cp, (const void *)temp_result, 32);
449449
}
@@ -461,7 +461,7 @@ char * php_sha256_crypt_r(const char *key, const char *salt, char *buffer, int b
461461
sha256_finish_ctx(&alt_ctx, temp_result);
462462

463463
/* Create byte sequence S. */
464-
cp = s_bytes = alloca(salt_len);
464+
cp = s_bytes = do_alloca(salt_len, use_heap);
465465
for (cnt = salt_len; cnt >= 32; cnt -= 32) {
466466
cp = __php_mempcpy(cp, temp_result, 32);
467467
}
@@ -571,6 +571,10 @@ char * php_sha256_crypt_r(const char *key, const char *salt, char *buffer, int b
571571
if (copied_salt != NULL) {
572572
ZEND_SECURE_ZERO(copied_salt, salt_len);
573573
}
574+
free_alloca(key, use_heap);
575+
free_alloca(salt, use_heap);
576+
free_alloca(p_bytes, use_heap);
577+
free_alloca(s_bytes, use_heap);
574578

575579
return buffer;
576580
}

ext/standard/crypt_sha512.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <limits.h>
1010
#ifdef PHP_WIN32
1111
# define __alignof__ __alignof
12-
# define alloca _alloca
1312
#else
1413
# ifndef HAVE_ALIGNOF
1514
# include <stddef.h>
@@ -404,15 +403,16 @@ php_sha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen)
404403

405404
salt_len = MIN(strcspn(salt, "$"), SALT_LEN_MAX);
406405
key_len = strlen(key);
406+
ALLOCA_FLAG(use_heap);
407407

408408
if ((key - (char *) 0) % __alignof__ (uint64_t) != 0) {
409-
char *tmp = (char *) alloca (key_len + __alignof__ (uint64_t));
409+
char *tmp = (char *) do_alloca(key_len + __alignof__ (uint64_t), use_heap);
410410
key = copied_key =
411411
memcpy(tmp + __alignof__(uint64_t) - (tmp - (char *) 0) % __alignof__(uint64_t), key, key_len);
412412
}
413413

414414
if ((salt - (char *) 0) % __alignof__ (uint64_t) != 0) {
415-
char *tmp = (char *) alloca(salt_len + 1 + __alignof__(uint64_t));
415+
char *tmp = (char *) do_alloca(salt_len + 1 + __alignof__(uint64_t), use_heap);
416416
salt = copied_salt = memcpy(tmp + __alignof__(uint64_t) - (tmp - (char *) 0) % __alignof__(uint64_t), salt, salt_len);
417417
copied_salt[salt_len] = 0;
418418
}
@@ -477,7 +477,7 @@ php_sha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen)
477477
sha512_finish_ctx(&alt_ctx, temp_result);
478478

479479
/* Create byte sequence P. */
480-
cp = p_bytes = alloca(key_len);
480+
cp = p_bytes = do_alloca(key_len, use_heap);
481481
for (cnt = key_len; cnt >= 64; cnt -= 64) {
482482
cp = __php_mempcpy((void *) cp, (const void *)temp_result, 64);
483483
}
@@ -496,7 +496,7 @@ php_sha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen)
496496
sha512_finish_ctx(&alt_ctx, temp_result);
497497

498498
/* Create byte sequence S. */
499-
cp = s_bytes = alloca(salt_len);
499+
cp = s_bytes = do_alloca(salt_len, use_heap);
500500
for (cnt = salt_len; cnt >= 64; cnt -= 64) {
501501
cp = __php_mempcpy(cp, temp_result, 64);
502502
}
@@ -618,6 +618,10 @@ php_sha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen)
618618
if (copied_salt != NULL) {
619619
ZEND_SECURE_ZERO(copied_salt, salt_len);
620620
}
621+
free_alloca(key, use_heap);
622+
free_alloca(salt, use_heap);
623+
free_alloca(p_bytes, use_heap);
624+
free_alloca(s_bytes, use_heap);
621625

622626
return buffer;
623627
}

0 commit comments

Comments
 (0)