Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 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 ATRACE_TAG ATRACE_TAG_RESOURCES |
| 18 | |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 19 | #include "android-base/logging.h" |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 20 | #include "android-base/macros.h" |
| 21 | #include "android-base/stringprintf.h" |
| 22 | #include "android-base/unique_fd.h" |
| 23 | #include "androidfw/ApkAssets.h" |
| 24 | #include "utils/misc.h" |
| 25 | #include "utils/Trace.h" |
| 26 | |
| 27 | #include "core_jni_helpers.h" |
| 28 | #include "jni.h" |
| 29 | #include "nativehelper/ScopedUtfChars.h" |
| 30 | |
| 31 | using ::android::base::unique_fd; |
| 32 | |
| 33 | namespace android { |
| 34 | |
| 35 | static jlong NativeLoad(JNIEnv* env, jclass /*clazz*/, jstring java_path, jboolean system, |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 36 | jboolean force_shared_lib, jboolean overlay, jboolean for_loader) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 37 | ScopedUtfChars path(env, java_path); |
| 38 | if (path.c_str() == nullptr) { |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | ATRACE_NAME(base::StringPrintf("LoadApkAssets(%s)", path.c_str()).c_str()); |
| 43 | |
| 44 | std::unique_ptr<const ApkAssets> apk_assets; |
| 45 | if (overlay) { |
| 46 | apk_assets = ApkAssets::LoadOverlay(path.c_str(), system); |
| 47 | } else if (force_shared_lib) { |
| 48 | apk_assets = ApkAssets::LoadAsSharedLibrary(path.c_str(), system); |
| 49 | } else { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 50 | apk_assets = ApkAssets::Load(path.c_str(), system, for_loader); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | if (apk_assets == nullptr) { |
| 54 | std::string error_msg = base::StringPrintf("Failed to load asset path %s", path.c_str()); |
| 55 | jniThrowException(env, "java/io/IOException", error_msg.c_str()); |
| 56 | return 0; |
| 57 | } |
| 58 | return reinterpret_cast<jlong>(apk_assets.release()); |
| 59 | } |
| 60 | |
| 61 | static jlong NativeLoadFromFd(JNIEnv* env, jclass /*clazz*/, jobject file_descriptor, |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 62 | jstring friendly_name, jboolean system, jboolean force_shared_lib, |
| 63 | jboolean for_loader) { |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 64 | ScopedUtfChars friendly_name_utf8(env, friendly_name); |
| 65 | if (friendly_name_utf8.c_str() == nullptr) { |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | ATRACE_NAME(base::StringPrintf("LoadApkAssetsFd(%s)", friendly_name_utf8.c_str()).c_str()); |
| 70 | |
| 71 | int fd = jniGetFDFromFileDescriptor(env, file_descriptor); |
| 72 | if (fd < 0) { |
| 73 | jniThrowException(env, "java/lang/IllegalArgumentException", "Bad FileDescriptor"); |
| 74 | return 0; |
| 75 | } |
| 76 | |
Nick Kralevich | 4b3a08c | 2019-01-28 10:39:10 -0800 | [diff] [blame] | 77 | unique_fd dup_fd(::fcntl(fd, F_DUPFD_CLOEXEC, 0)); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 78 | if (dup_fd < 0) { |
| 79 | jniThrowIOException(env, errno); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | std::unique_ptr<const ApkAssets> apk_assets = ApkAssets::LoadFromFd(std::move(dup_fd), |
| 84 | friendly_name_utf8.c_str(), |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 85 | system, force_shared_lib, |
| 86 | for_loader); |
| 87 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 88 | if (apk_assets == nullptr) { |
| 89 | std::string error_msg = base::StringPrintf("Failed to load asset path %s from fd %d", |
| 90 | friendly_name_utf8.c_str(), dup_fd.get()); |
| 91 | jniThrowException(env, "java/io/IOException", error_msg.c_str()); |
| 92 | return 0; |
| 93 | } |
| 94 | return reinterpret_cast<jlong>(apk_assets.release()); |
| 95 | } |
| 96 | |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 97 | static jlong NativeLoadArsc(JNIEnv* env, jclass /*clazz*/, jstring java_path, |
| 98 | jboolean for_loader) { |
| 99 | ScopedUtfChars path(env, java_path); |
| 100 | if (path.c_str() == nullptr) { |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | ATRACE_NAME(base::StringPrintf("LoadApkAssetsArsc(%s)", path.c_str()).c_str()); |
| 105 | |
| 106 | std::unique_ptr<const ApkAssets> apk_assets = ApkAssets::LoadArsc(path.c_str(), for_loader); |
| 107 | |
| 108 | if (apk_assets == nullptr) { |
| 109 | std::string error_msg = base::StringPrintf("Failed to load asset path %s", path.c_str()); |
| 110 | jniThrowException(env, "java/io/IOException", error_msg.c_str()); |
| 111 | return 0; |
| 112 | } |
| 113 | return reinterpret_cast<jlong>(apk_assets.release()); |
| 114 | } |
| 115 | |
| 116 | static jlong NativeLoadArscFromFd(JNIEnv* env, jclass /*clazz*/, jobject file_descriptor, |
| 117 | jstring friendly_name, jboolean for_loader) { |
| 118 | ScopedUtfChars friendly_name_utf8(env, friendly_name); |
| 119 | if (friendly_name_utf8.c_str() == nullptr) { |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | int fd = jniGetFDFromFileDescriptor(env, file_descriptor); |
| 124 | ATRACE_NAME(base::StringPrintf("LoadApkAssetsArscFd(%d)", fd).c_str()); |
| 125 | if (fd < 0) { |
| 126 | jniThrowException(env, "java/lang/IllegalArgumentException", "Bad FileDescriptor"); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | unique_fd dup_fd(::fcntl(fd, F_DUPFD_CLOEXEC, 0)); |
| 131 | if (dup_fd < 0) { |
| 132 | jniThrowIOException(env, errno); |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | std::unique_ptr<const ApkAssets> apk_assets = |
| 137 | ApkAssets::LoadArsc(std::move(dup_fd), friendly_name_utf8.c_str(), for_loader); |
| 138 | if (apk_assets == nullptr) { |
| 139 | std::string error_msg = base::StringPrintf("Failed to load asset path from fd %d", fd); |
| 140 | jniThrowException(env, "java/io/IOException", error_msg.c_str()); |
| 141 | return 0; |
| 142 | } |
| 143 | return reinterpret_cast<jlong>(apk_assets.release()); |
| 144 | } |
| 145 | |
| 146 | static jlong NativeLoadEmpty(JNIEnv* env, jclass /*clazz*/, jboolean for_loader) { |
| 147 | std::unique_ptr<const ApkAssets> apk_assets = ApkAssets::LoadEmpty(for_loader); |
| 148 | return reinterpret_cast<jlong>(apk_assets.release()); |
| 149 | } |
| 150 | |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 151 | static void NativeDestroy(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) { |
| 152 | delete reinterpret_cast<ApkAssets*>(ptr); |
| 153 | } |
| 154 | |
| 155 | static jstring NativeGetAssetPath(JNIEnv* env, jclass /*clazz*/, jlong ptr) { |
| 156 | const ApkAssets* apk_assets = reinterpret_cast<const ApkAssets*>(ptr); |
| 157 | return env->NewStringUTF(apk_assets->GetPath().c_str()); |
| 158 | } |
| 159 | |
| 160 | static jlong NativeGetStringBlock(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) { |
| 161 | const ApkAssets* apk_assets = reinterpret_cast<const ApkAssets*>(ptr); |
| 162 | return reinterpret_cast<jlong>(apk_assets->GetLoadedArsc()->GetStringPool()); |
| 163 | } |
| 164 | |
| 165 | static jboolean NativeIsUpToDate(JNIEnv* /*env*/, jclass /*clazz*/, jlong ptr) { |
| 166 | const ApkAssets* apk_assets = reinterpret_cast<const ApkAssets*>(ptr); |
Winson | b0085ce | 2019-02-19 12:48:22 -0800 | [diff] [blame] | 167 | return apk_assets->IsUpToDate() ? JNI_TRUE : JNI_FALSE; |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static jlong NativeOpenXml(JNIEnv* env, jclass /*clazz*/, jlong ptr, jstring file_name) { |
| 171 | ScopedUtfChars path_utf8(env, file_name); |
| 172 | if (path_utf8.c_str() == nullptr) { |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | const ApkAssets* apk_assets = reinterpret_cast<const ApkAssets*>(ptr); |
| 177 | std::unique_ptr<Asset> asset = apk_assets->Open(path_utf8.c_str(), |
| 178 | Asset::AccessMode::ACCESS_RANDOM); |
| 179 | if (asset == nullptr) { |
| 180 | jniThrowException(env, "java/io/FileNotFoundException", path_utf8.c_str()); |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | // DynamicRefTable is only needed when looking up resource references. Opening an XML file |
| 185 | // directly from an ApkAssets has no notion of proper resource references. |
| 186 | std::unique_ptr<ResXMLTree> xml_tree = util::make_unique<ResXMLTree>(nullptr /*dynamicRefTable*/); |
| 187 | status_t err = xml_tree->setTo(asset->getBuffer(true), asset->getLength(), true); |
| 188 | asset.reset(); |
| 189 | |
| 190 | if (err != NO_ERROR) { |
| 191 | jniThrowException(env, "java/io/FileNotFoundException", "Corrupt XML binary file"); |
| 192 | return 0; |
| 193 | } |
| 194 | return reinterpret_cast<jlong>(xml_tree.release()); |
| 195 | } |
| 196 | |
| 197 | // JNI registration. |
| 198 | static const JNINativeMethod gApkAssetsMethods[] = { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 199 | {"nativeLoad", "(Ljava/lang/String;ZZZZ)J", (void*)NativeLoad}, |
| 200 | {"nativeLoadFromFd", "(Ljava/io/FileDescriptor;Ljava/lang/String;ZZZ)J", |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 201 | (void*)NativeLoadFromFd}, |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 202 | {"nativeLoadArsc", "(Ljava/lang/String;Z)J", (void*)NativeLoadArsc}, |
| 203 | {"nativeLoadArscFromFd", "(Ljava/io/FileDescriptor;Ljava/lang/String;Z)J", |
| 204 | (void*)NativeLoadArscFromFd}, |
| 205 | {"nativeLoadEmpty", "(Z)J", (void*)NativeLoadEmpty}, |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 206 | {"nativeDestroy", "(J)V", (void*)NativeDestroy}, |
| 207 | {"nativeGetAssetPath", "(J)Ljava/lang/String;", (void*)NativeGetAssetPath}, |
| 208 | {"nativeGetStringBlock", "(J)J", (void*)NativeGetStringBlock}, |
| 209 | {"nativeIsUpToDate", "(J)Z", (void*)NativeIsUpToDate}, |
| 210 | {"nativeOpenXml", "(JLjava/lang/String;)J", (void*)NativeOpenXml}, |
| 211 | }; |
| 212 | |
| 213 | int register_android_content_res_ApkAssets(JNIEnv* env) { |
| 214 | return RegisterMethodsOrDie(env, "android/content/res/ApkAssets", gApkAssetsMethods, |
| 215 | arraysize(gApkAssetsMethods)); |
| 216 | } |
| 217 | |
| 218 | } // namespace android |