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#include <nativehelper/JNIHelp.h>
24#include <nativehelper/JniConstants.h>
25#include <nativehelper/ScopedLocalRef.h>
26
27#include <algorithm>
28#include <errno.h>
29#include <limits>
30#include <unistd.h>
31
32namespace {
33
34static void throwErrnoException(JNIEnv* env, const char* functionName, int error) {
35    static jmethodID ctor = env->GetMethodID(JniConstants::errnoExceptionClass,
36            "<init>", "(Ljava/lang/String;I)V");
37
38    ScopedLocalRef<jstring> detailMessage(env, env->NewStringUTF(functionName));
39    if (detailMessage.get() == NULL) {
40        // Not really much we can do here. We're probably dead in the water,
41        // but let's try to stumble on...
42        env->ExceptionClear();
43    }
44
45    jobject exception = env->NewObject(JniConstants::errnoExceptionClass, ctor,
46            detailMessage.get(), error);
47    env->Throw(reinterpret_cast<jthrowable>(exception));
48}
49
50static jobject SharedMemory_create(JNIEnv* env, jobject, jstring jname, jint size) {
51
52    // Name is optional so we can't use ScopedUtfChars for this as it throws NPE on null
53    const char* name = jname ? env->GetStringUTFChars(jname, nullptr) : nullptr;
54
55    int fd = ashmem_create_region(name, size);
56
57    // Capture the error, if there is one, before calling ReleaseStringUTFChars
58    int err = fd < 0 ? errno : 0;
59
60    if (name) {
61        env->ReleaseStringUTFChars(jname, name);
62    }
63
64    if (fd < 0) {
65        throwErrnoException(env, "SharedMemory_create", err);
66        return nullptr;
67    }
68
69    return jniCreateFileDescriptor(env, fd);
70}
71
72static jint SharedMemory_getSize(JNIEnv* env, jobject, jobject fileDescriptor) {
73    int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
74    if (!ashmem_valid(fd)) {
75        return -1;
76    }
77    size_t size = ashmem_get_size_region(fd);
78    return static_cast<jint>(std::min(size, static_cast<size_t>(std::numeric_limits<jint>::max())));
79}
80
81static jint SharedMemory_setProt(JNIEnv* env, jobject, jobject fileDescriptor, jint prot) {
82    int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
83    int err = 0;
84    if (ashmem_set_prot_region(fd, prot)) {
85        err = errno;
86    }
87    return err;
88}
89
90static const JNINativeMethod methods[] = {
91    {"nCreate", "(Ljava/lang/String;I)Ljava/io/FileDescriptor;", (void*)SharedMemory_create},
92    {"nGetSize", "(Ljava/io/FileDescriptor;)I", (void*)SharedMemory_getSize},
93    {"nSetProt", "(Ljava/io/FileDescriptor;I)I", (void*)SharedMemory_setProt},
94};
95
96} // anonymous namespace
97
98namespace android {
99
100int register_android_os_SharedMemory(JNIEnv* env)
101{
102    return RegisterMethodsOrDie(env, "android/os/SharedMemory", methods, NELEM(methods));
103}
104
105} // namespace android
106