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> |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 22 | #include <stdlib.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 23 | #include <cstdio> |
Andreas Gampe | 373a9b5 | 2017-10-18 09:01:57 -0700 | [diff] [blame] | 24 | #include "nativehelper/scoped_local_ref.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 25 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 26 | #include "android-base/stringprintf.h" |
| 27 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 28 | #include "art_field-inl.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 29 | #include "base/file_utils.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 30 | #include "base/logging.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 31 | #include "base/macros.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 32 | #include "base/mem_map.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 33 | #include "base/mutex.h" |
| 34 | #include "base/os.h" |
Andreas Gampe | dcc528d | 2017-12-07 13:37:10 -0800 | [diff] [blame] | 35 | #include "base/runtime_debug.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 36 | #include "base/stl_util.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 37 | #include "base/unix_file/fd_file.h" |
| 38 | #include "class_linker.h" |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 39 | #include "class_loader_utils.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 40 | #include "compiler_callbacks.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 41 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 42 | #include "dex/dex_file-inl.h" |
| 43 | #include "dex/dex_file_loader.h" |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 44 | #include "dex/method_reference.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 45 | #include "dex/primitive.h" |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 46 | #include "dex/type_reference.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 47 | #include "gc/heap.h" |
Vladimir Marko | ec91d48 | 2018-11-29 12:17:11 +0000 | [diff] [blame] | 48 | #include "gc/space/image_space.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 49 | #include "gc_root-inl.h" |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 50 | #include "gtest/gtest.h" |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 51 | #include "handle_scope-inl.h" |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 52 | #include "interpreter/unstarted_runtime.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 53 | #include "jni/java_vm_ext.h" |
| 54 | #include "jni/jni_internal.h" |
Andreas Gampe | 70f5fd0 | 2018-10-24 19:58:37 -0700 | [diff] [blame] | 55 | #include "mirror/class-alloc-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 56 | #include "mirror/class-inl.h" |
Vladimir Marko | bdc93b4 | 2019-03-29 16:12:04 +0000 | [diff] [blame] | 57 | #include "mirror/class_loader-inl.h" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 58 | #include "mirror/object_array-alloc-inl.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 59 | #include "native/dalvik_system_DexFile.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 60 | #include "noop_compiler_callbacks.h" |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 61 | #include "profile/profile_compilation_info.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 62 | #include "runtime-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 63 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 64 | #include "thread.h" |
| 65 | #include "well_known_classes.h" |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 66 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 67 | namespace art { |
| 68 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 69 | using android::base::StringPrintf; |
| 70 | |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 71 | static bool unstarted_initialized_ = false; |
| 72 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 73 | CommonRuntimeTestImpl::CommonRuntimeTestImpl() |
Vladimir Marko | 483c41a | 2021-11-12 12:45:23 +0000 | [diff] [blame] | 74 | : class_linker_(nullptr), |
| 75 | java_lang_dex_file_(nullptr), |
| 76 | boot_class_path_(), |
| 77 | callbacks_(), |
| 78 | use_boot_image_(false) { |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 79 | } |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 80 | |
| 81 | CommonRuntimeTestImpl::~CommonRuntimeTestImpl() { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 82 | // Ensure the dex files are cleaned up before the runtime. |
| 83 | loaded_dex_files_.clear(); |
| 84 | runtime_.reset(); |
| 85 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 86 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 87 | void CommonRuntimeTestImpl::SetUp() { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 88 | CommonArtTestImpl::SetUp(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 89 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 90 | std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB)); |
| 91 | std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB)); |
| 92 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 93 | RuntimeOptions options; |
Vladimir Marko | ec91d48 | 2018-11-29 12:17:11 +0000 | [diff] [blame] | 94 | std::string boot_class_path_string = |
| 95 | GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()); |
| 96 | std::string boot_class_path_locations_string = |
| 97 | GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()); |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 98 | |
Richard Uhler | c275259 | 2015-01-02 13:28:22 -0800 | [diff] [blame] | 99 | options.push_back(std::make_pair(boot_class_path_string, nullptr)); |
Vladimir Marko | ec91d48 | 2018-11-29 12:17:11 +0000 | [diff] [blame] | 100 | options.push_back(std::make_pair(boot_class_path_locations_string, nullptr)); |
Vladimir Marko | 483c41a | 2021-11-12 12:45:23 +0000 | [diff] [blame] | 101 | if (use_boot_image_) { |
| 102 | options.emplace_back("-Ximage:" + GetImageLocation(), nullptr); |
| 103 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 104 | options.push_back(std::make_pair("-Xcheck:jni", nullptr)); |
Richard Uhler | c275259 | 2015-01-02 13:28:22 -0800 | [diff] [blame] | 105 | options.push_back(std::make_pair(min_heap_string, nullptr)); |
| 106 | options.push_back(std::make_pair(max_heap_string, nullptr)); |
Andreas Gampe | 591b1d2 | 2019-06-25 13:08:16 -0700 | [diff] [blame] | 107 | |
| 108 | // Technically this is redundant w/ common_art_test, but still check. |
Andreas Gampe | 1c5b42f | 2017-06-15 18:20:45 -0700 | [diff] [blame] | 109 | options.push_back(std::make_pair("-XX:SlowDebug=true", nullptr)); |
Andreas Gampe | 46c4c85 | 2017-06-21 19:49:08 -0700 | [diff] [blame] | 110 | static bool gSlowDebugTestFlag = false; |
| 111 | RegisterRuntimeDebugFlag(&gSlowDebugTestFlag); |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 112 | |
| 113 | callbacks_.reset(new NoopCompilerCallbacks()); |
| 114 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 115 | SetUpRuntimeOptions(&options); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 116 | |
Vladimir Marko | 483c41a | 2021-11-12 12:45:23 +0000 | [diff] [blame] | 117 | // Install compiler-callbacks if SetUpRuntimeOptions hasn't deleted them. |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 118 | if (callbacks_.get() != nullptr) { |
| 119 | options.push_back(std::make_pair("compilercallbacks", callbacks_.get())); |
| 120 | } |
| 121 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 122 | PreRuntimeCreate(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 123 | if (!Runtime::Create(options, false)) { |
| 124 | LOG(FATAL) << "Failed to create runtime"; |
Elliott Hughes | c1896c9 | 2018-11-29 11:33:18 -0800 | [diff] [blame] | 125 | UNREACHABLE(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 126 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 127 | PostRuntimeCreate(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 128 | runtime_.reset(Runtime::Current()); |
| 129 | class_linker_ = runtime_->GetClassLinker(); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 130 | |
Andreas Gampe | a00f012 | 2015-12-16 16:54:35 -0800 | [diff] [blame] | 131 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we |
| 132 | // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess. |
Vladimir Marko | ddf4fd3 | 2021-11-22 16:31:57 +0000 | [diff] [blame] | 133 | Thread::Current()->TransitionFromRunnableToSuspended(ThreadState::kNative); |
Andreas Gampe | a00f012 | 2015-12-16 16:54:35 -0800 | [diff] [blame] | 134 | |
| 135 | // Get the boot class path from the runtime so it can be used in tests. |
| 136 | boot_class_path_ = class_linker_->GetBootClassPath(); |
| 137 | ASSERT_FALSE(boot_class_path_.empty()); |
| 138 | java_lang_dex_file_ = boot_class_path_[0]; |
| 139 | |
| 140 | FinalizeSetup(); |
Andreas Gampe | 46c4c85 | 2017-06-21 19:49:08 -0700 | [diff] [blame] | 141 | |
Roland Levillain | f040914 | 2021-03-22 15:45:03 +0000 | [diff] [blame] | 142 | if (kIsDebugBuild) { |
| 143 | // Ensure that we're really running with debug checks enabled. |
| 144 | CHECK(gSlowDebugTestFlag); |
| 145 | } |
Andreas Gampe | a00f012 | 2015-12-16 16:54:35 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 148 | void CommonRuntimeTestImpl::FinalizeSetup() { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 149 | // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this |
| 150 | // set up. |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 151 | if (!unstarted_initialized_) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 152 | interpreter::UnstartedRuntime::Initialize(); |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 153 | unstarted_initialized_ = true; |
Vladimir Marko | 8388148 | 2021-01-07 10:59:54 +0000 | [diff] [blame] | 154 | } else { |
| 155 | interpreter::UnstartedRuntime::Reinitialize(); |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 156 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 157 | |
Andreas Gampe | a00f012 | 2015-12-16 16:54:35 -0800 | [diff] [blame] | 158 | { |
| 159 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 160 | runtime_->RunRootClinits(soa.Self()); |
Andreas Gampe | a00f012 | 2015-12-16 16:54:35 -0800 | [diff] [blame] | 161 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 162 | |
| 163 | // We're back in native, take the opportunity to initialize well known classes. |
| 164 | WellKnownClasses::Init(Thread::Current()->GetJniEnv()); |
| 165 | |
| 166 | // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread |
| 167 | // pool is created by the runtime. |
| 168 | runtime_->GetHeap()->CreateThreadPool(); |
| 169 | runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 170 | // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject). |
| 171 | runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 174 | void CommonRuntimeTestImpl::TearDown() { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 175 | CommonArtTestImpl::TearDown(); |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 176 | if (runtime_ != nullptr) { |
| 177 | runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption after the test |
| 178 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Andreas Gampe | 1fe5e5c | 2014-07-11 21:14:35 -0700 | [diff] [blame] | 181 | // Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. |
| 182 | #ifdef ART_TARGET |
| 183 | #ifndef ART_TARGET_NATIVETEST_DIR |
| 184 | #error "ART_TARGET_NATIVETEST_DIR not set." |
| 185 | #endif |
| 186 | // Wrap it as a string literal. |
| 187 | #define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" |
| 188 | #else |
| 189 | #define ART_TARGET_NATIVETEST_DIR_STRING "" |
| 190 | #endif |
| 191 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 192 | std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(jobject jclass_loader) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 193 | ScopedObjectAccess soa(Thread::Current()); |
| 194 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 195 | StackHandleScope<1> hs(soa.Self()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 196 | Handle<mirror::ClassLoader> class_loader = hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 197 | soa.Decode<mirror::ClassLoader>(jclass_loader)); |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 198 | return GetDexFiles(soa, class_loader); |
| 199 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 200 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 201 | std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles( |
| 202 | ScopedObjectAccess& soa, |
| 203 | Handle<mirror::ClassLoader> class_loader) { |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 204 | DCHECK( |
| 205 | (class_loader->GetClass() == |
| 206 | soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)) || |
| 207 | (class_loader->GetClass() == |
| 208 | soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DelegateLastClassLoader))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 209 | |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 210 | std::vector<const DexFile*> ret; |
| 211 | VisitClassLoaderDexFiles(soa, |
| 212 | class_loader, |
| 213 | [&](const DexFile* cp_dex_file) { |
| 214 | if (cp_dex_file == nullptr) { |
| 215 | LOG(WARNING) << "Null DexFile"; |
| 216 | } else { |
| 217 | ret.push_back(cp_dex_file); |
| 218 | } |
| 219 | return true; |
| 220 | }); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 221 | return ret; |
| 222 | } |
| 223 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 224 | const DexFile* CommonRuntimeTestImpl::GetFirstDexFile(jobject jclass_loader) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 225 | std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader)); |
| 226 | DCHECK(!tmp.empty()); |
| 227 | const DexFile* ret = tmp[0]; |
| 228 | DCHECK(ret != nullptr); |
| 229 | return ret; |
| 230 | } |
| 231 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 232 | jobject CommonRuntimeTestImpl::LoadMultiDex(const char* first_dex_name, |
| 233 | const char* second_dex_name) { |
| 234 | std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles(first_dex_name); |
| 235 | std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles(second_dex_name); |
| 236 | std::vector<const DexFile*> class_path; |
| 237 | CHECK_NE(0U, first_dex_files.size()); |
| 238 | CHECK_NE(0U, second_dex_files.size()); |
| 239 | for (auto& dex_file : first_dex_files) { |
| 240 | class_path.push_back(dex_file.get()); |
| 241 | loaded_dex_files_.push_back(std::move(dex_file)); |
| 242 | } |
| 243 | for (auto& dex_file : second_dex_files) { |
| 244 | class_path.push_back(dex_file.get()); |
| 245 | loaded_dex_files_.push_back(std::move(dex_file)); |
| 246 | } |
| 247 | |
| 248 | Thread* self = Thread::Current(); |
| 249 | jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self, |
| 250 | class_path); |
| 251 | self->SetClassLoaderOverride(class_loader); |
| 252 | return class_loader; |
| 253 | } |
| 254 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 255 | jobject CommonRuntimeTestImpl::LoadDex(const char* dex_name) { |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 256 | jobject class_loader = LoadDexInPathClassLoader(dex_name, nullptr); |
| 257 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 258 | return class_loader; |
| 259 | } |
| 260 | |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 261 | jobject |
| 262 | CommonRuntimeTestImpl::LoadDexInWellKnownClassLoader(const std::vector<std::string>& dex_names, |
| 263 | jclass loader_class, |
| 264 | jobject parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 265 | jobject shared_libraries, |
| 266 | jobject shared_libraries_after) { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 267 | std::vector<const DexFile*> class_path; |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 268 | for (const std::string& dex_name : dex_names) { |
| 269 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name.c_str()); |
| 270 | CHECK_NE(0U, dex_files.size()); |
| 271 | for (auto& dex_file : dex_files) { |
| 272 | class_path.push_back(dex_file.get()); |
| 273 | loaded_dex_files_.push_back(std::move(dex_file)); |
| 274 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 275 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 276 | Thread* self = Thread::Current(); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 277 | ScopedObjectAccess soa(self); |
| 278 | |
| 279 | jobject result = Runtime::Current()->GetClassLinker()->CreateWellKnownClassLoader( |
| 280 | self, |
| 281 | class_path, |
| 282 | loader_class, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 283 | parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 284 | shared_libraries, |
| 285 | shared_libraries_after); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 286 | |
| 287 | { |
| 288 | // Verify we build the correct chain. |
| 289 | |
| 290 | ObjPtr<mirror::ClassLoader> actual_class_loader = soa.Decode<mirror::ClassLoader>(result); |
| 291 | // Verify that the result has the correct class. |
| 292 | CHECK_EQ(soa.Decode<mirror::Class>(loader_class), actual_class_loader->GetClass()); |
| 293 | // Verify that the parent is not null. The boot class loader will be set up as a |
| 294 | // proper object. |
| 295 | ObjPtr<mirror::ClassLoader> actual_parent(actual_class_loader->GetParent()); |
| 296 | CHECK(actual_parent != nullptr); |
| 297 | |
| 298 | if (parent_loader != nullptr) { |
| 299 | // We were given a parent. Verify that it's what we expect. |
| 300 | ObjPtr<mirror::ClassLoader> expected_parent = soa.Decode<mirror::ClassLoader>(parent_loader); |
| 301 | CHECK_EQ(expected_parent, actual_parent); |
| 302 | } else { |
| 303 | // No parent given. The parent must be the BootClassLoader. |
| 304 | CHECK(Runtime::Current()->GetClassLinker()->IsBootClassLoader(soa, actual_parent)); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return result; |
| 309 | } |
| 310 | |
| 311 | jobject CommonRuntimeTestImpl::LoadDexInPathClassLoader(const std::string& dex_name, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 312 | jobject parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 313 | jobject shared_libraries, |
| 314 | jobject shared_libraries_after) { |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 315 | return LoadDexInPathClassLoader(std::vector<std::string>{ dex_name }, |
| 316 | parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 317 | shared_libraries, |
| 318 | shared_libraries_after); |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | jobject CommonRuntimeTestImpl::LoadDexInPathClassLoader(const std::vector<std::string>& names, |
| 322 | jobject parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 323 | jobject shared_libraries, |
| 324 | jobject shared_libraries_after) { |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 325 | return LoadDexInWellKnownClassLoader(names, |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 326 | WellKnownClasses::dalvik_system_PathClassLoader, |
Nicolas Geoffray | e167273 | 2018-11-30 01:09:49 +0000 | [diff] [blame] | 327 | parent_loader, |
Brad Stenning | 9c924e8 | 2021-10-11 19:09:00 -0700 | [diff] [blame] | 328 | shared_libraries, |
| 329 | shared_libraries_after); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | jobject CommonRuntimeTestImpl::LoadDexInDelegateLastClassLoader(const std::string& dex_name, |
| 333 | jobject parent_loader) { |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 334 | return LoadDexInWellKnownClassLoader({ dex_name }, |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 335 | WellKnownClasses::dalvik_system_DelegateLastClassLoader, |
| 336 | parent_loader); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 337 | } |
| 338 | |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 339 | jobject CommonRuntimeTestImpl::LoadDexInInMemoryDexClassLoader(const std::string& dex_name, |
| 340 | jobject parent_loader) { |
Dan Zimmerman | b682ea4 | 2019-12-23 06:59:06 -0800 | [diff] [blame] | 341 | return LoadDexInWellKnownClassLoader({ dex_name }, |
David Brazdil | 1a9ac53 | 2019-03-05 11:57:13 +0000 | [diff] [blame] | 342 | WellKnownClasses::dalvik_system_InMemoryDexClassLoader, |
| 343 | parent_loader); |
| 344 | } |
| 345 | |
Andreas Gampe | 26761f7 | 2017-07-20 18:00:39 -0700 | [diff] [blame] | 346 | void CommonRuntimeTestImpl::FillHeap(Thread* self, |
| 347 | ClassLinker* class_linker, |
| 348 | VariableSizedHandleScope* handle_scope) { |
| 349 | DCHECK(handle_scope != nullptr); |
| 350 | |
| 351 | Runtime::Current()->GetHeap()->SetIdealFootprint(1 * GB); |
| 352 | |
| 353 | // Class java.lang.Object. |
| 354 | Handle<mirror::Class> c(handle_scope->NewHandle( |
| 355 | class_linker->FindSystemClass(self, "Ljava/lang/Object;"))); |
| 356 | // Array helps to fill memory faster. |
| 357 | Handle<mirror::Class> ca(handle_scope->NewHandle( |
| 358 | class_linker->FindSystemClass(self, "[Ljava/lang/Object;"))); |
| 359 | |
| 360 | // Start allocating with ~128K |
| 361 | size_t length = 128 * KB; |
| 362 | while (length > 40) { |
| 363 | const int32_t array_length = length / 4; // Object[] has elements of size 4. |
| 364 | MutableHandle<mirror::Object> h(handle_scope->NewHandle<mirror::Object>( |
| 365 | mirror::ObjectArray<mirror::Object>::Alloc(self, ca.Get(), array_length))); |
| 366 | if (self->IsExceptionPending() || h == nullptr) { |
| 367 | self->ClearException(); |
| 368 | |
| 369 | // Try a smaller length |
| 370 | length = length / 2; |
| 371 | // Use at most a quarter the reported free space. |
| 372 | size_t mem = Runtime::Current()->GetHeap()->GetFreeMemory(); |
| 373 | if (length * 4 > mem) { |
| 374 | length = mem / 4; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | // Allocate simple objects till it fails. |
| 380 | while (!self->IsExceptionPending()) { |
| 381 | handle_scope->NewHandle<mirror::Object>(c->AllocObject(self)); |
| 382 | } |
| 383 | self->ClearException(); |
| 384 | } |
| 385 | |
| 386 | void CommonRuntimeTestImpl::SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options) { |
| 387 | // Use a smaller heap |
| 388 | bool found = false; |
| 389 | for (std::pair<std::string, const void*>& pair : *options) { |
| 390 | if (pair.first.find("-Xmx") == 0) { |
| 391 | pair.first = "-Xmx4M"; // Smallest we can go. |
| 392 | found = true; |
| 393 | } |
| 394 | } |
| 395 | if (!found) { |
| 396 | options->emplace_back("-Xmx4M", nullptr); |
| 397 | } |
| 398 | } |
| 399 | |
Vladimir Marko | 2b076df | 2019-02-20 11:27:52 +0000 | [diff] [blame] | 400 | void CommonRuntimeTestImpl::MakeInterpreted(ObjPtr<mirror::Class> klass) { |
| 401 | PointerSize pointer_size = class_linker_->GetImagePointerSize(); |
| 402 | for (ArtMethod& method : klass->GetMethods(pointer_size)) { |
| 403 | class_linker_->SetEntryPointsToInterpreter(&method); |
| 404 | } |
| 405 | } |
| 406 | |
Vladimir Marko | ec91d48 | 2018-11-29 12:17:11 +0000 | [diff] [blame] | 407 | bool CommonRuntimeTestImpl::StartDex2OatCommandLine(/*out*/std::vector<std::string>* argv, |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 408 | /*out*/std::string* error_msg, |
| 409 | bool use_runtime_bcp_and_image) { |
Vladimir Marko | ec91d48 | 2018-11-29 12:17:11 +0000 | [diff] [blame] | 410 | DCHECK(argv != nullptr); |
| 411 | DCHECK(argv->empty()); |
| 412 | |
| 413 | Runtime* runtime = Runtime::Current(); |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 414 | if (use_runtime_bcp_and_image && runtime->GetHeap()->GetBootImageSpaces().empty()) { |
Vladimir Marko | ec91d48 | 2018-11-29 12:17:11 +0000 | [diff] [blame] | 415 | *error_msg = "No image location found for Dex2Oat."; |
| 416 | return false; |
| 417 | } |
Vladimir Marko | ec91d48 | 2018-11-29 12:17:11 +0000 | [diff] [blame] | 418 | |
| 419 | argv->push_back(runtime->GetCompilerExecutable()); |
| 420 | if (runtime->IsJavaDebuggable()) { |
| 421 | argv->push_back("--debuggable"); |
| 422 | } |
| 423 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(argv); |
| 424 | |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 425 | if (use_runtime_bcp_and_image) { |
| 426 | argv->push_back("--runtime-arg"); |
| 427 | argv->push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames())); |
| 428 | argv->push_back("--runtime-arg"); |
| 429 | argv->push_back(GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations())); |
Vladimir Marko | ec91d48 | 2018-11-29 12:17:11 +0000 | [diff] [blame] | 430 | |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 431 | const std::vector<gc::space::ImageSpace*>& image_spaces = |
| 432 | runtime->GetHeap()->GetBootImageSpaces(); |
| 433 | DCHECK(!image_spaces.empty()); |
| 434 | argv->push_back("--boot-image=" + image_spaces[0]->GetImageLocation()); |
| 435 | } |
Vladimir Marko | ec91d48 | 2018-11-29 12:17:11 +0000 | [diff] [blame] | 436 | |
| 437 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 438 | argv->insert(argv->end(), compiler_options.begin(), compiler_options.end()); |
| 439 | return true; |
| 440 | } |
| 441 | |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 442 | bool CommonRuntimeTestImpl::CompileBootImage(const std::vector<std::string>& extra_args, |
| 443 | const std::string& image_file_name_prefix, |
| 444 | ArrayRef<const std::string> dex_files, |
| 445 | ArrayRef<const std::string> dex_locations, |
| 446 | std::string* error_msg, |
| 447 | const std::string& use_fd_prefix) { |
| 448 | Runtime* const runtime = Runtime::Current(); |
| 449 | std::vector<std::string> argv { |
| 450 | runtime->GetCompilerExecutable(), |
| 451 | "--runtime-arg", |
| 452 | "-Xms64m", |
| 453 | "--runtime-arg", |
| 454 | "-Xmx64m", |
| 455 | "--runtime-arg", |
| 456 | "-Xverify:softfail", |
| 457 | }; |
| 458 | CHECK_EQ(dex_files.size(), dex_locations.size()); |
| 459 | for (const std::string& dex_file : dex_files) { |
| 460 | argv.push_back("--dex-file=" + dex_file); |
| 461 | } |
| 462 | for (const std::string& dex_location : dex_locations) { |
| 463 | argv.push_back("--dex-location=" + dex_location); |
| 464 | } |
| 465 | if (runtime->IsJavaDebuggable()) { |
| 466 | argv.push_back("--debuggable"); |
| 467 | } |
| 468 | runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv); |
| 469 | |
| 470 | if (!kIsTargetBuild) { |
| 471 | argv.push_back("--host"); |
| 472 | } |
| 473 | |
| 474 | std::unique_ptr<File> art_file; |
| 475 | std::unique_ptr<File> vdex_file; |
| 476 | std::unique_ptr<File> oat_file; |
| 477 | if (!use_fd_prefix.empty()) { |
| 478 | art_file.reset(OS::CreateEmptyFile((use_fd_prefix + ".art").c_str())); |
| 479 | vdex_file.reset(OS::CreateEmptyFile((use_fd_prefix + ".vdex").c_str())); |
| 480 | oat_file.reset(OS::CreateEmptyFile((use_fd_prefix + ".oat").c_str())); |
| 481 | argv.push_back("--image-fd=" + std::to_string(art_file->Fd())); |
| 482 | argv.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd())); |
| 483 | argv.push_back("--oat-fd=" + std::to_string(oat_file->Fd())); |
| 484 | argv.push_back("--oat-location=" + image_file_name_prefix + ".oat"); |
| 485 | } else { |
| 486 | argv.push_back("--image=" + image_file_name_prefix + ".art"); |
| 487 | argv.push_back("--oat-file=" + image_file_name_prefix + ".oat"); |
| 488 | argv.push_back("--oat-location=" + image_file_name_prefix + ".oat"); |
| 489 | } |
| 490 | |
| 491 | std::vector<std::string> compiler_options = runtime->GetCompilerOptions(); |
| 492 | argv.insert(argv.end(), compiler_options.begin(), compiler_options.end()); |
| 493 | |
| 494 | // We must set --android-root. |
| 495 | const char* android_root = getenv("ANDROID_ROOT"); |
| 496 | CHECK(android_root != nullptr); |
| 497 | argv.push_back("--android-root=" + std::string(android_root)); |
| 498 | argv.insert(argv.end(), extra_args.begin(), extra_args.end()); |
| 499 | |
| 500 | bool result = RunDex2Oat(argv, error_msg); |
| 501 | if (art_file != nullptr) { |
| 502 | CHECK_EQ(0, art_file->FlushClose()); |
| 503 | } |
| 504 | if (vdex_file != nullptr) { |
| 505 | CHECK_EQ(0, vdex_file->FlushClose()); |
| 506 | } |
| 507 | if (oat_file != nullptr) { |
| 508 | CHECK_EQ(0, oat_file->FlushClose()); |
| 509 | } |
| 510 | return result; |
| 511 | } |
| 512 | |
| 513 | bool CommonRuntimeTestImpl::RunDex2Oat(const std::vector<std::string>& args, |
| 514 | std::string* error_msg) { |
| 515 | // We only want fatal logging for the error message. |
| 516 | auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:f", 1) == 0; }; |
| 517 | ForkAndExecResult res = ForkAndExec(args, post_fork_fn, error_msg); |
| 518 | if (res.stage != ForkAndExecResult::kFinished) { |
| 519 | *error_msg = strerror(errno); |
| 520 | return false; |
| 521 | } |
| 522 | return res.StandardSuccess(); |
| 523 | } |
| 524 | |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 525 | std::string CommonRuntimeTestImpl::GetImageLocation() { |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 526 | return GetImageDirectory() + "/boot.art"; |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | std::string CommonRuntimeTestImpl::GetSystemImageFile() { |
David Srbecky | cf0c6ef | 2020-02-05 16:25:36 +0000 | [diff] [blame] | 530 | std::string isa = GetInstructionSetString(kRuntimeISA); |
David Srbecky | 0c0f302 | 2020-02-13 15:53:01 +0000 | [diff] [blame] | 531 | return GetImageDirectory() + "/" + isa + "/boot.art"; |
Vladimir Marko | 672c080 | 2019-07-26 13:03:13 +0100 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | void CommonRuntimeTestImpl::EnterTransactionMode() { |
| 535 | CHECK(!Runtime::Current()->IsActiveTransaction()); |
| 536 | Runtime::Current()->EnterTransactionMode(/*strict=*/ false, /*root=*/ nullptr); |
| 537 | } |
| 538 | |
| 539 | void CommonRuntimeTestImpl::ExitTransactionMode() { |
| 540 | Runtime::Current()->ExitTransactionMode(); |
| 541 | CHECK(!Runtime::Current()->IsActiveTransaction()); |
| 542 | } |
| 543 | |
| 544 | void CommonRuntimeTestImpl::RollbackAndExitTransactionMode() { |
| 545 | Runtime::Current()->RollbackAndExitTransactionMode(); |
| 546 | CHECK(!Runtime::Current()->IsActiveTransaction()); |
| 547 | } |
| 548 | |
| 549 | bool CommonRuntimeTestImpl::IsTransactionAborted() { |
| 550 | return Runtime::Current()->IsTransactionAborted(); |
| 551 | } |
| 552 | |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 553 | void CommonRuntimeTestImpl::VisitDexes(ArrayRef<const std::string> dexes, |
| 554 | const std::function<void(MethodReference)>& method_visitor, |
| 555 | const std::function<void(TypeReference)>& class_visitor, |
| 556 | size_t method_frequency, |
| 557 | size_t class_frequency) { |
| 558 | size_t method_counter = 0; |
| 559 | size_t class_counter = 0; |
| 560 | for (const std::string& dex : dexes) { |
| 561 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 562 | std::string error_msg; |
| 563 | const ArtDexFileLoader dex_file_loader; |
| 564 | CHECK(dex_file_loader.Open(dex.c_str(), |
| 565 | dex, |
| 566 | /*verify*/ true, |
| 567 | /*verify_checksum*/ false, |
| 568 | &error_msg, |
| 569 | &dex_files)) |
| 570 | << error_msg; |
| 571 | for (const std::unique_ptr<const DexFile>& dex_file : dex_files) { |
| 572 | for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) { |
| 573 | if (++method_counter % method_frequency == 0) { |
| 574 | method_visitor(MethodReference(dex_file.get(), i)); |
| 575 | } |
| 576 | } |
| 577 | for (size_t i = 0; i < dex_file->NumTypeIds(); ++i) { |
| 578 | if (++class_counter % class_frequency == 0) { |
| 579 | class_visitor(TypeReference(dex_file.get(), dex::TypeIndex(i))); |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | void CommonRuntimeTestImpl::GenerateProfile(ArrayRef<const std::string> dexes, |
| 587 | File* out_file, |
| 588 | size_t method_frequency, |
Vladimir Marko | a220980 | 2021-04-23 13:28:29 +0000 | [diff] [blame] | 589 | size_t type_frequency, |
| 590 | bool for_boot_image) { |
| 591 | ProfileCompilationInfo profile(for_boot_image); |
Vladimir Marko | fdd4684 | 2020-03-25 14:57:17 +0000 | [diff] [blame] | 592 | VisitDexes( |
| 593 | dexes, |
| 594 | [&profile](MethodReference ref) { |
| 595 | uint32_t flags = ProfileCompilationInfo::MethodHotness::kFlagHot | |
| 596 | ProfileCompilationInfo::MethodHotness::kFlagStartup; |
| 597 | EXPECT_TRUE(profile.AddMethod( |
| 598 | ProfileMethodInfo(ref), |
| 599 | static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags))); |
| 600 | }, |
| 601 | [&profile](TypeReference ref) { |
| 602 | std::set<dex::TypeIndex> classes; |
| 603 | classes.insert(ref.TypeIndex()); |
| 604 | EXPECT_TRUE(profile.AddClassesForDex(ref.dex_file, classes.begin(), classes.end())); |
| 605 | }, |
| 606 | method_frequency, |
| 607 | type_frequency); |
| 608 | profile.Save(out_file->Fd()); |
| 609 | EXPECT_EQ(out_file->Flush(), 0); |
| 610 | } |
| 611 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 612 | CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 613 | vm_->SetCheckJniAbortHook(Hook, &actual_); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | CheckJniAbortCatcher::~CheckJniAbortCatcher() { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 617 | vm_->SetCheckJniAbortHook(nullptr, nullptr); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 618 | EXPECT_TRUE(actual_.empty()) << actual_; |
| 619 | } |
| 620 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 621 | void CheckJniAbortCatcher::Check(const std::string& expected_text) { |
| 622 | Check(expected_text.c_str()); |
| 623 | } |
| 624 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 625 | void CheckJniAbortCatcher::Check(const char* expected_text) { |
| 626 | EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n" |
| 627 | << "Expected to find: " << expected_text << "\n" |
| 628 | << "In the output : " << actual_; |
| 629 | actual_.clear(); |
| 630 | } |
| 631 | |
| 632 | void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) { |
| 633 | // We use += because when we're hooking the aborts like this, multiple problems can be found. |
| 634 | *reinterpret_cast<std::string*>(data) += reason; |
| 635 | } |
| 636 | |
| 637 | } // namespace art |