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.

ControlFlow.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /*
  2. * Javassist, a Java-bytecode translator toolkit.
  3. * Copyright (C) 1999- Shigeru Chiba. All Rights Reserved.
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. Alternatively, the contents of this file may be used under
  8. * the terms of the GNU Lesser General Public License Version 2.1 or later,
  9. * or the Apache License Version 2.0.
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. */
  16. package javassist.bytecode.analysis;
  17. import java.util.ArrayList;
  18. import javassist.CtClass;
  19. import javassist.CtMethod;
  20. import javassist.bytecode.BadBytecode;
  21. import javassist.bytecode.MethodInfo;
  22. import javassist.bytecode.stackmap.BasicBlock;
  23. /**
  24. * Represents the control flow graph of a given method.
  25. *
  26. * <p>To obtain the control flow graph, do the following:</p>
  27. *
  28. * <pre>CtMethod m = ...
  29. * ControlFlow cf = new ControlFlow(m);
  30. * Block[] blocks = cf.basicBlocks();
  31. * </pre>
  32. *
  33. * <p><code>blocks</code> is an array of basic blocks in
  34. * that method body.</p>
  35. *
  36. * @see javassist.CtMethod
  37. * @see Block
  38. * @see Frame
  39. * @see Analyzer
  40. * @author Shigeru Chiba
  41. * @since 3.16
  42. */
  43. public class ControlFlow {
  44. private CtClass clazz;
  45. private MethodInfo methodInfo;
  46. private Block[] basicBlocks;
  47. private Frame[] frames;
  48. /**
  49. * Constructs a control-flow analyzer for the given method.
  50. */
  51. public ControlFlow(CtMethod method) throws BadBytecode {
  52. this(method.getDeclaringClass(), method.getMethodInfo2());
  53. }
  54. /**
  55. * Constructs a control-flow analyzer.
  56. */
  57. public ControlFlow(CtClass ctclazz, MethodInfo minfo) throws BadBytecode {
  58. clazz = ctclazz;
  59. methodInfo = minfo;
  60. frames = null;
  61. basicBlocks = (Block[])new BasicBlock.Maker() {
  62. protected BasicBlock makeBlock(int pos) {
  63. return new Block(pos, methodInfo);
  64. }
  65. protected BasicBlock[] makeArray(int size) {
  66. return new Block[size];
  67. }
  68. }.make(minfo);
  69. int size = basicBlocks.length;
  70. int[] counters = new int[size];
  71. for (int i = 0; i < size; i++) {
  72. Block b = basicBlocks[i];
  73. b.index = i;
  74. b.entrances = new Block[b.incomings()];
  75. counters[i] = 0;
  76. }
  77. for (int i = 0; i < size; i++) {
  78. Block b = basicBlocks[i];
  79. for (int k = 0; k < b.exits(); k++) {
  80. Block e = b.exit(k);
  81. e.entrances[counters[e.index]++] = b;
  82. }
  83. }
  84. }
  85. /**
  86. * Returns all the basic blocks in the method body.
  87. */
  88. public Block[] basicBlocks() {
  89. return basicBlocks;
  90. }
  91. /**
  92. * Returns the types of the local variables and stack frame entries
  93. * available at the given position. If the byte at the position is
  94. * not the first byte of an instruction, then this method returns
  95. * null.
  96. *
  97. * @param pos the position.
  98. */
  99. public Frame frameAt(int pos) throws BadBytecode {
  100. if (frames == null)
  101. frames = new Analyzer().analyze(clazz, methodInfo);
  102. return frames[pos];
  103. }
  104. /**
  105. * Constructs a dominator tree. This method returns an array of
  106. * the tree nodes. The first element of the array is the root
  107. * of the tree.
  108. *
  109. * <p> The order of the elements is the same as that
  110. * of the elements in the <code>Block</code> array returned
  111. * by the <code>basicBlocks</code>
  112. * method. If a <code>Block</code> object is at the i-th position
  113. * in the <code>Block</code> array, then
  114. * the <code>Node</code> object referring to that
  115. * <code>Block</code> object is at the i-th position in the
  116. * array returned by this method.
  117. * For every array element <code>node</code>, its index in the
  118. * array is equivalent to <code>node.block().index()</code>.
  119. *
  120. * @return an array of the tree nodes, or null if the method is abstract.
  121. * @see Node#block()
  122. * @see Block#index()
  123. */
  124. public Node[] dominatorTree() {
  125. int size = basicBlocks.length;
  126. if (size == 0)
  127. return null;
  128. Node[] nodes = new Node[size];
  129. boolean[] visited = new boolean[size];
  130. int[] distance = new int[size];
  131. for (int i = 0; i < size; i++) {
  132. nodes[i] = new Node(basicBlocks[i]);
  133. visited[i] = false;
  134. }
  135. Access access = new Access(nodes) {
  136. BasicBlock[] exits(Node n) { return n.block.getExit(); }
  137. BasicBlock[] entrances(Node n) { return n.block.entrances; }
  138. };
  139. nodes[0].makeDepth1stTree(null, visited, 0, distance, access);
  140. do {
  141. for (int i = 0; i < size; i++)
  142. visited[i] = false;
  143. } while (nodes[0].makeDominatorTree(visited, distance, access));
  144. Node.setChildren(nodes);
  145. return nodes;
  146. }
  147. /**
  148. * Constructs a post dominator tree. This method returns an array of
  149. * the tree nodes. Note that the tree has multiple roots.
  150. * The parent of the root nodes is null.
  151. *
  152. * <p> The order of the elements is the same as that
  153. * of the elements in the <code>Block</code> array returned
  154. * by the <code>basicBlocks</code>
  155. * method. If a <code>Block</code> object is at the i-th position
  156. * in the <code>Block</code> array, then
  157. * the <code>Node</code> object referring to that
  158. * <code>Block</code> object is at the i-th position in the
  159. * array returned by this method.
  160. * For every array element <code>node</code>, its index in the
  161. * array is equivalent to <code>node.block().index()</code>.
  162. *
  163. * @return an array of the tree nodes, or null if the method is abstract.
  164. * @see Node#block()
  165. * @see Block#index()
  166. */
  167. public Node[] postDominatorTree() {
  168. int size = basicBlocks.length;
  169. if (size == 0)
  170. return null;
  171. Node[] nodes = new Node[size];
  172. boolean[] visited = new boolean[size];
  173. int[] distance = new int[size];
  174. for (int i = 0; i < size; i++) {
  175. nodes[i] = new Node(basicBlocks[i]);
  176. visited[i] = false;
  177. }
  178. Access access = new Access(nodes) {
  179. BasicBlock[] exits(Node n) { return n.block.entrances; }
  180. BasicBlock[] entrances(Node n) { return n.block.getExit(); }
  181. };
  182. int counter = 0;
  183. for (int i = 0; i < size; i++)
  184. if (nodes[i].block.exits() == 0)
  185. counter = nodes[i].makeDepth1stTree(null, visited, counter, distance, access);
  186. boolean changed;
  187. do {
  188. for (int i = 0; i < size; i++)
  189. visited[i] = false;
  190. changed = false;
  191. for (int i = 0; i < size; i++)
  192. if (nodes[i].block.exits() == 0)
  193. if (nodes[i].makeDominatorTree(visited, distance, access))
  194. changed = true;
  195. } while (changed);
  196. Node.setChildren(nodes);
  197. return nodes;
  198. }
  199. /**
  200. * Basic block.
  201. * It is a sequence of contiguous instructions that do not contain
  202. * jump/branch instructions except the last one.
  203. * Since Java6 or later does not allow <code>JSR</code>,
  204. * we deal with <code>JSR</code> as a non-branch instruction.
  205. */
  206. public static class Block extends BasicBlock {
  207. /**
  208. * A field that can be freely used for storing extra data.
  209. * A client program of this control-flow analyzer can append
  210. * an additional attribute to a <code>Block</code> object.
  211. * The Javassist library never accesses this field.
  212. */
  213. public Object clientData = null;
  214. int index;
  215. MethodInfo method;
  216. Block[] entrances;
  217. Block(int pos, MethodInfo minfo) {
  218. super(pos);
  219. method = minfo;
  220. }
  221. protected void toString2(StringBuffer sbuf) {
  222. super.toString2(sbuf);
  223. sbuf.append(", incoming{");
  224. for (int i = 0; i < entrances.length; i++)
  225. sbuf.append(entrances[i].position).append(", ");
  226. sbuf.append("}");
  227. }
  228. BasicBlock[] getExit() { return exit; }
  229. /**
  230. * Returns the position of this block in the array of
  231. * basic blocks that the <code>basicBlocks</code> method
  232. * returns.
  233. *
  234. * @see #basicBlocks()
  235. */
  236. public int index() { return index; }
  237. /**
  238. * Returns the position of the first instruction
  239. * in this block.
  240. */
  241. public int position() { return position; }
  242. /**
  243. * Returns the length of this block.
  244. */
  245. public int length() { return length; }
  246. /**
  247. * Returns the number of the control paths entering this block.
  248. */
  249. public int incomings() { return incoming; }
  250. /**
  251. * Returns the block that the control may jump into this block from.
  252. */
  253. public Block incoming(int n) {
  254. return entrances[n];
  255. }
  256. /**
  257. * Return the number of the blocks that may be executed
  258. * after this block.
  259. */
  260. public int exits() { return exit == null ? 0 : exit.length; }
  261. /**
  262. * Returns the n-th block that may be executed after this
  263. * block.
  264. *
  265. * @param n an index in the array of exit blocks.
  266. */
  267. public Block exit(int n) { return (Block)exit[n]; }
  268. /**
  269. * Returns catch clauses that will catch an exception thrown
  270. * in this block.
  271. */
  272. public Catcher[] catchers() {
  273. ArrayList catchers = new ArrayList();
  274. BasicBlock.Catch c = toCatch;
  275. while (c != null) {
  276. catchers.add(new Catcher(c));
  277. c = c.next;
  278. }
  279. return (Catcher[])catchers.toArray(new Catcher[catchers.size()]);
  280. }
  281. }
  282. static abstract class Access {
  283. Node[] all;
  284. Access(Node[] nodes) { all = nodes; }
  285. Node node(BasicBlock b) { return all[((Block)b).index]; }
  286. abstract BasicBlock[] exits(Node n);
  287. abstract BasicBlock[] entrances(Node n);
  288. }
  289. /**
  290. * A node of (post) dominator trees.
  291. */
  292. public static class Node {
  293. private Block block;
  294. private Node parent;
  295. private Node[] children;
  296. Node(Block b) {
  297. block = b;
  298. parent = null;
  299. }
  300. /**
  301. * Returns a <code>String</code> representation.
  302. */
  303. public String toString() {
  304. StringBuffer sbuf = new StringBuffer();
  305. sbuf.append("Node[pos=").append(block().position());
  306. sbuf.append(", parent=");
  307. sbuf.append(parent == null ? "*" : Integer.toString(parent.block().position()));
  308. sbuf.append(", children{");
  309. for (int i = 0; i < children.length; i++)
  310. sbuf.append(children[i].block().position()).append(", ");
  311. sbuf.append("}]");
  312. return sbuf.toString();
  313. }
  314. /**
  315. * Returns the basic block indicated by this node.
  316. */
  317. public Block block() { return block; }
  318. /**
  319. * Returns the parent of this node.
  320. */
  321. public Node parent() { return parent; }
  322. /**
  323. * Returns the number of the children of this node.
  324. */
  325. public int children() { return children.length; }
  326. /**
  327. * Returns the n-th child of this node.
  328. *
  329. * @param n an index in the array of children.
  330. */
  331. public Node child(int n) { return children[n]; }
  332. /*
  333. * After executing this method, distance[] represents the post order of the tree nodes.
  334. * It also represents distances from the root; a bigger number represents a shorter
  335. * distance. parent is set to its parent in the depth first spanning tree.
  336. */
  337. int makeDepth1stTree(Node caller, boolean[] visited, int counter, int[] distance, Access access) {
  338. int index = block.index;
  339. if (visited[index])
  340. return counter;
  341. visited[index] = true;
  342. parent = caller;
  343. BasicBlock[] exits = access.exits(this);
  344. if (exits != null)
  345. for (int i = 0; i < exits.length; i++) {
  346. Node n = access.node(exits[i]);
  347. counter = n.makeDepth1stTree(this, visited, counter, distance, access);
  348. }
  349. distance[index] = counter++;
  350. return counter;
  351. }
  352. boolean makeDominatorTree(boolean[] visited, int[] distance, Access access) {
  353. int index = block.index;
  354. if (visited[index])
  355. return false;
  356. visited[index] = true;
  357. boolean changed = false;
  358. BasicBlock[] exits = access.exits(this);
  359. if (exits != null)
  360. for (int i = 0; i < exits.length; i++) {
  361. Node n = access.node(exits[i]);
  362. if (n.makeDominatorTree(visited, distance, access))
  363. changed = true;
  364. }
  365. BasicBlock[] entrances = access.entrances(this);
  366. if (entrances != null)
  367. for (int i = 0; i < entrances.length; i++) {
  368. if (parent != null) {
  369. Node n = getAncestor(parent, access.node(entrances[i]), distance);
  370. if (n != parent) {
  371. parent = n;
  372. changed = true;
  373. }
  374. }
  375. }
  376. return changed;
  377. }
  378. private static Node getAncestor(Node n1, Node n2, int[] distance) {
  379. while (n1 != n2) {
  380. if (distance[n1.block.index] < distance[n2.block.index])
  381. n1 = n1.parent;
  382. else
  383. n2 = n2.parent;
  384. if (n1 == null || n2 == null)
  385. return null;
  386. }
  387. return n1;
  388. }
  389. private static void setChildren(Node[] all) {
  390. int size = all.length;
  391. int[] nchildren = new int[size];
  392. for (int i = 0; i < size; i++)
  393. nchildren[i] = 0;
  394. for (int i = 0; i < size; i++) {
  395. Node p = all[i].parent;
  396. if (p != null)
  397. nchildren[p.block.index]++;
  398. }
  399. for (int i = 0; i < size; i++)
  400. all[i].children = new Node[nchildren[i]];
  401. for (int i = 0; i < size; i++)
  402. nchildren[i] = 0;
  403. for (int i = 0; i < size; i++) {
  404. Node n = all[i];
  405. Node p = n.parent;
  406. if (p != null)
  407. p.children[nchildren[p.block.index]++] = n;
  408. }
  409. }
  410. }
  411. /**
  412. * Represents a catch clause.
  413. */
  414. public static class Catcher {
  415. private Block node;
  416. private int typeIndex;
  417. Catcher(BasicBlock.Catch c) {
  418. node = (Block)c.body;
  419. typeIndex = c.typeIndex;
  420. }
  421. /**
  422. * Returns the first block of the catch clause.
  423. */
  424. public Block block() { return node; }
  425. /**
  426. * Returns the name of the exception type that
  427. * this catch clause catches.
  428. */
  429. public String type() {
  430. if (typeIndex == 0)
  431. return "java.lang.Throwable";
  432. else
  433. return node.method.getConstPool().getClassInfo(typeIndex);
  434. }
  435. }
  436. }