blob: 558ffffe5ab512224afcd81266946b7f45f19328 [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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700112 REQUIRES(!Locks::jni_libraries_lock_, !globals_lock_, !weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700113
114 void DumpReferenceTables(std::ostream& os)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700115 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!globals_lock_, !weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700116
117 bool SetCheckJniEnabled(bool enabled);
118
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700119 void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700120 REQUIRES(!globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700121
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700122 void DisallowNewWeakGlobals() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
123 void AllowNewWeakGlobals() REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
124 void BroadcastForNewWeakGlobals() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700125 REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700126
Mathieu Chartier0795f232016-09-27 18:43:30 -0700127 jobject AddGlobalRef(Thread* self, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700129
Mathieu Chartier0795f232016-09-27 18:43:30 -0700130 jweak AddWeakGlobalRef(Thread* self, ObjPtr<mirror::Object> obj)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700131 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700132
Mathieu Chartier90443472015-07-16 20:32:27 -0700133 void DeleteGlobalRef(Thread* self, jobject obj) REQUIRES(!globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700134
Mathieu Chartier90443472015-07-16 20:32:27 -0700135 void DeleteWeakGlobalRef(Thread* self, jweak obj) REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700136
Mathieu Chartier97509952015-07-13 14:35:43 -0700137 void SweepJniWeakGlobals(IsMarkedVisitor* visitor)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700138 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700139
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700140 mirror::Object* DecodeGlobal(IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700141 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700142
Jeff Hao83c81952015-05-27 19:29:29 -0700143 void UpdateGlobal(Thread* self, IndirectRef ref, mirror::Object* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700144 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700145
Ian Rogers68d8b422014-07-17 11:09:10 -0700146 mirror::Object* DecodeWeakGlobal(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700147 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700148 REQUIRES(!weak_globals_lock_);
149
150 mirror::Object* DecodeWeakGlobalLocked(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700152 REQUIRES(weak_globals_lock_);
153
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700154 // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be
155 // null.
156 mirror::Object* DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700157 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700158 REQUIRES(!weak_globals_lock_);
159
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800160 // Checks if the weak global ref has been cleared by the GC without decode (read barrier.)
161 bool IsWeakGlobalCleared(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700162 REQUIRES_SHARED(Locks::mutator_lock_)
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800163 REQUIRES(!weak_globals_lock_);
164
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700165 Mutex& WeakGlobalsLock() RETURN_CAPABILITY(weak_globals_lock_) {
166 return weak_globals_lock_;
167 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700168
Jeff Hao83c81952015-05-27 19:29:29 -0700169 void UpdateWeakGlobal(Thread* self, IndirectRef ref, mirror::Object* result)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700170 REQUIRES_SHARED(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700171
Ian Rogers68d8b422014-07-17 11:09:10 -0700172 const JNIInvokeInterface* GetUncheckedFunctions() const {
173 return unchecked_functions_;
174 }
175
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700176 void TrimGlobals() REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier90443472015-07-16 20:32:27 -0700177 REQUIRES(!globals_lock_);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800178
Alex Light185d1342016-08-11 10:48:03 -0700179 jint HandleGetEnv(/*out*/void** env, jint version);
180
181 void AddEnvironmentHook(GetEnvHook hook);
182
183 static bool IsBadJniVersion(int version);
184
Ian Rogers68d8b422014-07-17 11:09:10 -0700185 private:
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700186 // Return true if self can currently access weak globals.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700187 bool MayAccessWeakGlobalsUnlocked(Thread* self) const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700188 bool MayAccessWeakGlobals(Thread* self) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700189 REQUIRES_SHARED(Locks::mutator_lock_)
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700190 REQUIRES(weak_globals_lock_);
191
Ian Rogers68d8b422014-07-17 11:09:10 -0700192 Runtime* const runtime_;
193
194 // Used for testing. By default, we'll LOG(FATAL) the reason.
195 void (*check_jni_abort_hook_)(void* data, const std::string& reason);
196 void* check_jni_abort_hook_data_;
197
198 // Extra checking.
199 bool check_jni_;
200 bool force_copy_;
201 const bool tracing_enabled_;
202
203 // Extra diagnostics.
204 const std::string trace_;
205
Ian Rogers68d8b422014-07-17 11:09:10 -0700206 // JNI global references.
207 ReaderWriterMutex globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
208 // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject.
209 IndirectReferenceTable globals_;
210
Mathieu Chartier598302a2015-09-23 14:52:39 -0700211 // No lock annotation since UnloadNativeLibraries is called on libraries_ but locks the
212 // jni_libraries_lock_ internally.
213 std::unique_ptr<Libraries> libraries_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700214
215 // Used by -Xcheck:jni.
216 const JNIInvokeInterface* const unchecked_functions_;
217
218 // JNI weak global references.
219 Mutex weak_globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
220 // Since weak_globals_ contain weak roots, be careful not to
221 // directly access the object references in it. Use Get() with the
222 // read barrier enabled.
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700223 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
224 IndirectReferenceTable weak_globals_;
225 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
226 Atomic<bool> allow_accessing_weak_globals_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700227 ConditionVariable weak_globals_add_condition_ GUARDED_BY(weak_globals_lock_);
228
Alex Light185d1342016-08-11 10:48:03 -0700229 // TODO Maybe move this to Runtime.
230 std::vector<GetEnvHook> env_hooks_;
231
Ian Rogers68d8b422014-07-17 11:09:10 -0700232 DISALLOW_COPY_AND_ASSIGN(JavaVMExt);
233};
234
235} // namespace art
236
237#endif // ART_RUNTIME_JAVA_VM_EXT_H_