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