Skip to content

Fix late transfer free's - #1073

Closed
benzea wants to merge 1 commit into
libusb:masterfrom
benzea:benzea/fix-late-transfer-free
Closed

benzea wants to merge 1 commit into
libusb:masterfrom
benzea:benzea/fix-late-transfer-free

Conversation

@benzea

@benzea benzea commented Feb 15, 2022

Copy link
Copy Markdown

The log handler in libusb_free_transfer now tries to get the context. However, that requires a pointer to the device, which may either be NULL already (do_close in core.c does this), or may currently even be an invalid pointer if the device for a non in-flight transfer was closed.

This PR fixes both issues.

@benzea

benzea commented Feb 15, 2022 •

Copy link
Copy Markdown
Author

btw. I caught this because the libfprint CI tests suddenly started to fail (after the Fedora update to 1.0.25 which includes #1058). For these tests, we use umockdev, which allows us to replay (or emulate) arbitrary USB transfers. What umockdev offers specifically is:

  • Ability to intercept ioctl/read/write calls
  • Using this, it has two USB replayers available:
    • A very simple one, that can't handle control transfers though
    • A more complex one working with usbmon (wireshark) captures
  • API allowing us to emulate ioctl's from a separate process (using C, python or other languages)

Now, I was wondering how big the interest is in having umockdev based tests inside libusb proper. Note that there is the caveat that the poll behaviour is not properly emulated.

@tormodvolden

Copy link
Copy Markdown
Contributor

We'd sure love to have some automatic testing with device emulation.

@mcuee mcuee added the core Related to common codes label Feb 16, 2022
@mcuee

mcuee commented Feb 16, 2022

Copy link
Copy Markdown
Member

Now, I was wondering how big the interest is in having umockdev based tests inside libusb proper. Note that there is the caveat that the poll behaviour is not properly emulated.

Yes, for sure that will be very good.

@benzea

benzea commented Feb 17, 2022

Copy link
Copy Markdown
Author

Eeks. This breaks the synchronous API. It uses transfer->dev_handle == NULL to decide whether the device was closed in the meantime, rather than the transfer completing successfully.

Not sure what to do about that right now. The straight forward but ugly thing would to track all transfers in dev_handle that were not free'ed yet. Then one can clear dev_handle in device close.

@benzea

benzea commented Feb 17, 2022

Copy link
Copy Markdown
Author

Or, well, we change the check in sync_transfer_wait_for_completion. But I am not sure in how far this is an API break.

@benzea
benzea force-pushed the benzea/fix-late-transfer-free branch from 6428090 to 2845a87 Compare February 17, 2022 16:49
@benzea

benzea commented Feb 17, 2022 •

Copy link
Copy Markdown
Author

OK, here is the fancy patch, which ensures that libusb_free_transfer has the device and therefore the context when running.

The simple version is to just replace TRANSFER_CTX(transfer) with NULL.

Another alternative could be to keep the previous patch but change the logic in sync_transfer_wait_for_completion . But, I am not sure what assumptions other API users might make about dev_handle.

Comment thread libusb/libusbi.h Resolved
transfer->dev_handle currently has the behaviour that it will be unset
if the device is closed. The sync API uses this fact to catch an error
case.

In other cases, transfer->dev_handle will keep its value, which means
that if the transfer lives longer than the device handle, the pointer
becomes invalid.

The transfer does however keep a reference to the device, which owns the
pointer to the context. As such, we can track this reference internal to
the transfer, and it is set while the transfer is in-flight.

With this, switch the logging infrastructure to use itransfer->dev->ctx
while checking that itransfer->dev is non-NULL.

Note that this was a regression caused by 6cae9c6 ("core: update
usbi_dbg to take the context as an argument"), specifically when
resolving the context while freeing a transfer after closing a device.

Note that the transfer will now keep a reference to the device until it
is free'ed. This allows it to use the correct context for logging even
in libusb_free_transfer.

The alternative to all this would be to just explicitly pass NULL to the
log handler in libusb_free_transfer.

Closes: libusb#1038
@benzea
benzea force-pushed the benzea/fix-late-transfer-free branch from d05c436 to 010691f Compare March 2, 2022 20:30
classabbyamp added a commit to classabbyamp/void-packages that referenced this pull request Mar 22, 2022
paper42 pushed a commit to void-linux/void-packages that referenced this pull request Mar 22, 2022
Sapein pushed a commit to Sapein/void-packages that referenced this pull request Mar 22, 2022
ftrvxmtrx pushed a commit to ftrvxmtrx/void-packages that referenced this pull request Mar 23, 2022
akierig pushed a commit to akierig/void-packages that referenced this pull request Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Related to common codes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Segfault on Homebrew libusb-1.0.25 libusb_free_transfer()

4 participants