Coverage Report

Created: 2026-02-09 07:07

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
209k
#define FILE_NAME "/tmp/fuzzer.php"
29
248k
#define MAX_STEPS 1000
30
839k
#define MAX_SIZE (8 * 1024)
31
9.93M
#define ZEND_VM_ENTER_BIT 1ULL
32
33
static uint32_t steps_left;
34
static bool bailed_out = false;
35
36
13.7k
static zend_always_inline void fuzzer_bailout(void) {
37
13.7k
  bailed_out = true;
38
13.7k
  zend_bailout();
39
13.7k
}
fuzzer-tracing-jit.c:fuzzer_bailout
Line
Count
Source
36
5.92k
static zend_always_inline void fuzzer_bailout(void) {
37
5.92k
  bailed_out = true;
38
5.92k
  zend_bailout();
39
5.92k
}
fuzzer-function-jit.c:fuzzer_bailout
Line
Count
Source
36
7.56k
static zend_always_inline void fuzzer_bailout(void) {
37
7.56k
  bailed_out = true;
38
7.56k
  zend_bailout();
39
7.56k
}
fuzzer-execute.c:fuzzer_bailout
Line
Count
Source
36
278
static zend_always_inline void fuzzer_bailout(void) {
37
278
  bailed_out = true;
38
278
  zend_bailout();
39
278
}
40
41
10.8M
static zend_always_inline void fuzzer_step(void) {
42
10.8M
  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.53k
    steps_left = MAX_STEPS;
46
3.53k
    fuzzer_bailout();
47
3.53k
  }
48
10.8M
}
fuzzer-tracing-jit.c:fuzzer_step
Line
Count
Source
41
4.75M
static zend_always_inline void fuzzer_step(void) {
42
4.75M
  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.36k
    steps_left = MAX_STEPS;
46
1.36k
    fuzzer_bailout();
47
1.36k
  }
48
4.75M
}
fuzzer-function-jit.c:fuzzer_step
Line
Count
Source
41
5.70M
static zend_always_inline void fuzzer_step(void) {
42
5.70M
  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.90k
    steps_left = MAX_STEPS;
46
1.90k
    fuzzer_bailout();
47
1.90k
  }
48
5.70M
}
fuzzer-execute.c:fuzzer_step
Line
Count
Source
41
359k
static zend_always_inline void fuzzer_step(void) {
42
359k
  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
261
    steps_left = MAX_STEPS;
46
261
    fuzzer_bailout();
47
261
  }
48
359k
}
49
50
static void (*orig_execute_ex)(zend_execute_data *execute_data);
51
52
258k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
258k
#ifdef ZEND_CHECK_STACK_LIMIT
55
258k
  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
258k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
258k
  const zend_op *opline = EX(opline);
64
65
9.96M
  while (1) {
66
9.81M
    fuzzer_step();
67
9.81M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
9.81M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
113k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
113k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
113k
      } else {
73
113k
        return;
74
113k
      }
75
113k
    }
76
9.81M
  }
77
258k
}
fuzzer-tracing-jit.c:fuzzer_execute_ex
Line
Count
Source
52
103k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
103k
#ifdef ZEND_CHECK_STACK_LIMIT
55
103k
  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
103k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
103k
  const zend_op *opline = EX(opline);
64
65
4.34M
  while (1) {
66
4.27M
    fuzzer_step();
67
4.27M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
4.27M
    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.27M
  }
77
103k
}
fuzzer-function-jit.c:fuzzer_execute_ex
Line
Count
Source
52
106k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
106k
#ifdef ZEND_CHECK_STACK_LIMIT
55
106k
  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
106k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
106k
  const zend_op *opline = EX(opline);
64
65
5.27M
  while (1) {
66
5.21M
    fuzzer_step();
67
5.21M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
5.21M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
38.4k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
38.4k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
38.4k
      } else {
73
38.4k
        return;
74
38.4k
      }
75
38.4k
    }
76
5.21M
  }
77
106k
}
fuzzer-execute.c:fuzzer_execute_ex
Line
Count
Source
52
48.7k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
48.7k
#ifdef ZEND_CHECK_STACK_LIMIT
55
48.7k
  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
48.7k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
48.7k
  const zend_op *opline = EX(opline);
64
65
339k
  while (1) {
66
331k
    fuzzer_step();
67
331k
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
331k
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
40.7k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
40.7k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
40.7k
      } else {
73
40.7k
        return;
74
40.7k
      }
75
40.7k
    }
76
331k
  }
77
48.7k
}
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.23k
    zend_string *str, const char *filename, zend_compile_position position) {
84
4.23k
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
5
    fuzzer_bailout();
87
5
  }
88
89
4.23k
  return orig_compile_string(str, filename, position);
90
4.23k
}
fuzzer-tracing-jit.c:fuzzer_compile_string
Line
Count
Source
83
2.33k
    zend_string *str, const char *filename, zend_compile_position position) {
84
2.33k
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
2
    fuzzer_bailout();
87
2
  }
88
89
2.33k
  return orig_compile_string(str, filename, position);
90
2.33k
}
fuzzer-function-jit.c:fuzzer_compile_string
Line
Count
Source
83
1.85k
    zend_string *str, const char *filename, zend_compile_position position) {
84
1.85k
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
3
    fuzzer_bailout();
87
3
  }
88
89
1.85k
  return orig_compile_string(str, filename, position);
90
1.85k
}
fuzzer-execute.c:fuzzer_compile_string
Line
Count
Source
83
43
    zend_string *str, const char *filename, zend_compile_position position) {
84
43
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
0
    fuzzer_bailout();
87
0
  }
88
89
43
  return orig_compile_string(str, filename, position);
90
43
}
91
92
static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);
93
94
1.00M
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
1.00M
  fuzzer_step();
96
97
1.00M
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
2.25M
  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.25M
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
1.25M
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
10.2k
      fuzzer_bailout();
104
10.2k
    }
105
1.25M
  }
106
107
1.00M
  orig_execute_internal(execute_data, return_value);
108
1.00M
}
fuzzer-tracing-jit.c:fuzzer_execute_internal
Line
Count
Source
94
481k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
481k
  fuzzer_step();
96
97
481k
  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
621k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
621k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
4.55k
      fuzzer_bailout();
104
4.55k
    }
105
621k
  }
106
107
481k
  orig_execute_internal(execute_data, return_value);
108
481k
}
fuzzer-function-jit.c:fuzzer_execute_internal
Line
Count
Source
94
491k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
491k
  fuzzer_step();
96
97
491k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
1.09M
  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
605k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
605k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
5.65k
      fuzzer_bailout();
104
5.65k
    }
105
605k
  }
106
107
491k
  orig_execute_internal(execute_data, return_value);
108
491k
}
fuzzer-execute.c:fuzzer_execute_internal
Line
Count
Source
94
28.8k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
28.8k
  fuzzer_step();
96
97
28.8k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
55.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
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
17
      fuzzer_bailout();
104
17
    }
105
27.0k
  }
106
107
28.8k
  orig_execute_internal(execute_data, return_value);
108
28.8k
}
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
56.5k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
56.5k
  steps_left = MAX_STEPS;
137
56.5k
  zend_object *exception = EG(exception);
138
56.5k
  EG(exception) = NULL;
139
56.5k
  zval retval, args[2];
140
56.5k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
141
56.5k
  ZEND_ASSERT(fn != NULL);
142
143
56.5k
  ZVAL_STRING(&args[0], FILE_NAME);
144
56.5k
  ZVAL_TRUE(&args[1]);
145
56.5k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
146
56.5k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
147
56.5k
  zval_ptr_dtor(&args[0]);
148
56.5k
  zval_ptr_dtor(&retval);
149
56.5k
  EG(exception) = exception;
150
56.5k
}
fuzzer-tracing-jit.c:opcache_invalidate
Line
Count
Source
135
31.3k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
31.3k
  steps_left = MAX_STEPS;
137
31.3k
  zend_object *exception = EG(exception);
138
31.3k
  EG(exception) = NULL;
139
31.3k
  zval retval, args[2];
140
31.3k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
141
31.3k
  ZEND_ASSERT(fn != NULL);
142
143
31.3k
  ZVAL_STRING(&args[0], FILE_NAME);
144
31.3k
  ZVAL_TRUE(&args[1]);
145
31.3k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
146
31.3k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
147
31.3k
  zval_ptr_dtor(&args[0]);
148
31.3k
  zval_ptr_dtor(&retval);
149
31.3k
  EG(exception) = exception;
150
31.3k
}
fuzzer-function-jit.c:opcache_invalidate
Line
Count
Source
135
25.1k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
25.1k
  steps_left = MAX_STEPS;
137
25.1k
  zend_object *exception = EG(exception);
138
25.1k
  EG(exception) = NULL;
139
25.1k
  zval retval, args[2];
140
25.1k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
141
25.1k
  ZEND_ASSERT(fn != NULL);
142
143
25.1k
  ZVAL_STRING(&args[0], FILE_NAME);
144
25.1k
  ZVAL_TRUE(&args[1]);
145
25.1k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
146
25.1k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
147
25.1k
  zval_ptr_dtor(&args[0]);
148
25.1k
  zval_ptr_dtor(&retval);
149
25.1k
  EG(exception) = exception;
150
25.1k
}
Unexecuted instantiation: fuzzer-execute.c:opcache_invalidate