tud_msc_inquiry_cb:
   28|     12|TU_ATTR_WEAK void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
   29|     12|  (void) lun; (void) vendor_id; (void) product_id; (void) product_rev;
   30|     12|}
tud_msc_inquiry2_cb:
   31|     12|TU_ATTR_WEAK uint32_t tud_msc_inquiry2_cb(uint8_t lun, scsi_inquiry_resp_t *inquiry_resp, uint32_t bufsize) {
   32|     12|  (void) lun; (void) inquiry_resp; (void) bufsize;
   33|     12|  return 0;
   34|     12|}
tud_msc_read10_complete_cb:
  214|     71|TU_ATTR_WEAK void tud_msc_read10_complete_cb(uint8_t lun) {
  215|     71|  (void) lun;
  216|     71|}
tud_msc_write10_complete_cb:
  218|     50|TU_ATTR_WEAK void tud_msc_write10_complete_cb(uint8_t lun) {
  219|     50|  (void) lun;
  220|     50|}
tud_msc_scsi_complete_cb:
  222|     75|TU_ATTR_WEAK void tud_msc_scsi_complete_cb(uint8_t lun, uint8_t const scsi_cmd[16]) {
  223|     75|  (void) lun;
  224|     75|  (void) scsi_cmd;
  225|     75|}
tud_msc_start_stop_cb:
  231|      4|TU_ATTR_WEAK bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
  232|      4|  (void) lun;
  233|      4|  (void) power_condition;
  234|      4|  (void) start;
  235|      4|  (void) load_eject;
  236|       |  return true;
  237|      4|}
tud_msc_prevent_allow_medium_removal_cb:
  239|      1|TU_ATTR_WEAK bool tud_msc_prevent_allow_medium_removal_cb(uint8_t lun, uint8_t prohibit_removal, uint8_t control) {
  240|      1|  (void) lun;
  241|      1|  (void) prohibit_removal;
  242|      1|  (void) control;
  243|       |  return true;
  244|      1|}
tud_msc_request_sense_cb:
  246|     28|TU_ATTR_WEAK int32_t tud_msc_request_sense_cb(uint8_t lun, void* buffer, uint16_t bufsize) {
  247|     28|  (void) lun;
  248|     28|  (void) buffer;
  249|     28|  (void) bufsize;
  250|     28|  return sizeof(scsi_sense_fixed_resp_t);
  251|     28|}
tud_msc_is_writable_cb:
  253|  3.16k|TU_ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun) {
  254|  3.16k|  (void) lun;
  255|       |  return true;
  256|  3.16k|}
tud_msc_set_sense:
  287|    221|bool tud_msc_set_sense(uint8_t lun, uint8_t sense_key, uint8_t add_sense_code, uint8_t add_sense_qualifier) {
  288|    221|  (void) lun;
  289|    221|  _mscd_itf.sense_key           = sense_key;
  290|    221|  _mscd_itf.add_sense_code      = add_sense_code;
  291|    221|  _mscd_itf.add_sense_qualifier = add_sense_qualifier;
  292|       |  return true;
  293|    221|}
mscd_init:
  338|    520|void mscd_init(void) {
  339|    520|  TU_LOG_INT(CFG_TUD_MSC_LOG_LEVEL, sizeof(mscd_interface_t));
  340|    520|  tu_memclr(&_mscd_itf, sizeof(mscd_interface_t));
  ------------------
  |  |   95|    520|#define tu_memclr(buffer, size)  (void) memset((buffer), 0, (size))
  ------------------
  341|    520|}
mscd_xfer_cb:
  651|  52.8k|bool mscd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) {
  652|  52.8k|  (void) event;
  653|  52.8k|  TU_SYSVIEW_CALL(CFG_TUSB_SYSVIEW_LEVEL_CLASS, TU_SV_ID_MSC_XFER);
  654|       |  bool const ret = mscd_xfer(rhport, ep_addr, xferred_bytes);
  655|  52.8k|  TU_SYSVIEW_RET(CFG_TUSB_SYSVIEW_LEVEL_CLASS, TU_SV_ID_MSC_XFER);
  656|  52.8k|  return ret;
  657|  52.8k|}
fuzz.c:proc_read_io_data:
  859|  49.0k|static void proc_read_io_data(mscd_interface_t* p_msc, int32_t nbytes) {
  860|  49.0k|  const uint8_t rhport = p_msc->rhport;
  861|  49.0k|  if (nbytes > 0) {
  ------------------
  |  Branch (861:7): [True: 49.0k, False: 0]
  ------------------
  862|  49.0k|    TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_epbuf.buf, (uint16_t) nbytes, false),);
  ------------------
  |  |  112|  49.0k|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|  49.0k|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |  112|  49.0k|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  109|  49.0k|#define TU_ASSERT_2ARGS(_cond, _ret)   TU_ASSERT_DEFINE(_cond, _ret)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  104|  49.0k|  do {                                                                \
  |  |  |  |  |  |  |  |  |  |  105|  49.0k|    if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   54|      0|  #define TU_MESS_FAILED() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (54:41): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |                   if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   77|      0|  #define TU_BREAKPOINT() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (77:40): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (105:10): [True: 0, False: 49.0k]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  106|  49.0k|  } while(0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (106:11): [Folded, False: 49.0k]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  863|  49.0k|  } else {
  864|       |    // nbytes is status
  865|      0|    switch (nbytes) {
  866|      0|      case TUD_MSC_RET_ERROR:
  ------------------
  |  Branch (866:7): [True: 0, False: 0]
  ------------------
  867|       |        // error -> endpoint is stalled & status in CSW set to failed
  868|      0|        TU_LOG_DRV("  IO read() failed\r\n");
  869|      0|        set_sense_medium_not_present(p_msc->cbw.lun);
  870|      0|        fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
  871|      0|        break;
  872|       |
  873|      0|      case TUD_MSC_RET_BUSY:
  ------------------
  |  Branch (873:7): [True: 0, False: 0]
  ------------------
  874|       |        // not ready yet -> fake a transfer complete so that this driver callback will fire again
  875|      0|        dcd_event_xfer_complete(rhport, p_msc->ep_in, 0, XFER_RESULT_SUCCESS, false);
  876|      0|        break;
  877|       |
  878|      0|      default: break; // nothing to do
  ------------------
  |  Branch (878:7): [True: 0, False: 0]
  ------------------
  879|      0|    }
  880|      0|  }
  881|  49.0k|}
fuzz.c:fail_scsi_op:
  108|    193|static void fail_scsi_op(mscd_interface_t* p_msc, uint8_t status) {
  109|    193|  msc_cbw_t const * p_cbw = &p_msc->cbw;
  110|    193|  msc_csw_t       * p_csw = &p_msc->csw;
  111|    193|  uint8_t rhport = p_msc->rhport;
  112|       |
  113|    193|  p_csw->status       = status;
  114|    193|  p_csw->data_residue = p_msc->cbw.total_bytes - p_msc->xferred_len;
  115|    193|  p_msc->stage        = MSC_STAGE_STATUS;
  116|       |
  117|       |  // failed but sense key is not set: default to Illegal Request
  118|    193|  if (p_msc->sense_key == 0) {
  ------------------
  |  Branch (118:7): [True: 193, False: 0]
  ------------------
  119|    193|    (void) tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
  120|    193|  }
  121|       |
  122|       |  // If there is data stage and not yet complete, stall it
  123|    193|  if (p_cbw->total_bytes && p_csw->data_residue) {
  ------------------
  |  Branch (123:7): [True: 175, False: 18]
  |  Branch (123:29): [True: 167, False: 8]
  ------------------
  124|    167|    if (is_data_in(p_cbw->dir)) {
  ------------------
  |  Branch (124:9): [True: 82, False: 85]
  ------------------
  125|     82|      usbd_edpt_stall(rhport, p_msc->ep_in);
  126|     85|    } else {
  127|     85|      usbd_edpt_stall(rhport, p_msc->ep_out);
  128|     85|    }
  129|    167|  }
  130|    193|}
fuzz.c:is_data_in:
   89|    891|TU_ATTR_ALWAYS_INLINE static inline bool is_data_in(uint8_t dir) {
   90|    891|  return tu_bit_test(dir, 7);
   91|    891|}
fuzz.c:proc_write_io_data:
  919|  3.02k|static void proc_write_io_data(mscd_interface_t* p_msc, uint32_t xferred_bytes, int32_t nbytes) {
  920|  3.02k|  if (nbytes < 0) {
  ------------------
  |  Branch (920:7): [True: 0, False: 3.02k]
  ------------------
  921|       |    // nbytes is status
  922|      0|    switch (nbytes) {
  923|      0|      case TUD_MSC_RET_ERROR:
  ------------------
  |  Branch (923:7): [True: 0, False: 0]
  ------------------
  924|       |        // IO error -> failed this scsi op
  925|      0|        TU_LOG_DRV("  IO write() failed\r\n");
  926|      0|        set_sense_medium_not_present(p_msc->cbw.lun);
  927|      0|        fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
  928|      0|        break;
  929|       |
  930|      0|      default: break; // nothing to do
  ------------------
  |  Branch (930:7): [True: 0, False: 0]
  ------------------
  931|      0|    }
  932|  3.02k|  } else {
  933|  3.02k|    if ((uint32_t)nbytes < xferred_bytes) {
  ------------------
  |  Branch (933:9): [True: 0, False: 3.02k]
  ------------------
  934|       |      // Application consume less than what we got including TUD_MSC_RET_BUSY (0)
  935|      0|      const uint32_t left_over = xferred_bytes - (uint32_t)nbytes;
  936|      0|      if (nbytes > 0) {
  ------------------
  |  Branch (936:11): [True: 0, False: 0]
  ------------------
  937|      0|        memmove(_mscd_epbuf.buf, _mscd_epbuf.buf + nbytes, left_over);
  938|      0|      }
  939|       |
  940|       |      // fake a transfer complete with adjusted parameters --> callback will be invoked with adjusted parameters
  941|      0|      dcd_event_xfer_complete(p_msc->rhport, p_msc->ep_out, left_over, XFER_RESULT_SUCCESS, false);
  942|  3.02k|    } else {
  943|       |      // Application consume all bytes in our buffer
  944|  3.02k|      p_msc->xferred_len += xferred_bytes;
  945|       |
  946|  3.02k|      if (p_msc->xferred_len >= p_msc->total_len) {
  ------------------
  |  Branch (946:11): [True: 26, False: 2.99k]
  ------------------
  947|       |        // Data Stage is complete
  948|     26|        p_msc->stage = MSC_STAGE_STATUS;
  949|  2.99k|      } else {
  950|       |        // prepare to receive more data from host
  951|  2.99k|        proc_write10_cmd(p_msc);
  952|  2.99k|      }
  953|  3.02k|    }
  954|  3.02k|  }
  955|  3.02k|}
fuzz.c:proc_write10_cmd:
  883|  3.15k|static void proc_write10_cmd(mscd_interface_t* p_msc) {
  884|  3.15k|  msc_cbw_t const* p_cbw = &p_msc->cbw;
  885|  3.15k|  const bool writable = tud_msc_is_writable_cb(p_cbw->lun);
  886|       |
  887|  3.15k|  if (!writable) {
  ------------------
  |  Branch (887:7): [True: 0, False: 3.15k]
  ------------------
  888|       |    // Not writable, complete this SCSI op with error
  889|       |    // Sense = Write protected
  890|      0|    (void) tud_msc_set_sense(p_cbw->lun, SCSI_SENSE_DATA_PROTECT, 0x27, 0x00);
  891|      0|    fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
  892|      0|    return;
  893|      0|  }
  894|       |
  895|       |  // remaining bytes capped at class buffer
  896|  3.15k|  uint16_t nbytes = (uint16_t)tu_min32(CFG_TUD_MSC_EP_BUFSIZE, p_cbw->total_bytes - p_msc->xferred_len);
  ------------------
  |  |   10|  3.15k|#define CFG_TUD_MSC_EP_BUFSIZE 512
  ------------------
  897|       |  // Write10 callback will be called later when usb transfer complete
  898|  3.15k|  TU_ASSERT(usbd_edpt_xfer(p_msc->rhport, p_msc->ep_out, _mscd_epbuf.buf, nbytes, false),);
  ------------------
  |  |  112|  3.15k|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|  3.15k|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |  112|  3.15k|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  109|  3.15k|#define TU_ASSERT_2ARGS(_cond, _ret)   TU_ASSERT_DEFINE(_cond, _ret)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  104|  3.15k|  do {                                                                \
  |  |  |  |  |  |  |  |  |  |  105|  3.15k|    if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   54|      0|  #define TU_MESS_FAILED() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (54:41): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |                   if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   77|      0|  #define TU_BREAKPOINT() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (77:40): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (105:10): [True: 0, False: 3.15k]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  106|  3.15k|  } while(0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (106:11): [Folded, False: 3.15k]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  899|  3.15k|}
fuzz.c:proc_stage_status:
  184|    340|static bool proc_stage_status(mscd_interface_t *p_msc) {
  185|    340|  uint8_t rhport = p_msc->rhport;
  186|    340|  msc_cbw_t const *p_cbw = &p_msc->cbw;
  187|       |
  188|       |  // skip status if epin is currently stalled, will do it when received Clear Stall request
  189|    340|  if (!usbd_edpt_stalled(rhport, p_msc->ep_in)) {
  ------------------
  |  Branch (189:7): [True: 258, False: 82]
  ------------------
  190|    258|    if ((p_cbw->total_bytes > p_msc->xferred_len) && is_data_in(p_cbw->dir)) {
  ------------------
  |  Branch (190:9): [True: 147, False: 111]
  |  Branch (190:54): [True: 62, False: 85]
  ------------------
  191|       |      // 6.7 The 13 Cases: case 5 (Hi > Di): STALL before status
  192|       |      // TU_LOG_DRV("  SCSI case 5 (Hi > Di): %lu > %lu\r\n", p_cbw->total_bytes, p_msc->xferred_len);
  193|     62|      usbd_edpt_stall(rhport, p_msc->ep_in);
  194|    196|    } else {
  195|    196|      TU_ASSERT(send_csw(p_msc));
  ------------------
  |  |  112|    196|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|    196|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |  112|    196|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  108|    196|#define TU_ASSERT_1ARGS(_cond)         TU_ASSERT_DEFINE(_cond, false)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  104|    196|  do {                                                                \
  |  |  |  |  |  |  |  |  |  |  105|    196|    if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   54|      0|  #define TU_MESS_FAILED() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (54:41): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |                   if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   77|      0|  #define TU_BREAKPOINT() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (77:40): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (105:10): [True: 0, False: 196]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  106|    196|  } while(0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (106:11): [Folded, False: 196]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  196|    196|    }
  197|    258|  }
  198|       |
  199|       |  #if TU_CHECK_MCU(OPT_MCU_CXD56)
  200|       |  // WORKAROUND: cxd56 has its own nuttx usb stack which does not forward Set/ClearFeature(Endpoint) to DCD.
  201|       |  // There is no way for us to know when EP is un-stall, therefore we will unconditionally un-stall here and
  202|       |  // hope everything will work
  203|       |  if (usbd_edpt_stalled(rhport, p_msc->ep_in)) {
  204|       |    usbd_edpt_clear_stall(rhport, p_msc->ep_in);
  205|       |    send_csw(p_msc);
  206|       |  }
  207|       |  #endif
  208|    340|  return true;
  209|    340|}
fuzz.c:prepare_cbw:
  102|    631|TU_ATTR_ALWAYS_INLINE static inline bool prepare_cbw(mscd_interface_t* p_msc) {
  103|    631|  uint8_t rhport = p_msc->rhport;
  104|    631|  p_msc->stage = MSC_STAGE_CMD;
  105|       |  return usbd_edpt_xfer(rhport, p_msc->ep_out,  _mscd_epbuf.buf, sizeof(msc_cbw_t), false);
  106|    631|}
fuzz.c:send_csw:
   93|    196|TU_ATTR_ALWAYS_INLINE static inline bool send_csw(mscd_interface_t* p_msc) {
   94|       |  // Data residue is always = host expect - actual transferred
   95|    196|  uint8_t rhport = p_msc->rhport;
   96|    196|  p_msc->csw.data_residue = p_msc->cbw.total_bytes - p_msc->xferred_len;
   97|    196|  p_msc->stage = MSC_STAGE_STATUS_SENT;
   98|    196|  memcpy(_mscd_epbuf.buf, &p_msc->csw, sizeof(msc_csw_t)); //-V1086
   99|       |  return usbd_edpt_xfer(rhport, p_msc->ep_in , _mscd_epbuf.buf, sizeof(msc_csw_t), false);
  100|    196|}
fuzz.c:mscd_xfer:
  453|  52.8k|static bool mscd_xfer(uint8_t rhport, uint8_t ep_addr, uint32_t xferred_bytes) {
  454|  52.8k|  mscd_interface_t* p_msc = &_mscd_itf;
  455|  52.8k|  msc_cbw_t * p_cbw = &p_msc->cbw;
  456|  52.8k|  msc_csw_t * p_csw = &p_msc->csw;
  457|       |
  458|  52.8k|  switch (p_msc->stage) {
  459|    520|    case MSC_STAGE_CMD: {
  ------------------
  |  Branch (459:5): [True: 520, False: 52.3k]
  ------------------
  460|       |      //------------- new CBW received -------------//
  461|       |      // Complete IN while waiting for CMD is usually Status of previous SCSI op, ignore it
  462|    520|      if (ep_addr != p_msc->ep_out) {
  ------------------
  |  Branch (462:11): [True: 0, False: 520]
  ------------------
  463|      0|        return true;
  464|      0|      }
  465|       |
  466|    520|      const uint32_t signature = tu_le32toh(tu_unaligned_read32(_mscd_epbuf.buf));
  ------------------
  |  |  294|    520|  #define tu_le32toh(u32) (u32)
  ------------------
  467|       |
  468|    520|      if (!(xferred_bytes == sizeof(msc_cbw_t) && signature == MSC_CBW_SIGNATURE)) {
  ------------------
  |  Branch (468:13): [True: 520, False: 0]
  |  Branch (468:51): [True: 520, False: 0]
  ------------------
  469|       |        // BOT 6.6.1 If CBW is not valid stall both endpoints until reset recovery
  470|      0|        TU_LOG_DRV("  SCSI CBW is not valid\r\n");
  471|      0|        p_msc->stage = MSC_STAGE_NEED_RESET;
  472|      0|        usbd_edpt_stall(rhport, p_msc->ep_in);
  473|      0|        usbd_edpt_stall(rhport, p_msc->ep_out);
  474|      0|        return false;
  475|      0|      }
  476|       |
  477|    520|      memcpy(p_cbw, _mscd_epbuf.buf, sizeof(msc_cbw_t));
  478|       |
  479|    520|      TU_LOG_DRV("  SCSI Command [Lun%u]: %s\r\n", p_cbw->lun, tu_lookup_find(&_msc_scsi_cmd_table, p_cbw->command[0]));
  480|       |      // TU_LOG_MEM(CFG_TUD_MSC_LOG_LEVEL, p_cbw, xferred_bytes, 2);
  481|       |
  482|    520|      p_csw->signature    = MSC_CSW_SIGNATURE;
  483|    520|      p_csw->tag          = p_cbw->tag;
  484|    520|      p_csw->data_residue = 0;
  485|    520|      p_csw->status       = MSC_CSW_STATUS_PASSED;
  486|       |
  487|       |      /*------------- Parse command and prepare DATA -------------*/
  488|    520|      p_msc->stage = MSC_STAGE_DATA;
  489|    520|      p_msc->total_len = p_cbw->total_bytes;
  490|    520|      p_msc->xferred_len = 0;
  491|       |
  492|       |      // Read10 or Write10
  493|    520|      if ((SCSI_CMD_READ_10 == p_cbw->command[0]) || (SCSI_CMD_WRITE_10 == p_cbw->command[0])) {
  ------------------
  |  Branch (493:11): [True: 124, False: 396]
  |  Branch (493:54): [True: 183, False: 213]
  ------------------
  494|    307|        uint8_t const status = rdwr10_validate_cmd(p_cbw);
  495|       |
  496|    307|        if (status != MSC_CSW_STATUS_PASSED) {
  ------------------
  |  Branch (496:13): [True: 67, False: 240]
  ------------------
  497|     67|          fail_scsi_op(p_msc, status);
  498|    240|        } else if (p_cbw->total_bytes > 0) {
  ------------------
  |  Branch (498:20): [True: 239, False: 1]
  ------------------
  499|    239|          if (SCSI_CMD_READ_10 == p_cbw->command[0]) {
  ------------------
  |  Branch (499:15): [True: 85, False: 154]
  ------------------
  500|     85|            proc_read10_cmd(p_msc);
  501|    154|          } else {
  502|    154|            proc_write10_cmd(p_msc);
  503|    154|          }
  504|    239|        } else {
  505|       |          // no data transfer, only exist in complaint test suite
  506|      1|          p_msc->stage = MSC_STAGE_STATUS;
  507|      1|        }
  508|    307|      } else {
  509|       |        // For other SCSI commands
  510|       |        // 1. OUT : queue transfer (invoke app callback after done)
  511|       |        // 2. IN & Zero: Process if is built-in, else Invoke app callback. Skip DATA if zero length
  512|    213|        if ((p_cbw->total_bytes > 0) && !is_data_in(p_cbw->dir)) {
  ------------------
  |  Branch (512:13): [True: 209, False: 4]
  |  Branch (512:41): [True: 78, False: 131]
  ------------------
  513|     78|          if (p_cbw->total_bytes > CFG_TUD_MSC_EP_BUFSIZE) {
  ------------------
  |  |   10|     78|#define CFG_TUD_MSC_EP_BUFSIZE 512
  ------------------
  |  Branch (513:15): [True: 55, False: 23]
  ------------------
  514|     55|            TU_LOG_DRV("  SCSI reject non READ10/WRITE10 with large data\r\n");
  515|     55|            fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
  516|     55|          } else {
  517|       |            // Didn't check for case 9 (Ho > Dn), which requires examining scsi command first
  518|       |            // but it is OK to just receive data then responded with failed status
  519|     23|            TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_out, _mscd_epbuf.buf, (uint16_t) p_msc->total_len, false));
  ------------------
  |  |  112|     23|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|     23|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |  112|     23|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  108|     23|#define TU_ASSERT_1ARGS(_cond)         TU_ASSERT_DEFINE(_cond, false)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  104|     23|  do {                                                                \
  |  |  |  |  |  |  |  |  |  |  105|     23|    if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   54|      0|  #define TU_MESS_FAILED() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (54:41): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |                   if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   77|      0|  #define TU_BREAKPOINT() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (77:40): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (105:10): [True: 0, False: 23]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  106|     23|  } while(0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (106:11): [Folded, False: 23]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  520|     23|          }
  521|    135|        } else {
  522|       |          // First process if it is a built-in commands
  523|    135|          int32_t resplen = proc_builtin_scsi(p_cbw->lun, p_cbw->command, _mscd_epbuf.buf, CFG_TUD_MSC_EP_BUFSIZE);
  ------------------
  |  |   10|    135|#define CFG_TUD_MSC_EP_BUFSIZE 512
  ------------------
  524|       |
  525|       |          // Invoke user callback if not built-in
  526|    135|          if ((resplen < 0) && (p_msc->sense_key == 0)) {
  ------------------
  |  Branch (526:15): [True: 10, False: 125]
  |  Branch (526:32): [True: 10, False: 0]
  ------------------
  527|     10|            resplen = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_epbuf.buf,
  528|     10|                                      (uint16_t) tu_min32(p_msc->total_len, CFG_TUD_MSC_EP_BUFSIZE));
  ------------------
  |  |   10|     10|#define CFG_TUD_MSC_EP_BUFSIZE 512
  ------------------
  529|     10|          }
  530|       |
  531|    135|          if (resplen < 0) {
  ------------------
  |  Branch (531:15): [True: 10, False: 125]
  ------------------
  532|       |            // unsupported command
  533|     10|            TU_LOG_DRV("  SCSI unsupported or failed command\r\n");
  534|     10|            fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
  535|    125|          } else if (resplen == 0) {
  ------------------
  |  Branch (535:22): [True: 54, False: 71]
  ------------------
  536|     54|            if (p_cbw->total_bytes > 0) {
  ------------------
  |  Branch (536:17): [True: 52, False: 2]
  ------------------
  537|       |              // 6.7 The 13 Cases: case 4 (Hi > Dn)
  538|       |              // TU_LOG_DRV("  SCSI case 4 (Hi > Dn): %lu\r\n", p_cbw->total_bytes);
  539|     52|              fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
  540|     52|            } else {
  541|       |              // case 1 Hn = Dn: all good
  542|      2|              p_msc->stage = MSC_STAGE_STATUS;
  543|      2|            }
  544|     71|          } else {
  545|     71|            if (p_cbw->total_bytes == 0) {
  ------------------
  |  Branch (545:17): [True: 1, False: 70]
  ------------------
  546|       |              // 6.7 The 13 Cases: case 2 (Hn < Di)
  547|       |              // TU_LOG_DRV("  SCSI case 2 (Hn < Di): %lu\r\n", p_cbw->total_bytes);
  548|      1|              fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
  549|     70|            } else {
  550|       |              // cannot return more than host expect
  551|     70|              p_msc->total_len = tu_min32((uint32_t)resplen, p_cbw->total_bytes);
  552|     70|              TU_ASSERT(usbd_edpt_xfer(rhport, p_msc->ep_in, _mscd_epbuf.buf, (uint16_t) p_msc->total_len, false));
  ------------------
  |  |  112|     70|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|     70|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |  112|     70|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  108|     70|#define TU_ASSERT_1ARGS(_cond)         TU_ASSERT_DEFINE(_cond, false)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  104|     70|  do {                                                                \
  |  |  |  |  |  |  |  |  |  |  105|     70|    if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   54|      0|  #define TU_MESS_FAILED() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (54:41): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |                   if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   77|      0|  #define TU_BREAKPOINT() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (77:40): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (105:10): [True: 0, False: 70]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  106|     70|  } while(0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (106:11): [Folded, False: 70]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  553|     70|            }
  554|     71|          }
  555|    135|        }
  556|    213|      }
  557|    520|      break;
  558|    520|    }
  559|       |
  560|  52.1k|    case MSC_STAGE_DATA:
  ------------------
  |  Branch (560:5): [True: 52.1k, False: 716]
  ------------------
  561|  52.1k|      TU_LOG_DRV("  SCSI Data [Lun%u]\r\n", p_cbw->lun);
  562|       |      // sanity check to avoid buffer overflow
  563|  52.1k|      TU_ASSERT(xferred_bytes <= CFG_TUD_MSC_EP_BUFSIZE);
  ------------------
  |  |  112|  52.1k|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|  52.1k|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |  112|  52.1k|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  108|  52.1k|#define TU_ASSERT_1ARGS(_cond)         TU_ASSERT_DEFINE(_cond, false)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  104|  52.1k|  do {                                                                \
  |  |  |  |  |  |  |  |  |  |  105|  52.1k|    if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   54|      0|  #define TU_MESS_FAILED() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (54:41): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |                   if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   77|      0|  #define TU_BREAKPOINT() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (77:40): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (105:10): [True: 0, False: 52.1k]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  106|  52.1k|  } while(0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (106:11): [Folded, False: 52.1k]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  564|       |      // TU_LOG_MEM(CFG_TUD_MSC_LOG_LEVEL, _mscd_epbuf.buf, xferred_bytes, 2);
  565|       |
  566|  52.1k|      if (SCSI_CMD_READ_10 == p_cbw->command[0]) {
  ------------------
  |  Branch (566:11): [True: 49.0k, False: 3.10k]
  ------------------
  567|  49.0k|        p_msc->xferred_len += xferred_bytes;
  568|       |
  569|  49.0k|        if ( p_msc->xferred_len >= p_msc->total_len ) {
  ------------------
  |  Branch (569:14): [True: 48, False: 48.9k]
  ------------------
  570|       |          // Data Stage is complete
  571|     48|          p_msc->stage = MSC_STAGE_STATUS;
  572|  48.9k|        }else {
  573|  48.9k|          proc_read10_cmd(p_msc);
  574|  48.9k|        }
  575|  49.0k|      } else if (SCSI_CMD_WRITE_10 == p_cbw->command[0]) {
  ------------------
  |  Branch (575:18): [True: 3.02k, False: 78]
  ------------------
  576|  3.02k|        proc_write10_host_data(p_msc, xferred_bytes);
  577|  3.02k|      } else {
  578|     78|        p_msc->xferred_len += xferred_bytes;
  579|       |
  580|       |        // OUT transfer, invoke callback if needed
  581|     78|        if ( !is_data_in(p_cbw->dir) ) {
  ------------------
  |  Branch (581:14): [True: 8, False: 70]
  ------------------
  582|      8|          int32_t cb_result = tud_msc_scsi_cb(p_cbw->lun, p_cbw->command, _mscd_epbuf.buf, (uint16_t) p_msc->total_len);
  583|       |
  584|      8|          if ( cb_result < 0 ) {
  ------------------
  |  Branch (584:16): [True: 8, False: 0]
  ------------------
  585|       |            // unsupported command
  586|      8|            TU_LOG_DRV("  SCSI unsupported command\r\n");
  587|      8|            fail_scsi_op(p_msc, MSC_CSW_STATUS_FAILED);
  588|      8|          }else {
  589|       |            // TODO haven't implement this scenario any further yet
  590|      0|          }
  591|      8|        }
  592|       |
  593|     78|        if ( p_msc->xferred_len >= p_msc->total_len ) {
  ------------------
  |  Branch (593:14): [True: 78, False: 0]
  ------------------
  594|       |          // Data Stage is complete
  595|     78|          p_msc->stage = MSC_STAGE_STATUS;
  596|     78|        } else {
  597|       |          // This scenario with command that take more than one transfer is already rejected at Command stage
  598|      0|          TU_BREAKPOINT();
  ------------------
  |  |   77|      0|  #define TU_BREAKPOINT() do {} while (0)
  |  |  ------------------
  |  |  |  Branch (77:40): [Folded, False: 0]
  |  |  ------------------
  ------------------
  599|      0|        }
  600|     78|      }
  601|  52.1k|    break;
  602|       |
  603|      0|    case MSC_STAGE_STATUS:
  ------------------
  |  Branch (603:5): [True: 0, False: 52.8k]
  ------------------
  604|       |      // processed immediately after this switch, supposedly to be empty
  605|      0|    break;
  606|       |
  607|    196|    case MSC_STAGE_STATUS_SENT:
  ------------------
  |  Branch (607:5): [True: 196, False: 52.6k]
  ------------------
  608|       |      // Status phase is complete
  609|    196|      if ((ep_addr == p_msc->ep_in) && (xferred_bytes == sizeof(msc_csw_t))) {
  ------------------
  |  Branch (609:11): [True: 196, False: 0]
  |  Branch (609:40): [True: 196, False: 0]
  ------------------
  610|    196|        TU_LOG_DRV("  SCSI Status [Lun%u] = %u\r\n", p_cbw->lun, p_csw->status);
  611|       |        // TU_LOG_MEM(CFG_TUD_MSC_LOG_LEVEL, p_csw, xferred_bytes, 2);
  612|       |
  613|       |        // Invoke complete callback if defined
  614|       |        // Note: There is racing issue with samd51 + qspi flash testing with arduino
  615|       |        // if complete_cb() is invoked after queuing the status.
  616|    196|        switch (p_cbw->command[0]) {
  617|     71|          case SCSI_CMD_READ_10:
  ------------------
  |  Branch (617:11): [True: 71, False: 125]
  ------------------
  618|     71|            tud_msc_read10_complete_cb(p_cbw->lun);
  619|     71|            break;
  620|       |
  621|     50|          case SCSI_CMD_WRITE_10:
  ------------------
  |  Branch (621:11): [True: 50, False: 146]
  ------------------
  622|     50|            tud_msc_write10_complete_cb(p_cbw->lun);
  623|     50|            break;
  624|       |
  625|     75|          default:
  ------------------
  |  Branch (625:11): [True: 75, False: 121]
  ------------------
  626|     75|            tud_msc_scsi_complete_cb(p_cbw->lun, p_cbw->command);
  627|     75|            break;
  628|    196|        }
  629|       |
  630|    196|        if (!usbd_edpt_stalled(rhport, p_msc->ep_out)) {
  ------------------
  |  Branch (630:13): [True: 111, False: 85]
  ------------------
  631|    111|          TU_ASSERT(prepare_cbw(p_msc));
  ------------------
  |  |  112|    111|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|    111|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |  112|    111|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  108|    111|#define TU_ASSERT_1ARGS(_cond)         TU_ASSERT_DEFINE(_cond, false)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  104|    111|  do {                                                                \
  |  |  |  |  |  |  |  |  |  |  105|    111|    if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   54|      0|  #define TU_MESS_FAILED() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (54:41): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |                   if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   77|      0|  #define TU_BREAKPOINT() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (77:40): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (105:10): [True: 0, False: 111]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  106|    111|  } while(0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (106:11): [Folded, False: 111]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  632|    111|        } else {
  633|     85|          p_msc->stage = MSC_STAGE_CMD;
  634|     85|        }
  635|    196|      } else {
  636|       |        // Any xfer ended here is considered unknown error, ignore it
  637|      0|        TU_LOG1("  Warning expect SCSI Status but received unknown data\r\n");
  638|      0|      }
  639|    196|      break;
  640|       |
  641|    196|    default: break; // nothing to do
  ------------------
  |  Branch (641:5): [True: 0, False: 52.8k]
  ------------------
  642|  52.8k|  }
  643|       |
  644|  52.8k|  if (p_msc->stage == MSC_STAGE_STATUS) {
  ------------------
  |  Branch (644:7): [True: 340, False: 52.5k]
  ------------------
  645|    340|    TU_ASSERT(proc_stage_status(p_msc));
  ------------------
  |  |  112|    340|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|    340|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |  112|    340|#define TU_ASSERT(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_ASSERT_2ARGS, TU_ASSERT_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  108|    340|#define TU_ASSERT_1ARGS(_cond)         TU_ASSERT_DEFINE(_cond, false)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  104|    340|  do {                                                                \
  |  |  |  |  |  |  |  |  |  |  105|    340|    if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   54|      0|  #define TU_MESS_FAILED() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (54:41): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |                   if ( !(_cond) ) { TU_MESS_FAILED(); TU_BREAKPOINT(); return _ret; } \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |   77|      0|  #define TU_BREAKPOINT() do {} while (0)
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  |  |  Branch (77:40): [Folded, False: 0]
  |  |  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (105:10): [True: 0, False: 340]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  106|    340|  } while(0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (106:11): [Folded, False: 340]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  646|    340|  }
  647|       |
  648|  52.8k|  return true;
  649|  52.8k|}
fuzz.c:rdwr10_validate_cmd:
  152|    307|static uint8_t rdwr10_validate_cmd(msc_cbw_t const* cbw) {
  153|    307|  uint8_t status = MSC_CSW_STATUS_PASSED;
  154|    307|  uint16_t const block_count = rdwr10_get_blockcount(cbw);
  155|       |
  156|    307|  if (cbw->total_bytes == 0) {
  ------------------
  |  Branch (156:7): [True: 17, False: 290]
  ------------------
  157|     17|    if (block_count > 0) {
  ------------------
  |  Branch (157:9): [True: 16, False: 1]
  ------------------
  158|     16|      TU_LOG_DRV("  SCSI case 2 (Hn < Di) or case 3 (Hn < Do) \r\n");
  159|     16|      status = MSC_CSW_STATUS_PHASE_ERROR;
  160|     16|    } else {
  161|       |      // no data transfer, only exist in complaint test suite
  162|      1|    }
  163|    290|  } else {
  164|    290|    if (SCSI_CMD_READ_10 == cbw->command[0] && !is_data_in(cbw->dir)) {
  ------------------
  |  Branch (164:9): [True: 113, False: 177]
  |  Branch (164:48): [True: 12, False: 101]
  ------------------
  165|     12|      TU_LOG_DRV("  SCSI case 10 (Ho <> Di)\r\n");
  166|     12|      status = MSC_CSW_STATUS_PHASE_ERROR;
  167|    278|    } else if (SCSI_CMD_WRITE_10 == cbw->command[0] && is_data_in(cbw->dir)) {
  ------------------
  |  Branch (167:16): [True: 177, False: 101]
  |  Branch (167:56): [True: 5, False: 172]
  ------------------
  168|      5|      TU_LOG_DRV("  SCSI case 8 (Hi <> Do)\r\n");
  169|      5|      status = MSC_CSW_STATUS_PHASE_ERROR;
  170|    273|    } else if (0 == block_count || (cbw->total_bytes >> 16) >= block_count) { // or block size > 16-bit, without a divide
  ------------------
  |  Branch (170:16): [True: 5, False: 268]
  |  Branch (170:36): [True: 23, False: 245]
  ------------------
  171|     28|      TU_LOG_DRV("  SCSI case 4 Hi > Dn (READ10) or case 9 Ho > Dn (WRITE10) \r\n");
  172|     28|      status = MSC_CSW_STATUS_FAILED;
  173|    245|    } else if (cbw->total_bytes / block_count == 0) {
  ------------------
  |  Branch (173:16): [True: 6, False: 239]
  ------------------
  174|      6|      TU_LOG_DRV(" Computed block size = 0. SCSI case 7 Hi < Di (READ10) or case 13 Ho < Do (WRIT10)\r\n");
  175|      6|      status = MSC_CSW_STATUS_PHASE_ERROR;
  176|    239|    } else {
  177|       |      // nothing to do
  178|    239|    }
  179|    290|  }
  180|       |
  181|    307|  return status;
  182|    307|}
fuzz.c:rdwr10_get_blockcount:
  138|  52.3k|TU_ATTR_ALWAYS_INLINE static inline uint16_t rdwr10_get_blockcount(msc_cbw_t const* cbw) {
  139|  52.3k|  uint16_t const block_count = tu_unaligned_read16(cbw->command + offsetof(scsi_write10_t, block_count));
  140|  52.3k|  return tu_ntohs(block_count);
  ------------------
  |  |  285|  52.3k|  #define tu_ntohs(u16)  (TU_BSWAP16(u16))
  |  |  ------------------
  |  |  |  |  206|  52.3k|    #define TU_BSWAP16(u16) (__builtin_bswap16(u16))
  |  |  ------------------
  ------------------
  141|  52.3k|}
fuzz.c:proc_read10_cmd:
  840|  49.0k|static void proc_read10_cmd(mscd_interface_t* p_msc) {
  841|  49.0k|  msc_cbw_t const* p_cbw = &p_msc->cbw;
  842|  49.0k|  uint16_t const block_sz = rdwr10_get_blocksize(p_cbw); // already verified non-zero
  843|  49.0k|  TU_VERIFY(block_sz != 0, );
  ------------------
  |  |   95|  49.0k|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|  49.0k|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |   95|  49.0k|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |   93|  49.0k|#define TU_VERIFY_2ARGS(_cond, _ret)   TU_VERIFY_DEFINE(_cond, _ret)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   86|  49.0k|  do {                                \
  |  |  |  |  |  |  |  |  |  |   87|  49.0k|    if (!(_cond)) {                   \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (87:9): [True: 0, False: 49.0k]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   88|      0|      return _ret;                    \
  |  |  |  |  |  |  |  |  |  |   89|      0|    }                                 \
  |  |  |  |  |  |  |  |  |  |   90|  49.0k|  } while (0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (90:12): [Folded, False: 49.0k]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  844|       |  // Adjust lba & offset with transferred bytes
  845|  49.0k|  uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
  846|  49.0k|  uint32_t const offset = p_msc->xferred_len % block_sz;
  847|       |
  848|       |  // remaining bytes capped at class buffer
  849|  49.0k|  int32_t nbytes = (int32_t)tu_min32(CFG_TUD_MSC_EP_BUFSIZE, p_cbw->total_bytes - p_msc->xferred_len);
  ------------------
  |  |   10|  49.0k|#define CFG_TUD_MSC_EP_BUFSIZE 512
  ------------------
  850|       |
  851|  49.0k|  p_msc->pending_io = true;
  852|  49.0k|  nbytes = tud_msc_read10_cb(p_cbw->lun, lba, offset, _mscd_epbuf.buf, (uint32_t)nbytes);
  853|  49.0k|  if (nbytes != TUD_MSC_RET_ASYNC) {
  ------------------
  |  Branch (853:7): [True: 49.0k, False: 0]
  ------------------
  854|       |    p_msc->pending_io = false;
  855|  49.0k|    proc_read_io_data(p_msc, nbytes);
  856|  49.0k|  }
  857|  49.0k|}
fuzz.c:rdwr10_get_blocksize:
  143|  52.0k|TU_ATTR_ALWAYS_INLINE static inline uint16_t rdwr10_get_blocksize(msc_cbw_t const* cbw) {
  144|       |  // first extract block count in the command
  145|  52.0k|  uint16_t const block_count = rdwr10_get_blockcount(cbw);
  146|  52.0k|  if (block_count == 0) {
  ------------------
  |  Branch (146:7): [True: 0, False: 52.0k]
  ------------------
  147|      0|    return 0; // invalid block count
  148|      0|  }
  149|  52.0k|  return (uint16_t) (cbw->total_bytes / block_count);
  150|  52.0k|}
fuzz.c:rdwr10_get_lba:
  132|  52.0k|TU_ATTR_ALWAYS_INLINE static inline uint32_t rdwr10_get_lba(uint8_t const command[]) {
  133|       |  // use offsetof to avoid pointer to the odd/unaligned address
  134|  52.0k|  const uint32_t lba = tu_unaligned_read32(command + offsetof(scsi_write10_t, lba));
  135|  52.0k|  return tu_ntohl(lba); // lba is in Big Endian
  ------------------
  |  |  288|  52.0k|  #define tu_ntohl(u32)  (TU_BSWAP32(u32))
  |  |  ------------------
  |  |  |  |  207|  52.0k|    #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
  |  |  ------------------
  ------------------
  136|  52.0k|}
fuzz.c:proc_builtin_scsi:
  665|    135|static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_t* buffer, uint32_t bufsize) {
  666|    135|  (void)bufsize; // TODO refractor later
  667|    135|  int32_t resplen;
  668|       |
  669|    135|  mscd_interface_t* p_msc = &_mscd_itf;
  670|       |
  671|    135|  switch (scsi_cmd[0]) {
  672|     49|    case SCSI_CMD_TEST_UNIT_READY:
  ------------------
  |  Branch (672:5): [True: 49, False: 86]
  ------------------
  673|     49|      resplen = 0;
  674|     49|      if (!tud_msc_test_unit_ready_cb(lun)) {
  ------------------
  |  Branch (674:11): [True: 0, False: 49]
  ------------------
  675|       |        // Failed status response
  676|      0|        resplen = -1;
  677|       |
  678|       |        // set default sense if not set by callback
  679|      0|        if (p_msc->sense_key == 0) {
  ------------------
  |  Branch (679:13): [True: 0, False: 0]
  ------------------
  680|      0|          set_sense_medium_not_present(lun);
  681|      0|        }
  682|      0|      }
  683|     49|      break;
  684|       |
  685|      4|    case SCSI_CMD_START_STOP_UNIT: {
  ------------------
  |  Branch (685:5): [True: 4, False: 131]
  ------------------
  686|      4|      resplen = 0;
  687|      4|      scsi_start_stop_unit_t const* start_stop = (scsi_start_stop_unit_t const*)scsi_cmd;
  688|      4|      if (!tud_msc_start_stop_cb(lun, start_stop->power_condition, start_stop->start, start_stop->load_eject)) {
  ------------------
  |  Branch (688:11): [True: 0, False: 4]
  ------------------
  689|       |        // Failed status response
  690|      0|        resplen = -1;
  691|       |
  692|       |        // set default sense if not set by callback
  693|      0|        if (p_msc->sense_key == 0) {
  ------------------
  |  Branch (693:13): [True: 0, False: 0]
  ------------------
  694|      0|          set_sense_medium_not_present(lun);
  695|      0|        }
  696|      0|      }
  697|      4|      break;
  698|      0|    }
  699|       |
  700|      1|    case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: {
  ------------------
  |  Branch (700:5): [True: 1, False: 134]
  ------------------
  701|      1|      resplen = 0;
  702|      1|      scsi_prevent_allow_medium_removal_t const* prevent_allow = (scsi_prevent_allow_medium_removal_t const*)scsi_cmd;
  703|      1|      if (!tud_msc_prevent_allow_medium_removal_cb(lun, prevent_allow->prohibit_removal, prevent_allow->control)) {
  ------------------
  |  Branch (703:11): [True: 0, False: 1]
  ------------------
  704|       |        // Failed status response
  705|      0|        resplen = -1;
  706|       |
  707|       |        // set default sense if not set by callback
  708|      0|        if (p_msc->sense_key == 0) {
  ------------------
  |  Branch (708:13): [True: 0, False: 0]
  ------------------
  709|      0|          set_sense_medium_not_present(lun);
  710|      0|        }
  711|      0|      }
  712|      1|      break;
  713|      0|    }
  714|       |
  715|     11|    case SCSI_CMD_READ_CAPACITY_10: {
  ------------------
  |  Branch (715:5): [True: 11, False: 124]
  ------------------
  716|     11|      uint32_t block_count;
  717|     11|      uint32_t block_size;
  718|     11|      uint16_t block_size_u16;
  719|       |
  720|     11|      tud_msc_capacity_cb(lun, &block_count, &block_size_u16);
  721|     11|      block_size = (uint32_t)block_size_u16;
  722|       |
  723|       |      // Invalid block size/count from callback, possibly unit is not ready
  724|       |      // stall this request, set sense key to NOT READY
  725|     11|      if (block_count == 0 || block_size == 0) {
  ------------------
  |  Branch (725:11): [True: 0, False: 11]
  |  Branch (725:31): [True: 0, False: 11]
  ------------------
  726|      0|        resplen = -1;
  727|       |
  728|       |        // set default sense if not set by callback
  729|      0|        if (p_msc->sense_key == 0) {
  ------------------
  |  Branch (729:13): [True: 0, False: 0]
  ------------------
  730|      0|          set_sense_medium_not_present(lun);
  731|      0|        }
  732|     11|      } else {
  733|     11|        scsi_read_capacity10_resp_t read_capa10;
  734|       |
  735|     11|        read_capa10.last_lba = tu_htonl(block_count-1);
  ------------------
  |  |  287|     11|  #define tu_htonl(u32)  (TU_BSWAP32(u32))
  |  |  ------------------
  |  |  |  |  207|     11|    #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
  |  |  ------------------
  ------------------
  736|     11|        read_capa10.block_size = tu_htonl(block_size);
  ------------------
  |  |  287|     11|  #define tu_htonl(u32)  (TU_BSWAP32(u32))
  |  |  ------------------
  |  |  |  |  207|     11|    #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
  |  |  ------------------
  ------------------
  737|       |
  738|     11|        resplen = sizeof(read_capa10);
  739|     11|        TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &read_capa10, (size_t) resplen));
  ------------------
  |  |   95|     11|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|     11|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |   95|     11|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |   92|     11|#define TU_VERIFY_1ARGS(_cond)         TU_VERIFY_DEFINE(_cond, false)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   86|     11|  do {                                \
  |  |  |  |  |  |  |  |  |  |   87|     11|    if (!(_cond)) {                   \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (87:9): [True: 0, False: 11]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   88|      0|      return _ret;                    \
  |  |  |  |  |  |  |  |  |  |   89|      0|    }                                 \
  |  |  |  |  |  |  |  |  |  |   90|     11|  } while (0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (90:12): [Folded, False: 11]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  740|     11|      }
  741|     11|      break;
  742|     11|    }
  743|       |
  744|     11|    case SCSI_CMD_READ_FORMAT_CAPACITY: {
  ------------------
  |  Branch (744:5): [True: 8, False: 127]
  ------------------
  745|      8|      scsi_read_format_capacity_data_t read_fmt_capa = {
  746|      8|        .list_length = 8,
  747|      8|        .block_num = 0,
  748|      8|        .descriptor_type = 2, // formatted media
  749|      8|        .block_size_u16 = 0
  750|      8|      };
  751|       |
  752|      8|      uint32_t block_count;
  753|      8|      uint16_t block_size;
  754|       |
  755|      8|      tud_msc_capacity_cb(lun, &block_count, &block_size);
  756|       |
  757|       |      // Invalid block size/count from callback, possibly unit is not ready
  758|       |      // stall this request, set sense key to NOT READY
  759|      8|      if (block_count == 0 || block_size == 0) {
  ------------------
  |  Branch (759:11): [True: 0, False: 8]
  |  Branch (759:31): [True: 0, False: 8]
  ------------------
  760|      0|        resplen = -1;
  761|       |
  762|       |        // set default sense if not set by callback
  763|      0|        if (p_msc->sense_key == 0) {
  ------------------
  |  Branch (763:13): [True: 0, False: 0]
  ------------------
  764|      0|          set_sense_medium_not_present(lun);
  765|      0|        }
  766|      8|      } else {
  767|      8|        read_fmt_capa.block_num = tu_htonl(block_count);
  ------------------
  |  |  287|      8|  #define tu_htonl(u32)  (TU_BSWAP32(u32))
  |  |  ------------------
  |  |  |  |  207|      8|    #define TU_BSWAP32(u32) (__builtin_bswap32(u32))
  |  |  ------------------
  ------------------
  768|      8|        read_fmt_capa.block_size_u16 = tu_htons(block_size);
  ------------------
  |  |  284|      8|  #define tu_htons(u16)  (TU_BSWAP16(u16))
  |  |  ------------------
  |  |  |  |  206|      8|    #define TU_BSWAP16(u16) (__builtin_bswap16(u16))
  |  |  ------------------
  ------------------
  769|       |
  770|      8|        resplen = sizeof(read_fmt_capa);
  771|      8|        TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &read_fmt_capa, (size_t) resplen));
  ------------------
  |  |   95|      8|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|      8|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |   95|      8|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |   92|      8|#define TU_VERIFY_1ARGS(_cond)         TU_VERIFY_DEFINE(_cond, false)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   86|      8|  do {                                \
  |  |  |  |  |  |  |  |  |  |   87|      8|    if (!(_cond)) {                   \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (87:9): [True: 0, False: 8]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   88|      0|      return _ret;                    \
  |  |  |  |  |  |  |  |  |  |   89|      0|    }                                 \
  |  |  |  |  |  |  |  |  |  |   90|      8|  } while (0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (90:12): [Folded, False: 8]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  772|      8|      }
  773|      8|      break;
  774|      8|    }
  775|       |
  776|     12|    case SCSI_CMD_INQUIRY: {
  ------------------
  |  Branch (776:5): [True: 12, False: 123]
  ------------------
  777|     12|      scsi_inquiry_resp_t *inquiry_rsp = (scsi_inquiry_resp_t *) buffer;
  778|     12|      tu_memclr(inquiry_rsp, sizeof(scsi_inquiry_resp_t));
  ------------------
  |  |   95|     12|#define tu_memclr(buffer, size)  (void) memset((buffer), 0, (size))
  ------------------
  779|     12|      inquiry_rsp->is_removable = 1;
  780|     12|      inquiry_rsp->version = 2;
  781|     12|      inquiry_rsp->response_data_format = 2;
  782|     12|      inquiry_rsp->additional_length = sizeof(scsi_inquiry_resp_t) - 5;
  783|       |
  784|     12|      resplen = (int32_t) tud_msc_inquiry2_cb(lun, inquiry_rsp, bufsize);
  785|     12|      if (resplen == 0) {
  ------------------
  |  Branch (785:11): [True: 12, False: 0]
  ------------------
  786|       |        // stub callback with no response, use v1 callback
  787|     12|        tud_msc_inquiry_cb(lun, inquiry_rsp->vendor_id, inquiry_rsp->product_id, inquiry_rsp->product_rev);
  788|     12|        resplen = sizeof(scsi_inquiry_resp_t);
  789|     12|      }
  790|     12|      break;
  791|      8|    }
  792|       |
  793|     12|    case SCSI_CMD_MODE_SENSE_6: {
  ------------------
  |  Branch (793:5): [True: 12, False: 123]
  ------------------
  794|     12|      scsi_mode_sense6_resp_t mode_resp = {
  795|     12|        .data_len = 3,
  796|     12|        .medium_type = 0,
  797|     12|        .write_protected = false,
  798|     12|        .reserved = 0,
  799|     12|        .block_descriptor_len = 0 // no block descriptor are included
  800|     12|      };
  801|       |
  802|     12|      bool writable = tud_msc_is_writable_cb(lun);
  803|       |
  804|     12|      mode_resp.write_protected = !writable;
  805|       |
  806|     12|      resplen = sizeof(mode_resp);
  807|     12|      TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &mode_resp, (size_t) resplen));
  ------------------
  |  |   95|     12|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|     12|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |   95|     12|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |   92|     12|#define TU_VERIFY_1ARGS(_cond)         TU_VERIFY_DEFINE(_cond, false)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   86|     12|  do {                                \
  |  |  |  |  |  |  |  |  |  |   87|     12|    if (!(_cond)) {                   \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (87:9): [True: 0, False: 12]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   88|      0|      return _ret;                    \
  |  |  |  |  |  |  |  |  |  |   89|      0|    }                                 \
  |  |  |  |  |  |  |  |  |  |   90|     12|  } while (0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (90:12): [Folded, False: 12]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  808|     12|      break;
  809|     12|    }
  810|       |
  811|     28|    case SCSI_CMD_REQUEST_SENSE: {
  ------------------
  |  Branch (811:5): [True: 28, False: 107]
  ------------------
  812|     28|      scsi_sense_fixed_resp_t sense_rsp = {
  813|     28|        .response_code = 0x70, // current, fixed format
  814|     28|        .valid = 1
  815|     28|      };
  816|       |
  817|     28|      sense_rsp.add_sense_len = sizeof(scsi_sense_fixed_resp_t) - 8;
  818|     28|      sense_rsp.sense_key = (uint8_t)(p_msc->sense_key & 0x0F);
  819|     28|      sense_rsp.add_sense_code = p_msc->add_sense_code;
  820|     28|      sense_rsp.add_sense_qualifier = p_msc->add_sense_qualifier;
  821|       |
  822|     28|      resplen = sizeof(sense_rsp);
  823|     28|      TU_VERIFY(0 == tu_memcpy_s(buffer, bufsize, &sense_rsp, (size_t) resplen));
  ------------------
  |  |   95|     28|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|     28|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |   95|     28|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |   92|     28|#define TU_VERIFY_1ARGS(_cond)         TU_VERIFY_DEFINE(_cond, false)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   86|     28|  do {                                \
  |  |  |  |  |  |  |  |  |  |   87|     28|    if (!(_cond)) {                   \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (87:9): [True: 0, False: 28]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   88|      0|      return _ret;                    \
  |  |  |  |  |  |  |  |  |  |   89|      0|    }                                 \
  |  |  |  |  |  |  |  |  |  |   90|     28|  } while (0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (90:12): [Folded, False: 28]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  824|       |
  825|       |      // request sense callback could overwrite the sense data
  826|     28|      resplen = tud_msc_request_sense_cb(lun, buffer, (uint16_t)bufsize);
  827|       |
  828|       |      // Clear sense data after copy
  829|     28|      (void) tud_msc_set_sense(lun, 0, 0, 0);
  830|     28|      break;
  831|     28|    }
  832|       |
  833|     10|    default: resplen = -1;
  ------------------
  |  Branch (833:5): [True: 10, False: 125]
  ------------------
  834|     10|      break;
  835|    135|  }
  836|       |
  837|    135|  return resplen;
  838|    135|}
fuzz.c:proc_write10_host_data:
  902|  3.02k|static void proc_write10_host_data(mscd_interface_t* p_msc, uint32_t xferred_bytes) {
  903|  3.02k|  msc_cbw_t const* p_cbw = &p_msc->cbw;
  904|  3.02k|  uint16_t const block_sz = rdwr10_get_blocksize(p_cbw); // already verified non-zero
  905|  3.02k|  TU_VERIFY(block_sz != 0, );
  ------------------
  |  |   95|  3.02k|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  ------------------
  |  |  |  |  131|  3.02k|#define TU_GET_3RD_ARG(arg1, arg2, arg3, ...)        arg3
  |  |  |  |  ------------------
  |  |  |  |  |  |   95|  3.02k|#define TU_VERIFY(...)                 TU_GET_3RD_ARG(__VA_ARGS__, TU_VERIFY_2ARGS, TU_VERIFY_1ARGS, _dummy)(__VA_ARGS__)
  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |   93|  3.02k|#define TU_VERIFY_2ARGS(_cond, _ret)   TU_VERIFY_DEFINE(_cond, _ret)
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   86|  3.02k|  do {                                \
  |  |  |  |  |  |  |  |  |  |   87|  3.02k|    if (!(_cond)) {                   \
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (87:9): [True: 0, False: 3.02k]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |   88|      0|      return _ret;                    \
  |  |  |  |  |  |  |  |  |  |   89|      0|    }                                 \
  |  |  |  |  |  |  |  |  |  |   90|  3.02k|  } while (0)
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  |  |  |  Branch (90:12): [Folded, False: 3.02k]
  |  |  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  |  |  ------------------
  |  |  |  |  |  |  ------------------
  |  |  |  |  ------------------
  |  |  ------------------
  ------------------
  906|       |
  907|       |  // Adjust lba & offset with transferred bytes
  908|  3.02k|  uint32_t const lba = rdwr10_get_lba(p_cbw->command) + (p_msc->xferred_len / block_sz);
  909|  3.02k|  uint32_t const offset = p_msc->xferred_len % block_sz;
  910|       |
  911|  3.02k|  p_msc->pending_io = true;
  912|  3.02k|  int32_t nbytes =  tud_msc_write10_cb(p_cbw->lun, lba, offset, _mscd_epbuf.buf, xferred_bytes);
  913|  3.02k|  if (nbytes != TUD_MSC_RET_ASYNC) {
  ------------------
  |  Branch (913:7): [True: 3.02k, False: 0]
  ------------------
  914|       |    p_msc->pending_io = false;
  915|  3.02k|    proc_write_io_data(p_msc, xferred_bytes, nbytes);
  916|  3.02k|  }
  917|  3.02k|}

fuzz.c:tu_bit_test:
  183|    891|TU_ATTR_ALWAYS_INLINE static inline bool     tu_bit_test (uint32_t value, uint8_t pos) { return (value & TU_BIT(pos)) ? true : false; }
  ------------------
  |  |   46|    891|#define TU_BIT(n)                     (1UL << (n))
  ------------------
  |  Branch (183:97): [True: 451, False: 440]
  ------------------
fuzz.c:tu_min32:
  188|  52.2k|TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_min32 (uint32_t x, uint32_t y) { return (x < y) ? x : y; }
  ------------------
  |  Branch (188:89): [True: 52.1k, False: 119]
  ------------------
fuzz.c:tu_unaligned_read32:
  242|  52.6k|TU_ATTR_ALWAYS_INLINE static inline uint32_t tu_unaligned_read32(const void *mem) {
  243|  52.6k|  tu_unaligned_uint32_t const *ua32 = (tu_unaligned_uint32_t const *) mem;
  244|  52.6k|  return ua32->val;
  245|  52.6k|}
fuzz.c:tu_unaligned_read16:
  252|  52.3k|TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_unaligned_read16(const void *mem) {
  253|  52.3k|  tu_unaligned_uint16_t const *ua16 = (tu_unaligned_uint16_t const *) mem;
  254|  52.3k|  return ua16->val;
  255|  52.3k|}
fuzz.c:tu_memcpy_s:
  118|     59|TU_ATTR_ALWAYS_INLINE static inline int tu_memcpy_s(void *dest, size_t destsz, const void *src, size_t count) {
  119|     59|  if (dest == NULL) {
  ------------------
  |  Branch (119:7): [True: 0, False: 59]
  ------------------
  120|      0|    return -1;
  121|      0|  }
  122|       |
  123|     59|  if (count == 0u) {
  ------------------
  |  Branch (123:7): [True: 0, False: 59]
  ------------------
  124|      0|    return 0;
  125|      0|  }
  126|       |
  127|     59|  if (src == NULL) {
  ------------------
  |  Branch (127:7): [True: 0, False: 59]
  ------------------
  128|      0|    return -1;
  129|      0|  }
  130|       |
  131|     59|  if (count > destsz) {
  ------------------
  |  Branch (131:7): [True: 0, False: 59]
  ------------------
  132|      0|    return -1;
  133|      0|  }
  134|       |
  135|     59|  (void) memcpy(dest, src, count);
  136|     59|  return 0;
  137|     59|}

usbd_edpt_xfer:
   16|  53.1k|bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t length, bool is_isr) {
   17|  53.1k|  (void) rhport;
   18|  53.1k|  (void) is_isr;
   19|  53.1k|  queued_ep = ep_addr;
   20|  53.1k|  queued_buffer = buffer;
   21|  53.1k|  queued_length = length;
   22|       |  return true;
   23|  53.1k|}
usbd_edpt_stall:
   36|    229|void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr) {
   37|    229|  (void) rhport;
   38|       |  ep_stalled[ep_addr >> 7] = true;
   39|    229|}
usbd_edpt_stalled:
   41|    536|bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr) {
   42|    536|  (void) rhport;
   43|    536|  return ep_stalled[ep_addr >> 7];
   44|    536|}
tud_msc_test_unit_ready_cb:
   82|     49|bool tud_msc_test_unit_ready_cb(uint8_t lun) {
   83|     49|  (void) lun;
   84|       |  return true;
   85|     49|}
tud_msc_capacity_cb:
   87|     19|void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) {
   88|     19|  (void) lun;
   89|     19|  *block_count = 1024;
   90|     19|  *block_size = 512;
   91|     19|}
tud_msc_read10_cb:
   93|  49.0k|int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) {
   94|  49.0k|  (void) lun;
   95|  49.0k|  (void) lba;
   96|  49.0k|  (void) offset;
   97|  49.0k|  memset(buffer, 0, bufsize);
   98|  49.0k|  return (int32_t) bufsize;
   99|  49.0k|}
tud_msc_write10_cb:
  102|  3.02k|                            uint8_t *buffer, uint32_t bufsize) {
  103|  3.02k|  (void) lun;
  104|  3.02k|  (void) lba;
  105|  3.02k|  (void) offset;
  106|  3.02k|  (void) buffer;
  107|  3.02k|  return (int32_t) bufsize;
  108|  3.02k|}
tud_msc_scsi_cb:
  110|     18|int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const command[16], void *buffer, uint16_t bufsize) {
  111|     18|  (void) lun;
  112|     18|  (void) command;
  113|     18|  (void) buffer;
  114|     18|  (void) bufsize;
  115|     18|  return -1;
  116|     18|}
LLVMFuzzerTestOneInput:
  118|    528|int LLVMFuzzerTestOneInput(uint8_t const *data, size_t size) {
  119|    528|  if (size < sizeof(msc_cbw_t)) return 0;
  ------------------
  |  Branch (119:7): [True: 8, False: 520]
  ------------------
  120|       |
  121|    520|  ep_stalled[0] = ep_stalled[1] = false;
  122|       |
  123|    520|  mscd_init();
  124|    520|  _mscd_itf.rhport = 0;
  125|    520|  _mscd_itf.ep_out = 0x01;
  126|    520|  _mscd_itf.ep_in = 0x81;
  127|    520|  prepare_cbw(&_mscd_itf);
  128|       |
  129|    520|  msc_cbw_t cbw;
  130|    520|  memcpy(&cbw, data, sizeof(cbw));
  131|    520|  cbw.signature = MSC_CBW_SIGNATURE;
  132|    520|  memcpy(queued_buffer, &cbw, sizeof(cbw));
  133|    520|  queued_length = 0;  // don't re-complete the CBW if nothing new was queued
  134|    520|  mscd_xfer_cb(0, 0x01, XFER_RESULT_SUCCESS, sizeof(cbw));
  135|       |
  136|    520|  size_t offset = sizeof(cbw);
  137|       |
  138|       |  // Allow enough steps for multi-block READ10/WRITE10 commands to reach their
  139|       |  // CSW (one CFG_TUD_MSC_EP_BUFSIZE chunk per step), with a hard cap to bound
  140|       |  // run time for fuzz-controlled total_bytes. The driver ends the loop by
  141|       |  // itself once the stage is back to MSC_STAGE_CMD or MSC_STAGE_NEED_RESET.
  142|    520|  unsigned max_steps = cbw.total_bytes / CFG_TUD_MSC_EP_BUFSIZE + 4;
  ------------------
  |  |   10|    520|#define CFG_TUD_MSC_EP_BUFSIZE 512
  ------------------
  143|    520|  if (max_steps > 1024) max_steps = 1024;
  ------------------
  |  Branch (143:7): [True: 309, False: 211]
  ------------------
  144|       |
  145|  52.8k|  for (unsigned step = 0; step < max_steps && _mscd_itf.stage != MSC_STAGE_CMD &&
  ------------------
  |  Branch (145:27): [True: 52.8k, False: 37]
  |  Branch (145:47): [True: 52.6k, False: 196]
  ------------------
  146|  52.6k|                          _mscd_itf.stage != MSC_STAGE_NEED_RESET; ++step) {
  ------------------
  |  Branch (146:27): [True: 52.6k, False: 0]
  ------------------
  147|  52.6k|    uint8_t ep = queued_ep;
  148|  52.6k|    uint16_t length = queued_length;
  149|  52.6k|    if (length == 0 || length > CFG_TUD_MSC_EP_BUFSIZE) break;
  ------------------
  |  |   10|  52.4k|#define CFG_TUD_MSC_EP_BUFSIZE 512
  ------------------
  |  Branch (149:9): [True: 144, False: 52.4k]
  |  Branch (149:24): [True: 0, False: 52.4k]
  ------------------
  150|  52.4k|    if (_mscd_itf.stage == MSC_STAGE_DATA && ep == 0x01) {
  ------------------
  |  Branch (150:9): [True: 52.2k, False: 196]
  |  Branch (150:46): [True: 3.17k, False: 49.0k]
  ------------------
  151|  3.17k|      if (offset + length > size) break;
  ------------------
  |  Branch (151:11): [True: 143, False: 3.03k]
  ------------------
  152|  3.03k|      memcpy(queued_buffer, data + offset, length);
  153|  3.03k|      offset += length;
  154|  3.03k|    }
  155|  52.3k|    queued_length = 0;
  156|  52.3k|    mscd_xfer_cb(0, ep, XFER_RESULT_SUCCESS, length);
  157|  52.3k|  }
  158|    520|  return 0;
  159|    528|}

