OSDN Git Service

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