blob: 314fe9bcbf77d5219991bae49bbee4d2891c5940 [file] [log] [blame]
Elliott Hughes06209252013-11-06 16:20:54 -08001/*
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 Hughesa9325132015-04-16 17:56:12 -070029#undef _FORTIFY_SOURCE
30
Elliott Hughes06209252013-11-06 16:20:54 -080031#include <errno.h>
Elliott Hughesf64b8ea2014-02-03 16:20:46 -080032#include <fcntl.h>
Elliott Hughes4358d532024-06-06 21:08:17 +000033#include <stdint.h>
34#include <sys/mman.h>
Elliott Hughes0f461e32014-01-09 10:17:03 -080035#include <sys/resource.h>
Elliott Hughes06209252013-11-06 16:20:54 -080036#include <sys/types.h>
Elliott Hughes06209252013-11-06 16:20:54 -080037#include <unistd.h>
38
Elliott Hughes4358d532024-06-06 21:08:17 +000039#include "platform/bionic/macros.h"
40#include "platform/bionic/page.h"
41#include "private/ErrnoRestorer.h"
Josh Gao97271922019-11-06 13:15:00 -080042#include "private/bionic_fdtrack.h"
43
Josh Gaob36efa42016-09-15 13:55:41 -070044#if defined(__LP64__)
Elliott Hughes06209252013-11-06 16:20:54 -080045#error This code is only needed on 32-bit systems!
46#endif
47
Elliott Hughes4358d532024-06-06 21:08:17 +000048// To implement lseek64() on ILP32, we need to use the _llseek() system call
49// which splits the off64_t into two 32-bit arguments and returns the off64_t
50// result via a pointer because 32-bit kernels can't accept 64-bit arguments
51// or return 64-bit results. (Our symbol is __llseek with two underscores for
52// historical reasons, but it's exposed as ABI so we can't fix it.)
Elliott Hughes06209252013-11-06 16:20:54 -080053extern "C" int __llseek(int, unsigned long, unsigned long, off64_t*, int);
Elliott Hughes06209252013-11-06 16:20:54 -080054
Elliott Hughes06209252013-11-06 16:20:54 -080055off64_t lseek64(int fd, off64_t off, int whence) {
56 off64_t result;
57 unsigned long off_hi = static_cast<unsigned long>(off >> 32);
58 unsigned long off_lo = static_cast<unsigned long>(off);
59 if (__llseek(fd, off_hi, off_lo, &result, whence) < 0) {
60 return -1;
61 }
62 return result;
63}
64
65// There is no pread for 32-bit off_t, so we need to widen and call pread64.
66ssize_t pread(int fd, void* buf, size_t byte_count, off_t offset) {
67 return pread64(fd, buf, byte_count, static_cast<off64_t>(offset));
68}
69
70// There is no pwrite for 32-bit off_t, so we need to widen and call pwrite64.
71ssize_t pwrite(int fd, const void* buf, size_t byte_count, off_t offset) {
72 return pwrite64(fd, buf, byte_count, static_cast<off64_t>(offset));
73}
Elliott Hughes0f461e32014-01-09 10:17:03 -080074
Elliott Hughesf64b8ea2014-02-03 16:20:46 -080075// There is no fallocate for 32-bit off_t, so we need to widen and call fallocate64.
76int fallocate(int fd, int mode, off_t offset, off_t length) {
77 return fallocate64(fd, mode, static_cast<off64_t>(offset), static_cast<off64_t>(length));
78}
79
Elliott Hughes0f461e32014-01-09 10:17:03 -080080// There is no getrlimit64 system call, so we need to use prlimit64.
81int getrlimit64(int resource, rlimit64* limits64) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070082 return prlimit64(0, resource, nullptr, limits64);
Elliott Hughes0f461e32014-01-09 10:17:03 -080083}
84
85// There is no setrlimit64 system call, so we need to use prlimit64.
86int setrlimit64(int resource, const rlimit64* limits64) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070087 return prlimit64(0, resource, limits64, nullptr);
Elliott Hughes0f461e32014-01-09 10:17:03 -080088}
Elliott Hughes4151db52015-10-28 17:14:48 -070089
90// There is no prlimit system call, so we need to use prlimit64.
91int prlimit(pid_t pid, int resource, const rlimit* n32, rlimit* o32) {
92 rlimit64 n64;
93 if (n32 != nullptr) {
94 n64.rlim_cur = (n32->rlim_cur == RLIM_INFINITY) ? RLIM64_INFINITY : n32->rlim_cur;
95 n64.rlim_max = (n32->rlim_max == RLIM_INFINITY) ? RLIM64_INFINITY : n32->rlim_max;
96 }
97
98 rlimit64 o64;
99 int result = prlimit64(pid, resource,
100 (n32 != nullptr) ? &n64 : nullptr,
101 (o32 != nullptr) ? &o64 : nullptr);
102 if (result != -1 && o32 != nullptr) {
103 o32->rlim_cur = (o64.rlim_cur == RLIM64_INFINITY) ? RLIM_INFINITY : o64.rlim_cur;
104 o32->rlim_max = (o64.rlim_max == RLIM64_INFINITY) ? RLIM_INFINITY : o64.rlim_max;
105 }
106 return result;
107}
Elliott Hughes4358d532024-06-06 21:08:17 +0000108
109// mmap2(2) is like mmap(2), but the offset is in 4096-byte blocks (regardless
110// of page size), not bytes, to enable mapping parts of large files past the
111// 4GiB limit but without the inconvenience of dealing with 64-bit values, with
112// no down side since mappings need to be page aligned anyway, and the 32-bit
113// architectures that support this system call all have 4KiB pages.
114extern "C" void* __mmap2(void*, size_t, int, int, int, size_t);
115
116void* mmap64(void* addr, size_t size, int prot, int flags, int fd, off64_t offset) {
117 static constexpr size_t MMAP2_SHIFT = 12;
118
119 if (offset < 0 || (offset & ((1UL << MMAP2_SHIFT) - 1)) != 0) {
120 errno = EINVAL;
121 return MAP_FAILED;
122 }
123
124 // Prevent allocations large enough for `end - start` to overflow,
125 // to avoid security bugs.
126 size_t rounded = __BIONIC_ALIGN(size, page_size());
127 if (rounded < size || rounded > PTRDIFF_MAX) {
128 errno = ENOMEM;
129 return MAP_FAILED;
130 }
131
132 return __mmap2(addr, size, prot, flags, fd, offset >> MMAP2_SHIFT);
133}
134
135void* mmap(void* addr, size_t size, int prot, int flags, int fd, off_t offset) {
136 return mmap64(addr, size, prot, flags, fd, static_cast<off64_t>(offset));
137}