1 /*
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>
32
33 #if __ANDROID_API__ < __ANDROID_API_L__
34
35 #include <errno.h>
36 #include <sys/mman.h>
37 #include <unistd.h>
38
39 __BEGIN_DECLS
40
41 /*
42 * While this was never an inline, this function alone has caused most of the
43 * bug reports related to _FILE_OFFSET_BITS=64. Providing an inline for it
44 * should allow a lot more code to build with _FILE_OFFSET_BITS=64 when
45 * targeting pre-L.
46 */
47 static __inline void* mmap64(void* __addr, size_t __size, int __prot, int __flags, int __fd,
48 off64_t __offset) __RENAME(mmap64);
mmap64(void * __addr,size_t __size,int __prot,int __flags,int __fd,off64_t __offset)49 static __inline void* mmap64(void* __addr, size_t __size, int __prot, int __flags, int __fd,
50 off64_t __offset) {
51 const int __mmap2_shift = 12; // 2**12 == 4096
52 if (__offset < 0 || (__offset & ((1UL << __mmap2_shift) - 1)) != 0) {
53 errno = EINVAL;
54 return MAP_FAILED;
55 }
56
57 // prevent allocations large enough for `end - start` to overflow
58 size_t __rounded = __BIONIC_ALIGN(__size, PAGE_SIZE);
59 if (__rounded < __size || __rounded > PTRDIFF_MAX) {
60 errno = ENOMEM;
61 return MAP_FAILED;
62 }
63
64 extern void* __mmap2(void* __addr, size_t __size, int __prot, int __flags, int __fd,
65 size_t __offset);
66 return __mmap2(__addr, __size, __prot, __flags, __fd, __offset >> __mmap2_shift);
67 }
68
69 __END_DECLS
70
71 #endif /* __ANDROID_API__ < __ANDROID_API_L__ */
72