blob: af87cb42262716a46e4116d57a9c8a343a8b4cd4 [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 {
37 JNIEnvExt(Thread* self, JavaVMExt* vm);
38 ~JNIEnvExt();
39
40 void DumpReferenceTables(std::ostream& os)
41 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
42
43 void SetCheckJniEnabled(bool enabled);
44
45 void PushFrame(int capacity);
46 void PopFrame();
47
48 template<typename T>
49 T AddLocalReference(mirror::Object* obj)
50 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
51
52 static Offset SegmentStateOffset();
53
54 static Offset LocalRefCookieOffset() {
55 return Offset(OFFSETOF_MEMBER(JNIEnvExt, local_ref_cookie));
56 }
57
58 static Offset SelfOffset() {
59 return Offset(OFFSETOF_MEMBER(JNIEnvExt, self));
60 }
61
62 jobject NewLocalRef(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
63 void DeleteLocalRef(jobject obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
64
65 Thread* const self;
66 JavaVMExt* const vm;
67
68 // Cookie used when using the local indirect reference table.
69 uint32_t local_ref_cookie;
70
71 // JNI local references.
72 IndirectReferenceTable locals GUARDED_BY(Locks::mutator_lock_);
73
74 // Stack of cookies corresponding to PushLocalFrame/PopLocalFrame calls.
75 // TODO: to avoid leaks (and bugs), we need to clear this vector on entry (or return)
76 // to a native method.
77 std::vector<uint32_t> stacked_local_ref_cookies;
78
79 // Frequently-accessed fields cached from JavaVM.
80 bool check_jni;
81
82 // How many nested "critical" JNI calls are we in?
83 int critical;
84
85 // Entered JNI monitors, for bulk exit on thread detach.
86 ReferenceTable monitors;
87
88 // Used by -Xcheck:jni.
89 const JNINativeInterface* unchecked_functions;
90};
91
92// Used to save and restore the JNIEnvExt state when not going through code created by the JNI
93// compiler.
94class ScopedJniEnvLocalRefState {
95 public:
96 explicit ScopedJniEnvLocalRefState(JNIEnvExt* env) : env_(env) {
97 saved_local_ref_cookie_ = env->local_ref_cookie;
98 env->local_ref_cookie = env->locals.GetSegmentState();
99 }
100
101 ~ScopedJniEnvLocalRefState() {
102 env_->locals.SetSegmentState(env_->local_ref_cookie);
103 env_->local_ref_cookie = saved_local_ref_cookie_;
104 }
105
106 private:
107 JNIEnvExt* const env_;
108 uint32_t saved_local_ref_cookie_;
109
110 DISALLOW_COPY_AND_ASSIGN(ScopedJniEnvLocalRefState);
111};
112
113} // namespace art
114
115#endif // ART_RUNTIME_JNI_ENV_EXT_H_