|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | Zend Engine | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | This source file is subject to version 2.00 of the Zend license, | |
| 8 | + | that is bundled with this package in the file LICENSE, and is | |
| 9 | + | available through the world-wide-web at the following url: | |
| 10 | + | http://www.zend.com/license/2_00.txt. | |
| 11 | + | If you did not receive a copy of the Zend license and are unable to | |
| 12 | + | obtain it through the world-wide-web, please send a note to | |
| 13 | + | [email protected] so we can mail you a copy immediately. | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | + | Authors: Levi Morrison <[email protected]> | |
| 16 | + | Sammy Kaye Powers <[email protected]> | |
| 17 | + +----------------------------------------------------------------------+ |
| 18 | +*/ |
| 19 | + |
| 20 | +#include "zend_observer.h" |
| 21 | + |
| 22 | +#include "zend_extensions.h" |
| 23 | +#include "zend_llist.h" |
| 24 | +#include "zend_vm.h" |
| 25 | + |
| 26 | +zend_llist zend_observers_fcall_list; |
| 27 | +int zend_observer_fcall_op_array_extension = -1; |
| 28 | + |
| 29 | +ZEND_TLS zend_arena *fcall_handlers_arena = NULL; |
| 30 | + |
| 31 | +ZEND_API extern inline void zend_observer_maybe_fcall_call_begin( |
| 32 | + zend_execute_data *execute_data); |
| 33 | +ZEND_API extern inline void zend_observer_maybe_fcall_call_end( |
| 34 | + zend_execute_data *execute_data, |
| 35 | + zval *return_value); |
| 36 | + |
| 37 | +// Call during minit/startup ONLY |
| 38 | +ZEND_API void zend_observer_fcall_register(zend_observer_fcall_init init) { |
| 39 | + /* We don't want to get an extension handle unless an ext installs an observer */ |
| 40 | + if (!ZEND_OBSERVER_ENABLED) { |
| 41 | + zend_observer_fcall_op_array_extension = |
| 42 | + zend_get_op_array_extension_handle(); |
| 43 | + |
| 44 | + /* ZEND_CALL_TRAMPOLINE has SPEC(OBSERVER) but zend_init_call_trampoline_op() |
| 45 | + * is called before any extensions have registered as an observer. So we |
| 46 | + * adjust the offset to the observed handler when we know we need to observe. */ |
| 47 | + ZEND_VM_SET_OPCODE_HANDLER(&EG(call_trampoline_op)); |
| 48 | + |
| 49 | + /* ZEND_HANDLE_EXCEPTION also has SPEC(OBSERVER) and no observer extensions |
| 50 | + * exist when zend_init_exception_op() is called. */ |
| 51 | + ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)); |
| 52 | + ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+1); |
| 53 | + ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+2); |
| 54 | + } |
| 55 | + zend_llist_add_element(&zend_observers_fcall_list, &init); |
| 56 | +} |
| 57 | + |
| 58 | +// Called by engine before MINITs |
| 59 | +ZEND_API void zend_observer_startup(void) { |
| 60 | + zend_llist_init(&zend_observers_fcall_list, sizeof(zend_observer_fcall_init), NULL, 1); |
| 61 | +} |
| 62 | + |
| 63 | +ZEND_API void zend_observer_activate(void) { |
| 64 | + if (ZEND_OBSERVER_ENABLED) { |
| 65 | + fcall_handlers_arena = zend_arena_create(4096); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +ZEND_API void zend_observer_deactivate(void) { |
| 70 | + if (fcall_handlers_arena) { |
| 71 | + zend_arena_destroy(fcall_handlers_arena); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +ZEND_API void zend_observer_shutdown(void) { |
| 76 | + zend_llist_destroy(&zend_observers_fcall_list); |
| 77 | +} |
| 78 | + |
| 79 | +ZEND_API void zend_observer_fcall_install(zend_function *function) { |
| 80 | + zend_llist_element *element; |
| 81 | + zend_llist *list = &zend_observers_fcall_list; |
| 82 | + zend_op_array *op_array = &function->op_array; |
| 83 | + |
| 84 | + if (fcall_handlers_arena == NULL) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + ZEND_ASSERT(function->type != ZEND_INTERNAL_FUNCTION); |
| 89 | + |
| 90 | + zend_llist handlers_list; |
| 91 | + zend_llist_init(&handlers_list, sizeof(zend_observer_fcall), NULL, 0); |
| 92 | + for (element = list->head; element; element = element->next) { |
| 93 | + zend_observer_fcall_init init; |
| 94 | + memcpy(&init, element->data, sizeof init); |
| 95 | + zend_observer_fcall handlers = init(function); |
| 96 | + if (handlers.begin || handlers.end) { |
| 97 | + zend_llist_add_element(&handlers_list, &handlers); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + ZEND_ASSERT(RUN_TIME_CACHE(op_array)); |
| 102 | + void *ext; |
| 103 | + if (handlers_list.count) { |
| 104 | + size_t size = sizeof(zend_observer_fcall_cache) + (handlers_list.count - 1) * sizeof(zend_observer_fcall); |
| 105 | + zend_observer_fcall_cache *cache = zend_arena_alloc(&fcall_handlers_arena, size); |
| 106 | + zend_observer_fcall *handler = cache->handlers; |
| 107 | + for (element = handlers_list.head; element; element = element->next) { |
| 108 | + memcpy(handler++, element->data, sizeof *handler); |
| 109 | + } |
| 110 | + cache->end = handler; |
| 111 | + ext = cache; |
| 112 | + } else { |
| 113 | + ext = ZEND_OBSERVER_NOT_OBSERVED; |
| 114 | + } |
| 115 | + |
| 116 | + ZEND_OBSERVER_HANDLERS(op_array) = ext; |
| 117 | + zend_llist_destroy(&handlers_list); |
| 118 | +} |
| 119 | + |
| 120 | +ZEND_API void zend_observe_fcall_begin( |
| 121 | + zend_observer_fcall_cache *cache, |
| 122 | + zend_execute_data *execute_data) |
| 123 | +{ |
| 124 | + zend_observer_fcall *handler, *end = cache->end; |
| 125 | + for (handler = cache->handlers; handler != end; ++handler) { |
| 126 | + if (handler->begin) { |
| 127 | + handler->begin(execute_data); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +ZEND_API void zend_observer_fcall_call_end_helper( |
| 133 | + zend_execute_data *execute_data, |
| 134 | + zval *return_value) |
| 135 | +{ |
| 136 | + zend_function *func = execute_data->func; |
| 137 | + ZEND_ASSUME(ZEND_OBSERVABLE_FN(func->common.fn_flags)); |
| 138 | + void *observer_handlers = ZEND_OBSERVER_HANDLERS(&func->op_array); |
| 139 | + // TODO: Fix exceptions from generators |
| 140 | + // ZEND_ASSERT(observer_handlers); |
| 141 | + if (observer_handlers && observer_handlers != ZEND_OBSERVER_NOT_OBSERVED) { |
| 142 | + zend_observer_fcall_cache *cache = observer_handlers; |
| 143 | + zend_observe_fcall_end(cache, execute_data, return_value); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +ZEND_API void zend_observe_fcall_end( |
| 148 | + zend_observer_fcall_cache *cache, |
| 149 | + zend_execute_data *execute_data, |
| 150 | + zval *return_value) |
| 151 | +{ |
| 152 | + zend_observer_fcall *handler = cache->end, *end = cache->handlers; |
| 153 | + while (handler-- != end) { |
| 154 | + if (handler->end) { |
| 155 | + handler->end(execute_data, return_value); |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | + |
0 commit comments