Page MenuHomeFreeBSD

bhyve: Avoid triggering false -Wfree-nonheap-object warnings.
ClosedPublic

Authored by jhb on Sep 29 2022, 10:40 PM.
Tags
None
Referenced Files
Unknown Object (File)
Oct 4 2024, 3:22 AM
Unknown Object (File)
Oct 4 2024, 3:18 AM
Unknown Object (File)
Oct 4 2024, 3:17 AM
Unknown Object (File)
Oct 4 2024, 1:36 AM
Unknown Object (File)
Oct 2 2024, 12:51 PM
Unknown Object (File)
Oct 1 2024, 7:49 PM
Unknown Object (File)
Oct 1 2024, 1:40 PM
Unknown Object (File)
Sep 25 2024, 5:18 AM
Subscribers

Details

Summary

XHCI port and slot numbers are 1-based rather than 0-based. To handle
this, bhyve was subtracting one item from the pointers saved in the
softc so that index 1 accessed index 0 of the allocated array.

However, this is UB and confused GCC 12. The compiler noticed that
the calls to free() were using an offset and emitted a warning.
Rather than storing UB pointers in the softc, push the decrement
operation into the existing macros that wrap accesses to the relevant
arrays.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

jhb requested review of this revision.Sep 29 2022, 10:40 PM

Can we avoid the UB by simply allocating an extra slot in each array? This trick is saving 16 bytes of memory per xhci instance...

This revision is now accepted and ready to land.Sep 30 2022, 2:41 PM

Can we avoid the UB by simply allocating an extra slot in each array? This trick is saving 16 bytes of memory per xhci instance...

Well, this is even safe on CHERI (too much C uses tricks like this). But we could allocate an extra one perhaps and just leave index 0 unused. We could also perhaps just adjust the references, especially if they are all wrapped via macros like XHCI_DEVINST_PTR() which could deal with subtracting one from the index.

This revision now requires review to proceed.Dec 8 2022, 11:52 PM
This revision is now accepted and ready to land.Dec 9 2022, 6:51 AM
usr.sbin/bhyve/pci_xhci.c
294

These should technically have parens around the expansion.

2833–2834

We're still using the old trick for portregs - probably it's better to be consistent one way or the other?

usr.sbin/bhyve/pci_xhci.c
2833–2834

Oh, yes, agreed.

jhb marked 2 inline comments as done.Dec 23 2022, 8:59 PM

Fix portregs as well and add parens

This revision now requires review to proceed.Dec 24 2022, 8:23 PM
This revision is now accepted and ready to land.Jan 9 2023, 10:06 AM