Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 16 | |
| 17 | #include "intern_table.h" |
| 18 | |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 19 | #include "base/hash_set.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 20 | #include "common_runtime_test.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "mirror/object.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 22 | #include "handle_scope-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include "mirror/string.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 24 | #include "scoped_thread_state_change-inl.h" |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 25 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 26 | namespace art { |
| 27 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 28 | class InternTableTest : public CommonRuntimeTest {}; |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 29 | |
| 30 | TEST_F(InternTableTest, Intern) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 31 | ScopedObjectAccess soa(Thread::Current()); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 32 | InternTable intern_table; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 33 | StackHandleScope<4> hs(soa.Self()); |
| 34 | Handle<mirror::String> foo_1(hs.NewHandle(intern_table.InternStrong(3, "foo"))); |
| 35 | Handle<mirror::String> foo_2(hs.NewHandle(intern_table.InternStrong(3, "foo"))); |
| 36 | Handle<mirror::String> foo_3( |
| 37 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 38 | Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 39 | ASSERT_TRUE(foo_1 != nullptr); |
| 40 | ASSERT_TRUE(foo_2 != nullptr); |
| 41 | ASSERT_TRUE(foo_3 != nullptr); |
| 42 | ASSERT_TRUE(bar != nullptr); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 43 | EXPECT_EQ(foo_1.Get(), foo_2.Get()); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 44 | EXPECT_TRUE(foo_1->Equals("foo")); |
| 45 | EXPECT_TRUE(foo_2->Equals("foo")); |
| 46 | EXPECT_TRUE(foo_3->Equals("foo")); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 47 | EXPECT_NE(foo_1.Get(), bar.Get()); |
| 48 | EXPECT_NE(foo_2.Get(), bar.Get()); |
| 49 | EXPECT_NE(foo_3.Get(), bar.Get()); |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 52 | TEST_F(InternTableTest, Size) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 53 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 54 | InternTable t; |
| 55 | EXPECT_EQ(0U, t.Size()); |
| 56 | t.InternStrong(3, "foo"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 57 | StackHandleScope<1> hs(soa.Self()); |
| 58 | Handle<mirror::String> foo( |
| 59 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 60 | t.InternWeak(foo.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 61 | EXPECT_EQ(1U, t.Size()); |
| 62 | t.InternStrong(3, "bar"); |
| 63 | EXPECT_EQ(2U, t.Size()); |
| 64 | } |
| 65 | |
Alexey Grebenkin | 21f2364 | 2016-12-02 17:44:54 +0300 | [diff] [blame] | 66 | // Check if table indexes match on 64 and 32 bit machines. |
| 67 | // This is done by ensuring hash values are the same on every machine and limited to 32-bit wide. |
| 68 | // Otherwise cross compilation can cause a table to be filled on host using one indexing algorithm |
| 69 | // and later on a device with different sizeof(size_t) can use another indexing algorithm. |
| 70 | // Thus the table may provide wrong data. |
| 71 | TEST_F(InternTableTest, CrossHash) { |
| 72 | ScopedObjectAccess soa(Thread::Current()); |
| 73 | InternTable t; |
| 74 | |
| 75 | // A string that has a negative hash value. |
| 76 | GcRoot<mirror::String> str(mirror::String::AllocFromModifiedUtf8(soa.Self(), "00000000")); |
| 77 | |
| 78 | MutexLock mu(Thread::Current(), *Locks::intern_table_lock_); |
| 79 | for (InternTable::Table::UnorderedSet& table : t.strong_interns_.tables_) { |
| 80 | // The negative hash value shall be 32-bit wide on every host. |
| 81 | ASSERT_TRUE(IsUint<32>(table.hashfn_(str))); |
| 82 | } |
| 83 | } |
| 84 | |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 85 | class TestPredicate : public IsMarkedVisitor { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 86 | public: |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 87 | mirror::Object* IsMarked(mirror::Object* s) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 88 | bool erased = false; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 89 | for (auto it = expected_.begin(), end = expected_.end(); it != end; ++it) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 90 | if (*it == s) { |
| 91 | expected_.erase(it); |
| 92 | erased = true; |
| 93 | break; |
| 94 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 95 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 96 | EXPECT_TRUE(erased); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 97 | return nullptr; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 98 | } |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 99 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 100 | void Expect(const mirror::String* s) { |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 101 | expected_.push_back(s); |
| 102 | } |
| 103 | |
| 104 | ~TestPredicate() { |
| 105 | EXPECT_EQ(0U, expected_.size()); |
| 106 | } |
| 107 | |
| 108 | private: |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 109 | mutable std::vector<const mirror::String*> expected_; |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 110 | }; |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 111 | |
Elliott Hughes | c33a32b | 2011-10-11 18:18:07 -0700 | [diff] [blame] | 112 | TEST_F(InternTableTest, SweepInternTableWeaks) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 113 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 114 | InternTable t; |
| 115 | t.InternStrong(3, "foo"); |
| 116 | t.InternStrong(3, "bar"); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 117 | StackHandleScope<5> hs(soa.Self()); |
| 118 | Handle<mirror::String> hello( |
| 119 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "hello"))); |
| 120 | Handle<mirror::String> world( |
| 121 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "world"))); |
| 122 | Handle<mirror::String> s0(hs.NewHandle(t.InternWeak(hello.Get()))); |
| 123 | Handle<mirror::String> s1(hs.NewHandle(t.InternWeak(world.Get()))); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 124 | |
| 125 | EXPECT_EQ(4U, t.Size()); |
| 126 | |
| 127 | // We should traverse only the weaks... |
Elliott Hughes | 410c0c8 | 2011-09-01 17:58:25 -0700 | [diff] [blame] | 128 | TestPredicate p; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 129 | p.Expect(s0.Get()); |
| 130 | p.Expect(s1.Get()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 131 | { |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 132 | ReaderMutexLock mu(soa.Self(), *Locks::heap_bitmap_lock_); |
Mathieu Chartier | 9750995 | 2015-07-13 14:35:43 -0700 | [diff] [blame] | 133 | t.SweepInternTableWeaks(&p); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 134 | } |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 135 | |
| 136 | EXPECT_EQ(2U, t.Size()); |
| 137 | |
Elliott Hughes | e5448b5 | 2012-01-18 16:44:06 -0800 | [diff] [blame] | 138 | // Just check that we didn't corrupt the map. |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 139 | Handle<mirror::String> still_here( |
| 140 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "still here"))); |
| 141 | t.InternWeak(still_here.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 142 | EXPECT_EQ(3U, t.Size()); |
| 143 | } |
| 144 | |
| 145 | TEST_F(InternTableTest, ContainsWeak) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 146 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 147 | { |
| 148 | // Strongs are never weak. |
| 149 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 150 | StackHandleScope<2> hs(soa.Self()); |
| 151 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 152 | EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get())); |
| 153 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 154 | EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get())); |
| 155 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | { |
| 159 | // Weaks are always weak. |
| 160 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 161 | StackHandleScope<4> hs(soa.Self()); |
| 162 | Handle<mirror::String> foo_1( |
| 163 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 164 | Handle<mirror::String> foo_2( |
| 165 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 166 | EXPECT_NE(foo_1.Get(), foo_2.Get()); |
| 167 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo_1.Get()))); |
| 168 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo_2.Get()))); |
| 169 | EXPECT_TRUE(t.ContainsWeak(interned_foo_2.Get())); |
| 170 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | { |
| 174 | // A weak can be promoted to a strong. |
| 175 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 176 | StackHandleScope<3> hs(soa.Self()); |
| 177 | Handle<mirror::String> foo( |
| 178 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 179 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternWeak(foo.Get()))); |
| 180 | EXPECT_TRUE(t.ContainsWeak(interned_foo_1.Get())); |
| 181 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 182 | EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get())); |
| 183 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | { |
| 187 | // Interning a weak after a strong gets you the strong. |
| 188 | InternTable t; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 189 | StackHandleScope<3> hs(soa.Self()); |
| 190 | Handle<mirror::String> interned_foo_1(hs.NewHandle(t.InternStrong(3, "foo"))); |
| 191 | EXPECT_FALSE(t.ContainsWeak(interned_foo_1.Get())); |
| 192 | Handle<mirror::String> foo( |
| 193 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "foo"))); |
| 194 | Handle<mirror::String> interned_foo_2(hs.NewHandle(t.InternWeak(foo.Get()))); |
| 195 | EXPECT_FALSE(t.ContainsWeak(interned_foo_2.Get())); |
| 196 | EXPECT_EQ(interned_foo_1.Get(), interned_foo_2.Get()); |
Elliott Hughes | cf4c6c4 | 2011-09-01 15:16:42 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 200 | TEST_F(InternTableTest, LookupStrong) { |
| 201 | ScopedObjectAccess soa(Thread::Current()); |
| 202 | InternTable intern_table; |
| 203 | StackHandleScope<3> hs(soa.Self()); |
| 204 | Handle<mirror::String> foo(hs.NewHandle(intern_table.InternStrong(3, "foo"))); |
| 205 | Handle<mirror::String> bar(hs.NewHandle(intern_table.InternStrong(3, "bar"))); |
| 206 | Handle<mirror::String> foobar(hs.NewHandle(intern_table.InternStrong(6, "foobar"))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 207 | ASSERT_TRUE(foo != nullptr); |
| 208 | ASSERT_TRUE(bar != nullptr); |
| 209 | ASSERT_TRUE(foobar != nullptr); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 210 | ASSERT_TRUE(foo->Equals("foo")); |
| 211 | ASSERT_TRUE(bar->Equals("bar")); |
| 212 | ASSERT_TRUE(foobar->Equals("foobar")); |
| 213 | ASSERT_NE(foo.Get(), bar.Get()); |
| 214 | ASSERT_NE(foo.Get(), foobar.Get()); |
| 215 | ASSERT_NE(bar.Get(), foobar.Get()); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 216 | ObjPtr<mirror::String> lookup_foo = intern_table.LookupStrong(soa.Self(), 3, "foo"); |
| 217 | EXPECT_OBJ_PTR_EQ(lookup_foo, foo.Get()); |
| 218 | ObjPtr<mirror::String> lookup_bar = intern_table.LookupStrong(soa.Self(), 3, "bar"); |
| 219 | EXPECT_OBJ_PTR_EQ(lookup_bar, bar.Get()); |
| 220 | ObjPtr<mirror::String> lookup_foobar = intern_table.LookupStrong(soa.Self(), 6, "foobar"); |
| 221 | EXPECT_OBJ_PTR_EQ(lookup_foobar, foobar.Get()); |
| 222 | ObjPtr<mirror::String> lookup_foox = intern_table.LookupStrong(soa.Self(), 4, "foox"); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 223 | EXPECT_TRUE(lookup_foox == nullptr); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 224 | ObjPtr<mirror::String> lookup_fooba = intern_table.LookupStrong(soa.Self(), 5, "fooba"); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 225 | EXPECT_TRUE(lookup_fooba == nullptr); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 226 | ObjPtr<mirror::String> lookup_foobaR = intern_table.LookupStrong(soa.Self(), 6, "foobaR"); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 227 | EXPECT_TRUE(lookup_foobaR == nullptr); |
| 228 | // Try a hash conflict. |
| 229 | ASSERT_EQ(ComputeUtf16HashFromModifiedUtf8("foobar", 6), |
| 230 | ComputeUtf16HashFromModifiedUtf8("foobbS", 6)); |
Mathieu Chartier | 9e86809 | 2016-10-31 14:58:04 -0700 | [diff] [blame] | 231 | ObjPtr<mirror::String> lookup_foobbS = intern_table.LookupStrong(soa.Self(), 6, "foobbS"); |
Vladimir Marko | cac5a7e | 2016-02-22 10:39:50 +0000 | [diff] [blame] | 232 | EXPECT_TRUE(lookup_foobbS == nullptr); |
| 233 | } |
| 234 | |
Brian Carlstrom | 7e93b50 | 2011-08-04 14:16:22 -0700 | [diff] [blame] | 235 | } // namespace art |