| 1 | /* |
| 2 | * Copyright (C) 2011 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 "NativeLibraryHelper" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include "core_jni_helpers.h" |
| 21 | |
| 22 | #include <nativehelper/ScopedUtfChars.h> |
| 23 | #include <androidfw/ZipFileRO.h> |
| 24 | #include <androidfw/ZipUtils.h> |
| 25 | #include <utils/Log.h> |
| 26 | #include <utils/Vector.h> |
| 27 | |
| 28 | #include <zlib.h> |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
| 36 | #include <inttypes.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <linux/fs.h> |
| 40 | |
| 41 | #include <memory> |
| 42 | |
| 43 | #define APK_LIB "lib/" |
| 44 | #define APK_LIB_LEN (sizeof(APK_LIB) - 1) |
| 45 | |
| 46 | #define LIB_PREFIX "/lib" |
| 47 | #define LIB_PREFIX_LEN (sizeof(LIB_PREFIX) - 1) |
| 48 | |
| 49 | #define LIB_SUFFIX ".so" |
| 50 | #define LIB_SUFFIX_LEN (sizeof(LIB_SUFFIX) - 1) |
| 51 | |
| 52 | #define RS_BITCODE_SUFFIX ".bc" |
| 53 | |
| 54 | #define TMP_FILE_PATTERN "/tmp.XXXXXX" |
| 55 | #define TMP_FILE_PATTERN_LEN (sizeof(TMP_FILE_PATTERN) - 1) |
| 56 | |
| 57 | namespace android { |
| 58 | |
| 59 | // These match PackageManager.java install codes |
| 60 | enum install_status_t { |
| 61 | INSTALL_SUCCEEDED = 1, |
| 62 | INSTALL_FAILED_INVALID_APK = -2, |
| 63 | INSTALL_FAILED_INSUFFICIENT_STORAGE = -4, |
| 64 | INSTALL_FAILED_CONTAINER_ERROR = -18, |
| 65 | INSTALL_FAILED_INTERNAL_ERROR = -110, |
| 66 | INSTALL_FAILED_NO_MATCHING_ABIS = -113, |
| 67 | NO_NATIVE_LIBRARIES = -114 |
| 68 | }; |
| 69 | |
| 70 | typedef install_status_t (*iterFunc)(JNIEnv*, void*, ZipFileRO*, ZipEntryRO, const char*); |
| 71 | |
| 72 | // Equivalent to android.os.FileUtils.isFilenameSafe |
| 73 | static bool |
| 74 | isFilenameSafe(const char* filename) |
| 75 | { |
| 76 | off_t offset = 0; |
| 77 | for (;;) { |
| 78 | switch (*(filename + offset)) { |
| 79 | case 0: |
| 80 | // Null. |
| 81 | // If we've reached the end, all the other characters are good. |
| 82 | return true; |
| 83 | |
| 84 | case 'A' ... 'Z': |
| 85 | case 'a' ... 'z': |
| 86 | case '0' ... '9': |
| 87 | case '+': |
| 88 | case ',': |
| 89 | case '-': |
| 90 | case '.': |
| 91 | case '/': |
| 92 | case '=': |
| 93 | case '_': |
| 94 | offset++; |
| 95 | break; |
| 96 | |
| 97 | default: |
| 98 | // We found something that is not good. |
| 99 | return false; |
| 100 | } |
| 101 | } |
| 102 | // Should not reach here. |
| 103 | } |
| 104 | |
| 105 | static bool |
| 106 | isFileDifferent(const char* filePath, uint32_t fileSize, time_t modifiedTime, |
| 107 | uint32_t zipCrc, struct stat64* st) |
| 108 | { |
| 109 | if (lstat64(filePath, st) < 0) { |
| 110 | // File is not found or cannot be read. |
| 111 | ALOGV("Couldn't stat %s, copying: %s\n", filePath, strerror(errno)); |
| 112 | return true; |
| 113 | } |
| 114 | |
| 115 | if (!S_ISREG(st->st_mode)) { |
| 116 | return true; |
| 117 | } |
| 118 | |
| 119 | if (static_cast<uint64_t>(st->st_size) != static_cast<uint64_t>(fileSize)) { |
| 120 | return true; |
| 121 | } |
| 122 | |
| 123 | // For some reason, bionic doesn't define st_mtime as time_t |
| 124 | if (time_t(st->st_mtime) != modifiedTime) { |
| 125 | ALOGV("mod time doesn't match: %ld vs. %ld\n", st->st_mtime, modifiedTime); |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | int fd = TEMP_FAILURE_RETRY(open(filePath, O_RDONLY | O_CLOEXEC)); |
| 130 | if (fd < 0) { |
| 131 | ALOGV("Couldn't open file %s: %s", filePath, strerror(errno)); |
| 132 | return true; |
| 133 | } |
| 134 | |
| 135 | // uLong comes from zlib.h. It's a bit of a wart that they're |
| 136 | // potentially using a 64-bit type for a 32-bit CRC. |
| 137 | uLong crc = crc32(0L, Z_NULL, 0); |
| 138 | unsigned char crcBuffer[16384]; |
| 139 | ssize_t numBytes; |
| 140 | while ((numBytes = TEMP_FAILURE_RETRY(read(fd, crcBuffer, sizeof(crcBuffer)))) > 0) { |
| 141 | crc = crc32(crc, crcBuffer, numBytes); |
| 142 | } |
| 143 | close(fd); |
| 144 | |
| 145 | ALOGV("%s: crc = %lx, zipCrc = %" PRIu32 "\n", filePath, crc, zipCrc); |
| 146 | |
| 147 | if (crc != static_cast<uLong>(zipCrc)) { |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | static install_status_t |
| 155 | sumFiles(JNIEnv*, void* arg, ZipFileRO* zipFile, ZipEntryRO zipEntry, const char*) |
| 156 | { |
| 157 | size_t* total = (size_t*) arg; |
| 158 | uint32_t uncompLen; |
| 159 | |
| 160 | if (!zipFile->getEntryInfo(zipEntry, NULL, &uncompLen, NULL, NULL, NULL, NULL)) { |
| 161 | return INSTALL_FAILED_INVALID_APK; |
| 162 | } |
| 163 | |
| 164 | *total += static_cast<size_t>(uncompLen); |
| 165 | |
| 166 | return INSTALL_SUCCEEDED; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Copy the native library if needed. |
| 171 | * |
| 172 | * This function assumes the library and path names passed in are considered safe. |
| 173 | */ |
| 174 | static install_status_t |
| 175 | copyFileIfChanged(JNIEnv *env, void* arg, ZipFileRO* zipFile, ZipEntryRO zipEntry, const char* fileName) |
| 176 | { |
| 177 | void** args = reinterpret_cast<void**>(arg); |
| 178 | jstring* javaNativeLibPath = (jstring*) args[0]; |
| 179 | jboolean extractNativeLibs = *(jboolean*) args[1]; |
| 180 | |
| 181 | ScopedUtfChars nativeLibPath(env, *javaNativeLibPath); |
| 182 | |
| 183 | uint32_t uncompLen; |
| 184 | uint32_t when; |
| 185 | uint32_t crc; |
| 186 | |
| 187 | uint16_t method; |
| 188 | off64_t offset; |
| 189 | |
| 190 | if (!zipFile->getEntryInfo(zipEntry, &method, &uncompLen, NULL, &offset, &when, &crc)) { |
| 191 | ALOGE("Couldn't read zip entry info\n"); |
| 192 | return INSTALL_FAILED_INVALID_APK; |
| 193 | } |
| 194 | |
| 195 | if (!extractNativeLibs) { |
| 196 | // check if library is uncompressed and page-aligned |
| 197 | if (method != ZipFileRO::kCompressStored) { |
| 198 | ALOGE("Library '%s' is compressed - will not be able to open it directly from apk.\n", |
| 199 | fileName); |
| 200 | return INSTALL_FAILED_INVALID_APK; |
| 201 | } |
| 202 | |
| 203 | if (offset % PAGE_SIZE != 0) { |
| 204 | ALOGE("Library '%s' is not page-aligned - will not be able to open it directly from" |
| 205 | " apk.\n", fileName); |
| 206 | return INSTALL_FAILED_INVALID_APK; |
| 207 | } |
| 208 | |
| 209 | return INSTALL_SUCCEEDED; |
| 210 | } |
| 211 | |
| 212 | // Build local file path |
| 213 | const size_t fileNameLen = strlen(fileName); |
| 214 | char localFileName[nativeLibPath.size() + fileNameLen + 2]; |
| 215 | |
| 216 | if (strlcpy(localFileName, nativeLibPath.c_str(), sizeof(localFileName)) != nativeLibPath.size()) { |
| 217 | ALOGE("Couldn't allocate local file name for library"); |
| 218 | return INSTALL_FAILED_INTERNAL_ERROR; |
| 219 | } |
| 220 | |
| 221 | *(localFileName + nativeLibPath.size()) = '/'; |
| 222 | |
| 223 | if (strlcpy(localFileName + nativeLibPath.size() + 1, fileName, sizeof(localFileName) |
| 224 | - nativeLibPath.size() - 1) != fileNameLen) { |
| 225 | ALOGE("Couldn't allocate local file name for library"); |
| 226 | return INSTALL_FAILED_INTERNAL_ERROR; |
| 227 | } |
| 228 | |
| 229 | // Only copy out the native file if it's different. |
| 230 | struct tm t; |
| 231 | ZipUtils::zipTimeToTimespec(when, &t); |
| 232 | const time_t modTime = mktime(&t); |
| 233 | struct stat64 st; |
| 234 | if (!isFileDifferent(localFileName, uncompLen, modTime, crc, &st)) { |
| 235 | return INSTALL_SUCCEEDED; |
| 236 | } |
| 237 | |
| 238 | char localTmpFileName[nativeLibPath.size() + TMP_FILE_PATTERN_LEN + 1]; |
| 239 | if (strlcpy(localTmpFileName, nativeLibPath.c_str(), sizeof(localTmpFileName)) |
| 240 | != nativeLibPath.size()) { |
| 241 | ALOGE("Couldn't allocate local file name for library"); |
| 242 | return INSTALL_FAILED_INTERNAL_ERROR; |
| 243 | } |
| 244 | |
| 245 | if (strlcpy(localTmpFileName + nativeLibPath.size(), TMP_FILE_PATTERN, |
| 246 | TMP_FILE_PATTERN_LEN + 1) != TMP_FILE_PATTERN_LEN) { |
| 247 | ALOGE("Couldn't allocate temporary file name for library"); |
| 248 | return INSTALL_FAILED_INTERNAL_ERROR; |
| 249 | } |
| 250 | |
| 251 | int fd = mkstemp(localTmpFileName); |
| 252 | if (fd < 0) { |
| 253 | ALOGE("Couldn't open temporary file name: %s: %s\n", localTmpFileName, strerror(errno)); |
| 254 | return INSTALL_FAILED_CONTAINER_ERROR; |
| 255 | } |
| 256 | |
| 257 | // If a filesystem like f2fs supports per-file compression, set the compression bit before data |
| 258 | // writes |
| 259 | unsigned int flags; |
| 260 | if (ioctl(fd, FS_IOC_GETFLAGS, &flags) == -1) { |
| 261 | ALOGE("Failed to call FS_IOC_GETFLAGS on %s: %s\n", localTmpFileName, strerror(errno)); |
| 262 | } else if ((flags & FS_COMPR_FL) == 0) { |
| 263 | flags |= FS_COMPR_FL; |
| 264 | ioctl(fd, FS_IOC_SETFLAGS, &flags); |
| 265 | } |
| 266 | |
| 267 | if (!zipFile->uncompressEntry(zipEntry, fd)) { |
| 268 | ALOGE("Failed uncompressing %s to %s\n", fileName, localTmpFileName); |
| 269 | close(fd); |
| 270 | unlink(localTmpFileName); |
| 271 | return INSTALL_FAILED_CONTAINER_ERROR; |
| 272 | } |
| 273 | |
| 274 | if (fsync(fd) < 0) { |
| 275 | ALOGE("Coulnd't fsync temporary file name: %s: %s\n", localTmpFileName, strerror(errno)); |
| 276 | close(fd); |
| 277 | unlink(localTmpFileName); |
| 278 | return INSTALL_FAILED_INTERNAL_ERROR; |
| 279 | } |
| 280 | |
| 281 | close(fd); |
| 282 | |
| 283 | // Set the modification time for this file to the ZIP's mod time. |
| 284 | struct timeval times[2]; |
| 285 | times[0].tv_sec = st.st_atime; |
| 286 | times[1].tv_sec = modTime; |
| 287 | times[0].tv_usec = times[1].tv_usec = 0; |
| 288 | if (utimes(localTmpFileName, times) < 0) { |
| 289 | ALOGE("Couldn't change modification time on %s: %s\n", localTmpFileName, strerror(errno)); |
| 290 | unlink(localTmpFileName); |
| 291 | return INSTALL_FAILED_CONTAINER_ERROR; |
| 292 | } |
| 293 | |
| 294 | // Set the mode to 755 |
| 295 | static const mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; |
| 296 | if (chmod(localTmpFileName, mode) < 0) { |
| 297 | ALOGE("Couldn't change permissions on %s: %s\n", localTmpFileName, strerror(errno)); |
| 298 | unlink(localTmpFileName); |
| 299 | return INSTALL_FAILED_CONTAINER_ERROR; |
| 300 | } |
| 301 | |
| 302 | // Finally, rename it to the final name. |
| 303 | if (rename(localTmpFileName, localFileName) < 0) { |
| 304 | ALOGE("Couldn't rename %s to %s: %s\n", localTmpFileName, localFileName, strerror(errno)); |
| 305 | unlink(localTmpFileName); |
| 306 | return INSTALL_FAILED_CONTAINER_ERROR; |
| 307 | } |
| 308 | |
| 309 | ALOGV("Successfully moved %s to %s\n", localTmpFileName, localFileName); |
| 310 | |
| 311 | return INSTALL_SUCCEEDED; |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * An iterator over all shared libraries in a zip file. An entry is |
| 316 | * considered to be a shared library if all of the conditions below are |
| 317 | * satisfied : |
| 318 | * |
| 319 | * - The entry is under the lib/ directory. |
| 320 | * - The entry name ends with ".so" and the entry name starts with "lib", |
| 321 | * an exception is made for entries whose name is "gdbserver". |
| 322 | * - The entry filename is "safe" (as determined by isFilenameSafe). |
| 323 | * |
| 324 | */ |
| 325 | class NativeLibrariesIterator { |
| 326 | private: |
| 327 | NativeLibrariesIterator(ZipFileRO* zipFile, bool debuggable, void* cookie) |
| 328 | : mZipFile(zipFile), mDebuggable(debuggable), mCookie(cookie), mLastSlash(NULL) { |
| 329 | fileName[0] = '\0'; |
| 330 | } |
| 331 | |
| 332 | public: |
| 333 | static NativeLibrariesIterator* create(ZipFileRO* zipFile, bool debuggable) { |
| 334 | void* cookie = NULL; |
| 335 | // Do not specify a suffix to find both .so files and gdbserver. |
| 336 | if (!zipFile->startIteration(&cookie, APK_LIB, NULL /* suffix */)) { |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | return new NativeLibrariesIterator(zipFile, debuggable, cookie); |
| 341 | } |
| 342 | |
| 343 | ZipEntryRO next() { |
| 344 | ZipEntryRO next = NULL; |
| 345 | while ((next = mZipFile->nextEntry(mCookie)) != NULL) { |
| 346 | // Make sure this entry has a filename. |
| 347 | if (mZipFile->getEntryFileName(next, fileName, sizeof(fileName))) { |
| 348 | continue; |
| 349 | } |
| 350 | |
| 351 | // Make sure the filename is at least to the minimum library name size. |
| 352 | const size_t fileNameLen = strlen(fileName); |
| 353 | static const size_t minLength = APK_LIB_LEN + 2 + LIB_PREFIX_LEN + 1 + LIB_SUFFIX_LEN; |
| 354 | if (fileNameLen < minLength) { |
| 355 | continue; |
| 356 | } |
| 357 | |
| 358 | const char* lastSlash = strrchr(fileName, '/'); |
| 359 | ALOG_ASSERT(lastSlash != NULL, "last slash was null somehow for %s\n", fileName); |
| 360 | |
| 361 | // Skip directories. |
| 362 | if (*(lastSlash + 1) == 0) { |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | // Make sure the filename is safe. |
| 367 | if (!isFilenameSafe(lastSlash + 1)) { |
| 368 | continue; |
| 369 | } |
| 370 | |
| 371 | if (!mDebuggable) { |
| 372 | // Make sure the filename starts with lib and ends with ".so". |
| 373 | if (strncmp(fileName + fileNameLen - LIB_SUFFIX_LEN, LIB_SUFFIX, LIB_SUFFIX_LEN) |
| 374 | || strncmp(lastSlash, LIB_PREFIX, LIB_PREFIX_LEN)) { |
| 375 | continue; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | mLastSlash = lastSlash; |
| 380 | break; |
| 381 | } |
| 382 | |
| 383 | return next; |
| 384 | } |
| 385 | |
| 386 | inline const char* currentEntry() const { |
| 387 | return fileName; |
| 388 | } |
| 389 | |
| 390 | inline const char* lastSlash() const { |
| 391 | return mLastSlash; |
| 392 | } |
| 393 | |
| 394 | virtual ~NativeLibrariesIterator() { |
| 395 | mZipFile->endIteration(mCookie); |
| 396 | } |
| 397 | private: |
| 398 | |
| 399 | char fileName[PATH_MAX]; |
| 400 | ZipFileRO* const mZipFile; |
| 401 | const bool mDebuggable; |
| 402 | void* mCookie; |
| 403 | const char* mLastSlash; |
| 404 | }; |
| 405 | |
| 406 | static install_status_t |
| 407 | iterateOverNativeFiles(JNIEnv *env, jlong apkHandle, jstring javaCpuAbi, |
| 408 | jboolean debuggable, iterFunc callFunc, void* callArg) { |
| 409 | ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle); |
| 410 | if (zipFile == NULL) { |
| 411 | return INSTALL_FAILED_INVALID_APK; |
| 412 | } |
| 413 | |
| 414 | std::unique_ptr<NativeLibrariesIterator> it( |
| 415 | NativeLibrariesIterator::create(zipFile, debuggable)); |
| 416 | if (it.get() == NULL) { |
| 417 | return INSTALL_FAILED_INVALID_APK; |
| 418 | } |
| 419 | |
| 420 | const ScopedUtfChars cpuAbi(env, javaCpuAbi); |
| 421 | if (cpuAbi.c_str() == NULL) { |
| 422 | // This would've thrown, so this return code isn't observable by |
| 423 | // Java. |
| 424 | return INSTALL_FAILED_INVALID_APK; |
| 425 | } |
| 426 | ZipEntryRO entry = NULL; |
| 427 | while ((entry = it->next()) != NULL) { |
| 428 | const char* fileName = it->currentEntry(); |
| 429 | const char* lastSlash = it->lastSlash(); |
| 430 | |
| 431 | // Check to make sure the CPU ABI of this file is one we support. |
| 432 | const char* cpuAbiOffset = fileName + APK_LIB_LEN; |
| 433 | const size_t cpuAbiRegionSize = lastSlash - cpuAbiOffset; |
| 434 | |
| 435 | if (cpuAbi.size() == cpuAbiRegionSize && !strncmp(cpuAbiOffset, cpuAbi.c_str(), cpuAbiRegionSize)) { |
| 436 | install_status_t ret = callFunc(env, callArg, zipFile, entry, lastSlash + 1); |
| 437 | |
| 438 | if (ret != INSTALL_SUCCEEDED) { |
| 439 | ALOGV("Failure for entry %s", lastSlash + 1); |
| 440 | return ret; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | return INSTALL_SUCCEEDED; |
| 446 | } |
| 447 | |
| 448 | |
| 449 | static int findSupportedAbi(JNIEnv *env, jlong apkHandle, jobjectArray supportedAbisArray, |
| 450 | jboolean debuggable) { |
| 451 | const int numAbis = env->GetArrayLength(supportedAbisArray); |
| 452 | Vector<ScopedUtfChars*> supportedAbis; |
| 453 | |
| 454 | for (int i = 0; i < numAbis; ++i) { |
| 455 | supportedAbis.add(new ScopedUtfChars(env, |
| 456 | (jstring) env->GetObjectArrayElement(supportedAbisArray, i))); |
| 457 | } |
| 458 | |
| 459 | ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle); |
| 460 | if (zipFile == NULL) { |
| 461 | return INSTALL_FAILED_INVALID_APK; |
| 462 | } |
| 463 | |
| 464 | std::unique_ptr<NativeLibrariesIterator> it( |
| 465 | NativeLibrariesIterator::create(zipFile, debuggable)); |
| 466 | if (it.get() == NULL) { |
| 467 | return INSTALL_FAILED_INVALID_APK; |
| 468 | } |
| 469 | |
| 470 | ZipEntryRO entry = NULL; |
| 471 | int status = NO_NATIVE_LIBRARIES; |
| 472 | while ((entry = it->next()) != NULL) { |
| 473 | // We're currently in the lib/ directory of the APK, so it does have some native |
| 474 | // code. We should return INSTALL_FAILED_NO_MATCHING_ABIS if none of the |
| 475 | // libraries match. |
| 476 | if (status == NO_NATIVE_LIBRARIES) { |
| 477 | status = INSTALL_FAILED_NO_MATCHING_ABIS; |
| 478 | } |
| 479 | |
| 480 | const char* fileName = it->currentEntry(); |
| 481 | const char* lastSlash = it->lastSlash(); |
| 482 | |
| 483 | // Check to see if this CPU ABI matches what we are looking for. |
| 484 | const char* abiOffset = fileName + APK_LIB_LEN; |
| 485 | const size_t abiSize = lastSlash - abiOffset; |
| 486 | for (int i = 0; i < numAbis; i++) { |
| 487 | const ScopedUtfChars* abi = supportedAbis[i]; |
| 488 | if (abi->size() == abiSize && !strncmp(abiOffset, abi->c_str(), abiSize)) { |
| 489 | // The entry that comes in first (i.e. with a lower index) has the higher priority. |
| 490 | if (((i < status) && (status >= 0)) || (status < 0) ) { |
| 491 | status = i; |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | for (int i = 0; i < numAbis; ++i) { |
| 498 | delete supportedAbis[i]; |
| 499 | } |
| 500 | |
| 501 | return status; |
| 502 | } |
| 503 | |
| 504 | static jint |
| 505 | com_android_internal_content_NativeLibraryHelper_copyNativeBinaries(JNIEnv *env, jclass clazz, |
| 506 | jlong apkHandle, jstring javaNativeLibPath, jstring javaCpuAbi, |
| 507 | jboolean extractNativeLibs, jboolean debuggable) |
| 508 | { |
| 509 | void* args[] = { &javaNativeLibPath, &extractNativeLibs }; |
| 510 | return (jint) iterateOverNativeFiles(env, apkHandle, javaCpuAbi, debuggable, |
| 511 | copyFileIfChanged, reinterpret_cast<void*>(args)); |
| 512 | } |
| 513 | |
| 514 | static jlong |
| 515 | com_android_internal_content_NativeLibraryHelper_sumNativeBinaries(JNIEnv *env, jclass clazz, |
| 516 | jlong apkHandle, jstring javaCpuAbi, jboolean debuggable) |
| 517 | { |
| 518 | size_t totalSize = 0; |
| 519 | |
| 520 | iterateOverNativeFiles(env, apkHandle, javaCpuAbi, debuggable, sumFiles, &totalSize); |
| 521 | |
| 522 | return totalSize; |
| 523 | } |
| 524 | |
| 525 | static jint |
| 526 | com_android_internal_content_NativeLibraryHelper_findSupportedAbi(JNIEnv *env, jclass clazz, |
| 527 | jlong apkHandle, jobjectArray javaCpuAbisToSearch, jboolean debuggable) |
| 528 | { |
| 529 | return (jint) findSupportedAbi(env, apkHandle, javaCpuAbisToSearch, debuggable); |
| 530 | } |
| 531 | |
| 532 | enum bitcode_scan_result_t { |
| 533 | APK_SCAN_ERROR = -1, |
| 534 | NO_BITCODE_PRESENT = 0, |
| 535 | BITCODE_PRESENT = 1, |
| 536 | }; |
| 537 | |
| 538 | static jint |
| 539 | com_android_internal_content_NativeLibraryHelper_hasRenderscriptBitcode(JNIEnv *env, jclass clazz, |
| 540 | jlong apkHandle) { |
| 541 | ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle); |
| 542 | void* cookie = NULL; |
| 543 | if (!zipFile->startIteration(&cookie, NULL /* prefix */, RS_BITCODE_SUFFIX)) { |
| 544 | return APK_SCAN_ERROR; |
| 545 | } |
| 546 | |
| 547 | char fileName[PATH_MAX]; |
| 548 | ZipEntryRO next = NULL; |
| 549 | while ((next = zipFile->nextEntry(cookie)) != NULL) { |
| 550 | if (zipFile->getEntryFileName(next, fileName, sizeof(fileName))) { |
| 551 | continue; |
| 552 | } |
| 553 | const char* lastSlash = strrchr(fileName, '/'); |
| 554 | const char* baseName = (lastSlash == NULL) ? fileName : fileName + 1; |
| 555 | if (isFilenameSafe(baseName)) { |
| 556 | zipFile->endIteration(cookie); |
| 557 | return BITCODE_PRESENT; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | zipFile->endIteration(cookie); |
| 562 | return NO_BITCODE_PRESENT; |
| 563 | } |
| 564 | |
| 565 | static jlong |
| 566 | com_android_internal_content_NativeLibraryHelper_openApk(JNIEnv *env, jclass, jstring apkPath) |
| 567 | { |
| 568 | ScopedUtfChars filePath(env, apkPath); |
| 569 | ZipFileRO* zipFile = ZipFileRO::open(filePath.c_str()); |
| 570 | |
| 571 | return reinterpret_cast<jlong>(zipFile); |
| 572 | } |
| 573 | |
| 574 | static jlong |
| 575 | com_android_internal_content_NativeLibraryHelper_openApkFd(JNIEnv *env, jclass, |
| 576 | jobject fileDescriptor, jstring debugPathName) |
| 577 | { |
| 578 | ScopedUtfChars debugFilePath(env, debugPathName); |
| 579 | |
| 580 | int fd = jniGetFDFromFileDescriptor(env, fileDescriptor); |
| 581 | if (fd < 0) { |
| 582 | jniThrowException(env, "java/lang/IllegalArgumentException", "Bad FileDescriptor"); |
| 583 | return 0; |
| 584 | } |
| 585 | |
| 586 | int dupedFd = fcntl(fd, F_DUPFD_CLOEXEC, 0); |
| 587 | if (dupedFd == -1) { |
| 588 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
| 589 | "Failed to dup FileDescriptor: %s", strerror(errno)); |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | ZipFileRO* zipFile = ZipFileRO::openFd(dupedFd, debugFilePath.c_str()); |
| 594 | |
| 595 | return reinterpret_cast<jlong>(zipFile); |
| 596 | } |
| 597 | |
| 598 | static void |
| 599 | com_android_internal_content_NativeLibraryHelper_close(JNIEnv *env, jclass, jlong apkHandle) |
| 600 | { |
| 601 | delete reinterpret_cast<ZipFileRO*>(apkHandle); |
| 602 | } |
| 603 | |
| 604 | static const JNINativeMethod gMethods[] = { |
| 605 | {"nativeOpenApk", |
| 606 | "(Ljava/lang/String;)J", |
| 607 | (void *)com_android_internal_content_NativeLibraryHelper_openApk}, |
| 608 | {"nativeOpenApkFd", |
| 609 | "(Ljava/io/FileDescriptor;Ljava/lang/String;)J", |
| 610 | (void *)com_android_internal_content_NativeLibraryHelper_openApkFd}, |
| 611 | {"nativeClose", |
| 612 | "(J)V", |
| 613 | (void *)com_android_internal_content_NativeLibraryHelper_close}, |
| 614 | {"nativeCopyNativeBinaries", |
| 615 | "(JLjava/lang/String;Ljava/lang/String;ZZ)I", |
| 616 | (void *)com_android_internal_content_NativeLibraryHelper_copyNativeBinaries}, |
| 617 | {"nativeSumNativeBinaries", |
| 618 | "(JLjava/lang/String;Z)J", |
| 619 | (void *)com_android_internal_content_NativeLibraryHelper_sumNativeBinaries}, |
| 620 | {"nativeFindSupportedAbi", |
| 621 | "(J[Ljava/lang/String;Z)I", |
| 622 | (void *)com_android_internal_content_NativeLibraryHelper_findSupportedAbi}, |
| 623 | {"hasRenderscriptBitcode", "(J)I", |
| 624 | (void *)com_android_internal_content_NativeLibraryHelper_hasRenderscriptBitcode}, |
| 625 | }; |
| 626 | |
| 627 | |
| 628 | int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env) |
| 629 | { |
| 630 | return RegisterMethodsOrDie(env, |
| 631 | "com/android/internal/content/NativeLibraryHelper", gMethods, NELEM(gMethods)); |
| 632 | } |
| 633 | |
| 634 | }; |