From bb3d4456ee632beb5c783ed26ffaf7d7d460b489 Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Mon, 15 Jun 2020 14:26:22 +0300 Subject: [PATCH] Change GC_COLLECTABLE flag into GC_NOT_COLLECTABLE to simplify GC_MAY_LEAK() check --- Zend/zend_ast.c | 2 +- Zend/zend_gc.c | 4 ++-- Zend/zend_gc.h | 5 ++--- Zend/zend_hash.c | 6 +++--- Zend/zend_objects.c | 2 +- Zend/zend_string.h | 6 +++--- Zend/zend_types.h | 23 ++++++++++++++--------- Zend/zend_vm_def.h | 2 +- Zend/zend_vm_execute.h | 2 +- ext/ffi/ffi.c | 2 +- ext/opcache/ZendAccelerator.c | 2 +- ext/opcache/jit/zend_jit_x86.dasc | 4 +--- ext/opcache/zend_file_cache.c | 2 +- ext/opcache/zend_persist.c | 8 ++++---- 14 files changed, 36 insertions(+), 34 deletions(-) diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index b55f06604e..6a1c2d692f 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -846,7 +846,7 @@ ZEND_API zend_ast_ref * ZEND_FASTCALL zend_ast_copy(zend_ast *ast) ref = emalloc(tree_size); zend_ast_tree_copy(ast, GC_AST(ref)); GC_SET_REFCOUNT(ref, 1); - GC_TYPE_INFO(ref) = IS_CONSTANT_AST; + GC_TYPE_INFO(ref) = GC_CONSTANT_AST; return ref; } diff --git a/Zend/zend_gc.c b/Zend/zend_gc.c index 5dd11f1597..aa9602ff04 100644 --- a/Zend/zend_gc.c +++ b/Zend/zend_gc.c @@ -1574,7 +1574,7 @@ ZEND_API int zend_gc_collect_cycles(void) zend_object *obj = (zend_object*)p; EG(objects_store).object_buckets[obj->handle] = SET_OBJ_INVALID(obj); - GC_TYPE_INFO(obj) = IS_NULL | + GC_TYPE_INFO(obj) = GC_NULL | (GC_TYPE_INFO(obj) & ~GC_TYPE_MASK); /* Modify current before calling free_obj (bug #78811: free_obj() can cause the root buffer (with current) to be reallocated.) */ current->ref = GC_MAKE_GARBAGE(((char*)obj) - obj->handlers->offset); @@ -1589,7 +1589,7 @@ ZEND_API int zend_gc_collect_cycles(void) } else if (GC_TYPE(p) == IS_ARRAY) { zend_array *arr = (zend_array*)p; - GC_TYPE_INFO(arr) = IS_NULL | + GC_TYPE_INFO(arr) = GC_NULL | (GC_TYPE_INFO(arr) & ~GC_TYPE_MASK); /* GC may destroy arrays with rc>1. This is valid and safe. */ diff --git a/Zend/zend_gc.h b/Zend/zend_gc.h index 8fc0073181..b334173763 100644 --- a/Zend/zend_gc.h +++ b/Zend/zend_gc.h @@ -66,12 +66,11 @@ END_EXTERN_C() #define GC_MAY_LEAK(ref) \ ((GC_TYPE_INFO(ref) & \ - (GC_INFO_MASK | (GC_COLLECTABLE << GC_FLAGS_SHIFT))) == \ - (GC_COLLECTABLE << GC_FLAGS_SHIFT)) + (GC_INFO_MASK | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))) == 0) static zend_always_inline void gc_check_possible_root(zend_refcounted *ref) { - if (EXPECTED(GC_TYPE_INFO(ref) == IS_REFERENCE)) { + if (EXPECTED(GC_TYPE_INFO(ref) == GC_REFERENCE)) { zval *zv = &((zend_reference*)ref)->val; if (!Z_COLLECTABLE_P(zv)) { diff --git a/Zend/zend_hash.c b/Zend/zend_hash.c index ae67adcf0a..37e46cd8bc 100644 --- a/Zend/zend_hash.c +++ b/Zend/zend_hash.c @@ -249,7 +249,7 @@ ZEND_API const HashTable zend_empty_array = { static zend_always_inline void _zend_hash_init_int(HashTable *ht, uint32_t nSize, dtor_func_t pDestructor, zend_bool persistent) { GC_SET_REFCOUNT(ht, 1); - GC_TYPE_INFO(ht) = IS_ARRAY | (persistent ? (GC_PERSISTENT << GC_FLAGS_SHIFT) : (GC_COLLECTABLE << GC_FLAGS_SHIFT)); + GC_TYPE_INFO(ht) = GC_ARRAY | (persistent ? ((GC_PERSISTENT|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT) : 0); HT_FLAGS(ht) = HASH_FLAG_UNINITIALIZED; ht->nTableMask = HT_MIN_MASK; HT_SET_DATA_ADDR(ht, &uninitialized_bucket); @@ -1618,7 +1618,7 @@ ZEND_API void ZEND_FASTCALL zend_array_destroy(HashTable *ht) /* break possible cycles */ GC_REMOVE_FROM_BUFFER(ht); - GC_TYPE_INFO(ht) = IS_NULL /*???| (GC_WHITE << 16)*/; + GC_TYPE_INFO(ht) = GC_NULL /*???| (GC_WHITE << 16)*/; if (ht->nNumUsed) { /* In some rare cases destructors of regular arrays may be changed */ @@ -2071,7 +2071,7 @@ ZEND_API HashTable* ZEND_FASTCALL zend_array_dup(HashTable *source) ALLOC_HASHTABLE(target); GC_SET_REFCOUNT(target, 1); - GC_TYPE_INFO(target) = IS_ARRAY | (GC_COLLECTABLE << GC_FLAGS_SHIFT); + GC_TYPE_INFO(target) = GC_ARRAY; target->pDestructor = ZVAL_PTR_DTOR; diff --git a/Zend/zend_objects.c b/Zend/zend_objects.c index 0e873f0681..14ca14a4b1 100644 --- a/Zend/zend_objects.c +++ b/Zend/zend_objects.c @@ -29,7 +29,7 @@ static zend_always_inline void _zend_object_std_init(zend_object *object, zend_class_entry *ce) { GC_SET_REFCOUNT(object, 1); - GC_TYPE_INFO(object) = IS_OBJECT | (GC_COLLECTABLE << GC_FLAGS_SHIFT); + GC_TYPE_INFO(object) = GC_OBJECT; object->ce = ce; object->properties = NULL; zend_objects_store_put(object); diff --git a/Zend/zend_string.h b/Zend/zend_string.h index eb5ab09a6e..98bd735976 100644 --- a/Zend/zend_string.h +++ b/Zend/zend_string.h @@ -86,7 +86,7 @@ END_EXTERN_C() #define ZSTR_ALLOCA_ALLOC(str, _len, use_heap) do { \ (str) = (zend_string *)do_alloca(ZEND_MM_ALIGNED_SIZE_EX(_ZSTR_STRUCT_SIZE(_len), 8), (use_heap)); \ GC_SET_REFCOUNT(str, 1); \ - GC_TYPE_INFO(str) = IS_STRING; \ + GC_TYPE_INFO(str) = GC_STRING; \ ZSTR_H(str) = 0; \ ZSTR_LEN(str) = _len; \ } while (0) @@ -141,7 +141,7 @@ static zend_always_inline zend_string *zend_string_alloc(size_t len, int persist zend_string *ret = (zend_string *)pemalloc(ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(len)), persistent); GC_SET_REFCOUNT(ret, 1); - GC_TYPE_INFO(ret) = IS_STRING | ((persistent ? IS_STR_PERSISTENT : 0) << GC_FLAGS_SHIFT); + GC_TYPE_INFO(ret) = GC_STRING | ((persistent ? IS_STR_PERSISTENT : 0) << GC_FLAGS_SHIFT); ZSTR_H(ret) = 0; ZSTR_LEN(ret) = len; return ret; @@ -152,7 +152,7 @@ static zend_always_inline zend_string *zend_string_safe_alloc(size_t n, size_t m zend_string *ret = (zend_string *)safe_pemalloc(n, m, ZEND_MM_ALIGNED_SIZE(_ZSTR_STRUCT_SIZE(l)), persistent); GC_SET_REFCOUNT(ret, 1); - GC_TYPE_INFO(ret) = IS_STRING | ((persistent ? IS_STR_PERSISTENT : 0) << GC_FLAGS_SHIFT); + GC_TYPE_INFO(ret) = GC_STRING | ((persistent ? IS_STR_PERSISTENT : 0) << GC_FLAGS_SHIFT); ZSTR_H(ret) = 0; ZSTR_LEN(ret) = (n * m) + l; return ret; diff --git a/Zend/zend_types.h b/Zend/zend_types.h index 5299748873..10a751d1b4 100644 --- a/Zend/zend_types.h +++ b/Zend/zend_types.h @@ -653,14 +653,19 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) { #define Z_GC_TYPE_INFO_P(zval_p) Z_GC_TYPE_INFO(*(zval_p)) /* zval_gc_flags(zval.value->gc.u.type_info) (common flags) */ -#define GC_COLLECTABLE (1<<4) +#define GC_NOT_COLLECTABLE (1<<4) #define GC_PROTECTED (1<<5) /* used for recursion detection */ #define GC_IMMUTABLE (1<<6) /* can't be changed in place */ #define GC_PERSISTENT (1<<7) /* allocated using malloc */ #define GC_PERSISTENT_LOCAL (1<<8) /* persistent, but thread-local */ -#define GC_ARRAY (IS_ARRAY | (GC_COLLECTABLE << GC_FLAGS_SHIFT)) -#define GC_OBJECT (IS_OBJECT | (GC_COLLECTABLE << GC_FLAGS_SHIFT)) +#define GC_NULL (IS_NULL | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) +#define GC_STRING (IS_STRING | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) +#define GC_ARRAY IS_ARRAY +#define GC_OBJECT IS_OBJECT +#define GC_RESOURCE (IS_RESOURCE | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) +#define GC_REFERENCE (IS_REFERENCE | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) +#define GC_CONSTANT_AST (IS_CONSTANT_AST | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) /* zval.u1.v.type_flags */ #define IS_TYPE_REFCOUNTED (1<<0) @@ -974,7 +979,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) { (zend_resource *) emalloc(sizeof(zend_resource)); \ zval *__z; \ GC_SET_REFCOUNT(_res, 1); \ - GC_TYPE_INFO(_res) = IS_RESOURCE; \ + GC_TYPE_INFO(_res) = GC_RESOURCE; \ _res->handle = (h); \ _res->type = (t); \ _res->ptr = (p); \ @@ -988,7 +993,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) { (zend_resource *) malloc(sizeof(zend_resource)); \ zval *__z; \ GC_SET_REFCOUNT(_res, 1); \ - GC_TYPE_INFO(_res) = IS_RESOURCE | \ + GC_TYPE_INFO(_res) = GC_RESOURCE | \ (GC_PERSISTENT << GC_FLAGS_SHIFT); \ _res->handle = (h); \ _res->type = (t); \ @@ -1008,7 +1013,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) { zend_reference *_ref = \ (zend_reference *) emalloc(sizeof(zend_reference)); \ GC_SET_REFCOUNT(_ref, 1); \ - GC_TYPE_INFO(_ref) = IS_REFERENCE; \ + GC_TYPE_INFO(_ref) = GC_REFERENCE; \ _ref->sources.ptr = NULL; \ Z_REF_P(z) = _ref; \ Z_TYPE_INFO_P(z) = IS_REFERENCE_EX; \ @@ -1018,7 +1023,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) { zend_reference *_ref = \ (zend_reference *) emalloc(sizeof(zend_reference)); \ GC_SET_REFCOUNT(_ref, 1); \ - GC_TYPE_INFO(_ref) = IS_REFERENCE; \ + GC_TYPE_INFO(_ref) = GC_REFERENCE; \ ZVAL_COPY_VALUE(&_ref->val, r); \ _ref->sources.ptr = NULL; \ Z_REF_P(z) = _ref; \ @@ -1030,7 +1035,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) { zend_reference *_ref = \ (zend_reference *) emalloc(sizeof(zend_reference)); \ GC_SET_REFCOUNT(_ref, (refcount)); \ - GC_TYPE_INFO(_ref) = IS_REFERENCE; \ + GC_TYPE_INFO(_ref) = GC_REFERENCE; \ ZVAL_COPY_VALUE(&_ref->val, _z); \ _ref->sources.ptr = NULL; \ Z_REF_P(_z) = _ref; \ @@ -1041,7 +1046,7 @@ static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) { zend_reference *_ref = \ (zend_reference *) malloc(sizeof(zend_reference)); \ GC_SET_REFCOUNT(_ref, 1); \ - GC_TYPE_INFO(_ref) = IS_REFERENCE | \ + GC_TYPE_INFO(_ref) = GC_REFERENCE | \ (GC_PERSISTENT << GC_FLAGS_SHIFT); \ ZVAL_COPY_VALUE(&_ref->val, r); \ _ref->sources.ptr = NULL; \ diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index b8b0dc5e3a..768101f9b3 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -8250,7 +8250,7 @@ ZEND_VM_HANDLER(183, ZEND_BIND_STATIC, CV, UNUSED, REF) if (UNEXPECTED(!Z_ISREF_P(value))) { zend_reference *ref = (zend_reference*)emalloc(sizeof(zend_reference)); GC_SET_REFCOUNT(ref, 2); - GC_TYPE_INFO(ref) = IS_REFERENCE; + GC_TYPE_INFO(ref) = GC_REFERENCE; ZVAL_COPY_VALUE(&ref->val, value); ref->sources.ptr = NULL; Z_REF_P(value) = ref; diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 16779b6616..a6f2ed827e 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -45113,7 +45113,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_BIND_STATIC_SPEC_CV_UNUSED_HAN if (UNEXPECTED(!Z_ISREF_P(value))) { zend_reference *ref = (zend_reference*)emalloc(sizeof(zend_reference)); GC_SET_REFCOUNT(ref, 2); - GC_TYPE_INFO(ref) = IS_REFERENCE; + GC_TYPE_INFO(ref) = GC_REFERENCE; ZVAL_COPY_VALUE(&ref->val, value); ref->sources.ptr = NULL; Z_REF_P(value) = ref; diff --git a/ext/ffi/ffi.c b/ext/ffi/ffi.c index 59d4738d38..4de14fe738 100644 --- a/ext/ffi/ffi.c +++ b/ext/ffi/ffi.c @@ -231,7 +231,7 @@ static zend_always_inline void zend_ffi_type_dtor(zend_ffi_type *type) /* {{{ */ static zend_always_inline void zend_ffi_object_init(zend_object *object, zend_class_entry *ce) /* {{{ */ { GC_SET_REFCOUNT(object, 1); - GC_TYPE_INFO(object) = IS_OBJECT | ((GC_COLLECTABLE | IS_OBJ_DESTRUCTOR_CALLED) << GC_FLAGS_SHIFT); + GC_TYPE_INFO(object) = GC_OBJECT | (IS_OBJ_DESTRUCTOR_CALLED << GC_FLAGS_SHIFT); object->ce = ce; object->properties = NULL; zend_objects_store_put(object); diff --git a/ext/opcache/ZendAccelerator.c b/ext/opcache/ZendAccelerator.c index aa81410c82..ae4d315188 100644 --- a/ext/opcache/ZendAccelerator.c +++ b/ext/opcache/ZendAccelerator.c @@ -507,7 +507,7 @@ zend_string* ZEND_FASTCALL accel_new_interned_string(zend_string *str) STRTAB_COLLISION(s) = *hash_slot; *hash_slot = STRTAB_STR_TO_POS(&ZCSG(interned_strings), s); GC_SET_REFCOUNT(s, 1); - GC_TYPE_INFO(s) = IS_STRING | ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT); + GC_TYPE_INFO(s) = GC_STRING | ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT); ZSTR_H(s) = h; ZSTR_LEN(s) = ZSTR_LEN(str); memcpy(ZSTR_VAL(s), ZSTR_VAL(str), ZSTR_LEN(s) + 1); diff --git a/ext/opcache/jit/zend_jit_x86.dasc b/ext/opcache/jit/zend_jit_x86.dasc index 475265bf7d..97d4fbe77a 100644 --- a/ext/opcache/jit/zend_jit_x86.dasc +++ b/ext/opcache/jit/zend_jit_x86.dasc @@ -1253,9 +1253,7 @@ static void* dasm_labels[zend_lb_MAX]; |.endmacro |.macro IF_GC_MAY_NOT_LEAK, ptr, tmp_reg, label -| mov tmp_reg, dword [ptr + 4] -| and tmp_reg, (GC_INFO_MASK | (GC_COLLECTABLE << GC_FLAGS_SHIFT)) -| cmp tmp_reg, (GC_COLLECTABLE << GC_FLAGS_SHIFT) +| test dword [ptr + 4],(GC_INFO_MASK | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT)) | jne label |.endmacro diff --git a/ext/opcache/zend_file_cache.c b/ext/opcache/zend_file_cache.c index 03271678c5..7bf36b7931 100644 --- a/ext/opcache/zend_file_cache.c +++ b/ext/opcache/zend_file_cache.c @@ -280,7 +280,7 @@ static void *zend_file_cache_unserialize_interned(zend_string *str, int in_shm) memcpy(ret, str, size); /* String wasn't interned but we will use it as interned anyway */ GC_SET_REFCOUNT(ret, 1); - GC_TYPE_INFO(ret) = IS_STRING | ((IS_STR_INTERNED | IS_STR_PERSISTENT | IS_STR_PERMANENT) << GC_FLAGS_SHIFT); + GC_TYPE_INFO(ret) = GC_STRING | ((IS_STR_INTERNED | IS_STR_PERSISTENT | IS_STR_PERMANENT) << GC_FLAGS_SHIFT); } } else { ret = str; diff --git a/ext/opcache/zend_persist.c b/ext/opcache/zend_persist.c index 11f26cc123..1ad195c689 100644 --- a/ext/opcache/zend_persist.c +++ b/ext/opcache/zend_persist.c @@ -37,9 +37,9 @@ #define zend_set_str_gc_flags(str) do { \ if (file_cache_only) { \ - GC_TYPE_INFO(str) = IS_STRING | (IS_STR_INTERNED << GC_FLAGS_SHIFT); \ + GC_TYPE_INFO(str) = GC_STRING | (IS_STR_INTERNED << GC_FLAGS_SHIFT); \ } else { \ - GC_TYPE_INFO(str) = IS_STRING | ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT); \ + GC_TYPE_INFO(str) = GC_STRING | ((IS_STR_INTERNED | IS_STR_PERMANENT) << GC_FLAGS_SHIFT); \ } \ } while (0) @@ -286,7 +286,7 @@ static HashTable *zend_persist_attributes(HashTable *attributes) ptr = zend_shared_memdup_put_free(attributes, sizeof(HashTable)); GC_SET_REFCOUNT(ptr, 2); - GC_TYPE_INFO(ptr) = IS_ARRAY | (IS_ARRAY_IMMUTABLE << GC_FLAGS_SHIFT); + GC_TYPE_INFO(ptr) = GC_ARRAY | ((IS_ARRAY_IMMUTABLE|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT); } return ptr; @@ -456,7 +456,7 @@ static void zend_persist_op_array_ex(zend_op_array *op_array, zend_persistent_sc op_array->static_variables = zend_shared_memdup_put_free(op_array->static_variables, sizeof(HashTable)); /* make immutable array */ GC_SET_REFCOUNT(op_array->static_variables, 2); - GC_TYPE_INFO(op_array->static_variables) = IS_ARRAY | (IS_ARRAY_IMMUTABLE << GC_FLAGS_SHIFT); + GC_TYPE_INFO(op_array->static_variables) = GC_ARRAY | ((IS_ARRAY_IMMUTABLE|GC_NOT_COLLECTABLE) << GC_FLAGS_SHIFT); } ZEND_MAP_PTR_INIT(op_array->static_variables_ptr, &op_array->static_variables); -- 2.40.0