From bf123da562c8bd71083138ba329bb044c3006c75 Mon Sep 17 00:00:00 2001 From: Tony Su Date: Tue, 4 Apr 2023 18:09:38 +0800 Subject: [PATCH] [Zend]: Fix unnecessary alignment in ZEND_CALL_FRAME_SLOT macro (#10988) Alignment is not necessary while calculating slots reserved for zend_execute_data and _zend_vm_stack. ZEND_STATIC_ASSERT ensures the correct alignment while code compilation. Credit is to Ilija Tovilo. PR: https://github.com/php/php-src/pull/10988 Signed-off-by: Tony Su Reviewed-by : Ilija Tovilo Reviewed-by : Dmitry Stogov Reviewed-by : Niels Dossche --- Zend/zend_compile.h | 6 +++++- Zend/zend_execute.h | 6 +++++- Zend/zend_portability.h | 6 ++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Zend/zend_compile.h b/Zend/zend_compile.h index 31ed95f6751..9a2129d908d 100644 --- a/Zend/zend_compile.h +++ b/Zend/zend_compile.h @@ -606,8 +606,12 @@ struct _zend_execute_data { #define ZEND_CALL_NUM_ARGS(call) \ (call)->This.u2.num_args +/* Ensure the correct alignment before slots calculation */ +ZEND_STATIC_ASSERT(ZEND_MM_ALIGNED_SIZE(sizeof(zval)) == sizeof(zval), + "zval must be aligned by ZEND_MM_ALIGNMENT"); +/* A number of call frame slots (zvals) reserved for zend_execute_data. */ #define ZEND_CALL_FRAME_SLOT \ - ((int)((ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / ZEND_MM_ALIGNED_SIZE(sizeof(zval)))) + ((int)((sizeof(zend_execute_data) + sizeof(zval) - 1) / sizeof(zval))) #define ZEND_CALL_VAR(call, n) \ ((zval*)(((char*)(call)) + ((int)(n)))) diff --git a/Zend/zend_execute.h b/Zend/zend_execute.h index 323b6269ee5..6e868d75a0d 100644 --- a/Zend/zend_execute.h +++ b/Zend/zend_execute.h @@ -195,8 +195,12 @@ struct _zend_vm_stack { zend_vm_stack prev; }; +/* Ensure the correct alignment before slots calculation */ +ZEND_STATIC_ASSERT(ZEND_MM_ALIGNED_SIZE(sizeof(zval)) == sizeof(zval), + "zval must be aligned by ZEND_MM_ALIGNMENT"); +/* A number of call frame slots (zvals) reserved for _zend_vm_stack. */ #define ZEND_VM_STACK_HEADER_SLOTS \ - ((ZEND_MM_ALIGNED_SIZE(sizeof(struct _zend_vm_stack)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / ZEND_MM_ALIGNED_SIZE(sizeof(zval))) + ((sizeof(struct _zend_vm_stack) + sizeof(zval) - 1) / sizeof(zval)) #define ZEND_VM_STACK_ELEMENTS(stack) \ (((zval*)(stack)) + ZEND_VM_STACK_HEADER_SLOTS) diff --git a/Zend/zend_portability.h b/Zend/zend_portability.h index 48e648ce4ba..c098d29d518 100644 --- a/Zend/zend_portability.h +++ b/Zend/zend_portability.h @@ -750,4 +750,10 @@ extern "C++" { # define ZEND_CGG_DIAGNOSTIC_IGNORED_END #endif +#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) /* C11 */ +# define ZEND_STATIC_ASSERT(c, m) _Static_assert((c), m) +#else +# define ZEND_STATIC_ASSERT(c, m) +#endif + #endif /* ZEND_PORTABILITY_H */