Jump to content
Tuts 4 You

How to add new instruction with dexlib2?


Modify

Recommended Posts

I try to add new instruction,

// old

const/v4 v?, 0x0

// new

const/v4 v?, 0x0
const-string v?, "Hello" // new instruction

can someone help me, adding instruction without duplicate register number?

Link to comment

@CodeExplorer

I try with this code:

private static MethodImplementation addNewInstructions(@Nonnull MethodImplementation implementation) {

		MutableMethodImplementation mutableImplementation = new MutableMethodImplementation(implementation);

		List<BuilderInstruction> instructions = mutableImplementation.getInstructions();
		
		for (int i = 0; i < instructions.size(); i++) {
			
			Instruction instruction = instructions.get(i);
			
			if (instruction instanceof ReferenceInstruction) {

				if (((ReferenceInstruction) instruction).getReferenceType() == ReferenceType.METHOD) {

					Opcode opcode = instruction.getOpcode();

					if (!opcode.name.endsWith("range")) {
						mutableImplementation.addInstruction(i++, new BuilderInstruction21c(Opcode.CONST_STRING,
								0, new ImmutableStringReference("Hello World"))); // look
					}
				}
			}
		}

		return mutableImplementation;
	}

after :

// before

.line 54
iget-object v0, p0, Lapp/test/MainActivity;->ed:Landroid/widget/TextView;


// after

.line 54
iget-object v0, p0, Lapp/test/MainActivity;->ed:Landroid/widget/TextView; // v0 already register

const-string v0, "Hello World" // v0 duplicate register

If i storing a new value in it, I'll be clobbering any value that was already there, and later code that expects the previous value will likely fail.

Edited by Modify
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...