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