• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 #ifndef ART_RUNTIME_INTERN_TABLE_INL_H_
18 #define ART_RUNTIME_INTERN_TABLE_INL_H_
19 
20 #include "intern_table.h"
21 
22 #include "dex/utf.h"
23 #include "gc/space/image_space.h"
24 #include "gc_root-inl.h"
25 #include "image.h"
26 #include "mirror/string-inl.h"
27 #include "thread-current-inl.h"
28 
29 namespace art {
30 
31 ALWAYS_INLINE
Hash(uint32_t utf16_length,const char * utf8_data)32 inline uint32_t InternTable::Utf8String::Hash(uint32_t utf16_length, const char* utf8_data) {
33   DCHECK_EQ(utf16_length, CountModifiedUtf8Chars(utf8_data));
34   if (LIKELY(utf8_data[utf16_length] == 0)) {
35     int32_t hash = ComputeUtf16Hash(utf8_data, utf16_length);
36     DCHECK_EQ(hash, ComputeUtf16HashFromModifiedUtf8(utf8_data, utf16_length));
37     return hash;
38   } else {
39     return ComputeUtf16HashFromModifiedUtf8(utf8_data, utf16_length);
40   }
41 }
42 
43 ALWAYS_INLINE
operator()44 inline size_t InternTable::StringHash::operator()(const GcRoot<mirror::String>& root) const {
45   if (kIsDebugBuild) {
46     Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
47   }
48   ObjPtr<mirror::String> s = root.Read<kWithoutReadBarrier>();
49   int32_t hash = s->GetStoredHashCode();
50   DCHECK_EQ(hash, s->ComputeHashCode());
51   // An additional cast to prevent undesired sign extension.
52   return static_cast<uint32_t>(hash);
53 }
54 
55 ALWAYS_INLINE
operator()56 inline bool InternTable::StringEquals::operator()(const GcRoot<mirror::String>& a,
57                                                   const GcRoot<mirror::String>& b) const {
58   if (kIsDebugBuild) {
59     Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
60   }
61   return a.Read<kWithoutReadBarrier>()->Equals(b.Read<kWithoutReadBarrier>());
62 }
63 
64 ALWAYS_INLINE
operator()65 inline bool InternTable::StringEquals::operator()(const GcRoot<mirror::String>& a,
66                                                   const Utf8String& b) const {
67   if (kIsDebugBuild) {
68     Locks::mutator_lock_->AssertSharedHeld(Thread::Current());
69   }
70   ObjPtr<mirror::String> a_string = a.Read<kWithoutReadBarrier>();
71   uint32_t a_length = static_cast<uint32_t>(a_string->GetLength());
72   if (a_length != b.GetUtf16Length()) {
73     return false;
74   }
75   DCHECK_GE(strlen(b.GetUtf8Data()), a_length);
76   if (a_string->IsCompressed()) {
77     // Modified UTF-8 single byte character range is 0x01 .. 0x7f.
78     // The string compression occurs on regular ASCII with same exact range,
79     // not on extended ASCII which is up to 0xff.
80     return b.GetUtf8Data()[a_length] == 0 &&
81            memcmp(b.GetUtf8Data(), a_string->GetValueCompressed(), a_length * sizeof(uint8_t)) == 0;
82   } else if (mirror::kUseStringCompression && b.GetUtf8Data()[a_length] == 0) {
83     // ASCII string `b` cannot equal non-ASCII `a_string`.
84     return false;
85   } else {
86     const uint16_t* a_value = a_string->GetValue();
87     return CompareModifiedUtf8ToUtf16AsCodePointValues(b.GetUtf8Data(), a_value, a_length) == 0;
88   }
89 }
90 
91 template <typename Visitor>
AddImageStringsToTable(gc::space::ImageSpace * image_space,const Visitor & visitor)92 inline void InternTable::AddImageStringsToTable(gc::space::ImageSpace* image_space,
93                                                 const Visitor& visitor) {
94   DCHECK(image_space != nullptr);
95   // Only add if we have the interned strings section.
96   const ImageHeader& header = image_space->GetImageHeader();
97   const ImageSection& section = header.GetInternedStringsSection();
98   if (section.Size() > 0) {
99     AddTableFromMemory(image_space->Begin() + section.Offset(), visitor, !header.IsAppImage());
100   }
101 }
102 
103 template <typename Visitor>
AddTableFromMemory(const uint8_t * ptr,const Visitor & visitor,bool is_boot_image)104 inline size_t InternTable::AddTableFromMemory(const uint8_t* ptr,
105                                               const Visitor& visitor,
106                                               bool is_boot_image) {
107   size_t read_count = 0;
108   UnorderedSet set(ptr, /*make copy*/false, &read_count);
109   {
110     // Hold the lock while calling the visitor to prevent possible race
111     // conditions with another thread adding intern strings.
112     MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
113     // Visit the unordered set, may remove elements.
114     visitor(set);
115     if (!set.empty()) {
116       strong_interns_.AddInternStrings(std::move(set), is_boot_image);
117     }
118   }
119   return read_count;
120 }
121 
AddInternStrings(UnorderedSet && intern_strings,bool is_boot_image)122 inline void InternTable::Table::AddInternStrings(UnorderedSet&& intern_strings,
123                                                  bool is_boot_image) {
124   if (kIsDebugBuild) {
125     // Avoid doing read barriers since the space might not yet be added to the heap.
126     // See b/117803941
127     for (GcRoot<mirror::String>& string : intern_strings) {
128       ObjPtr<mirror::String> s = string.Read<kWithoutReadBarrier>();
129       uint32_t hash = static_cast<uint32_t>(s->GetStoredHashCode());
130       CHECK_EQ(hash, static_cast<uint32_t>(s->ComputeHashCode()));
131       CHECK(Find(s, hash) == nullptr)
132           << "Already found " << string.Read<kWithoutReadBarrier>()->ToModifiedUtf8()
133           << " in the intern table";
134     }
135   }
136 
137   // Insert before the last (unfrozen) table since we add new interns into the back.
138   // Keep the order of previous frozen tables unchanged, so that we can can remember
139   // the number of searched frozen tables and not search them again.
140   DCHECK(!tables_.empty());
141   tables_.insert(tables_.end() - 1, InternalTable(std::move(intern_strings), is_boot_image));
142 }
143 
144 template <typename Visitor>
VisitInterns(const Visitor & visitor,bool visit_boot_images,bool visit_non_boot_images)145 inline void InternTable::VisitInterns(const Visitor& visitor,
146                                       bool visit_boot_images,
147                                       bool visit_non_boot_images) {
148   auto visit_tables = [&](dchecked_vector<Table::InternalTable>& tables)
149       NO_THREAD_SAFETY_ANALYSIS {
150     for (Table::InternalTable& table : tables) {
151       // Determine if we want to visit the table based on the flags.
152       const bool visit = table.IsBootImage() ? visit_boot_images : visit_non_boot_images;
153       if (visit) {
154         for (auto& intern : table.set_) {
155           visitor(intern);
156         }
157       }
158     }
159   };
160   visit_tables(strong_interns_.tables_);
161   visit_tables(weak_interns_.tables_);
162 }
163 
CountInterns(bool visit_boot_images,bool visit_non_boot_images)164 inline size_t InternTable::CountInterns(bool visit_boot_images, bool visit_non_boot_images) const {
165   size_t ret = 0u;
166   auto visit_tables = [&](const dchecked_vector<Table::InternalTable>& tables)
167       NO_THREAD_SAFETY_ANALYSIS {
168     for (const Table::InternalTable& table : tables) {
169       // Determine if we want to visit the table based on the flags.
170       const bool visit = table.IsBootImage() ? visit_boot_images : visit_non_boot_images;
171       if (visit) {
172         ret += table.set_.size();
173       }
174     }
175   };
176   visit_tables(strong_interns_.tables_);
177   visit_tables(weak_interns_.tables_);
178   return ret;
179 }
180 
181 }  // namespace art
182 
183 #endif  // ART_RUNTIME_INTERN_TABLE_INL_H_
184