decl.c: Reorder #include's and remove duplicates.
[official-gcc.git] / gcc / java / verify-glue.c
blob508383d5fff2d0219196daacc9a16ee4ebc976d7
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 "options.h"
31 #include "tree.h"
32 #include "stringpool.h"
33 #include "diagnostic-core.h"
34 #include "alias.h"
35 #include "parse.h"
37 #include "verify.h"
38 #include "java-except.h"
40 void *
41 vfy_alloc (size_t bytes)
43 return xmalloc (bytes);
46 void
47 vfy_free (void *mem)
49 free (mem);
52 bool
53 vfy_strings_equal (vfy_string one, vfy_string two)
55 return one == two;
58 const char *
59 vfy_string_bytes (vfy_string str)
61 return IDENTIFIER_POINTER (str);
64 int
65 vfy_string_length (vfy_string str)
67 return IDENTIFIER_LENGTH (str);
70 vfy_string
71 vfy_init_name (void)
73 return init_identifier_node;
76 vfy_string
77 vfy_clinit_name (void)
79 return clinit_identifier_node;
82 static const char*
83 skip_one_type (const char* ptr)
85 int ch = *ptr++;
87 while (ch == '[')
89 ch = *ptr++;
92 if (ch == 'L')
94 do { ch = *ptr++; } while (ch != ';');
97 return ptr;
101 vfy_count_arguments (vfy_string signature)
103 const char *ptr = IDENTIFIER_POINTER (signature);
104 int arg_count = 0;
106 /* Skip '('. */
107 ptr++;
109 /* Count args. */
110 while (*ptr != ')')
112 ptr = skip_one_type (ptr);
113 arg_count += 1;
116 return arg_count;
119 vfy_string
120 vfy_get_string (const char *s, int len)
122 return get_identifier_with_length (s, len);
125 vfy_string
126 vfy_get_signature (vfy_method *method)
128 return method->signature;
131 vfy_string
132 vfy_get_method_name (vfy_method *method)
134 return method->name;
137 bool
138 vfy_is_static (vfy_method *method)
140 return METHOD_STATIC (method->method);
143 const unsigned char *
144 vfy_get_bytecode (vfy_method *method)
146 return method->bytes;
149 vfy_exception *
150 vfy_get_exceptions (vfy_method *method)
152 return method->exceptions;
155 void
156 vfy_get_exception (vfy_exception *exceptions, int index, int *handler,
157 int *start, int *end, int *handler_type)
159 *handler = exceptions[index].handler;
160 *start = exceptions[index].start;
161 *end = exceptions[index].end;
162 *handler_type = exceptions[index].type;
166 vfy_tag (vfy_constants *pool, int index)
168 int result = JPOOL_TAG (pool, index);
169 /* gcj will resolve constant pool entries other than string and
170 class references. The verifier doesn't care about the values, so
171 we just strip off the resolved flag. */
172 if ((result & CONSTANT_ResolvedFlag) != 0
173 && result != CONSTANT_ResolvedString
174 && result != CONSTANT_ResolvedClass)
175 result &= ~ CONSTANT_ResolvedFlag;
176 return result;
179 void
180 vfy_load_indexes (vfy_constants *pool, int index,
181 vfy_uint_16 *index0, vfy_uint_16 *index1)
183 *index0 = JPOOL_USHORT1 (pool, index);
184 *index1 = JPOOL_USHORT2 (pool, index);
187 vfy_constants *
188 vfy_get_constants (vfy_jclass klass)
190 return TYPE_JCF (klass);
194 vfy_get_constants_size (vfy_jclass klass)
196 return JPOOL_SIZE (TYPE_JCF (klass));
199 vfy_string
200 vfy_get_pool_string (vfy_constants *pool, int index)
202 return get_name_constant (pool, index);
205 vfy_jclass
206 vfy_get_pool_class (vfy_constants *pool, int index)
208 vfy_jclass k;
209 k = get_class_constant (pool, index);
210 return k;
213 vfy_string
214 vfy_get_class_name (vfy_jclass klass)
216 return DECL_NAME (TYPE_NAME (klass));
219 bool
220 vfy_is_assignable_from (vfy_jclass target, vfy_jclass source)
222 /* Any class is always assignable to itself, or java.lang.Object. */
223 if (source == target || target == object_type_node)
224 return true;
226 /* For the C++ ABI, perform this test statically. */
227 if (! flag_indirect_dispatch)
228 return can_widen_reference_to (source, target);
230 /* For the BC-ABI, we assume at compile time that reference types are always
231 compatible. However, a type assertion table entry is emitted so that the
232 runtime can detect binary-incompatible changes. */
234 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE, source,
235 target);
236 return true;
239 char
240 vfy_get_primitive_char (vfy_jclass klass)
242 tree sig;
243 gcc_assert (vfy_is_primitive (klass));
244 sig = build_java_signature (klass);
245 return (IDENTIFIER_POINTER (sig))[0];
248 bool
249 vfy_is_array (vfy_jclass klass)
251 return TYPE_ARRAY_P (klass);
254 bool
255 vfy_is_interface (vfy_jclass klass)
257 return CLASS_INTERFACE (TYPE_NAME (klass));
260 bool
261 vfy_is_primitive (vfy_jclass klass)
263 return JPRIMITIVE_TYPE_P (klass);
266 vfy_jclass
267 vfy_get_superclass (vfy_jclass klass)
269 vfy_jclass k;
270 k = CLASSTYPE_SUPER (klass);
271 return k;
274 vfy_jclass
275 vfy_get_array_class (vfy_jclass klass)
277 vfy_jclass k;
278 k = build_java_array_type (klass, -1);
279 return k;
282 vfy_jclass
283 vfy_get_component_type (vfy_jclass klass)
285 vfy_jclass k;
286 gcc_assert (vfy_is_array (klass));
287 k = TYPE_ARRAY_ELEMENT (klass);
288 if (TREE_CODE (k) == POINTER_TYPE)
289 k = TREE_TYPE (k);
290 return k;
293 bool
294 vfy_is_abstract (vfy_jclass klass)
296 return CLASS_ABSTRACT (TYPE_NAME (klass));
299 vfy_jclass
300 vfy_find_class (vfy_jclass ignore ATTRIBUTE_UNUSED, vfy_string name)
302 vfy_jclass k;
304 k = get_type_from_signature (name);
305 if (TREE_CODE (k) == POINTER_TYPE)
306 k = TREE_TYPE (k);
308 return k;
311 vfy_jclass
312 vfy_object_type (void)
314 vfy_jclass k;
315 k = object_type_node;
316 return k;
319 vfy_jclass
320 vfy_class_type (void)
322 return class_type_node;
325 vfy_jclass
326 vfy_string_type (void)
328 vfy_jclass k;
329 k = string_type_node;
330 return k;
333 vfy_jclass
334 vfy_throwable_type (void)
336 vfy_jclass k;
337 k = throwable_type_node;
338 return k;
341 vfy_jclass
342 vfy_unsuitable_type (void)
344 return TYPE_SECOND;
347 vfy_jclass
348 vfy_return_address_type (void)
350 return TYPE_RETURN_ADDR;
353 vfy_jclass
354 vfy_null_type (void)
356 return TYPE_NULL;
359 bool
360 vfy_class_has_field (vfy_jclass klass, vfy_string name,
361 vfy_string signature)
363 tree field = TYPE_FIELDS (klass);
364 while (field != NULL_TREE)
366 if (DECL_NAME (field) == name
367 && build_java_signature (TREE_TYPE (field)) == signature)
368 return true;
369 field = DECL_CHAIN (field);
371 return false;
375 vfy_fail (const char *message, int pc, vfy_jclass ignore1 ATTRIBUTE_UNUSED,
376 vfy_method *ignore2 ATTRIBUTE_UNUSED)
378 if (pc == -1)
379 error ("verification failed: %s", message);
380 else
381 error ("verification failed at PC=%d: %s", pc, message);
382 /* We have to return a value for the verifier to throw. */
383 return 1;
386 vfy_jclass
387 vfy_get_primitive_type (int type)
389 vfy_jclass k;
390 k = decode_newarray_type (type);
391 return k;
394 void
395 vfy_note_stack_depth (vfy_method *method, int pc, int depth)
397 tree val = make_tree_vec (method->max_locals + depth);
398 (*type_states)[pc] = val;
399 /* Called for side effects. */
400 lookup_label (pc);
403 void
404 vfy_note_stack_type (vfy_method *method, int pc, int slot, vfy_jclass type)
406 tree vec;
408 slot += method->max_locals;
410 if (type == object_type_node)
411 type = object_ptr_type_node;
413 vec = (*type_states)[pc];
414 TREE_VEC_ELT (vec, slot) = type;
415 /* Called for side effects. */
416 lookup_label (pc);
419 void
420 vfy_note_local_type (vfy_method *method ATTRIBUTE_UNUSED, int pc, int slot,
421 vfy_jclass type)
423 tree vec;
425 if (type == object_type_node)
426 type = object_ptr_type_node;
428 vec = (*type_states)[pc];
429 TREE_VEC_ELT (vec, slot) = type;
430 /* Called for side effects. */
431 lookup_label (pc);
434 void
435 vfy_note_instruction_seen (int pc)
437 instruction_bits[pc] |= BCODE_VERIFIED;
440 /* Verify the bytecodes of the current method.
441 Return 1 on success, 0 on failure. */
443 verify_jvm_instructions_new (JCF *jcf, const unsigned char *byte_ops,
444 long length)
446 vfy_method method;
447 int i, result, eh_count;
448 vfy_exception *exceptions;
450 method_init_exceptions ();
452 JCF_SEEK (jcf, DECL_CODE_OFFSET (current_function_decl) + length);
453 eh_count = JCF_readu2 (jcf);
455 exceptions = (vfy_exception *) xmalloc (eh_count * sizeof (vfy_exception));
456 for (i = 0; i < eh_count; ++i)
458 int start_pc, end_pc, handler_pc, catch_type;
459 unsigned char *p = jcf->read_ptr + 8 * i;
460 start_pc = GET_u2 (p);
461 end_pc = GET_u2 (p+2);
462 handler_pc = GET_u2 (p+4);
463 catch_type = GET_u2 (p+6);
465 if (start_pc < 0 || start_pc >= length
466 || end_pc < 0 || end_pc > length || start_pc >= end_pc
467 || handler_pc < 0 || handler_pc >= length)
469 error ("bad pc in exception_table");
470 free (exceptions);
471 return 0;
474 exceptions[i].handler = handler_pc;
475 exceptions[i].start = start_pc;
476 exceptions[i].end = end_pc;
477 exceptions[i].type = catch_type;
479 add_handler (start_pc, end_pc,
480 lookup_label (handler_pc),
481 catch_type == 0 ? NULL_TREE
482 : get_class_constant (jcf, catch_type));
483 instruction_bits[handler_pc] |= BCODE_EXCEPTION_TARGET;
486 gcc_assert (sanity_check_exception_range (&whole_range));
488 method.method = current_function_decl;
489 method.signature = build_java_signature (TREE_TYPE (current_function_decl));
490 method.name = DECL_NAME (current_function_decl);
491 method.bytes = byte_ops;
492 method.exceptions = exceptions;
493 method.defining_class = DECL_CONTEXT (current_function_decl);
494 method.max_stack = DECL_MAX_STACK (current_function_decl);
495 method.max_locals = DECL_MAX_LOCALS (current_function_decl);
496 method.code_length = length;
497 method.exc_count = eh_count;
499 result = verify_method (&method);
501 free (exceptions);
503 return result;