• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include <cstring>
18 
19 #include "libabckit/include/c/abckit.h"
20 #include "helpers/helpers.h"
21 #include "helpers/helpers_runtime.h"
22 #include "libabckit/include/c/metadata_core.h"
23 #include "libabckit/include/c/extensions/js/metadata_js.h"
24 #include "ir_impl.h"
25 #include "libabckit/include/c/isa/isa_dynamic.h"
26 #include "libabckit/include/c/statuses.h"
27 
28 namespace libabckit::test {
29 
30 static auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 static auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 static auto g_implJsI = AbckitGetJsInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 static auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 static auto g_implJsM = AbckitGetJsModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35 static auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
36 static auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
37 
38 class LibAbcKitJSModifyApiModulesTest : public ::testing::Test {};
39 
40 static std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> g_expectedShema = {
41     {{},
42      {1},
43      {{4, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
44       {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
45       {6, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
46       {0, ABCKIT_ISA_API_DYNAMIC_OPCODE_CONSTANT, {}}}},
47     {{0},
48      {2},
49      {
50          {1, ABCKIT_ISA_API_DYNAMIC_OPCODE_WIDE_STPATCHVAR, {0}},
51          {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_WIDE_LDPATCHVAR, {}},
52          {3, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN, {2}},
53      }},
54     {{1}, {}, {}}};
55 
LoadStorePatchTest(AbckitGraph * graph)56 static void LoadStorePatchTest(AbckitGraph *graph)
57 {
58     AbckitInst *a = g_implG->gFindOrCreateConstantI32(graph, 4);
59     AbckitInst *patch = g_dynG->iCreateWideStpatchvar(graph, a, 0);
60     AbckitInst *lda = g_dynG->iCreateWideLdpatchvar(graph, 0);
61     AbckitInst *ret = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN);
62     g_implG->iSetInput(ret, lda, 0);
63     g_implG->iInsertBefore(patch, ret);
64     g_implG->iInsertBefore(lda, ret);
65 }
66 
67 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateWideStpatchvar, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateWideStpatchvar)68 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateWideStpatchvar)
69 {
70     auto output =
71         helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/patch/patch_test.abc", "patch_test");
72     auto expected = "1\n";
73     EXPECT_TRUE(helpers::Match(output, expected));
74     helpers::TransformMethod(
75         ABCKIT_ABC_DIR "ut/extensions/js/modify_api/patch/patch_test.abc",
76         ABCKIT_ABC_DIR "ut/extensions/js/modify_api/patch/patch_test_modified.abc", "foo",
77         [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) { LoadStorePatchTest(graph); },
78         [](AbckitGraph *graph) {
79             std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas(g_expectedShema);
80             helpers::VerifyGraph(graph, bbSchemas);
81         });
82     output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/patch/patch_test_modified.abc",
83                                         "patch_test");
84     expected = "4\n";
85     EXPECT_TRUE(helpers::Match(output, expected));
86 }
87 
88 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateWideLdpatchvar, abc-kind=JS, category=positive
TEST_F(LibAbcKitJSModifyApiModulesTest,DynamicIcreateWideLdpatchvar)89 TEST_F(LibAbcKitJSModifyApiModulesTest, DynamicIcreateWideLdpatchvar)
90 {
91     auto output =
92         helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/patch/patch_test.abc", "patch_test");
93     auto expected = "1\n";
94     EXPECT_TRUE(helpers::Match(output, expected));
95     helpers::TransformMethod(
96         ABCKIT_ABC_DIR "ut/extensions/js/modify_api/patch/patch_test.abc",
97         ABCKIT_ABC_DIR "ut/extensions/js/modify_api/patch/patch_test_modified.abc", "foo",
98         [](AbckitFile *, AbckitCoreFunction *, AbckitGraph *graph) { LoadStorePatchTest(graph); },
99         [](AbckitGraph *graph) {
100             std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas(g_expectedShema);
101             helpers::VerifyGraph(graph, bbSchemas);
102         });
103     output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/extensions/js/modify_api/patch/patch_test_modified.abc",
104                                         "patch_test");
105     expected = "4\n";
106     EXPECT_TRUE(helpers::Match(output, expected));
107 }
108 
109 }  // namespace libabckit::test
110