JesusFreke / smali

smali/baksmali
6.29k stars 1.07k forks source link

dexlib2: update builder instruction upon instruction insertion/deletion #786

Closed auermich93 closed 3 years ago

auermich93 commented 3 years ago

Hi,

wouldn't it be desired if the method location of a builder instruction would be updated, e.g. consider the following use case:

MutableMethodImplementation mutableMethodImplementation = new  MutableMethodImplementation(methodImplementation);
List<BuilderInstruction> builderInstructions = mutableMethodImplementation.getInstructions();

var firstInstruction = builderInstructions.get(0);
LOGGER.debug("Index: " + firstInstruction.getLocation().getIndex());

// shift original first instruction by one position
mutableMethodImplementation.addInstruction(0, someInstruction);

LOGGER.debug("Index: " + firstInstruction.getLocation().getIndex());

I know that the MutableMethodImplementation class doesn't directly maintain a list of instructions.

JesusFreke commented 3 years ago

Modifying the method invalidates existing instructions (sorry, I know - not documented :).

I think this should work:

MutableMethodImplementation mutableMethodImplementation = new  MutableMethodImplementation(methodImplementation);
List<BuilderInstruction> builderInstructions = mutableMethodImplementation.getInstructions();

LOGGER.debug("Index: " + builderInstructions.get(0).getLocation().getIndex());

// shift original first instruction by one position
mutableMethodImplementation.addInstruction(0, someInstruction);

LOGGER.debug("Index: " + builderInstructions.get(0).getLocation().getIndex());
auermich93 commented 3 years ago

I know that this works, but I intended to keep a list of instrumentation points, where an instrumentation point encapsulates an index and a builder instruction. The index refers to the instruction index of the unmodified method and the builder instruction was meant to keep the updated location. I have a working solution but an up-to-date builder instruction would simplify things.