Merge from mainline (160224:163495).
[official-gcc/graphite-test-results.git] / gcc / java / verify-glue.c
blob78d35495478360e2226b5ad3f2ffff5782cfd777
1 /* Glue to interface gcj with bytecode verifier.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 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 "tree.h"
31 #include "parse.h"
33 #include "verify.h"
34 #include "java-tree.h"
35 #include "java-except.h"
36 #include "diagnostic-core.h"
37 #include "toplev.h"
39 void *
40 vfy_alloc (size_t bytes)
42 return xmalloc (bytes);
45 void
46 vfy_free (void *mem)
48 free (mem);
51 bool
52 vfy_strings_equal (vfy_string one, vfy_string two)
54 return one == two;
57 const char *
58 vfy_string_bytes (vfy_string str)
60 return IDENTIFIER_POINTER (str);
63 int
64 vfy_string_length (vfy_string str)
66 return IDENTIFIER_LENGTH (str);
69 vfy_string
70 vfy_init_name (void)
72 return init_identifier_node;
75 vfy_string
76 vfy_clinit_name (void)
78 return clinit_identifier_node;
81 static const char*
82 skip_one_type (const char* ptr)
84 int ch = *ptr++;
86 while (ch == '[')
88 ch = *ptr++;
91 if (ch == 'L')
93 do { ch = *ptr++; } while (ch != ';');
96 return ptr;
99 int
100 vfy_count_arguments (vfy_string signature)
102 const char *ptr = IDENTIFIER_POINTER (signature);
103 int arg_count = 0;
105 /* Skip '('. */
106 ptr++;
108 /* Count args. */
109 while (*ptr != ')')
111 ptr = skip_one_type (ptr);
112 arg_count += 1;
115 return arg_count;
118 vfy_string
119 vfy_get_string (const char *s, int len)
121 return get_identifier_with_length (s, len);
124 vfy_string
125 vfy_get_signature (vfy_method *method)
127 return method->signature;
130 vfy_string
131 vfy_get_method_name (vfy_method *method)
133 return method->name;
136 bool
137 vfy_is_static (vfy_method *method)
139 return METHOD_STATIC (method->method);
142 const unsigned char *
143 vfy_get_bytecode (vfy_method *method)
145 return method->bytes;
148 vfy_exception *
149 vfy_get_exceptions (vfy_method *method)
151 return method->exceptions;
154 void
155 vfy_get_exception (vfy_exception *exceptions, int index, int *handler,
156 int *start, int *end, int *handler_type)
158 *handler = exceptions[index].handler;
159 *start = exceptions[index].start;
160 *end = exceptions[index].end;
161 *handler_type = exceptions[index].type;
165 vfy_tag (vfy_constants *pool, int index)
167 int result = JPOOL_TAG (pool, index);
168 /* gcj will resolve constant pool entries other than string and
169 class references. The verifier doesn't care about the values, so
170 we just strip off the resolved flag. */
171 if ((result & CONSTANT_ResolvedFlag) != 0
172 && result != CONSTANT_ResolvedString
173 && result != CONSTANT_ResolvedClass)
174 result &= ~ CONSTANT_ResolvedFlag;
175 return result;
178 void
179 vfy_load_indexes (vfy_constants *pool, int index,
180 vfy_uint_16 *index0, vfy_uint_16 *index1)
182 *index0 = JPOOL_USHORT1 (pool, index);
183 *index1 = JPOOL_USHORT2 (pool, index);
186 vfy_constants *
187 vfy_get_constants (vfy_jclass klass)
189 return TYPE_JCF (klass);
193 vfy_get_constants_size (vfy_jclass klass)
195 return JPOOL_SIZE (TYPE_JCF (klass));
198 vfy_string
199 vfy_get_pool_string (vfy_constants *pool, int index)
201 return get_name_constant (pool, index);
204 vfy_jclass
205 vfy_get_pool_class (vfy_constants *pool, int index)
207 vfy_jclass k;
208 k = get_class_constant (pool, index);
209 return k;
212 vfy_string
213 vfy_get_class_name (vfy_jclass klass)
215 return DECL_NAME (TYPE_NAME (klass));
218 bool
219 vfy_is_assignable_from (vfy_jclass target, vfy_jclass source)
221 /* Any class is always assignable to itself, or java.lang.Object. */
222 if (source == target || target == object_type_node)
223 return true;
225 /* For the C++ ABI, perform this test statically. */
226 if (! flag_indirect_dispatch)
227 return can_widen_reference_to (source, target);
229 /* For the BC-ABI, we assume at compile time that reference types are always
230 compatible. However, a type assertion table entry is emitted so that the
231 runtime can detect binary-incompatible changes. */
233 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE, source,
234 target);
235 return true;
238 char
239 vfy_get_primitive_char (vfy_jclass klass)
241 tree sig;
242 gcc_assert (vfy_is_primitive (klass));
243 sig = build_java_signature (klass);
244 return (IDENTIFIER_POINTER (sig))[0];
247 bool
248 vfy_is_array (vfy_jclass klass)
250 return TYPE_ARRAY_P (klass);
253 bool
254 vfy_is_interface (vfy_jclass klass)
256 return CLASS_INTERFACE (TYPE_NAME (klass));
259 bool
260 vfy_is_primitive (vfy_jclass klass)
262 return JPRIMITIVE_TYPE_P (klass);
265 vfy_jclass
266 vfy_get_superclass (vfy_jclass klass)
268 vfy_jclass k;
269 k = CLASSTYPE_SUPER (klass);
270 return k;
273 vfy_jclass
274 vfy_get_array_class (vfy_jclass klass)
276 vfy_jclass k;
277 k = build_java_array_type (klass, -1);
278 return k;
281 vfy_jclass
282 vfy_get_component_type (vfy_jclass klass)
284 vfy_jclass k;
285 gcc_assert (vfy_is_array (klass));
286 k = TYPE_ARRAY_ELEMENT (klass);
287 if (TREE_CODE (k) == POINTER_TYPE)
288 k = TREE_TYPE (k);
289 return k;
292 bool
293 vfy_is_abstract (vfy_jclass klass)
295 return CLASS_ABSTRACT (TYPE_NAME (klass));
298 vfy_jclass
299 vfy_find_class (vfy_jclass ignore ATTRIBUTE_UNUSED, vfy_string name)
301 vfy_jclass k;
303 k = get_type_from_signature (name);
304 if (TREE_CODE (k) == POINTER_TYPE)
305 k = TREE_TYPE (k);
307 return k;
310 vfy_jclass
311 vfy_object_type (void)
313 vfy_jclass k;
314 k = object_type_node;
315 return k;
318 vfy_jclass
319 vfy_class_type (void)
321 return class_type_node;
324 vfy_jclass
325 vfy_string_type (void)
327 vfy_jclass k;
328 k = string_type_node;
329 return k;
332 vfy_jclass
333 vfy_throwable_type (void)
335 vfy_jclass k;
336 k = throwable_type_node;
337 return k;
340 vfy_jclass
341 vfy_unsuitable_type (void)
343 return TYPE_SECOND;
346 vfy_jclass
347 vfy_return_address_type (void)
349 return TYPE_RETURN_ADDR;
352 vfy_jclass
353 vfy_null_type (void)
355 return TYPE_NULL;
358 bool
359 vfy_class_has_field (vfy_jclass klass, vfy_string name,
360 vfy_string signature)
362 tree field = TYPE_FIELDS (klass);
363 while (field != NULL_TREE)
365 if (DECL_NAME (field) == name
366 && build_java_signature (TREE_TYPE (field)) == signature)
367 return true;
368 field = DECL_CHAIN (field);
370 return false;
374 vfy_fail (const char *message, int pc, vfy_jclass ignore1 ATTRIBUTE_UNUSED,
375 vfy_method *ignore2 ATTRIBUTE_UNUSED)
377 if (pc == -1)
378 error ("verification failed: %s", message);
379 else
380 error ("verification failed at PC=%d: %s", pc, message);
381 /* We have to return a value for the verifier to throw. */
382 return 1;
385 vfy_jclass
386 vfy_get_primitive_type (int type)
388 vfy_jclass k;
389 k = decode_newarray_type (type);
390 return k;
393 void
394 vfy_note_stack_depth (vfy_method *method, int pc, int depth)
396 tree val = make_tree_vec (method->max_locals + depth);
397 VEC_replace (tree, type_states, pc, val);
398 /* Called for side effects. */
399 lookup_label (pc);
402 void
403 vfy_note_stack_type (vfy_method *method, int pc, int slot, vfy_jclass type)
405 tree vec;
407 slot += method->max_locals;
409 if (type == object_type_node)
410 type = object_ptr_type_node;
412 vec = VEC_index (tree, type_states, pc);
413 TREE_VEC_ELT (vec, slot) = type;
414 /* Called for side effects. */
415 lookup_label (pc);
418 void
419 vfy_note_local_type (vfy_method *method ATTRIBUTE_UNUSED, int pc, int slot,
420 vfy_jclass type)
422 tree vec;
424 if (type == object_type_node)
425 type = object_ptr_type_node;
427 vec = VEC_index (tree, type_states, pc);
428 TREE_VEC_ELT (vec, slot) = type;
429 /* Called for side effects. */
430 lookup_label (pc);
433 void
434 vfy_note_instruction_seen (int pc)
436 instruction_bits[pc] |= BCODE_VERIFIED;
439 /* Verify the bytecodes of the current method.
440 Return 1 on success, 0 on failure. */
442 verify_jvm_instructions_new (JCF *jcf, const unsigned char *byte_ops,
443 long length)
445 vfy_method method;
446 int i, result, eh_count;
447 vfy_exception *exceptions;
449 method_init_exceptions ();
451 JCF_SEEK (jcf, DECL_CODE_OFFSET (current_function_decl) + length);
452 eh_count = JCF_readu2 (jcf);
454 exceptions = (vfy_exception *) xmalloc (eh_count * sizeof (vfy_exception));
455 for (i = 0; i < eh_count; ++i)
457 int start_pc, end_pc, handler_pc, catch_type;
458 unsigned char *p = jcf->read_ptr + 8 * i;
459 start_pc = GET_u2 (p);
460 end_pc = GET_u2 (p+2);
461 handler_pc = GET_u2 (p+4);
462 catch_type = GET_u2 (p+6);
464 if (start_pc < 0 || start_pc >= length
465 || end_pc < 0 || end_pc > length || start_pc >= end_pc
466 || handler_pc < 0 || handler_pc >= length)
468 error ("bad pc in exception_table");
469 free (exceptions);
470 return 0;
473 exceptions[i].handler = handler_pc;
474 exceptions[i].start = start_pc;
475 exceptions[i].end = end_pc;
476 exceptions[i].type = catch_type;
478 add_handler (start_pc, end_pc,
479 lookup_label (handler_pc),
480 catch_type == 0 ? NULL_TREE
481 : get_class_constant (jcf, catch_type));
482 instruction_bits[handler_pc] |= BCODE_EXCEPTION_TARGET;
485 gcc_assert (sanity_check_exception_range (&whole_range));
487 method.method = current_function_decl;
488 method.signature = build_java_signature (TREE_TYPE (current_function_decl));
489 method.name = DECL_NAME (current_function_decl);
490 method.bytes = byte_ops;
491 method.exceptions = exceptions;
492 method.defining_class = DECL_CONTEXT (current_function_decl);
493 method.max_stack = DECL_MAX_STACK (current_function_decl);
494 method.max_locals = DECL_MAX_LOCALS (current_function_decl);
495 method.code_length = length;
496 method.exc_count = eh_count;
498 result = verify_method (&method);
500 free (exceptions);
502 return result;