blob: 74f1f01f47021295c19c1b3149f316a88bf567c5 [file] [log] [blame]
Logancf3e5212010-12-29 01:44:55 +08001/*
Stephen Hinescc366e52012-02-21 17:22:04 -08002 * copyright 2010-2012, the android open source project
Logancf3e5212010-12-29 01:44:55 +08003 *
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
Logancf3e5212010-12-29 01:44:55 +080017#include "Script.h"
18
Logan35849002011-01-15 07:30:43 +080019#include "Config.h"
20
Logan Chiend2a5f302011-07-19 20:32:25 +080021#if USE_OLD_JIT
22#include "OldJIT/CacheReader.h"
23#include "OldJIT/CacheWriter.h"
24#endif
25
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070026#include "MCCacheReader.h"
27#include "MCCacheWriter.h"
Zonr Chang2fcbd022012-01-06 21:04:31 +080028#include "CompilerOption.h"
Logan Chiend2a5f302011-07-19 20:32:25 +080029
30#if USE_OLD_JIT
31#include "OldJIT/ContextManager.h"
32#endif
33
Logan4dcd6792011-02-28 05:12:00 +080034#include "DebugHelper.h"
Logan04329712011-01-06 06:10:57 +080035#include "FileHandle.h"
Daniel Malea094881f2011-12-14 17:39:16 -050036#include "GDBJITRegistrar.h"
Logancf3e5212010-12-29 01:44:55 +080037#include "ScriptCompiled.h"
Logan9a5f8682011-01-07 06:09:57 +080038#include "ScriptCached.h"
39#include "Sha1Helper.h"
Logan474cbd22011-01-31 01:47:44 +080040#include "SourceInfo.h"
Logancf3e5212010-12-29 01:44:55 +080041
Logan89eb47f2011-01-07 10:45:16 +080042#include <errno.h>
Logan474cbd22011-01-31 01:47:44 +080043#include <sys/stat.h>
44#include <sys/types.h>
Bhanu Chetlapallieb4509b2012-01-08 20:42:56 -080045#include <unistd.h>
Logancf3e5212010-12-29 01:44:55 +080046
Logan89eb47f2011-01-07 10:45:16 +080047#include <new>
48#include <string.h>
Logan033f46e2011-01-06 05:51:24 +080049#include <cutils/properties.h>
50
Loganecf4cbd2011-01-06 05:34:11 +080051namespace {
52
Logan033f46e2011-01-06 05:51:24 +080053bool getBooleanProp(const char *str) {
Loganf340bf72011-01-14 17:51:40 +080054 char buf[PROPERTY_VALUE_MAX];
55 property_get(str, buf, "0");
56 return strcmp(buf, "0") != 0;
Logan033f46e2011-01-06 05:51:24 +080057}
58
Loganecf4cbd2011-01-06 05:34:11 +080059} // namespace anonymous
60
Logancf3e5212010-12-29 01:44:55 +080061namespace bcc {
62
63Script::~Script() {
Logan35849002011-01-15 07:30:43 +080064 switch (mStatus) {
65 case ScriptStatus::Compiled:
Logancf3e5212010-12-29 01:44:55 +080066 delete mCompiled;
Logan35849002011-01-15 07:30:43 +080067 break;
68
69#if USE_CACHE
70 case ScriptStatus::Cached:
Shih-wei Liaoc4cf6542011-01-13 01:43:01 -080071 delete mCached;
Logan35849002011-01-15 07:30:43 +080072 break;
73#endif
74
75 default:
76 break;
Logancf3e5212010-12-29 01:44:55 +080077 }
Logan474cbd22011-01-31 01:47:44 +080078
Shih-wei Liao6a60f4e2012-01-17 02:58:40 -080079 for (size_t i = 0; i < 2; ++i) {
Logan474cbd22011-01-31 01:47:44 +080080 delete mSourceList[i];
Shih-wei Liao6a60f4e2012-01-17 02:58:40 -080081 }
Logancf3e5212010-12-29 01:44:55 +080082}
83
84
Logan474cbd22011-01-31 01:47:44 +080085int Script::addSourceBC(size_t idx,
86 char const *resName,
87 const char *bitcode,
88 size_t bitcodeSize,
89 unsigned long flags) {
Shih-wei Liao898c5a92011-05-18 07:02:39 -070090
91 if (!resName) {
92 mErrorCode = BCC_INVALID_VALUE;
Steve Block10c14122012-01-08 10:15:06 +000093 ALOGE("Invalid argument: resName = NULL\n");
Shih-wei Liao898c5a92011-05-18 07:02:39 -070094 return 1;
95 }
96
Loganecf4cbd2011-01-06 05:34:11 +080097 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +080098 mErrorCode = BCC_INVALID_OPERATION;
Steve Block10c14122012-01-08 10:15:06 +000099 ALOGE("Bad operation: Adding source after bccPrepareExecutable\n");
Logancf3e5212010-12-29 01:44:55 +0800100 return 1;
101 }
102
Logan474cbd22011-01-31 01:47:44 +0800103 if (!bitcode) {
104 mErrorCode = BCC_INVALID_VALUE;
Steve Block10c14122012-01-08 10:15:06 +0000105 ALOGE("Invalid argument: bitcode = NULL\n");
Logan474cbd22011-01-31 01:47:44 +0800106 return 1;
107 }
108
109 mSourceList[idx] = SourceInfo::createFromBuffer(resName,
110 bitcode, bitcodeSize,
111 flags);
112
113 if (!mSourceList[idx]) {
114 mErrorCode = BCC_OUT_OF_MEMORY;
Steve Block10c14122012-01-08 10:15:06 +0000115 ALOGE("Out of memory while adding source bitcode\n");
Logan474cbd22011-01-31 01:47:44 +0800116 return 1;
117 }
118
Loganecf4cbd2011-01-06 05:34:11 +0800119 return 0;
Logancf3e5212010-12-29 01:44:55 +0800120}
121
122
Logan474cbd22011-01-31 01:47:44 +0800123int Script::addSourceModule(size_t idx,
124 llvm::Module *module,
125 unsigned long flags) {
Logancf3e5212010-12-29 01:44:55 +0800126 if (mStatus != ScriptStatus::Unknown) {
127 mErrorCode = BCC_INVALID_OPERATION;
Steve Block10c14122012-01-08 10:15:06 +0000128 ALOGE("Bad operation: Adding source after bccPrepareExecutable\n");
Logancf3e5212010-12-29 01:44:55 +0800129 return 1;
130 }
131
Logan474cbd22011-01-31 01:47:44 +0800132 if (!module) {
133 mErrorCode = BCC_INVALID_VALUE;
Steve Block10c14122012-01-08 10:15:06 +0000134 ALOGE("Invalid argument: module = NULL\n");
Logan474cbd22011-01-31 01:47:44 +0800135 return 1;
136 }
137
138 mSourceList[idx] = SourceInfo::createFromModule(module, flags);
139
140 if (!mSourceList[idx]) {
141 mErrorCode = BCC_OUT_OF_MEMORY;
Steve Block10c14122012-01-08 10:15:06 +0000142 ALOGE("Out of memory when add source module\n");
Logan474cbd22011-01-31 01:47:44 +0800143 return 1;
144 }
145
Loganecf4cbd2011-01-06 05:34:11 +0800146 return 0;
Logancf3e5212010-12-29 01:44:55 +0800147}
148
149
Logan474cbd22011-01-31 01:47:44 +0800150int Script::addSourceFile(size_t idx,
151 char const *path,
152 unsigned long flags) {
Logan3133c412011-01-06 06:15:40 +0800153 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800154 mErrorCode = BCC_INVALID_OPERATION;
Steve Block10c14122012-01-08 10:15:06 +0000155 ALOGE("Bad operation: Adding source after bccPrepareExecutable\n");
Logancf3e5212010-12-29 01:44:55 +0800156 return 1;
157 }
158
Logan474cbd22011-01-31 01:47:44 +0800159 if (!path) {
160 mErrorCode = BCC_INVALID_VALUE;
Steve Block10c14122012-01-08 10:15:06 +0000161 ALOGE("Invalid argument: path = NULL\n");
Logan474cbd22011-01-31 01:47:44 +0800162 return 1;
163 }
164
165 struct stat sb;
166 if (stat(path, &sb) != 0) {
167 mErrorCode = BCC_INVALID_VALUE;
Steve Block10c14122012-01-08 10:15:06 +0000168 ALOGE("File not found: %s\n", path);
Logan474cbd22011-01-31 01:47:44 +0800169 return 1;
170 }
171
172 mSourceList[idx] = SourceInfo::createFromFile(path, flags);
173
174 if (!mSourceList[idx]) {
175 mErrorCode = BCC_OUT_OF_MEMORY;
Steve Block10c14122012-01-08 10:15:06 +0000176 ALOGE("Out of memory while adding source file\n");
Logan474cbd22011-01-31 01:47:44 +0800177 return 1;
178 }
179
Logan3133c412011-01-06 06:15:40 +0800180 return 0;
Logancf3e5212010-12-29 01:44:55 +0800181}
182
Shih-wei Liaod8ed6a92012-03-03 01:44:29 -0800183int Script::prepareRelocatable(char const *objPath,
Shih-wei Liao6a60f4e2012-01-17 02:58:40 -0800184 llvm::Reloc::Model RelocModel,
185 unsigned long flags) {
Zonr Chang2fcbd022012-01-06 21:04:31 +0800186 CompilerOption option;
Shih-wei Liao8afed382012-01-10 15:57:24 +0800187 option.RelocModelOpt = RelocModel;
Zonr Chang2fcbd022012-01-06 21:04:31 +0800188 option.LoadAfterCompile = false;
189 int status = internalCompile(option);
Joseph Wen34c600a2011-07-25 17:59:17 -0700190 if (status != 0) {
Steve Block10c14122012-01-08 10:15:06 +0000191 ALOGE("LLVM error message: %s\n", getCompilerErrorMessage());
Shih-wei Liaoa0ed34e2012-03-03 01:33:30 -0800192 return status;
193 }
Shih-wei Liaod8ed6a92012-03-03 01:44:29 -0800194
195 FileHandle objFile;
196 if (objFile.open(objPath, OpenMode::Write) < 0) {
197 ALOGE("Failed to open %s for write.\n", objPath);
198 return 1;
Joseph Wen34c600a2011-07-25 17:59:17 -0700199 }
Shih-wei Liaod8ed6a92012-03-03 01:44:29 -0800200
201 if (static_cast<size_t>(objFile.write(getELF(),
202 getELFSize())) != getELFSize()) {
203 objFile.close();
204 ::unlink(objPath);
205 ALOGE("Unable to write ELF to file %s.\n", objPath);
206 return false;
207 }
208
209 return 0;
Joseph Wen34c600a2011-07-25 17:59:17 -0700210}
211
Logancf3e5212010-12-29 01:44:55 +0800212
Shih-wei Liaoa471ebb2012-02-05 00:49:58 -0800213int Script::prepareSharedObject(char const *cacheDir,
214 char const *cacheName,
215 char const *objPath,
216 char const *dsoPath,
217 unsigned long flags) {
218 // TODO: Support cached shared object.
219 return 1;
220}
221
222
Logan Chien311c26f2011-07-11 14:30:34 +0800223int Script::prepareExecutable(char const *cacheDir,
224 char const *cacheName,
225 unsigned long flags) {
Loganecf4cbd2011-01-06 05:34:11 +0800226 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800227 mErrorCode = BCC_INVALID_OPERATION;
Steve Block10c14122012-01-08 10:15:06 +0000228 ALOGE("Invalid operation: %s\n", __func__);
Logancf3e5212010-12-29 01:44:55 +0800229 return 1;
230 }
231
Daniel Malea094881f2011-12-14 17:39:16 -0500232 int status = -1;
Logan35849002011-01-15 07:30:43 +0800233#if USE_CACHE
Zonr Chang743dd712012-01-19 10:13:52 +0800234 if (internalLoadCache(cacheDir, cacheName,
Shih-wei Liao32ef88b2012-02-04 23:33:11 -0800235 ScriptObject::Executable, /* checkOnly */ false) == 0) {
Stephen Hinese0918ac2012-03-01 23:28:09 -0800236 status = 0;
Shih-wei Liao32ef88b2012-02-04 23:33:11 -0800237 }
Logan35849002011-01-15 07:30:43 +0800238#endif
Loganecf4cbd2011-01-06 05:34:11 +0800239
Stephen Hinese0918ac2012-03-01 23:28:09 -0800240 if (status != 0) {
241 CompilerOption option;
242 status = internalCompile(option);
Stephen Hinese0918ac2012-03-01 23:28:09 -0800243
Shih-wei Liaoa0ed34e2012-03-03 01:33:30 -0800244 if (status != 0) {
245 ALOGE("LLVM error message: %s\n", getCompilerErrorMessage());
246 return status;
247 }
248
249 status = writeCache();
250 if (status != 0) {
251 ALOGE("Failed to write the cache for %s\n", cacheName);
252 return status;
253 }
Stephen Hines27b35102011-05-11 17:58:48 -0700254 }
Daniel Malea094881f2011-12-14 17:39:16 -0500255
256 // FIXME: Registration can be conditional on the presence of debug metadata
Shih-wei Liaoa0ed34e2012-03-03 01:33:30 -0800257 registerObjectWithGDB(getELF(), getELFSize()); // thread-safe registration
258
Stephen Hines27b35102011-05-11 17:58:48 -0700259 return status;
Logan033f46e2011-01-06 05:51:24 +0800260}
261
Logan35849002011-01-15 07:30:43 +0800262#if USE_CACHE
Zonr Chang743dd712012-01-19 10:13:52 +0800263int Script::internalLoadCache(char const *cacheDir, char const *cacheName,
264 ScriptObject::ObjectType objectType,
265 bool checkOnly) {
266 mObjectType = objectType;
267
Shih-wei Liao32ef88b2012-02-04 23:33:11 -0800268 if ((cacheDir == NULL) || (cacheName == NULL)) {
Zonr Chang743dd712012-01-19 10:13:52 +0800269 return 1;
Shih-wei Liao32ef88b2012-02-04 23:33:11 -0800270 }
Zonr Chang743dd712012-01-19 10:13:52 +0800271
272 // Set cache file Name
273 mCacheName = cacheName;
274
Shih-wei Liao32ef88b2012-02-04 23:33:11 -0800275 // Santize mCacheDir. Ensure that mCacheDir ends with '/'.
Zonr Chang743dd712012-01-19 10:13:52 +0800276 mCacheDir = cacheDir;
Shih-wei Liao32ef88b2012-02-04 23:33:11 -0800277 if (!mCacheDir.empty() && *mCacheDir.rbegin() != '/') {
Zonr Chang743dd712012-01-19 10:13:52 +0800278 mCacheDir.push_back('/');
Shih-wei Liao32ef88b2012-02-04 23:33:11 -0800279 }
Zonr Chang743dd712012-01-19 10:13:52 +0800280
Shih-wei Liao32ef88b2012-02-04 23:33:11 -0800281 if (!isCacheable()) {
Logan033f46e2011-01-06 05:51:24 +0800282 return 1;
Shih-wei Liao32ef88b2012-02-04 23:33:11 -0800283 }
Logan04329712011-01-06 06:10:57 +0800284
Zonr Chang4ea08862012-01-17 17:26:49 +0800285 std::string objPath = getCachedObjectPath();
286 std::string infoPath = getCacheInfoPath();
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700287
Logan Chien311c26f2011-07-11 14:30:34 +0800288 FileHandle objFile;
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700289 if (objFile.open(objPath.c_str(), OpenMode::Read) < 0) {
Logan Chien03a2e302011-07-13 21:46:32 +0800290 // Unable to open the executable file in read mode.
Logan04329712011-01-06 06:10:57 +0800291 return 1;
292 }
293
Logan Chien311c26f2011-07-11 14:30:34 +0800294 FileHandle infoFile;
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700295 if (infoFile.open(infoPath.c_str(), OpenMode::Read) < 0) {
Logan Chien03a2e302011-07-13 21:46:32 +0800296 // Unable to open the metadata information file in read mode.
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700297 return 1;
298 }
299
Logan Chien311c26f2011-07-11 14:30:34 +0800300#if USE_OLD_JIT
301 CacheReader reader;
302#elif USE_MCJIT
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700303 MCCacheReader reader;
304
305 // Register symbol lookup function
306 if (mpExtSymbolLookupFn) {
307 reader.registerSymbolCallback(mpExtSymbolLookupFn,
308 mpExtSymbolLookupFnContext);
309 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700310#endif
Logan04329712011-01-06 06:10:57 +0800311
Logan9a5f8682011-01-07 06:09:57 +0800312 // Dependencies
Joseph Wen2ca6e572011-06-24 14:12:23 -0700313 reader.addDependency(BCC_FILE_RESOURCE, pathLibBCC_SHA1, sha1LibBCC_SHA1);
Joseph Wen76919072011-07-07 23:06:15 -0700314 reader.addDependency(BCC_FILE_RESOURCE, pathLibRS, sha1LibRS);
Logan9a5f8682011-01-07 06:09:57 +0800315
Logan474cbd22011-01-31 01:47:44 +0800316 for (size_t i = 0; i < 2; ++i) {
317 if (mSourceList[i]) {
318 mSourceList[i]->introDependency(reader);
319 }
Logan9a5f8682011-01-07 06:09:57 +0800320 }
321
Joseph Wen34c600a2011-07-25 17:59:17 -0700322 if (checkOnly)
Joseph Wen49281042011-07-26 10:04:09 -0700323 return !reader.checkCacheFile(&objFile, &infoFile, this);
Joseph Wen34c600a2011-07-25 17:59:17 -0700324
Logan9a5f8682011-01-07 06:09:57 +0800325 // Read cache file
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700326 ScriptCached *cached = reader.readCacheFile(&objFile, &infoFile, this);
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700327
Logan04329712011-01-06 06:10:57 +0800328 if (!cached) {
Logan42598052011-01-26 22:41:13 +0800329 mIsContextSlotNotAvail = reader.isContextSlotNotAvail();
Logan04329712011-01-06 06:10:57 +0800330 return 1;
331 }
332
333 mCached = cached;
334 mStatus = ScriptStatus::Cached;
Logan033f46e2011-01-06 05:51:24 +0800335
Loganf3c83ce2011-01-07 06:36:33 +0800336 // Dirty hack for libRS.
337 // TODO(all): This dirty hack should be removed in the future.
Shih-wei Liao8eb5fe92011-02-01 04:17:38 -0800338 if (!cached->isLibRSThreadable() && mpExtSymbolLookupFn) {
Loganf3c83ce2011-01-07 06:36:33 +0800339 mpExtSymbolLookupFn(mpExtSymbolLookupFnContext, "__clearThreadable");
340 }
341
Loganf7f0ac52011-01-07 03:53:43 +0800342 return 0;
Logan033f46e2011-01-06 05:51:24 +0800343}
Logan35849002011-01-15 07:30:43 +0800344#endif
Logan033f46e2011-01-06 05:51:24 +0800345
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800346int Script::internalCompile(const CompilerOption &option) {
Logan033f46e2011-01-06 05:51:24 +0800347 // Create the ScriptCompiled object
Nowar Gu09b6c1c2011-05-24 23:49:07 +0800348 mCompiled = new (std::nothrow) ScriptCompiled(this);
Loganecf4cbd2011-01-06 05:34:11 +0800349
350 if (!mCompiled) {
351 mErrorCode = BCC_OUT_OF_MEMORY;
Steve Block10c14122012-01-08 10:15:06 +0000352 ALOGE("Out of memory: %s %d\n", __FILE__, __LINE__);
Loganecf4cbd2011-01-06 05:34:11 +0800353 return 1;
354 }
355
356 mStatus = ScriptStatus::Compiled;
357
Logan033f46e2011-01-06 05:51:24 +0800358 // Register symbol lookup function
Loganecf4cbd2011-01-06 05:34:11 +0800359 if (mpExtSymbolLookupFn) {
360 mCompiled->registerSymbolCallback(mpExtSymbolLookupFn,
361 mpExtSymbolLookupFnContext);
362 }
363
Zonr Changdf3fee42012-01-10 15:58:36 +0800364 if (!mSourceList[0]) {
365 ALOGE("Source bitcode is not set.\n");
Logan474cbd22011-01-31 01:47:44 +0800366 return 1;
367 }
368
Shih-wei Liaod88c0d12012-01-17 20:32:58 -0800369 // Parse Source bitcode file (if necessary)
370 if (mSourceList[0]->prepareModule() != 0) {
371 ALOGE("Unable to setup source module\n");
Zonr Changdf3fee42012-01-10 15:58:36 +0800372 return 1;
Shih-wei Liaod88c0d12012-01-17 20:32:58 -0800373 }
374
375 // Parse Library bitcode file (if necessary)
376 if (mSourceList[1]) {
377 if (mSourceList[1]->prepareModule(mSourceList[0]->getContext()) != 0) {
378 ALOGE("Unable to setup library module\n");
379 return 1;
380 }
381 }
Zonr Changdf3fee42012-01-10 15:58:36 +0800382
383 // Set the main source module
384 if (mCompiled->readModule(mSourceList[0]->getModule()) != 0) {
Steve Block10c14122012-01-08 10:15:06 +0000385 ALOGE("Unable to read source module\n");
Logan474cbd22011-01-31 01:47:44 +0800386 return 1;
Loganecf4cbd2011-01-06 05:34:11 +0800387 }
388
Logan3133c412011-01-06 06:15:40 +0800389 // Link the source module with the library module
Shih-wei Liaod88c0d12012-01-17 20:32:58 -0800390 if (mSourceList[1]) {
Zonr Changdf3fee42012-01-10 15:58:36 +0800391 if (mCompiled->linkModule(mSourceList[1]->getModule()) != 0) {
Steve Block10c14122012-01-08 10:15:06 +0000392 ALOGE("Unable to link library module\n");
Logan04329712011-01-06 06:10:57 +0800393 return 1;
394 }
395 }
Loganecf4cbd2011-01-06 05:34:11 +0800396
Logan3133c412011-01-06 06:15:40 +0800397 // Compile and JIT the code
Zonr Chang2fcbd022012-01-06 21:04:31 +0800398 if (mCompiled->compile(option) != 0) {
Steve Block10c14122012-01-08 10:15:06 +0000399 ALOGE("Unable to compile.\n");
Logan04329712011-01-06 06:10:57 +0800400 return 1;
401 }
402
Shih-wei Liaoa0ed34e2012-03-03 01:33:30 -0800403 return 0;
404}
405
406int Script::writeCache() {
407 // Not compiled script or encouter error during the compilation.
408 if ((mStatus != ScriptStatus::Compiled) ||
409 (getCompilerErrorMessage() == NULL))
410 return 1;
411
Logan35849002011-01-15 07:30:43 +0800412#if USE_CACHE
Logan42598052011-01-26 22:41:13 +0800413 // Note: If we re-compile the script because the cached context slot not
414 // available, then we don't have to write the cache.
415
416 // Note: If the address of the context is not in the context slot, then
417 // we don't have to cache it.
418
Shih-wei Liaoabc7f512012-01-18 00:34:07 -0800419 if (
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700420#if USE_OLD_JIT
Logan1dc63142011-02-25 17:14:51 +0800421 !mIsContextSlotNotAvail &&
422 ContextManager::get().isManagingContext(getContext()) &&
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700423#endif
Shih-wei Liaoabc7f512012-01-18 00:34:07 -0800424 isCacheable()) {
Logan42598052011-01-26 22:41:13 +0800425
Zonr Chang4ea08862012-01-17 17:26:49 +0800426 std::string objPath = getCachedObjectPath();
427 std::string infoPath = getCacheInfoPath();
Logan Chien311c26f2011-07-11 14:30:34 +0800428
Jeff Brown937a0bc2011-01-26 23:20:14 -0800429 // Remove the file if it already exists before writing the new file.
430 // The old file may still be mapped elsewhere in memory and we do not want
431 // to modify its contents. (The same script may be running concurrently in
432 // the same process or a different process!)
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700433 ::unlink(objPath.c_str());
Logan Chien311c26f2011-07-11 14:30:34 +0800434#if !USE_OLD_JIT && USE_MCJIT
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700435 ::unlink(infoPath.c_str());
436#endif
Jeff Brown937a0bc2011-01-26 23:20:14 -0800437
Logan Chien03a2e302011-07-13 21:46:32 +0800438 FileHandle objFile;
439 FileHandle infoFile;
440
441 if (objFile.open(objPath.c_str(), OpenMode::Write) >= 0 &&
442 infoFile.open(infoPath.c_str(), OpenMode::Write) >= 0) {
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700443
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700444#if USE_OLD_JIT
Logan04329712011-01-06 06:10:57 +0800445 CacheWriter writer;
Logan Chien311c26f2011-07-11 14:30:34 +0800446#elif USE_MCJIT
447 MCCacheWriter writer;
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700448#endif
Logana27a83f2011-01-07 10:25:48 +0800449
Joseph Wen2ca6e572011-06-24 14:12:23 -0700450#ifdef TARGET_BUILD
Logana2e15af2011-01-07 11:46:08 +0800451 // Dependencies
Joseph Wen2ca6e572011-06-24 14:12:23 -0700452 writer.addDependency(BCC_FILE_RESOURCE, pathLibBCC_SHA1, sha1LibBCC_SHA1);
Joseph Wen76919072011-07-07 23:06:15 -0700453 writer.addDependency(BCC_FILE_RESOURCE, pathLibRS, sha1LibRS);
Logane1323992011-01-12 04:47:13 +0800454#endif
Logana2e15af2011-01-07 11:46:08 +0800455
Logan474cbd22011-01-31 01:47:44 +0800456 for (size_t i = 0; i < 2; ++i) {
Logan825c3b22011-02-28 05:05:48 +0800457 if (mSourceList[i]) {
458 mSourceList[i]->introDependency(writer);
459 }
Logana2e15af2011-01-07 11:46:08 +0800460 }
461
Logana27a83f2011-01-07 10:25:48 +0800462 // libRS is threadable dirty hack
463 // TODO: This should be removed in the future
464 uint32_t libRS_threadable = 0;
465 if (mpExtSymbolLookupFn) {
Logan89eb47f2011-01-07 10:45:16 +0800466 libRS_threadable =
467 (uint32_t)mpExtSymbolLookupFn(mpExtSymbolLookupFnContext,
468 "__isThreadable");
Logana27a83f2011-01-07 10:25:48 +0800469 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700470
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700471 if (!writer.writeCacheFile(&objFile, &infoFile, this, libRS_threadable)) {
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700472 objFile.truncate();
473 objFile.close();
Logana27a83f2011-01-07 10:25:48 +0800474
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700475 if (unlink(objPath.c_str()) != 0) {
Steve Block10c14122012-01-08 10:15:06 +0000476 ALOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700477 objPath.c_str(), strerror(errno));
Logan89eb47f2011-01-07 10:45:16 +0800478 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700479
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700480 infoFile.truncate();
481 infoFile.close();
482
483 if (unlink(infoPath.c_str()) != 0) {
Steve Block10c14122012-01-08 10:15:06 +0000484 ALOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n",
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700485 infoPath.c_str(), strerror(errno));
486 }
Logan89eb47f2011-01-07 10:45:16 +0800487 }
488 }
Logan04329712011-01-06 06:10:57 +0800489 }
Logan35849002011-01-15 07:30:43 +0800490#endif // USE_CACHE
Logan04329712011-01-06 06:10:57 +0800491
492 return 0;
Logancf3e5212010-12-29 01:44:55 +0800493}
494
495
496char const *Script::getCompilerErrorMessage() {
497 if (mStatus != ScriptStatus::Compiled) {
498 mErrorCode = BCC_INVALID_OPERATION;
499 return NULL;
500 }
501
502 return mCompiled->getCompilerErrorMessage();
503}
504
505
506void *Script::lookup(const char *name) {
Logan89eb47f2011-01-07 10:45:16 +0800507 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700508 case ScriptStatus::Compiled: {
509 return mCompiled->lookup(name);
510 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700511
Logan35849002011-01-15 07:30:43 +0800512#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700513 case ScriptStatus::Cached: {
514 return mCached->lookup(name);
515 }
Logan35849002011-01-15 07:30:43 +0800516#endif
Logan89eb47f2011-01-07 10:45:16 +0800517
Shih-wei Liaod50be322011-07-01 22:53:31 -0700518 default: {
519 mErrorCode = BCC_INVALID_OPERATION;
520 return NULL;
521 }
Logancf3e5212010-12-29 01:44:55 +0800522 }
Logancf3e5212010-12-29 01:44:55 +0800523}
524
525
Loganbe79ada2011-01-13 01:33:45 +0800526size_t Script::getExportVarCount() const {
Logan89eb47f2011-01-07 10:45:16 +0800527 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700528 case ScriptStatus::Compiled: {
529 return mCompiled->getExportVarCount();
530 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700531
Logan35849002011-01-15 07:30:43 +0800532#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700533 case ScriptStatus::Cached: {
534 return mCached->getExportVarCount();
535 }
Logan35849002011-01-15 07:30:43 +0800536#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700537
Shih-wei Liaod50be322011-07-01 22:53:31 -0700538 default: {
539 return 0;
540 }
Loganbe79ada2011-01-13 01:33:45 +0800541 }
542}
543
544
545size_t Script::getExportFuncCount() const {
546 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700547 case ScriptStatus::Compiled: {
548 return mCompiled->getExportFuncCount();
549 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700550
Logan35849002011-01-15 07:30:43 +0800551#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700552 case ScriptStatus::Cached: {
553 return mCached->getExportFuncCount();
554 }
Logan35849002011-01-15 07:30:43 +0800555#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700556
Shih-wei Liaod50be322011-07-01 22:53:31 -0700557 default: {
558 return 0;
559 }
Loganbe79ada2011-01-13 01:33:45 +0800560 }
561}
562
563
Stephen Hinescc366e52012-02-21 17:22:04 -0800564size_t Script::getExportForEachCount() const {
565 switch (mStatus) {
566 case ScriptStatus::Compiled: {
567 return mCompiled->getExportForEachCount();
568 }
569
570#if USE_CACHE
571 case ScriptStatus::Cached: {
572 return mCached->getExportForEachCount();
573 }
574#endif
575
576 default: {
577 return 0;
578 }
579 }
580}
581
582
Loganbe79ada2011-01-13 01:33:45 +0800583size_t Script::getPragmaCount() const {
584 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700585 case ScriptStatus::Compiled: {
586 return mCompiled->getPragmaCount();
587 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700588
Logan35849002011-01-15 07:30:43 +0800589#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700590 case ScriptStatus::Cached: {
591 return mCached->getPragmaCount();
592 }
Logan35849002011-01-15 07:30:43 +0800593#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700594
Shih-wei Liaod50be322011-07-01 22:53:31 -0700595 default: {
596 return 0;
597 }
Loganbe79ada2011-01-13 01:33:45 +0800598 }
599}
600
601
602size_t Script::getFuncCount() const {
603 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700604 case ScriptStatus::Compiled: {
605 return mCompiled->getFuncCount();
606 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700607
Logan35849002011-01-15 07:30:43 +0800608#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700609 case ScriptStatus::Cached: {
610 return mCached->getFuncCount();
611 }
Logan35849002011-01-15 07:30:43 +0800612#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700613
Shih-wei Liaod50be322011-07-01 22:53:31 -0700614 default: {
615 return 0;
616 }
Loganbe79ada2011-01-13 01:33:45 +0800617 }
618}
619
620
Stephen Hines071288a2011-01-27 14:38:26 -0800621size_t Script::getObjectSlotCount() const {
622 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700623 case ScriptStatus::Compiled: {
624 return mCompiled->getObjectSlotCount();
625 }
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700626
Stephen Hines071288a2011-01-27 14:38:26 -0800627#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700628 case ScriptStatus::Cached: {
629 return mCached->getObjectSlotCount();
630 }
Stephen Hines071288a2011-01-27 14:38:26 -0800631#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700632
Shih-wei Liaod50be322011-07-01 22:53:31 -0700633 default: {
634 return 0;
635 }
Stephen Hines071288a2011-01-27 14:38:26 -0800636 }
637}
638
639
Loganbe79ada2011-01-13 01:33:45 +0800640void Script::getExportVarList(size_t varListSize, void **varList) {
641 switch (mStatus) {
642#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700643 case ScriptStatus::STATUS: \
644 m##STATUS->getExportVarList(varListSize, varList); \
645 break;
Logancf3e5212010-12-29 01:44:55 +0800646
Logan35849002011-01-15 07:30:43 +0800647#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700648 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800649#endif
650
Shih-wei Liaod50be322011-07-01 22:53:31 -0700651 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800652#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800653
Shih-wei Liaod50be322011-07-01 22:53:31 -0700654 default: {
655 mErrorCode = BCC_INVALID_OPERATION;
656 }
Logan89eb47f2011-01-07 10:45:16 +0800657 }
Logancf3e5212010-12-29 01:44:55 +0800658}
659
Joseph Wenf36637f2011-07-06 18:27:12 -0700660void Script::getExportVarNameList(std::vector<std::string> &varList) {
661 switch (mStatus) {
662 case ScriptStatus::Compiled: {
663 return mCompiled->getExportVarNameList(varList);
664 }
665
666 default: {
667 mErrorCode = BCC_INVALID_OPERATION;
668 }
669 }
670}
671
Logancf3e5212010-12-29 01:44:55 +0800672
Loganbe79ada2011-01-13 01:33:45 +0800673void Script::getExportFuncList(size_t funcListSize, void **funcList) {
Logan89eb47f2011-01-07 10:45:16 +0800674 switch (mStatus) {
Loganbe79ada2011-01-13 01:33:45 +0800675#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700676 case ScriptStatus::STATUS: \
677 m##STATUS->getExportFuncList(funcListSize, funcList); \
678 break;
Logancf3e5212010-12-29 01:44:55 +0800679
Logan35849002011-01-15 07:30:43 +0800680#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700681 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800682#endif
683
Shih-wei Liaod50be322011-07-01 22:53:31 -0700684 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800685#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800686
Shih-wei Liaod50be322011-07-01 22:53:31 -0700687 default: {
688 mErrorCode = BCC_INVALID_OPERATION;
689 }
Logan89eb47f2011-01-07 10:45:16 +0800690 }
Logancf3e5212010-12-29 01:44:55 +0800691}
692
Joseph Wenf36637f2011-07-06 18:27:12 -0700693void Script::getExportFuncNameList(std::vector<std::string> &funcList) {
694 switch (mStatus) {
695 case ScriptStatus::Compiled: {
696 return mCompiled->getExportFuncNameList(funcList);
697 }
698
699 default: {
700 mErrorCode = BCC_INVALID_OPERATION;
701 }
702 }
703}
704
Stephen Hinescc366e52012-02-21 17:22:04 -0800705void Script::getExportForEachList(size_t funcListSize, void **funcList) {
706 switch (mStatus) {
707#define DELEGATE(STATUS) \
708 case ScriptStatus::STATUS: \
709 m##STATUS->getExportForEachList(funcListSize, funcList); \
710 break;
711
712#if USE_CACHE
713 DELEGATE(Cached);
714#endif
715
716 DELEGATE(Compiled);
717#undef DELEGATE
718
719 default: {
720 mErrorCode = BCC_INVALID_OPERATION;
721 }
722 }
723}
724
725void Script::getExportForEachNameList(std::vector<std::string> &forEachList) {
726 switch (mStatus) {
727 case ScriptStatus::Compiled: {
728 return mCompiled->getExportForEachNameList(forEachList);
729 }
730
731 default: {
732 mErrorCode = BCC_INVALID_OPERATION;
733 }
734 }
735}
Logancf3e5212010-12-29 01:44:55 +0800736
Loganbe79ada2011-01-13 01:33:45 +0800737void Script::getPragmaList(size_t pragmaListSize,
738 char const **keyList,
739 char const **valueList) {
Logan89eb47f2011-01-07 10:45:16 +0800740 switch (mStatus) {
Loganbe79ada2011-01-13 01:33:45 +0800741#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700742 case ScriptStatus::STATUS: \
743 m##STATUS->getPragmaList(pragmaListSize, keyList, valueList); \
744 break;
Logancf3e5212010-12-29 01:44:55 +0800745
Logan35849002011-01-15 07:30:43 +0800746#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700747 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800748#endif
749
Shih-wei Liaod50be322011-07-01 22:53:31 -0700750 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800751#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800752
Shih-wei Liaod50be322011-07-01 22:53:31 -0700753 default: {
754 mErrorCode = BCC_INVALID_OPERATION;
755 }
Logan89eb47f2011-01-07 10:45:16 +0800756 }
Logancf3e5212010-12-29 01:44:55 +0800757}
758
759
Loganf340bf72011-01-14 17:51:40 +0800760void Script::getFuncInfoList(size_t funcInfoListSize,
761 FuncInfo *funcInfoList) {
Logan89eb47f2011-01-07 10:45:16 +0800762 switch (mStatus) {
Loganbe79ada2011-01-13 01:33:45 +0800763#define DELEGATE(STATUS) \
Shih-wei Liaod50be322011-07-01 22:53:31 -0700764 case ScriptStatus::STATUS: \
765 m##STATUS->getFuncInfoList(funcInfoListSize, funcInfoList); \
766 break;
Logancf3e5212010-12-29 01:44:55 +0800767
Logan35849002011-01-15 07:30:43 +0800768#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700769 DELEGATE(Cached);
Logan35849002011-01-15 07:30:43 +0800770#endif
771
Shih-wei Liaod50be322011-07-01 22:53:31 -0700772 DELEGATE(Compiled);
Loganbe79ada2011-01-13 01:33:45 +0800773#undef DELEGATE
Logan89eb47f2011-01-07 10:45:16 +0800774
Shih-wei Liaod50be322011-07-01 22:53:31 -0700775 default: {
776 mErrorCode = BCC_INVALID_OPERATION;
777 }
Logan89eb47f2011-01-07 10:45:16 +0800778 }
Logancf3e5212010-12-29 01:44:55 +0800779}
780
Stephen Hines071288a2011-01-27 14:38:26 -0800781
782void Script::getObjectSlotList(size_t objectSlotListSize,
783 uint32_t *objectSlotList) {
784 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700785#define DELEGATE(STATUS) \
786 case ScriptStatus::STATUS: \
787 m##STATUS->getObjectSlotList(objectSlotListSize, objectSlotList); \
788 break;
Stephen Hines071288a2011-01-27 14:38:26 -0800789
790#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700791 DELEGATE(Cached);
Stephen Hines071288a2011-01-27 14:38:26 -0800792#endif
793
Shih-wei Liaod50be322011-07-01 22:53:31 -0700794 DELEGATE(Compiled);
Stephen Hines071288a2011-01-27 14:38:26 -0800795#undef DELEGATE
796
Shih-wei Liaod50be322011-07-01 22:53:31 -0700797 default: {
798 mErrorCode = BCC_INVALID_OPERATION;
799 }
Stephen Hines071288a2011-01-27 14:38:26 -0800800 }
801}
802
803
Logan Chiend2a5f302011-07-19 20:32:25 +0800804#if USE_OLD_JIT
Logana27a83f2011-01-07 10:25:48 +0800805char *Script::getContext() {
806 switch (mStatus) {
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700807
Logan35849002011-01-15 07:30:43 +0800808#if USE_CACHE
Shih-wei Liaod50be322011-07-01 22:53:31 -0700809 case ScriptStatus::Cached: {
810 return mCached->getContext();
811 }
Logan35849002011-01-15 07:30:43 +0800812#endif
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700813
Shih-wei Liaod50be322011-07-01 22:53:31 -0700814 case ScriptStatus::Compiled: {
815 return mCompiled->getContext();
816 }
Logana27a83f2011-01-07 10:25:48 +0800817
Shih-wei Liaod50be322011-07-01 22:53:31 -0700818 default: {
819 mErrorCode = BCC_INVALID_OPERATION;
820 return NULL;
821 }
Logan02286cb2011-01-07 00:30:47 +0800822 }
Logan02286cb2011-01-07 00:30:47 +0800823}
Logan Chiend2a5f302011-07-19 20:32:25 +0800824#endif
Logan02286cb2011-01-07 00:30:47 +0800825
Logancf3e5212010-12-29 01:44:55 +0800826
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800827int Script::registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logancf3e5212010-12-29 01:44:55 +0800828 mpExtSymbolLookupFn = pFn;
829 mpExtSymbolLookupFnContext = pContext;
830
Logan7d2219f2011-01-06 06:19:25 +0800831 if (mStatus != ScriptStatus::Unknown) {
Logancf3e5212010-12-29 01:44:55 +0800832 mErrorCode = BCC_INVALID_OPERATION;
Steve Block10c14122012-01-08 10:15:06 +0000833 ALOGE("Invalid operation: %s\n", __func__);
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800834 return 1;
Logancf3e5212010-12-29 01:44:55 +0800835 }
Shih-wei Liaoce82d492011-01-20 12:34:03 -0800836 return 0;
Logancf3e5212010-12-29 01:44:55 +0800837}
Shih-wei Liaob65410d2011-06-19 11:11:48 -0700838
Shih-wei Liaoabc7f512012-01-18 00:34:07 -0800839bool Script::isCacheable() const {
840#if USE_CACHE
841 if (getBooleanProp("debug.bcc.nocache")) {
842 // Android system environment property: Disables the cache mechanism by
843 // setting "debug.bcc.nocache". So we will not load the cache file any
844 // way.
845 return false;
846 }
847
848 if (mCacheDir.empty() || mCacheName.empty()) {
849 // The application developer has not specified the cachePath, so
850 // we don't know where to open the cache file.
851 return false;
852 }
853
854 return true;
855#else
856 return false;
857#endif
858}
859
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700860#if USE_MCJIT
861size_t Script::getELFSize() const {
862 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700863 case ScriptStatus::Compiled: {
864 return mCompiled->getELFSize();
865 }
Daniel Malea094881f2011-12-14 17:39:16 -0500866#if USE_CACHE
867 case ScriptStatus::Cached: {
868 return mCached->getELFSize();
869 }
870#endif
Shih-wei Liaod50be322011-07-01 22:53:31 -0700871 default: {
872 return 0;
873 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700874 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700875}
876
877const char *Script::getELF() const {
878 switch (mStatus) {
Shih-wei Liaod50be322011-07-01 22:53:31 -0700879 case ScriptStatus::Compiled: {
880 return mCompiled->getELF();
881 }
Daniel Malea094881f2011-12-14 17:39:16 -0500882#if USE_CACHE
883 case ScriptStatus::Cached: {
884 return mCached->getELF();
885 }
886#endif
Shih-wei Liaod50be322011-07-01 22:53:31 -0700887 default: {
888 return NULL;
889 }
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700890 }
891}
892#endif
Logancf3e5212010-12-29 01:44:55 +0800893
894} // namespace bcc