Mon, 25 Jun 2012 21:33:35 -0400
7178670: runtime/7158800/BadUtf8.java fails in SymbolTable::rehash_table
Summary: Cannot delete _buckets and HashtableEntries in shared space (CDS)
Reviewed-by: acorn, kvn, dlong, dcubed, kamg
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
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 */
25 #ifndef SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
26 #define SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP
28 #include "memory/allocation.inline.hpp"
29 #include "oops/symbol.hpp"
30 #include "utilities/hashtable.hpp"
32 // The symbol table holds all Symbol*s and corresponding interned strings.
33 // Symbol*s and literal strings should be canonicalized.
34 //
35 // The interned strings are created lazily.
36 //
37 // It is implemented as an open hash table with a fixed number of buckets.
38 //
39 // %note:
40 // - symbolTableEntrys are allocated in blocks to reduce the space overhead.
42 class BoolObjectClosure;
43 class outputStream;
46 // Class to hold a newly created or referenced Symbol* temporarily in scope.
47 // new_symbol() and lookup() will create a Symbol* if not already in the
48 // symbol table and add to the symbol's reference count.
49 // probe() and lookup_only() will increment the refcount if symbol is found.
50 class TempNewSymbol : public StackObj {
51 Symbol* _temp;
53 public:
54 TempNewSymbol() : _temp(NULL) {}
55 // Creating or looking up a symbol increments the symbol's reference count
56 TempNewSymbol(Symbol *s) : _temp(s) {}
58 // Operator= increments reference count.
59 void operator=(const TempNewSymbol &s) {
60 _temp = s._temp;
61 if (_temp !=NULL) _temp->increment_refcount();
62 }
64 // Decrement reference counter so it can go away if it's unique
65 ~TempNewSymbol() { if (_temp != NULL) _temp->decrement_refcount(); }
67 // Operators so they can be used like Symbols
68 Symbol* operator -> () const { return _temp; }
69 bool operator == (Symbol* o) const { return _temp == o; }
70 // Sneaky conversion function
71 operator Symbol*() { return _temp; }
72 };
74 class SymbolTable : public Hashtable<Symbol*> {
75 friend class VMStructs;
76 friend class ClassFileParser;
78 private:
79 // The symbol table
80 static SymbolTable* _the_table;
82 // Set if one bucket is out of balance due to hash algorithm deficiency
83 static bool _needs_rehashing;
84 static jint _seed;
86 // For statistics
87 static int symbols_removed;
88 static int symbols_counted;
90 Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F
92 // Adding elements
93 Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue,
94 bool c_heap, TRAPS);
96 bool basic_add(Handle class_loader, constantPoolHandle cp, int names_count,
97 const char** names, int* lengths, int* cp_indices,
98 unsigned int* hashValues, TRAPS);
100 static void new_symbols(Handle class_loader, constantPoolHandle cp,
101 int names_count,
102 const char** name, int* lengths,
103 int* cp_indices, unsigned int* hashValues,
104 TRAPS) {
105 add(class_loader, cp, names_count, name, lengths, cp_indices, hashValues, THREAD);
106 }
108 // Table size
109 enum {
110 symbol_table_size = 20011
111 };
113 Symbol* lookup(int index, const char* name, int len, unsigned int hash);
115 SymbolTable()
116 : Hashtable<Symbol*>(symbol_table_size, sizeof (HashtableEntry<Symbol*>)) {}
118 SymbolTable(HashtableBucket* t, int number_of_entries)
119 : Hashtable<Symbol*>(symbol_table_size, sizeof (HashtableEntry<Symbol*>), t,
120 number_of_entries) {}
122 // Arena for permanent symbols (null class loader) that are never unloaded
123 static Arena* _arena;
124 static Arena* arena() { return _arena; } // called for statistics
126 static void initialize_symbols(int arena_alloc_size = 0);
128 static bool use_alternate_hashcode() { return _seed != 0; }
129 static jint seed() { return _seed; }
131 unsigned int new_hash(Symbol* sym);
132 public:
133 enum {
134 symbol_alloc_batch_size = 8,
135 // Pick initial size based on java -version size measurements
136 symbol_alloc_arena_size = 360*K
137 };
139 // The symbol table
140 static SymbolTable* the_table() { return _the_table; }
142 static void create_table() {
143 assert(_the_table == NULL, "One symbol table allowed.");
144 _the_table = new SymbolTable();
145 initialize_symbols(symbol_alloc_arena_size);
146 }
148 static void create_table(HashtableBucket* t, int length,
149 int number_of_entries) {
150 assert(_the_table == NULL, "One symbol table allowed.");
151 assert(length == symbol_table_size * sizeof(HashtableBucket),
152 "bad shared symbol size.");
153 _the_table = new SymbolTable(t, number_of_entries);
154 // if CDS give symbol table a default arena size since most symbols
155 // are already allocated in the shared misc section.
156 initialize_symbols();
157 }
159 static unsigned int hash_symbol(const char* s, int len);
161 static Symbol* lookup(const char* name, int len, TRAPS);
162 // lookup only, won't add. Also calculate hash.
163 static Symbol* lookup_only(const char* name, int len, unsigned int& hash);
164 // Only copy to C string to be added if lookup failed.
165 static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS);
167 static void release(Symbol* sym);
169 // Look up the address of the literal in the SymbolTable for this Symbol*
170 static Symbol** lookup_symbol_addr(Symbol* sym);
172 // jchar (utf16) version of lookups
173 static Symbol* lookup_unicode(const jchar* name, int len, TRAPS);
174 static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash);
176 static void add(Handle class_loader, constantPoolHandle cp, int names_count,
177 const char** names, int* lengths, int* cp_indices,
178 unsigned int* hashValues, TRAPS);
180 // Release any dead symbols
181 static void unlink();
183 // iterate over symbols
184 static void symbols_do(SymbolClosure *cl);
186 // Symbol creation
187 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) {
188 assert(utf8_buffer != NULL, "just checking");
189 return lookup(utf8_buffer, length, THREAD);
190 }
191 static Symbol* new_symbol(const char* name, TRAPS) {
192 return new_symbol(name, (int)strlen(name), THREAD);
193 }
194 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) {
195 assert(begin <= end && end <= sym->utf8_length(), "just checking");
196 return lookup(sym, begin, end, THREAD);
197 }
199 // Create a symbol in the arena for symbols that are not deleted
200 static Symbol* new_permanent_symbol(const char* name, TRAPS);
202 // Symbol lookup
203 static Symbol* lookup(int index, const char* name, int len, TRAPS);
205 // Needed for preloading classes in signatures when compiling.
206 // Returns the symbol is already present in symbol table, otherwise
207 // NULL. NO ALLOCATION IS GUARANTEED!
208 static Symbol* probe(const char* name, int len) {
209 unsigned int ignore_hash;
210 return lookup_only(name, len, ignore_hash);
211 }
212 static Symbol* probe_unicode(const jchar* name, int len) {
213 unsigned int ignore_hash;
214 return lookup_only_unicode(name, len, ignore_hash);
215 }
217 // Histogram
218 static void print_histogram() PRODUCT_RETURN;
219 static void print() PRODUCT_RETURN;
221 // Debugging
222 static void verify();
223 static void dump(outputStream* st);
225 // Sharing
226 static void copy_buckets(char** top, char*end) {
227 the_table()->Hashtable<Symbol*>::copy_buckets(top, end);
228 }
229 static void copy_table(char** top, char*end) {
230 the_table()->Hashtable<Symbol*>::copy_table(top, end);
231 }
232 static void reverse(void* boundary = NULL) {
233 the_table()->Hashtable<Symbol*>::reverse(boundary);
234 }
236 // Rehash the symbol table if it gets out of balance
237 static void rehash_table();
238 static bool needs_rehashing() { return _needs_rehashing; }
239 };
242 class StringTable : public Hashtable<oop> {
243 friend class VMStructs;
245 private:
246 // The string table
247 static StringTable* _the_table;
249 // Set if one bucket is out of balance due to hash algorithm deficiency
250 static bool _needs_rehashing;
251 static jint _seed;
253 static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS);
254 oop basic_add(int index, Handle string_or_null, jchar* name, int len,
255 unsigned int hashValue, TRAPS);
257 oop lookup(int index, jchar* chars, int length, unsigned int hashValue);
259 StringTable() : Hashtable<oop>((int)StringTableSize,
260 sizeof (HashtableEntry<oop>)) {}
262 StringTable(HashtableBucket* t, int number_of_entries)
263 : Hashtable<oop>((int)StringTableSize, sizeof (HashtableEntry<oop>), t,
264 number_of_entries) {}
266 static bool use_alternate_hashcode() { return _seed != 0; }
267 static jint seed() { return _seed; }
269 unsigned int new_hash(oop s);
270 public:
271 // The string table
272 static StringTable* the_table() { return _the_table; }
274 static void create_table() {
275 assert(_the_table == NULL, "One string table allowed.");
276 _the_table = new StringTable();
277 }
279 static void create_table(HashtableBucket* t, int length,
280 int number_of_entries) {
281 assert(_the_table == NULL, "One string table allowed.");
282 assert((size_t)length == StringTableSize * sizeof(HashtableBucket),
283 "bad shared string size.");
284 _the_table = new StringTable(t, number_of_entries);
285 }
287 // GC support
288 // Delete pointers to otherwise-unreachable objects.
289 static void unlink(BoolObjectClosure* cl);
291 // Invoke "f->do_oop" on the locations of all oops in the table.
292 static void oops_do(OopClosure* f);
294 // Hashing algorithm, used as the hash value used by the
295 // StringTable for bucket selection and comparison (stored in the
296 // HashtableEntry structures). This is used in the String.intern() method.
297 static unsigned int hash_string(const jchar* s, int len);
299 // Internal test.
300 static void test_alt_hash() PRODUCT_RETURN;
302 // Probing
303 static oop lookup(Symbol* symbol);
305 // Interning
306 static oop intern(Symbol* symbol, TRAPS);
307 static oop intern(oop string, TRAPS);
308 static oop intern(const char *utf8_string, TRAPS);
310 // Debugging
311 static void verify();
312 static void dump(outputStream* st);
314 // Sharing
315 static void copy_buckets(char** top, char*end) {
316 the_table()->Hashtable<oop>::copy_buckets(top, end);
317 }
318 static void copy_table(char** top, char*end) {
319 the_table()->Hashtable<oop>::copy_table(top, end);
320 }
321 static void reverse() {
322 the_table()->Hashtable<oop>::reverse();
323 }
325 // Rehash the symbol table if it gets out of balance
326 static void rehash_table();
327 static bool needs_rehashing() { return _needs_rehashing; }
328 };
329 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP