am 40e7a878: Merge "Prevent the madvise(MADV_MERGEABLE) mmap hack from affecting errno."

* commit '40e7a87864fee75f7af2caf8e1972d0d764135cd':
  Prevent the madvise(MADV_MERGEABLE) mmap hack from affecting errno.
This commit is contained in:
Elliott Hughes 2013-07-17 13:22:39 -07:00 committed by Android Git Automerger
commit e35d6c28df
2 changed files with 19 additions and 15 deletions

View file

@ -106,7 +106,6 @@ libc_common_src_files := \
bionic/md5.c \
bionic/memmem.c \
bionic/memswap.c \
bionic/mmap.c \
bionic/openat.c \
bionic/open.c \
bionic/pathconf.c \
@ -200,6 +199,7 @@ libc_bionic_src_files := \
bionic/__memcpy_chk.cpp \
bionic/__memmove_chk.cpp \
bionic/__memset_chk.cpp \
bionic/mmap.cpp \
bionic/pthread_attr.cpp \
bionic/pthread_detach.cpp \
bionic/pthread_equal.cpp \

View file

@ -25,26 +25,30 @@
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <unistd.h>
#include <errno.h>
#include <sys/mman.h>
#include <unistd.h>
extern void* __mmap2(void*, size_t, int, int, int, size_t);
#include "private/ErrnoRestorer.h"
#define MMAP2_SHIFT 12
void* mmap(void *addr, size_t size, int prot, int flags, int fd, long offset)
{
void *ret;
// mmap2(2) is like mmap(2), but the offset is in 4096-byte blocks, not bytes.
extern "C" void* __mmap2(void*, size_t, int, int, int, size_t);
if (offset & ((1UL << MMAP2_SHIFT)-1)) {
errno = EINVAL;
return MAP_FAILED;
}
#define MMAP2_SHIFT 12 // 2**12 == 4096
ret = __mmap2(addr, size, prot, flags, fd, (size_t)offset >> MMAP2_SHIFT);
void* mmap(void* addr, size_t size, int prot, int flags, int fd, long offset) {
if (offset & ((1UL << MMAP2_SHIFT)-1)) {
errno = EINVAL;
return MAP_FAILED;
}
if (ret && (flags & (MAP_PRIVATE | MAP_ANONYMOUS)))
madvise(ret, size, MADV_MERGEABLE);
void* result = __mmap2(addr, size, prot, flags, fd, offset >> MMAP2_SHIFT);
return ret;
if (result != MAP_FAILED && (flags & (MAP_PRIVATE | MAP_ANONYMOUS)) != 0) {
ErrnoRestorer errno_restorer;
madvise(result, size, MADV_MERGEABLE);
}
return result;
}