2015-06-25 Zhouyi Zhou <yizhouzhou@ict.ac.cn>
[official-gcc.git] / gcc / java / verify-glue.c
blobc98a3ddf0d100859e867bec81eefc1f982d874a3
1 /* Glue to interface gcj with bytecode verifier.
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>.
20 Java and all Java-based marks are trademarks or registered trademarks
21 of Sun Microsystems, Inc. in the United States and other countries.
22 The Free Software Foundation is independent of Sun Microsystems, Inc. */
24 /* Written by Tom Tromey <tromey@redhat.com>. */
26 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "alias.h"
31 #include "symtab.h"
32 #include "options.h"
33 #include "tree.h"
34 #include "stringpool.h"
35 #include "parse.h"
37 #include "verify.h"
38 #include "java-tree.h"
39 #include "java-except.h"
40 #include "diagnostic-core.h"
42 void *
43 vfy_alloc (size_t bytes)
45 return xmalloc (bytes);
48 void
49 vfy_free (void *mem)
51 free (mem);
54 bool
55 vfy_strings_equal (vfy_string one, vfy_string two)
57 return one == two;
60 const char *
61 vfy_string_bytes (vfy_string str)
63 return IDENTIFIER_POINTER (str);
66 int
67 vfy_string_length (vfy_string str)
69 return IDENTIFIER_LENGTH (str);
72 vfy_string
73 vfy_init_name (void)
75 return init_identifier_node;
78 vfy_string
79 vfy_clinit_name (void)
81 return clinit_identifier_node;
84 static const char*
85 skip_one_type (const char* ptr)
87 int ch = *ptr++;
89 while (ch == '[')
91 ch = *ptr++;
94 if (ch == 'L')
96 do { ch = *ptr++; } while (ch != ';');
99 return ptr;
103 vfy_count_arguments (vfy_string signature)
105 const char *ptr = IDENTIFIER_POINTER (signature);
106 int arg_count = 0;
108 /* Skip '('. */
109 ptr++;
111 /* Count args. */
112 while (*ptr != ')')
114 ptr = skip_one_type (ptr);
115 arg_count += 1;
118 return arg_count;
121 vfy_string
122 vfy_get_string (const char *s, int len)
124 return get_identifier_with_length (s, len);
127 vfy_string
128 vfy_get_signature (vfy_method *method)
130 return method->signature;
133 vfy_string
134 vfy_get_method_name (vfy_method *method)
136 return method->name;
139 bool
140 vfy_is_static (vfy_method *method)
142 return METHOD_STATIC (method->method);
145 const unsigned char *
146 vfy_get_bytecode (vfy_method *method)
148 return method->bytes;
151 vfy_exception *
152 vfy_get_exceptions (vfy_method *method)
154 return method->exceptions;
157 void
158 vfy_get_exception (vfy_exception *exceptions, int index, int *handler,
159 int *start, int *end, int *handler_type)
161 *handler = exceptions[index].handler;
162 *start = exceptions[index].start;
163 *end = exceptions[index].end;
164 *handler_type = exceptions[index].type;
168 vfy_tag (vfy_constants *pool, int index)
170 int result = JPOOL_TAG (pool, index);
171 /* gcj will resolve constant pool entries other than string and
172 class references. The verifier doesn't care about the values, so
173 we just strip off the resolved flag. */
174 if ((result & CONSTANT_ResolvedFlag) != 0
175 && result != CONSTANT_ResolvedString
176 && result != CONSTANT_ResolvedClass)
177 result &= ~ CONSTANT_ResolvedFlag;
178 return result;
181 void
182 vfy_load_indexes (vfy_constants *pool, int index,
183 vfy_uint_16 *index0, vfy_uint_16 *index1)
185 *index0 = JPOOL_USHORT1 (pool, index);
186 *index1 = JPOOL_USHORT2 (pool, index);
189 vfy_constants *
190 vfy_get_constants (vfy_jclass klass)
192 return TYPE_JCF (klass);
196 vfy_get_constants_size (vfy_jclass klass)
198 return JPOOL_SIZE (TYPE_JCF (klass));
201 vfy_string
202 vfy_get_pool_string (vfy_constants *pool, int index)
204 return get_name_constant (pool, index);
207 vfy_jclass
208 vfy_get_pool_class (vfy_constants *pool, int index)
210 vfy_jclass k;
211 k = get_class_constant (pool, index);
212 return k;
215 vfy_string
216 vfy_get_class_name (vfy_jclass klass)
218 return DECL_NAME (TYPE_NAME (klass));
221 bool
222 vfy_is_assignable_from (vfy_jclass target, vfy_jclass source)
224 /* Any class is always assignable to itself, or java.lang.Object. */
225 if (source == target || target == object_type_node)
226 return true;
228 /* For the C++ ABI, perform this test statically. */
229 if (! flag_indirect_dispatch)
230 return can_widen_reference_to (source, target);
232 /* For the BC-ABI, we assume at compile time that reference types are always
233 compatible. However, a type assertion table entry is emitted so that the
234 runtime can detect binary-incompatible changes. */
236 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE, source,
237 target);
238 return true;
241 char
242 vfy_get_primitive_char (vfy_jclass klass)
244 tree sig;
245 gcc_assert (vfy_is_primitive (klass));
246 sig = build_java_signature (klass);
247 return (IDENTIFIER_POINTER (sig))[0];
250 bool
251 vfy_is_array (vfy_jclass klass)
253 return TYPE_ARRAY_P (klass);
256 bool
257 vfy_is_interface (vfy_jclass klass)
259 return CLASS_INTERFACE (TYPE_NAME (klass));
262 bool
263 vfy_is_primitive (vfy_jclass klass)
265 return JPRIMITIVE_TYPE_P (klass);
268 vfy_jclass
269 vfy_get_superclass (vfy_jclass klass)
271 vfy_jclass k;
272 k = CLASSTYPE_SUPER (klass);
273 return k;
276 vfy_jclass
277 vfy_get_array_class (vfy_jclass klass)
279 vfy_jclass k;
280 k = build_java_array_type (klass, -1);
281 return k;
284 vfy_jclass
285 vfy_get_component_type (vfy_jclass klass)
287 vfy_jclass k;
288 gcc_assert (vfy_is_array (klass));
289 k = TYPE_ARRAY_ELEMENT (klass);
290 if (TREE_CODE (k) == POINTER_TYPE)
291 k = TREE_TYPE (k);
292 return k;
295 bool
296 vfy_is_abstract (vfy_jclass klass)
298 return CLASS_ABSTRACT (TYPE_NAME (klass));
301 vfy_jclass
302 vfy_find_class (vfy_jclass ignore ATTRIBUTE_UNUSED, vfy_string name)
304 vfy_jclass k;
306 k = get_type_from_signature (name);
307 if (TREE_CODE (k) == POINTER_TYPE)
308 k = TREE_TYPE (k);
310 return k;
313 vfy_jclass
314 vfy_object_type (void)
316 vfy_jclass k;
317 k = object_type_node;
318 return k;
321 vfy_jclass
322 vfy_class_type (void)
324 return class_type_node;
327 vfy_jclass
328 vfy_string_type (void)
330 vfy_jclass k;
331 k = string_type_node;
332 return k;
335 vfy_jclass
336 vfy_throwable_type (void)
338 vfy_jclass k;
339 k = throwable_type_node;
340 return k;
343 vfy_jclass
344 vfy_unsuitable_type (void)
346 return TYPE_SECOND;
349 vfy_jclass
350 vfy_return_address_type (void)
352 return TYPE_RETURN_ADDR;
355 vfy_jclass
356 vfy_null_type (void)
358 return TYPE_NULL;
361 bool
362 vfy_class_has_field (vfy_jclass klass, vfy_string name,
363 vfy_string signature)
365 tree field = TYPE_FIELDS (klass);
366 while (field != NULL_TREE)
368 if (DECL_NAME (field) == name
369 && build_java_signature (TREE_TYPE (field)) == signature)
370 return true;
371 field = DECL_CHAIN (field);
373 return false;
377 vfy_fail (const char *message, int pc, vfy_jclass ignore1 ATTRIBUTE_UNUSED,
378 vfy_method *ignore2 ATTRIBUTE_UNUSED)
380 if (pc == -1)
381 error ("verification failed: %s", message);
382 else
383 error ("verification failed at PC=%d: %s", pc, message);
384 /* We have to return a value for the verifier to throw. */
385 return 1;
388 vfy_jclass
389 vfy_get_primitive_type (int type)
391 vfy_jclass k;
392 k = decode_newarray_type (type);
393 return k;
396 void
397 vfy_note_stack_depth (vfy_method *method, int pc, int depth)
399 tree val = make_tree_vec (method->max_locals + depth);
400 (*type_states)[pc] = val;
401 /* Called for side effects. */
402 lookup_label (pc);
405 void
406 vfy_note_stack_type (vfy_method *method, int pc, int slot, vfy_jclass type)
408 tree vec;
410 slot += method->max_locals;
412 if (type == object_type_node)
413 type = object_ptr_type_node;
415 vec = (*type_states)[pc];
416 TREE_VEC_ELT (vec, slot) = type;
417 /* Called for side effects. */
418 lookup_label (pc);
421 void
422 vfy_note_local_type (vfy_method *method ATTRIBUTE_UNUSED, int pc, int slot,
423 vfy_jclass type)
425 tree vec;
427 if (type == object_type_node)
428 type = object_ptr_type_node;
430 vec = (*type_states)[pc];
431 TREE_VEC_ELT (vec, slot) = type;
432 /* Called for side effects. */
433 lookup_label (pc);
436 void
437 vfy_note_instruction_seen (int pc)
439 instruction_bits[pc] |= BCODE_VERIFIED;
442 /* Verify the bytecodes of the current method.
443 Return 1 on success, 0 on failure. */
445 verify_jvm_instructions_new (JCF *jcf, const unsigned char *byte_ops,
446 long length)
448 vfy_method method;
449 int i, result, eh_count;
450 vfy_exception *exceptions;
452 method_init_exceptions ();
454 JCF_SEEK (jcf, DECL_CODE_OFFSET (current_function_decl) + length);
455 eh_count = JCF_readu2 (jcf);
457 exceptions = (vfy_exception *) xmalloc (eh_count * sizeof (vfy_exception));
458 for (i = 0; i < eh_count; ++i)
460 int start_pc, end_pc, handler_pc, catch_type;
461 unsigned char *p = jcf->read_ptr + 8 * i;
462 start_pc = GET_u2 (p);
463 end_pc = GET_u2 (p+2);
464 handler_pc = GET_u2 (p+4);
465 catch_type = GET_u2 (p+6);
467 if (start_pc < 0 || start_pc >= length
468 || end_pc < 0 || end_pc > length || start_pc >= end_pc
469 || handler_pc < 0 || handler_pc >= length)
471 error ("bad pc in exception_table");
472 free (exceptions);
473 return 0;
476 exceptions[i].handler = handler_pc;
477 exceptions[i].start = start_pc;
478 exceptions[i].end = end_pc;
479 exceptions[i].type = catch_type;
481 add_handler (start_pc, end_pc,
482 lookup_label (handler_pc),
483 catch_type == 0 ? NULL_TREE
484 : get_class_constant (jcf, catch_type));
485 instruction_bits[handler_pc] |= BCODE_EXCEPTION_TARGET;
488 gcc_assert (sanity_check_exception_range (&whole_range));
490 method.method = current_function_decl;
491 method.signature = build_java_signature (TREE_TYPE (current_function_decl));
492 method.name = DECL_NAME (current_function_decl);
493 method.bytes = byte_ops;
494 method.exceptions = exceptions;
495 method.defining_class = DECL_CONTEXT (current_function_decl);
496 method.max_stack = DECL_MAX_STACK (current_function_decl);
497 method.max_locals = DECL_MAX_LOCALS (current_function_decl);
498 method.code_length = length;
499 method.exc_count = eh_count;
501 result = verify_method (&method);
503 free (exceptions);
505 return result;