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)
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>. */
29 #include "coretypes.h"
35 #include "java-tree.h"
36 #include "java-except.h"
40 vfy_alloc (size_t bytes
)
42 return xmalloc (bytes
);
52 vfy_strings_equal (vfy_string one
, vfy_string two
)
58 vfy_string_bytes (vfy_string str
)
60 return IDENTIFIER_POINTER (str
);
64 vfy_string_length (vfy_string str
)
66 return IDENTIFIER_LENGTH (str
);
72 return init_identifier_node
;
76 vfy_clinit_name (void)
78 return clinit_identifier_node
;
82 skip_one_type (const char* ptr
)
93 do { ch
= *ptr
++; } while (ch
!= ';');
100 vfy_count_arguments (vfy_string signature
)
102 const char *ptr
= IDENTIFIER_POINTER (signature
);
111 ptr
= skip_one_type (ptr
);
119 vfy_get_string (const char *s
, int len
)
121 return get_identifier_with_length (s
, len
);
125 vfy_get_signature (vfy_method
*method
)
127 return method
->signature
;
131 vfy_get_method_name (vfy_method
*method
)
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
;
149 vfy_get_exceptions (vfy_method
*method
)
151 return method
->exceptions
;
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
;
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
);
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
));
199 vfy_get_pool_string (vfy_constants
*pool
, int index
)
201 return get_name_constant (pool
, index
);
205 vfy_get_pool_class (vfy_constants
*pool
, int index
)
208 k
= get_class_constant (pool
, index
);
213 vfy_get_class_name (vfy_jclass klass
)
215 return DECL_NAME (TYPE_NAME (klass
));
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
)
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
,
239 vfy_get_primitive_char (vfy_jclass klass
)
242 gcc_assert (vfy_is_primitive (klass
));
243 sig
= build_java_signature (klass
);
244 return (IDENTIFIER_POINTER (sig
))[0];
248 vfy_is_array (vfy_jclass klass
)
250 return TYPE_ARRAY_P (klass
);
254 vfy_is_interface (vfy_jclass klass
)
256 return CLASS_INTERFACE (TYPE_NAME (klass
));
260 vfy_is_primitive (vfy_jclass klass
)
262 return JPRIMITIVE_TYPE_P (klass
);
266 vfy_get_superclass (vfy_jclass klass
)
269 k
= CLASSTYPE_SUPER (klass
);
274 vfy_get_array_class (vfy_jclass klass
)
277 k
= build_java_array_type (klass
, -1);
282 vfy_get_component_type (vfy_jclass klass
)
285 gcc_assert (vfy_is_array (klass
));
286 k
= TYPE_ARRAY_ELEMENT (klass
);
287 if (TREE_CODE (k
) == POINTER_TYPE
)
293 vfy_is_abstract (vfy_jclass klass
)
295 return CLASS_ABSTRACT (TYPE_NAME (klass
));
299 vfy_find_class (vfy_jclass ignore ATTRIBUTE_UNUSED
, vfy_string name
)
303 k
= get_type_from_signature (name
);
304 if (TREE_CODE (k
) == POINTER_TYPE
)
311 vfy_object_type (void)
314 k
= object_type_node
;
319 vfy_class_type (void)
321 return class_type_node
;
325 vfy_string_type (void)
328 k
= string_type_node
;
333 vfy_throwable_type (void)
336 k
= throwable_type_node
;
341 vfy_unsuitable_type (void)
347 vfy_return_address_type (void)
349 return TYPE_RETURN_ADDR
;
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
)
368 field
= TREE_CHAIN (field
);
374 vfy_fail (const char *message
, int pc
, vfy_jclass ignore1 ATTRIBUTE_UNUSED
,
375 vfy_method
*ignore2 ATTRIBUTE_UNUSED
)
378 error ("verification failed: %s", message
);
380 error ("verification failed at PC=%d: %s", pc
, message
);
381 /* We have to return a value for the verifier to throw. */
386 vfy_get_primitive_type (int type
)
389 k
= decode_newarray_type (type
);
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. */
403 vfy_note_stack_type (vfy_method
*method
, int pc
, int slot
, vfy_jclass type
)
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. */
419 vfy_note_local_type (vfy_method
*method ATTRIBUTE_UNUSED
, int pc
, int slot
,
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. */
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
,
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");
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
);