blob: 82b7cdce8e8a52ae1d23a5de6480981818ccbab4 [file] [log] [blame]
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -07001/*
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#include "MCCacheReader.h"
18
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070019#include "DebugHelper.h"
20#include "FileHandle.h"
21#include "ScriptCached.h"
22#include "Runtime.h"
23
24#include <bcc/bcc_mccache.h>
25
26#include <llvm/ADT/OwningPtr.h>
27
28#include <errno.h>
29#include <sys/stat.h>
30#include <sys/types.h>
31
32#include <utility>
33#include <vector>
34
35#include <new>
36
37#include <stdlib.h>
38#include <string.h>
39
40using namespace std;
41
42
43#if USE_MCJIT
44namespace bcc {
45
46MCCacheReader::~MCCacheReader() {
47 if (mpHeader) { free(mpHeader); }
48 if (mpCachedDependTable) { free(mpCachedDependTable); }
49 if (mpPragmaList) { free(mpPragmaList); }
Joseph Wenf36637f2011-07-06 18:27:12 -070050 if (mpVarNameList) { free(mpVarNameList); }
51 if (mpFuncNameList) { free(mpFuncNameList); }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070052}
53
Shih-wei Liao6d0804b2011-06-19 11:30:00 -070054ScriptCached *MCCacheReader::readCacheFile(FileHandle *objFile,
55 FileHandle *infoFile,
56 Script *S) {
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070057 // Check file handle
58 if (!objFile || objFile->getFD() < 0 || !infoFile || infoFile->getFD() < 0) {
Doug Kwandeff7392011-07-11 00:48:07 -070059 return NULL;
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070060 }
61
62 mObjFile = objFile;
63 mInfoFile = infoFile;
64
65 // Allocate ScriptCached object
66 mpResult.reset(new (nothrow) ScriptCached(S));
67
68 if (!mpResult) {
69 LOGE("Unable to allocate ScriptCached object.\n");
70 return NULL;
71 }
72
73 bool result = checkFileSize()
74 && readHeader()
75 && checkHeader()
76 && checkMachineIntType()
77 && checkSectionOffsetAndSize()
78 && readStringPool()
79 && checkStringPool()
80 && readDependencyTable()
81 && checkDependency()
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070082 && readPragmaList()
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070083 && readObjectSlotList()
84 && readObjFile()
Joseph Wenf36637f2011-07-06 18:27:12 -070085 && readVarNameList()
86 && readFuncNameList()
87 //&& relocate()
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070088 ;
89
90 return result ? mpResult.take() : NULL;
91}
92
93
94bool MCCacheReader::checkFileSize() {
95 struct stat stfile;
96 if (fstat(mInfoFile->getFD(), &stfile) < 0) {
97 LOGE("Unable to stat cache file.\n");
98 return false;
99 }
100
101 mInfoFileSize = stfile.st_size;
102
103 if (mInfoFileSize < (off_t)sizeof(MCO_Header)) {
104 LOGE("Cache file is too small to be correct.\n");
105 return false;
106 }
107
108 return true;
109}
110
111
112bool MCCacheReader::readHeader() {
113 if (mInfoFile->seek(0, SEEK_SET) != 0) {
114 LOGE("Unable to seek to 0. (reason: %s)\n", strerror(errno));
115 return false;
116 }
117
118 mpHeader = (MCO_Header *)malloc(sizeof(MCO_Header));
119 if (!mpHeader) {
120 LOGE("Unable to allocate for cache header.\n");
121 return false;
122 }
123
124 if (mInfoFile->read(reinterpret_cast<char *>(mpHeader), sizeof(MCO_Header)) !=
125 (ssize_t)sizeof(MCO_Header)) {
126 LOGE("Unable to read cache header.\n");
127 return false;
128 }
129
130 // Dirty hack for libRS.
131 // TODO(all): This should be removed in the future.
132 if (mpHeader->libRS_threadable) {
133 mpResult->mLibRSThreadable = true;
134 }
135
136 return true;
137}
138
139
140bool MCCacheReader::checkHeader() {
141 if (memcmp(mpHeader->magic, OBCC_MAGIC, 4) != 0) {
142 LOGE("Bad magic word\n");
143 return false;
144 }
145
146 if (memcmp(mpHeader->version, OBCC_VERSION, 4) != 0) {
147 mpHeader->version[4 - 1] = '\0'; // ensure c-style string terminated
148 LOGI("Cache file format version mismatch: now %s cached %s\n",
149 OBCC_VERSION, mpHeader->version);
150 return false;
151 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700152 return true;
153}
154
155
156bool MCCacheReader::checkMachineIntType() {
157 uint32_t number = 0x00000001;
158
159 bool isLittleEndian = (*reinterpret_cast<char *>(&number) == 1);
160 if ((isLittleEndian && mpHeader->endianness != 'e') ||
161 (!isLittleEndian && mpHeader->endianness != 'E')) {
162 LOGE("Machine endianness mismatch.\n");
163 return false;
164 }
165
166 if ((unsigned int)mpHeader->sizeof_off_t != sizeof(off_t) ||
167 (unsigned int)mpHeader->sizeof_size_t != sizeof(size_t) ||
168 (unsigned int)mpHeader->sizeof_ptr_t != sizeof(void *)) {
169 LOGE("Machine integer size mismatch.\n");
170 return false;
171 }
172
173 return true;
174}
175
176
177bool MCCacheReader::checkSectionOffsetAndSize() {
178#define CHECK_SECTION_OFFSET(NAME) \
179 do { \
180 off_t offset = mpHeader-> NAME##_offset; \
181 off_t size = (off_t)mpHeader-> NAME##_size; \
182 \
183 if (mInfoFileSize < offset || mInfoFileSize < offset + size) { \
184 LOGE(#NAME " section overflow.\n"); \
185 return false; \
186 } \
187 \
188 if (offset % sizeof(int) != 0) { \
189 LOGE(#NAME " offset must aligned to %d.\n", (int)sizeof(int)); \
190 return false; \
191 } \
192 \
193 if (size < static_cast<off_t>(sizeof(size_t))) { \
194 LOGE(#NAME " size is too small to be correct.\n"); \
195 return false; \
196 } \
197 } while (0)
198
199 CHECK_SECTION_OFFSET(str_pool);
200 CHECK_SECTION_OFFSET(depend_tab);
201 //CHECK_SECTION_OFFSET(reloc_tab);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700202 CHECK_SECTION_OFFSET(pragma_list);
203
204#undef CHECK_SECTION_OFFSET
205
206 return true;
207}
208
209
210#define CACHE_READER_READ_SECTION(TYPE, AUTO_MANAGED_HOLDER, NAME) \
211 TYPE *NAME##_raw = (TYPE *)malloc(mpHeader->NAME##_size); \
212 \
213 if (!NAME##_raw) { \
214 LOGE("Unable to allocate for " #NAME "\n"); \
215 return false; \
216 } \
217 \
218 /* We have to ensure that some one will deallocate NAME##_raw */ \
219 AUTO_MANAGED_HOLDER = NAME##_raw; \
220 \
221 if (mInfoFile->seek(mpHeader->NAME##_offset, SEEK_SET) == -1) { \
222 LOGE("Unable to seek to " #NAME " section\n"); \
223 return false; \
224 } \
225 \
226 if (mInfoFile->read(reinterpret_cast<char *>(NAME##_raw), \
227 mpHeader->NAME##_size) != (ssize_t)mpHeader->NAME##_size) \
228 { \
229 LOGE("Unable to read " #NAME ".\n"); \
230 return false; \
231 }
232
233
234bool MCCacheReader::readStringPool() {
235 CACHE_READER_READ_SECTION(OBCC_StringPool,
236 mpResult->mpStringPoolRaw, str_pool);
237
238 char *str_base = reinterpret_cast<char *>(str_pool_raw);
239
240 vector<char const *> &pool = mpResult->mStringPool;
241 for (size_t i = 0; i < str_pool_raw->count; ++i) {
242 char *str = str_base + str_pool_raw->list[i].offset;
243 pool.push_back(str);
244 }
245
246 return true;
247}
248
249
250bool MCCacheReader::checkStringPool() {
251 OBCC_StringPool *poolR = mpResult->mpStringPoolRaw;
252 vector<char const *> &pool = mpResult->mStringPool;
253
254 // Ensure that every c-style string is ended with '\0'
255 for (size_t i = 0; i < poolR->count; ++i) {
256 if (pool[i][poolR->list[i].length] != '\0') {
257 LOGE("The %lu-th string does not end with '\\0'.\n", (unsigned long)i);
258 return false;
259 }
260 }
261
262 return true;
263}
264
265
266bool MCCacheReader::readDependencyTable() {
267 CACHE_READER_READ_SECTION(OBCC_DependencyTable, mpCachedDependTable,
268 depend_tab);
269 return true;
270}
271
272
273bool MCCacheReader::checkDependency() {
274 if (mDependencies.size() != mpCachedDependTable->count) {
275 LOGE("Dependencies count mismatch. (%lu vs %lu)\n",
276 (unsigned long)mDependencies.size(),
277 (unsigned long)mpCachedDependTable->count);
278 return false;
279 }
280
281 vector<char const *> &strPool = mpResult->mStringPool;
282 map<string, pair<uint32_t, unsigned char const *> >::iterator dep;
283
284 dep = mDependencies.begin();
285 for (size_t i = 0; i < mpCachedDependTable->count; ++i, ++dep) {
286 string const &depName = dep->first;
287 uint32_t depType = dep->second.first;
288 unsigned char const *depSHA1 = dep->second.second;
289
290 OBCC_Dependency *depCached =&mpCachedDependTable->table[i];
291 char const *depCachedName = strPool[depCached->res_name_strp_index];
292 uint32_t depCachedType = depCached->res_type;
293 unsigned char const *depCachedSHA1 = depCached->sha1;
294
295 if (depName != depCachedName) {
296 LOGE("Cache dependency name mismatch:\n");
297 LOGE(" given: %s\n", depName.c_str());
298 LOGE(" cached: %s\n", depCachedName);
299
300 return false;
301 }
302
303 if (memcmp(depSHA1, depCachedSHA1, 20) != 0) {
304 LOGE("Cache dependency %s sha1 mismatch:\n", depCachedName);
305
306#define PRINT_SHA1(PREFIX, X, POSTFIX) \
307 LOGE(PREFIX "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" \
308 "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x" POSTFIX, \
309 X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], \
310 X[10],X[11],X[12],X[13],X[14],X[15],X[16],X[17],X[18],X[19]);
311
312 PRINT_SHA1(" given: ", depSHA1, "\n");
313 PRINT_SHA1(" cached: ", depCachedSHA1, "\n");
314
315#undef PRINT_SHA1
316
317 return false;
318 }
319
320 if (depType != depCachedType) {
321 LOGE("Cache dependency %s resource type mismatch.\n", depCachedName);
322 return false;
323 }
324 }
325
326 return true;
327}
328
Joseph Wenf36637f2011-07-06 18:27:12 -0700329bool MCCacheReader::readVarNameList() {
330 CACHE_READER_READ_SECTION(OBCC_String_Ptr, mpVarNameList, export_var_name_list);
331 vector<char const *> const &strPool = mpResult->mStringPool;
332
Joseph Wen8eabcbf2011-07-07 12:37:48 -0700333 mpResult->mpExportVars = (OBCC_ExportVarList*)
334 malloc(sizeof(size_t) +
335 sizeof(void*) * export_var_name_list_raw->count);
336 if (!mpResult->mpExportVars) {
337 LOGE("Unable to allocate for mpExportVars\n");
338 return false;
339 }
340 mpResult->mpExportVars->count = export_var_name_list_raw->count;
341
Joseph Wenf36637f2011-07-06 18:27:12 -0700342 for (size_t i = 0; i < export_var_name_list_raw->count; ++i) {
343 mpResult->mpExportVars->cached_addr_list[i] =
344 rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_var_name_list_raw->strp_indexs[i]]);
345#if DEBUG_MCJIT_REFLECT
346 LOGD("Get symbol address: %s -> %p",
347 strPool[export_var_name_list_raw->strp_indexs[i]], mpResult->mpExportVars->cached_addr_list[i]);
348#endif
349 }
350 return true;
351}
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700352
Joseph Wenf36637f2011-07-06 18:27:12 -0700353bool MCCacheReader::readFuncNameList() {
354 CACHE_READER_READ_SECTION(OBCC_String_Ptr, mpFuncNameList, export_func_name_list);
355 vector<char const *> const &strPool = mpResult->mStringPool;
356
Joseph Wen8eabcbf2011-07-07 12:37:48 -0700357 mpResult->mpExportFuncs = (OBCC_ExportFuncList*)
358 malloc(sizeof(size_t) +
359 sizeof(void*) * export_func_name_list_raw->count);
360 if (!mpResult->mpExportFuncs) {
361 LOGE("Unable to allocate for mpExportFuncs\n");
362 return false;
363 }
364 mpResult->mpExportFuncs->count = export_func_name_list_raw->count;
365
Joseph Wenf36637f2011-07-06 18:27:12 -0700366 for (size_t i = 0; i < export_func_name_list_raw->count; ++i) {
367 mpResult->mpExportFuncs->cached_addr_list[i] =
368 rsloaderGetSymbolAddress(mpResult->mRSExecutable, strPool[export_func_name_list_raw->strp_indexs[i]]);
369#if DEBUG_MCJIT_REFLECT
370 LOGD("Get function address: %s -> %p",
371 strPool[export_func_name_list_raw->strp_indexs[i]], mpResult->mpExportFuncs->cached_addr_list[i]);
372#endif
373 }
374 return true;
375}
376
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700377bool MCCacheReader::readPragmaList() {
378 CACHE_READER_READ_SECTION(OBCC_PragmaList, mpPragmaList, pragma_list);
379
380 vector<char const *> const &strPool = mpResult->mStringPool;
381 ScriptCached::PragmaList &pragmas = mpResult->mPragmas;
382
383 for (size_t i = 0; i < pragma_list_raw->count; ++i) {
384 OBCC_Pragma *pragma = &pragma_list_raw->list[i];
385 pragmas.push_back(make_pair(strPool[pragma->key_strp_index],
386 strPool[pragma->value_strp_index]));
387 }
388
389 return true;
390}
391
392
393bool MCCacheReader::readObjectSlotList() {
394 CACHE_READER_READ_SECTION(OBCC_ObjectSlotList,
395 mpResult->mpObjectSlotList, object_slot_list);
396 return true;
397}
398
399void *MCCacheReader::resolveSymbolAdapter(void *context, char const *name) {
400 MCCacheReader *self = reinterpret_cast<MCCacheReader *>(context);
401
402 if (void *Addr = FindRuntimeFunction(name)) {
403 return Addr;
404 }
405
406 if (self->mpSymbolLookupFn) {
Shih-wei Liao6d0804b2011-06-19 11:30:00 -0700407 if (void *Addr =
408 self->mpSymbolLookupFn(self->mpSymbolLookupContext, name)) {
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700409 return Addr;
410 }
411 }
412
413 LOGE("Unable to resolve symbol: %s\n", name);
414 return NULL;
415}
416
417bool MCCacheReader::readObjFile() {
418 llvm::SmallVector<char, 1024> mEmittedELFExecutable;
419 char readBuffer[1024];
420 int readSize;
421 while ((readSize = mObjFile->read(readBuffer, 1024)) > 0) {
422 mEmittedELFExecutable.append(readBuffer, readBuffer + readSize);
423 }
424 if (readSize != 0) {
425 LOGE("Read file Error");
426 return false;
427 }
Ying Wanga61d5012011-07-01 15:54:00 -0700428 LOGD("Read object file size %d", (int)mEmittedELFExecutable.size());
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700429 mpResult->mRSExecutable =
430 rsloaderCreateExec((unsigned char *)&*mEmittedELFExecutable.begin(),
431 mEmittedELFExecutable.size(),
432 &resolveSymbolAdapter, this);
433 return true;
434}
435
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700436#undef CACHE_READER_READ_SECTION
437
438bool MCCacheReader::readRelocationTable() {
439 // TODO(logan): Not finished.
440 return true;
441}
442
443
444bool MCCacheReader::relocate() {
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700445 return true;
446}
447
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700448} // namespace bcc
449#endif