2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
24 Data generators for fuzzing test data in a reproducible way.
28 #include "SDL_config.h"
30 /* Visual Studio 2008 doesn't have stdint.h */
31 #if defined(_MSC_VER) && _MSC_VER <= 1500
32 #define UINT8_MAX ~(Uint8)0
33 #define UINT16_MAX ~(Uint16)0
34 #define UINT32_MAX ~(Uint32)0
35 #define UINT64_MAX ~(Uint64)0
48 * Counter for fuzzer invocations
50 static int fuzzerInvocationCounter = 0;
53 * Context for shared random number generator
55 static SDLTest_RandomContext rndContext;
58 * Note: doxygen documentation markup for functions is in the header file.
62 SDLTest_FuzzerInit(Uint64 execKey)
64 Uint32 a = (execKey >> 32) & 0x00000000FFFFFFFF;
65 Uint32 b = execKey & 0x00000000FFFFFFFF;
66 SDL_memset((void *)&rndContext, 0, sizeof(SDLTest_RandomContext));
67 SDLTest_RandomInit(&rndContext, a, b);
68 fuzzerInvocationCounter = 0;
72 SDLTest_GetFuzzerInvocationCount()
74 return fuzzerInvocationCounter;
80 fuzzerInvocationCounter++;
82 return (Uint8) SDLTest_RandomInt(&rndContext) & 0x000000FF;
88 fuzzerInvocationCounter++;
90 return (Sint8) SDLTest_RandomInt(&rndContext) & 0x000000FF;
94 SDLTest_RandomUint16()
96 fuzzerInvocationCounter++;
98 return (Uint16) SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
102 SDLTest_RandomSint16()
104 fuzzerInvocationCounter++;
106 return (Sint16) SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
110 SDLTest_RandomSint32()
112 fuzzerInvocationCounter++;
114 return (Sint32) SDLTest_RandomInt(&rndContext);
118 SDLTest_RandomUint32()
120 fuzzerInvocationCounter++;
122 return (Uint32) SDLTest_RandomInt(&rndContext);
126 SDLTest_RandomUint64()
129 Uint32 *vp = (void *)&value;
131 fuzzerInvocationCounter++;
133 vp[0] = SDLTest_RandomSint32();
134 vp[1] = SDLTest_RandomSint32();
140 SDLTest_RandomSint64()
143 Uint32 *vp = (void *)&value;
145 fuzzerInvocationCounter++;
147 vp[0] = SDLTest_RandomSint32();
148 vp[1] = SDLTest_RandomSint32();
156 SDLTest_RandomIntegerInRange(Sint32 pMin, Sint32 pMax)
167 } else if(pMin == pMax) {
171 number = SDLTest_RandomUint32();
172 /* invocation count increment in preceeding call */
174 return (Sint32)((number % ((max + 1) - min)) + min);
178 * Generates a unsigned boundary value between the given boundaries.
179 * Boundary values are inclusive. See the examples below.
180 * If boundary2 < boundary1, the values are swapped.
181 * If boundary1 == boundary2, value of boundary1 will be returned
183 * Generating boundary values for Uint8:
184 * BoundaryValues(UINT8_MAX, 10, 20, True) -> [10,11,19,20]
185 * BoundaryValues(UINT8_MAX, 10, 20, False) -> [9,21]
186 * BoundaryValues(UINT8_MAX, 0, 15, True) -> [0, 1, 14, 15]
187 * BoundaryValues(UINT8_MAX, 0, 15, False) -> [16]
188 * BoundaryValues(UINT8_MAX, 0, 0xFF, False) -> [0], error set
190 * Generator works the same for other types of unsigned integers.
192 * \param maxValue The biggest value that is acceptable for this data type.
193 * For instance, for Uint8 -> 255, Uint16 -> 65536 etc.
194 * \param boundary1 defines lower boundary
195 * \param boundary2 defines upper boundary
196 * \param validDomain Generate only for valid domain (for the data type)
198 * \returns Returns a random boundary value for the domain or 0 in case of error
201 SDLTest_GenerateUnsignedBoundaryValues(const Uint64 maxValue, Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
209 if (boundary1 > boundary2) {
218 if (validDomain == SDL_TRUE) {
223 /* Generate up to 4 values within bounds */
227 tempBuf[index] = b1 + index;
229 } while (index < delta);
233 tempBuf[index] = b1 + 1;
235 tempBuf[index] = b2 - 1;
241 /* Generate up to 2 values outside of bounds */
243 tempBuf[index] = b1 - 1;
248 tempBuf[index] = b2 + 1;
254 /* There are no valid boundaries */
259 return tempBuf[SDLTest_RandomUint8() % index];
264 SDLTest_RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain)
266 /* max value for Uint8 */
267 const Uint64 maxValue = UCHAR_MAX;
268 return (Uint8)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
269 (Uint64) boundary1, (Uint64) boundary2,
274 SDLTest_RandomUint16BoundaryValue(Uint16 boundary1, Uint16 boundary2, SDL_bool validDomain)
276 /* max value for Uint16 */
277 const Uint64 maxValue = USHRT_MAX;
278 return (Uint16)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
279 (Uint64) boundary1, (Uint64) boundary2,
284 SDLTest_RandomUint32BoundaryValue(Uint32 boundary1, Uint32 boundary2, SDL_bool validDomain)
286 /* max value for Uint32 */
287 #if ((ULONG_MAX) == (UINT_MAX))
288 const Uint64 maxValue = ULONG_MAX;
290 const Uint64 maxValue = UINT_MAX;
292 return (Uint32)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
293 (Uint64) boundary1, (Uint64) boundary2,
298 SDLTest_RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
300 /* max value for Uint64 */
301 const Uint64 maxValue = ULLONG_MAX;
302 return SDLTest_GenerateUnsignedBoundaryValues(maxValue,
303 (Uint64) boundary1, (Uint64) boundary2,
308 * Generates a signed boundary value between the given boundaries.
309 * Boundary values are inclusive. See the examples below.
310 * If boundary2 < boundary1, the values are swapped.
311 * If boundary1 == boundary2, value of boundary1 will be returned
313 * Generating boundary values for Sint8:
314 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, True) -> [-10,-9,19,20]
315 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, False) -> [-11,21]
316 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -30, -15, True) -> [-30, -29, -16, -15]
317 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 15, False) -> [16]
318 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 127, False) -> [0], error set
320 * Generator works the same for other types of signed integers.
322 * \param minValue The smallest value that is acceptable for this data type.
323 * For instance, for Uint8 -> -127, etc.
324 * \param maxValue The biggest value that is acceptable for this data type.
325 * For instance, for Uint8 -> 127, etc.
326 * \param boundary1 defines lower boundary
327 * \param boundary2 defines upper boundary
328 * \param validDomain Generate only for valid domain (for the data type)
330 * \returns Returns a random boundary value for the domain or 0 in case of error
333 SDLTest_GenerateSignedBoundaryValues(const Sint64 minValue, const Sint64 maxValue, Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
341 if (boundary1 > boundary2) {
350 if (validDomain == SDL_TRUE) {
355 /* Generate up to 4 values within bounds */
359 tempBuf[index] = b1 + index;
361 } while (index < delta);
365 tempBuf[index] = b1 + 1;
367 tempBuf[index] = b2 - 1;
373 /* Generate up to 2 values outside of bounds */
375 tempBuf[index] = b1 - 1;
380 tempBuf[index] = b2 + 1;
386 /* There are no valid boundaries */
391 return tempBuf[SDLTest_RandomUint8() % index];
396 SDLTest_RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain)
398 /* min & max values for Sint8 */
399 const Sint64 maxValue = SCHAR_MAX;
400 const Sint64 minValue = SCHAR_MIN;
401 return (Sint8)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
402 (Sint64) boundary1, (Sint64) boundary2,
407 SDLTest_RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, SDL_bool validDomain)
409 /* min & max values for Sint16 */
410 const Sint64 maxValue = SHRT_MAX;
411 const Sint64 minValue = SHRT_MIN;
412 return (Sint16)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
413 (Sint64) boundary1, (Sint64) boundary2,
418 SDLTest_RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain)
420 /* min & max values for Sint32 */
421 #if ((ULONG_MAX) == (UINT_MAX))
422 const Sint64 maxValue = LONG_MAX;
423 const Sint64 minValue = LONG_MIN;
425 const Sint64 maxValue = INT_MAX;
426 const Sint64 minValue = INT_MIN;
428 return (Sint32)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
429 (Sint64) boundary1, (Sint64) boundary2,
434 SDLTest_RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
436 /* min & max values for Sint64 */
437 const Sint64 maxValue = LLONG_MAX;
438 const Sint64 minValue = LLONG_MIN;
439 return SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
440 boundary1, boundary2,
445 SDLTest_RandomUnitFloat()
447 return (float) SDLTest_RandomUint32() / UINT_MAX;
451 SDLTest_RandomFloat()
453 return (float) (SDLTest_RandomUnitDouble() * (double)2.0 * (double)FLT_MAX - (double)(FLT_MAX));
457 SDLTest_RandomUnitDouble()
459 return (double) (SDLTest_RandomUint64() >> 11) * (1.0/9007199254740992.0);
463 SDLTest_RandomDouble()
469 r += (double)SDLTest_RandomInt(&rndContext) * s;
470 } while (s > DBL_EPSILON);
472 fuzzerInvocationCounter++;
479 SDLTest_RandomAsciiString()
481 return SDLTest_RandomAsciiStringWithMaximumLength(255);
485 SDLTest_RandomAsciiStringWithMaximumLength(int maxLength)
490 SDL_InvalidParamError("maxLength");
494 size = (SDLTest_RandomUint32() % (maxLength + 1));
496 return SDLTest_RandomAsciiStringOfSize(size);
500 SDLTest_RandomAsciiStringOfSize(int size)
507 SDL_InvalidParamError("size");
511 string = (char *)SDL_malloc((size + 1) * sizeof(char));
516 for(counter = 0; counter < size; ++counter) {
517 string[counter] = (char)SDLTest_RandomIntegerInRange(32, 126);
520 string[counter] = '\0';
522 fuzzerInvocationCounter++;