1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
Copyright (C) 2011 Markus Kauppila <markus.kauppila@gmail.com>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include <stdio.h>
#include <stdlib.h>
#include "../../../include/SDL_test.h"
#include "fuzzer.h"
//! context for test-specific random number generator
static RND_CTX rndContext;
Uint64
GenerateExecKey(char *runSeed, char *suiteName,
char *testName, int iterationNumber)
{
if(runSeed == NULL) {
fprintf(stderr, "Error: Incorrect runSeed given to GenerateExecKey function\n");
return -1;
}
if(suiteName == NULL) {
fprintf(stderr, "Error: Incorrect suiteName given to GenerateExecKey function\n");
return -1;
}
if(testName == NULL) {
fprintf(stderr, "Error: Incorrect testName given to GenerateExecKey function\n");
return -1;
}
if(iterationNumber < 0) {
fprintf(stderr, "Error: Incorrect iteration number given to GenerateExecKey function\n");
return -1;
}
// Change to itoa
char iterationString[16];
memset(iterationString, 0, sizeof(iterationString));
SDL_snprintf(iterationString, sizeof(iterationString) - 1, "%d", iterationNumber);
// combine the parameters
const Uint32 runSeedLength = strlen(runSeed);
const Uint32 suiteNameLength = strlen(suiteName);
const Uint32 testNameLength = strlen(testName);
const Uint32 iterationStringLength = strlen(iterationString);
// size of the entire + 3 for slashes and + 1 for '\0'
const Uint32 entireString = runSeedLength + suiteNameLength +
testNameLength + iterationStringLength + 3 + 1;
char *buffer = SDL_malloc(entireString);
if(!buffer) {
return -1;
}
SDL_snprintf(buffer, entireString, "%s/%s/%s/%d", runSeed, suiteName,
testName, iterationNumber);
MD5_CTX md5Context;
utl_md5Init(&md5Context);
utl_md5Update(&md5Context, buffer, entireString);
utl_md5Final(&md5Context);
SDL_free(buffer);
const char *execKey = md5Context.digest;
//printf("Debug: digest = %s\n", execKey);
// Casting fixes compiler warnings
Uint64 key = ((Uint64) execKey[8]) << 56 |
((Uint64) execKey[9]) << 48 |
((Uint64) execKey[10]) << 40 |
((Uint64) execKey[11]) << 32 |
((Uint64) execKey[12]) << 24 |
((Uint64) execKey[13]) << 16 |
((Uint64) execKey[14]) << 8 |
((Uint64) execKey[15]) << 0;
return key;
}
void
InitFuzzer(Uint64 execKey)
{
Uint32 a = (execKey >> 32) & 0x00000000FFFFFFFF;
Uint32 b = execKey & 0x00000000FFFFFFFF;
//printf("Debug: execKey: %llx\n", execKey);
//printf("Debug: a = %x - b = %x\n", a, b);
utl_randomInit(&rndContext, a, b);
}
void
DeinitFuzzer()
{
}
Sint32
RandomInteger()
{
return utl_randomInt(&rndContext);
}
Uint32
RandomPositiveInteger()
{
return utl_randomInt(&rndContext);
}
Sint32
RandomIntegerInRange(Sint32 pMin, Sint32 pMax)
{
Sint64 min = (Sint64) pMin, max = (Sint64) pMax;
if(min > max) {
Sint64 temp = min;
min = max;
max = temp;
} else if(min == max) {
return min;
}
Sint32 number = abs(utl_randomInt(&rndContext));
return (number % ((max + 1) - min)) + min;
}
/*!
* Generates boundary values 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
*
* Generator works the same for other types of unsigned integers.
*
* \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 validDomain Generate only for valid domain (for the data type)
*
* \param outBuffer The generated boundary values are put here
* \param outBufferSize Size of outBuffer
*
* \returns NULL on error, outBuffer on success
*/
Uint64 *
GenerateUnsignedBoundaryValues(const Uint64 maxValue,
Uint64 pBoundary1, Uint64 pBoundary2, SDL_bool validDomain,
Uint64 *outBuffer, Uint32 *outBufferSize)
{
Uint64 boundary1 = pBoundary1, boundary2 = pBoundary2;
if(outBuffer != NULL) {
SDL_free(outBuffer);
}
if(boundary1 > boundary2) {
Uint64 temp = boundary1;
boundary1 = boundary2;
boundary2 = temp;
}
Uint64 tempBuf[8];
memset(tempBuf, 0, 8 * sizeof(Uint64));
Uint64 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(boundary2 < maxValue && boundary2 < UINT64_MAX) {
tempBuf[index++] = boundary2 + 1;
}
}
if(index == 0) {
// There are no valid boundaries
return NULL;
}
// Create the return buffer
outBuffer = SDL_malloc(index * sizeof(Uint64));
if(outBuffer == NULL) {
return NULL;
}
SDL_memcpy(outBuffer, tempBuf, index * sizeof(Uint64));
*outBufferSize = index;
return outBuffer;
}
Uint8
RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain)
{
Uint64 *buffer = NULL;
Uint32 size;
// max value for Uint8
const Uint64 maxValue = UINT8_MAX;
buffer = GenerateUnsignedBoundaryValues(maxValue,
(Uint64) boundary1, (Uint64) boundary2,
validDomain, buffer, &size);
if(buffer == NULL) {
return -1; // Change to some better error value? What would be better?
}
Uint32 index = RandomInteger() % size;
Uint8 retVal = (Uint8) buffer[index];
SDL_free(buffer);
return retVal;
}
Uint16
RandomUint16BoundaryValue(Uint16 boundary1, Uint16 boundary2, SDL_bool validDomain)
{
Uint64 *buffer = NULL;
Uint32 size;
// max value for Uint16
const Uint64 maxValue = UINT16_MAX;
buffer = GenerateUnsignedBoundaryValues(maxValue,
(Uint64) boundary1, (Uint64) boundary2,
validDomain, buffer, &size);
if(buffer == NULL) {
return -1; // Change to some better error value? What would be better?
}
Uint32 index = RandomInteger() % size;
Uint16 retVal = (Uint16) buffer[index];
SDL_free(buffer);
return retVal;
}
Uint32
RandomUint32BoundaryValue(Uint32 boundary1, Uint32 boundary2, SDL_bool validDomain)
{
Uint64 *buffer = NULL;
Uint32 size;
// max value for Uint32
const Uint64 maxValue = UINT32_MAX;
buffer = GenerateUnsignedBoundaryValues(maxValue,
(Uint64) boundary1, (Uint64) boundary2,
validDomain, buffer, &size);
if(buffer == NULL) {
return -1; // Change to some better error value? What would be better?
}
Uint32 index = RandomInteger() % size;
Uint32 retVal = (Uint32) buffer[index];
SDL_free(buffer);
return retVal;
}
Uint64
RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
{
Uint64 *buffer = NULL;
Uint32 size;
// max value for Uint64
const Uint64 maxValue = UINT64_MAX;
buffer = GenerateUnsignedBoundaryValues(maxValue,
(Uint64) boundary1, (Uint64) boundary2,
validDomain, buffer, &size);
if(buffer == NULL) {
return -1; // Change to some better error value? What would be better?
}
Uint32 index = RandomInteger() % size;
Uint64 retVal = (Uint64) buffer[index];
SDL_free(buffer);
return retVal;
}
Sint8
RandomSint8BoundaryValue()
{
int value = 0; //GenerateBoundaryValueForSize(8);
return (RandomPositiveInteger() % 2 == 0 ? value : -value);
}
char *
RandomAsciiString()
{
return RandomAsciiStringWithMaximumLength(255);
}
char *
RandomAsciiStringWithMaximumLength(int maxSize)
{
if(maxSize < 0) {
return NULL;
}
int size = abs(RandomInteger()) % maxSize;
char *string = SDL_malloc(size * sizeof(size));
int counter = 0;
for( ; counter < size; ++counter) {
string[counter] = (char) RandomIntegerInRange(1, 127);
}
string[counter] = '\0';
return string;
}