Coverage Report

Created: 2025-12-31 07:28

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
126k
#define FILE_NAME "/tmp/fuzzer.php"
29
137k
#define MAX_STEPS 1000
30
419k
#define MAX_SIZE (8 * 1024)
31
4.23M
#define ZEND_VM_ENTER_BIT 1ULL
32
33
static uint32_t steps_left;
34
static bool bailed_out = false;
35
36
5.81k
static zend_always_inline void fuzzer_bailout(void) {
37
5.81k
  bailed_out = true;
38
5.81k
  zend_bailout();
39
5.81k
}
fuzzer-tracing-jit.c:fuzzer_bailout
Line
Count
Source
36
5.51k
static zend_always_inline void fuzzer_bailout(void) {
37
5.51k
  bailed_out = true;
38
5.51k
  zend_bailout();
39
5.51k
}
Unexecuted instantiation: fuzzer-function-jit.c:fuzzer_bailout
fuzzer-execute.c:fuzzer_bailout
Line
Count
Source
36
300
static zend_always_inline void fuzzer_bailout(void) {
37
300
  bailed_out = true;
38
300
  zend_bailout();
39
300
}
40
41
4.61M
static zend_always_inline void fuzzer_step(void) {
42
4.61M
  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.31k
    steps_left = MAX_STEPS;
46
1.31k
    fuzzer_bailout();
47
1.31k
  }
48
4.61M
}
fuzzer-tracing-jit.c:fuzzer_step
Line
Count
Source
41
4.22M
static zend_always_inline void fuzzer_step(void) {
42
4.22M
  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.03k
    steps_left = MAX_STEPS;
46
1.03k
    fuzzer_bailout();
47
1.03k
  }
48
4.22M
}
Unexecuted instantiation: fuzzer-function-jit.c:fuzzer_step
fuzzer-execute.c:fuzzer_step
Line
Count
Source
41
383k
static zend_always_inline void fuzzer_step(void) {
42
383k
  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
276
    steps_left = MAX_STEPS;
46
276
    fuzzer_bailout();
47
276
  }
48
383k
}
49
50
static void (*orig_execute_ex)(zend_execute_data *execute_data);
51
52
127k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
127k
#ifdef ZEND_CHECK_STACK_LIMIT
55
127k
  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
127k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
127k
  const zend_op *opline = EX(opline);
64
65
4.25M
  while (1) {
66
4.18M
    fuzzer_step();
67
4.18M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
4.18M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
54.1k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
54.1k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
54.1k
      } else {
73
54.1k
        return;
74
54.1k
      }
75
54.1k
    }
76
4.18M
  }
77
127k
}
fuzzer-tracing-jit.c:fuzzer_execute_ex
Line
Count
Source
52
73.2k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
73.2k
#ifdef ZEND_CHECK_STACK_LIMIT
55
73.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
73.2k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
73.2k
  const zend_op *opline = EX(opline);
64
65
3.89M
  while (1) {
66
3.82M
    fuzzer_step();
67
3.82M
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
3.82M
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
8.00k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
8.00k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
8.00k
      } else {
73
8.00k
        return;
74
8.00k
      }
75
8.00k
    }
76
3.82M
  }
77
73.2k
}
Unexecuted instantiation: fuzzer-function-jit.c:fuzzer_execute_ex
fuzzer-execute.c:fuzzer_execute_ex
Line
Count
Source
52
53.7k
static void fuzzer_execute_ex(zend_execute_data *execute_data) {
53
54
53.7k
#ifdef ZEND_CHECK_STACK_LIMIT
55
53.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
53.7k
#endif /* ZEND_CHECK_STACK_LIMIT */
62
63
53.7k
  const zend_op *opline = EX(opline);
64
65
360k
  while (1) {
66
352k
    fuzzer_step();
67
352k
    opline = ((zend_vm_opcode_handler_func_t) zend_get_opcode_handler_func(opline))(execute_data, opline);
68
352k
    if ((uintptr_t) opline & ZEND_VM_ENTER_BIT) {
69
46.1k
      opline = (const zend_op *) ((uintptr_t) opline & ~ZEND_VM_ENTER_BIT);
70
46.1k
      if (opline) {
71
0
        execute_data = EG(current_execute_data);
72
46.1k
      } else {
73
46.1k
        return;
74
46.1k
      }
75
46.1k
    }
76
352k
  }
77
53.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
445
    zend_string *str, const char *filename, zend_compile_position position) {
84
445
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
0
    fuzzer_bailout();
87
0
  }
88
89
445
  return orig_compile_string(str, filename, position);
90
445
}
fuzzer-tracing-jit.c:fuzzer_compile_string
Line
Count
Source
83
397
    zend_string *str, const char *filename, zend_compile_position position) {
84
397
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
0
    fuzzer_bailout();
87
0
  }
88
89
397
  return orig_compile_string(str, filename, position);
90
397
}
Unexecuted instantiation: fuzzer-function-jit.c:fuzzer_compile_string
fuzzer-execute.c:fuzzer_compile_string
Line
Count
Source
83
48
    zend_string *str, const char *filename, zend_compile_position position) {
84
48
  if (ZSTR_LEN(str) > MAX_SIZE) {
85
    /* Avoid compiling huge inputs via eval(). */
86
0
    fuzzer_bailout();
87
0
  }
88
89
48
  return orig_compile_string(str, filename, position);
90
48
}
91
92
static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);
93
94
431k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
431k
  fuzzer_step();
96
97
431k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
993k
  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
561k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
561k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
4.49k
      fuzzer_bailout();
104
4.49k
    }
105
561k
  }
106
107
431k
  orig_execute_internal(execute_data, return_value);
108
431k
}
fuzzer-tracing-jit.c:fuzzer_execute_internal
Line
Count
Source
94
400k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
400k
  fuzzer_step();
96
97
400k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
934k
  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
533k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
533k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
4.47k
      fuzzer_bailout();
104
4.47k
    }
105
533k
  }
106
107
400k
  orig_execute_internal(execute_data, return_value);
108
400k
}
Unexecuted instantiation: fuzzer-function-jit.c:fuzzer_execute_internal
fuzzer-execute.c:fuzzer_execute_internal
Line
Count
Source
94
30.6k
static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
95
30.6k
  fuzzer_step();
96
97
30.6k
  uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
98
59.1k
  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
28.4k
    zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
102
28.4k
    if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
103
24
      fuzzer_bailout();
104
24
    }
105
28.4k
  }
106
107
30.6k
  orig_execute_internal(execute_data, return_value);
108
30.6k
}
109
110
4
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
4
  putenv("USE_TRACKED_ALLOC=1");
114
115
  /* Just like other SAPIs, ignore SIGPIPEs. */
116
4
  signal(SIGPIPE, SIG_IGN);
117
118
4
  fuzzer_init_php(extra_ini);
119
120
4
  orig_execute_ex = zend_execute_ex;
121
4
  zend_execute_ex = fuzzer_execute_ex;
122
4
  orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
123
4
  zend_execute_internal = fuzzer_execute_internal;
124
4
  orig_compile_string = zend_compile_string;
125
4
  zend_compile_string = fuzzer_compile_string;
126
4
}
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
}
Unexecuted instantiation: fuzzer-function-jit.c:fuzzer_init_php_for_execute
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
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-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
}
Unexecuted instantiation: fuzzer-function-jit.c:create_file
Unexecuted instantiation: fuzzer-execute.c:create_file
134
135
13.2k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
13.2k
  steps_left = MAX_STEPS;
137
13.2k
  zend_exception_save();
138
13.2k
  zval retval, args[2];
139
13.2k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
140
13.2k
  ZEND_ASSERT(fn != NULL);
141
142
13.2k
  ZVAL_STRING(&args[0], FILE_NAME);
143
13.2k
  ZVAL_TRUE(&args[1]);
144
13.2k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
145
13.2k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
146
13.2k
  zval_ptr_dtor(&args[0]);
147
13.2k
  zval_ptr_dtor(&retval);
148
13.2k
  zend_exception_restore();
149
13.2k
}
fuzzer-tracing-jit.c:opcache_invalidate
Line
Count
Source
135
13.2k
ZEND_ATTRIBUTE_UNUSED static void opcache_invalidate(void) {
136
13.2k
  steps_left = MAX_STEPS;
137
13.2k
  zend_exception_save();
138
13.2k
  zval retval, args[2];
139
13.2k
  zend_function *fn = zend_hash_str_find_ptr(CG(function_table), ZEND_STRL("opcache_invalidate"));
140
13.2k
  ZEND_ASSERT(fn != NULL);
141
142
13.2k
  ZVAL_STRING(&args[0], FILE_NAME);
143
13.2k
  ZVAL_TRUE(&args[1]);
144
13.2k
  zend_call_known_function(fn, NULL, NULL, &retval, 2, args, NULL);
145
13.2k
  ZEND_ASSERT(Z_TYPE(retval) == IS_TRUE);
146
13.2k
  zval_ptr_dtor(&args[0]);
147
13.2k
  zval_ptr_dtor(&retval);
148
13.2k
  zend_exception_restore();
149
13.2k
}
Unexecuted instantiation: fuzzer-function-jit.c:opcache_invalidate
Unexecuted instantiation: fuzzer-execute.c:opcache_invalidate