SDL_test_fuzzer.c 13 KB

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