1 /**
2 * Copyright (c) 2023-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
16 #include "plugins/ets/runtime/ets_exceptions.h"
17
18 #include "plugins/ets/runtime/ets_class_linker_extension.h"
19 #include "plugins/ets/runtime/ets_coroutine.h"
20 #include "plugins/ets/runtime/ets_handle.h"
21 #include "plugins/ets/runtime/ets_handle_scope.h"
22 #include "plugins/ets/runtime/ets_panda_file_items.h"
23 #include "plugins/ets/runtime/ets_vm.h"
24 #include "plugins/ets/runtime/types/ets_method.h"
25 #include "plugins/ets/runtime/types/ets_string.h"
26 #include "plugins/ets/runtime/types/ets_error_options.h"
27
28 namespace ark::ets {
29
GetExceptionClass(EtsCoroutine * coroutine,const char * classDescriptor,bool * isError)30 static EtsClass *GetExceptionClass(EtsCoroutine *coroutine, const char *classDescriptor, bool *isError)
31 {
32 ASSERT(coroutine != nullptr);
33 ASSERT(classDescriptor != nullptr);
34 ASSERT(isError != nullptr);
35
36 EtsClassLinker *classLinker = coroutine->GetPandaVM()->GetClassLinker();
37 EtsClass *cls = classLinker->GetClass(classDescriptor, true);
38 if (cls == nullptr) {
39 LOG(ERROR, CLASS_LINKER) << "Class " << classDescriptor << " not found";
40 return nullptr;
41 }
42
43 if (!classLinker->InitializeClass(coroutine, cls)) {
44 LOG(ERROR, CLASS_LINKER) << "Class " << classDescriptor << " cannot be initialized";
45 return nullptr;
46 }
47
48 EtsClass *errorBaseCls = classLinker->GetClass(panda_file_items::class_descriptors::ERROR.data(), true);
49 EtsClass *exceptionBaseCls = classLinker->GetClass(panda_file_items::class_descriptors::EXCEPTION.data(), true);
50
51 if (errorBaseCls->IsAssignableFrom(cls)) {
52 *isError = true;
53 return cls;
54 }
55 if (exceptionBaseCls->IsAssignableFrom(cls)) {
56 *isError = false;
57 return cls;
58 }
59
60 UNREACHABLE();
61 return nullptr;
62 }
63
SetupEtsException(EtsCoroutine * coroutine,const char * classDescriptor,const char * msg)64 EtsObject *SetupEtsException(EtsCoroutine *coroutine, const char *classDescriptor, const char *msg)
65 {
66 [[maybe_unused]] EtsHandleScope scope(coroutine);
67 EtsHandle<EtsObject> cause(coroutine, EtsObject::FromCoreType(coroutine->GetException()));
68 coroutine->ClearException();
69 EtsHandle<EtsErrorOptions> errOptionHandle(coroutine, EtsErrorOptions::Create(coroutine));
70 errOptionHandle->SetCause(cause.GetPtr());
71
72 bool isError = false;
73 EtsClass *cls = GetExceptionClass(coroutine, classDescriptor, &isError);
74 if (cls == nullptr) {
75 return nullptr;
76 }
77
78 EtsString *etsMsg = nullptr;
79 if (msg != nullptr) {
80 etsMsg = EtsString::CreateFromMUtf8(msg);
81 } else {
82 etsMsg = EtsString::CreateNewEmptyString();
83 }
84 if (UNLIKELY(etsMsg == nullptr)) {
85 // OOM happened during msg allocation
86 ASSERT(coroutine->HasPendingException());
87 return nullptr;
88 }
89
90 Method::Proto proto(Method::Proto::ShortyVector {panda_file::Type(panda_file::Type::TypeId::VOID),
91 panda_file::Type(panda_file::Type::TypeId::REFERENCE),
92 panda_file::Type(panda_file::Type::TypeId::REFERENCE)},
93 Method::Proto::RefTypeVector {isError ? panda_file_items::class_descriptors::OBJECT
94 : panda_file_items::class_descriptors::STRING,
95 panda_file_items::class_descriptors::OBJECT});
96 EtsMethod *ctor = cls->GetDirectMethod(panda_file_items::CTOR.data(), proto);
97 if (ctor == nullptr) {
98 LOG(FATAL, RUNTIME) << "No method " << panda_file_items::CTOR << " in class " << classDescriptor;
99 return nullptr;
100 }
101
102 EtsHandle<EtsString> msgHandle(coroutine, etsMsg);
103 EtsHandle<EtsObject> excHandle(coroutine, EtsObject::Create(cls));
104 // clang-format off
105 std::array args {
106 Value(excHandle.GetPtr()->GetCoreType()),
107 Value(isError ? msgHandle.GetPtr()->AsObject()->GetCoreType() : msgHandle.GetPtr()->GetCoreType()),
108 Value(isError ? errOptionHandle.GetPtr()->AsObject()->GetCoreType() : cause.GetPtr()->GetCoreType())
109 };
110 // clang-format on
111
112 EtsMethod::ToRuntimeMethod(ctor)->InvokeVoid(coroutine, args.data());
113 if (LIKELY(!coroutine->HasPendingException())) {
114 return excHandle.GetPtr();
115 }
116 return nullptr;
117 }
118
119 // NOTE: Is used to throw all language exceptional objects (currently Errors and Exceptions)
ThrowEtsException(EtsCoroutine * coroutine,const char * classDescriptor,const char * msg)120 void ThrowEtsException(EtsCoroutine *coroutine, const char *classDescriptor, const char *msg)
121 {
122 ASSERT(coroutine != nullptr);
123 ASSERT(coroutine == EtsCoroutine::GetCurrent());
124
125 if (coroutine->IsUsePreAllocObj()) {
126 coroutine->SetUsePreAllocObj(false);
127 coroutine->SetException(coroutine->GetVM()->GetOOMErrorObject());
128 return;
129 }
130
131 EtsObject *exc = SetupEtsException(coroutine, classDescriptor, msg);
132 if (LIKELY(exc != nullptr)) {
133 coroutine->SetException(exc->GetCoreType());
134 }
135 ASSERT(coroutine->HasPendingException());
136 }
137
138 } // namespace ark::ets
139