@Override public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg, RepositoryLookupFailureCallback lookupFailureCallback) { Instruction ins = location.getHandle().getInstruction(); try { if (ins instanceof InvokeInstruction) { if (!Hierarchy.isSubtype(type, baseClassType)) { return null; } Stream stream = new Stream(location, type.getClassName(), baseClassType.getClassName()).setIsOpenOnCreation(true) .setIgnoreImplicitExceptions(true); if (bugType != null) { stream.setInteresting(bugType); } return stream; } } catch (ClassNotFoundException e) { lookupFailureCallback.reportMissingClass(e); } return null; } }
@Override public int compareTo(StreamEscape other) { int cmp = source.compareTo(other.source); if (cmp != 0) { return cmp; } return target.compareTo(other.target); }
@Override public boolean equals(Object o) { if (!(o instanceof StreamEscape)) { return false; } StreamEscape other = (StreamEscape) o; return source.equals(other.source) && target.equals(other.target); }
@Override public boolean equals(Object o) { if (!(o instanceof Stream)) { return false; } Stream other = (Stream) o; if (!getLocation().equals(other.getLocation())) { return false; } if (!streamBase.equals(other.streamBase)) { return false; } if (!getResourceClass().equals(other.getResourceClass())) { return false; } if (instanceParam != other.instanceParam) { return false; } return true; }
public boolean isStreamOpen(BasicBlock basicBlock, InstructionHandle handle, ConstantPoolGen cpg, ResourceValueFrame frame) { if (isOpenOnCreation) { return false; } Instruction ins = handle.getInstruction(); if (!(ins instanceof INVOKESPECIAL)) { return false; } // Does this instruction open the stream? INVOKESPECIAL inv = (INVOKESPECIAL) ins; return frame.isValid() && getInstanceValue(frame, inv, cpg).isInstance() && matchMethod(inv, cpg, this.getResourceClass(), Const.CONSTRUCTOR_NAME); }
public boolean isStreamClose(BasicBlock basicBlock, InstructionHandle handle, ConstantPoolGen cpg, ResourceValueFrame frame, RepositoryLookupFailureCallback lookupFailureCallback) { if (!mightCloseStream(basicBlock, handle, cpg)) { return false; if (!frame.isValid() || !getInstanceValue(frame, inv, cpg).isInstance()) { return false; String classClosed = inv.getClassName(cpg); if (relatedType(classClosed) ) { return true; return relatedType("java.io.ObjectOutputStream"); } else if ("java.io.ObjectInput".equals(classClosed)) { return relatedType("java.io.ObjectInputStream");
Stream paramStream = new Stream(firstLocation, objectType.getClassName(), streamBase.getClassName()); paramStream.setIsOpenOnCreation(true); paramStream.setOpenLocation(firstLocation); paramStream.setInstanceParam(local); resourceCollection.addPreexistingResource(paramStream); Stream stream = i.next(); StreamEquivalenceClass equivalenceClass = resourceTracker.getStreamEquivalenceClass(stream); if (stream.isClosed()) { equivalenceClass.setClosed(); if (stream.isClosed()) { if (stream.isUninteresting()) { continue; Location openLocation = stream.getOpenLocation(); if (openLocation == null) { continue; String leakClass = stream.getStreamBase(); if (isMainMethod(method) && (leakClass.contains("InputStream") || leakClass.contains("Reader"))) { return; .addClassAndMethod(methodGen, sourceFile).addTypeOfNamedClass(leakClass) .describe(TypeAnnotation.CLOSEIT_ROLE), SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen, sourceFile, stream.getLocation().getHandle()));
Stream stream = new Stream(location, fieldClass, streamBaseClass); stream.setIsOpenOnCreation(true); stream.setOpenLocation(location); if (bugPatternType != null) { stream.setInteresting(bugPatternType);
@Override public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg, RepositoryLookupFailureCallback lookupFailureCallback) { Instruction ins = location.getHandle().getInstruction(); if (ins.getOpcode() != Const.GETSTATIC) { return null; } GETSTATIC getstatic = (GETSTATIC) ins; if (!className.equals(getstatic.getClassName(cpg)) || !fieldName.equals(getstatic.getName(cpg)) || !fieldSig.equals(getstatic.getSignature(cpg))) { return null; } return new Stream(location, type.getClassName(), streamBaseClass).setIgnoreImplicitExceptions(true).setIsOpenOnCreation( true); } }
Stream result = new Stream(location, type.getClassName(), baseClassType.getClassName()) .setIgnoreImplicitExceptions(true); if (!isUninteresting) { result.setInteresting(bugType);
Location creationPoint = stream.getLocation(); if (handle == creationPoint.getHandle() && basicBlock == creationPoint.getBasicBlock()) { if (stream.isOpenOnCreation()) { status = ResourceValueFrame.OPEN; stream.setOpenLocation(location); resourceTracker.addStreamOpenLocation(location, stream); } else { stream.setOpenLocation(location); resourceTracker.addStreamOpenLocation(location, stream); } else if (resourceTracker.isResourceClose(basicBlock, handle, cpg, stream, frame)) {
@Override public void inspectResult(ClassContext classContext, MethodGen methodGen, CFG cfg, Dataflow<ResourceValueFrame, ResourceValueAnalysis<Stream>> dataflow, Stream stream) { if (DEBUG) { System.out.printf("Result for %s in %s%n", stream, methodGen); dataflow.dumpDataflow(dataflow.getAnalysis()); } ResourceValueFrame exitFrame = dataflow.getResultFact(cfg.getExit()); int exitStatus = exitFrame.getStatus(); if (exitStatus == ResourceValueFrame.OPEN || exitStatus == ResourceValueFrame.OPEN_ON_EXCEPTION_PATH) { // FIXME: Stream object should be queried for the // priority. String bugType = stream.getBugType(); int priority = NORMAL_PRIORITY; if (exitStatus == ResourceValueFrame.OPEN_ON_EXCEPTION_PATH) { bugType += "_EXCEPTION_PATH"; priority = LOW_PRIORITY; } potentialOpenStreamList.add(new PotentialOpenStream(bugType, priority, stream)); } else if (exitStatus == ResourceValueFrame.CLOSED) { // Remember that this stream was closed on all paths. // Later, we will mark all of the streams in its equivalence class // as having been closed. stream.setClosed(); } }
@Override public boolean isParamInstance(Stream resource, int slot) { return resource.getInstanceParam() == slot; }
if (stream.getOpenLocation() != null) { resourceTracker.addStreamEscape(stream, location);
Stream paramStream = new Stream(firstLocation, objectType.getClassName(), streamBase.getClassName()); paramStream.setIsOpenOnCreation(true); paramStream.setOpenLocation(firstLocation); paramStream.setInstanceParam(local); resourceCollection.addPreexistingResource(paramStream); Stream stream = i.next(); StreamEquivalenceClass equivalenceClass = resourceTracker.getStreamEquivalenceClass(stream); if (stream.isClosed()) { equivalenceClass.setClosed(); if (stream.isClosed()) { if (stream.isUninteresting()) { continue; Location openLocation = stream.getOpenLocation(); if (openLocation == null) { continue; String leakClass = stream.getStreamBase(); if (isMainMethod(method) && (leakClass.contains("InputStream") || leakClass.contains("Reader"))) { return; .addClassAndMethod(methodGen, sourceFile).addTypeOfNamedClass(leakClass) .describe(TypeAnnotation.CLOSEIT_ROLE), SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen, sourceFile, stream.getLocation().getHandle()));
Stream stream = new Stream(location, fieldClass, streamBaseClass); stream.setIsOpenOnCreation(true); stream.setOpenLocation(location); if (bugPatternType != null) { stream.setInteresting(bugPatternType);
@Override public int hashCode() { return getLocation().hashCode() + 3 * streamBase.hashCode() + 7 * getResourceClass().hashCode() + 11 * instanceParam; }
public boolean isStreamOpen(BasicBlock basicBlock, InstructionHandle handle, ConstantPoolGen cpg, ResourceValueFrame frame) { if (isOpenOnCreation) { return false; } Instruction ins = handle.getInstruction(); if (!(ins instanceof INVOKESPECIAL)) { return false; } // Does this instruction open the stream? INVOKESPECIAL inv = (INVOKESPECIAL) ins; return frame.isValid() && getInstanceValue(frame, inv, cpg).isInstance() && matchMethod(inv, cpg, this.getResourceClass(), "<init>"); }
@Override public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg, RepositoryLookupFailureCallback lookupFailureCallback) { Instruction ins = location.getHandle().getInstruction(); if (ins.getOpcode() != Constants.GETSTATIC) { return null; } GETSTATIC getstatic = (GETSTATIC) ins; if (!className.equals(getstatic.getClassName(cpg)) || !fieldName.equals(getstatic.getName(cpg)) || !fieldSig.equals(getstatic.getSignature(cpg))) { return null; } return new Stream(location, type.getClassName(), streamBaseClass).setIgnoreImplicitExceptions(true).setIsOpenOnCreation( true); } }
Stream result = new Stream(location, type.getClassName(), baseClassType.getClassName()) .setIgnoreImplicitExceptions(true); if (!isUninteresting) { result.setInteresting(bugType);