Home | History | Annotate | Download | only in openjdkjvmti
      1 /* Copyright (C) 2017 The Android Open Source Project
      2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
      3  *
      4  * This file implements interfaces from the file jvmti.h. This implementation
      5  * is licensed under the same terms as the file jvmti.h.  The
      6  * copyright and license information for the file jvmti.h follows.
      7  *
      8  * Copyright (c) 2003, 2011, Oracle and/or its affiliates. All rights reserved.
      9  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     10  *
     11  * This code is free software; you can redistribute it and/or modify it
     12  * under the terms of the GNU General Public License version 2 only, as
     13  * published by the Free Software Foundation.  Oracle designates this
     14  * particular file as subject to the "Classpath" exception as provided
     15  * by Oracle in the LICENSE file that accompanied this code.
     16  *
     17  * This code is distributed in the hope that it will be useful, but WITHOUT
     18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
     19  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
     20  * version 2 for more details (a copy is included in the LICENSE file that
     21  * accompanied this code).
     22  *
     23  * You should have received a copy of the GNU General Public License version
     24  * 2 along with this work; if not, write to the Free Software Foundation,
     25  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
     26  *
     27  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
     28  * or visit www.oracle.com if you need additional information or have any
     29  * questions.
     30  */
     31 
     32 #include "fixed_up_dex_file.h"
     33 #include "dex_file-inl.h"
     34 
     35 // Runtime includes.
     36 #include "dex_to_dex_decompiler.h"
     37 #include "oat_file.h"
     38 #include "vdex_file.h"
     39 
     40 namespace openjdkjvmti {
     41 
     42 static void RecomputeDexChecksum(art::DexFile* dex_file)
     43     REQUIRES_SHARED(art::Locks::mutator_lock_) {
     44   reinterpret_cast<art::DexFile::Header*>(const_cast<uint8_t*>(dex_file->Begin()))->checksum_ =
     45       dex_file->CalculateChecksum();
     46 }
     47 
     48 static void DoDexUnquicken(const art::DexFile& new_dex_file, const art::DexFile& original_dex_file)
     49     REQUIRES_SHARED(art::Locks::mutator_lock_) {
     50   const art::OatDexFile* oat_dex = original_dex_file.GetOatDexFile();
     51   if (oat_dex == nullptr) {
     52     return;
     53   }
     54   const art::OatFile* oat_file = oat_dex->GetOatFile();
     55   if (oat_file == nullptr) {
     56     return;
     57   }
     58   const art::VdexFile* vdex = oat_file->GetVdexFile();
     59   if (vdex == nullptr) {
     60     return;
     61   }
     62   vdex->FullyUnquickenDexFile(new_dex_file, original_dex_file);
     63 }
     64 
     65 std::unique_ptr<FixedUpDexFile> FixedUpDexFile::Create(const art::DexFile& original) {
     66   // Copy the data into mutable memory.
     67   std::vector<unsigned char> data;
     68   data.resize(original.Size());
     69   memcpy(data.data(), original.Begin(), original.Size());
     70   std::string error;
     71   std::unique_ptr<const art::DexFile> new_dex_file(art::DexFile::Open(
     72       data.data(),
     73       data.size(),
     74       /*location*/"Unquickening_dexfile.dex",
     75       /*location_checksum*/0,
     76       /*oat_dex_file*/nullptr,
     77       /*verify*/false,
     78       /*verify_checksum*/false,
     79       &error));
     80   if (new_dex_file.get() == nullptr) {
     81     LOG(ERROR) << "Unable to open dex file from memory for unquickening! error: " << error;
     82     return nullptr;
     83   }
     84 
     85   DoDexUnquicken(*new_dex_file, original);
     86   RecomputeDexChecksum(const_cast<art::DexFile*>(new_dex_file.get()));
     87   std::unique_ptr<FixedUpDexFile> ret(new FixedUpDexFile(std::move(new_dex_file), std::move(data)));
     88   return ret;
     89 }
     90 
     91 }  // namespace openjdkjvmti
     92