VirtualBox

source: vbox/trunk/src/VBox/Devices/testcase/tstDeviceSsmFuzz.cpp@ 83261

Last change on this file since 83261 was 83261, checked in by vboxsync, 5 years ago

Devices/tstDevice: Testbench updates, work in progress

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: tstDeviceSsmFuzz.cpp 83261 2020-03-11 16:10:11Z vboxsync $ */
2/** @file
3 * tstDeviceSsmFuzz - SSM fuzzing testcase.
4 */
5
6/*
7 * Copyright (C) 2020 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
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/string.h>
29
30#include "tstDeviceBuiltin.h"
31#include "tstDeviceCfg.h"
32#include "tstDeviceInternal.h"
33
34
35/*********************************************************************************************************************************
36* Defined Constants And Macros *
37*********************************************************************************************************************************/
38
39
40/*********************************************************************************************************************************
41* Structures and Typedefs *
42*********************************************************************************************************************************/
43
44
45static PCTSTDEVCFGITEM tstDevSsmFuzzGetCfgItem(PCTSTDEVCFGITEM paCfg, uint32_t cCfgItems, const char *pszName)
46{
47 for (uint32_t i = 0; i < cCfgItems; i++)
48 {
49 if (!RTStrCmp(paCfg[i].pszKey, pszName))
50 return &paCfg[i];
51 }
52
53 return NULL;
54}
55
56
57static const char *tstDevSsmFuzzGetCfgString(PCTSTDEVCFGITEM paCfg, uint32_t cCfgItems, const char *pszName)
58{
59 PCTSTDEVCFGITEM pCfgItem = tstDevSsmFuzzGetCfgItem(paCfg, cCfgItems, pszName);
60 if ( pCfgItem
61 && pCfgItem->enmType == TSTDEVCFGITEMTYPE_STRING)
62 return pCfgItem->u.psz;
63
64 return NULL;
65}
66
67
68static uint64_t tstDevSsmFuzzGetCfgU64(PCTSTDEVCFGITEM paCfg, uint32_t cCfgItems, const char *pszName)
69{
70 PCTSTDEVCFGITEM pCfgItem = tstDevSsmFuzzGetCfgItem(paCfg, cCfgItems, pszName);
71 if ( pCfgItem
72 && pCfgItem->enmType == TSTDEVCFGITEMTYPE_INTEGER)
73 return (uint64_t)pCfgItem->u.i64;
74
75 return 0;
76}
77
78
79static uint32_t tstDevSsmFuzzGetCfgU32(PCTSTDEVCFGITEM paCfg, uint32_t cCfgItems, const char *pszName)
80{
81 PCTSTDEVCFGITEM pCfgItem = tstDevSsmFuzzGetCfgItem(paCfg, cCfgItems, pszName);
82 if ( pCfgItem
83 && pCfgItem->enmType == TSTDEVCFGITEMTYPE_INTEGER)
84 return (uint32_t)pCfgItem->u.i64;
85
86 return 0;
87}
88
89
90/**
91 * Entry point for the SSM fuzzer.
92 *
93 * @returns VBox status code.
94 * @param hDut The device under test.
95 * @param paCfg The testcase config.
96 * @param cCfgItems Number of config items.
97 */
98static DECLCALLBACK(int) tstDevSsmFuzzEntry(TSTDEVDUT hDut, PCTSTDEVCFGITEM paCfg, uint32_t cCfgItems)
99{
100 RT_NOREF(hDut, paCfg);
101
102 RTFUZZCTX hFuzzCtx;
103 int rc = RTFuzzCtxCreate(&hFuzzCtx, RTFUZZCTXTYPE_BLOB);
104 if (RT_SUCCESS(rc))
105 {
106 rc = RTFuzzCtxCorpusInputAddFromDirPath(hFuzzCtx, tstDevSsmFuzzGetCfgString(paCfg, cCfgItems, "CorpusPath"));
107 if (RT_SUCCESS(rc))
108 {
109 rc = RTFuzzCtxCfgSetInputSeedMaximum(hFuzzCtx, (size_t)tstDevSsmFuzzGetCfgU64(paCfg, cCfgItems, "InputSizeMax"));
110 if (RT_SUCCESS(rc))
111 {
112 rc = RTFuzzCtxReseed(hFuzzCtx, tstDevSsmFuzzGetCfgU64(paCfg, cCfgItems, "Seed"));
113 if (RT_SUCCESS(rc))
114 {
115 /* Create a new SSM handle to use. */
116 PSSMHANDLE pSsm = (PSSMHANDLE)RTMemAllocZ(sizeof(*pSsm));
117 if (RT_LIKELY(pSsm))
118 {
119 pSsm->pDut = hDut;
120 pSsm->pbSavedState = NULL;
121 pSsm->cbSavedState = 0;
122 pSsm->offDataBuffer = 0;
123 pSsm->uCurUnitVer = tstDevSsmFuzzGetCfgU32(paCfg, cCfgItems, "UnitVersion");
124 pSsm->rc = VINF_SUCCESS;
125
126 uint64_t cRuntimeMs = tstDevSsmFuzzGetCfgU64(paCfg, cCfgItems, "RuntimeSec") * RT_MS_1SEC_64;
127 uint64_t tsStart = RTTimeMilliTS();
128 uint64_t cFuzzedInputs = 0;
129 do
130 {
131 RTFUZZINPUT hFuzzInp;
132 rc = RTFuzzCtxInputGenerate(hFuzzCtx, &hFuzzInp);
133 if (RT_SUCCESS(rc))
134 {
135 void *pvBlob = NULL;
136 size_t cbBlob = 0;
137
138 rc = RTFuzzInputQueryBlobData(hFuzzInp, &pvBlob, &cbBlob);
139 if (RT_SUCCESS(rc))
140 {
141 pSsm->pbSavedState = (uint8_t *)pvBlob;
142 pSsm->cbSavedState = cbBlob;
143 pSsm->offDataBuffer = 0;
144 pSsm->rc = VINF_SUCCESS;
145
146 /* Get the SSM handler from the device. */
147 int rcDut = VINF_SUCCESS;
148 PTSTDEVDUTSSM pSsmClbks = RTListGetFirst(&hDut->LstSsmHandlers, TSTDEVDUTSSM, NdSsm);
149 if (pSsmClbks)
150 {
151 /* Load preparations. */
152 if (pSsmClbks->pfnLoadPrep)
153 rcDut = pSsmClbks->pfnLoadPrep(hDut->pDevIns, pSsm);
154 if (RT_SUCCESS(rcDut))
155 rcDut = pSsmClbks->pfnLoadExec(hDut->pDevIns, pSsm, pSsm->uCurUnitVer, SSM_PASS_FINAL);
156
157 cFuzzedInputs++;
158 }
159 if (RT_SUCCESS(rcDut))
160 RTFuzzInputAddToCtxCorpus(hFuzzInp);
161 }
162 RTFuzzInputRelease(hFuzzInp);
163 }
164 } while ( RT_SUCCESS(rc)
165 && RTTimeMilliTS() - tsStart < cRuntimeMs);
166
167 RTMemFree(pSsm);
168 }
169 else
170 rc = VERR_NO_MEMORY;
171 }
172 }
173 }
174
175 RTFuzzCtxRelease(hFuzzCtx);
176 }
177
178 return rc;
179}
180
181
182const TSTDEVTESTCASEREG g_TestcaseSsmFuzz =
183{
184 /** szName */
185 "SsmFuzz",
186 /** pszDesc */
187 "Fuzzes devices SSM state loaders",
188 /** fFlags */
189 0,
190 /** pfnTestEntry */
191 tstDevSsmFuzzEntry
192};
193
Note: See TracBrowser for help on using the repository browser.

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