VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTR0MemUserKernel.cpp@ 78338

Last change on this file since 78338 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette