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