blob: 588906a134fb11e7d04ee44fa58e065d594b7dd7 [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
207 reader.addDependency(BCC_FILE_RESOURCE, pathLibBCC, sha1LibBCC);
208 reader.addDependency(BCC_FILE_RESOURCE, pathLibRS, sha1LibRS);
209
210 if (sourceBC) {
Logan9a5f8682011-01-07 06:09:57 +0800211 reader.addDependency(BCC_APK_RESOURCE, sourceResName, sourceSHA1);
212 }
213
214 // Read cache file
Logane7eb7732011-01-07 07:11:56 +0800215 ScriptCached *cached = reader.readCacheFile(&file, this);
Logan04329712011-01-06 06:10:57 +0800216 if (!cached) {
217 return 1;
218 }
219
220 mCached = cached;
221 mStatus = ScriptStatus::Cached;
Logan033f46e2011-01-06 05:51:24 +0800222
Loganf3c83ce2011-01-07 06:36:33 +0800223 // Dirty hack for libRS.
224 // TODO(all): This dirty hack should be removed in the future.
225 if (cached->isLibRSThreadable() && mpExtSymbolLookupFn) {
226 mpExtSymbolLookupFn(mpExtSymbolLookupFnContext, "__clearThreadable");
227 }
228
Loganf7f0ac52011-01-07 03:53:43 +0800229 return 0;
Logan033f46e2011-01-06 05:51:24 +0800230}
231
232
233int Script::internalCompile() {
234 // Create the ScriptCompiled object
Loganecf4cbd2011-01-06 05:34:11 +0800235 mCompiled = new (nothrow) ScriptCompiled(this);
236
237 if (!mCompiled) {
238 mErrorCode = BCC_OUT_OF_MEMORY;
239 LOGE("Out of memory: %s %d\n", __FILE__, __LINE__);
240 return 1;
241 }
242
243 mStatus = ScriptStatus::Compiled;
244
Logan033f46e2011-01-06 05:51:24 +0800245 // Register symbol lookup function
Loganecf4cbd2011-01-06 05:34:11 +0800246 if (mpExtSymbolLookupFn) {
247 mCompiled->registerSymbolCallback(mpExtSymbolLookupFn,
248 mpExtSymbolLookupFnContext);
249 }
250
Logan033f46e2011-01-06 05:51:24 +0800251 // Setup the source bitcode / module
Loganecf4cbd2011-01-06 05:34:11 +0800252 if (sourceBC) {
Loganf30a6712011-01-06 05:55:34 +0800253 if (mCompiled->readBC(sourceBC, sourceSize, sourceResName, 0) != 0) {
Logan65719812011-01-07 11:17:14 +0800254 LOGE("Unable to readBC, bitcode=%p, size=%lu\n",
255 sourceBC, (unsigned long)sourceSize);
Logan033f46e2011-01-06 05:51:24 +0800256 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800257 }
Logan65719812011-01-07 11:17:14 +0800258
Shih-wei Liaoefbd0ed2011-01-07 06:36:25 -0800259 LOGI("Load sourceBC\n");
Loganecf4cbd2011-01-06 05:34:11 +0800260 } else if (sourceModule) {
Logan033f46e2011-01-06 05:51:24 +0800261 if (mCompiled->readModule(sourceModule) != 0) {
262 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800263 }
Logan65719812011-01-07 11:17:14 +0800264
Shih-wei Liaoefbd0ed2011-01-07 06:36:25 -0800265 LOGI("Load sourceModule\n");
Loganecf4cbd2011-01-06 05:34:11 +0800266 }
267
Logan3133c412011-01-06 06:15:40 +0800268 // Link the source module with the library module
Logan04329712011-01-06 06:10:57 +0800269 if (libraryBC) {
270 if (mCompiled->linkBC(libraryBC, librarySize) != 0) {
271 return 1;
272 }
Logan65719812011-01-07 11:17:14 +0800273
274 LOGE("Load Library\n");
Logan04329712011-01-06 06:10:57 +0800275 }
Loganecf4cbd2011-01-06 05:34:11 +0800276
Logan3133c412011-01-06 06:15:40 +0800277 // Compile and JIT the code
Logan04329712011-01-06 06:10:57 +0800278 if (mCompiled->compile() != 0) {
Logan65719812011-01-07 11:17:14 +0800279 LOGE("Unable to compile.\n");
Logan04329712011-01-06 06:10:57 +0800280 return 1;
281 }
282
283 // TODO(logan): Write the cache out
Logan04329712011-01-06 06:10:57 +0800284 if (cacheFile && !getBooleanProp("debug.bcc.nocache")) {
Logan89eb47f2011-01-07 10:45:16 +0800285 FileHandle file;
Logan04329712011-01-06 06:10:57 +0800286
Logan77124df2011-01-06 06:22:17 +0800287 if (file.open(cacheFile, OpenMode::Write) >= 0) {
Logan04329712011-01-06 06:10:57 +0800288 CacheWriter writer;
Logana27a83f2011-01-07 10:25:48 +0800289
Logana2e15af2011-01-07 11:46:08 +0800290 // Dependencies
291 writer.addDependency(BCC_FILE_RESOURCE, pathLibBCC, sha1LibBCC);
292 writer.addDependency(BCC_FILE_RESOURCE, pathLibRS, sha1LibRS);
293
294 if (sourceBC) {
295 writer.addDependency(BCC_APK_RESOURCE, sourceResName, sourceSHA1);
296 }
297
Logana27a83f2011-01-07 10:25:48 +0800298 // libRS is threadable dirty hack
299 // TODO: This should be removed in the future
300 uint32_t libRS_threadable = 0;
301 if (mpExtSymbolLookupFn) {
Logan89eb47f2011-01-07 10:45:16 +0800302 libRS_threadable =
303 (uint32_t)mpExtSymbolLookupFn(mpExtSymbolLookupFnContext,
304 "__isThreadable");
Logana27a83f2011-01-07 10:25:48 +0800305 }
306
Logan89eb47f2011-01-07 10:45:16 +0800307 if (!writer.writeCacheFile(&file, this, libRS_threadable)) {
308 file.truncate();
309 file.close();
Logana27a83f2011-01-07 10:25:48 +0800310
Logan89eb47f2011-01-07 10:45:16 +0800311 if (unlink(cacheFile) != 0) {
312 LOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
313 cacheFile, strerror(errno));
314 }
315 }
316 }
Logan04329712011-01-06 06:10:57 +0800317 }
Logan04329712011-01-06 06:10:57 +0800318
319 return 0;
Logancf3e5212010-12-29 01:44:55 +0800320}
321
322
323char const *Script::getCompilerErrorMessage() {
324 if (mStatus != ScriptStatus::Compiled) {
325 mErrorCode = BCC_INVALID_OPERATION;
326 return NULL;
327 }
328
329 return mCompiled->getCompilerErrorMessage();
330}
331
332
333void *Script::lookup(const char *name) {
Logan89eb47f2011-01-07 10:45:16 +0800334 switch (mStatus) {
335 case ScriptStatus::Compiled:
336 return mCompiled->lookup(name);
337
338 case ScriptStatus::Cached:
339 return mCached->lookup(name);
340
341 default:
Logancf3e5212010-12-29 01:44:55 +0800342 mErrorCode = BCC_INVALID_OPERATION;
343 return NULL;
344 }
Logancf3e5212010-12-29 01:44:55 +0800345}
346
347
348void Script::getExportVars(BCCsizei *actualVarCount,
349 BCCsizei maxVarCount,
350 BCCvoid **vars) {
Logan89eb47f2011-01-07 10:45:16 +0800351 switch (mStatus) {
352 case ScriptStatus::Compiled:
353 mCompiled->getExportVars(actualVarCount, maxVarCount, vars);
354 break;
Logancf3e5212010-12-29 01:44:55 +0800355
Logan89eb47f2011-01-07 10:45:16 +0800356 case ScriptStatus::Cached:
357 mCached->getExportVars(actualVarCount, maxVarCount, vars);
358 break;
359
360 default:
361 mErrorCode = BCC_INVALID_OPERATION;
362 }
Logancf3e5212010-12-29 01:44:55 +0800363}
364
365
366void Script::getExportFuncs(BCCsizei *actualFuncCount,
367 BCCsizei maxFuncCount,
368 BCCvoid **funcs) {
Logan89eb47f2011-01-07 10:45:16 +0800369 switch (mStatus) {
370 case ScriptStatus::Compiled:
371 mCompiled->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
372 break;
Logancf3e5212010-12-29 01:44:55 +0800373
Logan89eb47f2011-01-07 10:45:16 +0800374 case ScriptStatus::Cached:
375 mCached->getExportFuncs(actualFuncCount, maxFuncCount, funcs);
376 break;
377
378 default:
379 mErrorCode = BCC_INVALID_OPERATION;
380 }
Logancf3e5212010-12-29 01:44:55 +0800381}
382
383
384void Script::getPragmas(BCCsizei *actualStringCount,
385 BCCsizei maxStringCount,
386 BCCchar **strings) {
Logan89eb47f2011-01-07 10:45:16 +0800387 switch (mStatus) {
388 case ScriptStatus::Compiled:
389 mCompiled->getPragmas(actualStringCount, maxStringCount, strings);
390 break;
Logancf3e5212010-12-29 01:44:55 +0800391
Logan89eb47f2011-01-07 10:45:16 +0800392 case ScriptStatus::Cached:
393 mCached->getPragmas(actualStringCount, maxStringCount, strings);
394 break;
395
396 default:
397 mErrorCode = BCC_INVALID_OPERATION;
398 }
Logancf3e5212010-12-29 01:44:55 +0800399}
400
401
402void Script::getFunctions(BCCsizei *actualFunctionCount,
403 BCCsizei maxFunctionCount,
404 BCCchar **functions) {
Logan89eb47f2011-01-07 10:45:16 +0800405 switch (mStatus) {
406 case ScriptStatus::Compiled:
407 mCompiled->getFunctions(actualFunctionCount, maxFunctionCount, functions);
408 break;
Logancf3e5212010-12-29 01:44:55 +0800409
Logan89eb47f2011-01-07 10:45:16 +0800410 case ScriptStatus::Cached:
411 mCached->getFunctions(actualFunctionCount, maxFunctionCount, functions);
412 break;
413
414 default:
415 mErrorCode = BCC_INVALID_OPERATION;
416 }
Logancf3e5212010-12-29 01:44:55 +0800417}
418
Logana27a83f2011-01-07 10:25:48 +0800419char *Script::getContext() {
420 switch (mStatus) {
421 case ScriptStatus::Compiled:
422 return mCompiled->getContext();
423
424 case ScriptStatus::Cached:
425 return mCached->getContext();
426
427 default:
428 mErrorCode = BCC_INVALID_OPERATION;
Logan02286cb2011-01-07 00:30:47 +0800429 return NULL;
430 }
Logan02286cb2011-01-07 00:30:47 +0800431}
432
Logancf3e5212010-12-29 01:44:55 +0800433
434void Script::getFunctionBinary(BCCchar *function,
435 BCCvoid **base,
436 BCCsizei *length) {
Logan216ec712011-01-07 12:12:31 +0800437 switch (mStatus) {
438 case ScriptStatus::Compiled:
439 mCompiled->getFunctionBinary(function, base, length);
440 return;
441
442 case ScriptStatus::Cached:
443 mCached->getFunctionBinary(function, base, length);
444 return;
445
446 default:
447 *base = NULL;
448 *length = 0;
Logancf3e5212010-12-29 01:44:55 +0800449 return;
450 }
451
Logancf3e5212010-12-29 01:44:55 +0800452}
453
454
455void Script::registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) {
456 mpExtSymbolLookupFn = pFn;
457 mpExtSymbolLookupFnContext = pContext;
458
Logan7d2219f2011-01-06 06:19:25 +0800459 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800460 mErrorCode = BCC_INVALID_OPERATION;
Logan7d2219f2011-01-06 06:19:25 +0800461 LOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800462 }
Logancf3e5212010-12-29 01:44:55 +0800463}
464
465} // namespace bcc