blob: 7374920f2b3b2038638f9eca5c80b0d9df78f0af [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:
Richard Uhlerda0a69e2016-10-11 15:06:38 +010046 // Creates a new JavaVMExt object.
47 // Returns nullptr on error, in which case error_msg is set to a message
48 // describing the error.
49 static std::unique_ptr<JavaVMExt> Create(Runtime* runtime,
50 const RuntimeArgumentMap& runtime_options,
51 std::string* error_msg);
52
53
Ian Rogers68d8b422014-07-17 11:09:10 -070054 ~JavaVMExt();
55
56 bool ForceCopy() const {
57 return force_copy_;
58 }
59
60 bool IsCheckJniEnabled() const {
61 return check_jni_;
62 }
63
64 bool IsTracingEnabled() const {
65 return tracing_enabled_;
66 }
67
68 Runtime* GetRuntime() const {
69 return runtime_;
70 }
71
72 void SetCheckJniAbortHook(void (*hook)(void*, const std::string&), void* data) {
73 check_jni_abort_hook_ = hook;
74 check_jni_abort_hook_data_ = data;
75 }
76
77 // Aborts execution unless there is an abort handler installed in which case it will return. Its
78 // therefore important that callers return after aborting as otherwise code following the abort
79 // will be executed in the abort handler case.
80 void JniAbort(const char* jni_function_name, const char* msg);
81
82 void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap);
83
84 void JniAbortF(const char* jni_function_name, const char* fmt, ...)
85 __attribute__((__format__(__printf__, 3, 4)));
86
87 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
88 // when a native method that matches the -Xjnitrace argument calls a JNI function
89 // such as NewByteArray.
90 // If -verbose:third-party-jni is on, we want to log any JNI function calls
91 // made by a third-party native method.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 bool ShouldTrace(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070093
94 /**
95 * Loads the given shared library. 'path' is an absolute pathname.
96 *
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080097 * Returns 'true' on success. On failure, sets 'error_msg' to a
Ian Rogers68d8b422014-07-17 11:09:10 -070098 * human-readable description of the error.
99 */
Dimitry Ivanov942dc2982016-02-24 13:33:33 -0800100 bool LoadNativeLibrary(JNIEnv* env,
101 const std::string& path,
102 jobject class_loader,
103 jstring library_path,
Dimitry Ivanovd5bbadf2015-12-15 14:08:18 -0800104 std::string* error_msg);
Ian Rogers68d8b422014-07-17 11:09:10 -0700105
Mathieu Chartier598302a2015-09-23 14:52:39 -0700106 // Unload native libraries with cleared class loaders.
107 void UnloadNativeLibraries()
108 REQUIRES(!Locks::jni_libraries_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700109 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier598302a2015-09-23 14:52:39 -0700110
Ian Rogers68d8b422014-07-17 11:09:10 -0700111 /**
112 * Returns a pointer to the code for the native method 'm', found
113 * using dlsym(3) on every native library that's been loaded so far.
114 */
Mathieu Chartiere401d142015-04-22 13:56:20 -0700115 void* FindCodeForNativeMethod(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700116 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700117
118 void DumpForSigQuit(std::ostream& os)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700119 REQUIRES(!Locks::jni_libraries_lock_,
120 !Locks::jni_globals_lock_,
121 !Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700122
123 void DumpReferenceTables(std::ostream& os)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700124 REQUIRES_SHARED(Locks::mutator_lock_)
125 REQUIRES(!Locks::jni_globals_lock_, !Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700126
127 bool SetCheckJniEnabled(bool enabled);
128
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700130 REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700131
Andreas Gampe05a364c2016-10-14 13:27:12 -0700132 void DisallowNewWeakGlobals()
133 REQUIRES_SHARED(Locks::mutator_lock_)
134 REQUIRES(!Locks::jni_weak_globals_lock_);
135 void AllowNewWeakGlobals()
136 REQUIRES_SHARED(Locks::mutator_lock_)
137 REQUIRES(!Locks::jni_weak_globals_lock_);
138 void BroadcastForNewWeakGlobals()
Andreas Gampe05a364c2016-10-14 13:27:12 -0700139 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700140
Mathieu Chartier0795f232016-09-27 18:43:30 -0700141 jobject AddGlobalRef(Thread* self, ObjPtr<mirror::Object> obj)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700142 REQUIRES_SHARED(Locks::mutator_lock_)
143 REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700144
Mathieu Chartier0795f232016-09-27 18:43:30 -0700145 jweak AddWeakGlobalRef(Thread* self, ObjPtr<mirror::Object> obj)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700146 REQUIRES_SHARED(Locks::mutator_lock_)
147 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700148
Andreas Gampe05a364c2016-10-14 13:27:12 -0700149 void DeleteGlobalRef(Thread* self, jobject obj) REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700150
Andreas Gampe05a364c2016-10-14 13:27:12 -0700151 void DeleteWeakGlobalRef(Thread* self, jweak obj) REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700152
Mathieu Chartier97509952015-07-13 14:35:43 -0700153 void SweepJniWeakGlobals(IsMarkedVisitor* visitor)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700154 REQUIRES_SHARED(Locks::mutator_lock_)
155 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700156
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700157 ObjPtr<mirror::Object> DecodeGlobal(IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700158 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700159
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700160 void UpdateGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700161 REQUIRES_SHARED(Locks::mutator_lock_)
162 REQUIRES(!Locks::jni_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700163
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700164 ObjPtr<mirror::Object> DecodeWeakGlobal(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700165 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700166 REQUIRES(!Locks::jni_weak_globals_lock_);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700167
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700168 ObjPtr<mirror::Object> DecodeWeakGlobalLocked(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_);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700171
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700172 // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be
173 // null.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700174 ObjPtr<mirror::Object> DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700175 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700176 REQUIRES(!Locks::jni_weak_globals_lock_);
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700177
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800178 // Checks if the weak global ref has been cleared by the GC without decode (read barrier.)
179 bool IsWeakGlobalCleared(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700180 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700181 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700182
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700183 void UpdateWeakGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700184 REQUIRES_SHARED(Locks::mutator_lock_)
185 REQUIRES(!Locks::jni_weak_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700186
Ian Rogers68d8b422014-07-17 11:09:10 -0700187 const JNIInvokeInterface* GetUncheckedFunctions() const {
188 return unchecked_functions_;
189 }
190
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700191 void TrimGlobals() REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700192 REQUIRES(!Locks::jni_globals_lock_);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800193
Alex Light185d1342016-08-11 10:48:03 -0700194 jint HandleGetEnv(/*out*/void** env, jint version);
195
196 void AddEnvironmentHook(GetEnvHook hook);
197
198 static bool IsBadJniVersion(int version);
199
Ian Rogers68d8b422014-07-17 11:09:10 -0700200 private:
Richard Uhlerda0a69e2016-10-11 15:06:38 +0100201 // The constructor should not be called directly. It may leave the object in
202 // an erroneous state, and the result needs to be checked.
203 JavaVMExt(Runtime* runtime, const RuntimeArgumentMap& runtime_options, std::string* error_msg);
204
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700205 // Return true if self can currently access weak globals.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700206 bool MayAccessWeakGlobalsUnlocked(Thread* self) const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700207 bool MayAccessWeakGlobals(Thread* self) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700208 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700209 REQUIRES(Locks::jni_weak_globals_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700210
Ian Rogers68d8b422014-07-17 11:09:10 -0700211 Runtime* const runtime_;
212
213 // Used for testing. By default, we'll LOG(FATAL) the reason.
214 void (*check_jni_abort_hook_)(void* data, const std::string& reason);
215 void* check_jni_abort_hook_data_;
216
217 // Extra checking.
218 bool check_jni_;
219 bool force_copy_;
220 const bool tracing_enabled_;
221
222 // Extra diagnostics.
223 const std::string trace_;
224
Ian Rogers68d8b422014-07-17 11:09:10 -0700225 // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject.
226 IndirectReferenceTable globals_;
227
Mathieu Chartier598302a2015-09-23 14:52:39 -0700228 // No lock annotation since UnloadNativeLibraries is called on libraries_ but locks the
229 // jni_libraries_lock_ internally.
230 std::unique_ptr<Libraries> libraries_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700231
232 // Used by -Xcheck:jni.
233 const JNIInvokeInterface* const unchecked_functions_;
234
Ian Rogers68d8b422014-07-17 11:09:10 -0700235 // Since weak_globals_ contain weak roots, be careful not to
236 // directly access the object references in it. Use Get() with the
237 // read barrier enabled.
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700238 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
239 IndirectReferenceTable weak_globals_;
240 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
241 Atomic<bool> allow_accessing_weak_globals_;
Andreas Gampe05a364c2016-10-14 13:27:12 -0700242 ConditionVariable weak_globals_add_condition_ GUARDED_BY(Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700243
Alex Light185d1342016-08-11 10:48:03 -0700244 // TODO Maybe move this to Runtime.
245 std::vector<GetEnvHook> env_hooks_;
246
Ian Rogers68d8b422014-07-17 11:09:10 -0700247 DISALLOW_COPY_AND_ASSIGN(JavaVMExt);
248};
249
250} // namespace art
251
252#endif // ART_RUNTIME_JAVA_VM_EXT_H_