SDL_test_fuzzer.c 13 KB

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