2009-03-04 04:28:35 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
2013-07-17 22:12:26 +02:00
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
#include <errno.h>
|
2015-11-07 16:40:26 +01:00
|
|
|
#include <stdint.h>
|
2009-03-04 04:28:35 +01:00
|
|
|
#include <sys/mman.h>
|
2013-07-17 22:12:26 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-08-04 06:48:41 +02:00
|
|
|
#include "private/bionic_macros.h"
|
2013-07-17 22:12:26 +02:00
|
|
|
#include "private/ErrnoRestorer.h"
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-07-17 22:12:26 +02:00
|
|
|
// 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);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-07-17 22:12:26 +02:00
|
|
|
#define MMAP2_SHIFT 12 // 2**12 == 4096
|
2013-05-18 02:08:41 +02:00
|
|
|
|
2014-03-03 20:54:21 +01:00
|
|
|
static bool kernel_has_MADV_MERGEABLE = true;
|
|
|
|
|
2013-09-18 23:41:18 +02:00
|
|
|
void* mmap64(void* addr, size_t size, int prot, int flags, int fd, off64_t offset) {
|
2014-01-28 01:28:31 +01:00
|
|
|
if (offset < 0 || (offset & ((1UL << MMAP2_SHIFT)-1)) != 0) {
|
2013-07-17 22:12:26 +02:00
|
|
|
errno = EINVAL;
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
2012-03-21 21:19:51 +01:00
|
|
|
|
2015-08-04 06:48:41 +02:00
|
|
|
// prevent allocations large enough for `end - start` to overflow
|
2017-10-06 01:39:33 +02:00
|
|
|
size_t rounded = __BIONIC_ALIGN(size, PAGE_SIZE);
|
2015-11-03 11:30:29 +01:00
|
|
|
if (rounded < size || rounded > PTRDIFF_MAX) {
|
2015-08-04 06:48:41 +02:00
|
|
|
errno = ENOMEM;
|
|
|
|
return MAP_FAILED;
|
|
|
|
}
|
|
|
|
|
2015-09-24 14:28:59 +02:00
|
|
|
bool is_private_anonymous =
|
|
|
|
(flags & (MAP_PRIVATE | MAP_ANONYMOUS)) == (MAP_PRIVATE | MAP_ANONYMOUS);
|
|
|
|
bool is_stack_or_grows_down = (flags & (MAP_STACK | MAP_GROWSDOWN)) != 0;
|
|
|
|
|
2014-01-28 01:28:31 +01:00
|
|
|
void* result = __mmap2(addr, size, prot, flags, fd, offset >> MMAP2_SHIFT);
|
2014-03-03 20:54:21 +01:00
|
|
|
|
2015-09-24 14:28:59 +02:00
|
|
|
if (result != MAP_FAILED && kernel_has_MADV_MERGEABLE &&
|
|
|
|
is_private_anonymous && !is_stack_or_grows_down) {
|
2013-07-17 22:12:26 +02:00
|
|
|
ErrnoRestorer errno_restorer;
|
2014-03-03 20:54:21 +01:00
|
|
|
int rc = madvise(result, size, MADV_MERGEABLE);
|
|
|
|
if (rc == -1 && errno == EINVAL) {
|
|
|
|
kernel_has_MADV_MERGEABLE = false;
|
|
|
|
}
|
2013-07-17 22:12:26 +02:00
|
|
|
}
|
2013-05-18 02:08:41 +02:00
|
|
|
|
2013-07-17 22:12:26 +02:00
|
|
|
return result;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2013-09-18 23:41:18 +02:00
|
|
|
|
|
|
|
void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset) {
|
2014-01-28 01:28:31 +01:00
|
|
|
return mmap64(addr, size, prot, flags, fd, static_cast<off64_t>(offset));
|
2013-09-18 23:41:18 +02:00
|
|
|
}
|