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 "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 ark::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 #ifdef PANDA_NIGHTLY_TEST_ON
32 seed_ = time(NULL);
33 #else
34 // NOLINTNEXTLINE(readability-magic-numbers)
35 seed_ = 0x0BADDEAD;
36 #endif
37 srand(seed_);
38 }
39
~TLABTest()40 ~TLABTest() override
41 {
42 for (auto i : allocatedMemMmap_) {
43 ark::os::mem::UnmapRaw(std::get<0>(i), std::get<1>(i));
44 }
45 }
46
47 NO_COPY_SEMANTIC(TLABTest);
48 NO_MOVE_SEMANTIC(TLABTest);
49
50 protected:
CreateNewTLAB()51 TLAB *CreateNewTLAB()
52 {
53 void *mem = ark::os::mem::MapRWAnonymousRaw(TLAB_TEST_SIZE);
54 ASAN_UNPOISON_MEMORY_REGION(mem, TLAB_TEST_SIZE);
55 std::pair<void *, size_t> newPair {mem, TLAB_TEST_SIZE};
56 allocatedMemMmap_.push_back(newPair);
57 auto tlabBuffOffs = AlignUp(ToUintPtr(mem) + sizeof(mem::TLAB), DEFAULT_ALIGNMENT_IN_BYTES) - ToUintPtr(mem);
58 auto newTlab = new (mem) TLAB(ToVoidPtr(ToUintPtr(mem) + tlabBuffOffs), TLAB_TEST_SIZE - tlabBuffOffs);
59 return newTlab;
60 }
61
62 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
63 unsigned seed_ {};
64
65 private:
66 std::vector<std::pair<void *, size_t>> allocatedMemMmap_;
67 };
68
TEST_F(TLABTest,AccessTest)69 TEST_F(TLABTest, AccessTest)
70 {
71 static constexpr size_t ALLOC_SIZE = 512;
72 static constexpr size_t ALLOC_COUNT = 500000;
73 TLAB *tlab = CreateNewTLAB();
74 ASSERT_TRUE(tlab != nullptr);
75 // All accesses has been created according implementation as we want to create in JIT.
76 bool overflow = false;
77 auto freePointerAddr = static_cast<uintptr_t *>(ToVoidPtr(ToUintPtr(tlab) + TLAB::TLABFreePointerOffset()));
78 auto endAddr = static_cast<uintptr_t *>(ToVoidPtr(ToUintPtr(tlab) + TLAB::TLABEndAddrOffset()));
79 for (size_t i = 1; i < ALLOC_COUNT; i++) {
80 uintptr_t oldFreePointer = (*freePointerAddr);
81 // NOTE: All objects, allocated in Runtime, must have the DEFAULT_ALIGNMENT alignment.
82 void *mem = tlab->Alloc(AlignUp(ALLOC_SIZE, DEFAULT_ALIGNMENT_IN_BYTES));
83 if (mem != nullptr) {
84 ASSERT_TRUE(ToUintPtr(mem) == oldFreePointer);
85 } else {
86 ASSERT_TRUE(*endAddr < (oldFreePointer + ALLOC_SIZE));
87 overflow = true;
88 }
89 }
90 ASSERT_EQ(overflow, true) << "Increase the size of alloc_count to get overflow";
91 }
92
TEST_F(TLABTest,AlignedAlloc)93 TEST_F(TLABTest, AlignedAlloc)
94 {
95 constexpr size_t ARRAY_SIZE = 1024;
96 TLAB *tlab = CreateNewTLAB();
97 Alignment align = DEFAULT_ALIGNMENT;
98 std::array<int *, ARRAY_SIZE> arr {};
99
100 size_t mask = GetAlignmentInBytes(align) - 1;
101
102 // Allocations
103 srand(seed_);
104 for (size_t i = 0; i < ARRAY_SIZE; ++i) {
105 arr[i] = static_cast<int *>(tlab->Alloc(sizeof(int)));
106 // NOLINTNEXTLINE(cert-msc50-cpp)
107 *arr[i] = rand() % std::numeric_limits<int>::max();
108 }
109
110 // Allocations checking
111 srand(seed_);
112 for (size_t i = 0; i < ARRAY_SIZE; ++i) {
113 ASSERT_NE(arr[i], nullptr) << "value of i: " << i << ", align: " << align;
114 ASSERT_EQ(reinterpret_cast<size_t>(arr[i]) & mask, static_cast<size_t>(0))
115 << "value of i: " << i << ", align: " << align;
116 // NOLINTNEXTLINE(cert-msc50-cpp)
117 ASSERT_EQ(*arr[i], rand() % std::numeric_limits<int>::max()) << "value of i: " << i << ", align: " << align;
118 }
119
120 void *ptr = tlab->Alloc(TLAB_TEST_SIZE);
121 ASSERT_EQ(ptr, nullptr) << "Here Alloc with allocation size = " << TLAB_TEST_SIZE << " bytes should return nullptr";
122 }
123
124 } // namespace ark::mem
125