@Override public boolean prescreen(Method method, ClassContext classContext) { if (Const.STATIC_INITIALIZER_NAME.equals(method.getName())) { return false; } Code code = method.getCode(); if (code.getCode().length > 5000) { return false; } BitSet bytecodeSet = classContext.getBytecodeSet(method); if (bytecodeSet == null) { return false; } // The pattern requires a get/put pair accessing the same field. boolean hasGetStatic = bytecodeSet.get(Const.GETSTATIC); boolean hasPutStatic = bytecodeSet.get(Const.PUTSTATIC); if (!hasGetStatic || !hasPutStatic) { return false; } // If the method is synchronized, then we'll assume that // things are properly synchronized if (method.isSynchronized()) { return false; } reported.clear(); return true; }
public static Method findImplementation(JavaClass clazz, String name, String signature) { Method[] m = clazz.getMethods(); for (Method aM : m) { if (aM.getName().equals(name) && aM.getSignature().equals(signature) && !aM.isPrivate() && !aM.isStatic()) { return aM; } } return null; } }
@Override public void visit(Method method) { if(method.isPublic() && "next".equals(method.getName()) && method.getArgumentTypes().length == 0){ shouldVisitCode = true; super.visit(method); } else { shouldVisitCode = false; } }
/** * Methods marked with the "Synthetic" attribute do not appear in the source * code */ private boolean isSynthetic(Method m) { if ((m.getAccessFlags() & Const.ACC_SYNTHETIC) != 0) { return true; } Attribute[] attrs = m.getAttributes(); for (Attribute attr : attrs) { if (attr instanceof Synthetic) { return true; } } return false; }
/** is there a JUnit3TestSuite */ private boolean hasSuite(Method[] methods) { for (Method m : methods) { if (m.getName().equals("suite") && m.isPublic() && m.isStatic() // && m.getReturnType().equals(junit.framework.Test.class) // && m.getArgumentTypes().length == 0 && m.getSignature().equals("()Ljunit/framework/Test;")) { return true; } } return false; }
AnalysisContext.logError("Error getting jump information", e); BitSet lineMentionedMultipleTimes = new BitSet(); BitSet pcInFinallyBlock = new BitSet(); Code code = method.getCode(); if (code == null) { return lineMentionedMultipleTimes; int end = jumpInfo.getNextJump(pc + 1); if (end >= pc) { pcInFinallyBlock.set(pc, end); BitSet foundOnce = new BitSet(); BitSet afterHandler = new BitSet(); LineNumberTable lineNumberTable = method.getLineNumberTable(); int lineNum = -1; int prevStartPc = -1;
IsNullValueDataflow nullValueDataflow = classContext.getIsNullValueDataflow(method); MethodGen methodGen = classContext.getMethodGen(method); String sourceFile = classContext.getJavaClass().getSourceFileName(); if (lineMentionedMultipleTimes.cardinality() > 0) { linesWithLoadsOfNotDefinitelyNullValues = new BitSet(); LineNumberTable lineNumbers = method.getLineNumberTable(); for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) { Location location = i.next(); Instruction ins = handle.getInstruction(); if (!(ins instanceof ALOAD)) { continue; SourceLineAnnotation sourceLineAnnotation = SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen, sourceFile, handle); SourceLineAnnotation prevSourceLineAnnotation = SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen, sourceFile, prevHandle); int startLine = sourceLineAnnotation.getStartLine(); new BugInstance(this, "NP_LOAD_OF_KNOWN_NULL_VALUE", priority).addClassAndMethod(methodGen, sourceFile) .addOptionalAnnotation(variableAnnotation), sourceLineAnnotation);
private void analyzeMethod(ClassContext classContext, Method method) throws DataflowAnalysisException, CFGBuilderException { if (BCELUtil.isSynthetic(method) || (method.getAccessFlags() & Const.ACC_BRIDGE) == Const.ACC_BRIDGE) { return; System.out.println(" Analyzing method " + classContext.getJavaClass().getClassName() + "." + method.getName()); Dataflow<BitSet, LiveLocalStoreAnalysis> llsaDataflow = classContext.getLiveLocalStoreDataflow(method); int numLocals = method.getCode().getMaxLocals(); int[] localStoreCount = new int[numLocals]; int[] localLoadCount = new int[numLocals]; int localsThatAreParameters = PreorderVisitor.getNumberArguments(method.getSignature()); if (!method.isStatic()) { localsThatAreParameters++; BitSet liveStoreSourceLineSet = new BitSet(); for (Field f : javaClass.getFields()) { if (f.getName().equals(lvName) && f.isStatic() == method.isStatic()) { shadowedField = f; propertySet.addProperty(DeadLocalStoreProperty.SHADOWS_FIELD); priority--; if ("main".equals(method.getName()) && method.isStatic() && "([Ljava/lang/String;)V".equals(method.getSignature())) { propertySet.addProperty(DeadLocalStoreProperty.DEAD_INCREMENT_IN_MAIN);
System.out.println("Found guaranteed null deref in " + method.getName()); for (Location loc : doomedLocations) { System.out.println("Doomed at " + loc); BitSet knownNull = new BitSet(); SourceLineAnnotation sourceLineAnnotation = SourceLineAnnotation.fromVisitedInstruction(classContext, method, loc); if (sourceLineAnnotation == null) { continue; } else if (pu.isReturnFromNonNullMethod()) { bugType = "NP_NONNULL_RETURN_VIOLATION"; String methodName = method.getName(); String methodSig = method.getSignature(); if ("clone".equals(methodName) && "()Ljava/lang/Object;".equals(methodSig)) { bugType = "NP_CLONE_COULD_RETURN_NULL"; if (!hasManyPreceedingNullTests(sourceLineAnnotation.getStartBytecode())) { hasManyNullTests = false; int pos = loc.getHandle().getPosition(); if (pos != source + 3) {
return NONE_FINALLY_INFO; BitSet exceptionTargets = new BitSet(); Map<Integer, TryBlock> finallyTargets = new LinkedHashMap<>(); for(CodeException codeException : method.getCode().getExceptionTable()) { if(codeException.getCatchType() == 0) { TryBlock block = finallyTargets.get(codeException.getHandlerPC()); exceptionTargets.set(codeException.getHandlerPC()); BitSet branchTargets = new BitSet(); for(InstructionHandle ih = il.getStart(); ih != null; ih = ih.getNext()) { Instruction inst = ih.getInstruction(); if(inst instanceof BranchInstruction) { branchTargets.set(((BranchInstruction) inst).getTarget().getPosition());
PatternElementMatch test = match.getFirstLabeledMatch("test"); InstructionHandle testInstructionHandle = test.getMatchedInstructionInstructionHandle(); if (reported.get(testInstructionHandle.getPosition())) { return; System.out.println("Field store postdominates " + postDom); extent.and(postDom); if (DEBUG) { System.out.println("extent: " + extent); Instruction ins = handle.getInstruction(); if (DEBUG) { System.out.println(location); InstructionHandle handle = j.next(); InstructionHandle nextHandle = handle.getNext(); Instruction ins = handle.getInstruction(); boolean isDefaultAccess = (method.getAccessFlags() & (Const.ACC_PUBLIC | Const.ACC_PRIVATE | Const.ACC_PROTECTED)) == 0; if (method.isPublic()) { priority = NORMAL_PRIORITY; } else if (method.isProtected() || isDefaultAccess) { priority = NORMAL_PRIORITY;
UnconditionalValueDerefDataflow dataflow = classContext.getUnconditionalValueDerefDataflow(method); SignatureParser parser = new SignatureParser(method.getSignature()); int paramLocalOffset = method.isStatic() ? 0 : 1; BitSet unconditionalDerefSet = new BitSet(); UnconditionalValueDerefSet entryFact = dataflow.getResultFact(cfg.getEntry()); Iterator<String> paramIterator = parser.parameterSignatureIterator(); .getEffectiveTypeQualifierAnnotation(xmethod, i, nonnullTypeQualifierValue); boolean implicitNullCheckForEquals = false; if (directTypeQualifierAnnotation == null && "equals".equals(method.getName()) && "(Ljava/lang/Object;)Z".equals(method.getSignature()) && !method.isStatic()) { implicitNullCheckForEquals = true; Code code = method.getCode(); ConstantPool cp = jclass.getConstantPool(); byte codeBytes[] = code.getCode(); for (CodeException e : code.getExceptionTable()) { unconditionalDerefSet.set(i); } else if (isCaught(classContext, method, entryFact, paramVN)) { || jclass.isFinal()) { priority--; reportBug(new BugInstance(this, bugPattern, priority).addClassAndMethod(jclass, method).add( LocalVariableAnnotation.getParameterLocalVariableAnnotation(method, paramLocal)));
String methodName = getMethodName(); if (Const.CONSTRUCTOR_NAME.equals(methodName) || methodName.startsWith("set") && getCode().getCode().length <= 5 || !previousStores.get(getRegisterOperand())) { priority = HIGH_PRIORITY; previousStores.set(getRegisterOperand()); XClass c = getXClass(); LocalVariableAnnotation local = LocalVariableAnnotation.getLocalVariableAnnotation(getMethod(), } else { for (XField f : c.getXFields()) { if (f.getName().equals(local.getName()) && (f.isStatic() || !getMethod().isStatic())) { bugReporter.reportBug(new BugInstance(this, "SA_LOCAL_SELF_ASSIGNMENT_INSTEAD_OF_FIELD", priority).addClassAndMethod(this).add(local).addField(f) .describe(FieldAnnotation.DID_YOU_MEAN_ROLE).addSourceLine(this)); return; .add(local).addSourceLine(this)); } else { previousStores.set(getRegisterOperand());
int resetForMethodEntry0(@SlashedClassName String className, Method m) { methodName = m.getName(); String signature = m.getSignature(); stack.clear(); lvValues.clear(); exceptionHandlers.clear(); Code code = m.getCode(); if (code != null) { CodeException[] exceptionTable = code.getExceptionTable(); if (exceptionTable != null) { for (CodeException ex : exceptionTable) { exceptionHandlers.set(ex.getHandlerPC()); System.out.println(" --- " + className + " " + m.getName() + " " + signature); Type[] argTypes = Type.getArgumentTypes(signature); int reg = 0; if (!m.isStatic()) { Item it = Item.initialArgument("L" + className + ";", reg); setLVValue(reg, it); Item it = Item.initialArgument(argType.getSignature(), reg); setLVValue(reg, it); reg += it.getSize();
String sourceFile = classContext.getJavaClass().getSourceFileName(); if (DEBUG) { System.out.println("Checking " + methodName); Location location = i.next(); InstructionHandle handle = location.getHandle(); Instruction ins = handle.getInstruction(); SourceLineAnnotation sourceLineAnnotation = SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen, sourceFile, handle); if (ins instanceof CHECKCAST) { int pc = handle.getPosition(); Instruction ins = handle.getInstruction(); if (line > 0 && linesMentionedMultipleTimes.get(line)) { split = true; if (operandType.equals(TopType.instance())) { String castSig = castType.getSignature(); SourceLineAnnotation sourceLineAnnotation = SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen, sourceFile, handle); int catchSize = Util.getSizeOfSurroundingTryBlock(classContext.getJavaClass().getConstantPool(), method.getCode(), "java/lang/ClassCastException", position);
private boolean uniqueLocations(Collection<Location> derefLocationSet) { boolean uniqueDereferenceLocations = false; LineNumberTable table = method.getLineNumberTable(); if (table == null) { uniqueDereferenceLocations = true; } else { BitSet linesMentionedMultipleTimes = classContext.linesMentionedMultipleTimes(method); for (Location loc : derefLocationSet) { int lineNumber = table.getSourceLine(loc.getHandle().getPosition()); if (!linesMentionedMultipleTimes.get(lineNumber)) { uniqueDereferenceLocations = true; } } } return uniqueDereferenceLocations; }
private boolean uniqueLocations(Collection<Location> derefLocationSet) { boolean uniqueDereferenceLocations = false; CodeException[] exceptionTable = method.getCode().getExceptionTable(); if (exceptionTable == null) { return true; } checkForCatchAll: { for (CodeException e : exceptionTable) { if (e.getCatchType() == 0) { break checkForCatchAll; } } return true; } LineNumberTable table = method.getLineNumberTable(); if (table == null) { uniqueDereferenceLocations = true; } else { BitSet linesMentionedMultipleTimes = classContext.linesMentionedMultipleTimes(method); for (Location loc : derefLocationSet) { int lineNumber = table.getSourceLine(loc.getHandle().getPosition()); if (lineNumber > 0 && !linesMentionedMultipleTimes.get(lineNumber)) { uniqueDereferenceLocations = true; } } } return uniqueDereferenceLocations; }
private void analyzeMethod(ClassContext classContext, Method method) throws DataflowAnalysisException, CFGBuilderException { if (BCELUtil.isSynthetic(method) || (method.getAccessFlags() & Const.ACC_BRIDGE) == Const.ACC_BRIDGE) { return; TypeDataflow typeDataflow = classContext.getTypeDataflow(method); String sourceFileName = javaClass.getSourceFileName(); Instruction ins = handle.getInstruction(); TypeFrame typeFrame = typeDataflow.getFactAtLocation(location); Type objType = typeFrame.getStackValue(2); if(extendsConcurrentMap(ClassName.toDottedClassName(ClassName.fromFieldSignature(objType.getSignature())))) { InstructionHandle next = handle.getNext(); boolean isIgnored = next != null && next.getInstruction() instanceof POP; if (vna.getValue(pos).equals(vn) && live.get(pos)) { BugAnnotation ba = ValueNumberSourceInfo.findAnnotationFromValueNumber(method, location, vn, vnaDataflow.getFactAtLocation(location), "VALUE_OF"); Type type = typeFrame.getTopValue(); int priority = getPriorityForBeingMutable(type); BugInstance bugInstance = new BugInstance(this, pattern, priority) .addClassAndMethod(methodGen, sourceFileName).addCalledMethod(methodGen, invoke) .add(new TypeAnnotation(type)).add(ba); SourceLineAnnotation where = SourceLineAnnotation.fromVisitedInstruction(classContext, method, location); accumulator.accumulateBug(bugInstance, where);
boolean confused = undeterminedBranchSet.get(lineNumber) || (definitelySameBranchSet.get(lineNumber) && definitelyDifferentBranchSet.get(lineNumber)); reportIt = false; } else { int pc = redundantBranch.location.getHandle().getPosition(); for (CodeException e : method.getCode().getExceptionTable()) { if (e.getCatchType() == 0 && e.getStartPC() != e.getHandlerPC() && e.getEndPC() <= pc && pc <= e.getEndPC() + 5) {
for (BasicBlock block : blockList) { InstructionHandle first = block.getFirstInstruction(); if (first != null && first.getPosition() == obj.getHandlerPC() && first.getInstruction() instanceof ASTORE) { ASTORE astore = (ASTORE) first.getInstruction(); BitSet liveStoreSet = dataflow.getFactAtLocation(new Location(first, block)); if (!liveStoreSet.get(astore.getIndex())) { Method m = getMethod(); bugReporter.reportSkippedAnalysis(DescriptorFactory.instance().getMethodDescriptor(getClassName(), getMethodName(), getMethodSig(), m.isStatic())); } catch (DataflowAnalysisException e) { bugReporter.logError("Error checking for dead exception store", e);