1 /**
2 * Copyright (c) 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 "libabckit/include/c/metadata_core.h"
17 #include "libabckit/include/c/ir_core.h"
18 #include "libabckit/include/c/isa/isa_dynamic.h"
19 #include "libabckit/include/c/abckit.h"
20
21 #include "helpers/helpers.h"
22 #include "helpers/helpers_runtime.h"
23
24 #include <gtest/gtest.h>
25
26 // NOLINTBEGIN(readability-magic-numbers)
27
28 namespace libabckit::test {
29
30 class LibAbcKitDynamicTryCatchTest : public ::testing::Test {};
31
32 namespace {
33 auto *g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 auto *g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 auto *g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36 auto *g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
37 auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
38
39 constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "ut/ir_core/insert_try_catch/insert_try_catch_dynamic_wrapper.abc";
40 constexpr auto OUTPUT_PATH = ABCKIT_ABC_DIR "ut/ir_core/insert_try_catch/insert_try_catch_dynamic_wrapper_modified.abc";
41
42 enum class TryCatchScenario {
43 DEFAULT_POSITIVE = 0,
44 };
45
CreateBBSchema(TryCatchScenario scenario)46 [[maybe_unused]] std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> CreateBBSchema(TryCatchScenario scenario)
47 {
48 helpers::BBSchema<AbckitIsaApiDynamicOpcode> bb0 {{}, {1}, {}};
49
50 helpers::BBSchema<AbckitIsaApiDynamicOpcode> bb2 {{1}, {}, {}};
51
52 switch (scenario) {
53 case TryCatchScenario::DEFAULT_POSITIVE:
54 return {bb0,
55 {{0},
56 {2},
57 {
58 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_CREATEEMPTYOBJECT, {}},
59 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_LOADSTRING, {}},
60 {8, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN, {4}},
61 }},
62 bb2};
63 default:
64 LIBABCKIT_UNREACHABLE_TEST(DEBUG);
65 }
66 }
67 } // namespace
68
69 // Test: test-kind=api, api=GraphApiImpl::gInsertTryCatch, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynamicTryCatchTest,TryCatchWrapPositive)70 TEST_F(LibAbcKitDynamicTryCatchTest, TryCatchWrapPositive)
71 {
72 auto output = helpers::ExecuteDynamicAbc(INPUT_PATH, "insert_try_catch_dynamic_wrapper");
73 EXPECT_TRUE(helpers::Match(output, "TRY\nTHROW\nOUTER_CATCH\n"));
74
75 helpers::TransformMethod(
76 INPUT_PATH, OUTPUT_PATH, "bar",
77 [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
78 AbckitBasicBlock *startBB = g_implG->gGetStartBasicBlock(graph);
79
80 AbckitBasicBlock *tryFirstBB = helpers::BBgetSuccBlocks(startBB)[0];
81 AbckitBasicBlock *tryLastBB = g_implG->bbGetTrueBranch(tryFirstBB);
82
83 AbckitBasicBlock *catchFirstBB = g_implG->bbCreateEmpty(graph);
84 auto *str =
85 g_dynG->iCreateLoadString(graph, g_implM->createString(file, "INNER_CATCH", strlen("INNER_CATCH")));
86 g_implG->bbAddInstBack(catchFirstBB, str);
87 auto *print =
88 g_dynG->iCreateTryldglobalbyname(graph, g_implM->createString(file, "print", strlen("print")));
89 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
90 g_implG->bbAddInstBack(catchFirstBB, print);
91 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
92 auto *callArg = g_dynG->iCreateCallarg1(graph, print, str);
93 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
94 g_implG->bbAddInstBack(catchFirstBB, callArg);
95
96 g_implG->gInsertTryCatch(tryFirstBB, tryLastBB, catchFirstBB, catchFirstBB);
97 },
98 [&]([[maybe_unused]] AbckitGraph *graph) {});
99
100 output = helpers::ExecuteDynamicAbc(OUTPUT_PATH, "insert_try_catch_dynamic_wrapper");
101 EXPECT_TRUE(helpers::Match(output, "TRY\nTHROW\nINNER_CATCH\n"));
102 }
103
104 // Test: test-kind=api, api=GraphApiImpl::gInsertTryCatch, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynamicTryCatchTest,TryCatchWrapPositiveTwoBBs)105 TEST_F(LibAbcKitDynamicTryCatchTest, TryCatchWrapPositiveTwoBBs)
106 {
107 auto output = helpers::ExecuteDynamicAbc(INPUT_PATH, "insert_try_catch_dynamic_wrapper");
108 EXPECT_TRUE(helpers::Match(output, "TRY\nTHROW\nOUTER_CATCH\n"));
109
110 helpers::TransformMethod(
111 INPUT_PATH, OUTPUT_PATH, "bar",
112 [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
113 AbckitBasicBlock *startBB = g_implG->gGetStartBasicBlock(graph);
114
115 AbckitBasicBlock *tryFirstBB = helpers::BBgetSuccBlocks(startBB)[0];
116 AbckitBasicBlock *tryLastBB = g_implG->bbGetTrueBranch(tryFirstBB);
117
118 AbckitBasicBlock *catchFirstBB = g_implG->bbCreateEmpty(graph);
119 auto *str = g_dynG->iCreateLoadString(
120 graph, g_implM->createString(file, "INNER_CATCH_BB1", strlen("INNER_CATCH_BB1")));
121 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
122 g_implG->bbAddInstBack(catchFirstBB, str);
123 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
124 auto *print =
125 g_dynG->iCreateTryldglobalbyname(graph, g_implM->createString(file, "print", strlen("print")));
126 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
127 g_implG->bbAddInstBack(catchFirstBB, print);
128 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
129 auto *callArg = g_dynG->iCreateCallarg1(graph, print, str);
130 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
131 g_implG->bbAddInstBack(catchFirstBB, callArg);
132 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
133
134 AbckitBasicBlock *catchLastBB = g_implG->bbCreateEmpty(graph);
135 auto *str0 = g_dynG->iCreateLoadString(
136 graph, g_implM->createString(file, "INNER_CATCH_BB2", strlen("INNER_CATCH_BB2")));
137 g_implG->bbAddInstBack(catchLastBB, str0);
138 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
139 auto *print0 =
140 g_dynG->iCreateTryldglobalbyname(graph, g_implM->createString(file, "print", strlen("print")));
141 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
142 g_implG->bbAddInstBack(catchLastBB, print0);
143 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
144 auto *callArg0 = g_dynG->iCreateCallarg1(graph, print0, str0);
145 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
146 g_implG->bbAddInstBack(catchLastBB, callArg0);
147 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
148
149 g_implG->bbAppendSuccBlock(catchFirstBB, catchLastBB);
150
151 g_implG->gInsertTryCatch(tryFirstBB, tryLastBB, catchFirstBB, catchLastBB);
152 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
153 },
154 [&]([[maybe_unused]] AbckitGraph *graph) {});
155
156 output = helpers::ExecuteDynamicAbc(OUTPUT_PATH, "insert_try_catch_dynamic_wrapper");
157 EXPECT_TRUE(helpers::Match(output, "TRY\nTHROW\nINNER_CATCH_BB1\nINNER_CATCH_BB2\n"));
158 }
159
160 // Test: test-kind=internal, abc-kind=ArkTS1, category=internal, extension=c
TEST_F(LibAbcKitDynamicTryCatchTest,InsertTryCatchValid)161 TEST_F(LibAbcKitDynamicTryCatchTest, InsertTryCatchValid)
162 {
163 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/insert_try_catch/insert_try_catch_dynamic.abc",
164 "insert_try_catch_dynamic");
165 EXPECT_TRUE(helpers::Match(output, "TRY1\nTRY2\nCATCH\nTRY1\nTRY3\n"));
166
167 helpers::TransformMethod(
168 ABCKIT_ABC_DIR "ut/ir_core/insert_try_catch/insert_try_catch_dynamic.abc",
169 ABCKIT_ABC_DIR "ut/ir_core/insert_try_catch/insert_try_catch_dynamic_modified.abc", "main",
170 []([[maybe_unused]] AbckitFile *file, [[maybe_unused]] AbckitCoreFunction *method,
171 [[maybe_unused]] AbckitGraph *graph) {},
172 [&]([[maybe_unused]] AbckitGraph *graph) {});
173
174 output = helpers::ExecuteDynamicAbc(
175 ABCKIT_ABC_DIR "ut/ir_core/insert_try_catch/insert_try_catch_dynamic_modified.abc", "insert_try_catch_dynamic");
176 EXPECT_TRUE(helpers::Match(output, "TRY1\nTRY2\nCATCH\nTRY1\nTRY3\n"));
177 }
178
179 } // namespace libabckit::test
180
181 // NOLINTEND(readability-magic-numbers)
182