1 /* Glue to interface gcj with bytecode verifier.
2 Copyright (C) 2003, 2004, 2005, 2006 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 2, 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 COPYING. If not, write to
18 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
21 Java and all Java-based marks are trademarks or registered trademarks
22 of Sun Microsystems, Inc. in the United States and other countries.
23 The Free Software Foundation is independent of Sun Microsystems, Inc. */
25 /* Written by Tom Tromey <tromey@redhat.com>. */
30 #include "coretypes.h"
36 #include "java-tree.h"
37 #include "java-except.h"
41 vfy_alloc (size_t bytes
)
43 return xmalloc (bytes
);
53 vfy_strings_equal (vfy_string one
, vfy_string two
)
59 vfy_string_bytes (vfy_string str
)
61 return IDENTIFIER_POINTER (str
);
65 vfy_string_length (vfy_string str
)
67 return IDENTIFIER_LENGTH (str
);
73 return init_identifier_node
;
77 vfy_clinit_name (void)
79 return clinit_identifier_node
;
83 skip_one_type (const char* ptr
)
94 do { ch
= *ptr
++; } while (ch
!= ';');
101 vfy_count_arguments (vfy_string signature
)
103 const char *ptr
= IDENTIFIER_POINTER (signature
);
112 ptr
= skip_one_type (ptr
);
120 vfy_get_string (const char *s
, int len
)
122 return get_identifier_with_length (s
, len
);
126 vfy_get_signature (vfy_method
*method
)
128 return method
->signature
;
132 vfy_get_method_name (vfy_method
*method
)
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
;
150 vfy_get_exceptions (vfy_method
*method
)
152 return method
->exceptions
;
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
;
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
);
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
));
200 vfy_get_pool_string (vfy_constants
*pool
, int index
)
202 return get_name_constant (pool
, index
);
206 vfy_get_pool_class (vfy_constants
*pool
, int index
)
209 k
= get_class_constant (pool
, index
);
214 vfy_make_string (const char *s
, int len
)
217 char *s2
= (char *) s
;
220 result
= get_identifier (s2
);
226 vfy_get_class_name (vfy_jclass klass
)
228 return DECL_NAME (TYPE_NAME (klass
));
232 vfy_is_assignable_from (vfy_jclass target
, vfy_jclass source
)
234 /* Any class is always assignable to itself, or java.lang.Object. */
235 if (source
== target
|| target
== object_type_node
)
238 /* For the C++ ABI, perform this test statically. */
239 if (! flag_indirect_dispatch
)
240 return can_widen_reference_to (source
, target
);
242 /* For the BC-ABI, we assume at compile time that reference types are always
243 compatible. However, a type assertion table entry is emitted so that the
244 runtime can detect binary-incompatible changes. */
246 add_type_assertion (current_class
, JV_ASSERT_TYPES_COMPATIBLE
, source
,
252 vfy_get_primitive_char (vfy_jclass klass
)
255 gcc_assert (vfy_is_primitive (klass
));
256 sig
= build_java_signature (klass
);
257 return (IDENTIFIER_POINTER (sig
))[0];
261 vfy_is_array (vfy_jclass klass
)
263 return TYPE_ARRAY_P (klass
);
267 vfy_is_interface (vfy_jclass klass
)
269 return CLASS_INTERFACE (TYPE_NAME (klass
));
273 vfy_is_primitive (vfy_jclass klass
)
275 return JPRIMITIVE_TYPE_P (klass
);
279 vfy_get_superclass (vfy_jclass klass
)
282 k
= CLASSTYPE_SUPER (klass
);
287 vfy_get_array_class (vfy_jclass klass
)
290 k
= build_java_array_type (klass
, -1);
295 vfy_get_component_type (vfy_jclass klass
)
298 gcc_assert (vfy_is_array (klass
));
299 k
= TYPE_ARRAY_ELEMENT (klass
);
300 if (TREE_CODE (k
) == POINTER_TYPE
)
306 vfy_is_abstract (vfy_jclass klass
)
308 return CLASS_ABSTRACT (TYPE_NAME (klass
));
312 vfy_find_class (vfy_jclass ignore ATTRIBUTE_UNUSED
, vfy_string name
)
316 k
= get_type_from_signature (name
);
317 if (TREE_CODE (k
) == POINTER_TYPE
)
324 vfy_object_type (void)
327 k
= object_type_node
;
332 vfy_class_type (void)
334 return class_type_node
;
338 vfy_string_type (void)
341 k
= string_type_node
;
346 vfy_throwable_type (void)
349 k
= throwable_type_node
;
354 vfy_unsuitable_type (void)
360 vfy_return_address_type (void)
362 return TYPE_RETURN_ADDR
;
372 vfy_class_has_field (vfy_jclass klass
, vfy_string name
,
373 vfy_string signature
)
375 tree field
= TYPE_FIELDS (klass
);
376 while (field
!= NULL_TREE
)
378 if (DECL_NAME (field
) == name
379 && build_java_signature (TREE_TYPE (field
)) == signature
)
381 field
= TREE_CHAIN (field
);
387 vfy_fail (const char *message
, int pc
, vfy_jclass ignore1 ATTRIBUTE_UNUSED
,
388 vfy_method
*ignore2 ATTRIBUTE_UNUSED
)
391 error ("verification failed: %s", message
);
393 error ("verification failed at PC=%d: %s", pc
, message
);
394 /* We have to return a value for the verifier to throw. */
399 vfy_get_primitive_type (int type
)
402 k
= decode_newarray_type (type
);
407 vfy_note_stack_depth (vfy_method
*method
, int pc
, int depth
)
409 tree label
= lookup_label (pc
);
410 LABEL_TYPE_STATE (label
) = make_tree_vec (method
->max_locals
+ depth
);
414 vfy_note_stack_type (vfy_method
*method
, int pc
, int slot
, vfy_jclass type
)
418 slot
+= method
->max_locals
;
420 if (type
== object_type_node
)
421 type
= object_ptr_type_node
;
423 label
= lookup_label (pc
);
424 vec
= LABEL_TYPE_STATE (label
);
425 TREE_VEC_ELT (vec
, slot
) = type
;
429 vfy_note_local_type (vfy_method
*method ATTRIBUTE_UNUSED
, int pc
, int slot
,
434 if (type
== object_type_node
)
435 type
= object_ptr_type_node
;
437 label
= lookup_label (pc
);
438 vec
= LABEL_TYPE_STATE (label
);
439 TREE_VEC_ELT (vec
, slot
) = type
;
443 vfy_note_instruction_seen (int pc
)
445 instruction_bits
[pc
] |= BCODE_VERIFIED
;
448 /* Verify the bytecodes of the current method.
449 Return 1 on success, 0 on failure. */
451 verify_jvm_instructions_new (JCF
*jcf
, const unsigned char *byte_ops
,
455 int i
, result
, eh_count
;
456 vfy_exception
*exceptions
;
458 method_init_exceptions ();
460 JCF_SEEK (jcf
, DECL_CODE_OFFSET (current_function_decl
) + length
);
461 eh_count
= JCF_readu2 (jcf
);
463 exceptions
= (vfy_exception
*) xmalloc (eh_count
* sizeof (vfy_exception
));
464 for (i
= 0; i
< eh_count
; ++i
)
466 int start_pc
, end_pc
, handler_pc
, catch_type
;
467 unsigned char *p
= jcf
->read_ptr
+ 8 * i
;
468 start_pc
= GET_u2 (p
);
469 end_pc
= GET_u2 (p
+2);
470 handler_pc
= GET_u2 (p
+4);
471 catch_type
= GET_u2 (p
+6);
473 if (start_pc
< 0 || start_pc
>= length
474 || end_pc
< 0 || end_pc
> length
|| start_pc
>= end_pc
475 || handler_pc
< 0 || handler_pc
>= length
)
477 error ("bad pc in exception_table");
482 exceptions
[i
].handler
= handler_pc
;
483 exceptions
[i
].start
= start_pc
;
484 exceptions
[i
].end
= end_pc
;
485 exceptions
[i
].type
= catch_type
;
487 add_handler (start_pc
, end_pc
,
488 lookup_label (handler_pc
),
489 catch_type
== 0 ? NULL_TREE
490 : get_class_constant (jcf
, catch_type
));
491 instruction_bits
[handler_pc
] |= BCODE_EXCEPTION_TARGET
;
494 gcc_assert (sanity_check_exception_range (&whole_range
));
496 method
.method
= current_function_decl
;
497 method
.signature
= build_java_signature (TREE_TYPE (current_function_decl
));
498 method
.name
= DECL_NAME (current_function_decl
);
499 method
.bytes
= byte_ops
;
500 method
.exceptions
= exceptions
;
501 method
.defining_class
= DECL_CONTEXT (current_function_decl
);
502 method
.max_stack
= DECL_MAX_STACK (current_function_decl
);
503 method
.max_locals
= DECL_MAX_LOCALS (current_function_decl
);
504 method
.code_length
= length
;
505 method
.exc_count
= eh_count
;
507 result
= verify_method (&method
);