Mon, 06 Aug 2012 15:54:45 -0400
7116786: RFE: Detailed information on VerifyErrors
Summary: Provide additional detail in VerifyError messages
Reviewed-by: sspitsyn, acorn
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*, mtSymbol> {
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;
85 // For statistics
86 static int symbols_removed;
87 static int symbols_counted;
89 Symbol* allocate_symbol(const u1* name, int len, bool c_heap, TRAPS); // Assumes no characters larger than 0x7F
91 // Adding elements
92 Symbol* basic_add(int index, u1* name, int len, unsigned int hashValue,
93 bool c_heap, TRAPS);
95 bool basic_add(Handle class_loader, constantPoolHandle cp, int names_count,
96 const char** names, int* lengths, int* cp_indices,
97 unsigned int* hashValues, TRAPS);
99 static void new_symbols(Handle class_loader, constantPoolHandle cp,
100 int names_count,
101 const char** name, int* lengths,
102 int* cp_indices, unsigned int* hashValues,
103 TRAPS) {
104 add(class_loader, cp, names_count, name, lengths, cp_indices, hashValues, THREAD);
105 }
107 // Table size
108 enum {
109 symbol_table_size = 20011
110 };
112 Symbol* lookup(int index, const char* name, int len, unsigned int hash);
114 SymbolTable()
115 : Hashtable<Symbol*, mtSymbol>(symbol_table_size, sizeof (HashtableEntry<Symbol*, mtSymbol>)) {}
117 SymbolTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
118 : Hashtable<Symbol*, mtSymbol>(symbol_table_size, sizeof (HashtableEntry<Symbol*, mtSymbol>), t,
119 number_of_entries) {}
121 // Arena for permanent symbols (null class loader) that are never unloaded
122 static Arena* _arena;
123 static Arena* arena() { return _arena; } // called for statistics
125 static void initialize_symbols(int arena_alloc_size = 0);
126 public:
127 enum {
128 symbol_alloc_batch_size = 8,
129 // Pick initial size based on java -version size measurements
130 symbol_alloc_arena_size = 360*K
131 };
133 // The symbol table
134 static SymbolTable* the_table() { return _the_table; }
136 static void create_table() {
137 assert(_the_table == NULL, "One symbol table allowed.");
138 _the_table = new SymbolTable();
139 initialize_symbols(symbol_alloc_arena_size);
140 }
142 static void create_table(HashtableBucket<mtSymbol>* t, int length,
143 int number_of_entries) {
144 assert(_the_table == NULL, "One symbol table allowed.");
145 assert(length == symbol_table_size * sizeof(HashtableBucket<mtSymbol>),
146 "bad shared symbol size.");
147 _the_table = new SymbolTable(t, number_of_entries);
148 // if CDS give symbol table a default arena size since most symbols
149 // are already allocated in the shared misc section.
150 initialize_symbols();
151 }
153 static unsigned int hash_symbol(const char* s, int len);
155 static Symbol* lookup(const char* name, int len, TRAPS);
156 // lookup only, won't add. Also calculate hash.
157 static Symbol* lookup_only(const char* name, int len, unsigned int& hash);
158 // Only copy to C string to be added if lookup failed.
159 static Symbol* lookup(const Symbol* sym, int begin, int end, TRAPS);
161 static void release(Symbol* sym);
163 // Look up the address of the literal in the SymbolTable for this Symbol*
164 static Symbol** lookup_symbol_addr(Symbol* sym);
166 // jchar (utf16) version of lookups
167 static Symbol* lookup_unicode(const jchar* name, int len, TRAPS);
168 static Symbol* lookup_only_unicode(const jchar* name, int len, unsigned int& hash);
170 static void add(Handle class_loader, constantPoolHandle cp, int names_count,
171 const char** names, int* lengths, int* cp_indices,
172 unsigned int* hashValues, TRAPS);
174 // Release any dead symbols
175 static void unlink();
177 // iterate over symbols
178 static void symbols_do(SymbolClosure *cl);
180 // Symbol creation
181 static Symbol* new_symbol(const char* utf8_buffer, int length, TRAPS) {
182 assert(utf8_buffer != NULL, "just checking");
183 return lookup(utf8_buffer, length, THREAD);
184 }
185 static Symbol* new_symbol(const char* name, TRAPS) {
186 return new_symbol(name, (int)strlen(name), THREAD);
187 }
188 static Symbol* new_symbol(const Symbol* sym, int begin, int end, TRAPS) {
189 assert(begin <= end && end <= sym->utf8_length(), "just checking");
190 return lookup(sym, begin, end, THREAD);
191 }
193 // Create a symbol in the arena for symbols that are not deleted
194 static Symbol* new_permanent_symbol(const char* name, TRAPS);
196 // Symbol lookup
197 static Symbol* lookup(int index, const char* name, int len, TRAPS);
199 // Needed for preloading classes in signatures when compiling.
200 // Returns the symbol is already present in symbol table, otherwise
201 // NULL. NO ALLOCATION IS GUARANTEED!
202 static Symbol* probe(const char* name, int len) {
203 unsigned int ignore_hash;
204 return lookup_only(name, len, ignore_hash);
205 }
206 static Symbol* probe_unicode(const jchar* name, int len) {
207 unsigned int ignore_hash;
208 return lookup_only_unicode(name, len, ignore_hash);
209 }
211 // Histogram
212 static void print_histogram() PRODUCT_RETURN;
213 static void print() PRODUCT_RETURN;
215 // Debugging
216 static void verify();
217 static void dump(outputStream* st);
219 // Sharing
220 static void copy_buckets(char** top, char*end) {
221 the_table()->Hashtable<Symbol*, mtSymbol>::copy_buckets(top, end);
222 }
223 static void copy_table(char** top, char*end) {
224 the_table()->Hashtable<Symbol*, mtSymbol>::copy_table(top, end);
225 }
226 static void reverse(void* boundary = NULL) {
227 the_table()->Hashtable<Symbol*, mtSymbol>::reverse(boundary);
228 }
230 // Rehash the symbol table if it gets out of balance
231 static void rehash_table();
232 static bool needs_rehashing() { return _needs_rehashing; }
233 };
235 class StringTable : public Hashtable<oop, mtSymbol> {
236 friend class VMStructs;
238 private:
239 // The string table
240 static StringTable* _the_table;
242 // Set if one bucket is out of balance due to hash algorithm deficiency
243 static bool _needs_rehashing;
245 static oop intern(Handle string_or_null, jchar* chars, int length, TRAPS);
246 oop basic_add(int index, Handle string_or_null, jchar* name, int len,
247 unsigned int hashValue, TRAPS);
249 oop lookup(int index, jchar* chars, int length, unsigned int hashValue);
251 StringTable() : Hashtable<oop, mtSymbol>((int)StringTableSize,
252 sizeof (HashtableEntry<oop, mtSymbol>)) {}
254 StringTable(HashtableBucket<mtSymbol>* t, int number_of_entries)
255 : Hashtable<oop, mtSymbol>((int)StringTableSize, sizeof (HashtableEntry<oop, mtSymbol>), t,
256 number_of_entries) {}
257 public:
258 // The string table
259 static StringTable* the_table() { return _the_table; }
261 static void create_table() {
262 assert(_the_table == NULL, "One string table allowed.");
263 _the_table = new StringTable();
264 }
266 static void create_table(HashtableBucket<mtSymbol>* t, int length,
267 int number_of_entries) {
268 assert(_the_table == NULL, "One string table allowed.");
269 assert((size_t)length == StringTableSize * sizeof(HashtableBucket<mtSymbol>),
270 "bad shared string size.");
271 _the_table = new StringTable(t, number_of_entries);
272 }
274 // GC support
275 // Delete pointers to otherwise-unreachable objects.
276 static void unlink(BoolObjectClosure* cl);
278 // Invoke "f->do_oop" on the locations of all oops in the table.
279 static void oops_do(OopClosure* f);
281 // Hashing algorithm, used as the hash value used by the
282 // StringTable for bucket selection and comparison (stored in the
283 // HashtableEntry structures). This is used in the String.intern() method.
284 static unsigned int hash_string(const jchar* s, int len);
286 // Internal test.
287 static void test_alt_hash() PRODUCT_RETURN;
289 // Probing
290 static oop lookup(Symbol* symbol);
292 // Interning
293 static oop intern(Symbol* symbol, TRAPS);
294 static oop intern(oop string, TRAPS);
295 static oop intern(const char *utf8_string, TRAPS);
297 // Debugging
298 static void verify();
299 static void dump(outputStream* st);
301 // Sharing
302 static void copy_buckets(char** top, char*end) {
303 the_table()->Hashtable<oop, mtSymbol>::copy_buckets(top, end);
304 }
305 static void copy_table(char** top, char*end) {
306 the_table()->Hashtable<oop, mtSymbol>::copy_table(top, end);
307 }
308 static void reverse() {
309 the_table()->Hashtable<oop, mtSymbol>::reverse();
310 }
312 // Rehash the symbol table if it gets out of balance
313 static void rehash_table();
314 static bool needs_rehashing() { return _needs_rehashing; }
315 };
316 #endif // SHARE_VM_CLASSFILE_SYMBOLTABLE_HPP