Skip to content

tests: usb: device_next: add test for incomplete control transfers with missing data and status stage #91203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

jfischer-no
Copy link
Collaborator

@jfischer-no jfischer-no commented Jun 6, 2025

Add test for incomplete control transfers with missing data and status stage.
Fix UDC buffer leak when the host omits control data stage.

@jfischer-no jfischer-no self-assigned this Jun 6, 2025
@jfischer-no jfischer-no added area: USB Universal Serial Bus Experimental Experimental features not enabled by default labels Jun 6, 2025
@github-actions github-actions bot requested a review from tmon-nordic June 6, 2025 17:48
@jfischer-no jfischer-no force-pushed the pr-device_next-test-control-request-handling branch from 8680cc2 to c1015f4 Compare June 6, 2025 21:54
Copy link
Collaborator

@tmon-nordic tmon-nordic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed to work on DWC2 in Completer mode (though it still fails in buffer DMA mode) when used together with #91281

@@ -73,7 +73,8 @@ int usbh_req_setup(struct usb_device *const udev,

memcpy(xfer->setup_pkt, &req, sizeof(req));

__ASSERT((buf != NULL && wLength) || (buf == NULL && !wLength),
__ASSERT(IS_ENABLED(CONFIG_ZTEST) ||
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Omitting asserts during tests suggests there is something seriously wrong with the implementation. Alternatively it suggests that the assert is incorrectly used.

* never reach the stack.
*/
LOG_INF("Drop setup packet (%p)", (void *)data->setup);
net_buf_unref(data->setup);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With DWC2 in Buffer DMA mode, it is possible for data->setup to be equal to buf. If this is the case, then the unref here does free buf and there is a NULL pointer dereference a while later.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can no longer trigger this with this PR rebased on top of main.

if (data->stage != CTRL_PIPE_STAGE_SETUP) {
LOG_INF("Sequence %u not completed", data->stage);

if (data->stage == CTRL_PIPE_STAGE_DATA_OUT) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With DWC2 in Buffer DMA mode, I can get the setup leak to happen with following data->stage values here:

  • CTRL_PIPE_STAGE_DATA_OUT
  • CTRL_PIPE_STAGE_STATUS_IN
  • CTRL_PIPE_STAGE_ERROR

@carlescufi carlescufi added this to the v4.2.0 milestone Jun 17, 2025
tmon-nordic
tmon-nordic previously approved these changes Jun 25, 2025
Copy link
Collaborator

@tmon-nordic tmon-nordic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR has to be rebased to fix conflicts.

carlescufi
carlescufi previously approved these changes Jun 25, 2025
@dkalowsk
Copy link
Collaborator

@jfischer-no please rebase

The previous setup packet reference will simply be overwritten when the
host omits data (OUT) stage. If a new setup packet arrives before the
previous data stage is complete, free the last setup packet buffer.

Signed-off-by: Johann Fischer <[email protected]>
Drop the queued control transfer when a new setup packet arrives.

Signed-off-by: Johann Fischer <[email protected]>
For testing purposes, allow the status stage to be omitted.

Signed-off-by: Johann Fischer <[email protected]>
Ignore assers in the control request handling when Kconfig option ZTEST
is enabled.

Signed-off-by: Johann Fischer <[email protected]>
Add a test for incomplete control transfers with missing data and status
stages.

Signed-off-by: Johann Fischer <[email protected]>
Add test for the string descriptors.

Signed-off-by: Johann Fischer <[email protected]>
Do not use qemu_cortex_m3 for the "build all" case, as it overflows and
provides no benefit over native_sim. Add native_sim/native/64 platform
to the tests.

Signed-off-by: Johann Fischer <[email protected]>
@jfischer-no jfischer-no dismissed stale reviews from carlescufi and tmon-nordic via 4401a0f June 27, 2025 10:49
@jfischer-no jfischer-no force-pushed the pr-device_next-test-control-request-handling branch from c1015f4 to 4401a0f Compare June 27, 2025 10:49
@jfischer-no jfischer-no added the bug The issue is a bug, or the PR is fixing a bug label Jun 27, 2025
Copy link

@carlescufi carlescufi requested a review from tmon-nordic June 27, 2025 11:05
@dkalowsk dkalowsk merged commit a168111 into zephyrproject-rtos:main Jun 27, 2025
29 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: USB Universal Serial Bus bug The issue is a bug, or the PR is fixing a bug Experimental Experimental features not enabled by default
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants