blob: 9900c2a4e086db31b626c3d24499e311073aa976 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Brian Carlstrom7e93b502011-08-04 14:16:22 -070016
Vladimir Marko4e7b3c72021-02-23 18:13:47 +000017#include "intern_table-inl.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070018
Alexey Grebenkin21f23642016-12-02 17:44:54 +030019#include "base/hash_set.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080020#include "common_runtime_test.h"
David Sehr0225f8e2018-01-31 08:52:24 +000021#include "dex/utf.h"
Andreas Gampec15a2f42017-04-21 12:09:39 -070022#include "gc_root-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070023#include "handle_scope-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "mirror/object.h"
Ian Rogerse63db272014-07-15 15:36:11 -070025#include "mirror/string.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070026#include "scoped_thread_state_change-inl.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070027
Brian Carlstrom7e93b502011-08-04 14:16:22 -070028namespace art {
29
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080030class InternTableTest : public CommonRuntimeTest {};
Brian Carlstrom7e93b502011-08-04 14:16:22 -070031
32TEST_F(InternTableTest, Intern) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070033 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070034 InternTable intern_table;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070035 StackHandleScope<4> hs(soa.Self());
36 Handle<mirror::String> foo_1(hs.NewHandle(intern_table.InternStrong(3, "foo")));
37 Handle<mirror::String> foo_2(hs.NewHandle(intern_table.InternStrong(3, "foo")));
38 Handle<mirror::String> foo_3(
39 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
40 Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar")));
Andreas Gampefa4333d2017-02-14 11:10:34 -080041 ASSERT_TRUE(foo_1 != nullptr);
42 ASSERT_TRUE(foo_2 != nullptr);
43 ASSERT_TRUE(foo_3 != nullptr);
44 ASSERT_TRUE(bar != nullptr);
Vladimir Markocac5a7e2016-02-22 10:39:50 +000045 EXPECT_EQ(foo_1.Get(), foo_2.Get());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070046 EXPECT_TRUE(foo_1->Equals("foo"));
47 EXPECT_TRUE(foo_2->Equals("foo"));
48 EXPECT_TRUE(foo_3->Equals("foo"));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070049 EXPECT_NE(foo_1.Get(), bar.Get());
50 EXPECT_NE(foo_2.Get(), bar.Get());
51 EXPECT_NE(foo_3.Get(), bar.Get());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070052}
53
Elliott Hughescf4c6c42011-09-01 15:16:42 -070054TEST_F(InternTableTest, Size) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070055 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070056 InternTable t;
57 EXPECT_EQ(0U, t.Size());
58 t.InternStrong(3, "foo");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070059 StackHandleScope<1> hs(soa.Self());
60 Handle<mirror::String> foo(
61 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
62 t.InternWeak(foo.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070063 EXPECT_EQ(1U, t.Size());
64 t.InternStrong(3, "bar");
65 EXPECT_EQ(2U, t.Size());
66}
67
Alexey Grebenkin21f23642016-12-02 17:44:54 +030068// Check if table indexes match on 64 and 32 bit machines.
69// This is done by ensuring hash values are the same on every machine and limited to 32-bit wide.
70// Otherwise cross compilation can cause a table to be filled on host using one indexing algorithm
71// and later on a device with different sizeof(size_t) can use another indexing algorithm.
72// Thus the table may provide wrong data.
73TEST_F(InternTableTest, CrossHash) {
74 ScopedObjectAccess soa(Thread::Current());
75 InternTable t;
76
77 // A string that has a negative hash value.
Vladimir Marko365c0202022-03-22 09:53:31 +000078 ObjPtr<mirror::String> str = mirror::String::AllocFromModifiedUtf8(soa.Self(), "00000000");
79 // `String::GetHashCode()` ensures that the stored hash is calculated.
80 int32_t hash = str->GetHashCode();
81 ASSERT_LT(hash, 0);
Alexey Grebenkin21f23642016-12-02 17:44:54 +030082
83 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
Mathieu Chartier8cc418e2018-10-31 10:54:30 -070084 for (InternTable::Table::InternalTable& table : t.strong_interns_.tables_) {
Alexey Grebenkin21f23642016-12-02 17:44:54 +030085 // The negative hash value shall be 32-bit wide on every host.
Vladimir Marko365c0202022-03-22 09:53:31 +000086 ASSERT_TRUE(IsUint<32>(table.set_.hashfn_(GcRoot<mirror::String>(str))));
Alexey Grebenkin21f23642016-12-02 17:44:54 +030087 }
88}
89
Mathieu Chartier97509952015-07-13 14:35:43 -070090class TestPredicate : public IsMarkedVisitor {
Elliott Hughes410c0c82011-09-01 17:58:25 -070091 public:
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010092 mirror::Object* IsMarked(mirror::Object* s) override REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070093 bool erased = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -070094 for (auto it = expected_.begin(), end = expected_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070095 if (*it == s) {
96 expected_.erase(it);
97 erased = true;
98 break;
99 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700100 }
Elliott Hughes410c0c82011-09-01 17:58:25 -0700101 EXPECT_TRUE(erased);
Mathieu Chartier97509952015-07-13 14:35:43 -0700102 return nullptr;
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700103 }
Elliott Hughes410c0c82011-09-01 17:58:25 -0700104
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 void Expect(const mirror::String* s) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700106 expected_.push_back(s);
107 }
108
109 ~TestPredicate() {
110 EXPECT_EQ(0U, expected_.size());
111 }
112
113 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 mutable std::vector<const mirror::String*> expected_;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700115};
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700116
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700117TEST_F(InternTableTest, SweepInternTableWeaks) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700118 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700119 InternTable t;
120 t.InternStrong(3, "foo");
121 t.InternStrong(3, "bar");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700122 StackHandleScope<5> hs(soa.Self());
123 Handle<mirror::String> hello(
124 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello")));
125 Handle<mirror::String> world(
126 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "world")));
127 Handle<mirror::String> s0(hs.NewHandle(t.InternWeak(hello.Get())));
128 Handle<mirror::String> s1(hs.NewHandle(t.InternWeak(world.Get())));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700129
130 EXPECT_EQ(4U, t.Size());
131
132 // We should traverse only the weaks...
Elliott Hughes410c0c82011-09-01 17:58:25 -0700133 TestPredicate p;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700134 p.Expect(s0.Get());
135 p.Expect(s1.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700136 {
Ian Rogers1f539342012-10-03 21:09:42 -0700137 ReaderMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700138 t.SweepInternTableWeaks(&p);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700139 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700140
141 EXPECT_EQ(2U, t.Size());
142
Elliott Hughese5448b52012-01-18 16:44:06 -0800143 // Just check that we didn't corrupt the map.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700144 Handle<mirror::String> still_here(
145 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "still here")));
146 t.InternWeak(still_here.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700147 EXPECT_EQ(3U, t.Size());
148}
149
150TEST_F(InternTableTest, ContainsWeak) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700151 ScopedObjectAccess soa(Thread::Current());
Vladimir Marko365c0202022-03-22 09:53:31 +0000152 auto ContainsWeak = [&](InternTable& t, ObjPtr<mirror::String> s)
153 REQUIRES_SHARED(Locks::mutator_lock_) {
154 return t.LookupWeak(soa.Self(), s) == s;
155 };
156
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700157 {
158 // Strongs are never weak.
159 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700160 StackHandleScope<2> hs(soa.Self());
161 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo")));
Vladimir Marko365c0202022-03-22 09:53:31 +0000162 EXPECT_FALSE(ContainsWeak(t, interned_foo_1.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700163 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo")));
Vladimir Marko365c0202022-03-22 09:53:31 +0000164 EXPECT_FALSE(ContainsWeak(t, interned_foo_2.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700165 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700166 }
167
168 {
169 // Weaks are always weak.
170 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700171 StackHandleScope<4> hs(soa.Self());
172 Handle<mirror::String> foo_1(
173 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
174 Handle<mirror::String> foo_2(
175 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
176 EXPECT_NE(foo_1.Get(), foo_2.Get());
177 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo_1.Get())));
178 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo_2.Get())));
Vladimir Marko365c0202022-03-22 09:53:31 +0000179 EXPECT_TRUE(ContainsWeak(t, interned_foo_2.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700180 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700181 }
182
183 {
184 // A weak can be promoted to a strong.
185 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700186 StackHandleScope<3> hs(soa.Self());
187 Handle<mirror::String> foo(
188 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
189 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo.Get())));
Vladimir Marko365c0202022-03-22 09:53:31 +0000190 EXPECT_TRUE(ContainsWeak(t, interned_foo_1.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700191 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo")));
Vladimir Marko365c0202022-03-22 09:53:31 +0000192 EXPECT_FALSE(ContainsWeak(t, interned_foo_2.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700193 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700194 }
195
196 {
197 // Interning a weak after a strong gets you the strong.
198 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700199 StackHandleScope<3> hs(soa.Self());
200 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo")));
Vladimir Marko365c0202022-03-22 09:53:31 +0000201 EXPECT_FALSE(ContainsWeak(t, interned_foo_1.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700202 Handle<mirror::String> foo(
203 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
204 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo.Get())));
Vladimir Marko365c0202022-03-22 09:53:31 +0000205 EXPECT_FALSE(ContainsWeak(t, interned_foo_2.Get()));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700206 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700207 }
208}
209
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000210TEST_F(InternTableTest, LookupStrong) {
211 ScopedObjectAccess soa(Thread::Current());
212 InternTable intern_table;
213 StackHandleScope<3> hs(soa.Self());
214 Handle<mirror::String> foo(hs.NewHandle(intern_table.InternStrong(3, "foo")));
215 Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar")));
216 Handle<mirror::String> foobar(hs.NewHandle(intern_table.InternStrong(6, "foobar")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800217 ASSERT_TRUE(foo != nullptr);
218 ASSERT_TRUE(bar != nullptr);
219 ASSERT_TRUE(foobar != nullptr);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000220 ASSERT_TRUE(foo->Equals("foo"));
221 ASSERT_TRUE(bar->Equals("bar"));
222 ASSERT_TRUE(foobar->Equals("foobar"));
223 ASSERT_NE(foo.Get(), bar.Get());
224 ASSERT_NE(foo.Get(), foobar.Get());
225 ASSERT_NE(bar.Get(), foobar.Get());
Mathieu Chartier9e868092016-10-31 14:58:04 -0700226 ObjPtr<mirror::String> lookup_foo = intern_table.LookupStrong(soa.Self(), 3, "foo");
227 EXPECT_OBJ_PTR_EQ(lookup_foo, foo.Get());
228 ObjPtr<mirror::String> lookup_bar = intern_table.LookupStrong(soa.Self(), 3, "bar");
229 EXPECT_OBJ_PTR_EQ(lookup_bar, bar.Get());
230 ObjPtr<mirror::String> lookup_foobar = intern_table.LookupStrong(soa.Self(), 6, "foobar");
231 EXPECT_OBJ_PTR_EQ(lookup_foobar, foobar.Get());
232 ObjPtr<mirror::String> lookup_foox = intern_table.LookupStrong(soa.Self(), 4, "foox");
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000233 EXPECT_TRUE(lookup_foox == nullptr);
Mathieu Chartier9e868092016-10-31 14:58:04 -0700234 ObjPtr<mirror::String> lookup_fooba = intern_table.LookupStrong(soa.Self(), 5, "fooba");
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000235 EXPECT_TRUE(lookup_fooba == nullptr);
Mathieu Chartier9e868092016-10-31 14:58:04 -0700236 ObjPtr<mirror::String> lookup_foobaR = intern_table.LookupStrong(soa.Self(), 6, "foobaR");
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000237 EXPECT_TRUE(lookup_foobaR == nullptr);
238 // Try a hash conflict.
239 ASSERT_EQ(ComputeUtf16HashFromModifiedUtf8("foobar", 6),
240 ComputeUtf16HashFromModifiedUtf8("foobbS", 6));
Mathieu Chartier9e868092016-10-31 14:58:04 -0700241 ObjPtr<mirror::String> lookup_foobbS = intern_table.LookupStrong(soa.Self(), 6, "foobbS");
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000242 EXPECT_TRUE(lookup_foobbS == nullptr);
243}
244
Vladimir Marko365c0202022-03-22 09:53:31 +0000245TEST_F(InternTableTest, InternStrongFrozenWeak) {
246 ScopedObjectAccess soa(Thread::Current());
247 InternTable intern_table;
248 StackHandleScope<1> hs(soa.Self());
249 Handle<mirror::String> foo(
250 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
251 ASSERT_TRUE(foo != nullptr);
252 ObjPtr<mirror::String> weak_foo = intern_table.InternWeak(foo.Get());
253 ASSERT_TRUE(weak_foo == foo.Get());
254
255 intern_table.AddNewTable();
256
257 ObjPtr<mirror::String> strong_foo = intern_table.InternStrong(foo.Get());
258 ASSERT_TRUE(strong_foo == foo.Get());
259}
260
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700261} // namespace art