You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cartr opened this issue
Jun 27, 2019
· 2 comments
· Fixed by #5570
Labels
arch-arm32-bit ARMbugObserved behavior contradicts documented or intended behaviorstage1The process of building from source via WebAssembly and the C backend.
Prior to ARMv6, using LDR or LDRH with an unaligned address wouldn't load the data like you'd expect. (Depending on the situation, sometimes the result was "unpredictable", and sometimes it would mask off the lower bits of the address and use them to byte-rotate the result of the read.) But Zig can sometimes generate instructions that attempt to read unaligned constant addresses when compiling for ARMv4T and ARMv5. Here's a minimal example:
The ldrh instruction attempts to read a constant from __unnamed_2, which is not halfword-aligned, so incorrect data is loaded.
Although Zig's documentation does say that "[p]acked structs have 1-byte alignment", it seems kind of bad to generate code that silently does the wrong thing. It should maybe do something slower but safer, like using two LDRB instructions to read the data, when it isn't sure the pointer is aligned. Or maybe this would be solved by #1512?
The text was updated successfully, but these errors were encountered:
andrewrk
added
arch-arm
32-bit ARM
bug
Observed behavior contradicts documented or intended behavior
stage1
The process of building from source via WebAssembly and the C backend.
labels
Jun 27, 2019
I'll get spun up on this. If users wants to concern themselves with explicitly aligning their packed structs per #1512 then they may, but I think it's a fair prerequisite for the compiler to handle this case correctly with LDRB by default.
(Now that sub-architectures have been removed, you have to compile with -target arm-freestanding -mcpu=arm7tdmi to reproduce the issue. Here's an updated Compiler Explorer link: https://godbolt.org/z/c8Ku6e)
arch-arm32-bit ARMbugObserved behavior contradicts documented or intended behaviorstage1The process of building from source via WebAssembly and the C backend.
Prior to ARMv6, using
LDR
orLDRH
with an unaligned address wouldn't load the data like you'd expect. (Depending on the situation, sometimes the result was "unpredictable", and sometimes it would mask off the lower bits of the address and use them to byte-rotate the result of the read.) But Zig can sometimes generate instructions that attempt to read unaligned constant addresses when compiling for ARMv4T and ARMv5. Here's a minimal example:According to https://godbolt.org/z/Jrmak7 , this generates the following assembly when compiled for
armv5-freestanding
in Debug mode:Assembly
The
ldrh
instruction attempts to read a constant from__unnamed_2
, which is not halfword-aligned, so incorrect data is loaded.Although Zig's documentation does say that "[p]acked structs have 1-byte alignment", it seems kind of bad to generate code that silently does the wrong thing. It should maybe do something slower but safer, like using two
LDRB
instructions to read the data, when it isn't sure the pointer is aligned. Or maybe this would be solved by #1512?The text was updated successfully, but these errors were encountered: