blob: 1b69c32b7693377f4fef6d7b15655ad01dff807d [file] [log] [blame]
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -08001/*
2 * Copyright (C) 2011 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
17#ifndef ART_RUNTIME_COMMON_RUNTIME_TEST_H_
18#define ART_RUNTIME_COMMON_RUNTIME_TEST_H_
19
Ian Rogerse63db272014-07-15 15:36:11 -070020#include <gtest/gtest.h>
21#include <jni.h>
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080022
Vladimir Markofdd46842020-03-25 14:57:17 +000023#include <functional>
Ian Rogerse63db272014-07-15 15:36:11 -070024#include <string>
25
Andreas Gampe170331f2017-12-07 18:41:03 -080026#include <android-base/logging.h>
27
David Srbecky3e52aa42015-04-12 07:45:18 +010028#include "arch/instruction_set.h"
David Sehrd5f8de82018-04-27 14:12:03 -070029#include "base/common_art_test.h"
Andreas Gampe7fbc4a52018-11-28 08:26:47 -080030#include "base/locks.h"
David Sehrc431b9d2018-03-02 12:01:51 -080031#include "base/os.h"
Mathieu Chartier2daa1342018-02-20 16:19:28 -080032#include "base/unix_file/fd_file.h"
33#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/compact_dex_level.h"
Mathieu Chartier3398c782016-09-30 10:27:43 -070035// TODO: Add inl file and avoid including inl.
36#include "obj_ptr-inl.h"
Andreas Gampe5a0430d2019-01-04 14:33:57 -080037#include "runtime_globals.h"
Calin Juravlec79470d2017-07-12 17:37:42 -070038#include "scoped_thread_state_change-inl.h"
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080039
40namespace art {
41
Vladimir Markofdd46842020-03-25 14:57:17 +000042class MethodReference;
43class TypeReference;
44
Andreas Gampe170331f2017-12-07 18:41:03 -080045using LogSeverity = android::base::LogSeverity;
46using ScopedLogSeverity = android::base::ScopedLogSeverity;
47
David Sehra8d23cb2019-04-08 11:29:11 -070048template<class MirrorType>
49static inline ObjPtr<MirrorType> MakeObjPtr(MirrorType* ptr) {
50 return ptr;
51}
52
53template<class MirrorType>
54static inline ObjPtr<MirrorType> MakeObjPtr(ObjPtr<MirrorType> ptr) {
55 return ptr;
56}
57
58// OBJ pointer helpers to avoid needing .Decode everywhere.
59#define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr())
60#define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr())
61#define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr())
62#define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr())
63
Ian Rogerse63db272014-07-15 15:36:11 -070064class ClassLinker;
65class CompilerCallbacks;
66class DexFile;
67class JavaVMExt;
68class Runtime;
Vladimir Marko4f990712021-07-14 12:45:13 +010069using RuntimeOptions = std::vector<std::pair<std::string, const void*>>;
Andreas Gampe26761f72017-07-20 18:00:39 -070070class Thread;
71class VariableSizedHandleScope;
Ian Rogerse63db272014-07-15 15:36:11 -070072
David Sehrd5f8de82018-04-27 14:12:03 -070073class CommonRuntimeTestImpl : public CommonArtTestImpl {
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080074 public:
Mathieu Chartier91c91162016-01-15 09:48:15 -080075 CommonRuntimeTestImpl();
76 virtual ~CommonRuntimeTestImpl();
Andreas Gampe7747c8d2014-08-06 14:53:03 -070077
Andreas Gampe26761f72017-07-20 18:00:39 -070078 // A helper function to fill the heap.
79 static void FillHeap(Thread* self,
80 ClassLinker* class_linker,
81 VariableSizedHandleScope* handle_scope)
82 REQUIRES_SHARED(Locks::mutator_lock_);
83 // A helper to set up a small heap (4M) to make FillHeap faster.
84 static void SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options);
85
Mathieu Chartier2daa1342018-02-20 16:19:28 -080086 template <typename Mutator>
87 bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) {
88 std::vector<std::unique_ptr<const DexFile>> dex_files;
89 std::string error_msg;
90 const ArtDexFileLoader dex_file_loader;
91 CHECK(dex_file_loader.Open(input_jar.c_str(),
92 input_jar.c_str(),
Andreas Gampe98ea9d92018-10-19 14:06:15 -070093 /*verify=*/ true,
94 /*verify_checksum=*/ true,
Mathieu Chartier2daa1342018-02-20 16:19:28 -080095 &error_msg,
96 &dex_files)) << error_msg;
97 EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported";
98 const std::unique_ptr<const DexFile>& dex = dex_files[0];
99 CHECK(dex->EnableWrite()) << "Failed to enable write";
100 DexFile* dex_file = const_cast<DexFile*>(dex.get());
101 mutator(dex_file);
102 const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum();
103 if (!output_dex->WriteFully(dex->Begin(), dex->Size())) {
104 return false;
105 }
106 if (output_dex->Flush() != 0) {
107 PLOG(FATAL) << "Could not flush the output file.";
108 }
109 return true;
110 }
111
Vladimir Marko2b076df2019-02-20 11:27:52 +0000112 void MakeInterpreted(ObjPtr<mirror::Class> klass)
113 REQUIRES_SHARED(Locks::mutator_lock_);
114
Vladimir Markob9c29f62019-03-20 14:22:51 +0000115 bool StartDex2OatCommandLine(/*out*/std::vector<std::string>* argv,
Vladimir Markofdd46842020-03-25 14:57:17 +0000116 /*out*/std::string* error_msg,
117 bool use_runtime_bcp_and_image = true);
Vladimir Marko7a85e702018-12-03 18:47:23 +0000118
David Srbeckycf0c6ef2020-02-05 16:25:36 +0000119 bool CompileBootImage(const std::vector<std::string>& extra_args,
120 const std::string& image_file_name_prefix,
121 ArrayRef<const std::string> dex_files,
122 ArrayRef<const std::string> dex_locations,
123 std::string* error_msg,
124 const std::string& use_fd_prefix = "");
125
126 bool CompileBootImage(const std::vector<std::string>& extra_args,
127 const std::string& image_file_name_prefix,
128 ArrayRef<const std::string> dex_files,
129 std::string* error_msg,
130 const std::string& use_fd_prefix = "") {
131 return CompileBootImage(
132 extra_args, image_file_name_prefix, dex_files, dex_files, error_msg, use_fd_prefix);
133 }
134
135 bool RunDex2Oat(const std::vector<std::string>& args, std::string* error_msg);
136
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800137 protected:
Mathieu Chartier91c91162016-01-15 09:48:15 -0800138 // Allow subclases such as CommonCompilerTest to add extra options.
139 virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {}
140
141 // Called before the runtime is created.
142 virtual void PreRuntimeCreate() {}
143
144 // Called after the runtime is created.
145 virtual void PostRuntimeCreate() {}
146
Calin Juravle7865ac72017-06-28 11:03:12 -0700147 // Loads the test dex file identified by the given dex_name into a PathClassLoader.
148 // Returns the created class loader.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700149 jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_);
Calin Juravle7865ac72017-06-28 11:03:12 -0700150 // Loads the test dex file identified by the given first_dex_name and second_dex_name
151 // into a PathClassLoader. Returns the created class loader.
Nicolas Geoffrayd01f60c2016-10-28 14:45:48 +0100152 jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name)
153 REQUIRES_SHARED(Locks::mutator_lock_);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800154
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000155 jobject LoadDexInPathClassLoader(const std::string& dex_name,
156 jobject parent_loader,
Brad Stenning9c924e82021-10-11 19:09:00 -0700157 jobject shared_libraries = nullptr,
158 jobject shared_libraries_after = nullptr);
Dan Zimmermanb682ea42019-12-23 06:59:06 -0800159 jobject LoadDexInPathClassLoader(const std::vector<std::string>& dex_names,
160 jobject parent_loader,
Brad Stenning9c924e82021-10-11 19:09:00 -0700161 jobject shared_libraries = nullptr,
162 jobject shared_libraries_after = nullptr);
Calin Juravle7865ac72017-06-28 11:03:12 -0700163 jobject LoadDexInDelegateLastClassLoader(const std::string& dex_name, jobject parent_loader);
David Brazdil1a9ac532019-03-05 11:57:13 +0000164 jobject LoadDexInInMemoryDexClassLoader(const std::string& dex_name, jobject parent_loader);
Dan Zimmermanb682ea42019-12-23 06:59:06 -0800165 jobject LoadDexInWellKnownClassLoader(const std::vector<std::string>& dex_names,
Calin Juravle7865ac72017-06-28 11:03:12 -0700166 jclass loader_class,
Nicolas Geoffraye1672732018-11-30 01:09:49 +0000167 jobject parent_loader,
Brad Stenning9c924e82021-10-11 19:09:00 -0700168 jobject shared_libraries = nullptr,
169 jobject shared_libraries_after = nullptr);
Calin Juravle7865ac72017-06-28 11:03:12 -0700170
Vladimir Markofdd46842020-03-25 14:57:17 +0000171 void VisitDexes(ArrayRef<const std::string> dexes,
172 const std::function<void(MethodReference)>& method_visitor,
173 const std::function<void(TypeReference)>& class_visitor,
174 size_t method_frequency = 1u,
175 size_t class_frequency = 1u);
176
177 void GenerateProfile(ArrayRef<const std::string> dexes,
178 File* out_file,
179 size_t method_frequency = 1u,
Vladimir Markoa2209802021-04-23 13:28:29 +0000180 size_t type_frequency = 1u,
181 bool for_boot_image = false);
182 void GenerateBootProfile(ArrayRef<const std::string> dexes,
183 File* out_file,
184 size_t method_frequency = 1u,
185 size_t type_frequency = 1u) {
186 return GenerateProfile(
187 dexes, out_file, method_frequency, type_frequency, /*for_boot_image=*/ true);
188 }
Vladimir Markofdd46842020-03-25 14:57:17 +0000189
Ian Rogers700a4022014-05-19 16:49:03 -0700190 std::unique_ptr<Runtime> runtime_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800191
192 // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all
193 // owned by the runtime.
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800194 ClassLinker* class_linker_;
Richard Uhlerfbef44d2014-12-23 09:48:51 -0800195 const DexFile* java_lang_dex_file_;
196 std::vector<const DexFile*> boot_class_path_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800197
Calin Juravlec79470d2017-07-12 17:37:42 -0700198 // Get the dex files from a PathClassLoader or DelegateLastClassLoader.
199 // This only looks into the current class loader and does not recurse into the parents.
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700200 std::vector<const DexFile*> GetDexFiles(jobject jclass_loader);
Vladimir Marko4e0b5d72022-11-09 10:58:15 +0000201 std::vector<const DexFile*> GetDexFiles(Thread* self, Handle<mirror::ClassLoader> class_loader)
Calin Juravlec79470d2017-07-12 17:37:42 -0700202 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampe81c6f8d2015-03-25 17:19:53 -0700203
204 // Get the first dex file from a PathClassLoader. Will abort if it is null.
205 const DexFile* GetFirstDexFile(jobject jclass_loader);
206
Andreas Gampebb9c6b12015-03-29 13:56:36 -0700207 std::unique_ptr<CompilerCallbacks> callbacks_;
208
Vladimir Marko483c41a2021-11-12 12:45:23 +0000209 bool use_boot_image_;
210
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100211 virtual void SetUp();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800212
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100213 virtual void TearDown();
Mathieu Chartier91c91162016-01-15 09:48:15 -0800214
Przemyslaw Szczepaniaka794c262016-07-25 17:31:06 +0100215 // Called to finish up runtime creation and filling test fields. By default runs root
216 // initializers, initialize well-known classes, and creates the heap thread pool.
217 virtual void FinalizeSetup();
Vladimir Marko672c0802019-07-26 13:03:13 +0100218
David Srbecky6355d692020-03-26 14:10:26 +0000219 // Returns the directory where the pre-compiled boot.art can be found.
Vladimir Marko672c0802019-07-26 13:03:13 +0100220 static std::string GetImageLocation();
221 static std::string GetSystemImageFile();
222
Hans Boehmba5cc5b2022-09-09 22:43:53 +0000223 static void EnterTransactionMode() REQUIRES_SHARED(Locks::mutator_lock_);
Vladimir Marko672c0802019-07-26 13:03:13 +0100224 static void ExitTransactionMode();
225 static void RollbackAndExitTransactionMode() REQUIRES_SHARED(Locks::mutator_lock_);
226 static bool IsTransactionAborted();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800227};
228
Mathieu Chartier91c91162016-01-15 09:48:15 -0800229template <typename TestType>
230class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl {
231 public:
232 CommonRuntimeTestBase() {}
233 virtual ~CommonRuntimeTestBase() {}
234
235 protected:
Roland Levillainf73caca2018-08-24 17:19:07 +0100236 void SetUp() override {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800237 CommonRuntimeTestImpl::SetUp();
238 }
239
Roland Levillainf73caca2018-08-24 17:19:07 +0100240 void TearDown() override {
Mathieu Chartier91c91162016-01-15 09:48:15 -0800241 CommonRuntimeTestImpl::TearDown();
242 }
Mathieu Chartier91c91162016-01-15 09:48:15 -0800243};
244
245using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>;
246
247template <typename Param>
248using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>;
249
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800250// Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on
251// rather than aborting, so be careful!
252class CheckJniAbortCatcher {
253 public:
Ian Rogerse63db272014-07-15 15:36:11 -0700254 CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800255
Ian Rogerse63db272014-07-15 15:36:11 -0700256 ~CheckJniAbortCatcher();
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800257
Igor Murashkin367f3dd2016-09-01 17:00:24 -0700258 void Check(const std::string& expected_text);
Ian Rogerse63db272014-07-15 15:36:11 -0700259 void Check(const char* expected_text);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800260
261 private:
Ian Rogerse63db272014-07-15 15:36:11 -0700262 static void Hook(void* data, const std::string& reason);
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800263
Ian Rogers68d8b422014-07-17 11:09:10 -0700264 JavaVMExt* const vm_;
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800265 std::string actual_;
266
267 DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher);
268};
269
Roland Levillain14d13072020-01-08 17:57:39 +0000270#define TEST_DISABLED() \
271 do { \
272 printf("WARNING: TEST DISABLED\n"); \
273 return; \
274 } while (false)
275
Andreas Gampe38aa0b52018-07-10 23:26:55 -0700276#define TEST_DISABLED_FOR_ARM() \
277 if (kRuntimeISA == InstructionSet::kArm || kRuntimeISA == InstructionSet::kThumb2) { \
278 printf("WARNING: TEST DISABLED FOR ARM\n"); \
279 return; \
280 }
281
282#define TEST_DISABLED_FOR_ARM64() \
283 if (kRuntimeISA == InstructionSet::kArm64) { \
284 printf("WARNING: TEST DISABLED FOR ARM64\n"); \
285 return; \
286 }
287
Roland Levillain19772bf2017-02-16 11:28:10 +0000288#define TEST_DISABLED_FOR_X86() \
Vladimir Marko33bff252017-11-01 14:35:42 +0000289 if (kRuntimeISA == InstructionSet::kX86) { \
Roland Levillain19772bf2017-02-16 11:28:10 +0000290 printf("WARNING: TEST DISABLED FOR X86\n"); \
Vladimir Marko57070da2017-02-14 16:16:30 +0000291 return; \
292 }
293
Evgeny Astigeevichd2ac3072020-01-29 11:51:40 +0000294#define TEST_DISABLED_FOR_X86_64() \
295 if (kRuntimeISA == InstructionSet::kX86_64) { \
296 printf("WARNING: TEST DISABLED FOR X86_64\n"); \
297 return; \
298 }
299
Roland Levillain6d729a72017-06-30 18:34:01 +0100300#define TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS() \
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000301 if (!gUseReadBarrier || !kUseBakerReadBarrier) { \
Roland Levillain6d729a72017-06-30 18:34:01 +0100302 printf("WARNING: TEST DISABLED FOR GC WITHOUT BAKER READ BARRIER\n"); \
303 return; \
304 }
305
Alex Klyubin3856af02017-10-23 13:53:13 -0700306#define TEST_DISABLED_FOR_HEAP_POISONING() \
307 if (kPoisonHeapReferences) { \
308 printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \
309 return; \
310 }
Roland Levillainf5dd1142018-07-03 13:29:18 +0100311
312#define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS() \
Lokesh Gidraca5ed9f2022-04-20 01:39:28 +0000313 if (kRunningOnMemoryTool && kPoisonHeapReferences && !gUseReadBarrier) { \
Roland Levillainf5dd1142018-07-03 13:29:18 +0100314 printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING WITHOUT READ BARRIERS\n"); \
315 return; \
316 }
317
Nicolas Geoffray8d6651d2019-07-08 10:03:16 +0100318#define TEST_DISABLED_FOR_KERNELS_WITH_CACHE_SEGFAULT() \
319 if (CacheOperationsMaySegFault()) { \
320 printf("WARNING: TEST DISABLED ON KERNEL THAT SEGFAULT ON CACHE OPERATIONS\n"); \
321 return; \
322 }
323
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800324} // namespace art
325
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800326#endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_