Coverage Report

Created: 2025-11-16 06:23

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
245k
#define FILE_NAME "/tmp/fuzzer.php"
29
288k
#define MAX_STEPS 1000
30
771k
#define MAX_SIZE (8 * 1024)
31
8.19M
#define ZEND_VM_ENTER_BIT 1ULL
32
33
static uint32_t steps_left;
34
static bool bailed_out = false;
35
36
10.9k
static zend_always_inline void fuzzer_bailout(void) {
37
10.9k
  bailed_out = true;
38
10.9k
  zend_bailout();
39
10.9k
}
fuzzer-tracing-jit.c:fuzzer_bailout
Line
Count
Source
36
5.06k
static zend_always_inline void fuzzer_bailout(void) {
37
5.06k
  bailed_out = true;
38
5.06k
  zend_bailout();
39
5.06k
}
fuzzer-function-jit.c:fuzzer_bailout
Line
Count
Source
36
5.61k
static zend_always_inline void fuzzer_bailout(void) {
37
5.61k
  bailed_out = true;
38
5.61k
  zend_bailout();
39
5.61k
}
fuzzer-execute.c:fuzzer_bailout
Line
Count
Source
36
236
static zend_always_inline void fuzzer_bailout(void) {
37
236
  bailed_out = true;
38
236
  zend_bailout();
39
236
}
40
41
8.96M
static zend_always_inline void fuzzer_step(void) {
42
8.96M
  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
2.74k
    steps_left = MAX_STEPS;
46
2.74k
    fuzzer_bailout();
47
2.74k
  }
48
8.96M
}
fuzzer-tracing-jit.c:fuzzer_step
Line
Count
Source
41
4.07M
static zend_always_inline void fuzzer_step(void) {
42
4.07M
  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.09k
    steps_left = MAX_STEPS;
46
1.09k
    fuzzer_bailout();
47
1.09k
  }
48
4.07M
}
fuzzer-function-jit.c:fuzzer_step
Line
Count
Source
41
4.58M
static zend_always_inline void fuzzer_step(void) {
42
4.58M
  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.44k
    steps_left = MAX_STEPS;
46
1.44k
    fuzzer_bailout();
47
1.44k
  }
48
4.58M
}
fuzzer-execute.c:fuzzer_step
Line
Count
Source
41
301k
static zend_always_inline void fuzzer_step(void) {
42
301k
  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
216
    steps_left = MAX_STEPS;
46
216
    fuzzer_bailout();
47
216
  }
48
301k
}
49
50
static void (*orig_execute_ex)(zend_execute_data *execute_data);
51
52
219k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
219k
#ifdef ZEND_CHECK_STACK_LIMIT
55
219k
  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
219k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
219k
  const zend_op *opline = EX(opline);
64
65
8.20M
  while (1) {
66
8.08M
    fuzzer_step();
67
8.08M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
8.08M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
105k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
105k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
105k
      } else {
73
105k
        return;
74
105k
      }
75
105k
    }
76
8.08M
  }
77
219k
}
fuzzer-tracing-jit.c:fuzzer_execute_ex
Line
Count
Source
52
85.6k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
85.6k
#ifdef ZEND_CHECK_STACK_LIMIT
55
85.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
85.6k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
85.6k
  const zend_op *opline = EX(opline);
64
65
3.69M
  while (1) {
66
3.64M
    fuzzer_step();
67
3.64M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
3.64M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
33.3k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
33.3k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
33.3k
      } else {
73
33.3k
        return;
74
33.3k
      }
75
33.3k
    }
76
3.64M
  }
77
85.6k
}
fuzzer-function-jit.c:fuzzer_execute_ex
Line
Count
Source
52
93.6k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
93.6k
#ifdef ZEND_CHECK_STACK_LIMIT
55
93.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
93.6k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
93.6k
  const zend_op *opline = EX(opline);
64
65
4.21M
  while (1) {
66
4.16M
    fuzzer_step();
67
4.16M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
4.16M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
39.5k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
39.5k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
39.5k
      } else {
73
39.5k
        return;
74
39.5k
      }
75
39.5k
    }
76
4.16M
  }
77
93.6k
}
fuzzer-execute.c:fuzzer_execute_ex
Line
Count
Source
52
40.2k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
40.2k
#ifdef ZEND_CHECK_STACK_LIMIT
55
40.2k
  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
40.2k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
40.2k
  const zend_op *opline = EX(opline);
64
65
284k
  while (1) {
66
277k
    fuzzer_step();
67
277k
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
277k
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
32.8k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
32.8k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
32.8k
      } else {
73
32.8k
        return;
74
32.8k
      }
75
32.8k
    }
76
277k
  }
77
40.2k
}
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
3.91k
    zend_string *str, const char *filename, zend_compile_position position) {
84
3.91k
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
7
    fuzzer_bailout();
87
7
  }
88
89
3.91k
  return orig_compile_string(str, filename, position);
90
3.91k
}
fuzzer-tracing-jit.c:fuzzer_compile_string
Line
Count
Source
83
2.21k
    zend_string *str, const char *filename, zend_compile_position position) {
84
2.21k
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
3
    fuzzer_bailout();
87
3
  }
88
89
2.21k
  return orig_compile_string(str, filename, position);
90
2.21k
}
fuzzer-function-jit.c:fuzzer_compile_string
Line
Count
Source
83
1.66k
    zend_string *str, const char *filename, zend_compile_position position) {
84
1.66k
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
4
    fuzzer_bailout();
87
4
  }
88
89
1.66k
  return orig_compile_string(str, filename, position);
90
1.66k
}
fuzzer-execute.c:fuzzer_compile_string
Line
Count
Source
83
40
    zend_string *str, const char *filename, zend_compile_position position) {
84
40
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
0
    fuzzer_bailout();
87
0
  }
88
89
40
  return orig_compile_string(str, filename, position);
90
40
}
91
92
static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);
93
94
880k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
880k
  fuzzer_step();
96
97
880k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
2.00M
  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
1.12M
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
1.12M
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
8.15k
      fuzzer_bailout();
104
8.15k
    }
105
1.12M
  }
106
107
880k
  orig_execute_internal(execute_data, return_value);
108
880k
}
fuzzer-tracing-jit.c:fuzzer_execute_internal
Line
Count
Source
94
432k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
432k
  fuzzer_step();
96
97
432k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
998k
  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
566k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
566k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
3.96k
      fuzzer_bailout();
104
3.96k
    }
105
566k
  }
106
107
432k
  orig_execute_internal(execute_data, return_value);
108
432k
}
fuzzer-function-jit.c:fuzzer_execute_internal
Line
Count
Source
94
424k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
424k
  fuzzer_step();
96
97
424k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
961k
  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
537k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
537k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
4.17k
      fuzzer_bailout();
104
4.17k
    }
105
537k
  }
106
107
424k
  orig_execute_internal(execute_data, return_value);
108
424k
}
fuzzer-execute.c:fuzzer_execute_internal
Line
Count
Source
94
24.3k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
24.3k
  fuzzer_step();
96
97
24.3k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
46.2k
  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
21.9k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
21.9k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
20
      fuzzer_bailout();
104
20
    }
105
21.9k
  }
106
107
24.3k
  orig_execute_internal(execute_data, return_value);
108
24.3k
}
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
70.8k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
70.8k
  steps_left = MAX_STEPS;
137
70.8k
  zend_exception_save();
138
70.8k
  zval retval, args[2];
139
70.8k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
140
70.8k
  ZEND_ASSERT(fn != NULL);
141
142
70.8k
  ZVAL_STRING(&args[0], FILE_NAME);
143
70.8k
  ZVAL_TRUE(&args[1]);
144
70.8k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
145
70.8k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
146
70.8k
  zval_ptr_dtor(&args[0]);
147
70.8k
  zval_ptr_dtor(&retval);
148
70.8k
  zend_exception_restore();
149
70.8k
}
fuzzer-tracing-jit.c:opcache_invalidate
Line
Count
Source
135
36.8k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
36.8k
  steps_left = MAX_STEPS;
137
36.8k
  zend_exception_save();
138
36.8k
  zval retval, args[2];
139
36.8k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
140
36.8k
  ZEND_ASSERT(fn != NULL);
141
142
36.8k
  ZVAL_STRING(&args[0], FILE_NAME);
143
36.8k
  ZVAL_TRUE(&args[1]);
144
36.8k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
145
36.8k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
146
36.8k
  zval_ptr_dtor(&args[0]);
147
36.8k
  zval_ptr_dtor(&retval);
148
36.8k
  zend_exception_restore();
149
36.8k
}
fuzzer-function-jit.c:opcache_invalidate
Line
Count
Source
135
34.0k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
34.0k
  steps_left = MAX_STEPS;
137
34.0k
  zend_exception_save();
138
34.0k
  zval retval, args[2];
139
34.0k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
140
34.0k
  ZEND_ASSERT(fn != NULL);
141
142
34.0k
  ZVAL_STRING(&args[0], FILE_NAME);
143
34.0k
  ZVAL_TRUE(&args[1]);
144
34.0k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
145
34.0k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
146
34.0k
  zval_ptr_dtor(&args[0]);
147
34.0k
  zval_ptr_dtor(&retval);
148
34.0k
  zend_exception_restore();
149
34.0k
}
Unexecuted instantiation: fuzzer-execute.c:opcache_invalidate