Skip to content
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

mchp: Remove undefined behavior in espi msvw handlers #22

Merged
merged 1 commit into from May 10, 2023

Conversation

DHowett
Copy link
Contributor

@DHowett DHowett commented Dec 19, 2022

The code in espi_msvw[12]_interrupt relies on undefined behavior today. __builtin_ctz is specified as returning values in the range [0, 31], but we are checking for 32.

This behavior may be unexpected compared to the CTZ/CLZ instruction on ARM, which use the value 32 to indicate that there are no ones in the provided input.

GCC 11+ optimizes the two loops below into infinite loops, as it can see that the condition will never be met.

After this change, the disassembly of espi_mswv1_interrupt can be confirmed to contain an exit behind a branch.

... // r4 is loaded with girq24_result and has bits successively cleared
1a: b90c cbnz r4, 20 <espi_mswv1_interrupt+0x20>
1c: e8bd 81f0 ldmia.w sp!, {r4, r5, r6, r7, r8, pc}
20: fa94 f5a4 rbit r5, r4
...

BUG=EmbeddedController#21
BRANCH=hx20-hx30
TEST=Examined the disassembly for espi_msvw[12]_interrupt; see above

Signed-off-by: Dustin L. Howett dustin@howett.net

@DHowett

This comment was marked as resolved.

@kiram9 kiram9 changed the base branch from hx20 to hx20-hx30 January 4, 2023 23:46
@DHowett DHowett changed the title [hx20] mchp: Remove undefined behavior in espi msvw handlers mchp: Remove undefined behavior in espi msvw handlers Jan 5, 2023
The code in espi_msvw[12]_interrupt relies on undefined behavior today.
__builtin_ctz is specified as returning values in the range [0, 31], but
we are checking for 32.

This behavior may be unexpected compared to the CTZ/CLZ instruction on
ARM, which use the value 32 to indicate that there are no ones in the
provided input.

GCC 11+ optimizes the two loops below into infinite loops, as it can see
that the condition will never be met.

After this change, the disassembly of espi_mswv1_interrupt can be
confirmed to contain an exit behind a branch.

   ... // r4 is loaded with girq24_result and has bits successively cleared
   1a:   b90c            cbnz    r4, 20 <espi_mswv1_interrupt+0x20>
   1c:   e8bd 81f0       ldmia.w sp!, {r4, r5, r6, r7, r8, pc}
   20:   fa94 f5a4       rbit    r5, r4
   ...

BUG=EmbeddedController#21
BRANCH=hx20-hx30
TEST=Examined the disassembly for espi_msvw[12]_interrupt; see above

Signed-off-by: Dustin L. Howett <dustin@howett.net>
@DHowett
Copy link
Contributor Author

DHowett commented Jan 23, 2023

@kiram9 kiram9 merged commit 5374ce8 into FrameworkComputer:hx20-hx30 May 10, 2023
@github-actions github-actions bot locked and limited conversation to collaborators May 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants