Skip to content

Commit 2d78a98

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

File tree

6 files changed

+60
-507
lines changed

6 files changed

+60
-507
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: 17 additions & 7 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,16 +369,19 @@ 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);
373+
char *tmp_key = NULL;
374+
char *tmp_salt = NULL;
373375

374376
if ((key - (char *) 0) % __alignof__ (uint32_t) != 0) {
375-
char *tmp = (char *) alloca(key_len + __alignof__(uint32_t));
376-
key = copied_key = memcpy(tmp + __alignof__(uint32_t) - (tmp - (char *) 0) % __alignof__(uint32_t), key, key_len);
377+
tmp_key = (char *) do_alloca(key_len + __alignof__(uint32_t), use_heap);
378+
key = copied_key = memcpy(tmp_key + __alignof__(uint32_t) - (tmp_key - (char *) 0) % __alignof__(uint32_t), key, key_len);
377379
}
378380

379381
if ((salt - (char *) 0) % __alignof__(uint32_t) != 0) {
380-
char *tmp = (char *) alloca(salt_len + 1 + __alignof__(uint32_t));
382+
tmp_salt = (char *) do_alloca(salt_len + 1 + __alignof__(uint32_t), use_heap);
381383
salt = copied_salt =
382-
memcpy(tmp + __alignof__(uint32_t) - (tmp - (char *) 0) % __alignof__ (uint32_t), salt, salt_len);
384+
memcpy(tmp_salt + __alignof__(uint32_t) - (tmp_salt - (char *) 0) % __alignof__ (uint32_t), salt, salt_len);
383385
copied_salt[salt_len] = 0;
384386
}
385387

@@ -443,7 +445,7 @@ char * php_sha256_crypt_r(const char *key, const char *salt, char *buffer, int b
443445
sha256_finish_ctx(&alt_ctx, temp_result);
444446

445447
/* Create byte sequence P. */
446-
cp = p_bytes = alloca(key_len);
448+
cp = p_bytes = do_alloca(key_len, use_heap);
447449
for (cnt = key_len; cnt >= 32; cnt -= 32) {
448450
cp = __php_mempcpy((void *)cp, (const void *)temp_result, 32);
449451
}
@@ -461,7 +463,7 @@ char * php_sha256_crypt_r(const char *key, const char *salt, char *buffer, int b
461463
sha256_finish_ctx(&alt_ctx, temp_result);
462464

463465
/* Create byte sequence S. */
464-
cp = s_bytes = alloca(salt_len);
466+
cp = s_bytes = do_alloca(salt_len, use_heap);
465467
for (cnt = salt_len; cnt >= 32; cnt -= 32) {
466468
cp = __php_mempcpy(cp, temp_result, 32);
467469
}
@@ -571,6 +573,14 @@ char * php_sha256_crypt_r(const char *key, const char *salt, char *buffer, int b
571573
if (copied_salt != NULL) {
572574
ZEND_SECURE_ZERO(copied_salt, salt_len);
573575
}
576+
if (tmp_key != NULL) {
577+
free_alloca(tmp_key, use_heap);
578+
}
579+
if (tmp_salt != NULL) {
580+
free_alloca(tmp_salt, use_heap);
581+
}
582+
free_alloca(p_bytes, use_heap);
583+
free_alloca(s_bytes, use_heap);
574584

575585
return buffer;
576586
}

ext/standard/crypt_sha512.c

Lines changed: 17 additions & 7 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,16 +403,19 @@ 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);
407+
char *tmp_key = NULL;
408+
char *tmp_salt = NULL;
407409

408410
if ((key - (char *) 0) % __alignof__ (uint64_t) != 0) {
409-
char *tmp = (char *) alloca (key_len + __alignof__ (uint64_t));
411+
tmp_key = (char *) do_alloca(key_len + __alignof__ (uint64_t), use_heap);
410412
key = copied_key =
411-
memcpy(tmp + __alignof__(uint64_t) - (tmp - (char *) 0) % __alignof__(uint64_t), key, key_len);
413+
memcpy(tmp_key + __alignof__(uint64_t) - (tmp_key - (char *) 0) % __alignof__(uint64_t), key, key_len);
412414
}
413415

414416
if ((salt - (char *) 0) % __alignof__ (uint64_t) != 0) {
415-
char *tmp = (char *) alloca(salt_len + 1 + __alignof__(uint64_t));
416-
salt = copied_salt = memcpy(tmp + __alignof__(uint64_t) - (tmp - (char *) 0) % __alignof__(uint64_t), salt, salt_len);
417+
tmp_salt = (char *) do_alloca(salt_len + 1 + __alignof__(uint64_t), use_heap);
418+
salt = copied_salt = memcpy(tmp_salt + __alignof__(uint64_t) - (tmp_salt - (char *) 0) % __alignof__(uint64_t), salt, salt_len);
417419
copied_salt[salt_len] = 0;
418420
}
419421

@@ -477,7 +479,7 @@ php_sha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen)
477479
sha512_finish_ctx(&alt_ctx, temp_result);
478480

479481
/* Create byte sequence P. */
480-
cp = p_bytes = alloca(key_len);
482+
cp = p_bytes = do_alloca(key_len, use_heap);
481483
for (cnt = key_len; cnt >= 64; cnt -= 64) {
482484
cp = __php_mempcpy((void *) cp, (const void *)temp_result, 64);
483485
}
@@ -496,7 +498,7 @@ php_sha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen)
496498
sha512_finish_ctx(&alt_ctx, temp_result);
497499

498500
/* Create byte sequence S. */
499-
cp = s_bytes = alloca(salt_len);
501+
cp = s_bytes = do_alloca(salt_len, use_heap);
500502
for (cnt = salt_len; cnt >= 64; cnt -= 64) {
501503
cp = __php_mempcpy(cp, temp_result, 64);
502504
}
@@ -618,6 +620,14 @@ php_sha512_crypt_r(const char *key, const char *salt, char *buffer, int buflen)
618620
if (copied_salt != NULL) {
619621
ZEND_SECURE_ZERO(copied_salt, salt_len);
620622
}
623+
if (tmp_key != NULL) {
624+
free_alloca(tmp_key, use_heap);
625+
}
626+
if (tmp_salt != NULL) {
627+
free_alloca(tmp_salt, use_heap);
628+
}
629+
free_alloca(p_bytes, use_heap);
630+
free_alloca(s_bytes, use_heap);
621631

622632
return buffer;
623633
}

0 commit comments

Comments
 (0)