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"
34 #include "stringpool.h"
38 #include "java-tree.h"
39 #include "java-except.h"
40 #include "diagnostic-core.h"
43 vfy_alloc (size_t bytes
)
45 return xmalloc (bytes
);
55 vfy_strings_equal (vfy_string one
, vfy_string two
)
61 vfy_string_bytes (vfy_string str
)
63 return IDENTIFIER_POINTER (str
);
67 vfy_string_length (vfy_string str
)
69 return IDENTIFIER_LENGTH (str
);
75 return init_identifier_node
;
79 vfy_clinit_name (void)
81 return clinit_identifier_node
;
85 skip_one_type (const char* ptr
)
96 do { ch
= *ptr
++; } while (ch
!= ';');
103 vfy_count_arguments (vfy_string signature
)
105 const char *ptr
= IDENTIFIER_POINTER (signature
);
114 ptr
= skip_one_type (ptr
);
122 vfy_get_string (const char *s
, int len
)
124 return get_identifier_with_length (s
, len
);
128 vfy_get_signature (vfy_method
*method
)
130 return method
->signature
;
134 vfy_get_method_name (vfy_method
*method
)
140 vfy_is_static (vfy_method
*method
)
142 return METHOD_STATIC (method
->method
);
145 const unsigned char *
146 vfy_get_bytecode (vfy_method
*method
)
148 return method
->bytes
;
152 vfy_get_exceptions (vfy_method
*method
)
154 return method
->exceptions
;
158 vfy_get_exception (vfy_exception
*exceptions
, int index
, int *handler
,
159 int *start
, int *end
, int *handler_type
)
161 *handler
= exceptions
[index
].handler
;
162 *start
= exceptions
[index
].start
;
163 *end
= exceptions
[index
].end
;
164 *handler_type
= exceptions
[index
].type
;
168 vfy_tag (vfy_constants
*pool
, int index
)
170 int result
= JPOOL_TAG (pool
, index
);
171 /* gcj will resolve constant pool entries other than string and
172 class references. The verifier doesn't care about the values, so
173 we just strip off the resolved flag. */
174 if ((result
& CONSTANT_ResolvedFlag
) != 0
175 && result
!= CONSTANT_ResolvedString
176 && result
!= CONSTANT_ResolvedClass
)
177 result
&= ~ CONSTANT_ResolvedFlag
;
182 vfy_load_indexes (vfy_constants
*pool
, int index
,
183 vfy_uint_16
*index0
, vfy_uint_16
*index1
)
185 *index0
= JPOOL_USHORT1 (pool
, index
);
186 *index1
= JPOOL_USHORT2 (pool
, index
);
190 vfy_get_constants (vfy_jclass klass
)
192 return TYPE_JCF (klass
);
196 vfy_get_constants_size (vfy_jclass klass
)
198 return JPOOL_SIZE (TYPE_JCF (klass
));
202 vfy_get_pool_string (vfy_constants
*pool
, int index
)
204 return get_name_constant (pool
, index
);
208 vfy_get_pool_class (vfy_constants
*pool
, int index
)
211 k
= get_class_constant (pool
, index
);
216 vfy_get_class_name (vfy_jclass klass
)
218 return DECL_NAME (TYPE_NAME (klass
));
222 vfy_is_assignable_from (vfy_jclass target
, vfy_jclass source
)
224 /* Any class is always assignable to itself, or java.lang.Object. */
225 if (source
== target
|| target
== object_type_node
)
228 /* For the C++ ABI, perform this test statically. */
229 if (! flag_indirect_dispatch
)
230 return can_widen_reference_to (source
, target
);
232 /* For the BC-ABI, we assume at compile time that reference types are always
233 compatible. However, a type assertion table entry is emitted so that the
234 runtime can detect binary-incompatible changes. */
236 add_type_assertion (current_class
, JV_ASSERT_TYPES_COMPATIBLE
, source
,
242 vfy_get_primitive_char (vfy_jclass klass
)
245 gcc_assert (vfy_is_primitive (klass
));
246 sig
= build_java_signature (klass
);
247 return (IDENTIFIER_POINTER (sig
))[0];
251 vfy_is_array (vfy_jclass klass
)
253 return TYPE_ARRAY_P (klass
);
257 vfy_is_interface (vfy_jclass klass
)
259 return CLASS_INTERFACE (TYPE_NAME (klass
));
263 vfy_is_primitive (vfy_jclass klass
)
265 return JPRIMITIVE_TYPE_P (klass
);
269 vfy_get_superclass (vfy_jclass klass
)
272 k
= CLASSTYPE_SUPER (klass
);
277 vfy_get_array_class (vfy_jclass klass
)
280 k
= build_java_array_type (klass
, -1);
285 vfy_get_component_type (vfy_jclass klass
)
288 gcc_assert (vfy_is_array (klass
));
289 k
= TYPE_ARRAY_ELEMENT (klass
);
290 if (TREE_CODE (k
) == POINTER_TYPE
)
296 vfy_is_abstract (vfy_jclass klass
)
298 return CLASS_ABSTRACT (TYPE_NAME (klass
));
302 vfy_find_class (vfy_jclass ignore ATTRIBUTE_UNUSED
, vfy_string name
)
306 k
= get_type_from_signature (name
);
307 if (TREE_CODE (k
) == POINTER_TYPE
)
314 vfy_object_type (void)
317 k
= object_type_node
;
322 vfy_class_type (void)
324 return class_type_node
;
328 vfy_string_type (void)
331 k
= string_type_node
;
336 vfy_throwable_type (void)
339 k
= throwable_type_node
;
344 vfy_unsuitable_type (void)
350 vfy_return_address_type (void)
352 return TYPE_RETURN_ADDR
;
362 vfy_class_has_field (vfy_jclass klass
, vfy_string name
,
363 vfy_string signature
)
365 tree field
= TYPE_FIELDS (klass
);
366 while (field
!= NULL_TREE
)
368 if (DECL_NAME (field
) == name
369 && build_java_signature (TREE_TYPE (field
)) == signature
)
371 field
= DECL_CHAIN (field
);
377 vfy_fail (const char *message
, int pc
, vfy_jclass ignore1 ATTRIBUTE_UNUSED
,
378 vfy_method
*ignore2 ATTRIBUTE_UNUSED
)
381 error ("verification failed: %s", message
);
383 error ("verification failed at PC=%d: %s", pc
, message
);
384 /* We have to return a value for the verifier to throw. */
389 vfy_get_primitive_type (int type
)
392 k
= decode_newarray_type (type
);
397 vfy_note_stack_depth (vfy_method
*method
, int pc
, int depth
)
399 tree val
= make_tree_vec (method
->max_locals
+ depth
);
400 (*type_states
)[pc
] = val
;
401 /* Called for side effects. */
406 vfy_note_stack_type (vfy_method
*method
, int pc
, int slot
, vfy_jclass type
)
410 slot
+= method
->max_locals
;
412 if (type
== object_type_node
)
413 type
= object_ptr_type_node
;
415 vec
= (*type_states
)[pc
];
416 TREE_VEC_ELT (vec
, slot
) = type
;
417 /* Called for side effects. */
422 vfy_note_local_type (vfy_method
*method ATTRIBUTE_UNUSED
, int pc
, int slot
,
427 if (type
== object_type_node
)
428 type
= object_ptr_type_node
;
430 vec
= (*type_states
)[pc
];
431 TREE_VEC_ELT (vec
, slot
) = type
;
432 /* Called for side effects. */
437 vfy_note_instruction_seen (int pc
)
439 instruction_bits
[pc
] |= BCODE_VERIFIED
;
442 /* Verify the bytecodes of the current method.
443 Return 1 on success, 0 on failure. */
445 verify_jvm_instructions_new (JCF
*jcf
, const unsigned char *byte_ops
,
449 int i
, result
, eh_count
;
450 vfy_exception
*exceptions
;
452 method_init_exceptions ();
454 JCF_SEEK (jcf
, DECL_CODE_OFFSET (current_function_decl
) + length
);
455 eh_count
= JCF_readu2 (jcf
);
457 exceptions
= (vfy_exception
*) xmalloc (eh_count
* sizeof (vfy_exception
));
458 for (i
= 0; i
< eh_count
; ++i
)
460 int start_pc
, end_pc
, handler_pc
, catch_type
;
461 unsigned char *p
= jcf
->read_ptr
+ 8 * i
;
462 start_pc
= GET_u2 (p
);
463 end_pc
= GET_u2 (p
+2);
464 handler_pc
= GET_u2 (p
+4);
465 catch_type
= GET_u2 (p
+6);
467 if (start_pc
< 0 || start_pc
>= length
468 || end_pc
< 0 || end_pc
> length
|| start_pc
>= end_pc
469 || handler_pc
< 0 || handler_pc
>= length
)
471 error ("bad pc in exception_table");
476 exceptions
[i
].handler
= handler_pc
;
477 exceptions
[i
].start
= start_pc
;
478 exceptions
[i
].end
= end_pc
;
479 exceptions
[i
].type
= catch_type
;
481 add_handler (start_pc
, end_pc
,
482 lookup_label (handler_pc
),
483 catch_type
== 0 ? NULL_TREE
484 : get_class_constant (jcf
, catch_type
));
485 instruction_bits
[handler_pc
] |= BCODE_EXCEPTION_TARGET
;
488 gcc_assert (sanity_check_exception_range (&whole_range
));
490 method
.method
= current_function_decl
;
491 method
.signature
= build_java_signature (TREE_TYPE (current_function_decl
));
492 method
.name
= DECL_NAME (current_function_decl
);
493 method
.bytes
= byte_ops
;
494 method
.exceptions
= exceptions
;
495 method
.defining_class
= DECL_CONTEXT (current_function_decl
);
496 method
.max_stack
= DECL_MAX_STACK (current_function_decl
);
497 method
.max_locals
= DECL_MAX_LOCALS (current_function_decl
);
498 method
.code_length
= length
;
499 method
.exc_count
= eh_count
;
501 result
= verify_method (&method
);