1 /*
2 * Copyright (C) 2017 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 "SharedMemory"
18
19 #include "core_jni_helpers.h"
20
21 #include <cutils/ashmem.h>
22 #include <utils/Log.h>
23
24 #include <nativehelper/jni_macros.h>
25 #include <nativehelper/JNIPlatformHelp.h>
26 #include <nativehelper/ScopedLocalRef.h>
27
28 #include <algorithm>
29 #include <errno.h>
30 #include <limits>
31 #include <unistd.h>
32
33 namespace {
34
35 jclass errnoExceptionClass;
36 jmethodID errnoExceptionCtor; // MethodID for ErrnoException.<init>(String,I)
37
SharedMemory_nCreate(JNIEnv * env,jobject,jstring jname,jint size)38 jobject SharedMemory_nCreate(JNIEnv* env, jobject, jstring jname, jint size) {
39
40 // Name is optional so we can't use ScopedUtfChars for this as it throws NPE on null
41 const char* name = jname ? env->GetStringUTFChars(jname, nullptr) : nullptr;
42
43 int fd = ashmem_create_region(name, size);
44
45 // Capture the error, if there is one, before calling ReleaseStringUTFChars
46 int err = fd < 0 ? errno : 0;
47
48 if (name) {
49 env->ReleaseStringUTFChars(jname, name);
50 }
51
52 if (fd < 0) {
53 jniThrowErrnoException(env, "SharedMemory_create", err);
54 return nullptr;
55 }
56
57 jobject jifd = jniCreateFileDescriptor(env, fd);
58 if (jifd == nullptr) {
59 close(fd);
60 }
61 return jifd;
62 }
63
SharedMemory_nGetSize(JNIEnv * env,jobject,jobject fileDescriptor)64 jint SharedMemory_nGetSize(JNIEnv* env, jobject, jobject fileDescriptor) {
65 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
66 if (!ashmem_valid(fd)) {
67 return -1;
68 }
69 size_t size = ashmem_get_size_region(fd);
70 return static_cast<jint>(std::min(size, static_cast<size_t>(std::numeric_limits<jint>::max())));
71 }
72
SharedMemory_nSetProt(JNIEnv * env,jobject,jobject fileDescriptor,jint prot)73 jint SharedMemory_nSetProt(JNIEnv* env, jobject, jobject fileDescriptor, jint prot) {
74 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
75 int err = 0;
76 if (ashmem_set_prot_region(fd, prot)) {
77 err = errno;
78 }
79 return err;
80 }
81
82 const JNINativeMethod methods[] = {
83 NATIVE_METHOD(SharedMemory, nCreate, "(Ljava/lang/String;I)Ljava/io/FileDescriptor;"),
84 NATIVE_METHOD(SharedMemory, nGetSize, "(Ljava/io/FileDescriptor;)I"),
85 NATIVE_METHOD(SharedMemory, nSetProt, "(Ljava/io/FileDescriptor;I)I")
86 };
87
88 } // anonymous namespace
89
90 namespace android {
91
register_android_os_SharedMemory(JNIEnv * env)92 int register_android_os_SharedMemory(JNIEnv* env) {
93 errnoExceptionClass =
94 MakeGlobalRefOrDie(env, FindClassOrDie(env, "android/system/ErrnoException"));
95 errnoExceptionCtor =
96 GetMethodIDOrDie(env, errnoExceptionClass, "<init>", "(Ljava/lang/String;I)V");
97 return RegisterMethodsOrDie(env, "android/os/SharedMemory", methods, NELEM(methods));
98 }
99
100 } // namespace android
101