cg.addField(field); InstructionList ctor = new InstructionList(); MethodGen ctorMethod = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, new Type[] {Type.OBJECT}, new String[] {"arg0"}, "<init>", "org.apache.geode.internal.util.bcel.SerializableImplWithValue", ctor, cp); ctorMethod.setMaxStack(2); InstructionHandle ctor_ih_0 = ctor.append(fac.createLoad(Type.OBJECT, 0)); ctor.append(fac.createInvoke(CLASS_NAME_SERIALIZABLE_IMPL, "<init>", Type.VOID, Type.NO_ARGS, cg.addMethod(ctorMethod.getMethod()); MethodGen getterMethod = new MethodGen(Constants.ACC_PUBLIC, Type.OBJECT, null, null, GET_VALUE, CLASS_NAME_SERIALIZABLE_IMPL_WITH_VALUE, getter, cp); getterMethod.setMaxStack(1); cg.addMethod(getterMethod.getMethod()); MethodGen setterMethod = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, new Type[] {Type.OBJECT}, new String[] {field.getName()}, SET_VALUE, CLASS_NAME_SERIALIZABLE_IMPL_WITH_VALUE, setter, cp); setterMethod.setMaxStack(2); cg.addMethod(setterMethod.getMethod()); byte[] bytes = jClazz.getBytes(); return defineClass(jClazz.getClassName(), bytes, 0, bytes.length);
public void setMaxLocals() { // Get the current number of local variable slots int maxLocals = super.getMaxLocals(); // Get numer of actual variables final LocalVariableGen[] localVars = super.getLocalVariables(); if (localVars != null) { if (localVars.length > maxLocals) maxLocals = localVars.length; } // We want at least 5 local variable slots (for parameters) if (maxLocals < 5) maxLocals = 5; super.setMaxLocals(maxLocals); }
private void analyzeMethod(Method m, ClassContext classContext) throws CFGBuilderException, DataflowAnalysisException { MethodGen methodGen = classContext.getMethodGen(m); ConstantPoolGen cpg = classContext.getConstantPoolGen(); CFG cfg = classContext.getCFG(m); if (methodGen == null || methodGen.getInstructionList() == null) { return; //No instruction .. nothing to do } for (Iterator<Location> i = cfg.locationIterator(); i.hasNext(); ) { Location location = i.next(); Instruction inst = location.getHandle().getInstruction(); if (inst instanceof InvokeInstruction) { InvokeInstruction invoke = (InvokeInstruction) inst; String methodName = invoke.getMethodName(cpg); if ("enableDefaultTyping".equals(methodName)) { JavaClass clz = classContext.getJavaClass(); bugReporter.reportBug(new BugInstance(this, DESERIALIZATION_TYPE, HIGH_PRIORITY) .addClass(clz) .addMethod(clz, m) .addCalledMethod(cpg, invoke) .addSourceLine(classContext, m, location) ); } } } }
@Override public String toString() { return this.getClass().getSimpleName() + "(" + methodGen.getClassName() + "." + methodGen.getMethod().getName() + methodGen.getMethod().getSignature() + ")"; }
/** * Factory method for creating a source line annotation describing an entire * method. * * @param methodGen * the method being visited * @return the SourceLineAnnotation, or null if we do not have line number * information for the method */ public static SourceLineAnnotation fromVisitedMethod(MethodGen methodGen, String sourceFile) { LineNumberTable lineNumberTable = methodGen.getLineNumberTable(methodGen.getConstantPool()); String className = methodGen.getClassName(); int codeSize = methodGen.getInstructionList().getLength(); if (lineNumberTable == null) { return createUnknown(className, sourceFile, 0, codeSize - 1); } return forEntireMethod(className, sourceFile, lineNumberTable, codeSize); }
private void createSetMethod(String method_name, String field_name, Class field_type) { InstructionList tmpIl = new InstructionList(); MethodGen method = new MethodGen(ACC_PUBLIC | ACC_FINAL, Type.VOID, buildArgs(new Class[] {field_type}), new String[] {"arg"}, method_name, classname, tmpIl, cp); tmpIl.append(InstructionConstants.ALOAD_0); tmpIl.append(InstructionConstants.ALOAD_1); tmpIl.append(factory.createFieldAccess(classname, field_name, getObjectType(Object.class), Constants.PUTFIELD)); tmpIl.append(InstructionFactory.createReturn(Type.VOID)); method.setMaxStack(); method.setMaxLocals(); cg.addMethod(method.getMethod()); tmpIl.dispose(); }
InstructionList il; cp = clazz.getConstantPool(); il = new InstructionList(); Type[] methodArgTypes = translate(method.getParameterTypes()); MethodGen mg = new MethodGen( Constants.ACC_FINAL | Constants.ACC_PUBLIC, methodReturnType, method.getName(), clazz.getClassName(), il, cp); mg.addAttribute(new Synthetic(cp.addUtf8("Synthetic"), 0, null, cp .getConstantPool())); mg.addException(throwsException[i].getName()); il.append(InstructionFactory.createThis()); il.append(new INVOKESPECIAL(cp.addMethodref(method.getDeclaringClass() .getName(), method.getName(), method.getSignature()))); mg.setMaxStack(); mg.setMaxLocals(); clazz.addMethod(mg.getMethod());
new ClassGen(template, "java.lang.Object", "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null); ConstantPoolGen cp = cg.getConstantPool(); // cg creates constant pool InstructionList il = new InstructionList(); MethodGen mg = new MethodGen(Constants.ACC_STATIC | LocalVariableGen lg = mg.addLocalVariable("in", new ObjectType("java.io.BufferedReader"), null, null); int in = lg.getIndex(); lg = mg.addLocalVariable("name", Type.STRING, null, null); int name = lg.getIndex(); il.append(ACONST_NULL); InstructionHandle handler = il.append(RETURN); mg.addExceptionHandler(try_start, try_end, handler, new ObjectType("java.io.IOException")); il.append(RETURN); mg.setMaxStack(5); // Needed stack size cg.addMethod(mg.getMethod()); cg.getJavaClass().dump(template + ".class");
public void replaceStrings() { for (ClassGen cg : cgs.values()) { int replaced = 0; for (Method method : cg.getMethods()) { MethodGen mg = new MethodGen(method, cg.getClassName(), cg.getConstantPool()); InstructionList list = mg.getInstructionList(); if (list == null) { continue; InstructionHandle[] handles = list.getInstructionHandles(); for (int i = 1; i < handles.length; i++) { if (GenericMethods.isCall(handles[i].getInstruction()) && handles[i - 1].getInstruction() instanceof LDC) { String methodCallClass = GenericMethods.getCallClassName(handles[i].getInstruction(), cg.getConstantPool()); String methodCallMethod = GenericMethods.getCallMethodName(handles[i].getInstruction(), cg.getConstantPool()); String methodCallSig = GenericMethods.getCallSignature(handles[i].getInstruction(), cg.getConstantPool()); continue; logger.debug(encryptedString + " -> " + decryptedString + " in " + cg.getClassName() + "." + method.getName()); int stringRef = cg.getConstantPool().addString(decryptedString); LDC lc = new LDC(stringRef); mg.setInstructionList(list); mg.setMaxLocals(); mg.setMaxStack(); cg.replaceMethod(method, mg.getMethod());
mg.addAnnotationEntry(createSimpleVisibleAnnotation(mg .getConstantPool())); Type.VOID, new Type[] { new ObjectType("java.io.Reader") }, Const.INVOKESPECIAL)); LocalVariableGen lg = mg.addLocalVariable("in", new ObjectType( "java.io.BufferedReader"), null, null); final int in = lg.getIndex(); lg.setStart(il.append(new ASTORE(in))); // "in" valid from here lg = mg.addLocalVariable("name", Type.STRING, null, null); final int name = lg.getIndex(); il.append(InstructionConst.ACONST_NULL); final LocalVariableGen var_ex = mg.addLocalVariable("ex", Type .getType("Ljava.io.IOException;"), null, null); final int var_ex_slot = var_ex.getIndex(); var_ex.setStart(handler); var_ex.setEnd(il.append(InstructionConst.RETURN)); mg.addExceptionHandler(try_start, try_end, handler, new ObjectType( "java.io.IOException")); mg.setMaxStack(); mg.setMaxLocals(); cg.addMethod(mg.getMethod()); il.dispose(); // Allow instruction handles to be reused cg.addEmptyConstructor(Const.ACC_PUBLIC);
setMaxStack(c.getMaxStack()); setMaxLocals(c.getMaxLocals()); end = il.getEnd(); } else { end = il.findHandle(end_pc); end = end.getPrev(); // Make it inclusive addExceptionHandler(il.findHandle(ce.getStartPC()), end, il.findHandle(ce.getHandlerPC()), c_type); addLineNumber(il.findHandle(l.getStartPC()), l.getLineNumber()); removeLocalVariables(); addLocalVariable(l.getName(), Type.getType(l.getSignature()), l.getIndex(), start, end); addCodeAttribute(a); addException(names[j]); } else addAttribute(a);
InstructionList invokeInstructions = new InstructionList(); MethodEntry[] operations = getOperations(info); invokeInstructions.append(new ALOAD(1)); // Stack: => ..., arg1 [String] invokeInstructions.append(ifOperationEqualsNull); // Stack: => ... beginTryBlock = beginTryBlock.getNext(); endTryBlock = jumpToSuperInvoke.getPrev(); invokeInstructions.append(new ARETURN()); MethodGen invokeMethod = new MethodGen( Constants.ACC_PUBLIC, Type.OBJECT, className, invokeInstructions, cp ); invokeMethod.setMaxLocals(7); invokeMethod.setMaxStack(calculateMaxStackSize(info)); invokeMethod.addException(ReflectionException.class.getName()); invokeMethod.addException(MBeanException.class.getName()); invokeMethod.addExceptionHandler(beginTryBlock, endTryBlock, exceptionHandlerCode, new ObjectType("java.lang.Throwable"));
if (a instanceof Code) { final Code c = (Code) a; setMaxStack(c.getMaxStack()); setMaxLocals(c.getMaxLocals()); final CodeException[] ces = c.getExceptionTable(); if (ces != null) { addExceptionHandler(il.findHandle(ce.getStartPC()), end, il.findHandle(ce .getHandlerPC()), c_type); addLineNumber(ih, l.getLineNumber()); updateLocalVariableTable((LocalVariableTable) a); } else if (a instanceof LocalVariableTypeTable) { this.local_variable_type_table = (LocalVariableTypeTable) a.copy(cp.getConstantPool()); } else { addCodeAttribute(a); final String[] names = ((ExceptionTable) a).getExceptionNames(); for (final String name2 : names) { addException(name2); final AnnotationEntry[] aes = runtimeAnnotations.getAnnotationEntries(); for (final AnnotationEntry element : aes) { addAnnotationEntry(new AnnotationEntryGen(element, cp, false)); addAttribute(a);
ClassGen classGen = new ClassGen(jclass); Method[] methodList = jclass.getMethods(); for (Method method : methodList) { if (method.isAbstract() || method.isNative()) { continue; if (methodName != null && !method.getName().equals(methodName)) { continue; MethodGen methodGen = new MethodGen(method, jclass.getClassName(), classGen.getConstantPool());
final ConstantPoolGen cpg = classGen.getConstantPool(); }; final InstructionList il = new InstructionList(); argTypes, argNames, "buildKeys", _className, il, classGen.getConstantPool()); buildKeys.addException("org.apache.xalan.xsltc.TransletException"); il.append(RETURN);
public void translate(ClassGenerator classGen, MethodGenerator methodGen) { ConstantPoolGen cpg = classGen.getConstantPool(); InstructionList il = methodGen.getInstructionList(); il.append(new PUSH(cpg, _value)); }
public static boolean disassemble(String name) { JavaClass mod = null; try { mod = Repository.lookupClass(name); } catch (Exception e) { System.err.println("Could not get class " + name); return false; } ClassGen modClass = new ClassGen(mod); ConstantPoolGen cp = modClass.getConstantPool(); Method[] methods = mod.getMethods(); for (int i = 0; i < methods.length; i++) { System.out.println("* "+methods[i].getName()); MethodGen mg = new MethodGen(methods[i], mod.getClassName(), cp); InstructionList il = mg.getInstructionList(); System.out.println(il.toString()); } return true; } }
public Method injection(final JavaClass clazz, Method method, final ConstantPoolGen cp, final int firstStringOffset) { final MethodGen methodGen = new MethodGen(method, clazz.getClassName(), cp); final InstructionList instructionList = methodGen.getInstructionList(); instructionList.insert(instructionList.getStart(), makeWillBeAddedInstructionList(methodGen, firstStringOffset)); methodGen.setMaxStack(); methodGen.setMaxLocals(); method = methodGen.getMethod(); instructionList.dispose(); return method; }
public void fixStack() { for (ClassGen cg : cgs.values()) { for (Method method : cg.getMethods()) { MethodGen mg = new MethodGen(method, cg.getClassName(), cg.getConstantPool()); mg.removeNOPs(); mg.setMaxLocals(); mg.setMaxStack(); cg.replaceMethod(method, mg.getMethod()); logger.debug(String.format("Reset MaxStack and MaxLocals in %s.%s", cg.getClassName(), mg.getName())); } } }
@Override public void visitMethod( final Method method ) { final MethodGen mg = new MethodGen(method, _clazz.getClassName(), _cp); _out.println(" InstructionList il = new InstructionList();"); _out.println(" MethodGen method = new MethodGen(" + printFlags(method.getAccessFlags(), FLAGS.METHOD) + ", " + printType(mg.getReturnType()) + ", " + printArgumentTypes(mg.getArgumentTypes()) + ", " + "new String[] { " + Utility.printArray(mg.getArgumentNames(), false, true) + " }, \"" + method.getName() + "\", \"" + _clazz.getClassName() + "\", il, _cp);"); _out.println(); final BCELFactory factory = new BCELFactory(mg, _out); factory.start(); _out.println(" method.setMaxStack();"); _out.println(" method.setMaxLocals();"); _out.println(" _cg.addMethod(method.getMethod());"); _out.println(" il.dispose();"); }