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> |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 23 | #include <stdlib.h> |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 24 | |
| 25 | #include "../../external/icu/icu4c/source/common/unicode/uvernum.h" |
Andreas Gampe | 1fe5e5c | 2014-07-11 21:14:35 -0700 | [diff] [blame] | 26 | #include "base/macros.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 27 | #include "base/logging.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 28 | #include "base/stl_util.h" |
| 29 | #include "base/stringprintf.h" |
| 30 | #include "base/unix_file/fd_file.h" |
| 31 | #include "class_linker.h" |
| 32 | #include "compiler_callbacks.h" |
| 33 | #include "dex_file.h" |
Hiroshi Yamauchi | 94f7b49 | 2014-07-22 18:08:23 -0700 | [diff] [blame] | 34 | #include "gc_root-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 35 | #include "gc/heap.h" |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 36 | #include "gtest/gtest.h" |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 37 | #include "handle_scope-inl.h" |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 38 | #include "interpreter/unstarted_runtime.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 39 | #include "jni_internal.h" |
| 40 | #include "mirror/class_loader.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 41 | #include "mem_map.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 42 | #include "noop_compiler_callbacks.h" |
| 43 | #include "os.h" |
| 44 | #include "runtime-inl.h" |
| 45 | #include "scoped_thread_state_change.h" |
| 46 | #include "thread.h" |
| 47 | #include "well_known_classes.h" |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 48 | |
| 49 | int main(int argc, char **argv) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 50 | // Gtests can be very noisy. For example, an executable with multiple tests will trigger native |
| 51 | // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses |
| 52 | // everything else. In case you want to see all messages, comment out the line. |
Richard Uhler | 892fc96 | 2015-03-10 16:57:05 +0000 | [diff] [blame] | 53 | setenv("ANDROID_LOG_TAGS", "*:e", 1); |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 54 | |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 55 | art::InitLogging(argv); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 56 | LOG(::art::INFO) << "Running main() from common_runtime_test.cc..."; |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 57 | testing::InitGoogleTest(&argc, argv); |
| 58 | return RUN_ALL_TESTS(); |
| 59 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 60 | |
| 61 | namespace art { |
| 62 | |
| 63 | ScratchFile::ScratchFile() { |
| 64 | // ANDROID_DATA needs to be set |
| 65 | CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) << |
| 66 | "Are you subclassing RuntimeTest?"; |
| 67 | filename_ = getenv("ANDROID_DATA"); |
| 68 | filename_ += "/TmpFile-XXXXXX"; |
| 69 | int fd = mkstemp(&filename_[0]); |
| 70 | CHECK_NE(-1, fd); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 71 | file_.reset(new File(fd, GetFilename(), true)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) { |
| 75 | filename_ = other.GetFilename(); |
| 76 | filename_ += suffix; |
| 77 | int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666); |
| 78 | CHECK_NE(-1, fd); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 79 | file_.reset(new File(fd, GetFilename(), true)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | ScratchFile::ScratchFile(File* file) { |
| 83 | CHECK(file != NULL); |
| 84 | filename_ = file->GetPath(); |
| 85 | file_.reset(file); |
| 86 | } |
| 87 | |
| 88 | ScratchFile::~ScratchFile() { |
| 89 | Unlink(); |
| 90 | } |
| 91 | |
| 92 | int ScratchFile::GetFd() const { |
| 93 | return file_->Fd(); |
| 94 | } |
| 95 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 96 | void ScratchFile::Close() { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 97 | if (file_.get() != nullptr) { |
| 98 | if (file_->FlushCloseOrErase() != 0) { |
| 99 | PLOG(WARNING) << "Error closing scratch file."; |
| 100 | } |
| 101 | } |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void ScratchFile::Unlink() { |
| 105 | if (!OS::FileExists(filename_.c_str())) { |
| 106 | return; |
| 107 | } |
| 108 | Close(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 109 | int unlink_result = unlink(filename_.c_str()); |
| 110 | CHECK_EQ(0, unlink_result); |
| 111 | } |
| 112 | |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 113 | static bool unstarted_initialized_ = false; |
| 114 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 115 | CommonRuntimeTest::CommonRuntimeTest() {} |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 116 | CommonRuntimeTest::~CommonRuntimeTest() { |
| 117 | // Ensure the dex files are cleaned up before the runtime. |
| 118 | loaded_dex_files_.clear(); |
| 119 | runtime_.reset(); |
| 120 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 121 | |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 122 | void CommonRuntimeTest::SetUpAndroidRoot() { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 123 | if (IsHost()) { |
| 124 | // $ANDROID_ROOT is set on the device, but not necessarily on the host. |
| 125 | // But it needs to be set so that icu4c can find its locale data. |
| 126 | const char* android_root_from_env = getenv("ANDROID_ROOT"); |
| 127 | if (android_root_from_env == nullptr) { |
| 128 | // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set. |
| 129 | const char* android_host_out = getenv("ANDROID_HOST_OUT"); |
| 130 | if (android_host_out != nullptr) { |
| 131 | setenv("ANDROID_ROOT", android_host_out, 1); |
| 132 | } else { |
| 133 | // Build it from ANDROID_BUILD_TOP or cwd |
| 134 | std::string root; |
| 135 | const char* android_build_top = getenv("ANDROID_BUILD_TOP"); |
| 136 | if (android_build_top != nullptr) { |
| 137 | root += android_build_top; |
| 138 | } else { |
| 139 | // Not set by build server, so default to current directory |
| 140 | char* cwd = getcwd(nullptr, 0); |
| 141 | setenv("ANDROID_BUILD_TOP", cwd, 1); |
| 142 | root += cwd; |
| 143 | free(cwd); |
| 144 | } |
| 145 | #if defined(__linux__) |
| 146 | root += "/out/host/linux-x86"; |
| 147 | #elif defined(__APPLE__) |
| 148 | root += "/out/host/darwin-x86"; |
| 149 | #else |
| 150 | #error unsupported OS |
| 151 | #endif |
| 152 | setenv("ANDROID_ROOT", root.c_str(), 1); |
| 153 | } |
| 154 | } |
| 155 | setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>. |
| 156 | |
| 157 | // Not set by build server, so default |
| 158 | if (getenv("ANDROID_HOST_OUT") == nullptr) { |
| 159 | setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1); |
| 160 | } |
| 161 | } |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 162 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 163 | |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 164 | void CommonRuntimeTest::SetUpAndroidData(std::string& android_data) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 165 | // On target, Cannot use /mnt/sdcard because it is mounted noexec, so use subdir of dalvik-cache |
Andreas Gampe | 5a79fde | 2014-08-06 13:12:26 -0700 | [diff] [blame] | 166 | if (IsHost()) { |
| 167 | const char* tmpdir = getenv("TMPDIR"); |
| 168 | if (tmpdir != nullptr && tmpdir[0] != 0) { |
| 169 | android_data = tmpdir; |
| 170 | } else { |
| 171 | android_data = "/tmp"; |
| 172 | } |
| 173 | } else { |
| 174 | android_data = "/data/dalvik-cache"; |
| 175 | } |
| 176 | android_data += "/art-data-XXXXXX"; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 177 | if (mkdtemp(&android_data[0]) == nullptr) { |
| 178 | PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed"; |
| 179 | } |
| 180 | setenv("ANDROID_DATA", android_data.c_str(), 1); |
| 181 | } |
| 182 | |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 183 | void CommonRuntimeTest::TearDownAndroidData(const std::string& android_data, bool fail_on_error) { |
| 184 | if (fail_on_error) { |
| 185 | ASSERT_EQ(rmdir(android_data.c_str()), 0); |
| 186 | } else { |
| 187 | rmdir(android_data.c_str()); |
| 188 | } |
| 189 | } |
| 190 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 191 | std::string CommonRuntimeTest::GetCoreArtLocation() { |
| 192 | return GetCoreFileLocation("art"); |
| 193 | } |
| 194 | |
| 195 | std::string CommonRuntimeTest::GetCoreOatLocation() { |
| 196 | return GetCoreFileLocation("oat"); |
| 197 | } |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 198 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 199 | std::unique_ptr<const DexFile> CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) { |
| 200 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 201 | std::string error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 202 | MemMap::Init(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 203 | if (!DexFile::Open(location, location, &error_msg, &dex_files)) { |
| 204 | LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n"; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 205 | UNREACHABLE(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 206 | } else { |
| 207 | CHECK_EQ(1U, dex_files.size()) << "Expected only one dex file in " << location; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 208 | return std::move(dex_files[0]); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | void CommonRuntimeTest::SetUp() { |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 213 | SetUpAndroidRoot(); |
| 214 | SetUpAndroidData(android_data_); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 215 | dalvik_cache_.append(android_data_.c_str()); |
| 216 | dalvik_cache_.append("/dalvik-cache"); |
| 217 | int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700); |
| 218 | ASSERT_EQ(mkdir_result, 0); |
| 219 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 220 | std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB)); |
| 221 | std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB)); |
| 222 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 223 | |
| 224 | RuntimeOptions options; |
Richard Uhler | c275259 | 2015-01-02 13:28:22 -0800 | [diff] [blame] | 225 | std::string boot_class_path_string = "-Xbootclasspath:" + GetLibCoreDexFileName(); |
| 226 | options.push_back(std::make_pair(boot_class_path_string, nullptr)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 227 | options.push_back(std::make_pair("-Xcheck:jni", nullptr)); |
Richard Uhler | c275259 | 2015-01-02 13:28:22 -0800 | [diff] [blame] | 228 | options.push_back(std::make_pair(min_heap_string, nullptr)); |
| 229 | options.push_back(std::make_pair(max_heap_string, nullptr)); |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 230 | |
| 231 | callbacks_.reset(new NoopCompilerCallbacks()); |
| 232 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 233 | SetUpRuntimeOptions(&options); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 234 | |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 235 | // Install compiler-callbacks if SetupRuntimeOptions hasn't deleted them. |
| 236 | if (callbacks_.get() != nullptr) { |
| 237 | options.push_back(std::make_pair("compilercallbacks", callbacks_.get())); |
| 238 | } |
| 239 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 240 | PreRuntimeCreate(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 241 | if (!Runtime::Create(options, false)) { |
| 242 | LOG(FATAL) << "Failed to create runtime"; |
| 243 | return; |
| 244 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 245 | PostRuntimeCreate(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 246 | runtime_.reset(Runtime::Current()); |
| 247 | class_linker_ = runtime_->GetClassLinker(); |
| 248 | class_linker_->FixupDexCaches(runtime_->GetResolutionMethod()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 249 | |
| 250 | // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this |
| 251 | // set up. |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 252 | if (!unstarted_initialized_) { |
| 253 | interpreter::UnstartedRuntimeInitialize(); |
| 254 | unstarted_initialized_ = true; |
| 255 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 256 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 257 | class_linker_->RunRootClinits(); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 258 | boot_class_path_ = class_linker_->GetBootClassPath(); |
| 259 | java_lang_dex_file_ = boot_class_path_[0]; |
| 260 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 261 | |
| 262 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we |
| 263 | // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess. |
| 264 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 265 | |
| 266 | // We're back in native, take the opportunity to initialize well known classes. |
| 267 | WellKnownClasses::Init(Thread::Current()->GetJniEnv()); |
| 268 | |
| 269 | // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread |
| 270 | // pool is created by the runtime. |
| 271 | runtime_->GetHeap()->CreateThreadPool(); |
| 272 | runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 273 | // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject). |
| 274 | runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U); |
Richard Uhler | c275259 | 2015-01-02 13:28:22 -0800 | [diff] [blame] | 275 | |
| 276 | // Get the boot class path from the runtime so it can be used in tests. |
| 277 | boot_class_path_ = class_linker_->GetBootClassPath(); |
| 278 | ASSERT_FALSE(boot_class_path_.empty()); |
| 279 | java_lang_dex_file_ = boot_class_path_[0]; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 282 | void CommonRuntimeTest::ClearDirectory(const char* dirpath) { |
| 283 | ASSERT_TRUE(dirpath != nullptr); |
| 284 | DIR* dir = opendir(dirpath); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 285 | ASSERT_TRUE(dir != nullptr); |
| 286 | dirent* e; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 287 | struct stat s; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 288 | while ((e = readdir(dir)) != nullptr) { |
| 289 | if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) { |
| 290 | continue; |
| 291 | } |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 292 | std::string filename(dirpath); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 293 | filename.push_back('/'); |
| 294 | filename.append(e->d_name); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 295 | int stat_result = lstat(filename.c_str(), &s); |
| 296 | ASSERT_EQ(0, stat_result) << "unable to stat " << filename; |
| 297 | if (S_ISDIR(s.st_mode)) { |
| 298 | ClearDirectory(filename.c_str()); |
| 299 | int rmdir_result = rmdir(filename.c_str()); |
| 300 | ASSERT_EQ(0, rmdir_result) << filename; |
| 301 | } else { |
| 302 | int unlink_result = unlink(filename.c_str()); |
| 303 | ASSERT_EQ(0, unlink_result) << filename; |
| 304 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 305 | } |
| 306 | closedir(dir); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void CommonRuntimeTest::TearDown() { |
| 310 | const char* android_data = getenv("ANDROID_DATA"); |
| 311 | ASSERT_TRUE(android_data != nullptr); |
| 312 | ClearDirectory(dalvik_cache_.c_str()); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 313 | int rmdir_cache_result = rmdir(dalvik_cache_.c_str()); |
| 314 | ASSERT_EQ(0, rmdir_cache_result); |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 315 | TearDownAndroidData(android_data_, true); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 316 | |
| 317 | // icu4c has a fixed 10-element array "gCommonICUDataArray". |
| 318 | // If we run > 10 tests, we fill that array and u_setCommonData fails. |
| 319 | // There's a function to clear the array, but it's not public... |
| 320 | typedef void (*IcuCleanupFn)(); |
| 321 | void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT); |
| 322 | CHECK(sym != nullptr) << dlerror(); |
| 323 | IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym); |
| 324 | (*icu_cleanup_fn)(); |
| 325 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 326 | Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test |
| 327 | } |
| 328 | |
| 329 | std::string CommonRuntimeTest::GetLibCoreDexFileName() { |
| 330 | return GetDexFileName("core-libart"); |
| 331 | } |
| 332 | |
| 333 | std::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) { |
| 334 | if (IsHost()) { |
| 335 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 336 | CHECK(host_dir != nullptr); |
| 337 | return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str()); |
| 338 | } |
| 339 | return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str()); |
| 340 | } |
| 341 | |
| 342 | std::string CommonRuntimeTest::GetTestAndroidRoot() { |
| 343 | if (IsHost()) { |
| 344 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 345 | CHECK(host_dir != nullptr); |
| 346 | return host_dir; |
| 347 | } |
| 348 | return GetAndroidRoot(); |
| 349 | } |
| 350 | |
Andreas Gampe | 1fe5e5c | 2014-07-11 21:14:35 -0700 | [diff] [blame] | 351 | // Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. |
| 352 | #ifdef ART_TARGET |
| 353 | #ifndef ART_TARGET_NATIVETEST_DIR |
| 354 | #error "ART_TARGET_NATIVETEST_DIR not set." |
| 355 | #endif |
| 356 | // Wrap it as a string literal. |
| 357 | #define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" |
| 358 | #else |
| 359 | #define ART_TARGET_NATIVETEST_DIR_STRING "" |
| 360 | #endif |
| 361 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 362 | std::string CommonRuntimeTest::GetTestDexFileName(const char* name) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 363 | CHECK(name != nullptr); |
| 364 | std::string filename; |
| 365 | if (IsHost()) { |
| 366 | filename += getenv("ANDROID_HOST_OUT"); |
| 367 | filename += "/framework/"; |
| 368 | } else { |
Andreas Gampe | 1fe5e5c | 2014-07-11 21:14:35 -0700 | [diff] [blame] | 369 | filename += ART_TARGET_NATIVETEST_DIR_STRING; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 370 | } |
| 371 | filename += "art-gtest-"; |
| 372 | filename += name; |
| 373 | filename += ".jar"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 374 | return filename; |
| 375 | } |
| 376 | |
| 377 | std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) { |
| 378 | std::string filename = GetTestDexFileName(name); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 379 | std::string error_msg; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 380 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 381 | bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files); |
| 382 | CHECK(success) << "Failed to open '" << filename << "': " << error_msg; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 383 | for (auto& dex_file : dex_files) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 384 | CHECK_EQ(PROT_READ, dex_file->GetPermissions()); |
| 385 | CHECK(dex_file->IsReadOnly()); |
| 386 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 387 | return dex_files; |
| 388 | } |
| 389 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 390 | std::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) { |
| 391 | std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 392 | EXPECT_EQ(1U, vector.size()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 393 | return std::move(vector[0]); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 396 | std::vector<const DexFile*> CommonRuntimeTest::GetDexFiles(jobject jclass_loader) { |
| 397 | std::vector<const DexFile*> ret; |
| 398 | |
| 399 | ScopedObjectAccess soa(Thread::Current()); |
| 400 | |
| 401 | StackHandleScope<4> hs(Thread::Current()); |
| 402 | Handle<mirror::ClassLoader> class_loader = hs.NewHandle( |
| 403 | soa.Decode<mirror::ClassLoader*>(jclass_loader)); |
| 404 | |
| 405 | DCHECK_EQ(class_loader->GetClass(), |
| 406 | soa.Decode<mirror::Class*>(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 407 | DCHECK_EQ(class_loader->GetParent()->GetClass(), |
| 408 | soa.Decode<mirror::Class*>(WellKnownClasses::java_lang_BootClassLoader)); |
| 409 | |
| 410 | // The class loader is a PathClassLoader which inherits from BaseDexClassLoader. |
| 411 | // We need to get the DexPathList and loop through it. |
| 412 | Handle<mirror::ArtField> cookie_field = |
| 413 | hs.NewHandle(soa.DecodeField(WellKnownClasses::dalvik_system_DexFile_cookie)); |
| 414 | Handle<mirror::ArtField> dex_file_field = |
| 415 | hs.NewHandle( |
| 416 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList__Element_dexFile)); |
| 417 | mirror::Object* dex_path_list = |
| 418 | soa.DecodeField(WellKnownClasses::dalvik_system_PathClassLoader_pathList)-> |
| 419 | GetObject(class_loader.Get()); |
| 420 | if (dex_path_list != nullptr && dex_file_field.Get() != nullptr && |
| 421 | cookie_field.Get() != nullptr) { |
| 422 | // DexPathList has an array dexElements of Elements[] which each contain a dex file. |
| 423 | mirror::Object* dex_elements_obj = |
| 424 | soa.DecodeField(WellKnownClasses::dalvik_system_DexPathList_dexElements)-> |
| 425 | GetObject(dex_path_list); |
| 426 | // Loop through each dalvik.system.DexPathList$Element's dalvik.system.DexFile and look |
| 427 | // at the mCookie which is a DexFile vector. |
| 428 | if (dex_elements_obj != nullptr) { |
| 429 | Handle<mirror::ObjectArray<mirror::Object>> dex_elements = |
| 430 | hs.NewHandle(dex_elements_obj->AsObjectArray<mirror::Object>()); |
| 431 | for (int32_t i = 0; i < dex_elements->GetLength(); ++i) { |
| 432 | mirror::Object* element = dex_elements->GetWithoutChecks(i); |
| 433 | if (element == nullptr) { |
| 434 | // Should never happen, fall back to java code to throw a NPE. |
| 435 | break; |
| 436 | } |
| 437 | mirror::Object* dex_file = dex_file_field->GetObject(element); |
| 438 | if (dex_file != nullptr) { |
| 439 | mirror::LongArray* long_array = cookie_field->GetObject(dex_file)->AsLongArray(); |
| 440 | DCHECK(long_array != nullptr); |
| 441 | int32_t long_array_size = long_array->GetLength(); |
| 442 | for (int32_t j = 0; j < long_array_size; ++j) { |
| 443 | const DexFile* cp_dex_file = reinterpret_cast<const DexFile*>(static_cast<uintptr_t>( |
| 444 | long_array->GetWithoutChecks(j))); |
| 445 | if (cp_dex_file == nullptr) { |
| 446 | LOG(WARNING) << "Null DexFile"; |
| 447 | continue; |
| 448 | } |
| 449 | ret.push_back(cp_dex_file); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | return ret; |
| 457 | } |
| 458 | |
| 459 | const DexFile* CommonRuntimeTest::GetFirstDexFile(jobject jclass_loader) { |
| 460 | std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader)); |
| 461 | DCHECK(!tmp.empty()); |
| 462 | const DexFile* ret = tmp[0]; |
| 463 | DCHECK(ret != nullptr); |
| 464 | return ret; |
| 465 | } |
| 466 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 467 | jobject CommonRuntimeTest::LoadDex(const char* dex_name) { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 468 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name); |
| 469 | std::vector<const DexFile*> class_path; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 470 | CHECK_NE(0U, dex_files.size()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 471 | for (auto& dex_file : dex_files) { |
| 472 | class_path.push_back(dex_file.get()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 473 | loaded_dex_files_.push_back(std::move(dex_file)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 474 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 475 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 476 | Thread* self = Thread::Current(); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 477 | jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self, class_path); |
| 478 | self->SetClassLoaderOverride(class_loader); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 479 | return class_loader; |
| 480 | } |
| 481 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 482 | std::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) { |
| 483 | CHECK(suffix != nullptr); |
| 484 | |
| 485 | std::string location; |
| 486 | if (IsHost()) { |
| 487 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 488 | CHECK(host_dir != NULL); |
| 489 | location = StringPrintf("%s/framework/core.%s", host_dir, suffix); |
| 490 | } else { |
| 491 | location = StringPrintf("/data/art-test/core.%s", suffix); |
| 492 | } |
| 493 | |
| 494 | return location; |
| 495 | } |
| 496 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 497 | CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 498 | vm_->SetCheckJniAbortHook(Hook, &actual_); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | CheckJniAbortCatcher::~CheckJniAbortCatcher() { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 502 | vm_->SetCheckJniAbortHook(nullptr, nullptr); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 503 | EXPECT_TRUE(actual_.empty()) << actual_; |
| 504 | } |
| 505 | |
| 506 | void CheckJniAbortCatcher::Check(const char* expected_text) { |
| 507 | EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n" |
| 508 | << "Expected to find: " << expected_text << "\n" |
| 509 | << "In the output : " << actual_; |
| 510 | actual_.clear(); |
| 511 | } |
| 512 | |
| 513 | void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) { |
| 514 | // We use += because when we're hooking the aborts like this, multiple problems can be found. |
| 515 | *reinterpret_cast<std::string*>(data) += reason; |
| 516 | } |
| 517 | |
| 518 | } // namespace art |
| 519 | |
| 520 | namespace std { |
| 521 | |
| 522 | template <typename T> |
| 523 | std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) { |
| 524 | os << ::art::ToString(rhs); |
| 525 | return os; |
| 526 | } |
| 527 | |
| 528 | } // namespace std |