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"
31 /* Visual Studio 2008 doesn't have stdint.h */
32 #if defined(_MSC_VER) && _MSC_VER <= 1500
33 #define UINT8_MAX _UI8_MAX
34 #define UINT16_MAX _UI16_MAX
35 #define UINT32_MAX _UI32_MAX
36 #define INT64_MIN _I64_MIN
37 #define INT64_MAX _I64_MAX
38 #define UINT64_MAX _UI64_MAX
49 * Counter for fuzzer invocations
51 static int fuzzerInvocationCounter = 0;
54 * Context for shared random number generator
56 static SDLTest_RandomContext rndContext;
59 * Note: doxygen documentation markup for functions is in the header file.
63 SDLTest_FuzzerInit(Uint64 execKey)
65 Uint32 a = (execKey >> 32) & 0x00000000FFFFFFFF;
66 Uint32 b = execKey & 0x00000000FFFFFFFF;
67 SDL_memset((void *)&rndContext, 0, sizeof(SDLTest_RandomContext));
68 SDLTest_RandomInit(&rndContext, a, b);
69 fuzzerInvocationCounter = 0;
73 SDLTest_GetFuzzerInvocationCount()
75 return fuzzerInvocationCounter;
81 fuzzerInvocationCounter++;
83 return (Uint8) SDLTest_RandomInt(&rndContext) & 0x000000FF;
89 fuzzerInvocationCounter++;
91 return (Sint8) SDLTest_RandomInt(&rndContext) & 0x000000FF;
95 SDLTest_RandomUint16()
97 fuzzerInvocationCounter++;
99 return (Uint16) SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
103 SDLTest_RandomSint16()
105 fuzzerInvocationCounter++;
107 return (Sint16) SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
111 SDLTest_RandomSint32()
113 fuzzerInvocationCounter++;
115 return (Sint32) SDLTest_RandomInt(&rndContext);
119 SDLTest_RandomUint32()
121 fuzzerInvocationCounter++;
123 return (Uint32) SDLTest_RandomInt(&rndContext);
127 SDLTest_RandomUint64()
130 Uint32 *vp = (void *)&value;
132 fuzzerInvocationCounter++;
134 vp[0] = SDLTest_RandomSint32();
135 vp[1] = SDLTest_RandomSint32();
141 SDLTest_RandomSint64()
144 Uint32 *vp = (void *)&value;
146 fuzzerInvocationCounter++;
148 vp[0] = SDLTest_RandomSint32();
149 vp[1] = SDLTest_RandomSint32();
157 SDLTest_RandomIntegerInRange(Sint32 pMin, Sint32 pMax)
168 } else if(pMin == pMax) {
172 number = SDLTest_RandomUint32();
173 /* invocation count increment in preceeding call */
175 return (Sint32)((number % ((max + 1) - min)) + min);
179 * Generates a unsigned boundary value between the given boundaries.
180 * Boundary values are inclusive. See the examples below.
181 * If boundary2 < boundary1, the values are swapped.
182 * If boundary1 == boundary2, value of boundary1 will be returned
184 * Generating boundary values for Uint8:
185 * BoundaryValues(UINT8_MAX, 10, 20, True) -> [10,11,19,20]
186 * BoundaryValues(UINT8_MAX, 10, 20, False) -> [9,21]
187 * BoundaryValues(UINT8_MAX, 0, 15, True) -> [0, 1, 14, 15]
188 * BoundaryValues(UINT8_MAX, 0, 15, False) -> [16]
189 * BoundaryValues(UINT8_MAX, 0, 0xFF, False) -> [0], error set
191 * Generator works the same for other types of unsigned integers.
193 * \param maxValue The biggest value that is acceptable for this data type.
194 * For instance, for Uint8 -> 255, Uint16 -> 65536 etc.
195 * \param boundary1 defines lower boundary
196 * \param boundary2 defines upper boundary
197 * \param validDomain Generate only for valid domain (for the data type)
199 * \returns Returns a random boundary value for the domain or 0 in case of error
202 SDLTest_GenerateUnsignedBoundaryValues(const Uint64 maxValue, Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
210 if (boundary1 > boundary2) {
219 if (validDomain == SDL_TRUE) {
224 /* Generate up to 4 values within bounds */
228 tempBuf[index] = b1 + index;
230 } while (index < delta);
234 tempBuf[index] = b1 + 1;
236 tempBuf[index] = b2 - 1;
242 /* Generate up to 2 values outside of bounds */
244 tempBuf[index] = b1 - 1;
249 tempBuf[index] = b2 + 1;
255 /* There are no valid boundaries */
260 return tempBuf[SDLTest_RandomUint8() % index];
265 SDLTest_RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain)
267 /* max value for Uint8 */
268 const Uint64 maxValue = UCHAR_MAX;
269 return (Uint8)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
270 (Uint64) boundary1, (Uint64) boundary2,
275 SDLTest_RandomUint16BoundaryValue(Uint16 boundary1, Uint16 boundary2, SDL_bool validDomain)
277 /* max value for Uint16 */
278 const Uint64 maxValue = USHRT_MAX;
279 return (Uint16)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
280 (Uint64) boundary1, (Uint64) boundary2,
285 SDLTest_RandomUint32BoundaryValue(Uint32 boundary1, Uint32 boundary2, SDL_bool validDomain)
287 /* max value for Uint32 */
288 #if ((ULONG_MAX) == (UINT_MAX))
289 const Uint64 maxValue = ULONG_MAX;
291 const Uint64 maxValue = UINT_MAX;
293 return (Uint32)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
294 (Uint64) boundary1, (Uint64) boundary2,
299 SDLTest_RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
301 /* max value for Uint64 */
302 const Uint64 maxValue = UINT64_MAX;
303 return SDLTest_GenerateUnsignedBoundaryValues(maxValue,
304 (Uint64) boundary1, (Uint64) boundary2,
309 * Generates a signed boundary value between the given boundaries.
310 * Boundary values are inclusive. See the examples below.
311 * If boundary2 < boundary1, the values are swapped.
312 * If boundary1 == boundary2, value of boundary1 will be returned
314 * Generating boundary values for Sint8:
315 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, True) -> [-10,-9,19,20]
316 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, False) -> [-11,21]
317 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -30, -15, True) -> [-30, -29, -16, -15]
318 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 15, False) -> [16]
319 * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 127, False) -> [0], error set
321 * Generator works the same for other types of signed integers.
323 * \param minValue The smallest value that is acceptable for this data type.
324 * For instance, for Uint8 -> -127, etc.
325 * \param maxValue The biggest value that is acceptable for this data type.
326 * For instance, for Uint8 -> 127, etc.
327 * \param boundary1 defines lower boundary
328 * \param boundary2 defines upper boundary
329 * \param validDomain Generate only for valid domain (for the data type)
331 * \returns Returns a random boundary value for the domain or 0 in case of error
334 SDLTest_GenerateSignedBoundaryValues(const Sint64 minValue, const Sint64 maxValue, Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
342 if (boundary1 > boundary2) {
351 if (validDomain == SDL_TRUE) {
356 /* Generate up to 4 values within bounds */
360 tempBuf[index] = b1 + index;
362 } while (index < delta);
366 tempBuf[index] = b1 + 1;
368 tempBuf[index] = b2 - 1;
374 /* Generate up to 2 values outside of bounds */
376 tempBuf[index] = b1 - 1;
381 tempBuf[index] = b2 + 1;
387 /* There are no valid boundaries */
392 return tempBuf[SDLTest_RandomUint8() % index];
397 SDLTest_RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain)
399 /* min & max values for Sint8 */
400 const Sint64 maxValue = SCHAR_MAX;
401 const Sint64 minValue = SCHAR_MIN;
402 return (Sint8)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
403 (Sint64) boundary1, (Sint64) boundary2,
408 SDLTest_RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, SDL_bool validDomain)
410 /* min & max values for Sint16 */
411 const Sint64 maxValue = SHRT_MAX;
412 const Sint64 minValue = SHRT_MIN;
413 return (Sint16)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
414 (Sint64) boundary1, (Sint64) boundary2,
419 SDLTest_RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain)
421 /* min & max values for Sint32 */
422 #if ((ULONG_MAX) == (UINT_MAX))
423 const Sint64 maxValue = LONG_MAX;
424 const Sint64 minValue = LONG_MIN;
426 const Sint64 maxValue = INT_MAX;
427 const Sint64 minValue = INT_MIN;
429 return (Sint32)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
430 (Sint64) boundary1, (Sint64) boundary2,
435 SDLTest_RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
437 /* min & max values for Sint64 */
438 const Sint64 maxValue = INT64_MAX;
439 const Sint64 minValue = INT64_MIN;
440 return SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
441 boundary1, boundary2,
446 SDLTest_RandomUnitFloat()
448 return SDLTest_RandomUint32() / (float) UINT_MAX;
452 SDLTest_RandomFloat()
454 return (float) (SDLTest_RandomUnitDouble() * (double)2.0 * (double)FLT_MAX - (double)(FLT_MAX));
458 SDLTest_RandomUnitDouble()
460 return (double) (SDLTest_RandomUint64() >> 11) * (1.0/9007199254740992.0);
464 SDLTest_RandomDouble()
470 r += (double)SDLTest_RandomInt(&rndContext) * s;
471 } while (s > DBL_EPSILON);
473 fuzzerInvocationCounter++;
480 SDLTest_RandomAsciiString()
482 return SDLTest_RandomAsciiStringWithMaximumLength(255);
486 SDLTest_RandomAsciiStringWithMaximumLength(int maxLength)
491 SDL_InvalidParamError("maxLength");
495 size = (SDLTest_RandomUint32() % (maxLength + 1));
497 return SDLTest_RandomAsciiStringOfSize(size);
501 SDLTest_RandomAsciiStringOfSize(int size)
508 SDL_InvalidParamError("size");
512 string = (char *)SDL_malloc((size + 1) * sizeof(char));
517 for(counter = 0; counter < size; ++counter) {
518 string[counter] = (char)SDLTest_RandomIntegerInRange(32, 126);
521 string[counter] = '\0';
523 fuzzerInvocationCounter++;