ssa_integrity.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine, SSA validation |
  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: Nikita Popov <nikic@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "ZendAccelerator.h"
  19. #include "Optimizer/zend_optimizer_internal.h"
  20. /* The ssa_verify_integrity() function ensures that that certain invariants of the SSA form and
  21. * CFG are upheld and prints messages to stderr if this is not the case. */
  22. static inline zend_bool is_in_use_chain(zend_ssa *ssa, int var, int check) {
  23. int use;
  24. FOREACH_USE(&ssa->vars[var], use) {
  25. if (use == check) {
  26. return 1;
  27. }
  28. } FOREACH_USE_END();
  29. return 0;
  30. }
  31. static inline zend_bool is_in_phi_use_chain(zend_ssa *ssa, int var, zend_ssa_phi *check) {
  32. zend_ssa_phi *phi;
  33. FOREACH_PHI_USE(&ssa->vars[var], phi) {
  34. if (phi == check) {
  35. return 1;
  36. }
  37. } FOREACH_PHI_USE_END();
  38. return 0;
  39. }
  40. static inline zend_bool is_used_by_op(zend_ssa *ssa, int op, int check) {
  41. zend_ssa_op *ssa_op = &ssa->ops[op];
  42. return (ssa_op->op1_use == check)
  43. || (ssa_op->op2_use == check)
  44. || (ssa_op->result_use == check);
  45. }
  46. static inline zend_bool is_defined_by_op(zend_ssa *ssa, int op, int check) {
  47. zend_ssa_op *ssa_op = &ssa->ops[op];
  48. return (ssa_op->op1_def == check)
  49. || (ssa_op->op2_def == check)
  50. || (ssa_op->result_def == check);
  51. }
  52. static inline zend_bool is_in_phi_sources(zend_ssa *ssa, zend_ssa_phi *phi, int check) {
  53. int source;
  54. FOREACH_PHI_SOURCE(phi, source) {
  55. if (source == check) {
  56. return 1;
  57. }
  58. } FOREACH_PHI_SOURCE_END();
  59. return 0;
  60. }
  61. static inline zend_bool is_in_predecessors(zend_cfg *cfg, zend_basic_block *block, int check) {
  62. int i, *predecessors = &cfg->predecessors[block->predecessor_offset];
  63. for (i = 0; i < block->predecessors_count; i++) {
  64. if (predecessors[i] == check) {
  65. return 1;
  66. }
  67. }
  68. return 0;
  69. }
  70. static inline zend_bool is_in_successors(zend_basic_block *block, int check) {
  71. int s;
  72. for (s = 0; s < block->successors_count; s++) {
  73. if (block->successors[s] == check) {
  74. return 1;
  75. }
  76. }
  77. return 0;
  78. }
  79. static inline zend_bool is_var_type(zend_uchar type) {
  80. return type == IS_CV || type == IS_VAR || type == IS_TMP_VAR;
  81. }
  82. #define FAIL(...) do { \
  83. if (status == SUCCESS) { \
  84. fprintf(stderr, "\nIn function %s::%s (%s):\n", \
  85. op_array->scope ? ZSTR_VAL(op_array->scope->name) : "", \
  86. op_array->function_name ? ZSTR_VAL(op_array->function_name) : "{main}", extra); \
  87. } \
  88. fprintf(stderr, __VA_ARGS__); \
  89. status = FAILURE; \
  90. } while (0)
  91. #define VARFMT "%d (%s%s)"
  92. #define VAR(i) \
  93. (i), (ssa->vars[i].var < op_array->last_var ? "CV $" : "TMP"), \
  94. (ssa->vars[i].var < op_array->last_var ? ZSTR_VAL(op_array->vars[ssa->vars[i].var]) : "")
  95. #define INSTRFMT "%d (%s)"
  96. #define INSTR(i) \
  97. (i), (zend_get_opcode_name(op_array->opcodes[i].opcode))
  98. int ssa_verify_integrity(zend_op_array *op_array, zend_ssa *ssa, const char *extra) {
  99. zend_cfg *cfg = &ssa->cfg;
  100. zend_ssa_phi *phi;
  101. int i, status = SUCCESS;
  102. /* Vars */
  103. for (i = 0; i < ssa->vars_count; i++) {
  104. zend_ssa_var *var = &ssa->vars[i];
  105. int use, c;
  106. uint32_t type = ssa->var_info[i].type;
  107. if (var->definition < 0 && !var->definition_phi && i > op_array->last_var) {
  108. if (var->use_chain >= 0) {
  109. FAIL("var " VARFMT " without def has op uses\n", VAR(i));
  110. }
  111. if (var->phi_use_chain) {
  112. FAIL("var " VARFMT " without def has phi uses\n", VAR(i));
  113. }
  114. }
  115. if (var->definition >= 0 && var->definition_phi) {
  116. FAIL("var " VARFMT " has both def and def_phi\n", VAR(i));
  117. }
  118. if (var->definition >= 0) {
  119. if (!is_defined_by_op(ssa, var->definition, i)) {
  120. FAIL("var " VARFMT " not defined by op " INSTRFMT "\n",
  121. VAR(i), INSTR(var->definition));
  122. }
  123. }
  124. if (var->definition_phi) {
  125. if (var->definition_phi->ssa_var != i) {
  126. FAIL("var " VARFMT " not defined by given phi\n", VAR(i));
  127. }
  128. }
  129. c = 0;
  130. FOREACH_USE(var, use) {
  131. if (++c > 10000) {
  132. FAIL("cycle in uses of " VARFMT "\n", VAR(i));
  133. return status;
  134. }
  135. if (!is_used_by_op(ssa, use, i)) {
  136. fprintf(stderr, "var " VARFMT " not in uses of op %d\n", VAR(i), use);
  137. }
  138. } FOREACH_USE_END();
  139. c = 0;
  140. FOREACH_PHI_USE(var, phi) {
  141. if (++c > 10000) {
  142. FAIL("cycle in phi uses of " VARFMT "\n", VAR(i));
  143. return status;
  144. }
  145. if (!is_in_phi_sources(ssa, phi, i)) {
  146. FAIL("var " VARFMT " not in phi sources of %d\n", VAR(i), phi->ssa_var);
  147. }
  148. } FOREACH_PHI_USE_END();
  149. if ((type & MAY_BE_ARRAY_KEY_ANY) && !(type & MAY_BE_ARRAY_OF_ANY)) {
  150. FAIL("var " VARFMT " has array key type but not value type\n", VAR(i));
  151. }
  152. if ((type & MAY_BE_ARRAY_OF_ANY) && !(type & MAY_BE_ARRAY_KEY_ANY)) {
  153. FAIL("var " VARFMT " has array value type but not key type\n", VAR(i));
  154. }
  155. }
  156. /* Instructions */
  157. FOREACH_INSTR_NUM(i) {
  158. zend_ssa_op *ssa_op = &ssa->ops[i];
  159. zend_op *opline = &op_array->opcodes[i];
  160. if (is_var_type(opline->op1_type)) {
  161. if (ssa_op->op1_use < 0 && ssa_op->op1_def < 0) {
  162. FAIL("var op1 of " INSTRFMT " does not use/def an ssa var\n", INSTR(i));
  163. }
  164. } else {
  165. if (ssa_op->op1_use >= 0 || ssa_op->op1_def >= 0) {
  166. FAIL("non-var op1 of " INSTRFMT " uses or defs an ssa var\n", INSTR(i));
  167. }
  168. }
  169. if (is_var_type(opline->op2_type)) {
  170. if (ssa_op->op2_use < 0 && ssa_op->op2_def < 0) {
  171. FAIL("var op2 of " INSTRFMT " does not use/def an ssa var\n", INSTR(i));
  172. }
  173. } else {
  174. if (ssa_op->op2_use >= 0 || ssa_op->op2_def >= 0) {
  175. FAIL("non-var op2 of " INSTRFMT " uses or defs an ssa var\n", INSTR(i));
  176. }
  177. }
  178. if (is_var_type(opline->result_type)) {
  179. if (ssa_op->result_use < 0 && ssa_op->result_def < 0) {
  180. FAIL("var result of " INSTRFMT " does not use/def an ssa var\n", INSTR(i));
  181. }
  182. } else {
  183. if (ssa_op->result_use >= 0 || ssa_op->result_def >= 0) {
  184. FAIL("non-var result of " INSTRFMT " uses or defs an ssa var\n", INSTR(i));
  185. }
  186. }
  187. if (ssa_op->op1_use >= 0) {
  188. if (ssa_op->op1_use >= ssa->vars_count) {
  189. FAIL("op1 use %d out of range\n", ssa_op->op1_use);
  190. }
  191. if (!is_in_use_chain(ssa, ssa_op->op1_use, i)) {
  192. FAIL("op1 use of " VARFMT " in " INSTRFMT " not in use chain\n",
  193. VAR(ssa_op->op1_use), INSTR(i));
  194. }
  195. if (VAR_NUM(opline->op1.var) != ssa->vars[ssa_op->op1_use].var) {
  196. FAIL("op1 use of " VARFMT " does not match op %d of " INSTRFMT "\n",
  197. VAR(ssa_op->op1_use), VAR_NUM(opline->op1.var), INSTR(i));
  198. }
  199. }
  200. if (ssa_op->op2_use >= 0) {
  201. if (ssa_op->op2_use >= ssa->vars_count) {
  202. FAIL("op2 use %d out of range\n", ssa_op->op2_use);
  203. }
  204. if (!is_in_use_chain(ssa, ssa_op->op2_use, i)) {
  205. FAIL("op2 use of " VARFMT " in " INSTRFMT " not in use chain\n",
  206. VAR(ssa_op->op2_use), INSTR(i));
  207. }
  208. if (VAR_NUM(opline->op2.var) != ssa->vars[ssa_op->op2_use].var) {
  209. FAIL("op2 use of " VARFMT " does not match op %d of " INSTRFMT "\n",
  210. VAR(ssa_op->op2_use), VAR_NUM(opline->op2.var), INSTR(i));
  211. }
  212. }
  213. if (ssa_op->result_use >= 0) {
  214. if (ssa_op->result_use >= ssa->vars_count) {
  215. FAIL("result use %d out of range\n", ssa_op->result_use);
  216. }
  217. if (!is_in_use_chain(ssa, ssa_op->result_use, i)) {
  218. FAIL("result use of " VARFMT " in " INSTRFMT " not in use chain\n",
  219. VAR(ssa_op->result_use), INSTR(i));
  220. }
  221. if (VAR_NUM(opline->result.var) != ssa->vars[ssa_op->result_use].var) {
  222. FAIL("result use of " VARFMT " does not match op %d of " INSTRFMT "\n",
  223. VAR(ssa_op->result_use), VAR_NUM(opline->result.var), INSTR(i));
  224. }
  225. }
  226. if (ssa_op->op1_def >= 0) {
  227. if (ssa_op->op1_def >= ssa->vars_count) {
  228. FAIL("op1 def %d out of range\n", ssa_op->op1_def);
  229. }
  230. if (ssa->vars[ssa_op->op1_def].definition != i) {
  231. FAIL("op1 def of " VARFMT " in " INSTRFMT " invalid\n",
  232. VAR(ssa_op->op1_def), INSTR(i));
  233. }
  234. if (VAR_NUM(opline->op1.var) != ssa->vars[ssa_op->op1_def].var) {
  235. FAIL("op1 def of " VARFMT " does not match op %d of " INSTRFMT "\n",
  236. VAR(ssa_op->op1_def), VAR_NUM(opline->op1.var), INSTR(i));
  237. }
  238. }
  239. if (ssa_op->op2_def >= 0) {
  240. if (ssa_op->op2_def >= ssa->vars_count) {
  241. FAIL("op2 def %d out of range\n", ssa_op->op2_def);
  242. }
  243. if (ssa->vars[ssa_op->op2_def].definition != i) {
  244. FAIL("op2 def of " VARFMT " in " INSTRFMT " invalid\n",
  245. VAR(ssa_op->op2_def), INSTR(i));
  246. }
  247. if (VAR_NUM(opline->op2.var) != ssa->vars[ssa_op->op2_def].var) {
  248. FAIL("op2 def of " VARFMT " does not match op %d of " INSTRFMT "\n",
  249. VAR(ssa_op->op2_def), VAR_NUM(opline->op2.var), INSTR(i));
  250. }
  251. }
  252. if (ssa_op->result_def >= 0) {
  253. if (ssa_op->result_def >= ssa->vars_count) {
  254. FAIL("result def %d out of range\n", ssa_op->result_def);
  255. }
  256. if (ssa->vars[ssa_op->result_def].definition != i) {
  257. FAIL("result def of " VARFMT " in " INSTRFMT " invalid\n",
  258. VAR(ssa_op->result_def), INSTR(i));
  259. }
  260. if (VAR_NUM(opline->result.var) != ssa->vars[ssa_op->result_def].var) {
  261. FAIL("result def of " VARFMT " does not match op %d of " INSTRFMT "\n",
  262. VAR(ssa_op->result_def), VAR_NUM(opline->result.var), INSTR(i));
  263. }
  264. }
  265. } FOREACH_INSTR_NUM_END();
  266. /* Phis */
  267. FOREACH_PHI(phi) {
  268. int source;
  269. FOREACH_PHI_SOURCE(phi, source) {
  270. if (source < 0) {
  271. FAIL(VARFMT " negative source\n", VAR(phi->ssa_var));
  272. }
  273. if (!is_in_phi_use_chain(ssa, source, phi)) {
  274. FAIL(VARFMT " not in phi use chain of %d\n", VAR(phi->ssa_var), source);
  275. }
  276. if (ssa->vars[source].var != ssa->vars[phi->ssa_var].var) {
  277. FAIL(VARFMT " source of phi for " VARFMT "\n", VAR(source), VAR(phi->ssa_var));
  278. }
  279. } FOREACH_PHI_SOURCE_END();
  280. if (ssa->vars[phi->ssa_var].definition_phi != phi) {
  281. FAIL(VARFMT " does not define this phi\n", VAR(phi->ssa_var));
  282. }
  283. } FOREACH_PHI_END();
  284. /* Blocks */
  285. for (i = 0; i < cfg->blocks_count; i++) {
  286. zend_basic_block *block = &cfg->blocks[i];
  287. int *predecessors = &cfg->predecessors[block->predecessor_offset];
  288. int s, j;
  289. if (i != 0 && block->start < (block-1)->start + (block-1)->len) {
  290. FAIL("Block %d start %d smaller previous end %d\n",
  291. i, block->start, (block-1)->start + (block-1)->len);
  292. }
  293. if (i != cfg->blocks_count-1 && block->start + block->len > (block+1)->start) {
  294. FAIL("Block %d end %d greater next start %d\n",
  295. i, block->start + block->len, (block+1)->start);
  296. }
  297. for (j = block->start; j < block->start + block->len; j++) {
  298. if (cfg->map[j] != i) {
  299. FAIL("Instr " INSTRFMT " not associated with block %d\n", INSTR(j), i);
  300. }
  301. }
  302. if (!(block->flags & ZEND_BB_REACHABLE)) {
  303. if (ssa->blocks[i].phis) {
  304. FAIL("Unreachable block %d has phis\n", i);
  305. }
  306. continue;
  307. }
  308. for (s = 0; s < block->successors_count; s++) {
  309. zend_basic_block *next_block;
  310. if (block->successors[s] < 0) {
  311. FAIL("Successor number %d of %d negative", s, i);
  312. }
  313. next_block = &cfg->blocks[block->successors[s]];
  314. if (!(next_block->flags & ZEND_BB_REACHABLE)) {
  315. FAIL("Successor %d of %d not reachable\n", block->successors[s], i);
  316. }
  317. if (!is_in_predecessors(cfg, next_block, i)) {
  318. FAIL("Block %d predecessors missing %d\n", block->successors[s], i);
  319. }
  320. }
  321. for (j = 0; j < block->predecessors_count; j++) {
  322. if (predecessors[j] >= 0) {
  323. int k;
  324. zend_basic_block *prev_block = &cfg->blocks[predecessors[j]];
  325. if (!(prev_block->flags & ZEND_BB_REACHABLE)) {
  326. FAIL("Predecessor %d of %d not reachable\n", predecessors[j], i);
  327. }
  328. if (!is_in_successors(prev_block, i)) {
  329. FAIL("Block %d successors missing %d\n", predecessors[j], i);
  330. }
  331. for (k = 0; k < block->predecessors_count; k++) {
  332. if (k != j && predecessors[k] == predecessors[j]) {
  333. FAIL("Block %d has duplicate predecessor %d\n", i, predecessors[j]);
  334. }
  335. }
  336. }
  337. }
  338. }
  339. return status;
  340. }