author | xuelei |
Fri, 14 Sep 2018 20:30:28 -0700 | |
changeset 51755 | 6c394ed56b07 |
parent 51406 | f4b4dfac45b1 |
child 54847 | 59ea39bb2809 |
permissions | -rw-r--r-- |
41289 | 1 |
/* |
51179
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
2 |
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
41289 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
#include "precompiled.hpp" |
|
49449
ef5d5d343e2a
8199263: Split interfaceSupport.hpp to not require including .inline.hpp files
coleenp
parents:
47216
diff
changeset
|
25 |
#include "runtime/interfaceSupport.inline.hpp" |
41289 | 26 |
#include "classfile/symbolTable.hpp" |
51179
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
27 |
#include "threadHelper.inline.hpp" |
41289 | 28 |
#include "unittest.hpp" |
29 |
||
41671
9e0c6db4918a
8166925: several native TESTs should be changed to TEST_VM
iignatyev
parents:
41289
diff
changeset
|
30 |
TEST_VM(SymbolTable, temp_new_symbol) { |
41289 | 31 |
// Assert messages assume these symbols are unique, and the refcounts start at |
32 |
// one, but code does not rely on this. |
|
33 |
JavaThread* THREAD = JavaThread::current(); |
|
34 |
// the thread should be in vm to use locks |
|
35 |
ThreadInVMfromNative ThreadInVMfromNative(THREAD); |
|
36 |
||
37 |
Symbol* abc = SymbolTable::new_symbol("abc", CATCH); |
|
38 |
int abccount = abc->refcount(); |
|
39 |
TempNewSymbol ss = abc; |
|
40 |
ASSERT_EQ(ss->refcount(), abccount) << "only one abc"; |
|
41 |
ASSERT_EQ(ss->refcount(), abc->refcount()) << "should match TempNewSymbol"; |
|
42 |
||
43 |
Symbol* efg = SymbolTable::new_symbol("efg", CATCH); |
|
44 |
Symbol* hij = SymbolTable::new_symbol("hij", CATCH); |
|
45 |
int efgcount = efg->refcount(); |
|
46 |
int hijcount = hij->refcount(); |
|
47 |
||
48 |
TempNewSymbol s1 = efg; |
|
49 |
TempNewSymbol s2 = hij; |
|
50 |
ASSERT_EQ(s1->refcount(), efgcount) << "one efg"; |
|
51 |
ASSERT_EQ(s2->refcount(), hijcount) << "one hij"; |
|
52 |
||
53 |
// Assignment operator |
|
54 |
s1 = s2; |
|
55 |
ASSERT_EQ(hij->refcount(), hijcount + 1) << "should be two hij"; |
|
56 |
ASSERT_EQ(efg->refcount(), efgcount - 1) << "should be no efg"; |
|
57 |
||
58 |
s1 = ss; // s1 is abc |
|
59 |
ASSERT_EQ(s1->refcount(), abccount + 1) << "should be two abc (s1 and ss)"; |
|
60 |
ASSERT_EQ(hij->refcount(), hijcount) << "should only have one hij now (s2)"; |
|
61 |
||
62 |
s1 = s1; // self assignment |
|
63 |
ASSERT_EQ(s1->refcount(), abccount + 1) << "should still be two abc (s1 and ss)"; |
|
64 |
||
65 |
TempNewSymbol s3; |
|
66 |
Symbol* klm = SymbolTable::new_symbol("klm", CATCH); |
|
67 |
int klmcount = klm->refcount(); |
|
68 |
s3 = klm; // assignment |
|
69 |
ASSERT_EQ(s3->refcount(), klmcount) << "only one klm now"; |
|
70 |
||
71 |
Symbol* xyz = SymbolTable::new_symbol("xyz", CATCH); |
|
72 |
int xyzcount = xyz->refcount(); |
|
73 |
{ // inner scope |
|
74 |
TempNewSymbol s_inner = xyz; |
|
75 |
} |
|
76 |
ASSERT_EQ(xyz->refcount(), xyzcount - 1) |
|
77 |
<< "Should have been decremented by dtor in inner scope"; |
|
51179
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
78 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
79 |
// Test overflowing refcount making symbol permanent |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
80 |
Symbol* bigsym = SymbolTable::new_symbol("bigsym", CATCH); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
81 |
for (int i = 0; i < PERM_REFCOUNT + 100; i++) { |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
82 |
bigsym->increment_refcount(); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
83 |
} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
84 |
ASSERT_EQ(bigsym->refcount(), PERM_REFCOUNT) << "should not have overflowed"; |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
85 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
86 |
// Test that PERM_REFCOUNT is sticky |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
87 |
for (int i = 0; i < 10; i++) { |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
88 |
bigsym->decrement_refcount(); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
89 |
} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
90 |
ASSERT_EQ(bigsym->refcount(), PERM_REFCOUNT) << "should be sticky"; |
41289 | 91 |
} |
51179
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
92 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
93 |
// TODO: Make two threads one decrementing the refcount and the other trying to increment. |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
94 |
// try_increment_refcount should return false |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
95 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
96 |
#define SYM_NAME_LENGTH 30 |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
97 |
static char symbol_name[SYM_NAME_LENGTH]; |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
98 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
99 |
class SymbolThread : public JavaTestThread { |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
100 |
public: |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
101 |
SymbolThread(Semaphore* post) : JavaTestThread(post) {} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
102 |
virtual ~SymbolThread() {} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
103 |
void main_run() { |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
104 |
Thread* THREAD = Thread::current(); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
105 |
for (int i = 0; i < 1000; i++) { |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
106 |
TempNewSymbol sym = SymbolTable::new_symbol(symbol_name, CATCH); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
107 |
// Create and destroy new symbol |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
108 |
EXPECT_TRUE(sym->refcount() != 0) << "Symbol refcount unexpectedly zeroed"; |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
109 |
} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
110 |
} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
111 |
}; |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
112 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
113 |
#define SYM_TEST_THREAD_COUNT 5 |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
114 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
115 |
class DriverSymbolThread : public JavaTestThread { |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
116 |
public: |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
117 |
Semaphore _done; |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
118 |
DriverSymbolThread(Semaphore* post) : JavaTestThread(post) { }; |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
119 |
virtual ~DriverSymbolThread(){} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
120 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
121 |
void main_run() { |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
122 |
Semaphore done(0); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
123 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
124 |
Thread* THREAD = Thread::current(); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
125 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
126 |
// Find a symbol where there will probably be only one instance. |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
127 |
for (int i = 0; i < 100; i++) { |
51262
d4b9a434af84
8208084: Windows build failure - "'snprintf': identifier not found"
mvala
parents:
51179
diff
changeset
|
128 |
os::snprintf(symbol_name, SYM_NAME_LENGTH, "some_symbol%d", i); |
51179
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
129 |
TempNewSymbol ts = SymbolTable::new_symbol(symbol_name, CATCH); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
130 |
if (ts->refcount() == 1) { |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
131 |
EXPECT_TRUE(ts->refcount() == 1) << "Symbol is just created"; |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
132 |
break; // found a unique symbol |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
133 |
} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
134 |
} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
135 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
136 |
SymbolThread* st[SYM_TEST_THREAD_COUNT]; |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
137 |
for (int i = 0; i < SYM_TEST_THREAD_COUNT; i++) { |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
138 |
st[i] = new SymbolThread(&done); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
139 |
st[i]->doit(); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
140 |
} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
141 |
|
51406
f4b4dfac45b1
8209518: symbol table gtest fails with semaphore error
coleenp
parents:
51262
diff
changeset
|
142 |
for (int i = 0; i < SYM_TEST_THREAD_COUNT; i++) { |
51179
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
143 |
done.wait(); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
144 |
} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
145 |
} |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
146 |
}; |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
147 |
|
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
148 |
TEST_VM(SymbolTable, test_symbol_refcount_parallel) { |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
149 |
mt_test_doer<DriverSymbolThread>(); |
516acf6956a2
8207359: Make SymbolTable increment_refcount disallow zero
coleenp
parents:
49449
diff
changeset
|
150 |
} |