blob: 98d91275165f1b747f031b87436dcad1e594c497 [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:
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -070066 static void MakeExecutable(const ByteArray* byte_array) {
67 uintptr_t data = reinterpret_cast<uintptr_t>(byte_array->GetData());
68 uintptr_t base = RoundDown(data, kPageSize);
69 uintptr_t limit = RoundUp(data + byte_array->GetLength(), kPageSize);
70 uintptr_t len = limit - base;
71 int result = mprotect(reinterpret_cast<void*>(base), len, PROT_READ | PROT_WRITE | PROT_EXEC);
72 CHECK_EQ(result, 0);
73 }
74
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070075 protected:
76 virtual void SetUp() {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070077 is_host_ = getenv("ANDROID_BUILD_TOP") != NULL;
78
Elliott Hughes0af55432011-08-17 18:37:28 -070079 if (is_host_) {
80 // $ANDROID_ROOT is set on the device, but not on the host.
81 // We need to set this so that icu4c can find its locale data.
82 std::string root;
83 root += getenv("ANDROID_BUILD_TOP");
84 root += "/out/host/linux-x86";
85 setenv("ANDROID_ROOT", root.c_str(), 1);
86 }
87
Brian Carlstrom69b15fb2011-09-03 12:25:21 -070088 // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of art-cache
89 android_data_ = (is_host_ ? "/tmp/art-data-XXXXXX" : "/data/art-cache/art-data-XXXXXX");
Elliott Hughes0f4c41d2011-09-04 14:58:03 -070090 if (mkdtemp(&android_data_[0]) == NULL) {
91 PLOG(FATAL) << "mkdtemp(\"" << &android_data_[0] << "\") failed";
92 }
Elliott Hughes34023802011-08-30 12:06:17 -070093 setenv("ANDROID_DATA", android_data_.c_str(), 1);
94 art_cache_.append(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -070095 art_cache_.append("/art-cache");
96 int mkdir_result = mkdir(art_cache_.c_str(), 0700);
97 ASSERT_EQ(mkdir_result, 0);
98
Elliott Hughesa5b897e2011-08-16 11:33:06 -070099 java_lang_dex_file_.reset(GetLibCoreDex());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700100
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700101 boot_class_path_.push_back(java_lang_dex_file_.get());
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700102
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700103 Runtime::Options options;
104 options.push_back(std::make_pair("bootclasspath", &boot_class_path_));
105 options.push_back(std::make_pair("-Xcheck:jni", reinterpret_cast<void*>(NULL)));
106 runtime_.reset(Runtime::Create(options, false));
Elliott Hughes90a33692011-08-30 13:27:07 -0700107 ASSERT_TRUE(runtime_.get() != NULL);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700108 runtime_->Start();
Carl Shapiro7a909592011-07-24 19:21:59 -0700109 class_linker_ = runtime_->GetClassLinker();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700110
111 Heap::VerifyHeap(); // Check for heap corruption before the test
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700112 }
113
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700114 virtual void TearDown() {
115 const char* android_data = getenv("ANDROID_DATA");
116 ASSERT_TRUE(android_data != NULL);
117 DIR* dir = opendir(art_cache_.c_str());
118 ASSERT_TRUE(dir != NULL);
119 while (true) {
120 struct dirent entry;
121 struct dirent* entry_ptr;
122 int readdir_result = readdir_r(dir, &entry, &entry_ptr);
123 ASSERT_EQ(0, readdir_result);
124 if (entry_ptr == NULL) {
125 break;
126 }
127 if ((strcmp(entry_ptr->d_name, ".") == 0) || (strcmp(entry_ptr->d_name, "..") == 0)) {
128 continue;
129 }
130 std::string filename(art_cache_);
131 filename.push_back('/');
132 filename.append(entry_ptr->d_name);
133 int unlink_result = unlink(filename.c_str());
134 ASSERT_EQ(0, unlink_result);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400135 }
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700136 closedir(dir);
137 int rmdir_cache_result = rmdir(art_cache_.c_str());
138 ASSERT_EQ(0, rmdir_cache_result);
Elliott Hughes34023802011-08-30 12:06:17 -0700139 int rmdir_data_result = rmdir(android_data_.c_str());
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700140 ASSERT_EQ(0, rmdir_data_result);
Elliott Hughes0af55432011-08-17 18:37:28 -0700141
142 // icu4c has a fixed 10-element array "gCommonICUDataArray".
143 // If we run > 10 tests, we fill that array and u_setCommonData fails.
144 // There's a function to clear the array, but it's not public...
145 typedef void (*IcuCleanupFn)();
146 void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT);
147 CHECK(sym != NULL);
148 IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym);
149 (*icu_cleanup_fn)();
Ian Rogers0cfe1fb2011-08-26 03:29:44 -0700150
151 Heap::VerifyHeap(); // Check for heap corruption after the test
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700152 }
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400153
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700154 std::string GetLibCoreDexFileName() {
155 if (is_host_) {
156 const char* host_dir = getenv("ANDROID_HOST_OUT");
157 CHECK(host_dir != NULL);
158 return StringPrintf("%s/framework/core-hostdex.jar", host_dir);
159 }
160 return std::string("/system/framework/core.jar");
161 }
162
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700163 const DexFile* GetLibCoreDex() {
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700164 std::string libcore_dex_file_name = GetLibCoreDexFileName();
Elliott Hughes40ef99e2011-08-11 17:44:34 -0700165 return DexFile::OpenZip(libcore_dex_file_name);
Jesse Wilsonac5b9e22011-07-27 15:11:13 -0400166 }
167
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700168 uint32_t FindTypeIdxByDescriptor(const DexFile& dex_file, const StringPiece& descriptor) {
169 for (size_t i = 0; i < dex_file.NumTypeIds(); i++) {
170 const DexFile::TypeId& type_id = dex_file.GetTypeId(i);
171 if (descriptor == dex_file.GetTypeDescriptor(type_id)) {
172 return i;
173 }
174 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700175 CHECK(false) << "Failed to find type index for " << descriptor;
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700176 return 0;
177 }
178
179 uint32_t FindFieldIdxByDescriptorAndName(const DexFile& dex_file,
180 const StringPiece& class_descriptor,
181 const StringPiece& field_name) {
182 for (size_t i = 0; i < dex_file.NumFieldIds(); i++) {
183 const DexFile::FieldId& field_id = dex_file.GetFieldId(i);
184 if (class_descriptor == dex_file.GetFieldClassDescriptor(field_id)
185 && field_name == dex_file.GetFieldName(field_id)) {
186 return i;
187 }
188 }
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700189 CHECK(false) << "Failed to find field index for " << class_descriptor << " " << field_name;
Brian Carlstrom848a4b32011-09-04 11:29:27 -0700190 return 0;
191 }
192
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700193 const ClassLoader* AllocPathClassLoader(const DexFile* dex_file) {
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700194 CHECK(dex_file != NULL);
195 class_linker_->RegisterDexFile(*dex_file);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700196 std::vector<const DexFile*> dex_files;
197 dex_files.push_back(dex_file);
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700198 return PathClassLoader::Alloc(dex_files);
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700199 }
200
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700201 const DexFile* OpenTestDexFile(const char* name) {
202 CHECK(name != NULL);
203 std::string filename;
204 if (is_host_) {
205 // on the host, just read target dex file
206 filename += getenv("ANDROID_PRODUCT_OUT");
207 }
208 filename += "/system/framework/art-test-dex-";
209 filename += name;
210 filename += ".jar";
211 const DexFile* dex_file = DexFile::OpenZip(filename);
Brian Carlstrom69b15fb2011-09-03 12:25:21 -0700212 CHECK(dex_file != NULL) << "Failed to open " << filename;
Brian Carlstrom9f30b382011-08-28 22:41:38 -0700213 return dex_file;
214 }
215
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700216 const ClassLoader* LoadDex(const char* dex_name) {
217 const DexFile* dex_file = OpenTestDexFile(dex_name);
218 CHECK(dex_file != NULL);
219 loaded_dex_files_.push_back(dex_file);
220 class_linker_->RegisterDexFile(*dex_file);
221 std::vector<const DexFile*> class_path;
222 class_path.push_back(dex_file);
223 const ClassLoader* class_loader = PathClassLoader::Alloc(class_path);
224 CHECK(class_loader != NULL);
225 Thread::Current()->SetClassLoaderOverride(class_loader);
226 return class_loader;
227 }
228
229 std::string ConvertClassNameToClassDescriptor(const char* class_name) {
230 std::string desc;
231 desc += "L";
232 desc += class_name;
233 desc += ";";
234 std::replace(desc.begin(), desc.end(), '.', '/');
235 return desc;
236 }
237
238 void CompileMethod(Method* method) {
239 CHECK(method != NULL);
240 Compiler compiler;
241 compiler.CompileOne(method);
242 MakeExecutable(method->GetCodeArray());
243 MakeExecutable(method->GetInvokeStubArray());
244 }
245
246 void CompileDirectMethod(const ClassLoader* class_loader,
247 const char* class_name,
248 const char* method_name,
249 const char* signature) {
250 std::string class_descriptor = ConvertClassNameToClassDescriptor(class_name);
251 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
252 CHECK(klass != NULL) << "Class not found " << class_name;
253 Method* method = klass->FindDirectMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700254 CHECK(method != NULL) << "Direct method not found: "
255 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700256 CompileMethod(method);
257 }
258
259 void CompileVirtualMethod(const ClassLoader* class_loader,
260 const char* class_name,
261 const char* method_name,
262 const char* signature) {
263 std::string class_descriptor = ConvertClassNameToClassDescriptor(class_name);
264 Class* klass = class_linker_->FindClass(class_descriptor, class_loader);
265 CHECK(klass != NULL) << "Class not found " << class_name;
266 Method* method = klass->FindVirtualMethod(method_name, signature);
Elliott Hughes0f4c41d2011-09-04 14:58:03 -0700267 CHECK(method != NULL) << "Virtual method not found: "
268 << class_name << "." << method_name << signature;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700269 CompileMethod(method);
270 }
271
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700272 bool is_host_;
Elliott Hughes34023802011-08-30 12:06:17 -0700273 std::string android_data_;
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700274 std::string art_cache_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700275 UniquePtr<const DexFile> java_lang_dex_file_;
Brian Carlstrom9ea1cb12011-08-24 23:18:18 -0700276 std::vector<const DexFile*> boot_class_path_;
Elliott Hughes90a33692011-08-30 13:27:07 -0700277 UniquePtr<Runtime> runtime_;
Carl Shapiro7a909592011-07-24 19:21:59 -0700278 ClassLinker* class_linker_;
Brian Carlstrom9baa4ae2011-09-01 21:14:14 -0700279
280 private:
281 std::vector<const DexFile*> loaded_dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700282};
283
Brian Carlstrom934486c2011-07-12 23:42:50 -0700284} // namespace art
Elliott Hughes34023802011-08-30 12:06:17 -0700285
286namespace std {
287
288// TODO: isn't gtest supposed to be able to print STL types for itself?
289template <typename T>
290std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) {
291 os << "[";
292 for (size_t i = 0; i < rhs.size(); ++i) {
293 os << rhs[i];
294 if (i < rhs.size() - 1) {
295 os << ", ";
296 }
297 }
298 os << "]";
299 return os;
300}
301
302} // namespace std