blob: 1a74ed34d293f236d08e13fd56e484b2dbdb6db3 [file] [log] [blame]
Stephen Hines4a68b1c2012-05-03 12:28:14 -07001/*
2 * Copyright 2010-2012, 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#include "MCCacheReader.h"
18
19#include "BCCRuntimeSymbolResolver.h"
20#include "DebugHelper.h"
21#include "InputFile.h"
22#include "ScriptCached.h"
23#include "SymbolResolverProxy.h"
24#include "SymbolResolvers.h"
25
26#include <bcc/bcc_mccache.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
39#include <stdlib.h>
40#include <string.h>
41
42using namespace std;
43
44namespace bcc {
45
46MCCacheReader::~MCCacheReader() {
47 if (mpHeader) { free(mpHeader); }
48 if (mpCachedDependTable) { free(mpCachedDependTable); }
49 if (mpPragmaList) { free(mpPragmaList); }
50 if (mpVarNameList) { free(mpVarNameList); }
51 if (mpFuncNameList) { free(mpFuncNameList); }
52}
53
54ScriptCached *MCCacheReader::readCacheFile(InputFile &objFile,
55 InputFile &infoFile,
56 Script *S) {
57 bool result = checkCacheFile(objFile, infoFile, S)
58 && readPragmaList()
59 && readObjectSlotList()
60 && readObjFile()
61 && readVarNameList()
62 && readFuncNameList()
63 && readForEachNameList()
64 //&& relocate()
65 ;
66
67 return result ? mpResult.take() : NULL;
68}
69
70bool MCCacheReader::checkCacheFile(InputFile &objFile,
71 InputFile &infoFile,
72 Script *S) {
73 // Check file handle
74 if (objFile.hasError() || infoFile.hasError()) {
75 return false;
76 }
77
78 mObjFile = &objFile;
79 mInfoFile = &infoFile;
80
81 // Allocate ScriptCached object
82 mpResult.reset(new (nothrow) ScriptCached(S));
83
84 if (!mpResult) {
85 ALOGE("Unable to allocate ScriptCached object.\n");
86 return false;
87 }
88
89 bool result = checkFileSize()
90 && readHeader()
91 && checkHeader()
92 && checkMachineIntType()
93 && checkSectionOffsetAndSize()
94 && readStringPool()
95 && checkStringPool()
96 && readDependencyTable()
97 && checkDependency()
98 ;
99
100 return result;
101}
102
103
104bool MCCacheReader::checkFileSize() {
105 struct stat stfile;
106 if (::stat(mInfoFile->getName().c_str(), &stfile) < 0) {
107 ALOGE("Unable to stat cache file.\n");
108 return false;
109 }
110
111 mInfoFileSize = stfile.st_size;
112
113 if (mInfoFileSize < (off_t)sizeof(MCO_Header)) {
114 ALOGE("Cache file is too small to be correct.\n");
115 return false;
116 }
117
118 return true;
119}
120
121
122bool MCCacheReader::readHeader() {
123 if (mInfoFile->seek(0) != 0) {
124 ALOGE("Unable to seek to 0. (reason: %s)\n", strerror(errno));
125 return false;
126 }
127
128 mpHeader = (MCO_Header *)malloc(sizeof(MCO_Header));
129 if (!mpHeader) {
130 ALOGE("Unable to allocate for cache header.\n");
131 return false;
132 }
133
134 if (mInfoFile->read(reinterpret_cast<char *>(mpHeader), sizeof(MCO_Header)) !=
135 (ssize_t)sizeof(MCO_Header)) {
136 ALOGE("Unable to read cache header.\n");
137 return false;
138 }
139
140 // Dirty hack for libRS.
141 // TODO(all): This should be removed in the future.
142 if (mpHeader->libRS_threadable) {
143 mpResult->mLibRSThreadable = true;
144 }
145
146 return true;
147}
148
149
150bool MCCacheReader::checkHeader() {
151 if (memcmp(mpHeader->magic, MCO_MAGIC, 4) != 0) {
152 ALOGE("Bad magic word\n");
153 return false;
154 }
155
156 if (memcmp(mpHeader->version, MCO_VERSION, 4) != 0) {
157 mpHeader->version[4 - 1] = '\0'; // ensure c-style string terminated
158 ALOGI("Cache file format version mismatch: now %s cached %s\n",
159 MCO_VERSION, mpHeader->version);
160 return false;
161 }
162 return true;
163}
164
165
166bool MCCacheReader::checkMachineIntType() {
167 uint32_t number = 0x00000001;
168
169 bool isLittleEndian = (*reinterpret_cast<char *>(&number) == 1);
170 if ((isLittleEndian && mpHeader->endianness != 'e') ||
171 (!isLittleEndian && mpHeader->endianness != 'E')) {
172 ALOGE("Machine endianness mismatch.\n");
173 return false;
174 }
175
176 if ((unsigned int)mpHeader->sizeof_off_t != sizeof(off_t) ||
177 (unsigned int)mpHeader->sizeof_size_t != sizeof(size_t) ||
178 (unsigned int)mpHeader->sizeof_ptr_t != sizeof(void *)) {
179 ALOGE("Machine integer size mismatch.\n");
180 return false;
181 }
182
183 return true;
184}
185
186
187bool MCCacheReader::checkSectionOffsetAndSize() {
188#define CHECK_SECTION_OFFSET(NAME) \
189 do { \
190 off_t offset = mpHeader-> NAME##_offset; \
191 off_t size = (off_t)mpHeader-> NAME##_size; \
192 \
193 if (mInfoFileSize < offset || mInfoFileSize < offset + size) { \
194 ALOGE(#NAME " section overflow.\n"); \
195 return false; \
196 } \
197 \
198 if (offset % sizeof(int) != 0) { \
199 ALOGE(#NAME " offset must aligned to %d.\n", (int)sizeof(int)); \
200 return false; \
201 } \
202 \
203 if (size < static_cast<off_t>(sizeof(size_t))) { \
204 ALOGE(#NAME " size is too small to be correct.\n"); \
205 return false; \
206 } \
207 } while (0)
208
209 CHECK_SECTION_OFFSET(str_pool);
210 CHECK_SECTION_OFFSET(depend_tab);
211 //CHECK_SECTION_OFFSET(reloc_tab);
212 CHECK_SECTION_OFFSET(pragma_list);
213
214#undef CHECK_SECTION_OFFSET
215
216 return true;
217}
218
219
220#define CACHE_READER_READ_SECTION(TYPE, AUTO_MANAGED_HOLDER, NAME) \
221 TYPE *NAME##_raw = (TYPE *)malloc(mpHeader->NAME##_size); \
222 \
223 if (!NAME##_raw) { \
224 ALOGE("Unable to allocate for " #NAME "\n"); \
225 return false; \
226 } \
227 \
228 /* We have to ensure that some one will deallocate NAME##_raw */ \
229 AUTO_MANAGED_HOLDER = NAME##_raw; \
230 \
231 if (mInfoFile->seek(mpHeader->NAME##_offset) == -1) { \
232 ALOGE("Unable to seek to " #NAME " section\n"); \
233 return false; \
234 } \
235 \
236 if (mInfoFile->read(reinterpret_cast<char *>(NAME##_raw), \
237 mpHeader->NAME##_size) != (ssize_t)mpHeader->NAME##_size) \
238 { \
239 ALOGE("Unable to read " #NAME ".\n"); \
240 return false; \
241 }
242
243
244bool MCCacheReader::readStringPool() {
245 CACHE_READER_READ_SECTION(MCO_StringPool,
246 mpResult->mpStringPoolRaw, str_pool);
247
248 char *str_base = reinterpret_cast<char *>(str_pool_raw);
249
250 vector<char const *> &pool = mpResult->mStringPool;
251 for (size_t i = 0; i < str_pool_raw->count; ++i) {
252 char *str = str_base + str_pool_raw->list[i].offset;
253 pool.push_back(str);
254 }
255
256 return true;
257}
258
259
260bool MCCacheReader::checkStringPool() {
261 MCO_StringPool *poolR = mpResult->mpStringPoolRaw;
262 vector<char const *> &pool = mpResult->mStringPool;
263
264 // Ensure that every c-style string is ended with '\0'
265 for (size_t i = 0; i < poolR->count; ++i) {
266 if (pool[i][poolR->list[i].length] != '\0') {
267 ALOGE("The %lu-th string does not end with '\\0'.\n", (unsigned long)i);
268 return false;
269 }
270 }
271
272 return true;
273}
274
275
276bool MCCacheReader::readDependencyTable() {
277 CACHE_READER_READ_SECTION(MCO_DependencyTable, mpCachedDependTable,
278 depend_tab);
279 return true;
280}
281
282
283bool MCCacheReader::checkDependency() {
284 if (mDependencies.size() != mpCachedDependTable->count) {
285 ALOGE("Dependencies count mismatch. (%lu vs %lu)\n",
286 (unsigned long)mDependencies.size(),
287 (unsigned long)mpCachedDependTable->count);
288 return false;
289 }
290
291 vector<char const *> &strPool = mpResult->mStringPool;
Stephen Hines0f6b1d32012-05-03 12:29:33 -0700292 map<string, pair<uint32_t, unsigned char const *> >::iterator dep;
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700293
294 dep = mDependencies.begin();
295 for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) {
296 string const &depName = dep->first;
Stephen Hines0f6b1d32012-05-03 12:29:33 -0700297 uint32_t depType = dep->second.first;
298 unsigned char const *depSHA1 = dep->second.second;
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700299
300 MCO_Dependency *depCached =&mpCachedDependTable->table[i];
301 char const *depCachedName = strPool[depCached->res_name_strp_index];
Stephen Hines0f6b1d32012-05-03 12:29:33 -0700302 uint32_t depCachedType = depCached->res_type;
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700303 unsigned char const *depCachedSHA1 = depCached->sha1;
304
305 if (depName != depCachedName) {
306 ALOGE("Cache dependency name mismatch:\n");
307 ALOGE(" given: %s\n", depName.c_str());
308 ALOGE(" cached: %s\n", depCachedName);
309
310 return false;
311 }
312
313 if (memcmp(depSHA1, depCachedSHA1, 20) != 0) {
314 ALOGE("Cache dependency %s sha1 mismatch:\n", depCachedName);
315
316#define PRINT_SHA1(PREFIX, X, POSTFIX) \
317 ALOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
318 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \
319 X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], \
320 X[10],X[11],X[12],X[13],X[14],X[15],X[16],X[17],X[18],X[19]);
321
322 PRINT_SHA1(" given: ", depSHA1, "\n");
323 PRINT_SHA1(" cached: ", depCachedSHA1, "\n");
324
325#undef PRINT_SHA1
326
327 return false;
328 }
Stephen Hines0f6b1d32012-05-03 12:29:33 -0700329
330 if (depType != depCachedType) {
331 ALOGE("Cache dependency %s resource type mismatch.\n", depCachedName);
332 return false;
333 }
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700334 }
335
336 return true;
337}
338
339bool MCCacheReader::readVarNameList() {
340 CACHE_READER_READ_SECTION(MCO_String_Ptr, mpVarNameList, export_var_name_list);
341 vector<char const *> const &strPool = mpResult->mStringPool;
342
343 mpResult->mpExportVars = (MCO_ExportVarList*)
344 malloc(sizeof(size_t) +
345 sizeof(void*) * export_var_name_list_raw->count);
346 if (!mpResult->mpExportVars) {
347 ALOGE("Unable to allocate for mpExportVars\n");
348 return false;
349 }
350 mpResult->mpExportVars->count = export_var_name_list_raw->count;
351
352 for (size_t i = 0; i < export_var_name_list_raw->count; ++i) {
353 mpResult->mpExportVars->cached_addr_list[i] =
354 rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_var_name_list_raw->strp_indexs[i]]);
355#if DEBUG_MC_REFLECT
356 ALOGD("Get symbol address: %s -> %p",
357 strPool[export_var_name_list_raw->strp_indexs[i]], mpResult->mpExportVars->cached_addr_list[i]);
358#endif
359 }
360 return true;
361}
362
363bool MCCacheReader::readFuncNameList() {
364 CACHE_READER_READ_SECTION(MCO_String_Ptr, mpFuncNameList, export_func_name_list);
365 vector<char const *> const &strPool = mpResult->mStringPool;
366
367 mpResult->mpExportFuncs = (MCO_ExportFuncList*)
368 malloc(sizeof(size_t) +
369 sizeof(void*) * export_func_name_list_raw->count);
370 if (!mpResult->mpExportFuncs) {
371 ALOGE("Unable to allocate for mpExportFuncs\n");
372 return false;
373 }
374 mpResult->mpExportFuncs->count = export_func_name_list_raw->count;
375
376 for (size_t i = 0; i < export_func_name_list_raw->count; ++i) {
377 mpResult->mpExportFuncs->cached_addr_list[i] =
378 rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_func_name_list_raw->strp_indexs[i]]);
379#if DEBUG_MC_REFLECT
380 ALOGD("Get function address: %s -> %p",
381 strPool[export_func_name_list_raw->strp_indexs[i]], mpResult->mpExportFuncs->cached_addr_list[i]);
382#endif
383 }
384 return true;
385}
386
387bool MCCacheReader::readForEachNameList() {
388 CACHE_READER_READ_SECTION(MCO_String_Ptr, mpForEachNameList, export_foreach_name_list);
389 vector<char const *> const &strPool = mpResult->mStringPool;
390
391 mpResult->mpExportForEach = (MCO_ExportForEachList*)
392 malloc(sizeof(size_t) +
393 sizeof(void*) * export_foreach_name_list_raw->count);
394 if (!mpResult->mpExportForEach) {
395 ALOGE("Unable to allocate for mpExportForEach\n");
396 return false;
397 }
398 mpResult->mpExportForEach->count = export_foreach_name_list_raw->count;
399
400 for (size_t i = 0; i < export_foreach_name_list_raw->count; ++i) {
401 mpResult->mpExportForEach->cached_addr_list[i] =
402 rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_foreach_name_list_raw->strp_indexs[i]]);
403#if DEBUG_MC_REFLECT
404 ALOGE("Get foreach function address: %s -> %p",
405 strPool[export_foreach_name_list_raw->strp_indexs[i]], mpResult->mpExportForEach->cached_addr_list[i]);
406#endif
407 }
408 return true;
409}
410
411bool MCCacheReader::readPragmaList() {
412 CACHE_READER_READ_SECTION(MCO_PragmaList, mpPragmaList, pragma_list);
413
414 vector<char const *> const &strPool = mpResult->mStringPool;
415 ScriptCached::PragmaList &pragmas = mpResult->mPragmas;
416
417 for (size_t i = 0; i < pragma_list_raw->count; ++i) {
418 MCO_Pragma *pragma = &pragma_list_raw->list[i];
419 pragmas.push_back(make_pair(strPool[pragma->key_strp_index],
420 strPool[pragma->value_strp_index]));
421 }
422
423 return true;
424}
425
426
427bool MCCacheReader::readObjectSlotList() {
428 CACHE_READER_READ_SECTION(MCO_ObjectSlotList,
429 mpResult->mpObjectSlotList, object_slot_list);
430 return true;
431}
432
433bool MCCacheReader::readObjFile() {
434 if (mpResult->mCachedELFExecutable.size() != 0) {
435 ALOGE("Attempted to read cached object into a non-empty script");
436 return false;
437 }
438 char readBuffer[1024];
439 int readSize;
440 while ((readSize = mObjFile->read(readBuffer, 1024)) > 0) {
441 mpResult->mCachedELFExecutable.append(readBuffer, readBuffer + readSize);
442 }
443 if (readSize != 0) {
444 ALOGE("Read file Error");
445 return false;
446 }
447 ALOGD("Read object file size %d", (int)mpResult->mCachedELFExecutable.size());
448
449 BCCRuntimeSymbolResolver bccRuntimesResolver;
450 LookupFunctionSymbolResolver<void *> rsSymbolResolver(mpSymbolLookupFn,
451 mpSymbolLookupContext);
452
453 SymbolResolverProxy resolver;
454 resolver.chainResolver(bccRuntimesResolver);
455 resolver.chainResolver(rsSymbolResolver);
456
457 mpResult->mRSExecutable =
458 rsloaderCreateExec((unsigned char *)&*(mpResult->mCachedELFExecutable.begin()),
459 mpResult->mCachedELFExecutable.size(),
460 SymbolResolverProxy::LookupFunction, &resolver);
461
462 // Point ELF section headers to location of executable code, otherwise
463 // execution through GDB stops unexpectedly as GDB translates breakpoints
464 // in JITted code incorrectly (and complains about being unable to insert
465 // breakpoint at an invalid address)
466 rsloaderUpdateSectionHeaders(mpResult->mRSExecutable,
467 (unsigned char*) mpResult->mCachedELFExecutable.begin());
468
469 return true;
470}
471
472#undef CACHE_READER_READ_SECTION
473
474bool MCCacheReader::readRelocationTable() {
475 // TODO(logan): Not finished.
476 return true;
477}
478
479
480bool MCCacheReader::relocate() {
481 return true;
482}
483
484} // namespace bcc