VirtualBox

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

Last change on this file since 72437 was 72437, 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: 10.0 KB
Line 
1/* $Id: fuzzmastercmd.cpp 72437 2018-06-04 21:16:02Z 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 for (;;)
129 RTThreadSleep(3600 * RT_MS_1SEC);
130 RTFuzzObsExecStop(hFuzzObs);
131 }
132 else
133 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Failed to start fuzzing observer: %Rrc\n", rc);
134 }
135 else
136 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Failed to load corpus seeds from \"%s\": %Rrc\n", pszInpSeedDir, rc);
137 }
138 else
139 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Failed to set maximum input size to %zu: %Rrc\n", cbInputMax, rc);
140 }
141 else
142 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Failed to set test program arguments: %Rrc\n", rc);
143 }
144 else
145 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Failed to set the specified binary as test program: %Rrc\n", rc);
146
147 RTFuzzCtxRelease(hFuzzCtx);
148 }
149 else
150 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Error obtaining the fuzzing context from the observer: %Rrc\n", rc);
151
152 RTFuzzObsDestroy(hFuzzObs);
153 }
154 else
155 rc = rtFuzzCmdMasterErrorRc(NULL, rc, "Error creating observer instance: %Rrc\n", rc);
156
157 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
158}
159
160
161RTR3DECL(RTEXITCODE) RTFuzzCmdMaster(unsigned cArgs, char **papszArgs)
162{
163 /*
164 * Parse the command line.
165 */
166 static const RTGETOPTDEF s_aOptions[] =
167 {
168 { "--binary", 'b', RTGETOPT_REQ_STRING },
169 { "--processes", 'p', RTGETOPT_REQ_UINT32 },
170 { "--input-as-file", 'f', RTGETOPT_REQ_STRING },
171 { "--input-seed-file", 'i', RTGETOPT_REQ_STRING },
172 { "--input-seed-dir", 's', RTGETOPT_REQ_STRING },
173 { "--input-seed-size-max", 'm', RTGETOPT_REQ_UINT32 },
174 { "--args", 'a', RTGETOPT_REQ_STRING },
175 { "--help", 'h', RTGETOPT_REQ_NOTHING },
176 { "--version", 'V', RTGETOPT_REQ_NOTHING },
177 };
178
179 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
180
181 const char *pszBinary = NULL;
182 uint32_t cProcs = 0;
183 const char *pszInpSeedDir = NULL;
184 int cClientArgs = 0;
185 size_t cbInputMax = 0;
186 char **papszClientArgs = NULL;
187 bool fInputFile = false;
188 const char *pszTmpDir = NULL;
189
190 RTGETOPTSTATE GetState;
191 int rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1,
192 RTGETOPTINIT_FLAGS_OPTS_FIRST);
193 if (RT_SUCCESS(rc))
194 {
195 bool fContinue = true;
196 do
197 {
198 RTGETOPTUNION ValueUnion;
199 int chOpt = RTGetOpt(&GetState, &ValueUnion);
200 switch (chOpt)
201 {
202 case 0:
203 rcExit = rtFuzzCmdMasterDoIt(pszBinary, cProcs, pszInpSeedDir, cbInputMax,
204 papszClientArgs, cClientArgs, fInputFile, pszTmpDir);
205 fContinue = false;
206 break;
207
208 case 'b':
209 pszBinary = ValueUnion.psz;
210 break;
211
212 case 'p':
213 cProcs = ValueUnion.u32;
214 break;
215
216 case 'f':
217 pszTmpDir = ValueUnion.psz;
218 fInputFile = true;
219 break;
220
221 case 'a':
222 rc = RTGetOptArgvFromString(&papszClientArgs, &cClientArgs, ValueUnion.psz, 0, NULL);
223 break;
224
225 case 's':
226 pszInpSeedDir = ValueUnion.psz;
227 break;
228
229 case 'm':
230 cbInputMax = ValueUnion.u32;
231 break;
232
233 case 'h':
234 RTPrintf("Usage: to be written\nOption dump:\n");
235 for (unsigned i = 0; i < RT_ELEMENTS(s_aOptions); i++)
236 RTPrintf(" -%c,%s\n", s_aOptions[i].iShort, s_aOptions[i].pszLong);
237 fContinue = false;
238 break;
239
240 case 'V':
241 RTPrintf("%sr%d\n", RTBldCfgVersion(), RTBldCfgRevision());
242 fContinue = false;
243 break;
244
245 default:
246 rcExit = RTGetOptPrintError(chOpt, &ValueUnion);
247 fContinue = false;
248 break;
249 }
250 } while (fContinue);
251 }
252 else
253 rcExit = RTMsgErrorExit(RTEXITCODE_SYNTAX, "RTGetOptInit: %Rrc", rc);
254 return rcExit;
255}
256
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