Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 17 | #include "common_runtime_test.h" |
| 18 | |
| 19 | #include <dirent.h> |
| 20 | #include <dlfcn.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <ScopedLocalRef.h> |
| 23 | |
| 24 | #include "../../external/icu/icu4c/source/common/unicode/uvernum.h" |
Andreas Gampe | 1fe5e5c | 2014-07-11 21:14:35 -0700 | [diff] [blame] | 25 | #include "base/macros.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 26 | #include "base/logging.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 27 | #include "base/stl_util.h" |
| 28 | #include "base/stringprintf.h" |
| 29 | #include "base/unix_file/fd_file.h" |
| 30 | #include "class_linker.h" |
| 31 | #include "compiler_callbacks.h" |
| 32 | #include "dex_file.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 33 | #include "gc_root-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 34 | #include "gc/heap.h" |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 35 | #include "gtest/gtest.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 36 | #include "jni_internal.h" |
| 37 | #include "mirror/class_loader.h" |
| 38 | #include "noop_compiler_callbacks.h" |
| 39 | #include "os.h" |
| 40 | #include "runtime-inl.h" |
| 41 | #include "scoped_thread_state_change.h" |
| 42 | #include "thread.h" |
| 43 | #include "well_known_classes.h" |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 44 | |
| 45 | int main(int argc, char **argv) { |
| 46 | art::InitLogging(argv); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 47 | LOG(INFO) << "Running main() from common_runtime_test.cc..."; |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 48 | testing::InitGoogleTest(&argc, argv); |
| 49 | return RUN_ALL_TESTS(); |
| 50 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 51 | |
| 52 | namespace art { |
| 53 | |
| 54 | ScratchFile::ScratchFile() { |
| 55 | // ANDROID_DATA needs to be set |
| 56 | CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) << |
| 57 | "Are you subclassing RuntimeTest?"; |
| 58 | filename_ = getenv("ANDROID_DATA"); |
| 59 | filename_ += "/TmpFile-XXXXXX"; |
| 60 | int fd = mkstemp(&filename_[0]); |
| 61 | CHECK_NE(-1, fd); |
| 62 | file_.reset(new File(fd, GetFilename())); |
| 63 | } |
| 64 | |
| 65 | ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) { |
| 66 | filename_ = other.GetFilename(); |
| 67 | filename_ += suffix; |
| 68 | int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666); |
| 69 | CHECK_NE(-1, fd); |
| 70 | file_.reset(new File(fd, GetFilename())); |
| 71 | } |
| 72 | |
| 73 | ScratchFile::ScratchFile(File* file) { |
| 74 | CHECK(file != NULL); |
| 75 | filename_ = file->GetPath(); |
| 76 | file_.reset(file); |
| 77 | } |
| 78 | |
| 79 | ScratchFile::~ScratchFile() { |
| 80 | Unlink(); |
| 81 | } |
| 82 | |
| 83 | int ScratchFile::GetFd() const { |
| 84 | return file_->Fd(); |
| 85 | } |
| 86 | |
| 87 | void ScratchFile::Unlink() { |
| 88 | if (!OS::FileExists(filename_.c_str())) { |
| 89 | return; |
| 90 | } |
| 91 | int unlink_result = unlink(filename_.c_str()); |
| 92 | CHECK_EQ(0, unlink_result); |
| 93 | } |
| 94 | |
| 95 | CommonRuntimeTest::CommonRuntimeTest() {} |
| 96 | CommonRuntimeTest::~CommonRuntimeTest() {} |
| 97 | |
Andreas Gampe | f896965 | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 98 | void CommonRuntimeTest::SetUpAndroidRoot() { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 99 | if (IsHost()) { |
| 100 | // $ANDROID_ROOT is set on the device, but not necessarily on the host. |
| 101 | // But it needs to be set so that icu4c can find its locale data. |
| 102 | const char* android_root_from_env = getenv("ANDROID_ROOT"); |
| 103 | if (android_root_from_env == nullptr) { |
| 104 | // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set. |
| 105 | const char* android_host_out = getenv("ANDROID_HOST_OUT"); |
| 106 | if (android_host_out != nullptr) { |
| 107 | setenv("ANDROID_ROOT", android_host_out, 1); |
| 108 | } else { |
| 109 | // Build it from ANDROID_BUILD_TOP or cwd |
| 110 | std::string root; |
| 111 | const char* android_build_top = getenv("ANDROID_BUILD_TOP"); |
| 112 | if (android_build_top != nullptr) { |
| 113 | root += android_build_top; |
| 114 | } else { |
| 115 | // Not set by build server, so default to current directory |
| 116 | char* cwd = getcwd(nullptr, 0); |
| 117 | setenv("ANDROID_BUILD_TOP", cwd, 1); |
| 118 | root += cwd; |
| 119 | free(cwd); |
| 120 | } |
| 121 | #if defined(__linux__) |
| 122 | root += "/out/host/linux-x86"; |
| 123 | #elif defined(__APPLE__) |
| 124 | root += "/out/host/darwin-x86"; |
| 125 | #else |
| 126 | #error unsupported OS |
| 127 | #endif |
| 128 | setenv("ANDROID_ROOT", root.c_str(), 1); |
| 129 | } |
| 130 | } |
| 131 | setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>. |
| 132 | |
| 133 | // Not set by build server, so default |
| 134 | if (getenv("ANDROID_HOST_OUT") == nullptr) { |
| 135 | setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1); |
| 136 | } |
| 137 | } |
Andreas Gampe | f896965 | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 138 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 139 | |
Andreas Gampe | f896965 | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 140 | void CommonRuntimeTest::SetUpAndroidData(std::string& android_data) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 141 | // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache |
Andreas Gampe | 2994e29 | 2014-08-06 13:12:26 -0700 | [diff] [blame^] | 142 | if (IsHost()) { |
| 143 | const char* tmpdir = getenv("TMPDIR"); |
| 144 | if (tmpdir != nullptr && tmpdir[0] != 0) { |
| 145 | android_data = tmpdir; |
| 146 | } else { |
| 147 | android_data = "/tmp"; |
| 148 | } |
| 149 | } else { |
| 150 | android_data = "/data/dalvik-cache"; |
| 151 | } |
| 152 | android_data += "/art-data-XXXXXX"; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 153 | if (mkdtemp(&android_data[0]) == nullptr) { |
| 154 | PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed"; |
| 155 | } |
| 156 | setenv("ANDROID_DATA", android_data.c_str(), 1); |
| 157 | } |
| 158 | |
Andreas Gampe | f896965 | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 159 | void CommonRuntimeTest::TearDownAndroidData(const std::string& android_data, bool fail_on_error) { |
| 160 | if (fail_on_error) { |
| 161 | ASSERT_EQ(rmdir(android_data.c_str()), 0); |
| 162 | } else { |
| 163 | rmdir(android_data.c_str()); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 168 | const DexFile* CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) { |
| 169 | std::vector<const DexFile*> dex_files; |
| 170 | std::string error_msg; |
| 171 | if (!DexFile::Open(location, location, &error_msg, &dex_files)) { |
| 172 | LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n"; |
| 173 | return nullptr; |
| 174 | } else { |
| 175 | CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location; |
| 176 | return dex_files[0]; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void CommonRuntimeTest::SetUp() { |
Andreas Gampe | f896965 | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 181 | SetUpAndroidRoot(); |
| 182 | SetUpAndroidData(android_data_); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 183 | dalvik_cache_.append(android_data_.c_str()); |
| 184 | dalvik_cache_.append("/dalvik-cache"); |
| 185 | int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700); |
| 186 | ASSERT_EQ(mkdir_result, 0); |
| 187 | |
| 188 | std::string error_msg; |
| 189 | java_lang_dex_file_ = LoadExpectSingleDexFile(GetLibCoreDexFileName().c_str()); |
| 190 | boot_class_path_.push_back(java_lang_dex_file_); |
| 191 | |
| 192 | std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB)); |
| 193 | std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB)); |
| 194 | |
| 195 | callbacks_.reset(new NoopCompilerCallbacks()); |
| 196 | |
| 197 | RuntimeOptions options; |
| 198 | options.push_back(std::make_pair("bootclasspath", &boot_class_path_)); |
| 199 | options.push_back(std::make_pair("-Xcheck:jni", nullptr)); |
| 200 | options.push_back(std::make_pair(min_heap_string.c_str(), nullptr)); |
| 201 | options.push_back(std::make_pair(max_heap_string.c_str(), nullptr)); |
| 202 | options.push_back(std::make_pair("compilercallbacks", callbacks_.get())); |
| 203 | SetUpRuntimeOptions(&options); |
| 204 | if (!Runtime::Create(options, false)) { |
| 205 | LOG(FATAL) << "Failed to create runtime"; |
| 206 | return; |
| 207 | } |
| 208 | runtime_.reset(Runtime::Current()); |
| 209 | class_linker_ = runtime_->GetClassLinker(); |
| 210 | class_linker_->FixupDexCaches(runtime_->GetResolutionMethod()); |
| 211 | class_linker_->RunRootClinits(); |
| 212 | |
| 213 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we |
| 214 | // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess. |
| 215 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 216 | |
| 217 | // We're back in native, take the opportunity to initialize well known classes. |
| 218 | WellKnownClasses::Init(Thread::Current()->GetJniEnv()); |
| 219 | |
| 220 | // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread |
| 221 | // pool is created by the runtime. |
| 222 | runtime_->GetHeap()->CreateThreadPool(); |
| 223 | runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test |
| 224 | } |
| 225 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 226 | void CommonRuntimeTest::ClearDirectory(const char* dirpath) { |
| 227 | ASSERT_TRUE(dirpath != nullptr); |
| 228 | DIR* dir = opendir(dirpath); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 229 | ASSERT_TRUE(dir != nullptr); |
| 230 | dirent* e; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 231 | struct stat s; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 232 | while ((e = readdir(dir)) != nullptr) { |
| 233 | if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) { |
| 234 | continue; |
| 235 | } |
| 236 | std::string filename(dalvik_cache_); |
| 237 | filename.push_back('/'); |
| 238 | filename.append(e->d_name); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 239 | int stat_result = lstat(filename.c_str(), &s); |
| 240 | ASSERT_EQ(0, stat_result) << "unable to stat " << filename; |
| 241 | if (S_ISDIR(s.st_mode)) { |
| 242 | ClearDirectory(filename.c_str()); |
| 243 | int rmdir_result = rmdir(filename.c_str()); |
| 244 | ASSERT_EQ(0, rmdir_result) << filename; |
| 245 | } else { |
| 246 | int unlink_result = unlink(filename.c_str()); |
| 247 | ASSERT_EQ(0, unlink_result) << filename; |
| 248 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 249 | } |
| 250 | closedir(dir); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void CommonRuntimeTest::TearDown() { |
| 254 | const char* android_data = getenv("ANDROID_DATA"); |
| 255 | ASSERT_TRUE(android_data != nullptr); |
| 256 | ClearDirectory(dalvik_cache_.c_str()); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 257 | int rmdir_cache_result = rmdir(dalvik_cache_.c_str()); |
| 258 | ASSERT_EQ(0, rmdir_cache_result); |
Andreas Gampe | f896965 | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 259 | TearDownAndroidData(android_data_, true); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 260 | |
| 261 | // icu4c has a fixed 10-element array "gCommonICUDataArray". |
| 262 | // If we run > 10 tests, we fill that array and u_setCommonData fails. |
| 263 | // There's a function to clear the array, but it's not public... |
| 264 | typedef void (*IcuCleanupFn)(); |
| 265 | void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT); |
| 266 | CHECK(sym != nullptr) << dlerror(); |
| 267 | IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym); |
| 268 | (*icu_cleanup_fn)(); |
| 269 | |
| 270 | STLDeleteElements(&opened_dex_files_); |
| 271 | |
| 272 | Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test |
| 273 | } |
| 274 | |
| 275 | std::string CommonRuntimeTest::GetLibCoreDexFileName() { |
| 276 | return GetDexFileName("core-libart"); |
| 277 | } |
| 278 | |
| 279 | std::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) { |
| 280 | if (IsHost()) { |
| 281 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 282 | CHECK(host_dir != nullptr); |
| 283 | return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str()); |
| 284 | } |
| 285 | return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str()); |
| 286 | } |
| 287 | |
| 288 | std::string CommonRuntimeTest::GetTestAndroidRoot() { |
| 289 | if (IsHost()) { |
| 290 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 291 | CHECK(host_dir != nullptr); |
| 292 | return host_dir; |
| 293 | } |
| 294 | return GetAndroidRoot(); |
| 295 | } |
| 296 | |
Andreas Gampe | 1fe5e5c | 2014-07-11 21:14:35 -0700 | [diff] [blame] | 297 | // Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. |
| 298 | #ifdef ART_TARGET |
| 299 | #ifndef ART_TARGET_NATIVETEST_DIR |
| 300 | #error "ART_TARGET_NATIVETEST_DIR not set." |
| 301 | #endif |
| 302 | // Wrap it as a string literal. |
| 303 | #define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" |
| 304 | #else |
| 305 | #define ART_TARGET_NATIVETEST_DIR_STRING "" |
| 306 | #endif |
| 307 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 308 | std::vector<const DexFile*> CommonRuntimeTest::OpenTestDexFiles(const char* name) { |
| 309 | CHECK(name != nullptr); |
| 310 | std::string filename; |
| 311 | if (IsHost()) { |
| 312 | filename += getenv("ANDROID_HOST_OUT"); |
| 313 | filename += "/framework/"; |
| 314 | } else { |
Andreas Gampe | 1fe5e5c | 2014-07-11 21:14:35 -0700 | [diff] [blame] | 315 | filename += ART_TARGET_NATIVETEST_DIR_STRING; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 316 | } |
| 317 | filename += "art-gtest-"; |
| 318 | filename += name; |
| 319 | filename += ".jar"; |
| 320 | std::string error_msg; |
| 321 | std::vector<const DexFile*> dex_files; |
| 322 | bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files); |
| 323 | CHECK(success) << "Failed to open '" << filename << "': " << error_msg; |
| 324 | for (const DexFile* dex_file : dex_files) { |
| 325 | CHECK_EQ(PROT_READ, dex_file->GetPermissions()); |
| 326 | CHECK(dex_file->IsReadOnly()); |
| 327 | } |
| 328 | opened_dex_files_.insert(opened_dex_files_.end(), dex_files.begin(), dex_files.end()); |
| 329 | return dex_files; |
| 330 | } |
| 331 | |
| 332 | const DexFile* CommonRuntimeTest::OpenTestDexFile(const char* name) { |
| 333 | std::vector<const DexFile*> vector = OpenTestDexFiles(name); |
| 334 | EXPECT_EQ(1U, vector.size()); |
| 335 | return vector[0]; |
| 336 | } |
| 337 | |
| 338 | jobject CommonRuntimeTest::LoadDex(const char* dex_name) { |
| 339 | std::vector<const DexFile*> dex_files = OpenTestDexFiles(dex_name); |
| 340 | CHECK_NE(0U, dex_files.size()); |
| 341 | for (const DexFile* dex_file : dex_files) { |
| 342 | class_linker_->RegisterDexFile(*dex_file); |
| 343 | } |
| 344 | ScopedObjectAccessUnchecked soa(Thread::Current()); |
| 345 | ScopedLocalRef<jobject> class_loader_local(soa.Env(), |
| 346 | soa.Env()->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 347 | jobject class_loader = soa.Env()->NewGlobalRef(class_loader_local.get()); |
| 348 | soa.Self()->SetClassLoaderOverride(soa.Decode<mirror::ClassLoader*>(class_loader_local.get())); |
| 349 | Runtime::Current()->SetCompileTimeClassPath(class_loader, dex_files); |
| 350 | return class_loader; |
| 351 | } |
| 352 | |
| 353 | CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) { |
| 354 | vm_->check_jni_abort_hook = Hook; |
| 355 | vm_->check_jni_abort_hook_data = &actual_; |
| 356 | } |
| 357 | |
| 358 | CheckJniAbortCatcher::~CheckJniAbortCatcher() { |
| 359 | vm_->check_jni_abort_hook = nullptr; |
| 360 | vm_->check_jni_abort_hook_data = nullptr; |
| 361 | EXPECT_TRUE(actual_.empty()) << actual_; |
| 362 | } |
| 363 | |
| 364 | void CheckJniAbortCatcher::Check(const char* expected_text) { |
| 365 | EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n" |
| 366 | << "Expected to find: " << expected_text << "\n" |
| 367 | << "In the output : " << actual_; |
| 368 | actual_.clear(); |
| 369 | } |
| 370 | |
| 371 | void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) { |
| 372 | // We use += because when we're hooking the aborts like this, multiple problems can be found. |
| 373 | *reinterpret_cast<std::string*>(data) += reason; |
| 374 | } |
| 375 | |
| 376 | } // namespace art |
| 377 | |
| 378 | namespace std { |
| 379 | |
| 380 | template <typename T> |
| 381 | std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) { |
| 382 | os << ::art::ToString(rhs); |
| 383 | return os; |
| 384 | } |
| 385 | |
| 386 | } // namespace std |