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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444
  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 (https://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. * <https://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="https://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. }