1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "gtest/gtest.h"
18
19 #include "berberis/base/memfd_backed_mmap.h"
20 #include "berberis/base/mmap.h"
21
22 namespace berberis {
23
24 namespace {
25
TEST(MemfdBackedMap,type_uintptr_t)26 TEST(MemfdBackedMap, type_uintptr_t) {
27 #if defined(__LP64__)
28 constexpr size_t kTableSize = 1 << 26; // 64M mapping size
29 constexpr size_t kMemfdFileSize = 1 << 24; // 16M file size
30 #else
31 constexpr size_t kTableSize = 1 << 14; // 16k mapping size
32 constexpr size_t kMemfdFileSize = 1 << 12; // 4k file size
33 #endif
34
35 uintptr_t default_value = 42;
36 int memfd = CreateAndFillMemfd("uintptr", kMemfdFileSize, default_value);
37 uintptr_t* ptr = reinterpret_cast<uintptr_t*>(
38 CreateMemfdBackedMapOrDie(memfd, kTableSize * sizeof(default_value), kMemfdFileSize));
39 close(memfd);
40
41 ASSERT_EQ(ptr[3], default_value);
42
43 ptr[3] = 0;
44
45 ASSERT_EQ(ptr[3], 0U);
46 ASSERT_EQ(ptr[73], default_value);
47 ASSERT_EQ(ptr[kMemfdFileSize + 3], default_value);
48 ASSERT_EQ(ptr[kMemfdFileSize + 73], default_value);
49 ASSERT_EQ(ptr[3 * kMemfdFileSize + 3], default_value);
50
51 MunmapOrDie(ptr, kTableSize * sizeof(default_value));
52 }
53
54 } // namespace
55
56 } // namespace berberis
57