-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix left shift overflow when preparing endpoints
There is an overflowing left shift due to the interplay between the driver and the state module. The endpoint addresses produced by the driver could result in a left-shift overflow, and subsequent panic, back in the state module (`check_allocated`). The fault is the usage of the `capacity()` call, which returns the number of endpoints (up to 16), not the endpoint indices (0-7, inclusive). The error happens when `capacity() >= 8`: 1. When enabling endpoints, `all_ep_addrs` produces an invalid `EndpointAddress` with index 8 (or more). 2. `check_allocated` caller produces a raw index (using `index`), calculated to be at least `2 * 8 = 16`. 3. `check_allocated` left shifts `1 << 16` and panics. The commit refactors the modules, moving the iterator construction into the endpoint allocator. The allocator ensures that the accessed endpoints are valid and won't cause a panic on access. The commit updates the driver accordingly. The approach is more amenable to unit testing, and the commit includes extra asserts to show endpoint allocation and iteration does not panic. The same overflow could have happened during endpoint allocation, depending on the address supplied by the caller. This commit refactors that method, too. Note that this commit slightly changes the `enable_endpoints` behavior. Specifically, we won't enable the control endpoint. This is intentional; EP0 control IN / OUT are always enabled in hardware, so we don't need the software call. The Teensy 4 examples should have demonstrated this panic. And I was able to demonstrate the issue with a debug build just before this commit. I likely only tested release builds when preparing the release, and release builds hide this defect. To prevent this in the future, I'm enabling overflow checks in release builds. I tested this by building debug builds of the serial example on the Teensy 4, and ensuring that it did not panic. The test_class continues to pass the usb-device test suite.
- Loading branch information
Showing
4 changed files
with
103 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,6 @@ members = [ | |
|
||
[workspace.package] | ||
edition = "2021" | ||
|
||
[profile.release] | ||
overflow-checks = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters