Coverage Report

Created: 2025-11-16 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/opcache/jit/zend_jit.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend JIT                                                             |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) The PHP Group                                          |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 3.01 of the PHP license,      |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | https://www.php.net/license/3_01.txt                                 |
11
   | If you did not receive a copy of the PHP license and are unable to   |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@php.net so we can mail you a copy immediately.               |
14
   +----------------------------------------------------------------------+
15
   | Authors: Dmitry Stogov <dmitry@php.net>                              |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include "main/php.h"
20
#include "main/SAPI.h"
21
#include "php_version.h"
22
#include <ZendAccelerator.h>
23
#include "zend_shared_alloc.h"
24
#include "Zend/zend_execute.h"
25
#include "Zend/zend_vm.h"
26
#include "Zend/zend_exceptions.h"
27
#include "Zend/zend_constants.h"
28
#include "Zend/zend_closures.h"
29
#include "Zend/zend_ini.h"
30
#include "Zend/zend_observer.h"
31
#include "zend_smart_str.h"
32
#include "jit/zend_jit.h"
33
34
#ifdef HAVE_JIT
35
36
#include "Optimizer/zend_func_info.h"
37
#include "Optimizer/zend_ssa.h"
38
#include "Optimizer/zend_inference.h"
39
#include "Optimizer/zend_call_graph.h"
40
#include "Optimizer/zend_dump.h"
41
#include "Optimizer/zend_worklist.h"
42
43
#include "jit/zend_jit_internal.h"
44
45
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
46
#include <pthread.h>
47
#endif
48
49
#ifdef ZTS
50
int jit_globals_id;
51
size_t jit_globals_offset;
52
#else
53
zend_jit_globals jit_globals;
54
#endif
55
56
//#define CONTEXT_THREADED_JIT
57
#define ZEND_JIT_USE_RC_INFERENCE
58
59
#ifdef ZEND_JIT_USE_RC_INFERENCE
60
0
# define ZEND_SSA_RC_INFERENCE_FLAG ZEND_SSA_RC_INFERENCE
61
0
# define RC_MAY_BE_1(info)          (((info) & (MAY_BE_RC1|MAY_BE_REF)) != 0)
62
0
# define RC_MAY_BE_N(info)          (((info) & (MAY_BE_RCN|MAY_BE_REF)) != 0)
63
#else
64
# define ZEND_SSA_RC_INFERENCE_FLAG 0
65
# define RC_MAY_BE_1(info)          1
66
# define RC_MAY_BE_N(info)          1
67
#endif
68
69
0
#define JIT_PREFIX      "JIT$"
70
#define JIT_STUB_PREFIX "JIT$$"
71
0
#define TRACE_PREFIX    "TRACE-"
72
73
bool zend_jit_startup_ok = false;
74
75
zend_ulong zend_jit_profile_counter = 0;
76
int zend_jit_profile_counter_rid = -1;
77
78
int16_t zend_jit_hot_counters[ZEND_HOT_COUNTERS_COUNT];
79
80
const zend_op *zend_jit_halt_op = NULL;
81
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
82
static int zend_write_protect = 1;
83
#endif
84
85
static void *dasm_buf = NULL;
86
static void *dasm_end = NULL;
87
static void **dasm_ptr = NULL;
88
89
static size_t dasm_size = 0;
90
91
static zend_long jit_bisect_pos = 0;
92
93
static zend_vm_opcode_handler_t zend_jit_runtime_jit_handler = NULL;
94
static zend_vm_opcode_handler_t zend_jit_profile_jit_handler = NULL;
95
static zend_vm_opcode_handler_t zend_jit_func_hot_counter_handler = NULL;
96
static zend_vm_opcode_handler_t zend_jit_loop_hot_counter_handler = NULL;
97
static zend_vm_opcode_handler_t zend_jit_func_trace_counter_handler = NULL;
98
static zend_vm_opcode_handler_t zend_jit_ret_trace_counter_handler = NULL;
99
static zend_vm_opcode_handler_t zend_jit_loop_trace_counter_handler = NULL;
100
101
#if ZEND_VM_KIND == ZEND_VM_KIND_CALL || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
102
static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV zend_runtime_jit(ZEND_OPCODE_HANDLER_ARGS);
103
#else
104
static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV zend_runtime_jit(ZEND_OPCODE_HANDLER_ARGS);
105
#endif
106
107
static int zend_jit_trace_op_len(const zend_op *opline);
108
static int zend_jit_trace_may_exit(const zend_op_array *op_array, const zend_op *opline);
109
static uint32_t _zend_jit_trace_get_exit_point(const zend_op *to_opline, uint32_t flags ZEND_FILE_LINE_DC);
110
0
#define zend_jit_trace_get_exit_point(to_opline, flags) _zend_jit_trace_get_exit_point(to_opline, flags ZEND_FILE_LINE_CC)
111
static const void *zend_jit_trace_get_exit_addr(uint32_t n);
112
static void zend_jit_trace_add_code(const void *start, uint32_t size);
113
static zend_string *zend_jit_func_name(const zend_op_array *op_array);
114
115
static bool zend_jit_needs_arg_dtor(const zend_function *func, uint32_t arg_num, zend_call_info *call_info);
116
static bool zend_jit_supported_binary_op(uint8_t op, uint32_t op1_info, uint32_t op2_info);
117
118
0
static bool dominates(const zend_basic_block *blocks, int a, int b) {
119
0
  while (blocks[b].level > blocks[a].level) {
120
0
    b = blocks[b].idom;
121
0
  }
122
0
  return a == b;
123
0
}
124
125
static bool zend_ssa_is_last_use(const zend_op_array *op_array, const zend_ssa *ssa, int var, int use)
126
0
{
127
0
  int next_use;
128
129
0
  if (ssa->vars[var].phi_use_chain) {
130
0
    zend_ssa_phi *phi = ssa->vars[var].phi_use_chain;
131
0
    do {
132
0
      if (!ssa->vars[phi->ssa_var].no_val) {
133
0
        return 0;
134
0
      }
135
0
      phi = zend_ssa_next_use_phi(ssa, var, phi);
136
0
    } while (phi);
137
0
  }
138
139
0
  if (ssa->cfg.blocks[ssa->cfg.map[use]].loop_header > 0
140
0
   || (ssa->cfg.blocks[ssa->cfg.map[use]].flags & ZEND_BB_LOOP_HEADER)) {
141
0
    int b = ssa->cfg.map[use];
142
0
    int prev_use = ssa->vars[var].use_chain;
143
0
    int def_block;
144
145
0
    if (ssa->vars[var].definition >= 0) {
146
0
      def_block =ssa->cfg.map[ssa->vars[var].definition];
147
0
    } else {
148
0
      ZEND_ASSERT(ssa->vars[var].definition_phi);
149
0
      def_block = ssa->vars[var].definition_phi->block;
150
0
    }
151
0
    if (dominates(ssa->cfg.blocks, def_block,
152
0
        (ssa->cfg.blocks[b].flags & ZEND_BB_LOOP_HEADER) ? b : ssa->cfg.blocks[b].loop_header)) {
153
0
      return 0;
154
0
    }
155
156
0
    while (prev_use >= 0 && prev_use != use) {
157
0
      if (b != ssa->cfg.map[prev_use]
158
0
       && dominates(ssa->cfg.blocks, b, ssa->cfg.map[prev_use])
159
0
       && !zend_ssa_is_no_val_use(op_array->opcodes + prev_use, ssa->ops + prev_use, var)) {
160
0
        return 0;
161
0
      }
162
0
      prev_use = zend_ssa_next_use(ssa->ops, var, prev_use);
163
0
    }
164
0
  }
165
166
0
  next_use = zend_ssa_next_use(ssa->ops, var, use);
167
0
  if (next_use < 0) {
168
0
    return 1;
169
0
  } else if (zend_ssa_is_no_val_use(op_array->opcodes + next_use, ssa->ops + next_use, var)) {
170
0
    return 1;
171
0
  }
172
0
  return 0;
173
0
}
174
175
static int zend_jit_is_constant_cmp_long_long(const zend_op  *opline,
176
                                              zend_ssa_range *op1_range,
177
                                              zend_jit_addr   op1_addr,
178
                                              zend_ssa_range *op2_range,
179
                                              zend_jit_addr   op2_addr,
180
                                              bool           *result)
181
0
{
182
0
  zend_long op1_min;
183
0
  zend_long op1_max;
184
0
  zend_long op2_min;
185
0
  zend_long op2_max;
186
187
0
  if (op1_range) {
188
0
    op1_min = op1_range->min;
189
0
    op1_max = op1_range->max;
190
0
  } else if (Z_MODE(op1_addr) == IS_CONST_ZVAL) {
191
0
    ZEND_ASSERT(Z_TYPE_P(Z_ZV(op1_addr)) == IS_LONG);
192
0
    op1_min = op1_max = Z_LVAL_P(Z_ZV(op1_addr));
193
0
  } else {
194
0
    return 0;
195
0
  }
196
197
0
  if (op2_range) {
198
0
    op2_min = op2_range->min;
199
0
    op2_max = op2_range->max;
200
0
  } else if (Z_MODE(op2_addr) == IS_CONST_ZVAL) {
201
0
    ZEND_ASSERT(Z_TYPE_P(Z_ZV(op2_addr)) == IS_LONG);
202
0
    op2_min = op2_max = Z_LVAL_P(Z_ZV(op2_addr));
203
0
  } else {
204
0
    return 0;
205
0
  }
206
207
0
  switch (opline->opcode) {
208
0
    case ZEND_IS_EQUAL:
209
0
    case ZEND_IS_IDENTICAL:
210
0
    case ZEND_CASE:
211
0
    case ZEND_CASE_STRICT:
212
0
      if (op1_min == op1_max && op2_min == op2_max && op1_min == op2_min) {
213
0
        *result = true;
214
0
        return 1;
215
0
      } else if (op1_max < op2_min || op1_min > op2_max) {
216
0
        *result = false;
217
0
        return 1;
218
0
      }
219
0
      return 0;
220
0
    case ZEND_IS_NOT_EQUAL:
221
0
    case ZEND_IS_NOT_IDENTICAL:
222
0
      if (op1_min == op1_max && op2_min == op2_max && op1_min == op2_min) {
223
0
        *result = false;
224
0
        return 1;
225
0
      } else if (op1_max < op2_min || op1_min > op2_max) {
226
0
        *result = true;
227
0
        return 1;
228
0
      }
229
0
      return 0;
230
0
    case ZEND_IS_SMALLER:
231
0
      if (op1_max < op2_min) {
232
0
        *result = true;
233
0
        return 1;
234
0
      } else if (op1_min >= op2_max) {
235
0
        *result = false;
236
0
        return 1;
237
0
      }
238
0
      return 0;
239
0
    case ZEND_IS_SMALLER_OR_EQUAL:
240
0
      if (op1_max <= op2_min) {
241
0
        *result = true;
242
0
        return 1;
243
0
      } else if (op1_min > op2_max) {
244
0
        *result = false;
245
0
        return 1;
246
0
      }
247
0
      return 0;
248
0
    default:
249
0
      ZEND_UNREACHABLE();
250
0
  }
251
0
  return 0;
252
0
}
253
254
#define ADVANCE_SSA_OP(ssa_op, offset) \
255
0
  do { \
256
0
    if (ssa_op) ssa_op += offset; \
257
0
  } while (0)
258
259
static int zend_jit_needs_call_chain(zend_call_info *call_info, uint32_t b, const zend_op_array *op_array, zend_ssa *ssa, const zend_ssa_op *ssa_op, const zend_op *opline, int call_level, zend_jit_trace_rec *trace)
260
0
{
261
0
  int skip;
262
263
0
  if (trace) {
264
0
    zend_jit_trace_rec *p = trace;
265
266
0
    ADVANCE_SSA_OP(ssa_op, 1);
267
0
    while (1) {
268
0
      if (p->op == ZEND_JIT_TRACE_VM) {
269
0
        switch (p->opline->opcode) {
270
0
          case ZEND_SEND_ARRAY:
271
0
          case ZEND_SEND_USER:
272
0
          case ZEND_SEND_UNPACK:
273
0
          case ZEND_INIT_FCALL:
274
0
          case ZEND_INIT_METHOD_CALL:
275
0
          case ZEND_INIT_STATIC_METHOD_CALL:
276
0
          case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL:
277
0
          case ZEND_INIT_FCALL_BY_NAME:
278
0
          case ZEND_INIT_NS_FCALL_BY_NAME:
279
0
          case ZEND_INIT_DYNAMIC_CALL:
280
0
          case ZEND_NEW:
281
0
          case ZEND_INIT_USER_CALL:
282
0
          case ZEND_FAST_CALL:
283
0
          case ZEND_JMP:
284
0
          case ZEND_JMPZ:
285
0
          case ZEND_JMPNZ:
286
0
          case ZEND_JMPZ_EX:
287
0
          case ZEND_JMPNZ_EX:
288
0
          case ZEND_FE_RESET_R:
289
0
          case ZEND_FE_RESET_RW:
290
0
          case ZEND_JMP_SET:
291
0
          case ZEND_COALESCE:
292
0
          case ZEND_JMP_NULL:
293
0
          case ZEND_ASSERT_CHECK:
294
0
          case ZEND_CATCH:
295
0
          case ZEND_DECLARE_ANON_CLASS:
296
0
          case ZEND_FE_FETCH_R:
297
0
          case ZEND_FE_FETCH_RW:
298
0
          case ZEND_BIND_INIT_STATIC_OR_JMP:
299
0
          case ZEND_JMP_FRAMELESS:
300
0
            return 1;
301
0
          case ZEND_DO_ICALL:
302
0
          case ZEND_DO_UCALL:
303
0
          case ZEND_DO_FCALL_BY_NAME:
304
0
          case ZEND_DO_FCALL:
305
0
          case ZEND_CALLABLE_CONVERT:
306
0
            return 0;
307
0
          case ZEND_SEND_VAL:
308
0
          case ZEND_SEND_VAR:
309
0
          case ZEND_SEND_VAL_EX:
310
0
          case ZEND_SEND_VAR_EX:
311
0
          case ZEND_SEND_FUNC_ARG:
312
0
          case ZEND_SEND_REF:
313
0
          case ZEND_SEND_VAR_NO_REF:
314
0
          case ZEND_SEND_VAR_NO_REF_EX:
315
            /* skip */
316
0
            break;
317
0
          default:
318
0
            if (zend_may_throw(opline, ssa_op, op_array, ssa)) {
319
0
              return 1;
320
0
            }
321
0
        }
322
0
        ADVANCE_SSA_OP(ssa_op, zend_jit_trace_op_len(opline));
323
0
      } else if (p->op == ZEND_JIT_TRACE_ENTER ||
324
0
                 p->op == ZEND_JIT_TRACE_BACK ||
325
0
                 p->op == ZEND_JIT_TRACE_END) {
326
0
        return 1;
327
0
      }
328
0
      p++;
329
0
    }
330
0
  }
331
332
0
  if (!call_info) {
333
0
    const zend_op *end = op_array->opcodes + op_array->last;
334
335
0
    opline++;
336
0
    ADVANCE_SSA_OP(ssa_op, 1);
337
0
    skip = (call_level == 1);
338
0
    while (opline != end) {
339
0
      if (!skip) {
340
0
        if (zend_may_throw(opline, ssa_op, op_array, ssa)) {
341
0
          return 1;
342
0
        }
343
0
      }
344
0
      switch (opline->opcode) {
345
0
        case ZEND_SEND_VAL:
346
0
        case ZEND_SEND_VAR:
347
0
        case ZEND_SEND_VAL_EX:
348
0
        case ZEND_SEND_VAR_EX:
349
0
        case ZEND_SEND_FUNC_ARG:
350
0
        case ZEND_SEND_REF:
351
0
        case ZEND_SEND_VAR_NO_REF:
352
0
        case ZEND_SEND_VAR_NO_REF_EX:
353
0
          skip = 0;
354
0
          break;
355
0
        case ZEND_SEND_ARRAY:
356
0
        case ZEND_SEND_USER:
357
0
        case ZEND_SEND_UNPACK:
358
0
        case ZEND_INIT_FCALL:
359
0
        case ZEND_INIT_METHOD_CALL:
360
0
        case ZEND_INIT_STATIC_METHOD_CALL:
361
0
        case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL:
362
0
        case ZEND_INIT_FCALL_BY_NAME:
363
0
        case ZEND_INIT_NS_FCALL_BY_NAME:
364
0
        case ZEND_INIT_DYNAMIC_CALL:
365
0
        case ZEND_NEW:
366
0
        case ZEND_INIT_USER_CALL:
367
0
        case ZEND_FAST_CALL:
368
0
        case ZEND_JMP:
369
0
        case ZEND_JMPZ:
370
0
        case ZEND_JMPNZ:
371
0
        case ZEND_JMPZ_EX:
372
0
        case ZEND_JMPNZ_EX:
373
0
        case ZEND_FE_RESET_R:
374
0
        case ZEND_FE_RESET_RW:
375
0
        case ZEND_JMP_SET:
376
0
        case ZEND_COALESCE:
377
0
        case ZEND_JMP_NULL:
378
0
        case ZEND_ASSERT_CHECK:
379
0
        case ZEND_CATCH:
380
0
        case ZEND_DECLARE_ANON_CLASS:
381
0
        case ZEND_FE_FETCH_R:
382
0
        case ZEND_FE_FETCH_RW:
383
0
        case ZEND_BIND_INIT_STATIC_OR_JMP:
384
0
        case ZEND_JMP_FRAMELESS:
385
0
          return 1;
386
0
        case ZEND_DO_ICALL:
387
0
        case ZEND_DO_UCALL:
388
0
        case ZEND_DO_FCALL_BY_NAME:
389
0
        case ZEND_DO_FCALL:
390
0
        case ZEND_CALLABLE_CONVERT:
391
0
          end = opline;
392
0
          if (end - op_array->opcodes >= ssa->cfg.blocks[b].start + ssa->cfg.blocks[b].len) {
393
            /* INIT_FCALL and DO_FCALL in different BasicBlocks */
394
0
            return 1;
395
0
          }
396
0
          return 0;
397
0
      }
398
0
      opline++;
399
0
      ADVANCE_SSA_OP(ssa_op, 1);
400
0
    }
401
402
0
    return 1;
403
0
  } else {
404
0
    const zend_op *end = call_info->caller_call_opline;
405
406
    /* end may be null if an opcode like EXIT is part of the argument list. */
407
0
    if (!end || end - op_array->opcodes >= ssa->cfg.blocks[b].start + ssa->cfg.blocks[b].len) {
408
      /* INIT_FCALL and DO_FCALL in different BasicBlocks */
409
0
      return 1;
410
0
    }
411
412
0
    opline++;
413
0
    ADVANCE_SSA_OP(ssa_op, 1);
414
0
    skip = (call_level == 1);
415
0
    while (opline != end) {
416
0
      if (skip) {
417
0
        switch (opline->opcode) {
418
0
          case ZEND_SEND_VAL:
419
0
          case ZEND_SEND_VAR:
420
0
          case ZEND_SEND_VAL_EX:
421
0
          case ZEND_SEND_VAR_EX:
422
0
          case ZEND_SEND_FUNC_ARG:
423
0
          case ZEND_SEND_REF:
424
0
          case ZEND_SEND_VAR_NO_REF:
425
0
          case ZEND_SEND_VAR_NO_REF_EX:
426
0
            skip = 0;
427
0
            break;
428
0
          case ZEND_SEND_ARRAY:
429
0
          case ZEND_SEND_USER:
430
0
          case ZEND_SEND_UNPACK:
431
0
            return 1;
432
0
        }
433
0
      } else {
434
0
        if (zend_may_throw(opline, ssa_op, op_array, ssa)) {
435
0
          return 1;
436
0
        }
437
0
      }
438
0
      opline++;
439
0
      ADVANCE_SSA_OP(ssa_op, 1);
440
0
    }
441
442
0
    return 0;
443
0
  }
444
0
}
445
446
static uint32_t skip_valid_arguments(const zend_op_array *op_array, zend_ssa *ssa, const zend_call_info *call_info)
447
0
{
448
0
  uint32_t num_args = 0;
449
0
  zend_function *func = call_info->callee_func;
450
451
  /* It's okay to handle prototypes here, because they can only increase the accepted arguments.
452
   * Anything legal for the parent method is also legal for the parent method. */
453
0
  while (num_args < call_info->num_args) {
454
0
    zend_arg_info *arg_info = func->op_array.arg_info + num_args;
455
456
0
    if (ZEND_TYPE_IS_SET(arg_info->type)) {
457
0
      if (ZEND_TYPE_IS_ONLY_MASK(arg_info->type)) {
458
0
        zend_op *opline = call_info->arg_info[num_args].opline;
459
0
        zend_ssa_op *ssa_op = ssa->ops ? &ssa->ops[opline - op_array->opcodes] : NULL;
460
0
        uint32_t type_mask = ZEND_TYPE_PURE_MASK(arg_info->type);
461
0
        if ((OP1_INFO() & (MAY_BE_ANY|MAY_BE_UNDEF)) & ~type_mask) {
462
0
          break;
463
0
        }
464
0
      } else {
465
0
        break;
466
0
      }
467
0
    }
468
0
    num_args++;
469
0
  }
470
0
  return num_args;
471
0
}
472
473
static uint32_t zend_ssa_cv_info(const zend_op_array *op_array, zend_ssa *ssa, uint32_t var)
474
0
{
475
0
  uint32_t j, info;
476
477
0
  if (ssa->vars && ssa->var_info) {
478
0
    info = ssa->var_info[var].type;
479
0
    for (j = op_array->last_var; j < ssa->vars_count; j++) {
480
0
      if (ssa->vars[j].var == var) {
481
0
        info |= ssa->var_info[j].type;
482
0
      }
483
0
    }
484
0
  } else {
485
0
    info = MAY_BE_RC1 | MAY_BE_RCN | MAY_BE_REF | MAY_BE_ANY | MAY_BE_UNDEF |
486
0
      MAY_BE_ARRAY_KEY_ANY | MAY_BE_ARRAY_OF_ANY | MAY_BE_ARRAY_OF_REF;
487
0
  }
488
489
0
#ifdef ZEND_JIT_USE_RC_INFERENCE
490
  /* Refcount may be increased by RETURN opcode */
491
0
  if ((info & MAY_BE_RC1) && !(info & MAY_BE_RCN)) {
492
0
    for (j = 0; j < ssa->cfg.blocks_count; j++) {
493
0
      if ((ssa->cfg.blocks[j].flags & ZEND_BB_REACHABLE) &&
494
0
          ssa->cfg.blocks[j].len > 0) {
495
0
        const zend_op *opline = op_array->opcodes + ssa->cfg.blocks[j].start + ssa->cfg.blocks[j].len - 1;
496
497
0
        if (opline->opcode == ZEND_RETURN) {
498
0
          if (opline->op1_type == IS_CV && opline->op1.var == EX_NUM_TO_VAR(var)) {
499
0
            info |= MAY_BE_RCN;
500
0
            break;
501
0
          }
502
0
        }
503
0
      }
504
0
    }
505
0
  }
506
0
#endif
507
508
0
  return info;
509
0
}
510
511
static bool zend_jit_may_avoid_refcounting(const zend_op *opline, uint32_t op1_info)
512
0
{
513
0
  switch (opline->opcode) {
514
0
    case ZEND_FETCH_OBJ_FUNC_ARG:
515
0
      if (!JIT_G(current_frame) ||
516
0
          !JIT_G(current_frame)->call->func ||
517
0
          !TRACE_FRAME_IS_LAST_SEND_BY_VAL(JIT_G(current_frame)->call)) {
518
0
        return 0;
519
0
      }
520
      /* break missing intentionally */
521
0
    case ZEND_FETCH_OBJ_R:
522
0
    case ZEND_FETCH_OBJ_IS:
523
0
      if ((op1_info & MAY_BE_OBJECT)
524
0
       && opline->op2_type == IS_CONST
525
0
       && Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) == IS_STRING
526
0
       && Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] != '\0') {
527
0
        return 1;
528
0
      }
529
0
      break;
530
0
    case ZEND_FETCH_DIM_FUNC_ARG:
531
0
      if (!JIT_G(current_frame) ||
532
0
          !JIT_G(current_frame)->call->func ||
533
0
          !TRACE_FRAME_IS_LAST_SEND_BY_VAL(JIT_G(current_frame)->call)) {
534
0
        return 0;
535
0
      }
536
      /* break missing intentionally */
537
0
    case ZEND_FETCH_DIM_R:
538
0
    case ZEND_FETCH_DIM_IS:
539
0
      return 1;
540
0
    case ZEND_ISSET_ISEMPTY_DIM_OBJ:
541
0
      if (!(opline->extended_value & ZEND_ISEMPTY)) {
542
0
        return 1;
543
0
      }
544
0
      break;
545
0
  }
546
0
  return 0;
547
0
}
548
549
static bool zend_jit_is_persistent_constant(zval *key, uint32_t flags)
550
0
{
551
0
  zval *zv;
552
0
  zend_constant *c = NULL;
553
554
  /* null/true/false are resolved during compilation, so don't check for them here. */
555
0
  zv = zend_hash_find_known_hash(EG(zend_constants), Z_STR_P(key));
556
0
  if (zv) {
557
0
    c = (zend_constant*)Z_PTR_P(zv);
558
0
  } else if (flags & IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE) {
559
0
    key++;
560
0
    zv = zend_hash_find_known_hash(EG(zend_constants), Z_STR_P(key));
561
0
    if (zv) {
562
0
      c = (zend_constant*)Z_PTR_P(zv);
563
0
    }
564
0
  }
565
0
  return c && (ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT);
566
0
}
567
568
static zend_class_entry* zend_get_known_class(const zend_op_array *op_array, const zend_op *opline, uint8_t op_type, znode_op op)
569
0
{
570
0
  zend_class_entry *ce = NULL;
571
572
0
  if (op_type == IS_CONST) {
573
0
    zval *zv = RT_CONSTANT(opline, op);
574
0
    zend_string *class_name;
575
576
0
    ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
577
0
    class_name = Z_STR_P(zv);
578
0
    ce = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
579
0
    if (ce && (ce->type == ZEND_INTERNAL_CLASS || ce->info.user.filename != op_array->filename)) {
580
0
      ce = NULL;
581
0
    }
582
0
  } else {
583
0
    ZEND_ASSERT(op_type == IS_UNUSED);
584
0
    if ((op.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF) {
585
0
      ce = op_array->scope;
586
0
    } else {
587
0
      ZEND_ASSERT((op.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_PARENT);
588
0
      ce = op_array->scope;
589
0
      if (ce) {
590
0
        if (ce->parent) {
591
0
          ce = ce->parent;
592
0
          if (ce->type == ZEND_INTERNAL_CLASS || ce->info.user.filename != op_array->filename) {
593
0
            ce = NULL;
594
0
          }
595
0
        } else {
596
0
          ce = NULL;
597
0
        }
598
0
      }
599
0
    }
600
0
  }
601
602
0
  return ce;
603
0
}
604
605
static zend_property_info* zend_get_known_property_info(const zend_op_array *op_array, zend_class_entry *ce, zend_string *member, bool on_this, zend_string *filename)
606
0
{
607
0
  zend_property_info *info = NULL;
608
609
0
  if ((on_this && (op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) ||
610
0
      !ce ||
611
0
      !(ce->ce_flags & ZEND_ACC_LINKED) ||
612
0
      (ce->ce_flags & ZEND_ACC_TRAIT) ||
613
0
      ce->create_object) {
614
0
    return NULL;
615
0
  }
616
617
0
  if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
618
0
    if (ce->info.user.filename != filename) {
619
      /* class declaration might be changed independently */
620
0
      return NULL;
621
0
    }
622
623
0
    if (ce->parent) {
624
0
      zend_class_entry *parent = ce->parent;
625
626
0
      do {
627
0
        if (parent->type == ZEND_INTERNAL_CLASS) {
628
0
          break;
629
0
        } else if (parent->info.user.filename != filename) {
630
          /* some of parents class declarations might be changed independently */
631
          /* TODO: this check may be not enough, because even
632
           * in the same it's possible to conditionally define
633
           * few classes with the same name, and "parent" may
634
           * change from request to request.
635
           */
636
0
          return NULL;
637
0
        }
638
0
        parent = parent->parent;
639
0
      } while (parent);
640
0
    }
641
0
  }
642
643
  // TODO: Treat property hooks more precisely.
644
0
  info = (zend_property_info*)zend_hash_find_ptr(&ce->properties_info, member);
645
0
  if (info == NULL ||
646
0
      !IS_VALID_PROPERTY_OFFSET(info->offset) ||
647
0
      (info->flags & ZEND_ACC_STATIC) ||
648
0
      info->hooks) {
649
0
    return NULL;
650
0
  }
651
652
0
  if (info->flags & ZEND_ACC_PUBLIC) {
653
0
    return info;
654
0
  } else if (on_this) {
655
0
    if (ce == info->ce) {
656
0
      if (ce == op_array->scope) {
657
0
        return info;
658
0
      } else {
659
0
        return NULL;
660
0
      }
661
0
    } else if ((info->flags & ZEND_ACC_PROTECTED)
662
0
        && instanceof_function_slow(ce, info->ce)) {
663
0
      return info;
664
0
    }
665
0
  }
666
667
0
  return NULL;
668
0
}
669
670
static bool zend_may_be_dynamic_property(zend_class_entry *ce, zend_string *member, bool on_this, const zend_op_array *op_array)
671
0
{
672
0
  zend_property_info *info;
673
674
0
  if (!ce || (ce->ce_flags & ZEND_ACC_TRAIT) || (op_array->fn_flags & ZEND_ACC_TRAIT_CLONE)) {
675
0
    return 1;
676
0
  }
677
678
0
  if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
679
0
    if (ce->info.user.filename != op_array->filename) {
680
      /* class declaration might be changed independently */
681
0
      return 1;
682
0
    }
683
0
  }
684
685
  // TODO: Treat property hooks more precisely.
686
0
  info = (zend_property_info*)zend_hash_find_ptr(&ce->properties_info, member);
687
0
  if (info == NULL ||
688
0
      !IS_VALID_PROPERTY_OFFSET(info->offset) ||
689
0
      (info->flags & ZEND_ACC_STATIC) ||
690
0
      info->hooks) {
691
0
    return 1;
692
0
  }
693
694
0
  if (!(info->flags & ZEND_ACC_PUBLIC) &&
695
0
      (!on_this || info->ce != ce)) {
696
0
    return 1;
697
0
  }
698
699
0
  return 0;
700
0
}
701
702
static bool zend_jit_class_may_be_modified(const zend_class_entry *ce, const zend_op_array *called_from)
703
0
{
704
0
  uint32_t i;
705
706
0
  if (ce->type == ZEND_INTERNAL_CLASS) {
707
#ifdef _WIN32
708
    /* ASLR */
709
    return 1;
710
#else
711
0
    return 0;
712
0
#endif
713
0
  } else if (ce->type == ZEND_USER_CLASS) {
714
0
    if (ce->ce_flags & ZEND_ACC_PRELOADED) {
715
0
      return 0;
716
0
    }
717
0
    if (ce->info.user.filename == called_from->filename) {
718
0
      if (ce->parent
719
0
       && (!(ce->ce_flags & ZEND_ACC_LINKED)
720
0
        || zend_jit_class_may_be_modified(ce->parent, called_from))) {
721
0
        return 1;
722
0
      }
723
0
      if (ce->num_interfaces) {
724
0
        if (!(ce->ce_flags & ZEND_ACC_LINKED)) {
725
0
          return 1;
726
0
        }
727
0
        for (i = 0; i < ce->num_interfaces; i++) {
728
0
          if (zend_jit_class_may_be_modified(ce->interfaces[i], called_from)) {
729
0
            return 1;
730
0
          }
731
0
        }
732
0
      }
733
0
      if (ce->num_traits) {
734
0
        if (!(ce->ce_flags & ZEND_ACC_LINKED)) {
735
0
          return 1;
736
0
        }
737
0
        for (i=0; i < ce->num_traits; i++) {
738
0
          zend_class_entry *trait = zend_fetch_class_by_name(ce->trait_names[i].name,
739
0
            ce->trait_names[i].lc_name,
740
0
            ZEND_FETCH_CLASS_TRAIT | ZEND_FETCH_CLASS_NO_AUTOLOAD | ZEND_FETCH_CLASS_SILENT);
741
0
          if (!trait || zend_jit_class_may_be_modified(trait, called_from)) {
742
0
            return 1;
743
0
          }
744
0
        }
745
0
      }
746
0
      return 0;
747
0
    }
748
0
  }
749
0
  return 1;
750
0
}
751
752
static bool zend_jit_may_be_modified(const zend_function *func, const zend_op_array *called_from)
753
0
{
754
0
  if (func->type == ZEND_INTERNAL_FUNCTION) {
755
#ifdef _WIN32
756
    /* ASLR */
757
    return 1;
758
#else
759
0
    return 0;
760
0
#endif
761
0
  } else if (func->type == ZEND_USER_FUNCTION) {
762
0
    if (func->common.fn_flags & ZEND_ACC_PRELOADED) {
763
0
      return 0;
764
0
    }
765
0
    if (func->op_array.filename == called_from->filename
766
0
     && (!func->op_array.scope
767
0
      || !zend_jit_class_may_be_modified(func->op_array.scope, called_from))) {
768
0
      return 0;
769
0
    }
770
0
  }
771
0
  return 1;
772
0
}
773
774
#define OP_RANGE(ssa_op, opN) \
775
0
  (((opline->opN##_type & (IS_TMP_VAR|IS_VAR|IS_CV)) && \
776
0
    ssa->var_info && \
777
0
    (ssa_op)->opN##_use >= 0 && \
778
0
    ssa->var_info[(ssa_op)->opN##_use].has_range) ? \
779
0
   &ssa->var_info[(ssa_op)->opN##_use].range : NULL)
780
781
0
#define OP1_RANGE()      OP_RANGE(ssa_op, op1)
782
0
#define OP2_RANGE()      OP_RANGE(ssa_op, op2)
783
0
#define OP1_DATA_RANGE() OP_RANGE(ssa_op + 1, op1)
784
785
#include "jit/zend_jit_helpers.c"
786
#include "Zend/zend_cpuinfo.h"
787
788
#ifdef HAVE_GCC_GLOBAL_REGS
789
# define GCC_GLOBAL_REGS 1
790
#else
791
0
# define GCC_GLOBAL_REGS 0
792
#endif
793
794
/* By default avoid JITing inline handlers if it does not seem profitable due to lack of
795
 * type information. Disabling this option allows testing some JIT handlers in the
796
 * presence of try/catch blocks, which prevent SSA construction. */
797
#ifndef PROFITABILITY_CHECKS
798
# define PROFITABILITY_CHECKS 1
799
#endif
800
801
0
#define BP_JIT_IS 6 /* Used for ISSET_ISEMPTY_DIM_OBJ. see BP_VAR_*defines in Zend/zend_compile.h */
802
803
/* The generated code may contain tautological comparisons, ignore them. */
804
#if defined(__clang__)
805
# pragma clang diagnostic push
806
# pragma clang diagnostic ignored "-Wtautological-compare"
807
# pragma clang diagnostic ignored "-Wstring-compare"
808
#endif
809
810
#include "jit/zend_jit_ir.c"
811
812
#if defined(__clang__)
813
# pragma clang diagnostic pop
814
#endif
815
816
#ifdef _WIN32
817
# include <Windows.h>
818
#else
819
# include <sys/mman.h>
820
# if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
821
#   define MAP_ANONYMOUS MAP_ANON
822
# endif
823
#endif
824
825
ZEND_EXT_API void zend_jit_status(zval *ret)
826
15
{
827
15
  zval stats;
828
15
  array_init(&stats);
829
15
  add_assoc_bool(&stats, "enabled", JIT_G(enabled));
830
15
  add_assoc_bool(&stats, "on", JIT_G(on));
831
15
  add_assoc_long(&stats, "kind", JIT_G(trigger));
832
15
  add_assoc_long(&stats, "opt_level", JIT_G(opt_level));
833
15
  add_assoc_long(&stats, "opt_flags", JIT_G(opt_flags));
834
15
  if (dasm_buf) {
835
0
    add_assoc_long(&stats, "buffer_size", (char*)dasm_end - (char*)dasm_buf);
836
0
    add_assoc_long(&stats, "buffer_free", (char*)dasm_end - (char*)*dasm_ptr);
837
15
  } else {
838
15
    add_assoc_long(&stats, "buffer_size", 0);
839
15
    add_assoc_long(&stats, "buffer_free", 0);
840
15
  }
841
15
  add_assoc_zval(ret, "jit", &stats);
842
15
}
843
844
static bool zend_jit_inc_call_level(uint8_t opcode)
845
0
{
846
0
  switch (opcode) {
847
0
    case ZEND_INIT_FCALL:
848
0
    case ZEND_INIT_FCALL_BY_NAME:
849
0
    case ZEND_INIT_NS_FCALL_BY_NAME:
850
0
    case ZEND_INIT_METHOD_CALL:
851
0
    case ZEND_INIT_DYNAMIC_CALL:
852
0
    case ZEND_INIT_STATIC_METHOD_CALL:
853
0
    case ZEND_INIT_PARENT_PROPERTY_HOOK_CALL:
854
0
    case ZEND_INIT_USER_CALL:
855
0
    case ZEND_NEW:
856
0
      return true;
857
0
    default:
858
0
      return false;
859
0
  }
860
0
}
861
862
static bool zend_jit_dec_call_level(uint8_t opcode)
863
0
{
864
0
  switch (opcode) {
865
0
    case ZEND_DO_FCALL:
866
0
    case ZEND_DO_ICALL:
867
0
    case ZEND_DO_UCALL:
868
0
    case ZEND_DO_FCALL_BY_NAME:
869
0
    case ZEND_CALLABLE_CONVERT:
870
0
      return true;
871
0
    default:
872
0
      return false;
873
0
  }
874
0
}
875
876
static zend_string *zend_jit_func_name(const zend_op_array *op_array)
877
0
{
878
0
  smart_str buf = {0};
879
880
0
  if (op_array->function_name) {
881
0
    smart_str_appends(&buf, JIT_PREFIX);
882
0
    if (op_array->scope) {
883
0
      smart_str_appendl(&buf, ZSTR_VAL(op_array->scope->name), ZSTR_LEN(op_array->scope->name));
884
0
      smart_str_appends(&buf, "::");
885
0
    }
886
0
    smart_str_appendl(&buf, ZSTR_VAL(op_array->function_name), ZSTR_LEN(op_array->function_name));
887
0
    if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
888
0
      smart_str_appends(&buf, ":");
889
0
      smart_str_appendl(&buf, ZSTR_VAL(op_array->filename), ZSTR_LEN(op_array->filename));
890
0
      smart_str_appends(&buf, ":");
891
0
      smart_str_append_long(&buf, op_array->line_start);
892
0
    }
893
0
    smart_str_0(&buf);
894
0
    return buf.s;
895
0
  } else if (op_array->filename) {
896
0
    smart_str_appends(&buf, JIT_PREFIX);
897
0
    smart_str_appendl(&buf, ZSTR_VAL(op_array->filename), ZSTR_LEN(op_array->filename));
898
0
    smart_str_0(&buf);
899
0
    return buf.s;
900
0
  } else {
901
0
    return NULL;
902
0
  }
903
0
}
904
905
static int zend_may_overflow(const zend_op *opline, const zend_ssa_op *ssa_op, const zend_op_array *op_array, zend_ssa *ssa)
906
0
{
907
0
  int res;
908
0
  zend_long op1_min, op1_max, op2_min, op2_max;
909
910
0
  if (!ssa->ops || !ssa->var_info) {
911
0
    return 1;
912
0
  }
913
0
  switch (opline->opcode) {
914
0
    case ZEND_PRE_INC:
915
0
    case ZEND_POST_INC:
916
0
      res = ssa_op->op1_def;
917
0
      if (res < 0
918
0
       || !ssa->var_info[res].has_range
919
0
       || ssa->var_info[res].range.overflow) {
920
0
        if (!OP1_HAS_RANGE()) {
921
0
          return 1;
922
0
        }
923
0
        op1_max = OP1_MAX_RANGE();
924
0
        if (op1_max == ZEND_LONG_MAX) {
925
0
          return 1;
926
0
        }
927
0
      }
928
0
      return 0;
929
0
    case ZEND_PRE_DEC:
930
0
    case ZEND_POST_DEC:
931
0
      res = ssa_op->op1_def;
932
0
      if (res < 0
933
0
       || !ssa->var_info[res].has_range
934
0
       || ssa->var_info[res].range.underflow) {
935
0
        if (!OP1_HAS_RANGE()) {
936
0
          return 1;
937
0
        }
938
0
        op1_min = OP1_MIN_RANGE();
939
0
        if (op1_min == ZEND_LONG_MIN) {
940
0
          return 1;
941
0
        }
942
0
      }
943
0
      return 0;
944
0
    case ZEND_ADD:
945
0
      res = ssa_op->result_def;
946
0
      if (res < 0
947
0
       || !ssa->var_info[res].has_range
948
0
       || ssa->var_info[res].range.underflow) {
949
0
        if (!OP1_HAS_RANGE() || !OP2_HAS_RANGE()) {
950
0
          return 1;
951
0
        }
952
0
        op1_min = OP1_MIN_RANGE();
953
0
        op2_min = OP2_MIN_RANGE();
954
0
        if (zend_add_will_overflow(op1_min, op2_min)) {
955
0
          return 1;
956
0
        }
957
0
      }
958
0
      if (res < 0
959
0
       || !ssa->var_info[res].has_range
960
0
       || ssa->var_info[res].range.overflow) {
961
0
        if (!OP1_HAS_RANGE() || !OP2_HAS_RANGE()) {
962
0
          return 1;
963
0
        }
964
0
        op1_max = OP1_MAX_RANGE();
965
0
        op2_max = OP2_MAX_RANGE();
966
0
        if (zend_add_will_overflow(op1_max, op2_max)) {
967
0
          return 1;
968
0
        }
969
0
      }
970
0
      return 0;
971
0
    case ZEND_SUB:
972
0
      res = ssa_op->result_def;
973
0
      if (res < 0
974
0
       || !ssa->var_info[res].has_range
975
0
       || ssa->var_info[res].range.underflow) {
976
0
        if (!OP1_HAS_RANGE() || !OP2_HAS_RANGE()) {
977
0
          return 1;
978
0
        }
979
0
        op1_min = OP1_MIN_RANGE();
980
0
        op2_max = OP2_MAX_RANGE();
981
0
        if (zend_sub_will_overflow(op1_min, op2_max)) {
982
0
          return 1;
983
0
        }
984
0
      }
985
0
      if (res < 0
986
0
       || !ssa->var_info[res].has_range
987
0
       || ssa->var_info[res].range.overflow) {
988
0
        if (!OP1_HAS_RANGE() || !OP2_HAS_RANGE()) {
989
0
          return 1;
990
0
        }
991
0
        op1_max = OP1_MAX_RANGE();
992
0
        op2_min = OP2_MIN_RANGE();
993
0
        if (zend_sub_will_overflow(op1_max, op2_min)) {
994
0
          return 1;
995
0
        }
996
0
      }
997
0
      return 0;
998
0
    case ZEND_MUL:
999
0
      res = ssa_op->result_def;
1000
0
      return (res < 0 ||
1001
0
        !ssa->var_info[res].has_range ||
1002
0
        ssa->var_info[res].range.underflow ||
1003
0
        ssa->var_info[res].range.overflow);
1004
0
    case ZEND_ASSIGN_OP:
1005
0
      if (opline->extended_value == ZEND_ADD) {
1006
0
        res = ssa_op->op1_def;
1007
0
        if (res < 0
1008
0
         || !ssa->var_info[res].has_range
1009
0
         || ssa->var_info[res].range.underflow) {
1010
0
          if (!OP1_HAS_RANGE() || !OP2_HAS_RANGE()) {
1011
0
            return 1;
1012
0
          }
1013
0
          op1_min = OP1_MIN_RANGE();
1014
0
          op2_min = OP2_MIN_RANGE();
1015
0
          if (zend_add_will_overflow(op1_min, op2_min)) {
1016
0
            return 1;
1017
0
          }
1018
0
        }
1019
0
        if (res < 0
1020
0
         || !ssa->var_info[res].has_range
1021
0
         || ssa->var_info[res].range.overflow) {
1022
0
          if (!OP1_HAS_RANGE() || !OP2_HAS_RANGE()) {
1023
0
            return 1;
1024
0
          }
1025
0
          op1_max = OP1_MAX_RANGE();
1026
0
          op2_max = OP2_MAX_RANGE();
1027
0
          if (zend_add_will_overflow(op1_max, op2_max)) {
1028
0
            return 1;
1029
0
          }
1030
0
        }
1031
0
        return 0;
1032
0
      } else if (opline->extended_value == ZEND_SUB) {
1033
0
        res = ssa_op->op1_def;
1034
0
        if (res < 0
1035
0
         || !ssa->var_info[res].has_range
1036
0
         || ssa->var_info[res].range.underflow) {
1037
0
          if (!OP1_HAS_RANGE() || !OP2_HAS_RANGE()) {
1038
0
            return 1;
1039
0
          }
1040
0
          op1_min = OP1_MIN_RANGE();
1041
0
          op2_max = OP2_MAX_RANGE();
1042
0
          if (zend_sub_will_overflow(op1_min, op2_max)) {
1043
0
            return 1;
1044
0
          }
1045
0
        }
1046
0
        if (res < 0
1047
0
         || !ssa->var_info[res].has_range
1048
0
         || ssa->var_info[res].range.overflow) {
1049
0
          if (!OP1_HAS_RANGE() || !OP2_HAS_RANGE()) {
1050
0
            return 1;
1051
0
          }
1052
0
          op1_max = OP1_MAX_RANGE();
1053
0
          op2_min = OP2_MIN_RANGE();
1054
0
          if (zend_sub_will_overflow(op1_max, op2_min)) {
1055
0
            return 1;
1056
0
          }
1057
0
        }
1058
0
        return 0;
1059
0
      } else if (opline->extended_value == ZEND_MUL) {
1060
0
        res = ssa_op->op1_def;
1061
0
        return (res < 0 ||
1062
0
          !ssa->var_info[res].has_range ||
1063
0
          ssa->var_info[res].range.underflow ||
1064
0
          ssa->var_info[res].range.overflow);
1065
0
      }
1066
0
      ZEND_FALLTHROUGH;
1067
0
    default:
1068
0
      return 1;
1069
0
  }
1070
0
}
1071
1072
static int zend_jit_build_cfg(const zend_op_array *op_array, zend_cfg *cfg)
1073
0
{
1074
0
  uint32_t flags;
1075
1076
0
  flags = ZEND_CFG_STACKLESS | ZEND_CFG_NO_ENTRY_PREDECESSORS | ZEND_SSA_RC_INFERENCE_FLAG | ZEND_SSA_USE_CV_RESULTS | ZEND_CFG_RECV_ENTRY;
1077
1078
0
  zend_build_cfg(&CG(arena), op_array, flags, cfg);
1079
1080
  /* Don't JIT huge functions. Apart from likely being detrimental due to the amount of
1081
   * generated code, some of our analysis is recursive and will stack overflow with many
1082
   * blocks. */
1083
0
  if (cfg->blocks_count > 100000) {
1084
0
    return FAILURE;
1085
0
  }
1086
1087
0
  zend_cfg_build_predecessors(&CG(arena), cfg);
1088
1089
  /* Compute Dominators Tree */
1090
0
  zend_cfg_compute_dominators_tree(op_array, cfg);
1091
1092
  /* Identify reducible and irreducible loops */
1093
0
  zend_cfg_identify_loops(op_array, cfg);
1094
1095
0
  return SUCCESS;
1096
0
}
1097
1098
static int zend_jit_op_array_analyze1(const zend_op_array *op_array, zend_script *script, zend_ssa *ssa)
1099
0
{
1100
0
  if (zend_jit_build_cfg(op_array, &ssa->cfg) != SUCCESS) {
1101
0
    return FAILURE;
1102
0
  }
1103
1104
#if 0
1105
  /* TODO: debugger and profiler supports? */
1106
  if ((ssa->cfg.flags & ZEND_FUNC_HAS_EXTENDED_INFO)) {
1107
    return FAILURE;
1108
  }
1109
#endif
1110
1111
  /* TODO: move this to zend_cfg.c ? */
1112
0
  if (!op_array->function_name) {
1113
0
    ssa->cfg.flags |= ZEND_FUNC_INDIRECT_VAR_ACCESS;
1114
0
  }
1115
1116
0
  if ((JIT_G(opt_level) >= ZEND_JIT_LEVEL_OPT_FUNC)
1117
0
   && ssa->cfg.blocks
1118
0
   && op_array->last_try_catch == 0
1119
0
   && !(op_array->fn_flags & ZEND_ACC_GENERATOR)
1120
0
   && !(ssa->cfg.flags & ZEND_FUNC_INDIRECT_VAR_ACCESS)) {
1121
0
    if (zend_build_ssa(&CG(arena), script, op_array, ZEND_SSA_RC_INFERENCE | ZEND_SSA_USE_CV_RESULTS, ssa) != SUCCESS) {
1122
0
      return FAILURE;
1123
0
    }
1124
1125
0
    zend_ssa_compute_use_def_chains(&CG(arena), op_array, ssa);
1126
1127
0
    zend_ssa_find_false_dependencies(op_array, ssa);
1128
1129
0
    zend_ssa_find_sccs(op_array, ssa);
1130
0
  }
1131
1132
0
  return SUCCESS;
1133
0
}
1134
1135
static int zend_jit_op_array_analyze2(const zend_op_array *op_array, zend_script *script, zend_ssa *ssa, uint32_t optimization_level)
1136
0
{
1137
0
  if ((JIT_G(opt_level) >= ZEND_JIT_LEVEL_OPT_FUNC)
1138
0
   && ssa->cfg.blocks
1139
0
   && op_array->last_try_catch == 0
1140
0
   && !(op_array->fn_flags & ZEND_ACC_GENERATOR)
1141
0
   && !(ssa->cfg.flags & ZEND_FUNC_INDIRECT_VAR_ACCESS)) {
1142
0
    if (zend_ssa_inference(&CG(arena), op_array, script, ssa,
1143
0
        optimization_level & ~ZEND_OPTIMIZER_NARROW_TO_DOUBLE) != SUCCESS) {
1144
0
      return FAILURE;
1145
0
    }
1146
0
  }
1147
1148
0
  return SUCCESS;
1149
0
}
1150
1151
static void zend_jit_allocate_registers(zend_jit_ctx *ctx, const zend_op_array *op_array, zend_ssa *ssa)
1152
0
{
1153
0
  void *checkpoint;
1154
0
  int candidates_count, i;
1155
0
  zend_jit_reg_var *ra;
1156
1157
0
  checkpoint = zend_arena_checkpoint(CG(arena));
1158
0
  ra = zend_arena_calloc(&CG(arena), ssa->vars_count, sizeof(zend_jit_reg_var));
1159
0
  candidates_count = 0;
1160
0
  for (i = 0; i < ssa->vars_count; i++) {
1161
0
    if (zend_jit_may_be_in_reg(op_array, ssa, i)) {
1162
0
      ra[i].ref = IR_NULL;
1163
0
      candidates_count++;
1164
0
    }
1165
0
  }
1166
0
  if (!candidates_count) {
1167
0
    zend_arena_release(&CG(arena), checkpoint);
1168
0
    return;
1169
0
  }
1170
1171
0
  if (JIT_G(opt_flags) & ZEND_JIT_REG_ALLOC_GLOBAL) {
1172
    /* Naive SSA resolution */
1173
0
    for (i = 0; i < ssa->vars_count; i++) {
1174
0
      if (ssa->vars[i].definition_phi && !ssa->vars[i].no_val) {
1175
0
        zend_ssa_phi *phi = ssa->vars[i].definition_phi;
1176
0
        int k, src;
1177
1178
0
        if (phi->pi >= 0) {
1179
0
          src = phi->sources[0];
1180
0
          if (ra[i].ref) {
1181
0
            if (!ra[src].ref) {
1182
0
              ra[i].flags |= ZREG_LOAD;
1183
0
            } else {
1184
0
              ra[i].flags |= ZREG_PI;
1185
0
            }
1186
0
          } else if (ra[src].ref) {
1187
0
            ra[src].flags |= ZREG_STORE;
1188
0
          }
1189
0
        } else {
1190
0
          int need_move = 0;
1191
1192
0
          for (k = 0; k < ssa->cfg.blocks[phi->block].predecessors_count; k++) {
1193
0
            src = phi->sources[k];
1194
0
            if (src >= 0) {
1195
0
              if (ssa->vars[src].definition_phi
1196
0
               && ssa->vars[src].definition_phi->pi >= 0
1197
0
               && phi->block == ssa->vars[src].definition_phi->block) {
1198
                /* Skip zero-length interval for Pi variable */
1199
0
                src = ssa->vars[src].definition_phi->sources[0];
1200
0
              }
1201
0
              if (ra[i].ref) {
1202
0
                if (!ra[src].ref) {
1203
0
                  need_move = 1;
1204
0
                }
1205
0
              } else if (ra[src].ref) {
1206
0
                need_move = 1;
1207
0
              }
1208
0
            }
1209
0
          }
1210
0
          if (need_move) {
1211
0
            if (ra[i].ref) {
1212
0
              ra[i].flags |= ZREG_LOAD;
1213
0
            }
1214
0
            for (k = 0; k < ssa->cfg.blocks[phi->block].predecessors_count; k++) {
1215
0
              src = phi->sources[k];
1216
0
              if (src >= 0) {
1217
0
                if (ssa->vars[src].definition_phi
1218
0
                 && ssa->vars[src].definition_phi->pi >= 0
1219
0
                 && phi->block == ssa->vars[src].definition_phi->block) {
1220
                  /* Skip zero-length interval for Pi variable */
1221
0
                  src = ssa->vars[src].definition_phi->sources[0];
1222
0
                }
1223
0
                if (ra[src].ref) {
1224
0
                  ra[src].flags |= ZREG_STORE;
1225
0
                }
1226
0
              }
1227
0
            }
1228
0
          } else {
1229
0
            ra[i].flags |= ZREG_PHI;
1230
0
          }
1231
0
        }
1232
0
      }
1233
0
    }
1234
1235
    /* Remove useless register allocation */
1236
0
    for (i = 0; i < ssa->vars_count; i++) {
1237
0
      if (ra[i].ref &&
1238
0
          ((ra[i].flags & ZREG_LOAD) ||
1239
0
           ((ra[i].flags & ZREG_STORE) && ssa->vars[i].definition >= 0)) &&
1240
0
          ssa->vars[i].use_chain < 0) {
1241
0
          bool may_remove = true;
1242
0
        zend_ssa_phi *phi = ssa->vars[i].phi_use_chain;
1243
1244
0
        while (phi) {
1245
0
          if (ra[phi->ssa_var].ref &&
1246
0
              !(ra[phi->ssa_var].flags & ZREG_LOAD)) {
1247
0
            may_remove = false;
1248
0
            break;
1249
0
          }
1250
0
          phi = zend_ssa_next_use_phi(ssa, i, phi);
1251
0
        }
1252
0
        if (may_remove) {
1253
0
          ra[i].ref = IR_UNUSED;
1254
0
        }
1255
0
      }
1256
0
    }
1257
1258
    /* Remove intervals used once */
1259
0
    for (i = 0; i < ssa->vars_count; i++) {
1260
0
      if (ra[i].ref) {
1261
0
        if (!(ra[i].flags & (ZREG_LOAD|ZREG_STORE))) {
1262
0
          uint32_t var_num = ssa->vars[i].var;
1263
0
          uint32_t op_num = ssa->vars[i].definition;
1264
1265
          /* Check if a tempoary variable may be freed by exception handler */
1266
0
          if (op_array->last_live_range
1267
0
           && var_num >= op_array->last_var
1268
0
           && ssa->vars[i].definition >= 0
1269
0
           && ssa->ops[op_num].result_def == i) {
1270
0
            const zend_live_range *range = op_array->live_range;
1271
1272
0
            op_num++;
1273
0
            if (op_array->opcodes[op_num].opcode == ZEND_OP_DATA) {
1274
0
              op_num++;
1275
0
            }
1276
0
            for (uint32_t j = 0; j < op_array->last_live_range; range++, j++) {
1277
0
              if (range->start > op_num) {
1278
                /* further blocks will not be relevant... */
1279
0
                break;
1280
0
              } else if (op_num < range->end && var_num == (range->var & ~ZEND_LIVE_MASK)) {
1281
                /* check if opcodes in range may throw */
1282
0
                do {
1283
0
                  if (zend_may_throw(op_array->opcodes + op_num, ssa->ops + op_num, op_array, ssa)) {
1284
0
                    ra[i].flags |= ZREG_STORE;
1285
0
                    break;
1286
0
                  }
1287
0
                  op_num++;
1288
0
                  if (op_array->opcodes[op_num].opcode == ZEND_OP_DATA) {
1289
0
                    op_num++;
1290
0
                  }
1291
0
                } while (op_num < range->end);
1292
0
                break;
1293
0
              }
1294
0
            }
1295
0
          }
1296
0
        }
1297
0
        if ((ra[i].flags & ZREG_LOAD)
1298
0
         && (ra[i].flags & ZREG_STORE)
1299
0
         && (ssa->vars[i].use_chain < 0
1300
0
          || zend_ssa_next_use(ssa->ops, i, ssa->vars[i].use_chain) < 0)) {
1301
0
          bool may_remove = true;
1302
0
          zend_ssa_phi *phi = ssa->vars[i].phi_use_chain;
1303
1304
0
          while (phi) {
1305
0
            if (ra[phi->ssa_var].ref &&
1306
0
                !(ra[phi->ssa_var].flags & ZREG_LOAD)) {
1307
0
              may_remove = false;
1308
0
              break;
1309
0
            }
1310
0
            phi = zend_ssa_next_use_phi(ssa, i, phi);
1311
0
          }
1312
0
          if (may_remove) {
1313
0
            ra[i].ref = IR_UNUSED;
1314
0
          }
1315
0
        }
1316
0
      }
1317
0
    }
1318
0
  }
1319
1320
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_REG_ALLOC) {
1321
0
    fprintf(stderr, "Live Ranges \"%s\"\n", op_array->function_name ? ZSTR_VAL(op_array->function_name) : "[main]");
1322
0
    for (i = 0; i < ssa->vars_count; i++) {
1323
0
      if (ra[i].ref) {
1324
0
        fprintf(stderr, "#%d.", i);
1325
0
        uint32_t var_num = ssa->vars[i].var;
1326
0
        zend_dump_var(op_array, (var_num < op_array->last_var ? IS_CV : 0), var_num);
1327
0
        if (ra[i].flags & ZREG_LOAD) {
1328
0
          fprintf(stderr, " load");
1329
0
        }
1330
0
        if (ra[i].flags & ZREG_STORE) {
1331
0
          fprintf(stderr, " store");
1332
0
        }
1333
0
        fprintf(stderr, "\n");
1334
0
      }
1335
0
    }
1336
0
    fprintf(stderr, "\n");
1337
0
  }
1338
1339
0
  ctx->ra = ra;
1340
0
}
1341
1342
static int zend_jit_compute_post_order(zend_cfg *cfg, int start, int *post_order)
1343
0
{
1344
0
  int count = 0;
1345
0
  int b, n, *p;
1346
0
  zend_basic_block *bb;
1347
0
  zend_worklist worklist;
1348
0
  ALLOCA_FLAG(use_heap)
1349
1350
0
  ZEND_WORKLIST_ALLOCA(&worklist, cfg->blocks_count, use_heap);
1351
0
  zend_worklist_push(&worklist, start);
1352
1353
0
  while (zend_worklist_len(&worklist) != 0) {
1354
0
next:
1355
0
    b = zend_worklist_peek(&worklist);
1356
0
    bb = &cfg->blocks[b];
1357
0
    n = bb->successors_count;
1358
0
    if (n > 0) {
1359
0
      p = bb->successors;
1360
0
      do {
1361
0
        if (cfg->blocks[*p].flags & (ZEND_BB_CATCH|ZEND_BB_FINALLY|ZEND_BB_FINALLY_END)) {
1362
          /* skip */
1363
0
        } else if (zend_worklist_push(&worklist, *p)) {
1364
0
          goto next;
1365
0
        }
1366
0
        p++;
1367
0
        n--;
1368
0
      } while (n > 0);
1369
0
    }
1370
0
    zend_worklist_pop(&worklist);
1371
0
    post_order[count++] = b;
1372
0
  }
1373
0
  ZEND_WORKLIST_FREE_ALLOCA(&worklist, use_heap);
1374
0
  return count;
1375
0
}
1376
1377
static bool zend_jit_next_is_send_result(const zend_op *opline)
1378
0
{
1379
0
  if (opline->result_type == IS_TMP_VAR
1380
0
   && (opline+1)->opcode == ZEND_SEND_VAL
1381
0
   && (opline+1)->op1_type == IS_TMP_VAR
1382
0
   && (opline+1)->op2_type != IS_CONST
1383
0
   && (opline+1)->op1.var == opline->result.var) {
1384
0
    return 1;
1385
0
  }
1386
0
  return 0;
1387
0
}
1388
1389
static bool zend_jit_supported_binary_op(uint8_t op, uint32_t op1_info, uint32_t op2_info)
1390
0
{
1391
0
  if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
1392
0
    return false;
1393
0
  }
1394
0
  switch (op) {
1395
0
    case ZEND_POW:
1396
0
    case ZEND_DIV:
1397
      // TODO: check for division by zero ???
1398
0
      return false;
1399
0
    case ZEND_ADD:
1400
0
    case ZEND_SUB:
1401
0
    case ZEND_MUL:
1402
0
      return (op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE))
1403
0
        && (op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE));
1404
0
    case ZEND_BW_OR:
1405
0
    case ZEND_BW_AND:
1406
0
    case ZEND_BW_XOR:
1407
0
    case ZEND_SL:
1408
0
    case ZEND_SR:
1409
0
    case ZEND_MOD:
1410
0
      return (op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG);
1411
0
    case ZEND_CONCAT:
1412
0
      return (op1_info & MAY_BE_STRING) && (op2_info & MAY_BE_STRING);
1413
0
    EMPTY_SWITCH_DEFAULT_CASE()
1414
0
  }
1415
0
}
1416
1417
static int zend_jit(const zend_op_array *op_array, zend_ssa *ssa, const zend_op *rt_opline)
1418
0
{
1419
0
  int b, i, end;
1420
0
  zend_op *opline;
1421
0
  zend_jit_ctx ctx;
1422
0
  zend_jit_ctx *jit = &ctx;
1423
0
  zend_jit_reg_var *ra = NULL;
1424
0
  zend_vm_opcode_handler_t handler;
1425
0
  int call_level = 0;
1426
0
  void *checkpoint = NULL;
1427
0
  bool recv_emitted = false;   /* emitted at least one RECV opcode */
1428
0
  uint8_t smart_branch_opcode;
1429
0
  uint32_t target_label, target_label2;
1430
0
  uint32_t op1_info, op1_def_info, op2_info, res_info, res_use_info, op1_mem_info;
1431
0
  zend_jit_addr op1_addr, op1_def_addr, op2_addr, op2_def_addr, res_addr;
1432
0
  zend_class_entry *ce = NULL;
1433
0
  bool ce_is_instanceof;
1434
0
  bool on_this;
1435
1436
0
  ZEND_ASSERT(!(op_array->fn_flags & ZEND_ACC_CLOSURE) || !(op_array->scope));
1437
1438
0
  if (JIT_G(bisect_limit)) {
1439
0
    jit_bisect_pos++;
1440
0
    if (jit_bisect_pos >= JIT_G(bisect_limit)) {
1441
0
      if (jit_bisect_pos == JIT_G(bisect_limit)) {
1442
0
        fprintf(stderr, "Not JITing %s%s%s in %s:%d and after due to jit_bisect_limit\n",
1443
0
          op_array->scope ? ZSTR_VAL(op_array->scope->name) : "",
1444
0
          op_array->scope ? "::" : "",
1445
0
          op_array->function_name ? ZSTR_VAL(op_array->function_name) : "{main}",
1446
0
          ZSTR_VAL(op_array->filename), op_array->line_start);
1447
0
      }
1448
0
      return FAILURE;
1449
0
    }
1450
0
  }
1451
1452
0
  if (ssa->cfg.flags & ZEND_FUNC_IRREDUCIBLE) {
1453
    /* We can't order blocks properly */
1454
0
    return FAILURE;
1455
0
  }
1456
1457
0
  if (rt_opline) {
1458
    /* Set BB_ENTRY flag to limit register usage across the OSR ENTRY point */
1459
0
    ssa->cfg.blocks[ssa->cfg.map[rt_opline - op_array->opcodes]].flags |= ZEND_BB_ENTRY;
1460
0
  }
1461
1462
0
  zend_jit_start(&ctx, op_array, ssa);
1463
0
  if (JIT_G(opt_flags) & (ZEND_JIT_REG_ALLOC_LOCAL|ZEND_JIT_REG_ALLOC_GLOBAL)) {
1464
0
    checkpoint = zend_arena_checkpoint(CG(arena));
1465
0
    zend_jit_allocate_registers(&ctx, op_array, ssa);
1466
0
    ra = ctx.ra;
1467
0
  }
1468
1469
  /* Process blocks in Reverse Post Order */
1470
0
  int *sorted_blocks = alloca(sizeof(int) * ssa->cfg.blocks_count);
1471
0
  int n = zend_jit_compute_post_order(&ssa->cfg, 0, sorted_blocks);
1472
1473
0
  while (n > 0) {
1474
0
    b = sorted_blocks[--n];
1475
0
    if ((ssa->cfg.blocks[b].flags & ZEND_BB_REACHABLE) == 0) {
1476
0
      continue;
1477
0
    }
1478
1479
0
    if (ssa->cfg.blocks[b].flags & (ZEND_BB_START|ZEND_BB_RECV_ENTRY)) {
1480
0
      opline = op_array->opcodes + ssa->cfg.blocks[b].start;
1481
0
      if (ssa->cfg.flags & ZEND_CFG_RECV_ENTRY) {
1482
0
        if (opline->opcode == ZEND_RECV_INIT) {
1483
0
          if (JIT_G(opt_level) < ZEND_JIT_LEVEL_INLINE) {
1484
0
            if (opline != op_array->opcodes && (opline-1)->opcode != ZEND_RECV_INIT) {
1485
0
              zend_jit_recv_entry(&ctx, b);
1486
0
            }
1487
0
          } else {
1488
0
            if (opline != op_array->opcodes && recv_emitted) {
1489
0
              zend_jit_recv_entry(&ctx, b);
1490
0
            }
1491
0
          }
1492
0
          recv_emitted = true;
1493
0
        } else if (opline->opcode == ZEND_RECV) {
1494
0
          if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
1495
            /* skip */
1496
0
            zend_jit_bb_start(&ctx, b);
1497
0
            zend_jit_bb_end(&ctx, b);
1498
0
            continue;
1499
0
          } else if (recv_emitted) {
1500
0
            zend_jit_recv_entry(&ctx, b);
1501
0
          } else {
1502
0
            recv_emitted = true;
1503
0
          }
1504
0
        } else {
1505
0
          if (recv_emitted) {
1506
0
            zend_jit_recv_entry(&ctx, b);
1507
0
          } else if (JIT_G(opt_level) < ZEND_JIT_LEVEL_INLINE &&
1508
0
                     ssa->cfg.blocks[b].len == 1 &&
1509
0
                     (ssa->cfg.blocks[b].flags & ZEND_BB_EXIT)) {
1510
            /* don't generate code for BB with single opcode */
1511
0
            zend_jit_free_ctx(&ctx);
1512
1513
0
            if (JIT_G(opt_flags) & (ZEND_JIT_REG_ALLOC_LOCAL|ZEND_JIT_REG_ALLOC_GLOBAL)) {
1514
0
              zend_arena_release(&CG(arena), checkpoint);
1515
0
            }
1516
0
            return SUCCESS;
1517
0
          }
1518
0
        }
1519
0
      } else if (JIT_G(opt_level) < ZEND_JIT_LEVEL_INLINE &&
1520
0
                 ssa->cfg.blocks[b].len == 1 &&
1521
0
                 (ssa->cfg.blocks[b].flags & ZEND_BB_EXIT)) {
1522
        /* don't generate code for BB with single opcode */
1523
0
        zend_jit_free_ctx(&ctx);
1524
1525
0
        if (JIT_G(opt_flags) & (ZEND_JIT_REG_ALLOC_LOCAL|ZEND_JIT_REG_ALLOC_GLOBAL)) {
1526
0
          zend_arena_release(&CG(arena), checkpoint);
1527
0
        }
1528
0
        return SUCCESS;
1529
0
      }
1530
0
    }
1531
1532
0
    zend_jit_bb_start(&ctx, b);
1533
1534
0
    if ((JIT_G(opt_flags) & ZEND_JIT_REG_ALLOC_GLOBAL) && ctx.ra) {
1535
0
      zend_ssa_phi *phi = ssa->blocks[b].phis;
1536
1537
      /* First try to insert IR Phi */
1538
0
      while (phi) {
1539
0
        zend_jit_reg_var *ival = &ctx.ra[phi->ssa_var];
1540
1541
0
        if (ival->ref) {
1542
0
          if (ival->flags & ZREG_PI) {
1543
0
            zend_jit_gen_pi(jit, phi);
1544
0
          } else if (ival->flags & ZREG_PHI) {
1545
0
            zend_jit_gen_phi(jit, phi);
1546
0
          }
1547
0
        }
1548
0
        phi = phi->next;
1549
0
      }
1550
0
    }
1551
1552
0
    if (rt_opline
1553
0
     && (ssa->cfg.blocks[b].flags & (ZEND_BB_START|ZEND_BB_RECV_ENTRY)) == 0
1554
0
     && rt_opline == op_array->opcodes + ssa->cfg.blocks[b].start) {
1555
0
      zend_jit_osr_entry(&ctx, b); /* OSR (On-Stack-Replacement) Entry-Point */
1556
0
    }
1557
1558
0
    if (JIT_G(opt_level) < ZEND_JIT_LEVEL_INLINE) {
1559
0
      if ((ssa->cfg.blocks[b].flags & ZEND_BB_FOLLOW)
1560
0
        && ssa->cfg.blocks[b].start != 0
1561
0
        && (op_array->opcodes[ssa->cfg.blocks[b].start - 1].opcode == ZEND_NOP
1562
0
         || op_array->opcodes[ssa->cfg.blocks[b].start - 1].opcode == ZEND_SWITCH_LONG
1563
0
         || op_array->opcodes[ssa->cfg.blocks[b].start - 1].opcode == ZEND_SWITCH_STRING
1564
0
         || op_array->opcodes[ssa->cfg.blocks[b].start - 1].opcode == ZEND_MATCH)) {
1565
0
        zend_jit_reset_last_valid_opline(&ctx);
1566
0
      } else {
1567
0
        zend_jit_set_last_valid_opline(&ctx, op_array->opcodes + ssa->cfg.blocks[b].start);
1568
0
      }
1569
0
    } else if (ssa->cfg.blocks[b].flags & ZEND_BB_TARGET) {
1570
0
      zend_jit_reset_last_valid_opline(&ctx);
1571
0
    } else if (ssa->cfg.blocks[b].flags & ZEND_BB_RECV_ENTRY) {
1572
0
      zend_jit_reset_last_valid_opline(&ctx);
1573
0
    } else if (ssa->cfg.blocks[b].flags & (ZEND_BB_START|ZEND_BB_ENTRY)) {
1574
0
      zend_jit_set_last_valid_opline(&ctx, op_array->opcodes + ssa->cfg.blocks[b].start);
1575
0
    }
1576
0
    if (ssa->cfg.blocks[b].flags & ZEND_BB_LOOP_HEADER) {
1577
0
      zend_jit_check_timeout(&ctx, op_array->opcodes + ssa->cfg.blocks[b].start, NULL);
1578
0
    }
1579
0
    if (!ssa->cfg.blocks[b].len) {
1580
0
      zend_jit_bb_end(&ctx, b);
1581
0
      continue;
1582
0
    }
1583
0
    if ((JIT_G(opt_flags) & ZEND_JIT_REG_ALLOC_GLOBAL) && ra) {
1584
0
      zend_ssa_phi *phi = ssa->blocks[b].phis;
1585
1586
0
      while (phi) {
1587
0
        zend_jit_reg_var *ival = &ra[phi->ssa_var];
1588
1589
0
        if (ival->ref) {
1590
0
          if (ival->flags & ZREG_LOAD) {
1591
0
            ZEND_ASSERT(ival->ref == IR_NULL);
1592
1593
0
            if (!zend_jit_load_var(&ctx, ssa->var_info[phi->ssa_var].type, ssa->vars[phi->ssa_var].var, phi->ssa_var)) {
1594
0
              goto jit_failure;
1595
0
            }
1596
0
          } else if (ival->flags & ZREG_STORE) {
1597
0
            ZEND_ASSERT(ival->ref != IR_NULL);
1598
1599
0
            if (!zend_jit_store_var(&ctx, ssa->var_info[phi->ssa_var].type, ssa->vars[phi->ssa_var].var, phi->ssa_var, 1)) {
1600
0
              goto jit_failure;
1601
0
            }
1602
0
          }
1603
0
        }
1604
0
        phi = phi->next;
1605
0
      }
1606
0
    }
1607
0
    end = ssa->cfg.blocks[b].start + ssa->cfg.blocks[b].len - 1;
1608
0
    for (i = ssa->cfg.blocks[b].start; i <= end; i++) {
1609
0
      zend_ssa_op *ssa_op = ssa->ops ? &ssa->ops[i] : NULL;
1610
0
      opline = op_array->opcodes + i;
1611
0
      if (zend_jit_inc_call_level(opline->opcode)) {
1612
0
        call_level++;
1613
0
      }
1614
1615
0
      if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_INLINE) {
1616
0
        switch (opline->opcode) {
1617
0
          case ZEND_PRE_INC:
1618
0
          case ZEND_PRE_DEC:
1619
0
          case ZEND_POST_INC:
1620
0
          case ZEND_POST_DEC:
1621
0
            if (opline->op1_type != IS_CV) {
1622
0
              break;
1623
0
            }
1624
0
            op1_info = OP1_INFO();
1625
0
            if (!(op1_info & MAY_BE_LONG)) {
1626
0
              break;
1627
0
            }
1628
0
            if (opline->result_type != IS_UNUSED) {
1629
0
              res_use_info = -1;
1630
1631
0
              if (opline->result_type == IS_CV
1632
0
               && ssa->vars
1633
0
               && ssa_op->result_use >= 0
1634
0
               && !ssa->vars[ssa_op->result_use].no_val) {
1635
0
                zend_jit_addr res_use_addr = RES_USE_REG_ADDR();
1636
1637
0
                if (Z_MODE(res_use_addr) != IS_REG
1638
0
                 || Z_LOAD(res_use_addr)
1639
0
                 || Z_STORE(res_use_addr)) {
1640
0
                  res_use_info = RES_USE_INFO();
1641
0
                }
1642
0
              }
1643
0
              res_info = RES_INFO();
1644
0
              res_addr = RES_REG_ADDR();
1645
0
            } else {
1646
0
              res_use_info = -1;
1647
0
              res_info = -1;
1648
0
              res_addr = 0;
1649
0
            }
1650
0
            op1_def_info = OP1_DEF_INFO();
1651
0
            if (!zend_jit_inc_dec(&ctx, opline,
1652
0
                op1_info, OP1_REG_ADDR(),
1653
0
                op1_def_info, OP1_DEF_REG_ADDR(),
1654
0
                res_use_info, res_info,
1655
0
                res_addr,
1656
0
                (op1_info & MAY_BE_LONG) && (op1_def_info & MAY_BE_DOUBLE) && zend_may_overflow(opline, ssa_op, op_array, ssa),
1657
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
1658
0
              goto jit_failure;
1659
0
            }
1660
0
            goto done;
1661
0
          case ZEND_BW_OR:
1662
0
          case ZEND_BW_AND:
1663
0
          case ZEND_BW_XOR:
1664
0
          case ZEND_SL:
1665
0
          case ZEND_SR:
1666
0
          case ZEND_MOD:
1667
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
1668
0
              break;
1669
0
            }
1670
0
            op1_info = OP1_INFO();
1671
0
            op2_info = OP2_INFO();
1672
0
            if (!(op1_info & MAY_BE_LONG)
1673
0
             || !(op2_info & MAY_BE_LONG)) {
1674
0
              break;
1675
0
            }
1676
0
            res_addr = RES_REG_ADDR();
1677
0
            if (Z_MODE(res_addr) != IS_REG
1678
0
             && (i + 1) <= end
1679
0
             && zend_jit_next_is_send_result(opline)) {
1680
0
              i++;
1681
0
              res_use_info = -1;
1682
0
              res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, (opline+1)->result.var);
1683
0
              if (!zend_jit_reuse_ip(&ctx)) {
1684
0
                goto jit_failure;
1685
0
              }
1686
0
            } else {
1687
0
              res_use_info = -1;
1688
1689
0
              if (opline->result_type == IS_CV
1690
0
               && ssa->vars
1691
0
               && ssa_op->result_use >= 0
1692
0
               && !ssa->vars[ssa_op->result_use].no_val) {
1693
0
                zend_jit_addr res_use_addr = RES_USE_REG_ADDR();
1694
1695
0
                if (Z_MODE(res_use_addr) != IS_REG
1696
0
                 || Z_LOAD(res_use_addr)
1697
0
                 || Z_STORE(res_use_addr)) {
1698
0
                  res_use_info = RES_USE_INFO();
1699
0
                }
1700
0
              }
1701
0
            }
1702
0
            if (!zend_jit_long_math(&ctx, opline,
1703
0
                op1_info, OP1_RANGE(), OP1_REG_ADDR(),
1704
0
                op2_info, OP2_RANGE(), OP2_REG_ADDR(),
1705
0
                res_use_info, RES_INFO(), res_addr,
1706
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
1707
0
              goto jit_failure;
1708
0
            }
1709
0
            goto done;
1710
0
          case ZEND_ADD:
1711
0
          case ZEND_SUB:
1712
0
          case ZEND_MUL:
1713
//          case ZEND_DIV: // TODO: check for division by zero ???
1714
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
1715
0
              break;
1716
0
            }
1717
0
            op1_info = OP1_INFO();
1718
0
            op2_info = OP2_INFO();
1719
0
            if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
1720
0
              break;
1721
0
            }
1722
0
            if (opline->opcode == ZEND_ADD &&
1723
0
                (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY &&
1724
0
                (op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
1725
              /* pass */
1726
0
            } else if (!(op1_info & (MAY_BE_LONG|MAY_BE_DOUBLE)) ||
1727
0
                !(op2_info & (MAY_BE_LONG|MAY_BE_DOUBLE))) {
1728
0
              break;
1729
0
            }
1730
0
            res_addr = RES_REG_ADDR();
1731
0
            if (Z_MODE(res_addr) != IS_REG
1732
0
             && (i + 1) <= end
1733
0
             && zend_jit_next_is_send_result(opline)) {
1734
0
              i++;
1735
0
              res_use_info = -1;
1736
0
              res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, (opline+1)->result.var);
1737
0
              if (!zend_jit_reuse_ip(&ctx)) {
1738
0
                goto jit_failure;
1739
0
              }
1740
0
            } else {
1741
0
              res_use_info = -1;
1742
1743
0
              if (opline->result_type == IS_CV
1744
0
               && ssa->vars
1745
0
               && ssa_op->result_use >= 0
1746
0
               && !ssa->vars[ssa_op->result_use].no_val) {
1747
0
                zend_jit_addr res_use_addr = RES_USE_REG_ADDR();
1748
1749
0
                if (Z_MODE(res_use_addr) != IS_REG
1750
0
                 || Z_LOAD(res_use_addr)
1751
0
                 || Z_STORE(res_use_addr)) {
1752
0
                  res_use_info = RES_USE_INFO();
1753
0
                }
1754
0
              }
1755
0
            }
1756
0
            res_info = RES_INFO();
1757
0
            if (opline->opcode == ZEND_ADD &&
1758
0
                (op1_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY &&
1759
0
                (op2_info & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY) {
1760
0
              if (!zend_jit_add_arrays(&ctx, opline, op1_info, OP1_REG_ADDR(), op2_info, OP2_REG_ADDR(), res_addr)) {
1761
0
                goto jit_failure;
1762
0
              }
1763
0
            } else {
1764
0
              if (!zend_jit_math(&ctx, opline,
1765
0
                  op1_info, OP1_REG_ADDR(),
1766
0
                  op2_info, OP2_REG_ADDR(),
1767
0
                  res_use_info, res_info, res_addr,
1768
0
                  (op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG) && (res_info & MAY_BE_DOUBLE) && zend_may_overflow(opline, ssa_op, op_array, ssa),
1769
0
                  zend_may_throw(opline, ssa_op, op_array, ssa))) {
1770
0
                goto jit_failure;
1771
0
              }
1772
0
            }
1773
0
            goto done;
1774
0
          case ZEND_CONCAT:
1775
0
          case ZEND_FAST_CONCAT:
1776
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
1777
0
              break;
1778
0
            }
1779
0
            op1_info = OP1_INFO();
1780
0
            op2_info = OP2_INFO();
1781
0
            if ((op1_info & MAY_BE_UNDEF) || (op2_info & MAY_BE_UNDEF)) {
1782
0
              break;
1783
0
            }
1784
0
            if (!(op1_info & MAY_BE_STRING) ||
1785
0
                !(op2_info & MAY_BE_STRING)) {
1786
0
              break;
1787
0
            }
1788
0
            res_addr = RES_REG_ADDR();
1789
0
            if ((i + 1) <= end
1790
0
             && zend_jit_next_is_send_result(opline)) {
1791
0
              i++;
1792
0
              res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, (opline+1)->result.var);
1793
0
              if (!zend_jit_reuse_ip(&ctx)) {
1794
0
                goto jit_failure;
1795
0
              }
1796
0
            }
1797
0
            if (!zend_jit_concat(&ctx, opline,
1798
0
                op1_info, op2_info, res_addr,
1799
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
1800
0
              goto jit_failure;
1801
0
            }
1802
0
            goto done;
1803
0
          case ZEND_ASSIGN_OP:
1804
0
            if (opline->op1_type != IS_CV || opline->result_type != IS_UNUSED) {
1805
0
              break;
1806
0
            }
1807
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
1808
0
              break;
1809
0
            }
1810
0
            op1_info = OP1_INFO();
1811
0
            op2_info = OP2_INFO();
1812
0
            if (!zend_jit_supported_binary_op(
1813
0
                opline->extended_value, op1_info, op2_info)) {
1814
0
              break;
1815
0
            }
1816
0
            op1_addr = OP1_REG_ADDR();
1817
0
            op1_mem_info = -1;
1818
0
            if (Z_MODE(op1_addr) != IS_REG
1819
0
             || Z_LOAD(op1_addr)
1820
0
             || Z_STORE(op1_addr)) {
1821
0
              op1_mem_info = op1_info;
1822
0
            }
1823
0
            op1_def_info = OP1_DEF_INFO();
1824
0
            if (!zend_jit_assign_op(&ctx, opline,
1825
0
                op1_info, op1_addr, OP1_RANGE(),
1826
0
                op1_def_info, OP1_DEF_REG_ADDR(), op1_mem_info,
1827
0
                op2_info, OP2_REG_ADDR(), OP2_RANGE(),
1828
0
                (op1_info & MAY_BE_LONG) && (op2_info & MAY_BE_LONG) && (op1_def_info & MAY_BE_DOUBLE) && zend_may_overflow(opline, ssa_op, op_array, ssa),
1829
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
1830
0
              goto jit_failure;
1831
0
            }
1832
0
            goto done;
1833
0
          case ZEND_ASSIGN_DIM_OP:
1834
0
            if (opline->op1_type != IS_CV || opline->result_type != IS_UNUSED) {
1835
0
              break;
1836
0
            }
1837
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
1838
0
              break;
1839
0
            }
1840
0
            if (!zend_jit_supported_binary_op(
1841
0
                opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
1842
0
              break;
1843
0
            }
1844
0
            if (!zend_jit_assign_dim_op(&ctx, opline,
1845
0
                OP1_INFO(), OP1_DEF_INFO(), OP1_REG_ADDR(), 0,
1846
0
                OP2_INFO(), (opline->op2_type != IS_UNUSED) ? OP2_REG_ADDR() : 0,
1847
0
                (opline->op2_type != IS_UNUSED) ? OP2_RANGE() : NULL,
1848
0
                OP1_DATA_INFO(), OP1_DATA_REG_ADDR(), OP1_DATA_RANGE(), IS_UNKNOWN,
1849
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
1850
0
              goto jit_failure;
1851
0
            }
1852
0
            goto done;
1853
0
          case ZEND_ASSIGN_DIM:
1854
0
            if (opline->op1_type != IS_CV) {
1855
0
              break;
1856
0
            }
1857
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
1858
0
              break;
1859
0
            }
1860
0
            if (!zend_jit_assign_dim(&ctx, opline,
1861
0
                OP1_INFO(), OP1_REG_ADDR(), 0,
1862
0
                OP2_INFO(), (opline->op2_type != IS_UNUSED) ? OP2_REG_ADDR() : 0,
1863
0
                (opline->op2_type != IS_UNUSED) ? OP2_RANGE() : NULL,
1864
0
                OP1_DATA_INFO(), OP1_DATA_REG_ADDR(),
1865
0
                (ctx.ra && (ssa_op+1)->op1_def >= 0) ? OP1_DATA_DEF_REG_ADDR() : 0,
1866
0
                (opline->result_type != IS_UNUSED) ? RES_REG_ADDR() : 0,
1867
0
                IS_UNKNOWN,
1868
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
1869
0
              goto jit_failure;
1870
0
            }
1871
0
            goto done;
1872
0
          case ZEND_PRE_INC_OBJ:
1873
0
          case ZEND_PRE_DEC_OBJ:
1874
0
          case ZEND_POST_INC_OBJ:
1875
0
          case ZEND_POST_DEC_OBJ:
1876
0
            if (opline->op2_type != IS_CONST
1877
0
             || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
1878
0
             || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
1879
0
              break;
1880
0
            }
1881
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
1882
0
              break;
1883
0
            }
1884
0
            ce = NULL;
1885
0
            ce_is_instanceof = false;
1886
0
            on_this = false;
1887
0
            if (opline->op1_type == IS_UNUSED) {
1888
0
              op1_info = MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN;
1889
0
              ce = op_array->scope;
1890
              /* scope is NULL for closures. */
1891
0
              if (ce) {
1892
0
                ce_is_instanceof = !(ce->ce_flags & ZEND_ACC_FINAL);
1893
0
              }
1894
0
              op1_addr = 0;
1895
0
              on_this = true;
1896
0
            } else {
1897
0
              op1_info = OP1_INFO();
1898
0
              if (!(op1_info & MAY_BE_OBJECT)) {
1899
0
                break;
1900
0
              }
1901
0
              op1_addr = OP1_REG_ADDR();
1902
0
              if (ssa->var_info && ssa->ops) {
1903
0
                zend_ssa_op *ssa_op = &ssa->ops[opline - op_array->opcodes];
1904
0
                if (ssa_op->op1_use >= 0) {
1905
0
                  zend_ssa_var_info *op1_ssa = ssa->var_info + ssa_op->op1_use;
1906
0
                  if (op1_ssa->ce && !op1_ssa->ce->create_object) {
1907
0
                    ce = op1_ssa->ce;
1908
0
                    ce_is_instanceof = op1_ssa->is_instanceof;
1909
0
                  }
1910
0
                }
1911
0
              }
1912
0
            }
1913
0
            if (!zend_jit_incdec_obj(&ctx, opline, op_array, ssa, ssa_op,
1914
0
                op1_info, op1_addr,
1915
0
                0, ce, ce_is_instanceof, on_this, 0, NULL, IS_UNKNOWN)) {
1916
0
              goto jit_failure;
1917
0
            }
1918
0
            goto done;
1919
0
          case ZEND_ASSIGN_OBJ_OP:
1920
0
            if (opline->result_type != IS_UNUSED) {
1921
0
              break;
1922
0
            }
1923
0
            if (opline->op2_type != IS_CONST
1924
0
             || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
1925
0
             || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
1926
0
              break;
1927
0
            }
1928
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
1929
0
              break;
1930
0
            }
1931
0
            if (!zend_jit_supported_binary_op(
1932
0
                opline->extended_value, MAY_BE_ANY, OP1_DATA_INFO())) {
1933
0
              break;
1934
0
            }
1935
0
            ce = NULL;
1936
0
            ce_is_instanceof = false;
1937
0
            on_this = false;
1938
0
            if (opline->op1_type == IS_UNUSED) {
1939
0
              op1_info = MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN;
1940
0
              ce = op_array->scope;
1941
              /* scope is NULL for closures. */
1942
0
              if (ce) {
1943
0
                ce_is_instanceof = !(ce->ce_flags & ZEND_ACC_FINAL);
1944
0
              }
1945
0
              op1_addr = 0;
1946
0
              on_this = true;
1947
0
            } else {
1948
0
              op1_info = OP1_INFO();
1949
0
              if (!(op1_info & MAY_BE_OBJECT)) {
1950
0
                break;
1951
0
              }
1952
0
              op1_addr = OP1_REG_ADDR();
1953
0
              if (ssa->var_info && ssa->ops) {
1954
0
                zend_ssa_op *ssa_op = &ssa->ops[opline - op_array->opcodes];
1955
0
                if (ssa_op->op1_use >= 0) {
1956
0
                  zend_ssa_var_info *op1_ssa = ssa->var_info + ssa_op->op1_use;
1957
0
                  if (op1_ssa->ce && !op1_ssa->ce->create_object) {
1958
0
                    ce = op1_ssa->ce;
1959
0
                    ce_is_instanceof = op1_ssa->is_instanceof;
1960
0
                  }
1961
0
                }
1962
0
              }
1963
0
            }
1964
0
            if (!zend_jit_assign_obj_op(&ctx, opline, op_array, ssa, ssa_op,
1965
0
                op1_info, op1_addr, OP1_DATA_INFO(), OP1_DATA_REG_ADDR(), OP1_DATA_RANGE(),
1966
0
                0, ce, ce_is_instanceof, on_this, 0, NULL, IS_UNKNOWN)) {
1967
0
              goto jit_failure;
1968
0
            }
1969
0
            goto done;
1970
0
          case ZEND_ASSIGN_OBJ:
1971
0
            if (opline->op2_type != IS_CONST
1972
0
             || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
1973
0
             || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
1974
0
              break;
1975
0
            }
1976
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
1977
0
              break;
1978
0
            }
1979
0
            ce = NULL;
1980
0
            ce_is_instanceof = false;
1981
0
            on_this = false;
1982
0
            if (opline->op1_type == IS_UNUSED) {
1983
0
              op1_info = MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN;
1984
0
              ce = op_array->scope;
1985
              /* scope is NULL for closures. */
1986
0
              if (ce) {
1987
0
                ce_is_instanceof = !(ce->ce_flags & ZEND_ACC_FINAL);
1988
0
              }
1989
0
              op1_addr = 0;
1990
0
              on_this = true;
1991
0
            } else {
1992
0
              op1_info = OP1_INFO();
1993
0
              if (!(op1_info & MAY_BE_OBJECT)) {
1994
0
                break;
1995
0
              }
1996
0
              op1_addr = OP1_REG_ADDR();
1997
0
              if (ssa->var_info && ssa->ops) {
1998
0
                zend_ssa_op *ssa_op = &ssa->ops[opline - op_array->opcodes];
1999
0
                if (ssa_op->op1_use >= 0) {
2000
0
                  zend_ssa_var_info *op1_ssa = ssa->var_info + ssa_op->op1_use;
2001
0
                  if (op1_ssa->ce && !op1_ssa->ce->create_object) {
2002
0
                    ce = op1_ssa->ce;
2003
0
                    ce_is_instanceof = op1_ssa->is_instanceof;
2004
0
                  }
2005
0
                }
2006
0
              }
2007
0
            }
2008
0
            if (!zend_jit_assign_obj(&ctx, opline, op_array, ssa, ssa_op,
2009
0
                op1_info, op1_addr, OP1_DATA_INFO(), OP1_DATA_REG_ADDR(), OP1_DATA_DEF_REG_ADDR(),
2010
0
                (opline->result_type != IS_UNUSED) ? RES_REG_ADDR() : 0,
2011
0
                0, ce, ce_is_instanceof, on_this, 0, NULL, IS_UNKNOWN,
2012
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
2013
0
              goto jit_failure;
2014
0
            }
2015
0
            goto done;
2016
0
          case ZEND_ASSIGN:
2017
0
            if (opline->op1_type != IS_CV) {
2018
0
              break;
2019
0
            }
2020
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
2021
0
              break;
2022
0
            }
2023
0
            op2_addr = OP2_REG_ADDR();
2024
0
            if (ra
2025
0
             && ssa->ops[opline - op_array->opcodes].op2_def >= 0
2026
0
             && !ssa->vars[ssa->ops[opline - op_array->opcodes].op2_def].no_val) {
2027
0
              op2_def_addr = OP2_DEF_REG_ADDR();
2028
0
            } else {
2029
0
              op2_def_addr = op2_addr;
2030
0
            }
2031
0
            op1_info = OP1_INFO();
2032
0
            if (ra && ssa->vars[ssa_op->op1_use].no_val) {
2033
0
              op1_info |= MAY_BE_UNDEF; // requres type assignment
2034
0
            }
2035
0
            if (opline->result_type == IS_UNUSED) {
2036
0
              res_addr = 0;
2037
0
              res_info = -1;
2038
0
            } else {
2039
0
              res_addr = RES_REG_ADDR();
2040
0
              res_info = RES_INFO();
2041
0
              if (Z_MODE(res_addr) != IS_REG
2042
0
               && (i + 1) <= end
2043
0
               && zend_jit_next_is_send_result(opline)
2044
0
               && (!(op1_info & MAY_HAVE_DTOR) || !(op1_info & MAY_BE_RC1))) {
2045
0
                i++;
2046
0
                res_addr = ZEND_ADDR_MEM_ZVAL(ZREG_RX, (opline+1)->result.var);
2047
0
                if (!zend_jit_reuse_ip(&ctx)) {
2048
0
                  goto jit_failure;
2049
0
                }
2050
0
              }
2051
0
            }
2052
0
            if (!zend_jit_assign(&ctx, opline,
2053
0
                op1_info, OP1_REG_ADDR(),
2054
0
                OP1_DEF_INFO(), OP1_DEF_REG_ADDR(),
2055
0
                OP2_INFO(), op2_addr, op2_def_addr,
2056
0
                res_info, res_addr,
2057
0
                0,
2058
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
2059
0
              goto jit_failure;
2060
0
            }
2061
0
            goto done;
2062
0
          case ZEND_QM_ASSIGN:
2063
0
            op1_addr = OP1_REG_ADDR();
2064
0
            if (ra
2065
0
             && ssa->ops[opline - op_array->opcodes].op1_def >= 0
2066
0
             && !ssa->vars[ssa->ops[opline - op_array->opcodes].op1_def].no_val) {
2067
0
              op1_def_addr = OP1_DEF_REG_ADDR();
2068
0
            } else {
2069
0
              op1_def_addr = op1_addr;
2070
0
            }
2071
0
            if (!zend_jit_qm_assign(&ctx, opline,
2072
0
                OP1_INFO(), op1_addr, op1_def_addr,
2073
0
                -1, RES_INFO(), RES_REG_ADDR())) {
2074
0
              goto jit_failure;
2075
0
            }
2076
0
            goto done;
2077
0
          case ZEND_INIT_FCALL:
2078
0
          case ZEND_INIT_FCALL_BY_NAME:
2079
0
          case ZEND_INIT_NS_FCALL_BY_NAME:
2080
0
            if (!zend_jit_init_fcall(&ctx, opline, b, op_array, ssa, ssa_op, call_level, NULL, 0)) {
2081
0
              goto jit_failure;
2082
0
            }
2083
0
            goto done;
2084
0
          case ZEND_SEND_VAL:
2085
0
          case ZEND_SEND_VAL_EX:
2086
0
            if (opline->op2_type == IS_CONST) {
2087
              /* Named parameters not supported in JIT (yet) */
2088
0
              break;
2089
0
            }
2090
0
            if (opline->opcode == ZEND_SEND_VAL_EX
2091
0
             && opline->op2.num > MAX_ARG_FLAG_NUM) {
2092
0
              break;
2093
0
            }
2094
0
            if (!zend_jit_send_val(&ctx, opline,
2095
0
                OP1_INFO(), OP1_REG_ADDR())) {
2096
0
              goto jit_failure;
2097
0
            }
2098
0
            goto done;
2099
0
          case ZEND_SEND_REF:
2100
0
            if (opline->op2_type == IS_CONST) {
2101
              /* Named parameters not supported in JIT (yet) */
2102
0
              break;
2103
0
            }
2104
0
            if (!zend_jit_send_ref(&ctx, opline, op_array,
2105
0
                OP1_INFO(), 0)) {
2106
0
              goto jit_failure;
2107
0
            }
2108
0
            goto done;
2109
0
          case ZEND_SEND_VAR:
2110
0
          case ZEND_SEND_VAR_EX:
2111
0
          case ZEND_SEND_VAR_NO_REF:
2112
0
          case ZEND_SEND_VAR_NO_REF_EX:
2113
0
          case ZEND_SEND_FUNC_ARG:
2114
0
            if (opline->op2_type == IS_CONST) {
2115
              /* Named parameters not supported in JIT (yet) */
2116
0
              break;
2117
0
            }
2118
0
            if ((opline->opcode == ZEND_SEND_VAR_EX
2119
0
              || opline->opcode == ZEND_SEND_VAR_NO_REF_EX)
2120
0
             && opline->op2.num > MAX_ARG_FLAG_NUM) {
2121
0
              break;
2122
0
            }
2123
0
            op1_addr = OP1_REG_ADDR();
2124
0
            if (ra
2125
0
             && ssa->ops[opline - op_array->opcodes].op1_def >= 0
2126
0
             && !ssa->vars[ssa->ops[opline - op_array->opcodes].op1_def].no_val) {
2127
0
              op1_def_addr = OP1_DEF_REG_ADDR();
2128
0
            } else {
2129
0
              op1_def_addr = op1_addr;
2130
0
            }
2131
0
            if (!zend_jit_send_var(&ctx, opline, op_array,
2132
0
                OP1_INFO(), op1_addr, op1_def_addr)) {
2133
0
              goto jit_failure;
2134
0
            }
2135
0
            goto done;
2136
0
          case ZEND_CHECK_FUNC_ARG:
2137
0
            if (opline->op2_type == IS_CONST) {
2138
              /* Named parameters not supported in JIT (yet) */
2139
0
              break;
2140
0
            }
2141
0
            if (opline->op2.num > MAX_ARG_FLAG_NUM) {
2142
0
              break;
2143
0
            }
2144
0
            if (!zend_jit_check_func_arg(&ctx, opline)) {
2145
0
              goto jit_failure;
2146
0
            }
2147
0
            goto done;
2148
0
          case ZEND_CHECK_UNDEF_ARGS:
2149
0
            if (!zend_jit_check_undef_args(&ctx, opline)) {
2150
0
              goto jit_failure;
2151
0
            }
2152
0
            goto done;
2153
0
          case ZEND_DO_UCALL:
2154
0
            ZEND_FALLTHROUGH;
2155
0
          case ZEND_DO_ICALL:
2156
0
          case ZEND_DO_FCALL_BY_NAME:
2157
0
          case ZEND_DO_FCALL:
2158
0
            if (!zend_jit_do_fcall(&ctx, opline, op_array, ssa, call_level, b + 1, NULL)) {
2159
0
              goto jit_failure;
2160
0
            }
2161
0
            goto done;
2162
0
          case ZEND_IS_EQUAL:
2163
0
          case ZEND_IS_NOT_EQUAL:
2164
0
          case ZEND_IS_SMALLER:
2165
0
          case ZEND_IS_SMALLER_OR_EQUAL:
2166
0
          case ZEND_CASE: {
2167
0
            res_addr = RES_REG_ADDR();
2168
0
            if ((opline->result_type & IS_TMP_VAR)
2169
0
             && (i + 1) <= end
2170
0
             && ((opline+1)->opcode == ZEND_JMPZ
2171
0
              || (opline+1)->opcode == ZEND_JMPNZ
2172
0
              || (opline+1)->opcode == ZEND_JMPZ_EX
2173
0
              || (opline+1)->opcode == ZEND_JMPNZ_EX)
2174
0
             && (opline+1)->op1_type == IS_TMP_VAR
2175
0
             && (opline+1)->op1.var == opline->result.var) {
2176
0
              i++;
2177
0
              smart_branch_opcode = (opline+1)->opcode;
2178
0
              target_label = ssa->cfg.blocks[b].successors[0];
2179
0
              target_label2 = ssa->cfg.blocks[b].successors[1];
2180
              /* For EX variant write into the result of EX opcode. */
2181
0
              if ((opline+1)->opcode == ZEND_JMPZ_EX
2182
0
                  || (opline+1)->opcode == ZEND_JMPNZ_EX) {
2183
0
                res_addr = OP_REG_ADDR(opline + 1, ssa_op + 1, result_type, result, result_def);
2184
0
              }
2185
0
            } else {
2186
0
              smart_branch_opcode = 0;
2187
0
              target_label = target_label2 = (uint32_t)-1;
2188
0
            }
2189
0
            if (!zend_jit_cmp(&ctx, opline,
2190
0
                OP1_INFO(), OP1_RANGE(), OP1_REG_ADDR(),
2191
0
                OP2_INFO(), OP2_RANGE(), OP2_REG_ADDR(),
2192
0
                res_addr,
2193
0
                zend_may_throw(opline, ssa_op, op_array, ssa),
2194
0
                smart_branch_opcode, target_label, target_label2,
2195
0
                NULL, 0)) {
2196
0
              goto jit_failure;
2197
0
            }
2198
0
            goto done;
2199
0
          }
2200
0
          case ZEND_IS_IDENTICAL:
2201
0
          case ZEND_IS_NOT_IDENTICAL:
2202
0
          case ZEND_CASE_STRICT:
2203
0
            res_addr = RES_REG_ADDR();
2204
0
            if ((opline->result_type & IS_TMP_VAR)
2205
0
             && (i + 1) <= end
2206
0
             && ((opline+1)->opcode == ZEND_JMPZ
2207
0
              || (opline+1)->opcode == ZEND_JMPZ_EX
2208
0
              || (opline+1)->opcode == ZEND_JMPNZ_EX
2209
0
              || (opline+1)->opcode == ZEND_JMPNZ)
2210
0
             && (opline+1)->op1_type == IS_TMP_VAR
2211
0
             && (opline+1)->op1.var == opline->result.var) {
2212
0
              i++;
2213
0
              smart_branch_opcode = (opline+1)->opcode;
2214
0
              target_label = ssa->cfg.blocks[b].successors[0];
2215
0
              target_label2 = ssa->cfg.blocks[b].successors[1];
2216
              /* For EX variant write into the result of EX opcode. */
2217
0
              if ((opline+1)->opcode == ZEND_JMPZ_EX
2218
0
                  || (opline+1)->opcode == ZEND_JMPNZ_EX) {
2219
0
                res_addr = OP_REG_ADDR(opline + 1, ssa_op + 1, result_type, result, result_def);
2220
0
              }
2221
0
            } else {
2222
0
              smart_branch_opcode = 0;
2223
0
              target_label = target_label2 = (uint32_t)-1;
2224
0
            }
2225
0
            if (!zend_jit_identical(&ctx, opline,
2226
0
                OP1_INFO(), OP1_RANGE(), OP1_REG_ADDR(),
2227
0
                OP2_INFO(), OP2_RANGE(), OP2_REG_ADDR(),
2228
0
                res_addr,
2229
0
                zend_may_throw(opline, ssa_op, op_array, ssa),
2230
0
                smart_branch_opcode, target_label, target_label2,
2231
0
                NULL, 0)) {
2232
0
              goto jit_failure;
2233
0
            }
2234
0
            goto done;
2235
0
          case ZEND_DEFINED:
2236
0
            if ((opline->result_type & IS_TMP_VAR)
2237
0
             && (i + 1) <= end
2238
0
             && ((opline+1)->opcode == ZEND_JMPZ
2239
0
              || (opline+1)->opcode == ZEND_JMPNZ)
2240
0
             && (opline+1)->op1_type == IS_TMP_VAR
2241
0
             && (opline+1)->op1.var == opline->result.var) {
2242
0
              i++;
2243
0
              smart_branch_opcode = (opline+1)->opcode;
2244
0
              target_label = ssa->cfg.blocks[b].successors[0];
2245
0
              target_label2 = ssa->cfg.blocks[b].successors[1];
2246
0
            } else {
2247
0
              smart_branch_opcode = 0;
2248
0
              target_label = target_label2 = (uint32_t)-1;
2249
0
            }
2250
0
            if (!zend_jit_defined(&ctx, opline, smart_branch_opcode, target_label, target_label2, NULL)) {
2251
0
              goto jit_failure;
2252
0
            }
2253
0
            goto done;
2254
0
          case ZEND_TYPE_CHECK:
2255
0
            if (opline->extended_value == MAY_BE_RESOURCE) {
2256
              // TODO: support for is_resource() ???
2257
0
              break;
2258
0
            }
2259
0
            if ((opline->result_type & IS_TMP_VAR)
2260
0
             && (i + 1) <= end
2261
0
             && ((opline+1)->opcode == ZEND_JMPZ
2262
0
              || (opline+1)->opcode == ZEND_JMPNZ)
2263
0
             && (opline+1)->op1_type == IS_TMP_VAR
2264
0
             && (opline+1)->op1.var == opline->result.var) {
2265
0
              i++;
2266
0
              smart_branch_opcode = (opline+1)->opcode;
2267
0
              target_label = ssa->cfg.blocks[b].successors[0];
2268
0
              target_label2 = ssa->cfg.blocks[b].successors[1];
2269
0
            } else {
2270
0
              smart_branch_opcode = 0;
2271
0
              target_label = target_label2 = (uint32_t)-1;
2272
0
            }
2273
0
            if (!zend_jit_type_check(&ctx, opline, OP1_INFO(), smart_branch_opcode, target_label, target_label2, NULL)) {
2274
0
              goto jit_failure;
2275
0
            }
2276
0
            goto done;
2277
0
          case ZEND_RETURN:
2278
0
            op1_info = OP1_INFO();
2279
0
            if ((PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info))
2280
0
             || op_array->type == ZEND_EVAL_CODE
2281
             // TODO: support for top-level code
2282
0
             || !op_array->function_name
2283
             // TODO: support for IS_UNDEF ???
2284
0
             || (op1_info & MAY_BE_UNDEF)) {
2285
0
              if (!zend_jit_tail_handler(&ctx, opline)) {
2286
0
                goto jit_failure;
2287
0
              }
2288
0
            } else {
2289
0
              if (!zend_jit_return(&ctx, opline, op_array,
2290
0
                  op1_info, OP1_REG_ADDR())) {
2291
0
                goto jit_failure;
2292
0
              }
2293
0
            }
2294
0
            goto done;
2295
0
          case ZEND_BOOL:
2296
0
          case ZEND_BOOL_NOT:
2297
0
            if (!zend_jit_bool_jmpznz(&ctx, opline,
2298
0
                OP1_INFO(), OP1_REG_ADDR(), RES_REG_ADDR(),
2299
0
                -1, -1,
2300
0
                zend_may_throw(opline, ssa_op, op_array, ssa),
2301
0
                opline->opcode, NULL)) {
2302
0
              goto jit_failure;
2303
0
            }
2304
0
            goto done;
2305
0
          case ZEND_JMPZ:
2306
0
          case ZEND_JMPNZ:
2307
0
            if (opline > op_array->opcodes + ssa->cfg.blocks[b].start &&
2308
0
                ((opline-1)->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
2309
              /* smart branch */
2310
0
              if (!zend_jit_cond_jmp(&ctx, opline + 1, ssa->cfg.blocks[b].successors[0])) {
2311
0
                goto jit_failure;
2312
0
              }
2313
0
              goto done;
2314
0
            }
2315
0
            ZEND_FALLTHROUGH;
2316
0
          case ZEND_JMPZ_EX:
2317
0
          case ZEND_JMPNZ_EX:
2318
0
            if (opline->result_type == IS_UNDEF) {
2319
0
              res_addr = 0;
2320
0
            } else {
2321
0
              res_addr = RES_REG_ADDR();
2322
0
            }
2323
0
            if (!zend_jit_bool_jmpznz(&ctx, opline,
2324
0
                OP1_INFO(), OP1_REG_ADDR(), res_addr,
2325
0
                ssa->cfg.blocks[b].successors[0], ssa->cfg.blocks[b].successors[1],
2326
0
                zend_may_throw(opline, ssa_op, op_array, ssa),
2327
0
                opline->opcode, NULL)) {
2328
0
              goto jit_failure;
2329
0
            }
2330
0
            goto done;
2331
0
          case ZEND_ISSET_ISEMPTY_CV:
2332
0
            if ((opline->extended_value & ZEND_ISEMPTY)) {
2333
              // TODO: support for empty() ???
2334
0
              break;
2335
0
            }
2336
0
            if ((opline->result_type & IS_TMP_VAR)
2337
0
             && (i + 1) <= end
2338
0
             && ((opline+1)->opcode == ZEND_JMPZ
2339
0
              || (opline+1)->opcode == ZEND_JMPNZ)
2340
0
             && (opline+1)->op1_type == IS_TMP_VAR
2341
0
             && (opline+1)->op1.var == opline->result.var) {
2342
0
              i++;
2343
0
              smart_branch_opcode = (opline+1)->opcode;
2344
0
              target_label = ssa->cfg.blocks[b].successors[0];
2345
0
              target_label2 = ssa->cfg.blocks[b].successors[1];
2346
0
            } else {
2347
0
              smart_branch_opcode = 0;
2348
0
              target_label = target_label2 = (uint32_t)-1;
2349
0
            }
2350
0
            if (!zend_jit_isset_isempty_cv(&ctx, opline,
2351
0
                OP1_INFO(), OP1_REG_ADDR(),
2352
0
                smart_branch_opcode, target_label, target_label2,
2353
0
                NULL)) {
2354
0
              goto jit_failure;
2355
0
            }
2356
0
            goto done;
2357
0
          case ZEND_IN_ARRAY:
2358
0
            if (opline->op1_type == IS_VAR || opline->op1_type == IS_TMP_VAR) {
2359
0
              break;
2360
0
            }
2361
0
            op1_info = OP1_INFO();
2362
0
            if ((op1_info & (MAY_BE_ANY|MAY_BE_UNDEF|MAY_BE_REF)) != MAY_BE_STRING) {
2363
0
              break;
2364
0
            }
2365
0
            if ((opline->result_type & IS_TMP_VAR)
2366
0
             && (i + 1) <= end
2367
0
             && ((opline+1)->opcode == ZEND_JMPZ
2368
0
              || (opline+1)->opcode == ZEND_JMPNZ)
2369
0
             && (opline+1)->op1_type == IS_TMP_VAR
2370
0
             && (opline+1)->op1.var == opline->result.var) {
2371
0
              i++;
2372
0
              smart_branch_opcode = (opline+1)->opcode;
2373
0
              target_label = ssa->cfg.blocks[b].successors[0];
2374
0
              target_label2 = ssa->cfg.blocks[b].successors[1];
2375
0
            } else {
2376
0
              smart_branch_opcode = 0;
2377
0
              target_label = target_label2 = (uint32_t)-1;
2378
0
            }
2379
0
            if (!zend_jit_in_array(&ctx, opline,
2380
0
                op1_info, OP1_REG_ADDR(),
2381
0
                smart_branch_opcode, target_label, target_label2,
2382
0
                NULL)) {
2383
0
              goto jit_failure;
2384
0
            }
2385
0
            goto done;
2386
0
          case ZEND_FETCH_DIM_R:
2387
0
          case ZEND_FETCH_DIM_IS:
2388
0
          case ZEND_FETCH_LIST_R:
2389
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
2390
0
              break;
2391
0
            }
2392
0
            if (!zend_jit_fetch_dim_read(&ctx, opline, ssa, ssa_op,
2393
0
                OP1_INFO(), OP1_REG_ADDR(), 0,
2394
0
                OP2_INFO(), OP2_REG_ADDR(), OP2_RANGE(),
2395
0
                RES_INFO(), RES_REG_ADDR(), IS_UNKNOWN)) {
2396
0
              goto jit_failure;
2397
0
            }
2398
0
            goto done;
2399
0
          case ZEND_FETCH_DIM_W:
2400
0
          case ZEND_FETCH_DIM_RW:
2401
//          case ZEND_FETCH_DIM_UNSET:
2402
0
          case ZEND_FETCH_LIST_W:
2403
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
2404
0
              break;
2405
0
            }
2406
0
            if (opline->op1_type != IS_CV) {
2407
0
              break;
2408
0
            }
2409
0
            if (!zend_jit_fetch_dim(&ctx, opline,
2410
0
                OP1_INFO(), OP1_REG_ADDR(),
2411
0
                OP2_INFO(), (opline->op2_type != IS_UNUSED) ? OP2_REG_ADDR() : 0,
2412
0
                (opline->op2_type != IS_UNUSED) ? OP2_RANGE() : 0,
2413
0
                RES_REG_ADDR(), IS_UNKNOWN)) {
2414
0
              goto jit_failure;
2415
0
            }
2416
0
            goto done;
2417
0
          case ZEND_ISSET_ISEMPTY_DIM_OBJ:
2418
0
            if ((opline->extended_value & ZEND_ISEMPTY)) {
2419
              // TODO: support for empty() ???
2420
0
              break;
2421
0
            }
2422
0
            if (PROFITABILITY_CHECKS && (!ssa->ops || !ssa->var_info)) {
2423
0
              break;
2424
0
            }
2425
0
            if ((opline->result_type & IS_TMP_VAR)
2426
0
             && (i + 1) <= end
2427
0
             && ((opline+1)->opcode == ZEND_JMPZ
2428
0
              || (opline+1)->opcode == ZEND_JMPNZ)
2429
0
             && (opline+1)->op1_type == IS_TMP_VAR
2430
0
             && (opline+1)->op1.var == opline->result.var) {
2431
0
              i++;
2432
0
              smart_branch_opcode = (opline+1)->opcode;
2433
0
              target_label = ssa->cfg.blocks[b].successors[0];
2434
0
              target_label2 = ssa->cfg.blocks[b].successors[1];
2435
0
            } else {
2436
0
              smart_branch_opcode = 0;
2437
0
              target_label = target_label2 = (uint32_t)-1;
2438
0
            }
2439
0
            if (!zend_jit_isset_isempty_dim(&ctx, opline,
2440
0
                OP1_INFO(), OP1_REG_ADDR(), 0,
2441
0
                OP2_INFO(), OP2_REG_ADDR(), OP2_RANGE(), IS_UNKNOWN,
2442
0
                zend_may_throw(opline, ssa_op, op_array, ssa),
2443
0
                smart_branch_opcode, target_label, target_label2,
2444
0
                NULL)) {
2445
0
              goto jit_failure;
2446
0
            }
2447
0
            goto done;
2448
0
          case ZEND_FETCH_OBJ_R:
2449
0
          case ZEND_FETCH_OBJ_IS:
2450
0
          case ZEND_FETCH_OBJ_W:
2451
0
            ce = NULL;
2452
0
            ce_is_instanceof = false;
2453
0
            on_this = false;
2454
0
            if (opline->op1_type == IS_UNUSED) {
2455
0
              op1_info = MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN;
2456
0
              op1_addr = 0;
2457
0
              ce = op_array->scope;
2458
              /* scope is NULL for closures. */
2459
0
              if (ce) {
2460
0
                ce_is_instanceof = !(ce->ce_flags & ZEND_ACC_FINAL);
2461
0
              }
2462
0
              on_this = true;
2463
0
            } else {
2464
0
              op1_info = OP1_INFO();
2465
0
              if (!(op1_info & MAY_BE_OBJECT)) {
2466
0
                break;
2467
0
              }
2468
0
              op1_addr = OP1_REG_ADDR();
2469
0
              if (ssa->var_info && ssa->ops) {
2470
0
                zend_ssa_op *ssa_op = &ssa->ops[opline - op_array->opcodes];
2471
0
                if (ssa_op->op1_use >= 0) {
2472
0
                  zend_ssa_var_info *op1_ssa = ssa->var_info + ssa_op->op1_use;
2473
0
                  if (op1_ssa->ce && !op1_ssa->ce->create_object) {
2474
0
                    ce = op1_ssa->ce;
2475
0
                    ce_is_instanceof = op1_ssa->is_instanceof;
2476
0
                  }
2477
0
                }
2478
0
              }
2479
0
            }
2480
0
            if (opline->op2_type != IS_CONST
2481
0
             || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING
2482
0
             || Z_STRVAL_P(RT_CONSTANT(opline, opline->op2))[0] == '\0') {
2483
0
              break;
2484
0
            }
2485
0
            if (!zend_jit_fetch_obj(&ctx, opline, op_array, ssa, ssa_op,
2486
0
                op1_info, op1_addr, 0, ce, ce_is_instanceof, on_this, 0, 0, NULL,
2487
0
                RES_REG_ADDR(), IS_UNKNOWN,
2488
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
2489
0
              goto jit_failure;
2490
0
            }
2491
0
            goto done;
2492
0
          case ZEND_FETCH_STATIC_PROP_R:
2493
0
          case ZEND_FETCH_STATIC_PROP_IS:
2494
0
          case ZEND_FETCH_STATIC_PROP_W:
2495
0
          case ZEND_FETCH_STATIC_PROP_RW:
2496
0
          case ZEND_FETCH_STATIC_PROP_UNSET:
2497
0
            if (!(opline->op1_type == IS_CONST
2498
0
             && (opline->op2_type == IS_CONST
2499
0
              || (opline->op2_type == IS_UNUSED
2500
0
               && ((opline->op2.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
2501
0
                || (opline->op2.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_PARENT))))) {
2502
0
              break;
2503
0
            }
2504
0
            if (!zend_jit_fetch_static_prop(&ctx, opline, op_array)) {
2505
0
              goto jit_failure;
2506
0
            }
2507
0
            goto done;
2508
0
          case ZEND_BIND_GLOBAL:
2509
0
            if (!ssa->ops || !ssa->var_info) {
2510
0
              op1_info = MAY_BE_ANY|MAY_BE_REF;
2511
0
            } else {
2512
0
              op1_info = OP1_INFO();
2513
0
            }
2514
0
            if (!zend_jit_bind_global(&ctx, opline, op1_info)) {
2515
0
              goto jit_failure;
2516
0
            }
2517
0
            goto done;
2518
0
          case ZEND_RECV:
2519
0
            if (!zend_jit_recv(&ctx, opline, op_array)) {
2520
0
              goto jit_failure;
2521
0
            }
2522
0
            goto done;
2523
0
          case ZEND_RECV_INIT:
2524
0
            if (!zend_jit_recv_init(&ctx, opline, op_array,
2525
0
                (opline + 1)->opcode != ZEND_RECV_INIT,
2526
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
2527
0
              goto jit_failure;
2528
0
            }
2529
0
            goto done;
2530
0
          case ZEND_FREE:
2531
0
          case ZEND_FE_FREE:
2532
0
            if (!zend_jit_free(&ctx, opline, OP1_INFO(),
2533
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
2534
0
              goto jit_failure;
2535
0
            }
2536
0
            goto done;
2537
0
          case ZEND_ECHO:
2538
0
            op1_info = OP1_INFO();
2539
0
            if ((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_STRING) {
2540
0
              break;
2541
0
            }
2542
0
            if (!zend_jit_echo(&ctx, opline, op1_info)) {
2543
0
              goto jit_failure;
2544
0
            }
2545
0
            goto done;
2546
0
          case ZEND_STRLEN:
2547
0
            op1_info = OP1_INFO();
2548
0
            if ((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_STRING) {
2549
0
              break;
2550
0
            }
2551
0
            if (!zend_jit_strlen(&ctx, opline, op1_info, OP1_REG_ADDR(), RES_REG_ADDR())) {
2552
0
              goto jit_failure;
2553
0
            }
2554
0
            goto done;
2555
0
          case ZEND_COUNT:
2556
0
            op1_info = OP1_INFO();
2557
0
            if ((op1_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_ARRAY) {
2558
0
              break;
2559
0
            }
2560
0
            if (!zend_jit_count(&ctx, opline, op1_info, OP1_REG_ADDR(), RES_REG_ADDR(), zend_may_throw(opline, ssa_op, op_array, ssa))) {
2561
0
              goto jit_failure;
2562
0
            }
2563
0
            goto done;
2564
0
          case ZEND_FETCH_THIS:
2565
0
            if (!zend_jit_fetch_this(&ctx, opline, op_array, 0)) {
2566
0
              goto jit_failure;
2567
0
            }
2568
0
            goto done;
2569
0
          case ZEND_SWITCH_LONG:
2570
0
          case ZEND_SWITCH_STRING:
2571
0
          case ZEND_MATCH:
2572
0
            if (!zend_jit_switch(&ctx, opline, op_array, ssa, NULL, NULL)) {
2573
0
              goto jit_failure;
2574
0
            }
2575
0
            goto done;
2576
0
          case ZEND_VERIFY_RETURN_TYPE:
2577
0
            if (opline->op1_type == IS_UNUSED) {
2578
              /* Always throws */
2579
0
              break;
2580
0
            }
2581
0
            if (opline->op1_type == IS_CONST) {
2582
              /* TODO Different instruction format, has return value */
2583
0
              break;
2584
0
            }
2585
0
            if (op_array->fn_flags & ZEND_ACC_RETURN_REFERENCE) {
2586
              /* Not worth bothering with */
2587
0
              break;
2588
0
            }
2589
0
            if (OP1_INFO() & MAY_BE_REF) {
2590
              /* TODO May need reference unwrapping. */
2591
0
              break;
2592
0
            }
2593
0
            if (!zend_jit_verify_return_type(&ctx, opline, op_array, OP1_INFO())) {
2594
0
              goto jit_failure;
2595
0
            }
2596
0
            goto done;
2597
0
          case ZEND_FE_RESET_R:
2598
0
            op1_info = OP1_INFO();
2599
0
            if ((op1_info & (MAY_BE_ANY|MAY_BE_REF|MAY_BE_UNDEF)) != MAY_BE_ARRAY) {
2600
0
              break;
2601
0
            }
2602
0
            if (!zend_jit_fe_reset(&ctx, opline, op1_info)) {
2603
0
              goto jit_failure;
2604
0
            }
2605
0
            goto done;
2606
0
          case ZEND_FE_FETCH_R:
2607
0
            op1_info = OP1_INFO();
2608
0
            if ((op1_info & MAY_BE_ANY) != MAY_BE_ARRAY) {
2609
0
              break;
2610
0
            }
2611
0
            if (!zend_jit_fe_fetch(&ctx, opline, op1_info, OP2_INFO(),
2612
0
                ssa->cfg.blocks[b].successors[0], opline->opcode, NULL)) {
2613
0
              goto jit_failure;
2614
0
            }
2615
0
            goto done;
2616
0
          case ZEND_FETCH_CONSTANT:
2617
0
            if (!zend_jit_fetch_constant(&ctx, opline, op_array, ssa, ssa_op, RES_REG_ADDR())) {
2618
0
              goto jit_failure;
2619
0
            }
2620
0
            goto done;
2621
0
          case ZEND_JMP_FRAMELESS:
2622
0
            if (!zend_jit_jmp_frameless(&ctx, opline, /* exit_addr */ NULL, /* guard */ 0)) {
2623
0
              goto jit_failure;
2624
0
            }
2625
0
            goto done;
2626
0
          case ZEND_INIT_METHOD_CALL:
2627
0
            if (opline->op2_type != IS_CONST
2628
0
             || Z_TYPE_P(RT_CONSTANT(opline, opline->op2)) != IS_STRING) {
2629
0
              break;
2630
0
            }
2631
0
            ce = NULL;
2632
0
            ce_is_instanceof = false;
2633
0
            on_this = false;
2634
0
            if (opline->op1_type == IS_UNUSED) {
2635
0
              op1_info = MAY_BE_OBJECT|MAY_BE_RC1|MAY_BE_RCN;
2636
0
              op1_addr = 0;
2637
0
              ce = op_array->scope;
2638
              /* scope is NULL for closures. */
2639
0
              if (ce) {
2640
0
                ce_is_instanceof = !(ce->ce_flags & ZEND_ACC_FINAL);
2641
0
              }
2642
0
              on_this = true;
2643
0
            } else {
2644
0
              op1_info = OP1_INFO();
2645
0
              if (!(op1_info & MAY_BE_OBJECT)) {
2646
0
                break;
2647
0
              }
2648
0
              op1_addr = OP1_REG_ADDR();
2649
0
              if (ssa->var_info && ssa->ops) {
2650
0
                zend_ssa_op *ssa_op = &ssa->ops[opline - op_array->opcodes];
2651
0
                if (ssa_op->op1_use >= 0) {
2652
0
                  zend_ssa_var_info *op1_ssa = ssa->var_info + ssa_op->op1_use;
2653
0
                  if (op1_ssa->ce && !op1_ssa->ce->create_object) {
2654
0
                    ce = op1_ssa->ce;
2655
0
                    ce_is_instanceof = op1_ssa->is_instanceof;
2656
0
                  }
2657
0
                }
2658
0
              }
2659
0
            }
2660
0
            if (!zend_jit_init_method_call(&ctx, opline, b, op_array, ssa, ssa_op, call_level,
2661
0
                op1_info, op1_addr, ce, ce_is_instanceof, on_this, 0, NULL,
2662
0
                NULL, 0,
2663
0
                -1, -1,
2664
0
                0)) {
2665
0
              goto jit_failure;
2666
0
            }
2667
0
            goto done;
2668
0
          case ZEND_INIT_STATIC_METHOD_CALL:
2669
0
            if (!(opline->op2_type == IS_CONST
2670
0
             && (opline->op1_type == IS_CONST
2671
0
              || (opline->op1_type == IS_UNUSED
2672
0
               && ((opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_SELF
2673
0
                || (opline->op1.num & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_PARENT))))) {
2674
0
              break;
2675
0
            }
2676
0
            if (!zend_jit_init_static_method_call(&ctx, opline, b, op_array, ssa, ssa_op, call_level,
2677
0
                NULL, 0)) {
2678
0
              goto jit_failure;
2679
0
            }
2680
0
            goto done;
2681
0
          case ZEND_ROPE_INIT:
2682
0
          case ZEND_ROPE_ADD:
2683
0
          case ZEND_ROPE_END:
2684
0
            op2_info = OP2_INFO();
2685
0
            if ((op2_info & (MAY_BE_UNDEF|MAY_BE_ANY|MAY_BE_REF)) != MAY_BE_STRING) {
2686
0
              break;
2687
0
            }
2688
0
            if (!zend_jit_rope(&ctx, opline, op2_info)) {
2689
0
              goto jit_failure;
2690
0
            }
2691
0
            goto done;
2692
0
          case ZEND_FRAMELESS_ICALL_0:
2693
0
            jit_frameless_icall0(jit, opline);
2694
0
            goto done;
2695
0
          case ZEND_FRAMELESS_ICALL_1:
2696
0
            op1_info = OP1_INFO();
2697
0
            jit_frameless_icall1(jit, opline, op1_info);
2698
0
            goto done;
2699
0
          case ZEND_FRAMELESS_ICALL_2:
2700
0
            op1_info = OP1_INFO();
2701
0
            op2_info = OP2_INFO();
2702
0
            jit_frameless_icall2(jit, opline, op1_info, op2_info);
2703
0
            goto done;
2704
0
          case ZEND_FRAMELESS_ICALL_3:
2705
0
            op1_info = OP1_INFO();
2706
0
            op2_info = OP2_INFO();
2707
0
            jit_frameless_icall3(jit, opline, op1_info, op2_info, OP1_DATA_INFO());
2708
0
            goto done;
2709
0
          default:
2710
0
            break;
2711
0
        }
2712
0
      }
2713
2714
0
      switch (opline->opcode) {
2715
0
        case ZEND_RECV_INIT:
2716
0
        case ZEND_BIND_GLOBAL:
2717
0
          if (opline == op_array->opcodes ||
2718
0
              opline->opcode != op_array->opcodes[i-1].opcode) {
2719
            /* repeatable opcodes */
2720
0
            if (!zend_jit_handler(&ctx, opline,
2721
0
                zend_may_throw(opline, ssa_op, op_array, ssa))) {
2722
0
              goto jit_failure;
2723
0
            }
2724
0
          }
2725
0
          zend_jit_set_last_valid_opline(&ctx, opline+1);
2726
0
          break;
2727
0
        case ZEND_NOP:
2728
0
        case ZEND_OP_DATA:
2729
0
        case ZEND_SWITCH_LONG:
2730
0
        case ZEND_SWITCH_STRING:
2731
0
          break;
2732
0
        case ZEND_MATCH:
2733
          /* We have to exit to the VM because the MATCH handler performs an N-way jump for
2734
           * which we can't generate simple (opcache.jit=1201) JIT code. */
2735
0
          if (!zend_jit_tail_handler(&ctx, opline)) {
2736
0
            goto jit_failure;
2737
0
          }
2738
0
          break;
2739
0
        case ZEND_JMP:
2740
0
          if (JIT_G(opt_level) < ZEND_JIT_LEVEL_INLINE) {
2741
0
            const zend_op *target = OP_JMP_ADDR(opline, opline->op1);
2742
2743
0
            if (!zend_jit_set_ip(&ctx, target)) {
2744
0
              goto jit_failure;
2745
0
            }
2746
0
          }
2747
0
          break;
2748
0
        case ZEND_CATCH:
2749
0
        case ZEND_FAST_CALL:
2750
0
        case ZEND_FAST_RET:
2751
0
        case ZEND_GENERATOR_CREATE:
2752
0
        case ZEND_GENERATOR_RETURN:
2753
0
        case ZEND_RETURN_BY_REF:
2754
0
        case ZEND_RETURN:
2755
0
        case ZEND_MATCH_ERROR:
2756
        /* switch through trampoline */
2757
0
        case ZEND_YIELD:
2758
0
        case ZEND_YIELD_FROM:
2759
0
        case ZEND_THROW:
2760
0
        case ZEND_VERIFY_NEVER_TYPE:
2761
0
          if (!zend_jit_tail_handler(&ctx, opline)) {
2762
0
            goto jit_failure;
2763
0
          }
2764
          /* THROW and EXIT may be used in the middle of BB */
2765
          /* don't generate code for the rest of BB */
2766
2767
          /* Skip current opline for call_level computation because it does not influence call_level.
2768
           * Don't include last opline because end of loop already checks call level of last opline. */
2769
0
          i++;
2770
0
          for (; i < end; i++) {
2771
0
            opline = op_array->opcodes + i;
2772
0
            if (zend_jit_inc_call_level(opline->opcode)) {
2773
0
              call_level++;
2774
0
            } else if (zend_jit_dec_call_level(opline->opcode)) {
2775
0
              call_level--;
2776
0
            }
2777
0
          }
2778
0
          opline = op_array->opcodes + end;
2779
0
          break;
2780
        /* stackless execution */
2781
0
        case ZEND_INCLUDE_OR_EVAL:
2782
0
        case ZEND_DO_FCALL:
2783
0
        case ZEND_DO_UCALL:
2784
0
        case ZEND_DO_FCALL_BY_NAME:
2785
0
          if (!zend_jit_call(&ctx, opline, b + 1)) {
2786
0
            goto jit_failure;
2787
0
          }
2788
0
          break;
2789
0
        case ZEND_JMPZ:
2790
0
        case ZEND_JMPNZ:
2791
0
          if (opline > op_array->opcodes + ssa->cfg.blocks[b].start &&
2792
0
              ((opline-1)->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
2793
            /* smart branch */
2794
0
            if (!zend_jit_cond_jmp(&ctx, opline + 1, ssa->cfg.blocks[b].successors[0])) {
2795
0
              goto jit_failure;
2796
0
            }
2797
0
            goto done;
2798
0
          }
2799
0
          ZEND_FALLTHROUGH;
2800
0
        case ZEND_JMPZ_EX:
2801
0
        case ZEND_JMPNZ_EX:
2802
0
        case ZEND_JMP_SET:
2803
0
        case ZEND_COALESCE:
2804
0
        case ZEND_JMP_NULL:
2805
0
        case ZEND_FE_RESET_R:
2806
0
        case ZEND_FE_RESET_RW:
2807
0
        case ZEND_ASSERT_CHECK:
2808
0
        case ZEND_FE_FETCH_R:
2809
0
        case ZEND_FE_FETCH_RW:
2810
0
        case ZEND_BIND_INIT_STATIC_OR_JMP:
2811
0
        case ZEND_JMP_FRAMELESS:
2812
0
          if (!zend_jit_handler(&ctx, opline,
2813
0
              zend_may_throw(opline, ssa_op, op_array, ssa)) ||
2814
0
              !zend_jit_cond_jmp(&ctx, opline + 1, ssa->cfg.blocks[b].successors[0])) {
2815
0
            goto jit_failure;
2816
0
          }
2817
0
          break;
2818
0
        case ZEND_NEW:
2819
0
          if (!zend_jit_handler(&ctx, opline, 1)) {
2820
0
            return 0;
2821
0
          }
2822
0
          if (opline->extended_value == 0 && (opline+1)->opcode == ZEND_DO_FCALL) {
2823
0
            zend_class_entry *ce = NULL;
2824
2825
0
            if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_OPT_FUNC) {
2826
0
              if (ssa->ops && ssa->var_info) {
2827
0
                zend_ssa_var_info *res_ssa = &ssa->var_info[ssa->ops[opline - op_array->opcodes].result_def];
2828
0
                if (res_ssa->ce && !res_ssa->is_instanceof) {
2829
0
                  ce = res_ssa->ce;
2830
0
                }
2831
0
              }
2832
0
            } else {
2833
0
              if (opline->op1_type == IS_CONST) {
2834
0
                zval *zv = RT_CONSTANT(opline, opline->op1);
2835
0
                if (Z_TYPE_P(zv) == IS_STRING) {
2836
0
                  zval *lc = zv + 1;
2837
0
                  ce = (zend_class_entry*)zend_hash_find_ptr(EG(class_table), Z_STR_P(lc));
2838
0
                }
2839
0
              }
2840
0
            }
2841
2842
0
            i++;
2843
2844
0
            if (!ce || !(ce->ce_flags & ZEND_ACC_LINKED) || ce->constructor) {
2845
0
              const zend_op *next_opline = opline + 1;
2846
2847
0
              ZEND_ASSERT(b + 1 == ssa->cfg.blocks[b].successors[0]);
2848
0
              zend_jit_constructor(&ctx, next_opline, op_array, ssa, call_level, b + 1);
2849
0
            }
2850
2851
            /* We skip over the DO_FCALL, so decrement call_level ourselves. */
2852
0
            call_level--;
2853
0
          }
2854
0
          break;
2855
0
        case ZEND_FETCH_OBJ_R:
2856
0
          if (!zend_jit_handler(&ctx, opline,
2857
0
            zend_may_throw(opline, ssa_op, op_array, ssa))) {
2858
0
            goto jit_failure;
2859
0
          }
2860
2861
          /* Cache slot is only used for IS_CONST op2, so only that can result in hook fast path. */
2862
0
          if (opline->op2_type == IS_CONST) {
2863
0
            if (JIT_G(opt_level) < ZEND_JIT_LEVEL_INLINE) {
2864
0
              if (opline->op1_type == IS_UNUSED) {
2865
0
                ce = op_array->scope;
2866
0
              } else {
2867
0
                ce = NULL;
2868
0
              }
2869
0
            }
2870
2871
0
            if (!ce || !(ce->ce_flags & ZEND_ACC_FINAL) || ce->num_hooked_props > 0) {
2872
              /* If a simple hook is called, exit to the VM. */
2873
0
              ir_ref if_hook_enter = ir_IF(jit_CMP_IP(jit, IR_EQ, opline + 1));
2874
0
              ir_IF_FALSE(if_hook_enter);
2875
0
              if (GCC_GLOBAL_REGS) {
2876
0
                ir_TAILCALL(IR_VOID, ir_LOAD_A(jit_IP(jit)));
2877
0
              } else {
2878
0
                zend_jit_vm_enter(jit, jit_IP(jit));
2879
0
              }
2880
0
              ir_IF_TRUE(if_hook_enter);
2881
0
            }
2882
0
          }
2883
2884
0
          break;
2885
0
        default:
2886
0
          if (!zend_jit_handler(&ctx, opline,
2887
0
              zend_may_throw(opline, ssa_op, op_array, ssa))) {
2888
0
            goto jit_failure;
2889
0
          }
2890
0
          if (i == end
2891
0
           && (opline->result_type & (IS_SMART_BRANCH_JMPZ|IS_SMART_BRANCH_JMPNZ)) != 0) {
2892
            /* smart branch split across basic blocks */
2893
0
            if (!zend_jit_set_cond(&ctx, opline + 2, opline->result.var)) {
2894
0
              goto jit_failure;
2895
0
            }
2896
0
          }
2897
0
      }
2898
0
done:
2899
0
      if (zend_jit_dec_call_level(opline->opcode)) {
2900
0
        call_level--;
2901
0
      }
2902
0
    }
2903
0
    zend_jit_bb_end(&ctx, b);
2904
0
  }
2905
2906
0
  if (jit->return_inputs) {
2907
0
    zend_jit_common_return(jit);
2908
2909
0
    bool left_frame = false;
2910
0
    if (op_array->last_var > 100) {
2911
      /* To many CVs to unroll */
2912
0
      if (!zend_jit_free_cvs(&ctx)) {
2913
0
        goto jit_failure;
2914
0
      }
2915
0
      left_frame = true;
2916
0
    }
2917
0
    if (!left_frame) {
2918
0
      int j;
2919
2920
0
      for (j = 0 ; j < op_array->last_var; j++) {
2921
0
        uint32_t info = zend_ssa_cv_info(op_array, ssa, j);
2922
2923
0
        if (info & (MAY_BE_STRING|MAY_BE_ARRAY|MAY_BE_OBJECT|MAY_BE_RESOURCE|MAY_BE_REF)) {
2924
0
          if (!left_frame) {
2925
0
            left_frame = true;
2926
0
              if (!zend_jit_leave_frame(&ctx)) {
2927
0
              goto jit_failure;
2928
0
              }
2929
0
          }
2930
0
          if (!zend_jit_free_cv(&ctx, info, j)) {
2931
0
            goto jit_failure;
2932
0
          }
2933
0
        }
2934
0
      }
2935
0
    }
2936
0
    if (!zend_jit_leave_func(&ctx, op_array, NULL, MAY_BE_ANY, left_frame,
2937
0
        NULL, NULL, (ssa->cfg.flags & ZEND_FUNC_INDIRECT_VAR_ACCESS) != 0, 1)) {
2938
0
      goto jit_failure;
2939
0
    }
2940
0
  }
2941
2942
0
  handler = zend_jit_finish(&ctx);
2943
0
  if (!handler) {
2944
0
    goto jit_failure;
2945
0
  }
2946
0
  zend_jit_free_ctx(&ctx);
2947
2948
0
  if (JIT_G(opt_flags) & (ZEND_JIT_REG_ALLOC_LOCAL|ZEND_JIT_REG_ALLOC_GLOBAL)) {
2949
0
    zend_arena_release(&CG(arena), checkpoint);
2950
0
  }
2951
0
  return SUCCESS;
2952
2953
0
jit_failure:
2954
0
  zend_jit_free_ctx(&ctx);
2955
0
  if (JIT_G(opt_flags) & (ZEND_JIT_REG_ALLOC_LOCAL|ZEND_JIT_REG_ALLOC_GLOBAL)) {
2956
0
    zend_arena_release(&CG(arena), checkpoint);
2957
0
  }
2958
0
  return FAILURE;
2959
0
}
2960
2961
static void zend_jit_collect_calls(zend_op_array *op_array, zend_script *script)
2962
0
{
2963
0
  zend_func_info *func_info;
2964
2965
0
  if (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC ||
2966
0
      JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST ||
2967
0
      JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS) {
2968
0
      func_info = ZEND_FUNC_INFO(op_array);
2969
0
  } else {
2970
0
    func_info = zend_arena_calloc(&CG(arena), 1, sizeof(zend_func_info));
2971
0
    ZEND_SET_FUNC_INFO(op_array, func_info);
2972
0
  }
2973
0
  zend_analyze_calls(&CG(arena), script, ZEND_CALL_TREE, op_array, func_info);
2974
0
}
2975
2976
static void zend_jit_cleanup_func_info(zend_op_array *op_array)
2977
0
{
2978
0
  zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
2979
0
  zend_call_info *caller_info, *callee_info;
2980
2981
0
  if (func_info) {
2982
0
    caller_info = func_info->caller_info;
2983
0
    callee_info = func_info->callee_info;
2984
2985
0
    if (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC ||
2986
0
        JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST ||
2987
0
        JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS) {
2988
0
      func_info->num = 0;
2989
0
      func_info->flags &= ZEND_FUNC_JIT_ON_FIRST_EXEC
2990
0
        | ZEND_FUNC_JIT_ON_PROF_REQUEST
2991
0
        | ZEND_FUNC_JIT_ON_HOT_COUNTERS
2992
0
        | ZEND_FUNC_JIT_ON_HOT_TRACE;
2993
0
      memset(&func_info->ssa, 0, sizeof(zend_func_info) - offsetof(zend_func_info, ssa));
2994
0
    } else {
2995
0
      ZEND_SET_FUNC_INFO(op_array, NULL);
2996
0
    }
2997
2998
0
    while (caller_info) {
2999
0
      if (caller_info->caller_op_array) {
3000
0
        zend_jit_cleanup_func_info(caller_info->caller_op_array);
3001
0
      }
3002
0
      caller_info = caller_info->next_caller;
3003
0
    }
3004
0
    while (callee_info) {
3005
0
      if (callee_info->callee_func && callee_info->callee_func->type == ZEND_USER_FUNCTION) {
3006
0
        zend_jit_cleanup_func_info(&callee_info->callee_func->op_array);
3007
0
      }
3008
0
      callee_info = callee_info->next_callee;
3009
0
    }
3010
0
  }
3011
0
}
3012
3013
static int zend_real_jit_func(zend_op_array *op_array, zend_script *script, const zend_op *rt_opline, uint8_t trigger)
3014
0
{
3015
0
  zend_ssa ssa;
3016
0
  void *checkpoint;
3017
0
  zend_func_info *func_info;
3018
0
  uint8_t orig_trigger;
3019
3020
0
  if (*dasm_ptr == dasm_end) {
3021
0
    return FAILURE;
3022
0
  }
3023
3024
0
  orig_trigger = JIT_G(trigger);
3025
0
  JIT_G(trigger) = trigger;
3026
0
  checkpoint = zend_arena_checkpoint(CG(arena));
3027
3028
  /* Build SSA */
3029
0
  memset(&ssa, 0, sizeof(zend_ssa));
3030
3031
0
  if (op_array->fn_flags & ZEND_ACC_CLOSURE) {
3032
0
    if (trigger == ZEND_JIT_ON_FIRST_EXEC) {
3033
0
      zend_jit_op_array_extension *jit_extension = (zend_jit_op_array_extension*)ZEND_FUNC_INFO(op_array);
3034
0
      op_array = (zend_op_array*) jit_extension->op_array;
3035
0
    } else if (trigger == ZEND_JIT_ON_HOT_COUNTERS) {
3036
0
      zend_jit_op_array_hot_extension *jit_extension = (zend_jit_op_array_hot_extension*)ZEND_FUNC_INFO(op_array);
3037
0
      op_array = (zend_op_array*) jit_extension->op_array;
3038
0
    } else {
3039
0
      ZEND_ASSERT(!op_array->scope);
3040
0
    }
3041
0
  }
3042
3043
0
  if (zend_jit_op_array_analyze1(op_array, script, &ssa) != SUCCESS) {
3044
0
    goto jit_failure;
3045
0
  }
3046
3047
0
  if (JIT_G(opt_level) >= ZEND_JIT_LEVEL_OPT_FUNCS) {
3048
0
    zend_jit_collect_calls(op_array, script);
3049
0
    func_info = ZEND_FUNC_INFO(op_array);
3050
0
    func_info->call_map = zend_build_call_map(&CG(arena), func_info, op_array);
3051
0
    if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
3052
0
      zend_init_func_return_info(op_array, script, &func_info->return_info);
3053
0
    }
3054
0
  }
3055
3056
0
  if (zend_jit_op_array_analyze2(op_array, script, &ssa, ZCG(accel_directives).optimization_level) != SUCCESS) {
3057
0
    goto jit_failure;
3058
0
  }
3059
3060
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_SSA) {
3061
0
    zend_dump_op_array(op_array, ZEND_DUMP_HIDE_UNREACHABLE|ZEND_DUMP_RC_INFERENCE|ZEND_DUMP_SSA, "JIT", &ssa);
3062
0
  }
3063
3064
0
  if (zend_jit(op_array, &ssa, rt_opline) != SUCCESS) {
3065
0
    goto jit_failure;
3066
0
  }
3067
3068
0
  zend_jit_cleanup_func_info(op_array);
3069
0
  zend_arena_release(&CG(arena), checkpoint);
3070
0
  JIT_G(trigger) = orig_trigger;
3071
0
  return SUCCESS;
3072
3073
0
jit_failure:
3074
0
  zend_jit_cleanup_func_info(op_array);
3075
0
  zend_arena_release(&CG(arena), checkpoint);
3076
0
  JIT_G(trigger) = orig_trigger;
3077
0
  return FAILURE;
3078
0
}
3079
3080
/* Run-time JIT handler */
3081
#if ZEND_VM_KIND == ZEND_VM_KIND_CALL || ZEND_VM_KIND == ZEND_VM_KIND_TAILCALL
3082
static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_CCONV zend_runtime_jit(ZEND_OPCODE_HANDLER_ARGS)
3083
#else
3084
static ZEND_OPCODE_HANDLER_RET ZEND_OPCODE_HANDLER_FUNC_CCONV zend_runtime_jit(ZEND_OPCODE_HANDLER_ARGS)
3085
#endif
3086
0
{
3087
#if GCC_GLOBAL_REGS
3088
  zend_execute_data *execute_data;
3089
  zend_op *opline;
3090
#else
3091
0
  const zend_op *orig_opline = opline;
3092
0
#endif
3093
3094
0
  execute_data = EG(current_execute_data);
3095
0
  zend_op_array *op_array = &EX(func)->op_array;
3096
0
  opline = op_array->opcodes;
3097
0
  zend_jit_op_array_extension *jit_extension;
3098
0
  bool do_bailout = 0;
3099
3100
0
  zend_shared_alloc_lock();
3101
0
  jit_extension = (zend_jit_op_array_extension*)ZEND_FUNC_INFO(op_array);
3102
3103
0
  if (jit_extension && !(jit_extension->func_info.flags & ZEND_FUNC_JITED)) {
3104
3105
0
    SHM_UNPROTECT();
3106
0
    zend_jit_unprotect();
3107
3108
0
    zend_try {
3109
      /* restore original opcode handlers */
3110
0
      if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
3111
0
        while (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT) {
3112
0
          opline++;
3113
0
        }
3114
0
      }
3115
0
      ((zend_op*)opline)->handler = jit_extension->orig_handler;
3116
3117
      /* perform real JIT for this function */
3118
0
      zend_real_jit_func(op_array, NULL, NULL, ZEND_JIT_ON_FIRST_EXEC);
3119
3120
0
      jit_extension->func_info.flags |= ZEND_FUNC_JITED;
3121
0
    } zend_catch {
3122
0
      do_bailout = true;
3123
0
    } zend_end_try();
3124
3125
0
    zend_jit_protect();
3126
0
    SHM_PROTECT();
3127
0
  }
3128
3129
0
  zend_shared_alloc_unlock();
3130
3131
0
  if (do_bailout) {
3132
0
    zend_bailout();
3133
0
  }
3134
3135
  /* JIT-ed code is going to be called by VM */
3136
#if GCC_GLOBAL_REGS
3137
  return; // ZEND_VM_CONTINUE
3138
#else
3139
0
  opline = orig_opline;
3140
0
  ZEND_OPCODE_RETURN();
3141
0
#endif
3142
0
}
3143
3144
0
void zend_jit_check_funcs(HashTable *function_table, bool is_method) {
3145
0
  zend_op *opline;
3146
0
  zend_function *func;
3147
0
  zend_op_array *op_array;
3148
0
  uintptr_t counter;
3149
0
  zend_jit_op_array_extension *jit_extension;
3150
3151
0
  ZEND_HASH_MAP_REVERSE_FOREACH_PTR(function_table, func) {
3152
0
    if (func->type == ZEND_INTERNAL_FUNCTION) {
3153
0
      break;
3154
0
    }
3155
0
    op_array = &func->op_array;
3156
0
    opline = op_array->opcodes;
3157
0
    if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
3158
0
      while (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT) {
3159
0
        opline++;
3160
0
      }
3161
0
    }
3162
0
    if (opline->handler == zend_jit_profile_jit_handler) {
3163
0
      if (!RUN_TIME_CACHE(op_array)) {
3164
0
        continue;
3165
0
      }
3166
0
      counter = (uintptr_t)ZEND_COUNTER_INFO(op_array);
3167
0
      ZEND_COUNTER_INFO(op_array) = 0;
3168
0
      jit_extension = (zend_jit_op_array_extension*)ZEND_FUNC_INFO(op_array);
3169
0
      opline->handler = jit_extension->orig_handler;
3170
0
      if (((double)counter / (double)zend_jit_profile_counter) > JIT_G(prof_threshold)) {
3171
0
        zend_real_jit_func(op_array, NULL, NULL, ZEND_JIT_ON_PROF_REQUEST);
3172
0
      }
3173
0
    }
3174
0
  } ZEND_HASH_FOREACH_END();
3175
0
}
3176
3177
void ZEND_FASTCALL zend_jit_hot_func(zend_execute_data *execute_data, const zend_op *opline)
3178
0
{
3179
0
  zend_op_array *op_array = &EX(func)->op_array;
3180
0
  zend_jit_op_array_hot_extension *jit_extension;
3181
0
  uint32_t i;
3182
0
  bool do_bailout = 0;
3183
3184
0
  zend_shared_alloc_lock();
3185
0
  jit_extension = (zend_jit_op_array_hot_extension*)ZEND_FUNC_INFO(op_array);
3186
3187
0
  if (jit_extension && !(jit_extension->func_info.flags & ZEND_FUNC_JITED)) {
3188
0
    SHM_UNPROTECT();
3189
0
    zend_jit_unprotect();
3190
3191
0
    zend_try {
3192
0
      for (i = 0; i < op_array->last; i++) {
3193
0
        op_array->opcodes[i].handler = jit_extension->orig_handlers[i];
3194
0
      }
3195
3196
0
      EX(opline) = opline;
3197
3198
      /* perform real JIT for this function */
3199
0
      zend_real_jit_func(op_array, NULL, opline, ZEND_JIT_ON_HOT_COUNTERS);
3200
3201
0
      jit_extension->func_info.flags |= ZEND_FUNC_JITED;
3202
0
    } zend_catch {
3203
0
      do_bailout = 1;
3204
0
    } zend_end_try();
3205
3206
0
    zend_jit_protect();
3207
0
    SHM_PROTECT();
3208
0
  }
3209
3210
0
  zend_shared_alloc_unlock();
3211
3212
0
  if (do_bailout) {
3213
0
    zend_bailout();
3214
0
  }
3215
  /* JIT-ed code is going to be called by VM */
3216
0
}
3217
3218
static void zend_jit_setup_hot_counters_ex(zend_op_array *op_array, zend_cfg *cfg)
3219
0
{
3220
0
  if (JIT_G(hot_func)) {
3221
0
    zend_op *opline = op_array->opcodes;
3222
3223
0
    if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
3224
0
      while (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT) {
3225
0
        opline++;
3226
0
      }
3227
0
    }
3228
3229
0
    opline->handler = zend_jit_func_hot_counter_handler;
3230
0
  }
3231
3232
0
  if (JIT_G(hot_loop)) {
3233
0
    uint32_t i;
3234
3235
0
    for (i = 0; i < cfg->blocks_count; i++) {
3236
0
      if ((cfg->blocks[i].flags & ZEND_BB_REACHABLE) &&
3237
0
          (cfg->blocks[i].flags & ZEND_BB_LOOP_HEADER)) {
3238
0
          op_array->opcodes[cfg->blocks[i].start].handler =
3239
0
          zend_jit_loop_hot_counter_handler;
3240
0
      }
3241
0
    }
3242
0
  }
3243
0
}
3244
3245
static int zend_jit_restart_hot_counters(zend_op_array *op_array)
3246
0
{
3247
0
  zend_jit_op_array_hot_extension *jit_extension;
3248
0
  zend_cfg cfg;
3249
0
  uint32_t i;
3250
3251
0
  jit_extension = (zend_jit_op_array_hot_extension*)ZEND_FUNC_INFO(op_array);
3252
0
  for (i = 0; i < op_array->last; i++) {
3253
0
    op_array->opcodes[i].handler = jit_extension->orig_handlers[i];
3254
0
  }
3255
3256
0
  if (zend_jit_build_cfg(op_array, &cfg) != SUCCESS) {
3257
0
    return FAILURE;
3258
0
  }
3259
3260
0
  zend_jit_setup_hot_counters_ex(op_array, &cfg);
3261
3262
0
  return SUCCESS;
3263
0
}
3264
3265
static int zend_jit_setup_hot_counters(zend_op_array *op_array)
3266
0
{
3267
0
  zend_jit_op_array_hot_extension *jit_extension;
3268
0
  zend_cfg cfg;
3269
0
  uint32_t i;
3270
3271
0
  ZEND_ASSERT(!JIT_G(hot_func) || zend_jit_func_hot_counter_handler != NULL);
3272
0
  ZEND_ASSERT(!JIT_G(hot_loop) || zend_jit_loop_hot_counter_handler != NULL);
3273
3274
0
  if (zend_jit_build_cfg(op_array, &cfg) != SUCCESS) {
3275
0
    return FAILURE;
3276
0
  }
3277
3278
0
  jit_extension = (zend_jit_op_array_hot_extension*)zend_shared_alloc(sizeof(zend_jit_op_array_hot_extension) + (op_array->last - 1) * sizeof(void*));
3279
0
  if (!jit_extension) {
3280
0
    return FAILURE;
3281
0
  }
3282
0
  memset(&jit_extension->func_info, 0, sizeof(zend_func_info));
3283
0
  jit_extension->func_info.flags = ZEND_FUNC_JIT_ON_HOT_COUNTERS;
3284
0
  jit_extension->op_array = op_array;
3285
0
  jit_extension->counter = &zend_jit_hot_counters[zend_jit_op_array_hash(op_array) & (ZEND_HOT_COUNTERS_COUNT - 1)];
3286
0
  for (i = 0; i < op_array->last; i++) {
3287
0
    jit_extension->orig_handlers[i] = op_array->opcodes[i].handler;
3288
0
  }
3289
0
  ZEND_SET_FUNC_INFO(op_array, (void*)jit_extension);
3290
3291
0
  zend_jit_setup_hot_counters_ex(op_array, &cfg);
3292
3293
0
  zend_shared_alloc_register_xlat_entry(op_array->opcodes, jit_extension);
3294
3295
0
  return SUCCESS;
3296
0
}
3297
3298
#include "jit/zend_jit_trace.c"
3299
3300
int zend_jit_op_array(zend_op_array *op_array, zend_script *script)
3301
0
{
3302
0
  if (dasm_ptr == NULL) {
3303
0
    return FAILURE;
3304
0
  }
3305
3306
0
  if (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC) {
3307
0
    zend_jit_op_array_extension *jit_extension;
3308
0
    zend_op *opline = op_array->opcodes;
3309
3310
0
    if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
3311
0
      ZEND_SET_FUNC_INFO(op_array, NULL);
3312
0
      zend_error(E_WARNING, "Preloading is incompatible with first-exec and profile triggered JIT");
3313
0
      return SUCCESS;
3314
0
    }
3315
3316
    /* Set run-time JIT handler */
3317
0
    ZEND_ASSERT(zend_jit_runtime_jit_handler != NULL);
3318
0
    if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
3319
0
      while (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT) {
3320
0
        opline++;
3321
0
      }
3322
0
    }
3323
0
    jit_extension = (zend_jit_op_array_extension*)zend_shared_alloc(sizeof(zend_jit_op_array_extension));
3324
0
    if (!jit_extension) {
3325
0
      return FAILURE;
3326
0
    }
3327
0
    memset(&jit_extension->func_info, 0, sizeof(zend_func_info));
3328
0
    jit_extension->func_info.flags = ZEND_FUNC_JIT_ON_FIRST_EXEC;
3329
0
    jit_extension->op_array = op_array;
3330
0
    jit_extension->orig_handler = opline->handler;
3331
0
    ZEND_SET_FUNC_INFO(op_array, (void*)jit_extension);
3332
0
    opline->handler = zend_jit_runtime_jit_handler;
3333
0
    zend_shared_alloc_register_xlat_entry(op_array->opcodes, jit_extension);
3334
3335
0
    return SUCCESS;
3336
0
  } else if (JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST) {
3337
0
    zend_jit_op_array_extension *jit_extension;
3338
0
    zend_op *opline = op_array->opcodes;
3339
3340
0
    if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
3341
0
      ZEND_SET_FUNC_INFO(op_array, NULL);
3342
0
      zend_error(E_WARNING, "Preloading is incompatible with first-exec and profile triggered JIT");
3343
0
      return SUCCESS;
3344
0
    }
3345
3346
0
    ZEND_ASSERT(zend_jit_profile_jit_handler != NULL);
3347
0
    if (op_array->function_name) {
3348
0
      if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
3349
0
        while (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT) {
3350
0
          opline++;
3351
0
        }
3352
0
      }
3353
0
      jit_extension = (zend_jit_op_array_extension*)zend_shared_alloc(sizeof(zend_jit_op_array_extension));
3354
0
      if (!jit_extension) {
3355
0
        return FAILURE;
3356
0
      }
3357
0
      memset(&jit_extension->func_info, 0, sizeof(zend_func_info));
3358
0
      jit_extension->func_info.flags = ZEND_FUNC_JIT_ON_PROF_REQUEST;
3359
0
      jit_extension->op_array = op_array;
3360
0
      jit_extension->orig_handler = opline->handler;
3361
0
      ZEND_SET_FUNC_INFO(op_array, (void*)jit_extension);
3362
0
      opline->handler = zend_jit_profile_jit_handler;
3363
0
      zend_shared_alloc_register_xlat_entry(op_array->opcodes, jit_extension);
3364
0
    }
3365
3366
0
    return SUCCESS;
3367
0
  } else if (JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS) {
3368
0
    return zend_jit_setup_hot_counters(op_array);
3369
0
  } else if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
3370
0
    return zend_jit_setup_hot_trace_counters(op_array);
3371
0
  } else if (JIT_G(trigger) == ZEND_JIT_ON_SCRIPT_LOAD) {
3372
0
    return zend_real_jit_func(op_array, script, NULL, ZEND_JIT_ON_SCRIPT_LOAD);
3373
0
  } else {
3374
0
    ZEND_UNREACHABLE();
3375
0
  }
3376
0
  return FAILURE;
3377
0
}
3378
3379
static void zend_jit_link_func_info(zend_op_array *op_array)
3380
0
{
3381
0
  if (!ZEND_FUNC_INFO(op_array)) {
3382
0
    void *jit_extension = zend_shared_alloc_get_xlat_entry(op_array->opcodes);
3383
3384
0
    if (jit_extension) {
3385
0
      ZEND_SET_FUNC_INFO(op_array, jit_extension);
3386
0
    }
3387
0
  }
3388
0
}
3389
3390
int zend_jit_script(zend_script *script)
3391
0
{
3392
0
  void *checkpoint;
3393
0
  zend_call_graph call_graph;
3394
0
  zend_func_info *info;
3395
0
  int i;
3396
3397
0
  if (dasm_ptr == NULL || *dasm_ptr == dasm_end) {
3398
0
    return FAILURE;
3399
0
  }
3400
3401
0
  checkpoint = zend_arena_checkpoint(CG(arena));
3402
3403
0
  call_graph.op_arrays_count = 0;
3404
0
  zend_build_call_graph(&CG(arena), script, &call_graph);
3405
3406
0
  zend_analyze_call_graph(&CG(arena), script, &call_graph);
3407
3408
0
  if (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC ||
3409
0
      JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST ||
3410
0
      JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS ||
3411
0
      JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
3412
0
    for (i = 0; i < call_graph.op_arrays_count; i++) {
3413
0
      if (zend_jit_op_array(call_graph.op_arrays[i], script) != SUCCESS) {
3414
0
        goto jit_failure;
3415
0
      }
3416
0
    }
3417
0
  } else if (JIT_G(trigger) == ZEND_JIT_ON_SCRIPT_LOAD) {
3418
0
    for (i = 0; i < call_graph.op_arrays_count; i++) {
3419
0
      info = ZEND_FUNC_INFO(call_graph.op_arrays[i]);
3420
0
      if (info) {
3421
0
        if (zend_jit_op_array_analyze1(call_graph.op_arrays[i], script, &info->ssa) != SUCCESS) {
3422
0
          goto jit_failure;
3423
0
        }
3424
0
        info->ssa.cfg.flags |= info->flags;
3425
0
        info->flags = info->ssa.cfg.flags;
3426
0
      }
3427
0
    }
3428
3429
0
    for (i = 0; i < call_graph.op_arrays_count; i++) {
3430
0
      info = ZEND_FUNC_INFO(call_graph.op_arrays[i]);
3431
0
      if (info) {
3432
0
        info->call_map = zend_build_call_map(&CG(arena), info, call_graph.op_arrays[i]);
3433
0
        if (call_graph.op_arrays[i]->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
3434
0
          zend_init_func_return_info(call_graph.op_arrays[i], script, &info->return_info);
3435
0
        }
3436
0
      }
3437
0
    }
3438
3439
0
    for (i = 0; i < call_graph.op_arrays_count; i++) {
3440
0
      info = ZEND_FUNC_INFO(call_graph.op_arrays[i]);
3441
0
      if (info) {
3442
0
        if (zend_jit_op_array_analyze2(call_graph.op_arrays[i], script, &info->ssa, ZCG(accel_directives).optimization_level) != SUCCESS) {
3443
0
          goto jit_failure;
3444
0
        }
3445
0
        info->flags = info->ssa.cfg.flags;
3446
0
      }
3447
0
    }
3448
3449
0
    for (i = 0; i < call_graph.op_arrays_count; i++) {
3450
0
      info = ZEND_FUNC_INFO(call_graph.op_arrays[i]);
3451
0
      if (info) {
3452
0
        if (JIT_G(debug) & ZEND_JIT_DEBUG_SSA) {
3453
0
          zend_dump_op_array(call_graph.op_arrays[i], ZEND_DUMP_HIDE_UNREACHABLE|ZEND_DUMP_RC_INFERENCE|ZEND_DUMP_SSA, "JIT", &info->ssa);
3454
0
        }
3455
0
        if (zend_jit(call_graph.op_arrays[i], &info->ssa, NULL) != SUCCESS) {
3456
0
          goto jit_failure;
3457
0
        }
3458
0
      }
3459
0
    }
3460
3461
0
    for (i = 0; i < call_graph.op_arrays_count; i++) {
3462
0
      ZEND_SET_FUNC_INFO(call_graph.op_arrays[i], NULL);
3463
0
    }
3464
0
  } else {
3465
0
    ZEND_UNREACHABLE();
3466
0
  }
3467
3468
0
  zend_arena_release(&CG(arena), checkpoint);
3469
3470
0
  if (JIT_G(trigger) == ZEND_JIT_ON_FIRST_EXEC
3471
0
   || JIT_G(trigger) == ZEND_JIT_ON_PROF_REQUEST
3472
0
   || JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS
3473
0
   || JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
3474
0
    zend_class_entry *ce;
3475
0
    zend_op_array *op_array;
3476
0
    zval *zv;
3477
0
    zend_property_info *prop;
3478
3479
0
    ZEND_HASH_MAP_FOREACH_VAL(&script->class_table, zv) {
3480
0
      if (Z_TYPE_P(zv) == IS_ALIAS_PTR) {
3481
0
        continue;
3482
0
      }
3483
3484
0
      ce = Z_PTR_P(zv);
3485
0
      ZEND_ASSERT(ce->type == ZEND_USER_CLASS);
3486
3487
0
      ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, op_array) {
3488
0
        zend_jit_link_func_info(op_array);
3489
0
      } ZEND_HASH_FOREACH_END();
3490
3491
0
      if (ce->num_hooked_props > 0) {
3492
0
        ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop) {
3493
0
          if (prop->hooks) {
3494
0
            for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
3495
0
              if (prop->hooks[i]) {
3496
0
                op_array = &prop->hooks[i]->op_array;
3497
0
                zend_jit_link_func_info(op_array);
3498
0
              }
3499
0
            }
3500
0
          }
3501
0
        } ZEND_HASH_FOREACH_END();
3502
0
      }
3503
0
    } ZEND_HASH_FOREACH_END();
3504
0
  }
3505
3506
0
  return SUCCESS;
3507
3508
0
jit_failure:
3509
0
  if (JIT_G(trigger) == ZEND_JIT_ON_SCRIPT_LOAD) {
3510
0
    for (i = 0; i < call_graph.op_arrays_count; i++) {
3511
0
      ZEND_SET_FUNC_INFO(call_graph.op_arrays[i], NULL);
3512
0
    }
3513
0
  }
3514
0
  zend_arena_release(&CG(arena), checkpoint);
3515
0
  return FAILURE;
3516
0
}
3517
3518
void zend_jit_unprotect(void)
3519
0
{
3520
0
#ifdef HAVE_MPROTECT
3521
0
  if (!(JIT_G(debug) & (ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF_DUMP))) {
3522
0
    int opts = PROT_READ | PROT_WRITE;
3523
#ifdef ZTS
3524
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
3525
    if (zend_write_protect) {
3526
      pthread_jit_write_protect_np(0);
3527
    }
3528
#endif
3529
    opts |= PROT_EXEC;
3530
#endif
3531
0
    if (mprotect(dasm_buf, dasm_size, opts) != 0) {
3532
0
      fprintf(stderr, "mprotect() failed [%d] %s\n", errno, strerror(errno));
3533
0
    }
3534
0
  }
3535
#elif defined(_WIN32)
3536
  if (!(JIT_G(debug) & (ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF_DUMP))) {
3537
    DWORD old, new;
3538
#ifdef ZTS
3539
    new = PAGE_EXECUTE_READWRITE;
3540
#else
3541
    new = PAGE_READWRITE;
3542
#endif
3543
    if (!VirtualProtect(dasm_buf, dasm_size, new, &old)) {
3544
      DWORD err = GetLastError();
3545
      char *msg = php_win32_error_to_msg(err);
3546
      fprintf(stderr, "VirtualProtect() failed [%lu] %s\n", err, msg);
3547
      php_win32_error_msg_free(msg);
3548
    }
3549
  }
3550
#endif
3551
0
}
3552
3553
void zend_jit_protect(void)
3554
0
{
3555
0
#ifdef HAVE_MPROTECT
3556
0
  if (!(JIT_G(debug) & (ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF_DUMP))) {
3557
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
3558
    if (zend_write_protect) {
3559
      pthread_jit_write_protect_np(1);
3560
    }
3561
#endif
3562
0
    if (mprotect(dasm_buf, dasm_size, PROT_READ | PROT_EXEC) != 0) {
3563
0
      fprintf(stderr, "mprotect() failed [%d] %s\n", errno, strerror(errno));
3564
0
    }
3565
0
  }
3566
#elif defined(_WIN32)
3567
  if (!(JIT_G(debug) & (ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF_DUMP))) {
3568
    DWORD old;
3569
3570
    if (!VirtualProtect(dasm_buf, dasm_size, PAGE_EXECUTE_READ, &old)) {
3571
      DWORD err = GetLastError();
3572
      char *msg = php_win32_error_to_msg(err);
3573
      fprintf(stderr, "VirtualProtect() failed [%lu] %s\n", err, msg);
3574
      php_win32_error_msg_free(msg);
3575
    }
3576
  }
3577
#endif
3578
0
}
3579
3580
static void zend_jit_init_handlers(void)
3581
0
{
3582
#if ZEND_VM_KIND == ZEND_VM_KIND_HYBRID
3583
    zend_jit_runtime_jit_handler = (zend_vm_opcode_handler_t)zend_jit_stub_handlers[jit_stub_hybrid_runtime_jit];
3584
    zend_jit_profile_jit_handler = (zend_vm_opcode_handler_t)zend_jit_stub_handlers[jit_stub_hybrid_profile_jit];
3585
    zend_jit_func_hot_counter_handler = (zend_vm_opcode_handler_t)zend_jit_stub_handlers[jit_stub_hybrid_func_hot_counter];
3586
    zend_jit_loop_hot_counter_handler = (zend_vm_opcode_handler_t)zend_jit_stub_handlers[jit_stub_hybrid_loop_hot_counter];
3587
    zend_jit_func_trace_counter_handler = (zend_vm_opcode_handler_t)zend_jit_stub_handlers[jit_stub_hybrid_func_trace_counter];
3588
    zend_jit_ret_trace_counter_handler = (zend_vm_opcode_handler_t)zend_jit_stub_handlers[jit_stub_hybrid_ret_trace_counter];
3589
    zend_jit_loop_trace_counter_handler = (zend_vm_opcode_handler_t)zend_jit_stub_handlers[jit_stub_hybrid_loop_trace_counter];
3590
#else
3591
0
    zend_jit_runtime_jit_handler = zend_runtime_jit;
3592
0
    zend_jit_profile_jit_handler = zend_jit_profile_helper;
3593
0
    zend_jit_func_hot_counter_handler = zend_jit_func_counter_helper;
3594
0
    zend_jit_loop_hot_counter_handler = zend_jit_loop_counter_helper;
3595
0
    zend_jit_func_trace_counter_handler = zend_jit_func_trace_helper;
3596
0
    zend_jit_ret_trace_counter_handler = zend_jit_ret_trace_helper;
3597
0
    zend_jit_loop_trace_counter_handler = zend_jit_loop_trace_helper;
3598
0
#endif
3599
0
}
3600
3601
static void zend_jit_globals_ctor(zend_jit_globals *jit_globals)
3602
16
{
3603
16
  memset(jit_globals, 0, sizeof(zend_jit_globals));
3604
16
  zend_jit_trace_init_caches();
3605
16
}
3606
3607
#ifdef ZTS
3608
static void zend_jit_globals_dtor(zend_jit_globals *jit_globals)
3609
{
3610
  zend_jit_trace_free_caches(jit_globals);
3611
}
3612
#endif
3613
3614
static int zend_jit_parse_config_num(zend_long jit)
3615
0
{
3616
0
  if (jit == 0) {
3617
0
    JIT_G(on) = 0;
3618
0
    return SUCCESS;
3619
0
  }
3620
3621
0
  if (jit < 0) return FAILURE;
3622
3623
0
  if (jit % 10 == 0 || jit % 10 > 5) return FAILURE;
3624
0
  JIT_G(opt_level) = jit % 10;
3625
3626
0
  jit /= 10;
3627
0
  if (jit % 10 > 5 || jit % 10 == 4) return FAILURE;
3628
0
  JIT_G(trigger) = jit % 10;
3629
3630
0
  jit /= 10;
3631
0
  if (jit % 10 > 2) return FAILURE;
3632
0
  JIT_G(opt_flags) = jit % 10;
3633
3634
0
  jit /= 10;
3635
0
  if (jit % 10 > 1) return FAILURE;
3636
0
  JIT_G(opt_flags) |= ((jit % 10) ? ZEND_JIT_CPU_AVX : 0);
3637
3638
0
  if (jit / 10 != 0) return FAILURE;
3639
3640
0
  JIT_G(on) = 1;
3641
3642
0
  return SUCCESS;
3643
0
}
3644
3645
int zend_jit_config(zend_string *jit, int stage)
3646
141k
{
3647
141k
  if (stage != ZEND_INI_STAGE_STARTUP && !JIT_G(enabled)) {
3648
141k
    if (stage == ZEND_INI_STAGE_RUNTIME) {
3649
70.8k
      zend_error(E_WARNING, "Cannot change opcache.jit setting at run-time (JIT is disabled)");
3650
70.8k
    }
3651
141k
    return FAILURE;
3652
141k
  }
3653
3654
16
  if (zend_string_equals_literal_ci(jit, "disable")) {
3655
16
    JIT_G(enabled) = 0;
3656
16
    JIT_G(on) = 0;
3657
16
    return SUCCESS;
3658
16
  } else if (ZSTR_LEN(jit) == 0
3659
0
      || zend_string_equals_literal_ci(jit, "0")
3660
0
      || zend_string_equals_literal_ci(jit, "off")
3661
0
      || zend_string_equals_literal_ci(jit, "no")
3662
0
      || zend_string_equals_literal_ci(jit, "false")) {
3663
0
    JIT_G(enabled) = 1;
3664
0
    JIT_G(on) = 0;
3665
0
    return SUCCESS;
3666
0
  } else if (zend_string_equals_literal_ci(jit, "1")
3667
0
      || zend_string_equals_literal_ci(jit, "on")
3668
0
      || zend_string_equals_literal_ci(jit, "yes")
3669
0
      || zend_string_equals_literal_ci(jit, "true")
3670
0
      || zend_string_equals_literal_ci(jit, "tracing")) {
3671
0
    JIT_G(enabled) = 1;
3672
0
    JIT_G(on) = 1;
3673
0
    JIT_G(opt_level) = ZEND_JIT_LEVEL_OPT_FUNCS;
3674
0
    JIT_G(trigger) = ZEND_JIT_ON_HOT_TRACE;
3675
0
    JIT_G(opt_flags) = ZEND_JIT_REG_ALLOC_GLOBAL | ZEND_JIT_CPU_AVX;
3676
0
    return SUCCESS;
3677
0
  } else if (zend_string_equals_ci(jit, ZSTR_KNOWN(ZEND_STR_FUNCTION))) {
3678
0
    JIT_G(enabled) = 1;
3679
0
    JIT_G(on) = 1;
3680
0
    JIT_G(opt_level) = ZEND_JIT_LEVEL_OPT_SCRIPT;
3681
0
    JIT_G(trigger) = ZEND_JIT_ON_SCRIPT_LOAD;
3682
0
    JIT_G(opt_flags) = ZEND_JIT_REG_ALLOC_GLOBAL | ZEND_JIT_CPU_AVX;
3683
0
    return SUCCESS;
3684
0
  } else  {
3685
0
    char *end;
3686
0
    zend_long num = ZEND_STRTOL(ZSTR_VAL(jit), &end, 10);
3687
0
    if (end != ZSTR_VAL(jit) + ZSTR_LEN(jit) || zend_jit_parse_config_num(num) != SUCCESS) {
3688
0
      goto failure;
3689
0
    }
3690
0
    JIT_G(enabled) = 1;
3691
0
    return SUCCESS;
3692
0
  }
3693
3694
0
failure:
3695
0
  zend_error(E_WARNING, "Invalid \"opcache.jit\" setting. Should be \"disable\", \"on\", \"off\", \"tracing\", \"function\" or 4-digit number");
3696
0
  JIT_G(enabled) = 0;
3697
0
  JIT_G(on) = 0;
3698
0
  return FAILURE;
3699
16
}
3700
3701
int zend_jit_debug_config(zend_long old_val, zend_long new_val, int stage)
3702
16
{
3703
16
  if (stage != ZEND_INI_STAGE_STARTUP) {
3704
0
    if (((old_val ^ new_val) & ZEND_JIT_DEBUG_PERSISTENT) != 0) {
3705
0
      if (stage == ZEND_INI_STAGE_RUNTIME) {
3706
0
        zend_error(E_WARNING, "Some opcache.jit_debug bits cannot be changed after startup");
3707
0
      }
3708
0
      return FAILURE;
3709
0
    }
3710
0
  }
3711
16
  return SUCCESS;
3712
16
}
3713
3714
void zend_jit_init(void)
3715
16
{
3716
#ifdef ZTS
3717
  jit_globals_id = ts_allocate_fast_id(&jit_globals_id, &jit_globals_offset, sizeof(zend_jit_globals), (ts_allocate_ctor) zend_jit_globals_ctor, (ts_allocate_dtor) zend_jit_globals_dtor);
3718
#else
3719
16
  zend_jit_globals_ctor(&jit_globals);
3720
16
#endif
3721
16
}
3722
3723
#if ZEND_VM_KIND != ZEND_VM_KIND_CALL && ZEND_VM_KIND != ZEND_VM_KIND_TAILCALL && ZEND_VM_KIND != ZEND_VM_KIND_HYBRID
3724
# error JIT is compatible only with CALL and HYBRID VM
3725
#endif
3726
3727
int zend_jit_check_support(void)
3728
0
{
3729
0
  int i;
3730
3731
0
  if (zend_execute_ex != execute_ex) {
3732
0
    if (zend_dtrace_enabled) {
3733
0
      zend_error(E_WARNING, "JIT is incompatible with DTrace. JIT disabled.");
3734
0
    } else if (strcmp(sapi_module.name, "phpdbg") != 0) {
3735
0
      zend_error(E_WARNING, "JIT is incompatible with third party extensions that override zend_execute_ex(). JIT disabled.");
3736
0
    }
3737
0
    JIT_G(enabled) = 0;
3738
0
    JIT_G(on) = 0;
3739
0
    return FAILURE;
3740
0
  }
3741
3742
0
  for (i = 0; i <= 256; i++) {
3743
0
    switch (i) {
3744
      /* JIT has no effect on these opcodes */
3745
0
      case ZEND_BEGIN_SILENCE:
3746
0
      case ZEND_END_SILENCE:
3747
0
        break;
3748
0
      default:
3749
0
        if (zend_get_user_opcode_handler(i) != NULL) {
3750
0
          zend_error(E_WARNING, "JIT is incompatible with third party extensions that setup user opcode handlers. JIT disabled.");
3751
0
          JIT_G(enabled) = 0;
3752
0
          JIT_G(on) = 0;
3753
0
          return FAILURE;
3754
0
        }
3755
0
    }
3756
0
  }
3757
3758
#if defined(IR_TARGET_AARCH64)
3759
  if (JIT_G(buffer_size) > 128*1024*1024) {
3760
    zend_error(E_WARNING, "JIT on AArch64 doesn't support opcache.jit_buffer_size above 128M.");
3761
    JIT_G(enabled) = 0;
3762
    JIT_G(on) = 0;
3763
    return FAILURE;
3764
  }
3765
#elif defined(IR_TARGET_X64)
3766
0
  if (JIT_G(buffer_size) > 2 * Z_L(1024*1024*1024)) {
3767
0
    zend_error(E_WARNING, "JIT on x86_64 doesn't support opcache.jit_buffer_size above 2G.");
3768
0
    JIT_G(enabled) = 0;
3769
0
    JIT_G(on) = 0;
3770
0
    return FAILURE;
3771
0
  }
3772
0
#endif
3773
3774
0
  return SUCCESS;
3775
0
}
3776
3777
void zend_jit_startup(void *buf, size_t size, bool reattached)
3778
0
{
3779
0
  zend_jit_halt_op = zend_get_halt_op();
3780
0
  zend_jit_profile_counter_rid = zend_get_op_array_extension_handle(ACCELERATOR_PRODUCT_NAME);
3781
3782
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
3783
  zend_write_protect = pthread_jit_write_protect_supported_np();
3784
#endif
3785
3786
0
  dasm_buf = buf;
3787
0
  dasm_size = size;
3788
0
  dasm_ptr = dasm_end = (void*)(((char*)dasm_buf) + size - sizeof(*dasm_ptr) * 2);
3789
3790
0
#ifdef HAVE_MPROTECT
3791
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
3792
  if (zend_write_protect) {
3793
    pthread_jit_write_protect_np(1);
3794
  }
3795
#endif
3796
0
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF_DUMP)) {
3797
0
    if (mprotect(dasm_buf, dasm_size, PROT_READ | PROT_WRITE | PROT_EXEC) != 0) {
3798
0
      fprintf(stderr, "mprotect() failed [%d] %s\n", errno, strerror(errno));
3799
0
    }
3800
0
  } else {
3801
0
    if (mprotect(dasm_buf, dasm_size, PROT_READ | PROT_EXEC) != 0) {
3802
0
      fprintf(stderr, "mprotect() failed [%d] %s\n", errno, strerror(errno));
3803
0
    }
3804
0
  }
3805
#elif defined(_WIN32)
3806
  if (JIT_G(debug) & (ZEND_JIT_DEBUG_GDB|ZEND_JIT_DEBUG_PERF_DUMP)) {
3807
    DWORD old;
3808
3809
    if (!VirtualProtect(dasm_buf, dasm_size, PAGE_EXECUTE_READWRITE, &old)) {
3810
      DWORD err = GetLastError();
3811
      char *msg = php_win32_error_to_msg(err);
3812
      fprintf(stderr, "VirtualProtect() failed [%lu] %s\n", err, msg);
3813
      php_win32_error_msg_free(msg);
3814
    }
3815
  } else {
3816
    DWORD old;
3817
3818
    if (!VirtualProtect(dasm_buf, dasm_size, PAGE_EXECUTE_READ, &old)) {
3819
      DWORD err = GetLastError();
3820
      char *msg = php_win32_error_to_msg(err);
3821
      fprintf(stderr, "VirtualProtect() failed [%lu] %s\n", err, msg);
3822
      php_win32_error_msg_free(msg);
3823
    }
3824
  }
3825
#endif
3826
3827
0
  if (!reattached) {
3828
0
    zend_jit_unprotect();
3829
0
    *dasm_ptr = dasm_buf;
3830
#if defined(_WIN32)
3831
    zend_jit_stub_handlers = dasm_buf;
3832
    *dasm_ptr = (void**)*dasm_ptr + sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0]);
3833
#elif defined(IR_TARGET_AARCH64)
3834
    zend_jit_stub_handlers = dasm_buf;
3835
    *dasm_ptr = (void**)*dasm_ptr + (sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0])) * 2;
3836
    memset(zend_jit_stub_handlers, 0, (sizeof(zend_jit_stubs) / sizeof(zend_jit_stubs[0])) * 2 * sizeof(void*));
3837
#endif
3838
0
    *dasm_ptr = (void*)ZEND_MM_ALIGNED_SIZE_EX(((size_t)(*dasm_ptr)), 16);
3839
0
    zend_jit_protect();
3840
0
  } else {
3841
#if defined(_WIN32) || defined(IR_TARGET_AARCH64)
3842
    zend_jit_stub_handlers = dasm_buf;
3843
    zend_jit_init_handlers();
3844
#endif
3845
0
  }
3846
3847
0
  zend_jit_unprotect();
3848
0
  zend_jit_setup(reattached);
3849
0
  zend_jit_protect();
3850
0
  if (!reattached) {
3851
0
    zend_jit_init_handlers();
3852
0
  }
3853
3854
0
  zend_jit_trace_startup(reattached);
3855
3856
0
  zend_jit_unprotect();
3857
  /* save JIT buffer pos */
3858
0
  dasm_ptr[1] = dasm_ptr[0];
3859
0
  zend_jit_protect();
3860
0
}
3861
3862
void zend_jit_shutdown(void)
3863
0
{
3864
0
  if (JIT_G(debug) & ZEND_JIT_DEBUG_SIZE && dasm_ptr != NULL) {
3865
0
    fprintf(stderr, "\nJIT memory usage: %td\n", (ptrdiff_t)((char*)*dasm_ptr - (char*)dasm_buf));
3866
0
  }
3867
3868
0
  zend_jit_shutdown_ir();
3869
3870
#ifdef ZTS
3871
  ts_free_id(jit_globals_id);
3872
#else
3873
0
  zend_jit_trace_free_caches(&jit_globals);
3874
0
#endif
3875
3876
  /* Reset global pointers to prevent use-after-free in `zend_jit_status()`
3877
   * after gracefully restarting Apache with mod_php, see:
3878
   * https://github.com/php/php-src/pull/19212 */
3879
0
  dasm_ptr = NULL;
3880
0
  dasm_buf = NULL;
3881
0
  dasm_end = NULL;
3882
0
  dasm_size = 0;
3883
0
}
3884
3885
static void zend_jit_reset_counters(void)
3886
0
{
3887
0
  int i;
3888
3889
0
  for (i = 0; i < ZEND_HOT_COUNTERS_COUNT; i++) {
3890
0
    zend_jit_hot_counters[i] = ZEND_JIT_COUNTER_INIT;
3891
0
  }
3892
0
}
3893
3894
void zend_jit_activate(void)
3895
247k
{
3896
#ifdef ZTS
3897
  if (!zend_jit_startup_ok) {
3898
    JIT_G(enabled) = 0;
3899
    JIT_G(on) = 0;
3900
    return;
3901
  }
3902
#endif
3903
247k
  zend_jit_profile_counter = 0;
3904
247k
  if (JIT_G(on)) {
3905
0
    if (JIT_G(trigger) == ZEND_JIT_ON_HOT_COUNTERS) {
3906
0
      zend_jit_reset_counters();
3907
0
    } else if (JIT_G(trigger) == ZEND_JIT_ON_HOT_TRACE) {
3908
0
      zend_jit_reset_counters();
3909
0
      zend_jit_trace_reset_caches();
3910
0
    }
3911
0
  }
3912
247k
}
3913
3914
void zend_jit_deactivate(void)
3915
247k
{
3916
247k
  if (zend_jit_profile_counter && !CG(unclean_shutdown)) {
3917
0
    zend_class_entry *ce;
3918
3919
0
    zend_shared_alloc_lock();
3920
0
    SHM_UNPROTECT();
3921
0
    zend_jit_unprotect();
3922
3923
0
    zend_jit_check_funcs(EG(function_table), false);
3924
0
    ZEND_HASH_MAP_REVERSE_FOREACH_PTR(EG(class_table), ce) {
3925
0
      if (ce->type == ZEND_INTERNAL_CLASS) {
3926
0
        break;
3927
0
      }
3928
0
      zend_jit_check_funcs(&ce->function_table, true);
3929
0
    } ZEND_HASH_FOREACH_END();
3930
3931
0
    zend_jit_protect();
3932
0
    SHM_PROTECT();
3933
0
    zend_shared_alloc_unlock();
3934
0
  }
3935
3936
247k
  zend_jit_profile_counter = 0;
3937
247k
}
3938
3939
static void zend_jit_restart_preloaded_op_array(zend_op_array *op_array, void *context)
3940
0
{
3941
0
  ZEND_IGNORE_VALUE(context);
3942
3943
0
  zend_func_info *func_info = ZEND_FUNC_INFO(op_array);
3944
3945
0
  if (!func_info) {
3946
0
    return;
3947
0
  }
3948
3949
0
  if (func_info->flags & ZEND_FUNC_JIT_ON_HOT_TRACE) {
3950
0
    zend_jit_restart_hot_trace_counters(op_array);
3951
0
  } else if (func_info->flags & ZEND_FUNC_JIT_ON_HOT_COUNTERS) {
3952
0
    zend_jit_restart_hot_counters(op_array);
3953
#if 0
3954
  // TODO: We have to restore handlers for some inner basic-blocks, but we didn't store them ???
3955
  } else if (func_info->flags & (ZEND_FUNC_JIT_ON_FIRST_EXEC|ZEND_FUNC_JIT_ON_PROF_REQUEST)) {
3956
    zend_op *opline = op_array->opcodes;
3957
    zend_jit_op_array_extension *jit_extension =
3958
      (zend_jit_op_array_extension*)func_info;
3959
3960
    if (!(op_array->fn_flags & ZEND_ACC_HAS_TYPE_HINTS)) {
3961
      while (opline->opcode == ZEND_RECV || opline->opcode == ZEND_RECV_INIT) {
3962
        opline++;
3963
      }
3964
    }
3965
    if (func_info->flags & ZEND_FUNC_JIT_ON_FIRST_EXEC) {
3966
      opline->handler = zend_jit_runtime_jit_handler;
3967
    } else {
3968
      opline->handler = zend_jit_profile_jit_handler;
3969
    }
3970
#endif
3971
0
  }
3972
0
}
3973
3974
static void zend_jit_restart_preloaded_script(zend_persistent_script *script)
3975
0
{
3976
0
  zend_foreach_op_array(&script->script, zend_jit_restart_preloaded_op_array, NULL);
3977
0
}
3978
3979
void zend_jit_restart(void)
3980
0
{
3981
0
  if (dasm_buf) {
3982
0
    zend_jit_unprotect();
3983
3984
    /* restore JIT buffer pos */
3985
0
    dasm_ptr[0] = dasm_ptr[1];
3986
3987
0
    zend_jit_trace_restart();
3988
3989
0
    if (ZCSG(preload_script)) {
3990
0
      zend_jit_restart_preloaded_script(ZCSG(preload_script));
3991
0
      if (ZCSG(saved_scripts)) {
3992
0
        zend_persistent_script **p = ZCSG(saved_scripts);
3993
3994
0
        while (*p) {
3995
0
          zend_jit_restart_preloaded_script(*p);
3996
0
          p++;
3997
0
        }
3998
0
      }
3999
0
    }
4000
4001
0
    zend_jit_protect();
4002
0
  }
4003
0
}
4004
4005
#endif /* HAVE_JIT */