Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "vdex_file.h"
     18 
     19 #include <sys/mman.h>  // For the PROT_* and MAP_* constants.
     20 #include <sys/stat.h>  // for mkdir()
     21 
     22 #include <memory>
     23 #include <unordered_set>
     24 
     25 #include <android-base/logging.h>
     26 
     27 #include "base/bit_utils.h"
     28 #include "base/leb128.h"
     29 #include "base/stl_util.h"
     30 #include "base/unix_file/fd_file.h"
     31 #include "class_linker.h"
     32 #include "class_loader_context.h"
     33 #include "dex/art_dex_file_loader.h"
     34 #include "dex/class_accessor-inl.h"
     35 #include "dex/dex_file_loader.h"
     36 #include "dex_to_dex_decompiler.h"
     37 #include "gc/heap.h"
     38 #include "gc/space/image_space.h"
     39 #include "quicken_info.h"
     40 #include "runtime.h"
     41 #include "verifier/verifier_deps.h"
     42 
     43 namespace art {
     44 
     45 constexpr uint8_t VdexFile::VerifierDepsHeader::kVdexInvalidMagic[4];
     46 constexpr uint8_t VdexFile::VerifierDepsHeader::kVdexMagic[4];
     47 constexpr uint8_t VdexFile::VerifierDepsHeader::kVerifierDepsVersion[4];
     48 constexpr uint8_t VdexFile::VerifierDepsHeader::kDexSectionVersion[4];
     49 constexpr uint8_t VdexFile::VerifierDepsHeader::kDexSectionVersionEmpty[4];
     50 
     51 bool VdexFile::VerifierDepsHeader::IsMagicValid() const {
     52   return (memcmp(magic_, kVdexMagic, sizeof(kVdexMagic)) == 0);
     53 }
     54 
     55 bool VdexFile::VerifierDepsHeader::IsVerifierDepsVersionValid() const {
     56   return (memcmp(verifier_deps_version_, kVerifierDepsVersion, sizeof(kVerifierDepsVersion)) == 0);
     57 }
     58 
     59 bool VdexFile::VerifierDepsHeader::IsDexSectionVersionValid() const {
     60   return (memcmp(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)) == 0) ||
     61       (memcmp(dex_section_version_, kDexSectionVersionEmpty, sizeof(kDexSectionVersionEmpty)) == 0);
     62 }
     63 
     64 bool VdexFile::VerifierDepsHeader::HasDexSection() const {
     65   return (memcmp(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion)) == 0);
     66 }
     67 
     68 VdexFile::VerifierDepsHeader::VerifierDepsHeader(uint32_t number_of_dex_files,
     69                                                  uint32_t verifier_deps_size,
     70                                                  bool has_dex_section,
     71                                                  uint32_t bootclasspath_checksums_size,
     72                                                  uint32_t class_loader_context_size)
     73     : number_of_dex_files_(number_of_dex_files),
     74       verifier_deps_size_(verifier_deps_size),
     75       bootclasspath_checksums_size_(bootclasspath_checksums_size),
     76       class_loader_context_size_(class_loader_context_size) {
     77   memcpy(magic_, kVdexMagic, sizeof(kVdexMagic));
     78   memcpy(verifier_deps_version_, kVerifierDepsVersion, sizeof(kVerifierDepsVersion));
     79   if (has_dex_section) {
     80     memcpy(dex_section_version_, kDexSectionVersion, sizeof(kDexSectionVersion));
     81   } else {
     82     memcpy(dex_section_version_, kDexSectionVersionEmpty, sizeof(kDexSectionVersionEmpty));
     83   }
     84   DCHECK(IsMagicValid());
     85   DCHECK(IsVerifierDepsVersionValid());
     86   DCHECK(IsDexSectionVersionValid());
     87 }
     88 
     89 VdexFile::DexSectionHeader::DexSectionHeader(uint32_t dex_size,
     90                                              uint32_t dex_shared_data_size,
     91                                              uint32_t quickening_info_size)
     92     : dex_size_(dex_size),
     93       dex_shared_data_size_(dex_shared_data_size),
     94       quickening_info_size_(quickening_info_size) {
     95 }
     96 
     97 std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr,
     98                                                   size_t mmap_size,
     99                                                   bool mmap_reuse,
    100                                                   const std::string& vdex_filename,
    101                                                   bool writable,
    102                                                   bool low_4gb,
    103                                                   bool unquicken,
    104                                                   std::string* error_msg) {
    105   if (!OS::FileExists(vdex_filename.c_str())) {
    106     *error_msg = "File " + vdex_filename + " does not exist.";
    107     return nullptr;
    108   }
    109 
    110   std::unique_ptr<File> vdex_file;
    111   if (writable) {
    112     vdex_file.reset(OS::OpenFileReadWrite(vdex_filename.c_str()));
    113   } else {
    114     vdex_file.reset(OS::OpenFileForReading(vdex_filename.c_str()));
    115   }
    116   if (vdex_file == nullptr) {
    117     *error_msg = "Could not open file " + vdex_filename +
    118                  (writable ? " for read/write" : "for reading");
    119     return nullptr;
    120   }
    121 
    122   int64_t vdex_length = vdex_file->GetLength();
    123   if (vdex_length == -1) {
    124     *error_msg = "Could not read the length of file " + vdex_filename;
    125     return nullptr;
    126   }
    127 
    128   return OpenAtAddress(mmap_addr,
    129                        mmap_size,
    130                        mmap_reuse,
    131                        vdex_file->Fd(),
    132                        vdex_length,
    133                        vdex_filename,
    134                        writable,
    135                        low_4gb,
    136                        unquicken,
    137                        error_msg);
    138 }
    139 
    140 std::unique_ptr<VdexFile> VdexFile::OpenAtAddress(uint8_t* mmap_addr,
    141                                                   size_t mmap_size,
    142                                                   bool mmap_reuse,
    143                                                   int file_fd,
    144                                                   size_t vdex_length,
    145                                                   const std::string& vdex_filename,
    146                                                   bool writable,
    147                                                   bool low_4gb,
    148                                                   bool unquicken,
    149                                                   std::string* error_msg) {
    150   if (mmap_addr != nullptr && mmap_size < vdex_length) {
    151     LOG(WARNING) << "Insufficient pre-allocated space to mmap vdex.";
    152     mmap_addr = nullptr;
    153     mmap_reuse = false;
    154   }
    155   CHECK(!mmap_reuse || mmap_addr != nullptr);
    156   MemMap mmap = MemMap::MapFileAtAddress(
    157       mmap_addr,
    158       vdex_length,
    159       (writable || unquicken) ? PROT_READ | PROT_WRITE : PROT_READ,
    160       unquicken ? MAP_PRIVATE : MAP_SHARED,
    161       file_fd,
    162       /* start= */ 0u,
    163       low_4gb,
    164       vdex_filename.c_str(),
    165       mmap_reuse,
    166       /* reservation= */ nullptr,
    167       error_msg);
    168   if (!mmap.IsValid()) {
    169     *error_msg = "Failed to mmap file " + vdex_filename + " : " + *error_msg;
    170     return nullptr;
    171   }
    172 
    173   std::unique_ptr<VdexFile> vdex(new VdexFile(std::move(mmap)));
    174   if (!vdex->IsValid()) {
    175     *error_msg = "Vdex file is not valid";
    176     return nullptr;
    177   }
    178 
    179   if (unquicken && vdex->HasDexSection()) {
    180     std::vector<std::unique_ptr<const DexFile>> unique_ptr_dex_files;
    181     if (!vdex->OpenAllDexFiles(&unique_ptr_dex_files, error_msg)) {
    182       return nullptr;
    183     }
    184     vdex->Unquicken(MakeNonOwningPointerVector(unique_ptr_dex_files),
    185                     /* decompile_return_instruction= */ false);
    186     // Update the quickening info size to pretend there isn't any.
    187     size_t offset = vdex->GetDexSectionHeaderOffset();
    188     reinterpret_cast<DexSectionHeader*>(vdex->mmap_.Begin() + offset)->quickening_info_size_ = 0;
    189   }
    190 
    191   return vdex;
    192 }
    193 
    194 const uint8_t* VdexFile::GetNextDexFileData(const uint8_t* cursor) const {
    195   DCHECK(cursor == nullptr || (cursor > Begin() && cursor <= End()));
    196   if (cursor == nullptr) {
    197     // Beginning of the iteration, return the first dex file if there is one.
    198     return HasDexSection() ? DexBegin() + sizeof(QuickeningTableOffsetType) : nullptr;
    199   } else {
    200     // Fetch the next dex file. Return null if there is none.
    201     const uint8_t* data = cursor + reinterpret_cast<const DexFile::Header*>(cursor)->file_size_;
    202     // Dex files are required to be 4 byte aligned. the OatWriter makes sure they are, see
    203     // OatWriter::SeekToDexFiles.
    204     data = AlignUp(data, 4);
    205 
    206     return (data == DexEnd()) ? nullptr : data + sizeof(QuickeningTableOffsetType);
    207   }
    208 }
    209 
    210 bool VdexFile::OpenAllDexFiles(std::vector<std::unique_ptr<const DexFile>>* dex_files,
    211                                std::string* error_msg) {
    212   const ArtDexFileLoader dex_file_loader;
    213   size_t i = 0;
    214   for (const uint8_t* dex_file_start = GetNextDexFileData(nullptr);
    215        dex_file_start != nullptr;
    216        dex_file_start = GetNextDexFileData(dex_file_start), ++i) {
    217     size_t size = reinterpret_cast<const DexFile::Header*>(dex_file_start)->file_size_;
    218     // TODO: Supply the location information for a vdex file.
    219     static constexpr char kVdexLocation[] = "";
    220     std::string location = DexFileLoader::GetMultiDexLocation(i, kVdexLocation);
    221     std::unique_ptr<const DexFile> dex(dex_file_loader.OpenWithDataSection(
    222         dex_file_start,
    223         size,
    224         /*data_base=*/ nullptr,
    225         /*data_size=*/ 0u,
    226         location,
    227         GetLocationChecksum(i),
    228         /*oat_dex_file=*/ nullptr,
    229         /*verify=*/ false,
    230         /*verify_checksum=*/ false,
    231         error_msg));
    232     if (dex == nullptr) {
    233       return false;
    234     }
    235     dex_files->push_back(std::move(dex));
    236   }
    237   return true;
    238 }
    239 
    240 void VdexFile::Unquicken(const std::vector<const DexFile*>& target_dex_files,
    241                          bool decompile_return_instruction) const {
    242   const uint8_t* source_dex = GetNextDexFileData(nullptr);
    243   for (const DexFile* target_dex : target_dex_files) {
    244     UnquickenDexFile(*target_dex, source_dex, decompile_return_instruction);
    245     source_dex = GetNextDexFileData(source_dex);
    246   }
    247   DCHECK(source_dex == nullptr);
    248 }
    249 
    250 uint32_t VdexFile::GetQuickeningInfoTableOffset(const uint8_t* source_dex_begin) const {
    251   DCHECK_GE(source_dex_begin, DexBegin());
    252   DCHECK_LT(source_dex_begin, DexEnd());
    253   return reinterpret_cast<const QuickeningTableOffsetType*>(source_dex_begin)[-1];
    254 }
    255 
    256 CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable(
    257     const uint8_t* source_dex_begin,
    258     const ArrayRef<const uint8_t>& quickening_info) const {
    259   // The offset a is in preheader right before the dex file.
    260   const uint32_t offset = GetQuickeningInfoTableOffset(source_dex_begin);
    261   return CompactOffsetTable::Accessor(quickening_info.SubArray(offset).data());
    262 }
    263 
    264 CompactOffsetTable::Accessor VdexFile::GetQuickenInfoOffsetTable(
    265     const DexFile& dex_file,
    266     const ArrayRef<const uint8_t>& quickening_info) const {
    267   return GetQuickenInfoOffsetTable(dex_file.Begin(), quickening_info);
    268 }
    269 
    270 static ArrayRef<const uint8_t> GetQuickeningInfoAt(const ArrayRef<const uint8_t>& quickening_info,
    271                                                    uint32_t quickening_offset) {
    272   // Subtract offset of one since 0 represents unused and cannot be in the table.
    273   ArrayRef<const uint8_t> remaining = quickening_info.SubArray(quickening_offset - 1);
    274   return remaining.SubArray(0u, QuickenInfoTable::SizeInBytes(remaining));
    275 }
    276 
    277 void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
    278                                 const DexFile& source_dex_file,
    279                                 bool decompile_return_instruction) const {
    280   UnquickenDexFile(target_dex_file, source_dex_file.Begin(), decompile_return_instruction);
    281 }
    282 
    283 void VdexFile::UnquickenDexFile(const DexFile& target_dex_file,
    284                                 const uint8_t* source_dex_begin,
    285                                 bool decompile_return_instruction) const {
    286   ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo();
    287   if (quickening_info.empty()) {
    288     // Bail early if there is no quickening info and no need to decompile. This means there is also
    289     // no RETURN_VOID to decompile since the empty table takes a non zero amount of space.
    290     return;
    291   }
    292   // Make sure to not unquicken the same code item multiple times.
    293   std::unordered_set<const dex::CodeItem*> unquickened_code_item;
    294   CompactOffsetTable::Accessor accessor(GetQuickenInfoOffsetTable(source_dex_begin,
    295                                                                   quickening_info));
    296   for (ClassAccessor class_accessor : target_dex_file.GetClasses()) {
    297     for (const ClassAccessor::Method& method : class_accessor.GetMethods()) {
    298       const dex::CodeItem* code_item = method.GetCodeItem();
    299       if (code_item != nullptr && unquickened_code_item.emplace(code_item).second) {
    300         const uint32_t offset = accessor.GetOffset(method.GetIndex());
    301         // Offset being 0 means not quickened.
    302         if (offset != 0u) {
    303           ArrayRef<const uint8_t> quicken_data = GetQuickeningInfoAt(quickening_info, offset);
    304           optimizer::ArtDecompileDEX(
    305               target_dex_file,
    306               *code_item,
    307               quicken_data,
    308               decompile_return_instruction);
    309         }
    310       }
    311     }
    312   }
    313 }
    314 
    315 ArrayRef<const uint8_t> VdexFile::GetQuickenedInfoOf(const DexFile& dex_file,
    316                                                      uint32_t dex_method_idx) const {
    317   ArrayRef<const uint8_t> quickening_info = GetQuickeningInfo();
    318   if (quickening_info.empty()) {
    319     return ArrayRef<const uint8_t>();
    320   }
    321   CHECK_LT(dex_method_idx, dex_file.NumMethodIds());
    322   const uint32_t quickening_offset =
    323       GetQuickenInfoOffsetTable(dex_file, quickening_info).GetOffset(dex_method_idx);
    324   if (quickening_offset == 0u) {
    325     return ArrayRef<const uint8_t>();
    326   }
    327   return GetQuickeningInfoAt(quickening_info, quickening_offset);
    328 }
    329 
    330 static std::string ComputeBootClassPathChecksumString() {
    331   Runtime* const runtime = Runtime::Current();
    332   return gc::space::ImageSpace::GetBootClassPathChecksums(
    333           runtime->GetHeap()->GetBootImageSpaces(),
    334           runtime->GetClassLinker()->GetBootClassPath());
    335 }
    336 
    337 static bool CreateDirectories(const std::string& child_path, /* out */ std::string* error_msg) {
    338   size_t last_slash_pos = child_path.find_last_of('/');
    339   CHECK_NE(last_slash_pos, std::string::npos) << "Invalid path: " << child_path;
    340   std::string parent_path = child_path.substr(0, last_slash_pos);
    341   if (OS::DirectoryExists(parent_path.c_str())) {
    342     return true;
    343   } else if (CreateDirectories(parent_path, error_msg)) {
    344     if (mkdir(parent_path.c_str(), 0700) == 0) {
    345       return true;
    346     }
    347     *error_msg = "Could not create directory " + parent_path;
    348     return false;
    349   } else {
    350     return false;
    351   }
    352 }
    353 
    354 bool VdexFile::WriteToDisk(const std::string& path,
    355                            const std::vector<const DexFile*>& dex_files,
    356                            const verifier::VerifierDeps& verifier_deps,
    357                            const std::string& class_loader_context,
    358                            std::string* error_msg) {
    359   std::vector<uint8_t> verifier_deps_data;
    360   verifier_deps.Encode(dex_files, &verifier_deps_data);
    361 
    362   std::string boot_checksum = ComputeBootClassPathChecksumString();
    363   DCHECK_NE(boot_checksum, "");
    364 
    365   VdexFile::VerifierDepsHeader deps_header(dex_files.size(),
    366                                            verifier_deps_data.size(),
    367                                            /* has_dex_section= */ false,
    368                                            boot_checksum.size(),
    369                                            class_loader_context.size());
    370 
    371   if (!CreateDirectories(path, error_msg)) {
    372     return false;
    373   }
    374 
    375   std::unique_ptr<File> out(OS::CreateEmptyFileWriteOnly(path.c_str()));
    376   if (out == nullptr) {
    377     *error_msg = "Could not open " + path + " for writing";
    378     return false;
    379   }
    380 
    381   if (!out->WriteFully(reinterpret_cast<const char*>(&deps_header), sizeof(deps_header))) {
    382     *error_msg = "Could not write vdex header to " + path;
    383     out->Unlink();
    384     return false;
    385   }
    386 
    387   for (const DexFile* dex_file : dex_files) {
    388     const uint32_t* checksum_ptr = &dex_file->GetHeader().checksum_;
    389     static_assert(sizeof(*checksum_ptr) == sizeof(VdexFile::VdexChecksum));
    390     if (!out->WriteFully(reinterpret_cast<const char*>(checksum_ptr),
    391                          sizeof(VdexFile::VdexChecksum))) {
    392       *error_msg = "Could not write dex checksums to " + path;
    393       out->Unlink();
    394     return false;
    395     }
    396   }
    397 
    398   if (!out->WriteFully(reinterpret_cast<const char*>(verifier_deps_data.data()),
    399                        verifier_deps_data.size())) {
    400     *error_msg = "Could not write verifier deps to " + path;
    401     out->Unlink();
    402     return false;
    403   }
    404 
    405   if (!out->WriteFully(boot_checksum.c_str(), boot_checksum.size())) {
    406     *error_msg = "Could not write boot classpath checksum to " + path;
    407     out->Unlink();
    408     return false;
    409   }
    410 
    411   if (!out->WriteFully(class_loader_context.c_str(), class_loader_context.size())) {
    412     *error_msg = "Could not write class loader context to " + path;
    413     out->Unlink();
    414     return false;
    415   }
    416 
    417   if (out->FlushClose() != 0) {
    418     *error_msg = "Could not flush and close " + path;
    419     out->Unlink();
    420     return false;
    421   }
    422 
    423   return true;
    424 }
    425 
    426 bool VdexFile::MatchesDexFileChecksums(const std::vector<const DexFile::Header*>& dex_headers)
    427     const {
    428   const VerifierDepsHeader& header = GetVerifierDepsHeader();
    429   if (dex_headers.size() != header.GetNumberOfDexFiles()) {
    430     LOG(WARNING) << "Mismatch of number of dex files in vdex (expected="
    431         << header.GetNumberOfDexFiles() << ", actual=" << dex_headers.size() << ")";
    432     return false;
    433   }
    434   const VdexChecksum* checksums = header.GetDexChecksumsArray();
    435   for (size_t i = 0; i < dex_headers.size(); ++i) {
    436     if (checksums[i] != dex_headers[i]->checksum_) {
    437       LOG(WARNING) << "Mismatch of dex file checksum in vdex (index=" << i << ")";
    438       return false;
    439     }
    440   }
    441   return true;
    442 }
    443 
    444 bool VdexFile::MatchesBootClassPathChecksums() const {
    445   ArrayRef<const uint8_t> data = GetBootClassPathChecksumData();
    446   std::string vdex(reinterpret_cast<const char*>(data.data()), data.size());
    447   std::string runtime = ComputeBootClassPathChecksumString();
    448   if (vdex == runtime) {
    449     return true;
    450   } else {
    451     LOG(WARNING) << "Mismatch of boot class path checksum in vdex (expected="
    452         << vdex << ", actual=" << runtime << ")";
    453     return false;
    454   }
    455 }
    456 
    457 bool VdexFile::MatchesClassLoaderContext(const ClassLoaderContext& context) const {
    458   ArrayRef<const uint8_t> data = GetClassLoaderContextData();
    459   std::string spec(reinterpret_cast<const char*>(data.data()), data.size());
    460   ClassLoaderContext::VerificationResult result = context.VerifyClassLoaderContextMatch(spec);
    461   if (result != ClassLoaderContext::VerificationResult::kMismatch) {
    462     return true;
    463   } else {
    464     LOG(WARNING) << "Mismatch of class loader context in vdex (expected="
    465         << spec << ", actual=" << context.EncodeContextForOatFile("") << ")";
    466     return false;
    467   }
    468 }
    469 
    470 }  // namespace art
    471