Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 1 | /* |
| 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 Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | #include <jni.h> |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 22 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 23 | #include <string> |
| 24 | |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 25 | #include <android-base/logging.h> |
| 26 | |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 27 | #include "arch/instruction_set.h" |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 28 | #include "base/common_art_test.h" |
David Sehr | 1979c64 | 2018-04-26 14:41:18 -0700 | [diff] [blame] | 29 | #include "base/globals.h" |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 30 | #include "base/mutex.h" |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 31 | #include "base/os.h" |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 32 | #include "base/unix_file/fd_file.h" |
| 33 | #include "dex/art_dex_file_loader.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 34 | #include "dex/compact_dex_level.h" |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 35 | // TODO: Add inl file and avoid including inl. |
| 36 | #include "obj_ptr-inl.h" |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 37 | #include "scoped_thread_state_change-inl.h" |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
Andreas Gampe | 170331f | 2017-12-07 18:41:03 -0800 | [diff] [blame] | 41 | using LogSeverity = android::base::LogSeverity; |
| 42 | using ScopedLogSeverity = android::base::ScopedLogSeverity; |
| 43 | |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 44 | // OBJ pointer helpers to avoid needing .Decode everywhere. |
Mathieu Chartier | 1cc62e4 | 2016-10-03 18:01:28 -0700 | [diff] [blame] | 45 | #define EXPECT_OBJ_PTR_EQ(a, b) EXPECT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 46 | #define ASSERT_OBJ_PTR_EQ(a, b) ASSERT_EQ(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 47 | #define EXPECT_OBJ_PTR_NE(a, b) EXPECT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
| 48 | #define ASSERT_OBJ_PTR_NE(a, b) ASSERT_NE(MakeObjPtr(a).Ptr(), MakeObjPtr(b).Ptr()); |
Mathieu Chartier | 3398c78 | 2016-09-30 10:27:43 -0700 | [diff] [blame] | 49 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 50 | class ClassLinker; |
| 51 | class CompilerCallbacks; |
| 52 | class DexFile; |
| 53 | class JavaVMExt; |
| 54 | class Runtime; |
| 55 | typedef std::vector<std::pair<std::string, const void*>> RuntimeOptions; |
Andreas Gampe | 26761f7 | 2017-07-20 18:00:39 -0700 | [diff] [blame] | 56 | class Thread; |
| 57 | class VariableSizedHandleScope; |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 58 | |
David Sehr | d5f8de8 | 2018-04-27 14:12:03 -0700 | [diff] [blame] | 59 | class CommonRuntimeTestImpl : public CommonArtTestImpl { |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 60 | public: |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 61 | CommonRuntimeTestImpl(); |
| 62 | virtual ~CommonRuntimeTestImpl(); |
Andreas Gampe | 7747c8d | 2014-08-06 14:53:03 -0700 | [diff] [blame] | 63 | |
David Srbecky | 3e52aa4 | 2015-04-12 07:45:18 +0100 | [diff] [blame] | 64 | static std::string GetAndroidTargetToolsDir(InstructionSet isa); |
| 65 | |
Andreas Gampe | 26761f7 | 2017-07-20 18:00:39 -0700 | [diff] [blame] | 66 | // A helper function to fill the heap. |
| 67 | static void FillHeap(Thread* self, |
| 68 | ClassLinker* class_linker, |
| 69 | VariableSizedHandleScope* handle_scope) |
| 70 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 71 | // A helper to set up a small heap (4M) to make FillHeap faster. |
| 72 | static void SetUpRuntimeOptionsForFillHeap(RuntimeOptions *options); |
| 73 | |
Mathieu Chartier | 2daa134 | 2018-02-20 16:19:28 -0800 | [diff] [blame] | 74 | template <typename Mutator> |
| 75 | bool MutateDexFile(File* output_dex, const std::string& input_jar, const Mutator& mutator) { |
| 76 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 77 | std::string error_msg; |
| 78 | const ArtDexFileLoader dex_file_loader; |
| 79 | CHECK(dex_file_loader.Open(input_jar.c_str(), |
| 80 | input_jar.c_str(), |
| 81 | /*verify*/ true, |
| 82 | /*verify_checksum*/ true, |
| 83 | &error_msg, |
| 84 | &dex_files)) << error_msg; |
| 85 | EXPECT_EQ(dex_files.size(), 1u) << "Only one input dex is supported"; |
| 86 | const std::unique_ptr<const DexFile>& dex = dex_files[0]; |
| 87 | CHECK(dex->EnableWrite()) << "Failed to enable write"; |
| 88 | DexFile* dex_file = const_cast<DexFile*>(dex.get()); |
| 89 | mutator(dex_file); |
| 90 | const_cast<DexFile::Header&>(dex_file->GetHeader()).checksum_ = dex_file->CalculateChecksum(); |
| 91 | if (!output_dex->WriteFully(dex->Begin(), dex->Size())) { |
| 92 | return false; |
| 93 | } |
| 94 | if (output_dex->Flush() != 0) { |
| 95 | PLOG(FATAL) << "Could not flush the output file."; |
| 96 | } |
| 97 | return true; |
| 98 | } |
| 99 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 100 | protected: |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 101 | // Allow subclases such as CommonCompilerTest to add extra options. |
| 102 | virtual void SetUpRuntimeOptions(RuntimeOptions* options ATTRIBUTE_UNUSED) {} |
| 103 | |
| 104 | // Called before the runtime is created. |
| 105 | virtual void PreRuntimeCreate() {} |
| 106 | |
| 107 | // Called after the runtime is created. |
| 108 | virtual void PostRuntimeCreate() {} |
| 109 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 110 | // Loads the test dex file identified by the given dex_name into a PathClassLoader. |
| 111 | // Returns the created class loader. |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 112 | jobject LoadDex(const char* dex_name) REQUIRES_SHARED(Locks::mutator_lock_); |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 113 | // Loads the test dex file identified by the given first_dex_name and second_dex_name |
| 114 | // into a PathClassLoader. Returns the created class loader. |
Nicolas Geoffray | d01f60c | 2016-10-28 14:45:48 +0100 | [diff] [blame] | 115 | jobject LoadMultiDex(const char* first_dex_name, const char* second_dex_name) |
| 116 | REQUIRES_SHARED(Locks::mutator_lock_); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 117 | |
Calin Juravle | 7865ac7 | 2017-06-28 11:03:12 -0700 | [diff] [blame] | 118 | jobject LoadDexInPathClassLoader(const std::string& dex_name, jobject parent_loader); |
| 119 | jobject LoadDexInDelegateLastClassLoader(const std::string& dex_name, jobject parent_loader); |
| 120 | jobject LoadDexInWellKnownClassLoader(const std::string& dex_name, |
| 121 | jclass loader_class, |
| 122 | jobject parent_loader); |
| 123 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 124 | std::unique_ptr<Runtime> runtime_; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 125 | |
| 126 | // The class_linker_, java_lang_dex_file_, and boot_class_path_ are all |
| 127 | // owned by the runtime. |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 128 | ClassLinker* class_linker_; |
Richard Uhler | fbef44d | 2014-12-23 09:48:51 -0800 | [diff] [blame] | 129 | const DexFile* java_lang_dex_file_; |
| 130 | std::vector<const DexFile*> boot_class_path_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 131 | |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 132 | // Get the dex files from a PathClassLoader or DelegateLastClassLoader. |
| 133 | // This only looks into the current class loader and does not recurse into the parents. |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 134 | std::vector<const DexFile*> GetDexFiles(jobject jclass_loader); |
Calin Juravle | c79470d | 2017-07-12 17:37:42 -0700 | [diff] [blame] | 135 | std::vector<const DexFile*> GetDexFiles(ScopedObjectAccess& soa, |
| 136 | Handle<mirror::ClassLoader> class_loader) |
| 137 | REQUIRES_SHARED(Locks::mutator_lock_); |
Andreas Gampe | 81c6f8d | 2015-03-25 17:19:53 -0700 | [diff] [blame] | 138 | |
| 139 | // Get the first dex file from a PathClassLoader. Will abort if it is null. |
| 140 | const DexFile* GetFirstDexFile(jobject jclass_loader); |
| 141 | |
Andreas Gampe | bb9c6b1 | 2015-03-29 13:56:36 -0700 | [diff] [blame] | 142 | std::unique_ptr<CompilerCallbacks> callbacks_; |
| 143 | |
Przemyslaw Szczepaniak | a794c26 | 2016-07-25 17:31:06 +0100 | [diff] [blame] | 144 | virtual void SetUp(); |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 145 | |
Przemyslaw Szczepaniak | a794c26 | 2016-07-25 17:31:06 +0100 | [diff] [blame] | 146 | virtual void TearDown(); |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 147 | |
Przemyslaw Szczepaniak | a794c26 | 2016-07-25 17:31:06 +0100 | [diff] [blame] | 148 | // Called to finish up runtime creation and filling test fields. By default runs root |
| 149 | // initializers, initialize well-known classes, and creates the heap thread pool. |
| 150 | virtual void FinalizeSetup(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 151 | }; |
| 152 | |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 153 | template <typename TestType> |
| 154 | class CommonRuntimeTestBase : public TestType, public CommonRuntimeTestImpl { |
| 155 | public: |
| 156 | CommonRuntimeTestBase() {} |
| 157 | virtual ~CommonRuntimeTestBase() {} |
| 158 | |
| 159 | protected: |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 160 | void SetUp() override { |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 161 | CommonRuntimeTestImpl::SetUp(); |
| 162 | } |
| 163 | |
Roland Levillain | f73caca | 2018-08-24 17:19:07 +0100 | [diff] [blame] | 164 | void TearDown() override { |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 165 | CommonRuntimeTestImpl::TearDown(); |
| 166 | } |
Mathieu Chartier | 91c9116 | 2016-01-15 09:48:15 -0800 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | using CommonRuntimeTest = CommonRuntimeTestBase<testing::Test>; |
| 170 | |
| 171 | template <typename Param> |
| 172 | using CommonRuntimeTestWithParam = CommonRuntimeTestBase<testing::TestWithParam<Param>>; |
| 173 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 174 | // Sets a CheckJni abort hook to catch failures. Note that this will cause CheckJNI to carry on |
| 175 | // rather than aborting, so be careful! |
| 176 | class CheckJniAbortCatcher { |
| 177 | public: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 178 | CheckJniAbortCatcher(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 179 | |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 180 | ~CheckJniAbortCatcher(); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 181 | |
Igor Murashkin | 367f3dd | 2016-09-01 17:00:24 -0700 | [diff] [blame] | 182 | void Check(const std::string& expected_text); |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 183 | void Check(const char* expected_text); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 184 | |
| 185 | private: |
Ian Rogers | e63db27 | 2014-07-15 15:36:11 -0700 | [diff] [blame] | 186 | static void Hook(void* data, const std::string& reason); |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 187 | |
Ian Rogers | 68d8b42 | 2014-07-17 11:09:10 -0700 | [diff] [blame] | 188 | JavaVMExt* const vm_; |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 189 | std::string actual_; |
| 190 | |
| 191 | DISALLOW_COPY_AND_ASSIGN(CheckJniAbortCatcher); |
| 192 | }; |
| 193 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 194 | #define TEST_DISABLED_FOR_ARM() \ |
| 195 | if (kRuntimeISA == InstructionSet::kArm || kRuntimeISA == InstructionSet::kThumb2) { \ |
| 196 | printf("WARNING: TEST DISABLED FOR ARM\n"); \ |
| 197 | return; \ |
| 198 | } |
| 199 | |
| 200 | #define TEST_DISABLED_FOR_ARM64() \ |
| 201 | if (kRuntimeISA == InstructionSet::kArm64) { \ |
| 202 | printf("WARNING: TEST DISABLED FOR ARM64\n"); \ |
| 203 | return; \ |
| 204 | } |
| 205 | |
Nicolas Geoffray | 54accbc | 2014-08-13 03:40:45 +0100 | [diff] [blame] | 206 | #define TEST_DISABLED_FOR_MIPS() \ |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 207 | if (kRuntimeISA == InstructionSet::kMips) { \ |
Nicolas Geoffray | 54accbc | 2014-08-13 03:40:45 +0100 | [diff] [blame] | 208 | printf("WARNING: TEST DISABLED FOR MIPS\n"); \ |
| 209 | return; \ |
| 210 | } |
| 211 | |
Andreas Gampe | 38aa0b5 | 2018-07-10 23:26:55 -0700 | [diff] [blame] | 212 | #define TEST_DISABLED_FOR_MIPS64() \ |
| 213 | if (kRuntimeISA == InstructionSet::kMips64) { \ |
| 214 | printf("WARNING: TEST DISABLED FOR MIPS64\n"); \ |
| 215 | return; \ |
| 216 | } |
| 217 | |
Roland Levillain | 19772bf | 2017-02-16 11:28:10 +0000 | [diff] [blame] | 218 | #define TEST_DISABLED_FOR_X86() \ |
Vladimir Marko | 33bff25 | 2017-11-01 14:35:42 +0000 | [diff] [blame] | 219 | if (kRuntimeISA == InstructionSet::kX86) { \ |
Roland Levillain | 19772bf | 2017-02-16 11:28:10 +0000 | [diff] [blame] | 220 | printf("WARNING: TEST DISABLED FOR X86\n"); \ |
Vladimir Marko | 57070da | 2017-02-14 16:16:30 +0000 | [diff] [blame] | 221 | return; \ |
| 222 | } |
| 223 | |
jessicahandojo | 3aaa37b | 2016-07-29 14:46:37 -0700 | [diff] [blame] | 224 | #define TEST_DISABLED_FOR_STRING_COMPRESSION() \ |
| 225 | if (mirror::kUseStringCompression) { \ |
| 226 | printf("WARNING: TEST DISABLED FOR STRING COMPRESSION\n"); \ |
| 227 | return; \ |
| 228 | } |
| 229 | |
Roland Levillain | 6d729a7 | 2017-06-30 18:34:01 +0100 | [diff] [blame] | 230 | #define TEST_DISABLED_WITHOUT_BAKER_READ_BARRIERS() \ |
| 231 | if (!kEmitCompilerReadBarrier || !kUseBakerReadBarrier) { \ |
| 232 | printf("WARNING: TEST DISABLED FOR GC WITHOUT BAKER READ BARRIER\n"); \ |
| 233 | return; \ |
| 234 | } |
| 235 | |
Alex Klyubin | 3856af0 | 2017-10-23 13:53:13 -0700 | [diff] [blame] | 236 | #define TEST_DISABLED_FOR_HEAP_POISONING() \ |
| 237 | if (kPoisonHeapReferences) { \ |
| 238 | printf("WARNING: TEST DISABLED FOR HEAP POISONING\n"); \ |
| 239 | return; \ |
| 240 | } |
Roland Levillain | f5dd114 | 2018-07-03 13:29:18 +0100 | [diff] [blame] | 241 | |
| 242 | #define TEST_DISABLED_FOR_MEMORY_TOOL_WITH_HEAP_POISONING_WITHOUT_READ_BARRIERS() \ |
| 243 | if (kRunningOnMemoryTool && kPoisonHeapReferences && !kEmitCompilerReadBarrier) { \ |
| 244 | printf("WARNING: TEST DISABLED FOR MEMORY TOOL WITH HEAP POISONING WITHOUT READ BARRIERS\n"); \ |
| 245 | return; \ |
| 246 | } |
| 247 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 248 | } // namespace art |
| 249 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 250 | #endif // ART_RUNTIME_COMMON_RUNTIME_TEST_H_ |