xref: /PHP-8.1/Zend/zend_observer.c (revision 85b669e5)
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    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Levi Morrison <levim@php.net>                               |
16    |          Sammy Kaye Powers <sammyk@php.net>                          |
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 #define ZEND_OBSERVER_DATA(op_array) \
27 	ZEND_OP_ARRAY_EXTENSION(op_array, zend_observer_fcall_op_array_extension)
28 
29 #define ZEND_OBSERVER_NOT_OBSERVED ((void *) 2)
30 
31 #define ZEND_OBSERVABLE_FN(fn_flags) \
32 	(!(fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
33 
34 zend_llist zend_observers_fcall_list;
35 zend_llist zend_observer_error_callbacks;
36 zend_llist zend_observer_fiber_init;
37 zend_llist zend_observer_fiber_switch;
38 zend_llist zend_observer_fiber_destroy;
39 
40 int zend_observer_fcall_op_array_extension;
41 
42 ZEND_TLS zend_execute_data *first_observed_frame;
43 ZEND_TLS zend_execute_data *current_observed_frame;
44 
45 // Call during minit/startup ONLY
zend_observer_fcall_register(zend_observer_fcall_init init)46 ZEND_API void zend_observer_fcall_register(zend_observer_fcall_init init)
47 {
48 	zend_llist_add_element(&zend_observers_fcall_list, &init);
49 }
50 
51 // Called by engine before MINITs
zend_observer_startup(void)52 ZEND_API void zend_observer_startup(void)
53 {
54 	zend_llist_init(&zend_observers_fcall_list, sizeof(zend_observer_fcall_init), NULL, 1);
55 	zend_llist_init(&zend_observer_error_callbacks, sizeof(zend_observer_error_cb), NULL, 1);
56 	zend_llist_init(&zend_observer_fiber_init, sizeof(zend_observer_fiber_init_handler), NULL, 1);
57 	zend_llist_init(&zend_observer_fiber_switch, sizeof(zend_observer_fiber_switch_handler), NULL, 1);
58 	zend_llist_init(&zend_observer_fiber_destroy, sizeof(zend_observer_fiber_destroy_handler), NULL, 1);
59 
60 	zend_observer_fcall_op_array_extension = -1;
61 }
62 
zend_observer_post_startup(void)63 ZEND_API void zend_observer_post_startup(void)
64 {
65 	if (zend_observers_fcall_list.count) {
66 		/* We don't want to get an extension handle unless an ext installs an observer
67 		 * Allocate each a begin and an end pointer */
68 		zend_observer_fcall_op_array_extension =
69 			zend_get_op_array_extension_handles("Zend Observer", (int) zend_observers_fcall_list.count * 2);
70 
71 		/* ZEND_CALL_TRAMPOLINE has SPEC(OBSERVER) but zend_init_call_trampoline_op()
72 		 * is called before any extensions have registered as an observer. So we
73 		 * adjust the offset to the observed handler when we know we need to observe. */
74 		ZEND_VM_SET_OPCODE_HANDLER(&EG(call_trampoline_op));
75 
76 		/* ZEND_HANDLE_EXCEPTION also has SPEC(OBSERVER) and no observer extensions
77 		 * exist when zend_init_exception_op() is called. */
78 		ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op));
79 		ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op) + 1);
80 		ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op) + 2);
81 	}
82 }
83 
zend_observer_activate(void)84 ZEND_API void zend_observer_activate(void)
85 {
86 	first_observed_frame = NULL;
87 	current_observed_frame = NULL;
88 }
89 
zend_observer_deactivate(void)90 ZEND_API void zend_observer_deactivate(void)
91 {
92 	// now empty and unused, but kept for ABI compatibility
93 }
94 
zend_observer_shutdown(void)95 ZEND_API void zend_observer_shutdown(void)
96 {
97 	zend_llist_destroy(&zend_observers_fcall_list);
98 	zend_llist_destroy(&zend_observer_error_callbacks);
99 	zend_llist_destroy(&zend_observer_fiber_switch);
100 }
101 
zend_observer_fcall_install(zend_execute_data * execute_data)102 static void zend_observer_fcall_install(zend_execute_data *execute_data)
103 {
104 	zend_llist *list = &zend_observers_fcall_list;
105 	zend_function *function = execute_data->func;
106 	zend_op_array *op_array = &function->op_array;
107 
108 	ZEND_ASSERT(function->type != ZEND_INTERNAL_FUNCTION);
109 
110 	ZEND_ASSERT(RUN_TIME_CACHE(op_array));
111 	zend_observer_fcall_begin_handler *begin_handlers = (zend_observer_fcall_begin_handler *)&ZEND_OBSERVER_DATA(op_array);
112 	zend_observer_fcall_end_handler *end_handlers = (zend_observer_fcall_end_handler *)begin_handlers + list->count, *end_handlers_start = end_handlers;
113 
114 	*begin_handlers = ZEND_OBSERVER_NOT_OBSERVED;
115 	*end_handlers = ZEND_OBSERVER_NOT_OBSERVED;
116 
117 	for (zend_llist_element *element = list->head; element; element = element->next) {
118 		zend_observer_fcall_init init;
119 		memcpy(&init, element->data, sizeof init);
120 		zend_observer_fcall_handlers handlers = init(execute_data);
121 		if (handlers.begin) {
122 			*(begin_handlers++) = handlers.begin;
123 		}
124 		if (handlers.end) {
125 			*(end_handlers++) = handlers.end;
126 		}
127 	}
128 
129 	// end handlers are executed in reverse order
130 	for (--end_handlers; end_handlers_start < end_handlers; --end_handlers, ++end_handlers_start) {
131 		zend_observer_fcall_end_handler tmp = *end_handlers;
132 		*end_handlers = *end_handlers_start;
133 		*end_handlers_start = tmp;
134 	}
135 }
136 
_zend_observe_fcall_begin(zend_execute_data * execute_data)137 static void ZEND_FASTCALL _zend_observe_fcall_begin(zend_execute_data *execute_data)
138 {
139 	if (!ZEND_OBSERVER_ENABLED) {
140 		return;
141 	}
142 
143 	zend_op_array *op_array = &execute_data->func->op_array;
144 	uint32_t fn_flags = op_array->fn_flags;
145 
146 	if (!ZEND_OBSERVABLE_FN(fn_flags)) {
147 		return;
148 	}
149 
150 	zend_observer_fcall_begin_handler *handler = (zend_observer_fcall_begin_handler *)&ZEND_OBSERVER_DATA(op_array);
151 	if (!*handler) {
152 		zend_observer_fcall_install(execute_data);
153 	}
154 
155 	zend_observer_fcall_begin_handler *possible_handlers_end = handler + zend_observers_fcall_list.count;
156 
157 	zend_observer_fcall_end_handler *end_handler = (zend_observer_fcall_end_handler *)possible_handlers_end;
158 	if (*end_handler != ZEND_OBSERVER_NOT_OBSERVED) {
159 		if (first_observed_frame == NULL) {
160 			first_observed_frame = execute_data;
161 		}
162 		current_observed_frame = execute_data;
163 	}
164 
165 	if (*handler == ZEND_OBSERVER_NOT_OBSERVED) {
166 		return;
167 	}
168 
169 	do {
170 		(*handler)(execute_data);
171 	} while (++handler != possible_handlers_end && *handler != NULL);
172 }
173 
zend_observer_generator_resume(zend_execute_data * execute_data)174 ZEND_API void ZEND_FASTCALL zend_observer_generator_resume(zend_execute_data *execute_data)
175 {
176 	_zend_observe_fcall_begin(execute_data);
177 }
178 
zend_observer_fcall_begin(zend_execute_data * execute_data)179 ZEND_API void ZEND_FASTCALL zend_observer_fcall_begin(zend_execute_data *execute_data)
180 {
181 	ZEND_ASSUME(execute_data->func);
182 	if (!(execute_data->func->common.fn_flags & ZEND_ACC_GENERATOR)) {
183 		_zend_observe_fcall_begin(execute_data);
184 	}
185 }
186 
zend_observer_is_skipped_frame(zend_execute_data * execute_data)187 static inline bool zend_observer_is_skipped_frame(zend_execute_data *execute_data) {
188 	zend_function *func = execute_data->func;
189 
190 	if (!func || func->type == ZEND_INTERNAL_FUNCTION || !ZEND_OBSERVABLE_FN(func->common.fn_flags)) {
191 		return true;
192 	}
193 
194 	zend_observer_fcall_end_handler end_handler = (&ZEND_OBSERVER_DATA(&func->op_array))[zend_observers_fcall_list.count];
195 	if (end_handler == NULL || end_handler == ZEND_OBSERVER_NOT_OBSERVED) {
196 		return true;
197 	}
198 
199 	return false;
200 }
201 
zend_observer_fcall_end(zend_execute_data * execute_data,zval * return_value)202 ZEND_API void ZEND_FASTCALL zend_observer_fcall_end(zend_execute_data *execute_data, zval *return_value)
203 {
204 	zend_function *func = execute_data->func;
205 
206 	if (!ZEND_OBSERVER_ENABLED
207 	 || !ZEND_OBSERVABLE_FN(func->common.fn_flags)) {
208 		return;
209 	}
210 
211 	zend_observer_fcall_end_handler *handler = (zend_observer_fcall_end_handler *)&ZEND_OBSERVER_DATA(&func->op_array) + zend_observers_fcall_list.count;
212 	// TODO: Fix exceptions from generators
213 	// ZEND_ASSERT(fcall_data);
214 	if (!*handler || *handler == ZEND_OBSERVER_NOT_OBSERVED) {
215 		return;
216 	}
217 
218 	zend_observer_fcall_end_handler *possible_handlers_end = handler + zend_observers_fcall_list.count;
219 	do {
220 		(*handler)(execute_data, return_value);
221 	} while (++handler != possible_handlers_end && *handler != NULL);
222 
223 	if (first_observed_frame == execute_data) {
224 		first_observed_frame = NULL;
225 		current_observed_frame = NULL;
226 	} else {
227 		zend_execute_data *ex = execute_data->prev_execute_data;
228 		while (ex && zend_observer_is_skipped_frame(ex)) {
229 			ex = ex->prev_execute_data;
230 		}
231 		current_observed_frame = ex;
232 	}
233 }
234 
zend_observer_fcall_end_all(void)235 ZEND_API void zend_observer_fcall_end_all(void)
236 {
237 	zend_execute_data *ex = current_observed_frame;
238 	while (ex != NULL) {
239 		if (ex->func && ex->func->type != ZEND_INTERNAL_FUNCTION) {
240 			zend_observer_fcall_end(ex, NULL);
241 		}
242 		ex = ex->prev_execute_data;
243 	}
244 }
245 
zend_observer_error_register(zend_observer_error_cb cb)246 ZEND_API void zend_observer_error_register(zend_observer_error_cb cb)
247 {
248 	zend_llist_add_element(&zend_observer_error_callbacks, &cb);
249 }
250 
zend_observer_error_notify(int type,zend_string * error_filename,uint32_t error_lineno,zend_string * message)251 void zend_observer_error_notify(int type, zend_string *error_filename, uint32_t error_lineno, zend_string *message)
252 {
253 	for (zend_llist_element *element = zend_observer_error_callbacks.head; element; element = element->next) {
254 		zend_observer_error_cb callback = *(zend_observer_error_cb *) (element->data);
255 		callback(type, error_filename, error_lineno, message);
256 	}
257 }
258 
zend_observer_fiber_init_register(zend_observer_fiber_init_handler handler)259 ZEND_API void zend_observer_fiber_init_register(zend_observer_fiber_init_handler handler)
260 {
261 	zend_llist_add_element(&zend_observer_fiber_init, &handler);
262 }
263 
zend_observer_fiber_switch_register(zend_observer_fiber_switch_handler handler)264 ZEND_API void zend_observer_fiber_switch_register(zend_observer_fiber_switch_handler handler)
265 {
266 	zend_llist_add_element(&zend_observer_fiber_switch, &handler);
267 }
268 
zend_observer_fiber_destroy_register(zend_observer_fiber_destroy_handler handler)269 ZEND_API void zend_observer_fiber_destroy_register(zend_observer_fiber_destroy_handler handler)
270 {
271 	zend_llist_add_element(&zend_observer_fiber_destroy, &handler);
272 }
273 
zend_observer_fiber_init_notify(zend_fiber_context * initializing)274 ZEND_API void ZEND_FASTCALL zend_observer_fiber_init_notify(zend_fiber_context *initializing)
275 {
276 	zend_llist_element *element;
277 	zend_observer_fiber_init_handler callback;
278 
279 	for (element = zend_observer_fiber_init.head; element; element = element->next) {
280 		callback = *(zend_observer_fiber_init_handler *) element->data;
281 		callback(initializing);
282 	}
283 }
284 
zend_observer_fiber_switch_notify(zend_fiber_context * from,zend_fiber_context * to)285 ZEND_API void ZEND_FASTCALL zend_observer_fiber_switch_notify(zend_fiber_context *from, zend_fiber_context *to)
286 {
287 	zend_llist_element *element;
288 	zend_observer_fiber_switch_handler callback;
289 
290 	for (element = zend_observer_fiber_switch.head; element; element = element->next) {
291 		callback = *(zend_observer_fiber_switch_handler *) element->data;
292 		callback(from, to);
293 	}
294 }
295 
zend_observer_fiber_destroy_notify(zend_fiber_context * destroying)296 ZEND_API void ZEND_FASTCALL zend_observer_fiber_destroy_notify(zend_fiber_context *destroying)
297 {
298 	zend_llist_element *element;
299 	zend_observer_fiber_destroy_handler callback;
300 
301 	for (element = zend_observer_fiber_destroy.head; element; element = element->next) {
302 		callback = *(zend_observer_fiber_destroy_handler *) element->data;
303 		callback(destroying);
304 	}
305 }
306