blob: 4eb34083d1913e840a1d16d40b062a72b9e7b6a2 [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"
Vladimir Markofdd46842020-03-25 14:57:17 +000044#include "dex/method_reference.h"
David Sehr67bf42e2018-02-26 16:43:04 -080045#include "dex/primitive.h"
Vladimir Markofdd46842020-03-25 14:57:17 +000046#include "dex/type_reference.h"
Ian Rogerse63db272014-07-15 15:36:11 -070047#include "gc/heap.h"
Vladimir Markoec91d482018-11-29 12:17:11 +000048#include "gc/space/image_space.h"
Steven Morelande431e272017-07-18 16:53:49 -070049#include "gc_root-inl.h"
Elliott Hugheseb02a122012-06-12 11:35:40 -070050#include "gtest/gtest.h"
Andreas Gampe81c6f8d2015-03-25 17:19:53 -070051#include "handle_scope-inl.h"
Andreas Gampe9b5cba42015-03-11 09:53:50 -070052#include "interpreter/unstarted_runtime.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010053#include "jni/java_vm_ext.h"
54#include "jni/jni_internal.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070055#include "mirror/class-alloc-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070056#include "mirror/class-inl.h"
Vladimir Markobdc93b42019-03-29 16:12:04 +000057#include "mirror/class_loader-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070058#include "mirror/object_array-alloc-inl.h"
Mathieu Chartiere58991b2015-10-13 07:59:34 -070059#include "native/dalvik_system_DexFile.h"
Ian Rogerse63db272014-07-15 15:36:11 -070060#include "noop_compiler_callbacks.h"
Vladimir Markofdd46842020-03-25 14:57:17 +000061#include "profile/profile_compilation_info.h"
Ian Rogerse63db272014-07-15 15:36:11 -070062#include "runtime-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070063#include "scoped_thread_state_change-inl.h"
Ian Rogerse63db272014-07-15 15:36:11 -070064#include "thread.h"
65#include "well_known_classes.h"
Elliott Hugheseb02a122012-06-12 11:35:40 -070066
Ian Rogerse63db272014-07-15 15:36:11 -070067namespace art {
68
Andreas Gampe46ee31b2016-12-14 10:11:49 -080069using android::base::StringPrintf;
70
Andreas Gampe9b5cba42015-03-11 09:53:50 -070071static bool unstarted_initialized_ = false;
72
Andreas Gampe48864112017-01-19 17:23:17 -080073CommonRuntimeTestImpl::CommonRuntimeTestImpl()
74 : class_linker_(nullptr), java_lang_dex_file_(nullptr) {
75}
Mathieu Chartier91c91162016-01-15 09:48:15 -080076
77CommonRuntimeTestImpl::~CommonRuntimeTestImpl() {
Richard Uhlerfbef44d2014-12-23 09:48:51 -080078 // Ensure the dex files are cleaned up before the runtime.
79 loaded_dex_files_.clear();
80 runtime_.reset();
81}
Ian Rogerse63db272014-07-15 15:36:11 -070082
Mathieu Chartier91c91162016-01-15 09:48:15 -080083void CommonRuntimeTestImpl::SetUp() {
David Sehrd5f8de82018-04-27 14:12:03 -070084 CommonArtTestImpl::SetUp();
Ian Rogerse63db272014-07-15 15:36:11 -070085
Ian Rogerse63db272014-07-15 15:36:11 -070086 std::string min_heap_string(StringPrintf("-Xms%zdm", gc::Heap::kDefaultInitialSize / MB));
87 std::string max_heap_string(StringPrintf("-Xmx%zdm", gc::Heap::kDefaultMaximumSize / MB));
88
Ian Rogerse63db272014-07-15 15:36:11 -070089 RuntimeOptions options;
Vladimir Markoec91d482018-11-29 12:17:11 +000090 std::string boot_class_path_string =
91 GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames());
92 std::string boot_class_path_locations_string =
93 GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations());
Narayan Kamathd1ef4362015-11-12 11:49:06 +000094
Richard Uhlerc2752592015-01-02 13:28:22 -080095 options.push_back(std::make_pair(boot_class_path_string, nullptr));
Vladimir Markoec91d482018-11-29 12:17:11 +000096 options.push_back(std::make_pair(boot_class_path_locations_string, nullptr));
Ian Rogerse63db272014-07-15 15:36:11 -070097 options.push_back(std::make_pair("-Xcheck:jni", nullptr));
Richard Uhlerc2752592015-01-02 13:28:22 -080098 options.push_back(std::make_pair(min_heap_string, nullptr));
99 options.push_back(std::make_pair(max_heap_string, nullptr));
Andreas Gampe591b1d22019-06-25 13:08:16 -0700100
101 // Technically this is redundant w/ common_art_test, but still check.
Andreas Gampe1c5b42f2017-06-15 18:20:45 -0700102 options.push_back(std::make_pair("-XX:SlowDebug=true", nullptr));
Andreas Gampe46c4c852017-06-21 19:49:08 -0700103 static bool gSlowDebugTestFlag = false;
104 RegisterRuntimeDebugFlag(&gSlowDebugTestFlag);
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700105
106 callbacks_.reset(new NoopCompilerCallbacks());
107
Ian Rogerse63db272014-07-15 15:36:11 -0700108 SetUpRuntimeOptions(&options);
Igor Murashkinaaebaa02015-01-26 10:55:53 -0800109
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700110 // Install compiler-callbacks if SetupRuntimeOptions hasn't deleted them.
111 if (callbacks_.get() != nullptr) {
112 options.push_back(std::make_pair("compilercallbacks", callbacks_.get()));
113 }
114
Richard Uhler66d874d2015-01-15 09:37:19 -0800115 PreRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700116 if (!Runtime::Create(options, false)) {
117 LOG(FATAL) << "Failed to create runtime";
Elliott Hughesc1896c92018-11-29 11:33:18 -0800118 UNREACHABLE();
Ian Rogerse63db272014-07-15 15:36:11 -0700119 }
Richard Uhler66d874d2015-01-15 09:37:19 -0800120 PostRuntimeCreate();
Ian Rogerse63db272014-07-15 15:36:11 -0700121 runtime_.reset(Runtime::Current());
122 class_linker_ = runtime_->GetClassLinker();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700123
Andreas Gampea00f0122015-12-16 16:54:35 -0800124 // Runtime::Create acquired the mutator_lock_ that is normally given away when we
125 // Runtime::Start, give it away now and then switch to a more managable ScopedObjectAccess.
126 Thread::Current()->TransitionFromRunnableToSuspended(kNative);
127
128 // Get the boot class path from the runtime so it can be used in tests.
129 boot_class_path_ = class_linker_->GetBootClassPath();
130 ASSERT_FALSE(boot_class_path_.empty());
131 java_lang_dex_file_ = boot_class_path_[0];
132
133 FinalizeSetup();
Andreas Gampe46c4c852017-06-21 19:49:08 -0700134
Roland Levillainf0409142021-03-22 15:45:03 +0000135 if (kIsDebugBuild) {
136 // Ensure that we're really running with debug checks enabled.
137 CHECK(gSlowDebugTestFlag);
138 }
Andreas Gampea00f0122015-12-16 16:54:35 -0800139}
140
Mathieu Chartier91c91162016-01-15 09:48:15 -0800141void CommonRuntimeTestImpl::FinalizeSetup() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700142 // Initialize maps for unstarted runtime. This needs to be here, as running clinits needs this
143 // set up.
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700144 if (!unstarted_initialized_) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700145 interpreter::UnstartedRuntime::Initialize();
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700146 unstarted_initialized_ = true;
Vladimir Marko83881482021-01-07 10:59:54 +0000147 } else {
148 interpreter::UnstartedRuntime::Reinitialize();
Andreas Gampe9b5cba42015-03-11 09:53:50 -0700149 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700150
Andreas Gampea00f0122015-12-16 16:54:35 -0800151 {
152 ScopedObjectAccess soa(Thread::Current());
Vladimir Markodcfcce42018-06-27 10:00:28 +0000153 runtime_->RunRootClinits(soa.Self());
Andreas Gampea00f0122015-12-16 16:54:35 -0800154 }
Ian Rogerse63db272014-07-15 15:36:11 -0700155
156 // We're back in native, take the opportunity to initialize well known classes.
157 WellKnownClasses::Init(Thread::Current()->GetJniEnv());
158
159 // Create the heap thread pool so that the GC runs in parallel for tests. Normally, the thread
160 // pool is created by the runtime.
161 runtime_->GetHeap()->CreateThreadPool();
162 runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption before the test
Hiroshi Yamauchi4460a842015-03-09 11:57:48 -0700163 // Reduce timinig-dependent flakiness in OOME behavior (eg StubTest.AllocObject).
164 runtime_->GetHeap()->SetMinIntervalHomogeneousSpaceCompactionByOom(0U);
Ian Rogerse63db272014-07-15 15:36:11 -0700165}
166
Mathieu Chartier91c91162016-01-15 09:48:15 -0800167void CommonRuntimeTestImpl::TearDown() {
David Sehrd5f8de82018-04-27 14:12:03 -0700168 CommonArtTestImpl::TearDown();
Andreas Gampe48864112017-01-19 17:23:17 -0800169 if (runtime_ != nullptr) {
170 runtime_->GetHeap()->VerifyHeap(); // Check for heap corruption after the test
171 }
Ian Rogerse63db272014-07-15 15:36:11 -0700172}
173
Andreas Gampe1fe5e5c2014-07-11 21:14:35 -0700174// Check that for target builds we have ART_TARGET_NATIVETEST_DIR set.
175#ifdef ART_TARGET
176#ifndef ART_TARGET_NATIVETEST_DIR
177#error "ART_TARGET_NATIVETEST_DIR not set."
178#endif
179// Wrap it as a string literal.
180#define ART_TARGET_NATIVETEST_DIR_STRING STRINGIFY(ART_TARGET_NATIVETEST_DIR) "/"
181#else
182#define ART_TARGET_NATIVETEST_DIR_STRING ""
183#endif
184
Mathieu Chartier91c91162016-01-15 09:48:15 -0800185std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(jobject jclass_loader) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700186 ScopedObjectAccess soa(Thread::Current());
187
Calin Juravlec79470d2017-07-12 17:37:42 -0700188 StackHandleScope<1> hs(soa.Self());
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700189 Handle<mirror::ClassLoader> class_loader = hs.NewHandle(
Mathieu Chartier0795f232016-09-27 18:43:30 -0700190 soa.Decode<mirror::ClassLoader>(jclass_loader));
Calin Juravlec79470d2017-07-12 17:37:42 -0700191 return GetDexFiles(soa, class_loader);
192}
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700193
Calin Juravlec79470d2017-07-12 17:37:42 -0700194std::vector<const DexFile*> CommonRuntimeTestImpl::GetDexFiles(
195 ScopedObjectAccess& soa,
196 Handle<mirror::ClassLoader> class_loader) {
Calin Juravlec79470d2017-07-12 17:37:42 -0700197 DCHECK(
198 (class_loader->GetClass() ==
199 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_PathClassLoader)) ||
200 (class_loader->GetClass() ==
201 soa.Decode<mirror::Class>(WellKnownClasses::dalvik_system_DelegateLastClassLoader)));
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700202
Andreas Gampeb8e7c372018-02-20 18:24:55 -0800203 std::vector<const DexFile*> ret;
204 VisitClassLoaderDexFiles(soa,
205 class_loader,
206 [&](const DexFile* cp_dex_file) {
207 if (cp_dex_file == nullptr) {
208 LOG(WARNING) << "Null DexFile";
209 } else {
210 ret.push_back(cp_dex_file);
211 }
212 return true;
213 });
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700214 return ret;
215}
216
Mathieu Chartier91c91162016-01-15 09:48:15 -0800217const DexFile* CommonRuntimeTestImpl::GetFirstDexFile(jobject jclass_loader) {
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700218 std::vector<const DexFile*> tmp(GetDexFiles(jclass_loader));
219 DCHECK(!tmp.empty());
220 const DexFile* ret = tmp[0];
221 DCHECK(ret != nullptr);
222 return ret;
223}
224
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100225jobject CommonRuntimeTestImpl::LoadMultiDex(const char* first_dex_name,
226 const char* second_dex_name) {
227 std::vector<std::unique_ptr<const DexFile>> first_dex_files = OpenTestDexFiles(first_dex_name);
228 std::vector<std::unique_ptr<const DexFile>> second_dex_files = OpenTestDexFiles(second_dex_name);
229 std::vector<const DexFile*> class_path;
230 CHECK_NE(0U, first_dex_files.size());
231 CHECK_NE(0U, second_dex_files.size());
232 for (auto& dex_file : first_dex_files) {
233 class_path.push_back(dex_file.get());
234 loaded_dex_files_.push_back(std::move(dex_file));
235 }
236 for (auto& dex_file : second_dex_files) {
237 class_path.push_back(dex_file.get());
238 loaded_dex_files_.push_back(std::move(dex_file));
239 }
240
241 Thread* self = Thread::Current();
242 jobject class_loader = Runtime::Current()->GetClassLinker()->CreatePathClassLoader(self,
243 class_path);
244 self->SetClassLoaderOverride(class_loader);
245 return class_loader;
246}
247
Mathieu Chartier91c91162016-01-15 09:48:15 -0800248jobject CommonRuntimeTestImpl::LoadDex(const char* dex_name) {
Calin Juravle7865ac72017-06-28 11:03:12 -0700249 jobject class_loader = LoadDexInPathClassLoader(dex_name, nullptr);
250 Thread::Current()->SetClassLoaderOverride(class_loader);
251 return class_loader;
252}
253
Dan Zimmermanb682ea42019-12-23 06:59:06 -0800254jobject
255CommonRuntimeTestImpl::LoadDexInWellKnownClassLoader(const std::vector<std::string>& dex_names,
256 jclass loader_class,
257 jobject parent_loader,
Brad Stenning9c924e82021-10-11 19:09:00 -0700258 jobject shared_libraries,
259 jobject shared_libraries_after) {
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800260 std::vector<const DexFile*> class_path;
Dan Zimmermanb682ea42019-12-23 06:59:06 -0800261 for (const std::string& dex_name : dex_names) {
262 std::vector<std::unique_ptr<const DexFile>> dex_files = OpenTestDexFiles(dex_name.c_str());
263 CHECK_NE(0U, dex_files.size());
264 for (auto& dex_file : dex_files) {
265 class_path.push_back(dex_file.get());
266 loaded_dex_files_.push_back(std::move(dex_file));
267 }
Ian Rogerse63db272014-07-15 15:36:11 -0700268 }
Ian Rogers68d8b422014-07-17 11:09:10 -0700269 Thread* self = Thread::Current();
Calin Juravle7865ac72017-06-28 11:03:12 -0700270 ScopedObjectAccess soa(self);
271
272 jobject result = Runtime::Current()->GetClassLinker()->CreateWellKnownClassLoader(
273 self,
274 class_path,
275 loader_class,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000276 parent_loader,
Brad Stenning9c924e82021-10-11 19:09:00 -0700277 shared_libraries,
278 shared_libraries_after);
Calin Juravle7865ac72017-06-28 11:03:12 -0700279
280 {
281 // Verify we build the correct chain.
282
283 ObjPtr<mirror::ClassLoader> actual_class_loader = soa.Decode<mirror::ClassLoader>(result);
284 // Verify that the result has the correct class.
285 CHECK_EQ(soa.Decode<mirror::Class>(loader_class), actual_class_loader->GetClass());
286 // Verify that the parent is not null. The boot class loader will be set up as a
287 // proper object.
288 ObjPtr<mirror::ClassLoader> actual_parent(actual_class_loader->GetParent());
289 CHECK(actual_parent != nullptr);
290
291 if (parent_loader != nullptr) {
292 // We were given a parent. Verify that it's what we expect.
293 ObjPtr<mirror::ClassLoader> expected_parent = soa.Decode<mirror::ClassLoader>(parent_loader);
294 CHECK_EQ(expected_parent, actual_parent);
295 } else {
296 // No parent given. The parent must be the BootClassLoader.
297 CHECK(Runtime::Current()->GetClassLinker()->IsBootClassLoader(soa, actual_parent));
298 }
299 }
300
301 return result;
302}
303
304jobject CommonRuntimeTestImpl::LoadDexInPathClassLoader(const std::string& dex_name,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000305 jobject parent_loader,
Brad Stenning9c924e82021-10-11 19:09:00 -0700306 jobject shared_libraries,
307 jobject shared_libraries_after) {
Dan Zimmermanb682ea42019-12-23 06:59:06 -0800308 return LoadDexInPathClassLoader(std::vector<std::string>{ dex_name },
309 parent_loader,
Brad Stenning9c924e82021-10-11 19:09:00 -0700310 shared_libraries,
311 shared_libraries_after);
Dan Zimmermanb682ea42019-12-23 06:59:06 -0800312}
313
314jobject CommonRuntimeTestImpl::LoadDexInPathClassLoader(const std::vector<std::string>& names,
315 jobject parent_loader,
Brad Stenning9c924e82021-10-11 19:09:00 -0700316 jobject shared_libraries,
317 jobject shared_libraries_after) {
Dan Zimmermanb682ea42019-12-23 06:59:06 -0800318 return LoadDexInWellKnownClassLoader(names,
Calin Juravle7865ac72017-06-28 11:03:12 -0700319 WellKnownClasses::dalvik_system_PathClassLoader,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000320 parent_loader,
Brad Stenning9c924e82021-10-11 19:09:00 -0700321 shared_libraries,
322 shared_libraries_after);
Calin Juravle7865ac72017-06-28 11:03:12 -0700323}
324
325jobject CommonRuntimeTestImpl::LoadDexInDelegateLastClassLoader(const std::string& dex_name,
326 jobject parent_loader) {
Dan Zimmermanb682ea42019-12-23 06:59:06 -0800327 return LoadDexInWellKnownClassLoader({ dex_name },
Calin Juravle7865ac72017-06-28 11:03:12 -0700328 WellKnownClasses::dalvik_system_DelegateLastClassLoader,
329 parent_loader);
Ian Rogerse63db272014-07-15 15:36:11 -0700330}
331
David Brazdil1a9ac532019-03-05 11:57:13 +0000332jobject CommonRuntimeTestImpl::LoadDexInInMemoryDexClassLoader(const std::string& dex_name,
333 jobject parent_loader) {
Dan Zimmermanb682ea42019-12-23 06:59:06 -0800334 return LoadDexInWellKnownClassLoader({ dex_name },
David Brazdil1a9ac532019-03-05 11:57:13 +0000335 WellKnownClasses::dalvik_system_InMemoryDexClassLoader,
336 parent_loader);
337}
338
Andreas Gampe26761f72017-07-20 18:00:39 -0700339void CommonRuntimeTestImpl::FillHeap(Thread* self,
340 ClassLinker* class_linker,
341 VariableSizedHandleScope* handle_scope) {
342 DCHECK(handle_scope != nullptr);
343
344 Runtime::Current()->GetHeap()->SetIdealFootprint(1 * GB);
345
346 // Class java.lang.Object.
347 Handle<mirror::Class> c(handle_scope->NewHandle(
348 class_linker->FindSystemClass(self, "Ljava/lang/Object;")));
349 // Array helps to fill memory faster.
350 Handle<mirror::Class> ca(handle_scope->NewHandle(
351 class_linker->FindSystemClass(self, "[Ljava/lang/Object;")));
352
353 // Start allocating with ~128K
354 size_t length = 128 * KB;
355 while (length > 40) {
356 const int32_t array_length = length / 4; // Object[] has elements of size 4.
357 MutableHandle<mirror::Object> h(handle_scope->NewHandle<mirror::Object>(
358 mirror::ObjectArray<mirror::Object>::Alloc(self, ca.Get(), array_length)));
359 if (self->IsExceptionPending() || h == nullptr) {
360 self->ClearException();
361
362 // Try a smaller length
363 length = length / 2;
364 // Use at most a quarter the reported free space.
365 size_t mem = Runtime::Current()->GetHeap()->GetFreeMemory();
366 if (length * 4 > mem) {
367 length = mem / 4;
368 }
369 }
370 }
371
372 // Allocate simple objects till it fails.
373 while (!self->IsExceptionPending()) {
374 handle_scope->NewHandle<mirror::Object>(c->AllocObject(self));
375 }
376 self->ClearException();
377}
378
379void CommonRuntimeTestImpl::SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options) {
380 // Use a smaller heap
381 bool found = false;
382 for (std::pair<std::string, const void*>& pair : *options) {
383 if (pair.first.find("-Xmx") == 0) {
384 pair.first = "-Xmx4M"; // Smallest we can go.
385 found = true;
386 }
387 }
388 if (!found) {
389 options->emplace_back("-Xmx4M", nullptr);
390 }
391}
392
Vladimir Marko2b076df2019-02-20 11:27:52 +0000393void CommonRuntimeTestImpl::MakeInterpreted(ObjPtr<mirror::Class> klass) {
394 PointerSize pointer_size = class_linker_->GetImagePointerSize();
395 for (ArtMethod& method : klass->GetMethods(pointer_size)) {
396 class_linker_->SetEntryPointsToInterpreter(&method);
397 }
398}
399
Vladimir Markoec91d482018-11-29 12:17:11 +0000400bool CommonRuntimeTestImpl::StartDex2OatCommandLine(/*out*/std::vector<std::string>* argv,
Vladimir Markofdd46842020-03-25 14:57:17 +0000401 /*out*/std::string* error_msg,
402 bool use_runtime_bcp_and_image) {
Vladimir Markoec91d482018-11-29 12:17:11 +0000403 DCHECK(argv != nullptr);
404 DCHECK(argv->empty());
405
406 Runtime* runtime = Runtime::Current();
Vladimir Markofdd46842020-03-25 14:57:17 +0000407 if (use_runtime_bcp_and_image && runtime->GetHeap()->GetBootImageSpaces().empty()) {
Vladimir Markoec91d482018-11-29 12:17:11 +0000408 *error_msg = "No image location found for Dex2Oat.";
409 return false;
410 }
Vladimir Markoec91d482018-11-29 12:17:11 +0000411
412 argv->push_back(runtime->GetCompilerExecutable());
413 if (runtime->IsJavaDebuggable()) {
414 argv->push_back("--debuggable");
415 }
416 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(argv);
417
Vladimir Markofdd46842020-03-25 14:57:17 +0000418 if (use_runtime_bcp_and_image) {
419 argv->push_back("--runtime-arg");
420 argv->push_back(GetClassPathOption("-Xbootclasspath:", GetLibCoreDexFileNames()));
421 argv->push_back("--runtime-arg");
422 argv->push_back(GetClassPathOption("-Xbootclasspath-locations:", GetLibCoreDexLocations()));
Vladimir Markoec91d482018-11-29 12:17:11 +0000423
Vladimir Markofdd46842020-03-25 14:57:17 +0000424 const std::vector<gc::space::ImageSpace*>& image_spaces =
425 runtime->GetHeap()->GetBootImageSpaces();
426 DCHECK(!image_spaces.empty());
427 argv->push_back("--boot-image=" + image_spaces[0]->GetImageLocation());
428 }
Vladimir Markoec91d482018-11-29 12:17:11 +0000429
430 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
431 argv->insert(argv->end(), compiler_options.begin(), compiler_options.end());
432 return true;
433}
434
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000435bool CommonRuntimeTestImpl::CompileBootImage(const std::vector<std::string>& extra_args,
436 const std::string& image_file_name_prefix,
437 ArrayRef<const std::string> dex_files,
438 ArrayRef<const std::string> dex_locations,
439 std::string* error_msg,
440 const std::string& use_fd_prefix) {
441 Runtime* const runtime = Runtime::Current();
442 std::vector<std::string> argv {
443 runtime->GetCompilerExecutable(),
444 "--runtime-arg",
445 "-Xms64m",
446 "--runtime-arg",
447 "-Xmx64m",
448 "--runtime-arg",
449 "-Xverify:softfail",
450 };
451 CHECK_EQ(dex_files.size(), dex_locations.size());
452 for (const std::string& dex_file : dex_files) {
453 argv.push_back("--dex-file=" + dex_file);
454 }
455 for (const std::string& dex_location : dex_locations) {
456 argv.push_back("--dex-location=" + dex_location);
457 }
458 if (runtime->IsJavaDebuggable()) {
459 argv.push_back("--debuggable");
460 }
461 runtime->AddCurrentRuntimeFeaturesAsDex2OatArguments(&argv);
462
463 if (!kIsTargetBuild) {
464 argv.push_back("--host");
465 }
466
467 std::unique_ptr<File> art_file;
468 std::unique_ptr<File> vdex_file;
469 std::unique_ptr<File> oat_file;
470 if (!use_fd_prefix.empty()) {
471 art_file.reset(OS::CreateEmptyFile((use_fd_prefix + ".art").c_str()));
472 vdex_file.reset(OS::CreateEmptyFile((use_fd_prefix + ".vdex").c_str()));
473 oat_file.reset(OS::CreateEmptyFile((use_fd_prefix + ".oat").c_str()));
474 argv.push_back("--image-fd=" + std::to_string(art_file->Fd()));
475 argv.push_back("--output-vdex-fd=" + std::to_string(vdex_file->Fd()));
476 argv.push_back("--oat-fd=" + std::to_string(oat_file->Fd()));
477 argv.push_back("--oat-location=" + image_file_name_prefix + ".oat");
478 } else {
479 argv.push_back("--image=" + image_file_name_prefix + ".art");
480 argv.push_back("--oat-file=" + image_file_name_prefix + ".oat");
481 argv.push_back("--oat-location=" + image_file_name_prefix + ".oat");
482 }
483
484 std::vector<std::string> compiler_options = runtime->GetCompilerOptions();
485 argv.insert(argv.end(), compiler_options.begin(), compiler_options.end());
486
487 // We must set --android-root.
488 const char* android_root = getenv("ANDROID_ROOT");
489 CHECK(android_root != nullptr);
490 argv.push_back("--android-root=" + std::string(android_root));
491 argv.insert(argv.end(), extra_args.begin(), extra_args.end());
492
493 bool result = RunDex2Oat(argv, error_msg);
494 if (art_file != nullptr) {
495 CHECK_EQ(0, art_file->FlushClose());
496 }
497 if (vdex_file != nullptr) {
498 CHECK_EQ(0, vdex_file->FlushClose());
499 }
500 if (oat_file != nullptr) {
501 CHECK_EQ(0, oat_file->FlushClose());
502 }
503 return result;
504}
505
506bool CommonRuntimeTestImpl::RunDex2Oat(const std::vector<std::string>& args,
507 std::string* error_msg) {
508 // We only want fatal logging for the error message.
509 auto post_fork_fn = []() { return setenv("ANDROID_LOG_TAGS", "*:f", 1) == 0; };
510 ForkAndExecResult res = ForkAndExec(args, post_fork_fn, error_msg);
511 if (res.stage != ForkAndExecResult::kFinished) {
512 *error_msg = strerror(errno);
513 return false;
514 }
515 return res.StandardSuccess();
516}
517
Vladimir Marko672c0802019-07-26 13:03:13 +0100518std::string CommonRuntimeTestImpl::GetImageLocation() {
David Srbecky0c0f3022020-02-13 15:53:01 +0000519 return GetImageDirectory() + "/boot.art";
Vladimir Marko672c0802019-07-26 13:03:13 +0100520}
521
522std::string CommonRuntimeTestImpl::GetSystemImageFile() {
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000523 std::string isa = GetInstructionSetString(kRuntimeISA);
David Srbecky0c0f3022020-02-13 15:53:01 +0000524 return GetImageDirectory() + "/" + isa + "/boot.art";
Vladimir Marko672c0802019-07-26 13:03:13 +0100525}
526
527void CommonRuntimeTestImpl::EnterTransactionMode() {
528 CHECK(!Runtime::Current()->IsActiveTransaction());
529 Runtime::Current()->EnterTransactionMode(/*strict=*/ false, /*root=*/ nullptr);
530}
531
532void CommonRuntimeTestImpl::ExitTransactionMode() {
533 Runtime::Current()->ExitTransactionMode();
534 CHECK(!Runtime::Current()->IsActiveTransaction());
535}
536
537void CommonRuntimeTestImpl::RollbackAndExitTransactionMode() {
538 Runtime::Current()->RollbackAndExitTransactionMode();
539 CHECK(!Runtime::Current()->IsActiveTransaction());
540}
541
542bool CommonRuntimeTestImpl::IsTransactionAborted() {
543 return Runtime::Current()->IsTransactionAborted();
544}
545
Vladimir Markofdd46842020-03-25 14:57:17 +0000546void CommonRuntimeTestImpl::VisitDexes(ArrayRef<const std::string> dexes,
547 const std::function<void(MethodReference)>& method_visitor,
548 const std::function<void(TypeReference)>& class_visitor,
549 size_t method_frequency,
550 size_t class_frequency) {
551 size_t method_counter = 0;
552 size_t class_counter = 0;
553 for (const std::string& dex : dexes) {
554 std::vector<std::unique_ptr<const DexFile>> dex_files;
555 std::string error_msg;
556 const ArtDexFileLoader dex_file_loader;
557 CHECK(dex_file_loader.Open(dex.c_str(),
558 dex,
559 /*verify*/ true,
560 /*verify_checksum*/ false,
561 &error_msg,
562 &dex_files))
563 << error_msg;
564 for (const std::unique_ptr<const DexFile>& dex_file : dex_files) {
565 for (size_t i = 0; i < dex_file->NumMethodIds(); ++i) {
566 if (++method_counter % method_frequency == 0) {
567 method_visitor(MethodReference(dex_file.get(), i));
568 }
569 }
570 for (size_t i = 0; i < dex_file->NumTypeIds(); ++i) {
571 if (++class_counter % class_frequency == 0) {
572 class_visitor(TypeReference(dex_file.get(), dex::TypeIndex(i)));
573 }
574 }
575 }
576 }
577}
578
579void CommonRuntimeTestImpl::GenerateProfile(ArrayRef<const std::string> dexes,
580 File* out_file,
581 size_t method_frequency,
Vladimir Markoa2209802021-04-23 13:28:29 +0000582 size_t type_frequency,
583 bool for_boot_image) {
584 ProfileCompilationInfo profile(for_boot_image);
Vladimir Markofdd46842020-03-25 14:57:17 +0000585 VisitDexes(
586 dexes,
587 [&profile](MethodReference ref) {
588 uint32_t flags = ProfileCompilationInfo::MethodHotness::kFlagHot |
589 ProfileCompilationInfo::MethodHotness::kFlagStartup;
590 EXPECT_TRUE(profile.AddMethod(
591 ProfileMethodInfo(ref),
592 static_cast<ProfileCompilationInfo::MethodHotness::Flag>(flags)));
593 },
594 [&profile](TypeReference ref) {
595 std::set<dex::TypeIndex> classes;
596 classes.insert(ref.TypeIndex());
597 EXPECT_TRUE(profile.AddClassesForDex(ref.dex_file, classes.begin(), classes.end()));
598 },
599 method_frequency,
600 type_frequency);
601 profile.Save(out_file->Fd());
602 EXPECT_EQ(out_file->Flush(), 0);
603}
604
Ian Rogerse63db272014-07-15 15:36:11 -0700605CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
Ian Rogers68d8b422014-07-17 11:09:10 -0700606 vm_->SetCheckJniAbortHook(Hook, &actual_);
Ian Rogerse63db272014-07-15 15:36:11 -0700607}
608
609CheckJniAbortCatcher::~CheckJniAbortCatcher() {
Ian Rogers68d8b422014-07-17 11:09:10 -0700610 vm_->SetCheckJniAbortHook(nullptr, nullptr);
Ian Rogerse63db272014-07-15 15:36:11 -0700611 EXPECT_TRUE(actual_.empty()) << actual_;
612}
613
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700614void CheckJniAbortCatcher::Check(const std::string& expected_text) {
615 Check(expected_text.c_str());
616}
617
Ian Rogerse63db272014-07-15 15:36:11 -0700618void CheckJniAbortCatcher::Check(const char* expected_text) {
619 EXPECT_TRUE(actual_.find(expected_text) != std::string::npos) << "\n"
620 << "Expected to find: " << expected_text << "\n"
621 << "In the output : " << actual_;
622 actual_.clear();
623}
624
625void CheckJniAbortCatcher::Hook(void* data, const std::string& reason) {
626 // We use += because when we're hooking the aborts like this, multiple problems can be found.
627 *reinterpret_cast<std::string*>(data) += reason;
628}
629
630} // namespace art