defineclass.cc 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  1. // defineclass.cc - defining a class from .class format.
  2. /* Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. /*
  8. Author: Kresten Krab Thorup <krab@gnu.org>
  9. Written using the online versions of Java Language Specification (1st
  10. ed.) and The Java Virtual Machine Specification (2nd ed.).
  11. Future work may include reading (and handling) attributes which are
  12. currently being ignored ("InnerClasses", "LineNumber", etc...).
  13. */
  14. #include <config.h>
  15. #include <java-interp.h>
  16. #include <stdlib.h>
  17. #include <java-cpool.h>
  18. #include <gcj/cni.h>
  19. #include <java/lang/Class.h>
  20. #include <java/lang/Float.h>
  21. #include <java/lang/Double.h>
  22. #include <java/lang/Character.h>
  23. #include <java/lang/LinkageError.h>
  24. #include <java/lang/InternalError.h>
  25. #include <java/lang/ClassFormatError.h>
  26. #include <java/lang/NoClassDefFoundError.h>
  27. #include <java/lang/ClassCircularityError.h>
  28. #include <java/lang/IncompatibleClassChangeError.h>
  29. #include <java/lang/reflect/Modifier.h>
  30. using namespace gcj;
  31. #ifdef INTERPRETER
  32. // these go in some separate functions, to avoid having _Jv_InitClass
  33. // inserted all over the place.
  34. static void throw_internal_error (char *msg)
  35. __attribute__ ((__noreturn__));
  36. static void throw_no_class_def_found_error (jstring msg)
  37. __attribute__ ((__noreturn__));
  38. static void throw_no_class_def_found_error (char *msg)
  39. __attribute__ ((__noreturn__));
  40. static void throw_class_format_error (jstring msg)
  41. __attribute__ ((__noreturn__));
  42. static void throw_incompatible_class_change_error (jstring msg)
  43. __attribute__ ((__noreturn__));
  44. static void throw_class_circularity_error (jstring msg)
  45. __attribute__ ((__noreturn__));
  46. /**
  47. * We define class reading using a class. It is practical, since then
  48. * the entire class-reader can be a friend of class Class (it needs to
  49. * write all it's different structures); but also because this makes it
  50. * easy to make class definition reentrant, and thus two threads can be
  51. * defining classes at the same time. This class (_Jv_ClassReader) is
  52. * never exposed outside this file, so we don't have to worry about
  53. * public or private members here.
  54. */
  55. struct _Jv_ClassReader {
  56. // do verification? Currently, there is no option to disable this.
  57. // This flag just controls the verificaiton done by the class loader;
  58. // i.e., checking the integrity of the constant pool; and it is
  59. // allways on. You always want this as far as I can see, but it also
  60. // controls weither identifiers and type descriptors/signatures are
  61. // verified as legal. This could be somewhat more expensive since it
  62. // will call Characher.isJavaIdentifier{Start,Part} for each character
  63. // in any identifier (field name or method name) it comes by. Thus,
  64. // it might be useful to turn off this verification for classes that
  65. // come from a trusted source. However, for GCJ, trusted classes are
  66. // most likely to be linked in.
  67. bool verify;
  68. // input data.
  69. unsigned char *bytes;
  70. int len;
  71. // current input position
  72. int pos;
  73. // the constant pool data
  74. int pool_count;
  75. unsigned char *tags;
  76. unsigned int *offsets;
  77. // the class to define (see java-interp.h)
  78. _Jv_InterpClass *def;
  79. /* check that the given number of input bytes are available */
  80. inline void check (int num)
  81. {
  82. if (pos + num > len)
  83. throw_class_format_error ("Premature end of data");
  84. }
  85. /* skip a given number of bytes in input */
  86. inline void skip (int num)
  87. {
  88. check (num);
  89. pos += num;
  90. }
  91. /* read an unsignend 1-byte unit */
  92. inline static jint get1u (unsigned char* bytes)
  93. {
  94. return bytes[0];
  95. }
  96. /* read an unsigned 1-byte unit */
  97. inline jint read1u ()
  98. {
  99. skip (1);
  100. return get1u (bytes+pos-1);
  101. }
  102. /* read an unsigned 2-byte unit */
  103. inline static jint get2u (unsigned char *bytes)
  104. {
  105. return (((jint)bytes[0]) << 8) | ((jint)bytes[1]);
  106. }
  107. /* read an unsigned 2-byte unit */
  108. inline jint read2u ()
  109. {
  110. skip (2);
  111. return get2u (bytes+pos-2);
  112. }
  113. /* read a 4-byte unit */
  114. static jint get4 (unsigned char *bytes)
  115. {
  116. return (((jint)bytes[0]) << 24)
  117. | (((jint)bytes[1]) << 16)
  118. | (((jint)bytes[2]) << 8)
  119. | (((jint)bytes[3]) << 0);
  120. }
  121. /* read a 4-byte unit, (we don't do that quite so often) */
  122. inline jint read4 ()
  123. {
  124. skip (4);
  125. return get4 (bytes+pos-4);
  126. }
  127. /* read a 8-byte unit */
  128. static jlong get8 (unsigned char* bytes)
  129. {
  130. return (((jlong)bytes[0]) << 56)
  131. | (((jlong)bytes[1]) << 48)
  132. | (((jlong)bytes[2]) << 40)
  133. | (((jlong)bytes[3]) << 32)
  134. | (((jlong)bytes[4]) << 24)
  135. | (((jlong)bytes[5]) << 16)
  136. | (((jlong)bytes[6]) << 8)
  137. | (((jlong)bytes[7]) << 0);
  138. }
  139. /* read a 8-byte unit */
  140. inline jlong read8 ()
  141. {
  142. skip (8);
  143. return get8 (bytes+pos-8);
  144. }
  145. inline void check_tag (int index, char expected_tag)
  146. {
  147. if (index < 0
  148. || index > pool_count
  149. || tags[index] != expected_tag)
  150. throw_class_format_error ("erroneous constant pool tag");
  151. }
  152. inline void verify_identifier (_Jv_Utf8Const* name)
  153. {
  154. if (! _Jv_VerifyIdentifier (name))
  155. throw_class_format_error ("erroneous identifier");
  156. }
  157. inline void verify_classname (unsigned char* ptr, _Jv_ushort length)
  158. {
  159. if (! _Jv_VerifyClassName (ptr, length))
  160. throw_class_format_error ("erroneous class name");
  161. }
  162. inline void verify_classname (_Jv_Utf8Const *name)
  163. {
  164. if (! _Jv_VerifyClassName (name))
  165. throw_class_format_error ("erroneous class name");
  166. }
  167. inline void verify_field_signature (_Jv_Utf8Const *sig)
  168. {
  169. if (! _Jv_VerifyFieldSignature (sig))
  170. throw_class_format_error ("erroneous type descriptor");
  171. }
  172. inline void verify_method_signature (_Jv_Utf8Const *sig)
  173. {
  174. if (! _Jv_VerifyMethodSignature (sig))
  175. throw_class_format_error ("erroneous type descriptor");
  176. }
  177. _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length)
  178. {
  179. if (klass == 0 || length < 0 || offset+length > data->length)
  180. throw_internal_error ("arguments to _Jv_DefineClass");
  181. verify = true;
  182. bytes = (unsigned char*) (elements (data)+offset);
  183. len = length;
  184. pos = 0;
  185. def = (_Jv_InterpClass*) klass;
  186. }
  187. /** and here goes the parser members defined out-of-line */
  188. void parse ();
  189. void read_constpool ();
  190. void prepare_pool_entry (int index, unsigned char tag);
  191. void read_fields ();
  192. void read_methods ();
  193. void read_one_class_attribute ();
  194. void read_one_method_attribute (int method);
  195. void read_one_code_attribute (int method);
  196. void read_one_field_attribute (int field);
  197. void throw_class_format_error (char *msg);
  198. /** check an utf8 entry, without creating a Utf8Const object */
  199. bool is_attribute_name (int index, char *name);
  200. /** here goes the class-loader members defined out-of-line */
  201. void handleConstantPool ();
  202. void handleClassBegin (int, int, int);
  203. void handleInterfacesBegin (int);
  204. void handleInterface (int, int);
  205. void handleFieldsBegin (int);
  206. void handleField (int, int, int, int);
  207. void handleFieldsEnd ();
  208. void handleConstantValueAttribute (int,int);
  209. void handleMethodsBegin (int);
  210. void handleMethod (int, int, int, int);
  211. void handleMethodsEnd ();
  212. void handleCodeAttribute (int, int, int, int, int, int);
  213. void handleExceptionTableEntry (int, int, int, int, int, int);
  214. void checkExtends (jclass sub, jclass super);
  215. void checkImplements (jclass sub, jclass super);
  216. /*
  217. * FIXME: we should keep a hash table of utf8-strings, since many will
  218. * be the same. It's a little tricky, however, because the hash table
  219. * needs to interact gracefully with the garbage collector. Much
  220. * memory is to be saved by this, however! perhaps the improvement
  221. * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
  222. * computes the hash value anyway.
  223. */
  224. };
  225. void
  226. _Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length)
  227. {
  228. _Jv_ClassReader reader (klass, data, offset, length);
  229. reader.parse();
  230. /* that's it! */
  231. }
  232. /** This section defines the parsing/scanning of the class data */
  233. void
  234. _Jv_ClassReader::parse ()
  235. {
  236. int magic = read4 ();
  237. /* FIXME: Decide which range of version numbers to allow */
  238. /* int minor_version = */ read2u ();
  239. /* int major_verson = */ read2u ();
  240. if (magic != (int) 0xCAFEBABE)
  241. throw_class_format_error ("bad magic number");
  242. pool_count = read2u ();
  243. read_constpool ();
  244. int access_flags = read2u ();
  245. int this_class = read2u ();
  246. int super_class = read2u ();
  247. check_tag (this_class, JV_CONSTANT_Class);
  248. if (super_class != 0)
  249. check_tag (super_class, JV_CONSTANT_Class);
  250. handleClassBegin (access_flags, this_class, super_class);
  251. int interfaces_count = read2u ();
  252. handleInterfacesBegin (interfaces_count);
  253. for (int i = 0; i < interfaces_count; i++)
  254. {
  255. int iface = read2u ();
  256. check_tag (iface, JV_CONSTANT_Class);
  257. handleInterface (i, iface);
  258. }
  259. read_fields ();
  260. read_methods ();
  261. int attributes_count = read2u ();
  262. for (int i = 0; i < attributes_count; i++)
  263. {
  264. read_one_class_attribute ();
  265. }
  266. if (pos != len)
  267. throw_class_format_error ("unused data before end of file");
  268. // tell everyone we're done.
  269. def->state = JV_STATE_LOADED;
  270. def->notifyAll ();
  271. }
  272. void _Jv_ClassReader::read_constpool ()
  273. {
  274. tags = (unsigned char*) _Jv_AllocBytes (pool_count);
  275. offsets = (unsigned int *) _Jv_AllocBytes (sizeof (int)
  276. * pool_count) ;
  277. /** first, we scan the constant pool, collecting tags and offsets */
  278. tags[0] = JV_CONSTANT_Undefined;
  279. offsets[0] = pos;
  280. for (int c = 1; c < pool_count; c++)
  281. {
  282. tags[c] = read1u ();
  283. offsets[c] = pos;
  284. switch (tags[c])
  285. {
  286. case JV_CONSTANT_String:
  287. case JV_CONSTANT_Class:
  288. skip (2);
  289. break;
  290. case JV_CONSTANT_Fieldref:
  291. case JV_CONSTANT_Methodref:
  292. case JV_CONSTANT_InterfaceMethodref:
  293. case JV_CONSTANT_NameAndType:
  294. case JV_CONSTANT_Integer:
  295. case JV_CONSTANT_Float:
  296. skip (4);
  297. break;
  298. case JV_CONSTANT_Double:
  299. case JV_CONSTANT_Long:
  300. skip (8);
  301. tags[++c] = JV_CONSTANT_Undefined;
  302. break;
  303. case JV_CONSTANT_Utf8:
  304. {
  305. int len = read2u ();
  306. skip (len);
  307. }
  308. break;
  309. case JV_CONSTANT_Unicode:
  310. throw_class_format_error ("unicode not supported");
  311. break;
  312. default:
  313. throw_class_format_error ("erroneous constant pool tag");
  314. }
  315. }
  316. handleConstantPool ();
  317. }
  318. void _Jv_ClassReader::read_fields ()
  319. {
  320. int fields_count = read2u ();
  321. handleFieldsBegin (fields_count);
  322. for (int i = 0; i < fields_count; i++)
  323. {
  324. int access_flags = read2u ();
  325. int name_index = read2u ();
  326. int descriptor_index = read2u ();
  327. int attributes_count = read2u ();
  328. check_tag (name_index, JV_CONSTANT_Utf8);
  329. prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
  330. check_tag (descriptor_index, JV_CONSTANT_Utf8);
  331. prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
  332. handleField (i, access_flags, name_index, descriptor_index);
  333. for (int j = 0; j < attributes_count; j++)
  334. {
  335. read_one_field_attribute (i);
  336. }
  337. }
  338. handleFieldsEnd ();
  339. }
  340. bool
  341. _Jv_ClassReader::is_attribute_name (int index, char *name)
  342. {
  343. check_tag (index, JV_CONSTANT_Utf8);
  344. int len = get2u (bytes+offsets[index]);
  345. if (len != (int) strlen (name))
  346. return false;
  347. else
  348. return !memcmp (bytes+offsets[index]+2, name, len);
  349. }
  350. void _Jv_ClassReader::read_one_field_attribute (int field_index)
  351. {
  352. int name = read2u ();
  353. int length = read4 ();
  354. if (is_attribute_name (name, "ConstantValue"))
  355. {
  356. int cv = read2u ();
  357. if (cv < pool_count
  358. && cv > 0
  359. && (tags[cv] == JV_CONSTANT_Integer
  360. || tags[cv] == JV_CONSTANT_Float
  361. || tags[cv] == JV_CONSTANT_Long
  362. || tags[cv] == JV_CONSTANT_Double
  363. || tags[cv] == JV_CONSTANT_String))
  364. {
  365. handleConstantValueAttribute (field_index, cv);
  366. }
  367. else
  368. {
  369. throw_class_format_error ("erroneous ConstantValue attribute");
  370. }
  371. if (length != 2)
  372. throw_class_format_error ("erroneous ConstantValue attribute");
  373. }
  374. else
  375. {
  376. skip (length);
  377. }
  378. }
  379. void _Jv_ClassReader::read_methods ()
  380. {
  381. int methods_count = read2u ();
  382. handleMethodsBegin (methods_count);
  383. for (int i = 0; i < methods_count; i++)
  384. {
  385. int access_flags = read2u ();
  386. int name_index = read2u ();
  387. int descriptor_index = read2u ();
  388. int attributes_count = read2u ();
  389. check_tag (name_index, JV_CONSTANT_Utf8);
  390. prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
  391. check_tag (name_index, JV_CONSTANT_Utf8);
  392. prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
  393. handleMethod (i, access_flags, name_index,
  394. descriptor_index);
  395. for (int j = 0; j < attributes_count; j++)
  396. {
  397. read_one_method_attribute (i);
  398. }
  399. }
  400. handleMethodsEnd ();
  401. }
  402. void _Jv_ClassReader::read_one_method_attribute (int method_index)
  403. {
  404. int name = read2u ();
  405. int length = read4 ();
  406. if (is_attribute_name (name, "Exceptions"))
  407. {
  408. _Jv_Method *method = reinterpret_cast<_Jv_Method *>
  409. (&def->methods[method_index]);
  410. if (method->throws != NULL)
  411. throw_class_format_error ("only one Exceptions attribute allowed per method");
  412. int num_exceptions = read2u ();
  413. // We use malloc here because the GC won't scan the method
  414. // objects. FIXME this means a memory leak if we GC a class.
  415. // (Currently we never do.)
  416. _Jv_Utf8Const **exceptions =
  417. (_Jv_Utf8Const **) _Jv_Malloc ((num_exceptions + 1) * sizeof (_Jv_Utf8Const *));
  418. int out = 0;
  419. _Jv_word *pool_data = def->constants.data;
  420. for (int i = 0; i < num_exceptions; ++i)
  421. {
  422. try
  423. {
  424. int ndx = read2u ();
  425. // JLS 2nd Ed. 4.7.5 requires that the tag not be 0.
  426. if (ndx != 0)
  427. {
  428. check_tag (ndx, JV_CONSTANT_Class);
  429. exceptions[out++] = pool_data[ndx].utf8;
  430. }
  431. }
  432. catch (java::lang::Throwable *exc)
  433. {
  434. _Jv_Free (exceptions);
  435. throw exc;
  436. }
  437. }
  438. exceptions[out] = NULL;
  439. method->throws = exceptions;
  440. }
  441. else if (is_attribute_name (name, "Code"))
  442. {
  443. int start_off = pos;
  444. int max_stack = read2u ();
  445. int max_locals = read2u ();
  446. int code_length = read4 ();
  447. int code_start = pos;
  448. skip (code_length);
  449. int exception_table_length = read2u ();
  450. handleCodeAttribute (method_index,
  451. max_stack, max_locals,
  452. code_start, code_length,
  453. exception_table_length);
  454. for (int i = 0; i < exception_table_length; i++)
  455. {
  456. int start_pc = read2u ();
  457. int end_pc = read2u ();
  458. int handler_pc = read2u ();
  459. int catch_type = read2u ();
  460. if (start_pc > end_pc
  461. || start_pc < 0
  462. // END_PC can be equal to CODE_LENGTH.
  463. // See JVM Spec 4.7.4.
  464. || end_pc > code_length
  465. || handler_pc >= code_length)
  466. throw_class_format_error ("erroneous exception handler info");
  467. if (! (tags[catch_type] == JV_CONSTANT_Class
  468. || tags[catch_type] == 0))
  469. {
  470. throw_class_format_error ("erroneous exception handler info");
  471. }
  472. handleExceptionTableEntry (method_index,
  473. i,
  474. start_pc,
  475. end_pc,
  476. handler_pc,
  477. catch_type);
  478. }
  479. int attributes_count = read2u ();
  480. for (int i = 0; i < attributes_count; i++)
  481. {
  482. read_one_code_attribute (method_index);
  483. }
  484. if ((pos - start_off) != length)
  485. throw_class_format_error ("code attribute too short");
  486. }
  487. else
  488. {
  489. /* ignore unknown attributes */
  490. skip (length);
  491. }
  492. }
  493. void _Jv_ClassReader::read_one_code_attribute (int /*method*/)
  494. {
  495. /* ignore for now, ... later we may want to pick up
  496. line number information, for debugging purposes;
  497. in fact, the whole debugger issue is open! */
  498. /* int name = */ read2u ();
  499. int length = read4 ();
  500. skip (length);
  501. }
  502. void _Jv_ClassReader::read_one_class_attribute ()
  503. {
  504. /* we also ignore the class attributes, ...
  505. some day we'll add inner-classes support. */
  506. /* int name = */ read2u ();
  507. int length = read4 ();
  508. skip (length);
  509. }
  510. /* this section defines the semantic actions of the parser */
  511. void _Jv_ClassReader::handleConstantPool ()
  512. {
  513. /** now, we actually define the class' constant pool */
  514. // the pool is scanned explicitly by the collector
  515. jbyte *pool_tags = (jbyte*) _Jv_AllocBytes (pool_count);
  516. _Jv_word *pool_data
  517. = (_Jv_word*) _Jv_AllocBytes (pool_count * sizeof (_Jv_word));
  518. def->constants.tags = pool_tags;
  519. def->constants.data = pool_data;
  520. def->constants.size = pool_count;
  521. // Here we make a pass to collect the strings! We do this, because
  522. // internally in the GCJ runtime, classes are encoded with .'s not /'s.
  523. // Therefore, we first collect the strings, and then translate the rest
  524. // of the utf8-entries (thus not representing strings) from /-notation
  525. // to .-notation.
  526. for (int i = 1; i < pool_count; i++)
  527. {
  528. if (tags[i] == JV_CONSTANT_String)
  529. {
  530. unsigned char* str_data = bytes + offsets [i];
  531. int utf_index = get2u (str_data);
  532. check_tag (utf_index, JV_CONSTANT_Utf8);
  533. unsigned char *utf_data = bytes + offsets[utf_index];
  534. int len = get2u (utf_data);
  535. pool_data[i].utf8 = _Jv_makeUtf8Const ((char*)(utf_data+2), len);
  536. pool_tags[i] = JV_CONSTANT_String;
  537. }
  538. else
  539. {
  540. pool_tags[i] = JV_CONSTANT_Undefined;
  541. }
  542. }
  543. // and now, we scan everything else but strings & utf8-entries. This
  544. // leaves out those utf8-entries which are not used; which will be left
  545. // with a tag of JV_CONSTANT_Undefined in the class definition.
  546. for (int index = 1; index < pool_count; index++)
  547. {
  548. switch (tags[index])
  549. {
  550. case JV_CONSTANT_Undefined:
  551. case JV_CONSTANT_String:
  552. case JV_CONSTANT_Utf8:
  553. continue;
  554. default:
  555. prepare_pool_entry (index, tags[index]);
  556. }
  557. }
  558. }
  559. /* this is a recursive procedure, which will prepare pool entries as needed.
  560. Which is how we avoid initializing those entries which go unused. */
  561. void
  562. _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag)
  563. {
  564. /* these two, pool_data and pool_tags, point into the class
  565. structure we are currently defining */
  566. unsigned char *pool_tags = (unsigned char*) def->constants.tags;
  567. _Jv_word *pool_data = def->constants.data;
  568. /* this entry was already prepared */
  569. if (pool_tags[index] == this_tag)
  570. return;
  571. /* this_data points to the constant-pool information for the current
  572. constant-pool entry */
  573. unsigned char *this_data = bytes + offsets[index];
  574. switch (this_tag)
  575. {
  576. case JV_CONSTANT_Utf8:
  577. {
  578. // If we came here, it is because some other tag needs this
  579. // utf8-entry for type information! Thus, we translate /'s to .'s in
  580. // order to accomondate gcj's internal representation.
  581. int len = get2u (this_data);
  582. char *buffer = (char*) __builtin_alloca (len);
  583. char *s = ((char*) this_data)+2;
  584. /* FIXME: avoid using a buffer here */
  585. for (int i = 0; i < len; i++)
  586. {
  587. if (s[i] == '/')
  588. buffer[i] = '.';
  589. else
  590. buffer[i] = (char) s[i];
  591. }
  592. pool_data[index].utf8 = _Jv_makeUtf8Const (buffer, len);
  593. pool_tags[index] = JV_CONSTANT_Utf8;
  594. }
  595. break;
  596. case JV_CONSTANT_Class:
  597. {
  598. int utf_index = get2u (this_data);
  599. check_tag (utf_index, JV_CONSTANT_Utf8);
  600. prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
  601. if (verify)
  602. verify_classname (pool_data[utf_index].utf8);
  603. pool_data[index].utf8 = pool_data[utf_index].utf8;
  604. pool_tags[index] = JV_CONSTANT_Class;
  605. }
  606. break;
  607. case JV_CONSTANT_String:
  608. // already handled before...
  609. break;
  610. case JV_CONSTANT_Fieldref:
  611. case JV_CONSTANT_Methodref:
  612. case JV_CONSTANT_InterfaceMethodref:
  613. {
  614. int class_index = get2u (this_data);
  615. int nat_index = get2u (this_data+2);
  616. check_tag (class_index, JV_CONSTANT_Class);
  617. prepare_pool_entry (class_index, JV_CONSTANT_Class);
  618. check_tag (nat_index, JV_CONSTANT_NameAndType);
  619. prepare_pool_entry (nat_index, JV_CONSTANT_NameAndType);
  620. // here, verify the signature and identifier name
  621. if (verify)
  622. {
  623. _Jv_ushort name_index, type_index;
  624. _Jv_loadIndexes (&pool_data[nat_index],
  625. name_index, type_index);
  626. if (this_tag == JV_CONSTANT_Fieldref)
  627. _Jv_VerifyFieldSignature (pool_data[type_index].utf8);
  628. else
  629. _Jv_VerifyMethodSignature (pool_data[type_index].utf8);
  630. _Jv_Utf8Const* name = pool_data[name_index].utf8;
  631. if (this_tag != JV_CONSTANT_Fieldref
  632. && ( _Jv_equalUtf8Consts (name, clinit_name)
  633. || _Jv_equalUtf8Consts (name, init_name)))
  634. /* ignore */;
  635. else
  636. verify_identifier (pool_data[name_index].utf8);
  637. }
  638. _Jv_storeIndexes (&pool_data[index], class_index, nat_index);
  639. pool_tags[index] = this_tag;
  640. }
  641. break;
  642. case JV_CONSTANT_NameAndType:
  643. {
  644. _Jv_ushort name_index = get2u (this_data);
  645. _Jv_ushort type_index = get2u (this_data+2);
  646. check_tag (name_index, JV_CONSTANT_Utf8);
  647. prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
  648. check_tag (type_index, JV_CONSTANT_Utf8);
  649. prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
  650. _Jv_storeIndexes (&pool_data[index], name_index, type_index);
  651. pool_tags[index] = JV_CONSTANT_NameAndType;
  652. }
  653. break;
  654. case JV_CONSTANT_Float:
  655. {
  656. jfloat f = java::lang::Float::intBitsToFloat ((jint) get4 (this_data));
  657. _Jv_storeFloat (&pool_data[index], f);
  658. pool_tags[index] = JV_CONSTANT_Float;
  659. }
  660. break;
  661. case JV_CONSTANT_Integer:
  662. {
  663. int i = get4 (this_data);
  664. _Jv_storeInt (&pool_data[index], i);
  665. pool_tags[index] = JV_CONSTANT_Integer;
  666. }
  667. break;
  668. case JV_CONSTANT_Double:
  669. {
  670. jdouble d
  671. = java::lang::Double::longBitsToDouble ((jlong) get8 (this_data));
  672. _Jv_storeDouble (&pool_data[index], d);
  673. pool_tags[index] = JV_CONSTANT_Double;
  674. }
  675. break;
  676. case JV_CONSTANT_Long:
  677. {
  678. jlong i = get8 (this_data);
  679. _Jv_storeLong (&pool_data[index], i);
  680. pool_tags[index] = JV_CONSTANT_Long;
  681. }
  682. break;
  683. default:
  684. throw_class_format_error ("erroneous constant pool tag");
  685. }
  686. }
  687. void
  688. _Jv_ClassReader::handleClassBegin
  689. (int access_flags, int this_class, int super_class)
  690. {
  691. using namespace java::lang::reflect;
  692. unsigned char *pool_tags = (unsigned char*) def->constants.tags;
  693. _Jv_word *pool_data = def->constants.data;
  694. check_tag (this_class, JV_CONSTANT_Class);
  695. _Jv_Utf8Const *loadedName = pool_data[this_class].utf8;
  696. // was ClassLoader.defineClass called with an expected class name?
  697. if (def->name == 0)
  698. {
  699. jclass orig = _Jv_FindClassInCache (loadedName, def->loader);
  700. if (orig == 0)
  701. {
  702. def->name = loadedName;
  703. }
  704. else
  705. {
  706. jstring msg = JvNewStringUTF ("anonymous "
  707. "class data denotes "
  708. "existing class ");
  709. msg = msg->concat (orig->getName ());
  710. throw_no_class_def_found_error (msg);
  711. }
  712. }
  713. // assert that the loaded class has the expected name, 5.3.5
  714. else if (! _Jv_equalUtf8Consts (loadedName, def->name))
  715. {
  716. jstring msg = JvNewStringUTF ("loaded class ");
  717. msg = msg->concat (def->getName ());
  718. msg = msg->concat (_Jv_NewStringUTF (" was in fact named "));
  719. jstring klass_name = _Jv_NewStringUTF (loadedName->data);
  720. msg = msg->concat (klass_name);
  721. throw_no_class_def_found_error (msg);
  722. }
  723. def->accflags = access_flags | java::lang::reflect::Modifier::INTERPRETED;
  724. pool_data[this_class].clazz = def;
  725. pool_tags[this_class] = JV_CONSTANT_ResolvedClass;
  726. if (super_class == 0 && ! (access_flags & Modifier::INTERFACE))
  727. {
  728. // FIXME: Consider this carefully!
  729. if (! _Jv_equalUtf8Consts (def->name, java::lang::Object::class$.name))
  730. throw_no_class_def_found_error ("loading java.lang.Object");
  731. }
  732. // In the pre-loading state, it can be looked up in the
  733. // cache only by this thread! This allows the super-class
  734. // to include references to this class.
  735. def->state = JV_STATE_PRELOADING;
  736. {
  737. JvSynchronize sync (&java::lang::Class::class$);
  738. _Jv_RegisterClass (def);
  739. }
  740. if (super_class != 0)
  741. {
  742. // Load the superclass.
  743. check_tag (super_class, JV_CONSTANT_Class);
  744. _Jv_Utf8Const* super_name = pool_data[super_class].utf8;
  745. // Load the superclass using our defining loader.
  746. jclass the_super = _Jv_FindClass (super_name,
  747. def->loader);
  748. // This will establish that we are allowed to be a subclass,
  749. // and check for class circularity error.
  750. checkExtends (def, the_super);
  751. // Note: for an interface we will find Object as the
  752. // superclass. We still check it above to ensure class file
  753. // validity, but we simply assign `null' to the actual field in
  754. // this case.
  755. def->superclass = (((access_flags & Modifier::INTERFACE))
  756. ? NULL : the_super);
  757. pool_data[super_class].clazz = the_super;
  758. pool_tags[super_class] = JV_CONSTANT_ResolvedClass;
  759. }
  760. // Now we've come past the circularity problem, we can
  761. // now say that we're loading.
  762. def->state = JV_STATE_LOADING;
  763. def->notifyAll ();
  764. }
  765. ///// implements the checks described in sect. 5.3.5.3
  766. void
  767. _Jv_ClassReader::checkExtends (jclass sub, jclass super)
  768. {
  769. using namespace java::lang::reflect;
  770. // having an interface or a final class as a superclass is no good
  771. if ((super->accflags & (Modifier::INTERFACE | Modifier::FINAL)) != 0)
  772. {
  773. throw_incompatible_class_change_error (sub->getName ());
  774. }
  775. // if the super class is not public, we need to check some more
  776. if ((super->accflags & Modifier::PUBLIC) == 0)
  777. {
  778. // With package scope, the classes must have the same
  779. // class loader.
  780. if ( sub->loader != super->loader
  781. || !_Jv_ClassNameSamePackage (sub->name, super->name))
  782. {
  783. throw_incompatible_class_change_error (sub->getName ());
  784. }
  785. }
  786. for (; super != 0; super = super->superclass)
  787. {
  788. if (super == sub)
  789. throw_class_circularity_error (sub->getName ());
  790. }
  791. }
  792. void _Jv_ClassReader::handleInterfacesBegin (int count)
  793. {
  794. def->interfaces = (jclass*) _Jv_AllocBytes (count*sizeof (jclass));
  795. def->interface_count = count;
  796. }
  797. void _Jv_ClassReader::handleInterface (int if_number, int offset)
  798. {
  799. _Jv_word * pool_data = def->constants.data;
  800. unsigned char * pool_tags = (unsigned char*) def->constants.tags;
  801. jclass the_interface;
  802. if (pool_tags[offset] == JV_CONSTANT_Class)
  803. {
  804. _Jv_Utf8Const* name = pool_data[offset].utf8;
  805. the_interface = _Jv_FindClass (name, def->loader);
  806. }
  807. else if (pool_tags[offset] == JV_CONSTANT_ResolvedClass)
  808. {
  809. the_interface = pool_data[offset].clazz;
  810. }
  811. else
  812. {
  813. throw_no_class_def_found_error ("erroneous constant pool tag");
  814. }
  815. // checks the validity of the_interface, and that we are in fact
  816. // allowed to implement that interface.
  817. checkImplements (def, the_interface);
  818. pool_data[offset].clazz = the_interface;
  819. pool_tags[offset] = JV_CONSTANT_ResolvedClass;
  820. def->interfaces[if_number] = the_interface;
  821. }
  822. void
  823. _Jv_ClassReader::checkImplements (jclass sub, jclass super)
  824. {
  825. using namespace java::lang::reflect;
  826. // well, it *must* be an interface
  827. if ((super->accflags & Modifier::INTERFACE) == 0)
  828. {
  829. throw_incompatible_class_change_error (sub->getName ());
  830. }
  831. // if it has package scope, it must also be defined by the
  832. // same loader.
  833. if ((super->accflags & Modifier::PUBLIC) == 0)
  834. {
  835. if ( sub->loader != super->loader
  836. || !_Jv_ClassNameSamePackage (sub->name, super->name))
  837. {
  838. throw_incompatible_class_change_error (sub->getName ());
  839. }
  840. }
  841. // FIXME: add interface circularity check here
  842. if (sub == super)
  843. {
  844. throw_class_circularity_error (sub->getName ());
  845. }
  846. }
  847. void _Jv_ClassReader::handleFieldsBegin (int count)
  848. {
  849. def->fields = (_Jv_Field*)
  850. _Jv_AllocBytes (count * sizeof (_Jv_Field));
  851. def->field_count = count;
  852. def->field_initializers = (_Jv_ushort*)
  853. _Jv_AllocBytes (count * sizeof (_Jv_ushort));
  854. for (int i = 0; i < count; i++)
  855. def->field_initializers[i] = (_Jv_ushort) 0;
  856. }
  857. void _Jv_ClassReader::handleField (int field_no,
  858. int flags,
  859. int name,
  860. int desc)
  861. {
  862. using namespace java::lang::reflect;
  863. _Jv_word *pool_data = def->constants.data;
  864. _Jv_Field *field = &def->fields[field_no];
  865. _Jv_Utf8Const *field_name = pool_data[name].utf8;
  866. #ifndef COMPACT_FIELDS
  867. field->name = field_name;
  868. #else
  869. field->nameIndex = name;
  870. #endif
  871. if (verify)
  872. verify_identifier (field_name);
  873. // ignore flags we don't know about.
  874. field->flags = flags & Modifier::ALL_FLAGS;
  875. if (verify)
  876. {
  877. if (field->flags & (Modifier::SYNCHRONIZED
  878. | Modifier::NATIVE
  879. | Modifier::INTERFACE
  880. | Modifier::ABSTRACT))
  881. throw_class_format_error ("erroneous field access flags");
  882. if (1 < ( ((field->flags & Modifier::PUBLIC) ? 1 : 0)
  883. +((field->flags & Modifier::PRIVATE) ? 1 : 0)
  884. +((field->flags & Modifier::PROTECTED) ? 1 : 0)))
  885. throw_class_format_error ("erroneous field access flags");
  886. }
  887. _Jv_Utf8Const* sig = pool_data[desc].utf8;
  888. if (verify)
  889. _Jv_VerifyFieldSignature (sig);
  890. // field->type is really a jclass, but while it is still
  891. // unresolved we keep an _Jv_Utf8Const* instead.
  892. field->type = (jclass) sig;
  893. field->flags |= _Jv_FIELD_UNRESOLVED_FLAG;
  894. field->u.boffset = 0;
  895. }
  896. void _Jv_ClassReader::handleConstantValueAttribute (int field_index,
  897. int value)
  898. {
  899. using namespace java::lang::reflect;
  900. _Jv_Field *field = &def->fields[field_index];
  901. if ((field->flags & (Modifier::STATIC
  902. | Modifier::FINAL
  903. | Modifier::PRIVATE)) == 0)
  904. {
  905. // Ignore, as per vmspec #4.7.2
  906. return;
  907. }
  908. // do not allow multiple constant fields!
  909. if (field->flags & _Jv_FIELD_CONSTANT_VALUE)
  910. throw_class_format_error ("field has multiple ConstantValue attributes");
  911. field->flags |= _Jv_FIELD_CONSTANT_VALUE;
  912. def->field_initializers[field_index] = value;
  913. /* type check the initializer */
  914. if (value <= 0 || value >= pool_count)
  915. throw_class_format_error ("erroneous ConstantValue attribute");
  916. /* FIXME: do the rest */
  917. }
  918. void _Jv_ClassReader::handleFieldsEnd ()
  919. {
  920. using namespace java::lang::reflect;
  921. // We need to reorganize the fields so that the static ones are first,
  922. // to conform to GCJ class layout.
  923. int low = 0;
  924. int high = def->field_count-1;
  925. _Jv_Field *fields = def->fields;
  926. _Jv_ushort *inits = def->field_initializers;
  927. // this is kind of a raw version of quicksort.
  928. while (low < high)
  929. {
  930. // go forward on low, while it's a static
  931. while (low < high && (fields[low].flags & Modifier::STATIC) != 0)
  932. low++;
  933. // go backwards on high, while it's a non-static
  934. while (low < high && (fields[high].flags & Modifier::STATIC) == 0)
  935. high--;
  936. if (low==high)
  937. break;
  938. _Jv_Field tmp = fields[low];
  939. _Jv_ushort itmp = inits[low];
  940. fields[low] = fields[high];
  941. inits[low] = inits[high];
  942. fields[high] = tmp;
  943. inits[high] = itmp;
  944. high -= 1;
  945. low += 1;
  946. }
  947. if ((fields[low].flags & Modifier::STATIC) != 0)
  948. low += 1;
  949. def->static_field_count = low;
  950. }
  951. void
  952. _Jv_ClassReader::handleMethodsBegin (int count)
  953. {
  954. def->methods = (_Jv_Method *) _Jv_AllocBytes (sizeof (_Jv_Method) * count);
  955. def->interpreted_methods
  956. = (_Jv_MethodBase **) _Jv_AllocBytes (sizeof (_Jv_MethodBase *)
  957. * count);
  958. for (int i = 0; i < count; i++)
  959. {
  960. def->interpreted_methods[i] = 0;
  961. def->methods[i].index = (_Jv_ushort) -1;
  962. }
  963. def->method_count = count;
  964. }
  965. void _Jv_ClassReader::handleMethod
  966. (int mth_index, int accflags, int name, int desc)
  967. {
  968. using namespace java::lang::reflect;
  969. _Jv_word *pool_data = def->constants.data;
  970. _Jv_Method *method = &def->methods[mth_index];
  971. check_tag (name, JV_CONSTANT_Utf8);
  972. prepare_pool_entry (name, JV_CONSTANT_Utf8);
  973. method->name = pool_data[name].utf8;
  974. check_tag (desc, JV_CONSTANT_Utf8);
  975. prepare_pool_entry (desc, JV_CONSTANT_Utf8);
  976. method->signature = pool_data[desc].utf8;
  977. // ignore unknown flags
  978. method->accflags = accflags & Modifier::ALL_FLAGS;
  979. // intialize...
  980. method->ncode = 0;
  981. method->throws = NULL;
  982. if (verify)
  983. {
  984. if (_Jv_equalUtf8Consts (method->name, clinit_name)
  985. || _Jv_equalUtf8Consts (method->name, init_name))
  986. /* ignore */;
  987. else
  988. verify_identifier (method->name);
  989. _Jv_VerifyMethodSignature (method->signature);
  990. if (method->accflags & (Modifier::VOLATILE
  991. | Modifier::TRANSIENT
  992. | Modifier::INTERFACE))
  993. throw_class_format_error ("erroneous method access flags");
  994. if (1 < ( ((method->accflags & Modifier::PUBLIC) ? 1 : 0)
  995. +((method->accflags & Modifier::PRIVATE) ? 1 : 0)
  996. +((method->accflags & Modifier::PROTECTED) ? 1 : 0)))
  997. throw_class_format_error ("erroneous method access flags");
  998. }
  999. }
  1000. void _Jv_ClassReader::handleCodeAttribute
  1001. (int method_index, int max_stack, int max_locals,
  1002. int code_start, int code_length, int exc_table_length)
  1003. {
  1004. int size = _Jv_InterpMethod::size (exc_table_length, code_length);
  1005. _Jv_InterpMethod *method =
  1006. (_Jv_InterpMethod*) (_Jv_AllocBytes (size));
  1007. method->max_stack = max_stack;
  1008. method->max_locals = max_locals;
  1009. method->code_length = code_length;
  1010. method->exc_count = exc_table_length;
  1011. method->defining_class = def;
  1012. method->self = &def->methods[method_index];
  1013. method->prepared = NULL;
  1014. // grab the byte code!
  1015. memcpy ((void*) method->bytecode (),
  1016. (void*) (bytes+code_start),
  1017. code_length);
  1018. def->interpreted_methods[method_index] = method;
  1019. }
  1020. void _Jv_ClassReader::handleExceptionTableEntry
  1021. (int method_index, int exc_index,
  1022. int start_pc, int end_pc, int handler_pc, int catch_type)
  1023. {
  1024. _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
  1025. (def->interpreted_methods[method_index]);
  1026. _Jv_InterpException *exc = method->exceptions ();
  1027. exc[exc_index].start_pc.i = start_pc;
  1028. exc[exc_index].end_pc.i = end_pc;
  1029. exc[exc_index].handler_pc.i = handler_pc;
  1030. exc[exc_index].handler_type.i = catch_type;
  1031. }
  1032. void _Jv_ClassReader::handleMethodsEnd ()
  1033. {
  1034. using namespace java::lang::reflect;
  1035. for (int i = 0; i < def->method_count; i++)
  1036. {
  1037. _Jv_Method *method = &def->methods[i];
  1038. if ((method->accflags & Modifier::NATIVE) != 0)
  1039. {
  1040. if (def->interpreted_methods[i] != 0)
  1041. throw_class_format_error ("code provided for native method");
  1042. else
  1043. {
  1044. _Jv_JNIMethod *m = (_Jv_JNIMethod *)
  1045. _Jv_AllocBytes (sizeof (_Jv_JNIMethod));
  1046. m->defining_class = def;
  1047. m->self = method;
  1048. m->function = NULL;
  1049. def->interpreted_methods[i] = m;
  1050. }
  1051. }
  1052. else if ((method->accflags & Modifier::ABSTRACT) != 0)
  1053. {
  1054. if (def->interpreted_methods[i] != 0)
  1055. throw_class_format_error ("code provided for abstract method");
  1056. }
  1057. else
  1058. {
  1059. if (def->interpreted_methods[i] == 0)
  1060. throw_class_format_error ("method with no code");
  1061. }
  1062. }
  1063. }
  1064. void _Jv_ClassReader::throw_class_format_error (char *msg)
  1065. {
  1066. jstring str;
  1067. if (def->name != NULL)
  1068. {
  1069. jsize mlen = strlen (msg);
  1070. unsigned char* data = (unsigned char*) def->name->data;
  1071. int ulen = def->name->length;
  1072. unsigned char* limit = data + ulen;
  1073. jsize nlen = _Jv_strLengthUtf8 ((char *) data, ulen);
  1074. jsize len = nlen + mlen + 3;
  1075. str = JvAllocString(len);
  1076. jchar *chrs = JvGetStringChars(str);
  1077. while (data < limit)
  1078. *chrs++ = UTF8_GET(data, limit);
  1079. *chrs++ = ' ';
  1080. *chrs++ = '(';
  1081. for (;;)
  1082. {
  1083. char c = *msg++;
  1084. if (c == 0)
  1085. break;
  1086. *chrs++ = c & 0xFFFF;
  1087. }
  1088. *chrs++ = ')';
  1089. }
  1090. else
  1091. str = JvNewStringLatin1 (msg);
  1092. ::throw_class_format_error (str);
  1093. }
  1094. /** Here we define the exceptions that can be thrown */
  1095. static void
  1096. throw_no_class_def_found_error (jstring msg)
  1097. {
  1098. throw (msg
  1099. ? new java::lang::NoClassDefFoundError (msg)
  1100. : new java::lang::NoClassDefFoundError);
  1101. }
  1102. static void
  1103. throw_no_class_def_found_error (char *msg)
  1104. {
  1105. throw_no_class_def_found_error (JvNewStringLatin1 (msg));
  1106. }
  1107. static void
  1108. throw_class_format_error (jstring msg)
  1109. {
  1110. throw (msg
  1111. ? new java::lang::ClassFormatError (msg)
  1112. : new java::lang::ClassFormatError);
  1113. }
  1114. static void
  1115. throw_internal_error (char *msg)
  1116. {
  1117. throw new java::lang::InternalError (JvNewStringLatin1 (msg));
  1118. }
  1119. static void
  1120. throw_incompatible_class_change_error (jstring msg)
  1121. {
  1122. throw new java::lang::IncompatibleClassChangeError (msg);
  1123. }
  1124. static void
  1125. throw_class_circularity_error (jstring msg)
  1126. {
  1127. throw new java::lang::ClassCircularityError (msg);
  1128. }
  1129. #endif /* INTERPRETER */
  1130. /** This section takes care of verifying integrity of identifiers,
  1131. signatures, field ddescriptors, and class names */
  1132. #define UTF8_PEEK(PTR, LIMIT) \
  1133. ({ unsigned char* xxkeep = (PTR); \
  1134. int xxch = UTF8_GET(PTR,LIMIT); \
  1135. PTR = xxkeep; xxch; })
  1136. /* Verify one element of a type descriptor or signature. */
  1137. static unsigned char*
  1138. _Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok)
  1139. {
  1140. if (ptr >= limit)
  1141. return 0;
  1142. int ch = UTF8_GET (ptr, limit);
  1143. switch (ch)
  1144. {
  1145. case 'V':
  1146. if (! void_ok)
  1147. return 0;
  1148. case 'S': case 'B': case 'I': case 'J':
  1149. case 'Z': case 'C': case 'F': case 'D':
  1150. break;
  1151. case 'L':
  1152. {
  1153. unsigned char *start = ptr, *end;
  1154. do
  1155. {
  1156. if (ptr > limit)
  1157. return 0;
  1158. end = ptr;
  1159. if ((ch = UTF8_GET (ptr, limit)) == -1)
  1160. return 0;
  1161. }
  1162. while (ch != ';');
  1163. if (! _Jv_VerifyClassName (start, (unsigned short) (end-start)))
  1164. return 0;
  1165. }
  1166. break;
  1167. case '[':
  1168. return _Jv_VerifyOne (ptr, limit, false);
  1169. break;
  1170. default:
  1171. return 0;
  1172. }
  1173. return ptr;
  1174. }
  1175. /* Verification and loading procedures. */
  1176. bool
  1177. _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
  1178. {
  1179. unsigned char* ptr = (unsigned char*) sig->data;
  1180. unsigned char* limit = ptr + sig->length;
  1181. ptr = _Jv_VerifyOne (ptr, limit, false);
  1182. return ptr == limit;
  1183. }
  1184. bool
  1185. _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig)
  1186. {
  1187. unsigned char* ptr = (unsigned char*) sig->data;
  1188. unsigned char* limit = ptr + sig->length;
  1189. if (ptr == limit || UTF8_GET(ptr,limit) != '(')
  1190. return false;
  1191. while (ptr && UTF8_PEEK (ptr, limit) != ')')
  1192. ptr = _Jv_VerifyOne (ptr, limit, false);
  1193. if (UTF8_GET (ptr, limit) != ')')
  1194. return false;
  1195. // get the return type
  1196. ptr = _Jv_VerifyOne (ptr, limit, true);
  1197. return ptr == limit;
  1198. }
  1199. /* We try to avoid calling the Character methods all the time, in
  1200. fact, they will only be called for non-standard things. */
  1201. static __inline__ int
  1202. is_identifier_start (int c)
  1203. {
  1204. unsigned int ch = (unsigned)c;
  1205. if ((ch - 0x41U) < 29U) /* A ... Z */
  1206. return 1;
  1207. if ((ch - 0x61U) < 29U) /* a ... z */
  1208. return 1;
  1209. if (ch == 0x5FU) /* _ */
  1210. return 1;
  1211. return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
  1212. }
  1213. static __inline__ int
  1214. is_identifier_part (int c)
  1215. {
  1216. unsigned int ch = (unsigned)c;
  1217. if ((ch - 0x41U) < 29U) /* A ... Z */
  1218. return 1;
  1219. if ((ch - 0x61U) < 29U) /* a ... z */
  1220. return 1;
  1221. if ((ch - 0x30) < 10U) /* 0 .. 9 */
  1222. return 1;
  1223. if (ch == 0x5FU || ch == 0x24U) /* _ $ */
  1224. return 1;
  1225. return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
  1226. }
  1227. bool
  1228. _Jv_VerifyIdentifier (_Jv_Utf8Const* name)
  1229. {
  1230. unsigned char *ptr = (unsigned char*) name->data;
  1231. unsigned char *limit = ptr + name->length;
  1232. int ch;
  1233. if ((ch = UTF8_GET (ptr, limit))==-1
  1234. || ! is_identifier_start (ch))
  1235. return false;
  1236. while (ptr != limit)
  1237. {
  1238. if ((ch = UTF8_GET (ptr, limit))==-1
  1239. || ! is_identifier_part (ch))
  1240. return false;
  1241. }
  1242. return true;
  1243. }
  1244. bool
  1245. _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length)
  1246. {
  1247. unsigned char *limit = ptr+length;
  1248. int ch;
  1249. if ('[' == UTF8_PEEK (ptr, limit))
  1250. {
  1251. unsigned char *end = _Jv_VerifyOne (++ptr, limit, false);
  1252. // _Jv_VerifyOne must leave us looking at the terminating nul
  1253. // byte.
  1254. if (! end || *end)
  1255. return false;
  1256. else
  1257. return true;
  1258. }
  1259. next_level:
  1260. for (;;) {
  1261. if ((ch = UTF8_GET (ptr, limit))==-1)
  1262. return false;
  1263. if (! is_identifier_start (ch))
  1264. return false;
  1265. for (;;) {
  1266. if (ptr == limit)
  1267. return true;
  1268. else if ((ch = UTF8_GET (ptr, limit))==-1)
  1269. return false;
  1270. else if (ch == '.')
  1271. goto next_level;
  1272. else if (! is_identifier_part (ch))
  1273. return false;
  1274. }
  1275. }
  1276. }
  1277. bool
  1278. _Jv_VerifyClassName (_Jv_Utf8Const *name)
  1279. {
  1280. return _Jv_VerifyClassName ((unsigned char*)&name->data[0],
  1281. (_Jv_ushort) name->length);
  1282. }
  1283. /* Returns true, if NAME1 and NAME2 represent classes in the same
  1284. package. */
  1285. bool
  1286. _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2)
  1287. {
  1288. unsigned char* ptr1 = (unsigned char*) name1->data;
  1289. unsigned char* limit1 = ptr1 + name1->length;
  1290. unsigned char* last1 = ptr1;
  1291. // scan name1, and find the last occurrence of '.'
  1292. while (ptr1 < limit1) {
  1293. int ch1 = UTF8_GET (ptr1, limit1);
  1294. if (ch1 == '.')
  1295. last1 = ptr1;
  1296. else if (ch1 == -1)
  1297. return false;
  1298. }
  1299. // Now the length of NAME1's package name is LEN.
  1300. int len = last1 - (unsigned char*) name1->data;
  1301. // If this is longer than NAME2, then we're off.
  1302. if (len > name2->length)
  1303. return false;
  1304. // Then compare the first len bytes for equality.
  1305. if (memcmp ((void*) name1->data, (void*) name2->data, len) == 0)
  1306. {
  1307. // Check that there are no .'s after position LEN in NAME2.
  1308. unsigned char* ptr2 = (unsigned char*) name2->data + len;
  1309. unsigned char* limit2 =
  1310. (unsigned char*) name2->data + name2->length;
  1311. while (ptr2 < limit2)
  1312. {
  1313. int ch2 = UTF8_GET (ptr2, limit2);
  1314. if (ch2 == -1 || ch2 == '.')
  1315. return false;
  1316. }
  1317. return true;
  1318. }
  1319. return false;
  1320. }