Commit a3b30763 authored by Greg Stark's avatar Greg Stark

Be conservative about alignment requirements of struct epoll_event.

Use MAXALIGN size/alignment to guarantee that later uses of memory are
aligned correctly. E.g. epoll_event might need 8 byte alignment on some
platforms, but earlier allocations like WaitEventSet and WaitEvent might
not sized to guarantee that when purely using sizeof().

Found by myself while testing on an Sun Ultra 5 (Sparc IIi) with some
editorializing by Andres Freund.

In passing fix a couple typos in the area
parent 4edb7bd2
...@@ -475,7 +475,7 @@ ResetLatch(volatile Latch *latch) ...@@ -475,7 +475,7 @@ ResetLatch(volatile Latch *latch)
/* /*
* Create a WaitEventSet with space for nevents different events to wait for. * Create a WaitEventSet with space for nevents different events to wait for.
* *
* These events can then efficiently waited upon together, using * These events can then be efficiently waited upon together, using
* WaitEventSetWait(). * WaitEventSetWait().
*/ */
WaitEventSet * WaitEventSet *
...@@ -485,35 +485,41 @@ CreateWaitEventSet(MemoryContext context, int nevents) ...@@ -485,35 +485,41 @@ CreateWaitEventSet(MemoryContext context, int nevents)
char *data; char *data;
Size sz = 0; Size sz = 0;
sz += sizeof(WaitEventSet); /*
sz += sizeof(WaitEvent) * nevents; * Use MAXALIGN size/alignment to guarantee that later uses of memory are
* aligned correctly. E.g. epoll_event might need 8 byte alignment on some
* platforms, but earlier allocations like WaitEventSet and WaitEvent
* might not sized to guarantee that when purely using sizeof().
*/
sz += MAXALIGN(sizeof(WaitEventSet));
sz += MAXALIGN(sizeof(WaitEvent) * nevents);
#if defined(WAIT_USE_EPOLL) #if defined(WAIT_USE_EPOLL)
sz += sizeof(struct epoll_event) * nevents; sz += MAXALIGN(sizeof(struct epoll_event) * nevents);
#elif defined(WAIT_USE_POLL) #elif defined(WAIT_USE_POLL)
sz += sizeof(struct pollfd) * nevents; sz += MAXALIGN(sizeof(struct pollfd) * nevents);
#elif defined(WAIT_USE_WIN32) #elif defined(WAIT_USE_WIN32)
/* need space for the pgwin32_signal_event */ /* need space for the pgwin32_signal_event */
sz += sizeof(HANDLE) * (nevents + 1); sz += MAXALIGN(sizeof(HANDLE) * (nevents + 1));
#endif #endif
data = (char *) MemoryContextAllocZero(context, sz); data = (char *) MemoryContextAllocZero(context, sz);
set = (WaitEventSet *) data; set = (WaitEventSet *) data;
data += sizeof(WaitEventSet); data += MAXALIGN(sizeof(WaitEventSet));
set->events = (WaitEvent *) data; set->events = (WaitEvent *) data;
data += sizeof(WaitEvent) * nevents; data += MAXALIGN(sizeof(WaitEvent) * nevents);
#if defined(WAIT_USE_EPOLL) #if defined(WAIT_USE_EPOLL)
set->epoll_ret_events = (struct epoll_event *) data; set->epoll_ret_events = (struct epoll_event *) data;
data += sizeof(struct epoll_event) * nevents; data += MAXALIGN(sizeof(struct epoll_event) * nevents);
#elif defined(WAIT_USE_POLL) #elif defined(WAIT_USE_POLL)
set->pollfds = (struct pollfd *) data; set->pollfds = (struct pollfd *) data;
data += sizeof(struct pollfd) * nevents; data += MAXALIGN(sizeof(struct pollfd) * nevents);
#elif defined(WAIT_USE_WIN32) #elif defined(WAIT_USE_WIN32)
set->handles = (HANDLE) data; set->handles = (HANDLE) data;
data += sizeof(HANDLE) * nevents; data += MAXALIGN(sizeof(HANDLE) * nevents);
#endif #endif
set->latch = NULL; set->latch = NULL;
...@@ -616,7 +622,7 @@ AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch, ...@@ -616,7 +622,7 @@ AddWaitEventToSet(WaitEventSet *set, uint32 events, pgsocket fd, Latch *latch,
if (set->latch) if (set->latch)
elog(ERROR, "cannot wait on more than one latch"); elog(ERROR, "cannot wait on more than one latch");
if ((events & WL_LATCH_SET) != WL_LATCH_SET) if ((events & WL_LATCH_SET) != WL_LATCH_SET)
elog(ERROR, "latch events only spuport being set"); elog(ERROR, "latch events only support being set");
} }
else else
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment