Android DEX 文件打开分析

来源:互联网 发布:淘宝开店支付宝提现 编辑:程序博客网 时间:2024/06/06 03:38

函数为
DexFile_openDexFileNative

ClassLinker::OpenDexFilesFromOat
1、 FindOpenedOatDexFile 查找缓存中是否存在,checksum为空或者相同
2、 CreateOatFileForDexLocation
一、生成oat文件 执行execv 调用 dex2oat 生成Oat 会启动进程生成较慢
std::string dex2oat(Runtime::Current()->GetCompilerExecutable()); 文件路径待研究
execv执行成功就返回成功,不管是不是成功
二、OatFile::Open打开,如果失败返回null OatFile* OatFile::Open
调用 ElfFile::Open 判断非ELF文件返回NULL

3、CreateOatFileForDexLocation 生成失败 调用 DexFile::Open 不生成优化

  // Failed, bail.  if (open_oat_file.get() == nullptr) {    std::string error_msg;    // dex2oat was disabled or crashed. Add the dex file in the list of dex_files to make progress.    DexFile::Open(dex_location, dex_location, &error_msg, dex_files);    error_msgs->push_back(error_msg);    return false;  }

经过以上分析 内存打开Dex文件的时候 hook execv 然后失败,防止转换oat成功可以提高首次启动速度,且能
正常运行,但是每次的速度是变慢的,DEX加固的时候有的手机会出现卡死的情况(有buggly时才会出现),暂时
不知道原因,待以后研究明白在说明,如果有人知道原因也麻烦告知。
后来发现凡是不生成oat都会有一定的概率卡屏,实际上是收到BUS_ADRERR信号,通过调试访问的地址正是dex文件的映射地址,但是内存访问确实没有问题的,因此猜测是虚拟机发出的信号,具体机制还不清楚,又经过测试, Dalvik虚拟机打开dex文件存在同样的问题。待深入研究后才能确定是具体原因。

原创粉丝点击