blob: cf2c9d028c3da46a0ed2a1ae78cfed41890b69d4 [file] [log] [blame]
Brian Carlstrom9004cb62013-07-26 15:48:31 -07001/*
2 * Copyright (C) 2013 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 "mem_map.h"
18
19#include "UniquePtr.h"
20#include "gtest/gtest.h"
21
22namespace art {
23
Hiroshi Yamauchifd7e7f12013-10-22 14:17:48 -070024class MemMapTest : public testing::Test {
25 public:
26 byte* BaseBegin(MemMap* mem_map) {
27 return reinterpret_cast<byte*>(mem_map->base_begin_);
28 }
29 size_t BaseSize(MemMap* mem_map) {
30 return mem_map->base_size_;
31 }
32};
Brian Carlstrom9004cb62013-07-26 15:48:31 -070033
34TEST_F(MemMapTest, MapAnonymousEmpty) {
Ian Rogers8d31bbd2013-10-13 10:44:14 -070035 std::string error_msg;
Brian Carlstrom9004cb62013-07-26 15:48:31 -070036 UniquePtr<MemMap> map(MemMap::MapAnonymous("MapAnonymousEmpty",
37 NULL,
38 0,
Ian Rogers8d31bbd2013-10-13 10:44:14 -070039 PROT_READ,
40 &error_msg));
41 ASSERT_TRUE(map.get() != NULL) << error_msg;
42 ASSERT_TRUE(error_msg.empty());
Brian Carlstrom9004cb62013-07-26 15:48:31 -070043}
44
Hiroshi Yamauchifd7e7f12013-10-22 14:17:48 -070045TEST_F(MemMapTest, RemapAtEnd) {
46 std::string error_msg;
47 // Cast the page size to size_t.
48 const size_t page_size = static_cast<size_t>(kPageSize);
49 // Map a two-page memory region.
50 MemMap* m0 = MemMap::MapAnonymous("MemMapTest_RemapAtEndTest_map0",
51 NULL,
52 2 * page_size,
53 PROT_READ | PROT_WRITE,
54 &error_msg);
55 // Check its state and write to it.
56 byte* base0 = m0->Begin();
57 ASSERT_TRUE(base0 != NULL) << error_msg;
58 size_t size0 = m0->Size();
59 EXPECT_EQ(m0->Size(), 2 * page_size);
60 EXPECT_EQ(BaseBegin(m0), base0);
61 EXPECT_EQ(BaseSize(m0), size0);
62 memset(base0, 42, 2 * page_size);
63 // Remap the latter half into a second MemMap.
64 MemMap* m1 = m0->RemapAtEnd(base0 + page_size,
65 "MemMapTest_RemapAtEndTest_map1",
66 PROT_READ | PROT_WRITE,
67 &error_msg);
68 // Check the states of the two maps.
69 EXPECT_EQ(m0->Begin(), base0) << error_msg;
70 EXPECT_EQ(m0->Size(), page_size);
71 EXPECT_EQ(BaseBegin(m0), base0);
72 EXPECT_EQ(BaseSize(m0), page_size);
73 byte* base1 = m1->Begin();
74 size_t size1 = m1->Size();
75 EXPECT_EQ(base1, base0 + page_size);
76 EXPECT_EQ(size1, page_size);
77 EXPECT_EQ(BaseBegin(m1), base1);
78 EXPECT_EQ(BaseSize(m1), size1);
79 // Write to the second region.
80 memset(base1, 43, page_size);
81 // Check the contents of the two regions.
82 for (size_t i = 0; i < page_size; ++i) {
83 EXPECT_EQ(base0[i], 42);
84 }
85 for (size_t i = 0; i < page_size; ++i) {
86 EXPECT_EQ(base1[i], 43);
87 }
88 // Unmap the first region.
89 delete m0;
90 // Make sure the second region is still accessible after the first
91 // region is unmapped.
92 for (size_t i = 0; i < page_size; ++i) {
93 EXPECT_EQ(base1[i], 43);
94 }
95 delete m1;
96}
97
Brian Carlstrom9004cb62013-07-26 15:48:31 -070098} // namespace art