blob: 50aabdcdf5ab64b453a17a85eab9b2c28ab50536 [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;
Andreas Gampe57cf00b2017-06-05 17:15:32 -070035class IsMarkedVisitor;
Ian Rogers68d8b422014-07-17 11:09:10 -070036class Libraries;
37class ParsedOptions;
38class Runtime;
Igor Murashkinaaebaa02015-01-26 10:55:53 -080039struct RuntimeArgumentMap;
Ian Rogers68d8b422014-07-17 11:09:10 -070040
Alex Light185d1342016-08-11 10:48:03 -070041class JavaVMExt;
42// Hook definition for runtime plugins.
43using GetEnvHook = jint (*)(JavaVMExt* vm, /*out*/void** new_env, jint version);
44
Ian Rogers68d8b422014-07-17 11:09:10 -070045class JavaVMExt : public JavaVM {
46 public:
Richard Uhlerda0a69e2016-10-11 15:06:38 +010047 // Creates a new JavaVMExt object.
48 // Returns nullptr on error, in which case error_msg is set to a message
49 // describing the error.
50 static std::unique_ptr<JavaVMExt> Create(Runtime* runtime,
51 const RuntimeArgumentMap& runtime_options,
52 std::string* error_msg);
53
54
Ian Rogers68d8b422014-07-17 11:09:10 -070055 ~JavaVMExt();
56
57 bool ForceCopy() const {
58 return force_copy_;
59 }
60
61 bool IsCheckJniEnabled() const {
62 return check_jni_;
63 }
64
65 bool IsTracingEnabled() const {
66 return tracing_enabled_;
67 }
68
69 Runtime* GetRuntime() const {
70 return runtime_;
71 }
72
73 void SetCheckJniAbortHook(void (*hook)(void*, const std::string&), void* data) {
74 check_jni_abort_hook_ = hook;
75 check_jni_abort_hook_data_ = data;
76 }
77
78 // Aborts execution unless there is an abort handler installed in which case it will return. Its
79 // therefore important that callers return after aborting as otherwise code following the abort
80 // will be executed in the abort handler case.
81 void JniAbort(const char* jni_function_name, const char* msg);
82
83 void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap);
84
85 void JniAbortF(const char* jni_function_name, const char* fmt, ...)
86 __attribute__((__format__(__printf__, 3, 4)));
87
88 // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
89 // when a native method that matches the -Xjnitrace argument calls a JNI function
90 // such as NewByteArray.
91 // If -verbose:third-party-jni is on, we want to log any JNI function calls
92 // made by a third-party native method.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 bool ShouldTrace(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070094
95 /**
96 * Loads the given shared library. 'path' is an absolute pathname.
97 *
Dmitriy Ivanovf5a30992015-11-11 14:18:55 -080098 * Returns 'true' on success. On failure, sets 'error_msg' to a
Ian Rogers68d8b422014-07-17 11:09:10 -070099 * human-readable description of the error.
100 */
Dimitry Ivanov942dc2982016-02-24 13:33:33 -0800101 bool LoadNativeLibrary(JNIEnv* env,
102 const std::string& path,
103 jobject class_loader,
104 jstring library_path,
Dimitry Ivanovd5bbadf2015-12-15 14:08:18 -0800105 std::string* error_msg);
Ian Rogers68d8b422014-07-17 11:09:10 -0700106
Mathieu Chartier598302a2015-09-23 14:52:39 -0700107 // Unload native libraries with cleared class loaders.
108 void UnloadNativeLibraries()
109 REQUIRES(!Locks::jni_libraries_lock_)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700110 REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier598302a2015-09-23 14:52:39 -0700111
Ian Rogers68d8b422014-07-17 11:09:10 -0700112 /**
113 * Returns a pointer to the code for the native method 'm', found
114 * using dlsym(3) on every native library that's been loaded so far.
115 */
Mathieu Chartiere401d142015-04-22 13:56:20 -0700116 void* FindCodeForNativeMethod(ArtMethod* m)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700117 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700118
119 void DumpForSigQuit(std::ostream& os)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700120 REQUIRES(!Locks::jni_libraries_lock_,
121 !Locks::jni_globals_lock_,
122 !Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700123
124 void DumpReferenceTables(std::ostream& os)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700125 REQUIRES_SHARED(Locks::mutator_lock_)
126 REQUIRES(!Locks::jni_globals_lock_, !Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700127
128 bool SetCheckJniEnabled(bool enabled);
129
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700130 void VisitRoots(RootVisitor* visitor) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700131 REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700132
Andreas Gampe05a364c2016-10-14 13:27:12 -0700133 void DisallowNewWeakGlobals()
134 REQUIRES_SHARED(Locks::mutator_lock_)
135 REQUIRES(!Locks::jni_weak_globals_lock_);
136 void AllowNewWeakGlobals()
137 REQUIRES_SHARED(Locks::mutator_lock_)
138 REQUIRES(!Locks::jni_weak_globals_lock_);
139 void BroadcastForNewWeakGlobals()
Andreas Gampe05a364c2016-10-14 13:27:12 -0700140 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700141
Mathieu Chartier0795f232016-09-27 18:43:30 -0700142 jobject AddGlobalRef(Thread* self, ObjPtr<mirror::Object> obj)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700143 REQUIRES_SHARED(Locks::mutator_lock_)
144 REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700145
Mathieu Chartier0795f232016-09-27 18:43:30 -0700146 jweak AddWeakGlobalRef(Thread* self, ObjPtr<mirror::Object> obj)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700147 REQUIRES_SHARED(Locks::mutator_lock_)
148 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700149
Andreas Gampe05a364c2016-10-14 13:27:12 -0700150 void DeleteGlobalRef(Thread* self, jobject obj) REQUIRES(!Locks::jni_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700151
Andreas Gampe05a364c2016-10-14 13:27:12 -0700152 void DeleteWeakGlobalRef(Thread* self, jweak obj) REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700153
Mathieu Chartier97509952015-07-13 14:35:43 -0700154 void SweepJniWeakGlobals(IsMarkedVisitor* visitor)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700155 REQUIRES_SHARED(Locks::mutator_lock_)
156 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700157
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700158 ObjPtr<mirror::Object> DecodeGlobal(IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700159 REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700160
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700161 void UpdateGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700162 REQUIRES_SHARED(Locks::mutator_lock_)
163 REQUIRES(!Locks::jni_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700164
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700165 ObjPtr<mirror::Object> DecodeWeakGlobal(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700166 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700167 REQUIRES(!Locks::jni_weak_globals_lock_);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700168
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700169 ObjPtr<mirror::Object> DecodeWeakGlobalLocked(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700170 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700171 REQUIRES(Locks::jni_weak_globals_lock_);
Mathieu Chartier673ed3d2015-08-28 14:56:43 -0700172
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700173 // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be
174 // null.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700175 ObjPtr<mirror::Object> DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700176 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700177 REQUIRES(!Locks::jni_weak_globals_lock_);
Hiroshi Yamauchi498b1602015-09-16 21:11:44 -0700178
Hiroshi Yamauchi04302db2015-11-11 23:45:34 -0800179 // Checks if the weak global ref has been cleared by the GC without decode (read barrier.)
180 bool IsWeakGlobalCleared(Thread* self, IndirectRef ref)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700181 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700182 REQUIRES(!Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700183
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700184 void UpdateWeakGlobal(Thread* self, IndirectRef ref, ObjPtr<mirror::Object> result)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700185 REQUIRES_SHARED(Locks::mutator_lock_)
186 REQUIRES(!Locks::jni_weak_globals_lock_);
Jeff Hao83c81952015-05-27 19:29:29 -0700187
Ian Rogers68d8b422014-07-17 11:09:10 -0700188 const JNIInvokeInterface* GetUncheckedFunctions() const {
189 return unchecked_functions_;
190 }
191
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700192 void TrimGlobals() REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700193 REQUIRES(!Locks::jni_globals_lock_);
Mathieu Chartier91c2f0c2014-11-26 11:21:15 -0800194
Alex Light185d1342016-08-11 10:48:03 -0700195 jint HandleGetEnv(/*out*/void** env, jint version);
196
197 void AddEnvironmentHook(GetEnvHook hook);
198
199 static bool IsBadJniVersion(int version);
200
Ian Rogers68d8b422014-07-17 11:09:10 -0700201 private:
Richard Uhlerda0a69e2016-10-11 15:06:38 +0100202 // The constructor should not be called directly. It may leave the object in
203 // an erroneous state, and the result needs to be checked.
204 JavaVMExt(Runtime* runtime, const RuntimeArgumentMap& runtime_options, std::string* error_msg);
205
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700206 // Return true if self can currently access weak globals.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700207 bool MayAccessWeakGlobalsUnlocked(Thread* self) const REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700208 bool MayAccessWeakGlobals(Thread* self) const
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700209 REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe05a364c2016-10-14 13:27:12 -0700210 REQUIRES(Locks::jni_weak_globals_lock_);
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700211
Ian Rogers68d8b422014-07-17 11:09:10 -0700212 Runtime* const runtime_;
213
214 // Used for testing. By default, we'll LOG(FATAL) the reason.
215 void (*check_jni_abort_hook_)(void* data, const std::string& reason);
216 void* check_jni_abort_hook_data_;
217
218 // Extra checking.
219 bool check_jni_;
220 bool force_copy_;
221 const bool tracing_enabled_;
222
223 // Extra diagnostics.
224 const std::string trace_;
225
Ian Rogers68d8b422014-07-17 11:09:10 -0700226 // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject.
227 IndirectReferenceTable globals_;
228
Mathieu Chartier598302a2015-09-23 14:52:39 -0700229 // No lock annotation since UnloadNativeLibraries is called on libraries_ but locks the
230 // jni_libraries_lock_ internally.
231 std::unique_ptr<Libraries> libraries_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700232
233 // Used by -Xcheck:jni.
234 const JNIInvokeInterface* const unchecked_functions_;
235
Ian Rogers68d8b422014-07-17 11:09:10 -0700236 // Since weak_globals_ contain weak roots, be careful not to
237 // directly access the object references in it. Use Get() with the
238 // read barrier enabled.
Mathieu Chartier30b5e272015-09-01 11:14:34 -0700239 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
240 IndirectReferenceTable weak_globals_;
241 // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
242 Atomic<bool> allow_accessing_weak_globals_;
Andreas Gampe05a364c2016-10-14 13:27:12 -0700243 ConditionVariable weak_globals_add_condition_ GUARDED_BY(Locks::jni_weak_globals_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -0700244
Alex Light185d1342016-08-11 10:48:03 -0700245 // TODO Maybe move this to Runtime.
246 std::vector<GetEnvHook> env_hooks_;
247
Ian Rogers68d8b422014-07-17 11:09:10 -0700248 DISALLOW_COPY_AND_ASSIGN(JavaVMExt);
249};
250
251} // namespace art
252
253#endif // ART_RUNTIME_JAVA_VM_EXT_H_