1 19 20 package edu.umd.cs.findbugs.detect; 21 22 23 import edu.umd.cs.findbugs.ba.*; 24 import org.apache.bcel.Constants; 25 import org.apache.bcel.Repository; 26 import org.apache.bcel.generic.*; 27 28 32 public class StreamFrameModelingVisitor extends ResourceValueFrameModelingVisitor { 33 private StreamResourceTracker resourceTracker; 34 private Stream stream; 35 private Location location; 36 37 public StreamFrameModelingVisitor(ConstantPoolGen cpg, StreamResourceTracker resourceTracker, 38 Stream stream) { 39 super(cpg); 40 this.resourceTracker = resourceTracker; 41 this.stream = stream; 42 } 43 44 @Override 45 public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock) throws DataflowAnalysisException { 46 this.location = new Location(handle, basicBlock); 48 49 final Instruction ins = handle.getInstruction(); 50 final ConstantPoolGen cpg = getCPG(); 51 final ResourceValueFrame frame = getFrame(); 52 53 int status = -1; 54 boolean created = false; 55 56 Location creationPoint = stream.getLocation(); 58 if (handle == creationPoint.getHandle() && basicBlock == creationPoint.getBasicBlock()) { 59 if (stream.isOpenOnCreation()) { 61 status = ResourceValueFrame.OPEN; 62 stream.setOpenLocation(location); 63 resourceTracker.addStreamOpenLocation(location, stream); 64 } else { 65 status = ResourceValueFrame.CREATED; 66 } 67 created = true; 68 } else if (resourceTracker.isResourceOpen(basicBlock, handle, cpg, stream, frame)) { 69 status = ResourceValueFrame.OPEN; 71 stream.setOpenLocation(location); 72 resourceTracker.addStreamOpenLocation(location, stream); 73 } else if (resourceTracker.isResourceClose(basicBlock, handle, cpg, stream, frame)) { 74 status = ResourceValueFrame.CLOSED; 76 } 77 78 analyzeInstruction(ins); 80 81 if (status != -1) { 83 frame.setStatus(status); 84 if (created) 85 frame.setValue(frame.getNumSlots() - 1, ResourceValue.instance()); 86 } 87 88 } 89 90 @Override 91 protected boolean instanceEscapes(InvokeInstruction inv, int instanceArgNum) { 92 ConstantPoolGen cpg = getCPG(); 93 String className = inv.getClassName(cpg); 94 95 97 boolean escapes = (inv.getOpcode() == Constants.INVOKESTATIC || instanceArgNum != 0); 98 String methodName = inv.getMethodName(cpg); 99 String methodSig = inv.getSignature(cpg); 100 if (inv.getOpcode() == Constants.INVOKEVIRTUAL 101 && (methodName.equals("load") || methodName.equals("loadFromXml") || methodName.equals("store") 102 || methodName.equals("save")) 103 && className.equals("java.util.Properties")) 104 escapes = false; 105 if (inv.getOpcode() == Constants.INVOKEVIRTUAL 106 && (methodName.equals("load") || methodName.equals("store")) 107 && className.equals("java.security.KeyStore")) 108 escapes = false; 109 if (inv.getOpcode() == Constants.INVOKEVIRTUAL 110 && "getChannel".equals(methodName) 111 && "()Ljava/nio/channels/FileChannel;".equals(methodSig)) 112 escapes = true; 113 114 if (FindOpenStream.DEBUG && escapes) { 115 System.out.println("ESCAPE at " + location + " at call to " + className +"." + methodName +":" + methodSig); 116 } 117 118 if (stream.getOpenLocation() != null) 120 resourceTracker.addStreamEscape(stream, location); 121 122 return escapes; 123 } 124 } 125 126 | Popular Tags |