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.

Subroutines.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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.generic.*;
  57. import org.aspectj.apache.bcel.verifier.exc.*;
  58. import java.awt.Color;
  59. import java.io.Serializable;
  60. import java.util.ArrayList;
  61. import java.util.Enumeration;
  62. import java.util.HashSet;
  63. import java.util.Hashtable;
  64. import java.util.Iterator;
  65. /**
  66. * Instances of this class contain information about the subroutines
  67. * found in a code array of a method.
  68. * This implementation considers the top-level (the instructions
  69. * reachable without a JSR or JSR_W starting off from the first
  70. * instruction in a code array of a method) being a special subroutine;
  71. * see getTopLevel() for that.
  72. * Please note that the definition of subroutines in the Java Virtual
  73. * Machine Specification, Second Edition is somewhat incomplete.
  74. * Therefore, JustIce uses an own, more rigid notion.
  75. * Basically, a subroutine is a piece of code that starts at the target
  76. * of a JSR of JSR_W instruction and ends at a corresponding RET
  77. * instruction. Note also that the control flow of a subroutine
  78. * may be complex and non-linear; and that subroutines may be nested.
  79. * JustIce also mandates subroutines not to be protected by exception
  80. * handling code (for the sake of control flow predictability).
  81. * To understand JustIce's notion of subroutines, please read
  82. *
  83. * TODO: refer to the paper.
  84. *
  85. * @version $Id: Subroutines.java,v 1.4 2009/09/09 19:56:20 aclement Exp $
  86. * @author <A HREF="https://www.inf.fu-berlin.de/~ehaase"/>Enver Haase</A>
  87. * @see #getTopLevel()
  88. */
  89. public class Subroutines{
  90. /**
  91. * This inner class implements the Subroutine interface.
  92. */
  93. private class SubroutineImpl implements Subroutine{
  94. /**
  95. * UNSET, a symbol for an uninitialized localVariable
  96. * field. This is used for the "top-level" Subroutine;
  97. * i.e. no subroutine.
  98. */
  99. private final static int UNSET = -1;
  100. /**
  101. * The Local Variable slot where the first
  102. * instruction of this subroutine (an ASTORE) stores
  103. * the JsrInstruction's ReturnAddress in and
  104. * the RET of this subroutine operates on.
  105. */
  106. private int localVariable = UNSET;
  107. /** The instructions that belong to this subroutine. */
  108. private HashSet<Serializable> instructions = new HashSet<Serializable>(); // Elements: InstructionHandle
  109. /*
  110. * Refer to the Subroutine interface for documentation.
  111. */
  112. public boolean contains(InstructionHandle inst){
  113. return instructions.contains(inst);
  114. }
  115. /**
  116. * The JSR or JSR_W instructions that define this
  117. * subroutine by targeting it.
  118. */
  119. private HashSet<InstructionHandle> theJSRs = new HashSet<InstructionHandle>();
  120. /**
  121. * The RET instruction that leaves this subroutine.
  122. */
  123. private InstructionHandle theRET;
  124. /**
  125. * Returns a String representation of this object, merely
  126. * for debugging purposes.
  127. * (Internal) Warning: Verbosity on a problematic subroutine may cause
  128. * stack overflow errors due to recursive subSubs() calls.
  129. * Don't use this, then.
  130. */
  131. public String toString(){
  132. String ret = "Subroutine: Local variable is '"+localVariable+"', JSRs are '"+theJSRs+"', RET is '"+theRET+"', Instructions: '"+instructions.toString()+"'.";
  133. ret += " Accessed local variable slots: '";
  134. int[] alv = getAccessedLocalsIndices();
  135. for (int i=0; i<alv.length; i++){
  136. ret += alv[i]+" ";
  137. }
  138. ret+="'.";
  139. ret += " Recursively (via subsub...routines) accessed local variable slots: '";
  140. alv = getRecursivelyAccessedLocalsIndices();
  141. for (int i=0; i<alv.length; i++){
  142. ret += alv[i]+" ";
  143. }
  144. ret+="'.";
  145. return ret;
  146. }
  147. /**
  148. * Sets the leaving RET instruction. Must be invoked after all instructions are added.
  149. * Must not be invoked for top-level 'subroutine'.
  150. */
  151. void setLeavingRET(){
  152. if (localVariable == UNSET){
  153. throw new AssertionViolatedException("setLeavingRET() called for top-level 'subroutine' or forgot to set local variable first.");
  154. }
  155. Iterator<Serializable> iter = instructions.iterator();
  156. InstructionHandle ret = null;
  157. while(iter.hasNext()){
  158. InstructionHandle actual = (InstructionHandle) iter.next();
  159. if (actual.getInstruction() instanceof RET){
  160. if (ret != null){
  161. throw new StructuralCodeConstraintException("Subroutine with more then one RET detected: '"+ret+"' and '"+actual+"'.");
  162. }
  163. else{
  164. ret = actual;
  165. }
  166. }
  167. }
  168. if (ret == null){
  169. throw new StructuralCodeConstraintException("Subroutine without a RET detected.");
  170. }
  171. if (((RET) ret.getInstruction()).getIndex() != localVariable){
  172. throw new StructuralCodeConstraintException("Subroutine uses '"+ret+"' which does not match the correct local variable '"+localVariable+"'.");
  173. }
  174. theRET = ret;
  175. }
  176. /*
  177. * Refer to the Subroutine interface for documentation.
  178. */
  179. public InstructionHandle[] getEnteringJsrInstructions(){
  180. if (this == TOPLEVEL) {
  181. throw new AssertionViolatedException("getLeavingRET() called on top level pseudo-subroutine.");
  182. }
  183. InstructionHandle[] jsrs = new InstructionHandle[theJSRs.size()];
  184. return (theJSRs.toArray(jsrs));
  185. }
  186. /**
  187. * Adds a new JSR or JSR_W that has this subroutine as its target.
  188. */
  189. public void addEnteringJsrInstruction(InstructionHandle jsrInst){
  190. if ( (jsrInst == null) || (! (jsrInst.getInstruction().isJsrInstruction()))){
  191. throw new AssertionViolatedException("Expecting JsrInstruction InstructionHandle.");
  192. }
  193. if (localVariable == UNSET){
  194. throw new AssertionViolatedException("Set the localVariable first!");
  195. }
  196. else{
  197. // Something is wrong when an ASTORE is targeted that does not operate on the same local variable than the rest of the
  198. // JsrInstruction-targets and the RET.
  199. // (We don't know out leader here so we cannot check if we're really targeted!)
  200. if (localVariable != ( (((InstructionBranch) jsrInst.getInstruction()).getTarget().getInstruction())).getIndex()){
  201. throw new AssertionViolatedException("Setting a wrong JsrInstruction.");
  202. }
  203. }
  204. theJSRs.add(jsrInst);
  205. }
  206. /*
  207. * Refer to the Subroutine interface for documentation.
  208. */
  209. public InstructionHandle getLeavingRET(){
  210. if (this == TOPLEVEL) {
  211. throw new AssertionViolatedException("getLeavingRET() called on top level pseudo-subroutine.");
  212. }
  213. return theRET;
  214. }
  215. /*
  216. * Refer to the Subroutine interface for documentation.
  217. */
  218. public InstructionHandle[] getInstructions(){
  219. InstructionHandle[] ret = new InstructionHandle[instructions.size()];
  220. return instructions.toArray(ret);
  221. }
  222. /*
  223. * Adds an instruction to this subroutine.
  224. * All instructions must have been added before invoking setLeavingRET().
  225. * @see #setLeavingRET
  226. */
  227. void addInstruction(InstructionHandle ih){
  228. if (theRET != null){
  229. throw new AssertionViolatedException("All instructions must have been added before invoking setLeavingRET().");
  230. }
  231. instructions.add(ih);
  232. }
  233. /* Satisfies Subroutine.getRecursivelyAccessedLocalsIndices(). */
  234. public int[] getRecursivelyAccessedLocalsIndices(){
  235. HashSet<Integer> s = new HashSet<Integer>();
  236. int[] lvs = getAccessedLocalsIndices();
  237. for (int j=0; j<lvs.length; j++){
  238. s.add(new Integer(lvs[j]));
  239. }
  240. _getRecursivelyAccessedLocalsIndicesHelper(s, this.subSubs());
  241. int[] ret = new int[s.size()];
  242. Iterator<Integer> i = s.iterator();
  243. int j=-1;
  244. while (i.hasNext()){
  245. j++;
  246. ret[j] = i.next().intValue();
  247. }
  248. return ret;
  249. }
  250. /**
  251. * A recursive helper method for getRecursivelyAccessedLocalsIndices().
  252. * @see #getRecursivelyAccessedLocalsIndices()
  253. */
  254. private void _getRecursivelyAccessedLocalsIndicesHelper(HashSet<Integer> s, Subroutine[] subs){
  255. for (int i=0; i<subs.length; i++){
  256. int[] lvs = subs[i].getAccessedLocalsIndices();
  257. for (int j=0; j<lvs.length; j++){
  258. s.add(new Integer(lvs[j]));
  259. }
  260. if(subs[i].subSubs().length != 0){
  261. _getRecursivelyAccessedLocalsIndicesHelper(s, subs[i].subSubs());
  262. }
  263. }
  264. }
  265. /*
  266. * Satisfies Subroutine.getAccessedLocalIndices().
  267. */
  268. public int[] getAccessedLocalsIndices(){
  269. //TODO: Implement caching.
  270. HashSet<Serializable> acc = new HashSet<Serializable>();
  271. if (theRET == null && this != TOPLEVEL){
  272. throw new AssertionViolatedException("This subroutine object must be built up completely before calculating accessed locals.");
  273. }
  274. Iterator<Serializable> i = instructions.iterator();
  275. while (i.hasNext()){
  276. InstructionHandle ih = (InstructionHandle) i.next();
  277. // RET is not a LocalVariableInstruction in the current version of BCEL.
  278. if (ih.getInstruction() instanceof InstructionLV || ih.getInstruction() instanceof RET){
  279. int idx = ((ih.getInstruction())).getIndex();
  280. acc.add(new Integer(idx));
  281. // LONG? DOUBLE?.
  282. try{
  283. // LocalVariableInstruction instances are typed without the need to look into
  284. // the constant pool.
  285. if (ih.getInstruction() instanceof InstructionLV){
  286. int s = ((InstructionLV) ih.getInstruction()).getType(null).getSize();
  287. if (s==2) acc.add(new Integer(idx+1));
  288. }
  289. }
  290. catch(RuntimeException re){
  291. throw new AssertionViolatedException("Oops. BCEL did not like NULL as a ConstantPoolGen object.");
  292. }
  293. }
  294. }
  295. int[] ret = new int[acc.size()];
  296. i = acc.iterator();
  297. int j=-1;
  298. while (i.hasNext()){
  299. j++;
  300. ret[j] = ((Integer) i.next()).intValue();
  301. }
  302. return ret;
  303. }
  304. /*
  305. * Satisfies Subroutine.subSubs().
  306. */
  307. public Subroutine[] subSubs(){
  308. HashSet<Subroutine> h = new HashSet<Subroutine>();
  309. Iterator<Serializable> i = instructions.iterator();
  310. while (i.hasNext()){
  311. Instruction inst = ((InstructionHandle) i.next()).getInstruction();
  312. if (inst.isJsrInstruction()){
  313. InstructionHandle targ = ((InstructionBranch) inst).getTarget();
  314. h.add(getSubroutine(targ));
  315. }
  316. }
  317. Subroutine[] ret = new Subroutine[h.size()];
  318. return h.toArray(ret);
  319. }
  320. /*
  321. * Sets the local variable slot the ASTORE that is targeted
  322. * by the JsrInstructions of this subroutine operates on.
  323. * This subroutine's RET operates on that same local variable
  324. * slot, of course.
  325. */
  326. void setLocalVariable(int i){
  327. if (localVariable != UNSET){
  328. throw new AssertionViolatedException("localVariable set twice.");
  329. }
  330. else{
  331. localVariable = i;
  332. }
  333. }
  334. /**
  335. * The default constructor.
  336. */
  337. public SubroutineImpl(){
  338. }
  339. }// end Inner Class SubrouteImpl
  340. /**
  341. * The Hashtable containing the subroutines found.
  342. * Key: InstructionHandle of the leader of the subroutine.
  343. * Elements: SubroutineImpl objects.
  344. */
  345. private Hashtable<InstructionHandle, Subroutine> subroutines = new Hashtable<InstructionHandle, Subroutine>();
  346. /**
  347. * This is referring to a special subroutine, namely the
  348. * top level. This is not really a subroutine but we use
  349. * it to distinguish between top level instructions and
  350. * unreachable instructions.
  351. */
  352. public final Subroutine TOPLEVEL;
  353. /**
  354. * Constructor.
  355. * @param il A MethodGen object representing method to
  356. * create the Subroutine objects of.
  357. */
  358. public Subroutines(MethodGen mg){
  359. InstructionHandle[] all = mg.getInstructionList().getInstructionHandles();
  360. CodeExceptionGen[] handlers = mg.getExceptionHandlers();
  361. // Define our "Toplevel" fake subroutine.
  362. TOPLEVEL = new SubroutineImpl();
  363. // Calculate "real" subroutines.
  364. HashSet<InstructionHandle> sub_leaders = new HashSet<InstructionHandle>(); // Elements: InstructionHandle
  365. InstructionHandle ih = all[0];
  366. for (int i=0; i<all.length; i++){
  367. Instruction inst = all[i].getInstruction();
  368. if (inst.isJsrInstruction()){
  369. sub_leaders.add(((InstructionBranch) inst).getTarget());
  370. }
  371. }
  372. // Build up the database.
  373. Iterator<InstructionHandle> iter = sub_leaders.iterator();
  374. while (iter.hasNext()){
  375. SubroutineImpl sr = new SubroutineImpl();
  376. InstructionHandle astore = (iter.next());
  377. sr.setLocalVariable( ( (astore.getInstruction())).getIndex() );
  378. subroutines.put(astore, sr);
  379. }
  380. // Fake it a bit. We want a virtual "TopLevel" subroutine.
  381. subroutines.put(all[0], TOPLEVEL);
  382. sub_leaders.add(all[0]);
  383. // Tell the subroutines about their JsrInstructions.
  384. // Note that there cannot be a JSR targeting the top-level
  385. // since "Jsr 0" is disallowed in Pass 3a.
  386. // Instructions shared by a subroutine and the toplevel are
  387. // disallowed and checked below, after the BFS.
  388. for (int i=0; i<all.length; i++){
  389. Instruction inst = all[i].getInstruction();
  390. if (inst.isJsrInstruction()){
  391. InstructionHandle leader = ((InstructionBranch) inst).getTarget();
  392. ((SubroutineImpl) getSubroutine(leader)).addEnteringJsrInstruction(all[i]);
  393. }
  394. }
  395. // Now do a BFS from every subroutine leader to find all the
  396. // instructions that belong to a subroutine.
  397. HashSet<InstructionHandle> instructions_assigned = new HashSet<InstructionHandle>(); // we don't want to assign an instruction to two or more Subroutine objects.
  398. Hashtable<InstructionHandle, Color> colors = new Hashtable<InstructionHandle, Color>(); //Graph colouring. Key: InstructionHandle, Value: java.awt.Color .
  399. iter = sub_leaders.iterator();
  400. while (iter.hasNext()){
  401. // Do some BFS with "actual" as the root of the graph.
  402. InstructionHandle actual = (iter.next());
  403. // Init colors
  404. for (int i=0; i<all.length; i++){
  405. colors.put(all[i], Color.white);
  406. }
  407. colors.put(actual, Color.gray);
  408. // Init Queue
  409. ArrayList<InstructionHandle> Q = new ArrayList<InstructionHandle>();
  410. Q.add(actual); // add(Obj) adds to the end, remove(0) removes from the start.
  411. /* BFS ALGORITHM MODIFICATION: Start out with multiple "root" nodes, as exception handlers are starting points of top-level code, too. [why top-level? TODO: Refer to the special JustIce notion of subroutines.]*/
  412. if (actual == all[0]){
  413. for (int j=0; j<handlers.length; j++){
  414. colors.put(handlers[j].getHandlerPC(), Color.gray);
  415. Q.add(handlers[j].getHandlerPC());
  416. }
  417. }
  418. /* CONTINUE NORMAL BFS ALGORITHM */
  419. // Loop until Queue is empty
  420. while (Q.size() != 0){
  421. InstructionHandle u = Q.remove(0);
  422. InstructionHandle[] successors = getSuccessors(u);
  423. for (int i=0; i<successors.length; i++){
  424. if (colors.get(successors[i]) == Color.white){
  425. colors.put(successors[i], Color.gray);
  426. Q.add(successors[i]);
  427. }
  428. }
  429. colors.put(u, Color.black);
  430. }
  431. // BFS ended above.
  432. for (int i=0; i<all.length; i++){
  433. if (colors.get(all[i]) == Color.black){
  434. ((SubroutineImpl) (actual==all[0]?getTopLevel():getSubroutine(actual))).addInstruction(all[i]);
  435. if (instructions_assigned.contains(all[i])){
  436. throw new StructuralCodeConstraintException("Instruction '"+all[i]+"' is part of more than one subroutine (or of the top level and a subroutine).");
  437. }
  438. else{
  439. instructions_assigned.add(all[i]);
  440. }
  441. }
  442. }
  443. if (actual != all[0]){// If we don't deal with the top-level 'subroutine'
  444. ((SubroutineImpl) getSubroutine(actual)).setLeavingRET();
  445. }
  446. }
  447. // Now make sure no instruction of a Subroutine is protected by exception handling code
  448. // as is mandated by JustIces notion of subroutines.
  449. for (int i=0; i<handlers.length; i++){
  450. InstructionHandle _protected = handlers[i].getStartPC();
  451. while (_protected != handlers[i].getEndPC().getNext()){// Note the inclusive/inclusive notation of "generic API" exception handlers!
  452. Enumeration<Subroutine> subs = subroutines.elements();
  453. while (subs.hasMoreElements()){
  454. Subroutine sub = subs.nextElement();
  455. if (sub != subroutines.get(all[0])){ // We don't want to forbid top-level exception handlers.
  456. if (sub.contains(_protected)){
  457. throw new StructuralCodeConstraintException("Subroutine instruction '"+_protected+"' is protected by an exception handler, '"+handlers[i]+"'. This is forbidden by the JustIce verifier due to its clear definition of subroutines.");
  458. }
  459. }
  460. }
  461. _protected = _protected.getNext();
  462. }
  463. }
  464. // Now make sure no subroutine is calling a subroutine
  465. // that uses the same local variable for the RET as themselves
  466. // (recursively).
  467. // This includes that subroutines may not call themselves
  468. // recursively, even not through intermediate calls to other
  469. // subroutines.
  470. noRecursiveCalls(getTopLevel(), new HashSet<Integer>());
  471. }
  472. /**
  473. * This (recursive) utility method makes sure that
  474. * no subroutine is calling a subroutine
  475. * that uses the same local variable for the RET as themselves
  476. * (recursively).
  477. * This includes that subroutines may not call themselves
  478. * recursively, even not through intermediate calls to other
  479. * subroutines.
  480. *
  481. * @throws StructuralCodeConstraintException if the above constraint is not satisfied.
  482. */
  483. private void noRecursiveCalls(Subroutine sub, HashSet<Integer> set){
  484. Subroutine[] subs = sub.subSubs();
  485. for (int i=0; i<subs.length; i++){
  486. int index = ((RET) (subs[i].getLeavingRET().getInstruction())).getIndex();
  487. if (!set.add(new Integer(index))){
  488. // Don't use toString() here because of possibly infinite recursive subSubs() calls then.
  489. SubroutineImpl si = (SubroutineImpl) subs[i];
  490. throw new StructuralCodeConstraintException("Subroutine with local variable '"+si.localVariable+"', JSRs '"+si.theJSRs+"', RET '"+si.theRET+"' is called by a subroutine which uses the same local variable index as itself; maybe even a recursive call? JustIce's clean definition of a subroutine forbids both.");
  491. }
  492. noRecursiveCalls(subs[i], set);
  493. set.remove(new Integer(index));
  494. }
  495. }
  496. /**
  497. * Returns the Subroutine object associated with the given
  498. * leader (that is, the first instruction of the subroutine).
  499. * You must not use this to get the top-level instructions
  500. * modeled as a Subroutine object.
  501. *
  502. * @see #getTopLevel()
  503. */
  504. public Subroutine getSubroutine(InstructionHandle leader){
  505. Subroutine ret = subroutines.get(leader);
  506. if (ret == null){
  507. throw new AssertionViolatedException("Subroutine requested for an InstructionHandle that is not a leader of a subroutine.");
  508. }
  509. if (ret == TOPLEVEL){
  510. throw new AssertionViolatedException("TOPLEVEL special subroutine requested; use getTopLevel().");
  511. }
  512. return ret;
  513. }
  514. /**
  515. * Returns the subroutine object associated with the
  516. * given instruction. This is a costly operation, you
  517. * should consider using getSubroutine(InstructionHandle).
  518. * Returns 'null' if the given InstructionHandle lies
  519. * in so-called 'dead code', i.e. code that can never
  520. * be executed.
  521. *
  522. * @see #getSubroutine(InstructionHandle)
  523. * @see #getTopLevel()
  524. */
  525. public Subroutine subroutineOf(InstructionHandle any){
  526. Iterator<Subroutine> i = subroutines.values().iterator();
  527. while (i.hasNext()){
  528. Subroutine s = i.next();
  529. if (s.contains(any)) return s;
  530. }
  531. System.err.println("DEBUG: Please verify '"+any+"' lies in dead code.");
  532. return null;
  533. //throw new AssertionViolatedException("No subroutine for InstructionHandle found (DEAD CODE?).");
  534. }
  535. /**
  536. * For easy handling, the piece of code that is <B>not</B> a
  537. * subroutine, the top-level, is also modeled as a Subroutine
  538. * object.
  539. * It is a special Subroutine object where <B>you must not invoke
  540. * getEnteringJsrInstructions() or getLeavingRET()</B>.
  541. *
  542. * @see Subroutine#getEnteringJsrInstructions()
  543. * @see Subroutine#getLeavingRET()
  544. */
  545. public Subroutine getTopLevel(){
  546. return TOPLEVEL;
  547. }
  548. /**
  549. * A utility method that calculates the successors of a given InstructionHandle
  550. * <B>in the same subroutine</B>. That means, a RET does not have any successors
  551. * as defined here. A JsrInstruction has its physical successor as its successor
  552. * (opposed to its target) as defined here.
  553. */
  554. private static InstructionHandle[] getSuccessors(InstructionHandle instruction){
  555. final InstructionHandle[] empty = new InstructionHandle[0];
  556. final InstructionHandle[] single = new InstructionHandle[1];
  557. final InstructionHandle[] pair = new InstructionHandle[2];
  558. Instruction inst = instruction.getInstruction();
  559. if (inst instanceof RET){
  560. return empty;
  561. }
  562. // Terminates method normally.
  563. if (inst.isReturnInstruction()){
  564. return empty;
  565. }
  566. // Terminates method abnormally, because JustIce mandates
  567. // subroutines not to be protected by exception handlers.
  568. if (inst.getOpcode()==Constants.ATHROW){
  569. return empty;
  570. }
  571. // See method comment.
  572. if (inst.isJsrInstruction()){
  573. single[0] = instruction.getNext();
  574. return single;
  575. }
  576. if (inst.getOpcode()==Constants.GOTO || inst.getOpcode()==Constants.GOTO_W){
  577. single[0] = ((InstructionBranch) inst).getTarget();
  578. return single;
  579. }
  580. if (inst instanceof InstructionBranch){
  581. if (inst instanceof InstructionSelect){
  582. // BCEL's getTargets() returns only the non-default targets,
  583. // thanks to Eli Tilevich for reporting.
  584. InstructionHandle[] matchTargets = ((InstructionSelect) inst).getTargets();
  585. InstructionHandle[] ret = new InstructionHandle[matchTargets.length+1];
  586. ret[0] = ((InstructionSelect) inst).getTarget();
  587. System.arraycopy(matchTargets, 0, ret, 1, matchTargets.length);
  588. return ret;
  589. }
  590. else{
  591. pair[0] = instruction.getNext();
  592. pair[1] = ((InstructionBranch) inst).getTarget();
  593. return pair;
  594. }
  595. }
  596. // default case: Fall through.
  597. single[0] = instruction.getNext();
  598. return single;
  599. }
  600. /**
  601. * Returns a String representation of this object; merely for debugging puposes.
  602. */
  603. public String toString(){
  604. return "---\n"+subroutines.toString()+"\n---\n";
  605. }
  606. }