Coverage Report

Created: 2025-12-14 06:10

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
290k
#define FILE_NAME "/tmp/fuzzer.php"
29
340k
#define MAX_STEPS 1000
30
911k
#define MAX_SIZE (8 * 1024)
31
9.69M
#define ZEND_VM_ENTER_BIT 1ULL
32
33
static uint32_t steps_left;
34
static bool bailed_out = false;
35
36
13.1k
static zend_always_inline void fuzzer_bailout(void) {
37
13.1k
  bailed_out = true;
38
13.1k
  zend_bailout();
39
13.1k
}
fuzzer-tracing-jit.c:fuzzer_bailout
Line
Count
Source
36
5.73k
static zend_always_inline void fuzzer_bailout(void) {
37
5.73k
  bailed_out = true;
38
5.73k
  zend_bailout();
39
5.73k
}
fuzzer-function-jit.c:fuzzer_bailout
Line
Count
Source
36
7.13k
static zend_always_inline void fuzzer_bailout(void) {
37
7.13k
  bailed_out = true;
38
7.13k
  zend_bailout();
39
7.13k
}
fuzzer-execute.c:fuzzer_bailout
Line
Count
Source
36
282
static zend_always_inline void fuzzer_bailout(void) {
37
282
  bailed_out = true;
38
282
  zend_bailout();
39
282
}
40
41
10.5M
static zend_always_inline void fuzzer_step(void) {
42
10.5M
  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
3.31k
    steps_left = MAX_STEPS;
46
3.31k
    fuzzer_bailout();
47
3.31k
  }
48
10.5M
}
fuzzer-tracing-jit.c:fuzzer_step
Line
Count
Source
41
4.57M
static zend_always_inline void fuzzer_step(void) {
42
4.57M
  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.24k
    steps_left = MAX_STEPS;
46
1.24k
    fuzzer_bailout();
47
1.24k
  }
48
4.57M
}
fuzzer-function-jit.c:fuzzer_step
Line
Count
Source
41
5.64M
static zend_always_inline void fuzzer_step(void) {
42
5.64M
  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.81k
    steps_left = MAX_STEPS;
46
1.81k
    fuzzer_bailout();
47
1.81k
  }
48
5.64M
}
fuzzer-execute.c:fuzzer_step
Line
Count
Source
41
368k
static zend_always_inline void fuzzer_step(void) {
42
368k
  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
259
    steps_left = MAX_STEPS;
46
259
    fuzzer_bailout();
47
259
  }
48
368k
}
49
50
static void (*orig_execute_ex)(zend_execute_data *execute_data);
51
52
263k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
263k
#ifdef ZEND_CHECK_STACK_LIMIT
55
263k
  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
263k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
263k
  const zend_op *opline = EX(opline);
64
65
9.70M
  while (1) {
66
9.56M
    fuzzer_step();
67
9.56M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
9.56M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
127k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
127k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
127k
      } else {
73
127k
        return;
74
127k
      }
75
127k
    }
76
9.56M
  }
77
263k
}
fuzzer-tracing-jit.c:fuzzer_execute_ex
Line
Count
Source
52
93.9k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
93.9k
#ifdef ZEND_CHECK_STACK_LIMIT
55
93.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
93.9k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
93.9k
  const zend_op *opline = EX(opline);
64
65
4.15M
  while (1) {
66
4.09M
    fuzzer_step();
67
4.09M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
4.09M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
34.7k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
34.7k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
34.7k
      } else {
73
34.7k
        return;
74
34.7k
      }
75
34.7k
    }
76
4.09M
  }
77
93.9k
}
fuzzer-function-jit.c:fuzzer_execute_ex
Line
Count
Source
52
111k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
111k
#ifdef ZEND_CHECK_STACK_LIMIT
55
111k
  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
111k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
111k
  const zend_op *opline = EX(opline);
64
65
5.20M
  while (1) {
66
5.13M
    fuzzer_step();
67
5.13M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
5.13M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
42.4k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
42.4k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
42.4k
      } else {
73
42.4k
        return;
74
42.4k
      }
75
42.4k
    }
76
5.13M
  }
77
111k
}
fuzzer-execute.c:fuzzer_execute_ex
Line
Count
Source
52
58.1k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
58.1k
#ifdef ZEND_CHECK_STACK_LIMIT
55
58.1k
  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
58.1k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
58.1k
  const zend_op *opline = EX(opline);
64
65
346k
  while (1) {
66
339k
    fuzzer_step();
67
339k
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
339k
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
50.5k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
50.5k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
50.5k
      } else {
73
50.5k
        return;
74
50.5k
      }
75
50.5k
    }
76
339k
  }
77
58.1k
}
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
4.19k
    zend_string *str, const char *filename, zend_compile_position position) {
84
4.19k
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
7
    fuzzer_bailout();
87
7
  }
88
89
4.19k
  return orig_compile_string(str, filename, position);
90
4.19k
}
fuzzer-tracing-jit.c:fuzzer_compile_string
Line
Count
Source
83
2.27k
    zend_string *str, const char *filename, zend_compile_position position) {
84
2.27k
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
3
    fuzzer_bailout();
87
3
  }
88
89
2.27k
  return orig_compile_string(str, filename, position);
90
2.27k
}
fuzzer-function-jit.c:fuzzer_compile_string
Line
Count
Source
83
1.87k
    zend_string *str, const char *filename, zend_compile_position position) {
84
1.87k
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
4
    fuzzer_bailout();
87
4
  }
88
89
1.87k
  return orig_compile_string(str, filename, position);
90
1.87k
}
fuzzer-execute.c:fuzzer_compile_string
Line
Count
Source
83
41
    zend_string *str, const char *filename, zend_compile_position position) {
84
41
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
0
    fuzzer_bailout();
87
0
  }
88
89
41
  return orig_compile_string(str, filename, position);
90
41
}
91
92
static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);
93
94
1.02M
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
1.02M
  fuzzer_step();
96
97
1.02M
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
2.32M
  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.30M
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
1.30M
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
9.82k
      fuzzer_bailout();
104
9.82k
    }
105
1.30M
  }
106
107
1.02M
  orig_execute_internal(execute_data, return_value);
108
1.02M
}
fuzzer-tracing-jit.c:fuzzer_execute_internal
Line
Count
Source
94
478k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
478k
  fuzzer_step();
96
97
478k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
1.10M
  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
626k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
626k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
4.48k
      fuzzer_bailout();
104
4.48k
    }
105
626k
  }
106
107
478k
  orig_execute_internal(execute_data, return_value);
108
478k
}
fuzzer-function-jit.c:fuzzer_execute_internal
Line
Count
Source
94
514k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
514k
  fuzzer_step();
96
97
514k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
1.16M
  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
648k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
648k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
5.31k
      fuzzer_bailout();
104
5.31k
    }
105
648k
  }
106
107
514k
  orig_execute_internal(execute_data, return_value);
108
514k
}
fuzzer-execute.c:fuzzer_execute_internal
Line
Count
Source
94
29.2k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
29.2k
  fuzzer_step();
96
97
29.2k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
56.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
27.0k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
27.0k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
23
      fuzzer_bailout();
104
23
    }
105
27.0k
  }
106
107
29.2k
  orig_execute_internal(execute_data, return_value);
108
29.2k
}
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
79.0k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
79.0k
  steps_left = MAX_STEPS;
137
79.0k
  zend_exception_save();
138
79.0k
  zval retval, args[2];
139
79.0k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
140
79.0k
  ZEND_ASSERT(fn != NULL);
141
142
79.0k
  ZVAL_STRING(&args[0], FILE_NAME);
143
79.0k
  ZVAL_TRUE(&args[1]);
144
79.0k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
145
79.0k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
146
79.0k
  zval_ptr_dtor(&args[0]);
147
79.0k
  zval_ptr_dtor(&retval);
148
79.0k
  zend_exception_restore();
149
79.0k
}
fuzzer-tracing-jit.c:opcache_invalidate
Line
Count
Source
135
40.4k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
40.4k
  steps_left = MAX_STEPS;
137
40.4k
  zend_exception_save();
138
40.4k
  zval retval, args[2];
139
40.4k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
140
40.4k
  ZEND_ASSERT(fn != NULL);
141
142
40.4k
  ZVAL_STRING(&args[0], FILE_NAME);
143
40.4k
  ZVAL_TRUE(&args[1]);
144
40.4k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
145
40.4k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
146
40.4k
  zval_ptr_dtor(&args[0]);
147
40.4k
  zval_ptr_dtor(&retval);
148
40.4k
  zend_exception_restore();
149
40.4k
}
fuzzer-function-jit.c:opcache_invalidate
Line
Count
Source
135
38.6k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
38.6k
  steps_left = MAX_STEPS;
137
38.6k
  zend_exception_save();
138
38.6k
  zval retval, args[2];
139
38.6k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
140
38.6k
  ZEND_ASSERT(fn != NULL);
141
142
38.6k
  ZVAL_STRING(&args[0], FILE_NAME);
143
38.6k
  ZVAL_TRUE(&args[1]);
144
38.6k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
145
38.6k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
146
38.6k
  zval_ptr_dtor(&args[0]);
147
38.6k
  zval_ptr_dtor(&retval);
148
38.6k
  zend_exception_restore();
149
38.6k
}
Unexecuted instantiation: fuzzer-execute.c:opcache_invalidate