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