fuzzer-execute.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #include <main/php.h>
  17. #include "fuzzer.h"
  18. #include "fuzzer-sapi.h"
  19. #define MAX_STEPS 1000
  20. #define MAX_SIZE (8 * 1024)
  21. static uint32_t steps_left;
  22. /* Because the fuzzer is always compiled with clang,
  23. * we can assume that we don't use global registers / hybrid VM. */
  24. typedef int (ZEND_FASTCALL *opcode_handler_t)(zend_execute_data *);
  25. static zend_always_inline void fuzzer_step(void) {
  26. if (--steps_left == 0) {
  27. /* Reset steps before bailing out, so code running after bailout (e.g. in
  28. * destructors) will get another MAX_STEPS, rather than UINT32_MAX steps. */
  29. steps_left = MAX_STEPS;
  30. zend_bailout();
  31. }
  32. }
  33. static void fuzzer_execute_ex(zend_execute_data *execute_data) {
  34. while (1) {
  35. int ret;
  36. fuzzer_step();
  37. if ((ret = ((opcode_handler_t) EX(opline)->handler)(execute_data)) != 0) {
  38. if (ret > 0) {
  39. execute_data = EG(current_execute_data);
  40. } else {
  41. return;
  42. }
  43. }
  44. }
  45. }
  46. static zend_op_array *(*orig_compile_string)(zend_string *source_string, const char *filename);
  47. static zend_op_array *fuzzer_compile_string(zend_string *str, const char *filename) {
  48. if (ZSTR_LEN(str) > MAX_SIZE) {
  49. /* Avoid compiling huge inputs via eval(). */
  50. zend_bailout();
  51. }
  52. return orig_compile_string(str, filename);
  53. }
  54. static void (*orig_execute_internal)(zend_execute_data *execute_data, zval *return_value);
  55. static void fuzzer_execute_internal(zend_execute_data *execute_data, zval *return_value) {
  56. fuzzer_step();
  57. uint32_t num_args = ZEND_CALL_NUM_ARGS(execute_data);
  58. for (uint32_t i = 0; i < num_args; i++) {
  59. /* Some internal functions like preg_replace() may be slow on large inputs.
  60. * Limit the maximum size of string inputs. */
  61. zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
  62. if (Z_TYPE_P(arg) == IS_STRING && Z_STRLEN_P(arg) > MAX_SIZE) {
  63. zend_bailout();
  64. }
  65. }
  66. orig_execute_internal(execute_data, return_value);
  67. }
  68. int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
  69. if (Size > MAX_SIZE) {
  70. /* Large inputs have a large impact on fuzzer performance,
  71. * but are unlikely to be necessary to reach new codepaths. */
  72. return 0;
  73. }
  74. steps_left = MAX_STEPS;
  75. fuzzer_do_request_from_buffer("/fuzzer.php", (const char *) Data, Size, /* execute */ 1);
  76. return 0;
  77. }
  78. int LLVMFuzzerInitialize(int *argc, char ***argv) {
  79. /* Compilation will often trigger fatal errors.
  80. * Use tracked allocation mode to avoid leaks in that case. */
  81. putenv("USE_TRACKED_ALLOC=1");
  82. /* Just like other SAPIs, ignore SIGPIPEs. */
  83. signal(SIGPIPE, SIG_IGN);
  84. fuzzer_init_php();
  85. zend_execute_ex = fuzzer_execute_ex;
  86. orig_execute_internal = zend_execute_internal ? zend_execute_internal : execute_internal;
  87. zend_execute_internal = fuzzer_execute_internal;
  88. orig_compile_string = zend_compile_string;
  89. zend_compile_string = fuzzer_compile_string;
  90. /* fuzzer_shutdown_php(); */
  91. return 0;
  92. }