Skip to content

Commit 6c05d26

Browse files
committed
Use int64 for zend_long
1 parent 9c33091 commit 6c05d26

18 files changed

+117
-57
lines changed

Zend/Zend.m4

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ AX_CHECK_COMPILE_FLAG([-fno-common],
211211
[CFLAGS="-fno-common $CFLAGS"])
212212
213213
ZEND_CHECK_ALIGNMENT
214+
ZEND_CHECK_INT64
214215
ZEND_CHECK_SIGNALS
215216
ZEND_CHECK_MAX_EXECUTION_TIMERS
216217
])
@@ -414,6 +415,43 @@ AS_VAR_IF([php_cv_align_mm], [failed],
414415
])
415416
])
416417

418+
dnl
419+
dnl ZEND_CHECK_INT64
420+
dnl
421+
dnl Check whether to enable 64 bit integer if supported by the system.
422+
dnl
423+
AC_DEFUN([ZEND_CHECK_INT64], [dnl
424+
AC_COMPILE_IFELSE(
425+
[AC_LANG_PROGRAM(
426+
[[]],
427+
[[
428+
#if !(defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64))
429+
#error "Not a 64-bit platform"
430+
#endif
431+
]]
432+
)],
433+
[ZEND_INT64=yes],
434+
[ZEND_INT64=no])
435+
436+
AC_ARG_ENABLE([zend-int64],
437+
[AS_HELP_STRING([--enable-zend-int64], [Enable 64bit integer support (enabled by default on 64bit arch)])],
438+
[ZEND_INT64=$enableval],
439+
[ZEND_INT64=$ZEND_INT64])
440+
441+
AS_VAR_IF([ZEND_INT64], [yes],
442+
AC_CHECK_TYPE([int64_t],,
443+
[AC_MSG_ERROR([int64_t not found])],
444+
[#include <stdint.h>]))
445+
446+
AS_VAR_IF([ZEND_INT64], [yes],
447+
[AC_DEFINE([ZEND_INT64], [1],
448+
[Define to 1 if zend_long as int64 is supported and enabled.])
449+
AS_VAR_APPEND([CFLAGS], [" -DZEND_INT64"])])
450+
451+
AC_MSG_CHECKING([whether to enable 64 bit integer support])
452+
AC_MSG_RESULT([$ZEND_INT64])
453+
])
454+
417455
dnl
418456
dnl ZEND_CHECK_SIGNALS
419457
dnl

Zend/zend_alloc.c

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ static size_t _real_page_size = ZEND_MM_PAGE_SIZE;
174174
#endif
175175

176176
typedef uint32_t zend_mm_page_info; /* 4-byte integer */
177-
typedef zend_ulong zend_mm_bitset; /* 4-byte or 8-byte integer */
177+
typedef size_t zend_mm_bitset; /* 4-byte or 8-byte integer */
178178

179179
#define ZEND_MM_ALIGNED_OFFSET(size, alignment) \
180180
(((size_t)(size)) & ((alignment) - 1))
@@ -571,7 +571,7 @@ static void *zend_mm_mmap(size_t size)
571571
/* number of trailing set (1) bits */
572572
ZEND_ATTRIBUTE_CONST static zend_always_inline int zend_mm_bitset_nts(zend_mm_bitset bitset)
573573
{
574-
#if (defined(__GNUC__) || __has_builtin(__builtin_ctzl)) && SIZEOF_ZEND_LONG == SIZEOF_LONG && defined(PHP_HAVE_BUILTIN_CTZL)
574+
#if (defined(__GNUC__) || __has_builtin(__builtin_ctzl)) && SIZEOF_SIZE_T == SIZEOF_LONG && defined(PHP_HAVE_BUILTIN_CTZL)
575575
return __builtin_ctzl(~bitset);
576576
#elif (defined(__GNUC__) || __has_builtin(__builtin_ctzll)) && defined(PHP_HAVE_BUILTIN_CTZLL)
577577
return __builtin_ctzll(~bitset);
@@ -594,7 +594,7 @@ ZEND_ATTRIBUTE_CONST static zend_always_inline int zend_mm_bitset_nts(zend_mm_bi
594594
if (bitset == (zend_mm_bitset)-1) return ZEND_MM_BITSET_LEN;
595595

596596
n = 0;
597-
#if SIZEOF_ZEND_LONG == 8
597+
#if SIZEOF_SIZE_T == 8
598598
if (sizeof(zend_mm_bitset) == 8) {
599599
if ((bitset & 0xffffffff) == 0xffffffff) {n += 32; bitset = bitset >> Z_UL(32);}
600600
}
@@ -614,12 +614,12 @@ static zend_always_inline int zend_mm_bitset_is_set(zend_mm_bitset *bitset, int
614614

615615
static zend_always_inline void zend_mm_bitset_set_bit(zend_mm_bitset *bitset, int bit)
616616
{
617-
bitset[bit / ZEND_MM_BITSET_LEN] |= (Z_UL(1) << (bit & (ZEND_MM_BITSET_LEN-1)));
617+
bitset[bit / ZEND_MM_BITSET_LEN] |= ((zend_mm_bitset)1 << (bit & (ZEND_MM_BITSET_LEN-1)));
618618
}
619619

620620
static zend_always_inline void zend_mm_bitset_reset_bit(zend_mm_bitset *bitset, int bit)
621621
{
622-
bitset[bit / ZEND_MM_BITSET_LEN] &= ~(Z_UL(1) << (bit & (ZEND_MM_BITSET_LEN-1)));
622+
bitset[bit / ZEND_MM_BITSET_LEN] &= ~((zend_mm_bitset)1 << (bit & (ZEND_MM_BITSET_LEN-1)));
623623
}
624624

625625
static zend_always_inline void zend_mm_bitset_set_range(zend_mm_bitset *bitset, int start, int len)
@@ -2082,7 +2082,7 @@ static zend_mm_heap *zend_mm_init(void)
20822082
#endif
20832083
zend_mm_init_key(heap);
20842084
#if ZEND_MM_LIMIT
2085-
heap->limit = (size_t)Z_L(-1) >> 1;
2085+
heap->limit = (size_t)-1 >> 1;
20862086
heap->overflow = 0;
20872087
#endif
20882088
#if ZEND_MM_CUSTOM
@@ -2334,7 +2334,7 @@ static void zend_mm_check_leaks(zend_mm_heap *heap)
23342334
repeated = zend_mm_find_leaks_huge(heap, list);
23352335
total += 1 + repeated;
23362336
if (repeated) {
2337-
zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(uintptr_t)repeated);
2337+
zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, ZEND_ULONG2PTR(repeated));
23382338
}
23392339

23402340
heap->huge_list = list = list->next;
@@ -2373,7 +2373,7 @@ static void zend_mm_check_leaks(zend_mm_heap *heap)
23732373
zend_mm_find_leaks(heap, p, i + bin_pages[bin_num], &leak);
23742374
total += 1 + repeated;
23752375
if (repeated) {
2376-
zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(uintptr_t)repeated);
2376+
zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, ZEND_ULONG2PTR(repeated));
23772377
}
23782378
}
23792379
dbg = (zend_mm_debug_info*)((char*)dbg + bin_data_size[bin_num]);
@@ -2399,7 +2399,7 @@ static void zend_mm_check_leaks(zend_mm_heap *heap)
23992399
repeated = zend_mm_find_leaks(heap, p, i + pages_count, &leak);
24002400
total += 1 + repeated;
24012401
if (repeated) {
2402-
zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, (void *)(uintptr_t)repeated);
2402+
zend_message_dispatcher(ZMSG_MEMORY_LEAK_REPEATED, ZEND_ULONG2PTR(repeated));
24032403
}
24042404
i += pages_count;
24052405
}
@@ -2981,14 +2981,14 @@ static ZEND_COLD ZEND_NORETURN void zend_out_of_memory(void)
29812981
#if ZEND_MM_CUSTOM
29822982
static zend_always_inline void tracked_add(zend_mm_heap *heap, void *ptr, size_t size) {
29832983
zval size_zv;
2984-
zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2;
2985-
ZEND_ASSERT((void *) (uintptr_t) (h << ZEND_MM_ALIGNMENT_LOG2) == ptr);
2984+
zend_ulong h = ZEND_PTR2ULONG(ptr) >> ZEND_MM_ALIGNMENT_LOG2;
2985+
ZEND_ASSERT(ZEND_ULONG2PTR(h << ZEND_MM_ALIGNMENT_LOG2) == ptr);
29862986
ZVAL_LONG(&size_zv, size);
29872987
zend_hash_index_add_new(heap->tracked_allocs, h, &size_zv);
29882988
}
29892989

29902990
static zend_always_inline zval *tracked_get_size_zv(zend_mm_heap *heap, void *ptr) {
2991-
zend_ulong h = ((uintptr_t) ptr) >> ZEND_MM_ALIGNMENT_LOG2;
2991+
zend_ulong h = ZEND_PTR2ULONG(ptr) >> ZEND_MM_ALIGNMENT_LOG2;
29922992
zval *size_zv = zend_hash_index_find(heap->tracked_allocs, h);
29932993
ZEND_ASSERT(size_zv && "Trying to free pointer not allocated through ZendMM");
29942994
return size_zv;
@@ -3074,7 +3074,7 @@ static void tracked_free_all(zend_mm_heap *heap) {
30743074
HashTable *tracked_allocs = heap->tracked_allocs;
30753075
zend_ulong h;
30763076
ZEND_HASH_FOREACH_NUM_KEY(tracked_allocs, h) {
3077-
void *ptr = (void *) (uintptr_t) (h << ZEND_MM_ALIGNMENT_LOG2);
3077+
void *ptr = ZEND_ULONG2PTR(h << ZEND_MM_ALIGNMENT_LOG2);
30783078
free(ptr);
30793079
} ZEND_HASH_FOREACH_END();
30803080
}
@@ -3285,7 +3285,7 @@ static void alloc_globals_ctor(zend_alloc_globals *alloc_globals)
32853285
zend_mm_heap *mm_heap = alloc_globals->mm_heap = malloc(sizeof(zend_mm_heap));
32863286
memset(mm_heap, 0, sizeof(zend_mm_heap));
32873287
mm_heap->use_custom_heap = ZEND_MM_CUSTOM_HEAP_STD;
3288-
mm_heap->limit = (size_t)Z_L(-1) >> 1;
3288+
mm_heap->limit = (size_t)-1 >> 1;
32893289
mm_heap->overflow = 0;
32903290

32913291
if (!tracked) {
@@ -3506,7 +3506,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
35063506
#endif
35073507
zend_mm_init_key(heap);
35083508
#if ZEND_MM_LIMIT
3509-
heap->limit = (size_t)Z_L(-1) >> 1;
3509+
heap->limit = (size_t)-1 >> 1;
35103510
heap->overflow = 0;
35113511
#endif
35123512
#if ZEND_MM_CUSTOM

Zend/zend_compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12360,7 +12360,7 @@ static void zend_eval_const_expr(zend_ast **ast_ptr) /* {{{ */
1236012360
} else if (Z_TYPE_P(dim) != IS_STRING || is_numeric_string(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset, NULL, 1) != IS_LONG) {
1236112361
return;
1236212362
}
12363-
if (offset < 0 || (size_t)offset >= Z_STRLEN_P(container)) {
12363+
if (offset < 0 || ZEND_SIZE_T_LTE_ZEND_LONG(Z_STRLEN_P(container), offset)) {
1236412364
return;
1236512365
}
1236612366
c = (uint8_t) Z_STRVAL_P(container)[offset];

Zend/zend_execute.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2157,11 +2157,11 @@ static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim,
21572157
}
21582158
}
21592159

2160-
if ((size_t)offset >= ZSTR_LEN(s)) {
2160+
if (ZEND_SIZE_T_LTE_ZEND_LONG(ZSTR_LEN(s), offset)) {
21612161
/* Extend string if needed */
2162-
zend_long old_len = ZSTR_LEN(s);
2162+
size_t old_len = ZSTR_LEN(s);
21632163
ZVAL_NEW_STR(str, zend_string_extend(s, (size_t)offset + 1, 0));
2164-
memset(Z_STRVAL_P(str) + old_len, ' ', offset - old_len);
2164+
memset(Z_STRVAL_P(str) + old_len, ' ', (size_t)offset - old_len);
21652165
Z_STRVAL_P(str)[offset+1] = 0;
21662166
} else {
21672167
zend_string_forget_hash_val(Z_STR_P(str));
@@ -3148,7 +3148,7 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
31483148
}
31493149
out:
31503150

3151-
if (UNEXPECTED(ZSTR_LEN(str) < ((offset < 0) ? -(size_t)offset : ((size_t)offset + 1)))) {
3151+
if (UNEXPECTED(ZEND_SIZE_T_LT_ZEND_ULONG(ZSTR_LEN(str), ((offset < 0) ? -(zend_ulong)offset : ((zend_ulong)offset + 1))))) {
31523152
if (type != BP_VAR_IS) {
31533153
zend_error(E_WARNING, "Uninitialized string offset " ZEND_LONG_FMT, offset);
31543154
ZVAL_EMPTY_STRING(result);
@@ -3309,7 +3309,7 @@ static zend_never_inline bool ZEND_FASTCALL zend_isset_dim_slow(zval *container,
33093309
if (UNEXPECTED(lval < 0)) { /* Handle negative offset */
33103310
lval += (zend_long)Z_STRLEN_P(container);
33113311
}
3312-
if (EXPECTED(lval >= 0) && (size_t)lval < Z_STRLEN_P(container)) {
3312+
if (EXPECTED(lval >= 0) && ZEND_SIZE_T_GT_ZEND_LONG(Z_STRLEN_P(container), lval)) {
33133313
return 1;
33143314
} else {
33153315
return 0;
@@ -3348,7 +3348,7 @@ static zend_never_inline bool ZEND_FASTCALL zend_isempty_dim_slow(zval *containe
33483348
if (UNEXPECTED(lval < 0)) { /* Handle negative offset */
33493349
lval += (zend_long)Z_STRLEN_P(container);
33503350
}
3351-
if (EXPECTED(lval >= 0) && (size_t)lval < Z_STRLEN_P(container)) {
3351+
if (EXPECTED(lval >= 0) && ZEND_SIZE_T_GT_ZEND_LONG(Z_STRLEN_P(container), lval)) {
33523352
return (Z_STRVAL_P(container)[lval] == '0');
33533353
} else {
33543354
return 1;

Zend/zend_execute_API.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string *
12151215
ALLOC_HASHTABLE(CG(unlinked_uses));
12161216
zend_hash_init(CG(unlinked_uses), 0, NULL, NULL, 0);
12171217
}
1218-
zend_hash_index_add_empty_element(CG(unlinked_uses), (zend_long)(uintptr_t)ce);
1218+
zend_hash_index_add_empty_element(CG(unlinked_uses), ZEND_PTR2ULONG(ce));
12191219
return ce;
12201220
}
12211221
return NULL;

Zend/zend_generators.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static void zend_generator_remove_child(zend_generator_node *node, zend_generato
179179
node->child.single = NULL;
180180
} else {
181181
HashTable *ht = node->child.ht;
182-
zend_hash_index_del(ht, (zend_ulong) child);
182+
zend_hash_index_del(ht, ZEND_PTR2ULONG(child));
183183
if (node->children == 2) {
184184
zend_generator *other_child;
185185
ZEND_HASH_FOREACH_PTR(ht, other_child) {
@@ -552,11 +552,11 @@ static void zend_generator_add_child(zend_generator *generator, zend_generator *
552552
HashTable *ht = emalloc(sizeof(HashTable));
553553
zend_hash_init(ht, 0, NULL, NULL, 0);
554554
zend_hash_index_add_new_ptr(ht,
555-
(zend_ulong) node->child.single, node->child.single);
555+
ZEND_PTR2ULONG(node->child.single), node->child.single);
556556
node->child.ht = ht;
557557
}
558558

559-
zend_hash_index_add_new_ptr(node->child.ht, (zend_ulong) child, child);
559+
zend_hash_index_add_new_ptr(node->child.ht, ZEND_PTR2ULONG(child), child);
560560
}
561561

562562
++node->children;

Zend/zend_inheritance.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3316,7 +3316,7 @@ static void check_unrecoverable_load_failure(const zend_class_entry *ce) {
33163316
* a dependence on the inheritance hierarchy of this specific class. Instead we fall back to
33173317
* a fatal error, as would happen if we did not allow exceptions in the first place. */
33183318
if (CG(unlinked_uses)
3319-
&& zend_hash_index_del(CG(unlinked_uses), (zend_long)(uintptr_t)ce) == SUCCESS) {
3319+
&& zend_hash_index_del(CG(unlinked_uses), ZEND_PTR2ULONG(ce)) == SUCCESS) {
33203320
zend_exception_uncaught_error(
33213321
"During inheritance of %s with variance dependencies", ZSTR_VAL(ce->name));
33223322
}
@@ -3611,7 +3611,7 @@ ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string
36113611
}
36123612

36133613
if (CG(unlinked_uses)) {
3614-
zend_hash_index_del(CG(unlinked_uses), (zend_long)(uintptr_t) ce);
3614+
zend_hash_index_del(CG(unlinked_uses), ZEND_PTR2ULONG(ce));
36153615
}
36163616

36173617
orig_linking_class = CG(current_linking_class);

Zend/zend_long.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include <stdint.h>
2424

2525
/* This is the heart of the whole int64 enablement in zval. */
26-
#if defined(__x86_64__) || defined(__LP64__) || defined(_LP64) || defined(_WIN64)
26+
#ifdef ZEND_INT64
2727
# define ZEND_ENABLE_ZVAL_LONG64 1
2828
#endif
2929

@@ -38,6 +38,12 @@ typedef int64_t zend_off_t;
3838
# define Z_L(i) INT64_C(i)
3939
# define Z_UL(i) UINT64_C(i)
4040
# define SIZEOF_ZEND_LONG 8
41+
# define ZEND_PTR2ULONG(ptr) ((zend_ulong)(uintptr_t)(ptr))
42+
# define ZEND_ULONG2PTR(i) \
43+
( (UNEXPECTED((i) > (zend_ulong)UINTPTR_MAX)) \
44+
? (assert(!"zend_ulong out of pointer range"), NULL) \
45+
: (void *)(uintptr_t)(i) \
46+
)
4147
#else
4248
typedef int32_t zend_long;
4349
typedef uint32_t zend_ulong;
@@ -48,6 +54,8 @@ typedef int32_t zend_off_t;
4854
# define Z_L(i) INT32_C(i)
4955
# define Z_UL(i) UINT32_C(i)
5056
# define SIZEOF_ZEND_LONG 4
57+
# define ZEND_PTR2ULONG(ptr) ((zend_ulong)(uintptr_t)(ptr))
58+
# define ZEND_ULONG2PTR(i) ((void *)(uintptr_t)(i))
5159
#endif
5260

5361

Zend/zend_object_handlers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ static zend_always_inline uintptr_t zend_get_property_offset(zend_class_entry *c
454454
*info_ptr = property_info;
455455
}
456456
if (cache_slot) {
457-
CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*)(uintptr_t)offset);
457+
CACHE_POLYMORPHIC_PTR_EX(cache_slot, ce, (void*) offset);
458458
CACHE_PTR_EX(cache_slot + 2, property_info);
459459
}
460460
return offset;

Zend/zend_operators.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,11 @@ ZEND_API void zend_reset_lc_ctype_locale(void);
528528
#define ZVAL_OFFSETOF_TYPE \
529529
(offsetof(zval, u1.type_info) - offsetof(zval, value))
530530

531-
#if defined(HAVE_ASM_GOTO) && !__has_feature(memory_sanitizer)
532-
# define ZEND_USE_ASM_ARITHMETIC 1
533-
#else
531+
//#if defined(HAVE_ASM_GOTO) && !__has_feature(memory_sanitizer)
532+
//# define ZEND_USE_ASM_ARITHMETIC 1
533+
//#else
534534
# define ZEND_USE_ASM_ARITHMETIC 0
535-
#endif
535+
//#endif
536536

537537
static zend_always_inline void fast_long_increment_function(zval *op1)
538538
{

0 commit comments

Comments
 (0)