123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- #include <main/php.h>
- #include "fuzzer.h"
- #include "fuzzer-sapi.h"
- #define MAX_STEPS 1000
- #define MAX_SIZE (8 * 1024)
- static uint32_t steps_left;
- typedef int (ZEND_FASTCALL *opcode_handler_t)(zend_execute_data *);
- static zend_always_inline void fuzzer_step(void) {
- if (--steps_left == 0) {
-
- steps_left = MAX_STEPS;
- zend_bailout();
- }
- }
- static void fuzzer_execute_ex(zend_execute_data *execute_data) {
- while (1) {
- int ret;
- fuzzer_step();
- if ((ret = ((opcode_handler_t) EX(opline)->handler)(execute_data)) != 0) {
- if (ret > 0) {
- execute_data = EG(current_execute_data);
- } else {
- return;
- }
- }
- }
- }
- static zend_op_array *(*orig_compile_string)(zend_string *source_string, const char *filename);
- static zend_op_array *fuzzer_compile_string(zend_string *str, const char *filename) {
- if (ZSTR_LEN(str) > MAX_SIZE) {
-
- zend_bailout();
- }
- return orig_compile_string(str, filename);
- }
- static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);
- static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
- fuzzer_step();
- uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
- for (uint32_t i = 0; i < num_args; i++) {
-
- zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
- if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
- zend_bailout();
- }
- }
- orig_execute_internal(execute_data, return_value);
- }
- int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
- if (Size > MAX_SIZE) {
-
- return 0;
- }
- steps_left = MAX_STEPS;
- fuzzer_do_request_from_buffer("/fuzzer.php", (const char *) Data, Size, 1);
- return 0;
- }
- int LLVMFuzzerInitialize(int *argc, char ***argv) {
-
- putenv("USE_TRACKED_ALLOC=1");
-
- signal(SIGPIPE, SIG_IGN);
- fuzzer_init_php();
- zend_execute_ex = fuzzer_execute_ex;
- orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
- zend_execute_internal = fuzzer_execute_internal;
- orig_compile_string = zend_compile_string;
- zend_compile_string = fuzzer_compile_string;
-
- return 0;
- }
|