Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 42 | using namespace std; |
| 43 | |
| 44 | namespace bcc { |
| 45 | |
| 46 | MCCacheReader::~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 | |
| 54 | ScriptCached *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 | |
| 70 | bool 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 | |
| 104 | bool 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 | |
| 122 | bool 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 | |
| 150 | bool 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 | |
| 166 | bool 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 | |
| 187 | bool 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 | |
| 244 | bool 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 | |
| 260 | bool 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 | |
| 276 | bool MCCacheReader::readDependencyTable() { |
| 277 | CACHE_READER_READ_SECTION(MCO_DependencyTable, mpCachedDependTable, |
| 278 | depend_tab); |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | |
| 283 | bool 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; |
| 292 | map<string, unsigned char const *>::iterator dep; |
| 293 | |
| 294 | dep = mDependencies.begin(); |
| 295 | for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) { |
| 296 | string const &depName = dep->first; |
| 297 | unsigned char const *depSHA1 = dep->second; |
| 298 | |
| 299 | MCO_Dependency *depCached =&mpCachedDependTable->table[i]; |
| 300 | char const *depCachedName = strPool[depCached->res_name_strp_index]; |
| 301 | //uint32_t depCachedType = depCached->res_type; |
| 302 | unsigned char const *depCachedSHA1 = depCached->sha1; |
| 303 | |
| 304 | if (depName != depCachedName) { |
| 305 | ALOGE("Cache dependency name mismatch:\n"); |
| 306 | ALOGE(" given: %s\n", depName.c_str()); |
| 307 | ALOGE(" cached: %s\n", depCachedName); |
| 308 | |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | if (memcmp(depSHA1, depCachedSHA1, 20) != 0) { |
| 313 | ALOGE("Cache dependency %s sha1 mismatch:\n", depCachedName); |
| 314 | |
| 315 | #define PRINT_SHA1(PREFIX, X, POSTFIX) \ |
| 316 | ALOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \ |
| 317 | "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \ |
| 318 | X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], \ |
| 319 | X[10],X[11],X[12],X[13],X[14],X[15],X[16],X[17],X[18],X[19]); |
| 320 | |
| 321 | PRINT_SHA1(" given: ", depSHA1, "\n"); |
| 322 | PRINT_SHA1(" cached: ", depCachedSHA1, "\n"); |
| 323 | |
| 324 | #undef PRINT_SHA1 |
| 325 | |
| 326 | return false; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return true; |
| 331 | } |
| 332 | |
| 333 | bool MCCacheReader::readVarNameList() { |
| 334 | CACHE_READER_READ_SECTION(MCO_String_Ptr, mpVarNameList, export_var_name_list); |
| 335 | vector<char const *> const &strPool = mpResult->mStringPool; |
| 336 | |
| 337 | mpResult->mpExportVars = (MCO_ExportVarList*) |
| 338 | malloc(sizeof(size_t) + |
| 339 | sizeof(void*) * export_var_name_list_raw->count); |
| 340 | if (!mpResult->mpExportVars) { |
| 341 | ALOGE("Unable to allocate for mpExportVars\n"); |
| 342 | return false; |
| 343 | } |
| 344 | mpResult->mpExportVars->count = export_var_name_list_raw->count; |
| 345 | |
| 346 | for (size_t i = 0; i < export_var_name_list_raw->count; ++i) { |
| 347 | mpResult->mpExportVars->cached_addr_list[i] = |
| 348 | rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_var_name_list_raw->strp_indexs[i]]); |
| 349 | #if DEBUG_MC_REFLECT |
| 350 | ALOGD("Get symbol address: %s -> %p", |
| 351 | strPool[export_var_name_list_raw->strp_indexs[i]], mpResult->mpExportVars->cached_addr_list[i]); |
| 352 | #endif |
| 353 | } |
| 354 | return true; |
| 355 | } |
| 356 | |
| 357 | bool MCCacheReader::readFuncNameList() { |
| 358 | CACHE_READER_READ_SECTION(MCO_String_Ptr, mpFuncNameList, export_func_name_list); |
| 359 | vector<char const *> const &strPool = mpResult->mStringPool; |
| 360 | |
| 361 | mpResult->mpExportFuncs = (MCO_ExportFuncList*) |
| 362 | malloc(sizeof(size_t) + |
| 363 | sizeof(void*) * export_func_name_list_raw->count); |
| 364 | if (!mpResult->mpExportFuncs) { |
| 365 | ALOGE("Unable to allocate for mpExportFuncs\n"); |
| 366 | return false; |
| 367 | } |
| 368 | mpResult->mpExportFuncs->count = export_func_name_list_raw->count; |
| 369 | |
| 370 | for (size_t i = 0; i < export_func_name_list_raw->count; ++i) { |
| 371 | mpResult->mpExportFuncs->cached_addr_list[i] = |
| 372 | rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_func_name_list_raw->strp_indexs[i]]); |
| 373 | #if DEBUG_MC_REFLECT |
| 374 | ALOGD("Get function address: %s -> %p", |
| 375 | strPool[export_func_name_list_raw->strp_indexs[i]], mpResult->mpExportFuncs->cached_addr_list[i]); |
| 376 | #endif |
| 377 | } |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | bool MCCacheReader::readForEachNameList() { |
| 382 | CACHE_READER_READ_SECTION(MCO_String_Ptr, mpForEachNameList, export_foreach_name_list); |
| 383 | vector<char const *> const &strPool = mpResult->mStringPool; |
| 384 | |
| 385 | mpResult->mpExportForEach = (MCO_ExportForEachList*) |
| 386 | malloc(sizeof(size_t) + |
| 387 | sizeof(void*) * export_foreach_name_list_raw->count); |
| 388 | if (!mpResult->mpExportForEach) { |
| 389 | ALOGE("Unable to allocate for mpExportForEach\n"); |
| 390 | return false; |
| 391 | } |
| 392 | mpResult->mpExportForEach->count = export_foreach_name_list_raw->count; |
| 393 | |
| 394 | for (size_t i = 0; i < export_foreach_name_list_raw->count; ++i) { |
| 395 | mpResult->mpExportForEach->cached_addr_list[i] = |
| 396 | rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_foreach_name_list_raw->strp_indexs[i]]); |
| 397 | #if DEBUG_MC_REFLECT |
| 398 | ALOGE("Get foreach function address: %s -> %p", |
| 399 | strPool[export_foreach_name_list_raw->strp_indexs[i]], mpResult->mpExportForEach->cached_addr_list[i]); |
| 400 | #endif |
| 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | |
| 405 | bool MCCacheReader::readPragmaList() { |
| 406 | CACHE_READER_READ_SECTION(MCO_PragmaList, mpPragmaList, pragma_list); |
| 407 | |
| 408 | vector<char const *> const &strPool = mpResult->mStringPool; |
| 409 | ScriptCached::PragmaList &pragmas = mpResult->mPragmas; |
| 410 | |
| 411 | for (size_t i = 0; i < pragma_list_raw->count; ++i) { |
| 412 | MCO_Pragma *pragma = &pragma_list_raw->list[i]; |
| 413 | pragmas.push_back(make_pair(strPool[pragma->key_strp_index], |
| 414 | strPool[pragma->value_strp_index])); |
| 415 | } |
| 416 | |
| 417 | return true; |
| 418 | } |
| 419 | |
| 420 | |
| 421 | bool MCCacheReader::readObjectSlotList() { |
| 422 | CACHE_READER_READ_SECTION(MCO_ObjectSlotList, |
| 423 | mpResult->mpObjectSlotList, object_slot_list); |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | bool MCCacheReader::readObjFile() { |
| 428 | if (mpResult->mCachedELFExecutable.size() != 0) { |
| 429 | ALOGE("Attempted to read cached object into a non-empty script"); |
| 430 | return false; |
| 431 | } |
| 432 | char readBuffer[1024]; |
| 433 | int readSize; |
| 434 | while ((readSize = mObjFile->read(readBuffer, 1024)) > 0) { |
| 435 | mpResult->mCachedELFExecutable.append(readBuffer, readBuffer + readSize); |
| 436 | } |
| 437 | if (readSize != 0) { |
| 438 | ALOGE("Read file Error"); |
| 439 | return false; |
| 440 | } |
| 441 | ALOGD("Read object file size %d", (int)mpResult->mCachedELFExecutable.size()); |
| 442 | |
| 443 | BCCRuntimeSymbolResolver bccRuntimesResolver; |
| 444 | LookupFunctionSymbolResolver<void *> rsSymbolResolver(mpSymbolLookupFn, |
| 445 | mpSymbolLookupContext); |
| 446 | |
| 447 | SymbolResolverProxy resolver; |
| 448 | resolver.chainResolver(bccRuntimesResolver); |
| 449 | resolver.chainResolver(rsSymbolResolver); |
| 450 | |
| 451 | mpResult->mRSExecutable = |
| 452 | rsloaderCreateExec((unsigned char *)&*(mpResult->mCachedELFExecutable.begin()), |
| 453 | mpResult->mCachedELFExecutable.size(), |
| 454 | SymbolResolverProxy::LookupFunction, &resolver); |
| 455 | |
| 456 | // Point ELF section headers to location of executable code, otherwise |
| 457 | // execution through GDB stops unexpectedly as GDB translates breakpoints |
| 458 | // in JITted code incorrectly (and complains about being unable to insert |
| 459 | // breakpoint at an invalid address) |
| 460 | rsloaderUpdateSectionHeaders(mpResult->mRSExecutable, |
| 461 | (unsigned char*) mpResult->mCachedELFExecutable.begin()); |
| 462 | |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | #undef CACHE_READER_READ_SECTION |
| 467 | |
| 468 | bool MCCacheReader::readRelocationTable() { |
| 469 | // TODO(logan): Not finished. |
| 470 | return true; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | bool MCCacheReader::relocate() { |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | } // namespace bcc |