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 #ifndef PANDA_RUNTIME_EXCEPTIONS_H_
16 #define PANDA_RUNTIME_EXCEPTIONS_H_
17 
18 #include "runtime/include/class-inl.h"
19 #include "runtime/include/coretypes/array.h"
20 #include "runtime/include/language_context.h"
21 #include "runtime/include/method.h"
22 #include "runtime/include/stack_walker.h"
23 
24 namespace ark {
25 
26 void ThrowException(const LanguageContext &ctx, ManagedThread *thread, const uint8_t *mutf8Name,
27                     const uint8_t *mutf8Msg);
28 
29 PANDA_PUBLIC_API void ThrowNullPointerException();
30 // This function could be used in case there are no managed stack frames.
31 // For example when the main thread creates an instance of VM and calls
32 // a native function which throws an exception. In this case there is no need to
33 // get current executing method from the managed stack.
34 PANDA_PUBLIC_API void ThrowNullPointerException(const LanguageContext &ctx, ManagedThread *thread);
35 
36 void ThrowStackOverflowException(ManagedThread *thread);
37 
38 void ThrowArrayIndexOutOfBoundsException(coretypes::ArraySsizeT idx, coretypes::ArraySizeT length);
39 void ThrowArrayIndexOutOfBoundsException(coretypes::ArraySsizeT idx, coretypes::ArraySizeT length,
40                                          const LanguageContext &ctx, ManagedThread *thread);
41 
42 void ThrowIndexOutOfBoundsException(coretypes::ArraySsizeT idx, coretypes::ArraySsizeT length);
43 
44 void ThrowIllegalStateException(const PandaString &msg);
45 
46 void ThrowStringIndexOutOfBoundsException(coretypes::ArraySsizeT idx, coretypes::ArraySizeT length);
47 
48 void ThrowNegativeArraySizeException(coretypes::ArraySsizeT size);
49 
50 void ThrowNegativeArraySizeException(const PandaString &msg);
51 
52 void ThrowArithmeticException();
53 
54 void ThrowClassCastException(const Class *dstType, const Class *srcType);
55 
56 void ThrowAbstractMethodError(const Method *method);
57 
58 void ThrowIncompatibleClassChangeErrorForMethodConflict(const Method *method);
59 
60 void ThrowArrayStoreException(const Class *arrayClass, const Class *elementClass);
61 
62 void ThrowArrayStoreException(const PandaString &msg);
63 
64 void ThrowRuntimeException(const PandaString &msg);
65 
66 void ThrowFileNotFoundException(const PandaString &msg);
67 
68 void ThrowIOException(const PandaString &msg);
69 
70 void ThrowIllegalArgumentException(const PandaString &msg);
71 
72 void ThrowClassCircularityError(const PandaString &className, const LanguageContext &ctx);
73 
74 void ThrowOutOfMemoryError(ManagedThread *thread, const PandaString &msg);
75 
76 PANDA_PUBLIC_API void ThrowOutOfMemoryError(const PandaString &msg);
77 
78 void FindCatchBlockInCallStack(ManagedThread *thread);
79 
80 void DropCFrameIfNecessary(ManagedThread *thread, StackWalker *stack, Frame *origFrame, FrameAccessor nextFrame,
81                            Method *method);
82 
83 void FindCatchBlockInCFrames(ManagedThread *thread, StackWalker *stack, Frame *origFrame);
84 
85 void ThrowIllegalAccessException(const PandaString &msg);
86 
87 void ThrowUnsupportedOperationException();
88 
89 void ThrowVerificationException(const PandaString &msg);
90 
91 void ThrowVerificationException(const LanguageContext &ctx, const PandaString &msg);
92 
93 void ThrowInstantiationError(const PandaString &msg);
94 
95 void ThrowNoClassDefFoundError(const PandaString &msg);
96 
97 void ThrowTypedErrorDyn(const std::string &msg);
98 
99 void ThrowReferenceErrorDyn(const std::string &msg);
100 
101 void ThrowIllegalMonitorStateException(const PandaString &msg);
102 
103 void ThrowCloneNotSupportedException();
104 
105 void HandlePendingException(UnwindPolicy policy = UnwindPolicy::ALL);
106 
SetExceptionEvent([[maybe_unused]] events::ExceptionType type, [[maybe_unused]] ManagedThread *thread)107 inline void SetExceptionEvent([[maybe_unused]] events::ExceptionType type, [[maybe_unused]] ManagedThread *thread)
108 {
109 #ifdef PANDA_EVENTS_ENABLED
110     auto stack = StackWalker::Create(thread);
111     EVENT_EXCEPTION(std::string(stack.GetMethod()->GetFullName()), stack.GetBytecodePc(), stack.GetNativePc(), type);
112 #endif
113 }
114 
115 }  // namespace ark
116 
117 #endif  // PANDA_RUNTIME_EXCEPTIONS_H_
118