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