SDL_test_fuzzer.c 13 KB

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