blob: f8f9efe2804fb1f6e82250e2dfd98404ea5a46cb [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
Kenny Root66269ea2011-07-12 14:14:01 -070022#include <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
30#include <fcntl.h>
31#include <stdlib.h>
32#include <string.h>
33#include <time.h>
34#include <unistd.h>
Dmitriy Ivanovdec46882014-09-30 15:10:48 -070035#include <inttypes.h>
Kenny Root66269ea2011-07-12 14:14:01 -070036#include <sys/stat.h>
37#include <sys/types.h>
38
Elliott Hughes93bb56e2016-09-11 14:50:12 -070039#include <memory>
Kenny Root66269ea2011-07-12 14:14:01 -070040
41#define APK_LIB "lib/"
42#define APK_LIB_LEN (sizeof(APK_LIB) - 1)
43
44#define LIB_PREFIX "/lib"
45#define LIB_PREFIX_LEN (sizeof(LIB_PREFIX) - 1)
46
47#define LIB_SUFFIX ".so"
48#define LIB_SUFFIX_LEN (sizeof(LIB_SUFFIX) - 1)
49
Narayan Kamathd47e38b2014-05-16 16:45:56 +010050#define RS_BITCODE_SUFFIX ".bc"
Narayan Kamathd47e38b2014-05-16 16:45:56 +010051
Kenny Root66269ea2011-07-12 14:14:01 -070052#define TMP_FILE_PATTERN "/tmp.XXXXXX"
53#define TMP_FILE_PATTERN_LEN (sizeof(TMP_FILE_PATTERN) - 1)
54
55namespace android {
56
Kenny Root66269ea2011-07-12 14:14:01 -070057// These match PackageManager.java install codes
Ramin Zaghi1378aba2014-02-28 15:03:19 +000058enum install_status_t {
Kenny Root1ebd74a2011-08-03 15:09:44 -070059 INSTALL_SUCCEEDED = 1,
Kenny Root66269ea2011-07-12 14:14:01 -070060 INSTALL_FAILED_INVALID_APK = -2,
61 INSTALL_FAILED_INSUFFICIENT_STORAGE = -4,
Kenny Root1ebd74a2011-08-03 15:09:44 -070062 INSTALL_FAILED_CONTAINER_ERROR = -18,
63 INSTALL_FAILED_INTERNAL_ERROR = -110,
Narayan Kamathd11f2232014-04-10 10:37:17 +010064 INSTALL_FAILED_NO_MATCHING_ABIS = -113,
65 NO_NATIVE_LIBRARIES = -114
Ramin Zaghi1378aba2014-02-28 15:03:19 +000066};
Kenny Root66269ea2011-07-12 14:14:01 -070067
Kenny Root1ebd74a2011-08-03 15:09:44 -070068typedef install_status_t (*iterFunc)(JNIEnv*, void*, ZipFileRO*, ZipEntryRO, const char*);
69
Ramin Zaghi1378aba2014-02-28 15:03:19 +000070// Equivalent to android.os.FileUtils.isFilenameSafe
Kenny Root66269ea2011-07-12 14:14:01 -070071static bool
72isFilenameSafe(const char* filename)
73{
74 off_t offset = 0;
75 for (;;) {
76 switch (*(filename + offset)) {
77 case 0:
78 // Null.
79 // If we've reached the end, all the other characters are good.
80 return true;
81
82 case 'A' ... 'Z':
83 case 'a' ... 'z':
84 case '0' ... '9':
85 case '+':
86 case ',':
87 case '-':
88 case '.':
89 case '/':
90 case '=':
91 case '_':
92 offset++;
93 break;
94
95 default:
96 // We found something that is not good.
97 return false;
98 }
99 }
100 // Should not reach here.
101}
102
103static bool
Narayan Kamath407753c2015-06-16 12:02:57 +0100104isFileDifferent(const char* filePath, uint32_t fileSize, time_t modifiedTime,
105 uint32_t zipCrc, struct stat64* st)
Kenny Root66269ea2011-07-12 14:14:01 -0700106{
107 if (lstat64(filePath, st) < 0) {
108 // File is not found or cannot be read.
Steve Block71f2cf12011-10-20 11:56:00 +0100109 ALOGV("Couldn't stat %s, copying: %s\n", filePath, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700110 return true;
111 }
112
113 if (!S_ISREG(st->st_mode)) {
114 return true;
115 }
116
Andreas Gampe0f0b4912014-11-12 08:03:48 -0800117 if (static_cast<uint64_t>(st->st_size) != static_cast<uint64_t>(fileSize)) {
Kenny Root66269ea2011-07-12 14:14:01 -0700118 return true;
119 }
120
121 // For some reason, bionic doesn't define st_mtime as time_t
122 if (time_t(st->st_mtime) != modifiedTime) {
Steve Block71f2cf12011-10-20 11:56:00 +0100123 ALOGV("mod time doesn't match: %ld vs. %ld\n", st->st_mtime, modifiedTime);
Kenny Root66269ea2011-07-12 14:14:01 -0700124 return true;
125 }
126
127 int fd = TEMP_FAILURE_RETRY(open(filePath, O_RDONLY));
128 if (fd < 0) {
Steve Block71f2cf12011-10-20 11:56:00 +0100129 ALOGV("Couldn't open file %s: %s", filePath, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700130 return true;
131 }
132
Narayan Kamath407753c2015-06-16 12:02:57 +0100133 // uLong comes from zlib.h. It's a bit of a wart that they're
134 // potentially using a 64-bit type for a 32-bit CRC.
135 uLong crc = crc32(0L, Z_NULL, 0);
Kenny Root66269ea2011-07-12 14:14:01 -0700136 unsigned char crcBuffer[16384];
137 ssize_t numBytes;
138 while ((numBytes = TEMP_FAILURE_RETRY(read(fd, crcBuffer, sizeof(crcBuffer)))) > 0) {
139 crc = crc32(crc, crcBuffer, numBytes);
140 }
141 close(fd);
142
Narayan Kamath407753c2015-06-16 12:02:57 +0100143 ALOGV("%s: crc = %lx, zipCrc = %" PRIu32 "\n", filePath, crc, zipCrc);
Kenny Root66269ea2011-07-12 14:14:01 -0700144
Narayan Kamath407753c2015-06-16 12:02:57 +0100145 if (crc != static_cast<uLong>(zipCrc)) {
Kenny Root66269ea2011-07-12 14:14:01 -0700146 return true;
147 }
148
149 return false;
150}
151
Kenny Root1ebd74a2011-08-03 15:09:44 -0700152static install_status_t
Narayan Kamathafd31e02013-12-03 13:16:03 +0000153sumFiles(JNIEnv*, void* arg, ZipFileRO* zipFile, ZipEntryRO zipEntry, const char*)
Kenny Root66269ea2011-07-12 14:14:01 -0700154{
155 size_t* total = (size_t*) arg;
Narayan Kamath407753c2015-06-16 12:02:57 +0100156 uint32_t uncompLen;
Kenny Root66269ea2011-07-12 14:14:01 -0700157
158 if (!zipFile->getEntryInfo(zipEntry, NULL, &uncompLen, NULL, NULL, NULL, NULL)) {
Kenny Root1ebd74a2011-08-03 15:09:44 -0700159 return INSTALL_FAILED_INVALID_APK;
Kenny Root66269ea2011-07-12 14:14:01 -0700160 }
161
Narayan Kamath407753c2015-06-16 12:02:57 +0100162 *total += static_cast<size_t>(uncompLen);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700163
164 return INSTALL_SUCCEEDED;
Kenny Root66269ea2011-07-12 14:14:01 -0700165}
166
167/*
168 * Copy the native library if needed.
169 *
170 * This function assumes the library and path names passed in are considered safe.
171 */
Kenny Root1ebd74a2011-08-03 15:09:44 -0700172static install_status_t
Kenny Root66269ea2011-07-12 14:14:01 -0700173copyFileIfChanged(JNIEnv *env, void* arg, ZipFileRO* zipFile, ZipEntryRO zipEntry, const char* fileName)
174{
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700175 void** args = reinterpret_cast<void**>(arg);
176 jstring* javaNativeLibPath = (jstring*) args[0];
177 jboolean extractNativeLibs = *(jboolean*) args[1];
178 jboolean hasNativeBridge = *(jboolean*) args[2];
179
Kenny Root66269ea2011-07-12 14:14:01 -0700180 ScopedUtfChars nativeLibPath(env, *javaNativeLibPath);
181
Narayan Kamath407753c2015-06-16 12:02:57 +0100182 uint32_t uncompLen;
183 uint32_t when;
184 uint32_t crc;
Kenny Root66269ea2011-07-12 14:14:01 -0700185
Narayan Kamath407753c2015-06-16 12:02:57 +0100186 uint16_t method;
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700187 off64_t offset;
188
189 if (!zipFile->getEntryInfo(zipEntry, &method, &uncompLen, NULL, &offset, &when, &crc)) {
Steve Block5baa3a62011-12-20 16:23:08 +0000190 ALOGD("Couldn't read zip entry info\n");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700191 return INSTALL_FAILED_INVALID_APK;
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700192 }
193
194 if (!extractNativeLibs) {
195 // check if library is uncompressed and page-aligned
196 if (method != ZipFileRO::kCompressStored) {
197 ALOGD("Library '%s' is compressed - will not be able to open it directly from apk.\n",
198 fileName);
199 return INSTALL_FAILED_INVALID_APK;
200 }
201
202 if (offset % PAGE_SIZE != 0) {
203 ALOGD("Library '%s' is not page-aligned - will not be able to open it directly from"
204 " apk.\n", fileName);
205 return INSTALL_FAILED_INVALID_APK;
206 }
207
208 if (!hasNativeBridge) {
209 return INSTALL_SUCCEEDED;
210 }
Kenny Root66269ea2011-07-12 14:14:01 -0700211 }
212
213 // Build local file path
214 const size_t fileNameLen = strlen(fileName);
215 char localFileName[nativeLibPath.size() + fileNameLen + 2];
216
217 if (strlcpy(localFileName, nativeLibPath.c_str(), sizeof(localFileName)) != nativeLibPath.size()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000218 ALOGD("Couldn't allocate local file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700219 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700220 }
221
222 *(localFileName + nativeLibPath.size()) = '/';
223
224 if (strlcpy(localFileName + nativeLibPath.size() + 1, fileName, sizeof(localFileName)
225 - nativeLibPath.size() - 1) != fileNameLen) {
Steve Block5baa3a62011-12-20 16:23:08 +0000226 ALOGD("Couldn't allocate local file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700227 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700228 }
229
230 // Only copy out the native file if it's different.
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700231 struct tm t;
232 ZipUtils::zipTimeToTimespec(when, &t);
Narayan Kamath407753c2015-06-16 12:02:57 +0100233 const time_t modTime = mktime(&t);
Elliott Hughesad076282014-01-17 18:27:27 -0800234 struct stat64 st;
Kenny Root66269ea2011-07-12 14:14:01 -0700235 if (!isFileDifferent(localFileName, uncompLen, modTime, crc, &st)) {
Kenny Root1ebd74a2011-08-03 15:09:44 -0700236 return INSTALL_SUCCEEDED;
Kenny Root66269ea2011-07-12 14:14:01 -0700237 }
238
239 char localTmpFileName[nativeLibPath.size() + TMP_FILE_PATTERN_LEN + 2];
240 if (strlcpy(localTmpFileName, nativeLibPath.c_str(), sizeof(localTmpFileName))
241 != nativeLibPath.size()) {
Steve Block5baa3a62011-12-20 16:23:08 +0000242 ALOGD("Couldn't allocate local file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700243 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700244 }
245
Andreas Gampec012ae32016-12-15 16:12:39 -0800246 *(localTmpFileName + nativeLibPath.size()) = '/';
Kenny Root66269ea2011-07-12 14:14:01 -0700247
248 if (strlcpy(localTmpFileName + nativeLibPath.size(), TMP_FILE_PATTERN,
249 TMP_FILE_PATTERN_LEN - nativeLibPath.size()) != TMP_FILE_PATTERN_LEN) {
Steve Block6215d3f2012-01-04 20:05:49 +0000250 ALOGI("Couldn't allocate temporary file name for library");
Kenny Root1ebd74a2011-08-03 15:09:44 -0700251 return INSTALL_FAILED_INTERNAL_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700252 }
253
254 int fd = mkstemp(localTmpFileName);
255 if (fd < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000256 ALOGI("Couldn't open temporary file name: %s: %s\n", localTmpFileName, strerror(errno));
Kenny Root1ebd74a2011-08-03 15:09:44 -0700257 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700258 }
259
260 if (!zipFile->uncompressEntry(zipEntry, fd)) {
Steve Block6215d3f2012-01-04 20:05:49 +0000261 ALOGI("Failed uncompressing %s to %s\n", fileName, localTmpFileName);
Kenny Root66269ea2011-07-12 14:14:01 -0700262 close(fd);
263 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700264 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700265 }
266
267 close(fd);
268
269 // Set the modification time for this file to the ZIP's mod time.
270 struct timeval times[2];
271 times[0].tv_sec = st.st_atime;
272 times[1].tv_sec = modTime;
273 times[0].tv_usec = times[1].tv_usec = 0;
274 if (utimes(localTmpFileName, times) < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000275 ALOGI("Couldn't change modification time on %s: %s\n", localTmpFileName, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700276 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700277 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700278 }
279
280 // Set the mode to 755
281 static const mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
282 if (chmod(localTmpFileName, mode) < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000283 ALOGI("Couldn't change permissions on %s: %s\n", localTmpFileName, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700284 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700285 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700286 }
287
288 // Finally, rename it to the final name.
289 if (rename(localTmpFileName, localFileName) < 0) {
Steve Block6215d3f2012-01-04 20:05:49 +0000290 ALOGI("Couldn't rename %s to %s: %s\n", localTmpFileName, localFileName, strerror(errno));
Kenny Root66269ea2011-07-12 14:14:01 -0700291 unlink(localTmpFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700292 return INSTALL_FAILED_CONTAINER_ERROR;
Kenny Root66269ea2011-07-12 14:14:01 -0700293 }
294
Steve Block71f2cf12011-10-20 11:56:00 +0100295 ALOGV("Successfully moved %s to %s\n", localTmpFileName, localFileName);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700296
297 return INSTALL_SUCCEEDED;
Kenny Root66269ea2011-07-12 14:14:01 -0700298}
299
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000300/*
301 * An iterator over all shared libraries in a zip file. An entry is
302 * considered to be a shared library if all of the conditions below are
303 * satisfied :
304 *
305 * - The entry is under the lib/ directory.
306 * - The entry name ends with ".so" and the entry name starts with "lib",
307 * an exception is made for entries whose name is "gdbserver".
308 * - The entry filename is "safe" (as determined by isFilenameSafe).
309 *
310 */
311class NativeLibrariesIterator {
312private:
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100313 NativeLibrariesIterator(ZipFileRO* zipFile, bool debuggable, void* cookie)
314 : mZipFile(zipFile), mDebuggable(debuggable), mCookie(cookie), mLastSlash(NULL) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000315 fileName[0] = '\0';
Kenny Root66269ea2011-07-12 14:14:01 -0700316 }
317
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000318public:
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100319 static NativeLibrariesIterator* create(ZipFileRO* zipFile, bool debuggable) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000320 void* cookie = NULL;
Yusuke Sato957c2372015-08-03 16:17:16 -0700321 // Do not specify a suffix to find both .so files and gdbserver.
322 if (!zipFile->startIteration(&cookie, APK_LIB, NULL /* suffix */)) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000323 return NULL;
324 }
325
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100326 return new NativeLibrariesIterator(zipFile, debuggable, cookie);
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000327 }
328
329 ZipEntryRO next() {
330 ZipEntryRO next = NULL;
331 while ((next = mZipFile->nextEntry(mCookie)) != NULL) {
332 // Make sure this entry has a filename.
333 if (mZipFile->getEntryFileName(next, fileName, sizeof(fileName))) {
334 continue;
335 }
336
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000337 // Make sure the filename is at least to the minimum library name size.
338 const size_t fileNameLen = strlen(fileName);
339 static const size_t minLength = APK_LIB_LEN + 2 + LIB_PREFIX_LEN + 1 + LIB_SUFFIX_LEN;
340 if (fileNameLen < minLength) {
341 continue;
342 }
343
344 const char* lastSlash = strrchr(fileName, '/');
345 ALOG_ASSERT(lastSlash != NULL, "last slash was null somehow for %s\n", fileName);
346
Andreas Gampec012ae32016-12-15 16:12:39 -0800347 // Skip directories.
348 if (*(lastSlash + 1) == 0) {
349 continue;
350 }
351
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000352 // Make sure the filename is safe.
353 if (!isFilenameSafe(lastSlash + 1)) {
354 continue;
355 }
356
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100357 if (!mDebuggable) {
358 // Make sure the filename starts with lib and ends with ".so".
359 if (strncmp(fileName + fileNameLen - LIB_SUFFIX_LEN, LIB_SUFFIX, LIB_SUFFIX_LEN)
360 || strncmp(lastSlash, LIB_PREFIX, LIB_PREFIX_LEN)) {
361 continue;
362 }
363 }
364
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000365 mLastSlash = lastSlash;
366 break;
367 }
368
369 return next;
370 }
371
372 inline const char* currentEntry() const {
373 return fileName;
374 }
375
376 inline const char* lastSlash() const {
377 return mLastSlash;
378 }
379
380 virtual ~NativeLibrariesIterator() {
381 mZipFile->endIteration(mCookie);
382 }
383private:
384
Kenny Root66269ea2011-07-12 14:14:01 -0700385 char fileName[PATH_MAX];
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000386 ZipFileRO* const mZipFile;
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100387 const bool mDebuggable;
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000388 void* mCookie;
389 const char* mLastSlash;
390};
Kenny Root66269ea2011-07-12 14:14:01 -0700391
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000392static install_status_t
393iterateOverNativeFiles(JNIEnv *env, jlong apkHandle, jstring javaCpuAbi,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100394 jboolean debuggable, iterFunc callFunc, void* callArg) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000395 ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle);
396 if (zipFile == NULL) {
Narayan Kamathafd31e02013-12-03 13:16:03 +0000397 return INSTALL_FAILED_INVALID_APK;
398 }
Kenny Root66269ea2011-07-12 14:14:01 -0700399
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100400 std::unique_ptr<NativeLibrariesIterator> it(
401 NativeLibrariesIterator::create(zipFile, debuggable));
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000402 if (it.get() == NULL) {
403 return INSTALL_FAILED_INVALID_APK;
404 }
405
406 const ScopedUtfChars cpuAbi(env, javaCpuAbi);
407 if (cpuAbi.c_str() == NULL) {
408 // This would've thrown, so this return code isn't observable by
409 // Java.
410 return INSTALL_FAILED_INVALID_APK;
411 }
Narayan Kamathafd31e02013-12-03 13:16:03 +0000412 ZipEntryRO entry = NULL;
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000413 while ((entry = it->next()) != NULL) {
414 const char* fileName = it->currentEntry();
415 const char* lastSlash = it->lastSlash();
Kenny Root66269ea2011-07-12 14:14:01 -0700416
417 // Check to make sure the CPU ABI of this file is one we support.
418 const char* cpuAbiOffset = fileName + APK_LIB_LEN;
419 const size_t cpuAbiRegionSize = lastSlash - cpuAbiOffset;
420
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000421 if (cpuAbi.size() == cpuAbiRegionSize && !strncmp(cpuAbiOffset, cpuAbi.c_str(), cpuAbiRegionSize)) {
422 install_status_t ret = callFunc(env, callArg, zipFile, entry, lastSlash + 1);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700423
424 if (ret != INSTALL_SUCCEEDED) {
Steve Block71f2cf12011-10-20 11:56:00 +0100425 ALOGV("Failure for entry %s", lastSlash + 1);
Kenny Root1ebd74a2011-08-03 15:09:44 -0700426 return ret;
427 }
Kenny Root66269ea2011-07-12 14:14:01 -0700428 }
429 }
430
431 return INSTALL_SUCCEEDED;
432}
433
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000434
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100435static int findSupportedAbi(JNIEnv *env, jlong apkHandle, jobjectArray supportedAbisArray,
436 jboolean debuggable) {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000437 const int numAbis = env->GetArrayLength(supportedAbisArray);
438 Vector<ScopedUtfChars*> supportedAbis;
439
440 for (int i = 0; i < numAbis; ++i) {
441 supportedAbis.add(new ScopedUtfChars(env,
442 (jstring) env->GetObjectArrayElement(supportedAbisArray, i)));
443 }
444
445 ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle);
446 if (zipFile == NULL) {
447 return INSTALL_FAILED_INVALID_APK;
448 }
449
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100450 std::unique_ptr<NativeLibrariesIterator> it(
451 NativeLibrariesIterator::create(zipFile, debuggable));
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000452 if (it.get() == NULL) {
453 return INSTALL_FAILED_INVALID_APK;
454 }
455
456 ZipEntryRO entry = NULL;
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000457 int status = NO_NATIVE_LIBRARIES;
458 while ((entry = it->next()) != NULL) {
459 // We're currently in the lib/ directory of the APK, so it does have some native
460 // code. We should return INSTALL_FAILED_NO_MATCHING_ABIS if none of the
461 // libraries match.
462 if (status == NO_NATIVE_LIBRARIES) {
463 status = INSTALL_FAILED_NO_MATCHING_ABIS;
464 }
465
466 const char* fileName = it->currentEntry();
467 const char* lastSlash = it->lastSlash();
468
469 // Check to see if this CPU ABI matches what we are looking for.
470 const char* abiOffset = fileName + APK_LIB_LEN;
471 const size_t abiSize = lastSlash - abiOffset;
472 for (int i = 0; i < numAbis; i++) {
473 const ScopedUtfChars* abi = supportedAbis[i];
474 if (abi->size() == abiSize && !strncmp(abiOffset, abi->c_str(), abiSize)) {
475 // The entry that comes in first (i.e. with a lower index) has the higher priority.
476 if (((i < status) && (status >= 0)) || (status < 0) ) {
477 status = i;
478 }
479 }
480 }
481 }
482
483 for (int i = 0; i < numAbis; ++i) {
484 delete supportedAbis[i];
485 }
486
487 return status;
488}
489
Kenny Root66269ea2011-07-12 14:14:01 -0700490static jint
491com_android_internal_content_NativeLibraryHelper_copyNativeBinaries(JNIEnv *env, jclass clazz,
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700492 jlong apkHandle, jstring javaNativeLibPath, jstring javaCpuAbi,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100493 jboolean extractNativeLibs, jboolean hasNativeBridge, jboolean debuggable)
Kenny Root66269ea2011-07-12 14:14:01 -0700494{
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700495 void* args[] = { &javaNativeLibPath, &extractNativeLibs, &hasNativeBridge };
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100496 return (jint) iterateOverNativeFiles(env, apkHandle, javaCpuAbi, debuggable,
Dmitriy Ivanovdec46882014-09-30 15:10:48 -0700497 copyFileIfChanged, reinterpret_cast<void*>(args));
Kenny Root66269ea2011-07-12 14:14:01 -0700498}
499
500static jlong
501com_android_internal_content_NativeLibraryHelper_sumNativeBinaries(JNIEnv *env, jclass clazz,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100502 jlong apkHandle, jstring javaCpuAbi, jboolean debuggable)
Kenny Root66269ea2011-07-12 14:14:01 -0700503{
504 size_t totalSize = 0;
505
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100506 iterateOverNativeFiles(env, apkHandle, javaCpuAbi, debuggable, sumFiles, &totalSize);
Kenny Root66269ea2011-07-12 14:14:01 -0700507
508 return totalSize;
509}
510
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000511static jint
512com_android_internal_content_NativeLibraryHelper_findSupportedAbi(JNIEnv *env, jclass clazz,
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100513 jlong apkHandle, jobjectArray javaCpuAbisToSearch, jboolean debuggable)
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000514{
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100515 return (jint) findSupportedAbi(env, apkHandle, javaCpuAbisToSearch, debuggable);
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000516}
517
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100518enum bitcode_scan_result_t {
519 APK_SCAN_ERROR = -1,
520 NO_BITCODE_PRESENT = 0,
521 BITCODE_PRESENT = 1,
522};
523
524static jint
525com_android_internal_content_NativeLibraryHelper_hasRenderscriptBitcode(JNIEnv *env, jclass clazz,
526 jlong apkHandle) {
527 ZipFileRO* zipFile = reinterpret_cast<ZipFileRO*>(apkHandle);
528 void* cookie = NULL;
Yusuke Sato957c2372015-08-03 16:17:16 -0700529 if (!zipFile->startIteration(&cookie, NULL /* prefix */, RS_BITCODE_SUFFIX)) {
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100530 return APK_SCAN_ERROR;
531 }
532
533 char fileName[PATH_MAX];
534 ZipEntryRO next = NULL;
535 while ((next = zipFile->nextEntry(cookie)) != NULL) {
536 if (zipFile->getEntryFileName(next, fileName, sizeof(fileName))) {
537 continue;
538 }
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100539 const char* lastSlash = strrchr(fileName, '/');
540 const char* baseName = (lastSlash == NULL) ? fileName : fileName + 1;
Yusuke Sato957c2372015-08-03 16:17:16 -0700541 if (isFilenameSafe(baseName)) {
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100542 zipFile->endIteration(cookie);
543 return BITCODE_PRESENT;
544 }
545 }
546
547 zipFile->endIteration(cookie);
548 return NO_BITCODE_PRESENT;
549}
550
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000551static jlong
552com_android_internal_content_NativeLibraryHelper_openApk(JNIEnv *env, jclass, jstring apkPath)
553{
554 ScopedUtfChars filePath(env, apkPath);
555 ZipFileRO* zipFile = ZipFileRO::open(filePath.c_str());
556
557 return reinterpret_cast<jlong>(zipFile);
558}
559
560static void
561com_android_internal_content_NativeLibraryHelper_close(JNIEnv *env, jclass, jlong apkHandle)
562{
563 delete reinterpret_cast<ZipFileRO*>(apkHandle);
564}
565
Daniel Micay76f6a862015-09-19 17:31:01 -0400566static const JNINativeMethod gMethods[] = {
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000567 {"nativeOpenApk",
568 "(Ljava/lang/String;)J",
569 (void *)com_android_internal_content_NativeLibraryHelper_openApk},
570 {"nativeClose",
571 "(J)V",
572 (void *)com_android_internal_content_NativeLibraryHelper_close},
Kenny Root66269ea2011-07-12 14:14:01 -0700573 {"nativeCopyNativeBinaries",
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100574 "(JLjava/lang/String;Ljava/lang/String;ZZZ)I",
Kenny Root66269ea2011-07-12 14:14:01 -0700575 (void *)com_android_internal_content_NativeLibraryHelper_copyNativeBinaries},
576 {"nativeSumNativeBinaries",
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100577 "(JLjava/lang/String;Z)J",
Kenny Root66269ea2011-07-12 14:14:01 -0700578 (void *)com_android_internal_content_NativeLibraryHelper_sumNativeBinaries},
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000579 {"nativeFindSupportedAbi",
Tamas Berghammerd6dd6b82016-10-07 19:12:51 +0100580 "(J[Ljava/lang/String;Z)I",
Ramin Zaghi1378aba2014-02-28 15:03:19 +0000581 (void *)com_android_internal_content_NativeLibraryHelper_findSupportedAbi},
Narayan Kamathd47e38b2014-05-16 16:45:56 +0100582 {"hasRenderscriptBitcode", "(J)I",
583 (void *)com_android_internal_content_NativeLibraryHelper_hasRenderscriptBitcode},
Kenny Root66269ea2011-07-12 14:14:01 -0700584};
585
586
587int register_com_android_internal_content_NativeLibraryHelper(JNIEnv *env)
588{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800589 return RegisterMethodsOrDie(env,
590 "com/android/internal/content/NativeLibraryHelper", gMethods, NELEM(gMethods));
Kenny Root66269ea2011-07-12 14:14:01 -0700591}
592
593};