[Fb-contrib-commit] fb-contrib/src/com/mebigfatguy/fbcontrib/detect BloatedSynchronizedBlock.java,1.
Brought to you by:
dbrosius
|
From: Dave B. <dbr...@us...> - 2006-01-02 14:57:20
|
Update of /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13635/src/com/mebigfatguy/fbcontrib/detect Modified Files: BloatedSynchronizedBlock.java Log Message: any load on the object that is the synchronizing object, is unsafe Index: BloatedSynchronizedBlock.java =================================================================== RCS file: /cvsroot/fb-contrib/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/BloatedSynchronizedBlock.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- BloatedSynchronizedBlock.java 2 Jan 2006 04:23:21 -0000 1.3 +++ BloatedSynchronizedBlock.java 2 Jan 2006 14:57:11 -0000 1.4 @@ -23,14 +23,17 @@ import edu.umd.cs.findbugs.BugInstance; import edu.umd.cs.findbugs.BugReporter; import edu.umd.cs.findbugs.BytecodeScanningDetector; +import edu.umd.cs.findbugs.OpcodeStack; import edu.umd.cs.findbugs.StatelessDetector; public class BloatedSynchronizedBlock extends BytecodeScanningDetector implements StatelessDetector { private BugReporter bugReporter; private static final String BSB_MIN_SAFE_CODE_SIZE = "fb-contrib.bsb.minsize"; + private OpcodeStack stack = new OpcodeStack(); private int syncPC; private int lastPC; + private int monitorReg; private boolean isStatic; private int minSafeCodeLength; @@ -65,7 +68,9 @@ else syncPC = -1; lastPC = -1; + monitorReg = -1; isStatic = obj.isStatic(); + stack.resetForMethodEntry(this); } /** @@ -75,25 +80,50 @@ * @param seen the opcode of the currently parsed instruction */ public void sawOpcode(int seen) { - if (seen == MONITORENTER) { - if (syncPC < 0) - syncPC = getPC(); - } - else if (seen == MONITOREXIT) - syncPC = -1; - else if (syncPC >= 0) { - boolean unsafe = ((seen == PUTFIELD) || (seen == GETFIELD)); - unsafe |= (!isStatic) && ((seen == ALOAD_0) || (seen == ASTORE_0)); - if (unsafe) { - if ((getPC() - syncPC) > minSafeCodeLength) { - bugReporter.reportBug(new BugInstance(this, "BSB_BLOATED_SYNCHRONIZED_BLOCK", NORMAL_PRIORITY) - .addClass(this) - .addMethod(this) - .addSourceLineRange(this, syncPC + 1, lastPC)); + try { + if (seen == MONITORENTER) { + if (syncPC < 0) { + syncPC = getPC(); + if (stack.getStackDepth() > 0) { + OpcodeStack.Item itm = stack.getStackItem(0); + monitorReg = itm.getRegisterNumber(); + } } + } + else if (seen == MONITOREXIT) syncPC = -1; + else if (syncPC >= 0) { + boolean unsafe = ((seen == PUTFIELD) || (seen == GETFIELD)); + unsafe |= (!isStatic) && ((seen == ALOAD_0) || (seen == ASTORE_0)); + unsafe |= (monitorReg >= 0) && (aloadReg(seen) == monitorReg); + if (unsafe) { + if ((getPC() - syncPC) > minSafeCodeLength) { + bugReporter.reportBug(new BugInstance(this, "BSB_BLOATED_SYNCHRONIZED_BLOCK", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLineRange(this, syncPC + 1, lastPC)); + } + syncPC = -1; + } } + lastPC = getPC(); + } finally { + stack.sawOpcode(this, seen); } - lastPC = getPC(); + } + + /** + * return the register number used in this ALOAD method, if it is an aload instruction + * + * @param seen the currently visited opcode + * + * @return the register used if it's an aload instruction, or -1 otherwise + */ + public int aloadReg(int seen) { + if (seen == ALOAD) + return getRegisterOperand(); + if ((seen >= ALOAD_0) && (seen <= ALOAD_3)) + return seen - ALOAD_0; + return -1; } } |