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