zend_call_graph.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine, Call Graph |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Dmitry Stogov <dmitry@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "zend_compile.h"
  20. #include "zend_extensions.h"
  21. #include "Optimizer/zend_optimizer.h"
  22. #include "zend_optimizer_internal.h"
  23. #include "zend_inference.h"
  24. #include "zend_call_graph.h"
  25. #include "zend_func_info.h"
  26. #include "zend_inference.h"
  27. #include "zend_call_graph.h"
  28. typedef int (*zend_op_array_func_t)(zend_call_graph *call_graph, zend_op_array *op_array);
  29. static int zend_op_array_calc(zend_call_graph *call_graph, zend_op_array *op_array)
  30. {
  31. (void) op_array;
  32. call_graph->op_arrays_count++;
  33. return SUCCESS;
  34. }
  35. static int zend_op_array_collect(zend_call_graph *call_graph, zend_op_array *op_array)
  36. {
  37. zend_func_info *func_info = call_graph->func_infos + call_graph->op_arrays_count;
  38. ZEND_SET_FUNC_INFO(op_array, func_info);
  39. call_graph->op_arrays[call_graph->op_arrays_count] = op_array;
  40. func_info->num = call_graph->op_arrays_count;
  41. func_info->num_args = -1;
  42. func_info->return_value_used = -1;
  43. call_graph->op_arrays_count++;
  44. return SUCCESS;
  45. }
  46. static int zend_foreach_op_array(zend_call_graph *call_graph, zend_script *script, zend_op_array_func_t func)
  47. {
  48. zend_class_entry *ce;
  49. zend_op_array *op_array;
  50. if (func(call_graph, &script->main_op_array) != SUCCESS) {
  51. return FAILURE;
  52. }
  53. ZEND_HASH_FOREACH_PTR(&script->function_table, op_array) {
  54. if (func(call_graph, op_array) != SUCCESS) {
  55. return FAILURE;
  56. }
  57. } ZEND_HASH_FOREACH_END();
  58. ZEND_HASH_FOREACH_PTR(&script->class_table, ce) {
  59. ZEND_HASH_FOREACH_PTR(&ce->function_table, op_array) {
  60. if (op_array->scope == ce) {
  61. if (func(call_graph, op_array) != SUCCESS) {
  62. return FAILURE;
  63. }
  64. }
  65. } ZEND_HASH_FOREACH_END();
  66. } ZEND_HASH_FOREACH_END();
  67. return SUCCESS;
  68. }
  69. int zend_analyze_calls(zend_arena **arena, zend_script *script, uint32_t build_flags, zend_op_array *op_array, zend_func_info *func_info)
  70. {
  71. zend_op *opline = op_array->opcodes;
  72. zend_op *end = opline + op_array->last;
  73. zend_function *func;
  74. zend_call_info *call_info;
  75. int call = 0;
  76. zend_call_info **call_stack;
  77. ALLOCA_FLAG(use_heap);
  78. call_stack = do_alloca((op_array->last / 2) * sizeof(zend_call_info*), use_heap);
  79. call_info = NULL;
  80. while (opline != end) {
  81. switch (opline->opcode) {
  82. case ZEND_INIT_FCALL:
  83. case ZEND_INIT_METHOD_CALL:
  84. case ZEND_INIT_STATIC_METHOD_CALL:
  85. call_stack[call] = call_info;
  86. func = zend_optimizer_get_called_func(
  87. script, op_array, opline, (build_flags & ZEND_RT_CONSTANTS) != 0);
  88. if (func) {
  89. call_info = zend_arena_calloc(arena, 1, sizeof(zend_call_info) + (sizeof(zend_send_arg_info) * ((int)opline->extended_value - 1)));
  90. call_info->caller_op_array = op_array;
  91. call_info->caller_init_opline = opline;
  92. call_info->caller_call_opline = NULL;
  93. call_info->callee_func = func;
  94. call_info->num_args = opline->extended_value;
  95. call_info->next_callee = func_info->callee_info;
  96. func_info->callee_info = call_info;
  97. if (build_flags & ZEND_CALL_TREE) {
  98. call_info->next_caller = NULL;
  99. } else if (func->type == ZEND_INTERNAL_FUNCTION) {
  100. call_info->next_caller = NULL;
  101. } else {
  102. zend_func_info *callee_func_info = ZEND_FUNC_INFO(&func->op_array);
  103. if (callee_func_info) {
  104. call_info->next_caller = callee_func_info->caller_info;
  105. callee_func_info->caller_info = call_info;
  106. } else {
  107. call_info->next_caller = NULL;
  108. }
  109. }
  110. } else {
  111. call_info = NULL;
  112. }
  113. call++;
  114. break;
  115. case ZEND_INIT_FCALL_BY_NAME:
  116. case ZEND_INIT_NS_FCALL_BY_NAME:
  117. case ZEND_INIT_DYNAMIC_CALL:
  118. case ZEND_NEW:
  119. case ZEND_INIT_USER_CALL:
  120. call_stack[call] = call_info;
  121. call_info = NULL;
  122. call++;
  123. break;
  124. case ZEND_DO_FCALL:
  125. case ZEND_DO_ICALL:
  126. case ZEND_DO_UCALL:
  127. case ZEND_DO_FCALL_BY_NAME:
  128. func_info->flags |= ZEND_FUNC_HAS_CALLS;
  129. if (call_info) {
  130. call_info->caller_call_opline = opline;
  131. }
  132. call--;
  133. call_info = call_stack[call];
  134. break;
  135. case ZEND_SEND_VAL:
  136. case ZEND_SEND_VAR:
  137. case ZEND_SEND_VAL_EX:
  138. case ZEND_SEND_VAR_EX:
  139. case ZEND_SEND_FUNC_ARG:
  140. case ZEND_SEND_REF:
  141. case ZEND_SEND_VAR_NO_REF:
  142. case ZEND_SEND_VAR_NO_REF_EX:
  143. case ZEND_SEND_USER:
  144. if (call_info) {
  145. uint32_t num = opline->op2.num;
  146. if (num > 0) {
  147. num--;
  148. }
  149. call_info->arg_info[num].opline = opline;
  150. }
  151. break;
  152. case ZEND_SEND_ARRAY:
  153. case ZEND_SEND_UNPACK:
  154. /* TODO: set info about var_arg call ??? */
  155. if (call_info) {
  156. call_info->num_args = -1;
  157. }
  158. break;
  159. case ZEND_EXIT:
  160. /* In this case the DO_CALL opcode may have been dropped
  161. * and caller_call_opline will be NULL. */
  162. break;
  163. }
  164. opline++;
  165. }
  166. free_alloca(call_stack, use_heap);
  167. return SUCCESS;
  168. }
  169. static int zend_is_indirectly_recursive(zend_op_array *root, zend_op_array *op_array, zend_bitset visited)
  170. {
  171. zend_func_info *func_info;
  172. zend_call_info *call_info;
  173. int ret = 0;
  174. if (op_array == root) {
  175. return 1;
  176. }
  177. func_info = ZEND_FUNC_INFO(op_array);
  178. if (zend_bitset_in(visited, func_info->num)) {
  179. return 0;
  180. }
  181. zend_bitset_incl(visited, func_info->num);
  182. call_info = func_info->caller_info;
  183. while (call_info) {
  184. if (zend_is_indirectly_recursive(root, call_info->caller_op_array, visited)) {
  185. call_info->recursive = 1;
  186. ret = 1;
  187. }
  188. call_info = call_info->next_caller;
  189. }
  190. return ret;
  191. }
  192. static void zend_analyze_recursion(zend_call_graph *call_graph)
  193. {
  194. zend_op_array *op_array;
  195. zend_func_info *func_info;
  196. zend_call_info *call_info;
  197. int i;
  198. int set_len = zend_bitset_len(call_graph->op_arrays_count);
  199. zend_bitset visited;
  200. ALLOCA_FLAG(use_heap);
  201. visited = ZEND_BITSET_ALLOCA(set_len, use_heap);
  202. for (i = 0; i < call_graph->op_arrays_count; i++) {
  203. op_array = call_graph->op_arrays[i];
  204. func_info = call_graph->func_infos + i;
  205. call_info = func_info->caller_info;
  206. while (call_info) {
  207. if (call_info->caller_op_array == op_array) {
  208. call_info->recursive = 1;
  209. func_info->flags |= ZEND_FUNC_RECURSIVE | ZEND_FUNC_RECURSIVE_DIRECTLY;
  210. } else {
  211. memset(visited, 0, sizeof(zend_ulong) * set_len);
  212. if (zend_is_indirectly_recursive(op_array, call_info->caller_op_array, visited)) {
  213. call_info->recursive = 1;
  214. func_info->flags |= ZEND_FUNC_RECURSIVE | ZEND_FUNC_RECURSIVE_INDIRECTLY;
  215. }
  216. }
  217. call_info = call_info->next_caller;
  218. }
  219. }
  220. free_alloca(visited, use_heap);
  221. }
  222. static void zend_sort_op_arrays(zend_call_graph *call_graph)
  223. {
  224. (void) call_graph;
  225. // TODO: perform topological sort of cyclic call graph
  226. }
  227. int zend_build_call_graph(zend_arena **arena, zend_script *script, uint32_t build_flags, zend_call_graph *call_graph) /* {{{ */
  228. {
  229. int i;
  230. call_graph->op_arrays_count = 0;
  231. if (zend_foreach_op_array(call_graph, script, zend_op_array_calc) != SUCCESS) {
  232. return FAILURE;
  233. }
  234. call_graph->op_arrays = (zend_op_array**)zend_arena_calloc(arena, call_graph->op_arrays_count, sizeof(zend_op_array*));
  235. call_graph->func_infos = (zend_func_info*)zend_arena_calloc(arena, call_graph->op_arrays_count, sizeof(zend_func_info));
  236. call_graph->op_arrays_count = 0;
  237. if (zend_foreach_op_array(call_graph, script, zend_op_array_collect) != SUCCESS) {
  238. return FAILURE;
  239. }
  240. for (i = 0; i < call_graph->op_arrays_count; i++) {
  241. zend_analyze_calls(arena, script, build_flags, call_graph->op_arrays[i], call_graph->func_infos + i);
  242. }
  243. zend_analyze_recursion(call_graph);
  244. zend_sort_op_arrays(call_graph);
  245. return SUCCESS;
  246. }
  247. /* }}} */
  248. zend_call_info **zend_build_call_map(zend_arena **arena, zend_func_info *info, zend_op_array *op_array) /* {{{ */
  249. {
  250. zend_call_info **map, *call;
  251. if (!info->callee_info) {
  252. /* Don't build call map if function contains no calls */
  253. return NULL;
  254. }
  255. map = zend_arena_calloc(arena, sizeof(zend_call_info *), op_array->last);
  256. for (call = info->callee_info; call; call = call->next_callee) {
  257. int i;
  258. map[call->caller_init_opline - op_array->opcodes] = call;
  259. if (call->caller_call_opline) {
  260. map[call->caller_call_opline - op_array->opcodes] = call;
  261. }
  262. for (i = 0; i < call->num_args; i++) {
  263. if (call->arg_info[i].opline) {
  264. map[call->arg_info[i].opline - op_array->opcodes] = call;
  265. }
  266. }
  267. }
  268. return map;
  269. }
  270. /* }}} */
  271. /*
  272. * Local variables:
  273. * tab-width: 4
  274. * c-basic-offset: 4
  275. * indent-tabs-mode: t
  276. * End:
  277. */