VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTR0MemUserKernelDriver.cpp@ 100762

Last change on this file since 100762 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: tstRTR0MemUserKernelDriver.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT R0 Testcase - Thread Preemption, driver program.
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/initterm.h>
42
43#include <iprt/errcore.h>
44#include <iprt/path.h>
45#include <iprt/param.h>
46#include <iprt/stream.h>
47#include <iprt/string.h>
48#include <iprt/test.h>
49#include <iprt/thread.h>
50#ifdef VBOX
51# include <VBox/sup.h>
52# include "tstRTR0MemUserKernel.h"
53#endif
54
55
56/**
57 * Entry point.
58 */
59extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
60{
61 RT_NOREF3(argc, argv, envp);
62#ifndef VBOX
63 RTPrintf("tstSup: SKIPPED\n");
64 return 0;
65#else
66 /*
67 * Init.
68 */
69 RTTEST hTest;
70 int rc = RTTestInitAndCreate("tstRTR0MemUserKernel", &hTest);
71 if (rc)
72 return rc;
73 RTTestBanner(hTest);
74
75 uint8_t *pbPage = (uint8_t *)RTTestGuardedAllocTail(hTest, PAGE_SIZE);
76 if (!pbPage)
77 {
78 RTTestFailed(hTest, "RTTestGuardedAllocTail failed with rc=%Rrc\n", rc);
79 return RTTestSummaryAndDestroy(hTest);
80 }
81
82 PSUPDRVSESSION pSession;
83 rc = SUPR3Init(&pSession);
84 if (RT_FAILURE(rc))
85 {
86 RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
87 return RTTestSummaryAndDestroy(hTest);
88 }
89
90 char szPath[RTPATH_MAX];
91 rc = RTPathExecDir(szPath, sizeof(szPath));
92 if (RT_SUCCESS(rc))
93 rc = RTPathAppend(szPath, sizeof(szPath), "tstRTR0MemUserKernel.r0");
94 if (RT_FAILURE(rc))
95 {
96 RTTestFailed(hTest, "Failed constructing .r0 filename (rc=%Rrc)", rc);
97 return RTTestSummaryAndDestroy(hTest);
98 }
99
100 void *pvImageBase;
101 rc = SUPR3LoadServiceModule(szPath, "tstRTR0MemUserKernel",
102 "TSTRTR0MemUserKernelSrvReqHandler",
103 &pvImageBase);
104 if (RT_FAILURE(rc))
105 {
106 RTTestFailed(hTest, "SUPR3LoadServiceModule(%s,,,) failed with rc=%Rrc\n", szPath, rc);
107 return RTTestSummaryAndDestroy(hTest);
108 }
109
110 /* test request */
111 struct
112 {
113 SUPR0SERVICEREQHDR Hdr;
114 char szMsg[256];
115 } Req;
116
117 /*
118 * Sanity checks.
119 */
120 RTTestSub(hTest, "Sanity");
121 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
122 Req.Hdr.cbReq = sizeof(Req);
123 Req.szMsg[0] = '\0';
124 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0MemUserKernel", sizeof("tstRTR0MemUserKernel") - 1,
125 TSTRTR0MEMUSERKERNEL_SANITY_OK, 0, &Req.Hdr), VINF_SUCCESS);
126 if (RT_FAILURE(rc))
127 return RTTestSummaryAndDestroy(hTest);
128 RTTESTI_CHECK_MSG(Req.szMsg[0] == '\0', ("%s", Req.szMsg));
129 if (Req.szMsg[0] != '\0')
130 return RTTestSummaryAndDestroy(hTest);
131
132 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
133 Req.Hdr.cbReq = sizeof(Req);
134 Req.szMsg[0] = '\0';
135 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0MemUserKernel", sizeof("tstRTR0MemUserKernel") - 1,
136 TSTRTR0MEMUSERKERNEL_SANITY_FAILURE, 0, &Req.Hdr), VINF_SUCCESS);
137 if (RT_FAILURE(rc))
138 return RTTestSummaryAndDestroy(hTest);
139 RTTESTI_CHECK_MSG(!strncmp(Req.szMsg, RT_STR_TUPLE("!42failure42")), ("%s", Req.szMsg));
140 if (strncmp(Req.szMsg, RT_STR_TUPLE("!42failure42")))
141 return RTTestSummaryAndDestroy(hTest);
142
143 /*
144 * Basic tests, bail out on failure.
145 */
146 RTTestSub(hTest, "Basics");
147 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
148 Req.Hdr.cbReq = sizeof(Req);
149 Req.szMsg[0] = '\0';
150 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0MemUserKernel", sizeof("tstRTR0MemUserKernel") - 1,
151 TSTRTR0MEMUSERKERNEL_BASIC, (uintptr_t)pbPage, &Req.Hdr), VINF_SUCCESS);
152 if (RT_FAILURE(rc))
153 return RTTestSummaryAndDestroy(hTest);
154 if (Req.szMsg[0] == '!')
155 {
156 RTTestIFailed("%s", &Req.szMsg[1]);
157 return RTTestSummaryAndDestroy(hTest);
158 }
159 if (Req.szMsg[0])
160 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
161
162 /*
163 * Good buffer, bail out on failure.
164 */
165 RTTestSub(hTest, "Good buffer");
166 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
167 Req.Hdr.cbReq = sizeof(Req);
168 Req.szMsg[0] = '\0';
169 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0MemUserKernel", sizeof("tstRTR0MemUserKernel") - 1,
170 TSTRTR0MEMUSERKERNEL_GOOD, (uintptr_t)pbPage, &Req.Hdr), VINF_SUCCESS);
171 if (RT_FAILURE(rc))
172 return RTTestSummaryAndDestroy(hTest);
173 if (Req.szMsg[0] == '!')
174 {
175 RTTestIFailed("%s", &Req.szMsg[1]);
176 return RTTestSummaryAndDestroy(hTest);
177 }
178 if (Req.szMsg[0])
179 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
180
181 /*
182 * Bad buffer, bail out on failure.
183 */
184 RTTestSub(hTest, "Bad buffer");
185 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
186 Req.Hdr.cbReq = sizeof(Req);
187 Req.szMsg[0] = '\0';
188 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0MemUserKernel", sizeof("tstRTR0MemUserKernel") - 1,
189 TSTRTR0MEMUSERKERNEL_BAD, (uintptr_t)pbPage + PAGE_SIZE, &Req.Hdr), VINF_SUCCESS);
190 if (RT_FAILURE(rc))
191 return RTTestSummaryAndDestroy(hTest);
192 if (Req.szMsg[0] == '!')
193 {
194 RTTestIFailed("%s", &Req.szMsg[1]);
195 return RTTestSummaryAndDestroy(hTest);
196 }
197 if (Req.szMsg[0])
198 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
199
200 /*
201 * Bad buffer, bail out on failure.
202 */
203 RTTestSub(hTest, "Kernel buffer");
204 Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
205 Req.Hdr.cbReq = sizeof(Req);
206 Req.szMsg[0] = '\0';
207 RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstRTR0MemUserKernel", sizeof("tstRTR0MemUserKernel") - 1,
208 TSTRTR0MEMUSERKERNEL_INVALID_ADDRESS, (uintptr_t)pvImageBase, &Req.Hdr), VINF_SUCCESS);
209 if (RT_FAILURE(rc))
210 return RTTestSummaryAndDestroy(hTest);
211 if (Req.szMsg[0] == '!')
212 {
213 RTTestIFailed("%s", &Req.szMsg[1]);
214 return RTTestSummaryAndDestroy(hTest);
215 }
216 if (Req.szMsg[0])
217 RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
218
219 /*
220 * Done.
221 */
222 return RTTestSummaryAndDestroy(hTest);
223#endif
224}
225
226
227#if !defined(VBOX_WITH_HARDENING) || !defined(RT_OS_WINDOWS)
228/**
229 * Main entry point.
230 */
231int main(int argc, char **argv, char **envp)
232{
233 return TrustedMain(argc, argv, envp);
234}
235#endif
236
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