1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #define LOG_TAG "netjniutils"
16
17 #include "netjniutils/netjniutils.h"
18
19 #include <dlfcn.h>
20 #include <stdbool.h>
21 #include <string.h>
22 #include <sys/system_properties.h>
23
24 #include <android/api-level.h>
25 #include <android/log.h>
26
27 namespace android {
28 namespace netjniutils {
29
30 namespace {
31
IsAtLeastS()32 bool IsAtLeastS() {
33 // TODO(b/158749603#comment19): move to android::modules::sdklevel::IsAtLeastS().
34 int api_level = android_get_device_api_level();
35
36 // Guarded check for branches that do not have __ANDROID_API_S__.
37 #ifdef __ANDROID_API_S__
38 if (api_level >= __ANDROID_API_S__) {
39 return true;
40 }
41 #endif
42
43 if (api_level < __ANDROID_API_R__) {
44 return false;
45 }
46
47 // Device looks like R or above, check codename as it could be (S or above).
48 static constexpr const char* kCodenameProperty = "ro.build.version.codename";
49 char codename[PROP_VALUE_MAX] = { 0 };
50 return (__system_property_get(kCodenameProperty, codename) > 0 &&
51 codename[0] >= 'S' && codename[1] == '\0');
52 }
53
GetNativeFileDescriptorWithoutNdk(JNIEnv * env,jobject javaFd)54 int GetNativeFileDescriptorWithoutNdk(JNIEnv* env, jobject javaFd) {
55 // Prior to Android S, we need to find the descriptor field in the FileDescriptor class. The
56 // symbol name has been stable in libcore, but is a private implementation detail.
57 // Older libnativehelper_compat_c++ versions had a jniGetFdFromFileDescriptor method, but this
58 // was removed in S to replace it with the NDK API in libnativehelper.
59 // The code is copied here instead. This code can be removed once R is not supported anymore.
60 static const jfieldID descriptorFieldID = [env]() -> jfieldID {
61 jclass cls = env->FindClass("java/io/FileDescriptor");
62 jfieldID fieldID = env->GetFieldID(cls, "descriptor", "I");
63 env->DeleteLocalRef(cls);
64 if (fieldID == nullptr) {
65 __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "Failed to get descriptor field.");
66 }
67 return fieldID;
68 }();
69
70 return javaFd != nullptr ? env->GetIntField(javaFd, descriptorFieldID) : -1;
71 }
72
GetNativeFileDescriptorWithNdk(JNIEnv * env,jobject javaFd)73 int GetNativeFileDescriptorWithNdk(JNIEnv* env, jobject javaFd) {
74 // Since Android S, there is an NDK API to get a file descriptor present in libnativehelper.so.
75 // libnativehelper is loaded into all processes by the zygote since the zygote uses it
76 // to load the Android Runtime and is also a public library (because of the NDK API).
77 typedef int (*ndkGetFd_t)(JNIEnv*, jobject);
78 static const ndkGetFd_t ndkGetFd = []() -> ndkGetFd_t {
79 void* handle = dlopen("libnativehelper.so", RTLD_NOLOAD | RTLD_NODELETE);
80 auto ndkGetFd = reinterpret_cast<ndkGetFd_t>(dlsym(handle, "AFileDescriptor_getFd"));
81 if (ndkGetFd == nullptr) {
82 __android_log_print(ANDROID_LOG_FATAL, LOG_TAG,
83 "Failed to dlsym(AFileDescriptor_getFd): %s", dlerror());
84 dlclose(handle);
85 }
86 return ndkGetFd;
87 }();
88
89 return javaFd != nullptr ? ndkGetFd(env, javaFd) : -1;
90 }
91
92 } // namespace
93
GetNativeFileDescriptor(JNIEnv * env,jobject javaFd)94 int GetNativeFileDescriptor(JNIEnv* env, jobject javaFd) {
95 static const bool preferNdkFileDescriptorApi = []() -> bool { return IsAtLeastS(); }();
96 if (preferNdkFileDescriptorApi) {
97 return GetNativeFileDescriptorWithNdk(env, javaFd);
98 } else {
99 return GetNativeFileDescriptorWithoutNdk(env, javaFd);
100 }
101 }
102
103 } // namespace netjniutils
104 } // namespace android
105