1 /*
2  * Copyright (C) 2010 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 #define LOG_TAG "JniConstants"
18 
19 #include "JniConstants.h"
20 
21 #include <atomic>
22 #include <mutex>
23 #include <stdlib.h>
24 
25 #include <log/log.h>
26 #include <nativehelper/ScopedLocalRef.h>
27 
28 namespace {
29 
findClass(JNIEnv * env,const char * name)30 jclass findClass(JNIEnv* env, const char* name) {
31     ScopedLocalRef<jclass> localClass(env, env->FindClass(name));
32     jclass result = reinterpret_cast<jclass>(env->NewGlobalRef(localClass.get()));
33     if (result == NULL) {
34         ALOGE("failed to find class '%s'", name);
35         abort();
36     }
37     return result;
38 }
39 
40 // Mutex protecting static variables
41 static std::mutex g_constants_mutex;
42 
43 // Flag indicating whether cached constants are valid
44 static bool g_constants_valid = false;
45 
46 // Mapping between C++ names and java class descriptors.
47 #define JCLASS_CONSTANTS_LIST(V)                                                            \
48     V(BooleanClass, "java/lang/Boolean")                                                    \
49     V(ByteBufferClass, "java/nio/ByteBuffer")                                               \
50     V(DoubleClass, "java/lang/Double")                                                      \
51     V(ErrnoExceptionClass, "android/system/ErrnoException")                                 \
52     V(FileDescriptorClass, "java/io/FileDescriptor")                                        \
53     V(GaiExceptionClass, "android/system/GaiException")                                     \
54     V(Inet6AddressClass, "java/net/Inet6Address")                                           \
55     V(Inet6AddressHolderClass, "java/net/Inet6Address$Inet6AddressHolder")                  \
56     V(InetAddressClass, "java/net/InetAddress")                                             \
57     V(InetAddressHolderClass, "java/net/InetAddress$InetAddressHolder")                     \
58     V(InetSocketAddressClass, "java/net/InetSocketAddress")                                 \
59     V(InetSocketAddressHolderClass, "java/net/InetSocketAddress$InetSocketAddressHolder")   \
60     V(IntegerClass, "java/lang/Integer")                                                    \
61     V(LocaleDataClass, "libcore/icu/LocaleData")                                            \
62     V(LongClass, "java/lang/Long")                                                          \
63     V(NetlinkSocketAddressClass, "android/system/NetlinkSocketAddress")                     \
64     V(PacketSocketAddressClass, "android/system/PacketSocketAddress")                       \
65     V(VmSocketAddressClass, "android/system/VmSocketAddress")                               \
66     V(PrimitiveByteArrayClass, "[B")                                                        \
67     V(StringClass, "java/lang/String")                                                      \
68     V(StructAddrinfoClass, "android/system/StructAddrinfo")                                 \
69     V(StructCmsghdrClass, "android/system/StructCmsghdr")                                   \
70     V(StructGroupReqClass, "android/system/StructGroupReq")                                 \
71     V(StructIfaddrsClass, "android/system/StructIfaddrs")                                   \
72     V(StructLingerClass, "android/system/StructLinger")                                     \
73     V(StructMsghdrClass, "android/system/StructMsghdr")                                     \
74     V(StructPasswdClass, "android/system/StructPasswd")                                     \
75     V(StructPollfdClass, "android/system/StructPollfd")                                     \
76     V(StructStatClass, "android/system/StructStat")                                         \
77     V(StructStatVfsClass, "android/system/StructStatVfs")                                   \
78     V(StructTimevalClass, "android/system/StructTimeval")                                   \
79     V(StructTimespecClass, "android/system/StructTimespec")                                 \
80     V(StructUcredClass, "android/system/StructUcred")                                       \
81     V(StructUtsnameClass, "android/system/StructUtsname")                                   \
82     V(UnixSocketAddressClass, "android/system/UnixSocketAddress")
83 
84 #define DECLARE_JCLASS_CONSTANT(cppname, _) jclass g_ ## cppname;
JCLASS_CONSTANTS_LIST(DECLARE_JCLASS_CONSTANT)85 JCLASS_CONSTANTS_LIST(DECLARE_JCLASS_CONSTANT)
86 
87 // EnsureJniConstantsInitialized initializes cached constants. It should be
88 // called before returning a heap object from the cache to ensure cache is
89 // initialized. This pattern is only necessary because if a process finishes one
90 // runtime and starts another then JNI_OnLoad may not be called.
91 void EnsureJniConstantsInitialized(JNIEnv* env) {
92     std::lock_guard guard(g_constants_mutex);
93     if (g_constants_valid) {
94         return;
95     }
96 
97 #define INITIALIZE_JCLASS_CONSTANT(cppname, javaname) g_ ## cppname = findClass(env, javaname);
98 JCLASS_CONSTANTS_LIST(INITIALIZE_JCLASS_CONSTANT)
99 
100     g_constants_valid = true;
101 }
102 
103 }  // namespace
104 
105 #define CONSTANT_GETTER(cppname, _)                                                         \
106 jclass JniConstants::Get ## cppname(JNIEnv* env) {                                          \
107     EnsureJniConstantsInitialized(env);                                                     \
108     return g_ ## cppname;                                                                   \
109 }
JCLASS_CONSTANTS_LIST(CONSTANT_GETTER)110 JCLASS_CONSTANTS_LIST(CONSTANT_GETTER)
111 
112 void JniConstants::Initialize(JNIEnv* env) {
113     EnsureJniConstantsInitialized(env);
114 }
115 
Invalidate()116 void JniConstants::Invalidate() {
117     // This method is called when a new runtime instance is created. There is no
118     // notification of a runtime instance being destroyed in the JNI interface
119     // so we piggyback on creation. Since only one runtime is supported at a
120     // time, we know the constants are invalid when JNI_CreateJavaVM() is
121     // called.
122     //
123     // Clean shutdown would require calling DeleteGlobalRef() for each of the
124     // class references, but JavaVM is unavailable because ART only calls this
125     // once all threads are unregistered.
126     std::lock_guard guard(g_constants_mutex);
127     g_constants_valid = false;
128 }
129