AnsweredAssumed Answered

fsl_usdhc.c USDHC_ReceiveCommandResponse() miscompile

Question asked by Buck Fobb on Jun 11, 2019
Latest reply on Jun 14, 2019 by Kerry Zhou

I could be wrong but it seems gcc (any version) with -O3 enabled miscompiles fsl_usdhc.c USDHC_ReceiveCommandResponse(). This appears to be an issue with gcc. Gcc doesn't properly generate the asm code; gcc seems to only partially implement the function, and does not return, thus barging right into the next function that gcc places. At least on my builds.

 

However, it seems like NXP should refactor the function so that it doesn't cause gcc to barf on it.

 

I think you should be able to repro this with the sdmmc example projects.

 

Additionally, here's a link to a Compiler Explorer example: Compiler Explorer . It is the USDHC_ReceiveCommandResponse() function verbatim, with a contrived example, hoping to show what's going on. Once the assembly gets past "if (command->responseType == kCARD_ResponseTypeR2)" it doesn't return.

 

If you do come up with a refactor that works, please post/share it. I don't have a solution as yet.

 

Hope this helps.

 

PS: I should also point out that your implementation of the do-while loop in there is incorrect: since it is a post decrement, the do loop is executed with i = 0, which causes "command->response[i - 1U] <<= 8U;" to write out-of-bounds.

Outcomes