Invalid register: vX. Must be between v0 and v15, inclusive.

2,925 views
Skip to first unread message

Jaime Fernández

unread,
Mar 10, 2014, 10:25:21 AM3/10/14
to apk...@googlegroups.com
Hi,

I'm trying to add smali code to an existing project but if I add a register greater than 15 I got this error:

 Invalid register: v16. Must be between v0 and v15, inclusive.

I've updated locals directive but I still get this error.

The code that I want to add:

a=0;//     const-string v16, "STRING"
a=0;//     invoke-static {v16}, Lcom/example/Utils;->dump(Ljava/lang/String;)V

What I'm doing wrong?

Thanks for your help.

Ryszard Wiśniewski

unread,
Mar 10, 2014, 11:02:41 AM3/10/14
to apk...@googlegroups.com

Hi Jaime, most of dalvik instructions can only access first 16 registers. Registers above them are basically for storing values for later use and can't be accessed directly by math operations, etc. As far as I know only move instructions can use these registers - and not all variants of move.

--
You received this message because you are subscribed to the Google Groups "apktool" group.
To unsubscribe from this group and stop receiving emails from it, send an email to apktool+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jaime Fernández

unread,
Mar 10, 2014, 11:26:23 AM3/10/14
to apk...@googlegroups.com
Ok! Well, then I'll have to watch first 16 registers to figure out what register I can use. Thanks for your help!

Ryszard Wiśniewski

unread,
Mar 10, 2014, 11:32:51 AM3/10/14
to apk...@googlegroups.com

Look for some register that you can "export" above 16, then use it for your needs and then "import" its value back before it's used by original code. Quite awkward, but this is how it is done - even in the code created by dx tool.

Jaime Fernández

unread,
Mar 10, 2014, 12:35:00 PM3/10/14
to apk...@googlegroups.com
Perfect!
Thank you very much!

Jaime Fernández

unread,
Mar 18, 2014, 6:56:15 AM3/18/14
to apk...@googlegroups.com
Hi! I have a new issue regarding to this question. In some methods I can't increment .locals number. For example, I have a method with ".locals 14" and I want to use a new register so I increment .locals to 15. When I run apktool I got this error:

I: Using Apktool 2.0.0-Beta9 on out
I: Checking whether sources has changed...
I: Smaling...
../../../../../../../tmp/BRUT4802429665909000465.bak[29,0] Invalid register: v16. Must be between v0 and v15, inclusive.
Exception in thread "main" brut.androlib.AndrolibException: Could not smali file: com/example/test/MainActivity.java
at brut.androlib.src.SmaliBuilder.buildFile(SmaliBuilder.java:115)
at brut.androlib.src.SmaliBuilder.build(SmaliBuilder.java:56)
at brut.androlib.src.SmaliBuilder.build(SmaliBuilder.java:41)
at brut.androlib.Androlib.buildSourcesSmali(Androlib.java:338)
at brut.androlib.Androlib.buildSources(Androlib.java:299)
at brut.androlib.Androlib.build(Androlib.java:285)
at brut.androlib.Androlib.build(Androlib.java:258)
at brut.apktool.Main.cmdBuild(Main.java:236)
at brut.apktool.Main.main(Main.java:88)

Thanks!

Ryszard Wiśniewski

unread,
Mar 18, 2014, 8:29:06 AM3/18/14
to apk...@googlegroups.com

I'm not 100% sure this is the case, but I think so.

The problem is: pX registers are just regular vX registers and above limits affects them as well. p0 is always the first free vX register, so in your case p0 = v14. Actually you can use both of these names - they are the same. If you replace ".locals v14" with ".locals v15" then p0 remains in first 16 registers, so it should be ok. p2, if there is one, was above 16 regs from the beginning, so it should be ok as well. Unfortunately p1 was v15 and now it's v16, so existing code isn't valid anymore, because it tries to use this register directly.

I don't see a straightforward way to fix this problem. I think you will have to modify existing code.

Jaime Fernández

unread,
Mar 18, 2014, 11:22:08 AM3/18/14
to apk...@googlegroups.com
Hi,
I have this problem with methods with no arguments.

Ryszard Wiśniewski

unread,
Mar 18, 2014, 2:39:25 PM3/18/14
to apk...@googlegroups.com
Hmm... and do you maybe change ".locals 15" to ".locals 16" for non-static methods? Because non-static methods have implicit p0 register with "this" reference in it. If this is not the case then post some example, please.

Jaime Fernández

unread,
Mar 19, 2014, 7:03:56 AM3/19/14
to apk...@googlegroups.com
You're right! Sorry by this stupid mistake :)

Thanks very much!

Ryszard Wiśniewski

unread,
Mar 19, 2014, 7:06:18 AM3/19/14
to apk...@googlegroups.com

No problem :-)

Reply all
Reply to author
Forward
0 new messages