• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-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 
18 #include "assembly-parser.h"
19 #include "runtime/include/runtime.h"
20 #include "runtime/include/panda_vm.h"
21 #include "runtime/include/class_linker.h"
22 #include "runtime/include/thread_scopes.h"
23 #include "runtime/mem/vm_handle.h"
24 #include "runtime/handle_scope-inl.h"
25 #include "runtime/include/coretypes/array.h"
26 #include "runtime/include/coretypes/string.h"
27 #include "runtime/mem/object_helpers-inl.h"
28 
29 namespace ark::mem {
Separator()30 inline std::string Separator()
31 {
32 #ifdef _WIN32
33     return "\\";
34 #else
35     return "/";
36 #endif
37 }
38 
39 class StaticAnalyzerTest : public testing::Test {
40 public:
StaticAnalyzerTest()41     StaticAnalyzerTest()
42     {
43         RuntimeOptions options;
44         options.SetLoadRuntimes({"core"});
45         options.SetGcType("epsilon");
46         options.SetGcTriggerType("debug-never");
47         auto execPath = ark::os::file::File::GetExecutablePath();
48         std::string pandaStdLib =
49             execPath.Value() + Separator() + ".." + Separator() + "pandastdlib" + Separator() + "pandastdlib.bin";
50         options.SetBootPandaFiles({pandaStdLib});
51 
52         Runtime::Create(options);
53     }
54 
~StaticAnalyzerTest()55     ~StaticAnalyzerTest() override
56     {
57         Runtime::Destroy();
58     }
59 
60     NO_COPY_SEMANTIC(StaticAnalyzerTest);
61     NO_MOVE_SEMANTIC(StaticAnalyzerTest);
62 
AllocString()63     coretypes::String *AllocString()
64     {
65         Runtime *runtime = Runtime::GetCurrent();
66         LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
67         ScopedManagedCodeThread s(MTManagedThread::GetCurrent());
68         return coretypes::String::CreateEmptyString(ctx, runtime->GetPandaVM());
69     }
70 
AllocStringArray(size_t length)71     coretypes::Array *AllocStringArray(size_t length)
72     {
73         Runtime *runtime = Runtime::GetCurrent();
74         LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
75         SpaceType spaceType = SpaceType::SPACE_TYPE_OBJECT;
76         auto *klass = runtime->GetClassLinker()->GetExtension(ctx)->GetClassRoot(ClassRoot::ARRAY_STRING);
77         ScopedManagedCodeThread s(MTManagedThread::GetCurrent());
78         return coretypes::Array::Create(klass, length, spaceType);
79     }
80 };
81 
TEST_F(StaticAnalyzerTest,TestArray)82 TEST_F(StaticAnalyzerTest, TestArray)
83 {
84     coretypes::Array *array = AllocStringArray(2);
85     ASSERT_NE(nullptr, array);
86     ObjectHeader *expected = AllocString();
87     ASSERT_NE(nullptr, expected);
88     array->Set(0U, expected);  // SUPPRESS_CSA
89     // SUPPRESS_CSA_NEXTLINE
90     array->Set(1U, expected);
91 
92     size_t count = 0;
93     // SUPPRESS_CSA_NEXTLINE(alpha.core.WasteObjHeader)
94     auto handler = [array, &count, expected](ObjectHeader *obj, ObjectHeader *ref, uint32_t offset, bool isVolatile) {
95         ++count;
96         EXPECT_EQ(array, obj);
97         EXPECT_EQ(expected, ref);
98         EXPECT_EQ(ref, ObjectAccessor::GetObject<true>(obj, offset));
99         EXPECT_FALSE(isVolatile);
100         return true;
101     };
102     // SUPPRESS_CSA_NEXTLINE(alpha.core.WasteObjHeader)
103     GCStaticObjectHelpers::TraverseAllObjectsWithInfo<false>(array, handler);
104     ASSERT_EQ(2U, count);
105 }
106 
107 class ClearA {
108     NO_COPY_SEMANTIC(ClearA);
109     NO_MOVE_SEMANTIC(ClearA);
110 
111 public:
112     ClearA() = default;
VirtualAllocString(StaticAnalyzerTest & sat)113     virtual ObjectHeader *VirtualAllocString([[maybe_unused]] StaticAnalyzerTest &sat)
114     {
115         return nullptr;
116     }
117 
118     virtual ~ClearA() = default;
119 };
120 
121 class TriggeredB : public ClearA {
122     NO_COPY_SEMANTIC(TriggeredB);
123     NO_MOVE_SEMANTIC(TriggeredB);
124 
VirtualAllocString(StaticAnalyzerTest & sat)125     ObjectHeader *VirtualAllocString(StaticAnalyzerTest &sat) override
126     {
127         return sat.AllocString();
128     }
129 
130 public:
131     TriggeredB() = default;
132     ~TriggeredB() override = default;
133 };
134 
TEST_F(StaticAnalyzerTest,TestVirtualFuncs)135 TEST_F(StaticAnalyzerTest, TestVirtualFuncs)
136 {
137     coretypes::Array *array = AllocStringArray(2);
138     ASSERT_NE(nullptr, array);
139     std::unique_ptr<ClearA> ca = std::make_unique<TriggeredB>();
140     ObjectHeader *expected = ca->VirtualAllocString(*this);
141     ASSERT_NE(nullptr, expected);
142     array->Set(0U, expected);  // SUPPRESS_CSA
143 }
144 }  // namespace ark::mem
145