blob: 8fb8fd0688eb106ae9f073e82e97e651d8fd28e7 [file] [log] [blame]
Logancf3e5212010-12-29 01:44:55 +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 "Script.h"
21
Loganf7f0ac52011-01-07 03:53:43 +080022#include "CacheReader.h"
Logan89eb47f2011-01-07 10:45:16 +080023#include "CacheWriter.h"
Logan04329712011-01-06 06:10:57 +080024#include "FileHandle.h"
Logancf3e5212010-12-29 01:44:55 +080025#include "ScriptCompiled.h"
Logan9a5f8682011-01-07 06:09:57 +080026#include "ScriptCached.h"
27#include "Sha1Helper.h"
Logancf3e5212010-12-29 01:44:55 +080028
Logan89eb47f2011-01-07 10:45:16 +080029#include <errno.h>
Logancf3e5212010-12-29 01:44:55 +080030
Logan89eb47f2011-01-07 10:45:16 +080031#include <new>
32#include <string.h>
Logan033f46e2011-01-06 05:51:24 +080033#include <cutils/properties.h>
34
Logan89eb47f2011-01-07 10:45:16 +080035
Loganecf4cbd2011-01-06 05:34:11 +080036namespace {
37
38// Input: cacheDir
39// Input: resName
40// Input: extName
41//
42// Note: cacheFile = resName + extName
43//
44// Output: Returns cachePath == cacheDir + cacheFile
45char *genCacheFileName(const char *cacheDir,
46 const char *resName,
47 const char *extName) {
48 char cachePath[512];
49 char cacheFile[sizeof(cachePath)];
50 const size_t kBufLen = sizeof(cachePath) - 1;
51
52 cacheFile[0] = '\0';
53 // Note: resName today is usually something like
54 // "/com.android.fountain:raw/fountain"
55 if (resName[0] != '/') {
56 // Get the absolute path of the raw/***.bc file.
57
58 // Generate the absolute path. This doesn't do everything it
59 // should, e.g. if resName is "./out/whatever" it doesn't crunch
60 // the leading "./" out because this if-block is not triggered,
61 // but it'll make do.
62 //
63 if (getcwd(cacheFile, kBufLen) == NULL) {
64 LOGE("Can't get CWD while opening raw/***.bc file\n");
65 return NULL;
66 }
67 // Append "/" at the end of cacheFile so far.
68 strncat(cacheFile, "/", kBufLen);
69 }
70
71 // cacheFile = resName + extName
72 //
73 strncat(cacheFile, resName, kBufLen);
74 if (extName != NULL) {
75 // TODO(srhines): strncat() is a bit dangerous
76 strncat(cacheFile, extName, kBufLen);
77 }
78
79 // Turn the path into a flat filename by replacing
80 // any slashes after the first one with '@' characters.
81 char *cp = cacheFile + 1;
82 while (*cp != '\0') {
83 if (*cp == '/') {
84 *cp = '@';
85 }
86 cp++;
87 }
88
89 // Tack on the file name for the actual cache file path.
90 strncpy(cachePath, cacheDir, kBufLen);
91 strncat(cachePath, cacheFile, kBufLen);
92
93 LOGV("Cache file for '%s' '%s' is '%s'\n", resName, extName, cachePath);
94 return strdup(cachePath);
95}
96
Logan033f46e2011-01-06 05:51:24 +080097bool getBooleanProp(const char *str) {
98 char buf[PROPERTY_VALUE_MAX];
99 property_get(str, buf, "0");
100 return strcmp(buf, "0") != 0;
101}
102
Loganecf4cbd2011-01-06 05:34:11 +0800103} // namespace anonymous
104
Logancf3e5212010-12-29 01:44:55 +0800105namespace bcc {
106
107Script::~Script() {
108 if (mStatus == ScriptStatus::Compiled) {
109 delete mCompiled;
110 }
111}
112
113
114int Script::readBC(const char *bitcode,
115 size_t bitcodeSize,
Logancf3e5212010-12-29 01:44:55 +0800116 const BCCchar *resName,
117 const BCCchar *cacheDir) {
Loganecf4cbd2011-01-06 05:34:11 +0800118 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800119 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800120 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800121 return 1;
122 }
123
Loganecf4cbd2011-01-06 05:34:11 +0800124 sourceBC = bitcode;
125 sourceResName = resName;
126 sourceSize = bitcodeSize;
127
Logan033f46e2011-01-06 05:51:24 +0800128 if (cacheDir && resName) {
129 cacheFile = genCacheFileName(cacheDir, resName, ".oBCC");
130 }
131
Loganecf4cbd2011-01-06 05:34:11 +0800132 return 0;
Logancf3e5212010-12-29 01:44:55 +0800133}
134
135
136int Script::readModule(llvm::Module *module) {
137 if (mStatus != ScriptStatus::Unknown) {
138 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800139 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800140 return 1;
141 }
142
Loganecf4cbd2011-01-06 05:34:11 +0800143 sourceModule = module;
144 return 0;
Logancf3e5212010-12-29 01:44:55 +0800145}
146
147
148int Script::linkBC(const char *bitcode, size_t bitcodeSize) {
Logan3133c412011-01-06 06:15:40 +0800149 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800150 mErrorCode = BCC_INVALID_OPERATION;
Logan3133c412011-01-06 06:15:40 +0800151 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800152 return 1;
153 }
154
Logan3133c412011-01-06 06:15:40 +0800155 libraryBC = bitcode;
156 librarySize = bitcodeSize;
157 return 0;
Logancf3e5212010-12-29 01:44:55 +0800158}
159
160
Logancf3e5212010-12-29 01:44:55 +0800161int Script::compile() {
Loganecf4cbd2011-01-06 05:34:11 +0800162 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800163 mErrorCode = BCC_INVALID_OPERATION;
Loganecf4cbd2011-01-06 05:34:11 +0800164 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800165 return 1;
166 }
167
Loganecf4cbd2011-01-06 05:34:11 +0800168 // Load Cache File
Logan033f46e2011-01-06 05:51:24 +0800169 if (cacheFile && internalLoadCache() == 0) {
170 return 0;
171 }
Loganecf4cbd2011-01-06 05:34:11 +0800172
Logan033f46e2011-01-06 05:51:24 +0800173 return internalCompile();
174}
175
176
177int Script::internalLoadCache() {
178 if (getBooleanProp("debug.bcc.nocache")) {
179 // Android system environment property disable the cache mechanism by
180 // setting "debug.bcc.nocache". So we will not load the cache file any
181 // way.
182 return 1;
183 }
184
Logan04329712011-01-06 06:10:57 +0800185 if (!cacheFile) {
186 // The application developer has not specify resName or cacheDir, so
187 // we don't know where to open the cache file.
188 return 1;
Logan033f46e2011-01-06 05:51:24 +0800189 }
Logan04329712011-01-06 06:10:57 +0800190
Logana2e15af2011-01-07 11:46:08 +0800191 if (sourceBC) {
192 // If we are going to create cache file. We have to calculate sha1sum
193 // first (no matter we can open the file now or not.)
194 calcSHA1(sourceSHA1, sourceBC, sourceSize);
195 }
196
Logan04329712011-01-06 06:10:57 +0800197 FileHandle file;
198
Logan77124df2011-01-06 06:22:17 +0800199 if (file.open(cacheFile, OpenMode::Read) < 0) {
Logan04329712011-01-06 06:10:57 +0800200 // Unable to open the cache file in read mode.
201 return 1;
202 }
203
Logane7eb7732011-01-07 07:11:56 +0800204 CacheReader reader;
Logan04329712011-01-06 06:10:57 +0800205
Logan9a5f8682011-01-07 06:09:57 +0800206 // Dependencies
Logane1323992011-01-12 04:47:13 +0800207#if defined(USE_LIBBCC_SHA1SUM)
Logan9a5f8682011-01-07 06:09:57 +0800208 reader.addDependency(BCC_FILE_RESOURCE, pathLibBCC, sha1LibBCC);
Logane1323992011-01-12 04:47:13 +0800209#endif
210
Logan9a5f8682011-01-07 06:09:57 +0800211 reader.addDependency(BCC_FILE_RESOURCE, pathLibRS, sha1LibRS);
212
213 if (sourceBC) {
Logan9a5f8682011-01-07 06:09:57 +0800214 reader.addDependency(BCC_APK_RESOURCE, sourceResName, sourceSHA1);
215 }
216
217 // Read cache file
Logane7eb7732011-01-07 07:11:56 +0800218 ScriptCached *cached = reader.readCacheFile(&file, this);
Logan04329712011-01-06 06:10:57 +0800219 if (!cached) {
220 return 1;
221 }
222
223 mCached = cached;
224 mStatus = ScriptStatus::Cached;
Logan033f46e2011-01-06 05:51:24 +0800225
Loganf3c83ce2011-01-07 06:36:33 +0800226 // Dirty hack for libRS.
227 // TODO(all): This dirty hack should be removed in the future.
228 if (cached->isLibRSThreadable() && mpExtSymbolLookupFn) {
229 mpExtSymbolLookupFn(mpExtSymbolLookupFnContext, "__clearThreadable");
230 }
231
Loganf7f0ac52011-01-07 03:53:43 +0800232 return 0;
Logan033f46e2011-01-06 05:51:24 +0800233}
234
235
236int Script::internalCompile() {
237 // Create the ScriptCompiled object
Loganecf4cbd2011-01-06 05:34:11 +0800238 mCompiled = new (nothrow) ScriptCompiled(this);
239
240 if (!mCompiled) {
241 mErrorCode = BCC_OUT_OF_MEMORY;
242 LOGE("Out of memory: %s %d\n", __FILE__, __LINE__);
243 return 1;
244 }
245
246 mStatus = ScriptStatus::Compiled;
247
Logan033f46e2011-01-06 05:51:24 +0800248 // Register symbol lookup function
Loganecf4cbd2011-01-06 05:34:11 +0800249 if (mpExtSymbolLookupFn) {
250 mCompiled->registerSymbolCallback(mpExtSymbolLookupFn,
251 mpExtSymbolLookupFnContext);
252 }
253
Logan033f46e2011-01-06 05:51:24 +0800254 // Setup the source bitcode / module
Loganecf4cbd2011-01-06 05:34:11 +0800255 if (sourceBC) {
Loganf30a6712011-01-06 05:55:34 +0800256 if (mCompiled->readBC(sourceBC, sourceSize, sourceResName, 0) != 0) {
Logan65719812011-01-07 11:17:14 +0800257 LOGE("Unable to readBC, bitcode=%p, size=%lu\n",
258 sourceBC, (unsigned long)sourceSize);
Logan033f46e2011-01-06 05:51:24 +0800259 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800260 }
Logan65719812011-01-07 11:17:14 +0800261
Shih-wei Liaoefbd0ed2011-01-07 06:36:25 -0800262 LOGI("Load sourceBC\n");
Loganecf4cbd2011-01-06 05:34:11 +0800263 } else if (sourceModule) {
Logan033f46e2011-01-06 05:51:24 +0800264 if (mCompiled->readModule(sourceModule) != 0) {
265 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800266 }
Logan65719812011-01-07 11:17:14 +0800267
Shih-wei Liaoefbd0ed2011-01-07 06:36:25 -0800268 LOGI("Load sourceModule\n");
Loganecf4cbd2011-01-06 05:34:11 +0800269 }
270
Logan3133c412011-01-06 06:15:40 +0800271 // Link the source module with the library module
Logan04329712011-01-06 06:10:57 +0800272 if (libraryBC) {
273 if (mCompiled->linkBC(libraryBC, librarySize) != 0) {
274 return 1;
275 }
Logan65719812011-01-07 11:17:14 +0800276
277 LOGE("Load Library\n");
Logan04329712011-01-06 06:10:57 +0800278 }
Loganecf4cbd2011-01-06 05:34:11 +0800279
Logan3133c412011-01-06 06:15:40 +0800280 // Compile and JIT the code
Logan04329712011-01-06 06:10:57 +0800281 if (mCompiled->compile() != 0) {
Logan65719812011-01-07 11:17:14 +0800282 LOGE("Unable to compile.\n");
Logan04329712011-01-06 06:10:57 +0800283 return 1;
284 }
285
286 // TODO(logan): Write the cache out
Logan04329712011-01-06 06:10:57 +0800287 if (cacheFile && !getBooleanProp("debug.bcc.nocache")) {
Logan89eb47f2011-01-07 10:45:16 +0800288 FileHandle file;
Logan04329712011-01-06 06:10:57 +0800289
Logan77124df2011-01-06 06:22:17 +0800290 if (file.open(cacheFile, OpenMode::Write) >= 0) {
Logan04329712011-01-06 06:10:57 +0800291 CacheWriter writer;
Logana27a83f2011-01-07 10:25:48 +0800292
Logana2e15af2011-01-07 11:46:08 +0800293 // Dependencies
Logane1323992011-01-12 04:47:13 +0800294#if defined(USE_LIBBCC_SHA1SUM)
Logana2e15af2011-01-07 11:46:08 +0800295 writer.addDependency(BCC_FILE_RESOURCE, pathLibBCC, sha1LibBCC);
Logane1323992011-01-12 04:47:13 +0800296#endif
Logana2e15af2011-01-07 11:46:08 +0800297 writer.addDependency(BCC_FILE_RESOURCE, pathLibRS, sha1LibRS);
298
299 if (sourceBC) {
300 writer.addDependency(BCC_APK_RESOURCE, sourceResName, sourceSHA1);
301 }
302
Logana27a83f2011-01-07 10:25:48 +0800303 // libRS is threadable dirty hack
304 // TODO: This should be removed in the future
305 uint32_t libRS_threadable = 0;
306 if (mpExtSymbolLookupFn) {
Logan89eb47f2011-01-07 10:45:16 +0800307 libRS_threadable =
308 (uint32_t)mpExtSymbolLookupFn(mpExtSymbolLookupFnContext,
309 "__isThreadable");
Logana27a83f2011-01-07 10:25:48 +0800310 }
311
Logan89eb47f2011-01-07 10:45:16 +0800312 if (!writer.writeCacheFile(&file, this, libRS_threadable)) {
313 file.truncate();
314 file.close();
Logana27a83f2011-01-07 10:25:48 +0800315
Logan89eb47f2011-01-07 10:45:16 +0800316 if (unlink(cacheFile) != 0) {
317 LOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
318 cacheFile, strerror(errno));
319 }
320 }
321 }
Logan04329712011-01-06 06:10:57 +0800322 }
Logan04329712011-01-06 06:10:57 +0800323
324 return 0;
Logancf3e5212010-12-29 01:44:55 +0800325}
326
327
328char const *Script::getCompilerErrorMessage() {
329 if (mStatus != ScriptStatus::Compiled) {
330 mErrorCode = BCC_INVALID_OPERATION;
331 return NULL;
332 }
333
334 return mCompiled->getCompilerErrorMessage();
335}
336
337
338void *Script::lookup(const char *name) {
Logan89eb47f2011-01-07 10:45:16 +0800339 switch (mStatus) {
340 case ScriptStatus::Compiled:
341 return mCompiled->lookup(name);
342
343 case ScriptStatus::Cached:
344 return mCached->lookup(name);
345
346 default:
Logancf3e5212010-12-29 01:44:55 +0800347 mErrorCode = BCC_INVALID_OPERATION;
348 return NULL;
349 }
Logancf3e5212010-12-29 01:44:55 +0800350}
351
352
353void Script::getExportVars(BCCsizei *actualVarCount,
354 BCCsizei maxVarCount,
355 BCCvoid **vars) {
Logan89eb47f2011-01-07 10:45:16 +0800356 switch (mStatus) {
357 case ScriptStatus::Compiled:
358 mCompiled->getExportVars(actualVarCount, maxVarCount, vars);
359 break;
Logancf3e5212010-12-29 01:44:55 +0800360
Logan89eb47f2011-01-07 10:45:16 +0800361 case ScriptStatus::Cached:
362 mCached->getExportVars(actualVarCount, maxVarCount, vars);
363 break;
364
365 default:
366 mErrorCode = BCC_INVALID_OPERATION;
367 }
Logancf3e5212010-12-29 01:44:55 +0800368}
369
370
371void Script::getExportFuncs(BCCsizei *actualFuncCount,
372 BCCsizei maxFuncCount,
373 BCCvoid **funcs) {
Logan89eb47f2011-01-07 10:45:16 +0800374 switch (mStatus) {
375 case ScriptStatus::Compiled:
376 mCompiled->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
377 break;
Logancf3e5212010-12-29 01:44:55 +0800378
Logan89eb47f2011-01-07 10:45:16 +0800379 case ScriptStatus::Cached:
380 mCached->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
381 break;
382
383 default:
384 mErrorCode = BCC_INVALID_OPERATION;
385 }
Logancf3e5212010-12-29 01:44:55 +0800386}
387
388
389void Script::getPragmas(BCCsizei *actualStringCount,
390 BCCsizei maxStringCount,
391 BCCchar **strings) {
Logan89eb47f2011-01-07 10:45:16 +0800392 switch (mStatus) {
393 case ScriptStatus::Compiled:
394 mCompiled->getPragmas(actualStringCount, maxStringCount, strings);
395 break;
Logancf3e5212010-12-29 01:44:55 +0800396
Logan89eb47f2011-01-07 10:45:16 +0800397 case ScriptStatus::Cached:
398 mCached->getPragmas(actualStringCount, maxStringCount, strings);
399 break;
400
401 default:
402 mErrorCode = BCC_INVALID_OPERATION;
403 }
Logancf3e5212010-12-29 01:44:55 +0800404}
405
406
407void Script::getFunctions(BCCsizei *actualFunctionCount,
408 BCCsizei maxFunctionCount,
409 BCCchar **functions) {
Logan89eb47f2011-01-07 10:45:16 +0800410 switch (mStatus) {
411 case ScriptStatus::Compiled:
412 mCompiled->getFunctions(actualFunctionCount, maxFunctionCount, functions);
413 break;
Logancf3e5212010-12-29 01:44:55 +0800414
Logan89eb47f2011-01-07 10:45:16 +0800415 case ScriptStatus::Cached:
416 mCached->getFunctions(actualFunctionCount, maxFunctionCount, functions);
417 break;
418
419 default:
420 mErrorCode = BCC_INVALID_OPERATION;
421 }
Logancf3e5212010-12-29 01:44:55 +0800422}
423
Logana27a83f2011-01-07 10:25:48 +0800424char *Script::getContext() {
425 switch (mStatus) {
426 case ScriptStatus::Compiled:
427 return mCompiled->getContext();
428
429 case ScriptStatus::Cached:
430 return mCached->getContext();
431
432 default:
433 mErrorCode = BCC_INVALID_OPERATION;
Logan02286cb2011-01-07 00:30:47 +0800434 return NULL;
435 }
Logan02286cb2011-01-07 00:30:47 +0800436}
437
Logancf3e5212010-12-29 01:44:55 +0800438
439void Script::getFunctionBinary(BCCchar *function,
440 BCCvoid **base,
441 BCCsizei *length) {
Logan216ec712011-01-07 12:12:31 +0800442 switch (mStatus) {
443 case ScriptStatus::Compiled:
444 mCompiled->getFunctionBinary(function, base, length);
445 return;
446
447 case ScriptStatus::Cached:
448 mCached->getFunctionBinary(function, base, length);
449 return;
450
451 default:
452 *base = NULL;
453 *length = 0;
Logancf3e5212010-12-29 01:44:55 +0800454 return;
455 }
456
Logancf3e5212010-12-29 01:44:55 +0800457}
458
459
460void Script::registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
461 mpExtSymbolLookupFn = pFn;
462 mpExtSymbolLookupFnContext = pContext;
463
Logan7d2219f2011-01-06 06:19:25 +0800464 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800465 mErrorCode = BCC_INVALID_OPERATION;
Logan7d2219f2011-01-06 06:19:25 +0800466 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800467 }
Logancf3e5212010-12-29 01:44:55 +0800468}
469
470} // namespace bcc