blob: 408b1f7a6bb80a47a6ec4b1cb326d1444507caa5 [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
Logane7eb7732011-01-07 07:11:56 +080052ScriptCached *CacheReader::readCacheFile(FileHandle *file, Script *S) {
Loganf7f0ac52011-01-07 03:53:43 +080053 // Check file handle
54 if (!file || file->getFD() < 0) {
55 return NULL;
56 }
57
Logana2e15af2011-01-07 11:46:08 +080058 mFile = file;
59
Loganf7f0ac52011-01-07 03:53:43 +080060 // Allocate ScriptCached object
Logane7eb7732011-01-07 07:11:56 +080061 mpResult.reset(new (nothrow) ScriptCached(S));
Loganf7f0ac52011-01-07 03:53:43 +080062
Logan856ceb22011-01-07 05:21:26 +080063 if (!mpResult) {
Loganf7f0ac52011-01-07 03:53:43 +080064 LOGE("Unable to allocate ScriptCached object.\n");
65 return NULL;
66 }
67
68 bool result = checkFileSize()
69 && readHeader()
70 && checkHeader()
71 && checkMachineIntType()
72 && checkSectionOffsetAndSize()
73 && readStringPool()
74 && checkStringPool()
75 && readDependencyTable()
76 && checkDependency()
77 && readExportVarList()
78 && readExportFuncList()
79 && readPragmaList()
80 && readFuncTable()
Stephen Hines071288a2011-01-27 14:38:26 -080081 && readObjectSlotList()
Loganf7f0ac52011-01-07 03:53:43 +080082 && readContext()
83 && checkContext()
84 //&& readRelocationTable()
85 //&& relocate()
86 ;
87
Logan856ceb22011-01-07 05:21:26 +080088 return result ? mpResult.take() : NULL;
Loganf7f0ac52011-01-07 03:53:43 +080089}
90
91
92bool CacheReader::checkFileSize() {
93 struct stat stfile;
94 if (fstat(mFile->getFD(), &stfile) < 0) {
95 LOGE("Unable to stat cache file.\n");
96 return false;
97 }
98
99 mFileSize = stfile.st_size;
100
101 if (mFileSize < (off_t)sizeof(OBCC_Header) ||
Logan1dc63142011-02-25 17:14:51 +0800102 mFileSize < (off_t)ContextManager::ContextSize) {
Loganf7f0ac52011-01-07 03:53:43 +0800103 LOGE("Cache file is too small to be correct.\n");
104 return false;
105 }
106
107 return true;
108}
109
110
111bool CacheReader::readHeader() {
112 if (mFile->seek(0, SEEK_SET) != 0) {
113 LOGE("Unable to seek to 0. (reason: %s)\n", strerror(errno));
114 return false;
115 }
116
Logan856ceb22011-01-07 05:21:26 +0800117 mpHeader = (OBCC_Header *)malloc(sizeof(OBCC_Header));
118 if (!mpHeader) {
Loganf7f0ac52011-01-07 03:53:43 +0800119 LOGE("Unable to allocate for cache header.\n");
120 return false;
121 }
122
Logan856ceb22011-01-07 05:21:26 +0800123 if (mFile->read(reinterpret_cast<char *>(mpHeader), sizeof(OBCC_Header)) !=
Loganf7f0ac52011-01-07 03:53:43 +0800124 (ssize_t)sizeof(OBCC_Header)) {
125 LOGE("Unable to read cache header.\n");
126 return false;
127 }
128
Logana2e15af2011-01-07 11:46:08 +0800129 // Dirty hack for libRS.
130 // TODO(all): This should be removed in the future.
131 if (mpHeader->libRS_threadable) {
132 mpResult->mLibRSThreadable = true;
133 }
134
Loganf7f0ac52011-01-07 03:53:43 +0800135 return true;
136}
137
138
139bool CacheReader::checkHeader() {
Logan856ceb22011-01-07 05:21:26 +0800140 if (memcmp(mpHeader->magic, OBCC_MAGIC, 4) != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800141 LOGE("Bad magic word\n");
142 return false;
143 }
144
Logan856ceb22011-01-07 05:21:26 +0800145 if (memcmp(mpHeader->version, OBCC_VERSION, 4) != 0) {
Logane1323992011-01-12 04:47:13 +0800146 mpHeader->version[4 - 1] = '\0'; // ensure c-style string terminated
Shih-wei Liaoa6526652011-01-12 04:42:07 -0800147 LOGI("Cache file format version mismatch: now %s cached %s\n",
Logane1323992011-01-12 04:47:13 +0800148 OBCC_VERSION, mpHeader->version);
149 return false;
150 }
Stephen Hinesd18a3df2011-06-24 17:38:56 -0700151
152 if (memcmp(mpHeader->libbcc_build_checksum, libbcc_build_checksum, 41) != 0) {
153 mpHeader->libbcc_build_checksum[41 - 1] = '\0'; // ensure terminated
154 LOGW("Build checksum mismatch: lib %s cached %s\n", libbcc_build_checksum,
155 mpHeader->libbcc_build_checksum);
156 return false;
157 }
158
Loganf7f0ac52011-01-07 03:53:43 +0800159 return true;
160}
161
162
163bool CacheReader::checkMachineIntType() {
164 uint32_t number = 0x00000001;
165
166 bool isLittleEndian = (*reinterpret_cast<char *>(&number) == 1);
Logan856ceb22011-01-07 05:21:26 +0800167 if ((isLittleEndian && mpHeader->endianness != 'e') ||
168 (!isLittleEndian && mpHeader->endianness != 'E')) {
Loganf7f0ac52011-01-07 03:53:43 +0800169 LOGE("Machine endianness mismatch.\n");
170 return false;
171 }
172
Logan856ceb22011-01-07 05:21:26 +0800173 if ((unsigned int)mpHeader->sizeof_off_t != sizeof(off_t) ||
174 (unsigned int)mpHeader->sizeof_size_t != sizeof(size_t) ||
175 (unsigned int)mpHeader->sizeof_ptr_t != sizeof(void *)) {
Loganf7f0ac52011-01-07 03:53:43 +0800176 LOGE("Machine integer size mismatch.\n");
177 return false;
178 }
179
180 return true;
181}
182
183
184bool CacheReader::checkSectionOffsetAndSize() {
185#define CHECK_SECTION_OFFSET(NAME) \
186 do { \
Logan856ceb22011-01-07 05:21:26 +0800187 off_t offset = mpHeader-> NAME##_offset; \
188 off_t size = (off_t)mpHeader-> NAME##_size; \
Loganf7f0ac52011-01-07 03:53:43 +0800189 \
190 if (mFileSize < offset || mFileSize < offset + size) { \
191 LOGE(#NAME " section overflow.\n"); \
192 return false; \
193 } \
194 \
195 if (offset % sizeof(int) != 0) { \
Stephen Hines40b7fba2011-03-17 19:50:20 -0700196 LOGE(#NAME " offset must aligned to %d.\n", (int)sizeof(int)); \
Loganf7f0ac52011-01-07 03:53:43 +0800197 return false; \
198 } \
199 \
200 if (size < static_cast<off_t>(sizeof(size_t))) { \
201 LOGE(#NAME " size is too small to be correct.\n"); \
202 return false; \
203 } \
204 } while (0)
205
206 CHECK_SECTION_OFFSET(str_pool);
207 CHECK_SECTION_OFFSET(depend_tab);
Logana2e15af2011-01-07 11:46:08 +0800208 //CHECK_SECTION_OFFSET(reloc_tab);
Loganf7f0ac52011-01-07 03:53:43 +0800209 CHECK_SECTION_OFFSET(export_var_list);
210 CHECK_SECTION_OFFSET(export_func_list);
211 CHECK_SECTION_OFFSET(pragma_list);
212
213#undef CHECK_SECTION_OFFSET
214
Logan856ceb22011-01-07 05:21:26 +0800215 if (mFileSize < mpHeader->context_offset ||
Logan1dc63142011-02-25 17:14:51 +0800216 mFileSize < (off_t)mpHeader->context_offset +
217 (off_t)ContextManager::ContextSize) {
Loganf7f0ac52011-01-07 03:53:43 +0800218 LOGE("context section overflow.\n");
219 return false;
220 }
221
222 long pagesize = sysconf(_SC_PAGESIZE);
Logan856ceb22011-01-07 05:21:26 +0800223 if (mpHeader->context_offset % pagesize != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800224 LOGE("context offset must aligned to pagesize.\n");
225 return false;
226 }
227
228 // TODO(logan): Move this to some where else.
Logan856ceb22011-01-07 05:21:26 +0800229 if ((uintptr_t)mpHeader->context_cached_addr % pagesize != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800230 LOGE("cached address is not aligned to pagesize.\n");
231 return false;
232 }
233
234 return true;
235}
236
237
Logan856ceb22011-01-07 05:21:26 +0800238#define CACHE_READER_READ_SECTION(TYPE, AUTO_MANAGED_HOLDER, NAME) \
239 TYPE *NAME##_raw = (TYPE *)malloc(mpHeader->NAME##_size); \
240 \
241 if (!NAME##_raw) { \
242 LOGE("Unable to allocate for " #NAME "\n"); \
243 return false; \
244 } \
245 \
246 /* We have to ensure that some one will deallocate NAME##_raw */ \
247 AUTO_MANAGED_HOLDER = NAME##_raw; \
248 \
249 if (mFile->seek(mpHeader->NAME##_offset, SEEK_SET) == -1) { \
250 LOGE("Unable to seek to " #NAME " section\n"); \
251 return false; \
252 } \
253 \
254 if (mFile->read(reinterpret_cast<char *>(NAME##_raw), \
255 mpHeader->NAME##_size) != (ssize_t)mpHeader->NAME##_size) \
256 { \
257 LOGE("Unable to read " #NAME ".\n"); \
258 return false; \
259 }
260
261
Loganf7f0ac52011-01-07 03:53:43 +0800262bool CacheReader::readStringPool() {
Logan856ceb22011-01-07 05:21:26 +0800263 CACHE_READER_READ_SECTION(OBCC_StringPool,
264 mpResult->mpStringPoolRaw, str_pool);
Loganf7f0ac52011-01-07 03:53:43 +0800265
Logan856ceb22011-01-07 05:21:26 +0800266 char *str_base = reinterpret_cast<char *>(str_pool_raw);
Loganf7f0ac52011-01-07 03:53:43 +0800267
Logan856ceb22011-01-07 05:21:26 +0800268 vector<char const *> &pool = mpResult->mStringPool;
269 for (size_t i = 0; i < str_pool_raw->count; ++i) {
270 char *str = str_base + str_pool_raw->list[i].offset;
Loganf7f0ac52011-01-07 03:53:43 +0800271 pool.push_back(str);
272 }
273
274 return true;
275}
276
277
278bool CacheReader::checkStringPool() {
Logan856ceb22011-01-07 05:21:26 +0800279 OBCC_StringPool *poolR = mpResult->mpStringPoolRaw;
280 vector<char const *> &pool = mpResult->mStringPool;
Loganf7f0ac52011-01-07 03:53:43 +0800281
282 // Ensure that every c-style string is ended with '\0'
283 for (size_t i = 0; i < poolR->count; ++i) {
284 if (pool[i][poolR->list[i].length] != '\0') {
285 LOGE("The %lu-th string does not end with '\\0'.\n", (unsigned long)i);
286 return false;
287 }
288 }
289
290 return true;
291}
292
293
294bool CacheReader::readDependencyTable() {
Logan856ceb22011-01-07 05:21:26 +0800295 CACHE_READER_READ_SECTION(OBCC_DependencyTable, mpCachedDependTable,
296 depend_tab);
Loganf7f0ac52011-01-07 03:53:43 +0800297 return true;
298}
299
300
301bool CacheReader::checkDependency() {
Logan856ceb22011-01-07 05:21:26 +0800302 if (mDependencies.size() != mpCachedDependTable->count) {
303 LOGE("Dependencies count mismatch. (%lu vs %lu)\n",
304 (unsigned long)mDependencies.size(),
305 (unsigned long)mpCachedDependTable->count);
306 return false;
307 }
308
309 vector<char const *> &strPool = mpResult->mStringPool;
Logan75cc8a52011-01-07 06:06:52 +0800310 map<string, pair<uint32_t, unsigned char const *> >::iterator dep;
Logan856ceb22011-01-07 05:21:26 +0800311
312 dep = mDependencies.begin();
313 for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) {
314 string const &depName = dep->first;
Logan856ceb22011-01-07 05:21:26 +0800315 uint32_t depType = dep->second.first;
Logan75cc8a52011-01-07 06:06:52 +0800316 unsigned char const *depSHA1 = dep->second.second;
Logan856ceb22011-01-07 05:21:26 +0800317
318 OBCC_Dependency *depCached =&mpCachedDependTable->table[i];
319 char const *depCachedName = strPool[depCached->res_name_strp_index];
Logan856ceb22011-01-07 05:21:26 +0800320 uint32_t depCachedType = depCached->res_type;
Logan75cc8a52011-01-07 06:06:52 +0800321 unsigned char const *depCachedSHA1 = depCached->sha1;
Logan856ceb22011-01-07 05:21:26 +0800322
323 if (depName != depCachedName) {
324 LOGE("Cache dependency name mismatch:\n");
325 LOGE(" given: %s\n", depName.c_str());
326 LOGE(" cached: %s\n", depCachedName);
327
328 return false;
329 }
330
331 if (memcmp(depSHA1, depCachedSHA1, 20) != 0) {
332 LOGE("Cache dependency %s sha1 mismatch:\n", depCachedName);
333
334#define PRINT_SHA1(PREFIX, X, POSTFIX) \
335 LOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
336 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \
337 X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], \
338 X[10],X[11],X[12],X[13],X[14],X[15],X[16],X[17],X[18],X[19]);
339
340 PRINT_SHA1(" given: ", depSHA1, "\n");
341 PRINT_SHA1(" cached: ", depCachedSHA1, "\n");
342
343#undef PRINT_SHA1
344
345 return false;
346 }
347
348 if (depType != depCachedType) {
349 LOGE("Cache dependency %s resource type mismatch.\n", depCachedName);
350 return false;
351 }
352 }
353
Loganf7f0ac52011-01-07 03:53:43 +0800354 return true;
355}
356
357bool CacheReader::readExportVarList() {
Logan856ceb22011-01-07 05:21:26 +0800358 CACHE_READER_READ_SECTION(OBCC_ExportVarList,
359 mpResult->mpExportVars, export_var_list);
Loganf7f0ac52011-01-07 03:53:43 +0800360 return true;
361}
362
363
364bool CacheReader::readExportFuncList() {
Logan856ceb22011-01-07 05:21:26 +0800365 CACHE_READER_READ_SECTION(OBCC_ExportFuncList,
366 mpResult->mpExportFuncs, export_func_list);
Loganf7f0ac52011-01-07 03:53:43 +0800367 return true;
368}
369
370
371bool CacheReader::readPragmaList() {
Logan856ceb22011-01-07 05:21:26 +0800372 CACHE_READER_READ_SECTION(OBCC_PragmaList, mpPragmaList, pragma_list);
Loganf7f0ac52011-01-07 03:53:43 +0800373
Logan856ceb22011-01-07 05:21:26 +0800374 vector<char const *> const &strPool = mpResult->mStringPool;
375 ScriptCached::PragmaList &pragmas = mpResult->mPragmas;
Loganf7f0ac52011-01-07 03:53:43 +0800376
Logan856ceb22011-01-07 05:21:26 +0800377 for (size_t i = 0; i < pragma_list_raw->count; ++i) {
378 OBCC_Pragma *pragma = &pragma_list_raw->list[i];
Loganf7f0ac52011-01-07 03:53:43 +0800379 pragmas.push_back(make_pair(strPool[pragma->key_strp_index],
380 strPool[pragma->value_strp_index]));
381 }
382
383 return true;
384}
385
386
Stephen Hines071288a2011-01-27 14:38:26 -0800387bool CacheReader::readObjectSlotList() {
388 CACHE_READER_READ_SECTION(OBCC_ObjectSlotList,
389 mpResult->mpObjectSlotList, object_slot_list);
390 return true;
391}
392
393
Loganf7f0ac52011-01-07 03:53:43 +0800394bool CacheReader::readFuncTable() {
Logan856ceb22011-01-07 05:21:26 +0800395 CACHE_READER_READ_SECTION(OBCC_FuncTable, mpFuncTable, func_table);
396
397 vector<char const *> &strPool = mpResult->mStringPool;
398 ScriptCached::FuncTable &table = mpResult->mFunctions;
399 for (size_t i = 0; i < func_table_raw->count; ++i) {
400 OBCC_FuncInfo *func = &func_table_raw->table[i];
401 table.insert(make_pair(strPool[func->name_strp_index],
402 make_pair(func->cached_addr, func->size)));
403 }
404
405 return true;
Loganf7f0ac52011-01-07 03:53:43 +0800406}
407
Logan856ceb22011-01-07 05:21:26 +0800408#undef CACHE_READER_READ_SECTION
409
Loganf7f0ac52011-01-07 03:53:43 +0800410
411bool CacheReader::readContext() {
Logan1dc63142011-02-25 17:14:51 +0800412 mpResult->mContext =
413 ContextManager::get().allocateContext(mpHeader->context_cached_addr,
414 mFile->getFD(),
415 mpHeader->context_offset);
Loganf7f0ac52011-01-07 03:53:43 +0800416
Logan856ceb22011-01-07 05:21:26 +0800417 if (!mpResult->mContext) {
Loganf7f0ac52011-01-07 03:53:43 +0800418 // Unable to allocate at cached address. Give up.
Logan42598052011-01-26 22:41:13 +0800419 mIsContextSlotNotAvail = true;
Loganf7f0ac52011-01-07 03:53:43 +0800420 return false;
421
422 // TODO(logan): If relocation is fixed, we should try to allocate the
423 // code in different location, and relocate the context.
424 }
425
426 return true;
427}
428
429
430bool CacheReader::checkContext() {
Logan856ceb22011-01-07 05:21:26 +0800431 uint32_t sum = mpHeader->context_parity_checksum;
432 uint32_t *ptr = reinterpret_cast<uint32_t *>(mpResult->mContext);
Loganf7f0ac52011-01-07 03:53:43 +0800433
Logan1dc63142011-02-25 17:14:51 +0800434 for (size_t i = 0; i < ContextManager::ContextSize / sizeof(uint32_t); ++i) {
Loganf7f0ac52011-01-07 03:53:43 +0800435 sum ^= *ptr++;
436 }
437
438 if (sum != 0) {
439 LOGE("Checksum check failed\n");
440 return false;
441 }
442
443 LOGI("Passed checksum even parity verification.\n");
444 return true;
445}
446
447
448bool CacheReader::readRelocationTable() {
449 // TODO(logan): Not finished.
450 return true;
451}
452
453
454bool CacheReader::relocate() {
455 // TODO(logan): Not finished.
456 return true;
457}
458
459
460} // namespace bcc