verify-glue.c 10 KB

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