For every instance (virtual, non-static) method in Dalvik, the first parameter is a reference to itself, or, in Java, the
this reference. I wanted to know if it was legal to reassign the register value.
Just so I’m sure you know what I’m talking about, here’s a simple Java class with an instance method called
The above smali gets turned into this (you can safely ignore
Do your decompilations look different? It may be because mine was generated using
baksmali --use-locals which separates the registers into registers used within the method body (locals) and those passed as parameters. Local registers are named
v2, and so on and the parameters are named
The default behavior is to name all registers based on how they’re actually laid out by Dalvik:
r2 and so on, regardless of if they’re local or parameters. To clarify, a method like this:
Has a register layout like this:
r2- local registers
thisregister (p0 with
Jparameter (wide types use two registers)
I wondered if
p0 was somehow special and if it was possible to rewrite it. One of the optimizers I’m working on needs to rewrite Smali and it works best if it knows all of the available registers at a certain point in code. A register is “available” if it’s not used for the rest of the execution. If you’ve ever written a tool to automatically modify Smali, you have probably run into this problem.
Spoiler warning: It is not special and it is possible to reassign
Here’s the code I used to test:
And then to compile and run it:
The test code outputs the expected
5 with no errors or warnings. It makes sense that a register should be able to hold a reference to anything, but the only way to be absolutely sure (without closely examining the source) is to test it.