| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | #define LOG_TAG "IMemory" | 
 | 18 |  | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 19 | #include <atomic> | 
| Mathias Agopian | e9e9fe4 | 2017-02-28 16:25:16 -0800 | [diff] [blame] | 20 | #include <stdatomic.h> | 
 | 21 |  | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 22 | #include <fcntl.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <stdint.h> | 
 | 24 | #include <stdio.h> | 
 | 25 | #include <stdlib.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | #include <sys/types.h> | 
 | 27 | #include <sys/mman.h> | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 28 | #include <unistd.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 |  | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 30 | #include <binder/IMemory.h> | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 31 | #include <binder/Parcel.h> | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 32 | #include <log/log.h> | 
| Mathias Agopian | e9e9fe4 | 2017-02-28 16:25:16 -0800 | [diff] [blame] | 33 |  | 
| Mark Salyzyn | a5e161b | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 34 | #include <utils/KeyedVector.h> | 
 | 35 | #include <utils/threads.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 |  | 
 | 37 | #define VERBOSE   0 | 
 | 38 |  | 
 | 39 | namespace android { | 
 | 40 | // --------------------------------------------------------------------------- | 
 | 41 |  | 
 | 42 | class HeapCache : public IBinder::DeathRecipient | 
 | 43 | { | 
 | 44 | public: | 
 | 45 |     HeapCache(); | 
 | 46 |     virtual ~HeapCache(); | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 47 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 |     virtual void binderDied(const wp<IBinder>& who); | 
 | 49 |  | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 50 |     sp<IMemoryHeap> find_heap(const sp<IBinder>& binder); | 
 | 51 |     void free_heap(const sp<IBinder>& binder); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 |     sp<IMemoryHeap> get_heap(const sp<IBinder>& binder); | 
 | 53 |     void dump_heaps(); | 
 | 54 |  | 
 | 55 | private: | 
 | 56 |     // For IMemory.cpp | 
 | 57 |     struct heap_info_t { | 
 | 58 |         sp<IMemoryHeap> heap; | 
 | 59 |         int32_t         count; | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 60 |         // Note that this cannot be meaningfully copied. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 |     }; | 
 | 62 |  | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 63 |     void free_heap(const wp<IBinder>& binder); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 |  | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 65 |     Mutex mHeapCacheLock;  // Protects entire vector below. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 |     KeyedVector< wp<IBinder>, heap_info_t > mHeapCache; | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 67 |     // We do not use the copy-on-write capabilities of KeyedVector. | 
 | 68 |     // TODO: Reimplemement based on standard C++ container? | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | }; | 
 | 70 |  | 
 | 71 | static sp<HeapCache> gHeapCache = new HeapCache(); | 
 | 72 |  | 
 | 73 | /******************************************************************************/ | 
 | 74 |  | 
 | 75 | enum { | 
 | 76 |     HEAP_ID = IBinder::FIRST_CALL_TRANSACTION | 
 | 77 | }; | 
 | 78 |  | 
 | 79 | class BpMemoryHeap : public BpInterface<IMemoryHeap> | 
 | 80 | { | 
 | 81 | public: | 
| Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 82 |     explicit BpMemoryHeap(const sp<IBinder>& impl); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 |     virtual ~BpMemoryHeap(); | 
 | 84 |  | 
 | 85 |     virtual int getHeapID() const; | 
 | 86 |     virtual void* getBase() const; | 
 | 87 |     virtual size_t getSize() const; | 
 | 88 |     virtual uint32_t getFlags() const; | 
| Andy Hung | 5b02852 | 2018-10-22 15:29:49 -0700 | [diff] [blame] | 89 |     off_t getOffset() const override; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 |  | 
 | 91 | private: | 
 | 92 |     friend class IMemory; | 
 | 93 |     friend class HeapCache; | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 94 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 |     // for debugging in this module | 
 | 96 |     static inline sp<IMemoryHeap> find_heap(const sp<IBinder>& binder) { | 
 | 97 |         return gHeapCache->find_heap(binder); | 
 | 98 |     } | 
 | 99 |     static inline void free_heap(const sp<IBinder>& binder) { | 
 | 100 |         gHeapCache->free_heap(binder); | 
 | 101 |     } | 
 | 102 |     static inline sp<IMemoryHeap> get_heap(const sp<IBinder>& binder) { | 
 | 103 |         return gHeapCache->get_heap(binder); | 
 | 104 |     } | 
 | 105 |     static inline void dump_heaps() { | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 106 |         gHeapCache->dump_heaps(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 |     } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 |  | 
 | 109 |     void assertMapped() const; | 
 | 110 |     void assertReallyMapped() const; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 |  | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 112 |     mutable std::atomic<int32_t> mHeapId; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 |     mutable void*       mBase; | 
 | 114 |     mutable size_t      mSize; | 
 | 115 |     mutable uint32_t    mFlags; | 
| Andy Hung | 5b02852 | 2018-10-22 15:29:49 -0700 | [diff] [blame] | 116 |     mutable off_t       mOffset; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 |     mutable bool        mRealHeap; | 
 | 118 |     mutable Mutex       mLock; | 
 | 119 | }; | 
 | 120 |  | 
 | 121 | // ---------------------------------------------------------------------------- | 
 | 122 |  | 
 | 123 | enum { | 
 | 124 |     GET_MEMORY = IBinder::FIRST_CALL_TRANSACTION | 
 | 125 | }; | 
 | 126 |  | 
 | 127 | class BpMemory : public BpInterface<IMemory> | 
 | 128 | { | 
 | 129 | public: | 
| Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 130 |     explicit BpMemory(const sp<IBinder>& impl); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 |     virtual ~BpMemory(); | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 132 |     // NOLINTNEXTLINE(google-default-arguments) | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 133 |     virtual sp<IMemoryHeap> getMemory(ssize_t* offset=nullptr, size_t* size=nullptr) const; | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 134 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | private: | 
 | 136 |     mutable sp<IMemoryHeap> mHeap; | 
 | 137 |     mutable ssize_t mOffset; | 
 | 138 |     mutable size_t mSize; | 
 | 139 | }; | 
 | 140 |  | 
 | 141 | /******************************************************************************/ | 
 | 142 |  | 
 | 143 | void* IMemory::fastPointer(const sp<IBinder>& binder, ssize_t offset) const | 
 | 144 | { | 
 | 145 |     sp<IMemoryHeap> realHeap = BpMemoryHeap::get_heap(binder); | 
 | 146 |     void* const base = realHeap->base(); | 
 | 147 |     if (base == MAP_FAILED) | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 148 |         return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 |     return static_cast<char*>(base) + offset; | 
 | 150 | } | 
 | 151 |  | 
 | 152 | void* IMemory::pointer() const { | 
 | 153 |     ssize_t offset; | 
 | 154 |     sp<IMemoryHeap> heap = getMemory(&offset); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 155 |     void* const base = heap!=nullptr ? heap->base() : MAP_FAILED; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 |     if (base == MAP_FAILED) | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 157 |         return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 158 |     return static_cast<char*>(base) + offset; | 
 | 159 | } | 
 | 160 |  | 
 | 161 | size_t IMemory::size() const { | 
 | 162 |     size_t size; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 163 |     getMemory(nullptr, &size); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 |     return size; | 
 | 165 | } | 
 | 166 |  | 
 | 167 | ssize_t IMemory::offset() const { | 
 | 168 |     ssize_t offset; | 
 | 169 |     getMemory(&offset); | 
 | 170 |     return offset; | 
 | 171 | } | 
 | 172 |  | 
 | 173 | /******************************************************************************/ | 
 | 174 |  | 
 | 175 | BpMemory::BpMemory(const sp<IBinder>& impl) | 
 | 176 |     : BpInterface<IMemory>(impl), mOffset(0), mSize(0) | 
 | 177 | { | 
 | 178 | } | 
 | 179 |  | 
 | 180 | BpMemory::~BpMemory() | 
 | 181 | { | 
 | 182 | } | 
 | 183 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 184 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | sp<IMemoryHeap> BpMemory::getMemory(ssize_t* offset, size_t* size) const | 
 | 186 | { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 187 |     if (mHeap == nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 |         Parcel data, reply; | 
 | 189 |         data.writeInterfaceToken(IMemory::getInterfaceDescriptor()); | 
 | 190 |         if (remote()->transact(GET_MEMORY, data, &reply) == NO_ERROR) { | 
 | 191 |             sp<IBinder> heap = reply.readStrongBinder(); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 192 |             if (heap != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 |                 mHeap = interface_cast<IMemoryHeap>(heap); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 194 |                 if (mHeap != nullptr) { | 
| Andy Hung | 5b02852 | 2018-10-22 15:29:49 -0700 | [diff] [blame] | 195 |                     const int64_t offset64 = reply.readInt64(); | 
 | 196 |                     const uint64_t size64 = reply.readUint64(); | 
 | 197 |                     const ssize_t o = (ssize_t)offset64; | 
 | 198 |                     const size_t s = (size_t)size64; | 
| Christopher Tate | 94b0d4e | 2016-02-05 19:02:56 -0800 | [diff] [blame] | 199 |                     size_t heapSize = mHeap->getSize(); | 
| Andy Hung | 5b02852 | 2018-10-22 15:29:49 -0700 | [diff] [blame] | 200 |                     if (s == size64 && o == offset64 // ILP32 bounds check | 
 | 201 |                             && s <= heapSize | 
| Christopher Tate | 94b0d4e | 2016-02-05 19:02:56 -0800 | [diff] [blame] | 202 |                             && o >= 0 | 
 | 203 |                             && (static_cast<size_t>(o) <= heapSize - s)) { | 
 | 204 |                         mOffset = o; | 
 | 205 |                         mSize = s; | 
 | 206 |                     } else { | 
 | 207 |                         // Hm. | 
 | 208 |                         android_errorWriteWithInfoLog(0x534e4554, | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 209 |                             "26877992", -1, nullptr, 0); | 
| Christopher Tate | 94b0d4e | 2016-02-05 19:02:56 -0800 | [diff] [blame] | 210 |                         mOffset = 0; | 
 | 211 |                         mSize = 0; | 
 | 212 |                     } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 |                 } | 
 | 214 |             } | 
 | 215 |         } | 
 | 216 |     } | 
 | 217 |     if (offset) *offset = mOffset; | 
 | 218 |     if (size) *size = mSize; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 219 |     return (mSize > 0) ? mHeap : nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | } | 
 | 221 |  | 
 | 222 | // --------------------------------------------------------------------------- | 
 | 223 |  | 
 | 224 | IMPLEMENT_META_INTERFACE(Memory, "android.utils.IMemory"); | 
 | 225 |  | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 226 | BnMemory::BnMemory() { | 
 | 227 | } | 
 | 228 |  | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 229 | BnMemory::~BnMemory() { | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 230 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 232 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | status_t BnMemory::onTransact( | 
 | 234 |     uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
 | 235 | { | 
 | 236 |     switch(code) { | 
 | 237 |         case GET_MEMORY: { | 
 | 238 |             CHECK_INTERFACE(IMemory, data, reply); | 
 | 239 |             ssize_t offset; | 
 | 240 |             size_t size; | 
| Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 241 |             reply->writeStrongBinder( IInterface::asBinder(getMemory(&offset, &size)) ); | 
| Andy Hung | 5b02852 | 2018-10-22 15:29:49 -0700 | [diff] [blame] | 242 |             reply->writeInt64(offset); | 
 | 243 |             reply->writeUint64(size); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 |             return NO_ERROR; | 
 | 245 |         } break; | 
 | 246 |         default: | 
 | 247 |             return BBinder::onTransact(code, data, reply, flags); | 
 | 248 |     } | 
 | 249 | } | 
 | 250 |  | 
 | 251 |  | 
 | 252 | /******************************************************************************/ | 
 | 253 |  | 
 | 254 | BpMemoryHeap::BpMemoryHeap(const sp<IBinder>& impl) | 
 | 255 |     : BpInterface<IMemoryHeap>(impl), | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 256 |         mHeapId(-1), mBase(MAP_FAILED), mSize(0), mFlags(0), mOffset(0), mRealHeap(false) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | { | 
 | 258 | } | 
 | 259 |  | 
 | 260 | BpMemoryHeap::~BpMemoryHeap() { | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 261 |     int32_t heapId = mHeapId.load(memory_order_relaxed); | 
 | 262 |     if (heapId != -1) { | 
 | 263 |         close(heapId); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 |         if (mRealHeap) { | 
 | 265 |             // by construction we're the last one | 
 | 266 |             if (mBase != MAP_FAILED) { | 
| Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 267 |                 sp<IBinder> binder = IInterface::asBinder(this); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 |  | 
 | 269 |                 if (VERBOSE) { | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 270 |                     ALOGD("UNMAPPING binder=%p, heap=%p, size=%zu, fd=%d", | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 271 |                             binder.get(), this, mSize, heapId); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 |                 } | 
 | 273 |  | 
 | 274 |                 munmap(mBase, mSize); | 
 | 275 |             } | 
 | 276 |         } else { | 
 | 277 |             // remove from list only if it was mapped before | 
| Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 278 |             sp<IBinder> binder = IInterface::asBinder(this); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 |             free_heap(binder); | 
 | 280 |         } | 
 | 281 |     } | 
 | 282 | } | 
 | 283 |  | 
 | 284 | void BpMemoryHeap::assertMapped() const | 
 | 285 | { | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 286 |     int32_t heapId = mHeapId.load(memory_order_acquire); | 
 | 287 |     if (heapId == -1) { | 
| Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 288 |         sp<IBinder> binder(IInterface::asBinder(const_cast<BpMemoryHeap*>(this))); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 |         sp<BpMemoryHeap> heap(static_cast<BpMemoryHeap*>(find_heap(binder).get())); | 
 | 290 |         heap->assertReallyMapped(); | 
 | 291 |         if (heap->mBase != MAP_FAILED) { | 
 | 292 |             Mutex::Autolock _l(mLock); | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 293 |             if (mHeapId.load(memory_order_relaxed) == -1) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 |                 mBase   = heap->mBase; | 
 | 295 |                 mSize   = heap->mSize; | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 296 |                 mOffset = heap->mOffset; | 
| Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 297 |                 int fd = fcntl(heap->mHeapId.load(memory_order_relaxed), F_DUPFD_CLOEXEC, 0); | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 298 |                 ALOGE_IF(fd==-1, "cannot dup fd=%d", | 
 | 299 |                         heap->mHeapId.load(memory_order_relaxed)); | 
 | 300 |                 mHeapId.store(fd, memory_order_release); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 |             } | 
 | 302 |         } else { | 
 | 303 |             // something went wrong | 
 | 304 |             free_heap(binder); | 
 | 305 |         } | 
 | 306 |     } | 
 | 307 | } | 
 | 308 |  | 
 | 309 | void BpMemoryHeap::assertReallyMapped() const | 
 | 310 | { | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 311 |     int32_t heapId = mHeapId.load(memory_order_acquire); | 
 | 312 |     if (heapId == -1) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 |  | 
 | 314 |         // remote call without mLock held, worse case scenario, we end up | 
 | 315 |         // calling transact() from multiple threads, but that's not a problem, | 
 | 316 |         // only mmap below must be in the critical section. | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 317 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 |         Parcel data, reply; | 
 | 319 |         data.writeInterfaceToken(IMemoryHeap::getInterfaceDescriptor()); | 
 | 320 |         status_t err = remote()->transact(HEAP_ID, data, &reply); | 
 | 321 |         int parcel_fd = reply.readFileDescriptor(); | 
| Andy Hung | 5b02852 | 2018-10-22 15:29:49 -0700 | [diff] [blame] | 322 |         const uint64_t size64 = reply.readUint64(); | 
 | 323 |         const int64_t offset64 = reply.readInt64(); | 
 | 324 |         const uint32_t flags = reply.readUint32(); | 
 | 325 |         const size_t size = (size_t)size64; | 
 | 326 |         const off_t offset = (off_t)offset64; | 
 | 327 |         if (err != NO_ERROR || // failed transaction | 
 | 328 |                 size != size64 || offset != offset64) { // ILP32 size check | 
 | 329 |             ALOGE("binder=%p transaction failed fd=%d, size=%zu, err=%d (%s)", | 
 | 330 |                     IInterface::asBinder(this).get(), | 
 | 331 |                     parcel_fd, size, err, strerror(-err)); | 
 | 332 |             return; | 
 | 333 |         } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 |         Mutex::Autolock _l(mLock); | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 336 |         if (mHeapId.load(memory_order_relaxed) == -1) { | 
| Nick Kralevich | ec9ec7d | 2016-12-17 19:47:27 -0800 | [diff] [blame] | 337 |             int fd = fcntl(parcel_fd, F_DUPFD_CLOEXEC, 0); | 
| Andy Hung | 5b02852 | 2018-10-22 15:29:49 -0700 | [diff] [blame] | 338 |             ALOGE_IF(fd == -1, "cannot dup fd=%d, size=%zu, err=%d (%s)", | 
| John Eckerdal | 6b0b063 | 2016-04-21 15:04:14 +0200 | [diff] [blame] | 339 |                     parcel_fd, size, err, strerror(errno)); | 
 | 340 |  | 
 | 341 |             int access = PROT_READ; | 
 | 342 |             if (!(flags & READ_ONLY)) { | 
 | 343 |                 access |= PROT_WRITE; | 
 | 344 |             } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 |             mRealHeap = true; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 346 |             mBase = mmap(nullptr, size, access, MAP_SHARED, fd, offset); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 |             if (mBase == MAP_FAILED) { | 
| Andy Hung | 5b02852 | 2018-10-22 15:29:49 -0700 | [diff] [blame] | 348 |                 ALOGE("cannot map BpMemoryHeap (binder=%p), size=%zu, fd=%d (%s)", | 
| Marco Nelissen | 097ca27 | 2014-11-14 08:01:01 -0800 | [diff] [blame] | 349 |                         IInterface::asBinder(this).get(), size, fd, strerror(errno)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 |                 close(fd); | 
 | 351 |             } else { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 352 |                 mSize = size; | 
 | 353 |                 mFlags = flags; | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 354 |                 mOffset = offset; | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 355 |                 mHeapId.store(fd, memory_order_release); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 356 |             } | 
 | 357 |         } | 
 | 358 |     } | 
 | 359 | } | 
 | 360 |  | 
 | 361 | int BpMemoryHeap::getHeapID() const { | 
 | 362 |     assertMapped(); | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 363 |     // We either stored mHeapId ourselves, or loaded it with acquire semantics. | 
 | 364 |     return mHeapId.load(memory_order_relaxed); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | } | 
 | 366 |  | 
 | 367 | void* BpMemoryHeap::getBase() const { | 
 | 368 |     assertMapped(); | 
 | 369 |     return mBase; | 
 | 370 | } | 
 | 371 |  | 
 | 372 | size_t BpMemoryHeap::getSize() const { | 
 | 373 |     assertMapped(); | 
 | 374 |     return mSize; | 
 | 375 | } | 
 | 376 |  | 
 | 377 | uint32_t BpMemoryHeap::getFlags() const { | 
 | 378 |     assertMapped(); | 
 | 379 |     return mFlags; | 
 | 380 | } | 
 | 381 |  | 
| Andy Hung | 5b02852 | 2018-10-22 15:29:49 -0700 | [diff] [blame] | 382 | off_t BpMemoryHeap::getOffset() const { | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 383 |     assertMapped(); | 
 | 384 |     return mOffset; | 
 | 385 | } | 
 | 386 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | // --------------------------------------------------------------------------- | 
 | 388 |  | 
 | 389 | IMPLEMENT_META_INTERFACE(MemoryHeap, "android.utils.IMemoryHeap"); | 
 | 390 |  | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 391 | BnMemoryHeap::BnMemoryHeap() { | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 392 | } | 
 | 393 |  | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 394 | BnMemoryHeap::~BnMemoryHeap() { | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 395 | } | 
 | 396 |  | 
| Jiyong Park | b86c866 | 2018-10-29 23:01:57 +0900 | [diff] [blame] | 397 | // NOLINTNEXTLINE(google-default-arguments) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | status_t BnMemoryHeap::onTransact( | 
| Mathias Agopian | 83c0446 | 2009-05-22 19:00:22 -0700 | [diff] [blame] | 399 |         uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | { | 
 | 401 |     switch(code) { | 
 | 402 |        case HEAP_ID: { | 
 | 403 |             CHECK_INTERFACE(IMemoryHeap, data, reply); | 
 | 404 |             reply->writeFileDescriptor(getHeapID()); | 
| Andy Hung | 5b02852 | 2018-10-22 15:29:49 -0700 | [diff] [blame] | 405 |             reply->writeUint64(getSize()); | 
 | 406 |             reply->writeInt64(getOffset()); | 
 | 407 |             reply->writeUint32(getFlags()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 |             return NO_ERROR; | 
 | 409 |         } break; | 
 | 410 |         default: | 
 | 411 |             return BBinder::onTransact(code, data, reply, flags); | 
 | 412 |     } | 
 | 413 | } | 
 | 414 |  | 
 | 415 | /*****************************************************************************/ | 
 | 416 |  | 
 | 417 | HeapCache::HeapCache() | 
 | 418 |     : DeathRecipient() | 
 | 419 | { | 
 | 420 | } | 
 | 421 |  | 
 | 422 | HeapCache::~HeapCache() | 
 | 423 | { | 
 | 424 | } | 
 | 425 |  | 
 | 426 | void HeapCache::binderDied(const wp<IBinder>& binder) | 
 | 427 | { | 
| Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 428 |     //ALOGD("binderDied binder=%p", binder.unsafe_get()); | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 429 |     free_heap(binder); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | } | 
 | 431 |  | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 432 | sp<IMemoryHeap> HeapCache::find_heap(const sp<IBinder>& binder) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | { | 
 | 434 |     Mutex::Autolock _l(mHeapCacheLock); | 
 | 435 |     ssize_t i = mHeapCache.indexOfKey(binder); | 
 | 436 |     if (i>=0) { | 
 | 437 |         heap_info_t& info = mHeapCache.editValueAt(i); | 
| Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 438 |         ALOGD_IF(VERBOSE, | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 439 |                 "found binder=%p, heap=%p, size=%zu, fd=%d, count=%d", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 |                 binder.get(), info.heap.get(), | 
 | 441 |                 static_cast<BpMemoryHeap*>(info.heap.get())->mSize, | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 442 |                 static_cast<BpMemoryHeap*>(info.heap.get()) | 
 | 443 |                     ->mHeapId.load(memory_order_relaxed), | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 |                 info.count); | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 445 |         ++info.count; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 446 |         return info.heap; | 
 | 447 |     } else { | 
 | 448 |         heap_info_t info; | 
 | 449 |         info.heap = interface_cast<IMemoryHeap>(binder); | 
 | 450 |         info.count = 1; | 
| Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 451 |         //ALOGD("adding binder=%p, heap=%p, count=%d", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 452 |         //      binder.get(), info.heap.get(), info.count); | 
 | 453 |         mHeapCache.add(binder, info); | 
 | 454 |         return info.heap; | 
 | 455 |     } | 
 | 456 | } | 
 | 457 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | void HeapCache::free_heap(const sp<IBinder>& binder)  { | 
 | 459 |     free_heap( wp<IBinder>(binder) ); | 
 | 460 | } | 
 | 461 |  | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 462 | void HeapCache::free_heap(const wp<IBinder>& binder) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | { | 
 | 464 |     sp<IMemoryHeap> rel; | 
 | 465 |     { | 
 | 466 |         Mutex::Autolock _l(mHeapCacheLock); | 
 | 467 |         ssize_t i = mHeapCache.indexOfKey(binder); | 
 | 468 |         if (i>=0) { | 
 | 469 |             heap_info_t& info(mHeapCache.editValueAt(i)); | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 470 |             if (--info.count == 0) { | 
| Steve Block | 9d45368 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 471 |                 ALOGD_IF(VERBOSE, | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 472 |                         "removing binder=%p, heap=%p, size=%zu, fd=%d, count=%d", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 473 |                         binder.unsafe_get(), info.heap.get(), | 
 | 474 |                         static_cast<BpMemoryHeap*>(info.heap.get())->mSize, | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 475 |                         static_cast<BpMemoryHeap*>(info.heap.get()) | 
 | 476 |                             ->mHeapId.load(memory_order_relaxed), | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 |                         info.count); | 
 | 478 |                 rel = mHeapCache.valueAt(i).heap; | 
 | 479 |                 mHeapCache.removeItemsAt(i); | 
 | 480 |             } | 
 | 481 |         } else { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 482 |             ALOGE("free_heap binder=%p not found!!!", binder.unsafe_get()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 |         } | 
 | 484 |     } | 
 | 485 | } | 
 | 486 |  | 
 | 487 | sp<IMemoryHeap> HeapCache::get_heap(const sp<IBinder>& binder) | 
 | 488 | { | 
 | 489 |     sp<IMemoryHeap> realHeap; | 
 | 490 |     Mutex::Autolock _l(mHeapCacheLock); | 
 | 491 |     ssize_t i = mHeapCache.indexOfKey(binder); | 
 | 492 |     if (i>=0)   realHeap = mHeapCache.valueAt(i).heap; | 
 | 493 |     else        realHeap = interface_cast<IMemoryHeap>(binder); | 
 | 494 |     return realHeap; | 
 | 495 | } | 
 | 496 |  | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 497 | void HeapCache::dump_heaps() | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | { | 
 | 499 |     Mutex::Autolock _l(mHeapCacheLock); | 
 | 500 |     int c = mHeapCache.size(); | 
 | 501 |     for (int i=0 ; i<c ; i++) { | 
 | 502 |         const heap_info_t& info = mHeapCache.valueAt(i); | 
 | 503 |         BpMemoryHeap const* h(static_cast<BpMemoryHeap const *>(info.heap.get())); | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 504 |         ALOGD("hey=%p, heap=%p, count=%d, (fd=%d, base=%p, size=%zu)", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 |                 mHeapCache.keyAt(i).unsafe_get(), | 
| Anu Sundararajan | 5728a92 | 2011-06-22 15:58:59 -0500 | [diff] [blame] | 506 |                 info.heap.get(), info.count, | 
| Hans Boehm | 7e20297 | 2016-07-12 18:05:49 -0700 | [diff] [blame] | 507 |                 h->mHeapId.load(memory_order_relaxed), h->mBase, h->mSize); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 508 |     } | 
 | 509 | } | 
 | 510 |  | 
 | 511 |  | 
 | 512 | // --------------------------------------------------------------------------- | 
 | 513 | }; // namespace android |