zend_func_info.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine, Func Info |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 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. | https://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. | Xinchen Hui <laruence@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend_compile.h"
  20. #include "zend_extensions.h"
  21. #include "zend_ssa.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. #ifdef _WIN32
  28. #include "win32/ioutil.h"
  29. #endif
  30. typedef uint32_t (*info_func_t)(const zend_call_info *call_info, const zend_ssa *ssa);
  31. typedef struct _func_info_t {
  32. const char *name;
  33. int name_len;
  34. uint32_t info;
  35. info_func_t info_func;
  36. } func_info_t;
  37. #define F0(name, info) \
  38. {name, sizeof(name)-1, (info), NULL}
  39. #define F1(name, info) \
  40. {name, sizeof(name)-1, (MAY_BE_RC1 | (info)), NULL}
  41. #define FN(name, info) \
  42. {name, sizeof(name)-1, (MAY_BE_RC1 | MAY_BE_RCN | (info)), NULL}
  43. #define FC(name, callback) \
  44. {name, sizeof(name)-1, 0, callback}
  45. #include "zend_func_infos.h"
  46. static uint32_t zend_range_info(const zend_call_info *call_info, const zend_ssa *ssa)
  47. {
  48. if (!call_info->send_unpack
  49. && (call_info->num_args == 2 || call_info->num_args == 3)
  50. && ssa
  51. && !(ssa->cfg.flags & ZEND_SSA_TSSA)) {
  52. zend_op_array *op_array = call_info->caller_op_array;
  53. uint32_t t1 = _ssa_op1_info(op_array, ssa, call_info->arg_info[0].opline,
  54. &ssa->ops[call_info->arg_info[0].opline - op_array->opcodes]);
  55. uint32_t t2 = _ssa_op1_info(op_array, ssa, call_info->arg_info[1].opline,
  56. &ssa->ops[call_info->arg_info[1].opline - op_array->opcodes]);
  57. uint32_t t3 = 0;
  58. uint32_t tmp = MAY_BE_RC1 | MAY_BE_ARRAY;
  59. if (call_info->num_args == 3) {
  60. t3 = _ssa_op1_info(op_array, ssa, call_info->arg_info[2].opline,
  61. &ssa->ops[call_info->arg_info[2].opline - op_array->opcodes]);
  62. }
  63. if ((t1 & MAY_BE_STRING) && (t2 & MAY_BE_STRING)) {
  64. tmp |= MAY_BE_ARRAY_OF_LONG | MAY_BE_ARRAY_OF_DOUBLE | MAY_BE_ARRAY_OF_STRING;
  65. }
  66. if ((t1 & (MAY_BE_DOUBLE|MAY_BE_STRING))
  67. || (t2 & (MAY_BE_DOUBLE|MAY_BE_STRING))
  68. || (t3 & (MAY_BE_DOUBLE|MAY_BE_STRING))) {
  69. tmp |= MAY_BE_ARRAY_OF_DOUBLE;
  70. }
  71. if ((t1 & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))
  72. && (t2 & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
  73. if ((t3 & MAY_BE_ANY) != MAY_BE_DOUBLE) {
  74. tmp |= MAY_BE_ARRAY_OF_LONG;
  75. }
  76. }
  77. if (tmp & MAY_BE_ARRAY_OF_ANY) {
  78. tmp |= MAY_BE_ARRAY_PACKED;
  79. }
  80. return tmp;
  81. } else {
  82. /* May throw */
  83. return MAY_BE_RC1 | MAY_BE_ARRAY | MAY_BE_ARRAY_PACKED | MAY_BE_ARRAY_OF_LONG | MAY_BE_ARRAY_OF_DOUBLE | MAY_BE_ARRAY_OF_STRING;
  84. }
  85. }
  86. static const func_info_t old_func_infos[] = {
  87. FC("range", zend_range_info),
  88. };
  89. static HashTable func_info;
  90. ZEND_API int zend_func_info_rid = -1;
  91. uint32_t zend_get_internal_func_info(
  92. const zend_function *callee_func, const zend_call_info *call_info, const zend_ssa *ssa) {
  93. if (callee_func->common.scope) {
  94. /* This is a method, not a function. */
  95. return 0;
  96. }
  97. zend_string *name = callee_func->common.function_name;
  98. if (!name) {
  99. /* zend_pass_function has no name. */
  100. return 0;
  101. }
  102. zval *zv = zend_hash_find_known_hash(&func_info, name);
  103. if (!zv) {
  104. return 0;
  105. }
  106. func_info_t *info = Z_PTR_P(zv);
  107. if (info->info_func) {
  108. return call_info ? info->info_func(call_info, ssa) : 0;
  109. } else {
  110. return info->info;
  111. }
  112. }
  113. ZEND_API uint32_t zend_get_func_info(
  114. const zend_call_info *call_info, const zend_ssa *ssa,
  115. zend_class_entry **ce, bool *ce_is_instanceof)
  116. {
  117. uint32_t ret = 0;
  118. const zend_function *callee_func = call_info->callee_func;
  119. *ce = NULL;
  120. *ce_is_instanceof = 0;
  121. if (callee_func->type == ZEND_INTERNAL_FUNCTION) {
  122. uint32_t internal_ret = zend_get_internal_func_info(callee_func, call_info, ssa);
  123. #if !ZEND_DEBUG
  124. if (internal_ret) {
  125. return internal_ret;
  126. }
  127. #endif
  128. ret = zend_get_return_info_from_signature_only(
  129. callee_func, /* script */ NULL, ce, ce_is_instanceof, /* use_tentative_return_info */ !call_info->is_prototype);
  130. #if ZEND_DEBUG
  131. if (internal_ret) {
  132. zend_string *name = callee_func->common.function_name;
  133. /* Check whether the func_info information is a subset of the information we can
  134. * compute from the specified return type, otherwise it contains redundant types. */
  135. if (internal_ret & ~ret) {
  136. fprintf(stderr, "Inaccurate func info for %s()\n", ZSTR_VAL(name));
  137. }
  138. /* Check whether the func info is completely redundant with arginfo. */
  139. if (internal_ret == ret) {
  140. fprintf(stderr, "Useless func info for %s()\n", ZSTR_VAL(name));
  141. }
  142. /* If the return type is not mixed, check that the types match exactly if we exclude
  143. * RC and array information. */
  144. uint32_t ret_any = ret & MAY_BE_ANY, internal_ret_any = internal_ret & MAY_BE_ANY;
  145. if (ret_any != MAY_BE_ANY) {
  146. uint32_t diff = internal_ret_any ^ ret_any;
  147. /* Func info may contain "true" types as well as isolated "null" and "false". */
  148. if (diff && !(diff == MAY_BE_FALSE && (ret & MAY_BE_FALSE))
  149. && (internal_ret_any & ~(MAY_BE_NULL|MAY_BE_FALSE))) {
  150. fprintf(stderr, "Incorrect func info for %s()\n", ZSTR_VAL(name));
  151. }
  152. }
  153. return internal_ret;
  154. }
  155. #endif
  156. } else {
  157. if (!call_info->is_prototype) {
  158. // FIXME: the order of functions matters!!!
  159. zend_func_info *info = ZEND_FUNC_INFO((zend_op_array*)callee_func);
  160. if (info) {
  161. ret = info->return_info.type;
  162. *ce = info->return_info.ce;
  163. *ce_is_instanceof = info->return_info.is_instanceof;
  164. }
  165. }
  166. if (!ret) {
  167. ret = zend_get_return_info_from_signature_only(
  168. callee_func, /* TODO: script */ NULL, ce, ce_is_instanceof, /* use_tentative_return_info */ !call_info->is_prototype);
  169. /* It's allowed to override a method that return non-reference with a method that returns a reference */
  170. if (call_info->is_prototype && (ret & ~MAY_BE_REF)) {
  171. ret |= MAY_BE_REF;
  172. *ce = NULL;
  173. }
  174. }
  175. }
  176. return ret;
  177. }
  178. static void zend_func_info_add(const func_info_t *func_infos, size_t n)
  179. {
  180. for (size_t i = 0; i < n; i++) {
  181. zend_string *key = zend_string_init_interned(func_infos[i].name, func_infos[i].name_len, 1);
  182. if (zend_hash_add_ptr(&func_info, key, (void**)&func_infos[i]) == NULL) {
  183. fprintf(stderr, "ERROR: Duplicate function info for \"%s\"\n", func_infos[i].name);
  184. }
  185. zend_string_release_ex(key, 1);
  186. }
  187. }
  188. int zend_func_info_startup(void)
  189. {
  190. if (zend_func_info_rid == -1) {
  191. zend_func_info_rid = zend_get_resource_handle("Zend Optimizer");
  192. if (zend_func_info_rid < 0) {
  193. return FAILURE;
  194. }
  195. zend_hash_init(&func_info, sizeof(old_func_infos)/sizeof(func_info_t) + sizeof(func_infos)/sizeof(func_info_t), NULL, NULL, 1);
  196. zend_func_info_add(old_func_infos, sizeof(old_func_infos)/sizeof(func_info_t));
  197. zend_func_info_add(func_infos, sizeof(func_infos)/sizeof(func_info_t));
  198. }
  199. return SUCCESS;
  200. }
  201. int zend_func_info_shutdown(void)
  202. {
  203. if (zend_func_info_rid != -1) {
  204. zend_hash_destroy(&func_info);
  205. zend_func_info_rid = -1;
  206. }
  207. return SUCCESS;
  208. }