Colin Cross | f572b91 | 2017-06-20 18:07:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include <sys/cdefs.h> |
| 18 | #include <unistd.h> |
| 19 | |
| 20 | #include <functional> |
| 21 | |
| 22 | #include "Binder.h" |
| 23 | #include "log.h" |
| 24 | |
| 25 | __BEGIN_DECLS |
| 26 | |
| 27 | // Weak undefined references to the symbols in libbinder and libhwbinder |
| 28 | // so that libmemunreachable can call them in processes that have them |
| 29 | // loaded without requiring libmemunreachable to have dependencies on them. |
| 30 | ssize_t __attribute__((weak)) getBinderKernelReferences(size_t, uintptr_t*); |
| 31 | ssize_t __attribute__((weak)) getHWBinderKernelReferences(size_t, uintptr_t*); |
| 32 | |
| 33 | __END_DECLS |
| 34 | |
| 35 | namespace android { |
| 36 | |
| 37 | static bool BinderReferencesToVector(allocator::vector<uintptr_t>& refs, |
| 38 | std::function<ssize_t(size_t, uintptr_t*)> fn) { |
| 39 | if (fn == nullptr) { |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | size_t size = refs.size(); |
| 44 | |
| 45 | do { |
| 46 | refs.resize(size); |
| 47 | |
| 48 | ssize_t ret = fn(refs.size(), refs.data()); |
| 49 | if (ret < 0) { |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | size = ret; |
| 54 | } while (size > refs.size()); |
| 55 | |
| 56 | refs.resize(size); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | bool BinderReferences(allocator::vector<uintptr_t>& refs) { |
| 61 | refs.clear(); |
| 62 | |
| 63 | allocator::vector<uintptr_t> binder_refs{refs.get_allocator()}; |
| 64 | if (BinderReferencesToVector(refs, getBinderKernelReferences)) { |
| 65 | refs.insert(refs.end(), binder_refs.begin(), binder_refs.end()); |
| 66 | } else { |
| 67 | MEM_ALOGE("getBinderKernelReferences failed"); |
| 68 | } |
| 69 | |
| 70 | allocator::vector<uintptr_t> hwbinder_refs{refs.get_allocator()}; |
| 71 | if (BinderReferencesToVector(hwbinder_refs, getHWBinderKernelReferences)) { |
| 72 | refs.insert(refs.end(), hwbinder_refs.begin(), hwbinder_refs.end()); |
| 73 | } else { |
| 74 | MEM_ALOGE("getHWBinderKernelReferences failed"); |
| 75 | } |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | } // namespace android |