Package edu.umd.cs.findbugs.detect
Class ConstructorThrow
- java.lang.Object
-
- All Implemented Interfaces:
Detector,Priorities,org.apache.bcel.classfile.Visitor
public class ConstructorThrow extends OpcodeStackDetector
This detector can find constructors that throw exception.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class edu.umd.cs.findbugs.bcel.OpcodeStackDetector
OpcodeStackDetector.WithCustomJumpInfo
-
-
Field Summary
Fields Modifier and Type Field Description private BugAccumulatorbugAccumulatorprivate java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Set<java.lang.String>>>exHandlesToMethodCallsByMethodsMapThe containing methods (DottedClassName complete with signature) to the methods called directly from the containing one to the caught Exceptions by the surrounding try-catches of the call sites.private booleanhadObjectConstructorprivate booleanisFinalClassprivate booleanisFinalFinalizerprivate booleanisFirstPassprivate java.util.Map<java.lang.String,java.util.Set<org.apache.bcel.classfile.JavaClass>>thrownExsByMethodMapThe DottedClassName complete with signature of the method to the set of the Exceptions thrown directly from the method.-
Fields inherited from class edu.umd.cs.findbugs.bcel.OpcodeStackDetector
stack
-
Fields inherited from class edu.umd.cs.findbugs.visitclass.DismantleBytecode
codeBytes, lineNumberTable, M_BR, M_CP, M_INT, M_PAD, M_R, M_UINT
-
Fields inherited from interface edu.umd.cs.findbugs.Priorities
EXP_PRIORITY, HIGH_PRIORITY, IGNORE_PRIORITY, LOW_PRIORITY, NORMAL_PRIORITY
-
-
Constructor Summary
Constructors Constructor Description ConstructorThrow(BugReporter bugReporter)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaccumulateBug()private voidaddToExHandlesToMethodCallsByMethodsMap(java.lang.String containerMethod, java.lang.String calledMethod, java.util.Collection<java.lang.String> caughtExes)private voidaddToThrownExsByMethodMap(java.lang.String containingMethod, org.apache.bcel.classfile.JavaClass thrownExClass)private voidcollectExceptionsByMethods(int seen)Fills the inner collections while visiting the method.private java.lang.StringgetCalledMethodFQN()Gives back the fully qualified name (DottedClassName) of the called method complete with the signature.private java.util.Set<java.lang.String>getSurroundingCaughtExes(org.apache.bcel.classfile.ConstantPool cp)Gets the DottedClassNames of the Exceptions which are caught by a try-catch block at the current PC.private java.util.Set<org.apache.bcel.classfile.JavaClass>getUnhandledExThrowsInMethod(java.lang.String method, java.util.Set<java.lang.String> visitedMethods)Get the Exceptions thrown from the inside of the method, either directly or indirectly from called methods.private static booleanhasFinalFinalizer(org.apache.bcel.classfile.JavaClass jc)private static booleanisHandled(org.apache.bcel.classfile.JavaClass thrownEx, java.lang.String caughtEx)Checks if the thrown Exception is handled by the caught Exception.private booleanisHandled(org.apache.bcel.classfile.JavaClass thrownEx, java.util.Set<java.lang.String> exHandles)Checks whether the Exception is handled in all call sites.private static booleanisThrownExNotCaught(org.apache.bcel.classfile.JavaClass thrownEx, java.util.Set<java.lang.String> caughtExes)Checks if the thrown exception is not caught.private voidreportConstructorThrow(int seen)Reports ConstructorThrow bug if there is an unhandled unchecked exception thrown directly or indirectly from the currently visited method.private voidresetState()voidsawOpcode(int seen)1.private static java.lang.StringtoDotted(java.lang.String signature)voidvisit(org.apache.bcel.classfile.JavaClass obj)Visit a class to find the constructor, then collect all the methods that gets called in it.voidvisit(org.apache.bcel.classfile.Method obj)voidvisitAfter(org.apache.bcel.classfile.JavaClass obj)-
Methods inherited from class edu.umd.cs.findbugs.bcel.OpcodeStackDetector
afterOpcode, beforeOpcode, getStack, isUsingCustomUserValue, visitCode
-
Methods inherited from class edu.umd.cs.findbugs.BytecodeScanningDetector
getClassContext, report, shouldVisitCode, visitClassContext
-
Methods inherited from class edu.umd.cs.findbugs.visitclass.DismantleBytecode
areOppositeBranches, atCatchBlock, getBranchFallThrough, getBranchOffset, getBranchTarget, getClassConstantOperand, getClassDescriptorOperand, getCodeByte, getConstantRefOperand, getDefaultSwitchOffset, getDottedClassConstantOperand, getFieldDescriptorOperand, getIntConstant, getLongConstant, getMaxPC, getMethodDescriptorOperand, getNameConstantOperand, getNextCodeByte, getNextOpcode, getNextPC, getOpcode, getPC, getPrevOpcode, getRefConstantOperand, getRefFieldIsStatic, getRegisterOperand, getSigConstantOperand, getStringConstantOperand, getSwitchLabels, getSwitchOffsets, getXClassOperand, getXFieldOperand, getXMethodOperand, isBranch, isMethodCall, isRegisterLoad, isRegisterStore, isRegisterStore, isReturn, isShift, isSwitch, isWideOpcode, printOpCode, sawBranchTo, sawClass, sawDouble, sawField, sawFloat, sawIMethod, sawInt, sawLong, sawMethod, sawRegister, sawString, visit
-
Methods inherited from class edu.umd.cs.findbugs.visitclass.AnnotationVisitor
getAnnotationParameterAsEnum, getAnnotationParameterAsString, getAnnotationParameterAsStringArray, visitAnnotation, visitAnnotation, visitParameterAnnotation, visitParameterAnnotation, visitSyntheticParameterAnnotation
-
Methods inherited from class edu.umd.cs.findbugs.visitclass.PreorderVisitor
amVisitingMainMethod, asUnsignedByte, doVisitMethod, getClassDescriptor, getClassName, getCode, getConstantPool, getDottedClassName, getDottedFieldSig, getDottedMethodSig, getDottedSuperclassName, getField, getFieldDescriptor, getFieldIsStatic, getFieldName, getFieldSig, getFullyQualifiedFieldName, getFullyQualifiedMethodName, getMethod, getMethodDescriptor, getMethodName, getMethodSig, getMethodVisitOrder, getNumberArguments, getNumberMethodArguments, getPackageName, getSizeOfSurroundingTryBlock, getSizeOfSurroundingTryBlock, getSourceFile, getStringFromIndex, getSuperclassName, getSurroundingCaughtExceptions, getSurroundingCaughtExceptions, getSurroundingCaughtExceptionTypes, getSurroundingTryBlock, getSurroundingTryBlock, getThisClass, getXClass, getXField, getXMethod, hasInterestingClass, hasInterestingMethod, isVisitMethodsInCallOrder, setupVisitorForClass, setVisitMethodsInCallOrder, shouldVisit, toString, visitAfter, visitAnnotationDefault, visitAnnotationEntry, visitBootstrapMethods, visitConstantInvokeDynamic, visitConstantMethodHandle, visitConstantMethodType, visitConstantModule, visitConstantPackage, visitConstantPool, visitEnclosingMethod, visitingField, visitingMethod, visitInnerClasses, visitJavaClass, visitLineNumberTable, visitLocalVariableTable, visitMethodParameters, visitParameterAnnotationEntry, visitStackMap, visitStackMapEntry
-
Methods inherited from class edu.umd.cs.findbugs.visitclass.BetterVisitor
clone, report, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visitCodeException, visitConstantClass, visitConstantDouble, visitConstantFieldref, visitConstantFloat, visitConstantInteger, visitConstantInterfaceMethodref, visitConstantLong, visitConstantMethodref, visitConstantNameAndType, visitConstantString, visitConstantUtf8, visitConstantValue, visitDeprecated, visitExceptionTable, visitField, visitInnerClass, visitLineNumber, visitLocalVariable, visitLocalVariableTypeTable, visitMethod, visitSignature, visitSourceFile, visitSynthetic, visitUnknown
-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.bcel.classfile.Visitor
visitConstantDynamic, visitMethodParameter, visitModule, visitModuleExports, visitModuleMainClass, visitModuleOpens, visitModulePackages, visitModuleProvides, visitModuleRequires, visitNestHost, visitNestMembers, visitRecord, visitRecordComponent, visitStackMapType
-
-
-
-
Field Detail
-
bugAccumulator
private final BugAccumulator bugAccumulator
-
exHandlesToMethodCallsByMethodsMap
private final java.util.Map<java.lang.String,java.util.Map<java.lang.String,java.util.Set<java.lang.String>>> exHandlesToMethodCallsByMethodsMap
The containing methods (DottedClassName complete with signature) to the methods called directly from the containing one to the caught Exceptions by the surrounding try-catches of the call sites. If the call site is not inside a try-catch then an empty string.
-
thrownExsByMethodMap
private final java.util.Map<java.lang.String,java.util.Set<org.apache.bcel.classfile.JavaClass>> thrownExsByMethodMap
The DottedClassName complete with signature of the method to the set of the Exceptions thrown directly from the method.
-
isFinalClass
private boolean isFinalClass
-
isFinalFinalizer
private boolean isFinalFinalizer
-
isFirstPass
private boolean isFirstPass
-
hadObjectConstructor
private boolean hadObjectConstructor
-
-
Constructor Detail
-
ConstructorThrow
public ConstructorThrow(BugReporter bugReporter)
-
-
Method Detail
-
visit
public void visit(org.apache.bcel.classfile.JavaClass obj)
Visit a class to find the constructor, then collect all the methods that gets called in it. Also, we are checking for final declaration on the class, or a final finalizer, as if present no finalizer attack can happen.- Overrides:
visitin classBetterVisitor
-
hasFinalFinalizer
private static boolean hasFinalFinalizer(org.apache.bcel.classfile.JavaClass jc)
-
visit
public void visit(org.apache.bcel.classfile.Method obj)
- Overrides:
visitin classBetterVisitor
-
visitAfter
public void visitAfter(org.apache.bcel.classfile.JavaClass obj)
- Overrides:
visitAfterin classPreorderVisitor
-
sawOpcode
public void sawOpcode(int seen)
1. Check for any throw expression in the constructor. 2. Check for any exception throw inside constructor, or any of the called methods. If the class is final, we are fine, no finalizer attack can happen. In the first pass the detector shouldn't report, because there could be a final finalizer and a throwing constructor. Reporting in this case would be a false positive as classes with a final finalizer are not vulnerable to the finalizer attack.- Specified by:
sawOpcodein classOpcodeStackDetector- See Also:
OpcodeStackDetector.beforeOpcode(int)
-
reportConstructorThrow
private void reportConstructorThrow(int seen)
Reports ConstructorThrow bug if there is an unhandled unchecked exception thrown directly or indirectly from the currently visited method. If the exception is thrown directly, the bug is reported at the throw. If the exception is thrown indirectly (through a method call), the bug is reported at the call of the method which throws the exception.
-
getUnhandledExThrowsInMethod
private java.util.Set<org.apache.bcel.classfile.JavaClass> getUnhandledExThrowsInMethod(java.lang.String method, java.util.Set<java.lang.String> visitedMethods)Get the Exceptions thrown from the inside of the method, either directly or indirectly from called methods. Uses inner collections which are needed to filled correctly.- Parameters:
method- the method to visit and get the exceptions thrown out of itvisitedMethods- the names of the already visited methods, needed to prevent stackoverflow by recursively checking method call cycles- Returns:
- the JavaClasses of the Exceptions thrown from the method
-
isHandled
private boolean isHandled(org.apache.bcel.classfile.JavaClass thrownEx, java.util.Set<java.lang.String> exHandles)Checks whether the Exception is handled in all call sites.- Parameters:
thrownEx- the thrown Exception which needs to be handledexHandles- the set of the dotted class names of the caught Exceptions in the call sites.- Returns:
- true if the Exception handled in all call sites.
-
isHandled
private static boolean isHandled(org.apache.bcel.classfile.JavaClass thrownEx, @NonNull @DottedClassName java.lang.String caughtEx)Checks if the thrown Exception is handled by the caught Exception.- Parameters:
thrownEx- the thrown Exception which needs to be handledcaughtEx- the name of the caught Exception at the call site. If no Exception is caught, then it's an empty string or other nonnull string which is not a name of any Exception.- Returns:
- true if the Exception is handled.
-
getSurroundingCaughtExes
private java.util.Set<java.lang.String> getSurroundingCaughtExes(org.apache.bcel.classfile.ConstantPool cp)
Gets the DottedClassNames of the Exceptions which are caught by a try-catch block at the current PC.- Parameters:
cp- ConstantPool- Returns:
- Set of the DottedClassNames of the caught Exceptions.
-
isThrownExNotCaught
private static boolean isThrownExNotCaught(org.apache.bcel.classfile.JavaClass thrownEx, java.util.Set<java.lang.String> caughtExes)Checks if the thrown exception is not caught.- Parameters:
thrownEx- the Exception to catch.caughtExes- the set of the DottedClassNames of the caught Exceptions at call site.- Returns:
- true if the exception is not caught.
-
toDotted
private static java.lang.String toDotted(java.lang.String signature)
-
collectExceptionsByMethods
private void collectExceptionsByMethods(int seen)
Fills the inner collections while visiting the method.- Parameters:
seen- the opcode @see #sawOpcode(int)
-
addToExHandlesToMethodCallsByMethodsMap
private void addToExHandlesToMethodCallsByMethodsMap(java.lang.String containerMethod, java.lang.String calledMethod, java.util.Collection<java.lang.String> caughtExes)
-
addToThrownExsByMethodMap
private void addToThrownExsByMethodMap(java.lang.String containingMethod, org.apache.bcel.classfile.JavaClass thrownExClass)
-
getCalledMethodFQN
private java.lang.String getCalledMethodFQN()
Gives back the fully qualified name (DottedClassName) of the called method complete with the signature. Needs to be called from method call opcode. This is in sync withPreorderVisitor.getFullyQualifiedMethodName()function.- Returns:
- the fully qualified name of the method (dotted) with the signature.
-
resetState
private void resetState()
-
accumulateBug
private void accumulateBug()
-
-