• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 panda::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.SetBootClassSpaces({"core"});
45         options.SetRuntimeType("core");
46         options.SetGcType("epsilon");
47         options.SetGcTriggerType("debug-never");
48         auto exec_path = panda::os::file::File::GetExecutablePath();
49         std::string panda_std_lib =
50             exec_path.Value() + separator() + ".." + separator() + "pandastdlib" + separator() + "pandastdlib.bin";
51         options.SetBootPandaFiles({panda_std_lib});
52 
53         Runtime::Create(options);
54     }
55 
~StaticAnalyzerTest()56     ~StaticAnalyzerTest()
57     {
58         Runtime::Destroy();
59     }
60 
AllocString()61     coretypes::String *AllocString()
62     {
63         Runtime *runtime = Runtime::GetCurrent();
64         LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
65         ScopedManagedCodeThread s(MTManagedThread::GetCurrent());
66         return coretypes::String::CreateEmptyString(ctx, runtime->GetPandaVM());
67     }
68 
AllocStringArray(size_t length)69     coretypes::Array *AllocStringArray(size_t length)
70     {
71         Runtime *runtime = Runtime::GetCurrent();
72         LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
73         SpaceType space_type = SpaceType::SPACE_TYPE_OBJECT;
74         auto *klass = runtime->GetClassLinker()->GetExtension(ctx)->GetClassRoot(ClassRoot::ARRAY_STRING);
75         ScopedManagedCodeThread s(MTManagedThread::GetCurrent());
76         return coretypes::Array::Create(klass, length, space_type);
77     }
78 };
79 
TEST_F(StaticAnalyzerTest,TestArray)80 TEST_F(StaticAnalyzerTest, TestArray)
81 {
82     coretypes::Array *array = AllocStringArray(2);
83     ASSERT_NE(nullptr, array);
84     ObjectHeader *expected = AllocString();
85     ASSERT_NE(nullptr, expected);
86     array->Set(0U, expected);  // SUPPRESS_CSA
87     // SUPPRESS_CSA_NEXTLINE
88     array->Set(1U, expected);
89 
90     size_t count = 0;
91     // SUPPRESS_CSA_NEXTLINE(alpha.core.WasteObjHeader)
92     auto handler = [array, &count, expected](ObjectHeader *obj, ObjectHeader *ref, uint32_t offset, bool is_volatile) {
93         ++count;
94         EXPECT_EQ(array, obj);
95         EXPECT_EQ(expected, ref);
96         EXPECT_EQ(ref, ObjectAccessor::GetObject<true>(obj, offset));
97         EXPECT_FALSE(is_volatile);
98         return true;
99     };
100     GCStaticObjectHelpers::TraverseAllObjectsWithInfo(array, handler);  // SUPPRESS_CSA(alpha.core.WasteObjHeader)
101     ASSERT_EQ(2U, count);
102 }
103 }  // namespace panda::mem
104