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