/** * Dump the disassembled code of all methods in the class. */ public static void printCode(Method[] methods) { for (Method m : methods) { System.out.println(m); Code code = m.getCode(); if (code != null) { System.out.println(code); } } } }
public static int getSizeOfSurroundingTryBlock(@CheckForNull Method method, @CheckForNull String vmNameOfExceptionClass, int pc) { if (method == null) { return Integer.MAX_VALUE; } return getSizeOfSurroundingTryBlock(method.getConstantPool(), method.getCode(), vmNameOfExceptionClass, pc); }
private static LineNumberTable getLineNumberTable(PreorderVisitor visitor) { Code code = visitor.getMethod().getCode(); if (code == null) { return null; } return code.getLineNumberTable(); }
/** * Compute hash on given method. * * @param method * the method * @return this object */ public MethodHash computeHash(Method method) { final MessageDigest digest = Util.getMD5Digest(); byte[] code; if (method.getCode() == null || method.getCode().getCode() == null) { code = new byte[0]; } else { code = method.getCode().getCode(); } BytecodeScanner.Callback callback = (opcode, index) -> digest.update((byte) opcode); BytecodeScanner bytecodeScanner = new BytecodeScanner(); bytecodeScanner.scan(code, callback); hash = digest.digest(); return this; }
public InstructionHandleMap(MethodGen methodGen) { map = new Object[methodGen.getMethod().getCode().getLength()]; }
private static int getLineNumber(Method method, InstructionHandle handle) { LineNumberTable table = method.getCode().getLineNumberTable(); if (table == null) { return -1; } return table.getSourceLine(handle.getPosition()); } }
@Override public CompactLocationNumbering analyze(IAnalysisCache analysisCache, MethodDescriptor descriptor) throws CheckedAnalysisException { Method method = analysisCache.getMethodAnalysis(Method.class, descriptor); if (method.getCode() == null) { return null; } CFG cfg = getCFG(analysisCache, descriptor); return new CompactLocationNumbering(cfg); } }
@Override public void visitClassContext(ClassContext classContext) { Method[] methodList = classContext.getJavaClass().getMethods(); for (Method method : methodList) { if (method.getCode() == null) { continue; } try { analyzeMethod(classContext, method); } catch (CFGBuilderException e) { bugReporter.logError("Detector " + this.getClass().getName() + " caught exception", e); } catch (DataflowAnalysisException e) { // bugReporter.logError("Detector " + this.getClass().getName() // + " caught exception", e); } } }
public boolean isCaught(ClassContext classContext, Method method, UnconditionalValueDerefSet entryFact, ValueNumber paramVN) { boolean caught = true; Set<Location> dereferenceSites = entryFact.getDerefLocationSet(paramVN); if (dereferenceSites != null && !dereferenceSites.isEmpty()) { ConstantPool cp = classContext.getJavaClass().getConstantPool(); for(Location loc : dereferenceSites) { if (!FindNullDeref.catchesNull(cp, method.getCode(), loc)) { caught = false; } } } return caught; }
@Override public void visitClassContext(ClassContext classContext) { Method[] methodList = classContext.getJavaClass().getMethods(); for (Method method : methodList) { if (method.getCode() == null) { continue; } try { analyzeMethod(classContext, method); } catch (MethodUnprofitableException mue) { if (SystemProperties.getBoolean("unprofitable.debug")) { // don't // report bugReporter.logError("skipping unprofitable method in " + getClass().getName()); } } catch (CFGBuilderException e) { bugReporter.logError("Detector " + this.getClass().getName() + " caught exception", e); } catch (DataflowAnalysisException e) { bugReporter.logError("Detector " + this.getClass().getName() + " caught exception", e); } } }
@Override public @CheckForNull JumpInfo analyze(IAnalysisCache analysisCache, MethodDescriptor descriptor) throws CheckedAnalysisException { Method method = analysisCache.getMethodAnalysis(Method.class, descriptor); JavaClass jclass = getJavaClass(analysisCache, descriptor.getClassDescriptor()); Code code = method.getCode(); if (code == null) { return null; } JumpStackComputation branchAnalysis = new JumpStackComputation(descriptor); return computeJumpInfo(jclass, method, branchAnalysis); }
@Override public void visitClassContext(ClassContext classContext) { Method[] methodList = classContext.getJavaClass().getMethods(); for (Method method : methodList) { if (method.getCode() == null) { continue; } try { analyzeMethod(classContext, method); } catch (CFGBuilderException e) { bugReporter.logError("Detector " + this.getClass().getName() + " caught exception", e); } catch (DataflowAnalysisException e) { // bugReporter.logError("Detector " + this.getClass().getName() // + " caught exception", e); } bugAccumulator.reportAccumulatedBugs(); } }
boolean inIndirectCatchNullBlock(Location loc) { int pc = loc.getHandle().getPosition(); int catchSize = Util.getSizeOfSurroundingTryBlock(classContext.getJavaClass().getConstantPool(), method.getCode(), "java/lang/Exception", pc); if (catchSize < 5) { return true; } catchSize = Util.getSizeOfSurroundingTryBlock(classContext.getJavaClass().getConstantPool(), method.getCode(), "java/lang/RuntimeException", pc); if (catchSize < 5) { return true; } catchSize = Util.getSizeOfSurroundingTryBlock(classContext.getJavaClass().getConstantPool(), method.getCode(), "java/lang/Throwable", pc); if (catchSize < 5) { return true; } return false; } }
boolean inExplictCatchNullBlock(Location loc) { int pc = loc.getHandle().getPosition(); int catchSize = Util.getSizeOfSurroundingTryBlock(classContext.getJavaClass().getConstantPool(), method.getCode(), "java/lang/NullPointerException", pc); if (catchSize < Integer.MAX_VALUE) { return true; } return false; }
@Override public void visitClassContext(ClassContext classContext) { Method[] methodList = classContext.getJavaClass().getMethods(); for (Method method : methodList) { if (method.getCode() == null) { continue; } try { analyzeMethod(classContext, method); } catch (CFGBuilderException e) { bugReporter.logError("Error compting field store types", e); } catch (DataflowAnalysisException e) { bugReporter.logError("Error compting field store types", e); } } }
@Override public void visitClassContext(ClassContext classContext) { JavaClass javaClass = classContext.getJavaClass(); Method[] methodList = javaClass.getMethods(); for (Method method : methodList) { if (method.getCode() == null) { continue; } try { analyzeMethod(classContext, method); } catch (MethodUnprofitableException e) { assert true; // move along; nothing to see } catch (CFGBuilderException e) { String msg = "Detector " + this.getClass().getName() + " caught exception while analyzing " + javaClass.getClassName() + "." + method.getName() + " : " + method.getSignature(); bugReporter.logError(msg, e); } catch (DataflowAnalysisException e) { String msg = "Detector " + this.getClass().getName() + " caught exception while analyzing " + javaClass.getClassName() + "." + method.getName() + " : " + method.getSignature(); bugReporter.logError(msg, e); } } }
@Override public UnpackedCode analyze(IAnalysisCache analysisCache, MethodDescriptor descriptor) throws CheckedAnalysisException { Method method = getMethod(analysisCache, descriptor); Code code = method.getCode(); if (code == null) { return null; } byte[] instructionList = code.getCode(); // Create callback UnpackedBytecodeCallback callback = new UnpackedBytecodeCallback(instructionList.length); // Scan the method. BytecodeScanner scanner = new BytecodeScanner(); scanner.scan(instructionList, callback); return callback.getUnpackedCode(); } }
@Override public void visitClassContext(ClassContext classContext) { JavaClass javaClass = classContext.getJavaClass(); Method[] methodList = javaClass.getMethods(); for (Method method : methodList) { if (method.getCode() == null) { continue; } if (!prescreen(classContext, method)) { continue; } try { analyzeMethod(classContext, method); } catch (CFGBuilderException e) { bugReporter.logError("FindSleepWithLockHeld caught exception", e); } catch (DataflowAnalysisException e) { bugReporter.logError("FindSleepWithLockHeld caught exception", e); } } }
/** * Create from Method and bytecode offset in a visited class. * * @param jclass * JavaClass of visited class * @param method * Method in visited class * @param pc * bytecode offset in visited method * @return SourceLineAnnotation describing visited instruction */ public static SourceLineAnnotation fromVisitedInstruction(JavaClass jclass, Method method, int pc) { LineNumberTable lineNumberTable = method.getCode().getLineNumberTable(); String className = jclass.getClassName(); String sourceFile = jclass.getSourceFileName(); if (lineNumberTable == null) { return createUnknown(className, sourceFile, pc, pc); } int startLine = lineNumberTable.getSourceLine(pc); return new SourceLineAnnotation(className, sourceFile, startLine, startLine, pc, pc); }
@Override public MethodBytecodeSet analyze(IAnalysisCache analysisCache, MethodDescriptor descriptor) throws CheckedAnalysisException { Method method = analysisCache.getMethodAnalysis(Method.class, descriptor); Code code = method.getCode(); if (code == null) { return null; } byte[] instructionList = code.getCode(); // Create callback UnpackedBytecodeCallback callback = new UnpackedBytecodeCallback(instructionList.length); // Scan the method. BytecodeScanner scanner = new BytecodeScanner(); scanner.scan(instructionList, callback); UnpackedCode unpackedCode = callback.getUnpackedCode(); MethodBytecodeSet result = null; if (unpackedCode != null) { result = unpackedCode.getBytecodeSet(); } return result; }