blob: c819acd5ac4bcb03d488ba7ff8578e585d870b07 [file] [log] [blame]
Alex Lighta01de592016-11-15 10:43:06 -08001/* Copyright (C) 2016 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#ifndef ART_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_
33#define ART_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_
34
35#include <string>
36
37#include <jni.h>
38
39#include "art_jvmti.h"
40#include "art_method.h"
41#include "class_linker.h"
42#include "dex_file.h"
43#include "gc_root-inl.h"
44#include "globals.h"
45#include "jni_env_ext-inl.h"
46#include "jvmti.h"
47#include "linear_alloc.h"
48#include "mem_map.h"
49#include "mirror/array-inl.h"
50#include "mirror/array.h"
51#include "mirror/class-inl.h"
52#include "mirror/class.h"
53#include "mirror/class_loader-inl.h"
54#include "mirror/string-inl.h"
55#include "oat_file.h"
56#include "obj_ptr.h"
57#include "scoped_thread_state_change-inl.h"
58#include "stack.h"
59#include "thread_list.h"
60#include "transform.h"
61#include "utf.h"
62#include "utils/dex_cache_arrays_layout-inl.h"
63
64namespace openjdkjvmti {
65
66// Class that can redefine a single class's methods.
67class Redefiner {
68 public:
69 // Redefine the given class with the given dex data. Note this function does not take ownership of
70 // the dex_data pointer. It is not used after this call however and may be freed if desired.
Alex Light0b772572016-12-02 17:27:31 -080071 // The caller is responsible for freeing it. The runtime makes its own copy of the data.
Alex Lighta01de592016-11-15 10:43:06 -080072 static jvmtiError RedefineClass(ArtJvmTiEnv* env,
73 art::Runtime* runtime,
74 art::Thread* self,
75 jclass klass,
76 const std::string& original_dex_location,
77 jint data_len,
78 unsigned char* dex_data,
79 std::string* error_msg);
80
81 private:
82 jvmtiError result_;
83 art::Runtime* runtime_;
84 art::Thread* self_;
85 // Kept as a jclass since we have weird run-state changes that make keeping it around as a
86 // mirror::Class difficult and confusing.
87 jclass klass_;
88 std::unique_ptr<const art::DexFile> dex_file_;
89 std::string* error_msg_;
90 char* class_sig_;
91
92 // TODO Maybe change jclass to a mirror::Class
93 Redefiner(art::Runtime* runtime,
94 art::Thread* self,
95 jclass klass,
96 char* class_sig,
97 std::unique_ptr<const art::DexFile>& redefined_dex_file,
98 std::string* error_msg)
99 : result_(ERR(INTERNAL)),
100 runtime_(runtime),
101 self_(self),
102 klass_(klass),
103 dex_file_(std::move(redefined_dex_file)),
104 error_msg_(error_msg),
105 class_sig_(class_sig) { }
106
107 static std::unique_ptr<art::MemMap> MoveDataToMemMap(const std::string& original_location,
108 jint data_len,
109 unsigned char* dex_data,
110 std::string* error_msg);
111
112 // TODO Put on all the lock qualifiers.
113 jvmtiError Run() REQUIRES_SHARED(art::Locks::mutator_lock_);
114
115 bool FinishRemainingAllocations(
116 /*out*/art::MutableHandle<art::mirror::ClassLoader>* source_class_loader,
117 /*out*/art::MutableHandle<art::mirror::Object>* source_dex_file_obj,
118 /*out*/art::MutableHandle<art::mirror::LongArray>* new_dex_file_cookie,
119 /*out*/art::MutableHandle<art::mirror::DexCache>* new_dex_cache)
120 REQUIRES_SHARED(art::Locks::mutator_lock_);
121
122 // Preallocates all needed allocations in klass so that we can pause execution safely.
123 // TODO We should be able to free the arrays if they end up not being used. Investigate doing this
124 // in the future. For now we will just take the memory hit.
125 bool EnsureClassAllocationsFinished() REQUIRES_SHARED(art::Locks::mutator_lock_);
126
127 art::mirror::ClassLoader* GetClassLoader() REQUIRES_SHARED(art::Locks::mutator_lock_);
128
129 // This finds the java.lang.DexFile we will add the native DexFile to as part of the classpath.
130 // TODO Make sure the DexFile object returned is the one that the klass_ actually comes from.
131 art::mirror::Object* FindSourceDexFileObject(art::Handle<art::mirror::ClassLoader> loader)
132 REQUIRES_SHARED(art::Locks::mutator_lock_);
133
134 art::mirror::Class* GetMirrorClass() REQUIRES_SHARED(art::Locks::mutator_lock_);
135
136 // Allocates and fills the new DexFileCookie
137 art::mirror::LongArray* AllocateDexFileCookie(art::Handle<art::mirror::Object> java_dex_file_obj)
138 REQUIRES_SHARED(art::Locks::mutator_lock_);
139
140 art::mirror::DexCache* CreateNewDexCache(art::Handle<art::mirror::ClassLoader> loader)
141 REQUIRES_SHARED(art::Locks::mutator_lock_);
142
143 void RecordFailure(jvmtiError result, const std::string& error_msg);
144
145 // TODO Actually write this.
146 // This will check that no constraints are violated (more than 1 class in dex file, any changes in
147 // number/declaration of methods & fields, changes in access flags, etc.)
148 bool EnsureRedefinitionIsValid() {
Alex Light0b772572016-12-02 17:27:31 -0800149 LOG(WARNING) << "Redefinition is not checked for validity currently";
Alex Lighta01de592016-11-15 10:43:06 -0800150 return true;
151 }
152
153 bool UpdateJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file,
154 art::ObjPtr<art::mirror::LongArray> new_cookie,
155 /*out*/art::ObjPtr<art::mirror::LongArray>* original_cookie)
156 REQUIRES(art::Locks::mutator_lock_);
157
158 void RestoreJavaDexFile(art::ObjPtr<art::mirror::Object> java_dex_file,
159 art::ObjPtr<art::mirror::LongArray> original_cookie)
160 REQUIRES(art::Locks::mutator_lock_);
161
162 bool UpdateClass(art::ObjPtr<art::mirror::Class> mclass,
163 art::ObjPtr<art::mirror::DexCache> new_dex_cache)
164 REQUIRES(art::Locks::mutator_lock_);
165};
166
167} // namespace openjdkjvmti
168
169#endif // ART_RUNTIME_OPENJDKJVMTI_TI_REDEFINE_H_