|
| 1 | +/* |
| 2 | + * The version-specific side of abi.h: the only translation unit reading the |
| 3 | + * engine globals by their compiled-in layout. |
| 4 | + */ |
| 5 | + |
| 6 | +#define PHPSTANTURBO_ABI_IMPL |
| 7 | +#include "support.h" |
| 8 | +#include "reg.h" |
| 9 | + |
| 10 | +#include "zend_closures.h" |
| 11 | +#include "zend_weakrefs.h" |
| 12 | + |
| 13 | +pt_abi_globals pt_abi; |
| 14 | + |
| 15 | +void pt_abi_init() |
| 16 | +{ |
| 17 | +#define PT_ABI_EG_FIELD(field) pt_abi.eg_##field = &EG(field); |
| 18 | +#define PT_ABI_CG_FIELD(field) pt_abi.cg_##field = &CG(field); |
| 19 | +#define PT_ABI_KNOWN_STRING(id) pt_abi.known_##id = ZSTR_KNOWN(id); |
| 20 | +#define PT_ABI_OBJECT_HANDLER(member) pt_abi.handler_offset_##member = offsetof(zend_object_handlers, member); |
| 21 | + PT_ABI_EG_FIELDS(PT_ABI_EG_FIELD) |
| 22 | + PT_ABI_CG_FIELDS(PT_ABI_CG_FIELD) |
| 23 | + PT_ABI_KNOWN_STRINGS(PT_ABI_KNOWN_STRING) |
| 24 | + PT_ABI_OBJECT_HANDLERS(PT_ABI_OBJECT_HANDLER) |
| 25 | +#undef PT_ABI_EG_FIELD |
| 26 | +#undef PT_ABI_CG_FIELD |
| 27 | +#undef PT_ABI_KNOWN_STRING |
| 28 | +#undef PT_ABI_OBJECT_HANDLER |
| 29 | + |
| 30 | + pt_abi.offset_internal_function_handler = offsetof(zend_internal_function, handler); |
| 31 | + pt_abi.offset_class_entry_info = offsetof(zend_class_entry, info); |
| 32 | + pt_abi.php_version_id = PHP_VERSION_ID; |
| 33 | +#if PHP_VERSION_ID >= 80400 |
| 34 | + static_assert(offsetof(zend_object, extra_flags) == offsetof(zend_object, handle) + sizeof(uint32_t), "pt_abi_object_is_lazy() reads extra_flags right after handle"); |
| 35 | + pt_abi.object_lazy_flags = IS_OBJ_LAZY_UNINITIALIZED | IS_OBJ_LAZY_PROXY; |
| 36 | +#else |
| 37 | + pt_abi.object_lazy_flags = 0; |
| 38 | +#endif |
| 39 | + pt_abi.is_reference_ex = IS_REFERENCE_EX; |
| 40 | + pt_abi.acc_use_guards = ZEND_ACC_USE_GUARDS; |
| 41 | + pt_abi.dval_to_lval = [](double d) -> zend_long { return zend_dval_to_lval(d); }; |
| 42 | +} |
| 43 | + |
| 44 | +const zend_object_handlers *pt_abi_object_handlers(const pt_abi_handlers &overrides) |
| 45 | +{ |
| 46 | + auto *handlers = (zend_object_handlers *) pemalloc(sizeof(zend_object_handlers), 1); |
| 47 | + memcpy(handlers, &std_object_handlers, sizeof(zend_object_handlers)); |
| 48 | + handlers->offset = overrides.offset; |
| 49 | + if (overrides.free_obj != NULL) handlers->free_obj = overrides.free_obj; |
| 50 | + if (overrides.get_gc != NULL) handlers->get_gc = overrides.get_gc; |
| 51 | + if (overrides.clone_obj != NULL) handlers->clone_obj = overrides.clone_obj; |
| 52 | + if (overrides.get_closure != NULL) handlers->get_closure = overrides.get_closure; |
| 53 | + if (overrides.compare != NULL) handlers->compare = overrides.compare; |
| 54 | + return handlers; |
| 55 | +} |
| 56 | + |
| 57 | +zend_function_entry *pt_abi_function_entries(const reg::FunctionEntry *entries, size_t count) |
| 58 | +{ |
| 59 | + zend_function_entry *functions = (zend_function_entry *) ecalloc(count + 1, sizeof(zend_function_entry)); |
| 60 | + for (size_t i = 0; i < count; i++) { |
| 61 | + functions[i].fname = entries[i].fname; |
| 62 | + functions[i].handler = entries[i].handler; |
| 63 | + functions[i].arg_info = entries[i].argInfo; |
| 64 | + functions[i].num_args = entries[i].numArgs; |
| 65 | + functions[i].flags = entries[i].flags; |
| 66 | + } |
| 67 | + return functions; |
| 68 | +} |
| 69 | + |
| 70 | +zend_class_entry *pt_abi_register_internal_class(const char *name, const reg::FunctionEntry *entries, size_t count) |
| 71 | +{ |
| 72 | + zend_function_entry *functions = pt_abi_function_entries(entries, count); |
| 73 | + zend_class_entry ce; |
| 74 | + INIT_CLASS_ENTRY_EX(ce, name, strlen(name), functions); |
| 75 | + zend_class_entry *registered = zend_register_internal_class(&ce); |
| 76 | + efree(functions); |
| 77 | + return registered; |
| 78 | +} |
| 79 | + |
| 80 | +void pt_abi_create_closure(zval *out, zend_function *fn, zend_class_entry *scope, zend_class_entry *calledScope, zend_object *thisObject) |
| 81 | +{ |
| 82 | +#if PHP_VERSION_ID >= 80600 |
| 83 | + /* php-src fbb2e1f23d6: $this is passed as zend_object* from 8.6 on */ |
| 84 | + zend_create_closure(out, fn, scope, calledScope, thisObject); |
| 85 | +#else |
| 86 | + zval thisZv; |
| 87 | + if (thisObject != NULL) { |
| 88 | + ZVAL_OBJ(&thisZv, thisObject); |
| 89 | + } |
| 90 | + zend_create_closure(out, fn, scope, calledScope, thisObject != NULL ? &thisZv : NULL); |
| 91 | +#endif |
| 92 | +} |
| 93 | + |
| 94 | +zend_object *pt_abi_closure_this(zval *closure) |
| 95 | +{ |
| 96 | +#if PHP_VERSION_ID >= 80600 |
| 97 | + /* PHP 8.6 holds the bound $this as a zend_object (NULL when unbound) */ |
| 98 | + return zend_get_closure_this_ptr(closure); |
| 99 | +#else |
| 100 | + zval *thisZv = zend_get_closure_this_ptr(closure); |
| 101 | + return thisZv != NULL && Z_TYPE_P(thisZv) == IS_OBJECT ? Z_OBJ_P(thisZv) : NULL; |
| 102 | +#endif |
| 103 | +} |
| 104 | + |
| 105 | +/* zend_weakrefs_hash_clean()/_destroy() only exist since PHP 8.5; before, |
| 106 | + * the same unregister-then-destroy is spelled out with the API there is */ |
| 107 | +void pt_abi_weakrefs_hash_clean(HashTable *ht) |
| 108 | +{ |
| 109 | +#if PHP_VERSION_ID >= 80500 |
| 110 | + zend_weakrefs_hash_clean(ht); |
| 111 | +#else |
| 112 | + zend_ulong objKey; |
| 113 | + ZEND_HASH_MAP_FOREACH_NUM_KEY(ht, objKey) { |
| 114 | + zend_weakrefs_hash_del(ht, zend_weakref_key_to_object(objKey)); |
| 115 | + } ZEND_HASH_FOREACH_END(); |
| 116 | +#endif |
| 117 | +} |
| 118 | + |
| 119 | +void pt_abi_weakrefs_hash_destroy(HashTable *ht) |
| 120 | +{ |
| 121 | +#if PHP_VERSION_ID >= 80500 |
| 122 | + zend_weakrefs_hash_destroy(ht); |
| 123 | +#else |
| 124 | + pt_abi_weakrefs_hash_clean(ht); |
| 125 | + zend_hash_destroy(ht); |
| 126 | +#endif |
| 127 | +} |
| 128 | + |
| 129 | +void pt_abi_call_stack_size_error() |
| 130 | +{ |
| 131 | +#if PHP_VERSION_ID >= 80400 |
| 132 | + zend_call_stack_size_error(); |
| 133 | +#else |
| 134 | + /* static in PHP 8.3; the same message */ |
| 135 | + zend_throw_error(nullptr, "Maximum call stack size of %zu bytes (zend.max_allowed_stack_size - zend.reserved_stack_size) reached. Infinite recursion?", |
| 136 | + (size_t) ((uintptr_t) EG(stack_base) - (uintptr_t) EG(stack_limit))); |
| 137 | +#endif |
| 138 | +} |
| 139 | + |
| 140 | +void pt_abi_try_assign_ref_str(zval *zv, zend_string *str) |
| 141 | +{ |
| 142 | + ZEND_TRY_ASSIGN_REF_STR(zv, str); |
| 143 | +} |
| 144 | + |
| 145 | +void pt_abi_try_assign_ref_true(zval *zv) |
| 146 | +{ |
| 147 | + ZEND_TRY_ASSIGN_REF_TRUE(zv); |
| 148 | +} |
0 commit comments