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/isa/isa_dynamic.h"
17 #include "libabckit/include/c/metadata_core.h"
18 #include "libabckit/include/c/abckit.h"
19
20 #include "helpers/helpers.h"
21 #include "helpers/helpers_runtime.h"
22
23 #include <gtest/gtest.h>
24
25 // NOLINTBEGIN(readability-magic-numbers)
26 namespace libabckit::test {
27
28 class LibAbcKitDynamicCatchPhiTest : public ::testing::Test {};
29
30 namespace {
31 auto *g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 auto *g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 auto *g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 auto *g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 auto *g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36
37 enum class TryCatchScenario {
38 DEFAULT_POSITIVE = 0,
39 };
40
CreateBBSchema(TryCatchScenario scenario)41 [[maybe_unused]] std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> CreateBBSchema(TryCatchScenario scenario)
42 {
43 helpers::BBSchema<AbckitIsaApiDynamicOpcode> bb0 {{}, {1}, {}};
44
45 helpers::BBSchema<AbckitIsaApiDynamicOpcode> bb2 {{1}, {}, {}};
46
47 switch (scenario) {
48 case TryCatchScenario::DEFAULT_POSITIVE:
49 return {bb0,
50 {{0},
51 {2},
52 {
53 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_CREATEEMPTYOBJECT, {}},
54 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_LOADSTRING, {}},
55 {8, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN, {4}},
56 }},
57 bb2};
58 default:
59 LIBABCKIT_UNREACHABLE_TEST(DEBUG);
60 }
61 }
62 } // namespace
63
64 // Test: test-kind=api, api=GraphApiImpl::bbCreateCatchPhi, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynamicCatchPhiTest,CatchPhiDynamicValid)65 TEST_F(LibAbcKitDynamicCatchPhiTest, CatchPhiDynamicValid)
66 {
67 auto output =
68 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_dynamic.abc", "catchphi_dynamic");
69 EXPECT_TRUE(helpers::Match(output, "CATCH\n"));
70
71 helpers::TransformMethod(
72 ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_dynamic.abc",
73 ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_dynamic_modified.abc", "main",
74 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
75 auto *print = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG1);
76 auto *catchPhi = g_implG->bbCreateCatchPhi(g_implG->iGetBasicBlock(print), 0);
77 g_implG->iSetInput(print, catchPhi, 1);
78 },
79 [&]([[maybe_unused]] AbckitGraph *graph) {});
80
81 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_dynamic_modified.abc",
82 "catchphi_dynamic");
83 EXPECT_TRUE(helpers::Match(output, "Error: abckit_error\n"));
84 }
85
86 // Test: test-kind=api, api=GraphApiImpl::bbCreateCatchPhi, abc-kind=ArkTS1, category=positive, extension=c
TEST_F(LibAbcKitDynamicCatchPhiTest,CatchPhiNoAccDynamicValid)87 TEST_F(LibAbcKitDynamicCatchPhiTest, CatchPhiNoAccDynamicValid)
88 {
89 auto output =
90 helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_dynamic.abc", "catchphi_dynamic");
91 EXPECT_TRUE(helpers::Match(output, "CATCH\n"));
92
93 helpers::TransformMethod(
94 ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_dynamic.abc",
95 ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_dynamic_noacc_modified.abc", "main",
96 [](AbckitFile * /*file*/, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
97 auto *print = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_CALLARG1);
98 auto constantI64Impl = g_implG->gFindOrCreateConstantI32(graph, 42);
99 auto *catchPhi = g_implG->bbCreateCatchPhi(g_implG->iGetBasicBlock(print), 2, constantI64Impl, print);
100 g_implG->iSetInput(print, catchPhi, 1);
101 },
102 [&]([[maybe_unused]] AbckitGraph *graph) {});
103
104 output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/ir_core/catchphi/catchphi_dynamic_noacc_modified.abc",
105 "catchphi_dynamic");
106 EXPECT_TRUE(helpers::Match(output, "42\n"));
107 }
108
109 } // namespace libabckit::test
110 // NOLINTEND(readability-magic-numbers)
111