std_object_handlers not interceptable?
Description
I am trying to implement a Aspect-oriented-programming extension for PHP, but since std_object_handlers got the const keyword, it seems to be impossible to intercept every std_object_handlers instance/object. I tried it with a zend_extension, but it seems not possible.
Overloading zend_execute_ex and zend_execute_internal for function handling works with ease like this:
original_zend_execute_ex = zend_execute_ex;
zend_execute_ex = aop_execute_ex;
Is there any other way to intercept read/write operations for every class/object instance in PHP? And LD_PRELOAD is not a option, this library should be installable with PECL.
I'm sorry if this is not a real bug, I can't find a solution and I have been looking for days.
PHP Version
PHP 8.1.2
Operating System
Ubuntu 20.04
Instead of changing std_object_handlers directly, it's also possible to swap handlers (the whole struct) at object creation time, i.e. by wrapping the create_object handlers.
I think, albeit it is a bit annoying, the const-ness of std_object_handlers makes sense, because a lot of code (and extensions) is memcpy()-ing the default handlers from there and may already have copied before or after your extension code even runs.
@bwoebi Can you please elaborate how I would achieve this? How is it possible to swap the struct?
basically, in a bit of pseudo code:
typedef struct {
zend_object_handlers *original_handlers;
zend_object (*original_create)(zend_class_entry *);
zend_object_handlers custom_handlers;
} original_class_data;
HashTable<zend_class_entry *, original_class_data> original_info;
void wrapped_custom_dtor(zend_object *obj) {
original_class_data *class_data = (original_class_data *)zend_hash_find_ptr(&original_info, obj->ce);
// do some stuff
class_data->original_handlers->dtor_obj(obj);
}
zend_object *wrapper_create_object(zend_class_entry *ce) {
original_class_data *class_data = (original_class_data *)zend_hash_find_ptr(&original_info, ce);
zend_object *obj = class_data->original_create(ce);
class_data->original_handlers = obj->handlers;
memcpy(&class_data->custom_handlers, obj->handlers, sizeof(zend_object_handlers));
// and whatever else you need to wrap
class_data->custom_handlers.dtor_obj = wrapped_custom_dtor;
obj->handlers = &class_data->custom_handlers;
return obj;
}
foreach (EG(class_table) as class_entry) {
original_class_data data;
data.original_create = class_entry->create_object;
class_entry->create_object = wrapper_create_object
zend_hash_add_ptr(&original_info, class_entry, &data);
}
I'm going to try that, thanks for the information!
@bwoebi I think this is not a viable option, because user-defined classes are not listed in the class_table
Edit: Nevermind, the op_array_handler inside a zend_extension seems to register user-defined classes.
@bwoebi I might be wrong, but as it seems, the create_object (zend_API.c:1658) is created after every PHP and Zend extension hook. Trying to overload the create_object, the original one returns NULL and calling it is therefore not possible.
Just to clarify, I am instantiating a custom class in a custom PHP file.
There seems to be a bit of miscommunication and or misunderstanding here.
We aren't going to change the constness of the standard handlers, and there is a usable alternative given here which hopefully by this time (several months later) you already figured out :)
@krakjoe Nope. I found nothing. I went through every little corner, but it is not possible. AOP and every library is dead since the costness change and now I'm creating my own programming language with AOP capabilities