1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef PANDA_RUNTIME_TESTS_CLASS_LINKER_TEST_EXTENSION_H_ 16 #define PANDA_RUNTIME_TESTS_CLASS_LINKER_TEST_EXTENSION_H_ 17 18 #include "runtime/include/class_linker.h" 19 #include "runtime/include/class_linker_extension.h" 20 #include "runtime/include/coretypes/class.h" 21 #include "runtime/include/panda_vm.h" 22 23 namespace panda::test { 24 25 class ClassLinkerTestExtension : public ClassLinkerExtension { 26 public: ClassLinkerTestExtension(ManagedThread * thread,panda_file::SourceLang lang)27 ClassLinkerTestExtension(ManagedThread *thread, panda_file::SourceLang lang) 28 : ClassLinkerExtension(lang), thread_(thread) 29 { 30 } 31 ~ClassLinkerTestExtension()32 ~ClassLinkerTestExtension() 33 { 34 FreeLoadedClasses(); 35 } 36 InitializeArrayClass(Class * array_class,Class * component_class)37 void InitializeArrayClass(Class *array_class, Class *component_class) override 38 { 39 auto *object_class = GetClassRoot(ClassRoot::OBJECT); 40 array_class->SetBase(object_class); 41 array_class->SetComponentType(component_class); 42 } 43 InitializePrimitiveClass(Class * primitive_class)44 void InitializePrimitiveClass([[maybe_unused]] Class *primitive_class) override {} 45 GetClassVTableSize(ClassRoot root)46 size_t GetClassVTableSize([[maybe_unused]] ClassRoot root) override 47 { 48 return 0; 49 } 50 GetClassIMTSize(ClassRoot root)51 size_t GetClassIMTSize([[maybe_unused]] ClassRoot root) override 52 { 53 return 0; 54 } 55 GetClassSize(ClassRoot root)56 size_t GetClassSize(ClassRoot root) override 57 { 58 return Class::ComputeClassSize(GetClassVTableSize(root), GetClassIMTSize(root), 0, 0, 0, 0, 0, 0); 59 } 60 GetArrayClassVTableSize()61 size_t GetArrayClassVTableSize() override 62 { 63 return GetClassVTableSize(ClassRoot::OBJECT); 64 } 65 GetArrayClassIMTSize()66 size_t GetArrayClassIMTSize() override 67 { 68 return GetClassIMTSize(ClassRoot::OBJECT); 69 } 70 GetArrayClassSize()71 size_t GetArrayClassSize() override 72 { 73 return GetClassSize(ClassRoot::OBJECT); 74 } 75 CreateClass(const uint8_t * descriptor,size_t vtable_size,size_t imt_size,size_t size)76 Class *CreateClass(const uint8_t *descriptor, size_t vtable_size, size_t imt_size, size_t size) override 77 { 78 auto vm = thread_->GetVM(); 79 auto allocator = vm->GetHeapManager()->GetObjectAllocator(); 80 void *ptr = allocator->AllocateNonMovable(coretypes::Class::GetSize(size), DEFAULT_ALIGNMENT, nullptr); 81 auto *res = reinterpret_cast<coretypes::Class *>(ptr); 82 res->InitClass(descriptor, vtable_size, imt_size, size); 83 vm->GetGC()->InitGCBits(res); 84 res->SetClass(GetClassRoot(ClassRoot::CLASS)); 85 auto *klass = res->GetRuntimeClass(); 86 klass->SetManagedObject(res); 87 klass->SetSourceLang(GetLanguage()); 88 AddCreatedClass(klass); 89 return klass; 90 } 91 FreeClass(Class * klass)92 void FreeClass(Class *klass) override 93 { 94 RemoveCreatedClass(klass); 95 } 96 InitializeClass(Class *)97 void InitializeClass(Class *) override {} 98 GetNativeEntryPointFor(panda::Method *)99 const void *GetNativeEntryPointFor(panda::Method *) const override 100 { 101 return nullptr; 102 } 103 CanThrowException(const Method *)104 bool CanThrowException(const Method * /* method */) const override 105 { 106 return true; 107 } 108 GetErrorHandler()109 ClassLinkerErrorHandler *GetErrorHandler() override 110 { 111 return nullptr; 112 } 113 114 private: 115 bool InitializeImpl(bool compressed_string_enabled) override; 116 117 ManagedThread *thread_; 118 }; 119 120 } // namespace panda::test 121 122 #endif // PANDA_RUNTIME_TESTS_CLASS_LINKER_TEST_EXTENSION_H_ 123