ashmem_test.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright (C) 2016 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. #include <errno.h>
  17. #include <linux/fs.h>
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <sys/mman.h>
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <android-base/macros.h>
  24. #include <android-base/unique_fd.h>
  25. #include <cutils/ashmem.h>
  26. #include <gtest/gtest.h>
  27. using android::base::unique_fd;
  28. void TestCreateRegion(size_t size, unique_fd &fd, int prot) {
  29. fd = unique_fd(ashmem_create_region(nullptr, size));
  30. ASSERT_TRUE(fd >= 0);
  31. ASSERT_TRUE(ashmem_valid(fd));
  32. ASSERT_EQ(size, static_cast<size_t>(ashmem_get_size_region(fd)));
  33. ASSERT_EQ(0, ashmem_set_prot_region(fd, prot));
  34. }
  35. void TestMmap(const unique_fd& fd, size_t size, int prot, void** region, off_t off = 0) {
  36. ASSERT_TRUE(fd >= 0);
  37. ASSERT_TRUE(ashmem_valid(fd));
  38. *region = mmap(nullptr, size, prot, MAP_SHARED, fd, off);
  39. ASSERT_NE(MAP_FAILED, *region);
  40. }
  41. void TestProtDenied(const unique_fd &fd, size_t size, int prot) {
  42. ASSERT_TRUE(fd >= 0);
  43. ASSERT_TRUE(ashmem_valid(fd));
  44. EXPECT_EQ(MAP_FAILED, mmap(nullptr, size, prot, MAP_SHARED, fd, 0));
  45. }
  46. void TestProtIs(const unique_fd& fd, int prot) {
  47. ASSERT_TRUE(fd >= 0);
  48. ASSERT_TRUE(ashmem_valid(fd));
  49. EXPECT_EQ(prot, ioctl(fd, ASHMEM_GET_PROT_MASK));
  50. }
  51. void FillData(uint8_t* data, size_t dataLen) {
  52. for (size_t i = 0; i < dataLen; i++) {
  53. data[i] = i & 0xFF;
  54. }
  55. }
  56. TEST(AshmemTest, BasicTest) {
  57. constexpr size_t size = PAGE_SIZE;
  58. uint8_t data[size];
  59. FillData(data, size);
  60. unique_fd fd;
  61. ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
  62. void *region1;
  63. ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ | PROT_WRITE, &region1));
  64. memcpy(region1, &data, size);
  65. ASSERT_EQ(0, memcmp(region1, &data, size));
  66. EXPECT_EQ(0, munmap(region1, size));
  67. void *region2;
  68. ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ, &region2));
  69. ASSERT_EQ(0, memcmp(region2, &data, size));
  70. EXPECT_EQ(0, munmap(region2, size));
  71. }
  72. TEST(AshmemTest, ForkTest) {
  73. constexpr size_t size = PAGE_SIZE;
  74. uint8_t data[size];
  75. FillData(data, size);
  76. unique_fd fd;
  77. ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
  78. void *region1;
  79. ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ | PROT_WRITE, &region1));
  80. memcpy(region1, &data, size);
  81. ASSERT_EQ(0, memcmp(region1, &data, size));
  82. EXPECT_EQ(0, munmap(region1, size));
  83. ASSERT_EXIT(
  84. {
  85. if (!ashmem_valid(fd)) {
  86. _exit(3);
  87. }
  88. void* region2 = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  89. if (region2 == MAP_FAILED) {
  90. _exit(1);
  91. }
  92. if (memcmp(region2, &data, size) != 0) {
  93. _exit(2);
  94. }
  95. memset(region2, 0, size);
  96. munmap(region2, size);
  97. _exit(0);
  98. },
  99. ::testing::ExitedWithCode(0), "");
  100. memset(&data, 0, size);
  101. void *region2;
  102. ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ | PROT_WRITE, &region2));
  103. ASSERT_EQ(0, memcmp(region2, &data, size));
  104. EXPECT_EQ(0, munmap(region2, size));
  105. }
  106. TEST(AshmemTest, FileOperationsTest) {
  107. unique_fd fd;
  108. void* region;
  109. // Allocate a 4-page buffer, but leave page-sized holes on either side
  110. constexpr size_t size = PAGE_SIZE * 4;
  111. constexpr size_t dataSize = PAGE_SIZE * 2;
  112. constexpr size_t holeSize = PAGE_SIZE;
  113. ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
  114. ASSERT_NO_FATAL_FAILURE(TestMmap(fd, dataSize, PROT_READ | PROT_WRITE, &region, holeSize));
  115. uint8_t data[dataSize];
  116. FillData(data, dataSize);
  117. memcpy(region, data, dataSize);
  118. constexpr off_t dataStart = holeSize;
  119. constexpr off_t dataEnd = dataStart + dataSize;
  120. // The sequence of seeks below looks something like this:
  121. //
  122. // [ ][data][data][ ]
  123. // --^ lseek(99, SEEK_SET)
  124. // ------^ lseek(dataStart, SEEK_CUR)
  125. // ------^ lseek(0, SEEK_DATA)
  126. // ------------^ lseek(dataStart, SEEK_HOLE)
  127. // ^-- lseek(-99, SEEK_END)
  128. // ^------ lseek(-dataStart, SEEK_CUR)
  129. const struct {
  130. // lseek() parameters
  131. off_t offset;
  132. int whence;
  133. // Expected lseek() return value
  134. off_t ret;
  135. } seeks[] = {
  136. {99, SEEK_SET, 99}, {dataStart, SEEK_CUR, dataStart + 99},
  137. {0, SEEK_DATA, dataStart}, {dataStart, SEEK_HOLE, dataEnd},
  138. {-99, SEEK_END, size - 99}, {-dataStart, SEEK_CUR, dataEnd - 99},
  139. };
  140. for (const auto& cfg : seeks) {
  141. errno = 0;
  142. ASSERT_TRUE(ashmem_valid(fd));
  143. auto off = lseek(fd, cfg.offset, cfg.whence);
  144. ASSERT_EQ(cfg.ret, off) << "lseek(" << cfg.offset << ", " << cfg.whence << ") failed"
  145. << (errno ? ": " : "") << (errno ? strerror(errno) : "");
  146. if (off >= dataStart && off < dataEnd) {
  147. off_t dataOff = off - dataStart;
  148. ssize_t readSize = dataSize - dataOff;
  149. uint8_t buf[readSize];
  150. ASSERT_EQ(readSize, TEMP_FAILURE_RETRY(read(fd, buf, readSize)));
  151. EXPECT_EQ(0, memcmp(buf, data + dataOff, readSize));
  152. }
  153. }
  154. EXPECT_EQ(0, munmap(region, dataSize));
  155. }
  156. TEST(AshmemTest, ProtTest) {
  157. unique_fd fd;
  158. constexpr size_t size = PAGE_SIZE;
  159. void *region;
  160. ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ));
  161. TestProtDenied(fd, size, PROT_WRITE);
  162. TestProtIs(fd, PROT_READ);
  163. ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_READ, &region));
  164. EXPECT_EQ(0, munmap(region, size));
  165. ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_WRITE));
  166. TestProtDenied(fd, size, PROT_READ);
  167. TestProtIs(fd, PROT_WRITE);
  168. ASSERT_NO_FATAL_FAILURE(TestMmap(fd, size, PROT_WRITE, &region));
  169. EXPECT_EQ(0, munmap(region, size));
  170. ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
  171. TestProtIs(fd, PROT_READ | PROT_WRITE);
  172. ASSERT_EQ(0, ashmem_set_prot_region(fd, PROT_READ));
  173. errno = 0;
  174. ASSERT_EQ(-1, ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE))
  175. << "kernel shouldn't allow adding protection bits";
  176. EXPECT_EQ(EINVAL, errno);
  177. TestProtIs(fd, PROT_READ);
  178. TestProtDenied(fd, size, PROT_WRITE);
  179. }
  180. TEST(AshmemTest, ForkProtTest) {
  181. unique_fd fd;
  182. constexpr size_t size = PAGE_SIZE;
  183. int protFlags[] = { PROT_READ, PROT_WRITE };
  184. for (size_t i = 0; i < arraysize(protFlags); i++) {
  185. ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd, PROT_READ | PROT_WRITE));
  186. ASSERT_EXIT(
  187. {
  188. if (!ashmem_valid(fd)) {
  189. _exit(3);
  190. } else if (ashmem_set_prot_region(fd, protFlags[i]) >= 0) {
  191. _exit(0);
  192. } else {
  193. _exit(1);
  194. }
  195. },
  196. ::testing::ExitedWithCode(0), "");
  197. ASSERT_NO_FATAL_FAILURE(TestProtDenied(fd, size, protFlags[1-i]));
  198. }
  199. }
  200. TEST(AshmemTest, ForkMultiRegionTest) {
  201. constexpr size_t size = PAGE_SIZE;
  202. uint8_t data[size];
  203. FillData(data, size);
  204. constexpr int nRegions = 16;
  205. unique_fd fd[nRegions];
  206. for (int i = 0; i < nRegions; i++) {
  207. ASSERT_NO_FATAL_FAILURE(TestCreateRegion(size, fd[i], PROT_READ | PROT_WRITE));
  208. void *region;
  209. ASSERT_NO_FATAL_FAILURE(TestMmap(fd[i], size, PROT_READ | PROT_WRITE, &region));
  210. memcpy(region, &data, size);
  211. ASSERT_EQ(0, memcmp(region, &data, size));
  212. EXPECT_EQ(0, munmap(region, size));
  213. }
  214. ASSERT_EXIT({
  215. for (int i = 0; i < nRegions; i++) {
  216. if (!ashmem_valid(fd[i])) {
  217. _exit(3);
  218. }
  219. void *region = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd[i], 0);
  220. if (region == MAP_FAILED) {
  221. _exit(1);
  222. }
  223. if (memcmp(region, &data, size) != 0) {
  224. munmap(region, size);
  225. _exit(2);
  226. }
  227. memset(region, 0, size);
  228. munmap(region, size);
  229. }
  230. _exit(0);
  231. }, ::testing::ExitedWithCode(0), "");
  232. memset(&data, 0, size);
  233. for (int i = 0; i < nRegions; i++) {
  234. void *region;
  235. ASSERT_NO_FATAL_FAILURE(TestMmap(fd[i], size, PROT_READ | PROT_WRITE, &region));
  236. ASSERT_EQ(0, memcmp(region, &data, size));
  237. EXPECT_EQ(0, munmap(region, size));
  238. }
  239. }