-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
Replace type punning with memcpy. #7415
Conversation
The type punning in the existing code is undefined behaviour in C. In particular, the existing code fails when running on Arm Cortex-M devices. On Cortex-M, accessing a uint64_t that is not 8-byte aligned generates a hard fault. Change-Id: I2aecaa220e581af7c91a8bc7886499d70e2aa6f2
@grant-arm this looks great! want to add a unit test in |
@areusch can you explain a bit more on how to test the changes brought in this PR? |
@liangfu @grant-arm it seems like we should be able to call |
@areusch I think we can still call TVMNDArray_Load() with strm non-aligned. Its just that when de-referencing a pointer to non word-aligned location -- we should do it under memcpy so that it does what required based on the target architecture. Inside TVMNDArray_Load, I think data is already accessed via memcpy and its just some metadata such as shape are the ones directly de-referenced. Was that your concern ? I think this might trigger a compiler warning (-Wcast-align) and a runtime error in some platforms. Therefore, depending on whether this fix clears away all cast-align warnings, we might be able to use -Wcast-align=strict which makes the warning appear for x86 too, if we are using gcc 8 or higher. If this is the case, we might be able to create a failing compilation test case using build_static_runtime ? |
@manupa-arm @grant-arm it seems to me that this change is replacing pointer dereferences to now as to producing unaligned |
Thank you for the feedback @areusch . I'm going to take a look at implementing a unit test along the lines of your suggestion. |
@areusch @grant-arm, what I proposed was some trick to make it also a compiler failure using -Werror with -Wcast-align=strict. This way the compiler will not allow such code to be compiled in the first place. What do you think ? |
@manupa-arm i'm not super-familiar with |
Sorry to jump in but I think a couple of points need to be considered here. I will point out that once we start running tests on Corstone 300 (Cortex-M + Ethos-U), note that the runtime will run on the Cortex-M and publish that upstream in a month or so, there will be a test on a target in the CI that actually breaks for this. After all if the runtime fails with misaligned faults it's fairly fundamental to running any tests. -Wcast-align=strict works well across all architectures as an option from GCC-8 ( notably it wouldn't work on x86_64 even if the variant of -Wcast-align=strict was setup in GCC 7 as IIRC x86_64 is not a STRICT_ALIGNMENT target in GCC parlance). The default compiler in CI is from Ubuntu 18.04 and thus will not show up on x86_64 in GCC-7 . $> gcc -Wcast-align=strict /tmp/tst.c I would suggest fixing the issue, it's undefined C . Yes it's nice to have a test but that will come automatically with testing initial support for Ethos-U55 in the Corstone300 FVP or any Cortex-M test that runs on an FVP. Thanks, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@u99127 @grant-arm okay sorry to delay this one--for some reason I was under the impression x86_64 raised SIGBUS on unaligned accesses. looks like i'm dead wrong on that. i agree given that's false, a unit test would be hard to write, LGTM. thanks for fixing this!
@tmoreau89 want to merge? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
The type punning in the existing code is undefined behaviour in C. In particular, the existing code fails when running on Arm Cortex-M devices. On Cortex-M, accessing a uint64_t that is not 8-byte aligned generates a hard fault. Change-Id: I2aecaa220e581af7c91a8bc7886499d70e2aa6f2
The type punning in the existing code is undefined behaviour in C. In particular, the existing code fails when running on Arm Cortex-M devices. On Cortex-M, accessing a uint64_t that is not 8-byte aligned generates a hard fault. Change-Id: I2aecaa220e581af7c91a8bc7886499d70e2aa6f2
The type punning in the existing code is undefined behaviour in C.
In particular, the existing code fails when running on Arm Cortex-M devices.
On Cortex-M, accessing a uint64_t that is not 8-byte aligned generates a hard fault.
The GCC bugs page (https://gcc.gnu.org/bugs/) Non-Bugs/C/Casting section provides a good explanation of why "Dereferencing a pointer that violates the aliasing rules results in undefined behavior."
@manupa-arm @areusch @liangfu