SDL_test_fuzzer.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /*
  19. Data generators for fuzzing test data in a reproducible way.
  20. */
  21. #include "SDL_config.h"
  22. /* Visual Studio 2008 doesn't have stdint.h */
  23. #if defined(_MSC_VER) && _MSC_VER <= 1500
  24. #define UINT8_MAX ~(Uint8)0
  25. #define UINT16_MAX ~(Uint16)0
  26. #define UINT32_MAX ~(Uint32)0
  27. #define UINT64_MAX ~(Uint64)0
  28. #else
  29. #include <stdint.h>
  30. #endif
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <limits.h>
  34. #include <float.h>
  35. #include "SDL_test.h"
  36. /**
  37. * Counter for fuzzer invocations
  38. */
  39. static int fuzzerInvocationCounter = 0;
  40. /**
  41. * Context for shared random number generator
  42. */
  43. static SDLTest_RandomContext rndContext;
  44. /*
  45. * Note: doxygen documentation markup for functions is in the header file.
  46. */
  47. void
  48. SDLTest_FuzzerInit(Uint64 execKey)
  49. {
  50. Uint32 a = (execKey >> 32) & 0x00000000FFFFFFFF;
  51. Uint32 b = execKey & 0x00000000FFFFFFFF;
  52. SDL_memset((void *)&rndContext, 0, sizeof(SDLTest_RandomContext));
  53. SDLTest_RandomInit(&rndContext, a, b);
  54. fuzzerInvocationCounter = 0;
  55. }
  56. int
  57. SDLTest_GetFuzzerInvocationCount()
  58. {
  59. return fuzzerInvocationCounter;
  60. }
  61. Uint8
  62. SDLTest_RandomUint8()
  63. {
  64. fuzzerInvocationCounter++;
  65. return (Uint8) SDLTest_RandomInt(&rndContext) & 0x000000FF;
  66. }
  67. Sint8
  68. SDLTest_RandomSint8()
  69. {
  70. fuzzerInvocationCounter++;
  71. return (Sint8) SDLTest_RandomInt(&rndContext) & 0x000000FF;
  72. }
  73. Uint16
  74. SDLTest_RandomUint16()
  75. {
  76. fuzzerInvocationCounter++;
  77. return (Uint16) SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
  78. }
  79. Sint16
  80. SDLTest_RandomSint16()
  81. {
  82. fuzzerInvocationCounter++;
  83. return (Sint16) SDLTest_RandomInt(&rndContext) & 0x0000FFFF;
  84. }
  85. Sint32
  86. SDLTest_RandomSint32()
  87. {
  88. fuzzerInvocationCounter++;
  89. return (Sint32) SDLTest_RandomInt(&rndContext);
  90. }
  91. Uint32
  92. SDLTest_RandomUint32()
  93. {
  94. fuzzerInvocationCounter++;
  95. return (Uint32) SDLTest_RandomInt(&rndContext);
  96. }
  97. Uint64
  98. SDLTest_RandomUint64()
  99. {
  100. Uint64 value = 0;
  101. Uint32 *vp = (void *)&value;
  102. fuzzerInvocationCounter++;
  103. vp[0] = SDLTest_RandomSint32();
  104. vp[1] = SDLTest_RandomSint32();
  105. return value;
  106. }
  107. Sint64
  108. SDLTest_RandomSint64()
  109. {
  110. Uint64 value = 0;
  111. Uint32 *vp = (void *)&value;
  112. fuzzerInvocationCounter++;
  113. vp[0] = SDLTest_RandomSint32();
  114. vp[1] = SDLTest_RandomSint32();
  115. return value;
  116. }
  117. Sint32
  118. SDLTest_RandomIntegerInRange(Sint32 pMin, Sint32 pMax)
  119. {
  120. Sint64 min = pMin;
  121. Sint64 max = pMax;
  122. Sint64 temp;
  123. Sint64 number;
  124. if(pMin > pMax) {
  125. temp = min;
  126. min = max;
  127. max = temp;
  128. } else if(pMin == pMax) {
  129. return (Sint32)min;
  130. }
  131. number = SDLTest_RandomUint32();
  132. /* invocation count increment in preceeding call */
  133. return (Sint32)((number % ((max + 1) - min)) + min);
  134. }
  135. /* !
  136. * Generates a unsigned boundary value between the given boundaries.
  137. * Boundary values are inclusive. See the examples below.
  138. * If boundary2 < boundary1, the values are swapped.
  139. * If boundary1 == boundary2, value of boundary1 will be returned
  140. *
  141. * Generating boundary values for Uint8:
  142. * BoundaryValues(UINT8_MAX, 10, 20, True) -> [10,11,19,20]
  143. * BoundaryValues(UINT8_MAX, 10, 20, False) -> [9,21]
  144. * BoundaryValues(UINT8_MAX, 0, 15, True) -> [0, 1, 14, 15]
  145. * BoundaryValues(UINT8_MAX, 0, 15, False) -> [16]
  146. * BoundaryValues(UINT8_MAX, 0, 0xFF, False) -> [0], error set
  147. *
  148. * Generator works the same for other types of unsigned integers.
  149. *
  150. * \param maxValue The biggest value that is acceptable for this data type.
  151. * For instance, for Uint8 -> 255, Uint16 -> 65536 etc.
  152. * \param boundary1 defines lower boundary
  153. * \param boundary2 defines upper boundary
  154. * \param validDomain Generate only for valid domain (for the data type)
  155. *
  156. * \returns Returns a random boundary value for the domain or 0 in case of error
  157. */
  158. Uint64
  159. SDLTest_GenerateUnsignedBoundaryValues(const Uint64 maxValue, Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
  160. {
  161. Uint64 b1, b2;
  162. Uint64 delta;
  163. Uint64 tempBuf[4];
  164. Uint8 index;
  165. /* Maybe swap */
  166. if (boundary1 > boundary2) {
  167. b1 = boundary2;
  168. b2 = boundary1;
  169. } else {
  170. b1 = boundary1;
  171. b2 = boundary2;
  172. }
  173. index = 0;
  174. if (validDomain == SDL_TRUE) {
  175. if (b1 == b2) {
  176. return b1;
  177. }
  178. /* Generate up to 4 values within bounds */
  179. delta = b2 - b1;
  180. if (delta < 4) {
  181. do {
  182. tempBuf[index] = b1 + index;
  183. index++;
  184. } while (index < delta);
  185. } else {
  186. tempBuf[index] = b1;
  187. index++;
  188. tempBuf[index] = b1 + 1;
  189. index++;
  190. tempBuf[index] = b2 - 1;
  191. index++;
  192. tempBuf[index] = b2;
  193. index++;
  194. }
  195. } else {
  196. /* Generate up to 2 values outside of bounds */
  197. if (b1 > 0) {
  198. tempBuf[index] = b1 - 1;
  199. index++;
  200. }
  201. if (b2 < maxValue) {
  202. tempBuf[index] = b2 + 1;
  203. index++;
  204. }
  205. }
  206. if (index == 0) {
  207. /* There are no valid boundaries */
  208. SDL_Unsupported();
  209. return 0;
  210. }
  211. return tempBuf[SDLTest_RandomUint8() % index];
  212. }
  213. Uint8
  214. SDLTest_RandomUint8BoundaryValue(Uint8 boundary1, Uint8 boundary2, SDL_bool validDomain)
  215. {
  216. /* max value for Uint8 */
  217. const Uint64 maxValue = UCHAR_MAX;
  218. return (Uint8)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
  219. (Uint64) boundary1, (Uint64) boundary2,
  220. validDomain);
  221. }
  222. Uint16
  223. SDLTest_RandomUint16BoundaryValue(Uint16 boundary1, Uint16 boundary2, SDL_bool validDomain)
  224. {
  225. /* max value for Uint16 */
  226. const Uint64 maxValue = USHRT_MAX;
  227. return (Uint16)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
  228. (Uint64) boundary1, (Uint64) boundary2,
  229. validDomain);
  230. }
  231. Uint32
  232. SDLTest_RandomUint32BoundaryValue(Uint32 boundary1, Uint32 boundary2, SDL_bool validDomain)
  233. {
  234. /* max value for Uint32 */
  235. #if ((ULONG_MAX) == (UINT_MAX))
  236. const Uint64 maxValue = ULONG_MAX;
  237. #else
  238. const Uint64 maxValue = UINT_MAX;
  239. #endif
  240. return (Uint32)SDLTest_GenerateUnsignedBoundaryValues(maxValue,
  241. (Uint64) boundary1, (Uint64) boundary2,
  242. validDomain);
  243. }
  244. Uint64
  245. SDLTest_RandomUint64BoundaryValue(Uint64 boundary1, Uint64 boundary2, SDL_bool validDomain)
  246. {
  247. /* max value for Uint64 */
  248. const Uint64 maxValue = ULLONG_MAX;
  249. return SDLTest_GenerateUnsignedBoundaryValues(maxValue,
  250. (Uint64) boundary1, (Uint64) boundary2,
  251. validDomain);
  252. }
  253. /* !
  254. * Generates a signed boundary value between the given boundaries.
  255. * Boundary values are inclusive. See the examples below.
  256. * If boundary2 < boundary1, the values are swapped.
  257. * If boundary1 == boundary2, value of boundary1 will be returned
  258. *
  259. * Generating boundary values for Sint8:
  260. * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, True) -> [-10,-9,19,20]
  261. * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -10, 20, False) -> [-11,21]
  262. * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -30, -15, True) -> [-30, -29, -16, -15]
  263. * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 15, False) -> [16]
  264. * SignedBoundaryValues(SCHAR_MIN, SCHAR_MAX, -127, 127, False) -> [0], error set
  265. *
  266. * Generator works the same for other types of signed integers.
  267. *
  268. * \param minValue The smallest value that is acceptable for this data type.
  269. * For instance, for Uint8 -> -127, etc.
  270. * \param maxValue The biggest value that is acceptable for this data type.
  271. * For instance, for Uint8 -> 127, etc.
  272. * \param boundary1 defines lower boundary
  273. * \param boundary2 defines upper boundary
  274. * \param validDomain Generate only for valid domain (for the data type)
  275. *
  276. * \returns Returns a random boundary value for the domain or 0 in case of error
  277. */
  278. Sint64
  279. SDLTest_GenerateSignedBoundaryValues(const Sint64 minValue, const Sint64 maxValue, Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
  280. {
  281. Sint64 b1, b2;
  282. Sint64 delta;
  283. Sint64 tempBuf[4];
  284. Uint8 index;
  285. /* Maybe swap */
  286. if (boundary1 > boundary2) {
  287. b1 = boundary2;
  288. b2 = boundary1;
  289. } else {
  290. b1 = boundary1;
  291. b2 = boundary2;
  292. }
  293. index = 0;
  294. if (validDomain == SDL_TRUE) {
  295. if (b1 == b2) {
  296. return b1;
  297. }
  298. /* Generate up to 4 values within bounds */
  299. delta = b2 - b1;
  300. if (delta < 4) {
  301. do {
  302. tempBuf[index] = b1 + index;
  303. index++;
  304. } while (index < delta);
  305. } else {
  306. tempBuf[index] = b1;
  307. index++;
  308. tempBuf[index] = b1 + 1;
  309. index++;
  310. tempBuf[index] = b2 - 1;
  311. index++;
  312. tempBuf[index] = b2;
  313. index++;
  314. }
  315. } else {
  316. /* Generate up to 2 values outside of bounds */
  317. if (b1 > minValue) {
  318. tempBuf[index] = b1 - 1;
  319. index++;
  320. }
  321. if (b2 < maxValue) {
  322. tempBuf[index] = b2 + 1;
  323. index++;
  324. }
  325. }
  326. if (index == 0) {
  327. /* There are no valid boundaries */
  328. SDL_Unsupported();
  329. return minValue;
  330. }
  331. return tempBuf[SDLTest_RandomUint8() % index];
  332. }
  333. Sint8
  334. SDLTest_RandomSint8BoundaryValue(Sint8 boundary1, Sint8 boundary2, SDL_bool validDomain)
  335. {
  336. /* min & max values for Sint8 */
  337. const Sint64 maxValue = SCHAR_MAX;
  338. const Sint64 minValue = SCHAR_MIN;
  339. return (Sint8)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
  340. (Sint64) boundary1, (Sint64) boundary2,
  341. validDomain);
  342. }
  343. Sint16
  344. SDLTest_RandomSint16BoundaryValue(Sint16 boundary1, Sint16 boundary2, SDL_bool validDomain)
  345. {
  346. /* min & max values for Sint16 */
  347. const Sint64 maxValue = SHRT_MAX;
  348. const Sint64 minValue = SHRT_MIN;
  349. return (Sint16)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
  350. (Sint64) boundary1, (Sint64) boundary2,
  351. validDomain);
  352. }
  353. Sint32
  354. SDLTest_RandomSint32BoundaryValue(Sint32 boundary1, Sint32 boundary2, SDL_bool validDomain)
  355. {
  356. /* min & max values for Sint32 */
  357. #if ((ULONG_MAX) == (UINT_MAX))
  358. const Sint64 maxValue = LONG_MAX;
  359. const Sint64 minValue = LONG_MIN;
  360. #else
  361. const Sint64 maxValue = INT_MAX;
  362. const Sint64 minValue = INT_MIN;
  363. #endif
  364. return (Sint32)SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
  365. (Sint64) boundary1, (Sint64) boundary2,
  366. validDomain);
  367. }
  368. Sint64
  369. SDLTest_RandomSint64BoundaryValue(Sint64 boundary1, Sint64 boundary2, SDL_bool validDomain)
  370. {
  371. /* min & max values for Sint64 */
  372. const Sint64 maxValue = LLONG_MAX;
  373. const Sint64 minValue = LLONG_MIN;
  374. return SDLTest_GenerateSignedBoundaryValues(minValue, maxValue,
  375. boundary1, boundary2,
  376. validDomain);
  377. }
  378. float
  379. SDLTest_RandomUnitFloat()
  380. {
  381. return (float) SDLTest_RandomUint32() / UINT_MAX;
  382. }
  383. float
  384. SDLTest_RandomFloat()
  385. {
  386. return (float) (SDLTest_RandomUnitDouble() * (double)2.0 * (double)FLT_MAX - (double)(FLT_MAX));
  387. }
  388. double
  389. SDLTest_RandomUnitDouble()
  390. {
  391. return (double) (SDLTest_RandomUint64() >> 11) * (1.0/9007199254740992.0);
  392. }
  393. double
  394. SDLTest_RandomDouble()
  395. {
  396. double r = 0.0;
  397. double s = 1.0;
  398. do {
  399. s /= UINT_MAX + 1.0;
  400. r += (double)SDLTest_RandomInt(&rndContext) * s;
  401. } while (s > DBL_EPSILON);
  402. fuzzerInvocationCounter++;
  403. return r;
  404. }
  405. char *
  406. SDLTest_RandomAsciiString()
  407. {
  408. return SDLTest_RandomAsciiStringWithMaximumLength(255);
  409. }
  410. char *
  411. SDLTest_RandomAsciiStringWithMaximumLength(int maxLength)
  412. {
  413. int size;
  414. if(maxLength < 1) {
  415. SDL_InvalidParamError("maxLength");
  416. return NULL;
  417. }
  418. size = (SDLTest_RandomUint32() % (maxLength + 1));
  419. return SDLTest_RandomAsciiStringOfSize(size);
  420. }
  421. char *
  422. SDLTest_RandomAsciiStringOfSize(int size)
  423. {
  424. char *string;
  425. int counter;
  426. if(size < 1) {
  427. SDL_InvalidParamError("size");
  428. return NULL;
  429. }
  430. string = (char *)SDL_malloc((size + 1) * sizeof(char));
  431. if (string==NULL) {
  432. return NULL;
  433. }
  434. for(counter = 0; counter < size; ++counter) {
  435. string[counter] = (char)SDLTest_RandomIntegerInRange(32, 126);
  436. }
  437. string[counter] = '\0';
  438. fuzzerInvocationCounter++;
  439. return string;
  440. }