blob: c11a9e7d51348c087ecd28ce28fa723418c02598 [file] [log] [blame]
Loganf7f0ac52011-01-07 03:53:43 +08001/*
2 * Copyright 2010, 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
Loganf7f0ac52011-01-07 03:53:43 +080017#include "CacheReader.h"
18
19#include "ContextManager.h"
Logan4dcd6792011-02-28 05:12:00 +080020#include "DebugHelper.h"
Loganf7f0ac52011-01-07 03:53:43 +080021#include "FileHandle.h"
22#include "ScriptCached.h"
23
24#include <bcc/bcc_cache.h>
25
26#include <llvm/ADT/OwningPtr.h>
27
28#include <errno.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31
32#include <utility>
33#include <vector>
34
35#include <new>
36
Logan856ceb22011-01-07 05:21:26 +080037#include <stdlib.h>
Loganf7f0ac52011-01-07 03:53:43 +080038#include <string.h>
39
40using namespace std;
41
42
43namespace bcc {
44
Logan856ceb22011-01-07 05:21:26 +080045CacheReader::~CacheReader() {
46 if (mpHeader) { free(mpHeader); }
47 if (mpCachedDependTable) { free(mpCachedDependTable); }
48 if (mpPragmaList) { free(mpPragmaList); }
49 if (mpFuncTable) { free(mpFuncTable); }
50}
51
Logan Chien03a2e302011-07-13 21:46:32 +080052ScriptCached *CacheReader::readCacheFile(FileHandle *objFile,
53 FileHandle *infoFile,
54 Script *S) {
Loganf7f0ac52011-01-07 03:53:43 +080055 // Check file handle
Logan Chien03a2e302011-07-13 21:46:32 +080056 if (!objFile || objFile->getFD() < 0 ||
57 !infoFile || infoFile->getFD() < 0) {
Loganf7f0ac52011-01-07 03:53:43 +080058 return NULL;
59 }
60
Logan Chien03a2e302011-07-13 21:46:32 +080061 mObjFile = objFile;
62 mInfoFile = infoFile;
Logana2e15af2011-01-07 11:46:08 +080063
Loganf7f0ac52011-01-07 03:53:43 +080064 // Allocate ScriptCached object
Logane7eb7732011-01-07 07:11:56 +080065 mpResult.reset(new (nothrow) ScriptCached(S));
Loganf7f0ac52011-01-07 03:53:43 +080066
Logan856ceb22011-01-07 05:21:26 +080067 if (!mpResult) {
Loganf7f0ac52011-01-07 03:53:43 +080068 LOGE("Unable to allocate ScriptCached object.\n");
69 return NULL;
70 }
71
72 bool result = checkFileSize()
73 && readHeader()
74 && checkHeader()
75 && checkMachineIntType()
76 && checkSectionOffsetAndSize()
77 && readStringPool()
78 && checkStringPool()
79 && readDependencyTable()
80 && checkDependency()
81 && readExportVarList()
82 && readExportFuncList()
83 && readPragmaList()
84 && readFuncTable()
Stephen Hines071288a2011-01-27 14:38:26 -080085 && readObjectSlotList()
Loganf7f0ac52011-01-07 03:53:43 +080086 && readContext()
87 && checkContext()
88 //&& readRelocationTable()
89 //&& relocate()
90 ;
91
Logan856ceb22011-01-07 05:21:26 +080092 return result ? mpResult.take() : NULL;
Loganf7f0ac52011-01-07 03:53:43 +080093}
94
95
96bool CacheReader::checkFileSize() {
97 struct stat stfile;
Logan Chien03a2e302011-07-13 21:46:32 +080098
99 if (fstat(mInfoFile->getFD(), &stfile) < 0) {
100 LOGE("Unable to stat metadata information file.\n");
Loganf7f0ac52011-01-07 03:53:43 +0800101 return false;
102 }
103
Logan Chien03a2e302011-07-13 21:46:32 +0800104 mInfoFileSize = stfile.st_size;
Loganf7f0ac52011-01-07 03:53:43 +0800105
Logan Chien03a2e302011-07-13 21:46:32 +0800106 if (mInfoFileSize < (off_t)sizeof(OBCC_Header)) {
107 LOGE("Metadata information file is too small to be correct.\n");
108 return false;
109 }
110
111 if (fstat(mObjFile->getFD(), &stfile) < 0) {
112 LOGE("Unable to stat executable file.\n");
113 return false;
114 }
115
116 if (stfile.st_size < (off_t)ContextManager::ContextSize) {
117 LOGE("Executable file is too small to be correct.\n");
Loganf7f0ac52011-01-07 03:53:43 +0800118 return false;
119 }
120
121 return true;
122}
123
124
125bool CacheReader::readHeader() {
Logan Chien03a2e302011-07-13 21:46:32 +0800126 if (mInfoFile->seek(0, SEEK_SET) != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800127 LOGE("Unable to seek to 0. (reason: %s)\n", strerror(errno));
128 return false;
129 }
130
Logan856ceb22011-01-07 05:21:26 +0800131 mpHeader = (OBCC_Header *)malloc(sizeof(OBCC_Header));
132 if (!mpHeader) {
Loganf7f0ac52011-01-07 03:53:43 +0800133 LOGE("Unable to allocate for cache header.\n");
134 return false;
135 }
136
Logan Chien03a2e302011-07-13 21:46:32 +0800137 if (mInfoFile->read((char *)mpHeader, sizeof(OBCC_Header)) !=
Loganf7f0ac52011-01-07 03:53:43 +0800138 (ssize_t)sizeof(OBCC_Header)) {
139 LOGE("Unable to read cache header.\n");
140 return false;
141 }
142
Logana2e15af2011-01-07 11:46:08 +0800143 // Dirty hack for libRS.
144 // TODO(all): This should be removed in the future.
145 if (mpHeader->libRS_threadable) {
146 mpResult->mLibRSThreadable = true;
147 }
148
Loganf7f0ac52011-01-07 03:53:43 +0800149 return true;
150}
151
152
153bool CacheReader::checkHeader() {
Logan856ceb22011-01-07 05:21:26 +0800154 if (memcmp(mpHeader->magic, OBCC_MAGIC, 4) != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800155 LOGE("Bad magic word\n");
156 return false;
157 }
158
Logan856ceb22011-01-07 05:21:26 +0800159 if (memcmp(mpHeader->version, OBCC_VERSION, 4) != 0) {
Logane1323992011-01-12 04:47:13 +0800160 mpHeader->version[4 - 1] = '\0'; // ensure c-style string terminated
Shih-wei Liaoa6526652011-01-12 04:42:07 -0800161 LOGI("Cache file format version mismatch: now %s cached %s\n",
Logane1323992011-01-12 04:47:13 +0800162 OBCC_VERSION, mpHeader->version);
163 return false;
164 }
Loganf7f0ac52011-01-07 03:53:43 +0800165 return true;
166}
167
168
169bool CacheReader::checkMachineIntType() {
170 uint32_t number = 0x00000001;
171
172 bool isLittleEndian = (*reinterpret_cast<char *>(&number) == 1);
Logan856ceb22011-01-07 05:21:26 +0800173 if ((isLittleEndian && mpHeader->endianness != 'e') ||
174 (!isLittleEndian && mpHeader->endianness != 'E')) {
Loganf7f0ac52011-01-07 03:53:43 +0800175 LOGE("Machine endianness mismatch.\n");
176 return false;
177 }
178
Logan856ceb22011-01-07 05:21:26 +0800179 if ((unsigned int)mpHeader->sizeof_off_t != sizeof(off_t) ||
180 (unsigned int)mpHeader->sizeof_size_t != sizeof(size_t) ||
181 (unsigned int)mpHeader->sizeof_ptr_t != sizeof(void *)) {
Loganf7f0ac52011-01-07 03:53:43 +0800182 LOGE("Machine integer size mismatch.\n");
183 return false;
184 }
185
186 return true;
187}
188
189
190bool CacheReader::checkSectionOffsetAndSize() {
191#define CHECK_SECTION_OFFSET(NAME) \
192 do { \
Logan856ceb22011-01-07 05:21:26 +0800193 off_t offset = mpHeader-> NAME##_offset; \
194 off_t size = (off_t)mpHeader-> NAME##_size; \
Loganf7f0ac52011-01-07 03:53:43 +0800195 \
Logan Chien03a2e302011-07-13 21:46:32 +0800196 if (mInfoFileSize < offset || mInfoFileSize < offset + size) { \
Loganf7f0ac52011-01-07 03:53:43 +0800197 LOGE(#NAME " section overflow.\n"); \
198 return false; \
199 } \
200 \
201 if (offset % sizeof(int) != 0) { \
Stephen Hines40b7fba2011-03-17 19:50:20 -0700202 LOGE(#NAME " offset must aligned to %d.\n", (int)sizeof(int)); \
Loganf7f0ac52011-01-07 03:53:43 +0800203 return false; \
204 } \
205 \
206 if (size < static_cast<off_t>(sizeof(size_t))) { \
207 LOGE(#NAME " size is too small to be correct.\n"); \
208 return false; \
209 } \
210 } while (0)
211
212 CHECK_SECTION_OFFSET(str_pool);
213 CHECK_SECTION_OFFSET(depend_tab);
Logana2e15af2011-01-07 11:46:08 +0800214 //CHECK_SECTION_OFFSET(reloc_tab);
Loganf7f0ac52011-01-07 03:53:43 +0800215 CHECK_SECTION_OFFSET(export_var_list);
216 CHECK_SECTION_OFFSET(export_func_list);
217 CHECK_SECTION_OFFSET(pragma_list);
218
219#undef CHECK_SECTION_OFFSET
220
Loganf7f0ac52011-01-07 03:53:43 +0800221 // TODO(logan): Move this to some where else.
Logan Chien03a2e302011-07-13 21:46:32 +0800222 long pagesize = sysconf(_SC_PAGESIZE);
Logan856ceb22011-01-07 05:21:26 +0800223 if ((uintptr_t)mpHeader->context_cached_addr % pagesize != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800224 LOGE("cached address is not aligned to pagesize.\n");
225 return false;
226 }
227
228 return true;
229}
230
231
Logan856ceb22011-01-07 05:21:26 +0800232#define CACHE_READER_READ_SECTION(TYPE, AUTO_MANAGED_HOLDER, NAME) \
233 TYPE *NAME##_raw = (TYPE *)malloc(mpHeader->NAME##_size); \
234 \
235 if (!NAME##_raw) { \
236 LOGE("Unable to allocate for " #NAME "\n"); \
237 return false; \
238 } \
239 \
240 /* We have to ensure that some one will deallocate NAME##_raw */ \
241 AUTO_MANAGED_HOLDER = NAME##_raw; \
242 \
Logan Chien03a2e302011-07-13 21:46:32 +0800243 if (mInfoFile->seek(mpHeader->NAME##_offset, SEEK_SET) == -1) { \
Logan856ceb22011-01-07 05:21:26 +0800244 LOGE("Unable to seek to " #NAME " section\n"); \
245 return false; \
246 } \
247 \
Logan Chien03a2e302011-07-13 21:46:32 +0800248 if (mInfoFile->read(reinterpret_cast<char *>(NAME##_raw), \
Logan856ceb22011-01-07 05:21:26 +0800249 mpHeader->NAME##_size) != (ssize_t)mpHeader->NAME##_size) \
250 { \
251 LOGE("Unable to read " #NAME ".\n"); \
252 return false; \
253 }
254
255
Loganf7f0ac52011-01-07 03:53:43 +0800256bool CacheReader::readStringPool() {
Logan856ceb22011-01-07 05:21:26 +0800257 CACHE_READER_READ_SECTION(OBCC_StringPool,
258 mpResult->mpStringPoolRaw, str_pool);
Loganf7f0ac52011-01-07 03:53:43 +0800259
Logan856ceb22011-01-07 05:21:26 +0800260 char *str_base = reinterpret_cast<char *>(str_pool_raw);
Loganf7f0ac52011-01-07 03:53:43 +0800261
Logan856ceb22011-01-07 05:21:26 +0800262 vector<char const *> &pool = mpResult->mStringPool;
263 for (size_t i = 0; i < str_pool_raw->count; ++i) {
264 char *str = str_base + str_pool_raw->list[i].offset;
Loganf7f0ac52011-01-07 03:53:43 +0800265 pool.push_back(str);
266 }
267
268 return true;
269}
270
271
272bool CacheReader::checkStringPool() {
Logan856ceb22011-01-07 05:21:26 +0800273 OBCC_StringPool *poolR = mpResult->mpStringPoolRaw;
274 vector<char const *> &pool = mpResult->mStringPool;
Loganf7f0ac52011-01-07 03:53:43 +0800275
276 // Ensure that every c-style string is ended with '\0'
277 for (size_t i = 0; i < poolR->count; ++i) {
278 if (pool[i][poolR->list[i].length] != '\0') {
279 LOGE("The %lu-th string does not end with '\\0'.\n", (unsigned long)i);
280 return false;
281 }
282 }
283
284 return true;
285}
286
287
288bool CacheReader::readDependencyTable() {
Logan856ceb22011-01-07 05:21:26 +0800289 CACHE_READER_READ_SECTION(OBCC_DependencyTable, mpCachedDependTable,
290 depend_tab);
Loganf7f0ac52011-01-07 03:53:43 +0800291 return true;
292}
293
294
295bool CacheReader::checkDependency() {
Logan856ceb22011-01-07 05:21:26 +0800296 if (mDependencies.size() != mpCachedDependTable->count) {
297 LOGE("Dependencies count mismatch. (%lu vs %lu)\n",
298 (unsigned long)mDependencies.size(),
299 (unsigned long)mpCachedDependTable->count);
300 return false;
301 }
302
303 vector<char const *> &strPool = mpResult->mStringPool;
Logan75cc8a52011-01-07 06:06:52 +0800304 map<string, pair<uint32_t, unsigned char const *> >::iterator dep;
Logan856ceb22011-01-07 05:21:26 +0800305
306 dep = mDependencies.begin();
307 for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) {
308 string const &depName = dep->first;
Logan856ceb22011-01-07 05:21:26 +0800309 uint32_t depType = dep->second.first;
Logan75cc8a52011-01-07 06:06:52 +0800310 unsigned char const *depSHA1 = dep->second.second;
Logan856ceb22011-01-07 05:21:26 +0800311
312 OBCC_Dependency *depCached =&mpCachedDependTable->table[i];
313 char const *depCachedName = strPool[depCached->res_name_strp_index];
Logan856ceb22011-01-07 05:21:26 +0800314 uint32_t depCachedType = depCached->res_type;
Logan75cc8a52011-01-07 06:06:52 +0800315 unsigned char const *depCachedSHA1 = depCached->sha1;
Logan856ceb22011-01-07 05:21:26 +0800316
317 if (depName != depCachedName) {
318 LOGE("Cache dependency name mismatch:\n");
319 LOGE(" given: %s\n", depName.c_str());
320 LOGE(" cached: %s\n", depCachedName);
321
322 return false;
323 }
324
325 if (memcmp(depSHA1, depCachedSHA1, 20) != 0) {
326 LOGE("Cache dependency %s sha1 mismatch:\n", depCachedName);
327
328#define PRINT_SHA1(PREFIX, X, POSTFIX) \
329 LOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
330 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \
331 X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], \
332 X[10],X[11],X[12],X[13],X[14],X[15],X[16],X[17],X[18],X[19]);
333
334 PRINT_SHA1(" given: ", depSHA1, "\n");
335 PRINT_SHA1(" cached: ", depCachedSHA1, "\n");
336
337#undef PRINT_SHA1
338
339 return false;
340 }
341
342 if (depType != depCachedType) {
343 LOGE("Cache dependency %s resource type mismatch.\n", depCachedName);
344 return false;
345 }
346 }
347
Loganf7f0ac52011-01-07 03:53:43 +0800348 return true;
349}
350
351bool CacheReader::readExportVarList() {
Logan856ceb22011-01-07 05:21:26 +0800352 CACHE_READER_READ_SECTION(OBCC_ExportVarList,
353 mpResult->mpExportVars, export_var_list);
Loganf7f0ac52011-01-07 03:53:43 +0800354 return true;
355}
356
357
358bool CacheReader::readExportFuncList() {
Logan856ceb22011-01-07 05:21:26 +0800359 CACHE_READER_READ_SECTION(OBCC_ExportFuncList,
360 mpResult->mpExportFuncs, export_func_list);
Loganf7f0ac52011-01-07 03:53:43 +0800361 return true;
362}
363
364
365bool CacheReader::readPragmaList() {
Logan856ceb22011-01-07 05:21:26 +0800366 CACHE_READER_READ_SECTION(OBCC_PragmaList, mpPragmaList, pragma_list);
Loganf7f0ac52011-01-07 03:53:43 +0800367
Logan856ceb22011-01-07 05:21:26 +0800368 vector<char const *> const &strPool = mpResult->mStringPool;
369 ScriptCached::PragmaList &pragmas = mpResult->mPragmas;
Loganf7f0ac52011-01-07 03:53:43 +0800370
Logan856ceb22011-01-07 05:21:26 +0800371 for (size_t i = 0; i < pragma_list_raw->count; ++i) {
372 OBCC_Pragma *pragma = &pragma_list_raw->list[i];
Loganf7f0ac52011-01-07 03:53:43 +0800373 pragmas.push_back(make_pair(strPool[pragma->key_strp_index],
374 strPool[pragma->value_strp_index]));
375 }
376
377 return true;
378}
379
380
Stephen Hines071288a2011-01-27 14:38:26 -0800381bool CacheReader::readObjectSlotList() {
382 CACHE_READER_READ_SECTION(OBCC_ObjectSlotList,
383 mpResult->mpObjectSlotList, object_slot_list);
384 return true;
385}
386
387
Loganf7f0ac52011-01-07 03:53:43 +0800388bool CacheReader::readFuncTable() {
Logan856ceb22011-01-07 05:21:26 +0800389 CACHE_READER_READ_SECTION(OBCC_FuncTable, mpFuncTable, func_table);
390
391 vector<char const *> &strPool = mpResult->mStringPool;
392 ScriptCached::FuncTable &table = mpResult->mFunctions;
393 for (size_t i = 0; i < func_table_raw->count; ++i) {
394 OBCC_FuncInfo *func = &func_table_raw->table[i];
395 table.insert(make_pair(strPool[func->name_strp_index],
396 make_pair(func->cached_addr, func->size)));
397 }
398
399 return true;
Loganf7f0ac52011-01-07 03:53:43 +0800400}
401
Logan856ceb22011-01-07 05:21:26 +0800402#undef CACHE_READER_READ_SECTION
403
Loganf7f0ac52011-01-07 03:53:43 +0800404
405bool CacheReader::readContext() {
Logan1dc63142011-02-25 17:14:51 +0800406 mpResult->mContext =
407 ContextManager::get().allocateContext(mpHeader->context_cached_addr,
Logan Chien03a2e302011-07-13 21:46:32 +0800408 mObjFile->getFD(), 0);
Loganf7f0ac52011-01-07 03:53:43 +0800409
Logan856ceb22011-01-07 05:21:26 +0800410 if (!mpResult->mContext) {
Loganf7f0ac52011-01-07 03:53:43 +0800411 // Unable to allocate at cached address. Give up.
Logan42598052011-01-26 22:41:13 +0800412 mIsContextSlotNotAvail = true;
Loganf7f0ac52011-01-07 03:53:43 +0800413 return false;
414
415 // TODO(logan): If relocation is fixed, we should try to allocate the
416 // code in different location, and relocate the context.
417 }
418
419 return true;
420}
421
422
423bool CacheReader::checkContext() {
Logan856ceb22011-01-07 05:21:26 +0800424 uint32_t sum = mpHeader->context_parity_checksum;
425 uint32_t *ptr = reinterpret_cast<uint32_t *>(mpResult->mContext);
Loganf7f0ac52011-01-07 03:53:43 +0800426
Logan1dc63142011-02-25 17:14:51 +0800427 for (size_t i = 0; i < ContextManager::ContextSize / sizeof(uint32_t); ++i) {
Loganf7f0ac52011-01-07 03:53:43 +0800428 sum ^= *ptr++;
429 }
430
431 if (sum != 0) {
432 LOGE("Checksum check failed\n");
433 return false;
434 }
435
436 LOGI("Passed checksum even parity verification.\n");
437 return true;
438}
439
440
441bool CacheReader::readRelocationTable() {
442 // TODO(logan): Not finished.
443 return true;
444}
445
446
447bool CacheReader::relocate() {
448 // TODO(logan): Not finished.
449 return true;
450}
451
452
453} // namespace bcc