blob: 88ec829decd792fd7d9bf973fbdd9968b29ff504 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Daniel Micay4200e262015-11-03 05:14:08 -05002 * Copyright (C) 2015 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
Daniel Micay4200e262015-11-03 05:14:08 -050028
Daniel Micayc22a7de2015-11-07 10:40:26 -050029#include <errno.h>
Daniel Micay4200e262015-11-03 05:14:08 -050030#include <sys/mman.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031#include <stdarg.h>
Daniel Micayc22a7de2015-11-07 10:40:26 -050032#include <stdint.h>
33#include <unistd.h>
34
Josh Gao4956c372019-12-19 16:35:51 -080035#include "platform/bionic/macros.h"
Peter Collingbournebb11ee62022-05-02 12:26:16 -070036#include "platform/bionic/page.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037
Elliott Hughes50080a22019-06-19 12:47:53 -070038extern "C" void* __mremap(void*, size_t, size_t, int, void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039
Daniel Micay4200e262015-11-03 05:14:08 -050040void* mremap(void* old_address, size_t old_size, size_t new_size, int flags, ...) {
Daniel Micayc22a7de2015-11-07 10:40:26 -050041 // prevent allocations large enough for `end - start` to overflow
Peter Collingbournebb11ee62022-05-02 12:26:16 -070042 size_t rounded = __BIONIC_ALIGN(new_size, page_size());
Daniel Micayc22a7de2015-11-07 10:40:26 -050043 if (rounded < new_size || rounded > PTRDIFF_MAX) {
44 errno = ENOMEM;
45 return MAP_FAILED;
46 }
47
Daniel Micay4200e262015-11-03 05:14:08 -050048 void* new_address = nullptr;
49 // The optional argument is only valid if the MREMAP_FIXED flag is set,
50 // so we assume it's not present otherwise.
51 if ((flags & MREMAP_FIXED) != 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052 va_list ap;
Daniel Micay4200e262015-11-03 05:14:08 -050053 va_start(ap, flags);
54 new_address = va_arg(ap, void*);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055 va_end(ap);
Daniel Micay4200e262015-11-03 05:14:08 -050056 }
Elliott Hughes50080a22019-06-19 12:47:53 -070057 return __mremap(old_address, old_size, new_size, flags, new_address);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080058}