Coverage Report

Created: 2026-01-18 06:49

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 (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | https://www.php.net/license/3_01.txt                                 |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Authors: Nikita Popov <nikic@php.net>                                |
14
   +----------------------------------------------------------------------+
15
 */
16
17
#include <main/php.h>
18
19
#if defined(__FreeBSD__)
20
# include <sys/sysctl.h>
21
#endif
22
23
#include "fuzzer.h"
24
#include "fuzzer-sapi.h"
25
#include "zend_exceptions.h"
26
#include "zend_vm.h"
27
28
59.2k
#define FILE_NAME "/tmp/fuzzer.php"
29
60.7k
#define MAX_STEPS 1000
30
171k
#define MAX_SIZE (8 * 1024)
31
2.25M
#define ZEND_VM_ENTER_BIT 1ULL
32
33
static uint32_t steps_left;
34
static bool bailed_out = false;
35
36
3.33k
static zend_always_inline void fuzzer_bailout(void) {
37
3.33k
  bailed_out = true;
38
3.33k
  zend_bailout();
39
3.33k
}
fuzzer-tracing-jit.c:fuzzer_bailout
Line
Count
Source
36
51
static zend_always_inline void fuzzer_bailout(void) {
37
51
  bailed_out = true;
38
51
  zend_bailout();
39
51
}
fuzzer-function-jit.c:fuzzer_bailout
Line
Count
Source
36
58
static zend_always_inline void fuzzer_bailout(void) {
37
58
  bailed_out = true;
38
58
  zend_bailout();
39
58
}
fuzzer-execute.c:fuzzer_bailout
Line
Count
Source
36
3.22k
static zend_always_inline void fuzzer_bailout(void) {
37
3.22k
  bailed_out = true;
38
3.22k
  zend_bailout();
39
3.22k
}
40
41
2.37M
static zend_always_inline void fuzzer_step(void) {
42
2.37M
  if (--steps_left == 0) {
43
    /* Reset steps before bailing out, so code running after bailout (e.g. in
44
     * destructors) will get another MAX_STEPS, rather than UINT32_MAX steps. */
45
1.38k
    steps_left = MAX_STEPS;
46
1.38k
    fuzzer_bailout();
47
1.38k
  }
48
2.37M
}
fuzzer-tracing-jit.c:fuzzer_step
Line
Count
Source
41
49.4k
static zend_always_inline void fuzzer_step(void) {
42
49.4k
  if (--steps_left == 0) {
43
    /* Reset steps before bailing out, so code running after bailout (e.g. in
44
     * destructors) will get another MAX_STEPS, rather than UINT32_MAX steps. */
45
41
    steps_left = MAX_STEPS;
46
41
    fuzzer_bailout();
47
41
  }
48
49.4k
}
fuzzer-function-jit.c:fuzzer_step
Line
Count
Source
41
50.2k
static zend_always_inline void fuzzer_step(void) {
42
50.2k
  if (--steps_left == 0) {
43
    /* Reset steps before bailing out, so code running after bailout (e.g. in
44
     * destructors) will get another MAX_STEPS, rather than UINT32_MAX steps. */
45
48
    steps_left = MAX_STEPS;
46
48
    fuzzer_bailout();
47
48
  }
48
50.2k
}
fuzzer-execute.c:fuzzer_step
Line
Count
Source
41
2.27M
static zend_always_inline void fuzzer_step(void) {
42
2.27M
  if (--steps_left == 0) {
43
    /* Reset steps before bailing out, so code running after bailout (e.g. in
44
     * destructors) will get another MAX_STEPS, rather than UINT32_MAX steps. */
45
1.29k
    steps_left = MAX_STEPS;
46
1.29k
    fuzzer_bailout();
47
1.29k
  }
48
2.27M
}
49
50
static void (*orig_execute_ex)(zend_execute_data *execute_data);
51
52
76.9k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
76.9k
#ifdef ZEND_CHECK_STACK_LIMIT
55
76.9k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
56
0
    zend_call_stack_size_error();
57
    /* No opline was executed before exception */
58
0
    EG(opline_before_exception) = NULL;
59
    /* Fall through to handle exception below. */
60
0
  }
61
76.9k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
76.9k
  const zend_op *opline = EX(opline);
64
65
2.32M
  while (1) {
66
2.25M
    fuzzer_step();
67
2.25M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
2.25M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
3.90k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
3.90k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
3.90k
      } else {
73
3.90k
        return;
74
3.90k
      }
75
3.90k
    }
76
2.25M
  }
77
76.9k
}
fuzzer-tracing-jit.c:fuzzer_execute_ex
Line
Count
Source
52
2.06k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
2.06k
#ifdef ZEND_CHECK_STACK_LIMIT
55
2.06k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
56
0
    zend_call_stack_size_error();
57
    /* No opline was executed before exception */
58
0
    EG(opline_before_exception) = NULL;
59
    /* Fall through to handle exception below. */
60
0
  }
61
2.06k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
2.06k
  const zend_op *opline = EX(opline);
64
65
48.0k
  while (1) {
66
46.0k
    fuzzer_step();
67
46.0k
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
46.0k
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
55
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
55
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
55
      } else {
73
55
        return;
74
55
      }
75
55
    }
76
46.0k
  }
77
2.06k
}
fuzzer-function-jit.c:fuzzer_execute_ex
Line
Count
Source
52
2.28k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
2.28k
#ifdef ZEND_CHECK_STACK_LIMIT
55
2.28k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
56
0
    zend_call_stack_size_error();
57
    /* No opline was executed before exception */
58
0
    EG(opline_before_exception) = NULL;
59
    /* Fall through to handle exception below. */
60
0
  }
61
2.28k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
2.28k
  const zend_op *opline = EX(opline);
64
65
47.5k
  while (1) {
66
45.2k
    fuzzer_step();
67
45.2k
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
45.2k
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
14
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
14
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
14
      } else {
73
14
        return;
74
14
      }
75
14
    }
76
45.2k
  }
77
2.28k
}
fuzzer-execute.c:fuzzer_execute_ex
Line
Count
Source
52
72.6k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
72.6k
#ifdef ZEND_CHECK_STACK_LIMIT
55
72.6k
  if (UNEXPECTED(zend_call_stack_overflowed(EG(stack_limit)))) {
56
0
    zend_call_stack_size_error();
57
    /* No opline was executed before exception */
58
0
    EG(opline_before_exception) = NULL;
59
    /* Fall through to handle exception below. */
60
0
  }
61
72.6k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
72.6k
  const zend_op *opline = EX(opline);
64
65
2.23M
  while (1) {
66
2.16M
    fuzzer_step();
67
2.16M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
2.16M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
3.83k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
3.83k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
3.83k
      } else {
73
3.83k
        return;
74
3.83k
      }
75
3.83k
    }
76
2.16M
  }
77
72.6k
}
78
79
static zend_op_array *(*orig_compile_string)(
80
    zend_string *source_string, const char *filename, zend_compile_position position);
81
82
static zend_op_array *fuzzer_compile_string(
83
93
    zend_string *str, const char *filename, zend_compile_position position) {
84
93
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
0
    fuzzer_bailout();
87
0
  }
88
89
93
  return orig_compile_string(str, filename, position);
90
93
}
fuzzer-tracing-jit.c:fuzzer_compile_string
Line
Count
Source
83
11
    zend_string *str, const char *filename, zend_compile_position position) {
84
11
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
0
    fuzzer_bailout();
87
0
  }
88
89
11
  return orig_compile_string(str, filename, position);
90
11
}
fuzzer-function-jit.c:fuzzer_compile_string
Line
Count
Source
83
6
    zend_string *str, const char *filename, zend_compile_position position) {
84
6
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
0
    fuzzer_bailout();
87
0
  }
88
89
6
  return orig_compile_string(str, filename, position);
90
6
}
fuzzer-execute.c:fuzzer_compile_string
Line
Count
Source
83
76
    zend_string *str, const char *filename, zend_compile_position position) {
84
76
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
0
    fuzzer_bailout();
87
0
  }
88
89
76
  return orig_compile_string(str, filename, position);
90
76
}
91
92
static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);
93
94
121k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
121k
  fuzzer_step();
96
97
121k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
342k
  for (uint32_t i = 0; i < num_args; i++) {
99
    /* Some internal functions like preg_replace() may be slow on large inputs.
100
     * Limit the maximum size of string inputs. */
101
220k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
220k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
1.95k
      fuzzer_bailout();
104
1.95k
    }
105
220k
  }
106
107
121k
  orig_execute_internal(execute_data, return_value);
108
121k
}
fuzzer-tracing-jit.c:fuzzer_execute_internal
Line
Count
Source
94
3.40k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
3.40k
  fuzzer_step();
96
97
3.40k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
8.44k
  for (uint32_t i = 0; i < num_args; i++) {
99
    /* Some internal functions like preg_replace() may be slow on large inputs.
100
     * Limit the maximum size of string inputs. */
101
5.03k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
5.03k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
10
      fuzzer_bailout();
104
10
    }
105
5.03k
  }
106
107
3.40k
  orig_execute_internal(execute_data, return_value);
108
3.40k
}
fuzzer-function-jit.c:fuzzer_execute_internal
Line
Count
Source
94
4.97k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
4.97k
  fuzzer_step();
96
97
4.97k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
10.9k
  for (uint32_t i = 0; i < num_args; i++) {
99
    /* Some internal functions like preg_replace() may be slow on large inputs.
100
     * Limit the maximum size of string inputs. */
101
5.95k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
5.95k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
10
      fuzzer_bailout();
104
10
    }
105
5.95k
  }
106
107
4.97k
  orig_execute_internal(execute_data, return_value);
108
4.97k
}
fuzzer-execute.c:fuzzer_execute_internal
Line
Count
Source
94
113k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
113k
  fuzzer_step();
96
97
113k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
322k
  for (uint32_t i = 0; i < num_args; i++) {
99
    /* Some internal functions like preg_replace() may be slow on large inputs.
100
     * Limit the maximum size of string inputs. */
101
209k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
209k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
1.93k
      fuzzer_bailout();
104
1.93k
    }
105
209k
  }
106
107
113k
  orig_execute_internal(execute_data, return_value);
108
113k
}
109
110
6
static void fuzzer_init_php_for_execute(const char *extra_ini) {
111
  /* Compilation will often trigger fatal errors.
112
   * Use tracked allocation mode to avoid leaks in that case. */
113
6
  putenv("USE_TRACKED_ALLOC=1");
114
115
  /* Just like other SAPIs, ignore SIGPIPEs. */
116
6
  signal(SIGPIPE, SIG_IGN);
117
118
6
  fuzzer_init_php(extra_ini);
119
120
6
  orig_execute_ex = zend_execute_ex;
121
6
  zend_execute_ex = fuzzer_execute_ex;
122
6
  orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
123
6
  zend_execute_internal = fuzzer_execute_internal;
124
6
  orig_compile_string = zend_compile_string;
125
6
  zend_compile_string = fuzzer_compile_string;
126
6
}
fuzzer-tracing-jit.c:fuzzer_init_php_for_execute
Line
Count
Source
110
2
static void fuzzer_init_php_for_execute(const char *extra_ini) {
111
  /* Compilation will often trigger fatal errors.
112
   * Use tracked allocation mode to avoid leaks in that case. */
113
2
  putenv("USE_TRACKED_ALLOC=1");
114
115
  /* Just like other SAPIs, ignore SIGPIPEs. */
116
2
  signal(SIGPIPE, SIG_IGN);
117
118
2
  fuzzer_init_php(extra_ini);
119
120
2
  orig_execute_ex = zend_execute_ex;
121
2
  zend_execute_ex = fuzzer_execute_ex;
122
2
  orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
123
2
  zend_execute_internal = fuzzer_execute_internal;
124
2
  orig_compile_string = zend_compile_string;
125
2
  zend_compile_string = fuzzer_compile_string;
126
2
}
fuzzer-function-jit.c:fuzzer_init_php_for_execute
Line
Count
Source
110
2
static void fuzzer_init_php_for_execute(const char *extra_ini) {
111
  /* Compilation will often trigger fatal errors.
112
   * Use tracked allocation mode to avoid leaks in that case. */
113
2
  putenv("USE_TRACKED_ALLOC=1");
114
115
  /* Just like other SAPIs, ignore SIGPIPEs. */
116
2
  signal(SIGPIPE, SIG_IGN);
117
118
2
  fuzzer_init_php(extra_ini);
119
120
2
  orig_execute_ex = zend_execute_ex;
121
2
  zend_execute_ex = fuzzer_execute_ex;
122
2
  orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
123
2
  zend_execute_internal = fuzzer_execute_internal;
124
2
  orig_compile_string = zend_compile_string;
125
2
  zend_compile_string = fuzzer_compile_string;
126
2
}
fuzzer-execute.c:fuzzer_init_php_for_execute
Line
Count
Source
110
2
static void fuzzer_init_php_for_execute(const char *extra_ini) {
111
  /* Compilation will often trigger fatal errors.
112
   * Use tracked allocation mode to avoid leaks in that case. */
113
2
  putenv("USE_TRACKED_ALLOC=1");
114
115
  /* Just like other SAPIs, ignore SIGPIPEs. */
116
2
  signal(SIGPIPE, SIG_IGN);
117
118
2
  fuzzer_init_php(extra_ini);
119
120
2
  orig_execute_ex = zend_execute_ex;
121
2
  zend_execute_ex = fuzzer_execute_ex;
122
2
  orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
123
2
  zend_execute_internal = fuzzer_execute_internal;
124
2
  orig_compile_string = zend_compile_string;
125
2
  zend_compile_string = fuzzer_compile_string;
126
2
}
127
128
4
ZEND_ATTRIBUTE_UNUSED static void create_file(void) {
129
  /* For opcache_invalidate() to work, the dummy file name used for fuzzing needs to
130
   * actually exist. */
131
4
  FILE *f = fopen(FILE_NAME, "w");
132
4
  fclose(f);
133
4
}
fuzzer-tracing-jit.c:create_file
Line
Count
Source
128
2
ZEND_ATTRIBUTE_UNUSED static void create_file(void) {
129
  /* For opcache_invalidate() to work, the dummy file name used for fuzzing needs to
130
   * actually exist. */
131
2
  FILE *f = fopen(FILE_NAME, "w");
132
2
  fclose(f);
133
2
}
fuzzer-function-jit.c:create_file
Line
Count
Source
128
2
ZEND_ATTRIBUTE_UNUSED static void create_file(void) {
129
  /* For opcache_invalidate() to work, the dummy file name used for fuzzing needs to
130
   * actually exist. */
131
2
  FILE *f = fopen(FILE_NAME, "w");
132
2
  fclose(f);
133
2
}
Unexecuted instantiation: fuzzer-execute.c:create_file
134
135
160
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
160
  steps_left = MAX_STEPS;
137
160
  zval retval, args[2];
138
160
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
160
  ZEND_ASSERT(fn != NULL);
140
141
160
  ZVAL_STRING(&args[0], FILE_NAME);
142
160
  ZVAL_TRUE(&args[1]);
143
160
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
160
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
160
  zval_ptr_dtor(&args[0]);
146
160
  zval_ptr_dtor(&retval);
147
160
}
fuzzer-tracing-jit.c:opcache_invalidate
Line
Count
Source
135
99
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
99
  steps_left = MAX_STEPS;
137
99
  zval retval, args[2];
138
99
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
99
  ZEND_ASSERT(fn != NULL);
140
141
99
  ZVAL_STRING(&args[0], FILE_NAME);
142
99
  ZVAL_TRUE(&args[1]);
143
99
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
99
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
99
  zval_ptr_dtor(&args[0]);
146
99
  zval_ptr_dtor(&retval);
147
99
}
fuzzer-function-jit.c:opcache_invalidate
Line
Count
Source
135
61
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
61
  steps_left = MAX_STEPS;
137
61
  zval retval, args[2];
138
61
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
139
61
  ZEND_ASSERT(fn != NULL);
140
141
61
  ZVAL_STRING(&args[0], FILE_NAME);
142
61
  ZVAL_TRUE(&args[1]);
143
61
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
144
61
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
145
61
  zval_ptr_dtor(&args[0]);
146
61
  zval_ptr_dtor(&retval);
147
61
}
Unexecuted instantiation: fuzzer-execute.c:opcache_invalidate