1 /*
2 * Copyright (C) 2024 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/mmap.h"
20
21 #include <sys/mman.h>
22
23 #include <cstdint>
24
25 namespace berberis {
26
27 namespace {
28
29 #if defined(__LP64__)
TEST(MmapTest,kMmapImpl_MmapBerberis32Bit)30 TEST(MmapTest, kMmapImpl_MmapBerberis32Bit) {
31 constexpr size_t k8Mb = 0x1 << 23;
32 for (size_t i = 0; i < 100; i++) {
33 void* result = MmapImpl({.size = k8Mb,
34 .prot = PROT_READ | PROT_WRITE,
35 .flags = MAP_PRIVATE | MAP_ANONYMOUS,
36 .berberis_flags = kMmapBerberis32Bit});
37 ASSERT_NE(result, MAP_FAILED);
38 *reinterpret_cast<uint64_t*>(result) = 42;
39 ASSERT_EQ(*reinterpret_cast<uint64_t*>(result), 42UL);
40 }
41 }
42
TEST(MmapTest,MmapImpl_kMmapBerberis32Bit_FailsFor4G)43 TEST(MmapTest, MmapImpl_kMmapBerberis32Bit_FailsFor4G) {
44 constexpr size_t k4Gb = 0x1L << 32;
45 void* result = MmapImpl({.size = k4Gb,
46 .prot = PROT_READ | PROT_WRITE,
47 .flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
48 .berberis_flags = kMmapBerberis32Bit});
49 ASSERT_EQ(result, MAP_FAILED);
50 }
51
TEST(MmapTest,MmapImpl_kMmapBerberis32Bit_FailsFor0)52 TEST(MmapTest, MmapImpl_kMmapBerberis32Bit_FailsFor0) {
53 void* result = MmapImpl({.size = 0,
54 .prot = PROT_READ | PROT_WRITE,
55 .flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE,
56 .berberis_flags = kMmapBerberis32Bit});
57 ASSERT_EQ(result, MAP_FAILED);
58 }
59 #endif
60
61 } // namespace
62
63 } // namespace berberis
64