Closed TheMalkavien closed 1 month ago
It looks sane to me, though memory alignment isn't something I'm used to worrying about (obviously). I should have a rp2040 platform by the end of today to test with.
I'm wondering whether we should align the radiobuf
(used when receiving) as well:
https://github.com/meshtastic/firmware/blob/9bebad2dbe3650c133d6efac5e028d52ce7fa12a/src/mesh/RadioInterface.h#L94
As we're doing pointer arithmetic for the payload afterwards: https://github.com/meshtastic/firmware/blob/9bebad2dbe3650c133d6efac5e028d52ce7fa12a/src/mesh/RadioLibInterface.cpp#L392
Hi,
I'd say, every structure should be aligned for performance at least. I'm rusty, but I found it strange that the compiler does not align global variables by default.
Anyway, in your example, even if the start of the buffer is properly aligned, there is a risk that if sizeof(PacketHeader) is not modulo "word", your pointer will be not aligned. I'ts not a problem if you use it byte by byte, but using it with a dereferrence will mostly cause problems.
Everything I say must be verified, I'm not myself an expert in memory alignment or gcc specificities.
@TheMalkavien Thanks, makes sense. We force sizeof(PacketHeader)
to be 16, but that might thus be an issue as well. Not sure how far we should go with this, but at least this PR looks good to me.
Hi,
As discussed into the #4855 issue, here is my humble PR.
It fixes #4855 by avoiding the use of non aligned pointers. On some hardware, we do not care at all, but for some the result is not guaranteed, and it makes some hardware crash (rp2040 crashes, Heltecv3 PKI_FAILED, ...).
There is 2 main changes :
As discussed, these two changes are a quick way to fix crashes or bugs linked to the non-alignment of some objects. It will probably not fix all the alignment problems in the future (or even in the present), but it should be considered to avoid such difficult to debug behavior. Maybe the compiler can provide some useful flags for that, and of course be very careful when using pointers (as usual ? :p ).
Tested and working on RP2040-lora and Heltecv3
Thanks !