blob: b68f9eca70cde6484bde5cd50376bc3137426f06 [file] [log] [blame]
Svet Ganov53a441c2016-04-19 19:38:00 -07001/*
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
Svet Ganov53a441c2016-04-19 19:38:00 -070017#include "core_jni_helpers.h"
18#include <cutils/ashmem.h>
Svetoslav Ganov04df7382016-05-10 18:55:47 -070019#include <linux/ashmem.h>
Svet Ganov53a441c2016-04-19 19:38:00 -070020#include <sys/mman.h>
21
22namespace android {
23
24static 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
Svet Ganov53a441c2016-04-19 19:38:00 -070047 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
56static jlong android_util_MemoryIntArray_open(JNIEnv* env, jobject clazz, jint fd,
Svetoslav Ganov74c99832016-12-05 20:07:20 -080057 jboolean owner)
Svet Ganov53a441c2016-04-19 19:38:00 -070058{
59 if (fd < 0) {
60 jniThrowException(env, "java/io/IOException", "bad file descriptor");
61 return -1;
62 }
63
Svet Ganov9d723d32016-08-27 11:05:56 -070064 if (!ashmem_valid(fd)) {
65 jniThrowIOException(env, errno);
66 return -1;
67 }
68
Svet Ganov53a441c2016-04-19 19:38:00 -070069 int ashmemSize = ashmem_get_size_region(fd);
70 if (ashmemSize <= 0) {
71 jniThrowException(env, "java/io/IOException", "bad ashmem size");
72 return -1;
73 }
74
Svetoslav Ganov74c99832016-12-05 20:07:20 -080075 // IMPORTANT: Ashmem allows the caller to change its size until
76 // it is memory mapped for the first time which lazily creates
77 // the underlying VFS file. So the size we get above may not
78 // reflect the size of the underlying shared memory region. Therefore,
79 // we first memory map to set the size in stone an verify if
80 // the underlying ashmem region has the same size as the one we
81 // memory mapped. This is critical as we use the underlying
82 // ashmem size for boundary checks and memory unmapping.
83 int protMode = owner ? (PROT_READ | PROT_WRITE) : PROT_READ;
Svet Ganov53a441c2016-04-19 19:38:00 -070084 void* ashmemAddr = mmap(NULL, ashmemSize, protMode, MAP_SHARED, fd, 0);
85 if (ashmemAddr == MAP_FAILED) {
86 jniThrowException(env, "java/io/IOException", "cannot mmap ashmem");
87 return -1;
88 }
89
Svetoslav Ganov74c99832016-12-05 20:07:20 -080090 // Check if the mapped size is the same as the ashmem region.
91 int mmapedSize = ashmem_get_size_region(fd);
92 if (mmapedSize != ashmemSize) {
93 munmap(reinterpret_cast<void *>(ashmemAddr), ashmemSize);
94 jniThrowException(env, "java/io/IOException", "bad file descriptor");
95 return -1;
96 }
97
Svet Ganov53a441c2016-04-19 19:38:00 -070098 if (owner) {
99 int size = ashmemSize / sizeof(std::atomic_int);
100 new (ashmemAddr) std::atomic_int[size];
101 }
102
Svetoslav Ganov74c99832016-12-05 20:07:20 -0800103 if (owner) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700104 int setProtResult = ashmem_set_prot_region(fd, PROT_READ);
105 if (setProtResult < 0) {
106 jniThrowException(env, "java/io/IOException", "cannot set ashmem prot mode");
107 return -1;
108 }
109 }
110
111 return reinterpret_cast<jlong>(ashmemAddr);
112}
113
114static void android_util_MemoryIntArray_close(JNIEnv* env, jobject clazz, jint fd,
115 jlong ashmemAddr, jboolean owner)
116{
117 if (fd < 0) {
118 jniThrowException(env, "java/io/IOException", "bad file descriptor");
119 return;
120 }
121
Svet Ganov9d723d32016-08-27 11:05:56 -0700122 if (!ashmem_valid(fd)) {
123 jniThrowIOException(env, errno);
124 return;
125 }
126
Svet Ganov53a441c2016-04-19 19:38:00 -0700127 int ashmemSize = ashmem_get_size_region(fd);
128 if (ashmemSize <= 0) {
129 jniThrowException(env, "java/io/IOException", "bad ashmem size");
130 return;
131 }
132
133 int unmapResult = munmap(reinterpret_cast<void *>(ashmemAddr), ashmemSize);
134 if (unmapResult < 0) {
135 jniThrowException(env, "java/io/IOException", "munmap failed");
136 return;
137 }
138
139 // We don't deallocate the atomic ints we created with placement new in the ashmem
140 // region as the kernel we reclaim all pages when the ashmem region is destroyed.
141 if (owner && (ashmem_unpin_region(fd, 0, 0) != ASHMEM_IS_UNPINNED)) {
142 jniThrowException(env, "java/io/IOException", "ashmem unpinning failed");
143 return;
144 }
Svet Ganov53a441c2016-04-19 19:38:00 -0700145}
146
147static jint android_util_MemoryIntArray_get(JNIEnv* env, jobject clazz,
Svetoslav Ganov74c99832016-12-05 20:07:20 -0800148 jint fd, jlong address, jint index)
Svet Ganov53a441c2016-04-19 19:38:00 -0700149{
150 if (fd < 0) {
151 jniThrowException(env, "java/io/IOException", "bad file descriptor");
152 return -1;
153 }
154
Svet Ganov9d723d32016-08-27 11:05:56 -0700155 if (!ashmem_valid(fd)) {
156 jniThrowIOException(env, errno);
157 return -1;
158 }
159
Svetoslav Ganov04df7382016-05-10 18:55:47 -0700160 if (ashmem_pin_region(fd, 0, 0) == ASHMEM_WAS_PURGED) {
161 jniThrowException(env, "java/io/IOException", "ashmem region was purged");
162 return -1;
Svet Ganov53a441c2016-04-19 19:38:00 -0700163 }
164
165 std::atomic_int* value = reinterpret_cast<std::atomic_int*>(address) + index;
Svetoslav Ganov04df7382016-05-10 18:55:47 -0700166 return value->load(std::memory_order_relaxed);
Svet Ganov53a441c2016-04-19 19:38:00 -0700167}
168
169static void android_util_MemoryIntArray_set(JNIEnv* env, jobject clazz,
Svetoslav Ganov74c99832016-12-05 20:07:20 -0800170 jint fd, jlong address, jint index, jint newValue)
Svet Ganov53a441c2016-04-19 19:38:00 -0700171{
172 if (fd < 0) {
173 jniThrowException(env, "java/io/IOException", "bad file descriptor");
174 return;
175 }
176
Svet Ganov9d723d32016-08-27 11:05:56 -0700177 if (!ashmem_valid(fd)) {
178 jniThrowIOException(env, errno);
179 return;
180 }
181
Svetoslav Ganov04df7382016-05-10 18:55:47 -0700182 if (ashmem_pin_region(fd, 0, 0) == ASHMEM_WAS_PURGED) {
183 jniThrowException(env, "java/io/IOException", "ashmem region was purged");
184 return;
Svet Ganov53a441c2016-04-19 19:38:00 -0700185 }
186
187 std::atomic_int* value = reinterpret_cast<std::atomic_int*>(address) + index;
188 value->store(newValue, std::memory_order_relaxed);
Svet Ganov53a441c2016-04-19 19:38:00 -0700189}
190
191static jint android_util_MemoryIntArray_size(JNIEnv* env, jobject clazz, jint fd) {
192 if (fd < 0) {
193 jniThrowException(env, "java/io/IOException", "bad file descriptor");
194 return -1;
195 }
196
Svet Ganov9d723d32016-08-27 11:05:56 -0700197 if (!ashmem_valid(fd)) {
198 jniThrowIOException(env, errno);
199 return -1;
200 }
201
Svet Ganov53a441c2016-04-19 19:38:00 -0700202 int ashmemSize = ashmem_get_size_region(fd);
203 if (ashmemSize < 0) {
Svet Ganov53a441c2016-04-19 19:38:00 -0700204 jniThrowIOException(env, errno);
205 return -1;
206 }
207 return ashmemSize / sizeof(std::atomic_int);
208}
209
210static const JNINativeMethod methods[] = {
211 {"nativeCreate", "(Ljava/lang/String;I)I", (void*)android_util_MemoryIntArray_create},
Svetoslav Ganov74c99832016-12-05 20:07:20 -0800212 {"nativeOpen", "(IZ)J", (void*)android_util_MemoryIntArray_open},
Svet Ganov53a441c2016-04-19 19:38:00 -0700213 {"nativeClose", "(IJZ)V", (void*)android_util_MemoryIntArray_close},
Svetoslav Ganov74c99832016-12-05 20:07:20 -0800214 {"nativeGet", "(IJI)I", (void*)android_util_MemoryIntArray_get},
215 {"nativeSet", "(IJII)V", (void*) android_util_MemoryIntArray_set},
Svet Ganov53a441c2016-04-19 19:38:00 -0700216 {"nativeSize", "(I)I", (void*) android_util_MemoryIntArray_size},
217};
218
219int register_android_util_MemoryIntArray(JNIEnv* env)
220{
221 return RegisterMethodsOrDie(env, "android/util/MemoryIntArray", methods, NELEM(methods));
222}
223
224}