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