• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include "it_test_vm.h"
32 
33 #define MAP_RESERVED0080 0x0080
34 
CheckedMmap(int prot,int flags,int fd)35 static int CheckedMmap(int prot, int flags, int fd)
36 {
37     void *p = NULL;
38     int pageSize = getpagesize();
39     int ret;
40 
41     if (pageSize < 0) {
42         printf("err: mmap size invalid\n");
43         return -1;
44     }
45     p = mmap(NULL, pageSize, prot, flags, fd, 0);
46     if (p == MAP_FAILED) {
47         return errno;
48     } else {
49         ret = munmap(p, pageSize);
50         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
51         return 0;
52     }
53 }
54 
Testcase(void)55 static int Testcase(void)
56 {
57     int shmfd;
58     int ret;
59     int count;
60     int pageSize = getpagesize();
61     char *buf = NULL;
62 
63     shmfd = shm_open("/test", O_RDWR | O_CREAT, 0644);
64     ICUNIT_ASSERT_NOT_EQUAL(shmfd, -1, shmfd);
65 
66     if (pageSize <= 0) {
67         printf("err: malloc size invalid\n");
68         return -1;
69     }
70     if (pageSize <= 0) {
71         printf("err: malloc size invalid\n");
72         return -1;
73     }
74     buf = static_cast<char *>(malloc(pageSize));
75     ICUNIT_ASSERT_NOT_EQUAL(buf, NULL, buf);
76     (void)memset_s(buf, pageSize, 0xf, pageSize);
77 
78     count = write(shmfd, buf, pageSize);
79     if (count != pageSize) {
80         free(buf);
81         ICUNIT_ASSERT_EQUAL(count, pageSize, count);
82     }
83     free(buf);
84 
85     /* Simple MAP_ANONYMOUS */
86     ret = CheckedMmap(PROT_READ | PROT_WRITE, MAP_ANONYMOUS, -1);
87     ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
88 
89     /* Simple shm fd shared */
90     ret = CheckedMmap(PROT_READ | PROT_WRITE, MAP_SHARED, shmfd);
91     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
92 
93     /* Simple shm fd private */
94     ret = CheckedMmap(PROT_READ | PROT_WRITE, MAP_PRIVATE, shmfd);
95     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
96 
97     /* MAP_ANONYMOUS with extra PROT flags */
98     ret = CheckedMmap(PROT_READ | PROT_WRITE | 0x100000, MAP_ANONYMOUS, -1);
99     ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
100 
101     /* Shm fd with garbage PROT */
102     ret = CheckedMmap(0xffff, MAP_SHARED, shmfd);
103     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
104 
105     /* Undefined flag. */
106     ret = CheckedMmap(PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_RESERVED0080, -1);
107     ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
108 
109     /* Both MAP_SHARED and MAP_PRIVATE */
110     ret = CheckedMmap(PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_SHARED, -1);
111     ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
112 
113     /* Shm fd with both SHARED and PRIVATE */
114     ret = CheckedMmap(PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_SHARED, shmfd);
115     ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
116 
117     /* At least one of MAP_SHARED or MAP_PRIVATE without ANON */
118     ret = CheckedMmap(PROT_READ | PROT_WRITE, 0, shmfd);
119     ICUNIT_ASSERT_EQUAL(ret, EINVAL, ret);
120 
121     /* MAP_ANONYMOUS with either sharing flag (impacts fork). */
122     ret = CheckedMmap(PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1);
123     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
124 
125     ret = CheckedMmap(PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1);
126     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
127 
128     ret = CheckedMmap(PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0);
129     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
130 
131     ret = close(shmfd);
132     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
133     return 0;
134 }
135 
ItTestMmap002(void)136 void ItTestMmap002(void)
137 {
138     TEST_ADD_CASE("IT_MEM_MMAP_002", Testcase, TEST_LOS, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
139 }
140