verify-impl.c 82 KB

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