Coverage Report

Created: 2026-06-13 07:01

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
211k
#define FILE_NAME "/tmp/fuzzer.php"
27
249k
#define MAX_STEPS 1000
28
769k
#define MAX_SIZE (8 * 1024)
29
9.04M
#define ZEND_VM_ENTER_BIT 1ULL
30
31
static uint32_t steps_left;
32
static bool bailed_out = false;
33
34
11.9k
static zend_always_inline void fuzzer_bailout(void) {
35
11.9k
  bailed_out = true;
36
11.9k
  zend_bailout();
37
11.9k
}
fuzzer-tracing-jit.c:fuzzer_bailout
Line
Count
Source
34
4.73k
static zend_always_inline void fuzzer_bailout(void) {
35
4.73k
  bailed_out = true;
36
4.73k
  zend_bailout();
37
4.73k
}
fuzzer-function-jit.c:fuzzer_bailout
Line
Count
Source
34
6.57k
static zend_always_inline void fuzzer_bailout(void) {
35
6.57k
  bailed_out = true;
36
6.57k
  zend_bailout();
37
6.57k
}
fuzzer-execute.c:fuzzer_bailout
Line
Count
Source
34
614
static zend_always_inline void fuzzer_bailout(void) {
35
614
  bailed_out = true;
36
614
  zend_bailout();
37
614
}
38
39
9.84M
static zend_always_inline void fuzzer_step(void) {
40
9.84M
  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.31k
    steps_left = MAX_STEPS;
44
3.31k
    fuzzer_bailout();
45
3.31k
  }
46
9.84M
}
fuzzer-tracing-jit.c:fuzzer_step
Line
Count
Source
39
3.98M
static zend_always_inline void fuzzer_step(void) {
40
3.98M
  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.16k
    steps_left = MAX_STEPS;
44
1.16k
    fuzzer_bailout();
45
1.16k
  }
46
3.98M
}
fuzzer-function-jit.c:fuzzer_step
Line
Count
Source
39
5.20M
static zend_always_inline void fuzzer_step(void) {
40
5.20M
  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.73k
    steps_left = MAX_STEPS;
44
1.73k
    fuzzer_bailout();
45
1.73k
  }
46
5.20M
}
fuzzer-execute.c:fuzzer_step
Line
Count
Source
39
658k
static zend_always_inline void fuzzer_step(void) {
40
658k
  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
412
    steps_left = MAX_STEPS;
44
412
    fuzzer_bailout();
45
412
  }
46
658k
}
47
48
static void (*orig_execute_ex)(zend_execute_data *execute_data);
49
50
247k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
51
52
247k
#ifdef ZEND_CHECK_STACK_LIMIT
53
247k
  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
247k
#endif /* ZEND_CHECK_STACK_LIMIT */
60
61
247k
  const zend_op *opline = EX(opline);
62
63
9.05M
  while (1) {
64
8.92M
    fuzzer_step();
65
8.92M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
8.92M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
121k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
121k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
121k
      } else {
71
121k
        return;
72
121k
      }
73
121k
    }
74
8.92M
  }
75
247k
}
fuzzer-tracing-jit.c:fuzzer_execute_ex
Line
Count
Source
50
91.4k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
51
52
91.4k
#ifdef ZEND_CHECK_STACK_LIMIT
53
91.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
91.4k
#endif /* ZEND_CHECK_STACK_LIMIT */
60
61
91.4k
  const zend_op *opline = EX(opline);
62
63
3.62M
  while (1) {
64
3.57M
    fuzzer_step();
65
3.57M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
3.57M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
37.5k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
37.5k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
37.5k
      } else {
71
37.5k
        return;
72
37.5k
      }
73
37.5k
    }
74
3.57M
  }
75
91.4k
}
fuzzer-function-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.80M
  while (1) {
64
4.74M
    fuzzer_step();
65
4.74M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
4.74M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
42.4k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
42.4k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
42.4k
      } else {
71
42.4k
        return;
72
42.4k
      }
73
42.4k
    }
74
4.74M
  }
75
104k
}
fuzzer-execute.c:fuzzer_execute_ex
Line
Count
Source
50
51.3k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
51
52
51.3k
#ifdef ZEND_CHECK_STACK_LIMIT
53
51.3k
  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
51.3k
#endif /* ZEND_CHECK_STACK_LIMIT */
60
61
51.3k
  const zend_op *opline = EX(opline);
62
63
615k
  while (1) {
64
605k
    fuzzer_step();
65
605k
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
66
605k
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
67
41.5k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
68
41.5k
      if (opline) {
69
0
        execute_data = EG(current_execute_data);
70
41.5k
      } else {
71
41.5k
        return;
72
41.5k
      }
73
41.5k
    }
74
605k
  }
75
51.3k
}
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
4.80k
    zend_string *str, const char *filename, zend_compile_position position) {
82
4.80k
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
9
    fuzzer_bailout();
85
9
  }
86
87
4.80k
  return orig_compile_string(str, filename, position);
88
4.80k
}
fuzzer-tracing-jit.c:fuzzer_compile_string
Line
Count
Source
81
2.65k
    zend_string *str, const char *filename, zend_compile_position position) {
82
2.65k
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
2
    fuzzer_bailout();
85
2
  }
86
87
2.65k
  return orig_compile_string(str, filename, position);
88
2.65k
}
fuzzer-function-jit.c:fuzzer_compile_string
Line
Count
Source
81
1.95k
    zend_string *str, const char *filename, zend_compile_position position) {
82
1.95k
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
5
    fuzzer_bailout();
85
5
  }
86
87
1.95k
  return orig_compile_string(str, filename, position);
88
1.95k
}
fuzzer-execute.c:fuzzer_compile_string
Line
Count
Source
81
196
    zend_string *str, const char *filename, zend_compile_position position) {
82
196
  if (ZSTR_LEN(str) > MAX_SIZE) {
83
    /* Avoid compiling huge inputs via eval(). */
84
2
    fuzzer_bailout();
85
2
  }
86
87
196
  return orig_compile_string(str, filename, position);
88
196
}
89
90
static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);
91
92
913k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
913k
  fuzzer_step();
94
95
913k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
2.04M
  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.13M
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
1.13M
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
8.60k
      fuzzer_bailout();
102
8.60k
    }
103
1.13M
  }
104
105
913k
  orig_execute_internal(execute_data, return_value);
106
913k
}
fuzzer-tracing-jit.c:fuzzer_execute_internal
Line
Count
Source
92
407k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
407k
  fuzzer_step();
94
95
407k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
935k
  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
527k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
527k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
3.57k
      fuzzer_bailout();
102
3.57k
    }
103
527k
  }
104
105
407k
  orig_execute_internal(execute_data, return_value);
106
407k
}
fuzzer-function-jit.c:fuzzer_execute_internal
Line
Count
Source
92
452k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
452k
  fuzzer_step();
94
95
452k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
1.00M
  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
555k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
555k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
4.83k
      fuzzer_bailout();
102
4.83k
    }
103
555k
  }
104
105
452k
  orig_execute_internal(execute_data, return_value);
106
452k
}
fuzzer-execute.c:fuzzer_execute_internal
Line
Count
Source
92
53.0k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
93
53.0k
  fuzzer_step();
94
95
53.0k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
96
105k
  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
52.6k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
100
52.6k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
101
200
      fuzzer_bailout();
102
200
    }
103
52.6k
  }
104
105
53.0k
  orig_execute_internal(execute_data, return_value);
106
53.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
58.0k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
134
58.0k
  steps_left = MAX_STEPS;
135
58.0k
  zend_object *exception = EG(exception);
136
58.0k
  EG(exception) = NULL;
137
58.0k
  zval retval, args[2];
138
58.0k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
58.0k
  ZEND_ASSERT(fn != NULL);
140
141
58.0k
  ZVAL_STRING(&args[0], FILE_NAME);
142
58.0k
  ZVAL_TRUE(&args[1]);
143
58.0k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
58.0k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
58.0k
  zval_ptr_dtor(&args[0]);
146
58.0k
  zval_ptr_dtor(&retval);
147
58.0k
  EG(exception) = exception;
148
58.0k
}
fuzzer-tracing-jit.c:opcache_invalidate
Line
Count
Source
133
31.7k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
134
31.7k
  steps_left = MAX_STEPS;
135
31.7k
  zend_object *exception = EG(exception);
136
31.7k
  EG(exception) = NULL;
137
31.7k
  zval retval, args[2];
138
31.7k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
31.7k
  ZEND_ASSERT(fn != NULL);
140
141
31.7k
  ZVAL_STRING(&args[0], FILE_NAME);
142
31.7k
  ZVAL_TRUE(&args[1]);
143
31.7k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
31.7k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
31.7k
  zval_ptr_dtor(&args[0]);
146
31.7k
  zval_ptr_dtor(&retval);
147
31.7k
  EG(exception) = exception;
148
31.7k
}
fuzzer-function-jit.c:opcache_invalidate
Line
Count
Source
133
26.3k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
134
26.3k
  steps_left = MAX_STEPS;
135
26.3k
  zend_object *exception = EG(exception);
136
26.3k
  EG(exception) = NULL;
137
26.3k
  zval retval, args[2];
138
26.3k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
26.3k
  ZEND_ASSERT(fn != NULL);
140
141
26.3k
  ZVAL_STRING(&args[0], FILE_NAME);
142
26.3k
  ZVAL_TRUE(&args[1]);
143
26.3k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
26.3k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
26.3k
  zval_ptr_dtor(&args[0]);
146
26.3k
  zval_ptr_dtor(&retval);
147
26.3k
  EG(exception) = exception;
148
26.3k
}
Unexecuted instantiation: fuzzer-execute.c:opcache_invalidate