private boolean returnsString(InvokeInstruction inv) { String methodSig = inv.getSignature(getCPG()); return methodSig.endsWith(")Ljava/lang/String;"); }
public static boolean mightCloseStream(BasicBlock basicBlock, InstructionHandle handle, ConstantPoolGen cpg) { Instruction ins = handle.getInstruction(); if ((ins instanceof INVOKEVIRTUAL) || (ins instanceof INVOKEINTERFACE)) { // Does this instruction close the stream? InvokeInstruction inv = (InvokeInstruction) ins; // It's a close if the invoked class is any subtype of the stream // base class. // (Basically, we may not see the exact original stream class, // even though it's the same instance.) return "close".equals(inv.getName(cpg)) && "()V".equals(inv.getSignature(cpg)); } return false; }
public boolean matches(Instruction instruction, ConstantPoolGen cpg) { if(instruction != null && instruction instanceof InvokeInstruction) { InvokeInstruction invokeInstruction = (InvokeInstruction) instruction; if (classesNames.size() != 0 && !classesNames.contains(invokeInstruction.getClassName(cpg))) { return false; } else if (methodNames.size() != 0 && !methodNames.contains(invokeInstruction.getMethodName(cpg))) { return false; } else if (argSignatures.size() != 0 && !argSignatures.contains(invokeInstruction.getSignature(cpg))) { return false; } return true; } return false; } }
/** * Get the number of parameters passed to method invocation. * * @param inv * @param cpg * @return int number of parameters */ public static int getNumParametersForInvocation(InvokeInstruction inv, ConstantPoolGen cpg) { SignatureParser sigParser = new SignatureParser(inv.getSignature(cpg)); return sigParser.getNumParameters(); }
/** * Get the number of parameters passed to method invocation. * * @param inv * @param cpg * @return int number of parameters */ public static int getNumParametersForInvocation(InvokeInstruction inv, ConstantPoolGen cpg) { GenericSignatureParser sigParser = new GenericSignatureParser(inv.getSignature(cpg)); return sigParser.getNumParameters(); }
/** * Get the number of arguments passed to given method invocation. * * @param ins * the method invocation instruction * @param cpg * the ConstantPoolGen for the class containing the method * @return number of arguments; note that this excludes the object instance * for instance methods */ public int getNumArguments(InvokeInstruction ins, ConstantPoolGen cpg) { SignatureParser parser = new SignatureParser(ins.getSignature(cpg)); return parser.getNumParameters(); }
/** * Get the <i>i</i>th argument passed to given method invocation. * * @param ins * the method invocation instruction * @param cpg * the ConstantPoolGen for the class containing the method * @param i * index of the argument; 0 for the first argument, etc. * @param numArguments * total number of arguments to the method * @return the <i>i</i>th argument * @throws DataflowAnalysisException */ @Deprecated public ValueType getArgument(InvokeInstruction ins, ConstantPoolGen cpg, int i, int numArguments) throws DataflowAnalysisException { SignatureParser sigParser = new SignatureParser(ins.getSignature(cpg)); return getArgument(ins, cpg, i, sigParser); }
private static Map<InstructionHandle, Call> buildCallMap(CFG cfg, ConstantPoolGen cpg) { Map<InstructionHandle, Call> callMap = new HashMap<>(); for (Iterator<Location> i = cfg.locationIterator(); i.hasNext();) { InstructionHandle handle = i.next().getHandle(); Instruction ins = handle.getInstruction(); if (ins instanceof InvokeInstruction) { InvokeInstruction inv = (InvokeInstruction) ins; Call call = new Call(inv.getClassName(cpg), inv.getName(cpg), inv.getSignature(cpg)); callMap.put(handle, call); } } return callMap; }
/** * Convenience method for generating a method signature in human readable * form. * * @param inv * an InvokeInstruction * @param cpg * the ConstantPoolGen for the class the instruction belongs to */ public static String convertMethodSignature(InvokeInstruction inv, ConstantPoolGen cpg) { return convertMethodSignature(inv.getClassName(cpg), inv.getName(cpg), inv.getSignature(cpg)); }
public MethodDescriptor(InvokeInstruction iins, ConstantPoolGen cpg) { super(ClassName.toSlashedClassName(iins.getClassName(cpg)), iins.getMethodName(cpg), iins.getSignature(cpg), iins instanceof INVOKESTATIC); }
/** * Determine if given Instruction is a monitor wait. * * @param ins * the Instruction * @param cpg * the ConstantPoolGen for the Instruction * * @return true if the instruction is a monitor wait, false if not */ public static boolean isMonitorWait(Instruction ins, ConstantPoolGen cpg) { if (!(ins instanceof InvokeInstruction)) { return false; } if (ins.getOpcode() == Const.INVOKESTATIC) { return false; } InvokeInstruction inv = (InvokeInstruction) ins; String methodName = inv.getMethodName(cpg); String methodSig = inv.getSignature(cpg); return isMonitorWait(methodName, methodSig); }
/** * Determine if given Instruction is a monitor wait. * * @param ins * the Instruction * @param cpg * the ConstantPoolGen for the Instruction * * @return true if the instruction is a monitor wait, false if not */ public static boolean isMonitorNotify(Instruction ins, ConstantPoolGen cpg) { if (!(ins instanceof InvokeInstruction)) { return false; } if (ins.getOpcode() == Const.INVOKESTATIC) { return false; } InvokeInstruction inv = (InvokeInstruction) ins; String methodName = inv.getMethodName(cpg); String methodSig = inv.getSignature(cpg); return isMonitorNotify(methodName, methodSig); }
/** * Get a MethodDescriptor describing the method called by given * InvokeInstruction. * * @param inv * the InvokeInstruction * @param cpg * ConstantPoolGen of class containing instruction * @return MethodDescriptor describing the called method */ public static MethodDescriptor getCalledMethodDescriptor(InvokeInstruction inv, ConstantPoolGen cpg) { String calledClassName = inv.getClassName(cpg).replace('.', '/'); String calledMethodName = inv.getMethodName(cpg); String calledMethodSig = inv.getSignature(cpg); boolean isStatic = inv.getOpcode() == Const.INVOKESTATIC; return DescriptorFactory.instance().getMethodDescriptor(calledClassName, calledMethodName, calledMethodSig, isStatic); }
@Override public boolean mightCloseResource(BasicBlock basicBlock, InstructionHandle handle, ConstantPoolGen cpg) throws DataflowAnalysisException { InvokeInstruction inv = toInvokeInstruction(handle.getInstruction()); if (inv == null) { return false; } String className = inv.getClassName(cpg); String methodName = inv.getName(cpg); String methodSig = inv.getSignature(cpg); try { if ("unlock".equals(methodName) && "()V".equals(methodSig) && Hierarchy.isSubtype(className, "java.util.concurrent.locks.Lock")) { return true; } } catch (ClassNotFoundException e) { lookupFailureCallback.reportMissingClass(e); } return false; }
/** * Add a method annotation for the method which is called by given * instruction. * * @param cpg * the constant pool for the method containing the call * @param inv * the InvokeInstruction * @return this object */ @Nonnull public BugInstance addCalledMethod(ConstantPoolGen cpg, InvokeInstruction inv) { String className = inv.getClassName(cpg); String methodName = inv.getMethodName(cpg); String methodSig = inv.getSignature(cpg); addMethod(className, methodName, methodSig, inv.getOpcode() == Const.INVOKESTATIC); describe(MethodAnnotation.METHOD_CALLED); return this; }
public MethodDescriptor getInvokedMethod(ConstantPoolGen cpg, InvokeInstruction inv) { String invoked = inv.getClassName(cpg); String methodName = inv.getMethodName(cpg); String methodSig = inv.getSignature(cpg); MethodDescriptor invokedMethod = DescriptorFactory.instance().getMethodDescriptor(ClassName.toSlashedClassName(invoked), methodName, methodSig, inv instanceof INVOKESTATIC); return invokedMethod; }
/** * Look up the method referenced by given InvokeInstruction. This method * does <em>not</em> look for implementations in super or subclasses * according to the virtual dispatch rules. * * @param inv * the InvokeInstruction * @param cpg * the ConstantPoolGen used by the class the InvokeInstruction * belongs to * @param chooser * JavaClassAndMethodChooser to use to pick the method from among * the candidates * @return the JavaClassAndMethod, or null if no such method is defined in * the class */ public static JavaClassAndMethod findExactMethod(InvokeInstruction inv, ConstantPoolGen cpg, JavaClassAndMethodChooser chooser) throws ClassNotFoundException { String className = inv.getClassName(cpg); String methodName = inv.getName(cpg); String methodSig = inv.getSignature(cpg); JavaClass jclass = Repository.lookupClass(className); return findMethod(jclass, methodName, methodSig, chooser); }
/** * Create an XMethod object from an InvokeInstruction. * * @param invokeInstruction * the InvokeInstruction * @param cpg * ConstantPoolGen from the class containing the instruction * @return XMethod representing the method called by the InvokeInstruction */ public static XMethod createXMethod(InvokeInstruction invokeInstruction, ConstantPoolGen cpg) { String className = invokeInstruction.getClassName(cpg); String methodName = invokeInstruction.getName(cpg); String methodSig = invokeInstruction.getSignature(cpg); if (invokeInstruction instanceof INVOKEDYNAMIC) { // XXX the lambda representation makes no sense for XMethod // "classical" instruction attributes are filled with garbage, causing // the code later to produce crazy errors (looking for non existing types etc) // We should NOT be called here from our code, but 3rd party code still may // use this method. So *at least* provide a valid class name, which is // (don't ask me why) is encoded in the first argument type of the lambda // className = invokeInstruction.getArgumentTypes(cpg)[0].toString(); className = "java.lang.Object"; } return createXMethod(className, methodName, methodSig, invokeInstruction.getOpcode() == Const.INVOKESTATIC); }
@Override public void transferInstruction(InstructionHandle handle, BasicBlock basicBlock, LockSet fact) throws DataflowAnalysisException { Instruction ins = handle.getInstruction(); short opcode = ins.getOpcode(); if (opcode == Const.MONITORENTER || opcode == Const.MONITOREXIT) { ValueNumberFrame frame = vnaDataflow.getFactAtLocation(new Location(handle, basicBlock)); modifyLock(frame, fact, opcode == Const.MONITORENTER ? 1 : -1); } else if (opcode == Const.INVOKEVIRTUAL || opcode == Const.INVOKEINTERFACE) { InvokeInstruction inv = (InvokeInstruction) ins; String name = inv.getMethodName(methodGen.getConstantPool()); String sig = inv.getSignature(methodGen.getConstantPool()); ValueNumberFrame frame = vnaDataflow.getFactAtLocation(new Location(handle, basicBlock)); if ("()V".equals(sig) && ("lock".equals(name) || "lockInterruptibly".equals(name))) { modifyLock(frame, fact, 1); } else if ("()V".equals(sig) && ("unlock".equals(name))) { modifyLock(frame, fact, -1); } } else if ((ins instanceof ReturnInstruction) && isSynchronized && !isStatic) { lockOp(fact, vna.getThisValue().getNumber(), -1); } }
/** * Look up the method referenced by given InvokeInstruction. This method * does <em>not</em> look for implementations in super or subclasses * according to the virtual dispatch rules. * * @param inv * the InvokeInstruction * @param cpg * the ConstantPoolGen used by the class the InvokeInstruction * belongs to * @param chooser * JavaClassAndMethodChooser to use to pick the method from among * the candidates * @return the JavaClassAndMethod, or null if no such method is defined in * the class */ public static XMethod findExactMethod(InvokeInstruction inv, ConstantPoolGen cpg, JavaClassAndMethodChooser chooser) { String className = inv.getClassName(cpg); String methodName = inv.getName(cpg); String methodSig = inv.getSignature(cpg); XMethod result = findMethod(DescriptorFactory.createClassDescriptorFromDottedClassName(className), methodName, methodSig, inv instanceof INVOKESTATIC); return thisOrNothing(result, chooser); }