ElfCache.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright 2014-present Facebook, Inc.
  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. #pragma once
  17. #include <climits> // for PATH_MAX
  18. #include <cstring>
  19. #include <memory>
  20. #include <mutex>
  21. #include <string>
  22. #include <unordered_map>
  23. #include <vector>
  24. #include <boost/container/flat_map.hpp>
  25. #include <boost/intrusive/list.hpp>
  26. #include <boost/operators.hpp>
  27. #include <glog/logging.h>
  28. #include <folly/Range.h>
  29. #include <folly/experimental/symbolizer/Elf.h>
  30. #include <folly/hash/Hash.h>
  31. namespace folly {
  32. namespace symbolizer {
  33. /**
  34. * Number of ELF files loaded by the dynamic loader.
  35. */
  36. size_t countLoadedElfFiles();
  37. class ElfCacheBase {
  38. public:
  39. virtual std::shared_ptr<ElfFile> getFile(StringPiece path) = 0;
  40. virtual ~ElfCacheBase() {}
  41. };
  42. /**
  43. * Cache ELF files. Async-signal-safe: does memory allocation upfront.
  44. *
  45. * Will not grow; once the capacity is reached, lookups for files that
  46. * aren't already in the cache will fail (return nullptr).
  47. *
  48. * Not MT-safe. May not be used concurrently from multiple threads.
  49. *
  50. * NOTE that async-signal-safety is preserved only as long as the
  51. * SignalSafeElfCache object exists; after the SignalSafeElfCache object
  52. * is destroyed, destroying returned shared_ptr<ElfFile> objects may
  53. * cause ElfFile objects to be destroyed, and that's not async-signal-safe.
  54. */
  55. class SignalSafeElfCache : public ElfCacheBase {
  56. public:
  57. explicit SignalSafeElfCache(size_t capacity);
  58. std::shared_ptr<ElfFile> getFile(StringPiece path) override;
  59. private:
  60. // We can't use std::string (allocating memory is bad!) so we roll our
  61. // own wrapper around a fixed-size, null-terminated string.
  62. class Path : private boost::totally_ordered<Path> {
  63. public:
  64. Path() {
  65. assign(folly::StringPiece());
  66. }
  67. explicit Path(StringPiece s) {
  68. assign(s);
  69. }
  70. void assign(StringPiece s) {
  71. DCHECK_LE(s.size(), kMaxSize);
  72. if (!s.empty()) {
  73. memcpy(data_, s.data(), s.size());
  74. }
  75. data_[s.size()] = '\0';
  76. }
  77. bool operator<(const Path& other) const {
  78. return strcmp(data_, other.data_) < 0;
  79. }
  80. bool operator==(const Path& other) const {
  81. return strcmp(data_, other.data_) == 0;
  82. }
  83. const char* data() const {
  84. return data_;
  85. }
  86. static constexpr size_t kMaxSize = PATH_MAX - 1;
  87. private:
  88. char data_[kMaxSize + 1];
  89. };
  90. Path scratchpad_; // Preallocated key for map_ lookups.
  91. boost::container::flat_map<Path, int> map_;
  92. std::vector<std::shared_ptr<ElfFile>> slots_;
  93. };
  94. /**
  95. * General-purpose ELF file cache.
  96. *
  97. * LRU of given capacity. MT-safe (uses locking). Not async-signal-safe.
  98. */
  99. class ElfCache : public ElfCacheBase {
  100. public:
  101. explicit ElfCache(size_t capacity);
  102. std::shared_ptr<ElfFile> getFile(StringPiece path) override;
  103. private:
  104. std::mutex mutex_;
  105. typedef boost::intrusive::list_member_hook<> LruLink;
  106. struct Entry {
  107. std::string path;
  108. ElfFile file;
  109. LruLink lruLink;
  110. };
  111. static std::shared_ptr<ElfFile> filePtr(const std::shared_ptr<Entry>& e);
  112. size_t capacity_;
  113. std::unordered_map<StringPiece, std::shared_ptr<Entry>, Hash> files_;
  114. typedef boost::intrusive::list<
  115. Entry,
  116. boost::intrusive::member_hook<Entry, LruLink, &Entry::lruLink>,
  117. boost::intrusive::constant_time_size<false>>
  118. LruList;
  119. LruList lruList_;
  120. };
  121. } // namespace symbolizer
  122. } // namespace folly