1 //===-- ConstString.cpp -----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Utility/ConstString.h"
11 
12 #include "lldb/Utility/Stream.h"
13 
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/ADT/iterator.h"            // for iterator_facade_base
16 #include "llvm/Support/Allocator.h"       // for BumpPtrAllocator
17 #include "llvm/Support/DJB.h"             // for djbHash
18 #include "llvm/Support/FormatProviders.h" // for format_provider
19 #include "llvm/Support/RWMutex.h"
20 #include "llvm/Support/Threading.h"
21 
22 #include <algorithm> // for min
23 #include <array>
24 #include <utility> // for make_pair, pair
25 
26 #include <inttypes.h> // for PRIu64
27 #include <stdint.h>   // for uint8_t, uint32_t, uint64_t
28 #include <string.h>   // for size_t, strlen
29 
30 using namespace lldb_private;
31 
32 class Pool {
33 public:
34   typedef const char *StringPoolValueType;
35   typedef llvm::StringMap<StringPoolValueType, llvm::BumpPtrAllocator>
36       StringPool;
37   typedef llvm::StringMapEntry<StringPoolValueType> StringPoolEntryType;
38 
39   static StringPoolEntryType &
40   GetStringMapEntryFromKeyData(const char *keyData) {
41     return StringPoolEntryType::GetStringMapEntryFromKeyData(keyData);
42   }
43 
44   static size_t GetConstCStringLength(const char *ccstr) {
45     if (ccstr != nullptr) {
46       // Since the entry is read only, and we derive the entry entirely from
47       // the pointer, we don't need the lock.
48       const StringPoolEntryType &entry = GetStringMapEntryFromKeyData(ccstr);
49       return entry.getKey().size();
50     }
51     return 0;
52   }
53 
54   StringPoolValueType GetMangledCounterpart(const char *ccstr) const {
55     if (ccstr != nullptr) {
56       const uint8_t h = hash(llvm::StringRef(ccstr));
57       llvm::sys::SmartScopedReader<false> rlock(m_string_pools[h].m_mutex);
58       return GetStringMapEntryFromKeyData(ccstr).getValue();
59     }
60     return nullptr;
61   }
62 
63   bool SetMangledCounterparts(const char *key_ccstr, const char *value_ccstr) {
64     if (key_ccstr != nullptr && value_ccstr != nullptr) {
65       {
66         const uint8_t h = hash(llvm::StringRef(key_ccstr));
67         llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
68         GetStringMapEntryFromKeyData(key_ccstr).setValue(value_ccstr);
69       }
70       {
71         const uint8_t h = hash(llvm::StringRef(value_ccstr));
72         llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
73         GetStringMapEntryFromKeyData(value_ccstr).setValue(key_ccstr);
74       }
75       return true;
76     }
77     return false;
78   }
79 
80   const char *GetConstCString(const char *cstr) {
81     if (cstr != nullptr)
82       return GetConstCStringWithLength(cstr, strlen(cstr));
83     return nullptr;
84   }
85 
86   const char *GetConstCStringWithLength(const char *cstr, size_t cstr_len) {
87     if (cstr != nullptr)
88       return GetConstCStringWithStringRef(llvm::StringRef(cstr, cstr_len));
89     return nullptr;
90   }
91 
92   const char *GetConstCStringWithStringRef(const llvm::StringRef &string_ref) {
93     if (string_ref.data()) {
94       const uint8_t h = hash(string_ref);
95 
96       {
97         llvm::sys::SmartScopedReader<false> rlock(m_string_pools[h].m_mutex);
98         auto it = m_string_pools[h].m_string_map.find(string_ref);
99         if (it != m_string_pools[h].m_string_map.end())
100           return it->getKeyData();
101       }
102 
103       llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
104       StringPoolEntryType &entry =
105           *m_string_pools[h]
106                .m_string_map.insert(std::make_pair(string_ref, nullptr))
107                .first;
108       return entry.getKeyData();
109     }
110     return nullptr;
111   }
112 
113   const char *
114   GetConstCStringAndSetMangledCounterPart(llvm::StringRef demangled,
115                                           const char *mangled_ccstr) {
116     const char *demangled_ccstr = nullptr;
117 
118     {
119       const uint8_t h = hash(demangled);
120       llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
121 
122       // Make or update string pool entry with the mangled counterpart
123       StringPool &map = m_string_pools[h].m_string_map;
124       StringPoolEntryType &entry = *map.try_emplace(demangled).first;
125 
126       assert((entry.second == nullptr || entry.second == mangled_ccstr ||
127               strlen(entry.second) == 0) &&
128              "The demangled string must have a unique counterpart or otherwise "
129              "it must be empty");
130 
131       entry.second = mangled_ccstr;
132 
133       // Extract the const version of the demangled_cstr
134       demangled_ccstr = entry.getKeyData();
135     }
136 
137     {
138       // Now assign the demangled const string as the counterpart of the
139       // mangled const string...
140       const uint8_t h = hash(llvm::StringRef(mangled_ccstr));
141       llvm::sys::SmartScopedWriter<false> wlock(m_string_pools[h].m_mutex);
142       GetStringMapEntryFromKeyData(mangled_ccstr).setValue(demangled_ccstr);
143     }
144 
145     // Return the constant demangled C string
146     return demangled_ccstr;
147   }
148 
149   const char *GetConstTrimmedCStringWithLength(const char *cstr,
150                                                size_t cstr_len) {
151     if (cstr != nullptr) {
152       const size_t trimmed_len = std::min<size_t>(strlen(cstr), cstr_len);
153       return GetConstCStringWithLength(cstr, trimmed_len);
154     }
155     return nullptr;
156   }
157 
158   //------------------------------------------------------------------
159   // Return the size in bytes that this object and any items in its collection
160   // of uniqued strings + data count values takes in memory.
161   //------------------------------------------------------------------
162   size_t MemorySize() const {
163     size_t mem_size = sizeof(Pool);
164     for (const auto &pool : m_string_pools) {
165       llvm::sys::SmartScopedReader<false> rlock(pool.m_mutex);
166       for (const auto &entry : pool.m_string_map)
167         mem_size += sizeof(StringPoolEntryType) + entry.getKey().size();
168     }
169     return mem_size;
170   }
171 
172 protected:
173   uint8_t hash(const llvm::StringRef &s) const {
174     uint32_t h = llvm::djbHash(s);
175     return ((h >> 24) ^ (h >> 16) ^ (h >> 8) ^ h) & 0xff;
176   }
177 
178   struct PoolEntry {
179     mutable llvm::sys::SmartRWMutex<false> m_mutex;
180     StringPool m_string_map;
181   };
182 
183   std::array<PoolEntry, 256> m_string_pools;
184 };
185 
186 //----------------------------------------------------------------------
187 // Frameworks and dylibs aren't supposed to have global C++ initializers so we
188 // hide the string pool in a static function so that it will get initialized on
189 // the first call to this static function.
190 //
191 // Note, for now we make the string pool a pointer to the pool, because we
192 // can't guarantee that some objects won't get destroyed after the global
193 // destructor chain is run, and trying to make sure no destructors touch
194 // ConstStrings is difficult.  So we leak the pool instead.
195 //----------------------------------------------------------------------
196 static Pool &StringPool() {
197   static llvm::once_flag g_pool_initialization_flag;
198   static Pool *g_string_pool = nullptr;
199 
200   llvm::call_once(g_pool_initialization_flag,
201                  []() { g_string_pool = new Pool(); });
202 
203   return *g_string_pool;
204 }
205 
206 ConstString::ConstString(const char *cstr)
207     : m_string(StringPool().GetConstCString(cstr)) {}
208 
209 ConstString::ConstString(const char *cstr, size_t cstr_len)
210     : m_string(StringPool().GetConstCStringWithLength(cstr, cstr_len)) {}
211 
212 ConstString::ConstString(const llvm::StringRef &s)
213     : m_string(StringPool().GetConstCStringWithLength(s.data(), s.size())) {}
214 
215 bool ConstString::operator<(const ConstString &rhs) const {
216   if (m_string == rhs.m_string)
217     return false;
218 
219   llvm::StringRef lhs_string_ref(GetStringRef());
220   llvm::StringRef rhs_string_ref(rhs.GetStringRef());
221 
222   // If both have valid C strings, then return the comparison
223   if (lhs_string_ref.data() && rhs_string_ref.data())
224     return lhs_string_ref < rhs_string_ref;
225 
226   // Else one of them was nullptr, so if LHS is nullptr then it is less than
227   return lhs_string_ref.data() == nullptr;
228 }
229 
230 Stream &lldb_private::operator<<(Stream &s, const ConstString &str) {
231   const char *cstr = str.GetCString();
232   if (cstr != nullptr)
233     s << cstr;
234 
235   return s;
236 }
237 
238 size_t ConstString::GetLength() const {
239   return Pool::GetConstCStringLength(m_string);
240 }
241 
242 bool ConstString::Equals(const ConstString &lhs, const ConstString &rhs,
243                          const bool case_sensitive) {
244   if (lhs.m_string == rhs.m_string)
245     return true;
246 
247   // Since the pointers weren't equal, and identical ConstStrings always have
248   // identical pointers, the result must be false for case sensitive equality
249   // test.
250   if (case_sensitive)
251     return false;
252 
253   // perform case insensitive equality test
254   llvm::StringRef lhs_string_ref(lhs.GetStringRef());
255   llvm::StringRef rhs_string_ref(rhs.GetStringRef());
256   return lhs_string_ref.equals_lower(rhs_string_ref);
257 }
258 
259 int ConstString::Compare(const ConstString &lhs, const ConstString &rhs,
260                          const bool case_sensitive) {
261   // If the iterators are the same, this is the same string
262   const char *lhs_cstr = lhs.m_string;
263   const char *rhs_cstr = rhs.m_string;
264   if (lhs_cstr == rhs_cstr)
265     return 0;
266   if (lhs_cstr && rhs_cstr) {
267     llvm::StringRef lhs_string_ref(lhs.GetStringRef());
268     llvm::StringRef rhs_string_ref(rhs.GetStringRef());
269 
270     if (case_sensitive) {
271       return lhs_string_ref.compare(rhs_string_ref);
272     } else {
273       return lhs_string_ref.compare_lower(rhs_string_ref);
274     }
275   }
276 
277   if (lhs_cstr)
278     return +1; // LHS isn't nullptr but RHS is
279   else
280     return -1; // LHS is nullptr but RHS isn't
281 }
282 
283 void ConstString::Dump(Stream *s, const char *fail_value) const {
284   if (s != nullptr) {
285     const char *cstr = AsCString(fail_value);
286     if (cstr != nullptr)
287       s->PutCString(cstr);
288   }
289 }
290 
291 void ConstString::DumpDebug(Stream *s) const {
292   const char *cstr = GetCString();
293   size_t cstr_len = GetLength();
294   // Only print the parens if we have a non-nullptr string
295   const char *parens = cstr ? "\"" : "";
296   s->Printf("%*p: ConstString, string = %s%s%s, length = %" PRIu64,
297             static_cast<int>(sizeof(void *) * 2),
298             static_cast<const void *>(this), parens, cstr, parens,
299             static_cast<uint64_t>(cstr_len));
300 }
301 
302 void ConstString::SetCString(const char *cstr) {
303   m_string = StringPool().GetConstCString(cstr);
304 }
305 
306 void ConstString::SetString(const llvm::StringRef &s) {
307   m_string = StringPool().GetConstCStringWithLength(s.data(), s.size());
308 }
309 
310 void ConstString::SetStringWithMangledCounterpart(llvm::StringRef demangled,
311                                                    const ConstString &mangled) {
312   m_string = StringPool().GetConstCStringAndSetMangledCounterPart(
313       demangled, mangled.m_string);
314 }
315 
316 bool ConstString::GetMangledCounterpart(ConstString &counterpart) const {
317   counterpart.m_string = StringPool().GetMangledCounterpart(m_string);
318   return (bool)counterpart;
319 }
320 
321 void ConstString::SetCStringWithLength(const char *cstr, size_t cstr_len) {
322   m_string = StringPool().GetConstCStringWithLength(cstr, cstr_len);
323 }
324 
325 void ConstString::SetTrimmedCStringWithLength(const char *cstr,
326                                               size_t cstr_len) {
327   m_string = StringPool().GetConstTrimmedCStringWithLength(cstr, cstr_len);
328 }
329 
330 size_t ConstString::StaticMemorySize() {
331   // Get the size of the static string pool
332   return StringPool().MemorySize();
333 }
334 
335 void llvm::format_provider<ConstString>::format(const ConstString &CS,
336                                                 llvm::raw_ostream &OS,
337                                                 llvm::StringRef Options) {
338   format_provider<StringRef>::format(CS.AsCString(), OS, Options);
339 }
340