JesusFreke / smali

smali/baksmali
6.29k stars 1.07k forks source link

hiddenapi: smali creates invalid dex file #815

Closed robertsmd closed 3 years ago

robertsmd commented 3 years ago

running the following command:

java -jar ~/Downloads/smali-2.5.2.jar a -a 29 -o classes.dex classes_dex

results in

>>> dexdump classes.dex
Processing 'classes.dex'...
dexdump E 05-11 19:28:27 28058 4030355 dexdump.cc:1884] Failure to verify dex file 'classes.dex': Non-zero padding 7f before section of type 61440 at offset 0x8998be

or from dex2oat:

05-11 20:07:58.018 22289 22289 E dex2oat : Failed to open dex file for layout: Failure to verify dex file: Non-zero padding 7f before section of type 61440 at offset 0x899892

I am running on an api 29 emulator.

any help is appreciated!

JesusFreke commented 3 years ago

Interesting. It sounds like the hiddenapi section has an undocumented alignment requirement.

robertsmd commented 3 years ago

Interesting. It sounds like the hiddenapi section has an undocumented alignment requirement.

Might be a hint here: https://android.googlesource.com/platform/art/+/master/tools/hiddenapi/hiddenapi.cc#621

robertsmd commented 3 years ago

@JesusFreke looks like I'll need your help here. Adding getItemAlignment() (returning 4 for uint_32) to the class HiddenApiClassDataItem didn't work the trick for me.

robertsmd commented 3 years ago

@Lanchon @iBotPeaches any help here would be appreciated so we can fix this, since you all have more experience with the dex format.

robertsmd commented 3 years ago

Adding line offsetWriter.align(); just before this line fixes the error above, but results in additional errors when verifying:

dexdump E 05-12 22:59:12 80234 5118856 dexdump.cc:1884] Failure to verify dex file 'classes.dex': Hiddenapi class data unexpected offset (27508 != 27506) for class def 42
robertsmd commented 3 years ago

appears to be an issue where the sorting isn't matching what is expected

robertsmd commented 3 years ago

Issue solved. writeClass needs to be called on the classes in order sorted by key (class name), but writerestriction (for hiddenapi restrictions) needs to be called on the classes in order sorted by the value (numerically ordered by class_idx (more info on class_idx can be found here. PR #816 to fix it is submitted. @JesusFreke