blob: 2f8decf98f865000eb7f9e46c99f9108f7b7fba1 [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_JNI_ENV_EXT_H_
18#define ART_RUNTIME_JNI_ENV_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 "object_callbacks.h"
26#include "reference_table.h"
27
28namespace art {
29
30class JavaVMExt;
31
32// Maximum number of local references in the indirect reference table. The value is arbitrary but
33// low enough that it forces sanity checks.
34static constexpr size_t kLocalsMax = 512;
35
36struct JNIEnvExt : public JNIEnv {
Andreas Gampe3f5881f2015-04-08 10:26:16 -070037 static JNIEnvExt* Create(Thread* self, JavaVMExt* vm);
38
Ian Rogers68d8b422014-07-17 11:09:10 -070039 ~JNIEnvExt();
40
41 void DumpReferenceTables(std::ostream& os)
Mathieu Chartier90443472015-07-16 20:32:27 -070042 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070043
44 void SetCheckJniEnabled(bool enabled);
45
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070046 void PushFrame(int capacity) SHARED_REQUIRES(Locks::mutator_lock_);
47 void PopFrame() SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070048
49 template<typename T>
50 T AddLocalReference(mirror::Object* obj)
Mathieu Chartier90443472015-07-16 20:32:27 -070051 SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070052
Andreas Gampe4d98c842015-12-09 15:14:04 -080053 static Offset SegmentStateOffset(size_t pointer_size);
54 static Offset LocalRefCookieOffset(size_t pointer_size);
55 static Offset SelfOffset(size_t pointer_size);
Ian Rogers68d8b422014-07-17 11:09:10 -070056
Mathieu Chartier90443472015-07-16 20:32:27 -070057 jobject NewLocalRef(mirror::Object* obj) SHARED_REQUIRES(Locks::mutator_lock_);
58 void DeleteLocalRef(jobject obj) SHARED_REQUIRES(Locks::mutator_lock_);
Ian Rogers68d8b422014-07-17 11:09:10 -070059
60 Thread* const self;
61 JavaVMExt* const vm;
62
63 // Cookie used when using the local indirect reference table.
64 uint32_t local_ref_cookie;
65
66 // JNI local references.
67 IndirectReferenceTable locals GUARDED_BY(Locks::mutator_lock_);
68
69 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
70 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
71 // to a native method.
72 std::vector<uint32_t> stacked_local_ref_cookies;
73
74 // Frequently-accessed fields cached from JavaVM.
75 bool check_jni;
76
77 // How many nested "critical" JNI calls are we in?
78 int critical;
79
80 // Entered JNI monitors, for bulk exit on thread detach.
81 ReferenceTable monitors;
82
83 // Used by -Xcheck:jni.
84 const JNINativeInterface* unchecked_functions;
Andreas Gampe3f5881f2015-04-08 10:26:16 -070085
Andreas Gampe5f4a09a2015-09-28 13:16:33 -070086 // Functions to keep track of monitor lock and unlock operations. Used to ensure proper locking
87 // rules in CheckJNI mode.
88
89 // Record locking of a monitor.
90 void RecordMonitorEnter(jobject obj) SHARED_REQUIRES(Locks::mutator_lock_);
91
92 // Check the release, that is, that the release is performed in the same JNI "segment."
93 void CheckMonitorRelease(jobject obj) SHARED_REQUIRES(Locks::mutator_lock_);
94
95 // Check that no monitors are held that have been acquired in this JNI "segment."
96 void CheckNoHeldMonitors() SHARED_REQUIRES(Locks::mutator_lock_);
97
Andreas Gampe3f5881f2015-04-08 10:26:16 -070098 private:
99 // The constructor should not be called directly. It may leave the object in an erronuous state,
100 // and the result needs to be checked.
101 JNIEnvExt(Thread* self, JavaVMExt* vm);
Andreas Gampe5f4a09a2015-09-28 13:16:33 -0700102
103 // All locked objects, with the (Java caller) stack frame that locked them. Used in CheckJNI
104 // to ensure that only monitors locked in this native frame are being unlocked, and that at
105 // the end all are unlocked.
106 std::vector<std::pair<uintptr_t, jobject>> locked_objects_;
Ian Rogers68d8b422014-07-17 11:09:10 -0700107};
108
109// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
110// compiler.
111class ScopedJniEnvLocalRefState {
112 public:
113 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
114 saved_local_ref_cookie_ = env->local_ref_cookie;
115 env->local_ref_cookie = env->locals.GetSegmentState();
116 }
117
118 ~ScopedJniEnvLocalRefState() {
119 env_->locals.SetSegmentState(env_->local_ref_cookie);
120 env_->local_ref_cookie = saved_local_ref_cookie_;
121 }
122
123 private:
124 JNIEnvExt* const env_;
125 uint32_t saved_local_ref_cookie_;
126
127 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
128};
129
130} // namespace art
131
132#endif // ART_RUNTIME_JNI_ENV_EXT_H_