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/isa/isa_dynamic.h"
18 #include "libabckit/include/c/metadata_core.h"
19 #include "libabckit/include/c/ir_core.h"
20
21 #include "helpers/helpers_runtime.h"
22 #include "helpers/helpers.h"
23
24 #include <gtest/gtest.h>
25
26 // NOLINTBEGIN(readability-magic-numbers)
27 namespace libabckit::test {
28
29 namespace {
30 auto g_impl = AbckitGetApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
31 auto g_implI = AbckitGetInspectApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
32 auto g_implM = AbckitGetModifyApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
33 auto g_implG = AbckitGetGraphApiImpl(ABCKIT_VERSION_RELEASE_1_0_0);
34 auto g_dynG = AbckitGetIsaApiDynamicImpl(ABCKIT_VERSION_RELEASE_1_0_0);
35
TransformLoadStringIr(AbckitFile * file,AbckitGraph * graph)36 void TransformLoadStringIr(AbckitFile *file, AbckitGraph *graph)
37 {
38 auto *loadNullStr = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_LOADSTRING);
39 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
40 if (loadNullStr != nullptr) {
41 g_implG->iRemove(loadNullStr);
42 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
43 }
44
45 auto *ret = helpers::FindFirstInst(graph, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN);
46 ASSERT_NE(ret, nullptr);
47 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
48
49 auto *str = g_implM->createString(file, "foo", strlen("foo"));
50 ASSERT_NE(str, nullptr);
51 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
52
53 auto *loadStr = g_dynG->iCreateLoadString(graph, str);
54 ASSERT_NE(loadStr, nullptr);
55 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
56
57 g_implG->iInsertBefore(loadStr, ret);
58 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
59
60 g_implG->iSetInput(ret, loadStr, 0);
61 ASSERT_EQ(g_impl->getLastError(), ABCKIT_STATUS_NO_ERROR);
62 }
63
64 } // namespace
65
66 class LibAbcKitLoadStringDynamicTest : public ::testing::Test {};
67
68 // Test: test-kind=api, api=IsaApiDynamicImpl::iCreateLoadString, abc-kind=ArkTS2, category=positive, extension=c
TEST_F(LibAbcKitLoadStringDynamicTest,LibAbcKitTestLoadString)69 TEST_F(LibAbcKitLoadStringDynamicTest, LibAbcKitTestLoadString)
70 {
71 auto output = helpers::ExecuteDynamicAbc(ABCKIT_ABC_DIR "ut/isa/isa_dynamic/load_string/load_string_dynamic.abc",
72 "load_string_dynamic");
73 EXPECT_TRUE(helpers::Match(output, "asd\n"));
74
75 helpers::TransformMethod(
76 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/load_string/load_string_dynamic.abc",
77 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/load_string/load_string_dynamic_modified.abc", "load_string_dynamic.foo",
78 [](AbckitFile *file, AbckitCoreFunction * /*method*/, AbckitGraph *graph) {
79 TransformLoadStringIr(file, graph);
80 },
81 [](AbckitGraph *graph) {
82 std::vector<helpers::BBSchema<AbckitIsaApiDynamicOpcode>> bbSchemas(
83 {{{},
84 {1},
85 {{3, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
86 {4, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}},
87 {5, ABCKIT_ISA_API_DYNAMIC_OPCODE_PARAMETER, {}}}},
88 {{0},
89 {2},
90 {
91 {2, ABCKIT_ISA_API_DYNAMIC_OPCODE_LOADSTRING, {}},
92 {1, ABCKIT_ISA_API_DYNAMIC_OPCODE_RETURN, {2}},
93 }},
94 {{1}, {}, {}}});
95 helpers::VerifyGraph(graph, bbSchemas);
96 });
97
98 output = helpers::ExecuteDynamicAbc(
99 ABCKIT_ABC_DIR "ut/isa/isa_dynamic/load_string/load_string_dynamic_modified.abc", "load_string_dynamic");
100 EXPECT_TRUE(helpers::Match(output, "foo\n"));
101 }
102
103 } // namespace libabckit::test
104 // NOLINTEND(readability-magic-numbers)
105