1 | /* $Id: tstRTR0MemUserKernel.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT R0 Testcase - Thread Preemption.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/mem.h>
|
---|
42 |
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/param.h>
|
---|
45 | #include <iprt/time.h>
|
---|
46 | #include <iprt/string.h>
|
---|
47 | #include <VBox/sup.h>
|
---|
48 | #include "tstRTR0MemUserKernel.h"
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Service request callback function.
|
---|
54 | *
|
---|
55 | * @returns VBox status code.
|
---|
56 | * @param pSession The caller's session.
|
---|
57 | * @param u64Arg 64-bit integer argument.
|
---|
58 | * @param pReqHdr The request header. Input / Output. Optional.
|
---|
59 | */
|
---|
60 | DECLEXPORT(int) TSTRTR0MemUserKernelSrvReqHandler(PSUPDRVSESSION pSession, uint32_t uOperation,
|
---|
61 | uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
|
---|
62 | {
|
---|
63 | NOREF(pSession);
|
---|
64 | if (!RT_VALID_PTR(pReqHdr))
|
---|
65 | return VERR_INVALID_PARAMETER;
|
---|
66 | char *pszErr = (char *)(pReqHdr + 1);
|
---|
67 | size_t cchErr = pReqHdr->cbReq - sizeof(*pReqHdr);
|
---|
68 | if (cchErr < 32 || cchErr >= 0x10000)
|
---|
69 | return VERR_INVALID_PARAMETER;
|
---|
70 | *pszErr = '\0';
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * R3Ptr is valid and good for up to a page. The page before
|
---|
74 | * and after are both invalid. Or, it's a kernel page.
|
---|
75 | */
|
---|
76 | RTR3PTR R3Ptr = (RTR3PTR)u64Arg;
|
---|
77 | if (R3Ptr != u64Arg)
|
---|
78 | return VERR_INVALID_PARAMETER;
|
---|
79 |
|
---|
80 | /*
|
---|
81 | * Allocate a kernel buffer.
|
---|
82 | */
|
---|
83 | uint8_t *pbKrnlBuf = (uint8_t *)RTMemAlloc(PAGE_SIZE * 2);
|
---|
84 | if (!pbKrnlBuf)
|
---|
85 | {
|
---|
86 | RTStrPrintf(pszErr, cchErr, "!no memory for kernel buffers");
|
---|
87 | return VINF_SUCCESS;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /*
|
---|
91 | * The big switch.
|
---|
92 | */
|
---|
93 | switch (uOperation)
|
---|
94 | {
|
---|
95 | case TSTRTR0MEMUSERKERNEL_SANITY_OK:
|
---|
96 | break;
|
---|
97 |
|
---|
98 | case TSTRTR0MEMUSERKERNEL_SANITY_FAILURE:
|
---|
99 | RTStrPrintf(pszErr, cchErr, "!42failure42%1024s", "");
|
---|
100 | break;
|
---|
101 |
|
---|
102 | case TSTRTR0MEMUSERKERNEL_BASIC:
|
---|
103 | {
|
---|
104 | int rc = RTR0MemUserCopyFrom(pbKrnlBuf, R3Ptr, PAGE_SIZE);
|
---|
105 | if (rc == VINF_SUCCESS)
|
---|
106 | {
|
---|
107 | rc = RTR0MemUserCopyTo(R3Ptr, pbKrnlBuf, PAGE_SIZE);
|
---|
108 | if (rc == VINF_SUCCESS)
|
---|
109 | {
|
---|
110 | if (RTR0MemUserIsValidAddr(R3Ptr))
|
---|
111 | {
|
---|
112 | if (RTR0MemKernelIsValidAddr(pbKrnlBuf))
|
---|
113 | {
|
---|
114 | if (RTR0MemAreKrnlAndUsrDifferent())
|
---|
115 | {
|
---|
116 | RTStrPrintf(pszErr, cchErr, "RTR0MemAreKrnlAndUsrDifferent returns true");
|
---|
117 | if (!RTR0MemUserIsValidAddr((uintptr_t)pbKrnlBuf))
|
---|
118 | {
|
---|
119 | if (!RTR0MemKernelIsValidAddr((void *)R3Ptr))
|
---|
120 | {
|
---|
121 | /* done */
|
---|
122 | }
|
---|
123 | else
|
---|
124 | RTStrPrintf(pszErr, cchErr, "! #5 - RTR0MemKernelIsValidAddr -> true, expected false");
|
---|
125 | }
|
---|
126 | else
|
---|
127 | RTStrPrintf(pszErr, cchErr, "! #5 - RTR0MemUserIsValidAddr -> true, expected false");
|
---|
128 | }
|
---|
129 | else
|
---|
130 | RTStrPrintf(pszErr, cchErr, "RTR0MemAreKrnlAndUsrDifferent returns false");
|
---|
131 | }
|
---|
132 | else
|
---|
133 | RTStrPrintf(pszErr, cchErr, "! #4 - RTR0MemKernelIsValidAddr -> false, expected true");
|
---|
134 | }
|
---|
135 | else
|
---|
136 | RTStrPrintf(pszErr, cchErr, "! #3 - RTR0MemUserIsValidAddr -> false, expected true");
|
---|
137 | }
|
---|
138 | else
|
---|
139 | RTStrPrintf(pszErr, cchErr, "! #2 - RTR0MemUserCopyTo -> %Rrc expected %Rrc", rc, VINF_SUCCESS);
|
---|
140 | }
|
---|
141 | else
|
---|
142 | RTStrPrintf(pszErr, cchErr, "! #1 - RTR0MemUserCopyFrom -> %Rrc expected %Rrc", rc, VINF_SUCCESS);
|
---|
143 | break;
|
---|
144 | }
|
---|
145 |
|
---|
146 | #define TEST_OFF_SIZE(off, size, rcExpect) \
|
---|
147 | if (1) \
|
---|
148 | { \
|
---|
149 | int rc = RTR0MemUserCopyFrom(pbKrnlBuf, R3Ptr + (off), (size)); \
|
---|
150 | if (rc != (rcExpect)) \
|
---|
151 | { \
|
---|
152 | RTStrPrintf(pszErr, cchErr, "!RTR0MemUserCopyFrom(, +%#x, %#x) -> %Rrc, expected %Rrc", \
|
---|
153 | (off), (size), rc, (rcExpect)); \
|
---|
154 | break; \
|
---|
155 | } \
|
---|
156 | rc = RTR0MemUserCopyTo(R3Ptr + (off), pbKrnlBuf, (size)); \
|
---|
157 | if (rc != (rcExpect)) \
|
---|
158 | { \
|
---|
159 | RTStrPrintf(pszErr, cchErr, "!RTR0MemUserCopyTo(+%#x,, %#x) -> %Rrc, expected %Rrc", \
|
---|
160 | (off), (size), rc, (rcExpect)); \
|
---|
161 | break; \
|
---|
162 | } \
|
---|
163 | } else do {} while (0)
|
---|
164 |
|
---|
165 | case TSTRTR0MEMUSERKERNEL_GOOD:
|
---|
166 | {
|
---|
167 | for (unsigned off = 0; off < 16 && !*pszErr; off++)
|
---|
168 | for (unsigned cb = 0; cb < PAGE_SIZE - 16; cb++)
|
---|
169 | TEST_OFF_SIZE(off, cb, VINF_SUCCESS);
|
---|
170 | break;
|
---|
171 | }
|
---|
172 |
|
---|
173 | case TSTRTR0MEMUSERKERNEL_BAD:
|
---|
174 | {
|
---|
175 | for (unsigned off = 0; off < 16 && !*pszErr; off++)
|
---|
176 | for (unsigned cb = 0; cb < PAGE_SIZE - 16; cb++)
|
---|
177 | TEST_OFF_SIZE(off, cb, cb > 0 ? VERR_ACCESS_DENIED : VINF_SUCCESS);
|
---|
178 | break;
|
---|
179 | }
|
---|
180 |
|
---|
181 | case TSTRTR0MEMUSERKERNEL_INVALID_ADDRESS:
|
---|
182 | {
|
---|
183 | if ( !RTR0MemUserIsValidAddr(R3Ptr)
|
---|
184 | && RTR0MemKernelIsValidAddr((void *)R3Ptr))
|
---|
185 | {
|
---|
186 | for (unsigned off = 0; off < 16 && !*pszErr; off++)
|
---|
187 | for (unsigned cb = 0; cb < PAGE_SIZE - 16; cb++)
|
---|
188 | TEST_OFF_SIZE(off, cb, cb > 0 ? VERR_ACCESS_DENIED : VINF_SUCCESS); /* ... */
|
---|
189 | }
|
---|
190 | else
|
---|
191 | RTStrPrintf(pszErr, cchErr, "RTR0MemUserIsValidAddr returns true");
|
---|
192 | break;
|
---|
193 | }
|
---|
194 |
|
---|
195 | default:
|
---|
196 | RTStrPrintf(pszErr, cchErr, "!Unknown test #%d", uOperation);
|
---|
197 | break;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /* The error indicator is the '!' in the message buffer. */
|
---|
201 | RTMemFree(pbKrnlBuf);
|
---|
202 | return VINF_SUCCESS;
|
---|
203 | }
|
---|
204 |
|
---|