blob: c017d367cb6e0c441621d6ae7a2934e3d6be09e8 [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
Loganf7f0ac52011-01-07 03:53:43 +080054ScriptCached *CacheReader::readCacheFile(FileHandle *file) {
55 // Check file handle
56 if (!file || file->getFD() < 0) {
57 return NULL;
58 }
59
60 // Allocate ScriptCached object
Logan856ceb22011-01-07 05:21:26 +080061 mpResult.reset(new (nothrow) ScriptCached(mpOwner));
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()
81 && readContext()
82 && checkContext()
83 //&& readRelocationTable()
84 //&& relocate()
85 ;
86
87
88 // TODO(logan): This is the hack for libRS. Should be turned on
89 // before caching is ready to go.
90#if 0
91 // Check the cache file has __isThreadable or not. If it is set,
92 // then we have to call mpSymbolLookupFn for __clearThreadable.
Logan856ceb22011-01-07 05:21:26 +080093 if (mpHeader->libRSThreadable && mpSymbolLookupFn) {
Loganf7f0ac52011-01-07 03:53:43 +080094 mpSymbolLookupFn(mpSymbolLookupContext, "__clearThreadable");
95 }
96#endif
97
Logan856ceb22011-01-07 05:21:26 +080098 return result ? mpResult.take() : NULL;
Loganf7f0ac52011-01-07 03:53:43 +080099}
100
101
102bool CacheReader::checkFileSize() {
103 struct stat stfile;
104 if (fstat(mFile->getFD(), &stfile) < 0) {
105 LOGE("Unable to stat cache file.\n");
106 return false;
107 }
108
109 mFileSize = stfile.st_size;
110
111 if (mFileSize < (off_t)sizeof(OBCC_Header) ||
112 mFileSize < (off_t)BCC_CONTEXT_SIZE) {
113 LOGE("Cache file is too small to be correct.\n");
114 return false;
115 }
116
117 return true;
118}
119
120
121bool CacheReader::readHeader() {
122 if (mFile->seek(0, SEEK_SET) != 0) {
123 LOGE("Unable to seek to 0. (reason: %s)\n", strerror(errno));
124 return false;
125 }
126
Logan856ceb22011-01-07 05:21:26 +0800127 mpHeader = (OBCC_Header *)malloc(sizeof(OBCC_Header));
128 if (!mpHeader) {
Loganf7f0ac52011-01-07 03:53:43 +0800129 LOGE("Unable to allocate for cache header.\n");
130 return false;
131 }
132
Logan856ceb22011-01-07 05:21:26 +0800133 if (mFile->read(reinterpret_cast<char *>(mpHeader), sizeof(OBCC_Header)) !=
Loganf7f0ac52011-01-07 03:53:43 +0800134 (ssize_t)sizeof(OBCC_Header)) {
135 LOGE("Unable to read cache header.\n");
136 return false;
137 }
138
139 return true;
140}
141
142
143bool CacheReader::checkHeader() {
Logan856ceb22011-01-07 05:21:26 +0800144 if (memcmp(mpHeader->magic, OBCC_MAGIC, 4) != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800145 LOGE("Bad magic word\n");
146 return false;
147 }
148
Logan856ceb22011-01-07 05:21:26 +0800149 if (memcmp(mpHeader->version, OBCC_VERSION, 4) != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800150 LOGE("Bad oBCC version 0x%08x\n",
Logan856ceb22011-01-07 05:21:26 +0800151 *reinterpret_cast<uint32_t *>(mpHeader->version));
Loganf7f0ac52011-01-07 03:53:43 +0800152 return false;
153 }
154
155 return true;
156}
157
158
159bool CacheReader::checkMachineIntType() {
160 uint32_t number = 0x00000001;
161
162 bool isLittleEndian = (*reinterpret_cast<char *>(&number) == 1);
Logan856ceb22011-01-07 05:21:26 +0800163 if ((isLittleEndian && mpHeader->endianness != 'e') ||
164 (!isLittleEndian && mpHeader->endianness != 'E')) {
Loganf7f0ac52011-01-07 03:53:43 +0800165 LOGE("Machine endianness mismatch.\n");
166 return false;
167 }
168
Logan856ceb22011-01-07 05:21:26 +0800169 if ((unsigned int)mpHeader->sizeof_off_t != sizeof(off_t) ||
170 (unsigned int)mpHeader->sizeof_size_t != sizeof(size_t) ||
171 (unsigned int)mpHeader->sizeof_ptr_t != sizeof(void *)) {
Loganf7f0ac52011-01-07 03:53:43 +0800172 LOGE("Machine integer size mismatch.\n");
173 return false;
174 }
175
176 return true;
177}
178
179
180bool CacheReader::checkSectionOffsetAndSize() {
181#define CHECK_SECTION_OFFSET(NAME) \
182 do { \
Logan856ceb22011-01-07 05:21:26 +0800183 off_t offset = mpHeader-> NAME##_offset; \
184 off_t size = (off_t)mpHeader-> NAME##_size; \
Loganf7f0ac52011-01-07 03:53:43 +0800185 \
186 if (mFileSize < offset || mFileSize < offset + size) { \
187 LOGE(#NAME " section overflow.\n"); \
188 return false; \
189 } \
190 \
191 if (offset % sizeof(int) != 0) { \
192 LOGE(#NAME " offset must aligned to %d.\n", sizeof(int)); \
193 return false; \
194 } \
195 \
196 if (size < static_cast<off_t>(sizeof(size_t))) { \
197 LOGE(#NAME " size is too small to be correct.\n"); \
198 return false; \
199 } \
200 } while (0)
201
202 CHECK_SECTION_OFFSET(str_pool);
203 CHECK_SECTION_OFFSET(depend_tab);
204 CHECK_SECTION_OFFSET(reloc_tab);
205 CHECK_SECTION_OFFSET(export_var_list);
206 CHECK_SECTION_OFFSET(export_func_list);
207 CHECK_SECTION_OFFSET(pragma_list);
208
209#undef CHECK_SECTION_OFFSET
210
Logan856ceb22011-01-07 05:21:26 +0800211 if (mFileSize < mpHeader->context_offset ||
212 mFileSize < mpHeader->context_offset + BCC_CONTEXT_SIZE) {
Loganf7f0ac52011-01-07 03:53:43 +0800213 LOGE("context section overflow.\n");
214 return false;
215 }
216
217 long pagesize = sysconf(_SC_PAGESIZE);
Logan856ceb22011-01-07 05:21:26 +0800218 if (mpHeader->context_offset % pagesize != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800219 LOGE("context offset must aligned to pagesize.\n");
220 return false;
221 }
222
223 // TODO(logan): Move this to some where else.
Logan856ceb22011-01-07 05:21:26 +0800224 if ((uintptr_t)mpHeader->context_cached_addr % pagesize != 0) {
Loganf7f0ac52011-01-07 03:53:43 +0800225 LOGE("cached address is not aligned to pagesize.\n");
226 return false;
227 }
228
229 return true;
230}
231
232
Logan856ceb22011-01-07 05:21:26 +0800233#define CACHE_READER_READ_SECTION(TYPE, AUTO_MANAGED_HOLDER, NAME) \
234 TYPE *NAME##_raw = (TYPE *)malloc(mpHeader->NAME##_size); \
235 \
236 if (!NAME##_raw) { \
237 LOGE("Unable to allocate for " #NAME "\n"); \
238 return false; \
239 } \
240 \
241 /* We have to ensure that some one will deallocate NAME##_raw */ \
242 AUTO_MANAGED_HOLDER = NAME##_raw; \
243 \
244 if (mFile->seek(mpHeader->NAME##_offset, SEEK_SET) == -1) { \
245 LOGE("Unable to seek to " #NAME " section\n"); \
246 return false; \
247 } \
248 \
249 if (mFile->read(reinterpret_cast<char *>(NAME##_raw), \
250 mpHeader->NAME##_size) != (ssize_t)mpHeader->NAME##_size) \
251 { \
252 LOGE("Unable to read " #NAME ".\n"); \
253 return false; \
254 }
255
256
Loganf7f0ac52011-01-07 03:53:43 +0800257bool CacheReader::readStringPool() {
Logan856ceb22011-01-07 05:21:26 +0800258 CACHE_READER_READ_SECTION(OBCC_StringPool,
259 mpResult->mpStringPoolRaw, str_pool);
Loganf7f0ac52011-01-07 03:53:43 +0800260
Logan856ceb22011-01-07 05:21:26 +0800261 char *str_base = reinterpret_cast<char *>(str_pool_raw);
Loganf7f0ac52011-01-07 03:53:43 +0800262
Logan856ceb22011-01-07 05:21:26 +0800263 vector<char const *> &pool = mpResult->mStringPool;
264 for (size_t i = 0; i < str_pool_raw->count; ++i) {
265 char *str = str_base + str_pool_raw->list[i].offset;
Loganf7f0ac52011-01-07 03:53:43 +0800266 pool.push_back(str);
267 }
268
269 return true;
270}
271
272
273bool CacheReader::checkStringPool() {
Logan856ceb22011-01-07 05:21:26 +0800274 OBCC_StringPool *poolR = mpResult->mpStringPoolRaw;
275 vector<char const *> &pool = mpResult->mStringPool;
Loganf7f0ac52011-01-07 03:53:43 +0800276
277 // Ensure that every c-style string is ended with '\0'
278 for (size_t i = 0; i < poolR->count; ++i) {
279 if (pool[i][poolR->list[i].length] != '\0') {
280 LOGE("The %lu-th string does not end with '\\0'.\n", (unsigned long)i);
281 return false;
282 }
283 }
284
285 return true;
286}
287
288
289bool CacheReader::readDependencyTable() {
Logan856ceb22011-01-07 05:21:26 +0800290 CACHE_READER_READ_SECTION(OBCC_DependencyTable, mpCachedDependTable,
291 depend_tab);
Loganf7f0ac52011-01-07 03:53:43 +0800292 return true;
293}
294
295
296bool CacheReader::checkDependency() {
Logan856ceb22011-01-07 05:21:26 +0800297 if (mDependencies.size() != mpCachedDependTable->count) {
298 LOGE("Dependencies count mismatch. (%lu vs %lu)\n",
299 (unsigned long)mDependencies.size(),
300 (unsigned long)mpCachedDependTable->count);
301 return false;
302 }
303
304 vector<char const *> &strPool = mpResult->mStringPool;
Logan75cc8a52011-01-07 06:06:52 +0800305 map<string, pair<uint32_t, unsigned char const *> >::iterator dep;
Logan856ceb22011-01-07 05:21:26 +0800306
307 dep = mDependencies.begin();
308 for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) {
309 string const &depName = dep->first;
Logan856ceb22011-01-07 05:21:26 +0800310 uint32_t depType = dep->second.first;
Logan75cc8a52011-01-07 06:06:52 +0800311 unsigned char const *depSHA1 = dep->second.second;
Logan856ceb22011-01-07 05:21:26 +0800312
313 OBCC_Dependency *depCached =&mpCachedDependTable->table[i];
314 char const *depCachedName = strPool[depCached->res_name_strp_index];
Logan856ceb22011-01-07 05:21:26 +0800315 uint32_t depCachedType = depCached->res_type;
Logan75cc8a52011-01-07 06:06:52 +0800316 unsigned char const *depCachedSHA1 = depCached->sha1;
Logan856ceb22011-01-07 05:21:26 +0800317
318 if (depName != depCachedName) {
319 LOGE("Cache dependency name mismatch:\n");
320 LOGE(" given: %s\n", depName.c_str());
321 LOGE(" cached: %s\n", depCachedName);
322
323 return false;
324 }
325
326 if (memcmp(depSHA1, depCachedSHA1, 20) != 0) {
327 LOGE("Cache dependency %s sha1 mismatch:\n", depCachedName);
328
329#define PRINT_SHA1(PREFIX, X, POSTFIX) \
330 LOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
331 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \
332 X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], \
333 X[10],X[11],X[12],X[13],X[14],X[15],X[16],X[17],X[18],X[19]);
334
335 PRINT_SHA1(" given: ", depSHA1, "\n");
336 PRINT_SHA1(" cached: ", depCachedSHA1, "\n");
337
338#undef PRINT_SHA1
339
340 return false;
341 }
342
343 if (depType != depCachedType) {
344 LOGE("Cache dependency %s resource type mismatch.\n", depCachedName);
345 return false;
346 }
347 }
348
Loganf7f0ac52011-01-07 03:53:43 +0800349 return true;
350}
351
352bool CacheReader::readExportVarList() {
Logan856ceb22011-01-07 05:21:26 +0800353 CACHE_READER_READ_SECTION(OBCC_ExportVarList,
354 mpResult->mpExportVars, export_var_list);
Loganf7f0ac52011-01-07 03:53:43 +0800355 return true;
356}
357
358
359bool CacheReader::readExportFuncList() {
Logan856ceb22011-01-07 05:21:26 +0800360 CACHE_READER_READ_SECTION(OBCC_ExportFuncList,
361 mpResult->mpExportFuncs, export_func_list);
Loganf7f0ac52011-01-07 03:53:43 +0800362 return true;
363}
364
365
366bool CacheReader::readPragmaList() {
Logan856ceb22011-01-07 05:21:26 +0800367 CACHE_READER_READ_SECTION(OBCC_PragmaList, mpPragmaList, pragma_list);
Loganf7f0ac52011-01-07 03:53:43 +0800368
Logan856ceb22011-01-07 05:21:26 +0800369 vector<char const *> const &strPool = mpResult->mStringPool;
370 ScriptCached::PragmaList &pragmas = mpResult->mPragmas;
Loganf7f0ac52011-01-07 03:53:43 +0800371
Logan856ceb22011-01-07 05:21:26 +0800372 for (size_t i = 0; i < pragma_list_raw->count; ++i) {
373 OBCC_Pragma *pragma = &pragma_list_raw->list[i];
Loganf7f0ac52011-01-07 03:53:43 +0800374 pragmas.push_back(make_pair(strPool[pragma->key_strp_index],
375 strPool[pragma->value_strp_index]));
376 }
377
378 return true;
379}
380
381
382bool CacheReader::readFuncTable() {
Logan856ceb22011-01-07 05:21:26 +0800383 CACHE_READER_READ_SECTION(OBCC_FuncTable, mpFuncTable, func_table);
384
385 vector<char const *> &strPool = mpResult->mStringPool;
386 ScriptCached::FuncTable &table = mpResult->mFunctions;
387 for (size_t i = 0; i < func_table_raw->count; ++i) {
388 OBCC_FuncInfo *func = &func_table_raw->table[i];
389 table.insert(make_pair(strPool[func->name_strp_index],
390 make_pair(func->cached_addr, func->size)));
391 }
392
393 return true;
Loganf7f0ac52011-01-07 03:53:43 +0800394}
395
Logan856ceb22011-01-07 05:21:26 +0800396#undef CACHE_READER_READ_SECTION
397
Loganf7f0ac52011-01-07 03:53:43 +0800398
399bool CacheReader::readContext() {
Logan856ceb22011-01-07 05:21:26 +0800400 mpResult->mContext = allocateContext(mpHeader->context_cached_addr,
Loganf7f0ac52011-01-07 03:53:43 +0800401 mFile->getFD(),
Logan856ceb22011-01-07 05:21:26 +0800402 mpHeader->context_offset);
Loganf7f0ac52011-01-07 03:53:43 +0800403
Logan856ceb22011-01-07 05:21:26 +0800404 if (!mpResult->mContext) {
Loganf7f0ac52011-01-07 03:53:43 +0800405 // Unable to allocate at cached address. Give up.
406 return false;
407
408 // TODO(logan): If relocation is fixed, we should try to allocate the
409 // code in different location, and relocate the context.
410 }
411
412 return true;
413}
414
415
416bool CacheReader::checkContext() {
Logan856ceb22011-01-07 05:21:26 +0800417 uint32_t sum = mpHeader->context_parity_checksum;
418 uint32_t *ptr = reinterpret_cast<uint32_t *>(mpResult->mContext);
Loganf7f0ac52011-01-07 03:53:43 +0800419
420 for (size_t i = 0; i < BCC_CONTEXT_SIZE / sizeof(uint32_t); ++i) {
421 sum ^= *ptr++;
422 }
423
424 if (sum != 0) {
425 LOGE("Checksum check failed\n");
426 return false;
427 }
428
429 LOGI("Passed checksum even parity verification.\n");
430 return true;
431}
432
433
434bool CacheReader::readRelocationTable() {
435 // TODO(logan): Not finished.
436 return true;
437}
438
439
440bool CacheReader::relocate() {
441 // TODO(logan): Not finished.
442 return true;
443}
444
445
446} // namespace bcc