blob: 7eb537e7e09cce1e4baca3e42fc194cb4b99441b [file] [log] [blame]
Dan Alberta613d0d2017-10-05 16:39:33 -07001/*
2 * Copyright (C) 2017 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
29#pragma once
30
31#include <sys/cdefs.h>
Dan Albertb37e9aa2017-10-24 09:20:41 +000032#include <sys/mman.h>
33#include <sys/syscall.h>
34#include <unistd.h>
Dan Alberta613d0d2017-10-05 16:39:33 -070035
36#if __ANDROID_API__ < __ANDROID_API_L__
37
38__BEGIN_DECLS
39
40/*
41 * While this was never an inline, this function alone has caused most of the
42 * bug reports related to _FILE_OFFSET_BITS=64. Providing an inline for it
43 * should allow a lot more code to build with _FILE_OFFSET_BITS=64 when
44 * targeting pre-L.
45 */
46static __inline void* mmap64(void* __addr, size_t __size, int __prot, int __flags, int __fd,
Dan Albert8c2323c2017-10-05 16:39:33 -070047 off64_t __offset) __RENAME(mmap64);
48static __inline void* mmap64(void* __addr, size_t __size, int __prot, int __flags, int __fd,
Dan Alberta613d0d2017-10-05 16:39:33 -070049 off64_t __offset) {
50 const int __mmap2_shift = 12; // 2**12 == 4096
51 if (__offset < 0 || (__offset & ((1UL << __mmap2_shift) - 1)) != 0) {
52 errno = EINVAL;
53 return MAP_FAILED;
54 }
55
56 // prevent allocations large enough for `end - start` to overflow
57 size_t __rounded = __BIONIC_ALIGN(__size, PAGE_SIZE);
58 if (__rounded < __size || __rounded > PTRDIFF_MAX) {
59 errno = ENOMEM;
60 return MAP_FAILED;
61 }
62
63 extern void* __mmap2(void* __addr, size_t __size, int __prot, int __flags, int __fd,
64 size_t __offset);
65 return __mmap2(__addr, __size, __prot, __flags, __fd, __offset >> __mmap2_shift);
66}
67
68__END_DECLS
69
70#endif /* __ANDROID_API__ < __ANDROID_API_L__ */