Coverage Report

Created: 2026-09-14 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/sapi/fuzzer/fuzzer-execute-common.h
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Authors: Nikita Popov <nikic@php.net>                                |
12
   +----------------------------------------------------------------------+
13
 */
14
15
#include <main/php.h>
16
17
#if defined(__FreeBSD__)
18
# include <sys/sysctl.h>
19
#endif
20
21
#include "fuzzer.h"
22
#include "fuzzer-sapi.h"
23
#include "zend_exceptions.h"
24
#include "zend_vm.h"
25
26
263k
#define FILE_NAME "/tmp/fuzzer.php"
27
309k
#define MAX_STEPS 1000
28
882k
#define MAX_SIZE (8 * 1024)
29
10.2M
#define ZEND_VM_ENTER_BIT 1ULL
30
31
static uint32_t steps_left;
32
static bool bailed_out = false;
33
34
13.1k
static zend_always_inline void fuzzer_bailout(void) {
35
13.1k
  bailed_out = true;
36
13.1k
  zend_bailout();
37
13.1k
}
fuzzer-tracing-jit.c:fuzzer_bailout
Line
Count
Source
34
5.49k
static zend_always_inline void fuzzer_bailout(void) {
35
5.49k
  bailed_out = true;
36
5.49k
  zend_bailout();
37
5.49k
}
fuzzer-function-jit.c:fuzzer_bailout
Line
Count
Source
34
6.79k
static zend_always_inline void fuzzer_bailout(void) {
35
6.79k
  bailed_out = true;
36
6.79k
  zend_bailout();
37
6.79k
}
fuzzer-execute.c:fuzzer_bailout
Line
Count
Source
34
887
static zend_always_inline void fuzzer_bailout(void) {
35
887
  bailed_out = true;
36
887
  zend_bailout();
37
887
}
38
39
11.1M
static zend_always_inline void fuzzer_step(void) {
40
11.1M
  if (--steps_left == 0) {
41
    /* Reset steps before bailing out, so code running after bailout (e.g. in
42
     * destructors) will get another MAX_STEPS, rather than UINT32_MAX steps. */
43
3.95k
    steps_left = MAX_STEPS;
44
3.95k
    fuzzer_bailout();
45
3.95k
  }
46
11.1M
}
fuzzer-tracing-jit.c:fuzzer_step
Line
Count
Source
39
4.67M
static zend_always_inline void fuzzer_step(void) {
40
4.67M
  if (--steps_left == 0) {
41
    /* Reset steps before bailing out, so code running after bailout (e.g. in
42
     * destructors) will get another MAX_STEPS, rather than UINT32_MAX steps. */
43
1.40k
    steps_left = MAX_STEPS;
44
1.40k
    fuzzer_bailout();
45
1.40k
  }
46
4.67M
}
fuzzer-function-jit.c:fuzzer_step
Line
Count
Source
39
5.54M
static zend_always_inline void fuzzer_step(void) {
40
5.54M
  if (--steps_left == 0) {
41
    /* Reset steps before bailing out, so code running after bailout (e.g. in
42
     * destructors) will get another MAX_STEPS, rather than UINT32_MAX steps. */
43
1.96k
    steps_left = MAX_STEPS;
44
1.96k
    fuzzer_bailout();
45
1.96k
  }
46
5.54M
}
fuzzer-execute.c:fuzzer_step
Line
Count
Source
39
928k
static zend_always_inline void fuzzer_step(void) {
40
928k
  if (--steps_left == 0) {
41
    /* Reset steps before bailing out, so code running after bailout (e.g. in
42
     * destructors) will get another MAX_STEPS, rather than UINT32_MAX steps. */
43
595
    steps_left = MAX_STEPS;
44
595
    fuzzer_bailout();
45
595
  }
46
928k
}
47
48
static void (*orig_execute_ex)(zend_execute_data *execute_data);
49
50
297k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
51
52
297k
#ifdef ZEND_CHECK_STACK_LIMIT
53
297k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
54
0
    zend_call_stack_size_error();
55
    /* No opline was executed before exception */
56
0
    EG(opline_before_exception) = NULL;
57
    /* Fall through to handle exception below. */
58
0
  }
59
297k
#endif /* ZEND_CHECK_STACK_LIMIT */
60
61
297k
  const zend_op *opline = EX(opline);
62
63
10.2M
  while (1) {
64
10.1M
    fuzzer_step();
65
10.1M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
10.1M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
159k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
159k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
159k
      } else {
71
159k
        return;
72
159k
      }
73
159k
    }
74
10.1M
  }
75
297k
}
fuzzer-tracing-jit.c:fuzzer_execute_ex
Line
Count
Source
50
104k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
51
52
104k
#ifdef ZEND_CHECK_STACK_LIMIT
53
104k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
54
0
    zend_call_stack_size_error();
55
    /* No opline was executed before exception */
56
0
    EG(opline_before_exception) = NULL;
57
    /* Fall through to handle exception below. */
58
0
  }
59
104k
#endif /* ZEND_CHECK_STACK_LIMIT */
60
61
104k
  const zend_op *opline = EX(opline);
62
63
4.25M
  while (1) {
64
4.19M
    fuzzer_step();
65
4.19M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
4.19M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
46.5k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
46.5k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
46.5k
      } else {
71
46.5k
        return;
72
46.5k
      }
73
46.5k
    }
74
4.19M
  }
75
104k
}
fuzzer-function-jit.c:fuzzer_execute_ex
Line
Count
Source
50
119k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
51
52
119k
#ifdef ZEND_CHECK_STACK_LIMIT
53
119k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
54
0
    zend_call_stack_size_error();
55
    /* No opline was executed before exception */
56
0
    EG(opline_before_exception) = NULL;
57
    /* Fall through to handle exception below. */
58
0
  }
59
119k
#endif /* ZEND_CHECK_STACK_LIMIT */
60
61
119k
  const zend_op *opline = EX(opline);
62
63
5.12M
  while (1) {
64
5.05M
    fuzzer_step();
65
5.05M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
5.05M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
50.6k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
50.6k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
50.6k
      } else {
71
50.6k
        return;
72
50.6k
      }
73
50.6k
    }
74
5.05M
  }
75
119k
}
fuzzer-execute.c:fuzzer_execute_ex
Line
Count
Source
50
73.4k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
51
52
73.4k
#ifdef ZEND_CHECK_STACK_LIMIT
53
73.4k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
54
0
    zend_call_stack_size_error();
55
    /* No opline was executed before exception */
56
0
    EG(opline_before_exception) = NULL;
57
    /* Fall through to handle exception below. */
58
0
  }
59
73.4k
#endif /* ZEND_CHECK_STACK_LIMIT */
60
61
73.4k
  const zend_op *opline = EX(opline);
62
63
868k
  while (1) {
64
856k
    fuzzer_step();
65
856k
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
856k
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
61.8k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
61.8k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
61.8k
      } else {
71
61.8k
        return;
72
61.8k
      }
73
61.8k
    }
74
856k
  }
75
73.4k
}
76
77
static zend_op_array *(*orig_compile_string)(
78
    zend_string *source_string, const char *filename, zend_compile_position position);
79
80
static zend_op_array *fuzzer_compile_string(
81
6.11k
    zend_string *str, const char *filename, zend_compile_position position) {
82
6.11k
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
9
    fuzzer_bailout();
85
9
  }
86
87
6.11k
  return orig_compile_string(str, filename, position);
88
6.11k
}
fuzzer-tracing-jit.c:fuzzer_compile_string
Line
Count
Source
81
3.12k
    zend_string *str, const char *filename, zend_compile_position position) {
82
3.12k
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
2
    fuzzer_bailout();
85
2
  }
86
87
3.12k
  return orig_compile_string(str, filename, position);
88
3.12k
}
fuzzer-function-jit.c:fuzzer_compile_string
Line
Count
Source
81
2.76k
    zend_string *str, const char *filename, zend_compile_position position) {
82
2.76k
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
5
    fuzzer_bailout();
85
5
  }
86
87
2.76k
  return orig_compile_string(str, filename, position);
88
2.76k
}
fuzzer-execute.c:fuzzer_compile_string
Line
Count
Source
81
218
    zend_string *str, const char *filename, zend_compile_position position) {
82
218
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
2
    fuzzer_bailout();
85
2
  }
86
87
218
  return orig_compile_string(str, filename, position);
88
218
}
89
90
static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);
91
92
1.03M
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
1.03M
  fuzzer_step();
94
95
1.03M
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
2.30M
  for (uint32_t i = 0; i < num_args; i++) {
97
    /* Some internal functions like preg_replace() may be slow on large inputs.
98
     * Limit the maximum size of string inputs. */
99
1.27M
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
1.27M
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
9.21k
      fuzzer_bailout();
102
9.21k
    }
103
1.27M
  }
104
105
1.03M
  orig_execute_internal(execute_data, return_value);
106
1.03M
}
fuzzer-tracing-jit.c:fuzzer_execute_internal
Line
Count
Source
92
475k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
475k
  fuzzer_step();
94
95
475k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
1.08M
  for (uint32_t i = 0; i < num_args; i++) {
97
    /* Some internal functions like preg_replace() may be slow on large inputs.
98
     * Limit the maximum size of string inputs. */
99
604k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
604k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
4.09k
      fuzzer_bailout();
102
4.09k
    }
103
604k
  }
104
105
475k
  orig_execute_internal(execute_data, return_value);
106
475k
}
fuzzer-function-jit.c:fuzzer_execute_internal
Line
Count
Source
92
487k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
487k
  fuzzer_step();
94
95
487k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
1.08M
  for (uint32_t i = 0; i < num_args; i++) {
97
    /* Some internal functions like preg_replace() may be slow on large inputs.
98
     * Limit the maximum size of string inputs. */
99
596k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
596k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
4.83k
      fuzzer_bailout();
102
4.83k
    }
103
596k
  }
104
105
487k
  orig_execute_internal(execute_data, return_value);
106
487k
}
fuzzer-execute.c:fuzzer_execute_internal
Line
Count
Source
92
72.0k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
72.0k
  fuzzer_step();
94
95
72.0k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
145k
  for (uint32_t i = 0; i < num_args; i++) {
97
    /* Some internal functions like preg_replace() may be slow on large inputs.
98
     * Limit the maximum size of string inputs. */
99
73.1k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
73.1k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
290
      fuzzer_bailout();
102
290
    }
103
73.1k
  }
104
105
72.0k
  orig_execute_internal(execute_data, return_value);
106
72.0k
}
107
108
6
static void fuzzer_init_php_for_execute(const char *extra_ini) {
109
  /* Compilation will often trigger fatal errors.
110
   * Use tracked allocation mode to avoid leaks in that case. */
111
6
  putenv("USE_TRACKED_ALLOC=1");
112
113
  /* Just like other SAPIs, ignore SIGPIPEs. */
114
6
  signal(SIGPIPE, SIG_IGN);
115
116
6
  fuzzer_init_php(extra_ini);
117
118
6
  orig_execute_ex = zend_execute_ex;
119
6
  zend_execute_ex = fuzzer_execute_ex;
120
6
  orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
121
6
  zend_execute_internal = fuzzer_execute_internal;
122
6
  orig_compile_string = zend_compile_string;
123
6
  zend_compile_string = fuzzer_compile_string;
124
6
}
fuzzer-tracing-jit.c:fuzzer_init_php_for_execute
Line
Count
Source
108
2
static void fuzzer_init_php_for_execute(const char *extra_ini) {
109
  /* Compilation will often trigger fatal errors.
110
   * Use tracked allocation mode to avoid leaks in that case. */
111
2
  putenv("USE_TRACKED_ALLOC=1");
112
113
  /* Just like other SAPIs, ignore SIGPIPEs. */
114
2
  signal(SIGPIPE, SIG_IGN);
115
116
2
  fuzzer_init_php(extra_ini);
117
118
2
  orig_execute_ex = zend_execute_ex;
119
2
  zend_execute_ex = fuzzer_execute_ex;
120
2
  orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
121
2
  zend_execute_internal = fuzzer_execute_internal;
122
2
  orig_compile_string = zend_compile_string;
123
2
  zend_compile_string = fuzzer_compile_string;
124
2
}
fuzzer-function-jit.c:fuzzer_init_php_for_execute
Line
Count
Source
108
2
static void fuzzer_init_php_for_execute(const char *extra_ini) {
109
  /* Compilation will often trigger fatal errors.
110
   * Use tracked allocation mode to avoid leaks in that case. */
111
2
  putenv("USE_TRACKED_ALLOC=1");
112
113
  /* Just like other SAPIs, ignore SIGPIPEs. */
114
2
  signal(SIGPIPE, SIG_IGN);
115
116
2
  fuzzer_init_php(extra_ini);
117
118
2
  orig_execute_ex = zend_execute_ex;
119
2
  zend_execute_ex = fuzzer_execute_ex;
120
2
  orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
121
2
  zend_execute_internal = fuzzer_execute_internal;
122
2
  orig_compile_string = zend_compile_string;
123
2
  zend_compile_string = fuzzer_compile_string;
124
2
}
fuzzer-execute.c:fuzzer_init_php_for_execute
Line
Count
Source
108
2
static void fuzzer_init_php_for_execute(const char *extra_ini) {
109
  /* Compilation will often trigger fatal errors.
110
   * Use tracked allocation mode to avoid leaks in that case. */
111
2
  putenv("USE_TRACKED_ALLOC=1");
112
113
  /* Just like other SAPIs, ignore SIGPIPEs. */
114
2
  signal(SIGPIPE, SIG_IGN);
115
116
2
  fuzzer_init_php(extra_ini);
117
118
2
  orig_execute_ex = zend_execute_ex;
119
2
  zend_execute_ex = fuzzer_execute_ex;
120
2
  orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
121
2
  zend_execute_internal = fuzzer_execute_internal;
122
2
  orig_compile_string = zend_compile_string;
123
2
  zend_compile_string = fuzzer_compile_string;
124
2
}
125
126
4
ZEND_ATTRIBUTE_UNUSED static void create_file(void) {
127
  /* For opcache_invalidate() to work, the dummy file name used for fuzzing needs to
128
   * actually exist. */
129
4
  FILE *f = fopen(FILE_NAME, "w");
130
4
  fclose(f);
131
4
}
fuzzer-tracing-jit.c:create_file
Line
Count
Source
126
2
ZEND_ATTRIBUTE_UNUSED static void create_file(void) {
127
  /* For opcache_invalidate() to work, the dummy file name used for fuzzing needs to
128
   * actually exist. */
129
2
  FILE *f = fopen(FILE_NAME, "w");
130
2
  fclose(f);
131
2
}
fuzzer-function-jit.c:create_file
Line
Count
Source
126
2
ZEND_ATTRIBUTE_UNUSED static void create_file(void) {
127
  /* For opcache_invalidate() to work, the dummy file name used for fuzzing needs to
128
   * actually exist. */
129
2
  FILE *f = fopen(FILE_NAME, "w");
130
2
  fclose(f);
131
2
}
Unexecuted instantiation: fuzzer-execute.c:create_file
132
133
72.5k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
134
72.5k
  steps_left = MAX_STEPS;
135
72.5k
  zend_object *exception = EG(exception);
136
72.5k
  EG(exception) = NULL;
137
72.5k
  zval retval, args[2];
138
72.5k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
72.5k
  ZEND_ASSERT(fn != NULL);
140
141
72.5k
  ZVAL_STRING(&args[0], FILE_NAME);
142
72.5k
  ZVAL_TRUE(&args[1]);
143
72.5k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
72.5k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
72.5k
  zval_ptr_dtor(&args[0]);
146
72.5k
  zval_ptr_dtor(&retval);
147
72.5k
  EG(exception) = exception;
148
72.5k
}
fuzzer-tracing-jit.c:opcache_invalidate
Line
Count
Source
133
40.0k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
134
40.0k
  steps_left = MAX_STEPS;
135
40.0k
  zend_object *exception = EG(exception);
136
40.0k
  EG(exception) = NULL;
137
40.0k
  zval retval, args[2];
138
40.0k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
40.0k
  ZEND_ASSERT(fn != NULL);
140
141
40.0k
  ZVAL_STRING(&args[0], FILE_NAME);
142
40.0k
  ZVAL_TRUE(&args[1]);
143
40.0k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
40.0k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
40.0k
  zval_ptr_dtor(&args[0]);
146
40.0k
  zval_ptr_dtor(&retval);
147
40.0k
  EG(exception) = exception;
148
40.0k
}
fuzzer-function-jit.c:opcache_invalidate
Line
Count
Source
133
32.5k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
134
32.5k
  steps_left = MAX_STEPS;
135
32.5k
  zend_object *exception = EG(exception);
136
32.5k
  EG(exception) = NULL;
137
32.5k
  zval retval, args[2];
138
32.5k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
32.5k
  ZEND_ASSERT(fn != NULL);
140
141
32.5k
  ZVAL_STRING(&args[0], FILE_NAME);
142
32.5k
  ZVAL_TRUE(&args[1]);
143
32.5k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
32.5k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
32.5k
  zval_ptr_dtor(&args[0]);
146
32.5k
  zval_ptr_dtor(&retval);
147
32.5k
  EG(exception) = exception;
148
32.5k
}
Unexecuted instantiation: fuzzer-execute.c:opcache_invalidate