Changeset 72454 in vbox for trunk/src/VBox/Runtime/common/fuzz/fuzz-observer.cpp
- Timestamp:
- Jun 5, 2018 7:32:45 PM (7 years ago)
- svn:sync-xref-src-repo-rev:
- 122935
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/common/fuzz/fuzz-observer.cpp
r72437 r72454 1 1 /* $Id$ */ 2 2 /** @file 3 * IPRT Fuzzing framework API (Fuzz).3 * IPRT - Fuzzing framework API, observer. 4 4 */ 5 5 … … 29 29 * Header Files * 30 30 *********************************************************************************************************************************/ 31 #include <iprt/fuzz.h> 32 #include "internal/iprt.h" 33 31 34 #include <iprt/asm.h> 32 35 #include <iprt/assert.h> 33 #include <iprt/cdefs.h>34 36 #include <iprt/ctype.h> 35 37 #include <iprt/err.h> 36 38 #include <iprt/env.h> 37 39 #include <iprt/file.h> 38 #include <iprt/fuzz.h> 40 #include <iprt/md5.h> 41 #include <iprt/mem.h> 42 #include <iprt/mp.h> 39 43 #include <iprt/path.h> 40 44 #include <iprt/pipe.h> 41 45 #include <iprt/process.h> 42 #include <iprt/md5.h>43 #include <iprt/mem.h>44 #include <iprt/mp.h>45 46 #include <iprt/semaphore.h> 46 47 #include <iprt/stream.h> … … 51 52 52 53 /********************************************************************************************************************************* 53 * Defined Constants And Macros *54 *********************************************************************************************************************************/55 56 57 /*********************************************************************************************************************************58 54 * Structures and Typedefs * 59 55 *********************************************************************************************************************************/ 60 61 56 /** Pointer to the internal fuzzing observer state. */ 62 57 typedef struct RTFUZZOBSINT *PRTFUZZOBSINT; … … 69 64 { 70 65 /** The thread handle. */ 71 RTTHREAD hThr d;66 RTTHREAD hThread; 72 67 /** The observer ID. */ 73 68 uint32_t idObs; … … 95 90 RTFUZZCTX hFuzzCtx; 96 91 /** Temp directory for input files. */ 97 char 92 char *pszTmpDir; 98 93 /** The binary to run. */ 99 char 94 char *pszBinary; 100 95 /** Arguments to run the binary with, terminated by a NULL entry. */ 101 char 96 char **papszArgs; 102 97 /** Number of arguments. */ 103 98 uint32_t cArgs; … … 107 102 volatile bool fShutdown; 108 103 /** Global observer thread handle. */ 109 RTTHREAD hThr dGlobal;104 RTTHREAD hThreadGlobal; 110 105 /** The event semaphore handle for the global observer thread. */ 111 106 RTSEMEVENT hEvtGlobal; … … 113 108 volatile uint64_t bmEvt; 114 109 /** Number of threads created - one for each process. */ 115 uint32_t cThr ds;110 uint32_t cThreads; 116 111 /** Pointer to the array of observer thread states. */ 117 PRTFUZZOBSTHRD paObsThr ds;112 PRTFUZZOBSTHRD paObsThreads; 118 113 } RTFUZZOBSINT; 119 114 120 121 /*********************************************************************************************************************************122 * Global variables *123 *********************************************************************************************************************************/124 125 126 127 /*********************************************************************************************************************************128 * Internal Functions *129 *********************************************************************************************************************************/130 115 131 116 … … 134 119 * 135 120 * @returns IPRT status code. 136 * @param hThr d The thread handle.121 * @param hThread The thread handle. 137 122 * @param pvUser Opaque user data. 138 123 */ 139 static DECLCALLBACK(int) rtFuzzObsWorkerLoop(RTTHREAD hThr d, void *pvUser)124 static DECLCALLBACK(int) rtFuzzObsWorkerLoop(RTTHREAD hThread, void *pvUser) 140 125 { 141 126 PRTFUZZOBSTHRD pObsThrd = (PRTFUZZOBSTHRD)pvUser; … … 161 146 { 162 147 /* Wait for work. */ 163 int rc = RTThreadUserWait(hThr d, RT_INDEFINITE_WAIT);148 int rc = RTThreadUserWait(hThread, RT_INDEFINITE_WAIT); 164 149 AssertRC(rc); 165 150 … … 312 297 * 313 298 * @returns IPRT status code. 314 * @param hThr d The thread handle.299 * @param hThread The thread handle. 315 300 * @param pvUser Opaque user data. 316 301 */ 317 static DECLCALLBACK(int) rtFuzzObsMasterLoop(RTTHREAD hThr d, void *pvUser)318 { 319 RT_NOREF(hThr d);302 static DECLCALLBACK(int) rtFuzzObsMasterLoop(RTTHREAD hThread, void *pvUser) 303 { 304 RT_NOREF(hThread); 320 305 int rc = VINF_SUCCESS; 321 306 PRTFUZZOBSINT pThis = (PRTFUZZOBSINT)pvUser; 322 307 323 RTThreadUserSignal(hThr d);308 RTThreadUserSignal(hThread); 324 309 325 310 while ( !pThis->fShutdown … … 333 318 { 334 319 /* Create a new input for this observer and kick it. */ 335 PRTFUZZOBSTHRD pObsThrd = &pThis->paObsThr ds[idxObs];320 PRTFUZZOBSTHRD pObsThrd = &pThis->paObsThreads[idxObs]; 336 321 337 322 /* Release the old input. */ … … 351 336 { 352 337 ASMAtomicWriteBool(&pObsThrd->fNewInput, true); 353 RTThreadUserSignal(pObsThrd->hThr d);338 RTThreadUserSignal(pObsThrd->hThread); 354 339 } 355 340 } … … 382 367 383 368 ASMAtomicBitSet(&pThis->bmEvt, idObs); 384 return RTThreadCreate(&pObsThrd->hThr d, rtFuzzObsWorkerLoop, pObsThrd, 0, RTTHREADTYPE_IO,369 return RTThreadCreate(&pObsThrd->hThread, rtFuzzObsWorkerLoop, pObsThrd, 0, RTTHREADTYPE_IO, 385 370 RTTHREADFLAGS_WAITABLE, "Fuzz-Worker"); 386 371 } … … 392 377 * @returns IPRT status code. 393 378 * @param pThis The internal fuzzing observer state. 394 * @param cThr dsNumber of worker threads to create.395 */ 396 static int rtFuzzObsWorkersCreate(PRTFUZZOBSINT pThis, uint32_t cThr ds)379 * @param cThreads Number of worker threads to create. 380 */ 381 static int rtFuzzObsWorkersCreate(PRTFUZZOBSINT pThis, uint32_t cThreads) 397 382 { 398 383 int rc = VINF_SUCCESS; 399 PRTFUZZOBSTHRD paObsThr ds = (PRTFUZZOBSTHRD)RTMemAllocZ(cThrds * sizeof(RTFUZZOBSTHRD));400 if (RT_LIKELY(paObsThr ds))401 { 402 for (unsigned i = 0; i < cThr ds && RT_SUCCESS(rc); i++)403 { 404 rc = rtFuzzObsWorkerThreadInit(pThis, i, &paObsThr ds[i]);384 PRTFUZZOBSTHRD paObsThreads = (PRTFUZZOBSTHRD)RTMemAllocZ(cThreads * sizeof(RTFUZZOBSTHRD)); 385 if (RT_LIKELY(paObsThreads)) 386 { 387 for (unsigned i = 0; i < cThreads && RT_SUCCESS(rc); i++) 388 { 389 rc = rtFuzzObsWorkerThreadInit(pThis, i, &paObsThreads[i]); 405 390 if (RT_FAILURE(rc)) 406 391 { … … 411 396 412 397 if (RT_SUCCESS(rc)) 413 pThis->paObsThr ds = paObsThrds;398 pThis->paObsThreads = paObsThreads; 414 399 else 415 RTMemFree(paObsThr ds);400 RTMemFree(paObsThreads); 416 401 } 417 402 … … 433 418 if (RT_SUCCESS(rc)) 434 419 { 435 rc = RTThreadCreate(&pThis->hThr dGlobal, rtFuzzObsMasterLoop, pThis, 0, RTTHREADTYPE_IO,420 rc = RTThreadCreate(&pThis->hThreadGlobal, rtFuzzObsMasterLoop, pThis, 0, RTTHREADTYPE_IO, 436 421 RTTHREADFLAGS_WAITABLE, "Fuzz-Master"); 437 422 if (RT_SUCCESS(rc)) 438 423 { 439 RTThreadUserWait(pThis->hThr dGlobal, RT_INDEFINITE_WAIT);424 RTThreadUserWait(pThis->hThreadGlobal, RT_INDEFINITE_WAIT); 440 425 } 441 426 else … … 461 446 pThis->papszArgs = NULL; 462 447 pThis->fFlags = 0; 463 pThis->hThr dGlobal = NIL_RTTHREAD;448 pThis->hThreadGlobal = NIL_RTTHREAD; 464 449 pThis->hEvtGlobal = NIL_RTSEMEVENT; 465 450 pThis->bmEvt = 0; 466 pThis->cThr ds= 0;467 pThis->paObsThr ds = NULL;451 pThis->cThreads = 0; 452 pThis->paObsThreads = NULL; 468 453 rc = RTFuzzCtxCreate(&pThis->hFuzzCtx); 469 454 if (RT_SUCCESS(rc)) … … 488 473 489 474 /* Wait for the master thread to terminate. */ 490 if (pThis->hThr dGlobal != NIL_RTTHREAD)475 if (pThis->hThreadGlobal != NIL_RTTHREAD) 491 476 { 492 477 ASMAtomicXchgBool(&pThis->fShutdown, true); 493 RTThreadWait(pThis->hThr dGlobal, RT_INDEFINITE_WAIT, NULL);478 RTThreadWait(pThis->hThreadGlobal, RT_INDEFINITE_WAIT, NULL); 494 479 } 495 480 496 481 /* Clean up the workers. */ 497 if (pThis->paObsThr ds)498 { 499 for (unsigned i = 0; i < pThis->cThr ds; i++)500 { 501 PRTFUZZOBSTHRD pThrd = &pThis->paObsThr ds[i];482 if (pThis->paObsThreads) 483 { 484 for (unsigned i = 0; i < pThis->cThreads; i++) 485 { 486 PRTFUZZOBSTHRD pThrd = &pThis->paObsThreads[i]; 502 487 ASMAtomicXchgBool(&pThrd->fShutdown, true); 503 RTThreadWait(pThrd->hThr d, RT_INDEFINITE_WAIT, NULL);488 RTThreadWait(pThrd->hThread, RT_INDEFINITE_WAIT, NULL); 504 489 if (pThrd->hFuzzInput) 505 490 RTFuzzInputRelease(pThrd->hFuzzInput); 506 491 } 507 RTMemFree(pThis->paObsThr ds);508 pThis->paObsThr ds = NULL;492 RTMemFree(pThis->paObsThreads); 493 pThis->paObsThreads = NULL; 509 494 } 510 495
Note:
See TracChangeset
for help on using the changeset viewer.