Automatic Android dex code transformation

I want to convert / Dex tools. Transformation goals include measuring code coverage. Please note that source files are not available. Thus, the Dex tool is the only option.

I am wondering if there is any existing code base that I could look at as examples to write a tool to achieve my goal.

I know about the Smali project and many other projects that are built on Smali. However, none of these projects is a good example for my purpose.

I am looking for code that automatically converts the smali code or dexlib view from which smali is generated. A later option is preferred for my purpose, because you can avoid the overhead of creating smali.

+8
android dalvik dex smali
source share
5 answers

In some cases, smali itself does a small rewrite of the commands when reassembling the dex file. Things like replacing a const string with a const-string / jumbo or a goto statement with a β€œlarger” one if the target is out of range. This includes replacing instructions on the potentially large instruction list and correcting offsets accordingly.

CodeItem.fixInstructions is the method responsible for this.


In addition, there is an asmdex library . I'm not so familiar with this, but it looks like it might be related to what you want to do.

+1
source share

This is a lot of code, but dx DexMerger is an example program that converts dex files. This was complicated by the fact that he needed to guess the size of the output so that the work on the links worked.

You will also need to create an infrastructure for rewriting dalvik instructions. DexMerger InstructionTransformer does a shallow rewrite: it corrects offsets from one mapping to another. To measure code coverage, your command rewrite probably should be much more complex.

+3
source share

Another opportunity that has recently appeared is Dexpler . This is the Soot extension, which is the basis for the analysis and toolkit of Java programs. Dexpler reads the .apk files and converts them to the intermediate Jimple format. Then the Jimple code can be arbitrarily installed, and eventually dumped into a new apk.

+2
source share

(For the record, I answer my question here)

In the end, I did not find a tool that matched my requirements. So I founded my own tool called Ella based on DexLib. Out of the box, it performs several functions such as measuring code coverage, traces of recording marks, etc. But it can be easily expanded to do other types of transformations.

+1
source share

I know this a little late, but just in case you are still interested, or perhaps for some other readers. ASMDEX has already been mentioned. And I think that your best choice at the moment for what you are trying to achieve.

As for adding new registers, look at the class org.ow2.asmdex.util.RegisterShiftMethodAdapter. This is not perfect! Actually, since this is a terrible change to existing 4-bit instructions when adding a register, this means that some kind of register will turn out to be 0xF and will not go into 4 bits.

But this should be a good start.

0
source share

All Articles