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.

SubroutineScanner.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.HashMap;
  18. import java.util.HashSet;
  19. import java.util.Map;
  20. import java.util.Set;
  21. import javassist.bytecode.BadBytecode;
  22. import javassist.bytecode.CodeAttribute;
  23. import javassist.bytecode.CodeIterator;
  24. import javassist.bytecode.ExceptionTable;
  25. import javassist.bytecode.MethodInfo;
  26. import javassist.bytecode.Opcode;
  27. /**
  28. * Discovers the subroutines in a method, and tracks all callers.
  29. *
  30. * @author Jason T. Greene
  31. */
  32. public class SubroutineScanner implements Opcode {
  33. private Subroutine[] subroutines;
  34. Map subTable = new HashMap();
  35. Set done = new HashSet();
  36. public Subroutine[] scan(MethodInfo method) throws BadBytecode {
  37. CodeAttribute code = method.getCodeAttribute();
  38. CodeIterator iter = code.iterator();
  39. subroutines = new Subroutine[code.getCodeLength()];
  40. subTable.clear();
  41. done.clear();
  42. scan(0, iter, null);
  43. ExceptionTable exceptions = code.getExceptionTable();
  44. for (int i = 0; i < exceptions.size(); i++) {
  45. int handler = exceptions.handlerPc(i);
  46. // If an exception is thrown in subroutine, the handler
  47. // is part of the same subroutine.
  48. scan(handler, iter, subroutines[exceptions.startPc(i)]);
  49. }
  50. return subroutines;
  51. }
  52. private void scan(int pos, CodeIterator iter, Subroutine sub) throws BadBytecode {
  53. // Skip already processed blocks
  54. if (done.contains(new Integer(pos)))
  55. return;
  56. done.add(new Integer(pos));
  57. int old = iter.lookAhead();
  58. iter.move(pos);
  59. boolean next;
  60. do {
  61. pos = iter.next();
  62. next = scanOp(pos, iter, sub) && iter.hasNext();
  63. } while (next);
  64. iter.move(old);
  65. }
  66. private boolean scanOp(int pos, CodeIterator iter, Subroutine sub) throws BadBytecode {
  67. subroutines[pos] = sub;
  68. int opcode = iter.byteAt(pos);
  69. if (opcode == TABLESWITCH) {
  70. scanTableSwitch(pos, iter, sub);
  71. return false;
  72. }
  73. if (opcode == LOOKUPSWITCH) {
  74. scanLookupSwitch(pos, iter, sub);
  75. return false;
  76. }
  77. // All forms of return and throw end current code flow
  78. if (Util.isReturn(opcode) || opcode == RET || opcode == ATHROW)
  79. return false;
  80. if (Util.isJumpInstruction(opcode)) {
  81. int target = Util.getJumpTarget(pos, iter);
  82. if (opcode == JSR || opcode == JSR_W) {
  83. Subroutine s = (Subroutine) subTable.get(new Integer(target));
  84. if (s == null) {
  85. s = new Subroutine(target, pos);
  86. subTable.put(new Integer(target), s);
  87. scan(target, iter, s);
  88. } else {
  89. s.addCaller(pos);
  90. }
  91. } else {
  92. scan(target, iter, sub);
  93. // GOTO ends current code flow
  94. if (Util.isGoto(opcode))
  95. return false;
  96. }
  97. }
  98. return true;
  99. }
  100. private void scanLookupSwitch(int pos, CodeIterator iter, Subroutine sub) throws BadBytecode {
  101. int index = (pos & ~3) + 4;
  102. // default
  103. scan(pos + iter.s32bitAt(index), iter, sub);
  104. int npairs = iter.s32bitAt(index += 4);
  105. int end = npairs * 8 + (index += 4);
  106. // skip "match"
  107. for (index += 4; index < end; index += 8) {
  108. int target = iter.s32bitAt(index) + pos;
  109. scan(target, iter, sub);
  110. }
  111. }
  112. private void scanTableSwitch(int pos, CodeIterator iter, Subroutine sub) throws BadBytecode {
  113. // Skip 4 byte alignment padding
  114. int index = (pos & ~3) + 4;
  115. // default
  116. scan(pos + iter.s32bitAt(index), iter, sub);
  117. int low = iter.s32bitAt(index += 4);
  118. int high = iter.s32bitAt(index += 4);
  119. int end = (high - low + 1) * 4 + (index += 4);
  120. // Offset table
  121. for (; index < end; index += 4) {
  122. int target = iter.s32bitAt(index) + pos;
  123. scan(target, iter, sub);
  124. }
  125. }
  126. }