google / smali

Other
209 stars 34 forks source link

different signature after compilation #66

Open farfromrefug opened 3 months ago

farfromrefug commented 3 months ago

I have a script which modifies a jar using APKTool which uses smali 3.0.7. With the recompiled jar ,I end up with errors like this:

java.lang.String com.android.server.policy.SingleKeyGestureDetector$MessageObject.toString() failed to verify: java.lang.String com.android.server.policy.SingleKeyGestureDetector$MessageObject.toString(): [0x0] invalid argument count (1) exceeds outsSize (0) (declaration of 'com.android.server.policy.SingleKeyGestureDetector$MessageObject' appears in /system/framework/services.jar!classes2.dex)
                         E      at com.android.server.policy.SingleKeyGestureDetector.interceptKeyDown(SingleKeyGestureDetector.java:286)
                         E      at com.android.server.policy.SingleKeyGestureDetector.interceptKey(SingleKeyGestureDetector.java:220)
                         E      at com.android.server.policy.PhoneWindowManager.handleKeyGesture(PhoneWindowManager.java:5795)
                         E      at com.android.server.policy.PhoneWindowManager.interceptKeyBeforeQueueing(PhoneWindowManager.java:5295)
                         E      at com.android.server.wm.InputManagerCallback.interceptKeyBeforeQueueing(InputManagerCallback.java:165)
                         E      at com.android.server.input.InputManagerService.interceptKeyBeforeQueueing(InputManagerService.java:2563)

It seems like the method com.android.server.policy.SingleKeyGestureDetector$MessageObject.toString() does not have the same signature in the rebuilt jar.

melcz commented 1 month ago

Hi, could you try with a built version of main? If that doesn't solve it, we'll need more info to trace the issue within dexlib2

MG1937 commented 1 month ago

Hi, could you try with a built version of main? If that doesn't solve it, we'll need more info to trace the issue within dexlib2

@melcz @farfromrefug Hi, I fixed the issue, the exception happen due to opcode "invoke-custom"'s referenceType CALL_SITE, because inside DexWriter#writeCodeItem function, code do not handle outParamCount for CALL_SITE referenceType, so when a method body only have invoke-custom inst (which usually happen when turn "record" keyword function into smali code), the final outs_size of this function will be zero, then ART VM will throw out "exceeds outsSize" exception.

And BTW I add below code after referenceType check to fix this issue:

                } else if (instruction.getOpcode().referenceType == ReferenceType.CALL_SITE) {
                    outParamCount = ((VariableRegisterInstruction)instruction).getRegisterCount();
                }
farfromrefug commented 1 month ago

@MG1937 Awesome! It is commited? I dont see it

MG1937 commented 1 month ago

@MG1937 Awesome! It is commited? I dont see it

No, I don't submit PR, I just change the code and rebuild it in local😂

farfromrefug commented 1 month ago

@MG1937 Awesome! It is commited? I dont see it

No, I don't submit PR, I just change the code and rebuild it in local😂

OK i might try to create a PR. Can you point me to where i need to change the code. That i can try to build locally and see if it works.

MG1937 commented 1 month ago

@MG1937 Awesome! It is commited? I dont see it

No, I don't submit PR, I just change the code and rebuild it in local😂

OK i might try to create a PR. Can you point me to where i need to change the code. That i can try to build locally and see if it works.

you can search below code inside DexWriter#writeCodeItem function: if (instruction.getOpcode().referenceType == ReferenceType.METHOD) then add the patch code I mentioned before after this if statement to fix this issue. patch code:

                } else if (instruction.getOpcode().referenceType == ReferenceType.CALL_SITE) {
                    outParamCount = ((VariableRegisterInstruction)instruction).getRegisterCount();
                }
farfromrefug commented 1 month ago

@MG1937 Awesome! It is commited? I dont see it

No, I don't submit PR, I just change the code and rebuild it in local😂

OK i might try to create a PR. Can you point me to where i need to change the code. That i can try to build locally and see if it works.

you can search below code inside DexWriter#writeCodeItem function: if (instruction.getOpcode().referenceType == ReferenceType.METHOD) then add the patch code I mentioned before after this if statement to fix this issue. patch code:

                } else if (instruction.getOpcode().referenceType == ReferenceType.CALL_SITE) {
                    outParamCount = ((VariableRegisterInstruction)instruction).getRegisterCount();
                }

Thanks a lot. I am testing right now! if it works i will create a PR.

farfromrefug commented 1 month ago

@MG1937 it works! thank you so much. I created a PR referencing you @melcz can you look at the PR https://github.com/google/smali/pull/76 ? Thanks