blob: dc0426987b1e3254be3cc59bb6c5d046e5d57f9c [file] [log] [blame]
Kenny Root66269ea2011-07-12 14:14:01 -07001/*
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
Andreas Gampeed6b9df2014-11-20 22:02:20 -080020#include "core_jni_helpers.h"
Kenny Root66269ea2011-07-12 14:14:01 -070021
Steven Moreland2279b252017-07-19 09:50:45 -070022#include <nativehelper/ScopedUtfChars.h>
Ramin Zaghi1378aba2014-02-28 15:03:19 +000023#include <androidfw/ZipFileRO.h>
24#include <androidfw/ZipUtils.h>
25#include <utils/Log.h>
26#include <utils/Vector.h>
Kenny Root66269ea2011-07-12 14:14:01 -070027
28#include <zlib.h>
29
Bill Peckhama74879d2018-09-08 10:06:40 -070030#include <errno.h>
Kenny Root66269ea2011-07-12 14:14:01 -070031#include <fcntl.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35#include <unistd.h>
Dmitriy Ivanovdec46882014-09-30 15:10:48 -070036#include <inttypes.h>
Kenny Root66269ea2011-07-12 14:14:01 -070037#include <sys/stat.h>
38#include <sys/types.h>
39
Elliott Hughes93bb56e2016-09-11 14:50:12 -070040#include <memory>
Kenny Root66269ea2011-07-12 14:14:01 -070041
42#define APK_LIB "lib/"
43#define APK_LIB_LEN (sizeof(APK_LIB) - 1)
44
45#define LIB_PREFIX "/lib"
46#define LIB_PREFIX_LEN (sizeof(LIB_PREFIX) - 1)
47
48#define LIB_SUFFIX ".so"
49#define LIB_SUFFIX_LEN (sizeof(LIB_SUFFIX) - 1)
50
Narayan Kamathd47e38b2014-05-16 16:45:56 +010051#define RS_BITCODE_SUFFIX ".bc"
Narayan Kamathd47e38b2014-05-16 16:45:56 +010052
Kenny Root66269ea2011-07-12 14:14:01 -070053#define TMP_FILE_PATTERN "/tmp.XXXXXX"
54#define TMP_FILE_PATTERN_LEN (sizeof(TMP_FILE_PATTERN) - 1)
55
56namespace android {
57
Kenny Root66269ea2011-07-12 14:14:01 -070058// These match PackageManager.java install codes
Ramin Zaghi1378aba2014-02-28 15:03:19 +000059enum install_status_t {
Kenny Root1ebd74a2011-08-03 15:09:44 -070060 INSTALL_SUCCEEDED = 1,
Kenny Root66269ea2011-07-12 14:14:01 -070061 INSTALL_FAILED_INVALID_APK = -2,
62 INSTALL_FAILED_INSUFFICIENT_STORAGE = -4,
Kenny Root1ebd74a2011-08-03 15:09:44 -070063 INSTALL_FAILED_CONTAINER_ERROR = -18,
64 INSTALL_FAILED_INTERNAL_ERROR = -110,
Narayan Kamathd11f2232014-04-10 10:37:17 +010065 INSTALL_FAILED_NO_MATCHING_ABIS = -113,
66 NO_NATIVE_LIBRARIES = -114
Ramin Zaghi1378aba2014-02-28 15:03:19 +000067};
Kenny Root66269ea2011-07-12 14:14:01 -070068
Kenny Root1ebd74a2011-08-03 15:09:44 -070069typedef install_status_t (*iterFunc)(JNIEnv*, void*, ZipFileRO*, ZipEntryRO, const char*);
70
Ramin Zaghi1378aba2014-02-28 15:03:19 +000071// Equivalent to android.os.FileUtils.isFilenameSafe
Kenny Root66269ea2011-07-12 14:14:01 -070072static bool
73isFilenameSafe(const char* filename)
74{
75 off_t offset = 0;
76 for (;;) {
77 switch (*(filename + offset)) {
78 case 0:
79 // Null.
80 // If we've reached the end, all the other characters are good.
81 return true;
82
83 case 'A' ... 'Z':
84 case 'a' ... 'z':
85 case '0' ... '9':
86 case '+':
87 case ',':
88 case '-':
89 case '.':
90 case '/':
91 case '=':
92 case '_':
93 offset++;
94 break;
95
96 default:
97 // We found something that is not good.
98 return false;
99 }
100 }
101 // Should not reach here.
102}
103
104static bool
Narayan Kamath407753c2015-06-16 12:02:57 +0100105isFileDifferent(const char* filePath, uint32_t fileSize, time_t modifiedTime,
106 uint32_t zipCrc, struct stat64* st)
Kenny Root66269ea2011-07-12 14:14:01 -0700107{
108 if (lstat64(filePath, st) < 0) {
109 // File is not found or cannot be read.
Steve Block71f2cf12011-10-20 11:56:00 +0100110 ALOGV("Couldn't stat %s, copying: %s\n", filePath, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700111 return true;
112 }
113
114 if (!S_ISREG(st->st_mode)) {
115 return true;
116 }
117
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800118 if (static_cast<uint64_t>(st->st_size) != static_cast<uint64_t>(fileSize)) {
Kenny Root66269ea2011-07-12 14:14:01 -0700119 return true;
120 }
121
122 // For some reason, bionic doesn't define st_mtime as time_t
123 if (time_t(st->st_mtime) != modifiedTime) {
Steve Block71f2cf12011-10-20 11:56:00 +0100124 ALOGV("mod time doesn't match: %ld vs. %ld\n", st->st_mtime, modifiedTime);
Kenny Root66269ea2011-07-12 14:14:01 -0700125 return true;
126 }
127
128 int fd = TEMP_FAILURE_RETRY(open(filePath, O_RDONLY));
129 if (fd < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100130 ALOGV("Couldn't open file %s: %s", filePath, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700131 return true;
132 }
133
Narayan Kamath407753c2015-06-16 12:02:57 +0100134 // uLong comes from zlib.h. It's a bit of a wart that they're
135 // potentially using a 64-bit type for a 32-bit CRC.
136 uLong crc = crc32(0L, Z_NULL, 0);
Kenny Root66269ea2011-07-12 14:14:01 -0700137 unsigned char crcBuffer[16384];
138 ssize_t numBytes;
139 while ((numBytes = TEMP_FAILURE_RETRY(read(fd, crcBuffer, sizeof(crcBuffer)))) > 0) {
140 crc = crc32(crc, crcBuffer, numBytes);
141 }
142 close(fd);
143
Narayan Kamath407753c2015-06-16 12:02:57 +0100144 ALOGV("%s: crc = %lx, zipCrc = %" PRIu32 "\n", filePath, crc, zipCrc);
Kenny Root66269ea2011-07-12 14:14:01 -0700145
Narayan Kamath407753c2015-06-16 12:02:57 +0100146 if (crc != static_cast<uLong>(zipCrc)) {
Kenny Root66269ea2011-07-12 14:14:01 -0700147 return true;
148 }
149
150 return false;
151}
152
Kenny Root1ebd74a2011-08-03 15:09:44 -0700153static install_status_t
Narayan Kamathafd31e02013-12-03 13:16:03 +0000154sumFiles(JNIEnv*, void* arg, ZipFileRO* zipFile, ZipEntryRO zipEntry, const char*)
Kenny Root66269ea2011-07-12 14:14:01 -0700155{
156 size_t* total = (size_t*) arg;
Narayan Kamath407753c2015-06-16 12:02:57 +0100157 uint32_t uncompLen;
Kenny Root66269ea2011-07-12 14:14:01 -0700158
159 if (!zipFile->getEntryInfo(zipEntry, NULL, &uncompLen, NULL, NULL, NULL, NULL)) {
Kenny Root1ebd74a2011-08-03 15:09:44 -0700160 return INSTALL_FAILED_INVALID_APK;
Kenny Root66269ea2011-07-12 14:14:01 -0700161 }
162
Narayan Kamath407753c2015-06-16 12:02:57 +0100163 *total += static_cast<size_t>(uncompLen);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700164
165 return INSTALL_SUCCEEDED;
Kenny Root66269ea2011-07-12 14:14:01 -0700166}
167
168/*
169 * Copy the native library if needed.
170 *
171 * This function assumes the library and path names passed in are considered safe.
172 */
Kenny Root1ebd74a2011-08-03 15:09:44 -0700173static install_status_t
Kenny Root66269ea2011-07-12 14:14:01 -0700174copyFileIfChanged(JNIEnv *env, void* arg, ZipFileRO* zipFile, ZipEntryRO zipEntry, const char* fileName)
175{
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700176 void** args = reinterpret_cast<void**>(arg);
177 jstring* javaNativeLibPath = (jstring*) args[0];
178 jboolean extractNativeLibs = *(jboolean*) args[1];
179 jboolean hasNativeBridge = *(jboolean*) args[2];
180
Kenny Root66269ea2011-07-12 14:14:01 -0700181 ScopedUtfChars nativeLibPath(env, *javaNativeLibPath);
182
Narayan Kamath407753c2015-06-16 12:02:57 +0100183 uint32_t uncompLen;
184 uint32_t when;
185 uint32_t crc;
Kenny Root66269ea2011-07-12 14:14:01 -0700186
Narayan Kamath407753c2015-06-16 12:02:57 +0100187 uint16_t method;
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700188 off64_t offset;
189
190 if (!zipFile->getEntryInfo(zipEntry, &method, &uncompLen, NULL, &offset, &when, &crc)) {
Steve Block5baa3a62011-12-20 16:23:08 +0000191 ALOGD("Couldn't read zip entry info\n");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700192 return INSTALL_FAILED_INVALID_APK;
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700193 }
194
195 if (!extractNativeLibs) {
196 // check if library is uncompressed and page-aligned
197 if (method != ZipFileRO::kCompressStored) {
198 ALOGD("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 ALOGD("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 if (!hasNativeBridge) {
210 return INSTALL_SUCCEEDED;
211 }
Kenny Root66269ea2011-07-12 14:14:01 -0700212 }
213
214 // Build local file path
215 const size_t fileNameLen = strlen(fileName);
216 char localFileName[nativeLibPath.size() + fileNameLen + 2];
217
218 if (strlcpy(localFileName, nativeLibPath.c_str(), sizeof(localFileName)) != nativeLibPath.size()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000219 ALOGD("Couldn't allocate local file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700220 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700221 }
222
223 *(localFileName + nativeLibPath.size()) = '/';
224
225 if (strlcpy(localFileName + nativeLibPath.size() + 1, fileName, sizeof(localFileName)
226 - nativeLibPath.size() - 1) != fileNameLen) {
Steve Block5baa3a62011-12-20 16:23:08 +0000227 ALOGD("Couldn't allocate local file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700228 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700229 }
230
231 // Only copy out the native file if it's different.
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700232 struct tm t;
233 ZipUtils::zipTimeToTimespec(when, &t);
Narayan Kamath407753c2015-06-16 12:02:57 +0100234 const time_t modTime = mktime(&t);
Elliott Hughesad076282014-01-17 18:27:27 -0800235 struct stat64 st;
Kenny Root66269ea2011-07-12 14:14:01 -0700236 if (!isFileDifferent(localFileName, uncompLen, modTime, crc, &st)) {
Kenny Root1ebd74a2011-08-03 15:09:44 -0700237 return INSTALL_SUCCEEDED;
Kenny Root66269ea2011-07-12 14:14:01 -0700238 }
239
Ivan Lozano62fc1482017-12-14 19:03:56 -0800240 char localTmpFileName[nativeLibPath.size() + TMP_FILE_PATTERN_LEN + 1];
Kenny Root66269ea2011-07-12 14:14:01 -0700241 if (strlcpy(localTmpFileName, nativeLibPath.c_str(), sizeof(localTmpFileName))
242 != nativeLibPath.size()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000243 ALOGD("Couldn't allocate local file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700244 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700245 }
246
Kenny Root66269ea2011-07-12 14:14:01 -0700247 if (strlcpy(localTmpFileName + nativeLibPath.size(), TMP_FILE_PATTERN,
Ivan Lozano62fc1482017-12-14 19:03:56 -0800248 TMP_FILE_PATTERN_LEN + 1) != TMP_FILE_PATTERN_LEN) {
Steve Block6215d3f2012-01-04 20:05:49 +0000249 ALOGI("Couldn't allocate temporary file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700250 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700251 }
252
253 int fd = mkstemp(localTmpFileName);
254 if (fd < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000255 ALOGI("Couldn't open temporary file name: %s: %s\n", localTmpFileName, strerror(errno));
Kenny Root1ebd74a2011-08-03 15:09:44 -0700256 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700257 }
258
259 if (!zipFile->uncompressEntry(zipEntry, fd)) {
Steve Block6215d3f2012-01-04 20:05:49 +0000260 ALOGI("Failed uncompressing %s to %s\n", fileName, localTmpFileName);
Kenny Root66269ea2011-07-12 14:14:01 -0700261 close(fd);
262 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700263 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700264 }
265
266 close(fd);
267
268 // Set the modification time for this file to the ZIP's mod time.
269 struct timeval times[2];
270 times[0].tv_sec = st.st_atime;
271 times[1].tv_sec = modTime;
272 times[0].tv_usec = times[1].tv_usec = 0;
273 if (utimes(localTmpFileName, times) < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000274 ALOGI("Couldn't change modification time on %s: %s\n", localTmpFileName, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700275 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700276 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700277 }
278
279 // Set the mode to 755
280 static const mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
281 if (chmod(localTmpFileName, mode) < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000282 ALOGI("Couldn't change permissions on %s: %s\n", localTmpFileName, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700283 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700284 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700285 }
286
287 // Finally, rename it to the final name.
288 if (rename(localTmpFileName, localFileName) < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000289 ALOGI("Couldn't rename %s to %s: %s\n", localTmpFileName, localFileName, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700290 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700291 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700292 }
293
Steve Block71f2cf12011-10-20 11:56:00 +0100294 ALOGV("Successfully moved %s to %s\n", localTmpFileName, localFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700295
296 return INSTALL_SUCCEEDED;
Kenny Root66269ea2011-07-12 14:14:01 -0700297}
298
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000299/*
300 * An iterator over all shared libraries in a zip file. An entry is
301 * considered to be a shared library if all of the conditions below are
302 * satisfied :
303 *
304 * - The entry is under the lib/ directory.
305 * - The entry name ends with ".so" and the entry name starts with "lib",
306 * an exception is made for entries whose name is "gdbserver".
307 * - The entry filename is "safe" (as determined by isFilenameSafe).
308 *
309 */
310class NativeLibrariesIterator {
311private:
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100312 NativeLibrariesIterator(ZipFileRO* zipFile, bool debuggable, void* cookie)
313 : mZipFile(zipFile), mDebuggable(debuggable), mCookie(cookie), mLastSlash(NULL) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000314 fileName[0] = '\0';
Kenny Root66269ea2011-07-12 14:14:01 -0700315 }
316
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000317public:
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100318 static NativeLibrariesIterator* create(ZipFileRO* zipFile, bool debuggable) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000319 void* cookie = NULL;
Yusuke Sato957c2372015-08-03 16:17:16 -0700320 // Do not specify a suffix to find both .so files and gdbserver.
321 if (!zipFile->startIteration(&cookie, APK_LIB, NULL /* suffix */)) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000322 return NULL;
323 }
324
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100325 return new NativeLibrariesIterator(zipFile, debuggable, cookie);
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000326 }
327
328 ZipEntryRO next() {
329 ZipEntryRO next = NULL;
330 while ((next = mZipFile->nextEntry(mCookie)) != NULL) {
331 // Make sure this entry has a filename.
332 if (mZipFile->getEntryFileName(next, fileName, sizeof(fileName))) {
333 continue;
334 }
335
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000336 // Make sure the filename is at least to the minimum library name size.
337 const size_t fileNameLen = strlen(fileName);
338 static const size_t minLength = APK_LIB_LEN + 2 + LIB_PREFIX_LEN + 1 + LIB_SUFFIX_LEN;
339 if (fileNameLen < minLength) {
340 continue;
341 }
342
343 const char* lastSlash = strrchr(fileName, '/');
344 ALOG_ASSERT(lastSlash != NULL, "last slash was null somehow for %s\n", fileName);
345
Andreas Gampec012ae32016-12-15 16:12:39 -0800346 // Skip directories.
347 if (*(lastSlash + 1) == 0) {
348 continue;
349 }
350
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000351 // Make sure the filename is safe.
352 if (!isFilenameSafe(lastSlash + 1)) {
353 continue;
354 }
355
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100356 if (!mDebuggable) {
357 // Make sure the filename starts with lib and ends with ".so".
358 if (strncmp(fileName + fileNameLen - LIB_SUFFIX_LEN, LIB_SUFFIX, LIB_SUFFIX_LEN)
359 || strncmp(lastSlash, LIB_PREFIX, LIB_PREFIX_LEN)) {
360 continue;
361 }
362 }
363
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000364 mLastSlash = lastSlash;
365 break;
366 }
367
368 return next;
369 }
370
371 inline const char* currentEntry() const {
372 return fileName;
373 }
374
375 inline const char* lastSlash() const {
376 return mLastSlash;
377 }
378
379 virtual ~NativeLibrariesIterator() {
380 mZipFile->endIteration(mCookie);
381 }
382private:
383
Kenny Root66269ea2011-07-12 14:14:01 -0700384 char fileName[PATH_MAX];
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000385 ZipFileRO* const mZipFile;
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100386 const bool mDebuggable;
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000387 void* mCookie;
388 const char* mLastSlash;
389};
Kenny Root66269ea2011-07-12 14:14:01 -0700390
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000391static install_status_t
392iterateOverNativeFiles(JNIEnv *env, jlong apkHandle, jstring javaCpuAbi,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100393 jboolean debuggable, iterFunc callFunc, void* callArg) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000394 ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle);
395 if (zipFile == NULL) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000396 return INSTALL_FAILED_INVALID_APK;
397 }
Kenny Root66269ea2011-07-12 14:14:01 -0700398
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100399 std::unique_ptr<NativeLibrariesIterator> it(
400 NativeLibrariesIterator::create(zipFile, debuggable));
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000401 if (it.get() == NULL) {
402 return INSTALL_FAILED_INVALID_APK;
403 }
404
405 const ScopedUtfChars cpuAbi(env, javaCpuAbi);
406 if (cpuAbi.c_str() == NULL) {
407 // This would've thrown, so this return code isn't observable by
408 // Java.
409 return INSTALL_FAILED_INVALID_APK;
410 }
Narayan Kamathafd31e02013-12-03 13:16:03 +0000411 ZipEntryRO entry = NULL;
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000412 while ((entry = it->next()) != NULL) {
413 const char* fileName = it->currentEntry();
414 const char* lastSlash = it->lastSlash();
Kenny Root66269ea2011-07-12 14:14:01 -0700415
416 // Check to make sure the CPU ABI of this file is one we support.
417 const char* cpuAbiOffset = fileName + APK_LIB_LEN;
418 const size_t cpuAbiRegionSize = lastSlash - cpuAbiOffset;
419
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000420 if (cpuAbi.size() == cpuAbiRegionSize && !strncmp(cpuAbiOffset, cpuAbi.c_str(), cpuAbiRegionSize)) {
421 install_status_t ret = callFunc(env, callArg, zipFile, entry, lastSlash + 1);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700422
423 if (ret != INSTALL_SUCCEEDED) {
Steve Block71f2cf12011-10-20 11:56:00 +0100424 ALOGV("Failure for entry %s", lastSlash + 1);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700425 return ret;
426 }
Kenny Root66269ea2011-07-12 14:14:01 -0700427 }
428 }
429
430 return INSTALL_SUCCEEDED;
431}
432
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000433
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100434static int findSupportedAbi(JNIEnv *env, jlong apkHandle, jobjectArray supportedAbisArray,
435 jboolean debuggable) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000436 const int numAbis = env->GetArrayLength(supportedAbisArray);
437 Vector<ScopedUtfChars*> supportedAbis;
438
439 for (int i = 0; i < numAbis; ++i) {
440 supportedAbis.add(new ScopedUtfChars(env,
441 (jstring) env->GetObjectArrayElement(supportedAbisArray, i)));
442 }
443
444 ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle);
445 if (zipFile == NULL) {
446 return INSTALL_FAILED_INVALID_APK;
447 }
448
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100449 std::unique_ptr<NativeLibrariesIterator> it(
450 NativeLibrariesIterator::create(zipFile, debuggable));
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000451 if (it.get() == NULL) {
452 return INSTALL_FAILED_INVALID_APK;
453 }
454
455 ZipEntryRO entry = NULL;
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000456 int status = NO_NATIVE_LIBRARIES;
457 while ((entry = it->next()) != NULL) {
458 // We're currently in the lib/ directory of the APK, so it does have some native
459 // code. We should return INSTALL_FAILED_NO_MATCHING_ABIS if none of the
460 // libraries match.
461 if (status == NO_NATIVE_LIBRARIES) {
462 status = INSTALL_FAILED_NO_MATCHING_ABIS;
463 }
464
465 const char* fileName = it->currentEntry();
466 const char* lastSlash = it->lastSlash();
467
468 // Check to see if this CPU ABI matches what we are looking for.
469 const char* abiOffset = fileName + APK_LIB_LEN;
470 const size_t abiSize = lastSlash - abiOffset;
471 for (int i = 0; i < numAbis; i++) {
472 const ScopedUtfChars* abi = supportedAbis[i];
473 if (abi->size() == abiSize && !strncmp(abiOffset, abi->c_str(), abiSize)) {
474 // The entry that comes in first (i.e. with a lower index) has the higher priority.
475 if (((i < status) && (status >= 0)) || (status < 0) ) {
476 status = i;
477 }
478 }
479 }
480 }
481
482 for (int i = 0; i < numAbis; ++i) {
483 delete supportedAbis[i];
484 }
485
486 return status;
487}
488
Kenny Root66269ea2011-07-12 14:14:01 -0700489static jint
490com_android_internal_content_NativeLibraryHelper_copyNativeBinaries(JNIEnv *env, jclass clazz,
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700491 jlong apkHandle, jstring javaNativeLibPath, jstring javaCpuAbi,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100492 jboolean extractNativeLibs, jboolean hasNativeBridge, jboolean debuggable)
Kenny Root66269ea2011-07-12 14:14:01 -0700493{
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700494 void* args[] = { &javaNativeLibPath, &extractNativeLibs, &hasNativeBridge };
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100495 return (jint) iterateOverNativeFiles(env, apkHandle, javaCpuAbi, debuggable,
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700496 copyFileIfChanged, reinterpret_cast<void*>(args));
Kenny Root66269ea2011-07-12 14:14:01 -0700497}
498
499static jlong
500com_android_internal_content_NativeLibraryHelper_sumNativeBinaries(JNIEnv *env, jclass clazz,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100501 jlong apkHandle, jstring javaCpuAbi, jboolean debuggable)
Kenny Root66269ea2011-07-12 14:14:01 -0700502{
503 size_t totalSize = 0;
504
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100505 iterateOverNativeFiles(env, apkHandle, javaCpuAbi, debuggable, sumFiles, &totalSize);
Kenny Root66269ea2011-07-12 14:14:01 -0700506
507 return totalSize;
508}
509
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000510static jint
511com_android_internal_content_NativeLibraryHelper_findSupportedAbi(JNIEnv *env, jclass clazz,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100512 jlong apkHandle, jobjectArray javaCpuAbisToSearch, jboolean debuggable)
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000513{
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100514 return (jint) findSupportedAbi(env, apkHandle, javaCpuAbisToSearch, debuggable);
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000515}
516
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100517enum bitcode_scan_result_t {
518 APK_SCAN_ERROR = -1,
519 NO_BITCODE_PRESENT = 0,
520 BITCODE_PRESENT = 1,
521};
522
523static jint
524com_android_internal_content_NativeLibraryHelper_hasRenderscriptBitcode(JNIEnv *env, jclass clazz,
525 jlong apkHandle) {
526 ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle);
527 void* cookie = NULL;
Yusuke Sato957c2372015-08-03 16:17:16 -0700528 if (!zipFile->startIteration(&cookie, NULL /* prefix */, RS_BITCODE_SUFFIX)) {
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100529 return APK_SCAN_ERROR;
530 }
531
532 char fileName[PATH_MAX];
533 ZipEntryRO next = NULL;
534 while ((next = zipFile->nextEntry(cookie)) != NULL) {
535 if (zipFile->getEntryFileName(next, fileName, sizeof(fileName))) {
536 continue;
537 }
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100538 const char* lastSlash = strrchr(fileName, '/');
539 const char* baseName = (lastSlash == NULL) ? fileName : fileName + 1;
Yusuke Sato957c2372015-08-03 16:17:16 -0700540 if (isFilenameSafe(baseName)) {
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100541 zipFile->endIteration(cookie);
542 return BITCODE_PRESENT;
543 }
544 }
545
546 zipFile->endIteration(cookie);
547 return NO_BITCODE_PRESENT;
548}
549
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000550static jlong
551com_android_internal_content_NativeLibraryHelper_openApk(JNIEnv *env, jclass, jstring apkPath)
552{
553 ScopedUtfChars filePath(env, apkPath);
554 ZipFileRO* zipFile = ZipFileRO::open(filePath.c_str());
555
556 return reinterpret_cast<jlong>(zipFile);
557}
558
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000559static jlong
560com_android_internal_content_NativeLibraryHelper_openApkFd(JNIEnv *env, jclass,
561 jobject fileDescriptor, jstring debugPathName)
562{
563 ScopedUtfChars debugFilePath(env, debugPathName);
564
565 int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
566 if (fd < 0) {
567 jniThrowException(env, "java/lang/IllegalArgumentException", "Bad FileDescriptor");
568 return 0;
569 }
570
Bill Peckhama74879d2018-09-08 10:06:40 -0700571 int dupedFd = dup(fd);
572 if (dupedFd == -1) {
573 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
574 "Failed to dup FileDescriptor: %s", strerror(errno));
575 return 0;
576 }
577
578 ZipFileRO* zipFile = ZipFileRO::openFd(dupedFd, debugFilePath.c_str());
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000579
580 return reinterpret_cast<jlong>(zipFile);
581}
582
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000583static void
584com_android_internal_content_NativeLibraryHelper_close(JNIEnv *env, jclass, jlong apkHandle)
585{
586 delete reinterpret_cast<ZipFileRO*>(apkHandle);
587}
588
Daniel Micay76f6a862015-09-19 17:31:01 -0400589static const JNINativeMethod gMethods[] = {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000590 {"nativeOpenApk",
591 "(Ljava/lang/String;)J",
592 (void *)com_android_internal_content_NativeLibraryHelper_openApk},
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000593 {"nativeOpenApkFd",
594 "(Ljava/io/FileDescriptor;Ljava/lang/String;)J",
595 (void *)com_android_internal_content_NativeLibraryHelper_openApkFd},
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000596 {"nativeClose",
597 "(J)V",
598 (void *)com_android_internal_content_NativeLibraryHelper_close},
Kenny Root66269ea2011-07-12 14:14:01 -0700599 {"nativeCopyNativeBinaries",
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100600 "(JLjava/lang/String;Ljava/lang/String;ZZZ)I",
Kenny Root66269ea2011-07-12 14:14:01 -0700601 (void *)com_android_internal_content_NativeLibraryHelper_copyNativeBinaries},
602 {"nativeSumNativeBinaries",
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100603 "(JLjava/lang/String;Z)J",
Kenny Root66269ea2011-07-12 14:14:01 -0700604 (void *)com_android_internal_content_NativeLibraryHelper_sumNativeBinaries},
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000605 {"nativeFindSupportedAbi",
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100606 "(J[Ljava/lang/String;Z)I",
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000607 (void *)com_android_internal_content_NativeLibraryHelper_findSupportedAbi},
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100608 {"hasRenderscriptBitcode", "(J)I",
609 (void *)com_android_internal_content_NativeLibraryHelper_hasRenderscriptBitcode},
Kenny Root66269ea2011-07-12 14:14:01 -0700610};
611
612
613int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env)
614{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800615 return RegisterMethodsOrDie(env,
616 "com/android/internal/content/NativeLibraryHelper", gMethods, NELEM(gMethods));
Kenny Root66269ea2011-07-12 14:14:01 -0700617}
618
619};