dcd_fuzz.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2022 Nathaniel Brough
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. #include "device/dcd.h"
  26. #include "fuzz/fuzz_private.h"
  27. #include <assert.h>
  28. #include <cstdint>
  29. #include <limits>
  30. #define UNUSED(x) (void)(x)
  31. //--------------------------------------------------------------------+
  32. // State tracker
  33. //--------------------------------------------------------------------+
  34. struct State {
  35. bool interrupts_enabled;
  36. bool sof_enabled;
  37. uint8_t address;
  38. };
  39. static State state = {false, 0, 0};
  40. //--------------------------------------------------------------------+
  41. // Controller API
  42. // All no-ops as we are fuzzing.
  43. //--------------------------------------------------------------------+
  44. extern "C" {
  45. void dcd_init(uint8_t rhport) {
  46. UNUSED(rhport);
  47. return;
  48. }
  49. void dcd_int_handler(uint8_t rhport) {
  50. assert(_fuzz_data_provider.has_value());
  51. if (!state.interrupts_enabled) {
  52. return;
  53. }
  54. // Choose if we want to generate a signal based on the fuzzed data.
  55. if (_fuzz_data_provider->ConsumeBool()) {
  56. dcd_event_bus_signal(
  57. rhport,
  58. // Choose a random event based on the fuzz data.
  59. (dcd_eventid_t)_fuzz_data_provider->ConsumeIntegralInRange<uint8_t>(
  60. DCD_EVENT_INVALID + 1, DCD_EVENT_COUNT - 1),
  61. // Identify trigger as either an interrupt or a syncrhonous call
  62. // depending on fuzz data.
  63. _fuzz_data_provider->ConsumeBool());
  64. }
  65. if (_fuzz_data_provider->ConsumeBool()) {
  66. constexpr size_t kSetupFrameLength = 8;
  67. std::vector<uint8_t> setup =
  68. _fuzz_data_provider->ConsumeBytes<uint8_t>(kSetupFrameLength);
  69. // Fuzz consumer may return less than requested. If this is the case
  70. // we want to make sure that at least that length is allocated and available
  71. // to the signal handler.
  72. if (setup.size() != kSetupFrameLength) {
  73. setup.resize(kSetupFrameLength);
  74. }
  75. dcd_event_setup_received(rhport, setup.data(),
  76. // Identify trigger as either an interrupt or a
  77. // syncrhonous call depending on fuzz data.
  78. _fuzz_data_provider->ConsumeBool());
  79. }
  80. }
  81. void dcd_int_enable(uint8_t rhport) {
  82. state.interrupts_enabled = true;
  83. UNUSED(rhport);
  84. return;
  85. }
  86. void dcd_int_disable(uint8_t rhport) {
  87. state.interrupts_enabled = false;
  88. UNUSED(rhport);
  89. return;
  90. }
  91. void dcd_set_address(uint8_t rhport, uint8_t dev_addr) {
  92. UNUSED(rhport);
  93. state.address = dev_addr;
  94. // Respond with status.
  95. dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
  96. return;
  97. }
  98. void dcd_remote_wakeup(uint8_t rhport) {
  99. UNUSED(rhport);
  100. return;
  101. }
  102. void dcd_connect(uint8_t rhport) {
  103. UNUSED(rhport);
  104. return;
  105. }
  106. void dcd_disconnect(uint8_t rhport) {
  107. UNUSED(rhport);
  108. return;
  109. }
  110. void dcd_sof_enable(uint8_t rhport, bool en) {
  111. state.sof_enabled = en;
  112. UNUSED(rhport);
  113. return;
  114. }
  115. //--------------------------------------------------------------------+
  116. // Endpoint API
  117. //--------------------------------------------------------------------+
  118. // Configure endpoint's registers according to descriptor
  119. bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *desc_ep) {
  120. UNUSED(rhport);
  121. UNUSED(desc_ep);
  122. return _fuzz_data_provider->ConsumeBool();
  123. }
  124. // Close all non-control endpoints, cancel all pending transfers if any.
  125. // Invoked when switching from a non-zero Configuration by SET_CONFIGURE
  126. // therefore required for multiple configuration support.
  127. void dcd_edpt_close_all(uint8_t rhport) {
  128. UNUSED(rhport);
  129. return;
  130. }
  131. // Close an endpoint.
  132. // Since it is weak, caller must TU_ASSERT this function's existence before
  133. // calling it.
  134. void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr) {
  135. UNUSED(rhport);
  136. UNUSED(ep_addr);
  137. return;
  138. }
  139. // Submit a transfer, When complete dcd_event_xfer_complete() is invoked to
  140. // notify the stack
  141. bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer,
  142. uint16_t total_bytes) {
  143. UNUSED(rhport);
  144. UNUSED(buffer);
  145. UNUSED(total_bytes);
  146. uint8_t const dir = tu_edpt_dir(ep_addr);
  147. if (dir == TUSB_DIR_IN) {
  148. std::vector<uint8_t> temp =
  149. _fuzz_data_provider->ConsumeBytes<uint8_t>(total_bytes);
  150. std::copy(temp.begin(), temp.end(), buffer);
  151. }
  152. // Ignore output data as it's not useful for fuzzing without a more
  153. // complex fuzzed backend. But we need to make sure it's not
  154. // optimised out.
  155. volatile uint8_t *dont_optimise0 = buffer;
  156. volatile uint16_t dont_optimise1 = total_bytes;
  157. UNUSED(dont_optimise0);
  158. UNUSED(dont_optimise1);
  159. return _fuzz_data_provider->ConsumeBool();
  160. }
  161. /* TODO: implement a fuzzed version of this.
  162. bool dcd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t *ff,
  163. uint16_t total_bytes) {}
  164. */
  165. // Stall endpoint, any queuing transfer should be removed from endpoint
  166. void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) {
  167. UNUSED(rhport);
  168. UNUSED(ep_addr);
  169. return;
  170. }
  171. // clear stall, data toggle is also reset to DATA0
  172. // This API never calls with control endpoints, since it is auto cleared when
  173. // receiving setup packet
  174. void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) {
  175. UNUSED(rhport);
  176. UNUSED(ep_addr);
  177. return;
  178. }
  179. }