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
40namespace openjdkjvmti {
41
42static 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// TODO This is more complicated then it seems like it should be.
49// The fact we don't keep around the data of where in the flat binary log of dex-quickening changes
50// each dex file starts means we need to search for it. Since JVMTI is the exception though we are
51// not going to put in the effort to optimize for it.
52static void DoDexUnquicken(const art::DexFile& new_dex_file,
53                           const art::DexFile& original_dex_file)
54    REQUIRES_SHARED(art::Locks::mutator_lock_) {
55  const art::OatDexFile* oat_dex = original_dex_file.GetOatDexFile();
56  if (oat_dex == nullptr) {
57    return;
58  }
59  const art::OatFile* oat_file = oat_dex->GetOatFile();
60  if (oat_file == nullptr) {
61    return;
62  }
63  const art::VdexFile* vdex = oat_file->GetVdexFile();
64  if (vdex == nullptr || vdex->GetQuickeningInfo().size() == 0) {
65    return;
66  }
67  const art::ArrayRef<const uint8_t> quickening_info(vdex->GetQuickeningInfo());
68  const uint8_t* quickening_info_ptr = quickening_info.data();
69  for (const art::OatDexFile* cur_oat_dex : oat_file->GetOatDexFiles()) {
70    std::string error;
71    std::unique_ptr<const art::DexFile> cur_dex_file(cur_oat_dex->OpenDexFile(&error));
72    DCHECK(cur_dex_file.get() != nullptr);
73    // Is this the dex file we are looking for?
74    if (UNLIKELY(cur_dex_file->Begin() == original_dex_file.Begin())) {
75      // Simple sanity check.
76      CHECK_EQ(new_dex_file.NumClassDefs(), original_dex_file.NumClassDefs());
77      for (uint32_t i = 0; i < new_dex_file.NumClassDefs(); ++i) {
78        const art::DexFile::ClassDef& class_def = new_dex_file.GetClassDef(i);
79        const uint8_t* class_data = new_dex_file.GetClassData(class_def);
80        if (class_data == nullptr) {
81          continue;
82        }
83        for (art::ClassDataItemIterator it(new_dex_file, class_data); it.HasNext(); it.Next()) {
84          if (it.IsAtMethod() && it.GetMethodCodeItem() != nullptr) {
85            uint32_t quickening_size = *reinterpret_cast<const uint32_t*>(quickening_info_ptr);
86            quickening_info_ptr += sizeof(uint32_t);
87            art::optimizer::ArtDecompileDEX(
88                *it.GetMethodCodeItem(),
89                art::ArrayRef<const uint8_t>(quickening_info_ptr, quickening_size),
90                /*decompile_return_instruction*/true);
91            quickening_info_ptr += quickening_size;
92          }
93        }
94      }
95      // We don't need to bother looking through the rest of the dex-files.
96      break;
97    } else {
98      // Not the dex file we want. Skip over all the quickening info for all its classes.
99      for (uint32_t i = 0; i < cur_dex_file->NumClassDefs(); ++i) {
100        const art::DexFile::ClassDef& class_def = cur_dex_file->GetClassDef(i);
101        const uint8_t* class_data = cur_dex_file->GetClassData(class_def);
102        if (class_data == nullptr) {
103          continue;
104        }
105        for (art::ClassDataItemIterator it(*cur_dex_file, class_data); it.HasNext(); it.Next()) {
106          if (it.IsAtMethod() && it.GetMethodCodeItem() != nullptr) {
107            uint32_t quickening_size = *reinterpret_cast<const uint32_t*>(quickening_info_ptr);
108            quickening_info_ptr += sizeof(uint32_t);
109            quickening_info_ptr += quickening_size;
110          }
111        }
112      }
113    }
114  }
115}
116
117std::unique_ptr<FixedUpDexFile> FixedUpDexFile::Create(const art::DexFile& original) {
118  // Copy the data into mutable memory.
119  std::vector<unsigned char> data;
120  data.resize(original.Size());
121  memcpy(data.data(), original.Begin(), original.Size());
122  std::string error;
123  std::unique_ptr<const art::DexFile> new_dex_file(art::DexFile::Open(
124      data.data(),
125      data.size(),
126      /*location*/"Unquickening_dexfile.dex",
127      /*location_checksum*/0,
128      /*oat_dex_file*/nullptr,
129      /*verify*/false,
130      /*verify_checksum*/false,
131      &error));
132  if (new_dex_file.get() == nullptr) {
133    LOG(ERROR) << "Unable to open dex file from memory for unquickening! error: " << error;
134    return nullptr;
135  }
136
137  DoDexUnquicken(*new_dex_file, original);
138  RecomputeDexChecksum(const_cast<art::DexFile*>(new_dex_file.get()));
139  std::unique_ptr<FixedUpDexFile> ret(new FixedUpDexFile(std::move(new_dex_file), std::move(data)));
140  return ret;
141}
142
143}  // namespace openjdkjvmti
144