| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 The Android Open Source Project | 
|  | 3 | * 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 | */ | 
|  | 28 |  | 
| Elliott Hughes | a932513 | 2015-04-16 17:56:12 -0700 | [diff] [blame] | 29 | #undef _FORTIFY_SOURCE | 
|  | 30 |  | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 31 | #include <errno.h> | 
| Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 32 | #include <fcntl.h> | 
| Elliott Hughes | bebe3d5 | 2024-06-07 17:16:34 -0400 | [diff] [blame] | 33 | #include <stdarg.h> | 
| Elliott Hughes | 4358d53 | 2024-06-06 21:08:17 +0000 | [diff] [blame] | 34 | #include <stdint.h> | 
|  | 35 | #include <sys/mman.h> | 
| Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 36 | #include <sys/resource.h> | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 37 | #include <sys/types.h> | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 38 | #include <unistd.h> | 
|  | 39 |  | 
| Elliott Hughes | 4358d53 | 2024-06-06 21:08:17 +0000 | [diff] [blame] | 40 | #include "platform/bionic/macros.h" | 
|  | 41 | #include "platform/bionic/page.h" | 
|  | 42 | #include "private/ErrnoRestorer.h" | 
| Josh Gao | 9727192 | 2019-11-06 13:15:00 -0800 | [diff] [blame] | 43 | #include "private/bionic_fdtrack.h" | 
|  | 44 |  | 
| Josh Gao | b36efa4 | 2016-09-15 13:55:41 -0700 | [diff] [blame] | 45 | #if defined(__LP64__) | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 46 | #error This code is only needed on 32-bit systems! | 
|  | 47 | #endif | 
|  | 48 |  | 
| Elliott Hughes | 4358d53 | 2024-06-06 21:08:17 +0000 | [diff] [blame] | 49 | // To implement lseek64() on ILP32, we need to use the _llseek() system call | 
|  | 50 | // which splits the off64_t into two 32-bit arguments and returns the off64_t | 
|  | 51 | // result via a pointer because 32-bit kernels can't accept 64-bit arguments | 
|  | 52 | // or return 64-bit results. (Our symbol is __llseek with two underscores for | 
|  | 53 | // historical reasons, but it's exposed as ABI so we can't fix it.) | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 54 | extern "C" int __llseek(int, unsigned long, unsigned long, off64_t*, int); | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 55 |  | 
| Elliott Hughes | 0620925 | 2013-11-06 16:20:54 -0800 | [diff] [blame] | 56 | off64_t lseek64(int fd, off64_t off, int whence) { | 
|  | 57 | off64_t result; | 
|  | 58 | unsigned long off_hi = static_cast<unsigned long>(off >> 32); | 
|  | 59 | unsigned long off_lo = static_cast<unsigned long>(off); | 
|  | 60 | if (__llseek(fd, off_hi, off_lo, &result, whence) < 0) { | 
|  | 61 | return -1; | 
|  | 62 | } | 
|  | 63 | return result; | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | // There is no pread for 32-bit off_t, so we need to widen and call pread64. | 
|  | 67 | ssize_t pread(int fd, void* buf, size_t byte_count, off_t offset) { | 
|  | 68 | return pread64(fd, buf, byte_count, static_cast<off64_t>(offset)); | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | // There is no pwrite for 32-bit off_t, so we need to widen and call pwrite64. | 
|  | 72 | ssize_t pwrite(int fd, const void* buf, size_t byte_count, off_t offset) { | 
|  | 73 | return pwrite64(fd, buf, byte_count, static_cast<off64_t>(offset)); | 
|  | 74 | } | 
| Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 75 |  | 
| Elliott Hughes | f64b8ea | 2014-02-03 16:20:46 -0800 | [diff] [blame] | 76 | // There is no fallocate for 32-bit off_t, so we need to widen and call fallocate64. | 
|  | 77 | int fallocate(int fd, int mode, off_t offset, off_t length) { | 
|  | 78 | return fallocate64(fd, mode, static_cast<off64_t>(offset), static_cast<off64_t>(length)); | 
|  | 79 | } | 
|  | 80 |  | 
| Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 81 | // There is no getrlimit64 system call, so we need to use prlimit64. | 
|  | 82 | int getrlimit64(int resource, rlimit64* limits64) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 83 | return prlimit64(0, resource, nullptr, limits64); | 
| Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 84 | } | 
|  | 85 |  | 
|  | 86 | // There is no setrlimit64 system call, so we need to use prlimit64. | 
|  | 87 | int setrlimit64(int resource, const rlimit64* limits64) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 88 | return prlimit64(0, resource, limits64, nullptr); | 
| Elliott Hughes | 0f461e3 | 2014-01-09 10:17:03 -0800 | [diff] [blame] | 89 | } | 
| Elliott Hughes | 4151db5 | 2015-10-28 17:14:48 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | // There is no prlimit system call, so we need to use prlimit64. | 
|  | 92 | int prlimit(pid_t pid, int resource, const rlimit* n32, rlimit* o32) { | 
|  | 93 | rlimit64 n64; | 
|  | 94 | if (n32 != nullptr) { | 
|  | 95 | n64.rlim_cur = (n32->rlim_cur == RLIM_INFINITY) ? RLIM64_INFINITY : n32->rlim_cur; | 
|  | 96 | n64.rlim_max = (n32->rlim_max == RLIM_INFINITY) ? RLIM64_INFINITY : n32->rlim_max; | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | rlimit64 o64; | 
|  | 100 | int result = prlimit64(pid, resource, | 
|  | 101 | (n32 != nullptr) ? &n64 : nullptr, | 
|  | 102 | (o32 != nullptr) ? &o64 : nullptr); | 
|  | 103 | if (result != -1 && o32 != nullptr) { | 
|  | 104 | o32->rlim_cur = (o64.rlim_cur == RLIM64_INFINITY) ? RLIM_INFINITY : o64.rlim_cur; | 
|  | 105 | o32->rlim_max = (o64.rlim_max == RLIM64_INFINITY) ? RLIM_INFINITY : o64.rlim_max; | 
|  | 106 | } | 
|  | 107 | return result; | 
|  | 108 | } | 
| Elliott Hughes | 4358d53 | 2024-06-06 21:08:17 +0000 | [diff] [blame] | 109 |  | 
|  | 110 | // mmap2(2) is like mmap(2), but the offset is in 4096-byte blocks (regardless | 
|  | 111 | // of page size), not bytes, to enable mapping parts of large files past the | 
|  | 112 | // 4GiB limit but without the inconvenience of dealing with 64-bit values, with | 
|  | 113 | // no down side since mappings need to be page aligned anyway, and the 32-bit | 
|  | 114 | // architectures that support this system call all have 4KiB pages. | 
|  | 115 | extern "C" void* __mmap2(void*, size_t, int, int, int, size_t); | 
|  | 116 |  | 
|  | 117 | void* mmap64(void* addr, size_t size, int prot, int flags, int fd, off64_t offset) { | 
|  | 118 | static constexpr size_t MMAP2_SHIFT = 12; | 
|  | 119 |  | 
|  | 120 | if (offset < 0 || (offset & ((1UL << MMAP2_SHIFT) - 1)) != 0) { | 
|  | 121 | errno = EINVAL; | 
|  | 122 | return MAP_FAILED; | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | // Prevent allocations large enough for `end - start` to overflow, | 
|  | 126 | // to avoid security bugs. | 
|  | 127 | size_t rounded = __BIONIC_ALIGN(size, page_size()); | 
|  | 128 | if (rounded < size || rounded > PTRDIFF_MAX) { | 
|  | 129 | errno = ENOMEM; | 
|  | 130 | return MAP_FAILED; | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | return __mmap2(addr, size, prot, flags, fd, offset >> MMAP2_SHIFT); | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset) { | 
|  | 137 | return mmap64(addr, size, prot, flags, fd, static_cast<off64_t>(offset)); | 
|  | 138 | } | 
| Elliott Hughes | bebe3d5 | 2024-06-07 17:16:34 -0400 | [diff] [blame] | 139 |  | 
|  | 140 | // The only difference here is that the libc API uses varargs for the | 
|  | 141 | // optional `new_address` argument that's only used by MREMAP_FIXED. | 
|  | 142 | extern "C" void* __mremap(void*, size_t, size_t, int, void*); | 
|  | 143 |  | 
|  | 144 | void* mremap(void* old_address, size_t old_size, size_t new_size, int flags, ...) { | 
|  | 145 | // Prevent allocations large enough for `end - start` to overflow, | 
|  | 146 | // to avoid security bugs. | 
|  | 147 | size_t rounded = __BIONIC_ALIGN(new_size, page_size()); | 
|  | 148 | if (rounded < new_size || rounded > PTRDIFF_MAX) { | 
|  | 149 | errno = ENOMEM; | 
|  | 150 | return MAP_FAILED; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | // The optional argument is only valid if the MREMAP_FIXED flag is set, | 
|  | 154 | // so we assume it's not present otherwise. | 
|  | 155 | void* new_address = nullptr; | 
|  | 156 | if ((flags & MREMAP_FIXED) != 0) { | 
|  | 157 | va_list ap; | 
|  | 158 | va_start(ap, flags); | 
|  | 159 | new_address = va_arg(ap, void*); | 
|  | 160 | va_end(ap); | 
|  | 161 | } | 
|  | 162 | return __mremap(old_address, old_size, new_size, flags, new_address); | 
|  | 163 | } | 
| Elliott Hughes | b3904ee | 2024-08-13 20:19:31 +0000 | [diff] [blame] | 164 |  | 
|  | 165 | // mseal(2) is LP64-only. | 
|  | 166 | int mseal(void*, size_t, unsigned long) { | 
|  | 167 | errno = ENOSYS; | 
|  | 168 | return -1; | 
|  | 169 | } |