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 | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame^] | 37 | #include "interpreter/unstarted_runtime.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 38 | #include "jni_internal.h" |
| 39 | #include "mirror/class_loader.h" |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 40 | #include "mem_map.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 41 | #include "noop_compiler_callbacks.h" |
| 42 | #include "os.h" |
| 43 | #include "runtime-inl.h" |
| 44 | #include "scoped_thread_state_change.h" |
| 45 | #include "thread.h" |
| 46 | #include "well_known_classes.h" |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 47 | |
| 48 | int main(int argc, char **argv) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 49 | // Gtests can be very noisy. For example, an executable with multiple tests will trigger native |
| 50 | // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses |
| 51 | // 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] | 52 | setenv("ANDROID_LOG_TAGS", "*:e", 1); |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 53 | |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 54 | art::InitLogging(argv); |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 55 | LOG(::art::INFO) << "Running main() from common_runtime_test.cc..."; |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 56 | testing::InitGoogleTest(&argc, argv); |
| 57 | return RUN_ALL_TESTS(); |
| 58 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 59 | |
| 60 | namespace art { |
| 61 | |
| 62 | ScratchFile::ScratchFile() { |
| 63 | // ANDROID_DATA needs to be set |
| 64 | CHECK_NE(static_cast<char*>(nullptr), getenv("ANDROID_DATA")) << |
| 65 | "Are you subclassing RuntimeTest?"; |
| 66 | filename_ = getenv("ANDROID_DATA"); |
| 67 | filename_ += "/TmpFile-XXXXXX"; |
| 68 | int fd = mkstemp(&filename_[0]); |
| 69 | CHECK_NE(-1, fd); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 70 | file_.reset(new File(fd, GetFilename(), true)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | ScratchFile::ScratchFile(const ScratchFile& other, const char* suffix) { |
| 74 | filename_ = other.GetFilename(); |
| 75 | filename_ += suffix; |
| 76 | int fd = open(filename_.c_str(), O_RDWR | O_CREAT, 0666); |
| 77 | CHECK_NE(-1, fd); |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 78 | file_.reset(new File(fd, GetFilename(), true)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | ScratchFile::ScratchFile(File* file) { |
| 82 | CHECK(file != NULL); |
| 83 | filename_ = file->GetPath(); |
| 84 | file_.reset(file); |
| 85 | } |
| 86 | |
| 87 | ScratchFile::~ScratchFile() { |
| 88 | Unlink(); |
| 89 | } |
| 90 | |
| 91 | int ScratchFile::GetFd() const { |
| 92 | return file_->Fd(); |
| 93 | } |
| 94 | |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 95 | void ScratchFile::Close() { |
Andreas Gampe | 4303ba9 | 2014-11-06 01:00:46 -0800 | [diff] [blame] | 96 | if (file_.get() != nullptr) { |
| 97 | if (file_->FlushCloseOrErase() != 0) { |
| 98 | PLOG(WARNING) << "Error closing scratch file."; |
| 99 | } |
| 100 | } |
Andreas Gampe | e21dc3d | 2014-12-08 16:59:43 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void ScratchFile::Unlink() { |
| 104 | if (!OS::FileExists(filename_.c_str())) { |
| 105 | return; |
| 106 | } |
| 107 | Close(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 108 | int unlink_result = unlink(filename_.c_str()); |
| 109 | CHECK_EQ(0, unlink_result); |
| 110 | } |
| 111 | |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame^] | 112 | static bool unstarted_initialized_ = false; |
| 113 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 114 | CommonRuntimeTest::CommonRuntimeTest() {} |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 115 | CommonRuntimeTest::~CommonRuntimeTest() { |
| 116 | // Ensure the dex files are cleaned up before the runtime. |
| 117 | loaded_dex_files_.clear(); |
| 118 | runtime_.reset(); |
| 119 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 120 | |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 121 | void CommonRuntimeTest::SetUpAndroidRoot() { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 122 | if (IsHost()) { |
| 123 | // $ANDROID_ROOT is set on the device, but not necessarily on the host. |
| 124 | // But it needs to be set so that icu4c can find its locale data. |
| 125 | const char* android_root_from_env = getenv("ANDROID_ROOT"); |
| 126 | if (android_root_from_env == nullptr) { |
| 127 | // Use ANDROID_HOST_OUT for ANDROID_ROOT if it is set. |
| 128 | const char* android_host_out = getenv("ANDROID_HOST_OUT"); |
| 129 | if (android_host_out != nullptr) { |
| 130 | setenv("ANDROID_ROOT", android_host_out, 1); |
| 131 | } else { |
| 132 | // Build it from ANDROID_BUILD_TOP or cwd |
| 133 | std::string root; |
| 134 | const char* android_build_top = getenv("ANDROID_BUILD_TOP"); |
| 135 | if (android_build_top != nullptr) { |
| 136 | root += android_build_top; |
| 137 | } else { |
| 138 | // Not set by build server, so default to current directory |
| 139 | char* cwd = getcwd(nullptr, 0); |
| 140 | setenv("ANDROID_BUILD_TOP", cwd, 1); |
| 141 | root += cwd; |
| 142 | free(cwd); |
| 143 | } |
| 144 | #if defined(__linux__) |
| 145 | root += "/out/host/linux-x86"; |
| 146 | #elif defined(__APPLE__) |
| 147 | root += "/out/host/darwin-x86"; |
| 148 | #else |
| 149 | #error unsupported OS |
| 150 | #endif |
| 151 | setenv("ANDROID_ROOT", root.c_str(), 1); |
| 152 | } |
| 153 | } |
| 154 | setenv("LD_LIBRARY_PATH", ":", 0); // Required by java.lang.System.<clinit>. |
| 155 | |
| 156 | // Not set by build server, so default |
| 157 | if (getenv("ANDROID_HOST_OUT") == nullptr) { |
| 158 | setenv("ANDROID_HOST_OUT", getenv("ANDROID_ROOT"), 1); |
| 159 | } |
| 160 | } |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 161 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 162 | |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 163 | void CommonRuntimeTest::SetUpAndroidData(std::string& android_data) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 164 | // 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] | 165 | if (IsHost()) { |
| 166 | const char* tmpdir = getenv("TMPDIR"); |
| 167 | if (tmpdir != nullptr && tmpdir[0] != 0) { |
| 168 | android_data = tmpdir; |
| 169 | } else { |
| 170 | android_data = "/tmp"; |
| 171 | } |
| 172 | } else { |
| 173 | android_data = "/data/dalvik-cache"; |
| 174 | } |
| 175 | android_data += "/art-data-XXXXXX"; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 176 | if (mkdtemp(&android_data[0]) == nullptr) { |
| 177 | PLOG(FATAL) << "mkdtemp(\"" << &android_data[0] << "\") failed"; |
| 178 | } |
| 179 | setenv("ANDROID_DATA", android_data.c_str(), 1); |
| 180 | } |
| 181 | |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 182 | void CommonRuntimeTest::TearDownAndroidData(const std::string& android_data, bool fail_on_error) { |
| 183 | if (fail_on_error) { |
| 184 | ASSERT_EQ(rmdir(android_data.c_str()), 0); |
| 185 | } else { |
| 186 | rmdir(android_data.c_str()); |
| 187 | } |
| 188 | } |
| 189 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 190 | std::string CommonRuntimeTest::GetCoreArtLocation() { |
| 191 | return GetCoreFileLocation("art"); |
| 192 | } |
| 193 | |
| 194 | std::string CommonRuntimeTest::GetCoreOatLocation() { |
| 195 | return GetCoreFileLocation("oat"); |
| 196 | } |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 197 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 198 | std::unique_ptr<const DexFile> CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) { |
| 199 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 200 | std::string error_msg; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 201 | MemMap::Init(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 202 | if (!DexFile::Open(location, location, &error_msg, &dex_files)) { |
| 203 | LOG(FATAL) << "Could not open .dex file '" << location << "': " << error_msg << "\n"; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 204 | UNREACHABLE(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 205 | } else { |
| 206 | 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] | 207 | return std::move(dex_files[0]); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
| 211 | void CommonRuntimeTest::SetUp() { |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 212 | SetUpAndroidRoot(); |
| 213 | SetUpAndroidData(android_data_); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 214 | dalvik_cache_.append(android_data_.c_str()); |
| 215 | dalvik_cache_.append("/dalvik-cache"); |
| 216 | int mkdir_result = mkdir(dalvik_cache_.c_str(), 0700); |
| 217 | ASSERT_EQ(mkdir_result, 0); |
| 218 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 219 | std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB)); |
| 220 | std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB)); |
| 221 | |
| 222 | callbacks_.reset(new NoopCompilerCallbacks()); |
| 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)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 230 | options.push_back(std::make_pair("compilercallbacks", callbacks_.get())); |
| 231 | SetUpRuntimeOptions(&options); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 232 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 233 | PreRuntimeCreate(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 234 | if (!Runtime::Create(options, false)) { |
| 235 | LOG(FATAL) << "Failed to create runtime"; |
| 236 | return; |
| 237 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 238 | PostRuntimeCreate(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 239 | runtime_.reset(Runtime::Current()); |
| 240 | class_linker_ = runtime_->GetClassLinker(); |
| 241 | class_linker_->FixupDexCaches(runtime_->GetResolutionMethod()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 242 | |
| 243 | // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this |
| 244 | // set up. |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame^] | 245 | if (!unstarted_initialized_) { |
| 246 | interpreter::UnstartedRuntimeInitialize(); |
| 247 | unstarted_initialized_ = true; |
| 248 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 249 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 250 | class_linker_->RunRootClinits(); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 251 | boot_class_path_ = class_linker_->GetBootClassPath(); |
| 252 | java_lang_dex_file_ = boot_class_path_[0]; |
| 253 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 254 | |
| 255 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we |
| 256 | // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess. |
| 257 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 258 | |
| 259 | // We're back in native, take the opportunity to initialize well known classes. |
| 260 | WellKnownClasses::Init(Thread::Current()->GetJniEnv()); |
| 261 | |
| 262 | // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread |
| 263 | // pool is created by the runtime. |
| 264 | runtime_->GetHeap()->CreateThreadPool(); |
| 265 | runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test |
Richard Uhler | c275259 | 2015-01-02 13:28:22 -0800 | [diff] [blame] | 266 | |
| 267 | // Get the boot class path from the runtime so it can be used in tests. |
| 268 | boot_class_path_ = class_linker_->GetBootClassPath(); |
| 269 | ASSERT_FALSE(boot_class_path_.empty()); |
| 270 | java_lang_dex_file_ = boot_class_path_[0]; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 273 | void CommonRuntimeTest::ClearDirectory(const char* dirpath) { |
| 274 | ASSERT_TRUE(dirpath != nullptr); |
| 275 | DIR* dir = opendir(dirpath); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 276 | ASSERT_TRUE(dir != nullptr); |
| 277 | dirent* e; |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 278 | struct stat s; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 279 | while ((e = readdir(dir)) != nullptr) { |
| 280 | if ((strcmp(e->d_name, ".") == 0) || (strcmp(e->d_name, "..") == 0)) { |
| 281 | continue; |
| 282 | } |
Jeff Hao | f0a3f09 | 2014-07-24 16:26:09 -0700 | [diff] [blame] | 283 | std::string filename(dirpath); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 284 | filename.push_back('/'); |
| 285 | filename.append(e->d_name); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 286 | int stat_result = lstat(filename.c_str(), &s); |
| 287 | ASSERT_EQ(0, stat_result) << "unable to stat " << filename; |
| 288 | if (S_ISDIR(s.st_mode)) { |
| 289 | ClearDirectory(filename.c_str()); |
| 290 | int rmdir_result = rmdir(filename.c_str()); |
| 291 | ASSERT_EQ(0, rmdir_result) << filename; |
| 292 | } else { |
| 293 | int unlink_result = unlink(filename.c_str()); |
| 294 | ASSERT_EQ(0, unlink_result) << filename; |
| 295 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 296 | } |
| 297 | closedir(dir); |
Alex Light | a59dd80 | 2014-07-02 16:28:08 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void CommonRuntimeTest::TearDown() { |
| 301 | const char* android_data = getenv("ANDROID_DATA"); |
| 302 | ASSERT_TRUE(android_data != nullptr); |
| 303 | ClearDirectory(dalvik_cache_.c_str()); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 304 | int rmdir_cache_result = rmdir(dalvik_cache_.c_str()); |
| 305 | ASSERT_EQ(0, rmdir_cache_result); |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 306 | TearDownAndroidData(android_data_, true); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 307 | |
| 308 | // icu4c has a fixed 10-element array "gCommonICUDataArray". |
| 309 | // If we run > 10 tests, we fill that array and u_setCommonData fails. |
| 310 | // There's a function to clear the array, but it's not public... |
| 311 | typedef void (*IcuCleanupFn)(); |
| 312 | void* sym = dlsym(RTLD_DEFAULT, "u_cleanup_" U_ICU_VERSION_SHORT); |
| 313 | CHECK(sym != nullptr) << dlerror(); |
| 314 | IcuCleanupFn icu_cleanup_fn = reinterpret_cast<IcuCleanupFn>(sym); |
| 315 | (*icu_cleanup_fn)(); |
| 316 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 317 | Runtime::Current()->GetHeap()->VerifyHeap(); // Check for heap corruption after the test |
| 318 | } |
| 319 | |
| 320 | std::string CommonRuntimeTest::GetLibCoreDexFileName() { |
| 321 | return GetDexFileName("core-libart"); |
| 322 | } |
| 323 | |
| 324 | std::string CommonRuntimeTest::GetDexFileName(const std::string& jar_prefix) { |
| 325 | if (IsHost()) { |
| 326 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 327 | CHECK(host_dir != nullptr); |
| 328 | return StringPrintf("%s/framework/%s-hostdex.jar", host_dir, jar_prefix.c_str()); |
| 329 | } |
| 330 | return StringPrintf("%s/framework/%s.jar", GetAndroidRoot(), jar_prefix.c_str()); |
| 331 | } |
| 332 | |
| 333 | std::string CommonRuntimeTest::GetTestAndroidRoot() { |
| 334 | if (IsHost()) { |
| 335 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 336 | CHECK(host_dir != nullptr); |
| 337 | return host_dir; |
| 338 | } |
| 339 | return GetAndroidRoot(); |
| 340 | } |
| 341 | |
Andreas Gampe | 1fe5e5c | 2014-07-11 21:14:35 -0700 | [diff] [blame] | 342 | // Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. |
| 343 | #ifdef ART_TARGET |
| 344 | #ifndef ART_TARGET_NATIVETEST_DIR |
| 345 | #error "ART_TARGET_NATIVETEST_DIR not set." |
| 346 | #endif |
| 347 | // Wrap it as a string literal. |
| 348 | #define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" |
| 349 | #else |
| 350 | #define ART_TARGET_NATIVETEST_DIR_STRING "" |
| 351 | #endif |
| 352 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 353 | std::string CommonRuntimeTest::GetTestDexFileName(const char* name) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 354 | CHECK(name != nullptr); |
| 355 | std::string filename; |
| 356 | if (IsHost()) { |
| 357 | filename += getenv("ANDROID_HOST_OUT"); |
| 358 | filename += "/framework/"; |
| 359 | } else { |
Andreas Gampe | 1fe5e5c | 2014-07-11 21:14:35 -0700 | [diff] [blame] | 360 | filename += ART_TARGET_NATIVETEST_DIR_STRING; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 361 | } |
| 362 | filename += "art-gtest-"; |
| 363 | filename += name; |
| 364 | filename += ".jar"; |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 365 | return filename; |
| 366 | } |
| 367 | |
| 368 | std::vector<std::unique_ptr<const DexFile>> CommonRuntimeTest::OpenTestDexFiles(const char* name) { |
| 369 | std::string filename = GetTestDexFileName(name); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 370 | std::string error_msg; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 371 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 372 | bool success = DexFile::Open(filename.c_str(), filename.c_str(), &error_msg, &dex_files); |
| 373 | CHECK(success) << "Failed to open '" << filename << "': " << error_msg; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 374 | for (auto& dex_file : dex_files) { |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 375 | CHECK_EQ(PROT_READ, dex_file->GetPermissions()); |
| 376 | CHECK(dex_file->IsReadOnly()); |
| 377 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 378 | return dex_files; |
| 379 | } |
| 380 | |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 381 | std::unique_ptr<const DexFile> CommonRuntimeTest::OpenTestDexFile(const char* name) { |
| 382 | std::vector<std::unique_ptr<const DexFile>> vector = OpenTestDexFiles(name); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 383 | EXPECT_EQ(1U, vector.size()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 384 | return std::move(vector[0]); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | jobject CommonRuntimeTest::LoadDex(const char* dex_name) { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 388 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name); |
| 389 | std::vector<const DexFile*> class_path; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 390 | CHECK_NE(0U, dex_files.size()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 391 | for (auto& dex_file : dex_files) { |
| 392 | class_path.push_back(dex_file.get()); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 393 | class_linker_->RegisterDexFile(*dex_file); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 394 | loaded_dex_files_.push_back(std::move(dex_file)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 395 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 396 | Thread* self = Thread::Current(); |
| 397 | JNIEnvExt* env = self->GetJniEnv(); |
| 398 | ScopedLocalRef<jobject> class_loader_local(env, |
| 399 | env->AllocObject(WellKnownClasses::dalvik_system_PathClassLoader)); |
| 400 | jobject class_loader = env->NewGlobalRef(class_loader_local.get()); |
| 401 | self->SetClassLoaderOverride(class_loader_local.get()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 402 | Runtime::Current()->SetCompileTimeClassPath(class_loader, class_path); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 403 | return class_loader; |
| 404 | } |
| 405 | |
Igor Murashkin | 3774335 | 2014-11-13 14:38:00 -0800 | [diff] [blame] | 406 | std::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) { |
| 407 | CHECK(suffix != nullptr); |
| 408 | |
| 409 | std::string location; |
| 410 | if (IsHost()) { |
| 411 | const char* host_dir = getenv("ANDROID_HOST_OUT"); |
| 412 | CHECK(host_dir != NULL); |
| 413 | location = StringPrintf("%s/framework/core.%s", host_dir, suffix); |
| 414 | } else { |
| 415 | location = StringPrintf("/data/art-test/core.%s", suffix); |
| 416 | } |
| 417 | |
| 418 | return location; |
| 419 | } |
| 420 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 421 | CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 422 | vm_->SetCheckJniAbortHook(Hook, &actual_); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | CheckJniAbortCatcher::~CheckJniAbortCatcher() { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 426 | vm_->SetCheckJniAbortHook(nullptr, nullptr); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 427 | EXPECT_TRUE(actual_.empty()) << actual_; |
| 428 | } |
| 429 | |
| 430 | void CheckJniAbortCatcher::Check(const char* expected_text) { |
| 431 | EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n" |
| 432 | << "Expected to find: " << expected_text << "\n" |
| 433 | << "In the output : " << actual_; |
| 434 | actual_.clear(); |
| 435 | } |
| 436 | |
| 437 | void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) { |
| 438 | // We use += because when we're hooking the aborts like this, multiple problems can be found. |
| 439 | *reinterpret_cast<std::string*>(data) += reason; |
| 440 | } |
| 441 | |
| 442 | } // namespace art |
| 443 | |
| 444 | namespace std { |
| 445 | |
| 446 | template <typename T> |
| 447 | std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs) { |
| 448 | os << ::art::ToString(rhs); |
| 449 | return os; |
| 450 | } |
| 451 | |
| 452 | } // namespace std |