blob: b4c1529b2509960f261e25f53e74525c6d463f70 [file] [log] [blame]
Brian Carlstrom934486c2011-07-12 23:42:50 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
Brian Carlstromb0460ea2011-07-29 10:08:05 -07003#include <dirent.h>
Elliott Hughes0af55432011-08-17 18:37:28 -07004#include <dlfcn.h>
Brian Carlstromb0460ea2011-07-29 10:08:05 -07005#include <sys/stat.h>
6#include <sys/types.h>
7
Elliott Hughes90a33692011-08-30 13:27:07 -07008#include "UniquePtr.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07009#include "base64.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "class_linker.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070011#include "class_loader.h"
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070012#include "compiler.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "dex_file.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070014#include "gtest/gtest.h"
Brian Carlstrom1f870082011-08-23 16:02:11 -070015#include "heap.h"
16#include "runtime.h"
Elliott Hughes90a33692011-08-30 13:27:07 -070017#include "stringprintf.h"
18#include "thread.h"
Elliott Hughes0af55432011-08-17 18:37:28 -070019#include "unicode/uclean.h"
20#include "unicode/uvernum.h"
21
Brian Carlstrom934486c2011-07-12 23:42:50 -070022namespace art {
23
Brian Carlstrom9f30b382011-08-28 22:41:38 -070024static inline const DexFile* OpenDexFileBase64(const char* base64,
25 const std::string& location) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070026 CHECK(base64 != NULL);
27 size_t length;
Brian Carlstromf615a612011-07-23 12:50:34 -070028 byte* dex_bytes = DecodeBase64(base64, &length);
29 CHECK(dex_bytes != NULL);
Brian Carlstrom9f30b382011-08-28 22:41:38 -070030 const DexFile* dex_file = DexFile::OpenPtr(dex_bytes, length, location);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070031 CHECK(dex_file != NULL);
Brian Carlstromf615a612011-07-23 12:50:34 -070032 return dex_file;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033}
34
Brian Carlstromdb4d5402011-08-09 12:18:28 -070035class ScratchFile {
36 public:
37 ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070038 filename_ = getenv("ANDROID_DATA");
39 filename_ += "/TmpFile-XXXXXX";
40 fd_ = mkstemp(&filename_[0]);
Brian Carlstromdb4d5402011-08-09 12:18:28 -070041 CHECK_NE(-1, fd_);
42 }
43
44 ~ScratchFile() {
Elliott Hughes34023802011-08-30 12:06:17 -070045 int unlink_result = unlink(filename_.c_str());
Brian Carlstromdb4d5402011-08-09 12:18:28 -070046 CHECK_EQ(0, unlink_result);
47 int close_result = close(fd_);
48 CHECK_EQ(0, close_result);
49 }
50
51 const char* GetFilename() const {
Elliott Hughes34023802011-08-30 12:06:17 -070052 return filename_.c_str();
Brian Carlstromdb4d5402011-08-09 12:18:28 -070053 }
54
55 int GetFd() const {
56 return fd_;
57 }
58
59 private:
Elliott Hughes34023802011-08-30 12:06:17 -070060 std::string filename_;
Brian Carlstromdb4d5402011-08-09 12:18:28 -070061 int fd_;
62};
63
Brian Carlstromf734cf52011-08-17 16:28:14 -070064class CommonTest : public testing::Test {
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070065 public:
66 static void LogMaps() {
67 const char* maps_file = "/proc/self/maps";
68 std::string contents;
69 CHECK(ReadFileToString(maps_file, &contents));
70 // logcat can't handle it all at once
71 std::vector<std::string> lines;
72 Split(contents, '\n', lines);
73 LOG(INFO) << maps_file << ":";
74 for (size_t i = 0; i < lines.size(); i++) {
75 LOG(INFO) << " " << lines[i];
76 }
77 }
78
79 static void MakeExecutable(const ByteArray* byte_array) {
80 uintptr_t data = reinterpret_cast<uintptr_t>(byte_array->GetData());
81 uintptr_t base = RoundDown(data, kPageSize);
82 uintptr_t limit = RoundUp(data + byte_array->GetLength(), kPageSize);
83 uintptr_t len = limit - base;
84 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
85 CHECK_EQ(result, 0);
86 }
87
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070088 protected:
89 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070090 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
91
Elliott Hughes0af55432011-08-17 18:37:28 -070092 if (is_host_) {
93 // $ANDROID_ROOT is set on the device, but not on the host.
94 // We need to set this so that icu4c can find its locale data.
95 std::string root;
96 root += getenv("ANDROID_BUILD_TOP");
97 root += "/out/host/linux-x86";
98 setenv("ANDROID_ROOT", root.c_str(), 1);
99 }
100
Elliott Hughes34023802011-08-30 12:06:17 -0700101 android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/sdcard/art-data-XXXXXX");
102 ASSERT_TRUE(mkdtemp(&android_data_[0]) != NULL);
103 setenv("ANDROID_DATA", android_data_.c_str(), 1);
104 art_cache_.append(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700105 art_cache_.append("/art-cache");
106 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
107 ASSERT_EQ(mkdir_result, 0);
108
Elliott Hughesa5b897e2011-08-16 11:33:06 -0700109 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700110
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700111 boot_class_path_.push_back(java_lang_dex_file_.get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700112
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700113 runtime_.reset(Runtime::Create(boot_class_path_));
Elliott Hughes90a33692011-08-30 13:27:07 -0700114 ASSERT_TRUE(runtime_.get() != NULL);
Carl Shapiro7a909592011-07-24 19:21:59 -0700115 class_linker_ = runtime_->GetClassLinker();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700116
117 Heap::VerifyHeap(); // Check for heap corruption before the test
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700118 }
119
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700120 virtual void TearDown() {
121 const char* android_data = getenv("ANDROID_DATA");
122 ASSERT_TRUE(android_data != NULL);
123 DIR* dir = opendir(art_cache_.c_str());
124 ASSERT_TRUE(dir != NULL);
125 while (true) {
126 struct dirent entry;
127 struct dirent* entry_ptr;
128 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
129 ASSERT_EQ(0, readdir_result);
130 if (entry_ptr == NULL) {
131 break;
132 }
133 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
134 continue;
135 }
136 std::string filename(art_cache_);
137 filename.push_back('/');
138 filename.append(entry_ptr->d_name);
139 int unlink_result = unlink(filename.c_str());
140 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400141 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700142 closedir(dir);
143 int rmdir_cache_result = rmdir(art_cache_.c_str());
144 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700145 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700146 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700147
148 // icu4c has a fixed 10-element array "gCommonICUDataArray".
149 // If we run > 10 tests, we fill that array and u_setCommonData fails.
150 // There's a function to clear the array, but it's not public...
151 typedef void (*IcuCleanupFn)();
152 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
153 CHECK(sym != NULL);
154 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
155 (*icu_cleanup_fn)();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700156
157 Heap::VerifyHeap(); // Check for heap corruption after the test
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700158 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400159
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700160 std::string GetLibCoreDexFileName() {
161 if (is_host_) {
162 const char* host_dir = getenv("ANDROID_HOST_OUT");
163 CHECK(host_dir != NULL);
164 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
165 }
166 return std::string("/system/framework/core.jar");
167 }
168
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700169 const DexFile* GetLibCoreDex() {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700170 std::string libcore_dex_file_name = GetLibCoreDexFileName();
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700171 return DexFile::OpenZip(libcore_dex_file_name);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400172 }
173
Brian Carlstrom8a487412011-08-29 20:08:52 -0700174 const PathClassLoader* AllocPathClassLoader(const DexFile* dex_file) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700175 CHECK(dex_file != NULL);
176 class_linker_->RegisterDexFile(*dex_file);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700177 std::vector<const DexFile*> dex_files;
178 dex_files.push_back(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700179 return PathClassLoader::Alloc(dex_files);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700180 }
181
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700182 const DexFile* OpenTestDexFile(const char* name) {
183 CHECK(name != NULL);
184 std::string filename;
185 if (is_host_) {
186 // on the host, just read target dex file
187 filename += getenv("ANDROID_PRODUCT_OUT");
188 }
189 filename += "/system/framework/art-test-dex-";
190 filename += name;
191 filename += ".jar";
192 const DexFile* dex_file = DexFile::OpenZip(filename);
193 CHECK(dex_file != NULL) << "Could not open " << filename;
194 return dex_file;
195 }
196
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700197 const ClassLoader* LoadDex(const char* dex_name) {
198 const DexFile* dex_file = OpenTestDexFile(dex_name);
199 CHECK(dex_file != NULL);
200 loaded_dex_files_.push_back(dex_file);
201 class_linker_->RegisterDexFile(*dex_file);
202 std::vector<const DexFile*> class_path;
203 class_path.push_back(dex_file);
204 const ClassLoader* class_loader = PathClassLoader::Alloc(class_path);
205 CHECK(class_loader != NULL);
206 Thread::Current()->SetClassLoaderOverride(class_loader);
207 return class_loader;
208 }
209
210 std::string ConvertClassNameToClassDescriptor(const char* class_name) {
211 std::string desc;
212 desc += "L";
213 desc += class_name;
214 desc += ";";
215 std::replace(desc.begin(), desc.end(), '.', '/');
216 return desc;
217 }
218
219 void CompileMethod(Method* method) {
220 CHECK(method != NULL);
221 Compiler compiler;
222 compiler.CompileOne(method);
223 MakeExecutable(method->GetCodeArray());
224 MakeExecutable(method->GetInvokeStubArray());
225 }
226
227 void CompileDirectMethod(const ClassLoader* class_loader,
228 const char* class_name,
229 const char* method_name,
230 const char* signature) {
231 std::string class_descriptor = ConvertClassNameToClassDescriptor(class_name);
232 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
233 CHECK(klass != NULL) << "Class not found " << class_name;
234 Method* method = klass->FindDirectMethod(method_name, signature);
235 CHECK(method != NULL) << "Method not found " << method_name;
236 CompileMethod(method);
237 }
238
239 void CompileVirtualMethod(const ClassLoader* class_loader,
240 const char* class_name,
241 const char* method_name,
242 const char* signature) {
243 std::string class_descriptor = ConvertClassNameToClassDescriptor(class_name);
244 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
245 CHECK(klass != NULL) << "Class not found " << class_name;
246 Method* method = klass->FindVirtualMethod(method_name, signature);
247 CHECK(method != NULL) << "Method not found " << method_name;
248 CompileMethod(method);
249 }
250
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700251 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700252 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700253 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700254 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700255 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700256 UniquePtr<Runtime> runtime_;
Carl Shapiro7a909592011-07-24 19:21:59 -0700257 ClassLinker* class_linker_;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700258
259 private:
260 std::vector<const DexFile*> loaded_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700261};
262
Brian Carlstrom934486c2011-07-12 23:42:50 -0700263} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700264
265namespace std {
266
267// TODO: isn't gtest supposed to be able to print STL types for itself?
268template <typename T>
269std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
270 os << "[";
271 for (size_t i = 0; i < rhs.size(); ++i) {
272 os << rhs[i];
273 if (i < rhs.size() - 1) {
274 os << ", ";
275 }
276 }
277 os << "]";
278 return os;
279}
280
281} // namespace std