VirtualBox

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

Last change on this file since 36363 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.0 KB
Line 
1/* $Id: tstRTR0MemUserKernel.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT R0 Testcase - Thread Preemption.
4 */
5
6/*
7 * Copyright (C) 2009 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* Header Files *
29*******************************************************************************/
30#include <iprt/mem.h>
31
32#include <iprt/err.h>
33#include <iprt/param.h>
34#include <iprt/time.h>
35#include <iprt/string.h>
36#include <VBox/sup.h>
37#include "tstRTR0MemUserKernel.h"
38
39
40
41/**
42 * Service request callback function.
43 *
44 * @returns VBox status code.
45 * @param pSession The caller's session.
46 * @param u64Arg 64-bit integer argument.
47 * @param pReqHdr The request header. Input / Output. Optional.
48 */
49DECLEXPORT(int) TSTRTR0MemUserKernelSrvReqHandler(PSUPDRVSESSION pSession, uint32_t uOperation,
50 uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
51{
52 if (!VALID_PTR(pReqHdr))
53 return VERR_INVALID_PARAMETER;
54 char *pszErr = (char *)(pReqHdr + 1);
55 size_t cchErr = pReqHdr->cbReq - sizeof(*pReqHdr);
56 if (cchErr < 32 || cchErr >= 0x10000)
57 return VERR_INVALID_PARAMETER;
58 *pszErr = '\0';
59
60 /*
61 * R3Ptr is valid and good for up to a page. The page before
62 * and after are both invalid. Or, it's a kernel page.
63 */
64 RTR3PTR R3Ptr = (RTR3PTR)u64Arg;
65 if (R3Ptr != u64Arg)
66 return VERR_INVALID_PARAMETER;
67
68 /*
69 * Allocate a kernel buffer.
70 */
71 uint8_t *pbKrnlBuf = (uint8_t *)RTMemAlloc(PAGE_SIZE * 2);
72 if (!pbKrnlBuf)
73 {
74 RTStrPrintf(pszErr, cchErr, "!no memory for kernel buffers");
75 return VINF_SUCCESS;
76 }
77
78 /*
79 * The big switch.
80 */
81 switch (uOperation)
82 {
83 case TSTRTR0MEMUSERKERNEL_SANITY_OK:
84 break;
85
86 case TSTRTR0MEMUSERKERNEL_SANITY_FAILURE:
87 RTStrPrintf(pszErr, cchErr, "!42failure42%1024s", "");
88 break;
89
90 case TSTRTR0MEMUSERKERNEL_BASIC:
91 {
92 int rc = RTR0MemUserCopyFrom(pbKrnlBuf, R3Ptr, PAGE_SIZE);
93 if (rc == VINF_SUCCESS)
94 {
95 rc = RTR0MemUserCopyTo(R3Ptr, pbKrnlBuf, PAGE_SIZE);
96 if (rc == VINF_SUCCESS)
97 {
98 if (RTR0MemUserIsValidAddr(R3Ptr))
99 {
100 if (RTR0MemKernelIsValidAddr(pbKrnlBuf))
101 {
102 if (RTR0MemAreKrnlAndUsrDifferent())
103 {
104 RTStrPrintf(pszErr, cchErr, "RTR0MemAreKrnlAndUsrDifferent returns true");
105 if (!RTR0MemUserIsValidAddr((uintptr_t)pbKrnlBuf))
106 {
107 if (!RTR0MemKernelIsValidAddr((void *)R3Ptr))
108 {
109 /* done */
110 }
111 else
112 RTStrPrintf(pszErr, cchErr, "! #5 - RTR0MemKernelIsValidAddr -> true, expected false");
113 }
114 else
115 RTStrPrintf(pszErr, cchErr, "! #5 - RTR0MemUserIsValidAddr -> true, expected false");
116 }
117 else
118 RTStrPrintf(pszErr, cchErr, "RTR0MemAreKrnlAndUsrDifferent returns false");
119 }
120 else
121 RTStrPrintf(pszErr, cchErr, "! #4 - RTR0MemKernelIsValidAddr -> false, expected true");
122 }
123 else
124 RTStrPrintf(pszErr, cchErr, "! #3 - RTR0MemUserIsValidAddr -> false, expected true");
125 }
126 else
127 RTStrPrintf(pszErr, cchErr, "! #2 - RTR0MemUserCopyTo -> %Rrc expected %Rrc", rc, VINF_SUCCESS);
128 }
129 else
130 RTStrPrintf(pszErr, cchErr, "! #1 - RTR0MemUserCopyFrom -> %Rrc expected %Rrc", rc, VINF_SUCCESS);
131 break;
132 }
133
134#define TEST_OFF_SIZE(off, size, rcExpect) \
135 if (1) \
136 { \
137 int rc = RTR0MemUserCopyFrom(pbKrnlBuf, R3Ptr + (off), (size)); \
138 if (rc != (rcExpect)) \
139 { \
140 RTStrPrintf(pszErr, cchErr, "!RTR0MemUserCopyFrom(, +%#x, %#x) -> %Rrc, expected %Rrc", \
141 (off), (size), rc, (rcExpect)); \
142 break; \
143 } \
144 rc = RTR0MemUserCopyTo(R3Ptr + (off), pbKrnlBuf, (size)); \
145 if (rc != (rcExpect)) \
146 { \
147 RTStrPrintf(pszErr, cchErr, "!RTR0MemUserCopyTo(+%#x,, %#x) -> %Rrc, expected %Rrc", \
148 (off), (size), rc, (rcExpect)); \
149 break; \
150 } \
151 } else do {} while (0)
152
153 case TSTRTR0MEMUSERKERNEL_GOOD:
154 {
155 for (unsigned off = 0; off < 16 && !*pszErr; off++)
156 for (unsigned cb = 0; cb < PAGE_SIZE - 16; cb++)
157 TEST_OFF_SIZE(off, cb, VINF_SUCCESS);
158 break;
159 }
160
161 case TSTRTR0MEMUSERKERNEL_BAD:
162 {
163 for (unsigned off = 0; off < 16 && !*pszErr; off++)
164 for (unsigned cb = 0; cb < PAGE_SIZE - 16; cb++)
165 TEST_OFF_SIZE(off, cb, cb > 0 ? VERR_ACCESS_DENIED : VINF_SUCCESS);
166 break;
167 }
168
169 case TSTRTR0MEMUSERKERNEL_INVALID_ADDRESS:
170 {
171 if ( !RTR0MemUserIsValidAddr(R3Ptr)
172 && RTR0MemKernelIsValidAddr((void *)R3Ptr))
173 {
174 for (unsigned off = 0; off < 16 && !*pszErr; off++)
175 for (unsigned cb = 0; cb < PAGE_SIZE - 16; cb++)
176 TEST_OFF_SIZE(off, cb, cb > 0 ? VERR_ACCESS_DENIED : VINF_SUCCESS); /* ... */
177 }
178 else
179 RTStrPrintf(pszErr, cchErr, "RTR0MemUserIsValidAddr returns true");
180 break;
181 }
182
183 default:
184 RTStrPrintf(pszErr, cchErr, "!Unknown test #%d", uOperation);
185 break;
186 }
187
188 /* The error indicator is the '!' in the message buffer. */
189 RTMemFree(pbKrnlBuf);
190 return VINF_SUCCESS;
191}
192
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