Binder.cpp 2.2 KB

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