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