defineclass.cc 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  1. // defineclass.cc - defining a class from .class format.
  2. /* Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2012
  3. Free Software Foundation
  4. This file is part of libgcj.
  5. This software is copyrighted work licensed under the terms of the
  6. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  7. details. */
  8. /*
  9. Author: Kresten Krab Thorup <krab@gnu.org>
  10. Written using the online versions of Java Language Specification (1st
  11. ed.) and The Java Virtual Machine Specification (2nd ed.).
  12. Future work may include reading (and handling) attributes which are
  13. currently being ignored ("InnerClasses", "LineNumber", etc...).
  14. */
  15. #include <config.h>
  16. #include <java-interp.h>
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <java-cpool.h>
  20. #include <gcj/cni.h>
  21. #include <execution.h>
  22. #include <java/lang/Class.h>
  23. #include <java/lang/Float.h>
  24. #include <java/lang/Double.h>
  25. #include <java/lang/Character.h>
  26. #include <java/lang/LinkageError.h>
  27. #include <java/lang/InternalError.h>
  28. #include <java/lang/ClassFormatError.h>
  29. #include <java/lang/NoClassDefFoundError.h>
  30. #include <java/lang/ClassCircularityError.h>
  31. #include <java/lang/IncompatibleClassChangeError.h>
  32. #include <java/lang/reflect/Modifier.h>
  33. #include <java/lang/reflect/Field.h>
  34. #include <java/lang/reflect/Method.h>
  35. #include <java/security/ProtectionDomain.h>
  36. #include <java/io/DataOutputStream.h>
  37. #include <java/io/ByteArrayOutputStream.h>
  38. using namespace gcj;
  39. #ifdef INTERPRETER
  40. // these go in some separate functions, to avoid having _Jv_InitClass
  41. // inserted all over the place.
  42. static void throw_internal_error (const char *msg)
  43. __attribute__ ((__noreturn__));
  44. static void throw_no_class_def_found_error (jstring msg)
  45. __attribute__ ((__noreturn__));
  46. static void throw_no_class_def_found_error (const char *msg)
  47. __attribute__ ((__noreturn__));
  48. static void throw_class_format_error (jstring msg)
  49. __attribute__ ((__noreturn__));
  50. static void throw_incompatible_class_change_error (jstring msg)
  51. __attribute__ ((__noreturn__));
  52. static void throw_class_circularity_error (jstring msg)
  53. __attribute__ ((__noreturn__));
  54. /**
  55. * We define class reading using a class. It is practical, since then
  56. * the entire class-reader can be a friend of class Class (it needs to
  57. * write all it's different structures); but also because this makes it
  58. * easy to make class definition reentrant, and thus two threads can be
  59. * defining classes at the same time. This class (_Jv_ClassReader) is
  60. * never exposed outside this file, so we don't have to worry about
  61. * public or private members here.
  62. */
  63. struct _Jv_ClassReader
  64. {
  65. // do verification? Currently, there is no option to disable this.
  66. // This flag just controls the verificaiton done by the class loader;
  67. // i.e., checking the integrity of the constant pool; and it is
  68. // allways on. You always want this as far as I can see, but it also
  69. // controls weither identifiers and type descriptors/signatures are
  70. // verified as legal. This could be somewhat more expensive since it
  71. // will call Character.isJavaIdentifier{Start,Part} for each character
  72. // in any identifier (field name or method name) it comes by. Thus,
  73. // it might be useful to turn off this verification for classes that
  74. // come from a trusted source. However, for GCJ, trusted classes are
  75. // most likely to be linked in.
  76. bool verify;
  77. // original input data.
  78. jbyteArray input_data;
  79. jint input_offset;
  80. // input data.
  81. unsigned char *bytes;
  82. int len;
  83. // current input position
  84. int pos;
  85. // the constant pool data
  86. int pool_count;
  87. unsigned char *tags;
  88. unsigned int *offsets;
  89. // the class to define (see java-interp.h)
  90. jclass def;
  91. // the classes associated interpreter data.
  92. _Jv_InterpClass *def_interp;
  93. // The name we found.
  94. _Jv_Utf8Const **found_name;
  95. // True if this is a 1.5 class file.
  96. bool is_15;
  97. // Buffer holding extra reflection data.
  98. ::java::io::ByteArrayOutputStream *reflection_data;
  99. ::java::io::DataOutputStream *data_stream;
  100. /* check that the given number of input bytes are available */
  101. inline void check (int num)
  102. {
  103. if (pos + num > len)
  104. throw_class_format_error ("Premature end of data");
  105. }
  106. /* skip a given number of bytes in input */
  107. inline void skip (int num)
  108. {
  109. check (num);
  110. pos += num;
  111. }
  112. /* read an unsigned 1-byte unit */
  113. inline static jint get1u (unsigned char* bytes)
  114. {
  115. return bytes[0];
  116. }
  117. /* read an unsigned 1-byte unit */
  118. inline jint read1u ()
  119. {
  120. skip (1);
  121. return get1u (bytes+pos-1);
  122. }
  123. /* read an unsigned 2-byte unit */
  124. inline static jint get2u (unsigned char *bytes)
  125. {
  126. return (((jint)bytes[0]) << 8) | ((jint)bytes[1]);
  127. }
  128. /* read an unsigned 2-byte unit */
  129. inline jint read2u ()
  130. {
  131. skip (2);
  132. return get2u (bytes+pos-2);
  133. }
  134. /* read a 4-byte unit */
  135. static jint get4 (unsigned char *bytes)
  136. {
  137. return (((jint)bytes[0]) << 24)
  138. | (((jint)bytes[1]) << 16)
  139. | (((jint)bytes[2]) << 8)
  140. | (((jint)bytes[3]) << 0);
  141. }
  142. /* read a 4-byte unit, (we don't do that quite so often) */
  143. inline jint read4 ()
  144. {
  145. skip (4);
  146. return get4 (bytes+pos-4);
  147. }
  148. /* read a 8-byte unit */
  149. static jlong get8 (unsigned char* bytes)
  150. {
  151. return (((jlong)bytes[0]) << 56)
  152. | (((jlong)bytes[1]) << 48)
  153. | (((jlong)bytes[2]) << 40)
  154. | (((jlong)bytes[3]) << 32)
  155. | (((jlong)bytes[4]) << 24)
  156. | (((jlong)bytes[5]) << 16)
  157. | (((jlong)bytes[6]) << 8)
  158. | (((jlong)bytes[7]) << 0);
  159. }
  160. /* read a 8-byte unit */
  161. inline jlong read8 ()
  162. {
  163. skip (8);
  164. return get8 (bytes+pos-8);
  165. }
  166. inline void check_tag (int index, char expected_tag)
  167. {
  168. if (index < 0
  169. || index > pool_count
  170. || tags[index] != expected_tag)
  171. throw_class_format_error ("erroneous constant pool tag");
  172. }
  173. inline void verify_identifier (_Jv_Utf8Const* name)
  174. {
  175. if (! _Jv_VerifyIdentifier (name))
  176. throw_class_format_error ("erroneous identifier");
  177. }
  178. inline void verify_classname (unsigned char* ptr, _Jv_ushort length)
  179. {
  180. if (! _Jv_VerifyClassName (ptr, length))
  181. throw_class_format_error ("erroneous class name");
  182. }
  183. inline void verify_classname (_Jv_Utf8Const *name)
  184. {
  185. if (! _Jv_VerifyClassName (name))
  186. throw_class_format_error ("erroneous class name");
  187. }
  188. inline void verify_field_signature (_Jv_Utf8Const *sig)
  189. {
  190. if (! _Jv_VerifyFieldSignature (sig))
  191. throw_class_format_error ("erroneous type descriptor");
  192. }
  193. inline void verify_method_signature (_Jv_Utf8Const *sig)
  194. {
  195. if (! _Jv_VerifyMethodSignature (sig))
  196. throw_class_format_error ("erroneous type descriptor");
  197. }
  198. ::java::io::DataOutputStream *get_reflection_stream ()
  199. {
  200. if (reflection_data == NULL)
  201. {
  202. reflection_data = new ::java::io::ByteArrayOutputStream();
  203. data_stream = new ::java::io::DataOutputStream(reflection_data);
  204. }
  205. return data_stream;
  206. }
  207. _Jv_ClassReader (jclass klass, jbyteArray data, jint offset, jint length,
  208. java::security::ProtectionDomain *pd,
  209. _Jv_Utf8Const **name_result)
  210. {
  211. if (klass == 0 || length < 0 || offset+length > data->length)
  212. throw_internal_error ("arguments to _Jv_DefineClass");
  213. verify = true;
  214. input_data = data;
  215. input_offset = offset;
  216. bytes = (unsigned char*) (elements (data)+offset);
  217. len = length;
  218. pos = 0;
  219. is_15 = false;
  220. def = klass;
  221. found_name = name_result;
  222. reflection_data = NULL;
  223. data_stream = NULL;
  224. def->size_in_bytes = -1;
  225. def->vtable_method_count = -1;
  226. def->engine = &_Jv_soleInterpreterEngine;
  227. def->protectionDomain = pd;
  228. }
  229. /** and here goes the parser members defined out-of-line */
  230. void parse ();
  231. void read_constpool ();
  232. void prepare_pool_entry (int index, unsigned char tag,
  233. bool rewrite = true);
  234. void read_fields ();
  235. void read_methods ();
  236. void read_one_class_attribute ();
  237. void read_one_method_attribute (int method);
  238. void read_one_code_attribute (int method);
  239. void read_one_field_attribute (int field, bool *);
  240. void throw_class_format_error (const char *msg);
  241. void handleEnclosingMethod(int);
  242. void handleGenericSignature(jv_attr_type, unsigned short, int);
  243. void handleAnnotationElement();
  244. void handleAnnotation();
  245. void handleAnnotations();
  246. void handleMemberAnnotations(jv_attr_type, int, int);
  247. void handleAnnotationDefault(int, int);
  248. void handleParameterAnnotations(int, int);
  249. void finish_reflection_data ();
  250. /** check an utf8 entry, without creating a Utf8Const object */
  251. bool is_attribute_name (int index, const char *name);
  252. /** return the value of a utf8 entry in the passed array */
  253. int pool_Utf8_to_char_arr (int index, char **entry);
  254. /** here goes the class-loader members defined out-of-line */
  255. void handleConstantPool ();
  256. void handleClassBegin (int, int, int);
  257. void handleInterfacesBegin (int);
  258. void handleInterface (int, int);
  259. void handleFieldsBegin (int);
  260. void handleField (int, int, int, int, int *);
  261. void handleConstantValueAttribute (int, int, bool *);
  262. void handleMethodsBegin (int);
  263. void handleMethod (int, int, int, int);
  264. void handleMethodsEnd ();
  265. void handleCodeAttribute (int, int, int, int, int, int);
  266. void handleExceptionTableEntry (int, int, int, int, int, int);
  267. void checkExtends (jclass sub, jclass super);
  268. void checkImplements (jclass sub, jclass super);
  269. /*
  270. * FIXME: we should keep a hash table of utf8-strings, since many will
  271. * be the same. It's a little tricky, however, because the hash table
  272. * needs to interact gracefully with the garbage collector. Much
  273. * memory is to be saved by this, however! perhaps the improvement
  274. * could be implemented in prims.cc (_Jv_makeUtf8Const), since it
  275. * computes the hash value anyway.
  276. */
  277. };
  278. // Note that *NAME_RESULT will only be set if the class is registered
  279. // with the class loader. This is how the caller can know whether
  280. // unregistration is require.
  281. void
  282. _Jv_DefineClass (jclass klass, jbyteArray data, jint offset, jint length,
  283. java::security::ProtectionDomain *pd,
  284. _Jv_Utf8Const **name_result)
  285. {
  286. _Jv_ClassReader reader (klass, data, offset, length, pd, name_result);
  287. reader.parse();
  288. /* that's it! */
  289. }
  290. /** This section defines the parsing/scanning of the class data */
  291. // Major and minor version numbers for various releases.
  292. #define MAJOR_1_1 45
  293. #define MINOR_1_1 3
  294. #define MAJOR_1_2 46
  295. #define MINOR_1_2 0
  296. #define MAJOR_1_3 47
  297. #define MINOR_1_3 0
  298. #define MAJOR_1_4 48
  299. #define MINOR_1_4 0
  300. #define MAJOR_1_5 49
  301. #define MINOR_1_5 0
  302. #define MAJOR_1_6 50
  303. #define MINOR_1_6 0
  304. #define MAJOR_1_7 51
  305. #define MINOR_1_7 0
  306. void
  307. _Jv_ClassReader::parse ()
  308. {
  309. int magic = read4 ();
  310. if (magic != (int) 0xCAFEBABE)
  311. throw_class_format_error ("bad magic number");
  312. int minor_version = read2u ();
  313. int major_version = read2u ();
  314. if (major_version < MAJOR_1_1 || major_version > MAJOR_1_7
  315. || (major_version == MAJOR_1_7 && minor_version > MINOR_1_7))
  316. throw_class_format_error ("unrecognized class file version");
  317. is_15 = (major_version >= MAJOR_1_5);
  318. pool_count = read2u ();
  319. read_constpool ();
  320. int access_flags = read2u ();
  321. int this_class = read2u ();
  322. int super_class = read2u ();
  323. check_tag (this_class, JV_CONSTANT_Class);
  324. if (super_class != 0)
  325. check_tag (super_class, JV_CONSTANT_Class);
  326. handleClassBegin (access_flags, this_class, super_class);
  327. // Allocate our aux_info here, after the name is set, to fulfill our
  328. // contract with the collector interface.
  329. def->aux_info = (void *) _Jv_AllocRawObj (sizeof (_Jv_InterpClass));
  330. def_interp = (_Jv_InterpClass *) def->aux_info;
  331. int interfaces_count = read2u ();
  332. handleInterfacesBegin (interfaces_count);
  333. for (int i = 0; i < interfaces_count; i++)
  334. {
  335. int iface = read2u ();
  336. check_tag (iface, JV_CONSTANT_Class);
  337. handleInterface (i, iface);
  338. }
  339. read_fields ();
  340. read_methods ();
  341. int attributes_count = read2u ();
  342. for (int i = 0; i < attributes_count; i++)
  343. {
  344. read_one_class_attribute ();
  345. }
  346. if (pos != len)
  347. throw_class_format_error ("unused data before end of file");
  348. finish_reflection_data ();
  349. // Tell everyone we're done.
  350. def->state = JV_STATE_READ;
  351. if (gcj::verbose_class_flag)
  352. _Jv_Linker::print_class_loaded (def);
  353. ++gcj::loadedClasses;
  354. def->notifyAll ();
  355. }
  356. void
  357. _Jv_ClassReader::finish_reflection_data ()
  358. {
  359. if (data_stream == NULL)
  360. return;
  361. data_stream->writeByte(JV_DONE_ATTR);
  362. data_stream->flush();
  363. int nbytes = reflection_data->count;
  364. unsigned char *new_bytes = (unsigned char *) _Jv_AllocBytes (nbytes);
  365. memcpy (new_bytes, elements (reflection_data->buf), nbytes);
  366. def->reflection_data = new_bytes;
  367. }
  368. void
  369. _Jv_ClassReader::handleEnclosingMethod (int len)
  370. {
  371. if (len != 4)
  372. throw_class_format_error ("invalid EnclosingMethod attribute");
  373. // FIXME: only allow one...
  374. int class_index = read2u();
  375. check_tag (class_index, JV_CONSTANT_Class);
  376. prepare_pool_entry (class_index, JV_CONSTANT_Class);
  377. int method_index = read2u();
  378. // Zero is ok and means no enclosing method.
  379. if (method_index != 0)
  380. {
  381. check_tag (method_index, JV_CONSTANT_NameAndType);
  382. prepare_pool_entry (method_index, JV_CONSTANT_NameAndType);
  383. }
  384. ::java::io::DataOutputStream *stream = get_reflection_stream ();
  385. stream->writeByte(JV_CLASS_ATTR);
  386. stream->writeInt(5);
  387. stream->writeByte(JV_ENCLOSING_METHOD_KIND);
  388. stream->writeShort(class_index);
  389. stream->writeShort(method_index);
  390. }
  391. void
  392. _Jv_ClassReader::handleGenericSignature (jv_attr_type type,
  393. unsigned short index,
  394. int len)
  395. {
  396. if (len != 2)
  397. throw_class_format_error ("invalid Signature attribute");
  398. int cpool_idx = read2u();
  399. check_tag (cpool_idx, JV_CONSTANT_Utf8);
  400. prepare_pool_entry (cpool_idx, JV_CONSTANT_Utf8, false);
  401. ::java::io::DataOutputStream *stream = get_reflection_stream ();
  402. stream->writeByte(type);
  403. int attrlen = 3;
  404. if (type != JV_CLASS_ATTR)
  405. attrlen += 2;
  406. stream->writeInt(attrlen);
  407. if (type != JV_CLASS_ATTR)
  408. stream->writeShort(index);
  409. stream->writeByte(JV_SIGNATURE_KIND);
  410. stream->writeShort(cpool_idx);
  411. }
  412. void
  413. _Jv_ClassReader::handleAnnotationElement()
  414. {
  415. int tag = read1u();
  416. switch (tag)
  417. {
  418. case 'B':
  419. case 'C':
  420. case 'S':
  421. case 'Z':
  422. case 'I':
  423. {
  424. int index = read2u();
  425. check_tag (index, JV_CONSTANT_Integer);
  426. prepare_pool_entry (index, JV_CONSTANT_Integer);
  427. }
  428. break;
  429. case 'D':
  430. {
  431. int index = read2u();
  432. check_tag (index, JV_CONSTANT_Double);
  433. prepare_pool_entry (index, JV_CONSTANT_Double);
  434. }
  435. break;
  436. case 'F':
  437. {
  438. int index = read2u();
  439. check_tag (index, JV_CONSTANT_Float);
  440. prepare_pool_entry (index, JV_CONSTANT_Float);
  441. }
  442. break;
  443. case 'J':
  444. {
  445. int index = read2u();
  446. check_tag (index, JV_CONSTANT_Long);
  447. prepare_pool_entry (index, JV_CONSTANT_Long);
  448. }
  449. break;
  450. case 's':
  451. {
  452. int index = read2u();
  453. // Despite what the JVM spec says, compilers generate a Utf8
  454. // constant here, not a String.
  455. check_tag (index, JV_CONSTANT_Utf8);
  456. prepare_pool_entry (index, JV_CONSTANT_Utf8, false);
  457. }
  458. break;
  459. case 'e':
  460. {
  461. int type_name_index = read2u();
  462. int const_name_index = read2u ();
  463. check_tag (type_name_index, JV_CONSTANT_Utf8);
  464. prepare_pool_entry (type_name_index, JV_CONSTANT_Utf8);
  465. check_tag (const_name_index, JV_CONSTANT_Utf8);
  466. prepare_pool_entry (const_name_index, JV_CONSTANT_Utf8, false);
  467. }
  468. break;
  469. case 'c':
  470. {
  471. int index = read2u();
  472. check_tag (index, JV_CONSTANT_Utf8);
  473. prepare_pool_entry (index, JV_CONSTANT_Utf8);
  474. }
  475. break;
  476. case '@':
  477. handleAnnotation();
  478. break;
  479. case '[':
  480. {
  481. int n_array_elts = read2u ();
  482. for (int i = 0; i < n_array_elts; ++i)
  483. handleAnnotationElement();
  484. }
  485. break;
  486. default:
  487. throw_class_format_error ("invalid annotation element");
  488. }
  489. }
  490. void
  491. _Jv_ClassReader::handleAnnotation()
  492. {
  493. int type_index = read2u();
  494. check_tag (type_index, JV_CONSTANT_Utf8);
  495. prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
  496. int npairs = read2u();
  497. for (int i = 0; i < npairs; ++i)
  498. {
  499. int name_index = read2u();
  500. check_tag (name_index, JV_CONSTANT_Utf8);
  501. prepare_pool_entry (name_index, JV_CONSTANT_Utf8, false);
  502. handleAnnotationElement();
  503. }
  504. }
  505. void
  506. _Jv_ClassReader::handleAnnotations()
  507. {
  508. int num = read2u();
  509. while (num--)
  510. handleAnnotation();
  511. }
  512. void
  513. _Jv_ClassReader::handleMemberAnnotations(jv_attr_type member_type,
  514. int member_index,
  515. int len)
  516. {
  517. // We're going to copy the bytes in verbatim. But first we want to
  518. // make sure the attribute is well-formed, and we want to prepare
  519. // the constant pool. So, we save our starting point.
  520. int orig_pos = pos;
  521. handleAnnotations();
  522. // FIXME: check that we read all LEN bytes?
  523. ::java::io::DataOutputStream *stream = get_reflection_stream ();
  524. stream->writeByte(member_type);
  525. int newLen = len + 1;
  526. if (member_type != JV_CLASS_ATTR)
  527. newLen += 2;
  528. stream->writeInt(newLen);
  529. stream->writeByte(JV_ANNOTATIONS_KIND);
  530. if (member_type != JV_CLASS_ATTR)
  531. stream->writeShort(member_index);
  532. // Write the data as-is.
  533. stream->write(input_data, input_offset + orig_pos, len);
  534. }
  535. void
  536. _Jv_ClassReader::handleAnnotationDefault(int member_index, int len)
  537. {
  538. int orig_pos = pos;
  539. handleAnnotationElement();
  540. ::java::io::DataOutputStream *stream = get_reflection_stream ();
  541. stream->writeByte(JV_METHOD_ATTR);
  542. stream->writeInt(len + 3);
  543. stream->writeByte(JV_ANNOTATION_DEFAULT_KIND);
  544. stream->writeShort(member_index);
  545. stream->write(input_data, input_offset + orig_pos, len);
  546. }
  547. void
  548. _Jv_ClassReader::handleParameterAnnotations(int member_index, int len)
  549. {
  550. int orig_pos = pos;
  551. int n_params = read1u();
  552. for (int i = 0; i < n_params; ++i)
  553. handleAnnotations();
  554. ::java::io::DataOutputStream *stream = get_reflection_stream ();
  555. stream->writeByte(JV_METHOD_ATTR);
  556. stream->writeInt(len + 3);
  557. stream->writeByte(JV_PARAMETER_ANNOTATIONS_KIND);
  558. stream->writeShort(member_index);
  559. stream->write(input_data, input_offset + orig_pos, len);
  560. }
  561. void _Jv_ClassReader::read_constpool ()
  562. {
  563. tags = (unsigned char*) _Jv_AllocBytes (pool_count);
  564. offsets = (unsigned int *) _Jv_AllocBytes (sizeof (int) * pool_count) ;
  565. /** first, we scan the constant pool, collecting tags and offsets */
  566. tags[0] = JV_CONSTANT_Undefined;
  567. offsets[0] = pos;
  568. for (int c = 1; c < pool_count; c++)
  569. {
  570. tags[c] = read1u ();
  571. offsets[c] = pos;
  572. switch (tags[c])
  573. {
  574. case JV_CONSTANT_String:
  575. case JV_CONSTANT_Class:
  576. skip (2);
  577. break;
  578. case JV_CONSTANT_Fieldref:
  579. case JV_CONSTANT_Methodref:
  580. case JV_CONSTANT_InterfaceMethodref:
  581. case JV_CONSTANT_NameAndType:
  582. case JV_CONSTANT_Integer:
  583. case JV_CONSTANT_Float:
  584. skip (4);
  585. break;
  586. case JV_CONSTANT_Double:
  587. case JV_CONSTANT_Long:
  588. skip (8);
  589. tags[++c] = JV_CONSTANT_Undefined;
  590. break;
  591. case JV_CONSTANT_Utf8:
  592. {
  593. int len = read2u ();
  594. skip (len);
  595. }
  596. break;
  597. case JV_CONSTANT_Unicode:
  598. throw_class_format_error ("unicode not supported");
  599. break;
  600. default:
  601. throw_class_format_error ("erroneous constant pool tag");
  602. }
  603. }
  604. handleConstantPool ();
  605. }
  606. void _Jv_ClassReader::read_fields ()
  607. {
  608. int fields_count = read2u ();
  609. handleFieldsBegin (fields_count);
  610. // We want to sort the fields so that static fields come first,
  611. // followed by instance fields. We do this before parsing the
  612. // fields so that we can have the new indices available when
  613. // creating the annotation data structures.
  614. // Allocate this on the heap in case there are a large number of
  615. // fields.
  616. int *fieldmap = (int *) _Jv_AllocBytes (fields_count * sizeof (int));
  617. int save_pos = pos;
  618. int static_count = 0, instance_count = -1;
  619. for (int i = 0; i < fields_count; ++i)
  620. {
  621. using namespace java::lang::reflect;
  622. int access_flags = read2u ();
  623. skip (4);
  624. int attributes_count = read2u ();
  625. if ((access_flags & Modifier::STATIC) != 0)
  626. fieldmap[i] = static_count++;
  627. else
  628. fieldmap[i] = instance_count--;
  629. for (int j = 0; j < attributes_count; ++j)
  630. {
  631. skip (2);
  632. int length = read4 ();
  633. skip (length);
  634. }
  635. }
  636. pos = save_pos;
  637. // In the loop above, instance fields are represented by negative
  638. // numbers. Here we rewrite these to be proper offsets.
  639. for (int i = 0; i < fields_count; ++i)
  640. {
  641. if (fieldmap[i] < 0)
  642. fieldmap[i] = static_count - 1 - fieldmap[i];
  643. }
  644. def->static_field_count = static_count;
  645. for (int i = 0; i < fields_count; i++)
  646. {
  647. int access_flags = read2u ();
  648. int name_index = read2u ();
  649. int descriptor_index = read2u ();
  650. int attributes_count = read2u ();
  651. check_tag (name_index, JV_CONSTANT_Utf8);
  652. prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
  653. check_tag (descriptor_index, JV_CONSTANT_Utf8);
  654. prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
  655. handleField (i, access_flags, name_index, descriptor_index, fieldmap);
  656. bool found_value = false;
  657. for (int j = 0; j < attributes_count; j++)
  658. {
  659. read_one_field_attribute (fieldmap[i], &found_value);
  660. }
  661. }
  662. }
  663. bool
  664. _Jv_ClassReader::is_attribute_name (int index, const char *name)
  665. {
  666. check_tag (index, JV_CONSTANT_Utf8);
  667. int len = get2u (bytes+offsets[index]);
  668. if (len != (int) strlen (name))
  669. return false;
  670. else
  671. return !memcmp (bytes+offsets[index]+2, name, len);
  672. }
  673. // Get a UTF8 value from the constant pool and turn it into a garbage
  674. // collected char array.
  675. int _Jv_ClassReader::pool_Utf8_to_char_arr (int index, char** entry)
  676. {
  677. check_tag (index, JV_CONSTANT_Utf8);
  678. int len = get2u (bytes + offsets[index]);
  679. *entry = reinterpret_cast<char *> (_Jv_AllocBytes (len + 1));
  680. (*entry)[len] = '\0';
  681. memcpy (*entry, bytes + offsets[index] + 2, len);
  682. return len + 1;
  683. }
  684. void _Jv_ClassReader::read_one_field_attribute (int field_index,
  685. bool *found_value)
  686. {
  687. int name = read2u ();
  688. int length = read4 ();
  689. if (is_attribute_name (name, "ConstantValue"))
  690. {
  691. int cv = read2u ();
  692. if (cv < pool_count
  693. && cv > 0
  694. && (tags[cv] == JV_CONSTANT_Integer
  695. || tags[cv] == JV_CONSTANT_Float
  696. || tags[cv] == JV_CONSTANT_Long
  697. || tags[cv] == JV_CONSTANT_Double
  698. || tags[cv] == JV_CONSTANT_String))
  699. {
  700. handleConstantValueAttribute (field_index, cv, found_value);
  701. }
  702. else
  703. {
  704. throw_class_format_error ("erroneous ConstantValue attribute");
  705. }
  706. if (length != 2)
  707. throw_class_format_error ("erroneous ConstantValue attribute");
  708. }
  709. else if (is_attribute_name (name, "Signature"))
  710. handleGenericSignature(JV_FIELD_ATTR, field_index, length);
  711. else if (is_attribute_name (name, "RuntimeVisibleAnnotations"))
  712. handleMemberAnnotations(JV_FIELD_ATTR, field_index, length);
  713. else
  714. skip (length);
  715. }
  716. void _Jv_ClassReader::read_methods ()
  717. {
  718. int methods_count = read2u ();
  719. handleMethodsBegin (methods_count);
  720. for (int i = 0; i < methods_count; i++)
  721. {
  722. int access_flags = read2u ();
  723. int name_index = read2u ();
  724. int descriptor_index = read2u ();
  725. int attributes_count = read2u ();
  726. check_tag (name_index, JV_CONSTANT_Utf8);
  727. prepare_pool_entry (name_index, JV_CONSTANT_Utf8);
  728. check_tag (descriptor_index, JV_CONSTANT_Utf8);
  729. prepare_pool_entry (descriptor_index, JV_CONSTANT_Utf8);
  730. handleMethod (i, access_flags, name_index,
  731. descriptor_index);
  732. for (int j = 0; j < attributes_count; j++)
  733. {
  734. read_one_method_attribute (i);
  735. }
  736. }
  737. handleMethodsEnd ();
  738. }
  739. void _Jv_ClassReader::read_one_method_attribute (int method_index)
  740. {
  741. int name = read2u ();
  742. int length = read4 ();
  743. if (is_attribute_name (name, "Exceptions"))
  744. {
  745. _Jv_Method *method = reinterpret_cast<_Jv_Method *>
  746. (&def->methods[method_index]);
  747. if (method->throws != NULL)
  748. throw_class_format_error ("only one Exceptions attribute allowed per method");
  749. int num_exceptions = read2u ();
  750. _Jv_Utf8Const **exceptions =
  751. (_Jv_Utf8Const **) _Jv_AllocBytes ((num_exceptions + 1)
  752. * sizeof (_Jv_Utf8Const *));
  753. int out = 0;
  754. _Jv_word *pool_data = def->constants.data;
  755. for (int i = 0; i < num_exceptions; ++i)
  756. {
  757. int ndx = read2u ();
  758. // JLS 2nd Ed. 4.7.5 requires that the tag not be 0.
  759. if (ndx != 0)
  760. {
  761. check_tag (ndx, JV_CONSTANT_Class);
  762. exceptions[out++] = pool_data[ndx].utf8;
  763. }
  764. }
  765. exceptions[out] = NULL;
  766. method->throws = exceptions;
  767. }
  768. else if (is_attribute_name (name, "Code"))
  769. {
  770. int start_off = pos;
  771. int max_stack = read2u ();
  772. int max_locals = read2u ();
  773. int code_length = read4 ();
  774. int code_start = pos;
  775. skip (code_length);
  776. int exception_table_length = read2u ();
  777. handleCodeAttribute (method_index,
  778. max_stack, max_locals,
  779. code_start, code_length,
  780. exception_table_length);
  781. for (int i = 0; i < exception_table_length; i++)
  782. {
  783. int start_pc = read2u ();
  784. int end_pc = read2u ();
  785. int handler_pc = read2u ();
  786. int catch_type = read2u ();
  787. if (start_pc > end_pc
  788. || start_pc < 0
  789. // END_PC can be equal to CODE_LENGTH.
  790. // See JVM Spec 4.7.4.
  791. || end_pc > code_length
  792. || handler_pc >= code_length)
  793. throw_class_format_error ("erroneous exception handler info");
  794. if (! (tags[catch_type] == JV_CONSTANT_Class
  795. || tags[catch_type] == 0))
  796. {
  797. throw_class_format_error ("erroneous exception handler info");
  798. }
  799. handleExceptionTableEntry (method_index,
  800. i,
  801. start_pc,
  802. end_pc,
  803. handler_pc,
  804. catch_type);
  805. }
  806. int attributes_count = read2u ();
  807. for (int i = 0; i < attributes_count; i++)
  808. {
  809. read_one_code_attribute (method_index);
  810. }
  811. if ((pos - start_off) != length)
  812. throw_class_format_error ("code attribute too short");
  813. }
  814. else if (is_attribute_name (name, "Signature"))
  815. handleGenericSignature(JV_METHOD_ATTR, method_index, length);
  816. else if (is_attribute_name (name, "RuntimeVisibleAnnotations"))
  817. handleMemberAnnotations(JV_METHOD_ATTR, method_index, length);
  818. else if (is_attribute_name (name, "RuntimeVisibleParameterAnnotations"))
  819. handleParameterAnnotations(method_index, length);
  820. else if (is_attribute_name (name, "AnnotationDefault"))
  821. handleAnnotationDefault(method_index, length);
  822. else
  823. {
  824. /* ignore unknown attributes */
  825. skip (length);
  826. }
  827. }
  828. void _Jv_ClassReader::read_one_code_attribute (int method_index)
  829. {
  830. int name = read2u ();
  831. int length = read4 ();
  832. if (is_attribute_name (name, "LineNumberTable"))
  833. {
  834. _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
  835. (def_interp->interpreted_methods[method_index]);
  836. if (method->line_table != NULL)
  837. throw_class_format_error ("Method already has LineNumberTable");
  838. int table_len = read2u ();
  839. _Jv_LineTableEntry* table
  840. = (_Jv_LineTableEntry *) _Jv_AllocBytes (table_len
  841. * sizeof (_Jv_LineTableEntry));
  842. for (int i = 0; i < table_len; i++)
  843. {
  844. table[i].bytecode_pc = read2u ();
  845. table[i].line = read2u ();
  846. }
  847. method->line_table_len = table_len;
  848. method->line_table = table;
  849. }
  850. else if (is_attribute_name (name, "LocalVariableTable"))
  851. {
  852. _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
  853. (def_interp->interpreted_methods[method_index]);
  854. if (method->local_var_table != NULL)
  855. throw_class_format_error ("Method already has LocalVariableTable");
  856. int table_len = read2u ();
  857. _Jv_LocalVarTableEntry *table
  858. = reinterpret_cast<_Jv_LocalVarTableEntry *>
  859. (_Jv_AllocRawObj (table_len * sizeof (_Jv_LocalVarTableEntry)));
  860. for (int i = 0; i < table_len; i++)
  861. {
  862. table[i].bytecode_pc = read2u ();
  863. table[i].length = read2u ();
  864. pool_Utf8_to_char_arr (read2u (), &table[i].name);
  865. pool_Utf8_to_char_arr (read2u (), &table[i].descriptor);
  866. table[i].slot = read2u ();
  867. if (table[i].slot > method->max_locals || table[i].slot < 0)
  868. throw_class_format_error ("Malformed Local Variable Table: Invalid Slot");
  869. }
  870. method->local_var_table_len = table_len;
  871. method->local_var_table = table;
  872. }
  873. else
  874. {
  875. /* ignore unknown code attributes */
  876. skip (length);
  877. }
  878. }
  879. void _Jv_ClassReader::read_one_class_attribute ()
  880. {
  881. int name = read2u ();
  882. int length = read4 ();
  883. if (is_attribute_name (name, "SourceFile"))
  884. {
  885. int source_index = read2u ();
  886. check_tag (source_index, JV_CONSTANT_Utf8);
  887. prepare_pool_entry (source_index, JV_CONSTANT_Utf8, false);
  888. def_interp->source_file_name = _Jv_NewStringUtf8Const
  889. (def->constants.data[source_index].utf8);
  890. }
  891. else if (is_attribute_name (name, "Signature"))
  892. handleGenericSignature(JV_CLASS_ATTR, 0, length);
  893. else if (is_attribute_name (name, "EnclosingMethod"))
  894. handleEnclosingMethod(length);
  895. else if (is_attribute_name (name, "RuntimeVisibleAnnotations"))
  896. handleMemberAnnotations(JV_CLASS_ATTR, 0, length);
  897. else if (is_attribute_name (name, "InnerClasses"))
  898. {
  899. ::java::io::DataOutputStream *stream = get_reflection_stream ();
  900. stream->writeByte(JV_CLASS_ATTR);
  901. stream->writeInt(length + 1);
  902. stream->writeByte(JV_INNER_CLASSES_KIND);
  903. stream->write(input_data, input_offset + pos, length);
  904. skip (length);
  905. }
  906. else
  907. {
  908. /* Currently, we ignore most class attributes. */
  909. skip (length);
  910. }
  911. }
  912. /* this section defines the semantic actions of the parser */
  913. void _Jv_ClassReader::handleConstantPool ()
  914. {
  915. /** now, we actually define the class' constant pool */
  916. jbyte *pool_tags = (jbyte*) _Jv_AllocBytes (pool_count);
  917. _Jv_word *pool_data
  918. = (_Jv_word*) _Jv_AllocRawObj (pool_count * sizeof (_Jv_word));
  919. def->constants.tags = pool_tags;
  920. def->constants.data = pool_data;
  921. def->constants.size = pool_count;
  922. // Here we make a pass to collect the strings! We do this, because
  923. // internally in the GCJ runtime, classes are encoded with .'s not /'s.
  924. // Therefore, we first collect the strings, and then translate the rest
  925. // of the utf8-entries (thus not representing strings) from /-notation
  926. // to .-notation.
  927. for (int i = 1; i < pool_count; i++)
  928. {
  929. if (tags[i] == JV_CONSTANT_String)
  930. {
  931. unsigned char* str_data = bytes + offsets [i];
  932. int utf_index = get2u (str_data);
  933. check_tag (utf_index, JV_CONSTANT_Utf8);
  934. unsigned char *utf_data = bytes + offsets[utf_index];
  935. int len = get2u (utf_data);
  936. pool_data[i].utf8 = _Jv_makeUtf8Const ((char*)(utf_data+2), len);
  937. pool_tags[i] = JV_CONSTANT_String;
  938. }
  939. else
  940. {
  941. pool_tags[i] = JV_CONSTANT_Undefined;
  942. }
  943. }
  944. // and now, we scan everything else but strings & utf8-entries. This
  945. // leaves out those utf8-entries which are not used; which will be left
  946. // with a tag of JV_CONSTANT_Undefined in the class definition.
  947. for (int index = 1; index < pool_count; index++)
  948. {
  949. switch (tags[index])
  950. {
  951. case JV_CONSTANT_Undefined:
  952. case JV_CONSTANT_String:
  953. case JV_CONSTANT_Utf8:
  954. continue;
  955. default:
  956. prepare_pool_entry (index, tags[index]);
  957. }
  958. }
  959. }
  960. /* this is a recursive procedure, which will prepare pool entries as needed.
  961. Which is how we avoid initializing those entries which go unused.
  962. REWRITE is true iff this pool entry is the Utf8 representation of a
  963. class name or a signature.
  964. */
  965. void
  966. _Jv_ClassReader::prepare_pool_entry (int index, unsigned char this_tag,
  967. bool rewrite)
  968. {
  969. /* these two, pool_data and pool_tags, point into the class
  970. structure we are currently defining */
  971. unsigned char *pool_tags = (unsigned char*) def->constants.tags;
  972. _Jv_word *pool_data = def->constants.data;
  973. /* this entry was already prepared */
  974. if (pool_tags[index] == this_tag)
  975. return;
  976. /* this_data points to the constant-pool information for the current
  977. constant-pool entry */
  978. unsigned char *this_data = bytes + offsets[index];
  979. switch (this_tag)
  980. {
  981. case JV_CONSTANT_Utf8:
  982. {
  983. int len = get2u (this_data);
  984. char *s = ((char*) this_data)+2;
  985. pool_tags[index] = JV_CONSTANT_Utf8;
  986. if (! rewrite)
  987. {
  988. pool_data[index].utf8 = _Jv_makeUtf8Const (s, len);
  989. break;
  990. }
  991. // If REWRITE is set, it is because some other tag needs this
  992. // utf8-entry for type information: it is a class or a
  993. // signature. Thus, we translate /'s to .'s in order to
  994. // accomondate gcj's internal representation.
  995. char *buffer = (char*) __builtin_alloca (len);
  996. for (int i = 0; i < len; i++)
  997. {
  998. if (s[i] == '/')
  999. buffer[i] = '.';
  1000. else
  1001. buffer[i] = s[i];
  1002. }
  1003. pool_data[index].utf8 = _Jv_makeUtf8Const (buffer, len);
  1004. }
  1005. break;
  1006. case JV_CONSTANT_Class:
  1007. {
  1008. int utf_index = get2u (this_data);
  1009. check_tag (utf_index, JV_CONSTANT_Utf8);
  1010. prepare_pool_entry (utf_index, JV_CONSTANT_Utf8);
  1011. if (verify)
  1012. verify_classname (pool_data[utf_index].utf8);
  1013. pool_data[index].utf8 = pool_data[utf_index].utf8;
  1014. pool_tags[index] = JV_CONSTANT_Class;
  1015. }
  1016. break;
  1017. case JV_CONSTANT_String:
  1018. // already handled before...
  1019. break;
  1020. case JV_CONSTANT_Fieldref:
  1021. case JV_CONSTANT_Methodref:
  1022. case JV_CONSTANT_InterfaceMethodref:
  1023. {
  1024. int class_index = get2u (this_data);
  1025. int nat_index = get2u (this_data+2);
  1026. check_tag (class_index, JV_CONSTANT_Class);
  1027. prepare_pool_entry (class_index, JV_CONSTANT_Class);
  1028. check_tag (nat_index, JV_CONSTANT_NameAndType);
  1029. prepare_pool_entry (nat_index, JV_CONSTANT_NameAndType);
  1030. // here, verify the signature and identifier name
  1031. if (verify)
  1032. {
  1033. _Jv_ushort name_index, type_index;
  1034. _Jv_loadIndexes (&pool_data[nat_index],
  1035. name_index, type_index);
  1036. if (this_tag == JV_CONSTANT_Fieldref)
  1037. verify_field_signature (pool_data[type_index].utf8);
  1038. else
  1039. verify_method_signature (pool_data[type_index].utf8);
  1040. _Jv_Utf8Const* name = pool_data[name_index].utf8;
  1041. if (this_tag != JV_CONSTANT_Fieldref
  1042. && ( _Jv_equalUtf8Consts (name, clinit_name)
  1043. || _Jv_equalUtf8Consts (name, init_name)))
  1044. /* ignore */;
  1045. else
  1046. verify_identifier (pool_data[name_index].utf8);
  1047. }
  1048. _Jv_storeIndexes (&pool_data[index], class_index, nat_index);
  1049. pool_tags[index] = this_tag;
  1050. }
  1051. break;
  1052. case JV_CONSTANT_NameAndType:
  1053. {
  1054. _Jv_ushort name_index = get2u (this_data);
  1055. _Jv_ushort type_index = get2u (this_data+2);
  1056. check_tag (name_index, JV_CONSTANT_Utf8);
  1057. prepare_pool_entry (name_index, JV_CONSTANT_Utf8, false);
  1058. check_tag (type_index, JV_CONSTANT_Utf8);
  1059. prepare_pool_entry (type_index, JV_CONSTANT_Utf8);
  1060. _Jv_storeIndexes (&pool_data[index], name_index, type_index);
  1061. pool_tags[index] = JV_CONSTANT_NameAndType;
  1062. }
  1063. break;
  1064. case JV_CONSTANT_Float:
  1065. {
  1066. jfloat f = java::lang::Float::intBitsToFloat ((jint) get4 (this_data));
  1067. _Jv_storeFloat (&pool_data[index], f);
  1068. pool_tags[index] = JV_CONSTANT_Float;
  1069. }
  1070. break;
  1071. case JV_CONSTANT_Integer:
  1072. {
  1073. int i = get4 (this_data);
  1074. _Jv_storeInt (&pool_data[index], i);
  1075. pool_tags[index] = JV_CONSTANT_Integer;
  1076. }
  1077. break;
  1078. case JV_CONSTANT_Double:
  1079. {
  1080. jdouble d
  1081. = java::lang::Double::longBitsToDouble ((jlong) get8 (this_data));
  1082. _Jv_storeDouble (&pool_data[index], d);
  1083. pool_tags[index] = JV_CONSTANT_Double;
  1084. }
  1085. break;
  1086. case JV_CONSTANT_Long:
  1087. {
  1088. jlong i = get8 (this_data);
  1089. _Jv_storeLong (&pool_data[index], i);
  1090. pool_tags[index] = JV_CONSTANT_Long;
  1091. }
  1092. break;
  1093. default:
  1094. throw_class_format_error ("erroneous constant pool tag");
  1095. }
  1096. }
  1097. void
  1098. _Jv_ClassReader::handleClassBegin (int access_flags, int this_class, int super_class)
  1099. {
  1100. using namespace java::lang::reflect;
  1101. unsigned char *pool_tags = (unsigned char*) def->constants.tags;
  1102. _Jv_word *pool_data = def->constants.data;
  1103. check_tag (this_class, JV_CONSTANT_Class);
  1104. _Jv_Utf8Const *loadedName = pool_data[this_class].utf8;
  1105. // was ClassLoader.defineClass called with an expected class name?
  1106. if (def->name == 0)
  1107. {
  1108. jclass orig = def->loader->findLoadedClass(loadedName->toString());
  1109. if (orig == 0)
  1110. {
  1111. def->name = loadedName;
  1112. }
  1113. else
  1114. {
  1115. jstring msg = JvNewStringUTF ("anonymous "
  1116. "class data denotes "
  1117. "existing class ");
  1118. msg = msg->concat (orig->getName ());
  1119. throw_no_class_def_found_error (msg);
  1120. }
  1121. }
  1122. // assert that the loaded class has the expected name, 5.3.5
  1123. else if (! _Jv_equalUtf8Consts (loadedName, def->name))
  1124. {
  1125. jstring msg = JvNewStringUTF ("loaded class ");
  1126. msg = msg->concat (def->getName ());
  1127. msg = msg->concat (_Jv_NewStringUTF (" was in fact named "));
  1128. jstring klass_name = loadedName->toString();
  1129. msg = msg->concat (klass_name);
  1130. throw_no_class_def_found_error (msg);
  1131. }
  1132. def->accflags = access_flags | java::lang::reflect::Modifier::INTERPRETED;
  1133. pool_data[this_class].clazz = def;
  1134. pool_tags[this_class] = JV_CONSTANT_ResolvedClass;
  1135. if (super_class == 0)
  1136. {
  1137. // Note that this is ok if we are defining java.lang.Object.
  1138. // But there is no way to have this class be interpreted.
  1139. throw_class_format_error ("no superclass reference");
  1140. }
  1141. def->state = JV_STATE_PRELOADING;
  1142. // Register this class with its defining loader as well (despite the
  1143. // name of the function we're calling), so that super class lookups
  1144. // work properly. If there is an error, our caller will unregister
  1145. // this class from the class loader. Also, we don't need to hold a
  1146. // lock here, as our caller has acquired it.
  1147. _Jv_RegisterInitiatingLoader (def, def->loader);
  1148. // Note that we found a name so that unregistration can happen if
  1149. // needed.
  1150. *found_name = def->name;
  1151. if (super_class != 0)
  1152. {
  1153. // Load the superclass.
  1154. check_tag (super_class, JV_CONSTANT_Class);
  1155. _Jv_Utf8Const* super_name = pool_data[super_class].utf8;
  1156. // Load the superclass using our defining loader.
  1157. jclass the_super = _Jv_FindClass (super_name, def->loader);
  1158. // This will establish that we are allowed to be a subclass,
  1159. // and check for class circularity error.
  1160. checkExtends (def, the_super);
  1161. // Note: for an interface we will find Object as the
  1162. // superclass. We still check it above to ensure class file
  1163. // validity, but we simply assign `null' to the actual field in
  1164. // this case.
  1165. def->superclass = (((access_flags & Modifier::INTERFACE))
  1166. ? NULL : the_super);
  1167. pool_data[super_class].clazz = the_super;
  1168. pool_tags[super_class] = JV_CONSTANT_ResolvedClass;
  1169. }
  1170. // Now we've come past the circularity problem, we can
  1171. // now say that we're loading.
  1172. def->state = JV_STATE_LOADING;
  1173. def->notifyAll ();
  1174. }
  1175. ///// Implements the checks described in sect. 5.3.5.3
  1176. void
  1177. _Jv_ClassReader::checkExtends (jclass sub, jclass super)
  1178. {
  1179. using namespace java::lang::reflect;
  1180. _Jv_Linker::wait_for_state (super, JV_STATE_LOADING);
  1181. // Having an interface or a final class as a superclass is no good.
  1182. if ((super->accflags & (Modifier::INTERFACE | Modifier::FINAL)) != 0)
  1183. {
  1184. throw_incompatible_class_change_error (sub->getName ());
  1185. }
  1186. // If the super class is not public, we need to check some more.
  1187. if ((super->accflags & Modifier::PUBLIC) == 0)
  1188. {
  1189. // With package scope, the classes must have the same class
  1190. // loader.
  1191. if ( sub->loader != super->loader
  1192. || !_Jv_ClassNameSamePackage (sub->name, super->name))
  1193. {
  1194. throw_incompatible_class_change_error (sub->getName ());
  1195. }
  1196. }
  1197. for (; super != 0; super = super->getSuperclass ())
  1198. {
  1199. if (super == sub)
  1200. throw_class_circularity_error (sub->getName ());
  1201. }
  1202. }
  1203. void _Jv_ClassReader::handleInterfacesBegin (int count)
  1204. {
  1205. def->interfaces = (jclass*) _Jv_AllocRawObj (count*sizeof (jclass));
  1206. def->interface_count = count;
  1207. }
  1208. void _Jv_ClassReader::handleInterface (int if_number, int offset)
  1209. {
  1210. _Jv_word * pool_data = def->constants.data;
  1211. unsigned char * pool_tags = (unsigned char*) def->constants.tags;
  1212. jclass the_interface;
  1213. if (pool_tags[offset] == JV_CONSTANT_Class)
  1214. {
  1215. _Jv_Utf8Const* name = pool_data[offset].utf8;
  1216. the_interface = _Jv_FindClass (name, def->loader);
  1217. }
  1218. else if (pool_tags[offset] == JV_CONSTANT_ResolvedClass)
  1219. {
  1220. the_interface = pool_data[offset].clazz;
  1221. }
  1222. else
  1223. {
  1224. throw_no_class_def_found_error ("erroneous constant pool tag");
  1225. }
  1226. // checks the validity of the_interface, and that we are in fact
  1227. // allowed to implement that interface.
  1228. checkImplements (def, the_interface);
  1229. pool_data[offset].clazz = the_interface;
  1230. pool_tags[offset] = JV_CONSTANT_ResolvedClass;
  1231. def->interfaces[if_number] = the_interface;
  1232. }
  1233. void
  1234. _Jv_ClassReader::checkImplements (jclass sub, jclass super)
  1235. {
  1236. using namespace java::lang::reflect;
  1237. // well, it *must* be an interface
  1238. if ((super->accflags & Modifier::INTERFACE) == 0)
  1239. {
  1240. throw_incompatible_class_change_error (sub->getName ());
  1241. }
  1242. // if it has package scope, it must also be defined by the
  1243. // same loader.
  1244. if ((super->accflags & Modifier::PUBLIC) == 0)
  1245. {
  1246. if ( sub->loader != super->loader
  1247. || !_Jv_ClassNameSamePackage (sub->name, super->name))
  1248. {
  1249. throw_incompatible_class_change_error (sub->getName ());
  1250. }
  1251. }
  1252. // FIXME: add interface circularity check here
  1253. if (sub == super)
  1254. {
  1255. throw_class_circularity_error (sub->getName ());
  1256. }
  1257. }
  1258. void _Jv_ClassReader::handleFieldsBegin (int count)
  1259. {
  1260. def->fields = (_Jv_Field*) _Jv_AllocRawObj (count * sizeof (_Jv_Field));
  1261. def->field_count = count;
  1262. def_interp->field_initializers
  1263. = (_Jv_ushort*) _Jv_AllocRawObj (count * sizeof (_Jv_ushort));
  1264. for (int i = 0; i < count; i++)
  1265. def_interp->field_initializers[i] = (_Jv_ushort) 0;
  1266. }
  1267. void _Jv_ClassReader::handleField (int field_no,
  1268. int flags,
  1269. int name,
  1270. int desc,
  1271. int *fieldmap)
  1272. {
  1273. using namespace java::lang::reflect;
  1274. _Jv_word *pool_data = def->constants.data;
  1275. _Jv_Field *field = &def->fields[fieldmap[field_no]];
  1276. _Jv_Utf8Const *field_name = pool_data[name].utf8;
  1277. field->name = field_name;
  1278. // Ignore flags we don't know about.
  1279. field->flags = flags & (Field::FIELD_MODIFIERS
  1280. | Modifier::SYNTHETIC
  1281. | Modifier::ENUM);
  1282. _Jv_Utf8Const* sig = pool_data[desc].utf8;
  1283. if (verify)
  1284. {
  1285. verify_identifier (field_name);
  1286. for (int i = 0; i < field_no; ++i)
  1287. {
  1288. if (_Jv_equalUtf8Consts (field_name, def->fields[fieldmap[i]].name)
  1289. && _Jv_equalUtf8Consts (sig,
  1290. // We know the other fields are
  1291. // unresolved.
  1292. (_Jv_Utf8Const *) def->fields[i].type))
  1293. throw_class_format_error ("duplicate field name");
  1294. }
  1295. // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed.
  1296. if (1 < ( ((field->flags & Modifier::PUBLIC) ? 1 : 0)
  1297. +((field->flags & Modifier::PRIVATE) ? 1 : 0)
  1298. +((field->flags & Modifier::PROTECTED) ? 1 : 0)))
  1299. throw_class_format_error ("erroneous field access flags");
  1300. // FIXME: JVM spec S4.5: Verify ACC_FINAL and ACC_VOLATILE are not
  1301. // both set. Verify modifiers for interface fields.
  1302. }
  1303. if (verify)
  1304. verify_field_signature (sig);
  1305. // field->type is really a jclass, but while it is still
  1306. // unresolved we keep an _Jv_Utf8Const* instead.
  1307. field->type = (jclass) sig;
  1308. field->flags |= _Jv_FIELD_UNRESOLVED_FLAG;
  1309. field->u.boffset = 0;
  1310. }
  1311. void _Jv_ClassReader::handleConstantValueAttribute (int field_index,
  1312. int value,
  1313. bool *found_value)
  1314. {
  1315. using namespace java::lang::reflect;
  1316. _Jv_Field *field = &def->fields[field_index];
  1317. if ((field->flags & (Modifier::STATIC
  1318. | Modifier::FINAL
  1319. | Modifier::PRIVATE)) == 0)
  1320. {
  1321. // Ignore, as per vmspec #4.7.2
  1322. return;
  1323. }
  1324. // do not allow multiple constant fields!
  1325. if (*found_value)
  1326. throw_class_format_error ("field has multiple ConstantValue attributes");
  1327. *found_value = true;
  1328. def_interp->field_initializers[field_index] = value;
  1329. /* type check the initializer */
  1330. if (value <= 0 || value >= pool_count)
  1331. throw_class_format_error ("erroneous ConstantValue attribute");
  1332. /* FIXME: do the rest */
  1333. }
  1334. void
  1335. _Jv_ClassReader::handleMethodsBegin (int count)
  1336. {
  1337. def->methods = (_Jv_Method *) _Jv_AllocRawObj (sizeof (_Jv_Method) * count);
  1338. def_interp->interpreted_methods
  1339. = (_Jv_MethodBase **) _Jv_AllocRawObj (sizeof (_Jv_MethodBase *)
  1340. * count);
  1341. for (int i = 0; i < count; i++)
  1342. {
  1343. def_interp->interpreted_methods[i] = 0;
  1344. def->methods[i].index = (_Jv_ushort) -1;
  1345. }
  1346. def->method_count = count;
  1347. }
  1348. void _Jv_ClassReader::handleMethod
  1349. (int mth_index, int accflags, int name, int desc)
  1350. {
  1351. using namespace java::lang::reflect;
  1352. _Jv_word *pool_data = def->constants.data;
  1353. _Jv_Method *method = &def->methods[mth_index];
  1354. check_tag (name, JV_CONSTANT_Utf8);
  1355. prepare_pool_entry (name, JV_CONSTANT_Utf8, false);
  1356. method->name = pool_data[name].utf8;
  1357. check_tag (desc, JV_CONSTANT_Utf8);
  1358. prepare_pool_entry (desc, JV_CONSTANT_Utf8);
  1359. method->signature = pool_data[desc].utf8;
  1360. // ignore unknown flags
  1361. method->accflags = accflags & (Method::METHOD_MODIFIERS
  1362. | Modifier::BRIDGE
  1363. | Modifier::SYNTHETIC
  1364. | Modifier::VARARGS);
  1365. // Initialize...
  1366. method->ncode = 0;
  1367. method->throws = NULL;
  1368. if (verify)
  1369. {
  1370. if (_Jv_equalUtf8Consts (method->name, clinit_name)
  1371. || _Jv_equalUtf8Consts (method->name, init_name))
  1372. /* ignore */;
  1373. else
  1374. verify_identifier (method->name);
  1375. verify_method_signature (method->signature);
  1376. for (int i = 0; i < mth_index; ++i)
  1377. {
  1378. if (_Jv_equalUtf8Consts (method->name, def->methods[i].name)
  1379. && _Jv_equalUtf8Consts (method->signature,
  1380. def->methods[i].signature))
  1381. throw_class_format_error ("duplicate method");
  1382. }
  1383. // At most one of PUBLIC, PRIVATE, or PROTECTED is allowed.
  1384. if (1 < ( ((method->accflags & Modifier::PUBLIC) ? 1 : 0)
  1385. +((method->accflags & Modifier::PRIVATE) ? 1 : 0)
  1386. +((method->accflags & Modifier::PROTECTED) ? 1 : 0)))
  1387. throw_class_format_error ("erroneous method access flags");
  1388. // FIXME: JVM spec S4.6: if ABSTRACT modifier is set, verify other
  1389. // flags are not set. Verify flags for interface methods. Verify
  1390. // modifiers for initializers.
  1391. }
  1392. }
  1393. void _Jv_ClassReader::handleCodeAttribute
  1394. (int method_index, int max_stack, int max_locals,
  1395. int code_start, int code_length, int exc_table_length)
  1396. {
  1397. int size = _Jv_InterpMethod::size (exc_table_length, code_length);
  1398. _Jv_InterpMethod *method =
  1399. (_Jv_InterpMethod*) (_Jv_AllocRawObj (size));
  1400. method->max_stack = max_stack;
  1401. method->max_locals = max_locals;
  1402. method->code_length = code_length;
  1403. method->exc_count = exc_table_length;
  1404. method->is_15 = is_15;
  1405. method->defining_class = def;
  1406. method->self = &def->methods[method_index];
  1407. method->prepared = NULL;
  1408. method->line_table_len = 0;
  1409. method->line_table = NULL;
  1410. #ifdef DIRECT_THREADED
  1411. method->thread_count = 0;
  1412. #endif
  1413. // grab the byte code!
  1414. memcpy ((void*) method->bytecode (),
  1415. (void*) (bytes+code_start),
  1416. code_length);
  1417. def_interp->interpreted_methods[method_index] = method;
  1418. if ((method->self->accflags & java::lang::reflect::Modifier::STATIC))
  1419. {
  1420. // Precompute the ncode field for a static method. This lets us
  1421. // call a static method of an interpreted class from precompiled
  1422. // code without first resolving the class (that will happen
  1423. // during class initialization instead).
  1424. method->self->ncode = method->ncode (def);
  1425. }
  1426. }
  1427. void _Jv_ClassReader::handleExceptionTableEntry
  1428. (int method_index, int exc_index,
  1429. int start_pc, int end_pc, int handler_pc, int catch_type)
  1430. {
  1431. _Jv_InterpMethod *method = reinterpret_cast<_Jv_InterpMethod *>
  1432. (def_interp->interpreted_methods[method_index]);
  1433. _Jv_InterpException *exc = method->exceptions ();
  1434. exc[exc_index].start_pc.i = start_pc;
  1435. exc[exc_index].end_pc.i = end_pc;
  1436. exc[exc_index].handler_pc.i = handler_pc;
  1437. exc[exc_index].handler_type.i = catch_type;
  1438. }
  1439. void _Jv_ClassReader::handleMethodsEnd ()
  1440. {
  1441. using namespace java::lang::reflect;
  1442. for (int i = 0; i < def->method_count; i++)
  1443. {
  1444. _Jv_Method *method = &def->methods[i];
  1445. if ((method->accflags & Modifier::NATIVE) != 0)
  1446. {
  1447. if (def_interp->interpreted_methods[i] != 0)
  1448. throw_class_format_error ("code provided for native method");
  1449. else
  1450. {
  1451. _Jv_JNIMethod *m = (_Jv_JNIMethod *)
  1452. _Jv_AllocRawObj (sizeof (_Jv_JNIMethod));
  1453. m->defining_class = def;
  1454. m->self = method;
  1455. m->function = NULL;
  1456. def_interp->interpreted_methods[i] = m;
  1457. if ((method->accflags & Modifier::STATIC))
  1458. {
  1459. // Precompute the ncode field for a static method.
  1460. // This lets us call a static method of an
  1461. // interpreted class from precompiled code without
  1462. // first resolving the class (that will happen
  1463. // during class initialization instead).
  1464. method->ncode = m->ncode (def);
  1465. }
  1466. }
  1467. }
  1468. else if ((method->accflags & Modifier::ABSTRACT) != 0)
  1469. {
  1470. if (def_interp->interpreted_methods[i] != 0)
  1471. throw_class_format_error ("code provided for abstract method");
  1472. method->ncode = (void *) &_Jv_ThrowAbstractMethodError;
  1473. }
  1474. else
  1475. {
  1476. if (def_interp->interpreted_methods[i] == 0)
  1477. throw_class_format_error ("method with no code");
  1478. }
  1479. }
  1480. }
  1481. void _Jv_ClassReader::throw_class_format_error (const char *msg)
  1482. {
  1483. jstring str;
  1484. if (def->name != NULL)
  1485. {
  1486. jsize mlen = strlen (msg);
  1487. unsigned char* data = (unsigned char*) def->name->chars();
  1488. int ulen = def->name->len();
  1489. unsigned char* limit = data + ulen;
  1490. jsize nlen = _Jv_strLengthUtf8 ((char *) data, ulen);
  1491. jsize len = nlen + mlen + 3;
  1492. str = JvAllocString(len);
  1493. jchar *chrs = JvGetStringChars(str);
  1494. while (data < limit)
  1495. *chrs++ = UTF8_GET(data, limit);
  1496. *chrs++ = ' ';
  1497. *chrs++ = '(';
  1498. for (;;)
  1499. {
  1500. char c = *msg++;
  1501. if (c == 0)
  1502. break;
  1503. *chrs++ = c & 0xFFFF;
  1504. }
  1505. *chrs++ = ')';
  1506. }
  1507. else
  1508. str = JvNewStringLatin1 (msg);
  1509. ::throw_class_format_error (str);
  1510. }
  1511. /** Here we define the exceptions that can be thrown */
  1512. static void
  1513. throw_no_class_def_found_error (jstring msg)
  1514. {
  1515. throw (msg
  1516. ? new java::lang::NoClassDefFoundError (msg)
  1517. : new java::lang::NoClassDefFoundError);
  1518. }
  1519. static void
  1520. throw_no_class_def_found_error (const char *msg)
  1521. {
  1522. throw_no_class_def_found_error (JvNewStringLatin1 (msg));
  1523. }
  1524. static void
  1525. throw_class_format_error (jstring msg)
  1526. {
  1527. throw (msg
  1528. ? new java::lang::ClassFormatError (msg)
  1529. : new java::lang::ClassFormatError);
  1530. }
  1531. static void
  1532. throw_internal_error (const char *msg)
  1533. {
  1534. throw new java::lang::InternalError (JvNewStringLatin1 (msg));
  1535. }
  1536. static void
  1537. throw_incompatible_class_change_error (jstring msg)
  1538. {
  1539. throw new java::lang::IncompatibleClassChangeError (msg);
  1540. }
  1541. static void
  1542. throw_class_circularity_error (jstring msg)
  1543. {
  1544. throw new java::lang::ClassCircularityError (msg);
  1545. }
  1546. #endif /* INTERPRETER */
  1547. /** This section takes care of verifying integrity of identifiers,
  1548. signatures, field ddescriptors, and class names */
  1549. #define UTF8_PEEK(PTR, LIMIT) \
  1550. ({ unsigned char* xxkeep = (PTR); \
  1551. int xxch = UTF8_GET(PTR,LIMIT); \
  1552. PTR = xxkeep; xxch; })
  1553. /* Verify one element of a type descriptor or signature. */
  1554. static unsigned char*
  1555. _Jv_VerifyOne (unsigned char* ptr, unsigned char* limit, bool void_ok)
  1556. {
  1557. if (ptr >= limit)
  1558. return 0;
  1559. int ch = UTF8_GET (ptr, limit);
  1560. switch (ch)
  1561. {
  1562. case 'V':
  1563. if (! void_ok)
  1564. return 0;
  1565. case 'S': case 'B': case 'I': case 'J':
  1566. case 'Z': case 'C': case 'F': case 'D':
  1567. break;
  1568. case 'L':
  1569. {
  1570. unsigned char *start = ptr, *end;
  1571. do
  1572. {
  1573. if (ptr > limit)
  1574. return 0;
  1575. end = ptr;
  1576. if ((ch = UTF8_GET (ptr, limit)) == -1)
  1577. return 0;
  1578. }
  1579. while (ch != ';');
  1580. if (! _Jv_VerifyClassName (start, (unsigned short) (end-start)))
  1581. return 0;
  1582. }
  1583. break;
  1584. case '[':
  1585. return _Jv_VerifyOne (ptr, limit, false);
  1586. break;
  1587. default:
  1588. return 0;
  1589. }
  1590. return ptr;
  1591. }
  1592. /* Verification and loading procedures. */
  1593. bool
  1594. _Jv_VerifyFieldSignature (_Jv_Utf8Const*sig)
  1595. {
  1596. unsigned char* ptr = (unsigned char*) sig->chars();
  1597. unsigned char* limit = ptr + sig->len();
  1598. ptr = _Jv_VerifyOne (ptr, limit, false);
  1599. return ptr == limit;
  1600. }
  1601. bool
  1602. _Jv_VerifyMethodSignature (_Jv_Utf8Const*sig)
  1603. {
  1604. unsigned char* ptr = (unsigned char*) sig->chars();
  1605. unsigned char* limit = ptr + sig->len();
  1606. if (ptr == limit || UTF8_GET(ptr,limit) != '(')
  1607. return false;
  1608. while (ptr && UTF8_PEEK (ptr, limit) != ')')
  1609. ptr = _Jv_VerifyOne (ptr, limit, false);
  1610. if (! ptr || UTF8_GET (ptr, limit) != ')')
  1611. return false;
  1612. // get the return type
  1613. ptr = _Jv_VerifyOne (ptr, limit, true);
  1614. return ptr == limit;
  1615. }
  1616. /* We try to avoid calling the Character methods all the time, in
  1617. fact, they will only be called for non-standard things. */
  1618. static __inline__ int
  1619. is_identifier_start (int c)
  1620. {
  1621. unsigned int ch = (unsigned)c;
  1622. if ((ch - 0x41U) < 29U) /* A ... Z */
  1623. return 1;
  1624. if ((ch - 0x61U) < 29U) /* a ... z */
  1625. return 1;
  1626. if (ch == 0x5FU) /* _ */
  1627. return 1;
  1628. return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
  1629. }
  1630. static __inline__ int
  1631. is_identifier_part (int c)
  1632. {
  1633. unsigned int ch = (unsigned)c;
  1634. if ((ch - 0x41U) < 29U) /* A ... Z */
  1635. return 1;
  1636. if ((ch - 0x61U) < 29U) /* a ... z */
  1637. return 1;
  1638. if ((ch - 0x30) < 10U) /* 0 .. 9 */
  1639. return 1;
  1640. if (ch == 0x5FU || ch == 0x24U) /* _ $ */
  1641. return 1;
  1642. return java::lang::Character::isJavaIdentifierStart ((jchar) ch);
  1643. }
  1644. bool
  1645. _Jv_VerifyIdentifier (_Jv_Utf8Const* name)
  1646. {
  1647. unsigned char *ptr = (unsigned char*) name->chars();
  1648. unsigned char *limit = (unsigned char*) name->limit();
  1649. int ch;
  1650. if ((ch = UTF8_GET (ptr, limit))==-1
  1651. || ! is_identifier_start (ch))
  1652. return false;
  1653. while (ptr != limit)
  1654. {
  1655. if ((ch = UTF8_GET (ptr, limit))==-1
  1656. || ! is_identifier_part (ch))
  1657. return false;
  1658. }
  1659. return true;
  1660. }
  1661. bool
  1662. _Jv_VerifyClassName (unsigned char* ptr, _Jv_ushort length)
  1663. {
  1664. unsigned char *limit = ptr+length;
  1665. int ch;
  1666. if ('[' == UTF8_PEEK (ptr, limit))
  1667. {
  1668. unsigned char *end = _Jv_VerifyOne (++ptr, limit, false);
  1669. // _Jv_VerifyOne must leave us looking at the terminating nul
  1670. // byte.
  1671. if (! end || *end)
  1672. return false;
  1673. else
  1674. return true;
  1675. }
  1676. next_level:
  1677. for (;;) {
  1678. if ((ch = UTF8_GET (ptr, limit))==-1)
  1679. return false;
  1680. if (! is_identifier_start (ch))
  1681. return false;
  1682. for (;;) {
  1683. if (ptr == limit)
  1684. return true;
  1685. else if ((ch = UTF8_GET (ptr, limit))==-1)
  1686. return false;
  1687. else if (ch == '.')
  1688. goto next_level;
  1689. else if (! is_identifier_part (ch))
  1690. return false;
  1691. }
  1692. }
  1693. }
  1694. bool
  1695. _Jv_VerifyClassName (_Jv_Utf8Const *name)
  1696. {
  1697. return _Jv_VerifyClassName ((unsigned char*)name->chars(), name->len());
  1698. }
  1699. /* Returns true, if NAME1 and NAME2 represent classes in the same
  1700. package. Neither NAME2 nor NAME2 may name an array type. */
  1701. bool
  1702. _Jv_ClassNameSamePackage (_Jv_Utf8Const *name1, _Jv_Utf8Const *name2)
  1703. {
  1704. unsigned char* ptr1 = (unsigned char*) name1->chars();
  1705. unsigned char* limit1 = (unsigned char*) name1->limit();
  1706. unsigned char* last1 = ptr1;
  1707. // scan name1, and find the last occurrence of '.'
  1708. while (ptr1 < limit1) {
  1709. int ch1 = UTF8_GET (ptr1, limit1);
  1710. if (ch1 == '.')
  1711. last1 = ptr1;
  1712. else if (ch1 == -1)
  1713. return false;
  1714. }
  1715. // Now the length of NAME1's package name is LEN.
  1716. int len = last1 - (unsigned char*) name1->chars();
  1717. // If this is longer than NAME2, then we're off.
  1718. if (len > name2->len())
  1719. return false;
  1720. // Then compare the first len bytes for equality.
  1721. if (memcmp ((void*) name1->chars(), (void*) name2->chars(), len) == 0)
  1722. {
  1723. // Check that there are no .'s after position LEN in NAME2.
  1724. unsigned char* ptr2 = (unsigned char*) name2->chars() + len;
  1725. unsigned char* limit2 = (unsigned char*) name2->limit();
  1726. while (ptr2 < limit2)
  1727. {
  1728. int ch2 = UTF8_GET (ptr2, limit2);
  1729. if (ch2 == -1 || ch2 == '.')
  1730. return false;
  1731. }
  1732. return true;
  1733. }
  1734. return false;
  1735. }