blob: 135dc7be73219308bad3c75a87d58b0a3f93f2f2 [file] [log] [blame]
Elliott Hugheseb02a122012-06-12 11:35:40 -07001/*
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 Rogerse63db272014-07-15 15:36:11 -070017#include "common_runtime_test.h"
18
19#include <dirent.h>
20#include <dlfcn.h>
21#include <fcntl.h>
Andreas Gampe369810a2015-01-14 19:53:31 -080022#include <stdlib.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070023#include <cstdio>
Andreas Gampe373a9b52017-10-18 09:01:57 -070024#include "nativehelper/scoped_local_ref.h"
Ian Rogerse63db272014-07-15 15:36:11 -070025
Andreas Gampe46ee31b2016-12-14 10:11:49 -080026#include "android-base/stringprintf.h"
27
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "art_field-inl.h"
David Sehr891a50e2017-10-27 17:01:07 -070029#include "base/file_utils.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080030#include "base/logging.h"
Steven Morelande431e272017-07-18 16:53:49 -070031#include "base/macros.h"
David Sehr79e26072018-04-06 17:58:50 -070032#include "base/mem_map.h"
David Sehrc431b9d2018-03-02 12:01:51 -080033#include "base/mutex.h"
34#include "base/os.h"
Andreas Gampedcc528d2017-12-07 13:37:10 -080035#include "base/runtime_debug.h"
Ian Rogerse63db272014-07-15 15:36:11 -070036#include "base/stl_util.h"
Ian Rogerse63db272014-07-15 15:36:11 -070037#include "base/unix_file/fd_file.h"
38#include "class_linker.h"
Andreas Gampeb8e7c372018-02-20 18:24:55 -080039#include "class_loader_utils.h"
Ian Rogerse63db272014-07-15 15:36:11 -070040#include "compiler_callbacks.h"
David Sehr013fd802018-01-11 22:55:24 -080041#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080042#include "dex/dex_file-inl.h"
43#include "dex/dex_file_loader.h"
David Sehr67bf42e2018-02-26 16:43:04 -080044#include "dex/primitive.h"
Ian Rogerse63db272014-07-15 15:36:11 -070045#include "gc/heap.h"
Vladimir Markoec91d482018-11-29 12:17:11 +000046#include "gc/space/image_space.h"
Steven Morelande431e272017-07-18 16:53:49 -070047#include "gc_root-inl.h"
Elliott Hugheseb02a122012-06-12 11:35:40 -070048#include "gtest/gtest.h"
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070049#include "handle_scope-inl.h"
Andreas Gampe9b5cba42015-03-11 09:53:50 -070050#include "interpreter/unstarted_runtime.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010051#include "jni/java_vm_ext.h"
52#include "jni/jni_internal.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070053#include "mirror/class-alloc-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070054#include "mirror/class-inl.h"
Vladimir Markobdc93b42019-03-29 16:12:04 +000055#include "mirror/class_loader-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070056#include "mirror/object_array-alloc-inl.h"
Mathieu Chartiere58991b2015-10-13 07:59:34 -070057#include "native/dalvik_system_DexFile.h"
Ian Rogerse63db272014-07-15 15:36:11 -070058#include "noop_compiler_callbacks.h"
Ian Rogerse63db272014-07-15 15:36:11 -070059#include "runtime-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070060#include "scoped_thread_state_change-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070061#include "thread.h"
62#include "well_known_classes.h"
Elliott Hugheseb02a122012-06-12 11:35:40 -070063
Ian Rogerse63db272014-07-15 15:36:11 -070064namespace art {
65
Andreas Gampe46ee31b2016-12-14 10:11:49 -080066using android::base::StringPrintf;
67
Andreas Gampe9b5cba42015-03-11 09:53:50 -070068static bool unstarted_initialized_ = false;
69
Andreas Gampe48864112017-01-19 17:23:17 -080070CommonRuntimeTestImpl::CommonRuntimeTestImpl()
71 : class_linker_(nullptr), java_lang_dex_file_(nullptr) {
72}
Mathieu Chartier91c91162016-01-15 09:48:15 -080073
74CommonRuntimeTestImpl::~CommonRuntimeTestImpl() {
Richard Uhlerfbef44d2014-12-23 09:48:51 -080075 // Ensure the dex files are cleaned up before the runtime.
76 loaded_dex_files_.clear();
77 runtime_.reset();
78}
Ian Rogerse63db272014-07-15 15:36:11 -070079
Mathieu Chartier91c91162016-01-15 09:48:15 -080080std::string CommonRuntimeTestImpl::GetAndroidTargetToolsDir(InstructionSet isa) {
David Srbecky3e52aa42015-04-12 07:45:18 +010081 switch (isa) {
Vladimir Marko33bff252017-11-01 14:35:42 +000082 case InstructionSet::kArm:
83 case InstructionSet::kThumb2:
David Srbecky3e52aa42015-04-12 07:45:18 +010084 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/arm",
85 "arm-linux-androideabi",
86 "arm-linux-androideabi");
Vladimir Marko33bff252017-11-01 14:35:42 +000087 case InstructionSet::kArm64:
David Srbecky3e52aa42015-04-12 07:45:18 +010088 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/aarch64",
89 "aarch64-linux-android",
90 "aarch64-linux-android");
Vladimir Marko33bff252017-11-01 14:35:42 +000091 case InstructionSet::kX86:
92 case InstructionSet::kX86_64:
David Srbecky3e52aa42015-04-12 07:45:18 +010093 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/x86",
94 "x86_64-linux-android",
95 "x86_64-linux-android");
Vladimir Marko33bff252017-11-01 14:35:42 +000096 case InstructionSet::kMips:
97 case InstructionSet::kMips64:
David Srbecky3e52aa42015-04-12 07:45:18 +010098 return GetAndroidToolsDir("prebuilts/gcc/linux-x86/mips",
99 "mips64el-linux-android",
100 "mips64el-linux-android");
Vladimir Marko33bff252017-11-01 14:35:42 +0000101 case InstructionSet::kNone:
David Srbecky3e52aa42015-04-12 07:45:18 +0100102 break;
103 }
104 ADD_FAILURE() << "Invalid isa " << isa;
105 return "";
106}
107
Mathieu Chartier91c91162016-01-15 09:48:15 -0800108void CommonRuntimeTestImpl::SetUp() {
David Sehrd5f8de82018-04-27 14:12:03 -0700109 CommonArtTestImpl::SetUp();
Ian Rogerse63db272014-07-15 15:36:11 -0700110
Ian Rogerse63db272014-07-15 15:36:11 -0700111 std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
112 std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));
113
Ian Rogerse63db272014-07-15 15:36:11 -0700114 RuntimeOptions options;
Vladimir Markoec91d482018-11-29 12:17:11 +0000115 std::string boot_class_path_string =
116 GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames());
117 std::string boot_class_path_locations_string =
118 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations());
Narayan Kamathd1ef4362015-11-12 11:49:06 +0000119
Richard Uhlerc2752592015-01-02 13:28:22 -0800120 options.push_back(std::make_pair(boot_class_path_string, nullptr));
Vladimir Markoec91d482018-11-29 12:17:11 +0000121 options.push_back(std::make_pair(boot_class_path_locations_string, nullptr));
Ian Rogerse63db272014-07-15 15:36:11 -0700122 options.push_back(std::make_pair("-Xcheck:jni", nullptr));
Richard Uhlerc2752592015-01-02 13:28:22 -0800123 options.push_back(std::make_pair(min_heap_string, nullptr));
124 options.push_back(std::make_pair(max_heap_string, nullptr));
Andreas Gampe591b1d22019-06-25 13:08:16 -0700125
126 // Technically this is redundant w/ common_art_test, but still check.
Andreas Gampe1c5b42f2017-06-15 18:20:45 -0700127 options.push_back(std::make_pair("-XX:SlowDebug=true", nullptr));
Andreas Gampe46c4c852017-06-21 19:49:08 -0700128 static bool gSlowDebugTestFlag = false;
129 RegisterRuntimeDebugFlag(&gSlowDebugTestFlag);
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700130
131 callbacks_.reset(new NoopCompilerCallbacks());
132
Ian Rogerse63db272014-07-15 15:36:11 -0700133 SetUpRuntimeOptions(&options);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800134
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700135 // Install compiler-callbacks if SetupRuntimeOptions hasn't deleted them.
136 if (callbacks_.get() != nullptr) {
137 options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
138 }
139
Richard Uhler66d874d2015-01-15 09:37:19 -0800140 PreRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700141 if (!Runtime::Create(options, false)) {
142 LOG(FATAL) << "Failed to create runtime";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800143 UNREACHABLE();
Ian Rogerse63db272014-07-15 15:36:11 -0700144 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800145 PostRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700146 runtime_.reset(Runtime::Current());
147 class_linker_ = runtime_->GetClassLinker();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700148
Andreas Gampea00f0122015-12-16 16:54:35 -0800149 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
150 // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
151 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
152
153 // Get the boot class path from the runtime so it can be used in tests.
154 boot_class_path_ = class_linker_->GetBootClassPath();
155 ASSERT_FALSE(boot_class_path_.empty());
156 java_lang_dex_file_ = boot_class_path_[0];
157
158 FinalizeSetup();
Andreas Gampe46c4c852017-06-21 19:49:08 -0700159
160 // Ensure that we're really running with debug checks enabled.
161 CHECK(gSlowDebugTestFlag);
Andreas Gampea00f0122015-12-16 16:54:35 -0800162}
163
Mathieu Chartier91c91162016-01-15 09:48:15 -0800164void CommonRuntimeTestImpl::FinalizeSetup() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700165 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
166 // set up.
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700167 if (!unstarted_initialized_) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700168 interpreter::UnstartedRuntime::Initialize();
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700169 unstarted_initialized_ = true;
170 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700171
Andreas Gampea00f0122015-12-16 16:54:35 -0800172 {
173 ScopedObjectAccess soa(Thread::Current());
Vladimir Markodcfcce42018-06-27 10:00:28 +0000174 runtime_->RunRootClinits(soa.Self());
Andreas Gampea00f0122015-12-16 16:54:35 -0800175 }
Ian Rogerse63db272014-07-15 15:36:11 -0700176
177 // We're back in native, take the opportunity to initialize well known classes.
178 WellKnownClasses::Init(Thread::Current()->GetJniEnv());
179
180 // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread
181 // pool is created by the runtime.
182 runtime_->GetHeap()->CreateThreadPool();
183 runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700184 // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject).
185 runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U);
Ian Rogerse63db272014-07-15 15:36:11 -0700186}
187
Mathieu Chartier91c91162016-01-15 09:48:15 -0800188void CommonRuntimeTestImpl::TearDown() {
David Sehrd5f8de82018-04-27 14:12:03 -0700189 CommonArtTestImpl::TearDown();
Andreas Gampe48864112017-01-19 17:23:17 -0800190 if (runtime_ != nullptr) {
191 runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption after the test
192 }
Ian Rogerse63db272014-07-15 15:36:11 -0700193}
194
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700195// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
196#ifdef ART_TARGET
197#ifndef ART_TARGET_NATIVETEST_DIR
198#error "ART_TARGET_NATIVETEST_DIR not set."
199#endif
200// Wrap it as a string literal.
201#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
202#else
203#define ART_TARGET_NATIVETEST_DIR_STRING ""
204#endif
205
Mathieu Chartier91c91162016-01-15 09:48:15 -0800206std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(jobject jclass_loader) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700207 ScopedObjectAccess soa(Thread::Current());
208
Calin Juravlec79470d2017-07-12 17:37:42 -0700209 StackHandleScope<1> hs(soa.Self());
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700210 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700211 soa.Decode<mirror::ClassLoader>(jclass_loader));
Calin Juravlec79470d2017-07-12 17:37:42 -0700212 return GetDexFiles(soa, class_loader);
213}
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700214
Calin Juravlec79470d2017-07-12 17:37:42 -0700215std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(
216 ScopedObjectAccess& soa,
217 Handle<mirror::ClassLoader> class_loader) {
Calin Juravlec79470d2017-07-12 17:37:42 -0700218 DCHECK(
219 (class_loader->GetClass() ==
220 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)) ||
221 (class_loader->GetClass() ==
222 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DelegateLastClassLoader)));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700223
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800224 std::vector<const DexFile*> ret;
225 VisitClassLoaderDexFiles(soa,
226 class_loader,
227 [&](const DexFile* cp_dex_file) {
228 if (cp_dex_file == nullptr) {
229 LOG(WARNING) << "Null DexFile";
230 } else {
231 ret.push_back(cp_dex_file);
232 }
233 return true;
234 });
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700235 return ret;
236}
237
Mathieu Chartier91c91162016-01-15 09:48:15 -0800238const DexFile* CommonRuntimeTestImpl::GetFirstDexFile(jobject jclass_loader) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700239 std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader));
240 DCHECK(!tmp.empty());
241 const DexFile* ret = tmp[0];
242 DCHECK(ret != nullptr);
243 return ret;
244}
245
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100246jobject CommonRuntimeTestImpl::LoadMultiDex(const char* first_dex_name,
247 const char* second_dex_name) {
248 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles(first_dex_name);
249 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles(second_dex_name);
250 std::vector<const DexFile*> class_path;
251 CHECK_NE(0U, first_dex_files.size());
252 CHECK_NE(0U, second_dex_files.size());
253 for (auto& dex_file : first_dex_files) {
254 class_path.push_back(dex_file.get());
255 loaded_dex_files_.push_back(std::move(dex_file));
256 }
257 for (auto& dex_file : second_dex_files) {
258 class_path.push_back(dex_file.get());
259 loaded_dex_files_.push_back(std::move(dex_file));
260 }
261
262 Thread* self = Thread::Current();
263 jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self,
264 class_path);
265 self->SetClassLoaderOverride(class_loader);
266 return class_loader;
267}
268
Mathieu Chartier91c91162016-01-15 09:48:15 -0800269jobject CommonRuntimeTestImpl::LoadDex(const char* dex_name) {
Calin Juravle7865ac72017-06-28 11:03:12 -0700270 jobject class_loader = LoadDexInPathClassLoader(dex_name, nullptr);
271 Thread::Current()->SetClassLoaderOverride(class_loader);
272 return class_loader;
273}
274
275jobject CommonRuntimeTestImpl::LoadDexInWellKnownClassLoader(const std::string& dex_name,
276 jclass loader_class,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000277 jobject parent_loader,
278 jobject shared_libraries) {
Calin Juravle7865ac72017-06-28 11:03:12 -0700279 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name.c_str());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800280 std::vector<const DexFile*> class_path;
Ian Rogerse63db272014-07-15 15:36:11 -0700281 CHECK_NE(0U, dex_files.size());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800282 for (auto& dex_file : dex_files) {
283 class_path.push_back(dex_file.get());
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800284 loaded_dex_files_.push_back(std::move(dex_file));
Ian Rogerse63db272014-07-15 15:36:11 -0700285 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700286 Thread* self = Thread::Current();
Calin Juravle7865ac72017-06-28 11:03:12 -0700287 ScopedObjectAccess soa(self);
288
289 jobject result = Runtime::Current()->GetClassLinker()->CreateWellKnownClassLoader(
290 self,
291 class_path,
292 loader_class,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000293 parent_loader,
294 shared_libraries);
Calin Juravle7865ac72017-06-28 11:03:12 -0700295
296 {
297 // Verify we build the correct chain.
298
299 ObjPtr<mirror::ClassLoader> actual_class_loader = soa.Decode<mirror::ClassLoader>(result);
300 // Verify that the result has the correct class.
301 CHECK_EQ(soa.Decode<mirror::Class>(loader_class), actual_class_loader->GetClass());
302 // Verify that the parent is not null. The boot class loader will be set up as a
303 // proper object.
304 ObjPtr<mirror::ClassLoader> actual_parent(actual_class_loader->GetParent());
305 CHECK(actual_parent != nullptr);
306
307 if (parent_loader != nullptr) {
308 // We were given a parent. Verify that it's what we expect.
309 ObjPtr<mirror::ClassLoader> expected_parent = soa.Decode<mirror::ClassLoader>(parent_loader);
310 CHECK_EQ(expected_parent, actual_parent);
311 } else {
312 // No parent given. The parent must be the BootClassLoader.
313 CHECK(Runtime::Current()->GetClassLinker()->IsBootClassLoader(soa, actual_parent));
314 }
315 }
316
317 return result;
318}
319
320jobject CommonRuntimeTestImpl::LoadDexInPathClassLoader(const std::string& dex_name,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000321 jobject parent_loader,
322 jobject shared_libraries) {
Calin Juravle7865ac72017-06-28 11:03:12 -0700323 return LoadDexInWellKnownClassLoader(dex_name,
324 WellKnownClasses::dalvik_system_PathClassLoader,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000325 parent_loader,
326 shared_libraries);
Calin Juravle7865ac72017-06-28 11:03:12 -0700327}
328
329jobject CommonRuntimeTestImpl::LoadDexInDelegateLastClassLoader(const std::string& dex_name,
330 jobject parent_loader) {
331 return LoadDexInWellKnownClassLoader(dex_name,
332 WellKnownClasses::dalvik_system_DelegateLastClassLoader,
333 parent_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700334}
335
David Brazdil1a9ac532019-03-05 11:57:13 +0000336jobject CommonRuntimeTestImpl::LoadDexInInMemoryDexClassLoader(const std::string& dex_name,
337 jobject parent_loader) {
338 return LoadDexInWellKnownClassLoader(dex_name,
339 WellKnownClasses::dalvik_system_InMemoryDexClassLoader,
340 parent_loader);
341}
342
Andreas Gampe26761f72017-07-20 18:00:39 -0700343void 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
383void 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
Vladimir Marko2b076df2019-02-20 11:27:52 +0000397void CommonRuntimeTestImpl::MakeInterpreted(ObjPtr<mirror::Class> klass) {
398 PointerSize pointer_size = class_linker_->GetImagePointerSize();
399 for (ArtMethod& method : klass->GetMethods(pointer_size)) {
400 class_linker_->SetEntryPointsToInterpreter(&method);
401 }
402}
403
Vladimir Markoec91d482018-11-29 12:17:11 +0000404bool CommonRuntimeTestImpl::StartDex2OatCommandLine(/*out*/std::vector<std::string>* argv,
405 /*out*/std::string* error_msg) {
406 DCHECK(argv != nullptr);
407 DCHECK(argv->empty());
408
409 Runtime* runtime = Runtime::Current();
410 const std::vector<gc::space::ImageSpace*>& image_spaces =
411 runtime->GetHeap()->GetBootImageSpaces();
412 if (image_spaces.empty()) {
413 *error_msg = "No image location found for Dex2Oat.";
414 return false;
415 }
416 std::string image_location = image_spaces[0]->GetImageLocation();
417
418 argv->push_back(runtime->GetCompilerExecutable());
419 if (runtime->IsJavaDebuggable()) {
420 argv->push_back("--debuggable");
421 }
422 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(argv);
423
424 argv->push_back("--runtime-arg");
425 argv->push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
426 argv->push_back("--runtime-arg");
427 argv->push_back(GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
428
429 argv->push_back("--boot-image=" + image_location);
430
431 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
432 argv->insert(argv->end(), compiler_options.begin(), compiler_options.end());
433 return true;
434}
435
Vladimir Marko672c0802019-07-26 13:03:13 +0100436std::string CommonRuntimeTestImpl::GetImageDirectory() {
437 if (IsHost()) {
438 const char* host_dir = getenv("ANDROID_HOST_OUT");
439 CHECK(host_dir != nullptr);
440 return std::string(host_dir) + "/framework";
441 } else {
442 return std::string("/data/art-test");
443 }
444}
445
446std::string CommonRuntimeTestImpl::GetImageLocation() {
447 return GetImageDirectory() + "/core.art";
448}
449
450std::string CommonRuntimeTestImpl::GetSystemImageFile() {
451 return GetImageDirectory() + "/" + GetInstructionSetString(kRuntimeISA) + "/core.art";
452}
453
454void CommonRuntimeTestImpl::EnterTransactionMode() {
455 CHECK(!Runtime::Current()->IsActiveTransaction());
456 Runtime::Current()->EnterTransactionMode(/*strict=*/ false, /*root=*/ nullptr);
457}
458
459void CommonRuntimeTestImpl::ExitTransactionMode() {
460 Runtime::Current()->ExitTransactionMode();
461 CHECK(!Runtime::Current()->IsActiveTransaction());
462}
463
464void CommonRuntimeTestImpl::RollbackAndExitTransactionMode() {
465 Runtime::Current()->RollbackAndExitTransactionMode();
466 CHECK(!Runtime::Current()->IsActiveTransaction());
467}
468
469bool CommonRuntimeTestImpl::IsTransactionAborted() {
470 return Runtime::Current()->IsTransactionAborted();
471}
472
Ian Rogerse63db272014-07-15 15:36:11 -0700473CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700474 vm_->SetCheckJniAbortHook(Hook, &actual_);
Ian Rogerse63db272014-07-15 15:36:11 -0700475}
476
477CheckJniAbortCatcher::~CheckJniAbortCatcher() {
Ian Rogers68d8b422014-07-17 11:09:10 -0700478 vm_->SetCheckJniAbortHook(nullptr, nullptr);
Ian Rogerse63db272014-07-15 15:36:11 -0700479 EXPECT_TRUE(actual_.empty()) << actual_;
480}
481
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700482void CheckJniAbortCatcher::Check(const std::string& expected_text) {
483 Check(expected_text.c_str());
484}
485
Ian Rogerse63db272014-07-15 15:36:11 -0700486void CheckJniAbortCatcher::Check(const char* expected_text) {
487 EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
488 << "Expected to find: " << expected_text << "\n"
489 << "In the output : " << actual_;
490 actual_.clear();
491}
492
493void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
494 // We use += because when we're hooking the aborts like this, multiple problems can be found.
495 *reinterpret_cast<std::string*>(data) += reason;
496}
497
498} // namespace art
Andreas Gampec857f4a2018-10-25 13:12:37 -0700499
500// Allow other test code to run global initialization/configuration before
501// gtest infra takes over.
502extern "C"
503__attribute__((visibility("default"))) __attribute__((weak))
504void ArtTestGlobalInit() {
Andreas Gampec857f4a2018-10-25 13:12:37 -0700505}
506
507int main(int argc, char **argv) {
508 // Gtests can be very noisy. For example, an executable with multiple tests will trigger native
509 // bridge warnings. The following line reduces the minimum log severity to ERROR and suppresses
510 // everything else. In case you want to see all messages, comment out the line.
511 setenv("ANDROID_LOG_TAGS", "*:e", 1);
512
513 art::Locks::Init();
514 art::InitLogging(argv, art::Runtime::Abort);
515 art::MemMap::Init();
516 LOG(INFO) << "Running main() from common_runtime_test.cc...";
517 testing::InitGoogleTest(&argc, argv);
518 ArtTestGlobalInit();
519 return RUN_ALL_TESTS();
520}