Coverage Report

Created: 2026-07-25 06:39

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
270k
#define FILE_NAME "/tmp/fuzzer.php"
27
318k
#define MAX_STEPS 1000
28
899k
#define MAX_SIZE (8 * 1024)
29
10.4M
#define ZEND_VM_ENTER_BIT 1ULL
30
31
static uint32_t steps_left;
32
static bool bailed_out = false;
33
34
13.7k
static zend_always_inline void fuzzer_bailout(void) {
35
13.7k
  bailed_out = true;
36
13.7k
  zend_bailout();
37
13.7k
}
fuzzer-tracing-jit.c:fuzzer_bailout
Line
Count
Source
34
5.92k
static zend_always_inline void fuzzer_bailout(void) {
35
5.92k
  bailed_out = true;
36
5.92k
  zend_bailout();
37
5.92k
}
fuzzer-function-jit.c:fuzzer_bailout
Line
Count
Source
34
7.58k
static zend_always_inline void fuzzer_bailout(void) {
35
7.58k
  bailed_out = true;
36
7.58k
  zend_bailout();
37
7.58k
}
fuzzer-execute.c:fuzzer_bailout
Line
Count
Source
34
279
static zend_always_inline void fuzzer_bailout(void) {
35
279
  bailed_out = true;
36
279
  zend_bailout();
37
279
}
38
39
11.3M
static zend_always_inline void fuzzer_step(void) {
40
11.3M
  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.83k
    steps_left = MAX_STEPS;
44
3.83k
    fuzzer_bailout();
45
3.83k
  }
46
11.3M
}
fuzzer-tracing-jit.c:fuzzer_step
Line
Count
Source
39
4.95M
static zend_always_inline void fuzzer_step(void) {
40
4.95M
  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.53k
    steps_left = MAX_STEPS;
44
1.53k
    fuzzer_bailout();
45
1.53k
  }
46
4.95M
}
fuzzer-function-jit.c:fuzzer_step
Line
Count
Source
39
6.02M
static zend_always_inline void fuzzer_step(void) {
40
6.02M
  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
2.04k
    steps_left = MAX_STEPS;
44
2.04k
    fuzzer_bailout();
45
2.04k
  }
46
6.02M
}
fuzzer-execute.c:fuzzer_step
Line
Count
Source
39
381k
static zend_always_inline void fuzzer_step(void) {
40
381k
  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
265
    steps_left = MAX_STEPS;
44
265
    fuzzer_bailout();
45
265
  }
46
381k
}
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.4M
  while (1) {
64
10.3M
    fuzzer_step();
65
10.3M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
10.3M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
151k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
151k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
151k
      } else {
71
151k
        return;
72
151k
      }
73
151k
    }
74
10.3M
  }
75
297k
}
fuzzer-tracing-jit.c:fuzzer_execute_ex
Line
Count
Source
50
111k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
51
52
111k
#ifdef ZEND_CHECK_STACK_LIMIT
53
111k
  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
111k
#endif /* ZEND_CHECK_STACK_LIMIT */
60
61
111k
  const zend_op *opline = EX(opline);
62
63
4.52M
  while (1) {
64
4.46M
    fuzzer_step();
65
4.46M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
4.46M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
46.8k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
46.8k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
46.8k
      } else {
71
46.8k
        return;
72
46.8k
      }
73
46.8k
    }
74
4.46M
  }
75
111k
}
fuzzer-function-jit.c:fuzzer_execute_ex
Line
Count
Source
50
123k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
51
52
123k
#ifdef ZEND_CHECK_STACK_LIMIT
53
123k
  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
123k
#endif /* ZEND_CHECK_STACK_LIMIT */
60
61
123k
  const zend_op *opline = EX(opline);
62
63
5.57M
  while (1) {
64
5.49M
    fuzzer_step();
65
5.49M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
5.49M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
49.9k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
49.9k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
49.9k
      } else {
71
49.9k
        return;
72
49.9k
      }
73
49.9k
    }
74
5.49M
  }
75
123k
}
fuzzer-execute.c:fuzzer_execute_ex
Line
Count
Source
50
63.2k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
51
52
63.2k
#ifdef ZEND_CHECK_STACK_LIMIT
53
63.2k
  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
63.2k
#endif /* ZEND_CHECK_STACK_LIMIT */
60
61
63.2k
  const zend_op *opline = EX(opline);
62
63
361k
  while (1) {
64
353k
    fuzzer_step();
65
353k
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
353k
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
55.1k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
55.1k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
55.1k
      } else {
71
55.1k
        return;
72
55.1k
      }
73
55.1k
    }
74
353k
  }
75
63.2k
}
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.89k
    zend_string *str, const char *filename, zend_compile_position position) {
82
6.89k
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
7
    fuzzer_bailout();
85
7
  }
86
87
6.89k
  return orig_compile_string(str, filename, position);
88
6.89k
}
fuzzer-tracing-jit.c:fuzzer_compile_string
Line
Count
Source
81
4.18k
    zend_string *str, const char *filename, zend_compile_position position) {
82
4.18k
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
2
    fuzzer_bailout();
85
2
  }
86
87
4.18k
  return orig_compile_string(str, filename, position);
88
4.18k
}
fuzzer-function-jit.c:fuzzer_compile_string
Line
Count
Source
81
2.66k
    zend_string *str, const char *filename, zend_compile_position position) {
82
2.66k
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
5
    fuzzer_bailout();
85
5
  }
86
87
2.66k
  return orig_compile_string(str, filename, position);
88
2.66k
}
fuzzer-execute.c:fuzzer_compile_string
Line
Count
Source
81
44
    zend_string *str, const char *filename, zend_compile_position position) {
82
44
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
0
    fuzzer_bailout();
85
0
  }
86
87
44
  return orig_compile_string(str, filename, position);
88
44
}
89
90
static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);
91
92
1.04M
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
1.04M
  fuzzer_step();
94
95
1.04M
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
2.35M
  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.31M
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
1.31M
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
9.94k
      fuzzer_bailout();
102
9.94k
    }
103
1.31M
  }
104
105
1.04M
  orig_execute_internal(execute_data, return_value);
106
1.04M
}
fuzzer-tracing-jit.c:fuzzer_execute_internal
Line
Count
Source
92
496k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
496k
  fuzzer_step();
94
95
496k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
1.13M
  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
641k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
641k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
4.39k
      fuzzer_bailout();
102
4.39k
    }
103
641k
  }
104
105
496k
  orig_execute_internal(execute_data, return_value);
106
496k
}
fuzzer-function-jit.c:fuzzer_execute_internal
Line
Count
Source
92
523k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
523k
  fuzzer_step();
94
95
523k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
1.16M
  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
643k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
643k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
5.53k
      fuzzer_bailout();
102
5.53k
    }
103
643k
  }
104
105
523k
  orig_execute_internal(execute_data, return_value);
106
523k
}
fuzzer-execute.c:fuzzer_execute_internal
Line
Count
Source
92
28.0k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
28.0k
  fuzzer_step();
94
95
28.0k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
54.5k
  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
26.5k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
26.5k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
14
      fuzzer_bailout();
102
14
    }
103
26.5k
  }
104
105
28.0k
  orig_execute_internal(execute_data, return_value);
106
28.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
75.0k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
134
75.0k
  steps_left = MAX_STEPS;
135
75.0k
  zend_object *exception = EG(exception);
136
75.0k
  EG(exception) = NULL;
137
75.0k
  zval retval, args[2];
138
75.0k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
75.0k
  ZEND_ASSERT(fn != NULL);
140
141
75.0k
  ZVAL_STRING(&args[0], FILE_NAME);
142
75.0k
  ZVAL_TRUE(&args[1]);
143
75.0k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
75.0k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
75.0k
  zval_ptr_dtor(&args[0]);
146
75.0k
  zval_ptr_dtor(&retval);
147
75.0k
  EG(exception) = exception;
148
75.0k
}
fuzzer-tracing-jit.c:opcache_invalidate
Line
Count
Source
133
42.0k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
134
42.0k
  steps_left = MAX_STEPS;
135
42.0k
  zend_object *exception = EG(exception);
136
42.0k
  EG(exception) = NULL;
137
42.0k
  zval retval, args[2];
138
42.0k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
42.0k
  ZEND_ASSERT(fn != NULL);
140
141
42.0k
  ZVAL_STRING(&args[0], FILE_NAME);
142
42.0k
  ZVAL_TRUE(&args[1]);
143
42.0k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
42.0k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
42.0k
  zval_ptr_dtor(&args[0]);
146
42.0k
  zval_ptr_dtor(&retval);
147
42.0k
  EG(exception) = exception;
148
42.0k
}
fuzzer-function-jit.c:opcache_invalidate
Line
Count
Source
133
33.0k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
134
33.0k
  steps_left = MAX_STEPS;
135
33.0k
  zend_object *exception = EG(exception);
136
33.0k
  EG(exception) = NULL;
137
33.0k
  zval retval, args[2];
138
33.0k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
33.0k
  ZEND_ASSERT(fn != NULL);
140
141
33.0k
  ZVAL_STRING(&args[0], FILE_NAME);
142
33.0k
  ZVAL_TRUE(&args[1]);
143
33.0k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
33.0k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
33.0k
  zval_ptr_dtor(&args[0]);
146
33.0k
  zval_ptr_dtor(&retval);
147
33.0k
  EG(exception) = exception;
148
33.0k
}
Unexecuted instantiation: fuzzer-execute.c:opcache_invalidate