Coverage Report

Created: 2026-10-28 06:20

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