verify.cc 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237
  1. // verify.cc - verify bytecode
  2. /* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 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. // Written by Tom Tromey <tromey@redhat.com>
  8. // Define VERIFY_DEBUG to enable debugging output.
  9. #include <config.h>
  10. #include <string.h>
  11. #include <jvm.h>
  12. #include <gcj/cni.h>
  13. #include <java-insns.h>
  14. #include <java-interp.h>
  15. // On Solaris 10/x86, <signal.h> indirectly includes <ia32/sys/reg.h>, which
  16. // defines PC since g++ predefines __EXTENSIONS__. Undef here to avoid clash
  17. // with PC member of class _Jv_BytecodeVerifier below.
  18. #undef PC
  19. #ifdef INTERPRETER
  20. #include <java/lang/Class.h>
  21. #include <java/lang/VerifyError.h>
  22. #include <java/lang/Throwable.h>
  23. #include <java/lang/reflect/Modifier.h>
  24. #include <java/lang/StringBuffer.h>
  25. #include <java/lang/NoClassDefFoundError.h>
  26. #ifdef VERIFY_DEBUG
  27. #include <stdio.h>
  28. #endif /* VERIFY_DEBUG */
  29. // This is used to mark states which are not scheduled for
  30. // verification.
  31. #define INVALID_STATE ((state *) -1)
  32. static void debug_print (const char *fmt, ...)
  33. __attribute__ ((format (printf, 1, 2)));
  34. static inline void
  35. debug_print (MAYBE_UNUSED const char *fmt, ...)
  36. {
  37. #ifdef VERIFY_DEBUG
  38. va_list ap;
  39. va_start (ap, fmt);
  40. vfprintf (stderr, fmt, ap);
  41. va_end (ap);
  42. #endif /* VERIFY_DEBUG */
  43. }
  44. // This started as a fairly ordinary verifier, and for the most part
  45. // it remains so. It works in the obvious way, by modeling the effect
  46. // of each opcode as it is encountered. For most opcodes, this is a
  47. // straightforward operation.
  48. //
  49. // This verifier does not do type merging. It used to, but this
  50. // results in difficulty verifying some relatively simple code
  51. // involving interfaces, and it pushed some verification work into the
  52. // interpreter.
  53. //
  54. // Instead of merging reference types, when we reach a point where two
  55. // flows of control merge, we simply keep the union of reference types
  56. // from each branch. Then, when we need to verify a fact about a
  57. // reference on the stack (e.g., that it is compatible with the
  58. // argument type of a method), we check to ensure that all possible
  59. // types satisfy the requirement.
  60. //
  61. // Another area this verifier differs from the norm is in its handling
  62. // of subroutines. The JVM specification has some confusing things to
  63. // say about subroutines. For instance, it makes claims about not
  64. // allowing subroutines to merge and it rejects recursive subroutines.
  65. // For the most part these are red herrings; we used to try to follow
  66. // these things but they lead to problems. For example, the notion of
  67. // "being in a subroutine" is not well-defined: is an exception
  68. // handler in a subroutine? If you never execute the `ret' but
  69. // instead `goto 1' do you remain in the subroutine?
  70. //
  71. // For clarity on what is really required for type safety, read
  72. // "Simple Verification Technique for Complex Java Bytecode
  73. // Subroutines" by Alessandro Coglio. Among other things this paper
  74. // shows that recursive subroutines are not harmful to type safety.
  75. // We implement something similar to what he proposes. Note that this
  76. // means that this verifier will accept code that is rejected by some
  77. // other verifiers.
  78. //
  79. // For those not wanting to read the paper, the basic observation is
  80. // that we can maintain split states in subroutines. We maintain one
  81. // state for each calling `jsr'. In other words, we re-verify a
  82. // subroutine once for each caller, using the exact types held by the
  83. // callers (as opposed to the old approach of merging types and
  84. // keeping a bitmap registering what did or did not change). This
  85. // approach lets us continue to verify correctly even when a
  86. // subroutine is exited via `goto' or `athrow' and not `ret'.
  87. //
  88. // In some other areas the JVM specification is (mildly) incorrect,
  89. // so we diverge. For instance, you cannot
  90. // violate type safety by allocating an object with `new' and then
  91. // failing to initialize it, no matter how one branches or where one
  92. // stores the uninitialized reference. See "Improving the official
  93. // specification of Java bytecode verification" by Alessandro Coglio.
  94. //
  95. // Note that there's no real point in enforcing that padding bytes or
  96. // the mystery byte of invokeinterface must be 0, but we do that
  97. // regardless.
  98. //
  99. // The verifier is currently neither completely lazy nor eager when it
  100. // comes to loading classes. It tries to represent types by name when
  101. // possible, and then loads them when it needs to verify a fact about
  102. // the type. Checking types by name is valid because we only use
  103. // names which come from the current class' constant pool. Since all
  104. // such names are looked up using the same class loader, there is no
  105. // danger that we might be fooled into comparing different types with
  106. // the same name.
  107. //
  108. // In the future we plan to allow for a completely lazy mode of
  109. // operation, where the verifier will construct a list of type
  110. // assertions to be checked later.
  111. //
  112. // Some test cases for the verifier live in the "verify" module of the
  113. // Mauve test suite. However, some of these are presently
  114. // (2004-01-20) believed to be incorrect. (More precisely the notion
  115. // of "correct" is not well-defined, and this verifier differs from
  116. // others while remaining type-safe.) Some other tests live in the
  117. // libgcj test suite.
  118. class _Jv_BytecodeVerifier
  119. {
  120. private:
  121. static const int FLAG_INSN_START = 1;
  122. static const int FLAG_BRANCH_TARGET = 2;
  123. struct state;
  124. struct type;
  125. struct linked_utf8;
  126. struct ref_intersection;
  127. template<typename T>
  128. struct linked
  129. {
  130. T *val;
  131. linked<T> *next;
  132. };
  133. // The current PC.
  134. int PC;
  135. // The PC corresponding to the start of the current instruction.
  136. int start_PC;
  137. // The current state of the stack, locals, etc.
  138. state *current_state;
  139. // At each branch target we keep a linked list of all the states we
  140. // can process at that point. We'll only have multiple states at a
  141. // given PC if they both have different return-address types in the
  142. // same stack or local slot. This array is indexed by PC and holds
  143. // the list of all such states.
  144. linked<state> **states;
  145. // We keep a linked list of all the states which we must reverify.
  146. // This is the head of the list.
  147. state *next_verify_state;
  148. // We keep some flags for each instruction. The values are the
  149. // FLAG_* constants defined above. This is an array indexed by PC.
  150. char *flags;
  151. // The bytecode itself.
  152. unsigned char *bytecode;
  153. // The exceptions.
  154. _Jv_InterpException *exception;
  155. // Defining class.
  156. jclass current_class;
  157. // This method.
  158. _Jv_InterpMethod *current_method;
  159. // A linked list of utf8 objects we allocate.
  160. linked<_Jv_Utf8Const> *utf8_list;
  161. // A linked list of all ref_intersection objects we allocate.
  162. ref_intersection *isect_list;
  163. // Create a new Utf-8 constant and return it. We do this to avoid
  164. // having our Utf-8 constants prematurely collected.
  165. _Jv_Utf8Const *make_utf8_const (char *s, int len)
  166. {
  167. linked<_Jv_Utf8Const> *lu = (linked<_Jv_Utf8Const> *)
  168. _Jv_Malloc (sizeof (linked<_Jv_Utf8Const>)
  169. + _Jv_Utf8Const::space_needed(s, len));
  170. _Jv_Utf8Const *r = (_Jv_Utf8Const *) (lu + 1);
  171. r->init(s, len);
  172. lu->val = r;
  173. lu->next = utf8_list;
  174. utf8_list = lu;
  175. return r;
  176. }
  177. __attribute__ ((__noreturn__)) void verify_fail (const char *s, jint pc = -1)
  178. {
  179. using namespace java::lang;
  180. StringBuffer *buf = new StringBuffer ();
  181. buf->append (JvNewStringLatin1 ("verification failed"));
  182. if (pc == -1)
  183. pc = start_PC;
  184. if (pc != -1)
  185. {
  186. buf->append (JvNewStringLatin1 (" at PC "));
  187. buf->append (pc);
  188. }
  189. _Jv_InterpMethod *method = current_method;
  190. buf->append (JvNewStringLatin1 (" in "));
  191. buf->append (current_class->getName());
  192. buf->append ((jchar) ':');
  193. buf->append (method->get_method()->name->toString());
  194. buf->append ((jchar) '(');
  195. buf->append (method->get_method()->signature->toString());
  196. buf->append ((jchar) ')');
  197. buf->append (JvNewStringLatin1 (": "));
  198. buf->append (JvNewStringLatin1 (s));
  199. throw new java::lang::VerifyError (buf->toString ());
  200. }
  201. // This enum holds a list of tags for all the different types we
  202. // need to handle. Reference types are treated specially by the
  203. // type class.
  204. enum type_val
  205. {
  206. void_type,
  207. // The values for primitive types are chosen to correspond to values
  208. // specified to newarray.
  209. boolean_type = 4,
  210. char_type = 5,
  211. float_type = 6,
  212. double_type = 7,
  213. byte_type = 8,
  214. short_type = 9,
  215. int_type = 10,
  216. long_type = 11,
  217. // Used when overwriting second word of a double or long in the
  218. // local variables. Also used after merging local variable states
  219. // to indicate an unusable value.
  220. unsuitable_type,
  221. return_address_type,
  222. // This is the second word of a two-word value, i.e., a double or
  223. // a long.
  224. continuation_type,
  225. // Everything after `reference_type' must be a reference type.
  226. reference_type,
  227. null_type,
  228. uninitialized_reference_type
  229. };
  230. // This represents a merged class type. Some verifiers (including
  231. // earlier versions of this one) will compute the intersection of
  232. // two class types when merging states. However, this loses
  233. // critical information about interfaces implemented by the various
  234. // classes. So instead we keep track of all the actual classes that
  235. // have been merged.
  236. struct ref_intersection
  237. {
  238. // Whether or not this type has been resolved.
  239. bool is_resolved;
  240. // Actual type data.
  241. union
  242. {
  243. // For a resolved reference type, this is a pointer to the class.
  244. jclass klass;
  245. // For other reference types, this it the name of the class.
  246. _Jv_Utf8Const *name;
  247. } data;
  248. // Link to the next reference in the intersection.
  249. ref_intersection *ref_next;
  250. // This is used to keep track of all the allocated
  251. // ref_intersection objects, so we can free them.
  252. // FIXME: we should allocate these in chunks.
  253. ref_intersection *alloc_next;
  254. ref_intersection (jclass klass, _Jv_BytecodeVerifier *verifier)
  255. : ref_next (NULL)
  256. {
  257. is_resolved = true;
  258. data.klass = klass;
  259. alloc_next = verifier->isect_list;
  260. verifier->isect_list = this;
  261. }
  262. ref_intersection (_Jv_Utf8Const *name, _Jv_BytecodeVerifier *verifier)
  263. : ref_next (NULL)
  264. {
  265. is_resolved = false;
  266. data.name = name;
  267. alloc_next = verifier->isect_list;
  268. verifier->isect_list = this;
  269. }
  270. ref_intersection (ref_intersection *dup, ref_intersection *tail,
  271. _Jv_BytecodeVerifier *verifier)
  272. : ref_next (tail)
  273. {
  274. is_resolved = dup->is_resolved;
  275. data = dup->data;
  276. alloc_next = verifier->isect_list;
  277. verifier->isect_list = this;
  278. }
  279. bool equals (ref_intersection *other, _Jv_BytecodeVerifier *verifier)
  280. {
  281. if (! is_resolved && ! other->is_resolved
  282. && _Jv_equalUtf8Classnames (data.name, other->data.name))
  283. return true;
  284. if (! is_resolved)
  285. resolve (verifier);
  286. if (! other->is_resolved)
  287. other->resolve (verifier);
  288. return data.klass == other->data.klass;
  289. }
  290. // Merge THIS type into OTHER, returning the result. This will
  291. // return OTHER if all the classes in THIS already appear in
  292. // OTHER.
  293. ref_intersection *merge (ref_intersection *other,
  294. _Jv_BytecodeVerifier *verifier)
  295. {
  296. ref_intersection *tail = other;
  297. for (ref_intersection *self = this; self != NULL; self = self->ref_next)
  298. {
  299. bool add = true;
  300. for (ref_intersection *iter = other; iter != NULL;
  301. iter = iter->ref_next)
  302. {
  303. if (iter->equals (self, verifier))
  304. {
  305. add = false;
  306. break;
  307. }
  308. }
  309. if (add)
  310. tail = new ref_intersection (self, tail, verifier);
  311. }
  312. return tail;
  313. }
  314. void resolve (_Jv_BytecodeVerifier *verifier)
  315. {
  316. if (is_resolved)
  317. return;
  318. // This is useful if you want to see which classes have to be resolved
  319. // while doing the class verification.
  320. debug_print("resolving class: %s\n", data.name->chars());
  321. using namespace java::lang;
  322. java::lang::ClassLoader *loader
  323. = verifier->current_class->getClassLoaderInternal();
  324. // Due to special handling in to_array() array classes will always
  325. // be of the "L ... ;" kind. The separator char ('.' or '/' may vary
  326. // however.
  327. if (data.name->limit()[-1] == ';')
  328. {
  329. data.klass = _Jv_FindClassFromSignature (data.name->chars(), loader);
  330. if (data.klass == NULL)
  331. throw new java::lang::NoClassDefFoundError(data.name->toString());
  332. }
  333. else
  334. data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
  335. false, loader);
  336. is_resolved = true;
  337. }
  338. // See if an object of type OTHER can be assigned to an object of
  339. // type *THIS. This might resolve classes in one chain or the
  340. // other.
  341. bool compatible (ref_intersection *other,
  342. _Jv_BytecodeVerifier *verifier)
  343. {
  344. ref_intersection *self = this;
  345. for (; self != NULL; self = self->ref_next)
  346. {
  347. ref_intersection *other_iter = other;
  348. for (; other_iter != NULL; other_iter = other_iter->ref_next)
  349. {
  350. // Avoid resolving if possible.
  351. if (! self->is_resolved
  352. && ! other_iter->is_resolved
  353. && _Jv_equalUtf8Classnames (self->data.name,
  354. other_iter->data.name))
  355. continue;
  356. if (! self->is_resolved)
  357. self->resolve(verifier);
  358. // If the LHS of the expression is of type
  359. // java.lang.Object, assignment will succeed, no matter
  360. // what the type of the RHS is. Using this short-cut we
  361. // don't need to resolve the class of the RHS at
  362. // verification time.
  363. if (self->data.klass == &java::lang::Object::class$)
  364. continue;
  365. if (! other_iter->is_resolved)
  366. other_iter->resolve(verifier);
  367. if (! is_assignable_from_slow (self->data.klass,
  368. other_iter->data.klass))
  369. return false;
  370. }
  371. }
  372. return true;
  373. }
  374. bool isarray ()
  375. {
  376. // assert (ref_next == NULL);
  377. if (is_resolved)
  378. return data.klass->isArray ();
  379. else
  380. return data.name->first() == '[';
  381. }
  382. bool isinterface (_Jv_BytecodeVerifier *verifier)
  383. {
  384. // assert (ref_next == NULL);
  385. if (! is_resolved)
  386. resolve (verifier);
  387. return data.klass->isInterface ();
  388. }
  389. bool isabstract (_Jv_BytecodeVerifier *verifier)
  390. {
  391. // assert (ref_next == NULL);
  392. if (! is_resolved)
  393. resolve (verifier);
  394. using namespace java::lang::reflect;
  395. return Modifier::isAbstract (data.klass->getModifiers ());
  396. }
  397. jclass getclass (_Jv_BytecodeVerifier *verifier)
  398. {
  399. if (! is_resolved)
  400. resolve (verifier);
  401. return data.klass;
  402. }
  403. int count_dimensions ()
  404. {
  405. int ndims = 0;
  406. if (is_resolved)
  407. {
  408. jclass k = data.klass;
  409. while (k->isArray ())
  410. {
  411. k = k->getComponentType ();
  412. ++ndims;
  413. }
  414. }
  415. else
  416. {
  417. char *p = data.name->chars();
  418. while (*p++ == '[')
  419. ++ndims;
  420. }
  421. return ndims;
  422. }
  423. void *operator new (size_t bytes)
  424. {
  425. return _Jv_Malloc (bytes);
  426. }
  427. void operator delete (void *mem)
  428. {
  429. _Jv_Free (mem);
  430. }
  431. };
  432. // Return the type_val corresponding to a primitive signature
  433. // character. For instance `I' returns `int.class'.
  434. type_val get_type_val_for_signature (jchar sig)
  435. {
  436. type_val rt;
  437. switch (sig)
  438. {
  439. case 'Z':
  440. rt = boolean_type;
  441. break;
  442. case 'B':
  443. rt = byte_type;
  444. break;
  445. case 'C':
  446. rt = char_type;
  447. break;
  448. case 'S':
  449. rt = short_type;
  450. break;
  451. case 'I':
  452. rt = int_type;
  453. break;
  454. case 'J':
  455. rt = long_type;
  456. break;
  457. case 'F':
  458. rt = float_type;
  459. break;
  460. case 'D':
  461. rt = double_type;
  462. break;
  463. case 'V':
  464. rt = void_type;
  465. break;
  466. default:
  467. verify_fail ("invalid signature");
  468. }
  469. return rt;
  470. }
  471. // Return the type_val corresponding to a primitive class.
  472. type_val get_type_val_for_signature (jclass k)
  473. {
  474. return get_type_val_for_signature ((jchar) k->method_count);
  475. }
  476. // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
  477. // TARGET haven't been prepared.
  478. static bool is_assignable_from_slow (jclass target, jclass source)
  479. {
  480. // First, strip arrays.
  481. while (target->isArray ())
  482. {
  483. // If target is array, source must be as well.
  484. if (! source->isArray ())
  485. return false;
  486. target = target->getComponentType ();
  487. source = source->getComponentType ();
  488. }
  489. // Quick success.
  490. if (target == &java::lang::Object::class$)
  491. return true;
  492. do
  493. {
  494. if (source == target)
  495. return true;
  496. if (target->isPrimitive () || source->isPrimitive ())
  497. return false;
  498. if (target->isInterface ())
  499. {
  500. for (int i = 0; i < source->interface_count; ++i)
  501. {
  502. // We use a recursive call because we also need to
  503. // check superinterfaces.
  504. if (is_assignable_from_slow (target, source->getInterface (i)))
  505. return true;
  506. }
  507. }
  508. source = source->getSuperclass ();
  509. }
  510. while (source != NULL);
  511. return false;
  512. }
  513. // The `type' class is used to represent a single type in the
  514. // verifier.
  515. struct type
  516. {
  517. // The type key.
  518. type_val key;
  519. // For reference types, the representation of the type.
  520. ref_intersection *klass;
  521. // This is used in two situations.
  522. //
  523. // First, when constructing a new object, it is the PC of the
  524. // `new' instruction which created the object. We use the special
  525. // value UNINIT to mean that this is uninitialized. The special
  526. // value SELF is used for the case where the current method is
  527. // itself the <init> method. the special value EITHER is used
  528. // when we may optionally allow either an uninitialized or
  529. // initialized reference to match.
  530. //
  531. // Second, when the key is return_address_type, this holds the PC
  532. // of the instruction following the `jsr'.
  533. int pc;
  534. static const int UNINIT = -2;
  535. static const int SELF = -1;
  536. static const int EITHER = -3;
  537. // Basic constructor.
  538. type ()
  539. {
  540. key = unsuitable_type;
  541. klass = NULL;
  542. pc = UNINIT;
  543. }
  544. // Make a new instance given the type tag. We assume a generic
  545. // `reference_type' means Object.
  546. type (type_val k)
  547. {
  548. key = k;
  549. // For reference_type, if KLASS==NULL then that means we are
  550. // looking for a generic object of any kind, including an
  551. // uninitialized reference.
  552. klass = NULL;
  553. pc = UNINIT;
  554. }
  555. // Make a new instance given a class.
  556. type (jclass k, _Jv_BytecodeVerifier *verifier)
  557. {
  558. key = reference_type;
  559. klass = new ref_intersection (k, verifier);
  560. pc = UNINIT;
  561. }
  562. // Make a new instance given the name of a class.
  563. type (_Jv_Utf8Const *n, _Jv_BytecodeVerifier *verifier)
  564. {
  565. key = reference_type;
  566. klass = new ref_intersection (n, verifier);
  567. pc = UNINIT;
  568. }
  569. // Copy constructor.
  570. type (const type &t)
  571. {
  572. key = t.key;
  573. klass = t.klass;
  574. pc = t.pc;
  575. }
  576. // These operators are required because libgcj can't link in
  577. // -lstdc++.
  578. void *operator new[] (size_t bytes)
  579. {
  580. return _Jv_Malloc (bytes);
  581. }
  582. void operator delete[] (void *mem)
  583. {
  584. _Jv_Free (mem);
  585. }
  586. type& operator= (type_val k)
  587. {
  588. key = k;
  589. klass = NULL;
  590. pc = UNINIT;
  591. return *this;
  592. }
  593. type& operator= (const type& t)
  594. {
  595. key = t.key;
  596. klass = t.klass;
  597. pc = t.pc;
  598. return *this;
  599. }
  600. // Promote a numeric type.
  601. type &promote ()
  602. {
  603. if (key == boolean_type || key == char_type
  604. || key == byte_type || key == short_type)
  605. key = int_type;
  606. return *this;
  607. }
  608. // Mark this type as the uninitialized result of `new'.
  609. void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
  610. {
  611. if (key == reference_type)
  612. key = uninitialized_reference_type;
  613. else
  614. verifier->verify_fail ("internal error in type::uninitialized");
  615. pc = npc;
  616. }
  617. // Mark this type as now initialized.
  618. void set_initialized (int npc)
  619. {
  620. if (npc != UNINIT && pc == npc && key == uninitialized_reference_type)
  621. {
  622. key = reference_type;
  623. pc = UNINIT;
  624. }
  625. }
  626. // Mark this type as a particular return address.
  627. void set_return_address (int npc)
  628. {
  629. pc = npc;
  630. }
  631. // Return true if this type and type OTHER are considered
  632. // mergeable for the purposes of state merging. This is related
  633. // to subroutine handling. For this purpose two types are
  634. // considered unmergeable if they are both return-addresses but
  635. // have different PCs.
  636. bool state_mergeable_p (const type &other) const
  637. {
  638. return (key != return_address_type
  639. || other.key != return_address_type
  640. || pc == other.pc);
  641. }
  642. // Return true if an object of type K can be assigned to a variable
  643. // of type *THIS. Handle various special cases too. Might modify
  644. // *THIS or K. Note however that this does not perform numeric
  645. // promotion.
  646. bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
  647. {
  648. // Any type is compatible with the unsuitable type.
  649. if (key == unsuitable_type)
  650. return true;
  651. if (key < reference_type || k.key < reference_type)
  652. return key == k.key;
  653. // The `null' type is convertible to any initialized reference
  654. // type.
  655. if (key == null_type)
  656. return k.key != uninitialized_reference_type;
  657. if (k.key == null_type)
  658. return key != uninitialized_reference_type;
  659. // A special case for a generic reference.
  660. if (klass == NULL)
  661. return true;
  662. if (k.klass == NULL)
  663. verifier->verify_fail ("programmer error in type::compatible");
  664. // Handle the special 'EITHER' case, which is only used in a
  665. // special case of 'putfield'. Note that we only need to handle
  666. // this on the LHS of a check.
  667. if (! isinitialized () && pc == EITHER)
  668. {
  669. // If the RHS is uninitialized, it must be an uninitialized
  670. // 'this'.
  671. if (! k.isinitialized () && k.pc != SELF)
  672. return false;
  673. }
  674. else if (isinitialized () != k.isinitialized ())
  675. {
  676. // An initialized type and an uninitialized type are not
  677. // otherwise compatible.
  678. return false;
  679. }
  680. else
  681. {
  682. // Two uninitialized objects are compatible if either:
  683. // * The PCs are identical, or
  684. // * One PC is UNINIT.
  685. if (! isinitialized ())
  686. {
  687. if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
  688. return false;
  689. }
  690. }
  691. return klass->compatible(k.klass, verifier);
  692. }
  693. bool equals (const type &other, _Jv_BytecodeVerifier *vfy)
  694. {
  695. // Only works for reference types.
  696. if ((key != reference_type
  697. && key != uninitialized_reference_type)
  698. || (other.key != reference_type
  699. && other.key != uninitialized_reference_type))
  700. return false;
  701. // Only for single-valued types.
  702. if (klass->ref_next || other.klass->ref_next)
  703. return false;
  704. return klass->equals (other.klass, vfy);
  705. }
  706. bool isvoid () const
  707. {
  708. return key == void_type;
  709. }
  710. bool iswide () const
  711. {
  712. return key == long_type || key == double_type;
  713. }
  714. // Return number of stack or local variable slots taken by this
  715. // type.
  716. int depth () const
  717. {
  718. return iswide () ? 2 : 1;
  719. }
  720. bool isarray () const
  721. {
  722. // We treat null_type as not an array. This is ok based on the
  723. // current uses of this method.
  724. if (key == reference_type)
  725. return klass->isarray ();
  726. return false;
  727. }
  728. bool isnull () const
  729. {
  730. return key == null_type;
  731. }
  732. bool isinterface (_Jv_BytecodeVerifier *verifier)
  733. {
  734. if (key != reference_type)
  735. return false;
  736. return klass->isinterface (verifier);
  737. }
  738. bool isabstract (_Jv_BytecodeVerifier *verifier)
  739. {
  740. if (key != reference_type)
  741. return false;
  742. return klass->isabstract (verifier);
  743. }
  744. // Return the element type of an array.
  745. type element_type (_Jv_BytecodeVerifier *verifier)
  746. {
  747. if (key != reference_type)
  748. verifier->verify_fail ("programmer error in type::element_type()", -1);
  749. jclass k = klass->getclass (verifier)->getComponentType ();
  750. if (k->isPrimitive ())
  751. return type (verifier->get_type_val_for_signature (k));
  752. return type (k, verifier);
  753. }
  754. // Return the array type corresponding to an initialized
  755. // reference. We could expand this to work for other kinds of
  756. // types, but currently we don't need to.
  757. type to_array (_Jv_BytecodeVerifier *verifier)
  758. {
  759. if (key != reference_type)
  760. verifier->verify_fail ("internal error in type::to_array()");
  761. // In case the class is already resolved we can simply ask the runtime
  762. // to give us the array version.
  763. // If it is not resolved we prepend "[" to the classname to make the
  764. // array usage verification more lazy. In other words: makes new Foo[300]
  765. // pass the verifier if Foo.class is missing.
  766. if (klass->is_resolved)
  767. {
  768. jclass k = klass->getclass (verifier);
  769. return type (_Jv_GetArrayClass (k, k->getClassLoaderInternal()),
  770. verifier);
  771. }
  772. else
  773. {
  774. int len = klass->data.name->len();
  775. // If the classname is given in the Lp1/p2/cn; format we only need
  776. // to add a leading '['. The same procedure has to be done for
  777. // primitive arrays (ie. provided "[I", the result should be "[[I".
  778. // If the classname is given as p1.p2.cn we have to embed it into
  779. // "[L" and ';'.
  780. if (klass->data.name->limit()[-1] == ';' ||
  781. _Jv_isPrimitiveOrDerived(klass->data.name))
  782. {
  783. // Reserves space for leading '[' and trailing '\0' .
  784. char arrayName[len + 2];
  785. arrayName[0] = '[';
  786. strcpy(&arrayName[1], klass->data.name->chars());
  787. #ifdef VERIFY_DEBUG
  788. // This is only needed when we want to print the string to the
  789. // screen while debugging.
  790. arrayName[len + 1] = '\0';
  791. debug_print("len: %d - old: '%s' - new: '%s'\n", len, klass->data.name->chars(), arrayName);
  792. #endif
  793. return type (verifier->make_utf8_const( arrayName, len + 1 ),
  794. verifier);
  795. }
  796. else
  797. {
  798. // Reserves space for leading "[L" and trailing ';' and '\0' .
  799. char arrayName[len + 4];
  800. arrayName[0] = '[';
  801. arrayName[1] = 'L';
  802. strcpy(&arrayName[2], klass->data.name->chars());
  803. arrayName[len + 2] = ';';
  804. #ifdef VERIFY_DEBUG
  805. // This is only needed when we want to print the string to the
  806. // screen while debugging.
  807. arrayName[len + 3] = '\0';
  808. debug_print("len: %d - old: '%s' - new: '%s'\n", len, klass->data.name->chars(), arrayName);
  809. #endif
  810. return type (verifier->make_utf8_const( arrayName, len + 3 ),
  811. verifier);
  812. }
  813. }
  814. }
  815. bool isreference () const
  816. {
  817. return key >= reference_type;
  818. }
  819. int get_pc () const
  820. {
  821. return pc;
  822. }
  823. bool isinitialized () const
  824. {
  825. return key == reference_type || key == null_type;
  826. }
  827. bool isresolved () const
  828. {
  829. return (key == reference_type
  830. || key == null_type
  831. || key == uninitialized_reference_type);
  832. }
  833. void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
  834. {
  835. // The way this is written, we don't need to check isarray().
  836. if (key != reference_type)
  837. verifier->verify_fail ("internal error in verify_dimensions:"
  838. " not a reference type");
  839. if (klass->count_dimensions () < ndims)
  840. verifier->verify_fail ("array type has fewer dimensions"
  841. " than required");
  842. }
  843. // Merge OLD_TYPE into this. On error throw exception. Return
  844. // true if the merge caused a type change.
  845. bool merge (type& old_type, bool local_semantics,
  846. _Jv_BytecodeVerifier *verifier)
  847. {
  848. bool changed = false;
  849. bool refo = old_type.isreference ();
  850. bool refn = isreference ();
  851. if (refo && refn)
  852. {
  853. if (old_type.key == null_type)
  854. ;
  855. else if (key == null_type)
  856. {
  857. *this = old_type;
  858. changed = true;
  859. }
  860. else if (isinitialized () != old_type.isinitialized ())
  861. verifier->verify_fail ("merging initialized and uninitialized types");
  862. else
  863. {
  864. if (! isinitialized ())
  865. {
  866. if (pc == UNINIT)
  867. pc = old_type.pc;
  868. else if (old_type.pc == UNINIT)
  869. ;
  870. else if (pc != old_type.pc)
  871. verifier->verify_fail ("merging different uninitialized types");
  872. }
  873. ref_intersection *merged = old_type.klass->merge (klass,
  874. verifier);
  875. if (merged != klass)
  876. {
  877. klass = merged;
  878. changed = true;
  879. }
  880. }
  881. }
  882. else if (refo || refn || key != old_type.key)
  883. {
  884. if (local_semantics)
  885. {
  886. // If we already have an `unsuitable' type, then we
  887. // don't need to change again.
  888. if (key != unsuitable_type)
  889. {
  890. key = unsuitable_type;
  891. changed = true;
  892. }
  893. }
  894. else
  895. verifier->verify_fail ("unmergeable type");
  896. }
  897. return changed;
  898. }
  899. #ifdef VERIFY_DEBUG
  900. void print (void) const
  901. {
  902. char c = '?';
  903. switch (key)
  904. {
  905. case boolean_type: c = 'Z'; break;
  906. case byte_type: c = 'B'; break;
  907. case char_type: c = 'C'; break;
  908. case short_type: c = 'S'; break;
  909. case int_type: c = 'I'; break;
  910. case long_type: c = 'J'; break;
  911. case float_type: c = 'F'; break;
  912. case double_type: c = 'D'; break;
  913. case void_type: c = 'V'; break;
  914. case unsuitable_type: c = '-'; break;
  915. case return_address_type: c = 'r'; break;
  916. case continuation_type: c = '+'; break;
  917. case reference_type: c = 'L'; break;
  918. case null_type: c = '@'; break;
  919. case uninitialized_reference_type: c = 'U'; break;
  920. }
  921. debug_print ("%c", c);
  922. }
  923. #endif /* VERIFY_DEBUG */
  924. };
  925. // This class holds all the state information we need for a given
  926. // location.
  927. struct state
  928. {
  929. // The current top of the stack, in terms of slots.
  930. int stacktop;
  931. // The current depth of the stack. This will be larger than
  932. // STACKTOP when wide types are on the stack.
  933. int stackdepth;
  934. // The stack.
  935. type *stack;
  936. // The local variables.
  937. type *locals;
  938. // We keep track of the type of `this' specially. This is used to
  939. // ensure that an instance initializer invokes another initializer
  940. // on `this' before returning. We must keep track of this
  941. // specially because otherwise we might be confused by code which
  942. // assigns to locals[0] (overwriting `this') and then returns
  943. // without really initializing.
  944. type this_type;
  945. // The PC for this state. This is only valid on states which are
  946. // permanently attached to a given PC. For an object like
  947. // `current_state', which is used transiently, this has no
  948. // meaning.
  949. int pc;
  950. // We keep a linked list of all states requiring reverification.
  951. // If this is the special value INVALID_STATE then this state is
  952. // not on the list. NULL marks the end of the linked list.
  953. state *next;
  954. // NO_NEXT is the PC value meaning that a new state must be
  955. // acquired from the verification list.
  956. static const int NO_NEXT = -1;
  957. state ()
  958. : this_type ()
  959. {
  960. stack = NULL;
  961. locals = NULL;
  962. next = INVALID_STATE;
  963. }
  964. state (int max_stack, int max_locals)
  965. : this_type ()
  966. {
  967. stacktop = 0;
  968. stackdepth = 0;
  969. stack = new type[max_stack];
  970. for (int i = 0; i < max_stack; ++i)
  971. stack[i] = unsuitable_type;
  972. locals = new type[max_locals];
  973. for (int i = 0; i < max_locals; ++i)
  974. locals[i] = unsuitable_type;
  975. pc = NO_NEXT;
  976. next = INVALID_STATE;
  977. }
  978. state (const state *orig, int max_stack, int max_locals)
  979. {
  980. stack = new type[max_stack];
  981. locals = new type[max_locals];
  982. copy (orig, max_stack, max_locals);
  983. pc = NO_NEXT;
  984. next = INVALID_STATE;
  985. }
  986. ~state ()
  987. {
  988. if (stack)
  989. delete[] stack;
  990. if (locals)
  991. delete[] locals;
  992. }
  993. void *operator new[] (size_t bytes)
  994. {
  995. return _Jv_Malloc (bytes);
  996. }
  997. void operator delete[] (void *mem)
  998. {
  999. _Jv_Free (mem);
  1000. }
  1001. void *operator new (size_t bytes)
  1002. {
  1003. return _Jv_Malloc (bytes);
  1004. }
  1005. void operator delete (void *mem)
  1006. {
  1007. _Jv_Free (mem);
  1008. }
  1009. void copy (const state *copy, int max_stack, int max_locals)
  1010. {
  1011. stacktop = copy->stacktop;
  1012. stackdepth = copy->stackdepth;
  1013. for (int i = 0; i < max_stack; ++i)
  1014. stack[i] = copy->stack[i];
  1015. for (int i = 0; i < max_locals; ++i)
  1016. locals[i] = copy->locals[i];
  1017. this_type = copy->this_type;
  1018. // Don't modify `next' or `pc'.
  1019. }
  1020. // Modify this state to reflect entry to an exception handler.
  1021. void set_exception (type t, int max_stack)
  1022. {
  1023. stackdepth = 1;
  1024. stacktop = 1;
  1025. stack[0] = t;
  1026. for (int i = stacktop; i < max_stack; ++i)
  1027. stack[i] = unsuitable_type;
  1028. }
  1029. inline int get_pc () const
  1030. {
  1031. return pc;
  1032. }
  1033. void set_pc (int npc)
  1034. {
  1035. pc = npc;
  1036. }
  1037. // Merge STATE_OLD into this state. Destructively modifies this
  1038. // state. Returns true if the new state was in fact changed.
  1039. // Will throw an exception if the states are not mergeable.
  1040. bool merge (state *state_old, int max_locals,
  1041. _Jv_BytecodeVerifier *verifier)
  1042. {
  1043. bool changed = false;
  1044. // Special handling for `this'. If one or the other is
  1045. // uninitialized, then the merge is uninitialized.
  1046. if (this_type.isinitialized ())
  1047. this_type = state_old->this_type;
  1048. // Merge stacks.
  1049. if (state_old->stacktop != stacktop) // FIXME stackdepth instead?
  1050. verifier->verify_fail ("stack sizes differ");
  1051. for (int i = 0; i < state_old->stacktop; ++i)
  1052. {
  1053. if (stack[i].merge (state_old->stack[i], false, verifier))
  1054. changed = true;
  1055. }
  1056. // Merge local variables.
  1057. for (int i = 0; i < max_locals; ++i)
  1058. {
  1059. if (locals[i].merge (state_old->locals[i], true, verifier))
  1060. changed = true;
  1061. }
  1062. return changed;
  1063. }
  1064. // Ensure that `this' has been initialized.
  1065. void check_this_initialized (_Jv_BytecodeVerifier *verifier)
  1066. {
  1067. if (this_type.isreference () && ! this_type.isinitialized ())
  1068. verifier->verify_fail ("`this' is uninitialized");
  1069. }
  1070. // Set type of `this'.
  1071. void set_this_type (const type &k)
  1072. {
  1073. this_type = k;
  1074. }
  1075. // Mark each `new'd object we know of that was allocated at PC as
  1076. // initialized.
  1077. void set_initialized (int pc, int max_locals)
  1078. {
  1079. for (int i = 0; i < stacktop; ++i)
  1080. stack[i].set_initialized (pc);
  1081. for (int i = 0; i < max_locals; ++i)
  1082. locals[i].set_initialized (pc);
  1083. this_type.set_initialized (pc);
  1084. }
  1085. // This tests to see whether two states can be considered "merge
  1086. // compatible". If both states have a return-address in the same
  1087. // slot, and the return addresses are different, then they are not
  1088. // compatible and we must not try to merge them.
  1089. bool state_mergeable_p (state *other, int max_locals,
  1090. _Jv_BytecodeVerifier *verifier)
  1091. {
  1092. // This is tricky: if the stack sizes differ, then not only are
  1093. // these not mergeable, but in fact we should give an error, as
  1094. // we've found two execution paths that reach a branch target
  1095. // with different stack depths. FIXME stackdepth instead?
  1096. if (stacktop != other->stacktop)
  1097. verifier->verify_fail ("stack sizes differ");
  1098. for (int i = 0; i < stacktop; ++i)
  1099. if (! stack[i].state_mergeable_p (other->stack[i]))
  1100. return false;
  1101. for (int i = 0; i < max_locals; ++i)
  1102. if (! locals[i].state_mergeable_p (other->locals[i]))
  1103. return false;
  1104. return true;
  1105. }
  1106. void reverify (_Jv_BytecodeVerifier *verifier)
  1107. {
  1108. if (next == INVALID_STATE)
  1109. {
  1110. next = verifier->next_verify_state;
  1111. verifier->next_verify_state = this;
  1112. }
  1113. }
  1114. #ifdef VERIFY_DEBUG
  1115. void print (const char *leader, int pc,
  1116. int max_stack, int max_locals) const
  1117. {
  1118. debug_print ("%s [%4d]: [stack] ", leader, pc);
  1119. int i;
  1120. for (i = 0; i < stacktop; ++i)
  1121. stack[i].print ();
  1122. for (; i < max_stack; ++i)
  1123. debug_print (".");
  1124. debug_print (" [local] ");
  1125. for (i = 0; i < max_locals; ++i)
  1126. locals[i].print ();
  1127. debug_print (" | %p\n", this);
  1128. }
  1129. #else
  1130. inline void print (const char *, int, int, int) const
  1131. {
  1132. }
  1133. #endif /* VERIFY_DEBUG */
  1134. };
  1135. type pop_raw ()
  1136. {
  1137. if (current_state->stacktop <= 0)
  1138. verify_fail ("stack empty");
  1139. type r = current_state->stack[--current_state->stacktop];
  1140. current_state->stackdepth -= r.depth ();
  1141. if (current_state->stackdepth < 0)
  1142. verify_fail ("stack empty", start_PC);
  1143. return r;
  1144. }
  1145. type pop32 ()
  1146. {
  1147. type r = pop_raw ();
  1148. if (r.iswide ())
  1149. verify_fail ("narrow pop of wide type");
  1150. return r;
  1151. }
  1152. type pop_type (type match)
  1153. {
  1154. match.promote ();
  1155. type t = pop_raw ();
  1156. if (! match.compatible (t, this))
  1157. verify_fail ("incompatible type on stack");
  1158. return t;
  1159. }
  1160. // Pop a reference which is guaranteed to be initialized. MATCH
  1161. // doesn't have to be a reference type; in this case this acts like
  1162. // pop_type.
  1163. type pop_init_ref (type match)
  1164. {
  1165. type t = pop_raw ();
  1166. if (t.isreference () && ! t.isinitialized ())
  1167. verify_fail ("initialized reference required");
  1168. else if (! match.compatible (t, this))
  1169. verify_fail ("incompatible type on stack");
  1170. return t;
  1171. }
  1172. // Pop a reference type or a return address.
  1173. type pop_ref_or_return ()
  1174. {
  1175. type t = pop_raw ();
  1176. if (! t.isreference () && t.key != return_address_type)
  1177. verify_fail ("expected reference or return address on stack");
  1178. return t;
  1179. }
  1180. void push_type (type t)
  1181. {
  1182. // If T is a numeric type like short, promote it to int.
  1183. t.promote ();
  1184. int depth = t.depth ();
  1185. if (current_state->stackdepth + depth > current_method->max_stack)
  1186. verify_fail ("stack overflow");
  1187. current_state->stack[current_state->stacktop++] = t;
  1188. current_state->stackdepth += depth;
  1189. }
  1190. void set_variable (int index, type t)
  1191. {
  1192. // If T is a numeric type like short, promote it to int.
  1193. t.promote ();
  1194. int depth = t.depth ();
  1195. if (index > current_method->max_locals - depth)
  1196. verify_fail ("invalid local variable");
  1197. current_state->locals[index] = t;
  1198. if (depth == 2)
  1199. current_state->locals[index + 1] = continuation_type;
  1200. if (index > 0 && current_state->locals[index - 1].iswide ())
  1201. current_state->locals[index - 1] = unsuitable_type;
  1202. }
  1203. type get_variable (int index, type t)
  1204. {
  1205. int depth = t.depth ();
  1206. if (index > current_method->max_locals - depth)
  1207. verify_fail ("invalid local variable");
  1208. if (! t.compatible (current_state->locals[index], this))
  1209. verify_fail ("incompatible type in local variable");
  1210. if (depth == 2)
  1211. {
  1212. type t (continuation_type);
  1213. if (! current_state->locals[index + 1].compatible (t, this))
  1214. verify_fail ("invalid local variable");
  1215. }
  1216. return current_state->locals[index];
  1217. }
  1218. // Make sure ARRAY is an array type and that its elements are
  1219. // compatible with type ELEMENT. Returns the actual element type.
  1220. type require_array_type (type array, type element)
  1221. {
  1222. // An odd case. Here we just pretend that everything went ok. If
  1223. // the requested element type is some kind of reference, return
  1224. // the null type instead.
  1225. if (array.isnull ())
  1226. return element.isreference () ? type (null_type) : element;
  1227. if (! array.isarray ())
  1228. verify_fail ("array required");
  1229. type t = array.element_type (this);
  1230. if (! element.compatible (t, this))
  1231. {
  1232. // Special case for byte arrays, which must also be boolean
  1233. // arrays.
  1234. bool ok = true;
  1235. if (element.key == byte_type)
  1236. {
  1237. type e2 (boolean_type);
  1238. ok = e2.compatible (t, this);
  1239. }
  1240. if (! ok)
  1241. verify_fail ("incompatible array element type");
  1242. }
  1243. // Return T and not ELEMENT, because T might be specialized.
  1244. return t;
  1245. }
  1246. jint get_byte ()
  1247. {
  1248. if (PC >= current_method->code_length)
  1249. verify_fail ("premature end of bytecode");
  1250. return (jint) bytecode[PC++] & 0xff;
  1251. }
  1252. jint get_ushort ()
  1253. {
  1254. jint b1 = get_byte ();
  1255. jint b2 = get_byte ();
  1256. return (jint) ((b1 << 8) | b2) & 0xffff;
  1257. }
  1258. jint get_short ()
  1259. {
  1260. jint b1 = get_byte ();
  1261. jint b2 = get_byte ();
  1262. jshort s = (b1 << 8) | b2;
  1263. return (jint) s;
  1264. }
  1265. jint get_int ()
  1266. {
  1267. jint b1 = get_byte ();
  1268. jint b2 = get_byte ();
  1269. jint b3 = get_byte ();
  1270. jint b4 = get_byte ();
  1271. return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
  1272. }
  1273. int compute_jump (int offset)
  1274. {
  1275. int npc = start_PC + offset;
  1276. if (npc < 0 || npc >= current_method->code_length)
  1277. verify_fail ("branch out of range", start_PC);
  1278. return npc;
  1279. }
  1280. // Add a new state to the state list at NPC.
  1281. state *add_new_state (int npc, state *old_state)
  1282. {
  1283. state *new_state = new state (old_state, current_method->max_stack,
  1284. current_method->max_locals);
  1285. debug_print ("== New state in add_new_state\n");
  1286. new_state->print ("New", npc, current_method->max_stack,
  1287. current_method->max_locals);
  1288. linked<state> *nlink
  1289. = (linked<state> *) _Jv_Malloc (sizeof (linked<state>));
  1290. nlink->val = new_state;
  1291. nlink->next = states[npc];
  1292. states[npc] = nlink;
  1293. new_state->set_pc (npc);
  1294. return new_state;
  1295. }
  1296. // Merge the indicated state into the state at the branch target and
  1297. // schedule a new PC if there is a change. NPC is the PC of the
  1298. // branch target, and FROM_STATE is the state at the source of the
  1299. // branch. This method returns true if the destination state
  1300. // changed and requires reverification, false otherwise.
  1301. void merge_into (int npc, state *from_state)
  1302. {
  1303. // Iterate over all target states and merge our state into each,
  1304. // if applicable. FIXME one improvement we could make here is
  1305. // "state destruction". Merging a new state into an existing one
  1306. // might cause a return_address_type to be merged to
  1307. // unsuitable_type. In this case the resulting state may now be
  1308. // mergeable with other states currently held in parallel at this
  1309. // location. So in this situation we could pairwise compare and
  1310. // reduce the number of parallel states.
  1311. bool applicable = false;
  1312. for (linked<state> *iter = states[npc]; iter != NULL; iter = iter->next)
  1313. {
  1314. state *new_state = iter->val;
  1315. if (new_state->state_mergeable_p (from_state,
  1316. current_method->max_locals, this))
  1317. {
  1318. applicable = true;
  1319. debug_print ("== Merge states in merge_into\n");
  1320. from_state->print ("Frm", start_PC, current_method->max_stack,
  1321. current_method->max_locals);
  1322. new_state->print (" To", npc, current_method->max_stack,
  1323. current_method->max_locals);
  1324. bool changed = new_state->merge (from_state,
  1325. current_method->max_locals,
  1326. this);
  1327. new_state->print ("New", npc, current_method->max_stack,
  1328. current_method->max_locals);
  1329. if (changed)
  1330. new_state->reverify (this);
  1331. }
  1332. }
  1333. if (! applicable)
  1334. {
  1335. // Either we don't yet have a state at NPC, or we have a
  1336. // return-address type that is in conflict with all existing
  1337. // state. So, we need to create a new entry.
  1338. state *new_state = add_new_state (npc, from_state);
  1339. // A new state added in this way must always be reverified.
  1340. new_state->reverify (this);
  1341. }
  1342. }
  1343. void push_jump (int offset)
  1344. {
  1345. int npc = compute_jump (offset);
  1346. // According to the JVM Spec, we need to check for uninitialized
  1347. // objects here. However, this does not actually affect type
  1348. // safety, and the Eclipse java compiler generates code that
  1349. // violates this constraint.
  1350. merge_into (npc, current_state);
  1351. }
  1352. void push_exception_jump (type t, int pc)
  1353. {
  1354. // According to the JVM Spec, we need to check for uninitialized
  1355. // objects here. However, this does not actually affect type
  1356. // safety, and the Eclipse java compiler generates code that
  1357. // violates this constraint.
  1358. state s (current_state, current_method->max_stack,
  1359. current_method->max_locals);
  1360. if (current_method->max_stack < 1)
  1361. verify_fail ("stack overflow at exception handler");
  1362. s.set_exception (t, current_method->max_stack);
  1363. merge_into (pc, &s);
  1364. }
  1365. state *pop_jump ()
  1366. {
  1367. state *new_state = next_verify_state;
  1368. if (new_state == INVALID_STATE)
  1369. verify_fail ("programmer error in pop_jump");
  1370. if (new_state != NULL)
  1371. {
  1372. next_verify_state = new_state->next;
  1373. new_state->next = INVALID_STATE;
  1374. }
  1375. return new_state;
  1376. }
  1377. void invalidate_pc ()
  1378. {
  1379. PC = state::NO_NEXT;
  1380. }
  1381. void note_branch_target (int pc)
  1382. {
  1383. // Don't check `pc <= PC', because we've advanced PC after
  1384. // fetching the target and we haven't yet checked the next
  1385. // instruction.
  1386. if (pc < PC && ! (flags[pc] & FLAG_INSN_START))
  1387. verify_fail ("branch not to instruction start", start_PC);
  1388. flags[pc] |= FLAG_BRANCH_TARGET;
  1389. }
  1390. void skip_padding ()
  1391. {
  1392. while ((PC % 4) > 0)
  1393. if (get_byte () != 0)
  1394. verify_fail ("found nonzero padding byte");
  1395. }
  1396. // Do the work for a `ret' instruction. INDEX is the index into the
  1397. // local variables.
  1398. void handle_ret_insn (int index)
  1399. {
  1400. type ret_addr = get_variable (index, return_address_type);
  1401. // It would be nice if we could do this. However, the JVM Spec
  1402. // doesn't say that this is what happens. It is implied that
  1403. // reusing a return address is invalid, but there's no actual
  1404. // prohibition against it.
  1405. // set_variable (index, unsuitable_type);
  1406. int npc = ret_addr.get_pc ();
  1407. // We might be returning to a `jsr' that is at the end of the
  1408. // bytecode. This is ok if we never return from the called
  1409. // subroutine, but if we see this here it is an error.
  1410. if (npc >= current_method->code_length)
  1411. verify_fail ("fell off end");
  1412. // According to the JVM Spec, we need to check for uninitialized
  1413. // objects here. However, this does not actually affect type
  1414. // safety, and the Eclipse java compiler generates code that
  1415. // violates this constraint.
  1416. merge_into (npc, current_state);
  1417. invalidate_pc ();
  1418. }
  1419. void handle_jsr_insn (int offset)
  1420. {
  1421. int npc = compute_jump (offset);
  1422. // According to the JVM Spec, we need to check for uninitialized
  1423. // objects here. However, this does not actually affect type
  1424. // safety, and the Eclipse java compiler generates code that
  1425. // violates this constraint.
  1426. // Modify our state as appropriate for entry into a subroutine.
  1427. type ret_addr (return_address_type);
  1428. ret_addr.set_return_address (PC);
  1429. push_type (ret_addr);
  1430. merge_into (npc, current_state);
  1431. invalidate_pc ();
  1432. }
  1433. jclass construct_primitive_array_type (type_val prim)
  1434. {
  1435. jclass k = NULL;
  1436. switch (prim)
  1437. {
  1438. case boolean_type:
  1439. k = JvPrimClass (boolean);
  1440. break;
  1441. case char_type:
  1442. k = JvPrimClass (char);
  1443. break;
  1444. case float_type:
  1445. k = JvPrimClass (float);
  1446. break;
  1447. case double_type:
  1448. k = JvPrimClass (double);
  1449. break;
  1450. case byte_type:
  1451. k = JvPrimClass (byte);
  1452. break;
  1453. case short_type:
  1454. k = JvPrimClass (short);
  1455. break;
  1456. case int_type:
  1457. k = JvPrimClass (int);
  1458. break;
  1459. case long_type:
  1460. k = JvPrimClass (long);
  1461. break;
  1462. // These aren't used here but we call them out to avoid
  1463. // warnings.
  1464. case void_type:
  1465. case unsuitable_type:
  1466. case return_address_type:
  1467. case continuation_type:
  1468. case reference_type:
  1469. case null_type:
  1470. case uninitialized_reference_type:
  1471. default:
  1472. verify_fail ("unknown type in construct_primitive_array_type");
  1473. }
  1474. k = _Jv_GetArrayClass (k, NULL);
  1475. return k;
  1476. }
  1477. // This pass computes the location of branch targets and also
  1478. // instruction starts.
  1479. void branch_prepass ()
  1480. {
  1481. flags = (char *) _Jv_Malloc (current_method->code_length);
  1482. for (int i = 0; i < current_method->code_length; ++i)
  1483. flags[i] = 0;
  1484. PC = 0;
  1485. while (PC < current_method->code_length)
  1486. {
  1487. // Set `start_PC' early so that error checking can have the
  1488. // correct value.
  1489. start_PC = PC;
  1490. flags[PC] |= FLAG_INSN_START;
  1491. java_opcode opcode = (java_opcode) bytecode[PC++];
  1492. switch (opcode)
  1493. {
  1494. case op_nop:
  1495. case op_aconst_null:
  1496. case op_iconst_m1:
  1497. case op_iconst_0:
  1498. case op_iconst_1:
  1499. case op_iconst_2:
  1500. case op_iconst_3:
  1501. case op_iconst_4:
  1502. case op_iconst_5:
  1503. case op_lconst_0:
  1504. case op_lconst_1:
  1505. case op_fconst_0:
  1506. case op_fconst_1:
  1507. case op_fconst_2:
  1508. case op_dconst_0:
  1509. case op_dconst_1:
  1510. case op_iload_0:
  1511. case op_iload_1:
  1512. case op_iload_2:
  1513. case op_iload_3:
  1514. case op_lload_0:
  1515. case op_lload_1:
  1516. case op_lload_2:
  1517. case op_lload_3:
  1518. case op_fload_0:
  1519. case op_fload_1:
  1520. case op_fload_2:
  1521. case op_fload_3:
  1522. case op_dload_0:
  1523. case op_dload_1:
  1524. case op_dload_2:
  1525. case op_dload_3:
  1526. case op_aload_0:
  1527. case op_aload_1:
  1528. case op_aload_2:
  1529. case op_aload_3:
  1530. case op_iaload:
  1531. case op_laload:
  1532. case op_faload:
  1533. case op_daload:
  1534. case op_aaload:
  1535. case op_baload:
  1536. case op_caload:
  1537. case op_saload:
  1538. case op_istore_0:
  1539. case op_istore_1:
  1540. case op_istore_2:
  1541. case op_istore_3:
  1542. case op_lstore_0:
  1543. case op_lstore_1:
  1544. case op_lstore_2:
  1545. case op_lstore_3:
  1546. case op_fstore_0:
  1547. case op_fstore_1:
  1548. case op_fstore_2:
  1549. case op_fstore_3:
  1550. case op_dstore_0:
  1551. case op_dstore_1:
  1552. case op_dstore_2:
  1553. case op_dstore_3:
  1554. case op_astore_0:
  1555. case op_astore_1:
  1556. case op_astore_2:
  1557. case op_astore_3:
  1558. case op_iastore:
  1559. case op_lastore:
  1560. case op_fastore:
  1561. case op_dastore:
  1562. case op_aastore:
  1563. case op_bastore:
  1564. case op_castore:
  1565. case op_sastore:
  1566. case op_pop:
  1567. case op_pop2:
  1568. case op_dup:
  1569. case op_dup_x1:
  1570. case op_dup_x2:
  1571. case op_dup2:
  1572. case op_dup2_x1:
  1573. case op_dup2_x2:
  1574. case op_swap:
  1575. case op_iadd:
  1576. case op_isub:
  1577. case op_imul:
  1578. case op_idiv:
  1579. case op_irem:
  1580. case op_ishl:
  1581. case op_ishr:
  1582. case op_iushr:
  1583. case op_iand:
  1584. case op_ior:
  1585. case op_ixor:
  1586. case op_ladd:
  1587. case op_lsub:
  1588. case op_lmul:
  1589. case op_ldiv:
  1590. case op_lrem:
  1591. case op_lshl:
  1592. case op_lshr:
  1593. case op_lushr:
  1594. case op_land:
  1595. case op_lor:
  1596. case op_lxor:
  1597. case op_fadd:
  1598. case op_fsub:
  1599. case op_fmul:
  1600. case op_fdiv:
  1601. case op_frem:
  1602. case op_dadd:
  1603. case op_dsub:
  1604. case op_dmul:
  1605. case op_ddiv:
  1606. case op_drem:
  1607. case op_ineg:
  1608. case op_i2b:
  1609. case op_i2c:
  1610. case op_i2s:
  1611. case op_lneg:
  1612. case op_fneg:
  1613. case op_dneg:
  1614. case op_i2l:
  1615. case op_i2f:
  1616. case op_i2d:
  1617. case op_l2i:
  1618. case op_l2f:
  1619. case op_l2d:
  1620. case op_f2i:
  1621. case op_f2l:
  1622. case op_f2d:
  1623. case op_d2i:
  1624. case op_d2l:
  1625. case op_d2f:
  1626. case op_lcmp:
  1627. case op_fcmpl:
  1628. case op_fcmpg:
  1629. case op_dcmpl:
  1630. case op_dcmpg:
  1631. case op_monitorenter:
  1632. case op_monitorexit:
  1633. case op_ireturn:
  1634. case op_lreturn:
  1635. case op_freturn:
  1636. case op_dreturn:
  1637. case op_areturn:
  1638. case op_return:
  1639. case op_athrow:
  1640. case op_arraylength:
  1641. break;
  1642. case op_bipush:
  1643. case op_ldc:
  1644. case op_iload:
  1645. case op_lload:
  1646. case op_fload:
  1647. case op_dload:
  1648. case op_aload:
  1649. case op_istore:
  1650. case op_lstore:
  1651. case op_fstore:
  1652. case op_dstore:
  1653. case op_astore:
  1654. case op_ret:
  1655. case op_newarray:
  1656. get_byte ();
  1657. break;
  1658. case op_iinc:
  1659. case op_sipush:
  1660. case op_ldc_w:
  1661. case op_ldc2_w:
  1662. case op_getstatic:
  1663. case op_getfield:
  1664. case op_putfield:
  1665. case op_putstatic:
  1666. case op_new:
  1667. case op_anewarray:
  1668. case op_instanceof:
  1669. case op_checkcast:
  1670. case op_invokespecial:
  1671. case op_invokestatic:
  1672. case op_invokevirtual:
  1673. get_short ();
  1674. break;
  1675. case op_multianewarray:
  1676. get_short ();
  1677. get_byte ();
  1678. break;
  1679. case op_jsr:
  1680. case op_ifeq:
  1681. case op_ifne:
  1682. case op_iflt:
  1683. case op_ifge:
  1684. case op_ifgt:
  1685. case op_ifle:
  1686. case op_if_icmpeq:
  1687. case op_if_icmpne:
  1688. case op_if_icmplt:
  1689. case op_if_icmpge:
  1690. case op_if_icmpgt:
  1691. case op_if_icmple:
  1692. case op_if_acmpeq:
  1693. case op_if_acmpne:
  1694. case op_ifnull:
  1695. case op_ifnonnull:
  1696. case op_goto:
  1697. note_branch_target (compute_jump (get_short ()));
  1698. break;
  1699. case op_tableswitch:
  1700. {
  1701. skip_padding ();
  1702. note_branch_target (compute_jump (get_int ()));
  1703. jint low = get_int ();
  1704. jint hi = get_int ();
  1705. if (low > hi)
  1706. verify_fail ("invalid tableswitch", start_PC);
  1707. for (int i = low; i <= hi; ++i)
  1708. note_branch_target (compute_jump (get_int ()));
  1709. }
  1710. break;
  1711. case op_lookupswitch:
  1712. {
  1713. skip_padding ();
  1714. note_branch_target (compute_jump (get_int ()));
  1715. int npairs = get_int ();
  1716. if (npairs < 0)
  1717. verify_fail ("too few pairs in lookupswitch", start_PC);
  1718. while (npairs-- > 0)
  1719. {
  1720. get_int ();
  1721. note_branch_target (compute_jump (get_int ()));
  1722. }
  1723. }
  1724. break;
  1725. case op_invokeinterface:
  1726. get_short ();
  1727. get_byte ();
  1728. get_byte ();
  1729. break;
  1730. case op_wide:
  1731. {
  1732. opcode = (java_opcode) get_byte ();
  1733. get_short ();
  1734. if (opcode == op_iinc)
  1735. get_short ();
  1736. }
  1737. break;
  1738. case op_jsr_w:
  1739. case op_goto_w:
  1740. note_branch_target (compute_jump (get_int ()));
  1741. break;
  1742. // These are unused here, but we call them out explicitly
  1743. // so that -Wswitch-enum doesn't complain.
  1744. case op_putfield_1:
  1745. case op_putfield_2:
  1746. case op_putfield_4:
  1747. case op_putfield_8:
  1748. case op_putfield_a:
  1749. case op_putstatic_1:
  1750. case op_putstatic_2:
  1751. case op_putstatic_4:
  1752. case op_putstatic_8:
  1753. case op_putstatic_a:
  1754. case op_getfield_1:
  1755. case op_getfield_2s:
  1756. case op_getfield_2u:
  1757. case op_getfield_4:
  1758. case op_getfield_8:
  1759. case op_getfield_a:
  1760. case op_getstatic_1:
  1761. case op_getstatic_2s:
  1762. case op_getstatic_2u:
  1763. case op_getstatic_4:
  1764. case op_getstatic_8:
  1765. case op_getstatic_a:
  1766. case op_breakpoint:
  1767. default:
  1768. verify_fail ("unrecognized instruction in branch_prepass",
  1769. start_PC);
  1770. }
  1771. // See if any previous branch tried to branch to the middle of
  1772. // this instruction.
  1773. for (int pc = start_PC + 1; pc < PC; ++pc)
  1774. {
  1775. if ((flags[pc] & FLAG_BRANCH_TARGET))
  1776. verify_fail ("branch to middle of instruction", pc);
  1777. }
  1778. }
  1779. // Verify exception handlers.
  1780. for (int i = 0; i < current_method->exc_count; ++i)
  1781. {
  1782. if (! (flags[exception[i].handler_pc.i] & FLAG_INSN_START))
  1783. verify_fail ("exception handler not at instruction start",
  1784. exception[i].handler_pc.i);
  1785. if (! (flags[exception[i].start_pc.i] & FLAG_INSN_START))
  1786. verify_fail ("exception start not at instruction start",
  1787. exception[i].start_pc.i);
  1788. if (exception[i].end_pc.i != current_method->code_length
  1789. && ! (flags[exception[i].end_pc.i] & FLAG_INSN_START))
  1790. verify_fail ("exception end not at instruction start",
  1791. exception[i].end_pc.i);
  1792. flags[exception[i].handler_pc.i] |= FLAG_BRANCH_TARGET;
  1793. }
  1794. }
  1795. void check_pool_index (int index)
  1796. {
  1797. if (index < 0 || index >= current_class->constants.size)
  1798. verify_fail ("constant pool index out of range", start_PC);
  1799. }
  1800. type check_class_constant (int index)
  1801. {
  1802. check_pool_index (index);
  1803. _Jv_Constants *pool = &current_class->constants;
  1804. if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
  1805. return type (pool->data[index].clazz, this);
  1806. else if (pool->tags[index] == JV_CONSTANT_Class)
  1807. return type (pool->data[index].utf8, this);
  1808. verify_fail ("expected class constant", start_PC);
  1809. }
  1810. type check_constant (int index)
  1811. {
  1812. check_pool_index (index);
  1813. _Jv_Constants *pool = &current_class->constants;
  1814. int tag = pool->tags[index];
  1815. if (tag == JV_CONSTANT_ResolvedString || tag == JV_CONSTANT_String)
  1816. return type (&java::lang::String::class$, this);
  1817. else if (tag == JV_CONSTANT_Integer)
  1818. return type (int_type);
  1819. else if (tag == JV_CONSTANT_Float)
  1820. return type (float_type);
  1821. else if (current_method->is_15
  1822. && (tag == JV_CONSTANT_ResolvedClass || tag == JV_CONSTANT_Class))
  1823. return type (&java::lang::Class::class$, this);
  1824. verify_fail ("String, int, or float constant expected", start_PC);
  1825. }
  1826. type check_wide_constant (int index)
  1827. {
  1828. check_pool_index (index);
  1829. _Jv_Constants *pool = &current_class->constants;
  1830. if (pool->tags[index] == JV_CONSTANT_Long)
  1831. return type (long_type);
  1832. else if (pool->tags[index] == JV_CONSTANT_Double)
  1833. return type (double_type);
  1834. verify_fail ("long or double constant expected", start_PC);
  1835. }
  1836. // Helper for both field and method. These are laid out the same in
  1837. // the constant pool.
  1838. type handle_field_or_method (int index, int expected,
  1839. _Jv_Utf8Const **name,
  1840. _Jv_Utf8Const **fmtype)
  1841. {
  1842. check_pool_index (index);
  1843. _Jv_Constants *pool = &current_class->constants;
  1844. if (pool->tags[index] != expected)
  1845. verify_fail ("didn't see expected constant", start_PC);
  1846. // Once we know we have a Fieldref or Methodref we assume that it
  1847. // is correctly laid out in the constant pool. I think the code
  1848. // in defineclass.cc guarantees this.
  1849. _Jv_ushort class_index, name_and_type_index;
  1850. _Jv_loadIndexes (&pool->data[index],
  1851. class_index,
  1852. name_and_type_index);
  1853. _Jv_ushort name_index, desc_index;
  1854. _Jv_loadIndexes (&pool->data[name_and_type_index],
  1855. name_index, desc_index);
  1856. *name = pool->data[name_index].utf8;
  1857. *fmtype = pool->data[desc_index].utf8;
  1858. return check_class_constant (class_index);
  1859. }
  1860. // Return field's type, compute class' type if requested.
  1861. // If PUTFIELD is true, use the special 'putfield' semantics.
  1862. type check_field_constant (int index, type *class_type = NULL,
  1863. bool putfield = false)
  1864. {
  1865. _Jv_Utf8Const *name, *field_type;
  1866. type ct = handle_field_or_method (index,
  1867. JV_CONSTANT_Fieldref,
  1868. &name, &field_type);
  1869. if (class_type)
  1870. *class_type = ct;
  1871. type result;
  1872. if (field_type->first() == '[' || field_type->first() == 'L')
  1873. result = type (field_type, this);
  1874. else
  1875. result = get_type_val_for_signature (field_type->first());
  1876. // We have an obscure special case here: we can use `putfield' on
  1877. // a field declared in this class, even if `this' has not yet been
  1878. // initialized.
  1879. if (putfield
  1880. && ! current_state->this_type.isinitialized ()
  1881. && current_state->this_type.pc == type::SELF
  1882. && current_state->this_type.equals (ct, this)
  1883. // We don't look at the signature, figuring that if it is
  1884. // wrong we will fail during linking. FIXME?
  1885. && _Jv_Linker::has_field_p (current_class, name))
  1886. // Note that we don't actually know whether we're going to match
  1887. // against 'this' or some other object of the same type. So,
  1888. // here we set things up so that it doesn't matter. This relies
  1889. // on knowing what our caller is up to.
  1890. class_type->set_uninitialized (type::EITHER, this);
  1891. return result;
  1892. }
  1893. type check_method_constant (int index, bool is_interface,
  1894. _Jv_Utf8Const **method_name,
  1895. _Jv_Utf8Const **method_signature)
  1896. {
  1897. return handle_field_or_method (index,
  1898. (is_interface
  1899. ? JV_CONSTANT_InterfaceMethodref
  1900. : JV_CONSTANT_Methodref),
  1901. method_name, method_signature);
  1902. }
  1903. type get_one_type (char *&p)
  1904. {
  1905. char *start = p;
  1906. int arraycount = 0;
  1907. while (*p == '[')
  1908. {
  1909. ++arraycount;
  1910. ++p;
  1911. }
  1912. char v = *p++;
  1913. if (v == 'L')
  1914. {
  1915. while (*p != ';')
  1916. ++p;
  1917. ++p;
  1918. _Jv_Utf8Const *name = make_utf8_const (start, p - start);
  1919. return type (name, this);
  1920. }
  1921. // Casting to jchar here is ok since we are looking at an ASCII
  1922. // character.
  1923. type_val rt = get_type_val_for_signature (jchar (v));
  1924. if (arraycount == 0)
  1925. {
  1926. // Callers of this function eventually push their arguments on
  1927. // the stack. So, promote them here.
  1928. return type (rt).promote ();
  1929. }
  1930. jclass k = construct_primitive_array_type (rt);
  1931. while (--arraycount > 0)
  1932. k = _Jv_GetArrayClass (k, NULL);
  1933. return type (k, this);
  1934. }
  1935. void compute_argument_types (_Jv_Utf8Const *signature,
  1936. type *types)
  1937. {
  1938. char *p = signature->chars();
  1939. // Skip `('.
  1940. ++p;
  1941. int i = 0;
  1942. while (*p != ')')
  1943. types[i++] = get_one_type (p);
  1944. }
  1945. type compute_return_type (_Jv_Utf8Const *signature)
  1946. {
  1947. char *p = signature->chars();
  1948. while (*p != ')')
  1949. ++p;
  1950. ++p;
  1951. return get_one_type (p);
  1952. }
  1953. void check_return_type (type onstack)
  1954. {
  1955. type rt = compute_return_type (current_method->self->signature);
  1956. if (! rt.compatible (onstack, this))
  1957. verify_fail ("incompatible return type");
  1958. }
  1959. // Initialize the stack for the new method. Returns true if this
  1960. // method is an instance initializer.
  1961. bool initialize_stack ()
  1962. {
  1963. int var = 0;
  1964. bool is_init = _Jv_equalUtf8Consts (current_method->self->name,
  1965. gcj::init_name);
  1966. bool is_clinit = _Jv_equalUtf8Consts (current_method->self->name,
  1967. gcj::clinit_name);
  1968. using namespace java::lang::reflect;
  1969. if (! Modifier::isStatic (current_method->self->accflags))
  1970. {
  1971. type kurr (current_class, this);
  1972. if (is_init)
  1973. {
  1974. kurr.set_uninitialized (type::SELF, this);
  1975. is_init = true;
  1976. }
  1977. else if (is_clinit)
  1978. verify_fail ("<clinit> method must be static");
  1979. set_variable (0, kurr);
  1980. current_state->set_this_type (kurr);
  1981. ++var;
  1982. }
  1983. else
  1984. {
  1985. if (is_init)
  1986. verify_fail ("<init> method must be non-static");
  1987. }
  1988. // We have to handle wide arguments specially here.
  1989. int arg_count = _Jv_count_arguments (current_method->self->signature);
  1990. type arg_types[arg_count];
  1991. compute_argument_types (current_method->self->signature, arg_types);
  1992. for (int i = 0; i < arg_count; ++i)
  1993. {
  1994. set_variable (var, arg_types[i]);
  1995. ++var;
  1996. if (arg_types[i].iswide ())
  1997. ++var;
  1998. }
  1999. return is_init;
  2000. }
  2001. void verify_instructions_0 ()
  2002. {
  2003. current_state = new state (current_method->max_stack,
  2004. current_method->max_locals);
  2005. PC = 0;
  2006. start_PC = 0;
  2007. // True if we are verifying an instance initializer.
  2008. bool this_is_init = initialize_stack ();
  2009. states = (linked<state> **) _Jv_Malloc (sizeof (linked<state> *)
  2010. * current_method->code_length);
  2011. for (int i = 0; i < current_method->code_length; ++i)
  2012. states[i] = NULL;
  2013. next_verify_state = NULL;
  2014. while (true)
  2015. {
  2016. // If the PC was invalidated, get a new one from the work list.
  2017. if (PC == state::NO_NEXT)
  2018. {
  2019. state *new_state = pop_jump ();
  2020. // If it is null, we're done.
  2021. if (new_state == NULL)
  2022. break;
  2023. PC = new_state->get_pc ();
  2024. debug_print ("== State pop from pending list\n");
  2025. // Set up the current state.
  2026. current_state->copy (new_state, current_method->max_stack,
  2027. current_method->max_locals);
  2028. }
  2029. else
  2030. {
  2031. // We only have to do this checking in the situation where
  2032. // control flow falls through from the previous
  2033. // instruction. Otherwise merging is done at the time we
  2034. // push the branch. Note that we'll catch the
  2035. // off-the-end problem just below.
  2036. if (PC < current_method->code_length && states[PC] != NULL)
  2037. {
  2038. // We've already visited this instruction. So merge
  2039. // the states together. It is simplest, but not most
  2040. // efficient, to just always invalidate the PC here.
  2041. merge_into (PC, current_state);
  2042. invalidate_pc ();
  2043. continue;
  2044. }
  2045. }
  2046. // Control can't fall off the end of the bytecode. We need to
  2047. // check this in both cases, not just the fall-through case,
  2048. // because we don't check to see whether a `jsr' appears at
  2049. // the end of the bytecode until we process a `ret'.
  2050. if (PC >= current_method->code_length)
  2051. verify_fail ("fell off end");
  2052. // We only have to keep saved state at branch targets. If
  2053. // we're at a branch target and the state here hasn't been set
  2054. // yet, we set it now. You might notice that `ret' targets
  2055. // won't necessarily have FLAG_BRANCH_TARGET set. This
  2056. // doesn't matter, since those states will be filled in by
  2057. // merge_into.
  2058. if (states[PC] == NULL && (flags[PC] & FLAG_BRANCH_TARGET))
  2059. add_new_state (PC, current_state);
  2060. // Set this before handling exceptions so that debug output is
  2061. // sane.
  2062. start_PC = PC;
  2063. // Update states for all active exception handlers. Ordinarily
  2064. // there are not many exception handlers. So we simply run
  2065. // through them all.
  2066. for (int i = 0; i < current_method->exc_count; ++i)
  2067. {
  2068. if (PC >= exception[i].start_pc.i && PC < exception[i].end_pc.i)
  2069. {
  2070. type handler (&java::lang::Throwable::class$, this);
  2071. if (exception[i].handler_type.i != 0)
  2072. handler = check_class_constant (exception[i].handler_type.i);
  2073. push_exception_jump (handler, exception[i].handler_pc.i);
  2074. }
  2075. }
  2076. current_state->print (" ", PC, current_method->max_stack,
  2077. current_method->max_locals);
  2078. java_opcode opcode = (java_opcode) bytecode[PC++];
  2079. switch (opcode)
  2080. {
  2081. case op_nop:
  2082. break;
  2083. case op_aconst_null:
  2084. push_type (null_type);
  2085. break;
  2086. case op_iconst_m1:
  2087. case op_iconst_0:
  2088. case op_iconst_1:
  2089. case op_iconst_2:
  2090. case op_iconst_3:
  2091. case op_iconst_4:
  2092. case op_iconst_5:
  2093. push_type (int_type);
  2094. break;
  2095. case op_lconst_0:
  2096. case op_lconst_1:
  2097. push_type (long_type);
  2098. break;
  2099. case op_fconst_0:
  2100. case op_fconst_1:
  2101. case op_fconst_2:
  2102. push_type (float_type);
  2103. break;
  2104. case op_dconst_0:
  2105. case op_dconst_1:
  2106. push_type (double_type);
  2107. break;
  2108. case op_bipush:
  2109. get_byte ();
  2110. push_type (int_type);
  2111. break;
  2112. case op_sipush:
  2113. get_short ();
  2114. push_type (int_type);
  2115. break;
  2116. case op_ldc:
  2117. push_type (check_constant (get_byte ()));
  2118. break;
  2119. case op_ldc_w:
  2120. push_type (check_constant (get_ushort ()));
  2121. break;
  2122. case op_ldc2_w:
  2123. push_type (check_wide_constant (get_ushort ()));
  2124. break;
  2125. case op_iload:
  2126. push_type (get_variable (get_byte (), int_type));
  2127. break;
  2128. case op_lload:
  2129. push_type (get_variable (get_byte (), long_type));
  2130. break;
  2131. case op_fload:
  2132. push_type (get_variable (get_byte (), float_type));
  2133. break;
  2134. case op_dload:
  2135. push_type (get_variable (get_byte (), double_type));
  2136. break;
  2137. case op_aload:
  2138. push_type (get_variable (get_byte (), reference_type));
  2139. break;
  2140. case op_iload_0:
  2141. case op_iload_1:
  2142. case op_iload_2:
  2143. case op_iload_3:
  2144. push_type (get_variable (opcode - op_iload_0, int_type));
  2145. break;
  2146. case op_lload_0:
  2147. case op_lload_1:
  2148. case op_lload_2:
  2149. case op_lload_3:
  2150. push_type (get_variable (opcode - op_lload_0, long_type));
  2151. break;
  2152. case op_fload_0:
  2153. case op_fload_1:
  2154. case op_fload_2:
  2155. case op_fload_3:
  2156. push_type (get_variable (opcode - op_fload_0, float_type));
  2157. break;
  2158. case op_dload_0:
  2159. case op_dload_1:
  2160. case op_dload_2:
  2161. case op_dload_3:
  2162. push_type (get_variable (opcode - op_dload_0, double_type));
  2163. break;
  2164. case op_aload_0:
  2165. case op_aload_1:
  2166. case op_aload_2:
  2167. case op_aload_3:
  2168. push_type (get_variable (opcode - op_aload_0, reference_type));
  2169. break;
  2170. case op_iaload:
  2171. pop_type (int_type);
  2172. push_type (require_array_type (pop_init_ref (reference_type),
  2173. int_type));
  2174. break;
  2175. case op_laload:
  2176. pop_type (int_type);
  2177. push_type (require_array_type (pop_init_ref (reference_type),
  2178. long_type));
  2179. break;
  2180. case op_faload:
  2181. pop_type (int_type);
  2182. push_type (require_array_type (pop_init_ref (reference_type),
  2183. float_type));
  2184. break;
  2185. case op_daload:
  2186. pop_type (int_type);
  2187. push_type (require_array_type (pop_init_ref (reference_type),
  2188. double_type));
  2189. break;
  2190. case op_aaload:
  2191. pop_type (int_type);
  2192. push_type (require_array_type (pop_init_ref (reference_type),
  2193. reference_type));
  2194. break;
  2195. case op_baload:
  2196. pop_type (int_type);
  2197. require_array_type (pop_init_ref (reference_type), byte_type);
  2198. push_type (int_type);
  2199. break;
  2200. case op_caload:
  2201. pop_type (int_type);
  2202. require_array_type (pop_init_ref (reference_type), char_type);
  2203. push_type (int_type);
  2204. break;
  2205. case op_saload:
  2206. pop_type (int_type);
  2207. require_array_type (pop_init_ref (reference_type), short_type);
  2208. push_type (int_type);
  2209. break;
  2210. case op_istore:
  2211. set_variable (get_byte (), pop_type (int_type));
  2212. break;
  2213. case op_lstore:
  2214. set_variable (get_byte (), pop_type (long_type));
  2215. break;
  2216. case op_fstore:
  2217. set_variable (get_byte (), pop_type (float_type));
  2218. break;
  2219. case op_dstore:
  2220. set_variable (get_byte (), pop_type (double_type));
  2221. break;
  2222. case op_astore:
  2223. set_variable (get_byte (), pop_ref_or_return ());
  2224. break;
  2225. case op_istore_0:
  2226. case op_istore_1:
  2227. case op_istore_2:
  2228. case op_istore_3:
  2229. set_variable (opcode - op_istore_0, pop_type (int_type));
  2230. break;
  2231. case op_lstore_0:
  2232. case op_lstore_1:
  2233. case op_lstore_2:
  2234. case op_lstore_3:
  2235. set_variable (opcode - op_lstore_0, pop_type (long_type));
  2236. break;
  2237. case op_fstore_0:
  2238. case op_fstore_1:
  2239. case op_fstore_2:
  2240. case op_fstore_3:
  2241. set_variable (opcode - op_fstore_0, pop_type (float_type));
  2242. break;
  2243. case op_dstore_0:
  2244. case op_dstore_1:
  2245. case op_dstore_2:
  2246. case op_dstore_3:
  2247. set_variable (opcode - op_dstore_0, pop_type (double_type));
  2248. break;
  2249. case op_astore_0:
  2250. case op_astore_1:
  2251. case op_astore_2:
  2252. case op_astore_3:
  2253. set_variable (opcode - op_astore_0, pop_ref_or_return ());
  2254. break;
  2255. case op_iastore:
  2256. pop_type (int_type);
  2257. pop_type (int_type);
  2258. require_array_type (pop_init_ref (reference_type), int_type);
  2259. break;
  2260. case op_lastore:
  2261. pop_type (long_type);
  2262. pop_type (int_type);
  2263. require_array_type (pop_init_ref (reference_type), long_type);
  2264. break;
  2265. case op_fastore:
  2266. pop_type (float_type);
  2267. pop_type (int_type);
  2268. require_array_type (pop_init_ref (reference_type), float_type);
  2269. break;
  2270. case op_dastore:
  2271. pop_type (double_type);
  2272. pop_type (int_type);
  2273. require_array_type (pop_init_ref (reference_type), double_type);
  2274. break;
  2275. case op_aastore:
  2276. pop_type (reference_type);
  2277. pop_type (int_type);
  2278. require_array_type (pop_init_ref (reference_type), reference_type);
  2279. break;
  2280. case op_bastore:
  2281. pop_type (int_type);
  2282. pop_type (int_type);
  2283. require_array_type (pop_init_ref (reference_type), byte_type);
  2284. break;
  2285. case op_castore:
  2286. pop_type (int_type);
  2287. pop_type (int_type);
  2288. require_array_type (pop_init_ref (reference_type), char_type);
  2289. break;
  2290. case op_sastore:
  2291. pop_type (int_type);
  2292. pop_type (int_type);
  2293. require_array_type (pop_init_ref (reference_type), short_type);
  2294. break;
  2295. case op_pop:
  2296. pop32 ();
  2297. break;
  2298. case op_pop2:
  2299. {
  2300. type t = pop_raw ();
  2301. if (! t.iswide ())
  2302. pop32 ();
  2303. }
  2304. break;
  2305. case op_dup:
  2306. {
  2307. type t = pop32 ();
  2308. push_type (t);
  2309. push_type (t);
  2310. }
  2311. break;
  2312. case op_dup_x1:
  2313. {
  2314. type t1 = pop32 ();
  2315. type t2 = pop32 ();
  2316. push_type (t1);
  2317. push_type (t2);
  2318. push_type (t1);
  2319. }
  2320. break;
  2321. case op_dup_x2:
  2322. {
  2323. type t1 = pop32 ();
  2324. type t2 = pop_raw ();
  2325. if (! t2.iswide ())
  2326. {
  2327. type t3 = pop32 ();
  2328. push_type (t1);
  2329. push_type (t3);
  2330. }
  2331. else
  2332. push_type (t1);
  2333. push_type (t2);
  2334. push_type (t1);
  2335. }
  2336. break;
  2337. case op_dup2:
  2338. {
  2339. type t = pop_raw ();
  2340. if (! t.iswide ())
  2341. {
  2342. type t2 = pop32 ();
  2343. push_type (t2);
  2344. push_type (t);
  2345. push_type (t2);
  2346. }
  2347. else
  2348. push_type (t);
  2349. push_type (t);
  2350. }
  2351. break;
  2352. case op_dup2_x1:
  2353. {
  2354. type t1 = pop_raw ();
  2355. type t2 = pop32 ();
  2356. if (! t1.iswide ())
  2357. {
  2358. type t3 = pop32 ();
  2359. push_type (t2);
  2360. push_type (t1);
  2361. push_type (t3);
  2362. }
  2363. else
  2364. push_type (t1);
  2365. push_type (t2);
  2366. push_type (t1);
  2367. }
  2368. break;
  2369. case op_dup2_x2:
  2370. {
  2371. type t1 = pop_raw ();
  2372. if (t1.iswide ())
  2373. {
  2374. type t2 = pop_raw ();
  2375. if (t2.iswide ())
  2376. {
  2377. push_type (t1);
  2378. push_type (t2);
  2379. }
  2380. else
  2381. {
  2382. type t3 = pop32 ();
  2383. push_type (t1);
  2384. push_type (t3);
  2385. push_type (t2);
  2386. }
  2387. push_type (t1);
  2388. }
  2389. else
  2390. {
  2391. type t2 = pop32 ();
  2392. type t3 = pop_raw ();
  2393. if (t3.iswide ())
  2394. {
  2395. push_type (t2);
  2396. push_type (t1);
  2397. }
  2398. else
  2399. {
  2400. type t4 = pop32 ();
  2401. push_type (t2);
  2402. push_type (t1);
  2403. push_type (t4);
  2404. }
  2405. push_type (t3);
  2406. push_type (t2);
  2407. push_type (t1);
  2408. }
  2409. }
  2410. break;
  2411. case op_swap:
  2412. {
  2413. type t1 = pop32 ();
  2414. type t2 = pop32 ();
  2415. push_type (t1);
  2416. push_type (t2);
  2417. }
  2418. break;
  2419. case op_iadd:
  2420. case op_isub:
  2421. case op_imul:
  2422. case op_idiv:
  2423. case op_irem:
  2424. case op_ishl:
  2425. case op_ishr:
  2426. case op_iushr:
  2427. case op_iand:
  2428. case op_ior:
  2429. case op_ixor:
  2430. pop_type (int_type);
  2431. push_type (pop_type (int_type));
  2432. break;
  2433. case op_ladd:
  2434. case op_lsub:
  2435. case op_lmul:
  2436. case op_ldiv:
  2437. case op_lrem:
  2438. case op_land:
  2439. case op_lor:
  2440. case op_lxor:
  2441. pop_type (long_type);
  2442. push_type (pop_type (long_type));
  2443. break;
  2444. case op_lshl:
  2445. case op_lshr:
  2446. case op_lushr:
  2447. pop_type (int_type);
  2448. push_type (pop_type (long_type));
  2449. break;
  2450. case op_fadd:
  2451. case op_fsub:
  2452. case op_fmul:
  2453. case op_fdiv:
  2454. case op_frem:
  2455. pop_type (float_type);
  2456. push_type (pop_type (float_type));
  2457. break;
  2458. case op_dadd:
  2459. case op_dsub:
  2460. case op_dmul:
  2461. case op_ddiv:
  2462. case op_drem:
  2463. pop_type (double_type);
  2464. push_type (pop_type (double_type));
  2465. break;
  2466. case op_ineg:
  2467. case op_i2b:
  2468. case op_i2c:
  2469. case op_i2s:
  2470. push_type (pop_type (int_type));
  2471. break;
  2472. case op_lneg:
  2473. push_type (pop_type (long_type));
  2474. break;
  2475. case op_fneg:
  2476. push_type (pop_type (float_type));
  2477. break;
  2478. case op_dneg:
  2479. push_type (pop_type (double_type));
  2480. break;
  2481. case op_iinc:
  2482. get_variable (get_byte (), int_type);
  2483. get_byte ();
  2484. break;
  2485. case op_i2l:
  2486. pop_type (int_type);
  2487. push_type (long_type);
  2488. break;
  2489. case op_i2f:
  2490. pop_type (int_type);
  2491. push_type (float_type);
  2492. break;
  2493. case op_i2d:
  2494. pop_type (int_type);
  2495. push_type (double_type);
  2496. break;
  2497. case op_l2i:
  2498. pop_type (long_type);
  2499. push_type (int_type);
  2500. break;
  2501. case op_l2f:
  2502. pop_type (long_type);
  2503. push_type (float_type);
  2504. break;
  2505. case op_l2d:
  2506. pop_type (long_type);
  2507. push_type (double_type);
  2508. break;
  2509. case op_f2i:
  2510. pop_type (float_type);
  2511. push_type (int_type);
  2512. break;
  2513. case op_f2l:
  2514. pop_type (float_type);
  2515. push_type (long_type);
  2516. break;
  2517. case op_f2d:
  2518. pop_type (float_type);
  2519. push_type (double_type);
  2520. break;
  2521. case op_d2i:
  2522. pop_type (double_type);
  2523. push_type (int_type);
  2524. break;
  2525. case op_d2l:
  2526. pop_type (double_type);
  2527. push_type (long_type);
  2528. break;
  2529. case op_d2f:
  2530. pop_type (double_type);
  2531. push_type (float_type);
  2532. break;
  2533. case op_lcmp:
  2534. pop_type (long_type);
  2535. pop_type (long_type);
  2536. push_type (int_type);
  2537. break;
  2538. case op_fcmpl:
  2539. case op_fcmpg:
  2540. pop_type (float_type);
  2541. pop_type (float_type);
  2542. push_type (int_type);
  2543. break;
  2544. case op_dcmpl:
  2545. case op_dcmpg:
  2546. pop_type (double_type);
  2547. pop_type (double_type);
  2548. push_type (int_type);
  2549. break;
  2550. case op_ifeq:
  2551. case op_ifne:
  2552. case op_iflt:
  2553. case op_ifge:
  2554. case op_ifgt:
  2555. case op_ifle:
  2556. pop_type (int_type);
  2557. push_jump (get_short ());
  2558. break;
  2559. case op_if_icmpeq:
  2560. case op_if_icmpne:
  2561. case op_if_icmplt:
  2562. case op_if_icmpge:
  2563. case op_if_icmpgt:
  2564. case op_if_icmple:
  2565. pop_type (int_type);
  2566. pop_type (int_type);
  2567. push_jump (get_short ());
  2568. break;
  2569. case op_if_acmpeq:
  2570. case op_if_acmpne:
  2571. pop_type (reference_type);
  2572. pop_type (reference_type);
  2573. push_jump (get_short ());
  2574. break;
  2575. case op_goto:
  2576. push_jump (get_short ());
  2577. invalidate_pc ();
  2578. break;
  2579. case op_jsr:
  2580. handle_jsr_insn (get_short ());
  2581. break;
  2582. case op_ret:
  2583. handle_ret_insn (get_byte ());
  2584. break;
  2585. case op_tableswitch:
  2586. {
  2587. pop_type (int_type);
  2588. skip_padding ();
  2589. push_jump (get_int ());
  2590. jint low = get_int ();
  2591. jint high = get_int ();
  2592. // Already checked LOW -vs- HIGH.
  2593. for (int i = low; i <= high; ++i)
  2594. push_jump (get_int ());
  2595. invalidate_pc ();
  2596. }
  2597. break;
  2598. case op_lookupswitch:
  2599. {
  2600. pop_type (int_type);
  2601. skip_padding ();
  2602. push_jump (get_int ());
  2603. jint npairs = get_int ();
  2604. // Already checked NPAIRS >= 0.
  2605. jint lastkey = 0;
  2606. for (int i = 0; i < npairs; ++i)
  2607. {
  2608. jint key = get_int ();
  2609. if (i > 0 && key <= lastkey)
  2610. verify_fail ("lookupswitch pairs unsorted", start_PC);
  2611. lastkey = key;
  2612. push_jump (get_int ());
  2613. }
  2614. invalidate_pc ();
  2615. }
  2616. break;
  2617. case op_ireturn:
  2618. check_return_type (pop_type (int_type));
  2619. invalidate_pc ();
  2620. break;
  2621. case op_lreturn:
  2622. check_return_type (pop_type (long_type));
  2623. invalidate_pc ();
  2624. break;
  2625. case op_freturn:
  2626. check_return_type (pop_type (float_type));
  2627. invalidate_pc ();
  2628. break;
  2629. case op_dreturn:
  2630. check_return_type (pop_type (double_type));
  2631. invalidate_pc ();
  2632. break;
  2633. case op_areturn:
  2634. check_return_type (pop_init_ref (reference_type));
  2635. invalidate_pc ();
  2636. break;
  2637. case op_return:
  2638. // We only need to check this when the return type is
  2639. // void, because all instance initializers return void.
  2640. if (this_is_init)
  2641. current_state->check_this_initialized (this);
  2642. check_return_type (void_type);
  2643. invalidate_pc ();
  2644. break;
  2645. case op_getstatic:
  2646. push_type (check_field_constant (get_ushort ()));
  2647. break;
  2648. case op_putstatic:
  2649. pop_type (check_field_constant (get_ushort ()));
  2650. break;
  2651. case op_getfield:
  2652. {
  2653. type klass;
  2654. type field = check_field_constant (get_ushort (), &klass);
  2655. pop_type (klass);
  2656. push_type (field);
  2657. }
  2658. break;
  2659. case op_putfield:
  2660. {
  2661. type klass;
  2662. type field = check_field_constant (get_ushort (), &klass, true);
  2663. pop_type (field);
  2664. pop_type (klass);
  2665. }
  2666. break;
  2667. case op_invokevirtual:
  2668. case op_invokespecial:
  2669. case op_invokestatic:
  2670. case op_invokeinterface:
  2671. {
  2672. _Jv_Utf8Const *method_name, *method_signature;
  2673. type class_type
  2674. = check_method_constant (get_ushort (),
  2675. opcode == op_invokeinterface,
  2676. &method_name,
  2677. &method_signature);
  2678. // NARGS is only used when we're processing
  2679. // invokeinterface. It is simplest for us to compute it
  2680. // here and then verify it later.
  2681. int nargs = 0;
  2682. if (opcode == op_invokeinterface)
  2683. {
  2684. nargs = get_byte ();
  2685. if (get_byte () != 0)
  2686. verify_fail ("invokeinterface dummy byte is wrong");
  2687. }
  2688. bool is_init = false;
  2689. if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
  2690. {
  2691. is_init = true;
  2692. if (opcode != op_invokespecial)
  2693. verify_fail ("can't invoke <init>");
  2694. }
  2695. else if (method_name->first() == '<')
  2696. verify_fail ("can't invoke method starting with `<'");
  2697. // Pop arguments and check types.
  2698. int arg_count = _Jv_count_arguments (method_signature);
  2699. type arg_types[arg_count];
  2700. compute_argument_types (method_signature, arg_types);
  2701. for (int i = arg_count - 1; i >= 0; --i)
  2702. {
  2703. // This is only used for verifying the byte for
  2704. // invokeinterface.
  2705. nargs -= arg_types[i].depth ();
  2706. pop_init_ref (arg_types[i]);
  2707. }
  2708. if (opcode == op_invokeinterface
  2709. && nargs != 1)
  2710. verify_fail ("wrong argument count for invokeinterface");
  2711. if (opcode != op_invokestatic)
  2712. {
  2713. type t = class_type;
  2714. if (is_init)
  2715. {
  2716. // In this case the PC doesn't matter.
  2717. t.set_uninitialized (type::UNINIT, this);
  2718. // FIXME: check to make sure that the <init>
  2719. // call is to the right class.
  2720. // It must either be super or an exact class
  2721. // match.
  2722. }
  2723. type raw = pop_raw ();
  2724. if (! t.compatible (raw, this))
  2725. verify_fail ("incompatible type on stack");
  2726. if (is_init)
  2727. current_state->set_initialized (raw.get_pc (),
  2728. current_method->max_locals);
  2729. }
  2730. type rt = compute_return_type (method_signature);
  2731. if (! rt.isvoid ())
  2732. push_type (rt);
  2733. }
  2734. break;
  2735. case op_new:
  2736. {
  2737. type t = check_class_constant (get_ushort ());
  2738. if (t.isarray ())
  2739. verify_fail ("type is array");
  2740. t.set_uninitialized (start_PC, this);
  2741. push_type (t);
  2742. }
  2743. break;
  2744. case op_newarray:
  2745. {
  2746. int atype = get_byte ();
  2747. // We intentionally have chosen constants to make this
  2748. // valid.
  2749. if (atype < boolean_type || atype > long_type)
  2750. verify_fail ("type not primitive", start_PC);
  2751. pop_type (int_type);
  2752. type t (construct_primitive_array_type (type_val (atype)), this);
  2753. push_type (t);
  2754. }
  2755. break;
  2756. case op_anewarray:
  2757. pop_type (int_type);
  2758. push_type (check_class_constant (get_ushort ()).to_array (this));
  2759. break;
  2760. case op_arraylength:
  2761. {
  2762. type t = pop_init_ref (reference_type);
  2763. if (! t.isarray () && ! t.isnull ())
  2764. verify_fail ("array type expected");
  2765. push_type (int_type);
  2766. }
  2767. break;
  2768. case op_athrow:
  2769. pop_type (type (&java::lang::Throwable::class$, this));
  2770. invalidate_pc ();
  2771. break;
  2772. case op_checkcast:
  2773. pop_init_ref (reference_type);
  2774. push_type (check_class_constant (get_ushort ()));
  2775. break;
  2776. case op_instanceof:
  2777. pop_init_ref (reference_type);
  2778. check_class_constant (get_ushort ());
  2779. push_type (int_type);
  2780. break;
  2781. case op_monitorenter:
  2782. pop_init_ref (reference_type);
  2783. break;
  2784. case op_monitorexit:
  2785. pop_init_ref (reference_type);
  2786. break;
  2787. case op_wide:
  2788. {
  2789. switch (get_byte ())
  2790. {
  2791. case op_iload:
  2792. push_type (get_variable (get_ushort (), int_type));
  2793. break;
  2794. case op_lload:
  2795. push_type (get_variable (get_ushort (), long_type));
  2796. break;
  2797. case op_fload:
  2798. push_type (get_variable (get_ushort (), float_type));
  2799. break;
  2800. case op_dload:
  2801. push_type (get_variable (get_ushort (), double_type));
  2802. break;
  2803. case op_aload:
  2804. push_type (get_variable (get_ushort (), reference_type));
  2805. break;
  2806. case op_istore:
  2807. set_variable (get_ushort (), pop_type (int_type));
  2808. break;
  2809. case op_lstore:
  2810. set_variable (get_ushort (), pop_type (long_type));
  2811. break;
  2812. case op_fstore:
  2813. set_variable (get_ushort (), pop_type (float_type));
  2814. break;
  2815. case op_dstore:
  2816. set_variable (get_ushort (), pop_type (double_type));
  2817. break;
  2818. case op_astore:
  2819. set_variable (get_ushort (), pop_init_ref (reference_type));
  2820. break;
  2821. case op_ret:
  2822. handle_ret_insn (get_short ());
  2823. break;
  2824. case op_iinc:
  2825. get_variable (get_ushort (), int_type);
  2826. get_short ();
  2827. break;
  2828. default:
  2829. verify_fail ("unrecognized wide instruction", start_PC);
  2830. }
  2831. }
  2832. break;
  2833. case op_multianewarray:
  2834. {
  2835. type atype = check_class_constant (get_ushort ());
  2836. int dim = get_byte ();
  2837. if (dim < 1)
  2838. verify_fail ("too few dimensions to multianewarray", start_PC);
  2839. atype.verify_dimensions (dim, this);
  2840. for (int i = 0; i < dim; ++i)
  2841. pop_type (int_type);
  2842. push_type (atype);
  2843. }
  2844. break;
  2845. case op_ifnull:
  2846. case op_ifnonnull:
  2847. pop_type (reference_type);
  2848. push_jump (get_short ());
  2849. break;
  2850. case op_goto_w:
  2851. push_jump (get_int ());
  2852. invalidate_pc ();
  2853. break;
  2854. case op_jsr_w:
  2855. handle_jsr_insn (get_int ());
  2856. break;
  2857. // These are unused here, but we call them out explicitly
  2858. // so that -Wswitch-enum doesn't complain.
  2859. case op_putfield_1:
  2860. case op_putfield_2:
  2861. case op_putfield_4:
  2862. case op_putfield_8:
  2863. case op_putfield_a:
  2864. case op_putstatic_1:
  2865. case op_putstatic_2:
  2866. case op_putstatic_4:
  2867. case op_putstatic_8:
  2868. case op_putstatic_a:
  2869. case op_getfield_1:
  2870. case op_getfield_2s:
  2871. case op_getfield_2u:
  2872. case op_getfield_4:
  2873. case op_getfield_8:
  2874. case op_getfield_a:
  2875. case op_getstatic_1:
  2876. case op_getstatic_2s:
  2877. case op_getstatic_2u:
  2878. case op_getstatic_4:
  2879. case op_getstatic_8:
  2880. case op_getstatic_a:
  2881. case op_breakpoint:
  2882. default:
  2883. // Unrecognized opcode.
  2884. verify_fail ("unrecognized instruction in verify_instructions_0",
  2885. start_PC);
  2886. }
  2887. }
  2888. }
  2889. public:
  2890. void verify_instructions ()
  2891. {
  2892. branch_prepass ();
  2893. verify_instructions_0 ();
  2894. }
  2895. _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
  2896. {
  2897. // We just print the text as utf-8. This is just for debugging
  2898. // anyway.
  2899. debug_print ("--------------------------------\n");
  2900. debug_print ("-- Verifying method `%s'\n", m->self->name->chars());
  2901. current_method = m;
  2902. bytecode = m->bytecode ();
  2903. exception = m->exceptions ();
  2904. current_class = m->defining_class;
  2905. states = NULL;
  2906. flags = NULL;
  2907. utf8_list = NULL;
  2908. isect_list = NULL;
  2909. }
  2910. ~_Jv_BytecodeVerifier ()
  2911. {
  2912. if (flags)
  2913. _Jv_Free (flags);
  2914. while (utf8_list != NULL)
  2915. {
  2916. linked<_Jv_Utf8Const> *n = utf8_list->next;
  2917. _Jv_Free (utf8_list);
  2918. utf8_list = n;
  2919. }
  2920. while (isect_list != NULL)
  2921. {
  2922. ref_intersection *next = isect_list->alloc_next;
  2923. delete isect_list;
  2924. isect_list = next;
  2925. }
  2926. if (states)
  2927. {
  2928. for (int i = 0; i < current_method->code_length; ++i)
  2929. {
  2930. linked<state> *iter = states[i];
  2931. while (iter != NULL)
  2932. {
  2933. linked<state> *next = iter->next;
  2934. delete iter->val;
  2935. _Jv_Free (iter);
  2936. iter = next;
  2937. }
  2938. }
  2939. _Jv_Free (states);
  2940. }
  2941. }
  2942. };
  2943. void
  2944. _Jv_VerifyMethod (_Jv_InterpMethod *meth)
  2945. {
  2946. _Jv_BytecodeVerifier v (meth);
  2947. v.verify_instructions ();
  2948. }
  2949. #endif /* INTERPRETER */