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 #include "runtime/include/runtime.h"
18 #include "runtime/include/runtime_options.h"
19
20 namespace panda::mem::test {
21
22 class MemLeakTest : public testing::Test {
23 public:
CreateRuntime()24 void CreateRuntime()
25 {
26 RuntimeOptions options;
27 options.SetShouldLoadBootPandaFiles(false);
28 options.SetShouldInitializeIntrinsics(false);
29 options.SetRunGcInPlace(true);
30 Runtime::Create(options);
31 }
32 };
33
34 #ifndef NDEBUG
35
TEST_F(MemLeakTest,MemLeak4BTest)36 TEST_F(MemLeakTest, MemLeak4BTest)
37 {
38 EXPECT_DEATH(
39 {
40 CreateRuntime();
41 mem::InternalAllocatorPtr allocator = Runtime::GetCurrent()->GetInternalAllocator();
42 auto ptr = allocator->Alloc(4);
43 ASSERT_NE(ptr, nullptr);
44
45 Runtime::Destroy();
46 },
47 "");
48 }
49
TEST_F(MemLeakTest,MemLeak1KBTest)50 TEST_F(MemLeakTest, MemLeak1KBTest)
51 {
52 EXPECT_DEATH(
53 {
54 CreateRuntime();
55 mem::InternalAllocatorPtr allocator = Runtime::GetCurrent()->GetInternalAllocator();
56 auto ptr = allocator->Alloc(1_KB);
57 ASSERT_NE(ptr, nullptr);
58
59 Runtime::Destroy();
60 },
61 "");
62 }
63
TEST_F(MemLeakTest,MemLeak1MBTest)64 TEST_F(MemLeakTest, MemLeak1MBTest)
65 {
66 EXPECT_DEATH(
67 {
68 CreateRuntime();
69 mem::InternalAllocatorPtr allocator = Runtime::GetCurrent()->GetInternalAllocator();
70 auto ptr = allocator->Alloc(1_MB);
71 ASSERT_NE(ptr, nullptr);
72
73 Runtime::Destroy();
74 },
75 "");
76 }
77
78 // TODO(alovkov): add test for FreeInternalMemory #4618
79
80 #endif // NDEBUG
81
82 } // namespace panda::mem::test
83