#Fixing asan misaligment issues

27 messages · Page 1 of 1 (latest)

celest tulip
#

So I am pretty new to asan but I figured I would turn it on for my current project as it's actually supported and it's throwing a fair bit of warnings / errors. mainly in regard to memory loads / stores being misaligned

For example I get:

02:05:29    runtime error: store to misaligned address 0x020600000041 for type 'unsigned long', which requires 8 byte alignment
02:05:29    0x020600000041: note: pointer points here
02:05:29     00 00 00  01 be be be be be be be  be be be be be be be be  00 00 00 00 00 00 00 00  00 00 00 00 00

from the code ```cpp
template <class T>
inline void Write(const T& data)
{
EnsureHasRoom(sizeof(T));
((T)mWritePointer) = data;
mWritePointer += sizeof(T);

        mUsedSize = std::max(mUsedSize, GetPosition());
    }
where mWritePointer is an uint8_t*, I somewhat get what the issue is but I am really not sure how I would go about solving thse types of offisues
livid skiffBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

celest tulip
#

same with:

02:05:29     runtime error: member access within misaligned address 0x00020106886c for type '<redacted>', which requires 8 byte alignment
02:05:29    0x00020106886c: note: pointer points here
02:05:29      00 00 00 00 04 00 3c 00  6b 00 00 00 64 d4 5b 6e  ea 0a 00 00 01 00 00 00  76 8f 01 00 53 75 70 65

what would be from the folling lines:

        <redacted>* eventHeader = (<redacted>*)(inBlock.data() + readSize);
        readSize += eventHeader->size;
#

this mostly has to do with the fact I am working with buffers that essentially just store arbitrairy data that is either read or written to

#

Fixing asan misaligment issues

south merlin
celest tulip
south merlin
#
mWritePointer += reinterpret_cast<std::uintptr_t>(mWritePointer) % alignof(T);
celest tulip
#

right and same with reading

#

you align it to the next multiple

south merlin
#

this is kinda UB btw if you just reinterpret the pointer

#

use std::memcpy

celest tulip
#

ah, so doing a memcpy here would be fine?

south merlin
#
std::memcpy(mWritePointer, &data, sizeof(data));
celest tulip
#

that just removes any ub?

south merlin
#

in this case yeah

celest tulip
#

and then advance mWritePointer with sizeof(T)

south merlin
#

you advance the pointer, then memcpy

celest tulip
#

uh right, so you still need to align it?

south merlin
#

technically, you don't need to align it with std::memcpy but that's kinda dumb then since you can't access the object in the buffer

celest tulip
#
mWritePointer += reinterpret_cast<std::uintptr_t>(mWritePointer) % alignof(T);
memcpy
mWritePointer += sizeof(T);
#

so essentially this?

south merlin
#

if you just want it to be packed into the buffer, it's fine though

#

you can just use std::memcpy in that case

celest tulip
#

right 🤔 cause we generally pack into a buffer to serialize / compress it etc

#

and it's just arbitrairy data / alignment