diff options
author | Dan Bornstein <danfuzz@android.com> | 2011-05-25 13:15:47 -0700 |
---|---|---|
committer | Dan Bornstein <danfuzz@android.com> | 2011-05-25 16:23:03 -0700 |
commit | 9fdbd91288a237eb58e18e4de9c729c3c268c318 (patch) | |
tree | 3dd76752feac32b74906a8b59f0cc974e3db168d /libdex | |
parent | 43e796a261bec308b44e9e5a627820845812202c (diff) | |
download | android_dalvik-9fdbd91288a237eb58e18e4de9c729c3c268c318.tar.gz android_dalvik-9fdbd91288a237eb58e18e4de9c729c3c268c318.tar.bz2 android_dalvik-9fdbd91288a237eb58e18e4de9c729c3c268c318.zip |
Update dex file magic number.
Even though the dex format was technically resilient with respect to
the addition of new opcodes, consensus is that the errors one sees
when trying to use a new dex file on an old build were sufficiently
inscrutable that it was worth the effort to update the version number
embedded in the dex format magic.
This change updates dx to produce the new version number when extended
opcodes are enabled (which is the default, but may be overridden by
targeting an older API level).
This also updates the vm to recognize and accept both the new current
version number as well as the immediately previous one. Note: It won't
reject an old-version file if it happens to use the new opcodes; that
would just be a gratuitous and pointless failure.
Bug: 4364986
Change-Id: If8febbb0b91c1719df4247bf69c511251362d91f
Diffstat (limited to 'libdex')
-rw-r--r-- | libdex/DexFile.cpp | 11 | ||||
-rw-r--r-- | libdex/DexFile.h | 18 | ||||
-rw-r--r-- | libdex/DexSwapVerify.cpp | 45 |
3 files changed, 46 insertions, 28 deletions
diff --git a/libdex/DexFile.cpp b/libdex/DexFile.cpp index 4f3bf5630..da9affc29 100644 --- a/libdex/DexFile.cpp +++ b/libdex/DexFile.cpp @@ -336,16 +336,7 @@ DexFile* dexFileParse(const u1* data, size_t length, int flags) dexFileSetupBasicPointers(pDexFile, data); pHeader = pDexFile->pHeader; - magic = pHeader->magic; - if (memcmp(magic, DEX_MAGIC, 4) != 0) { - /* not expected */ - LOGE("bad magic number (0x%02x %02x %02x %02x)\n", - magic[0], magic[1], magic[2], magic[3]); - goto bail; - } - if (memcmp(magic+4, DEX_MAGIC_VERS, 4) != 0) { - LOGE("bad dex version (0x%02x %02x %02x %02x)\n", - magic[4], magic[5], magic[6], magic[7]); + if (!dexHasValidMagic(pHeader)) { goto bail; } diff --git a/libdex/DexFile.h b/libdex/DexFile.h index 4a090e148..c4f3f09b1 100644 --- a/libdex/DexFile.h +++ b/libdex/DexFile.h @@ -49,8 +49,15 @@ /* DEX file magic number */ #define DEX_MAGIC "dex\n" -/* version, encoded in 4 bytes of ASCII */ -#define DEX_MAGIC_VERS "035\0" + +/* current version, encoded in 4 bytes of ASCII */ +#define DEX_MAGIC_VERS "036\0" + +/* + * older but still-recognized version (corresponding to Android API + * levels 13 and earlier + */ +#define DEX_MAGIC_VERS_API_13 "035\0" /* same, but for optimized DEX header */ #define DEX_OPT_MAGIC "dey\n" @@ -560,6 +567,13 @@ int dexSwapAndVerify(u1* addr, int len); int dexSwapAndVerifyIfNecessary(u1* addr, int len); /* + * Check to see if the file magic and format version in the given + * header are recognized as valid. Returns true if they are + * acceptable. + */ +bool dexHasValidMagic(const DexHeader* pHeader); + +/* * Compute DEX checksum. */ u4 dexComputeChecksum(const DexHeader* pHeader); diff --git a/libdex/DexSwapVerify.cpp b/libdex/DexSwapVerify.cpp index ea05e0c7a..9a465dd4d 100644 --- a/libdex/DexSwapVerify.cpp +++ b/libdex/DexSwapVerify.cpp @@ -2781,6 +2781,32 @@ static bool crossVerifyEverything(CheckState* state, DexMapList* pMap) return okay; } +/* (documented in header file) */ +bool dexHasValidMagic(const DexHeader* pHeader) +{ + const u1* magic = pHeader->magic; + const u1* version = &magic[4]; + + if (memcmp(magic, DEX_MAGIC, 4) != 0) { + LOGE("ERROR: unrecognized magic number (%02x %02x %02x %02x)", + magic[0], magic[1], magic[2], magic[3]); + return false; + } + + if ((memcmp(version, DEX_MAGIC_VERS, 4) != 0) && + (memcmp(version, DEX_MAGIC_VERS_API_13, 4) != 0)) { + /* + * Magic was correct, but this is an unsupported older or + * newer format variant. + */ + LOGE("ERROR: unsupported dex version (%02x %02x %02x %02x)", + version[0], version[1], version[2], version[3]); + return false; + } + + return true; +} + /* * Fix the byte ordering of all fields in the DEX file, and do * structural verification. This is only required for code that opens @@ -2798,25 +2824,12 @@ int dexSwapAndVerify(u1* addr, int len) LOGV("+++ swapping and verifying\n"); /* - * Start by verifying the magic number. The caller verified that "len" - * says we have at least a header's worth of data. + * Note: The caller must have verified that "len" is at least as + * large as a dex file header. */ pHeader = (DexHeader*) addr; - if (memcmp(pHeader->magic, DEX_MAGIC, 4) != 0) { - /* really shouldn't be here -- this is weird */ - LOGE("ERROR: Can't byte swap: bad magic number " - "(0x%02x %02x %02x %02x)\n", - pHeader->magic[0], pHeader->magic[1], - pHeader->magic[2], pHeader->magic[3]); - okay = false; - } - if (okay && memcmp(pHeader->magic+4, DEX_MAGIC_VERS, 4) != 0) { - /* older or newer version we don't know how to read */ - LOGE("ERROR: Can't byte swap: bad dex version " - "(0x%02x %02x %02x %02x)\n", - pHeader->magic[4], pHeader->magic[5], - pHeader->magic[6], pHeader->magic[7]); + if (!dexHasValidMagic(pHeader)) { okay = false; } |