1 /*
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 <sys/mman.h>
18 #include <unistd.h>
19 
20 #include <gtest/gtest.h>
21 
22 #include <ion/ion.h>
23 #include "ion_test_fixture.h"
24 
25 class Map : public IonTest {};
26 
27 TEST_F(Map, MapFd) {
28     static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
29     for (const auto& heap : ion_heaps) {
30         for (size_t size : allocationSizes) {
31             SCOPED_TRACE(::testing::Message()
32                          << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
33             SCOPED_TRACE(::testing::Message() << "size " << size);
34             int map_fd = -1;
35 
36             ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), 0, &map_fd));
37             ASSERT_GE(map_fd, 0);
38 
39             void* ptr;
40             ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
41             ASSERT_TRUE(ptr != NULL);
42             ASSERT_EQ(0, close(map_fd));
43 
44             memset(ptr, 0xaa, size);
45 
46             ASSERT_EQ(0, munmap(ptr, size));
47         }
48     }
49 }
50 
51 TEST_F(Map, MapOffset) {
52     for (const auto& heap : ion_heaps) {
53         SCOPED_TRACE(::testing::Message()
54                      << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
55         int map_fd = -1;
56 
57         ASSERT_EQ(0, ion_alloc_fd(ionfd, getpagesize() * 2, 0, (1 << heap.heap_id), 0, &map_fd));
58         ASSERT_GE(map_fd, 0);
59 
60         unsigned char* ptr;
61         ptr = (unsigned char*)mmap(NULL, getpagesize() * 2, PROT_READ | PROT_WRITE, MAP_SHARED,
62                                    map_fd, 0);
63         ASSERT_TRUE(ptr != NULL);
64 
65         memset(ptr, 0, getpagesize());
66         memset(ptr + getpagesize(), 0xaa, getpagesize());
67 
68         ASSERT_EQ(0, munmap(ptr, getpagesize() * 2));
69 
70         ptr = (unsigned char*)mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, map_fd,
71                                    getpagesize());
72         ASSERT_TRUE(ptr != NULL);
73         ASSERT_EQ(ptr[0], 0xaa);
74         ASSERT_EQ(ptr[getpagesize() - 1], 0xaa);
75         ASSERT_EQ(0, munmap(ptr, getpagesize()));
76         ASSERT_EQ(0, close(map_fd));
77     }
78 }
79 
80 TEST_F(Map, MapCached) {
81     static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
82     for (const auto& heap : ion_heaps) {
83         for (size_t size : allocationSizes) {
84             SCOPED_TRACE(::testing::Message()
85                          << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
86             SCOPED_TRACE(::testing::Message() << "size " << size);
87             int map_fd = -1;
88             unsigned int flags = ION_FLAG_CACHED;
89 
90             ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), flags, &map_fd));
91             ASSERT_GE(map_fd, 0);
92 
93             void* ptr;
94             ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
95             ASSERT_TRUE(ptr != NULL);
96             ASSERT_EQ(0, close(map_fd));
97 
98             memset(ptr, 0xaa, size);
99 
100             ASSERT_EQ(0, munmap(ptr, size));
101         }
102     }
103 }
104 
105 TEST_F(Map, MapCachedNeedsSync) {
106     static const size_t allocationSizes[] = {4 * 1024, 64 * 1024, 1024 * 1024, 2 * 1024 * 1024};
107     for (const auto& heap : ion_heaps) {
108         for (size_t size : allocationSizes) {
109             SCOPED_TRACE(::testing::Message()
110                          << "heap:" << heap.name << ":" << heap.type << ":" << heap.heap_id);
111             SCOPED_TRACE(::testing::Message() << "size " << size);
112             int map_fd = -1;
113             unsigned int flags = ION_FLAG_CACHED | ION_FLAG_CACHED_NEEDS_SYNC;
114 
115             ASSERT_EQ(0, ion_alloc_fd(ionfd, size, 0, (1 << heap.heap_id), flags, &map_fd));
116             ASSERT_GE(map_fd, 0);
117 
118             void* ptr;
119             ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
120             ASSERT_TRUE(ptr != NULL);
121             ASSERT_EQ(0, close(map_fd));
122 
123             memset(ptr, 0xaa, size);
124 
125             ASSERT_EQ(0, munmap(ptr, size));
126         }
127     }
128 }
129