Coverage Report

Created: 2026-07-25 06:39

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