• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 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 #include "runtime/include/language_context.h"
16 
17 #include "macros.h"
18 #include "runtime/core/core_itable_builder.h"
19 #include "runtime/core/core_vm.h"
20 #include "runtime/core/core_vtable_builder.h"
21 #include "runtime/handle_scope-inl.h"
22 #include "runtime/include/class_linker.h"
23 #include "runtime/include/language_config.h"
24 #include "runtime/include/method.h"
25 #include "runtime/include/runtime.h"
26 #include "runtime/include/stack_walker.h"
27 #include "runtime/include/thread.h"
28 #include "runtime/mem/gc/gc.h"
29 #include "runtime/mem/vm_handle.h"
30 #include "runtime/tooling/default_inspector_extension.h"
31 
32 namespace ark {
GetCatchMethodAndOffset(Method * method,ManagedThread * thread) const33 std::pair<Method *, uint32_t> LanguageContextBase::GetCatchMethodAndOffset(Method *method, ManagedThread *thread) const
34 {
35     uint32_t catchOffset = 0;
36     Method *catchMethod = method;
37     auto stack = StackWalker::Create(thread);
38     while (stack.HasFrame()) {
39         catchMethod = stack.GetMethod();
40         if (catchMethod->GetPandaFile() == nullptr) {
41             stack.NextFrame();
42             continue;
43         }
44         if (stack.IsCFrame()) {
45             stack.NextFrame();
46             continue;
47         }
48         catchOffset = catchMethod->FindCatchBlock(thread->GetException()->ClassAddr<Class>(), stack.GetBytecodePc());
49         if (catchOffset != panda_file::INVALID_OFFSET) {
50             break;
51         }
52         stack.NextFrame();
53     }
54 
55     return std::make_pair(catchMethod, catchOffset);
56 }
57 
CreateClassLinkerExtension() const58 std::unique_ptr<ClassLinkerExtension> LanguageContextBase::CreateClassLinkerExtension() const
59 {
60     return nullptr;
61 }
62 
CreateInspectorExtension() const63 std::unique_ptr<tooling::InspectorExtension> LanguageContextBase::CreateInspectorExtension() const
64 {
65     std::unique_ptr<tooling::InspectorExtension> result;
66 
67     if (GetLanguageType() == LangTypeT::LANG_TYPE_STATIC) {
68         result = std::make_unique<tooling::StaticDefaultInspectorExtension>(GetLanguage());
69     } else {
70         result = std::make_unique<tooling::DynamicDefaultInspectorExtension>();
71     }
72     return result;
73 }
74 
CreatePtLangExt() const75 PandaUniquePtr<tooling::PtLangExt> LanguageContextBase::CreatePtLangExt() const
76 {
77     return nullptr;
78 }
79 
ThrowException(ManagedThread * thread,const uint8_t * mutf8Name,const uint8_t * mutf8Msg) const80 void LanguageContextBase::ThrowException([[maybe_unused]] ManagedThread *thread,
81                                          [[maybe_unused]] const uint8_t *mutf8Name,
82                                          [[maybe_unused]] const uint8_t *mutf8Msg) const
83 {
84 }
85 
SetExceptionToVReg(interpreter::AccVRegister & vreg,ObjectHeader * obj) const86 void LanguageContextBase::SetExceptionToVReg(
87     [[maybe_unused]] interpreter::AccVRegister &vreg,  // NOLINTNEXTLINE(google-runtime-references)
88     [[maybe_unused]] ObjectHeader *obj) const
89 {
90 }
91 
WrapClassInitializerException(ClassLinker * classLinker,ManagedThread * thread) const92 void LanguageContextBase::WrapClassInitializerException(ClassLinker *classLinker, ManagedThread *thread) const
93 {
94     ASSERT(thread->HasPendingException());
95 
96     LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(*thread->GetException()->ClassAddr<Class>());
97 
98     auto *errorClass = classLinker->GetExtension(ctx)->GetClass(ctx.GetErrorClassDescriptor(), false);
99     ASSERT(errorClass != nullptr);
100 
101     auto *cause = thread->GetException();
102     if (cause->IsInstanceOf(errorClass)) {
103         return;
104     }
105 
106     ark::ThrowException(ctx, thread, ctx.GetExceptionInInitializerErrorDescriptor(), nullptr);
107 }
108 
109 }  // namespace ark
110