1

I am trying to logcat the parameters of a function in smali with my own function. I made a new Android studio project and wrote the function then used APKtool to retrieve the Smali file.

This is my function:

.method public userFunction(II)V
    .locals 2
    .param p1, "a"    # I
    .param p2, "b"    # I

    .line 28
    invoke-static {p1}, Ljava/lang/Integer;->toString(I)Ljava/lang/String;

    move-result-object v0

    const-string v1, "Message"

    invoke-static {v1, v0}, Landroid/util/Log;->d(Ljava/lang/String;Ljava/lang/String;)I

    return-void
.end method

As you can see, it takes two integers and then logs one (forget for now that the other integer is unused).

This is the Android studio code I used to write the above function in Java.

    public void sendMessage(View view)
    {
        userFunction(3,2);
    }

    public void userFunction(int a, int b)
    {
        Log.d("Message", Integer.toString(a));
    }

Now what I can't make sense of, is how to call this function in Smali. My problem may be that I am testing this within a button. The sendMessage is an Onclick method for testing purposes and all it does is perform the userfunction.

My reasoning behind this is that the method call in Smali ends up being

invoke-virtual {p0, v0, v1}, Lcom/example/froggyfresh/MainActivity;->userFunction(II)V

From what I read, I believe the p0 to be the object calling the function. So I'm not sure how to call the function from within another method that is in the project I am trying to debug.

Is there a work around for this, to create some generic function to log the values?

The issue I get when using p0 in the APK I am trying to debug is APKtool gives me this error:

C:\Users\Dell\Desktop>apktool b base
I: Using Apktool 2.4.1
I: Checking whether sources has changed...
I: Smaling smali folder into classes.dex...
base\smali\funproject.smali[5059,1] Invalid register: v29. Must be between v0 and v15, inclusive.
Could not smali file: funproject.smali

I know that the p0 is a parameter register and that the parameters are stored in the final v(insertnumberhere) register. So I can make sense of the error but not sure how to go about fixing the issue.

1 Answer 1

3
+50

Just make your helper method a static method, and invoke it using invoke-static

invoke-static {v0, v1}, Lcom/example/froggyfresh/MainActivity;->userFunction(II)V

Regarding the register out of range thing, you would need to do some register value shuffling using the move instructions to get the values you need into the lower 16 registers.

e.g., say that you have a register, v23 that is unused. You could move the value from one of the lower registers into v23, and then move the value of p0 into that lower register.

move-object v2, v23
move-object p0, v2

and then after the call, you would need to shuffle the original value of v2 back

move-object v23, v2
2
  • Why are the 17-28th registers out of bounds? I was going off what you wrote here: github.com/JesusFreke/smali/wiki/Registers And from what I understand from what you wrote you make it seem like these registers can be referenced.
    – Kippet
    Jan 18, 2020 at 20:38
  • Each instruction has a certain number of bits to encode each register. Different instructions have different numbers of bits. the non-range invoke instructions have 4 bits to encode each register. You can only encode from 0 to 15 with 4 bits. If you look at the instruction reference, it shows the number of bits allocated for each register in each instruction. (source.android.com/devices/tech/dalvik/dalvik-bytecode)
    – JesusFreke
    Jan 19, 2020 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.