blob: 311515c7ed42e326ffe39b2117ed9882f0a59576 [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
17#include "intern_table.h"
18
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"
Andreas Gampec15a2f42017-04-21 12:09:39 -070021#include "gc_root-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/object.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070023#include "handle_scope-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070024#include "mirror/string.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "scoped_thread_state_change-inl.h"
Brian Carlstrom7e93b502011-08-04 14:16:22 -070026
Brian Carlstrom7e93b502011-08-04 14:16:22 -070027namespace art {
28
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080029class InternTableTest : public CommonRuntimeTest {};
Brian Carlstrom7e93b502011-08-04 14:16:22 -070030
31TEST_F(InternTableTest, Intern) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032 ScopedObjectAccess soa(Thread::Current());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070033 InternTable intern_table;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070034 StackHandleScope<4> hs(soa.Self());
35 Handle<mirror::String> foo_1(hs.NewHandle(intern_table.InternStrong(3, "foo")));
36 Handle<mirror::String> foo_2(hs.NewHandle(intern_table.InternStrong(3, "foo")));
37 Handle<mirror::String> foo_3(
38 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
39 Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar")));
Andreas Gampefa4333d2017-02-14 11:10:34 -080040 ASSERT_TRUE(foo_1 != nullptr);
41 ASSERT_TRUE(foo_2 != nullptr);
42 ASSERT_TRUE(foo_3 != nullptr);
43 ASSERT_TRUE(bar != nullptr);
Vladimir Markocac5a7e2016-02-22 10:39:50 +000044 EXPECT_EQ(foo_1.Get(), foo_2.Get());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070045 EXPECT_TRUE(foo_1->Equals("foo"));
46 EXPECT_TRUE(foo_2->Equals("foo"));
47 EXPECT_TRUE(foo_3->Equals("foo"));
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070048 EXPECT_NE(foo_1.Get(), bar.Get());
49 EXPECT_NE(foo_2.Get(), bar.Get());
50 EXPECT_NE(foo_3.Get(), bar.Get());
Brian Carlstrom7e93b502011-08-04 14:16:22 -070051}
52
Elliott Hughescf4c6c42011-09-01 15:16:42 -070053TEST_F(InternTableTest, Size) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070054 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070055 InternTable t;
56 EXPECT_EQ(0U, t.Size());
57 t.InternStrong(3, "foo");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070058 StackHandleScope<1> hs(soa.Self());
59 Handle<mirror::String> foo(
60 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
61 t.InternWeak(foo.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -070062 EXPECT_EQ(1U, t.Size());
63 t.InternStrong(3, "bar");
64 EXPECT_EQ(2U, t.Size());
65}
66
Alexey Grebenkin21f23642016-12-02 17:44:54 +030067// Check if table indexes match on 64 and 32 bit machines.
68// This is done by ensuring hash values are the same on every machine and limited to 32-bit wide.
69// Otherwise cross compilation can cause a table to be filled on host using one indexing algorithm
70// and later on a device with different sizeof(size_t) can use another indexing algorithm.
71// Thus the table may provide wrong data.
72TEST_F(InternTableTest, CrossHash) {
73 ScopedObjectAccess soa(Thread::Current());
74 InternTable t;
75
76 // A string that has a negative hash value.
77 GcRoot<mirror::String> str(mirror::String::AllocFromModifiedUtf8(soa.Self(), "00000000"));
78
79 MutexLock mu(Thread::Current(), *Locks::intern_table_lock_);
80 for (InternTable::Table::UnorderedSet& table : t.strong_interns_.tables_) {
81 // The negative hash value shall be 32-bit wide on every host.
82 ASSERT_TRUE(IsUint<32>(table.hashfn_(str)));
83 }
84}
85
Mathieu Chartier97509952015-07-13 14:35:43 -070086class TestPredicate : public IsMarkedVisitor {
Elliott Hughes410c0c82011-09-01 17:58:25 -070087 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070088 mirror::Object* IsMarked(mirror::Object* s) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070089 bool erased = false;
Mathieu Chartier02e25112013-08-14 16:14:24 -070090 for (auto it = expected_.begin(), end = expected_.end(); it != end; ++it) {
Elliott Hughes410c0c82011-09-01 17:58:25 -070091 if (*it == s) {
92 expected_.erase(it);
93 erased = true;
94 break;
95 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -070096 }
Elliott Hughes410c0c82011-09-01 17:58:25 -070097 EXPECT_TRUE(erased);
Mathieu Chartier97509952015-07-13 14:35:43 -070098 return nullptr;
Elliott Hughescf4c6c42011-09-01 15:16:42 -070099 }
Elliott Hughes410c0c82011-09-01 17:58:25 -0700100
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 void Expect(const mirror::String* s) {
Elliott Hughes410c0c82011-09-01 17:58:25 -0700102 expected_.push_back(s);
103 }
104
105 ~TestPredicate() {
106 EXPECT_EQ(0U, expected_.size());
107 }
108
109 private:
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800110 mutable std::vector<const mirror::String*> expected_;
Elliott Hughes410c0c82011-09-01 17:58:25 -0700111};
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700112
Elliott Hughesc33a32b2011-10-11 18:18:07 -0700113TEST_F(InternTableTest, SweepInternTableWeaks) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700114 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700115 InternTable t;
116 t.InternStrong(3, "foo");
117 t.InternStrong(3, "bar");
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700118 StackHandleScope<5> hs(soa.Self());
119 Handle<mirror::String> hello(
120 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello")));
121 Handle<mirror::String> world(
122 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "world")));
123 Handle<mirror::String> s0(hs.NewHandle(t.InternWeak(hello.Get())));
124 Handle<mirror::String> s1(hs.NewHandle(t.InternWeak(world.Get())));
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700125
126 EXPECT_EQ(4U, t.Size());
127
128 // We should traverse only the weaks...
Elliott Hughes410c0c82011-09-01 17:58:25 -0700129 TestPredicate p;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700130 p.Expect(s0.Get());
131 p.Expect(s1.Get());
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700132 {
Ian Rogers1f539342012-10-03 21:09:42 -0700133 ReaderMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_);
Mathieu Chartier97509952015-07-13 14:35:43 -0700134 t.SweepInternTableWeaks(&p);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700135 }
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700136
137 EXPECT_EQ(2U, t.Size());
138
Elliott Hughese5448b52012-01-18 16:44:06 -0800139 // Just check that we didn't corrupt the map.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700140 Handle<mirror::String> still_here(
141 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "still here")));
142 t.InternWeak(still_here.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700143 EXPECT_EQ(3U, t.Size());
144}
145
146TEST_F(InternTableTest, ContainsWeak) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 ScopedObjectAccess soa(Thread::Current());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700148 {
149 // Strongs are never weak.
150 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700151 StackHandleScope<2> hs(soa.Self());
152 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo")));
153 EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get()));
154 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo")));
155 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get()));
156 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700157 }
158
159 {
160 // Weaks are always weak.
161 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700162 StackHandleScope<4> hs(soa.Self());
163 Handle<mirror::String> foo_1(
164 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
165 Handle<mirror::String> foo_2(
166 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
167 EXPECT_NE(foo_1.Get(), foo_2.Get());
168 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo_1.Get())));
169 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo_2.Get())));
170 EXPECT_TRUE(t.ContainsWeak(interned_foo_2.Get()));
171 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700172 }
173
174 {
175 // A weak can be promoted to a strong.
176 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700177 StackHandleScope<3> hs(soa.Self());
178 Handle<mirror::String> foo(
179 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
180 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo.Get())));
181 EXPECT_TRUE(t.ContainsWeak(interned_foo_1.Get()));
182 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo")));
183 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get()));
184 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700185 }
186
187 {
188 // Interning a weak after a strong gets you the strong.
189 InternTable t;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700190 StackHandleScope<3> hs(soa.Self());
191 Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo")));
192 EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get()));
193 Handle<mirror::String> foo(
194 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo")));
195 Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo.Get())));
196 EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get()));
197 EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get());
Elliott Hughescf4c6c42011-09-01 15:16:42 -0700198 }
199}
200
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000201TEST_F(InternTableTest, LookupStrong) {
202 ScopedObjectAccess soa(Thread::Current());
203 InternTable intern_table;
204 StackHandleScope<3> hs(soa.Self());
205 Handle<mirror::String> foo(hs.NewHandle(intern_table.InternStrong(3, "foo")));
206 Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar")));
207 Handle<mirror::String> foobar(hs.NewHandle(intern_table.InternStrong(6, "foobar")));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800208 ASSERT_TRUE(foo != nullptr);
209 ASSERT_TRUE(bar != nullptr);
210 ASSERT_TRUE(foobar != nullptr);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000211 ASSERT_TRUE(foo->Equals("foo"));
212 ASSERT_TRUE(bar->Equals("bar"));
213 ASSERT_TRUE(foobar->Equals("foobar"));
214 ASSERT_NE(foo.Get(), bar.Get());
215 ASSERT_NE(foo.Get(), foobar.Get());
216 ASSERT_NE(bar.Get(), foobar.Get());
Mathieu Chartier9e868092016-10-31 14:58:04 -0700217 ObjPtr<mirror::String> lookup_foo = intern_table.LookupStrong(soa.Self(), 3, "foo");
218 EXPECT_OBJ_PTR_EQ(lookup_foo, foo.Get());
219 ObjPtr<mirror::String> lookup_bar = intern_table.LookupStrong(soa.Self(), 3, "bar");
220 EXPECT_OBJ_PTR_EQ(lookup_bar, bar.Get());
221 ObjPtr<mirror::String> lookup_foobar = intern_table.LookupStrong(soa.Self(), 6, "foobar");
222 EXPECT_OBJ_PTR_EQ(lookup_foobar, foobar.Get());
223 ObjPtr<mirror::String> lookup_foox = intern_table.LookupStrong(soa.Self(), 4, "foox");
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000224 EXPECT_TRUE(lookup_foox == nullptr);
Mathieu Chartier9e868092016-10-31 14:58:04 -0700225 ObjPtr<mirror::String> lookup_fooba = intern_table.LookupStrong(soa.Self(), 5, "fooba");
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000226 EXPECT_TRUE(lookup_fooba == nullptr);
Mathieu Chartier9e868092016-10-31 14:58:04 -0700227 ObjPtr<mirror::String> lookup_foobaR = intern_table.LookupStrong(soa.Self(), 6, "foobaR");
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000228 EXPECT_TRUE(lookup_foobaR == nullptr);
229 // Try a hash conflict.
230 ASSERT_EQ(ComputeUtf16HashFromModifiedUtf8("foobar", 6),
231 ComputeUtf16HashFromModifiedUtf8("foobbS", 6));
Mathieu Chartier9e868092016-10-31 14:58:04 -0700232 ObjPtr<mirror::String> lookup_foobbS = intern_table.LookupStrong(soa.Self(), 6, "foobbS");
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000233 EXPECT_TRUE(lookup_foobbS == nullptr);
234}
235
Brian Carlstrom7e93b502011-08-04 14:16:22 -0700236} // namespace art