blob: 7171896d30cbf270e55d9f1dba6d258db961ce99 [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>
32#include <sys/mman.h>
33#include <sys/syscall.h>
34#include <unistd.h>
35
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,
47 off64_t __offset) {
48 const int __mmap2_shift = 12; // 2**12 == 4096
49 if (__offset < 0 || (__offset & ((1UL << __mmap2_shift) - 1)) != 0) {
50 errno = EINVAL;
51 return MAP_FAILED;
52 }
53
54 // prevent allocations large enough for `end - start` to overflow
55 size_t __rounded = __BIONIC_ALIGN(__size, PAGE_SIZE);
56 if (__rounded < __size || __rounded > PTRDIFF_MAX) {
57 errno = ENOMEM;
58 return MAP_FAILED;
59 }
60
61 extern void* __mmap2(void* __addr, size_t __size, int __prot, int __flags, int __fd,
62 size_t __offset);
63 return __mmap2(__addr, __size, __prot, __flags, __fd, __offset >> __mmap2_shift);
64}
65
66__END_DECLS
67
68#endif /* __ANDROID_API__ < __ANDROID_API_L__ */