]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/java/verify-glue.c
tree-core.h: Include symtab.h.
[thirdparty/gcc.git] / gcc / java / verify-glue.c
CommitLineData
36739040 1/* Glue to interface gcj with bytecode verifier.
5624e564 2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
36739040
TT
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8328d52a 8the Free Software Foundation; either version 3, or (at your option)
36739040
TT
9any later version.
10
11GCC is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
8328d52a
NC
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>.
36739040
TT
19
20Java and all Java-based marks are trademarks or registered trademarks
21of Sun Microsystems, Inc. in the United States and other countries.
22The Free Software Foundation is independent of Sun Microsystems, Inc. */
23
24/* Written by Tom Tromey <tromey@redhat.com>. */
25
26#include "config.h"
27
28#include "system.h"
29#include "coretypes.h"
40e23961 30#include "alias.h"
36739040 31#include "tree.h"
c7131fb2 32#include "options.h"
d8a2d370 33#include "stringpool.h"
36739040
TT
34#include "parse.h"
35
36#include "verify.h"
37#include "java-tree.h"
38#include "java-except.h"
718f9c0f 39#include "diagnostic-core.h"
36739040
TT
40
41void *
42vfy_alloc (size_t bytes)
43{
44 return xmalloc (bytes);
45}
46
47void
48vfy_free (void *mem)
49{
50 free (mem);
51}
52
53bool
54vfy_strings_equal (vfy_string one, vfy_string two)
55{
56 return one == two;
57}
58
59const char *
60vfy_string_bytes (vfy_string str)
61{
62 return IDENTIFIER_POINTER (str);
63}
64
65int
66vfy_string_length (vfy_string str)
67{
68 return IDENTIFIER_LENGTH (str);
69}
70
71vfy_string
eb9744a7 72vfy_init_name (void)
36739040
TT
73{
74 return init_identifier_node;
75}
76
77vfy_string
eb9744a7 78vfy_clinit_name (void)
36739040
TT
79{
80 return clinit_identifier_node;
81}
82
83static const char*
84skip_one_type (const char* ptr)
85{
86 int ch = *ptr++;
87
88 while (ch == '[')
89 {
90 ch = *ptr++;
91 }
92
93 if (ch == 'L')
94 {
95 do { ch = *ptr++; } while (ch != ';');
96 }
97
98 return ptr;
99}
100
101int
102vfy_count_arguments (vfy_string signature)
103{
104 const char *ptr = IDENTIFIER_POINTER (signature);
105 int arg_count = 0;
106
107 /* Skip '('. */
108 ptr++;
109
110 /* Count args. */
111 while (*ptr != ')')
112 {
113 ptr = skip_one_type (ptr);
114 arg_count += 1;
115 }
116
117 return arg_count;
118}
119
120vfy_string
121vfy_get_string (const char *s, int len)
122{
123 return get_identifier_with_length (s, len);
124}
125
126vfy_string
127vfy_get_signature (vfy_method *method)
128{
129 return method->signature;
130}
131
132vfy_string
133vfy_get_method_name (vfy_method *method)
134{
135 return method->name;
136}
137
138bool
139vfy_is_static (vfy_method *method)
140{
141 return METHOD_STATIC (method->method);
142}
143
144const unsigned char *
145vfy_get_bytecode (vfy_method *method)
146{
147 return method->bytes;
148}
149
150vfy_exception *
151vfy_get_exceptions (vfy_method *method)
152{
153 return method->exceptions;
154}
155
156void
157vfy_get_exception (vfy_exception *exceptions, int index, int *handler,
158 int *start, int *end, int *handler_type)
159{
160 *handler = exceptions[index].handler;
161 *start = exceptions[index].start;
162 *end = exceptions[index].end;
163 *handler_type = exceptions[index].type;
164}
165
166int
167vfy_tag (vfy_constants *pool, int index)
168{
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;
178}
179
180void
181vfy_load_indexes (vfy_constants *pool, int index,
182 vfy_uint_16 *index0, vfy_uint_16 *index1)
183{
184 *index0 = JPOOL_USHORT1 (pool, index);
185 *index1 = JPOOL_USHORT2 (pool, index);
186}
187
188vfy_constants *
189vfy_get_constants (vfy_jclass klass)
190{
191 return TYPE_JCF (klass);
192}
193
194int
195vfy_get_constants_size (vfy_jclass klass)
196{
197 return JPOOL_SIZE (TYPE_JCF (klass));
198}
199
200vfy_string
201vfy_get_pool_string (vfy_constants *pool, int index)
202{
203 return get_name_constant (pool, index);
204}
205
206vfy_jclass
207vfy_get_pool_class (vfy_constants *pool, int index)
208{
209 vfy_jclass k;
210 k = get_class_constant (pool, index);
211 return k;
212}
213
36739040
TT
214vfy_string
215vfy_get_class_name (vfy_jclass klass)
216{
217 return DECL_NAME (TYPE_NAME (klass));
218}
219
220bool
221vfy_is_assignable_from (vfy_jclass target, vfy_jclass source)
222{
36739040
TT
223 /* Any class is always assignable to itself, or java.lang.Object. */
224 if (source == target || target == object_type_node)
225 return true;
226
a2da2c9a
BM
227 /* For the C++ ABI, perform this test statically. */
228 if (! flag_indirect_dispatch)
229 return can_widen_reference_to (source, target);
230
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. */
234
36739040
TT
235 add_type_assertion (current_class, JV_ASSERT_TYPES_COMPATIBLE, source,
236 target);
237 return true;
238}
239
240char
241vfy_get_primitive_char (vfy_jclass klass)
242{
243 tree sig;
ab184b2a 244 gcc_assert (vfy_is_primitive (klass));
36739040
TT
245 sig = build_java_signature (klass);
246 return (IDENTIFIER_POINTER (sig))[0];
247}
248
36739040
TT
249bool
250vfy_is_array (vfy_jclass klass)
251{
252 return TYPE_ARRAY_P (klass);
253}
254
255bool
256vfy_is_interface (vfy_jclass klass)
257{
258 return CLASS_INTERFACE (TYPE_NAME (klass));
259}
260
261bool
262vfy_is_primitive (vfy_jclass klass)
263{
264 return JPRIMITIVE_TYPE_P (klass);
265}
266
267vfy_jclass
268vfy_get_superclass (vfy_jclass klass)
269{
270 vfy_jclass k;
271 k = CLASSTYPE_SUPER (klass);
272 return k;
273}
274
275vfy_jclass
276vfy_get_array_class (vfy_jclass klass)
277{
278 vfy_jclass k;
279 k = build_java_array_type (klass, -1);
280 return k;
281}
282
283vfy_jclass
284vfy_get_component_type (vfy_jclass klass)
285{
286 vfy_jclass k;
ab184b2a 287 gcc_assert (vfy_is_array (klass));
36739040
TT
288 k = TYPE_ARRAY_ELEMENT (klass);
289 if (TREE_CODE (k) == POINTER_TYPE)
290 k = TREE_TYPE (k);
291 return k;
292}
293
294bool
295vfy_is_abstract (vfy_jclass klass)
296{
297 return CLASS_ABSTRACT (TYPE_NAME (klass));
298}
299
300vfy_jclass
301vfy_find_class (vfy_jclass ignore ATTRIBUTE_UNUSED, vfy_string name)
302{
303 vfy_jclass k;
304
305 k = get_type_from_signature (name);
306 if (TREE_CODE (k) == POINTER_TYPE)
307 k = TREE_TYPE (k);
308
309 return k;
310}
311
312vfy_jclass
eb9744a7 313vfy_object_type (void)
36739040
TT
314{
315 vfy_jclass k;
316 k = object_type_node;
317 return k;
318}
319
153d08d5
TT
320vfy_jclass
321vfy_class_type (void)
322{
323 return class_type_node;
324}
325
36739040 326vfy_jclass
eb9744a7 327vfy_string_type (void)
36739040
TT
328{
329 vfy_jclass k;
330 k = string_type_node;
331 return k;
332}
333
334vfy_jclass
eb9744a7 335vfy_throwable_type (void)
36739040
TT
336{
337 vfy_jclass k;
338 k = throwable_type_node;
339 return k;
340}
341
342vfy_jclass
343vfy_unsuitable_type (void)
344{
345 return TYPE_SECOND;
346}
347
348vfy_jclass
349vfy_return_address_type (void)
350{
351 return TYPE_RETURN_ADDR;
352}
353
354vfy_jclass
355vfy_null_type (void)
356{
357 return TYPE_NULL;
358}
359
1870a43b
TT
360bool
361vfy_class_has_field (vfy_jclass klass, vfy_string name,
362 vfy_string signature)
363{
364 tree field = TYPE_FIELDS (klass);
365 while (field != NULL_TREE)
366 {
367 if (DECL_NAME (field) == name
368 && build_java_signature (TREE_TYPE (field)) == signature)
369 return true;
910ad8de 370 field = DECL_CHAIN (field);
1870a43b
TT
371 }
372 return false;
373}
374
36739040
TT
375int
376vfy_fail (const char *message, int pc, vfy_jclass ignore1 ATTRIBUTE_UNUSED,
377 vfy_method *ignore2 ATTRIBUTE_UNUSED)
378{
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;
385}
386
387vfy_jclass
388vfy_get_primitive_type (int type)
389{
390 vfy_jclass k;
391 k = decode_newarray_type (type);
392 return k;
393}
394
395void
396vfy_note_stack_depth (vfy_method *method, int pc, int depth)
397{
3d4e2766 398 tree val = make_tree_vec (method->max_locals + depth);
9771b263 399 (*type_states)[pc] = val;
3d4e2766
TT
400 /* Called for side effects. */
401 lookup_label (pc);
36739040
TT
402}
403
404void
405vfy_note_stack_type (vfy_method *method, int pc, int slot, vfy_jclass type)
406{
3d4e2766 407 tree vec;
36739040
TT
408
409 slot += method->max_locals;
410
411 if (type == object_type_node)
412 type = object_ptr_type_node;
413
9771b263 414 vec = (*type_states)[pc];
36739040 415 TREE_VEC_ELT (vec, slot) = type;
3d4e2766
TT
416 /* Called for side effects. */
417 lookup_label (pc);
36739040
TT
418}
419
420void
421vfy_note_local_type (vfy_method *method ATTRIBUTE_UNUSED, int pc, int slot,
422 vfy_jclass type)
423{
3d4e2766 424 tree vec;
36739040
TT
425
426 if (type == object_type_node)
427 type = object_ptr_type_node;
428
9771b263 429 vec = (*type_states)[pc];
36739040 430 TREE_VEC_ELT (vec, slot) = type;
3d4e2766
TT
431 /* Called for side effects. */
432 lookup_label (pc);
36739040
TT
433}
434
435void
436vfy_note_instruction_seen (int pc)
437{
438 instruction_bits[pc] |= BCODE_VERIFIED;
439}
440
441/* Verify the bytecodes of the current method.
442 Return 1 on success, 0 on failure. */
443int
444verify_jvm_instructions_new (JCF *jcf, const unsigned char *byte_ops,
445 long length)
446{
447 vfy_method method;
448 int i, result, eh_count;
449 vfy_exception *exceptions;
450
451 method_init_exceptions ();
452
453 JCF_SEEK (jcf, DECL_CODE_OFFSET (current_function_decl) + length);
454 eh_count = JCF_readu2 (jcf);
455
456 exceptions = (vfy_exception *) xmalloc (eh_count * sizeof (vfy_exception));
457 for (i = 0; i < eh_count; ++i)
458 {
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);
465
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)
469 {
470 error ("bad pc in exception_table");
471 free (exceptions);
472 return 0;
473 }
474
475 exceptions[i].handler = handler_pc;
476 exceptions[i].start = start_pc;
477 exceptions[i].end = end_pc;
478 exceptions[i].type = catch_type;
479
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;
485 }
486
d393cb72 487 gcc_assert (sanity_check_exception_range (&whole_range));
36739040
TT
488
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;
499
500 result = verify_method (&method);
501
502 free (exceptions);
503
504 return result;
505}