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" |
Andreas Gampe | fdb7a61 | 2017-11-01 15:11:13 -0700 | [diff] [blame] | 27 | #include <unicode/uvernum.h> |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 28 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 29 | #include "art_field-inl.h" |
David Sehr | 891a50e | 2017-10-27 17:01:07 -0700 | [diff] [blame] | 30 | #include "base/file_utils.h" |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 31 | #include "base/logging.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 32 | #include "base/macros.h" |
David Sehr | 79e2607 | 2018-04-06 17:58:50 -0700 | [diff] [blame] | 33 | #include "base/mem_map.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 34 | #include "base/mutex.h" |
| 35 | #include "base/os.h" |
Andreas Gampe | dcc528d | 2017-12-07 13:37:10 -0800 | [diff] [blame] | 36 | #include "base/runtime_debug.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 37 | #include "base/stl_util.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 38 | #include "base/unix_file/fd_file.h" |
| 39 | #include "class_linker.h" |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 40 | #include "class_loader_utils.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 41 | #include "compiler_callbacks.h" |
David Sehr | 013fd80 | 2018-01-11 22:55:24 -0800 | [diff] [blame] | 42 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 43 | #include "dex/dex_file-inl.h" |
| 44 | #include "dex/dex_file_loader.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 45 | #include "dex/primitive.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 46 | #include "gc/heap.h" |
Steven Moreland | e431e27 | 2017-07-18 16:53:49 -0700 | [diff] [blame] | 47 | #include "gc_root-inl.h" |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 48 | #include "gtest/gtest.h" |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 49 | #include "handle_scope-inl.h" |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 50 | #include "interpreter/unstarted_runtime.h" |
Vladimir Marko | a3ad0cd | 2018-05-04 10:06:38 +0100 | [diff] [blame] | 51 | #include "jni/java_vm_ext.h" |
| 52 | #include "jni/jni_internal.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 53 | #include "mirror/class-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 54 | #include "mirror/class_loader.h" |
Mathieu Chartier | e58991b | 2015-10-13 07:59:34 -0700 | [diff] [blame] | 55 | #include "native/dalvik_system_DexFile.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 56 | #include "noop_compiler_callbacks.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 57 | #include "runtime-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 58 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 59 | #include "thread.h" |
| 60 | #include "well_known_classes.h" |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 61 | |
| 62 | int main(int argc, char **argv) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 63 | // Gtests can be very noisy. For example, an executable with multiple tests will trigger native |
| 64 | // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses |
| 65 | // everything else. In case you want to see all messages, comment out the line. |
Nicolas Geoffray | a7a4759 | 2015-11-24 09:17:30 +0000 | [diff] [blame] | 66 | setenv("ANDROID_LOG_TAGS", "*:e", 1); |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 67 | |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 68 | art::Locks::Init(); |
Andreas Gampe | 51d80cc | 2017-06-21 21:05:13 -0700 | [diff] [blame] | 69 | art::InitLogging(argv, art::Runtime::Abort); |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 70 | art::MemMap::Init(); |
Andreas Gampe | 3fec9ac | 2016-09-13 10:47:28 -0700 | [diff] [blame] | 71 | LOG(INFO) << "Running main() from common_runtime_test.cc..."; |
Elliott Hughes | eb02a12 | 2012-06-12 11:35:40 -0700 | [diff] [blame] | 72 | testing::InitGoogleTest(&argc, argv); |
| 73 | return RUN_ALL_TESTS(); |
| 74 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 75 | |
| 76 | namespace art { |
| 77 | |
Andreas Gampe | 46ee31b | 2016-12-14 10:11:49 -0800 | [diff] [blame] | 78 | using android::base::StringPrintf; |
| 79 | |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 80 | static bool unstarted_initialized_ = false; |
| 81 | |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 82 | CommonRuntimeTestImpl::CommonRuntimeTestImpl() |
| 83 | : class_linker_(nullptr), java_lang_dex_file_(nullptr) { |
| 84 | } |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 85 | |
| 86 | CommonRuntimeTestImpl::~CommonRuntimeTestImpl() { |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 87 | // Ensure the dex files are cleaned up before the runtime. |
| 88 | loaded_dex_files_.clear(); |
| 89 | runtime_.reset(); |
| 90 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 91 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 92 | std::string CommonRuntimeTestImpl::GetAndroidTargetToolsDir(InstructionSet isa) { |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 93 | switch (isa) { |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 94 | case InstructionSet::kArm: |
| 95 | case InstructionSet::kThumb2: |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 96 | return GetAndroidToolsDir("prebuilts/gcc/linux-x86/arm", |
| 97 | "arm-linux-androideabi", |
| 98 | "arm-linux-androideabi"); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 99 | case InstructionSet::kArm64: |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 100 | return GetAndroidToolsDir("prebuilts/gcc/linux-x86/aarch64", |
| 101 | "aarch64-linux-android", |
| 102 | "aarch64-linux-android"); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 103 | case InstructionSet::kX86: |
| 104 | case InstructionSet::kX86_64: |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 105 | return GetAndroidToolsDir("prebuilts/gcc/linux-x86/x86", |
| 106 | "x86_64-linux-android", |
| 107 | "x86_64-linux-android"); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 108 | case InstructionSet::kMips: |
| 109 | case InstructionSet::kMips64: |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 110 | return GetAndroidToolsDir("prebuilts/gcc/linux-x86/mips", |
| 111 | "mips64el-linux-android", |
| 112 | "mips64el-linux-android"); |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 113 | case InstructionSet::kNone: |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | ADD_FAILURE() << "Invalid isa " << isa; |
| 117 | return ""; |
| 118 | } |
| 119 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 120 | void CommonRuntimeTestImpl::SetUp() { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 121 | CommonArtTestImpl::SetUp(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 122 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 123 | std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB)); |
| 124 | std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB)); |
| 125 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 126 | |
| 127 | RuntimeOptions options; |
Narayan Kamath | d1ef436 | 2015-11-12 11:49:06 +0000 | [diff] [blame] | 128 | std::string boot_class_path_string = "-Xbootclasspath"; |
| 129 | for (const std::string &core_dex_file_name : GetLibCoreDexFileNames()) { |
| 130 | boot_class_path_string += ":"; |
| 131 | boot_class_path_string += core_dex_file_name; |
| 132 | } |
| 133 | |
Richard Uhler | c275259 | 2015-01-02 13:28:22 -0800 | [diff] [blame] | 134 | options.push_back(std::make_pair(boot_class_path_string, nullptr)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 135 | options.push_back(std::make_pair("-Xcheck:jni", nullptr)); |
Richard Uhler | c275259 | 2015-01-02 13:28:22 -0800 | [diff] [blame] | 136 | options.push_back(std::make_pair(min_heap_string, nullptr)); |
| 137 | options.push_back(std::make_pair(max_heap_string, nullptr)); |
Andreas Gampe | 1c5b42f | 2017-06-15 18:20:45 -0700 | [diff] [blame] | 138 | options.push_back(std::make_pair("-XX:SlowDebug=true", nullptr)); |
Andreas Gampe | 46c4c85 | 2017-06-21 19:49:08 -0700 | [diff] [blame] | 139 | static bool gSlowDebugTestFlag = false; |
| 140 | RegisterRuntimeDebugFlag(&gSlowDebugTestFlag); |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 141 | |
| 142 | callbacks_.reset(new NoopCompilerCallbacks()); |
| 143 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 144 | SetUpRuntimeOptions(&options); |
Igor Murashkin | aaebaa0 | 2015-01-26 10:55:53 -0800 | [diff] [blame] | 145 | |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 146 | // Install compiler-callbacks if SetupRuntimeOptions hasn't deleted them. |
| 147 | if (callbacks_.get() != nullptr) { |
| 148 | options.push_back(std::make_pair("compilercallbacks", callbacks_.get())); |
| 149 | } |
| 150 | |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 151 | PreRuntimeCreate(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 152 | if (!Runtime::Create(options, false)) { |
| 153 | LOG(FATAL) << "Failed to create runtime"; |
| 154 | return; |
| 155 | } |
Richard Uhler | 66d874d | 2015-01-15 09:37:19 -0800 | [diff] [blame] | 156 | PostRuntimeCreate(); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 157 | runtime_.reset(Runtime::Current()); |
| 158 | class_linker_ = runtime_->GetClassLinker(); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 159 | |
Andreas Gampe | a00f012 | 2015-12-16 16:54:35 -0800 | [diff] [blame] | 160 | // Runtime::Create acquired the mutator_lock_ that is normally given away when we |
| 161 | // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess. |
| 162 | Thread::Current()->TransitionFromRunnableToSuspended(kNative); |
| 163 | |
| 164 | // Get the boot class path from the runtime so it can be used in tests. |
| 165 | boot_class_path_ = class_linker_->GetBootClassPath(); |
| 166 | ASSERT_FALSE(boot_class_path_.empty()); |
| 167 | java_lang_dex_file_ = boot_class_path_[0]; |
| 168 | |
| 169 | FinalizeSetup(); |
Andreas Gampe | 46c4c85 | 2017-06-21 19:49:08 -0700 | [diff] [blame] | 170 | |
| 171 | // Ensure that we're really running with debug checks enabled. |
| 172 | CHECK(gSlowDebugTestFlag); |
Andreas Gampe | a00f012 | 2015-12-16 16:54:35 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 175 | void CommonRuntimeTestImpl::FinalizeSetup() { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 176 | // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this |
| 177 | // set up. |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 178 | if (!unstarted_initialized_) { |
Andreas Gampe | 799681b | 2015-05-15 19:24:12 -0700 | [diff] [blame] | 179 | interpreter::UnstartedRuntime::Initialize(); |
Andreas Gampe | 9b5cba4 | 2015-03-11 09:53:50 -0700 | [diff] [blame] | 180 | unstarted_initialized_ = true; |
| 181 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 182 | |
Andreas Gampe | a00f012 | 2015-12-16 16:54:35 -0800 | [diff] [blame] | 183 | { |
| 184 | ScopedObjectAccess soa(Thread::Current()); |
Vladimir Marko | dcfcce4 | 2018-06-27 10:00:28 +0000 | [diff] [blame] | 185 | runtime_->RunRootClinits(soa.Self()); |
Andreas Gampe | a00f012 | 2015-12-16 16:54:35 -0800 | [diff] [blame] | 186 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 187 | |
| 188 | // We're back in native, take the opportunity to initialize well known classes. |
| 189 | WellKnownClasses::Init(Thread::Current()->GetJniEnv()); |
| 190 | |
| 191 | // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread |
| 192 | // pool is created by the runtime. |
| 193 | runtime_->GetHeap()->CreateThreadPool(); |
| 194 | runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test |
Hiroshi Yamauchi | 4460a84 | 2015-03-09 11:57:48 -0700 | [diff] [blame] | 195 | // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject). |
| 196 | runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 199 | void CommonRuntimeTestImpl::TearDown() { |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 200 | CommonArtTestImpl::TearDown(); |
Andreas Gampe | 4886411 | 2017-01-19 17:23:17 -0800 | [diff] [blame] | 201 | if (runtime_ != nullptr) { |
| 202 | runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption after the test |
| 203 | } |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Andreas Gampe | 1fe5e5c | 2014-07-11 21:14:35 -0700 | [diff] [blame] | 206 | // Check that for target builds we have ART_TARGET_NATIVETEST_DIR set. |
| 207 | #ifdef ART_TARGET |
| 208 | #ifndef ART_TARGET_NATIVETEST_DIR |
| 209 | #error "ART_TARGET_NATIVETEST_DIR not set." |
| 210 | #endif |
| 211 | // Wrap it as a string literal. |
| 212 | #define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/" |
| 213 | #else |
| 214 | #define ART_TARGET_NATIVETEST_DIR_STRING "" |
| 215 | #endif |
| 216 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 217 | std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(jobject jclass_loader) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 218 | ScopedObjectAccess soa(Thread::Current()); |
| 219 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 220 | StackHandleScope<1> hs(soa.Self()); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 221 | Handle<mirror::ClassLoader> class_loader = hs.NewHandle( |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 222 | soa.Decode<mirror::ClassLoader>(jclass_loader)); |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 223 | return GetDexFiles(soa, class_loader); |
| 224 | } |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 225 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 226 | std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles( |
| 227 | ScopedObjectAccess& soa, |
| 228 | Handle<mirror::ClassLoader> class_loader) { |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 229 | DCHECK( |
| 230 | (class_loader->GetClass() == |
| 231 | soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)) || |
| 232 | (class_loader->GetClass() == |
| 233 | soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DelegateLastClassLoader))); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 234 | |
Andreas Gampe | b8e7c37 | 2018-02-20 18:24:55 -0800 | [diff] [blame] | 235 | std::vector<const DexFile*> ret; |
| 236 | VisitClassLoaderDexFiles(soa, |
| 237 | class_loader, |
| 238 | [&](const DexFile* cp_dex_file) { |
| 239 | if (cp_dex_file == nullptr) { |
| 240 | LOG(WARNING) << "Null DexFile"; |
| 241 | } else { |
| 242 | ret.push_back(cp_dex_file); |
| 243 | } |
| 244 | return true; |
| 245 | }); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 246 | return ret; |
| 247 | } |
| 248 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 249 | const DexFile* CommonRuntimeTestImpl::GetFirstDexFile(jobject jclass_loader) { |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 250 | std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader)); |
| 251 | DCHECK(!tmp.empty()); |
| 252 | const DexFile* ret = tmp[0]; |
| 253 | DCHECK(ret != nullptr); |
| 254 | return ret; |
| 255 | } |
| 256 | |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 257 | jobject CommonRuntimeTestImpl::LoadMultiDex(const char* first_dex_name, |
| 258 | const char* second_dex_name) { |
| 259 | std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles(first_dex_name); |
| 260 | std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles(second_dex_name); |
| 261 | std::vector<const DexFile*> class_path; |
| 262 | CHECK_NE(0U, first_dex_files.size()); |
| 263 | CHECK_NE(0U, second_dex_files.size()); |
| 264 | for (auto& dex_file : first_dex_files) { |
| 265 | class_path.push_back(dex_file.get()); |
| 266 | loaded_dex_files_.push_back(std::move(dex_file)); |
| 267 | } |
| 268 | for (auto& dex_file : second_dex_files) { |
| 269 | class_path.push_back(dex_file.get()); |
| 270 | loaded_dex_files_.push_back(std::move(dex_file)); |
| 271 | } |
| 272 | |
| 273 | Thread* self = Thread::Current(); |
| 274 | jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self, |
| 275 | class_path); |
| 276 | self->SetClassLoaderOverride(class_loader); |
| 277 | return class_loader; |
| 278 | } |
| 279 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 280 | jobject CommonRuntimeTestImpl::LoadDex(const char* dex_name) { |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 281 | jobject class_loader = LoadDexInPathClassLoader(dex_name, nullptr); |
| 282 | Thread::Current()->SetClassLoaderOverride(class_loader); |
| 283 | return class_loader; |
| 284 | } |
| 285 | |
| 286 | jobject CommonRuntimeTestImpl::LoadDexInWellKnownClassLoader(const std::string& dex_name, |
| 287 | jclass loader_class, |
| 288 | jobject parent_loader) { |
| 289 | std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name.c_str()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 290 | std::vector<const DexFile*> class_path; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 291 | CHECK_NE(0U, dex_files.size()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 292 | for (auto& dex_file : dex_files) { |
| 293 | class_path.push_back(dex_file.get()); |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 294 | loaded_dex_files_.push_back(std::move(dex_file)); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 295 | } |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 296 | Thread* self = Thread::Current(); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 297 | ScopedObjectAccess soa(self); |
| 298 | |
| 299 | jobject result = Runtime::Current()->GetClassLinker()->CreateWellKnownClassLoader( |
| 300 | self, |
| 301 | class_path, |
| 302 | loader_class, |
| 303 | parent_loader); |
| 304 | |
| 305 | { |
| 306 | // Verify we build the correct chain. |
| 307 | |
| 308 | ObjPtr<mirror::ClassLoader> actual_class_loader = soa.Decode<mirror::ClassLoader>(result); |
| 309 | // Verify that the result has the correct class. |
| 310 | CHECK_EQ(soa.Decode<mirror::Class>(loader_class), actual_class_loader->GetClass()); |
| 311 | // Verify that the parent is not null. The boot class loader will be set up as a |
| 312 | // proper object. |
| 313 | ObjPtr<mirror::ClassLoader> actual_parent(actual_class_loader->GetParent()); |
| 314 | CHECK(actual_parent != nullptr); |
| 315 | |
| 316 | if (parent_loader != nullptr) { |
| 317 | // We were given a parent. Verify that it's what we expect. |
| 318 | ObjPtr<mirror::ClassLoader> expected_parent = soa.Decode<mirror::ClassLoader>(parent_loader); |
| 319 | CHECK_EQ(expected_parent, actual_parent); |
| 320 | } else { |
| 321 | // No parent given. The parent must be the BootClassLoader. |
| 322 | CHECK(Runtime::Current()->GetClassLinker()->IsBootClassLoader(soa, actual_parent)); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | return result; |
| 327 | } |
| 328 | |
| 329 | jobject CommonRuntimeTestImpl::LoadDexInPathClassLoader(const std::string& dex_name, |
| 330 | jobject parent_loader) { |
| 331 | return LoadDexInWellKnownClassLoader(dex_name, |
| 332 | WellKnownClasses::dalvik_system_PathClassLoader, |
| 333 | parent_loader); |
| 334 | } |
| 335 | |
| 336 | jobject CommonRuntimeTestImpl::LoadDexInDelegateLastClassLoader(const std::string& dex_name, |
| 337 | jobject parent_loader) { |
| 338 | return LoadDexInWellKnownClassLoader(dex_name, |
| 339 | WellKnownClasses::dalvik_system_DelegateLastClassLoader, |
| 340 | parent_loader); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Andreas Gampe | 26761f7 | 2017-07-20 18:00:39 -0700 | [diff] [blame] | 343 | void CommonRuntimeTestImpl::FillHeap(Thread* self, |
| 344 | ClassLinker* class_linker, |
| 345 | VariableSizedHandleScope* handle_scope) { |
| 346 | DCHECK(handle_scope != nullptr); |
| 347 | |
| 348 | Runtime::Current()->GetHeap()->SetIdealFootprint(1 * GB); |
| 349 | |
| 350 | // Class java.lang.Object. |
| 351 | Handle<mirror::Class> c(handle_scope->NewHandle( |
| 352 | class_linker->FindSystemClass(self, "Ljava/lang/Object;"))); |
| 353 | // Array helps to fill memory faster. |
| 354 | Handle<mirror::Class> ca(handle_scope->NewHandle( |
| 355 | class_linker->FindSystemClass(self, "[Ljava/lang/Object;"))); |
| 356 | |
| 357 | // Start allocating with ~128K |
| 358 | size_t length = 128 * KB; |
| 359 | while (length > 40) { |
| 360 | const int32_t array_length = length / 4; // Object[] has elements of size 4. |
| 361 | MutableHandle<mirror::Object> h(handle_scope->NewHandle<mirror::Object>( |
| 362 | mirror::ObjectArray<mirror::Object>::Alloc(self, ca.Get(), array_length))); |
| 363 | if (self->IsExceptionPending() || h == nullptr) { |
| 364 | self->ClearException(); |
| 365 | |
| 366 | // Try a smaller length |
| 367 | length = length / 2; |
| 368 | // Use at most a quarter the reported free space. |
| 369 | size_t mem = Runtime::Current()->GetHeap()->GetFreeMemory(); |
| 370 | if (length * 4 > mem) { |
| 371 | length = mem / 4; |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // Allocate simple objects till it fails. |
| 377 | while (!self->IsExceptionPending()) { |
| 378 | handle_scope->NewHandle<mirror::Object>(c->AllocObject(self)); |
| 379 | } |
| 380 | self->ClearException(); |
| 381 | } |
| 382 | |
| 383 | void CommonRuntimeTestImpl::SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options) { |
| 384 | // Use a smaller heap |
| 385 | bool found = false; |
| 386 | for (std::pair<std::string, const void*>& pair : *options) { |
| 387 | if (pair.first.find("-Xmx") == 0) { |
| 388 | pair.first = "-Xmx4M"; // Smallest we can go. |
| 389 | found = true; |
| 390 | } |
| 391 | } |
| 392 | if (!found) { |
| 393 | options->emplace_back("-Xmx4M", nullptr); |
| 394 | } |
| 395 | } |
| 396 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 397 | CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 398 | vm_->SetCheckJniAbortHook(Hook, &actual_); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | CheckJniAbortCatcher::~CheckJniAbortCatcher() { |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 402 | vm_->SetCheckJniAbortHook(nullptr, nullptr); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 403 | EXPECT_TRUE(actual_.empty()) << actual_; |
| 404 | } |
| 405 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 406 | void CheckJniAbortCatcher::Check(const std::string& expected_text) { |
| 407 | Check(expected_text.c_str()); |
| 408 | } |
| 409 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 410 | void CheckJniAbortCatcher::Check(const char* expected_text) { |
| 411 | EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n" |
| 412 | << "Expected to find: " << expected_text << "\n" |
| 413 | << "In the output : " << actual_; |
| 414 | actual_.clear(); |
| 415 | } |
| 416 | |
| 417 | void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) { |
| 418 | // We use += because when we're hooking the aborts like this, multiple problems can be found. |
| 419 | *reinterpret_cast<std::string*>(data) += reason; |
| 420 | } |
| 421 | |
| 422 | } // namespace art |