Closed Luthaf closed 3 years ago
Adding -fsanitize=undefined
to the compiler flags gives something potentially related (same file/line)?
mmtf-cpp/include/mmtf/binary_decoder.hpp:124:25: runtime error: load of misaligned address 0x55a301b045f1 for type 'uint32_t', which requires 4 byte alignment
0x55a301b045f1: note: pointer points here
4c 69 73 74 00 00 00 04 00 00 00 34 00 00 00 00 00 00 00 13 00 00 00 05 00 00 00 28 00 00 00 1b
^
mmtf-cpp/include/mmtf/binary_decoder.hpp:124:25: runtime error: load of misaligned address 0x55a301b045f5 for type 'uint32_t', which requires 4 byte alignment
0x55a301b045f5: note: pointer points here
00 00 00 04 00 00 00 34 00 00 00 00 00 00 00 13 00 00 00 05 00 00 00 28 00 00 00 1b 00 00 00 3d
^
mmtf-cpp/include/mmtf/binary_decoder.hpp:124:25: runtime error: load of misaligned address 0x55a301b045f9 for type 'uint32_t', which requires 4 byte alignment
0x55a301b045f9: note: pointer points here
00 00 00 34 00 00 00 00 00 00 00 13 00 00 00 05 00 00 00 28 00 00 00 1b 00 00 00 3d 00 00 00 30
^
mmtf-cpp/include/mmtf/binary_decoder.hpp:124:25: runtime error: load of misaligned address 0x55a301b045fd for type 'uint32_t', which requires 4 byte alignment
0x55a301b045fd: note: pointer points here
00 00 00 00 00 00 00 13 00 00 00 05 00 00 00 28 00 00 00 1b 00 00 00 3d 00 00 00 30 00 00 00 53
^
mmtf-cpp/include/mmtf/binary_decoder.hpp:128:25: runtime error: load of misaligned address 0x55a301b0c9c1 for type 'uint16_t', which requires 2 byte alignment
0x55a301b0c9c1: note: pointer points here
00 00 00 64 03 b4 00 8c 00 00 00 00 00 00 ff 74 00 8c 01 e5 00 00 00 00 00 00 00 00 00 00 00 00
Just so I understand this correctly: does the code work if you drop the -march=native
?
Yes. Also, I am compiling in Release mode (-O3), I'll edit the first post with this.
Looks like the issue was undefined behavior of type punning in C++ (it breaks strict aliasing). The patch below fixes the issue for me (it should have the same performace, the compiler can recognize calls to memcpy)
diff --git a/include/mmtf/binary_decoder.hpp b/include/mmtf/binary_decoder.hpp
index 00b39bb..84d40b8 100644
--- a/include/mmtf/binary_decoder.hpp
+++ b/include/mmtf/binary_decoder.hpp
@@ -121,11 +121,17 @@ namespace {
#ifndef __EMSCRIPTEN__
void assignBigendian4(void* dst, const char* src) {
- *((uint32_t*)dst) = ntohl(*((uint32_t*)src));
+ uint32_t tmp;
+ std::memcpy(&tmp, src, sizeof(uint32_t));
+ tmp = ntohl(tmp);
+ std::memcpy(dst, &tmp, sizeof(uint32_t));
}
void assignBigendian2(void* dst, const char* src) {
- *((uint16_t*)dst) = ntohs(*((uint16_t*)src));
+ uint16_t tmp;
+ std::memcpy(&tmp, src, sizeof(uint16_t));
+ tmp = ntohs(tmp);
+ std::memcpy(dst, &tmp, sizeof(uint16_t));
}
#else
// Need to avoid how emscripten handles memory
@Luthaf the issue has been fixed in the master branch now. Thanks once again for bringing this to our attention.
Thanks for fixing this!
Thanks, Gerardo!
On Fri, Oct 23, 2020 at 6:15 AM Guillaume Fraux notifications@github.com wrote:
Thanks for fixing this!
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://urldefense.com/v3/__https://github.com/rcsb/mmtf-cpp/issues/36*issuecomment-715333578__;Iw!!Mih3wA!VpPKXwUYz-5L5l8WXpPPh5GH3C8d0qxq0G_kVDKDZGEalhGfjw4EUgyPPhCAwaY$, or unsubscribe https://urldefense.com/v3/__https://github.com/notifications/unsubscribe-auth/AA53AEEZCOWE7QHOWBGU2P3SMF6XTANCNFSM4KXF4H2A__;!!Mih3wA!VpPKXwUYz-5L5l8WXpPPh5GH3C8d0qxq0G_kVDKDZGEalhGfjw4EUgyPl60RaxQ$ .
OS: Ubuntu 18.04 LTS Compiler: GCC 7.4.0 (7.4.0-1ubuntu1~18.04.1) CPU: Intel(R) Core(TM) i5-4460S CPU @ 2.90GHz
Compiling the code with
-march=native
segfaults in the functions dealing with endianness. To reproduce:Here is the corresponding LLDB session
I have no idea what is going on here =/