• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 #include "core_jni_helpers.h"
18 #include <cutils/ashmem.h>
19 #include <linux/ashmem.h>
20 #include <sys/mman.h>
21 
22 namespace android {
23 
android_util_MemoryIntArray_create(JNIEnv * env,jobject clazz,jstring name,jint size)24 static jint android_util_MemoryIntArray_create(JNIEnv* env, jobject clazz, jstring name,
25         jint size)
26 {
27     if (name == NULL) {
28         jniThrowException(env, "java/io/IOException", "bad name");
29         return -1;
30     }
31 
32     if (size <= 0) {
33         jniThrowException(env, "java/io/IOException", "bad size");
34         return -1;
35     }
36 
37     const char* nameStr = env->GetStringUTFChars(name, NULL);
38     const int ashmemSize = sizeof(std::atomic_int) * size;
39     int fd = ashmem_create_region(nameStr, ashmemSize);
40     env->ReleaseStringUTFChars(name, nameStr);
41 
42     if (fd < 0) {
43         jniThrowException(env, "java/io/IOException", "ashmem creation failed");
44         return -1;
45     }
46 
47     int setProtResult = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE);
48     if (setProtResult < 0) {
49         jniThrowException(env, "java/io/IOException", "cannot set ashmem prot mode");
50         return -1;
51     }
52 
53     return fd;
54 }
55 
android_util_MemoryIntArray_open(JNIEnv * env,jobject clazz,jint fd,jboolean owner)56 static jlong android_util_MemoryIntArray_open(JNIEnv* env, jobject clazz, jint fd,
57     jboolean owner)
58 {
59     if (fd < 0) {
60         jniThrowException(env, "java/io/IOException", "bad file descriptor");
61         return -1;
62     }
63 
64     int ashmemSize = ashmem_get_size_region(fd);
65     if (ashmemSize <= 0) {
66         jniThrowException(env, "java/io/IOException", "bad ashmem size");
67         return -1;
68     }
69 
70     // IMPORTANT: Ashmem allows the caller to change its size until
71     // it is memory mapped for the first time which lazily creates
72     // the underlying VFS file. So the size we get above may not
73     // reflect the size of the underlying shared memory region. Therefore,
74     // we first memory map to set the size in stone an verify if
75     // the underlying ashmem region has the same size as the one we
76     // memory mapped. This is critical as we use the underlying
77     // ashmem size for boundary checks and memory unmapping.
78     int protMode = owner ? (PROT_READ | PROT_WRITE) : PROT_READ;
79     void* ashmemAddr = mmap(NULL, ashmemSize, protMode, MAP_SHARED, fd, 0);
80     if (ashmemAddr == MAP_FAILED) {
81         jniThrowException(env, "java/io/IOException", "cannot mmap ashmem");
82         return -1;
83     }
84 
85     // Check if the mapped size is the same as the ashmem region.
86     int mmapedSize = ashmem_get_size_region(fd);
87     if (mmapedSize != ashmemSize) {
88         munmap(reinterpret_cast<void *>(ashmemAddr), ashmemSize);
89         jniThrowException(env, "java/io/IOException", "bad file descriptor");
90         return -1;
91     }
92 
93     if (owner) {
94         int size = ashmemSize / sizeof(std::atomic_int);
95         new (ashmemAddr) std::atomic_int[size];
96     }
97 
98     if (owner) {
99         int setProtResult = ashmem_set_prot_region(fd, PROT_READ);
100         if (setProtResult < 0) {
101             jniThrowException(env, "java/io/IOException", "cannot set ashmem prot mode");
102             return -1;
103         }
104     }
105 
106     return reinterpret_cast<jlong>(ashmemAddr);
107 }
108 
android_util_MemoryIntArray_close(JNIEnv * env,jobject clazz,jint fd,jlong ashmemAddr,jboolean owner)109 static void android_util_MemoryIntArray_close(JNIEnv* env, jobject clazz, jint fd,
110     jlong ashmemAddr, jboolean owner)
111 {
112     if (fd < 0) {
113         jniThrowException(env, "java/io/IOException", "bad file descriptor");
114         return;
115     }
116 
117     int ashmemSize = ashmem_get_size_region(fd);
118     if (ashmemSize <= 0) {
119         jniThrowException(env, "java/io/IOException", "bad ashmem size");
120         return;
121     }
122 
123     int unmapResult = munmap(reinterpret_cast<void *>(ashmemAddr), ashmemSize);
124     if (unmapResult < 0) {
125         jniThrowException(env, "java/io/IOException", "munmap failed");
126         return;
127     }
128 
129     // We don't deallocate the atomic ints we created with placement new in the ashmem
130     // region as the kernel we reclaim all pages when the ashmem region is destroyed.
131     if (owner && (ashmem_unpin_region(fd, 0, 0) != ASHMEM_IS_UNPINNED)) {
132         jniThrowException(env, "java/io/IOException", "ashmem unpinning failed");
133         return;
134     }
135 
136     close(fd);
137 }
138 
android_util_MemoryIntArray_get(JNIEnv * env,jobject clazz,jint fd,jlong address,jint index)139 static jint android_util_MemoryIntArray_get(JNIEnv* env, jobject clazz,
140         jint fd, jlong address, jint index)
141 {
142     if (fd < 0) {
143         jniThrowException(env, "java/io/IOException", "bad file descriptor");
144         return -1;
145     }
146 
147     if (ashmem_pin_region(fd, 0, 0) == ASHMEM_WAS_PURGED) {
148         jniThrowException(env, "java/io/IOException", "ashmem region was purged");
149         return -1;
150     }
151 
152     std::atomic_int* value = reinterpret_cast<std::atomic_int*>(address) + index;
153     return value->load(std::memory_order_relaxed);
154 }
155 
android_util_MemoryIntArray_set(JNIEnv * env,jobject clazz,jint fd,jlong address,jint index,jint newValue)156 static void android_util_MemoryIntArray_set(JNIEnv* env, jobject clazz,
157         jint fd, jlong address, jint index, jint newValue)
158 {
159     if (fd < 0) {
160         jniThrowException(env, "java/io/IOException", "bad file descriptor");
161         return;
162     }
163 
164     if (ashmem_pin_region(fd, 0, 0) == ASHMEM_WAS_PURGED) {
165         jniThrowException(env, "java/io/IOException", "ashmem region was purged");
166         return;
167     }
168 
169     std::atomic_int* value = reinterpret_cast<std::atomic_int*>(address) + index;
170     value->store(newValue, std::memory_order_relaxed);
171 }
172 
android_util_MemoryIntArray_size(JNIEnv * env,jobject clazz,jint fd)173 static jint android_util_MemoryIntArray_size(JNIEnv* env, jobject clazz, jint fd) {
174     if (fd < 0) {
175         jniThrowException(env, "java/io/IOException", "bad file descriptor");
176         return -1;
177     }
178 
179     int ashmemSize = ashmem_get_size_region(fd);
180     if (ashmemSize < 0) {
181         // Some other error, throw exception
182         jniThrowIOException(env, errno);
183         return -1;
184     }
185     return ashmemSize / sizeof(std::atomic_int);
186 }
187 
188 static const JNINativeMethod methods[] = {
189     {"nativeCreate",  "(Ljava/lang/String;I)I", (void*)android_util_MemoryIntArray_create},
190     {"nativeOpen",  "(IZ)J", (void*)android_util_MemoryIntArray_open},
191     {"nativeClose", "(IJZ)V", (void*)android_util_MemoryIntArray_close},
192     {"nativeGet",  "(IJI)I", (void*)android_util_MemoryIntArray_get},
193     {"nativeSet", "(IJII)V", (void*) android_util_MemoryIntArray_set},
194     {"nativeSize", "(I)I", (void*) android_util_MemoryIntArray_size},
195 };
196 
register_android_util_MemoryIntArray(JNIEnv * env)197 int register_android_util_MemoryIntArray(JNIEnv* env)
198 {
199     return RegisterMethodsOrDie(env, "android/util/MemoryIntArray", methods, NELEM(methods));
200 }
201 
202 }
203