This is an archive of the discontinued LLVM Phabricator instance.

[libcxxabi] Fix alignment of allocated exceptions in 32 bit builds
ClosedPublic

Authored by EricWF on Oct 9 2016, 9:54 PM.

Details

Summary

In 32 bit builds on a 64 bit system std::malloc does not return correctly aligned memory. This leads to undefined behavior.

This patch switches to using posix_memalign to allocate correctly aligned memory instead.

Diff Detail

Event Timeline

EricWF updated this revision to Diff 74095.Oct 9 2016, 9:54 PM
EricWF retitled this revision from to [libcxxabi] Fix alignment of allocated exceptions in 32 bit builds.
EricWF updated this object.
EricWF added a subscriber: cfe-commits.
compnerd accepted this revision.Dec 5 2016, 10:16 AM
compnerd edited edge metadata.

I thought we had gotten this merged already. Oops. LGTM with the minor tweak for windows.

src/fallback_malloc.cpp
214

Given that libc++abi has some Windows support and usage (e.g. MinGW), we should change this to:

void *dest;
#if defined(_WIN32)
  if ((dest = _aligned_malloc(size, alignof(__aligned_type)) == nullptr)
    return fallback_malloc(size);
  return dest;
#else
  if (::posix_memalign(&dest, alignof(__aligned_type), size) == 0)
    return dest;
  return fallback_malloc(size);
#endif
This revision is now accepted and ready to land.Dec 5 2016, 10:16 AM
EricWF updated this revision to Diff 80658.Dec 7 2016, 2:10 PM
EricWF edited edge metadata.
  • Use _aligned_malloc on windows.
  • Ensure posix_memalign isn't called with a size of 0. I'm 99% sure that's currently impossible but we might as well handle it.

I'll commit after testing on OS X.

EricWF added a comment.Dec 7 2016, 7:45 PM

I ran into some test failures on OS X while attempting to commit this. The unwind.h header provided on OS X hasn't upstreamed the libunwind fix from r276215, meaning __cxa_exception is still under-aligned and therefore so is the exception.

I'll commit this in the next day or two after dealing with the apple bug.

EricWF updated this revision to Diff 90561.Mar 3 2017, 6:15 PM

Merge with master

EricWF closed this revision.Mar 3 2017, 6:16 PM