java_vm_ext.h revision 44409d33e05cd2f73d69e0a98daa5c9e2fe38089
1/*
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"
25#include "reference_table.h"
26
27namespace art {
28
29namespace mirror {
30  class Array;
31}  // namespace mirror
32
33class ArtMethod;
34class Libraries;
35class ParsedOptions;
36class Runtime;
37struct RuntimeArgumentMap;
38
39class JavaVMExt : public JavaVM {
40 public:
41  JavaVMExt(Runtime* runtime, const RuntimeArgumentMap& runtime_options);
42  ~JavaVMExt();
43
44  bool ForceCopy() const {
45    return force_copy_;
46  }
47
48  bool IsCheckJniEnabled() const {
49    return check_jni_;
50  }
51
52  bool IsTracingEnabled() const {
53    return tracing_enabled_;
54  }
55
56  Runtime* GetRuntime() const {
57    return runtime_;
58  }
59
60  void SetCheckJniAbortHook(void (*hook)(void*, const std::string&), void* data) {
61    check_jni_abort_hook_ = hook;
62    check_jni_abort_hook_data_ = data;
63  }
64
65  // Aborts execution unless there is an abort handler installed in which case it will return. Its
66  // therefore important that callers return after aborting as otherwise code following the abort
67  // will be executed in the abort handler case.
68  void JniAbort(const char* jni_function_name, const char* msg);
69
70  void JniAbortV(const char* jni_function_name, const char* fmt, va_list ap);
71
72  void JniAbortF(const char* jni_function_name, const char* fmt, ...)
73      __attribute__((__format__(__printf__, 3, 4)));
74
75  // If both "-Xcheck:jni" and "-Xjnitrace:" are enabled, we print trace messages
76  // when a native method that matches the -Xjnitrace argument calls a JNI function
77  // such as NewByteArray.
78  // If -verbose:third-party-jni is on, we want to log any JNI function calls
79  // made by a third-party native method.
80  bool ShouldTrace(ArtMethod* method) SHARED_REQUIRES(Locks::mutator_lock_);
81
82  /**
83   * Loads the given shared library. 'path' is an absolute pathname.
84   *
85   * Returns 'true' on success. On failure, sets 'error_msg' to a
86   * human-readable description of the error.
87   */
88  bool LoadNativeLibrary(JNIEnv* env, const std::string& path, jobject javaLoader,
89                         jstring library_path, jstring permitted_path, std::string* error_msg);
90
91  // Unload native libraries with cleared class loaders.
92  void UnloadNativeLibraries()
93      REQUIRES(!Locks::jni_libraries_lock_)
94      SHARED_REQUIRES(Locks::mutator_lock_);
95
96  /**
97   * Returns a pointer to the code for the native method 'm', found
98   * using dlsym(3) on every native library that's been loaded so far.
99   */
100  void* FindCodeForNativeMethod(ArtMethod* m)
101      SHARED_REQUIRES(Locks::mutator_lock_);
102
103  void DumpForSigQuit(std::ostream& os)
104      REQUIRES(!Locks::jni_libraries_lock_, !globals_lock_, !weak_globals_lock_);
105
106  void DumpReferenceTables(std::ostream& os)
107      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_, !weak_globals_lock_);
108
109  bool SetCheckJniEnabled(bool enabled);
110
111  void VisitRoots(RootVisitor* visitor) SHARED_REQUIRES(Locks::mutator_lock_)
112      REQUIRES(!globals_lock_);
113
114  void DisallowNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
115  void AllowNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
116  void BroadcastForNewWeakGlobals() SHARED_REQUIRES(Locks::mutator_lock_)
117      REQUIRES(!weak_globals_lock_);
118
119  jobject AddGlobalRef(Thread* self, mirror::Object* obj)
120      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_);
121
122  jweak AddWeakGlobalRef(Thread* self, mirror::Object* obj)
123    SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
124
125  void DeleteGlobalRef(Thread* self, jobject obj) REQUIRES(!globals_lock_);
126
127  void DeleteWeakGlobalRef(Thread* self, jweak obj) REQUIRES(!weak_globals_lock_);
128
129  void SweepJniWeakGlobals(IsMarkedVisitor* visitor)
130      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
131
132  mirror::Object* DecodeGlobal(IndirectRef ref)
133      SHARED_REQUIRES(Locks::mutator_lock_);
134
135  void UpdateGlobal(Thread* self, IndirectRef ref, mirror::Object* result)
136      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!globals_lock_);
137
138  mirror::Object* DecodeWeakGlobal(Thread* self, IndirectRef ref)
139      SHARED_REQUIRES(Locks::mutator_lock_)
140      REQUIRES(!weak_globals_lock_);
141
142  mirror::Object* DecodeWeakGlobalLocked(Thread* self, IndirectRef ref)
143      SHARED_REQUIRES(Locks::mutator_lock_)
144      REQUIRES(weak_globals_lock_);
145
146  // Like DecodeWeakGlobal() but to be used only during a runtime shutdown where self may be
147  // null.
148  mirror::Object* DecodeWeakGlobalDuringShutdown(Thread* self, IndirectRef ref)
149      SHARED_REQUIRES(Locks::mutator_lock_)
150      REQUIRES(!weak_globals_lock_);
151
152  // Checks if the weak global ref has been cleared by the GC without decode (read barrier.)
153  bool IsWeakGlobalCleared(Thread* self, IndirectRef ref)
154      SHARED_REQUIRES(Locks::mutator_lock_)
155      REQUIRES(!weak_globals_lock_);
156
157  Mutex& WeakGlobalsLock() RETURN_CAPABILITY(weak_globals_lock_) {
158    return weak_globals_lock_;
159  }
160
161  void UpdateWeakGlobal(Thread* self, IndirectRef ref, mirror::Object* result)
162      SHARED_REQUIRES(Locks::mutator_lock_) REQUIRES(!weak_globals_lock_);
163
164  const JNIInvokeInterface* GetUncheckedFunctions() const {
165    return unchecked_functions_;
166  }
167
168  void TrimGlobals() SHARED_REQUIRES(Locks::mutator_lock_)
169      REQUIRES(!globals_lock_);
170
171 private:
172  // Return true if self can currently access weak globals.
173  bool MayAccessWeakGlobalsUnlocked(Thread* self) const SHARED_REQUIRES(Locks::mutator_lock_);
174  bool MayAccessWeakGlobals(Thread* self) const
175      SHARED_REQUIRES(Locks::mutator_lock_)
176      REQUIRES(weak_globals_lock_);
177
178  Runtime* const runtime_;
179
180  // Used for testing. By default, we'll LOG(FATAL) the reason.
181  void (*check_jni_abort_hook_)(void* data, const std::string& reason);
182  void* check_jni_abort_hook_data_;
183
184  // Extra checking.
185  bool check_jni_;
186  bool force_copy_;
187  const bool tracing_enabled_;
188
189  // Extra diagnostics.
190  const std::string trace_;
191
192  // JNI global references.
193  ReaderWriterMutex globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
194  // Not guarded by globals_lock since we sometimes use SynchronizedGet in Thread::DecodeJObject.
195  IndirectReferenceTable globals_;
196
197  // No lock annotation since UnloadNativeLibraries is called on libraries_ but locks the
198  // jni_libraries_lock_ internally.
199  std::unique_ptr<Libraries> libraries_;
200
201  // Used by -Xcheck:jni.
202  const JNIInvokeInterface* const unchecked_functions_;
203
204  // JNI weak global references.
205  Mutex weak_globals_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
206  // Since weak_globals_ contain weak roots, be careful not to
207  // directly access the object references in it. Use Get() with the
208  // read barrier enabled.
209  // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
210  IndirectReferenceTable weak_globals_;
211  // Not guarded by weak_globals_lock since we may use SynchronizedGet in DecodeWeakGlobal.
212  Atomic<bool> allow_accessing_weak_globals_;
213  ConditionVariable weak_globals_add_condition_ GUARDED_BY(weak_globals_lock_);
214
215  DISALLOW_COPY_AND_ASSIGN(JavaVMExt);
216};
217
218}  // namespace art
219
220#endif  // ART_RUNTIME_JAVA_VM_EXT_H_
221