Search in sources :

Example 11 with CodeException

use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.

the class ExceptionSoftening method sawOpcode.

/**
 * overrides the visitor to find catch blocks that throw runtime exceptions
 *
 * @param seen
 *            the opcode of the currently parsed instruction
 */
@Override
public void sawOpcode(int seen) {
    try {
        stack.precomputation(this);
        int pc = getPC();
        CodeException ex = catchHandlerPCs.get(Integer.valueOf(pc));
        if (ex != null) {
            int endPC;
            if ((seen == Const.GOTO) || (seen == Const.GOTO_W)) {
                endPC = this.getBranchTarget();
            } else {
                endPC = Integer.MAX_VALUE;
            }
            ConstantPool pool = getConstantPool();
            ConstantClass ccls = (ConstantClass) pool.getConstant(ex.getCatchType());
            String catchSig = ccls.getBytes(pool);
            CatchInfo ci = new CatchInfo(ex.getHandlerPC(), endPC, catchSig);
            catchInfos.add(ci);
        }
        updateEndPCsOnCatchRegScope(catchInfos, pc, seen);
        removeFinishedCatchBlocks(catchInfos, pc);
        if (seen == Const.ATHROW) {
            processThrow();
        } else if ((seen == Const.IRETURN) && isBooleanMethod && !hasValidFalseReturn && (stack.getStackDepth() > 0)) {
            processBooleanReturn();
        }
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) ConstantPool(org.apache.bcel.classfile.ConstantPool) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantClass(org.apache.bcel.classfile.ConstantClass)

Example 12 with CodeException

use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.

the class LostExceptionStackTrace method prescreen.

/**
 * looks for methods that contain a catch block and an ATHROW opcode
 *
 * @param code
 *            the context object of the current code block
 * @param method
 *            the context object of the current method
 * @return if the class throws exceptions
 */
public boolean prescreen(Code code, Method method) {
    if (method.isSynthetic()) {
        return false;
    }
    CodeException[] ce = code.getExceptionTable();
    if (CollectionUtils.isEmpty(ce)) {
        return false;
    }
    BitSet bytecodeSet = getClassContext().getBytecodeSet(method);
    return (bytecodeSet != null) && bytecodeSet.get(Const.ATHROW);
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) BitSet(java.util.BitSet)

Example 13 with CodeException

use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.

the class LostExceptionStackTrace method sawOpcode.

/**
 * implements the visitor to find throwing alternative exceptions from a catch block, without forwarding along the original exception
 */
@Override
public void sawOpcode(int seen) {
    boolean markAsValid = false;
    try {
        stack.precomputation(this);
        int pc = getPC();
        for (CodeException ex : exceptions) {
            if (pc == ex.getEndPC()) {
                if (OpcodeUtils.isReturn(seen)) {
                    addCatchBlock(ex.getHandlerPC(), Integer.MAX_VALUE);
                } else if ((seen == Const.GOTO) || (seen == Const.GOTO_W)) {
                    addCatchBlock(ex.getHandlerPC(), this.getBranchTarget());
                } else {
                    addCatchBlock(ex.getHandlerPC(), Integer.MAX_VALUE);
                }
            } else if (pc == ex.getHandlerPC()) {
                removePreviousHandlers(pc);
            }
        }
        Iterator<CatchInfo> it = catchInfos.iterator();
        while (it.hasNext()) {
            try {
                CatchInfo catchInfo = it.next();
                if (pc == catchInfo.getStart()) {
                    if (!updateExceptionRegister(catchInfo, seen, pc)) {
                        it.remove();
                    }
                    break;
                } else if (pc > catchInfo.getFinish()) {
                    it.remove();
                    break;
                } else if ((pc > catchInfo.getStart()) && (pc <= catchInfo.getFinish())) {
                    if (seen == Const.INVOKESPECIAL) {
                        if (Values.CONSTRUCTOR.equals(getNameConstantOperand())) {
                            String className = getClassConstantOperand();
                            JavaClass exClass = Repository.lookupClass(className);
                            if (exClass.instanceOf(throwableClass)) {
                                String sig = getSigConstantOperand();
                                if ((sig.indexOf("Exception") >= 0) || (sig.indexOf("Throwable") >= 0) || (sig.indexOf("Error") >= 0)) {
                                    markAsValid = true;
                                    break;
                                }
                                if (exClass.instanceOf(assertionClass)) {
                                    // just ignore LEST for AssertionErrors
                                    markAsValid = true;
                                    break;
                                }
                            }
                        } else if (isPossibleExBuilder(catchInfo.getRegister())) {
                            markAsValid = true;
                        }
                    } else if (seen == Const.INVOKEVIRTUAL) {
                        String methodName = getNameConstantOperand();
                        if ("initCause".equals(methodName) || "addSuppressed".equals(methodName)) {
                            if (stack.getStackDepth() > 1) {
                                String className = getClassConstantOperand();
                                JavaClass exClass = Repository.lookupClass(className);
                                if (exClass.instanceOf(throwableClass)) {
                                    OpcodeStack.Item itm = stack.getStackItem(1);
                                    int reg = itm.getRegisterNumber();
                                    if (reg >= 0) {
                                        exReg.put(Integer.valueOf(reg), Boolean.TRUE);
                                    }
                                    // Fixes javac generated code
                                    markAsValid = true;
                                }
                            }
                        } else if (("getTargetException".equals(methodName) || "getCause".equals(methodName)) && "java/lang/reflect/InvocationTargetException".equals(getClassConstantOperand())) {
                            markAsValid = true;
                        } else if (isPossibleExBuilder(catchInfo.getRegister())) {
                            markAsValid = true;
                        }
                    } else if ((seen == Const.INVOKEINTERFACE) || (seen == Const.INVOKESTATIC)) {
                        if (isPossibleExBuilder(catchInfo.getRegister())) {
                            markAsValid = true;
                        }
                    } else if (seen == Const.ATHROW) {
                        if (stack.getStackDepth() > 0) {
                            OpcodeStack.Item itm = stack.getStackItem(0);
                            if ((itm.getRegisterNumber() != catchInfo.getRegister()) && (itm.getUserValue() == null)) {
                                if (!isPre14Class(itm.getJavaClass())) {
                                    int priority = getPrevOpcode(1) == Const.MONITOREXIT ? LOW_PRIORITY : NORMAL_PRIORITY;
                                    bugReporter.reportBug(new BugInstance(this, BugType.LEST_LOST_EXCEPTION_STACK_TRACE.name(), priority).addClass(this).addMethod(this).addSourceLine(this));
                                }
                                it.remove();
                                break;
                            }
                        }
                        if (catchInfo.getFinish() == Integer.MAX_VALUE) {
                            catchInfo.setFinish(pc);
                        }
                    } else if (OpcodeUtils.isAStore(seen)) {
                        if (lastWasExitPoint) {
                            // crazy jdk6 finally block injection -- shut
                            // off detection
                            catchInfos.clear();
                            break;
                        }
                        if (stack.getStackDepth() > 0) {
                            OpcodeStack.Item itm = stack.getStackItem(0);
                            int reg = RegisterUtils.getAStoreReg(this, seen);
                            exReg.put(Integer.valueOf(reg), (Boolean) itm.getUserValue());
                            if ((reg == catchInfo.getRegister()) && (catchInfo.getFinish() == Integer.MAX_VALUE)) {
                                it.remove();
                            }
                        }
                    } else if (OpcodeUtils.isALoad(seen)) {
                        Boolean valid = exReg.get(Integer.valueOf(RegisterUtils.getALoadReg(this, seen)));
                        if (valid != null) {
                            markAsValid = valid.booleanValue();
                        }
                    } else if (OpcodeUtils.isReturn(seen)) {
                        removeIndeterminateHandlers(pc);
                        break;
                    }
                }
            } catch (ClassNotFoundException cnfe) {
                bugReporter.reportMissingClass(cnfe);
                it.remove();
            }
        }
        lastWasExitPoint = (seen == Const.GOTO) || (seen == Const.GOTO_W) || (seen == Const.ATHROW) || OpcodeUtils.isReturn(seen);
    } finally {
        TernaryPatcher.pre(stack, seen);
        stack.sawOpcode(this, seen);
        TernaryPatcher.post(stack, seen);
        if (markAsValid && (stack.getStackDepth() > 0)) {
            OpcodeStack.Item itm = stack.getStackItem(0);
            itm.setUserValue(Boolean.TRUE);
        }
    }
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) JavaClass(org.apache.bcel.classfile.JavaClass)

Example 14 with CodeException

use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.

the class BloatedAssignmentScope method visitCode.

/**
 * implements the visitor to reset the register to location map
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    try {
        ignoreRegs.clear();
        Method method = getMethod();
        if (!method.isStatic()) {
            ignoreRegs.set(0);
        }
        int[] parmRegs = RegisterUtils.getParameterRegisters(method);
        for (int parm : parmRegs) {
            ignoreRegs.set(parm);
        }
        rootScopeBlock = new ScopeBlock(0, obj.getLength());
        tryBlocks.clear();
        catchHandlers.clear();
        CodeException[] exceptions = obj.getExceptionTable();
        if (exceptions != null) {
            for (CodeException ex : exceptions) {
                tryBlocks.set(ex.getStartPC());
                catchHandlers.set(ex.getHandlerPC());
            }
        }
        switchTargets.clear();
        stack.resetForMethodEntry(this);
        dontReport = false;
        sawDup = false;
        sawNull = false;
        super.visitCode(obj);
        if (!dontReport) {
            rootScopeBlock.findBugs(new HashSet<Integer>());
        }
    } finally {
        rootScopeBlock = null;
    }
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) Method(org.apache.bcel.classfile.Method) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod)

Example 15 with CodeException

use of org.apache.bcel.classfile.CodeException in project fb-contrib by mebigfatguy.

the class BuryingLogic method visitCode.

@Override
public void visitCode(Code obj) {
    Method m = getMethod();
    if (m.getReturnType() == Type.VOID) {
        return;
    }
    stack.resetForMethodEntry(this);
    ifBlocks.clear();
    activeUnconditional = null;
    CodeException[] ces = obj.getExceptionTable();
    if (CollectionUtils.isEmpty(ces)) {
        catchPCs = null;
    } else {
        catchPCs = new BitSet();
        for (CodeException ce : ces) {
            catchPCs.set(ce.getHandlerPC());
        }
    }
    gotoBranchPCs.clear();
    casePositions.clear();
    lookingForResetOp = false;
    try {
        super.visitCode(obj);
    } catch (StopOpcodeParsingException e) {
    // reported an issue, so get out
    }
}
Also used : CodeException(org.apache.bcel.classfile.CodeException) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BitSet(java.util.BitSet) Method(org.apache.bcel.classfile.Method)

Aggregations

CodeException (org.apache.bcel.classfile.CodeException)15 ToString (com.mebigfatguy.fbcontrib.utils.ToString)4 BitSet (java.util.BitSet)3 Method (org.apache.bcel.classfile.Method)3 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)2 BugInstance (edu.umd.cs.findbugs.BugInstance)2 ArrayList (java.util.ArrayList)2 JavaClass (org.apache.bcel.classfile.JavaClass)2 FQMethod (com.mebigfatguy.fbcontrib.utils.FQMethod)1 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)1 XMethod (edu.umd.cs.findbugs.ba.XMethod)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConstantClass (org.apache.bcel.classfile.ConstantClass)1 ConstantPool (org.apache.bcel.classfile.ConstantPool)1 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)1