2015-08-24 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / java / verify-glue.c
bloba1630c7d77eca70dc31f6e7c153405d8e78561e3
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 "tree.h"
32 #include "options.h"
33 #include "stringpool.h"
34 #include "parse.h"
36 #include "verify.h"
37 #include "java-tree.h"
38 #include "java-except.h"
39 #include "diagnostic-core.h"
41 void *
42 vfy_alloc (size_t bytes)
44 return xmalloc (bytes);
47 void
48 vfy_free (void *mem)
50 free (mem);
53 bool
54 vfy_strings_equal (vfy_string one, vfy_string two)
56 return one == two;
59 const char *
60 vfy_string_bytes (vfy_string str)
62 return IDENTIFIER_POINTER (str);
65 int
66 vfy_string_length (vfy_string str)
68 return IDENTIFIER_LENGTH (str);
71 vfy_string
72 vfy_init_name (void)
74 return init_identifier_node;
77 vfy_string
78 vfy_clinit_name (void)
80 return clinit_identifier_node;
83 static const char*
84 skip_one_type (const char* ptr)
86 int ch = *ptr++;
88 while (ch == '[')
90 ch = *ptr++;
93 if (ch == 'L')
95 do { ch = *ptr++; } while (ch != ';');
98 return ptr;
102 vfy_count_arguments (vfy_string signature)
104 const char *ptr = IDENTIFIER_POINTER (signature);
105 int arg_count = 0;
107 /* Skip '('. */
108 ptr++;
110 /* Count args. */
111 while (*ptr != ')')
113 ptr = skip_one_type (ptr);
114 arg_count += 1;
117 return arg_count;
120 vfy_string
121 vfy_get_string (const char *s, int len)
123 return get_identifier_with_length (s, len);
126 vfy_string
127 vfy_get_signature (vfy_method *method)
129 return method->signature;
132 vfy_string
133 vfy_get_method_name (vfy_method *method)
135 return method->name;
138 bool
139 vfy_is_static (vfy_method *method)
141 return METHOD_STATIC (method->method);
144 const unsigned char *
145 vfy_get_bytecode (vfy_method *method)
147 return method->bytes;
150 vfy_exception *
151 vfy_get_exceptions (vfy_method *method)
153 return method->exceptions;
156 void
157 vfy_get_exception (vfy_exception *exceptions, int index, int *handler,
158 int *start, int *end, int *handler_type)
160 *handler = exceptions[index].handler;
161 *start = exceptions[index].start;
162 *end = exceptions[index].end;
163 *handler_type = exceptions[index].type;
167 vfy_tag (vfy_constants *pool, int index)
169 int result = JPOOL_TAG (pool, index);
170 /* gcj will resolve constant pool entries other than string and
171 class references. The verifier doesn't care about the values, so
172 we just strip off the resolved flag. */
173 if ((result & CONSTANT_ResolvedFlag) != 0
174 && result != CONSTANT_ResolvedString
175 && result != CONSTANT_ResolvedClass)
176 result &= ~ CONSTANT_ResolvedFlag;
177 return result;
180 void
181 vfy_load_indexes (vfy_constants *pool, int index,
182 vfy_uint_16 *index0, vfy_uint_16 *index1)
184 *index0 = JPOOL_USHORT1 (pool, index);
185 *index1 = JPOOL_USHORT2 (pool, index);
188 vfy_constants *
189 vfy_get_constants (vfy_jclass klass)
191 return TYPE_JCF (klass);
195 vfy_get_constants_size (vfy_jclass klass)
197 return JPOOL_SIZE (TYPE_JCF (klass));
200 vfy_string
201 vfy_get_pool_string (vfy_constants *pool, int index)
203 return get_name_constant (pool, index);
206 vfy_jclass
207 vfy_get_pool_class (vfy_constants *pool, int index)
209 vfy_jclass k;
210 k = get_class_constant (pool, index);
211 return k;
214 vfy_string
215 vfy_get_class_name (vfy_jclass klass)
217 return DECL_NAME (TYPE_NAME (klass));
220 bool
221 vfy_is_assignable_from (vfy_jclass target, vfy_jclass source)
223 /* Any class is always assignable to itself, or java.lang.Object. */
224 if (source == target || target == object_type_node)
225 return true;
227 /* For the C++ ABI, perform this test statically. */
228 if (! flag_indirect_dispatch)
229 return can_widen_reference_to (source, target);
231 /* For the BC-ABI, we assume at compile time that reference types are always
232 compatible. However, a type assertion table entry is emitted so that the
233 runtime can detect binary-incompatible changes. */
235 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE, source,
236 target);
237 return true;
240 char
241 vfy_get_primitive_char (vfy_jclass klass)
243 tree sig;
244 gcc_assert (vfy_is_primitive (klass));
245 sig = build_java_signature (klass);
246 return (IDENTIFIER_POINTER (sig))[0];
249 bool
250 vfy_is_array (vfy_jclass klass)
252 return TYPE_ARRAY_P (klass);
255 bool
256 vfy_is_interface (vfy_jclass klass)
258 return CLASS_INTERFACE (TYPE_NAME (klass));
261 bool
262 vfy_is_primitive (vfy_jclass klass)
264 return JPRIMITIVE_TYPE_P (klass);
267 vfy_jclass
268 vfy_get_superclass (vfy_jclass klass)
270 vfy_jclass k;
271 k = CLASSTYPE_SUPER (klass);
272 return k;
275 vfy_jclass
276 vfy_get_array_class (vfy_jclass klass)
278 vfy_jclass k;
279 k = build_java_array_type (klass, -1);
280 return k;
283 vfy_jclass
284 vfy_get_component_type (vfy_jclass klass)
286 vfy_jclass k;
287 gcc_assert (vfy_is_array (klass));
288 k = TYPE_ARRAY_ELEMENT (klass);
289 if (TREE_CODE (k) == POINTER_TYPE)
290 k = TREE_TYPE (k);
291 return k;
294 bool
295 vfy_is_abstract (vfy_jclass klass)
297 return CLASS_ABSTRACT (TYPE_NAME (klass));
300 vfy_jclass
301 vfy_find_class (vfy_jclass ignore ATTRIBUTE_UNUSED, vfy_string name)
303 vfy_jclass k;
305 k = get_type_from_signature (name);
306 if (TREE_CODE (k) == POINTER_TYPE)
307 k = TREE_TYPE (k);
309 return k;
312 vfy_jclass
313 vfy_object_type (void)
315 vfy_jclass k;
316 k = object_type_node;
317 return k;
320 vfy_jclass
321 vfy_class_type (void)
323 return class_type_node;
326 vfy_jclass
327 vfy_string_type (void)
329 vfy_jclass k;
330 k = string_type_node;
331 return k;
334 vfy_jclass
335 vfy_throwable_type (void)
337 vfy_jclass k;
338 k = throwable_type_node;
339 return k;
342 vfy_jclass
343 vfy_unsuitable_type (void)
345 return TYPE_SECOND;
348 vfy_jclass
349 vfy_return_address_type (void)
351 return TYPE_RETURN_ADDR;
354 vfy_jclass
355 vfy_null_type (void)
357 return TYPE_NULL;
360 bool
361 vfy_class_has_field (vfy_jclass klass, vfy_string name,
362 vfy_string signature)
364 tree field = TYPE_FIELDS (klass);
365 while (field != NULL_TREE)
367 if (DECL_NAME (field) == name
368 && build_java_signature (TREE_TYPE (field)) == signature)
369 return true;
370 field = DECL_CHAIN (field);
372 return false;
376 vfy_fail (const char *message, int pc, vfy_jclass ignore1 ATTRIBUTE_UNUSED,
377 vfy_method *ignore2 ATTRIBUTE_UNUSED)
379 if (pc == -1)
380 error ("verification failed: %s", message);
381 else
382 error ("verification failed at PC=%d: %s", pc, message);
383 /* We have to return a value for the verifier to throw. */
384 return 1;
387 vfy_jclass
388 vfy_get_primitive_type (int type)
390 vfy_jclass k;
391 k = decode_newarray_type (type);
392 return k;
395 void
396 vfy_note_stack_depth (vfy_method *method, int pc, int depth)
398 tree val = make_tree_vec (method->max_locals + depth);
399 (*type_states)[pc] = val;
400 /* Called for side effects. */
401 lookup_label (pc);
404 void
405 vfy_note_stack_type (vfy_method *method, int pc, int slot, vfy_jclass type)
407 tree vec;
409 slot += method->max_locals;
411 if (type == object_type_node)
412 type = object_ptr_type_node;
414 vec = (*type_states)[pc];
415 TREE_VEC_ELT (vec, slot) = type;
416 /* Called for side effects. */
417 lookup_label (pc);
420 void
421 vfy_note_local_type (vfy_method *method ATTRIBUTE_UNUSED, int pc, int slot,
422 vfy_jclass type)
424 tree vec;
426 if (type == object_type_node)
427 type = object_ptr_type_node;
429 vec = (*type_states)[pc];
430 TREE_VEC_ELT (vec, slot) = type;
431 /* Called for side effects. */
432 lookup_label (pc);
435 void
436 vfy_note_instruction_seen (int pc)
438 instruction_bits[pc] |= BCODE_VERIFIED;
441 /* Verify the bytecodes of the current method.
442 Return 1 on success, 0 on failure. */
444 verify_jvm_instructions_new (JCF *jcf, const unsigned char *byte_ops,
445 long length)
447 vfy_method method;
448 int i, result, eh_count;
449 vfy_exception *exceptions;
451 method_init_exceptions ();
453 JCF_SEEK (jcf, DECL_CODE_OFFSET (current_function_decl) + length);
454 eh_count = JCF_readu2 (jcf);
456 exceptions = (vfy_exception *) xmalloc (eh_count * sizeof (vfy_exception));
457 for (i = 0; i < eh_count; ++i)
459 int start_pc, end_pc, handler_pc, catch_type;
460 unsigned char *p = jcf->read_ptr + 8 * i;
461 start_pc = GET_u2 (p);
462 end_pc = GET_u2 (p+2);
463 handler_pc = GET_u2 (p+4);
464 catch_type = GET_u2 (p+6);
466 if (start_pc < 0 || start_pc >= length
467 || end_pc < 0 || end_pc > length || start_pc >= end_pc
468 || handler_pc < 0 || handler_pc >= length)
470 error ("bad pc in exception_table");
471 free (exceptions);
472 return 0;
475 exceptions[i].handler = handler_pc;
476 exceptions[i].start = start_pc;
477 exceptions[i].end = end_pc;
478 exceptions[i].type = catch_type;
480 add_handler (start_pc, end_pc,
481 lookup_label (handler_pc),
482 catch_type == 0 ? NULL_TREE
483 : get_class_constant (jcf, catch_type));
484 instruction_bits[handler_pc] |= BCODE_EXCEPTION_TARGET;
487 gcc_assert (sanity_check_exception_range (&whole_range));
489 method.method = current_function_decl;
490 method.signature = build_java_signature (TREE_TYPE (current_function_decl));
491 method.name = DECL_NAME (current_function_decl);
492 method.bytes = byte_ops;
493 method.exceptions = exceptions;
494 method.defining_class = DECL_CONTEXT (current_function_decl);
495 method.max_stack = DECL_MAX_STACK (current_function_decl);
496 method.max_locals = DECL_MAX_LOCALS (current_function_decl);
497 method.code_length = length;
498 method.exc_count = eh_count;
500 result = verify_method (&method);
502 free (exceptions);
504 return result;