/** * Return whether or not this block is a null check. */ public boolean isNullCheck() { // Null check blocks must be exception throwers, // and are always empty. (The only kind of non-empty // exception throwing block is one terminated by an ATHROW). if (!isExceptionThrower() || getFirstInstruction() != null) { return false; } short opcode = exceptionThrower.getInstruction().getOpcode(); return nullCheckInstructionSet.get(opcode); }
if(firstHandle.getPosition() >= endOfFirstSegment) { return secondHandle.getPosition() >= endOfSecondSegment; if(secondHandle.getPosition() >= endOfSecondSegment) { return firstHandle.getPosition() >= endOfFirstSegment; Instruction firstInstruction = firstHandle.getInstruction(); Instruction secondInstruction = secondHandle.getInstruction(); if(firstInstruction instanceof BranchInstruction && secondInstruction instanceof BranchInstruction) { int firstOpcode = firstInstruction.getOpcode(); int secondOpcode = secondInstruction.getOpcode(); if(firstOpcode != secondOpcode) { return false; int firstTarget = ((BranchInstruction)firstInstruction).getTarget().getPosition(); int secondTarget = ((BranchInstruction)secondInstruction).getTarget().getPosition(); if(firstTarget == second) { if(oppositeChecks || secondTarget <= endOfSecondSegment) { if(!firstInstruction.equals(secondInstruction)) { return false; firstHandle = firstHandle.getNext(); secondHandle = secondHandle.getNext();
/** * Determine whether or not the given instruction is a control flow merge. * * @param handle * the instruction * @return true if the instruction is a control merge, false otherwise */ private static boolean isMerge(InstructionHandle handle) { if (handle.hasTargeters()) { // Check all targeters of this handle to see if any // of them are branches. If so, the instruction is a merge. InstructionTargeter[] targeterList = handle.getTargeters(); for (InstructionTargeter targeter : targeterList) { if (targeter instanceof BranchInstruction) { return true; } } } return false; }
private @CheckForNull InstructionHandle getPreviousInstruction(InstructionHandle handle, boolean skipNops) { while (handle.getPrev() != null) { handle = handle.getPrev(); Instruction prevIns = handle.getInstruction(); if (!(prevIns instanceof NOP && skipNops)) { return handle; } } return null; }
public static LocalVariableAnnotation getLocalVariableAnnotation(Method method, Location location, IndexedInstruction ins) { int local = ins.getIndex(); InstructionHandle handle = location.getHandle(); int position1 = handle.getNext().getPosition(); int position2 = handle.getPosition(); return getLocalVariableAnnotation(method, local, position1, position2); }
return NONE_FINALLY_INFO; BitSet exceptionTargets = new BitSet(); Map<Integer, TryBlock> finallyTargets = new LinkedHashMap<>(); for(CodeException codeException : method.getCode().getExceptionTable()) { exceptionTargets.set(codeException.getHandlerPC()); return NONE_FINALLY_INFO; InstructionList il = methodGen.getInstructionList(); 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()); return NONE_FINALLY_INFO; return new FinallyDuplicatesInfo(il.getInstructionPositions(), duplicates);
/** * Add an instruction to the subroutine. We keep track of which * instructions are part of which subroutines. No instruction may be * part of more than one subroutine. * * @param handle * the instruction to be added to the subroutine */ public void addInstruction(InstructionHandle handle) throws CFGBuilderException { int position = handle.getPosition(); if (usedInstructionSet.get(position)) { throw new CFGBuilderException("Instruction " + handle + " visited in multiple subroutines"); } instructionSet.set(position); usedInstructionSet.set(position); }
/** * Is the given instruction part of this subroutine? */ public boolean containsInstruction(InstructionHandle handle) { return instructionSet.get(handle.getPosition()); }
if (bytecodeSet.get(Const.INSTANCEOF) || bytecodeSet.get(Const.CHECKCAST)) { return; ConstantPoolGen cpg = classContext.getConstantPoolGen(); String sourceFile = classContext.getJavaClass().getSourceFileName(); if (DEBUG) { String methodName = methodGen.getClassName() + "." + methodGen.getName(); System.out.println("Checking " + methodName); Location location = i.next(); InstructionHandle handle = location.getHandle(); Instruction ins = handle.getInstruction(); String mName = invoke.getMethodName(cpg); if (!"writeObject".equals(mName)) { continue; String cName = invoke.getClassName(cpg); if (!"java.io.ObjectOutput".equals(cName) && !"java.io.ObjectOutputStream".equals(cName)) { continue; SourceLineAnnotation sourceLineAnnotation = SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen, sourceFile, handle);
SignatureParser sigParser = new SignatureParser(invokeInstruction.getSignature(cpg)); for (int i = nullArgSet.nextSetBit(0); i >= 0; i = nullArgSet.nextSetBit(i + 1)) { boolean definitelyNull = definitelyNullArgSet.get(i); if (DEBUG_NULLARG) { System.out.println("Checking " + m); boolean duplicated = isDuplicated(propertySet, location.getHandle().getPosition(), false); BugInstance warning = new BugInstance(this, "NP_NONNULL_PARAM_VIOLATION", priority) .addClassAndMethod(classContext.getJavaClass(), method).addMethod(m) .describe(MethodAnnotation.METHOD_CALLED).addParameterAnnotation(i, description) .addOptionalAnnotation(variableAnnotation).addSourceLine(classContext, method, location);
InvokeInstruction instruction = (InvokeInstruction) location.getHandle().getInstruction(); SignatureParser sigParser = new SignatureParser(instruction.getSignature(classContext.getConstantPoolGen())); for (int i = violatedParamSet.nextSetBit(0); i >= 0; i = violatedParamSet.nextSetBit(i + 1)) { boolean definitelyNull = definitelyNullArgSet.get(i); BugAnnotation variableAnnotation = ValueNumberSourceInfo.findAnnotationFromValueNumber(method, location, valueNumber, vnaFrame, "VALUE_OF"); warning.addOptionalAnnotation(variableAnnotation); } catch (DataflowAnalysisException e) { AnalysisContext.logError("error", e); warning.addParameterAnnotation(i, definitelyNull ? "INT_NULL_ARG" : "INT_MAYBE_NULL_ARG");
public List<Edge> getDuplicates(CFG cfg, Edge edge) { InstructionHandle ih = edge.getSource().getLastInstruction(); if(ih == null) { return Collections.emptyList(); } BitSet duplicates = getDuplicates(ih.getPosition()); if(duplicates.isEmpty()) { return Collections.emptyList(); } List<Edge> result = new ArrayList<>(); for(Iterator<Edge> edgeIterator = cfg.edgeIterator(); edgeIterator.hasNext(); ) { Edge next = edgeIterator.next(); if(next.getType() != edge.getType()) { continue; } InstructionHandle lastInst = next.getSource().getLastInstruction(); if(lastInst != null && lastInst.getPosition() >= 0 && duplicates.get(lastInst.getPosition())) { result.add(next); } } return result; }
/** * Add a load of given field at given instruction. * * @param handle * the instruction * @param field * the field */ public void addLoad(InstructionHandle handle, XField field) { getLoadStoreCount(field).loadCount++; handleToFieldMap.put(handle, field); loadHandleSet.set(handle.getPosition()); }
TypeDataflow typeDataflow = classContext.getTypeDataflow(method); String sourceFileName = javaClass.getSourceFileName(); Instruction ins = handle.getInstruction(); if ("putIfAbsent".equals(invoke.getMethodName(cpg))) { String signature = invoke.getSignature(cpg); if ("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;".equals(signature) && !(invoke instanceof INVOKESTATIC)) { 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);
return; Instruction ins = location.getHandle().getInstruction(); int opcode = ins.getOpcode(); int offset = 1; if (opcode == LCMP || opcode == LXOR || opcode == LAND || opcode == LOR || opcode == LSUB) { return; SourceLineAnnotation sourceLine = SourceLineAnnotation.fromVisitedInstruction(classContext, methodGen, sourceFile, location.getHandle()); int line = sourceLine.getStartLine(); BitSet occursMultipleTimes = classContext.linesMentionedMultipleTimes(method); if (line > 0 && occursMultipleTimes.get(line)) { return; BugInstance bug = new BugInstance(this, prefix + op, priority).addClassAndMethod(methodGen, sourceFile); if (ins instanceof InvokeInstruction) { bug.addCalledMethod(classContext.getConstantPoolGen(), (InvokeInstruction) ins);
return null; InstructionList il = methodGen.getInstructionList(); for (InstructionHandle handle = il.getStart(); handle != null; handle = handle.getNext()) { Instruction ins = handle.getInstruction(); short opcode = ins.getOpcode(); try { if (opcode == Const.INVOKESTATIC) { } else if (fieldInstructionOpcodeSet.get(opcode)) { boolean isLoad = (opcode == Const.GETFIELD || opcode == Const.GETSTATIC); XField field = Hierarchy.findXField((FieldInstruction) ins, cpg);
int outlineChunkStartOffset = first.getPosition(); int outlineChunkEndOffset = last.getPosition() + last.getInstruction().getLength(); InstructionHandle limit = last.getNext(); for (InstructionHandle ih = first; ih != limit; ih = ih.getNext()) { Instruction inst = ih.getInstruction(); if (ih.hasTargeters()) { if (inst instanceof OutlineableChunkEnd) { targetMap.put(ih, lastCopyHandle); getLocalVariableRegistry() .lookupRegisteredLocalVariable(oldLocalVarIndex, ih.getPosition()); LocalVariableGen newLVG = (LocalVariableGen)localVarMap.get(oldLVG); if (ih.hasTargeters()) { targetMap.put(ih, lastCopyHandle); lastCopyHandle); pendingTargetMappingHandle = pendingTargetMappingHandle.getNext(); } while(pendingTargetMappingHandle != ih); Instruction i = ih.getInstruction(); Instruction c = ch.getInstruction();
public void start() { if(!_mg.isAbstract() && !_mg.isNative()) { for(InstructionHandle ih = _mg.getInstructionList().getStart(); ih != null; ih = ih.getNext()) { Instruction i = ih.getInstruction(); if(i instanceof BranchInstruction) { branch_map.put(i, ih); // memorize container } if(ih.hasTargeters()) { if(i instanceof BranchInstruction) { _out.println(" InstructionHandle ih_" + ih.getPosition() + ";"); } else { _out.print(" InstructionHandle ih_" + ih.getPosition() + " = "); } } else { _out.print(" "); } if(!visitInstruction(i)) i.accept(this); } updateBranchTargets(); updateExceptionHandlers(); } }
for (InstructionHandle ih = il.getStart(); ih != null; ih = ih.getNext()) { Instruction inst = ih.getInstruction(); switch (inst.getOpcode()) { for (InstructionHandle ih = il.getStart(); ih != null; ih = ih.getNext()) { Instruction inst = ih.getInstruction(); BranchHandle oldIfHandle = (BranchHandle)ih; InstructionHandle target = oldIfInst.getTarget(); int relativeTargetOffset = target.getPosition() - oldIfHandle.getPosition(); BranchHandle invertedIfHandle = il.append(oldIfHandle, invertedIfInst);
System.out.println("Method: " + methodGen.getName() + " - " + methodGen.getSignature() + "in class " + methodGen.getClassName()); InstructionHandle handle = methodGen.getInstructionList().getStart(); while (handle != null) { int bytecodeOffset = handle.getPosition(); if (bytecodeOffset < 0) { throw new IllegalStateException("Bad bytecode offset: " + bytecodeOffset); handle = handle.getNext(); ++numBytecodes;