sccp.c 69 KB

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