Java Code Examples for org.apache.bcel.Const#DUP

The following examples show how to use org.apache.bcel.Const#DUP . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BetterCFGBuilder2.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * @param handle instruction handle which loads the object for further GETFIELD/PUTFIELD operation
 * @return true if this object is known to be non-null
 */
private boolean isSafeFieldSource(InstructionHandle handle) {
    while (handle != null && handle.getInstruction().getOpcode() == Const.DUP) {
        // Some compilers generate DUP for field increment code like
        // ALOAD_0 / DUP / GETFIELD x / ICONST_1 / IADD / PUTFIELD x
        handle = handle.getPrev();
    }
    if (handle == null) {
        return false;
    }
    Instruction inst = handle.getInstruction();
    if (inst.getOpcode() == Const.ALOAD_0) {
        return true;
    }
    return inst instanceof GETFIELD && ((GETFIELD) inst).getFieldName(cpg).startsWith("this$");
}
 
Example 2
Source File: MutableLock.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void sawOpcode(int seen) {

    switch (seen) {
    case Const.ALOAD_0:
        thisOnTOS = true;
        return;
    case Const.MONITOREXIT:
        setFields.clear();
        break;
    case Const.PUTFIELD:
        if (getClassConstantOperand().equals(getClassName())) {
            setFields.add(getNameConstantOperand());
        }
        break;
    case Const.GETFIELD:
        if (thisOnTOS && getClassConstantOperand().equals(getClassName()) && setFields.contains(getNameConstantOperand())
                && asUnsignedByte(codeBytes[getPC() + 3]) == Const.DUP && asUnsignedByte(codeBytes[getPC() + 5]) == Const.MONITORENTER

                && !finalFields.contains(getNameConstantOperand())) {
            bugReporter.reportBug(new BugInstance(this, "ML_SYNC_ON_UPDATED_FIELD", NORMAL_PRIORITY).addClassAndMethod(this)
                    .addReferencedField(this).addSourceLine(this, getPC() + 5));
        }
        break;
    default:
        break;
    }
    thisOnTOS = false;
}
 
Example 3
Source File: QuestionableBooleanAssignment.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (seen == Const.GOTO && getBranchOffset() == 4) {
        state = SEEN_GOTO;
    } else {
        switch (state) {
        case SEEN_NOTHING:
            if ((seen == Const.ICONST_1) || (seen == Const.ICONST_0)) {
                state = SEEN_ICONST_0_OR_1;
            }
            break;

        case SEEN_ICONST_0_OR_1:
            if (seen == Const.DUP) {
                state = SEEN_DUP;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_DUP:
            if (((seen >= Const.ISTORE_0) && (seen <= Const.ISTORE_3)) || (seen == Const.ISTORE)) {
                state = SEEN_ISTORE;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ISTORE:
            if (seen == Const.IFEQ || seen == Const.IFNE) {
                bug = new BugInstance(this, "QBA_QUESTIONABLE_BOOLEAN_ASSIGNMENT", HIGH_PRIORITY).addClassAndMethod(this)
                        .addSourceLine(this);
                state = SEEN_IF;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_IF:
            state = SEEN_NOTHING;
            if (seen == Const.NEW) {
                String cName = getClassConstantOperand();
                if ("java/lang/AssertionError".equals(cName)) {
                    break;
                }
            }
            bugReporter.reportBug(bug);
            break;
        case SEEN_GOTO:
            state = SEEN_NOTHING;
            break;
        default:
            break;
        }
    }
}
 
Example 4
Source File: SynchronizingOnContentsOfFieldToProtectField.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    // System.out.println(state + " " + getPC() + " " + Const.getOpcodeName(seen));
    if (countDown == 2 && seen == Const.GOTO) {
        CodeException tryBlock = getSurroundingTryBlock(getPC());
        if (tryBlock != null && tryBlock.getEndPC() == getPC()) {
            pendingBug.lowerPriority();
        }
    }
    if (countDown > 0) {
        countDown--;
        if (countDown == 0) {
            if (seen == Const.MONITOREXIT) {
                pendingBug.lowerPriority();
            }

            bugReporter.reportBug(pendingBug);
            pendingBug = null;
        }
    }
    if (seen == Const.PUTFIELD) {

        if (syncField != null && getPrevOpcode(1) != Const.ALOAD_0 && syncField.equals(getXFieldOperand())) {
            OpcodeStack.Item value = stack.getStackItem(0);
            int priority = Priorities.HIGH_PRIORITY;
            if (value.isNull()) {
                priority = Priorities.NORMAL_PRIORITY;
            }
            pendingBug = new BugInstance(this, "ML_SYNC_ON_FIELD_TO_GUARD_CHANGING_THAT_FIELD", priority)
                    .addClassAndMethod(this).addField(syncField).addSourceLine(this);
            countDown = 2;

        }

    }
    if (seen == Const.MONITOREXIT) {
        pendingBug = null;
        countDown = 0;
    }

    if (seen == Const.MONITORENTER) {
        syncField = null;
    }

    switch (state) {
    case 0:
        if (seen == Const.ALOAD_0) {
            state = 1;
        }
        break;
    case 1:
        if (seen == Const.GETFIELD) {
            state = 2;
            field = getXFieldOperand();
        } else {
            state = 0;
        }
        break;
    case 2:
        if (seen == Const.DUP) {
            state = 3;
        } else {
            state = 0;
        }
        break;
    case 3:
        if (isRegisterStore()) {
            state = 4;
        } else {
            state = 0;
        }
        break;
    case 4:
        if (seen == Const.MONITORENTER) {
            state = 0;
            syncField = field;
        } else {
            state = 0;
        }
        break;
    default:
        break;
    }

}
 
Example 5
Source File: FindFieldSelfAssignment.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {

    if (DEBUG) {
        System.out.printf("%5d %12s %s%n", getPC(), Const.getOpcodeName(seen), stack);
    }
    if (seen == Const.PUTFIELD) {
        OpcodeStack.Item top = stack.getStackItem(0);
        OpcodeStack.Item next = stack.getStackItem(1);

        if (possibleOverwrite != null && possibleOverwrite.equals(getXFieldOperand())) {
            bugReporter.reportBug(new BugInstance(this, "SA_FIELD_SELF_ASSIGNMENT", Priorities.HIGH_PRIORITY).addClassAndMethod(this)
                    .addReferencedField(this).addSourceLine(this));

        }
        possibleOverwrite = null;

        if (stack.getStackDepth() >= 4 && getNextOpcode() == Const.PUTFIELD) {
            OpcodeStack.Item third = stack.getStackItem(2);
            OpcodeStack.Item fourth = stack.getStackItem(3);
            XField f2 = third.getXField();
            int registerNumber2 = fourth.getRegisterNumber();
            if (f2 != null && f2.equals(getXFieldOperand()) && registerNumber2 >= 0
                    && registerNumber2 == third.getFieldLoadedFromRegister()
                    && !third.sameValue(top) && (third.getPC() == -1 || third.getPC() > lastMethodCall)) {
                possibleOverwrite = f2;
            }
        }

        XField f = top.getXField();
        int registerNumber = next.getRegisterNumber();
        if (f != null && f.equals(getXFieldOperand()) && registerNumber >= 0
                && registerNumber == top.getFieldLoadedFromRegister() && (top.getPC() == -1 || top.getPC() > lastMethodCall)) {
            int priority = NORMAL_PRIORITY;

            LocalVariableAnnotation possibleMatch = LocalVariableAnnotation.findMatchingIgnoredParameter(getClassContext(),
                    getMethod(), getNameConstantOperand(), getSigConstantOperand());
            if (possibleMatch != null) {
                priority--;
            } else {
                possibleMatch = LocalVariableAnnotation.findUniqueBestMatchingParameter(getClassContext(), getMethod(),
                        getNameConstantOperand(), getSigConstantOperand());
            }
            if (possibleMatch == null) {
                String signature = stack.getLVValue(registerNumber).getSignature();
                for (int i = 0; i < stack.getNumLocalValues(); i++) {
                    if (i != register) {
                        Item lvValue = stack.getLVValue(i);
                        if (lvValue != null && lvValue.getSignature().equals(signature)) {
                            priority--;
                            break;
                        }
                    }
                }
            }

            bugReporter.reportBug(new BugInstance(this, "SA_FIELD_SELF_ASSIGNMENT", priority).addClassAndMethod(this)
                    .addReferencedField(this).addOptionalAnnotation(possibleMatch).addSourceLine(this));

        }
    } else {
        possibleOverwrite = null;
    }
    if (isMethodCall()) {
        lastMethodCall = getPC();
    }
    switch (state) {
    case 0:
        if (seen == Const.DUP) {
            state = 6;
        }
        break;
    case 6:
        if (isRegisterStore()) {
            state = 7;
            register = getRegisterOperand();
        } else {
            state = 0;
        }
        break;
    case 7:
        if (isRegisterStore() && register == getRegisterOperand()) {
            bugReporter.reportBug(new BugInstance(this, "SA_LOCAL_DOUBLE_ASSIGNMENT", NORMAL_PRIORITY)
                    .addClassAndMethod(this)
                    .add(LocalVariableAnnotation.getLocalVariableAnnotation(getMethod(), register, getPC(), getPC() - 1))
                    .addSourceLine(this));
        }
        state = 0;
        break;
    default:
        break;
    }

}
 
Example 6
Source File: SynchronizeAndNullCheckField.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    // System.out.println(getPC() + " " + Const.getOpcodeName(seen) + " " +
    // currState);
    switch (currState) {
    case 0:
        if (seen == Const.GETFIELD || seen == Const.GETSTATIC) {
            syncField = FieldAnnotation.fromReferencedField(this);
            currState = 1;
        }
        break;
    case 1:
        if (seen == Const.DUP) {
            currState = 2;
        } else {
            currState = 0;
        }
        break;
    case 2:
        if (seen == Const.ASTORE || seen == Const.ASTORE_0 || seen == Const.ASTORE_1 || seen == Const.ASTORE_2 || seen == Const.ASTORE_3) {
            currState = 3;
        } else {
            currState = 0;
        }
        break;
    case 3:
        if (seen == Const.MONITORENTER) {
            currState = 4;
        } else {
            currState = 0;
        }
        break;
    case 4:
        if (seen == Const.GETFIELD || seen == Const.GETSTATIC) {
            gottenField = FieldAnnotation.fromReferencedField(this);
            currState = 5;
        } else {
            currState = 0;
        }
        break;
    case 5:
        if ((seen == Const.IFNONNULL || seen == Const.IFNULL) && gottenField.equals(syncField)) {
            BugInstance bug = new BugInstance(this, "NP_SYNC_AND_NULL_CHECK_FIELD", NORMAL_PRIORITY).addClass(this)
                    .addMethod(this).addField(syncField).addSourceLine(this);
            bugReporter.reportBug(bug);
        } else {
            currState = 0;
        }
        break;
    default:
        currState = 0;
    }
}
 
Example 7
Source File: VarArgsProblems.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    // System.out.println("State:" + state);
    if (seen == Const.GOTO && getBranchOffset() == 4) {
        state = SEEN_GOTO;
    } else {
        switch (state) {
        case SEEN_NOTHING:
            if ((seen == Const.ICONST_1)) {
                state = SEEN_ICONST_1;
            }
            break;

        case SEEN_ICONST_1:
            if (seen == Const.ANEWARRAY && primitiveArray.matcher(getClassConstantOperand()).matches()) {
                // System.out.println("Allocation of array of type " +
                // getClassConstantOperand());
                primitiveArraySig = getClassConstantOperand();
                state = SEEN_ANEWARRAY;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ANEWARRAY:
            if (seen == Const.DUP) {
                state = SEEN_DUP;
            } else {
                state = SEEN_NOTHING;
            }
            break;
        case SEEN_DUP:
            if (seen == Const.ICONST_0) {
                state = SEEN_ICONST_0;
            } else {
                state = SEEN_NOTHING;
            }
            break;
        case SEEN_ICONST_0:
            if (((seen >= Const.ALOAD_0) && (seen < Const.ALOAD_3)) || (seen == Const.ALOAD)) {
                state = SEEN_ALOAD;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ALOAD:
            if (seen == Const.AASTORE) {
                state = SEEN_AASTORE;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_AASTORE:
            if (seen == Const.INVOKESTATIC || seen == Const.INVOKEINTERFACE || seen == Const.INVOKESPECIAL || seen == Const.INVOKEVIRTUAL) {
                // System.out.println(getClassConstantOperand());
                // System.out.println(getNameConstantOperand());
                // System.out.println(getSigConstantOperand());
                if (getSigConstantOperand().indexOf("Ljava/lang/Object;)") == -1) {
                    break;
                }
                int priority = NORMAL_PRIORITY;
                if ("asList".equals(getNameConstantOperand()) && "java/util/Arrays".equals(getClassConstantOperand())) {
                    priority = HIGH_PRIORITY;
                }
                bugReporter.reportBug(new BugInstance(this, "VA_PRIMITIVE_ARRAY_PASSED_TO_OBJECT_VARARG", priority)
                        .addClassAndMethod(this).addType(primitiveArraySig).describe(TypeAnnotation.FOUND_ROLE)
                        .addCalledMethod(this).addSourceLine(this));
            }
            state = SEEN_NOTHING;
            break;

        case SEEN_GOTO:
            state = SEEN_NOTHING;
            break;
        default:
            throw new IllegalStateException("State " + state + " not expected");

        }
    }
}
 
Example 8
Source File: FormatStringChecker.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    // System.out.println(getPC() + " " + Const.getOpcodeName(seen) + " " + state);

    if (stack.getStackDepth() < stackDepth) {
        state = FormatState.NONE;
        stackDepth = 0;
        arguments = null;
    }
    if (seen == Const.ANEWARRAY && stack.getStackDepth() >= 2) {
        Object size = stack.getStackItem(0).getConstant();
        Object formatStr = stack.getStackItem(1).getConstant();
        if (size instanceof Integer && formatStr instanceof String) {
            arguments = new OpcodeStack.Item[(Integer) size];
            this.formatString = (String) formatStr;
            state = FormatState.READY_FOR_FORMAT;
            stackDepth = stack.getStackDepth();
        }
    } else if (state == FormatState.READY_FOR_FORMAT && seen == Const.DUP) {
        state = FormatState.EXPECTING_ASSIGNMENT;
    } else if (state == FormatState.EXPECTING_ASSIGNMENT && stack.getStackDepth() == stackDepth + 3 && seen == Const.AASTORE) {
        Object pos = stack.getStackItem(1).getConstant();
        OpcodeStack.Item value = stack.getStackItem(0);
        if (pos instanceof Integer) {
            int index = (Integer) pos;
            if (index >= 0 && index < arguments.length) {
                arguments[index] = value;
                state = FormatState.READY_FOR_FORMAT;
            } else {
                state = FormatState.NONE;
            }
        } else {
            state = FormatState.NONE;
        }
    } else if (state == FormatState.READY_FOR_FORMAT
            && (seen == Const.INVOKESPECIAL || seen == Const.INVOKEVIRTUAL || seen == Const.INVOKESTATIC || seen == Const.INVOKEINTERFACE)
            && stack.getStackDepth() == stackDepth) {

        String cl = getClassConstantOperand();
        String nm = getNameConstantOperand();
        String sig = getSigConstantOperand();
        XMethod m = getXMethodOperand();
        if ((m == null || m.isVarArgs())
                && sig.indexOf("Ljava/lang/String;[Ljava/lang/Object;)") >= 0
                && ("java/util/Formatter".equals(cl) && "format".equals(nm) || "java/lang/String".equals(cl)
                        && "format".equals(nm) || "java/io/PrintStream".equals(cl) && "format".equals(nm)
                        || "java/io/PrintStream".equals(cl) && "printf".equals(nm) || cl.endsWith("Writer")
                                && "format".equals(nm) || cl.endsWith("Writer") && "printf".equals(nm)) || cl.endsWith("Logger")
                                        && nm.endsWith("fmt")) {

            if (formatString.indexOf('\n') >= 0) {
                bugReporter.reportBug(new BugInstance(this, "VA_FORMAT_STRING_USES_NEWLINE", NORMAL_PRIORITY)
                        .addClassAndMethod(this).addCalledMethod(this).addString(formatString)
                        .describe(StringAnnotation.FORMAT_STRING_ROLE).addSourceLine(this));
            }
        }

    }
}