sccp.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine, SCCP - Sparse Conditional Constant Propagation |
  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: Nikita Popov <nikic@php.net> |
  16. | Dmitry Stogov <dmitry@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "php.h"
  20. #include "zend_API.h"
  21. #include "zend_exceptions.h"
  22. #include "zend_ini.h"
  23. #include "zend_type_info.h"
  24. #include "Optimizer/zend_optimizer_internal.h"
  25. #include "Optimizer/zend_call_graph.h"
  26. #include "Optimizer/zend_inference.h"
  27. #include "Optimizer/scdf.h"
  28. #include "Optimizer/zend_dump.h"
  29. /* This implements sparse conditional constant propagation (SCCP) based on the SCDF framework. The
  30. * used value lattice is defined as follows:
  31. *
  32. * BOT < {constant values} < TOP
  33. *
  34. * TOP indicates an underdefined value, i.e. that we do not yet know the value of variable.
  35. * BOT indicates an overdefined value, i.e. that we know the variable to be non-constant.
  36. *
  37. * All variables are optimistically initialized to TOP, apart from the implicit variables defined
  38. * at the start of the first block. Note that variables that MAY_BE_REF are *not* initialized to
  39. * BOT. We rely on the fact that any operation resulting in a reference will produce a BOT anyway.
  40. * This is better because such operations might never be reached due to the conditional nature of
  41. * the algorithm.
  42. *
  43. * The meet operation for phi functions is defined as follows:
  44. * BOT + any = BOT
  45. * TOP + any = any
  46. * C_i + C_i = C_i (i.e. two equal constants)
  47. * C_i + C_j = BOT (i.e. two different constants)
  48. *
  49. * When evaluating instructions TOP and BOT are handled as follows:
  50. * a) If any operand is BOT, the result is BOT. The main exception to this is op1 of ASSIGN, which
  51. * is ignored. However, if the op1 MAY_BE_REF we do have to propagate the BOT.
  52. * b) Otherwise, if the instruction can never be evaluated (either in general, or with the
  53. * specific modifiers) the result is BOT.
  54. * c) Otherwise, if any operand is TOP, the result is TOP.
  55. * d) Otherwise (at this point all operands are known and constant), if we can compute the result
  56. * for these specific constants (without throwing notices or similar) then that is the result.
  57. * e) Otherwise the result is BOT.
  58. *
  59. * It is sometimes possible to determine a result even if one argument is TOP / BOT, e.g. for things
  60. * like BOT*0. Right now we don't bother with this -- the only thing that is done is evaluating
  61. * TYPE_CHECKS based on the type information.
  62. *
  63. * Feasible successors for conditional branches are determined as follows:
  64. * a) If we don't support the branch type or branch on BOT, all successors are feasible.
  65. * b) Otherwise, if we branch on TOP none of the successors are feasible.
  66. * c) Otherwise (we branch on a constant), the feasible successors are marked based on the constant
  67. * (usually only one successor will be feasible).
  68. *
  69. * The original SCCP algorithm is extended with ability to propagate constant array
  70. * elements and object properties. The extension is based on a variation of Array
  71. * SSA form and its application to Spare Constant Propagation, described at
  72. * "Array SSA Form" by Vivek Sarkar, Kathleen Knobe and Stephen Fink in chapter
  73. * 16 of the SSA book.
  74. */
  75. #define SCP_DEBUG 0
  76. typedef struct _sccp_ctx {
  77. scdf_ctx scdf;
  78. zend_call_info **call_map;
  79. zval *values;
  80. zval top;
  81. zval bot;
  82. } sccp_ctx;
  83. #define TOP ((zend_uchar)-1)
  84. #define BOT ((zend_uchar)-2)
  85. #define PARTIAL_ARRAY ((zend_uchar)-3)
  86. #define PARTIAL_OBJECT ((zend_uchar)-4)
  87. #define IS_TOP(zv) (Z_TYPE_P(zv) == TOP)
  88. #define IS_BOT(zv) (Z_TYPE_P(zv) == BOT)
  89. #define IS_PARTIAL_ARRAY(zv) (Z_TYPE_P(zv) == PARTIAL_ARRAY)
  90. #define IS_PARTIAL_OBJECT(zv) (Z_TYPE_P(zv) == PARTIAL_OBJECT)
  91. #define MAKE_PARTIAL_ARRAY(zv) (Z_TYPE_INFO_P(zv) = PARTIAL_ARRAY | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
  92. #define MAKE_PARTIAL_OBJECT(zv) (Z_TYPE_INFO_P(zv) = PARTIAL_OBJECT | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
  93. #define MAKE_TOP(zv) (Z_TYPE_INFO_P(zv) = TOP)
  94. #define MAKE_BOT(zv) (Z_TYPE_INFO_P(zv) = BOT)
  95. static void scp_dump_value(zval *zv) {
  96. if (IS_TOP(zv)) {
  97. fprintf(stderr, " top");
  98. } else if (IS_BOT(zv)) {
  99. fprintf(stderr, " bot");
  100. } else if (Z_TYPE_P(zv) == IS_ARRAY || IS_PARTIAL_ARRAY(zv)) {
  101. fprintf(stderr, " %s[", IS_PARTIAL_ARRAY(zv) ? "partial " : "");
  102. zend_dump_ht(Z_ARRVAL_P(zv));
  103. fprintf(stderr, "]");
  104. } else if (IS_PARTIAL_OBJECT(zv)) {
  105. fprintf(stderr, " {");
  106. zend_dump_ht(Z_ARRVAL_P(zv));
  107. fprintf(stderr, "}");
  108. } else {
  109. zend_dump_const(zv);
  110. }
  111. }
  112. static void empty_partial_array(zval *zv)
  113. {
  114. MAKE_PARTIAL_ARRAY(zv);
  115. Z_ARR_P(zv) = zend_new_array(8);
  116. }
  117. static void dup_partial_array(zval *dst, zval *src)
  118. {
  119. MAKE_PARTIAL_ARRAY(dst);
  120. Z_ARR_P(dst) = zend_array_dup(Z_ARR_P(src));
  121. }
  122. static void empty_partial_object(zval *zv)
  123. {
  124. MAKE_PARTIAL_OBJECT(zv);
  125. Z_ARR_P(zv) = zend_new_array(8);
  126. }
  127. static void dup_partial_object(zval *dst, zval *src)
  128. {
  129. MAKE_PARTIAL_OBJECT(dst);
  130. Z_ARR_P(dst) = zend_array_dup(Z_ARR_P(src));
  131. }
  132. static inline bool value_known(zval *zv) {
  133. return !IS_TOP(zv) && !IS_BOT(zv);
  134. }
  135. /* Sets new value for variable and ensures that it is lower or equal
  136. * the previous one in the constant propagation lattice. */
  137. static void set_value(scdf_ctx *scdf, sccp_ctx *ctx, int var, zval *new) {
  138. zval *value = &ctx->values[var];
  139. if (IS_BOT(value) || IS_TOP(new)) {
  140. return;
  141. }
  142. #if SCP_DEBUG
  143. fprintf(stderr, "Lowering #%d.", var);
  144. zend_dump_var(scdf->op_array, IS_CV, scdf->ssa->vars[var].var);
  145. fprintf(stderr, " from");
  146. scp_dump_value(value);
  147. fprintf(stderr, " to");
  148. scp_dump_value(new);
  149. fprintf(stderr, "\n");
  150. #endif
  151. if (IS_TOP(value) || IS_BOT(new)) {
  152. zval_ptr_dtor_nogc(value);
  153. ZVAL_COPY(value, new);
  154. scdf_add_to_worklist(scdf, var);
  155. return;
  156. }
  157. /* Always replace PARTIAL_(ARRAY|OBJECT), as new maybe changed by join_partial_(arrays|object) */
  158. if (IS_PARTIAL_ARRAY(new) || IS_PARTIAL_OBJECT(new)) {
  159. if (Z_TYPE_P(value) != Z_TYPE_P(new)
  160. || zend_hash_num_elements(Z_ARR_P(new)) != zend_hash_num_elements(Z_ARR_P(value))) {
  161. zval_ptr_dtor_nogc(value);
  162. ZVAL_COPY(value, new);
  163. scdf_add_to_worklist(scdf, var);
  164. }
  165. return;
  166. }
  167. #if ZEND_DEBUG
  168. ZEND_ASSERT(zend_is_identical(value, new) ||
  169. (Z_TYPE_P(value) == IS_DOUBLE && Z_TYPE_P(new) == IS_DOUBLE && isnan(Z_DVAL_P(value)) && isnan(Z_DVAL_P(new))));
  170. #endif
  171. }
  172. static zval *get_op1_value(sccp_ctx *ctx, zend_op *opline, zend_ssa_op *ssa_op) {
  173. if (opline->op1_type == IS_CONST) {
  174. return CT_CONSTANT_EX(ctx->scdf.op_array, opline->op1.constant);
  175. } else if (ssa_op->op1_use != -1) {
  176. return &ctx->values[ssa_op->op1_use];
  177. } else {
  178. return NULL;
  179. }
  180. }
  181. static zval *get_op2_value(sccp_ctx *ctx, zend_op *opline, zend_ssa_op *ssa_op) {
  182. if (opline->op2_type == IS_CONST) {
  183. return CT_CONSTANT_EX(ctx->scdf.op_array, opline->op2.constant);
  184. } else if (ssa_op->op2_use != -1) {
  185. return &ctx->values[ssa_op->op2_use];
  186. } else {
  187. return NULL;
  188. }
  189. }
  190. static bool can_replace_op1(
  191. const zend_op_array *op_array, zend_op *opline, zend_ssa_op *ssa_op) {
  192. switch (opline->opcode) {
  193. case ZEND_PRE_INC:
  194. case ZEND_PRE_DEC:
  195. case ZEND_PRE_INC_OBJ:
  196. case ZEND_PRE_DEC_OBJ:
  197. case ZEND_POST_INC:
  198. case ZEND_POST_DEC:
  199. case ZEND_POST_INC_OBJ:
  200. case ZEND_POST_DEC_OBJ:
  201. case ZEND_ASSIGN:
  202. case ZEND_ASSIGN_REF:
  203. case ZEND_ASSIGN_DIM:
  204. case ZEND_ASSIGN_OBJ:
  205. case ZEND_ASSIGN_OBJ_REF:
  206. case ZEND_ASSIGN_OP:
  207. case ZEND_ASSIGN_DIM_OP:
  208. case ZEND_ASSIGN_OBJ_OP:
  209. case ZEND_ASSIGN_STATIC_PROP_OP:
  210. case ZEND_FETCH_DIM_W:
  211. case ZEND_FETCH_DIM_RW:
  212. case ZEND_FETCH_DIM_UNSET:
  213. case ZEND_FETCH_DIM_FUNC_ARG:
  214. case ZEND_FETCH_OBJ_W:
  215. case ZEND_FETCH_OBJ_RW:
  216. case ZEND_FETCH_OBJ_UNSET:
  217. case ZEND_FETCH_OBJ_FUNC_ARG:
  218. case ZEND_FETCH_LIST_W:
  219. case ZEND_UNSET_DIM:
  220. case ZEND_UNSET_OBJ:
  221. case ZEND_SEND_REF:
  222. case ZEND_SEND_VAR_EX:
  223. case ZEND_SEND_FUNC_ARG:
  224. case ZEND_SEND_UNPACK:
  225. case ZEND_SEND_ARRAY:
  226. case ZEND_SEND_USER:
  227. case ZEND_FE_RESET_RW:
  228. return 0;
  229. /* Do not accept CONST */
  230. case ZEND_ROPE_ADD:
  231. case ZEND_ROPE_END:
  232. case ZEND_BIND_STATIC:
  233. case ZEND_BIND_GLOBAL:
  234. case ZEND_MAKE_REF:
  235. case ZEND_UNSET_CV:
  236. case ZEND_ISSET_ISEMPTY_CV:
  237. return 0;
  238. case ZEND_INIT_ARRAY:
  239. case ZEND_ADD_ARRAY_ELEMENT:
  240. return !(opline->extended_value & ZEND_ARRAY_ELEMENT_REF);
  241. case ZEND_YIELD:
  242. return !(op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE);
  243. case ZEND_VERIFY_RETURN_TYPE:
  244. // TODO: This would require a non-local change ???
  245. return 0;
  246. case ZEND_OP_DATA:
  247. return (opline - 1)->opcode != ZEND_ASSIGN_OBJ_REF &&
  248. (opline - 1)->opcode != ZEND_ASSIGN_STATIC_PROP_REF;
  249. default:
  250. if (ssa_op->op1_def != -1) {
  251. ZEND_UNREACHABLE();
  252. return 0;
  253. }
  254. }
  255. return 1;
  256. }
  257. static bool can_replace_op2(
  258. const zend_op_array *op_array, zend_op *opline, zend_ssa_op *ssa_op) {
  259. switch (opline->opcode) {
  260. /* Do not accept CONST */
  261. case ZEND_DECLARE_CLASS_DELAYED:
  262. case ZEND_BIND_LEXICAL:
  263. case ZEND_FE_FETCH_R:
  264. case ZEND_FE_FETCH_RW:
  265. return 0;
  266. }
  267. return 1;
  268. }
  269. static bool try_replace_op1(
  270. sccp_ctx *ctx, zend_op *opline, zend_ssa_op *ssa_op, int var, zval *value) {
  271. if (ssa_op->op1_use == var && can_replace_op1(ctx->scdf.op_array, opline, ssa_op)) {
  272. zval zv;
  273. ZVAL_COPY(&zv, value);
  274. if (zend_optimizer_update_op1_const(ctx->scdf.op_array, opline, &zv)) {
  275. return 1;
  276. } else {
  277. // TODO: check the following special cases ???
  278. switch (opline->opcode) {
  279. case ZEND_CASE:
  280. opline->opcode = ZEND_IS_EQUAL;
  281. goto replace_op1_simple;
  282. case ZEND_CASE_STRICT:
  283. opline->opcode = ZEND_IS_IDENTICAL;
  284. goto replace_op1_simple;
  285. case ZEND_FETCH_LIST_R:
  286. case ZEND_SWITCH_STRING:
  287. case ZEND_SWITCH_LONG:
  288. case ZEND_MATCH:
  289. replace_op1_simple:
  290. if (Z_TYPE(zv) == IS_STRING) {
  291. zend_string_hash_val(Z_STR(zv));
  292. }
  293. opline->op1.constant = zend_optimizer_add_literal(ctx->scdf.op_array, &zv);
  294. opline->op1_type = IS_CONST;
  295. return 1;
  296. case ZEND_INSTANCEOF:
  297. zval_ptr_dtor_nogc(&zv);
  298. ZVAL_FALSE(&zv);
  299. opline->opcode = ZEND_QM_ASSIGN;
  300. opline->op1_type = IS_CONST;
  301. opline->op1.constant = zend_optimizer_add_literal(ctx->scdf.op_array, &zv);
  302. opline->op2_type = IS_UNUSED;
  303. if (ssa_op->op2_use >= 0) {
  304. ZEND_ASSERT(ssa_op->op2_def == -1);
  305. zend_ssa_unlink_use_chain(ctx->scdf.ssa, ssa_op - ctx->scdf.ssa->ops, ssa_op->op2_use);
  306. ssa_op->op2_use = -1;
  307. ssa_op->op2_use_chain = -1;
  308. }
  309. return 1;
  310. default:
  311. break;
  312. }
  313. zval_ptr_dtor_nogc(&zv);
  314. }
  315. }
  316. return 0;
  317. }
  318. static bool try_replace_op2(
  319. sccp_ctx *ctx, zend_op *opline, zend_ssa_op *ssa_op, int var, zval *value) {
  320. if (ssa_op->op2_use == var && can_replace_op2(ctx->scdf.op_array, opline, ssa_op)) {
  321. zval zv;
  322. ZVAL_COPY(&zv, value);
  323. if (zend_optimizer_update_op2_const(ctx->scdf.op_array, opline, &zv)) {
  324. return 1;
  325. } else {
  326. switch (opline->opcode) {
  327. case ZEND_FETCH_CLASS:
  328. if (Z_TYPE(zv) == IS_STRING) {
  329. ZEND_ASSERT((opline + 1)->opcode == ZEND_INSTANCEOF);
  330. ZEND_ASSERT(ssa_op->result_def == (ssa_op + 1)->op2_use);
  331. if (zend_optimizer_update_op2_const(ctx->scdf.op_array, opline + 1, &zv)) {
  332. zend_ssa_op *next_op = ssa_op + 1;
  333. zend_ssa_unlink_use_chain(ctx->scdf.ssa, next_op - ctx->scdf.ssa->ops, next_op->op2_use);
  334. next_op->op2_use = -1;
  335. next_op->op2_use_chain = -1;
  336. zend_ssa_remove_result_def(ctx->scdf.ssa, ssa_op);
  337. MAKE_NOP(opline);
  338. return 1;
  339. }
  340. }
  341. default:
  342. break;
  343. }
  344. zval_ptr_dtor_nogc(&zv);
  345. }
  346. }
  347. return 0;
  348. }
  349. static inline int ct_eval_binary_op(zval *result, zend_uchar binop, zval *op1, zval *op2) {
  350. /* TODO: We could implement support for evaluation of + on partial arrays. */
  351. if (IS_PARTIAL_ARRAY(op1) || IS_PARTIAL_ARRAY(op2)) {
  352. return FAILURE;
  353. }
  354. return zend_optimizer_eval_binary_op(result, binop, op1, op2);
  355. }
  356. static inline int ct_eval_bool_cast(zval *result, zval *op) {
  357. if (IS_PARTIAL_ARRAY(op)) {
  358. if (zend_hash_num_elements(Z_ARRVAL_P(op)) == 0) {
  359. /* An empty partial array may be non-empty at runtime, we don't know whether the
  360. * result will be true or false. */
  361. return FAILURE;
  362. }
  363. ZVAL_TRUE(result);
  364. return SUCCESS;
  365. }
  366. ZVAL_BOOL(result, zend_is_true(op));
  367. return SUCCESS;
  368. }
  369. static inline int zval_to_string_offset(zend_long *result, zval *op) {
  370. switch (Z_TYPE_P(op)) {
  371. case IS_LONG:
  372. *result = Z_LVAL_P(op);
  373. return SUCCESS;
  374. case IS_STRING:
  375. if (IS_LONG == is_numeric_string(
  376. Z_STRVAL_P(op), Z_STRLEN_P(op), result, NULL, 0)) {
  377. return SUCCESS;
  378. }
  379. return FAILURE;
  380. default:
  381. return FAILURE;
  382. }
  383. }
  384. static inline int fetch_array_elem(zval **result, zval *op1, zval *op2) {
  385. switch (Z_TYPE_P(op2)) {
  386. case IS_NULL:
  387. *result = zend_hash_find(Z_ARR_P(op1), ZSTR_EMPTY_ALLOC());
  388. return SUCCESS;
  389. case IS_FALSE:
  390. *result = zend_hash_index_find(Z_ARR_P(op1), 0);
  391. return SUCCESS;
  392. case IS_TRUE:
  393. *result = zend_hash_index_find(Z_ARR_P(op1), 1);
  394. return SUCCESS;
  395. case IS_LONG:
  396. *result = zend_hash_index_find(Z_ARR_P(op1), Z_LVAL_P(op2));
  397. return SUCCESS;
  398. case IS_DOUBLE: {
  399. zend_long lval = zend_dval_to_lval(Z_DVAL_P(op2));
  400. if (!zend_is_long_compatible(Z_DVAL_P(op2), lval)) {
  401. return FAILURE;
  402. }
  403. *result = zend_hash_index_find(Z_ARR_P(op1), lval);
  404. return SUCCESS;
  405. }
  406. case IS_STRING:
  407. *result = zend_symtable_find(Z_ARR_P(op1), Z_STR_P(op2));
  408. return SUCCESS;
  409. default:
  410. return FAILURE;
  411. }
  412. }
  413. static inline int ct_eval_fetch_dim(zval *result, zval *op1, zval *op2, int support_strings) {
  414. if (Z_TYPE_P(op1) == IS_ARRAY || IS_PARTIAL_ARRAY(op1)) {
  415. zval *value;
  416. if (fetch_array_elem(&value, op1, op2) == SUCCESS && value && !IS_BOT(value)) {
  417. ZVAL_COPY(result, value);
  418. return SUCCESS;
  419. }
  420. } else if (support_strings && Z_TYPE_P(op1) == IS_STRING) {
  421. zend_long index;
  422. if (zval_to_string_offset(&index, op2) == FAILURE) {
  423. return FAILURE;
  424. }
  425. if (index >= 0 && index < Z_STRLEN_P(op1)) {
  426. ZVAL_STR(result, zend_string_init(&Z_STRVAL_P(op1)[index], 1, 0));
  427. return SUCCESS;
  428. }
  429. }
  430. return FAILURE;
  431. }
  432. /* op1 may be NULL here to indicate an unset value */
  433. static inline int ct_eval_isset_isempty(zval *result, uint32_t extended_value, zval *op1) {
  434. zval zv;
  435. if (!(extended_value & ZEND_ISEMPTY)) {
  436. ZVAL_BOOL(result, op1 && Z_TYPE_P(op1) != IS_NULL);
  437. return SUCCESS;
  438. } else if (!op1) {
  439. ZVAL_TRUE(result);
  440. return SUCCESS;
  441. } else if (ct_eval_bool_cast(&zv, op1) == SUCCESS) {
  442. ZVAL_BOOL(result, Z_TYPE(zv) == IS_FALSE);
  443. return SUCCESS;
  444. } else {
  445. return FAILURE;
  446. }
  447. }
  448. static inline int ct_eval_isset_dim(zval *result, uint32_t extended_value, zval *op1, zval *op2) {
  449. if (Z_TYPE_P(op1) == IS_ARRAY || IS_PARTIAL_ARRAY(op1)) {
  450. zval *value;
  451. if (fetch_array_elem(&value, op1, op2) == FAILURE) {
  452. return FAILURE;
  453. }
  454. if (IS_PARTIAL_ARRAY(op1) && (!value || IS_BOT(value))) {
  455. return FAILURE;
  456. }
  457. return ct_eval_isset_isempty(result, extended_value, value);
  458. } else if (Z_TYPE_P(op1) == IS_STRING) {
  459. // TODO
  460. return FAILURE;
  461. } else {
  462. ZVAL_BOOL(result, (extended_value & ZEND_ISEMPTY));
  463. return SUCCESS;
  464. }
  465. }
  466. static inline int ct_eval_del_array_elem(zval *result, zval *key) {
  467. ZEND_ASSERT(IS_PARTIAL_ARRAY(result));
  468. switch (Z_TYPE_P(key)) {
  469. case IS_NULL:
  470. zend_hash_del(Z_ARR_P(result), ZSTR_EMPTY_ALLOC());
  471. break;
  472. case IS_FALSE:
  473. zend_hash_index_del(Z_ARR_P(result), 0);
  474. break;
  475. case IS_TRUE:
  476. zend_hash_index_del(Z_ARR_P(result), 1);
  477. break;
  478. case IS_LONG:
  479. zend_hash_index_del(Z_ARR_P(result), Z_LVAL_P(key));
  480. break;
  481. case IS_DOUBLE: {
  482. zend_long lval = zend_dval_to_lval(Z_DVAL_P(key));
  483. if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
  484. return FAILURE;
  485. }
  486. zend_hash_index_del(Z_ARR_P(result), lval);
  487. break;
  488. }
  489. case IS_STRING:
  490. zend_symtable_del(Z_ARR_P(result), Z_STR_P(key));
  491. break;
  492. default:
  493. return FAILURE;
  494. }
  495. return SUCCESS;
  496. }
  497. static inline int ct_eval_add_array_elem(zval *result, zval *value, zval *key) {
  498. if (!key) {
  499. SEPARATE_ARRAY(result);
  500. if ((value = zend_hash_next_index_insert(Z_ARR_P(result), value))) {
  501. Z_TRY_ADDREF_P(value);
  502. return SUCCESS;
  503. }
  504. return FAILURE;
  505. }
  506. switch (Z_TYPE_P(key)) {
  507. case IS_NULL:
  508. SEPARATE_ARRAY(result);
  509. value = zend_hash_update(Z_ARR_P(result), ZSTR_EMPTY_ALLOC(), value);
  510. break;
  511. case IS_FALSE:
  512. SEPARATE_ARRAY(result);
  513. value = zend_hash_index_update(Z_ARR_P(result), 0, value);
  514. break;
  515. case IS_TRUE:
  516. SEPARATE_ARRAY(result);
  517. value = zend_hash_index_update(Z_ARR_P(result), 1, value);
  518. break;
  519. case IS_LONG:
  520. SEPARATE_ARRAY(result);
  521. value = zend_hash_index_update(Z_ARR_P(result), Z_LVAL_P(key), value);
  522. break;
  523. case IS_DOUBLE: {
  524. zend_long lval = zend_dval_to_lval(Z_DVAL_P(key));
  525. if (!zend_is_long_compatible(Z_DVAL_P(key), lval)) {
  526. return FAILURE;
  527. }
  528. SEPARATE_ARRAY(result);
  529. value = zend_hash_index_update(
  530. Z_ARR_P(result), lval, value);
  531. break;
  532. }
  533. case IS_STRING:
  534. SEPARATE_ARRAY(result);
  535. value = zend_symtable_update(Z_ARR_P(result), Z_STR_P(key), value);
  536. break;
  537. default:
  538. return FAILURE;
  539. }
  540. Z_TRY_ADDREF_P(value);
  541. return SUCCESS;
  542. }
  543. static inline int ct_eval_add_array_unpack(zval *result, zval *array) {
  544. zend_string *key;
  545. zval *value;
  546. if (Z_TYPE_P(array) != IS_ARRAY) {
  547. return FAILURE;
  548. }
  549. SEPARATE_ARRAY(result);
  550. ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(array), key, value) {
  551. if (key) {
  552. value = zend_hash_update(Z_ARR_P(result), key, value);
  553. } else {
  554. value = zend_hash_next_index_insert(Z_ARR_P(result), value);
  555. }
  556. if (!value) {
  557. return FAILURE;
  558. }
  559. Z_TRY_ADDREF_P(value);
  560. } ZEND_HASH_FOREACH_END();
  561. return SUCCESS;
  562. }
  563. static inline int ct_eval_assign_dim(zval *result, zval *value, zval *key) {
  564. switch (Z_TYPE_P(result)) {
  565. case IS_NULL:
  566. case IS_FALSE:
  567. array_init(result);
  568. ZEND_FALLTHROUGH;
  569. case IS_ARRAY:
  570. case PARTIAL_ARRAY:
  571. return ct_eval_add_array_elem(result, value, key);
  572. case IS_STRING:
  573. // TODO Before enabling this case, make sure ARRAY_DIM result op is correct
  574. #if 0
  575. zend_long index;
  576. zend_string *new_str, *value_str;
  577. if (!key || Z_TYPE_P(value) == IS_ARRAY
  578. || zval_to_string_offset(&index, key) == FAILURE || index < 0) {
  579. return FAILURE;
  580. }
  581. if (index >= Z_STRLEN_P(result)) {
  582. new_str = zend_string_alloc(index + 1, 0);
  583. memcpy(ZSTR_VAL(new_str), Z_STRVAL_P(result), Z_STRLEN_P(result));
  584. memset(ZSTR_VAL(new_str) + Z_STRLEN_P(result), ' ', index - Z_STRLEN_P(result));
  585. ZSTR_VAL(new_str)[index + 1] = 0;
  586. } else {
  587. new_str = zend_string_init(Z_STRVAL_P(result), Z_STRLEN_P(result), 0);
  588. }
  589. value_str = zval_get_string(value);
  590. ZVAL_STR(result, new_str);
  591. Z_STRVAL_P(result)[index] = ZSTR_VAL(value_str)[0];
  592. zend_string_release_ex(value_str, 0);
  593. #endif
  594. return FAILURE;
  595. default:
  596. return FAILURE;
  597. }
  598. }
  599. static inline int fetch_obj_prop(zval **result, zval *op1, zval *op2) {
  600. switch (Z_TYPE_P(op2)) {
  601. case IS_STRING:
  602. *result = zend_symtable_find(Z_ARR_P(op1), Z_STR_P(op2));
  603. return SUCCESS;
  604. default:
  605. return FAILURE;
  606. }
  607. }
  608. static inline int ct_eval_fetch_obj(zval *result, zval *op1, zval *op2) {
  609. if (IS_PARTIAL_OBJECT(op1)) {
  610. zval *value;
  611. if (fetch_obj_prop(&value, op1, op2) == SUCCESS && value && !IS_BOT(value)) {
  612. ZVAL_COPY(result, value);
  613. return SUCCESS;
  614. }
  615. }
  616. return FAILURE;
  617. }
  618. static inline int ct_eval_isset_obj(zval *result, uint32_t extended_value, zval *op1, zval *op2) {
  619. if (IS_PARTIAL_OBJECT(op1)) {
  620. zval *value;
  621. if (fetch_obj_prop(&value, op1, op2) == FAILURE) {
  622. return FAILURE;
  623. }
  624. if (!value || IS_BOT(value)) {
  625. return FAILURE;
  626. }
  627. return ct_eval_isset_isempty(result, extended_value, value);
  628. } else {
  629. ZVAL_BOOL(result, (extended_value & ZEND_ISEMPTY));
  630. return SUCCESS;
  631. }
  632. }
  633. static inline int ct_eval_del_obj_prop(zval *result, zval *key) {
  634. ZEND_ASSERT(IS_PARTIAL_OBJECT(result));
  635. switch (Z_TYPE_P(key)) {
  636. case IS_STRING:
  637. zend_symtable_del(Z_ARR_P(result), Z_STR_P(key));
  638. break;
  639. default:
  640. return FAILURE;
  641. }
  642. return SUCCESS;
  643. }
  644. static inline int ct_eval_add_obj_prop(zval *result, zval *value, zval *key) {
  645. switch (Z_TYPE_P(key)) {
  646. case IS_STRING:
  647. value = zend_symtable_update(Z_ARR_P(result), Z_STR_P(key), value);
  648. break;
  649. default:
  650. return FAILURE;
  651. }
  652. Z_TRY_ADDREF_P(value);
  653. return SUCCESS;
  654. }
  655. static inline int ct_eval_assign_obj(zval *result, zval *value, zval *key) {
  656. switch (Z_TYPE_P(result)) {
  657. case IS_NULL:
  658. case IS_FALSE:
  659. empty_partial_object(result);
  660. ZEND_FALLTHROUGH;
  661. case PARTIAL_OBJECT:
  662. return ct_eval_add_obj_prop(result, value, key);
  663. default:
  664. return FAILURE;
  665. }
  666. }
  667. static inline int ct_eval_incdec(zval *result, zend_uchar opcode, zval *op1) {
  668. if (Z_TYPE_P(op1) == IS_ARRAY || IS_PARTIAL_ARRAY(op1)) {
  669. return FAILURE;
  670. }
  671. ZVAL_COPY(result, op1);
  672. if (opcode == ZEND_PRE_INC
  673. || opcode == ZEND_POST_INC
  674. || opcode == ZEND_PRE_INC_OBJ
  675. || opcode == ZEND_POST_INC_OBJ) {
  676. increment_function(result);
  677. } else {
  678. decrement_function(result);
  679. }
  680. return SUCCESS;
  681. }
  682. static inline void ct_eval_type_check(zval *result, uint32_t type_mask, zval *op1) {
  683. uint32_t type = Z_TYPE_P(op1);
  684. if (type == PARTIAL_ARRAY) {
  685. type = IS_ARRAY;
  686. } else if (type == PARTIAL_OBJECT) {
  687. type = IS_OBJECT;
  688. }
  689. ZVAL_BOOL(result, (type_mask >> type) & 1);
  690. }
  691. static inline int ct_eval_in_array(zval *result, uint32_t extended_value, zval *op1, zval *op2) {
  692. HashTable *ht;
  693. bool res;
  694. if (Z_TYPE_P(op2) != IS_ARRAY) {
  695. return FAILURE;
  696. }
  697. ht = Z_ARRVAL_P(op2);
  698. if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
  699. res = zend_hash_exists(ht, Z_STR_P(op1));
  700. } else if (extended_value) {
  701. if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
  702. res = zend_hash_index_exists(ht, Z_LVAL_P(op1));
  703. } else {
  704. res = 0;
  705. }
  706. } else if (Z_TYPE_P(op1) <= IS_FALSE) {
  707. res = zend_hash_exists(ht, ZSTR_EMPTY_ALLOC());
  708. } else {
  709. zend_string *key;
  710. zval key_tmp;
  711. res = 0;
  712. ZEND_HASH_FOREACH_STR_KEY(ht, key) {
  713. ZVAL_STR(&key_tmp, key);
  714. if (zend_compare(op1, &key_tmp) == 0) {
  715. res = 1;
  716. break;
  717. }
  718. } ZEND_HASH_FOREACH_END();
  719. }
  720. ZVAL_BOOL(result, res);
  721. return SUCCESS;
  722. }
  723. static inline int ct_eval_array_key_exists(zval *result, zval *op1, zval *op2) {
  724. zval *value;
  725. if (Z_TYPE_P(op2) != IS_ARRAY && !IS_PARTIAL_ARRAY(op2)) {
  726. return FAILURE;
  727. }
  728. if (Z_TYPE_P(op1) != IS_STRING && Z_TYPE_P(op1) != IS_LONG && Z_TYPE_P(op1) != IS_NULL) {
  729. return FAILURE;
  730. }
  731. if (fetch_array_elem(&value, op2, op1) == FAILURE) {
  732. return FAILURE;
  733. }
  734. if (IS_PARTIAL_ARRAY(op2) && (!value || IS_BOT(value))) {
  735. return FAILURE;
  736. }
  737. ZVAL_BOOL(result, value != NULL);
  738. return SUCCESS;
  739. }
  740. static bool can_ct_eval_func_call(zend_string *name, uint32_t num_args, zval **args) {
  741. /* Functions in this list must always produce the same result for the same arguments,
  742. * and have no dependence on global state (such as locales). It is okay if they throw
  743. * or warn on invalid arguments, as we detect this and will discard the evaluation result. */
  744. if (false
  745. || zend_string_equals_literal(name, "array_diff")
  746. || zend_string_equals_literal(name, "array_diff_assoc")
  747. || zend_string_equals_literal(name, "array_diff_key")
  748. || zend_string_equals_literal(name, "array_flip")
  749. || zend_string_equals_literal(name, "array_is_list")
  750. || zend_string_equals_literal(name, "array_key_exists")
  751. || zend_string_equals_literal(name, "array_keys")
  752. || zend_string_equals_literal(name, "array_merge")
  753. || zend_string_equals_literal(name, "array_merge_recursive")
  754. || zend_string_equals_literal(name, "array_replace")
  755. || zend_string_equals_literal(name, "array_replace_recursive")
  756. || zend_string_equals_literal(name, "array_unique")
  757. || zend_string_equals_literal(name, "array_values")
  758. || zend_string_equals_literal(name, "base64_decode")
  759. || zend_string_equals_literal(name, "base64_encode")
  760. #ifndef ZEND_WIN32
  761. /* On Windows this function may be code page dependent. */
  762. || zend_string_equals_literal(name, "dirname")
  763. #endif
  764. || zend_string_equals_literal(name, "explode")
  765. || zend_string_equals_literal(name, "imagetypes")
  766. || zend_string_equals_literal(name, "in_array")
  767. || zend_string_equals_literal(name, "implode")
  768. || zend_string_equals_literal(name, "ltrim")
  769. || zend_string_equals_literal(name, "php_sapi_name")
  770. || zend_string_equals_literal(name, "php_uname")
  771. || zend_string_equals_literal(name, "phpversion")
  772. || zend_string_equals_literal(name, "pow")
  773. || zend_string_equals_literal(name, "preg_quote")
  774. || zend_string_equals_literal(name, "rawurldecode")
  775. || zend_string_equals_literal(name, "rawurlencode")
  776. || zend_string_equals_literal(name, "rtrim")
  777. || zend_string_equals_literal(name, "serialize")
  778. || zend_string_equals_literal(name, "str_contains")
  779. || zend_string_equals_literal(name, "str_ends_with")
  780. || zend_string_equals_literal(name, "str_replace")
  781. || zend_string_equals_literal(name, "str_split")
  782. || zend_string_equals_literal(name, "str_starts_with")
  783. || zend_string_equals_literal(name, "strpos")
  784. || zend_string_equals_literal(name, "strstr")
  785. || zend_string_equals_literal(name, "substr")
  786. || zend_string_equals_literal(name, "trim")
  787. || zend_string_equals_literal(name, "urldecode")
  788. || zend_string_equals_literal(name, "urlencode")
  789. || zend_string_equals_literal(name, "version_compare")
  790. ) {
  791. return true;
  792. }
  793. if (num_args == 2) {
  794. if (zend_string_equals_literal(name, "str_repeat")) {
  795. /* Avoid creating overly large strings at compile-time. */
  796. bool overflow;
  797. return Z_TYPE_P(args[0]) == IS_STRING
  798. && Z_TYPE_P(args[1]) == IS_LONG
  799. && zend_safe_address(Z_STRLEN_P(args[0]), Z_LVAL_P(args[1]), 0, &overflow) < 64 * 1024
  800. && !overflow;
  801. }
  802. return false;
  803. }
  804. return false;
  805. }
  806. /* The functions chosen here are simple to implement and either likely to affect a branch,
  807. * or just happened to be commonly used with constant operands in WP (need to test other
  808. * applications as well, of course). */
  809. static inline int ct_eval_func_call(
  810. zend_op_array *op_array, zval *result, zend_string *name, uint32_t num_args, zval **args) {
  811. uint32_t i;
  812. zend_function *func = zend_hash_find_ptr(CG(function_table), name);
  813. if (!func || func->type != ZEND_INTERNAL_FUNCTION) {
  814. return FAILURE;
  815. }
  816. if (num_args == 1) {
  817. /* Handle a few functions for which we manually implement evaluation here. */
  818. if (zend_string_equals_literal(name, "chr")) {
  819. zend_long c;
  820. if (Z_TYPE_P(args[0]) != IS_LONG) {
  821. return FAILURE;
  822. }
  823. c = Z_LVAL_P(args[0]) & 0xff;
  824. ZVAL_CHAR(result, c);
  825. return SUCCESS;
  826. } else if (zend_string_equals_literal(name, "count")) {
  827. if (Z_TYPE_P(args[0]) != IS_ARRAY) {
  828. return FAILURE;
  829. }
  830. ZVAL_LONG(result, zend_hash_num_elements(Z_ARRVAL_P(args[0])));
  831. return SUCCESS;
  832. } else if (zend_string_equals_literal(name, "ini_get")) {
  833. zend_ini_entry *ini_entry;
  834. if (Z_TYPE_P(args[0]) != IS_STRING) {
  835. return FAILURE;
  836. }
  837. ini_entry = zend_hash_find_ptr(EG(ini_directives), Z_STR_P(args[0]));
  838. if (!ini_entry) {
  839. if (PG(enable_dl)) {
  840. return FAILURE;
  841. }
  842. ZVAL_FALSE(result);
  843. } else if (ini_entry->modifiable != ZEND_INI_SYSTEM) {
  844. return FAILURE;
  845. } else if (ini_entry->value) {
  846. ZVAL_STR_COPY(result, ini_entry->value);
  847. } else {
  848. ZVAL_EMPTY_STRING(result);
  849. }
  850. return SUCCESS;
  851. }
  852. }
  853. if (!can_ct_eval_func_call(name, num_args, args)) {
  854. return FAILURE;
  855. }
  856. zend_execute_data *prev_execute_data = EG(current_execute_data);
  857. zend_execute_data *execute_data, dummy_frame;
  858. zend_op dummy_opline;
  859. /* Add a dummy frame to get the correct strict_types behavior. */
  860. memset(&dummy_frame, 0, sizeof(zend_execute_data));
  861. memset(&dummy_opline, 0, sizeof(zend_op));
  862. dummy_frame.func = (zend_function *) op_array;
  863. dummy_frame.opline = &dummy_opline;
  864. dummy_opline.opcode = ZEND_DO_FCALL;
  865. execute_data = safe_emalloc(num_args, sizeof(zval), ZEND_CALL_FRAME_SLOT * sizeof(zval));
  866. memset(execute_data, 0, sizeof(zend_execute_data));
  867. execute_data->prev_execute_data = &dummy_frame;
  868. EG(current_execute_data) = execute_data;
  869. /* Enable suppression and counting of warnings. */
  870. ZEND_ASSERT(EG(capture_warnings_during_sccp) == 0);
  871. EG(capture_warnings_during_sccp) = 1;
  872. EX(func) = func;
  873. EX_NUM_ARGS() = num_args;
  874. for (i = 0; i < num_args; i++) {
  875. ZVAL_COPY(EX_VAR_NUM(i), args[i]);
  876. }
  877. ZVAL_NULL(result);
  878. func->internal_function.handler(execute_data, result);
  879. for (i = 0; i < num_args; i++) {
  880. zval_ptr_dtor_nogc(EX_VAR_NUM(i));
  881. }
  882. int retval = SUCCESS;
  883. if (EG(exception)) {
  884. zval_ptr_dtor(result);
  885. zend_clear_exception();
  886. retval = FAILURE;
  887. }
  888. if (EG(capture_warnings_during_sccp) > 1) {
  889. zval_ptr_dtor(result);
  890. retval = FAILURE;
  891. }
  892. EG(capture_warnings_during_sccp) = 0;
  893. efree(execute_data);
  894. EG(current_execute_data) = prev_execute_data;
  895. return retval;
  896. }
  897. #define SET_RESULT(op, zv) do { \
  898. if (ssa_op->op##_def >= 0) { \
  899. set_value(scdf, ctx, ssa_op->op##_def, zv); \
  900. } \
  901. } while (0)
  902. #define SET_RESULT_BOT(op) SET_RESULT(op, &ctx->bot)
  903. #define SET_RESULT_TOP(op) SET_RESULT(op, &ctx->top)
  904. #define SKIP_IF_TOP(op) if (IS_TOP(op)) return;
  905. static void sccp_visit_instr(scdf_ctx *scdf, zend_op *opline, zend_ssa_op *ssa_op) {
  906. sccp_ctx *ctx = (sccp_ctx *) scdf;
  907. zval *op1, *op2, zv; /* zv is a temporary to hold result values */
  908. op1 = get_op1_value(ctx, opline, ssa_op);
  909. op2 = get_op2_value(ctx, opline, ssa_op);
  910. switch (opline->opcode) {
  911. case ZEND_ASSIGN:
  912. /* The value of op1 is irrelevant here, because we are overwriting it
  913. * -- unless it can be a reference, in which case we propagate a BOT.
  914. * The result is also BOT in this case, because it might be a typed reference. */
  915. if (IS_BOT(op1) && (ctx->scdf.ssa->var_info[ssa_op->op1_use].type & MAY_BE_REF)) {
  916. SET_RESULT_BOT(op1);
  917. SET_RESULT_BOT(result);
  918. } else {
  919. SET_RESULT(op1, op2);
  920. SET_RESULT(result, op2);
  921. }
  922. return;
  923. case ZEND_TYPE_CHECK:
  924. /* We may be able to evaluate TYPE_CHECK based on type inference info,
  925. * even if we don't know the precise value. */
  926. if (!value_known(op1)) {
  927. uint32_t type = ctx->scdf.ssa->var_info[ssa_op->op1_use].type;
  928. uint32_t expected_type_mask = opline->extended_value;
  929. if (!(type & expected_type_mask) && !(type & MAY_BE_UNDEF)) {
  930. ZVAL_FALSE(&zv);
  931. SET_RESULT(result, &zv);
  932. return;
  933. } else if (!(type & ((MAY_BE_ANY|MAY_BE_UNDEF) - expected_type_mask))
  934. && !(expected_type_mask & MAY_BE_RESOURCE)) {
  935. ZVAL_TRUE(&zv);
  936. SET_RESULT(result, &zv);
  937. return;
  938. }
  939. }
  940. break;
  941. case ZEND_ASSIGN_DIM:
  942. {
  943. zval *data = get_op1_value(ctx, opline+1, ssa_op+1);
  944. /* If $a in $a[$b]=$c is UNDEF, treat it like NULL. There is no warning. */
  945. if ((ctx->scdf.ssa->var_info[ssa_op->op1_use].type & MAY_BE_ANY) == 0) {
  946. op1 = &EG(uninitialized_zval);
  947. }
  948. if (IS_BOT(op1)) {
  949. SET_RESULT_BOT(result);
  950. SET_RESULT_BOT(op1);
  951. return;
  952. }
  953. SKIP_IF_TOP(op1);
  954. SKIP_IF_TOP(data);
  955. if (op2) {
  956. SKIP_IF_TOP(op2);
  957. }
  958. if (op2 && IS_BOT(op2)) {
  959. /* Update of unknown index */
  960. SET_RESULT_BOT(result);
  961. if (ssa_op->op1_def >= 0) {
  962. empty_partial_array(&zv);
  963. SET_RESULT(op1, &zv);
  964. zval_ptr_dtor_nogc(&zv);
  965. } else {
  966. SET_RESULT_BOT(op1);
  967. }
  968. return;
  969. }
  970. if (IS_BOT(data)) {
  971. SET_RESULT_BOT(result);
  972. if ((IS_PARTIAL_ARRAY(op1)
  973. || Z_TYPE_P(op1) == IS_NULL
  974. || Z_TYPE_P(op1) == IS_FALSE
  975. || Z_TYPE_P(op1) == IS_ARRAY)
  976. && ssa_op->op1_def >= 0) {
  977. if (Z_TYPE_P(op1) == IS_NULL || Z_TYPE_P(op1) == IS_FALSE) {
  978. empty_partial_array(&zv);
  979. } else {
  980. dup_partial_array(&zv, op1);
  981. }
  982. if (!op2) {
  983. /* We can't add NEXT element into partial array (skip it) */
  984. SET_RESULT(op1, &zv);
  985. } else if (ct_eval_del_array_elem(&zv, op2) == SUCCESS) {
  986. SET_RESULT(op1, &zv);
  987. } else {
  988. SET_RESULT_BOT(op1);
  989. }
  990. zval_ptr_dtor_nogc(&zv);
  991. } else {
  992. SET_RESULT_BOT(op1);
  993. }
  994. } else {
  995. if (IS_PARTIAL_ARRAY(op1)) {
  996. dup_partial_array(&zv, op1);
  997. } else {
  998. ZVAL_COPY(&zv, op1);
  999. }
  1000. if (!op2 && IS_PARTIAL_ARRAY(&zv)) {
  1001. /* We can't add NEXT element into partial array (skip it) */
  1002. SET_RESULT(result, data);
  1003. SET_RESULT(op1, &zv);
  1004. } else if (ct_eval_assign_dim(&zv, data, op2) == SUCCESS) {
  1005. /* Mark array containing partial array as partial */
  1006. if (IS_PARTIAL_ARRAY(data)) {
  1007. MAKE_PARTIAL_ARRAY(&zv);
  1008. }
  1009. SET_RESULT(result, data);
  1010. SET_RESULT(op1, &zv);
  1011. } else {
  1012. SET_RESULT_BOT(result);
  1013. SET_RESULT_BOT(op1);
  1014. }
  1015. zval_ptr_dtor_nogc(&zv);
  1016. }
  1017. return;
  1018. }
  1019. case ZEND_ASSIGN_OBJ:
  1020. if (ssa_op->op1_def >= 0
  1021. && ctx->scdf.ssa->vars[ssa_op->op1_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
  1022. zval *data = get_op1_value(ctx, opline+1, ssa_op+1);
  1023. zend_ssa_var_info *var_info = &ctx->scdf.ssa->var_info[ssa_op->op1_use];
  1024. /* Don't try to propagate assignments to (potentially) typed properties. We would
  1025. * need to deal with errors and type conversions first. */
  1026. if (!var_info->ce || (var_info->ce->ce_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
  1027. SET_RESULT_BOT(result);
  1028. SET_RESULT_BOT(op1);
  1029. return;
  1030. }
  1031. if (IS_BOT(op1)) {
  1032. SET_RESULT_BOT(result);
  1033. SET_RESULT_BOT(op1);
  1034. return;
  1035. }
  1036. SKIP_IF_TOP(op1);
  1037. SKIP_IF_TOP(data);
  1038. SKIP_IF_TOP(op2);
  1039. if (IS_BOT(op2)) {
  1040. /* Update of unknown property */
  1041. SET_RESULT_BOT(result);
  1042. empty_partial_object(&zv);
  1043. SET_RESULT(op1, &zv);
  1044. zval_ptr_dtor_nogc(&zv);
  1045. return;
  1046. }
  1047. if (IS_BOT(data)) {
  1048. SET_RESULT_BOT(result);
  1049. if (IS_PARTIAL_OBJECT(op1)
  1050. || Z_TYPE_P(op1) == IS_NULL
  1051. || Z_TYPE_P(op1) == IS_FALSE) {
  1052. if (Z_TYPE_P(op1) == IS_NULL || Z_TYPE_P(op1) == IS_FALSE) {
  1053. empty_partial_object(&zv);
  1054. } else {
  1055. dup_partial_object(&zv, op1);
  1056. }
  1057. if (ct_eval_del_obj_prop(&zv, op2) == SUCCESS) {
  1058. SET_RESULT(op1, &zv);
  1059. } else {
  1060. SET_RESULT_BOT(op1);
  1061. }
  1062. zval_ptr_dtor_nogc(&zv);
  1063. } else {
  1064. SET_RESULT_BOT(op1);
  1065. }
  1066. } else {
  1067. if (IS_PARTIAL_OBJECT(op1)) {
  1068. dup_partial_object(&zv, op1);
  1069. } else {
  1070. ZVAL_COPY(&zv, op1);
  1071. }
  1072. if (ct_eval_assign_obj(&zv, data, op2) == SUCCESS) {
  1073. SET_RESULT(result, data);
  1074. SET_RESULT(op1, &zv);
  1075. } else {
  1076. SET_RESULT_BOT(result);
  1077. SET_RESULT_BOT(op1);
  1078. }
  1079. zval_ptr_dtor_nogc(&zv);
  1080. }
  1081. } else {
  1082. SET_RESULT_BOT(result);
  1083. SET_RESULT_BOT(op1);
  1084. }
  1085. return;
  1086. case ZEND_SEND_VAL:
  1087. case ZEND_SEND_VAR:
  1088. {
  1089. /* If the value of a SEND for an ICALL changes, we need to reconsider the
  1090. * ICALL result value. Otherwise we can ignore the opcode. */
  1091. zend_call_info *call;
  1092. if (!ctx->call_map) {
  1093. return;
  1094. }
  1095. call = ctx->call_map[opline - ctx->scdf.op_array->opcodes];
  1096. if (IS_TOP(op1) || !call || !call->caller_call_opline
  1097. || call->caller_call_opline->opcode != ZEND_DO_ICALL) {
  1098. return;
  1099. }
  1100. opline = call->caller_call_opline;
  1101. ssa_op = &ctx->scdf.ssa->ops[opline - ctx->scdf.op_array->opcodes];
  1102. break;
  1103. }
  1104. case ZEND_INIT_ARRAY:
  1105. case ZEND_ADD_ARRAY_ELEMENT:
  1106. {
  1107. zval *result = NULL;
  1108. if (opline->opcode == ZEND_ADD_ARRAY_ELEMENT) {
  1109. result = &ctx->values[ssa_op->result_use];
  1110. if (IS_BOT(result)) {
  1111. SET_RESULT_BOT(result);
  1112. SET_RESULT_BOT(op1);
  1113. return;
  1114. }
  1115. SKIP_IF_TOP(result);
  1116. }
  1117. if (op1) {
  1118. SKIP_IF_TOP(op1);
  1119. }
  1120. if (op2) {
  1121. SKIP_IF_TOP(op2);
  1122. }
  1123. /* We want to avoid keeping around intermediate arrays for each SSA variable in the
  1124. * ADD_ARRAY_ELEMENT chain. We do this by only keeping the array on the last opcode
  1125. * and use a NULL value everywhere else. */
  1126. if (result && Z_TYPE_P(result) == IS_NULL) {
  1127. SET_RESULT_BOT(result);
  1128. return;
  1129. }
  1130. if (op2 && IS_BOT(op2)) {
  1131. /* Update of unknown index */
  1132. SET_RESULT_BOT(op1);
  1133. if (ssa_op->result_def >= 0) {
  1134. empty_partial_array(&zv);
  1135. SET_RESULT(result, &zv);
  1136. zval_ptr_dtor_nogc(&zv);
  1137. } else {
  1138. SET_RESULT_BOT(result);
  1139. }
  1140. return;
  1141. }
  1142. if ((op1 && IS_BOT(op1))
  1143. || (opline->extended_value & ZEND_ARRAY_ELEMENT_REF)) {
  1144. SET_RESULT_BOT(op1);
  1145. if (ssa_op->result_def >= 0) {
  1146. if (!result) {
  1147. empty_partial_array(&zv);
  1148. } else {
  1149. MAKE_PARTIAL_ARRAY(result);
  1150. ZVAL_COPY_VALUE(&zv, result);
  1151. ZVAL_NULL(result);
  1152. }
  1153. if (!op2) {
  1154. /* We can't add NEXT element into partial array (skip it) */
  1155. SET_RESULT(result, &zv);
  1156. } else if (ct_eval_del_array_elem(&zv, op2) == SUCCESS) {
  1157. SET_RESULT(result, &zv);
  1158. } else {
  1159. SET_RESULT_BOT(result);
  1160. }
  1161. zval_ptr_dtor_nogc(&zv);
  1162. } else {
  1163. /* If any operand is BOT, mark the result as BOT right away.
  1164. * Exceptions to this rule are handled above. */
  1165. SET_RESULT_BOT(result);
  1166. }
  1167. } else {
  1168. if (result) {
  1169. ZVAL_COPY_VALUE(&zv, result);
  1170. ZVAL_NULL(result);
  1171. } else {
  1172. array_init(&zv);
  1173. }
  1174. if (op1) {
  1175. if (!op2 && IS_PARTIAL_ARRAY(&zv)) {
  1176. /* We can't add NEXT element into partial array (skip it) */
  1177. SET_RESULT(result, &zv);
  1178. } else if (ct_eval_add_array_elem(&zv, op1, op2) == SUCCESS) {
  1179. if (IS_PARTIAL_ARRAY(op1)) {
  1180. MAKE_PARTIAL_ARRAY(&zv);
  1181. }
  1182. SET_RESULT(result, &zv);
  1183. } else {
  1184. SET_RESULT_BOT(result);
  1185. }
  1186. } else {
  1187. SET_RESULT(result, &zv);
  1188. }
  1189. zval_ptr_dtor_nogc(&zv);
  1190. }
  1191. return;
  1192. }
  1193. case ZEND_ADD_ARRAY_UNPACK: {
  1194. zval *result = &ctx->values[ssa_op->result_use];
  1195. if (IS_BOT(result) || IS_BOT(op1)) {
  1196. SET_RESULT_BOT(result);
  1197. return;
  1198. }
  1199. SKIP_IF_TOP(result);
  1200. SKIP_IF_TOP(op1);
  1201. /* See comment for ADD_ARRAY_ELEMENT. */
  1202. if (Z_TYPE_P(result) == IS_NULL) {
  1203. SET_RESULT_BOT(result);
  1204. return;
  1205. }
  1206. ZVAL_COPY_VALUE(&zv, result);
  1207. ZVAL_NULL(result);
  1208. if (ct_eval_add_array_unpack(&zv, op1) == SUCCESS) {
  1209. SET_RESULT(result, &zv);
  1210. } else {
  1211. SET_RESULT_BOT(result);
  1212. }
  1213. zval_ptr_dtor_nogc(&zv);
  1214. return;
  1215. }
  1216. case ZEND_NEW:
  1217. if (ssa_op->result_def >= 0
  1218. && ctx->scdf.ssa->vars[ssa_op->result_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
  1219. empty_partial_object(&zv);
  1220. SET_RESULT(result, &zv);
  1221. zval_ptr_dtor_nogc(&zv);
  1222. } else {
  1223. SET_RESULT_BOT(result);
  1224. }
  1225. return;
  1226. case ZEND_ASSIGN_STATIC_PROP_REF:
  1227. case ZEND_ASSIGN_OBJ_REF:
  1228. /* Handled here because we also need to BOT the OP_DATA operand, while the generic
  1229. * code below will not do so. */
  1230. SET_RESULT_BOT(result);
  1231. SET_RESULT_BOT(op1);
  1232. SET_RESULT_BOT(op2);
  1233. opline++;
  1234. ssa_op++;
  1235. SET_RESULT_BOT(op1);
  1236. break;
  1237. }
  1238. if ((op1 && IS_BOT(op1)) || (op2 && IS_BOT(op2))) {
  1239. /* If any operand is BOT, mark the result as BOT right away.
  1240. * Exceptions to this rule are handled above. */
  1241. SET_RESULT_BOT(result);
  1242. SET_RESULT_BOT(op1);
  1243. SET_RESULT_BOT(op2);
  1244. return;
  1245. }
  1246. switch (opline->opcode) {
  1247. case ZEND_ADD:
  1248. case ZEND_SUB:
  1249. case ZEND_MUL:
  1250. case ZEND_DIV:
  1251. case ZEND_MOD:
  1252. case ZEND_POW:
  1253. case ZEND_SL:
  1254. case ZEND_SR:
  1255. case ZEND_CONCAT:
  1256. case ZEND_FAST_CONCAT:
  1257. case ZEND_IS_EQUAL:
  1258. case ZEND_IS_NOT_EQUAL:
  1259. case ZEND_IS_SMALLER:
  1260. case ZEND_IS_SMALLER_OR_EQUAL:
  1261. case ZEND_IS_IDENTICAL:
  1262. case ZEND_IS_NOT_IDENTICAL:
  1263. case ZEND_BW_OR:
  1264. case ZEND_BW_AND:
  1265. case ZEND_BW_XOR:
  1266. case ZEND_BOOL_XOR:
  1267. case ZEND_CASE:
  1268. case ZEND_CASE_STRICT:
  1269. SKIP_IF_TOP(op1);
  1270. SKIP_IF_TOP(op2);
  1271. if (ct_eval_binary_op(&zv, opline->opcode, op1, op2) == SUCCESS) {
  1272. SET_RESULT(result, &zv);
  1273. zval_ptr_dtor_nogc(&zv);
  1274. break;
  1275. }
  1276. SET_RESULT_BOT(result);
  1277. break;
  1278. case ZEND_ASSIGN_OP:
  1279. case ZEND_ASSIGN_DIM_OP:
  1280. case ZEND_ASSIGN_OBJ_OP:
  1281. case ZEND_ASSIGN_STATIC_PROP_OP:
  1282. if (op1) {
  1283. SKIP_IF_TOP(op1);
  1284. }
  1285. if (op2) {
  1286. SKIP_IF_TOP(op2);
  1287. }
  1288. if (opline->opcode == ZEND_ASSIGN_OP) {
  1289. if (ct_eval_binary_op(&zv, opline->extended_value, op1, op2) == SUCCESS) {
  1290. SET_RESULT(op1, &zv);
  1291. SET_RESULT(result, &zv);
  1292. zval_ptr_dtor_nogc(&zv);
  1293. break;
  1294. }
  1295. } else if (opline->opcode == ZEND_ASSIGN_DIM_OP) {
  1296. if ((IS_PARTIAL_ARRAY(op1) || Z_TYPE_P(op1) == IS_ARRAY)
  1297. && ssa_op->op1_def >= 0 && op2) {
  1298. zval tmp;
  1299. zval *data = get_op1_value(ctx, opline+1, ssa_op+1);
  1300. SKIP_IF_TOP(data);
  1301. if (ct_eval_fetch_dim(&tmp, op1, op2, 0) == SUCCESS) {
  1302. if (IS_BOT(data)) {
  1303. dup_partial_array(&zv, op1);
  1304. ct_eval_del_array_elem(&zv, op2);
  1305. SET_RESULT_BOT(result);
  1306. SET_RESULT(op1, &zv);
  1307. zval_ptr_dtor_nogc(&tmp);
  1308. zval_ptr_dtor_nogc(&zv);
  1309. break;
  1310. }
  1311. if (ct_eval_binary_op(&tmp, opline->extended_value, &tmp, data) != SUCCESS) {
  1312. SET_RESULT_BOT(result);
  1313. SET_RESULT_BOT(op1);
  1314. zval_ptr_dtor_nogc(&tmp);
  1315. break;
  1316. }
  1317. if (IS_PARTIAL_ARRAY(op1)) {
  1318. dup_partial_array(&zv, op1);
  1319. } else {
  1320. ZVAL_COPY(&zv, op1);
  1321. }
  1322. if (ct_eval_assign_dim(&zv, &tmp, op2) == SUCCESS) {
  1323. SET_RESULT(result, &tmp);
  1324. SET_RESULT(op1, &zv);
  1325. zval_ptr_dtor_nogc(&tmp);
  1326. zval_ptr_dtor_nogc(&zv);
  1327. break;
  1328. }
  1329. zval_ptr_dtor_nogc(&tmp);
  1330. zval_ptr_dtor_nogc(&zv);
  1331. }
  1332. }
  1333. } else if (opline->opcode == ZEND_ASSIGN_OBJ_OP) {
  1334. if (op1 && IS_PARTIAL_OBJECT(op1)
  1335. && ssa_op->op1_def >= 0
  1336. && ctx->scdf.ssa->vars[ssa_op->op1_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
  1337. zval tmp;
  1338. zval *data = get_op1_value(ctx, opline+1, ssa_op+1);
  1339. SKIP_IF_TOP(data);
  1340. if (ct_eval_fetch_obj(&tmp, op1, op2) == SUCCESS) {
  1341. if (IS_BOT(data)) {
  1342. dup_partial_object(&zv, op1);
  1343. ct_eval_del_obj_prop(&zv, op2);
  1344. SET_RESULT_BOT(result);
  1345. SET_RESULT(op1, &zv);
  1346. zval_ptr_dtor_nogc(&tmp);
  1347. zval_ptr_dtor_nogc(&zv);
  1348. break;
  1349. }
  1350. if (ct_eval_binary_op(&tmp, opline->extended_value, &tmp, data) != SUCCESS) {
  1351. SET_RESULT_BOT(result);
  1352. SET_RESULT_BOT(op1);
  1353. zval_ptr_dtor_nogc(&tmp);
  1354. break;
  1355. }
  1356. dup_partial_object(&zv, op1);
  1357. if (ct_eval_assign_obj(&zv, &tmp, op2) == SUCCESS) {
  1358. SET_RESULT(result, &tmp);
  1359. SET_RESULT(op1, &zv);
  1360. zval_ptr_dtor_nogc(&tmp);
  1361. zval_ptr_dtor_nogc(&zv);
  1362. break;
  1363. }
  1364. zval_ptr_dtor_nogc(&tmp);
  1365. zval_ptr_dtor_nogc(&zv);
  1366. }
  1367. }
  1368. }
  1369. SET_RESULT_BOT(result);
  1370. SET_RESULT_BOT(op1);
  1371. break;
  1372. case ZEND_PRE_INC_OBJ:
  1373. case ZEND_PRE_DEC_OBJ:
  1374. case ZEND_POST_INC_OBJ:
  1375. case ZEND_POST_DEC_OBJ:
  1376. if (op1) {
  1377. SKIP_IF_TOP(op1);
  1378. SKIP_IF_TOP(op2);
  1379. if (IS_PARTIAL_OBJECT(op1)
  1380. && ssa_op->op1_def >= 0
  1381. && ctx->scdf.ssa->vars[ssa_op->op1_def].escape_state == ESCAPE_STATE_NO_ESCAPE) {
  1382. zval tmp1, tmp2;
  1383. if (ct_eval_fetch_obj(&tmp1, op1, op2) == SUCCESS
  1384. && ct_eval_incdec(&tmp2, opline->opcode, &tmp1) == SUCCESS) {
  1385. dup_partial_object(&zv, op1);
  1386. ct_eval_assign_obj(&zv, &tmp2, op2);
  1387. if (opline->opcode == ZEND_PRE_INC_OBJ || opline->opcode == ZEND_PRE_DEC_OBJ) {
  1388. SET_RESULT(result, &tmp2);
  1389. } else {
  1390. SET_RESULT(result, &tmp1);
  1391. }
  1392. zval_ptr_dtor_nogc(&tmp1);
  1393. zval_ptr_dtor_nogc(&tmp2);
  1394. SET_RESULT(op1, &zv);
  1395. zval_ptr_dtor_nogc(&zv);
  1396. break;
  1397. }
  1398. }
  1399. }
  1400. SET_RESULT_BOT(op1);
  1401. SET_RESULT_BOT(result);
  1402. break;
  1403. case ZEND_PRE_INC:
  1404. case ZEND_PRE_DEC:
  1405. SKIP_IF_TOP(op1);
  1406. if (ct_eval_incdec(&zv, opline->opcode, op1) == SUCCESS) {
  1407. SET_RESULT(op1, &zv);
  1408. SET_RESULT(result, &zv);
  1409. zval_ptr_dtor_nogc(&zv);
  1410. break;
  1411. }
  1412. SET_RESULT_BOT(op1);
  1413. SET_RESULT_BOT(result);
  1414. break;
  1415. case ZEND_POST_INC:
  1416. case ZEND_POST_DEC:
  1417. SKIP_IF_TOP(op1);
  1418. SET_RESULT(result, op1);
  1419. if (ct_eval_incdec(&zv, opline->opcode, op1) == SUCCESS) {
  1420. SET_RESULT(op1, &zv);
  1421. zval_ptr_dtor_nogc(&zv);
  1422. break;
  1423. }
  1424. SET_RESULT_BOT(op1);
  1425. break;
  1426. case ZEND_BW_NOT:
  1427. case ZEND_BOOL_NOT:
  1428. SKIP_IF_TOP(op1);
  1429. if (IS_PARTIAL_ARRAY(op1)) {
  1430. SET_RESULT_BOT(result);
  1431. break;
  1432. }
  1433. if (zend_optimizer_eval_unary_op(&zv, opline->opcode, op1) == SUCCESS) {
  1434. SET_RESULT(result, &zv);
  1435. zval_ptr_dtor_nogc(&zv);
  1436. break;
  1437. }
  1438. SET_RESULT_BOT(result);
  1439. break;
  1440. case ZEND_CAST:
  1441. SKIP_IF_TOP(op1);
  1442. if (IS_PARTIAL_ARRAY(op1)) {
  1443. SET_RESULT_BOT(result);
  1444. break;
  1445. }
  1446. if (zend_optimizer_eval_cast(&zv, opline->extended_value, op1) == SUCCESS) {
  1447. SET_RESULT(result, &zv);
  1448. zval_ptr_dtor_nogc(&zv);
  1449. break;
  1450. }
  1451. SET_RESULT_BOT(result);
  1452. break;
  1453. case ZEND_BOOL:
  1454. case ZEND_JMPZ_EX:
  1455. case ZEND_JMPNZ_EX:
  1456. SKIP_IF_TOP(op1);
  1457. if (ct_eval_bool_cast(&zv, op1) == SUCCESS) {
  1458. SET_RESULT(result, &zv);
  1459. zval_ptr_dtor_nogc(&zv);
  1460. break;
  1461. }
  1462. SET_RESULT_BOT(result);
  1463. break;
  1464. case ZEND_STRLEN:
  1465. SKIP_IF_TOP(op1);
  1466. if (zend_optimizer_eval_strlen(&zv, op1) == SUCCESS) {
  1467. SET_RESULT(result, &zv);
  1468. zval_ptr_dtor_nogc(&zv);
  1469. break;
  1470. }
  1471. SET_RESULT_BOT(result);
  1472. break;
  1473. case ZEND_YIELD_FROM:
  1474. // tmp = yield from [] -> tmp = null
  1475. SKIP_IF_TOP(op1);
  1476. if (Z_TYPE_P(op1) == IS_ARRAY && zend_hash_num_elements(Z_ARR_P(op1)) == 0) {
  1477. ZVAL_NULL(&zv);
  1478. SET_RESULT(result, &zv);
  1479. break;
  1480. }
  1481. SET_RESULT_BOT(result);
  1482. break;
  1483. case ZEND_COUNT:
  1484. SKIP_IF_TOP(op1);
  1485. if (Z_TYPE_P(op1) == IS_ARRAY) {
  1486. ZVAL_LONG(&zv, zend_hash_num_elements(Z_ARRVAL_P(op1)));
  1487. SET_RESULT(result, &zv);
  1488. zval_ptr_dtor_nogc(&zv);
  1489. break;
  1490. }
  1491. SET_RESULT_BOT(result);
  1492. break;
  1493. case ZEND_IN_ARRAY:
  1494. SKIP_IF_TOP(op1);
  1495. SKIP_IF_TOP(op2);
  1496. if (ct_eval_in_array(&zv, opline->extended_value, op1, op2) == SUCCESS) {
  1497. SET_RESULT(result, &zv);
  1498. zval_ptr_dtor_nogc(&zv);
  1499. break;
  1500. }
  1501. SET_RESULT_BOT(result);
  1502. break;
  1503. case ZEND_ARRAY_KEY_EXISTS:
  1504. SKIP_IF_TOP(op1);
  1505. SKIP_IF_TOP(op2);
  1506. if (ct_eval_array_key_exists(&zv, op1, op2) == SUCCESS) {
  1507. SET_RESULT(result, &zv);
  1508. zval_ptr_dtor_nogc(&zv);
  1509. break;
  1510. }
  1511. SET_RESULT_BOT(result);
  1512. break;
  1513. case ZEND_FETCH_DIM_R:
  1514. case ZEND_FETCH_DIM_IS:
  1515. case ZEND_FETCH_LIST_R:
  1516. SKIP_IF_TOP(op1);
  1517. SKIP_IF_TOP(op2);
  1518. if (ct_eval_fetch_dim(&zv, op1, op2, (opline->opcode != ZEND_FETCH_LIST_R)) == SUCCESS) {
  1519. SET_RESULT(result, &zv);
  1520. zval_ptr_dtor_nogc(&zv);
  1521. break;
  1522. }
  1523. SET_RESULT_BOT(result);
  1524. break;
  1525. case ZEND_ISSET_ISEMPTY_DIM_OBJ:
  1526. SKIP_IF_TOP(op1);
  1527. SKIP_IF_TOP(op2);
  1528. if (ct_eval_isset_dim(&zv, opline->extended_value, op1, op2) == SUCCESS) {
  1529. SET_RESULT(result, &zv);
  1530. zval_ptr_dtor_nogc(&zv);
  1531. break;
  1532. }
  1533. SET_RESULT_BOT(result);
  1534. break;
  1535. case ZEND_FETCH_OBJ_R:
  1536. case ZEND_FETCH_OBJ_IS:
  1537. if (op1) {
  1538. SKIP_IF_TOP(op1);
  1539. SKIP_IF_TOP(op2);
  1540. if (ct_eval_fetch_obj(&zv, op1, op2) == SUCCESS) {
  1541. SET_RESULT(result, &zv);
  1542. zval_ptr_dtor_nogc(&zv);
  1543. break;
  1544. }
  1545. }
  1546. SET_RESULT_BOT(result);
  1547. break;
  1548. case ZEND_ISSET_ISEMPTY_PROP_OBJ:
  1549. if (op1) {
  1550. SKIP_IF_TOP(op1);
  1551. SKIP_IF_TOP(op2);
  1552. if (ct_eval_isset_obj(&zv, opline->extended_value, op1, op2) == SUCCESS) {
  1553. SET_RESULT(result, &zv);
  1554. zval_ptr_dtor_nogc(&zv);
  1555. break;
  1556. }
  1557. }
  1558. SET_RESULT_BOT(result);
  1559. break;
  1560. case ZEND_QM_ASSIGN:
  1561. case ZEND_JMP_SET:
  1562. case ZEND_COALESCE:
  1563. case ZEND_COPY_TMP:
  1564. SET_RESULT(result, op1);
  1565. break;
  1566. case ZEND_JMP_NULL:
  1567. switch (opline->extended_value) {
  1568. case ZEND_SHORT_CIRCUITING_CHAIN_EXPR:
  1569. ZVAL_NULL(&zv);
  1570. break;
  1571. case ZEND_SHORT_CIRCUITING_CHAIN_ISSET:
  1572. ZVAL_FALSE(&zv);
  1573. break;
  1574. case ZEND_SHORT_CIRCUITING_CHAIN_EMPTY:
  1575. ZVAL_TRUE(&zv);
  1576. break;
  1577. EMPTY_SWITCH_DEFAULT_CASE()
  1578. }
  1579. SET_RESULT(result, &zv);
  1580. break;
  1581. #if 0
  1582. case ZEND_FETCH_CLASS:
  1583. if (!op1) {
  1584. SET_RESULT_BOT(result);
  1585. break;
  1586. }
  1587. SET_RESULT(result, op1);
  1588. break;
  1589. #endif
  1590. case ZEND_ISSET_ISEMPTY_CV:
  1591. SKIP_IF_TOP(op1);
  1592. if (ct_eval_isset_isempty(&zv, opline->extended_value, op1) == SUCCESS) {
  1593. SET_RESULT(result, &zv);
  1594. zval_ptr_dtor_nogc(&zv);
  1595. break;
  1596. }
  1597. SET_RESULT_BOT(result);
  1598. break;
  1599. case ZEND_TYPE_CHECK:
  1600. SKIP_IF_TOP(op1);
  1601. ct_eval_type_check(&zv, opline->extended_value, op1);
  1602. SET_RESULT(result, &zv);
  1603. zval_ptr_dtor_nogc(&zv);
  1604. break;
  1605. case ZEND_INSTANCEOF:
  1606. SKIP_IF_TOP(op1);
  1607. ZVAL_FALSE(&zv);
  1608. SET_RESULT(result, &zv);
  1609. break;
  1610. case ZEND_ROPE_INIT:
  1611. SKIP_IF_TOP(op2);
  1612. if (IS_PARTIAL_ARRAY(op2)) {
  1613. SET_RESULT_BOT(result);
  1614. break;
  1615. }
  1616. if (zend_optimizer_eval_cast(&zv, IS_STRING, op2) == SUCCESS) {
  1617. SET_RESULT(result, &zv);
  1618. zval_ptr_dtor_nogc(&zv);
  1619. break;
  1620. }
  1621. SET_RESULT_BOT(result);
  1622. break;
  1623. case ZEND_ROPE_ADD:
  1624. case ZEND_ROPE_END:
  1625. // TODO The way this is currently implemented will result in quadratic runtime
  1626. // This is not necessary, the way the algorithm works it's okay to reuse the same
  1627. // string for all SSA vars with some extra checks
  1628. SKIP_IF_TOP(op1);
  1629. SKIP_IF_TOP(op2);
  1630. if (ct_eval_binary_op(&zv, ZEND_CONCAT, op1, op2) == SUCCESS) {
  1631. SET_RESULT(result, &zv);
  1632. zval_ptr_dtor_nogc(&zv);
  1633. break;
  1634. }
  1635. SET_RESULT_BOT(result);
  1636. break;
  1637. case ZEND_DO_ICALL:
  1638. {
  1639. zend_call_info *call;
  1640. zval *name, *args[3] = {NULL};
  1641. int i;
  1642. if (!ctx->call_map) {
  1643. SET_RESULT_BOT(result);
  1644. break;
  1645. }
  1646. call = ctx->call_map[opline - ctx->scdf.op_array->opcodes];
  1647. name = CT_CONSTANT_EX(ctx->scdf.op_array, call->caller_init_opline->op2.constant);
  1648. /* We already know it can't be evaluated, don't bother checking again */
  1649. if (ssa_op->result_def < 0 || IS_BOT(&ctx->values[ssa_op->result_def])) {
  1650. break;
  1651. }
  1652. /* We're only interested in functions with up to three arguments right now */
  1653. if (call->num_args > 3 || call->send_unpack || call->is_prototype) {
  1654. SET_RESULT_BOT(result);
  1655. break;
  1656. }
  1657. for (i = 0; i < call->num_args; i++) {
  1658. zend_op *opline = call->arg_info[i].opline;
  1659. if (opline->opcode != ZEND_SEND_VAL && opline->opcode != ZEND_SEND_VAR) {
  1660. SET_RESULT_BOT(result);
  1661. return;
  1662. }
  1663. args[i] = get_op1_value(ctx, opline,
  1664. &ctx->scdf.ssa->ops[opline - ctx->scdf.op_array->opcodes]);
  1665. if (args[i]) {
  1666. if (IS_BOT(args[i]) || IS_PARTIAL_ARRAY(args[i])) {
  1667. SET_RESULT_BOT(result);
  1668. return;
  1669. } else if (IS_TOP(args[i])) {
  1670. return;
  1671. }
  1672. }
  1673. }
  1674. /* We didn't get a BOT argument, so value stays the same */
  1675. if (!IS_TOP(&ctx->values[ssa_op->result_def])) {
  1676. break;
  1677. }
  1678. if (ct_eval_func_call(scdf->op_array, &zv, Z_STR_P(name), call->num_args, args) == SUCCESS) {
  1679. SET_RESULT(result, &zv);
  1680. zval_ptr_dtor_nogc(&zv);
  1681. break;
  1682. }
  1683. #if 0
  1684. /* sort out | uniq -c | sort -n */
  1685. fprintf(stderr, "%s\n", Z_STRVAL_P(name));
  1686. /*if (args[1]) {
  1687. php_printf("%s %Z %Z\n", Z_STRVAL_P(name), args[0], args[1]);
  1688. } else {
  1689. php_printf("%s %Z\n", Z_STRVAL_P(name), args[0]);
  1690. }*/
  1691. #endif
  1692. SET_RESULT_BOT(result);
  1693. break;
  1694. }
  1695. default:
  1696. {
  1697. /* If we have no explicit implementation return BOT */
  1698. SET_RESULT_BOT(result);
  1699. SET_RESULT_BOT(op1);
  1700. SET_RESULT_BOT(op2);
  1701. break;
  1702. }
  1703. }
  1704. }
  1705. /* Returns whether there is a successor */
  1706. static void sccp_mark_feasible_successors(
  1707. scdf_ctx *scdf,
  1708. int block_num, zend_basic_block *block,
  1709. zend_op *opline, zend_ssa_op *ssa_op) {
  1710. sccp_ctx *ctx = (sccp_ctx *) scdf;
  1711. zval *op1, zv;
  1712. int s;
  1713. /* We can't determine the branch target at compile-time for these */
  1714. switch (opline->opcode) {
  1715. case ZEND_ASSERT_CHECK:
  1716. case ZEND_CATCH:
  1717. case ZEND_FE_FETCH_R:
  1718. case ZEND_FE_FETCH_RW:
  1719. scdf_mark_edge_feasible(scdf, block_num, block->successors[0]);
  1720. scdf_mark_edge_feasible(scdf, block_num, block->successors[1]);
  1721. return;
  1722. }
  1723. op1 = get_op1_value(ctx, opline, ssa_op);
  1724. /* Branch target can be either one */
  1725. if (!op1 || IS_BOT(op1)) {
  1726. for (s = 0; s < block->successors_count; s++) {
  1727. scdf_mark_edge_feasible(scdf, block_num, block->successors[s]);
  1728. }
  1729. return;
  1730. }
  1731. /* Branch target not yet known */
  1732. if (IS_TOP(op1)) {
  1733. return;
  1734. }
  1735. switch (opline->opcode) {
  1736. case ZEND_JMPZ:
  1737. case ZEND_JMPZNZ:
  1738. case ZEND_JMPZ_EX:
  1739. {
  1740. if (ct_eval_bool_cast(&zv, op1) == FAILURE) {
  1741. scdf_mark_edge_feasible(scdf, block_num, block->successors[0]);
  1742. scdf_mark_edge_feasible(scdf, block_num, block->successors[1]);
  1743. return;
  1744. }
  1745. s = Z_TYPE(zv) == IS_TRUE;
  1746. break;
  1747. }
  1748. case ZEND_JMPNZ:
  1749. case ZEND_JMPNZ_EX:
  1750. case ZEND_JMP_SET:
  1751. {
  1752. if (ct_eval_bool_cast(&zv, op1) == FAILURE) {
  1753. scdf_mark_edge_feasible(scdf, block_num, block->successors[0]);
  1754. scdf_mark_edge_feasible(scdf, block_num, block->successors[1]);
  1755. return;
  1756. }
  1757. s = Z_TYPE(zv) == IS_FALSE;
  1758. break;
  1759. }
  1760. case ZEND_COALESCE:
  1761. s = (Z_TYPE_P(op1) == IS_NULL);
  1762. break;
  1763. case ZEND_JMP_NULL:
  1764. s = (Z_TYPE_P(op1) != IS_NULL);
  1765. break;
  1766. case ZEND_FE_RESET_R:
  1767. case ZEND_FE_RESET_RW:
  1768. /* A non-empty partial array is definitely non-empty, but an
  1769. * empty partial array may be non-empty at runtime. */
  1770. if (Z_TYPE_P(op1) != IS_ARRAY ||
  1771. (IS_PARTIAL_ARRAY(op1) && zend_hash_num_elements(Z_ARR_P(op1)) == 0)) {
  1772. scdf_mark_edge_feasible(scdf, block_num, block->successors[0]);
  1773. scdf_mark_edge_feasible(scdf, block_num, block->successors[1]);
  1774. return;
  1775. }
  1776. s = zend_hash_num_elements(Z_ARR_P(op1)) != 0;
  1777. break;
  1778. case ZEND_SWITCH_LONG:
  1779. case ZEND_SWITCH_STRING:
  1780. case ZEND_MATCH:
  1781. {
  1782. bool strict_comparison = opline->opcode == ZEND_MATCH;
  1783. zend_uchar type = Z_TYPE_P(op1);
  1784. bool correct_type =
  1785. (opline->opcode == ZEND_SWITCH_LONG && type == IS_LONG)
  1786. || (opline->opcode == ZEND_SWITCH_STRING && type == IS_STRING)
  1787. || (opline->opcode == ZEND_MATCH && (type == IS_LONG || type == IS_STRING));
  1788. if (correct_type) {
  1789. zend_op_array *op_array = scdf->op_array;
  1790. zend_ssa *ssa = scdf->ssa;
  1791. HashTable *jmptable = Z_ARRVAL_P(CT_CONSTANT_EX(op_array, opline->op2.constant));
  1792. zval *jmp_zv = type == IS_LONG
  1793. ? zend_hash_index_find(jmptable, Z_LVAL_P(op1))
  1794. : zend_hash_find(jmptable, Z_STR_P(op1));
  1795. int target;
  1796. if (jmp_zv) {
  1797. target = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, Z_LVAL_P(jmp_zv))];
  1798. } else {
  1799. target = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value)];
  1800. }
  1801. scdf_mark_edge_feasible(scdf, block_num, target);
  1802. return;
  1803. } else if (strict_comparison) {
  1804. zend_op_array *op_array = scdf->op_array;
  1805. zend_ssa *ssa = scdf->ssa;
  1806. int target = ssa->cfg.map[ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, opline->extended_value)];
  1807. scdf_mark_edge_feasible(scdf, block_num, target);
  1808. return;
  1809. }
  1810. s = block->successors_count - 1;
  1811. break;
  1812. }
  1813. default:
  1814. for (s = 0; s < block->successors_count; s++) {
  1815. scdf_mark_edge_feasible(scdf, block_num, block->successors[s]);
  1816. }
  1817. return;
  1818. }
  1819. scdf_mark_edge_feasible(scdf, block_num, block->successors[s]);
  1820. }
  1821. static void join_hash_tables(HashTable *ret, HashTable *ht1, HashTable *ht2)
  1822. {
  1823. zend_ulong index;
  1824. zend_string *key;
  1825. zval *val1, *val2;
  1826. ZEND_HASH_FOREACH_KEY_VAL(ht1, index, key, val1) {
  1827. if (key) {
  1828. val2 = zend_hash_find(ht2, key);
  1829. } else {
  1830. val2 = zend_hash_index_find(ht2, index);
  1831. }
  1832. if (val2 && zend_is_identical(val1, val2)) {
  1833. if (key) {
  1834. val1 = zend_hash_add_new(ret, key, val1);
  1835. } else {
  1836. val1 = zend_hash_index_add_new(ret, index, val1);
  1837. }
  1838. Z_TRY_ADDREF_P(val1);
  1839. }
  1840. } ZEND_HASH_FOREACH_END();
  1841. }
  1842. static int join_partial_arrays(zval *a, zval *b)
  1843. {
  1844. zval ret;
  1845. if ((Z_TYPE_P(a) != IS_ARRAY && !IS_PARTIAL_ARRAY(a))
  1846. || (Z_TYPE_P(b) != IS_ARRAY && !IS_PARTIAL_ARRAY(b))) {
  1847. return FAILURE;
  1848. }
  1849. empty_partial_array(&ret);
  1850. join_hash_tables(Z_ARRVAL(ret), Z_ARRVAL_P(a), Z_ARRVAL_P(b));
  1851. zval_ptr_dtor_nogc(a);
  1852. ZVAL_COPY_VALUE(a, &ret);
  1853. return SUCCESS;
  1854. }
  1855. static int join_partial_objects(zval *a, zval *b)
  1856. {
  1857. zval ret;
  1858. if (!IS_PARTIAL_OBJECT(a) || !IS_PARTIAL_OBJECT(b)) {
  1859. return FAILURE;
  1860. }
  1861. empty_partial_object(&ret);
  1862. join_hash_tables(Z_ARRVAL(ret), Z_ARRVAL_P(a), Z_ARRVAL_P(b));
  1863. zval_ptr_dtor_nogc(a);
  1864. ZVAL_COPY_VALUE(a, &ret);
  1865. return SUCCESS;
  1866. }
  1867. static void join_phi_values(zval *a, zval *b, bool escape) {
  1868. if (IS_BOT(a) || IS_TOP(b)) {
  1869. return;
  1870. }
  1871. if (IS_TOP(a)) {
  1872. zval_ptr_dtor_nogc(a);
  1873. ZVAL_COPY(a, b);
  1874. return;
  1875. }
  1876. if (IS_BOT(b)) {
  1877. zval_ptr_dtor_nogc(a);
  1878. MAKE_BOT(a);
  1879. return;
  1880. }
  1881. if (IS_PARTIAL_ARRAY(a) || IS_PARTIAL_ARRAY(b)) {
  1882. if (join_partial_arrays(a, b) != SUCCESS) {
  1883. zval_ptr_dtor_nogc(a);
  1884. MAKE_BOT(a);
  1885. }
  1886. } else if (IS_PARTIAL_OBJECT(a) || IS_PARTIAL_OBJECT(b)) {
  1887. if (escape || join_partial_objects(a, b) != SUCCESS) {
  1888. zval_ptr_dtor_nogc(a);
  1889. MAKE_BOT(a);
  1890. }
  1891. } else if (!zend_is_identical(a, b)) {
  1892. if (join_partial_arrays(a, b) != SUCCESS) {
  1893. zval_ptr_dtor_nogc(a);
  1894. MAKE_BOT(a);
  1895. }
  1896. }
  1897. }
  1898. static void sccp_visit_phi(scdf_ctx *scdf, zend_ssa_phi *phi) {
  1899. sccp_ctx *ctx = (sccp_ctx *) scdf;
  1900. zend_ssa *ssa = scdf->ssa;
  1901. ZEND_ASSERT(phi->ssa_var >= 0);
  1902. if (!IS_BOT(&ctx->values[phi->ssa_var])) {
  1903. zend_basic_block *block = &ssa->cfg.blocks[phi->block];
  1904. int *predecessors = &ssa->cfg.predecessors[block->predecessor_offset];
  1905. int i;
  1906. zval result;
  1907. MAKE_TOP(&result);
  1908. #if SCP_DEBUG
  1909. fprintf(stderr, "Handling phi(");
  1910. #endif
  1911. if (phi->pi >= 0) {
  1912. ZEND_ASSERT(phi->sources[0] >= 0);
  1913. if (scdf_is_edge_feasible(scdf, phi->pi, phi->block)) {
  1914. join_phi_values(&result, &ctx->values[phi->sources[0]], ssa->vars[phi->ssa_var].escape_state != ESCAPE_STATE_NO_ESCAPE);
  1915. }
  1916. } else {
  1917. for (i = 0; i < block->predecessors_count; i++) {
  1918. ZEND_ASSERT(phi->sources[i] >= 0);
  1919. if (scdf_is_edge_feasible(scdf, predecessors[i], phi->block)) {
  1920. #if SCP_DEBUG
  1921. scp_dump_value(&ctx->values[phi->sources[i]]);
  1922. fprintf(stderr, ",");
  1923. #endif
  1924. join_phi_values(&result, &ctx->values[phi->sources[i]], ssa->vars[phi->ssa_var].escape_state != ESCAPE_STATE_NO_ESCAPE);
  1925. } else {
  1926. #if SCP_DEBUG
  1927. fprintf(stderr, " --,");
  1928. #endif
  1929. }
  1930. }
  1931. }
  1932. #if SCP_DEBUG
  1933. fprintf(stderr, ")\n");
  1934. #endif
  1935. set_value(scdf, ctx, phi->ssa_var, &result);
  1936. zval_ptr_dtor_nogc(&result);
  1937. }
  1938. }
  1939. static zval *value_from_type_and_range(sccp_ctx *ctx, int var_num, zval *tmp) {
  1940. zend_ssa *ssa = ctx->scdf.ssa;
  1941. zend_ssa_var_info *info = &ssa->var_info[var_num];
  1942. if (info->type & MAY_BE_UNDEF) {
  1943. return NULL;
  1944. }
  1945. if (!(info->type & MAY_BE_ANY)) {
  1946. /* This code must be unreachable. We could replace operands with NULL, but this doesn't
  1947. * really make things better. It would be better to later remove this code entirely. */
  1948. return NULL;
  1949. }
  1950. if (!(info->type & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_NULL))) {
  1951. ZVAL_NULL(tmp);
  1952. return tmp;
  1953. }
  1954. if (!(info->type & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_FALSE))) {
  1955. ZVAL_FALSE(tmp);
  1956. return tmp;
  1957. }
  1958. if (!(info->type & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_TRUE))) {
  1959. ZVAL_TRUE(tmp);
  1960. return tmp;
  1961. }
  1962. if (!(info->type & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_LONG))
  1963. && info->has_range
  1964. && !info->range.overflow && !info->range.underflow
  1965. && info->range.min == info->range.max) {
  1966. ZVAL_LONG(tmp, info->range.min);
  1967. return tmp;
  1968. }
  1969. return NULL;
  1970. }
  1971. /* Call instruction -> remove opcodes that are part of the call */
  1972. static int remove_call(sccp_ctx *ctx, zend_op *opline, zend_ssa_op *ssa_op)
  1973. {
  1974. zend_ssa *ssa = ctx->scdf.ssa;
  1975. zend_op_array *op_array = ctx->scdf.op_array;
  1976. zend_call_info *call;
  1977. int i;
  1978. ZEND_ASSERT(ctx->call_map);
  1979. call = ctx->call_map[opline - op_array->opcodes];
  1980. ZEND_ASSERT(call);
  1981. ZEND_ASSERT(call->caller_call_opline == opline);
  1982. zend_ssa_remove_instr(ssa, opline, ssa_op);
  1983. zend_ssa_remove_instr(ssa, call->caller_init_opline,
  1984. &ssa->ops[call->caller_init_opline - op_array->opcodes]);
  1985. for (i = 0; i < call->num_args; i++) {
  1986. zend_ssa_remove_instr(ssa, call->arg_info[i].opline,
  1987. &ssa->ops[call->arg_info[i].opline - op_array->opcodes]);
  1988. }
  1989. // TODO: remove call_info completely???
  1990. call->callee_func = NULL;
  1991. return call->num_args + 2;
  1992. }
  1993. /* This is a basic DCE pass we run after SCCP. It only works on those instructions those result
  1994. * value(s) were determined by SCCP. It removes dead computational instructions and converts
  1995. * CV-affecting instructions into CONST ASSIGNs. This basic DCE is performed for multiple reasons:
  1996. * a) During operand replacement we eliminate FREEs. The corresponding computational instructions
  1997. * must be removed to avoid leaks. This way SCCP can run independently of the full DCE pass.
  1998. * b) The main DCE pass relies on type analysis to determine whether instructions have side-effects
  1999. * and can't be DCEd. This means that it will not be able collect all instructions rendered dead
  2000. * by SCCP, because they may have potentially side-effecting types, but the actual values are
  2001. * not. As such doing DCE here will allow us to eliminate more dead code in combination.
  2002. * c) The ordinary DCE pass cannot collect dead calls. However SCCP can result in dead calls, which
  2003. * we need to collect.
  2004. * d) The ordinary DCE pass cannot collect construction of dead non-escaping arrays and objects.
  2005. */
  2006. static int try_remove_definition(sccp_ctx *ctx, int var_num, zend_ssa_var *var, zval *value)
  2007. {
  2008. zend_ssa *ssa = ctx->scdf.ssa;
  2009. zend_op_array *op_array = ctx->scdf.op_array;
  2010. int removed_ops = 0;
  2011. if (var->definition >= 0) {
  2012. zend_op *opline = &op_array->opcodes[var->definition];
  2013. zend_ssa_op *ssa_op = &ssa->ops[var->definition];
  2014. if (ssa_op->result_def == var_num) {
  2015. if (opline->opcode == ZEND_ASSIGN) {
  2016. /* We can't drop the ASSIGN, but we can remove the result. */
  2017. if (var->use_chain < 0 && var->phi_use_chain == NULL) {
  2018. opline->result_type = IS_UNUSED;
  2019. zend_ssa_remove_result_def(ssa, ssa_op);
  2020. }
  2021. return 0;
  2022. }
  2023. if (ssa_op->op1_def >= 0 || ssa_op->op2_def >= 0) {
  2024. if (var->use_chain < 0 && var->phi_use_chain == NULL) {
  2025. switch (opline->opcode) {
  2026. case ZEND_ASSIGN:
  2027. case ZEND_ASSIGN_REF:
  2028. case ZEND_ASSIGN_DIM:
  2029. case ZEND_ASSIGN_OBJ:
  2030. case ZEND_ASSIGN_OBJ_REF:
  2031. case ZEND_ASSIGN_STATIC_PROP:
  2032. case ZEND_ASSIGN_STATIC_PROP_REF:
  2033. case ZEND_ASSIGN_OP:
  2034. case ZEND_ASSIGN_DIM_OP:
  2035. case ZEND_ASSIGN_OBJ_OP:
  2036. case ZEND_ASSIGN_STATIC_PROP_OP:
  2037. case ZEND_PRE_INC:
  2038. case ZEND_PRE_DEC:
  2039. case ZEND_PRE_INC_OBJ:
  2040. case ZEND_PRE_DEC_OBJ:
  2041. case ZEND_DO_ICALL:
  2042. case ZEND_DO_UCALL:
  2043. case ZEND_DO_FCALL_BY_NAME:
  2044. case ZEND_DO_FCALL:
  2045. case ZEND_INCLUDE_OR_EVAL:
  2046. case ZEND_YIELD:
  2047. case ZEND_YIELD_FROM:
  2048. case ZEND_ASSERT_CHECK:
  2049. opline->result_type = IS_UNUSED;
  2050. zend_ssa_remove_result_def(ssa, ssa_op);
  2051. break;
  2052. default:
  2053. break;
  2054. }
  2055. }
  2056. /* we cannot remove instruction that defines other variables */
  2057. return 0;
  2058. } else if (opline->opcode == ZEND_JMPZ_EX
  2059. || opline->opcode == ZEND_JMPNZ_EX
  2060. || opline->opcode == ZEND_JMP_SET
  2061. || opline->opcode == ZEND_COALESCE
  2062. || opline->opcode == ZEND_JMP_NULL
  2063. || opline->opcode == ZEND_FE_RESET_R
  2064. || opline->opcode == ZEND_FE_RESET_RW
  2065. || opline->opcode == ZEND_FE_FETCH_R
  2066. || opline->opcode == ZEND_FE_FETCH_RW
  2067. || opline->opcode == ZEND_NEW) {
  2068. /* we cannot simple remove jump instructions */
  2069. return 0;
  2070. } else if (var->use_chain >= 0
  2071. || var->phi_use_chain != NULL) {
  2072. if (value
  2073. && (opline->result_type & (IS_VAR|IS_TMP_VAR))
  2074. && opline->opcode != ZEND_QM_ASSIGN
  2075. && opline->opcode != ZEND_ROPE_INIT
  2076. && opline->opcode != ZEND_ROPE_ADD
  2077. && opline->opcode != ZEND_INIT_ARRAY
  2078. && opline->opcode != ZEND_ADD_ARRAY_ELEMENT
  2079. && opline->opcode != ZEND_ADD_ARRAY_UNPACK) {
  2080. /* Replace with QM_ASSIGN */
  2081. zend_uchar old_type = opline->result_type;
  2082. uint32_t old_var = opline->result.var;
  2083. ssa_op->result_def = -1;
  2084. if (opline->opcode == ZEND_DO_ICALL) {
  2085. removed_ops = remove_call(ctx, opline, ssa_op) - 1;
  2086. } else {
  2087. zend_ssa_remove_instr(ssa, opline, ssa_op);
  2088. }
  2089. ssa_op->result_def = var_num;
  2090. opline->opcode = ZEND_QM_ASSIGN;
  2091. opline->result_type = old_type;
  2092. opline->result.var = old_var;
  2093. Z_TRY_ADDREF_P(value);
  2094. zend_optimizer_update_op1_const(ctx->scdf.op_array, opline, value);
  2095. }
  2096. return 0;
  2097. } else if ((opline->op2_type & (IS_VAR|IS_TMP_VAR))
  2098. && (!value_known(&ctx->values[ssa_op->op2_use])
  2099. || IS_PARTIAL_ARRAY(&ctx->values[ssa_op->op2_use])
  2100. || IS_PARTIAL_OBJECT(&ctx->values[ssa_op->op2_use]))) {
  2101. return 0;
  2102. } else if ((opline->op1_type & (IS_VAR|IS_TMP_VAR))
  2103. && (!value_known(&ctx->values[ssa_op->op1_use])
  2104. || IS_PARTIAL_ARRAY(&ctx->values[ssa_op->op1_use])
  2105. || IS_PARTIAL_OBJECT(&ctx->values[ssa_op->op1_use]))) {
  2106. if (opline->opcode == ZEND_TYPE_CHECK
  2107. || opline->opcode == ZEND_BOOL) {
  2108. zend_ssa_remove_result_def(ssa, ssa_op);
  2109. /* For TYPE_CHECK we may compute the result value without knowing the
  2110. * operand, based on type inference information. Make sure the operand is
  2111. * freed and leave further cleanup to DCE. */
  2112. opline->opcode = ZEND_FREE;
  2113. opline->result_type = IS_UNUSED;
  2114. removed_ops++;
  2115. } else {
  2116. return 0;
  2117. }
  2118. } else {
  2119. zend_ssa_remove_result_def(ssa, ssa_op);
  2120. if (opline->opcode == ZEND_DO_ICALL) {
  2121. removed_ops = remove_call(ctx, opline, ssa_op);
  2122. } else {
  2123. zend_ssa_remove_instr(ssa, opline, ssa_op);
  2124. removed_ops++;
  2125. }
  2126. }
  2127. } else if (ssa_op->op1_def == var_num) {
  2128. if (opline->opcode == ZEND_ASSIGN) {
  2129. /* Leave assigns to DCE (due to dtor effects) */
  2130. return 0;
  2131. }
  2132. /* Compound assign or incdec -> convert to direct ASSIGN */
  2133. if (!value) {
  2134. /* In some cases zend_may_throw() may be avoided */
  2135. switch (opline->opcode) {
  2136. case ZEND_ASSIGN_DIM:
  2137. case ZEND_ASSIGN_OBJ:
  2138. case ZEND_ASSIGN_OP:
  2139. case ZEND_ASSIGN_DIM_OP:
  2140. case ZEND_ASSIGN_OBJ_OP:
  2141. case ZEND_ASSIGN_STATIC_PROP_OP:
  2142. if ((ssa_op->op2_use >= 0 && !value_known(&ctx->values[ssa_op->op2_use]))
  2143. || ((ssa_op+1)->op1_use >= 0 &&!value_known(&ctx->values[(ssa_op+1)->op1_use]))) {
  2144. return 0;
  2145. }
  2146. break;
  2147. case ZEND_PRE_INC_OBJ:
  2148. case ZEND_PRE_DEC_OBJ:
  2149. case ZEND_POST_INC_OBJ:
  2150. case ZEND_POST_DEC_OBJ:
  2151. if (ssa_op->op2_use >= 0 && !value_known(&ctx->values[ssa_op->op2_use])) {
  2152. return 0;
  2153. }
  2154. break;
  2155. case ZEND_INIT_ARRAY:
  2156. case ZEND_ADD_ARRAY_ELEMENT:
  2157. if (opline->op2_type == IS_UNUSED) {
  2158. return 0;
  2159. }
  2160. /* break missing intentionally */
  2161. default:
  2162. if (zend_may_throw(opline, ssa_op, op_array, ssa)) {
  2163. return 0;
  2164. }
  2165. break;
  2166. }
  2167. }
  2168. /* Mark result unused, if possible */
  2169. if (ssa_op->result_def >= 0) {
  2170. if (ssa->vars[ssa_op->result_def].use_chain < 0
  2171. && ssa->vars[ssa_op->result_def].phi_use_chain == NULL) {
  2172. zend_ssa_remove_result_def(ssa, ssa_op);
  2173. opline->result_type = IS_UNUSED;
  2174. } else if (opline->opcode != ZEND_PRE_INC &&
  2175. opline->opcode != ZEND_PRE_DEC) {
  2176. /* op1_def and result_def are different */
  2177. return removed_ops;
  2178. }
  2179. }
  2180. /* Destroy previous op2 */
  2181. if (opline->op2_type == IS_CONST) {
  2182. literal_dtor(&ZEND_OP2_LITERAL(opline));
  2183. } else if (ssa_op->op2_use >= 0) {
  2184. if (ssa_op->op2_use != ssa_op->op1_use) {
  2185. zend_ssa_unlink_use_chain(ssa, var->definition, ssa_op->op2_use);
  2186. }
  2187. ssa_op->op2_use = -1;
  2188. ssa_op->op2_use_chain = -1;
  2189. }
  2190. /* Remove OP_DATA opcode */
  2191. switch (opline->opcode) {
  2192. case ZEND_ASSIGN_DIM:
  2193. case ZEND_ASSIGN_OBJ:
  2194. removed_ops++;
  2195. zend_ssa_remove_instr(ssa, opline + 1, ssa_op + 1);
  2196. break;
  2197. case ZEND_ASSIGN_DIM_OP:
  2198. case ZEND_ASSIGN_OBJ_OP:
  2199. case ZEND_ASSIGN_STATIC_PROP_OP:
  2200. removed_ops++;
  2201. zend_ssa_remove_instr(ssa, opline + 1, ssa_op + 1);
  2202. break;
  2203. default:
  2204. break;
  2205. }
  2206. if (value) {
  2207. /* Convert to ASSIGN */
  2208. opline->opcode = ZEND_ASSIGN;
  2209. opline->op2_type = IS_CONST;
  2210. opline->op2.constant = zend_optimizer_add_literal(op_array, value);
  2211. Z_TRY_ADDREF_P(value);
  2212. } else {
  2213. /* Remove dead array or object construction */
  2214. removed_ops++;
  2215. if (var->use_chain >= 0 || var->phi_use_chain != NULL) {
  2216. zend_ssa_rename_var_uses(ssa, ssa_op->op1_def, ssa_op->op1_use, 1);
  2217. }
  2218. zend_ssa_remove_op1_def(ssa, ssa_op);
  2219. zend_ssa_remove_instr(ssa, opline, ssa_op);
  2220. }
  2221. }
  2222. } else if (var->definition_phi
  2223. && var->use_chain < 0
  2224. && var->phi_use_chain == NULL) {
  2225. zend_ssa_remove_phi(ssa, var->definition_phi);
  2226. }
  2227. return removed_ops;
  2228. }
  2229. /* This will try to replace uses of SSA variables we have determined to be constant. Not all uses
  2230. * can be replaced, because some instructions don't accept constant operands or only accept them
  2231. * if they have a certain type. */
  2232. static int replace_constant_operands(sccp_ctx *ctx) {
  2233. zend_ssa *ssa = ctx->scdf.ssa;
  2234. zend_op_array *op_array = ctx->scdf.op_array;
  2235. int i;
  2236. zval tmp;
  2237. int removed_ops = 0;
  2238. /* We iterate the variables backwards, so we can eliminate sequences like INIT_ROPE
  2239. * and INIT_ARRAY. */
  2240. for (i = ssa->vars_count - 1; i >= op_array->last_var; i--) {
  2241. zend_ssa_var *var = &ssa->vars[i];
  2242. zval *value;
  2243. int use;
  2244. if (IS_PARTIAL_ARRAY(&ctx->values[i])
  2245. || IS_PARTIAL_OBJECT(&ctx->values[i])) {
  2246. if (!Z_DELREF(ctx->values[i])) {
  2247. zend_array_destroy(Z_ARR(ctx->values[i]));
  2248. }
  2249. MAKE_BOT(&ctx->values[i]);
  2250. if ((var->use_chain < 0 && var->phi_use_chain == NULL) || var->no_val) {
  2251. removed_ops += try_remove_definition(ctx, i, var, NULL);
  2252. }
  2253. continue;
  2254. } else if (value_known(&ctx->values[i])) {
  2255. value = &ctx->values[i];
  2256. } else {
  2257. value = value_from_type_and_range(ctx, i, &tmp);
  2258. if (!value) {
  2259. continue;
  2260. }
  2261. }
  2262. FOREACH_USE(var, use) {
  2263. zend_op *opline = &op_array->opcodes[use];
  2264. zend_ssa_op *ssa_op = &ssa->ops[use];
  2265. if (try_replace_op1(ctx, opline, ssa_op, i, value)) {
  2266. if (opline->opcode == ZEND_NOP) {
  2267. removed_ops++;
  2268. }
  2269. ZEND_ASSERT(ssa_op->op1_def == -1);
  2270. if (ssa_op->op1_use != ssa_op->op2_use) {
  2271. zend_ssa_unlink_use_chain(ssa, use, ssa_op->op1_use);
  2272. } else {
  2273. ssa_op->op2_use_chain = ssa_op->op1_use_chain;
  2274. }
  2275. ssa_op->op1_use = -1;
  2276. ssa_op->op1_use_chain = -1;
  2277. }
  2278. if (try_replace_op2(ctx, opline, ssa_op, i, value)) {
  2279. ZEND_ASSERT(ssa_op->op2_def == -1);
  2280. if (ssa_op->op2_use != ssa_op->op1_use) {
  2281. zend_ssa_unlink_use_chain(ssa, use, ssa_op->op2_use);
  2282. }
  2283. ssa_op->op2_use = -1;
  2284. ssa_op->op2_use_chain = -1;
  2285. }
  2286. } FOREACH_USE_END();
  2287. if (value_known(&ctx->values[i])) {
  2288. removed_ops += try_remove_definition(ctx, i, var, value);
  2289. }
  2290. }
  2291. return removed_ops;
  2292. }
  2293. static void sccp_context_init(zend_optimizer_ctx *ctx, sccp_ctx *sccp,
  2294. zend_ssa *ssa, zend_op_array *op_array, zend_call_info **call_map) {
  2295. int i;
  2296. sccp->call_map = call_map;
  2297. sccp->values = zend_arena_alloc(&ctx->arena, sizeof(zval) * ssa->vars_count);
  2298. MAKE_TOP(&sccp->top);
  2299. MAKE_BOT(&sccp->bot);
  2300. i = 0;
  2301. for (; i < op_array->last_var; ++i) {
  2302. /* These are all undefined variables, which we have to mark BOT.
  2303. * Otherwise the undefined variable warning might not be preserved. */
  2304. MAKE_BOT(&sccp->values[i]);
  2305. }
  2306. for (; i < ssa->vars_count; ++i) {
  2307. if (ssa->vars[i].alias) {
  2308. MAKE_BOT(&sccp->values[i]);
  2309. } else {
  2310. MAKE_TOP(&sccp->values[i]);
  2311. }
  2312. }
  2313. }
  2314. static void sccp_context_free(sccp_ctx *sccp) {
  2315. int i;
  2316. for (i = sccp->scdf.op_array->last_var; i < sccp->scdf.ssa->vars_count; ++i) {
  2317. zval_ptr_dtor_nogc(&sccp->values[i]);
  2318. }
  2319. }
  2320. int sccp_optimize_op_array(zend_optimizer_ctx *ctx, zend_op_array *op_array, zend_ssa *ssa, zend_call_info **call_map)
  2321. {
  2322. sccp_ctx sccp;
  2323. int removed_ops = 0;
  2324. void *checkpoint = zend_arena_checkpoint(ctx->arena);
  2325. sccp_context_init(ctx, &sccp, ssa, op_array, call_map);
  2326. sccp.scdf.handlers.visit_instr = sccp_visit_instr;
  2327. sccp.scdf.handlers.visit_phi = sccp_visit_phi;
  2328. sccp.scdf.handlers.mark_feasible_successors = sccp_mark_feasible_successors;
  2329. scdf_init(ctx, &sccp.scdf, op_array, ssa);
  2330. scdf_solve(&sccp.scdf, "SCCP");
  2331. if (ctx->debug_level & ZEND_DUMP_SCCP) {
  2332. int i, first = 1;
  2333. for (i = op_array->last_var; i < ssa->vars_count; i++) {
  2334. zval *zv = &sccp.values[i];
  2335. if (IS_TOP(zv) || IS_BOT(zv)) {
  2336. continue;
  2337. }
  2338. if (first) {
  2339. first = 0;
  2340. fprintf(stderr, "\nSCCP Values for \"");
  2341. zend_dump_op_array_name(op_array);
  2342. fprintf(stderr, "\":\n");
  2343. }
  2344. fprintf(stderr, " #%d.", i);
  2345. zend_dump_var(op_array, IS_CV, ssa->vars[i].var);
  2346. fprintf(stderr, " =");
  2347. scp_dump_value(zv);
  2348. fprintf(stderr, "\n");
  2349. }
  2350. }
  2351. removed_ops += scdf_remove_unreachable_blocks(&sccp.scdf);
  2352. removed_ops += replace_constant_operands(&sccp);
  2353. sccp_context_free(&sccp);
  2354. zend_arena_release(&ctx->arena, checkpoint);
  2355. return removed_ops;
  2356. }