VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/fuzz/fuzzmastercmd.cpp@ 72438

Last change on this file since 72438 was 72438, checked in by vboxsync, 7 years ago

Runtime/RTFuzz: build fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/* $Id: fuzzmastercmd.cpp 72438 2018-06-04 21:17:56Z vboxsync $ */
2/** @file
3 * IPRT Fuzzing framework API (Fuzz).
4 */
5
6/*
7 * Copyright (C) 2018 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/asm.h>
32#include <iprt/assert.h>
33#include <iprt/buildconfig.h>
34#include <iprt/cdefs.h>
35#include <iprt/ctype.h>
36#include <iprt/err.h>
37#include <iprt/fuzz.h>
38#include <iprt/getopt.h>
39#include <iprt/mem.h>
40#include <iprt/message.h>
41#include <iprt/stream.h>
42#include <iprt/thread.h>
43
44
45/*********************************************************************************************************************************
46* Defined Constants And Macros *
47*********************************************************************************************************************************/
48
49
50/*********************************************************************************************************************************
51* Structures and Typedefs *
52*********************************************************************************************************************************/
53
54
55/*********************************************************************************************************************************
56* Global variables *
57*********************************************************************************************************************************/
58
59
60/*********************************************************************************************************************************
61* Internal Functions *
62*********************************************************************************************************************************/
63
64
65/**
66 * Wrapper around RTErrInfoSetV / RTMsgErrorV.
67 *
68 * @returns @a rc
69 * @param pErrInfo Extended error info.
70 * @param rc The return code.
71 * @param pszFormat The message format.
72 * @param ... The message format arguments.
73 */
74static int rtFuzzCmdMasterErrorRc(PRTERRINFO pErrInfo, int rc, const char *pszFormat, ...)
75{
76 va_list va;
77 va_start(va, pszFormat);
78 if (pErrInfo)
79 RTErrInfoSetV(pErrInfo, rc, pszFormat, va);
80 else
81 RTMsgErrorV(pszFormat, va);
82 va_end(va);
83 return rc;
84}
85
86
87/**
88 * Executes the
89 */
90static RTEXITCODE rtFuzzCmdMasterDoIt(const char *pszBinary, uint32_t cProcs, const char *pszInpSeedDir,
91 size_t cbInputMax, const char * const *papszClientArgs, unsigned cClientArgs,
92 bool fInputFile, const char *pszTmpDir)
93{
94 RTFUZZOBS hFuzzObs;
95
96 int rc = RTFuzzObsCreate(&hFuzzObs);
97 if (RT_SUCCESS(rc))
98 {
99 RTFUZZCTX hFuzzCtx;
100
101 rc = RTFuzzObsQueryCtx(hFuzzObs, &hFuzzCtx);
102 if (RT_SUCCESS(rc))
103 {
104 uint32_t fFlags = 0;
105
106 if (fInputFile)
107 {
108 fFlags |= RTFUZZ_OBS_BINARY_F_INPUT_FILE;
109 rc = RTFuzzObsSetTmpDirectory(hFuzzObs, pszTmpDir);
110 }
111
112 if (RT_SUCCESS(rc))
113 rc = RTFuzzObsSetTestBinary(hFuzzObs, pszBinary, fFlags);
114 if (RT_SUCCESS(rc))
115 {
116 rc = RTFuzzObsSetTestBinaryArgs(hFuzzObs, papszClientArgs, cClientArgs);
117 if (RT_SUCCESS(rc))
118 {
119 rc = RTFuzzCtxCfgSetInputSeedMaximum(hFuzzCtx, cbInputMax);
120 if (RT_SUCCESS(rc))
121 {
122 rc = RTFuzzCtxCorpusInputAddFromDirPath(hFuzzCtx, pszInpSeedDir);
123 if (RT_SUCCESS(rc))
124 {
125 rc = RTFuzzObsExecStart(hFuzzObs, cProcs);
126 if (RT_SUCCESS(rc))
127 {
128 RTThreadSleep(3600 * RT_MS_1SEC);
129 RTFuzzObsExecStop(hFuzzObs);
130 }
131 else
132 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Failed to start fuzzing observer: %Rrc\n", rc);
133 }
134 else
135 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Failed to load corpus seeds from \"%s\": %Rrc\n", pszInpSeedDir, rc);
136 }
137 else
138 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Failed to set maximum input size to %zu: %Rrc\n", cbInputMax, rc);
139 }
140 else
141 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Failed to set test program arguments: %Rrc\n", rc);
142 }
143 else
144 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Failed to set the specified binary as test program: %Rrc\n", rc);
145
146 RTFuzzCtxRelease(hFuzzCtx);
147 }
148 else
149 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Error obtaining the fuzzing context from the observer: %Rrc\n", rc);
150
151 RTFuzzObsDestroy(hFuzzObs);
152 }
153 else
154 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Error creating observer instance: %Rrc\n", rc);
155
156 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
157}
158
159
160RTR3DECL(RTEXITCODE) RTFuzzCmdMaster(unsigned cArgs, char **papszArgs)
161{
162 /*
163 * Parse the command line.
164 */
165 static const RTGETOPTDEF s_aOptions[] =
166 {
167 { "--binary", 'b', RTGETOPT_REQ_STRING },
168 { "--processes", 'p', RTGETOPT_REQ_UINT32 },
169 { "--input-as-file", 'f', RTGETOPT_REQ_STRING },
170 { "--input-seed-file", 'i', RTGETOPT_REQ_STRING },
171 { "--input-seed-dir", 's', RTGETOPT_REQ_STRING },
172 { "--input-seed-size-max", 'm', RTGETOPT_REQ_UINT32 },
173 { "--args", 'a', RTGETOPT_REQ_STRING },
174 { "--help", 'h', RTGETOPT_REQ_NOTHING },
175 { "--version", 'V', RTGETOPT_REQ_NOTHING },
176 };
177
178 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
179
180 const char *pszBinary = NULL;
181 uint32_t cProcs = 0;
182 const char *pszInpSeedDir = NULL;
183 int cClientArgs = 0;
184 size_t cbInputMax = 0;
185 char **papszClientArgs = NULL;
186 bool fInputFile = false;
187 const char *pszTmpDir = NULL;
188
189 RTGETOPTSTATE GetState;
190 int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
191 RTGETOPTINIT_FLAGS_OPTS_FIRST);
192 if (RT_SUCCESS(rc))
193 {
194 bool fContinue = true;
195 do
196 {
197 RTGETOPTUNION ValueUnion;
198 int chOpt = RTGetOpt(&GetState, &ValueUnion);
199 switch (chOpt)
200 {
201 case 0:
202 rcExit = rtFuzzCmdMasterDoIt(pszBinary, cProcs, pszInpSeedDir, cbInputMax,
203 papszClientArgs, cClientArgs, fInputFile, pszTmpDir);
204 fContinue = false;
205 break;
206
207 case 'b':
208 pszBinary = ValueUnion.psz;
209 break;
210
211 case 'p':
212 cProcs = ValueUnion.u32;
213 break;
214
215 case 'f':
216 pszTmpDir = ValueUnion.psz;
217 fInputFile = true;
218 break;
219
220 case 'a':
221 rc = RTGetOptArgvFromString(&papszClientArgs, &cClientArgs, ValueUnion.psz, 0, NULL);
222 break;
223
224 case 's':
225 pszInpSeedDir = ValueUnion.psz;
226 break;
227
228 case 'm':
229 cbInputMax = ValueUnion.u32;
230 break;
231
232 case 'h':
233 RTPrintf("Usage: to be written\nOption dump:\n");
234 for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
235 RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
236 fContinue = false;
237 break;
238
239 case 'V':
240 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
241 fContinue = false;
242 break;
243
244 default:
245 rcExit = RTGetOptPrintError(chOpt, &ValueUnion);
246 fContinue = false;
247 break;
248 }
249 } while (fContinue);
250 }
251 else
252 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
253 return rcExit;
254}
255
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