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
25 #include "precompiled.hpp"
26 #include "classfile/symbolTable.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "memory/oopFactory.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "memory/universe.hpp"
31 #include "oops/instanceKlass.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "oops/symbol.hpp"
34 #include "oops/typeArrayKlass.hpp"
35 #include "runtime/signature.hpp"
36
37 // Implementation of SignatureIterator
38
39 // Signature syntax:
40 //
41 // Signature = "(" {Parameter} ")" ReturnType.
42 // Parameter = FieldType.
43 // ReturnType = FieldType | "V".
44 // FieldType = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "[" FieldType.
45 // ClassName = string.
46
47
48 SignatureIterator::SignatureIterator(Symbol* signature) {
49 _signature = signature;
50 _parameter_index = 0;
51 }
52
53 void SignatureIterator::expect(char c) {
54 if (_signature->char_at(_index) != c) fatal("expecting %c", c);
55 _index++;
56 }
57
58 int SignatureIterator::parse_type() {
59 // Note: This function could be simplified by using "return T_XXX_size;"
60 // instead of the assignment and the break statements. However, it
61 // seems that the product build for win32_i486 with MS VC++ 6.0 doesn't
62 // work (stack underflow for some tests) - this seems to be a VC++ 6.0
63 // compiler bug (was problem - gri 4/27/2000).
64 int size = -1;
65 switch(_signature->char_at(_index)) {
66 case JVM_SIGNATURE_BYTE: do_byte(); if (_parameter_index < 0 ) _return_type = T_BYTE;
67 _index++; size = T_BYTE_size; break;
68 case JVM_SIGNATURE_CHAR: do_char(); if (_parameter_index < 0 ) _return_type = T_CHAR;
69 _index++; size = T_CHAR_size; break;
70 case JVM_SIGNATURE_DOUBLE: do_double(); if (_parameter_index < 0 ) _return_type = T_DOUBLE;
71 _index++; size = T_DOUBLE_size; break;
72 case JVM_SIGNATURE_FLOAT: do_float(); if (_parameter_index < 0 ) _return_type = T_FLOAT;
73 _index++; size = T_FLOAT_size; break;
74 case JVM_SIGNATURE_INT: do_int(); if (_parameter_index < 0 ) _return_type = T_INT;
75 _index++; size = T_INT_size; break;
76 case JVM_SIGNATURE_LONG: do_long(); if (_parameter_index < 0 ) _return_type = T_LONG;
77 _index++; size = T_LONG_size; break;
78 case JVM_SIGNATURE_SHORT: do_short(); if (_parameter_index < 0 ) _return_type = T_SHORT;
79 _index++; size = T_SHORT_size; break;
80 case JVM_SIGNATURE_BOOLEAN: do_bool(); if (_parameter_index < 0 ) _return_type = T_BOOLEAN;
81 _index++; size = T_BOOLEAN_size; break;
82 case JVM_SIGNATURE_VOID: do_void(); if (_parameter_index < 0 ) _return_type = T_VOID;
83 _index++; size = T_VOID_size; break;
84 case JVM_SIGNATURE_CLASS:
85 { int begin = ++_index;
86 Symbol* sig = _signature;
87 while (sig->char_at(_index++) != JVM_SIGNATURE_ENDCLASS) ;
88 do_object(begin, _index);
89 }
90 if (_parameter_index < 0 ) _return_type = T_OBJECT;
91 size = T_OBJECT_size;
92 break;
93 case JVM_SIGNATURE_ARRAY:
94 { int begin = ++_index;
95 Symbol* sig = _signature;
96 while (sig->char_at(_index) == JVM_SIGNATURE_ARRAY) {
97 _index++;
98 }
99 if (sig->char_at(_index) == JVM_SIGNATURE_CLASS) {
100 while (sig->char_at(_index++) != JVM_SIGNATURE_ENDCLASS) ;
101 } else {
102 _index++;
103 }
104 do_array(begin, _index);
105 if (_parameter_index < 0 ) _return_type = T_ARRAY;
106 }
107 size = T_ARRAY_size;
108 break;
109 default:
110 ShouldNotReachHere();
111 break;
112 }
113 assert(size >= 0, "size must be set");
114 return size;
115 }
116
117
118 void SignatureIterator::check_signature_end() {
119 if (_index < _signature->utf8_length()) {
120 tty->print_cr("too many chars in signature");
121 _signature->print_value_on(tty);
122 tty->print_cr(" @ %d", _index);
123 }
124 }
125
126
127 void SignatureIterator::iterate_parameters() {
128 // Parse parameters
129 _index = 0;
130 _parameter_index = 0;
131 expect(JVM_SIGNATURE_FUNC);
132 while (_signature->char_at(_index) != JVM_SIGNATURE_ENDFUNC) _parameter_index += parse_type();
133 expect(JVM_SIGNATURE_ENDFUNC);
134 _parameter_index = 0;
135 }
136
137 // Optimized version of iterate_parameters when fingerprint is known
138 void SignatureIterator::iterate_parameters( uint64_t fingerprint ) {
139 uint64_t saved_fingerprint = fingerprint;
140
141 // Check for too many arguments
142 if (fingerprint == (uint64_t)CONST64(-1)) {
143 SignatureIterator::iterate_parameters();
144 return;
145 }
146
147 assert(fingerprint, "Fingerprint should not be 0");
148
149 _parameter_index = 0;
150 fingerprint = fingerprint >> (static_feature_size + result_feature_size);
151 while ( 1 ) {
152 switch ( fingerprint & parameter_feature_mask ) {
153 case bool_parm:
154 do_bool();
155 _parameter_index += T_BOOLEAN_size;
156 break;
157 case byte_parm:
158 do_byte();
159 _parameter_index += T_BYTE_size;
160 break;
161 case char_parm:
162 do_char();
163 _parameter_index += T_CHAR_size;
164 break;
165 case short_parm:
166 do_short();
167 _parameter_index += T_SHORT_size;
168 break;
169 case int_parm:
170 do_int();
171 _parameter_index += T_INT_size;
172 break;
173 case obj_parm:
174 do_object(0, 0);
175 _parameter_index += T_OBJECT_size;
176 break;
177 case long_parm:
178 do_long();
179 _parameter_index += T_LONG_size;
180 break;
181 case float_parm:
182 do_float();
183 _parameter_index += T_FLOAT_size;
184 break;
185 case double_parm:
186 do_double();
187 _parameter_index += T_DOUBLE_size;
188 break;
189 case done_parm:
190 return;
191 default:
192 tty->print_cr("*** parameter is " UINT64_FORMAT, fingerprint & parameter_feature_mask);
193 tty->print_cr("*** fingerprint is " PTR64_FORMAT, saved_fingerprint);
194 ShouldNotReachHere();
195 break;
196 }
197 fingerprint >>= parameter_feature_size;
198 }
199 }
200
201
202 void SignatureIterator::iterate_returntype() {
203 // Ignore parameters
204 _index = 0;
205 expect(JVM_SIGNATURE_FUNC);
206 Symbol* sig = _signature;
207 // Need to skip over each type in the signature's argument list until a
208 // closing ')' is found., then get the return type. We cannot just scan
209 // for the first ')' because ')' is a legal character in a type name.
210 while (sig->char_at(_index) != JVM_SIGNATURE_ENDFUNC) {
211 switch(sig->char_at(_index)) {
212 case JVM_SIGNATURE_BYTE:
213 case JVM_SIGNATURE_CHAR:
214 case JVM_SIGNATURE_DOUBLE:
215 case JVM_SIGNATURE_FLOAT:
216 case JVM_SIGNATURE_INT:
217 case JVM_SIGNATURE_LONG:
218 case JVM_SIGNATURE_SHORT:
219 case JVM_SIGNATURE_BOOLEAN:
220 case JVM_SIGNATURE_VOID:
221 {
222 _index++;
223 }
224 break;
225 case JVM_SIGNATURE_CLASS:
226 {
227 while (sig->char_at(_index++) != JVM_SIGNATURE_ENDCLASS) ;
228 }
229 break;
230 case JVM_SIGNATURE_ARRAY:
231 {
232 while (sig->char_at(++_index) == JVM_SIGNATURE_ARRAY) ;
233 if (sig->char_at(_index) == JVM_SIGNATURE_CLASS) {
234 while (sig->char_at(_index++) != JVM_SIGNATURE_ENDCLASS) ;
235 } else {
236 _index++;
237 }
238 }
239 break;
240 default:
241 ShouldNotReachHere();
242 break;
243 }
244 }
245 expect(JVM_SIGNATURE_ENDFUNC);
246 // Parse return type
247 _parameter_index = -1;
248 parse_type();
249 check_signature_end();
250 _parameter_index = 0;
251 }
252
253
254 void SignatureIterator::iterate() {
255 // Parse parameters
256 _parameter_index = 0;
257 _index = 0;
258 expect(JVM_SIGNATURE_FUNC);
259 while (_signature->char_at(_index) != JVM_SIGNATURE_ENDFUNC) _parameter_index += parse_type();
260 expect(JVM_SIGNATURE_ENDFUNC);
261 // Parse return type
262 _parameter_index = -1;
263 parse_type();
264 check_signature_end();
265 _parameter_index = 0;
266 }
267
268
269 // Implementation of SignatureStream
270 SignatureStream::SignatureStream(Symbol* signature, bool is_method) :
271 _signature(signature), _at_return_type(false), _previous_name(NULL), _names(NULL) {
272 _begin = _end = (is_method ? 1 : 0); // skip first '(' in method signatures
273 next();
274 }
275
276 SignatureStream::~SignatureStream() {
277 // decrement refcount for names created during signature parsing
278 if (_names != NULL) {
279 for (int i = 0; i < _names->length(); i++) {
280 _names->at(i)->decrement_refcount();
281 }
282 }
283 }
284
285 bool SignatureStream::is_done() const {
286 return _end > _signature->utf8_length();
287 }
288
289
290 void SignatureStream::next_non_primitive(int t) {
291 switch (t) {
292 case JVM_SIGNATURE_CLASS: {
293 _type = T_OBJECT;
294 Symbol* sig = _signature;
295 while (sig->char_at(_end++) != JVM_SIGNATURE_ENDCLASS);
296 break;
297 }
298 case JVM_SIGNATURE_ARRAY: {
299 _type = T_ARRAY;
300 Symbol* sig = _signature;
301 char c = sig->char_at(_end);
302 while ('0' <= c && c <= '9') c = sig->char_at(_end++);
303 while (sig->char_at(_end) == JVM_SIGNATURE_ARRAY) {
304 _end++;
305 c = sig->char_at(_end);
306 while ('0' <= c && c <= '9') c = sig->char_at(_end++);
307 }
308 switch(sig->char_at(_end)) {
309 case JVM_SIGNATURE_BYTE:
310 case JVM_SIGNATURE_CHAR:
311 case JVM_SIGNATURE_DOUBLE:
312 case JVM_SIGNATURE_FLOAT:
313 case JVM_SIGNATURE_INT:
314 case JVM_SIGNATURE_LONG:
315 case JVM_SIGNATURE_SHORT:
316 case JVM_SIGNATURE_BOOLEAN:_end++; break;
317 default: {
318 while (sig->char_at(_end++) != JVM_SIGNATURE_ENDCLASS);
319 break;
320 }
321 }
322 break;
323 }
324 case JVM_SIGNATURE_ENDFUNC: _end++; next(); _at_return_type = true; break;
325 default : ShouldNotReachHere();
326 }
327 }
328
329
330 bool SignatureStream::is_object() const {
331 return _type == T_OBJECT
332 || _type == T_ARRAY;
333 }
334
335 bool SignatureStream::is_array() const {
336 return _type == T_ARRAY;
337 }
338
339 Symbol* SignatureStream::as_symbol() {
340 // Create a symbol from for string _begin _end
341 int begin = _begin;
342 int end = _end;
343
344 if ( _signature->char_at(_begin) == JVM_SIGNATURE_CLASS
345 && _signature->char_at(_end-1) == JVM_SIGNATURE_ENDCLASS) {
346 begin++;
347 end--;
348 }
349
350 const char* symbol_chars = (const char*)_signature->base() + begin;
351 int len = end - begin;
352
353 // Quick check for common symbols in signatures
354 assert((vmSymbols::java_lang_String()->utf8_length() == 16 && vmSymbols::java_lang_Object()->utf8_length() == 16), "sanity");
355 if (len == 16 &&
356 strncmp(symbol_chars, "java/lang/", 10) == 0) {
357 if (strncmp("String", symbol_chars + 10, 6) == 0) {
358 return vmSymbols::java_lang_String();
359 } else if (strncmp("Object", symbol_chars + 10, 6) == 0) {
360 return vmSymbols::java_lang_Object();
361 }
362 }
363
364 Symbol* name = _previous_name;
365 if (name != NULL && name->equals(symbol_chars, len)) {
366 return name;
367 }
368
369 // Save names for cleaning up reference count at the end of
370 // SignatureStream scope.
371 name = SymbolTable::new_symbol(symbol_chars, len);
372 if (!name->is_permanent()) {
373 if (_names == NULL) {
374 _names = new GrowableArray<Symbol*>(10);
375 }
376 _names->push(name); // save new symbol for decrementing later
377 }
378 _previous_name = name;
379 return name;
380 }
381
382 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
383 FailureMode failure_mode, TRAPS) {
384 if (!is_object()) return NULL;
385 Symbol* name = as_symbol();
386 if (failure_mode == ReturnNull) {
387 return SystemDictionary::resolve_or_null(name, class_loader, protection_domain, THREAD);
388 } else {
389 bool throw_error = (failure_mode == NCDFError);
390 return SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, THREAD);
391 }
392 }
393
394 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
395 FailureMode failure_mode, TRAPS) {
396 if (!is_object())
397 return Universe::java_mirror(type());
398 Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
399 if (klass == NULL) return NULL;
400 return klass->java_mirror();
401 }
402
403 Symbol* SignatureStream::as_symbol_or_null() {
404 // Create a symbol from for string _begin _end
405 ResourceMark rm;
406
407 int begin = _begin;
408 int end = _end;
409
410 if ( _signature->char_at(_begin) == JVM_SIGNATURE_CLASS
411 && _signature->char_at(_end-1) == JVM_SIGNATURE_ENDCLASS) {
412 begin++;
413 end--;
414 }
415
416 char* buffer = NEW_RESOURCE_ARRAY(char, end - begin);
417 for (int index = begin; index < end; index++) {
418 buffer[index - begin] = _signature->char_at(index);
419 }
420 Symbol* result = SymbolTable::probe(buffer, end - begin);
421 return result;
422 }
423
424 int SignatureStream::reference_parameter_count() {
425 int args_count = 0;
426 for ( ; !at_return_type(); next()) {
427 if (is_object()) {
428 args_count++;
429 }
430 }
431 return args_count;
432 }
433
434 #ifdef ASSERT
435 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
436 const char* method_sig = (const char*)sig->bytes();
437 ssize_t len = sig->utf8_length();
438 ssize_t index = 0;
439 if (method_sig != NULL && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
440 ++index;
441 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
442 ssize_t res = is_valid_type(&method_sig[index], len - index);
443 if (res == -1) {
444 return false;
445 } else {
446 index += res;
447 }
448 }
449 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
450 // check the return type
451 ++index;
452 return (is_valid_type(&method_sig[index], len - index) == (len - index));
453 }
454 }
459 const char* type_sig = (const char*)sig->bytes();
460 ssize_t len = sig->utf8_length();
461 return (type_sig != NULL && len >= 1 &&
462 (is_valid_type(type_sig, len) == len));
463 }
464
465 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
466 // Returns -1 if it is not, or the index of the next character that is not part
467 // of the type. The type encoding may end before 'limit' and that's ok.
468 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
469 ssize_t index = 0;
470
471 // Iterate over any number of array dimensions
472 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
473 if (index >= limit) {
474 return -1;
475 }
476 switch (type[index]) {
477 case JVM_SIGNATURE_BYTE:
478 case JVM_SIGNATURE_CHAR:
479 case JVM_SIGNATURE_DOUBLE:
480 case JVM_SIGNATURE_FLOAT:
481 case JVM_SIGNATURE_INT:
482 case JVM_SIGNATURE_LONG:
483 case JVM_SIGNATURE_SHORT:
484 case JVM_SIGNATURE_BOOLEAN:
485 case JVM_SIGNATURE_VOID:
486 return index + 1;
487 case JVM_SIGNATURE_CLASS:
488 for (index = index + 1; index < limit; ++index) {
489 char c = type[index];
490 switch (c) {
491 case JVM_SIGNATURE_ENDCLASS:
492 return index + 1;
493 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
494 return -1;
495 default: ; // fall through
496 }
497 }
498 // fall through
499 default: ; // fall through
500 }
501 return -1;
502 }
503 #endif // ASSERT
|
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
25 #include "precompiled.hpp"
26 #include "classfile/symbolTable.hpp"
27 #include "classfile/systemDictionary.hpp"
28 #include "memory/oopFactory.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "memory/universe.hpp"
31 #include "oops/instanceKlass.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "oops/symbol.hpp"
34 #include "oops/typeArrayKlass.hpp"
35 #include "runtime/fieldDescriptor.inline.hpp"
36 #include "runtime/handles.inline.hpp"
37 #include "runtime/safepointVerifiers.hpp"
38 #include "runtime/signature.hpp"
39
40 // Implementation of SignatureIterator
41
42 // Signature syntax:
43 //
44 // Signature = "(" {Parameter} ")" ReturnType.
45 // Parameter = FieldType.
46 // ReturnType = FieldType | "V".
47 // FieldType = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "[" FieldType.
48 // ClassName = string.
49
50 // The ClassName string can be any JVM-style UTF8 string except:
51 // - an empty string (the empty string is never a name of any kind)
52 // - a string which begins or ends with slash '/' (the package separator)
53 // - a string which contains adjacent slashes '//' (no empty package names)
54 // - a string which contains a semicolon ';' (the end-delimiter)
55 // - a string which contains a left bracket '[' (the array marker)
56 // - a string which contains a dot '.' (the external package separator)
57 //
58 // Other "meta-looking" characters, such as '(' and '<' and '+',
59 // are perfectly legitimate within a class name, for the JVM.
60 // Class names which contain double slashes ('a//b') and non-initial
61 // brackets ('a[b]') are reserved for possible enrichment of the
62 // type language.
63
64 void SignatureIterator::set_fingerprint(fingerprint_t fingerprint) {
65 if (!fp_is_valid(fingerprint)) {
66 _fingerprint = fingerprint;
67 _return_type = T_ILLEGAL;
68 } else if (fingerprint != _fingerprint) {
69 assert(_fingerprint == zero_fingerprint(), "consistent fingerprint values");
70 _fingerprint = fingerprint;
71 _return_type = fp_return_type(fingerprint);
72 }
73 }
74
75 BasicType SignatureIterator::return_type() {
76 if (_return_type == T_ILLEGAL) {
77 SignatureStream ss(_signature);
78 ss.skip_to_return_type();
79 _return_type = ss.type();
80 assert(_return_type != T_ILLEGAL, "illegal return type");
81 }
82 return _return_type;
83 }
84
85 bool SignatureIterator::fp_is_valid_type(BasicType type, bool for_return_type) {
86 assert(type != (BasicType)fp_parameters_done, "fingerprint is incorrectly at done");
87 assert(((int)type & ~fp_parameter_feature_mask) == 0, "fingerprint feature mask yielded non-zero value");
88 return (is_java_primitive(type) ||
89 is_reference_type(type) ||
90 (for_return_type && type == T_VOID));
91 }
92
93 ArgumentSizeComputer::ArgumentSizeComputer(Symbol* signature)
94 : SignatureIterator(signature)
95 {
96 _size = 0;
97 do_parameters_on(this); // non-virtual template execution
98 }
99
100 ArgumentCount::ArgumentCount(Symbol* signature)
101 : SignatureIterator(signature)
102 {
103 _size = 0;
104 do_parameters_on(this); // non-virtual template execution
105 }
106
107 ReferenceArgumentCount::ReferenceArgumentCount(Symbol* signature)
108 : SignatureIterator(signature)
109 {
110 _refs = 0;
111 do_parameters_on(this); // non-virtual template execution
112 }
113
114 void Fingerprinter::compute_fingerprint_and_return_type(bool static_flag) {
115 // See if we fingerprinted this method already
116 if (_method != NULL) {
117 assert(!static_flag, "must not be passed by caller");
118 static_flag = _method->is_static();
119 _fingerprint = _method->constMethod()->fingerprint();
120
121 if (_fingerprint != zero_fingerprint()) {
122 _return_type = _method->result_type();
123 assert(is_java_type(_return_type), "return type must be a java type");
124 return;
125 }
126
127 if (_method->size_of_parameters() > fp_max_size_of_parameters) {
128 _fingerprint = overflow_fingerprint();
129 _method->constMethod()->set_fingerprint(_fingerprint);
130 // as long as we are here compute the return type:
131 _return_type = ResultTypeFinder(_method->signature()).type();
132 assert(is_java_type(_return_type), "return type must be a java type");
133 return;
134 }
135 }
136
137 // Note: This will always take the slow path, since _fp==zero_fp.
138 initialize_accumulator();
139 do_parameters_on(this);
140 assert(fp_is_valid_type(_return_type, true), "bad result type");
141
142 // Fill in the return type and static bits:
143 _accumulator |= _return_type << fp_static_feature_size;
144 if (static_flag) {
145 _accumulator |= fp_is_static_bit;
146 } else {
147 _param_size += 1; // this is the convention for Method::compute_size_of_parameters
148 }
149
150 // Detect overflow. (We counted _param_size correctly.)
151 if (_method == NULL && _param_size > fp_max_size_of_parameters) {
152 // We did a one-pass computation of argument size, return type,
153 // and fingerprint.
154 _fingerprint = overflow_fingerprint();
155 return;
156 }
157
158 assert(_shift_count < BitsPerLong,
159 "shift count overflow %d (%d vs. %d): %s",
160 _shift_count, _param_size, fp_max_size_of_parameters,
161 _signature->as_C_string());
162 assert((_accumulator >> _shift_count) == fp_parameters_done, "must be zero");
163
164 // This is the result, along with _return_type:
165 _fingerprint = _accumulator;
166
167 // Cache the result on the method itself:
168 if (_method != NULL) {
169 _method->constMethod()->set_fingerprint(_fingerprint);
170 }
171 }
172
173 // Implementation of SignatureStream
174
175 static inline int decode_signature_char(int ch) {
176 switch (ch) {
177 #define EACH_SIG(ch, bt, ignore) \
178 case ch: return bt;
179 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
180 #undef EACH_SIG
181 }
182 return 0;
183 }
184
185 SignatureStream::SignatureStream(const Symbol* signature,
186 bool is_method) {
187 assert(!is_method || signature->starts_with(JVM_SIGNATURE_FUNC),
188 "method signature required");
189 _signature = signature;
190 _limit = signature->utf8_length();
191 int oz = (is_method ? 1 : 0);
192 _state = oz;
193 assert(_state == (int)(is_method ? _s_method : _s_field), "signature state incorrectly set");
194 _begin = _end = oz; // skip first '(' in method signatures
195 _array_prefix = 0; // just for definiteness
196 _previous_name = NULL;
197 _names = NULL;
198 next();
199 }
200
201 SignatureStream::~SignatureStream() {
202 // decrement refcount for names created during signature parsing
203 if (_names != NULL) {
204 for (int i = 0; i < _names->length(); i++) {
205 _names->at(i)->decrement_refcount();
206 }
207 } else if (_previous_name != NULL && !_previous_name->is_permanent()) {
208 _previous_name->decrement_refcount();
209 }
210 }
211
212 inline int SignatureStream::scan_non_primitive(BasicType type) {
213 const u1* base = _signature->bytes();
214 int end = _end;
215 int limit = _limit;
216 const u1* tem;
217 switch (type) {
218 case T_OBJECT:
219 tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
220 end = (tem == NULL ? limit : tem+1 - base);
221 break;
222
223 case T_ARRAY:
224 while (end < limit && (char) base[end++] == JVM_SIGNATURE_ARRAY) { }
225 --end; // skipped a non-'['
226 _array_prefix = end - _end; // number of '[' chars just skipped
227 if (Signature::has_envelope(base[end++])) {
228 tem = (const u1*) memchr(&base[end], JVM_SIGNATURE_ENDCLASS, limit - end);
229 end = (tem == NULL ? limit : tem+1 - base);
230 break;
231 }
232 break;
233
234 default : ShouldNotReachHere();
235 }
236 return end;
237 }
238
239 void SignatureStream::next() {
240 const Symbol* sig = _signature;
241 int len = _limit;
242 if (_end >= len) { set_done(); return; }
243 _begin = _end;
244 int ch = sig->char_at(_begin);
245 int btcode = decode_signature_char(ch);
246 if (btcode == 0) {
247 guarantee(ch == JVM_SIGNATURE_ENDFUNC, "bad signature char %c/%d", ch, ch);
248 assert(_state == _s_method, "must be in method");
249 _state = _s_method_return;
250 _begin = ++_end;
251 if (_end >= len) { set_done(); return; }
252 ch = sig->char_at(_begin);
253 btcode = decode_signature_char(ch);
254 }
255 BasicType bt = (BasicType) btcode;
256 assert(ch == type2char(bt), "bad signature char %c/%d", ch, ch);
257 _type = bt;
258 if (!is_reference_type(bt)) {
259 // Skip over a single character for a primitive type (or void).
260 _end++;
261 return;
262 }
263 _end = scan_non_primitive(bt);
264 }
265
266 int SignatureStream::skip_array_prefix(int max_skip_length) {
267 if (_type != T_ARRAY) {
268 return 0;
269 }
270 if (_array_prefix > max_skip_length) {
271 // strip some but not all levels of T_ARRAY
272 _array_prefix -= max_skip_length;
273 _begin += max_skip_length;
274 return max_skip_length;
275 }
276 // we are stripping all levels of T_ARRAY,
277 // so we must decode the next character
278 int whole_array_prefix = _array_prefix;
279 int new_begin = _begin + whole_array_prefix;
280 _begin = new_begin;
281 int ch = _signature->char_at(new_begin);
282 int btcode = decode_signature_char(ch);
283 BasicType bt = (BasicType) btcode;
284 assert(ch == type2char(bt), "bad signature char %c/%d", ch, ch);
285 _type = bt;
286 assert(bt != T_VOID && bt != T_ARRAY, "bad signature type");
287 // Don't bother to call scan_non_primitive, since it won't
288 // change the value of _end.
289 return whole_array_prefix;
290 }
291
292 bool Signature::is_valid_array_signature(const Symbol* sig) {
293 assert(sig->utf8_length() > 1, "this should already have been checked");
294 assert(sig->char_at(0) == JVM_SIGNATURE_ARRAY, "this should already have been checked");
295 // The first character is already checked
296 int i = 1;
297 int len = sig->utf8_length();
298 // First skip all '['s
299 while(i < len - 1 && sig->char_at(i) == JVM_SIGNATURE_ARRAY) i++;
300
301 // Check type
302 switch(sig->char_at(i)) {
303 case JVM_SIGNATURE_BYTE:
304 case JVM_SIGNATURE_CHAR:
305 case JVM_SIGNATURE_DOUBLE:
306 case JVM_SIGNATURE_FLOAT:
307 case JVM_SIGNATURE_INT:
308 case JVM_SIGNATURE_LONG:
309 case JVM_SIGNATURE_SHORT:
310 case JVM_SIGNATURE_BOOLEAN:
311 // If it is an array, the type is the last character
312 return (i + 1 == len);
313 case JVM_SIGNATURE_CLASS:
314 // If it is an object, the last character must be a ';'
315 return sig->char_at(len - 1) == JVM_SIGNATURE_ENDCLASS;
316 }
317 return false;
318 }
319
320 BasicType Signature::basic_type(int ch) {
321 int btcode = decode_signature_char(ch);
322 if (btcode == 0) return T_ILLEGAL;
323 return (BasicType) btcode;
324 }
325
326 static const int jl_len = 10, object_len = 6, jl_object_len = jl_len + object_len;
327 static const char jl_str[] = "java/lang/";
328
329 #ifdef ASSERT
330 static bool signature_symbols_sane() {
331 static bool done;
332 if (done) return true;
333 done = true;
334 // test some tense code that looks for common symbol names:
335 assert(vmSymbols::java_lang_Object()->utf8_length() == jl_object_len &&
336 vmSymbols::java_lang_Object()->starts_with(jl_str, jl_len) &&
337 vmSymbols::java_lang_Object()->ends_with("Object", object_len) &&
338 vmSymbols::java_lang_Object()->is_permanent() &&
339 vmSymbols::java_lang_String()->utf8_length() == jl_object_len &&
340 vmSymbols::java_lang_String()->starts_with(jl_str, jl_len) &&
341 vmSymbols::java_lang_String()->ends_with("String", object_len) &&
342 vmSymbols::java_lang_String()->is_permanent(),
343 "sanity");
344 return true;
345 }
346 #endif //ASSERT
347
348 // returns a symbol; the caller is responsible for decrementing it
349 Symbol* SignatureStream::find_symbol() {
350 // Create a symbol from for string _begin _end
351 int begin = raw_symbol_begin();
352 int end = raw_symbol_end();
353
354 const char* symbol_chars = (const char*)_signature->base() + begin;
355 int len = end - begin;
356
357 // Quick check for common symbols in signatures
358 assert(signature_symbols_sane(), "incorrect signature sanity check");
359 if (len == jl_object_len &&
360 memcmp(symbol_chars, jl_str, jl_len) == 0) {
361 if (memcmp("String", symbol_chars + jl_len, object_len) == 0) {
362 return vmSymbols::java_lang_String();
363 } else if (memcmp("Object", symbol_chars + jl_len, object_len) == 0) {
364 return vmSymbols::java_lang_Object();
365 }
366 }
367
368 Symbol* name = _previous_name;
369 if (name != NULL && name->equals(symbol_chars, len)) {
370 return name;
371 }
372
373 // Save names for cleaning up reference count at the end of
374 // SignatureStream scope.
375 name = SymbolTable::new_symbol(symbol_chars, len);
376
377 // Only allocate the GrowableArray for the _names buffer if more than
378 // one name is being processed in the signature.
379 if (_previous_name != NULL &&
380 !_previous_name->is_permanent() &&
381 !name->is_permanent() &&
382 _names == NULL) {
383 _names = new GrowableArray<Symbol*>(10);
384 _names->push(_previous_name);
385 }
386 if (!name->is_permanent() && _previous_name != NULL) {
387 if (_names == NULL) {
388 _names = new GrowableArray<Symbol*>(10);
389 }
390 _names->push(name); // save new symbol for decrementing later
391 }
392 _previous_name = name;
393 return name;
394 }
395
396 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
397 FailureMode failure_mode, TRAPS) {
398 if (!is_reference()) return NULL;
399 Symbol* name = as_symbol();
400 if (name == NULL) {
401 assert(failure_mode == CachedOrNull, "signature incorrect failure mode");
402 return NULL;
403 }
404 Klass* k = NULL;
405 if (failure_mode == ReturnNull) {
406 // Note: SD::resolve_or_null returns NULL for most failure modes,
407 // but not all. Circularity errors, invalid PDs, etc., throw.
408 k = SystemDictionary::resolve_or_null(name, class_loader, protection_domain, CHECK_NULL);
409 } else if (failure_mode == CachedOrNull) {
410 NoSafepointVerifier nsv; // no loading, now, we mean it!
411 assert(!HAS_PENDING_EXCEPTION, "");
412 k = SystemDictionary::find(name, class_loader, protection_domain, CHECK_NULL);
413 // SD::find does not trigger loading, so there should be no throws
414 // Still, bad things can happen, so we CHECK_NULL and ask callers
415 // to do likewise.
416 return k;
417 } else {
418 // The only remaining failure mode is NCDFError.
419 // The test here allows for an additional mode CNFException
420 // if callers need to request the reflective error instead.
421 bool throw_error = (failure_mode == NCDFError);
422 k = SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, CHECK_NULL);
423 }
424
425 return k;
426 }
427
428 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
429 FailureMode failure_mode, TRAPS) {
430 if (!is_reference())
431 return Universe::java_mirror(type());
432 Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
433 if (klass == NULL) return NULL;
434 return klass->java_mirror();
435 }
436
437 void SignatureStream::skip_to_return_type() {
438 while (!at_return_type()) {
439 next();
440 }
441 }
442
443 #ifdef ASSERT
444
445 extern bool signature_constants_sane(); // called from basic_types_init()
446
447 bool signature_constants_sane() {
448 // for the lookup table, test every 8-bit code point, and then some:
449 for (int i = -256; i <= 256; i++) {
450 int btcode = 0;
451 switch (i) {
452 #define EACH_SIG(ch, bt, ignore) \
453 case ch: { btcode = bt; break; }
454 SIGNATURE_TYPES_DO(EACH_SIG, ignore)
455 #undef EACH_SIG
456 }
457 int btc = decode_signature_char(i);
458 assert(btc == btcode, "misconfigured table: %d => %d not %d", i, btc, btcode);
459 }
460 return true;
461 }
462
463 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
464 const char* method_sig = (const char*)sig->bytes();
465 ssize_t len = sig->utf8_length();
466 ssize_t index = 0;
467 if (method_sig != NULL && len > 1 && method_sig[index] == JVM_SIGNATURE_FUNC) {
468 ++index;
469 while (index < len && method_sig[index] != JVM_SIGNATURE_ENDFUNC) {
470 ssize_t res = is_valid_type(&method_sig[index], len - index);
471 if (res == -1) {
472 return false;
473 } else {
474 index += res;
475 }
476 }
477 if (index < len && method_sig[index] == JVM_SIGNATURE_ENDFUNC) {
478 // check the return type
479 ++index;
480 return (is_valid_type(&method_sig[index], len - index) == (len - index));
481 }
482 }
487 const char* type_sig = (const char*)sig->bytes();
488 ssize_t len = sig->utf8_length();
489 return (type_sig != NULL && len >= 1 &&
490 (is_valid_type(type_sig, len) == len));
491 }
492
493 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
494 // Returns -1 if it is not, or the index of the next character that is not part
495 // of the type. The type encoding may end before 'limit' and that's ok.
496 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
497 ssize_t index = 0;
498
499 // Iterate over any number of array dimensions
500 while (index < limit && type[index] == JVM_SIGNATURE_ARRAY) ++index;
501 if (index >= limit) {
502 return -1;
503 }
504 switch (type[index]) {
505 case JVM_SIGNATURE_BYTE:
506 case JVM_SIGNATURE_CHAR:
507 case JVM_SIGNATURE_FLOAT:
508 case JVM_SIGNATURE_DOUBLE:
509 case JVM_SIGNATURE_INT:
510 case JVM_SIGNATURE_LONG:
511 case JVM_SIGNATURE_SHORT:
512 case JVM_SIGNATURE_BOOLEAN:
513 case JVM_SIGNATURE_VOID:
514 return index + 1;
515 case JVM_SIGNATURE_CLASS:
516 for (index = index + 1; index < limit; ++index) {
517 char c = type[index];
518 switch (c) {
519 case JVM_SIGNATURE_ENDCLASS:
520 return index + 1;
521 case '\0': case JVM_SIGNATURE_DOT: case JVM_SIGNATURE_ARRAY:
522 return -1;
523 default: ; // fall through
524 }
525 }
526 // fall through
527 default: ; // fall through
528 }
529 return -1;
530 }
531
532 #endif // ASSERT
|