1 /**
2 * Copyright (c) 2024-2025 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/ir_core.h"
17 #include "libabckit/include/c/isa/isa_static.h"
18 #include "libabckit/include/c/metadata_core.h"
19 #include "libabckit/include/c/abckit.h"
20
21 #include "helpers/helpers.h"
22 #include "helpers/helpers_runtime.h"
23 #include "libabckit/include/c/statuses.h"
24
25 #include <gtest/gtest.h>
26
27 // NOLINTBEGIN(readability-magic-numbers)
28 namespace libabckit::test {
29
30 class LibAbcKitStaticCatchPhiTest : 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_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
38
39 enum class TryCatchScenario {
40 DEFAULT_POSITIVE = 0,
41 };
42 } // namespace
43
44 // Test: test-kind=api, api=GraphApiImpl::bbCreateCatchPhi, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitStaticCatchPhiTest,CatchPhiStaticValid)45 TEST_F(LibAbcKitStaticCatchPhiTest, CatchPhiStaticValid)
46 {
47 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static.abc",
48 "catchphi_static/ETSGLOBAL", "main");
49 EXPECT_TRUE(helpers::Match(output, "CATCH\n"));
50
51 helpers::TransformMethod(
52 ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static.abc",
53 ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static_modified.abc", "foo",
54 [](AbckitFile * /*file*/, AbckitCoreFunction *method, AbckitGraph *graph) {
55 auto *oldCall = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC);
56 EXPECT_NE(oldCall, nullptr);
57 auto *log = helpers::FindMethodByName(g_implI->functionGetFile(method), "LogError");
58 auto *catchPhi = g_implG->bbCreateCatchPhi(g_implG->iGetBasicBlock(oldCall), 0);
59 auto *newCall = g_statG->iCreateCallStatic(graph, log, 1, catchPhi);
60 g_implG->iInsertBefore(newCall, oldCall);
61 },
62 [&]([[maybe_unused]] AbckitGraph *graph) {});
63
64 output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static_modified.abc",
65 "catchphi_static/ETSGLOBAL", "main");
66 EXPECT_TRUE(helpers::Match(output, "Error: abckit_error\nCATCH\n"));
67 }
68
69 // Test: test-kind=api, api=GraphApiImpl::bbCreateCatchPhi, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitStaticCatchPhiTest,CatchPhiNoAccStaticValid)70 TEST_F(LibAbcKitStaticCatchPhiTest, CatchPhiNoAccStaticValid)
71 {
72 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static.abc",
73 "catchphi_static/ETSGLOBAL", "main0");
74 EXPECT_TRUE(helpers::Match(output, "75\n13\n"));
75
76 helpers::TransformMethod(
77 ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static.abc",
78 ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static_modified_noacc.abc", "bar",
79 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
80 struct VisitorData {
81 const AbckitGraphApi *implG;
82 AbckitBasicBlock *returnBB;
83 } vd {g_implG, nullptr};
84
85 g_implG->gVisitBlocksRpo(graph, &vd, [](AbckitBasicBlock *curBB, void *data) {
86 const AbckitGraphApi *gImplG = static_cast<struct VisitorData *>(data)->implG;
87 if (gImplG->bbIsCatchBegin(curBB)) {
88 static_cast<struct VisitorData *>(data)->returnBB = curBB;
89 return false;
90 }
91 return true;
92 });
93
94 auto *throwableCall = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC);
95 ASSERT_NE(throwableCall, nullptr);
96 AbckitBasicBlock *returnBB = vd.returnBB;
97 ASSERT_NE(returnBB, nullptr);
98 AbckitInst *returnInst = g_implG->bbGetFirstInst(returnBB);
99 ASSERT_NE(returnInst, nullptr);
100 auto constantI64Impl = g_implG->gFindOrCreateConstantI32(graph, 42);
101 AbckitInst *catchPhi = g_implG->bbCreateCatchPhi(returnBB, 2, constantI64Impl, throwableCall);
102 ASSERT_NE(catchPhi, nullptr);
103 g_implG->iSetInput(returnInst, catchPhi, 0);
104 },
105 [&]([[maybe_unused]] AbckitGraph *ctxG) {});
106
107 output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static_modified_noacc.abc",
108 "catchphi_static/ETSGLOBAL", "main0");
109 EXPECT_TRUE(helpers::Match(output, "75\n42\n"));
110 }
111
112 // Test: test-kind=api, api=GraphApiImpl::bbCreateCatchPhi, abc-kind=ArkTS2, category=negative
TEST_F(LibAbcKitStaticCatchPhiTest,CatchPhiNoAccStaticInvalid)113 TEST_F(LibAbcKitStaticCatchPhiTest, CatchPhiNoAccStaticInvalid)
114 {
115 auto output = helpers::ExecuteStaticAbc(ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static.abc",
116 "catchphi_static/ETSGLOBAL", "main0");
117 EXPECT_TRUE(helpers::Match(output, "75\n13\n"));
118
119 helpers::TransformMethod(
120 ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static.abc",
121 ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_static_modified_noacc_neg.abc", "bar",
122 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
123 struct VisitorData {
124 const AbckitGraphApi *implG;
125 AbckitBasicBlock *returnBB;
126 } vd {g_implG, nullptr};
127
128 g_implG->gVisitBlocksRpo(graph, &vd, [](AbckitBasicBlock *curBB, void *data) {
129 const AbckitGraphApi *gImplG = static_cast<struct VisitorData *>(data)->implG;
130 if (gImplG->bbIsCatchBegin(curBB)) {
131 static_cast<struct VisitorData *>(data)->returnBB = curBB;
132 return false;
133 }
134 return true;
135 });
136
137 auto *throwableCall = helpers::FindFirstInst(graph, ABCKIT_ISA_API_STATIC_OPCODE_CALL_STATIC);
138 ASSERT_NE(throwableCall, nullptr);
139 AbckitBasicBlock *returnBB = vd.returnBB;
140 ASSERT_NE(returnBB, nullptr);
141 AbckitInst *returnInst = g_implG->bbGetFirstInst(returnBB);
142 ASSERT_NE(returnInst, nullptr);
143 auto constantI64Impl = g_implG->gFindOrCreateConstantI64(graph, 42);
144 // Mix integer and reference types as input
145 AbckitInst *catchPhi =
146 g_implG->bbCreateCatchPhi(returnBB, 4, constantI64Impl, throwableCall, throwableCall, throwableCall);
147 ASSERT_EQ(catchPhi, nullptr);
148 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_BAD_ARGUMENT);
149 // Call no side-effect API to clear last error
150 ASSERT_NE(g_implG->iGetInputCount(returnInst), 0);
151 },
152 [&]([[maybe_unused]] AbckitGraph *ctxG) {});
153 }
154
155 } // namespace libabckit::test
156 // NOLINTEND(readability-magic-numbers)
157