• 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 "libpandabase/utils/asan_interface.h"
19 #include "libpandabase/mem/mem.h"
20 #include "libpandabase/os/mem.h"
21 #include "runtime/mem/tlab.h"
22 
23 namespace panda::mem {
24 
25 constexpr size_t TLAB_TEST_SIZE = 4_MB;
26 
27 class TLABTest : public testing::Test {
28 public:
TLABTest()29     TLABTest()
30     {
31         // Logger::InitializeStdLogging(Logger::Level::DEBUG, Logger::Component::ALL);
32 #ifdef PANDA_NIGHTLY_TEST_ON
33         seed_ = time(NULL);
34 #else
35         seed_ = 0x0BADDEAD;
36 #endif
37         srand(seed_);
38     }
39 
~TLABTest()40     ~TLABTest()
41     {
42         for (auto i : allocated_mem_mmap_) {
43             panda::os::mem::UnmapRaw(std::get<0>(i), std::get<1>(i));
44         }
45         // Logger::Destroy();
46     }
47 
48 protected:
CreateNewTLAB()49     TLAB *CreateNewTLAB()
50     {
51         void *mem = panda::os::mem::MapRWAnonymousRaw(TLAB_TEST_SIZE);
52         ASAN_UNPOISON_MEMORY_REGION(mem, TLAB_TEST_SIZE);
53         std::pair<void *, size_t> new_pair {mem, TLAB_TEST_SIZE};
54         allocated_mem_mmap_.push_back(new_pair);
55         auto newTLAB =
56             new (mem) TLAB(ToVoidPtr(ToUintPtr(mem) + sizeof(mem::TLAB)), TLAB_TEST_SIZE - sizeof(mem::TLAB));
57         return newTLAB;
58     }
59 
60     std::vector<std::pair<void *, size_t>> allocated_mem_mmap_;
61     unsigned seed_;
62 };
63 
TEST_F(TLABTest,AccessTest)64 TEST_F(TLABTest, AccessTest)
65 {
66     static constexpr size_t ALLOC_SIZE = 512;
67     static constexpr size_t ALLOC_COUNT = 500000;
68     TLAB *tlab = CreateNewTLAB();
69     ASSERT_TRUE(tlab != nullptr);
70     // All accesses has been created according implementation as we want to create in JIT.
71     bool overflow = false;
72     auto free_pointer_addr = static_cast<uintptr_t *>(ToVoidPtr(ToUintPtr(tlab) + TLAB::TLABFreePointerOffset()));
73     auto end_addr = static_cast<uintptr_t *>(ToVoidPtr(ToUintPtr(tlab) + TLAB::TLABEndAddrOffset()));
74     for (size_t i = 1; i < ALLOC_COUNT; i++) {
75         uintptr_t old_free_pointer = (*free_pointer_addr);
76         // NOTE: All objects, allocated in Runtime, must have the DEFAULT_ALIGNMENT alignment.
77         void *mem = tlab->Alloc(AlignUp(ALLOC_SIZE, DEFAULT_ALIGNMENT_IN_BYTES));
78         if (mem != nullptr) {
79             ASSERT_TRUE(ToUintPtr(mem) == old_free_pointer);
80         } else {
81             ASSERT_TRUE(*end_addr < (old_free_pointer + ALLOC_SIZE));
82             overflow = true;
83         }
84     }
85     ASSERT_EQ(overflow, true) << "Increase the size of alloc_count to get overflow";
86 }
87 
TEST_F(TLABTest,AlignedAlloc)88 TEST_F(TLABTest, AlignedAlloc)
89 {
90     constexpr size_t ARRAY_SIZE = 1024;
91     TLAB *tlab = CreateNewTLAB();
92     Alignment align = DEFAULT_ALIGNMENT;
93     std::array<int *, ARRAY_SIZE> arr;
94 
95     size_t mask = GetAlignmentInBytes(align) - 1;
96 
97     // Allocations
98     srand(seed_);
99     for (size_t i = 0; i < ARRAY_SIZE; ++i) {
100         arr[i] = static_cast<int *>(tlab->Alloc(sizeof(int)));
101         *arr[i] = rand() % std::numeric_limits<int>::max();
102     }
103 
104     // Allocations checking
105     srand(seed_);
106     for (size_t i = 0; i < ARRAY_SIZE; ++i) {
107         ASSERT_NE(arr[i], nullptr) << "value of i: " << i << ", align: " << align;
108         ASSERT_EQ(reinterpret_cast<size_t>(arr[i]) & mask, static_cast<size_t>(0))
109             << "value of i: " << i << ", align: " << align;
110         ASSERT_EQ(*arr[i], rand() % std::numeric_limits<int>::max()) << "value of i: " << i << ", align: " << align;
111     }
112 
113     void *ptr = tlab->Alloc(TLAB_TEST_SIZE);
114     ASSERT_EQ(ptr, nullptr) << "Here Alloc with allocation size = " << TLAB_TEST_SIZE << " bytes should return nullptr";
115 }
116 
117 }  // namespace panda::mem
118