Adding one more detail: I'm creating an instantiation of class A using new like this:
A* a = new A(); Do I have to do something special with the new call to make sure it's 64 byte aligned? Can I solve this problem in the definition of A somehow? On Monday, November 26, 2018 at 10:39:46 AM UTC-5, [email protected] wrote: > > Thanks. Regarding your second point about how to align the aggregate: just > to make sure I understand, is the recommended approach something like this, > assuming class A is the aggregate that contains class C the child, and > class C's alignment is 64 bytes: > > class alignas(64) A { > C member_var; > /* class definition continued */ > }; > > > > However when I did this, I still got the same UBSAN error. So, I'm not > sure how to actually align the address of objects of type A. I would think > that specifying the alignment in the definition of class A would handle > this, but it doesn't. Note that the way I'm creating objects of class A is > via the new operator. > > Any pointers here would be helpful. > > > > On Wednesday, October 10, 2018 at 3:55:06 PM UTC-4, Reid Kleckner wrote: >> >> Generally, I think ubsan is discussed on the main clang list, cfe-dev. I >> don't think there's a separate group for it, since it's fairly low >> maintenance, and has more to do with checking fidly C++ language rules like >> this. >> >> In any case, this seems pretty straightforward. If you request a certain >> field alignment, that will raise the minimum required alignment for the >> aggregate, and clang emits a ubsan check for that. >> >> It seems like the character arrays are enough to prevent false sharing. >> If you put another padding array between 'r' and 'w' you shouldn't need to >> worry about false sharing between them. >> >> On Wed, Oct 10, 2018 at 11:17 AM <[email protected]> wrote: >> >>> >>> (I'm posting this question about UBSAN on the ASAN list because I >>> couldn't find a UBSAN list; let me know if there is one.) >>> >>> Hello, I have a pretty large C++11 program that I just ran UBSAN on and >>> I'm getting errors related to misaligned addresses and alignments. It's >>> non-trivial to show a sample program so I'm hoping I can get pointed in the >>> right direction without that for now. >>> >>> The class C in question has the following characteristics: >>> >>> >>> - Class C itself doesn't do anything special regarding memory >>> alignment. No posix_memalign, no alignas, etc. >>> - Class C, however, has a member that is of class M >>> - Class M does some alignment for some members using alignas. It >>> looks like this: >>> >>> #define FALSE_SHARING_RANGE 128 >>> >>> class M { >>> char pad0_[FALSE_SHARING_RANGE]; >>> alignas(FALSE_SHARING_RANGE) std::atomic<unsigned int> r; >>> alignas(FALSE_SHARING_RANGE) std::atomic<unsigned int> w; >>> char pad1_[FALSE_SHARING_RANGE - sizeof(w)]; >>> >>> void* v1; >>> const uint32_t s; >>> const uint32_t b; >>> const uint32_t pb; >>> } >>> >>> >>> Here are the UBSAN errors: >>> >>> runtime error: constructor call on misaligned address 0x000003330ca0 for >>> type 'C', which requires 128 byte alignment >>> 0x000003330ca0: note: pointer points here >>> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 >>> 00 00 00 00 00 00 00 00 00 >>> ^ >>> runtime error: constructor call on misaligned address 0x000003330ca0 for >>> type 'C *', which requires 128 byte alignment >>> 0x000003330ca0: note: pointer points here >>> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 >>> 00 00 00 00 00 00 00 00 00 >>> ^ >>> runtime error: member call on misaligned address 0x000003330ca0 for >>> type 'C *', which requires 128 byte alignment >>> 0x000003330ca0: note: pointer points here >>> >>> >>> 00 00 00 00 85 eb 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 >>> 00 00 00 00 00 00 00 00 00 >>> ^ >>> ... similar errors continue >>> >>> Note that when the alignas specifiers on r and w are removed, the UBSAN >>> errors go away. >>> >>> I'd greatly appreciate if someone can explain 1) why this is occuring; >>> 2) and how to properly write the code to make the error go away. (Note: my >>> motivation for the alignas attributes is to prevent false sharing between r >>> and w as this class is used by concurrent threads.) >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "address-sanitizer" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> For more options, visit https://groups.google.com/d/optout. >>> >> -- You received this message because you are subscribed to the Google Groups "address-sanitizer" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
