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/pt_default_lang_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
CreatePtLangExt() const63 std::unique_ptr<tooling::PtLangExt> LanguageContextBase::CreatePtLangExt() const
64 {
65 std::unique_ptr<tooling::PtLangExt> result;
66
67 if (GetLanguageType() == LangTypeT::LANG_TYPE_STATIC) {
68 result = std::make_unique<tooling::PtStaticDefaultExtension>(GetLanguage());
69 } else {
70 result = std::make_unique<tooling::PtDynamicDefaultExtension>();
71 }
72 return result;
73 }
74
ThrowException(ManagedThread * thread,const uint8_t * mutf8Name,const uint8_t * mutf8Msg) const75 void LanguageContextBase::ThrowException([[maybe_unused]] ManagedThread *thread,
76 [[maybe_unused]] const uint8_t *mutf8Name,
77 [[maybe_unused]] const uint8_t *mutf8Msg) const
78 {
79 }
80
SetExceptionToVReg(interpreter::AccVRegister & vreg,ObjectHeader * obj) const81 void LanguageContextBase::SetExceptionToVReg(
82 // CC-OFFNXT(G.FMT.06) false positive
83 [[maybe_unused]] interpreter::AccVRegister &vreg, // NOLINTNEXTLINE(google-runtime-references)
84 [[maybe_unused]] ObjectHeader *obj) const
85 {
86 }
87
WrapClassInitializerException(ClassLinker * classLinker,ManagedThread * thread) const88 void LanguageContextBase::WrapClassInitializerException(ClassLinker *classLinker, ManagedThread *thread) const
89 {
90 ASSERT(thread->HasPendingException());
91
92 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(*thread->GetException()->ClassAddr<Class>());
93
94 auto *errorClass = classLinker->GetExtension(ctx)->GetClass(ctx.GetErrorClassDescriptor(), false);
95 ASSERT(errorClass != nullptr);
96
97 auto *cause = thread->GetException();
98 if (cause->IsInstanceOf(errorClass)) {
99 return;
100 }
101
102 ark::ThrowException(ctx, thread, ctx.GetExceptionInInitializerErrorDescriptor(), nullptr);
103 }
104
105 } // namespace ark
106