diff --git a/ipc/chromium/src/base/pickle.cc b/ipc/chromium/src/base/pickle.cc index 65c8a26ffca1..3087a2597073 100644 --- a/ipc/chromium/src/base/pickle.cc +++ b/ipc/chromium/src/base/pickle.cc @@ -79,6 +79,8 @@ struct Copier { template struct Copier { static void Copy(T* dest, const char* iter) { + // The pointer ought to be properly aligned. + DCHECK_EQ((((uintptr_t)iter) & (MOZ_ALIGNOF(T) - 1)), 0); *dest = *reinterpret_cast(iter); } }; @@ -609,8 +611,22 @@ bool Pickle::WriteBytesZeroCopy(void* data, uint32_t data_len, uint32_t capacity) { BeginWrite(data_len, sizeof(memberAlignmentType)); + uint32_t new_capacity = AlignInt(capacity); +#ifndef MOZ_MEMORY + if (new_capacity > capacity) { + // If the buffer we were given is not large enough to contain padding + // after the data, reallocate it to make it so. When using jemalloc, + // we're guaranteed the buffer size is going to be at least 4-bytes + // aligned, so we skip realloc altogether. Even with other allocators, + // the realloc is likely not necessary, but we don't take chances. + // At least with ASan, it does matter to realloc to inform ASan we're + // going to use more data from the buffer (and let it actually realloc + // if it needs to). + data = realloc(data, new_capacity); + } +#endif buffers_.WriteBytesZeroCopy(reinterpret_cast(data), data_len, - capacity); + new_capacity); EndWrite(data_len); return true;