blob: 05717f41e75cd693db73fe3b66de3f3a9842f2f7 [file] [log] [blame]
Ian Rogers68d8b422014-07-17 11:09:10 -07001/*
2 * Copyright (C) 2011 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#ifndef ART_RUNTIME_JAVA_VM_EXT_H_
18#define ART_RUNTIME_JAVA_VM_EXT_H_
19
20#include "jni.h"
21
22#include "base/macros.h"
23#include "base/mutex.h"
24#include "indirect_reference_table.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070025#include "obj_ptr.h"
Ian Rogers68d8b422014-07-17 11:09:10 -070026#include "reference_table.h"
27
28namespace art {
29
30namespace mirror {
Ian Rogers68d8b422014-07-17 11:09:10 -070031 class Array;
32} // namespace mirror
33
Mathieu Chartiere401d142015-04-22 13:56:20 -070034class ArtMethod;
Ian Rogers68d8b422014-07-17 11:09:10 -070035class Libraries;
36class ParsedOptions;
37class Runtime;
Igor Murashkinaaebaa02015-01-26 10:55:53 -080038struct RuntimeArgumentMap;
Ian Rogers68d8b422014-07-17 11:09:10 -070039
Alex Light185d1342016-08-11 10:48:03 -070040class JavaVMExt;
41// Hook definition for runtime plugins.
42using GetEnvHook = jint (*)(JavaVMExt* vm, /*out*/void** new_env, jint version);
43
Ian Rogers68d8b422014-07-17 11:09:10 -070044class JavaVMExt : public JavaVM {
45 public:
Igor Murashkinaaebaa02015-01-26 10:55:53 -080046 JavaVMExt(Runtime* runtime, const RuntimeArgumentMap& runtime_options);
Ian Rogers68d8b422014-07-17 11:09:10 -070047 ~JavaVMExt();
48
49 bool ForceCopy() const {
50 return force_copy_;
51 }
52
53 bool IsCheckJniEnabled() const {
54 return check_jni_;
55 }
56
57 bool IsTracingEnabled() const {
58 return tracing_enabled_;
59 }
60
61 Runtime* GetRuntime() const {
62 return runtime_;
63 }
64
65 void SetCheckJniAbortHook(void (*hook)(void*, const std::string&), void* data) {
66 check_jni_abort_hook_ = hook;
67 check_jni_abort_hook_data_ = data;
68 }
69
70 // Aborts execution unless there is an abort handler installed in which case it will return. Its
71 // therefore important that callers return after aborting as otherwise code following the abort
72 // will be executed in the abort handler case.
73 void JniAbort(const char* jni_function_name, const char* msg);
74
75 void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap);
76
77 void JniAbortF(const char* jni_function_name, const char* fmt, ...)
78 __attribute__((__format__(__printf__, 3, 4)));
79
80 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
81 // when a native method that matches the -Xjnitrace argument calls a JNI function
82 // such as NewByteArray.
83 // If -verbose:third-party-jni is on, we want to log any JNI function calls
84 // made by a third-party native method.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070085 bool ShouldTrace(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070086
87 /**
88 * Loads the given shared library. 'path' is an absolute pathname.
89 *
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080090 * Returns 'true' on success. On failure, sets 'error_msg' to a
Ian Rogers68d8b422014-07-17 11:09:10 -070091 * human-readable description of the error.
92 */
Dimitry Ivanov942dc2982016-02-24 13:33:33 -080093 bool LoadNativeLibrary(JNIEnv* env,
94 const std::string& path,
95 jobject class_loader,
96 jstring library_path,
Dimitry Ivanovd5bbadf2015-12-15 14:08:18 -080097 std::string* error_msg);
Ian Rogers68d8b422014-07-17 11:09:10 -070098
Mathieu Chartier598302a2015-09-23 14:52:39 -070099 // Unload native libraries with cleared class loaders.
100 void UnloadNativeLibraries()
101 REQUIRES(!Locks::jni_libraries_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700102 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier598302a2015-09-23 14:52:39 -0700103
Ian Rogers68d8b422014-07-17 11:09:10 -0700104 /**
105 * Returns a pointer to the code for the native method 'm', found
106 * using dlsym(3) on every native library that's been loaded so far.
107 */
Mathieu Chartiere401d142015-04-22 13:56:20 -0700108 void* FindCodeForNativeMethod(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700110
111 void DumpForSigQuit(std::ostream& os)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700112 REQUIRES(!Locks::jni_libraries_lock_,
113 !Locks::jni_globals_lock_,
114 !Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700115
116 void DumpReferenceTables(std::ostream& os)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700117 REQUIRES_SHARED(Locks::mutator_lock_)
118 REQUIRES(!Locks::jni_globals_lock_, !Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700119
120 bool SetCheckJniEnabled(bool enabled);
121
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700122 void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700123 REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700124
Andreas Gampe05a364c2016-10-14 13:27:12 -0700125 void DisallowNewWeakGlobals()
126 REQUIRES_SHARED(Locks::mutator_lock_)
127 REQUIRES(!Locks::jni_weak_globals_lock_);
128 void AllowNewWeakGlobals()
129 REQUIRES_SHARED(Locks::mutator_lock_)
130 REQUIRES(!Locks::jni_weak_globals_lock_);
131 void BroadcastForNewWeakGlobals()
132 REQUIRES_SHARED(Locks::mutator_lock_)
133 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700134
Mathieu Chartier0795f232016-09-27 18:43:30 -0700135 jobject AddGlobalRef(Thread* self, ObjPtr<mirror::Object> obj)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700136 REQUIRES_SHARED(Locks::mutator_lock_)
137 REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700138
Mathieu Chartier0795f232016-09-27 18:43:30 -0700139 jweak AddWeakGlobalRef(Thread* self, ObjPtr<mirror::Object> obj)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700140 REQUIRES_SHARED(Locks::mutator_lock_)
141 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700142
Andreas Gampe05a364c2016-10-14 13:27:12 -0700143 void DeleteGlobalRef(Thread* self, jobject obj) REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700144
Andreas Gampe05a364c2016-10-14 13:27:12 -0700145 void DeleteWeakGlobalRef(Thread* self, jweak obj) REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700146
Mathieu Chartier97509952015-07-13 14:35:43 -0700147 void SweepJniWeakGlobals(IsMarkedVisitor* visitor)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700148 REQUIRES_SHARED(Locks::mutator_lock_)
149 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700150
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700151 ObjPtr<mirror::Object> DecodeGlobal(IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700152 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700153
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700154 void UpdateGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700155 REQUIRES_SHARED(Locks::mutator_lock_)
156 REQUIRES(!Locks::jni_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700157
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700158 ObjPtr<mirror::Object> DecodeWeakGlobal(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700159 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700160 REQUIRES(!Locks::jni_weak_globals_lock_);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700161
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700162 ObjPtr<mirror::Object> DecodeWeakGlobalLocked(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700163 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700164 REQUIRES(Locks::jni_weak_globals_lock_);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700165
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700166 // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be
167 // null.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700168 ObjPtr<mirror::Object> DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700170 REQUIRES(!Locks::jni_weak_globals_lock_);
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700171
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800172 // Checks if the weak global ref has been cleared by the GC without decode (read barrier.)
173 bool IsWeakGlobalCleared(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700174 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700175 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700176
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700177 void UpdateWeakGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700178 REQUIRES_SHARED(Locks::mutator_lock_)
179 REQUIRES(!Locks::jni_weak_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700180
Ian Rogers68d8b422014-07-17 11:09:10 -0700181 const JNIInvokeInterface* GetUncheckedFunctions() const {
182 return unchecked_functions_;
183 }
184
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700185 void TrimGlobals() REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700186 REQUIRES(!Locks::jni_globals_lock_);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800187
Alex Light185d1342016-08-11 10:48:03 -0700188 jint HandleGetEnv(/*out*/void** env, jint version);
189
190 void AddEnvironmentHook(GetEnvHook hook);
191
192 static bool IsBadJniVersion(int version);
193
Ian Rogers68d8b422014-07-17 11:09:10 -0700194 private:
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700195 // Return true if self can currently access weak globals.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700196 bool MayAccessWeakGlobalsUnlocked(Thread* self) const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700197 bool MayAccessWeakGlobals(Thread* self) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700198 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700199 REQUIRES(Locks::jni_weak_globals_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700200
Ian Rogers68d8b422014-07-17 11:09:10 -0700201 Runtime* const runtime_;
202
203 // Used for testing. By default, we'll LOG(FATAL) the reason.
204 void (*check_jni_abort_hook_)(void* data, const std::string& reason);
205 void* check_jni_abort_hook_data_;
206
207 // Extra checking.
208 bool check_jni_;
209 bool force_copy_;
210 const bool tracing_enabled_;
211
212 // Extra diagnostics.
213 const std::string trace_;
214
Ian Rogers68d8b422014-07-17 11:09:10 -0700215 // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject.
216 IndirectReferenceTable globals_;
217
Mathieu Chartier598302a2015-09-23 14:52:39 -0700218 // No lock annotation since UnloadNativeLibraries is called on libraries_ but locks the
219 // jni_libraries_lock_ internally.
220 std::unique_ptr<Libraries> libraries_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700221
222 // Used by -Xcheck:jni.
223 const JNIInvokeInterface* const unchecked_functions_;
224
Ian Rogers68d8b422014-07-17 11:09:10 -0700225 // Since weak_globals_ contain weak roots, be careful not to
226 // directly access the object references in it. Use Get() with the
227 // read barrier enabled.
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700228 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
229 IndirectReferenceTable weak_globals_;
230 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
231 Atomic<bool> allow_accessing_weak_globals_;
Andreas Gampe05a364c2016-10-14 13:27:12 -0700232 ConditionVariable weak_globals_add_condition_ GUARDED_BY(Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700233
Alex Light185d1342016-08-11 10:48:03 -0700234 // TODO Maybe move this to Runtime.
235 std::vector<GetEnvHook> env_hooks_;
236
Ian Rogers68d8b422014-07-17 11:09:10 -0700237 DISALLOW_COPY_AND_ASSIGN(JavaVMExt);
238};
239
240} // namespace art
241
242#endif // ART_RUNTIME_JAVA_VM_EXT_H_