Logan | f7f0ac5 | 2011-01-07 03:53:43 +0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 39 | #include <string.h> |
| 40 | |
| 41 | using namespace std; |
| 42 | |
| 43 | |
| 44 | namespace bcc { |
| 45 | |
| 46 | ScriptCached *CacheReader::readCacheFile(FileHandle *file) { |
| 47 | // Check file handle |
| 48 | if (!file || file->getFD() < 0) { |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | // Allocate ScriptCached object |
| 53 | mResult.reset(new (nothrow) ScriptCached(mpOwner)); |
| 54 | |
| 55 | if (!mResult) { |
| 56 | LOGE("Unable to allocate ScriptCached object.\n"); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
| 60 | bool result = checkFileSize() |
| 61 | && readHeader() |
| 62 | && checkHeader() |
| 63 | && checkMachineIntType() |
| 64 | && checkSectionOffsetAndSize() |
| 65 | && readStringPool() |
| 66 | && checkStringPool() |
| 67 | && readDependencyTable() |
| 68 | && checkDependency() |
| 69 | && readExportVarList() |
| 70 | && readExportFuncList() |
| 71 | && readPragmaList() |
| 72 | && readFuncTable() |
| 73 | && readContext() |
| 74 | && checkContext() |
| 75 | //&& readRelocationTable() |
| 76 | //&& relocate() |
| 77 | ; |
| 78 | |
| 79 | |
| 80 | // TODO(logan): This is the hack for libRS. Should be turned on |
| 81 | // before caching is ready to go. |
| 82 | #if 0 |
| 83 | // Check the cache file has __isThreadable or not. If it is set, |
| 84 | // then we have to call mpSymbolLookupFn for __clearThreadable. |
| 85 | if (mHeader->libRSThreadable && mpSymbolLookupFn) { |
| 86 | mpSymbolLookupFn(mpSymbolLookupContext, "__clearThreadable"); |
| 87 | } |
| 88 | #endif |
| 89 | |
| 90 | return result ? mResult.take() : NULL; |
| 91 | } |
| 92 | |
| 93 | |
| 94 | bool 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) || |
| 104 | mFileSize < (off_t)BCC_CONTEXT_SIZE) { |
| 105 | LOGE("Cache file is too small to be correct.\n"); |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | bool 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 | |
| 119 | mHeader = (OBCC_Header *)malloc(sizeof(OBCC_Header)); |
| 120 | if (!mHeader) { |
| 121 | LOGE("Unable to allocate for cache header.\n"); |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | if (mFile->read(reinterpret_cast<char *>(mHeader), sizeof(OBCC_Header)) != |
| 126 | (ssize_t)sizeof(OBCC_Header)) { |
| 127 | LOGE("Unable to read cache header.\n"); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | bool CacheReader::checkHeader() { |
| 136 | if (memcmp(mHeader->magic, OBCC_MAGIC, 4) != 0) { |
| 137 | LOGE("Bad magic word\n"); |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | if (memcmp(mHeader->version, OBCC_VERSION, 4) != 0) { |
| 142 | LOGE("Bad oBCC version 0x%08x\n", |
| 143 | *reinterpret_cast<uint32_t *>(mHeader->version)); |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | return true; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | bool CacheReader::checkMachineIntType() { |
| 152 | uint32_t number = 0x00000001; |
| 153 | |
| 154 | bool isLittleEndian = (*reinterpret_cast<char *>(&number) == 1); |
| 155 | if ((isLittleEndian && mHeader->endianness != 'e') || |
| 156 | (!isLittleEndian && mHeader->endianness != 'E')) { |
| 157 | LOGE("Machine endianness mismatch.\n"); |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | if ((unsigned int)mHeader->sizeof_off_t != sizeof(off_t) || |
| 162 | (unsigned int)mHeader->sizeof_size_t != sizeof(size_t) || |
| 163 | (unsigned int)mHeader->sizeof_ptr_t != sizeof(void *)) { |
| 164 | LOGE("Machine integer size mismatch.\n"); |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | return true; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | bool CacheReader::checkSectionOffsetAndSize() { |
| 173 | #define CHECK_SECTION_OFFSET(NAME) \ |
| 174 | do { \ |
| 175 | off_t offset = mHeader-> NAME##_offset; \ |
| 176 | off_t size = (off_t)mHeader-> NAME##_size; \ |
| 177 | \ |
| 178 | if (mFileSize < offset || mFileSize < offset + size) { \ |
| 179 | LOGE(#NAME " section overflow.\n"); \ |
| 180 | return false; \ |
| 181 | } \ |
| 182 | \ |
| 183 | if (offset % sizeof(int) != 0) { \ |
| 184 | LOGE(#NAME " offset must aligned to %d.\n", sizeof(int)); \ |
| 185 | return false; \ |
| 186 | } \ |
| 187 | \ |
| 188 | if (size < static_cast<off_t>(sizeof(size_t))) { \ |
| 189 | LOGE(#NAME " size is too small to be correct.\n"); \ |
| 190 | return false; \ |
| 191 | } \ |
| 192 | } while (0) |
| 193 | |
| 194 | CHECK_SECTION_OFFSET(str_pool); |
| 195 | CHECK_SECTION_OFFSET(depend_tab); |
| 196 | CHECK_SECTION_OFFSET(reloc_tab); |
| 197 | CHECK_SECTION_OFFSET(export_var_list); |
| 198 | CHECK_SECTION_OFFSET(export_func_list); |
| 199 | CHECK_SECTION_OFFSET(pragma_list); |
| 200 | |
| 201 | #undef CHECK_SECTION_OFFSET |
| 202 | |
| 203 | if (mFileSize < mHeader->context_offset || |
| 204 | mFileSize < mHeader->context_offset + BCC_CONTEXT_SIZE) { |
| 205 | LOGE("context section overflow.\n"); |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | long pagesize = sysconf(_SC_PAGESIZE); |
| 210 | if (mHeader->context_offset % pagesize != 0) { |
| 211 | LOGE("context offset must aligned to pagesize.\n"); |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | // TODO(logan): Move this to some where else. |
| 216 | if ((uintptr_t)mHeader->context_cached_addr % pagesize != 0) { |
| 217 | LOGE("cached address is not aligned to pagesize.\n"); |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | return true; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | bool CacheReader::readStringPool() { |
| 226 | OBCC_StringPool *poolR = (OBCC_StringPool *)malloc(mHeader->str_pool_size); |
| 227 | |
| 228 | if (!poolR) { |
| 229 | LOGE("Unable to allocate string pool.\n"); |
| 230 | return false; |
| 231 | } |
| 232 | |
| 233 | mResult->mpStringPoolRaw = poolR; // Managed by mResult from now on. |
| 234 | |
| 235 | if (mFile->read(reinterpret_cast<char *>(poolR), mHeader->str_pool_size) != |
| 236 | (ssize_t)mHeader->str_pool_size) { |
| 237 | LOGE("Unable to read string pool.\n"); |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | vector<char const *> &pool = mResult->mStringPool; |
| 242 | |
| 243 | for (size_t i = 0; i < poolR->count; ++i) { |
| 244 | char *str = reinterpret_cast<char *>(poolR) + poolR->list[i].offset; |
| 245 | pool.push_back(str); |
| 246 | } |
| 247 | |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | bool CacheReader::checkStringPool() { |
| 253 | OBCC_StringPool *poolR = mResult->mpStringPoolRaw; |
| 254 | vector<char const *> &pool = mResult->mStringPool; |
| 255 | |
| 256 | // Ensure that every c-style string is ended with '\0' |
| 257 | for (size_t i = 0; i < poolR->count; ++i) { |
| 258 | if (pool[i][poolR->list[i].length] != '\0') { |
| 259 | LOGE("The %lu-th string does not end with '\\0'.\n", (unsigned long)i); |
| 260 | return false; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return true; |
| 265 | } |
| 266 | |
| 267 | |
| 268 | bool CacheReader::readDependencyTable() { |
| 269 | // TODO(logan): Not finished. |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | bool CacheReader::checkDependency() { |
| 275 | // TODO(logan): Not finished. |
| 276 | return true; |
| 277 | } |
| 278 | |
| 279 | bool CacheReader::readExportVarList() { |
| 280 | char *varList = (char *)malloc(mHeader->export_var_list_size); |
| 281 | |
| 282 | if (!varList) { |
| 283 | LOGE("Unable to allocate exported variable list.\n"); |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | mResult->mpExportVars = reinterpret_cast<OBCC_ExportVarList *>(varList); |
| 288 | |
| 289 | if (mFile->seek(mHeader->export_var_list_offset, SEEK_SET) == -1) { |
| 290 | LOGE("Unable to seek to exported variable list section.\n"); |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | if (mFile->read(varList, mHeader->export_var_list_size) != |
| 295 | (ssize_t)mHeader->export_var_list_size) { |
| 296 | LOGE("Unable to read exported variable list.\n"); |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | bool CacheReader::readExportFuncList() { |
| 305 | char *funcList = (char *)malloc(mHeader->export_func_list_size); |
| 306 | |
| 307 | if (!funcList) { |
| 308 | LOGE("Unable to allocate exported function list.\n"); |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | mResult->mpExportFuncs = reinterpret_cast<OBCC_ExportFuncList *>(funcList); |
| 313 | |
| 314 | if (mFile->seek(mHeader->export_func_list_offset, SEEK_SET) == -1) { |
| 315 | LOGE("Unable to seek to exported function list section.\n"); |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | if (mFile->read(funcList, mHeader->export_func_list_size) != |
| 320 | (ssize_t)mHeader->export_func_list_size) { |
| 321 | LOGE("Unable to read exported function list.\n"); |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | |
| 329 | bool CacheReader::readPragmaList() { |
| 330 | OBCC_PragmaList *pragmaListRaw = |
| 331 | (OBCC_PragmaList *)malloc(mHeader->pragma_list_size); |
| 332 | |
| 333 | if (!pragmaListRaw) { |
| 334 | LOGE("Unable to allocate pragma list.\n"); |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | if (mFile->seek(mHeader->pragma_list_offset, SEEK_SET) == -1) { |
| 339 | LOGE("Unable to seek to pragma list section.\n"); |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | if (mFile->read(reinterpret_cast<char *>(pragmaListRaw), |
| 344 | mHeader->pragma_list_size) != |
| 345 | (ssize_t)mHeader->pragma_list_size) { |
| 346 | LOGE("Unable to read pragma list.\n"); |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | vector<char const *> const &strPool = mResult->mStringPool; |
| 351 | ScriptCached::PragmaList &pragmas = mResult->mPragmas; |
| 352 | |
| 353 | for (size_t i = 0; i < pragmaListRaw->count; ++i) { |
| 354 | OBCC_Pragma *pragma = &pragmaListRaw->list[i]; |
| 355 | pragmas.push_back(make_pair(strPool[pragma->key_strp_index], |
| 356 | strPool[pragma->value_strp_index])); |
| 357 | } |
| 358 | |
| 359 | return true; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | bool CacheReader::readFuncTable() { |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | |
| 368 | bool CacheReader::readContext() { |
| 369 | mResult->mContext = allocateContext(mHeader->context_cached_addr, |
| 370 | mFile->getFD(), |
| 371 | mHeader->context_offset); |
| 372 | |
| 373 | if (!mResult->mContext) { |
| 374 | // Unable to allocate at cached address. Give up. |
| 375 | return false; |
| 376 | |
| 377 | // TODO(logan): If relocation is fixed, we should try to allocate the |
| 378 | // code in different location, and relocate the context. |
| 379 | } |
| 380 | |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | bool CacheReader::checkContext() { |
| 386 | uint32_t sum = mHeader->context_parity_checksum; |
| 387 | uint32_t *ptr = reinterpret_cast<uint32_t *>(mResult->mContext); |
| 388 | |
| 389 | for (size_t i = 0; i < BCC_CONTEXT_SIZE / sizeof(uint32_t); ++i) { |
| 390 | sum ^= *ptr++; |
| 391 | } |
| 392 | |
| 393 | if (sum != 0) { |
| 394 | LOGE("Checksum check failed\n"); |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | LOGI("Passed checksum even parity verification.\n"); |
| 399 | return true; |
| 400 | } |
| 401 | |
| 402 | |
| 403 | bool CacheReader::readRelocationTable() { |
| 404 | // TODO(logan): Not finished. |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | |
| 409 | bool CacheReader::relocate() { |
| 410 | // TODO(logan): Not finished. |
| 411 | return true; |
| 412 | } |
| 413 | |
| 414 | |
| 415 | } // namespace bcc |