Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 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 | |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 22 | #include "dex/utf.h" |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 23 | #include "gc/space/image_space.h" |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 24 | #include "gc_root-inl.h" |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 25 | #include "image.h" |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 26 | #include "mirror/string-inl.h" |
| 27 | #include "thread-current-inl.h" |
Mathieu Chartier | cd0f38f | 2018-10-15 09:44:35 -0700 | [diff] [blame] | 28 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
Vladimir Marko | cdc3ab0 | 2022-04-05 13:33:12 +0000 | [diff] [blame] | 31 | ALWAYS_INLINE |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 32 | inline uint32_t InternTable::Utf8String::Hash(uint32_t utf16_length, const char* utf8_data) { |
Vladimir Marko | 4625f25 | 2022-02-23 11:20:26 +0000 | [diff] [blame] | 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 | |
Vladimir Marko | cdc3ab0 | 2022-04-05 13:33:12 +0000 | [diff] [blame] | 43 | ALWAYS_INLINE |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 44 | inline size_t InternTable::StringHash::operator()(const GcRoot<mirror::String>& root) const { |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 45 | if (kIsDebugBuild) { |
| 46 | Locks::mutator_lock_->AssertSharedHeld(Thread::Current()); |
| 47 | } |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 48 | ObjPtr<mirror::String> s = root.Read<kWithoutReadBarrier>(); |
| 49 | int32_t hash = s->GetStoredHashCode(); |
| 50 | DCHECK_EQ(hash, s->ComputeHashCode()); |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 51 | // An additional cast to prevent undesired sign extension. |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 52 | return static_cast<uint32_t>(hash); |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Vladimir Marko | cdc3ab0 | 2022-04-05 13:33:12 +0000 | [diff] [blame] | 55 | ALWAYS_INLINE |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 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 | |
Vladimir Marko | cdc3ab0 | 2022-04-05 13:33:12 +0000 | [diff] [blame] | 64 | ALWAYS_INLINE |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 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 | } |
Vladimir Marko | 4625f25 | 2022-02-23 11:20:26 +0000 | [diff] [blame] | 75 | DCHECK_GE(strlen(b.GetUtf8Data()), a_length); |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 76 | if (a_string->IsCompressed()) { |
Vladimir Marko | 4625f25 | 2022-02-23 11:20:26 +0000 | [diff] [blame] | 77 | // Modified UTF-8 single byte character range is 0x01 .. 0x7f. |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 78 | // The string compression occurs on regular ASCII with same exact range, |
Vladimir Marko | 4625f25 | 2022-02-23 11:20:26 +0000 | [diff] [blame] | 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; |
Vladimir Marko | 4e7b3c7 | 2021-02-23 18:13:47 +0000 | [diff] [blame] | 85 | } else { |
| 86 | const uint16_t* a_value = a_string->GetValue(); |
| 87 | return CompareModifiedUtf8ToUtf16AsCodePointValues(b.GetUtf8Data(), a_value, a_length) == 0; |
| 88 | } |
| 89 | } |
| 90 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 91 | template <typename 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. |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 96 | const ImageHeader& header = image_space->GetImageHeader(); |
| 97 | const ImageSection& section = header.GetInternedStringsSection(); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 98 | if (section.Size() > 0) { |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 99 | AddTableFromMemory(image_space->Begin() + section.Offset(), visitor, !header.IsAppImage()); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | template <typename Visitor> |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 104 | inline size_t InternTable::AddTableFromMemory(const uint8_t* ptr, |
| 105 | const Visitor& visitor, |
| 106 | bool is_boot_image) { |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 107 | size_t read_count = 0; |
| 108 | UnorderedSet set(ptr, /*make copy*/false, &read_count); |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 109 | { |
| 110 | // Hold the lock while calling the visitor to prevent possible race |
| 111 | // conditions with another thread adding intern strings. |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 112 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 113 | // Visit the unordered set, may remove elements. |
| 114 | visitor(set); |
| 115 | if (!set.empty()) { |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 116 | strong_interns_.AddInternStrings(std::move(set), is_boot_image); |
Mathieu Chartier | 41c0808 | 2018-10-31 11:50:26 -0700 | [diff] [blame] | 117 | } |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 118 | } |
| 119 | return read_count; |
| 120 | } |
| 121 | |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 122 | inline void InternTable::Table::AddInternStrings(UnorderedSet&& intern_strings, |
| 123 | bool is_boot_image) { |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 124 | if (kIsDebugBuild) { |
Mathieu Chartier | 2547af3 | 2018-10-16 17:48:20 -0700 | [diff] [blame] | 125 | // Avoid doing read barriers since the space might not yet be added to the heap. |
| 126 | // See b/117803941 |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 127 | for (GcRoot<mirror::String>& string : intern_strings) { |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 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) |
Mathieu Chartier | 2547af3 | 2018-10-16 17:48:20 -0700 | [diff] [blame] | 132 | << "Already found " << string.Read<kWithoutReadBarrier>()->ToModifiedUtf8() |
| 133 | << " in the intern table"; |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 134 | } |
| 135 | } |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 136 | |
Vladimir Marko | 47eea60 | 2022-04-04 10:27:25 +0000 | [diff] [blame] | 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)); |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | template <typename Visitor> |
| 145 | inline void InternTable::VisitInterns(const Visitor& visitor, |
| 146 | bool visit_boot_images, |
| 147 | bool visit_non_boot_images) { |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 148 | auto visit_tables = [&](dchecked_vector<Table::InternalTable>& tables) |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 149 | NO_THREAD_SAFETY_ANALYSIS { |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 150 | for (Table::InternalTable& table : tables) { |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 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; |
Mathieu Chartier | 8cc418e | 2018-10-31 10:54:30 -0700 | [diff] [blame] | 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_); |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 164 | inline size_t InternTable::CountInterns(bool visit_boot_images, bool visit_non_boot_images) const { |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 165 | size_t ret = 0u; |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 166 | auto visit_tables = [&](const dchecked_vector<Table::InternalTable>& tables) |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 167 | NO_THREAD_SAFETY_ANALYSIS { |
| 168 | for (const Table::InternalTable& table : tables) { |
Vladimir Marko | 365c020 | 2022-03-22 09:53:31 +0000 | [diff] [blame] | 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; |
Mathieu Chartier | 8fc7558 | 2018-11-01 14:21:33 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | 74ccee6 | 2018-10-10 10:30:29 -0700 | [diff] [blame] | 181 | } // namespace art |
| 182 | |
| 183 | #endif // ART_RUNTIME_INTERN_TABLE_INL_H_ |