Fix fuzzer random boundary functions; add tests for fuzzer

This commit is contained in:
Andreas Schiffler 2013-02-12 22:23:42 -08:00
parent e34e953eba
commit ea312e87fa
4 changed files with 1096 additions and 343 deletions

View file

@ -44,7 +44,7 @@
#include "SDL_test.h"
/**
*Counter for fuzzer invocations
* Counter for fuzzer invocations
*/
static int fuzzerInvocationCounter = 0;
@ -167,414 +167,268 @@ SDLTest_RandomIntegerInRange(Sint32 pMin, Sint32 pMax)
return (Sint32)min;
}
number = SDLTest_RandomUint32(); // invocation count increment in there
number = SDLTest_RandomUint32();
/* invocation count increment in preceeding call */
return (Sint32)((number % ((max + 1) - min)) + min);
}
/*!
* Generates boundary values between the given boundaries.
* Generates a unsigned boundary value between the given boundaries.
* Boundary values are inclusive. See the examples below.
* If boundary2 < boundary1, the values are swapped.
* If boundary1 == boundary2, value of boundary1 will be returned
*
* Generating boundary values for Uint8:
* BoundaryValues(sizeof(Uint8), 10, 20, True) -> [10,11,19,20]
* BoundaryValues(sizeof(Uint8), 10, 20, False) -> [9,21]
* BoundaryValues(sizeof(Uint8), 0, 15, True) -> [0, 1, 14, 15]
* BoundaryValues(sizeof(Uint8), 0, 15, False) -> [16]
* BoundaryValues(sizeof(Uint8), 0, 255, False) -> NULL
* BoundaryValues(UINT8_MAX, 10, 20, True) -> [10,11,19,20]
* BoundaryValues(UINT8_MAX, 10, 20, False) -> [9,21]
* BoundaryValues(UINT8_MAX, 0, 15, True) -> [0, 1, 14, 15]
* BoundaryValues(UINT8_MAX, 0, 15, False) -> [16]
* BoundaryValues(UINT8_MAX, 0, 0xFF, False) -> [0], error set
*
* Generator works the same for other types of unsigned integers.
*
* Note: outBuffer will be allocated and needs to be freed later.
* If outbuffer != NULL, it'll be freed.
*
* \param maxValue The biggest value that is acceptable for this data type.
* For instance, for Uint8 -> 255, Uint16 -> 65536 etc.
* \param pBoundary1 defines lower boundary
* \param pBoundary2 defines upper boundary
* \param boundary1 defines lower boundary
* \param boundary2 defines upper boundary
* \param validDomain Generate only for valid domain (for the data type)
*
* \param outBuffer The generated boundary values are put here
*
* \returns Returns the number of elements in outBuffer or -1 in case of error
* \returns Returns a random boundary value for the domain or 0 in case of error
*/
Uint32
SDLTest_GenerateUnsignedBoundaryValues(const Uint64 maxValue,
Uint64 pBoundary1, Uint64 pBoundary2, SDL_bool validDomain,
Uint64 *outBuffer)
Uint64
SDLTest_GenerateUnsignedBoundaryValues(const Uint64 maxValue, Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
{
Uint64 boundary1 = pBoundary1, boundary2 = pBoundary2;
Uint64 temp;
Uint64 b1, b2;
Uint64 delta;
Uint64 tempBuf[4];
int index;
if(outBuffer != NULL) {
SDL_free(outBuffer);
}
if(boundary1 > boundary2) {
temp = boundary1;
boundary1 = boundary2;
boundary2 = temp;
}
Uint8 index;
/* Maybe swap */
if (boundary1 > boundary2) {
b1 = boundary2;
b2 = boundary1;
} else {
b1 = boundary1;
b2 = boundary2;
}
index = 0;
if(boundary1 == boundary2) {
tempBuf[index++] = boundary1;
}
else if(validDomain) {
tempBuf[index++] = boundary1;
if(boundary1 < UINT64_MAX)
tempBuf[index++] = boundary1 + 1;
tempBuf[index++] = boundary2 - 1;
tempBuf[index++] = boundary2;
}
else {
if(boundary1 > 0) {
tempBuf[index++] = boundary1 - 1;
if (validDomain == SDL_TRUE) {
if (b1 == b2) {
return b1;
}
/* Generate up to 4 values within bounds */
delta = b2 - b1;
if (delta < 4) {
do {
tempBuf[index] = b1 + index;
index++;
} while (index < delta);
} else {
tempBuf[index] = b1;
index++;
tempBuf[index] = b1 + 1;
index++;
tempBuf[index] = b2 - 1;
index++;
tempBuf[index] = b2;
index++;
}
} else {
/* Generate up to 2 values outside of bounds */
if (b1 > 0) {
tempBuf[index] = b1 - 1;
index++;
}
if(boundary2 < maxValue && boundary2 < UINT64_MAX) {
tempBuf[index++] = boundary2 + 1;
if (b2 < maxValue) {
tempBuf[index] = b2 + 1;
index++;
}
}
if(index == 0) {
// There are no valid boundaries
if (index == 0) {
/* There are no valid boundaries */
SDL_Error(SDL_UNSUPPORTED);
return 0;
}
// Create the return buffer
outBuffer = (Uint64 *)SDL_malloc(index * sizeof(Uint64));
if(outBuffer == NULL) {
return 0;
}
SDL_memcpy(outBuffer, tempBuf, index * sizeof(Uint64));
return index;
return tempBuf[SDLTest_RandomUint8() % index];
}
Uint8
SDLTest_RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain)
{
Uint64 *buffer = NULL;
Uint32 size;
Uint32 index;
Uint8 retVal;
// max value for Uint8
const Uint64 maxValue = UINT8_MAX;
size = SDLTest_GenerateUnsignedBoundaryValues(maxValue,
/* max value for Uint8 */
const Uint64 maxValue = UCHAR_MAX;
return (Uint8)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
(Uint64) boundary1, (Uint64) boundary2,
validDomain, buffer);
if (buffer == NULL || size == 0) {
return 0;
}
index = SDLTest_RandomSint32() % size;
retVal = (Uint8)buffer[index];
SDL_free(buffer);
fuzzerInvocationCounter++;
return retVal;
validDomain);
}
Uint16
SDLTest_RandomUint16BoundaryValue(Uint16 boundary1, Uint16 boundary2, SDL_bool validDomain)
{
Uint64 *buffer = NULL;
Uint32 size;
Uint32 index;
Uint16 retVal;
// max value for Uint16
const Uint64 maxValue = UINT16_MAX;
size = SDLTest_GenerateUnsignedBoundaryValues(maxValue,
/* max value for Uint16 */
const Uint64 maxValue = USHRT_MAX;
return (Uint16)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
(Uint64) boundary1, (Uint64) boundary2,
validDomain, buffer);
if (buffer == NULL || size == 0) {
return 0;
}
index = SDLTest_RandomSint32() % size;
retVal = (Uint16) buffer[index];
SDL_free(buffer);
fuzzerInvocationCounter++;
return retVal;
validDomain);
}
Uint32
SDLTest_RandomUint32BoundaryValue(Uint32 boundary1, Uint32 boundary2, SDL_bool validDomain)
{
Uint64 *buffer = NULL;
Uint32 size;
Uint32 index;
Uint32 retVal;
// max value for Uint32
const Uint64 maxValue = UINT32_MAX;
size = SDLTest_GenerateUnsignedBoundaryValues(maxValue,
/* max value for Uint32 */
const Uint64 maxValue = ULONG_MAX;
return (Uint32)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
(Uint64) boundary1, (Uint64) boundary2,
validDomain, buffer);
if (buffer == NULL || size == 0) {
return 0;
}
index = SDLTest_RandomSint32() % size;
retVal = (Uint32) buffer[index];
SDL_free(buffer);
fuzzerInvocationCounter++;
return retVal;
validDomain);
}
Uint64
SDLTest_RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
{
Uint64 *buffer = NULL;
Uint32 size;
Uint32 index;
Uint64 retVal;
// max value for Uint64
const Uint64 maxValue = UINT64_MAX;
size = SDLTest_GenerateUnsignedBoundaryValues(maxValue,
/* max value for Uint64 */
const Uint64 maxValue = ULLONG_MAX;
return SDLTest_GenerateUnsignedBoundaryValues(maxValue,
(Uint64) boundary1, (Uint64) boundary2,
validDomain, buffer);
if (buffer == NULL || size == 0) {
return 0;
}
index = SDLTest_RandomSint32() % size;
retVal = (Uint64) buffer[index];
SDL_free(buffer);
fuzzerInvocationCounter++;
return retVal;
validDomain);
}
/*!
* Generates boundary values between the given boundaries.
* Generates a signed boundary value between the given boundaries.
* Boundary values are inclusive. See the examples below.
* If boundary2 < boundary1, the values are swapped.
* If boundary1 == boundary2, value of boundary1 will be returned
*
* Generating boundary values for Sint8:
* SignedBoundaryValues(sizeof(Sint8), -10, 20, True) -> [-11,-10,19,20]
* SignedBoundaryValues(sizeof(Sint8), -10, 20, False) -> [-11,21]
* SignedBoundaryValues(sizeof(Sint8), -30, -15, True) -> [-30, -29, -16, -15]
* SignedBoundaryValues(sizeof(Sint8), -128, 15, False) -> [16]
* SignedBoundaryValues(sizeof(Sint8), -128, 127, False) -> NULL
* SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, True) -> [-10,-9,19,20]
* SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, False) -> [-11,21]
* SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -30, -15, True) -> [-30, -29, -16, -15]
* SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 15, False) -> [16]
* SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 127, False) -> [0], error set
*
* Generator works the same for other types of signed integers.
*
* Note: outBuffer will be allocated and needs to be freed later.
* If outbuffer != NULL, it'll be freed.
*
*
* \param minValue The smallest value that is acceptable for this data type.
* For instance, for Uint8 -> -128, Uint16 -> -32,768 etc.
* \param minValue The smallest value that is acceptable for this data type.
* For instance, for Uint8 -> -127, etc.
* \param maxValue The biggest value that is acceptable for this data type.
* For instance, for Uint8 -> 127, Uint16 -> 32767 etc.
* \param pBoundary1 defines lower boundary
* \param pBoundary2 defines upper boundary
* For instance, for Uint8 -> 127, etc.
* \param boundary1 defines lower boundary
* \param boundary2 defines upper boundary
* \param validDomain Generate only for valid domain (for the data type)
*
* \param outBuffer The generated boundary values are put here
*
* \returns Returns the number of elements in outBuffer or -1 in case of error
* \returns Returns a random boundary value for the domain or 0 in case of error
*/
Uint32
SDLTest_GenerateSignedBoundaryValues(const Sint64 minValue, const Sint64 maxValue,
Sint64 pBoundary1, Sint64 pBoundary2, SDL_bool validDomain,
Sint64 *outBuffer)
Sint64
SDLTest_GenerateSignedBoundaryValues(const Sint64 minValue, const Sint64 maxValue, Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
{
int index;
Sint64 b1, b2;
Sint64 delta;
Sint64 tempBuf[4];
Sint64 boundary1 = pBoundary1, boundary2 = pBoundary2;
if(outBuffer != NULL) {
SDL_free(outBuffer);
}
if(boundary1 > boundary2) {
Sint64 temp = boundary1;
boundary1 = boundary2;
boundary2 = temp;
}
Uint8 index;
/* Maybe swap */
if (boundary1 > boundary2) {
b1 = boundary2;
b2 = boundary1;
} else {
b1 = boundary1;
b2 = boundary2;
}
index = 0;
if(boundary1 == boundary2) {
tempBuf[index++] = boundary1;
}
else if(validDomain) {
tempBuf[index++] = boundary1;
if(boundary1 < LLONG_MAX)
tempBuf[index++] = boundary1 + 1;
if(boundary2 > LLONG_MIN)
tempBuf[index++] = boundary2 - 1;
tempBuf[index++] = boundary2;
}
else {
if(boundary1 > minValue && boundary1 > LLONG_MIN) {
tempBuf[index++] = boundary1 - 1;
if (validDomain == SDL_TRUE) {
if (b1 == b2) {
return b1;
}
/* Generate up to 4 values within bounds */
delta = b2 - b1;
if (delta < 4) {
do {
tempBuf[index] = b1 + index;
index++;
} while (index < delta);
} else {
tempBuf[index] = b1;
index++;
tempBuf[index] = b1 + 1;
index++;
tempBuf[index] = b2 - 1;
index++;
tempBuf[index] = b2;
index++;
}
} else {
/* Generate up to 2 values outside of bounds */
if (b1 > minValue) {
tempBuf[index] = b1 - 1;
index++;
}
if(boundary2 < maxValue && boundary2 < UINT64_MAX) {
tempBuf[index++] = boundary2 + 1;
if (b2 < maxValue) {
tempBuf[index] = b2 + 1;
index++;
}
}
if(index == 0) {
// There are no valid boundaries
return 0;
if (index == 0) {
/* There are no valid boundaries */
SDL_Error(SDL_UNSUPPORTED);
return minValue;
}
// Create the return buffer
outBuffer = (Sint64 *)SDL_malloc(index * sizeof(Sint64));
if(outBuffer == NULL) {
return 0;
}
SDL_memcpy((void *)outBuffer, (void *)tempBuf, index * sizeof(Sint64));
return (Uint32)index;
return tempBuf[SDLTest_RandomUint8() % index];
}
Sint8
SDLTest_RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain)
{
// min & max values for Sint8
const Sint64 maxValue = CHAR_MAX;
const Sint64 minValue = CHAR_MIN;
Sint64 *buffer = NULL;
Uint32 size;
Uint32 index;
Sint8 retVal;
size = SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
/* min & max values for Sint8 */
const Sint64 maxValue = SCHAR_MAX;
const Sint64 minValue = SCHAR_MIN;
return (Sint8)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
(Sint64) boundary1, (Sint64) boundary2,
validDomain, buffer);
if (buffer == NULL || size == 0) {
return CHAR_MIN;
}
index = SDLTest_RandomSint32() % size;
retVal = (Sint8) buffer[index];
SDL_free(buffer);
fuzzerInvocationCounter++;
return retVal;
validDomain);
}
Sint16
SDLTest_RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, SDL_bool validDomain)
{
// min & max values for Sint16
/* min & max values for Sint16 */
const Sint64 maxValue = SHRT_MAX;
const Sint64 minValue = SHRT_MIN;
Sint64 *buffer = NULL;
Uint32 size;
Uint32 index;
Sint16 retVal;
size = SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
(Sint64) boundary1, (Sint64) boundary2,
validDomain, buffer);
if (buffer == NULL || size == 0) {
return SHRT_MIN;
}
index = SDLTest_RandomSint32() % size;
retVal = (Sint16) buffer[index];
SDL_free(buffer);
fuzzerInvocationCounter++;
return retVal;
return (Sint16)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
(Sint64) boundary1, (Sint64) boundary2,
validDomain);
}
Sint32
SDLTest_RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain)
{
// min & max values for Sint32
const Sint64 maxValue = INT_MAX;
const Sint64 minValue = INT_MIN;
Sint64 *buffer = NULL;
Uint32 size;
Uint32 index;
Sint32 retVal;
size = SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
/* min & max values for Sint32 */
const Sint64 maxValue = LONG_MAX;
const Sint64 minValue = LONG_MIN;
return (Sint32)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
(Sint64) boundary1, (Sint64) boundary2,
validDomain, buffer);
if (buffer == NULL || size == 0) {
return INT_MIN;
}
index = SDLTest_RandomSint32() % size;
retVal = (Sint32) buffer[index];
SDL_free(buffer);
fuzzerInvocationCounter++;
return retVal;
validDomain);
}
Sint64
SDLTest_RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
{
Sint64 *buffer = NULL;
Uint32 size;
Uint32 index;
Sint64 retVal;
// min & max values for Sint64
/* min & max values for Sint64 */
const Sint64 maxValue = LLONG_MAX;
const Sint64 minValue = LLONG_MIN;
size = SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
(Sint64) boundary1, (Sint64) boundary2,
validDomain, buffer);
if (buffer == NULL || size == 0) {
return LLONG_MIN;
}
index = SDLTest_RandomSint32() % size;
retVal = (Sint64) buffer[index];
SDL_free(buffer);
fuzzerInvocationCounter++;
return retVal;
return SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
boundary1, boundary2,
validDomain);
}
float