blob: 7288b199606fb3bddc4ac1b4a3ab1afa4a42cf1a [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
17#define LOG_TAG "bcc"
18#include <cutils/log.h>
19
20#include "CacheReader.h"
21
22#include "ContextManager.h"
23#include "FileHandle.h"
24#include "ScriptCached.h"
25
26#include <bcc/bcc_cache.h>
27
28#include <llvm/ADT/OwningPtr.h>
29
30#include <errno.h>
31#include <sys/stat.h>
32#include <sys/types.h>
33
34#include <utility>
35#include <vector>
36
37#include <new>
38
Logan856ceb22011-01-07 05:21:26 +080039#include <stdlib.h>
Loganf7f0ac52011-01-07 03:53:43 +080040#include <string.h>
41
42using namespace std;
43
44
45namespace bcc {
46
Logan856ceb22011-01-07 05:21:26 +080047CacheReader::~CacheReader() {
48 if (mpHeader) { free(mpHeader); }
49 if (mpCachedDependTable) { free(mpCachedDependTable); }
50 if (mpPragmaList) { free(mpPragmaList); }
51 if (mpFuncTable) { free(mpFuncTable); }
52}
53
Logane7eb7732011-01-07 07:11:56 +080054ScriptCached *CacheReader::readCacheFile(FileHandle *file, Script *S) {
Loganf7f0ac52011-01-07 03:53:43 +080055 // Check file handle
56 if (!file || file->getFD() < 0) {
57 return NULL;
58 }
59
Logana2e15af2011-01-07 11:46:08 +080060 mFile = file;
61
Loganf7f0ac52011-01-07 03:53:43 +080062 // Allocate ScriptCached object
Logane7eb7732011-01-07 07:11:56 +080063 mpResult.reset(new (nothrow) ScriptCached(S));
Loganf7f0ac52011-01-07 03:53:43 +080064
Logan856ceb22011-01-07 05:21:26 +080065 if (!mpResult) {
Loganf7f0ac52011-01-07 03:53:43 +080066 LOGE("Unable to allocate ScriptCached object.\n");
67 return NULL;
68 }
69
70 bool result = checkFileSize()
71 && readHeader()
72 && checkHeader()
73 && checkMachineIntType()
74 && checkSectionOffsetAndSize()
75 && readStringPool()
76 && checkStringPool()
77 && readDependencyTable()
78 && checkDependency()
79 && readExportVarList()
80 && readExportFuncList()
81 && readPragmaList()
82 && readFuncTable()
Stephen Hines071288a2011-01-27 14:38:26 -080083 && readObjectSlotList()
Loganf7f0ac52011-01-07 03:53:43 +080084 && readContext()
85 && checkContext()
86 //&& readRelocationTable()
87 //&& relocate()
88 ;
89
Logan856ceb22011-01-07 05:21:26 +080090 return result ? mpResult.take() : NULL;
Loganf7f0ac52011-01-07 03:53:43 +080091}
92
93
94bool CacheReader::checkFileSize() {
95 struct stat stfile;
96 if (fstat(mFile->getFD(), &stfile) < 0) {
97 LOGE("Unable to stat cache file.\n");
98 return false;
99 }
100
101 mFileSize = stfile.st_size;
102
103 if (mFileSize < (off_t)sizeof(OBCC_Header) ||
Logan1dc63142011-02-25 17:14:51 +0800104 mFileSize < (off_t)ContextManager::ContextSize) {
Loganf7f0ac52011-01-07 03:53:43 +0800105 LOGE("Cache file is too small to be correct.\n");
106 return false;
107 }
108
109 return true;
110}
111
112
113bool CacheReader::readHeader() {
114 if (mFile->seek(0, SEEK_SET) != 0) {
115 LOGE("Unable to seek to 0. (reason: %s)\n", strerror(errno));
116 return false;
117 }
118
Logan856ceb22011-01-07 05:21:26 +0800119 mpHeader = (OBCC_Header *)malloc(sizeof(OBCC_Header));
120 if (!mpHeader) {
Loganf7f0ac52011-01-07 03:53:43 +0800121 LOGE("Unable to allocate for cache header.\n");
122 return false;
123 }
124
Logan856ceb22011-01-07 05:21:26 +0800125 if (mFile->read(reinterpret_cast<char *>(mpHeader), sizeof(OBCC_Header)) !=
Loganf7f0ac52011-01-07 03:53:43 +0800126 (ssize_t)sizeof(OBCC_Header)) {
127 LOGE("Unable to read cache header.\n");
128 return false;
129 }
130
Logana2e15af2011-01-07 11:46:08 +0800131 // Dirty hack for libRS.
132 // TODO(all): This should be removed in the future.
133 if (mpHeader->libRS_threadable) {
134 mpResult->mLibRSThreadable = true;
135 }
136
Loganf7f0ac52011-01-07 03:53:43 +0800137 return true;
138}
139
140
141bool CacheReader::checkHeader() {
Logan856ceb22011-01-07 05:21:26 +0800142 if (memcmp(mpHeader->magic, OBCC_MAGIC, 4) != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800143 LOGE("Bad magic word\n");
144 return false;
145 }
146
Logan856ceb22011-01-07 05:21:26 +0800147 if (memcmp(mpHeader->version, OBCC_VERSION, 4) != 0) {
Logane1323992011-01-12 04:47:13 +0800148 mpHeader->version[4 - 1] = '\0'; // ensure c-style string terminated
Shih-wei Liaoa6526652011-01-12 04:42:07 -0800149 LOGI("Cache file format version mismatch: now %s cached %s\n",
Logane1323992011-01-12 04:47:13 +0800150 OBCC_VERSION, mpHeader->version);
151 return false;
152 }
153
154 if (memcmp(mpHeader->libbcc_build_time, libbcc_build_time, 24) != 0) {
155 mpHeader->libbcc_build_time[24 - 1] = '\0'; // ensure terminated
Shih-wei Liao39ebe2c2011-01-28 11:22:32 -0800156 LOGW("Build time mismatch: lib %s cached %s\n", libbcc_build_time,
Logane1323992011-01-12 04:47:13 +0800157 mpHeader->libbcc_build_time);
Loganf7f0ac52011-01-07 03:53:43 +0800158 return false;
159 }
160
161 return true;
162}
163
164
165bool CacheReader::checkMachineIntType() {
166 uint32_t number = 0x00000001;
167
168 bool isLittleEndian = (*reinterpret_cast<char *>(&number) == 1);
Logan856ceb22011-01-07 05:21:26 +0800169 if ((isLittleEndian && mpHeader->endianness != 'e') ||
170 (!isLittleEndian && mpHeader->endianness != 'E')) {
Loganf7f0ac52011-01-07 03:53:43 +0800171 LOGE("Machine endianness mismatch.\n");
172 return false;
173 }
174
Logan856ceb22011-01-07 05:21:26 +0800175 if ((unsigned int)mpHeader->sizeof_off_t != sizeof(off_t) ||
176 (unsigned int)mpHeader->sizeof_size_t != sizeof(size_t) ||
177 (unsigned int)mpHeader->sizeof_ptr_t != sizeof(void *)) {
Loganf7f0ac52011-01-07 03:53:43 +0800178 LOGE("Machine integer size mismatch.\n");
179 return false;
180 }
181
182 return true;
183}
184
185
186bool CacheReader::checkSectionOffsetAndSize() {
187#define CHECK_SECTION_OFFSET(NAME) \
188 do { \
Logan856ceb22011-01-07 05:21:26 +0800189 off_t offset = mpHeader-> NAME##_offset; \
190 off_t size = (off_t)mpHeader-> NAME##_size; \
Loganf7f0ac52011-01-07 03:53:43 +0800191 \
192 if (mFileSize < offset || mFileSize < offset + size) { \
193 LOGE(#NAME " section overflow.\n"); \
194 return false; \
195 } \
196 \
197 if (offset % sizeof(int) != 0) { \
198 LOGE(#NAME " offset must aligned to %d.\n", sizeof(int)); \
199 return false; \
200 } \
201 \
202 if (size < static_cast<off_t>(sizeof(size_t))) { \
203 LOGE(#NAME " size is too small to be correct.\n"); \
204 return false; \
205 } \
206 } while (0)
207
208 CHECK_SECTION_OFFSET(str_pool);
209 CHECK_SECTION_OFFSET(depend_tab);
Logana2e15af2011-01-07 11:46:08 +0800210 //CHECK_SECTION_OFFSET(reloc_tab);
Loganf7f0ac52011-01-07 03:53:43 +0800211 CHECK_SECTION_OFFSET(export_var_list);
212 CHECK_SECTION_OFFSET(export_func_list);
213 CHECK_SECTION_OFFSET(pragma_list);
214
215#undef CHECK_SECTION_OFFSET
216
Logan856ceb22011-01-07 05:21:26 +0800217 if (mFileSize < mpHeader->context_offset ||
Logan1dc63142011-02-25 17:14:51 +0800218 mFileSize < (off_t)mpHeader->context_offset +
219 (off_t)ContextManager::ContextSize) {
Loganf7f0ac52011-01-07 03:53:43 +0800220 LOGE("context section overflow.\n");
221 return false;
222 }
223
224 long pagesize = sysconf(_SC_PAGESIZE);
Logan856ceb22011-01-07 05:21:26 +0800225 if (mpHeader->context_offset % pagesize != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800226 LOGE("context offset must aligned to pagesize.\n");
227 return false;
228 }
229
230 // TODO(logan): Move this to some where else.
Logan856ceb22011-01-07 05:21:26 +0800231 if ((uintptr_t)mpHeader->context_cached_addr % pagesize != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800232 LOGE("cached address is not aligned to pagesize.\n");
233 return false;
234 }
235
236 return true;
237}
238
239
Logan856ceb22011-01-07 05:21:26 +0800240#define CACHE_READER_READ_SECTION(TYPE, AUTO_MANAGED_HOLDER, NAME) \
241 TYPE *NAME##_raw = (TYPE *)malloc(mpHeader->NAME##_size); \
242 \
243 if (!NAME##_raw) { \
244 LOGE("Unable to allocate for " #NAME "\n"); \
245 return false; \
246 } \
247 \
248 /* We have to ensure that some one will deallocate NAME##_raw */ \
249 AUTO_MANAGED_HOLDER = NAME##_raw; \
250 \
251 if (mFile->seek(mpHeader->NAME##_offset, SEEK_SET) == -1) { \
252 LOGE("Unable to seek to " #NAME " section\n"); \
253 return false; \
254 } \
255 \
256 if (mFile->read(reinterpret_cast<char *>(NAME##_raw), \
257 mpHeader->NAME##_size) != (ssize_t)mpHeader->NAME##_size) \
258 { \
259 LOGE("Unable to read " #NAME ".\n"); \
260 return false; \
261 }
262
263
Loganf7f0ac52011-01-07 03:53:43 +0800264bool CacheReader::readStringPool() {
Logan856ceb22011-01-07 05:21:26 +0800265 CACHE_READER_READ_SECTION(OBCC_StringPool,
266 mpResult->mpStringPoolRaw, str_pool);
Loganf7f0ac52011-01-07 03:53:43 +0800267
Logan856ceb22011-01-07 05:21:26 +0800268 char *str_base = reinterpret_cast<char *>(str_pool_raw);
Loganf7f0ac52011-01-07 03:53:43 +0800269
Logan856ceb22011-01-07 05:21:26 +0800270 vector<char const *> &pool = mpResult->mStringPool;
271 for (size_t i = 0; i < str_pool_raw->count; ++i) {
272 char *str = str_base + str_pool_raw->list[i].offset;
Loganf7f0ac52011-01-07 03:53:43 +0800273 pool.push_back(str);
274 }
275
276 return true;
277}
278
279
280bool CacheReader::checkStringPool() {
Logan856ceb22011-01-07 05:21:26 +0800281 OBCC_StringPool *poolR = mpResult->mpStringPoolRaw;
282 vector<char const *> &pool = mpResult->mStringPool;
Loganf7f0ac52011-01-07 03:53:43 +0800283
284 // Ensure that every c-style string is ended with '\0'
285 for (size_t i = 0; i < poolR->count; ++i) {
286 if (pool[i][poolR->list[i].length] != '\0') {
287 LOGE("The %lu-th string does not end with '\\0'.\n", (unsigned long)i);
288 return false;
289 }
290 }
291
292 return true;
293}
294
295
296bool CacheReader::readDependencyTable() {
Logan856ceb22011-01-07 05:21:26 +0800297 CACHE_READER_READ_SECTION(OBCC_DependencyTable, mpCachedDependTable,
298 depend_tab);
Loganf7f0ac52011-01-07 03:53:43 +0800299 return true;
300}
301
302
303bool CacheReader::checkDependency() {
Logan856ceb22011-01-07 05:21:26 +0800304 if (mDependencies.size() != mpCachedDependTable->count) {
305 LOGE("Dependencies count mismatch. (%lu vs %lu)\n",
306 (unsigned long)mDependencies.size(),
307 (unsigned long)mpCachedDependTable->count);
308 return false;
309 }
310
311 vector<char const *> &strPool = mpResult->mStringPool;
Logan75cc8a52011-01-07 06:06:52 +0800312 map<string, pair<uint32_t, unsigned char const *> >::iterator dep;
Logan856ceb22011-01-07 05:21:26 +0800313
314 dep = mDependencies.begin();
315 for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) {
316 string const &depName = dep->first;
Logan856ceb22011-01-07 05:21:26 +0800317 uint32_t depType = dep->second.first;
Logan75cc8a52011-01-07 06:06:52 +0800318 unsigned char const *depSHA1 = dep->second.second;
Logan856ceb22011-01-07 05:21:26 +0800319
320 OBCC_Dependency *depCached =&mpCachedDependTable->table[i];
321 char const *depCachedName = strPool[depCached->res_name_strp_index];
Logan856ceb22011-01-07 05:21:26 +0800322 uint32_t depCachedType = depCached->res_type;
Logan75cc8a52011-01-07 06:06:52 +0800323 unsigned char const *depCachedSHA1 = depCached->sha1;
Logan856ceb22011-01-07 05:21:26 +0800324
325 if (depName != depCachedName) {
326 LOGE("Cache dependency name mismatch:\n");
327 LOGE(" given: %s\n", depName.c_str());
328 LOGE(" cached: %s\n", depCachedName);
329
330 return false;
331 }
332
333 if (memcmp(depSHA1, depCachedSHA1, 20) != 0) {
334 LOGE("Cache dependency %s sha1 mismatch:\n", depCachedName);
335
336#define PRINT_SHA1(PREFIX, X, POSTFIX) \
337 LOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
338 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \
339 X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], \
340 X[10],X[11],X[12],X[13],X[14],X[15],X[16],X[17],X[18],X[19]);
341
342 PRINT_SHA1(" given: ", depSHA1, "\n");
343 PRINT_SHA1(" cached: ", depCachedSHA1, "\n");
344
345#undef PRINT_SHA1
346
347 return false;
348 }
349
350 if (depType != depCachedType) {
351 LOGE("Cache dependency %s resource type mismatch.\n", depCachedName);
352 return false;
353 }
354 }
355
Loganf7f0ac52011-01-07 03:53:43 +0800356 return true;
357}
358
359bool CacheReader::readExportVarList() {
Logan856ceb22011-01-07 05:21:26 +0800360 CACHE_READER_READ_SECTION(OBCC_ExportVarList,
361 mpResult->mpExportVars, export_var_list);
Loganf7f0ac52011-01-07 03:53:43 +0800362 return true;
363}
364
365
366bool CacheReader::readExportFuncList() {
Logan856ceb22011-01-07 05:21:26 +0800367 CACHE_READER_READ_SECTION(OBCC_ExportFuncList,
368 mpResult->mpExportFuncs, export_func_list);
Loganf7f0ac52011-01-07 03:53:43 +0800369 return true;
370}
371
372
373bool CacheReader::readPragmaList() {
Logan856ceb22011-01-07 05:21:26 +0800374 CACHE_READER_READ_SECTION(OBCC_PragmaList, mpPragmaList, pragma_list);
Loganf7f0ac52011-01-07 03:53:43 +0800375
Logan856ceb22011-01-07 05:21:26 +0800376 vector<char const *> const &strPool = mpResult->mStringPool;
377 ScriptCached::PragmaList &pragmas = mpResult->mPragmas;
Loganf7f0ac52011-01-07 03:53:43 +0800378
Logan856ceb22011-01-07 05:21:26 +0800379 for (size_t i = 0; i < pragma_list_raw->count; ++i) {
380 OBCC_Pragma *pragma = &pragma_list_raw->list[i];
Loganf7f0ac52011-01-07 03:53:43 +0800381 pragmas.push_back(make_pair(strPool[pragma->key_strp_index],
382 strPool[pragma->value_strp_index]));
383 }
384
385 return true;
386}
387
388
Stephen Hines071288a2011-01-27 14:38:26 -0800389bool CacheReader::readObjectSlotList() {
390 CACHE_READER_READ_SECTION(OBCC_ObjectSlotList,
391 mpResult->mpObjectSlotList, object_slot_list);
392 return true;
393}
394
395
Loganf7f0ac52011-01-07 03:53:43 +0800396bool CacheReader::readFuncTable() {
Logan856ceb22011-01-07 05:21:26 +0800397 CACHE_READER_READ_SECTION(OBCC_FuncTable, mpFuncTable, func_table);
398
399 vector<char const *> &strPool = mpResult->mStringPool;
400 ScriptCached::FuncTable &table = mpResult->mFunctions;
401 for (size_t i = 0; i < func_table_raw->count; ++i) {
402 OBCC_FuncInfo *func = &func_table_raw->table[i];
403 table.insert(make_pair(strPool[func->name_strp_index],
404 make_pair(func->cached_addr, func->size)));
405 }
406
407 return true;
Loganf7f0ac52011-01-07 03:53:43 +0800408}
409
Logan856ceb22011-01-07 05:21:26 +0800410#undef CACHE_READER_READ_SECTION
411
Loganf7f0ac52011-01-07 03:53:43 +0800412
413bool CacheReader::readContext() {
Logan1dc63142011-02-25 17:14:51 +0800414 mpResult->mContext =
415 ContextManager::get().allocateContext(mpHeader->context_cached_addr,
416 mFile->getFD(),
417 mpHeader->context_offset);
Loganf7f0ac52011-01-07 03:53:43 +0800418
Logan856ceb22011-01-07 05:21:26 +0800419 if (!mpResult->mContext) {
Loganf7f0ac52011-01-07 03:53:43 +0800420 // Unable to allocate at cached address. Give up.
Logan42598052011-01-26 22:41:13 +0800421 mIsContextSlotNotAvail = true;
Loganf7f0ac52011-01-07 03:53:43 +0800422 return false;
423
424 // TODO(logan): If relocation is fixed, we should try to allocate the
425 // code in different location, and relocate the context.
426 }
427
428 return true;
429}
430
431
432bool CacheReader::checkContext() {
Logan856ceb22011-01-07 05:21:26 +0800433 uint32_t sum = mpHeader->context_parity_checksum;
434 uint32_t *ptr = reinterpret_cast<uint32_t *>(mpResult->mContext);
Loganf7f0ac52011-01-07 03:53:43 +0800435
Logan1dc63142011-02-25 17:14:51 +0800436 for (size_t i = 0; i < ContextManager::ContextSize / sizeof(uint32_t); ++i) {
Loganf7f0ac52011-01-07 03:53:43 +0800437 sum ^= *ptr++;
438 }
439
440 if (sum != 0) {
441 LOGE("Checksum check failed\n");
442 return false;
443 }
444
445 LOGI("Passed checksum even parity verification.\n");
446 return true;
447}
448
449
450bool CacheReader::readRelocationTable() {
451 // TODO(logan): Not finished.
452 return true;
453}
454
455
456bool CacheReader::relocate() {
457 // TODO(logan): Not finished.
458 return true;
459}
460
461
462} // namespace bcc