xref: /PHP-8.3/Zend/zend_observer.c (revision 709540cc)
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(function) \
27 	ZEND_OP_ARRAY_EXTENSION((&(function)->common), zend_observer_fcall_op_array_extension)
28 
29 #define ZEND_OBSERVER_NOT_OBSERVED ((void *) 2)
30 
31 #define ZEND_OBSERVABLE_FN(function) \
32 	(ZEND_MAP_PTR(function->common.run_time_cache) && !(function->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE))
33 
34 zend_llist zend_observers_fcall_list;
35 zend_llist zend_observer_function_declared_callbacks;
36 zend_llist zend_observer_class_linked_callbacks;
37 zend_llist zend_observer_error_callbacks;
38 zend_llist zend_observer_fiber_init;
39 zend_llist zend_observer_fiber_switch;
40 zend_llist zend_observer_fiber_destroy;
41 
42 int zend_observer_fcall_op_array_extension;
43 bool zend_observer_errors_observed;
44 bool zend_observer_function_declared_observed;
45 bool zend_observer_class_linked_observed;
46 
47 ZEND_TLS zend_execute_data *current_observed_frame;
48 
49 // Call during minit/startup ONLY
zend_observer_fcall_register(zend_observer_fcall_init init)50 ZEND_API void zend_observer_fcall_register(zend_observer_fcall_init init)
51 {
52 	zend_llist_add_element(&zend_observers_fcall_list, &init);
53 }
54 
55 // Called by engine before MINITs
zend_observer_startup(void)56 ZEND_API void zend_observer_startup(void)
57 {
58 	zend_llist_init(&zend_observers_fcall_list, sizeof(zend_observer_fcall_init), NULL, 1);
59 	zend_llist_init(&zend_observer_function_declared_callbacks, sizeof(zend_observer_function_declared_cb), NULL, 1);
60 	zend_llist_init(&zend_observer_class_linked_callbacks, sizeof(zend_observer_class_linked_cb), NULL, 1);
61 	zend_llist_init(&zend_observer_error_callbacks, sizeof(zend_observer_error_cb), NULL, 1);
62 	zend_llist_init(&zend_observer_fiber_init, sizeof(zend_observer_fiber_init_handler), NULL, 1);
63 	zend_llist_init(&zend_observer_fiber_switch, sizeof(zend_observer_fiber_switch_handler), NULL, 1);
64 	zend_llist_init(&zend_observer_fiber_destroy, sizeof(zend_observer_fiber_destroy_handler), NULL, 1);
65 
66 	zend_observer_fcall_op_array_extension = -1;
67 }
68 
zend_observer_post_startup(void)69 ZEND_API void zend_observer_post_startup(void)
70 {
71 	if (zend_observers_fcall_list.count) {
72 		/* We don't want to get an extension handle unless an ext installs an observer
73 		 * Allocate each a begin and an end pointer */
74 		zend_observer_fcall_op_array_extension =
75 			zend_get_op_array_extension_handles("Zend Observer", (int) zend_observers_fcall_list.count * 2);
76 
77 		/* ZEND_CALL_TRAMPOLINE has SPEC(OBSERVER) but zend_init_call_trampoline_op()
78 		 * is called before any extensions have registered as an observer. So we
79 		 * adjust the offset to the observed handler when we know we need to observe. */
80 		ZEND_VM_SET_OPCODE_HANDLER(&EG(call_trampoline_op));
81 
82 		/* ZEND_HANDLE_EXCEPTION also has SPEC(OBSERVER) and no observer extensions
83 		 * exist when zend_init_exception_op() is called. */
84 		ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op));
85 		ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op) + 1);
86 		ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op) + 2);
87 
88 		// Add an observer temporary to store previous observed frames
89 		zend_internal_function *zif;
90 		ZEND_HASH_FOREACH_PTR(CG(function_table), zif) {
91 			++zif->T;
92 		} ZEND_HASH_FOREACH_END();
93 		zend_class_entry *ce;
94 		ZEND_HASH_MAP_FOREACH_PTR(CG(class_table), ce) {
95 			ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, zif) {
96 				++zif->T;
97 			} ZEND_HASH_FOREACH_END();
98 		} ZEND_HASH_FOREACH_END();
99 	}
100 }
101 
zend_observer_activate(void)102 ZEND_API void zend_observer_activate(void)
103 {
104 	current_observed_frame = NULL;
105 }
106 
zend_observer_shutdown(void)107 ZEND_API void zend_observer_shutdown(void)
108 {
109 	zend_llist_destroy(&zend_observers_fcall_list);
110 	zend_llist_destroy(&zend_observer_function_declared_callbacks);
111 	zend_llist_destroy(&zend_observer_class_linked_callbacks);
112 	zend_llist_destroy(&zend_observer_error_callbacks);
113 	zend_llist_destroy(&zend_observer_fiber_init);
114 	zend_llist_destroy(&zend_observer_fiber_switch);
115 	zend_llist_destroy(&zend_observer_fiber_destroy);
116 }
117 
zend_observer_fcall_install(zend_execute_data * execute_data)118 static void zend_observer_fcall_install(zend_execute_data *execute_data)
119 {
120 	zend_llist *list = &zend_observers_fcall_list;
121 	zend_function *function = execute_data->func;
122 
123 	ZEND_ASSERT(RUN_TIME_CACHE(&function->common));
124 	zend_observer_fcall_begin_handler *begin_handlers = (zend_observer_fcall_begin_handler *)&ZEND_OBSERVER_DATA(function);
125 	zend_observer_fcall_end_handler *end_handlers = (zend_observer_fcall_end_handler *)begin_handlers + list->count, *end_handlers_start = end_handlers;
126 
127 	*begin_handlers = ZEND_OBSERVER_NOT_OBSERVED;
128 	*end_handlers = ZEND_OBSERVER_NOT_OBSERVED;
129 
130 	for (zend_llist_element *element = list->head; element; element = element->next) {
131 		zend_observer_fcall_init init;
132 		memcpy(&init, element->data, sizeof init);
133 		zend_observer_fcall_handlers handlers = init(execute_data);
134 		if (handlers.begin) {
135 			*(begin_handlers++) = handlers.begin;
136 		}
137 		if (handlers.end) {
138 			*(end_handlers++) = handlers.end;
139 		}
140 	}
141 
142 	// end handlers are executed in reverse order
143 	for (--end_handlers; end_handlers_start < end_handlers; --end_handlers, ++end_handlers_start) {
144 		zend_observer_fcall_end_handler tmp = *end_handlers;
145 		*end_handlers = *end_handlers_start;
146 		*end_handlers_start = tmp;
147 	}
148 }
149 
zend_observer_remove_handler(void ** first_handler,void * old_handler)150 static bool zend_observer_remove_handler(void **first_handler, void *old_handler) {
151 	size_t registered_observers = zend_observers_fcall_list.count;
152 
153 	void **last_handler = first_handler + registered_observers - 1;
154 	for (void **cur_handler = first_handler; cur_handler <= last_handler; ++cur_handler) {
155 		if (*cur_handler == old_handler) {
156 			if (registered_observers == 1 || (cur_handler == first_handler && cur_handler[1] == NULL)) {
157 				*cur_handler = ZEND_OBSERVER_NOT_OBSERVED;
158 			} else {
159 				if (cur_handler != last_handler) {
160 					memmove(cur_handler, cur_handler + 1, sizeof(cur_handler) * (last_handler - cur_handler));
161 				}
162 				*last_handler = NULL;
163 			}
164 			return true;
165 		}
166 	}
167 	return false;
168 }
169 
zend_observer_add_begin_handler(zend_function * function,zend_observer_fcall_begin_handler begin)170 ZEND_API void zend_observer_add_begin_handler(zend_function *function, zend_observer_fcall_begin_handler begin) {
171 	size_t registered_observers = zend_observers_fcall_list.count;
172 	zend_observer_fcall_begin_handler *first_handler = (void *)&ZEND_OBSERVER_DATA(function), *last_handler = first_handler + registered_observers - 1;
173 	if (*first_handler == ZEND_OBSERVER_NOT_OBSERVED) {
174 		*first_handler = begin;
175 	} else {
176 		for (zend_observer_fcall_begin_handler *cur_handler = first_handler + 1; cur_handler <= last_handler; ++cur_handler) {
177 			if (*cur_handler == NULL) {
178 				*cur_handler = begin;
179 				return;
180 			}
181 		}
182 		// there's no space for new handlers, then it's forbidden to call this function
183 		ZEND_UNREACHABLE();
184 	}
185 }
186 
zend_observer_remove_begin_handler(zend_function * function,zend_observer_fcall_begin_handler begin)187 ZEND_API bool zend_observer_remove_begin_handler(zend_function *function, zend_observer_fcall_begin_handler begin) {
188 	return zend_observer_remove_handler((void **)&ZEND_OBSERVER_DATA(function), begin);
189 }
190 
zend_observer_add_end_handler(zend_function * function,zend_observer_fcall_end_handler end)191 ZEND_API void zend_observer_add_end_handler(zend_function *function, zend_observer_fcall_end_handler end) {
192 	size_t registered_observers = zend_observers_fcall_list.count;
193 	zend_observer_fcall_end_handler *end_handler = (zend_observer_fcall_end_handler *)&ZEND_OBSERVER_DATA(function) + registered_observers;
194 	// to allow to preserve the invariant that end handlers are in reverse order of begin handlers, push the new end handler in front
195 	if (*end_handler != ZEND_OBSERVER_NOT_OBSERVED) {
196 		// there's no space for new handlers, then it's forbidden to call this function
197 		ZEND_ASSERT(end_handler[registered_observers - 1] == NULL);
198 		memmove(end_handler + 1, end_handler, sizeof(end_handler) * (registered_observers - 1));
199 	}
200 	*end_handler = end;
201 }
202 
zend_observer_remove_end_handler(zend_function * function,zend_observer_fcall_end_handler end)203 ZEND_API bool zend_observer_remove_end_handler(zend_function *function, zend_observer_fcall_end_handler end) {
204 	size_t registered_observers = zend_observers_fcall_list.count;
205 	return zend_observer_remove_handler((void **)&ZEND_OBSERVER_DATA(function) + registered_observers, end);
206 }
207 
prev_observed_frame(zend_execute_data * execute_data)208 static inline zend_execute_data **prev_observed_frame(zend_execute_data *execute_data) {
209 	zend_function *func = EX(func);
210 	ZEND_ASSERT(func);
211 	return (zend_execute_data **)&Z_PTR_P(EX_VAR_NUM((ZEND_USER_CODE(func->type) ? func->op_array.last_var : ZEND_CALL_NUM_ARGS(execute_data)) + func->common.T - 1));
212 }
213 
_zend_observe_fcall_begin(zend_execute_data * execute_data)214 static void ZEND_FASTCALL _zend_observe_fcall_begin(zend_execute_data *execute_data)
215 {
216 	if (!ZEND_OBSERVER_ENABLED) {
217 		return;
218 	}
219 
220 	zend_function *function = execute_data->func;
221 
222 	if (!ZEND_OBSERVABLE_FN(function)) {
223 		return;
224 	}
225 
226 	zend_observer_fcall_begin_handler *handler = (zend_observer_fcall_begin_handler *)&ZEND_OBSERVER_DATA(function);
227 	if (!*handler) {
228 		zend_observer_fcall_install(execute_data);
229 	}
230 
231 	zend_observer_fcall_begin_handler *possible_handlers_end = handler + zend_observers_fcall_list.count;
232 
233 	zend_observer_fcall_end_handler *end_handler = (zend_observer_fcall_end_handler *)possible_handlers_end;
234 	if (*end_handler != ZEND_OBSERVER_NOT_OBSERVED) {
235 		*prev_observed_frame(execute_data) = current_observed_frame;
236 		current_observed_frame = execute_data;
237 	}
238 
239 	if (*handler == ZEND_OBSERVER_NOT_OBSERVED) {
240 		return;
241 	}
242 
243 	do {
244 		(*handler)(execute_data);
245 	} while (++handler != possible_handlers_end && *handler != NULL);
246 }
247 
zend_observer_generator_resume(zend_execute_data * execute_data)248 ZEND_API void ZEND_FASTCALL zend_observer_generator_resume(zend_execute_data *execute_data)
249 {
250 	_zend_observe_fcall_begin(execute_data);
251 }
252 
zend_observer_fcall_begin(zend_execute_data * execute_data)253 ZEND_API void ZEND_FASTCALL zend_observer_fcall_begin(zend_execute_data *execute_data)
254 {
255 	ZEND_ASSUME(execute_data->func);
256 	if (!(execute_data->func->common.fn_flags & ZEND_ACC_GENERATOR)) {
257 		_zend_observe_fcall_begin(execute_data);
258 	}
259 }
260 
call_end_observers(zend_execute_data * execute_data,zval * return_value)261 static inline void call_end_observers(zend_execute_data *execute_data, zval *return_value) {
262 	zend_function *func = execute_data->func;
263 	ZEND_ASSERT(func);
264 
265 	zend_observer_fcall_end_handler *handler = (zend_observer_fcall_end_handler *)&ZEND_OBSERVER_DATA(func) + zend_observers_fcall_list.count;
266 	// TODO: Fix exceptions from generators
267 	// ZEND_ASSERT(fcall_data);
268 	if (!*handler || *handler == ZEND_OBSERVER_NOT_OBSERVED) {
269 		return;
270 	}
271 
272 	zend_observer_fcall_end_handler *possible_handlers_end = handler + zend_observers_fcall_list.count;
273 	do {
274 		(*handler)(execute_data, return_value);
275 	} while (++handler != possible_handlers_end && *handler != NULL);
276 }
277 
zend_observer_fcall_end(zend_execute_data * execute_data,zval * return_value)278 ZEND_API void ZEND_FASTCALL zend_observer_fcall_end(zend_execute_data *execute_data, zval *return_value)
279 {
280 	if (execute_data != current_observed_frame) {
281 		return;
282 	}
283 	call_end_observers(execute_data, return_value);
284 	current_observed_frame = *prev_observed_frame(execute_data);
285 }
286 
zend_observer_fcall_end_all(void)287 ZEND_API void zend_observer_fcall_end_all(void)
288 {
289 	zend_execute_data *execute_data = current_observed_frame, *original_execute_data = EG(current_execute_data);
290 	current_observed_frame = NULL;
291 	while (execute_data) {
292 		EG(current_execute_data) = execute_data;
293 		call_end_observers(execute_data, NULL);
294 		execute_data = *prev_observed_frame(execute_data);
295 	}
296 	EG(current_execute_data) = original_execute_data;
297 }
298 
zend_observer_function_declared_register(zend_observer_function_declared_cb cb)299 ZEND_API void zend_observer_function_declared_register(zend_observer_function_declared_cb cb)
300 {
301 	zend_observer_function_declared_observed = true;
302 	zend_llist_add_element(&zend_observer_function_declared_callbacks, &cb);
303 }
304 
_zend_observer_function_declared_notify(zend_op_array * op_array,zend_string * name)305 ZEND_API void ZEND_FASTCALL _zend_observer_function_declared_notify(zend_op_array *op_array, zend_string *name)
306 {
307 	if (CG(compiler_options) & ZEND_COMPILE_IGNORE_OBSERVER) {
308 		return;
309 	}
310 
311 	for (zend_llist_element *element = zend_observer_function_declared_callbacks.head; element; element = element->next) {
312 		zend_observer_function_declared_cb callback = *(zend_observer_function_declared_cb *) (element->data);
313 		callback(op_array, name);
314 	}
315 }
316 
zend_observer_class_linked_register(zend_observer_class_linked_cb cb)317 ZEND_API void zend_observer_class_linked_register(zend_observer_class_linked_cb cb)
318 {
319 	zend_observer_class_linked_observed = true;
320 	zend_llist_add_element(&zend_observer_class_linked_callbacks, &cb);
321 }
322 
_zend_observer_class_linked_notify(zend_class_entry * ce,zend_string * name)323 ZEND_API void ZEND_FASTCALL _zend_observer_class_linked_notify(zend_class_entry *ce, zend_string *name)
324 {
325 	if (CG(compiler_options) & ZEND_COMPILE_IGNORE_OBSERVER) {
326 		return;
327 	}
328 
329 	for (zend_llist_element *element = zend_observer_class_linked_callbacks.head; element; element = element->next) {
330 		zend_observer_class_linked_cb callback = *(zend_observer_class_linked_cb *) (element->data);
331 		callback(ce, name);
332 	}
333 }
334 
zend_observer_error_register(zend_observer_error_cb cb)335 ZEND_API void zend_observer_error_register(zend_observer_error_cb cb)
336 {
337 	zend_observer_errors_observed = true;
338 	zend_llist_add_element(&zend_observer_error_callbacks, &cb);
339 }
340 
_zend_observer_error_notify(int type,zend_string * error_filename,uint32_t error_lineno,zend_string * message)341 ZEND_API void _zend_observer_error_notify(int type, zend_string *error_filename, uint32_t error_lineno, zend_string *message)
342 {
343 	for (zend_llist_element *element = zend_observer_error_callbacks.head; element; element = element->next) {
344 		zend_observer_error_cb callback = *(zend_observer_error_cb *) (element->data);
345 		callback(type, error_filename, error_lineno, message);
346 	}
347 }
348 
zend_observer_fiber_init_register(zend_observer_fiber_init_handler handler)349 ZEND_API void zend_observer_fiber_init_register(zend_observer_fiber_init_handler handler)
350 {
351 	zend_llist_add_element(&zend_observer_fiber_init, &handler);
352 }
353 
zend_observer_fiber_switch_register(zend_observer_fiber_switch_handler handler)354 ZEND_API void zend_observer_fiber_switch_register(zend_observer_fiber_switch_handler handler)
355 {
356 	zend_llist_add_element(&zend_observer_fiber_switch, &handler);
357 }
358 
zend_observer_fiber_destroy_register(zend_observer_fiber_destroy_handler handler)359 ZEND_API void zend_observer_fiber_destroy_register(zend_observer_fiber_destroy_handler handler)
360 {
361 	zend_llist_add_element(&zend_observer_fiber_destroy, &handler);
362 }
363 
zend_observer_fiber_init_notify(zend_fiber_context * initializing)364 ZEND_API void ZEND_FASTCALL zend_observer_fiber_init_notify(zend_fiber_context *initializing)
365 {
366 	zend_llist_element *element;
367 	zend_observer_fiber_init_handler callback;
368 
369 	initializing->top_observed_frame = NULL;
370 
371 	for (element = zend_observer_fiber_init.head; element; element = element->next) {
372 		callback = *(zend_observer_fiber_init_handler *) element->data;
373 		callback(initializing);
374 	}
375 }
376 
zend_observer_fiber_switch_notify(zend_fiber_context * from,zend_fiber_context * to)377 ZEND_API void ZEND_FASTCALL zend_observer_fiber_switch_notify(zend_fiber_context *from, zend_fiber_context *to)
378 {
379 	zend_llist_element *element;
380 	zend_observer_fiber_switch_handler callback;
381 
382 	if (from->status == ZEND_FIBER_STATUS_DEAD) {
383 		zend_observer_fcall_end_all(); // fiber is either finished (call will do nothing) or has bailed out
384 	}
385 
386 	for (element = zend_observer_fiber_switch.head; element; element = element->next) {
387 		callback = *(zend_observer_fiber_switch_handler *) element->data;
388 		callback(from, to);
389 	}
390 
391 	from->top_observed_frame = current_observed_frame;
392 	current_observed_frame = to->top_observed_frame;
393 }
394 
zend_observer_fiber_destroy_notify(zend_fiber_context * destroying)395 ZEND_API void ZEND_FASTCALL zend_observer_fiber_destroy_notify(zend_fiber_context *destroying)
396 {
397 	zend_llist_element *element;
398 	zend_observer_fiber_destroy_handler callback;
399 
400 	for (element = zend_observer_fiber_destroy.head; element; element = element->next) {
401 		callback = *(zend_observer_fiber_destroy_handler *) element->data;
402 		callback(destroying);
403 	}
404 }
405