Coverage Report

Created: 2026-02-14 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/Optimizer/zend_func_info.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Zend Engine, Func Info                                               |
4
   +----------------------------------------------------------------------+
5
   | Copyright (c) The PHP Group                                          |
6
   +----------------------------------------------------------------------+
7
   | This source file is subject to version 3.01 of the PHP license,      |
8
   | that is bundled with this package in the file LICENSE, and is        |
9
   | available through the world-wide-web at the following url:           |
10
   | https://www.php.net/license/3_01.txt                                 |
11
   | If you did not receive a copy of the PHP license and are unable to   |
12
   | obtain it through the world-wide-web, please send a note to          |
13
   | license@php.net so we can mail you a copy immediately.               |
14
   +----------------------------------------------------------------------+
15
   | Authors: Dmitry Stogov <dmitry@php.net>                              |
16
   |          Xinchen Hui <laruence@php.net>                              |
17
   +----------------------------------------------------------------------+
18
*/
19
20
#include "zend_compile.h"
21
#include "zend_extensions.h"
22
#include "zend_ssa.h"
23
#include "zend_optimizer_internal.h"
24
#include "zend_inference.h"
25
#include "zend_call_graph.h"
26
#include "zend_func_info.h"
27
#ifdef _WIN32
28
#include "win32/ioutil.h"
29
#endif
30
31
typedef uint32_t (*info_func_t)(const zend_call_info *call_info, const zend_ssa *ssa);
32
33
typedef struct _func_info_t {
34
  const char *name;
35
  unsigned    name_len;
36
  uint32_t    info;
37
  info_func_t info_func;
38
} func_info_t;
39
40
#define F0(name, info) \
41
  {name, sizeof(name)-1, (info), NULL}
42
#define F1(name, info) \
43
  {name, sizeof(name)-1, (MAY_BE_RC1 | (info)), NULL}
44
#define FN(name, info) \
45
  {name, sizeof(name)-1, (MAY_BE_RC1 | MAY_BE_RCN | (info)), NULL}
46
#define FC(name, callback) \
47
  {name, sizeof(name)-1, 0, callback}
48
49
#include "zend_func_infos.h"
50
51
static uint32_t zend_range_info(const zend_call_info *call_info, const zend_ssa *ssa)
52
191
{
53
191
  ZEND_ASSERT(!call_info->is_frameless);
54
55
191
  if (!call_info->send_unpack
56
191
   && (call_info->num_args == 2 || call_info->num_args == 3)
57
191
   && ssa
58
191
   && !(ssa->cfg.flags & ZEND_SSA_TSSA)) {
59
191
    const zend_op_array *op_array = call_info->caller_op_array;
60
191
    uint32_t t1 = _ssa_op1_info(op_array, ssa, call_info->arg_info[0].opline,
61
191
      ssa->ops ? &ssa->ops[call_info->arg_info[0].opline - op_array->opcodes] : NULL);
62
191
    uint32_t t2 = _ssa_op1_info(op_array, ssa, call_info->arg_info[1].opline,
63
191
      ssa->ops ? &ssa->ops[call_info->arg_info[1].opline - op_array->opcodes] : NULL);
64
191
    uint32_t t3 = 0;
65
191
    uint32_t tmp = MAY_BE_RC1 | MAY_BE_ARRAY;
66
67
191
    if (call_info->num_args == 3) {
68
10
      t3 = _ssa_op1_info(op_array, ssa, call_info->arg_info[2].opline,
69
10
        ssa->ops ? &ssa->ops[call_info->arg_info[2].opline - op_array->opcodes] : NULL);
70
10
    }
71
191
    if ((t1 & MAY_BE_STRING) && (t2 & MAY_BE_STRING)) {
72
10
      tmp |= MAY_BE_ARRAY_OF_LONG | MAY_BE_ARRAY_OF_DOUBLE | MAY_BE_ARRAY_OF_STRING;
73
10
    }
74
191
    if ((t1 & (MAY_BE_DOUBLE|MAY_BE_STRING))
75
181
        || (t2 & (MAY_BE_DOUBLE|MAY_BE_STRING))
76
181
        || (t3 & (MAY_BE_DOUBLE|MAY_BE_STRING))) {
77
10
      tmp |= MAY_BE_ARRAY_OF_DOUBLE;
78
10
    }
79
191
    if ((t1 & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))
80
191
        && (t2 & ((MAY_BE_ANY|MAY_BE_UNDEF)-MAY_BE_DOUBLE))) {
81
191
      tmp |= MAY_BE_ARRAY_OF_LONG;
82
191
    }
83
191
    if (tmp & MAY_BE_ARRAY_OF_ANY) {
84
191
      tmp |= MAY_BE_ARRAY_PACKED;
85
191
    }
86
191
    return tmp;
87
191
  } else {
88
    /* May throw */
89
0
    return MAY_BE_RC1 | MAY_BE_ARRAY | MAY_BE_ARRAY_EMPTY | MAY_BE_ARRAY_PACKED | MAY_BE_ARRAY_OF_LONG | MAY_BE_ARRAY_OF_DOUBLE | MAY_BE_ARRAY_OF_STRING;
90
0
  }
91
191
}
92
93
static const func_info_t old_func_infos[] = {
94
  FC("range",                        zend_range_info),
95
};
96
97
static HashTable func_info;
98
ZEND_API int zend_func_info_rid = -1;
99
100
uint32_t zend_get_internal_func_info(
101
12.3k
    const zend_function *callee_func, const zend_call_info *call_info, const zend_ssa *ssa) {
102
12.3k
  if (callee_func->common.scope) {
103
    /* This is a method, not a function. */
104
650
    return 0;
105
650
  }
106
107
11.7k
  zend_string *name = callee_func->common.function_name;
108
11.7k
  if (!name) {
109
    /* zend_pass_function has no name. */
110
0
    return 0;
111
0
  }
112
113
11.7k
  zval *zv = zend_hash_find_known_hash(&func_info, name);
114
11.7k
  if (!zv) {
115
6.13k
    return 0;
116
6.13k
  }
117
118
5.58k
  const func_info_t *info = Z_PTR_P(zv);
119
5.58k
  if (info->info_func) {
120
191
    return call_info ? info->info_func(call_info, ssa) : 0;
121
5.39k
  } else {
122
5.39k
    uint32_t ret = info->info;
123
124
5.39k
    if (ret & MAY_BE_ARRAY) {
125
2.42k
      ret |= MAY_BE_ARRAY_EMPTY;
126
2.42k
    }
127
5.39k
    return ret;
128
5.39k
  }
129
5.58k
}
130
131
ZEND_API uint32_t zend_get_func_info(
132
    const zend_call_info *call_info, const zend_ssa *ssa,
133
    zend_class_entry **ce, bool *ce_is_instanceof)
134
17.9k
{
135
17.9k
  uint32_t ret = 0;
136
17.9k
  const zend_function *callee_func = call_info->callee_func;
137
17.9k
  *ce = NULL;
138
17.9k
  *ce_is_instanceof = false;
139
140
17.9k
  if (callee_func->type == ZEND_INTERNAL_FUNCTION) {
141
12.3k
    uint32_t internal_ret = zend_get_internal_func_info(callee_func, call_info, ssa);
142
#if !ZEND_DEBUG
143
    if (internal_ret) {
144
      return internal_ret;
145
    }
146
#endif
147
148
12.3k
    ret = zend_get_return_info_from_signature_only(
149
12.3k
      callee_func, /* script */ NULL, ce, ce_is_instanceof, /* use_tentative_return_info */ !call_info->is_prototype);
150
151
12.3k
#if ZEND_DEBUG
152
12.3k
    if (internal_ret) {
153
5.58k
      zend_string *name = callee_func->common.function_name;
154
      /* Check whether the func_info information is a subset of the information we can
155
       * compute from the specified return type, otherwise it contains redundant types. */
156
5.58k
      if (internal_ret & ~ret) {
157
0
        fprintf(stderr, "Inaccurate func info for %s()\n", ZSTR_VAL(name));
158
0
      }
159
      /* Check whether the func info is completely redundant with arginfo. */
160
5.58k
      if (internal_ret == ret) {
161
0
        fprintf(stderr, "Useless func info for %s()\n", ZSTR_VAL(name));
162
0
      }
163
      /* If the return type is not mixed, check that the types match exactly if we exclude
164
       * RC and array information. */
165
5.58k
      uint32_t ret_any = ret & MAY_BE_ANY, internal_ret_any = internal_ret & MAY_BE_ANY;
166
5.58k
      if (ret_any != MAY_BE_ANY) {
167
5.56k
        uint32_t diff = internal_ret_any ^ ret_any;
168
        /* Func info may contain "true" types as well as isolated "null" and "false". */
169
5.56k
        if (diff && !(diff == MAY_BE_FALSE && (ret & MAY_BE_FALSE))
170
0
            && (internal_ret_any & ~(MAY_BE_NULL|MAY_BE_FALSE))) {
171
0
          fprintf(stderr, "Incorrect func info for %s()\n", ZSTR_VAL(name));
172
0
        }
173
5.56k
      }
174
5.58k
      return internal_ret;
175
5.58k
    }
176
12.3k
#endif
177
12.3k
  } else {
178
5.59k
    if (!call_info->is_prototype) {
179
      // FIXME: the order of functions matters!!!
180
5.53k
      const zend_func_info *info = ZEND_FUNC_INFO((zend_op_array*)callee_func);
181
5.53k
      if (info) {
182
5.51k
        ret = info->return_info.type;
183
5.51k
        *ce = info->return_info.ce;
184
5.51k
        *ce_is_instanceof = info->return_info.is_instanceof;
185
5.51k
      }
186
5.53k
    }
187
5.59k
    if (!ret) {
188
4.66k
      ret = zend_get_return_info_from_signature_only(
189
4.66k
        callee_func, /* TODO: script */ NULL, ce, ce_is_instanceof, /* use_tentative_return_info */ !call_info->is_prototype);
190
      /* It's allowed to override a method that return non-reference with a method that returns a reference */
191
4.66k
      if (call_info->is_prototype && (ret & ~MAY_BE_REF)) {
192
56
        ret |= MAY_BE_REF;
193
56
        *ce = NULL;
194
56
      }
195
4.66k
    }
196
5.59k
  }
197
12.3k
  return ret;
198
17.9k
}
199
200
static void zend_func_info_add(const func_info_t *new_func_infos, size_t n)
201
32
{
202
8.54k
  for (size_t i = 0; i < n; i++) {
203
8.51k
    zend_string *key = zend_string_init_interned(new_func_infos[i].name, new_func_infos[i].name_len, 1);
204
205
8.51k
    if (zend_hash_add_ptr(&func_info, key, (void**)&new_func_infos[i]) == NULL) {
206
0
      fprintf(stderr, "ERROR: Duplicate function info for \"%s\"\n", new_func_infos[i].name);
207
0
    }
208
209
8.51k
    zend_string_release_ex(key, 1);
210
8.51k
  }
211
32
}
212
213
zend_result zend_func_info_startup(void)
214
16
{
215
16
  if (zend_func_info_rid == -1) {
216
16
    zend_func_info_rid = zend_get_resource_handle("Zend Optimizer");
217
16
    if (zend_func_info_rid < 0) {
218
0
      return FAILURE;
219
0
    }
220
221
16
    zend_hash_init(&func_info, sizeof(old_func_infos)/sizeof(func_info_t) + sizeof(func_infos)/sizeof(func_info_t), NULL, NULL, 1);
222
223
16
    zend_func_info_add(old_func_infos, sizeof(old_func_infos)/sizeof(func_info_t));
224
16
    zend_func_info_add(func_infos, sizeof(func_infos)/sizeof(func_info_t));
225
16
  }
226
227
16
  return SUCCESS;
228
16
}
229
230
zend_result zend_func_info_shutdown(void)
231
0
{
232
0
  if (zend_func_info_rid != -1) {
233
0
    zend_hash_destroy(&func_info);
234
0
    zend_func_info_rid = -1;
235
0
  }
236
0
  return SUCCESS;
237
0
}