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