fuzzer.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef TOOLS_IPC_FUZZER_MUTATE_FUZZER_H_
  5. #define TOOLS_IPC_FUZZER_MUTATE_FUZZER_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <memory>
  9. #include <string>
  10. #include <unordered_map>
  11. #include <vector>
  12. #include "base/strings/string_util.h"
  13. #include "ipc/ipc_message.h"
  14. namespace ipc_fuzzer {
  15. // Interface implemented by those who generate basic types. The types all
  16. // correspond to the types which a pickle from base/pickle.h can pickle,
  17. // plus the floating point types.
  18. class Fuzzer {
  19. public:
  20. // Functions for various data types.
  21. virtual void FuzzBool(bool* value) = 0;
  22. virtual void FuzzInt(int* value) = 0;
  23. virtual void FuzzLong(long* value) = 0;
  24. virtual void FuzzSize(size_t* value) = 0;
  25. virtual void FuzzUChar(unsigned char* value) = 0;
  26. virtual void FuzzWChar(wchar_t* value) = 0;
  27. virtual void FuzzUInt16(uint16_t* value) = 0;
  28. virtual void FuzzUInt32(uint32_t* value) = 0;
  29. virtual void FuzzInt64(int64_t* value) = 0;
  30. virtual void FuzzUInt64(uint64_t* value) = 0;
  31. virtual void FuzzFloat(float* value) = 0;
  32. virtual void FuzzDouble(double *value) = 0;
  33. virtual void FuzzString(std::string* value) = 0;
  34. virtual void FuzzString16(base::string16* value) = 0;
  35. virtual void FuzzData(char* data, int length) = 0;
  36. virtual void FuzzBytes(void* data, int data_len) = 0;
  37. // Used to determine if a completely new value should be generated for
  38. // certain types instead of attempting to modify the existing one.
  39. virtual bool ShouldGenerate();
  40. };
  41. class NoOpFuzzer : public Fuzzer {
  42. public:
  43. NoOpFuzzer() {}
  44. virtual ~NoOpFuzzer() {}
  45. void FuzzBool(bool* value) override {}
  46. void FuzzInt(int* value) override {}
  47. void FuzzLong(long* value) override {}
  48. void FuzzSize(size_t* value) override {}
  49. void FuzzUChar(unsigned char* value) override {}
  50. void FuzzWChar(wchar_t* value) override {}
  51. void FuzzUInt16(uint16_t* value) override {}
  52. void FuzzUInt32(uint32_t* value) override {}
  53. void FuzzInt64(int64_t* value) override {}
  54. void FuzzUInt64(uint64_t* value) override {}
  55. void FuzzFloat(float* value) override {}
  56. void FuzzDouble(double* value) override {}
  57. void FuzzString(std::string* value) override {}
  58. void FuzzString16(base::string16* value) override {}
  59. void FuzzData(char* data, int length) override {}
  60. void FuzzBytes(void* data, int data_len) override {}
  61. };
  62. using FuzzerFunction = std::unique_ptr<IPC::Message> (*)(IPC::Message*,
  63. Fuzzer*);
  64. // Used for mutating messages. Once populated, the map associates a message ID
  65. // with a FuzzerFunction used for mutation of that message type.
  66. using FuzzerFunctionMap = std::unordered_map<uint32_t, FuzzerFunction>;
  67. void PopulateFuzzerFunctionMap(FuzzerFunctionMap* map);
  68. // Used for generating new messages. Once populated, the vector contains
  69. // FuzzerFunctions for all message types that we know how to generate.
  70. using FuzzerFunctionVector = std::vector<FuzzerFunction>;
  71. void PopulateFuzzerFunctionVector(FuzzerFunctionVector* function_vector);
  72. // Since IPC::Message can be serialized, we also track a global function vector
  73. // to handle generation of new messages while fuzzing.
  74. extern FuzzerFunctionVector g_function_vector;
  75. } // namespace ipc_fuzzer
  76. #endif // TOOLS_IPC_FUZZER_MUTATE_FUZZER_H_