This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
fuzzer.c
152 lines (114 loc) · 2.97 KB
1
2
3
4
5
6
7
#include "../SDL_test.h"
#include "fuzzer.h"
//! context for test-specific random number generator
8
static RND_CTX rndContext;
9
10
11
char *
GenerateExecKey(char *runSeed, char *suiteName,
12
13
14
15
char *testName, int iterationNumber)
{
if(runSeed == NULL || suiteName == NULL ||
testName == NULL || iterationNumber < 0) {
16
17
fprintf(stderr, "Error: Incorrect parameter given to GenerateExecKey function\n");
return NULL;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
}
char iterationString[256];
memset(iterationString, 0, sizeof(iterationString));
snprintf(iterationString, sizeof(iterationString), "%d", iterationNumber);
// combine the parameters
const int runSeedLength = strlen(runSeed);
const int suiteNameLength = strlen(suiteName);
const int testNameLength = strlen(testName);
const int iterationStringLength = strlen(iterationString);
// size of the entire + 3 for slashes and + 1 for '\0'
const int entireString = runSeedLength + suiteNameLength +
33
testNameLength + iterationStringLength + 3 + 1;
34
35
36
37
38
39
40
41
42
43
char *buffer = SDL_malloc(entireString);
if(!buffer) {
return NULL;
}
SDL_snprintf(buffer, entireString, "%s/%s/%s/%d", runSeed, suiteName,
testName, iterationNumber);
//printf("Debug: %s", buffer);
44
45
46
47
MD5_CTX md5Context;
utl_md5Init(&md5Context);
48
utl_md5Update(&md5Context, buffer, entireString);
49
50
utl_md5Final(&md5Context);
51
SDL_free(buffer);
52
53
54
55
56
57
const int keyLength = SDL_strlen(md5Context.digest);
char *key = SDL_malloc(keyLength);
SDL_snprintf(key, keyLength, "%s", md5Context.digest);
return key;
58
59
60
}
void
61
InitFuzzer(char *execKey)
62
{
63
64
65
66
67
//int a = execKey[8,9,10,11];
int a = execKey[8] | execKey[9] | execKey[10] | execKey[11];
int b = execKey[12] | execKey[13] | execKey[14] | execKey[15];
utl_randomInit(&rndContext, a, b);
68
69
70
71
72
73
74
75
76
77
78
}
void
DeinitFuzzer()
{
}
int
RandomInteger()
{
79
return utl_randomInt(&rndContext);
80
81
82
}
int
83
RandomPositiveInteger()
84
{
85
return abs(utl_randomInt(&rndContext));
86
87
88
89
90
91
92
93
94
}
int
RandomIntegerInRange(int min, int max)
{
if(min > max || (min - max) == 0) {
return -1; // Doesn't really make sense to return -1 on error?
}
95
int number = utl_randomInt(&rndContext);
96
97
number = abs(number);
98
return (number % ((max + 1) - min)) + min;
99
100
101
}
int
102
GenerateBoundaryValueForSize(const int size)
103
{
104
105
106
if(size < 0) {
return -1;
}
107
108
109
const int adjustment = RandomIntegerInRange(-1, 1);
int retValue = (1 << (RandomPositiveInteger() % size)) + adjustment;
110
111
112
113
return retValue;
}
114
115
116
117
118
119
120
121
122
123
124
125
126
int
RandomUint8BoundaryValue()
{
return GenerateBoundaryValueForSize(8);
}
int
RandomInt8BoundaryValue()
{
int value = GenerateBoundaryValueForSize(8);
return (RandomPositiveInteger() % 2 == 0 ? value : -value);
}
127
128
129
130
char *
RandomAsciiString()
{
131
132
133
134
135
136
137
138
139
140
return RandomAsciiStringWithMaximumLength(255);
}
char *
RandomAsciiStringWithMaximumLength(int maxSize)
{
if(maxSize < 0) {
return NULL;
}
141
int size = abs(RandomInteger) % maxSize;
142
143
144
145
char *string = SDL_malloc(size * sizeof(size));
int counter = 0;
for( ; counter < size; ++counter) {
146
string[counter] = (char) RandomIntegerInRange(1, 127);
147
148
}
149
150
string[counter] = '\0';
151
152
return string;
}