OSDN Git Service

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