public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg, RepositoryLookupFailureCallback lookupFailureCallback) { Instruction ins = location.getHandle().getInstruction(); try { if (ins instanceof InvokeInstruction) { if (!Hierarchy.isSubtype(type, baseClassType)) return null; Stream stream = new Stream(location, type.getClassName(), baseClassType.getClassName()) .setIsOpenOnCreation(true) .setIgnoreImplicitExceptions(true); if (bugType != null) stream.setInteresting(bugType); return stream; } } catch (ClassNotFoundException e) { lookupFailureCallback.reportMissingClass(e); } return null; }
public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg, RepositoryLookupFailureCallback lookupFailureCallback) { Instruction ins = location.getHandle().getInstruction(); if (ins.getOpcode() != Constants.GETSTATIC) return null; GETSTATIC getstatic = (GETSTATIC) ins; if (!className.equals(getstatic.getClassName(cpg)) || !fieldName.equals(getstatic.getName(cpg)) || !fieldSig.equals(getstatic.getSignature(cpg))) return null; return new Stream(location, type.getClassName(), streamBaseClass) .setIgnoreImplicitExceptions(true) .setIsOpenOnCreation(true); }
public String toString() { StringBuffer buf = new StringBuffer(); buf.append('{'); boolean first = true; for (ThrownExceptionIterator i = iterator(); i.hasNext();) { ObjectType type = i.next(); if (first) first = false; else buf.append(','); boolean implicit = !i.isExplicit(); if (implicit) buf.append('['); buf.append(type.toString()); if (implicit) buf.append(']'); } buf.append('}'); return buf.toString(); }
public void transfer(BasicBlock basicBlock, InstructionHandle end, BlockType start, BlockType result) throws DataflowAnalysisException { result.copyFrom(start); if (start.isValid()) { if (basicBlock.isExceptionHandler()) { CodeExceptionGen exceptionGen = basicBlock.getExceptionGen(); ObjectType catchType = exceptionGen.getCatchType(); if (catchType == null) { // Probably a finally block, or a synchronized block // exception-compensation catch block. result.pushFinally(); } else { // Catch type was explicitly specified: // this is probably a programmer-written catch block result.pushCatch(); } } } }
@Override public void visitCHECKCAST(CHECKCAST obj) { // cast to a safe object type ObjectType objectType = obj.getLoadClassType(cpg); if (objectType == null) { return; } String objectTypeSignature = objectType.getSignature(); if(!taintConfig.isClassTaintSafe(objectTypeSignature)) { return; } try { getFrame().popValue(); pushSafe(); } catch (DataflowAnalysisException ex) { throw new InvalidBytecodeException("empty stack for checkcast", ex); } }
private static boolean isIllegalFinalType(Type type, ClassContext classContext) { if (type instanceof ObjectType) { try { String className = ((ObjectType) type).getClassName(); if (className.startsWith("java.")) { // Types in java.lang are final for security reasons. return false; } JavaClass cls = classContext.getAnalysisContext().lookupClass(className); return cls.isFinal() && !cls.isEnum(); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } return false; }
/** * Creates a method class$(String) which is used * during SomeClass.class instruction * * @param generatedClassName the instance class name */ protected void createHelperMethodForDotClassCalls(String generatedClassName) { InstructionList il = new InstructionList(); MethodGen method = new MethodGen(Constants.ACC_STATIC, new ObjectType("java.lang.Class"), new Type[]{Type.STRING}, new String[]{"arg0"}, "class$", generatedClassName, il, constantsPool); InstructionHandle ih0 = il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); il.append(factory.createInvoke("java.lang.Class", "forName", new ObjectType("java.lang.Class"), new Type[]{Type.STRING}, Constants.INVOKESTATIC)); InstructionHandle ih4 = il.append(InstructionFactory.createReturn(Type.OBJECT)); InstructionHandle ih5 = il.append(InstructionFactory.createStore(Type.OBJECT, 1)); il.append(factory.createNew("java.lang.NoClassDefFoundError")); il.append(InstructionConstants.DUP); il.append(InstructionFactory.createLoad(Type.OBJECT, 1)); il.append(factory.createInvoke("java.lang.Throwable", "getMessage", Type.STRING, Type.NO_ARGS, Constants.INVOKEVIRTUAL)); il.append(factory.createInvoke("java.lang.NoClassDefFoundError", "<init>", Type.VOID, new Type[]{Type.STRING}, Constants.INVOKESPECIAL)); il.append(InstructionConstants.ATHROW); method.addExceptionHandler(ih0, ih4, ih5, new ObjectType("java.lang.ClassNotFoundException")); method.setMaxStack(); method.setMaxLocals(); classGen.addMethod(method.getMethod()); il.dispose(); }
private void generateEqualsMethod(String generatedClassName) { /* public boolean equals(Object o) { * return stubHelper.isEquals(this,o); * } */ InstructionList il = new InstructionList(); MethodGen method = new MethodGen(Constants.ACC_PUBLIC, Type.BOOLEAN, new Type[]{Type.OBJECT}, new String[]{"arg0"}, "equals", generatedClassName, il, constantsPool); il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); il.append(factory.createFieldAccess(generatedClassName, "stubHelper", new ObjectType("org.codehaus.jremoting.client.StubHelper"), Constants.GETFIELD)); il.append(InstructionFactory.createLoad(Type.OBJECT, 0)); il.append(InstructionFactory.createLoad(Type.OBJECT, 1)); il.append(factory.createInvoke("org.codehaus.jremoting.client.StubHelper", "isEquals", Type.BOOLEAN, new Type[]{Type.OBJECT, Type.OBJECT}, Constants.INVOKEINTERFACE)); il.append(InstructionFactory.createReturn(Type.INT)); method.setMaxStack(); method.setMaxLocals(); classGen.addMethod(method.getMethod()); il.dispose(); }
/** * Method getBCELType. * Maps the java datatype and the BCEL datatype * * @param clazz the class * @return Type the type */ protected Type getBCELType(Class clazz) { if (clazz.isPrimitive()) { return getBCELPrimitiveType(clazz.getName()); } else if (!clazz.isArray()) { return new ObjectType(clazz.getName()); } else { String className = clazz.getName(); int index = className.lastIndexOf('['); int arrayDepth = className.indexOf('[') - className.lastIndexOf('[') + 1; if (className.charAt(index + 1) == 'L') { return new ArrayType(new ObjectType(clazz.getComponentType().getName()), arrayDepth); } return new ArrayType(getBCELPrimitiveType(className.substring(arrayDepth)), arrayDepth); } }
public void transfer(BasicBlock basicBlock, @CheckForNull InstructionHandle end, BlockType start, BlockType result) throws DataflowAnalysisException { result.copyFrom(start); if (start.isValid()) { if (basicBlock.isExceptionHandler()) { CodeExceptionGen exceptionGen = basicBlock.getExceptionGen(); ObjectType catchType = exceptionGen.getCatchType(); if (catchType == null) { // Probably a finally block, or a synchronized block // exception-compensation catch block. result.pushFinally(); } else { // Catch type was explicitly specified: // this is probably a programmer-written catch block result.pushCatch(); } } } }
public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg, RepositoryLookupFailureCallback lookupFailureCallback) { Instruction ins = location.getHandle().getInstruction(); try { if (ins instanceof InvokeInstruction) { if (!Hierarchy.isSubtype(type, baseClassType)) return null; Stream stream = new Stream(location, type.getClassName(), baseClassType.getClassName()).setIsOpenOnCreation(true) .setIgnoreImplicitExceptions(true); if (bugType != null) stream.setInteresting(bugType); return stream; } } catch (ClassNotFoundException e) { lookupFailureCallback.reportMissingClass(e); } return null; }
/** * Get array of Obligation types corresponding to the parameters of the * given method. * * @param xmethod * a method * @return array of Obligation types for each of the method's parameters; a * null element means the corresponding parameter is not an * Obligation type */ public Obligation[] getParameterObligationTypes(XMethod xmethod) { Type[] paramTypes = Type.getArgumentTypes(xmethod.getSignature()); Obligation[] result = new Obligation[paramTypes.length]; for (int i = 0; i < paramTypes.length; i++) { if (!(paramTypes[i] instanceof ObjectType)) { continue; } try { result[i] = getObligationByType((ObjectType) paramTypes[i]); } catch (ClassNotFoundException e) { Global.getAnalysisCache().getErrorLogger().reportMissingClass(e); } } return result; }
protected void addFindMethod(ParsedMethod m) { GeneratedMethod gm = new GeneratedMethod(m); InstructionList il = gm.start(); writeMethodPreamble(gm, il); il.append(new PUSH(_cp, (ObjectType) gm.getReturnType())); m.getArguments()[0].pushAsObject(il); il.append(_factory.createInvoke(EM_TYPE, "find", Type.OBJECT, new Type[] { Type.CLASS, Type.OBJECT }, Constants.INVOKEINTERFACE)); il.append(_factory.createCheckCast(((ReferenceType) gm.getReturnType()))); il.append(InstructionFactory.createReturn(gm.getReturnType())); gm.done(); }
public static ObjectType getInstance(@DottedClassName String s) { if (FindBugs.DEBUG && s.startsWith("[")) { throw new IllegalArgumentException("Cannot create an ObjectType to represent an array type: " + s); } if (s.endsWith(";")) throw new IllegalArgumentException(s); if (s.indexOf("/") >= 0) { s = s.replace('/', '.'); } Map<String, ObjectType> map = instance.get(); ObjectType result = map.get(s); if (result != null) return result; result = ObjectType.getInstance(s); map.put(s, result); return result; }
/** * Determine whether or not a given ObjectType is a subtype of another. * Throws ClassNotFoundException if the question cannot be answered * definitively due to a missing class. * * @param type * a ReferenceType * @param possibleSupertype * another Reference type * @return true if <code>type</code> is a subtype of * <code>possibleSupertype</code>, false if not * @throws ClassNotFoundException * if a missing class prevents a definitive answer */ public boolean isSubtype(ObjectType type, ObjectType possibleSupertype) throws ClassNotFoundException { if (DEBUG_QUERIES) { System.out.println("isSubtype: check " + type + " subtype of " + possibleSupertype); } if (type.equals(possibleSupertype)) { if (DEBUG_QUERIES) { System.out.println(" ==> yes, types are same"); } return true; } ClassDescriptor typeClassDescriptor = DescriptorFactory.getClassDescriptor(type); ClassDescriptor possibleSuperclassClassDescriptor = DescriptorFactory.getClassDescriptor(possibleSupertype); return isSubtype(typeClassDescriptor, possibleSuperclassClassDescriptor); }
/** * Find the declared exceptions for the method called by given instruction. * * @param inv * the InvokeInstruction * @param cpg * the ConstantPoolGen used by the class the InvokeInstruction * belongs to * @return array of ObjectTypes of thrown exceptions, or null if we can't * find the method implementation */ public static @CheckForNull ObjectType[] findDeclaredExceptions(InvokeInstruction inv, ConstantPoolGen cpg) { XMethod method = findInvocationLeastUpperBound(inv, cpg, inv instanceof INVOKESTATIC ? Hierarchy.STATIC_METHOD : Hierarchy.INSTANCE_METHOD); if (method == null) return null; String[] exceptions = method.getThrownExceptions(); if (exceptions == null) return new ObjectType[0]; ObjectType[] result = new ObjectType[exceptions.length]; for (int i = 0; i < exceptions.length; ++i) { result[i] = ObjectTypeFactory.getInstance(ClassName.toDottedClassName(exceptions[i])); } return result; }
@Override public String toString() { StringBuilder buf = new StringBuilder(); buf.append('{'); boolean first = true; for (ThrownExceptionIterator i = iterator(); i.hasNext();) { ObjectType type = i.next(); if (first) first = false; else buf.append(','); boolean implicit = !i.isExplicit(); if (implicit) buf.append('['); buf.append(type.toString()); if (implicit) buf.append(']'); } buf.append('}'); return buf.toString(); }
protected void processQuerySetOperation(ParsedMethod m, Argument arg, InstructionList il, String methodName, String queryClass) { Class<?> type = arg.getArgumentClass(); if (type == Integer.class) { arg.push(il); il.append(_factory.createInvoke(INT_CLASS, "intValue", Type.INT, Type.NO_ARGS, Constants.INVOKEINTERFACE)); } else if (type == int.class) { arg.pushPrimitive(il); } else { throw new IllegalArgumentException("Parameter #" + arg.getArgNo() + " of " + m.getMethod() + " needs to be Integer or int."); } il.append(_factory .createInvoke(queryClass, methodName, new ObjectType(queryClass), new Type[] { Type.INT }, Constants.INVOKEINTERFACE)); }
public static CodeExceptionGen merge(@CheckForNull TypeMerger m, CodeExceptionGen e1, CodeExceptionGen e2) { if (e1 == null) return e2; if (e2 == null) return e1; if (m == null) return e1; if ( ! e1.getHandlerPC().equals( e2.getHandlerPC() ) ){ // log error return e1; } try { Type t = m.mergeTypes(e1.getCatchType(), e2.getCatchType()); return new CodeExceptionGen(e1.getStartPC(), e1.getEndPC(), e1.getHandlerPC(), (ObjectType) t); } catch (DataflowAnalysisException e) { // TODO Auto-generated catch block e.printStackTrace(); return e1; } }
public IOStreamFactory(String baseClass, String[] uninterestingSubclassList, String bugType) { this.baseClassType = new ObjectType(baseClass); this.uninterestingSubclassTypeList = new ObjectType[uninterestingSubclassList.length]; for (int i = 0; i < uninterestingSubclassList.length; ++i) { this.uninterestingSubclassTypeList[i] = new ObjectType(uninterestingSubclassList[i]); } this.bugType = bugType; }
public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg, RepositoryLookupFailureCallback lookupFailureCallback) { try { Instruction ins = location.getHandle().getInstruction(); if (ins.getOpcode() != Constants.NEW) return null; if (Hierarchy.isSubtype(type, baseClassType)) { boolean isUninteresting = false; for (int i = 0; i < uninterestingSubclassTypeList.length; ++i) { if (Hierarchy.isSubtype(type, uninterestingSubclassTypeList[i])) { isUninteresting = true; break; } } Stream result = new Stream(location, type.getClassName(), baseClassType.getClassName()) .setIgnoreImplicitExceptions(true); if (!isUninteresting) result.setInteresting(bugType); return result; } } catch (ClassNotFoundException e) { lookupFailureCallback.reportMissingClass(e); } return null; }
public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg, RepositoryLookupFailureCallback lookupFailureCallback) { Instruction ins = location.getHandle().getInstruction(); if (ins.getOpcode() != Constants.GETFIELD) return null; String fieldClass = type.getClassName(); try { if (fieldClass.startsWith("[")) return null; if (!Hierarchy.isSubtype(fieldClass, streamBaseClass)) return null; Stream stream = new Stream(location, fieldClass, streamBaseClass); stream.setIsOpenOnCreation(true); stream.setOpenLocation(location); if (bugPatternType != null) stream.setInteresting(bugPatternType); //System.out.println("Instance field stream at " + location); return stream; } catch (ClassNotFoundException e) { lookupFailureCallback.reportMissingClass(e); return null; } }
/** * Default implementation of merging reference types. * This just returns the first common superclass, which is compliant * with the JVM Spec. Subclasses may override this method * in order to implement extended type rules. * * @param aRef a ReferenceType * @param bRef a ReferenceType * @return the merged Type */ protected Type mergeReferenceTypes(ReferenceType aRef, ReferenceType bRef) throws DataflowAnalysisException { // Two concrete object types. // According to the JVM spec, 2nd edition, 4.9.2, // the result of merging types is the "first common superclass". // Interfaces are NOT considered! // This will use the Repository to look up classes. try { // Special case: ExceptionObjectTypes. // We want to preserve the ExceptionSets associated, // in order to track the exact set of exceptions if (isObjectType(aRef.getType()) && isObjectType(bRef.getType()) && (aRef.getType() == T_EXCEPTION || bRef.getType() == T_EXCEPTION)) { ExceptionSet union = exceptionSetFactory.createExceptionSet(); updateExceptionSet(union, (ObjectType) aRef); updateExceptionSet(union, (ObjectType) bRef); return ExceptionObjectType.fromExceptionSet(union); } return aRef.getFirstCommonSuperclass(bRef); } catch (ClassNotFoundException e) { lookupFailureCallback.reportMissingClass(e); throw new DataflowAnalysisException("Repository lookup failure: " + e.toString(), e); } }
int getIndexOfType(ObjectType type) { Integer index = typeIndexMap.get(type); if (index == null) { index = new Integer(getNumTypes()); typeList.add(type); typeIndexMap.put(type, index); } return index.intValue(); }
/** * Initialize object from an exception set. * * @param exceptionSet the exception set * @return a Type that is a supertype of all of the exceptions in * the exception set */ public static Type fromExceptionSet(ExceptionSet exceptionSet) throws ClassNotFoundException { Type commonSupertype = exceptionSet.getCommonSupertype(); if (commonSupertype.getType() != T_OBJECT) return commonSupertype; ObjectType exceptionSupertype = (ObjectType) commonSupertype; return new ExceptionObjectType(exceptionSupertype.getClassName(), exceptionSet); }
public ObjectType next() { if (!hasNext()) throw new NoSuchElementException(); ObjectType result = factory.getType(next); last = next; return result; }
/** * Add an exception. * * @param type the exception type * @param explicit true if the exception is explicitly declared * or thrown, false if implicit */ public void add(ObjectType type, boolean explicit) { int index = factory.getIndexOfType(type); if (!exceptionSet.get(index)) ++size; exceptionSet.set(index); if (explicit) explicitSet.set(index); commonSupertype = null; }
/** * Return whether or not the set contains any checked exceptions. */ public boolean containsCheckedExceptions() throws ClassNotFoundException { for (ThrownExceptionIterator i = iterator(); i.hasNext();) { ObjectType type = i.next(); if (!Hierarchy.isUncheckedException(type)) return true; } return false; }
@Override public void visitNEW(NEW obj) { Taint taint = new Taint(Taint.State.SAFE); ObjectType type = obj.getLoadClassType(cpg); taint.setRealInstanceClass(type); if (FindSecBugsGlobalConfig.getInstance().isDebugTaintState()) { taint.setDebugInfo("new " + type.getClassName() + "()"); } getFrame().pushValue(taint); }
private void generateStaticFields(RootClass k, ConstantPoolGen cp, ClassGen cg) { RootMember[] members = k.getMembers(); for (int i = 0; i < members.length; i++) { BasicMember member = (BasicMember) members[i]; TLeaf leaf = (TLeaf) lMap.get(member); if (leaf != null) { Type type = new ObjectType(leaf.getClass().getName()); FieldGen fg = new FieldGen(ACC_PUBLIC | ACC_STATIC, type, member.getName() + "Leaf", cp); cg.addField(fg.getField()); } } }
private static void generateMethods(RootClass k, ConstantPoolGen cp, InstructionList il, InstructionFactory factory, ClassGen cg, String className, String clonesClassName) { RootMember[] members = k.getMembers(); for (int i = 0; i < members.length; i++) { BasicMember member = (BasicMember) members[i]; Type type = member.getJavaType(); Type arrayType = new ArrayType(type, 1); MethodGen mg = new MethodGen(ACC_PUBLIC, type, null, null, nameMangler.mangleMember(member.getName()), className, il, cp); il.append(InstructionConstants.ALOAD_0); il.append(factory.createGetField(className, "clones", new ObjectType(clonesClassName))); il.append(InstructionConstants.ALOAD_0); il.append(factory.createGetField(className, "index", Type.INT)); il.append(factory.createInvoke(clonesClassName, nameMangler.mangleMember(member.getName()), type, new Type[] { Type.INT }, INVOKEVIRTUAL)); il.append(InstructionFactory.createReturn(type)); mg.setMaxStack(); mg.setMaxLocals(); cg.addMethod(mg.getMethod()); il.dispose(); } }
private Type[] getParamTypesThis(Method m) { Type[] params = getParamTypesNoThis(m); if (m.isStatic()) { return params; } Type[] newparams = new Type[1 + params.length]; newparams[0] = new ObjectType(c.getClassName()); for (int i = 0; i < params.length; i++) { newparams[i + 1] = params[i]; } return newparams; }
public Cashmerec() { cashmereObjectClass = lookupClass("ibis.cashmere.CashmereObject"); spawnCounterType = new ObjectType( "ibis.cashmere.impl.spawnSync.SpawnCounter"); irType = new ObjectType("ibis.cashmere.impl.spawnSync.InvocationRecord"); cashmereType = new ObjectType("ibis.cashmere.impl.Cashmere"); writeMethodsInterface = lookupClass( "ibis.cashmere.WriteMethodsInterface"); }
InstructionHandle insertDeleteLocalRecord(MethodGen m, InstructionList il, InstructionHandle i, int maxLocals) { String local_record_name = localRecordName(m); // Note: maxLocals has been recomputed at this point. il.insert(i, new ALOAD(maxLocals - 5)); il.insert(i, ins_f.createInvoke(local_record_name, "delete", Type.VOID, new Type[] { new ObjectType(local_record_name) }, Constants.INVOKESTATIC)); return i; }