Java Code Examples for org.objectweb.asm.tree.MethodNode#visitMaxs()

The following examples show how to use org.objectweb.asm.tree.MethodNode#visitMaxs() . 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: MarkerTransformer.java    From obfuscator with MIT License 6 votes vote down vote up
private MethodNode createMethod() {
    final MethodNode methodNode = new MethodNode(ACC_PUBLIC | ACC_STATIC, methodName, "()V", null, null);
    methodNode.visitCode();

    final Label label0 = new Label();
    methodNode.visitLabel(label0);
    methodNode.visitLineNumber(10, label0);
    methodNode.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    methodNode.visitLdcInsn(text);
    methodNode.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);

    final Label label1 = new Label();
    methodNode.visitLabel(label1);
    methodNode.visitLineNumber(11, label1);
    methodNode.visitInsn(RETURN);

    methodNode.visitMaxs(2, 0);
    methodNode.visitEnd();
    return methodNode;
}
 
Example 2
Source File: CaseAdapter.java    From jumbune with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public void visitEnd() {
	for (Object o : methods) {
		MethodNode mn = (MethodNode) o;
		currentMethodNode = (MethodNode) o;

		LOGGER.debug(MessageFormat.format(InstrumentationMessageLoader
				.getMessage(MessageConstants.LOG_INSTRUMENTING_METHOD),
				getClassName() + "##" + mn.name));

		if ((currentMethodNode.access & Opcodes.ACC_SYNTHETIC) == 0) {
			instrumentswitchBlocks(mn);
		}
		mn.visitMaxs(0, 0);
	}

	accept(cv);
}
 
Example 3
Source File: EngineExtender.java    From 07kit with GNU General Public License v3.0 6 votes vote down vote up
public MethodNode createGetter(GetterDefinition getter) {
    MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, getter.name,
            "()" + getter.signature, null, null);
    if (getter.member) {
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitFieldInsn(Opcodes.GETFIELD, getter.fieldClass,
                getter.fieldName, getter.actualSig);
    } else {
        method.visitFieldInsn(Opcodes.GETSTATIC, getter.fieldClass,
                getter.fieldName, getter.actualSig);
    }

    if (getter.multiplier != 0) {
        method.visitLdcInsn((int) getter.multiplier);
        method.visitInsn(Opcodes.IMUL);
    }

    method.visitInsn(Type.getType(getter.actualSig).getOpcode(Opcodes.IRETURN));
    method.visitMaxs(1, 1);
    method.visitEnd();
    return method;
}
 
Example 4
Source File: ConstructorThisInterpreterTest.java    From AVM with MIT License 5 votes vote down vote up
private MethodNode buildTestConstructor() {
    MethodNode methodVisitor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>", "(Lorg/aion/avm/core/persistence/ConstructorThisInterpreterTest;I)V", null, null);
    methodVisitor.visitCode();
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    methodVisitor.visitInsn(Opcodes.ICONST_5);
    methodVisitor.visitVarInsn(Opcodes.ILOAD, 2);
    Label label2 = new Label();
    methodVisitor.visitJumpInsn(Opcodes.IF_ICMPNE, label2);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    Label label3 = new Label();
    methodVisitor.visitJumpInsn(Opcodes.GOTO, label3);
    methodVisitor.visitLabel(label2);
    methodVisitor.visitFrame(Opcodes.F_FULL, 3, new Object[] {"org/aion/avm/core/persistence/ConstructorThisInterpreterTest", "org/aion/avm/core/persistence/ConstructorThisInterpreterTest", Opcodes.INTEGER}, 0, new Object[] {});
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitLabel(label3);
    methodVisitor.visitFrame(Opcodes.F_SAME1, 0, null, 1, new Object[] {"org/aion/avm/core/persistence/ConstructorThisInterpreterTest"});
    methodVisitor.visitVarInsn(Opcodes.ILOAD, 2);
    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, "org/aion/avm/core/persistence/ConstructorThisInterpreterTest", "bar", "I");
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitInsn(Opcodes.ICONST_1);
    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, "org/aion/avm/core/persistence/ConstructorThisInterpreterTest", "bar", "I");
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitInsn(Opcodes.ICONST_2);
    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, "org/aion/avm/core/persistence/ConstructorThisInterpreterTest", "bar", "I");
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(2, 3);
    methodVisitor.visitEnd();
    return methodVisitor;
}
 
Example 5
Source File: ConstructorThisInterpreterTest.java    From AVM with MIT License 5 votes vote down vote up
private MethodNode buildSecondConstructor() {
    MethodNode methodVisitor = new MethodNode(Opcodes.ACC_PUBLIC, "<init>", "(Lorg/aion/avm/core/NonStaticInnerClassTarget$Inner;)V", null, null);
    methodVisitor.visitCode();
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 1);
    methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, "org/aion/avm/core/NonStaticInnerClassTarget$Inner$Deeper", "this$1", "Lorg/aion/avm/core/NonStaticInnerClassTarget$Inner;");
    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
    methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
    methodVisitor.visitInsn(Opcodes.RETURN);
    methodVisitor.visitMaxs(2, 2);
    methodVisitor.visitEnd();
    return methodVisitor;
}
 
Example 6
Source File: StringPooler.java    From radon with GNU General Public License v3.0 5 votes vote down vote up
private MethodNode stringPool(String className, String methodName, String fieldName, ArrayList<String> strings) {
    MethodNode method = new MethodNode(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC | ACC_BRIDGE, methodName, "()V",
            null, null);

    method.visitCode();
    int numberOfStrings = strings.size();
    if (numberOfStrings <= 5)
        method.visitInsn(numberOfStrings + 3);
    else if (numberOfStrings <= 127)
        method.visitIntInsn(BIPUSH, strings.size());
    else if (numberOfStrings <= 32767)
        method.visitIntInsn(SIPUSH, strings.size());
    else
        method.visitLdcInsn(strings.size());


    method.visitTypeInsn(ANEWARRAY, "java/lang/String");

    for (int i = 0; i < strings.size(); i++) {
        method.visitInsn(DUP);

        if (i <= 5)
            method.visitInsn(i + 3);
        else if (i <= 127)
            method.visitIntInsn(BIPUSH, i);
        else if (i <= 32767)
            method.visitIntInsn(SIPUSH, i);
        else
            method.visitLdcInsn(i);

        method.visitLdcInsn(strings.get(i));
        method.visitInsn(AASTORE);
    }
    method.visitFieldInsn(PUTSTATIC, className, fieldName, "[Ljava/lang/String;");
    method.visitInsn(RETURN);
    method.visitMaxs(3, 0);
    method.visitEnd();

    return method;
}
 
Example 7
Source File: ASMHelper.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void copy(MethodNode src, MethodNode dst) {
	Map<LabelNode, LabelNode> labelMap = cloneLabels(src.instructions);
	dst.instructions = cloneInsnList(labelMap, src.instructions);
	dst.tryCatchBlocks = cloneTryCatchBlocks(labelMap, src.tryCatchBlocks);
	if (src.localVariables != null) {
		dst.localVariables = cloneLocals(labelMap, src.localVariables);
	}
	dst.visibleAnnotations = src.visibleAnnotations;
	dst.invisibleAnnotations = src.invisibleAnnotations;
	dst.visitMaxs(src.maxStack, src.maxLocals);
}
 
Example 8
Source File: ASMHelper.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void copy(MethodNode src, MethodNode dst) {
	Map<LabelNode, LabelNode> labelMap = cloneLabels(src.instructions);
	dst.instructions = cloneInsnList(labelMap, src.instructions);
	dst.tryCatchBlocks = cloneTryCatchBlocks(labelMap, src.tryCatchBlocks);
	if (src.localVariables != null) {
		dst.localVariables = cloneLocals(labelMap, src.localVariables);
	}
	dst.visibleAnnotations = src.visibleAnnotations;
	dst.invisibleAnnotations = src.invisibleAnnotations;
	dst.visitMaxs(src.maxStack, src.maxLocals);
}
 
Example 9
Source File: ASMHelper.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static void copy(MethodNode src, MethodNode dst) {
	Map<LabelNode, LabelNode> labelMap = cloneLabels(src.instructions);
	dst.instructions = cloneInsnList(labelMap, src.instructions);
	dst.tryCatchBlocks = cloneTryCatchBlocks(labelMap, src.tryCatchBlocks);
	if (src.localVariables != null) {
		dst.localVariables = cloneLocals(labelMap, src.localVariables);
	}
	dst.visibleAnnotations = src.visibleAnnotations;
	dst.invisibleAnnotations = src.invisibleAnnotations;
	dst.visitMaxs(src.maxStack, src.maxLocals);
}
 
Example 10
Source File: MainAdapter.java    From jumbune with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see org.jumbune.debugger.instrumentation.adapter.BaseAdapter#visitEnd()
 */
@Override
public void visitEnd() {
	for (Object o : methods) {
		MethodNode mn = (MethodNode) o;

		// finding main method
		if (InstrumentUtil.validateMethodName(mn.name, MAIN_METHOD)
				&& Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC == mn.access
				&& Type.getMethodDescriptor(Type.VOID_TYPE,
						TYPE_STRING_ARRAY).equals(mn.desc)) {
			LOGGER.debug(MessageFormat.format(InstrumentationMessageLoader
					.getMessage(MessageConstants.MAIN_METHOD_FOUND),
					getClassName()));

			InsnList insnList = mn.instructions;

			InsnList il = new InsnList();
			il.add(loadClasses());

			// insert as first instruction
			insnList.insertBefore(insnList.getFirst(), il);
			break;
		}
		mn.visitMaxs(0, 0);
	}
	accept(cv);
}
 
Example 11
Source File: ReturnAdapter.java    From jumbune with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * visit end method for intrumentation	
 */
@Override
public void visitEnd() {
	for (Object o : methods) {
		MethodNode mn = (MethodNode) o;

		if (validateMethods(mn)
				&& InstrumentUtil.validateMethodName(mn.name, "<clinit>")
				&& mn.name.indexOf("access$") < 0) {

			logger.debug("instrumenting " + getClassName() + "##" + mn.name);

			InsnList insnList = mn.instructions;
			AbstractInsnNode[] insnArr = insnList.toArray();
			for (AbstractInsnNode abstractInsnNode : insnArr) {
				if (Opcodes.RETURN >= abstractInsnNode.getOpcode()
						&& Opcodes.IRETURN <= abstractInsnNode.getOpcode()) {
					String msg = new StringBuilder("[Return] [method] [] ")
							.toString();

					String cSymbol = env.getClassSymbol(getClassName());
					String mSymbol = env.getMethodSymbol(getClassName(), cSymbol, mn.name);

					InsnList il = InstrumentUtil.addReturnLogging(
							cSymbol, mSymbol, msg);
					insnList.insertBefore(abstractInsnNode, il);
				}
			}
		}
		mn.visitMaxs(0, 0);
	}
	accept(cv);
}
 
Example 12
Source File: ContextWriteValidationAdapter.java    From jumbune with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * This method first copies the key/value to new temporary variables and
 * pass these variables to either PatternMatcher class or the class
 * specified by user. The call to these classes is embedded in Log method so
 * the result returned will be printed. To summarize this method will modify
 * context.write method for copying parameters to local variables and it
 * will then add a call for Log class which prints the result of
 * validation/matching by taking in message a call to these
 * PatternMatcher/User defined validation class. Sample output:
 * 
 * context.write(tempKey = key, tempVal = value); Log.info("result " +
 * PatternMatcher.match(tempVal, regEx)); Log.info("validation result " +
 * UserSpecifiedClass.method(tempVal));
 */
@Override
public void visitEnd() {
	JobConfig jobConfig = (JobConfig)getConfig();
	if (validateUserValidationClass(jobConfig.getUserValidations(),
			getClassName())
			|| validateRegexValidationClass(jobConfig.getRegex(),
					getClassName())) {
		for (int i = 0; i < methods.size(); i++) {
			MethodNode mn = (MethodNode) methods.get(i);
			int variableIndex = mn.maxLocals;
			/**
			 * context.write/output.collect has been written in the
			 * map/reduce methods and other user defined functions. Applying
			 * filter to skip synthetic methods.
			 */
			if (!InstrumentUtil.isSysntheticAccess(mn.access)) {
				InsnList insnList = mn.instructions;
				AbstractInsnNode[] insnArr = insnList.toArray();
				int writeCount = 0;
				for (int j = 0; j < insnArr.length; j++) {
					AbstractInsnNode abstractInsnNode = insnArr[j];

					if (abstractInsnNode instanceof MethodInsnNode) {
						MethodInsnNode min = (MethodInsnNode) abstractInsnNode;

						// write method
						if (InstrumentUtil.isOutputMethod(min)) {

							LOG.debug(MessageFormat.format(
									InstrumentationMessageLoader
											.getMessage(MessageConstants.LOG_ADDING_REGEX_VALIDATION_CALL),
									getClassName() + "##" + mn.name,
									writeCount++));

							InstructionsBean insBean = MethodByteCodeUtil
									.readMethodAndCopyParamToTemporaryVariables(
											min.getPrevious(),
											variableIndex, insnList,
											mn.localVariables);

							// Add the instance of temporary key/value index
							// to ContextWriteParams
							ContextWriteParams
									.getInstance()
									.setTempKeyVariableIndex(
											insBean.getTemporaryVariablesIndexList()
													.get(KEY_INDEX));
							ContextWriteParams
									.getInstance()
									.setTempValueVariableIndex(
											insBean.getTemporaryVariablesIndexList()
													.get(VALUE_INDEX));

							InsnList patternValidationInsnList = new InsnList();
							LOG.debug("***** Just going to add validations  keyIndex "
									+ insBean
											.getTemporaryVariablesIndexList()
											.get(KEY_INDEX)
									+ " Value Index "
									+ insBean
											.getTemporaryVariablesIndexList()
											.get(VALUE_INDEX));

							LOG.debug("ContextWriteParams.getInstance() KEY --  "
									+ ContextWriteParams.getInstance()
											.getTempKeyVariableIndex());
							addValidations(patternValidationInsnList, 
									insBean, mn);

							if (patternValidationInsnList != null
									&& patternValidationInsnList.size() > 0) {
								insnList.insert(abstractInsnNode,
										patternValidationInsnList);
							}
						}
					}
				}
			}
			mn.visitMaxs(0, 0);
		}
	}
	accept(cv);
}
 
Example 13
Source File: SubmitCaseAdapter.java    From jumbune with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void visitEnd() {
	for (Object o : methods) {
		MethodNode mn = (MethodNode) o;
		InsnList insnList = mn.instructions;
		AbstractInsnNode[] insnArr = insnList.toArray();

		/**
		 * Finding the method instruction nodes whose owner is mapreduce.Job
		 * and it submits the job
		 */
		for (AbstractInsnNode abstractInsnNode : insnArr) {
			if (abstractInsnNode instanceof MethodInsnNode) {
				MethodInsnNode min = (MethodInsnNode) abstractInsnNode;

				// finding job submission
				if (min.name.equals(EnumJobSubmitMethods.JOB_SUBMIT
						.toString())) {

					LOGGER.debug(MessageFormat.format(
							InstrumentationMessageLoader
									.getMessage(MessageConstants.JOB_SUBMISSION_FOUND),
							getClassName() + "##" + mn.name));

					// validating that the owner of the method call is

					if (min.owner.equals(EnumJobSubmitMethods.JOB_SUBMIT
							.getOwner().getInternalName())) {
						LOGGER.debug(MessageFormat.format(
								InstrumentationMessageLoader
										.getMessage(MessageConstants.LOG_OWNER_IS_JOB),
								getClassName() + "##" + mn.name));
						AbstractInsnNode ain = min.getPrevious();
						while (!(ain instanceof VarInsnNode)) {
							ain = ain.getPrevious();
						}
						VarInsnNode vin = (VarInsnNode) ain;
						int jobVariableIndex = vin.var;
						InsnList il = null;
						il = handleJobSubmitcase(mn, jobVariableIndex);
						insnList.insert(min, il);
					}
				}
			}
		}
		mn.visitMaxs(0, 0);
	}
	accept(cv);
}
 
Example 14
Source File: MREntryExitAdapter.java    From jumbune with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * visit end for instrumentation of map-reduce methods
 */
@Override
public void visitEnd() {
	if (isMapperClass() || isReducerClass()) {
		for (Object o : methods) {
			MethodNode mn = (MethodNode) o;
			/**
			 * Valid map/reduce method
			 */
			if (InstrumentUtil.validateMapReduceMethod(mn)) {
				InsnList insnList = mn.instructions;
				AbstractInsnNode[] insnArr = insnList.toArray();

				// adding entry logging
				LOGGER.debug(MessageFormat.format(
						InstrumentationMessageLoader
								.getMessage(MessageConstants.LOG_MAPREDUCE_METHOD_ENTRY),
						getClassName() + "##" + mn.name + "##" + mn.desc));
				String logMsg = new StringBuilder(
						MessageFormat.format(
								InstrumentationMessageLoader
										.getMessage(MessageConstants.ENTERED_MAPREDUCE),
								mn.name)).toString();

				// setting the logger number in ThreadLocal
				InsnList il1 = new InsnList();
				il1.add(new LabelNode());
				il1.add(new VarInsnNode(Opcodes.ALOAD, 0));
				il1.add(new FieldInsnNode(
						Opcodes.GETFIELD,
						ConfigurationUtil.convertQualifiedClassNameToInternalName(getClassName()),
						InstrumentConstants.FIELD_LOGGERNUMBER, "I"));
				il1.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
						CLASSNAME_MAPREDUCEEXECUTIL, "setLoggerNumber",
						Type.getMethodDescriptor(Type.VOID_TYPE,
								Type.INT_TYPE)));
				
				String symbol = env.getClassSymbol(getClassName());
				il1.add(InstrumentUtil.addLogMessage(symbol,
						mn.name, logMsg));

				il1.add(addMapCounter(mn));
				insnList.insertBefore(insnList.getFirst(), il1);

				// traversing the instructions for exit logging
				for (AbstractInsnNode abstractInsnNode : insnArr) {
					// return statement
					if (abstractInsnNode.getOpcode() >= Opcodes.IRETURN
							&& abstractInsnNode.getOpcode() <= Opcodes.RETURN) {
						LOGGER.debug(MessageFormat.format(
								InstrumentationMessageLoader
										.getMessage(MessageConstants.LOG_MAPREDUCE_METHOD_EXIT),
								getClassName() + "##" + mn.name));
						String logMsg2 = new StringBuilder(
								MessageFormat.format(
										InstrumentationMessageLoader
												.getMessage(MessageConstants.EXITING_MAPREDUCE),
										mn.name)).toString();
						
						symbol = getLogClazzName(); 
						InsnList il = InstrumentUtil.addLogMessage(
								symbol, mn.name, logMsg2);
						insnList.insert(abstractInsnNode.getPrevious(), il);
					}
				}
			}
			mn.visitMaxs(0, 0);
		}
	}
	accept(cv);
}
 
Example 15
Source File: ProfileAdapter.java    From jumbune with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
	public void visitEnd() {
		for (Object o : methods) {
			MethodNode mn = (MethodNode) o;
			InsnList insnList = mn.instructions;
			AbstractInsnNode[] insnArr = insnList.toArray();

			/**
			 * Finding the method instruction nodes whose owner is mapreduce.Job
			 * and it submits the job
			 */
			for (AbstractInsnNode abstractInsnNode : insnArr) {
				if (abstractInsnNode instanceof MethodInsnNode) {
					MethodInsnNode min = (MethodInsnNode) abstractInsnNode;

					// finding job submission
					if (InstrumentUtil.isJobSubmissionMethod(min)) {

						LOGGER.debug(MessageFormat.format(
								InstrumentationMessageLoader
										.getMessage(MessageConstants.JOB_SUBMISSION_FOUND),
								getClassName() + "##" + mn.name));

						// validating that the owner of the method call is
						if (InstrumentUtil.isOwnerJob(min)) {
							LOGGER.debug(MessageFormat.format(
									InstrumentationMessageLoader
											.getMessage(MessageConstants.LOG_OWNER_IS_JOB),
									getClassName() + "##" + mn.name));
							AbstractInsnNode ain = min.getPrevious();

							while (!(ain instanceof VarInsnNode)) {
								ain = ain.getPrevious();
							}

/*							VarInsnNode vin = (VarInsnNode) ain;
							int jobVariableIndex = vin.var;
							InsnList il = null;

							// old api check
							if (getOwnerType(min).getInternalName().equals(
									CLASSNAME_JOB_CONF)) {
								il = addProfilingForOldAPI(jobVariableIndex);
							} else {
								il = addProfiling(jobVariableIndex);
							}

							insnList.insertBefore(vin, il);
*/						}
					}
				}
			}
			mn.visitMaxs(0, 0);
		}
		accept(cv);
	}
 
Example 16
Source File: JobAdapter.java    From jumbune with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Visit end.
 *
 * @see org.jumbune.debugger.instrumentation.adapter.BaseAdapter#visitEnd()
 */
@Override
public void visitEnd() {
	for (Object o : methods) {
		MethodNode mn = (MethodNode) o;

		InsnList insnList = mn.instructions;
		AbstractInsnNode[] insnArr = insnList.toArray();

		LOGGER.debug(MessageFormat.format(InstrumentationMessageLoader
				.getMessage(MessageConstants.LOG_INSTRUMENTING_METHOD),
				getClassName() + "##" + mn.name));
		// traversing in reverse order as job submission comes at the end of
		// the method
		for (AbstractInsnNode abstractInsnNode : insnArr) {
			if (abstractInsnNode instanceof MethodInsnNode) {
				MethodInsnNode min = (MethodInsnNode) abstractInsnNode;

				// finding job submission
				if (InstrumentUtil.isJobSubmissionMethod(min)) {

					LOGGER.debug(MessageFormat.format(
							InstrumentationMessageLoader
									.getMessage(MessageConstants.JOB_SUBMISSION_FOUND),
							getClassName()));

					// validating if the owner is mapreduce.Job or JobClient
					if (InstrumentUtil.isOwnerJob(min)) {

						LOGGER.debug(MessageFormat.format(
								InstrumentationMessageLoader
										.getMessage(MessageConstants.LOG_OWNER_IS_JOB),
								getClassName()));
						// finding index of job variable
						AbstractInsnNode ain = min.getPrevious();
						while (!(ain instanceof VarInsnNode)) {
							ain = ain.getPrevious();
						}
						VarInsnNode vin = (VarInsnNode) ain;
						int jobVariableIndex = vin.var;

						InsnList il = new InsnList();

						// classpath is to be passed as libjars
						il.add(addClasspath(jobVariableIndex,
								getOwnerType(min)));
						// // output path modification
						il.add(modifyOutputPath(jobVariableIndex,
								getOwnerType(min)));
						insnList.insertBefore(vin, il);

						// Disabling the profiling for the pure jar - old
						// api
						if (getOwnerType(min).getInternalName().equals(
								CLASSNAME_JOB_CONF)) {

							il.add(new LabelNode());
							il.add(new VarInsnNode(Opcodes.ALOAD,
									jobVariableIndex));
							il.add(new LdcInsnNode(false));
							il.add(new MethodInsnNode(
									Opcodes.INVOKEVIRTUAL,
									CLASSNAME_JOB_CONF,
									"setProfileEnabled", Type
											.getMethodDescriptor(
													Type.VOID_TYPE,
													Type.BOOLEAN_TYPE)));
						} else {
							il.add(new LabelNode());
							il.add(new VarInsnNode(Opcodes.ALOAD,
									jobVariableIndex));
							il.add(new MethodInsnNode(
									Opcodes.INVOKEVIRTUAL,
									CLASSNAME_MR_JOB, "getConfiguration",
									Type.getMethodDescriptor(Type
											.getType(Configuration.class))));

							il.add(new LdcInsnNode(PROFILE_TASK));
							il.add(new LdcInsnNode(Boolean.toString(false)));
							il.add(new MethodInsnNode(
									Opcodes.INVOKEVIRTUAL,
									CLASSNAME_HADOOP_CONFIGURATION, "set",
									Type.getMethodDescriptor(
											Type.VOID_TYPE, TYPE_STRING,
											TYPE_STRING)));
						}

						insnList.insertBefore(vin, il);
					}
				}
			}
		}
		mn.visitMaxs(0, 0);
	}
	accept(cv);
}
 
Example 17
Source File: TimerAdapter.java    From jumbune with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * visit end method for intrumentation	
 */
@SuppressWarnings("unchecked")
@Override
public void visitEnd() {
	if (isMapperClass() || isReducerClass()) {
		for (Object o : methods) {
			MethodNode mn = (MethodNode) o;
			if (InstrumentUtil.validateMapReduceMethod(mn)) {
				logger.debug("instrumenting " + getClassName() + "##"
						+ mn.name);
				InsnList list = mn.instructions;
				AbstractInsnNode[] insnArr = list.toArray();
				int variable = mn.maxLocals;

				// adding variable declaration
				InsnList il = new InsnList();
				LabelNode newLabel = new LabelNode();
				il.add(newLabel);
				il.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
						"java/lang/System", "currentTimeMillis", Type
								.getMethodDescriptor(Type.LONG_TYPE)));
				il.add(new VarInsnNode(Opcodes.LSTORE, variable));
				list.insertBefore(list.getFirst(), il);

				// adding local variable
				LabelNode begin = new LabelNode();
				LabelNode end = new LabelNode();
				list.insertBefore(list.getFirst(), begin);
				list.insert(list.getLast(), end);
				Type type = Type.LONG_TYPE;
				LocalVariableNode lv = new LocalVariableNode("startMethod",
						type.getDescriptor(), null, begin, end, variable);
				mn.localVariables.add(lv);

				// finding the return statement
				for (AbstractInsnNode abstractInsnNode : insnArr) {
					if (abstractInsnNode.getOpcode() >= Opcodes.IRETURN
							&& abstractInsnNode.getOpcode() <= Opcodes.RETURN) {
						// adding logging statement
						String msg = new StringBuilder(
								"[Method executed] [time] ").toString();
						String cSymbol = env.getClassSymbol(getClassName());
						String mSymbol = env.getMethodSymbol(getClassName(), cSymbol, mn.name);
						InsnList il1 = InstrumentUtil.addTimerLogging(
								cSymbol,mSymbol, variable, msg);

						list.insertBefore(abstractInsnNode, il1);
					}
				}
			}
			mn.visitMaxs(0, 0);
		}
	}
	accept(cv);
}
 
Example 18
Source File: MethodEntryExitAdapter.java    From jumbune with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
* visit end method for intrumentation	
*/
@Override
public void visitEnd() {
	for (Object o : methods) {
		MethodNode mn = (MethodNode) o;

		// filtering the methods
		if (!(validateMapReduceClinitMethod(mn.name,MAP_METHOD , REDUCE_METHOD,CLINIT_METHOD)
				|| checkMethodNameAndArgumentLength(mn)
				|| (mn.access & Opcodes.ACC_SYNTHETIC) == Opcodes.ACC_SYNTHETIC)) {

			InsnList insnList = mn.instructions;
			AbstractInsnNode[] insnArr = insnList.toArray();

			// adding entry logging
			logger.debug(MessageFormat.format(InstrumentationMessageLoader
					.getMessage(MessageConstants.LOG_METHOD_ENTRY),
					getClassName() + "##" + mn.name + "##" + mn.desc));

			String logMsg = InstrumentationMessageLoader
					.getMessage(MessageConstants.ENTERED_METHOD);
			
			String cSymbol = env.getClassSymbol(getClassName());
			String mSymbol = env.getMethodSymbol(getClassName(),cSymbol, mn.name);
			
			InsnList il = InstrumentUtil.addLogMessage(cSymbol,
					mSymbol, logMsg);
			insnList.insertBefore(insnList.getFirst(), il);

			for (AbstractInsnNode abstractInsnNode : insnArr) {
				if (Opcodes.RETURN >= abstractInsnNode.getOpcode()
						&& Opcodes.IRETURN <= abstractInsnNode.getOpcode()) {
					// adding exit logging
					logger.debug(MessageFormat.format(
							InstrumentationMessageLoader
									.getMessage(MessageConstants.LOG_METHOD_EXIT),
							getClassName() + "##" + mn.name));

					logMsg = InstrumentationMessageLoader
							.getMessage(MessageConstants.EXITING_METHOD);
					cSymbol = env.getClassSymbol(getClassName());
					mSymbol = env.getMethodSymbol(getClassName(),cSymbol,mn.name);
					il = InstrumentUtil.addLogMessage(cSymbol,
							mSymbol, logMsg);

					// inserting the list at the associated label node
					AbstractInsnNode prevNode = abstractInsnNode
							.getPrevious();
					while (!(prevNode instanceof LabelNode)) {
						prevNode = prevNode.getPrevious();
					}
					insnList.insert(prevNode, il);
				}
			}
		}
		mn.visitMaxs(0, 0);
	}
	accept(cv);
}
 
Example 19
Source File: PartitionerAdapter.java    From jumbune with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * method for instrumenting jar for debugging partitioner
 */
@Override
public void visitEnd() {
	if (isMapperClass()) {
		LOG.debug("Instrumenting jar for debugging partitioner !!");
		for (int i = 0; i < methods.size(); i++) {
			MethodNode mn = (MethodNode) methods.get(i);
			final int variableIndex = mn.maxLocals;

			if (InstrumentUtil.validateMapMethod(mn)) {
				InsnList insnList = mn.instructions;
				AbstractInsnNode[] insnArr = insnList.toArray();

				// Get the index of temporary variables of key and value
				int keyVariableIndex = ContextWriteParams.getInstance()
						.getTempKeyVariableIndex();
				int valueVariableIndex = ContextWriteParams.getInstance()
						.getTempValueVariableIndex();

				LOG.debug("Index set in ContextWriteParams  "
						+ keyVariableIndex + " Index of Value  "
						+ valueVariableIndex + " for class "
						+ getClassName());

				for (AbstractInsnNode abstractInsnNode : insnArr) {
					if (abstractInsnNode instanceof MethodInsnNode) {
						MethodInsnNode min = (MethodInsnNode) abstractInsnNode;

						if (InstrumentUtil.isOutputMethod(min)) {
							// If both the key and value index are 0 that
							// means its yet not copied to temporary
							// variables just copy these params to temporary
							// variables
							if (keyVariableIndex == 0
									&& valueVariableIndex == 0) {
								LOG.debug("Since params are not copied to temporary variables do it now !!!!! "
										+ " \n previous to context.write  "
										+ min.getPrevious()
										+ " variableIndex "
										+ variableIndex
										+ " insnList " + insnList.size());
								InstructionsBean insBean = MethodByteCodeUtil
										.readMethodAndCopyParamToTemporaryVariables(
												min.getPrevious(),
												variableIndex, insnList,
												mn.localVariables);
								LOG.debug("After the instructions were added size of insnList :: "
										+ insnList.size());
								// Add the instance of temporary key/value
								// index to ContextWriteParams
								keyVariableIndex = insBean
										.getTemporaryVariablesIndexList()
										.get(KEY_INDEX);
								valueVariableIndex = insBean
										.getTemporaryVariablesIndexList()
										.get(VALUE_INDEX);

								ContextWriteParams.getInstance()
										.setTempKeyVariableIndex(
												keyVariableIndex);
								ContextWriteParams.getInstance()
										.setTempValueVariableIndex(
												valueVariableIndex);
							}
							// Add instructions for detecting the time taken
							// in partitioning
							insnList.insert(
									abstractInsnNode,
									getInsnListForCalculatePartitioningTime(
											keyVariableIndex,
											valueVariableIndex));
						}
					}
				}
			}
			mn.visitMaxs(0, 0);
		}
	}
	accept(cv);
}
 
Example 20
Source File: TrashCodeTransformer.java    From obfuscator with MIT License 4 votes vote down vote up
@NotNull
private MethodNode createMethod() {
    final MethodNode methodNode = new MethodNode((RandomUtil.nextInt(2) == 1 ? ACC_PUBLIC | ACC_STATIC : ACC_PRIVATE | ACC_STATIC), StringUtil.generateString(RandomUtil.nextInt(16) + 2), "()V", null, null);
    methodNode.visitCode();

    final Label label0 = new Label();
    methodNode.visitLabel(label0);
    methodNode.visitLineNumber(8, label0);
    methodNode.visitInsn(ICONST_0);
    methodNode.visitVarInsn(ISTORE, 0);

    final AtomicInteger index = new AtomicInteger(10);
    final Label label1 = new Label();
    methodNode.visitLabel(label1);
    methodNode.visitLineNumber(index.getAndIncrement(), label1);
    methodNode.visitVarInsn(ILOAD, 0);
    methodNode.visitLdcInsn(300000);
    methodNode.visitInsn(IADD);
    methodNode.visitVarInsn(ISTORE, 0);

    IntStream.range(0, RandomUtil.nextInt(216) + 1).mapToObj(i -> new Label()).forEachOrdered(label2 -> {
        methodNode.visitLabel(label2);
        methodNode.visitLineNumber(index.getAndIncrement(), label2);
        methodNode.visitVarInsn(ILOAD, 0);
        methodNode.visitLdcInsn(300000);
        methodNode.visitInsn(IADD);
        methodNode.visitVarInsn(ISTORE, 0);
    });

    final Label label3 = new Label();
    methodNode.visitLabel(label3);
    methodNode.visitLineNumber(66, label3);
    methodNode.visitInsn(RETURN);

    final Label label58 = new Label();
    methodNode.visitLabel(label58);
    methodNode.visitLocalVariable("i", "I", null, label1, label58, 0);
    methodNode.visitMaxs(2, 1);
    methodNode.visitEnd();

    return methodNode;
}