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)
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"
33 #include "stringpool.h"
37 #include "java-tree.h"
38 #include "java-except.h"
39 #include "diagnostic-core.h"
42 vfy_alloc (size_t bytes
)
44 return xmalloc (bytes
);
54 vfy_strings_equal (vfy_string one
, vfy_string two
)
60 vfy_string_bytes (vfy_string str
)
62 return IDENTIFIER_POINTER (str
);
66 vfy_string_length (vfy_string str
)
68 return IDENTIFIER_LENGTH (str
);
74 return init_identifier_node
;
78 vfy_clinit_name (void)
80 return clinit_identifier_node
;
84 skip_one_type (const char* ptr
)
95 do { ch
= *ptr
++; } while (ch
!= ';');
102 vfy_count_arguments (vfy_string signature
)
104 const char *ptr
= IDENTIFIER_POINTER (signature
);
113 ptr
= skip_one_type (ptr
);
121 vfy_get_string (const char *s
, int len
)
123 return get_identifier_with_length (s
, len
);
127 vfy_get_signature (vfy_method
*method
)
129 return method
->signature
;
133 vfy_get_method_name (vfy_method
*method
)
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
;
151 vfy_get_exceptions (vfy_method
*method
)
153 return method
->exceptions
;
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
;
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
);
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
));
201 vfy_get_pool_string (vfy_constants
*pool
, int index
)
203 return get_name_constant (pool
, index
);
207 vfy_get_pool_class (vfy_constants
*pool
, int index
)
210 k
= get_class_constant (pool
, index
);
215 vfy_get_class_name (vfy_jclass klass
)
217 return DECL_NAME (TYPE_NAME (klass
));
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
)
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
,
241 vfy_get_primitive_char (vfy_jclass klass
)
244 gcc_assert (vfy_is_primitive (klass
));
245 sig
= build_java_signature (klass
);
246 return (IDENTIFIER_POINTER (sig
))[0];
250 vfy_is_array (vfy_jclass klass
)
252 return TYPE_ARRAY_P (klass
);
256 vfy_is_interface (vfy_jclass klass
)
258 return CLASS_INTERFACE (TYPE_NAME (klass
));
262 vfy_is_primitive (vfy_jclass klass
)
264 return JPRIMITIVE_TYPE_P (klass
);
268 vfy_get_superclass (vfy_jclass klass
)
271 k
= CLASSTYPE_SUPER (klass
);
276 vfy_get_array_class (vfy_jclass klass
)
279 k
= build_java_array_type (klass
, -1);
284 vfy_get_component_type (vfy_jclass klass
)
287 gcc_assert (vfy_is_array (klass
));
288 k
= TYPE_ARRAY_ELEMENT (klass
);
289 if (TREE_CODE (k
) == POINTER_TYPE
)
295 vfy_is_abstract (vfy_jclass klass
)
297 return CLASS_ABSTRACT (TYPE_NAME (klass
));
301 vfy_find_class (vfy_jclass ignore ATTRIBUTE_UNUSED
, vfy_string name
)
305 k
= get_type_from_signature (name
);
306 if (TREE_CODE (k
) == POINTER_TYPE
)
313 vfy_object_type (void)
316 k
= object_type_node
;
321 vfy_class_type (void)
323 return class_type_node
;
327 vfy_string_type (void)
330 k
= string_type_node
;
335 vfy_throwable_type (void)
338 k
= throwable_type_node
;
343 vfy_unsuitable_type (void)
349 vfy_return_address_type (void)
351 return TYPE_RETURN_ADDR
;
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
)
370 field
= DECL_CHAIN (field
);
376 vfy_fail (const char *message
, int pc
, vfy_jclass ignore1 ATTRIBUTE_UNUSED
,
377 vfy_method
*ignore2 ATTRIBUTE_UNUSED
)
380 error ("verification failed: %s", message
);
382 error ("verification failed at PC=%d: %s", pc
, message
);
383 /* We have to return a value for the verifier to throw. */
388 vfy_get_primitive_type (int type
)
391 k
= decode_newarray_type (type
);
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. */
405 vfy_note_stack_type (vfy_method
*method
, int pc
, int slot
, vfy_jclass type
)
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. */
421 vfy_note_local_type (vfy_method
*method ATTRIBUTE_UNUSED
, int pc
, int slot
,
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. */
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
,
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");
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
);