Refine search
private boolean hasHardCodedStackItem(int stackOffset) { return stack.getStackItem(stackOffset).getUserValue() != null; }
/** * looks to see if the string used in a equals or compareTo is the same as that of a switch statement's switch on string. * * @return if the string is used in a switch */ private boolean lookupSwitchOnString() { if (stack.getStackDepth() > 1) { OpcodeStack.Item item = stack.getStackItem(1); String stringRef = (String) item.getUserValue(); if (stringRef == null) { return false; } if (!lookupSwitches.isEmpty()) { LookupDetails details = lookupSwitches.get(lookupSwitches.size() - 1); return stringRef.equals(details.getStringReference()); } } return true; }
/** * looks to see if the string used in a equals or compareTo is the same as that of a switch statement's switch on string. * * @return if the string is used in a switch */ private boolean lookupSwitchOnString() { if (stack.getStackDepth() > 1) { OpcodeStack.Item item = stack.getStackItem(1); String stringRef = (String) item.getUserValue(); if (stringRef == null) { return false; } if (!lookupSwitches.isEmpty()) { LookupDetails details = lookupSwitches.get(lookupSwitches.size() - 1); return stringRef.equals(details.getStringReference()); } } return true; }
private void checkStoredInstance(OpcodeStack stack) { Item storedValue = stack.getStackItem(0); Item arrayIndexItem = stack.getStackItem(1); Item targetArray = stack.getStackItem(2); Object arrayIndex = arrayIndexItem.getConstant(); if (arrayIndex instanceof Number) { ArrayData data = (ArrayData) targetArray.getUserValue(); Number index = (Number) arrayIndex; store(storedValue, data, index.intValue()); } }
/** * called before the execution of the parent OpcodeStack.sawOpcode() to save user values if the opcode is a GOTO or GOTO_W. * * @param stack * the OpcodeStack with the items containing user values * @param opcode * the opcode currently seen */ public static void pre(OpcodeStack stack, int opcode) { if (sawGOTO) { return; } sawGOTO = (opcode == Const.GOTO) || (opcode == Const.GOTO_W); if (sawGOTO) { int depth = stack.getStackDepth(); if (depth > 0) { userValues.clear(); for (int i = 0; i < depth; i++) { OpcodeStack.Item item = stack.getStackItem(i); userValues.add(item.getUserValue()); } } } }
private boolean hasHardCodedStackItem(int stackOffset) { return stack.getStackItem(stackOffset).getUserValue() != null; }
/** * called before the execution of the parent OpcodeStack.sawOpcode() to save * user values if the opcode is a GOTO or GOTO_W. * * @param stack * the OpcodeStack with the items containing user values * @param opcode * the opcode currently seen */ public static void pre(OpcodeStack stack, int opcode) { if (sawGOTO) { return; } sawGOTO = (opcode == Constants.GOTO) || (opcode == Constants.GOTO_W); if (sawGOTO) { int depth = stack.getStackDepth(); if (depth > 0) { userValues.clear(); for (int i = 0; i < depth; i++) { OpcodeStack.Item item = stack.getStackItem(i); userValues.add(item.getUserValue()); } } } }
/** * returns the number of parameters slf4j or log4j2 is expecting to inject into the format string * * @param signature * the method signature of the error, warn, info, debug statement * @return the number of expected parameters */ @SuppressWarnings("unchecked") private int getVarArgsParmCount(String signature) { if (SignatureBuilder.SIG_STRING_AND_OBJECT_TO_VOID.equals(signature)) { return 1; } if (SIG_STRING_AND_TWO_OBJECTS_TO_VOID.equals(signature)) { return 2; } OpcodeStack.Item item = stack.getStackItem(0); LOUserValue<Integer> uv = (LOUserValue<Integer>) item.getUserValue(); if ((uv != null) && (uv.getType() == LOUserValue.LOType.ARRAY_SIZE)) { Integer size = uv.getValue(); if (size != null) { return Math.abs(size.intValue()); } } return -1; }
private boolean processInvokeVirtual() { String clsName = getClassConstantOperand(); if (Values.SLASHED_JAVA_LANG_CLASS.equals(clsName)) { String methodName = getNameConstantOperand(); if ("getResource".equals(methodName)) { return true; } } else if ("java/net/URL".equals(clsName)) { String methodName = getNameConstantOperand(); if (("toURI".equals(methodName) || "getFile".equals(methodName)) && (stack.getStackDepth() > 0) && (stack.getStackItem(0).getUserValue() != null)) { return true; } } return false; }
/** * returns the number of parameters slf4j or log4j2 is expecting to inject into the format string * * @param signature * the method signature of the error, warn, info, debug statement * @return the number of expected parameters */ @SuppressWarnings("unchecked") private int getVarArgsParmCount(String signature) { if (SignatureBuilder.SIG_STRING_AND_OBJECT_TO_VOID.equals(signature)) { return 1; } if (SIG_STRING_AND_TWO_OBJECTS_TO_VOID.equals(signature)) { return 2; } OpcodeStack.Item item = stack.getStackItem(0); LOUserValue<Integer> uv = (LOUserValue<Integer>) item.getUserValue(); if ((uv != null) && (uv.getType() == LOUserValue.LOType.ARRAY_SIZE)) { Integer size = uv.getValue(); if (size != null) { return Math.abs(size.intValue()); } } return -1; }
private void processAStore(int seen) { if (stack.getStackDepth() > 0) { OpcodeStack.Item item = stack.getStackItem(0); Integer allocation = (Integer) item.getUserValue(); if (allocation != null) { Integer reg = Integer.valueOf(RegisterUtils.getAStoreReg(this, seen)); if (isFirstUse(reg.intValue())) { if (storedAllocations.values().contains(allocation)) { allocations.remove(allocation); storedAllocations.remove(reg); } else if (storedAllocations.containsKey(reg)) { allocations.remove(allocation); allocation = storedAllocations.remove(reg); allocations.remove(allocation); } else { storedAllocations.put(reg, allocation); } } else { item.setUserValue(null); allocations.remove(allocation); } } } }
@Override public void sawOpcode(int code) { if (code == INVOKESTATIC && Objects.equal("org/slf4j/LoggerFactory", getClassConstantOperand()) && Objects.equal("getLogger", getNameConstantOperand()) && Objects.equal("(Ljava/lang/Class;)Lorg/slf4j/Logger;", getSigConstantOperand())) { final Item passedItem = getStack().getStackItem(0); final Object userValue = passedItem.getUserValue(); if (userValue instanceof JavaType) { verifyPassedClassToGetLoggerMethod((JavaType) userValue); } } }
private boolean processInvokeVirtual() { String clsName = getClassConstantOperand(); if (Values.SLASHED_JAVA_LANG_CLASS.equals(clsName)) { String methodName = getNameConstantOperand(); if ("getResource".equals(methodName)) { return true; } } else if ("java/net/URL".equals(clsName)) { String methodName = getNameConstantOperand(); if (("toURI".equals(methodName) || "getFile".equals(methodName)) && (stack.getStackDepth() > 0) && (stack.getStackItem(0).getUserValue() != null)) { return true; } } return false; }
int countParameter(OpcodeStack stack, String methodSignature, ThrowableHandler throwableHandler) { String[] signatures = splitSignature(methodSignature); if (Objects.equal(signatures[signatures.length - 1], "[Ljava/lang/Object;")) { ArrayData arrayData = (ArrayData) stack.getStackItem(0).getUserValue(); if (arrayData == null || arrayData.getSize() < 0) { throw new IllegalStateException("no array initializer found"); } int parameterCount = arrayData.getSize(); if (arrayData.isMarked()) { --parameterCount; } return parameterCount; } int parameterCount = signatures.length - 1; // -1 means 'formatString' is not parameter if (Objects.equal(signatures[0], "Lorg/slf4j/Marker;")) { --parameterCount; } Item lastItem = stack.getStackItem(0); if (throwableHandler.checkThrowable(lastItem)) { --parameterCount; } return parameterCount; }
private void processAStore(int seen) { if (stack.getStackDepth() > 0) { OpcodeStack.Item item = stack.getStackItem(0); Integer allocation = (Integer) item.getUserValue(); if (allocation != null) { Integer reg = Integer.valueOf(RegisterUtils.getAStoreReg(this, seen)); if (isFirstUse(reg.intValue())) { if (storedAllocations.values().contains(allocation)) { allocations.remove(allocation); storedAllocations.remove(reg); } else if (storedAllocations.containsKey(reg)) { allocations.remove(allocation); allocation = storedAllocations.remove(reg); allocations.remove(allocation); } else { storedAllocations.put(reg, allocation); } } else { item.setUserValue(null); allocations.remove(allocation); } } } }
@Override public void sawOpcode(int seen) { if (seen == INVOKEVIRTUAL) { if ("getName".equals(getNameConstantOperand()) && SignatureBuilder.SIG_VOID_TO_STRING.equals(getSigConstantOperand()) && Values.SLASHED_JAVA_LANG_CLASS.equals(getClassConstantOperand())) { flag = true; } else if ("equals".equals(getNameConstantOperand()) && SignatureBuilder.SIG_OBJECT_TO_BOOLEAN.equals(getSigConstantOperand()) && Values.SLASHED_JAVA_LANG_STRING.equals(getClassConstantOperand())) { Item item = stack.getItemMethodInvokedOn(this); Object srcValue = item.getUserValue(); item = stack.getStackItem(0); Object dstValue = item.getUserValue(); if (Boolean.TRUE.equals(srcValue) && Boolean.TRUE.equals(dstValue)) { bugReporter.reportBug(new BugInstance(this, BugType.CCNE_COMPARE_CLASS_EQUALS_NAME.name(), NORMAL_PRIORITY).addClass(this).addMethod(this) .addSourceLine(this)); } } } } }
/** * returns whether the item at the stackpos location isn't an enum collection but could be * * @param stackPos * the position on the opstack to check * * @return whether the collection should be converted to an enum collection */ private boolean couldBeEnumCollection(int stackPos) { if (stack.getStackDepth() <= stackPos) { return false; } OpcodeStack.Item item = stack.getStackItem(stackPos); CollectionType userValue = (CollectionType) item.getUserValue(); if (userValue != null) { return userValue == CollectionType.REGULAR; } String realClass = item.getSignature(); return "Ljava/util/HashSet;".equals(realClass) || "Ljava/util/HashMap;".equals(realClass); }
@Override public void sawOpcode(int seen) { if (seen == Const.INVOKEVIRTUAL) { if ("getName".equals(getNameConstantOperand()) && SignatureBuilder.SIG_VOID_TO_STRING.equals(getSigConstantOperand()) && Values.SLASHED_JAVA_LANG_CLASS.equals(getClassConstantOperand())) { flag = true; } else if ("equals".equals(getNameConstantOperand()) && SignatureBuilder.SIG_OBJECT_TO_BOOLEAN.equals(getSigConstantOperand()) && Values.SLASHED_JAVA_LANG_STRING.equals(getClassConstantOperand())) { Item item = stack.getItemMethodInvokedOn(this); Object srcValue = item.getUserValue(); item = stack.getStackItem(0); Object dstValue = item.getUserValue(); if (Boolean.TRUE.equals(srcValue) && Boolean.TRUE.equals(dstValue)) { bugReporter.reportBug(new BugInstance(this, BugType.CCNE_COMPARE_CLASS_EQUALS_NAME.name(), NORMAL_PRIORITY).addClass(this).addMethod(this) .addSourceLine(this)); } } } } }
/** * called after the execution of the parent OpcodeStack.sawOpcode, to restore the user values after the GOTO or GOTO_W's mergeJumps were processed * * @param stack * the OpcodeStack with the items containing user values * @param opcode * the opcode currently seen */ public static void post(OpcodeStack stack, int opcode) { if (!sawGOTO || (opcode == Const.GOTO) || (opcode == Const.GOTO_W)) { return; } int depth = stack.getStackDepth(); for (int i = 0; i < depth && i < userValues.size(); i++) { OpcodeStack.Item item = stack.getStackItem(i); if (item.getUserValue() == null) { item.setUserValue(userValues.get(i)); } } userValues.clear(); sawGOTO = false; } }
@Nullable private ArrayData findArrayData(String signature) { int stackIndex = indexOf(signature, "[Ljava/lang/Object;"); if (stackIndex == -1) { // generate ArrayData from items in stack String[] signatures = splitSignature(signature); int parameterCount = signatures.length - 1; // 1 means format is not a parameter if (signatures[0].equals("Lorg/slf4j/Marker;")) { --parameterCount; } ArrayData data = new ArrayData(parameterCount); for (int i = 0; i < data.getSize(); ++i) { Item item = getStack().getStackItem(i); arrayDataHandler.store(item, data, parameterCount - 1 - i); } return data; } else { Object userValue = getStack().getStackItem(stackIndex).getUserValue(); if (userValue instanceof ArrayData) { return (ArrayData) userValue; } } return null; }