You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InstConstraintVisitor.java 86KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445
  1. package org.aspectj.apache.bcel.verifier.structurals;
  2. /* ====================================================================
  3. * The Apache Software License, Version 1.1
  4. *
  5. * Copyright (c) 2001 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Apache" and "Apache Software Foundation" and
  28. * "Apache BCEL" must not be used to endorse or promote products
  29. * derived from this software without prior written permission. For
  30. * written permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * "Apache BCEL", nor may "Apache" appear in their name, without
  34. * prior written permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation. For more
  52. * information on the Apache Software Foundation, please see
  53. * <http://www.apache.org/>.
  54. */
  55. import org.aspectj.apache.bcel.Constants;
  56. import org.aspectj.apache.bcel.Repository;
  57. import org.aspectj.apache.bcel.classfile.Constant;
  58. import org.aspectj.apache.bcel.classfile.ConstantClass;
  59. import org.aspectj.apache.bcel.classfile.ConstantDouble;
  60. import org.aspectj.apache.bcel.classfile.ConstantInteger;
  61. import org.aspectj.apache.bcel.classfile.ConstantFieldref;
  62. import org.aspectj.apache.bcel.classfile.ConstantFloat;
  63. import org.aspectj.apache.bcel.classfile.ConstantLong;
  64. import org.aspectj.apache.bcel.classfile.ConstantString;
  65. import org.aspectj.apache.bcel.classfile.ConstantPool;
  66. import org.aspectj.apache.bcel.classfile.Field;
  67. import org.aspectj.apache.bcel.classfile.JavaClass;
  68. import org.aspectj.apache.bcel.verifier.EmptyInstVisitor;
  69. import org.aspectj.apache.bcel.verifier.VerificationResult;
  70. import org.aspectj.apache.bcel.verifier.Verifier;
  71. import org.aspectj.apache.bcel.verifier.VerifierFactory;
  72. import org.aspectj.apache.bcel.verifier.exc.*;
  73. import org.aspectj.apache.bcel.generic.ArrayType;
  74. import org.aspectj.apache.bcel.generic.FieldInstruction;
  75. import org.aspectj.apache.bcel.generic.IINC;
  76. import org.aspectj.apache.bcel.generic.INVOKEINTERFACE;
  77. import org.aspectj.apache.bcel.generic.Instruction;
  78. import org.aspectj.apache.bcel.generic.InstructionBranch;
  79. import org.aspectj.apache.bcel.generic.InstructionLV;
  80. import org.aspectj.apache.bcel.generic.InvokeInstruction;
  81. import org.aspectj.apache.bcel.generic.LOOKUPSWITCH;
  82. import org.aspectj.apache.bcel.generic.MULTIANEWARRAY;
  83. import org.aspectj.apache.bcel.generic.MethodGen;
  84. import org.aspectj.apache.bcel.generic.ObjectType;
  85. import org.aspectj.apache.bcel.generic.RET;
  86. import org.aspectj.apache.bcel.generic.ReferenceType;
  87. import org.aspectj.apache.bcel.generic.ReturnaddressType;
  88. import org.aspectj.apache.bcel.generic.TABLESWITCH;
  89. import org.aspectj.apache.bcel.generic.Type;
  90. /**
  91. * A Visitor class testing for valid preconditions of JVM instructions.
  92. * The instance of this class will throw a StructuralCodeConstraintException
  93. * instance if an instruction is visitXXX()ed which has preconditions that are
  94. * not satisfied.
  95. * TODO: Currently, the JVM's behaviour concerning monitors (MONITORENTER,
  96. * MONITOREXIT) is not modeled in JustIce.
  97. *
  98. * @version $Id: InstConstraintVisitor.java,v 1.3 2008/08/28 00:02:13 aclement Exp $
  99. * @author <A HREF="http://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
  100. * @see org.aspectj.apache.bcel.verifier.exc.StructuralCodeConstraintException
  101. * @see org.aspectj.apache.bcel.verifier.exc.LinkingConstraintException
  102. */
  103. public class InstConstraintVisitor extends EmptyInstVisitor {
  104. private static ObjectType GENERIC_ARRAY = new ObjectType("org.aspectj.apache.bcel.verifier.structurals.GenericArray");
  105. /**
  106. * The constructor. Constructs a new instance of this class.
  107. */
  108. public InstConstraintVisitor(){}
  109. /**
  110. * The Execution Frame we're working on.
  111. *
  112. * @see #setFrame(Frame f)
  113. * @see #locals()
  114. * @see #stack()
  115. */
  116. private Frame frame = null;
  117. private ConstantPool cpg = null;
  118. /**
  119. * The MethodGen we're working on.
  120. *
  121. * @see #setMethodGen(MethodGen mg)
  122. */
  123. private MethodGen mg = null;
  124. /**
  125. * The OperandStack we're working on.
  126. *
  127. * @see #setFrame(Frame f)
  128. */
  129. private OperandStack stack(){
  130. return frame.getStack();
  131. }
  132. /**
  133. * The LocalVariables we're working on.
  134. *
  135. * @see #setFrame(Frame f)
  136. */
  137. private LocalVariables locals(){
  138. return frame.getLocals();
  139. }
  140. /**
  141. * This method is called by the visitXXX() to notify the acceptor of this InstConstraintVisitor
  142. * that a constraint violation has occured. This is done by throwing an instance of a
  143. * StructuralCodeConstraintException.
  144. * @throws org.aspectj.apache.bcel.verifier.exc.StructuralCodeConstraintException always.
  145. */
  146. private void constraintViolated(Instruction violator, String description){
  147. String fq_classname = violator.getClass().getName();
  148. throw new StructuralCodeConstraintException("Instruction "+ fq_classname.substring(fq_classname.lastIndexOf('.')+1) +" constraint violated: " + description);
  149. }
  150. /**
  151. * This returns the single instance of the InstConstraintVisitor class.
  152. * To operate correctly, other values must have been set before actually
  153. * using the instance.
  154. * Use this method for performance reasons.
  155. *
  156. * @see #setConstantPoolGen(ConstantPoolGen cpg)
  157. * @see #setMethodGen(MethodGen mg)
  158. */
  159. public void setFrame(Frame f){
  160. this.frame = f;
  161. //if (singleInstance.mg == null || singleInstance.cpg == null) throw new AssertionViolatedException("Forgot to set important values first.");
  162. }
  163. /**
  164. * Sets the ConstantPoolGen instance needed for constraint
  165. * checking prior to execution.
  166. */
  167. public void setConstantPoolGen(ConstantPool cpg){
  168. this.cpg = cpg;
  169. }
  170. /**
  171. * Sets the MethodGen instance needed for constraint
  172. * checking prior to execution.
  173. */
  174. public void setMethodGen(MethodGen mg){
  175. this.mg = mg;
  176. }
  177. /**
  178. * Assures index is of type INT.
  179. * @throws org.aspectj.apache.bcel.verifier.exc.StructuralCodeConstraintException if the above constraint is not satisfied.
  180. */
  181. private void indexOfInt(Instruction o, Type index){
  182. if (! index.equals(Type.INT))
  183. constraintViolated(o, "The 'index' is not of type int but of type "+index+".");
  184. }
  185. /**
  186. * Assures the ReferenceType r is initialized (or Type.NULL).
  187. * Formally, this means (!(r instanceof UninitializedObjectType)), because
  188. * there are no uninitialized array types.
  189. * @throws org.aspectj.apache.bcel.verifier.exc.StructuralCodeConstraintException if the above constraint is not satisfied.
  190. */
  191. private void referenceTypeIsInitialized(Instruction o, ReferenceType r){
  192. if (r instanceof UninitializedObjectType){
  193. constraintViolated(o, "Working on an uninitialized object '"+r+"'.");
  194. }
  195. }
  196. /** Assures value is of type INT. */
  197. private void valueOfInt(Instruction o, Type value){
  198. if (! value.equals(Type.INT))
  199. constraintViolated(o, "The 'value' is not of type int but of type "+value+".");
  200. }
  201. /**
  202. * Assures arrayref is of ArrayType or NULL;
  203. * returns true if and only if arrayref is non-NULL.
  204. * @throws org.aspectj.apache.bcel.verifier.exc.StructuralCodeConstraintException if the above constraint is violated.
  205. */
  206. private boolean arrayrefOfArrayType(Instruction o, Type arrayref){
  207. if (! ((arrayref instanceof ArrayType) || arrayref.equals(Type.NULL)) )
  208. constraintViolated(o, "The 'arrayref' does not refer to an array but is of type "+arrayref+".");
  209. return (arrayref instanceof ArrayType);
  210. }
  211. /***************************************************************/
  212. /* MISC */
  213. /***************************************************************/
  214. /**
  215. * Ensures the general preconditions of an instruction that accesses the stack.
  216. * This method is here because BCEL has no such superinterface for the stack
  217. * accessing instructions; and there are funny unexpected exceptions in the
  218. * semantices of the superinterfaces and superclasses provided.
  219. * E.g. SWAP is a StackConsumer, but DUP_X1 is not a StackProducer.
  220. * Therefore, this method is called by all StackProducer, StackConsumer,
  221. * and StackInstruction instances via their visitXXX() method.
  222. * Unfortunately, as the superclasses and superinterfaces overlap, some instructions
  223. * cause this method to be called two or three times. [TODO: Fix this.]
  224. *
  225. * @see #visitStackConsumer(StackConsumer o)
  226. * @see #visitStackProducer(StackProducer o)
  227. * @see #visitStackInstruction(StackInstruction o)
  228. */
  229. private void _visitStackAccessor(Instruction o){
  230. int consume = o.consumeStack(cpg); // Stack values are always consumed first; then produced.
  231. if (consume > stack().slotsUsed()){
  232. constraintViolated((Instruction) o, "Cannot consume "+consume+" stack slots: only "+stack().slotsUsed()+" slot(s) left on stack!\nStack:\n"+stack());
  233. }
  234. int produce = o.produceStack(cpg) - ((Instruction) o).consumeStack(cpg); // Stack values are always consumed first; then produced.
  235. if ( produce + stack().slotsUsed() > stack().maxStack() ){
  236. constraintViolated((Instruction) o, "Cannot produce "+produce+" stack slots: only "+(stack().maxStack()-stack().slotsUsed())+" free stack slot(s) left.\nStack:\n"+stack());
  237. }
  238. }
  239. /***************************************************************/
  240. /* "generic"visitXXXX methods where XXXX is an interface */
  241. /* therefore, we don't know the order of visiting; but we know */
  242. /* these methods are called before the visitYYYY methods below */
  243. /***************************************************************/
  244. /**
  245. * Assures the generic preconditions of a LoadClass instance.
  246. * The referenced class is loaded and pass2-verified.
  247. */
  248. public void visitLoadClass(Instruction o){
  249. ObjectType t = o.getLoadClassType(cpg);
  250. if (t != null){// null means "no class is loaded"
  251. Verifier v = VerifierFactory.getVerifier(t.getClassName());
  252. VerificationResult vr = v.doPass2();
  253. if (vr.getStatus() != VerificationResult.VERIFIED_OK){
  254. constraintViolated((Instruction) o, "Class '"+o.getLoadClassType(cpg).getClassName()+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
  255. }
  256. }
  257. }
  258. /**
  259. * Ensures the general preconditions of a StackConsumer instance.
  260. */
  261. public void visitStackConsumer(Instruction o){
  262. _visitStackAccessor(o);
  263. }
  264. /**
  265. * Ensures the general preconditions of a StackProducer instance.
  266. */
  267. public void visitStackProducer(Instruction o){
  268. _visitStackAccessor(o);
  269. }
  270. /***************************************************************/
  271. /* "generic" visitYYYY methods where YYYY is a superclass. */
  272. /* therefore, we know the order of visiting; we know */
  273. /* these methods are called after the visitXXXX methods above. */
  274. /***************************************************************/
  275. public void visitCPInstruction(Instruction o){
  276. int idx = o.getIndex();
  277. if ((idx < 0) || (idx >= cpg.getSize())){
  278. throw new AssertionViolatedException("Huh?! Constant pool index of instruction '"+o+"' illegal? Pass 3a should have checked this!");
  279. }
  280. }
  281. public void visitFieldInstruction(Instruction o){
  282. // visitLoadClass(o) has been called before: Every FieldOrMethod
  283. // implements LoadClass.
  284. // visitCPInstruction(o) has been called before.
  285. // A FieldInstruction may be: GETFIELD, GETSTATIC, PUTFIELD, PUTSTATIC
  286. Constant c = cpg.getConstant(o.getIndex());
  287. if (!(c instanceof ConstantFieldref)){
  288. constraintViolated(o, "Index '"+o.getIndex()+"' should refer to a CONSTANT_Fieldref_info structure, but refers to '"+c+"'.");
  289. }
  290. // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
  291. Type t = o.getType(cpg);
  292. if (t instanceof ObjectType){
  293. String name = ((ObjectType)t).getClassName();
  294. Verifier v = VerifierFactory.getVerifier( name );
  295. VerificationResult vr = v.doPass2();
  296. if (vr.getStatus() != VerificationResult.VERIFIED_OK){
  297. constraintViolated((Instruction) o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
  298. }
  299. }
  300. }
  301. public void visitInvokeInstruction(InvokeInstruction o){
  302. // visitLoadClass(o) has been called before: Every FieldOrMethod
  303. // implements LoadClass.
  304. // visitCPInstruction(o) has been called before.
  305. //TODO
  306. }
  307. public void visitStackInstruction(Instruction o){
  308. _visitStackAccessor(o);
  309. }
  310. /**
  311. * Assures the generic preconditions of a LocalVariableInstruction instance.
  312. * That is, the index of the local variable must be valid.
  313. */
  314. public void visitLocalVariableInstruction(InstructionLV o){
  315. if (locals().maxLocals() <= (o.getType(cpg).getSize()==1? o.getIndex() : o.getIndex()+1) ){
  316. constraintViolated(o, "The 'index' is not a valid index into the local variable array.");
  317. }
  318. }
  319. public void visitLoadInstruction(Instruction o){
  320. //visitLocalVariableInstruction(o) is called before, because it is more generic.
  321. // LOAD instructions must not read Type.UNKNOWN
  322. if (locals().get(o.getIndex()) == Type.UNKNOWN){
  323. constraintViolated(o, "Read-Access on local variable "+o.getIndex()+" with unknown content.");
  324. }
  325. // LOAD instructions, two-slot-values at index N must have Type.UNKNOWN
  326. // as a symbol for the higher halve at index N+1
  327. // [suppose some instruction put an int at N+1--- our double at N is defective]
  328. if (o.getType(cpg).getSize() == 2){
  329. if (locals().get(o.getIndex()+1) != Type.UNKNOWN){
  330. constraintViolated(o, "Reading a two-locals value from local variables "+o.getIndex()+" and "+(o.getIndex()+1)+" where the latter one is destroyed.");
  331. }
  332. }
  333. // LOAD instructions must read the correct type.
  334. if (!o.isALOAD()){
  335. if (locals().get(o.getIndex()) != o.getType(cpg) ){
  336. constraintViolated(o, "Local Variable type and LOADing Instruction type mismatch: Local Variable: '"+locals().get(o.getIndex())+"'; Instruction type: '"+o.getType(cpg)+"'.");
  337. }
  338. }
  339. else{ // we deal with an ALOAD
  340. if (!(locals().get(o.getIndex()) instanceof ReferenceType)){
  341. constraintViolated(o, "Local Variable type and LOADing Instruction type mismatch: Local Variable: '"+locals().get(o.getIndex())+"'; Instruction expects a ReferenceType.");
  342. }
  343. // ALOAD __IS ALLOWED__ to put uninitialized objects onto the stack!
  344. //referenceTypeIsInitialized(o, (ReferenceType) (locals().get(o.getIndex())));
  345. }
  346. // LOAD instructions must have enough free stack slots.
  347. if ((stack().maxStack() - stack().slotsUsed()) < o.getType(cpg).getSize()){
  348. constraintViolated(o, "Not enough free stack slots to load a '"+o.getType(cpg)+"' onto the OperandStack.");
  349. }
  350. }
  351. public void visitStoreInstruction(Instruction o){
  352. //visitLocalVariableInstruction(o) is called before, because it is more generic.
  353. if (stack().isEmpty()){ // Don't bother about 1 or 2 stack slots used. This check is implicitely done below while type checking.
  354. constraintViolated(o, "Cannot STORE: Stack to read from is empty.");
  355. }
  356. if (!o.isASTORE() ){
  357. if (! (stack().peek() == o.getType(cpg)) ){// the other xSTORE types are singletons in BCEL.
  358. constraintViolated(o, "Stack top type and STOREing Instruction type mismatch: Stack top: '"+stack().peek()+"'; Instruction type: '"+o.getType(cpg)+"'.");
  359. }
  360. }
  361. else{ // we deal with ASTORE
  362. Type stacktop = stack().peek();
  363. if ( (!(stacktop instanceof ReferenceType)) && (!(stacktop instanceof ReturnaddressType)) ){
  364. constraintViolated(o, "Stack top type and STOREing Instruction type mismatch: Stack top: '"+stack().peek()+"'; Instruction expects a ReferenceType or a ReturnadressType.");
  365. }
  366. if (stacktop instanceof ReferenceType){
  367. referenceTypeIsInitialized(o, (ReferenceType) stacktop);
  368. }
  369. }
  370. }
  371. public void visitReturnInstruction(Instruction o){
  372. if (o.getOpcode()==Constants.RETURN){
  373. return;
  374. }
  375. if (o.getOpcode()==Constants.ARETURN){
  376. if (stack().peek() == Type.NULL){
  377. return;
  378. }
  379. else{
  380. if (! (stack().peek() instanceof ReferenceType)){
  381. constraintViolated(o, "Reference type expected on top of stack, but is: '"+stack().peek()+"'.");
  382. }
  383. referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()));
  384. //ReferenceType objectref = (ReferenceType) (stack().peek());
  385. // TODO: This can only be checked if using Staerk-et-al's "set of object types" instead of a
  386. // "wider cast object type" created during verification.
  387. //if (! (objectref.isAssignmentCompatibleWith(mg.getType())) ){
  388. // constraintViolated(o, "Type on stack top which should be returned is a '"+stack().peek()+"' which is not assignment compatible with the return type of this method, '"+mg.getType()+"'.");
  389. //}
  390. }
  391. }
  392. else{
  393. Type method_type = mg.getType();
  394. if (method_type == Type.BOOLEAN ||
  395. method_type == Type.BYTE ||
  396. method_type == Type.SHORT ||
  397. method_type == Type.CHAR){
  398. method_type = Type.INT;
  399. }
  400. if (! ( method_type.equals( stack().peek() ))){
  401. constraintViolated(o, "Current method has return type of '"+mg.getType()+"' expecting a '"+method_type+"' on top of the stack. But stack top is a '"+stack().peek()+"'.");
  402. }
  403. }
  404. }
  405. /***************************************************************/
  406. /* "special"visitXXXX methods for one type of instruction each */
  407. /***************************************************************/
  408. public void visitAALOAD(Instruction o){
  409. Type arrayref = stack().peek(1);
  410. Type index = stack().peek(0);
  411. indexOfInt(o, index);
  412. if (arrayrefOfArrayType(o, arrayref)){
  413. if (! (((ArrayType) arrayref).getElementType() instanceof ReferenceType)){
  414. constraintViolated(o, "The 'arrayref' does not refer to an array with elements of a ReferenceType but to an array of "+((ArrayType) arrayref).getElementType()+".");
  415. }
  416. referenceTypeIsInitialized(o, (ReferenceType) (((ArrayType) arrayref).getElementType()));
  417. }
  418. }
  419. /**
  420. * Ensures the specific preconditions of the said instruction.
  421. */
  422. public void visitAASTORE(Instruction o){
  423. Type arrayref = stack().peek(2);
  424. Type index = stack().peek(1);
  425. Type value = stack().peek(0);
  426. indexOfInt(o, index);
  427. if (!(value instanceof ReferenceType)){
  428. constraintViolated(o, "The 'value' is not of a ReferenceType but of type "+value+".");
  429. }else{
  430. referenceTypeIsInitialized(o, (ReferenceType) value);
  431. }
  432. // Don't bother further with "referenceTypeIsInitialized()", there are no arrays
  433. // of an uninitialized object type.
  434. if (arrayrefOfArrayType(o, arrayref)){
  435. if (! (((ArrayType) arrayref).getElementType() instanceof ReferenceType)){
  436. constraintViolated(o, "The 'arrayref' does not refer to an array with elements of a ReferenceType but to an array of "+((ArrayType) arrayref).getElementType()+".");
  437. }
  438. if (! ((ReferenceType)value).isAssignmentCompatibleWith((ReferenceType) ((ArrayType) arrayref).getElementType())){
  439. constraintViolated(o, "The type of 'value' ('"+value+"') is not assignment compatible to the components of the array 'arrayref' refers to. ('"+((ArrayType) arrayref).getElementType()+"')");
  440. }
  441. }
  442. }
  443. public void visitACONST_NULL(Instruction o){
  444. // Nothing needs to be done here.
  445. }
  446. public void visitALOAD(Instruction o){
  447. //visitLoadInstruction(LoadInstruction) is called before.
  448. // Nothing else needs to be done here.
  449. }
  450. public void visitANEWARRAY(Instruction o){
  451. if (!stack().peek().equals(Type.INT))
  452. constraintViolated(o, "The 'count' at the stack top is not of type '"+Type.INT+"' but of type '"+stack().peek()+"'.");
  453. // The runtime constant pool item at that index must be a symbolic reference to a class,
  454. // array, or interface type. See Pass 3a.
  455. }
  456. /**
  457. * Ensures the specific preconditions of the said instruction.
  458. */
  459. public void visitARETURN(Instruction o){
  460. if (! (stack().peek() instanceof ReferenceType) ){
  461. constraintViolated(o, "The 'objectref' at the stack top is not of a ReferenceType but of type '"+stack().peek()+"'.");
  462. }
  463. ReferenceType objectref = (ReferenceType) (stack().peek());
  464. referenceTypeIsInitialized(o, objectref);
  465. // The check below should already done via visitReturnInstruction(ReturnInstruction), see there.
  466. // It cannot be done using Staerk-et-al's "set of object types" instead of a
  467. // "wider cast object type", anyway.
  468. //if (! objectref.isAssignmentCompatibleWith(mg.getReturnType() )){
  469. // constraintViolated(o, "The 'objectref' type "+objectref+" at the stack top is not assignment compatible with the return type '"+mg.getReturnType()+"' of the method.");
  470. //}
  471. }
  472. /**
  473. * Ensures the specific preconditions of the said instruction.
  474. */
  475. public void visitARRAYLENGTH(Instruction o){
  476. Type arrayref = stack().peek(0);
  477. arrayrefOfArrayType(o, arrayref);
  478. }
  479. /**
  480. * Ensures the specific preconditions of the said instruction.
  481. */
  482. public void visitASTORE(Instruction o){
  483. if (! ( (stack().peek() instanceof ReferenceType) || (stack().peek() instanceof ReturnaddressType) ) ){
  484. constraintViolated(o, "The 'objectref' is not of a ReferenceType or of ReturnaddressType but of "+stack().peek()+".");
  485. }
  486. if (stack().peek() instanceof ReferenceType){
  487. referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
  488. }
  489. }
  490. /**
  491. * Ensures the specific preconditions of the said instruction.
  492. */
  493. public void visitATHROW(Instruction o){
  494. // It's stated that 'objectref' must be of a ReferenceType --- but since Throwable is
  495. // not derived from an ArrayType, it follows that 'objectref' must be of an ObjectType or Type.NULL.
  496. if (! ((stack().peek() instanceof ObjectType) || (stack().peek().equals(Type.NULL))) ){
  497. constraintViolated(o, "The 'objectref' is not of an (initialized) ObjectType but of type "+stack().peek()+".");
  498. }
  499. // NULL is a subclass of every class, so to speak.
  500. if (stack().peek().equals(Type.NULL)) return;
  501. ObjectType exc = (ObjectType) (stack().peek());
  502. ObjectType throwable = (ObjectType) (Type.getType("Ljava/lang/Throwable;"));
  503. if ( (! (exc.subclassOf(throwable)) ) && (! (exc.equals(throwable))) ){
  504. constraintViolated(o, "The 'objectref' is not of class Throwable or of a subclass of Throwable, but of '"+stack().peek()+"'.");
  505. }
  506. }
  507. /**
  508. * Ensures the specific preconditions of the said instruction.
  509. */
  510. public void visitBALOAD(Instruction o){
  511. Type arrayref = stack().peek(1);
  512. Type index = stack().peek(0);
  513. indexOfInt(o, index);
  514. if (arrayrefOfArrayType(o, arrayref)){
  515. if (! ( (((ArrayType) arrayref).getElementType().equals(Type.BOOLEAN)) ||
  516. (((ArrayType) arrayref).getElementType().equals(Type.BYTE)) ) ){
  517. constraintViolated(o, "The 'arrayref' does not refer to an array with elements of a Type.BYTE or Type.BOOLEAN but to an array of '"+((ArrayType) arrayref).getElementType()+"'.");
  518. }
  519. }
  520. }
  521. /**
  522. * Ensures the specific preconditions of the said instruction.
  523. */
  524. public void visitBASTORE(Instruction o){
  525. Type arrayref = stack().peek(2);
  526. Type index = stack().peek(1);
  527. Type value = stack().peek(0);
  528. indexOfInt(o, index);
  529. valueOfInt(o, value);
  530. if (arrayrefOfArrayType(o, arrayref)){
  531. if (! ( (((ArrayType) arrayref).getElementType().equals(Type.BOOLEAN)) ||
  532. (((ArrayType) arrayref).getElementType().equals(Type.BYTE)) ) )
  533. constraintViolated(o, "The 'arrayref' does not refer to an array with elements of a Type.BYTE or Type.BOOLEAN but to an array of '"+((ArrayType) arrayref).getElementType()+"'.");
  534. }
  535. }
  536. /**
  537. * Ensures the specific preconditions of the said instruction.
  538. */
  539. public void visitBIPUSH(Instruction o){
  540. // Nothing to do...
  541. }
  542. /**
  543. * Ensures the specific preconditions of the said instruction.
  544. */
  545. public void visitBREAKPOINT(Instruction o){
  546. throw new AssertionViolatedException("In this JustIce verification pass there should not occur an illegal instruction such as BREAKPOINT.");
  547. }
  548. /**
  549. * Ensures the specific preconditions of the said instruction.
  550. */
  551. public void visitCALOAD(Instruction o){
  552. Type arrayref = stack().peek(1);
  553. Type index = stack().peek(0);
  554. indexOfInt(o, index);
  555. arrayrefOfArrayType(o, arrayref);
  556. }
  557. /**
  558. * Ensures the specific preconditions of the said instruction.
  559. */
  560. public void visitCASTORE(Instruction o){
  561. Type arrayref = stack().peek(2);
  562. Type index = stack().peek(1);
  563. Type value = stack().peek(0);
  564. indexOfInt(o, index);
  565. valueOfInt(o, value);
  566. if (arrayrefOfArrayType(o, arrayref)){
  567. if (! ((ArrayType) arrayref).getElementType().equals(Type.CHAR) ){
  568. constraintViolated(o, "The 'arrayref' does not refer to an array with elements of type char but to an array of type "+((ArrayType) arrayref).getElementType()+".");
  569. }
  570. }
  571. }
  572. /**
  573. * Ensures the specific preconditions of the said instruction.
  574. */
  575. public void visitCHECKCAST(Instruction o){
  576. // The objectref must be of type reference.
  577. Type objectref = stack().peek(0);
  578. if (!(objectref instanceof ReferenceType)){
  579. constraintViolated(o, "The 'objectref' is not of a ReferenceType but of type "+objectref+".");
  580. }
  581. else{
  582. referenceTypeIsInitialized(o, (ReferenceType) objectref);
  583. }
  584. // The unsigned indexbyte1 and indexbyte2 are used to construct an index into the runtime constant pool of the
  585. // current class (§3.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The runtime constant
  586. // pool item at the index must be a symbolic reference to a class, array, or interface type.
  587. Constant c = cpg.getConstant(o.getIndex());
  588. if (! (c instanceof ConstantClass)){
  589. constraintViolated(o, "The Constant at 'index' is not a ConstantClass, but '"+c+"'.");
  590. }
  591. }
  592. public void visitD2F(Instruction o) { checkTop(o,Type.DOUBLE); }
  593. public void visitD2I(Instruction o) { checkTop(o,Type.DOUBLE); }
  594. public void visitD2L(Instruction o) { checkTop(o,Type.DOUBLE); }
  595. public void visitDADD(Instruction o){
  596. checkTop(o,Type.DOUBLE);
  597. if (stack().peek(1) != Type.DOUBLE){
  598. constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
  599. }
  600. }
  601. public void visitDALOAD(Instruction o){
  602. indexOfInt(o, stack().peek());
  603. if (stack().peek(1) == Type.NULL){
  604. return;
  605. }
  606. if (! (stack().peek(1) instanceof ArrayType)){
  607. constraintViolated(o, "Stack next-to-top must be of type double[] but is '"+stack().peek(1)+"'.");
  608. }
  609. Type t = ((ArrayType) (stack().peek(1))).getBasicType();
  610. if (t != Type.DOUBLE){
  611. constraintViolated(o, "Stack next-to-top must be of type double[] but is '"+stack().peek(1)+"'.");
  612. }
  613. }
  614. public void visitDASTORE(Instruction o){
  615. if (stack().peek() != Type.DOUBLE){
  616. constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
  617. }
  618. indexOfInt(o, stack().peek(1));
  619. if (stack().peek(2) == Type.NULL){
  620. return;
  621. }
  622. if (! (stack().peek(2) instanceof ArrayType)){
  623. constraintViolated(o, "Stack next-to-next-to-top must be of type double[] but is '"+stack().peek(2)+"'.");
  624. }
  625. Type t = ((ArrayType) (stack().peek(2))).getBasicType();
  626. if (t != Type.DOUBLE){
  627. constraintViolated(o, "Stack next-to-next-to-top must be of type double[] but is '"+stack().peek(2)+"'.");
  628. }
  629. }
  630. public void visitDCMPG(Instruction o){
  631. checkTop(o,Type.DOUBLE);
  632. if (stack().peek(1) != Type.DOUBLE){
  633. constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
  634. }
  635. }
  636. public void visitDCMPL(Instruction o){
  637. checkTop(o,Type.DOUBLE);
  638. if (stack().peek(1) != Type.DOUBLE){
  639. constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
  640. }
  641. }
  642. public void visitDCONST(Instruction o){
  643. // There's nothing to be done here.
  644. }
  645. public void visitDDIV(Instruction o){
  646. checkTop(o,Type.DOUBLE);
  647. if (stack().peek(1) != Type.DOUBLE){
  648. constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
  649. }
  650. }
  651. public void visitDLOAD(Instruction o){
  652. //visitLoadInstruction(LoadInstruction) is called before.
  653. // Nothing else needs to be done here.
  654. }
  655. public void visitDMUL(Instruction o){
  656. checkTop(o,Type.DOUBLE);
  657. if (stack().peek(1) != Type.DOUBLE){
  658. constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
  659. }
  660. }
  661. public void visitDNEG(Instruction o){
  662. checkTop(o,Type.DOUBLE);
  663. }
  664. public void visitDREM(Instruction o){
  665. checkTop(o,Type.DOUBLE);
  666. if (stack().peek(1) != Type.DOUBLE){
  667. constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
  668. }
  669. }
  670. private void checkTop(Instruction o,Type t) {
  671. if (stack().peek()!=t)
  672. constraintViolated(o, "The value at the stack top is not of type '"+t+"', but of type '"+stack().peek()+"'.");
  673. }
  674. /**
  675. * Ensures the specific preconditions of the said instruction.
  676. */
  677. public void visitDRETURN(Instruction o){
  678. checkTop(o,Type.DOUBLE);
  679. }
  680. /**
  681. * Ensures the specific preconditions of the said instruction.
  682. */
  683. public void visitDSTORE(Instruction o){
  684. //visitStoreInstruction(StoreInstruction) is called before.
  685. // Nothing else needs to be done here.
  686. }
  687. /**
  688. * Ensures the specific preconditions of the said instruction.
  689. */
  690. public void visitDSUB(Instruction o){
  691. if (stack().peek() != Type.DOUBLE){
  692. constraintViolated(o, "The value at the stack top is not of type 'double', but of type '"+stack().peek()+"'.");
  693. }
  694. if (stack().peek(1) != Type.DOUBLE){
  695. constraintViolated(o, "The value at the stack next-to-top is not of type 'double', but of type '"+stack().peek(1)+"'.");
  696. }
  697. }
  698. /**
  699. * Ensures the specific preconditions of the said instruction.
  700. */
  701. public void visitDUP(Instruction o){
  702. if (stack().peek().getSize() != 1){
  703. constraintViolated(o, "Won't DUP type on stack top '"+stack().peek()+"' because it must occupy exactly one slot, not '"+stack().peek().getSize()+"'.");
  704. }
  705. }
  706. /**
  707. * Ensures the specific preconditions of the said instruction.
  708. */
  709. public void visitDUP_X1(Instruction o){
  710. if (stack().peek().getSize() != 1){
  711. constraintViolated(o, "Type on stack top '"+stack().peek()+"' should occupy exactly one slot, not '"+stack().peek().getSize()+"'.");
  712. }
  713. if (stack().peek(1).getSize() != 1){
  714. constraintViolated(o, "Type on stack next-to-top '"+stack().peek(1)+"' should occupy exactly one slot, not '"+stack().peek(1).getSize()+"'.");
  715. }
  716. }
  717. /**
  718. * Ensures the specific preconditions of the said instruction.
  719. */
  720. public void visitDUP_X2(Instruction o){
  721. if (stack().peek().getSize() != 1){
  722. constraintViolated(o, "Stack top type must be of size 1, but is '"+stack().peek()+"' of size '"+stack().peek().getSize()+"'.");
  723. }
  724. if (stack().peek(1).getSize() == 2){
  725. return; // Form 2, okay.
  726. }
  727. else{ //stack().peek(1).getSize == 1.
  728. if (stack().peek(2).getSize() != 1){
  729. constraintViolated(o, "If stack top's size is 1 and stack next-to-top's size is 1, stack next-to-next-to-top's size must also be 1, but is: '"+stack().peek(2)+"' of size '"+stack().peek(2).getSize()+"'.");
  730. }
  731. }
  732. }
  733. /**
  734. * Ensures the specific preconditions of the said instruction.
  735. */
  736. public void visitDUP2(Instruction o){
  737. if (stack().peek().getSize() == 2){
  738. return; // Form 2, okay.
  739. }
  740. else{ //stack().peek().getSize() == 1.
  741. if (stack().peek(1).getSize() != 1){
  742. constraintViolated(o, "If stack top's size is 1, then stack next-to-top's size must also be 1. But it is '"+stack().peek(1)+"' of size '"+stack().peek(1).getSize()+"'.");
  743. }
  744. }
  745. }
  746. /**
  747. * Ensures the specific preconditions of the said instruction.
  748. */
  749. public void visitDUP2_X1(Instruction o){
  750. if (stack().peek().getSize() == 2){
  751. if (stack().peek(1).getSize() != 1){
  752. constraintViolated(o, "If stack top's size is 2, then stack next-to-top's size must be 1. But it is '"+stack().peek(1)+"' of size '"+stack().peek(1).getSize()+"'.");
  753. }
  754. else{
  755. return; // Form 2
  756. }
  757. }
  758. else{ // stack top is of size 1
  759. if ( stack().peek(1).getSize() != 1 ){
  760. constraintViolated(o, "If stack top's size is 1, then stack next-to-top's size must also be 1. But it is '"+stack().peek(1)+"' of size '"+stack().peek(1).getSize()+"'.");
  761. }
  762. if ( stack().peek(2).getSize() != 1 ){
  763. constraintViolated(o, "If stack top's size is 1, then stack next-to-next-to-top's size must also be 1. But it is '"+stack().peek(2)+"' of size '"+stack().peek(2).getSize()+"'.");
  764. }
  765. }
  766. }
  767. /**
  768. * Ensures the specific preconditions of the said instruction.
  769. */
  770. public void visitDUP2_X2(Instruction o){
  771. if (stack().peek(0).getSize() == 2){
  772. if (stack().peek(1).getSize() == 2){
  773. return; // Form 4
  774. }
  775. else{// stack top size is 2, next-to-top's size is 1
  776. if ( stack().peek(2).getSize() != 1 ){
  777. constraintViolated(o, "If stack top's size is 2 and stack-next-to-top's size is 1, then stack next-to-next-to-top's size must also be 1. But it is '"+stack().peek(2)+"' of size '"+stack().peek(2).getSize()+"'.");
  778. }
  779. else{
  780. return; // Form 2
  781. }
  782. }
  783. }
  784. else{// stack top is of size 1
  785. if (stack().peek(1).getSize() == 1){
  786. if ( stack().peek(2).getSize() == 2 ){
  787. return; // Form 3
  788. }
  789. else{
  790. if ( stack().peek(3).getSize() == 1){
  791. return; // Form 1
  792. }
  793. }
  794. }
  795. }
  796. constraintViolated(o, "The operand sizes on the stack do not match any of the four forms of usage of this instruction.");
  797. }
  798. /**
  799. * Ensures the specific preconditions of the said instruction.
  800. */
  801. public void visitF2D(Instruction o){
  802. if (stack().peek() != Type.FLOAT){
  803. constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
  804. }
  805. }
  806. public void visitF2I(Instruction o){
  807. checkTop(o,Type.FLOAT);
  808. }
  809. public void visitF2L(Instruction o){
  810. checkTop(o,Type.FLOAT);
  811. }
  812. /**
  813. * Ensures the specific preconditions of the said instruction.
  814. */
  815. public void visitFADD(Instruction o){
  816. checkTop(o,Type.FLOAT);
  817. if (stack().peek(1) != Type.FLOAT){
  818. constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
  819. }
  820. }
  821. /**
  822. * Ensures the specific preconditions of the said instruction.
  823. */
  824. public void visitFALOAD(Instruction o){
  825. indexOfInt(o, stack().peek());
  826. if (stack().peek(1) == Type.NULL){
  827. return;
  828. }
  829. if (! (stack().peek(1) instanceof ArrayType)){
  830. constraintViolated(o, "Stack next-to-top must be of type float[] but is '"+stack().peek(1)+"'.");
  831. }
  832. Type t = ((ArrayType) (stack().peek(1))).getBasicType();
  833. if (t != Type.FLOAT){
  834. constraintViolated(o, "Stack next-to-top must be of type float[] but is '"+stack().peek(1)+"'.");
  835. }
  836. }
  837. /**
  838. * Ensures the specific preconditions of the said instruction.
  839. */
  840. public void visitFASTORE(Instruction o){
  841. checkTop(o,Type.FLOAT);
  842. indexOfInt(o, stack().peek(1));
  843. if (stack().peek(2) == Type.NULL){
  844. return;
  845. }
  846. if (! (stack().peek(2) instanceof ArrayType)){
  847. constraintViolated(o, "Stack next-to-next-to-top must be of type float[] but is '"+stack().peek(2)+"'.");
  848. }
  849. Type t = ((ArrayType) (stack().peek(2))).getBasicType();
  850. if (t != Type.FLOAT){
  851. constraintViolated(o, "Stack next-to-next-to-top must be of type float[] but is '"+stack().peek(2)+"'.");
  852. }
  853. }
  854. /**
  855. * Ensures the specific preconditions of the said instruction.
  856. */
  857. public void visitFCMPG(Instruction o){
  858. checkTop(o,Type.FLOAT);
  859. if (stack().peek(1) != Type.FLOAT){
  860. constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
  861. }
  862. }
  863. /**
  864. * Ensures the specific preconditions of the said instruction.
  865. */
  866. public void visitFCMPL(Instruction o){
  867. checkTop(o,Type.FLOAT);
  868. if (stack().peek(1) != Type.FLOAT){
  869. constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
  870. }
  871. }
  872. /**
  873. * Ensures the specific preconditions of the said instruction.
  874. */
  875. public void visitFCONST(Instruction o){
  876. // nothing to do here.
  877. }
  878. /**
  879. * Ensures the specific preconditions of the said instruction.
  880. */
  881. public void visitFDIV(Instruction o){
  882. checkTop(o,Type.FLOAT);
  883. if (stack().peek(1) != Type.FLOAT){
  884. constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
  885. }
  886. }
  887. /**
  888. * Ensures the specific preconditions of the said instruction.
  889. */
  890. public void visitFLOAD(Instruction o){
  891. //visitLoadInstruction(LoadInstruction) is called before.
  892. // Nothing else needs to be done here.
  893. }
  894. /**
  895. * Ensures the specific preconditions of the said instruction.
  896. */
  897. public void visitFMUL(Instruction o){
  898. checkTop(o,Type.FLOAT);
  899. if (stack().peek(1) != Type.FLOAT){
  900. constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
  901. }
  902. }
  903. /**
  904. * Ensures the specific preconditions of the said instruction.
  905. */
  906. public void visitFNEG(Instruction o){
  907. checkTop(o,Type.FLOAT);
  908. }
  909. /**
  910. * Ensures the specific preconditions of the said instruction.
  911. */
  912. public void visitFREM(Instruction o){
  913. checkTop(o,Type.FLOAT);
  914. if (stack().peek(1) != Type.FLOAT){
  915. constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
  916. }
  917. }
  918. /**
  919. * Ensures the specific preconditions of the said instruction.
  920. */
  921. public void visitFRETURN(Instruction o){
  922. checkTop(o,Type.FLOAT);
  923. }
  924. /**
  925. * Ensures the specific preconditions of the said instruction.
  926. */
  927. public void visitFSTORE(Instruction o){
  928. //visitStoreInstruction(StoreInstruction) is called before.
  929. // Nothing else needs to be done here.
  930. }
  931. /**
  932. * Ensures the specific preconditions of the said instruction.
  933. */
  934. public void visitFSUB(Instruction o){
  935. if (stack().peek() != Type.FLOAT){
  936. constraintViolated(o, "The value at the stack top is not of type 'float', but of type '"+stack().peek()+"'.");
  937. }
  938. if (stack().peek(1) != Type.FLOAT){
  939. constraintViolated(o, "The value at the stack next-to-top is not of type 'float', but of type '"+stack().peek(1)+"'.");
  940. }
  941. }
  942. /**
  943. * Ensures the specific preconditions of the said instruction.
  944. */
  945. public void visitGETFIELD(FieldInstruction o){
  946. Type objectref = stack().peek();
  947. if (! ( (objectref instanceof ObjectType) || (objectref == Type.NULL) ) ){
  948. constraintViolated(o, "Stack top should be an object reference that's not an array reference, but is '"+objectref+"'.");
  949. }
  950. String field_name = o.getFieldName(cpg);
  951. JavaClass jc = Repository.lookupClass(o.getClassType(cpg).getClassName());
  952. Field[] fields = jc.getFields();
  953. Field f = null;
  954. for (int i=0; i<fields.length; i++){
  955. if (fields[i].getName().equals(field_name)){
  956. f = fields[i];
  957. break;
  958. }
  959. }
  960. if (f == null){
  961. throw new AssertionViolatedException("Field not found?!?");
  962. }
  963. if (f.isProtected()){
  964. ObjectType classtype = o.getClassType(cpg);
  965. ObjectType curr = new ObjectType(mg.getClassName());
  966. if ( classtype.equals(curr) ||
  967. curr.subclassOf(classtype) ){
  968. Type t = stack().peek();
  969. if (t == Type.NULL){
  970. return;
  971. }
  972. if (! (t instanceof ObjectType) ){
  973. constraintViolated(o, "The 'objectref' must refer to an object that's not an array. Found instead: '"+t+"'.");
  974. }
  975. ObjectType objreftype = (ObjectType) t;
  976. if (! ( objreftype.equals(curr) ||
  977. objreftype.subclassOf(curr) ) ){
  978. //TODO: One day move to Staerk-et-al's "Set of object types" instead of "wider" object types
  979. // created during the verification.
  980. // "Wider" object types don't allow us to check for things like that below.
  981. //constraintViolated(o, "The referenced field has the ACC_PROTECTED modifier, and it's a member of the current class or a superclass of the current class. However, the referenced object type '"+stack().peek()+"' is not the current class or a subclass of the current class.");
  982. }
  983. }
  984. }
  985. // TODO: Could go into Pass 3a.
  986. if (f.isStatic()){
  987. constraintViolated(o, "Referenced field '"+f+"' is static which it shouldn't be.");
  988. }
  989. }
  990. /**
  991. * Ensures the specific preconditions of the said instruction.
  992. */
  993. public void visitGETSTATIC(FieldInstruction o){
  994. // Field must be static: see Pass 3a.
  995. }
  996. /**
  997. * Ensures the specific preconditions of the said instruction.
  998. */
  999. public void visitGOTO(Instruction o){
  1000. // nothing to do here.
  1001. }
  1002. /**
  1003. * Ensures the specific preconditions of the said instruction.
  1004. */
  1005. public void visitGOTO_W(Instruction o){
  1006. // nothing to do here.
  1007. }
  1008. /**
  1009. * Ensures the specific preconditions of the said instruction.
  1010. */
  1011. public void visitI2B(Instruction o){
  1012. if (stack().peek() != Type.INT){
  1013. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1014. }
  1015. }
  1016. /**
  1017. * Ensures the specific preconditions of the said instruction.
  1018. */
  1019. public void visitI2C(Instruction o){
  1020. if (stack().peek() != Type.INT){
  1021. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1022. }
  1023. }
  1024. /**
  1025. * Ensures the specific preconditions of the said instruction.
  1026. */
  1027. public void visitI2D(Instruction o){
  1028. if (stack().peek() != Type.INT){
  1029. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1030. }
  1031. }
  1032. public void visitI2F(Instruction o) { checkTop(o,Type.INT); }
  1033. public void visitI2L(Instruction o) { checkTop(o,Type.INT); }
  1034. public void visitI2S(Instruction o) { checkTop(o,Type.INT); }
  1035. public void visitIADD(Instruction o){
  1036. checkTop(o,Type.INT);
  1037. if (stack().peek(1) != Type.INT){
  1038. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1039. }
  1040. }
  1041. /**
  1042. * Ensures the specific preconditions of the said instruction.
  1043. */
  1044. public void visitIALOAD(Instruction o){
  1045. indexOfInt(o, stack().peek());
  1046. if (stack().peek(1) == Type.NULL){
  1047. return;
  1048. }
  1049. if (! (stack().peek(1) instanceof ArrayType)){
  1050. constraintViolated(o, "Stack next-to-top must be of type int[] but is '"+stack().peek(1)+"'.");
  1051. }
  1052. Type t = ((ArrayType) (stack().peek(1))).getBasicType();
  1053. if (t != Type.INT){
  1054. constraintViolated(o, "Stack next-to-top must be of type int[] but is '"+stack().peek(1)+"'.");
  1055. }
  1056. }
  1057. /**
  1058. * Ensures the specific preconditions of the said instruction.
  1059. */
  1060. public void visitIAND(Instruction o){
  1061. if (stack().peek() != Type.INT){
  1062. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1063. }
  1064. if (stack().peek(1) != Type.INT){
  1065. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1066. }
  1067. }
  1068. /**
  1069. * Ensures the specific preconditions of the said instruction.
  1070. */
  1071. public void visitIASTORE(Instruction o){
  1072. if (stack().peek() != Type.INT){
  1073. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1074. }
  1075. indexOfInt(o, stack().peek(1));
  1076. if (stack().peek(2) == Type.NULL){
  1077. return;
  1078. }
  1079. if (! (stack().peek(2) instanceof ArrayType)){
  1080. constraintViolated(o, "Stack next-to-next-to-top must be of type int[] but is '"+stack().peek(2)+"'.");
  1081. }
  1082. Type t = ((ArrayType) (stack().peek(2))).getBasicType();
  1083. if (t != Type.INT){
  1084. constraintViolated(o, "Stack next-to-next-to-top must be of type int[] but is '"+stack().peek(2)+"'.");
  1085. }
  1086. }
  1087. /**
  1088. * Ensures the specific preconditions of the said instruction.
  1089. */
  1090. public void visitICONST(Instruction o){
  1091. //nothing to do here.
  1092. }
  1093. /**
  1094. * Ensures the specific preconditions of the said instruction.
  1095. */
  1096. public void visitIDIV(Instruction o){
  1097. checkTop(o,Type.INT);
  1098. if (stack().peek(1) != Type.INT){
  1099. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1100. }
  1101. }
  1102. /**
  1103. * Ensures the specific preconditions of the said instruction.
  1104. */
  1105. public void visitIF_ACMPEQ(Instruction o){
  1106. if (!(stack().peek() instanceof ReferenceType)){
  1107. constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '"+stack().peek()+"'.");
  1108. }
  1109. referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
  1110. if (!(stack().peek(1) instanceof ReferenceType)){
  1111. constraintViolated(o, "The value at the stack next-to-top is not of a ReferenceType, but of type '"+stack().peek(1)+"'.");
  1112. }
  1113. referenceTypeIsInitialized(o, (ReferenceType) (stack().peek(1)) );
  1114. }
  1115. /**
  1116. * Ensures the specific preconditions of the said instruction.
  1117. */
  1118. public void visitIF_ACMPNE(Instruction o){
  1119. if (!(stack().peek() instanceof ReferenceType)){
  1120. constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '"+stack().peek()+"'.");
  1121. referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
  1122. }
  1123. if (!(stack().peek(1) instanceof ReferenceType)){
  1124. constraintViolated(o, "The value at the stack next-to-top is not of a ReferenceType, but of type '"+stack().peek(1)+"'.");
  1125. referenceTypeIsInitialized(o, (ReferenceType) (stack().peek(1)) );
  1126. }
  1127. }
  1128. /**
  1129. * Ensures the specific preconditions of the said instruction.
  1130. */
  1131. public void visitIF_ICMPEQ(Instruction o){
  1132. if (stack().peek() != Type.INT){
  1133. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1134. }
  1135. if (stack().peek(1) != Type.INT){
  1136. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1137. }
  1138. }
  1139. /**
  1140. * Ensures the specific preconditions of the said instruction.
  1141. */
  1142. public void visitIF_ICMPGE(Instruction o){
  1143. if (stack().peek() != Type.INT){
  1144. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1145. }
  1146. if (stack().peek(1) != Type.INT){
  1147. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1148. }
  1149. }
  1150. /**
  1151. * Ensures the specific preconditions of the said instruction.
  1152. */
  1153. public void visitIF_ICMPGT(Instruction o){
  1154. if (stack().peek() != Type.INT){
  1155. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1156. }
  1157. if (stack().peek(1) != Type.INT){
  1158. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1159. }
  1160. }
  1161. /**
  1162. * Ensures the specific preconditions of the said instruction.
  1163. */
  1164. public void visitIF_ICMPLE(Instruction o){
  1165. if (stack().peek() != Type.INT){
  1166. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1167. }
  1168. if (stack().peek(1) != Type.INT){
  1169. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1170. }
  1171. }
  1172. /**
  1173. * Ensures the specific preconditions of the said instruction.
  1174. */
  1175. public void visitIF_ICMPLT(Instruction o){
  1176. if (stack().peek() != Type.INT){
  1177. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1178. }
  1179. if (stack().peek(1) != Type.INT){
  1180. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1181. }
  1182. }
  1183. /**
  1184. * Ensures the specific preconditions of the said instruction.
  1185. */
  1186. public void visitIF_ICMPNE(Instruction o){
  1187. if (stack().peek() != Type.INT){
  1188. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1189. }
  1190. if (stack().peek(1) != Type.INT){
  1191. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1192. }
  1193. }
  1194. /**
  1195. * Ensures the specific preconditions of the said instruction.
  1196. */
  1197. public void visitIFEQ(Instruction o){
  1198. if (stack().peek() != Type.INT){
  1199. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1200. }
  1201. }
  1202. /**
  1203. * Ensures the specific preconditions of the said instruction.
  1204. */
  1205. public void visitIFGE(Instruction o){
  1206. if (stack().peek() != Type.INT){
  1207. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1208. }
  1209. }
  1210. /**
  1211. * Ensures the specific preconditions of the said instruction.
  1212. */
  1213. public void visitIFGT(Instruction o){
  1214. if (stack().peek() != Type.INT){
  1215. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1216. }
  1217. }
  1218. /**
  1219. * Ensures the specific preconditions of the said instruction.
  1220. */
  1221. public void visitIFLE(Instruction o){
  1222. if (stack().peek() != Type.INT){
  1223. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1224. }
  1225. }
  1226. /**
  1227. * Ensures the specific preconditions of the said instruction.
  1228. */
  1229. public void visitIFLT(Instruction o){
  1230. if (stack().peek() != Type.INT){
  1231. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1232. }
  1233. }
  1234. /**
  1235. * Ensures the specific preconditions of the said instruction.
  1236. */
  1237. public void visitIFNE(Instruction o){
  1238. if (stack().peek() != Type.INT){
  1239. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1240. }
  1241. }
  1242. /**
  1243. * Ensures the specific preconditions of the said instruction.
  1244. */
  1245. public void visitIFNONNULL(Instruction o){
  1246. if (!(stack().peek() instanceof ReferenceType)){
  1247. constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '"+stack().peek()+"'.");
  1248. }
  1249. referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
  1250. }
  1251. /**
  1252. * Ensures the specific preconditions of the said instruction.
  1253. */
  1254. public void visitIFNULL(Instruction o){
  1255. if (!(stack().peek() instanceof ReferenceType)){
  1256. constraintViolated(o, "The value at the stack top is not of a ReferenceType, but of type '"+stack().peek()+"'.");
  1257. }
  1258. referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
  1259. }
  1260. /**
  1261. * Ensures the specific preconditions of the said instruction.
  1262. */
  1263. public void visitIINC(IINC o){
  1264. // Mhhh. In BCEL, at this time "IINC" is not a LocalVariableInstruction.
  1265. if (locals().maxLocals() <= (o.getType(cpg).getSize()==1? o.getIndex() : o.getIndex()+1) ){
  1266. constraintViolated(o, "The 'index' is not a valid index into the local variable array.");
  1267. }
  1268. indexOfInt(o, locals().get(o.getIndex()));
  1269. }
  1270. /**
  1271. * Ensures the specific preconditions of the said instruction.
  1272. */
  1273. public void visitILOAD(Instruction o){
  1274. // All done by visitLocalVariableInstruction(), visitLoadInstruction()
  1275. }
  1276. /**
  1277. * Ensures the specific preconditions of the said instruction.
  1278. */
  1279. public void visitIMPDEP1(Instruction o){
  1280. throw new AssertionViolatedException("In this JustIce verification pass there should not occur an illegal instruction such as IMPDEP1.");
  1281. }
  1282. /**
  1283. * Ensures the specific preconditions of the said instruction.
  1284. */
  1285. public void visitIMPDEP2(Instruction o){
  1286. throw new AssertionViolatedException("In this JustIce verification pass there should not occur an illegal instruction such as IMPDEP2.");
  1287. }
  1288. /**
  1289. * Ensures the specific preconditions of the said instruction.
  1290. */
  1291. public void visitIMUL(Instruction o){
  1292. if (stack().peek() != Type.INT){
  1293. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1294. }
  1295. if (stack().peek(1) != Type.INT){
  1296. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1297. }
  1298. }
  1299. /**
  1300. * Ensures the specific preconditions of the said instruction.
  1301. */
  1302. public void visitINEG(Instruction o){
  1303. if (stack().peek() != Type.INT){
  1304. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1305. }
  1306. }
  1307. /**
  1308. * Ensures the specific preconditions of the said instruction.
  1309. */
  1310. public void visitINSTANCEOF(Instruction o){
  1311. // The objectref must be of type reference.
  1312. Type objectref = stack().peek(0);
  1313. if (!(objectref instanceof ReferenceType)){
  1314. constraintViolated(o, "The 'objectref' is not of a ReferenceType but of type "+objectref+".");
  1315. }
  1316. else{
  1317. referenceTypeIsInitialized(o, (ReferenceType) objectref);
  1318. }
  1319. // The unsigned indexbyte1 and indexbyte2 are used to construct an index into the runtime constant pool of the
  1320. // current class (§3.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The runtime constant
  1321. // pool item at the index must be a symbolic reference to a class, array, or interface type.
  1322. Constant c = cpg.getConstant(o.getIndex());
  1323. if (! (c instanceof ConstantClass)){
  1324. constraintViolated(o, "The Constant at 'index' is not a ConstantClass, but '"+c+"'.");
  1325. }
  1326. }
  1327. /**
  1328. * Ensures the specific preconditions of the said instruction.
  1329. */
  1330. public void visitINVOKEINTERFACE(INVOKEINTERFACE o){
  1331. // Method is not native, otherwise pass 3 would not happen.
  1332. int count = o.getCount();
  1333. if (count == 0){
  1334. constraintViolated(o, "The 'count' argument must not be 0.");
  1335. }
  1336. // It is a ConstantInterfaceMethodref, Pass 3a made it sure.
  1337. // TODO: Do we want to do anything with it?
  1338. //ConstantInterfaceMethodref cimr = (ConstantInterfaceMethodref) (cpg.getConstant(o.getIndex()));
  1339. // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
  1340. Type t = o.getType(cpg);
  1341. if (t instanceof ObjectType){
  1342. String name = ((ObjectType)t).getClassName();
  1343. Verifier v = VerifierFactory.getVerifier( name );
  1344. VerificationResult vr = v.doPass2();
  1345. if (vr.getStatus() != VerificationResult.VERIFIED_OK){
  1346. constraintViolated((Instruction) o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
  1347. }
  1348. }
  1349. Type[] argtypes = o.getArgumentTypes(cpg);
  1350. int nargs = argtypes.length;
  1351. for (int i=nargs-1; i>=0; i--){
  1352. Type fromStack = stack().peek( (nargs-1) - i ); // 0 to nargs-1
  1353. Type fromDesc = argtypes[i];
  1354. if (fromDesc == Type.BOOLEAN ||
  1355. fromDesc == Type.BYTE ||
  1356. fromDesc == Type.CHAR ||
  1357. fromDesc == Type.SHORT){
  1358. fromDesc = Type.INT;
  1359. }
  1360. if (! fromStack.equals(fromDesc)){
  1361. if (fromStack instanceof ReferenceType && fromDesc instanceof ReferenceType){
  1362. //ReferenceType rFromStack = (ReferenceType) fromStack;
  1363. //ReferenceType rFromDesc = (ReferenceType) fromDesc;
  1364. // TODO: This can only be checked when using Staerk-et-al's "set of object types"
  1365. // instead of a "wider cast object type" created during verification.
  1366. //if ( ! rFromStack.isAssignmentCompatibleWith(rFromDesc) ){
  1367. // constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack (which is not assignment compatible).");
  1368. //}
  1369. }
  1370. else{
  1371. constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack.");
  1372. }
  1373. }
  1374. }
  1375. Type objref = stack().peek(nargs);
  1376. if (objref == Type.NULL){
  1377. return;
  1378. }
  1379. if (! (objref instanceof ReferenceType) ){
  1380. constraintViolated(o, "Expecting a reference type as 'objectref' on the stack, not a '"+objref+"'.");
  1381. }
  1382. referenceTypeIsInitialized(o, (ReferenceType) objref);
  1383. if (!(objref instanceof ObjectType)){
  1384. if (!(objref instanceof ArrayType)){
  1385. constraintViolated(o, "Expecting an ObjectType as 'objectref' on the stack, not a '"+objref+"'."); // could be a ReturnaddressType
  1386. }
  1387. else{
  1388. objref = GENERIC_ARRAY;
  1389. }
  1390. }
  1391. // String objref_classname = ((ObjectType) objref).getClassName();
  1392. // String theInterface = o.getClassName(cpg);
  1393. // TODO: This can only be checked if we're using Staerk-et-al's "set of object types"
  1394. // instead of "wider cast object types" generated during verification.
  1395. //if ( ! Repository.implementationOf(objref_classname, theInterface) ){
  1396. // constraintViolated(o, "The 'objref' item '"+objref+"' does not implement '"+theInterface+"' as expected.");
  1397. //}
  1398. int counted_count = 1; // 1 for the objectref
  1399. for (int i=0; i<nargs; i++){
  1400. counted_count += argtypes[i].getSize();
  1401. }
  1402. if (count != counted_count){
  1403. constraintViolated(o, "The 'count' argument should probably read '"+counted_count+"' but is '"+count+"'.");
  1404. }
  1405. }
  1406. /**
  1407. * Ensures the specific preconditions of the said instruction.
  1408. */
  1409. public void visitINVOKESPECIAL(InvokeInstruction o){
  1410. // Don't init an object twice.
  1411. if ( (o.getMethodName(cpg).equals(Constants.CONSTRUCTOR_NAME)) && (!(stack().peek(o.getArgumentTypes(cpg).length) instanceof UninitializedObjectType)) ){
  1412. constraintViolated(o, "Possibly initializing object twice. A valid instruction sequence must not have an uninitialized object on the operand stack or in a local variable during a backwards branch, or in a local variable in code protected by an exception handler. Please see The Java Virtual Machine Specification, Second Edition, 4.9.4 (pages 147 and 148) for details.");
  1413. }
  1414. // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
  1415. Type t = o.getType(cpg);
  1416. if (t instanceof ObjectType){
  1417. String name = ((ObjectType)t).getClassName();
  1418. Verifier v = VerifierFactory.getVerifier( name );
  1419. VerificationResult vr = v.doPass2();
  1420. if (vr.getStatus() != VerificationResult.VERIFIED_OK){
  1421. constraintViolated((Instruction) o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
  1422. }
  1423. }
  1424. Type[] argtypes = o.getArgumentTypes(cpg);
  1425. int nargs = argtypes.length;
  1426. for (int i=nargs-1; i>=0; i--){
  1427. Type fromStack = stack().peek( (nargs-1) - i ); // 0 to nargs-1
  1428. Type fromDesc = argtypes[i];
  1429. if (fromDesc == Type.BOOLEAN ||
  1430. fromDesc == Type.BYTE ||
  1431. fromDesc == Type.CHAR ||
  1432. fromDesc == Type.SHORT){
  1433. fromDesc = Type.INT;
  1434. }
  1435. if (! fromStack.equals(fromDesc)){
  1436. if (fromStack instanceof ReferenceType && fromDesc instanceof ReferenceType){
  1437. ReferenceType rFromStack = (ReferenceType) fromStack;
  1438. ReferenceType rFromDesc = (ReferenceType) fromDesc;
  1439. // TODO: This can only be checked using Staerk-et-al's "set of object types", not
  1440. // using a "wider cast object type".
  1441. if ( ! rFromStack.isAssignmentCompatibleWith(rFromDesc) ){
  1442. constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack (which is not assignment compatible).");
  1443. }
  1444. }
  1445. else{
  1446. constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack.");
  1447. }
  1448. }
  1449. }
  1450. Type objref = stack().peek(nargs);
  1451. if (objref == Type.NULL){
  1452. return;
  1453. }
  1454. if (! (objref instanceof ReferenceType) ){
  1455. constraintViolated(o, "Expecting a reference type as 'objectref' on the stack, not a '"+objref+"'.");
  1456. }
  1457. String objref_classname = null;
  1458. if ( !(o.getMethodName(cpg).equals(Constants.CONSTRUCTOR_NAME))){
  1459. referenceTypeIsInitialized(o, (ReferenceType) objref);
  1460. if (!(objref instanceof ObjectType)){
  1461. if (!(objref instanceof ArrayType)){
  1462. constraintViolated(o, "Expecting an ObjectType as 'objectref' on the stack, not a '"+objref+"'."); // could be a ReturnaddressType
  1463. }
  1464. else{
  1465. objref = GENERIC_ARRAY;
  1466. }
  1467. }
  1468. objref_classname = ((ObjectType) objref).getClassName();
  1469. }
  1470. else{
  1471. if (!(objref instanceof UninitializedObjectType)){
  1472. constraintViolated(o, "Expecting an UninitializedObjectType as 'objectref' on the stack, not a '"+objref+"'. Otherwise, you couldn't invoke a method since an array has no methods (not to speak of a return address).");
  1473. }
  1474. objref_classname = ((UninitializedObjectType) objref).getInitialized().getClassName();
  1475. }
  1476. String theClass = o.getClassName(cpg);
  1477. if ( ! Repository.instanceOf(objref_classname, theClass) ){
  1478. constraintViolated(o, "The 'objref' item '"+objref+"' does not implement '"+theClass+"' as expected.");
  1479. }
  1480. }
  1481. /**
  1482. * Ensures the specific preconditions of the said instruction.
  1483. */
  1484. public void visitINVOKESTATIC(InvokeInstruction o){
  1485. // Method is not native, otherwise pass 3 would not happen.
  1486. Type t = o.getType(cpg);
  1487. if (t instanceof ObjectType){
  1488. String name = ((ObjectType)t).getClassName();
  1489. Verifier v = VerifierFactory.getVerifier( name );
  1490. VerificationResult vr = v.doPass2();
  1491. if (vr.getStatus() != VerificationResult.VERIFIED_OK){
  1492. constraintViolated((Instruction) o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
  1493. }
  1494. }
  1495. Type[] argtypes = o.getArgumentTypes(cpg);
  1496. int nargs = argtypes.length;
  1497. for (int i=nargs-1; i>=0; i--){
  1498. Type fromStack = stack().peek( (nargs-1) - i ); // 0 to nargs-1
  1499. Type fromDesc = argtypes[i];
  1500. if (fromDesc == Type.BOOLEAN ||
  1501. fromDesc == Type.BYTE ||
  1502. fromDesc == Type.CHAR ||
  1503. fromDesc == Type.SHORT){
  1504. fromDesc = Type.INT;
  1505. }
  1506. if (! fromStack.equals(fromDesc)){
  1507. if (fromStack instanceof ReferenceType && fromDesc instanceof ReferenceType){
  1508. ReferenceType rFromStack = (ReferenceType) fromStack;
  1509. ReferenceType rFromDesc = (ReferenceType) fromDesc;
  1510. // TODO: This check can possibly only be done using Staerk-et-al's "set of object types"
  1511. // instead of a "wider cast object type" created during verification.
  1512. if ( ! rFromStack.isAssignmentCompatibleWith(rFromDesc) ){
  1513. constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack (which is not assignment compatible).");
  1514. }
  1515. }
  1516. else{
  1517. constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack.");
  1518. }
  1519. }
  1520. }
  1521. }
  1522. /**
  1523. * Ensures the specific preconditions of the said instruction.
  1524. */
  1525. public void visitINVOKEVIRTUAL(InvokeInstruction o){
  1526. // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
  1527. Type t = o.getType(cpg);
  1528. if (t instanceof ObjectType){
  1529. String name = ((ObjectType)t).getClassName();
  1530. Verifier v = VerifierFactory.getVerifier( name );
  1531. VerificationResult vr = v.doPass2();
  1532. if (vr.getStatus() != VerificationResult.VERIFIED_OK){
  1533. constraintViolated((Instruction) o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
  1534. }
  1535. }
  1536. Type[] argtypes = o.getArgumentTypes(cpg);
  1537. int nargs = argtypes.length;
  1538. for (int i=nargs-1; i>=0; i--){
  1539. Type fromStack = stack().peek( (nargs-1) - i ); // 0 to nargs-1
  1540. Type fromDesc = argtypes[i];
  1541. if (fromDesc == Type.BOOLEAN ||
  1542. fromDesc == Type.BYTE ||
  1543. fromDesc == Type.CHAR ||
  1544. fromDesc == Type.SHORT){
  1545. fromDesc = Type.INT;
  1546. }
  1547. if (! fromStack.equals(fromDesc)){
  1548. if (fromStack instanceof ReferenceType && fromDesc instanceof ReferenceType){
  1549. ReferenceType rFromStack = (ReferenceType) fromStack;
  1550. ReferenceType rFromDesc = (ReferenceType) fromDesc;
  1551. // TODO: This can possibly only be checked when using Staerk-et-al's "set of object types" instead
  1552. // of a single "wider cast object type" created during verification.
  1553. if ( ! rFromStack.isAssignmentCompatibleWith(rFromDesc) ){
  1554. constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack (which is not assignment compatible).");
  1555. }
  1556. }
  1557. else{
  1558. constraintViolated(o, "Expecting a '"+fromDesc+"' but found a '"+fromStack+"' on the stack.");
  1559. }
  1560. }
  1561. }
  1562. Type objref = stack().peek(nargs);
  1563. if (objref == Type.NULL){
  1564. return;
  1565. }
  1566. if (! (objref instanceof ReferenceType) ){
  1567. constraintViolated(o, "Expecting a reference type as 'objectref' on the stack, not a '"+objref+"'.");
  1568. }
  1569. referenceTypeIsInitialized(o, (ReferenceType) objref);
  1570. if (!(objref instanceof ObjectType)){
  1571. if (!(objref instanceof ArrayType)){
  1572. constraintViolated(o, "Expecting an ObjectType as 'objectref' on the stack, not a '"+objref+"'."); // could be a ReturnaddressType
  1573. }
  1574. else{
  1575. objref = GENERIC_ARRAY;
  1576. }
  1577. }
  1578. String objref_classname = ((ObjectType) objref).getClassName();
  1579. String theClass = o.getClassName(cpg);
  1580. if ( ! Repository.instanceOf(objref_classname, theClass) ){
  1581. constraintViolated(o, "The 'objref' item '"+objref+"' does not implement '"+theClass+"' as expected.");
  1582. }
  1583. }
  1584. /**
  1585. * Ensures the specific preconditions of the said instruction.
  1586. */
  1587. public void visitIOR(Instruction o){
  1588. if (stack().peek() != Type.INT){
  1589. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1590. }
  1591. if (stack().peek(1) != Type.INT){
  1592. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1593. }
  1594. }
  1595. /**
  1596. * Ensures the specific preconditions of the said instruction.
  1597. */
  1598. public void visitIREM(Instruction o){
  1599. if (stack().peek() != Type.INT){
  1600. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1601. }
  1602. if (stack().peek(1) != Type.INT){
  1603. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1604. }
  1605. }
  1606. /**
  1607. * Ensures the specific preconditions of the said instruction.
  1608. */
  1609. public void visitIRETURN(Instruction o){
  1610. if (stack().peek() != Type.INT){
  1611. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1612. }
  1613. }
  1614. /**
  1615. * Ensures the specific preconditions of the said instruction.
  1616. */
  1617. public void visitISHL(Instruction o){
  1618. checkTop(o,Type.INT);
  1619. if (stack().peek(1) != Type.INT){
  1620. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1621. }
  1622. }
  1623. /**
  1624. * Ensures the specific preconditions of the said instruction.
  1625. */
  1626. public void visitISHR(Instruction o){
  1627. checkTop(o,Type.INT);
  1628. if (stack().peek(1) != Type.INT){
  1629. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1630. }
  1631. }
  1632. /**
  1633. * Ensures the specific preconditions of the said instruction.
  1634. */
  1635. public void visitISTORE(Instruction o){
  1636. //visitStoreInstruction(StoreInstruction) is called before.
  1637. // Nothing else needs to be done here.
  1638. }
  1639. /**
  1640. * Ensures the specific preconditions of the said instruction.
  1641. */
  1642. public void visitISUB(Instruction o){
  1643. checkTop(o,Type.INT);
  1644. if (stack().peek(1) != Type.INT){
  1645. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1646. }
  1647. }
  1648. /**
  1649. * Ensures the specific preconditions of the said instruction.
  1650. */
  1651. public void visitIUSHR(Instruction o){
  1652. checkTop(o,Type.INT);
  1653. if (stack().peek(1) != Type.INT){
  1654. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1655. }
  1656. }
  1657. /**
  1658. * Ensures the specific preconditions of the said instruction.
  1659. */
  1660. public void visitIXOR(Instruction o){
  1661. if (stack().peek() != Type.INT){
  1662. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1663. }
  1664. if (stack().peek(1) != Type.INT){
  1665. constraintViolated(o, "The value at the stack next-to-top is not of type 'int', but of type '"+stack().peek(1)+"'.");
  1666. }
  1667. }
  1668. /**
  1669. * Ensures the specific preconditions of the said instruction.
  1670. */
  1671. public void visitJSR(InstructionBranch o){
  1672. // nothing to do here.
  1673. }
  1674. /**
  1675. * Ensures the specific preconditions of the said instruction.
  1676. */
  1677. public void visitJSR_W(InstructionBranch o){
  1678. // nothing to do here.
  1679. }
  1680. /**
  1681. * Ensures the specific preconditions of the said instruction.
  1682. */
  1683. public void visitL2D(Instruction o){
  1684. if (stack().peek() != Type.LONG){
  1685. constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
  1686. }
  1687. }
  1688. /**
  1689. * Ensures the specific preconditions of the said instruction.
  1690. */
  1691. public void visitL2F(Instruction o){
  1692. if (stack().peek() != Type.LONG){
  1693. constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
  1694. }
  1695. }
  1696. /**
  1697. * Ensures the specific preconditions of the said instruction.
  1698. */
  1699. public void visitL2I(Instruction o){
  1700. if (stack().peek() != Type.LONG){
  1701. constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
  1702. }
  1703. }
  1704. /**
  1705. * Ensures the specific preconditions of the said instruction.
  1706. */
  1707. public void visitLADD(Instruction o){
  1708. if (stack().peek() != Type.LONG){
  1709. constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
  1710. }
  1711. if (stack().peek(1) != Type.LONG){
  1712. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1713. }
  1714. }
  1715. /**
  1716. * Ensures the specific preconditions of the said instruction.
  1717. */
  1718. public void visitLALOAD(Instruction o){
  1719. indexOfInt(o, stack().peek());
  1720. if (stack().peek(1) == Type.NULL){
  1721. return;
  1722. }
  1723. if (! (stack().peek(1) instanceof ArrayType)){
  1724. constraintViolated(o, "Stack next-to-top must be of type long[] but is '"+stack().peek(1)+"'.");
  1725. }
  1726. Type t = ((ArrayType) (stack().peek(1))).getBasicType();
  1727. if (t != Type.LONG){
  1728. constraintViolated(o, "Stack next-to-top must be of type long[] but is '"+stack().peek(1)+"'.");
  1729. }
  1730. }
  1731. /**
  1732. * Ensures the specific preconditions of the said instruction.
  1733. */
  1734. public void visitLAND(Instruction o){
  1735. if (stack().peek() != Type.LONG){
  1736. constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
  1737. }
  1738. if (stack().peek(1) != Type.LONG){
  1739. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1740. }
  1741. }
  1742. /**
  1743. * Ensures the specific preconditions of the said instruction.
  1744. */
  1745. public void visitLASTORE(Instruction o){
  1746. if (stack().peek() != Type.LONG){
  1747. constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
  1748. }
  1749. indexOfInt(o, stack().peek(1));
  1750. if (stack().peek(2) == Type.NULL){
  1751. return;
  1752. }
  1753. if (! (stack().peek(2) instanceof ArrayType)){
  1754. constraintViolated(o, "Stack next-to-next-to-top must be of type long[] but is '"+stack().peek(2)+"'.");
  1755. }
  1756. Type t = ((ArrayType) (stack().peek(2))).getBasicType();
  1757. if (t != Type.LONG){
  1758. constraintViolated(o, "Stack next-to-next-to-top must be of type long[] but is '"+stack().peek(2)+"'.");
  1759. }
  1760. }
  1761. /**
  1762. * Ensures the specific preconditions of the said instruction.
  1763. */
  1764. public void visitLCMP(Instruction o){
  1765. if (stack().peek() != Type.LONG){
  1766. constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
  1767. }
  1768. if (stack().peek(1) != Type.LONG){
  1769. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1770. }
  1771. }
  1772. /**
  1773. * Ensures the specific preconditions of the said instruction.
  1774. */
  1775. public void visitLCONST(Instruction o){
  1776. // Nothing to do here.
  1777. }
  1778. /**
  1779. * Ensures the specific preconditions of the said instruction.
  1780. */
  1781. public void visitLDC(Instruction o){
  1782. // visitCPInstruction is called first.
  1783. Constant c = cpg.getConstant(o.getIndex());
  1784. if (! ( ( c instanceof ConstantInteger) ||
  1785. ( c instanceof ConstantFloat ) ||
  1786. ( c instanceof ConstantString ) ) ){
  1787. constraintViolated(o, "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '"+c+"'.");
  1788. }
  1789. }
  1790. /**
  1791. * Ensures the specific preconditions of the said instruction.
  1792. */
  1793. public void visitLDC_W(Instruction o){
  1794. // visitCPInstruction is called first.
  1795. Constant c = cpg.getConstant(o.getIndex());
  1796. if (! ( ( c instanceof ConstantInteger) ||
  1797. ( c instanceof ConstantFloat ) ||
  1798. ( c instanceof ConstantString ) ) ){
  1799. constraintViolated(o, "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '"+c+"'.");
  1800. }
  1801. }
  1802. /**
  1803. * Ensures the specific preconditions of the said instruction.
  1804. */
  1805. public void visitLDC2_W(Instruction o){
  1806. // visitCPInstruction is called first.
  1807. Constant c = cpg.getConstant(o.getIndex());
  1808. if (! ( ( c instanceof ConstantLong) ||
  1809. ( c instanceof ConstantDouble ) ) ){
  1810. constraintViolated(o, "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '"+c+"'.");
  1811. }
  1812. }
  1813. /**
  1814. * Ensures the specific preconditions of the said instruction.
  1815. */
  1816. public void visitLDIV(Instruction o){
  1817. if (stack().peek() != Type.LONG){
  1818. constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
  1819. }
  1820. if (stack().peek(1) != Type.LONG){
  1821. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1822. }
  1823. }
  1824. /**
  1825. * Ensures the specific preconditions of the said instruction.
  1826. */
  1827. public void visitLLOAD(Instruction o){
  1828. //visitLoadInstruction(LoadInstruction) is called before.
  1829. // Nothing else needs to be done here.
  1830. }
  1831. /**
  1832. * Ensures the specific preconditions of the said instruction.
  1833. */
  1834. public void visitLMUL(Instruction o){
  1835. if (stack().peek() != Type.LONG){
  1836. constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
  1837. }
  1838. if (stack().peek(1) != Type.LONG){
  1839. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1840. }
  1841. }
  1842. public void visitLNEG(Instruction o){
  1843. checkTop(o,Type.LONG);
  1844. }
  1845. public void visitLOOKUPSWITCH(LOOKUPSWITCH o){
  1846. checkTop(o,Type.INT);
  1847. // See also pass 3a.
  1848. }
  1849. public void visitLOR(Instruction o){
  1850. if (stack().peek() != Type.LONG){
  1851. constraintViolated(o, "The value at the stack top is not of type 'long', but of type '"+stack().peek()+"'.");
  1852. }
  1853. if (stack().peek(1) != Type.LONG){
  1854. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1855. }
  1856. }
  1857. public void visitLREM(Instruction o){
  1858. checkTop(o,Type.LONG);
  1859. if (stack().peek(1) != Type.LONG){
  1860. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1861. }
  1862. }
  1863. public void visitLRETURN(Instruction o) { checkTop(o,Type.LONG); }
  1864. public void visitLSHL(Instruction o){
  1865. checkTop(o,Type.INT);
  1866. if (stack().peek(1) != Type.LONG){
  1867. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1868. }
  1869. }
  1870. /**
  1871. * Ensures the specific preconditions of the said instruction.
  1872. */
  1873. public void visitLSHR(Instruction o){
  1874. if (stack().peek() != Type.INT){
  1875. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  1876. }
  1877. if (stack().peek(1) != Type.LONG){
  1878. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1879. }
  1880. }
  1881. /**
  1882. * Ensures the specific preconditions of the said instruction.
  1883. */
  1884. public void visitLSTORE(Instruction o){
  1885. //visitStoreInstruction(StoreInstruction) is called before.
  1886. // Nothing else needs to be done here.
  1887. }
  1888. /**
  1889. * Ensures the specific preconditions of the said instruction.
  1890. */
  1891. public void visitLSUB(Instruction o){
  1892. checkTop(o,Type.LONG);
  1893. if (stack().peek(1) != Type.LONG){
  1894. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1895. }
  1896. }
  1897. /**
  1898. * Ensures the specific preconditions of the said instruction.
  1899. */
  1900. public void visitLUSHR(Instruction o){
  1901. checkTop(o,Type.INT);
  1902. if (stack().peek(1) != Type.LONG){
  1903. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1904. }
  1905. }
  1906. /**
  1907. * Ensures the specific preconditions of the said instruction.
  1908. */
  1909. public void visitLXOR(Instruction o){
  1910. checkTop(o,Type.LONG);
  1911. if (stack().peek(1) != Type.LONG){
  1912. constraintViolated(o, "The value at the stack next-to-top is not of type 'long', but of type '"+stack().peek(1)+"'.");
  1913. }
  1914. }
  1915. /**
  1916. * Ensures the specific preconditions of the said instruction.
  1917. */
  1918. public void visitMONITORENTER(Instruction o){
  1919. if (! ((stack().peek()) instanceof ReferenceType)){
  1920. constraintViolated(o, "The stack top should be of a ReferenceType, but is '"+stack().peek()+"'.");
  1921. }
  1922. referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
  1923. }
  1924. /**
  1925. * Ensures the specific preconditions of the said instruction.
  1926. */
  1927. public void visitMONITOREXIT(Instruction o){
  1928. if (! ((stack().peek()) instanceof ReferenceType)){
  1929. constraintViolated(o, "The stack top should be of a ReferenceType, but is '"+stack().peek()+"'.");
  1930. }
  1931. referenceTypeIsInitialized(o, (ReferenceType) (stack().peek()) );
  1932. }
  1933. /**
  1934. * Ensures the specific preconditions of the said instruction.
  1935. */
  1936. public void visitMULTIANEWARRAY(MULTIANEWARRAY o){
  1937. int dimensions = o.getDimensions();
  1938. // Dimensions argument is okay: see Pass 3a.
  1939. for (int i=0; i<dimensions; i++){
  1940. if (stack().peek(i) != Type.INT){
  1941. constraintViolated(o, "The '"+dimensions+"' upper stack types should be 'int' but aren't.");
  1942. }
  1943. }
  1944. // The runtime constant pool item at that index must be a symbolic reference to a class,
  1945. // array, or interface type. See Pass 3a.
  1946. }
  1947. /**
  1948. * Ensures the specific preconditions of the said instruction.
  1949. */
  1950. public void visitNEW(Instruction o){
  1951. //visitCPInstruction(CPInstruction) has been called before.
  1952. //visitLoadClass(LoadClass) has been called before.
  1953. Type t = o.getType(cpg);
  1954. if (! (t instanceof ReferenceType)){
  1955. throw new AssertionViolatedException("NEW.getType() returning a non-reference type?!");
  1956. }
  1957. if (! (t instanceof ObjectType)){
  1958. constraintViolated(o, "Expecting a class type (ObjectType) to work on. Found: '"+t+"'.");
  1959. }
  1960. ObjectType obj = (ObjectType) t;
  1961. //e.g.: Don't instantiate interfaces
  1962. if (! obj.referencesClass()){
  1963. constraintViolated(o, "Expecting a class type (ObjectType) to work on. Found: '"+obj+"'.");
  1964. }
  1965. }
  1966. public void visitNEWARRAY(Instruction o) { checkTop(o,Type.INT); }
  1967. public void visitNOP(Instruction o){ /* nothing is to be done here */ }
  1968. public void visitPOP(Instruction o){
  1969. if (stack().peek().getSize() != 1){
  1970. constraintViolated(o, "Stack top size should be 1 but stack top is '"+stack().peek()+"' of size '"+stack().peek().getSize()+"'.");
  1971. }
  1972. }
  1973. /**
  1974. * Ensures the specific preconditions of the said instruction.
  1975. */
  1976. public void visitPOP2(Instruction o){
  1977. if (stack().peek().getSize() != 2){
  1978. constraintViolated(o, "Stack top size should be 2 but stack top is '"+stack().peek()+"' of size '"+stack().peek().getSize()+"'.");
  1979. }
  1980. }
  1981. /**
  1982. * Ensures the specific preconditions of the said instruction.
  1983. */
  1984. public void visitPUTFIELD(FieldInstruction o){
  1985. Type objectref = stack().peek(1);
  1986. if (! ( (objectref instanceof ObjectType) || (objectref == Type.NULL) ) ){
  1987. constraintViolated(o, "Stack next-to-top should be an object reference that's not an array reference, but is '"+objectref+"'.");
  1988. }
  1989. String field_name = o.getFieldName(cpg);
  1990. JavaClass jc = Repository.lookupClass(o.getClassType(cpg).getClassName());
  1991. Field[] fields = jc.getFields();
  1992. Field f = null;
  1993. for (int i=0; i<fields.length; i++){
  1994. if (fields[i].getName().equals(field_name)){
  1995. f = fields[i];
  1996. break;
  1997. }
  1998. }
  1999. if (f == null){
  2000. throw new AssertionViolatedException("Field not found?!?");
  2001. }
  2002. Type value = stack().peek();
  2003. Type t = Type.getType(f.getSignature());
  2004. Type shouldbe = t;
  2005. if (shouldbe == Type.BOOLEAN ||
  2006. shouldbe == Type.BYTE ||
  2007. shouldbe == Type.CHAR ||
  2008. shouldbe == Type.SHORT){
  2009. shouldbe = Type.INT;
  2010. }
  2011. if (t instanceof ReferenceType){
  2012. ReferenceType rvalue = null;
  2013. if (value instanceof ReferenceType){
  2014. rvalue = (ReferenceType) value;
  2015. referenceTypeIsInitialized(o, rvalue);
  2016. }
  2017. else{
  2018. constraintViolated(o, "The stack top type '"+value+"' is not of a reference type as expected.");
  2019. }
  2020. // TODO: This can possibly only be checked using Staerk-et-al's "set-of-object types", not
  2021. // using "wider cast object types" created during verification.
  2022. // Comment it out if you encounter problems. See also the analogon at visitPUTSTATIC.
  2023. if (!(rvalue.isAssignmentCompatibleWith(shouldbe))){
  2024. constraintViolated(o, "The stack top type '"+value+"' is not assignment compatible with '"+shouldbe+"'.");
  2025. }
  2026. }
  2027. else{
  2028. if (shouldbe != value){
  2029. constraintViolated(o, "The stack top type '"+value+"' is not of type '"+shouldbe+"' as expected.");
  2030. }
  2031. }
  2032. if (f.isProtected()){
  2033. ObjectType classtype = o.getClassType(cpg);
  2034. ObjectType curr = new ObjectType(mg.getClassName());
  2035. if ( classtype.equals(curr) ||
  2036. curr.subclassOf(classtype) ){
  2037. Type tp = stack().peek(1);
  2038. if (tp == Type.NULL){
  2039. return;
  2040. }
  2041. if (! (tp instanceof ObjectType) ){
  2042. constraintViolated(o, "The 'objectref' must refer to an object that's not an array. Found instead: '"+tp+"'.");
  2043. }
  2044. ObjectType objreftype = (ObjectType) tp;
  2045. if (! ( objreftype.equals(curr) ||
  2046. objreftype.subclassOf(curr) ) ){
  2047. constraintViolated(o, "The referenced field has the ACC_PROTECTED modifier, and it's a member of the current class or a superclass of the current class. However, the referenced object type '"+stack().peek()+"' is not the current class or a subclass of the current class.");
  2048. }
  2049. }
  2050. }
  2051. // TODO: Could go into Pass 3a.
  2052. if (f.isStatic()){
  2053. constraintViolated(o, "Referenced field '"+f+"' is static which it shouldn't be.");
  2054. }
  2055. }
  2056. /**
  2057. * Ensures the specific preconditions of the said instruction.
  2058. */
  2059. public void visitPUTSTATIC(FieldInstruction o){
  2060. String field_name = o.getFieldName(cpg);
  2061. JavaClass jc = Repository.lookupClass(o.getClassType(cpg).getClassName());
  2062. Field[] fields = jc.getFields();
  2063. Field f = null;
  2064. for (int i=0; i<fields.length; i++){
  2065. if (fields[i].getName().equals(field_name)){
  2066. f = fields[i];
  2067. break;
  2068. }
  2069. }
  2070. if (f == null){
  2071. throw new AssertionViolatedException("Field not found?!?");
  2072. }
  2073. Type value = stack().peek();
  2074. Type t = Type.getType(f.getSignature());
  2075. Type shouldbe = t;
  2076. if (shouldbe == Type.BOOLEAN ||
  2077. shouldbe == Type.BYTE ||
  2078. shouldbe == Type.CHAR ||
  2079. shouldbe == Type.SHORT){
  2080. shouldbe = Type.INT;
  2081. }
  2082. if (t instanceof ReferenceType){
  2083. ReferenceType rvalue = null;
  2084. if (value instanceof ReferenceType){
  2085. rvalue = (ReferenceType) value;
  2086. referenceTypeIsInitialized(o, rvalue);
  2087. }
  2088. else{
  2089. constraintViolated(o, "The stack top type '"+value+"' is not of a reference type as expected.");
  2090. }
  2091. // TODO: This can possibly only be checked using Staerk-et-al's "set-of-object types", not
  2092. // using "wider cast object types" created during verification.
  2093. // Comment it out if you encounter problems. See also the analogon at visitPUTFIELD.
  2094. if (!(rvalue.isAssignmentCompatibleWith(shouldbe))){
  2095. constraintViolated(o, "The stack top type '"+value+"' is not assignment compatible with '"+shouldbe+"'.");
  2096. }
  2097. }
  2098. else{
  2099. if (shouldbe != value){
  2100. constraintViolated(o, "The stack top type '"+value+"' is not of type '"+shouldbe+"' as expected.");
  2101. }
  2102. }
  2103. // TODO: Interface fields may be assigned to only once. (Hard to implement in
  2104. // JustIce's execution model). This may only happen in <clinit>, see Pass 3a.
  2105. }
  2106. /**
  2107. * Ensures the specific preconditions of the said instruction.
  2108. */
  2109. public void visitRET(RET o){
  2110. if (! (locals().get(o.getIndex()) instanceof ReturnaddressType)){
  2111. constraintViolated(o, "Expecting a ReturnaddressType in local variable "+o.getIndex()+".");
  2112. }
  2113. if (locals().get(o.getIndex()) == ReturnaddressType.NO_TARGET){
  2114. throw new AssertionViolatedException("Oops: RET expecting a target!");
  2115. }
  2116. // Other constraints such as non-allowed overlapping subroutines are enforced
  2117. // while building the Subroutines data structure.
  2118. }
  2119. /**
  2120. * Ensures the specific preconditions of the said instruction.
  2121. */
  2122. public void visitRETURN(Instruction o){
  2123. if (mg.getName().equals(Constants.CONSTRUCTOR_NAME)){// If we leave an <init> method
  2124. if ((Frame._this != null) && (!(mg.getClassName().equals(Type.OBJECT.getClassName()))) ) {
  2125. constraintViolated(o, "Leaving a constructor that itself did not call a constructor.");
  2126. }
  2127. }
  2128. }
  2129. /**
  2130. * Ensures the specific preconditions of the said instruction.
  2131. */
  2132. public void visitSALOAD(Instruction o){
  2133. indexOfInt(o, stack().peek());
  2134. if (stack().peek(1) == Type.NULL){
  2135. return;
  2136. }
  2137. if (! (stack().peek(1) instanceof ArrayType)){
  2138. constraintViolated(o, "Stack next-to-top must be of type short[] but is '"+stack().peek(1)+"'.");
  2139. }
  2140. Type t = ((ArrayType) (stack().peek(1))).getBasicType();
  2141. if (t != Type.SHORT){
  2142. constraintViolated(o, "Stack next-to-top must be of type short[] but is '"+stack().peek(1)+"'.");
  2143. }
  2144. }
  2145. /**
  2146. * Ensures the specific preconditions of the said instruction.
  2147. */
  2148. public void visitSASTORE(Instruction o){
  2149. if (stack().peek() != Type.INT){
  2150. constraintViolated(o, "The value at the stack top is not of type 'int', but of type '"+stack().peek()+"'.");
  2151. }
  2152. indexOfInt(o, stack().peek(1));
  2153. if (stack().peek(2) == Type.NULL){
  2154. return;
  2155. }
  2156. if (! (stack().peek(2) instanceof ArrayType)){
  2157. constraintViolated(o, "Stack next-to-next-to-top must be of type short[] but is '"+stack().peek(2)+"'.");
  2158. }
  2159. Type t = ((ArrayType) (stack().peek(2))).getBasicType();
  2160. if (t != Type.SHORT){
  2161. constraintViolated(o, "Stack next-to-next-to-top must be of type short[] but is '"+stack().peek(2)+"'.");
  2162. }
  2163. }
  2164. /**
  2165. * Ensures the specific preconditions of the said instruction.
  2166. */
  2167. public void visitSIPUSH(Instruction o){
  2168. // nothing to do here. Generic visitXXX() methods did the trick before.
  2169. }
  2170. /**
  2171. * Ensures the specific preconditions of the said instruction.
  2172. */
  2173. public void visitSWAP(Instruction o){
  2174. if (stack().peek().getSize() != 1){
  2175. constraintViolated(o, "The value at the stack top is not of size '1', but of size '"+stack().peek().getSize()+"'.");
  2176. }
  2177. if (stack().peek(1).getSize() != 1){
  2178. constraintViolated(o, "The value at the stack next-to-top is not of size '1', but of size '"+stack().peek(1).getSize()+"'.");
  2179. }
  2180. }
  2181. /**
  2182. * Ensures the specific preconditions of the said instruction.
  2183. */
  2184. public void visitTABLESWITCH(TABLESWITCH o){
  2185. indexOfInt(o, stack().peek());
  2186. // See Pass 3a.
  2187. }
  2188. }