VirtualBox

source: vbox/trunk/src/VBox/Devices/testcase/tstDeviceIoFuzz.cpp@ 92016

最後變更 在這個檔案從92016是 92000,由 vboxsync 提交於 3 年 前

Devices/testcase/tstDevice: Some basic MMIO fuzzing, bugref:9006

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.7 KB
 
1/* $Id: tstDeviceIoFuzz.cpp 92000 2021-10-22 11:49:35Z vboxsync $ */
2/** @file
3 * tstDeviceSsmFuzz - I/O fuzzing testcase.
4 */
5
6/*
7 * Copyright (C) 2021 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEFAULT /** @todo */
23#include <VBox/types.h>
24#include <iprt/errcore.h>
25#include <iprt/mem.h>
26#include <iprt/fuzz.h>
27#include <iprt/time.h>
28#include <iprt/rand.h>
29#include <iprt/string.h>
30#include <iprt/stream.h>
31
32#include "tstDeviceBuiltin.h"
33#include "tstDeviceCfg.h"
34#include "tstDeviceInternal.h"
35
36
37/*********************************************************************************************************************************
38* Defined Constants And Macros *
39*********************************************************************************************************************************/
40
41
42/*********************************************************************************************************************************
43* Structures and Typedefs *
44*********************************************************************************************************************************/
45
46
47static const uint32_t g_aAccWidths[] = { 1, 2, 4, 8 };
48
49static PCTSTDEVCFGITEM tstDevSsmFuzzGetCfgItem(PCTSTDEVCFGITEM paCfg, uint32_t cCfgItems, const char *pszName)
50{
51 for (uint32_t i = 0; i < cCfgItems; i++)
52 {
53 if (!RTStrCmp(paCfg[i].pszKey, pszName))
54 return &paCfg[i];
55 }
56
57 return NULL;
58}
59
60
61static uint64_t tstDevSsmFuzzGetCfgU64(PCTSTDEVCFGITEM paCfg, uint32_t cCfgItems, const char *pszName)
62{
63 PCTSTDEVCFGITEM pCfgItem = tstDevSsmFuzzGetCfgItem(paCfg, cCfgItems, pszName);
64 if ( pCfgItem
65 && pCfgItem->enmType == TSTDEVCFGITEMTYPE_INTEGER)
66 return (uint64_t)pCfgItem->u.i64;
67
68 return 0;
69}
70
71
72/**
73 * Entry point for the SSM fuzzer.
74 *
75 * @returns VBox status code.
76 * @param hDut The device under test.
77 * @param paCfg The testcase config.
78 * @param cCfgItems Number of config items.
79 */
80static DECLCALLBACK(int) tstDevIoFuzzEntry(TSTDEVDUT hDut, PCTSTDEVCFGITEM paCfg, uint32_t cCfgItems)
81{
82 /* Determine the amount of I/O port handlers. */
83 uint32_t cIoPortRegs = 0;
84 PRTDEVDUTIOPORT pIoPort;
85 RTListForEach(&hDut->LstIoPorts, pIoPort, RTDEVDUTIOPORT, NdIoPorts)
86 {
87 cIoPortRegs++;
88 }
89
90 /* Determine the amount of MMIO regions. */
91 uint32_t cMmioRegions = 0;
92 PRTDEVDUTMMIO pMmio;
93 RTListForEach(&hDut->LstMmio, pMmio, RTDEVDUTMMIO, NdMmio)
94 {
95 cMmioRegions++;
96 }
97
98 RTRAND hRnd;
99 int rc = RTRandAdvCreateParkMiller(&hRnd);
100 if (RT_SUCCESS(rc))
101 {
102 RTRandAdvSeed(hRnd, 0x123456789);
103 uint64_t cRuntimeMs = tstDevSsmFuzzGetCfgU64(paCfg, cCfgItems, "RuntimeSec") * RT_MS_1SEC_64;
104 uint64_t tsStart = RTTimeMilliTS();
105 uint64_t cFuzzedInputs = 0;
106 RTCritSectEnter(&hDut->pDevIns->pCritSectRoR3->s.CritSect);
107 do
108 {
109 bool fMmio = false;
110
111 if ( cMmioRegions
112 && !cIoPortRegs)
113 fMmio = true;
114 else if ( !cMmioRegions
115 && cIoPortRegs)
116 fMmio = false;
117 else
118 fMmio = RT_BOOL(RTRandAdvU32Ex(hRnd, 0, 1));
119
120 if (fMmio)
121 {
122 uint32_t iMmio = RTRandAdvU32Ex(hRnd, 0, cMmioRegions - 1);
123 RTListForEach(&hDut->LstMmio, pMmio, RTDEVDUTMMIO, NdMmio)
124 {
125 if (!iMmio)
126 break;
127 iMmio--;
128 }
129
130 uint32_t uMin = pMmio->pfnWriteR3 ? 0 : 1;
131 uint32_t uMax = pMmio->pfnReadR3 ? 1 : 0;
132
133 RTGCPHYS offRegion = RTRandAdvU64Ex(hRnd, 0, pMmio->cbRegion);
134 bool fRead = RT_BOOL(uMin == uMax ? uMin : RTRandAdvU32Ex(hRnd, uMin, uMax));
135 uint64_t u64Value = fRead ? 0 : RTRandAdvU64(hRnd);
136 uint32_t cbValue = g_aAccWidths[RTRandAdvU32Ex(hRnd, 0, 2)];
137
138 if (fRead)
139 pMmio->pfnReadR3(hDut->pDevIns, pMmio->pvUserR3, offRegion, &u64Value, cbValue);
140 else
141 pMmio->pfnWriteR3(hDut->pDevIns, pMmio->pvUserR3, offRegion, &u64Value, cbValue);
142 }
143 else
144 {
145 uint32_t iIoPort = RTRandAdvU32Ex(hRnd, 0, cIoPortRegs - 1);
146 RTListForEach(&hDut->LstIoPorts, pIoPort, RTDEVDUTIOPORT, NdIoPorts)
147 {
148 if (!iIoPort)
149 break;
150 iIoPort--;
151 }
152
153 uint32_t uMin = pIoPort->pfnOutR3 ? 0 : 1;
154 uint32_t uMax = pIoPort->pfnInR3 ? 1 : 0;
155
156 uint32_t offPort = RTRandAdvU32Ex(hRnd, 0, pIoPort->cPorts);
157 bool fRead = RT_BOOL(uMin == uMax ? uMin : RTRandAdvU32Ex(hRnd, uMin, uMax));
158 uint32_t u32Value = fRead ? 0 : RTRandAdvU32(hRnd);
159 uint32_t cbValue = g_aAccWidths[RTRandAdvU32Ex(hRnd, 0, 2)];
160
161 if (fRead)
162 pIoPort->pfnInR3(hDut->pDevIns, pIoPort->pvUserR3, offPort, &u32Value, cbValue);
163 else
164 pIoPort->pfnOutR3(hDut->pDevIns, pIoPort->pvUserR3, offPort, u32Value, cbValue);
165 }
166
167 cFuzzedInputs++;
168 } while ( RT_SUCCESS(rc)
169 && RTTimeMilliTS() - tsStart < cRuntimeMs);
170 RTCritSectLeave(&hDut->pDevIns->pCritSectRoR3->s.CritSect);
171
172 RTPrintf("Fuzzed inputs: %u\n", cFuzzedInputs);
173 RTRandAdvDestroy(hRnd);
174 }
175
176 return rc;
177}
178
179
180const TSTDEVTESTCASEREG g_TestcaseIoFuzz =
181{
182 /** szName */
183 "IoFuzz",
184 /** pszDesc */
185 "Fuzzes devices I/O handlers",
186 /** fFlags */
187 0,
188 /** pfnTestEntry */
189 tstDevIoFuzzEntry
190};
191
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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