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/abckit.h"
17 #include "libabckit/include/c/ir_core.h"
18
19 #include "helpers/helpers.h"
20 #include "helpers/helpers_runtime.h"
21
22 #include <gtest/gtest.h>
23
24 namespace libabckit::test {
25
26 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
27 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
28 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
29 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
30 static auto g_statG = AbckitGetIsaApiStaticImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31
32 struct ReplaceContext {
33 AbckitGraph *ctx;
34 AbckitCoreFunction *oldMethod;
35 AbckitCoreFunction *newMethod;
36 };
37
EnumerateInsts(ReplaceContext * rctx,AbckitBasicBlock * bb)38 void EnumerateInsts(ReplaceContext *rctx, AbckitBasicBlock *bb)
39 {
40 auto *inst = g_implG->bbGetFirstInst(bb);
41 while (inst != nullptr) {
42 if (g_implG->iCheckIsCall(inst) && g_implG->iGetFunction(inst) == rctx->oldMethod) {
43 auto *call = g_statG->iCreateCallStatic(rctx->ctx, rctx->newMethod, 2, g_implG->iGetInput(inst, 0),
44 g_implG->iGetInput(inst, 1));
45 helpers::ReplaceInst(inst, call);
46 }
47 inst = g_implG->iGetNext(inst);
48 }
49 }
50
VisitAllBBs(ReplaceContext * rctx,AbckitBasicBlock * bb,std::set<AbckitBasicBlock * > & visitedBB)51 void VisitAllBBs(ReplaceContext *rctx, AbckitBasicBlock *bb, std::set<AbckitBasicBlock *> &visitedBB)
52 {
53 if (visitedBB.find(bb) != visitedBB.end()) {
54 return;
55 }
56 EnumerateInsts(rctx, bb);
57 visitedBB.emplace(bb);
58 for (int i = 0; i < g_implG->bbGetSuccBlockCount(bb); i++) {
59 VisitAllBBs(rctx, g_implG->bbGetSuccBlock(bb, i), visitedBB);
60 }
61 }
62
TransformIR(ReplaceContext * rctx)63 void TransformIR(ReplaceContext *rctx)
64 {
65 std::set<AbckitBasicBlock *> visitedBlocks;
66 auto *start = g_implG->gGetStartBasicBlock(rctx->ctx);
67 VisitAllBBs(rctx, start, visitedBlocks);
68 }
69
70 class AbckitScenarioTest : public ::testing::Test {};
71
72 // Test: test-kind=scenario, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(AbckitScenarioTest,LibAbcKitTestReplaceCallSite)73 TEST_F(AbckitScenarioTest, LibAbcKitTestReplaceCallSite)
74 {
75 constexpr auto INPUT_PATH = ABCKIT_ABC_DIR "scenarios/replace_callsite/replace_callsite_static.abc";
76 auto output = helpers::ExecuteStaticAbc(INPUT_PATH, "replace_callsite_static/ETSGLOBAL", "main");
77 EXPECT_TRUE(helpers::Match(output, "4\n3\n"));
78 AbckitFile *file = g_impl->openAbc(INPUT_PATH, strlen(INPUT_PATH));
79
80 helpers::TransformMethod(file, "main", [file](AbckitFile *, AbckitCoreFunction *, AbckitGraph *ctxG) {
81 auto *newMethod = helpers::FindMethodByName(file, "fixedApi");
82 ASSERT_NE(newMethod, nullptr);
83 auto *oldMethod = helpers::FindMethodByName(file, "api");
84 ASSERT_NE(oldMethod, nullptr);
85 ReplaceContext rctx = {ctxG, oldMethod, newMethod};
86 TransformIR(&rctx);
87 });
88 constexpr auto OUTPUT_PATH = ABCKIT_ABC_DIR "scenarios/replace_callsite/replace_callsite_static_modified.abc";
89 g_impl->writeAbc(file, OUTPUT_PATH, strlen(OUTPUT_PATH));
90 g_impl->closeFile(file);
91 output = helpers::ExecuteStaticAbc(OUTPUT_PATH, "replace_callsite_static/ETSGLOBAL", "main");
92 EXPECT_TRUE(helpers::Match(output, "5\n4\n"));
93 }
94
95 } // namespace libabckit::test
96