From 9ded07cff6c73bd3ea1bbc874180139d3a5d6f0c Mon Sep 17 00:00:00 2001 From: Philip Hatcher Date: Tue, 25 Feb 2014 14:16:37 +0100 Subject: [PATCH] bionic: make epoll_event structure packed Description: In the kernel the epoll_event structure is packed in 64 bit kernel builds to allow the structure to be more easily compatible with 32 bit user space. As a result, when user space is 64-bit the structure must be packed as well. Add unit test to show the ptr alignment issue. Change-Id: I2c4848d5e38a357219091f350f9b6e3da05090da Signed-off-by: Philip Hatcher Signed-off-by: Fengwei Yin Reviewed-by: Hazarika, Prodyut Tested-by: Hazarika, Prodyut --- libc/include/sys/epoll.h | 6 +++++- tests/sys_epoll_test.cpp | 29 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/libc/include/sys/epoll.h b/libc/include/sys/epoll.h index c06a081f0..4a5a37c56 100644 --- a/libc/include/sys/epoll.h +++ b/libc/include/sys/epoll.h @@ -67,7 +67,11 @@ typedef union epoll_data { struct epoll_event { uint32_t events; epoll_data_t data; -}; +} +#ifdef __x86_64__ +__packed +#endif +; int epoll_create(int); int epoll_create1(int); diff --git a/tests/sys_epoll_test.cpp b/tests/sys_epoll_test.cpp index daa064a88..6e7a80786 100644 --- a/tests/sys_epoll_test.cpp +++ b/tests/sys_epoll_test.cpp @@ -17,8 +17,10 @@ #include #include +#include #include #include +#include TEST(sys_epoll, smoke) { int epoll_fd = epoll_create(1); @@ -37,3 +39,30 @@ TEST(sys_epoll, smoke) { sigaddset(&ss, SIGPIPE); ASSERT_EQ(0, epoll_pwait(epoll_fd, events, 1, 1, &ss)); } + +TEST(sys_epoll, epoll_event_data) { + int epoll_fd = epoll_create(1); + ASSERT_NE(-1, epoll_fd) << strerror(errno); + + int fds[2]; + ASSERT_NE(-1, pipe(fds)); + + const uint64_t expected = 0x123456789abcdef0; + + // Get ready to poll on read end of pipe. + epoll_event ev; + ev.events = EPOLLIN; + ev.data.u64 = expected; + ASSERT_NE(-1, epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fds[0], &ev)); + + // Ensure there's something in the pipe. + ASSERT_EQ(1, write(fds[1], "\n", 1)); + + // Poll. + epoll_event events[1]; + ASSERT_EQ(1, epoll_wait(epoll_fd, events, 1, 1)); + ASSERT_EQ(expected, events[0].data.u64); + + close(fds[0]); + close(fds[1]); +}