• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdlib.h>
20 #include <stdint.h>
21 
22 #include <binder/IMemory.h>
23 
24 
25 namespace android {
26 
27 // ---------------------------------------------------------------------------
28 
29 class MemoryHeapBase : public virtual BnMemoryHeap
30 {
31 public:
32     enum {
33         READ_ONLY = IMemoryHeap::READ_ONLY,
34         // memory won't be mapped locally, but will be mapped in the remote
35         // process.
36         DONT_MAP_LOCALLY = 0x00000100,
37         NO_CACHING = 0x00000200,
38         // Bypass ashmem-libcutils to create a memfd shared region.
39         // Ashmem-libcutils will eventually migrate to memfd.
40         // Memfd has security benefits and supports file sealing.
41         // Calling process will need to modify selinux permissions to
42         // open access to tmpfs files. See audioserver for examples.
43         // This is only valid for size constructor.
44         // For host compilation targets, memfd is stubbed in favor of /tmp
45         // files so sealing is not enforced.
46         FORCE_MEMFD = 0x00000400,
47         // Default opt-out of sealing behavior in memfd to avoid potential DOS.
48         // Clients of shared files can seal at anytime via syscall, leading to
49         // TOC/TOU issues if additional seals prevent access from the creating
50         // process. Alternatively, seccomp fcntl().
51         MEMFD_ALLOW_SEALING = 0x00000800
52     };
53 
54     /*
55      * maps the memory referenced by fd. but DOESN'T take ownership
56      * of the filedescriptor (it makes a copy with dup()
57      */
58     MemoryHeapBase(int fd, size_t size, uint32_t flags = 0, off_t offset = 0);
59 
60     /*
61      * maps memory from the given device
62      */
63     explicit MemoryHeapBase(const char* device, size_t size = 0, uint32_t flags = 0);
64 
65     /*
66      * maps memory from ashmem, with the given name for debugging
67      * if the READ_ONLY flag is set, the memory will be writeable by the calling process,
68      * but not by others. this is NOT the case with the other ctors.
69      */
70     explicit MemoryHeapBase(size_t size, uint32_t flags = 0, char const* name = nullptr);
71 
72     virtual ~MemoryHeapBase();
73 
74     /* implement IMemoryHeap interface */
75     int         getHeapID() const override;
76 
77     /* virtual address of the heap. returns MAP_FAILED in case of error */
78     void*       getBase() const override;
79 
80     size_t      getSize() const override;
81     uint32_t    getFlags() const override;
82     off_t       getOffset() const override;
83 
84     const char*         getDevice() const;
85 
86     /* this closes this heap -- use carefully */
87     void dispose();
88 
89 protected:
90             MemoryHeapBase();
91     // init() takes ownership of fd
92     status_t init(int fd, void *base, size_t size,
93             int flags = 0, const char* device = nullptr);
94 
95 private:
96     status_t mapfd(int fd, bool writeableByCaller, size_t size, off_t offset = 0);
97 
98     int         mFD;
99     size_t      mSize;
100     void*       mBase;
101     uint32_t    mFlags;
102     const char* mDevice;
103     bool        mNeedUnmap;
104     off_t       mOffset;
105 };
106 
107 // ---------------------------------------------------------------------------
108 } // namespace android
109